1 /* Memory breakpoint operations for the remote server for GDB.
2 Copyright (C) 2002-2013 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
);
201 "Failed to read shadow memory of"
202 " breakpoint at 0x%s (%s).\n",
203 paddress (where
), strerror (err
));
207 memcpy (bp
->old_data
, buf
, breakpoint_len
);
209 err
= (*the_target
->write_memory
) (where
, breakpoint_data
,
215 "Failed to insert breakpoint at 0x%s (%s).\n",
216 paddress (where
), strerror (err
));
221 /* Link the breakpoint in. */
223 bp
->next
= proc
->raw_breakpoints
;
224 proc
->raw_breakpoints
= bp
;
228 /* Notice that breakpoint traps are always installed on top of fast
229 tracepoint jumps. This is even if the fast tracepoint is installed
230 at a later time compared to when the breakpoint was installed.
231 This means that a stopping breakpoint or tracepoint has higher
232 "priority". In turn, this allows having fast and slow tracepoints
233 (and breakpoints) at the same address behave correctly. */
236 /* A fast tracepoint jump. */
238 struct fast_tracepoint_jump
240 struct fast_tracepoint_jump
*next
;
242 /* A reference count. GDB can install more than one fast tracepoint
243 at the same address (each with its own action list, for
247 /* The fast tracepoint's insertion address. There can only be one
248 of these for a given PC. */
251 /* Non-zero if this fast tracepoint jump is currently inserted in
255 /* The length of the jump instruction. */
258 /* A poor-man's flexible array member, holding both the jump
259 instruction to insert, and a copy of the instruction that would
260 be in memory had not been a jump there (the shadow memory of the
262 unsigned char insn_and_shadow
[0];
265 /* Fast tracepoint FP's jump instruction to insert. */
266 #define fast_tracepoint_jump_insn(fp) \
267 ((fp)->insn_and_shadow + 0)
269 /* The shadow memory of fast tracepoint jump FP. */
270 #define fast_tracepoint_jump_shadow(fp) \
271 ((fp)->insn_and_shadow + (fp)->length)
274 /* Return the fast tracepoint jump set at WHERE. */
276 static struct fast_tracepoint_jump
*
277 find_fast_tracepoint_jump_at (CORE_ADDR where
)
279 struct process_info
*proc
= current_process ();
280 struct fast_tracepoint_jump
*jp
;
282 for (jp
= proc
->fast_tracepoint_jumps
; jp
!= NULL
; jp
= jp
->next
)
290 fast_tracepoint_jump_here (CORE_ADDR where
)
292 struct fast_tracepoint_jump
*jp
= find_fast_tracepoint_jump_at (where
);
298 delete_fast_tracepoint_jump (struct fast_tracepoint_jump
*todel
)
300 struct fast_tracepoint_jump
*bp
, **bp_link
;
302 struct process_info
*proc
= current_process ();
304 bp
= proc
->fast_tracepoint_jumps
;
305 bp_link
= &proc
->fast_tracepoint_jumps
;
311 if (--bp
->refcount
== 0)
313 struct fast_tracepoint_jump
*prev_bp_link
= *bp_link
;
319 /* Since there can be breakpoints inserted in the same
320 address range, we use `write_inferior_memory', which
321 takes care of layering breakpoints on top of fast
322 tracepoints, and on top of the buffer we pass it.
323 This works because we've already unlinked the fast
324 tracepoint jump above. Also note that we need to
325 pass the current shadow contents, because
326 write_inferior_memory updates any shadow memory with
327 what we pass here, and we want that to be a nop. */
328 buf
= alloca (bp
->length
);
329 memcpy (buf
, fast_tracepoint_jump_shadow (bp
), bp
->length
);
330 ret
= write_inferior_memory (bp
->pc
, buf
, bp
->length
);
333 /* Something went wrong, relink the jump. */
334 *bp_link
= prev_bp_link
;
338 "Failed to uninsert fast tracepoint jump "
339 "at 0x%s (%s) while deleting it.\n",
340 paddress (bp
->pc
), strerror (ret
));
356 warning ("Could not find fast tracepoint jump in list.");
361 inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump
*jp
)
366 struct fast_tracepoint_jump
*
367 set_fast_tracepoint_jump (CORE_ADDR where
,
368 unsigned char *insn
, ULONGEST length
)
370 struct process_info
*proc
= current_process ();
371 struct fast_tracepoint_jump
*jp
;
375 /* We refcount fast tracepoint jumps. Check if we already know
376 about a jump at this address. */
377 jp
= find_fast_tracepoint_jump_at (where
);
384 /* We don't, so create a new object. Double the length, because the
385 flexible array member holds both the jump insn, and the
387 jp
= xcalloc (1, sizeof (*jp
) + (length
* 2));
390 memcpy (fast_tracepoint_jump_insn (jp
), insn
, length
);
392 buf
= alloca (length
);
394 /* Note that there can be trap breakpoints inserted in the same
395 address range. To access the original memory contents, we use
396 `read_inferior_memory', which masks out breakpoints. */
397 err
= read_inferior_memory (where
, buf
, length
);
402 "Failed to read shadow memory of"
403 " fast tracepoint at 0x%s (%s).\n",
404 paddress (where
), strerror (err
));
408 memcpy (fast_tracepoint_jump_shadow (jp
), buf
, length
);
410 /* Link the jump in. */
412 jp
->next
= proc
->fast_tracepoint_jumps
;
413 proc
->fast_tracepoint_jumps
= jp
;
415 /* Since there can be trap breakpoints inserted in the same address
416 range, we use use `write_inferior_memory', which takes care of
417 layering breakpoints on top of fast tracepoints, on top of the
418 buffer we pass it. This works because we've already linked in
419 the fast tracepoint jump above. Also note that we need to pass
420 the current shadow contents, because write_inferior_memory
421 updates any shadow memory with what we pass here, and we want
423 err
= write_inferior_memory (where
, buf
, length
);
428 "Failed to insert fast tracepoint jump at 0x%s (%s).\n",
429 paddress (where
), strerror (err
));
432 proc
->fast_tracepoint_jumps
= jp
->next
;
442 uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc
)
444 struct fast_tracepoint_jump
*jp
;
447 jp
= find_fast_tracepoint_jump_at (pc
);
450 /* This can happen when we remove all breakpoints while handling
454 "Could not find fast tracepoint jump at 0x%s "
455 "in list (uninserting).\n",
466 /* Since there can be trap breakpoints inserted in the same
467 address range, we use use `write_inferior_memory', which
468 takes care of layering breakpoints on top of fast
469 tracepoints, and on top of the buffer we pass it. This works
470 because we've already marked the fast tracepoint fast
471 tracepoint jump uninserted above. Also note that we need to
472 pass the current shadow contents, because
473 write_inferior_memory updates any shadow memory with what we
474 pass here, and we want that to be a nop. */
475 buf
= alloca (jp
->length
);
476 memcpy (buf
, fast_tracepoint_jump_shadow (jp
), jp
->length
);
477 err
= write_inferior_memory (jp
->pc
, buf
, jp
->length
);
484 "Failed to uninsert fast tracepoint jump at 0x%s (%s).\n",
485 paddress (pc
), strerror (err
));
491 reinsert_fast_tracepoint_jumps_at (CORE_ADDR where
)
493 struct fast_tracepoint_jump
*jp
;
497 jp
= find_fast_tracepoint_jump_at (where
);
500 /* This can happen when we remove breakpoints when a tracepoint
501 hit causes a tracing stop, while handling a step-over. */
504 "Could not find fast tracepoint jump at 0x%s "
505 "in list (reinserting).\n",
511 error ("Jump already inserted at reinsert time.");
515 /* Since there can be trap breakpoints inserted in the same address
516 range, we use `write_inferior_memory', which takes care of
517 layering breakpoints on top of fast tracepoints, and on top of
518 the buffer we pass it. This works because we've already marked
519 the fast tracepoint jump inserted above. Also note that we need
520 to pass the current shadow contents, because
521 write_inferior_memory updates any shadow memory with what we pass
522 here, and we want that to be a nop. */
523 buf
= alloca (jp
->length
);
524 memcpy (buf
, fast_tracepoint_jump_shadow (jp
), jp
->length
);
525 err
= write_inferior_memory (where
, buf
, jp
->length
);
532 "Failed to reinsert fast tracepoint jump at 0x%s (%s).\n",
533 paddress (where
), strerror (err
));
538 set_breakpoint_at (CORE_ADDR where
, int (*handler
) (CORE_ADDR
))
540 struct process_info
*proc
= current_process ();
541 struct breakpoint
*bp
;
542 struct raw_breakpoint
*raw
;
544 raw
= set_raw_breakpoint_at (where
);
552 bp
= xcalloc (1, sizeof (struct breakpoint
));
553 bp
->type
= other_breakpoint
;
556 bp
->handler
= handler
;
558 bp
->next
= proc
->breakpoints
;
559 proc
->breakpoints
= bp
;
565 delete_raw_breakpoint (struct process_info
*proc
, struct raw_breakpoint
*todel
)
567 struct raw_breakpoint
*bp
, **bp_link
;
570 bp
= proc
->raw_breakpoints
;
571 bp_link
= &proc
->raw_breakpoints
;
579 struct raw_breakpoint
*prev_bp_link
= *bp_link
;
580 unsigned char buf
[MAX_BREAKPOINT_LEN
];
584 /* Since there can be trap breakpoints inserted in the
585 same address range, we use `write_inferior_memory',
586 which takes care of layering breakpoints on top of
587 fast tracepoints, and on top of the buffer we pass
588 it. This works because we've already unlinked the
589 fast tracepoint jump above. Also note that we need
590 to pass the current shadow contents, because
591 write_inferior_memory updates any shadow memory with
592 what we pass here, and we want that to be a nop. */
593 memcpy (buf
, bp
->old_data
, breakpoint_len
);
594 ret
= write_inferior_memory (bp
->pc
, buf
, breakpoint_len
);
597 /* Something went wrong, relink the breakpoint. */
598 *bp_link
= prev_bp_link
;
602 "Failed to uninsert raw breakpoint "
603 "at 0x%s (%s) while deleting it.\n",
604 paddress (bp
->pc
), strerror (ret
));
622 warning ("Could not find raw breakpoint in list.");
627 release_breakpoint (struct process_info
*proc
, struct breakpoint
*bp
)
632 newrefcount
= bp
->raw
->refcount
- 1;
633 if (newrefcount
== 0)
635 ret
= delete_raw_breakpoint (proc
, bp
->raw
);
640 bp
->raw
->refcount
= newrefcount
;
648 delete_breakpoint_1 (struct process_info
*proc
, struct breakpoint
*todel
)
650 struct breakpoint
*bp
, **bp_link
;
653 bp
= proc
->breakpoints
;
654 bp_link
= &proc
->breakpoints
;
662 err
= release_breakpoint (proc
, bp
);
676 warning ("Could not find breakpoint in list.");
681 delete_breakpoint (struct breakpoint
*todel
)
683 struct process_info
*proc
= current_process ();
684 return delete_breakpoint_1 (proc
, todel
);
688 find_gdb_breakpoint_at (CORE_ADDR where
)
690 struct process_info
*proc
= current_process ();
691 struct breakpoint
*bp
;
693 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
694 if (bp
->type
== gdb_breakpoint
&& bp
->raw
->pc
== where
)
701 set_gdb_breakpoint_at (CORE_ADDR where
)
703 struct breakpoint
*bp
;
705 if (breakpoint_data
== NULL
)
708 /* If we see GDB inserting a second breakpoint at the same address,
709 then the first breakpoint must have disappeared due to a shared
710 library unload. On targets where the shared libraries are
711 handled by userspace, like SVR4, for example, GDBserver can't
712 tell if a library was loaded or unloaded. Since we refcount
713 breakpoints, if we didn't do this, we'd just increase the
714 refcount of the previous breakpoint at this address, but the trap
715 was not planted in the inferior anymore, thus the breakpoint
716 would never be hit. */
717 bp
= find_gdb_breakpoint_at (where
);
720 delete_gdb_breakpoint_at (where
);
722 /* Might as well validate all other breakpoints. */
723 validate_breakpoints ();
726 bp
= set_breakpoint_at (where
, NULL
);
730 bp
->type
= gdb_breakpoint
;
735 delete_gdb_breakpoint_at (CORE_ADDR addr
)
737 struct breakpoint
*bp
;
740 if (breakpoint_data
== NULL
)
743 bp
= find_gdb_breakpoint_at (addr
);
747 /* Before deleting the breakpoint, make sure to free
748 its condition list. */
749 clear_gdb_breakpoint_conditions (addr
);
750 err
= delete_breakpoint (bp
);
757 /* Clear all conditions associated with this breakpoint address. */
760 clear_gdb_breakpoint_conditions (CORE_ADDR addr
)
762 struct breakpoint
*bp
= find_gdb_breakpoint_at (addr
);
763 struct point_cond_list
*cond
;
765 if (bp
== NULL
|| bp
->cond_list
== NULL
)
768 cond
= bp
->cond_list
;
772 struct point_cond_list
*cond_next
;
774 cond_next
= cond
->next
;
775 free (cond
->cond
->bytes
);
781 bp
->cond_list
= NULL
;
784 /* Add condition CONDITION to GDBserver's breakpoint BP. */
787 add_condition_to_breakpoint (struct breakpoint
*bp
,
788 struct agent_expr
*condition
)
790 struct point_cond_list
*new_cond
;
792 /* Create new condition. */
793 new_cond
= xcalloc (1, sizeof (*new_cond
));
794 new_cond
->cond
= condition
;
796 /* Add condition to the list. */
797 new_cond
->next
= bp
->cond_list
;
798 bp
->cond_list
= new_cond
;
801 /* Add a target-side condition CONDITION to the breakpoint at ADDR. */
804 add_breakpoint_condition (CORE_ADDR addr
, char **condition
)
806 struct breakpoint
*bp
= find_gdb_breakpoint_at (addr
);
807 char *actparm
= *condition
;
808 struct agent_expr
*cond
;
813 if (condition
== NULL
)
816 cond
= gdb_parse_agent_expr (&actparm
);
820 fprintf (stderr
, "Condition evaluation failed. "
821 "Assuming unconditional.\n");
825 add_condition_to_breakpoint (bp
, cond
);
827 *condition
= actparm
;
832 /* Evaluate condition (if any) at breakpoint BP. Return 1 if
833 true and 0 otherwise. */
836 gdb_condition_true_at_breakpoint (CORE_ADDR where
)
838 /* Fetch registers for the current inferior. */
839 struct breakpoint
*bp
= find_gdb_breakpoint_at (where
);
841 struct point_cond_list
*cl
;
843 struct eval_agent_expr_context ctx
;
848 /* Check if the breakpoint is unconditional. If it is,
849 the condition always evaluates to TRUE. */
850 if (bp
->cond_list
== NULL
)
853 ctx
.regcache
= get_thread_regcache (current_inferior
, 1);
857 /* Evaluate each condition in the breakpoint's list of conditions.
858 Return true if any of the conditions evaluates to TRUE.
860 If we failed to evaluate the expression, TRUE is returned. This
861 forces GDB to reevaluate the conditions. */
862 for (cl
= bp
->cond_list
;
863 cl
&& !value
&& !err
; cl
= cl
->next
)
865 /* Evaluate the condition. */
866 err
= gdb_eval_agent_expr (&ctx
, cl
->cond
, &value
);
875 /* Add commands COMMANDS to GDBserver's breakpoint BP. */
878 add_commands_to_breakpoint (struct breakpoint
*bp
,
879 struct agent_expr
*commands
, int persist
)
881 struct point_command_list
*new_cmd
;
883 /* Create new command. */
884 new_cmd
= xcalloc (1, sizeof (*new_cmd
));
885 new_cmd
->cmd
= commands
;
886 new_cmd
->persistence
= persist
;
888 /* Add commands to the list. */
889 new_cmd
->next
= bp
->command_list
;
890 bp
->command_list
= new_cmd
;
893 /* Add a target-side command COMMAND to the breakpoint at ADDR. */
896 add_breakpoint_commands (CORE_ADDR addr
, char **command
, int persist
)
898 struct breakpoint
*bp
= find_gdb_breakpoint_at (addr
);
899 char *actparm
= *command
;
900 struct agent_expr
*cmd
;
908 cmd
= gdb_parse_agent_expr (&actparm
);
912 fprintf (stderr
, "Command evaluation failed. "
917 add_commands_to_breakpoint (bp
, cmd
, persist
);
924 /* Return true if there are no commands to run at this location,
925 which likely means we want to report back to GDB. */
927 gdb_no_commands_at_breakpoint (CORE_ADDR where
)
929 struct breakpoint
*bp
= find_gdb_breakpoint_at (where
);
935 fprintf (stderr
, "at 0x%s, bp command_list is 0x%s\n",
937 phex_nz ((uintptr_t) bp
->command_list
, 0));
938 return (bp
->command_list
== NULL
);
942 run_breakpoint_commands (CORE_ADDR where
)
944 /* Fetch registers for the current inferior. */
945 struct breakpoint
*bp
= find_gdb_breakpoint_at (where
);
947 struct point_command_list
*cl
;
949 struct eval_agent_expr_context ctx
;
954 ctx
.regcache
= get_thread_regcache (current_inferior
, 1);
958 for (cl
= bp
->command_list
;
959 cl
&& !value
&& !err
; cl
= cl
->next
)
961 /* Run the command. */
962 err
= gdb_eval_agent_expr (&ctx
, cl
->cmd
, &value
);
964 /* If one command has a problem, stop digging the hole deeper. */
970 /* Return 1 if there is a breakpoint inserted in address WHERE
971 and if its condition, if it exists, is true. */
974 gdb_breakpoint_here (CORE_ADDR where
)
976 return (find_gdb_breakpoint_at (where
) != NULL
);
980 set_reinsert_breakpoint (CORE_ADDR stop_at
)
982 struct breakpoint
*bp
;
984 bp
= set_breakpoint_at (stop_at
, NULL
);
985 bp
->type
= reinsert_breakpoint
;
989 delete_reinsert_breakpoints (void)
991 struct process_info
*proc
= current_process ();
992 struct breakpoint
*bp
, **bp_link
;
994 bp
= proc
->breakpoints
;
995 bp_link
= &proc
->breakpoints
;
999 if (bp
->type
== reinsert_breakpoint
)
1001 *bp_link
= bp
->next
;
1002 release_breakpoint (proc
, bp
);
1007 bp_link
= &bp
->next
;
1014 uninsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1019 unsigned char buf
[MAX_BREAKPOINT_LEN
];
1022 /* Since there can be fast tracepoint jumps inserted in the same
1023 address range, we use `write_inferior_memory', which takes
1024 care of layering breakpoints on top of fast tracepoints, and
1025 on top of the buffer we pass it. This works because we've
1026 already unlinked the fast tracepoint jump above. Also note
1027 that we need to pass the current shadow contents, because
1028 write_inferior_memory updates any shadow memory with what we
1029 pass here, and we want that to be a nop. */
1030 memcpy (buf
, bp
->old_data
, breakpoint_len
);
1031 err
= write_inferior_memory (bp
->pc
, buf
, breakpoint_len
);
1038 "Failed to uninsert raw breakpoint at 0x%s (%s).\n",
1039 paddress (bp
->pc
), strerror (err
));
1045 uninsert_breakpoints_at (CORE_ADDR pc
)
1047 struct raw_breakpoint
*bp
;
1049 bp
= find_raw_breakpoint_at (pc
);
1052 /* This can happen when we remove all breakpoints while handling
1056 "Could not find breakpoint at 0x%s "
1057 "in list (uninserting).\n",
1063 uninsert_raw_breakpoint (bp
);
1067 uninsert_all_breakpoints (void)
1069 struct process_info
*proc
= current_process ();
1070 struct raw_breakpoint
*bp
;
1072 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1074 uninsert_raw_breakpoint (bp
);
1078 reinsert_raw_breakpoint (struct raw_breakpoint
*bp
)
1083 error ("Breakpoint already inserted at reinsert time.");
1085 err
= (*the_target
->write_memory
) (bp
->pc
, breakpoint_data
,
1089 else if (debug_threads
)
1091 "Failed to reinsert breakpoint at 0x%s (%s).\n",
1092 paddress (bp
->pc
), strerror (err
));
1096 reinsert_breakpoints_at (CORE_ADDR pc
)
1098 struct raw_breakpoint
*bp
;
1100 bp
= find_raw_breakpoint_at (pc
);
1103 /* This can happen when we remove all breakpoints while handling
1107 "Could not find raw breakpoint at 0x%s "
1108 "in list (reinserting).\n",
1113 reinsert_raw_breakpoint (bp
);
1117 reinsert_all_breakpoints (void)
1119 struct process_info
*proc
= current_process ();
1120 struct raw_breakpoint
*bp
;
1122 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1124 reinsert_raw_breakpoint (bp
);
1128 check_breakpoints (CORE_ADDR stop_pc
)
1130 struct process_info
*proc
= current_process ();
1131 struct breakpoint
*bp
, **bp_link
;
1133 bp
= proc
->breakpoints
;
1134 bp_link
= &proc
->breakpoints
;
1138 if (bp
->raw
->pc
== stop_pc
)
1140 if (!bp
->raw
->inserted
)
1142 warning ("Hit a removed breakpoint?");
1146 if (bp
->handler
!= NULL
&& (*bp
->handler
) (stop_pc
))
1148 *bp_link
= bp
->next
;
1150 release_breakpoint (proc
, bp
);
1157 bp_link
= &bp
->next
;
1163 set_breakpoint_data (const unsigned char *bp_data
, int bp_len
)
1165 breakpoint_data
= bp_data
;
1166 breakpoint_len
= bp_len
;
1170 breakpoint_here (CORE_ADDR addr
)
1172 return (find_raw_breakpoint_at (addr
) != NULL
);
1176 breakpoint_inserted_here (CORE_ADDR addr
)
1178 struct raw_breakpoint
*bp
;
1180 bp
= find_raw_breakpoint_at (addr
);
1182 return (bp
!= NULL
&& bp
->inserted
);
1186 validate_inserted_breakpoint (struct raw_breakpoint
*bp
)
1191 gdb_assert (bp
->inserted
);
1193 buf
= alloca (breakpoint_len
);
1194 err
= (*the_target
->read_memory
) (bp
->pc
, buf
, breakpoint_len
);
1195 if (err
|| memcmp (buf
, breakpoint_data
, breakpoint_len
) != 0)
1197 /* Tag it as gone. */
1199 bp
->shlib_disabled
= 1;
1207 delete_disabled_breakpoints (void)
1209 struct process_info
*proc
= current_process ();
1210 struct breakpoint
*bp
, *next
;
1212 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= next
)
1215 if (bp
->raw
->shlib_disabled
)
1216 delete_breakpoint_1 (proc
, bp
);
1220 /* Check if breakpoints we inserted still appear to be inserted. They
1221 may disappear due to a shared library unload, and worse, a new
1222 shared library may be reloaded at the same address as the
1223 previously unloaded one. If that happens, we should make sure that
1224 the shadow memory of the old breakpoints isn't used when reading or
1228 validate_breakpoints (void)
1230 struct process_info
*proc
= current_process ();
1231 struct breakpoint
*bp
;
1233 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
1235 if (bp
->raw
->inserted
)
1236 validate_inserted_breakpoint (bp
->raw
);
1239 delete_disabled_breakpoints ();
1243 check_mem_read (CORE_ADDR mem_addr
, unsigned char *buf
, int mem_len
)
1245 struct process_info
*proc
= current_process ();
1246 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
1247 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
1248 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
1249 int disabled_one
= 0;
1251 for (; jp
!= NULL
; jp
= jp
->next
)
1253 CORE_ADDR bp_end
= jp
->pc
+ jp
->length
;
1254 CORE_ADDR start
, end
;
1255 int copy_offset
, copy_len
, buf_offset
;
1257 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= buf
+ mem_len
1258 || buf
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
1260 if (mem_addr
>= bp_end
)
1262 if (jp
->pc
>= mem_end
)
1266 if (mem_addr
> start
)
1273 copy_len
= end
- start
;
1274 copy_offset
= start
- jp
->pc
;
1275 buf_offset
= start
- mem_addr
;
1278 memcpy (buf
+ buf_offset
,
1279 fast_tracepoint_jump_shadow (jp
) + copy_offset
,
1283 for (; bp
!= NULL
; bp
= bp
->next
)
1285 CORE_ADDR bp_end
= bp
->pc
+ breakpoint_len
;
1286 CORE_ADDR start
, end
;
1287 int copy_offset
, copy_len
, buf_offset
;
1289 gdb_assert (bp
->old_data
>= buf
+ mem_len
1290 || buf
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
1292 if (mem_addr
>= bp_end
)
1294 if (bp
->pc
>= mem_end
)
1298 if (mem_addr
> start
)
1305 copy_len
= end
- start
;
1306 copy_offset
= start
- bp
->pc
;
1307 buf_offset
= start
- mem_addr
;
1311 if (validate_inserted_breakpoint (bp
))
1312 memcpy (buf
+ buf_offset
, bp
->old_data
+ copy_offset
, copy_len
);
1319 delete_disabled_breakpoints ();
1323 check_mem_write (CORE_ADDR mem_addr
, unsigned char *buf
,
1324 const unsigned char *myaddr
, int mem_len
)
1326 struct process_info
*proc
= current_process ();
1327 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
1328 struct fast_tracepoint_jump
*jp
= proc
->fast_tracepoint_jumps
;
1329 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
1330 int disabled_one
= 0;
1332 /* First fast tracepoint jumps, then breakpoint traps on top. */
1334 for (; jp
!= NULL
; jp
= jp
->next
)
1336 CORE_ADDR jp_end
= jp
->pc
+ jp
->length
;
1337 CORE_ADDR start
, end
;
1338 int copy_offset
, copy_len
, buf_offset
;
1340 gdb_assert (fast_tracepoint_jump_shadow (jp
) >= myaddr
+ mem_len
1341 || myaddr
>= fast_tracepoint_jump_shadow (jp
) + (jp
)->length
);
1342 gdb_assert (fast_tracepoint_jump_insn (jp
) >= buf
+ mem_len
1343 || buf
>= fast_tracepoint_jump_insn (jp
) + (jp
)->length
);
1345 if (mem_addr
>= jp_end
)
1347 if (jp
->pc
>= mem_end
)
1351 if (mem_addr
> start
)
1358 copy_len
= end
- start
;
1359 copy_offset
= start
- jp
->pc
;
1360 buf_offset
= start
- mem_addr
;
1362 memcpy (fast_tracepoint_jump_shadow (jp
) + copy_offset
,
1363 myaddr
+ buf_offset
, copy_len
);
1365 memcpy (buf
+ buf_offset
,
1366 fast_tracepoint_jump_insn (jp
) + copy_offset
, copy_len
);
1369 for (; bp
!= NULL
; bp
= bp
->next
)
1371 CORE_ADDR bp_end
= bp
->pc
+ breakpoint_len
;
1372 CORE_ADDR start
, end
;
1373 int copy_offset
, copy_len
, buf_offset
;
1375 gdb_assert (bp
->old_data
>= myaddr
+ mem_len
1376 || myaddr
>= &bp
->old_data
[sizeof (bp
->old_data
)]);
1378 if (mem_addr
>= bp_end
)
1380 if (bp
->pc
>= mem_end
)
1384 if (mem_addr
> start
)
1391 copy_len
= end
- start
;
1392 copy_offset
= start
- bp
->pc
;
1393 buf_offset
= start
- mem_addr
;
1395 memcpy (bp
->old_data
+ copy_offset
, myaddr
+ buf_offset
, copy_len
);
1398 if (validate_inserted_breakpoint (bp
))
1399 memcpy (buf
+ buf_offset
, breakpoint_data
+ copy_offset
, copy_len
);
1406 delete_disabled_breakpoints ();
1409 /* Delete all breakpoints, and un-insert them from the inferior. */
1412 delete_all_breakpoints (void)
1414 struct process_info
*proc
= current_process ();
1416 while (proc
->breakpoints
)
1417 delete_breakpoint_1 (proc
, proc
->breakpoints
);
1420 /* Clear the "inserted" flag in all breakpoints. */
1423 mark_breakpoints_out (struct process_info
*proc
)
1425 struct raw_breakpoint
*raw_bp
;
1427 for (raw_bp
= proc
->raw_breakpoints
; raw_bp
!= NULL
; raw_bp
= raw_bp
->next
)
1428 raw_bp
->inserted
= 0;
1431 /* Release all breakpoints, but do not try to un-insert them from the
1435 free_all_breakpoints (struct process_info
*proc
)
1437 mark_breakpoints_out (proc
);
1439 /* Note: use PROC explicitly instead of deferring to
1440 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
1441 released when we get here. There should be no call to
1442 current_process from here on. */
1443 while (proc
->breakpoints
)
1444 delete_breakpoint_1 (proc
, proc
->breakpoints
);