Automatic date update in version.in
[binutils-gdb.git] / gdbserver / mem-break.cc
blob7ebb8c45f63c595992e9172018fcb6263a7b9408
1 /* Memory breakpoint operations for the remote server for GDB.
2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
4 Contributed by MontaVista Software.
6 This file is part of GDB.
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.
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.
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/>. */
21 #include "regcache.h"
22 #include "ax.h"
24 #define MAX_BREAKPOINT_LEN 8
26 /* Helper macro used in loops that append multiple items to a singly-linked
27 list instead of inserting items at the head of the list, as, say, in the
28 breakpoint lists. LISTPP is a pointer to the pointer that is the head of
29 the new list. ITEMP is a pointer to the item to be added to the list.
30 TAILP must be defined to be the same type as ITEMP, and initialized to
31 NULL. */
33 #define APPEND_TO_LIST(listpp, itemp, tailp) \
34 do \
35 { \
36 if ((tailp) == NULL) \
37 *(listpp) = (itemp); \
38 else \
39 (tailp)->next = (itemp); \
40 (tailp) = (itemp); \
41 } \
42 while (0)
44 /* GDB will never try to install multiple breakpoints at the same
45 address. However, we can see GDB requesting to insert a breakpoint
46 at an address is had already inserted one previously in a few
47 situations.
49 - The RSP documentation on Z packets says that to avoid potential
50 problems with duplicate packets, the operations should be
51 implemented in an idempotent way.
53 - A breakpoint is set at ADDR, an address in a shared library.
54 Then the shared library is unloaded. And then another, unrelated,
55 breakpoint at ADDR is set. There is not breakpoint removal request
56 between the first and the second breakpoint.
58 - When GDB wants to update the target-side breakpoint conditions or
59 commands, it re-inserts the breakpoint, with updated
60 conditions/commands associated.
62 Also, we need to keep track of internal breakpoints too, so we do
63 need to be able to install multiple breakpoints at the same address
64 transparently.
66 We keep track of two different, and closely related structures. A
67 raw breakpoint, which manages the low level, close to the metal
68 aspect of a breakpoint. It holds the breakpoint address, and for
69 software breakpoints, a buffer holding a copy of the instructions
70 that would be in memory had not been a breakpoint there (we call
71 that the shadow memory of the breakpoint). We occasionally need to
72 temporarily uninsert a breakpoint without the client knowing about
73 it (e.g., to step over an internal breakpoint), so we keep an
74 `inserted' state associated with this low level breakpoint
75 structure. There can only be one such object for a given address.
76 Then, we have (a bit higher level) breakpoints. This structure
77 holds a callback to be called whenever a breakpoint is hit, a
78 high-level type, and a link to a low level raw breakpoint. There
79 can be many high-level breakpoints at the same address, and all of
80 them will point to the same raw breakpoint, which is reference
81 counted. */
83 /* The low level, physical, raw breakpoint. */
84 struct raw_breakpoint
86 struct raw_breakpoint *next;
88 /* The low level type of the breakpoint (software breakpoint,
89 watchpoint, etc.) */
90 enum raw_bkpt_type raw_type;
92 /* A reference count. Each high level breakpoint referencing this
93 raw breakpoint accounts for one reference. */
94 int refcount;
96 /* The breakpoint's insertion address. There can only be one raw
97 breakpoint for a given PC. */
98 CORE_ADDR pc;
100 /* The breakpoint's kind. This is target specific. Most
101 architectures only use one specific instruction for breakpoints, while
102 others may use more than one. E.g., on ARM, we need to use different
103 breakpoint instructions on Thumb, Thumb-2, and ARM code. Likewise for
104 hardware breakpoints -- some architectures (including ARM) need to
105 setup debug registers differently depending on mode. */
106 int kind;
108 /* The breakpoint's shadow memory. */
109 unsigned char old_data[MAX_BREAKPOINT_LEN];
111 /* Positive if this breakpoint is currently inserted in the
112 inferior. Negative if it was, but we've detected that it's now
113 gone. Zero if not inserted. */
114 int inserted;
117 /* The type of a breakpoint. */
118 enum bkpt_type
120 /* A GDB breakpoint, requested with a Z0 packet. */
121 gdb_breakpoint_Z0,
123 /* A GDB hardware breakpoint, requested with a Z1 packet. */
124 gdb_breakpoint_Z1,
126 /* A GDB write watchpoint, requested with a Z2 packet. */
127 gdb_breakpoint_Z2,
129 /* A GDB read watchpoint, requested with a Z3 packet. */
130 gdb_breakpoint_Z3,
132 /* A GDB access watchpoint, requested with a Z4 packet. */
133 gdb_breakpoint_Z4,
135 /* A software single-step breakpoint. */
136 single_step_breakpoint,
138 /* Any other breakpoint type that doesn't require specific
139 treatment goes here. E.g., an event breakpoint. */
140 other_breakpoint,
143 struct point_cond_list
145 /* Pointer to the agent expression that is the breakpoint's
146 conditional. */
147 struct agent_expr *cond;
149 /* Pointer to the next condition. */
150 struct point_cond_list *next;
153 struct point_command_list
155 /* Pointer to the agent expression that is the breakpoint's
156 commands. */
157 struct agent_expr *cmd;
159 /* Flag that is true if this command should run even while GDB is
160 disconnected. */
161 int persistence;
163 /* Pointer to the next command. */
164 struct point_command_list *next;
167 /* A high level (in gdbserver's perspective) breakpoint. */
168 struct breakpoint
170 struct breakpoint *next;
172 /* The breakpoint's type. */
173 enum bkpt_type type;
175 /* Link to this breakpoint's raw breakpoint. This is always
176 non-NULL. */
177 struct raw_breakpoint *raw;
180 /* Breakpoint requested by GDB. */
182 struct gdb_breakpoint
184 struct breakpoint base;
186 /* Pointer to the condition list that should be evaluated on
187 the target or NULL if the breakpoint is unconditional or
188 if GDB doesn't want us to evaluate the conditionals on the
189 target's side. */
190 struct point_cond_list *cond_list;
192 /* Point to the list of commands to run when this is hit. */
193 struct point_command_list *command_list;
196 /* Breakpoint used by GDBserver. */
198 struct other_breakpoint
200 struct breakpoint base;
202 /* Function to call when we hit this breakpoint. If it returns 1,
203 the breakpoint shall be deleted; 0 or if this callback is NULL,
204 it will be left inserted. */
205 int (*handler) (CORE_ADDR);
208 /* Breakpoint for single step. */
210 struct single_step_breakpoint
212 struct breakpoint base;
214 /* Thread the reinsert breakpoint belongs to. */
215 ptid_t ptid;
218 /* Return the breakpoint size from its kind. */
220 static int
221 bp_size (struct raw_breakpoint *bp)
223 int size = 0;
225 the_target->sw_breakpoint_from_kind (bp->kind, &size);
226 return size;
229 /* Return the breakpoint opcode from its kind. */
231 static const gdb_byte *
232 bp_opcode (struct raw_breakpoint *bp)
234 int size = 0;
236 return the_target->sw_breakpoint_from_kind (bp->kind, &size);
239 /* See mem-break.h. */
241 enum target_hw_bp_type
242 raw_bkpt_type_to_target_hw_bp_type (enum raw_bkpt_type raw_type)
244 switch (raw_type)
246 case raw_bkpt_type_hw:
247 return hw_execute;
248 case raw_bkpt_type_write_wp:
249 return hw_write;
250 case raw_bkpt_type_read_wp:
251 return hw_read;
252 case raw_bkpt_type_access_wp:
253 return hw_access;
254 default:
255 internal_error ("bad raw breakpoint type %d", (int) raw_type);
259 /* See mem-break.h. */
261 static enum bkpt_type
262 Z_packet_to_bkpt_type (char z_type)
264 gdb_assert ('0' <= z_type && z_type <= '4');
266 return (enum bkpt_type) (gdb_breakpoint_Z0 + (z_type - '0'));
269 /* See mem-break.h. */
271 enum raw_bkpt_type
272 Z_packet_to_raw_bkpt_type (char z_type)
274 switch (z_type)
276 case Z_PACKET_SW_BP:
277 return raw_bkpt_type_sw;
278 case Z_PACKET_HW_BP:
279 return raw_bkpt_type_hw;
280 case Z_PACKET_WRITE_WP:
281 return raw_bkpt_type_write_wp;
282 case Z_PACKET_READ_WP:
283 return raw_bkpt_type_read_wp;
284 case Z_PACKET_ACCESS_WP:
285 return raw_bkpt_type_access_wp;
286 default:
287 gdb_assert_not_reached ("unhandled Z packet type.");
291 /* Return true if breakpoint TYPE is a GDB breakpoint. */
293 static int
294 is_gdb_breakpoint (enum bkpt_type type)
296 return (type == gdb_breakpoint_Z0
297 || type == gdb_breakpoint_Z1
298 || type == gdb_breakpoint_Z2
299 || type == gdb_breakpoint_Z3
300 || type == gdb_breakpoint_Z4);
303 bool
304 any_persistent_commands (process_info *proc)
306 struct breakpoint *bp;
307 struct point_command_list *cl;
309 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
311 if (is_gdb_breakpoint (bp->type))
313 struct gdb_breakpoint *gdb_bp = (struct gdb_breakpoint *) bp;
315 for (cl = gdb_bp->command_list; cl != NULL; cl = cl->next)
316 if (cl->persistence)
317 return true;
321 return false;
324 /* Find low-level breakpoint of type TYPE at address ADDR that is not
325 insert-disabled. Returns NULL if not found. */
327 static struct raw_breakpoint *
328 find_enabled_raw_code_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type)
330 struct process_info *proc = current_process ();
331 struct raw_breakpoint *bp;
333 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
334 if (bp->pc == addr
335 && bp->raw_type == type
336 && bp->inserted >= 0)
337 return bp;
339 return NULL;
342 /* Find low-level breakpoint of type TYPE at address ADDR. Returns
343 NULL if not found. */
345 static struct raw_breakpoint *
346 find_raw_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type, int kind)
348 struct process_info *proc = current_process ();
349 struct raw_breakpoint *bp;
351 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
352 if (bp->pc == addr && bp->raw_type == type && bp->kind == kind)
353 return bp;
355 return NULL;
358 /* See mem-break.h. */
361 insert_memory_breakpoint (struct raw_breakpoint *bp)
363 unsigned char buf[MAX_BREAKPOINT_LEN];
364 int err;
366 /* Note that there can be fast tracepoint jumps installed in the
367 same memory range, so to get at the original memory, we need to
368 use read_inferior_memory, which masks those out. */
369 err = read_inferior_memory (bp->pc, buf, bp_size (bp));
370 if (err != 0)
372 threads_debug_printf ("Failed to read shadow memory of"
373 " breakpoint at 0x%s (%s).",
374 paddress (bp->pc), safe_strerror (err));
376 else
378 memcpy (bp->old_data, buf, bp_size (bp));
380 err = the_target->write_memory (bp->pc, bp_opcode (bp),
381 bp_size (bp));
382 if (err != 0)
383 threads_debug_printf ("Failed to insert breakpoint at 0x%s (%s).",
384 paddress (bp->pc), safe_strerror (err));
386 return err != 0 ? -1 : 0;
389 /* See mem-break.h */
392 remove_memory_breakpoint (struct raw_breakpoint *bp)
394 unsigned char buf[MAX_BREAKPOINT_LEN];
395 int err;
397 /* Since there can be trap breakpoints inserted in the same address
398 range, we use `target_write_memory', which takes care of
399 layering breakpoints on top of fast tracepoints, and on top of
400 the buffer we pass it. This works because the caller has already
401 either unlinked the breakpoint or marked it uninserted. Also
402 note that we need to pass the current shadow contents, because
403 target_write_memory updates any shadow memory with what we pass
404 here, and we want that to be a nop. */
405 memcpy (buf, bp->old_data, bp_size (bp));
406 err = target_write_memory (bp->pc, buf, bp_size (bp));
407 if (err != 0)
408 threads_debug_printf ("Failed to uninsert raw breakpoint "
409 "at 0x%s (%s) while deleting it.",
410 paddress (bp->pc), safe_strerror (err));
412 return err != 0 ? -1 : 0;
415 /* Set a RAW breakpoint of type TYPE and kind KIND at WHERE. On
416 success, a pointer to the new breakpoint is returned. On failure,
417 returns NULL and writes the error code to *ERR. */
419 static struct raw_breakpoint *
420 set_raw_breakpoint_at (enum raw_bkpt_type type, CORE_ADDR where, int kind,
421 int *err)
423 struct process_info *proc = current_process ();
424 struct raw_breakpoint *bp;
426 if (type == raw_bkpt_type_sw || type == raw_bkpt_type_hw)
428 bp = find_enabled_raw_code_breakpoint_at (where, type);
429 if (bp != NULL && bp->kind != kind)
431 /* A different kind than previously seen. The previous
432 breakpoint must be gone then. */
433 threads_debug_printf
434 ("Inconsistent breakpoint kind? Was %d, now %d.",
435 bp->kind, kind);
436 bp->inserted = -1;
437 bp = NULL;
440 else
441 bp = find_raw_breakpoint_at (where, type, kind);
443 gdb::unique_xmalloc_ptr<struct raw_breakpoint> bp_holder;
444 if (bp == NULL)
446 bp_holder.reset (XCNEW (struct raw_breakpoint));
447 bp = bp_holder.get ();
448 bp->pc = where;
449 bp->kind = kind;
450 bp->raw_type = type;
453 if (!bp->inserted)
455 *err = the_target->insert_point (bp->raw_type, bp->pc, bp->kind, bp);
456 if (*err != 0)
458 threads_debug_printf ("Failed to insert breakpoint at 0x%s (%d).",
459 paddress (where), *err);
461 return NULL;
464 bp->inserted = 1;
467 /* If the breakpoint was allocated above, we know we want to keep it
468 now. */
469 bp_holder.release ();
471 /* Link the breakpoint in, if this is the first reference. */
472 if (++bp->refcount == 1)
474 bp->next = proc->raw_breakpoints;
475 proc->raw_breakpoints = bp;
477 return bp;
480 /* Notice that breakpoint traps are always installed on top of fast
481 tracepoint jumps. This is even if the fast tracepoint is installed
482 at a later time compared to when the breakpoint was installed.
483 This means that a stopping breakpoint or tracepoint has higher
484 "priority". In turn, this allows having fast and slow tracepoints
485 (and breakpoints) at the same address behave correctly. */
488 /* A fast tracepoint jump. */
490 struct fast_tracepoint_jump
492 struct fast_tracepoint_jump *next;
494 /* A reference count. GDB can install more than one fast tracepoint
495 at the same address (each with its own action list, for
496 example). */
497 int refcount;
499 /* The fast tracepoint's insertion address. There can only be one
500 of these for a given PC. */
501 CORE_ADDR pc;
503 /* Non-zero if this fast tracepoint jump is currently inserted in
504 the inferior. */
505 int inserted;
507 /* The length of the jump instruction. */
508 int length;
510 /* A poor-man's flexible array member, holding both the jump
511 instruction to insert, and a copy of the instruction that would
512 be in memory had not been a jump there (the shadow memory of the
513 tracepoint jump). */
514 unsigned char insn_and_shadow[0];
517 /* Fast tracepoint FP's jump instruction to insert. */
518 #define fast_tracepoint_jump_insn(fp) \
519 ((fp)->insn_and_shadow + 0)
521 /* The shadow memory of fast tracepoint jump FP. */
522 #define fast_tracepoint_jump_shadow(fp) \
523 ((fp)->insn_and_shadow + (fp)->length)
526 /* Return the fast tracepoint jump set at WHERE. */
528 static struct fast_tracepoint_jump *
529 find_fast_tracepoint_jump_at (CORE_ADDR where)
531 struct process_info *proc = current_process ();
532 struct fast_tracepoint_jump *jp;
534 for (jp = proc->fast_tracepoint_jumps; jp != NULL; jp = jp->next)
535 if (jp->pc == where)
536 return jp;
538 return NULL;
542 fast_tracepoint_jump_here (CORE_ADDR where)
544 struct fast_tracepoint_jump *jp = find_fast_tracepoint_jump_at (where);
546 return (jp != NULL);
550 delete_fast_tracepoint_jump (struct fast_tracepoint_jump *todel)
552 struct fast_tracepoint_jump *bp, **bp_link;
553 int ret;
554 struct process_info *proc = current_process ();
556 bp = proc->fast_tracepoint_jumps;
557 bp_link = &proc->fast_tracepoint_jumps;
559 while (bp)
561 if (bp == todel)
563 if (--bp->refcount == 0)
565 struct fast_tracepoint_jump *prev_bp_link = *bp_link;
566 unsigned char *buf;
568 /* Unlink it. */
569 *bp_link = bp->next;
571 /* Since there can be breakpoints inserted in the same
572 address range, we use `target_write_memory', which
573 takes care of layering breakpoints on top of fast
574 tracepoints, and on top of the buffer we pass it.
575 This works because we've already unlinked the fast
576 tracepoint jump above. Also note that we need to
577 pass the current shadow contents, because
578 target_write_memory updates any shadow memory with
579 what we pass here, and we want that to be a nop. */
580 buf = (unsigned char *) alloca (bp->length);
581 memcpy (buf, fast_tracepoint_jump_shadow (bp), bp->length);
582 ret = target_write_memory (bp->pc, buf, bp->length);
583 if (ret != 0)
585 /* Something went wrong, relink the jump. */
586 *bp_link = prev_bp_link;
588 threads_debug_printf
589 ("Failed to uninsert fast tracepoint jump "
590 "at 0x%s (%s) while deleting it.",
591 paddress (bp->pc), safe_strerror (ret));
592 return ret;
595 free (bp);
598 return 0;
600 else
602 bp_link = &bp->next;
603 bp = *bp_link;
607 warning ("Could not find fast tracepoint jump in list.");
608 return ENOENT;
611 void
612 inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump *jp)
614 jp->refcount++;
617 struct fast_tracepoint_jump *
618 set_fast_tracepoint_jump (CORE_ADDR where,
619 unsigned char *insn, ULONGEST length)
621 struct process_info *proc = current_process ();
622 struct fast_tracepoint_jump *jp;
623 int err;
624 unsigned char *buf;
626 /* We refcount fast tracepoint jumps. Check if we already know
627 about a jump at this address. */
628 jp = find_fast_tracepoint_jump_at (where);
629 if (jp != NULL)
631 jp->refcount++;
632 return jp;
635 /* We don't, so create a new object. Double the length, because the
636 flexible array member holds both the jump insn, and the
637 shadow. */
638 jp = (struct fast_tracepoint_jump *) xcalloc (1, sizeof (*jp) + (length * 2));
639 jp->pc = where;
640 jp->length = length;
641 memcpy (fast_tracepoint_jump_insn (jp), insn, length);
642 jp->refcount = 1;
643 buf = (unsigned char *) alloca (length);
645 /* Note that there can be trap breakpoints inserted in the same
646 address range. To access the original memory contents, we use
647 `read_inferior_memory', which masks out breakpoints. */
648 err = read_inferior_memory (where, buf, length);
649 if (err != 0)
651 threads_debug_printf ("Failed to read shadow memory of"
652 " fast tracepoint at 0x%s (%s).",
653 paddress (where), safe_strerror (err));
654 free (jp);
655 return NULL;
657 memcpy (fast_tracepoint_jump_shadow (jp), buf, length);
659 /* Link the jump in. */
660 jp->inserted = 1;
661 jp->next = proc->fast_tracepoint_jumps;
662 proc->fast_tracepoint_jumps = jp;
664 /* Since there can be trap breakpoints inserted in the same address
665 range, we use use `target_write_memory', which takes care of
666 layering breakpoints on top of fast tracepoints, on top of the
667 buffer we pass it. This works because we've already linked in
668 the fast tracepoint jump above. Also note that we need to pass
669 the current shadow contents, because target_write_memory
670 updates any shadow memory with what we pass here, and we want
671 that to be a nop. */
672 err = target_write_memory (where, buf, length);
673 if (err != 0)
675 threads_debug_printf
676 ("Failed to insert fast tracepoint jump at 0x%s (%s).",
677 paddress (where), safe_strerror (err));
679 /* Unlink it. */
680 proc->fast_tracepoint_jumps = jp->next;
681 free (jp);
683 return NULL;
686 return jp;
689 void
690 uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc)
692 struct fast_tracepoint_jump *jp;
693 int err;
695 jp = find_fast_tracepoint_jump_at (pc);
696 if (jp == NULL)
698 /* This can happen when we remove all breakpoints while handling
699 a step-over. */
700 threads_debug_printf ("Could not find fast tracepoint jump at 0x%s "
701 "in list (uninserting).",
702 paddress (pc));
703 return;
706 if (jp->inserted)
708 unsigned char *buf;
710 jp->inserted = 0;
712 /* Since there can be trap breakpoints inserted in the same
713 address range, we use use `target_write_memory', which
714 takes care of layering breakpoints on top of fast
715 tracepoints, and on top of the buffer we pass it. This works
716 because we've already marked the fast tracepoint fast
717 tracepoint jump uninserted above. Also note that we need to
718 pass the current shadow contents, because
719 target_write_memory updates any shadow memory with what we
720 pass here, and we want that to be a nop. */
721 buf = (unsigned char *) alloca (jp->length);
722 memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
723 err = target_write_memory (jp->pc, buf, jp->length);
724 if (err != 0)
726 jp->inserted = 1;
728 threads_debug_printf ("Failed to uninsert fast tracepoint jump at"
729 " 0x%s (%s).",
730 paddress (pc), safe_strerror (err));
735 void
736 reinsert_fast_tracepoint_jumps_at (CORE_ADDR where)
738 struct fast_tracepoint_jump *jp;
739 int err;
740 unsigned char *buf;
742 jp = find_fast_tracepoint_jump_at (where);
743 if (jp == NULL)
745 /* This can happen when we remove breakpoints when a tracepoint
746 hit causes a tracing stop, while handling a step-over. */
747 threads_debug_printf ("Could not find fast tracepoint jump at 0x%s "
748 "in list (reinserting).",
749 paddress (where));
750 return;
753 if (jp->inserted)
754 error ("Jump already inserted at reinsert time.");
756 jp->inserted = 1;
758 /* Since there can be trap breakpoints inserted in the same address
759 range, we use `target_write_memory', which takes care of
760 layering breakpoints on top of fast tracepoints, and on top of
761 the buffer we pass it. This works because we've already marked
762 the fast tracepoint jump inserted above. Also note that we need
763 to pass the current shadow contents, because
764 target_write_memory updates any shadow memory with what we pass
765 here, and we want that to be a nop. */
766 buf = (unsigned char *) alloca (jp->length);
767 memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
768 err = target_write_memory (where, buf, jp->length);
769 if (err != 0)
771 jp->inserted = 0;
773 threads_debug_printf ("Failed to reinsert fast tracepoint jump at"
774 " 0x%s (%s).",
775 paddress (where), safe_strerror (err));
779 /* Set a high-level breakpoint of type TYPE, with low level type
780 RAW_TYPE and kind KIND, at WHERE. On success, a pointer to the new
781 breakpoint is returned. On failure, returns NULL and writes the
782 error code to *ERR. HANDLER is called when the breakpoint is hit.
783 HANDLER should return 1 if the breakpoint should be deleted, 0
784 otherwise. */
786 static struct breakpoint *
787 set_breakpoint (enum bkpt_type type, enum raw_bkpt_type raw_type,
788 CORE_ADDR where, int kind,
789 int (*handler) (CORE_ADDR), int *err)
791 struct process_info *proc = current_process ();
792 struct breakpoint *bp;
793 struct raw_breakpoint *raw;
795 raw = set_raw_breakpoint_at (raw_type, where, kind, err);
797 if (raw == NULL)
799 /* warn? */
800 return NULL;
803 if (is_gdb_breakpoint (type))
805 struct gdb_breakpoint *gdb_bp = XCNEW (struct gdb_breakpoint);
807 bp = (struct breakpoint *) gdb_bp;
808 gdb_assert (handler == NULL);
810 else if (type == other_breakpoint)
812 struct other_breakpoint *other_bp = XCNEW (struct other_breakpoint);
814 other_bp->handler = handler;
815 bp = (struct breakpoint *) other_bp;
817 else if (type == single_step_breakpoint)
819 struct single_step_breakpoint *ss_bp
820 = XCNEW (struct single_step_breakpoint);
822 bp = (struct breakpoint *) ss_bp;
824 else
825 gdb_assert_not_reached ("unhandled breakpoint type");
827 bp->type = type;
828 bp->raw = raw;
830 bp->next = proc->breakpoints;
831 proc->breakpoints = bp;
833 return bp;
836 /* Set breakpoint of TYPE on address WHERE with handler HANDLER. */
838 static struct breakpoint *
839 set_breakpoint_type_at (enum bkpt_type type, CORE_ADDR where,
840 int (*handler) (CORE_ADDR))
842 int err_ignored;
843 CORE_ADDR placed_address = where;
844 int breakpoint_kind = target_breakpoint_kind_from_pc (&placed_address);
846 return set_breakpoint (type, raw_bkpt_type_sw,
847 placed_address, breakpoint_kind, handler,
848 &err_ignored);
851 /* See mem-break.h */
853 struct breakpoint *
854 set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
856 return set_breakpoint_type_at (other_breakpoint, where, handler);
860 static int
861 delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
863 struct raw_breakpoint *bp, **bp_link;
864 int ret;
866 bp = proc->raw_breakpoints;
867 bp_link = &proc->raw_breakpoints;
869 while (bp)
871 if (bp == todel)
873 if (bp->inserted > 0)
875 struct raw_breakpoint *prev_bp_link = *bp_link;
877 *bp_link = bp->next;
879 ret = the_target->remove_point (bp->raw_type, bp->pc,
880 bp->kind, bp);
881 if (ret != 0)
883 /* Something went wrong, relink the breakpoint. */
884 *bp_link = prev_bp_link;
886 threads_debug_printf ("Failed to uninsert raw breakpoint "
887 "at 0x%s while deleting it.",
888 paddress (bp->pc));
889 return ret;
892 else
893 *bp_link = bp->next;
895 free (bp);
896 return 0;
898 else
900 bp_link = &bp->next;
901 bp = *bp_link;
905 warning ("Could not find raw breakpoint in list.");
906 return ENOENT;
909 static int
910 release_breakpoint (struct process_info *proc, struct breakpoint *bp)
912 int newrefcount;
913 int ret;
915 newrefcount = bp->raw->refcount - 1;
916 if (newrefcount == 0)
918 ret = delete_raw_breakpoint (proc, bp->raw);
919 if (ret != 0)
920 return ret;
922 else
923 bp->raw->refcount = newrefcount;
925 free (bp);
927 return 0;
930 static int
931 delete_breakpoint_1 (struct process_info *proc, struct breakpoint *todel)
933 struct breakpoint *bp, **bp_link;
934 int err;
936 bp = proc->breakpoints;
937 bp_link = &proc->breakpoints;
939 while (bp)
941 if (bp == todel)
943 *bp_link = bp->next;
945 err = release_breakpoint (proc, bp);
946 if (err != 0)
947 return err;
949 bp = *bp_link;
950 return 0;
952 else
954 bp_link = &bp->next;
955 bp = *bp_link;
959 warning ("Could not find breakpoint in list.");
960 return ENOENT;
964 delete_breakpoint (struct breakpoint *todel)
966 struct process_info *proc = current_process ();
967 return delete_breakpoint_1 (proc, todel);
970 /* Locate a GDB breakpoint of type Z_TYPE and kind KIND placed at
971 address ADDR and return a pointer to its structure. If KIND is -1,
972 the breakpoint's kind is ignored. */
974 static struct gdb_breakpoint *
975 find_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind)
977 struct process_info *proc = current_process ();
979 /* In some situations the current process exits, we inform GDB, but
980 before GDB can acknowledge that the process has exited GDB tries to
981 detach from the inferior. As part of the detach process GDB will
982 remove all breakpoints, which means we can end up here when the
983 current process has already exited and so PROC is nullptr. In this
984 case just claim we can't find (and so delete) the breakpoint, GDB
985 will ignore this error during detach. */
986 if (proc == nullptr)
987 return nullptr;
989 struct breakpoint *bp;
990 enum bkpt_type type = Z_packet_to_bkpt_type (z_type);
992 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
993 if (bp->type == type && bp->raw->pc == addr
994 && (kind == -1 || bp->raw->kind == kind))
995 return (struct gdb_breakpoint *) bp;
997 return NULL;
1000 static int
1001 z_type_supported (char z_type)
1003 return (z_type >= '0' && z_type <= '4'
1004 && the_target->supports_z_point_type (z_type));
1007 /* Create a new GDB breakpoint of type Z_TYPE at ADDR with kind KIND.
1008 Returns a pointer to the newly created breakpoint on success. On
1009 failure returns NULL and sets *ERR to either -1 for error, or 1 if
1010 Z_TYPE breakpoints are not supported on this target. */
1012 struct gdb_breakpoint *
1013 set_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind, int *err)
1015 struct gdb_breakpoint *bp;
1016 enum bkpt_type type;
1017 enum raw_bkpt_type raw_type;
1019 if (!z_type_supported (z_type))
1021 *err = 1;
1022 return nullptr;
1025 /* If we see GDB inserting a second code breakpoint at the same
1026 address, then either: GDB is updating the breakpoint's conditions
1027 or commands; or, the first breakpoint must have disappeared due
1028 to a shared library unload. On targets where the shared
1029 libraries are handled by userspace, like SVR4, for example,
1030 GDBserver can't tell if a library was loaded or unloaded. Since
1031 we refcount raw breakpoints, we must be careful to make sure GDB
1032 breakpoints never contribute more than one reference. if we
1033 didn't do this, in case the previous breakpoint is gone due to a
1034 shared library unload, we'd just increase the refcount of the
1035 previous breakpoint at this address, but the trap was not planted
1036 in the inferior anymore, thus the breakpoint would never be hit.
1037 Note this must be careful to not create a window where
1038 breakpoints are removed from the target, for non-stop, in case
1039 the target can poke at memory while the program is running. */
1040 if (z_type == Z_PACKET_SW_BP
1041 || z_type == Z_PACKET_HW_BP)
1043 bp = find_gdb_breakpoint (z_type, addr, -1);
1045 if (bp != NULL)
1047 if (bp->base.raw->kind != kind)
1049 /* A different kind than previously seen. The previous
1050 breakpoint must be gone then. */
1051 bp->base.raw->inserted = -1;
1052 delete_breakpoint ((struct breakpoint *) bp);
1053 bp = NULL;
1055 else if (z_type == Z_PACKET_SW_BP)
1057 /* Check if the breakpoint is actually gone from the
1058 target, due to an solib unload, for example. Might
1059 as well validate _all_ breakpoints. */
1060 validate_breakpoints ();
1062 /* Breakpoints that don't pass validation are
1063 deleted. */
1064 bp = find_gdb_breakpoint (z_type, addr, -1);
1068 else
1070 /* Data breakpoints for the same address but different kind are
1071 expected. GDB doesn't merge these. The backend gets to do
1072 that if it wants/can. */
1073 bp = find_gdb_breakpoint (z_type, addr, kind);
1076 if (bp != NULL)
1078 /* We already know about this breakpoint, there's nothing else
1079 to do - GDB's reference is already accounted for. Note that
1080 whether the breakpoint inserted is left as is - we may be
1081 stepping over it, for example, in which case we don't want to
1082 force-reinsert it. */
1083 return bp;
1086 raw_type = Z_packet_to_raw_bkpt_type (z_type);
1087 type = Z_packet_to_bkpt_type (z_type);
1088 return (struct gdb_breakpoint *) set_breakpoint (type, raw_type, addr,
1089 kind, NULL, err);
1092 /* Delete a GDB breakpoint of type Z_TYPE and kind KIND previously
1093 inserted at ADDR with set_gdb_breakpoint_at. Returns 0 on success,
1094 -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
1095 target. */
1098 delete_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind)
1100 if (!z_type_supported (z_type))
1101 return 1;
1103 gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, kind);
1104 if (bp == NULL)
1105 return -1;
1107 /* Before deleting the breakpoint, make sure to free its condition
1108 and command lists. */
1109 clear_breakpoint_conditions_and_commands (bp);
1110 int err = delete_breakpoint ((struct breakpoint *) bp);
1111 if (err != 0)
1112 return -1;
1114 return 0;
1117 /* Clear all conditions associated with a breakpoint. */
1119 static void
1120 clear_breakpoint_conditions (struct gdb_breakpoint *bp)
1122 struct point_cond_list *cond;
1124 if (bp->cond_list == NULL)
1125 return;
1127 cond = bp->cond_list;
1129 while (cond != NULL)
1131 struct point_cond_list *cond_next;
1133 cond_next = cond->next;
1134 gdb_free_agent_expr (cond->cond);
1135 free (cond);
1136 cond = cond_next;
1139 bp->cond_list = NULL;
1142 /* Clear all commands associated with a breakpoint. */
1144 static void
1145 clear_breakpoint_commands (struct gdb_breakpoint *bp)
1147 struct point_command_list *cmd;
1149 if (bp->command_list == NULL)
1150 return;
1152 cmd = bp->command_list;
1154 while (cmd != NULL)
1156 struct point_command_list *cmd_next;
1158 cmd_next = cmd->next;
1159 gdb_free_agent_expr (cmd->cmd);
1160 free (cmd);
1161 cmd = cmd_next;
1164 bp->command_list = NULL;
1167 void
1168 clear_breakpoint_conditions_and_commands (struct gdb_breakpoint *bp)
1170 clear_breakpoint_conditions (bp);
1171 clear_breakpoint_commands (bp);
1174 /* Add condition CONDITION to GDBserver's breakpoint BP. */
1176 static void
1177 add_condition_to_breakpoint (struct gdb_breakpoint *bp,
1178 struct agent_expr *condition)
1180 struct point_cond_list *new_cond;
1182 /* Create new condition. */
1183 new_cond = XCNEW (struct point_cond_list);
1184 new_cond->cond = condition;
1186 /* Add condition to the list. */
1187 new_cond->next = bp->cond_list;
1188 bp->cond_list = new_cond;
1191 /* Add a target-side condition CONDITION to a breakpoint. */
1194 add_breakpoint_condition (struct gdb_breakpoint *bp, const char **condition)
1196 const char *actparm = *condition;
1197 struct agent_expr *cond;
1199 if (condition == NULL)
1200 return 1;
1202 if (bp == NULL)
1203 return 0;
1205 cond = gdb_parse_agent_expr (&actparm);
1207 if (cond == NULL)
1209 warning ("Condition evaluation failed. Assuming unconditional.");
1210 return 0;
1213 add_condition_to_breakpoint (bp, cond);
1215 *condition = actparm;
1217 return 1;
1220 /* Evaluate condition (if any) at breakpoint BP. Return 1 if
1221 true and 0 otherwise. */
1223 static int
1224 gdb_condition_true_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
1226 /* Fetch registers for the current inferior. */
1227 struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1228 ULONGEST value = 0;
1229 struct point_cond_list *cl;
1230 int err = 0;
1231 struct eval_agent_expr_context ctx;
1233 if (bp == NULL)
1234 return 0;
1236 /* Check if the breakpoint is unconditional. If it is,
1237 the condition always evaluates to TRUE. */
1238 if (bp->cond_list == NULL)
1239 return 1;
1241 ctx.regcache = get_thread_regcache (current_thread, 1);
1242 ctx.tframe = NULL;
1243 ctx.tpoint = NULL;
1245 /* Evaluate each condition in the breakpoint's list of conditions.
1246 Return true if any of the conditions evaluates to TRUE.
1248 If we failed to evaluate the expression, TRUE is returned. This
1249 forces GDB to reevaluate the conditions. */
1250 for (cl = bp->cond_list;
1251 cl && !value && !err; cl = cl->next)
1253 /* Evaluate the condition. */
1254 err = gdb_eval_agent_expr (&ctx, cl->cond, &value);
1257 if (err)
1258 return 1;
1260 return (value != 0);
1264 gdb_condition_true_at_breakpoint (CORE_ADDR where)
1266 /* Only check code (software or hardware) breakpoints. */
1267 return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
1268 || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
1271 /* Add commands COMMANDS to GDBserver's breakpoint BP. */
1273 static void
1274 add_commands_to_breakpoint (struct gdb_breakpoint *bp,
1275 struct agent_expr *commands, int persist)
1277 struct point_command_list *new_cmd;
1279 /* Create new command. */
1280 new_cmd = XCNEW (struct point_command_list);
1281 new_cmd->cmd = commands;
1282 new_cmd->persistence = persist;
1284 /* Add commands to the list. */
1285 new_cmd->next = bp->command_list;
1286 bp->command_list = new_cmd;
1289 /* Add a target-side command COMMAND to the breakpoint at ADDR. */
1292 add_breakpoint_commands (struct gdb_breakpoint *bp, const char **command,
1293 int persist)
1295 const char *actparm = *command;
1296 struct agent_expr *cmd;
1298 if (command == NULL)
1299 return 1;
1301 if (bp == NULL)
1302 return 0;
1304 cmd = gdb_parse_agent_expr (&actparm);
1306 if (cmd == NULL)
1308 warning ("Command evaluation failed. Disabling.");
1309 return 0;
1312 add_commands_to_breakpoint (bp, cmd, persist);
1314 *command = actparm;
1316 return 1;
1319 /* Return true if there are no commands to run at this location,
1320 which likely means we want to report back to GDB. */
1322 static int
1323 gdb_no_commands_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
1325 struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1327 if (bp == NULL)
1328 return 1;
1330 threads_debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s",
1331 paddress (addr), z_type,
1332 phex_nz ((uintptr_t) bp->command_list, 0));
1333 return (bp->command_list == NULL);
1336 /* Return true if there are no commands to run at this location,
1337 which likely means we want to report back to GDB. */
1340 gdb_no_commands_at_breakpoint (CORE_ADDR where)
1342 /* Only check code (software or hardware) breakpoints. */
1343 return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
1344 && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
1347 /* Run a breakpoint's commands. Returns 0 if there was a problem
1348 running any command, 1 otherwise. */
1350 static int
1351 run_breakpoint_commands_z_type (char z_type, CORE_ADDR addr)
1353 /* Fetch registers for the current inferior. */
1354 struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1355 ULONGEST value = 0;
1356 struct point_command_list *cl;
1357 int err = 0;
1358 struct eval_agent_expr_context ctx;
1360 if (bp == NULL)
1361 return 1;
1363 ctx.regcache = get_thread_regcache (current_thread, 1);
1364 ctx.tframe = NULL;
1365 ctx.tpoint = NULL;
1367 for (cl = bp->command_list;
1368 cl && !value && !err; cl = cl->next)
1370 /* Run the command. */
1371 err = gdb_eval_agent_expr (&ctx, cl->cmd, &value);
1373 /* If one command has a problem, stop digging the hole deeper. */
1374 if (err)
1375 return 0;
1378 return 1;
1381 void
1382 run_breakpoint_commands (CORE_ADDR where)
1384 /* Only check code (software or hardware) breakpoints. If one
1385 command has a problem, stop digging the hole deeper. */
1386 if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP, where))
1387 run_breakpoint_commands_z_type (Z_PACKET_HW_BP, where);
1390 /* See mem-break.h. */
1393 gdb_breakpoint_here (CORE_ADDR where)
1395 /* Only check code (software or hardware) breakpoints. */
1396 return (find_gdb_breakpoint (Z_PACKET_SW_BP, where, -1) != NULL
1397 || find_gdb_breakpoint (Z_PACKET_HW_BP, where, -1) != NULL);
1400 void
1401 set_single_step_breakpoint (CORE_ADDR stop_at, ptid_t ptid)
1403 struct single_step_breakpoint *bp;
1405 gdb_assert (current_thread->id.pid () == ptid.pid ());
1407 bp = (struct single_step_breakpoint *) set_breakpoint_type_at (single_step_breakpoint,
1408 stop_at, NULL);
1409 bp->ptid = ptid;
1412 void
1413 delete_single_step_breakpoints (struct thread_info *thread)
1415 process_info *proc = thread->process ();
1416 struct breakpoint *bp, **bp_link;
1418 bp = proc->breakpoints;
1419 bp_link = &proc->breakpoints;
1421 while (bp)
1423 if (bp->type == single_step_breakpoint
1424 && ((struct single_step_breakpoint *) bp)->ptid == thread->id)
1426 scoped_restore_current_thread restore_thread;
1428 switch_to_thread (thread);
1429 *bp_link = bp->next;
1430 release_breakpoint (proc, bp);
1431 bp = *bp_link;
1433 else
1435 bp_link = &bp->next;
1436 bp = *bp_link;
1441 static void
1442 uninsert_raw_breakpoint (struct raw_breakpoint *bp)
1444 if (bp->inserted < 0)
1446 threads_debug_printf ("Breakpoint at %s is marked insert-disabled.",
1447 paddress (bp->pc));
1449 else if (bp->inserted > 0)
1451 int err;
1453 bp->inserted = 0;
1455 err = the_target->remove_point (bp->raw_type, bp->pc, bp->kind, bp);
1456 if (err != 0)
1458 bp->inserted = 1;
1460 threads_debug_printf ("Failed to uninsert raw breakpoint at 0x%s.",
1461 paddress (bp->pc));
1466 void
1467 uninsert_breakpoints_at (CORE_ADDR pc)
1469 struct process_info *proc = current_process ();
1470 struct raw_breakpoint *bp;
1471 int found = 0;
1473 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1474 if ((bp->raw_type == raw_bkpt_type_sw
1475 || bp->raw_type == raw_bkpt_type_hw)
1476 && bp->pc == pc)
1478 found = 1;
1480 if (bp->inserted)
1481 uninsert_raw_breakpoint (bp);
1484 if (!found)
1486 /* This can happen when we remove all breakpoints while handling
1487 a step-over. */
1488 threads_debug_printf ("Could not find breakpoint at 0x%s "
1489 "in list (uninserting).",
1490 paddress (pc));
1494 void
1495 uninsert_all_breakpoints (void)
1497 struct process_info *proc = current_process ();
1498 struct raw_breakpoint *bp;
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->inserted)
1504 uninsert_raw_breakpoint (bp);
1507 void
1508 uninsert_single_step_breakpoints (struct thread_info *thread)
1510 process_info *proc = thread->process ();
1511 struct breakpoint *bp;
1513 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1515 if (bp->type == single_step_breakpoint
1516 && ((struct single_step_breakpoint *) bp)->ptid == thread->id)
1518 gdb_assert (bp->raw->inserted > 0);
1520 /* Only uninsert the raw breakpoint if it only belongs to a
1521 reinsert breakpoint. */
1522 if (bp->raw->refcount == 1)
1524 scoped_restore_current_thread restore_thread;
1526 switch_to_thread (thread);
1527 uninsert_raw_breakpoint (bp->raw);
1533 static void
1534 reinsert_raw_breakpoint (struct raw_breakpoint *bp)
1536 int err;
1538 if (bp->inserted)
1539 return;
1541 err = the_target->insert_point (bp->raw_type, bp->pc, bp->kind, bp);
1542 if (err == 0)
1543 bp->inserted = 1;
1544 else
1545 threads_debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).",
1546 paddress (bp->pc), err);
1549 void
1550 reinsert_breakpoints_at (CORE_ADDR pc)
1552 struct process_info *proc = current_process ();
1553 struct raw_breakpoint *bp;
1554 int found = 0;
1556 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1557 if ((bp->raw_type == raw_bkpt_type_sw
1558 || bp->raw_type == raw_bkpt_type_hw)
1559 && bp->pc == pc)
1561 found = 1;
1563 reinsert_raw_breakpoint (bp);
1566 if (!found)
1568 /* This can happen when we remove all breakpoints while handling
1569 a step-over. */
1570 threads_debug_printf ("Could not find raw breakpoint at 0x%s "
1571 "in list (reinserting).",
1572 paddress (pc));
1577 has_single_step_breakpoints (struct thread_info *thread)
1579 process_info *proc = thread->process ();
1580 struct breakpoint *bp, **bp_link;
1582 bp = proc->breakpoints;
1583 bp_link = &proc->breakpoints;
1585 while (bp)
1587 if (bp->type == single_step_breakpoint
1588 && ((struct single_step_breakpoint *) bp)->ptid == thread->id)
1589 return 1;
1590 else
1592 bp_link = &bp->next;
1593 bp = *bp_link;
1597 return 0;
1600 void
1601 reinsert_all_breakpoints (void)
1603 struct process_info *proc = current_process ();
1604 struct raw_breakpoint *bp;
1606 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1607 if ((bp->raw_type == raw_bkpt_type_sw
1608 || bp->raw_type == raw_bkpt_type_hw)
1609 && !bp->inserted)
1610 reinsert_raw_breakpoint (bp);
1613 void
1614 reinsert_single_step_breakpoints (struct thread_info *thread)
1616 process_info *proc = thread->process ();
1617 struct breakpoint *bp;
1619 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1621 if (bp->type == single_step_breakpoint
1622 && ((struct single_step_breakpoint *) bp)->ptid == thread->id)
1624 gdb_assert (bp->raw->inserted > 0);
1626 if (bp->raw->refcount == 1)
1628 scoped_restore_current_thread restore_thread;
1630 switch_to_thread (thread);
1631 reinsert_raw_breakpoint (bp->raw);
1637 void
1638 check_breakpoints (CORE_ADDR stop_pc)
1640 struct process_info *proc = current_process ();
1641 struct breakpoint *bp, **bp_link;
1643 bp = proc->breakpoints;
1644 bp_link = &proc->breakpoints;
1646 while (bp)
1648 struct raw_breakpoint *raw = bp->raw;
1650 if ((raw->raw_type == raw_bkpt_type_sw
1651 || raw->raw_type == raw_bkpt_type_hw)
1652 && raw->pc == stop_pc)
1654 if (!raw->inserted)
1656 warning ("Hit a removed breakpoint?");
1657 return;
1660 if (bp->type == other_breakpoint)
1662 struct other_breakpoint *other_bp
1663 = (struct other_breakpoint *) bp;
1665 if (other_bp->handler != NULL && (*other_bp->handler) (stop_pc))
1667 *bp_link = bp->next;
1669 release_breakpoint (proc, bp);
1671 bp = *bp_link;
1672 continue;
1677 bp_link = &bp->next;
1678 bp = *bp_link;
1683 breakpoint_here (CORE_ADDR addr)
1685 struct process_info *proc = current_process ();
1686 struct raw_breakpoint *bp;
1688 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1689 if ((bp->raw_type == raw_bkpt_type_sw
1690 || bp->raw_type == raw_bkpt_type_hw)
1691 && bp->pc == addr)
1692 return 1;
1694 return 0;
1698 breakpoint_inserted_here (CORE_ADDR addr)
1700 struct process_info *proc = current_process ();
1701 struct raw_breakpoint *bp;
1703 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1704 if ((bp->raw_type == raw_bkpt_type_sw
1705 || bp->raw_type == raw_bkpt_type_hw)
1706 && bp->pc == addr
1707 && bp->inserted)
1708 return 1;
1710 return 0;
1713 /* See mem-break.h. */
1716 software_breakpoint_inserted_here (CORE_ADDR addr)
1718 struct process_info *proc = current_process ();
1719 struct raw_breakpoint *bp;
1721 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1722 if (bp->raw_type == raw_bkpt_type_sw
1723 && bp->pc == addr
1724 && bp->inserted)
1725 return 1;
1727 return 0;
1730 /* See mem-break.h. */
1733 hardware_breakpoint_inserted_here (CORE_ADDR addr)
1735 struct process_info *proc = current_process ();
1736 struct raw_breakpoint *bp;
1738 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1739 if (bp->raw_type == raw_bkpt_type_hw
1740 && bp->pc == addr
1741 && bp->inserted)
1742 return 1;
1744 return 0;
1747 /* See mem-break.h. */
1750 single_step_breakpoint_inserted_here (CORE_ADDR addr)
1752 struct process_info *proc = current_process ();
1753 struct breakpoint *bp;
1755 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1756 if (bp->type == single_step_breakpoint
1757 && bp->raw->pc == addr
1758 && bp->raw->inserted)
1759 return 1;
1761 return 0;
1764 static int
1765 validate_inserted_breakpoint (struct raw_breakpoint *bp)
1767 unsigned char *buf;
1768 int err;
1770 gdb_assert (bp->inserted);
1771 gdb_assert (bp->raw_type == raw_bkpt_type_sw);
1773 buf = (unsigned char *) alloca (bp_size (bp));
1774 err = the_target->read_memory (bp->pc, buf, bp_size (bp));
1775 if (err || memcmp (buf, bp_opcode (bp), bp_size (bp)) != 0)
1777 /* Tag it as gone. */
1778 bp->inserted = -1;
1779 return 0;
1782 return 1;
1785 static void
1786 delete_disabled_breakpoints (void)
1788 struct process_info *proc = current_process ();
1789 struct breakpoint *bp, *next;
1791 for (bp = proc->breakpoints; bp != NULL; bp = next)
1793 next = bp->next;
1794 if (bp->raw->inserted < 0)
1796 /* If single_step_breakpoints become disabled, that means the
1797 manipulations (insertion and removal) of them are wrong. */
1798 gdb_assert (bp->type != single_step_breakpoint);
1799 delete_breakpoint_1 (proc, bp);
1804 /* Check if breakpoints we inserted still appear to be inserted. They
1805 may disappear due to a shared library unload, and worse, a new
1806 shared library may be reloaded at the same address as the
1807 previously unloaded one. If that happens, we should make sure that
1808 the shadow memory of the old breakpoints isn't used when reading or
1809 writing memory. */
1811 void
1812 validate_breakpoints (void)
1814 struct process_info *proc = current_process ();
1815 struct breakpoint *bp;
1817 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1819 struct raw_breakpoint *raw = bp->raw;
1821 if (raw->raw_type == raw_bkpt_type_sw && raw->inserted > 0)
1822 validate_inserted_breakpoint (raw);
1825 delete_disabled_breakpoints ();
1828 void
1829 check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
1831 struct process_info *proc = current_process ();
1832 struct raw_breakpoint *bp = proc->raw_breakpoints;
1833 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1834 CORE_ADDR mem_end = mem_addr + mem_len;
1835 int disabled_one = 0;
1837 for (; jp != NULL; jp = jp->next)
1839 CORE_ADDR bp_end = jp->pc + jp->length;
1840 CORE_ADDR start, end;
1841 int copy_offset, copy_len, buf_offset;
1843 gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len
1844 || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1846 if (mem_addr >= bp_end)
1847 continue;
1848 if (jp->pc >= mem_end)
1849 continue;
1851 start = jp->pc;
1852 if (mem_addr > start)
1853 start = mem_addr;
1855 end = bp_end;
1856 if (end > mem_end)
1857 end = mem_end;
1859 copy_len = end - start;
1860 copy_offset = start - jp->pc;
1861 buf_offset = start - mem_addr;
1863 if (jp->inserted)
1864 memcpy (buf + buf_offset,
1865 fast_tracepoint_jump_shadow (jp) + copy_offset,
1866 copy_len);
1869 for (; bp != NULL; bp = bp->next)
1871 CORE_ADDR bp_end = bp->pc + bp_size (bp);
1872 CORE_ADDR start, end;
1873 int copy_offset, copy_len, buf_offset;
1875 if (bp->raw_type != raw_bkpt_type_sw)
1876 continue;
1878 gdb_assert (bp->old_data >= buf + mem_len
1879 || buf >= &bp->old_data[sizeof (bp->old_data)]);
1881 if (mem_addr >= bp_end)
1882 continue;
1883 if (bp->pc >= mem_end)
1884 continue;
1886 start = bp->pc;
1887 if (mem_addr > start)
1888 start = mem_addr;
1890 end = bp_end;
1891 if (end > mem_end)
1892 end = mem_end;
1894 copy_len = end - start;
1895 copy_offset = start - bp->pc;
1896 buf_offset = start - mem_addr;
1898 if (bp->inserted > 0)
1900 if (validate_inserted_breakpoint (bp))
1901 memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
1902 else
1903 disabled_one = 1;
1907 if (disabled_one)
1908 delete_disabled_breakpoints ();
1911 void
1912 check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
1913 const unsigned char *myaddr, int mem_len)
1915 struct process_info *proc = current_process ();
1916 struct raw_breakpoint *bp = proc->raw_breakpoints;
1917 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1918 CORE_ADDR mem_end = mem_addr + mem_len;
1919 int disabled_one = 0;
1921 /* First fast tracepoint jumps, then breakpoint traps on top. */
1923 for (; jp != NULL; jp = jp->next)
1925 CORE_ADDR jp_end = jp->pc + jp->length;
1926 CORE_ADDR start, end;
1927 int copy_offset, copy_len, buf_offset;
1929 gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len
1930 || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1931 gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len
1932 || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length);
1934 if (mem_addr >= jp_end)
1935 continue;
1936 if (jp->pc >= mem_end)
1937 continue;
1939 start = jp->pc;
1940 if (mem_addr > start)
1941 start = mem_addr;
1943 end = jp_end;
1944 if (end > mem_end)
1945 end = mem_end;
1947 copy_len = end - start;
1948 copy_offset = start - jp->pc;
1949 buf_offset = start - mem_addr;
1951 memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset,
1952 myaddr + buf_offset, copy_len);
1953 if (jp->inserted)
1954 memcpy (buf + buf_offset,
1955 fast_tracepoint_jump_insn (jp) + copy_offset, copy_len);
1958 for (; bp != NULL; bp = bp->next)
1960 CORE_ADDR bp_end = bp->pc + bp_size (bp);
1961 CORE_ADDR start, end;
1962 int copy_offset, copy_len, buf_offset;
1964 if (bp->raw_type != raw_bkpt_type_sw)
1965 continue;
1967 gdb_assert (bp->old_data >= myaddr + mem_len
1968 || myaddr >= &bp->old_data[sizeof (bp->old_data)]);
1970 if (mem_addr >= bp_end)
1971 continue;
1972 if (bp->pc >= mem_end)
1973 continue;
1975 start = bp->pc;
1976 if (mem_addr > start)
1977 start = mem_addr;
1979 end = bp_end;
1980 if (end > mem_end)
1981 end = mem_end;
1983 copy_len = end - start;
1984 copy_offset = start - bp->pc;
1985 buf_offset = start - mem_addr;
1987 memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len);
1988 if (bp->inserted > 0)
1990 if (validate_inserted_breakpoint (bp))
1991 memcpy (buf + buf_offset, bp_opcode (bp) + copy_offset, copy_len);
1992 else
1993 disabled_one = 1;
1997 if (disabled_one)
1998 delete_disabled_breakpoints ();
2001 /* Delete all breakpoints, watchpoints, tracepoints, and catchpoints,
2002 and un-insert them from the inferior. */
2004 void
2005 delete_all_breakpoints (void)
2007 struct process_info *proc = current_process ();
2009 while (proc->breakpoints)
2010 delete_breakpoint_1 (proc, proc->breakpoints);
2013 /* Clear the "inserted" flag in all breakpoints. */
2015 void
2016 mark_breakpoints_out (struct process_info *proc)
2018 struct raw_breakpoint *raw_bp;
2020 for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next)
2021 raw_bp->inserted = 0;
2024 /* Release all breakpoints, watchpoints, tracepoints, and catchpoints,
2025 but do not try to un-insert them from the inferior. */
2027 void
2028 free_all_breakpoints (struct process_info *proc)
2030 mark_breakpoints_out (proc);
2032 /* Note: use PROC explicitly instead of deferring to
2033 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
2034 released when we get here. There should be no call to
2035 current_process from here on. */
2036 while (proc->breakpoints)
2037 delete_breakpoint_1 (proc, proc->breakpoints);
2040 /* Clone an agent expression. */
2042 static struct agent_expr *
2043 clone_agent_expr (const struct agent_expr *src_ax)
2045 struct agent_expr *ax;
2047 ax = XCNEW (struct agent_expr);
2048 ax->length = src_ax->length;
2049 ax->bytes = (unsigned char *) xcalloc (ax->length, 1);
2050 memcpy (ax->bytes, src_ax->bytes, ax->length);
2051 return ax;
2054 /* Deep-copy the contents of one breakpoint to another. */
2056 static struct breakpoint *
2057 clone_one_breakpoint (const struct breakpoint *src, ptid_t ptid)
2059 struct breakpoint *dest;
2060 struct raw_breakpoint *dest_raw;
2062 /* Clone the raw breakpoint. */
2063 dest_raw = XCNEW (struct raw_breakpoint);
2064 dest_raw->raw_type = src->raw->raw_type;
2065 dest_raw->refcount = src->raw->refcount;
2066 dest_raw->pc = src->raw->pc;
2067 dest_raw->kind = src->raw->kind;
2068 memcpy (dest_raw->old_data, src->raw->old_data, MAX_BREAKPOINT_LEN);
2069 dest_raw->inserted = src->raw->inserted;
2071 /* Clone the high-level breakpoint. */
2072 if (is_gdb_breakpoint (src->type))
2074 struct gdb_breakpoint *gdb_dest = XCNEW (struct gdb_breakpoint);
2075 struct point_cond_list *current_cond;
2076 struct point_cond_list *new_cond;
2077 struct point_cond_list *cond_tail = NULL;
2078 struct point_command_list *current_cmd;
2079 struct point_command_list *new_cmd;
2080 struct point_command_list *cmd_tail = NULL;
2082 /* Clone the condition list. */
2083 for (current_cond = ((struct gdb_breakpoint *) src)->cond_list;
2084 current_cond != NULL;
2085 current_cond = current_cond->next)
2087 new_cond = XCNEW (struct point_cond_list);
2088 new_cond->cond = clone_agent_expr (current_cond->cond);
2089 APPEND_TO_LIST (&gdb_dest->cond_list, new_cond, cond_tail);
2092 /* Clone the command list. */
2093 for (current_cmd = ((struct gdb_breakpoint *) src)->command_list;
2094 current_cmd != NULL;
2095 current_cmd = current_cmd->next)
2097 new_cmd = XCNEW (struct point_command_list);
2098 new_cmd->cmd = clone_agent_expr (current_cmd->cmd);
2099 new_cmd->persistence = current_cmd->persistence;
2100 APPEND_TO_LIST (&gdb_dest->command_list, new_cmd, cmd_tail);
2103 dest = (struct breakpoint *) gdb_dest;
2105 else if (src->type == other_breakpoint)
2107 struct other_breakpoint *other_dest = XCNEW (struct other_breakpoint);
2109 other_dest->handler = ((struct other_breakpoint *) src)->handler;
2110 dest = (struct breakpoint *) other_dest;
2112 else if (src->type == single_step_breakpoint)
2114 struct single_step_breakpoint *ss_dest
2115 = XCNEW (struct single_step_breakpoint);
2117 dest = (struct breakpoint *) ss_dest;
2118 /* Since single-step breakpoint is thread specific, don't copy
2119 thread id from SRC, use ID instead. */
2120 ss_dest->ptid = ptid;
2122 else
2123 gdb_assert_not_reached ("unhandled breakpoint type");
2125 dest->type = src->type;
2126 dest->raw = dest_raw;
2128 return dest;
2131 /* See mem-break.h. */
2133 void
2134 clone_all_breakpoints (struct thread_info *child_thread,
2135 const struct thread_info *parent_thread)
2137 const struct breakpoint *bp;
2138 struct breakpoint *new_bkpt;
2139 struct breakpoint *bkpt_tail = NULL;
2140 struct raw_breakpoint *raw_bkpt_tail = NULL;
2141 process_info *child_proc = child_thread->process ();
2142 process_info *parent_proc = parent_thread->process ();
2143 struct breakpoint **new_list = &child_proc->breakpoints;
2144 struct raw_breakpoint **new_raw_list = &child_proc->raw_breakpoints;
2146 for (bp = parent_proc->breakpoints; bp != NULL; bp = bp->next)
2148 new_bkpt = clone_one_breakpoint (bp, child_thread->id);
2149 APPEND_TO_LIST (new_list, new_bkpt, bkpt_tail);
2150 APPEND_TO_LIST (new_raw_list, new_bkpt->raw, raw_bkpt_tail);