record-full.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871
  1. /* Process record and replay target for GDB, the GNU debugger.
  2. Copyright (C) 2013-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "gdbcmd.h"
  16. #include "regcache.h"
  17. #include "gdbthread.h"
  18. #include "inferior.h"
  19. #include "event-top.h"
  20. #include "completer.h"
  21. #include "arch-utils.h"
  22. #include "gdbcore.h"
  23. #include "exec.h"
  24. #include "record.h"
  25. #include "record-full.h"
  26. #include "elf-bfd.h"
  27. #include "gcore.h"
  28. #include "gdbsupport/event-loop.h"
  29. #include "inf-loop.h"
  30. #include "gdb_bfd.h"
  31. #include "observable.h"
  32. #include "infrun.h"
  33. #include "gdbsupport/gdb_unlinker.h"
  34. #include "gdbsupport/byte-vector.h"
  35. #include "async-event.h"
  36. #include <signal.h>
  37. /* This module implements "target record-full", also known as "process
  38. record and replay". This target sits on top of a "normal" target
  39. (a target that "has execution"), and provides a record and replay
  40. functionality, including reverse debugging.
  41. Target record has two modes: recording, and replaying.
  42. In record mode, we intercept the resume and wait methods.
  43. Whenever gdb resumes the target, we run the target in single step
  44. mode, and we build up an execution log in which, for each executed
  45. instruction, we record all changes in memory and register state.
  46. This is invisible to the user, to whom it just looks like an
  47. ordinary debugging session (except for performance degradation).
  48. In replay mode, instead of actually letting the inferior run as a
  49. process, we simulate its execution by playing back the recorded
  50. execution log. For each instruction in the log, we simulate the
  51. instruction's side effects by duplicating the changes that it would
  52. have made on memory and registers. */
  53. #define DEFAULT_RECORD_FULL_INSN_MAX_NUM 200000
  54. #define RECORD_FULL_IS_REPLAY \
  55. (record_full_list->next || ::execution_direction == EXEC_REVERSE)
  56. #define RECORD_FULL_FILE_MAGIC netorder32(0x20091016)
  57. /* These are the core structs of the process record functionality.
  58. A record_full_entry is a record of the value change of a register
  59. ("record_full_reg") or a part of memory ("record_full_mem"). And each
  60. instruction must have a struct record_full_entry ("record_full_end")
  61. that indicates that this is the last struct record_full_entry of this
  62. instruction.
  63. Each struct record_full_entry is linked to "record_full_list" by "prev"
  64. and "next" pointers. */
  65. struct record_full_mem_entry
  66. {
  67. CORE_ADDR addr;
  68. int len;
  69. /* Set this flag if target memory for this entry
  70. can no longer be accessed. */
  71. int mem_entry_not_accessible;
  72. union
  73. {
  74. gdb_byte *ptr;
  75. gdb_byte buf[sizeof (gdb_byte *)];
  76. } u;
  77. };
  78. struct record_full_reg_entry
  79. {
  80. unsigned short num;
  81. unsigned short len;
  82. union
  83. {
  84. gdb_byte *ptr;
  85. gdb_byte buf[2 * sizeof (gdb_byte *)];
  86. } u;
  87. };
  88. struct record_full_end_entry
  89. {
  90. enum gdb_signal sigval;
  91. ULONGEST insn_num;
  92. };
  93. enum record_full_type
  94. {
  95. record_full_end = 0,
  96. record_full_reg,
  97. record_full_mem
  98. };
  99. /* This is the data structure that makes up the execution log.
  100. The execution log consists of a single linked list of entries
  101. of type "struct record_full_entry". It is doubly linked so that it
  102. can be traversed in either direction.
  103. The start of the list is anchored by a struct called
  104. "record_full_first". The pointer "record_full_list" either points
  105. to the last entry that was added to the list (in record mode), or to
  106. the next entry in the list that will be executed (in replay mode).
  107. Each list element (struct record_full_entry), in addition to next
  108. and prev pointers, consists of a union of three entry types: mem,
  109. reg, and end. A field called "type" determines which entry type is
  110. represented by a given list element.
  111. Each instruction that is added to the execution log is represented
  112. by a variable number of list elements ('entries'). The instruction
  113. will have one "reg" entry for each register that is changed by
  114. executing the instruction (including the PC in every case). It
  115. will also have one "mem" entry for each memory change. Finally,
  116. each instruction will have an "end" entry that separates it from
  117. the changes associated with the next instruction. */
  118. struct record_full_entry
  119. {
  120. struct record_full_entry *prev;
  121. struct record_full_entry *next;
  122. enum record_full_type type;
  123. union
  124. {
  125. /* reg */
  126. struct record_full_reg_entry reg;
  127. /* mem */
  128. struct record_full_mem_entry mem;
  129. /* end */
  130. struct record_full_end_entry end;
  131. } u;
  132. };
  133. /* If true, query if PREC cannot record memory
  134. change of next instruction. */
  135. bool record_full_memory_query = false;
  136. struct record_full_core_buf_entry
  137. {
  138. struct record_full_core_buf_entry *prev;
  139. struct target_section *p;
  140. bfd_byte *buf;
  141. };
  142. /* Record buf with core target. */
  143. static detached_regcache *record_full_core_regbuf = NULL;
  144. static target_section_table record_full_core_sections;
  145. static struct record_full_core_buf_entry *record_full_core_buf_list = NULL;
  146. /* The following variables are used for managing the linked list that
  147. represents the execution log.
  148. record_full_first is the anchor that holds down the beginning of
  149. the list.
  150. record_full_list serves two functions:
  151. 1) In record mode, it anchors the end of the list.
  152. 2) In replay mode, it traverses the list and points to
  153. the next instruction that must be emulated.
  154. record_full_arch_list_head and record_full_arch_list_tail are used
  155. to manage a separate list, which is used to build up the change
  156. elements of the currently executing instruction during record mode.
  157. When this instruction has been completely annotated in the "arch
  158. list", it will be appended to the main execution log. */
  159. static struct record_full_entry record_full_first;
  160. static struct record_full_entry *record_full_list = &record_full_first;
  161. static struct record_full_entry *record_full_arch_list_head = NULL;
  162. static struct record_full_entry *record_full_arch_list_tail = NULL;
  163. /* true ask user. false auto delete the last struct record_full_entry. */
  164. static bool record_full_stop_at_limit = true;
  165. /* Maximum allowed number of insns in execution log. */
  166. static unsigned int record_full_insn_max_num
  167. = DEFAULT_RECORD_FULL_INSN_MAX_NUM;
  168. /* Actual count of insns presently in execution log. */
  169. static unsigned int record_full_insn_num = 0;
  170. /* Count of insns logged so far (may be larger
  171. than count of insns presently in execution log). */
  172. static ULONGEST record_full_insn_count;
  173. static const char record_longname[]
  174. = N_("Process record and replay target");
  175. static const char record_doc[]
  176. = N_("Log program while executing and replay execution from log.");
  177. /* Base class implementing functionality common to both the
  178. "record-full" and "record-core" targets. */
  179. class record_full_base_target : public target_ops
  180. {
  181. public:
  182. const target_info &info () const override = 0;
  183. strata stratum () const override { return record_stratum; }
  184. void close () override;
  185. void async (int) override;
  186. ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
  187. bool stopped_by_watchpoint () override;
  188. bool stopped_data_address (CORE_ADDR *) override;
  189. bool stopped_by_sw_breakpoint () override;
  190. bool supports_stopped_by_sw_breakpoint () override;
  191. bool stopped_by_hw_breakpoint () override;
  192. bool supports_stopped_by_hw_breakpoint () override;
  193. bool can_execute_reverse () override;
  194. /* Add bookmark target methods. */
  195. gdb_byte *get_bookmark (const char *, int) override;
  196. void goto_bookmark (const gdb_byte *, int) override;
  197. enum exec_direction_kind execution_direction () override;
  198. enum record_method record_method (ptid_t ptid) override;
  199. void info_record () override;
  200. void save_record (const char *filename) override;
  201. bool supports_delete_record () override;
  202. void delete_record () override;
  203. bool record_is_replaying (ptid_t ptid) override;
  204. bool record_will_replay (ptid_t ptid, int dir) override;
  205. void record_stop_replaying () override;
  206. void goto_record_begin () override;
  207. void goto_record_end () override;
  208. void goto_record (ULONGEST insn) override;
  209. };
  210. /* The "record-full" target. */
  211. static const target_info record_full_target_info = {
  212. "record-full",
  213. record_longname,
  214. record_doc,
  215. };
  216. class record_full_target final : public record_full_base_target
  217. {
  218. public:
  219. const target_info &info () const override
  220. { return record_full_target_info; }
  221. void resume (ptid_t, int, enum gdb_signal) override;
  222. void disconnect (const char *, int) override;
  223. void detach (inferior *, int) override;
  224. void mourn_inferior () override;
  225. void kill () override;
  226. void store_registers (struct regcache *, int) override;
  227. enum target_xfer_status xfer_partial (enum target_object object,
  228. const char *annex,
  229. gdb_byte *readbuf,
  230. const gdb_byte *writebuf,
  231. ULONGEST offset, ULONGEST len,
  232. ULONGEST *xfered_len) override;
  233. int insert_breakpoint (struct gdbarch *,
  234. struct bp_target_info *) override;
  235. int remove_breakpoint (struct gdbarch *,
  236. struct bp_target_info *,
  237. enum remove_bp_reason) override;
  238. };
  239. /* The "record-core" target. */
  240. static const target_info record_full_core_target_info = {
  241. "record-core",
  242. record_longname,
  243. record_doc,
  244. };
  245. class record_full_core_target final : public record_full_base_target
  246. {
  247. public:
  248. const target_info &info () const override
  249. { return record_full_core_target_info; }
  250. void resume (ptid_t, int, enum gdb_signal) override;
  251. void disconnect (const char *, int) override;
  252. void kill () override;
  253. void fetch_registers (struct regcache *regcache, int regno) override;
  254. void prepare_to_store (struct regcache *regcache) override;
  255. void store_registers (struct regcache *, int) override;
  256. enum target_xfer_status xfer_partial (enum target_object object,
  257. const char *annex,
  258. gdb_byte *readbuf,
  259. const gdb_byte *writebuf,
  260. ULONGEST offset, ULONGEST len,
  261. ULONGEST *xfered_len) override;
  262. int insert_breakpoint (struct gdbarch *,
  263. struct bp_target_info *) override;
  264. int remove_breakpoint (struct gdbarch *,
  265. struct bp_target_info *,
  266. enum remove_bp_reason) override;
  267. bool has_execution (inferior *inf) override;
  268. };
  269. static record_full_target record_full_ops;
  270. static record_full_core_target record_full_core_ops;
  271. void
  272. record_full_target::detach (inferior *inf, int from_tty)
  273. {
  274. record_detach (this, inf, from_tty);
  275. }
  276. void
  277. record_full_target::disconnect (const char *args, int from_tty)
  278. {
  279. record_disconnect (this, args, from_tty);
  280. }
  281. void
  282. record_full_core_target::disconnect (const char *args, int from_tty)
  283. {
  284. record_disconnect (this, args, from_tty);
  285. }
  286. void
  287. record_full_target::mourn_inferior ()
  288. {
  289. record_mourn_inferior (this);
  290. }
  291. void
  292. record_full_target::kill ()
  293. {
  294. record_kill (this);
  295. }
  296. /* See record-full.h. */
  297. int
  298. record_full_is_used (void)
  299. {
  300. struct target_ops *t;
  301. t = find_record_target ();
  302. return (t == &record_full_ops
  303. || t == &record_full_core_ops);
  304. }
  305. /* Command lists for "set/show record full". */
  306. static struct cmd_list_element *set_record_full_cmdlist;
  307. static struct cmd_list_element *show_record_full_cmdlist;
  308. /* Command list for "record full". */
  309. static struct cmd_list_element *record_full_cmdlist;
  310. static void record_full_goto_insn (struct record_full_entry *entry,
  311. enum exec_direction_kind dir);
  312. /* Alloc and free functions for record_full_reg, record_full_mem, and
  313. record_full_end entries. */
  314. /* Alloc a record_full_reg record entry. */
  315. static inline struct record_full_entry *
  316. record_full_reg_alloc (struct regcache *regcache, int regnum)
  317. {
  318. struct record_full_entry *rec;
  319. struct gdbarch *gdbarch = regcache->arch ();
  320. rec = XCNEW (struct record_full_entry);
  321. rec->type = record_full_reg;
  322. rec->u.reg.num = regnum;
  323. rec->u.reg.len = register_size (gdbarch, regnum);
  324. if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
  325. rec->u.reg.u.ptr = (gdb_byte *) xmalloc (rec->u.reg.len);
  326. return rec;
  327. }
  328. /* Free a record_full_reg record entry. */
  329. static inline void
  330. record_full_reg_release (struct record_full_entry *rec)
  331. {
  332. gdb_assert (rec->type == record_full_reg);
  333. if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
  334. xfree (rec->u.reg.u.ptr);
  335. xfree (rec);
  336. }
  337. /* Alloc a record_full_mem record entry. */
  338. static inline struct record_full_entry *
  339. record_full_mem_alloc (CORE_ADDR addr, int len)
  340. {
  341. struct record_full_entry *rec;
  342. rec = XCNEW (struct record_full_entry);
  343. rec->type = record_full_mem;
  344. rec->u.mem.addr = addr;
  345. rec->u.mem.len = len;
  346. if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
  347. rec->u.mem.u.ptr = (gdb_byte *) xmalloc (len);
  348. return rec;
  349. }
  350. /* Free a record_full_mem record entry. */
  351. static inline void
  352. record_full_mem_release (struct record_full_entry *rec)
  353. {
  354. gdb_assert (rec->type == record_full_mem);
  355. if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
  356. xfree (rec->u.mem.u.ptr);
  357. xfree (rec);
  358. }
  359. /* Alloc a record_full_end record entry. */
  360. static inline struct record_full_entry *
  361. record_full_end_alloc (void)
  362. {
  363. struct record_full_entry *rec;
  364. rec = XCNEW (struct record_full_entry);
  365. rec->type = record_full_end;
  366. return rec;
  367. }
  368. /* Free a record_full_end record entry. */
  369. static inline void
  370. record_full_end_release (struct record_full_entry *rec)
  371. {
  372. xfree (rec);
  373. }
  374. /* Free one record entry, any type.
  375. Return entry->type, in case caller wants to know. */
  376. static inline enum record_full_type
  377. record_full_entry_release (struct record_full_entry *rec)
  378. {
  379. enum record_full_type type = rec->type;
  380. switch (type) {
  381. case record_full_reg:
  382. record_full_reg_release (rec);
  383. break;
  384. case record_full_mem:
  385. record_full_mem_release (rec);
  386. break;
  387. case record_full_end:
  388. record_full_end_release (rec);
  389. break;
  390. }
  391. return type;
  392. }
  393. /* Free all record entries in list pointed to by REC. */
  394. static void
  395. record_full_list_release (struct record_full_entry *rec)
  396. {
  397. if (!rec)
  398. return;
  399. while (rec->next)
  400. rec = rec->next;
  401. while (rec->prev)
  402. {
  403. rec = rec->prev;
  404. record_full_entry_release (rec->next);
  405. }
  406. if (rec == &record_full_first)
  407. {
  408. record_full_insn_num = 0;
  409. record_full_first.next = NULL;
  410. }
  411. else
  412. record_full_entry_release (rec);
  413. }
  414. /* Free all record entries forward of the given list position. */
  415. static void
  416. record_full_list_release_following (struct record_full_entry *rec)
  417. {
  418. struct record_full_entry *tmp = rec->next;
  419. rec->next = NULL;
  420. while (tmp)
  421. {
  422. rec = tmp->next;
  423. if (record_full_entry_release (tmp) == record_full_end)
  424. {
  425. record_full_insn_num--;
  426. record_full_insn_count--;
  427. }
  428. tmp = rec;
  429. }
  430. }
  431. /* Delete the first instruction from the beginning of the log, to make
  432. room for adding a new instruction at the end of the log.
  433. Note -- this function does not modify record_full_insn_num. */
  434. static void
  435. record_full_list_release_first (void)
  436. {
  437. struct record_full_entry *tmp;
  438. if (!record_full_first.next)
  439. return;
  440. /* Loop until a record_full_end. */
  441. while (1)
  442. {
  443. /* Cut record_full_first.next out of the linked list. */
  444. tmp = record_full_first.next;
  445. record_full_first.next = tmp->next;
  446. tmp->next->prev = &record_full_first;
  447. /* tmp is now isolated, and can be deleted. */
  448. if (record_full_entry_release (tmp) == record_full_end)
  449. break; /* End loop at first record_full_end. */
  450. if (!record_full_first.next)
  451. {
  452. gdb_assert (record_full_insn_num == 1);
  453. break; /* End loop when list is empty. */
  454. }
  455. }
  456. }
  457. /* Add a struct record_full_entry to record_full_arch_list. */
  458. static void
  459. record_full_arch_list_add (struct record_full_entry *rec)
  460. {
  461. if (record_debug > 1)
  462. gdb_printf (gdb_stdlog,
  463. "Process record: record_full_arch_list_add %s.\n",
  464. host_address_to_string (rec));
  465. if (record_full_arch_list_tail)
  466. {
  467. record_full_arch_list_tail->next = rec;
  468. rec->prev = record_full_arch_list_tail;
  469. record_full_arch_list_tail = rec;
  470. }
  471. else
  472. {
  473. record_full_arch_list_head = rec;
  474. record_full_arch_list_tail = rec;
  475. }
  476. }
  477. /* Return the value storage location of a record entry. */
  478. static inline gdb_byte *
  479. record_full_get_loc (struct record_full_entry *rec)
  480. {
  481. switch (rec->type) {
  482. case record_full_mem:
  483. if (rec->u.mem.len > sizeof (rec->u.mem.u.buf))
  484. return rec->u.mem.u.ptr;
  485. else
  486. return rec->u.mem.u.buf;
  487. case record_full_reg:
  488. if (rec->u.reg.len > sizeof (rec->u.reg.u.buf))
  489. return rec->u.reg.u.ptr;
  490. else
  491. return rec->u.reg.u.buf;
  492. case record_full_end:
  493. default:
  494. gdb_assert_not_reached ("unexpected record_full_entry type");
  495. return NULL;
  496. }
  497. }
  498. /* Record the value of a register NUM to record_full_arch_list. */
  499. int
  500. record_full_arch_list_add_reg (struct regcache *regcache, int regnum)
  501. {
  502. struct record_full_entry *rec;
  503. if (record_debug > 1)
  504. gdb_printf (gdb_stdlog,
  505. "Process record: add register num = %d to "
  506. "record list.\n",
  507. regnum);
  508. rec = record_full_reg_alloc (regcache, regnum);
  509. regcache->raw_read (regnum, record_full_get_loc (rec));
  510. record_full_arch_list_add (rec);
  511. return 0;
  512. }
  513. /* Record the value of a region of memory whose address is ADDR and
  514. length is LEN to record_full_arch_list. */
  515. int
  516. record_full_arch_list_add_mem (CORE_ADDR addr, int len)
  517. {
  518. struct record_full_entry *rec;
  519. if (record_debug > 1)
  520. gdb_printf (gdb_stdlog,
  521. "Process record: add mem addr = %s len = %d to "
  522. "record list.\n",
  523. paddress (target_gdbarch (), addr), len);
  524. if (!addr) /* FIXME: Why? Some arch must permit it... */
  525. return 0;
  526. rec = record_full_mem_alloc (addr, len);
  527. if (record_read_memory (target_gdbarch (), addr,
  528. record_full_get_loc (rec), len))
  529. {
  530. record_full_mem_release (rec);
  531. return -1;
  532. }
  533. record_full_arch_list_add (rec);
  534. return 0;
  535. }
  536. /* Add a record_full_end type struct record_full_entry to
  537. record_full_arch_list. */
  538. int
  539. record_full_arch_list_add_end (void)
  540. {
  541. struct record_full_entry *rec;
  542. if (record_debug > 1)
  543. gdb_printf (gdb_stdlog,
  544. "Process record: add end to arch list.\n");
  545. rec = record_full_end_alloc ();
  546. rec->u.end.sigval = GDB_SIGNAL_0;
  547. rec->u.end.insn_num = ++record_full_insn_count;
  548. record_full_arch_list_add (rec);
  549. return 0;
  550. }
  551. static void
  552. record_full_check_insn_num (void)
  553. {
  554. if (record_full_insn_num == record_full_insn_max_num)
  555. {
  556. /* Ask user what to do. */
  557. if (record_full_stop_at_limit)
  558. {
  559. if (!yquery (_("Do you want to auto delete previous execution "
  560. "log entries when record/replay buffer becomes "
  561. "full (record full stop-at-limit)?")))
  562. error (_("Process record: stopped by user."));
  563. record_full_stop_at_limit = 0;
  564. }
  565. }
  566. }
  567. /* Before inferior step (when GDB record the running message, inferior
  568. only can step), GDB will call this function to record the values to
  569. record_full_list. This function will call gdbarch_process_record to
  570. record the running message of inferior and set them to
  571. record_full_arch_list, and add it to record_full_list. */
  572. static void
  573. record_full_message (struct regcache *regcache, enum gdb_signal signal)
  574. {
  575. int ret;
  576. struct gdbarch *gdbarch = regcache->arch ();
  577. try
  578. {
  579. record_full_arch_list_head = NULL;
  580. record_full_arch_list_tail = NULL;
  581. /* Check record_full_insn_num. */
  582. record_full_check_insn_num ();
  583. /* If gdb sends a signal value to target_resume,
  584. save it in the 'end' field of the previous instruction.
  585. Maybe process record should record what really happened,
  586. rather than what gdb pretends has happened.
  587. So if Linux delivered the signal to the child process during
  588. the record mode, we will record it and deliver it again in
  589. the replay mode.
  590. If user says "ignore this signal" during the record mode, then
  591. it will be ignored again during the replay mode (no matter if
  592. the user says something different, like "deliver this signal"
  593. during the replay mode).
  594. User should understand that nothing he does during the replay
  595. mode will change the behavior of the child. If he tries,
  596. then that is a user error.
  597. But we should still deliver the signal to gdb during the replay,
  598. if we delivered it during the recording. Therefore we should
  599. record the signal during record_full_wait, not
  600. record_full_resume. */
  601. if (record_full_list != &record_full_first) /* FIXME better way
  602. to check */
  603. {
  604. gdb_assert (record_full_list->type == record_full_end);
  605. record_full_list->u.end.sigval = signal;
  606. }
  607. if (signal == GDB_SIGNAL_0
  608. || !gdbarch_process_record_signal_p (gdbarch))
  609. ret = gdbarch_process_record (gdbarch,
  610. regcache,
  611. regcache_read_pc (regcache));
  612. else
  613. ret = gdbarch_process_record_signal (gdbarch,
  614. regcache,
  615. signal);
  616. if (ret > 0)
  617. error (_("Process record: inferior program stopped."));
  618. if (ret < 0)
  619. error (_("Process record: failed to record execution log."));
  620. }
  621. catch (const gdb_exception &ex)
  622. {
  623. record_full_list_release (record_full_arch_list_tail);
  624. throw;
  625. }
  626. record_full_list->next = record_full_arch_list_head;
  627. record_full_arch_list_head->prev = record_full_list;
  628. record_full_list = record_full_arch_list_tail;
  629. if (record_full_insn_num == record_full_insn_max_num)
  630. record_full_list_release_first ();
  631. else
  632. record_full_insn_num++;
  633. }
  634. static bool
  635. record_full_message_wrapper_safe (struct regcache *regcache,
  636. enum gdb_signal signal)
  637. {
  638. try
  639. {
  640. record_full_message (regcache, signal);
  641. }
  642. catch (const gdb_exception &ex)
  643. {
  644. exception_print (gdb_stderr, ex);
  645. return false;
  646. }
  647. return true;
  648. }
  649. /* Set to 1 if record_full_store_registers and record_full_xfer_partial
  650. doesn't need record. */
  651. static int record_full_gdb_operation_disable = 0;
  652. scoped_restore_tmpl<int>
  653. record_full_gdb_operation_disable_set (void)
  654. {
  655. return make_scoped_restore (&record_full_gdb_operation_disable, 1);
  656. }
  657. /* Flag set to TRUE for target_stopped_by_watchpoint. */
  658. static enum target_stop_reason record_full_stop_reason
  659. = TARGET_STOPPED_BY_NO_REASON;
  660. /* Execute one instruction from the record log. Each instruction in
  661. the log will be represented by an arbitrary sequence of register
  662. entries and memory entries, followed by an 'end' entry. */
  663. static inline void
  664. record_full_exec_insn (struct regcache *regcache,
  665. struct gdbarch *gdbarch,
  666. struct record_full_entry *entry)
  667. {
  668. switch (entry->type)
  669. {
  670. case record_full_reg: /* reg */
  671. {
  672. gdb::byte_vector reg (entry->u.reg.len);
  673. if (record_debug > 1)
  674. gdb_printf (gdb_stdlog,
  675. "Process record: record_full_reg %s to "
  676. "inferior num = %d.\n",
  677. host_address_to_string (entry),
  678. entry->u.reg.num);
  679. regcache->cooked_read (entry->u.reg.num, reg.data ());
  680. regcache->cooked_write (entry->u.reg.num, record_full_get_loc (entry));
  681. memcpy (record_full_get_loc (entry), reg.data (), entry->u.reg.len);
  682. }
  683. break;
  684. case record_full_mem: /* mem */
  685. {
  686. /* Nothing to do if the entry is flagged not_accessible. */
  687. if (!entry->u.mem.mem_entry_not_accessible)
  688. {
  689. gdb::byte_vector mem (entry->u.mem.len);
  690. if (record_debug > 1)
  691. gdb_printf (gdb_stdlog,
  692. "Process record: record_full_mem %s to "
  693. "inferior addr = %s len = %d.\n",
  694. host_address_to_string (entry),
  695. paddress (gdbarch, entry->u.mem.addr),
  696. entry->u.mem.len);
  697. if (record_read_memory (gdbarch,
  698. entry->u.mem.addr, mem.data (),
  699. entry->u.mem.len))
  700. entry->u.mem.mem_entry_not_accessible = 1;
  701. else
  702. {
  703. if (target_write_memory (entry->u.mem.addr,
  704. record_full_get_loc (entry),
  705. entry->u.mem.len))
  706. {
  707. entry->u.mem.mem_entry_not_accessible = 1;
  708. if (record_debug)
  709. warning (_("Process record: error writing memory at "
  710. "addr = %s len = %d."),
  711. paddress (gdbarch, entry->u.mem.addr),
  712. entry->u.mem.len);
  713. }
  714. else
  715. {
  716. memcpy (record_full_get_loc (entry), mem.data (),
  717. entry->u.mem.len);
  718. /* We've changed memory --- check if a hardware
  719. watchpoint should trap. Note that this
  720. presently assumes the target beneath supports
  721. continuable watchpoints. On non-continuable
  722. watchpoints target, we'll want to check this
  723. _before_ actually doing the memory change, and
  724. not doing the change at all if the watchpoint
  725. traps. */
  726. if (hardware_watchpoint_inserted_in_range
  727. (regcache->aspace (),
  728. entry->u.mem.addr, entry->u.mem.len))
  729. record_full_stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
  730. }
  731. }
  732. }
  733. }
  734. break;
  735. }
  736. }
  737. static void record_full_restore (void);
  738. /* Asynchronous signal handle registered as event loop source for when
  739. we have pending events ready to be passed to the core. */
  740. static struct async_event_handler *record_full_async_inferior_event_token;
  741. static void
  742. record_full_async_inferior_event_handler (gdb_client_data data)
  743. {
  744. inferior_event_handler (INF_REG_EVENT);
  745. }
  746. /* Open the process record target for 'core' files. */
  747. static void
  748. record_full_core_open_1 (const char *name, int from_tty)
  749. {
  750. struct regcache *regcache = get_current_regcache ();
  751. int regnum = gdbarch_num_regs (regcache->arch ());
  752. int i;
  753. /* Get record_full_core_regbuf. */
  754. target_fetch_registers (regcache, -1);
  755. record_full_core_regbuf = new detached_regcache (regcache->arch (), false);
  756. for (i = 0; i < regnum; i ++)
  757. record_full_core_regbuf->raw_supply (i, *regcache);
  758. record_full_core_sections = build_section_table (core_bfd);
  759. current_inferior ()->push_target (&record_full_core_ops);
  760. record_full_restore ();
  761. }
  762. /* Open the process record target for 'live' processes. */
  763. static void
  764. record_full_open_1 (const char *name, int from_tty)
  765. {
  766. if (record_debug)
  767. gdb_printf (gdb_stdlog, "Process record: record_full_open_1\n");
  768. /* check exec */
  769. if (!target_has_execution ())
  770. error (_("Process record: the program is not being run."));
  771. if (non_stop)
  772. error (_("Process record target can't debug inferior in non-stop mode "
  773. "(non-stop)."));
  774. if (!gdbarch_process_record_p (target_gdbarch ()))
  775. error (_("Process record: the current architecture doesn't support "
  776. "record function."));
  777. current_inferior ()->push_target (&record_full_ops);
  778. }
  779. static void record_full_init_record_breakpoints (void);
  780. /* Open the process record target. */
  781. static void
  782. record_full_open (const char *name, int from_tty)
  783. {
  784. if (record_debug)
  785. gdb_printf (gdb_stdlog, "Process record: record_full_open\n");
  786. record_preopen ();
  787. /* Reset */
  788. record_full_insn_num = 0;
  789. record_full_insn_count = 0;
  790. record_full_list = &record_full_first;
  791. record_full_list->next = NULL;
  792. if (core_bfd)
  793. record_full_core_open_1 (name, from_tty);
  794. else
  795. record_full_open_1 (name, from_tty);
  796. /* Register extra event sources in the event loop. */
  797. record_full_async_inferior_event_token
  798. = create_async_event_handler (record_full_async_inferior_event_handler,
  799. NULL, "record-full");
  800. record_full_init_record_breakpoints ();
  801. gdb::observers::record_changed.notify (current_inferior (), 1, "full", NULL);
  802. }
  803. /* "close" target method. Close the process record target. */
  804. void
  805. record_full_base_target::close ()
  806. {
  807. struct record_full_core_buf_entry *entry;
  808. if (record_debug)
  809. gdb_printf (gdb_stdlog, "Process record: record_full_close\n");
  810. record_full_list_release (record_full_list);
  811. /* Release record_full_core_regbuf. */
  812. if (record_full_core_regbuf)
  813. {
  814. delete record_full_core_regbuf;
  815. record_full_core_regbuf = NULL;
  816. }
  817. /* Release record_full_core_buf_list. */
  818. while (record_full_core_buf_list)
  819. {
  820. entry = record_full_core_buf_list;
  821. record_full_core_buf_list = record_full_core_buf_list->prev;
  822. xfree (entry);
  823. }
  824. if (record_full_async_inferior_event_token)
  825. delete_async_event_handler (&record_full_async_inferior_event_token);
  826. }
  827. /* "async" target method. */
  828. void
  829. record_full_base_target::async (int enable)
  830. {
  831. if (enable)
  832. mark_async_event_handler (record_full_async_inferior_event_token);
  833. else
  834. clear_async_event_handler (record_full_async_inferior_event_token);
  835. beneath ()->async (enable);
  836. }
  837. /* The PTID and STEP arguments last passed to
  838. record_full_target::resume. */
  839. static ptid_t record_full_resume_ptid = null_ptid;
  840. static int record_full_resume_step = 0;
  841. /* True if we've been resumed, and so each record_full_wait call should
  842. advance execution. If this is false, record_full_wait will return a
  843. TARGET_WAITKIND_IGNORE. */
  844. static int record_full_resumed = 0;
  845. /* The execution direction of the last resume we got. This is
  846. necessary for async mode. Vis (order is not strictly accurate):
  847. 1. user has the global execution direction set to forward
  848. 2. user does a reverse-step command
  849. 3. record_full_resume is called with global execution direction
  850. temporarily switched to reverse
  851. 4. GDB's execution direction is reverted back to forward
  852. 5. target record notifies event loop there's an event to handle
  853. 6. infrun asks the target which direction was it going, and switches
  854. the global execution direction accordingly (to reverse)
  855. 7. infrun polls an event out of the record target, and handles it
  856. 8. GDB goes back to the event loop, and goto #4.
  857. */
  858. static enum exec_direction_kind record_full_execution_dir = EXEC_FORWARD;
  859. /* "resume" target method. Resume the process record target. */
  860. void
  861. record_full_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
  862. {
  863. record_full_resume_ptid = inferior_ptid;
  864. record_full_resume_step = step;
  865. record_full_resumed = 1;
  866. record_full_execution_dir = ::execution_direction;
  867. if (!RECORD_FULL_IS_REPLAY)
  868. {
  869. struct gdbarch *gdbarch = target_thread_architecture (ptid);
  870. record_full_message (get_current_regcache (), signal);
  871. if (!step)
  872. {
  873. /* This is not hard single step. */
  874. if (!gdbarch_software_single_step_p (gdbarch))
  875. {
  876. /* This is a normal continue. */
  877. step = 1;
  878. }
  879. else
  880. {
  881. /* This arch supports soft single step. */
  882. if (thread_has_single_step_breakpoints_set (inferior_thread ()))
  883. {
  884. /* This is a soft single step. */
  885. record_full_resume_step = 1;
  886. }
  887. else
  888. step = !insert_single_step_breakpoints (gdbarch);
  889. }
  890. }
  891. /* Make sure the target beneath reports all signals. */
  892. target_pass_signals ({});
  893. this->beneath ()->resume (ptid, step, signal);
  894. }
  895. }
  896. static int record_full_get_sig = 0;
  897. /* SIGINT signal handler, registered by "wait" method. */
  898. static void
  899. record_full_sig_handler (int signo)
  900. {
  901. if (record_debug)
  902. gdb_printf (gdb_stdlog, "Process record: get a signal\n");
  903. /* It will break the running inferior in replay mode. */
  904. record_full_resume_step = 1;
  905. /* It will let record_full_wait set inferior status to get the signal
  906. SIGINT. */
  907. record_full_get_sig = 1;
  908. }
  909. /* "wait" target method for process record target.
  910. In record mode, the target is always run in singlestep mode
  911. (even when gdb says to continue). The wait method intercepts
  912. the stop events and determines which ones are to be passed on to
  913. gdb. Most stop events are just singlestep events that gdb is not
  914. to know about, so the wait method just records them and keeps
  915. singlestepping.
  916. In replay mode, this function emulates the recorded execution log,
  917. one instruction at a time (forward or backward), and determines
  918. where to stop. */
  919. static ptid_t
  920. record_full_wait_1 (struct target_ops *ops,
  921. ptid_t ptid, struct target_waitstatus *status,
  922. target_wait_flags options)
  923. {
  924. scoped_restore restore_operation_disable
  925. = record_full_gdb_operation_disable_set ();
  926. if (record_debug)
  927. gdb_printf (gdb_stdlog,
  928. "Process record: record_full_wait "
  929. "record_full_resume_step = %d, "
  930. "record_full_resumed = %d, direction=%s\n",
  931. record_full_resume_step, record_full_resumed,
  932. record_full_execution_dir == EXEC_FORWARD
  933. ? "forward" : "reverse");
  934. if (!record_full_resumed)
  935. {
  936. gdb_assert ((options & TARGET_WNOHANG) != 0);
  937. /* No interesting event. */
  938. status->set_ignore ();
  939. return minus_one_ptid;
  940. }
  941. record_full_get_sig = 0;
  942. signal (SIGINT, record_full_sig_handler);
  943. record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
  944. if (!RECORD_FULL_IS_REPLAY && ops != &record_full_core_ops)
  945. {
  946. if (record_full_resume_step)
  947. {
  948. /* This is a single step. */
  949. return ops->beneath ()->wait (ptid, status, options);
  950. }
  951. else
  952. {
  953. /* This is not a single step. */
  954. ptid_t ret;
  955. CORE_ADDR tmp_pc;
  956. struct gdbarch *gdbarch
  957. = target_thread_architecture (record_full_resume_ptid);
  958. while (1)
  959. {
  960. ret = ops->beneath ()->wait (ptid, status, options);
  961. if (status->kind () == TARGET_WAITKIND_IGNORE)
  962. {
  963. if (record_debug)
  964. gdb_printf (gdb_stdlog,
  965. "Process record: record_full_wait "
  966. "target beneath not done yet\n");
  967. return ret;
  968. }
  969. for (thread_info *tp : all_non_exited_threads ())
  970. delete_single_step_breakpoints (tp);
  971. if (record_full_resume_step)
  972. return ret;
  973. /* Is this a SIGTRAP? */
  974. if (status->kind () == TARGET_WAITKIND_STOPPED
  975. && status->sig () == GDB_SIGNAL_TRAP)
  976. {
  977. struct regcache *regcache;
  978. enum target_stop_reason *stop_reason_p
  979. = &record_full_stop_reason;
  980. /* Yes -- this is likely our single-step finishing,
  981. but check if there's any reason the core would be
  982. interested in the event. */
  983. registers_changed ();
  984. switch_to_thread (current_inferior ()->process_target (),
  985. ret);
  986. regcache = get_current_regcache ();
  987. tmp_pc = regcache_read_pc (regcache);
  988. const struct address_space *aspace = regcache->aspace ();
  989. if (target_stopped_by_watchpoint ())
  990. {
  991. /* Always interested in watchpoints. */
  992. }
  993. else if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
  994. stop_reason_p))
  995. {
  996. /* There is a breakpoint here. Let the core
  997. handle it. */
  998. }
  999. else
  1000. {
  1001. /* This is a single-step trap. Record the
  1002. insn and issue another step.
  1003. FIXME: this part can be a random SIGTRAP too.
  1004. But GDB cannot handle it. */
  1005. int step = 1;
  1006. if (!record_full_message_wrapper_safe (regcache,
  1007. GDB_SIGNAL_0))
  1008. {
  1009. status->set_stopped (GDB_SIGNAL_0);
  1010. break;
  1011. }
  1012. process_stratum_target *proc_target
  1013. = current_inferior ()->process_target ();
  1014. if (gdbarch_software_single_step_p (gdbarch))
  1015. {
  1016. /* Try to insert the software single step breakpoint.
  1017. If insert success, set step to 0. */
  1018. set_executing (proc_target, inferior_ptid, false);
  1019. SCOPE_EXIT
  1020. {
  1021. set_executing (proc_target, inferior_ptid, true);
  1022. };
  1023. reinit_frame_cache ();
  1024. step = !insert_single_step_breakpoints (gdbarch);
  1025. }
  1026. if (record_debug)
  1027. gdb_printf (gdb_stdlog,
  1028. "Process record: record_full_wait "
  1029. "issuing one more step in the "
  1030. "target beneath\n");
  1031. ops->beneath ()->resume (ptid, step, GDB_SIGNAL_0);
  1032. proc_target->commit_resumed_state = true;
  1033. proc_target->commit_resumed ();
  1034. proc_target->commit_resumed_state = false;
  1035. continue;
  1036. }
  1037. }
  1038. /* The inferior is broken by a breakpoint or a signal. */
  1039. break;
  1040. }
  1041. return ret;
  1042. }
  1043. }
  1044. else
  1045. {
  1046. switch_to_thread (current_inferior ()->process_target (),
  1047. record_full_resume_ptid);
  1048. struct regcache *regcache = get_current_regcache ();
  1049. struct gdbarch *gdbarch = regcache->arch ();
  1050. const struct address_space *aspace = regcache->aspace ();
  1051. int continue_flag = 1;
  1052. int first_record_full_end = 1;
  1053. try
  1054. {
  1055. CORE_ADDR tmp_pc;
  1056. record_full_stop_reason = TARGET_STOPPED_BY_NO_REASON;
  1057. status->set_stopped (GDB_SIGNAL_0);
  1058. /* Check breakpoint when forward execute. */
  1059. if (execution_direction == EXEC_FORWARD)
  1060. {
  1061. tmp_pc = regcache_read_pc (regcache);
  1062. if (record_check_stopped_by_breakpoint (aspace, tmp_pc,
  1063. &record_full_stop_reason))
  1064. {
  1065. if (record_debug)
  1066. gdb_printf (gdb_stdlog,
  1067. "Process record: break at %s.\n",
  1068. paddress (gdbarch, tmp_pc));
  1069. goto replay_out;
  1070. }
  1071. }
  1072. /* If GDB is in terminal_inferior mode, it will not get the
  1073. signal. And in GDB replay mode, GDB doesn't need to be
  1074. in terminal_inferior mode, because inferior will not
  1075. executed. Then set it to terminal_ours to make GDB get
  1076. the signal. */
  1077. target_terminal::ours ();
  1078. /* In EXEC_FORWARD mode, record_full_list points to the tail of prev
  1079. instruction. */
  1080. if (execution_direction == EXEC_FORWARD && record_full_list->next)
  1081. record_full_list = record_full_list->next;
  1082. /* Loop over the record_full_list, looking for the next place to
  1083. stop. */
  1084. do
  1085. {
  1086. /* Check for beginning and end of log. */
  1087. if (execution_direction == EXEC_REVERSE
  1088. && record_full_list == &record_full_first)
  1089. {
  1090. /* Hit beginning of record log in reverse. */
  1091. status->set_no_history ();
  1092. break;
  1093. }
  1094. if (execution_direction != EXEC_REVERSE
  1095. && !record_full_list->next)
  1096. {
  1097. /* Hit end of record log going forward. */
  1098. status->set_no_history ();
  1099. break;
  1100. }
  1101. record_full_exec_insn (regcache, gdbarch, record_full_list);
  1102. if (record_full_list->type == record_full_end)
  1103. {
  1104. if (record_debug > 1)
  1105. gdb_printf
  1106. (gdb_stdlog,
  1107. "Process record: record_full_end %s to "
  1108. "inferior.\n",
  1109. host_address_to_string (record_full_list));
  1110. if (first_record_full_end
  1111. && execution_direction == EXEC_REVERSE)
  1112. {
  1113. /* When reverse execute, the first
  1114. record_full_end is the part of current
  1115. instruction. */
  1116. first_record_full_end = 0;
  1117. }
  1118. else
  1119. {
  1120. /* In EXEC_REVERSE mode, this is the
  1121. record_full_end of prev instruction. In
  1122. EXEC_FORWARD mode, this is the
  1123. record_full_end of current instruction. */
  1124. /* step */
  1125. if (record_full_resume_step)
  1126. {
  1127. if (record_debug > 1)
  1128. gdb_printf (gdb_stdlog,
  1129. "Process record: step.\n");
  1130. continue_flag = 0;
  1131. }
  1132. /* check breakpoint */
  1133. tmp_pc = regcache_read_pc (regcache);
  1134. if (record_check_stopped_by_breakpoint
  1135. (aspace, tmp_pc, &record_full_stop_reason))
  1136. {
  1137. if (record_debug)
  1138. gdb_printf (gdb_stdlog,
  1139. "Process record: break "
  1140. "at %s.\n",
  1141. paddress (gdbarch, tmp_pc));
  1142. continue_flag = 0;
  1143. }
  1144. if (record_full_stop_reason
  1145. == TARGET_STOPPED_BY_WATCHPOINT)
  1146. {
  1147. if (record_debug)
  1148. gdb_printf (gdb_stdlog,
  1149. "Process record: hit hw "
  1150. "watchpoint.\n");
  1151. continue_flag = 0;
  1152. }
  1153. /* Check target signal */
  1154. if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
  1155. /* FIXME: better way to check */
  1156. continue_flag = 0;
  1157. }
  1158. }
  1159. if (continue_flag)
  1160. {
  1161. if (execution_direction == EXEC_REVERSE)
  1162. {
  1163. if (record_full_list->prev)
  1164. record_full_list = record_full_list->prev;
  1165. }
  1166. else
  1167. {
  1168. if (record_full_list->next)
  1169. record_full_list = record_full_list->next;
  1170. }
  1171. }
  1172. }
  1173. while (continue_flag);
  1174. replay_out:
  1175. if (status->kind () == TARGET_WAITKIND_STOPPED)
  1176. {
  1177. if (record_full_get_sig)
  1178. status->set_stopped (GDB_SIGNAL_INT);
  1179. else if (record_full_list->u.end.sigval != GDB_SIGNAL_0)
  1180. /* FIXME: better way to check */
  1181. status->set_stopped (record_full_list->u.end.sigval);
  1182. else
  1183. status->set_stopped (GDB_SIGNAL_TRAP);
  1184. }
  1185. }
  1186. catch (const gdb_exception &ex)
  1187. {
  1188. if (execution_direction == EXEC_REVERSE)
  1189. {
  1190. if (record_full_list->next)
  1191. record_full_list = record_full_list->next;
  1192. }
  1193. else
  1194. record_full_list = record_full_list->prev;
  1195. throw;
  1196. }
  1197. }
  1198. signal (SIGINT, handle_sigint);
  1199. return inferior_ptid;
  1200. }
  1201. ptid_t
  1202. record_full_base_target::wait (ptid_t ptid, struct target_waitstatus *status,
  1203. target_wait_flags options)
  1204. {
  1205. ptid_t return_ptid;
  1206. clear_async_event_handler (record_full_async_inferior_event_token);
  1207. return_ptid = record_full_wait_1 (this, ptid, status, options);
  1208. if (status->kind () != TARGET_WAITKIND_IGNORE)
  1209. {
  1210. /* We're reporting a stop. Make sure any spurious
  1211. target_wait(WNOHANG) doesn't advance the target until the
  1212. core wants us resumed again. */
  1213. record_full_resumed = 0;
  1214. }
  1215. return return_ptid;
  1216. }
  1217. bool
  1218. record_full_base_target::stopped_by_watchpoint ()
  1219. {
  1220. if (RECORD_FULL_IS_REPLAY)
  1221. return record_full_stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
  1222. else
  1223. return beneath ()->stopped_by_watchpoint ();
  1224. }
  1225. bool
  1226. record_full_base_target::stopped_data_address (CORE_ADDR *addr_p)
  1227. {
  1228. if (RECORD_FULL_IS_REPLAY)
  1229. return false;
  1230. else
  1231. return this->beneath ()->stopped_data_address (addr_p);
  1232. }
  1233. /* The stopped_by_sw_breakpoint method of target record-full. */
  1234. bool
  1235. record_full_base_target::stopped_by_sw_breakpoint ()
  1236. {
  1237. return record_full_stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT;
  1238. }
  1239. /* The supports_stopped_by_sw_breakpoint method of target
  1240. record-full. */
  1241. bool
  1242. record_full_base_target::supports_stopped_by_sw_breakpoint ()
  1243. {
  1244. return true;
  1245. }
  1246. /* The stopped_by_hw_breakpoint method of target record-full. */
  1247. bool
  1248. record_full_base_target::stopped_by_hw_breakpoint ()
  1249. {
  1250. return record_full_stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT;
  1251. }
  1252. /* The supports_stopped_by_sw_breakpoint method of target
  1253. record-full. */
  1254. bool
  1255. record_full_base_target::supports_stopped_by_hw_breakpoint ()
  1256. {
  1257. return true;
  1258. }
  1259. /* Record registers change (by user or by GDB) to list as an instruction. */
  1260. static void
  1261. record_full_registers_change (struct regcache *regcache, int regnum)
  1262. {
  1263. /* Check record_full_insn_num. */
  1264. record_full_check_insn_num ();
  1265. record_full_arch_list_head = NULL;
  1266. record_full_arch_list_tail = NULL;
  1267. if (regnum < 0)
  1268. {
  1269. int i;
  1270. for (i = 0; i < gdbarch_num_regs (regcache->arch ()); i++)
  1271. {
  1272. if (record_full_arch_list_add_reg (regcache, i))
  1273. {
  1274. record_full_list_release (record_full_arch_list_tail);
  1275. error (_("Process record: failed to record execution log."));
  1276. }
  1277. }
  1278. }
  1279. else
  1280. {
  1281. if (record_full_arch_list_add_reg (regcache, regnum))
  1282. {
  1283. record_full_list_release (record_full_arch_list_tail);
  1284. error (_("Process record: failed to record execution log."));
  1285. }
  1286. }
  1287. if (record_full_arch_list_add_end ())
  1288. {
  1289. record_full_list_release (record_full_arch_list_tail);
  1290. error (_("Process record: failed to record execution log."));
  1291. }
  1292. record_full_list->next = record_full_arch_list_head;
  1293. record_full_arch_list_head->prev = record_full_list;
  1294. record_full_list = record_full_arch_list_tail;
  1295. if (record_full_insn_num == record_full_insn_max_num)
  1296. record_full_list_release_first ();
  1297. else
  1298. record_full_insn_num++;
  1299. }
  1300. /* "store_registers" method for process record target. */
  1301. void
  1302. record_full_target::store_registers (struct regcache *regcache, int regno)
  1303. {
  1304. if (!record_full_gdb_operation_disable)
  1305. {
  1306. if (RECORD_FULL_IS_REPLAY)
  1307. {
  1308. int n;
  1309. /* Let user choose if he wants to write register or not. */
  1310. if (regno < 0)
  1311. n =
  1312. query (_("Because GDB is in replay mode, changing the "
  1313. "value of a register will make the execution "
  1314. "log unusable from this point onward. "
  1315. "Change all registers?"));
  1316. else
  1317. n =
  1318. query (_("Because GDB is in replay mode, changing the value "
  1319. "of a register will make the execution log unusable "
  1320. "from this point onward. Change register %s?"),
  1321. gdbarch_register_name (regcache->arch (),
  1322. regno));
  1323. if (!n)
  1324. {
  1325. /* Invalidate the value of regcache that was set in function
  1326. "regcache_raw_write". */
  1327. if (regno < 0)
  1328. {
  1329. int i;
  1330. for (i = 0;
  1331. i < gdbarch_num_regs (regcache->arch ());
  1332. i++)
  1333. regcache->invalidate (i);
  1334. }
  1335. else
  1336. regcache->invalidate (regno);
  1337. error (_("Process record canceled the operation."));
  1338. }
  1339. /* Destroy the record from here forward. */
  1340. record_full_list_release_following (record_full_list);
  1341. }
  1342. record_full_registers_change (regcache, regno);
  1343. }
  1344. this->beneath ()->store_registers (regcache, regno);
  1345. }
  1346. /* "xfer_partial" method. Behavior is conditional on
  1347. RECORD_FULL_IS_REPLAY.
  1348. In replay mode, we cannot write memory unles we are willing to
  1349. invalidate the record/replay log from this point forward. */
  1350. enum target_xfer_status
  1351. record_full_target::xfer_partial (enum target_object object,
  1352. const char *annex, gdb_byte *readbuf,
  1353. const gdb_byte *writebuf, ULONGEST offset,
  1354. ULONGEST len, ULONGEST *xfered_len)
  1355. {
  1356. if (!record_full_gdb_operation_disable
  1357. && (object == TARGET_OBJECT_MEMORY
  1358. || object == TARGET_OBJECT_RAW_MEMORY) && writebuf)
  1359. {
  1360. if (RECORD_FULL_IS_REPLAY)
  1361. {
  1362. /* Let user choose if he wants to write memory or not. */
  1363. if (!query (_("Because GDB is in replay mode, writing to memory "
  1364. "will make the execution log unusable from this "
  1365. "point onward. Write memory at address %s?"),
  1366. paddress (target_gdbarch (), offset)))
  1367. error (_("Process record canceled the operation."));
  1368. /* Destroy the record from here forward. */
  1369. record_full_list_release_following (record_full_list);
  1370. }
  1371. /* Check record_full_insn_num */
  1372. record_full_check_insn_num ();
  1373. /* Record registers change to list as an instruction. */
  1374. record_full_arch_list_head = NULL;
  1375. record_full_arch_list_tail = NULL;
  1376. if (record_full_arch_list_add_mem (offset, len))
  1377. {
  1378. record_full_list_release (record_full_arch_list_tail);
  1379. if (record_debug)
  1380. gdb_printf (gdb_stdlog,
  1381. "Process record: failed to record "
  1382. "execution log.");
  1383. return TARGET_XFER_E_IO;
  1384. }
  1385. if (record_full_arch_list_add_end ())
  1386. {
  1387. record_full_list_release (record_full_arch_list_tail);
  1388. if (record_debug)
  1389. gdb_printf (gdb_stdlog,
  1390. "Process record: failed to record "
  1391. "execution log.");
  1392. return TARGET_XFER_E_IO;
  1393. }
  1394. record_full_list->next = record_full_arch_list_head;
  1395. record_full_arch_list_head->prev = record_full_list;
  1396. record_full_list = record_full_arch_list_tail;
  1397. if (record_full_insn_num == record_full_insn_max_num)
  1398. record_full_list_release_first ();
  1399. else
  1400. record_full_insn_num++;
  1401. }
  1402. return this->beneath ()->xfer_partial (object, annex, readbuf, writebuf,
  1403. offset, len, xfered_len);
  1404. }
  1405. /* This structure represents a breakpoint inserted while the record
  1406. target is active. We use this to know when to install/remove
  1407. breakpoints in/from the target beneath. For example, a breakpoint
  1408. may be inserted while recording, but removed when not replaying nor
  1409. recording. In that case, the breakpoint had not been inserted on
  1410. the target beneath, so we should not try to remove it there. */
  1411. struct record_full_breakpoint
  1412. {
  1413. record_full_breakpoint (struct address_space *address_space_,
  1414. CORE_ADDR addr_,
  1415. bool in_target_beneath_)
  1416. : address_space (address_space_),
  1417. addr (addr_),
  1418. in_target_beneath (in_target_beneath_)
  1419. {
  1420. }
  1421. /* The address and address space the breakpoint was set at. */
  1422. struct address_space *address_space;
  1423. CORE_ADDR addr;
  1424. /* True when the breakpoint has been also installed in the target
  1425. beneath. This will be false for breakpoints set during replay or
  1426. when recording. */
  1427. bool in_target_beneath;
  1428. };
  1429. /* The list of breakpoints inserted while the record target is
  1430. active. */
  1431. static std::vector<record_full_breakpoint> record_full_breakpoints;
  1432. /* Sync existing breakpoints to record_full_breakpoints. */
  1433. static void
  1434. record_full_init_record_breakpoints (void)
  1435. {
  1436. record_full_breakpoints.clear ();
  1437. for (bp_location *loc : all_bp_locations ())
  1438. {
  1439. if (loc->loc_type != bp_loc_software_breakpoint)
  1440. continue;
  1441. if (loc->inserted)
  1442. record_full_breakpoints.emplace_back
  1443. (loc->target_info.placed_address_space,
  1444. loc->target_info.placed_address, 1);
  1445. }
  1446. }
  1447. /* Behavior is conditional on RECORD_FULL_IS_REPLAY. We will not actually
  1448. insert or remove breakpoints in the real target when replaying, nor
  1449. when recording. */
  1450. int
  1451. record_full_target::insert_breakpoint (struct gdbarch *gdbarch,
  1452. struct bp_target_info *bp_tgt)
  1453. {
  1454. bool in_target_beneath = false;
  1455. if (!RECORD_FULL_IS_REPLAY)
  1456. {
  1457. /* When recording, we currently always single-step, so we don't
  1458. really need to install regular breakpoints in the inferior.
  1459. However, we do have to insert software single-step
  1460. breakpoints, in case the target can't hardware step. To keep
  1461. things simple, we always insert. */
  1462. scoped_restore restore_operation_disable
  1463. = record_full_gdb_operation_disable_set ();
  1464. int ret = this->beneath ()->insert_breakpoint (gdbarch, bp_tgt);
  1465. if (ret != 0)
  1466. return ret;
  1467. in_target_beneath = true;
  1468. }
  1469. /* Use the existing entries if found in order to avoid duplication
  1470. in record_full_breakpoints. */
  1471. for (const record_full_breakpoint &bp : record_full_breakpoints)
  1472. {
  1473. if (bp.addr == bp_tgt->placed_address
  1474. && bp.address_space == bp_tgt->placed_address_space)
  1475. {
  1476. gdb_assert (bp.in_target_beneath == in_target_beneath);
  1477. return 0;
  1478. }
  1479. }
  1480. record_full_breakpoints.emplace_back (bp_tgt->placed_address_space,
  1481. bp_tgt->placed_address,
  1482. in_target_beneath);
  1483. return 0;
  1484. }
  1485. /* "remove_breakpoint" method for process record target. */
  1486. int
  1487. record_full_target::remove_breakpoint (struct gdbarch *gdbarch,
  1488. struct bp_target_info *bp_tgt,
  1489. enum remove_bp_reason reason)
  1490. {
  1491. for (auto iter = record_full_breakpoints.begin ();
  1492. iter != record_full_breakpoints.end ();
  1493. ++iter)
  1494. {
  1495. struct record_full_breakpoint &bp = *iter;
  1496. if (bp.addr == bp_tgt->placed_address
  1497. && bp.address_space == bp_tgt->placed_address_space)
  1498. {
  1499. if (bp.in_target_beneath)
  1500. {
  1501. scoped_restore restore_operation_disable
  1502. = record_full_gdb_operation_disable_set ();
  1503. int ret = this->beneath ()->remove_breakpoint (gdbarch, bp_tgt,
  1504. reason);
  1505. if (ret != 0)
  1506. return ret;
  1507. }
  1508. if (reason == REMOVE_BREAKPOINT)
  1509. unordered_remove (record_full_breakpoints, iter);
  1510. return 0;
  1511. }
  1512. }
  1513. gdb_assert_not_reached ("removing unknown breakpoint");
  1514. }
  1515. /* "can_execute_reverse" method for process record target. */
  1516. bool
  1517. record_full_base_target::can_execute_reverse ()
  1518. {
  1519. return true;
  1520. }
  1521. /* "get_bookmark" method for process record and prec over core. */
  1522. gdb_byte *
  1523. record_full_base_target::get_bookmark (const char *args, int from_tty)
  1524. {
  1525. char *ret = NULL;
  1526. /* Return stringified form of instruction count. */
  1527. if (record_full_list && record_full_list->type == record_full_end)
  1528. ret = xstrdup (pulongest (record_full_list->u.end.insn_num));
  1529. if (record_debug)
  1530. {
  1531. if (ret)
  1532. gdb_printf (gdb_stdlog,
  1533. "record_full_get_bookmark returns %s\n", ret);
  1534. else
  1535. gdb_printf (gdb_stdlog,
  1536. "record_full_get_bookmark returns NULL\n");
  1537. }
  1538. return (gdb_byte *) ret;
  1539. }
  1540. /* "goto_bookmark" method for process record and prec over core. */
  1541. void
  1542. record_full_base_target::goto_bookmark (const gdb_byte *raw_bookmark,
  1543. int from_tty)
  1544. {
  1545. const char *bookmark = (const char *) raw_bookmark;
  1546. if (record_debug)
  1547. gdb_printf (gdb_stdlog,
  1548. "record_full_goto_bookmark receives %s\n", bookmark);
  1549. std::string name_holder;
  1550. if (bookmark[0] == '\'' || bookmark[0] == '\"')
  1551. {
  1552. if (bookmark[strlen (bookmark) - 1] != bookmark[0])
  1553. error (_("Unbalanced quotes: %s"), bookmark);
  1554. name_holder = std::string (bookmark + 1, strlen (bookmark) - 2);
  1555. bookmark = name_holder.c_str ();
  1556. }
  1557. record_goto (bookmark);
  1558. }
  1559. enum exec_direction_kind
  1560. record_full_base_target::execution_direction ()
  1561. {
  1562. return record_full_execution_dir;
  1563. }
  1564. /* The record_method method of target record-full. */
  1565. enum record_method
  1566. record_full_base_target::record_method (ptid_t ptid)
  1567. {
  1568. return RECORD_METHOD_FULL;
  1569. }
  1570. void
  1571. record_full_base_target::info_record ()
  1572. {
  1573. struct record_full_entry *p;
  1574. if (RECORD_FULL_IS_REPLAY)
  1575. gdb_printf (_("Replay mode:\n"));
  1576. else
  1577. gdb_printf (_("Record mode:\n"));
  1578. /* Find entry for first actual instruction in the log. */
  1579. for (p = record_full_first.next;
  1580. p != NULL && p->type != record_full_end;
  1581. p = p->next)
  1582. ;
  1583. /* Do we have a log at all? */
  1584. if (p != NULL && p->type == record_full_end)
  1585. {
  1586. /* Display instruction number for first instruction in the log. */
  1587. gdb_printf (_("Lowest recorded instruction number is %s.\n"),
  1588. pulongest (p->u.end.insn_num));
  1589. /* If in replay mode, display where we are in the log. */
  1590. if (RECORD_FULL_IS_REPLAY)
  1591. gdb_printf (_("Current instruction number is %s.\n"),
  1592. pulongest (record_full_list->u.end.insn_num));
  1593. /* Display instruction number for last instruction in the log. */
  1594. gdb_printf (_("Highest recorded instruction number is %s.\n"),
  1595. pulongest (record_full_insn_count));
  1596. /* Display log count. */
  1597. gdb_printf (_("Log contains %u instructions.\n"),
  1598. record_full_insn_num);
  1599. }
  1600. else
  1601. gdb_printf (_("No instructions have been logged.\n"));
  1602. /* Display max log size. */
  1603. gdb_printf (_("Max logged instructions is %u.\n"),
  1604. record_full_insn_max_num);
  1605. }
  1606. bool
  1607. record_full_base_target::supports_delete_record ()
  1608. {
  1609. return true;
  1610. }
  1611. /* The "delete_record" target method. */
  1612. void
  1613. record_full_base_target::delete_record ()
  1614. {
  1615. record_full_list_release_following (record_full_list);
  1616. }
  1617. /* The "record_is_replaying" target method. */
  1618. bool
  1619. record_full_base_target::record_is_replaying (ptid_t ptid)
  1620. {
  1621. return RECORD_FULL_IS_REPLAY;
  1622. }
  1623. /* The "record_will_replay" target method. */
  1624. bool
  1625. record_full_base_target::record_will_replay (ptid_t ptid, int dir)
  1626. {
  1627. /* We can currently only record when executing forwards. Should we be able
  1628. to record when executing backwards on targets that support reverse
  1629. execution, this needs to be changed. */
  1630. return RECORD_FULL_IS_REPLAY || dir == EXEC_REVERSE;
  1631. }
  1632. /* Go to a specific entry. */
  1633. static void
  1634. record_full_goto_entry (struct record_full_entry *p)
  1635. {
  1636. if (p == NULL)
  1637. error (_("Target insn not found."));
  1638. else if (p == record_full_list)
  1639. error (_("Already at target insn."));
  1640. else if (p->u.end.insn_num > record_full_list->u.end.insn_num)
  1641. {
  1642. gdb_printf (_("Go forward to insn number %s\n"),
  1643. pulongest (p->u.end.insn_num));
  1644. record_full_goto_insn (p, EXEC_FORWARD);
  1645. }
  1646. else
  1647. {
  1648. gdb_printf (_("Go backward to insn number %s\n"),
  1649. pulongest (p->u.end.insn_num));
  1650. record_full_goto_insn (p, EXEC_REVERSE);
  1651. }
  1652. registers_changed ();
  1653. reinit_frame_cache ();
  1654. inferior_thread ()->set_stop_pc (regcache_read_pc (get_current_regcache ()));
  1655. print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
  1656. }
  1657. /* The "goto_record_begin" target method. */
  1658. void
  1659. record_full_base_target::goto_record_begin ()
  1660. {
  1661. struct record_full_entry *p = NULL;
  1662. for (p = &record_full_first; p != NULL; p = p->next)
  1663. if (p->type == record_full_end)
  1664. break;
  1665. record_full_goto_entry (p);
  1666. }
  1667. /* The "goto_record_end" target method. */
  1668. void
  1669. record_full_base_target::goto_record_end ()
  1670. {
  1671. struct record_full_entry *p = NULL;
  1672. for (p = record_full_list; p->next != NULL; p = p->next)
  1673. ;
  1674. for (; p!= NULL; p = p->prev)
  1675. if (p->type == record_full_end)
  1676. break;
  1677. record_full_goto_entry (p);
  1678. }
  1679. /* The "goto_record" target method. */
  1680. void
  1681. record_full_base_target::goto_record (ULONGEST target_insn)
  1682. {
  1683. struct record_full_entry *p = NULL;
  1684. for (p = &record_full_first; p != NULL; p = p->next)
  1685. if (p->type == record_full_end && p->u.end.insn_num == target_insn)
  1686. break;
  1687. record_full_goto_entry (p);
  1688. }
  1689. /* The "record_stop_replaying" target method. */
  1690. void
  1691. record_full_base_target::record_stop_replaying ()
  1692. {
  1693. goto_record_end ();
  1694. }
  1695. /* "resume" method for prec over corefile. */
  1696. void
  1697. record_full_core_target::resume (ptid_t ptid, int step,
  1698. enum gdb_signal signal)
  1699. {
  1700. record_full_resume_step = step;
  1701. record_full_resumed = 1;
  1702. record_full_execution_dir = ::execution_direction;
  1703. }
  1704. /* "kill" method for prec over corefile. */
  1705. void
  1706. record_full_core_target::kill ()
  1707. {
  1708. if (record_debug)
  1709. gdb_printf (gdb_stdlog, "Process record: record_full_core_kill\n");
  1710. current_inferior ()->unpush_target (this);
  1711. }
  1712. /* "fetch_registers" method for prec over corefile. */
  1713. void
  1714. record_full_core_target::fetch_registers (struct regcache *regcache,
  1715. int regno)
  1716. {
  1717. if (regno < 0)
  1718. {
  1719. int num = gdbarch_num_regs (regcache->arch ());
  1720. int i;
  1721. for (i = 0; i < num; i ++)
  1722. regcache->raw_supply (i, *record_full_core_regbuf);
  1723. }
  1724. else
  1725. regcache->raw_supply (regno, *record_full_core_regbuf);
  1726. }
  1727. /* "prepare_to_store" method for prec over corefile. */
  1728. void
  1729. record_full_core_target::prepare_to_store (struct regcache *regcache)
  1730. {
  1731. }
  1732. /* "store_registers" method for prec over corefile. */
  1733. void
  1734. record_full_core_target::store_registers (struct regcache *regcache,
  1735. int regno)
  1736. {
  1737. if (record_full_gdb_operation_disable)
  1738. record_full_core_regbuf->raw_supply (regno, *regcache);
  1739. else
  1740. error (_("You can't do that without a process to debug."));
  1741. }
  1742. /* "xfer_partial" method for prec over corefile. */
  1743. enum target_xfer_status
  1744. record_full_core_target::xfer_partial (enum target_object object,
  1745. const char *annex, gdb_byte *readbuf,
  1746. const gdb_byte *writebuf, ULONGEST offset,
  1747. ULONGEST len, ULONGEST *xfered_len)
  1748. {
  1749. if (object == TARGET_OBJECT_MEMORY)
  1750. {
  1751. if (record_full_gdb_operation_disable || !writebuf)
  1752. {
  1753. for (target_section &p : record_full_core_sections)
  1754. {
  1755. if (offset >= p.addr)
  1756. {
  1757. struct record_full_core_buf_entry *entry;
  1758. ULONGEST sec_offset;
  1759. if (offset >= p.endaddr)
  1760. continue;
  1761. if (offset + len > p.endaddr)
  1762. len = p.endaddr - offset;
  1763. sec_offset = offset - p.addr;
  1764. /* Read readbuf or write writebuf p, offset, len. */
  1765. /* Check flags. */
  1766. if (p.the_bfd_section->flags & SEC_CONSTRUCTOR
  1767. || (p.the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
  1768. {
  1769. if (readbuf)
  1770. memset (readbuf, 0, len);
  1771. *xfered_len = len;
  1772. return TARGET_XFER_OK;
  1773. }
  1774. /* Get record_full_core_buf_entry. */
  1775. for (entry = record_full_core_buf_list; entry;
  1776. entry = entry->prev)
  1777. if (entry->p == &p)
  1778. break;
  1779. if (writebuf)
  1780. {
  1781. if (!entry)
  1782. {
  1783. /* Add a new entry. */
  1784. entry = XNEW (struct record_full_core_buf_entry);
  1785. entry->p = &p;
  1786. if (!bfd_malloc_and_get_section
  1787. (p.the_bfd_section->owner,
  1788. p.the_bfd_section,
  1789. &entry->buf))
  1790. {
  1791. xfree (entry);
  1792. return TARGET_XFER_EOF;
  1793. }
  1794. entry->prev = record_full_core_buf_list;
  1795. record_full_core_buf_list = entry;
  1796. }
  1797. memcpy (entry->buf + sec_offset, writebuf,
  1798. (size_t) len);
  1799. }
  1800. else
  1801. {
  1802. if (!entry)
  1803. return this->beneath ()->xfer_partial (object, annex,
  1804. readbuf, writebuf,
  1805. offset, len,
  1806. xfered_len);
  1807. memcpy (readbuf, entry->buf + sec_offset,
  1808. (size_t) len);
  1809. }
  1810. *xfered_len = len;
  1811. return TARGET_XFER_OK;
  1812. }
  1813. }
  1814. return TARGET_XFER_E_IO;
  1815. }
  1816. else
  1817. error (_("You can't do that without a process to debug."));
  1818. }
  1819. return this->beneath ()->xfer_partial (object, annex,
  1820. readbuf, writebuf, offset, len,
  1821. xfered_len);
  1822. }
  1823. /* "insert_breakpoint" method for prec over corefile. */
  1824. int
  1825. record_full_core_target::insert_breakpoint (struct gdbarch *gdbarch,
  1826. struct bp_target_info *bp_tgt)
  1827. {
  1828. return 0;
  1829. }
  1830. /* "remove_breakpoint" method for prec over corefile. */
  1831. int
  1832. record_full_core_target::remove_breakpoint (struct gdbarch *gdbarch,
  1833. struct bp_target_info *bp_tgt,
  1834. enum remove_bp_reason reason)
  1835. {
  1836. return 0;
  1837. }
  1838. /* "has_execution" method for prec over corefile. */
  1839. bool
  1840. record_full_core_target::has_execution (inferior *inf)
  1841. {
  1842. return true;
  1843. }
  1844. /* Record log save-file format
  1845. Version 1 (never released)
  1846. Header:
  1847. 4 bytes: magic number htonl(0x20090829).
  1848. NOTE: be sure to change whenever this file format changes!
  1849. Records:
  1850. record_full_end:
  1851. 1 byte: record type (record_full_end, see enum record_full_type).
  1852. record_full_reg:
  1853. 1 byte: record type (record_full_reg, see enum record_full_type).
  1854. 8 bytes: register id (network byte order).
  1855. MAX_REGISTER_SIZE bytes: register value.
  1856. record_full_mem:
  1857. 1 byte: record type (record_full_mem, see enum record_full_type).
  1858. 8 bytes: memory length (network byte order).
  1859. 8 bytes: memory address (network byte order).
  1860. n bytes: memory value (n == memory length).
  1861. Version 2
  1862. 4 bytes: magic number netorder32(0x20091016).
  1863. NOTE: be sure to change whenever this file format changes!
  1864. Records:
  1865. record_full_end:
  1866. 1 byte: record type (record_full_end, see enum record_full_type).
  1867. 4 bytes: signal
  1868. 4 bytes: instruction count
  1869. record_full_reg:
  1870. 1 byte: record type (record_full_reg, see enum record_full_type).
  1871. 4 bytes: register id (network byte order).
  1872. n bytes: register value (n == actual register size).
  1873. (eg. 4 bytes for x86 general registers).
  1874. record_full_mem:
  1875. 1 byte: record type (record_full_mem, see enum record_full_type).
  1876. 4 bytes: memory length (network byte order).
  1877. 8 bytes: memory address (network byte order).
  1878. n bytes: memory value (n == memory length).
  1879. */
  1880. /* bfdcore_read -- read bytes from a core file section. */
  1881. static inline void
  1882. bfdcore_read (bfd *obfd, asection *osec, void *buf, int len, int *offset)
  1883. {
  1884. int ret = bfd_get_section_contents (obfd, osec, buf, *offset, len);
  1885. if (ret)
  1886. *offset += len;
  1887. else
  1888. error (_("Failed to read %d bytes from core file %s ('%s')."),
  1889. len, bfd_get_filename (obfd),
  1890. bfd_errmsg (bfd_get_error ()));
  1891. }
  1892. static inline uint64_t
  1893. netorder64 (uint64_t input)
  1894. {
  1895. uint64_t ret;
  1896. store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
  1897. BFD_ENDIAN_BIG, input);
  1898. return ret;
  1899. }
  1900. static inline uint32_t
  1901. netorder32 (uint32_t input)
  1902. {
  1903. uint32_t ret;
  1904. store_unsigned_integer ((gdb_byte *) &ret, sizeof (ret),
  1905. BFD_ENDIAN_BIG, input);
  1906. return ret;
  1907. }
  1908. /* Restore the execution log from a core_bfd file. */
  1909. static void
  1910. record_full_restore (void)
  1911. {
  1912. uint32_t magic;
  1913. struct record_full_entry *rec;
  1914. asection *osec;
  1915. uint32_t osec_size;
  1916. int bfd_offset = 0;
  1917. struct regcache *regcache;
  1918. /* We restore the execution log from the open core bfd,
  1919. if there is one. */
  1920. if (core_bfd == NULL)
  1921. return;
  1922. /* "record_full_restore" can only be called when record list is empty. */
  1923. gdb_assert (record_full_first.next == NULL);
  1924. if (record_debug)
  1925. gdb_printf (gdb_stdlog, "Restoring recording from core file.\n");
  1926. /* Now need to find our special note section. */
  1927. osec = bfd_get_section_by_name (core_bfd, "null0");
  1928. if (record_debug)
  1929. gdb_printf (gdb_stdlog, "Find precord section %s.\n",
  1930. osec ? "succeeded" : "failed");
  1931. if (osec == NULL)
  1932. return;
  1933. osec_size = bfd_section_size (osec);
  1934. if (record_debug)
  1935. gdb_printf (gdb_stdlog, "%s", bfd_section_name (osec));
  1936. /* Check the magic code. */
  1937. bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
  1938. if (magic != RECORD_FULL_FILE_MAGIC)
  1939. error (_("Version mis-match or file format error in core file %s."),
  1940. bfd_get_filename (core_bfd));
  1941. if (record_debug)
  1942. gdb_printf (gdb_stdlog,
  1943. " Reading 4-byte magic cookie "
  1944. "RECORD_FULL_FILE_MAGIC (0x%s)\n",
  1945. phex_nz (netorder32 (magic), 4));
  1946. /* Restore the entries in recfd into record_full_arch_list_head and
  1947. record_full_arch_list_tail. */
  1948. record_full_arch_list_head = NULL;
  1949. record_full_arch_list_tail = NULL;
  1950. record_full_insn_num = 0;
  1951. try
  1952. {
  1953. regcache = get_current_regcache ();
  1954. while (1)
  1955. {
  1956. uint8_t rectype;
  1957. uint32_t regnum, len, signal, count;
  1958. uint64_t addr;
  1959. /* We are finished when offset reaches osec_size. */
  1960. if (bfd_offset >= osec_size)
  1961. break;
  1962. bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
  1963. switch (rectype)
  1964. {
  1965. case record_full_reg: /* reg */
  1966. /* Get register number to regnum. */
  1967. bfdcore_read (core_bfd, osec, &regnum,
  1968. sizeof (regnum), &bfd_offset);
  1969. regnum = netorder32 (regnum);
  1970. rec = record_full_reg_alloc (regcache, regnum);
  1971. /* Get val. */
  1972. bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
  1973. rec->u.reg.len, &bfd_offset);
  1974. if (record_debug)
  1975. gdb_printf (gdb_stdlog,
  1976. " Reading register %d (1 "
  1977. "plus %lu plus %d bytes)\n",
  1978. rec->u.reg.num,
  1979. (unsigned long) sizeof (regnum),
  1980. rec->u.reg.len);
  1981. break;
  1982. case record_full_mem: /* mem */
  1983. /* Get len. */
  1984. bfdcore_read (core_bfd, osec, &len,
  1985. sizeof (len), &bfd_offset);
  1986. len = netorder32 (len);
  1987. /* Get addr. */
  1988. bfdcore_read (core_bfd, osec, &addr,
  1989. sizeof (addr), &bfd_offset);
  1990. addr = netorder64 (addr);
  1991. rec = record_full_mem_alloc (addr, len);
  1992. /* Get val. */
  1993. bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
  1994. rec->u.mem.len, &bfd_offset);
  1995. if (record_debug)
  1996. gdb_printf (gdb_stdlog,
  1997. " Reading memory %s (1 plus "
  1998. "%lu plus %lu plus %d bytes)\n",
  1999. paddress (get_current_arch (),
  2000. rec->u.mem.addr),
  2001. (unsigned long) sizeof (addr),
  2002. (unsigned long) sizeof (len),
  2003. rec->u.mem.len);
  2004. break;
  2005. case record_full_end: /* end */
  2006. rec = record_full_end_alloc ();
  2007. record_full_insn_num ++;
  2008. /* Get signal value. */
  2009. bfdcore_read (core_bfd, osec, &signal,
  2010. sizeof (signal), &bfd_offset);
  2011. signal = netorder32 (signal);
  2012. rec->u.end.sigval = (enum gdb_signal) signal;
  2013. /* Get insn count. */
  2014. bfdcore_read (core_bfd, osec, &count,
  2015. sizeof (count), &bfd_offset);
  2016. count = netorder32 (count);
  2017. rec->u.end.insn_num = count;
  2018. record_full_insn_count = count + 1;
  2019. if (record_debug)
  2020. gdb_printf (gdb_stdlog,
  2021. " Reading record_full_end (1 + "
  2022. "%lu + %lu bytes), offset == %s\n",
  2023. (unsigned long) sizeof (signal),
  2024. (unsigned long) sizeof (count),
  2025. paddress (get_current_arch (),
  2026. bfd_offset));
  2027. break;
  2028. default:
  2029. error (_("Bad entry type in core file %s."),
  2030. bfd_get_filename (core_bfd));
  2031. break;
  2032. }
  2033. /* Add rec to record arch list. */
  2034. record_full_arch_list_add (rec);
  2035. }
  2036. }
  2037. catch (const gdb_exception &ex)
  2038. {
  2039. record_full_list_release (record_full_arch_list_tail);
  2040. throw;
  2041. }
  2042. /* Add record_full_arch_list_head to the end of record list. */
  2043. record_full_first.next = record_full_arch_list_head;
  2044. record_full_arch_list_head->prev = &record_full_first;
  2045. record_full_arch_list_tail->next = NULL;
  2046. record_full_list = &record_full_first;
  2047. /* Update record_full_insn_max_num. */
  2048. if (record_full_insn_num > record_full_insn_max_num)
  2049. {
  2050. record_full_insn_max_num = record_full_insn_num;
  2051. warning (_("Auto increase record/replay buffer limit to %u."),
  2052. record_full_insn_max_num);
  2053. }
  2054. /* Succeeded. */
  2055. gdb_printf (_("Restored records from core file %s.\n"),
  2056. bfd_get_filename (core_bfd));
  2057. print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
  2058. }
  2059. /* bfdcore_write -- write bytes into a core file section. */
  2060. static inline void
  2061. bfdcore_write (bfd *obfd, asection *osec, void *buf, int len, int *offset)
  2062. {
  2063. int ret = bfd_set_section_contents (obfd, osec, buf, *offset, len);
  2064. if (ret)
  2065. *offset += len;
  2066. else
  2067. error (_("Failed to write %d bytes to core file %s ('%s')."),
  2068. len, bfd_get_filename (obfd),
  2069. bfd_errmsg (bfd_get_error ()));
  2070. }
  2071. /* Restore the execution log from a file. We use a modified elf
  2072. corefile format, with an extra section for our data. */
  2073. static void
  2074. cmd_record_full_restore (const char *args, int from_tty)
  2075. {
  2076. core_file_command (args, from_tty);
  2077. record_full_open (args, from_tty);
  2078. }
  2079. /* Save the execution log to a file. We use a modified elf corefile
  2080. format, with an extra section for our data. */
  2081. void
  2082. record_full_base_target::save_record (const char *recfilename)
  2083. {
  2084. struct record_full_entry *cur_record_full_list;
  2085. uint32_t magic;
  2086. struct regcache *regcache;
  2087. struct gdbarch *gdbarch;
  2088. int save_size = 0;
  2089. asection *osec = NULL;
  2090. int bfd_offset = 0;
  2091. /* Open the save file. */
  2092. if (record_debug)
  2093. gdb_printf (gdb_stdlog, "Saving execution log to core file '%s'\n",
  2094. recfilename);
  2095. /* Open the output file. */
  2096. gdb_bfd_ref_ptr obfd (create_gcore_bfd (recfilename));
  2097. /* Arrange to remove the output file on failure. */
  2098. gdb::unlinker unlink_file (recfilename);
  2099. /* Save the current record entry to "cur_record_full_list". */
  2100. cur_record_full_list = record_full_list;
  2101. /* Get the values of regcache and gdbarch. */
  2102. regcache = get_current_regcache ();
  2103. gdbarch = regcache->arch ();
  2104. /* Disable the GDB operation record. */
  2105. scoped_restore restore_operation_disable
  2106. = record_full_gdb_operation_disable_set ();
  2107. /* Reverse execute to the begin of record list. */
  2108. while (1)
  2109. {
  2110. /* Check for beginning and end of log. */
  2111. if (record_full_list == &record_full_first)
  2112. break;
  2113. record_full_exec_insn (regcache, gdbarch, record_full_list);
  2114. if (record_full_list->prev)
  2115. record_full_list = record_full_list->prev;
  2116. }
  2117. /* Compute the size needed for the extra bfd section. */
  2118. save_size = 4; /* magic cookie */
  2119. for (record_full_list = record_full_first.next; record_full_list;
  2120. record_full_list = record_full_list->next)
  2121. switch (record_full_list->type)
  2122. {
  2123. case record_full_end:
  2124. save_size += 1 + 4 + 4;
  2125. break;
  2126. case record_full_reg:
  2127. save_size += 1 + 4 + record_full_list->u.reg.len;
  2128. break;
  2129. case record_full_mem:
  2130. save_size += 1 + 4 + 8 + record_full_list->u.mem.len;
  2131. break;
  2132. }
  2133. /* Make the new bfd section. */
  2134. osec = bfd_make_section_anyway_with_flags (obfd.get (), "precord",
  2135. SEC_HAS_CONTENTS
  2136. | SEC_READONLY);
  2137. if (osec == NULL)
  2138. error (_("Failed to create 'precord' section for corefile %s: %s"),
  2139. recfilename,
  2140. bfd_errmsg (bfd_get_error ()));
  2141. bfd_set_section_size (osec, save_size);
  2142. bfd_set_section_vma (osec, 0);
  2143. bfd_set_section_alignment (osec, 0);
  2144. /* Save corefile state. */
  2145. write_gcore_file (obfd.get ());
  2146. /* Write out the record log. */
  2147. /* Write the magic code. */
  2148. magic = RECORD_FULL_FILE_MAGIC;
  2149. if (record_debug)
  2150. gdb_printf (gdb_stdlog,
  2151. " Writing 4-byte magic cookie "
  2152. "RECORD_FULL_FILE_MAGIC (0x%s)\n",
  2153. phex_nz (magic, 4));
  2154. bfdcore_write (obfd.get (), osec, &magic, sizeof (magic), &bfd_offset);
  2155. /* Save the entries to recfd and forward execute to the end of
  2156. record list. */
  2157. record_full_list = &record_full_first;
  2158. while (1)
  2159. {
  2160. /* Save entry. */
  2161. if (record_full_list != &record_full_first)
  2162. {
  2163. uint8_t type;
  2164. uint32_t regnum, len, signal, count;
  2165. uint64_t addr;
  2166. type = record_full_list->type;
  2167. bfdcore_write (obfd.get (), osec, &type, sizeof (type), &bfd_offset);
  2168. switch (record_full_list->type)
  2169. {
  2170. case record_full_reg: /* reg */
  2171. if (record_debug)
  2172. gdb_printf (gdb_stdlog,
  2173. " Writing register %d (1 "
  2174. "plus %lu plus %d bytes)\n",
  2175. record_full_list->u.reg.num,
  2176. (unsigned long) sizeof (regnum),
  2177. record_full_list->u.reg.len);
  2178. /* Write regnum. */
  2179. regnum = netorder32 (record_full_list->u.reg.num);
  2180. bfdcore_write (obfd.get (), osec, &regnum,
  2181. sizeof (regnum), &bfd_offset);
  2182. /* Write regval. */
  2183. bfdcore_write (obfd.get (), osec,
  2184. record_full_get_loc (record_full_list),
  2185. record_full_list->u.reg.len, &bfd_offset);
  2186. break;
  2187. case record_full_mem: /* mem */
  2188. if (record_debug)
  2189. gdb_printf (gdb_stdlog,
  2190. " Writing memory %s (1 plus "
  2191. "%lu plus %lu plus %d bytes)\n",
  2192. paddress (gdbarch,
  2193. record_full_list->u.mem.addr),
  2194. (unsigned long) sizeof (addr),
  2195. (unsigned long) sizeof (len),
  2196. record_full_list->u.mem.len);
  2197. /* Write memlen. */
  2198. len = netorder32 (record_full_list->u.mem.len);
  2199. bfdcore_write (obfd.get (), osec, &len, sizeof (len),
  2200. &bfd_offset);
  2201. /* Write memaddr. */
  2202. addr = netorder64 (record_full_list->u.mem.addr);
  2203. bfdcore_write (obfd.get (), osec, &addr,
  2204. sizeof (addr), &bfd_offset);
  2205. /* Write memval. */
  2206. bfdcore_write (obfd.get (), osec,
  2207. record_full_get_loc (record_full_list),
  2208. record_full_list->u.mem.len, &bfd_offset);
  2209. break;
  2210. case record_full_end:
  2211. if (record_debug)
  2212. gdb_printf (gdb_stdlog,
  2213. " Writing record_full_end (1 + "
  2214. "%lu + %lu bytes)\n",
  2215. (unsigned long) sizeof (signal),
  2216. (unsigned long) sizeof (count));
  2217. /* Write signal value. */
  2218. signal = netorder32 (record_full_list->u.end.sigval);
  2219. bfdcore_write (obfd.get (), osec, &signal,
  2220. sizeof (signal), &bfd_offset);
  2221. /* Write insn count. */
  2222. count = netorder32 (record_full_list->u.end.insn_num);
  2223. bfdcore_write (obfd.get (), osec, &count,
  2224. sizeof (count), &bfd_offset);
  2225. break;
  2226. }
  2227. }
  2228. /* Execute entry. */
  2229. record_full_exec_insn (regcache, gdbarch, record_full_list);
  2230. if (record_full_list->next)
  2231. record_full_list = record_full_list->next;
  2232. else
  2233. break;
  2234. }
  2235. /* Reverse execute to cur_record_full_list. */
  2236. while (1)
  2237. {
  2238. /* Check for beginning and end of log. */
  2239. if (record_full_list == cur_record_full_list)
  2240. break;
  2241. record_full_exec_insn (regcache, gdbarch, record_full_list);
  2242. if (record_full_list->prev)
  2243. record_full_list = record_full_list->prev;
  2244. }
  2245. unlink_file.keep ();
  2246. /* Succeeded. */
  2247. gdb_printf (_("Saved core file %s with execution log.\n"),
  2248. recfilename);
  2249. }
  2250. /* record_full_goto_insn -- rewind the record log (forward or backward,
  2251. depending on DIR) to the given entry, changing the program state
  2252. correspondingly. */
  2253. static void
  2254. record_full_goto_insn (struct record_full_entry *entry,
  2255. enum exec_direction_kind dir)
  2256. {
  2257. scoped_restore restore_operation_disable
  2258. = record_full_gdb_operation_disable_set ();
  2259. struct regcache *regcache = get_current_regcache ();
  2260. struct gdbarch *gdbarch = regcache->arch ();
  2261. /* Assume everything is valid: we will hit the entry,
  2262. and we will not hit the end of the recording. */
  2263. if (dir == EXEC_FORWARD)
  2264. record_full_list = record_full_list->next;
  2265. do
  2266. {
  2267. record_full_exec_insn (regcache, gdbarch, record_full_list);
  2268. if (dir == EXEC_REVERSE)
  2269. record_full_list = record_full_list->prev;
  2270. else
  2271. record_full_list = record_full_list->next;
  2272. } while (record_full_list != entry);
  2273. }
  2274. /* Alias for "target record-full". */
  2275. static void
  2276. cmd_record_full_start (const char *args, int from_tty)
  2277. {
  2278. execute_command ("target record-full", from_tty);
  2279. }
  2280. static void
  2281. set_record_full_insn_max_num (const char *args, int from_tty,
  2282. struct cmd_list_element *c)
  2283. {
  2284. if (record_full_insn_num > record_full_insn_max_num)
  2285. {
  2286. /* Count down record_full_insn_num while releasing records from list. */
  2287. while (record_full_insn_num > record_full_insn_max_num)
  2288. {
  2289. record_full_list_release_first ();
  2290. record_full_insn_num--;
  2291. }
  2292. }
  2293. }
  2294. void _initialize_record_full ();
  2295. void
  2296. _initialize_record_full ()
  2297. {
  2298. struct cmd_list_element *c;
  2299. /* Init record_full_first. */
  2300. record_full_first.prev = NULL;
  2301. record_full_first.next = NULL;
  2302. record_full_first.type = record_full_end;
  2303. add_target (record_full_target_info, record_full_open);
  2304. add_deprecated_target_alias (record_full_target_info, "record");
  2305. add_target (record_full_core_target_info, record_full_open);
  2306. add_prefix_cmd ("full", class_obscure, cmd_record_full_start,
  2307. _("Start full execution recording."), &record_full_cmdlist,
  2308. 0, &record_cmdlist);
  2309. cmd_list_element *record_full_restore_cmd
  2310. = add_cmd ("restore", class_obscure, cmd_record_full_restore,
  2311. _("Restore the execution log from a file.\n\
  2312. Argument is filename. File must be created with 'record save'."),
  2313. &record_full_cmdlist);
  2314. set_cmd_completer (record_full_restore_cmd, filename_completer);
  2315. /* Deprecate the old version without "full" prefix. */
  2316. c = add_alias_cmd ("restore", record_full_restore_cmd, class_obscure, 1,
  2317. &record_cmdlist);
  2318. set_cmd_completer (c, filename_completer);
  2319. deprecate_cmd (c, "record full restore");
  2320. add_setshow_prefix_cmd ("full", class_support,
  2321. _("Set record options."),
  2322. _("Show record options."),
  2323. &set_record_full_cmdlist,
  2324. &show_record_full_cmdlist,
  2325. &set_record_cmdlist,
  2326. &show_record_cmdlist);
  2327. /* Record instructions number limit command. */
  2328. set_show_commands set_record_full_stop_at_limit_cmds
  2329. = add_setshow_boolean_cmd ("stop-at-limit", no_class,
  2330. &record_full_stop_at_limit, _("\
  2331. Set whether record/replay stops when record/replay buffer becomes full."), _("\
  2332. Show whether record/replay stops when record/replay buffer becomes full."),
  2333. _("Default is ON.\n\
  2334. When ON, if the record/replay buffer becomes full, ask user what to do.\n\
  2335. When OFF, if the record/replay buffer becomes full,\n\
  2336. delete the oldest recorded instruction to make room for each new one."),
  2337. NULL, NULL,
  2338. &set_record_full_cmdlist,
  2339. &show_record_full_cmdlist);
  2340. c = add_alias_cmd ("stop-at-limit",
  2341. set_record_full_stop_at_limit_cmds.set, no_class, 1,
  2342. &set_record_cmdlist);
  2343. deprecate_cmd (c, "set record full stop-at-limit");
  2344. c = add_alias_cmd ("stop-at-limit",
  2345. set_record_full_stop_at_limit_cmds.show, no_class, 1,
  2346. &show_record_cmdlist);
  2347. deprecate_cmd (c, "show record full stop-at-limit");
  2348. set_show_commands record_full_insn_number_max_cmds
  2349. = add_setshow_uinteger_cmd ("insn-number-max", no_class,
  2350. &record_full_insn_max_num,
  2351. _("Set record/replay buffer limit."),
  2352. _("Show record/replay buffer limit."), _("\
  2353. Set the maximum number of instructions to be stored in the\n\
  2354. record/replay buffer. A value of either \"unlimited\" or zero means no\n\
  2355. limit. Default is 200000."),
  2356. set_record_full_insn_max_num,
  2357. NULL, &set_record_full_cmdlist,
  2358. &show_record_full_cmdlist);
  2359. c = add_alias_cmd ("insn-number-max", record_full_insn_number_max_cmds.set,
  2360. no_class, 1, &set_record_cmdlist);
  2361. deprecate_cmd (c, "set record full insn-number-max");
  2362. c = add_alias_cmd ("insn-number-max", record_full_insn_number_max_cmds.show,
  2363. no_class, 1, &show_record_cmdlist);
  2364. deprecate_cmd (c, "show record full insn-number-max");
  2365. set_show_commands record_full_memory_query_cmds
  2366. = add_setshow_boolean_cmd ("memory-query", no_class,
  2367. &record_full_memory_query, _("\
  2368. Set whether query if PREC cannot record memory change of next instruction."),
  2369. _("\
  2370. Show whether query if PREC cannot record memory change of next instruction."),
  2371. _("\
  2372. Default is OFF.\n\
  2373. When ON, query if PREC cannot record memory change of next instruction."),
  2374. NULL, NULL,
  2375. &set_record_full_cmdlist,
  2376. &show_record_full_cmdlist);
  2377. c = add_alias_cmd ("memory-query", record_full_memory_query_cmds.set,
  2378. no_class, 1, &set_record_cmdlist);
  2379. deprecate_cmd (c, "set record full memory-query");
  2380. c = add_alias_cmd ("memory-query", record_full_memory_query_cmds.show,
  2381. no_class, 1,&show_record_cmdlist);
  2382. deprecate_cmd (c, "show record full memory-query");
  2383. }