Commit | Line | Data |
---|---|---|
611cb4a5 | 1 | /* Memory breakpoint operations for the remote server for GDB. |
ecd75fc8 | 2 | Copyright (C) 2002-2014 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" | |
7f216e7c | 24 | #include <stdint.h> |
611cb4a5 | 25 | |
f450004a | 26 | const unsigned char *breakpoint_data; |
611cb4a5 DJ |
27 | int breakpoint_len; |
28 | ||
29 | #define MAX_BREAKPOINT_LEN 8 | |
30 | ||
8b07ae33 PA |
31 | /* GDB will never try to install multiple breakpoints at the same |
32 | address. But, we need to keep track of internal breakpoints too, | |
33 | and so we do need to be able to install multiple breakpoints at the | |
34 | same address transparently. We keep track of two different, and | |
35 | closely related structures. A raw breakpoint, which manages the | |
36 | low level, close to the metal aspect of a breakpoint. It holds the | |
37 | breakpoint address, and a buffer holding a copy of the instructions | |
38 | that would be in memory had not been a breakpoint there (we call | |
39 | that the shadow memory of the breakpoint). We occasionally need to | |
40 | temporarilly uninsert a breakpoint without the client knowing about | |
41 | it (e.g., to step over an internal breakpoint), so we keep an | |
42 | `inserted' state associated with this low level breakpoint | |
43 | structure. There can only be one such object for a given address. | |
44 | Then, we have (a bit higher level) breakpoints. This structure | |
45 | holds a callback to be called whenever a breakpoint is hit, a | |
46 | high-level type, and a link to a low level raw breakpoint. There | |
47 | can be many high-level breakpoints at the same address, and all of | |
48 | them will point to the same raw breakpoint, which is reference | |
49 | counted. */ | |
50 | ||
51 | /* The low level, physical, raw breakpoint. */ | |
52 | struct raw_breakpoint | |
53 | { | |
54 | struct raw_breakpoint *next; | |
55 | ||
56 | /* A reference count. Each high level breakpoint referencing this | |
57 | raw breakpoint accounts for one reference. */ | |
58 | int refcount; | |
59 | ||
60 | /* The breakpoint's insertion address. There can only be one raw | |
61 | breakpoint for a given PC. */ | |
62 | CORE_ADDR pc; | |
63 | ||
64 | /* The breakpoint's shadow memory. */ | |
65 | unsigned char old_data[MAX_BREAKPOINT_LEN]; | |
66 | ||
67 | /* Non-zero if this breakpoint is currently inserted in the | |
68 | inferior. */ | |
69 | int inserted; | |
d3bbe7a0 PA |
70 | |
71 | /* Non-zero if this breakpoint is currently disabled because we no | |
72 | longer detect it as inserted. */ | |
73 | int shlib_disabled; | |
8b07ae33 PA |
74 | }; |
75 | ||
414a389f PA |
76 | /* The type of a breakpoint. */ |
77 | enum bkpt_type | |
78 | { | |
8b07ae33 PA |
79 | /* A GDB breakpoint, requested with a Z0 packet. */ |
80 | gdb_breakpoint, | |
81 | ||
414a389f PA |
82 | /* A basic-software-single-step breakpoint. */ |
83 | reinsert_breakpoint, | |
84 | ||
85 | /* Any other breakpoint type that doesn't require specific | |
86 | treatment goes here. E.g., an event breakpoint. */ | |
87 | other_breakpoint, | |
88 | }; | |
89 | ||
9f3a5c85 LM |
90 | struct point_cond_list |
91 | { | |
92 | /* Pointer to the agent expression that is the breakpoint's | |
93 | conditional. */ | |
94 | struct agent_expr *cond; | |
95 | ||
96 | /* Pointer to the next condition. */ | |
97 | struct point_cond_list *next; | |
98 | }; | |
99 | ||
d3ce09f5 SS |
100 | struct point_command_list |
101 | { | |
102 | /* Pointer to the agent expression that is the breakpoint's | |
103 | commands. */ | |
104 | struct agent_expr *cmd; | |
105 | ||
106 | /* Flag that is true if this command should run even while GDB is | |
107 | disconnected. */ | |
108 | int persistence; | |
109 | ||
110 | /* Pointer to the next command. */ | |
111 | struct point_command_list *next; | |
112 | }; | |
113 | ||
8b07ae33 | 114 | /* A high level (in gdbserver's perspective) breakpoint. */ |
611cb4a5 DJ |
115 | struct breakpoint |
116 | { | |
117 | struct breakpoint *next; | |
611cb4a5 | 118 | |
414a389f PA |
119 | /* The breakpoint's type. */ |
120 | enum bkpt_type type; | |
121 | ||
9f3a5c85 LM |
122 | /* Pointer to the condition list that should be evaluated on |
123 | the target or NULL if the breakpoint is unconditional or | |
124 | if GDB doesn't want us to evaluate the conditionals on the | |
125 | target's side. */ | |
126 | struct point_cond_list *cond_list; | |
127 | ||
d3ce09f5 SS |
128 | /* Point to the list of commands to run when this is hit. */ |
129 | struct point_command_list *command_list; | |
130 | ||
8b07ae33 PA |
131 | /* Link to this breakpoint's raw breakpoint. This is always |
132 | non-NULL. */ | |
133 | struct raw_breakpoint *raw; | |
134 | ||
b65d95c5 | 135 | /* Function to call when we hit this breakpoint. If it returns 1, |
8b07ae33 PA |
136 | the breakpoint shall be deleted; 0 or if this callback is NULL, |
137 | it will be left inserted. */ | |
b65d95c5 | 138 | int (*handler) (CORE_ADDR); |
611cb4a5 DJ |
139 | }; |
140 | ||
d3ce09f5 SS |
141 | int |
142 | any_persistent_commands () | |
143 | { | |
144 | struct process_info *proc = current_process (); | |
145 | struct breakpoint *bp; | |
146 | struct point_command_list *cl; | |
147 | ||
148 | for (bp = proc->breakpoints; bp != NULL; bp = bp->next) | |
149 | { | |
150 | for (cl = bp->command_list; cl != NULL; cl = cl->next) | |
151 | if (cl->persistence) | |
152 | return 1; | |
153 | } | |
154 | ||
155 | return 0; | |
156 | } | |
157 | ||
8b07ae33 PA |
158 | static struct raw_breakpoint * |
159 | find_raw_breakpoint_at (CORE_ADDR where) | |
160 | { | |
161 | struct process_info *proc = current_process (); | |
162 | struct raw_breakpoint *bp; | |
414a389f | 163 | |
8b07ae33 PA |
164 | for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next) |
165 | if (bp->pc == where) | |
166 | return bp; | |
167 | ||
168 | return NULL; | |
169 | } | |
170 | ||
171 | static struct raw_breakpoint * | |
d50171e4 | 172 | set_raw_breakpoint_at (CORE_ADDR where) |
611cb4a5 | 173 | { |
95954743 | 174 | struct process_info *proc = current_process (); |
8b07ae33 | 175 | struct raw_breakpoint *bp; |
d50171e4 | 176 | int err; |
6bf36717 | 177 | unsigned char buf[MAX_BREAKPOINT_LEN]; |
611cb4a5 DJ |
178 | |
179 | if (breakpoint_data == NULL) | |
180 | error ("Target does not support breakpoints."); | |
181 | ||
8b07ae33 PA |
182 | bp = find_raw_breakpoint_at (where); |
183 | if (bp != NULL) | |
184 | { | |
185 | bp->refcount++; | |
186 | return bp; | |
187 | } | |
188 | ||
d50171e4 PA |
189 | bp = xcalloc (1, sizeof (*bp)); |
190 | bp->pc = where; | |
8b07ae33 | 191 | bp->refcount = 1; |
611cb4a5 | 192 | |
fa593d66 PA |
193 | /* Note that there can be fast tracepoint jumps installed in the |
194 | same memory range, so to get at the original memory, we need to | |
195 | use read_inferior_memory, which masks those out. */ | |
6bf36717 | 196 | err = read_inferior_memory (where, buf, breakpoint_len); |
d50171e4 PA |
197 | if (err != 0) |
198 | { | |
199 | if (debug_threads) | |
200 | fprintf (stderr, | |
201 | "Failed to read shadow memory of" | |
202 | " breakpoint at 0x%s (%s).\n", | |
203 | paddress (where), strerror (err)); | |
204 | free (bp); | |
205 | return NULL; | |
206 | } | |
6bf36717 | 207 | memcpy (bp->old_data, buf, breakpoint_len); |
611cb4a5 | 208 | |
d50171e4 PA |
209 | err = (*the_target->write_memory) (where, breakpoint_data, |
210 | breakpoint_len); | |
211 | if (err != 0) | |
212 | { | |
213 | if (debug_threads) | |
214 | fprintf (stderr, | |
215 | "Failed to insert breakpoint at 0x%s (%s).\n", | |
216 | paddress (where), strerror (err)); | |
217 | free (bp); | |
218 | return NULL; | |
219 | } | |
220 | ||
221 | /* Link the breakpoint in. */ | |
222 | bp->inserted = 1; | |
8b07ae33 PA |
223 | bp->next = proc->raw_breakpoints; |
224 | proc->raw_breakpoints = bp; | |
d50171e4 PA |
225 | return bp; |
226 | } | |
227 | ||
fa593d66 PA |
228 | /* Notice that breakpoint traps are always installed on top of fast |
229 | tracepoint jumps. This is even if the fast tracepoint is installed | |
230 | at a later time compared to when the breakpoint was installed. | |
231 | This means that a stopping breakpoint or tracepoint has higher | |
232 | "priority". In turn, this allows having fast and slow tracepoints | |
233 | (and breakpoints) at the same address behave correctly. */ | |
234 | ||
235 | ||
236 | /* A fast tracepoint jump. */ | |
237 | ||
238 | struct fast_tracepoint_jump | |
239 | { | |
240 | struct fast_tracepoint_jump *next; | |
241 | ||
242 | /* A reference count. GDB can install more than one fast tracepoint | |
243 | at the same address (each with its own action list, for | |
244 | example). */ | |
245 | int refcount; | |
246 | ||
247 | /* The fast tracepoint's insertion address. There can only be one | |
248 | of these for a given PC. */ | |
249 | CORE_ADDR pc; | |
250 | ||
251 | /* Non-zero if this fast tracepoint jump is currently inserted in | |
252 | the inferior. */ | |
253 | int inserted; | |
254 | ||
255 | /* The length of the jump instruction. */ | |
256 | int length; | |
257 | ||
258 | /* A poor-man's flexible array member, holding both the jump | |
259 | instruction to insert, and a copy of the instruction that would | |
260 | be in memory had not been a jump there (the shadow memory of the | |
261 | tracepoint jump). */ | |
262 | unsigned char insn_and_shadow[0]; | |
263 | }; | |
264 | ||
265 | /* Fast tracepoint FP's jump instruction to insert. */ | |
266 | #define fast_tracepoint_jump_insn(fp) \ | |
267 | ((fp)->insn_and_shadow + 0) | |
268 | ||
269 | /* The shadow memory of fast tracepoint jump FP. */ | |
270 | #define fast_tracepoint_jump_shadow(fp) \ | |
271 | ((fp)->insn_and_shadow + (fp)->length) | |
272 | ||
273 | ||
274 | /* Return the fast tracepoint jump set at WHERE. */ | |
275 | ||
276 | static struct fast_tracepoint_jump * | |
277 | find_fast_tracepoint_jump_at (CORE_ADDR where) | |
278 | { | |
279 | struct process_info *proc = current_process (); | |
280 | struct fast_tracepoint_jump *jp; | |
281 | ||
282 | for (jp = proc->fast_tracepoint_jumps; jp != NULL; jp = jp->next) | |
283 | if (jp->pc == where) | |
284 | return jp; | |
285 | ||
286 | return NULL; | |
287 | } | |
288 | ||
289 | int | |
290 | fast_tracepoint_jump_here (CORE_ADDR where) | |
291 | { | |
292 | struct fast_tracepoint_jump *jp = find_fast_tracepoint_jump_at (where); | |
293 | ||
294 | return (jp != NULL); | |
295 | } | |
296 | ||
297 | int | |
298 | delete_fast_tracepoint_jump (struct fast_tracepoint_jump *todel) | |
299 | { | |
300 | struct fast_tracepoint_jump *bp, **bp_link; | |
301 | int ret; | |
302 | struct process_info *proc = current_process (); | |
303 | ||
304 | bp = proc->fast_tracepoint_jumps; | |
305 | bp_link = &proc->fast_tracepoint_jumps; | |
306 | ||
307 | while (bp) | |
308 | { | |
309 | if (bp == todel) | |
310 | { | |
311 | if (--bp->refcount == 0) | |
312 | { | |
313 | struct fast_tracepoint_jump *prev_bp_link = *bp_link; | |
6bf36717 | 314 | unsigned char *buf; |
fa593d66 PA |
315 | |
316 | /* Unlink it. */ | |
317 | *bp_link = bp->next; | |
318 | ||
319 | /* Since there can be breakpoints inserted in the same | |
320 | address range, we use `write_inferior_memory', which | |
321 | takes care of layering breakpoints on top of fast | |
322 | tracepoints, and on top of the buffer we pass it. | |
323 | This works because we've already unlinked the fast | |
324 | tracepoint jump above. Also note that we need to | |
325 | pass the current shadow contents, because | |
326 | write_inferior_memory updates any shadow memory with | |
327 | what we pass here, and we want that to be a nop. */ | |
6bf36717 JK |
328 | buf = alloca (bp->length); |
329 | memcpy (buf, fast_tracepoint_jump_shadow (bp), bp->length); | |
330 | ret = write_inferior_memory (bp->pc, buf, bp->length); | |
fa593d66 PA |
331 | if (ret != 0) |
332 | { | |
333 | /* Something went wrong, relink the jump. */ | |
334 | *bp_link = prev_bp_link; | |
335 | ||
336 | if (debug_threads) | |
337 | fprintf (stderr, | |
338 | "Failed to uninsert fast tracepoint jump " | |
339 | "at 0x%s (%s) while deleting it.\n", | |
340 | paddress (bp->pc), strerror (ret)); | |
341 | return ret; | |
342 | } | |
343 | ||
344 | free (bp); | |
345 | } | |
346 | ||
347 | return 0; | |
348 | } | |
349 | else | |
350 | { | |
351 | bp_link = &bp->next; | |
352 | bp = *bp_link; | |
353 | } | |
354 | } | |
355 | ||
356 | warning ("Could not find fast tracepoint jump in list."); | |
357 | return ENOENT; | |
358 | } | |
359 | ||
5c73ff4e YQ |
360 | void |
361 | inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump *jp) | |
362 | { | |
363 | jp->refcount++; | |
364 | } | |
365 | ||
fa593d66 PA |
366 | struct fast_tracepoint_jump * |
367 | set_fast_tracepoint_jump (CORE_ADDR where, | |
368 | unsigned char *insn, ULONGEST length) | |
369 | { | |
370 | struct process_info *proc = current_process (); | |
371 | struct fast_tracepoint_jump *jp; | |
372 | int err; | |
6bf36717 | 373 | unsigned char *buf; |
fa593d66 PA |
374 | |
375 | /* We refcount fast tracepoint jumps. Check if we already know | |
376 | about a jump at this address. */ | |
377 | jp = find_fast_tracepoint_jump_at (where); | |
378 | if (jp != NULL) | |
379 | { | |
380 | jp->refcount++; | |
381 | return jp; | |
382 | } | |
383 | ||
384 | /* We don't, so create a new object. Double the length, because the | |
385 | flexible array member holds both the jump insn, and the | |
386 | shadow. */ | |
387 | jp = xcalloc (1, sizeof (*jp) + (length * 2)); | |
388 | jp->pc = where; | |
389 | jp->length = length; | |
390 | memcpy (fast_tracepoint_jump_insn (jp), insn, length); | |
391 | jp->refcount = 1; | |
6bf36717 | 392 | buf = alloca (length); |
fa593d66 PA |
393 | |
394 | /* Note that there can be trap breakpoints inserted in the same | |
395 | address range. To access the original memory contents, we use | |
396 | `read_inferior_memory', which masks out breakpoints. */ | |
6bf36717 | 397 | err = read_inferior_memory (where, buf, length); |
fa593d66 PA |
398 | if (err != 0) |
399 | { | |
400 | if (debug_threads) | |
401 | fprintf (stderr, | |
402 | "Failed to read shadow memory of" | |
403 | " fast tracepoint at 0x%s (%s).\n", | |
404 | paddress (where), strerror (err)); | |
405 | free (jp); | |
406 | return NULL; | |
407 | } | |
6bf36717 | 408 | memcpy (fast_tracepoint_jump_shadow (jp), buf, length); |
fa593d66 PA |
409 | |
410 | /* Link the jump in. */ | |
411 | jp->inserted = 1; | |
412 | jp->next = proc->fast_tracepoint_jumps; | |
413 | proc->fast_tracepoint_jumps = jp; | |
414 | ||
415 | /* Since there can be trap breakpoints inserted in the same address | |
416 | range, we use use `write_inferior_memory', which takes care of | |
417 | layering breakpoints on top of fast tracepoints, on top of the | |
418 | buffer we pass it. This works because we've already linked in | |
419 | the fast tracepoint jump above. Also note that we need to pass | |
420 | the current shadow contents, because write_inferior_memory | |
421 | updates any shadow memory with what we pass here, and we want | |
422 | that to be a nop. */ | |
6bf36717 | 423 | err = write_inferior_memory (where, buf, length); |
fa593d66 PA |
424 | if (err != 0) |
425 | { | |
426 | if (debug_threads) | |
427 | fprintf (stderr, | |
428 | "Failed to insert fast tracepoint jump at 0x%s (%s).\n", | |
429 | paddress (where), strerror (err)); | |
430 | ||
431 | /* Unlink it. */ | |
432 | proc->fast_tracepoint_jumps = jp->next; | |
433 | free (jp); | |
434 | ||
435 | return NULL; | |
436 | } | |
437 | ||
438 | return jp; | |
439 | } | |
440 | ||
441 | void | |
442 | uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc) | |
443 | { | |
444 | struct fast_tracepoint_jump *jp; | |
445 | int err; | |
446 | ||
447 | jp = find_fast_tracepoint_jump_at (pc); | |
448 | if (jp == NULL) | |
449 | { | |
450 | /* This can happen when we remove all breakpoints while handling | |
451 | a step-over. */ | |
452 | if (debug_threads) | |
453 | fprintf (stderr, | |
454 | "Could not find fast tracepoint jump at 0x%s " | |
455 | "in list (uninserting).\n", | |
456 | paddress (pc)); | |
457 | return; | |
458 | } | |
459 | ||
460 | if (jp->inserted) | |
461 | { | |
6bf36717 JK |
462 | unsigned char *buf; |
463 | ||
fa593d66 PA |
464 | jp->inserted = 0; |
465 | ||
466 | /* Since there can be trap breakpoints inserted in the same | |
467 | address range, we use use `write_inferior_memory', which | |
468 | takes care of layering breakpoints on top of fast | |
469 | tracepoints, and on top of the buffer we pass it. This works | |
470 | because we've already marked the fast tracepoint fast | |
471 | tracepoint jump uninserted above. Also note that we need to | |
472 | pass the current shadow contents, because | |
473 | write_inferior_memory updates any shadow memory with what we | |
474 | pass here, and we want that to be a nop. */ | |
6bf36717 JK |
475 | buf = alloca (jp->length); |
476 | memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length); | |
477 | err = write_inferior_memory (jp->pc, buf, jp->length); | |
fa593d66 PA |
478 | if (err != 0) |
479 | { | |
480 | jp->inserted = 1; | |
481 | ||
482 | if (debug_threads) | |
483 | fprintf (stderr, | |
484 | "Failed to uninsert fast tracepoint jump at 0x%s (%s).\n", | |
485 | paddress (pc), strerror (err)); | |
486 | } | |
487 | } | |
488 | } | |
489 | ||
490 | void | |
491 | reinsert_fast_tracepoint_jumps_at (CORE_ADDR where) | |
492 | { | |
493 | struct fast_tracepoint_jump *jp; | |
494 | int err; | |
6bf36717 | 495 | unsigned char *buf; |
fa593d66 PA |
496 | |
497 | jp = find_fast_tracepoint_jump_at (where); | |
498 | if (jp == NULL) | |
499 | { | |
500 | /* This can happen when we remove breakpoints when a tracepoint | |
501 | hit causes a tracing stop, while handling a step-over. */ | |
502 | if (debug_threads) | |
503 | fprintf (stderr, | |
504 | "Could not find fast tracepoint jump at 0x%s " | |
505 | "in list (reinserting).\n", | |
506 | paddress (where)); | |
507 | return; | |
508 | } | |
509 | ||
510 | if (jp->inserted) | |
511 | error ("Jump already inserted at reinsert time."); | |
512 | ||
513 | jp->inserted = 1; | |
514 | ||
515 | /* Since there can be trap breakpoints inserted in the same address | |
516 | range, we use `write_inferior_memory', which takes care of | |
517 | layering breakpoints on top of fast tracepoints, and on top of | |
518 | the buffer we pass it. This works because we've already marked | |
519 | the fast tracepoint jump inserted above. Also note that we need | |
520 | to pass the current shadow contents, because | |
521 | write_inferior_memory updates any shadow memory with what we pass | |
522 | here, and we want that to be a nop. */ | |
6bf36717 JK |
523 | buf = alloca (jp->length); |
524 | memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length); | |
525 | err = write_inferior_memory (where, buf, jp->length); | |
fa593d66 PA |
526 | if (err != 0) |
527 | { | |
528 | jp->inserted = 0; | |
529 | ||
530 | if (debug_threads) | |
531 | fprintf (stderr, | |
532 | "Failed to reinsert fast tracepoint jump at 0x%s (%s).\n", | |
533 | paddress (where), strerror (err)); | |
534 | } | |
535 | } | |
536 | ||
414a389f | 537 | struct breakpoint * |
d50171e4 PA |
538 | set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR)) |
539 | { | |
540 | struct process_info *proc = current_process (); | |
541 | struct breakpoint *bp; | |
8b07ae33 | 542 | struct raw_breakpoint *raw; |
d50171e4 | 543 | |
8b07ae33 | 544 | raw = set_raw_breakpoint_at (where); |
d50171e4 | 545 | |
8b07ae33 | 546 | if (raw == NULL) |
d50171e4 PA |
547 | { |
548 | /* warn? */ | |
414a389f | 549 | return NULL; |
d50171e4 PA |
550 | } |
551 | ||
552 | bp = xcalloc (1, sizeof (struct breakpoint)); | |
414a389f | 553 | bp->type = other_breakpoint; |
8b07ae33 PA |
554 | |
555 | bp->raw = raw; | |
611cb4a5 DJ |
556 | bp->handler = handler; |
557 | ||
95954743 PA |
558 | bp->next = proc->breakpoints; |
559 | proc->breakpoints = bp; | |
414a389f PA |
560 | |
561 | return bp; | |
611cb4a5 DJ |
562 | } |
563 | ||
8b07ae33 PA |
564 | static int |
565 | delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel) | |
566 | { | |
567 | struct raw_breakpoint *bp, **bp_link; | |
568 | int ret; | |
569 | ||
570 | bp = proc->raw_breakpoints; | |
571 | bp_link = &proc->raw_breakpoints; | |
572 | ||
573 | while (bp) | |
574 | { | |
575 | if (bp == todel) | |
576 | { | |
577 | if (bp->inserted) | |
578 | { | |
579 | struct raw_breakpoint *prev_bp_link = *bp_link; | |
6bf36717 | 580 | unsigned char buf[MAX_BREAKPOINT_LEN]; |
8b07ae33 PA |
581 | |
582 | *bp_link = bp->next; | |
583 | ||
fa593d66 PA |
584 | /* Since there can be trap breakpoints inserted in the |
585 | same address range, we use `write_inferior_memory', | |
586 | which takes care of layering breakpoints on top of | |
587 | fast tracepoints, and on top of the buffer we pass | |
588 | it. This works because we've already unlinked the | |
589 | fast tracepoint jump above. Also note that we need | |
590 | to pass the current shadow contents, because | |
591 | write_inferior_memory updates any shadow memory with | |
592 | what we pass here, and we want that to be a nop. */ | |
6bf36717 JK |
593 | memcpy (buf, bp->old_data, breakpoint_len); |
594 | ret = write_inferior_memory (bp->pc, buf, breakpoint_len); | |
8b07ae33 PA |
595 | if (ret != 0) |
596 | { | |
597 | /* Something went wrong, relink the breakpoint. */ | |
598 | *bp_link = prev_bp_link; | |
599 | ||
600 | if (debug_threads) | |
601 | fprintf (stderr, | |
602 | "Failed to uninsert raw breakpoint " | |
603 | "at 0x%s (%s) while deleting it.\n", | |
604 | paddress (bp->pc), strerror (ret)); | |
605 | return ret; | |
606 | } | |
607 | ||
608 | } | |
609 | else | |
610 | *bp_link = bp->next; | |
611 | ||
612 | free (bp); | |
613 | return 0; | |
614 | } | |
615 | else | |
616 | { | |
617 | bp_link = &bp->next; | |
618 | bp = *bp_link; | |
619 | } | |
620 | } | |
621 | ||
622 | warning ("Could not find raw breakpoint in list."); | |
623 | return ENOENT; | |
624 | } | |
625 | ||
626 | static int | |
627 | release_breakpoint (struct process_info *proc, struct breakpoint *bp) | |
628 | { | |
629 | int newrefcount; | |
630 | int ret; | |
631 | ||
632 | newrefcount = bp->raw->refcount - 1; | |
633 | if (newrefcount == 0) | |
634 | { | |
635 | ret = delete_raw_breakpoint (proc, bp->raw); | |
636 | if (ret != 0) | |
637 | return ret; | |
638 | } | |
639 | else | |
640 | bp->raw->refcount = newrefcount; | |
641 | ||
642 | free (bp); | |
643 | ||
644 | return 0; | |
645 | } | |
646 | ||
647 | static int | |
648 | delete_breakpoint_1 (struct process_info *proc, struct breakpoint *todel) | |
611cb4a5 | 649 | { |
414a389f | 650 | struct breakpoint *bp, **bp_link; |
8b07ae33 | 651 | int err; |
611cb4a5 | 652 | |
414a389f PA |
653 | bp = proc->breakpoints; |
654 | bp_link = &proc->breakpoints; | |
655 | ||
656 | while (bp) | |
611cb4a5 | 657 | { |
414a389f | 658 | if (bp == todel) |
611cb4a5 | 659 | { |
414a389f PA |
660 | *bp_link = bp->next; |
661 | ||
8b07ae33 PA |
662 | err = release_breakpoint (proc, bp); |
663 | if (err != 0) | |
664 | return err; | |
665 | ||
666 | bp = *bp_link; | |
667 | return 0; | |
611cb4a5 | 668 | } |
414a389f PA |
669 | else |
670 | { | |
671 | bp_link = &bp->next; | |
672 | bp = *bp_link; | |
673 | } | |
611cb4a5 | 674 | } |
414a389f | 675 | |
611cb4a5 | 676 | warning ("Could not find breakpoint in list."); |
8b07ae33 PA |
677 | return ENOENT; |
678 | } | |
679 | ||
219f2f23 | 680 | int |
8b07ae33 PA |
681 | delete_breakpoint (struct breakpoint *todel) |
682 | { | |
683 | struct process_info *proc = current_process (); | |
684 | return delete_breakpoint_1 (proc, todel); | |
611cb4a5 DJ |
685 | } |
686 | ||
9f3a5c85 | 687 | struct breakpoint * |
8b07ae33 | 688 | find_gdb_breakpoint_at (CORE_ADDR where) |
611cb4a5 | 689 | { |
95954743 | 690 | struct process_info *proc = current_process (); |
8b07ae33 | 691 | struct breakpoint *bp; |
611cb4a5 | 692 | |
8b07ae33 PA |
693 | for (bp = proc->breakpoints; bp != NULL; bp = bp->next) |
694 | if (bp->type == gdb_breakpoint && bp->raw->pc == where) | |
695 | return bp; | |
611cb4a5 DJ |
696 | |
697 | return NULL; | |
698 | } | |
699 | ||
8b07ae33 PA |
700 | int |
701 | set_gdb_breakpoint_at (CORE_ADDR where) | |
68070c10 | 702 | { |
8b07ae33 PA |
703 | struct breakpoint *bp; |
704 | ||
705 | if (breakpoint_data == NULL) | |
706 | return 1; | |
707 | ||
d3bbe7a0 PA |
708 | /* If we see GDB inserting a second breakpoint at the same address, |
709 | then the first breakpoint must have disappeared due to a shared | |
710 | library unload. On targets where the shared libraries are | |
711 | handled by userspace, like SVR4, for example, GDBserver can't | |
712 | tell if a library was loaded or unloaded. Since we refcount | |
713 | breakpoints, if we didn't do this, we'd just increase the | |
714 | refcount of the previous breakpoint at this address, but the trap | |
715 | was not planted in the inferior anymore, thus the breakpoint | |
716 | would never be hit. */ | |
717 | bp = find_gdb_breakpoint_at (where); | |
718 | if (bp != NULL) | |
719 | { | |
720 | delete_gdb_breakpoint_at (where); | |
721 | ||
722 | /* Might as well validate all other breakpoints. */ | |
723 | validate_breakpoints (); | |
724 | } | |
725 | ||
8b07ae33 PA |
726 | bp = set_breakpoint_at (where, NULL); |
727 | if (bp == NULL) | |
728 | return -1; | |
729 | ||
730 | bp->type = gdb_breakpoint; | |
731 | return 0; | |
732 | } | |
733 | ||
734 | int | |
735 | delete_gdb_breakpoint_at (CORE_ADDR addr) | |
736 | { | |
737 | struct breakpoint *bp; | |
738 | int err; | |
739 | ||
740 | if (breakpoint_data == NULL) | |
741 | return 1; | |
742 | ||
743 | bp = find_gdb_breakpoint_at (addr); | |
744 | if (bp == NULL) | |
745 | return -1; | |
746 | ||
9f3a5c85 LM |
747 | /* Before deleting the breakpoint, make sure to free |
748 | its condition list. */ | |
749 | clear_gdb_breakpoint_conditions (addr); | |
8b07ae33 PA |
750 | err = delete_breakpoint (bp); |
751 | if (err) | |
752 | return -1; | |
753 | ||
754 | return 0; | |
755 | } | |
756 | ||
9f3a5c85 LM |
757 | /* Clear all conditions associated with this breakpoint address. */ |
758 | ||
759 | void | |
760 | clear_gdb_breakpoint_conditions (CORE_ADDR addr) | |
761 | { | |
762 | struct breakpoint *bp = find_gdb_breakpoint_at (addr); | |
412c89dd | 763 | struct point_cond_list *cond; |
9f3a5c85 LM |
764 | |
765 | if (bp == NULL || bp->cond_list == NULL) | |
766 | return; | |
767 | ||
768 | cond = bp->cond_list; | |
9f3a5c85 LM |
769 | |
770 | while (cond != NULL) | |
771 | { | |
412c89dd LM |
772 | struct point_cond_list *cond_next; |
773 | ||
774 | cond_next = cond->next; | |
775 | free (cond->cond->bytes); | |
9f3a5c85 LM |
776 | free (cond->cond); |
777 | free (cond); | |
412c89dd | 778 | cond = cond_next; |
9f3a5c85 LM |
779 | } |
780 | ||
781 | bp->cond_list = NULL; | |
782 | } | |
783 | ||
784 | /* Add condition CONDITION to GDBserver's breakpoint BP. */ | |
785 | ||
786 | void | |
787 | add_condition_to_breakpoint (struct breakpoint *bp, | |
788 | struct agent_expr *condition) | |
789 | { | |
790 | struct point_cond_list *new_cond; | |
791 | ||
792 | /* Create new condition. */ | |
793 | new_cond = xcalloc (1, sizeof (*new_cond)); | |
794 | new_cond->cond = condition; | |
795 | ||
796 | /* Add condition to the list. */ | |
797 | new_cond->next = bp->cond_list; | |
798 | bp->cond_list = new_cond; | |
799 | } | |
800 | ||
801 | /* Add a target-side condition CONDITION to the breakpoint at ADDR. */ | |
802 | ||
8b07ae33 | 803 | int |
9f3a5c85 LM |
804 | add_breakpoint_condition (CORE_ADDR addr, char **condition) |
805 | { | |
806 | struct breakpoint *bp = find_gdb_breakpoint_at (addr); | |
807 | char *actparm = *condition; | |
808 | struct agent_expr *cond; | |
809 | ||
810 | if (bp == NULL) | |
811 | return 1; | |
812 | ||
813 | if (condition == NULL) | |
814 | return 1; | |
815 | ||
816 | cond = gdb_parse_agent_expr (&actparm); | |
817 | ||
818 | if (cond == NULL) | |
819 | { | |
820 | fprintf (stderr, "Condition evaluation failed. " | |
821 | "Assuming unconditional.\n"); | |
822 | return 0; | |
823 | } | |
824 | ||
825 | add_condition_to_breakpoint (bp, cond); | |
826 | ||
827 | *condition = actparm; | |
828 | ||
829 | return 0; | |
830 | } | |
831 | ||
832 | /* Evaluate condition (if any) at breakpoint BP. Return 1 if | |
833 | true and 0 otherwise. */ | |
834 | ||
835 | int | |
836 | gdb_condition_true_at_breakpoint (CORE_ADDR where) | |
8b07ae33 | 837 | { |
9f3a5c85 | 838 | /* Fetch registers for the current inferior. */ |
8b07ae33 | 839 | struct breakpoint *bp = find_gdb_breakpoint_at (where); |
9f3a5c85 LM |
840 | ULONGEST value = 0; |
841 | struct point_cond_list *cl; | |
842 | int err = 0; | |
5ae4861a | 843 | struct eval_agent_expr_context ctx; |
9f3a5c85 LM |
844 | |
845 | if (bp == NULL) | |
846 | return 0; | |
8b07ae33 | 847 | |
9f3a5c85 LM |
848 | /* Check if the breakpoint is unconditional. If it is, |
849 | the condition always evaluates to TRUE. */ | |
850 | if (bp->cond_list == NULL) | |
851 | return 1; | |
852 | ||
5ae4861a YQ |
853 | ctx.regcache = get_thread_regcache (current_inferior, 1); |
854 | ctx.tframe = NULL; | |
855 | ctx.tpoint = NULL; | |
856 | ||
9f3a5c85 LM |
857 | /* Evaluate each condition in the breakpoint's list of conditions. |
858 | Return true if any of the conditions evaluates to TRUE. | |
859 | ||
860 | If we failed to evaluate the expression, TRUE is returned. This | |
861 | forces GDB to reevaluate the conditions. */ | |
862 | for (cl = bp->cond_list; | |
863 | cl && !value && !err; cl = cl->next) | |
864 | { | |
865 | /* Evaluate the condition. */ | |
5ae4861a | 866 | err = gdb_eval_agent_expr (&ctx, cl->cond, &value); |
9f3a5c85 LM |
867 | } |
868 | ||
869 | if (err) | |
870 | return 1; | |
871 | ||
872 | return (value != 0); | |
873 | } | |
874 | ||
d3ce09f5 SS |
875 | /* Add commands COMMANDS to GDBserver's breakpoint BP. */ |
876 | ||
877 | void | |
878 | add_commands_to_breakpoint (struct breakpoint *bp, | |
879 | struct agent_expr *commands, int persist) | |
880 | { | |
881 | struct point_command_list *new_cmd; | |
882 | ||
883 | /* Create new command. */ | |
884 | new_cmd = xcalloc (1, sizeof (*new_cmd)); | |
885 | new_cmd->cmd = commands; | |
886 | new_cmd->persistence = persist; | |
887 | ||
888 | /* Add commands to the list. */ | |
889 | new_cmd->next = bp->command_list; | |
890 | bp->command_list = new_cmd; | |
891 | } | |
892 | ||
893 | /* Add a target-side command COMMAND to the breakpoint at ADDR. */ | |
894 | ||
895 | int | |
896 | add_breakpoint_commands (CORE_ADDR addr, char **command, int persist) | |
897 | { | |
898 | struct breakpoint *bp = find_gdb_breakpoint_at (addr); | |
899 | char *actparm = *command; | |
900 | struct agent_expr *cmd; | |
901 | ||
902 | if (bp == NULL) | |
903 | return 1; | |
904 | ||
905 | if (command == NULL) | |
906 | return 1; | |
907 | ||
908 | cmd = gdb_parse_agent_expr (&actparm); | |
909 | ||
910 | if (cmd == NULL) | |
911 | { | |
912 | fprintf (stderr, "Command evaluation failed. " | |
913 | "Disabling.\n"); | |
914 | return 0; | |
915 | } | |
916 | ||
917 | add_commands_to_breakpoint (bp, cmd, persist); | |
918 | ||
919 | *command = actparm; | |
920 | ||
921 | return 0; | |
922 | } | |
923 | ||
924 | /* Return true if there are no commands to run at this location, | |
925 | which likely means we want to report back to GDB. */ | |
926 | int | |
927 | gdb_no_commands_at_breakpoint (CORE_ADDR where) | |
928 | { | |
929 | struct breakpoint *bp = find_gdb_breakpoint_at (where); | |
930 | ||
931 | if (bp == NULL) | |
932 | return 0; | |
933 | ||
934 | if (debug_threads) | |
657c4e8c DE |
935 | fprintf (stderr, "at 0x%s, bp command_list is 0x%s\n", |
936 | paddress (where), | |
937 | phex_nz ((uintptr_t) bp->command_list, 0)); | |
d3ce09f5 SS |
938 | return (bp->command_list == NULL); |
939 | } | |
940 | ||
941 | void | |
942 | run_breakpoint_commands (CORE_ADDR where) | |
943 | { | |
944 | /* Fetch registers for the current inferior. */ | |
945 | struct breakpoint *bp = find_gdb_breakpoint_at (where); | |
946 | ULONGEST value = 0; | |
947 | struct point_command_list *cl; | |
948 | int err = 0; | |
5ae4861a | 949 | struct eval_agent_expr_context ctx; |
d3ce09f5 SS |
950 | |
951 | if (bp == NULL) | |
952 | return; | |
953 | ||
5ae4861a YQ |
954 | ctx.regcache = get_thread_regcache (current_inferior, 1); |
955 | ctx.tframe = NULL; | |
956 | ctx.tpoint = NULL; | |
957 | ||
d3ce09f5 SS |
958 | for (cl = bp->command_list; |
959 | cl && !value && !err; cl = cl->next) | |
960 | { | |
961 | /* Run the command. */ | |
5ae4861a | 962 | err = gdb_eval_agent_expr (&ctx, cl->cmd, &value); |
d3ce09f5 SS |
963 | |
964 | /* If one command has a problem, stop digging the hole deeper. */ | |
965 | if (err) | |
966 | break; | |
967 | } | |
968 | } | |
969 | ||
9f3a5c85 LM |
970 | /* Return 1 if there is a breakpoint inserted in address WHERE |
971 | and if its condition, if it exists, is true. */ | |
972 | ||
973 | int | |
974 | gdb_breakpoint_here (CORE_ADDR where) | |
975 | { | |
976 | return (find_gdb_breakpoint_at (where) != NULL); | |
68070c10 PA |
977 | } |
978 | ||
d50171e4 PA |
979 | void |
980 | set_reinsert_breakpoint (CORE_ADDR stop_at) | |
611cb4a5 | 981 | { |
414a389f PA |
982 | struct breakpoint *bp; |
983 | ||
984 | bp = set_breakpoint_at (stop_at, NULL); | |
414a389f | 985 | bp->type = reinsert_breakpoint; |
611cb4a5 DJ |
986 | } |
987 | ||
988 | void | |
d50171e4 | 989 | delete_reinsert_breakpoints (void) |
611cb4a5 | 990 | { |
d50171e4 PA |
991 | struct process_info *proc = current_process (); |
992 | struct breakpoint *bp, **bp_link; | |
611cb4a5 | 993 | |
d50171e4 PA |
994 | bp = proc->breakpoints; |
995 | bp_link = &proc->breakpoints; | |
611cb4a5 | 996 | |
d50171e4 PA |
997 | while (bp) |
998 | { | |
414a389f PA |
999 | if (bp->type == reinsert_breakpoint) |
1000 | { | |
1001 | *bp_link = bp->next; | |
8b07ae33 | 1002 | release_breakpoint (proc, bp); |
414a389f PA |
1003 | bp = *bp_link; |
1004 | } | |
1005 | else | |
1006 | { | |
1007 | bp_link = &bp->next; | |
1008 | bp = *bp_link; | |
1009 | } | |
d50171e4 PA |
1010 | } |
1011 | } | |
b65d95c5 | 1012 | |
d50171e4 | 1013 | static void |
8b07ae33 | 1014 | uninsert_raw_breakpoint (struct raw_breakpoint *bp) |
d50171e4 PA |
1015 | { |
1016 | if (bp->inserted) | |
1017 | { | |
1018 | int err; | |
6bf36717 | 1019 | unsigned char buf[MAX_BREAKPOINT_LEN]; |
d50171e4 PA |
1020 | |
1021 | bp->inserted = 0; | |
fa593d66 PA |
1022 | /* Since there can be fast tracepoint jumps inserted in the same |
1023 | address range, we use `write_inferior_memory', which takes | |
1024 | care of layering breakpoints on top of fast tracepoints, and | |
1025 | on top of the buffer we pass it. This works because we've | |
1026 | already unlinked the fast tracepoint jump above. Also note | |
1027 | that we need to pass the current shadow contents, because | |
1028 | write_inferior_memory updates any shadow memory with what we | |
1029 | pass here, and we want that to be a nop. */ | |
6bf36717 JK |
1030 | memcpy (buf, bp->old_data, breakpoint_len); |
1031 | err = write_inferior_memory (bp->pc, buf, breakpoint_len); | |
d50171e4 PA |
1032 | if (err != 0) |
1033 | { | |
1034 | bp->inserted = 1; | |
611cb4a5 | 1035 | |
d50171e4 PA |
1036 | if (debug_threads) |
1037 | fprintf (stderr, | |
1038 | "Failed to uninsert raw breakpoint at 0x%s (%s).\n", | |
1039 | paddress (bp->pc), strerror (err)); | |
1040 | } | |
1041 | } | |
611cb4a5 DJ |
1042 | } |
1043 | ||
1044 | void | |
d50171e4 | 1045 | uninsert_breakpoints_at (CORE_ADDR pc) |
611cb4a5 | 1046 | { |
8b07ae33 | 1047 | struct raw_breakpoint *bp; |
611cb4a5 | 1048 | |
8b07ae33 | 1049 | bp = find_raw_breakpoint_at (pc); |
611cb4a5 | 1050 | if (bp == NULL) |
d50171e4 PA |
1051 | { |
1052 | /* This can happen when we remove all breakpoints while handling | |
1053 | a step-over. */ | |
1054 | if (debug_threads) | |
1055 | fprintf (stderr, | |
1056 | "Could not find breakpoint at 0x%s " | |
1057 | "in list (uninserting).\n", | |
1058 | paddress (pc)); | |
1059 | return; | |
1060 | } | |
611cb4a5 | 1061 | |
d50171e4 | 1062 | if (bp->inserted) |
8b07ae33 | 1063 | uninsert_raw_breakpoint (bp); |
611cb4a5 DJ |
1064 | } |
1065 | ||
0fb4aa4b PA |
1066 | void |
1067 | uninsert_all_breakpoints (void) | |
1068 | { | |
1069 | struct process_info *proc = current_process (); | |
1070 | struct raw_breakpoint *bp; | |
1071 | ||
1072 | for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next) | |
1073 | if (bp->inserted) | |
1074 | uninsert_raw_breakpoint (bp); | |
1075 | } | |
1076 | ||
d50171e4 | 1077 | static void |
8b07ae33 | 1078 | reinsert_raw_breakpoint (struct raw_breakpoint *bp) |
611cb4a5 | 1079 | { |
d50171e4 | 1080 | int err; |
611cb4a5 | 1081 | |
d50171e4 | 1082 | if (bp->inserted) |
611cb4a5 DJ |
1083 | error ("Breakpoint already inserted at reinsert time."); |
1084 | ||
d50171e4 PA |
1085 | err = (*the_target->write_memory) (bp->pc, breakpoint_data, |
1086 | breakpoint_len); | |
1087 | if (err == 0) | |
1088 | bp->inserted = 1; | |
1089 | else if (debug_threads) | |
1090 | fprintf (stderr, | |
1091 | "Failed to reinsert breakpoint at 0x%s (%s).\n", | |
1092 | paddress (bp->pc), strerror (err)); | |
611cb4a5 DJ |
1093 | } |
1094 | ||
d50171e4 PA |
1095 | void |
1096 | reinsert_breakpoints_at (CORE_ADDR pc) | |
611cb4a5 | 1097 | { |
8b07ae33 | 1098 | struct raw_breakpoint *bp; |
611cb4a5 | 1099 | |
8b07ae33 | 1100 | bp = find_raw_breakpoint_at (pc); |
611cb4a5 | 1101 | if (bp == NULL) |
611cb4a5 | 1102 | { |
d50171e4 PA |
1103 | /* This can happen when we remove all breakpoints while handling |
1104 | a step-over. */ | |
1105 | if (debug_threads) | |
1106 | fprintf (stderr, | |
8b07ae33 | 1107 | "Could not find raw breakpoint at 0x%s " |
d50171e4 PA |
1108 | "in list (reinserting).\n", |
1109 | paddress (pc)); | |
1110 | return; | |
611cb4a5 DJ |
1111 | } |
1112 | ||
414a389f | 1113 | reinsert_raw_breakpoint (bp); |
d50171e4 PA |
1114 | } |
1115 | ||
0fb4aa4b PA |
1116 | void |
1117 | reinsert_all_breakpoints (void) | |
1118 | { | |
1119 | struct process_info *proc = current_process (); | |
1120 | struct raw_breakpoint *bp; | |
1121 | ||
1122 | for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next) | |
1123 | if (!bp->inserted) | |
1124 | reinsert_raw_breakpoint (bp); | |
1125 | } | |
1126 | ||
d50171e4 PA |
1127 | void |
1128 | check_breakpoints (CORE_ADDR stop_pc) | |
1129 | { | |
1130 | struct process_info *proc = current_process (); | |
1131 | struct breakpoint *bp, **bp_link; | |
1132 | ||
1133 | bp = proc->breakpoints; | |
1134 | bp_link = &proc->breakpoints; | |
1135 | ||
1136 | while (bp) | |
b65d95c5 | 1137 | { |
8b07ae33 | 1138 | if (bp->raw->pc == stop_pc) |
d50171e4 | 1139 | { |
8b07ae33 | 1140 | if (!bp->raw->inserted) |
d50171e4 PA |
1141 | { |
1142 | warning ("Hit a removed breakpoint?"); | |
1143 | return; | |
1144 | } | |
1145 | ||
1146 | if (bp->handler != NULL && (*bp->handler) (stop_pc)) | |
1147 | { | |
1148 | *bp_link = bp->next; | |
1149 | ||
8b07ae33 | 1150 | release_breakpoint (proc, bp); |
d50171e4 PA |
1151 | |
1152 | bp = *bp_link; | |
1153 | continue; | |
1154 | } | |
1155 | } | |
1156 | ||
1157 | bp_link = &bp->next; | |
1158 | bp = *bp_link; | |
b65d95c5 | 1159 | } |
611cb4a5 DJ |
1160 | } |
1161 | ||
1162 | void | |
f450004a | 1163 | set_breakpoint_data (const unsigned char *bp_data, int bp_len) |
611cb4a5 DJ |
1164 | { |
1165 | breakpoint_data = bp_data; | |
1166 | breakpoint_len = bp_len; | |
1167 | } | |
1168 | ||
d50171e4 PA |
1169 | int |
1170 | breakpoint_here (CORE_ADDR addr) | |
1171 | { | |
8b07ae33 | 1172 | return (find_raw_breakpoint_at (addr) != NULL); |
d50171e4 PA |
1173 | } |
1174 | ||
1175 | int | |
1176 | breakpoint_inserted_here (CORE_ADDR addr) | |
1177 | { | |
8b07ae33 | 1178 | struct raw_breakpoint *bp; |
d50171e4 | 1179 | |
8b07ae33 | 1180 | bp = find_raw_breakpoint_at (addr); |
d50171e4 | 1181 | |
8b07ae33 | 1182 | return (bp != NULL && bp->inserted); |
d50171e4 PA |
1183 | } |
1184 | ||
d3bbe7a0 PA |
1185 | static int |
1186 | validate_inserted_breakpoint (struct raw_breakpoint *bp) | |
1187 | { | |
1188 | unsigned char *buf; | |
1189 | int err; | |
1190 | ||
1191 | gdb_assert (bp->inserted); | |
1192 | ||
1193 | buf = alloca (breakpoint_len); | |
1194 | err = (*the_target->read_memory) (bp->pc, buf, breakpoint_len); | |
1195 | if (err || memcmp (buf, breakpoint_data, breakpoint_len) != 0) | |
1196 | { | |
1197 | /* Tag it as gone. */ | |
1198 | bp->inserted = 0; | |
1199 | bp->shlib_disabled = 1; | |
1200 | return 0; | |
1201 | } | |
1202 | ||
1203 | return 1; | |
1204 | } | |
1205 | ||
1206 | static void | |
1207 | delete_disabled_breakpoints (void) | |
1208 | { | |
1209 | struct process_info *proc = current_process (); | |
1210 | struct breakpoint *bp, *next; | |
1211 | ||
1212 | for (bp = proc->breakpoints; bp != NULL; bp = next) | |
1213 | { | |
1214 | next = bp->next; | |
1215 | if (bp->raw->shlib_disabled) | |
1216 | delete_breakpoint_1 (proc, bp); | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | /* Check if breakpoints we inserted still appear to be inserted. They | |
1221 | may disappear due to a shared library unload, and worse, a new | |
1222 | shared library may be reloaded at the same address as the | |
1223 | previously unloaded one. If that happens, we should make sure that | |
1224 | the shadow memory of the old breakpoints isn't used when reading or | |
1225 | writing memory. */ | |
1226 | ||
1227 | void | |
1228 | validate_breakpoints (void) | |
1229 | { | |
1230 | struct process_info *proc = current_process (); | |
1231 | struct breakpoint *bp; | |
1232 | ||
1233 | for (bp = proc->breakpoints; bp != NULL; bp = bp->next) | |
1234 | { | |
1235 | if (bp->raw->inserted) | |
1236 | validate_inserted_breakpoint (bp->raw); | |
1237 | } | |
1238 | ||
1239 | delete_disabled_breakpoints (); | |
1240 | } | |
1241 | ||
611cb4a5 | 1242 | void |
f450004a | 1243 | check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len) |
611cb4a5 | 1244 | { |
95954743 | 1245 | struct process_info *proc = current_process (); |
8b07ae33 | 1246 | struct raw_breakpoint *bp = proc->raw_breakpoints; |
fa593d66 | 1247 | struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps; |
611cb4a5 | 1248 | CORE_ADDR mem_end = mem_addr + mem_len; |
d3bbe7a0 | 1249 | int disabled_one = 0; |
611cb4a5 | 1250 | |
fa593d66 PA |
1251 | for (; jp != NULL; jp = jp->next) |
1252 | { | |
1253 | CORE_ADDR bp_end = jp->pc + jp->length; | |
1254 | CORE_ADDR start, end; | |
1255 | int copy_offset, copy_len, buf_offset; | |
1256 | ||
6bf36717 JK |
1257 | gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len |
1258 | || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length); | |
1259 | ||
fa593d66 PA |
1260 | if (mem_addr >= bp_end) |
1261 | continue; | |
1262 | if (jp->pc >= mem_end) | |
1263 | continue; | |
1264 | ||
1265 | start = jp->pc; | |
1266 | if (mem_addr > start) | |
1267 | start = mem_addr; | |
1268 | ||
1269 | end = bp_end; | |
1270 | if (end > mem_end) | |
1271 | end = mem_end; | |
1272 | ||
1273 | copy_len = end - start; | |
1274 | copy_offset = start - jp->pc; | |
1275 | buf_offset = start - mem_addr; | |
1276 | ||
1277 | if (jp->inserted) | |
1278 | memcpy (buf + buf_offset, | |
1279 | fast_tracepoint_jump_shadow (jp) + copy_offset, | |
1280 | copy_len); | |
1281 | } | |
1282 | ||
611cb4a5 DJ |
1283 | for (; bp != NULL; bp = bp->next) |
1284 | { | |
1285 | CORE_ADDR bp_end = bp->pc + breakpoint_len; | |
1286 | CORE_ADDR start, end; | |
1287 | int copy_offset, copy_len, buf_offset; | |
1288 | ||
6bf36717 JK |
1289 | gdb_assert (bp->old_data >= buf + mem_len |
1290 | || buf >= &bp->old_data[sizeof (bp->old_data)]); | |
1291 | ||
611cb4a5 DJ |
1292 | if (mem_addr >= bp_end) |
1293 | continue; | |
1294 | if (bp->pc >= mem_end) | |
1295 | continue; | |
1296 | ||
1297 | start = bp->pc; | |
1298 | if (mem_addr > start) | |
1299 | start = mem_addr; | |
1300 | ||
1301 | end = bp_end; | |
1302 | if (end > mem_end) | |
1303 | end = mem_end; | |
1304 | ||
1305 | copy_len = end - start; | |
1306 | copy_offset = start - bp->pc; | |
1307 | buf_offset = start - mem_addr; | |
1308 | ||
8b07ae33 | 1309 | if (bp->inserted) |
d3bbe7a0 PA |
1310 | { |
1311 | if (validate_inserted_breakpoint (bp)) | |
1312 | memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len); | |
1313 | else | |
1314 | disabled_one = 1; | |
1315 | } | |
611cb4a5 | 1316 | } |
d3bbe7a0 PA |
1317 | |
1318 | if (disabled_one) | |
1319 | delete_disabled_breakpoints (); | |
611cb4a5 DJ |
1320 | } |
1321 | ||
1322 | void | |
b9fd1791 PA |
1323 | check_mem_write (CORE_ADDR mem_addr, unsigned char *buf, |
1324 | const unsigned char *myaddr, int mem_len) | |
611cb4a5 | 1325 | { |
95954743 | 1326 | struct process_info *proc = current_process (); |
8b07ae33 | 1327 | struct raw_breakpoint *bp = proc->raw_breakpoints; |
fa593d66 | 1328 | struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps; |
611cb4a5 | 1329 | CORE_ADDR mem_end = mem_addr + mem_len; |
d3bbe7a0 | 1330 | int disabled_one = 0; |
611cb4a5 | 1331 | |
fa593d66 PA |
1332 | /* First fast tracepoint jumps, then breakpoint traps on top. */ |
1333 | ||
1334 | for (; jp != NULL; jp = jp->next) | |
1335 | { | |
1336 | CORE_ADDR jp_end = jp->pc + jp->length; | |
1337 | CORE_ADDR start, end; | |
1338 | int copy_offset, copy_len, buf_offset; | |
1339 | ||
6bf36717 JK |
1340 | gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len |
1341 | || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length); | |
1342 | gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len | |
1343 | || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length); | |
1344 | ||
fa593d66 PA |
1345 | if (mem_addr >= jp_end) |
1346 | continue; | |
1347 | if (jp->pc >= mem_end) | |
1348 | continue; | |
1349 | ||
1350 | start = jp->pc; | |
1351 | if (mem_addr > start) | |
1352 | start = mem_addr; | |
1353 | ||
1354 | end = jp_end; | |
1355 | if (end > mem_end) | |
1356 | end = mem_end; | |
1357 | ||
1358 | copy_len = end - start; | |
1359 | copy_offset = start - jp->pc; | |
1360 | buf_offset = start - mem_addr; | |
1361 | ||
1362 | memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset, | |
b9fd1791 | 1363 | myaddr + buf_offset, copy_len); |
fa593d66 PA |
1364 | if (jp->inserted) |
1365 | memcpy (buf + buf_offset, | |
1366 | fast_tracepoint_jump_insn (jp) + copy_offset, copy_len); | |
1367 | } | |
1368 | ||
611cb4a5 DJ |
1369 | for (; bp != NULL; bp = bp->next) |
1370 | { | |
1371 | CORE_ADDR bp_end = bp->pc + breakpoint_len; | |
1372 | CORE_ADDR start, end; | |
1373 | int copy_offset, copy_len, buf_offset; | |
1374 | ||
6bf36717 JK |
1375 | gdb_assert (bp->old_data >= myaddr + mem_len |
1376 | || myaddr >= &bp->old_data[sizeof (bp->old_data)]); | |
1377 | ||
611cb4a5 DJ |
1378 | if (mem_addr >= bp_end) |
1379 | continue; | |
1380 | if (bp->pc >= mem_end) | |
1381 | continue; | |
1382 | ||
1383 | start = bp->pc; | |
1384 | if (mem_addr > start) | |
1385 | start = mem_addr; | |
1386 | ||
1387 | end = bp_end; | |
1388 | if (end > mem_end) | |
1389 | end = mem_end; | |
1390 | ||
1391 | copy_len = end - start; | |
1392 | copy_offset = start - bp->pc; | |
1393 | buf_offset = start - mem_addr; | |
1394 | ||
b9fd1791 | 1395 | memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len); |
d50171e4 | 1396 | if (bp->inserted) |
d3bbe7a0 PA |
1397 | { |
1398 | if (validate_inserted_breakpoint (bp)) | |
1399 | memcpy (buf + buf_offset, breakpoint_data + copy_offset, copy_len); | |
1400 | else | |
1401 | disabled_one = 1; | |
1402 | } | |
611cb4a5 | 1403 | } |
d3bbe7a0 PA |
1404 | |
1405 | if (disabled_one) | |
1406 | delete_disabled_breakpoints (); | |
611cb4a5 | 1407 | } |
ae13219e | 1408 | |
95954743 | 1409 | /* Delete all breakpoints, and un-insert them from the inferior. */ |
ae13219e DJ |
1410 | |
1411 | void | |
1412 | delete_all_breakpoints (void) | |
1413 | { | |
95954743 PA |
1414 | struct process_info *proc = current_process (); |
1415 | ||
1416 | while (proc->breakpoints) | |
8b07ae33 | 1417 | delete_breakpoint_1 (proc, proc->breakpoints); |
95954743 PA |
1418 | } |
1419 | ||
f9e39928 | 1420 | /* Clear the "inserted" flag in all breakpoints. */ |
95954743 PA |
1421 | |
1422 | void | |
f9e39928 | 1423 | mark_breakpoints_out (struct process_info *proc) |
95954743 | 1424 | { |
8b07ae33 | 1425 | struct raw_breakpoint *raw_bp; |
95954743 | 1426 | |
8b07ae33 PA |
1427 | for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next) |
1428 | raw_bp->inserted = 0; | |
f9e39928 PA |
1429 | } |
1430 | ||
1431 | /* Release all breakpoints, but do not try to un-insert them from the | |
1432 | inferior. */ | |
1433 | ||
1434 | void | |
1435 | free_all_breakpoints (struct process_info *proc) | |
1436 | { | |
1437 | mark_breakpoints_out (proc); | |
8b07ae33 PA |
1438 | |
1439 | /* Note: use PROC explicitly instead of deferring to | |
1440 | delete_all_breakpoints --- CURRENT_INFERIOR may already have been | |
1441 | released when we get here. There should be no call to | |
1442 | current_process from here on. */ | |
95954743 | 1443 | while (proc->breakpoints) |
8b07ae33 | 1444 | delete_breakpoint_1 (proc, proc->breakpoints); |
ae13219e | 1445 | } |