xref: /dflybsd-src/contrib/gdb-7/gdb/mem-break.c (revision a45ae5f869d9cfcb3e41dbab486e10bfa9e336bf)
15796c8dcSSimon Schubert /* Simulate breakpoints by patching locations in the target system, for GDB.
25796c8dcSSimon Schubert 
3*a45ae5f8SJohn Marino    Copyright (C) 1990-1993, 1995, 1997-2000, 2002, 2007-2012 Free
4*a45ae5f8SJohn Marino    Software Foundation, Inc.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    Contributed by Cygnus Support.  Written by John Gilmore.
75796c8dcSSimon Schubert 
85796c8dcSSimon Schubert    This file is part of GDB.
95796c8dcSSimon Schubert 
105796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
115796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
125796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
135796c8dcSSimon Schubert    (at your option) any later version.
145796c8dcSSimon Schubert 
155796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
165796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
175796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
185796c8dcSSimon Schubert    GNU General Public License for more details.
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
215796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
225796c8dcSSimon Schubert 
235796c8dcSSimon Schubert #include "defs.h"
245796c8dcSSimon Schubert 
255796c8dcSSimon Schubert /* This file is only useful if BREAKPOINT_FROM_PC is set.  If not, we
265796c8dcSSimon Schubert    punt.  */
275796c8dcSSimon Schubert 
285796c8dcSSimon Schubert #include "symtab.h"
295796c8dcSSimon Schubert #include "breakpoint.h"
305796c8dcSSimon Schubert #include "inferior.h"
315796c8dcSSimon Schubert #include "target.h"
325796c8dcSSimon Schubert 
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert /* Insert a breakpoint on targets that don't have any better
355796c8dcSSimon Schubert    breakpoint support.  We read the contents of the target location
365796c8dcSSimon Schubert    and stash it, then overwrite it with a breakpoint instruction.
375796c8dcSSimon Schubert    BP_TGT->placed_address is the target location in the target
385796c8dcSSimon Schubert    machine.  BP_TGT->shadow_contents is some memory allocated for
395796c8dcSSimon Schubert    saving the target contents.  It is guaranteed by the caller to be
405796c8dcSSimon Schubert    long enough to save BREAKPOINT_LEN bytes (this is accomplished via
415796c8dcSSimon Schubert    BREAKPOINT_MAX).  */
425796c8dcSSimon Schubert 
435796c8dcSSimon Schubert int
445796c8dcSSimon Schubert default_memory_insert_breakpoint (struct gdbarch *gdbarch,
455796c8dcSSimon Schubert 				  struct bp_target_info *bp_tgt)
465796c8dcSSimon Schubert {
475796c8dcSSimon Schubert   int val;
485796c8dcSSimon Schubert   const unsigned char *bp;
495796c8dcSSimon Schubert 
505796c8dcSSimon Schubert   /* Determine appropriate breakpoint contents and size for this address.  */
515796c8dcSSimon Schubert   bp = gdbarch_breakpoint_from_pc
525796c8dcSSimon Schubert        (gdbarch, &bp_tgt->placed_address, &bp_tgt->placed_size);
535796c8dcSSimon Schubert   if (bp == NULL)
545796c8dcSSimon Schubert     error (_("Software breakpoints not implemented for this target."));
555796c8dcSSimon Schubert 
565796c8dcSSimon Schubert   /* Save the memory contents.  */
575796c8dcSSimon Schubert   bp_tgt->shadow_len = bp_tgt->placed_size;
585796c8dcSSimon Schubert   val = target_read_memory (bp_tgt->placed_address, bp_tgt->shadow_contents,
595796c8dcSSimon Schubert 			    bp_tgt->placed_size);
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert   /* Write the breakpoint.  */
625796c8dcSSimon Schubert   if (val == 0)
63*a45ae5f8SJohn Marino     val = target_write_raw_memory (bp_tgt->placed_address, bp,
645796c8dcSSimon Schubert 				   bp_tgt->placed_size);
655796c8dcSSimon Schubert 
665796c8dcSSimon Schubert   return val;
675796c8dcSSimon Schubert }
685796c8dcSSimon Schubert 
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert int
715796c8dcSSimon Schubert default_memory_remove_breakpoint (struct gdbarch *gdbarch,
725796c8dcSSimon Schubert 				  struct bp_target_info *bp_tgt)
735796c8dcSSimon Schubert {
74*a45ae5f8SJohn Marino   return target_write_raw_memory (bp_tgt->placed_address, bp_tgt->shadow_contents,
755796c8dcSSimon Schubert 				  bp_tgt->placed_size);
765796c8dcSSimon Schubert }
775796c8dcSSimon Schubert 
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert int
805796c8dcSSimon Schubert memory_insert_breakpoint (struct gdbarch *gdbarch,
815796c8dcSSimon Schubert 			  struct bp_target_info *bp_tgt)
825796c8dcSSimon Schubert {
835796c8dcSSimon Schubert   return gdbarch_memory_insert_breakpoint (gdbarch, bp_tgt);
845796c8dcSSimon Schubert }
855796c8dcSSimon Schubert 
865796c8dcSSimon Schubert int
875796c8dcSSimon Schubert memory_remove_breakpoint (struct gdbarch *gdbarch,
885796c8dcSSimon Schubert 			  struct bp_target_info *bp_tgt)
895796c8dcSSimon Schubert {
905796c8dcSSimon Schubert   return gdbarch_memory_remove_breakpoint (gdbarch, bp_tgt);
915796c8dcSSimon Schubert }
92