gdbserver: on GDB breakpoint reinsertion, also delete the breakpoint's commands.
[deliverable/binutils-gdb.git] / gdb / gdbserver / mem-break.c
1 /* Memory breakpoint operations for the remote server for GDB.
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
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
10 the Free Software Foundation; either version 3 of the License, or
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "server.h"
22 #include "regcache.h"
23 #include "ax.h"
24 #include <stdint.h>
25
26 const unsigned char *breakpoint_data;
27 int breakpoint_len;
28
29 #define MAX_BREAKPOINT_LEN 8
30
31 /* GDB will never try to install multiple breakpoints at the same
32 address. However, we can see GDB requesting to insert a breakpoint
33 at an address is had already inserted one previously in a few
34 situations.
35
36 - The RSP documentation on Z packets says that to avoid potential
37 problems with duplicate packets, the operations should be
38 implemented in an idempotent way.
39
40 - A breakpoint is set at ADDR, an address in a shared library.
41 Then the shared library is unloaded. And then another, unrelated,
42 breakpoint at ADDR is set. There is not breakpoint removal request
43 between the first and the second breakpoint.
44
45 - When GDB wants to update the target-side breakpoint conditions or
46 commands, it re-inserts the breakpoint, with updated
47 conditions/commands associated.
48
49 Also, we need to keep track of internal breakpoints too, so we do
50 need to be able to install multiple breakpoints at the same address
51 transparently.
52
53 We keep track of two different, and closely related structures. A
54 raw breakpoint, which manages the low level, close to the metal
55 aspect of a breakpoint. It holds the breakpoint address, and for
56 software breakpoints, a buffer holding a copy of the instructions
57 that would be in memory had not been a breakpoint there (we call
58 that the shadow memory of the breakpoint). We occasionally need to
59 temporarilly uninsert a breakpoint without the client knowing about
60 it (e.g., to step over an internal breakpoint), so we keep an
61 `inserted' state associated with this low level breakpoint
62 structure. There can only be one such object for a given address.
63 Then, we have (a bit higher level) breakpoints. This structure
64 holds a callback to be called whenever a breakpoint is hit, a
65 high-level type, and a link to a low level raw breakpoint. There
66 can be many high-level breakpoints at the same address, and all of
67 them will point to the same raw breakpoint, which is reference
68 counted. */
69
70 /* The low level, physical, raw breakpoint. */
71 struct raw_breakpoint
72 {
73 struct raw_breakpoint *next;
74
75 /* The low level type of the breakpoint (software breakpoint,
76 watchpoint, etc.) */
77 enum raw_bkpt_type raw_type;
78
79 /* A reference count. Each high level breakpoint referencing this
80 raw breakpoint accounts for one reference. */
81 int refcount;
82
83 /* The breakpoint's insertion address. There can only be one raw
84 breakpoint for a given PC. */
85 CORE_ADDR pc;
86
87 /* The breakpoint's size. */
88 int size;
89
90 /* The breakpoint's shadow memory. */
91 unsigned char old_data[MAX_BREAKPOINT_LEN];
92
93 /* Positive if this breakpoint is currently inserted in the
94 inferior. Negative if it was, but we've detected that it's now
95 gone. Zero if not inserted. */
96 int inserted;
97 };
98
99 /* The type of a breakpoint. */
100 enum bkpt_type
101 {
102 /* A GDB breakpoint, requested with a Z0 packet. */
103 gdb_breakpoint_Z0,
104
105 /* A GDB hardware breakpoint, requested with a Z1 packet. */
106 gdb_breakpoint_Z1,
107
108 /* A GDB write watchpoint, requested with a Z2 packet. */
109 gdb_breakpoint_Z2,
110
111 /* A GDB read watchpoint, requested with a Z3 packet. */
112 gdb_breakpoint_Z3,
113
114 /* A GDB access watchpoint, requested with a Z4 packet. */
115 gdb_breakpoint_Z4,
116
117 /* A basic-software-single-step breakpoint. */
118 reinsert_breakpoint,
119
120 /* Any other breakpoint type that doesn't require specific
121 treatment goes here. E.g., an event breakpoint. */
122 other_breakpoint,
123 };
124
125 struct point_cond_list
126 {
127 /* Pointer to the agent expression that is the breakpoint's
128 conditional. */
129 struct agent_expr *cond;
130
131 /* Pointer to the next condition. */
132 struct point_cond_list *next;
133 };
134
135 struct point_command_list
136 {
137 /* Pointer to the agent expression that is the breakpoint's
138 commands. */
139 struct agent_expr *cmd;
140
141 /* Flag that is true if this command should run even while GDB is
142 disconnected. */
143 int persistence;
144
145 /* Pointer to the next command. */
146 struct point_command_list *next;
147 };
148
149 /* A high level (in gdbserver's perspective) breakpoint. */
150 struct breakpoint
151 {
152 struct breakpoint *next;
153
154 /* The breakpoint's type. */
155 enum bkpt_type type;
156
157 /* Pointer to the condition list that should be evaluated on
158 the target or NULL if the breakpoint is unconditional or
159 if GDB doesn't want us to evaluate the conditionals on the
160 target's side. */
161 struct point_cond_list *cond_list;
162
163 /* Point to the list of commands to run when this is hit. */
164 struct point_command_list *command_list;
165
166 /* Link to this breakpoint's raw breakpoint. This is always
167 non-NULL. */
168 struct raw_breakpoint *raw;
169
170 /* Function to call when we hit this breakpoint. If it returns 1,
171 the breakpoint shall be deleted; 0 or if this callback is NULL,
172 it will be left inserted. */
173 int (*handler) (CORE_ADDR);
174 };
175
176 /* See mem-break.h. */
177
178 enum target_hw_bp_type
179 raw_bkpt_type_to_target_hw_bp_type (enum raw_bkpt_type raw_type)
180 {
181 switch (raw_type)
182 {
183 case raw_bkpt_type_hw:
184 return hw_execute;
185 case raw_bkpt_type_write_wp:
186 return hw_write;
187 case raw_bkpt_type_read_wp:
188 return hw_read;
189 case raw_bkpt_type_access_wp:
190 return hw_access;
191 default:
192 fatal ("bad raw breakpoing type %d", (int) raw_type);
193 }
194 }
195
196 /* See mem-break.h. */
197
198 static enum bkpt_type
199 Z_packet_to_bkpt_type (char z_type)
200 {
201 gdb_assert ('0' <= z_type && z_type <= '4');
202
203 return gdb_breakpoint_Z0 + (z_type - '0');
204 }
205
206 /* See mem-break.h. */
207
208 enum raw_bkpt_type
209 Z_packet_to_raw_bkpt_type (char z_type)
210 {
211 switch (z_type)
212 {
213 case Z_PACKET_SW_BP:
214 return raw_bkpt_type_sw;
215 case Z_PACKET_HW_BP:
216 return raw_bkpt_type_hw;
217 case Z_PACKET_WRITE_WP:
218 return raw_bkpt_type_write_wp;
219 case Z_PACKET_READ_WP:
220 return raw_bkpt_type_read_wp;
221 case Z_PACKET_ACCESS_WP:
222 return raw_bkpt_type_access_wp;
223 default:
224 gdb_assert_not_reached ("unhandled Z packet type.");
225 }
226 }
227
228 int
229 any_persistent_commands ()
230 {
231 struct process_info *proc = current_process ();
232 struct breakpoint *bp;
233 struct point_command_list *cl;
234
235 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
236 {
237 for (cl = bp->command_list; cl != NULL; cl = cl->next)
238 if (cl->persistence)
239 return 1;
240 }
241
242 return 0;
243 }
244
245 /* Find low-level breakpoint of type TYPE at address ADDR that is not
246 insert-disabled. Returns NULL if not found. */
247
248 static struct raw_breakpoint *
249 find_enabled_raw_code_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type)
250 {
251 struct process_info *proc = current_process ();
252 struct raw_breakpoint *bp;
253
254 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
255 if (bp->pc == addr
256 && bp->raw_type == type
257 && bp->inserted >= 0)
258 return bp;
259
260 return NULL;
261 }
262
263 /* Find low-level breakpoint of type TYPE at address ADDR. Returns
264 NULL if not found. */
265
266 static struct raw_breakpoint *
267 find_raw_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type, int size)
268 {
269 struct process_info *proc = current_process ();
270 struct raw_breakpoint *bp;
271
272 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
273 if (bp->pc == addr && bp->raw_type == type && bp->size == size)
274 return bp;
275
276 return NULL;
277 }
278
279 /* See mem-break.h. */
280
281 int
282 insert_memory_breakpoint (struct raw_breakpoint *bp)
283 {
284 unsigned char buf[MAX_BREAKPOINT_LEN];
285 int err;
286
287 if (breakpoint_data == NULL)
288 return 1;
289
290 /* If the architecture treats the size field of Z packets as a
291 'kind' field, then we'll need to be able to know which is the
292 breakpoint instruction too. */
293 if (bp->size != breakpoint_len)
294 {
295 if (debug_threads)
296 debug_printf ("Don't know how to insert breakpoints of size %d.\n",
297 bp->size);
298 return -1;
299 }
300
301 /* Note that there can be fast tracepoint jumps installed in the
302 same memory range, so to get at the original memory, we need to
303 use read_inferior_memory, which masks those out. */
304 err = read_inferior_memory (bp->pc, buf, breakpoint_len);
305 if (err != 0)
306 {
307 if (debug_threads)
308 debug_printf ("Failed to read shadow memory of"
309 " breakpoint at 0x%s (%s).\n",
310 paddress (bp->pc), strerror (err));
311 }
312 else
313 {
314 memcpy (bp->old_data, buf, breakpoint_len);
315
316 err = (*the_target->write_memory) (bp->pc, breakpoint_data,
317 breakpoint_len);
318 if (err != 0)
319 {
320 if (debug_threads)
321 debug_printf ("Failed to insert breakpoint at 0x%s (%s).\n",
322 paddress (bp->pc), strerror (err));
323 }
324 }
325 return err != 0 ? -1 : 0;
326 }
327
328 /* See mem-break.h */
329
330 int
331 remove_memory_breakpoint (struct raw_breakpoint *bp)
332 {
333 unsigned char buf[MAX_BREAKPOINT_LEN];
334 int err;
335
336 /* Since there can be trap breakpoints inserted in the same address
337 range, we use `write_inferior_memory', which takes care of
338 layering breakpoints on top of fast tracepoints, and on top of
339 the buffer we pass it. This works because the caller has already
340 either unlinked the breakpoint or marked it uninserted. Also
341 note that we need to pass the current shadow contents, because
342 write_inferior_memory updates any shadow memory with what we pass
343 here, and we want that to be a nop. */
344 memcpy (buf, bp->old_data, breakpoint_len);
345 err = write_inferior_memory (bp->pc, buf, breakpoint_len);
346 if (err != 0)
347 {
348 if (debug_threads)
349 debug_printf ("Failed to uninsert raw breakpoint "
350 "at 0x%s (%s) while deleting it.\n",
351 paddress (bp->pc), strerror (err));
352 }
353 return err != 0 ? -1 : 0;
354 }
355
356 /* Set a RAW breakpoint of type TYPE and size SIZE at WHERE. On
357 success, a pointer to the new breakpoint is returned. On failure,
358 returns NULL and writes the error code to *ERR. */
359
360 static struct raw_breakpoint *
361 set_raw_breakpoint_at (enum raw_bkpt_type type, CORE_ADDR where, int size,
362 int *err)
363 {
364 struct process_info *proc = current_process ();
365 struct raw_breakpoint *bp;
366
367 if (type == raw_bkpt_type_sw || type == raw_bkpt_type_hw)
368 {
369 bp = find_enabled_raw_code_breakpoint_at (where, type);
370 if (bp != NULL && bp->size != size)
371 {
372 /* A different size than previously seen. The previous
373 breakpoint must be gone then. */
374 if (debug_threads)
375 debug_printf ("Inconsistent breakpoint size? Was %d, now %d.\n",
376 bp->size, size);
377 bp->inserted = -1;
378 bp = NULL;
379 }
380 }
381 else
382 bp = find_raw_breakpoint_at (where, type, size);
383
384 if (bp != NULL)
385 {
386 bp->refcount++;
387 return bp;
388 }
389
390 bp = xcalloc (1, sizeof (*bp));
391 bp->pc = where;
392 bp->size = size;
393 bp->refcount = 1;
394 bp->raw_type = type;
395
396 *err = the_target->insert_point (bp->raw_type, bp->pc, bp->size, bp);
397 if (*err != 0)
398 {
399 if (debug_threads)
400 debug_printf ("Failed to insert breakpoint at 0x%s (%d).\n",
401 paddress (where), *err);
402 free (bp);
403 return NULL;
404 }
405
406 bp->inserted = 1;
407 /* Link the breakpoint in. */
408 bp->next = proc->raw_breakpoints;
409 proc->raw_breakpoints = bp;
410 return bp;
411 }
412
413 /* Notice that breakpoint traps are always installed on top of fast
414 tracepoint jumps. This is even if the fast tracepoint is installed
415 at a later time compared to when the breakpoint was installed.
416 This means that a stopping breakpoint or tracepoint has higher
417 "priority". In turn, this allows having fast and slow tracepoints
418 (and breakpoints) at the same address behave correctly. */
419
420
421 /* A fast tracepoint jump. */
422
423 struct fast_tracepoint_jump
424 {
425 struct fast_tracepoint_jump *next;
426
427 /* A reference count. GDB can install more than one fast tracepoint
428 at the same address (each with its own action list, for
429 example). */
430 int refcount;
431
432 /* The fast tracepoint's insertion address. There can only be one
433 of these for a given PC. */
434 CORE_ADDR pc;
435
436 /* Non-zero if this fast tracepoint jump is currently inserted in
437 the inferior. */
438 int inserted;
439
440 /* The length of the jump instruction. */
441 int length;
442
443 /* A poor-man's flexible array member, holding both the jump
444 instruction to insert, and a copy of the instruction that would
445 be in memory had not been a jump there (the shadow memory of the
446 tracepoint jump). */
447 unsigned char insn_and_shadow[0];
448 };
449
450 /* Fast tracepoint FP's jump instruction to insert. */
451 #define fast_tracepoint_jump_insn(fp) \
452 ((fp)->insn_and_shadow + 0)
453
454 /* The shadow memory of fast tracepoint jump FP. */
455 #define fast_tracepoint_jump_shadow(fp) \
456 ((fp)->insn_and_shadow + (fp)->length)
457
458
459 /* Return the fast tracepoint jump set at WHERE. */
460
461 static struct fast_tracepoint_jump *
462 find_fast_tracepoint_jump_at (CORE_ADDR where)
463 {
464 struct process_info *proc = current_process ();
465 struct fast_tracepoint_jump *jp;
466
467 for (jp = proc->fast_tracepoint_jumps; jp != NULL; jp = jp->next)
468 if (jp->pc == where)
469 return jp;
470
471 return NULL;
472 }
473
474 int
475 fast_tracepoint_jump_here (CORE_ADDR where)
476 {
477 struct fast_tracepoint_jump *jp = find_fast_tracepoint_jump_at (where);
478
479 return (jp != NULL);
480 }
481
482 int
483 delete_fast_tracepoint_jump (struct fast_tracepoint_jump *todel)
484 {
485 struct fast_tracepoint_jump *bp, **bp_link;
486 int ret;
487 struct process_info *proc = current_process ();
488
489 bp = proc->fast_tracepoint_jumps;
490 bp_link = &proc->fast_tracepoint_jumps;
491
492 while (bp)
493 {
494 if (bp == todel)
495 {
496 if (--bp->refcount == 0)
497 {
498 struct fast_tracepoint_jump *prev_bp_link = *bp_link;
499 unsigned char *buf;
500
501 /* Unlink it. */
502 *bp_link = bp->next;
503
504 /* Since there can be breakpoints inserted in the same
505 address range, we use `write_inferior_memory', which
506 takes care of layering breakpoints on top of fast
507 tracepoints, and on top of the buffer we pass it.
508 This works because we've already unlinked the fast
509 tracepoint jump above. Also note that we need to
510 pass the current shadow contents, because
511 write_inferior_memory updates any shadow memory with
512 what we pass here, and we want that to be a nop. */
513 buf = alloca (bp->length);
514 memcpy (buf, fast_tracepoint_jump_shadow (bp), bp->length);
515 ret = write_inferior_memory (bp->pc, buf, bp->length);
516 if (ret != 0)
517 {
518 /* Something went wrong, relink the jump. */
519 *bp_link = prev_bp_link;
520
521 if (debug_threads)
522 debug_printf ("Failed to uninsert fast tracepoint jump "
523 "at 0x%s (%s) while deleting it.\n",
524 paddress (bp->pc), strerror (ret));
525 return ret;
526 }
527
528 free (bp);
529 }
530
531 return 0;
532 }
533 else
534 {
535 bp_link = &bp->next;
536 bp = *bp_link;
537 }
538 }
539
540 warning ("Could not find fast tracepoint jump in list.");
541 return ENOENT;
542 }
543
544 void
545 inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump *jp)
546 {
547 jp->refcount++;
548 }
549
550 struct fast_tracepoint_jump *
551 set_fast_tracepoint_jump (CORE_ADDR where,
552 unsigned char *insn, ULONGEST length)
553 {
554 struct process_info *proc = current_process ();
555 struct fast_tracepoint_jump *jp;
556 int err;
557 unsigned char *buf;
558
559 /* We refcount fast tracepoint jumps. Check if we already know
560 about a jump at this address. */
561 jp = find_fast_tracepoint_jump_at (where);
562 if (jp != NULL)
563 {
564 jp->refcount++;
565 return jp;
566 }
567
568 /* We don't, so create a new object. Double the length, because the
569 flexible array member holds both the jump insn, and the
570 shadow. */
571 jp = xcalloc (1, sizeof (*jp) + (length * 2));
572 jp->pc = where;
573 jp->length = length;
574 memcpy (fast_tracepoint_jump_insn (jp), insn, length);
575 jp->refcount = 1;
576 buf = alloca (length);
577
578 /* Note that there can be trap breakpoints inserted in the same
579 address range. To access the original memory contents, we use
580 `read_inferior_memory', which masks out breakpoints. */
581 err = read_inferior_memory (where, buf, length);
582 if (err != 0)
583 {
584 if (debug_threads)
585 debug_printf ("Failed to read shadow memory of"
586 " fast tracepoint at 0x%s (%s).\n",
587 paddress (where), strerror (err));
588 free (jp);
589 return NULL;
590 }
591 memcpy (fast_tracepoint_jump_shadow (jp), buf, length);
592
593 /* Link the jump in. */
594 jp->inserted = 1;
595 jp->next = proc->fast_tracepoint_jumps;
596 proc->fast_tracepoint_jumps = jp;
597
598 /* Since there can be trap breakpoints inserted in the same address
599 range, we use use `write_inferior_memory', which takes care of
600 layering breakpoints on top of fast tracepoints, on top of the
601 buffer we pass it. This works because we've already linked in
602 the fast tracepoint jump above. Also note that we need to pass
603 the current shadow contents, because write_inferior_memory
604 updates any shadow memory with what we pass here, and we want
605 that to be a nop. */
606 err = write_inferior_memory (where, buf, length);
607 if (err != 0)
608 {
609 if (debug_threads)
610 debug_printf ("Failed to insert fast tracepoint jump at 0x%s (%s).\n",
611 paddress (where), strerror (err));
612
613 /* Unlink it. */
614 proc->fast_tracepoint_jumps = jp->next;
615 free (jp);
616
617 return NULL;
618 }
619
620 return jp;
621 }
622
623 void
624 uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc)
625 {
626 struct fast_tracepoint_jump *jp;
627 int err;
628
629 jp = find_fast_tracepoint_jump_at (pc);
630 if (jp == NULL)
631 {
632 /* This can happen when we remove all breakpoints while handling
633 a step-over. */
634 if (debug_threads)
635 debug_printf ("Could not find fast tracepoint jump at 0x%s "
636 "in list (uninserting).\n",
637 paddress (pc));
638 return;
639 }
640
641 if (jp->inserted)
642 {
643 unsigned char *buf;
644
645 jp->inserted = 0;
646
647 /* Since there can be trap breakpoints inserted in the same
648 address range, we use use `write_inferior_memory', which
649 takes care of layering breakpoints on top of fast
650 tracepoints, and on top of the buffer we pass it. This works
651 because we've already marked the fast tracepoint fast
652 tracepoint jump uninserted above. Also note that we need to
653 pass the current shadow contents, because
654 write_inferior_memory updates any shadow memory with what we
655 pass here, and we want that to be a nop. */
656 buf = alloca (jp->length);
657 memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
658 err = write_inferior_memory (jp->pc, buf, jp->length);
659 if (err != 0)
660 {
661 jp->inserted = 1;
662
663 if (debug_threads)
664 debug_printf ("Failed to uninsert fast tracepoint jump at"
665 " 0x%s (%s).\n",
666 paddress (pc), strerror (err));
667 }
668 }
669 }
670
671 void
672 reinsert_fast_tracepoint_jumps_at (CORE_ADDR where)
673 {
674 struct fast_tracepoint_jump *jp;
675 int err;
676 unsigned char *buf;
677
678 jp = find_fast_tracepoint_jump_at (where);
679 if (jp == NULL)
680 {
681 /* This can happen when we remove breakpoints when a tracepoint
682 hit causes a tracing stop, while handling a step-over. */
683 if (debug_threads)
684 debug_printf ("Could not find fast tracepoint jump at 0x%s "
685 "in list (reinserting).\n",
686 paddress (where));
687 return;
688 }
689
690 if (jp->inserted)
691 error ("Jump already inserted at reinsert time.");
692
693 jp->inserted = 1;
694
695 /* Since there can be trap breakpoints inserted in the same address
696 range, we use `write_inferior_memory', which takes care of
697 layering breakpoints on top of fast tracepoints, and on top of
698 the buffer we pass it. This works because we've already marked
699 the fast tracepoint jump inserted above. Also note that we need
700 to pass the current shadow contents, because
701 write_inferior_memory updates any shadow memory with what we pass
702 here, and we want that to be a nop. */
703 buf = alloca (jp->length);
704 memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
705 err = write_inferior_memory (where, buf, jp->length);
706 if (err != 0)
707 {
708 jp->inserted = 0;
709
710 if (debug_threads)
711 debug_printf ("Failed to reinsert fast tracepoint jump at"
712 " 0x%s (%s).\n",
713 paddress (where), strerror (err));
714 }
715 }
716
717 /* Set a high-level breakpoint of type TYPE, with low level type
718 RAW_TYPE and size SIZE, at WHERE. On success, a pointer to the new
719 breakpoint is returned. On failure, returns NULL and writes the
720 error code to *ERR. HANDLER is called when the breakpoint is hit.
721 HANDLER should return 1 if the breakpoint should be deleted, 0
722 otherwise. */
723
724 static struct breakpoint *
725 set_breakpoint (enum bkpt_type type, enum raw_bkpt_type raw_type,
726 CORE_ADDR where, int size,
727 int (*handler) (CORE_ADDR), int *err)
728 {
729 struct process_info *proc = current_process ();
730 struct breakpoint *bp;
731 struct raw_breakpoint *raw;
732
733 raw = set_raw_breakpoint_at (raw_type, where, size, err);
734
735 if (raw == NULL)
736 {
737 /* warn? */
738 return NULL;
739 }
740
741 bp = xcalloc (1, sizeof (struct breakpoint));
742 bp->type = type;
743
744 bp->raw = raw;
745 bp->handler = handler;
746
747 bp->next = proc->breakpoints;
748 proc->breakpoints = bp;
749
750 return bp;
751 }
752
753 /* See mem-break.h */
754
755 struct breakpoint *
756 set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
757 {
758 int err_ignored;
759
760 return set_breakpoint (other_breakpoint, raw_bkpt_type_sw,
761 where, breakpoint_len, handler,
762 &err_ignored);
763 }
764
765
766 static int
767 delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
768 {
769 struct raw_breakpoint *bp, **bp_link;
770 int ret;
771
772 bp = proc->raw_breakpoints;
773 bp_link = &proc->raw_breakpoints;
774
775 while (bp)
776 {
777 if (bp == todel)
778 {
779 if (bp->inserted > 0)
780 {
781 struct raw_breakpoint *prev_bp_link = *bp_link;
782
783 *bp_link = bp->next;
784
785 ret = the_target->remove_point (bp->raw_type, bp->pc, bp->size,
786 bp);
787 if (ret != 0)
788 {
789 /* Something went wrong, relink the breakpoint. */
790 *bp_link = prev_bp_link;
791
792 if (debug_threads)
793 debug_printf ("Failed to uninsert raw breakpoint "
794 "at 0x%s while deleting it.\n",
795 paddress (bp->pc));
796 return ret;
797 }
798 }
799 else
800 *bp_link = bp->next;
801
802 free (bp);
803 return 0;
804 }
805 else
806 {
807 bp_link = &bp->next;
808 bp = *bp_link;
809 }
810 }
811
812 warning ("Could not find raw breakpoint in list.");
813 return ENOENT;
814 }
815
816 static int
817 release_breakpoint (struct process_info *proc, struct breakpoint *bp)
818 {
819 int newrefcount;
820 int ret;
821
822 newrefcount = bp->raw->refcount - 1;
823 if (newrefcount == 0)
824 {
825 ret = delete_raw_breakpoint (proc, bp->raw);
826 if (ret != 0)
827 return ret;
828 }
829 else
830 bp->raw->refcount = newrefcount;
831
832 free (bp);
833
834 return 0;
835 }
836
837 static int
838 delete_breakpoint_1 (struct process_info *proc, struct breakpoint *todel)
839 {
840 struct breakpoint *bp, **bp_link;
841 int err;
842
843 bp = proc->breakpoints;
844 bp_link = &proc->breakpoints;
845
846 while (bp)
847 {
848 if (bp == todel)
849 {
850 *bp_link = bp->next;
851
852 err = release_breakpoint (proc, bp);
853 if (err != 0)
854 return err;
855
856 bp = *bp_link;
857 return 0;
858 }
859 else
860 {
861 bp_link = &bp->next;
862 bp = *bp_link;
863 }
864 }
865
866 warning ("Could not find breakpoint in list.");
867 return ENOENT;
868 }
869
870 int
871 delete_breakpoint (struct breakpoint *todel)
872 {
873 struct process_info *proc = current_process ();
874 return delete_breakpoint_1 (proc, todel);
875 }
876
877 /* Locate a GDB breakpoint of type Z_TYPE and size SIZE placed at
878 address ADDR and return a pointer to its structure. If SIZE is -1,
879 the breakpoints' sizes are ignored. */
880
881 static struct breakpoint *
882 find_gdb_breakpoint (char z_type, CORE_ADDR addr, int size)
883 {
884 struct process_info *proc = current_process ();
885 struct breakpoint *bp;
886 enum bkpt_type type = Z_packet_to_bkpt_type (z_type);
887
888 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
889 if (bp->type == type && bp->raw->pc == addr
890 && (size == -1 || bp->raw->size == size))
891 return bp;
892
893 return NULL;
894 }
895
896 static int
897 z_type_supported (char z_type)
898 {
899 return (z_type >= '0' && z_type <= '4'
900 && the_target->supports_z_point_type (z_type));
901 }
902
903 /* Create a new GDB breakpoint of type Z_TYPE at ADDR with size SIZE.
904 Returns a pointer to the newly created breakpoint on success. On
905 failure returns NULL and sets *ERR to either -1 for error, or 1 if
906 Z_TYPE breakpoints are not supported on this target. */
907
908 static struct breakpoint *
909 set_gdb_breakpoint_1 (char z_type, CORE_ADDR addr, int size, int *err)
910 {
911 struct breakpoint *bp;
912 enum bkpt_type type;
913 enum raw_bkpt_type raw_type;
914
915 /* If we see GDB inserting a second code breakpoint at the same
916 address, then either: GDB is updating the breakpoint's conditions
917 or commands; or, the first breakpoint must have disappeared due
918 to a shared library unload. On targets where the shared
919 libraries are handled by userspace, like SVR4, for example,
920 GDBserver can't tell if a library was loaded or unloaded. Since
921 we refcount raw breakpoints, we must be careful to make sure GDB
922 breakpoints never contribute more than one reference. if we
923 didn't do this, in case the previous breakpoint is gone due to a
924 shared library unload, we'd just increase the refcount of the
925 previous breakpoint at this address, but the trap was not planted
926 in the inferior anymore, thus the breakpoint would never be hit.
927 Note this must be careful to not create a window where
928 breakpoints are removed from the target, for non-stop, in case
929 the target can poke at memory while the program is running. */
930 if (z_type == Z_PACKET_SW_BP
931 || z_type == Z_PACKET_HW_BP)
932 {
933 bp = find_gdb_breakpoint (z_type, addr, -1);
934
935 if (bp != NULL)
936 {
937 if (bp->raw->size != size)
938 {
939 /* A different size than previously seen. The previous
940 breakpoint must be gone then. */
941 bp->raw->inserted = -1;
942 delete_breakpoint (bp);
943 bp = NULL;
944 }
945 else if (z_type == Z_PACKET_SW_BP)
946 {
947 /* Check if the breakpoint is actually gone from the
948 target, due to an solib unload, for example. Might
949 as well validate _all_ breakpoints. */
950 validate_breakpoints ();
951
952 /* Breakpoints that don't pass validation are
953 deleted. */
954 bp = find_gdb_breakpoint (z_type, addr, -1);
955 }
956 }
957 }
958 else
959 {
960 /* Data breakpoints for the same address but different size are
961 expected. GDB doesn't merge these. The backend gets to do
962 that if it wants/can. */
963 bp = find_gdb_breakpoint (z_type, addr, size);
964 }
965
966 if (bp != NULL)
967 {
968 /* We already know about this breakpoint, there's nothing else
969 to do - GDB's reference is already accounted for. Note that
970 whether the breakpoint inserted is left as is - we may be
971 stepping over it, for example, in which case we don't want to
972 force-reinsert it. */
973 return bp;
974 }
975
976 raw_type = Z_packet_to_raw_bkpt_type (z_type);
977 type = Z_packet_to_bkpt_type (z_type);
978 return set_breakpoint (type, raw_type, addr, size, NULL, err);
979 }
980
981 static int
982 check_gdb_bp_preconditions (char z_type, int *err)
983 {
984 /* As software/memory breakpoints work by poking at memory, we need
985 to prepare to access memory. If that operation fails, we need to
986 return error. Seeing an error, if this is the first breakpoint
987 of that type that GDB tries to insert, GDB would then assume the
988 breakpoint type is supported, but it may actually not be. So we
989 need to check whether the type is supported at all before
990 preparing to access memory. */
991 if (!z_type_supported (z_type))
992 {
993 *err = 1;
994 return 0;
995 }
996 else if (current_inferior == NULL)
997 {
998 *err = -1;
999 return 0;
1000 }
1001 else
1002 return 1;
1003 }
1004
1005 /* See mem-break.h. This is a wrapper for set_gdb_breakpoint_1 that
1006 knows to prepare to access memory for Z0 breakpoints. */
1007
1008 struct breakpoint *
1009 set_gdb_breakpoint (char z_type, CORE_ADDR addr, int size, int *err)
1010 {
1011 struct breakpoint *bp;
1012
1013 if (!check_gdb_bp_preconditions (z_type, err))
1014 return NULL;
1015
1016 /* If inserting a software/memory breakpoint, need to prepare to
1017 access memory. */
1018 if (z_type == Z_PACKET_SW_BP)
1019 {
1020 *err = prepare_to_access_memory ();
1021 if (*err != 0)
1022 return NULL;
1023 }
1024
1025 bp = set_gdb_breakpoint_1 (z_type, addr, size, err);
1026
1027 if (z_type == Z_PACKET_SW_BP)
1028 done_accessing_memory ();
1029
1030 return bp;
1031 }
1032
1033 /* Delete a GDB breakpoint of type Z_TYPE and size SIZE previously
1034 inserted at ADDR with set_gdb_breakpoint_at. Returns 0 on success,
1035 -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
1036 target. */
1037
1038 static int
1039 delete_gdb_breakpoint_1 (char z_type, CORE_ADDR addr, int size)
1040 {
1041 struct breakpoint *bp;
1042 int err;
1043
1044 bp = find_gdb_breakpoint (z_type, addr, size);
1045 if (bp == NULL)
1046 return -1;
1047
1048 /* Before deleting the breakpoint, make sure to free its condition
1049 and command lists. */
1050 clear_breakpoint_conditions_and_commands (bp);
1051 err = delete_breakpoint (bp);
1052 if (err != 0)
1053 return -1;
1054
1055 return 0;
1056 }
1057
1058 /* See mem-break.h. This is a wrapper for delete_gdb_breakpoint that
1059 knows to prepare to access memory for Z0 breakpoints. */
1060
1061 int
1062 delete_gdb_breakpoint (char z_type, CORE_ADDR addr, int size)
1063 {
1064 int ret;
1065
1066 if (!check_gdb_bp_preconditions (z_type, &ret))
1067 return ret;
1068
1069 /* If inserting a software/memory breakpoint, need to prepare to
1070 access memory. */
1071 if (z_type == Z_PACKET_SW_BP)
1072 {
1073 int err;
1074
1075 err = prepare_to_access_memory ();
1076 if (err != 0)
1077 return -1;
1078 }
1079
1080 ret = delete_gdb_breakpoint_1 (z_type, addr, size);
1081
1082 if (z_type == Z_PACKET_SW_BP)
1083 done_accessing_memory ();
1084
1085 return ret;
1086 }
1087
1088 /* Clear all conditions associated with a breakpoint. */
1089
1090 static void
1091 clear_breakpoint_conditions (struct breakpoint *bp)
1092 {
1093 struct point_cond_list *cond;
1094
1095 if (bp->cond_list == NULL)
1096 return;
1097
1098 cond = bp->cond_list;
1099
1100 while (cond != NULL)
1101 {
1102 struct point_cond_list *cond_next;
1103
1104 cond_next = cond->next;
1105 gdb_free_agent_expr (cond->cond);
1106 free (cond);
1107 cond = cond_next;
1108 }
1109
1110 bp->cond_list = NULL;
1111 }
1112
1113 /* Clear all commands associated with a breakpoint. */
1114
1115 static void
1116 clear_breakpoint_commands (struct breakpoint *bp)
1117 {
1118 struct point_command_list *cmd;
1119
1120 if (bp->command_list == NULL)
1121 return;
1122
1123 cmd = bp->command_list;
1124
1125 while (cmd != NULL)
1126 {
1127 struct point_command_list *cmd_next;
1128
1129 cmd_next = cmd->next;
1130 gdb_free_agent_expr (cmd->cmd);
1131 free (cmd);
1132 cmd = cmd_next;
1133 }
1134
1135 bp->command_list = NULL;
1136 }
1137
1138 void
1139 clear_breakpoint_conditions_and_commands (struct breakpoint *bp)
1140 {
1141 clear_breakpoint_conditions (bp);
1142 clear_breakpoint_commands (bp);
1143 }
1144
1145 /* Add condition CONDITION to GDBserver's breakpoint BP. */
1146
1147 static void
1148 add_condition_to_breakpoint (struct breakpoint *bp,
1149 struct agent_expr *condition)
1150 {
1151 struct point_cond_list *new_cond;
1152
1153 /* Create new condition. */
1154 new_cond = xcalloc (1, sizeof (*new_cond));
1155 new_cond->cond = condition;
1156
1157 /* Add condition to the list. */
1158 new_cond->next = bp->cond_list;
1159 bp->cond_list = new_cond;
1160 }
1161
1162 /* Add a target-side condition CONDITION to a breakpoint. */
1163
1164 int
1165 add_breakpoint_condition (struct breakpoint *bp, char **condition)
1166 {
1167 char *actparm = *condition;
1168 struct agent_expr *cond;
1169
1170 if (condition == NULL)
1171 return 1;
1172
1173 if (bp == NULL)
1174 return 0;
1175
1176 cond = gdb_parse_agent_expr (&actparm);
1177
1178 if (cond == NULL)
1179 {
1180 fprintf (stderr, "Condition evaluation failed. "
1181 "Assuming unconditional.\n");
1182 return 0;
1183 }
1184
1185 add_condition_to_breakpoint (bp, cond);
1186
1187 *condition = actparm;
1188
1189 return 1;
1190 }
1191
1192 /* Evaluate condition (if any) at breakpoint BP. Return 1 if
1193 true and 0 otherwise. */
1194
1195 static int
1196 gdb_condition_true_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
1197 {
1198 /* Fetch registers for the current inferior. */
1199 struct breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1200 ULONGEST value = 0;
1201 struct point_cond_list *cl;
1202 int err = 0;
1203 struct eval_agent_expr_context ctx;
1204
1205 if (bp == NULL)
1206 return 0;
1207
1208 /* Check if the breakpoint is unconditional. If it is,
1209 the condition always evaluates to TRUE. */
1210 if (bp->cond_list == NULL)
1211 return 1;
1212
1213 ctx.regcache = get_thread_regcache (current_inferior, 1);
1214 ctx.tframe = NULL;
1215 ctx.tpoint = NULL;
1216
1217 /* Evaluate each condition in the breakpoint's list of conditions.
1218 Return true if any of the conditions evaluates to TRUE.
1219
1220 If we failed to evaluate the expression, TRUE is returned. This
1221 forces GDB to reevaluate the conditions. */
1222 for (cl = bp->cond_list;
1223 cl && !value && !err; cl = cl->next)
1224 {
1225 /* Evaluate the condition. */
1226 err = gdb_eval_agent_expr (&ctx, cl->cond, &value);
1227 }
1228
1229 if (err)
1230 return 1;
1231
1232 return (value != 0);
1233 }
1234
1235 int
1236 gdb_condition_true_at_breakpoint (CORE_ADDR where)
1237 {
1238 /* Only check code (software or hardware) breakpoints. */
1239 return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
1240 || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
1241 }
1242
1243 /* Add commands COMMANDS to GDBserver's breakpoint BP. */
1244
1245 void
1246 add_commands_to_breakpoint (struct breakpoint *bp,
1247 struct agent_expr *commands, int persist)
1248 {
1249 struct point_command_list *new_cmd;
1250
1251 /* Create new command. */
1252 new_cmd = xcalloc (1, sizeof (*new_cmd));
1253 new_cmd->cmd = commands;
1254 new_cmd->persistence = persist;
1255
1256 /* Add commands to the list. */
1257 new_cmd->next = bp->command_list;
1258 bp->command_list = new_cmd;
1259 }
1260
1261 /* Add a target-side command COMMAND to the breakpoint at ADDR. */
1262
1263 int
1264 add_breakpoint_commands (struct breakpoint *bp, char **command,
1265 int persist)
1266 {
1267 char *actparm = *command;
1268 struct agent_expr *cmd;
1269
1270 if (command == NULL)
1271 return 1;
1272
1273 if (bp == NULL)
1274 return 0;
1275
1276 cmd = gdb_parse_agent_expr (&actparm);
1277
1278 if (cmd == NULL)
1279 {
1280 fprintf (stderr, "Command evaluation failed. "
1281 "Disabling.\n");
1282 return 0;
1283 }
1284
1285 add_commands_to_breakpoint (bp, cmd, persist);
1286
1287 *command = actparm;
1288
1289 return 1;
1290 }
1291
1292 /* Return true if there are no commands to run at this location,
1293 which likely means we want to report back to GDB. */
1294
1295 static int
1296 gdb_no_commands_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
1297 {
1298 struct breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1299
1300 if (bp == NULL)
1301 return 1;
1302
1303 if (debug_threads)
1304 debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s\n",
1305 paddress (addr), z_type,
1306 phex_nz ((uintptr_t) bp->command_list, 0));
1307 return (bp->command_list == NULL);
1308 }
1309
1310 /* Return true if there are no commands to run at this location,
1311 which likely means we want to report back to GDB. */
1312
1313 int
1314 gdb_no_commands_at_breakpoint (CORE_ADDR where)
1315 {
1316 /* Only check code (software or hardware) breakpoints. */
1317 return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
1318 && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
1319 }
1320
1321 /* Run a breakpoint's commands. Returns 0 if there was a problem
1322 running any command, 1 otherwise. */
1323
1324 static int
1325 run_breakpoint_commands_z_type (char z_type, CORE_ADDR addr)
1326 {
1327 /* Fetch registers for the current inferior. */
1328 struct breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1329 ULONGEST value = 0;
1330 struct point_command_list *cl;
1331 int err = 0;
1332 struct eval_agent_expr_context ctx;
1333
1334 if (bp == NULL)
1335 return 1;
1336
1337 ctx.regcache = get_thread_regcache (current_inferior, 1);
1338 ctx.tframe = NULL;
1339 ctx.tpoint = NULL;
1340
1341 for (cl = bp->command_list;
1342 cl && !value && !err; cl = cl->next)
1343 {
1344 /* Run the command. */
1345 err = gdb_eval_agent_expr (&ctx, cl->cmd, &value);
1346
1347 /* If one command has a problem, stop digging the hole deeper. */
1348 if (err)
1349 return 0;
1350 }
1351
1352 return 1;
1353 }
1354
1355 void
1356 run_breakpoint_commands (CORE_ADDR where)
1357 {
1358 /* Only check code (software or hardware) breakpoints. If one
1359 command has a problem, stop digging the hole deeper. */
1360 if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP, where))
1361 run_breakpoint_commands_z_type (Z_PACKET_HW_BP, where);
1362 }
1363
1364 /* See mem-break.h. */
1365
1366 int
1367 gdb_breakpoint_here (CORE_ADDR where)
1368 {
1369 /* Only check code (software or hardware) breakpoints. */
1370 return (find_gdb_breakpoint (Z_PACKET_SW_BP, where, -1) != NULL
1371 || find_gdb_breakpoint (Z_PACKET_HW_BP, where, -1) != NULL);
1372 }
1373
1374 void
1375 set_reinsert_breakpoint (CORE_ADDR stop_at)
1376 {
1377 struct breakpoint *bp;
1378
1379 bp = set_breakpoint_at (stop_at, NULL);
1380 bp->type = reinsert_breakpoint;
1381 }
1382
1383 void
1384 delete_reinsert_breakpoints (void)
1385 {
1386 struct process_info *proc = current_process ();
1387 struct breakpoint *bp, **bp_link;
1388
1389 bp = proc->breakpoints;
1390 bp_link = &proc->breakpoints;
1391
1392 while (bp)
1393 {
1394 if (bp->type == reinsert_breakpoint)
1395 {
1396 *bp_link = bp->next;
1397 release_breakpoint (proc, bp);
1398 bp = *bp_link;
1399 }
1400 else
1401 {
1402 bp_link = &bp->next;
1403 bp = *bp_link;
1404 }
1405 }
1406 }
1407
1408 static void
1409 uninsert_raw_breakpoint (struct raw_breakpoint *bp)
1410 {
1411 if (bp->inserted < 0)
1412 {
1413 if (debug_threads)
1414 debug_printf ("Breakpoint at %s is marked insert-disabled.\n",
1415 paddress (bp->pc));
1416 }
1417 else if (bp->inserted > 0)
1418 {
1419 int err;
1420
1421 bp->inserted = 0;
1422
1423 err = the_target->remove_point (bp->raw_type, bp->pc, bp->size, bp);
1424 if (err != 0)
1425 {
1426 bp->inserted = 1;
1427
1428 if (debug_threads)
1429 debug_printf ("Failed to uninsert raw breakpoint at 0x%s.\n",
1430 paddress (bp->pc));
1431 }
1432 }
1433 }
1434
1435 void
1436 uninsert_breakpoints_at (CORE_ADDR pc)
1437 {
1438 struct process_info *proc = current_process ();
1439 struct raw_breakpoint *bp;
1440 int found = 0;
1441
1442 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1443 if ((bp->raw_type == raw_bkpt_type_sw
1444 || bp->raw_type == raw_bkpt_type_hw)
1445 && bp->pc == pc)
1446 {
1447 found = 1;
1448
1449 if (bp->inserted)
1450 uninsert_raw_breakpoint (bp);
1451 }
1452
1453 if (!found)
1454 {
1455 /* This can happen when we remove all breakpoints while handling
1456 a step-over. */
1457 if (debug_threads)
1458 debug_printf ("Could not find breakpoint at 0x%s "
1459 "in list (uninserting).\n",
1460 paddress (pc));
1461 }
1462 }
1463
1464 void
1465 uninsert_all_breakpoints (void)
1466 {
1467 struct process_info *proc = current_process ();
1468 struct raw_breakpoint *bp;
1469
1470 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1471 if ((bp->raw_type == raw_bkpt_type_sw
1472 || bp->raw_type == raw_bkpt_type_hw)
1473 && bp->inserted)
1474 uninsert_raw_breakpoint (bp);
1475 }
1476
1477 static void
1478 reinsert_raw_breakpoint (struct raw_breakpoint *bp)
1479 {
1480 int err;
1481
1482 if (bp->inserted)
1483 error ("Breakpoint already inserted at reinsert time.");
1484
1485 err = the_target->insert_point (bp->raw_type, bp->pc, bp->size, bp);
1486 if (err == 0)
1487 bp->inserted = 1;
1488 else if (debug_threads)
1489 debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).\n",
1490 paddress (bp->pc), err);
1491 }
1492
1493 void
1494 reinsert_breakpoints_at (CORE_ADDR pc)
1495 {
1496 struct process_info *proc = current_process ();
1497 struct raw_breakpoint *bp;
1498 int found = 0;
1499
1500 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1501 if ((bp->raw_type == raw_bkpt_type_sw
1502 || bp->raw_type == raw_bkpt_type_hw)
1503 && bp->pc == pc)
1504 {
1505 found = 1;
1506
1507 reinsert_raw_breakpoint (bp);
1508 }
1509
1510 if (!found)
1511 {
1512 /* This can happen when we remove all breakpoints while handling
1513 a step-over. */
1514 if (debug_threads)
1515 debug_printf ("Could not find raw breakpoint at 0x%s "
1516 "in list (reinserting).\n",
1517 paddress (pc));
1518 }
1519 }
1520
1521 void
1522 reinsert_all_breakpoints (void)
1523 {
1524 struct process_info *proc = current_process ();
1525 struct raw_breakpoint *bp;
1526
1527 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1528 if ((bp->raw_type == raw_bkpt_type_sw
1529 || bp->raw_type == raw_bkpt_type_hw)
1530 && !bp->inserted)
1531 reinsert_raw_breakpoint (bp);
1532 }
1533
1534 void
1535 check_breakpoints (CORE_ADDR stop_pc)
1536 {
1537 struct process_info *proc = current_process ();
1538 struct breakpoint *bp, **bp_link;
1539
1540 bp = proc->breakpoints;
1541 bp_link = &proc->breakpoints;
1542
1543 while (bp)
1544 {
1545 struct raw_breakpoint *raw = bp->raw;
1546
1547 if ((raw->raw_type == raw_bkpt_type_sw
1548 || raw->raw_type == raw_bkpt_type_hw)
1549 && raw->pc == stop_pc)
1550 {
1551 if (!raw->inserted)
1552 {
1553 warning ("Hit a removed breakpoint?");
1554 return;
1555 }
1556
1557 if (bp->handler != NULL && (*bp->handler) (stop_pc))
1558 {
1559 *bp_link = bp->next;
1560
1561 release_breakpoint (proc, bp);
1562
1563 bp = *bp_link;
1564 continue;
1565 }
1566 }
1567
1568 bp_link = &bp->next;
1569 bp = *bp_link;
1570 }
1571 }
1572
1573 void
1574 set_breakpoint_data (const unsigned char *bp_data, int bp_len)
1575 {
1576 breakpoint_data = bp_data;
1577 breakpoint_len = bp_len;
1578 }
1579
1580 int
1581 breakpoint_here (CORE_ADDR addr)
1582 {
1583 struct process_info *proc = current_process ();
1584 struct raw_breakpoint *bp;
1585
1586 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1587 if ((bp->raw_type == raw_bkpt_type_sw
1588 || bp->raw_type == raw_bkpt_type_hw)
1589 && bp->pc == addr)
1590 return 1;
1591
1592 return 0;
1593 }
1594
1595 int
1596 breakpoint_inserted_here (CORE_ADDR addr)
1597 {
1598 struct process_info *proc = current_process ();
1599 struct raw_breakpoint *bp;
1600
1601 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1602 if ((bp->raw_type == raw_bkpt_type_sw
1603 || bp->raw_type == raw_bkpt_type_hw)
1604 && bp->pc == addr
1605 && bp->inserted)
1606 return 1;
1607
1608 return 0;
1609 }
1610
1611 static int
1612 validate_inserted_breakpoint (struct raw_breakpoint *bp)
1613 {
1614 unsigned char *buf;
1615 int err;
1616
1617 gdb_assert (bp->inserted);
1618 gdb_assert (bp->raw_type == raw_bkpt_type_sw);
1619
1620 buf = alloca (breakpoint_len);
1621 err = (*the_target->read_memory) (bp->pc, buf, breakpoint_len);
1622 if (err || memcmp (buf, breakpoint_data, breakpoint_len) != 0)
1623 {
1624 /* Tag it as gone. */
1625 bp->inserted = -1;
1626 return 0;
1627 }
1628
1629 return 1;
1630 }
1631
1632 static void
1633 delete_disabled_breakpoints (void)
1634 {
1635 struct process_info *proc = current_process ();
1636 struct breakpoint *bp, *next;
1637
1638 for (bp = proc->breakpoints; bp != NULL; bp = next)
1639 {
1640 next = bp->next;
1641 if (bp->raw->inserted < 0)
1642 delete_breakpoint_1 (proc, bp);
1643 }
1644 }
1645
1646 /* Check if breakpoints we inserted still appear to be inserted. They
1647 may disappear due to a shared library unload, and worse, a new
1648 shared library may be reloaded at the same address as the
1649 previously unloaded one. If that happens, we should make sure that
1650 the shadow memory of the old breakpoints isn't used when reading or
1651 writing memory. */
1652
1653 void
1654 validate_breakpoints (void)
1655 {
1656 struct process_info *proc = current_process ();
1657 struct breakpoint *bp;
1658
1659 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1660 {
1661 struct raw_breakpoint *raw = bp->raw;
1662
1663 if (raw->raw_type == raw_bkpt_type_sw && raw->inserted > 0)
1664 validate_inserted_breakpoint (raw);
1665 }
1666
1667 delete_disabled_breakpoints ();
1668 }
1669
1670 void
1671 check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
1672 {
1673 struct process_info *proc = current_process ();
1674 struct raw_breakpoint *bp = proc->raw_breakpoints;
1675 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1676 CORE_ADDR mem_end = mem_addr + mem_len;
1677 int disabled_one = 0;
1678
1679 for (; jp != NULL; jp = jp->next)
1680 {
1681 CORE_ADDR bp_end = jp->pc + jp->length;
1682 CORE_ADDR start, end;
1683 int copy_offset, copy_len, buf_offset;
1684
1685 gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len
1686 || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1687
1688 if (mem_addr >= bp_end)
1689 continue;
1690 if (jp->pc >= mem_end)
1691 continue;
1692
1693 start = jp->pc;
1694 if (mem_addr > start)
1695 start = mem_addr;
1696
1697 end = bp_end;
1698 if (end > mem_end)
1699 end = mem_end;
1700
1701 copy_len = end - start;
1702 copy_offset = start - jp->pc;
1703 buf_offset = start - mem_addr;
1704
1705 if (jp->inserted)
1706 memcpy (buf + buf_offset,
1707 fast_tracepoint_jump_shadow (jp) + copy_offset,
1708 copy_len);
1709 }
1710
1711 for (; bp != NULL; bp = bp->next)
1712 {
1713 CORE_ADDR bp_end = bp->pc + breakpoint_len;
1714 CORE_ADDR start, end;
1715 int copy_offset, copy_len, buf_offset;
1716
1717 if (bp->raw_type != raw_bkpt_type_sw)
1718 continue;
1719
1720 gdb_assert (bp->old_data >= buf + mem_len
1721 || buf >= &bp->old_data[sizeof (bp->old_data)]);
1722
1723 if (mem_addr >= bp_end)
1724 continue;
1725 if (bp->pc >= mem_end)
1726 continue;
1727
1728 start = bp->pc;
1729 if (mem_addr > start)
1730 start = mem_addr;
1731
1732 end = bp_end;
1733 if (end > mem_end)
1734 end = mem_end;
1735
1736 copy_len = end - start;
1737 copy_offset = start - bp->pc;
1738 buf_offset = start - mem_addr;
1739
1740 if (bp->inserted > 0)
1741 {
1742 if (validate_inserted_breakpoint (bp))
1743 memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
1744 else
1745 disabled_one = 1;
1746 }
1747 }
1748
1749 if (disabled_one)
1750 delete_disabled_breakpoints ();
1751 }
1752
1753 void
1754 check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
1755 const unsigned char *myaddr, int mem_len)
1756 {
1757 struct process_info *proc = current_process ();
1758 struct raw_breakpoint *bp = proc->raw_breakpoints;
1759 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1760 CORE_ADDR mem_end = mem_addr + mem_len;
1761 int disabled_one = 0;
1762
1763 /* First fast tracepoint jumps, then breakpoint traps on top. */
1764
1765 for (; jp != NULL; jp = jp->next)
1766 {
1767 CORE_ADDR jp_end = jp->pc + jp->length;
1768 CORE_ADDR start, end;
1769 int copy_offset, copy_len, buf_offset;
1770
1771 gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len
1772 || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1773 gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len
1774 || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length);
1775
1776 if (mem_addr >= jp_end)
1777 continue;
1778 if (jp->pc >= mem_end)
1779 continue;
1780
1781 start = jp->pc;
1782 if (mem_addr > start)
1783 start = mem_addr;
1784
1785 end = jp_end;
1786 if (end > mem_end)
1787 end = mem_end;
1788
1789 copy_len = end - start;
1790 copy_offset = start - jp->pc;
1791 buf_offset = start - mem_addr;
1792
1793 memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset,
1794 myaddr + buf_offset, copy_len);
1795 if (jp->inserted)
1796 memcpy (buf + buf_offset,
1797 fast_tracepoint_jump_insn (jp) + copy_offset, copy_len);
1798 }
1799
1800 for (; bp != NULL; bp = bp->next)
1801 {
1802 CORE_ADDR bp_end = bp->pc + breakpoint_len;
1803 CORE_ADDR start, end;
1804 int copy_offset, copy_len, buf_offset;
1805
1806 if (bp->raw_type != raw_bkpt_type_sw)
1807 continue;
1808
1809 gdb_assert (bp->old_data >= myaddr + mem_len
1810 || myaddr >= &bp->old_data[sizeof (bp->old_data)]);
1811
1812 if (mem_addr >= bp_end)
1813 continue;
1814 if (bp->pc >= mem_end)
1815 continue;
1816
1817 start = bp->pc;
1818 if (mem_addr > start)
1819 start = mem_addr;
1820
1821 end = bp_end;
1822 if (end > mem_end)
1823 end = mem_end;
1824
1825 copy_len = end - start;
1826 copy_offset = start - bp->pc;
1827 buf_offset = start - mem_addr;
1828
1829 memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len);
1830 if (bp->inserted > 0)
1831 {
1832 if (validate_inserted_breakpoint (bp))
1833 memcpy (buf + buf_offset, breakpoint_data + copy_offset, copy_len);
1834 else
1835 disabled_one = 1;
1836 }
1837 }
1838
1839 if (disabled_one)
1840 delete_disabled_breakpoints ();
1841 }
1842
1843 /* Delete all breakpoints, and un-insert them from the inferior. */
1844
1845 void
1846 delete_all_breakpoints (void)
1847 {
1848 struct process_info *proc = current_process ();
1849
1850 while (proc->breakpoints)
1851 delete_breakpoint_1 (proc, proc->breakpoints);
1852 }
1853
1854 /* Clear the "inserted" flag in all breakpoints. */
1855
1856 void
1857 mark_breakpoints_out (struct process_info *proc)
1858 {
1859 struct raw_breakpoint *raw_bp;
1860
1861 for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next)
1862 raw_bp->inserted = 0;
1863 }
1864
1865 /* Release all breakpoints, but do not try to un-insert them from the
1866 inferior. */
1867
1868 void
1869 free_all_breakpoints (struct process_info *proc)
1870 {
1871 mark_breakpoints_out (proc);
1872
1873 /* Note: use PROC explicitly instead of deferring to
1874 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
1875 released when we get here. There should be no call to
1876 current_process from here on. */
1877 while (proc->breakpoints)
1878 delete_breakpoint_1 (proc, proc->breakpoints);
1879 }
This page took 0.12522 seconds and 4 git commands to generate.