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