Don't use free_contents in _bfd_elf_slurp_version_tables
[binutils-gdb.git] / gdbserver / mem-break.cc
blob3bee8bc89900fc578e7ea69b0e4e1ffea5a12120
1 /* Memory breakpoint operations for the remote server for GDB.
2 Copyright (C) 2002-2023 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 "server.h"
22 #include "regcache.h"
23 #include "ax.h"
25 #define MAX_BREAKPOINT_LEN 8
27 /* Helper macro used in loops that append multiple items to a singly-linked
28 list instead of inserting items at the head of the list, as, say, in the
29 breakpoint lists. LISTPP is a pointer to the pointer that is the head of
30 the new list. ITEMP is a pointer to the item to be added to the list.
31 TAILP must be defined to be the same type as ITEMP, and initialized to
32 NULL. */
34 #define APPEND_TO_LIST(listpp, itemp, tailp) \
35 do \
36 { \
37 if ((tailp) == NULL) \
38 *(listpp) = (itemp); \
39 else \
40 (tailp)->next = (itemp); \
41 (tailp) = (itemp); \
42 } \
43 while (0)
45 /* GDB will never try to install multiple breakpoints at the same
46 address. However, we can see GDB requesting to insert a breakpoint
47 at an address is had already inserted one previously in a few
48 situations.
50 - The RSP documentation on Z packets says that to avoid potential
51 problems with duplicate packets, the operations should be
52 implemented in an idempotent way.
54 - A breakpoint is set at ADDR, an address in a shared library.
55 Then the shared library is unloaded. And then another, unrelated,
56 breakpoint at ADDR is set. There is not breakpoint removal request
57 between the first and the second breakpoint.
59 - When GDB wants to update the target-side breakpoint conditions or
60 commands, it re-inserts the breakpoint, with updated
61 conditions/commands associated.
63 Also, we need to keep track of internal breakpoints too, so we do
64 need to be able to install multiple breakpoints at the same address
65 transparently.
67 We keep track of two different, and closely related structures. A
68 raw breakpoint, which manages the low level, close to the metal
69 aspect of a breakpoint. It holds the breakpoint address, and for
70 software breakpoints, a buffer holding a copy of the instructions
71 that would be in memory had not been a breakpoint there (we call
72 that the shadow memory of the breakpoint). We occasionally need to
73 temporarily uninsert a breakpoint without the client knowing about
74 it (e.g., to step over an internal breakpoint), so we keep an
75 `inserted' state associated with this low level breakpoint
76 structure. There can only be one such object for a given address.
77 Then, we have (a bit higher level) breakpoints. This structure
78 holds a callback to be called whenever a breakpoint is hit, a
79 high-level type, and a link to a low level raw breakpoint. There
80 can be many high-level breakpoints at the same address, and all of
81 them will point to the same raw breakpoint, which is reference
82 counted. */
84 /* The low level, physical, raw breakpoint. */
85 struct raw_breakpoint
87 struct raw_breakpoint *next;
89 /* The low level type of the breakpoint (software breakpoint,
90 watchpoint, etc.) */
91 enum raw_bkpt_type raw_type;
93 /* A reference count. Each high level breakpoint referencing this
94 raw breakpoint accounts for one reference. */
95 int refcount;
97 /* The breakpoint's insertion address. There can only be one raw
98 breakpoint for a given PC. */
99 CORE_ADDR pc;
101 /* The breakpoint's kind. This is target specific. Most
102 architectures only use one specific instruction for breakpoints, while
103 others may use more than one. E.g., on ARM, we need to use different
104 breakpoint instructions on Thumb, Thumb-2, and ARM code. Likewise for
105 hardware breakpoints -- some architectures (including ARM) need to
106 setup debug registers differently depending on mode. */
107 int kind;
109 /* The breakpoint's shadow memory. */
110 unsigned char old_data[MAX_BREAKPOINT_LEN];
112 /* Positive if this breakpoint is currently inserted in the
113 inferior. Negative if it was, but we've detected that it's now
114 gone. Zero if not inserted. */
115 int inserted;
118 /* The type of a breakpoint. */
119 enum bkpt_type
121 /* A GDB breakpoint, requested with a Z0 packet. */
122 gdb_breakpoint_Z0,
124 /* A GDB hardware breakpoint, requested with a Z1 packet. */
125 gdb_breakpoint_Z1,
127 /* A GDB write watchpoint, requested with a Z2 packet. */
128 gdb_breakpoint_Z2,
130 /* A GDB read watchpoint, requested with a Z3 packet. */
131 gdb_breakpoint_Z3,
133 /* A GDB access watchpoint, requested with a Z4 packet. */
134 gdb_breakpoint_Z4,
136 /* A software single-step breakpoint. */
137 single_step_breakpoint,
139 /* Any other breakpoint type that doesn't require specific
140 treatment goes here. E.g., an event breakpoint. */
141 other_breakpoint,
144 struct point_cond_list
146 /* Pointer to the agent expression that is the breakpoint's
147 conditional. */
148 struct agent_expr *cond;
150 /* Pointer to the next condition. */
151 struct point_cond_list *next;
154 struct point_command_list
156 /* Pointer to the agent expression that is the breakpoint's
157 commands. */
158 struct agent_expr *cmd;
160 /* Flag that is true if this command should run even while GDB is
161 disconnected. */
162 int persistence;
164 /* Pointer to the next command. */
165 struct point_command_list *next;
168 /* A high level (in gdbserver's perspective) breakpoint. */
169 struct breakpoint
171 struct breakpoint *next;
173 /* The breakpoint's type. */
174 enum bkpt_type type;
176 /* Link to this breakpoint's raw breakpoint. This is always
177 non-NULL. */
178 struct raw_breakpoint *raw;
181 /* Breakpoint requested by GDB. */
183 struct gdb_breakpoint
185 struct breakpoint base;
187 /* Pointer to the condition list that should be evaluated on
188 the target or NULL if the breakpoint is unconditional or
189 if GDB doesn't want us to evaluate the conditionals on the
190 target's side. */
191 struct point_cond_list *cond_list;
193 /* Point to the list of commands to run when this is hit. */
194 struct point_command_list *command_list;
197 /* Breakpoint used by GDBserver. */
199 struct other_breakpoint
201 struct breakpoint base;
203 /* Function to call when we hit this breakpoint. If it returns 1,
204 the breakpoint shall be deleted; 0 or if this callback is NULL,
205 it will be left inserted. */
206 int (*handler) (CORE_ADDR);
209 /* Breakpoint for single step. */
211 struct single_step_breakpoint
213 struct breakpoint base;
215 /* Thread the reinsert breakpoint belongs to. */
216 ptid_t ptid;
219 /* Return the breakpoint size from its kind. */
221 static int
222 bp_size (struct raw_breakpoint *bp)
224 int size = 0;
226 the_target->sw_breakpoint_from_kind (bp->kind, &size);
227 return size;
230 /* Return the breakpoint opcode from its kind. */
232 static const gdb_byte *
233 bp_opcode (struct raw_breakpoint *bp)
235 int size = 0;
237 return the_target->sw_breakpoint_from_kind (bp->kind, &size);
240 /* See mem-break.h. */
242 enum target_hw_bp_type
243 raw_bkpt_type_to_target_hw_bp_type (enum raw_bkpt_type raw_type)
245 switch (raw_type)
247 case raw_bkpt_type_hw:
248 return hw_execute;
249 case raw_bkpt_type_write_wp:
250 return hw_write;
251 case raw_bkpt_type_read_wp:
252 return hw_read;
253 case raw_bkpt_type_access_wp:
254 return hw_access;
255 default:
256 internal_error ("bad raw breakpoint type %d", (int) raw_type);
260 /* See mem-break.h. */
262 static enum bkpt_type
263 Z_packet_to_bkpt_type (char z_type)
265 gdb_assert ('0' <= z_type && z_type <= '4');
267 return (enum bkpt_type) (gdb_breakpoint_Z0 + (z_type - '0'));
270 /* See mem-break.h. */
272 enum raw_bkpt_type
273 Z_packet_to_raw_bkpt_type (char z_type)
275 switch (z_type)
277 case Z_PACKET_SW_BP:
278 return raw_bkpt_type_sw;
279 case Z_PACKET_HW_BP:
280 return raw_bkpt_type_hw;
281 case Z_PACKET_WRITE_WP:
282 return raw_bkpt_type_write_wp;
283 case Z_PACKET_READ_WP:
284 return raw_bkpt_type_read_wp;
285 case Z_PACKET_ACCESS_WP:
286 return raw_bkpt_type_access_wp;
287 default:
288 gdb_assert_not_reached ("unhandled Z packet type.");
292 /* Return true if breakpoint TYPE is a GDB breakpoint. */
294 static int
295 is_gdb_breakpoint (enum bkpt_type type)
297 return (type == gdb_breakpoint_Z0
298 || type == gdb_breakpoint_Z1
299 || type == gdb_breakpoint_Z2
300 || type == gdb_breakpoint_Z3
301 || type == gdb_breakpoint_Z4);
304 bool
305 any_persistent_commands (process_info *proc)
307 struct breakpoint *bp;
308 struct point_command_list *cl;
310 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
312 if (is_gdb_breakpoint (bp->type))
314 struct gdb_breakpoint *gdb_bp = (struct gdb_breakpoint *) bp;
316 for (cl = gdb_bp->command_list; cl != NULL; cl = cl->next)
317 if (cl->persistence)
318 return true;
322 return false;
325 /* Find low-level breakpoint of type TYPE at address ADDR that is not
326 insert-disabled. Returns NULL if not found. */
328 static struct raw_breakpoint *
329 find_enabled_raw_code_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type)
331 struct process_info *proc = current_process ();
332 struct raw_breakpoint *bp;
334 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
335 if (bp->pc == addr
336 && bp->raw_type == type
337 && bp->inserted >= 0)
338 return bp;
340 return NULL;
343 /* Find low-level breakpoint of type TYPE at address ADDR. Returns
344 NULL if not found. */
346 static struct raw_breakpoint *
347 find_raw_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type, int kind)
349 struct process_info *proc = current_process ();
350 struct raw_breakpoint *bp;
352 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
353 if (bp->pc == addr && bp->raw_type == type && bp->kind == kind)
354 return bp;
356 return NULL;
359 /* See mem-break.h. */
362 insert_memory_breakpoint (struct raw_breakpoint *bp)
364 unsigned char buf[MAX_BREAKPOINT_LEN];
365 int err;
367 /* Note that there can be fast tracepoint jumps installed in the
368 same memory range, so to get at the original memory, we need to
369 use read_inferior_memory, which masks those out. */
370 err = read_inferior_memory (bp->pc, buf, bp_size (bp));
371 if (err != 0)
373 threads_debug_printf ("Failed to read shadow memory of"
374 " breakpoint at 0x%s (%s).",
375 paddress (bp->pc), safe_strerror (err));
377 else
379 memcpy (bp->old_data, buf, bp_size (bp));
381 err = the_target->write_memory (bp->pc, bp_opcode (bp),
382 bp_size (bp));
383 if (err != 0)
384 threads_debug_printf ("Failed to insert breakpoint at 0x%s (%s).",
385 paddress (bp->pc), safe_strerror (err));
387 return err != 0 ? -1 : 0;
390 /* See mem-break.h */
393 remove_memory_breakpoint (struct raw_breakpoint *bp)
395 unsigned char buf[MAX_BREAKPOINT_LEN];
396 int err;
398 /* Since there can be trap breakpoints inserted in the same address
399 range, we use `target_write_memory', which takes care of
400 layering breakpoints on top of fast tracepoints, and on top of
401 the buffer we pass it. This works because the caller has already
402 either unlinked the breakpoint or marked it uninserted. Also
403 note that we need to pass the current shadow contents, because
404 target_write_memory updates any shadow memory with what we pass
405 here, and we want that to be a nop. */
406 memcpy (buf, bp->old_data, bp_size (bp));
407 err = target_write_memory (bp->pc, buf, bp_size (bp));
408 if (err != 0)
409 threads_debug_printf ("Failed to uninsert raw breakpoint "
410 "at 0x%s (%s) while deleting it.",
411 paddress (bp->pc), safe_strerror (err));
413 return err != 0 ? -1 : 0;
416 /* Set a RAW breakpoint of type TYPE and kind KIND at WHERE. On
417 success, a pointer to the new breakpoint is returned. On failure,
418 returns NULL and writes the error code to *ERR. */
420 static struct raw_breakpoint *
421 set_raw_breakpoint_at (enum raw_bkpt_type type, CORE_ADDR where, int kind,
422 int *err)
424 struct process_info *proc = current_process ();
425 struct raw_breakpoint *bp;
427 if (type == raw_bkpt_type_sw || type == raw_bkpt_type_hw)
429 bp = find_enabled_raw_code_breakpoint_at (where, type);
430 if (bp != NULL && bp->kind != kind)
432 /* A different kind than previously seen. The previous
433 breakpoint must be gone then. */
434 threads_debug_printf
435 ("Inconsistent breakpoint kind? Was %d, now %d.",
436 bp->kind, kind);
437 bp->inserted = -1;
438 bp = NULL;
441 else
442 bp = find_raw_breakpoint_at (where, type, kind);
444 gdb::unique_xmalloc_ptr<struct raw_breakpoint> bp_holder;
445 if (bp == NULL)
447 bp_holder.reset (XCNEW (struct raw_breakpoint));
448 bp = bp_holder.get ();
449 bp->pc = where;
450 bp->kind = kind;
451 bp->raw_type = type;
454 if (!bp->inserted)
456 *err = the_target->insert_point (bp->raw_type, bp->pc, bp->kind, bp);
457 if (*err != 0)
459 threads_debug_printf ("Failed to insert breakpoint at 0x%s (%d).",
460 paddress (where), *err);
462 return NULL;
465 bp->inserted = 1;
468 /* If the breakpoint was allocated above, we know we want to keep it
469 now. */
470 bp_holder.release ();
472 /* Link the breakpoint in, if this is the first reference. */
473 if (++bp->refcount == 1)
475 bp->next = proc->raw_breakpoints;
476 proc->raw_breakpoints = bp;
478 return bp;
481 /* Notice that breakpoint traps are always installed on top of fast
482 tracepoint jumps. This is even if the fast tracepoint is installed
483 at a later time compared to when the breakpoint was installed.
484 This means that a stopping breakpoint or tracepoint has higher
485 "priority". In turn, this allows having fast and slow tracepoints
486 (and breakpoints) at the same address behave correctly. */
489 /* A fast tracepoint jump. */
491 struct fast_tracepoint_jump
493 struct fast_tracepoint_jump *next;
495 /* A reference count. GDB can install more than one fast tracepoint
496 at the same address (each with its own action list, for
497 example). */
498 int refcount;
500 /* The fast tracepoint's insertion address. There can only be one
501 of these for a given PC. */
502 CORE_ADDR pc;
504 /* Non-zero if this fast tracepoint jump is currently inserted in
505 the inferior. */
506 int inserted;
508 /* The length of the jump instruction. */
509 int length;
511 /* A poor-man's flexible array member, holding both the jump
512 instruction to insert, and a copy of the instruction that would
513 be in memory had not been a jump there (the shadow memory of the
514 tracepoint jump). */
515 unsigned char insn_and_shadow[0];
518 /* Fast tracepoint FP's jump instruction to insert. */
519 #define fast_tracepoint_jump_insn(fp) \
520 ((fp)->insn_and_shadow + 0)
522 /* The shadow memory of fast tracepoint jump FP. */
523 #define fast_tracepoint_jump_shadow(fp) \
524 ((fp)->insn_and_shadow + (fp)->length)
527 /* Return the fast tracepoint jump set at WHERE. */
529 static struct fast_tracepoint_jump *
530 find_fast_tracepoint_jump_at (CORE_ADDR where)
532 struct process_info *proc = current_process ();
533 struct fast_tracepoint_jump *jp;
535 for (jp = proc->fast_tracepoint_jumps; jp != NULL; jp = jp->next)
536 if (jp->pc == where)
537 return jp;
539 return NULL;
543 fast_tracepoint_jump_here (CORE_ADDR where)
545 struct fast_tracepoint_jump *jp = find_fast_tracepoint_jump_at (where);
547 return (jp != NULL);
551 delete_fast_tracepoint_jump (struct fast_tracepoint_jump *todel)
553 struct fast_tracepoint_jump *bp, **bp_link;
554 int ret;
555 struct process_info *proc = current_process ();
557 bp = proc->fast_tracepoint_jumps;
558 bp_link = &proc->fast_tracepoint_jumps;
560 while (bp)
562 if (bp == todel)
564 if (--bp->refcount == 0)
566 struct fast_tracepoint_jump *prev_bp_link = *bp_link;
567 unsigned char *buf;
569 /* Unlink it. */
570 *bp_link = bp->next;
572 /* Since there can be breakpoints inserted in the same
573 address range, we use `target_write_memory', which
574 takes care of layering breakpoints on top of fast
575 tracepoints, and on top of the buffer we pass it.
576 This works because we've already unlinked the fast
577 tracepoint jump above. Also note that we need to
578 pass the current shadow contents, because
579 target_write_memory updates any shadow memory with
580 what we pass here, and we want that to be a nop. */
581 buf = (unsigned char *) alloca (bp->length);
582 memcpy (buf, fast_tracepoint_jump_shadow (bp), bp->length);
583 ret = target_write_memory (bp->pc, buf, bp->length);
584 if (ret != 0)
586 /* Something went wrong, relink the jump. */
587 *bp_link = prev_bp_link;
589 threads_debug_printf
590 ("Failed to uninsert fast tracepoint jump "
591 "at 0x%s (%s) while deleting it.",
592 paddress (bp->pc), safe_strerror (ret));
593 return ret;
596 free (bp);
599 return 0;
601 else
603 bp_link = &bp->next;
604 bp = *bp_link;
608 warning ("Could not find fast tracepoint jump in list.");
609 return ENOENT;
612 void
613 inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump *jp)
615 jp->refcount++;
618 struct fast_tracepoint_jump *
619 set_fast_tracepoint_jump (CORE_ADDR where,
620 unsigned char *insn, ULONGEST length)
622 struct process_info *proc = current_process ();
623 struct fast_tracepoint_jump *jp;
624 int err;
625 unsigned char *buf;
627 /* We refcount fast tracepoint jumps. Check if we already know
628 about a jump at this address. */
629 jp = find_fast_tracepoint_jump_at (where);
630 if (jp != NULL)
632 jp->refcount++;
633 return jp;
636 /* We don't, so create a new object. Double the length, because the
637 flexible array member holds both the jump insn, and the
638 shadow. */
639 jp = (struct fast_tracepoint_jump *) xcalloc (1, sizeof (*jp) + (length * 2));
640 jp->pc = where;
641 jp->length = length;
642 memcpy (fast_tracepoint_jump_insn (jp), insn, length);
643 jp->refcount = 1;
644 buf = (unsigned char *) alloca (length);
646 /* Note that there can be trap breakpoints inserted in the same
647 address range. To access the original memory contents, we use
648 `read_inferior_memory', which masks out breakpoints. */
649 err = read_inferior_memory (where, buf, length);
650 if (err != 0)
652 threads_debug_printf ("Failed to read shadow memory of"
653 " fast tracepoint at 0x%s (%s).",
654 paddress (where), safe_strerror (err));
655 free (jp);
656 return NULL;
658 memcpy (fast_tracepoint_jump_shadow (jp), buf, length);
660 /* Link the jump in. */
661 jp->inserted = 1;
662 jp->next = proc->fast_tracepoint_jumps;
663 proc->fast_tracepoint_jumps = jp;
665 /* Since there can be trap breakpoints inserted in the same address
666 range, we use use `target_write_memory', which takes care of
667 layering breakpoints on top of fast tracepoints, on top of the
668 buffer we pass it. This works because we've already linked in
669 the fast tracepoint jump above. Also note that we need to pass
670 the current shadow contents, because target_write_memory
671 updates any shadow memory with what we pass here, and we want
672 that to be a nop. */
673 err = target_write_memory (where, buf, length);
674 if (err != 0)
676 threads_debug_printf
677 ("Failed to insert fast tracepoint jump at 0x%s (%s).",
678 paddress (where), safe_strerror (err));
680 /* Unlink it. */
681 proc->fast_tracepoint_jumps = jp->next;
682 free (jp);
684 return NULL;
687 return jp;
690 void
691 uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc)
693 struct fast_tracepoint_jump *jp;
694 int err;
696 jp = find_fast_tracepoint_jump_at (pc);
697 if (jp == NULL)
699 /* This can happen when we remove all breakpoints while handling
700 a step-over. */
701 threads_debug_printf ("Could not find fast tracepoint jump at 0x%s "
702 "in list (uninserting).",
703 paddress (pc));
704 return;
707 if (jp->inserted)
709 unsigned char *buf;
711 jp->inserted = 0;
713 /* Since there can be trap breakpoints inserted in the same
714 address range, we use use `target_write_memory', which
715 takes care of layering breakpoints on top of fast
716 tracepoints, and on top of the buffer we pass it. This works
717 because we've already marked the fast tracepoint fast
718 tracepoint jump uninserted above. Also note that we need to
719 pass the current shadow contents, because
720 target_write_memory updates any shadow memory with what we
721 pass here, and we want that to be a nop. */
722 buf = (unsigned char *) alloca (jp->length);
723 memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
724 err = target_write_memory (jp->pc, buf, jp->length);
725 if (err != 0)
727 jp->inserted = 1;
729 threads_debug_printf ("Failed to uninsert fast tracepoint jump at"
730 " 0x%s (%s).",
731 paddress (pc), safe_strerror (err));
736 void
737 reinsert_fast_tracepoint_jumps_at (CORE_ADDR where)
739 struct fast_tracepoint_jump *jp;
740 int err;
741 unsigned char *buf;
743 jp = find_fast_tracepoint_jump_at (where);
744 if (jp == NULL)
746 /* This can happen when we remove breakpoints when a tracepoint
747 hit causes a tracing stop, while handling a step-over. */
748 threads_debug_printf ("Could not find fast tracepoint jump at 0x%s "
749 "in list (reinserting).",
750 paddress (where));
751 return;
754 if (jp->inserted)
755 error ("Jump already inserted at reinsert time.");
757 jp->inserted = 1;
759 /* Since there can be trap breakpoints inserted in the same address
760 range, we use `target_write_memory', which takes care of
761 layering breakpoints on top of fast tracepoints, and on top of
762 the buffer we pass it. This works because we've already marked
763 the fast tracepoint jump inserted above. Also note that we need
764 to pass the current shadow contents, because
765 target_write_memory updates any shadow memory with what we pass
766 here, and we want that to be a nop. */
767 buf = (unsigned char *) alloca (jp->length);
768 memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
769 err = target_write_memory (where, buf, jp->length);
770 if (err != 0)
772 jp->inserted = 0;
774 threads_debug_printf ("Failed to reinsert fast tracepoint jump at"
775 " 0x%s (%s).",
776 paddress (where), safe_strerror (err));
780 /* Set a high-level breakpoint of type TYPE, with low level type
781 RAW_TYPE and kind KIND, at WHERE. On success, a pointer to the new
782 breakpoint is returned. On failure, returns NULL and writes the
783 error code to *ERR. HANDLER is called when the breakpoint is hit.
784 HANDLER should return 1 if the breakpoint should be deleted, 0
785 otherwise. */
787 static struct breakpoint *
788 set_breakpoint (enum bkpt_type type, enum raw_bkpt_type raw_type,
789 CORE_ADDR where, int kind,
790 int (*handler) (CORE_ADDR), int *err)
792 struct process_info *proc = current_process ();
793 struct breakpoint *bp;
794 struct raw_breakpoint *raw;
796 raw = set_raw_breakpoint_at (raw_type, where, kind, err);
798 if (raw == NULL)
800 /* warn? */
801 return NULL;
804 if (is_gdb_breakpoint (type))
806 struct gdb_breakpoint *gdb_bp = XCNEW (struct gdb_breakpoint);
808 bp = (struct breakpoint *) gdb_bp;
809 gdb_assert (handler == NULL);
811 else if (type == other_breakpoint)
813 struct other_breakpoint *other_bp = XCNEW (struct other_breakpoint);
815 other_bp->handler = handler;
816 bp = (struct breakpoint *) other_bp;
818 else if (type == single_step_breakpoint)
820 struct single_step_breakpoint *ss_bp
821 = XCNEW (struct single_step_breakpoint);
823 bp = (struct breakpoint *) ss_bp;
825 else
826 gdb_assert_not_reached ("unhandled breakpoint type");
828 bp->type = type;
829 bp->raw = raw;
831 bp->next = proc->breakpoints;
832 proc->breakpoints = bp;
834 return bp;
837 /* Set breakpoint of TYPE on address WHERE with handler HANDLER. */
839 static struct breakpoint *
840 set_breakpoint_type_at (enum bkpt_type type, CORE_ADDR where,
841 int (*handler) (CORE_ADDR))
843 int err_ignored;
844 CORE_ADDR placed_address = where;
845 int breakpoint_kind = target_breakpoint_kind_from_pc (&placed_address);
847 return set_breakpoint (type, raw_bkpt_type_sw,
848 placed_address, breakpoint_kind, handler,
849 &err_ignored);
852 /* See mem-break.h */
854 struct breakpoint *
855 set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
857 return set_breakpoint_type_at (other_breakpoint, where, handler);
861 static int
862 delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
864 struct raw_breakpoint *bp, **bp_link;
865 int ret;
867 bp = proc->raw_breakpoints;
868 bp_link = &proc->raw_breakpoints;
870 while (bp)
872 if (bp == todel)
874 if (bp->inserted > 0)
876 struct raw_breakpoint *prev_bp_link = *bp_link;
878 *bp_link = bp->next;
880 ret = the_target->remove_point (bp->raw_type, bp->pc,
881 bp->kind, bp);
882 if (ret != 0)
884 /* Something went wrong, relink the breakpoint. */
885 *bp_link = prev_bp_link;
887 threads_debug_printf ("Failed to uninsert raw breakpoint "
888 "at 0x%s while deleting it.",
889 paddress (bp->pc));
890 return ret;
893 else
894 *bp_link = bp->next;
896 free (bp);
897 return 0;
899 else
901 bp_link = &bp->next;
902 bp = *bp_link;
906 warning ("Could not find raw breakpoint in list.");
907 return ENOENT;
910 static int
911 release_breakpoint (struct process_info *proc, struct breakpoint *bp)
913 int newrefcount;
914 int ret;
916 newrefcount = bp->raw->refcount - 1;
917 if (newrefcount == 0)
919 ret = delete_raw_breakpoint (proc, bp->raw);
920 if (ret != 0)
921 return ret;
923 else
924 bp->raw->refcount = newrefcount;
926 free (bp);
928 return 0;
931 static int
932 delete_breakpoint_1 (struct process_info *proc, struct breakpoint *todel)
934 struct breakpoint *bp, **bp_link;
935 int err;
937 bp = proc->breakpoints;
938 bp_link = &proc->breakpoints;
940 while (bp)
942 if (bp == todel)
944 *bp_link = bp->next;
946 err = release_breakpoint (proc, bp);
947 if (err != 0)
948 return err;
950 bp = *bp_link;
951 return 0;
953 else
955 bp_link = &bp->next;
956 bp = *bp_link;
960 warning ("Could not find breakpoint in list.");
961 return ENOENT;
965 delete_breakpoint (struct breakpoint *todel)
967 struct process_info *proc = current_process ();
968 return delete_breakpoint_1 (proc, todel);
971 /* Locate a GDB breakpoint of type Z_TYPE and kind KIND placed at
972 address ADDR and return a pointer to its structure. If KIND is -1,
973 the breakpoint's kind is ignored. */
975 static struct gdb_breakpoint *
976 find_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind)
978 struct process_info *proc = current_process ();
980 /* In some situations the current process exits, we inform GDB, but
981 before GDB can acknowledge that the process has exited GDB tries to
982 detach from the inferior. As part of the detach process GDB will
983 remove all breakpoints, which means we can end up here when the
984 current process has already exited and so PROC is nullptr. In this
985 case just claim we can't find (and so delete) the breakpoint, GDB
986 will ignore this error during detach. */
987 if (proc == nullptr)
988 return nullptr;
990 struct breakpoint *bp;
991 enum bkpt_type type = Z_packet_to_bkpt_type (z_type);
993 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
994 if (bp->type == type && bp->raw->pc == addr
995 && (kind == -1 || bp->raw->kind == kind))
996 return (struct gdb_breakpoint *) bp;
998 return NULL;
1001 static int
1002 z_type_supported (char z_type)
1004 return (z_type >= '0' && z_type <= '4'
1005 && the_target->supports_z_point_type (z_type));
1008 /* Create a new GDB breakpoint of type Z_TYPE at ADDR with kind KIND.
1009 Returns a pointer to the newly created breakpoint on success. On
1010 failure returns NULL and sets *ERR to either -1 for error, or 1 if
1011 Z_TYPE breakpoints are not supported on this target. */
1013 struct gdb_breakpoint *
1014 set_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind, int *err)
1016 struct gdb_breakpoint *bp;
1017 enum bkpt_type type;
1018 enum raw_bkpt_type raw_type;
1020 if (!z_type_supported (z_type))
1022 *err = 1;
1023 return nullptr;
1026 /* If we see GDB inserting a second code breakpoint at the same
1027 address, then either: GDB is updating the breakpoint's conditions
1028 or commands; or, the first breakpoint must have disappeared due
1029 to a shared library unload. On targets where the shared
1030 libraries are handled by userspace, like SVR4, for example,
1031 GDBserver can't tell if a library was loaded or unloaded. Since
1032 we refcount raw breakpoints, we must be careful to make sure GDB
1033 breakpoints never contribute more than one reference. if we
1034 didn't do this, in case the previous breakpoint is gone due to a
1035 shared library unload, we'd just increase the refcount of the
1036 previous breakpoint at this address, but the trap was not planted
1037 in the inferior anymore, thus the breakpoint would never be hit.
1038 Note this must be careful to not create a window where
1039 breakpoints are removed from the target, for non-stop, in case
1040 the target can poke at memory while the program is running. */
1041 if (z_type == Z_PACKET_SW_BP
1042 || z_type == Z_PACKET_HW_BP)
1044 bp = find_gdb_breakpoint (z_type, addr, -1);
1046 if (bp != NULL)
1048 if (bp->base.raw->kind != kind)
1050 /* A different kind than previously seen. The previous
1051 breakpoint must be gone then. */
1052 bp->base.raw->inserted = -1;
1053 delete_breakpoint ((struct breakpoint *) bp);
1054 bp = NULL;
1056 else if (z_type == Z_PACKET_SW_BP)
1058 /* Check if the breakpoint is actually gone from the
1059 target, due to an solib unload, for example. Might
1060 as well validate _all_ breakpoints. */
1061 validate_breakpoints ();
1063 /* Breakpoints that don't pass validation are
1064 deleted. */
1065 bp = find_gdb_breakpoint (z_type, addr, -1);
1069 else
1071 /* Data breakpoints for the same address but different kind are
1072 expected. GDB doesn't merge these. The backend gets to do
1073 that if it wants/can. */
1074 bp = find_gdb_breakpoint (z_type, addr, kind);
1077 if (bp != NULL)
1079 /* We already know about this breakpoint, there's nothing else
1080 to do - GDB's reference is already accounted for. Note that
1081 whether the breakpoint inserted is left as is - we may be
1082 stepping over it, for example, in which case we don't want to
1083 force-reinsert it. */
1084 return bp;
1087 raw_type = Z_packet_to_raw_bkpt_type (z_type);
1088 type = Z_packet_to_bkpt_type (z_type);
1089 return (struct gdb_breakpoint *) set_breakpoint (type, raw_type, addr,
1090 kind, NULL, err);
1093 /* Delete a GDB breakpoint of type Z_TYPE and kind KIND previously
1094 inserted at ADDR with set_gdb_breakpoint_at. Returns 0 on success,
1095 -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
1096 target. */
1099 delete_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind)
1101 if (!z_type_supported (z_type))
1102 return 1;
1104 gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, kind);
1105 if (bp == NULL)
1106 return -1;
1108 /* Before deleting the breakpoint, make sure to free its condition
1109 and command lists. */
1110 clear_breakpoint_conditions_and_commands (bp);
1111 int err = delete_breakpoint ((struct breakpoint *) bp);
1112 if (err != 0)
1113 return -1;
1115 return 0;
1118 /* Clear all conditions associated with a breakpoint. */
1120 static void
1121 clear_breakpoint_conditions (struct gdb_breakpoint *bp)
1123 struct point_cond_list *cond;
1125 if (bp->cond_list == NULL)
1126 return;
1128 cond = bp->cond_list;
1130 while (cond != NULL)
1132 struct point_cond_list *cond_next;
1134 cond_next = cond->next;
1135 gdb_free_agent_expr (cond->cond);
1136 free (cond);
1137 cond = cond_next;
1140 bp->cond_list = NULL;
1143 /* Clear all commands associated with a breakpoint. */
1145 static void
1146 clear_breakpoint_commands (struct gdb_breakpoint *bp)
1148 struct point_command_list *cmd;
1150 if (bp->command_list == NULL)
1151 return;
1153 cmd = bp->command_list;
1155 while (cmd != NULL)
1157 struct point_command_list *cmd_next;
1159 cmd_next = cmd->next;
1160 gdb_free_agent_expr (cmd->cmd);
1161 free (cmd);
1162 cmd = cmd_next;
1165 bp->command_list = NULL;
1168 void
1169 clear_breakpoint_conditions_and_commands (struct gdb_breakpoint *bp)
1171 clear_breakpoint_conditions (bp);
1172 clear_breakpoint_commands (bp);
1175 /* Add condition CONDITION to GDBserver's breakpoint BP. */
1177 static void
1178 add_condition_to_breakpoint (struct gdb_breakpoint *bp,
1179 struct agent_expr *condition)
1181 struct point_cond_list *new_cond;
1183 /* Create new condition. */
1184 new_cond = XCNEW (struct point_cond_list);
1185 new_cond->cond = condition;
1187 /* Add condition to the list. */
1188 new_cond->next = bp->cond_list;
1189 bp->cond_list = new_cond;
1192 /* Add a target-side condition CONDITION to a breakpoint. */
1195 add_breakpoint_condition (struct gdb_breakpoint *bp, const char **condition)
1197 const char *actparm = *condition;
1198 struct agent_expr *cond;
1200 if (condition == NULL)
1201 return 1;
1203 if (bp == NULL)
1204 return 0;
1206 cond = gdb_parse_agent_expr (&actparm);
1208 if (cond == NULL)
1210 warning ("Condition evaluation failed. Assuming unconditional.");
1211 return 0;
1214 add_condition_to_breakpoint (bp, cond);
1216 *condition = actparm;
1218 return 1;
1221 /* Evaluate condition (if any) at breakpoint BP. Return 1 if
1222 true and 0 otherwise. */
1224 static int
1225 gdb_condition_true_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
1227 /* Fetch registers for the current inferior. */
1228 struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1229 ULONGEST value = 0;
1230 struct point_cond_list *cl;
1231 int err = 0;
1232 struct eval_agent_expr_context ctx;
1234 if (bp == NULL)
1235 return 0;
1237 /* Check if the breakpoint is unconditional. If it is,
1238 the condition always evaluates to TRUE. */
1239 if (bp->cond_list == NULL)
1240 return 1;
1242 ctx.regcache = get_thread_regcache (current_thread, 1);
1243 ctx.tframe = NULL;
1244 ctx.tpoint = NULL;
1246 /* Evaluate each condition in the breakpoint's list of conditions.
1247 Return true if any of the conditions evaluates to TRUE.
1249 If we failed to evaluate the expression, TRUE is returned. This
1250 forces GDB to reevaluate the conditions. */
1251 for (cl = bp->cond_list;
1252 cl && !value && !err; cl = cl->next)
1254 /* Evaluate the condition. */
1255 err = gdb_eval_agent_expr (&ctx, cl->cond, &value);
1258 if (err)
1259 return 1;
1261 return (value != 0);
1265 gdb_condition_true_at_breakpoint (CORE_ADDR where)
1267 /* Only check code (software or hardware) breakpoints. */
1268 return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
1269 || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
1272 /* Add commands COMMANDS to GDBserver's breakpoint BP. */
1274 static void
1275 add_commands_to_breakpoint (struct gdb_breakpoint *bp,
1276 struct agent_expr *commands, int persist)
1278 struct point_command_list *new_cmd;
1280 /* Create new command. */
1281 new_cmd = XCNEW (struct point_command_list);
1282 new_cmd->cmd = commands;
1283 new_cmd->persistence = persist;
1285 /* Add commands to the list. */
1286 new_cmd->next = bp->command_list;
1287 bp->command_list = new_cmd;
1290 /* Add a target-side command COMMAND to the breakpoint at ADDR. */
1293 add_breakpoint_commands (struct gdb_breakpoint *bp, const char **command,
1294 int persist)
1296 const char *actparm = *command;
1297 struct agent_expr *cmd;
1299 if (command == NULL)
1300 return 1;
1302 if (bp == NULL)
1303 return 0;
1305 cmd = gdb_parse_agent_expr (&actparm);
1307 if (cmd == NULL)
1309 warning ("Command evaluation failed. Disabling.");
1310 return 0;
1313 add_commands_to_breakpoint (bp, cmd, persist);
1315 *command = actparm;
1317 return 1;
1320 /* Return true if there are no commands to run at this location,
1321 which likely means we want to report back to GDB. */
1323 static int
1324 gdb_no_commands_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
1326 struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1328 if (bp == NULL)
1329 return 1;
1331 threads_debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s",
1332 paddress (addr), z_type,
1333 phex_nz ((uintptr_t) bp->command_list, 0));
1334 return (bp->command_list == NULL);
1337 /* Return true if there are no commands to run at this location,
1338 which likely means we want to report back to GDB. */
1341 gdb_no_commands_at_breakpoint (CORE_ADDR where)
1343 /* Only check code (software or hardware) breakpoints. */
1344 return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
1345 && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
1348 /* Run a breakpoint's commands. Returns 0 if there was a problem
1349 running any command, 1 otherwise. */
1351 static int
1352 run_breakpoint_commands_z_type (char z_type, CORE_ADDR addr)
1354 /* Fetch registers for the current inferior. */
1355 struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1356 ULONGEST value = 0;
1357 struct point_command_list *cl;
1358 int err = 0;
1359 struct eval_agent_expr_context ctx;
1361 if (bp == NULL)
1362 return 1;
1364 ctx.regcache = get_thread_regcache (current_thread, 1);
1365 ctx.tframe = NULL;
1366 ctx.tpoint = NULL;
1368 for (cl = bp->command_list;
1369 cl && !value && !err; cl = cl->next)
1371 /* Run the command. */
1372 err = gdb_eval_agent_expr (&ctx, cl->cmd, &value);
1374 /* If one command has a problem, stop digging the hole deeper. */
1375 if (err)
1376 return 0;
1379 return 1;
1382 void
1383 run_breakpoint_commands (CORE_ADDR where)
1385 /* Only check code (software or hardware) breakpoints. If one
1386 command has a problem, stop digging the hole deeper. */
1387 if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP, where))
1388 run_breakpoint_commands_z_type (Z_PACKET_HW_BP, where);
1391 /* See mem-break.h. */
1394 gdb_breakpoint_here (CORE_ADDR where)
1396 /* Only check code (software or hardware) breakpoints. */
1397 return (find_gdb_breakpoint (Z_PACKET_SW_BP, where, -1) != NULL
1398 || find_gdb_breakpoint (Z_PACKET_HW_BP, where, -1) != NULL);
1401 void
1402 set_single_step_breakpoint (CORE_ADDR stop_at, ptid_t ptid)
1404 struct single_step_breakpoint *bp;
1406 gdb_assert (current_ptid.pid () == ptid.pid ());
1408 bp = (struct single_step_breakpoint *) set_breakpoint_type_at (single_step_breakpoint,
1409 stop_at, NULL);
1410 bp->ptid = ptid;
1413 void
1414 delete_single_step_breakpoints (struct thread_info *thread)
1416 struct process_info *proc = get_thread_process (thread);
1417 struct breakpoint *bp, **bp_link;
1419 bp = proc->breakpoints;
1420 bp_link = &proc->breakpoints;
1422 while (bp)
1424 if (bp->type == single_step_breakpoint
1425 && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1427 scoped_restore_current_thread restore_thread;
1429 switch_to_thread (thread);
1430 *bp_link = bp->next;
1431 release_breakpoint (proc, bp);
1432 bp = *bp_link;
1434 else
1436 bp_link = &bp->next;
1437 bp = *bp_link;
1442 static void
1443 uninsert_raw_breakpoint (struct raw_breakpoint *bp)
1445 if (bp->inserted < 0)
1447 threads_debug_printf ("Breakpoint at %s is marked insert-disabled.",
1448 paddress (bp->pc));
1450 else if (bp->inserted > 0)
1452 int err;
1454 bp->inserted = 0;
1456 err = the_target->remove_point (bp->raw_type, bp->pc, bp->kind, bp);
1457 if (err != 0)
1459 bp->inserted = 1;
1461 threads_debug_printf ("Failed to uninsert raw breakpoint at 0x%s.",
1462 paddress (bp->pc));
1467 void
1468 uninsert_breakpoints_at (CORE_ADDR pc)
1470 struct process_info *proc = current_process ();
1471 struct raw_breakpoint *bp;
1472 int found = 0;
1474 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1475 if ((bp->raw_type == raw_bkpt_type_sw
1476 || bp->raw_type == raw_bkpt_type_hw)
1477 && bp->pc == pc)
1479 found = 1;
1481 if (bp->inserted)
1482 uninsert_raw_breakpoint (bp);
1485 if (!found)
1487 /* This can happen when we remove all breakpoints while handling
1488 a step-over. */
1489 threads_debug_printf ("Could not find breakpoint at 0x%s "
1490 "in list (uninserting).",
1491 paddress (pc));
1495 void
1496 uninsert_all_breakpoints (void)
1498 struct process_info *proc = current_process ();
1499 struct raw_breakpoint *bp;
1501 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1502 if ((bp->raw_type == raw_bkpt_type_sw
1503 || bp->raw_type == raw_bkpt_type_hw)
1504 && bp->inserted)
1505 uninsert_raw_breakpoint (bp);
1508 void
1509 uninsert_single_step_breakpoints (struct thread_info *thread)
1511 struct process_info *proc = get_thread_process (thread);
1512 struct breakpoint *bp;
1514 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1516 if (bp->type == single_step_breakpoint
1517 && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1519 gdb_assert (bp->raw->inserted > 0);
1521 /* Only uninsert the raw breakpoint if it only belongs to a
1522 reinsert breakpoint. */
1523 if (bp->raw->refcount == 1)
1525 scoped_restore_current_thread restore_thread;
1527 switch_to_thread (thread);
1528 uninsert_raw_breakpoint (bp->raw);
1534 static void
1535 reinsert_raw_breakpoint (struct raw_breakpoint *bp)
1537 int err;
1539 if (bp->inserted)
1540 return;
1542 err = the_target->insert_point (bp->raw_type, bp->pc, bp->kind, bp);
1543 if (err == 0)
1544 bp->inserted = 1;
1545 else
1546 threads_debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).",
1547 paddress (bp->pc), err);
1550 void
1551 reinsert_breakpoints_at (CORE_ADDR pc)
1553 struct process_info *proc = current_process ();
1554 struct raw_breakpoint *bp;
1555 int found = 0;
1557 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1558 if ((bp->raw_type == raw_bkpt_type_sw
1559 || bp->raw_type == raw_bkpt_type_hw)
1560 && bp->pc == pc)
1562 found = 1;
1564 reinsert_raw_breakpoint (bp);
1567 if (!found)
1569 /* This can happen when we remove all breakpoints while handling
1570 a step-over. */
1571 threads_debug_printf ("Could not find raw breakpoint at 0x%s "
1572 "in list (reinserting).",
1573 paddress (pc));
1578 has_single_step_breakpoints (struct thread_info *thread)
1580 struct process_info *proc = get_thread_process (thread);
1581 struct breakpoint *bp, **bp_link;
1583 bp = proc->breakpoints;
1584 bp_link = &proc->breakpoints;
1586 while (bp)
1588 if (bp->type == single_step_breakpoint
1589 && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1590 return 1;
1591 else
1593 bp_link = &bp->next;
1594 bp = *bp_link;
1598 return 0;
1601 void
1602 reinsert_all_breakpoints (void)
1604 struct process_info *proc = current_process ();
1605 struct raw_breakpoint *bp;
1607 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1608 if ((bp->raw_type == raw_bkpt_type_sw
1609 || bp->raw_type == raw_bkpt_type_hw)
1610 && !bp->inserted)
1611 reinsert_raw_breakpoint (bp);
1614 void
1615 reinsert_single_step_breakpoints (struct thread_info *thread)
1617 struct process_info *proc = get_thread_process (thread);
1618 struct breakpoint *bp;
1620 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1622 if (bp->type == single_step_breakpoint
1623 && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1625 gdb_assert (bp->raw->inserted > 0);
1627 if (bp->raw->refcount == 1)
1629 scoped_restore_current_thread restore_thread;
1631 switch_to_thread (thread);
1632 reinsert_raw_breakpoint (bp->raw);
1638 void
1639 check_breakpoints (CORE_ADDR stop_pc)
1641 struct process_info *proc = current_process ();
1642 struct breakpoint *bp, **bp_link;
1644 bp = proc->breakpoints;
1645 bp_link = &proc->breakpoints;
1647 while (bp)
1649 struct raw_breakpoint *raw = bp->raw;
1651 if ((raw->raw_type == raw_bkpt_type_sw
1652 || raw->raw_type == raw_bkpt_type_hw)
1653 && raw->pc == stop_pc)
1655 if (!raw->inserted)
1657 warning ("Hit a removed breakpoint?");
1658 return;
1661 if (bp->type == other_breakpoint)
1663 struct other_breakpoint *other_bp
1664 = (struct other_breakpoint *) bp;
1666 if (other_bp->handler != NULL && (*other_bp->handler) (stop_pc))
1668 *bp_link = bp->next;
1670 release_breakpoint (proc, bp);
1672 bp = *bp_link;
1673 continue;
1678 bp_link = &bp->next;
1679 bp = *bp_link;
1684 breakpoint_here (CORE_ADDR addr)
1686 struct process_info *proc = current_process ();
1687 struct raw_breakpoint *bp;
1689 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1690 if ((bp->raw_type == raw_bkpt_type_sw
1691 || bp->raw_type == raw_bkpt_type_hw)
1692 && bp->pc == addr)
1693 return 1;
1695 return 0;
1699 breakpoint_inserted_here (CORE_ADDR addr)
1701 struct process_info *proc = current_process ();
1702 struct raw_breakpoint *bp;
1704 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1705 if ((bp->raw_type == raw_bkpt_type_sw
1706 || bp->raw_type == raw_bkpt_type_hw)
1707 && bp->pc == addr
1708 && bp->inserted)
1709 return 1;
1711 return 0;
1714 /* See mem-break.h. */
1717 software_breakpoint_inserted_here (CORE_ADDR addr)
1719 struct process_info *proc = current_process ();
1720 struct raw_breakpoint *bp;
1722 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1723 if (bp->raw_type == raw_bkpt_type_sw
1724 && bp->pc == addr
1725 && bp->inserted)
1726 return 1;
1728 return 0;
1731 /* See mem-break.h. */
1734 hardware_breakpoint_inserted_here (CORE_ADDR addr)
1736 struct process_info *proc = current_process ();
1737 struct raw_breakpoint *bp;
1739 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1740 if (bp->raw_type == raw_bkpt_type_hw
1741 && bp->pc == addr
1742 && bp->inserted)
1743 return 1;
1745 return 0;
1748 /* See mem-break.h. */
1751 single_step_breakpoint_inserted_here (CORE_ADDR addr)
1753 struct process_info *proc = current_process ();
1754 struct breakpoint *bp;
1756 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1757 if (bp->type == single_step_breakpoint
1758 && bp->raw->pc == addr
1759 && bp->raw->inserted)
1760 return 1;
1762 return 0;
1765 static int
1766 validate_inserted_breakpoint (struct raw_breakpoint *bp)
1768 unsigned char *buf;
1769 int err;
1771 gdb_assert (bp->inserted);
1772 gdb_assert (bp->raw_type == raw_bkpt_type_sw);
1774 buf = (unsigned char *) alloca (bp_size (bp));
1775 err = the_target->read_memory (bp->pc, buf, bp_size (bp));
1776 if (err || memcmp (buf, bp_opcode (bp), bp_size (bp)) != 0)
1778 /* Tag it as gone. */
1779 bp->inserted = -1;
1780 return 0;
1783 return 1;
1786 static void
1787 delete_disabled_breakpoints (void)
1789 struct process_info *proc = current_process ();
1790 struct breakpoint *bp, *next;
1792 for (bp = proc->breakpoints; bp != NULL; bp = next)
1794 next = bp->next;
1795 if (bp->raw->inserted < 0)
1797 /* If single_step_breakpoints become disabled, that means the
1798 manipulations (insertion and removal) of them are wrong. */
1799 gdb_assert (bp->type != single_step_breakpoint);
1800 delete_breakpoint_1 (proc, bp);
1805 /* Check if breakpoints we inserted still appear to be inserted. They
1806 may disappear due to a shared library unload, and worse, a new
1807 shared library may be reloaded at the same address as the
1808 previously unloaded one. If that happens, we should make sure that
1809 the shadow memory of the old breakpoints isn't used when reading or
1810 writing memory. */
1812 void
1813 validate_breakpoints (void)
1815 struct process_info *proc = current_process ();
1816 struct breakpoint *bp;
1818 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1820 struct raw_breakpoint *raw = bp->raw;
1822 if (raw->raw_type == raw_bkpt_type_sw && raw->inserted > 0)
1823 validate_inserted_breakpoint (raw);
1826 delete_disabled_breakpoints ();
1829 void
1830 check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
1832 struct process_info *proc = current_process ();
1833 struct raw_breakpoint *bp = proc->raw_breakpoints;
1834 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1835 CORE_ADDR mem_end = mem_addr + mem_len;
1836 int disabled_one = 0;
1838 for (; jp != NULL; jp = jp->next)
1840 CORE_ADDR bp_end = jp->pc + jp->length;
1841 CORE_ADDR start, end;
1842 int copy_offset, copy_len, buf_offset;
1844 gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len
1845 || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1847 if (mem_addr >= bp_end)
1848 continue;
1849 if (jp->pc >= mem_end)
1850 continue;
1852 start = jp->pc;
1853 if (mem_addr > start)
1854 start = mem_addr;
1856 end = bp_end;
1857 if (end > mem_end)
1858 end = mem_end;
1860 copy_len = end - start;
1861 copy_offset = start - jp->pc;
1862 buf_offset = start - mem_addr;
1864 if (jp->inserted)
1865 memcpy (buf + buf_offset,
1866 fast_tracepoint_jump_shadow (jp) + copy_offset,
1867 copy_len);
1870 for (; bp != NULL; bp = bp->next)
1872 CORE_ADDR bp_end = bp->pc + bp_size (bp);
1873 CORE_ADDR start, end;
1874 int copy_offset, copy_len, buf_offset;
1876 if (bp->raw_type != raw_bkpt_type_sw)
1877 continue;
1879 gdb_assert (bp->old_data >= buf + mem_len
1880 || buf >= &bp->old_data[sizeof (bp->old_data)]);
1882 if (mem_addr >= bp_end)
1883 continue;
1884 if (bp->pc >= mem_end)
1885 continue;
1887 start = bp->pc;
1888 if (mem_addr > start)
1889 start = mem_addr;
1891 end = bp_end;
1892 if (end > mem_end)
1893 end = mem_end;
1895 copy_len = end - start;
1896 copy_offset = start - bp->pc;
1897 buf_offset = start - mem_addr;
1899 if (bp->inserted > 0)
1901 if (validate_inserted_breakpoint (bp))
1902 memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
1903 else
1904 disabled_one = 1;
1908 if (disabled_one)
1909 delete_disabled_breakpoints ();
1912 void
1913 check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
1914 const unsigned char *myaddr, int mem_len)
1916 struct process_info *proc = current_process ();
1917 struct raw_breakpoint *bp = proc->raw_breakpoints;
1918 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1919 CORE_ADDR mem_end = mem_addr + mem_len;
1920 int disabled_one = 0;
1922 /* First fast tracepoint jumps, then breakpoint traps on top. */
1924 for (; jp != NULL; jp = jp->next)
1926 CORE_ADDR jp_end = jp->pc + jp->length;
1927 CORE_ADDR start, end;
1928 int copy_offset, copy_len, buf_offset;
1930 gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len
1931 || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1932 gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len
1933 || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length);
1935 if (mem_addr >= jp_end)
1936 continue;
1937 if (jp->pc >= mem_end)
1938 continue;
1940 start = jp->pc;
1941 if (mem_addr > start)
1942 start = mem_addr;
1944 end = jp_end;
1945 if (end > mem_end)
1946 end = mem_end;
1948 copy_len = end - start;
1949 copy_offset = start - jp->pc;
1950 buf_offset = start - mem_addr;
1952 memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset,
1953 myaddr + buf_offset, copy_len);
1954 if (jp->inserted)
1955 memcpy (buf + buf_offset,
1956 fast_tracepoint_jump_insn (jp) + copy_offset, copy_len);
1959 for (; bp != NULL; bp = bp->next)
1961 CORE_ADDR bp_end = bp->pc + bp_size (bp);
1962 CORE_ADDR start, end;
1963 int copy_offset, copy_len, buf_offset;
1965 if (bp->raw_type != raw_bkpt_type_sw)
1966 continue;
1968 gdb_assert (bp->old_data >= myaddr + mem_len
1969 || myaddr >= &bp->old_data[sizeof (bp->old_data)]);
1971 if (mem_addr >= bp_end)
1972 continue;
1973 if (bp->pc >= mem_end)
1974 continue;
1976 start = bp->pc;
1977 if (mem_addr > start)
1978 start = mem_addr;
1980 end = bp_end;
1981 if (end > mem_end)
1982 end = mem_end;
1984 copy_len = end - start;
1985 copy_offset = start - bp->pc;
1986 buf_offset = start - mem_addr;
1988 memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len);
1989 if (bp->inserted > 0)
1991 if (validate_inserted_breakpoint (bp))
1992 memcpy (buf + buf_offset, bp_opcode (bp) + copy_offset, copy_len);
1993 else
1994 disabled_one = 1;
1998 if (disabled_one)
1999 delete_disabled_breakpoints ();
2002 /* Delete all breakpoints, 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, but do not try to un-insert them from the
2025 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 struct process_info *child_proc = get_thread_process (child_thread);
2142 struct process_info *parent_proc = get_thread_process (parent_thread);
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, ptid_of (child_thread));
2149 APPEND_TO_LIST (new_list, new_bkpt, bkpt_tail);
2150 APPEND_TO_LIST (new_raw_list, new_bkpt->raw, raw_bkpt_tail);