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