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
));
374 threads_debug_printf ("Failed to read shadow memory of"
375 " breakpoint at 0x%s (%s).",
376 paddress (bp
->pc
), safe_strerror (err
));
380 memcpy (bp
->old_data
, buf
, bp_size (bp
));
382 err
= the_target
->write_memory (bp
->pc
, bp_opcode (bp
),
385 threads_debug_printf ("Failed to insert breakpoint at 0x%s (%s).",
386 paddress (bp
->pc
), safe_strerror (err
));
388 return err
!= 0 ? -1 : 0;
391 /* See mem-break.h */
394 remove_memory_breakpoint (struct raw_breakpoint
*bp
)
396 unsigned char buf
[MAX_BREAKPOINT_LEN
];
399 /* Since there can be trap breakpoints inserted in the same address
400 range, we use `target_write_memory', which takes care of
401 layering breakpoints on top of fast tracepoints, and on top of
402 the buffer we pass it. This works because the caller has already
403 either unlinked the breakpoint or marked it uninserted. Also
404 note that we need to pass the current shadow contents, because
405 target_write_memory updates any shadow memory with what we pass
406 here, and we want that to be a nop. */
407 memcpy (buf
, bp
->old_data
, bp_size (bp
));
408 err
= target_write_memory (bp
->pc
, buf
, bp_size (bp
));
410 threads_debug_printf ("Failed to uninsert raw breakpoint "
411 "at 0x%s (%s) while deleting it.",
412 paddress (bp
->pc
), safe_strerror (err
));
414 return err
!= 0 ? -1 : 0;
417 /* Set a RAW breakpoint of type TYPE and kind KIND at WHERE. On
418 success, a pointer to the new breakpoint is returned. On failure,
419 returns NULL and writes the error code to *ERR. */
421 static struct raw_breakpoint
*
422 set_raw_breakpoint_at (enum raw_bkpt_type type
, CORE_ADDR where
, int kind
,
425 struct process_info
*proc
= current_process ();
426 struct raw_breakpoint
*bp
;
428 if (type
== raw_bkpt_type_sw
|| type
== raw_bkpt_type_hw
)
430 bp
= find_enabled_raw_code_breakpoint_at (where
, type
);
431 if (bp
!= NULL
&& bp
->kind
!= kind
)
433 /* A different kind than previously seen. The previous
434 breakpoint must be gone then. */
436 ("Inconsistent breakpoint kind? Was %d, now %d.",
443 bp
= find_raw_breakpoint_at (where
, type
, kind
);
445 gdb::unique_xmalloc_ptr
<struct raw_breakpoint
> bp_holder
;
448 bp_holder
.reset (XCNEW (struct raw_breakpoint
));
449 bp
= bp_holder
.get ();
457 *err
= the_target
->insert_point (bp
->raw_type
, bp
->pc
, bp
->kind
, bp
);
460 threads_debug_printf ("Failed to insert breakpoint at 0x%s (%d).",
461 paddress (where
), *err
);
469 /* If the breakpoint was allocated above, we know we want to keep it
471 bp_holder
.release ();
473 /* Link the breakpoint in, if this is the first reference. */
474 if (++bp
->refcount
== 1)
476 bp
->next
= proc
->raw_breakpoints
;
477 proc
->raw_breakpoints
= bp
;
482 /* Notice that breakpoint traps are always installed on top of fast
483 tracepoint jumps. This is even if the fast tracepoint is installed
484 at a later time compared to when the breakpoint was installed.
485 This means that a stopping breakpoint or tracepoint has higher
486 "priority". In turn, this allows having fast and slow tracepoints
487 (and breakpoints) at the same address behave correctly. */
490 /* A fast tracepoint jump. */
492 struct fast_tracepoint_jump
494 struct fast_tracepoint_jump
*next
;
496 /* A reference count. GDB can install more than one fast tracepoint
497 at the same address (each with its own action list, for
501 /* The fast tracepoint's insertion address. There can only be one
502 of these for a given PC. */
505 /* Non-zero if this fast tracepoint jump is currently inserted in
509 /* The length of the jump instruction. */
512 /* A poor-man's flexible array member, holding both the jump
513 instruction to insert, and a copy of the instruction that would
514 be in memory had not been a jump there (the shadow memory of the
516 unsigned char insn_and_shadow
[0];
519 /* Fast tracepoint FP's jump instruction to insert. */
520 #define fast_tracepoint_jump_insn(fp) \
521 ((fp)->insn_and_shadow + 0)
523 /* The shadow memory of fast tracepoint jump FP. */
524 #define fast_tracepoint_jump_shadow(fp) \
525 ((fp)->insn_and_shadow + (fp)->length)
528 /* Return the fast tracepoint jump set at WHERE. */
530 static struct fast_tracepoint_jump
*
531 find_fast_tracepoint_jump_at (CORE_ADDR where
)
533 struct process_info
*proc
= current_process ();
534 struct fast_tracepoint_jump
*jp
;
536 for (jp
= proc
->fast_tracepoint_jumps
; jp
!= NULL
; jp
= jp
->next
)
544 fast_tracepoint_jump_here (CORE_ADDR where
)
546 struct fast_tracepoint_jump
*jp
= find_fast_tracepoint_jump_at (where
);
552 delete_fast_tracepoint_jump (struct fast_tracepoint_jump
*todel
)
554 struct fast_tracepoint_jump
*bp
, **bp_link
;
556 struct process_info
*proc
= current_process ();
558 bp
= proc
->fast_tracepoint_jumps
;
559 bp_link
= &proc
->fast_tracepoint_jumps
;
565 if (--bp
->refcount
== 0)
567 struct fast_tracepoint_jump
*prev_bp_link
= *bp_link
;
573 /* Since there can be breakpoints inserted in the same
574 address range, we use `target_write_memory', which
575 takes care of layering breakpoints on top of fast
576 tracepoints, and on top of the buffer we pass it.
577 This works because we've already unlinked the fast
578 tracepoint jump above. Also note that we need to
579 pass the current shadow contents, because
580 target_write_memory updates any shadow memory with
581 what we pass here, and we want that to be a nop. */
582 buf
= (unsigned char *) alloca (bp
->length
);
583 memcpy (buf
, fast_tracepoint_jump_shadow (bp
), bp
->length
);
584 ret
= target_write_memory (bp
->pc
, buf
, bp
->length
);
587 /* Something went wrong, relink the jump. */
588 *bp_link
= prev_bp_link
;
591 ("Failed to uninsert fast tracepoint jump "
592 "at 0x%s (%s) while deleting it.",
593 paddress (bp
->pc
), safe_strerror (ret
));
609 warning ("Could not find fast tracepoint jump in list.");
614 inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump
*jp
)
619 struct fast_tracepoint_jump
*
620 set_fast_tracepoint_jump (CORE_ADDR where
,
621 unsigned char *insn
, ULONGEST length
)
623 struct process_info
*proc
= current_process ();
624 struct fast_tracepoint_jump
*jp
;
628 /* We refcount fast tracepoint jumps. Check if we already know
629 about a jump at this address. */
630 jp
= find_fast_tracepoint_jump_at (where
);
637 /* We don't, so create a new object. Double the length, because the
638 flexible array member holds both the jump insn, and the
640 jp
= (struct fast_tracepoint_jump
*) xcalloc (1, sizeof (*jp
) + (length
* 2));
643 memcpy (fast_tracepoint_jump_insn (jp
), insn
, length
);
645 buf
= (unsigned char *) alloca (length
);
647 /* Note that there can be trap breakpoints inserted in the same
648 address range. To access the original memory contents, we use
649 `read_inferior_memory', which masks out breakpoints. */
650 err
= read_inferior_memory (where
, buf
, length
);
653 threads_debug_printf ("Failed to read shadow memory of"
654 " fast tracepoint at 0x%s (%s).",
655 paddress (where
), safe_strerror (err
));
659 memcpy (fast_tracepoint_jump_shadow (jp
), buf
, length
);
661 /* Link the jump in. */
663 jp
->next
= proc
->fast_tracepoint_jumps
;
664 proc
->fast_tracepoint_jumps
= jp
;
666 /* Since there can be trap breakpoints inserted in the same address
667 range, we use use `target_write_memory', which takes care of
668 layering breakpoints on top of fast tracepoints, on top of the
669 buffer we pass it. This works because we've already linked in
670 the fast tracepoint jump above. Also note that we need to pass
671 the current shadow contents, because target_write_memory
672 updates any shadow memory with what we pass here, and we want
674 err
= target_write_memory (where
, buf
, length
);
678 ("Failed to insert fast tracepoint jump at 0x%s (%s).",
679 paddress (where
), safe_strerror (err
));
682 proc
->fast_tracepoint_jumps
= jp
->next
;
692 uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc
)
694 struct fast_tracepoint_jump
*jp
;
697 jp
= find_fast_tracepoint_jump_at (pc
);
700 /* This can happen when we remove all breakpoints while handling
702 threads_debug_printf ("Could not find fast tracepoint jump at 0x%s "
703 "in list (uninserting).",
714 /* Since there can be trap breakpoints inserted in the same
715 address range, we use use `target_write_memory', which
716 takes care of layering breakpoints on top of fast
717 tracepoints, and on top of the buffer we pass it. This works
718 because we've already marked the fast tracepoint fast
719 tracepoint jump uninserted above. Also note that we need to
720 pass the current shadow contents, because
721 target_write_memory updates any shadow memory with what we
722 pass here, and we want that to be a nop. */
723 buf
= (unsigned char *) alloca (jp
->length
);
724 memcpy (buf
, fast_tracepoint_jump_shadow (jp
), jp
->length
);
725 err
= target_write_memory (jp
->pc
, buf
, jp
->length
);
730 threads_debug_printf ("Failed to uninsert fast tracepoint jump at"
732 paddress (pc
), safe_strerror (err
));
738 reinsert_fast_tracepoint_jumps_at (CORE_ADDR where
)
740 struct fast_tracepoint_jump
*jp
;
744 jp
= find_fast_tracepoint_jump_at (where
);
747 /* This can happen when we remove breakpoints when a tracepoint
748 hit causes a tracing stop, while handling a step-over. */
749 threads_debug_printf ("Could not find fast tracepoint jump at 0x%s "
750 "in list (reinserting).",
756 error ("Jump already inserted at reinsert time.");
760 /* Since there can be trap breakpoints inserted in the same address
761 range, we use `target_write_memory', which takes care of
762 layering breakpoints on top of fast tracepoints, and on top of
763 the buffer we pass it. This works because we've already marked
764 the fast tracepoint jump inserted above. Also note that we need
765 to pass the current shadow contents, because
766 target_write_memory updates any shadow memory with what we pass
767 here, and we want that to be a nop. */
768 buf
= (unsigned char *) alloca (jp
->length
);
769 memcpy (buf
, fast_tracepoint_jump_shadow (jp
), jp
->length
);
770 err
= target_write_memory (where
, buf
, jp
->length
);
775 threads_debug_printf ("Failed to reinsert fast tracepoint jump at"
777 paddress (where
), safe_strerror (err
));
781 /* Set a high-level breakpoint of type TYPE, with low level type
782 RAW_TYPE and kind KIND, at WHERE. On success, a pointer to the new
783 breakpoint is returned. On failure, returns NULL and writes the
784 error code to *ERR. HANDLER is called when the breakpoint is hit.
785 HANDLER should return 1 if the breakpoint should be deleted, 0
788 static struct breakpoint
*
789 set_breakpoint (enum bkpt_type type
, enum raw_bkpt_type raw_type
,
790 CORE_ADDR where
, int kind
,
791 int (*handler
) (CORE_ADDR
), int *err
)
793 struct process_info
*proc
= current_process ();
794 struct breakpoint
*bp
;
795 struct raw_breakpoint
*raw
;
797 raw
= set_raw_breakpoint_at (raw_type
, where
, kind
, err
);
805 if (is_gdb_breakpoint (type
))
807 struct gdb_breakpoint
*gdb_bp
= XCNEW (struct gdb_breakpoint
);
809 bp
= (struct breakpoint
*) gdb_bp
;
810 gdb_assert (handler
== NULL
);
812 else if (type
== other_breakpoint
)
814 struct other_breakpoint
*other_bp
= XCNEW (struct other_breakpoint
);
816 other_bp
->handler
= handler
;
817 bp
= (struct breakpoint
*) other_bp
;
819 else if (type
== single_step_breakpoint
)
821 struct single_step_breakpoint
*ss_bp
822 = XCNEW (struct single_step_breakpoint
);
824 bp
= (struct breakpoint
*) ss_bp
;
827 gdb_assert_not_reached ("unhandled breakpoint type");
832 bp
->next
= proc
->breakpoints
;
833 proc
->breakpoints
= bp
;
838 /* Set breakpoint of TYPE on address WHERE with handler HANDLER. */
840 static struct breakpoint
*
841 set_breakpoint_type_at (enum bkpt_type type
, CORE_ADDR where
,
842 int (*handler
) (CORE_ADDR
))
845 CORE_ADDR placed_address
= where
;
846 int breakpoint_kind
= target_breakpoint_kind_from_pc (&placed_address
);
848 return set_breakpoint (type
, raw_bkpt_type_sw
,
849 placed_address
, breakpoint_kind
, handler
,
853 /* See mem-break.h */
856 set_breakpoint_at (CORE_ADDR where
, int (*handler
) (CORE_ADDR
))
858 return set_breakpoint_type_at (other_breakpoint
, where
, handler
);
863 delete_raw_breakpoint (struct process_info
*proc
, struct raw_breakpoint
*todel
)
865 struct raw_breakpoint
*bp
, **bp_link
;
868 bp
= proc
->raw_breakpoints
;
869 bp_link
= &proc
->raw_breakpoints
;
875 if (bp
->inserted
> 0)
877 struct raw_breakpoint
*prev_bp_link
= *bp_link
;
881 ret
= the_target
->remove_point (bp
->raw_type
, bp
->pc
,
885 /* Something went wrong, relink the breakpoint. */
886 *bp_link
= prev_bp_link
;
888 threads_debug_printf ("Failed to uninsert raw breakpoint "
889 "at 0x%s while deleting it.",
907 warning ("Could not find raw breakpoint in list.");
912 release_breakpoint (struct process_info
*proc
, struct breakpoint
*bp
)
917 newrefcount
= bp
->raw
->refcount
- 1;
918 if (newrefcount
== 0)
920 ret
= delete_raw_breakpoint (proc
, bp
->raw
);
925 bp
->raw
->refcount
= newrefcount
;
933 delete_breakpoint_1 (struct process_info
*proc
, struct breakpoint
*todel
)
935 struct breakpoint
*bp
, **bp_link
;
938 bp
= proc
->breakpoints
;
939 bp_link
= &proc
->breakpoints
;
947 err
= release_breakpoint (proc
, bp
);
961 warning ("Could not find breakpoint in list.");
966 delete_breakpoint (struct breakpoint
*todel
)
968 struct process_info
*proc
= current_process ();
969 return delete_breakpoint_1 (proc
, todel
);
972 /* Locate a GDB breakpoint of type Z_TYPE and kind KIND placed at
973 address ADDR and return a pointer to its structure. If KIND is -1,
974 the breakpoint's kind is ignored. */
976 static struct gdb_breakpoint
*
977 find_gdb_breakpoint (char z_type
, CORE_ADDR addr
, int kind
)
979 struct process_info
*proc
= current_process ();
980 struct breakpoint
*bp
;
981 enum bkpt_type type
= Z_packet_to_bkpt_type (z_type
);
983 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
984 if (bp
->type
== type
&& bp
->raw
->pc
== addr
985 && (kind
== -1 || bp
->raw
->kind
== kind
))
986 return (struct gdb_breakpoint
*) bp
;
992 z_type_supported (char z_type
)
994 return (z_type
>= '0' && z_type
<= '4'
995 && the_target
->supports_z_point_type (z_type
));
998 /* Create a new GDB breakpoint of type Z_TYPE at ADDR with kind KIND.
999 Returns a pointer to the newly created breakpoint on success. On
1000 failure returns NULL and sets *ERR to either -1 for error, or 1 if
1001 Z_TYPE breakpoints are not supported on this target. */
1003 struct gdb_breakpoint
*
1004 set_gdb_breakpoint (char z_type
, CORE_ADDR addr
, int kind
, int *err
)
1006 struct gdb_breakpoint
*bp
;
1007 enum bkpt_type type
;
1008 enum raw_bkpt_type raw_type
;
1010 if (!z_type_supported (z_type
))
1016 /* If we see GDB inserting a second code breakpoint at the same
1017 address, then either: GDB is updating the breakpoint's conditions
1018 or commands; or, the first breakpoint must have disappeared due
1019 to a shared library unload. On targets where the shared
1020 libraries are handled by userspace, like SVR4, for example,
1021 GDBserver can't tell if a library was loaded or unloaded. Since
1022 we refcount raw breakpoints, we must be careful to make sure GDB
1023 breakpoints never contribute more than one reference. if we
1024 didn't do this, in case the previous breakpoint is gone due to a
1025 shared library unload, we'd just increase the refcount of the
1026 previous breakpoint at this address, but the trap was not planted
1027 in the inferior anymore, thus the breakpoint would never be hit.
1028 Note this must be careful to not create a window where
1029 breakpoints are removed from the target, for non-stop, in case
1030 the target can poke at memory while the program is running. */
1031 if (z_type
== Z_PACKET_SW_BP
1032 || z_type
== Z_PACKET_HW_BP
)
1034 bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1038 if (bp
->base
.raw
->kind
!= kind
)
1040 /* A different kind than previously seen. The previous
1041 breakpoint must be gone then. */
1042 bp
->base
.raw
->inserted
= -1;
1043 delete_breakpoint ((struct breakpoint
*) bp
);
1046 else if (z_type
== Z_PACKET_SW_BP
)
1048 /* Check if the breakpoint is actually gone from the
1049 target, due to an solib unload, for example. Might
1050 as well validate _all_ breakpoints. */
1051 validate_breakpoints ();
1053 /* Breakpoints that don't pass validation are
1055 bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1061 /* Data breakpoints for the same address but different kind are
1062 expected. GDB doesn't merge these. The backend gets to do
1063 that if it wants/can. */
1064 bp
= find_gdb_breakpoint (z_type
, addr
, kind
);
1069 /* We already know about this breakpoint, there's nothing else
1070 to do - GDB's reference is already accounted for. Note that
1071 whether the breakpoint inserted is left as is - we may be
1072 stepping over it, for example, in which case we don't want to
1073 force-reinsert it. */
1077 raw_type
= Z_packet_to_raw_bkpt_type (z_type
);
1078 type
= Z_packet_to_bkpt_type (z_type
);
1079 return (struct gdb_breakpoint
*) set_breakpoint (type
, raw_type
, addr
,
1083 /* Delete a GDB breakpoint of type Z_TYPE and kind KIND previously
1084 inserted at ADDR with set_gdb_breakpoint_at. Returns 0 on success,
1085 -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
1089 delete_gdb_breakpoint (char z_type
, CORE_ADDR addr
, int kind
)
1091 if (!z_type_supported (z_type
))
1094 gdb_breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, kind
);
1098 /* Before deleting the breakpoint, make sure to free its condition
1099 and command lists. */
1100 clear_breakpoint_conditions_and_commands (bp
);
1101 int err
= delete_breakpoint ((struct breakpoint
*) bp
);
1108 /* Clear all conditions associated with a breakpoint. */
1111 clear_breakpoint_conditions (struct gdb_breakpoint
*bp
)
1113 struct point_cond_list
*cond
;
1115 if (bp
->cond_list
== NULL
)
1118 cond
= bp
->cond_list
;
1120 while (cond
!= NULL
)
1122 struct point_cond_list
*cond_next
;
1124 cond_next
= cond
->next
;
1125 gdb_free_agent_expr (cond
->cond
);
1130 bp
->cond_list
= NULL
;
1133 /* Clear all commands associated with a breakpoint. */
1136 clear_breakpoint_commands (struct gdb_breakpoint
*bp
)
1138 struct point_command_list
*cmd
;
1140 if (bp
->command_list
== NULL
)
1143 cmd
= bp
->command_list
;
1147 struct point_command_list
*cmd_next
;
1149 cmd_next
= cmd
->next
;
1150 gdb_free_agent_expr (cmd
->cmd
);
1155 bp
->command_list
= NULL
;
1159 clear_breakpoint_conditions_and_commands (struct gdb_breakpoint
*bp
)
1161 clear_breakpoint_conditions (bp
);
1162 clear_breakpoint_commands (bp
);
1165 /* Add condition CONDITION to GDBserver's breakpoint BP. */
1168 add_condition_to_breakpoint (struct gdb_breakpoint
*bp
,
1169 struct agent_expr
*condition
)
1171 struct point_cond_list
*new_cond
;
1173 /* Create new condition. */
1174 new_cond
= XCNEW (struct point_cond_list
);
1175 new_cond
->cond
= condition
;
1177 /* Add condition to the list. */
1178 new_cond
->next
= bp
->cond_list
;
1179 bp
->cond_list
= new_cond
;
1182 /* Add a target-side condition CONDITION to a breakpoint. */
1185 add_breakpoint_condition (struct gdb_breakpoint
*bp
, const char **condition
)
1187 const char *actparm
= *condition
;
1188 struct agent_expr
*cond
;
1190 if (condition
== NULL
)
1196 cond
= gdb_parse_agent_expr (&actparm
);
1200 warning ("Condition evaluation failed. Assuming unconditional.");
1204 add_condition_to_breakpoint (bp
, cond
);
1206 *condition
= actparm
;
1211 /* Evaluate condition (if any) at breakpoint BP. Return 1 if
1212 true and 0 otherwise. */
1215 gdb_condition_true_at_breakpoint_z_type (char z_type
, CORE_ADDR addr
)
1217 /* Fetch registers for the current inferior. */
1218 struct gdb_breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1220 struct point_cond_list
*cl
;
1222 struct eval_agent_expr_context ctx
;
1227 /* Check if the breakpoint is unconditional. If it is,
1228 the condition always evaluates to TRUE. */
1229 if (bp
->cond_list
== NULL
)
1232 ctx
.regcache
= get_thread_regcache (current_thread
, 1);
1236 /* Evaluate each condition in the breakpoint's list of conditions.
1237 Return true if any of the conditions evaluates to TRUE.
1239 If we failed to evaluate the expression, TRUE is returned. This
1240 forces GDB to reevaluate the conditions. */
1241 for (cl
= bp
->cond_list
;
1242 cl
&& !value
&& !err
; cl
= cl
->next
)
1244 /* Evaluate the condition. */
1245 err
= gdb_eval_agent_expr (&ctx
, cl
->cond
, &value
);
1251 return (value
!= 0);
1255 gdb_condition_true_at_breakpoint (CORE_ADDR where
)
1257 /* Only check code (software or hardware) breakpoints. */
1258 return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP
, where
)
1259 || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP
, where
));
1262 /* Add commands COMMANDS to GDBserver's breakpoint BP. */
1265 add_commands_to_breakpoint (struct gdb_breakpoint
*bp
,
1266 struct agent_expr
*commands
, int persist
)
1268 struct point_command_list
*new_cmd
;
1270 /* Create new command. */
1271 new_cmd
= XCNEW (struct point_command_list
);
1272 new_cmd
->cmd
= commands
;
1273 new_cmd
->persistence
= persist
;
1275 /* Add commands to the list. */
1276 new_cmd
->next
= bp
->command_list
;
1277 bp
->command_list
= new_cmd
;
1280 /* Add a target-side command COMMAND to the breakpoint at ADDR. */
1283 add_breakpoint_commands (struct gdb_breakpoint
*bp
, const char **command
,
1286 const char *actparm
= *command
;
1287 struct agent_expr
*cmd
;
1289 if (command
== NULL
)
1295 cmd
= gdb_parse_agent_expr (&actparm
);
1299 warning ("Command evaluation failed. Disabling.");
1303 add_commands_to_breakpoint (bp
, cmd
, persist
);
1310 /* Return true if there are no commands to run at this location,
1311 which likely means we want to report back to GDB. */
1314 gdb_no_commands_at_breakpoint_z_type (char z_type
, CORE_ADDR addr
)
1316 struct gdb_breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1321 threads_debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s",
1322 paddress (addr
), z_type
,
1323 phex_nz ((uintptr_t) bp
->command_list
, 0));
1324 return (bp
->command_list
== NULL
);
1327 /* Return true if there are no commands to run at this location,
1328 which likely means we want to report back to GDB. */
1331 gdb_no_commands_at_breakpoint (CORE_ADDR where
)
1333 /* Only check code (software or hardware) breakpoints. */
1334 return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP
, where
)
1335 && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP
, where
));
1338 /* Run a breakpoint's commands. Returns 0 if there was a problem
1339 running any command, 1 otherwise. */
1342 run_breakpoint_commands_z_type (char z_type
, CORE_ADDR addr
)
1344 /* Fetch registers for the current inferior. */
1345 struct gdb_breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1347 struct point_command_list
*cl
;
1349 struct eval_agent_expr_context ctx
;
1354 ctx
.regcache
= get_thread_regcache (current_thread
, 1);
1358 for (cl
= bp
->command_list
;
1359 cl
&& !value
&& !err
; cl
= cl
->next
)
1361 /* Run the command. */
1362 err
= gdb_eval_agent_expr (&ctx
, cl
->cmd
, &value
);
1364 /* If one command has a problem, stop digging the hole deeper. */
1373 run_breakpoint_commands (CORE_ADDR where
)
1375 /* Only check code (software or hardware) breakpoints. If one
1376 command has a problem, stop digging the hole deeper. */
1377 if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP
, where
))
1378 run_breakpoint_commands_z_type (Z_PACKET_HW_BP
, where
);
1381 /* See mem-break.h. */
1384 gdb_breakpoint_here (CORE_ADDR where
)
1386 /* Only check code (software or hardware) breakpoints. */
1387 return (find_gdb_breakpoint (Z_PACKET_SW_BP
, where
, -1) != NULL
1388 || find_gdb_breakpoint (Z_PACKET_HW_BP
, where
, -1) != NULL
);
1392 set_single_step_breakpoint (CORE_ADDR stop_at
, ptid_t ptid
)
1394 struct single_step_breakpoint
*bp
;
1396 gdb_assert (current_ptid
.pid () == ptid
.pid ());
1398 bp
= (struct single_step_breakpoint
*) set_breakpoint_type_at (single_step_breakpoint
,
1404 delete_single_step_breakpoints (struct thread_info
*thread
)
1406 struct process_info
*proc
= get_thread_process (thread
);
1407 struct breakpoint
*bp
, **bp_link
;
1409 bp
= proc
->breakpoints
;
1410 bp_link
= &proc
->breakpoints
;
1414 if (bp
->type
== single_step_breakpoint
1415 && ((struct single_step_breakpoint
*) bp
)->ptid
== ptid_of (thread
))
1417 scoped_restore_current_thread restore_thread
;
1419 switch_to_thread (thread
);
1420 *bp_link
= bp
->next
;
1421 release_breakpoint (proc
, bp
);
1426 bp_link
= &bp
->next
;
1433 uninsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1435 if (bp
->inserted
< 0)
1437 threads_debug_printf ("Breakpoint at %s is marked insert-disabled.",
1440 else if (bp
->inserted
> 0)
1446 err
= the_target
->remove_point (bp
->raw_type
, bp
->pc
, bp
->kind
, bp
);
1451 threads_debug_printf ("Failed to uninsert raw breakpoint at 0x%s.",
1458 uninsert_breakpoints_at (CORE_ADDR pc
)
1460 struct process_info
*proc
= current_process ();
1461 struct raw_breakpoint
*bp
;
1464 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1465 if ((bp
->raw_type
== raw_bkpt_type_sw
1466 || bp
->raw_type
== raw_bkpt_type_hw
)
1472 uninsert_raw_breakpoint (bp
);
1477 /* This can happen when we remove all breakpoints while handling
1479 threads_debug_printf ("Could not find breakpoint at 0x%s "
1480 "in list (uninserting).",
1486 uninsert_all_breakpoints (void)
1488 struct process_info
*proc
= current_process ();
1489 struct raw_breakpoint
*bp
;
1491 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1492 if ((bp
->raw_type
== raw_bkpt_type_sw
1493 || bp
->raw_type
== raw_bkpt_type_hw
)
1495 uninsert_raw_breakpoint (bp
);
1499 uninsert_single_step_breakpoints (struct thread_info
*thread
)
1501 struct process_info
*proc
= get_thread_process (thread
);
1502 struct breakpoint
*bp
;
1504 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1506 if (bp
->type
== single_step_breakpoint
1507 && ((struct single_step_breakpoint
*) bp
)->ptid
== ptid_of (thread
))
1509 gdb_assert (bp
->raw
->inserted
> 0);
1511 /* Only uninsert the raw breakpoint if it only belongs to a
1512 reinsert breakpoint. */
1513 if (bp
->raw
->refcount
== 1)
1515 scoped_restore_current_thread restore_thread
;
1517 switch_to_thread (thread
);
1518 uninsert_raw_breakpoint (bp
->raw
);
1525 reinsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1532 err
= the_target
->insert_point (bp
->raw_type
, bp
->pc
, bp
->kind
, bp
);
1536 threads_debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).",
1537 paddress (bp
->pc
), err
);
1541 reinsert_breakpoints_at (CORE_ADDR pc
)
1543 struct process_info
*proc
= current_process ();
1544 struct raw_breakpoint
*bp
;
1547 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1548 if ((bp
->raw_type
== raw_bkpt_type_sw
1549 || bp
->raw_type
== raw_bkpt_type_hw
)
1554 reinsert_raw_breakpoint (bp
);
1559 /* This can happen when we remove all breakpoints while handling
1561 threads_debug_printf ("Could not find raw breakpoint at 0x%s "
1562 "in list (reinserting).",
1568 has_single_step_breakpoints (struct thread_info
*thread
)
1570 struct process_info
*proc
= get_thread_process (thread
);
1571 struct breakpoint
*bp
, **bp_link
;
1573 bp
= proc
->breakpoints
;
1574 bp_link
= &proc
->breakpoints
;
1578 if (bp
->type
== single_step_breakpoint
1579 && ((struct single_step_breakpoint
*) bp
)->ptid
== ptid_of (thread
))
1583 bp_link
= &bp
->next
;
1592 reinsert_all_breakpoints (void)
1594 struct process_info
*proc
= current_process ();
1595 struct raw_breakpoint
*bp
;
1597 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1598 if ((bp
->raw_type
== raw_bkpt_type_sw
1599 || bp
->raw_type
== raw_bkpt_type_hw
)
1601 reinsert_raw_breakpoint (bp
);
1605 reinsert_single_step_breakpoints (struct thread_info
*thread
)
1607 struct process_info
*proc
= get_thread_process (thread
);
1608 struct breakpoint
*bp
;
1610 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1612 if (bp
->type
== single_step_breakpoint
1613 && ((struct single_step_breakpoint
*) bp
)->ptid
== ptid_of (thread
))
1615 gdb_assert (bp
->raw
->inserted
> 0);
1617 if (bp
->raw
->refcount
== 1)
1619 scoped_restore_current_thread restore_thread
;
1621 switch_to_thread (thread
);
1622 reinsert_raw_breakpoint (bp
->raw
);
1629 check_breakpoints (CORE_ADDR stop_pc
)
1631 struct process_info
*proc
= current_process ();
1632 struct breakpoint
*bp
, **bp_link
;
1634 bp
= proc
->breakpoints
;
1635 bp_link
= &proc
->breakpoints
;
1639 struct raw_breakpoint
*raw
= bp
->raw
;
1641 if ((raw
->raw_type
== raw_bkpt_type_sw
1642 || raw
->raw_type
== raw_bkpt_type_hw
)
1643 && raw
->pc
== stop_pc
)
1647 warning ("Hit a removed breakpoint?");
1651 if (bp
->type
== other_breakpoint
)
1653 struct other_breakpoint
*other_bp
1654 = (struct other_breakpoint
*) bp
;
1656 if (other_bp
->handler
!= NULL
&& (*other_bp
->handler
) (stop_pc
))
1658 *bp_link
= bp
->next
;
1660 release_breakpoint (proc
, bp
);
1668 bp_link
= &bp
->next
;
1674 breakpoint_here (CORE_ADDR addr
)
1676 struct process_info
*proc
= current_process ();
1677 struct raw_breakpoint
*bp
;
1679 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1680 if ((bp
->raw_type
== raw_bkpt_type_sw
1681 || bp
->raw_type
== raw_bkpt_type_hw
)
1689 breakpoint_inserted_here (CORE_ADDR addr
)
1691 struct process_info
*proc
= current_process ();
1692 struct raw_breakpoint
*bp
;
1694 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1695 if ((bp
->raw_type
== raw_bkpt_type_sw
1696 || bp
->raw_type
== raw_bkpt_type_hw
)
1704 /* See mem-break.h. */
1707 software_breakpoint_inserted_here (CORE_ADDR addr
)
1709 struct process_info
*proc
= current_process ();
1710 struct raw_breakpoint
*bp
;
1712 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1713 if (bp
->raw_type
== raw_bkpt_type_sw
1721 /* See mem-break.h. */
1724 hardware_breakpoint_inserted_here (CORE_ADDR addr
)
1726 struct process_info
*proc
= current_process ();
1727 struct raw_breakpoint
*bp
;
1729 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1730 if (bp
->raw_type
== raw_bkpt_type_hw
1738 /* See mem-break.h. */
1741 single_step_breakpoint_inserted_here (CORE_ADDR addr
)
1743 struct process_info
*proc
= current_process ();
1744 struct breakpoint
*bp
;
1746 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1747 if (bp
->type
== single_step_breakpoint
1748 && bp
->raw
->pc
== addr
1749 && bp
->raw
->inserted
)
1756 validate_inserted_breakpoint (struct raw_breakpoint
*bp
)
1761 gdb_assert (bp
->inserted
);
1762 gdb_assert (bp
->raw_type
== raw_bkpt_type_sw
);
1764 buf
= (unsigned char *) alloca (bp_size (bp
));
1765 err
= the_target
->read_memory (bp
->pc
, buf
, bp_size (bp
));
1766 if (err
|| memcmp (buf
, bp_opcode (bp
), bp_size (bp
)) != 0)
1768 /* Tag it as gone. */
1777 delete_disabled_breakpoints (void)
1779 struct process_info
*proc
= current_process ();
1780 struct breakpoint
*bp
, *next
;
1782 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= next
)
1785 if (bp
->raw
->inserted
< 0)
1787 /* If single_step_breakpoints become disabled, that means the
1788 manipulations (insertion and removal) of them are wrong. */
1789 gdb_assert (bp
->type
!= single_step_breakpoint
);
1790 delete_breakpoint_1 (proc
, bp
);
1795 /* Check if breakpoints we inserted still appear to be inserted. They
1796 may disappear due to a shared library unload, and worse, a new
1797 shared library may be reloaded at the same address as the
1798 previously unloaded one. If that happens, we should make sure that
1799 the shadow memory of the old breakpoints isn't used when reading or
1803 validate_breakpoints (void)
1805 struct process_info
*proc
= current_process ();
1806 struct breakpoint
*bp
;
1808 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1810 struct raw_breakpoint
*raw
= bp
->raw
;
1812 if (raw
->raw_type
== raw_bkpt_type_sw
&& raw
->inserted
> 0)
1813 validate_inserted_breakpoint (raw
);
1816 delete_disabled_breakpoints ();
1820 check_mem_read (CORE_ADDR mem_addr
, unsigned char *buf
, int mem_len
)
1822 struct process_info
*proc
= current_process ();
1823 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
1824 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
1825 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
1826 int disabled_one
= 0;
1828 for (; jp
!= NULL
; jp
= jp
->next
)
1830 CORE_ADDR bp_end
= jp
->pc
+ jp
->length
;
1831 CORE_ADDR start
, end
;
1832 int copy_offset
, copy_len
, buf_offset
;
1834 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= buf
+ mem_len
1835 || buf
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
1837 if (mem_addr
>= bp_end
)
1839 if (jp
->pc
>= mem_end
)
1843 if (mem_addr
> start
)
1850 copy_len
= end
- start
;
1851 copy_offset
= start
- jp
->pc
;
1852 buf_offset
= start
- mem_addr
;
1855 memcpy (buf
+ buf_offset
,
1856 fast_tracepoint_jump_shadow (jp
) + copy_offset
,
1860 for (; bp
!= NULL
; bp
= bp
->next
)
1862 CORE_ADDR bp_end
= bp
->pc
+ bp_size (bp
);
1863 CORE_ADDR start
, end
;
1864 int copy_offset
, copy_len
, buf_offset
;
1866 if (bp
->raw_type
!= raw_bkpt_type_sw
)
1869 gdb_assert (bp
->old_data
>= buf
+ mem_len
1870 || buf
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
1872 if (mem_addr
>= bp_end
)
1874 if (bp
->pc
>= mem_end
)
1878 if (mem_addr
> start
)
1885 copy_len
= end
- start
;
1886 copy_offset
= start
- bp
->pc
;
1887 buf_offset
= start
- mem_addr
;
1889 if (bp
->inserted
> 0)
1891 if (validate_inserted_breakpoint (bp
))
1892 memcpy (buf
+ buf_offset
, bp
->old_data
+ copy_offset
, copy_len
);
1899 delete_disabled_breakpoints ();
1903 check_mem_write (CORE_ADDR mem_addr
, unsigned char *buf
,
1904 const unsigned char *myaddr
, int mem_len
)
1906 struct process_info
*proc
= current_process ();
1907 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
1908 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
1909 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
1910 int disabled_one
= 0;
1912 /* First fast tracepoint jumps, then breakpoint traps on top. */
1914 for (; jp
!= NULL
; jp
= jp
->next
)
1916 CORE_ADDR jp_end
= jp
->pc
+ jp
->length
;
1917 CORE_ADDR start
, end
;
1918 int copy_offset
, copy_len
, buf_offset
;
1920 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= myaddr
+ mem_len
1921 || myaddr
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
1922 gdb_assert (fast_tracepoint_jump_insn (jp
) >= buf
+ mem_len
1923 || buf
>= fast_tracepoint_jump_insn (jp
) + (jp
)->length
);
1925 if (mem_addr
>= jp_end
)
1927 if (jp
->pc
>= mem_end
)
1931 if (mem_addr
> start
)
1938 copy_len
= end
- start
;
1939 copy_offset
= start
- jp
->pc
;
1940 buf_offset
= start
- mem_addr
;
1942 memcpy (fast_tracepoint_jump_shadow (jp
) + copy_offset
,
1943 myaddr
+ buf_offset
, copy_len
);
1945 memcpy (buf
+ buf_offset
,
1946 fast_tracepoint_jump_insn (jp
) + copy_offset
, copy_len
);
1949 for (; bp
!= NULL
; bp
= bp
->next
)
1951 CORE_ADDR bp_end
= bp
->pc
+ bp_size (bp
);
1952 CORE_ADDR start
, end
;
1953 int copy_offset
, copy_len
, buf_offset
;
1955 if (bp
->raw_type
!= raw_bkpt_type_sw
)
1958 gdb_assert (bp
->old_data
>= myaddr
+ mem_len
1959 || myaddr
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
1961 if (mem_addr
>= bp_end
)
1963 if (bp
->pc
>= mem_end
)
1967 if (mem_addr
> start
)
1974 copy_len
= end
- start
;
1975 copy_offset
= start
- bp
->pc
;
1976 buf_offset
= start
- mem_addr
;
1978 memcpy (bp
->old_data
+ copy_offset
, myaddr
+ buf_offset
, copy_len
);
1979 if (bp
->inserted
> 0)
1981 if (validate_inserted_breakpoint (bp
))
1982 memcpy (buf
+ buf_offset
, bp_opcode (bp
) + copy_offset
, copy_len
);
1989 delete_disabled_breakpoints ();
1992 /* Delete all breakpoints, and un-insert them from the inferior. */
1995 delete_all_breakpoints (void)
1997 struct process_info
*proc
= current_process ();
1999 while (proc
->breakpoints
)
2000 delete_breakpoint_1 (proc
, proc
->breakpoints
);
2003 /* Clear the "inserted" flag in all breakpoints. */
2006 mark_breakpoints_out (struct process_info
*proc
)
2008 struct raw_breakpoint
*raw_bp
;
2010 for (raw_bp
= proc
->raw_breakpoints
; raw_bp
!= NULL
; raw_bp
= raw_bp
->next
)
2011 raw_bp
->inserted
= 0;
2014 /* Release all breakpoints, but do not try to un-insert them from the
2018 free_all_breakpoints (struct process_info
*proc
)
2020 mark_breakpoints_out (proc
);
2022 /* Note: use PROC explicitly instead of deferring to
2023 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
2024 released when we get here. There should be no call to
2025 current_process from here on. */
2026 while (proc
->breakpoints
)
2027 delete_breakpoint_1 (proc
, proc
->breakpoints
);
2030 /* Clone an agent expression. */
2032 static struct agent_expr
*
2033 clone_agent_expr (const struct agent_expr
*src_ax
)
2035 struct agent_expr
*ax
;
2037 ax
= XCNEW (struct agent_expr
);
2038 ax
->length
= src_ax
->length
;
2039 ax
->bytes
= (unsigned char *) xcalloc (ax
->length
, 1);
2040 memcpy (ax
->bytes
, src_ax
->bytes
, ax
->length
);
2044 /* Deep-copy the contents of one breakpoint to another. */
2046 static struct breakpoint
*
2047 clone_one_breakpoint (const struct breakpoint
*src
, ptid_t ptid
)
2049 struct breakpoint
*dest
;
2050 struct raw_breakpoint
*dest_raw
;
2052 /* Clone the raw breakpoint. */
2053 dest_raw
= XCNEW (struct raw_breakpoint
);
2054 dest_raw
->raw_type
= src
->raw
->raw_type
;
2055 dest_raw
->refcount
= src
->raw
->refcount
;
2056 dest_raw
->pc
= src
->raw
->pc
;
2057 dest_raw
->kind
= src
->raw
->kind
;
2058 memcpy (dest_raw
->old_data
, src
->raw
->old_data
, MAX_BREAKPOINT_LEN
);
2059 dest_raw
->inserted
= src
->raw
->inserted
;
2061 /* Clone the high-level breakpoint. */
2062 if (is_gdb_breakpoint (src
->type
))
2064 struct gdb_breakpoint
*gdb_dest
= XCNEW (struct gdb_breakpoint
);
2065 struct point_cond_list
*current_cond
;
2066 struct point_cond_list
*new_cond
;
2067 struct point_cond_list
*cond_tail
= NULL
;
2068 struct point_command_list
*current_cmd
;
2069 struct point_command_list
*new_cmd
;
2070 struct point_command_list
*cmd_tail
= NULL
;
2072 /* Clone the condition list. */
2073 for (current_cond
= ((struct gdb_breakpoint
*) src
)->cond_list
;
2074 current_cond
!= NULL
;
2075 current_cond
= current_cond
->next
)
2077 new_cond
= XCNEW (struct point_cond_list
);
2078 new_cond
->cond
= clone_agent_expr (current_cond
->cond
);
2079 APPEND_TO_LIST (&gdb_dest
->cond_list
, new_cond
, cond_tail
);
2082 /* Clone the command list. */
2083 for (current_cmd
= ((struct gdb_breakpoint
*) src
)->command_list
;
2084 current_cmd
!= NULL
;
2085 current_cmd
= current_cmd
->next
)
2087 new_cmd
= XCNEW (struct point_command_list
);
2088 new_cmd
->cmd
= clone_agent_expr (current_cmd
->cmd
);
2089 new_cmd
->persistence
= current_cmd
->persistence
;
2090 APPEND_TO_LIST (&gdb_dest
->command_list
, new_cmd
, cmd_tail
);
2093 dest
= (struct breakpoint
*) gdb_dest
;
2095 else if (src
->type
== other_breakpoint
)
2097 struct other_breakpoint
*other_dest
= XCNEW (struct other_breakpoint
);
2099 other_dest
->handler
= ((struct other_breakpoint
*) src
)->handler
;
2100 dest
= (struct breakpoint
*) other_dest
;
2102 else if (src
->type
== single_step_breakpoint
)
2104 struct single_step_breakpoint
*ss_dest
2105 = XCNEW (struct single_step_breakpoint
);
2107 dest
= (struct breakpoint
*) ss_dest
;
2108 /* Since single-step breakpoint is thread specific, don't copy
2109 thread id from SRC, use ID instead. */
2110 ss_dest
->ptid
= ptid
;
2113 gdb_assert_not_reached ("unhandled breakpoint type");
2115 dest
->type
= src
->type
;
2116 dest
->raw
= dest_raw
;
2121 /* See mem-break.h. */
2124 clone_all_breakpoints (struct thread_info
*child_thread
,
2125 const struct thread_info
*parent_thread
)
2127 const struct breakpoint
*bp
;
2128 struct breakpoint
*new_bkpt
;
2129 struct breakpoint
*bkpt_tail
= NULL
;
2130 struct raw_breakpoint
*raw_bkpt_tail
= NULL
;
2131 struct process_info
*child_proc
= get_thread_process (child_thread
);
2132 struct process_info
*parent_proc
= get_thread_process (parent_thread
);
2133 struct breakpoint
**new_list
= &child_proc
->breakpoints
;
2134 struct raw_breakpoint
**new_raw_list
= &child_proc
->raw_breakpoints
;
2136 for (bp
= parent_proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
2138 new_bkpt
= clone_one_breakpoint (bp
, ptid_of (child_thread
));
2139 APPEND_TO_LIST (new_list
, new_bkpt
, bkpt_tail
);
2140 APPEND_TO_LIST (new_raw_list
, new_bkpt
->raw
, raw_bkpt_tail
);