GDB (xrefs)
/tmp/gdb-8.1/gdb/mem-break.c
Go to the documentation of this file.
1 /* Simulate breakpoints by patching locations in the target system, for GDB.
2 
3  Copyright (C) 1990-2018 Free Software Foundation, Inc.
4 
5  Contributed by Cygnus Support. Written by John Gilmore.
6 
7  This file is part of GDB.
8 
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.
13 
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.
18 
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/>. */
21 
22 #include "defs.h"
23 #include "symtab.h"
24 #include "breakpoint.h"
25 #include "inferior.h"
26 #include "target.h"
27 /* Insert a breakpoint on targets that don't have any better
28  breakpoint support. We read the contents of the target location
29  and stash it, then overwrite it with a breakpoint instruction.
30  BP_TGT->placed_address is the target location in the target
31  machine. BP_TGT->shadow_contents is some memory allocated for
32  saving the target contents. It is guaranteed by the caller to be
33  long enough to save BREAKPOINT_LEN bytes (this is accomplished via
34  BREAKPOINT_MAX). */
35 
36 int
38  struct bp_target_info *bp_tgt)
39 {
40  CORE_ADDR addr = bp_tgt->placed_address;
41  const unsigned char *bp;
42  gdb_byte *readbuf;
43  int bplen;
44  int val;
45 
46  /* Determine appropriate breakpoint contents and size for this address. */
47  bp = gdbarch_sw_breakpoint_from_kind (gdbarch, bp_tgt->kind, &bplen);
48 
49  /* Save the memory contents in the shadow_contents buffer and then
50  write the breakpoint instruction. */
51  readbuf = (gdb_byte *) alloca (bplen);
52  val = target_read_memory (addr, readbuf, bplen);
53  if (val == 0)
54  {
55  /* These must be set together, either before or after the shadow
56  read, so that if we're "reinserting" a breakpoint that
57  doesn't have a shadow yet, the breakpoint masking code inside
58  target_read_memory doesn't mask out this breakpoint using an
59  unfilled shadow buffer. The core may be trying to reinsert a
60  permanent breakpoint, for targets that support breakpoint
61  conditions/commands on the target side for some types of
62  breakpoints, such as target remote. */
63  bp_tgt->shadow_len = bplen;
64  memcpy (bp_tgt->shadow_contents, readbuf, bplen);
65 
66  val = target_write_raw_memory (addr, bp, bplen);
67  }
68 
69  return val;
70 }
71 
72 
73 int
75  struct bp_target_info *bp_tgt)
76 {
77  int bplen;
78 
79  gdbarch_sw_breakpoint_from_kind (gdbarch, bp_tgt->kind, &bplen);
80 
82  bplen);
83 }
84 
85 
86 int
88  struct bp_target_info *bp_tgt)
89 {
91 }
92 
93 int
95  struct bp_target_info *bp_tgt,
96  enum remove_bp_reason reason)
97 {
99 }
100 
101 int
103  struct bp_target_info *bp_tgt)
104 {
105  CORE_ADDR addr = bp_tgt->placed_address;
106  const gdb_byte *bp;
107  int val;
108  int bplen;
109  gdb_byte cur_contents[BREAKPOINT_MAX];
110 
111  /* Determine appropriate breakpoint contents and size for this
112  address. */
113  bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &bplen);
114 
115  if (bp == NULL)
116  return 0;
117 
118  /* Make sure we see the memory breakpoints. */
119  scoped_restore restore_memory
121  val = target_read_memory (addr, cur_contents, bplen);
122 
123  /* If our breakpoint is no longer at the address, this means that
124  the program modified the code on us, so it is wrong to put back
125  the old value. */
126  return (val == 0 && memcmp (bp, cur_contents, bplen) == 0);
127 }
bfd_vma CORE_ADDR
Definition: common-types.h:41
scoped_restore_tmpl< int > make_scoped_restore_show_memory_breakpoints(int show)
Definition: target.c:1250
int gdbarch_memory_insert_breakpoint(struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition: gdbarch.c:2936
#define BREAKPOINT_MAX
Definition: breakpoint.h:62
gdb_byte shadow_contents[BREAKPOINT_MAX]
Definition: breakpoint.h:261
int target_write_raw_memory(CORE_ADDR memaddr, const gdb_byte *myaddr, ssize_t len)
Definition: target.c:1469
CORE_ADDR placed_address
Definition: breakpoint.h:248
const gdb_byte * gdbarch_sw_breakpoint_from_kind(struct gdbarch *gdbarch, int kind, int *size)
Definition: gdbarch.c:2878
int gdbarch_memory_remove_breakpoint(struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition: gdbarch.c:2953
bfd_byte gdb_byte
Definition: common-types.h:38
int default_memory_remove_breakpoint(struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition: mem-break.c:74
int target_read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition: target.c:1370
const gdb_byte * gdbarch_breakpoint_from_pc(struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
Definition: gdbarch.c:2844
int memory_validate_breakpoint(struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition: mem-break.c:102
int memory_insert_breakpoint(struct target_ops *ops, struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition: mem-break.c:87
int default_memory_insert_breakpoint(struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
Definition: mem-break.c:37
int memory_remove_breakpoint(struct target_ops *ops, struct gdbarch *gdbarch, struct bp_target_info *bp_tgt, enum remove_bp_reason reason)
Definition: mem-break.c:94
remove_bp_reason
Definition: breakpoint.h:47