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 fatal ("bad raw breakpoing type %d", (int) raw_type
);
196 /* See mem-break.h. */
198 static enum bkpt_type
199 Z_packet_to_bkpt_type (char z_type
)
201 gdb_assert ('0' <= z_type
&& z_type
<= '4');
203 return gdb_breakpoint_Z0
+ (z_type
- '0');
206 /* See mem-break.h. */
209 Z_packet_to_raw_bkpt_type (char z_type
)
214 return raw_bkpt_type_sw
;
216 return raw_bkpt_type_hw
;
217 case Z_PACKET_WRITE_WP
:
218 return raw_bkpt_type_write_wp
;
219 case Z_PACKET_READ_WP
:
220 return raw_bkpt_type_read_wp
;
221 case Z_PACKET_ACCESS_WP
:
222 return raw_bkpt_type_access_wp
;
224 gdb_assert_not_reached ("unhandled Z packet type.");
229 any_persistent_commands ()
231 struct process_info
*proc
= current_process ();
232 struct breakpoint
*bp
;
233 struct point_command_list
*cl
;
235 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
237 for (cl
= bp
->command_list
; cl
!= NULL
; cl
= cl
->next
)
245 /* Find low-level breakpoint of type TYPE at address ADDR that is not
246 insert-disabled. Returns NULL if not found. */
248 static struct raw_breakpoint
*
249 find_enabled_raw_code_breakpoint_at (CORE_ADDR addr
, enum raw_bkpt_type type
)
251 struct process_info
*proc
= current_process ();
252 struct raw_breakpoint
*bp
;
254 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
256 && bp
->raw_type
== type
257 && bp
->inserted
>= 0)
263 /* Find low-level breakpoint of type TYPE at address ADDR. Returns
264 NULL if not found. */
266 static struct raw_breakpoint
*
267 find_raw_breakpoint_at (CORE_ADDR addr
, enum raw_bkpt_type type
, int size
)
269 struct process_info
*proc
= current_process ();
270 struct raw_breakpoint
*bp
;
272 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
273 if (bp
->pc
== addr
&& bp
->raw_type
== type
&& bp
->size
== size
)
279 /* See mem-break.h. */
282 insert_memory_breakpoint (struct raw_breakpoint
*bp
)
284 unsigned char buf
[MAX_BREAKPOINT_LEN
];
287 if (breakpoint_data
== NULL
)
290 /* If the architecture treats the size field of Z packets as a
291 'kind' field, then we'll need to be able to know which is the
292 breakpoint instruction too. */
293 if (bp
->size
!= breakpoint_len
)
296 debug_printf ("Don't know how to insert breakpoints of size %d.\n",
301 /* Note that there can be fast tracepoint jumps installed in the
302 same memory range, so to get at the original memory, we need to
303 use read_inferior_memory, which masks those out. */
304 err
= read_inferior_memory (bp
->pc
, buf
, breakpoint_len
);
308 debug_printf ("Failed to read shadow memory of"
309 " breakpoint at 0x%s (%s).\n",
310 paddress (bp
->pc
), strerror (err
));
314 memcpy (bp
->old_data
, buf
, breakpoint_len
);
316 err
= (*the_target
->write_memory
) (bp
->pc
, breakpoint_data
,
321 debug_printf ("Failed to insert breakpoint at 0x%s (%s).\n",
322 paddress (bp
->pc
), strerror (err
));
325 return err
!= 0 ? -1 : 0;
328 /* See mem-break.h */
331 remove_memory_breakpoint (struct raw_breakpoint
*bp
)
333 unsigned char buf
[MAX_BREAKPOINT_LEN
];
336 /* Since there can be trap breakpoints inserted in the same address
337 range, we use `write_inferior_memory', which takes care of
338 layering breakpoints on top of fast tracepoints, and on top of
339 the buffer we pass it. This works because the caller has already
340 either unlinked the breakpoint or marked it uninserted. Also
341 note that we need to pass the current shadow contents, because
342 write_inferior_memory updates any shadow memory with what we pass
343 here, and we want that to be a nop. */
344 memcpy (buf
, bp
->old_data
, breakpoint_len
);
345 err
= write_inferior_memory (bp
->pc
, buf
, breakpoint_len
);
349 debug_printf ("Failed to uninsert raw breakpoint "
350 "at 0x%s (%s) while deleting it.\n",
351 paddress (bp
->pc
), strerror (err
));
353 return err
!= 0 ? -1 : 0;
356 /* Set a RAW breakpoint of type TYPE and size SIZE at WHERE. On
357 success, a pointer to the new breakpoint is returned. On failure,
358 returns NULL and writes the error code to *ERR. */
360 static struct raw_breakpoint
*
361 set_raw_breakpoint_at (enum raw_bkpt_type type
, CORE_ADDR where
, int size
,
364 struct process_info
*proc
= current_process ();
365 struct raw_breakpoint
*bp
;
367 if (type
== raw_bkpt_type_sw
|| type
== raw_bkpt_type_hw
)
369 bp
= find_enabled_raw_code_breakpoint_at (where
, type
);
370 if (bp
!= NULL
&& bp
->size
!= size
)
372 /* A different size than previously seen. The previous
373 breakpoint must be gone then. */
375 debug_printf ("Inconsistent breakpoint size? Was %d, now %d.\n",
382 bp
= find_raw_breakpoint_at (where
, type
, size
);
390 bp
= xcalloc (1, sizeof (*bp
));
396 *err
= the_target
->insert_point (bp
->raw_type
, bp
->pc
, bp
->size
, bp
);
400 debug_printf ("Failed to insert breakpoint at 0x%s (%d).\n",
401 paddress (where
), *err
);
407 /* Link the breakpoint in. */
408 bp
->next
= proc
->raw_breakpoints
;
409 proc
->raw_breakpoints
= bp
;
413 /* Notice that breakpoint traps are always installed on top of fast
414 tracepoint jumps. This is even if the fast tracepoint is installed
415 at a later time compared to when the breakpoint was installed.
416 This means that a stopping breakpoint or tracepoint has higher
417 "priority". In turn, this allows having fast and slow tracepoints
418 (and breakpoints) at the same address behave correctly. */
421 /* A fast tracepoint jump. */
423 struct fast_tracepoint_jump
425 struct fast_tracepoint_jump
*next
;
427 /* A reference count. GDB can install more than one fast tracepoint
428 at the same address (each with its own action list, for
432 /* The fast tracepoint's insertion address. There can only be one
433 of these for a given PC. */
436 /* Non-zero if this fast tracepoint jump is currently inserted in
440 /* The length of the jump instruction. */
443 /* A poor-man's flexible array member, holding both the jump
444 instruction to insert, and a copy of the instruction that would
445 be in memory had not been a jump there (the shadow memory of the
447 unsigned char insn_and_shadow
[0];
450 /* Fast tracepoint FP's jump instruction to insert. */
451 #define fast_tracepoint_jump_insn(fp) \
452 ((fp)->insn_and_shadow + 0)
454 /* The shadow memory of fast tracepoint jump FP. */
455 #define fast_tracepoint_jump_shadow(fp) \
456 ((fp)->insn_and_shadow + (fp)->length)
459 /* Return the fast tracepoint jump set at WHERE. */
461 static struct fast_tracepoint_jump
*
462 find_fast_tracepoint_jump_at (CORE_ADDR where
)
464 struct process_info
*proc
= current_process ();
465 struct fast_tracepoint_jump
*jp
;
467 for (jp
= proc
->fast_tracepoint_jumps
; jp
!= NULL
; jp
= jp
->next
)
475 fast_tracepoint_jump_here (CORE_ADDR where
)
477 struct fast_tracepoint_jump
*jp
= find_fast_tracepoint_jump_at (where
);
483 delete_fast_tracepoint_jump (struct fast_tracepoint_jump
*todel
)
485 struct fast_tracepoint_jump
*bp
, **bp_link
;
487 struct process_info
*proc
= current_process ();
489 bp
= proc
->fast_tracepoint_jumps
;
490 bp_link
= &proc
->fast_tracepoint_jumps
;
496 if (--bp
->refcount
== 0)
498 struct fast_tracepoint_jump
*prev_bp_link
= *bp_link
;
504 /* Since there can be breakpoints inserted in the same
505 address range, we use `write_inferior_memory', which
506 takes care of layering breakpoints on top of fast
507 tracepoints, and on top of the buffer we pass it.
508 This works because we've already unlinked the fast
509 tracepoint jump above. Also note that we need to
510 pass the current shadow contents, because
511 write_inferior_memory updates any shadow memory with
512 what we pass here, and we want that to be a nop. */
513 buf
= alloca (bp
->length
);
514 memcpy (buf
, fast_tracepoint_jump_shadow (bp
), bp
->length
);
515 ret
= write_inferior_memory (bp
->pc
, buf
, bp
->length
);
518 /* Something went wrong, relink the jump. */
519 *bp_link
= prev_bp_link
;
522 debug_printf ("Failed to uninsert fast tracepoint jump "
523 "at 0x%s (%s) while deleting it.\n",
524 paddress (bp
->pc
), strerror (ret
));
540 warning ("Could not find fast tracepoint jump in list.");
545 inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump
*jp
)
550 struct fast_tracepoint_jump
*
551 set_fast_tracepoint_jump (CORE_ADDR where
,
552 unsigned char *insn
, ULONGEST length
)
554 struct process_info
*proc
= current_process ();
555 struct fast_tracepoint_jump
*jp
;
559 /* We refcount fast tracepoint jumps. Check if we already know
560 about a jump at this address. */
561 jp
= find_fast_tracepoint_jump_at (where
);
568 /* We don't, so create a new object. Double the length, because the
569 flexible array member holds both the jump insn, and the
571 jp
= xcalloc (1, sizeof (*jp
) + (length
* 2));
574 memcpy (fast_tracepoint_jump_insn (jp
), insn
, length
);
576 buf
= alloca (length
);
578 /* Note that there can be trap breakpoints inserted in the same
579 address range. To access the original memory contents, we use
580 `read_inferior_memory', which masks out breakpoints. */
581 err
= read_inferior_memory (where
, buf
, length
);
585 debug_printf ("Failed to read shadow memory of"
586 " fast tracepoint at 0x%s (%s).\n",
587 paddress (where
), strerror (err
));
591 memcpy (fast_tracepoint_jump_shadow (jp
), buf
, length
);
593 /* Link the jump in. */
595 jp
->next
= proc
->fast_tracepoint_jumps
;
596 proc
->fast_tracepoint_jumps
= jp
;
598 /* Since there can be trap breakpoints inserted in the same address
599 range, we use use `write_inferior_memory', which takes care of
600 layering breakpoints on top of fast tracepoints, on top of the
601 buffer we pass it. This works because we've already linked in
602 the fast tracepoint jump above. Also note that we need to pass
603 the current shadow contents, because write_inferior_memory
604 updates any shadow memory with what we pass here, and we want
606 err
= write_inferior_memory (where
, buf
, length
);
610 debug_printf ("Failed to insert fast tracepoint jump at 0x%s (%s).\n",
611 paddress (where
), strerror (err
));
614 proc
->fast_tracepoint_jumps
= jp
->next
;
624 uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc
)
626 struct fast_tracepoint_jump
*jp
;
629 jp
= find_fast_tracepoint_jump_at (pc
);
632 /* This can happen when we remove all breakpoints while handling
635 debug_printf ("Could not find fast tracepoint jump at 0x%s "
636 "in list (uninserting).\n",
647 /* Since there can be trap breakpoints inserted in the same
648 address range, we use use `write_inferior_memory', which
649 takes care of layering breakpoints on top of fast
650 tracepoints, and on top of the buffer we pass it. This works
651 because we've already marked the fast tracepoint fast
652 tracepoint jump uninserted above. Also note that we need to
653 pass the current shadow contents, because
654 write_inferior_memory updates any shadow memory with what we
655 pass here, and we want that to be a nop. */
656 buf
= alloca (jp
->length
);
657 memcpy (buf
, fast_tracepoint_jump_shadow (jp
), jp
->length
);
658 err
= write_inferior_memory (jp
->pc
, buf
, jp
->length
);
664 debug_printf ("Failed to uninsert fast tracepoint jump at"
666 paddress (pc
), strerror (err
));
672 reinsert_fast_tracepoint_jumps_at (CORE_ADDR where
)
674 struct fast_tracepoint_jump
*jp
;
678 jp
= find_fast_tracepoint_jump_at (where
);
681 /* This can happen when we remove breakpoints when a tracepoint
682 hit causes a tracing stop, while handling a step-over. */
684 debug_printf ("Could not find fast tracepoint jump at 0x%s "
685 "in list (reinserting).\n",
691 error ("Jump already inserted at reinsert time.");
695 /* Since there can be trap breakpoints inserted in the same address
696 range, we use `write_inferior_memory', which takes care of
697 layering breakpoints on top of fast tracepoints, and on top of
698 the buffer we pass it. This works because we've already marked
699 the fast tracepoint jump inserted above. Also note that we need
700 to pass the current shadow contents, because
701 write_inferior_memory updates any shadow memory with what we pass
702 here, and we want that to be a nop. */
703 buf
= alloca (jp
->length
);
704 memcpy (buf
, fast_tracepoint_jump_shadow (jp
), jp
->length
);
705 err
= write_inferior_memory (where
, buf
, jp
->length
);
711 debug_printf ("Failed to reinsert fast tracepoint jump at"
713 paddress (where
), strerror (err
));
717 /* Set a high-level breakpoint of type TYPE, with low level type
718 RAW_TYPE and size SIZE, at WHERE. On success, a pointer to the new
719 breakpoint is returned. On failure, returns NULL and writes the
720 error code to *ERR. HANDLER is called when the breakpoint is hit.
721 HANDLER should return 1 if the breakpoint should be deleted, 0
724 static struct breakpoint
*
725 set_breakpoint (enum bkpt_type type
, enum raw_bkpt_type raw_type
,
726 CORE_ADDR where
, int size
,
727 int (*handler
) (CORE_ADDR
), int *err
)
729 struct process_info
*proc
= current_process ();
730 struct breakpoint
*bp
;
731 struct raw_breakpoint
*raw
;
733 raw
= set_raw_breakpoint_at (raw_type
, where
, size
, err
);
741 bp
= xcalloc (1, sizeof (struct breakpoint
));
745 bp
->handler
= handler
;
747 bp
->next
= proc
->breakpoints
;
748 proc
->breakpoints
= bp
;
753 /* See mem-break.h */
756 set_breakpoint_at (CORE_ADDR where
, int (*handler
) (CORE_ADDR
))
760 return set_breakpoint (other_breakpoint
, raw_bkpt_type_sw
,
761 where
, breakpoint_len
, handler
,
767 delete_raw_breakpoint (struct process_info
*proc
, struct raw_breakpoint
*todel
)
769 struct raw_breakpoint
*bp
, **bp_link
;
772 bp
= proc
->raw_breakpoints
;
773 bp_link
= &proc
->raw_breakpoints
;
779 if (bp
->inserted
> 0)
781 struct raw_breakpoint
*prev_bp_link
= *bp_link
;
785 ret
= the_target
->remove_point (bp
->raw_type
, bp
->pc
, bp
->size
,
789 /* Something went wrong, relink the breakpoint. */
790 *bp_link
= prev_bp_link
;
793 debug_printf ("Failed to uninsert raw breakpoint "
794 "at 0x%s while deleting it.\n",
812 warning ("Could not find raw breakpoint in list.");
817 release_breakpoint (struct process_info
*proc
, struct breakpoint
*bp
)
822 newrefcount
= bp
->raw
->refcount
- 1;
823 if (newrefcount
== 0)
825 ret
= delete_raw_breakpoint (proc
, bp
->raw
);
830 bp
->raw
->refcount
= newrefcount
;
838 delete_breakpoint_1 (struct process_info
*proc
, struct breakpoint
*todel
)
840 struct breakpoint
*bp
, **bp_link
;
843 bp
= proc
->breakpoints
;
844 bp_link
= &proc
->breakpoints
;
852 err
= release_breakpoint (proc
, bp
);
866 warning ("Could not find breakpoint in list.");
871 delete_breakpoint (struct breakpoint
*todel
)
873 struct process_info
*proc
= current_process ();
874 return delete_breakpoint_1 (proc
, todel
);
877 /* Locate a GDB breakpoint of type Z_TYPE and size SIZE placed at
878 address ADDR and return a pointer to its structure. If SIZE is -1,
879 the breakpoints' sizes are ignored. */
881 static struct breakpoint
*
882 find_gdb_breakpoint (char z_type
, CORE_ADDR addr
, int size
)
884 struct process_info
*proc
= current_process ();
885 struct breakpoint
*bp
;
886 enum bkpt_type type
= Z_packet_to_bkpt_type (z_type
);
888 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
889 if (bp
->type
== type
&& bp
->raw
->pc
== addr
890 && (size
== -1 || bp
->raw
->size
== size
))
897 z_type_supported (char z_type
)
899 return (z_type
>= '0' && z_type
<= '4'
900 && the_target
->supports_z_point_type (z_type
));
903 /* Create a new GDB breakpoint of type Z_TYPE at ADDR with size SIZE.
904 Returns a pointer to the newly created breakpoint on success. On
905 failure returns NULL and sets *ERR to either -1 for error, or 1 if
906 Z_TYPE breakpoints are not supported on this target. */
908 static struct breakpoint
*
909 set_gdb_breakpoint_1 (char z_type
, CORE_ADDR addr
, int size
, int *err
)
911 struct breakpoint
*bp
;
913 enum raw_bkpt_type raw_type
;
915 /* If we see GDB inserting a second code breakpoint at the same
916 address, then either: GDB is updating the breakpoint's conditions
917 or commands; or, the first breakpoint must have disappeared due
918 to a shared library unload. On targets where the shared
919 libraries are handled by userspace, like SVR4, for example,
920 GDBserver can't tell if a library was loaded or unloaded. Since
921 we refcount raw breakpoints, we must be careful to make sure GDB
922 breakpoints never contribute more than one reference. if we
923 didn't do this, in case the previous breakpoint is gone due to a
924 shared library unload, we'd just increase the refcount of the
925 previous breakpoint at this address, but the trap was not planted
926 in the inferior anymore, thus the breakpoint would never be hit.
927 Note this must be careful to not create a window where
928 breakpoints are removed from the target, for non-stop, in case
929 the target can poke at memory while the program is running. */
930 if (z_type
== Z_PACKET_SW_BP
931 || z_type
== Z_PACKET_HW_BP
)
933 bp
= find_gdb_breakpoint (z_type
, addr
, -1);
937 if (bp
->raw
->size
!= size
)
939 /* A different size than previously seen. The previous
940 breakpoint must be gone then. */
941 bp
->raw
->inserted
= -1;
942 delete_breakpoint (bp
);
945 else if (z_type
== Z_PACKET_SW_BP
)
947 /* Check if the breakpoint is actually gone from the
948 target, due to an solib unload, for example. Might
949 as well validate _all_ breakpoints. */
950 validate_breakpoints ();
952 /* Breakpoints that don't pass validation are
954 bp
= find_gdb_breakpoint (z_type
, addr
, -1);
960 /* Data breakpoints for the same address but different size are
961 expected. GDB doesn't merge these. The backend gets to do
962 that if it wants/can. */
963 bp
= find_gdb_breakpoint (z_type
, addr
, size
);
968 /* We already know about this breakpoint, there's nothing else
969 to do - GDB's reference is already accounted for. Note that
970 whether the breakpoint inserted is left as is - we may be
971 stepping over it, for example, in which case we don't want to
972 force-reinsert it. */
976 raw_type
= Z_packet_to_raw_bkpt_type (z_type
);
977 type
= Z_packet_to_bkpt_type (z_type
);
978 return set_breakpoint (type
, raw_type
, addr
, size
, NULL
, err
);
982 check_gdb_bp_preconditions (char z_type
, int *err
)
984 /* As software/memory breakpoints work by poking at memory, we need
985 to prepare to access memory. If that operation fails, we need to
986 return error. Seeing an error, if this is the first breakpoint
987 of that type that GDB tries to insert, GDB would then assume the
988 breakpoint type is supported, but it may actually not be. So we
989 need to check whether the type is supported at all before
990 preparing to access memory. */
991 if (!z_type_supported (z_type
))
996 else if (current_inferior
== NULL
)
1005 /* See mem-break.h. This is a wrapper for set_gdb_breakpoint_1 that
1006 knows to prepare to access memory for Z0 breakpoints. */
1009 set_gdb_breakpoint (char z_type
, CORE_ADDR addr
, int size
, int *err
)
1011 struct breakpoint
*bp
;
1013 if (!check_gdb_bp_preconditions (z_type
, err
))
1016 /* If inserting a software/memory breakpoint, need to prepare to
1018 if (z_type
== Z_PACKET_SW_BP
)
1020 *err
= prepare_to_access_memory ();
1025 bp
= set_gdb_breakpoint_1 (z_type
, addr
, size
, err
);
1027 if (z_type
== Z_PACKET_SW_BP
)
1028 done_accessing_memory ();
1033 /* Delete a GDB breakpoint of type Z_TYPE and size SIZE previously
1034 inserted at ADDR with set_gdb_breakpoint_at. Returns 0 on success,
1035 -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
1039 delete_gdb_breakpoint_1 (char z_type
, CORE_ADDR addr
, int size
)
1041 struct breakpoint
*bp
;
1044 bp
= find_gdb_breakpoint (z_type
, addr
, size
);
1048 /* Before deleting the breakpoint, make sure to free
1049 its condition list. */
1050 clear_breakpoint_conditions (bp
);
1051 err
= delete_breakpoint (bp
);
1058 /* See mem-break.h. This is a wrapper for delete_gdb_breakpoint that
1059 knows to prepare to access memory for Z0 breakpoints. */
1062 delete_gdb_breakpoint (char z_type
, CORE_ADDR addr
, int size
)
1066 if (!check_gdb_bp_preconditions (z_type
, &ret
))
1069 /* If inserting a software/memory breakpoint, need to prepare to
1071 if (z_type
== Z_PACKET_SW_BP
)
1075 err
= prepare_to_access_memory ();
1080 ret
= delete_gdb_breakpoint_1 (z_type
, addr
, size
);
1082 if (z_type
== Z_PACKET_SW_BP
)
1083 done_accessing_memory ();
1088 /* Clear all conditions associated with a breakpoint. */
1091 clear_breakpoint_conditions (struct breakpoint
*bp
)
1093 struct point_cond_list
*cond
;
1095 if (bp
->cond_list
== NULL
)
1098 cond
= bp
->cond_list
;
1100 while (cond
!= NULL
)
1102 struct point_cond_list
*cond_next
;
1104 cond_next
= cond
->next
;
1105 free (cond
->cond
->bytes
);
1111 bp
->cond_list
= NULL
;
1114 /* Add condition CONDITION to GDBserver's breakpoint BP. */
1117 add_condition_to_breakpoint (struct breakpoint
*bp
,
1118 struct agent_expr
*condition
)
1120 struct point_cond_list
*new_cond
;
1122 /* Create new condition. */
1123 new_cond
= xcalloc (1, sizeof (*new_cond
));
1124 new_cond
->cond
= condition
;
1126 /* Add condition to the list. */
1127 new_cond
->next
= bp
->cond_list
;
1128 bp
->cond_list
= new_cond
;
1131 /* Add a target-side condition CONDITION to a breakpoint. */
1134 add_breakpoint_condition (struct breakpoint
*bp
, char **condition
)
1136 char *actparm
= *condition
;
1137 struct agent_expr
*cond
;
1139 if (condition
== NULL
)
1145 cond
= gdb_parse_agent_expr (&actparm
);
1149 fprintf (stderr
, "Condition evaluation failed. "
1150 "Assuming unconditional.\n");
1154 add_condition_to_breakpoint (bp
, cond
);
1156 *condition
= actparm
;
1161 /* Evaluate condition (if any) at breakpoint BP. Return 1 if
1162 true and 0 otherwise. */
1165 gdb_condition_true_at_breakpoint_z_type (char z_type
, CORE_ADDR addr
)
1167 /* Fetch registers for the current inferior. */
1168 struct breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1170 struct point_cond_list
*cl
;
1172 struct eval_agent_expr_context ctx
;
1177 /* Check if the breakpoint is unconditional. If it is,
1178 the condition always evaluates to TRUE. */
1179 if (bp
->cond_list
== NULL
)
1182 ctx
.regcache
= get_thread_regcache (current_inferior
, 1);
1186 /* Evaluate each condition in the breakpoint's list of conditions.
1187 Return true if any of the conditions evaluates to TRUE.
1189 If we failed to evaluate the expression, TRUE is returned. This
1190 forces GDB to reevaluate the conditions. */
1191 for (cl
= bp
->cond_list
;
1192 cl
&& !value
&& !err
; cl
= cl
->next
)
1194 /* Evaluate the condition. */
1195 err
= gdb_eval_agent_expr (&ctx
, cl
->cond
, &value
);
1201 return (value
!= 0);
1205 gdb_condition_true_at_breakpoint (CORE_ADDR where
)
1207 /* Only check code (software or hardware) breakpoints. */
1208 return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP
, where
)
1209 || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP
, where
));
1212 /* Add commands COMMANDS to GDBserver's breakpoint BP. */
1215 add_commands_to_breakpoint (struct breakpoint
*bp
,
1216 struct agent_expr
*commands
, int persist
)
1218 struct point_command_list
*new_cmd
;
1220 /* Create new command. */
1221 new_cmd
= xcalloc (1, sizeof (*new_cmd
));
1222 new_cmd
->cmd
= commands
;
1223 new_cmd
->persistence
= persist
;
1225 /* Add commands to the list. */
1226 new_cmd
->next
= bp
->command_list
;
1227 bp
->command_list
= new_cmd
;
1230 /* Add a target-side command COMMAND to the breakpoint at ADDR. */
1233 add_breakpoint_commands (struct breakpoint
*bp
, char **command
,
1236 char *actparm
= *command
;
1237 struct agent_expr
*cmd
;
1239 if (command
== NULL
)
1245 cmd
= gdb_parse_agent_expr (&actparm
);
1249 fprintf (stderr
, "Command evaluation failed. "
1254 add_commands_to_breakpoint (bp
, cmd
, persist
);
1261 /* Return true if there are no commands to run at this location,
1262 which likely means we want to report back to GDB. */
1265 gdb_no_commands_at_breakpoint_z_type (char z_type
, CORE_ADDR addr
)
1267 struct breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1273 debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s\n",
1274 paddress (addr
), z_type
,
1275 phex_nz ((uintptr_t) bp
->command_list
, 0));
1276 return (bp
->command_list
== NULL
);
1279 /* Return true if there are no commands to run at this location,
1280 which likely means we want to report back to GDB. */
1283 gdb_no_commands_at_breakpoint (CORE_ADDR where
)
1285 /* Only check code (software or hardware) breakpoints. */
1286 return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP
, where
)
1287 && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP
, where
));
1290 /* Run a breakpoint's commands. Returns 0 if there was a problem
1291 running any command, 1 otherwise. */
1294 run_breakpoint_commands_z_type (char z_type
, CORE_ADDR addr
)
1296 /* Fetch registers for the current inferior. */
1297 struct breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1299 struct point_command_list
*cl
;
1301 struct eval_agent_expr_context ctx
;
1306 ctx
.regcache
= get_thread_regcache (current_inferior
, 1);
1310 for (cl
= bp
->command_list
;
1311 cl
&& !value
&& !err
; cl
= cl
->next
)
1313 /* Run the command. */
1314 err
= gdb_eval_agent_expr (&ctx
, cl
->cmd
, &value
);
1316 /* If one command has a problem, stop digging the hole deeper. */
1325 run_breakpoint_commands (CORE_ADDR where
)
1327 /* Only check code (software or hardware) breakpoints. If one
1328 command has a problem, stop digging the hole deeper. */
1329 if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP
, where
))
1330 run_breakpoint_commands_z_type (Z_PACKET_HW_BP
, where
);
1333 /* See mem-break.h. */
1336 gdb_breakpoint_here (CORE_ADDR where
)
1338 /* Only check code (software or hardware) breakpoints. */
1339 return (find_gdb_breakpoint (Z_PACKET_SW_BP
, where
, -1) != NULL
1340 || find_gdb_breakpoint (Z_PACKET_HW_BP
, where
, -1) != NULL
);
1344 set_reinsert_breakpoint (CORE_ADDR stop_at
)
1346 struct breakpoint
*bp
;
1348 bp
= set_breakpoint_at (stop_at
, NULL
);
1349 bp
->type
= reinsert_breakpoint
;
1353 delete_reinsert_breakpoints (void)
1355 struct process_info
*proc
= current_process ();
1356 struct breakpoint
*bp
, **bp_link
;
1358 bp
= proc
->breakpoints
;
1359 bp_link
= &proc
->breakpoints
;
1363 if (bp
->type
== reinsert_breakpoint
)
1365 *bp_link
= bp
->next
;
1366 release_breakpoint (proc
, bp
);
1371 bp_link
= &bp
->next
;
1378 uninsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1380 if (bp
->inserted
< 0)
1383 debug_printf ("Breakpoint at %s is marked insert-disabled.\n",
1386 else if (bp
->inserted
> 0)
1392 err
= the_target
->remove_point (bp
->raw_type
, bp
->pc
, bp
->size
, bp
);
1398 debug_printf ("Failed to uninsert raw breakpoint at 0x%s.\n",
1405 uninsert_breakpoints_at (CORE_ADDR pc
)
1407 struct process_info
*proc
= current_process ();
1408 struct raw_breakpoint
*bp
;
1411 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1412 if ((bp
->raw_type
== raw_bkpt_type_sw
1413 || bp
->raw_type
== raw_bkpt_type_hw
)
1419 uninsert_raw_breakpoint (bp
);
1424 /* This can happen when we remove all breakpoints while handling
1427 debug_printf ("Could not find breakpoint at 0x%s "
1428 "in list (uninserting).\n",
1434 uninsert_all_breakpoints (void)
1436 struct process_info
*proc
= current_process ();
1437 struct raw_breakpoint
*bp
;
1439 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1440 if ((bp
->raw_type
== raw_bkpt_type_sw
1441 || bp
->raw_type
== raw_bkpt_type_hw
)
1443 uninsert_raw_breakpoint (bp
);
1447 reinsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1452 error ("Breakpoint already inserted at reinsert time.");
1454 err
= the_target
->insert_point (bp
->raw_type
, bp
->pc
, bp
->size
, bp
);
1457 else if (debug_threads
)
1458 debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).\n",
1459 paddress (bp
->pc
), err
);
1463 reinsert_breakpoints_at (CORE_ADDR pc
)
1465 struct process_info
*proc
= current_process ();
1466 struct raw_breakpoint
*bp
;
1469 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1470 if ((bp
->raw_type
== raw_bkpt_type_sw
1471 || bp
->raw_type
== raw_bkpt_type_hw
)
1476 reinsert_raw_breakpoint (bp
);
1481 /* This can happen when we remove all breakpoints while handling
1484 debug_printf ("Could not find raw breakpoint at 0x%s "
1485 "in list (reinserting).\n",
1491 reinsert_all_breakpoints (void)
1493 struct process_info
*proc
= current_process ();
1494 struct raw_breakpoint
*bp
;
1496 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1497 if ((bp
->raw_type
== raw_bkpt_type_sw
1498 || bp
->raw_type
== raw_bkpt_type_hw
)
1500 reinsert_raw_breakpoint (bp
);
1504 check_breakpoints (CORE_ADDR stop_pc
)
1506 struct process_info
*proc
= current_process ();
1507 struct breakpoint
*bp
, **bp_link
;
1509 bp
= proc
->breakpoints
;
1510 bp_link
= &proc
->breakpoints
;
1514 struct raw_breakpoint
*raw
= bp
->raw
;
1516 if ((raw
->raw_type
== raw_bkpt_type_sw
1517 || raw
->raw_type
== raw_bkpt_type_hw
)
1518 && raw
->pc
== stop_pc
)
1522 warning ("Hit a removed breakpoint?");
1526 if (bp
->handler
!= NULL
&& (*bp
->handler
) (stop_pc
))
1528 *bp_link
= bp
->next
;
1530 release_breakpoint (proc
, bp
);
1537 bp_link
= &bp
->next
;
1543 set_breakpoint_data (const unsigned char *bp_data
, int bp_len
)
1545 breakpoint_data
= bp_data
;
1546 breakpoint_len
= bp_len
;
1550 breakpoint_here (CORE_ADDR addr
)
1552 struct process_info
*proc
= current_process ();
1553 struct raw_breakpoint
*bp
;
1555 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1556 if ((bp
->raw_type
== raw_bkpt_type_sw
1557 || bp
->raw_type
== raw_bkpt_type_hw
)
1565 breakpoint_inserted_here (CORE_ADDR addr
)
1567 struct process_info
*proc
= current_process ();
1568 struct raw_breakpoint
*bp
;
1570 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1571 if ((bp
->raw_type
== raw_bkpt_type_sw
1572 || bp
->raw_type
== raw_bkpt_type_hw
)
1581 validate_inserted_breakpoint (struct raw_breakpoint
*bp
)
1586 gdb_assert (bp
->inserted
);
1587 gdb_assert (bp
->raw_type
== raw_bkpt_type_sw
);
1589 buf
= alloca (breakpoint_len
);
1590 err
= (*the_target
->read_memory
) (bp
->pc
, buf
, breakpoint_len
);
1591 if (err
|| memcmp (buf
, breakpoint_data
, breakpoint_len
) != 0)
1593 /* Tag it as gone. */
1602 delete_disabled_breakpoints (void)
1604 struct process_info
*proc
= current_process ();
1605 struct breakpoint
*bp
, *next
;
1607 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= next
)
1610 if (bp
->raw
->inserted
< 0)
1611 delete_breakpoint_1 (proc
, bp
);
1615 /* Check if breakpoints we inserted still appear to be inserted. They
1616 may disappear due to a shared library unload, and worse, a new
1617 shared library may be reloaded at the same address as the
1618 previously unloaded one. If that happens, we should make sure that
1619 the shadow memory of the old breakpoints isn't used when reading or
1623 validate_breakpoints (void)
1625 struct process_info
*proc
= current_process ();
1626 struct breakpoint
*bp
;
1628 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1630 struct raw_breakpoint
*raw
= bp
->raw
;
1632 if (raw
->raw_type
== raw_bkpt_type_sw
&& raw
->inserted
> 0)
1633 validate_inserted_breakpoint (raw
);
1636 delete_disabled_breakpoints ();
1640 check_mem_read (CORE_ADDR mem_addr
, unsigned char *buf
, int mem_len
)
1642 struct process_info
*proc
= current_process ();
1643 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
1644 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
1645 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
1646 int disabled_one
= 0;
1648 for (; jp
!= NULL
; jp
= jp
->next
)
1650 CORE_ADDR bp_end
= jp
->pc
+ jp
->length
;
1651 CORE_ADDR start
, end
;
1652 int copy_offset
, copy_len
, buf_offset
;
1654 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= buf
+ mem_len
1655 || buf
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
1657 if (mem_addr
>= bp_end
)
1659 if (jp
->pc
>= mem_end
)
1663 if (mem_addr
> start
)
1670 copy_len
= end
- start
;
1671 copy_offset
= start
- jp
->pc
;
1672 buf_offset
= start
- mem_addr
;
1675 memcpy (buf
+ buf_offset
,
1676 fast_tracepoint_jump_shadow (jp
) + copy_offset
,
1680 for (; bp
!= NULL
; bp
= bp
->next
)
1682 CORE_ADDR bp_end
= bp
->pc
+ breakpoint_len
;
1683 CORE_ADDR start
, end
;
1684 int copy_offset
, copy_len
, buf_offset
;
1686 if (bp
->raw_type
!= raw_bkpt_type_sw
)
1689 gdb_assert (bp
->old_data
>= buf
+ mem_len
1690 || buf
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
1692 if (mem_addr
>= bp_end
)
1694 if (bp
->pc
>= mem_end
)
1698 if (mem_addr
> start
)
1705 copy_len
= end
- start
;
1706 copy_offset
= start
- bp
->pc
;
1707 buf_offset
= start
- mem_addr
;
1709 if (bp
->inserted
> 0)
1711 if (validate_inserted_breakpoint (bp
))
1712 memcpy (buf
+ buf_offset
, bp
->old_data
+ copy_offset
, copy_len
);
1719 delete_disabled_breakpoints ();
1723 check_mem_write (CORE_ADDR mem_addr
, unsigned char *buf
,
1724 const unsigned char *myaddr
, int mem_len
)
1726 struct process_info
*proc
= current_process ();
1727 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
1728 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
1729 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
1730 int disabled_one
= 0;
1732 /* First fast tracepoint jumps, then breakpoint traps on top. */
1734 for (; jp
!= NULL
; jp
= jp
->next
)
1736 CORE_ADDR jp_end
= jp
->pc
+ jp
->length
;
1737 CORE_ADDR start
, end
;
1738 int copy_offset
, copy_len
, buf_offset
;
1740 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= myaddr
+ mem_len
1741 || myaddr
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
1742 gdb_assert (fast_tracepoint_jump_insn (jp
) >= buf
+ mem_len
1743 || buf
>= fast_tracepoint_jump_insn (jp
) + (jp
)->length
);
1745 if (mem_addr
>= jp_end
)
1747 if (jp
->pc
>= mem_end
)
1751 if (mem_addr
> start
)
1758 copy_len
= end
- start
;
1759 copy_offset
= start
- jp
->pc
;
1760 buf_offset
= start
- mem_addr
;
1762 memcpy (fast_tracepoint_jump_shadow (jp
) + copy_offset
,
1763 myaddr
+ buf_offset
, copy_len
);
1765 memcpy (buf
+ buf_offset
,
1766 fast_tracepoint_jump_insn (jp
) + copy_offset
, copy_len
);
1769 for (; bp
!= NULL
; bp
= bp
->next
)
1771 CORE_ADDR bp_end
= bp
->pc
+ breakpoint_len
;
1772 CORE_ADDR start
, end
;
1773 int copy_offset
, copy_len
, buf_offset
;
1775 if (bp
->raw_type
!= raw_bkpt_type_sw
)
1778 gdb_assert (bp
->old_data
>= myaddr
+ mem_len
1779 || myaddr
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
1781 if (mem_addr
>= bp_end
)
1783 if (bp
->pc
>= mem_end
)
1787 if (mem_addr
> start
)
1794 copy_len
= end
- start
;
1795 copy_offset
= start
- bp
->pc
;
1796 buf_offset
= start
- mem_addr
;
1798 memcpy (bp
->old_data
+ copy_offset
, myaddr
+ buf_offset
, copy_len
);
1799 if (bp
->inserted
> 0)
1801 if (validate_inserted_breakpoint (bp
))
1802 memcpy (buf
+ buf_offset
, breakpoint_data
+ copy_offset
, copy_len
);
1809 delete_disabled_breakpoints ();
1812 /* Delete all breakpoints, and un-insert them from the inferior. */
1815 delete_all_breakpoints (void)
1817 struct process_info
*proc
= current_process ();
1819 while (proc
->breakpoints
)
1820 delete_breakpoint_1 (proc
, proc
->breakpoints
);
1823 /* Clear the "inserted" flag in all breakpoints. */
1826 mark_breakpoints_out (struct process_info
*proc
)
1828 struct raw_breakpoint
*raw_bp
;
1830 for (raw_bp
= proc
->raw_breakpoints
; raw_bp
!= NULL
; raw_bp
= raw_bp
->next
)
1831 raw_bp
->inserted
= 0;
1834 /* Release all breakpoints, but do not try to un-insert them from the
1838 free_all_breakpoints (struct process_info
*proc
)
1840 mark_breakpoints_out (proc
);
1842 /* Note: use PROC explicitly instead of deferring to
1843 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
1844 released when we get here. There should be no call to
1845 current_process from here on. */
1846 while (proc
->breakpoints
)
1847 delete_breakpoint_1 (proc
, proc
->breakpoints
);