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
);
679 /* Locate a breakpoint placed at address WHERE and return a pointer
682 static struct breakpoint
*
683 find_gdb_breakpoint_at (CORE_ADDR where
)
685 struct process_info
*proc
= current_process ();
686 struct breakpoint
*bp
;
688 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
689 if (bp
->type
== gdb_breakpoint
&& bp
->raw
->pc
== where
)
696 set_gdb_breakpoint_at (CORE_ADDR where
)
698 struct breakpoint
*bp
;
700 if (breakpoint_data
== NULL
)
703 /* If we see GDB inserting a second breakpoint at the same address,
704 then the first breakpoint must have disappeared due to a shared
705 library unload. On targets where the shared libraries are
706 handled by userspace, like SVR4, for example, GDBserver can't
707 tell if a library was loaded or unloaded. Since we refcount
708 breakpoints, if we didn't do this, we'd just increase the
709 refcount of the previous breakpoint at this address, but the trap
710 was not planted in the inferior anymore, thus the breakpoint
711 would never be hit. */
712 bp
= find_gdb_breakpoint_at (where
);
715 delete_gdb_breakpoint_at (where
);
717 /* Might as well validate all other breakpoints. */
718 validate_breakpoints ();
721 bp
= set_breakpoint_at (where
, NULL
);
725 bp
->type
= gdb_breakpoint
;
730 delete_gdb_breakpoint_at (CORE_ADDR addr
)
732 struct breakpoint
*bp
;
735 if (breakpoint_data
== NULL
)
738 bp
= find_gdb_breakpoint_at (addr
);
742 /* Before deleting the breakpoint, make sure to free
743 its condition list. */
744 clear_gdb_breakpoint_conditions (addr
);
745 err
= delete_breakpoint (bp
);
752 /* Clear all conditions associated with this breakpoint address. */
755 clear_gdb_breakpoint_conditions (CORE_ADDR addr
)
757 struct breakpoint
*bp
= find_gdb_breakpoint_at (addr
);
758 struct point_cond_list
*cond
;
760 if (bp
== NULL
|| bp
->cond_list
== NULL
)
763 cond
= bp
->cond_list
;
767 struct point_cond_list
*cond_next
;
769 cond_next
= cond
->next
;
770 free (cond
->cond
->bytes
);
776 bp
->cond_list
= NULL
;
779 /* Add condition CONDITION to GDBserver's breakpoint BP. */
782 add_condition_to_breakpoint (struct breakpoint
*bp
,
783 struct agent_expr
*condition
)
785 struct point_cond_list
*new_cond
;
787 /* Create new condition. */
788 new_cond
= xcalloc (1, sizeof (*new_cond
));
789 new_cond
->cond
= condition
;
791 /* Add condition to the list. */
792 new_cond
->next
= bp
->cond_list
;
793 bp
->cond_list
= new_cond
;
796 /* Add a target-side condition CONDITION to the breakpoint at ADDR. */
799 add_breakpoint_condition (CORE_ADDR addr
, char **condition
)
801 struct breakpoint
*bp
= find_gdb_breakpoint_at (addr
);
802 char *actparm
= *condition
;
803 struct agent_expr
*cond
;
805 if (condition
== NULL
)
811 cond
= gdb_parse_agent_expr (&actparm
);
815 fprintf (stderr
, "Condition evaluation failed. "
816 "Assuming unconditional.\n");
820 add_condition_to_breakpoint (bp
, cond
);
822 *condition
= actparm
;
827 /* Evaluate condition (if any) at breakpoint BP. Return 1 if
828 true and 0 otherwise. */
831 gdb_condition_true_at_breakpoint (CORE_ADDR where
)
833 /* Fetch registers for the current inferior. */
834 struct breakpoint
*bp
= find_gdb_breakpoint_at (where
);
836 struct point_cond_list
*cl
;
838 struct eval_agent_expr_context ctx
;
843 /* Check if the breakpoint is unconditional. If it is,
844 the condition always evaluates to TRUE. */
845 if (bp
->cond_list
== NULL
)
848 ctx
.regcache
= get_thread_regcache (current_inferior
, 1);
852 /* Evaluate each condition in the breakpoint's list of conditions.
853 Return true if any of the conditions evaluates to TRUE.
855 If we failed to evaluate the expression, TRUE is returned. This
856 forces GDB to reevaluate the conditions. */
857 for (cl
= bp
->cond_list
;
858 cl
&& !value
&& !err
; cl
= cl
->next
)
860 /* Evaluate the condition. */
861 err
= gdb_eval_agent_expr (&ctx
, cl
->cond
, &value
);
870 /* Add commands COMMANDS to GDBserver's breakpoint BP. */
873 add_commands_to_breakpoint (struct breakpoint
*bp
,
874 struct agent_expr
*commands
, int persist
)
876 struct point_command_list
*new_cmd
;
878 /* Create new command. */
879 new_cmd
= xcalloc (1, sizeof (*new_cmd
));
880 new_cmd
->cmd
= commands
;
881 new_cmd
->persistence
= persist
;
883 /* Add commands to the list. */
884 new_cmd
->next
= bp
->command_list
;
885 bp
->command_list
= new_cmd
;
888 /* Add a target-side command COMMAND to the breakpoint at ADDR. */
891 add_breakpoint_commands (CORE_ADDR addr
, char **command
, int persist
)
893 struct breakpoint
*bp
= find_gdb_breakpoint_at (addr
);
894 char *actparm
= *command
;
895 struct agent_expr
*cmd
;
903 cmd
= gdb_parse_agent_expr (&actparm
);
907 fprintf (stderr
, "Command evaluation failed. "
912 add_commands_to_breakpoint (bp
, cmd
, persist
);
919 /* Return true if there are no commands to run at this location,
920 which likely means we want to report back to GDB. */
922 gdb_no_commands_at_breakpoint (CORE_ADDR where
)
924 struct breakpoint
*bp
= find_gdb_breakpoint_at (where
);
930 debug_printf ("at 0x%s, bp command_list is 0x%s\n",
932 phex_nz ((uintptr_t) bp
->command_list
, 0));
933 return (bp
->command_list
== NULL
);
937 run_breakpoint_commands (CORE_ADDR where
)
939 /* Fetch registers for the current inferior. */
940 struct breakpoint
*bp
= find_gdb_breakpoint_at (where
);
942 struct point_command_list
*cl
;
944 struct eval_agent_expr_context ctx
;
949 ctx
.regcache
= get_thread_regcache (current_inferior
, 1);
953 for (cl
= bp
->command_list
;
954 cl
&& !value
&& !err
; cl
= cl
->next
)
956 /* Run the command. */
957 err
= gdb_eval_agent_expr (&ctx
, cl
->cmd
, &value
);
959 /* If one command has a problem, stop digging the hole deeper. */
965 /* Return 1 if there is a breakpoint inserted in address WHERE
966 and if its condition, if it exists, is true. */
969 gdb_breakpoint_here (CORE_ADDR where
)
971 return (find_gdb_breakpoint_at (where
) != NULL
);
975 set_reinsert_breakpoint (CORE_ADDR stop_at
)
977 struct breakpoint
*bp
;
979 bp
= set_breakpoint_at (stop_at
, NULL
);
980 bp
->type
= reinsert_breakpoint
;
984 delete_reinsert_breakpoints (void)
986 struct process_info
*proc
= current_process ();
987 struct breakpoint
*bp
, **bp_link
;
989 bp
= proc
->breakpoints
;
990 bp_link
= &proc
->breakpoints
;
994 if (bp
->type
== reinsert_breakpoint
)
997 release_breakpoint (proc
, bp
);
1002 bp_link
= &bp
->next
;
1009 uninsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1014 unsigned char buf
[MAX_BREAKPOINT_LEN
];
1017 /* Since there can be fast tracepoint jumps inserted in the same
1018 address range, we use `write_inferior_memory', which takes
1019 care of layering breakpoints on top of fast tracepoints, and
1020 on top of the buffer we pass it. This works because we've
1021 already unlinked the fast tracepoint jump above. Also note
1022 that we need to pass the current shadow contents, because
1023 write_inferior_memory updates any shadow memory with what we
1024 pass here, and we want that to be a nop. */
1025 memcpy (buf
, bp
->old_data
, breakpoint_len
);
1026 err
= write_inferior_memory (bp
->pc
, buf
, breakpoint_len
);
1032 debug_printf ("Failed to uninsert raw breakpoint at 0x%s (%s).\n",
1033 paddress (bp
->pc
), strerror (err
));
1039 uninsert_breakpoints_at (CORE_ADDR pc
)
1041 struct raw_breakpoint
*bp
;
1043 bp
= find_raw_breakpoint_at (pc
);
1046 /* This can happen when we remove all breakpoints while handling
1049 debug_printf ("Could not find breakpoint at 0x%s "
1050 "in list (uninserting).\n",
1056 uninsert_raw_breakpoint (bp
);
1060 uninsert_all_breakpoints (void)
1062 struct process_info
*proc
= current_process ();
1063 struct raw_breakpoint
*bp
;
1065 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1067 uninsert_raw_breakpoint (bp
);
1071 reinsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1076 error ("Breakpoint already inserted at reinsert time.");
1078 err
= (*the_target
->write_memory
) (bp
->pc
, breakpoint_data
,
1082 else if (debug_threads
)
1083 debug_printf ("Failed to reinsert breakpoint at 0x%s (%s).\n",
1084 paddress (bp
->pc
), strerror (err
));
1088 reinsert_breakpoints_at (CORE_ADDR pc
)
1090 struct raw_breakpoint
*bp
;
1092 bp
= find_raw_breakpoint_at (pc
);
1095 /* This can happen when we remove all breakpoints while handling
1098 debug_printf ("Could not find raw breakpoint at 0x%s "
1099 "in list (reinserting).\n",
1104 reinsert_raw_breakpoint (bp
);
1108 reinsert_all_breakpoints (void)
1110 struct process_info
*proc
= current_process ();
1111 struct raw_breakpoint
*bp
;
1113 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1115 reinsert_raw_breakpoint (bp
);
1119 check_breakpoints (CORE_ADDR stop_pc
)
1121 struct process_info
*proc
= current_process ();
1122 struct breakpoint
*bp
, **bp_link
;
1124 bp
= proc
->breakpoints
;
1125 bp_link
= &proc
->breakpoints
;
1129 if (bp
->raw
->pc
== stop_pc
)
1131 if (!bp
->raw
->inserted
)
1133 warning ("Hit a removed breakpoint?");
1137 if (bp
->handler
!= NULL
&& (*bp
->handler
) (stop_pc
))
1139 *bp_link
= bp
->next
;
1141 release_breakpoint (proc
, bp
);
1148 bp_link
= &bp
->next
;
1154 set_breakpoint_data (const unsigned char *bp_data
, int bp_len
)
1156 breakpoint_data
= bp_data
;
1157 breakpoint_len
= bp_len
;
1161 breakpoint_here (CORE_ADDR addr
)
1163 return (find_raw_breakpoint_at (addr
) != NULL
);
1167 breakpoint_inserted_here (CORE_ADDR addr
)
1169 struct raw_breakpoint
*bp
;
1171 bp
= find_raw_breakpoint_at (addr
);
1173 return (bp
!= NULL
&& bp
->inserted
);
1177 validate_inserted_breakpoint (struct raw_breakpoint
*bp
)
1182 gdb_assert (bp
->inserted
);
1184 buf
= alloca (breakpoint_len
);
1185 err
= (*the_target
->read_memory
) (bp
->pc
, buf
, breakpoint_len
);
1186 if (err
|| memcmp (buf
, breakpoint_data
, breakpoint_len
) != 0)
1188 /* Tag it as gone. */
1190 bp
->shlib_disabled
= 1;
1198 delete_disabled_breakpoints (void)
1200 struct process_info
*proc
= current_process ();
1201 struct breakpoint
*bp
, *next
;
1203 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= next
)
1206 if (bp
->raw
->shlib_disabled
)
1207 delete_breakpoint_1 (proc
, bp
);
1211 /* Check if breakpoints we inserted still appear to be inserted. They
1212 may disappear due to a shared library unload, and worse, a new
1213 shared library may be reloaded at the same address as the
1214 previously unloaded one. If that happens, we should make sure that
1215 the shadow memory of the old breakpoints isn't used when reading or
1219 validate_breakpoints (void)
1221 struct process_info
*proc
= current_process ();
1222 struct breakpoint
*bp
;
1224 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1226 if (bp
->raw
->inserted
)
1227 validate_inserted_breakpoint (bp
->raw
);
1230 delete_disabled_breakpoints ();
1234 check_mem_read (CORE_ADDR mem_addr
, unsigned char *buf
, int mem_len
)
1236 struct process_info
*proc
= current_process ();
1237 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
1238 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
1239 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
1240 int disabled_one
= 0;
1242 for (; jp
!= NULL
; jp
= jp
->next
)
1244 CORE_ADDR bp_end
= jp
->pc
+ jp
->length
;
1245 CORE_ADDR start
, end
;
1246 int copy_offset
, copy_len
, buf_offset
;
1248 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= buf
+ mem_len
1249 || buf
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
1251 if (mem_addr
>= bp_end
)
1253 if (jp
->pc
>= mem_end
)
1257 if (mem_addr
> start
)
1264 copy_len
= end
- start
;
1265 copy_offset
= start
- jp
->pc
;
1266 buf_offset
= start
- mem_addr
;
1269 memcpy (buf
+ buf_offset
,
1270 fast_tracepoint_jump_shadow (jp
) + copy_offset
,
1274 for (; bp
!= NULL
; bp
= bp
->next
)
1276 CORE_ADDR bp_end
= bp
->pc
+ breakpoint_len
;
1277 CORE_ADDR start
, end
;
1278 int copy_offset
, copy_len
, buf_offset
;
1280 gdb_assert (bp
->old_data
>= buf
+ mem_len
1281 || buf
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
1283 if (mem_addr
>= bp_end
)
1285 if (bp
->pc
>= mem_end
)
1289 if (mem_addr
> start
)
1296 copy_len
= end
- start
;
1297 copy_offset
= start
- bp
->pc
;
1298 buf_offset
= start
- mem_addr
;
1302 if (validate_inserted_breakpoint (bp
))
1303 memcpy (buf
+ buf_offset
, bp
->old_data
+ copy_offset
, copy_len
);
1310 delete_disabled_breakpoints ();
1314 check_mem_write (CORE_ADDR mem_addr
, unsigned char *buf
,
1315 const unsigned char *myaddr
, int mem_len
)
1317 struct process_info
*proc
= current_process ();
1318 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
1319 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
1320 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
1321 int disabled_one
= 0;
1323 /* First fast tracepoint jumps, then breakpoint traps on top. */
1325 for (; jp
!= NULL
; jp
= jp
->next
)
1327 CORE_ADDR jp_end
= jp
->pc
+ jp
->length
;
1328 CORE_ADDR start
, end
;
1329 int copy_offset
, copy_len
, buf_offset
;
1331 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= myaddr
+ mem_len
1332 || myaddr
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
1333 gdb_assert (fast_tracepoint_jump_insn (jp
) >= buf
+ mem_len
1334 || buf
>= fast_tracepoint_jump_insn (jp
) + (jp
)->length
);
1336 if (mem_addr
>= jp_end
)
1338 if (jp
->pc
>= mem_end
)
1342 if (mem_addr
> start
)
1349 copy_len
= end
- start
;
1350 copy_offset
= start
- jp
->pc
;
1351 buf_offset
= start
- mem_addr
;
1353 memcpy (fast_tracepoint_jump_shadow (jp
) + copy_offset
,
1354 myaddr
+ buf_offset
, copy_len
);
1356 memcpy (buf
+ buf_offset
,
1357 fast_tracepoint_jump_insn (jp
) + copy_offset
, copy_len
);
1360 for (; bp
!= NULL
; bp
= bp
->next
)
1362 CORE_ADDR bp_end
= bp
->pc
+ breakpoint_len
;
1363 CORE_ADDR start
, end
;
1364 int copy_offset
, copy_len
, buf_offset
;
1366 gdb_assert (bp
->old_data
>= myaddr
+ mem_len
1367 || myaddr
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
1369 if (mem_addr
>= bp_end
)
1371 if (bp
->pc
>= mem_end
)
1375 if (mem_addr
> start
)
1382 copy_len
= end
- start
;
1383 copy_offset
= start
- bp
->pc
;
1384 buf_offset
= start
- mem_addr
;
1386 memcpy (bp
->old_data
+ copy_offset
, myaddr
+ buf_offset
, copy_len
);
1389 if (validate_inserted_breakpoint (bp
))
1390 memcpy (buf
+ buf_offset
, breakpoint_data
+ copy_offset
, copy_len
);
1397 delete_disabled_breakpoints ();
1400 /* Delete all breakpoints, and un-insert them from the inferior. */
1403 delete_all_breakpoints (void)
1405 struct process_info
*proc
= current_process ();
1407 while (proc
->breakpoints
)
1408 delete_breakpoint_1 (proc
, proc
->breakpoints
);
1411 /* Clear the "inserted" flag in all breakpoints. */
1414 mark_breakpoints_out (struct process_info
*proc
)
1416 struct raw_breakpoint
*raw_bp
;
1418 for (raw_bp
= proc
->raw_breakpoints
; raw_bp
!= NULL
; raw_bp
= raw_bp
->next
)
1419 raw_bp
->inserted
= 0;
1422 /* Release all breakpoints, but do not try to un-insert them from the
1426 free_all_breakpoints (struct process_info
*proc
)
1428 mark_breakpoints_out (proc
);
1430 /* Note: use PROC explicitly instead of deferring to
1431 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
1432 released when we get here. There should be no call to
1433 current_process from here on. */
1434 while (proc
->breakpoints
)
1435 delete_breakpoint_1 (proc
, proc
->breakpoints
);