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