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