1 /* Memory breakpoint operations for the remote server for GDB.
2 Copyright (C) 2002-2018 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 (void)
308 struct process_info
*proc
= current_process ();
309 struct breakpoint
*bp
;
310 struct point_command_list
*cl
;
312 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
314 if (is_gdb_breakpoint (bp
->type
))
316 struct gdb_breakpoint
*gdb_bp
= (struct gdb_breakpoint
*) bp
;
318 for (cl
= gdb_bp
->command_list
; cl
!= NULL
; cl
= cl
->next
)
327 /* Find low-level breakpoint of type TYPE at address ADDR that is not
328 insert-disabled. Returns NULL if not found. */
330 static struct raw_breakpoint
*
331 find_enabled_raw_code_breakpoint_at (CORE_ADDR addr
, enum raw_bkpt_type type
)
333 struct process_info
*proc
= current_process ();
334 struct raw_breakpoint
*bp
;
336 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
338 && bp
->raw_type
== type
339 && bp
->inserted
>= 0)
345 /* Find low-level breakpoint of type TYPE at address ADDR. Returns
346 NULL if not found. */
348 static struct raw_breakpoint
*
349 find_raw_breakpoint_at (CORE_ADDR addr
, enum raw_bkpt_type type
, int kind
)
351 struct process_info
*proc
= current_process ();
352 struct raw_breakpoint
*bp
;
354 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
355 if (bp
->pc
== addr
&& bp
->raw_type
== type
&& bp
->kind
== kind
)
361 /* See mem-break.h. */
364 insert_memory_breakpoint (struct raw_breakpoint
*bp
)
366 unsigned char buf
[MAX_BREAKPOINT_LEN
];
369 /* Note that there can be fast tracepoint jumps installed in the
370 same memory range, so to get at the original memory, we need to
371 use read_inferior_memory, which masks those out. */
372 err
= read_inferior_memory (bp
->pc
, buf
, bp_size (bp
));
376 debug_printf ("Failed to read shadow memory of"
377 " breakpoint at 0x%s (%s).\n",
378 paddress (bp
->pc
), strerror (err
));
382 memcpy (bp
->old_data
, buf
, bp_size (bp
));
384 err
= (*the_target
->write_memory
) (bp
->pc
, bp_opcode (bp
),
389 debug_printf ("Failed to insert breakpoint at 0x%s (%s).\n",
390 paddress (bp
->pc
), strerror (err
));
393 return err
!= 0 ? -1 : 0;
396 /* See mem-break.h */
399 remove_memory_breakpoint (struct raw_breakpoint
*bp
)
401 unsigned char buf
[MAX_BREAKPOINT_LEN
];
404 /* Since there can be trap breakpoints inserted in the same address
405 range, we use `write_inferior_memory', which takes care of
406 layering breakpoints on top of fast tracepoints, and on top of
407 the buffer we pass it. This works because the caller has already
408 either unlinked the breakpoint or marked it uninserted. Also
409 note that we need to pass the current shadow contents, because
410 write_inferior_memory updates any shadow memory with what we pass
411 here, and we want that to be a nop. */
412 memcpy (buf
, bp
->old_data
, bp_size (bp
));
413 err
= write_inferior_memory (bp
->pc
, buf
, bp_size (bp
));
417 debug_printf ("Failed to uninsert raw breakpoint "
418 "at 0x%s (%s) while deleting it.\n",
419 paddress (bp
->pc
), strerror (err
));
421 return err
!= 0 ? -1 : 0;
424 /* Set a RAW breakpoint of type TYPE and kind KIND at WHERE. On
425 success, a pointer to the new breakpoint is returned. On failure,
426 returns NULL and writes the error code to *ERR. */
428 static struct raw_breakpoint
*
429 set_raw_breakpoint_at (enum raw_bkpt_type type
, CORE_ADDR where
, int kind
,
432 struct process_info
*proc
= current_process ();
433 struct raw_breakpoint
*bp
;
435 if (type
== raw_bkpt_type_sw
|| type
== raw_bkpt_type_hw
)
437 bp
= find_enabled_raw_code_breakpoint_at (where
, type
);
438 if (bp
!= NULL
&& bp
->kind
!= kind
)
440 /* A different kind than previously seen. The previous
441 breakpoint must be gone then. */
443 debug_printf ("Inconsistent breakpoint kind? Was %d, now %d.\n",
450 bp
= find_raw_breakpoint_at (where
, type
, kind
);
452 gdb::unique_xmalloc_ptr
<struct raw_breakpoint
> bp_holder
;
455 bp_holder
.reset (XCNEW (struct raw_breakpoint
));
456 bp
= bp_holder
.get ();
464 *err
= the_target
->insert_point (bp
->raw_type
, bp
->pc
, bp
->kind
, bp
);
468 debug_printf ("Failed to insert breakpoint at 0x%s (%d).\n",
469 paddress (where
), *err
);
477 /* If the breakpoint was allocated above, we know we want to keep it
479 bp_holder
.release ();
481 /* Link the breakpoint in, if this is the first reference. */
482 if (++bp
->refcount
== 1)
484 bp
->next
= proc
->raw_breakpoints
;
485 proc
->raw_breakpoints
= bp
;
490 /* Notice that breakpoint traps are always installed on top of fast
491 tracepoint jumps. This is even if the fast tracepoint is installed
492 at a later time compared to when the breakpoint was installed.
493 This means that a stopping breakpoint or tracepoint has higher
494 "priority". In turn, this allows having fast and slow tracepoints
495 (and breakpoints) at the same address behave correctly. */
498 /* A fast tracepoint jump. */
500 struct fast_tracepoint_jump
502 struct fast_tracepoint_jump
*next
;
504 /* A reference count. GDB can install more than one fast tracepoint
505 at the same address (each with its own action list, for
509 /* The fast tracepoint's insertion address. There can only be one
510 of these for a given PC. */
513 /* Non-zero if this fast tracepoint jump is currently inserted in
517 /* The length of the jump instruction. */
520 /* A poor-man's flexible array member, holding both the jump
521 instruction to insert, and a copy of the instruction that would
522 be in memory had not been a jump there (the shadow memory of the
524 unsigned char insn_and_shadow
[0];
527 /* Fast tracepoint FP's jump instruction to insert. */
528 #define fast_tracepoint_jump_insn(fp) \
529 ((fp)->insn_and_shadow + 0)
531 /* The shadow memory of fast tracepoint jump FP. */
532 #define fast_tracepoint_jump_shadow(fp) \
533 ((fp)->insn_and_shadow + (fp)->length)
536 /* Return the fast tracepoint jump set at WHERE. */
538 static struct fast_tracepoint_jump
*
539 find_fast_tracepoint_jump_at (CORE_ADDR where
)
541 struct process_info
*proc
= current_process ();
542 struct fast_tracepoint_jump
*jp
;
544 for (jp
= proc
->fast_tracepoint_jumps
; jp
!= NULL
; jp
= jp
->next
)
552 fast_tracepoint_jump_here (CORE_ADDR where
)
554 struct fast_tracepoint_jump
*jp
= find_fast_tracepoint_jump_at (where
);
560 delete_fast_tracepoint_jump (struct fast_tracepoint_jump
*todel
)
562 struct fast_tracepoint_jump
*bp
, **bp_link
;
564 struct process_info
*proc
= current_process ();
566 bp
= proc
->fast_tracepoint_jumps
;
567 bp_link
= &proc
->fast_tracepoint_jumps
;
573 if (--bp
->refcount
== 0)
575 struct fast_tracepoint_jump
*prev_bp_link
= *bp_link
;
581 /* Since there can be breakpoints inserted in the same
582 address range, we use `write_inferior_memory', which
583 takes care of layering breakpoints on top of fast
584 tracepoints, and on top of the buffer we pass it.
585 This works because we've already unlinked the fast
586 tracepoint jump above. Also note that we need to
587 pass the current shadow contents, because
588 write_inferior_memory updates any shadow memory with
589 what we pass here, and we want that to be a nop. */
590 buf
= (unsigned char *) alloca (bp
->length
);
591 memcpy (buf
, fast_tracepoint_jump_shadow (bp
), bp
->length
);
592 ret
= write_inferior_memory (bp
->pc
, buf
, bp
->length
);
595 /* Something went wrong, relink the jump. */
596 *bp_link
= prev_bp_link
;
599 debug_printf ("Failed to uninsert fast tracepoint jump "
600 "at 0x%s (%s) while deleting it.\n",
601 paddress (bp
->pc
), strerror (ret
));
617 warning ("Could not find fast tracepoint jump in list.");
622 inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump
*jp
)
627 struct fast_tracepoint_jump
*
628 set_fast_tracepoint_jump (CORE_ADDR where
,
629 unsigned char *insn
, ULONGEST length
)
631 struct process_info
*proc
= current_process ();
632 struct fast_tracepoint_jump
*jp
;
636 /* We refcount fast tracepoint jumps. Check if we already know
637 about a jump at this address. */
638 jp
= find_fast_tracepoint_jump_at (where
);
645 /* We don't, so create a new object. Double the length, because the
646 flexible array member holds both the jump insn, and the
648 jp
= (struct fast_tracepoint_jump
*) xcalloc (1, sizeof (*jp
) + (length
* 2));
651 memcpy (fast_tracepoint_jump_insn (jp
), insn
, length
);
653 buf
= (unsigned char *) alloca (length
);
655 /* Note that there can be trap breakpoints inserted in the same
656 address range. To access the original memory contents, we use
657 `read_inferior_memory', which masks out breakpoints. */
658 err
= read_inferior_memory (where
, buf
, length
);
662 debug_printf ("Failed to read shadow memory of"
663 " fast tracepoint at 0x%s (%s).\n",
664 paddress (where
), strerror (err
));
668 memcpy (fast_tracepoint_jump_shadow (jp
), buf
, length
);
670 /* Link the jump in. */
672 jp
->next
= proc
->fast_tracepoint_jumps
;
673 proc
->fast_tracepoint_jumps
= jp
;
675 /* Since there can be trap breakpoints inserted in the same address
676 range, we use use `write_inferior_memory', which takes care of
677 layering breakpoints on top of fast tracepoints, on top of the
678 buffer we pass it. This works because we've already linked in
679 the fast tracepoint jump above. Also note that we need to pass
680 the current shadow contents, because write_inferior_memory
681 updates any shadow memory with what we pass here, and we want
683 err
= write_inferior_memory (where
, buf
, length
);
687 debug_printf ("Failed to insert fast tracepoint jump at 0x%s (%s).\n",
688 paddress (where
), strerror (err
));
691 proc
->fast_tracepoint_jumps
= jp
->next
;
701 uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc
)
703 struct fast_tracepoint_jump
*jp
;
706 jp
= find_fast_tracepoint_jump_at (pc
);
709 /* This can happen when we remove all breakpoints while handling
712 debug_printf ("Could not find fast tracepoint jump at 0x%s "
713 "in list (uninserting).\n",
724 /* Since there can be trap breakpoints inserted in the same
725 address range, we use use `write_inferior_memory', which
726 takes care of layering breakpoints on top of fast
727 tracepoints, and on top of the buffer we pass it. This works
728 because we've already marked the fast tracepoint fast
729 tracepoint jump uninserted above. Also note that we need to
730 pass the current shadow contents, because
731 write_inferior_memory updates any shadow memory with what we
732 pass here, and we want that to be a nop. */
733 buf
= (unsigned char *) alloca (jp
->length
);
734 memcpy (buf
, fast_tracepoint_jump_shadow (jp
), jp
->length
);
735 err
= write_inferior_memory (jp
->pc
, buf
, jp
->length
);
741 debug_printf ("Failed to uninsert fast tracepoint jump at"
743 paddress (pc
), strerror (err
));
749 reinsert_fast_tracepoint_jumps_at (CORE_ADDR where
)
751 struct fast_tracepoint_jump
*jp
;
755 jp
= find_fast_tracepoint_jump_at (where
);
758 /* This can happen when we remove breakpoints when a tracepoint
759 hit causes a tracing stop, while handling a step-over. */
761 debug_printf ("Could not find fast tracepoint jump at 0x%s "
762 "in list (reinserting).\n",
768 error ("Jump already inserted at reinsert time.");
772 /* Since there can be trap breakpoints inserted in the same address
773 range, we use `write_inferior_memory', which takes care of
774 layering breakpoints on top of fast tracepoints, and on top of
775 the buffer we pass it. This works because we've already marked
776 the fast tracepoint jump inserted above. Also note that we need
777 to pass the current shadow contents, because
778 write_inferior_memory updates any shadow memory with what we pass
779 here, and we want that to be a nop. */
780 buf
= (unsigned char *) alloca (jp
->length
);
781 memcpy (buf
, fast_tracepoint_jump_shadow (jp
), jp
->length
);
782 err
= write_inferior_memory (where
, buf
, jp
->length
);
788 debug_printf ("Failed to reinsert fast tracepoint jump at"
790 paddress (where
), strerror (err
));
794 /* Set a high-level breakpoint of type TYPE, with low level type
795 RAW_TYPE and kind KIND, at WHERE. On success, a pointer to the new
796 breakpoint is returned. On failure, returns NULL and writes the
797 error code to *ERR. HANDLER is called when the breakpoint is hit.
798 HANDLER should return 1 if the breakpoint should be deleted, 0
801 static struct breakpoint
*
802 set_breakpoint (enum bkpt_type type
, enum raw_bkpt_type raw_type
,
803 CORE_ADDR where
, int kind
,
804 int (*handler
) (CORE_ADDR
), int *err
)
806 struct process_info
*proc
= current_process ();
807 struct breakpoint
*bp
;
808 struct raw_breakpoint
*raw
;
810 raw
= set_raw_breakpoint_at (raw_type
, where
, kind
, err
);
818 if (is_gdb_breakpoint (type
))
820 struct gdb_breakpoint
*gdb_bp
= XCNEW (struct gdb_breakpoint
);
822 bp
= (struct breakpoint
*) gdb_bp
;
823 gdb_assert (handler
== NULL
);
825 else if (type
== other_breakpoint
)
827 struct other_breakpoint
*other_bp
= XCNEW (struct other_breakpoint
);
829 other_bp
->handler
= handler
;
830 bp
= (struct breakpoint
*) other_bp
;
832 else if (type
== single_step_breakpoint
)
834 struct single_step_breakpoint
*ss_bp
835 = XCNEW (struct single_step_breakpoint
);
837 bp
= (struct breakpoint
*) ss_bp
;
840 gdb_assert_not_reached ("unhandled breakpoint type");
845 bp
->next
= proc
->breakpoints
;
846 proc
->breakpoints
= bp
;
851 /* Set breakpoint of TYPE on address WHERE with handler HANDLER. */
853 static struct breakpoint
*
854 set_breakpoint_type_at (enum bkpt_type type
, CORE_ADDR where
,
855 int (*handler
) (CORE_ADDR
))
858 CORE_ADDR placed_address
= where
;
859 int breakpoint_kind
= target_breakpoint_kind_from_pc (&placed_address
);
861 return set_breakpoint (type
, raw_bkpt_type_sw
,
862 placed_address
, breakpoint_kind
, handler
,
866 /* See mem-break.h */
869 set_breakpoint_at (CORE_ADDR where
, int (*handler
) (CORE_ADDR
))
871 return set_breakpoint_type_at (other_breakpoint
, where
, handler
);
876 delete_raw_breakpoint (struct process_info
*proc
, struct raw_breakpoint
*todel
)
878 struct raw_breakpoint
*bp
, **bp_link
;
881 bp
= proc
->raw_breakpoints
;
882 bp_link
= &proc
->raw_breakpoints
;
888 if (bp
->inserted
> 0)
890 struct raw_breakpoint
*prev_bp_link
= *bp_link
;
894 ret
= the_target
->remove_point (bp
->raw_type
, bp
->pc
, bp
->kind
,
898 /* Something went wrong, relink the breakpoint. */
899 *bp_link
= prev_bp_link
;
902 debug_printf ("Failed to uninsert raw breakpoint "
903 "at 0x%s while deleting it.\n",
921 warning ("Could not find raw breakpoint in list.");
926 release_breakpoint (struct process_info
*proc
, struct breakpoint
*bp
)
931 newrefcount
= bp
->raw
->refcount
- 1;
932 if (newrefcount
== 0)
934 ret
= delete_raw_breakpoint (proc
, bp
->raw
);
939 bp
->raw
->refcount
= newrefcount
;
947 delete_breakpoint_1 (struct process_info
*proc
, struct breakpoint
*todel
)
949 struct breakpoint
*bp
, **bp_link
;
952 bp
= proc
->breakpoints
;
953 bp_link
= &proc
->breakpoints
;
961 err
= release_breakpoint (proc
, bp
);
975 warning ("Could not find breakpoint in list.");
980 delete_breakpoint (struct breakpoint
*todel
)
982 struct process_info
*proc
= current_process ();
983 return delete_breakpoint_1 (proc
, todel
);
986 /* Locate a GDB breakpoint of type Z_TYPE and kind KIND placed at
987 address ADDR and return a pointer to its structure. If KIND is -1,
988 the breakpoint's kind is ignored. */
990 static struct gdb_breakpoint
*
991 find_gdb_breakpoint (char z_type
, CORE_ADDR addr
, int kind
)
993 struct process_info
*proc
= current_process ();
994 struct breakpoint
*bp
;
995 enum bkpt_type type
= Z_packet_to_bkpt_type (z_type
);
997 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
998 if (bp
->type
== type
&& bp
->raw
->pc
== addr
999 && (kind
== -1 || bp
->raw
->kind
== kind
))
1000 return (struct gdb_breakpoint
*) bp
;
1006 z_type_supported (char z_type
)
1008 return (z_type
>= '0' && z_type
<= '4'
1009 && the_target
->supports_z_point_type
!= NULL
1010 && the_target
->supports_z_point_type (z_type
));
1013 /* Create a new GDB breakpoint of type Z_TYPE at ADDR with kind KIND.
1014 Returns a pointer to the newly created breakpoint on success. On
1015 failure returns NULL and sets *ERR to either -1 for error, or 1 if
1016 Z_TYPE breakpoints are not supported on this target. */
1018 static struct gdb_breakpoint
*
1019 set_gdb_breakpoint_1 (char z_type
, CORE_ADDR addr
, int kind
, int *err
)
1021 struct gdb_breakpoint
*bp
;
1022 enum bkpt_type type
;
1023 enum raw_bkpt_type raw_type
;
1025 /* If we see GDB inserting a second code breakpoint at the same
1026 address, then either: GDB is updating the breakpoint's conditions
1027 or commands; or, the first breakpoint must have disappeared due
1028 to a shared library unload. On targets where the shared
1029 libraries are handled by userspace, like SVR4, for example,
1030 GDBserver can't tell if a library was loaded or unloaded. Since
1031 we refcount raw breakpoints, we must be careful to make sure GDB
1032 breakpoints never contribute more than one reference. if we
1033 didn't do this, in case the previous breakpoint is gone due to a
1034 shared library unload, we'd just increase the refcount of the
1035 previous breakpoint at this address, but the trap was not planted
1036 in the inferior anymore, thus the breakpoint would never be hit.
1037 Note this must be careful to not create a window where
1038 breakpoints are removed from the target, for non-stop, in case
1039 the target can poke at memory while the program is running. */
1040 if (z_type
== Z_PACKET_SW_BP
1041 || z_type
== Z_PACKET_HW_BP
)
1043 bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1047 if (bp
->base
.raw
->kind
!= kind
)
1049 /* A different kind than previously seen. The previous
1050 breakpoint must be gone then. */
1051 bp
->base
.raw
->inserted
= -1;
1052 delete_breakpoint ((struct breakpoint
*) bp
);
1055 else if (z_type
== Z_PACKET_SW_BP
)
1057 /* Check if the breakpoint is actually gone from the
1058 target, due to an solib unload, for example. Might
1059 as well validate _all_ breakpoints. */
1060 validate_breakpoints ();
1062 /* Breakpoints that don't pass validation are
1064 bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1070 /* Data breakpoints for the same address but different kind are
1071 expected. GDB doesn't merge these. The backend gets to do
1072 that if it wants/can. */
1073 bp
= find_gdb_breakpoint (z_type
, addr
, kind
);
1078 /* We already know about this breakpoint, there's nothing else
1079 to do - GDB's reference is already accounted for. Note that
1080 whether the breakpoint inserted is left as is - we may be
1081 stepping over it, for example, in which case we don't want to
1082 force-reinsert it. */
1086 raw_type
= Z_packet_to_raw_bkpt_type (z_type
);
1087 type
= Z_packet_to_bkpt_type (z_type
);
1088 return (struct gdb_breakpoint
*) set_breakpoint (type
, raw_type
, addr
,
1093 check_gdb_bp_preconditions (char z_type
, int *err
)
1095 /* As software/memory breakpoints work by poking at memory, we need
1096 to prepare to access memory. If that operation fails, we need to
1097 return error. Seeing an error, if this is the first breakpoint
1098 of that type that GDB tries to insert, GDB would then assume the
1099 breakpoint type is supported, but it may actually not be. So we
1100 need to check whether the type is supported at all before
1101 preparing to access memory. */
1102 if (!z_type_supported (z_type
))
1111 /* See mem-break.h. This is a wrapper for set_gdb_breakpoint_1 that
1112 knows to prepare to access memory for Z0 breakpoints. */
1114 struct gdb_breakpoint
*
1115 set_gdb_breakpoint (char z_type
, CORE_ADDR addr
, int kind
, int *err
)
1117 struct gdb_breakpoint
*bp
;
1119 if (!check_gdb_bp_preconditions (z_type
, err
))
1122 /* If inserting a software/memory breakpoint, need to prepare to
1124 if (z_type
== Z_PACKET_SW_BP
)
1126 if (prepare_to_access_memory () != 0)
1133 bp
= set_gdb_breakpoint_1 (z_type
, addr
, kind
, err
);
1135 if (z_type
== Z_PACKET_SW_BP
)
1136 done_accessing_memory ();
1141 /* Delete a GDB breakpoint of type Z_TYPE and kind KIND previously
1142 inserted at ADDR with set_gdb_breakpoint_at. Returns 0 on success,
1143 -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
1147 delete_gdb_breakpoint_1 (char z_type
, CORE_ADDR addr
, int kind
)
1149 struct gdb_breakpoint
*bp
;
1152 bp
= find_gdb_breakpoint (z_type
, addr
, kind
);
1156 /* Before deleting the breakpoint, make sure to free its condition
1157 and command lists. */
1158 clear_breakpoint_conditions_and_commands (bp
);
1159 err
= delete_breakpoint ((struct breakpoint
*) bp
);
1166 /* See mem-break.h. This is a wrapper for delete_gdb_breakpoint that
1167 knows to prepare to access memory for Z0 breakpoints. */
1170 delete_gdb_breakpoint (char z_type
, CORE_ADDR addr
, int kind
)
1174 if (!check_gdb_bp_preconditions (z_type
, &ret
))
1177 /* If inserting a software/memory breakpoint, need to prepare to
1179 if (z_type
== Z_PACKET_SW_BP
)
1183 err
= prepare_to_access_memory ();
1188 ret
= delete_gdb_breakpoint_1 (z_type
, addr
, kind
);
1190 if (z_type
== Z_PACKET_SW_BP
)
1191 done_accessing_memory ();
1196 /* Clear all conditions associated with a breakpoint. */
1199 clear_breakpoint_conditions (struct gdb_breakpoint
*bp
)
1201 struct point_cond_list
*cond
;
1203 if (bp
->cond_list
== NULL
)
1206 cond
= bp
->cond_list
;
1208 while (cond
!= NULL
)
1210 struct point_cond_list
*cond_next
;
1212 cond_next
= cond
->next
;
1213 gdb_free_agent_expr (cond
->cond
);
1218 bp
->cond_list
= NULL
;
1221 /* Clear all commands associated with a breakpoint. */
1224 clear_breakpoint_commands (struct gdb_breakpoint
*bp
)
1226 struct point_command_list
*cmd
;
1228 if (bp
->command_list
== NULL
)
1231 cmd
= bp
->command_list
;
1235 struct point_command_list
*cmd_next
;
1237 cmd_next
= cmd
->next
;
1238 gdb_free_agent_expr (cmd
->cmd
);
1243 bp
->command_list
= NULL
;
1247 clear_breakpoint_conditions_and_commands (struct gdb_breakpoint
*bp
)
1249 clear_breakpoint_conditions (bp
);
1250 clear_breakpoint_commands (bp
);
1253 /* Add condition CONDITION to GDBserver's breakpoint BP. */
1256 add_condition_to_breakpoint (struct gdb_breakpoint
*bp
,
1257 struct agent_expr
*condition
)
1259 struct point_cond_list
*new_cond
;
1261 /* Create new condition. */
1262 new_cond
= XCNEW (struct point_cond_list
);
1263 new_cond
->cond
= condition
;
1265 /* Add condition to the list. */
1266 new_cond
->next
= bp
->cond_list
;
1267 bp
->cond_list
= new_cond
;
1270 /* Add a target-side condition CONDITION to a breakpoint. */
1273 add_breakpoint_condition (struct gdb_breakpoint
*bp
, const char **condition
)
1275 const char *actparm
= *condition
;
1276 struct agent_expr
*cond
;
1278 if (condition
== NULL
)
1284 cond
= gdb_parse_agent_expr (&actparm
);
1288 warning ("Condition evaluation failed. Assuming unconditional.");
1292 add_condition_to_breakpoint (bp
, cond
);
1294 *condition
= actparm
;
1299 /* Evaluate condition (if any) at breakpoint BP. Return 1 if
1300 true and 0 otherwise. */
1303 gdb_condition_true_at_breakpoint_z_type (char z_type
, CORE_ADDR addr
)
1305 /* Fetch registers for the current inferior. */
1306 struct gdb_breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1308 struct point_cond_list
*cl
;
1310 struct eval_agent_expr_context ctx
;
1315 /* Check if the breakpoint is unconditional. If it is,
1316 the condition always evaluates to TRUE. */
1317 if (bp
->cond_list
== NULL
)
1320 ctx
.regcache
= get_thread_regcache (current_thread
, 1);
1324 /* Evaluate each condition in the breakpoint's list of conditions.
1325 Return true if any of the conditions evaluates to TRUE.
1327 If we failed to evaluate the expression, TRUE is returned. This
1328 forces GDB to reevaluate the conditions. */
1329 for (cl
= bp
->cond_list
;
1330 cl
&& !value
&& !err
; cl
= cl
->next
)
1332 /* Evaluate the condition. */
1333 err
= gdb_eval_agent_expr (&ctx
, cl
->cond
, &value
);
1339 return (value
!= 0);
1343 gdb_condition_true_at_breakpoint (CORE_ADDR where
)
1345 /* Only check code (software or hardware) breakpoints. */
1346 return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP
, where
)
1347 || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP
, where
));
1350 /* Add commands COMMANDS to GDBserver's breakpoint BP. */
1353 add_commands_to_breakpoint (struct gdb_breakpoint
*bp
,
1354 struct agent_expr
*commands
, int persist
)
1356 struct point_command_list
*new_cmd
;
1358 /* Create new command. */
1359 new_cmd
= XCNEW (struct point_command_list
);
1360 new_cmd
->cmd
= commands
;
1361 new_cmd
->persistence
= persist
;
1363 /* Add commands to the list. */
1364 new_cmd
->next
= bp
->command_list
;
1365 bp
->command_list
= new_cmd
;
1368 /* Add a target-side command COMMAND to the breakpoint at ADDR. */
1371 add_breakpoint_commands (struct gdb_breakpoint
*bp
, const char **command
,
1374 const char *actparm
= *command
;
1375 struct agent_expr
*cmd
;
1377 if (command
== NULL
)
1383 cmd
= gdb_parse_agent_expr (&actparm
);
1387 warning ("Command evaluation failed. Disabling.");
1391 add_commands_to_breakpoint (bp
, cmd
, persist
);
1398 /* Return true if there are no commands to run at this location,
1399 which likely means we want to report back to GDB. */
1402 gdb_no_commands_at_breakpoint_z_type (char z_type
, CORE_ADDR addr
)
1404 struct gdb_breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1410 debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s\n",
1411 paddress (addr
), z_type
,
1412 phex_nz ((uintptr_t) bp
->command_list
, 0));
1413 return (bp
->command_list
== NULL
);
1416 /* Return true if there are no commands to run at this location,
1417 which likely means we want to report back to GDB. */
1420 gdb_no_commands_at_breakpoint (CORE_ADDR where
)
1422 /* Only check code (software or hardware) breakpoints. */
1423 return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP
, where
)
1424 && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP
, where
));
1427 /* Run a breakpoint's commands. Returns 0 if there was a problem
1428 running any command, 1 otherwise. */
1431 run_breakpoint_commands_z_type (char z_type
, CORE_ADDR addr
)
1433 /* Fetch registers for the current inferior. */
1434 struct gdb_breakpoint
*bp
= find_gdb_breakpoint (z_type
, addr
, -1);
1436 struct point_command_list
*cl
;
1438 struct eval_agent_expr_context ctx
;
1443 ctx
.regcache
= get_thread_regcache (current_thread
, 1);
1447 for (cl
= bp
->command_list
;
1448 cl
&& !value
&& !err
; cl
= cl
->next
)
1450 /* Run the command. */
1451 err
= gdb_eval_agent_expr (&ctx
, cl
->cmd
, &value
);
1453 /* If one command has a problem, stop digging the hole deeper. */
1462 run_breakpoint_commands (CORE_ADDR where
)
1464 /* Only check code (software or hardware) breakpoints. If one
1465 command has a problem, stop digging the hole deeper. */
1466 if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP
, where
))
1467 run_breakpoint_commands_z_type (Z_PACKET_HW_BP
, where
);
1470 /* See mem-break.h. */
1473 gdb_breakpoint_here (CORE_ADDR where
)
1475 /* Only check code (software or hardware) breakpoints. */
1476 return (find_gdb_breakpoint (Z_PACKET_SW_BP
, where
, -1) != NULL
1477 || find_gdb_breakpoint (Z_PACKET_HW_BP
, where
, -1) != NULL
);
1481 set_single_step_breakpoint (CORE_ADDR stop_at
, ptid_t ptid
)
1483 struct single_step_breakpoint
*bp
;
1485 gdb_assert (ptid_get_pid (current_ptid
) == ptid_get_pid (ptid
));
1487 bp
= (struct single_step_breakpoint
*) set_breakpoint_type_at (single_step_breakpoint
,
1493 delete_single_step_breakpoints (struct thread_info
*thread
)
1495 struct process_info
*proc
= get_thread_process (thread
);
1496 struct breakpoint
*bp
, **bp_link
;
1498 bp
= proc
->breakpoints
;
1499 bp_link
= &proc
->breakpoints
;
1503 if (bp
->type
== single_step_breakpoint
1504 && ptid_equal (((struct single_step_breakpoint
*) bp
)->ptid
,
1507 struct thread_info
*saved_thread
= current_thread
;
1509 current_thread
= thread
;
1510 *bp_link
= bp
->next
;
1511 release_breakpoint (proc
, bp
);
1513 current_thread
= saved_thread
;
1517 bp_link
= &bp
->next
;
1524 uninsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1526 if (bp
->inserted
< 0)
1529 debug_printf ("Breakpoint at %s is marked insert-disabled.\n",
1532 else if (bp
->inserted
> 0)
1538 err
= the_target
->remove_point (bp
->raw_type
, bp
->pc
, bp
->kind
, bp
);
1544 debug_printf ("Failed to uninsert raw breakpoint at 0x%s.\n",
1551 uninsert_breakpoints_at (CORE_ADDR pc
)
1553 struct process_info
*proc
= current_process ();
1554 struct raw_breakpoint
*bp
;
1557 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1558 if ((bp
->raw_type
== raw_bkpt_type_sw
1559 || bp
->raw_type
== raw_bkpt_type_hw
)
1565 uninsert_raw_breakpoint (bp
);
1570 /* This can happen when we remove all breakpoints while handling
1573 debug_printf ("Could not find breakpoint at 0x%s "
1574 "in list (uninserting).\n",
1580 uninsert_all_breakpoints (void)
1582 struct process_info
*proc
= current_process ();
1583 struct raw_breakpoint
*bp
;
1585 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1586 if ((bp
->raw_type
== raw_bkpt_type_sw
1587 || bp
->raw_type
== raw_bkpt_type_hw
)
1589 uninsert_raw_breakpoint (bp
);
1593 uninsert_single_step_breakpoints (struct thread_info
*thread
)
1595 struct process_info
*proc
= get_thread_process (thread
);
1596 struct breakpoint
*bp
;
1598 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1600 if (bp
->type
== single_step_breakpoint
1601 && ptid_equal (((struct single_step_breakpoint
*) bp
)->ptid
,
1604 gdb_assert (bp
->raw
->inserted
> 0);
1606 /* Only uninsert the raw breakpoint if it only belongs to a
1607 reinsert breakpoint. */
1608 if (bp
->raw
->refcount
== 1)
1610 struct thread_info
*saved_thread
= current_thread
;
1612 current_thread
= thread
;
1613 uninsert_raw_breakpoint (bp
->raw
);
1614 current_thread
= saved_thread
;
1621 reinsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1628 err
= the_target
->insert_point (bp
->raw_type
, bp
->pc
, bp
->kind
, bp
);
1631 else if (debug_threads
)
1632 debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).\n",
1633 paddress (bp
->pc
), err
);
1637 reinsert_breakpoints_at (CORE_ADDR pc
)
1639 struct process_info
*proc
= current_process ();
1640 struct raw_breakpoint
*bp
;
1643 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1644 if ((bp
->raw_type
== raw_bkpt_type_sw
1645 || bp
->raw_type
== raw_bkpt_type_hw
)
1650 reinsert_raw_breakpoint (bp
);
1655 /* This can happen when we remove all breakpoints while handling
1658 debug_printf ("Could not find raw breakpoint at 0x%s "
1659 "in list (reinserting).\n",
1665 has_single_step_breakpoints (struct thread_info
*thread
)
1667 struct process_info
*proc
= get_thread_process (thread
);
1668 struct breakpoint
*bp
, **bp_link
;
1670 bp
= proc
->breakpoints
;
1671 bp_link
= &proc
->breakpoints
;
1675 if (bp
->type
== single_step_breakpoint
1676 && ptid_equal (((struct single_step_breakpoint
*) bp
)->ptid
,
1681 bp_link
= &bp
->next
;
1690 reinsert_all_breakpoints (void)
1692 struct process_info
*proc
= current_process ();
1693 struct raw_breakpoint
*bp
;
1695 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1696 if ((bp
->raw_type
== raw_bkpt_type_sw
1697 || bp
->raw_type
== raw_bkpt_type_hw
)
1699 reinsert_raw_breakpoint (bp
);
1703 reinsert_single_step_breakpoints (struct thread_info
*thread
)
1705 struct process_info
*proc
= get_thread_process (thread
);
1706 struct breakpoint
*bp
;
1708 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1710 if (bp
->type
== single_step_breakpoint
1711 && ptid_equal (((struct single_step_breakpoint
*) bp
)->ptid
,
1714 gdb_assert (bp
->raw
->inserted
> 0);
1716 if (bp
->raw
->refcount
== 1)
1718 struct thread_info
*saved_thread
= current_thread
;
1720 current_thread
= thread
;
1721 reinsert_raw_breakpoint (bp
->raw
);
1722 current_thread
= saved_thread
;
1729 check_breakpoints (CORE_ADDR stop_pc
)
1731 struct process_info
*proc
= current_process ();
1732 struct breakpoint
*bp
, **bp_link
;
1734 bp
= proc
->breakpoints
;
1735 bp_link
= &proc
->breakpoints
;
1739 struct raw_breakpoint
*raw
= bp
->raw
;
1741 if ((raw
->raw_type
== raw_bkpt_type_sw
1742 || raw
->raw_type
== raw_bkpt_type_hw
)
1743 && raw
->pc
== stop_pc
)
1747 warning ("Hit a removed breakpoint?");
1751 if (bp
->type
== other_breakpoint
)
1753 struct other_breakpoint
*other_bp
1754 = (struct other_breakpoint
*) bp
;
1756 if (other_bp
->handler
!= NULL
&& (*other_bp
->handler
) (stop_pc
))
1758 *bp_link
= bp
->next
;
1760 release_breakpoint (proc
, bp
);
1768 bp_link
= &bp
->next
;
1774 breakpoint_here (CORE_ADDR addr
)
1776 struct process_info
*proc
= current_process ();
1777 struct raw_breakpoint
*bp
;
1779 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1780 if ((bp
->raw_type
== raw_bkpt_type_sw
1781 || bp
->raw_type
== raw_bkpt_type_hw
)
1789 breakpoint_inserted_here (CORE_ADDR addr
)
1791 struct process_info
*proc
= current_process ();
1792 struct raw_breakpoint
*bp
;
1794 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1795 if ((bp
->raw_type
== raw_bkpt_type_sw
1796 || bp
->raw_type
== raw_bkpt_type_hw
)
1804 /* See mem-break.h. */
1807 software_breakpoint_inserted_here (CORE_ADDR addr
)
1809 struct process_info
*proc
= current_process ();
1810 struct raw_breakpoint
*bp
;
1812 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1813 if (bp
->raw_type
== raw_bkpt_type_sw
1821 /* See mem-break.h. */
1824 hardware_breakpoint_inserted_here (CORE_ADDR addr
)
1826 struct process_info
*proc
= current_process ();
1827 struct raw_breakpoint
*bp
;
1829 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1830 if (bp
->raw_type
== raw_bkpt_type_hw
1838 /* See mem-break.h. */
1841 single_step_breakpoint_inserted_here (CORE_ADDR addr
)
1843 struct process_info
*proc
= current_process ();
1844 struct breakpoint
*bp
;
1846 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1847 if (bp
->type
== single_step_breakpoint
1848 && bp
->raw
->pc
== addr
1849 && bp
->raw
->inserted
)
1856 validate_inserted_breakpoint (struct raw_breakpoint
*bp
)
1861 gdb_assert (bp
->inserted
);
1862 gdb_assert (bp
->raw_type
== raw_bkpt_type_sw
);
1864 buf
= (unsigned char *) alloca (bp_size (bp
));
1865 err
= (*the_target
->read_memory
) (bp
->pc
, buf
, bp_size (bp
));
1866 if (err
|| memcmp (buf
, bp_opcode (bp
), bp_size (bp
)) != 0)
1868 /* Tag it as gone. */
1877 delete_disabled_breakpoints (void)
1879 struct process_info
*proc
= current_process ();
1880 struct breakpoint
*bp
, *next
;
1882 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= next
)
1885 if (bp
->raw
->inserted
< 0)
1887 /* If single_step_breakpoints become disabled, that means the
1888 manipulations (insertion and removal) of them are wrong. */
1889 gdb_assert (bp
->type
!= single_step_breakpoint
);
1890 delete_breakpoint_1 (proc
, bp
);
1895 /* Check if breakpoints we inserted still appear to be inserted. They
1896 may disappear due to a shared library unload, and worse, a new
1897 shared library may be reloaded at the same address as the
1898 previously unloaded one. If that happens, we should make sure that
1899 the shadow memory of the old breakpoints isn't used when reading or
1903 validate_breakpoints (void)
1905 struct process_info
*proc
= current_process ();
1906 struct breakpoint
*bp
;
1908 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1910 struct raw_breakpoint
*raw
= bp
->raw
;
1912 if (raw
->raw_type
== raw_bkpt_type_sw
&& raw
->inserted
> 0)
1913 validate_inserted_breakpoint (raw
);
1916 delete_disabled_breakpoints ();
1920 check_mem_read (CORE_ADDR mem_addr
, unsigned char *buf
, int mem_len
)
1922 struct process_info
*proc
= current_process ();
1923 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
1924 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
1925 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
1926 int disabled_one
= 0;
1928 for (; jp
!= NULL
; jp
= jp
->next
)
1930 CORE_ADDR bp_end
= jp
->pc
+ jp
->length
;
1931 CORE_ADDR start
, end
;
1932 int copy_offset
, copy_len
, buf_offset
;
1934 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= buf
+ mem_len
1935 || buf
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
1937 if (mem_addr
>= bp_end
)
1939 if (jp
->pc
>= mem_end
)
1943 if (mem_addr
> start
)
1950 copy_len
= end
- start
;
1951 copy_offset
= start
- jp
->pc
;
1952 buf_offset
= start
- mem_addr
;
1955 memcpy (buf
+ buf_offset
,
1956 fast_tracepoint_jump_shadow (jp
) + copy_offset
,
1960 for (; bp
!= NULL
; bp
= bp
->next
)
1962 CORE_ADDR bp_end
= bp
->pc
+ bp_size (bp
);
1963 CORE_ADDR start
, end
;
1964 int copy_offset
, copy_len
, buf_offset
;
1966 if (bp
->raw_type
!= raw_bkpt_type_sw
)
1969 gdb_assert (bp
->old_data
>= buf
+ mem_len
1970 || buf
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
1972 if (mem_addr
>= bp_end
)
1974 if (bp
->pc
>= mem_end
)
1978 if (mem_addr
> start
)
1985 copy_len
= end
- start
;
1986 copy_offset
= start
- bp
->pc
;
1987 buf_offset
= start
- mem_addr
;
1989 if (bp
->inserted
> 0)
1991 if (validate_inserted_breakpoint (bp
))
1992 memcpy (buf
+ buf_offset
, bp
->old_data
+ copy_offset
, copy_len
);
1999 delete_disabled_breakpoints ();
2003 check_mem_write (CORE_ADDR mem_addr
, unsigned char *buf
,
2004 const unsigned char *myaddr
, int mem_len
)
2006 struct process_info
*proc
= current_process ();
2007 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
2008 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
2009 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
2010 int disabled_one
= 0;
2012 /* First fast tracepoint jumps, then breakpoint traps on top. */
2014 for (; jp
!= NULL
; jp
= jp
->next
)
2016 CORE_ADDR jp_end
= jp
->pc
+ jp
->length
;
2017 CORE_ADDR start
, end
;
2018 int copy_offset
, copy_len
, buf_offset
;
2020 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= myaddr
+ mem_len
2021 || myaddr
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
2022 gdb_assert (fast_tracepoint_jump_insn (jp
) >= buf
+ mem_len
2023 || buf
>= fast_tracepoint_jump_insn (jp
) + (jp
)->length
);
2025 if (mem_addr
>= jp_end
)
2027 if (jp
->pc
>= mem_end
)
2031 if (mem_addr
> start
)
2038 copy_len
= end
- start
;
2039 copy_offset
= start
- jp
->pc
;
2040 buf_offset
= start
- mem_addr
;
2042 memcpy (fast_tracepoint_jump_shadow (jp
) + copy_offset
,
2043 myaddr
+ buf_offset
, copy_len
);
2045 memcpy (buf
+ buf_offset
,
2046 fast_tracepoint_jump_insn (jp
) + copy_offset
, copy_len
);
2049 for (; bp
!= NULL
; bp
= bp
->next
)
2051 CORE_ADDR bp_end
= bp
->pc
+ bp_size (bp
);
2052 CORE_ADDR start
, end
;
2053 int copy_offset
, copy_len
, buf_offset
;
2055 if (bp
->raw_type
!= raw_bkpt_type_sw
)
2058 gdb_assert (bp
->old_data
>= myaddr
+ mem_len
2059 || myaddr
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
2061 if (mem_addr
>= bp_end
)
2063 if (bp
->pc
>= mem_end
)
2067 if (mem_addr
> start
)
2074 copy_len
= end
- start
;
2075 copy_offset
= start
- bp
->pc
;
2076 buf_offset
= start
- mem_addr
;
2078 memcpy (bp
->old_data
+ copy_offset
, myaddr
+ buf_offset
, copy_len
);
2079 if (bp
->inserted
> 0)
2081 if (validate_inserted_breakpoint (bp
))
2082 memcpy (buf
+ buf_offset
, bp_opcode (bp
) + copy_offset
, copy_len
);
2089 delete_disabled_breakpoints ();
2092 /* Delete all breakpoints, and un-insert them from the inferior. */
2095 delete_all_breakpoints (void)
2097 struct process_info
*proc
= current_process ();
2099 while (proc
->breakpoints
)
2100 delete_breakpoint_1 (proc
, proc
->breakpoints
);
2103 /* Clear the "inserted" flag in all breakpoints. */
2106 mark_breakpoints_out (struct process_info
*proc
)
2108 struct raw_breakpoint
*raw_bp
;
2110 for (raw_bp
= proc
->raw_breakpoints
; raw_bp
!= NULL
; raw_bp
= raw_bp
->next
)
2111 raw_bp
->inserted
= 0;
2114 /* Release all breakpoints, but do not try to un-insert them from the
2118 free_all_breakpoints (struct process_info
*proc
)
2120 mark_breakpoints_out (proc
);
2122 /* Note: use PROC explicitly instead of deferring to
2123 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
2124 released when we get here. There should be no call to
2125 current_process from here on. */
2126 while (proc
->breakpoints
)
2127 delete_breakpoint_1 (proc
, proc
->breakpoints
);
2130 /* Clone an agent expression. */
2132 static struct agent_expr
*
2133 clone_agent_expr (const struct agent_expr
*src_ax
)
2135 struct agent_expr
*ax
;
2137 ax
= XCNEW (struct agent_expr
);
2138 ax
->length
= src_ax
->length
;
2139 ax
->bytes
= (unsigned char *) xcalloc (ax
->length
, 1);
2140 memcpy (ax
->bytes
, src_ax
->bytes
, ax
->length
);
2144 /* Deep-copy the contents of one breakpoint to another. */
2146 static struct breakpoint
*
2147 clone_one_breakpoint (const struct breakpoint
*src
, ptid_t ptid
)
2149 struct breakpoint
*dest
;
2150 struct raw_breakpoint
*dest_raw
;
2152 /* Clone the raw breakpoint. */
2153 dest_raw
= XCNEW (struct raw_breakpoint
);
2154 dest_raw
->raw_type
= src
->raw
->raw_type
;
2155 dest_raw
->refcount
= src
->raw
->refcount
;
2156 dest_raw
->pc
= src
->raw
->pc
;
2157 dest_raw
->kind
= src
->raw
->kind
;
2158 memcpy (dest_raw
->old_data
, src
->raw
->old_data
, MAX_BREAKPOINT_LEN
);
2159 dest_raw
->inserted
= src
->raw
->inserted
;
2161 /* Clone the high-level breakpoint. */
2162 if (is_gdb_breakpoint (src
->type
))
2164 struct gdb_breakpoint
*gdb_dest
= XCNEW (struct gdb_breakpoint
);
2165 struct point_cond_list
*current_cond
;
2166 struct point_cond_list
*new_cond
;
2167 struct point_cond_list
*cond_tail
= NULL
;
2168 struct point_command_list
*current_cmd
;
2169 struct point_command_list
*new_cmd
;
2170 struct point_command_list
*cmd_tail
= NULL
;
2172 /* Clone the condition list. */
2173 for (current_cond
= ((struct gdb_breakpoint
*) src
)->cond_list
;
2174 current_cond
!= NULL
;
2175 current_cond
= current_cond
->next
)
2177 new_cond
= XCNEW (struct point_cond_list
);
2178 new_cond
->cond
= clone_agent_expr (current_cond
->cond
);
2179 APPEND_TO_LIST (&gdb_dest
->cond_list
, new_cond
, cond_tail
);
2182 /* Clone the command list. */
2183 for (current_cmd
= ((struct gdb_breakpoint
*) src
)->command_list
;
2184 current_cmd
!= NULL
;
2185 current_cmd
= current_cmd
->next
)
2187 new_cmd
= XCNEW (struct point_command_list
);
2188 new_cmd
->cmd
= clone_agent_expr (current_cmd
->cmd
);
2189 new_cmd
->persistence
= current_cmd
->persistence
;
2190 APPEND_TO_LIST (&gdb_dest
->command_list
, new_cmd
, cmd_tail
);
2193 dest
= (struct breakpoint
*) gdb_dest
;
2195 else if (src
->type
== other_breakpoint
)
2197 struct other_breakpoint
*other_dest
= XCNEW (struct other_breakpoint
);
2199 other_dest
->handler
= ((struct other_breakpoint
*) src
)->handler
;
2200 dest
= (struct breakpoint
*) other_dest
;
2202 else if (src
->type
== single_step_breakpoint
)
2204 struct single_step_breakpoint
*ss_dest
2205 = XCNEW (struct single_step_breakpoint
);
2207 dest
= (struct breakpoint
*) ss_dest
;
2208 /* Since single-step breakpoint is thread specific, don't copy
2209 thread id from SRC, use ID instead. */
2210 ss_dest
->ptid
= ptid
;
2213 gdb_assert_not_reached ("unhandled breakpoint type");
2215 dest
->type
= src
->type
;
2216 dest
->raw
= dest_raw
;
2221 /* See mem-break.h. */
2224 clone_all_breakpoints (struct thread_info
*child_thread
,
2225 const struct thread_info
*parent_thread
)
2227 const struct breakpoint
*bp
;
2228 struct breakpoint
*new_bkpt
;
2229 struct breakpoint
*bkpt_tail
= NULL
;
2230 struct raw_breakpoint
*raw_bkpt_tail
= NULL
;
2231 struct process_info
*child_proc
= get_thread_process (child_thread
);
2232 struct process_info
*parent_proc
= get_thread_process (parent_thread
);
2233 struct breakpoint
**new_list
= &child_proc
->breakpoints
;
2234 struct raw_breakpoint
**new_raw_list
= &child_proc
->raw_breakpoints
;
2236 for (bp
= parent_proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
2238 new_bkpt
= clone_one_breakpoint (bp
, ptid_of (child_thread
));
2239 APPEND_TO_LIST (new_list
, new_bkpt
, bkpt_tail
);
2240 APPEND_TO_LIST (new_raw_list
, new_bkpt
->raw
, raw_bkpt_tail
);