1 /* Memory breakpoint operations for the remote server for GDB.
2 Copyright (C) 2002-2022 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/>. */
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
34 #define APPEND_TO_LIST(listpp, itemp, tailp) \
37 if ((tailp) == NULL) \
38 *(listpp) = (itemp); \
40 (tailp)->next = (itemp); \
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
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
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
84 /* The low level, physical, raw breakpoint. */
87 struct raw_breakpoint
*next
;
89 /* The low level type of the breakpoint (software breakpoint,
91 enum raw_bkpt_type raw_type
;
93 /* A reference count. Each high level breakpoint referencing this
94 raw breakpoint accounts for one reference. */
97 /* The breakpoint's insertion address. There can only be one raw
98 breakpoint for a given 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. */
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. */
118 /* The type of a breakpoint. */
121 /* A GDB breakpoint, requested with a Z0 packet. */
124 /* A GDB hardware breakpoint, requested with a Z1 packet. */
127 /* A GDB write watchpoint, requested with a Z2 packet. */
130 /* A GDB read watchpoint, requested with a Z3 packet. */
133 /* A GDB access watchpoint, requested with a Z4 packet. */
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. */
144 struct point_cond_list
146 /* Pointer to the agent expression that is the breakpoint's
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
158 struct agent_expr
*cmd
;
160 /* Flag that is true if this command should run even while GDB is
164 /* Pointer to the next command. */
165 struct point_command_list
*next
;
168 /* A high level (in gdbserver's perspective) breakpoint. */
171 struct breakpoint
*next
;
173 /* The breakpoint's type. */
176 /* Link to this breakpoint's raw breakpoint. This is always
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
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. */
219 /* Return the breakpoint size from its kind. */
222 bp_size (struct raw_breakpoint
*bp
)
226 the_target
->sw_breakpoint_from_kind (bp
->kind
, &size
);
230 /* Return the breakpoint opcode from its kind. */
232 static const gdb_byte
*
233 bp_opcode (struct raw_breakpoint
*bp
)
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
)
247 case raw_bkpt_type_hw
:
249 case raw_bkpt_type_write_wp
:
251 case raw_bkpt_type_read_wp
:
253 case raw_bkpt_type_access_wp
:
256 internal_error (__FILE__
, __LINE__
,
257 "bad raw breakpoint type %d", (int) raw_type
);
261 /* See mem-break.h. */
263 static enum bkpt_type
264 Z_packet_to_bkpt_type (char z_type
)
266 gdb_assert ('0' <= z_type
&& z_type
<= '4');
268 return (enum bkpt_type
) (gdb_breakpoint_Z0
+ (z_type
- '0'));
271 /* See mem-break.h. */
274 Z_packet_to_raw_bkpt_type (char z_type
)
279 return raw_bkpt_type_sw
;
281 return raw_bkpt_type_hw
;
282 case Z_PACKET_WRITE_WP
:
283 return raw_bkpt_type_write_wp
;
284 case Z_PACKET_READ_WP
:
285 return raw_bkpt_type_read_wp
;
286 case Z_PACKET_ACCESS_WP
:
287 return raw_bkpt_type_access_wp
;
289 gdb_assert_not_reached ("unhandled Z packet type.");
293 /* Return true if breakpoint TYPE is a GDB breakpoint. */
296 is_gdb_breakpoint (enum bkpt_type type
)
298 return (type
== gdb_breakpoint_Z0
299 || type
== gdb_breakpoint_Z1
300 || type
== gdb_breakpoint_Z2
301 || type
== gdb_breakpoint_Z3
302 || type
== gdb_breakpoint_Z4
);
306 any_persistent_commands (process_info
*proc
)
308 struct breakpoint
*bp
;
309 struct point_command_list
*cl
;
311 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
313 if (is_gdb_breakpoint (bp
->type
))
315 struct gdb_breakpoint
*gdb_bp
= (struct gdb_breakpoint
*) bp
;
317 for (cl
= gdb_bp
->command_list
; cl
!= NULL
; cl
= cl
->next
)
326 /* Find low-level breakpoint of type TYPE at address ADDR that is not
327 insert-disabled. Returns NULL if not found. */
329 static struct raw_breakpoint
*
330 find_enabled_raw_code_breakpoint_at (CORE_ADDR addr
, enum raw_bkpt_type type
)
332 struct process_info
*proc
= current_process ();
333 struct raw_breakpoint
*bp
;
335 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
337 && bp
->raw_type
== type
338 && bp
->inserted
>= 0)
344 /* Find low-level breakpoint of type TYPE at address ADDR. Returns
345 NULL if not found. */
347 static struct raw_breakpoint
*
348 find_raw_breakpoint_at (CORE_ADDR addr
, enum raw_bkpt_type type
, int kind
)
350 struct process_info
*proc
= current_process ();
351 struct raw_breakpoint
*bp
;
353 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
354 if (bp
->pc
== addr
&& bp
->raw_type
== type
&& bp
->kind
== kind
)
360 /* See mem-break.h. */
363 insert_memory_breakpoint (struct raw_breakpoint
*bp
)
365 unsigned char buf
[MAX_BREAKPOINT_LEN
];
368 /* Note that there can be fast tracepoint jumps installed in the
369 same memory range, so to get at the original memory, we need to
370 use read_inferior_memory, which masks those out. */
371 err
= read_inferior_memory (bp
->pc
, buf
, bp_size (bp
));
375 debug_printf ("Failed to read shadow memory of"
376 " breakpoint at 0x%s (%s).\n",
377 paddress (bp
->pc
), safe_strerror (err
));
381 memcpy (bp
->old_data
, buf
, bp_size (bp
));
383 err
= the_target
->write_memory (bp
->pc
, bp_opcode (bp
),
388 debug_printf ("Failed to insert breakpoint at 0x%s (%s).\n",
389 paddress (bp
->pc
), safe_strerror (err
));
392 return err
!= 0 ? -1 : 0;
395 /* See mem-break.h */
398 remove_memory_breakpoint (struct raw_breakpoint
*bp
)
400 unsigned char buf
[MAX_BREAKPOINT_LEN
];
403 /* Since there can be trap breakpoints inserted in the same address
404 range, we use `target_write_memory', which takes care of
405 layering breakpoints on top of fast tracepoints, and on top of
406 the buffer we pass it. This works because the caller has already
407 either unlinked the breakpoint or marked it uninserted. Also
408 note that we need to pass the current shadow contents, because
409 target_write_memory updates any shadow memory with what we pass
410 here, and we want that to be a nop. */
411 memcpy (buf
, bp
->old_data
, bp_size (bp
));
412 err
= target_write_memory (bp
->pc
, buf
, bp_size (bp
));
416 debug_printf ("Failed to uninsert raw breakpoint "
417 "at 0x%s (%s) while deleting it.\n",
418 paddress (bp
->pc
), safe_strerror (err
));
420 return err
!= 0 ? -1 : 0;
423 /* Set a RAW breakpoint of type TYPE and kind KIND at WHERE. On
424 success, a pointer to the new breakpoint is returned. On failure,
425 returns NULL and writes the error code to *ERR. */
427 static struct raw_breakpoint
*
428 set_raw_breakpoint_at (enum raw_bkpt_type type
, CORE_ADDR where
, int kind
,
431 struct process_info
*proc
= current_process ();
432 struct raw_breakpoint
*bp
;
434 if (type
== raw_bkpt_type_sw
|| type
== raw_bkpt_type_hw
)
436 bp
= find_enabled_raw_code_breakpoint_at (where
, type
);
437 if (bp
!= NULL
&& bp
->kind
!= kind
)
439 /* A different kind than previously seen. The previous
440 breakpoint must be gone then. */
442 debug_printf ("Inconsistent breakpoint kind? Was %d, now %d.\n",
449 bp
= find_raw_breakpoint_at (where
, type
, kind
);
451 gdb::unique_xmalloc_ptr
<struct raw_breakpoint
> bp_holder
;
454 bp_holder
.reset (XCNEW (struct raw_breakpoint
));
455 bp
= bp_holder
.get ();
463 *err
= the_target
->insert_point (bp
->raw_type
, bp
->pc
, bp
->kind
, bp
);
467 debug_printf ("Failed to insert breakpoint at 0x%s (%d).\n",
468 paddress (where
), *err
);
476 /* If the breakpoint was allocated above, we know we want to keep it
478 bp_holder
.release ();
480 /* Link the breakpoint in, if this is the first reference. */
481 if (++bp
->refcount
== 1)
483 bp
->next
= proc
->raw_breakpoints
;
484 proc
->raw_breakpoints
= bp
;
489 /* Notice that breakpoint traps are always installed on top of fast
490 tracepoint jumps. This is even if the fast tracepoint is installed
491 at a later time compared to when the breakpoint was installed.
492 This means that a stopping breakpoint or tracepoint has higher
493 "priority". In turn, this allows having fast and slow tracepoints
494 (and breakpoints) at the same address behave correctly. */
497 /* A fast tracepoint jump. */
499 struct fast_tracepoint_jump
501 struct fast_tracepoint_jump
*next
;
503 /* A reference count. GDB can install more than one fast tracepoint
504 at the same address (each with its own action list, for
508 /* The fast tracepoint's insertion address. There can only be one
509 of these for a given PC. */
512 /* Non-zero if this fast tracepoint jump is currently inserted in
516 /* The length of the jump instruction. */
519 /* A poor-man's flexible array member, holding both the jump
520 instruction to insert, and a copy of the instruction that would
521 be in memory had not been a jump there (the shadow memory of the
523 unsigned char insn_and_shadow
[0];
526 /* Fast tracepoint FP's jump instruction to insert. */
527 #define fast_tracepoint_jump_insn(fp) \
528 ((fp)->insn_and_shadow + 0)
530 /* The shadow memory of fast tracepoint jump FP. */
531 #define fast_tracepoint_jump_shadow(fp) \
532 ((fp)->insn_and_shadow + (fp)->length)
535 /* Return the fast tracepoint jump set at WHERE. */
537 static struct fast_tracepoint_jump
*
538 find_fast_tracepoint_jump_at (CORE_ADDR where
)
540 struct process_info
*proc
= current_process ();
541 struct fast_tracepoint_jump
*jp
;
543 for (jp
= proc
->fast_tracepoint_jumps
; jp
!= NULL
; jp
= jp
->next
)
551 fast_tracepoint_jump_here (CORE_ADDR where
)
553 struct fast_tracepoint_jump
*jp
= find_fast_tracepoint_jump_at (where
);
559 delete_fast_tracepoint_jump (struct fast_tracepoint_jump
*todel
)
561 struct fast_tracepoint_jump
*bp
, **bp_link
;
563 struct process_info
*proc
= current_process ();
565 bp
= proc
->fast_tracepoint_jumps
;
566 bp_link
= &proc
->fast_tracepoint_jumps
;
572 if (--bp
->refcount
== 0)
574 struct fast_tracepoint_jump
*prev_bp_link
= *bp_link
;
580 /* Since there can be breakpoints inserted in the same
581 address range, we use `target_write_memory', which
582 takes care of layering breakpoints on top of fast
583 tracepoints, and on top of the buffer we pass it.
584 This works because we've already unlinked the fast
585 tracepoint jump above. Also note that we need to
586 pass the current shadow contents, because
587 target_write_memory updates any shadow memory with
588 what we pass here, and we want that to be a nop. */
589 buf
= (unsigned char *) alloca (bp
->length
);
590 memcpy (buf
, fast_tracepoint_jump_shadow (bp
), bp
->length
);
591 ret
= target_write_memory (bp
->pc
, buf
, bp
->length
);
594 /* Something went wrong, relink the jump. */
595 *bp_link
= prev_bp_link
;
598 debug_printf ("Failed to uninsert fast tracepoint jump "
599 "at 0x%s (%s) while deleting it.\n",
600 paddress (bp
->pc
), safe_strerror (ret
));
616 warning ("Could not find fast tracepoint jump in list.");
621 inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump
*jp
)
626 struct fast_tracepoint_jump
*
627 set_fast_tracepoint_jump (CORE_ADDR where
,
628 unsigned char *insn
, ULONGEST length
)
630 struct process_info
*proc
= current_process ();
631 struct fast_tracepoint_jump
*jp
;
635 /* We refcount fast tracepoint jumps. Check if we already know
636 about a jump at this address. */
637 jp
= find_fast_tracepoint_jump_at (where
);
644 /* We don't, so create a new object. Double the length, because the
645 flexible array member holds both the jump insn, and the
647 jp
= (struct fast_tracepoint_jump
*) xcalloc (1, sizeof (*jp
) + (length
* 2));
650 memcpy (fast_tracepoint_jump_insn (jp
), insn
, length
);
652 buf
= (unsigned char *) alloca (length
);
654 /* Note that there can be trap breakpoints inserted in the same
655 address range. To access the original memory contents, we use
656 `read_inferior_memory', which masks out breakpoints. */
657 err
= read_inferior_memory (where
, buf
, length
);
661 debug_printf ("Failed to read shadow memory of"
662 " fast tracepoint at 0x%s (%s).\n",
663 paddress (where
), safe_strerror (err
));
667 memcpy (fast_tracepoint_jump_shadow (jp
), buf
, length
);
669 /* Link the jump in. */
671 jp
->next
= proc
->fast_tracepoint_jumps
;
672 proc
->fast_tracepoint_jumps
= jp
;
674 /* Since there can be trap breakpoints inserted in the same address
675 range, we use use `target_write_memory', which takes care of
676 layering breakpoints on top of fast tracepoints, on top of the
677 buffer we pass it. This works because we've already linked in
678 the fast tracepoint jump above. Also note that we need to pass
679 the current shadow contents, because target_write_memory
680 updates any shadow memory with what we pass here, and we want
682 err
= target_write_memory (where
, buf
, length
);
686 debug_printf ("Failed to insert fast tracepoint jump at 0x%s (%s).\n",
687 paddress (where
), safe_strerror (err
));
690 proc
->fast_tracepoint_jumps
= jp
->next
;
700 uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc
)
702 struct fast_tracepoint_jump
*jp
;
705 jp
= find_fast_tracepoint_jump_at (pc
);
708 /* This can happen when we remove all breakpoints while handling
711 debug_printf ("Could not find fast tracepoint jump at 0x%s "
712 "in list (uninserting).\n",
723 /* Since there can be trap breakpoints inserted in the same
724 address range, we use use `target_write_memory', which
725 takes care of layering breakpoints on top of fast
726 tracepoints, and on top of the buffer we pass it. This works
727 because we've already marked the fast tracepoint fast
728 tracepoint jump uninserted above. Also note that we need to
729 pass the current shadow contents, because
730 target_write_memory updates any shadow memory with what we
731 pass here, and we want that to be a nop. */
732 buf
= (unsigned char *) alloca (jp
->length
);
733 memcpy (buf
, fast_tracepoint_jump_shadow (jp
), jp
->length
);
734 err
= target_write_memory (jp
->pc
, buf
, jp
->length
);
740 debug_printf ("Failed to uninsert fast tracepoint jump at"
742 paddress (pc
), safe_strerror (err
));
748 reinsert_fast_tracepoint_jumps_at (CORE_ADDR where
)
750 struct fast_tracepoint_jump
*jp
;
754 jp
= find_fast_tracepoint_jump_at (where
);
757 /* This can happen when we remove breakpoints when a tracepoint
758 hit causes a tracing stop, while handling a step-over. */
760 debug_printf ("Could not find fast tracepoint jump at 0x%s "
761 "in list (reinserting).\n",
767 error ("Jump already inserted at reinsert time.");
771 /* Since there can be trap breakpoints inserted in the same address
772 range, we use `target_write_memory', which takes care of
773 layering breakpoints on top of fast tracepoints, and on top of
774 the buffer we pass it. This works because we've already marked
775 the fast tracepoint jump inserted above. Also note that we need
776 to pass the current shadow contents, because
777 target_write_memory updates any shadow memory with what we pass
778 here, and we want that to be a nop. */
779 buf
= (unsigned char *) alloca (jp
->length
);
780 memcpy (buf
, fast_tracepoint_jump_shadow (jp
), jp
->length
);
781 err
= target_write_memory (where
, buf
, jp
->length
);
787 debug_printf ("Failed to reinsert fast tracepoint jump at"
789 paddress (where
), safe_strerror (err
));
793 /* Set a high-level breakpoint of type TYPE, with low level type
794 RAW_TYPE and kind KIND, at WHERE. On success, a pointer to the new
795 breakpoint is returned. On failure, returns NULL and writes the
796 error code to *ERR. HANDLER is called when the breakpoint is hit.
797 HANDLER should return 1 if the breakpoint should be deleted, 0
800 static struct breakpoint
*
801 set_breakpoint (enum bkpt_type type
, enum raw_bkpt_type raw_type
,
802 CORE_ADDR where
, int kind
,
803 int (*handler
) (CORE_ADDR
), int *err
)
805 struct process_info
*proc
= current_process ();
806 struct breakpoint
*bp
;
807 struct raw_breakpoint
*raw
;
809 raw
= set_raw_breakpoint_at (raw_type
, where
, kind
, err
);
817 if (is_gdb_breakpoint (type
))
819 struct gdb_breakpoint
*gdb_bp
= XCNEW (struct gdb_breakpoint
);
821 bp
= (struct breakpoint
*) gdb_bp
;
822 gdb_assert (handler
== NULL
);
824 else if (type
== other_breakpoint
)
826 struct other_breakpoint
*other_bp
= XCNEW (struct other_breakpoint
);
828 other_bp
->handler
= handler
;
829 bp
= (struct breakpoint
*) other_bp
;
831 else if (type
== single_step_breakpoint
)
833 struct single_step_breakpoint
*ss_bp
834 = XCNEW (struct single_step_breakpoint
);
836 bp
= (struct breakpoint
*) ss_bp
;
839 gdb_assert_not_reached ("unhandled breakpoint type");
844 bp
->next
= proc
->breakpoints
;
845 proc
->breakpoints
= bp
;
850 /* Set breakpoint of TYPE on address WHERE with handler HANDLER. */
852 static struct breakpoint
*
853 set_breakpoint_type_at (enum bkpt_type type
, CORE_ADDR where
,
854 int (*handler
) (CORE_ADDR
))
857 CORE_ADDR placed_address
= where
;
858 int breakpoint_kind
= target_breakpoint_kind_from_pc (&placed_address
);
860 return set_breakpoint (type
, raw_bkpt_type_sw
,
861 placed_address
, breakpoint_kind
, handler
,
865 /* See mem-break.h */
868 set_breakpoint_at (CORE_ADDR where
, int (*handler
) (CORE_ADDR
))
870 return set_breakpoint_type_at (other_breakpoint
, where
, handler
);
875 delete_raw_breakpoint (struct process_info
*proc
, struct raw_breakpoint
*todel
)
877 struct raw_breakpoint
*bp
, **bp_link
;
880 bp
= proc
->raw_breakpoints
;
881 bp_link
= &proc
->raw_breakpoints
;
887 if (bp
->inserted
> 0)
889 struct raw_breakpoint
*prev_bp_link
= *bp_link
;
893 ret
= the_target
->remove_point (bp
->raw_type
, bp
->pc
,
897 /* Something went wrong, relink the breakpoint. */
898 *bp_link
= prev_bp_link
;
901 debug_printf ("Failed to uninsert raw breakpoint "
902 "at 0x%s while deleting it.\n",
920 warning ("Could not find raw breakpoint in list.");
925 release_breakpoint (struct process_info
*proc
, struct breakpoint
*bp
)
930 newrefcount
= bp
->raw
->refcount
- 1;
931 if (newrefcount
== 0)
933 ret
= delete_raw_breakpoint (proc
, bp
->raw
);
938 bp
->raw
->refcount
= newrefcount
;
946 delete_breakpoint_1 (struct process_info
*proc
, struct breakpoint
*todel
)
948 struct breakpoint
*bp
, **bp_link
;
951 bp
= proc
->breakpoints
;
952 bp_link
= &proc
->breakpoints
;
960 err
= release_breakpoint (proc
, bp
);
974 warning ("Could not find breakpoint in list.");
979 delete_breakpoint (struct breakpoint
*todel
)
981 struct process_info
*proc
= current_process ();
982 return delete_breakpoint_1 (proc
, todel
);
985 /* Locate a GDB breakpoint of type Z_TYPE and kind KIND placed at
986 address ADDR and return a pointer to its structure. If KIND is -1,
987 the breakpoint's kind is ignored. */
989 static struct gdb_breakpoint
*
990 find_gdb_breakpoint (char z_type
, CORE_ADDR addr
, int kind
)
992 struct process_info
*proc
= current_process ();
993 struct breakpoint
*bp
;
994 enum bkpt_type type
= Z_packet_to_bkpt_type (z_type
);
996 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
997 if (bp
->type
== type
&& bp
->raw
->pc
== addr
998 && (kind
== -1 || bp
->raw
->kind
== kind
))
999 return (struct gdb_breakpoint
*) bp
;
1005 z_type_supported (char z_type
)
1007 return (z_type
>= '0' && z_type
<= '4'
1008 && the_target
->supports_z_point_type (z_type
));
1011 /* Create a new GDB breakpoint of type Z_TYPE at ADDR with kind KIND.
1012 Returns a pointer to the newly created breakpoint on success. On
1013 failure returns NULL and sets *ERR to either -1 for error, or 1 if
1014 Z_TYPE breakpoints are not supported on this target. */
1016 static struct gdb_breakpoint
*
1017 set_gdb_breakpoint_1 (char z_type
, CORE_ADDR addr
, int kind
, int *err
)
1019 struct gdb_breakpoint
*bp
;
1020 enum bkpt_type type
;
1021 enum raw_bkpt_type raw_type
;
1023 /* If we see GDB inserting a second code breakpoint at the same
1024 address, then either: GDB is updating the breakpoint's conditions
1025 or commands; or, the first breakpoint must have disappeared due
1026 to a shared library unload. On targets where the shared
1027 libraries are handled by userspace, like SVR4, for example,
1028 GDBserver can't tell if a library was loaded or unloaded. Since
1029 we refcount raw breakpoints, we must be careful to make sure GDB
1030 breakpoints never contribute more than one reference. if we
1031 didn't do this, in case the previous breakpoint is gone due to a
1032 shared library unload, we'd just increase the refcount of the
1033 previous breakpoint at this address, but the trap was not planted
1034 in the inferior anymore, thus the breakpoint would never be hit.
1035 Note this must be careful to not create a window where
1036 breakpoints are removed from the target, for non-stop, in case
1037 the target can poke at memory while the program is running. */
1038 if (z_type
== Z_PACKET_SW_BP
1039 || z_type
== Z_PACKET_HW_BP
)
1041 bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1045 if (bp
->base
.raw
->kind
!= kind
)
1047 /* A different kind than previously seen. The previous
1048 breakpoint must be gone then. */
1049 bp
->base
.raw
->inserted
= -1;
1050 delete_breakpoint ((struct breakpoint
*) bp
);
1053 else if (z_type
== Z_PACKET_SW_BP
)
1055 /* Check if the breakpoint is actually gone from the
1056 target, due to an solib unload, for example. Might
1057 as well validate _all_ breakpoints. */
1058 validate_breakpoints ();
1060 /* Breakpoints that don't pass validation are
1062 bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1068 /* Data breakpoints for the same address but different kind are
1069 expected. GDB doesn't merge these. The backend gets to do
1070 that if it wants/can. */
1071 bp
= find_gdb_breakpoint (z_type
, addr
, kind
);
1076 /* We already know about this breakpoint, there's nothing else
1077 to do - GDB's reference is already accounted for. Note that
1078 whether the breakpoint inserted is left as is - we may be
1079 stepping over it, for example, in which case we don't want to
1080 force-reinsert it. */
1084 raw_type
= Z_packet_to_raw_bkpt_type (z_type
);
1085 type
= Z_packet_to_bkpt_type (z_type
);
1086 return (struct gdb_breakpoint
*) set_breakpoint (type
, raw_type
, addr
,
1091 check_gdb_bp_preconditions (char z_type
, int *err
)
1093 /* As software/memory breakpoints work by poking at memory, we need
1094 to prepare to access memory. If that operation fails, we need to
1095 return error. Seeing an error, if this is the first breakpoint
1096 of that type that GDB tries to insert, GDB would then assume the
1097 breakpoint type is supported, but it may actually not be. So we
1098 need to check whether the type is supported at all before
1099 preparing to access memory. */
1100 if (!z_type_supported (z_type
))
1109 /* See mem-break.h. This is a wrapper for set_gdb_breakpoint_1 that
1110 knows to prepare to access memory for Z0 breakpoints. */
1112 struct gdb_breakpoint
*
1113 set_gdb_breakpoint (char z_type
, CORE_ADDR addr
, int kind
, int *err
)
1115 struct gdb_breakpoint
*bp
;
1117 if (!check_gdb_bp_preconditions (z_type
, err
))
1120 /* If inserting a software/memory breakpoint, need to prepare to
1122 if (z_type
== Z_PACKET_SW_BP
)
1124 if (prepare_to_access_memory () != 0)
1131 bp
= set_gdb_breakpoint_1 (z_type
, addr
, kind
, err
);
1133 if (z_type
== Z_PACKET_SW_BP
)
1134 done_accessing_memory ();
1139 /* Delete a GDB breakpoint of type Z_TYPE and kind KIND previously
1140 inserted at ADDR with set_gdb_breakpoint_at. Returns 0 on success,
1141 -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
1145 delete_gdb_breakpoint_1 (char z_type
, CORE_ADDR addr
, int kind
)
1147 struct gdb_breakpoint
*bp
;
1150 bp
= find_gdb_breakpoint (z_type
, addr
, kind
);
1154 /* Before deleting the breakpoint, make sure to free its condition
1155 and command lists. */
1156 clear_breakpoint_conditions_and_commands (bp
);
1157 err
= delete_breakpoint ((struct breakpoint
*) bp
);
1164 /* See mem-break.h. This is a wrapper for delete_gdb_breakpoint that
1165 knows to prepare to access memory for Z0 breakpoints. */
1168 delete_gdb_breakpoint (char z_type
, CORE_ADDR addr
, int kind
)
1172 if (!check_gdb_bp_preconditions (z_type
, &ret
))
1175 /* If inserting a software/memory breakpoint, need to prepare to
1177 if (z_type
== Z_PACKET_SW_BP
)
1181 err
= prepare_to_access_memory ();
1186 ret
= delete_gdb_breakpoint_1 (z_type
, addr
, kind
);
1188 if (z_type
== Z_PACKET_SW_BP
)
1189 done_accessing_memory ();
1194 /* Clear all conditions associated with a breakpoint. */
1197 clear_breakpoint_conditions (struct gdb_breakpoint
*bp
)
1199 struct point_cond_list
*cond
;
1201 if (bp
->cond_list
== NULL
)
1204 cond
= bp
->cond_list
;
1206 while (cond
!= NULL
)
1208 struct point_cond_list
*cond_next
;
1210 cond_next
= cond
->next
;
1211 gdb_free_agent_expr (cond
->cond
);
1216 bp
->cond_list
= NULL
;
1219 /* Clear all commands associated with a breakpoint. */
1222 clear_breakpoint_commands (struct gdb_breakpoint
*bp
)
1224 struct point_command_list
*cmd
;
1226 if (bp
->command_list
== NULL
)
1229 cmd
= bp
->command_list
;
1233 struct point_command_list
*cmd_next
;
1235 cmd_next
= cmd
->next
;
1236 gdb_free_agent_expr (cmd
->cmd
);
1241 bp
->command_list
= NULL
;
1245 clear_breakpoint_conditions_and_commands (struct gdb_breakpoint
*bp
)
1247 clear_breakpoint_conditions (bp
);
1248 clear_breakpoint_commands (bp
);
1251 /* Add condition CONDITION to GDBserver's breakpoint BP. */
1254 add_condition_to_breakpoint (struct gdb_breakpoint
*bp
,
1255 struct agent_expr
*condition
)
1257 struct point_cond_list
*new_cond
;
1259 /* Create new condition. */
1260 new_cond
= XCNEW (struct point_cond_list
);
1261 new_cond
->cond
= condition
;
1263 /* Add condition to the list. */
1264 new_cond
->next
= bp
->cond_list
;
1265 bp
->cond_list
= new_cond
;
1268 /* Add a target-side condition CONDITION to a breakpoint. */
1271 add_breakpoint_condition (struct gdb_breakpoint
*bp
, const char **condition
)
1273 const char *actparm
= *condition
;
1274 struct agent_expr
*cond
;
1276 if (condition
== NULL
)
1282 cond
= gdb_parse_agent_expr (&actparm
);
1286 warning ("Condition evaluation failed. Assuming unconditional.");
1290 add_condition_to_breakpoint (bp
, cond
);
1292 *condition
= actparm
;
1297 /* Evaluate condition (if any) at breakpoint BP. Return 1 if
1298 true and 0 otherwise. */
1301 gdb_condition_true_at_breakpoint_z_type (char z_type
, CORE_ADDR addr
)
1303 /* Fetch registers for the current inferior. */
1304 struct gdb_breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1306 struct point_cond_list
*cl
;
1308 struct eval_agent_expr_context ctx
;
1313 /* Check if the breakpoint is unconditional. If it is,
1314 the condition always evaluates to TRUE. */
1315 if (bp
->cond_list
== NULL
)
1318 ctx
.regcache
= get_thread_regcache (current_thread
, 1);
1322 /* Evaluate each condition in the breakpoint's list of conditions.
1323 Return true if any of the conditions evaluates to TRUE.
1325 If we failed to evaluate the expression, TRUE is returned. This
1326 forces GDB to reevaluate the conditions. */
1327 for (cl
= bp
->cond_list
;
1328 cl
&& !value
&& !err
; cl
= cl
->next
)
1330 /* Evaluate the condition. */
1331 err
= gdb_eval_agent_expr (&ctx
, cl
->cond
, &value
);
1337 return (value
!= 0);
1341 gdb_condition_true_at_breakpoint (CORE_ADDR where
)
1343 /* Only check code (software or hardware) breakpoints. */
1344 return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP
, where
)
1345 || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP
, where
));
1348 /* Add commands COMMANDS to GDBserver's breakpoint BP. */
1351 add_commands_to_breakpoint (struct gdb_breakpoint
*bp
,
1352 struct agent_expr
*commands
, int persist
)
1354 struct point_command_list
*new_cmd
;
1356 /* Create new command. */
1357 new_cmd
= XCNEW (struct point_command_list
);
1358 new_cmd
->cmd
= commands
;
1359 new_cmd
->persistence
= persist
;
1361 /* Add commands to the list. */
1362 new_cmd
->next
= bp
->command_list
;
1363 bp
->command_list
= new_cmd
;
1366 /* Add a target-side command COMMAND to the breakpoint at ADDR. */
1369 add_breakpoint_commands (struct gdb_breakpoint
*bp
, const char **command
,
1372 const char *actparm
= *command
;
1373 struct agent_expr
*cmd
;
1375 if (command
== NULL
)
1381 cmd
= gdb_parse_agent_expr (&actparm
);
1385 warning ("Command evaluation failed. Disabling.");
1389 add_commands_to_breakpoint (bp
, cmd
, persist
);
1396 /* Return true if there are no commands to run at this location,
1397 which likely means we want to report back to GDB. */
1400 gdb_no_commands_at_breakpoint_z_type (char z_type
, CORE_ADDR addr
)
1402 struct gdb_breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1408 debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s\n",
1409 paddress (addr
), z_type
,
1410 phex_nz ((uintptr_t) bp
->command_list
, 0));
1411 return (bp
->command_list
== NULL
);
1414 /* Return true if there are no commands to run at this location,
1415 which likely means we want to report back to GDB. */
1418 gdb_no_commands_at_breakpoint (CORE_ADDR where
)
1420 /* Only check code (software or hardware) breakpoints. */
1421 return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP
, where
)
1422 && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP
, where
));
1425 /* Run a breakpoint's commands. Returns 0 if there was a problem
1426 running any command, 1 otherwise. */
1429 run_breakpoint_commands_z_type (char z_type
, CORE_ADDR addr
)
1431 /* Fetch registers for the current inferior. */
1432 struct gdb_breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1434 struct point_command_list
*cl
;
1436 struct eval_agent_expr_context ctx
;
1441 ctx
.regcache
= get_thread_regcache (current_thread
, 1);
1445 for (cl
= bp
->command_list
;
1446 cl
&& !value
&& !err
; cl
= cl
->next
)
1448 /* Run the command. */
1449 err
= gdb_eval_agent_expr (&ctx
, cl
->cmd
, &value
);
1451 /* If one command has a problem, stop digging the hole deeper. */
1460 run_breakpoint_commands (CORE_ADDR where
)
1462 /* Only check code (software or hardware) breakpoints. If one
1463 command has a problem, stop digging the hole deeper. */
1464 if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP
, where
))
1465 run_breakpoint_commands_z_type (Z_PACKET_HW_BP
, where
);
1468 /* See mem-break.h. */
1471 gdb_breakpoint_here (CORE_ADDR where
)
1473 /* Only check code (software or hardware) breakpoints. */
1474 return (find_gdb_breakpoint (Z_PACKET_SW_BP
, where
, -1) != NULL
1475 || find_gdb_breakpoint (Z_PACKET_HW_BP
, where
, -1) != NULL
);
1479 set_single_step_breakpoint (CORE_ADDR stop_at
, ptid_t ptid
)
1481 struct single_step_breakpoint
*bp
;
1483 gdb_assert (current_ptid
.pid () == ptid
.pid ());
1485 bp
= (struct single_step_breakpoint
*) set_breakpoint_type_at (single_step_breakpoint
,
1491 delete_single_step_breakpoints (struct thread_info
*thread
)
1493 struct process_info
*proc
= get_thread_process (thread
);
1494 struct breakpoint
*bp
, **bp_link
;
1496 bp
= proc
->breakpoints
;
1497 bp_link
= &proc
->breakpoints
;
1501 if (bp
->type
== single_step_breakpoint
1502 && ((struct single_step_breakpoint
*) bp
)->ptid
== ptid_of (thread
))
1504 scoped_restore_current_thread restore_thread
;
1506 switch_to_thread (thread
);
1507 *bp_link
= bp
->next
;
1508 release_breakpoint (proc
, bp
);
1513 bp_link
= &bp
->next
;
1520 uninsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1522 if (bp
->inserted
< 0)
1525 debug_printf ("Breakpoint at %s is marked insert-disabled.\n",
1528 else if (bp
->inserted
> 0)
1534 err
= the_target
->remove_point (bp
->raw_type
, bp
->pc
, bp
->kind
, bp
);
1540 debug_printf ("Failed to uninsert raw breakpoint at 0x%s.\n",
1547 uninsert_breakpoints_at (CORE_ADDR pc
)
1549 struct process_info
*proc
= current_process ();
1550 struct raw_breakpoint
*bp
;
1553 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1554 if ((bp
->raw_type
== raw_bkpt_type_sw
1555 || bp
->raw_type
== raw_bkpt_type_hw
)
1561 uninsert_raw_breakpoint (bp
);
1566 /* This can happen when we remove all breakpoints while handling
1569 debug_printf ("Could not find breakpoint at 0x%s "
1570 "in list (uninserting).\n",
1576 uninsert_all_breakpoints (void)
1578 struct process_info
*proc
= current_process ();
1579 struct raw_breakpoint
*bp
;
1581 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1582 if ((bp
->raw_type
== raw_bkpt_type_sw
1583 || bp
->raw_type
== raw_bkpt_type_hw
)
1585 uninsert_raw_breakpoint (bp
);
1589 uninsert_single_step_breakpoints (struct thread_info
*thread
)
1591 struct process_info
*proc
= get_thread_process (thread
);
1592 struct breakpoint
*bp
;
1594 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1596 if (bp
->type
== single_step_breakpoint
1597 && ((struct single_step_breakpoint
*) bp
)->ptid
== ptid_of (thread
))
1599 gdb_assert (bp
->raw
->inserted
> 0);
1601 /* Only uninsert the raw breakpoint if it only belongs to a
1602 reinsert breakpoint. */
1603 if (bp
->raw
->refcount
== 1)
1605 scoped_restore_current_thread restore_thread
;
1607 switch_to_thread (thread
);
1608 uninsert_raw_breakpoint (bp
->raw
);
1615 reinsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1622 err
= the_target
->insert_point (bp
->raw_type
, bp
->pc
, bp
->kind
, bp
);
1625 else if (debug_threads
)
1626 debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).\n",
1627 paddress (bp
->pc
), err
);
1631 reinsert_breakpoints_at (CORE_ADDR pc
)
1633 struct process_info
*proc
= current_process ();
1634 struct raw_breakpoint
*bp
;
1637 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1638 if ((bp
->raw_type
== raw_bkpt_type_sw
1639 || bp
->raw_type
== raw_bkpt_type_hw
)
1644 reinsert_raw_breakpoint (bp
);
1649 /* This can happen when we remove all breakpoints while handling
1652 debug_printf ("Could not find raw breakpoint at 0x%s "
1653 "in list (reinserting).\n",
1659 has_single_step_breakpoints (struct thread_info
*thread
)
1661 struct process_info
*proc
= get_thread_process (thread
);
1662 struct breakpoint
*bp
, **bp_link
;
1664 bp
= proc
->breakpoints
;
1665 bp_link
= &proc
->breakpoints
;
1669 if (bp
->type
== single_step_breakpoint
1670 && ((struct single_step_breakpoint
*) bp
)->ptid
== ptid_of (thread
))
1674 bp_link
= &bp
->next
;
1683 reinsert_all_breakpoints (void)
1685 struct process_info
*proc
= current_process ();
1686 struct raw_breakpoint
*bp
;
1688 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1689 if ((bp
->raw_type
== raw_bkpt_type_sw
1690 || bp
->raw_type
== raw_bkpt_type_hw
)
1692 reinsert_raw_breakpoint (bp
);
1696 reinsert_single_step_breakpoints (struct thread_info
*thread
)
1698 struct process_info
*proc
= get_thread_process (thread
);
1699 struct breakpoint
*bp
;
1701 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1703 if (bp
->type
== single_step_breakpoint
1704 && ((struct single_step_breakpoint
*) bp
)->ptid
== ptid_of (thread
))
1706 gdb_assert (bp
->raw
->inserted
> 0);
1708 if (bp
->raw
->refcount
== 1)
1710 scoped_restore_current_thread restore_thread
;
1712 switch_to_thread (thread
);
1713 reinsert_raw_breakpoint (bp
->raw
);
1720 check_breakpoints (CORE_ADDR stop_pc
)
1722 struct process_info
*proc
= current_process ();
1723 struct breakpoint
*bp
, **bp_link
;
1725 bp
= proc
->breakpoints
;
1726 bp_link
= &proc
->breakpoints
;
1730 struct raw_breakpoint
*raw
= bp
->raw
;
1732 if ((raw
->raw_type
== raw_bkpt_type_sw
1733 || raw
->raw_type
== raw_bkpt_type_hw
)
1734 && raw
->pc
== stop_pc
)
1738 warning ("Hit a removed breakpoint?");
1742 if (bp
->type
== other_breakpoint
)
1744 struct other_breakpoint
*other_bp
1745 = (struct other_breakpoint
*) bp
;
1747 if (other_bp
->handler
!= NULL
&& (*other_bp
->handler
) (stop_pc
))
1749 *bp_link
= bp
->next
;
1751 release_breakpoint (proc
, bp
);
1759 bp_link
= &bp
->next
;
1765 breakpoint_here (CORE_ADDR addr
)
1767 struct process_info
*proc
= current_process ();
1768 struct raw_breakpoint
*bp
;
1770 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1771 if ((bp
->raw_type
== raw_bkpt_type_sw
1772 || bp
->raw_type
== raw_bkpt_type_hw
)
1780 breakpoint_inserted_here (CORE_ADDR addr
)
1782 struct process_info
*proc
= current_process ();
1783 struct raw_breakpoint
*bp
;
1785 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1786 if ((bp
->raw_type
== raw_bkpt_type_sw
1787 || bp
->raw_type
== raw_bkpt_type_hw
)
1795 /* See mem-break.h. */
1798 software_breakpoint_inserted_here (CORE_ADDR addr
)
1800 struct process_info
*proc
= current_process ();
1801 struct raw_breakpoint
*bp
;
1803 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1804 if (bp
->raw_type
== raw_bkpt_type_sw
1812 /* See mem-break.h. */
1815 hardware_breakpoint_inserted_here (CORE_ADDR addr
)
1817 struct process_info
*proc
= current_process ();
1818 struct raw_breakpoint
*bp
;
1820 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1821 if (bp
->raw_type
== raw_bkpt_type_hw
1829 /* See mem-break.h. */
1832 single_step_breakpoint_inserted_here (CORE_ADDR addr
)
1834 struct process_info
*proc
= current_process ();
1835 struct breakpoint
*bp
;
1837 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1838 if (bp
->type
== single_step_breakpoint
1839 && bp
->raw
->pc
== addr
1840 && bp
->raw
->inserted
)
1847 validate_inserted_breakpoint (struct raw_breakpoint
*bp
)
1852 gdb_assert (bp
->inserted
);
1853 gdb_assert (bp
->raw_type
== raw_bkpt_type_sw
);
1855 buf
= (unsigned char *) alloca (bp_size (bp
));
1856 err
= the_target
->read_memory (bp
->pc
, buf
, bp_size (bp
));
1857 if (err
|| memcmp (buf
, bp_opcode (bp
), bp_size (bp
)) != 0)
1859 /* Tag it as gone. */
1868 delete_disabled_breakpoints (void)
1870 struct process_info
*proc
= current_process ();
1871 struct breakpoint
*bp
, *next
;
1873 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= next
)
1876 if (bp
->raw
->inserted
< 0)
1878 /* If single_step_breakpoints become disabled, that means the
1879 manipulations (insertion and removal) of them are wrong. */
1880 gdb_assert (bp
->type
!= single_step_breakpoint
);
1881 delete_breakpoint_1 (proc
, bp
);
1886 /* Check if breakpoints we inserted still appear to be inserted. They
1887 may disappear due to a shared library unload, and worse, a new
1888 shared library may be reloaded at the same address as the
1889 previously unloaded one. If that happens, we should make sure that
1890 the shadow memory of the old breakpoints isn't used when reading or
1894 validate_breakpoints (void)
1896 struct process_info
*proc
= current_process ();
1897 struct breakpoint
*bp
;
1899 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1901 struct raw_breakpoint
*raw
= bp
->raw
;
1903 if (raw
->raw_type
== raw_bkpt_type_sw
&& raw
->inserted
> 0)
1904 validate_inserted_breakpoint (raw
);
1907 delete_disabled_breakpoints ();
1911 check_mem_read (CORE_ADDR mem_addr
, unsigned char *buf
, int mem_len
)
1913 struct process_info
*proc
= current_process ();
1914 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
1915 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
1916 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
1917 int disabled_one
= 0;
1919 for (; jp
!= NULL
; jp
= jp
->next
)
1921 CORE_ADDR bp_end
= jp
->pc
+ jp
->length
;
1922 CORE_ADDR start
, end
;
1923 int copy_offset
, copy_len
, buf_offset
;
1925 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= buf
+ mem_len
1926 || buf
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
1928 if (mem_addr
>= bp_end
)
1930 if (jp
->pc
>= mem_end
)
1934 if (mem_addr
> start
)
1941 copy_len
= end
- start
;
1942 copy_offset
= start
- jp
->pc
;
1943 buf_offset
= start
- mem_addr
;
1946 memcpy (buf
+ buf_offset
,
1947 fast_tracepoint_jump_shadow (jp
) + copy_offset
,
1951 for (; bp
!= NULL
; bp
= bp
->next
)
1953 CORE_ADDR bp_end
= bp
->pc
+ bp_size (bp
);
1954 CORE_ADDR start
, end
;
1955 int copy_offset
, copy_len
, buf_offset
;
1957 if (bp
->raw_type
!= raw_bkpt_type_sw
)
1960 gdb_assert (bp
->old_data
>= buf
+ mem_len
1961 || buf
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
1963 if (mem_addr
>= bp_end
)
1965 if (bp
->pc
>= mem_end
)
1969 if (mem_addr
> start
)
1976 copy_len
= end
- start
;
1977 copy_offset
= start
- bp
->pc
;
1978 buf_offset
= start
- mem_addr
;
1980 if (bp
->inserted
> 0)
1982 if (validate_inserted_breakpoint (bp
))
1983 memcpy (buf
+ buf_offset
, bp
->old_data
+ copy_offset
, copy_len
);
1990 delete_disabled_breakpoints ();
1994 check_mem_write (CORE_ADDR mem_addr
, unsigned char *buf
,
1995 const unsigned char *myaddr
, int mem_len
)
1997 struct process_info
*proc
= current_process ();
1998 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
1999 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
2000 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
2001 int disabled_one
= 0;
2003 /* First fast tracepoint jumps, then breakpoint traps on top. */
2005 for (; jp
!= NULL
; jp
= jp
->next
)
2007 CORE_ADDR jp_end
= jp
->pc
+ jp
->length
;
2008 CORE_ADDR start
, end
;
2009 int copy_offset
, copy_len
, buf_offset
;
2011 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= myaddr
+ mem_len
2012 || myaddr
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
2013 gdb_assert (fast_tracepoint_jump_insn (jp
) >= buf
+ mem_len
2014 || buf
>= fast_tracepoint_jump_insn (jp
) + (jp
)->length
);
2016 if (mem_addr
>= jp_end
)
2018 if (jp
->pc
>= mem_end
)
2022 if (mem_addr
> start
)
2029 copy_len
= end
- start
;
2030 copy_offset
= start
- jp
->pc
;
2031 buf_offset
= start
- mem_addr
;
2033 memcpy (fast_tracepoint_jump_shadow (jp
) + copy_offset
,
2034 myaddr
+ buf_offset
, copy_len
);
2036 memcpy (buf
+ buf_offset
,
2037 fast_tracepoint_jump_insn (jp
) + copy_offset
, copy_len
);
2040 for (; bp
!= NULL
; bp
= bp
->next
)
2042 CORE_ADDR bp_end
= bp
->pc
+ bp_size (bp
);
2043 CORE_ADDR start
, end
;
2044 int copy_offset
, copy_len
, buf_offset
;
2046 if (bp
->raw_type
!= raw_bkpt_type_sw
)
2049 gdb_assert (bp
->old_data
>= myaddr
+ mem_len
2050 || myaddr
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
2052 if (mem_addr
>= bp_end
)
2054 if (bp
->pc
>= mem_end
)
2058 if (mem_addr
> start
)
2065 copy_len
= end
- start
;
2066 copy_offset
= start
- bp
->pc
;
2067 buf_offset
= start
- mem_addr
;
2069 memcpy (bp
->old_data
+ copy_offset
, myaddr
+ buf_offset
, copy_len
);
2070 if (bp
->inserted
> 0)
2072 if (validate_inserted_breakpoint (bp
))
2073 memcpy (buf
+ buf_offset
, bp_opcode (bp
) + copy_offset
, copy_len
);
2080 delete_disabled_breakpoints ();
2083 /* Delete all breakpoints, and un-insert them from the inferior. */
2086 delete_all_breakpoints (void)
2088 struct process_info
*proc
= current_process ();
2090 while (proc
->breakpoints
)
2091 delete_breakpoint_1 (proc
, proc
->breakpoints
);
2094 /* Clear the "inserted" flag in all breakpoints. */
2097 mark_breakpoints_out (struct process_info
*proc
)
2099 struct raw_breakpoint
*raw_bp
;
2101 for (raw_bp
= proc
->raw_breakpoints
; raw_bp
!= NULL
; raw_bp
= raw_bp
->next
)
2102 raw_bp
->inserted
= 0;
2105 /* Release all breakpoints, but do not try to un-insert them from the
2109 free_all_breakpoints (struct process_info
*proc
)
2111 mark_breakpoints_out (proc
);
2113 /* Note: use PROC explicitly instead of deferring to
2114 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
2115 released when we get here. There should be no call to
2116 current_process from here on. */
2117 while (proc
->breakpoints
)
2118 delete_breakpoint_1 (proc
, proc
->breakpoints
);
2121 /* Clone an agent expression. */
2123 static struct agent_expr
*
2124 clone_agent_expr (const struct agent_expr
*src_ax
)
2126 struct agent_expr
*ax
;
2128 ax
= XCNEW (struct agent_expr
);
2129 ax
->length
= src_ax
->length
;
2130 ax
->bytes
= (unsigned char *) xcalloc (ax
->length
, 1);
2131 memcpy (ax
->bytes
, src_ax
->bytes
, ax
->length
);
2135 /* Deep-copy the contents of one breakpoint to another. */
2137 static struct breakpoint
*
2138 clone_one_breakpoint (const struct breakpoint
*src
, ptid_t ptid
)
2140 struct breakpoint
*dest
;
2141 struct raw_breakpoint
*dest_raw
;
2143 /* Clone the raw breakpoint. */
2144 dest_raw
= XCNEW (struct raw_breakpoint
);
2145 dest_raw
->raw_type
= src
->raw
->raw_type
;
2146 dest_raw
->refcount
= src
->raw
->refcount
;
2147 dest_raw
->pc
= src
->raw
->pc
;
2148 dest_raw
->kind
= src
->raw
->kind
;
2149 memcpy (dest_raw
->old_data
, src
->raw
->old_data
, MAX_BREAKPOINT_LEN
);
2150 dest_raw
->inserted
= src
->raw
->inserted
;
2152 /* Clone the high-level breakpoint. */
2153 if (is_gdb_breakpoint (src
->type
))
2155 struct gdb_breakpoint
*gdb_dest
= XCNEW (struct gdb_breakpoint
);
2156 struct point_cond_list
*current_cond
;
2157 struct point_cond_list
*new_cond
;
2158 struct point_cond_list
*cond_tail
= NULL
;
2159 struct point_command_list
*current_cmd
;
2160 struct point_command_list
*new_cmd
;
2161 struct point_command_list
*cmd_tail
= NULL
;
2163 /* Clone the condition list. */
2164 for (current_cond
= ((struct gdb_breakpoint
*) src
)->cond_list
;
2165 current_cond
!= NULL
;
2166 current_cond
= current_cond
->next
)
2168 new_cond
= XCNEW (struct point_cond_list
);
2169 new_cond
->cond
= clone_agent_expr (current_cond
->cond
);
2170 APPEND_TO_LIST (&gdb_dest
->cond_list
, new_cond
, cond_tail
);
2173 /* Clone the command list. */
2174 for (current_cmd
= ((struct gdb_breakpoint
*) src
)->command_list
;
2175 current_cmd
!= NULL
;
2176 current_cmd
= current_cmd
->next
)
2178 new_cmd
= XCNEW (struct point_command_list
);
2179 new_cmd
->cmd
= clone_agent_expr (current_cmd
->cmd
);
2180 new_cmd
->persistence
= current_cmd
->persistence
;
2181 APPEND_TO_LIST (&gdb_dest
->command_list
, new_cmd
, cmd_tail
);
2184 dest
= (struct breakpoint
*) gdb_dest
;
2186 else if (src
->type
== other_breakpoint
)
2188 struct other_breakpoint
*other_dest
= XCNEW (struct other_breakpoint
);
2190 other_dest
->handler
= ((struct other_breakpoint
*) src
)->handler
;
2191 dest
= (struct breakpoint
*) other_dest
;
2193 else if (src
->type
== single_step_breakpoint
)
2195 struct single_step_breakpoint
*ss_dest
2196 = XCNEW (struct single_step_breakpoint
);
2198 dest
= (struct breakpoint
*) ss_dest
;
2199 /* Since single-step breakpoint is thread specific, don't copy
2200 thread id from SRC, use ID instead. */
2201 ss_dest
->ptid
= ptid
;
2204 gdb_assert_not_reached ("unhandled breakpoint type");
2206 dest
->type
= src
->type
;
2207 dest
->raw
= dest_raw
;
2212 /* See mem-break.h. */
2215 clone_all_breakpoints (struct thread_info
*child_thread
,
2216 const struct thread_info
*parent_thread
)
2218 const struct breakpoint
*bp
;
2219 struct breakpoint
*new_bkpt
;
2220 struct breakpoint
*bkpt_tail
= NULL
;
2221 struct raw_breakpoint
*raw_bkpt_tail
= NULL
;
2222 struct process_info
*child_proc
= get_thread_process (child_thread
);
2223 struct process_info
*parent_proc
= get_thread_process (parent_thread
);
2224 struct breakpoint
**new_list
= &child_proc
->breakpoints
;
2225 struct raw_breakpoint
**new_raw_list
= &child_proc
->raw_breakpoints
;
2227 for (bp
= parent_proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
2229 new_bkpt
= clone_one_breakpoint (bp
, ptid_of (child_thread
));
2230 APPEND_TO_LIST (new_list
, new_bkpt
, bkpt_tail
);
2231 APPEND_TO_LIST (new_raw_list
, new_bkpt
->raw
, raw_bkpt_tail
);