15796c8dcSSimon Schubert /* Simulate breakpoints by patching locations in the target system, for GDB. 25796c8dcSSimon Schubert 3*ef5ccd6cSJohn Marino Copyright (C) 1990-2013 Free Software Foundation, Inc. 45796c8dcSSimon Schubert 55796c8dcSSimon Schubert Contributed by Cygnus Support. Written by John Gilmore. 65796c8dcSSimon Schubert 75796c8dcSSimon Schubert This file is part of GDB. 85796c8dcSSimon Schubert 95796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 105796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 115796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 125796c8dcSSimon Schubert (at your option) any later version. 135796c8dcSSimon Schubert 145796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 155796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 165796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 175796c8dcSSimon Schubert GNU General Public License for more details. 185796c8dcSSimon Schubert 195796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 205796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 215796c8dcSSimon Schubert 225796c8dcSSimon Schubert #include "defs.h" 235796c8dcSSimon Schubert #include "symtab.h" 245796c8dcSSimon Schubert #include "breakpoint.h" 255796c8dcSSimon Schubert #include "inferior.h" 265796c8dcSSimon Schubert #include "target.h" 27*ef5ccd6cSJohn Marino #include "gdb_string.h" 285796c8dcSSimon Schubert 295796c8dcSSimon Schubert 305796c8dcSSimon Schubert /* Insert a breakpoint on targets that don't have any better 315796c8dcSSimon Schubert breakpoint support. We read the contents of the target location 325796c8dcSSimon Schubert and stash it, then overwrite it with a breakpoint instruction. 335796c8dcSSimon Schubert BP_TGT->placed_address is the target location in the target 345796c8dcSSimon Schubert machine. BP_TGT->shadow_contents is some memory allocated for 355796c8dcSSimon Schubert saving the target contents. It is guaranteed by the caller to be 365796c8dcSSimon Schubert long enough to save BREAKPOINT_LEN bytes (this is accomplished via 375796c8dcSSimon Schubert BREAKPOINT_MAX). */ 385796c8dcSSimon Schubert 395796c8dcSSimon Schubert int 405796c8dcSSimon Schubert default_memory_insert_breakpoint (struct gdbarch *gdbarch, 415796c8dcSSimon Schubert struct bp_target_info *bp_tgt) 425796c8dcSSimon Schubert { 435796c8dcSSimon Schubert int val; 445796c8dcSSimon Schubert const unsigned char *bp; 45*ef5ccd6cSJohn Marino gdb_byte *readbuf; 465796c8dcSSimon Schubert 475796c8dcSSimon Schubert /* Determine appropriate breakpoint contents and size for this address. */ 485796c8dcSSimon Schubert bp = gdbarch_breakpoint_from_pc 495796c8dcSSimon Schubert (gdbarch, &bp_tgt->placed_address, &bp_tgt->placed_size); 505796c8dcSSimon Schubert if (bp == NULL) 515796c8dcSSimon Schubert error (_("Software breakpoints not implemented for this target.")); 525796c8dcSSimon Schubert 53*ef5ccd6cSJohn Marino /* Save the memory contents in the shadow_contents buffer and then 54*ef5ccd6cSJohn Marino write the breakpoint instruction. */ 555796c8dcSSimon Schubert bp_tgt->shadow_len = bp_tgt->placed_size; 56*ef5ccd6cSJohn Marino readbuf = alloca (bp_tgt->placed_size); 57*ef5ccd6cSJohn Marino val = target_read_memory (bp_tgt->placed_address, readbuf, 585796c8dcSSimon Schubert bp_tgt->placed_size); 595796c8dcSSimon Schubert if (val == 0) 60*ef5ccd6cSJohn Marino { 61*ef5ccd6cSJohn Marino memcpy (bp_tgt->shadow_contents, readbuf, bp_tgt->placed_size); 62a45ae5f8SJohn Marino val = target_write_raw_memory (bp_tgt->placed_address, bp, 635796c8dcSSimon Schubert bp_tgt->placed_size); 64*ef5ccd6cSJohn Marino } 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 { 74a45ae5f8SJohn 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