1 /* Memory breakpoint operations for the remote server for GDB.
2 Copyright (C) 2002-2014 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/>. */
26 const unsigned char *breakpoint_data
;
29 #define MAX_BREAKPOINT_LEN 8
31 /* GDB will never try to install multiple breakpoints at the same
32 address. However, we can see GDB requesting to insert a breakpoint
33 at an address is had already inserted one previously in a few
36 - The RSP documentation on Z packets says that to avoid potential
37 problems with duplicate packets, the operations should be
38 implemented in an idempotent way.
40 - A breakpoint is set at ADDR, an address in a shared library.
41 Then the shared library is unloaded. And then another, unrelated,
42 breakpoint at ADDR is set. There is not breakpoint removal request
43 between the first and the second breakpoint.
45 - When GDB wants to update the target-side breakpoint conditions or
46 commands, it re-inserts the breakpoint, with updated
47 conditions/commands associated.
49 Also, we need to keep track of internal breakpoints too, so we do
50 need to be able to install multiple breakpoints at the same address
53 We keep track of two different, and closely related structures. A
54 raw breakpoint, which manages the low level, close to the metal
55 aspect of a breakpoint. It holds the breakpoint address, and for
56 software breakpoints, a buffer holding a copy of the instructions
57 that would be in memory had not been a breakpoint there (we call
58 that the shadow memory of the breakpoint). We occasionally need to
59 temporarilly uninsert a breakpoint without the client knowing about
60 it (e.g., to step over an internal breakpoint), so we keep an
61 `inserted' state associated with this low level breakpoint
62 structure. There can only be one such object for a given address.
63 Then, we have (a bit higher level) breakpoints. This structure
64 holds a callback to be called whenever a breakpoint is hit, a
65 high-level type, and a link to a low level raw breakpoint. There
66 can be many high-level breakpoints at the same address, and all of
67 them will point to the same raw breakpoint, which is reference
70 /* The low level, physical, raw breakpoint. */
73 struct raw_breakpoint
*next
;
75 /* The low level type of the breakpoint (software breakpoint,
77 enum raw_bkpt_type raw_type
;
79 /* A reference count. Each high level breakpoint referencing this
80 raw breakpoint accounts for one reference. */
83 /* The breakpoint's insertion address. There can only be one raw
84 breakpoint for a given PC. */
87 /* The breakpoint's size. */
90 /* The breakpoint's shadow memory. */
91 unsigned char old_data
[MAX_BREAKPOINT_LEN
];
93 /* Positive if this breakpoint is currently inserted in the
94 inferior. Negative if it was, but we've detected that it's now
95 gone. Zero if not inserted. */
99 /* The type of a breakpoint. */
102 /* A GDB breakpoint, requested with a Z0 packet. */
105 /* A GDB hardware breakpoint, requested with a Z1 packet. */
108 /* A GDB write watchpoint, requested with a Z2 packet. */
111 /* A GDB read watchpoint, requested with a Z3 packet. */
114 /* A GDB access watchpoint, requested with a Z4 packet. */
117 /* A basic-software-single-step breakpoint. */
120 /* Any other breakpoint type that doesn't require specific
121 treatment goes here. E.g., an event breakpoint. */
125 struct point_cond_list
127 /* Pointer to the agent expression that is the breakpoint's
129 struct agent_expr
*cond
;
131 /* Pointer to the next condition. */
132 struct point_cond_list
*next
;
135 struct point_command_list
137 /* Pointer to the agent expression that is the breakpoint's
139 struct agent_expr
*cmd
;
141 /* Flag that is true if this command should run even while GDB is
145 /* Pointer to the next command. */
146 struct point_command_list
*next
;
149 /* A high level (in gdbserver's perspective) breakpoint. */
152 struct breakpoint
*next
;
154 /* The breakpoint's type. */
157 /* Pointer to the condition list that should be evaluated on
158 the target or NULL if the breakpoint is unconditional or
159 if GDB doesn't want us to evaluate the conditionals on the
161 struct point_cond_list
*cond_list
;
163 /* Point to the list of commands to run when this is hit. */
164 struct point_command_list
*command_list
;
166 /* Link to this breakpoint's raw breakpoint. This is always
168 struct raw_breakpoint
*raw
;
170 /* Function to call when we hit this breakpoint. If it returns 1,
171 the breakpoint shall be deleted; 0 or if this callback is NULL,
172 it will be left inserted. */
173 int (*handler
) (CORE_ADDR
);
176 /* See mem-break.h. */
178 enum target_hw_bp_type
179 raw_bkpt_type_to_target_hw_bp_type (enum raw_bkpt_type raw_type
)
183 case raw_bkpt_type_hw
:
185 case raw_bkpt_type_write_wp
:
187 case raw_bkpt_type_read_wp
:
189 case raw_bkpt_type_access_wp
:
192 internal_error (__FILE__
, __LINE__
,
193 "bad raw breakpoint type %d", (int) raw_type
);
197 /* See mem-break.h. */
199 static enum bkpt_type
200 Z_packet_to_bkpt_type (char z_type
)
202 gdb_assert ('0' <= z_type
&& z_type
<= '4');
204 return gdb_breakpoint_Z0
+ (z_type
- '0');
207 /* See mem-break.h. */
210 Z_packet_to_raw_bkpt_type (char z_type
)
215 return raw_bkpt_type_sw
;
217 return raw_bkpt_type_hw
;
218 case Z_PACKET_WRITE_WP
:
219 return raw_bkpt_type_write_wp
;
220 case Z_PACKET_READ_WP
:
221 return raw_bkpt_type_read_wp
;
222 case Z_PACKET_ACCESS_WP
:
223 return raw_bkpt_type_access_wp
;
225 gdb_assert_not_reached ("unhandled Z packet type.");
230 any_persistent_commands ()
232 struct process_info
*proc
= current_process ();
233 struct breakpoint
*bp
;
234 struct point_command_list
*cl
;
236 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
238 for (cl
= bp
->command_list
; cl
!= NULL
; cl
= cl
->next
)
246 /* Find low-level breakpoint of type TYPE at address ADDR that is not
247 insert-disabled. Returns NULL if not found. */
249 static struct raw_breakpoint
*
250 find_enabled_raw_code_breakpoint_at (CORE_ADDR addr
, enum raw_bkpt_type type
)
252 struct process_info
*proc
= current_process ();
253 struct raw_breakpoint
*bp
;
255 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
257 && bp
->raw_type
== type
258 && bp
->inserted
>= 0)
264 /* Find low-level breakpoint of type TYPE at address ADDR. Returns
265 NULL if not found. */
267 static struct raw_breakpoint
*
268 find_raw_breakpoint_at (CORE_ADDR addr
, enum raw_bkpt_type type
, int size
)
270 struct process_info
*proc
= current_process ();
271 struct raw_breakpoint
*bp
;
273 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
274 if (bp
->pc
== addr
&& bp
->raw_type
== type
&& bp
->size
== size
)
280 /* See mem-break.h. */
283 insert_memory_breakpoint (struct raw_breakpoint
*bp
)
285 unsigned char buf
[MAX_BREAKPOINT_LEN
];
288 if (breakpoint_data
== NULL
)
291 /* If the architecture treats the size field of Z packets as a
292 'kind' field, then we'll need to be able to know which is the
293 breakpoint instruction too. */
294 if (bp
->size
!= breakpoint_len
)
297 debug_printf ("Don't know how to insert breakpoints of size %d.\n",
302 /* Note that there can be fast tracepoint jumps installed in the
303 same memory range, so to get at the original memory, we need to
304 use read_inferior_memory, which masks those out. */
305 err
= read_inferior_memory (bp
->pc
, buf
, breakpoint_len
);
309 debug_printf ("Failed to read shadow memory of"
310 " breakpoint at 0x%s (%s).\n",
311 paddress (bp
->pc
), strerror (err
));
315 memcpy (bp
->old_data
, buf
, breakpoint_len
);
317 err
= (*the_target
->write_memory
) (bp
->pc
, breakpoint_data
,
322 debug_printf ("Failed to insert breakpoint at 0x%s (%s).\n",
323 paddress (bp
->pc
), strerror (err
));
326 return err
!= 0 ? -1 : 0;
329 /* See mem-break.h */
332 remove_memory_breakpoint (struct raw_breakpoint
*bp
)
334 unsigned char buf
[MAX_BREAKPOINT_LEN
];
337 /* Since there can be trap breakpoints inserted in the same address
338 range, we use `write_inferior_memory', which takes care of
339 layering breakpoints on top of fast tracepoints, and on top of
340 the buffer we pass it. This works because the caller has already
341 either unlinked the breakpoint or marked it uninserted. Also
342 note that we need to pass the current shadow contents, because
343 write_inferior_memory updates any shadow memory with what we pass
344 here, and we want that to be a nop. */
345 memcpy (buf
, bp
->old_data
, breakpoint_len
);
346 err
= write_inferior_memory (bp
->pc
, buf
, breakpoint_len
);
350 debug_printf ("Failed to uninsert raw breakpoint "
351 "at 0x%s (%s) while deleting it.\n",
352 paddress (bp
->pc
), strerror (err
));
354 return err
!= 0 ? -1 : 0;
357 /* Set a RAW breakpoint of type TYPE and size SIZE at WHERE. On
358 success, a pointer to the new breakpoint is returned. On failure,
359 returns NULL and writes the error code to *ERR. */
361 static struct raw_breakpoint
*
362 set_raw_breakpoint_at (enum raw_bkpt_type type
, CORE_ADDR where
, int size
,
365 struct process_info
*proc
= current_process ();
366 struct raw_breakpoint
*bp
;
368 if (type
== raw_bkpt_type_sw
|| type
== raw_bkpt_type_hw
)
370 bp
= find_enabled_raw_code_breakpoint_at (where
, type
);
371 if (bp
!= NULL
&& bp
->size
!= size
)
373 /* A different size than previously seen. The previous
374 breakpoint must be gone then. */
376 debug_printf ("Inconsistent breakpoint size? Was %d, now %d.\n",
383 bp
= find_raw_breakpoint_at (where
, type
, size
);
391 bp
= xcalloc (1, sizeof (*bp
));
397 *err
= the_target
->insert_point (bp
->raw_type
, bp
->pc
, bp
->size
, bp
);
401 debug_printf ("Failed to insert breakpoint at 0x%s (%d).\n",
402 paddress (where
), *err
);
408 /* Link the breakpoint in. */
409 bp
->next
= proc
->raw_breakpoints
;
410 proc
->raw_breakpoints
= bp
;
414 /* Notice that breakpoint traps are always installed on top of fast
415 tracepoint jumps. This is even if the fast tracepoint is installed
416 at a later time compared to when the breakpoint was installed.
417 This means that a stopping breakpoint or tracepoint has higher
418 "priority". In turn, this allows having fast and slow tracepoints
419 (and breakpoints) at the same address behave correctly. */
422 /* A fast tracepoint jump. */
424 struct fast_tracepoint_jump
426 struct fast_tracepoint_jump
*next
;
428 /* A reference count. GDB can install more than one fast tracepoint
429 at the same address (each with its own action list, for
433 /* The fast tracepoint's insertion address. There can only be one
434 of these for a given PC. */
437 /* Non-zero if this fast tracepoint jump is currently inserted in
441 /* The length of the jump instruction. */
444 /* A poor-man's flexible array member, holding both the jump
445 instruction to insert, and a copy of the instruction that would
446 be in memory had not been a jump there (the shadow memory of the
448 unsigned char insn_and_shadow
[0];
451 /* Fast tracepoint FP's jump instruction to insert. */
452 #define fast_tracepoint_jump_insn(fp) \
453 ((fp)->insn_and_shadow + 0)
455 /* The shadow memory of fast tracepoint jump FP. */
456 #define fast_tracepoint_jump_shadow(fp) \
457 ((fp)->insn_and_shadow + (fp)->length)
460 /* Return the fast tracepoint jump set at WHERE. */
462 static struct fast_tracepoint_jump
*
463 find_fast_tracepoint_jump_at (CORE_ADDR where
)
465 struct process_info
*proc
= current_process ();
466 struct fast_tracepoint_jump
*jp
;
468 for (jp
= proc
->fast_tracepoint_jumps
; jp
!= NULL
; jp
= jp
->next
)
476 fast_tracepoint_jump_here (CORE_ADDR where
)
478 struct fast_tracepoint_jump
*jp
= find_fast_tracepoint_jump_at (where
);
484 delete_fast_tracepoint_jump (struct fast_tracepoint_jump
*todel
)
486 struct fast_tracepoint_jump
*bp
, **bp_link
;
488 struct process_info
*proc
= current_process ();
490 bp
= proc
->fast_tracepoint_jumps
;
491 bp_link
= &proc
->fast_tracepoint_jumps
;
497 if (--bp
->refcount
== 0)
499 struct fast_tracepoint_jump
*prev_bp_link
= *bp_link
;
505 /* Since there can be breakpoints inserted in the same
506 address range, we use `write_inferior_memory', which
507 takes care of layering breakpoints on top of fast
508 tracepoints, and on top of the buffer we pass it.
509 This works because we've already unlinked the fast
510 tracepoint jump above. Also note that we need to
511 pass the current shadow contents, because
512 write_inferior_memory updates any shadow memory with
513 what we pass here, and we want that to be a nop. */
514 buf
= alloca (bp
->length
);
515 memcpy (buf
, fast_tracepoint_jump_shadow (bp
), bp
->length
);
516 ret
= write_inferior_memory (bp
->pc
, buf
, bp
->length
);
519 /* Something went wrong, relink the jump. */
520 *bp_link
= prev_bp_link
;
523 debug_printf ("Failed to uninsert fast tracepoint jump "
524 "at 0x%s (%s) while deleting it.\n",
525 paddress (bp
->pc
), strerror (ret
));
541 warning ("Could not find fast tracepoint jump in list.");
546 inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump
*jp
)
551 struct fast_tracepoint_jump
*
552 set_fast_tracepoint_jump (CORE_ADDR where
,
553 unsigned char *insn
, ULONGEST length
)
555 struct process_info
*proc
= current_process ();
556 struct fast_tracepoint_jump
*jp
;
560 /* We refcount fast tracepoint jumps. Check if we already know
561 about a jump at this address. */
562 jp
= find_fast_tracepoint_jump_at (where
);
569 /* We don't, so create a new object. Double the length, because the
570 flexible array member holds both the jump insn, and the
572 jp
= xcalloc (1, sizeof (*jp
) + (length
* 2));
575 memcpy (fast_tracepoint_jump_insn (jp
), insn
, length
);
577 buf
= alloca (length
);
579 /* Note that there can be trap breakpoints inserted in the same
580 address range. To access the original memory contents, we use
581 `read_inferior_memory', which masks out breakpoints. */
582 err
= read_inferior_memory (where
, buf
, length
);
586 debug_printf ("Failed to read shadow memory of"
587 " fast tracepoint at 0x%s (%s).\n",
588 paddress (where
), strerror (err
));
592 memcpy (fast_tracepoint_jump_shadow (jp
), buf
, length
);
594 /* Link the jump in. */
596 jp
->next
= proc
->fast_tracepoint_jumps
;
597 proc
->fast_tracepoint_jumps
= jp
;
599 /* Since there can be trap breakpoints inserted in the same address
600 range, we use use `write_inferior_memory', which takes care of
601 layering breakpoints on top of fast tracepoints, on top of the
602 buffer we pass it. This works because we've already linked in
603 the fast tracepoint jump above. Also note that we need to pass
604 the current shadow contents, because write_inferior_memory
605 updates any shadow memory with what we pass here, and we want
607 err
= write_inferior_memory (where
, buf
, length
);
611 debug_printf ("Failed to insert fast tracepoint jump at 0x%s (%s).\n",
612 paddress (where
), strerror (err
));
615 proc
->fast_tracepoint_jumps
= jp
->next
;
625 uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc
)
627 struct fast_tracepoint_jump
*jp
;
630 jp
= find_fast_tracepoint_jump_at (pc
);
633 /* This can happen when we remove all breakpoints while handling
636 debug_printf ("Could not find fast tracepoint jump at 0x%s "
637 "in list (uninserting).\n",
648 /* Since there can be trap breakpoints inserted in the same
649 address range, we use use `write_inferior_memory', which
650 takes care of layering breakpoints on top of fast
651 tracepoints, and on top of the buffer we pass it. This works
652 because we've already marked the fast tracepoint fast
653 tracepoint jump uninserted above. Also note that we need to
654 pass the current shadow contents, because
655 write_inferior_memory updates any shadow memory with what we
656 pass here, and we want that to be a nop. */
657 buf
= alloca (jp
->length
);
658 memcpy (buf
, fast_tracepoint_jump_shadow (jp
), jp
->length
);
659 err
= write_inferior_memory (jp
->pc
, buf
, jp
->length
);
665 debug_printf ("Failed to uninsert fast tracepoint jump at"
667 paddress (pc
), strerror (err
));
673 reinsert_fast_tracepoint_jumps_at (CORE_ADDR where
)
675 struct fast_tracepoint_jump
*jp
;
679 jp
= find_fast_tracepoint_jump_at (where
);
682 /* This can happen when we remove breakpoints when a tracepoint
683 hit causes a tracing stop, while handling a step-over. */
685 debug_printf ("Could not find fast tracepoint jump at 0x%s "
686 "in list (reinserting).\n",
692 error ("Jump already inserted at reinsert time.");
696 /* Since there can be trap breakpoints inserted in the same address
697 range, we use `write_inferior_memory', which takes care of
698 layering breakpoints on top of fast tracepoints, and on top of
699 the buffer we pass it. This works because we've already marked
700 the fast tracepoint jump inserted above. Also note that we need
701 to pass the current shadow contents, because
702 write_inferior_memory updates any shadow memory with what we pass
703 here, and we want that to be a nop. */
704 buf
= alloca (jp
->length
);
705 memcpy (buf
, fast_tracepoint_jump_shadow (jp
), jp
->length
);
706 err
= write_inferior_memory (where
, buf
, jp
->length
);
712 debug_printf ("Failed to reinsert fast tracepoint jump at"
714 paddress (where
), strerror (err
));
718 /* Set a high-level breakpoint of type TYPE, with low level type
719 RAW_TYPE and size SIZE, at WHERE. On success, a pointer to the new
720 breakpoint is returned. On failure, returns NULL and writes the
721 error code to *ERR. HANDLER is called when the breakpoint is hit.
722 HANDLER should return 1 if the breakpoint should be deleted, 0
725 static struct breakpoint
*
726 set_breakpoint (enum bkpt_type type
, enum raw_bkpt_type raw_type
,
727 CORE_ADDR where
, int size
,
728 int (*handler
) (CORE_ADDR
), int *err
)
730 struct process_info
*proc
= current_process ();
731 struct breakpoint
*bp
;
732 struct raw_breakpoint
*raw
;
734 raw
= set_raw_breakpoint_at (raw_type
, where
, size
, err
);
742 bp
= xcalloc (1, sizeof (struct breakpoint
));
746 bp
->handler
= handler
;
748 bp
->next
= proc
->breakpoints
;
749 proc
->breakpoints
= bp
;
754 /* See mem-break.h */
757 set_breakpoint_at (CORE_ADDR where
, int (*handler
) (CORE_ADDR
))
761 return set_breakpoint (other_breakpoint
, raw_bkpt_type_sw
,
762 where
, breakpoint_len
, handler
,
768 delete_raw_breakpoint (struct process_info
*proc
, struct raw_breakpoint
*todel
)
770 struct raw_breakpoint
*bp
, **bp_link
;
773 bp
= proc
->raw_breakpoints
;
774 bp_link
= &proc
->raw_breakpoints
;
780 if (bp
->inserted
> 0)
782 struct raw_breakpoint
*prev_bp_link
= *bp_link
;
786 ret
= the_target
->remove_point (bp
->raw_type
, bp
->pc
, bp
->size
,
790 /* Something went wrong, relink the breakpoint. */
791 *bp_link
= prev_bp_link
;
794 debug_printf ("Failed to uninsert raw breakpoint "
795 "at 0x%s while deleting it.\n",
813 warning ("Could not find raw breakpoint in list.");
818 release_breakpoint (struct process_info
*proc
, struct breakpoint
*bp
)
823 newrefcount
= bp
->raw
->refcount
- 1;
824 if (newrefcount
== 0)
826 ret
= delete_raw_breakpoint (proc
, bp
->raw
);
831 bp
->raw
->refcount
= newrefcount
;
839 delete_breakpoint_1 (struct process_info
*proc
, struct breakpoint
*todel
)
841 struct breakpoint
*bp
, **bp_link
;
844 bp
= proc
->breakpoints
;
845 bp_link
= &proc
->breakpoints
;
853 err
= release_breakpoint (proc
, bp
);
867 warning ("Could not find breakpoint in list.");
872 delete_breakpoint (struct breakpoint
*todel
)
874 struct process_info
*proc
= current_process ();
875 return delete_breakpoint_1 (proc
, todel
);
878 /* Locate a GDB breakpoint of type Z_TYPE and size SIZE placed at
879 address ADDR and return a pointer to its structure. If SIZE is -1,
880 the breakpoints' sizes are ignored. */
882 static struct breakpoint
*
883 find_gdb_breakpoint (char z_type
, CORE_ADDR addr
, int size
)
885 struct process_info
*proc
= current_process ();
886 struct breakpoint
*bp
;
887 enum bkpt_type type
= Z_packet_to_bkpt_type (z_type
);
889 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
890 if (bp
->type
== type
&& bp
->raw
->pc
== addr
891 && (size
== -1 || bp
->raw
->size
== size
))
898 z_type_supported (char z_type
)
900 return (z_type
>= '0' && z_type
<= '4'
901 && the_target
->supports_z_point_type
!= NULL
902 && the_target
->supports_z_point_type (z_type
));
905 /* Create a new GDB breakpoint of type Z_TYPE at ADDR with size SIZE.
906 Returns a pointer to the newly created breakpoint on success. On
907 failure returns NULL and sets *ERR to either -1 for error, or 1 if
908 Z_TYPE breakpoints are not supported on this target. */
910 static struct breakpoint
*
911 set_gdb_breakpoint_1 (char z_type
, CORE_ADDR addr
, int size
, int *err
)
913 struct breakpoint
*bp
;
915 enum raw_bkpt_type raw_type
;
917 /* If we see GDB inserting a second code breakpoint at the same
918 address, then either: GDB is updating the breakpoint's conditions
919 or commands; or, the first breakpoint must have disappeared due
920 to a shared library unload. On targets where the shared
921 libraries are handled by userspace, like SVR4, for example,
922 GDBserver can't tell if a library was loaded or unloaded. Since
923 we refcount raw breakpoints, we must be careful to make sure GDB
924 breakpoints never contribute more than one reference. if we
925 didn't do this, in case the previous breakpoint is gone due to a
926 shared library unload, we'd just increase the refcount of the
927 previous breakpoint at this address, but the trap was not planted
928 in the inferior anymore, thus the breakpoint would never be hit.
929 Note this must be careful to not create a window where
930 breakpoints are removed from the target, for non-stop, in case
931 the target can poke at memory while the program is running. */
932 if (z_type
== Z_PACKET_SW_BP
933 || z_type
== Z_PACKET_HW_BP
)
935 bp
= find_gdb_breakpoint (z_type
, addr
, -1);
939 if (bp
->raw
->size
!= size
)
941 /* A different size than previously seen. The previous
942 breakpoint must be gone then. */
943 bp
->raw
->inserted
= -1;
944 delete_breakpoint (bp
);
947 else if (z_type
== Z_PACKET_SW_BP
)
949 /* Check if the breakpoint is actually gone from the
950 target, due to an solib unload, for example. Might
951 as well validate _all_ breakpoints. */
952 validate_breakpoints ();
954 /* Breakpoints that don't pass validation are
956 bp
= find_gdb_breakpoint (z_type
, addr
, -1);
962 /* Data breakpoints for the same address but different size are
963 expected. GDB doesn't merge these. The backend gets to do
964 that if it wants/can. */
965 bp
= find_gdb_breakpoint (z_type
, addr
, size
);
970 /* We already know about this breakpoint, there's nothing else
971 to do - GDB's reference is already accounted for. Note that
972 whether the breakpoint inserted is left as is - we may be
973 stepping over it, for example, in which case we don't want to
974 force-reinsert it. */
978 raw_type
= Z_packet_to_raw_bkpt_type (z_type
);
979 type
= Z_packet_to_bkpt_type (z_type
);
980 return set_breakpoint (type
, raw_type
, addr
, size
, NULL
, err
);
984 check_gdb_bp_preconditions (char z_type
, int *err
)
986 /* As software/memory breakpoints work by poking at memory, we need
987 to prepare to access memory. If that operation fails, we need to
988 return error. Seeing an error, if this is the first breakpoint
989 of that type that GDB tries to insert, GDB would then assume the
990 breakpoint type is supported, but it may actually not be. So we
991 need to check whether the type is supported at all before
992 preparing to access memory. */
993 if (!z_type_supported (z_type
))
998 else if (current_thread
== NULL
)
1007 /* See mem-break.h. This is a wrapper for set_gdb_breakpoint_1 that
1008 knows to prepare to access memory for Z0 breakpoints. */
1011 set_gdb_breakpoint (char z_type
, CORE_ADDR addr
, int size
, int *err
)
1013 struct breakpoint
*bp
;
1015 if (!check_gdb_bp_preconditions (z_type
, err
))
1018 /* If inserting a software/memory breakpoint, need to prepare to
1020 if (z_type
== Z_PACKET_SW_BP
)
1022 *err
= prepare_to_access_memory ();
1027 bp
= set_gdb_breakpoint_1 (z_type
, addr
, size
, err
);
1029 if (z_type
== Z_PACKET_SW_BP
)
1030 done_accessing_memory ();
1035 /* Delete a GDB breakpoint of type Z_TYPE and size SIZE previously
1036 inserted at ADDR with set_gdb_breakpoint_at. Returns 0 on success,
1037 -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
1041 delete_gdb_breakpoint_1 (char z_type
, CORE_ADDR addr
, int size
)
1043 struct breakpoint
*bp
;
1046 bp
= find_gdb_breakpoint (z_type
, addr
, size
);
1050 /* Before deleting the breakpoint, make sure to free its condition
1051 and command lists. */
1052 clear_breakpoint_conditions_and_commands (bp
);
1053 err
= delete_breakpoint (bp
);
1060 /* See mem-break.h. This is a wrapper for delete_gdb_breakpoint that
1061 knows to prepare to access memory for Z0 breakpoints. */
1064 delete_gdb_breakpoint (char z_type
, CORE_ADDR addr
, int size
)
1068 if (!check_gdb_bp_preconditions (z_type
, &ret
))
1071 /* If inserting a software/memory breakpoint, need to prepare to
1073 if (z_type
== Z_PACKET_SW_BP
)
1077 err
= prepare_to_access_memory ();
1082 ret
= delete_gdb_breakpoint_1 (z_type
, addr
, size
);
1084 if (z_type
== Z_PACKET_SW_BP
)
1085 done_accessing_memory ();
1090 /* Clear all conditions associated with a breakpoint. */
1093 clear_breakpoint_conditions (struct breakpoint
*bp
)
1095 struct point_cond_list
*cond
;
1097 if (bp
->cond_list
== NULL
)
1100 cond
= bp
->cond_list
;
1102 while (cond
!= NULL
)
1104 struct point_cond_list
*cond_next
;
1106 cond_next
= cond
->next
;
1107 gdb_free_agent_expr (cond
->cond
);
1112 bp
->cond_list
= NULL
;
1115 /* Clear all commands associated with a breakpoint. */
1118 clear_breakpoint_commands (struct breakpoint
*bp
)
1120 struct point_command_list
*cmd
;
1122 if (bp
->command_list
== NULL
)
1125 cmd
= bp
->command_list
;
1129 struct point_command_list
*cmd_next
;
1131 cmd_next
= cmd
->next
;
1132 gdb_free_agent_expr (cmd
->cmd
);
1137 bp
->command_list
= NULL
;
1141 clear_breakpoint_conditions_and_commands (struct breakpoint
*bp
)
1143 clear_breakpoint_conditions (bp
);
1144 clear_breakpoint_commands (bp
);
1147 /* Add condition CONDITION to GDBserver's breakpoint BP. */
1150 add_condition_to_breakpoint (struct breakpoint
*bp
,
1151 struct agent_expr
*condition
)
1153 struct point_cond_list
*new_cond
;
1155 /* Create new condition. */
1156 new_cond
= xcalloc (1, sizeof (*new_cond
));
1157 new_cond
->cond
= condition
;
1159 /* Add condition to the list. */
1160 new_cond
->next
= bp
->cond_list
;
1161 bp
->cond_list
= new_cond
;
1164 /* Add a target-side condition CONDITION to a breakpoint. */
1167 add_breakpoint_condition (struct breakpoint
*bp
, char **condition
)
1169 char *actparm
= *condition
;
1170 struct agent_expr
*cond
;
1172 if (condition
== NULL
)
1178 cond
= gdb_parse_agent_expr (&actparm
);
1182 fprintf (stderr
, "Condition evaluation failed. "
1183 "Assuming unconditional.\n");
1187 add_condition_to_breakpoint (bp
, cond
);
1189 *condition
= actparm
;
1194 /* Evaluate condition (if any) at breakpoint BP. Return 1 if
1195 true and 0 otherwise. */
1198 gdb_condition_true_at_breakpoint_z_type (char z_type
, CORE_ADDR addr
)
1200 /* Fetch registers for the current inferior. */
1201 struct breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1203 struct point_cond_list
*cl
;
1205 struct eval_agent_expr_context ctx
;
1210 /* Check if the breakpoint is unconditional. If it is,
1211 the condition always evaluates to TRUE. */
1212 if (bp
->cond_list
== NULL
)
1215 ctx
.regcache
= get_thread_regcache (current_thread
, 1);
1219 /* Evaluate each condition in the breakpoint's list of conditions.
1220 Return true if any of the conditions evaluates to TRUE.
1222 If we failed to evaluate the expression, TRUE is returned. This
1223 forces GDB to reevaluate the conditions. */
1224 for (cl
= bp
->cond_list
;
1225 cl
&& !value
&& !err
; cl
= cl
->next
)
1227 /* Evaluate the condition. */
1228 err
= gdb_eval_agent_expr (&ctx
, cl
->cond
, &value
);
1234 return (value
!= 0);
1238 gdb_condition_true_at_breakpoint (CORE_ADDR where
)
1240 /* Only check code (software or hardware) breakpoints. */
1241 return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP
, where
)
1242 || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP
, where
));
1245 /* Add commands COMMANDS to GDBserver's breakpoint BP. */
1248 add_commands_to_breakpoint (struct breakpoint
*bp
,
1249 struct agent_expr
*commands
, int persist
)
1251 struct point_command_list
*new_cmd
;
1253 /* Create new command. */
1254 new_cmd
= xcalloc (1, sizeof (*new_cmd
));
1255 new_cmd
->cmd
= commands
;
1256 new_cmd
->persistence
= persist
;
1258 /* Add commands to the list. */
1259 new_cmd
->next
= bp
->command_list
;
1260 bp
->command_list
= new_cmd
;
1263 /* Add a target-side command COMMAND to the breakpoint at ADDR. */
1266 add_breakpoint_commands (struct breakpoint
*bp
, char **command
,
1269 char *actparm
= *command
;
1270 struct agent_expr
*cmd
;
1272 if (command
== NULL
)
1278 cmd
= gdb_parse_agent_expr (&actparm
);
1282 fprintf (stderr
, "Command evaluation failed. "
1287 add_commands_to_breakpoint (bp
, cmd
, persist
);
1294 /* Return true if there are no commands to run at this location,
1295 which likely means we want to report back to GDB. */
1298 gdb_no_commands_at_breakpoint_z_type (char z_type
, CORE_ADDR addr
)
1300 struct breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1306 debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s\n",
1307 paddress (addr
), z_type
,
1308 phex_nz ((uintptr_t) bp
->command_list
, 0));
1309 return (bp
->command_list
== NULL
);
1312 /* Return true if there are no commands to run at this location,
1313 which likely means we want to report back to GDB. */
1316 gdb_no_commands_at_breakpoint (CORE_ADDR where
)
1318 /* Only check code (software or hardware) breakpoints. */
1319 return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP
, where
)
1320 && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP
, where
));
1323 /* Run a breakpoint's commands. Returns 0 if there was a problem
1324 running any command, 1 otherwise. */
1327 run_breakpoint_commands_z_type (char z_type
, CORE_ADDR addr
)
1329 /* Fetch registers for the current inferior. */
1330 struct breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1332 struct point_command_list
*cl
;
1334 struct eval_agent_expr_context ctx
;
1339 ctx
.regcache
= get_thread_regcache (current_thread
, 1);
1343 for (cl
= bp
->command_list
;
1344 cl
&& !value
&& !err
; cl
= cl
->next
)
1346 /* Run the command. */
1347 err
= gdb_eval_agent_expr (&ctx
, cl
->cmd
, &value
);
1349 /* If one command has a problem, stop digging the hole deeper. */
1358 run_breakpoint_commands (CORE_ADDR where
)
1360 /* Only check code (software or hardware) breakpoints. If one
1361 command has a problem, stop digging the hole deeper. */
1362 if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP
, where
))
1363 run_breakpoint_commands_z_type (Z_PACKET_HW_BP
, where
);
1366 /* See mem-break.h. */
1369 gdb_breakpoint_here (CORE_ADDR where
)
1371 /* Only check code (software or hardware) breakpoints. */
1372 return (find_gdb_breakpoint (Z_PACKET_SW_BP
, where
, -1) != NULL
1373 || find_gdb_breakpoint (Z_PACKET_HW_BP
, where
, -1) != NULL
);
1377 set_reinsert_breakpoint (CORE_ADDR stop_at
)
1379 struct breakpoint
*bp
;
1381 bp
= set_breakpoint_at (stop_at
, NULL
);
1382 bp
->type
= reinsert_breakpoint
;
1386 delete_reinsert_breakpoints (void)
1388 struct process_info
*proc
= current_process ();
1389 struct breakpoint
*bp
, **bp_link
;
1391 bp
= proc
->breakpoints
;
1392 bp_link
= &proc
->breakpoints
;
1396 if (bp
->type
== reinsert_breakpoint
)
1398 *bp_link
= bp
->next
;
1399 release_breakpoint (proc
, bp
);
1404 bp_link
= &bp
->next
;
1411 uninsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1413 if (bp
->inserted
< 0)
1416 debug_printf ("Breakpoint at %s is marked insert-disabled.\n",
1419 else if (bp
->inserted
> 0)
1425 err
= the_target
->remove_point (bp
->raw_type
, bp
->pc
, bp
->size
, bp
);
1431 debug_printf ("Failed to uninsert raw breakpoint at 0x%s.\n",
1438 uninsert_breakpoints_at (CORE_ADDR pc
)
1440 struct process_info
*proc
= current_process ();
1441 struct raw_breakpoint
*bp
;
1444 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1445 if ((bp
->raw_type
== raw_bkpt_type_sw
1446 || bp
->raw_type
== raw_bkpt_type_hw
)
1452 uninsert_raw_breakpoint (bp
);
1457 /* This can happen when we remove all breakpoints while handling
1460 debug_printf ("Could not find breakpoint at 0x%s "
1461 "in list (uninserting).\n",
1467 uninsert_all_breakpoints (void)
1469 struct process_info
*proc
= current_process ();
1470 struct raw_breakpoint
*bp
;
1472 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1473 if ((bp
->raw_type
== raw_bkpt_type_sw
1474 || bp
->raw_type
== raw_bkpt_type_hw
)
1476 uninsert_raw_breakpoint (bp
);
1480 reinsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1485 error ("Breakpoint already inserted at reinsert time.");
1487 err
= the_target
->insert_point (bp
->raw_type
, bp
->pc
, bp
->size
, bp
);
1490 else if (debug_threads
)
1491 debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).\n",
1492 paddress (bp
->pc
), err
);
1496 reinsert_breakpoints_at (CORE_ADDR pc
)
1498 struct process_info
*proc
= current_process ();
1499 struct raw_breakpoint
*bp
;
1502 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1503 if ((bp
->raw_type
== raw_bkpt_type_sw
1504 || bp
->raw_type
== raw_bkpt_type_hw
)
1509 reinsert_raw_breakpoint (bp
);
1514 /* This can happen when we remove all breakpoints while handling
1517 debug_printf ("Could not find raw breakpoint at 0x%s "
1518 "in list (reinserting).\n",
1524 reinsert_all_breakpoints (void)
1526 struct process_info
*proc
= current_process ();
1527 struct raw_breakpoint
*bp
;
1529 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1530 if ((bp
->raw_type
== raw_bkpt_type_sw
1531 || bp
->raw_type
== raw_bkpt_type_hw
)
1533 reinsert_raw_breakpoint (bp
);
1537 check_breakpoints (CORE_ADDR stop_pc
)
1539 struct process_info
*proc
= current_process ();
1540 struct breakpoint
*bp
, **bp_link
;
1542 bp
= proc
->breakpoints
;
1543 bp_link
= &proc
->breakpoints
;
1547 struct raw_breakpoint
*raw
= bp
->raw
;
1549 if ((raw
->raw_type
== raw_bkpt_type_sw
1550 || raw
->raw_type
== raw_bkpt_type_hw
)
1551 && raw
->pc
== stop_pc
)
1555 warning ("Hit a removed breakpoint?");
1559 if (bp
->handler
!= NULL
&& (*bp
->handler
) (stop_pc
))
1561 *bp_link
= bp
->next
;
1563 release_breakpoint (proc
, bp
);
1570 bp_link
= &bp
->next
;
1576 set_breakpoint_data (const unsigned char *bp_data
, int bp_len
)
1578 breakpoint_data
= bp_data
;
1579 breakpoint_len
= bp_len
;
1583 breakpoint_here (CORE_ADDR addr
)
1585 struct process_info
*proc
= current_process ();
1586 struct raw_breakpoint
*bp
;
1588 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1589 if ((bp
->raw_type
== raw_bkpt_type_sw
1590 || bp
->raw_type
== raw_bkpt_type_hw
)
1598 breakpoint_inserted_here (CORE_ADDR addr
)
1600 struct process_info
*proc
= current_process ();
1601 struct raw_breakpoint
*bp
;
1603 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1604 if ((bp
->raw_type
== raw_bkpt_type_sw
1605 || bp
->raw_type
== raw_bkpt_type_hw
)
1614 validate_inserted_breakpoint (struct raw_breakpoint
*bp
)
1619 gdb_assert (bp
->inserted
);
1620 gdb_assert (bp
->raw_type
== raw_bkpt_type_sw
);
1622 buf
= alloca (breakpoint_len
);
1623 err
= (*the_target
->read_memory
) (bp
->pc
, buf
, breakpoint_len
);
1624 if (err
|| memcmp (buf
, breakpoint_data
, breakpoint_len
) != 0)
1626 /* Tag it as gone. */
1635 delete_disabled_breakpoints (void)
1637 struct process_info
*proc
= current_process ();
1638 struct breakpoint
*bp
, *next
;
1640 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= next
)
1643 if (bp
->raw
->inserted
< 0)
1644 delete_breakpoint_1 (proc
, bp
);
1648 /* Check if breakpoints we inserted still appear to be inserted. They
1649 may disappear due to a shared library unload, and worse, a new
1650 shared library may be reloaded at the same address as the
1651 previously unloaded one. If that happens, we should make sure that
1652 the shadow memory of the old breakpoints isn't used when reading or
1656 validate_breakpoints (void)
1658 struct process_info
*proc
= current_process ();
1659 struct breakpoint
*bp
;
1661 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1663 struct raw_breakpoint
*raw
= bp
->raw
;
1665 if (raw
->raw_type
== raw_bkpt_type_sw
&& raw
->inserted
> 0)
1666 validate_inserted_breakpoint (raw
);
1669 delete_disabled_breakpoints ();
1673 check_mem_read (CORE_ADDR mem_addr
, unsigned char *buf
, int mem_len
)
1675 struct process_info
*proc
= current_process ();
1676 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
1677 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
1678 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
1679 int disabled_one
= 0;
1681 for (; jp
!= NULL
; jp
= jp
->next
)
1683 CORE_ADDR bp_end
= jp
->pc
+ jp
->length
;
1684 CORE_ADDR start
, end
;
1685 int copy_offset
, copy_len
, buf_offset
;
1687 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= buf
+ mem_len
1688 || buf
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
1690 if (mem_addr
>= bp_end
)
1692 if (jp
->pc
>= mem_end
)
1696 if (mem_addr
> start
)
1703 copy_len
= end
- start
;
1704 copy_offset
= start
- jp
->pc
;
1705 buf_offset
= start
- mem_addr
;
1708 memcpy (buf
+ buf_offset
,
1709 fast_tracepoint_jump_shadow (jp
) + copy_offset
,
1713 for (; bp
!= NULL
; bp
= bp
->next
)
1715 CORE_ADDR bp_end
= bp
->pc
+ breakpoint_len
;
1716 CORE_ADDR start
, end
;
1717 int copy_offset
, copy_len
, buf_offset
;
1719 if (bp
->raw_type
!= raw_bkpt_type_sw
)
1722 gdb_assert (bp
->old_data
>= buf
+ mem_len
1723 || buf
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
1725 if (mem_addr
>= bp_end
)
1727 if (bp
->pc
>= mem_end
)
1731 if (mem_addr
> start
)
1738 copy_len
= end
- start
;
1739 copy_offset
= start
- bp
->pc
;
1740 buf_offset
= start
- mem_addr
;
1742 if (bp
->inserted
> 0)
1744 if (validate_inserted_breakpoint (bp
))
1745 memcpy (buf
+ buf_offset
, bp
->old_data
+ copy_offset
, copy_len
);
1752 delete_disabled_breakpoints ();
1756 check_mem_write (CORE_ADDR mem_addr
, unsigned char *buf
,
1757 const unsigned char *myaddr
, int mem_len
)
1759 struct process_info
*proc
= current_process ();
1760 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
1761 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
1762 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
1763 int disabled_one
= 0;
1765 /* First fast tracepoint jumps, then breakpoint traps on top. */
1767 for (; jp
!= NULL
; jp
= jp
->next
)
1769 CORE_ADDR jp_end
= jp
->pc
+ jp
->length
;
1770 CORE_ADDR start
, end
;
1771 int copy_offset
, copy_len
, buf_offset
;
1773 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= myaddr
+ mem_len
1774 || myaddr
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
1775 gdb_assert (fast_tracepoint_jump_insn (jp
) >= buf
+ mem_len
1776 || buf
>= fast_tracepoint_jump_insn (jp
) + (jp
)->length
);
1778 if (mem_addr
>= jp_end
)
1780 if (jp
->pc
>= mem_end
)
1784 if (mem_addr
> start
)
1791 copy_len
= end
- start
;
1792 copy_offset
= start
- jp
->pc
;
1793 buf_offset
= start
- mem_addr
;
1795 memcpy (fast_tracepoint_jump_shadow (jp
) + copy_offset
,
1796 myaddr
+ buf_offset
, copy_len
);
1798 memcpy (buf
+ buf_offset
,
1799 fast_tracepoint_jump_insn (jp
) + copy_offset
, copy_len
);
1802 for (; bp
!= NULL
; bp
= bp
->next
)
1804 CORE_ADDR bp_end
= bp
->pc
+ breakpoint_len
;
1805 CORE_ADDR start
, end
;
1806 int copy_offset
, copy_len
, buf_offset
;
1808 if (bp
->raw_type
!= raw_bkpt_type_sw
)
1811 gdb_assert (bp
->old_data
>= myaddr
+ mem_len
1812 || myaddr
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
1814 if (mem_addr
>= bp_end
)
1816 if (bp
->pc
>= mem_end
)
1820 if (mem_addr
> start
)
1827 copy_len
= end
- start
;
1828 copy_offset
= start
- bp
->pc
;
1829 buf_offset
= start
- mem_addr
;
1831 memcpy (bp
->old_data
+ copy_offset
, myaddr
+ buf_offset
, copy_len
);
1832 if (bp
->inserted
> 0)
1834 if (validate_inserted_breakpoint (bp
))
1835 memcpy (buf
+ buf_offset
, breakpoint_data
+ copy_offset
, copy_len
);
1842 delete_disabled_breakpoints ();
1845 /* Delete all breakpoints, and un-insert them from the inferior. */
1848 delete_all_breakpoints (void)
1850 struct process_info
*proc
= current_process ();
1852 while (proc
->breakpoints
)
1853 delete_breakpoint_1 (proc
, proc
->breakpoints
);
1856 /* Clear the "inserted" flag in all breakpoints. */
1859 mark_breakpoints_out (struct process_info
*proc
)
1861 struct raw_breakpoint
*raw_bp
;
1863 for (raw_bp
= proc
->raw_breakpoints
; raw_bp
!= NULL
; raw_bp
= raw_bp
->next
)
1864 raw_bp
->inserted
= 0;
1867 /* Release all breakpoints, but do not try to un-insert them from the
1871 free_all_breakpoints (struct process_info
*proc
)
1873 mark_breakpoints_out (proc
);
1875 /* Note: use PROC explicitly instead of deferring to
1876 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
1877 released when we get here. There should be no call to
1878 current_process from here on. */
1879 while (proc
->breakpoints
)
1880 delete_breakpoint_1 (proc
, proc
->breakpoints
);