mem-break.cc 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215
  1. /* Memory breakpoint operations for the remote server for GDB.
  2. Copyright (C) 2002-2022 Free Software Foundation, Inc.
  3. Contributed by MontaVista Software.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "server.h"
  16. #include "regcache.h"
  17. #include "ax.h"
  18. #define MAX_BREAKPOINT_LEN 8
  19. /* Helper macro used in loops that append multiple items to a singly-linked
  20. list instead of inserting items at the head of the list, as, say, in the
  21. breakpoint lists. LISTPP is a pointer to the pointer that is the head of
  22. the new list. ITEMP is a pointer to the item to be added to the list.
  23. TAILP must be defined to be the same type as ITEMP, and initialized to
  24. NULL. */
  25. #define APPEND_TO_LIST(listpp, itemp, tailp) \
  26. do \
  27. { \
  28. if ((tailp) == NULL) \
  29. *(listpp) = (itemp); \
  30. else \
  31. (tailp)->next = (itemp); \
  32. (tailp) = (itemp); \
  33. } \
  34. while (0)
  35. /* GDB will never try to install multiple breakpoints at the same
  36. address. However, we can see GDB requesting to insert a breakpoint
  37. at an address is had already inserted one previously in a few
  38. situations.
  39. - The RSP documentation on Z packets says that to avoid potential
  40. problems with duplicate packets, the operations should be
  41. implemented in an idempotent way.
  42. - A breakpoint is set at ADDR, an address in a shared library.
  43. Then the shared library is unloaded. And then another, unrelated,
  44. breakpoint at ADDR is set. There is not breakpoint removal request
  45. between the first and the second breakpoint.
  46. - When GDB wants to update the target-side breakpoint conditions or
  47. commands, it re-inserts the breakpoint, with updated
  48. conditions/commands associated.
  49. Also, we need to keep track of internal breakpoints too, so we do
  50. need to be able to install multiple breakpoints at the same address
  51. transparently.
  52. We keep track of two different, and closely related structures. A
  53. raw breakpoint, which manages the low level, close to the metal
  54. aspect of a breakpoint. It holds the breakpoint address, and for
  55. software breakpoints, a buffer holding a copy of the instructions
  56. that would be in memory had not been a breakpoint there (we call
  57. that the shadow memory of the breakpoint). We occasionally need to
  58. temporarilly uninsert a breakpoint without the client knowing about
  59. it (e.g., to step over an internal breakpoint), so we keep an
  60. `inserted' state associated with this low level breakpoint
  61. structure. There can only be one such object for a given address.
  62. Then, we have (a bit higher level) breakpoints. This structure
  63. holds a callback to be called whenever a breakpoint is hit, a
  64. high-level type, and a link to a low level raw breakpoint. There
  65. can be many high-level breakpoints at the same address, and all of
  66. them will point to the same raw breakpoint, which is reference
  67. counted. */
  68. /* The low level, physical, raw breakpoint. */
  69. struct raw_breakpoint
  70. {
  71. struct raw_breakpoint *next;
  72. /* The low level type of the breakpoint (software breakpoint,
  73. watchpoint, etc.) */
  74. enum raw_bkpt_type raw_type;
  75. /* A reference count. Each high level breakpoint referencing this
  76. raw breakpoint accounts for one reference. */
  77. int refcount;
  78. /* The breakpoint's insertion address. There can only be one raw
  79. breakpoint for a given PC. */
  80. CORE_ADDR pc;
  81. /* The breakpoint's kind. This is target specific. Most
  82. architectures only use one specific instruction for breakpoints, while
  83. others may use more than one. E.g., on ARM, we need to use different
  84. breakpoint instructions on Thumb, Thumb-2, and ARM code. Likewise for
  85. hardware breakpoints -- some architectures (including ARM) need to
  86. setup debug registers differently depending on mode. */
  87. int kind;
  88. /* The breakpoint's shadow memory. */
  89. unsigned char old_data[MAX_BREAKPOINT_LEN];
  90. /* Positive if this breakpoint is currently inserted in the
  91. inferior. Negative if it was, but we've detected that it's now
  92. gone. Zero if not inserted. */
  93. int inserted;
  94. };
  95. /* The type of a breakpoint. */
  96. enum bkpt_type
  97. {
  98. /* A GDB breakpoint, requested with a Z0 packet. */
  99. gdb_breakpoint_Z0,
  100. /* A GDB hardware breakpoint, requested with a Z1 packet. */
  101. gdb_breakpoint_Z1,
  102. /* A GDB write watchpoint, requested with a Z2 packet. */
  103. gdb_breakpoint_Z2,
  104. /* A GDB read watchpoint, requested with a Z3 packet. */
  105. gdb_breakpoint_Z3,
  106. /* A GDB access watchpoint, requested with a Z4 packet. */
  107. gdb_breakpoint_Z4,
  108. /* A software single-step breakpoint. */
  109. single_step_breakpoint,
  110. /* Any other breakpoint type that doesn't require specific
  111. treatment goes here. E.g., an event breakpoint. */
  112. other_breakpoint,
  113. };
  114. struct point_cond_list
  115. {
  116. /* Pointer to the agent expression that is the breakpoint's
  117. conditional. */
  118. struct agent_expr *cond;
  119. /* Pointer to the next condition. */
  120. struct point_cond_list *next;
  121. };
  122. struct point_command_list
  123. {
  124. /* Pointer to the agent expression that is the breakpoint's
  125. commands. */
  126. struct agent_expr *cmd;
  127. /* Flag that is true if this command should run even while GDB is
  128. disconnected. */
  129. int persistence;
  130. /* Pointer to the next command. */
  131. struct point_command_list *next;
  132. };
  133. /* A high level (in gdbserver's perspective) breakpoint. */
  134. struct breakpoint
  135. {
  136. struct breakpoint *next;
  137. /* The breakpoint's type. */
  138. enum bkpt_type type;
  139. /* Link to this breakpoint's raw breakpoint. This is always
  140. non-NULL. */
  141. struct raw_breakpoint *raw;
  142. };
  143. /* Breakpoint requested by GDB. */
  144. struct gdb_breakpoint
  145. {
  146. struct breakpoint base;
  147. /* Pointer to the condition list that should be evaluated on
  148. the target or NULL if the breakpoint is unconditional or
  149. if GDB doesn't want us to evaluate the conditionals on the
  150. target's side. */
  151. struct point_cond_list *cond_list;
  152. /* Point to the list of commands to run when this is hit. */
  153. struct point_command_list *command_list;
  154. };
  155. /* Breakpoint used by GDBserver. */
  156. struct other_breakpoint
  157. {
  158. struct breakpoint base;
  159. /* Function to call when we hit this breakpoint. If it returns 1,
  160. the breakpoint shall be deleted; 0 or if this callback is NULL,
  161. it will be left inserted. */
  162. int (*handler) (CORE_ADDR);
  163. };
  164. /* Breakpoint for single step. */
  165. struct single_step_breakpoint
  166. {
  167. struct breakpoint base;
  168. /* Thread the reinsert breakpoint belongs to. */
  169. ptid_t ptid;
  170. };
  171. /* Return the breakpoint size from its kind. */
  172. static int
  173. bp_size (struct raw_breakpoint *bp)
  174. {
  175. int size = 0;
  176. the_target->sw_breakpoint_from_kind (bp->kind, &size);
  177. return size;
  178. }
  179. /* Return the breakpoint opcode from its kind. */
  180. static const gdb_byte *
  181. bp_opcode (struct raw_breakpoint *bp)
  182. {
  183. int size = 0;
  184. return the_target->sw_breakpoint_from_kind (bp->kind, &size);
  185. }
  186. /* See mem-break.h. */
  187. enum target_hw_bp_type
  188. raw_bkpt_type_to_target_hw_bp_type (enum raw_bkpt_type raw_type)
  189. {
  190. switch (raw_type)
  191. {
  192. case raw_bkpt_type_hw:
  193. return hw_execute;
  194. case raw_bkpt_type_write_wp:
  195. return hw_write;
  196. case raw_bkpt_type_read_wp:
  197. return hw_read;
  198. case raw_bkpt_type_access_wp:
  199. return hw_access;
  200. default:
  201. internal_error (__FILE__, __LINE__,
  202. "bad raw breakpoint type %d", (int) raw_type);
  203. }
  204. }
  205. /* See mem-break.h. */
  206. static enum bkpt_type
  207. Z_packet_to_bkpt_type (char z_type)
  208. {
  209. gdb_assert ('0' <= z_type && z_type <= '4');
  210. return (enum bkpt_type) (gdb_breakpoint_Z0 + (z_type - '0'));
  211. }
  212. /* See mem-break.h. */
  213. enum raw_bkpt_type
  214. Z_packet_to_raw_bkpt_type (char z_type)
  215. {
  216. switch (z_type)
  217. {
  218. case Z_PACKET_SW_BP:
  219. return raw_bkpt_type_sw;
  220. case Z_PACKET_HW_BP:
  221. return raw_bkpt_type_hw;
  222. case Z_PACKET_WRITE_WP:
  223. return raw_bkpt_type_write_wp;
  224. case Z_PACKET_READ_WP:
  225. return raw_bkpt_type_read_wp;
  226. case Z_PACKET_ACCESS_WP:
  227. return raw_bkpt_type_access_wp;
  228. default:
  229. gdb_assert_not_reached ("unhandled Z packet type.");
  230. }
  231. }
  232. /* Return true if breakpoint TYPE is a GDB breakpoint. */
  233. static int
  234. is_gdb_breakpoint (enum bkpt_type type)
  235. {
  236. return (type == gdb_breakpoint_Z0
  237. || type == gdb_breakpoint_Z1
  238. || type == gdb_breakpoint_Z2
  239. || type == gdb_breakpoint_Z3
  240. || type == gdb_breakpoint_Z4);
  241. }
  242. bool
  243. any_persistent_commands (process_info *proc)
  244. {
  245. struct breakpoint *bp;
  246. struct point_command_list *cl;
  247. for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
  248. {
  249. if (is_gdb_breakpoint (bp->type))
  250. {
  251. struct gdb_breakpoint *gdb_bp = (struct gdb_breakpoint *) bp;
  252. for (cl = gdb_bp->command_list; cl != NULL; cl = cl->next)
  253. if (cl->persistence)
  254. return true;
  255. }
  256. }
  257. return false;
  258. }
  259. /* Find low-level breakpoint of type TYPE at address ADDR that is not
  260. insert-disabled. Returns NULL if not found. */
  261. static struct raw_breakpoint *
  262. find_enabled_raw_code_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type)
  263. {
  264. struct process_info *proc = current_process ();
  265. struct raw_breakpoint *bp;
  266. for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  267. if (bp->pc == addr
  268. && bp->raw_type == type
  269. && bp->inserted >= 0)
  270. return bp;
  271. return NULL;
  272. }
  273. /* Find low-level breakpoint of type TYPE at address ADDR. Returns
  274. NULL if not found. */
  275. static struct raw_breakpoint *
  276. find_raw_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type, int kind)
  277. {
  278. struct process_info *proc = current_process ();
  279. struct raw_breakpoint *bp;
  280. for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  281. if (bp->pc == addr && bp->raw_type == type && bp->kind == kind)
  282. return bp;
  283. return NULL;
  284. }
  285. /* See mem-break.h. */
  286. int
  287. insert_memory_breakpoint (struct raw_breakpoint *bp)
  288. {
  289. unsigned char buf[MAX_BREAKPOINT_LEN];
  290. int err;
  291. /* Note that there can be fast tracepoint jumps installed in the
  292. same memory range, so to get at the original memory, we need to
  293. use read_inferior_memory, which masks those out. */
  294. err = read_inferior_memory (bp->pc, buf, bp_size (bp));
  295. if (err != 0)
  296. {
  297. threads_debug_printf ("Failed to read shadow memory of"
  298. " breakpoint at 0x%s (%s).",
  299. paddress (bp->pc), safe_strerror (err));
  300. }
  301. else
  302. {
  303. memcpy (bp->old_data, buf, bp_size (bp));
  304. err = the_target->write_memory (bp->pc, bp_opcode (bp),
  305. bp_size (bp));
  306. if (err != 0)
  307. threads_debug_printf ("Failed to insert breakpoint at 0x%s (%s).",
  308. paddress (bp->pc), safe_strerror (err));
  309. }
  310. return err != 0 ? -1 : 0;
  311. }
  312. /* See mem-break.h */
  313. int
  314. remove_memory_breakpoint (struct raw_breakpoint *bp)
  315. {
  316. unsigned char buf[MAX_BREAKPOINT_LEN];
  317. int err;
  318. /* Since there can be trap breakpoints inserted in the same address
  319. range, we use `target_write_memory', which takes care of
  320. layering breakpoints on top of fast tracepoints, and on top of
  321. the buffer we pass it. This works because the caller has already
  322. either unlinked the breakpoint or marked it uninserted. Also
  323. note that we need to pass the current shadow contents, because
  324. target_write_memory updates any shadow memory with what we pass
  325. here, and we want that to be a nop. */
  326. memcpy (buf, bp->old_data, bp_size (bp));
  327. err = target_write_memory (bp->pc, buf, bp_size (bp));
  328. if (err != 0)
  329. threads_debug_printf ("Failed to uninsert raw breakpoint "
  330. "at 0x%s (%s) while deleting it.",
  331. paddress (bp->pc), safe_strerror (err));
  332. return err != 0 ? -1 : 0;
  333. }
  334. /* Set a RAW breakpoint of type TYPE and kind KIND at WHERE. On
  335. success, a pointer to the new breakpoint is returned. On failure,
  336. returns NULL and writes the error code to *ERR. */
  337. static struct raw_breakpoint *
  338. set_raw_breakpoint_at (enum raw_bkpt_type type, CORE_ADDR where, int kind,
  339. int *err)
  340. {
  341. struct process_info *proc = current_process ();
  342. struct raw_breakpoint *bp;
  343. if (type == raw_bkpt_type_sw || type == raw_bkpt_type_hw)
  344. {
  345. bp = find_enabled_raw_code_breakpoint_at (where, type);
  346. if (bp != NULL && bp->kind != kind)
  347. {
  348. /* A different kind than previously seen. The previous
  349. breakpoint must be gone then. */
  350. threads_debug_printf
  351. ("Inconsistent breakpoint kind? Was %d, now %d.",
  352. bp->kind, kind);
  353. bp->inserted = -1;
  354. bp = NULL;
  355. }
  356. }
  357. else
  358. bp = find_raw_breakpoint_at (where, type, kind);
  359. gdb::unique_xmalloc_ptr<struct raw_breakpoint> bp_holder;
  360. if (bp == NULL)
  361. {
  362. bp_holder.reset (XCNEW (struct raw_breakpoint));
  363. bp = bp_holder.get ();
  364. bp->pc = where;
  365. bp->kind = kind;
  366. bp->raw_type = type;
  367. }
  368. if (!bp->inserted)
  369. {
  370. *err = the_target->insert_point (bp->raw_type, bp->pc, bp->kind, bp);
  371. if (*err != 0)
  372. {
  373. threads_debug_printf ("Failed to insert breakpoint at 0x%s (%d).",
  374. paddress (where), *err);
  375. return NULL;
  376. }
  377. bp->inserted = 1;
  378. }
  379. /* If the breakpoint was allocated above, we know we want to keep it
  380. now. */
  381. bp_holder.release ();
  382. /* Link the breakpoint in, if this is the first reference. */
  383. if (++bp->refcount == 1)
  384. {
  385. bp->next = proc->raw_breakpoints;
  386. proc->raw_breakpoints = bp;
  387. }
  388. return bp;
  389. }
  390. /* Notice that breakpoint traps are always installed on top of fast
  391. tracepoint jumps. This is even if the fast tracepoint is installed
  392. at a later time compared to when the breakpoint was installed.
  393. This means that a stopping breakpoint or tracepoint has higher
  394. "priority". In turn, this allows having fast and slow tracepoints
  395. (and breakpoints) at the same address behave correctly. */
  396. /* A fast tracepoint jump. */
  397. struct fast_tracepoint_jump
  398. {
  399. struct fast_tracepoint_jump *next;
  400. /* A reference count. GDB can install more than one fast tracepoint
  401. at the same address (each with its own action list, for
  402. example). */
  403. int refcount;
  404. /* The fast tracepoint's insertion address. There can only be one
  405. of these for a given PC. */
  406. CORE_ADDR pc;
  407. /* Non-zero if this fast tracepoint jump is currently inserted in
  408. the inferior. */
  409. int inserted;
  410. /* The length of the jump instruction. */
  411. int length;
  412. /* A poor-man's flexible array member, holding both the jump
  413. instruction to insert, and a copy of the instruction that would
  414. be in memory had not been a jump there (the shadow memory of the
  415. tracepoint jump). */
  416. unsigned char insn_and_shadow[0];
  417. };
  418. /* Fast tracepoint FP's jump instruction to insert. */
  419. #define fast_tracepoint_jump_insn(fp) \
  420. ((fp)->insn_and_shadow + 0)
  421. /* The shadow memory of fast tracepoint jump FP. */
  422. #define fast_tracepoint_jump_shadow(fp) \
  423. ((fp)->insn_and_shadow + (fp)->length)
  424. /* Return the fast tracepoint jump set at WHERE. */
  425. static struct fast_tracepoint_jump *
  426. find_fast_tracepoint_jump_at (CORE_ADDR where)
  427. {
  428. struct process_info *proc = current_process ();
  429. struct fast_tracepoint_jump *jp;
  430. for (jp = proc->fast_tracepoint_jumps; jp != NULL; jp = jp->next)
  431. if (jp->pc == where)
  432. return jp;
  433. return NULL;
  434. }
  435. int
  436. fast_tracepoint_jump_here (CORE_ADDR where)
  437. {
  438. struct fast_tracepoint_jump *jp = find_fast_tracepoint_jump_at (where);
  439. return (jp != NULL);
  440. }
  441. int
  442. delete_fast_tracepoint_jump (struct fast_tracepoint_jump *todel)
  443. {
  444. struct fast_tracepoint_jump *bp, **bp_link;
  445. int ret;
  446. struct process_info *proc = current_process ();
  447. bp = proc->fast_tracepoint_jumps;
  448. bp_link = &proc->fast_tracepoint_jumps;
  449. while (bp)
  450. {
  451. if (bp == todel)
  452. {
  453. if (--bp->refcount == 0)
  454. {
  455. struct fast_tracepoint_jump *prev_bp_link = *bp_link;
  456. unsigned char *buf;
  457. /* Unlink it. */
  458. *bp_link = bp->next;
  459. /* Since there can be breakpoints inserted in the same
  460. address range, we use `target_write_memory', which
  461. takes care of layering breakpoints on top of fast
  462. tracepoints, and on top of the buffer we pass it.
  463. This works because we've already unlinked the fast
  464. tracepoint jump above. Also note that we need to
  465. pass the current shadow contents, because
  466. target_write_memory updates any shadow memory with
  467. what we pass here, and we want that to be a nop. */
  468. buf = (unsigned char *) alloca (bp->length);
  469. memcpy (buf, fast_tracepoint_jump_shadow (bp), bp->length);
  470. ret = target_write_memory (bp->pc, buf, bp->length);
  471. if (ret != 0)
  472. {
  473. /* Something went wrong, relink the jump. */
  474. *bp_link = prev_bp_link;
  475. threads_debug_printf
  476. ("Failed to uninsert fast tracepoint jump "
  477. "at 0x%s (%s) while deleting it.",
  478. paddress (bp->pc), safe_strerror (ret));
  479. return ret;
  480. }
  481. free (bp);
  482. }
  483. return 0;
  484. }
  485. else
  486. {
  487. bp_link = &bp->next;
  488. bp = *bp_link;
  489. }
  490. }
  491. warning ("Could not find fast tracepoint jump in list.");
  492. return ENOENT;
  493. }
  494. void
  495. inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump *jp)
  496. {
  497. jp->refcount++;
  498. }
  499. struct fast_tracepoint_jump *
  500. set_fast_tracepoint_jump (CORE_ADDR where,
  501. unsigned char *insn, ULONGEST length)
  502. {
  503. struct process_info *proc = current_process ();
  504. struct fast_tracepoint_jump *jp;
  505. int err;
  506. unsigned char *buf;
  507. /* We refcount fast tracepoint jumps. Check if we already know
  508. about a jump at this address. */
  509. jp = find_fast_tracepoint_jump_at (where);
  510. if (jp != NULL)
  511. {
  512. jp->refcount++;
  513. return jp;
  514. }
  515. /* We don't, so create a new object. Double the length, because the
  516. flexible array member holds both the jump insn, and the
  517. shadow. */
  518. jp = (struct fast_tracepoint_jump *) xcalloc (1, sizeof (*jp) + (length * 2));
  519. jp->pc = where;
  520. jp->length = length;
  521. memcpy (fast_tracepoint_jump_insn (jp), insn, length);
  522. jp->refcount = 1;
  523. buf = (unsigned char *) alloca (length);
  524. /* Note that there can be trap breakpoints inserted in the same
  525. address range. To access the original memory contents, we use
  526. `read_inferior_memory', which masks out breakpoints. */
  527. err = read_inferior_memory (where, buf, length);
  528. if (err != 0)
  529. {
  530. threads_debug_printf ("Failed to read shadow memory of"
  531. " fast tracepoint at 0x%s (%s).",
  532. paddress (where), safe_strerror (err));
  533. free (jp);
  534. return NULL;
  535. }
  536. memcpy (fast_tracepoint_jump_shadow (jp), buf, length);
  537. /* Link the jump in. */
  538. jp->inserted = 1;
  539. jp->next = proc->fast_tracepoint_jumps;
  540. proc->fast_tracepoint_jumps = jp;
  541. /* Since there can be trap breakpoints inserted in the same address
  542. range, we use use `target_write_memory', which takes care of
  543. layering breakpoints on top of fast tracepoints, on top of the
  544. buffer we pass it. This works because we've already linked in
  545. the fast tracepoint jump above. Also note that we need to pass
  546. the current shadow contents, because target_write_memory
  547. updates any shadow memory with what we pass here, and we want
  548. that to be a nop. */
  549. err = target_write_memory (where, buf, length);
  550. if (err != 0)
  551. {
  552. threads_debug_printf
  553. ("Failed to insert fast tracepoint jump at 0x%s (%s).",
  554. paddress (where), safe_strerror (err));
  555. /* Unlink it. */
  556. proc->fast_tracepoint_jumps = jp->next;
  557. free (jp);
  558. return NULL;
  559. }
  560. return jp;
  561. }
  562. void
  563. uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc)
  564. {
  565. struct fast_tracepoint_jump *jp;
  566. int err;
  567. jp = find_fast_tracepoint_jump_at (pc);
  568. if (jp == NULL)
  569. {
  570. /* This can happen when we remove all breakpoints while handling
  571. a step-over. */
  572. threads_debug_printf ("Could not find fast tracepoint jump at 0x%s "
  573. "in list (uninserting).",
  574. paddress (pc));
  575. return;
  576. }
  577. if (jp->inserted)
  578. {
  579. unsigned char *buf;
  580. jp->inserted = 0;
  581. /* Since there can be trap breakpoints inserted in the same
  582. address range, we use use `target_write_memory', which
  583. takes care of layering breakpoints on top of fast
  584. tracepoints, and on top of the buffer we pass it. This works
  585. because we've already marked the fast tracepoint fast
  586. tracepoint jump uninserted above. Also note that we need to
  587. pass the current shadow contents, because
  588. target_write_memory updates any shadow memory with what we
  589. pass here, and we want that to be a nop. */
  590. buf = (unsigned char *) alloca (jp->length);
  591. memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
  592. err = target_write_memory (jp->pc, buf, jp->length);
  593. if (err != 0)
  594. {
  595. jp->inserted = 1;
  596. threads_debug_printf ("Failed to uninsert fast tracepoint jump at"
  597. " 0x%s (%s).",
  598. paddress (pc), safe_strerror (err));
  599. }
  600. }
  601. }
  602. void
  603. reinsert_fast_tracepoint_jumps_at (CORE_ADDR where)
  604. {
  605. struct fast_tracepoint_jump *jp;
  606. int err;
  607. unsigned char *buf;
  608. jp = find_fast_tracepoint_jump_at (where);
  609. if (jp == NULL)
  610. {
  611. /* This can happen when we remove breakpoints when a tracepoint
  612. hit causes a tracing stop, while handling a step-over. */
  613. threads_debug_printf ("Could not find fast tracepoint jump at 0x%s "
  614. "in list (reinserting).",
  615. paddress (where));
  616. return;
  617. }
  618. if (jp->inserted)
  619. error ("Jump already inserted at reinsert time.");
  620. jp->inserted = 1;
  621. /* Since there can be trap breakpoints inserted in the same address
  622. range, we use `target_write_memory', which takes care of
  623. layering breakpoints on top of fast tracepoints, and on top of
  624. the buffer we pass it. This works because we've already marked
  625. the fast tracepoint jump inserted above. Also note that we need
  626. to pass the current shadow contents, because
  627. target_write_memory updates any shadow memory with what we pass
  628. here, and we want that to be a nop. */
  629. buf = (unsigned char *) alloca (jp->length);
  630. memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
  631. err = target_write_memory (where, buf, jp->length);
  632. if (err != 0)
  633. {
  634. jp->inserted = 0;
  635. threads_debug_printf ("Failed to reinsert fast tracepoint jump at"
  636. " 0x%s (%s).",
  637. paddress (where), safe_strerror (err));
  638. }
  639. }
  640. /* Set a high-level breakpoint of type TYPE, with low level type
  641. RAW_TYPE and kind KIND, at WHERE. On success, a pointer to the new
  642. breakpoint is returned. On failure, returns NULL and writes the
  643. error code to *ERR. HANDLER is called when the breakpoint is hit.
  644. HANDLER should return 1 if the breakpoint should be deleted, 0
  645. otherwise. */
  646. static struct breakpoint *
  647. set_breakpoint (enum bkpt_type type, enum raw_bkpt_type raw_type,
  648. CORE_ADDR where, int kind,
  649. int (*handler) (CORE_ADDR), int *err)
  650. {
  651. struct process_info *proc = current_process ();
  652. struct breakpoint *bp;
  653. struct raw_breakpoint *raw;
  654. raw = set_raw_breakpoint_at (raw_type, where, kind, err);
  655. if (raw == NULL)
  656. {
  657. /* warn? */
  658. return NULL;
  659. }
  660. if (is_gdb_breakpoint (type))
  661. {
  662. struct gdb_breakpoint *gdb_bp = XCNEW (struct gdb_breakpoint);
  663. bp = (struct breakpoint *) gdb_bp;
  664. gdb_assert (handler == NULL);
  665. }
  666. else if (type == other_breakpoint)
  667. {
  668. struct other_breakpoint *other_bp = XCNEW (struct other_breakpoint);
  669. other_bp->handler = handler;
  670. bp = (struct breakpoint *) other_bp;
  671. }
  672. else if (type == single_step_breakpoint)
  673. {
  674. struct single_step_breakpoint *ss_bp
  675. = XCNEW (struct single_step_breakpoint);
  676. bp = (struct breakpoint *) ss_bp;
  677. }
  678. else
  679. gdb_assert_not_reached ("unhandled breakpoint type");
  680. bp->type = type;
  681. bp->raw = raw;
  682. bp->next = proc->breakpoints;
  683. proc->breakpoints = bp;
  684. return bp;
  685. }
  686. /* Set breakpoint of TYPE on address WHERE with handler HANDLER. */
  687. static struct breakpoint *
  688. set_breakpoint_type_at (enum bkpt_type type, CORE_ADDR where,
  689. int (*handler) (CORE_ADDR))
  690. {
  691. int err_ignored;
  692. CORE_ADDR placed_address = where;
  693. int breakpoint_kind = target_breakpoint_kind_from_pc (&placed_address);
  694. return set_breakpoint (type, raw_bkpt_type_sw,
  695. placed_address, breakpoint_kind, handler,
  696. &err_ignored);
  697. }
  698. /* See mem-break.h */
  699. struct breakpoint *
  700. set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
  701. {
  702. return set_breakpoint_type_at (other_breakpoint, where, handler);
  703. }
  704. static int
  705. delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
  706. {
  707. struct raw_breakpoint *bp, **bp_link;
  708. int ret;
  709. bp = proc->raw_breakpoints;
  710. bp_link = &proc->raw_breakpoints;
  711. while (bp)
  712. {
  713. if (bp == todel)
  714. {
  715. if (bp->inserted > 0)
  716. {
  717. struct raw_breakpoint *prev_bp_link = *bp_link;
  718. *bp_link = bp->next;
  719. ret = the_target->remove_point (bp->raw_type, bp->pc,
  720. bp->kind, bp);
  721. if (ret != 0)
  722. {
  723. /* Something went wrong, relink the breakpoint. */
  724. *bp_link = prev_bp_link;
  725. threads_debug_printf ("Failed to uninsert raw breakpoint "
  726. "at 0x%s while deleting it.",
  727. paddress (bp->pc));
  728. return ret;
  729. }
  730. }
  731. else
  732. *bp_link = bp->next;
  733. free (bp);
  734. return 0;
  735. }
  736. else
  737. {
  738. bp_link = &bp->next;
  739. bp = *bp_link;
  740. }
  741. }
  742. warning ("Could not find raw breakpoint in list.");
  743. return ENOENT;
  744. }
  745. static int
  746. release_breakpoint (struct process_info *proc, struct breakpoint *bp)
  747. {
  748. int newrefcount;
  749. int ret;
  750. newrefcount = bp->raw->refcount - 1;
  751. if (newrefcount == 0)
  752. {
  753. ret = delete_raw_breakpoint (proc, bp->raw);
  754. if (ret != 0)
  755. return ret;
  756. }
  757. else
  758. bp->raw->refcount = newrefcount;
  759. free (bp);
  760. return 0;
  761. }
  762. static int
  763. delete_breakpoint_1 (struct process_info *proc, struct breakpoint *todel)
  764. {
  765. struct breakpoint *bp, **bp_link;
  766. int err;
  767. bp = proc->breakpoints;
  768. bp_link = &proc->breakpoints;
  769. while (bp)
  770. {
  771. if (bp == todel)
  772. {
  773. *bp_link = bp->next;
  774. err = release_breakpoint (proc, bp);
  775. if (err != 0)
  776. return err;
  777. bp = *bp_link;
  778. return 0;
  779. }
  780. else
  781. {
  782. bp_link = &bp->next;
  783. bp = *bp_link;
  784. }
  785. }
  786. warning ("Could not find breakpoint in list.");
  787. return ENOENT;
  788. }
  789. int
  790. delete_breakpoint (struct breakpoint *todel)
  791. {
  792. struct process_info *proc = current_process ();
  793. return delete_breakpoint_1 (proc, todel);
  794. }
  795. /* Locate a GDB breakpoint of type Z_TYPE and kind KIND placed at
  796. address ADDR and return a pointer to its structure. If KIND is -1,
  797. the breakpoint's kind is ignored. */
  798. static struct gdb_breakpoint *
  799. find_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind)
  800. {
  801. struct process_info *proc = current_process ();
  802. struct breakpoint *bp;
  803. enum bkpt_type type = Z_packet_to_bkpt_type (z_type);
  804. for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
  805. if (bp->type == type && bp->raw->pc == addr
  806. && (kind == -1 || bp->raw->kind == kind))
  807. return (struct gdb_breakpoint *) bp;
  808. return NULL;
  809. }
  810. static int
  811. z_type_supported (char z_type)
  812. {
  813. return (z_type >= '0' && z_type <= '4'
  814. && the_target->supports_z_point_type (z_type));
  815. }
  816. /* Create a new GDB breakpoint of type Z_TYPE at ADDR with kind KIND.
  817. Returns a pointer to the newly created breakpoint on success. On
  818. failure returns NULL and sets *ERR to either -1 for error, or 1 if
  819. Z_TYPE breakpoints are not supported on this target. */
  820. static struct gdb_breakpoint *
  821. set_gdb_breakpoint_1 (char z_type, CORE_ADDR addr, int kind, int *err)
  822. {
  823. struct gdb_breakpoint *bp;
  824. enum bkpt_type type;
  825. enum raw_bkpt_type raw_type;
  826. /* If we see GDB inserting a second code breakpoint at the same
  827. address, then either: GDB is updating the breakpoint's conditions
  828. or commands; or, the first breakpoint must have disappeared due
  829. to a shared library unload. On targets where the shared
  830. libraries are handled by userspace, like SVR4, for example,
  831. GDBserver can't tell if a library was loaded or unloaded. Since
  832. we refcount raw breakpoints, we must be careful to make sure GDB
  833. breakpoints never contribute more than one reference. if we
  834. didn't do this, in case the previous breakpoint is gone due to a
  835. shared library unload, we'd just increase the refcount of the
  836. previous breakpoint at this address, but the trap was not planted
  837. in the inferior anymore, thus the breakpoint would never be hit.
  838. Note this must be careful to not create a window where
  839. breakpoints are removed from the target, for non-stop, in case
  840. the target can poke at memory while the program is running. */
  841. if (z_type == Z_PACKET_SW_BP
  842. || z_type == Z_PACKET_HW_BP)
  843. {
  844. bp = find_gdb_breakpoint (z_type, addr, -1);
  845. if (bp != NULL)
  846. {
  847. if (bp->base.raw->kind != kind)
  848. {
  849. /* A different kind than previously seen. The previous
  850. breakpoint must be gone then. */
  851. bp->base.raw->inserted = -1;
  852. delete_breakpoint ((struct breakpoint *) bp);
  853. bp = NULL;
  854. }
  855. else if (z_type == Z_PACKET_SW_BP)
  856. {
  857. /* Check if the breakpoint is actually gone from the
  858. target, due to an solib unload, for example. Might
  859. as well validate _all_ breakpoints. */
  860. validate_breakpoints ();
  861. /* Breakpoints that don't pass validation are
  862. deleted. */
  863. bp = find_gdb_breakpoint (z_type, addr, -1);
  864. }
  865. }
  866. }
  867. else
  868. {
  869. /* Data breakpoints for the same address but different kind are
  870. expected. GDB doesn't merge these. The backend gets to do
  871. that if it wants/can. */
  872. bp = find_gdb_breakpoint (z_type, addr, kind);
  873. }
  874. if (bp != NULL)
  875. {
  876. /* We already know about this breakpoint, there's nothing else
  877. to do - GDB's reference is already accounted for. Note that
  878. whether the breakpoint inserted is left as is - we may be
  879. stepping over it, for example, in which case we don't want to
  880. force-reinsert it. */
  881. return bp;
  882. }
  883. raw_type = Z_packet_to_raw_bkpt_type (z_type);
  884. type = Z_packet_to_bkpt_type (z_type);
  885. return (struct gdb_breakpoint *) set_breakpoint (type, raw_type, addr,
  886. kind, NULL, err);
  887. }
  888. static int
  889. check_gdb_bp_preconditions (char z_type, int *err)
  890. {
  891. /* As software/memory breakpoints work by poking at memory, we need
  892. to prepare to access memory. If that operation fails, we need to
  893. return error. Seeing an error, if this is the first breakpoint
  894. of that type that GDB tries to insert, GDB would then assume the
  895. breakpoint type is supported, but it may actually not be. So we
  896. need to check whether the type is supported at all before
  897. preparing to access memory. */
  898. if (!z_type_supported (z_type))
  899. {
  900. *err = 1;
  901. return 0;
  902. }
  903. return 1;
  904. }
  905. /* See mem-break.h. This is a wrapper for set_gdb_breakpoint_1 that
  906. knows to prepare to access memory for Z0 breakpoints. */
  907. struct gdb_breakpoint *
  908. set_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind, int *err)
  909. {
  910. struct gdb_breakpoint *bp;
  911. if (!check_gdb_bp_preconditions (z_type, err))
  912. return NULL;
  913. /* If inserting a software/memory breakpoint, need to prepare to
  914. access memory. */
  915. if (z_type == Z_PACKET_SW_BP)
  916. {
  917. if (prepare_to_access_memory () != 0)
  918. {
  919. *err = -1;
  920. return NULL;
  921. }
  922. }
  923. bp = set_gdb_breakpoint_1 (z_type, addr, kind, err);
  924. if (z_type == Z_PACKET_SW_BP)
  925. done_accessing_memory ();
  926. return bp;
  927. }
  928. /* Delete a GDB breakpoint of type Z_TYPE and kind KIND previously
  929. inserted at ADDR with set_gdb_breakpoint_at. Returns 0 on success,
  930. -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
  931. target. */
  932. static int
  933. delete_gdb_breakpoint_1 (char z_type, CORE_ADDR addr, int kind)
  934. {
  935. struct gdb_breakpoint *bp;
  936. int err;
  937. bp = find_gdb_breakpoint (z_type, addr, kind);
  938. if (bp == NULL)
  939. return -1;
  940. /* Before deleting the breakpoint, make sure to free its condition
  941. and command lists. */
  942. clear_breakpoint_conditions_and_commands (bp);
  943. err = delete_breakpoint ((struct breakpoint *) bp);
  944. if (err != 0)
  945. return -1;
  946. return 0;
  947. }
  948. /* See mem-break.h. This is a wrapper for delete_gdb_breakpoint that
  949. knows to prepare to access memory for Z0 breakpoints. */
  950. int
  951. delete_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind)
  952. {
  953. int ret;
  954. if (!check_gdb_bp_preconditions (z_type, &ret))
  955. return ret;
  956. /* If inserting a software/memory breakpoint, need to prepare to
  957. access memory. */
  958. if (z_type == Z_PACKET_SW_BP)
  959. {
  960. int err;
  961. err = prepare_to_access_memory ();
  962. if (err != 0)
  963. return -1;
  964. }
  965. ret = delete_gdb_breakpoint_1 (z_type, addr, kind);
  966. if (z_type == Z_PACKET_SW_BP)
  967. done_accessing_memory ();
  968. return ret;
  969. }
  970. /* Clear all conditions associated with a breakpoint. */
  971. static void
  972. clear_breakpoint_conditions (struct gdb_breakpoint *bp)
  973. {
  974. struct point_cond_list *cond;
  975. if (bp->cond_list == NULL)
  976. return;
  977. cond = bp->cond_list;
  978. while (cond != NULL)
  979. {
  980. struct point_cond_list *cond_next;
  981. cond_next = cond->next;
  982. gdb_free_agent_expr (cond->cond);
  983. free (cond);
  984. cond = cond_next;
  985. }
  986. bp->cond_list = NULL;
  987. }
  988. /* Clear all commands associated with a breakpoint. */
  989. static void
  990. clear_breakpoint_commands (struct gdb_breakpoint *bp)
  991. {
  992. struct point_command_list *cmd;
  993. if (bp->command_list == NULL)
  994. return;
  995. cmd = bp->command_list;
  996. while (cmd != NULL)
  997. {
  998. struct point_command_list *cmd_next;
  999. cmd_next = cmd->next;
  1000. gdb_free_agent_expr (cmd->cmd);
  1001. free (cmd);
  1002. cmd = cmd_next;
  1003. }
  1004. bp->command_list = NULL;
  1005. }
  1006. void
  1007. clear_breakpoint_conditions_and_commands (struct gdb_breakpoint *bp)
  1008. {
  1009. clear_breakpoint_conditions (bp);
  1010. clear_breakpoint_commands (bp);
  1011. }
  1012. /* Add condition CONDITION to GDBserver's breakpoint BP. */
  1013. static void
  1014. add_condition_to_breakpoint (struct gdb_breakpoint *bp,
  1015. struct agent_expr *condition)
  1016. {
  1017. struct point_cond_list *new_cond;
  1018. /* Create new condition. */
  1019. new_cond = XCNEW (struct point_cond_list);
  1020. new_cond->cond = condition;
  1021. /* Add condition to the list. */
  1022. new_cond->next = bp->cond_list;
  1023. bp->cond_list = new_cond;
  1024. }
  1025. /* Add a target-side condition CONDITION to a breakpoint. */
  1026. int
  1027. add_breakpoint_condition (struct gdb_breakpoint *bp, const char **condition)
  1028. {
  1029. const char *actparm = *condition;
  1030. struct agent_expr *cond;
  1031. if (condition == NULL)
  1032. return 1;
  1033. if (bp == NULL)
  1034. return 0;
  1035. cond = gdb_parse_agent_expr (&actparm);
  1036. if (cond == NULL)
  1037. {
  1038. warning ("Condition evaluation failed. Assuming unconditional.");
  1039. return 0;
  1040. }
  1041. add_condition_to_breakpoint (bp, cond);
  1042. *condition = actparm;
  1043. return 1;
  1044. }
  1045. /* Evaluate condition (if any) at breakpoint BP. Return 1 if
  1046. true and 0 otherwise. */
  1047. static int
  1048. gdb_condition_true_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
  1049. {
  1050. /* Fetch registers for the current inferior. */
  1051. struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
  1052. ULONGEST value = 0;
  1053. struct point_cond_list *cl;
  1054. int err = 0;
  1055. struct eval_agent_expr_context ctx;
  1056. if (bp == NULL)
  1057. return 0;
  1058. /* Check if the breakpoint is unconditional. If it is,
  1059. the condition always evaluates to TRUE. */
  1060. if (bp->cond_list == NULL)
  1061. return 1;
  1062. ctx.regcache = get_thread_regcache (current_thread, 1);
  1063. ctx.tframe = NULL;
  1064. ctx.tpoint = NULL;
  1065. /* Evaluate each condition in the breakpoint's list of conditions.
  1066. Return true if any of the conditions evaluates to TRUE.
  1067. If we failed to evaluate the expression, TRUE is returned. This
  1068. forces GDB to reevaluate the conditions. */
  1069. for (cl = bp->cond_list;
  1070. cl && !value && !err; cl = cl->next)
  1071. {
  1072. /* Evaluate the condition. */
  1073. err = gdb_eval_agent_expr (&ctx, cl->cond, &value);
  1074. }
  1075. if (err)
  1076. return 1;
  1077. return (value != 0);
  1078. }
  1079. int
  1080. gdb_condition_true_at_breakpoint (CORE_ADDR where)
  1081. {
  1082. /* Only check code (software or hardware) breakpoints. */
  1083. return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
  1084. || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
  1085. }
  1086. /* Add commands COMMANDS to GDBserver's breakpoint BP. */
  1087. static void
  1088. add_commands_to_breakpoint (struct gdb_breakpoint *bp,
  1089. struct agent_expr *commands, int persist)
  1090. {
  1091. struct point_command_list *new_cmd;
  1092. /* Create new command. */
  1093. new_cmd = XCNEW (struct point_command_list);
  1094. new_cmd->cmd = commands;
  1095. new_cmd->persistence = persist;
  1096. /* Add commands to the list. */
  1097. new_cmd->next = bp->command_list;
  1098. bp->command_list = new_cmd;
  1099. }
  1100. /* Add a target-side command COMMAND to the breakpoint at ADDR. */
  1101. int
  1102. add_breakpoint_commands (struct gdb_breakpoint *bp, const char **command,
  1103. int persist)
  1104. {
  1105. const char *actparm = *command;
  1106. struct agent_expr *cmd;
  1107. if (command == NULL)
  1108. return 1;
  1109. if (bp == NULL)
  1110. return 0;
  1111. cmd = gdb_parse_agent_expr (&actparm);
  1112. if (cmd == NULL)
  1113. {
  1114. warning ("Command evaluation failed. Disabling.");
  1115. return 0;
  1116. }
  1117. add_commands_to_breakpoint (bp, cmd, persist);
  1118. *command = actparm;
  1119. return 1;
  1120. }
  1121. /* Return true if there are no commands to run at this location,
  1122. which likely means we want to report back to GDB. */
  1123. static int
  1124. gdb_no_commands_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
  1125. {
  1126. struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
  1127. if (bp == NULL)
  1128. return 1;
  1129. threads_debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s",
  1130. paddress (addr), z_type,
  1131. phex_nz ((uintptr_t) bp->command_list, 0));
  1132. return (bp->command_list == NULL);
  1133. }
  1134. /* Return true if there are no commands to run at this location,
  1135. which likely means we want to report back to GDB. */
  1136. int
  1137. gdb_no_commands_at_breakpoint (CORE_ADDR where)
  1138. {
  1139. /* Only check code (software or hardware) breakpoints. */
  1140. return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
  1141. && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
  1142. }
  1143. /* Run a breakpoint's commands. Returns 0 if there was a problem
  1144. running any command, 1 otherwise. */
  1145. static int
  1146. run_breakpoint_commands_z_type (char z_type, CORE_ADDR addr)
  1147. {
  1148. /* Fetch registers for the current inferior. */
  1149. struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
  1150. ULONGEST value = 0;
  1151. struct point_command_list *cl;
  1152. int err = 0;
  1153. struct eval_agent_expr_context ctx;
  1154. if (bp == NULL)
  1155. return 1;
  1156. ctx.regcache = get_thread_regcache (current_thread, 1);
  1157. ctx.tframe = NULL;
  1158. ctx.tpoint = NULL;
  1159. for (cl = bp->command_list;
  1160. cl && !value && !err; cl = cl->next)
  1161. {
  1162. /* Run the command. */
  1163. err = gdb_eval_agent_expr (&ctx, cl->cmd, &value);
  1164. /* If one command has a problem, stop digging the hole deeper. */
  1165. if (err)
  1166. return 0;
  1167. }
  1168. return 1;
  1169. }
  1170. void
  1171. run_breakpoint_commands (CORE_ADDR where)
  1172. {
  1173. /* Only check code (software or hardware) breakpoints. If one
  1174. command has a problem, stop digging the hole deeper. */
  1175. if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP, where))
  1176. run_breakpoint_commands_z_type (Z_PACKET_HW_BP, where);
  1177. }
  1178. /* See mem-break.h. */
  1179. int
  1180. gdb_breakpoint_here (CORE_ADDR where)
  1181. {
  1182. /* Only check code (software or hardware) breakpoints. */
  1183. return (find_gdb_breakpoint (Z_PACKET_SW_BP, where, -1) != NULL
  1184. || find_gdb_breakpoint (Z_PACKET_HW_BP, where, -1) != NULL);
  1185. }
  1186. void
  1187. set_single_step_breakpoint (CORE_ADDR stop_at, ptid_t ptid)
  1188. {
  1189. struct single_step_breakpoint *bp;
  1190. gdb_assert (current_ptid.pid () == ptid.pid ());
  1191. bp = (struct single_step_breakpoint *) set_breakpoint_type_at (single_step_breakpoint,
  1192. stop_at, NULL);
  1193. bp->ptid = ptid;
  1194. }
  1195. void
  1196. delete_single_step_breakpoints (struct thread_info *thread)
  1197. {
  1198. struct process_info *proc = get_thread_process (thread);
  1199. struct breakpoint *bp, **bp_link;
  1200. bp = proc->breakpoints;
  1201. bp_link = &proc->breakpoints;
  1202. while (bp)
  1203. {
  1204. if (bp->type == single_step_breakpoint
  1205. && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
  1206. {
  1207. scoped_restore_current_thread restore_thread;
  1208. switch_to_thread (thread);
  1209. *bp_link = bp->next;
  1210. release_breakpoint (proc, bp);
  1211. bp = *bp_link;
  1212. }
  1213. else
  1214. {
  1215. bp_link = &bp->next;
  1216. bp = *bp_link;
  1217. }
  1218. }
  1219. }
  1220. static void
  1221. uninsert_raw_breakpoint (struct raw_breakpoint *bp)
  1222. {
  1223. if (bp->inserted < 0)
  1224. {
  1225. threads_debug_printf ("Breakpoint at %s is marked insert-disabled.",
  1226. paddress (bp->pc));
  1227. }
  1228. else if (bp->inserted > 0)
  1229. {
  1230. int err;
  1231. bp->inserted = 0;
  1232. err = the_target->remove_point (bp->raw_type, bp->pc, bp->kind, bp);
  1233. if (err != 0)
  1234. {
  1235. bp->inserted = 1;
  1236. threads_debug_printf ("Failed to uninsert raw breakpoint at 0x%s.",
  1237. paddress (bp->pc));
  1238. }
  1239. }
  1240. }
  1241. void
  1242. uninsert_breakpoints_at (CORE_ADDR pc)
  1243. {
  1244. struct process_info *proc = current_process ();
  1245. struct raw_breakpoint *bp;
  1246. int found = 0;
  1247. for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1248. if ((bp->raw_type == raw_bkpt_type_sw
  1249. || bp->raw_type == raw_bkpt_type_hw)
  1250. && bp->pc == pc)
  1251. {
  1252. found = 1;
  1253. if (bp->inserted)
  1254. uninsert_raw_breakpoint (bp);
  1255. }
  1256. if (!found)
  1257. {
  1258. /* This can happen when we remove all breakpoints while handling
  1259. a step-over. */
  1260. threads_debug_printf ("Could not find breakpoint at 0x%s "
  1261. "in list (uninserting).",
  1262. paddress (pc));
  1263. }
  1264. }
  1265. void
  1266. uninsert_all_breakpoints (void)
  1267. {
  1268. struct process_info *proc = current_process ();
  1269. struct raw_breakpoint *bp;
  1270. for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1271. if ((bp->raw_type == raw_bkpt_type_sw
  1272. || bp->raw_type == raw_bkpt_type_hw)
  1273. && bp->inserted)
  1274. uninsert_raw_breakpoint (bp);
  1275. }
  1276. void
  1277. uninsert_single_step_breakpoints (struct thread_info *thread)
  1278. {
  1279. struct process_info *proc = get_thread_process (thread);
  1280. struct breakpoint *bp;
  1281. for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
  1282. {
  1283. if (bp->type == single_step_breakpoint
  1284. && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
  1285. {
  1286. gdb_assert (bp->raw->inserted > 0);
  1287. /* Only uninsert the raw breakpoint if it only belongs to a
  1288. reinsert breakpoint. */
  1289. if (bp->raw->refcount == 1)
  1290. {
  1291. scoped_restore_current_thread restore_thread;
  1292. switch_to_thread (thread);
  1293. uninsert_raw_breakpoint (bp->raw);
  1294. }
  1295. }
  1296. }
  1297. }
  1298. static void
  1299. reinsert_raw_breakpoint (struct raw_breakpoint *bp)
  1300. {
  1301. int err;
  1302. if (bp->inserted)
  1303. return;
  1304. err = the_target->insert_point (bp->raw_type, bp->pc, bp->kind, bp);
  1305. if (err == 0)
  1306. bp->inserted = 1;
  1307. else
  1308. threads_debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).",
  1309. paddress (bp->pc), err);
  1310. }
  1311. void
  1312. reinsert_breakpoints_at (CORE_ADDR pc)
  1313. {
  1314. struct process_info *proc = current_process ();
  1315. struct raw_breakpoint *bp;
  1316. int found = 0;
  1317. for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1318. if ((bp->raw_type == raw_bkpt_type_sw
  1319. || bp->raw_type == raw_bkpt_type_hw)
  1320. && bp->pc == pc)
  1321. {
  1322. found = 1;
  1323. reinsert_raw_breakpoint (bp);
  1324. }
  1325. if (!found)
  1326. {
  1327. /* This can happen when we remove all breakpoints while handling
  1328. a step-over. */
  1329. threads_debug_printf ("Could not find raw breakpoint at 0x%s "
  1330. "in list (reinserting).",
  1331. paddress (pc));
  1332. }
  1333. }
  1334. int
  1335. has_single_step_breakpoints (struct thread_info *thread)
  1336. {
  1337. struct process_info *proc = get_thread_process (thread);
  1338. struct breakpoint *bp, **bp_link;
  1339. bp = proc->breakpoints;
  1340. bp_link = &proc->breakpoints;
  1341. while (bp)
  1342. {
  1343. if (bp->type == single_step_breakpoint
  1344. && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
  1345. return 1;
  1346. else
  1347. {
  1348. bp_link = &bp->next;
  1349. bp = *bp_link;
  1350. }
  1351. }
  1352. return 0;
  1353. }
  1354. void
  1355. reinsert_all_breakpoints (void)
  1356. {
  1357. struct process_info *proc = current_process ();
  1358. struct raw_breakpoint *bp;
  1359. for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1360. if ((bp->raw_type == raw_bkpt_type_sw
  1361. || bp->raw_type == raw_bkpt_type_hw)
  1362. && !bp->inserted)
  1363. reinsert_raw_breakpoint (bp);
  1364. }
  1365. void
  1366. reinsert_single_step_breakpoints (struct thread_info *thread)
  1367. {
  1368. struct process_info *proc = get_thread_process (thread);
  1369. struct breakpoint *bp;
  1370. for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
  1371. {
  1372. if (bp->type == single_step_breakpoint
  1373. && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
  1374. {
  1375. gdb_assert (bp->raw->inserted > 0);
  1376. if (bp->raw->refcount == 1)
  1377. {
  1378. scoped_restore_current_thread restore_thread;
  1379. switch_to_thread (thread);
  1380. reinsert_raw_breakpoint (bp->raw);
  1381. }
  1382. }
  1383. }
  1384. }
  1385. void
  1386. check_breakpoints (CORE_ADDR stop_pc)
  1387. {
  1388. struct process_info *proc = current_process ();
  1389. struct breakpoint *bp, **bp_link;
  1390. bp = proc->breakpoints;
  1391. bp_link = &proc->breakpoints;
  1392. while (bp)
  1393. {
  1394. struct raw_breakpoint *raw = bp->raw;
  1395. if ((raw->raw_type == raw_bkpt_type_sw
  1396. || raw->raw_type == raw_bkpt_type_hw)
  1397. && raw->pc == stop_pc)
  1398. {
  1399. if (!raw->inserted)
  1400. {
  1401. warning ("Hit a removed breakpoint?");
  1402. return;
  1403. }
  1404. if (bp->type == other_breakpoint)
  1405. {
  1406. struct other_breakpoint *other_bp
  1407. = (struct other_breakpoint *) bp;
  1408. if (other_bp->handler != NULL && (*other_bp->handler) (stop_pc))
  1409. {
  1410. *bp_link = bp->next;
  1411. release_breakpoint (proc, bp);
  1412. bp = *bp_link;
  1413. continue;
  1414. }
  1415. }
  1416. }
  1417. bp_link = &bp->next;
  1418. bp = *bp_link;
  1419. }
  1420. }
  1421. int
  1422. breakpoint_here (CORE_ADDR addr)
  1423. {
  1424. struct process_info *proc = current_process ();
  1425. struct raw_breakpoint *bp;
  1426. for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1427. if ((bp->raw_type == raw_bkpt_type_sw
  1428. || bp->raw_type == raw_bkpt_type_hw)
  1429. && bp->pc == addr)
  1430. return 1;
  1431. return 0;
  1432. }
  1433. int
  1434. breakpoint_inserted_here (CORE_ADDR addr)
  1435. {
  1436. struct process_info *proc = current_process ();
  1437. struct raw_breakpoint *bp;
  1438. for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1439. if ((bp->raw_type == raw_bkpt_type_sw
  1440. || bp->raw_type == raw_bkpt_type_hw)
  1441. && bp->pc == addr
  1442. && bp->inserted)
  1443. return 1;
  1444. return 0;
  1445. }
  1446. /* See mem-break.h. */
  1447. int
  1448. software_breakpoint_inserted_here (CORE_ADDR addr)
  1449. {
  1450. struct process_info *proc = current_process ();
  1451. struct raw_breakpoint *bp;
  1452. for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1453. if (bp->raw_type == raw_bkpt_type_sw
  1454. && bp->pc == addr
  1455. && bp->inserted)
  1456. return 1;
  1457. return 0;
  1458. }
  1459. /* See mem-break.h. */
  1460. int
  1461. hardware_breakpoint_inserted_here (CORE_ADDR addr)
  1462. {
  1463. struct process_info *proc = current_process ();
  1464. struct raw_breakpoint *bp;
  1465. for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
  1466. if (bp->raw_type == raw_bkpt_type_hw
  1467. && bp->pc == addr
  1468. && bp->inserted)
  1469. return 1;
  1470. return 0;
  1471. }
  1472. /* See mem-break.h. */
  1473. int
  1474. single_step_breakpoint_inserted_here (CORE_ADDR addr)
  1475. {
  1476. struct process_info *proc = current_process ();
  1477. struct breakpoint *bp;
  1478. for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
  1479. if (bp->type == single_step_breakpoint
  1480. && bp->raw->pc == addr
  1481. && bp->raw->inserted)
  1482. return 1;
  1483. return 0;
  1484. }
  1485. static int
  1486. validate_inserted_breakpoint (struct raw_breakpoint *bp)
  1487. {
  1488. unsigned char *buf;
  1489. int err;
  1490. gdb_assert (bp->inserted);
  1491. gdb_assert (bp->raw_type == raw_bkpt_type_sw);
  1492. buf = (unsigned char *) alloca (bp_size (bp));
  1493. err = the_target->read_memory (bp->pc, buf, bp_size (bp));
  1494. if (err || memcmp (buf, bp_opcode (bp), bp_size (bp)) != 0)
  1495. {
  1496. /* Tag it as gone. */
  1497. bp->inserted = -1;
  1498. return 0;
  1499. }
  1500. return 1;
  1501. }
  1502. static void
  1503. delete_disabled_breakpoints (void)
  1504. {
  1505. struct process_info *proc = current_process ();
  1506. struct breakpoint *bp, *next;
  1507. for (bp = proc->breakpoints; bp != NULL; bp = next)
  1508. {
  1509. next = bp->next;
  1510. if (bp->raw->inserted < 0)
  1511. {
  1512. /* If single_step_breakpoints become disabled, that means the
  1513. manipulations (insertion and removal) of them are wrong. */
  1514. gdb_assert (bp->type != single_step_breakpoint);
  1515. delete_breakpoint_1 (proc, bp);
  1516. }
  1517. }
  1518. }
  1519. /* Check if breakpoints we inserted still appear to be inserted. They
  1520. may disappear due to a shared library unload, and worse, a new
  1521. shared library may be reloaded at the same address as the
  1522. previously unloaded one. If that happens, we should make sure that
  1523. the shadow memory of the old breakpoints isn't used when reading or
  1524. writing memory. */
  1525. void
  1526. validate_breakpoints (void)
  1527. {
  1528. struct process_info *proc = current_process ();
  1529. struct breakpoint *bp;
  1530. for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
  1531. {
  1532. struct raw_breakpoint *raw = bp->raw;
  1533. if (raw->raw_type == raw_bkpt_type_sw && raw->inserted > 0)
  1534. validate_inserted_breakpoint (raw);
  1535. }
  1536. delete_disabled_breakpoints ();
  1537. }
  1538. void
  1539. check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
  1540. {
  1541. struct process_info *proc = current_process ();
  1542. struct raw_breakpoint *bp = proc->raw_breakpoints;
  1543. struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
  1544. CORE_ADDR mem_end = mem_addr + mem_len;
  1545. int disabled_one = 0;
  1546. for (; jp != NULL; jp = jp->next)
  1547. {
  1548. CORE_ADDR bp_end = jp->pc + jp->length;
  1549. CORE_ADDR start, end;
  1550. int copy_offset, copy_len, buf_offset;
  1551. gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len
  1552. || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
  1553. if (mem_addr >= bp_end)
  1554. continue;
  1555. if (jp->pc >= mem_end)
  1556. continue;
  1557. start = jp->pc;
  1558. if (mem_addr > start)
  1559. start = mem_addr;
  1560. end = bp_end;
  1561. if (end > mem_end)
  1562. end = mem_end;
  1563. copy_len = end - start;
  1564. copy_offset = start - jp->pc;
  1565. buf_offset = start - mem_addr;
  1566. if (jp->inserted)
  1567. memcpy (buf + buf_offset,
  1568. fast_tracepoint_jump_shadow (jp) + copy_offset,
  1569. copy_len);
  1570. }
  1571. for (; bp != NULL; bp = bp->next)
  1572. {
  1573. CORE_ADDR bp_end = bp->pc + bp_size (bp);
  1574. CORE_ADDR start, end;
  1575. int copy_offset, copy_len, buf_offset;
  1576. if (bp->raw_type != raw_bkpt_type_sw)
  1577. continue;
  1578. gdb_assert (bp->old_data >= buf + mem_len
  1579. || buf >= &bp->old_data[sizeof (bp->old_data)]);
  1580. if (mem_addr >= bp_end)
  1581. continue;
  1582. if (bp->pc >= mem_end)
  1583. continue;
  1584. start = bp->pc;
  1585. if (mem_addr > start)
  1586. start = mem_addr;
  1587. end = bp_end;
  1588. if (end > mem_end)
  1589. end = mem_end;
  1590. copy_len = end - start;
  1591. copy_offset = start - bp->pc;
  1592. buf_offset = start - mem_addr;
  1593. if (bp->inserted > 0)
  1594. {
  1595. if (validate_inserted_breakpoint (bp))
  1596. memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
  1597. else
  1598. disabled_one = 1;
  1599. }
  1600. }
  1601. if (disabled_one)
  1602. delete_disabled_breakpoints ();
  1603. }
  1604. void
  1605. check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
  1606. const unsigned char *myaddr, int mem_len)
  1607. {
  1608. struct process_info *proc = current_process ();
  1609. struct raw_breakpoint *bp = proc->raw_breakpoints;
  1610. struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
  1611. CORE_ADDR mem_end = mem_addr + mem_len;
  1612. int disabled_one = 0;
  1613. /* First fast tracepoint jumps, then breakpoint traps on top. */
  1614. for (; jp != NULL; jp = jp->next)
  1615. {
  1616. CORE_ADDR jp_end = jp->pc + jp->length;
  1617. CORE_ADDR start, end;
  1618. int copy_offset, copy_len, buf_offset;
  1619. gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len
  1620. || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
  1621. gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len
  1622. || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length);
  1623. if (mem_addr >= jp_end)
  1624. continue;
  1625. if (jp->pc >= mem_end)
  1626. continue;
  1627. start = jp->pc;
  1628. if (mem_addr > start)
  1629. start = mem_addr;
  1630. end = jp_end;
  1631. if (end > mem_end)
  1632. end = mem_end;
  1633. copy_len = end - start;
  1634. copy_offset = start - jp->pc;
  1635. buf_offset = start - mem_addr;
  1636. memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset,
  1637. myaddr + buf_offset, copy_len);
  1638. if (jp->inserted)
  1639. memcpy (buf + buf_offset,
  1640. fast_tracepoint_jump_insn (jp) + copy_offset, copy_len);
  1641. }
  1642. for (; bp != NULL; bp = bp->next)
  1643. {
  1644. CORE_ADDR bp_end = bp->pc + bp_size (bp);
  1645. CORE_ADDR start, end;
  1646. int copy_offset, copy_len, buf_offset;
  1647. if (bp->raw_type != raw_bkpt_type_sw)
  1648. continue;
  1649. gdb_assert (bp->old_data >= myaddr + mem_len
  1650. || myaddr >= &bp->old_data[sizeof (bp->old_data)]);
  1651. if (mem_addr >= bp_end)
  1652. continue;
  1653. if (bp->pc >= mem_end)
  1654. continue;
  1655. start = bp->pc;
  1656. if (mem_addr > start)
  1657. start = mem_addr;
  1658. end = bp_end;
  1659. if (end > mem_end)
  1660. end = mem_end;
  1661. copy_len = end - start;
  1662. copy_offset = start - bp->pc;
  1663. buf_offset = start - mem_addr;
  1664. memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len);
  1665. if (bp->inserted > 0)
  1666. {
  1667. if (validate_inserted_breakpoint (bp))
  1668. memcpy (buf + buf_offset, bp_opcode (bp) + copy_offset, copy_len);
  1669. else
  1670. disabled_one = 1;
  1671. }
  1672. }
  1673. if (disabled_one)
  1674. delete_disabled_breakpoints ();
  1675. }
  1676. /* Delete all breakpoints, and un-insert them from the inferior. */
  1677. void
  1678. delete_all_breakpoints (void)
  1679. {
  1680. struct process_info *proc = current_process ();
  1681. while (proc->breakpoints)
  1682. delete_breakpoint_1 (proc, proc->breakpoints);
  1683. }
  1684. /* Clear the "inserted" flag in all breakpoints. */
  1685. void
  1686. mark_breakpoints_out (struct process_info *proc)
  1687. {
  1688. struct raw_breakpoint *raw_bp;
  1689. for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next)
  1690. raw_bp->inserted = 0;
  1691. }
  1692. /* Release all breakpoints, but do not try to un-insert them from the
  1693. inferior. */
  1694. void
  1695. free_all_breakpoints (struct process_info *proc)
  1696. {
  1697. mark_breakpoints_out (proc);
  1698. /* Note: use PROC explicitly instead of deferring to
  1699. delete_all_breakpoints --- CURRENT_INFERIOR may already have been
  1700. released when we get here. There should be no call to
  1701. current_process from here on. */
  1702. while (proc->breakpoints)
  1703. delete_breakpoint_1 (proc, proc->breakpoints);
  1704. }
  1705. /* Clone an agent expression. */
  1706. static struct agent_expr *
  1707. clone_agent_expr (const struct agent_expr *src_ax)
  1708. {
  1709. struct agent_expr *ax;
  1710. ax = XCNEW (struct agent_expr);
  1711. ax->length = src_ax->length;
  1712. ax->bytes = (unsigned char *) xcalloc (ax->length, 1);
  1713. memcpy (ax->bytes, src_ax->bytes, ax->length);
  1714. return ax;
  1715. }
  1716. /* Deep-copy the contents of one breakpoint to another. */
  1717. static struct breakpoint *
  1718. clone_one_breakpoint (const struct breakpoint *src, ptid_t ptid)
  1719. {
  1720. struct breakpoint *dest;
  1721. struct raw_breakpoint *dest_raw;
  1722. /* Clone the raw breakpoint. */
  1723. dest_raw = XCNEW (struct raw_breakpoint);
  1724. dest_raw->raw_type = src->raw->raw_type;
  1725. dest_raw->refcount = src->raw->refcount;
  1726. dest_raw->pc = src->raw->pc;
  1727. dest_raw->kind = src->raw->kind;
  1728. memcpy (dest_raw->old_data, src->raw->old_data, MAX_BREAKPOINT_LEN);
  1729. dest_raw->inserted = src->raw->inserted;
  1730. /* Clone the high-level breakpoint. */
  1731. if (is_gdb_breakpoint (src->type))
  1732. {
  1733. struct gdb_breakpoint *gdb_dest = XCNEW (struct gdb_breakpoint);
  1734. struct point_cond_list *current_cond;
  1735. struct point_cond_list *new_cond;
  1736. struct point_cond_list *cond_tail = NULL;
  1737. struct point_command_list *current_cmd;
  1738. struct point_command_list *new_cmd;
  1739. struct point_command_list *cmd_tail = NULL;
  1740. /* Clone the condition list. */
  1741. for (current_cond = ((struct gdb_breakpoint *) src)->cond_list;
  1742. current_cond != NULL;
  1743. current_cond = current_cond->next)
  1744. {
  1745. new_cond = XCNEW (struct point_cond_list);
  1746. new_cond->cond = clone_agent_expr (current_cond->cond);
  1747. APPEND_TO_LIST (&gdb_dest->cond_list, new_cond, cond_tail);
  1748. }
  1749. /* Clone the command list. */
  1750. for (current_cmd = ((struct gdb_breakpoint *) src)->command_list;
  1751. current_cmd != NULL;
  1752. current_cmd = current_cmd->next)
  1753. {
  1754. new_cmd = XCNEW (struct point_command_list);
  1755. new_cmd->cmd = clone_agent_expr (current_cmd->cmd);
  1756. new_cmd->persistence = current_cmd->persistence;
  1757. APPEND_TO_LIST (&gdb_dest->command_list, new_cmd, cmd_tail);
  1758. }
  1759. dest = (struct breakpoint *) gdb_dest;
  1760. }
  1761. else if (src->type == other_breakpoint)
  1762. {
  1763. struct other_breakpoint *other_dest = XCNEW (struct other_breakpoint);
  1764. other_dest->handler = ((struct other_breakpoint *) src)->handler;
  1765. dest = (struct breakpoint *) other_dest;
  1766. }
  1767. else if (src->type == single_step_breakpoint)
  1768. {
  1769. struct single_step_breakpoint *ss_dest
  1770. = XCNEW (struct single_step_breakpoint);
  1771. dest = (struct breakpoint *) ss_dest;
  1772. /* Since single-step breakpoint is thread specific, don't copy
  1773. thread id from SRC, use ID instead. */
  1774. ss_dest->ptid = ptid;
  1775. }
  1776. else
  1777. gdb_assert_not_reached ("unhandled breakpoint type");
  1778. dest->type = src->type;
  1779. dest->raw = dest_raw;
  1780. return dest;
  1781. }
  1782. /* See mem-break.h. */
  1783. void
  1784. clone_all_breakpoints (struct thread_info *child_thread,
  1785. const struct thread_info *parent_thread)
  1786. {
  1787. const struct breakpoint *bp;
  1788. struct breakpoint *new_bkpt;
  1789. struct breakpoint *bkpt_tail = NULL;
  1790. struct raw_breakpoint *raw_bkpt_tail = NULL;
  1791. struct process_info *child_proc = get_thread_process (child_thread);
  1792. struct process_info *parent_proc = get_thread_process (parent_thread);
  1793. struct breakpoint **new_list = &child_proc->breakpoints;
  1794. struct raw_breakpoint **new_raw_list = &child_proc->raw_breakpoints;
  1795. for (bp = parent_proc->breakpoints; bp != NULL; bp = bp->next)
  1796. {
  1797. new_bkpt = clone_one_breakpoint (bp, ptid_of (child_thread));
  1798. APPEND_TO_LIST (new_list, new_bkpt, bkpt_tail);
  1799. APPEND_TO_LIST (new_raw_list, new_bkpt->raw, raw_bkpt_tail);
  1800. }
  1801. }