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 | ||
51aa91f9 PA |
679 | /* Locate a breakpoint placed at address WHERE and return a pointer |
680 | to its structure. */ | |
681 | ||
682 | static struct breakpoint * | |
8b07ae33 | 683 | find_gdb_breakpoint_at (CORE_ADDR where) |
611cb4a5 | 684 | { |
95954743 | 685 | struct process_info *proc = current_process (); |
8b07ae33 | 686 | struct breakpoint *bp; |
611cb4a5 | 687 | |
8b07ae33 PA |
688 | for (bp = proc->breakpoints; bp != NULL; bp = bp->next) |
689 | if (bp->type == gdb_breakpoint && bp->raw->pc == where) | |
690 | return bp; | |
611cb4a5 DJ |
691 | |
692 | return NULL; | |
693 | } | |
694 | ||
8b07ae33 PA |
695 | int |
696 | set_gdb_breakpoint_at (CORE_ADDR where) | |
68070c10 | 697 | { |
8b07ae33 PA |
698 | struct breakpoint *bp; |
699 | ||
700 | if (breakpoint_data == NULL) | |
701 | return 1; | |
702 | ||
d3bbe7a0 PA |
703 | /* If we see GDB inserting a second breakpoint at the same address, |
704 | then the first breakpoint must have disappeared due to a shared | |
705 | library unload. On targets where the shared libraries are | |
706 | handled by userspace, like SVR4, for example, GDBserver can't | |
707 | tell if a library was loaded or unloaded. Since we refcount | |
708 | breakpoints, if we didn't do this, we'd just increase the | |
709 | refcount of the previous breakpoint at this address, but the trap | |
710 | was not planted in the inferior anymore, thus the breakpoint | |
711 | would never be hit. */ | |
712 | bp = find_gdb_breakpoint_at (where); | |
713 | if (bp != NULL) | |
714 | { | |
715 | delete_gdb_breakpoint_at (where); | |
716 | ||
717 | /* Might as well validate all other breakpoints. */ | |
718 | validate_breakpoints (); | |
719 | } | |
720 | ||
8b07ae33 PA |
721 | bp = set_breakpoint_at (where, NULL); |
722 | if (bp == NULL) | |
723 | return -1; | |
724 | ||
725 | bp->type = gdb_breakpoint; | |
726 | return 0; | |
727 | } | |
728 | ||
729 | int | |
730 | delete_gdb_breakpoint_at (CORE_ADDR addr) | |
731 | { | |
732 | struct breakpoint *bp; | |
733 | int err; | |
734 | ||
735 | if (breakpoint_data == NULL) | |
736 | return 1; | |
737 | ||
738 | bp = find_gdb_breakpoint_at (addr); | |
739 | if (bp == NULL) | |
740 | return -1; | |
741 | ||
9f3a5c85 LM |
742 | /* Before deleting the breakpoint, make sure to free |
743 | its condition list. */ | |
744 | clear_gdb_breakpoint_conditions (addr); | |
8b07ae33 PA |
745 | err = delete_breakpoint (bp); |
746 | if (err) | |
747 | return -1; | |
748 | ||
749 | return 0; | |
750 | } | |
751 | ||
9f3a5c85 LM |
752 | /* Clear all conditions associated with this breakpoint address. */ |
753 | ||
754 | void | |
755 | clear_gdb_breakpoint_conditions (CORE_ADDR addr) | |
756 | { | |
757 | struct breakpoint *bp = find_gdb_breakpoint_at (addr); | |
412c89dd | 758 | struct point_cond_list *cond; |
9f3a5c85 LM |
759 | |
760 | if (bp == NULL || bp->cond_list == NULL) | |
761 | return; | |
762 | ||
763 | cond = bp->cond_list; | |
9f3a5c85 LM |
764 | |
765 | while (cond != NULL) | |
766 | { | |
412c89dd LM |
767 | struct point_cond_list *cond_next; |
768 | ||
769 | cond_next = cond->next; | |
770 | free (cond->cond->bytes); | |
9f3a5c85 LM |
771 | free (cond->cond); |
772 | free (cond); | |
412c89dd | 773 | cond = cond_next; |
9f3a5c85 LM |
774 | } |
775 | ||
776 | bp->cond_list = NULL; | |
777 | } | |
778 | ||
779 | /* Add condition CONDITION to GDBserver's breakpoint BP. */ | |
780 | ||
781 | void | |
782 | add_condition_to_breakpoint (struct breakpoint *bp, | |
783 | struct agent_expr *condition) | |
784 | { | |
785 | struct point_cond_list *new_cond; | |
786 | ||
787 | /* Create new condition. */ | |
788 | new_cond = xcalloc (1, sizeof (*new_cond)); | |
789 | new_cond->cond = condition; | |
790 | ||
791 | /* Add condition to the list. */ | |
792 | new_cond->next = bp->cond_list; | |
793 | bp->cond_list = new_cond; | |
794 | } | |
795 | ||
796 | /* Add a target-side condition CONDITION to the breakpoint at ADDR. */ | |
797 | ||
8b07ae33 | 798 | int |
9f3a5c85 LM |
799 | add_breakpoint_condition (CORE_ADDR addr, char **condition) |
800 | { | |
801 | struct breakpoint *bp = find_gdb_breakpoint_at (addr); | |
802 | char *actparm = *condition; | |
803 | struct agent_expr *cond; | |
804 | ||
9f3a5c85 LM |
805 | if (condition == NULL) |
806 | return 1; | |
807 | ||
d708bcd1 PA |
808 | if (bp == NULL) |
809 | return 0; | |
810 | ||
9f3a5c85 LM |
811 | cond = gdb_parse_agent_expr (&actparm); |
812 | ||
813 | if (cond == NULL) | |
814 | { | |
815 | fprintf (stderr, "Condition evaluation failed. " | |
816 | "Assuming unconditional.\n"); | |
817 | return 0; | |
818 | } | |
819 | ||
820 | add_condition_to_breakpoint (bp, cond); | |
821 | ||
822 | *condition = actparm; | |
823 | ||
d708bcd1 | 824 | return 1; |
9f3a5c85 LM |
825 | } |
826 | ||
827 | /* Evaluate condition (if any) at breakpoint BP. Return 1 if | |
828 | true and 0 otherwise. */ | |
829 | ||
830 | int | |
831 | gdb_condition_true_at_breakpoint (CORE_ADDR where) | |
8b07ae33 | 832 | { |
9f3a5c85 | 833 | /* Fetch registers for the current inferior. */ |
8b07ae33 | 834 | struct breakpoint *bp = find_gdb_breakpoint_at (where); |
9f3a5c85 LM |
835 | ULONGEST value = 0; |
836 | struct point_cond_list *cl; | |
837 | int err = 0; | |
5ae4861a | 838 | struct eval_agent_expr_context ctx; |
9f3a5c85 LM |
839 | |
840 | if (bp == NULL) | |
841 | return 0; | |
8b07ae33 | 842 | |
9f3a5c85 LM |
843 | /* Check if the breakpoint is unconditional. If it is, |
844 | the condition always evaluates to TRUE. */ | |
845 | if (bp->cond_list == NULL) | |
846 | return 1; | |
847 | ||
5ae4861a YQ |
848 | ctx.regcache = get_thread_regcache (current_inferior, 1); |
849 | ctx.tframe = NULL; | |
850 | ctx.tpoint = NULL; | |
851 | ||
9f3a5c85 LM |
852 | /* Evaluate each condition in the breakpoint's list of conditions. |
853 | Return true if any of the conditions evaluates to TRUE. | |
854 | ||
855 | If we failed to evaluate the expression, TRUE is returned. This | |
856 | forces GDB to reevaluate the conditions. */ | |
857 | for (cl = bp->cond_list; | |
858 | cl && !value && !err; cl = cl->next) | |
859 | { | |
860 | /* Evaluate the condition. */ | |
5ae4861a | 861 | err = gdb_eval_agent_expr (&ctx, cl->cond, &value); |
9f3a5c85 LM |
862 | } |
863 | ||
864 | if (err) | |
865 | return 1; | |
866 | ||
867 | return (value != 0); | |
868 | } | |
869 | ||
d3ce09f5 SS |
870 | /* Add commands COMMANDS to GDBserver's breakpoint BP. */ |
871 | ||
872 | void | |
873 | add_commands_to_breakpoint (struct breakpoint *bp, | |
874 | struct agent_expr *commands, int persist) | |
875 | { | |
876 | struct point_command_list *new_cmd; | |
877 | ||
878 | /* Create new command. */ | |
879 | new_cmd = xcalloc (1, sizeof (*new_cmd)); | |
880 | new_cmd->cmd = commands; | |
881 | new_cmd->persistence = persist; | |
882 | ||
883 | /* Add commands to the list. */ | |
884 | new_cmd->next = bp->command_list; | |
885 | bp->command_list = new_cmd; | |
886 | } | |
887 | ||
888 | /* Add a target-side command COMMAND to the breakpoint at ADDR. */ | |
889 | ||
890 | int | |
891 | add_breakpoint_commands (CORE_ADDR addr, char **command, int persist) | |
892 | { | |
893 | struct breakpoint *bp = find_gdb_breakpoint_at (addr); | |
894 | char *actparm = *command; | |
895 | struct agent_expr *cmd; | |
896 | ||
d3ce09f5 SS |
897 | if (command == NULL) |
898 | return 1; | |
899 | ||
d708bcd1 PA |
900 | if (bp == NULL) |
901 | return 0; | |
902 | ||
d3ce09f5 SS |
903 | cmd = gdb_parse_agent_expr (&actparm); |
904 | ||
905 | if (cmd == NULL) | |
906 | { | |
907 | fprintf (stderr, "Command evaluation failed. " | |
908 | "Disabling.\n"); | |
909 | return 0; | |
910 | } | |
911 | ||
912 | add_commands_to_breakpoint (bp, cmd, persist); | |
913 | ||
914 | *command = actparm; | |
915 | ||
d708bcd1 | 916 | return 1; |
d3ce09f5 SS |
917 | } |
918 | ||
919 | /* Return true if there are no commands to run at this location, | |
920 | which likely means we want to report back to GDB. */ | |
921 | int | |
922 | gdb_no_commands_at_breakpoint (CORE_ADDR where) | |
923 | { | |
924 | struct breakpoint *bp = find_gdb_breakpoint_at (where); | |
925 | ||
926 | if (bp == NULL) | |
927 | return 0; | |
928 | ||
929 | if (debug_threads) | |
87ce2a04 DE |
930 | debug_printf ("at 0x%s, bp command_list is 0x%s\n", |
931 | paddress (where), | |
932 | phex_nz ((uintptr_t) bp->command_list, 0)); | |
d3ce09f5 SS |
933 | return (bp->command_list == NULL); |
934 | } | |
935 | ||
936 | void | |
937 | run_breakpoint_commands (CORE_ADDR where) | |
938 | { | |
939 | /* Fetch registers for the current inferior. */ | |
940 | struct breakpoint *bp = find_gdb_breakpoint_at (where); | |
941 | ULONGEST value = 0; | |
942 | struct point_command_list *cl; | |
943 | int err = 0; | |
5ae4861a | 944 | struct eval_agent_expr_context ctx; |
d3ce09f5 SS |
945 | |
946 | if (bp == NULL) | |
947 | return; | |
948 | ||
5ae4861a YQ |
949 | ctx.regcache = get_thread_regcache (current_inferior, 1); |
950 | ctx.tframe = NULL; | |
951 | ctx.tpoint = NULL; | |
952 | ||
d3ce09f5 SS |
953 | for (cl = bp->command_list; |
954 | cl && !value && !err; cl = cl->next) | |
955 | { | |
956 | /* Run the command. */ | |
5ae4861a | 957 | err = gdb_eval_agent_expr (&ctx, cl->cmd, &value); |
d3ce09f5 SS |
958 | |
959 | /* If one command has a problem, stop digging the hole deeper. */ | |
960 | if (err) | |
961 | break; | |
962 | } | |
963 | } | |
964 | ||
9f3a5c85 LM |
965 | /* Return 1 if there is a breakpoint inserted in address WHERE |
966 | and if its condition, if it exists, is true. */ | |
967 | ||
968 | int | |
969 | gdb_breakpoint_here (CORE_ADDR where) | |
970 | { | |
971 | return (find_gdb_breakpoint_at (where) != NULL); | |
68070c10 PA |
972 | } |
973 | ||
d50171e4 PA |
974 | void |
975 | set_reinsert_breakpoint (CORE_ADDR stop_at) | |
611cb4a5 | 976 | { |
414a389f PA |
977 | struct breakpoint *bp; |
978 | ||
979 | bp = set_breakpoint_at (stop_at, NULL); | |
414a389f | 980 | bp->type = reinsert_breakpoint; |
611cb4a5 DJ |
981 | } |
982 | ||
983 | void | |
d50171e4 | 984 | delete_reinsert_breakpoints (void) |
611cb4a5 | 985 | { |
d50171e4 PA |
986 | struct process_info *proc = current_process (); |
987 | struct breakpoint *bp, **bp_link; | |
611cb4a5 | 988 | |
d50171e4 PA |
989 | bp = proc->breakpoints; |
990 | bp_link = &proc->breakpoints; | |
611cb4a5 | 991 | |
d50171e4 PA |
992 | while (bp) |
993 | { | |
414a389f PA |
994 | if (bp->type == reinsert_breakpoint) |
995 | { | |
996 | *bp_link = bp->next; | |
8b07ae33 | 997 | release_breakpoint (proc, bp); |
414a389f PA |
998 | bp = *bp_link; |
999 | } | |
1000 | else | |
1001 | { | |
1002 | bp_link = &bp->next; | |
1003 | bp = *bp_link; | |
1004 | } | |
d50171e4 PA |
1005 | } |
1006 | } | |
b65d95c5 | 1007 | |
d50171e4 | 1008 | static void |
8b07ae33 | 1009 | uninsert_raw_breakpoint (struct raw_breakpoint *bp) |
d50171e4 PA |
1010 | { |
1011 | if (bp->inserted) | |
1012 | { | |
1013 | int err; | |
6bf36717 | 1014 | unsigned char buf[MAX_BREAKPOINT_LEN]; |
d50171e4 PA |
1015 | |
1016 | bp->inserted = 0; | |
fa593d66 PA |
1017 | /* Since there can be fast tracepoint jumps inserted in the same |
1018 | address range, we use `write_inferior_memory', which takes | |
1019 | care of layering breakpoints on top of fast tracepoints, and | |
1020 | on top of the buffer we pass it. This works because we've | |
1021 | already unlinked the fast tracepoint jump above. Also note | |
1022 | that we need to pass the current shadow contents, because | |
1023 | write_inferior_memory updates any shadow memory with what we | |
1024 | pass here, and we want that to be a nop. */ | |
6bf36717 JK |
1025 | memcpy (buf, bp->old_data, breakpoint_len); |
1026 | err = write_inferior_memory (bp->pc, buf, breakpoint_len); | |
d50171e4 PA |
1027 | if (err != 0) |
1028 | { | |
1029 | bp->inserted = 1; | |
611cb4a5 | 1030 | |
d50171e4 | 1031 | if (debug_threads) |
87ce2a04 DE |
1032 | debug_printf ("Failed to uninsert raw breakpoint at 0x%s (%s).\n", |
1033 | paddress (bp->pc), strerror (err)); | |
d50171e4 PA |
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) | |
87ce2a04 DE |
1049 | debug_printf ("Could not find breakpoint at 0x%s " |
1050 | "in list (uninserting).\n", | |
1051 | paddress (pc)); | |
d50171e4 PA |
1052 | return; |
1053 | } | |
611cb4a5 | 1054 | |
d50171e4 | 1055 | if (bp->inserted) |
8b07ae33 | 1056 | uninsert_raw_breakpoint (bp); |
611cb4a5 DJ |
1057 | } |
1058 | ||
0fb4aa4b PA |
1059 | void |
1060 | uninsert_all_breakpoints (void) | |
1061 | { | |
1062 | struct process_info *proc = current_process (); | |
1063 | struct raw_breakpoint *bp; | |
1064 | ||
1065 | for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next) | |
1066 | if (bp->inserted) | |
1067 | uninsert_raw_breakpoint (bp); | |
1068 | } | |
1069 | ||
d50171e4 | 1070 | static void |
8b07ae33 | 1071 | reinsert_raw_breakpoint (struct raw_breakpoint *bp) |
611cb4a5 | 1072 | { |
d50171e4 | 1073 | int err; |
611cb4a5 | 1074 | |
d50171e4 | 1075 | if (bp->inserted) |
611cb4a5 DJ |
1076 | error ("Breakpoint already inserted at reinsert time."); |
1077 | ||
d50171e4 PA |
1078 | err = (*the_target->write_memory) (bp->pc, breakpoint_data, |
1079 | breakpoint_len); | |
1080 | if (err == 0) | |
1081 | bp->inserted = 1; | |
1082 | else if (debug_threads) | |
87ce2a04 DE |
1083 | debug_printf ("Failed to reinsert breakpoint at 0x%s (%s).\n", |
1084 | paddress (bp->pc), strerror (err)); | |
611cb4a5 DJ |
1085 | } |
1086 | ||
d50171e4 PA |
1087 | void |
1088 | reinsert_breakpoints_at (CORE_ADDR pc) | |
611cb4a5 | 1089 | { |
8b07ae33 | 1090 | struct raw_breakpoint *bp; |
611cb4a5 | 1091 | |
8b07ae33 | 1092 | bp = find_raw_breakpoint_at (pc); |
611cb4a5 | 1093 | if (bp == NULL) |
611cb4a5 | 1094 | { |
d50171e4 PA |
1095 | /* This can happen when we remove all breakpoints while handling |
1096 | a step-over. */ | |
1097 | if (debug_threads) | |
87ce2a04 DE |
1098 | debug_printf ("Could not find raw breakpoint at 0x%s " |
1099 | "in list (reinserting).\n", | |
1100 | paddress (pc)); | |
d50171e4 | 1101 | return; |
611cb4a5 DJ |
1102 | } |
1103 | ||
414a389f | 1104 | reinsert_raw_breakpoint (bp); |
d50171e4 PA |
1105 | } |
1106 | ||
0fb4aa4b PA |
1107 | void |
1108 | reinsert_all_breakpoints (void) | |
1109 | { | |
1110 | struct process_info *proc = current_process (); | |
1111 | struct raw_breakpoint *bp; | |
1112 | ||
1113 | for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next) | |
1114 | if (!bp->inserted) | |
1115 | reinsert_raw_breakpoint (bp); | |
1116 | } | |
1117 | ||
d50171e4 PA |
1118 | void |
1119 | check_breakpoints (CORE_ADDR stop_pc) | |
1120 | { | |
1121 | struct process_info *proc = current_process (); | |
1122 | struct breakpoint *bp, **bp_link; | |
1123 | ||
1124 | bp = proc->breakpoints; | |
1125 | bp_link = &proc->breakpoints; | |
1126 | ||
1127 | while (bp) | |
b65d95c5 | 1128 | { |
8b07ae33 | 1129 | if (bp->raw->pc == stop_pc) |
d50171e4 | 1130 | { |
8b07ae33 | 1131 | if (!bp->raw->inserted) |
d50171e4 PA |
1132 | { |
1133 | warning ("Hit a removed breakpoint?"); | |
1134 | return; | |
1135 | } | |
1136 | ||
1137 | if (bp->handler != NULL && (*bp->handler) (stop_pc)) | |
1138 | { | |
1139 | *bp_link = bp->next; | |
1140 | ||
8b07ae33 | 1141 | release_breakpoint (proc, bp); |
d50171e4 PA |
1142 | |
1143 | bp = *bp_link; | |
1144 | continue; | |
1145 | } | |
1146 | } | |
1147 | ||
1148 | bp_link = &bp->next; | |
1149 | bp = *bp_link; | |
b65d95c5 | 1150 | } |
611cb4a5 DJ |
1151 | } |
1152 | ||
1153 | void | |
f450004a | 1154 | set_breakpoint_data (const unsigned char *bp_data, int bp_len) |
611cb4a5 DJ |
1155 | { |
1156 | breakpoint_data = bp_data; | |
1157 | breakpoint_len = bp_len; | |
1158 | } | |
1159 | ||
d50171e4 PA |
1160 | int |
1161 | breakpoint_here (CORE_ADDR addr) | |
1162 | { | |
8b07ae33 | 1163 | return (find_raw_breakpoint_at (addr) != NULL); |
d50171e4 PA |
1164 | } |
1165 | ||
1166 | int | |
1167 | breakpoint_inserted_here (CORE_ADDR addr) | |
1168 | { | |
8b07ae33 | 1169 | struct raw_breakpoint *bp; |
d50171e4 | 1170 | |
8b07ae33 | 1171 | bp = find_raw_breakpoint_at (addr); |
d50171e4 | 1172 | |
8b07ae33 | 1173 | return (bp != NULL && bp->inserted); |
d50171e4 PA |
1174 | } |
1175 | ||
d3bbe7a0 PA |
1176 | static int |
1177 | validate_inserted_breakpoint (struct raw_breakpoint *bp) | |
1178 | { | |
1179 | unsigned char *buf; | |
1180 | int err; | |
1181 | ||
1182 | gdb_assert (bp->inserted); | |
1183 | ||
1184 | buf = alloca (breakpoint_len); | |
1185 | err = (*the_target->read_memory) (bp->pc, buf, breakpoint_len); | |
1186 | if (err || memcmp (buf, breakpoint_data, breakpoint_len) != 0) | |
1187 | { | |
1188 | /* Tag it as gone. */ | |
1189 | bp->inserted = 0; | |
1190 | bp->shlib_disabled = 1; | |
1191 | return 0; | |
1192 | } | |
1193 | ||
1194 | return 1; | |
1195 | } | |
1196 | ||
1197 | static void | |
1198 | delete_disabled_breakpoints (void) | |
1199 | { | |
1200 | struct process_info *proc = current_process (); | |
1201 | struct breakpoint *bp, *next; | |
1202 | ||
1203 | for (bp = proc->breakpoints; bp != NULL; bp = next) | |
1204 | { | |
1205 | next = bp->next; | |
1206 | if (bp->raw->shlib_disabled) | |
1207 | delete_breakpoint_1 (proc, bp); | |
1208 | } | |
1209 | } | |
1210 | ||
1211 | /* Check if breakpoints we inserted still appear to be inserted. They | |
1212 | may disappear due to a shared library unload, and worse, a new | |
1213 | shared library may be reloaded at the same address as the | |
1214 | previously unloaded one. If that happens, we should make sure that | |
1215 | the shadow memory of the old breakpoints isn't used when reading or | |
1216 | writing memory. */ | |
1217 | ||
1218 | void | |
1219 | validate_breakpoints (void) | |
1220 | { | |
1221 | struct process_info *proc = current_process (); | |
1222 | struct breakpoint *bp; | |
1223 | ||
1224 | for (bp = proc->breakpoints; bp != NULL; bp = bp->next) | |
1225 | { | |
1226 | if (bp->raw->inserted) | |
1227 | validate_inserted_breakpoint (bp->raw); | |
1228 | } | |
1229 | ||
1230 | delete_disabled_breakpoints (); | |
1231 | } | |
1232 | ||
611cb4a5 | 1233 | void |
f450004a | 1234 | check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len) |
611cb4a5 | 1235 | { |
95954743 | 1236 | struct process_info *proc = current_process (); |
8b07ae33 | 1237 | struct raw_breakpoint *bp = proc->raw_breakpoints; |
fa593d66 | 1238 | struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps; |
611cb4a5 | 1239 | CORE_ADDR mem_end = mem_addr + mem_len; |
d3bbe7a0 | 1240 | int disabled_one = 0; |
611cb4a5 | 1241 | |
fa593d66 PA |
1242 | for (; jp != NULL; jp = jp->next) |
1243 | { | |
1244 | CORE_ADDR bp_end = jp->pc + jp->length; | |
1245 | CORE_ADDR start, end; | |
1246 | int copy_offset, copy_len, buf_offset; | |
1247 | ||
6bf36717 JK |
1248 | gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len |
1249 | || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length); | |
1250 | ||
fa593d66 PA |
1251 | if (mem_addr >= bp_end) |
1252 | continue; | |
1253 | if (jp->pc >= mem_end) | |
1254 | continue; | |
1255 | ||
1256 | start = jp->pc; | |
1257 | if (mem_addr > start) | |
1258 | start = mem_addr; | |
1259 | ||
1260 | end = bp_end; | |
1261 | if (end > mem_end) | |
1262 | end = mem_end; | |
1263 | ||
1264 | copy_len = end - start; | |
1265 | copy_offset = start - jp->pc; | |
1266 | buf_offset = start - mem_addr; | |
1267 | ||
1268 | if (jp->inserted) | |
1269 | memcpy (buf + buf_offset, | |
1270 | fast_tracepoint_jump_shadow (jp) + copy_offset, | |
1271 | copy_len); | |
1272 | } | |
1273 | ||
611cb4a5 DJ |
1274 | for (; bp != NULL; bp = bp->next) |
1275 | { | |
1276 | CORE_ADDR bp_end = bp->pc + breakpoint_len; | |
1277 | CORE_ADDR start, end; | |
1278 | int copy_offset, copy_len, buf_offset; | |
1279 | ||
6bf36717 JK |
1280 | gdb_assert (bp->old_data >= buf + mem_len |
1281 | || buf >= &bp->old_data[sizeof (bp->old_data)]); | |
1282 | ||
611cb4a5 DJ |
1283 | if (mem_addr >= bp_end) |
1284 | continue; | |
1285 | if (bp->pc >= mem_end) | |
1286 | continue; | |
1287 | ||
1288 | start = bp->pc; | |
1289 | if (mem_addr > start) | |
1290 | start = mem_addr; | |
1291 | ||
1292 | end = bp_end; | |
1293 | if (end > mem_end) | |
1294 | end = mem_end; | |
1295 | ||
1296 | copy_len = end - start; | |
1297 | copy_offset = start - bp->pc; | |
1298 | buf_offset = start - mem_addr; | |
1299 | ||
8b07ae33 | 1300 | if (bp->inserted) |
d3bbe7a0 PA |
1301 | { |
1302 | if (validate_inserted_breakpoint (bp)) | |
1303 | memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len); | |
1304 | else | |
1305 | disabled_one = 1; | |
1306 | } | |
611cb4a5 | 1307 | } |
d3bbe7a0 PA |
1308 | |
1309 | if (disabled_one) | |
1310 | delete_disabled_breakpoints (); | |
611cb4a5 DJ |
1311 | } |
1312 | ||
1313 | void | |
b9fd1791 PA |
1314 | check_mem_write (CORE_ADDR mem_addr, unsigned char *buf, |
1315 | const unsigned char *myaddr, int mem_len) | |
611cb4a5 | 1316 | { |
95954743 | 1317 | struct process_info *proc = current_process (); |
8b07ae33 | 1318 | struct raw_breakpoint *bp = proc->raw_breakpoints; |
fa593d66 | 1319 | struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps; |
611cb4a5 | 1320 | CORE_ADDR mem_end = mem_addr + mem_len; |
d3bbe7a0 | 1321 | int disabled_one = 0; |
611cb4a5 | 1322 | |
fa593d66 PA |
1323 | /* First fast tracepoint jumps, then breakpoint traps on top. */ |
1324 | ||
1325 | for (; jp != NULL; jp = jp->next) | |
1326 | { | |
1327 | CORE_ADDR jp_end = jp->pc + jp->length; | |
1328 | CORE_ADDR start, end; | |
1329 | int copy_offset, copy_len, buf_offset; | |
1330 | ||
6bf36717 JK |
1331 | gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len |
1332 | || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length); | |
1333 | gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len | |
1334 | || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length); | |
1335 | ||
fa593d66 PA |
1336 | if (mem_addr >= jp_end) |
1337 | continue; | |
1338 | if (jp->pc >= mem_end) | |
1339 | continue; | |
1340 | ||
1341 | start = jp->pc; | |
1342 | if (mem_addr > start) | |
1343 | start = mem_addr; | |
1344 | ||
1345 | end = jp_end; | |
1346 | if (end > mem_end) | |
1347 | end = mem_end; | |
1348 | ||
1349 | copy_len = end - start; | |
1350 | copy_offset = start - jp->pc; | |
1351 | buf_offset = start - mem_addr; | |
1352 | ||
1353 | memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset, | |
b9fd1791 | 1354 | myaddr + buf_offset, copy_len); |
fa593d66 PA |
1355 | if (jp->inserted) |
1356 | memcpy (buf + buf_offset, | |
1357 | fast_tracepoint_jump_insn (jp) + copy_offset, copy_len); | |
1358 | } | |
1359 | ||
611cb4a5 DJ |
1360 | for (; bp != NULL; bp = bp->next) |
1361 | { | |
1362 | CORE_ADDR bp_end = bp->pc + breakpoint_len; | |
1363 | CORE_ADDR start, end; | |
1364 | int copy_offset, copy_len, buf_offset; | |
1365 | ||
6bf36717 JK |
1366 | gdb_assert (bp->old_data >= myaddr + mem_len |
1367 | || myaddr >= &bp->old_data[sizeof (bp->old_data)]); | |
1368 | ||
611cb4a5 DJ |
1369 | if (mem_addr >= bp_end) |
1370 | continue; | |
1371 | if (bp->pc >= mem_end) | |
1372 | continue; | |
1373 | ||
1374 | start = bp->pc; | |
1375 | if (mem_addr > start) | |
1376 | start = mem_addr; | |
1377 | ||
1378 | end = bp_end; | |
1379 | if (end > mem_end) | |
1380 | end = mem_end; | |
1381 | ||
1382 | copy_len = end - start; | |
1383 | copy_offset = start - bp->pc; | |
1384 | buf_offset = start - mem_addr; | |
1385 | ||
b9fd1791 | 1386 | memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len); |
d50171e4 | 1387 | if (bp->inserted) |
d3bbe7a0 PA |
1388 | { |
1389 | if (validate_inserted_breakpoint (bp)) | |
1390 | memcpy (buf + buf_offset, breakpoint_data + copy_offset, copy_len); | |
1391 | else | |
1392 | disabled_one = 1; | |
1393 | } | |
611cb4a5 | 1394 | } |
d3bbe7a0 PA |
1395 | |
1396 | if (disabled_one) | |
1397 | delete_disabled_breakpoints (); | |
611cb4a5 | 1398 | } |
ae13219e | 1399 | |
95954743 | 1400 | /* Delete all breakpoints, and un-insert them from the inferior. */ |
ae13219e DJ |
1401 | |
1402 | void | |
1403 | delete_all_breakpoints (void) | |
1404 | { | |
95954743 PA |
1405 | struct process_info *proc = current_process (); |
1406 | ||
1407 | while (proc->breakpoints) | |
8b07ae33 | 1408 | delete_breakpoint_1 (proc, proc->breakpoints); |
95954743 PA |
1409 | } |
1410 | ||
f9e39928 | 1411 | /* Clear the "inserted" flag in all breakpoints. */ |
95954743 PA |
1412 | |
1413 | void | |
f9e39928 | 1414 | mark_breakpoints_out (struct process_info *proc) |
95954743 | 1415 | { |
8b07ae33 | 1416 | struct raw_breakpoint *raw_bp; |
95954743 | 1417 | |
8b07ae33 PA |
1418 | for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next) |
1419 | raw_bp->inserted = 0; | |
f9e39928 PA |
1420 | } |
1421 | ||
1422 | /* Release all breakpoints, but do not try to un-insert them from the | |
1423 | inferior. */ | |
1424 | ||
1425 | void | |
1426 | free_all_breakpoints (struct process_info *proc) | |
1427 | { | |
1428 | mark_breakpoints_out (proc); | |
8b07ae33 PA |
1429 | |
1430 | /* Note: use PROC explicitly instead of deferring to | |
1431 | delete_all_breakpoints --- CURRENT_INFERIOR may already have been | |
1432 | released when we get here. There should be no call to | |
1433 | current_process from here on. */ | |
95954743 | 1434 | while (proc->breakpoints) |
8b07ae33 | 1435 | delete_breakpoint_1 (proc, proc->breakpoints); |
ae13219e | 1436 | } |