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
69 /* Non-zero if this breakpoint is currently disabled because we no
70 longer detect it as inserted. */
74 /* The type of a breakpoint. */
77 /* A GDB breakpoint, requested with a Z0 packet. */
80 /* A basic-software-single-step breakpoint. */
83 /* Any other breakpoint type that doesn't require specific
84 treatment goes here. E.g., an event breakpoint. */
88 /* A high level (in gdbserver's perspective) breakpoint. */
91 struct breakpoint *next;
93 /* The breakpoint's type. */
96 /* Link to this breakpoint's raw breakpoint. This is always
98 struct raw_breakpoint *raw;
100 /* Function to call when we hit this breakpoint. If it returns 1,
101 the breakpoint shall be deleted; 0 or if this callback is NULL,
102 it will be left inserted. */
103 int (*handler) (CORE_ADDR);
106 static struct raw_breakpoint *
107 find_raw_breakpoint_at (CORE_ADDR where)
109 struct process_info *proc = current_process ();
110 struct raw_breakpoint *bp;
112 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
119 static struct raw_breakpoint *
120 set_raw_breakpoint_at (CORE_ADDR where)
122 struct process_info *proc = current_process ();
123 struct raw_breakpoint *bp;
126 if (breakpoint_data == NULL)
127 error ("Target does not support breakpoints.");
129 bp = find_raw_breakpoint_at (where);
136 bp = xcalloc (1, sizeof (*bp));
140 err = (*the_target->read_memory) (where, bp->old_data,
146 "Failed to read shadow memory of"
147 " breakpoint at 0x%s (%s).\n",
148 paddress (where), strerror (err));
153 err = (*the_target->write_memory) (where, breakpoint_data,
159 "Failed to insert breakpoint at 0x%s (%s).\n",
160 paddress (where), strerror (err));
165 /* Link the breakpoint in. */
167 bp->next = proc->raw_breakpoints;
168 proc->raw_breakpoints = bp;
173 set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
175 struct process_info *proc = current_process ();
176 struct breakpoint *bp;
177 struct raw_breakpoint *raw;
179 raw = set_raw_breakpoint_at (where);
187 bp = xcalloc (1, sizeof (struct breakpoint));
188 bp->type = other_breakpoint;
191 bp->handler = handler;
193 bp->next = proc->breakpoints;
194 proc->breakpoints = bp;
200 delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
202 struct raw_breakpoint *bp, **bp_link;
205 bp = proc->raw_breakpoints;
206 bp_link = &proc->raw_breakpoints;
214 struct raw_breakpoint *prev_bp_link = *bp_link;
218 ret = (*the_target->write_memory) (bp->pc, bp->old_data,
222 /* Something went wrong, relink the breakpoint. */
223 *bp_link = prev_bp_link;
227 "Failed to uninsert raw breakpoint "
228 "at 0x%s (%s) while deleting it.\n",
229 paddress (bp->pc), strerror (ret));
247 warning ("Could not find raw breakpoint in list.");
252 release_breakpoint (struct process_info *proc, struct breakpoint *bp)
257 newrefcount = bp->raw->refcount - 1;
258 if (newrefcount == 0)
260 ret = delete_raw_breakpoint (proc, bp->raw);
265 bp->raw->refcount = newrefcount;
273 delete_breakpoint_1 (struct process_info *proc, struct breakpoint *todel)
275 struct breakpoint *bp, **bp_link;
278 bp = proc->breakpoints;
279 bp_link = &proc->breakpoints;
287 err = release_breakpoint (proc, bp);
301 warning ("Could not find breakpoint in list.");
306 delete_breakpoint (struct breakpoint *todel)
308 struct process_info *proc = current_process ();
309 return delete_breakpoint_1 (proc, todel);
312 static struct breakpoint *
313 find_gdb_breakpoint_at (CORE_ADDR where)
315 struct process_info *proc = current_process ();
316 struct breakpoint *bp;
318 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
319 if (bp->type == gdb_breakpoint && bp->raw->pc == where)
326 set_gdb_breakpoint_at (CORE_ADDR where)
328 struct breakpoint *bp;
330 if (breakpoint_data == NULL)
333 /* If we see GDB inserting a second breakpoint at the same address,
334 then the first breakpoint must have disappeared due to a shared
335 library unload. On targets where the shared libraries are
336 handled by userspace, like SVR4, for example, GDBserver can't
337 tell if a library was loaded or unloaded. Since we refcount
338 breakpoints, if we didn't do this, we'd just increase the
339 refcount of the previous breakpoint at this address, but the trap
340 was not planted in the inferior anymore, thus the breakpoint
341 would never be hit. */
342 bp = find_gdb_breakpoint_at (where);
345 delete_gdb_breakpoint_at (where);
347 /* Might as well validate all other breakpoints. */
348 validate_breakpoints ();
351 bp = set_breakpoint_at (where, NULL);
355 bp->type = gdb_breakpoint;
360 delete_gdb_breakpoint_at (CORE_ADDR addr)
362 struct breakpoint *bp;
365 if (breakpoint_data == NULL)
368 bp = find_gdb_breakpoint_at (addr);
372 err = delete_breakpoint (bp);
380 gdb_breakpoint_here (CORE_ADDR where)
382 struct breakpoint *bp = find_gdb_breakpoint_at (where);
388 set_reinsert_breakpoint (CORE_ADDR stop_at)
390 struct breakpoint *bp;
392 bp = set_breakpoint_at (stop_at, NULL);
393 bp->type = reinsert_breakpoint;
397 delete_reinsert_breakpoints (void)
399 struct process_info *proc = current_process ();
400 struct breakpoint *bp, **bp_link;
402 bp = proc->breakpoints;
403 bp_link = &proc->breakpoints;
407 if (bp->type == reinsert_breakpoint)
410 release_breakpoint (proc, bp);
422 uninsert_raw_breakpoint (struct raw_breakpoint *bp)
429 err = (*the_target->write_memory) (bp->pc, bp->old_data,
437 "Failed to uninsert raw breakpoint at 0x%s (%s).\n",
438 paddress (bp->pc), strerror (err));
444 uninsert_breakpoints_at (CORE_ADDR pc)
446 struct raw_breakpoint *bp;
448 bp = find_raw_breakpoint_at (pc);
451 /* This can happen when we remove all breakpoints while handling
455 "Could not find breakpoint at 0x%s "
456 "in list (uninserting).\n",
462 uninsert_raw_breakpoint (bp);
466 reinsert_raw_breakpoint (struct raw_breakpoint *bp)
471 error ("Breakpoint already inserted at reinsert time.");
473 err = (*the_target->write_memory) (bp->pc, breakpoint_data,
477 else if (debug_threads)
479 "Failed to reinsert breakpoint at 0x%s (%s).\n",
480 paddress (bp->pc), strerror (err));
484 reinsert_breakpoints_at (CORE_ADDR pc)
486 struct raw_breakpoint *bp;
488 bp = find_raw_breakpoint_at (pc);
491 /* This can happen when we remove all breakpoints while handling
495 "Could not find raw breakpoint at 0x%s "
496 "in list (reinserting).\n",
501 reinsert_raw_breakpoint (bp);
505 check_breakpoints (CORE_ADDR stop_pc)
507 struct process_info *proc = current_process ();
508 struct breakpoint *bp, **bp_link;
510 bp = proc->breakpoints;
511 bp_link = &proc->breakpoints;
515 if (bp->raw->pc == stop_pc)
517 if (!bp->raw->inserted)
519 warning ("Hit a removed breakpoint?");
523 if (bp->handler != NULL && (*bp->handler) (stop_pc))
527 release_breakpoint (proc, bp);
540 set_breakpoint_data (const unsigned char *bp_data, int bp_len)
542 breakpoint_data = bp_data;
543 breakpoint_len = bp_len;
547 breakpoint_here (CORE_ADDR addr)
549 return (find_raw_breakpoint_at (addr) != NULL);
553 breakpoint_inserted_here (CORE_ADDR addr)
555 struct raw_breakpoint *bp;
557 bp = find_raw_breakpoint_at (addr);
559 return (bp != NULL && bp->inserted);
563 validate_inserted_breakpoint (struct raw_breakpoint *bp)
568 gdb_assert (bp->inserted);
570 buf = alloca (breakpoint_len);
571 err = (*the_target->read_memory) (bp->pc, buf, breakpoint_len);
572 if (err || memcmp (buf, breakpoint_data, breakpoint_len) != 0)
574 /* Tag it as gone. */
576 bp->shlib_disabled = 1;
584 delete_disabled_breakpoints (void)
586 struct process_info *proc = current_process ();
587 struct breakpoint *bp, *next;
589 for (bp = proc->breakpoints; bp != NULL; bp = next)
592 if (bp->raw->shlib_disabled)
593 delete_breakpoint_1 (proc, bp);
597 /* Check if breakpoints we inserted still appear to be inserted. They
598 may disappear due to a shared library unload, and worse, a new
599 shared library may be reloaded at the same address as the
600 previously unloaded one. If that happens, we should make sure that
601 the shadow memory of the old breakpoints isn't used when reading or
605 validate_breakpoints (void)
607 struct process_info *proc = current_process ();
608 struct breakpoint *bp;
610 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
612 if (bp->raw->inserted)
613 validate_inserted_breakpoint (bp->raw);
616 delete_disabled_breakpoints ();
620 check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
622 struct process_info *proc = current_process ();
623 struct raw_breakpoint *bp = proc->raw_breakpoints;
624 CORE_ADDR mem_end = mem_addr + mem_len;
625 int disabled_one = 0;
627 for (; bp != NULL; bp = bp->next)
629 CORE_ADDR bp_end = bp->pc + breakpoint_len;
630 CORE_ADDR start, end;
631 int copy_offset, copy_len, buf_offset;
633 if (mem_addr >= bp_end)
635 if (bp->pc >= mem_end)
639 if (mem_addr > start)
646 copy_len = end - start;
647 copy_offset = start - bp->pc;
648 buf_offset = start - mem_addr;
652 if (validate_inserted_breakpoint (bp))
653 memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
660 delete_disabled_breakpoints ();
664 check_mem_write (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
666 struct process_info *proc = current_process ();
667 struct raw_breakpoint *bp = proc->raw_breakpoints;
668 CORE_ADDR mem_end = mem_addr + mem_len;
669 int disabled_one = 0;
671 for (; bp != NULL; bp = bp->next)
673 CORE_ADDR bp_end = bp->pc + breakpoint_len;
674 CORE_ADDR start, end;
675 int copy_offset, copy_len, buf_offset;
677 if (mem_addr >= bp_end)
679 if (bp->pc >= mem_end)
683 if (mem_addr > start)
690 copy_len = end - start;
691 copy_offset = start - bp->pc;
692 buf_offset = start - mem_addr;
694 memcpy (bp->old_data + copy_offset, buf + buf_offset, copy_len);
697 if (validate_inserted_breakpoint (bp))
698 memcpy (buf + buf_offset, breakpoint_data + copy_offset, copy_len);
705 delete_disabled_breakpoints ();
708 /* Delete all breakpoints, and un-insert them from the inferior. */
711 delete_all_breakpoints (void)
713 struct process_info *proc = current_process ();
715 while (proc->breakpoints)
716 delete_breakpoint_1 (proc, proc->breakpoints);
719 /* Clear the "inserted" flag in all breakpoints. */
722 mark_breakpoints_out (struct process_info *proc)
724 struct raw_breakpoint *raw_bp;
726 for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next)
727 raw_bp->inserted = 0;
730 /* Release all breakpoints, but do not try to un-insert them from the
734 free_all_breakpoints (struct process_info *proc)
736 mark_breakpoints_out (proc);
738 /* Note: use PROC explicitly instead of deferring to
739 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
740 released when we get here. There should be no call to
741 current_process from here on. */
742 while (proc->breakpoints)
743 delete_breakpoint_1 (proc, proc->breakpoints);