Fix seg-fault when generating an empty DLL with LTO enabled.
[binutils-gdb.git] / gdbserver / mem-break.cc
blobc669842228d0737a15d86f864f52a0dd4b557980
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 temporarilly 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 ();
979 struct breakpoint *bp;
980 enum bkpt_type type = Z_packet_to_bkpt_type (z_type);
982 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
983 if (bp->type == type && bp->raw->pc == addr
984 && (kind == -1 || bp->raw->kind == kind))
985 return (struct gdb_breakpoint *) bp;
987 return NULL;
990 static int
991 z_type_supported (char z_type)
993 return (z_type >= '0' && z_type <= '4'
994 && the_target->supports_z_point_type (z_type));
997 /* Create a new GDB breakpoint of type Z_TYPE at ADDR with kind KIND.
998 Returns a pointer to the newly created breakpoint on success. On
999 failure returns NULL and sets *ERR to either -1 for error, or 1 if
1000 Z_TYPE breakpoints are not supported on this target. */
1002 struct gdb_breakpoint *
1003 set_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind, int *err)
1005 struct gdb_breakpoint *bp;
1006 enum bkpt_type type;
1007 enum raw_bkpt_type raw_type;
1009 if (!z_type_supported (z_type))
1011 *err = 1;
1012 return nullptr;
1015 /* If we see GDB inserting a second code breakpoint at the same
1016 address, then either: GDB is updating the breakpoint's conditions
1017 or commands; or, the first breakpoint must have disappeared due
1018 to a shared library unload. On targets where the shared
1019 libraries are handled by userspace, like SVR4, for example,
1020 GDBserver can't tell if a library was loaded or unloaded. Since
1021 we refcount raw breakpoints, we must be careful to make sure GDB
1022 breakpoints never contribute more than one reference. if we
1023 didn't do this, in case the previous breakpoint is gone due to a
1024 shared library unload, we'd just increase the refcount of the
1025 previous breakpoint at this address, but the trap was not planted
1026 in the inferior anymore, thus the breakpoint would never be hit.
1027 Note this must be careful to not create a window where
1028 breakpoints are removed from the target, for non-stop, in case
1029 the target can poke at memory while the program is running. */
1030 if (z_type == Z_PACKET_SW_BP
1031 || z_type == Z_PACKET_HW_BP)
1033 bp = find_gdb_breakpoint (z_type, addr, -1);
1035 if (bp != NULL)
1037 if (bp->base.raw->kind != kind)
1039 /* A different kind than previously seen. The previous
1040 breakpoint must be gone then. */
1041 bp->base.raw->inserted = -1;
1042 delete_breakpoint ((struct breakpoint *) bp);
1043 bp = NULL;
1045 else if (z_type == Z_PACKET_SW_BP)
1047 /* Check if the breakpoint is actually gone from the
1048 target, due to an solib unload, for example. Might
1049 as well validate _all_ breakpoints. */
1050 validate_breakpoints ();
1052 /* Breakpoints that don't pass validation are
1053 deleted. */
1054 bp = find_gdb_breakpoint (z_type, addr, -1);
1058 else
1060 /* Data breakpoints for the same address but different kind are
1061 expected. GDB doesn't merge these. The backend gets to do
1062 that if it wants/can. */
1063 bp = find_gdb_breakpoint (z_type, addr, kind);
1066 if (bp != NULL)
1068 /* We already know about this breakpoint, there's nothing else
1069 to do - GDB's reference is already accounted for. Note that
1070 whether the breakpoint inserted is left as is - we may be
1071 stepping over it, for example, in which case we don't want to
1072 force-reinsert it. */
1073 return bp;
1076 raw_type = Z_packet_to_raw_bkpt_type (z_type);
1077 type = Z_packet_to_bkpt_type (z_type);
1078 return (struct gdb_breakpoint *) set_breakpoint (type, raw_type, addr,
1079 kind, NULL, err);
1082 /* Delete a GDB breakpoint of type Z_TYPE and kind KIND previously
1083 inserted at ADDR with set_gdb_breakpoint_at. Returns 0 on success,
1084 -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
1085 target. */
1088 delete_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind)
1090 if (!z_type_supported (z_type))
1091 return 1;
1093 gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, kind);
1094 if (bp == NULL)
1095 return -1;
1097 /* Before deleting the breakpoint, make sure to free its condition
1098 and command lists. */
1099 clear_breakpoint_conditions_and_commands (bp);
1100 int err = delete_breakpoint ((struct breakpoint *) bp);
1101 if (err != 0)
1102 return -1;
1104 return 0;
1107 /* Clear all conditions associated with a breakpoint. */
1109 static void
1110 clear_breakpoint_conditions (struct gdb_breakpoint *bp)
1112 struct point_cond_list *cond;
1114 if (bp->cond_list == NULL)
1115 return;
1117 cond = bp->cond_list;
1119 while (cond != NULL)
1121 struct point_cond_list *cond_next;
1123 cond_next = cond->next;
1124 gdb_free_agent_expr (cond->cond);
1125 free (cond);
1126 cond = cond_next;
1129 bp->cond_list = NULL;
1132 /* Clear all commands associated with a breakpoint. */
1134 static void
1135 clear_breakpoint_commands (struct gdb_breakpoint *bp)
1137 struct point_command_list *cmd;
1139 if (bp->command_list == NULL)
1140 return;
1142 cmd = bp->command_list;
1144 while (cmd != NULL)
1146 struct point_command_list *cmd_next;
1148 cmd_next = cmd->next;
1149 gdb_free_agent_expr (cmd->cmd);
1150 free (cmd);
1151 cmd = cmd_next;
1154 bp->command_list = NULL;
1157 void
1158 clear_breakpoint_conditions_and_commands (struct gdb_breakpoint *bp)
1160 clear_breakpoint_conditions (bp);
1161 clear_breakpoint_commands (bp);
1164 /* Add condition CONDITION to GDBserver's breakpoint BP. */
1166 static void
1167 add_condition_to_breakpoint (struct gdb_breakpoint *bp,
1168 struct agent_expr *condition)
1170 struct point_cond_list *new_cond;
1172 /* Create new condition. */
1173 new_cond = XCNEW (struct point_cond_list);
1174 new_cond->cond = condition;
1176 /* Add condition to the list. */
1177 new_cond->next = bp->cond_list;
1178 bp->cond_list = new_cond;
1181 /* Add a target-side condition CONDITION to a breakpoint. */
1184 add_breakpoint_condition (struct gdb_breakpoint *bp, const char **condition)
1186 const char *actparm = *condition;
1187 struct agent_expr *cond;
1189 if (condition == NULL)
1190 return 1;
1192 if (bp == NULL)
1193 return 0;
1195 cond = gdb_parse_agent_expr (&actparm);
1197 if (cond == NULL)
1199 warning ("Condition evaluation failed. Assuming unconditional.");
1200 return 0;
1203 add_condition_to_breakpoint (bp, cond);
1205 *condition = actparm;
1207 return 1;
1210 /* Evaluate condition (if any) at breakpoint BP. Return 1 if
1211 true and 0 otherwise. */
1213 static int
1214 gdb_condition_true_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
1216 /* Fetch registers for the current inferior. */
1217 struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1218 ULONGEST value = 0;
1219 struct point_cond_list *cl;
1220 int err = 0;
1221 struct eval_agent_expr_context ctx;
1223 if (bp == NULL)
1224 return 0;
1226 /* Check if the breakpoint is unconditional. If it is,
1227 the condition always evaluates to TRUE. */
1228 if (bp->cond_list == NULL)
1229 return 1;
1231 ctx.regcache = get_thread_regcache (current_thread, 1);
1232 ctx.tframe = NULL;
1233 ctx.tpoint = NULL;
1235 /* Evaluate each condition in the breakpoint's list of conditions.
1236 Return true if any of the conditions evaluates to TRUE.
1238 If we failed to evaluate the expression, TRUE is returned. This
1239 forces GDB to reevaluate the conditions. */
1240 for (cl = bp->cond_list;
1241 cl && !value && !err; cl = cl->next)
1243 /* Evaluate the condition. */
1244 err = gdb_eval_agent_expr (&ctx, cl->cond, &value);
1247 if (err)
1248 return 1;
1250 return (value != 0);
1254 gdb_condition_true_at_breakpoint (CORE_ADDR where)
1256 /* Only check code (software or hardware) breakpoints. */
1257 return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
1258 || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
1261 /* Add commands COMMANDS to GDBserver's breakpoint BP. */
1263 static void
1264 add_commands_to_breakpoint (struct gdb_breakpoint *bp,
1265 struct agent_expr *commands, int persist)
1267 struct point_command_list *new_cmd;
1269 /* Create new command. */
1270 new_cmd = XCNEW (struct point_command_list);
1271 new_cmd->cmd = commands;
1272 new_cmd->persistence = persist;
1274 /* Add commands to the list. */
1275 new_cmd->next = bp->command_list;
1276 bp->command_list = new_cmd;
1279 /* Add a target-side command COMMAND to the breakpoint at ADDR. */
1282 add_breakpoint_commands (struct gdb_breakpoint *bp, const char **command,
1283 int persist)
1285 const char *actparm = *command;
1286 struct agent_expr *cmd;
1288 if (command == NULL)
1289 return 1;
1291 if (bp == NULL)
1292 return 0;
1294 cmd = gdb_parse_agent_expr (&actparm);
1296 if (cmd == NULL)
1298 warning ("Command evaluation failed. Disabling.");
1299 return 0;
1302 add_commands_to_breakpoint (bp, cmd, persist);
1304 *command = actparm;
1306 return 1;
1309 /* Return true if there are no commands to run at this location,
1310 which likely means we want to report back to GDB. */
1312 static int
1313 gdb_no_commands_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
1315 struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1317 if (bp == NULL)
1318 return 1;
1320 threads_debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s",
1321 paddress (addr), z_type,
1322 phex_nz ((uintptr_t) bp->command_list, 0));
1323 return (bp->command_list == NULL);
1326 /* Return true if there are no commands to run at this location,
1327 which likely means we want to report back to GDB. */
1330 gdb_no_commands_at_breakpoint (CORE_ADDR where)
1332 /* Only check code (software or hardware) breakpoints. */
1333 return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
1334 && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
1337 /* Run a breakpoint's commands. Returns 0 if there was a problem
1338 running any command, 1 otherwise. */
1340 static int
1341 run_breakpoint_commands_z_type (char z_type, CORE_ADDR addr)
1343 /* Fetch registers for the current inferior. */
1344 struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1345 ULONGEST value = 0;
1346 struct point_command_list *cl;
1347 int err = 0;
1348 struct eval_agent_expr_context ctx;
1350 if (bp == NULL)
1351 return 1;
1353 ctx.regcache = get_thread_regcache (current_thread, 1);
1354 ctx.tframe = NULL;
1355 ctx.tpoint = NULL;
1357 for (cl = bp->command_list;
1358 cl && !value && !err; cl = cl->next)
1360 /* Run the command. */
1361 err = gdb_eval_agent_expr (&ctx, cl->cmd, &value);
1363 /* If one command has a problem, stop digging the hole deeper. */
1364 if (err)
1365 return 0;
1368 return 1;
1371 void
1372 run_breakpoint_commands (CORE_ADDR where)
1374 /* Only check code (software or hardware) breakpoints. If one
1375 command has a problem, stop digging the hole deeper. */
1376 if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP, where))
1377 run_breakpoint_commands_z_type (Z_PACKET_HW_BP, where);
1380 /* See mem-break.h. */
1383 gdb_breakpoint_here (CORE_ADDR where)
1385 /* Only check code (software or hardware) breakpoints. */
1386 return (find_gdb_breakpoint (Z_PACKET_SW_BP, where, -1) != NULL
1387 || find_gdb_breakpoint (Z_PACKET_HW_BP, where, -1) != NULL);
1390 void
1391 set_single_step_breakpoint (CORE_ADDR stop_at, ptid_t ptid)
1393 struct single_step_breakpoint *bp;
1395 gdb_assert (current_ptid.pid () == ptid.pid ());
1397 bp = (struct single_step_breakpoint *) set_breakpoint_type_at (single_step_breakpoint,
1398 stop_at, NULL);
1399 bp->ptid = ptid;
1402 void
1403 delete_single_step_breakpoints (struct thread_info *thread)
1405 struct process_info *proc = get_thread_process (thread);
1406 struct breakpoint *bp, **bp_link;
1408 bp = proc->breakpoints;
1409 bp_link = &proc->breakpoints;
1411 while (bp)
1413 if (bp->type == single_step_breakpoint
1414 && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1416 scoped_restore_current_thread restore_thread;
1418 switch_to_thread (thread);
1419 *bp_link = bp->next;
1420 release_breakpoint (proc, bp);
1421 bp = *bp_link;
1423 else
1425 bp_link = &bp->next;
1426 bp = *bp_link;
1431 static void
1432 uninsert_raw_breakpoint (struct raw_breakpoint *bp)
1434 if (bp->inserted < 0)
1436 threads_debug_printf ("Breakpoint at %s is marked insert-disabled.",
1437 paddress (bp->pc));
1439 else if (bp->inserted > 0)
1441 int err;
1443 bp->inserted = 0;
1445 err = the_target->remove_point (bp->raw_type, bp->pc, bp->kind, bp);
1446 if (err != 0)
1448 bp->inserted = 1;
1450 threads_debug_printf ("Failed to uninsert raw breakpoint at 0x%s.",
1451 paddress (bp->pc));
1456 void
1457 uninsert_breakpoints_at (CORE_ADDR pc)
1459 struct process_info *proc = current_process ();
1460 struct raw_breakpoint *bp;
1461 int found = 0;
1463 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1464 if ((bp->raw_type == raw_bkpt_type_sw
1465 || bp->raw_type == raw_bkpt_type_hw)
1466 && bp->pc == pc)
1468 found = 1;
1470 if (bp->inserted)
1471 uninsert_raw_breakpoint (bp);
1474 if (!found)
1476 /* This can happen when we remove all breakpoints while handling
1477 a step-over. */
1478 threads_debug_printf ("Could not find breakpoint at 0x%s "
1479 "in list (uninserting).",
1480 paddress (pc));
1484 void
1485 uninsert_all_breakpoints (void)
1487 struct process_info *proc = current_process ();
1488 struct raw_breakpoint *bp;
1490 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1491 if ((bp->raw_type == raw_bkpt_type_sw
1492 || bp->raw_type == raw_bkpt_type_hw)
1493 && bp->inserted)
1494 uninsert_raw_breakpoint (bp);
1497 void
1498 uninsert_single_step_breakpoints (struct thread_info *thread)
1500 struct process_info *proc = get_thread_process (thread);
1501 struct breakpoint *bp;
1503 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1505 if (bp->type == single_step_breakpoint
1506 && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1508 gdb_assert (bp->raw->inserted > 0);
1510 /* Only uninsert the raw breakpoint if it only belongs to a
1511 reinsert breakpoint. */
1512 if (bp->raw->refcount == 1)
1514 scoped_restore_current_thread restore_thread;
1516 switch_to_thread (thread);
1517 uninsert_raw_breakpoint (bp->raw);
1523 static void
1524 reinsert_raw_breakpoint (struct raw_breakpoint *bp)
1526 int err;
1528 if (bp->inserted)
1529 return;
1531 err = the_target->insert_point (bp->raw_type, bp->pc, bp->kind, bp);
1532 if (err == 0)
1533 bp->inserted = 1;
1534 else
1535 threads_debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).",
1536 paddress (bp->pc), err);
1539 void
1540 reinsert_breakpoints_at (CORE_ADDR pc)
1542 struct process_info *proc = current_process ();
1543 struct raw_breakpoint *bp;
1544 int found = 0;
1546 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1547 if ((bp->raw_type == raw_bkpt_type_sw
1548 || bp->raw_type == raw_bkpt_type_hw)
1549 && bp->pc == pc)
1551 found = 1;
1553 reinsert_raw_breakpoint (bp);
1556 if (!found)
1558 /* This can happen when we remove all breakpoints while handling
1559 a step-over. */
1560 threads_debug_printf ("Could not find raw breakpoint at 0x%s "
1561 "in list (reinserting).",
1562 paddress (pc));
1567 has_single_step_breakpoints (struct thread_info *thread)
1569 struct process_info *proc = get_thread_process (thread);
1570 struct breakpoint *bp, **bp_link;
1572 bp = proc->breakpoints;
1573 bp_link = &proc->breakpoints;
1575 while (bp)
1577 if (bp->type == single_step_breakpoint
1578 && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1579 return 1;
1580 else
1582 bp_link = &bp->next;
1583 bp = *bp_link;
1587 return 0;
1590 void
1591 reinsert_all_breakpoints (void)
1593 struct process_info *proc = current_process ();
1594 struct raw_breakpoint *bp;
1596 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1597 if ((bp->raw_type == raw_bkpt_type_sw
1598 || bp->raw_type == raw_bkpt_type_hw)
1599 && !bp->inserted)
1600 reinsert_raw_breakpoint (bp);
1603 void
1604 reinsert_single_step_breakpoints (struct thread_info *thread)
1606 struct process_info *proc = get_thread_process (thread);
1607 struct breakpoint *bp;
1609 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1611 if (bp->type == single_step_breakpoint
1612 && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1614 gdb_assert (bp->raw->inserted > 0);
1616 if (bp->raw->refcount == 1)
1618 scoped_restore_current_thread restore_thread;
1620 switch_to_thread (thread);
1621 reinsert_raw_breakpoint (bp->raw);
1627 void
1628 check_breakpoints (CORE_ADDR stop_pc)
1630 struct process_info *proc = current_process ();
1631 struct breakpoint *bp, **bp_link;
1633 bp = proc->breakpoints;
1634 bp_link = &proc->breakpoints;
1636 while (bp)
1638 struct raw_breakpoint *raw = bp->raw;
1640 if ((raw->raw_type == raw_bkpt_type_sw
1641 || raw->raw_type == raw_bkpt_type_hw)
1642 && raw->pc == stop_pc)
1644 if (!raw->inserted)
1646 warning ("Hit a removed breakpoint?");
1647 return;
1650 if (bp->type == other_breakpoint)
1652 struct other_breakpoint *other_bp
1653 = (struct other_breakpoint *) bp;
1655 if (other_bp->handler != NULL && (*other_bp->handler) (stop_pc))
1657 *bp_link = bp->next;
1659 release_breakpoint (proc, bp);
1661 bp = *bp_link;
1662 continue;
1667 bp_link = &bp->next;
1668 bp = *bp_link;
1673 breakpoint_here (CORE_ADDR addr)
1675 struct process_info *proc = current_process ();
1676 struct raw_breakpoint *bp;
1678 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1679 if ((bp->raw_type == raw_bkpt_type_sw
1680 || bp->raw_type == raw_bkpt_type_hw)
1681 && bp->pc == addr)
1682 return 1;
1684 return 0;
1688 breakpoint_inserted_here (CORE_ADDR addr)
1690 struct process_info *proc = current_process ();
1691 struct raw_breakpoint *bp;
1693 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1694 if ((bp->raw_type == raw_bkpt_type_sw
1695 || bp->raw_type == raw_bkpt_type_hw)
1696 && bp->pc == addr
1697 && bp->inserted)
1698 return 1;
1700 return 0;
1703 /* See mem-break.h. */
1706 software_breakpoint_inserted_here (CORE_ADDR addr)
1708 struct process_info *proc = current_process ();
1709 struct raw_breakpoint *bp;
1711 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1712 if (bp->raw_type == raw_bkpt_type_sw
1713 && bp->pc == addr
1714 && bp->inserted)
1715 return 1;
1717 return 0;
1720 /* See mem-break.h. */
1723 hardware_breakpoint_inserted_here (CORE_ADDR addr)
1725 struct process_info *proc = current_process ();
1726 struct raw_breakpoint *bp;
1728 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1729 if (bp->raw_type == raw_bkpt_type_hw
1730 && bp->pc == addr
1731 && bp->inserted)
1732 return 1;
1734 return 0;
1737 /* See mem-break.h. */
1740 single_step_breakpoint_inserted_here (CORE_ADDR addr)
1742 struct process_info *proc = current_process ();
1743 struct breakpoint *bp;
1745 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1746 if (bp->type == single_step_breakpoint
1747 && bp->raw->pc == addr
1748 && bp->raw->inserted)
1749 return 1;
1751 return 0;
1754 static int
1755 validate_inserted_breakpoint (struct raw_breakpoint *bp)
1757 unsigned char *buf;
1758 int err;
1760 gdb_assert (bp->inserted);
1761 gdb_assert (bp->raw_type == raw_bkpt_type_sw);
1763 buf = (unsigned char *) alloca (bp_size (bp));
1764 err = the_target->read_memory (bp->pc, buf, bp_size (bp));
1765 if (err || memcmp (buf, bp_opcode (bp), bp_size (bp)) != 0)
1767 /* Tag it as gone. */
1768 bp->inserted = -1;
1769 return 0;
1772 return 1;
1775 static void
1776 delete_disabled_breakpoints (void)
1778 struct process_info *proc = current_process ();
1779 struct breakpoint *bp, *next;
1781 for (bp = proc->breakpoints; bp != NULL; bp = next)
1783 next = bp->next;
1784 if (bp->raw->inserted < 0)
1786 /* If single_step_breakpoints become disabled, that means the
1787 manipulations (insertion and removal) of them are wrong. */
1788 gdb_assert (bp->type != single_step_breakpoint);
1789 delete_breakpoint_1 (proc, bp);
1794 /* Check if breakpoints we inserted still appear to be inserted. They
1795 may disappear due to a shared library unload, and worse, a new
1796 shared library may be reloaded at the same address as the
1797 previously unloaded one. If that happens, we should make sure that
1798 the shadow memory of the old breakpoints isn't used when reading or
1799 writing memory. */
1801 void
1802 validate_breakpoints (void)
1804 struct process_info *proc = current_process ();
1805 struct breakpoint *bp;
1807 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1809 struct raw_breakpoint *raw = bp->raw;
1811 if (raw->raw_type == raw_bkpt_type_sw && raw->inserted > 0)
1812 validate_inserted_breakpoint (raw);
1815 delete_disabled_breakpoints ();
1818 void
1819 check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
1821 struct process_info *proc = current_process ();
1822 struct raw_breakpoint *bp = proc->raw_breakpoints;
1823 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1824 CORE_ADDR mem_end = mem_addr + mem_len;
1825 int disabled_one = 0;
1827 for (; jp != NULL; jp = jp->next)
1829 CORE_ADDR bp_end = jp->pc + jp->length;
1830 CORE_ADDR start, end;
1831 int copy_offset, copy_len, buf_offset;
1833 gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len
1834 || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1836 if (mem_addr >= bp_end)
1837 continue;
1838 if (jp->pc >= mem_end)
1839 continue;
1841 start = jp->pc;
1842 if (mem_addr > start)
1843 start = mem_addr;
1845 end = bp_end;
1846 if (end > mem_end)
1847 end = mem_end;
1849 copy_len = end - start;
1850 copy_offset = start - jp->pc;
1851 buf_offset = start - mem_addr;
1853 if (jp->inserted)
1854 memcpy (buf + buf_offset,
1855 fast_tracepoint_jump_shadow (jp) + copy_offset,
1856 copy_len);
1859 for (; bp != NULL; bp = bp->next)
1861 CORE_ADDR bp_end = bp->pc + bp_size (bp);
1862 CORE_ADDR start, end;
1863 int copy_offset, copy_len, buf_offset;
1865 if (bp->raw_type != raw_bkpt_type_sw)
1866 continue;
1868 gdb_assert (bp->old_data >= buf + mem_len
1869 || buf >= &bp->old_data[sizeof (bp->old_data)]);
1871 if (mem_addr >= bp_end)
1872 continue;
1873 if (bp->pc >= mem_end)
1874 continue;
1876 start = bp->pc;
1877 if (mem_addr > start)
1878 start = mem_addr;
1880 end = bp_end;
1881 if (end > mem_end)
1882 end = mem_end;
1884 copy_len = end - start;
1885 copy_offset = start - bp->pc;
1886 buf_offset = start - mem_addr;
1888 if (bp->inserted > 0)
1890 if (validate_inserted_breakpoint (bp))
1891 memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
1892 else
1893 disabled_one = 1;
1897 if (disabled_one)
1898 delete_disabled_breakpoints ();
1901 void
1902 check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
1903 const unsigned char *myaddr, int mem_len)
1905 struct process_info *proc = current_process ();
1906 struct raw_breakpoint *bp = proc->raw_breakpoints;
1907 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1908 CORE_ADDR mem_end = mem_addr + mem_len;
1909 int disabled_one = 0;
1911 /* First fast tracepoint jumps, then breakpoint traps on top. */
1913 for (; jp != NULL; jp = jp->next)
1915 CORE_ADDR jp_end = jp->pc + jp->length;
1916 CORE_ADDR start, end;
1917 int copy_offset, copy_len, buf_offset;
1919 gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len
1920 || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1921 gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len
1922 || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length);
1924 if (mem_addr >= jp_end)
1925 continue;
1926 if (jp->pc >= mem_end)
1927 continue;
1929 start = jp->pc;
1930 if (mem_addr > start)
1931 start = mem_addr;
1933 end = jp_end;
1934 if (end > mem_end)
1935 end = mem_end;
1937 copy_len = end - start;
1938 copy_offset = start - jp->pc;
1939 buf_offset = start - mem_addr;
1941 memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset,
1942 myaddr + buf_offset, copy_len);
1943 if (jp->inserted)
1944 memcpy (buf + buf_offset,
1945 fast_tracepoint_jump_insn (jp) + copy_offset, copy_len);
1948 for (; bp != NULL; bp = bp->next)
1950 CORE_ADDR bp_end = bp->pc + bp_size (bp);
1951 CORE_ADDR start, end;
1952 int copy_offset, copy_len, buf_offset;
1954 if (bp->raw_type != raw_bkpt_type_sw)
1955 continue;
1957 gdb_assert (bp->old_data >= myaddr + mem_len
1958 || myaddr >= &bp->old_data[sizeof (bp->old_data)]);
1960 if (mem_addr >= bp_end)
1961 continue;
1962 if (bp->pc >= mem_end)
1963 continue;
1965 start = bp->pc;
1966 if (mem_addr > start)
1967 start = mem_addr;
1969 end = bp_end;
1970 if (end > mem_end)
1971 end = mem_end;
1973 copy_len = end - start;
1974 copy_offset = start - bp->pc;
1975 buf_offset = start - mem_addr;
1977 memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len);
1978 if (bp->inserted > 0)
1980 if (validate_inserted_breakpoint (bp))
1981 memcpy (buf + buf_offset, bp_opcode (bp) + copy_offset, copy_len);
1982 else
1983 disabled_one = 1;
1987 if (disabled_one)
1988 delete_disabled_breakpoints ();
1991 /* Delete all breakpoints, and un-insert them from the inferior. */
1993 void
1994 delete_all_breakpoints (void)
1996 struct process_info *proc = current_process ();
1998 while (proc->breakpoints)
1999 delete_breakpoint_1 (proc, proc->breakpoints);
2002 /* Clear the "inserted" flag in all breakpoints. */
2004 void
2005 mark_breakpoints_out (struct process_info *proc)
2007 struct raw_breakpoint *raw_bp;
2009 for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next)
2010 raw_bp->inserted = 0;
2013 /* Release all breakpoints, but do not try to un-insert them from the
2014 inferior. */
2016 void
2017 free_all_breakpoints (struct process_info *proc)
2019 mark_breakpoints_out (proc);
2021 /* Note: use PROC explicitly instead of deferring to
2022 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
2023 released when we get here. There should be no call to
2024 current_process from here on. */
2025 while (proc->breakpoints)
2026 delete_breakpoint_1 (proc, proc->breakpoints);
2029 /* Clone an agent expression. */
2031 static struct agent_expr *
2032 clone_agent_expr (const struct agent_expr *src_ax)
2034 struct agent_expr *ax;
2036 ax = XCNEW (struct agent_expr);
2037 ax->length = src_ax->length;
2038 ax->bytes = (unsigned char *) xcalloc (ax->length, 1);
2039 memcpy (ax->bytes, src_ax->bytes, ax->length);
2040 return ax;
2043 /* Deep-copy the contents of one breakpoint to another. */
2045 static struct breakpoint *
2046 clone_one_breakpoint (const struct breakpoint *src, ptid_t ptid)
2048 struct breakpoint *dest;
2049 struct raw_breakpoint *dest_raw;
2051 /* Clone the raw breakpoint. */
2052 dest_raw = XCNEW (struct raw_breakpoint);
2053 dest_raw->raw_type = src->raw->raw_type;
2054 dest_raw->refcount = src->raw->refcount;
2055 dest_raw->pc = src->raw->pc;
2056 dest_raw->kind = src->raw->kind;
2057 memcpy (dest_raw->old_data, src->raw->old_data, MAX_BREAKPOINT_LEN);
2058 dest_raw->inserted = src->raw->inserted;
2060 /* Clone the high-level breakpoint. */
2061 if (is_gdb_breakpoint (src->type))
2063 struct gdb_breakpoint *gdb_dest = XCNEW (struct gdb_breakpoint);
2064 struct point_cond_list *current_cond;
2065 struct point_cond_list *new_cond;
2066 struct point_cond_list *cond_tail = NULL;
2067 struct point_command_list *current_cmd;
2068 struct point_command_list *new_cmd;
2069 struct point_command_list *cmd_tail = NULL;
2071 /* Clone the condition list. */
2072 for (current_cond = ((struct gdb_breakpoint *) src)->cond_list;
2073 current_cond != NULL;
2074 current_cond = current_cond->next)
2076 new_cond = XCNEW (struct point_cond_list);
2077 new_cond->cond = clone_agent_expr (current_cond->cond);
2078 APPEND_TO_LIST (&gdb_dest->cond_list, new_cond, cond_tail);
2081 /* Clone the command list. */
2082 for (current_cmd = ((struct gdb_breakpoint *) src)->command_list;
2083 current_cmd != NULL;
2084 current_cmd = current_cmd->next)
2086 new_cmd = XCNEW (struct point_command_list);
2087 new_cmd->cmd = clone_agent_expr (current_cmd->cmd);
2088 new_cmd->persistence = current_cmd->persistence;
2089 APPEND_TO_LIST (&gdb_dest->command_list, new_cmd, cmd_tail);
2092 dest = (struct breakpoint *) gdb_dest;
2094 else if (src->type == other_breakpoint)
2096 struct other_breakpoint *other_dest = XCNEW (struct other_breakpoint);
2098 other_dest->handler = ((struct other_breakpoint *) src)->handler;
2099 dest = (struct breakpoint *) other_dest;
2101 else if (src->type == single_step_breakpoint)
2103 struct single_step_breakpoint *ss_dest
2104 = XCNEW (struct single_step_breakpoint);
2106 dest = (struct breakpoint *) ss_dest;
2107 /* Since single-step breakpoint is thread specific, don't copy
2108 thread id from SRC, use ID instead. */
2109 ss_dest->ptid = ptid;
2111 else
2112 gdb_assert_not_reached ("unhandled breakpoint type");
2114 dest->type = src->type;
2115 dest->raw = dest_raw;
2117 return dest;
2120 /* See mem-break.h. */
2122 void
2123 clone_all_breakpoints (struct thread_info *child_thread,
2124 const struct thread_info *parent_thread)
2126 const struct breakpoint *bp;
2127 struct breakpoint *new_bkpt;
2128 struct breakpoint *bkpt_tail = NULL;
2129 struct raw_breakpoint *raw_bkpt_tail = NULL;
2130 struct process_info *child_proc = get_thread_process (child_thread);
2131 struct process_info *parent_proc = get_thread_process (parent_thread);
2132 struct breakpoint **new_list = &child_proc->breakpoints;
2133 struct raw_breakpoint **new_raw_list = &child_proc->raw_breakpoints;
2135 for (bp = parent_proc->breakpoints; bp != NULL; bp = bp->next)
2137 new_bkpt = clone_one_breakpoint (bp, ptid_of (child_thread));
2138 APPEND_TO_LIST (new_list, new_bkpt, bkpt_tail);
2139 APPEND_TO_LIST (new_raw_list, new_bkpt->raw, raw_bkpt_tail);