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. But, we need to keep track of internal breakpoints too,
33 and so we do need to be able to install multiple breakpoints at the
34 same address transparently. We keep track of two different, and
35 closely related structures. A raw breakpoint, which manages the
36 low level, close to the metal aspect of a breakpoint. It holds the
37 breakpoint address, and a buffer holding a copy of the instructions
38 that would be in memory had not been a breakpoint there (we call
39 that the shadow memory of the breakpoint). We occasionally need to
40 temporarilly uninsert a breakpoint without the client knowing about
41 it (e.g., to step over an internal breakpoint), so we keep an
42 `inserted' state associated with this low level breakpoint
43 structure. There can only be one such object for a given address.
44 Then, we have (a bit higher level) breakpoints. This structure
45 holds a callback to be called whenever a breakpoint is hit, a
46 high-level type, and a link to a low level raw breakpoint. There
47 can be many high-level breakpoints at the same address, and all of
48 them will point to the same raw breakpoint, which is reference
51 /* The low level, physical, raw breakpoint. */
54 struct raw_breakpoint
*next
;
56 /* A reference count. Each high level breakpoint referencing this
57 raw breakpoint accounts for one reference. */
60 /* The breakpoint's insertion address. There can only be one raw
61 breakpoint for a given PC. */
64 /* The breakpoint's shadow memory. */
65 unsigned char old_data
[MAX_BREAKPOINT_LEN
];
67 /* Non-zero if this breakpoint is currently inserted in the
71 /* Non-zero if this breakpoint is currently disabled because we no
72 longer detect it as inserted. */
76 /* The type of a breakpoint. */
79 /* A GDB breakpoint, requested with a Z0 packet. */
82 /* A basic-software-single-step breakpoint. */
85 /* Any other breakpoint type that doesn't require specific
86 treatment goes here. E.g., an event breakpoint. */
90 struct point_cond_list
92 /* Pointer to the agent expression that is the breakpoint's
94 struct agent_expr
*cond
;
96 /* Pointer to the next condition. */
97 struct point_cond_list
*next
;
100 struct point_command_list
102 /* Pointer to the agent expression that is the breakpoint's
104 struct agent_expr
*cmd
;
106 /* Flag that is true if this command should run even while GDB is
110 /* Pointer to the next command. */
111 struct point_command_list
*next
;
114 /* A high level (in gdbserver's perspective) breakpoint. */
117 struct breakpoint
*next
;
119 /* The breakpoint's type. */
122 /* Pointer to the condition list that should be evaluated on
123 the target or NULL if the breakpoint is unconditional or
124 if GDB doesn't want us to evaluate the conditionals on the
126 struct point_cond_list
*cond_list
;
128 /* Point to the list of commands to run when this is hit. */
129 struct point_command_list
*command_list
;
131 /* Link to this breakpoint's raw breakpoint. This is always
133 struct raw_breakpoint
*raw
;
135 /* Function to call when we hit this breakpoint. If it returns 1,
136 the breakpoint shall be deleted; 0 or if this callback is NULL,
137 it will be left inserted. */
138 int (*handler
) (CORE_ADDR
);
142 any_persistent_commands ()
144 struct process_info
*proc
= current_process ();
145 struct breakpoint
*bp
;
146 struct point_command_list
*cl
;
148 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
150 for (cl
= bp
->command_list
; cl
!= NULL
; cl
= cl
->next
)
158 static struct raw_breakpoint
*
159 find_raw_breakpoint_at (CORE_ADDR where
)
161 struct process_info
*proc
= current_process ();
162 struct raw_breakpoint
*bp
;
164 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
171 static struct raw_breakpoint
*
172 set_raw_breakpoint_at (CORE_ADDR where
)
174 struct process_info
*proc
= current_process ();
175 struct raw_breakpoint
*bp
;
177 unsigned char buf
[MAX_BREAKPOINT_LEN
];
179 if (breakpoint_data
== NULL
)
180 error ("Target does not support breakpoints.");
182 bp
= find_raw_breakpoint_at (where
);
189 bp
= xcalloc (1, sizeof (*bp
));
193 /* Note that there can be fast tracepoint jumps installed in the
194 same memory range, so to get at the original memory, we need to
195 use read_inferior_memory, which masks those out. */
196 err
= read_inferior_memory (where
, buf
, breakpoint_len
);
200 debug_printf ("Failed to read shadow memory of"
201 " breakpoint at 0x%s (%s).\n",
202 paddress (where
), strerror (err
));
206 memcpy (bp
->old_data
, buf
, breakpoint_len
);
208 err
= (*the_target
->write_memory
) (where
, breakpoint_data
,
213 debug_printf ("Failed to insert breakpoint at 0x%s (%s).\n",
214 paddress (where
), strerror (err
));
219 /* Link the breakpoint in. */
221 bp
->next
= proc
->raw_breakpoints
;
222 proc
->raw_breakpoints
= bp
;
226 /* Notice that breakpoint traps are always installed on top of fast
227 tracepoint jumps. This is even if the fast tracepoint is installed
228 at a later time compared to when the breakpoint was installed.
229 This means that a stopping breakpoint or tracepoint has higher
230 "priority". In turn, this allows having fast and slow tracepoints
231 (and breakpoints) at the same address behave correctly. */
234 /* A fast tracepoint jump. */
236 struct fast_tracepoint_jump
238 struct fast_tracepoint_jump
*next
;
240 /* A reference count. GDB can install more than one fast tracepoint
241 at the same address (each with its own action list, for
245 /* The fast tracepoint's insertion address. There can only be one
246 of these for a given PC. */
249 /* Non-zero if this fast tracepoint jump is currently inserted in
253 /* The length of the jump instruction. */
256 /* A poor-man's flexible array member, holding both the jump
257 instruction to insert, and a copy of the instruction that would
258 be in memory had not been a jump there (the shadow memory of the
260 unsigned char insn_and_shadow
[0];
263 /* Fast tracepoint FP's jump instruction to insert. */
264 #define fast_tracepoint_jump_insn(fp) \
265 ((fp)->insn_and_shadow + 0)
267 /* The shadow memory of fast tracepoint jump FP. */
268 #define fast_tracepoint_jump_shadow(fp) \
269 ((fp)->insn_and_shadow + (fp)->length)
272 /* Return the fast tracepoint jump set at WHERE. */
274 static struct fast_tracepoint_jump
*
275 find_fast_tracepoint_jump_at (CORE_ADDR where
)
277 struct process_info
*proc
= current_process ();
278 struct fast_tracepoint_jump
*jp
;
280 for (jp
= proc
->fast_tracepoint_jumps
; jp
!= NULL
; jp
= jp
->next
)
288 fast_tracepoint_jump_here (CORE_ADDR where
)
290 struct fast_tracepoint_jump
*jp
= find_fast_tracepoint_jump_at (where
);
296 delete_fast_tracepoint_jump (struct fast_tracepoint_jump
*todel
)
298 struct fast_tracepoint_jump
*bp
, **bp_link
;
300 struct process_info
*proc
= current_process ();
302 bp
= proc
->fast_tracepoint_jumps
;
303 bp_link
= &proc
->fast_tracepoint_jumps
;
309 if (--bp
->refcount
== 0)
311 struct fast_tracepoint_jump
*prev_bp_link
= *bp_link
;
317 /* Since there can be breakpoints inserted in the same
318 address range, we use `write_inferior_memory', which
319 takes care of layering breakpoints on top of fast
320 tracepoints, and on top of the buffer we pass it.
321 This works because we've already unlinked the fast
322 tracepoint jump above. Also note that we need to
323 pass the current shadow contents, because
324 write_inferior_memory updates any shadow memory with
325 what we pass here, and we want that to be a nop. */
326 buf
= alloca (bp
->length
);
327 memcpy (buf
, fast_tracepoint_jump_shadow (bp
), bp
->length
);
328 ret
= write_inferior_memory (bp
->pc
, buf
, bp
->length
);
331 /* Something went wrong, relink the jump. */
332 *bp_link
= prev_bp_link
;
335 debug_printf ("Failed to uninsert fast tracepoint jump "
336 "at 0x%s (%s) while deleting it.\n",
337 paddress (bp
->pc
), strerror (ret
));
353 warning ("Could not find fast tracepoint jump in list.");
358 inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump
*jp
)
363 struct fast_tracepoint_jump
*
364 set_fast_tracepoint_jump (CORE_ADDR where
,
365 unsigned char *insn
, ULONGEST length
)
367 struct process_info
*proc
= current_process ();
368 struct fast_tracepoint_jump
*jp
;
372 /* We refcount fast tracepoint jumps. Check if we already know
373 about a jump at this address. */
374 jp
= find_fast_tracepoint_jump_at (where
);
381 /* We don't, so create a new object. Double the length, because the
382 flexible array member holds both the jump insn, and the
384 jp
= xcalloc (1, sizeof (*jp
) + (length
* 2));
387 memcpy (fast_tracepoint_jump_insn (jp
), insn
, length
);
389 buf
= alloca (length
);
391 /* Note that there can be trap breakpoints inserted in the same
392 address range. To access the original memory contents, we use
393 `read_inferior_memory', which masks out breakpoints. */
394 err
= read_inferior_memory (where
, buf
, length
);
398 debug_printf ("Failed to read shadow memory of"
399 " fast tracepoint at 0x%s (%s).\n",
400 paddress (where
), strerror (err
));
404 memcpy (fast_tracepoint_jump_shadow (jp
), buf
, length
);
406 /* Link the jump in. */
408 jp
->next
= proc
->fast_tracepoint_jumps
;
409 proc
->fast_tracepoint_jumps
= jp
;
411 /* Since there can be trap breakpoints inserted in the same address
412 range, we use use `write_inferior_memory', which takes care of
413 layering breakpoints on top of fast tracepoints, on top of the
414 buffer we pass it. This works because we've already linked in
415 the fast tracepoint jump above. Also note that we need to pass
416 the current shadow contents, because write_inferior_memory
417 updates any shadow memory with what we pass here, and we want
419 err
= write_inferior_memory (where
, buf
, length
);
423 debug_printf ("Failed to insert fast tracepoint jump at 0x%s (%s).\n",
424 paddress (where
), strerror (err
));
427 proc
->fast_tracepoint_jumps
= jp
->next
;
437 uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc
)
439 struct fast_tracepoint_jump
*jp
;
442 jp
= find_fast_tracepoint_jump_at (pc
);
445 /* This can happen when we remove all breakpoints while handling
448 debug_printf ("Could not find fast tracepoint jump at 0x%s "
449 "in list (uninserting).\n",
460 /* Since there can be trap breakpoints inserted in the same
461 address range, we use use `write_inferior_memory', which
462 takes care of layering breakpoints on top of fast
463 tracepoints, and on top of the buffer we pass it. This works
464 because we've already marked the fast tracepoint fast
465 tracepoint jump uninserted above. Also note that we need to
466 pass the current shadow contents, because
467 write_inferior_memory updates any shadow memory with what we
468 pass here, and we want that to be a nop. */
469 buf
= alloca (jp
->length
);
470 memcpy (buf
, fast_tracepoint_jump_shadow (jp
), jp
->length
);
471 err
= write_inferior_memory (jp
->pc
, buf
, jp
->length
);
477 debug_printf ("Failed to uninsert fast tracepoint jump at"
479 paddress (pc
), strerror (err
));
485 reinsert_fast_tracepoint_jumps_at (CORE_ADDR where
)
487 struct fast_tracepoint_jump
*jp
;
491 jp
= find_fast_tracepoint_jump_at (where
);
494 /* This can happen when we remove breakpoints when a tracepoint
495 hit causes a tracing stop, while handling a step-over. */
497 debug_printf ("Could not find fast tracepoint jump at 0x%s "
498 "in list (reinserting).\n",
504 error ("Jump already inserted at reinsert time.");
508 /* Since there can be trap breakpoints inserted in the same address
509 range, we use `write_inferior_memory', which takes care of
510 layering breakpoints on top of fast tracepoints, and on top of
511 the buffer we pass it. This works because we've already marked
512 the fast tracepoint jump inserted above. Also note that we need
513 to pass the current shadow contents, because
514 write_inferior_memory updates any shadow memory with what we pass
515 here, and we want that to be a nop. */
516 buf
= alloca (jp
->length
);
517 memcpy (buf
, fast_tracepoint_jump_shadow (jp
), jp
->length
);
518 err
= write_inferior_memory (where
, buf
, jp
->length
);
524 debug_printf ("Failed to reinsert fast tracepoint jump at"
526 paddress (where
), strerror (err
));
531 set_breakpoint_at (CORE_ADDR where
, int (*handler
) (CORE_ADDR
))
533 struct process_info
*proc
= current_process ();
534 struct breakpoint
*bp
;
535 struct raw_breakpoint
*raw
;
537 raw
= set_raw_breakpoint_at (where
);
545 bp
= xcalloc (1, sizeof (struct breakpoint
));
546 bp
->type
= other_breakpoint
;
549 bp
->handler
= handler
;
551 bp
->next
= proc
->breakpoints
;
552 proc
->breakpoints
= bp
;
558 delete_raw_breakpoint (struct process_info
*proc
, struct raw_breakpoint
*todel
)
560 struct raw_breakpoint
*bp
, **bp_link
;
563 bp
= proc
->raw_breakpoints
;
564 bp_link
= &proc
->raw_breakpoints
;
572 struct raw_breakpoint
*prev_bp_link
= *bp_link
;
573 unsigned char buf
[MAX_BREAKPOINT_LEN
];
577 /* Since there can be trap breakpoints inserted in the
578 same address range, we use `write_inferior_memory',
579 which takes care of layering breakpoints on top of
580 fast tracepoints, and on top of the buffer we pass
581 it. This works because we've already unlinked the
582 fast tracepoint jump above. Also note that we need
583 to pass the current shadow contents, because
584 write_inferior_memory updates any shadow memory with
585 what we pass here, and we want that to be a nop. */
586 memcpy (buf
, bp
->old_data
, breakpoint_len
);
587 ret
= write_inferior_memory (bp
->pc
, buf
, breakpoint_len
);
590 /* Something went wrong, relink the breakpoint. */
591 *bp_link
= prev_bp_link
;
594 debug_printf ("Failed to uninsert raw breakpoint "
595 "at 0x%s (%s) while deleting it.\n",
596 paddress (bp
->pc
), strerror (ret
));
614 warning ("Could not find raw breakpoint in list.");
619 release_breakpoint (struct process_info
*proc
, struct breakpoint
*bp
)
624 newrefcount
= bp
->raw
->refcount
- 1;
625 if (newrefcount
== 0)
627 ret
= delete_raw_breakpoint (proc
, bp
->raw
);
632 bp
->raw
->refcount
= newrefcount
;
640 delete_breakpoint_1 (struct process_info
*proc
, struct breakpoint
*todel
)
642 struct breakpoint
*bp
, **bp_link
;
645 bp
= proc
->breakpoints
;
646 bp_link
= &proc
->breakpoints
;
654 err
= release_breakpoint (proc
, bp
);
668 warning ("Could not find breakpoint in list.");
673 delete_breakpoint (struct breakpoint
*todel
)
675 struct process_info
*proc
= current_process ();
676 return delete_breakpoint_1 (proc
, todel
);
680 find_gdb_breakpoint_at (CORE_ADDR where
)
682 struct process_info
*proc
= current_process ();
683 struct breakpoint
*bp
;
685 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
686 if (bp
->type
== gdb_breakpoint
&& bp
->raw
->pc
== where
)
693 set_gdb_breakpoint_at (CORE_ADDR where
)
695 struct breakpoint
*bp
;
697 if (breakpoint_data
== NULL
)
700 /* If we see GDB inserting a second breakpoint at the same address,
701 then the first breakpoint must have disappeared due to a shared
702 library unload. On targets where the shared libraries are
703 handled by userspace, like SVR4, for example, GDBserver can't
704 tell if a library was loaded or unloaded. Since we refcount
705 breakpoints, if we didn't do this, we'd just increase the
706 refcount of the previous breakpoint at this address, but the trap
707 was not planted in the inferior anymore, thus the breakpoint
708 would never be hit. */
709 bp
= find_gdb_breakpoint_at (where
);
712 delete_gdb_breakpoint_at (where
);
714 /* Might as well validate all other breakpoints. */
715 validate_breakpoints ();
718 bp
= set_breakpoint_at (where
, NULL
);
722 bp
->type
= gdb_breakpoint
;
727 delete_gdb_breakpoint_at (CORE_ADDR addr
)
729 struct breakpoint
*bp
;
732 if (breakpoint_data
== NULL
)
735 bp
= find_gdb_breakpoint_at (addr
);
739 /* Before deleting the breakpoint, make sure to free
740 its condition list. */
741 clear_gdb_breakpoint_conditions (addr
);
742 err
= delete_breakpoint (bp
);
749 /* Clear all conditions associated with this breakpoint address. */
752 clear_gdb_breakpoint_conditions (CORE_ADDR addr
)
754 struct breakpoint
*bp
= find_gdb_breakpoint_at (addr
);
755 struct point_cond_list
*cond
;
757 if (bp
== NULL
|| bp
->cond_list
== NULL
)
760 cond
= bp
->cond_list
;
764 struct point_cond_list
*cond_next
;
766 cond_next
= cond
->next
;
767 free (cond
->cond
->bytes
);
773 bp
->cond_list
= NULL
;
776 /* Add condition CONDITION to GDBserver's breakpoint BP. */
779 add_condition_to_breakpoint (struct breakpoint
*bp
,
780 struct agent_expr
*condition
)
782 struct point_cond_list
*new_cond
;
784 /* Create new condition. */
785 new_cond
= xcalloc (1, sizeof (*new_cond
));
786 new_cond
->cond
= condition
;
788 /* Add condition to the list. */
789 new_cond
->next
= bp
->cond_list
;
790 bp
->cond_list
= new_cond
;
793 /* Add a target-side condition CONDITION to the breakpoint at ADDR. */
796 add_breakpoint_condition (CORE_ADDR addr
, char **condition
)
798 struct breakpoint
*bp
= find_gdb_breakpoint_at (addr
);
799 char *actparm
= *condition
;
800 struct agent_expr
*cond
;
805 if (condition
== NULL
)
808 cond
= gdb_parse_agent_expr (&actparm
);
812 fprintf (stderr
, "Condition evaluation failed. "
813 "Assuming unconditional.\n");
817 add_condition_to_breakpoint (bp
, cond
);
819 *condition
= actparm
;
824 /* Evaluate condition (if any) at breakpoint BP. Return 1 if
825 true and 0 otherwise. */
828 gdb_condition_true_at_breakpoint (CORE_ADDR where
)
830 /* Fetch registers for the current inferior. */
831 struct breakpoint
*bp
= find_gdb_breakpoint_at (where
);
833 struct point_cond_list
*cl
;
835 struct eval_agent_expr_context ctx
;
840 /* Check if the breakpoint is unconditional. If it is,
841 the condition always evaluates to TRUE. */
842 if (bp
->cond_list
== NULL
)
845 ctx
.regcache
= get_thread_regcache (current_inferior
, 1);
849 /* Evaluate each condition in the breakpoint's list of conditions.
850 Return true if any of the conditions evaluates to TRUE.
852 If we failed to evaluate the expression, TRUE is returned. This
853 forces GDB to reevaluate the conditions. */
854 for (cl
= bp
->cond_list
;
855 cl
&& !value
&& !err
; cl
= cl
->next
)
857 /* Evaluate the condition. */
858 err
= gdb_eval_agent_expr (&ctx
, cl
->cond
, &value
);
867 /* Add commands COMMANDS to GDBserver's breakpoint BP. */
870 add_commands_to_breakpoint (struct breakpoint
*bp
,
871 struct agent_expr
*commands
, int persist
)
873 struct point_command_list
*new_cmd
;
875 /* Create new command. */
876 new_cmd
= xcalloc (1, sizeof (*new_cmd
));
877 new_cmd
->cmd
= commands
;
878 new_cmd
->persistence
= persist
;
880 /* Add commands to the list. */
881 new_cmd
->next
= bp
->command_list
;
882 bp
->command_list
= new_cmd
;
885 /* Add a target-side command COMMAND to the breakpoint at ADDR. */
888 add_breakpoint_commands (CORE_ADDR addr
, char **command
, int persist
)
890 struct breakpoint
*bp
= find_gdb_breakpoint_at (addr
);
891 char *actparm
= *command
;
892 struct agent_expr
*cmd
;
900 cmd
= gdb_parse_agent_expr (&actparm
);
904 fprintf (stderr
, "Command evaluation failed. "
909 add_commands_to_breakpoint (bp
, cmd
, persist
);
916 /* Return true if there are no commands to run at this location,
917 which likely means we want to report back to GDB. */
919 gdb_no_commands_at_breakpoint (CORE_ADDR where
)
921 struct breakpoint
*bp
= find_gdb_breakpoint_at (where
);
927 debug_printf ("at 0x%s, bp command_list is 0x%s\n",
929 phex_nz ((uintptr_t) bp
->command_list
, 0));
930 return (bp
->command_list
== NULL
);
934 run_breakpoint_commands (CORE_ADDR where
)
936 /* Fetch registers for the current inferior. */
937 struct breakpoint
*bp
= find_gdb_breakpoint_at (where
);
939 struct point_command_list
*cl
;
941 struct eval_agent_expr_context ctx
;
946 ctx
.regcache
= get_thread_regcache (current_inferior
, 1);
950 for (cl
= bp
->command_list
;
951 cl
&& !value
&& !err
; cl
= cl
->next
)
953 /* Run the command. */
954 err
= gdb_eval_agent_expr (&ctx
, cl
->cmd
, &value
);
956 /* If one command has a problem, stop digging the hole deeper. */
962 /* Return 1 if there is a breakpoint inserted in address WHERE
963 and if its condition, if it exists, is true. */
966 gdb_breakpoint_here (CORE_ADDR where
)
968 return (find_gdb_breakpoint_at (where
) != NULL
);
972 set_reinsert_breakpoint (CORE_ADDR stop_at
)
974 struct breakpoint
*bp
;
976 bp
= set_breakpoint_at (stop_at
, NULL
);
977 bp
->type
= reinsert_breakpoint
;
981 delete_reinsert_breakpoints (void)
983 struct process_info
*proc
= current_process ();
984 struct breakpoint
*bp
, **bp_link
;
986 bp
= proc
->breakpoints
;
987 bp_link
= &proc
->breakpoints
;
991 if (bp
->type
== reinsert_breakpoint
)
994 release_breakpoint (proc
, bp
);
1006 uninsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1011 unsigned char buf
[MAX_BREAKPOINT_LEN
];
1014 /* Since there can be fast tracepoint jumps inserted in the same
1015 address range, we use `write_inferior_memory', which takes
1016 care of layering breakpoints on top of fast tracepoints, and
1017 on top of the buffer we pass it. This works because we've
1018 already unlinked the fast tracepoint jump above. Also note
1019 that we need to pass the current shadow contents, because
1020 write_inferior_memory updates any shadow memory with what we
1021 pass here, and we want that to be a nop. */
1022 memcpy (buf
, bp
->old_data
, breakpoint_len
);
1023 err
= write_inferior_memory (bp
->pc
, buf
, breakpoint_len
);
1029 debug_printf ("Failed to uninsert raw breakpoint at 0x%s (%s).\n",
1030 paddress (bp
->pc
), strerror (err
));
1036 uninsert_breakpoints_at (CORE_ADDR pc
)
1038 struct raw_breakpoint
*bp
;
1040 bp
= find_raw_breakpoint_at (pc
);
1043 /* This can happen when we remove all breakpoints while handling
1046 debug_printf ("Could not find breakpoint at 0x%s "
1047 "in list (uninserting).\n",
1053 uninsert_raw_breakpoint (bp
);
1057 uninsert_all_breakpoints (void)
1059 struct process_info
*proc
= current_process ();
1060 struct raw_breakpoint
*bp
;
1062 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1064 uninsert_raw_breakpoint (bp
);
1068 reinsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1073 error ("Breakpoint already inserted at reinsert time.");
1075 err
= (*the_target
->write_memory
) (bp
->pc
, breakpoint_data
,
1079 else if (debug_threads
)
1080 debug_printf ("Failed to reinsert breakpoint at 0x%s (%s).\n",
1081 paddress (bp
->pc
), strerror (err
));
1085 reinsert_breakpoints_at (CORE_ADDR pc
)
1087 struct raw_breakpoint
*bp
;
1089 bp
= find_raw_breakpoint_at (pc
);
1092 /* This can happen when we remove all breakpoints while handling
1095 debug_printf ("Could not find raw breakpoint at 0x%s "
1096 "in list (reinserting).\n",
1101 reinsert_raw_breakpoint (bp
);
1105 reinsert_all_breakpoints (void)
1107 struct process_info
*proc
= current_process ();
1108 struct raw_breakpoint
*bp
;
1110 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1112 reinsert_raw_breakpoint (bp
);
1116 check_breakpoints (CORE_ADDR stop_pc
)
1118 struct process_info
*proc
= current_process ();
1119 struct breakpoint
*bp
, **bp_link
;
1121 bp
= proc
->breakpoints
;
1122 bp_link
= &proc
->breakpoints
;
1126 if (bp
->raw
->pc
== stop_pc
)
1128 if (!bp
->raw
->inserted
)
1130 warning ("Hit a removed breakpoint?");
1134 if (bp
->handler
!= NULL
&& (*bp
->handler
) (stop_pc
))
1136 *bp_link
= bp
->next
;
1138 release_breakpoint (proc
, bp
);
1145 bp_link
= &bp
->next
;
1151 set_breakpoint_data (const unsigned char *bp_data
, int bp_len
)
1153 breakpoint_data
= bp_data
;
1154 breakpoint_len
= bp_len
;
1158 breakpoint_here (CORE_ADDR addr
)
1160 return (find_raw_breakpoint_at (addr
) != NULL
);
1164 breakpoint_inserted_here (CORE_ADDR addr
)
1166 struct raw_breakpoint
*bp
;
1168 bp
= find_raw_breakpoint_at (addr
);
1170 return (bp
!= NULL
&& bp
->inserted
);
1174 validate_inserted_breakpoint (struct raw_breakpoint
*bp
)
1179 gdb_assert (bp
->inserted
);
1181 buf
= alloca (breakpoint_len
);
1182 err
= (*the_target
->read_memory
) (bp
->pc
, buf
, breakpoint_len
);
1183 if (err
|| memcmp (buf
, breakpoint_data
, breakpoint_len
) != 0)
1185 /* Tag it as gone. */
1187 bp
->shlib_disabled
= 1;
1195 delete_disabled_breakpoints (void)
1197 struct process_info
*proc
= current_process ();
1198 struct breakpoint
*bp
, *next
;
1200 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= next
)
1203 if (bp
->raw
->shlib_disabled
)
1204 delete_breakpoint_1 (proc
, bp
);
1208 /* Check if breakpoints we inserted still appear to be inserted. They
1209 may disappear due to a shared library unload, and worse, a new
1210 shared library may be reloaded at the same address as the
1211 previously unloaded one. If that happens, we should make sure that
1212 the shadow memory of the old breakpoints isn't used when reading or
1216 validate_breakpoints (void)
1218 struct process_info
*proc
= current_process ();
1219 struct breakpoint
*bp
;
1221 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1223 if (bp
->raw
->inserted
)
1224 validate_inserted_breakpoint (bp
->raw
);
1227 delete_disabled_breakpoints ();
1231 check_mem_read (CORE_ADDR mem_addr
, unsigned char *buf
, int mem_len
)
1233 struct process_info
*proc
= current_process ();
1234 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
1235 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
1236 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
1237 int disabled_one
= 0;
1239 for (; jp
!= NULL
; jp
= jp
->next
)
1241 CORE_ADDR bp_end
= jp
->pc
+ jp
->length
;
1242 CORE_ADDR start
, end
;
1243 int copy_offset
, copy_len
, buf_offset
;
1245 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= buf
+ mem_len
1246 || buf
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
1248 if (mem_addr
>= bp_end
)
1250 if (jp
->pc
>= mem_end
)
1254 if (mem_addr
> start
)
1261 copy_len
= end
- start
;
1262 copy_offset
= start
- jp
->pc
;
1263 buf_offset
= start
- mem_addr
;
1266 memcpy (buf
+ buf_offset
,
1267 fast_tracepoint_jump_shadow (jp
) + copy_offset
,
1271 for (; bp
!= NULL
; bp
= bp
->next
)
1273 CORE_ADDR bp_end
= bp
->pc
+ breakpoint_len
;
1274 CORE_ADDR start
, end
;
1275 int copy_offset
, copy_len
, buf_offset
;
1277 gdb_assert (bp
->old_data
>= buf
+ mem_len
1278 || buf
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
1280 if (mem_addr
>= bp_end
)
1282 if (bp
->pc
>= mem_end
)
1286 if (mem_addr
> start
)
1293 copy_len
= end
- start
;
1294 copy_offset
= start
- bp
->pc
;
1295 buf_offset
= start
- mem_addr
;
1299 if (validate_inserted_breakpoint (bp
))
1300 memcpy (buf
+ buf_offset
, bp
->old_data
+ copy_offset
, copy_len
);
1307 delete_disabled_breakpoints ();
1311 check_mem_write (CORE_ADDR mem_addr
, unsigned char *buf
,
1312 const unsigned char *myaddr
, int mem_len
)
1314 struct process_info
*proc
= current_process ();
1315 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
1316 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
1317 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
1318 int disabled_one
= 0;
1320 /* First fast tracepoint jumps, then breakpoint traps on top. */
1322 for (; jp
!= NULL
; jp
= jp
->next
)
1324 CORE_ADDR jp_end
= jp
->pc
+ jp
->length
;
1325 CORE_ADDR start
, end
;
1326 int copy_offset
, copy_len
, buf_offset
;
1328 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= myaddr
+ mem_len
1329 || myaddr
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
1330 gdb_assert (fast_tracepoint_jump_insn (jp
) >= buf
+ mem_len
1331 || buf
>= fast_tracepoint_jump_insn (jp
) + (jp
)->length
);
1333 if (mem_addr
>= jp_end
)
1335 if (jp
->pc
>= mem_end
)
1339 if (mem_addr
> start
)
1346 copy_len
= end
- start
;
1347 copy_offset
= start
- jp
->pc
;
1348 buf_offset
= start
- mem_addr
;
1350 memcpy (fast_tracepoint_jump_shadow (jp
) + copy_offset
,
1351 myaddr
+ buf_offset
, copy_len
);
1353 memcpy (buf
+ buf_offset
,
1354 fast_tracepoint_jump_insn (jp
) + copy_offset
, copy_len
);
1357 for (; bp
!= NULL
; bp
= bp
->next
)
1359 CORE_ADDR bp_end
= bp
->pc
+ breakpoint_len
;
1360 CORE_ADDR start
, end
;
1361 int copy_offset
, copy_len
, buf_offset
;
1363 gdb_assert (bp
->old_data
>= myaddr
+ mem_len
1364 || myaddr
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
1366 if (mem_addr
>= bp_end
)
1368 if (bp
->pc
>= mem_end
)
1372 if (mem_addr
> start
)
1379 copy_len
= end
- start
;
1380 copy_offset
= start
- bp
->pc
;
1381 buf_offset
= start
- mem_addr
;
1383 memcpy (bp
->old_data
+ copy_offset
, myaddr
+ buf_offset
, copy_len
);
1386 if (validate_inserted_breakpoint (bp
))
1387 memcpy (buf
+ buf_offset
, breakpoint_data
+ copy_offset
, copy_len
);
1394 delete_disabled_breakpoints ();
1397 /* Delete all breakpoints, and un-insert them from the inferior. */
1400 delete_all_breakpoints (void)
1402 struct process_info
*proc
= current_process ();
1404 while (proc
->breakpoints
)
1405 delete_breakpoint_1 (proc
, proc
->breakpoints
);
1408 /* Clear the "inserted" flag in all breakpoints. */
1411 mark_breakpoints_out (struct process_info
*proc
)
1413 struct raw_breakpoint
*raw_bp
;
1415 for (raw_bp
= proc
->raw_breakpoints
; raw_bp
!= NULL
; raw_bp
= raw_bp
->next
)
1416 raw_bp
->inserted
= 0;
1419 /* Release all breakpoints, but do not try to un-insert them from the
1423 free_all_breakpoints (struct process_info
*proc
)
1425 mark_breakpoints_out (proc
);
1427 /* Note: use PROC explicitly instead of deferring to
1428 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
1429 released when we get here. There should be no call to
1430 current_process from here on. */
1431 while (proc
->breakpoints
)
1432 delete_breakpoint_1 (proc
, proc
->breakpoints
);