1 /* Memory breakpoint operations for the remote server for GDB.
2 Copyright (C) 2002, 2003, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 Contributed by MontaVista Software.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 const unsigned char *breakpoint_data
;
27 #define MAX_BREAKPOINT_LEN 8
29 /* GDB will never try to install multiple breakpoints at the same
30 address. But, we need to keep track of internal breakpoints too,
31 and so we do need to be able to install multiple breakpoints at the
32 same address transparently. We keep track of two different, and
33 closely related structures. A raw breakpoint, which manages the
34 low level, close to the metal aspect of a breakpoint. It holds the
35 breakpoint address, and a buffer holding a copy of the instructions
36 that would be in memory had not been a breakpoint there (we call
37 that the shadow memory of the breakpoint). We occasionally need to
38 temporarilly uninsert a breakpoint without the client knowing about
39 it (e.g., to step over an internal breakpoint), so we keep an
40 `inserted' state associated with this low level breakpoint
41 structure. There can only be one such object for a given address.
42 Then, we have (a bit higher level) breakpoints. This structure
43 holds a callback to be called whenever a breakpoint is hit, a
44 high-level type, and a link to a low level raw breakpoint. There
45 can be many high-level breakpoints at the same address, and all of
46 them will point to the same raw breakpoint, which is reference
49 /* The low level, physical, raw breakpoint. */
52 struct raw_breakpoint
*next
;
54 /* A reference count. Each high level breakpoint referencing this
55 raw breakpoint accounts for one reference. */
58 /* The breakpoint's insertion address. There can only be one raw
59 breakpoint for a given PC. */
62 /* The breakpoint's shadow memory. */
63 unsigned char old_data
[MAX_BREAKPOINT_LEN
];
65 /* Non-zero if this breakpoint is currently inserted in the
70 /* The type of a breakpoint. */
73 /* A GDB breakpoint, requested with a Z0 packet. */
76 /* A basic-software-single-step breakpoint. */
79 /* Any other breakpoint type that doesn't require specific
80 treatment goes here. E.g., an event breakpoint. */
84 /* A high level (in gdbserver's perspective) breakpoint. */
87 struct breakpoint
*next
;
89 /* The breakpoint's type. */
92 /* Link to this breakpoint's raw breakpoint. This is always
94 struct raw_breakpoint
*raw
;
96 /* Function to call when we hit this breakpoint. If it returns 1,
97 the breakpoint shall be deleted; 0 or if this callback is NULL,
98 it will be left inserted. */
99 int (*handler
) (CORE_ADDR
);
102 static struct raw_breakpoint
*
103 find_raw_breakpoint_at (CORE_ADDR where
)
105 struct process_info
*proc
= current_process ();
106 struct raw_breakpoint
*bp
;
108 for (bp
= proc
->raw_breakpoints
; bp
!= NULL
; bp
= bp
->next
)
115 static struct raw_breakpoint
*
116 set_raw_breakpoint_at (CORE_ADDR where
)
118 struct process_info
*proc
= current_process ();
119 struct raw_breakpoint
*bp
;
122 if (breakpoint_data
== NULL
)
123 error ("Target does not support breakpoints.");
125 bp
= find_raw_breakpoint_at (where
);
132 bp
= xcalloc (1, sizeof (*bp
));
136 err
= (*the_target
->read_memory
) (where
, bp
->old_data
,
142 "Failed to read shadow memory of"
143 " breakpoint at 0x%s (%s).\n",
144 paddress (where
), strerror (err
));
149 err
= (*the_target
->write_memory
) (where
, breakpoint_data
,
155 "Failed to insert breakpoint at 0x%s (%s).\n",
156 paddress (where
), strerror (err
));
161 /* Link the breakpoint in. */
163 bp
->next
= proc
->raw_breakpoints
;
164 proc
->raw_breakpoints
= bp
;
169 set_breakpoint_at (CORE_ADDR where
, int (*handler
) (CORE_ADDR
))
171 struct process_info
*proc
= current_process ();
172 struct breakpoint
*bp
;
173 struct raw_breakpoint
*raw
;
175 raw
= set_raw_breakpoint_at (where
);
183 bp
= xcalloc (1, sizeof (struct breakpoint
));
184 bp
->type
= other_breakpoint
;
187 bp
->handler
= handler
;
189 bp
->next
= proc
->breakpoints
;
190 proc
->breakpoints
= bp
;
196 delete_raw_breakpoint (struct process_info
*proc
, struct raw_breakpoint
*todel
)
198 struct raw_breakpoint
*bp
, **bp_link
;
201 bp
= proc
->raw_breakpoints
;
202 bp_link
= &proc
->raw_breakpoints
;
210 struct raw_breakpoint
*prev_bp_link
= *bp_link
;
214 ret
= (*the_target
->write_memory
) (bp
->pc
, bp
->old_data
,
218 /* Something went wrong, relink the breakpoint. */
219 *bp_link
= prev_bp_link
;
223 "Failed to uninsert raw breakpoint "
224 "at 0x%s (%s) while deleting it.\n",
225 paddress (bp
->pc
), strerror (ret
));
243 warning ("Could not find raw breakpoint in list.");
248 release_breakpoint (struct process_info
*proc
, struct breakpoint
*bp
)
253 newrefcount
= bp
->raw
->refcount
- 1;
254 if (newrefcount
== 0)
256 ret
= delete_raw_breakpoint (proc
, bp
->raw
);
261 bp
->raw
->refcount
= newrefcount
;
269 delete_breakpoint_1 (struct process_info
*proc
, struct breakpoint
*todel
)
271 struct breakpoint
*bp
, **bp_link
;
274 bp
= proc
->breakpoints
;
275 bp_link
= &proc
->breakpoints
;
283 err
= release_breakpoint (proc
, bp
);
297 warning ("Could not find breakpoint in list.");
302 delete_breakpoint (struct breakpoint
*todel
)
304 struct process_info
*proc
= current_process ();
305 return delete_breakpoint_1 (proc
, todel
);
308 static struct breakpoint
*
309 find_gdb_breakpoint_at (CORE_ADDR where
)
311 struct process_info
*proc
= current_process ();
312 struct breakpoint
*bp
;
314 for (bp
= proc
->breakpoints
; bp
!= NULL
; bp
= bp
->next
)
315 if (bp
->type
== gdb_breakpoint
&& bp
->raw
->pc
== where
)
322 set_gdb_breakpoint_at (CORE_ADDR where
)
324 struct breakpoint
*bp
;
326 if (breakpoint_data
== NULL
)
329 bp
= set_breakpoint_at (where
, NULL
);
333 bp
->type
= gdb_breakpoint
;
338 delete_gdb_breakpoint_at (CORE_ADDR addr
)
340 struct breakpoint
*bp
;
343 if (breakpoint_data
== NULL
)
346 bp
= find_gdb_breakpoint_at (addr
);
350 err
= delete_breakpoint (bp
);
358 gdb_breakpoint_here (CORE_ADDR where
)
360 struct breakpoint
*bp
= find_gdb_breakpoint_at (where
);
366 set_reinsert_breakpoint (CORE_ADDR stop_at
)
368 struct breakpoint
*bp
;
370 bp
= set_breakpoint_at (stop_at
, NULL
);
371 bp
->type
= reinsert_breakpoint
;
375 delete_reinsert_breakpoints (void)
377 struct process_info
*proc
= current_process ();
378 struct breakpoint
*bp
, **bp_link
;
380 bp
= proc
->breakpoints
;
381 bp_link
= &proc
->breakpoints
;
385 if (bp
->type
== reinsert_breakpoint
)
388 release_breakpoint (proc
, bp
);
400 uninsert_raw_breakpoint (struct raw_breakpoint
*bp
)
407 err
= (*the_target
->write_memory
) (bp
->pc
, bp
->old_data
,
415 "Failed to uninsert raw breakpoint at 0x%s (%s).\n",
416 paddress (bp
->pc
), strerror (err
));
422 uninsert_breakpoints_at (CORE_ADDR pc
)
424 struct raw_breakpoint
*bp
;
426 bp
= find_raw_breakpoint_at (pc
);
429 /* This can happen when we remove all breakpoints while handling
433 "Could not find breakpoint at 0x%s "
434 "in list (uninserting).\n",
440 uninsert_raw_breakpoint (bp
);
444 reinsert_raw_breakpoint (struct raw_breakpoint
*bp
)
449 error ("Breakpoint already inserted at reinsert time.");
451 err
= (*the_target
->write_memory
) (bp
->pc
, breakpoint_data
,
455 else if (debug_threads
)
457 "Failed to reinsert breakpoint at 0x%s (%s).\n",
458 paddress (bp
->pc
), strerror (err
));
462 reinsert_breakpoints_at (CORE_ADDR pc
)
464 struct raw_breakpoint
*bp
;
466 bp
= find_raw_breakpoint_at (pc
);
469 /* This can happen when we remove all breakpoints while handling
473 "Could not find raw breakpoint at 0x%s "
474 "in list (reinserting).\n",
479 reinsert_raw_breakpoint (bp
);
483 check_breakpoints (CORE_ADDR stop_pc
)
485 struct process_info
*proc
= current_process ();
486 struct breakpoint
*bp
, **bp_link
;
488 bp
= proc
->breakpoints
;
489 bp_link
= &proc
->breakpoints
;
493 if (bp
->raw
->pc
== stop_pc
)
495 if (!bp
->raw
->inserted
)
497 warning ("Hit a removed breakpoint?");
501 if (bp
->handler
!= NULL
&& (*bp
->handler
) (stop_pc
))
505 release_breakpoint (proc
, bp
);
518 set_breakpoint_data (const unsigned char *bp_data
, int bp_len
)
520 breakpoint_data
= bp_data
;
521 breakpoint_len
= bp_len
;
525 breakpoint_here (CORE_ADDR addr
)
527 return (find_raw_breakpoint_at (addr
) != NULL
);
531 breakpoint_inserted_here (CORE_ADDR addr
)
533 struct raw_breakpoint
*bp
;
535 bp
= find_raw_breakpoint_at (addr
);
537 return (bp
!= NULL
&& bp
->inserted
);
541 check_mem_read (CORE_ADDR mem_addr
, unsigned char *buf
, int mem_len
)
543 struct process_info
*proc
= current_process ();
544 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
545 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
547 for (; bp
!= NULL
; bp
= bp
->next
)
549 CORE_ADDR bp_end
= bp
->pc
+ breakpoint_len
;
550 CORE_ADDR start
, end
;
551 int copy_offset
, copy_len
, buf_offset
;
553 if (mem_addr
>= bp_end
)
555 if (bp
->pc
>= mem_end
)
559 if (mem_addr
> start
)
566 copy_len
= end
- start
;
567 copy_offset
= start
- bp
->pc
;
568 buf_offset
= start
- mem_addr
;
571 memcpy (buf
+ buf_offset
, bp
->old_data
+ copy_offset
, copy_len
);
576 check_mem_write (CORE_ADDR mem_addr
, unsigned char *buf
, int mem_len
)
578 struct process_info
*proc
= current_process ();
579 struct raw_breakpoint
*bp
= proc
->raw_breakpoints
;
580 CORE_ADDR mem_end
= mem_addr
+ mem_len
;
582 for (; bp
!= NULL
; bp
= bp
->next
)
584 CORE_ADDR bp_end
= bp
->pc
+ breakpoint_len
;
585 CORE_ADDR start
, end
;
586 int copy_offset
, copy_len
, buf_offset
;
588 if (mem_addr
>= bp_end
)
590 if (bp
->pc
>= mem_end
)
594 if (mem_addr
> start
)
601 copy_len
= end
- start
;
602 copy_offset
= start
- bp
->pc
;
603 buf_offset
= start
- mem_addr
;
605 memcpy (bp
->old_data
+ copy_offset
, buf
+ buf_offset
, copy_len
);
607 memcpy (buf
+ buf_offset
, breakpoint_data
+ copy_offset
, copy_len
);
611 /* Delete all breakpoints, and un-insert them from the inferior. */
614 delete_all_breakpoints (void)
616 struct process_info
*proc
= current_process ();
618 while (proc
->breakpoints
)
619 delete_breakpoint_1 (proc
, proc
->breakpoints
);
622 /* Release all breakpoints, but do not try to un-insert them from the
626 free_all_breakpoints (struct process_info
*proc
)
628 struct raw_breakpoint
*raw_bp
;
630 for (raw_bp
= proc
->raw_breakpoints
; raw_bp
!= NULL
; raw_bp
= raw_bp
->next
)
631 raw_bp
->inserted
= 0;
633 /* Note: use PROC explicitly instead of deferring to
634 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
635 released when we get here. There should be no call to
636 current_process from here on. */
637 while (proc
->breakpoints
)
638 delete_breakpoint_1 (proc
, proc
->breakpoints
);
This page took 0.042538 seconds and 4 git commands to generate.