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