1*56bb7041Schristos /* Memory breakpoint operations for the remote server for GDB.
2*56bb7041Schristos    Copyright (C) 2002-2020 Free Software Foundation, Inc.
3*56bb7041Schristos 
4*56bb7041Schristos    Contributed by MontaVista Software.
5*56bb7041Schristos 
6*56bb7041Schristos    This file is part of GDB.
7*56bb7041Schristos 
8*56bb7041Schristos    This program is free software; you can redistribute it and/or modify
9*56bb7041Schristos    it under the terms of the GNU General Public License as published by
10*56bb7041Schristos    the Free Software Foundation; either version 3 of the License, or
11*56bb7041Schristos    (at your option) any later version.
12*56bb7041Schristos 
13*56bb7041Schristos    This program is distributed in the hope that it will be useful,
14*56bb7041Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*56bb7041Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*56bb7041Schristos    GNU General Public License for more details.
17*56bb7041Schristos 
18*56bb7041Schristos    You should have received a copy of the GNU General Public License
19*56bb7041Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20*56bb7041Schristos 
21*56bb7041Schristos #include "server.h"
22*56bb7041Schristos #include "regcache.h"
23*56bb7041Schristos #include "ax.h"
24*56bb7041Schristos 
25*56bb7041Schristos #define MAX_BREAKPOINT_LEN 8
26*56bb7041Schristos 
27*56bb7041Schristos /* Helper macro used in loops that append multiple items to a singly-linked
28*56bb7041Schristos    list instead of inserting items at the head of the list, as, say, in the
29*56bb7041Schristos    breakpoint lists.  LISTPP is a pointer to the pointer that is the head of
30*56bb7041Schristos    the new list.  ITEMP is a pointer to the item to be added to the list.
31*56bb7041Schristos    TAILP must be defined to be the same type as ITEMP, and initialized to
32*56bb7041Schristos    NULL.  */
33*56bb7041Schristos 
34*56bb7041Schristos #define APPEND_TO_LIST(listpp, itemp, tailp) \
35*56bb7041Schristos 	  do \
36*56bb7041Schristos 	    { \
37*56bb7041Schristos 	      if ((tailp) == NULL) \
38*56bb7041Schristos 		*(listpp) = (itemp); \
39*56bb7041Schristos 	      else \
40*56bb7041Schristos 		(tailp)->next = (itemp); \
41*56bb7041Schristos 	      (tailp) = (itemp); \
42*56bb7041Schristos 	    } \
43*56bb7041Schristos 	  while (0)
44*56bb7041Schristos 
45*56bb7041Schristos /* GDB will never try to install multiple breakpoints at the same
46*56bb7041Schristos    address.  However, we can see GDB requesting to insert a breakpoint
47*56bb7041Schristos    at an address is had already inserted one previously in a few
48*56bb7041Schristos    situations.
49*56bb7041Schristos 
50*56bb7041Schristos    - The RSP documentation on Z packets says that to avoid potential
51*56bb7041Schristos    problems with duplicate packets, the operations should be
52*56bb7041Schristos    implemented in an idempotent way.
53*56bb7041Schristos 
54*56bb7041Schristos    - A breakpoint is set at ADDR, an address in a shared library.
55*56bb7041Schristos    Then the shared library is unloaded.  And then another, unrelated,
56*56bb7041Schristos    breakpoint at ADDR is set.  There is not breakpoint removal request
57*56bb7041Schristos    between the first and the second breakpoint.
58*56bb7041Schristos 
59*56bb7041Schristos    - When GDB wants to update the target-side breakpoint conditions or
60*56bb7041Schristos    commands, it re-inserts the breakpoint, with updated
61*56bb7041Schristos    conditions/commands associated.
62*56bb7041Schristos 
63*56bb7041Schristos    Also, we need to keep track of internal breakpoints too, so we do
64*56bb7041Schristos    need to be able to install multiple breakpoints at the same address
65*56bb7041Schristos    transparently.
66*56bb7041Schristos 
67*56bb7041Schristos    We keep track of two different, and closely related structures.  A
68*56bb7041Schristos    raw breakpoint, which manages the low level, close to the metal
69*56bb7041Schristos    aspect of a breakpoint.  It holds the breakpoint address, and for
70*56bb7041Schristos    software breakpoints, a buffer holding a copy of the instructions
71*56bb7041Schristos    that would be in memory had not been a breakpoint there (we call
72*56bb7041Schristos    that the shadow memory of the breakpoint).  We occasionally need to
73*56bb7041Schristos    temporarilly uninsert a breakpoint without the client knowing about
74*56bb7041Schristos    it (e.g., to step over an internal breakpoint), so we keep an
75*56bb7041Schristos    `inserted' state associated with this low level breakpoint
76*56bb7041Schristos    structure.  There can only be one such object for a given address.
77*56bb7041Schristos    Then, we have (a bit higher level) breakpoints.  This structure
78*56bb7041Schristos    holds a callback to be called whenever a breakpoint is hit, a
79*56bb7041Schristos    high-level type, and a link to a low level raw breakpoint.  There
80*56bb7041Schristos    can be many high-level breakpoints at the same address, and all of
81*56bb7041Schristos    them will point to the same raw breakpoint, which is reference
82*56bb7041Schristos    counted.  */
83*56bb7041Schristos 
84*56bb7041Schristos /* The low level, physical, raw breakpoint.  */
85*56bb7041Schristos struct raw_breakpoint
86*56bb7041Schristos {
87*56bb7041Schristos   struct raw_breakpoint *next;
88*56bb7041Schristos 
89*56bb7041Schristos   /* The low level type of the breakpoint (software breakpoint,
90*56bb7041Schristos      watchpoint, etc.)  */
91*56bb7041Schristos   enum raw_bkpt_type raw_type;
92*56bb7041Schristos 
93*56bb7041Schristos   /* A reference count.  Each high level breakpoint referencing this
94*56bb7041Schristos      raw breakpoint accounts for one reference.  */
95*56bb7041Schristos   int refcount;
96*56bb7041Schristos 
97*56bb7041Schristos   /* The breakpoint's insertion address.  There can only be one raw
98*56bb7041Schristos      breakpoint for a given PC.  */
99*56bb7041Schristos   CORE_ADDR pc;
100*56bb7041Schristos 
101*56bb7041Schristos   /* The breakpoint's kind.  This is target specific.  Most
102*56bb7041Schristos      architectures only use one specific instruction for breakpoints, while
103*56bb7041Schristos      others may use more than one.  E.g., on ARM, we need to use different
104*56bb7041Schristos      breakpoint instructions on Thumb, Thumb-2, and ARM code.  Likewise for
105*56bb7041Schristos      hardware breakpoints -- some architectures (including ARM) need to
106*56bb7041Schristos      setup debug registers differently depending on mode.  */
107*56bb7041Schristos   int kind;
108*56bb7041Schristos 
109*56bb7041Schristos   /* The breakpoint's shadow memory.  */
110*56bb7041Schristos   unsigned char old_data[MAX_BREAKPOINT_LEN];
111*56bb7041Schristos 
112*56bb7041Schristos   /* Positive if this breakpoint is currently inserted in the
113*56bb7041Schristos      inferior.  Negative if it was, but we've detected that it's now
114*56bb7041Schristos      gone.  Zero if not inserted.  */
115*56bb7041Schristos   int inserted;
116*56bb7041Schristos };
117*56bb7041Schristos 
118*56bb7041Schristos /* The type of a breakpoint.  */
119*56bb7041Schristos enum bkpt_type
120*56bb7041Schristos   {
121*56bb7041Schristos     /* A GDB breakpoint, requested with a Z0 packet.  */
122*56bb7041Schristos     gdb_breakpoint_Z0,
123*56bb7041Schristos 
124*56bb7041Schristos     /* A GDB hardware breakpoint, requested with a Z1 packet.  */
125*56bb7041Schristos     gdb_breakpoint_Z1,
126*56bb7041Schristos 
127*56bb7041Schristos     /* A GDB write watchpoint, requested with a Z2 packet.  */
128*56bb7041Schristos     gdb_breakpoint_Z2,
129*56bb7041Schristos 
130*56bb7041Schristos     /* A GDB read watchpoint, requested with a Z3 packet.  */
131*56bb7041Schristos     gdb_breakpoint_Z3,
132*56bb7041Schristos 
133*56bb7041Schristos     /* A GDB access watchpoint, requested with a Z4 packet.  */
134*56bb7041Schristos     gdb_breakpoint_Z4,
135*56bb7041Schristos 
136*56bb7041Schristos     /* A software single-step breakpoint.  */
137*56bb7041Schristos     single_step_breakpoint,
138*56bb7041Schristos 
139*56bb7041Schristos     /* Any other breakpoint type that doesn't require specific
140*56bb7041Schristos        treatment goes here.  E.g., an event breakpoint.  */
141*56bb7041Schristos     other_breakpoint,
142*56bb7041Schristos   };
143*56bb7041Schristos 
144*56bb7041Schristos struct point_cond_list
145*56bb7041Schristos {
146*56bb7041Schristos   /* Pointer to the agent expression that is the breakpoint's
147*56bb7041Schristos      conditional.  */
148*56bb7041Schristos   struct agent_expr *cond;
149*56bb7041Schristos 
150*56bb7041Schristos   /* Pointer to the next condition.  */
151*56bb7041Schristos   struct point_cond_list *next;
152*56bb7041Schristos };
153*56bb7041Schristos 
154*56bb7041Schristos struct point_command_list
155*56bb7041Schristos {
156*56bb7041Schristos   /* Pointer to the agent expression that is the breakpoint's
157*56bb7041Schristos      commands.  */
158*56bb7041Schristos   struct agent_expr *cmd;
159*56bb7041Schristos 
160*56bb7041Schristos   /* Flag that is true if this command should run even while GDB is
161*56bb7041Schristos      disconnected.  */
162*56bb7041Schristos   int persistence;
163*56bb7041Schristos 
164*56bb7041Schristos   /* Pointer to the next command.  */
165*56bb7041Schristos   struct point_command_list *next;
166*56bb7041Schristos };
167*56bb7041Schristos 
168*56bb7041Schristos /* A high level (in gdbserver's perspective) breakpoint.  */
169*56bb7041Schristos struct breakpoint
170*56bb7041Schristos {
171*56bb7041Schristos   struct breakpoint *next;
172*56bb7041Schristos 
173*56bb7041Schristos   /* The breakpoint's type.  */
174*56bb7041Schristos   enum bkpt_type type;
175*56bb7041Schristos 
176*56bb7041Schristos   /* Link to this breakpoint's raw breakpoint.  This is always
177*56bb7041Schristos      non-NULL.  */
178*56bb7041Schristos   struct raw_breakpoint *raw;
179*56bb7041Schristos };
180*56bb7041Schristos 
181*56bb7041Schristos /* Breakpoint requested by GDB.  */
182*56bb7041Schristos 
183*56bb7041Schristos struct gdb_breakpoint
184*56bb7041Schristos {
185*56bb7041Schristos   struct breakpoint base;
186*56bb7041Schristos 
187*56bb7041Schristos   /* Pointer to the condition list that should be evaluated on
188*56bb7041Schristos      the target or NULL if the breakpoint is unconditional or
189*56bb7041Schristos      if GDB doesn't want us to evaluate the conditionals on the
190*56bb7041Schristos      target's side.  */
191*56bb7041Schristos   struct point_cond_list *cond_list;
192*56bb7041Schristos 
193*56bb7041Schristos   /* Point to the list of commands to run when this is hit.  */
194*56bb7041Schristos   struct point_command_list *command_list;
195*56bb7041Schristos };
196*56bb7041Schristos 
197*56bb7041Schristos /* Breakpoint used by GDBserver.  */
198*56bb7041Schristos 
199*56bb7041Schristos struct other_breakpoint
200*56bb7041Schristos {
201*56bb7041Schristos   struct breakpoint base;
202*56bb7041Schristos 
203*56bb7041Schristos   /* Function to call when we hit this breakpoint.  If it returns 1,
204*56bb7041Schristos      the breakpoint shall be deleted; 0 or if this callback is NULL,
205*56bb7041Schristos      it will be left inserted.  */
206*56bb7041Schristos   int (*handler) (CORE_ADDR);
207*56bb7041Schristos };
208*56bb7041Schristos 
209*56bb7041Schristos /* Breakpoint for single step.  */
210*56bb7041Schristos 
211*56bb7041Schristos struct single_step_breakpoint
212*56bb7041Schristos {
213*56bb7041Schristos   struct breakpoint base;
214*56bb7041Schristos 
215*56bb7041Schristos   /* Thread the reinsert breakpoint belongs to.  */
216*56bb7041Schristos   ptid_t ptid;
217*56bb7041Schristos };
218*56bb7041Schristos 
219*56bb7041Schristos /* Return the breakpoint size from its kind.  */
220*56bb7041Schristos 
221*56bb7041Schristos static int
bp_size(struct raw_breakpoint * bp)222*56bb7041Schristos bp_size (struct raw_breakpoint *bp)
223*56bb7041Schristos {
224*56bb7041Schristos   int size = 0;
225*56bb7041Schristos 
226*56bb7041Schristos   the_target->sw_breakpoint_from_kind (bp->kind, &size);
227*56bb7041Schristos   return size;
228*56bb7041Schristos }
229*56bb7041Schristos 
230*56bb7041Schristos /* Return the breakpoint opcode from its kind.  */
231*56bb7041Schristos 
232*56bb7041Schristos static const gdb_byte *
bp_opcode(struct raw_breakpoint * bp)233*56bb7041Schristos bp_opcode (struct raw_breakpoint *bp)
234*56bb7041Schristos {
235*56bb7041Schristos   int size = 0;
236*56bb7041Schristos 
237*56bb7041Schristos   return the_target->sw_breakpoint_from_kind (bp->kind, &size);
238*56bb7041Schristos }
239*56bb7041Schristos 
240*56bb7041Schristos /* See mem-break.h.  */
241*56bb7041Schristos 
242*56bb7041Schristos enum target_hw_bp_type
raw_bkpt_type_to_target_hw_bp_type(enum raw_bkpt_type raw_type)243*56bb7041Schristos raw_bkpt_type_to_target_hw_bp_type (enum raw_bkpt_type raw_type)
244*56bb7041Schristos {
245*56bb7041Schristos   switch (raw_type)
246*56bb7041Schristos     {
247*56bb7041Schristos     case raw_bkpt_type_hw:
248*56bb7041Schristos       return hw_execute;
249*56bb7041Schristos     case raw_bkpt_type_write_wp:
250*56bb7041Schristos       return hw_write;
251*56bb7041Schristos     case raw_bkpt_type_read_wp:
252*56bb7041Schristos       return hw_read;
253*56bb7041Schristos     case raw_bkpt_type_access_wp:
254*56bb7041Schristos       return hw_access;
255*56bb7041Schristos     default:
256*56bb7041Schristos       internal_error (__FILE__, __LINE__,
257*56bb7041Schristos 		      "bad raw breakpoint type %d", (int) raw_type);
258*56bb7041Schristos     }
259*56bb7041Schristos }
260*56bb7041Schristos 
261*56bb7041Schristos /* See mem-break.h.  */
262*56bb7041Schristos 
263*56bb7041Schristos static enum bkpt_type
Z_packet_to_bkpt_type(char z_type)264*56bb7041Schristos Z_packet_to_bkpt_type (char z_type)
265*56bb7041Schristos {
266*56bb7041Schristos   gdb_assert ('0' <= z_type && z_type <= '4');
267*56bb7041Schristos 
268*56bb7041Schristos   return (enum bkpt_type) (gdb_breakpoint_Z0 + (z_type - '0'));
269*56bb7041Schristos }
270*56bb7041Schristos 
271*56bb7041Schristos /* See mem-break.h.  */
272*56bb7041Schristos 
273*56bb7041Schristos enum raw_bkpt_type
Z_packet_to_raw_bkpt_type(char z_type)274*56bb7041Schristos Z_packet_to_raw_bkpt_type (char z_type)
275*56bb7041Schristos {
276*56bb7041Schristos   switch (z_type)
277*56bb7041Schristos     {
278*56bb7041Schristos     case Z_PACKET_SW_BP:
279*56bb7041Schristos       return raw_bkpt_type_sw;
280*56bb7041Schristos     case Z_PACKET_HW_BP:
281*56bb7041Schristos       return raw_bkpt_type_hw;
282*56bb7041Schristos     case Z_PACKET_WRITE_WP:
283*56bb7041Schristos       return raw_bkpt_type_write_wp;
284*56bb7041Schristos     case Z_PACKET_READ_WP:
285*56bb7041Schristos       return raw_bkpt_type_read_wp;
286*56bb7041Schristos     case Z_PACKET_ACCESS_WP:
287*56bb7041Schristos       return raw_bkpt_type_access_wp;
288*56bb7041Schristos     default:
289*56bb7041Schristos       gdb_assert_not_reached ("unhandled Z packet type.");
290*56bb7041Schristos     }
291*56bb7041Schristos }
292*56bb7041Schristos 
293*56bb7041Schristos /* Return true if breakpoint TYPE is a GDB breakpoint.  */
294*56bb7041Schristos 
295*56bb7041Schristos static int
is_gdb_breakpoint(enum bkpt_type type)296*56bb7041Schristos is_gdb_breakpoint (enum bkpt_type type)
297*56bb7041Schristos {
298*56bb7041Schristos   return (type == gdb_breakpoint_Z0
299*56bb7041Schristos 	  || type == gdb_breakpoint_Z1
300*56bb7041Schristos 	  || type == gdb_breakpoint_Z2
301*56bb7041Schristos 	  || type == gdb_breakpoint_Z3
302*56bb7041Schristos 	  || type == gdb_breakpoint_Z4);
303*56bb7041Schristos }
304*56bb7041Schristos 
305*56bb7041Schristos bool
any_persistent_commands(process_info * proc)306*56bb7041Schristos any_persistent_commands (process_info *proc)
307*56bb7041Schristos {
308*56bb7041Schristos   struct breakpoint *bp;
309*56bb7041Schristos   struct point_command_list *cl;
310*56bb7041Schristos 
311*56bb7041Schristos   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
312*56bb7041Schristos     {
313*56bb7041Schristos       if (is_gdb_breakpoint (bp->type))
314*56bb7041Schristos 	{
315*56bb7041Schristos 	  struct gdb_breakpoint *gdb_bp = (struct gdb_breakpoint *) bp;
316*56bb7041Schristos 
317*56bb7041Schristos 	  for (cl = gdb_bp->command_list; cl != NULL; cl = cl->next)
318*56bb7041Schristos 	    if (cl->persistence)
319*56bb7041Schristos 	      return true;
320*56bb7041Schristos 	}
321*56bb7041Schristos     }
322*56bb7041Schristos 
323*56bb7041Schristos   return false;
324*56bb7041Schristos }
325*56bb7041Schristos 
326*56bb7041Schristos /* Find low-level breakpoint of type TYPE at address ADDR that is not
327*56bb7041Schristos    insert-disabled.  Returns NULL if not found.  */
328*56bb7041Schristos 
329*56bb7041Schristos static struct raw_breakpoint *
find_enabled_raw_code_breakpoint_at(CORE_ADDR addr,enum raw_bkpt_type type)330*56bb7041Schristos find_enabled_raw_code_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type)
331*56bb7041Schristos {
332*56bb7041Schristos   struct process_info *proc = current_process ();
333*56bb7041Schristos   struct raw_breakpoint *bp;
334*56bb7041Schristos 
335*56bb7041Schristos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
336*56bb7041Schristos     if (bp->pc == addr
337*56bb7041Schristos 	&& bp->raw_type == type
338*56bb7041Schristos 	&& bp->inserted >= 0)
339*56bb7041Schristos       return bp;
340*56bb7041Schristos 
341*56bb7041Schristos   return NULL;
342*56bb7041Schristos }
343*56bb7041Schristos 
344*56bb7041Schristos /* Find low-level breakpoint of type TYPE at address ADDR.  Returns
345*56bb7041Schristos    NULL if not found.  */
346*56bb7041Schristos 
347*56bb7041Schristos static struct raw_breakpoint *
find_raw_breakpoint_at(CORE_ADDR addr,enum raw_bkpt_type type,int kind)348*56bb7041Schristos find_raw_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type, int kind)
349*56bb7041Schristos {
350*56bb7041Schristos   struct process_info *proc = current_process ();
351*56bb7041Schristos   struct raw_breakpoint *bp;
352*56bb7041Schristos 
353*56bb7041Schristos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
354*56bb7041Schristos     if (bp->pc == addr && bp->raw_type == type && bp->kind == kind)
355*56bb7041Schristos       return bp;
356*56bb7041Schristos 
357*56bb7041Schristos   return NULL;
358*56bb7041Schristos }
359*56bb7041Schristos 
360*56bb7041Schristos /* See mem-break.h.  */
361*56bb7041Schristos 
362*56bb7041Schristos int
insert_memory_breakpoint(struct raw_breakpoint * bp)363*56bb7041Schristos insert_memory_breakpoint (struct raw_breakpoint *bp)
364*56bb7041Schristos {
365*56bb7041Schristos   unsigned char buf[MAX_BREAKPOINT_LEN];
366*56bb7041Schristos   int err;
367*56bb7041Schristos 
368*56bb7041Schristos   /* Note that there can be fast tracepoint jumps installed in the
369*56bb7041Schristos      same memory range, so to get at the original memory, we need to
370*56bb7041Schristos      use read_inferior_memory, which masks those out.  */
371*56bb7041Schristos   err = read_inferior_memory (bp->pc, buf, bp_size (bp));
372*56bb7041Schristos   if (err != 0)
373*56bb7041Schristos     {
374*56bb7041Schristos       if (debug_threads)
375*56bb7041Schristos 	debug_printf ("Failed to read shadow memory of"
376*56bb7041Schristos 		      " breakpoint at 0x%s (%s).\n",
377*56bb7041Schristos 		      paddress (bp->pc), safe_strerror (err));
378*56bb7041Schristos     }
379*56bb7041Schristos   else
380*56bb7041Schristos     {
381*56bb7041Schristos       memcpy (bp->old_data, buf, bp_size (bp));
382*56bb7041Schristos 
383*56bb7041Schristos       err = the_target->write_memory (bp->pc, bp_opcode (bp),
384*56bb7041Schristos 				      bp_size (bp));
385*56bb7041Schristos       if (err != 0)
386*56bb7041Schristos 	{
387*56bb7041Schristos 	  if (debug_threads)
388*56bb7041Schristos 	    debug_printf ("Failed to insert breakpoint at 0x%s (%s).\n",
389*56bb7041Schristos 			  paddress (bp->pc), safe_strerror (err));
390*56bb7041Schristos 	}
391*56bb7041Schristos     }
392*56bb7041Schristos   return err != 0 ? -1 : 0;
393*56bb7041Schristos }
394*56bb7041Schristos 
395*56bb7041Schristos /* See mem-break.h  */
396*56bb7041Schristos 
397*56bb7041Schristos int
remove_memory_breakpoint(struct raw_breakpoint * bp)398*56bb7041Schristos remove_memory_breakpoint (struct raw_breakpoint *bp)
399*56bb7041Schristos {
400*56bb7041Schristos   unsigned char buf[MAX_BREAKPOINT_LEN];
401*56bb7041Schristos   int err;
402*56bb7041Schristos 
403*56bb7041Schristos   /* Since there can be trap breakpoints inserted in the same address
404*56bb7041Schristos      range, we use `target_write_memory', which takes care of
405*56bb7041Schristos      layering breakpoints on top of fast tracepoints, and on top of
406*56bb7041Schristos      the buffer we pass it.  This works because the caller has already
407*56bb7041Schristos      either unlinked the breakpoint or marked it uninserted.  Also
408*56bb7041Schristos      note that we need to pass the current shadow contents, because
409*56bb7041Schristos      target_write_memory updates any shadow memory with what we pass
410*56bb7041Schristos      here, and we want that to be a nop.  */
411*56bb7041Schristos   memcpy (buf, bp->old_data, bp_size (bp));
412*56bb7041Schristos   err = target_write_memory (bp->pc, buf, bp_size (bp));
413*56bb7041Schristos   if (err != 0)
414*56bb7041Schristos     {
415*56bb7041Schristos       if (debug_threads)
416*56bb7041Schristos 	debug_printf ("Failed to uninsert raw breakpoint "
417*56bb7041Schristos 		      "at 0x%s (%s) while deleting it.\n",
418*56bb7041Schristos 		      paddress (bp->pc), safe_strerror (err));
419*56bb7041Schristos     }
420*56bb7041Schristos   return err != 0 ? -1 : 0;
421*56bb7041Schristos }
422*56bb7041Schristos 
423*56bb7041Schristos /* Set a RAW breakpoint of type TYPE and kind KIND at WHERE.  On
424*56bb7041Schristos    success, a pointer to the new breakpoint is returned.  On failure,
425*56bb7041Schristos    returns NULL and writes the error code to *ERR.  */
426*56bb7041Schristos 
427*56bb7041Schristos static struct raw_breakpoint *
set_raw_breakpoint_at(enum raw_bkpt_type type,CORE_ADDR where,int kind,int * err)428*56bb7041Schristos set_raw_breakpoint_at (enum raw_bkpt_type type, CORE_ADDR where, int kind,
429*56bb7041Schristos 		       int *err)
430*56bb7041Schristos {
431*56bb7041Schristos   struct process_info *proc = current_process ();
432*56bb7041Schristos   struct raw_breakpoint *bp;
433*56bb7041Schristos 
434*56bb7041Schristos   if (type == raw_bkpt_type_sw || type == raw_bkpt_type_hw)
435*56bb7041Schristos     {
436*56bb7041Schristos       bp = find_enabled_raw_code_breakpoint_at (where, type);
437*56bb7041Schristos       if (bp != NULL && bp->kind != kind)
438*56bb7041Schristos 	{
439*56bb7041Schristos 	  /* A different kind than previously seen.  The previous
440*56bb7041Schristos 	     breakpoint must be gone then.  */
441*56bb7041Schristos 	  if (debug_threads)
442*56bb7041Schristos 	    debug_printf ("Inconsistent breakpoint kind?  Was %d, now %d.\n",
443*56bb7041Schristos 			  bp->kind, kind);
444*56bb7041Schristos 	  bp->inserted = -1;
445*56bb7041Schristos 	  bp = NULL;
446*56bb7041Schristos 	}
447*56bb7041Schristos     }
448*56bb7041Schristos   else
449*56bb7041Schristos     bp = find_raw_breakpoint_at (where, type, kind);
450*56bb7041Schristos 
451*56bb7041Schristos   gdb::unique_xmalloc_ptr<struct raw_breakpoint> bp_holder;
452*56bb7041Schristos   if (bp == NULL)
453*56bb7041Schristos     {
454*56bb7041Schristos       bp_holder.reset (XCNEW (struct raw_breakpoint));
455*56bb7041Schristos       bp = bp_holder.get ();
456*56bb7041Schristos       bp->pc = where;
457*56bb7041Schristos       bp->kind = kind;
458*56bb7041Schristos       bp->raw_type = type;
459*56bb7041Schristos     }
460*56bb7041Schristos 
461*56bb7041Schristos   if (!bp->inserted)
462*56bb7041Schristos     {
463*56bb7041Schristos       *err = the_target->insert_point (bp->raw_type, bp->pc, bp->kind, bp);
464*56bb7041Schristos       if (*err != 0)
465*56bb7041Schristos 	{
466*56bb7041Schristos 	  if (debug_threads)
467*56bb7041Schristos 	    debug_printf ("Failed to insert breakpoint at 0x%s (%d).\n",
468*56bb7041Schristos 			  paddress (where), *err);
469*56bb7041Schristos 
470*56bb7041Schristos 	  return NULL;
471*56bb7041Schristos 	}
472*56bb7041Schristos 
473*56bb7041Schristos       bp->inserted = 1;
474*56bb7041Schristos     }
475*56bb7041Schristos 
476*56bb7041Schristos   /* If the breakpoint was allocated above, we know we want to keep it
477*56bb7041Schristos      now.  */
478*56bb7041Schristos   bp_holder.release ();
479*56bb7041Schristos 
480*56bb7041Schristos   /* Link the breakpoint in, if this is the first reference.  */
481*56bb7041Schristos   if (++bp->refcount == 1)
482*56bb7041Schristos     {
483*56bb7041Schristos       bp->next = proc->raw_breakpoints;
484*56bb7041Schristos       proc->raw_breakpoints = bp;
485*56bb7041Schristos     }
486*56bb7041Schristos   return bp;
487*56bb7041Schristos }
488*56bb7041Schristos 
489*56bb7041Schristos /* Notice that breakpoint traps are always installed on top of fast
490*56bb7041Schristos    tracepoint jumps.  This is even if the fast tracepoint is installed
491*56bb7041Schristos    at a later time compared to when the breakpoint was installed.
492*56bb7041Schristos    This means that a stopping breakpoint or tracepoint has higher
493*56bb7041Schristos    "priority".  In turn, this allows having fast and slow tracepoints
494*56bb7041Schristos    (and breakpoints) at the same address behave correctly.  */
495*56bb7041Schristos 
496*56bb7041Schristos 
497*56bb7041Schristos /* A fast tracepoint jump.  */
498*56bb7041Schristos 
499*56bb7041Schristos struct fast_tracepoint_jump
500*56bb7041Schristos {
501*56bb7041Schristos   struct fast_tracepoint_jump *next;
502*56bb7041Schristos 
503*56bb7041Schristos   /* A reference count.  GDB can install more than one fast tracepoint
504*56bb7041Schristos      at the same address (each with its own action list, for
505*56bb7041Schristos      example).  */
506*56bb7041Schristos   int refcount;
507*56bb7041Schristos 
508*56bb7041Schristos   /* The fast tracepoint's insertion address.  There can only be one
509*56bb7041Schristos      of these for a given PC.  */
510*56bb7041Schristos   CORE_ADDR pc;
511*56bb7041Schristos 
512*56bb7041Schristos   /* Non-zero if this fast tracepoint jump is currently inserted in
513*56bb7041Schristos      the inferior.  */
514*56bb7041Schristos   int inserted;
515*56bb7041Schristos 
516*56bb7041Schristos   /* The length of the jump instruction.  */
517*56bb7041Schristos   int length;
518*56bb7041Schristos 
519*56bb7041Schristos   /* A poor-man's flexible array member, holding both the jump
520*56bb7041Schristos      instruction to insert, and a copy of the instruction that would
521*56bb7041Schristos      be in memory had not been a jump there (the shadow memory of the
522*56bb7041Schristos      tracepoint jump).  */
523*56bb7041Schristos   unsigned char insn_and_shadow[0];
524*56bb7041Schristos };
525*56bb7041Schristos 
526*56bb7041Schristos /* Fast tracepoint FP's jump instruction to insert.  */
527*56bb7041Schristos #define fast_tracepoint_jump_insn(fp) \
528*56bb7041Schristos   ((fp)->insn_and_shadow + 0)
529*56bb7041Schristos 
530*56bb7041Schristos /* The shadow memory of fast tracepoint jump FP.  */
531*56bb7041Schristos #define fast_tracepoint_jump_shadow(fp) \
532*56bb7041Schristos   ((fp)->insn_and_shadow + (fp)->length)
533*56bb7041Schristos 
534*56bb7041Schristos 
535*56bb7041Schristos /* Return the fast tracepoint jump set at WHERE.  */
536*56bb7041Schristos 
537*56bb7041Schristos static struct fast_tracepoint_jump *
find_fast_tracepoint_jump_at(CORE_ADDR where)538*56bb7041Schristos find_fast_tracepoint_jump_at (CORE_ADDR where)
539*56bb7041Schristos {
540*56bb7041Schristos   struct process_info *proc = current_process ();
541*56bb7041Schristos   struct fast_tracepoint_jump *jp;
542*56bb7041Schristos 
543*56bb7041Schristos   for (jp = proc->fast_tracepoint_jumps; jp != NULL; jp = jp->next)
544*56bb7041Schristos     if (jp->pc == where)
545*56bb7041Schristos       return jp;
546*56bb7041Schristos 
547*56bb7041Schristos   return NULL;
548*56bb7041Schristos }
549*56bb7041Schristos 
550*56bb7041Schristos int
fast_tracepoint_jump_here(CORE_ADDR where)551*56bb7041Schristos fast_tracepoint_jump_here (CORE_ADDR where)
552*56bb7041Schristos {
553*56bb7041Schristos   struct fast_tracepoint_jump *jp = find_fast_tracepoint_jump_at (where);
554*56bb7041Schristos 
555*56bb7041Schristos   return (jp != NULL);
556*56bb7041Schristos }
557*56bb7041Schristos 
558*56bb7041Schristos int
delete_fast_tracepoint_jump(struct fast_tracepoint_jump * todel)559*56bb7041Schristos delete_fast_tracepoint_jump (struct fast_tracepoint_jump *todel)
560*56bb7041Schristos {
561*56bb7041Schristos   struct fast_tracepoint_jump *bp, **bp_link;
562*56bb7041Schristos   int ret;
563*56bb7041Schristos   struct process_info *proc = current_process ();
564*56bb7041Schristos 
565*56bb7041Schristos   bp = proc->fast_tracepoint_jumps;
566*56bb7041Schristos   bp_link = &proc->fast_tracepoint_jumps;
567*56bb7041Schristos 
568*56bb7041Schristos   while (bp)
569*56bb7041Schristos     {
570*56bb7041Schristos       if (bp == todel)
571*56bb7041Schristos 	{
572*56bb7041Schristos 	  if (--bp->refcount == 0)
573*56bb7041Schristos 	    {
574*56bb7041Schristos 	      struct fast_tracepoint_jump *prev_bp_link = *bp_link;
575*56bb7041Schristos 	      unsigned char *buf;
576*56bb7041Schristos 
577*56bb7041Schristos 	      /* Unlink it.  */
578*56bb7041Schristos 	      *bp_link = bp->next;
579*56bb7041Schristos 
580*56bb7041Schristos 	      /* Since there can be breakpoints inserted in the same
581*56bb7041Schristos 		 address range, we use `target_write_memory', which
582*56bb7041Schristos 		 takes care of layering breakpoints on top of fast
583*56bb7041Schristos 		 tracepoints, and on top of the buffer we pass it.
584*56bb7041Schristos 		 This works because we've already unlinked the fast
585*56bb7041Schristos 		 tracepoint jump above.  Also note that we need to
586*56bb7041Schristos 		 pass the current shadow contents, because
587*56bb7041Schristos 		 target_write_memory updates any shadow memory with
588*56bb7041Schristos 		 what we pass here, and we want that to be a nop.  */
589*56bb7041Schristos 	      buf = (unsigned char *) alloca (bp->length);
590*56bb7041Schristos 	      memcpy (buf, fast_tracepoint_jump_shadow (bp), bp->length);
591*56bb7041Schristos 	      ret = target_write_memory (bp->pc, buf, bp->length);
592*56bb7041Schristos 	      if (ret != 0)
593*56bb7041Schristos 		{
594*56bb7041Schristos 		  /* Something went wrong, relink the jump.  */
595*56bb7041Schristos 		  *bp_link = prev_bp_link;
596*56bb7041Schristos 
597*56bb7041Schristos 		  if (debug_threads)
598*56bb7041Schristos 		    debug_printf ("Failed to uninsert fast tracepoint jump "
599*56bb7041Schristos 				  "at 0x%s (%s) while deleting it.\n",
600*56bb7041Schristos 				  paddress (bp->pc), safe_strerror (ret));
601*56bb7041Schristos 		  return ret;
602*56bb7041Schristos 		}
603*56bb7041Schristos 
604*56bb7041Schristos 	      free (bp);
605*56bb7041Schristos 	    }
606*56bb7041Schristos 
607*56bb7041Schristos 	  return 0;
608*56bb7041Schristos 	}
609*56bb7041Schristos       else
610*56bb7041Schristos 	{
611*56bb7041Schristos 	  bp_link = &bp->next;
612*56bb7041Schristos 	  bp = *bp_link;
613*56bb7041Schristos 	}
614*56bb7041Schristos     }
615*56bb7041Schristos 
616*56bb7041Schristos   warning ("Could not find fast tracepoint jump in list.");
617*56bb7041Schristos   return ENOENT;
618*56bb7041Schristos }
619*56bb7041Schristos 
620*56bb7041Schristos void
inc_ref_fast_tracepoint_jump(struct fast_tracepoint_jump * jp)621*56bb7041Schristos inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump *jp)
622*56bb7041Schristos {
623*56bb7041Schristos   jp->refcount++;
624*56bb7041Schristos }
625*56bb7041Schristos 
626*56bb7041Schristos struct fast_tracepoint_jump *
set_fast_tracepoint_jump(CORE_ADDR where,unsigned char * insn,ULONGEST length)627*56bb7041Schristos set_fast_tracepoint_jump (CORE_ADDR where,
628*56bb7041Schristos 			  unsigned char *insn, ULONGEST length)
629*56bb7041Schristos {
630*56bb7041Schristos   struct process_info *proc = current_process ();
631*56bb7041Schristos   struct fast_tracepoint_jump *jp;
632*56bb7041Schristos   int err;
633*56bb7041Schristos   unsigned char *buf;
634*56bb7041Schristos 
635*56bb7041Schristos   /* We refcount fast tracepoint jumps.  Check if we already know
636*56bb7041Schristos      about a jump at this address.  */
637*56bb7041Schristos   jp = find_fast_tracepoint_jump_at (where);
638*56bb7041Schristos   if (jp != NULL)
639*56bb7041Schristos     {
640*56bb7041Schristos       jp->refcount++;
641*56bb7041Schristos       return jp;
642*56bb7041Schristos     }
643*56bb7041Schristos 
644*56bb7041Schristos   /* We don't, so create a new object.  Double the length, because the
645*56bb7041Schristos      flexible array member holds both the jump insn, and the
646*56bb7041Schristos      shadow.  */
647*56bb7041Schristos   jp = (struct fast_tracepoint_jump *) xcalloc (1, sizeof (*jp) + (length * 2));
648*56bb7041Schristos   jp->pc = where;
649*56bb7041Schristos   jp->length = length;
650*56bb7041Schristos   memcpy (fast_tracepoint_jump_insn (jp), insn, length);
651*56bb7041Schristos   jp->refcount = 1;
652*56bb7041Schristos   buf = (unsigned char *) alloca (length);
653*56bb7041Schristos 
654*56bb7041Schristos   /* Note that there can be trap breakpoints inserted in the same
655*56bb7041Schristos      address range.  To access the original memory contents, we use
656*56bb7041Schristos      `read_inferior_memory', which masks out breakpoints.  */
657*56bb7041Schristos   err = read_inferior_memory (where, buf, length);
658*56bb7041Schristos   if (err != 0)
659*56bb7041Schristos     {
660*56bb7041Schristos       if (debug_threads)
661*56bb7041Schristos 	debug_printf ("Failed to read shadow memory of"
662*56bb7041Schristos 		      " fast tracepoint at 0x%s (%s).\n",
663*56bb7041Schristos 		      paddress (where), safe_strerror (err));
664*56bb7041Schristos       free (jp);
665*56bb7041Schristos       return NULL;
666*56bb7041Schristos     }
667*56bb7041Schristos   memcpy (fast_tracepoint_jump_shadow (jp), buf, length);
668*56bb7041Schristos 
669*56bb7041Schristos   /* Link the jump in.  */
670*56bb7041Schristos   jp->inserted = 1;
671*56bb7041Schristos   jp->next = proc->fast_tracepoint_jumps;
672*56bb7041Schristos   proc->fast_tracepoint_jumps = jp;
673*56bb7041Schristos 
674*56bb7041Schristos   /* Since there can be trap breakpoints inserted in the same address
675*56bb7041Schristos      range, we use use `target_write_memory', which takes care of
676*56bb7041Schristos      layering breakpoints on top of fast tracepoints, on top of the
677*56bb7041Schristos      buffer we pass it.  This works because we've already linked in
678*56bb7041Schristos      the fast tracepoint jump above.  Also note that we need to pass
679*56bb7041Schristos      the current shadow contents, because target_write_memory
680*56bb7041Schristos      updates any shadow memory with what we pass here, and we want
681*56bb7041Schristos      that to be a nop.  */
682*56bb7041Schristos   err = target_write_memory (where, buf, length);
683*56bb7041Schristos   if (err != 0)
684*56bb7041Schristos     {
685*56bb7041Schristos       if (debug_threads)
686*56bb7041Schristos 	debug_printf ("Failed to insert fast tracepoint jump at 0x%s (%s).\n",
687*56bb7041Schristos 		      paddress (where), safe_strerror (err));
688*56bb7041Schristos 
689*56bb7041Schristos       /* Unlink it.  */
690*56bb7041Schristos       proc->fast_tracepoint_jumps = jp->next;
691*56bb7041Schristos       free (jp);
692*56bb7041Schristos 
693*56bb7041Schristos       return NULL;
694*56bb7041Schristos     }
695*56bb7041Schristos 
696*56bb7041Schristos   return jp;
697*56bb7041Schristos }
698*56bb7041Schristos 
699*56bb7041Schristos void
uninsert_fast_tracepoint_jumps_at(CORE_ADDR pc)700*56bb7041Schristos uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc)
701*56bb7041Schristos {
702*56bb7041Schristos   struct fast_tracepoint_jump *jp;
703*56bb7041Schristos   int err;
704*56bb7041Schristos 
705*56bb7041Schristos   jp = find_fast_tracepoint_jump_at (pc);
706*56bb7041Schristos   if (jp == NULL)
707*56bb7041Schristos     {
708*56bb7041Schristos       /* This can happen when we remove all breakpoints while handling
709*56bb7041Schristos 	 a step-over.  */
710*56bb7041Schristos       if (debug_threads)
711*56bb7041Schristos 	debug_printf ("Could not find fast tracepoint jump at 0x%s "
712*56bb7041Schristos 		      "in list (uninserting).\n",
713*56bb7041Schristos 		      paddress (pc));
714*56bb7041Schristos       return;
715*56bb7041Schristos     }
716*56bb7041Schristos 
717*56bb7041Schristos   if (jp->inserted)
718*56bb7041Schristos     {
719*56bb7041Schristos       unsigned char *buf;
720*56bb7041Schristos 
721*56bb7041Schristos       jp->inserted = 0;
722*56bb7041Schristos 
723*56bb7041Schristos       /* Since there can be trap breakpoints inserted in the same
724*56bb7041Schristos 	 address range, we use use `target_write_memory', which
725*56bb7041Schristos 	 takes care of layering breakpoints on top of fast
726*56bb7041Schristos 	 tracepoints, and on top of the buffer we pass it.  This works
727*56bb7041Schristos 	 because we've already marked the fast tracepoint fast
728*56bb7041Schristos 	 tracepoint jump uninserted above.  Also note that we need to
729*56bb7041Schristos 	 pass the current shadow contents, because
730*56bb7041Schristos 	 target_write_memory updates any shadow memory with what we
731*56bb7041Schristos 	 pass here, and we want that to be a nop.  */
732*56bb7041Schristos       buf = (unsigned char *) alloca (jp->length);
733*56bb7041Schristos       memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
734*56bb7041Schristos       err = target_write_memory (jp->pc, buf, jp->length);
735*56bb7041Schristos       if (err != 0)
736*56bb7041Schristos 	{
737*56bb7041Schristos 	  jp->inserted = 1;
738*56bb7041Schristos 
739*56bb7041Schristos 	  if (debug_threads)
740*56bb7041Schristos 	    debug_printf ("Failed to uninsert fast tracepoint jump at"
741*56bb7041Schristos 			  " 0x%s (%s).\n",
742*56bb7041Schristos 			  paddress (pc), safe_strerror (err));
743*56bb7041Schristos 	}
744*56bb7041Schristos     }
745*56bb7041Schristos }
746*56bb7041Schristos 
747*56bb7041Schristos void
reinsert_fast_tracepoint_jumps_at(CORE_ADDR where)748*56bb7041Schristos reinsert_fast_tracepoint_jumps_at (CORE_ADDR where)
749*56bb7041Schristos {
750*56bb7041Schristos   struct fast_tracepoint_jump *jp;
751*56bb7041Schristos   int err;
752*56bb7041Schristos   unsigned char *buf;
753*56bb7041Schristos 
754*56bb7041Schristos   jp = find_fast_tracepoint_jump_at (where);
755*56bb7041Schristos   if (jp == NULL)
756*56bb7041Schristos     {
757*56bb7041Schristos       /* This can happen when we remove breakpoints when a tracepoint
758*56bb7041Schristos 	 hit causes a tracing stop, while handling a step-over.  */
759*56bb7041Schristos       if (debug_threads)
760*56bb7041Schristos 	debug_printf ("Could not find fast tracepoint jump at 0x%s "
761*56bb7041Schristos 		      "in list (reinserting).\n",
762*56bb7041Schristos 		      paddress (where));
763*56bb7041Schristos       return;
764*56bb7041Schristos     }
765*56bb7041Schristos 
766*56bb7041Schristos   if (jp->inserted)
767*56bb7041Schristos     error ("Jump already inserted at reinsert time.");
768*56bb7041Schristos 
769*56bb7041Schristos   jp->inserted = 1;
770*56bb7041Schristos 
771*56bb7041Schristos   /* Since there can be trap breakpoints inserted in the same address
772*56bb7041Schristos      range, we use `target_write_memory', which takes care of
773*56bb7041Schristos      layering breakpoints on top of fast tracepoints, and on top of
774*56bb7041Schristos      the buffer we pass it.  This works because we've already marked
775*56bb7041Schristos      the fast tracepoint jump inserted above.  Also note that we need
776*56bb7041Schristos      to pass the current shadow contents, because
777*56bb7041Schristos      target_write_memory updates any shadow memory with what we pass
778*56bb7041Schristos      here, and we want that to be a nop.  */
779*56bb7041Schristos   buf = (unsigned char *) alloca (jp->length);
780*56bb7041Schristos   memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
781*56bb7041Schristos   err = target_write_memory (where, buf, jp->length);
782*56bb7041Schristos   if (err != 0)
783*56bb7041Schristos     {
784*56bb7041Schristos       jp->inserted = 0;
785*56bb7041Schristos 
786*56bb7041Schristos       if (debug_threads)
787*56bb7041Schristos 	debug_printf ("Failed to reinsert fast tracepoint jump at"
788*56bb7041Schristos 		      " 0x%s (%s).\n",
789*56bb7041Schristos 		      paddress (where), safe_strerror (err));
790*56bb7041Schristos     }
791*56bb7041Schristos }
792*56bb7041Schristos 
793*56bb7041Schristos /* Set a high-level breakpoint of type TYPE, with low level type
794*56bb7041Schristos    RAW_TYPE and kind KIND, at WHERE.  On success, a pointer to the new
795*56bb7041Schristos    breakpoint is returned.  On failure, returns NULL and writes the
796*56bb7041Schristos    error code to *ERR.  HANDLER is called when the breakpoint is hit.
797*56bb7041Schristos    HANDLER should return 1 if the breakpoint should be deleted, 0
798*56bb7041Schristos    otherwise.  */
799*56bb7041Schristos 
800*56bb7041Schristos static struct breakpoint *
set_breakpoint(enum bkpt_type type,enum raw_bkpt_type raw_type,CORE_ADDR where,int kind,int (* handler)(CORE_ADDR),int * err)801*56bb7041Schristos set_breakpoint (enum bkpt_type type, enum raw_bkpt_type raw_type,
802*56bb7041Schristos 		CORE_ADDR where, int kind,
803*56bb7041Schristos 		int (*handler) (CORE_ADDR), int *err)
804*56bb7041Schristos {
805*56bb7041Schristos   struct process_info *proc = current_process ();
806*56bb7041Schristos   struct breakpoint *bp;
807*56bb7041Schristos   struct raw_breakpoint *raw;
808*56bb7041Schristos 
809*56bb7041Schristos   raw = set_raw_breakpoint_at (raw_type, where, kind, err);
810*56bb7041Schristos 
811*56bb7041Schristos   if (raw == NULL)
812*56bb7041Schristos     {
813*56bb7041Schristos       /* warn? */
814*56bb7041Schristos       return NULL;
815*56bb7041Schristos     }
816*56bb7041Schristos 
817*56bb7041Schristos   if (is_gdb_breakpoint (type))
818*56bb7041Schristos     {
819*56bb7041Schristos       struct gdb_breakpoint *gdb_bp = XCNEW (struct gdb_breakpoint);
820*56bb7041Schristos 
821*56bb7041Schristos       bp = (struct breakpoint *) gdb_bp;
822*56bb7041Schristos       gdb_assert (handler == NULL);
823*56bb7041Schristos     }
824*56bb7041Schristos   else if (type == other_breakpoint)
825*56bb7041Schristos     {
826*56bb7041Schristos       struct other_breakpoint *other_bp = XCNEW (struct other_breakpoint);
827*56bb7041Schristos 
828*56bb7041Schristos       other_bp->handler = handler;
829*56bb7041Schristos       bp = (struct breakpoint *) other_bp;
830*56bb7041Schristos     }
831*56bb7041Schristos   else if (type == single_step_breakpoint)
832*56bb7041Schristos     {
833*56bb7041Schristos       struct single_step_breakpoint *ss_bp
834*56bb7041Schristos 	= XCNEW (struct single_step_breakpoint);
835*56bb7041Schristos 
836*56bb7041Schristos       bp = (struct breakpoint *) ss_bp;
837*56bb7041Schristos     }
838*56bb7041Schristos   else
839*56bb7041Schristos     gdb_assert_not_reached ("unhandled breakpoint type");
840*56bb7041Schristos 
841*56bb7041Schristos   bp->type = type;
842*56bb7041Schristos   bp->raw = raw;
843*56bb7041Schristos 
844*56bb7041Schristos   bp->next = proc->breakpoints;
845*56bb7041Schristos   proc->breakpoints = bp;
846*56bb7041Schristos 
847*56bb7041Schristos   return bp;
848*56bb7041Schristos }
849*56bb7041Schristos 
850*56bb7041Schristos /* Set breakpoint of TYPE on address WHERE with handler HANDLER.  */
851*56bb7041Schristos 
852*56bb7041Schristos static struct breakpoint *
set_breakpoint_type_at(enum bkpt_type type,CORE_ADDR where,int (* handler)(CORE_ADDR))853*56bb7041Schristos set_breakpoint_type_at (enum bkpt_type type, CORE_ADDR where,
854*56bb7041Schristos 			int (*handler) (CORE_ADDR))
855*56bb7041Schristos {
856*56bb7041Schristos   int err_ignored;
857*56bb7041Schristos   CORE_ADDR placed_address = where;
858*56bb7041Schristos   int breakpoint_kind = target_breakpoint_kind_from_pc (&placed_address);
859*56bb7041Schristos 
860*56bb7041Schristos   return set_breakpoint (type, raw_bkpt_type_sw,
861*56bb7041Schristos 			 placed_address, breakpoint_kind, handler,
862*56bb7041Schristos 			 &err_ignored);
863*56bb7041Schristos }
864*56bb7041Schristos 
865*56bb7041Schristos /* See mem-break.h  */
866*56bb7041Schristos 
867*56bb7041Schristos struct breakpoint *
set_breakpoint_at(CORE_ADDR where,int (* handler)(CORE_ADDR))868*56bb7041Schristos set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
869*56bb7041Schristos {
870*56bb7041Schristos   return set_breakpoint_type_at (other_breakpoint, where, handler);
871*56bb7041Schristos }
872*56bb7041Schristos 
873*56bb7041Schristos 
874*56bb7041Schristos static int
delete_raw_breakpoint(struct process_info * proc,struct raw_breakpoint * todel)875*56bb7041Schristos delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
876*56bb7041Schristos {
877*56bb7041Schristos   struct raw_breakpoint *bp, **bp_link;
878*56bb7041Schristos   int ret;
879*56bb7041Schristos 
880*56bb7041Schristos   bp = proc->raw_breakpoints;
881*56bb7041Schristos   bp_link = &proc->raw_breakpoints;
882*56bb7041Schristos 
883*56bb7041Schristos   while (bp)
884*56bb7041Schristos     {
885*56bb7041Schristos       if (bp == todel)
886*56bb7041Schristos 	{
887*56bb7041Schristos 	  if (bp->inserted > 0)
888*56bb7041Schristos 	    {
889*56bb7041Schristos 	      struct raw_breakpoint *prev_bp_link = *bp_link;
890*56bb7041Schristos 
891*56bb7041Schristos 	      *bp_link = bp->next;
892*56bb7041Schristos 
893*56bb7041Schristos 	      ret = the_target->remove_point (bp->raw_type, bp->pc,
894*56bb7041Schristos 					      bp->kind, bp);
895*56bb7041Schristos 	      if (ret != 0)
896*56bb7041Schristos 		{
897*56bb7041Schristos 		  /* Something went wrong, relink the breakpoint.  */
898*56bb7041Schristos 		  *bp_link = prev_bp_link;
899*56bb7041Schristos 
900*56bb7041Schristos 		  if (debug_threads)
901*56bb7041Schristos 		    debug_printf ("Failed to uninsert raw breakpoint "
902*56bb7041Schristos 				  "at 0x%s while deleting it.\n",
903*56bb7041Schristos 				  paddress (bp->pc));
904*56bb7041Schristos 		  return ret;
905*56bb7041Schristos 		}
906*56bb7041Schristos 	    }
907*56bb7041Schristos 	  else
908*56bb7041Schristos 	    *bp_link = bp->next;
909*56bb7041Schristos 
910*56bb7041Schristos 	  free (bp);
911*56bb7041Schristos 	  return 0;
912*56bb7041Schristos 	}
913*56bb7041Schristos       else
914*56bb7041Schristos 	{
915*56bb7041Schristos 	  bp_link = &bp->next;
916*56bb7041Schristos 	  bp = *bp_link;
917*56bb7041Schristos 	}
918*56bb7041Schristos     }
919*56bb7041Schristos 
920*56bb7041Schristos   warning ("Could not find raw breakpoint in list.");
921*56bb7041Schristos   return ENOENT;
922*56bb7041Schristos }
923*56bb7041Schristos 
924*56bb7041Schristos static int
release_breakpoint(struct process_info * proc,struct breakpoint * bp)925*56bb7041Schristos release_breakpoint (struct process_info *proc, struct breakpoint *bp)
926*56bb7041Schristos {
927*56bb7041Schristos   int newrefcount;
928*56bb7041Schristos   int ret;
929*56bb7041Schristos 
930*56bb7041Schristos   newrefcount = bp->raw->refcount - 1;
931*56bb7041Schristos   if (newrefcount == 0)
932*56bb7041Schristos     {
933*56bb7041Schristos       ret = delete_raw_breakpoint (proc, bp->raw);
934*56bb7041Schristos       if (ret != 0)
935*56bb7041Schristos 	return ret;
936*56bb7041Schristos     }
937*56bb7041Schristos   else
938*56bb7041Schristos     bp->raw->refcount = newrefcount;
939*56bb7041Schristos 
940*56bb7041Schristos   free (bp);
941*56bb7041Schristos 
942*56bb7041Schristos   return 0;
943*56bb7041Schristos }
944*56bb7041Schristos 
945*56bb7041Schristos static int
delete_breakpoint_1(struct process_info * proc,struct breakpoint * todel)946*56bb7041Schristos delete_breakpoint_1 (struct process_info *proc, struct breakpoint *todel)
947*56bb7041Schristos {
948*56bb7041Schristos   struct breakpoint *bp, **bp_link;
949*56bb7041Schristos   int err;
950*56bb7041Schristos 
951*56bb7041Schristos   bp = proc->breakpoints;
952*56bb7041Schristos   bp_link = &proc->breakpoints;
953*56bb7041Schristos 
954*56bb7041Schristos   while (bp)
955*56bb7041Schristos     {
956*56bb7041Schristos       if (bp == todel)
957*56bb7041Schristos 	{
958*56bb7041Schristos 	  *bp_link = bp->next;
959*56bb7041Schristos 
960*56bb7041Schristos 	  err = release_breakpoint (proc, bp);
961*56bb7041Schristos 	  if (err != 0)
962*56bb7041Schristos 	    return err;
963*56bb7041Schristos 
964*56bb7041Schristos 	  bp = *bp_link;
965*56bb7041Schristos 	  return 0;
966*56bb7041Schristos 	}
967*56bb7041Schristos       else
968*56bb7041Schristos 	{
969*56bb7041Schristos 	  bp_link = &bp->next;
970*56bb7041Schristos 	  bp = *bp_link;
971*56bb7041Schristos 	}
972*56bb7041Schristos     }
973*56bb7041Schristos 
974*56bb7041Schristos   warning ("Could not find breakpoint in list.");
975*56bb7041Schristos   return ENOENT;
976*56bb7041Schristos }
977*56bb7041Schristos 
978*56bb7041Schristos int
delete_breakpoint(struct breakpoint * todel)979*56bb7041Schristos delete_breakpoint (struct breakpoint *todel)
980*56bb7041Schristos {
981*56bb7041Schristos   struct process_info *proc = current_process ();
982*56bb7041Schristos   return delete_breakpoint_1 (proc, todel);
983*56bb7041Schristos }
984*56bb7041Schristos 
985*56bb7041Schristos /* Locate a GDB breakpoint of type Z_TYPE and kind KIND placed at
986*56bb7041Schristos    address ADDR and return a pointer to its structure.  If KIND is -1,
987*56bb7041Schristos    the breakpoint's kind is ignored.  */
988*56bb7041Schristos 
989*56bb7041Schristos static struct gdb_breakpoint *
find_gdb_breakpoint(char z_type,CORE_ADDR addr,int kind)990*56bb7041Schristos find_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind)
991*56bb7041Schristos {
992*56bb7041Schristos   struct process_info *proc = current_process ();
993*56bb7041Schristos   struct breakpoint *bp;
994*56bb7041Schristos   enum bkpt_type type = Z_packet_to_bkpt_type (z_type);
995*56bb7041Schristos 
996*56bb7041Schristos   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
997*56bb7041Schristos     if (bp->type == type && bp->raw->pc == addr
998*56bb7041Schristos 	&& (kind == -1 || bp->raw->kind == kind))
999*56bb7041Schristos       return (struct gdb_breakpoint *) bp;
1000*56bb7041Schristos 
1001*56bb7041Schristos   return NULL;
1002*56bb7041Schristos }
1003*56bb7041Schristos 
1004*56bb7041Schristos static int
z_type_supported(char z_type)1005*56bb7041Schristos z_type_supported (char z_type)
1006*56bb7041Schristos {
1007*56bb7041Schristos   return (z_type >= '0' && z_type <= '4'
1008*56bb7041Schristos 	  && the_target->supports_z_point_type (z_type));
1009*56bb7041Schristos }
1010*56bb7041Schristos 
1011*56bb7041Schristos /* Create a new GDB breakpoint of type Z_TYPE at ADDR with kind KIND.
1012*56bb7041Schristos    Returns a pointer to the newly created breakpoint on success.  On
1013*56bb7041Schristos    failure returns NULL and sets *ERR to either -1 for error, or 1 if
1014*56bb7041Schristos    Z_TYPE breakpoints are not supported on this target.  */
1015*56bb7041Schristos 
1016*56bb7041Schristos static struct gdb_breakpoint *
set_gdb_breakpoint_1(char z_type,CORE_ADDR addr,int kind,int * err)1017*56bb7041Schristos set_gdb_breakpoint_1 (char z_type, CORE_ADDR addr, int kind, int *err)
1018*56bb7041Schristos {
1019*56bb7041Schristos   struct gdb_breakpoint *bp;
1020*56bb7041Schristos   enum bkpt_type type;
1021*56bb7041Schristos   enum raw_bkpt_type raw_type;
1022*56bb7041Schristos 
1023*56bb7041Schristos   /* If we see GDB inserting a second code breakpoint at the same
1024*56bb7041Schristos      address, then either: GDB is updating the breakpoint's conditions
1025*56bb7041Schristos      or commands; or, the first breakpoint must have disappeared due
1026*56bb7041Schristos      to a shared library unload.  On targets where the shared
1027*56bb7041Schristos      libraries are handled by userspace, like SVR4, for example,
1028*56bb7041Schristos      GDBserver can't tell if a library was loaded or unloaded.  Since
1029*56bb7041Schristos      we refcount raw breakpoints, we must be careful to make sure GDB
1030*56bb7041Schristos      breakpoints never contribute more than one reference.  if we
1031*56bb7041Schristos      didn't do this, in case the previous breakpoint is gone due to a
1032*56bb7041Schristos      shared library unload, we'd just increase the refcount of the
1033*56bb7041Schristos      previous breakpoint at this address, but the trap was not planted
1034*56bb7041Schristos      in the inferior anymore, thus the breakpoint would never be hit.
1035*56bb7041Schristos      Note this must be careful to not create a window where
1036*56bb7041Schristos      breakpoints are removed from the target, for non-stop, in case
1037*56bb7041Schristos      the target can poke at memory while the program is running.  */
1038*56bb7041Schristos   if (z_type == Z_PACKET_SW_BP
1039*56bb7041Schristos       || z_type == Z_PACKET_HW_BP)
1040*56bb7041Schristos     {
1041*56bb7041Schristos       bp = find_gdb_breakpoint (z_type, addr, -1);
1042*56bb7041Schristos 
1043*56bb7041Schristos       if (bp != NULL)
1044*56bb7041Schristos 	{
1045*56bb7041Schristos 	  if (bp->base.raw->kind != kind)
1046*56bb7041Schristos 	    {
1047*56bb7041Schristos 	      /* A different kind than previously seen.  The previous
1048*56bb7041Schristos 		 breakpoint must be gone then.  */
1049*56bb7041Schristos 	      bp->base.raw->inserted = -1;
1050*56bb7041Schristos 	      delete_breakpoint ((struct breakpoint *) bp);
1051*56bb7041Schristos 	      bp = NULL;
1052*56bb7041Schristos 	    }
1053*56bb7041Schristos 	  else if (z_type == Z_PACKET_SW_BP)
1054*56bb7041Schristos 	    {
1055*56bb7041Schristos 	      /* Check if the breakpoint is actually gone from the
1056*56bb7041Schristos 		 target, due to an solib unload, for example.  Might
1057*56bb7041Schristos 		 as well validate _all_ breakpoints.  */
1058*56bb7041Schristos 	      validate_breakpoints ();
1059*56bb7041Schristos 
1060*56bb7041Schristos 	      /* Breakpoints that don't pass validation are
1061*56bb7041Schristos 		 deleted.  */
1062*56bb7041Schristos 	      bp = find_gdb_breakpoint (z_type, addr, -1);
1063*56bb7041Schristos 	    }
1064*56bb7041Schristos 	}
1065*56bb7041Schristos     }
1066*56bb7041Schristos   else
1067*56bb7041Schristos     {
1068*56bb7041Schristos       /* Data breakpoints for the same address but different kind are
1069*56bb7041Schristos 	 expected.  GDB doesn't merge these.  The backend gets to do
1070*56bb7041Schristos 	 that if it wants/can.  */
1071*56bb7041Schristos       bp = find_gdb_breakpoint (z_type, addr, kind);
1072*56bb7041Schristos     }
1073*56bb7041Schristos 
1074*56bb7041Schristos   if (bp != NULL)
1075*56bb7041Schristos     {
1076*56bb7041Schristos       /* We already know about this breakpoint, there's nothing else
1077*56bb7041Schristos 	 to do - GDB's reference is already accounted for.  Note that
1078*56bb7041Schristos 	 whether the breakpoint inserted is left as is - we may be
1079*56bb7041Schristos 	 stepping over it, for example, in which case we don't want to
1080*56bb7041Schristos 	 force-reinsert it.  */
1081*56bb7041Schristos       return bp;
1082*56bb7041Schristos     }
1083*56bb7041Schristos 
1084*56bb7041Schristos   raw_type = Z_packet_to_raw_bkpt_type (z_type);
1085*56bb7041Schristos   type = Z_packet_to_bkpt_type (z_type);
1086*56bb7041Schristos   return (struct gdb_breakpoint *) set_breakpoint (type, raw_type, addr,
1087*56bb7041Schristos 						   kind, NULL, err);
1088*56bb7041Schristos }
1089*56bb7041Schristos 
1090*56bb7041Schristos static int
check_gdb_bp_preconditions(char z_type,int * err)1091*56bb7041Schristos check_gdb_bp_preconditions (char z_type, int *err)
1092*56bb7041Schristos {
1093*56bb7041Schristos   /* As software/memory breakpoints work by poking at memory, we need
1094*56bb7041Schristos      to prepare to access memory.  If that operation fails, we need to
1095*56bb7041Schristos      return error.  Seeing an error, if this is the first breakpoint
1096*56bb7041Schristos      of that type that GDB tries to insert, GDB would then assume the
1097*56bb7041Schristos      breakpoint type is supported, but it may actually not be.  So we
1098*56bb7041Schristos      need to check whether the type is supported at all before
1099*56bb7041Schristos      preparing to access memory.  */
1100*56bb7041Schristos   if (!z_type_supported (z_type))
1101*56bb7041Schristos     {
1102*56bb7041Schristos       *err = 1;
1103*56bb7041Schristos       return 0;
1104*56bb7041Schristos     }
1105*56bb7041Schristos 
1106*56bb7041Schristos   return 1;
1107*56bb7041Schristos }
1108*56bb7041Schristos 
1109*56bb7041Schristos /* See mem-break.h.  This is a wrapper for set_gdb_breakpoint_1 that
1110*56bb7041Schristos    knows to prepare to access memory for Z0 breakpoints.  */
1111*56bb7041Schristos 
1112*56bb7041Schristos struct gdb_breakpoint *
set_gdb_breakpoint(char z_type,CORE_ADDR addr,int kind,int * err)1113*56bb7041Schristos set_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind, int *err)
1114*56bb7041Schristos {
1115*56bb7041Schristos   struct gdb_breakpoint *bp;
1116*56bb7041Schristos 
1117*56bb7041Schristos   if (!check_gdb_bp_preconditions (z_type, err))
1118*56bb7041Schristos     return NULL;
1119*56bb7041Schristos 
1120*56bb7041Schristos   /* If inserting a software/memory breakpoint, need to prepare to
1121*56bb7041Schristos      access memory.  */
1122*56bb7041Schristos   if (z_type == Z_PACKET_SW_BP)
1123*56bb7041Schristos     {
1124*56bb7041Schristos       if (prepare_to_access_memory () != 0)
1125*56bb7041Schristos 	{
1126*56bb7041Schristos 	  *err = -1;
1127*56bb7041Schristos 	  return NULL;
1128*56bb7041Schristos 	}
1129*56bb7041Schristos     }
1130*56bb7041Schristos 
1131*56bb7041Schristos   bp = set_gdb_breakpoint_1 (z_type, addr, kind, err);
1132*56bb7041Schristos 
1133*56bb7041Schristos   if (z_type == Z_PACKET_SW_BP)
1134*56bb7041Schristos     done_accessing_memory ();
1135*56bb7041Schristos 
1136*56bb7041Schristos   return bp;
1137*56bb7041Schristos }
1138*56bb7041Schristos 
1139*56bb7041Schristos /* Delete a GDB breakpoint of type Z_TYPE and kind KIND previously
1140*56bb7041Schristos    inserted at ADDR with set_gdb_breakpoint_at.  Returns 0 on success,
1141*56bb7041Schristos    -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
1142*56bb7041Schristos    target.  */
1143*56bb7041Schristos 
1144*56bb7041Schristos static int
delete_gdb_breakpoint_1(char z_type,CORE_ADDR addr,int kind)1145*56bb7041Schristos delete_gdb_breakpoint_1 (char z_type, CORE_ADDR addr, int kind)
1146*56bb7041Schristos {
1147*56bb7041Schristos   struct gdb_breakpoint *bp;
1148*56bb7041Schristos   int err;
1149*56bb7041Schristos 
1150*56bb7041Schristos   bp = find_gdb_breakpoint (z_type, addr, kind);
1151*56bb7041Schristos   if (bp == NULL)
1152*56bb7041Schristos     return -1;
1153*56bb7041Schristos 
1154*56bb7041Schristos   /* Before deleting the breakpoint, make sure to free its condition
1155*56bb7041Schristos      and command lists.  */
1156*56bb7041Schristos   clear_breakpoint_conditions_and_commands (bp);
1157*56bb7041Schristos   err = delete_breakpoint ((struct breakpoint *) bp);
1158*56bb7041Schristos   if (err != 0)
1159*56bb7041Schristos     return -1;
1160*56bb7041Schristos 
1161*56bb7041Schristos   return 0;
1162*56bb7041Schristos }
1163*56bb7041Schristos 
1164*56bb7041Schristos /* See mem-break.h.  This is a wrapper for delete_gdb_breakpoint that
1165*56bb7041Schristos    knows to prepare to access memory for Z0 breakpoints.  */
1166*56bb7041Schristos 
1167*56bb7041Schristos int
delete_gdb_breakpoint(char z_type,CORE_ADDR addr,int kind)1168*56bb7041Schristos delete_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind)
1169*56bb7041Schristos {
1170*56bb7041Schristos   int ret;
1171*56bb7041Schristos 
1172*56bb7041Schristos   if (!check_gdb_bp_preconditions (z_type, &ret))
1173*56bb7041Schristos     return ret;
1174*56bb7041Schristos 
1175*56bb7041Schristos   /* If inserting a software/memory breakpoint, need to prepare to
1176*56bb7041Schristos      access memory.  */
1177*56bb7041Schristos   if (z_type == Z_PACKET_SW_BP)
1178*56bb7041Schristos     {
1179*56bb7041Schristos       int err;
1180*56bb7041Schristos 
1181*56bb7041Schristos       err = prepare_to_access_memory ();
1182*56bb7041Schristos       if (err != 0)
1183*56bb7041Schristos 	return -1;
1184*56bb7041Schristos     }
1185*56bb7041Schristos 
1186*56bb7041Schristos   ret = delete_gdb_breakpoint_1 (z_type, addr, kind);
1187*56bb7041Schristos 
1188*56bb7041Schristos   if (z_type == Z_PACKET_SW_BP)
1189*56bb7041Schristos     done_accessing_memory ();
1190*56bb7041Schristos 
1191*56bb7041Schristos   return ret;
1192*56bb7041Schristos }
1193*56bb7041Schristos 
1194*56bb7041Schristos /* Clear all conditions associated with a breakpoint.  */
1195*56bb7041Schristos 
1196*56bb7041Schristos static void
clear_breakpoint_conditions(struct gdb_breakpoint * bp)1197*56bb7041Schristos clear_breakpoint_conditions (struct gdb_breakpoint *bp)
1198*56bb7041Schristos {
1199*56bb7041Schristos   struct point_cond_list *cond;
1200*56bb7041Schristos 
1201*56bb7041Schristos   if (bp->cond_list == NULL)
1202*56bb7041Schristos     return;
1203*56bb7041Schristos 
1204*56bb7041Schristos   cond = bp->cond_list;
1205*56bb7041Schristos 
1206*56bb7041Schristos   while (cond != NULL)
1207*56bb7041Schristos     {
1208*56bb7041Schristos       struct point_cond_list *cond_next;
1209*56bb7041Schristos 
1210*56bb7041Schristos       cond_next = cond->next;
1211*56bb7041Schristos       gdb_free_agent_expr (cond->cond);
1212*56bb7041Schristos       free (cond);
1213*56bb7041Schristos       cond = cond_next;
1214*56bb7041Schristos     }
1215*56bb7041Schristos 
1216*56bb7041Schristos   bp->cond_list = NULL;
1217*56bb7041Schristos }
1218*56bb7041Schristos 
1219*56bb7041Schristos /* Clear all commands associated with a breakpoint.  */
1220*56bb7041Schristos 
1221*56bb7041Schristos static void
clear_breakpoint_commands(struct gdb_breakpoint * bp)1222*56bb7041Schristos clear_breakpoint_commands (struct gdb_breakpoint *bp)
1223*56bb7041Schristos {
1224*56bb7041Schristos   struct point_command_list *cmd;
1225*56bb7041Schristos 
1226*56bb7041Schristos   if (bp->command_list == NULL)
1227*56bb7041Schristos     return;
1228*56bb7041Schristos 
1229*56bb7041Schristos   cmd = bp->command_list;
1230*56bb7041Schristos 
1231*56bb7041Schristos   while (cmd != NULL)
1232*56bb7041Schristos     {
1233*56bb7041Schristos       struct point_command_list *cmd_next;
1234*56bb7041Schristos 
1235*56bb7041Schristos       cmd_next = cmd->next;
1236*56bb7041Schristos       gdb_free_agent_expr (cmd->cmd);
1237*56bb7041Schristos       free (cmd);
1238*56bb7041Schristos       cmd = cmd_next;
1239*56bb7041Schristos     }
1240*56bb7041Schristos 
1241*56bb7041Schristos   bp->command_list = NULL;
1242*56bb7041Schristos }
1243*56bb7041Schristos 
1244*56bb7041Schristos void
clear_breakpoint_conditions_and_commands(struct gdb_breakpoint * bp)1245*56bb7041Schristos clear_breakpoint_conditions_and_commands (struct gdb_breakpoint *bp)
1246*56bb7041Schristos {
1247*56bb7041Schristos   clear_breakpoint_conditions (bp);
1248*56bb7041Schristos   clear_breakpoint_commands (bp);
1249*56bb7041Schristos }
1250*56bb7041Schristos 
1251*56bb7041Schristos /* Add condition CONDITION to GDBserver's breakpoint BP.  */
1252*56bb7041Schristos 
1253*56bb7041Schristos static void
add_condition_to_breakpoint(struct gdb_breakpoint * bp,struct agent_expr * condition)1254*56bb7041Schristos add_condition_to_breakpoint (struct gdb_breakpoint *bp,
1255*56bb7041Schristos 			     struct agent_expr *condition)
1256*56bb7041Schristos {
1257*56bb7041Schristos   struct point_cond_list *new_cond;
1258*56bb7041Schristos 
1259*56bb7041Schristos   /* Create new condition.  */
1260*56bb7041Schristos   new_cond = XCNEW (struct point_cond_list);
1261*56bb7041Schristos   new_cond->cond = condition;
1262*56bb7041Schristos 
1263*56bb7041Schristos   /* Add condition to the list.  */
1264*56bb7041Schristos   new_cond->next = bp->cond_list;
1265*56bb7041Schristos   bp->cond_list = new_cond;
1266*56bb7041Schristos }
1267*56bb7041Schristos 
1268*56bb7041Schristos /* Add a target-side condition CONDITION to a breakpoint.  */
1269*56bb7041Schristos 
1270*56bb7041Schristos int
add_breakpoint_condition(struct gdb_breakpoint * bp,const char ** condition)1271*56bb7041Schristos add_breakpoint_condition (struct gdb_breakpoint *bp, const char **condition)
1272*56bb7041Schristos {
1273*56bb7041Schristos   const char *actparm = *condition;
1274*56bb7041Schristos   struct agent_expr *cond;
1275*56bb7041Schristos 
1276*56bb7041Schristos   if (condition == NULL)
1277*56bb7041Schristos     return 1;
1278*56bb7041Schristos 
1279*56bb7041Schristos   if (bp == NULL)
1280*56bb7041Schristos     return 0;
1281*56bb7041Schristos 
1282*56bb7041Schristos   cond = gdb_parse_agent_expr (&actparm);
1283*56bb7041Schristos 
1284*56bb7041Schristos   if (cond == NULL)
1285*56bb7041Schristos     {
1286*56bb7041Schristos       warning ("Condition evaluation failed. Assuming unconditional.");
1287*56bb7041Schristos       return 0;
1288*56bb7041Schristos     }
1289*56bb7041Schristos 
1290*56bb7041Schristos   add_condition_to_breakpoint (bp, cond);
1291*56bb7041Schristos 
1292*56bb7041Schristos   *condition = actparm;
1293*56bb7041Schristos 
1294*56bb7041Schristos   return 1;
1295*56bb7041Schristos }
1296*56bb7041Schristos 
1297*56bb7041Schristos /* Evaluate condition (if any) at breakpoint BP.  Return 1 if
1298*56bb7041Schristos    true and 0 otherwise.  */
1299*56bb7041Schristos 
1300*56bb7041Schristos static int
gdb_condition_true_at_breakpoint_z_type(char z_type,CORE_ADDR addr)1301*56bb7041Schristos gdb_condition_true_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
1302*56bb7041Schristos {
1303*56bb7041Schristos   /* Fetch registers for the current inferior.  */
1304*56bb7041Schristos   struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1305*56bb7041Schristos   ULONGEST value = 0;
1306*56bb7041Schristos   struct point_cond_list *cl;
1307*56bb7041Schristos   int err = 0;
1308*56bb7041Schristos   struct eval_agent_expr_context ctx;
1309*56bb7041Schristos 
1310*56bb7041Schristos   if (bp == NULL)
1311*56bb7041Schristos     return 0;
1312*56bb7041Schristos 
1313*56bb7041Schristos   /* Check if the breakpoint is unconditional.  If it is,
1314*56bb7041Schristos      the condition always evaluates to TRUE.  */
1315*56bb7041Schristos   if (bp->cond_list == NULL)
1316*56bb7041Schristos     return 1;
1317*56bb7041Schristos 
1318*56bb7041Schristos   ctx.regcache = get_thread_regcache (current_thread, 1);
1319*56bb7041Schristos   ctx.tframe = NULL;
1320*56bb7041Schristos   ctx.tpoint = NULL;
1321*56bb7041Schristos 
1322*56bb7041Schristos   /* Evaluate each condition in the breakpoint's list of conditions.
1323*56bb7041Schristos      Return true if any of the conditions evaluates to TRUE.
1324*56bb7041Schristos 
1325*56bb7041Schristos      If we failed to evaluate the expression, TRUE is returned.  This
1326*56bb7041Schristos      forces GDB to reevaluate the conditions.  */
1327*56bb7041Schristos   for (cl = bp->cond_list;
1328*56bb7041Schristos        cl && !value && !err; cl = cl->next)
1329*56bb7041Schristos     {
1330*56bb7041Schristos       /* Evaluate the condition.  */
1331*56bb7041Schristos       err = gdb_eval_agent_expr (&ctx, cl->cond, &value);
1332*56bb7041Schristos     }
1333*56bb7041Schristos 
1334*56bb7041Schristos   if (err)
1335*56bb7041Schristos     return 1;
1336*56bb7041Schristos 
1337*56bb7041Schristos   return (value != 0);
1338*56bb7041Schristos }
1339*56bb7041Schristos 
1340*56bb7041Schristos int
gdb_condition_true_at_breakpoint(CORE_ADDR where)1341*56bb7041Schristos gdb_condition_true_at_breakpoint (CORE_ADDR where)
1342*56bb7041Schristos {
1343*56bb7041Schristos   /* Only check code (software or hardware) breakpoints.  */
1344*56bb7041Schristos   return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
1345*56bb7041Schristos 	  || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
1346*56bb7041Schristos }
1347*56bb7041Schristos 
1348*56bb7041Schristos /* Add commands COMMANDS to GDBserver's breakpoint BP.  */
1349*56bb7041Schristos 
1350*56bb7041Schristos static void
add_commands_to_breakpoint(struct gdb_breakpoint * bp,struct agent_expr * commands,int persist)1351*56bb7041Schristos add_commands_to_breakpoint (struct gdb_breakpoint *bp,
1352*56bb7041Schristos 			    struct agent_expr *commands, int persist)
1353*56bb7041Schristos {
1354*56bb7041Schristos   struct point_command_list *new_cmd;
1355*56bb7041Schristos 
1356*56bb7041Schristos   /* Create new command.  */
1357*56bb7041Schristos   new_cmd = XCNEW (struct point_command_list);
1358*56bb7041Schristos   new_cmd->cmd = commands;
1359*56bb7041Schristos   new_cmd->persistence = persist;
1360*56bb7041Schristos 
1361*56bb7041Schristos   /* Add commands to the list.  */
1362*56bb7041Schristos   new_cmd->next = bp->command_list;
1363*56bb7041Schristos   bp->command_list = new_cmd;
1364*56bb7041Schristos }
1365*56bb7041Schristos 
1366*56bb7041Schristos /* Add a target-side command COMMAND to the breakpoint at ADDR.  */
1367*56bb7041Schristos 
1368*56bb7041Schristos int
add_breakpoint_commands(struct gdb_breakpoint * bp,const char ** command,int persist)1369*56bb7041Schristos add_breakpoint_commands (struct gdb_breakpoint *bp, const char **command,
1370*56bb7041Schristos 			 int persist)
1371*56bb7041Schristos {
1372*56bb7041Schristos   const char *actparm = *command;
1373*56bb7041Schristos   struct agent_expr *cmd;
1374*56bb7041Schristos 
1375*56bb7041Schristos   if (command == NULL)
1376*56bb7041Schristos     return 1;
1377*56bb7041Schristos 
1378*56bb7041Schristos   if (bp == NULL)
1379*56bb7041Schristos     return 0;
1380*56bb7041Schristos 
1381*56bb7041Schristos   cmd = gdb_parse_agent_expr (&actparm);
1382*56bb7041Schristos 
1383*56bb7041Schristos   if (cmd == NULL)
1384*56bb7041Schristos     {
1385*56bb7041Schristos       warning ("Command evaluation failed. Disabling.");
1386*56bb7041Schristos       return 0;
1387*56bb7041Schristos     }
1388*56bb7041Schristos 
1389*56bb7041Schristos   add_commands_to_breakpoint (bp, cmd, persist);
1390*56bb7041Schristos 
1391*56bb7041Schristos   *command = actparm;
1392*56bb7041Schristos 
1393*56bb7041Schristos   return 1;
1394*56bb7041Schristos }
1395*56bb7041Schristos 
1396*56bb7041Schristos /* Return true if there are no commands to run at this location,
1397*56bb7041Schristos    which likely means we want to report back to GDB.  */
1398*56bb7041Schristos 
1399*56bb7041Schristos static int
gdb_no_commands_at_breakpoint_z_type(char z_type,CORE_ADDR addr)1400*56bb7041Schristos gdb_no_commands_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
1401*56bb7041Schristos {
1402*56bb7041Schristos   struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1403*56bb7041Schristos 
1404*56bb7041Schristos   if (bp == NULL)
1405*56bb7041Schristos     return 1;
1406*56bb7041Schristos 
1407*56bb7041Schristos   if (debug_threads)
1408*56bb7041Schristos     debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s\n",
1409*56bb7041Schristos 		  paddress (addr), z_type,
1410*56bb7041Schristos 		  phex_nz ((uintptr_t) bp->command_list, 0));
1411*56bb7041Schristos   return (bp->command_list == NULL);
1412*56bb7041Schristos }
1413*56bb7041Schristos 
1414*56bb7041Schristos /* Return true if there are no commands to run at this location,
1415*56bb7041Schristos    which likely means we want to report back to GDB.  */
1416*56bb7041Schristos 
1417*56bb7041Schristos int
gdb_no_commands_at_breakpoint(CORE_ADDR where)1418*56bb7041Schristos gdb_no_commands_at_breakpoint (CORE_ADDR where)
1419*56bb7041Schristos {
1420*56bb7041Schristos   /* Only check code (software or hardware) breakpoints.  */
1421*56bb7041Schristos   return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
1422*56bb7041Schristos 	  && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
1423*56bb7041Schristos }
1424*56bb7041Schristos 
1425*56bb7041Schristos /* Run a breakpoint's commands.  Returns 0 if there was a problem
1426*56bb7041Schristos    running any command, 1 otherwise.  */
1427*56bb7041Schristos 
1428*56bb7041Schristos static int
run_breakpoint_commands_z_type(char z_type,CORE_ADDR addr)1429*56bb7041Schristos run_breakpoint_commands_z_type (char z_type, CORE_ADDR addr)
1430*56bb7041Schristos {
1431*56bb7041Schristos   /* Fetch registers for the current inferior.  */
1432*56bb7041Schristos   struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1433*56bb7041Schristos   ULONGEST value = 0;
1434*56bb7041Schristos   struct point_command_list *cl;
1435*56bb7041Schristos   int err = 0;
1436*56bb7041Schristos   struct eval_agent_expr_context ctx;
1437*56bb7041Schristos 
1438*56bb7041Schristos   if (bp == NULL)
1439*56bb7041Schristos     return 1;
1440*56bb7041Schristos 
1441*56bb7041Schristos   ctx.regcache = get_thread_regcache (current_thread, 1);
1442*56bb7041Schristos   ctx.tframe = NULL;
1443*56bb7041Schristos   ctx.tpoint = NULL;
1444*56bb7041Schristos 
1445*56bb7041Schristos   for (cl = bp->command_list;
1446*56bb7041Schristos        cl && !value && !err; cl = cl->next)
1447*56bb7041Schristos     {
1448*56bb7041Schristos       /* Run the command.  */
1449*56bb7041Schristos       err = gdb_eval_agent_expr (&ctx, cl->cmd, &value);
1450*56bb7041Schristos 
1451*56bb7041Schristos       /* If one command has a problem, stop digging the hole deeper.  */
1452*56bb7041Schristos       if (err)
1453*56bb7041Schristos 	return 0;
1454*56bb7041Schristos     }
1455*56bb7041Schristos 
1456*56bb7041Schristos   return 1;
1457*56bb7041Schristos }
1458*56bb7041Schristos 
1459*56bb7041Schristos void
run_breakpoint_commands(CORE_ADDR where)1460*56bb7041Schristos run_breakpoint_commands (CORE_ADDR where)
1461*56bb7041Schristos {
1462*56bb7041Schristos   /* Only check code (software or hardware) breakpoints.  If one
1463*56bb7041Schristos      command has a problem, stop digging the hole deeper.  */
1464*56bb7041Schristos   if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP, where))
1465*56bb7041Schristos     run_breakpoint_commands_z_type (Z_PACKET_HW_BP, where);
1466*56bb7041Schristos }
1467*56bb7041Schristos 
1468*56bb7041Schristos /* See mem-break.h.  */
1469*56bb7041Schristos 
1470*56bb7041Schristos int
gdb_breakpoint_here(CORE_ADDR where)1471*56bb7041Schristos gdb_breakpoint_here (CORE_ADDR where)
1472*56bb7041Schristos {
1473*56bb7041Schristos   /* Only check code (software or hardware) breakpoints.  */
1474*56bb7041Schristos   return (find_gdb_breakpoint (Z_PACKET_SW_BP, where, -1) != NULL
1475*56bb7041Schristos 	  || find_gdb_breakpoint (Z_PACKET_HW_BP, where, -1) != NULL);
1476*56bb7041Schristos }
1477*56bb7041Schristos 
1478*56bb7041Schristos void
set_single_step_breakpoint(CORE_ADDR stop_at,ptid_t ptid)1479*56bb7041Schristos set_single_step_breakpoint (CORE_ADDR stop_at, ptid_t ptid)
1480*56bb7041Schristos {
1481*56bb7041Schristos   struct single_step_breakpoint *bp;
1482*56bb7041Schristos 
1483*56bb7041Schristos   gdb_assert (current_ptid.pid () == ptid.pid ());
1484*56bb7041Schristos 
1485*56bb7041Schristos   bp = (struct single_step_breakpoint *) set_breakpoint_type_at (single_step_breakpoint,
1486*56bb7041Schristos 								stop_at, NULL);
1487*56bb7041Schristos   bp->ptid = ptid;
1488*56bb7041Schristos }
1489*56bb7041Schristos 
1490*56bb7041Schristos void
delete_single_step_breakpoints(struct thread_info * thread)1491*56bb7041Schristos delete_single_step_breakpoints (struct thread_info *thread)
1492*56bb7041Schristos {
1493*56bb7041Schristos   struct process_info *proc = get_thread_process (thread);
1494*56bb7041Schristos   struct breakpoint *bp, **bp_link;
1495*56bb7041Schristos 
1496*56bb7041Schristos   bp = proc->breakpoints;
1497*56bb7041Schristos   bp_link = &proc->breakpoints;
1498*56bb7041Schristos 
1499*56bb7041Schristos   while (bp)
1500*56bb7041Schristos     {
1501*56bb7041Schristos       if (bp->type == single_step_breakpoint
1502*56bb7041Schristos 	  && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1503*56bb7041Schristos 	{
1504*56bb7041Schristos 	  struct thread_info *saved_thread = current_thread;
1505*56bb7041Schristos 
1506*56bb7041Schristos 	  current_thread = thread;
1507*56bb7041Schristos 	  *bp_link = bp->next;
1508*56bb7041Schristos 	  release_breakpoint (proc, bp);
1509*56bb7041Schristos 	  bp = *bp_link;
1510*56bb7041Schristos 	  current_thread = saved_thread;
1511*56bb7041Schristos 	}
1512*56bb7041Schristos       else
1513*56bb7041Schristos 	{
1514*56bb7041Schristos 	  bp_link = &bp->next;
1515*56bb7041Schristos 	  bp = *bp_link;
1516*56bb7041Schristos 	}
1517*56bb7041Schristos     }
1518*56bb7041Schristos }
1519*56bb7041Schristos 
1520*56bb7041Schristos static void
uninsert_raw_breakpoint(struct raw_breakpoint * bp)1521*56bb7041Schristos uninsert_raw_breakpoint (struct raw_breakpoint *bp)
1522*56bb7041Schristos {
1523*56bb7041Schristos   if (bp->inserted < 0)
1524*56bb7041Schristos     {
1525*56bb7041Schristos       if (debug_threads)
1526*56bb7041Schristos 	debug_printf ("Breakpoint at %s is marked insert-disabled.\n",
1527*56bb7041Schristos 		      paddress (bp->pc));
1528*56bb7041Schristos     }
1529*56bb7041Schristos   else if (bp->inserted > 0)
1530*56bb7041Schristos     {
1531*56bb7041Schristos       int err;
1532*56bb7041Schristos 
1533*56bb7041Schristos       bp->inserted = 0;
1534*56bb7041Schristos 
1535*56bb7041Schristos       err = the_target->remove_point (bp->raw_type, bp->pc, bp->kind, bp);
1536*56bb7041Schristos       if (err != 0)
1537*56bb7041Schristos 	{
1538*56bb7041Schristos 	  bp->inserted = 1;
1539*56bb7041Schristos 
1540*56bb7041Schristos 	  if (debug_threads)
1541*56bb7041Schristos 	    debug_printf ("Failed to uninsert raw breakpoint at 0x%s.\n",
1542*56bb7041Schristos 			  paddress (bp->pc));
1543*56bb7041Schristos 	}
1544*56bb7041Schristos     }
1545*56bb7041Schristos }
1546*56bb7041Schristos 
1547*56bb7041Schristos void
uninsert_breakpoints_at(CORE_ADDR pc)1548*56bb7041Schristos uninsert_breakpoints_at (CORE_ADDR pc)
1549*56bb7041Schristos {
1550*56bb7041Schristos   struct process_info *proc = current_process ();
1551*56bb7041Schristos   struct raw_breakpoint *bp;
1552*56bb7041Schristos   int found = 0;
1553*56bb7041Schristos 
1554*56bb7041Schristos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1555*56bb7041Schristos     if ((bp->raw_type == raw_bkpt_type_sw
1556*56bb7041Schristos 	 || bp->raw_type == raw_bkpt_type_hw)
1557*56bb7041Schristos 	&& bp->pc == pc)
1558*56bb7041Schristos       {
1559*56bb7041Schristos 	found = 1;
1560*56bb7041Schristos 
1561*56bb7041Schristos 	if (bp->inserted)
1562*56bb7041Schristos 	  uninsert_raw_breakpoint (bp);
1563*56bb7041Schristos       }
1564*56bb7041Schristos 
1565*56bb7041Schristos   if (!found)
1566*56bb7041Schristos     {
1567*56bb7041Schristos       /* This can happen when we remove all breakpoints while handling
1568*56bb7041Schristos 	 a step-over.  */
1569*56bb7041Schristos       if (debug_threads)
1570*56bb7041Schristos 	debug_printf ("Could not find breakpoint at 0x%s "
1571*56bb7041Schristos 		      "in list (uninserting).\n",
1572*56bb7041Schristos 		      paddress (pc));
1573*56bb7041Schristos     }
1574*56bb7041Schristos }
1575*56bb7041Schristos 
1576*56bb7041Schristos void
uninsert_all_breakpoints(void)1577*56bb7041Schristos uninsert_all_breakpoints (void)
1578*56bb7041Schristos {
1579*56bb7041Schristos   struct process_info *proc = current_process ();
1580*56bb7041Schristos   struct raw_breakpoint *bp;
1581*56bb7041Schristos 
1582*56bb7041Schristos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1583*56bb7041Schristos     if ((bp->raw_type == raw_bkpt_type_sw
1584*56bb7041Schristos 	 || bp->raw_type == raw_bkpt_type_hw)
1585*56bb7041Schristos 	&& bp->inserted)
1586*56bb7041Schristos       uninsert_raw_breakpoint (bp);
1587*56bb7041Schristos }
1588*56bb7041Schristos 
1589*56bb7041Schristos void
uninsert_single_step_breakpoints(struct thread_info * thread)1590*56bb7041Schristos uninsert_single_step_breakpoints (struct thread_info *thread)
1591*56bb7041Schristos {
1592*56bb7041Schristos   struct process_info *proc = get_thread_process (thread);
1593*56bb7041Schristos   struct breakpoint *bp;
1594*56bb7041Schristos 
1595*56bb7041Schristos   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1596*56bb7041Schristos     {
1597*56bb7041Schristos     if (bp->type == single_step_breakpoint
1598*56bb7041Schristos 	&& ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1599*56bb7041Schristos       {
1600*56bb7041Schristos 	gdb_assert (bp->raw->inserted > 0);
1601*56bb7041Schristos 
1602*56bb7041Schristos 	/* Only uninsert the raw breakpoint if it only belongs to a
1603*56bb7041Schristos 	   reinsert breakpoint.  */
1604*56bb7041Schristos 	if (bp->raw->refcount == 1)
1605*56bb7041Schristos 	  {
1606*56bb7041Schristos 	    struct thread_info *saved_thread = current_thread;
1607*56bb7041Schristos 
1608*56bb7041Schristos 	    current_thread = thread;
1609*56bb7041Schristos 	    uninsert_raw_breakpoint (bp->raw);
1610*56bb7041Schristos 	    current_thread = saved_thread;
1611*56bb7041Schristos 	  }
1612*56bb7041Schristos       }
1613*56bb7041Schristos     }
1614*56bb7041Schristos }
1615*56bb7041Schristos 
1616*56bb7041Schristos static void
reinsert_raw_breakpoint(struct raw_breakpoint * bp)1617*56bb7041Schristos reinsert_raw_breakpoint (struct raw_breakpoint *bp)
1618*56bb7041Schristos {
1619*56bb7041Schristos   int err;
1620*56bb7041Schristos 
1621*56bb7041Schristos   if (bp->inserted)
1622*56bb7041Schristos     return;
1623*56bb7041Schristos 
1624*56bb7041Schristos   err = the_target->insert_point (bp->raw_type, bp->pc, bp->kind, bp);
1625*56bb7041Schristos   if (err == 0)
1626*56bb7041Schristos     bp->inserted = 1;
1627*56bb7041Schristos   else if (debug_threads)
1628*56bb7041Schristos     debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).\n",
1629*56bb7041Schristos 		  paddress (bp->pc), err);
1630*56bb7041Schristos }
1631*56bb7041Schristos 
1632*56bb7041Schristos void
reinsert_breakpoints_at(CORE_ADDR pc)1633*56bb7041Schristos reinsert_breakpoints_at (CORE_ADDR pc)
1634*56bb7041Schristos {
1635*56bb7041Schristos   struct process_info *proc = current_process ();
1636*56bb7041Schristos   struct raw_breakpoint *bp;
1637*56bb7041Schristos   int found = 0;
1638*56bb7041Schristos 
1639*56bb7041Schristos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1640*56bb7041Schristos     if ((bp->raw_type == raw_bkpt_type_sw
1641*56bb7041Schristos 	 || bp->raw_type == raw_bkpt_type_hw)
1642*56bb7041Schristos 	&& bp->pc == pc)
1643*56bb7041Schristos       {
1644*56bb7041Schristos 	found = 1;
1645*56bb7041Schristos 
1646*56bb7041Schristos 	reinsert_raw_breakpoint (bp);
1647*56bb7041Schristos       }
1648*56bb7041Schristos 
1649*56bb7041Schristos   if (!found)
1650*56bb7041Schristos     {
1651*56bb7041Schristos       /* This can happen when we remove all breakpoints while handling
1652*56bb7041Schristos 	 a step-over.  */
1653*56bb7041Schristos       if (debug_threads)
1654*56bb7041Schristos 	debug_printf ("Could not find raw breakpoint at 0x%s "
1655*56bb7041Schristos 		      "in list (reinserting).\n",
1656*56bb7041Schristos 		      paddress (pc));
1657*56bb7041Schristos     }
1658*56bb7041Schristos }
1659*56bb7041Schristos 
1660*56bb7041Schristos int
has_single_step_breakpoints(struct thread_info * thread)1661*56bb7041Schristos has_single_step_breakpoints (struct thread_info *thread)
1662*56bb7041Schristos {
1663*56bb7041Schristos   struct process_info *proc = get_thread_process (thread);
1664*56bb7041Schristos   struct breakpoint *bp, **bp_link;
1665*56bb7041Schristos 
1666*56bb7041Schristos   bp = proc->breakpoints;
1667*56bb7041Schristos   bp_link = &proc->breakpoints;
1668*56bb7041Schristos 
1669*56bb7041Schristos   while (bp)
1670*56bb7041Schristos     {
1671*56bb7041Schristos       if (bp->type == single_step_breakpoint
1672*56bb7041Schristos 	  && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1673*56bb7041Schristos 	return 1;
1674*56bb7041Schristos       else
1675*56bb7041Schristos 	{
1676*56bb7041Schristos 	  bp_link = &bp->next;
1677*56bb7041Schristos 	  bp = *bp_link;
1678*56bb7041Schristos 	}
1679*56bb7041Schristos     }
1680*56bb7041Schristos 
1681*56bb7041Schristos   return 0;
1682*56bb7041Schristos }
1683*56bb7041Schristos 
1684*56bb7041Schristos void
reinsert_all_breakpoints(void)1685*56bb7041Schristos reinsert_all_breakpoints (void)
1686*56bb7041Schristos {
1687*56bb7041Schristos   struct process_info *proc = current_process ();
1688*56bb7041Schristos   struct raw_breakpoint *bp;
1689*56bb7041Schristos 
1690*56bb7041Schristos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1691*56bb7041Schristos     if ((bp->raw_type == raw_bkpt_type_sw
1692*56bb7041Schristos 	 || bp->raw_type == raw_bkpt_type_hw)
1693*56bb7041Schristos 	&& !bp->inserted)
1694*56bb7041Schristos       reinsert_raw_breakpoint (bp);
1695*56bb7041Schristos }
1696*56bb7041Schristos 
1697*56bb7041Schristos void
reinsert_single_step_breakpoints(struct thread_info * thread)1698*56bb7041Schristos reinsert_single_step_breakpoints (struct thread_info *thread)
1699*56bb7041Schristos {
1700*56bb7041Schristos   struct process_info *proc = get_thread_process (thread);
1701*56bb7041Schristos   struct breakpoint *bp;
1702*56bb7041Schristos 
1703*56bb7041Schristos   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1704*56bb7041Schristos     {
1705*56bb7041Schristos       if (bp->type == single_step_breakpoint
1706*56bb7041Schristos 	  && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1707*56bb7041Schristos 	{
1708*56bb7041Schristos 	  gdb_assert (bp->raw->inserted > 0);
1709*56bb7041Schristos 
1710*56bb7041Schristos 	  if (bp->raw->refcount == 1)
1711*56bb7041Schristos 	    {
1712*56bb7041Schristos 	      struct thread_info *saved_thread = current_thread;
1713*56bb7041Schristos 
1714*56bb7041Schristos 	      current_thread = thread;
1715*56bb7041Schristos 	      reinsert_raw_breakpoint (bp->raw);
1716*56bb7041Schristos 	      current_thread = saved_thread;
1717*56bb7041Schristos 	    }
1718*56bb7041Schristos 	}
1719*56bb7041Schristos     }
1720*56bb7041Schristos }
1721*56bb7041Schristos 
1722*56bb7041Schristos void
check_breakpoints(CORE_ADDR stop_pc)1723*56bb7041Schristos check_breakpoints (CORE_ADDR stop_pc)
1724*56bb7041Schristos {
1725*56bb7041Schristos   struct process_info *proc = current_process ();
1726*56bb7041Schristos   struct breakpoint *bp, **bp_link;
1727*56bb7041Schristos 
1728*56bb7041Schristos   bp = proc->breakpoints;
1729*56bb7041Schristos   bp_link = &proc->breakpoints;
1730*56bb7041Schristos 
1731*56bb7041Schristos   while (bp)
1732*56bb7041Schristos     {
1733*56bb7041Schristos       struct raw_breakpoint *raw = bp->raw;
1734*56bb7041Schristos 
1735*56bb7041Schristos       if ((raw->raw_type == raw_bkpt_type_sw
1736*56bb7041Schristos 	   || raw->raw_type == raw_bkpt_type_hw)
1737*56bb7041Schristos 	  && raw->pc == stop_pc)
1738*56bb7041Schristos 	{
1739*56bb7041Schristos 	  if (!raw->inserted)
1740*56bb7041Schristos 	    {
1741*56bb7041Schristos 	      warning ("Hit a removed breakpoint?");
1742*56bb7041Schristos 	      return;
1743*56bb7041Schristos 	    }
1744*56bb7041Schristos 
1745*56bb7041Schristos 	  if (bp->type == other_breakpoint)
1746*56bb7041Schristos 	    {
1747*56bb7041Schristos 	      struct other_breakpoint *other_bp
1748*56bb7041Schristos 		= (struct other_breakpoint *) bp;
1749*56bb7041Schristos 
1750*56bb7041Schristos 	      if (other_bp->handler != NULL && (*other_bp->handler) (stop_pc))
1751*56bb7041Schristos 		{
1752*56bb7041Schristos 		  *bp_link = bp->next;
1753*56bb7041Schristos 
1754*56bb7041Schristos 		  release_breakpoint (proc, bp);
1755*56bb7041Schristos 
1756*56bb7041Schristos 		  bp = *bp_link;
1757*56bb7041Schristos 		  continue;
1758*56bb7041Schristos 		}
1759*56bb7041Schristos 	    }
1760*56bb7041Schristos 	}
1761*56bb7041Schristos 
1762*56bb7041Schristos       bp_link = &bp->next;
1763*56bb7041Schristos       bp = *bp_link;
1764*56bb7041Schristos     }
1765*56bb7041Schristos }
1766*56bb7041Schristos 
1767*56bb7041Schristos int
breakpoint_here(CORE_ADDR addr)1768*56bb7041Schristos breakpoint_here (CORE_ADDR addr)
1769*56bb7041Schristos {
1770*56bb7041Schristos   struct process_info *proc = current_process ();
1771*56bb7041Schristos   struct raw_breakpoint *bp;
1772*56bb7041Schristos 
1773*56bb7041Schristos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1774*56bb7041Schristos     if ((bp->raw_type == raw_bkpt_type_sw
1775*56bb7041Schristos 	 || bp->raw_type == raw_bkpt_type_hw)
1776*56bb7041Schristos 	&& bp->pc == addr)
1777*56bb7041Schristos       return 1;
1778*56bb7041Schristos 
1779*56bb7041Schristos   return 0;
1780*56bb7041Schristos }
1781*56bb7041Schristos 
1782*56bb7041Schristos int
breakpoint_inserted_here(CORE_ADDR addr)1783*56bb7041Schristos breakpoint_inserted_here (CORE_ADDR addr)
1784*56bb7041Schristos {
1785*56bb7041Schristos   struct process_info *proc = current_process ();
1786*56bb7041Schristos   struct raw_breakpoint *bp;
1787*56bb7041Schristos 
1788*56bb7041Schristos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1789*56bb7041Schristos     if ((bp->raw_type == raw_bkpt_type_sw
1790*56bb7041Schristos 	 || bp->raw_type == raw_bkpt_type_hw)
1791*56bb7041Schristos 	&& bp->pc == addr
1792*56bb7041Schristos 	&& bp->inserted)
1793*56bb7041Schristos       return 1;
1794*56bb7041Schristos 
1795*56bb7041Schristos   return 0;
1796*56bb7041Schristos }
1797*56bb7041Schristos 
1798*56bb7041Schristos /* See mem-break.h.  */
1799*56bb7041Schristos 
1800*56bb7041Schristos int
software_breakpoint_inserted_here(CORE_ADDR addr)1801*56bb7041Schristos software_breakpoint_inserted_here (CORE_ADDR addr)
1802*56bb7041Schristos {
1803*56bb7041Schristos   struct process_info *proc = current_process ();
1804*56bb7041Schristos   struct raw_breakpoint *bp;
1805*56bb7041Schristos 
1806*56bb7041Schristos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1807*56bb7041Schristos     if (bp->raw_type == raw_bkpt_type_sw
1808*56bb7041Schristos 	&& bp->pc == addr
1809*56bb7041Schristos 	&& bp->inserted)
1810*56bb7041Schristos       return 1;
1811*56bb7041Schristos 
1812*56bb7041Schristos   return 0;
1813*56bb7041Schristos }
1814*56bb7041Schristos 
1815*56bb7041Schristos /* See mem-break.h.  */
1816*56bb7041Schristos 
1817*56bb7041Schristos int
hardware_breakpoint_inserted_here(CORE_ADDR addr)1818*56bb7041Schristos hardware_breakpoint_inserted_here (CORE_ADDR addr)
1819*56bb7041Schristos {
1820*56bb7041Schristos   struct process_info *proc = current_process ();
1821*56bb7041Schristos   struct raw_breakpoint *bp;
1822*56bb7041Schristos 
1823*56bb7041Schristos   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1824*56bb7041Schristos     if (bp->raw_type == raw_bkpt_type_hw
1825*56bb7041Schristos 	&& bp->pc == addr
1826*56bb7041Schristos 	&& bp->inserted)
1827*56bb7041Schristos       return 1;
1828*56bb7041Schristos 
1829*56bb7041Schristos   return 0;
1830*56bb7041Schristos }
1831*56bb7041Schristos 
1832*56bb7041Schristos /* See mem-break.h.  */
1833*56bb7041Schristos 
1834*56bb7041Schristos int
single_step_breakpoint_inserted_here(CORE_ADDR addr)1835*56bb7041Schristos single_step_breakpoint_inserted_here (CORE_ADDR addr)
1836*56bb7041Schristos {
1837*56bb7041Schristos   struct process_info *proc = current_process ();
1838*56bb7041Schristos   struct breakpoint *bp;
1839*56bb7041Schristos 
1840*56bb7041Schristos   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1841*56bb7041Schristos     if (bp->type == single_step_breakpoint
1842*56bb7041Schristos 	&& bp->raw->pc == addr
1843*56bb7041Schristos 	&& bp->raw->inserted)
1844*56bb7041Schristos       return 1;
1845*56bb7041Schristos 
1846*56bb7041Schristos   return 0;
1847*56bb7041Schristos }
1848*56bb7041Schristos 
1849*56bb7041Schristos static int
validate_inserted_breakpoint(struct raw_breakpoint * bp)1850*56bb7041Schristos validate_inserted_breakpoint (struct raw_breakpoint *bp)
1851*56bb7041Schristos {
1852*56bb7041Schristos   unsigned char *buf;
1853*56bb7041Schristos   int err;
1854*56bb7041Schristos 
1855*56bb7041Schristos   gdb_assert (bp->inserted);
1856*56bb7041Schristos   gdb_assert (bp->raw_type == raw_bkpt_type_sw);
1857*56bb7041Schristos 
1858*56bb7041Schristos   buf = (unsigned char *) alloca (bp_size (bp));
1859*56bb7041Schristos   err = the_target->read_memory (bp->pc, buf, bp_size (bp));
1860*56bb7041Schristos   if (err || memcmp (buf, bp_opcode (bp), bp_size (bp)) != 0)
1861*56bb7041Schristos     {
1862*56bb7041Schristos       /* Tag it as gone.  */
1863*56bb7041Schristos       bp->inserted = -1;
1864*56bb7041Schristos       return 0;
1865*56bb7041Schristos     }
1866*56bb7041Schristos 
1867*56bb7041Schristos   return 1;
1868*56bb7041Schristos }
1869*56bb7041Schristos 
1870*56bb7041Schristos static void
delete_disabled_breakpoints(void)1871*56bb7041Schristos delete_disabled_breakpoints (void)
1872*56bb7041Schristos {
1873*56bb7041Schristos   struct process_info *proc = current_process ();
1874*56bb7041Schristos   struct breakpoint *bp, *next;
1875*56bb7041Schristos 
1876*56bb7041Schristos   for (bp = proc->breakpoints; bp != NULL; bp = next)
1877*56bb7041Schristos     {
1878*56bb7041Schristos       next = bp->next;
1879*56bb7041Schristos       if (bp->raw->inserted < 0)
1880*56bb7041Schristos 	{
1881*56bb7041Schristos 	  /* If single_step_breakpoints become disabled, that means the
1882*56bb7041Schristos 	     manipulations (insertion and removal) of them are wrong.  */
1883*56bb7041Schristos 	  gdb_assert (bp->type != single_step_breakpoint);
1884*56bb7041Schristos 	  delete_breakpoint_1 (proc, bp);
1885*56bb7041Schristos 	}
1886*56bb7041Schristos     }
1887*56bb7041Schristos }
1888*56bb7041Schristos 
1889*56bb7041Schristos /* Check if breakpoints we inserted still appear to be inserted.  They
1890*56bb7041Schristos    may disappear due to a shared library unload, and worse, a new
1891*56bb7041Schristos    shared library may be reloaded at the same address as the
1892*56bb7041Schristos    previously unloaded one.  If that happens, we should make sure that
1893*56bb7041Schristos    the shadow memory of the old breakpoints isn't used when reading or
1894*56bb7041Schristos    writing memory.  */
1895*56bb7041Schristos 
1896*56bb7041Schristos void
validate_breakpoints(void)1897*56bb7041Schristos validate_breakpoints (void)
1898*56bb7041Schristos {
1899*56bb7041Schristos   struct process_info *proc = current_process ();
1900*56bb7041Schristos   struct breakpoint *bp;
1901*56bb7041Schristos 
1902*56bb7041Schristos   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1903*56bb7041Schristos     {
1904*56bb7041Schristos       struct raw_breakpoint *raw = bp->raw;
1905*56bb7041Schristos 
1906*56bb7041Schristos       if (raw->raw_type == raw_bkpt_type_sw && raw->inserted > 0)
1907*56bb7041Schristos 	validate_inserted_breakpoint (raw);
1908*56bb7041Schristos     }
1909*56bb7041Schristos 
1910*56bb7041Schristos   delete_disabled_breakpoints ();
1911*56bb7041Schristos }
1912*56bb7041Schristos 
1913*56bb7041Schristos void
check_mem_read(CORE_ADDR mem_addr,unsigned char * buf,int mem_len)1914*56bb7041Schristos check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
1915*56bb7041Schristos {
1916*56bb7041Schristos   struct process_info *proc = current_process ();
1917*56bb7041Schristos   struct raw_breakpoint *bp = proc->raw_breakpoints;
1918*56bb7041Schristos   struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1919*56bb7041Schristos   CORE_ADDR mem_end = mem_addr + mem_len;
1920*56bb7041Schristos   int disabled_one = 0;
1921*56bb7041Schristos 
1922*56bb7041Schristos   for (; jp != NULL; jp = jp->next)
1923*56bb7041Schristos     {
1924*56bb7041Schristos       CORE_ADDR bp_end = jp->pc + jp->length;
1925*56bb7041Schristos       CORE_ADDR start, end;
1926*56bb7041Schristos       int copy_offset, copy_len, buf_offset;
1927*56bb7041Schristos 
1928*56bb7041Schristos       gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len
1929*56bb7041Schristos 		  || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1930*56bb7041Schristos 
1931*56bb7041Schristos       if (mem_addr >= bp_end)
1932*56bb7041Schristos 	continue;
1933*56bb7041Schristos       if (jp->pc >= mem_end)
1934*56bb7041Schristos 	continue;
1935*56bb7041Schristos 
1936*56bb7041Schristos       start = jp->pc;
1937*56bb7041Schristos       if (mem_addr > start)
1938*56bb7041Schristos 	start = mem_addr;
1939*56bb7041Schristos 
1940*56bb7041Schristos       end = bp_end;
1941*56bb7041Schristos       if (end > mem_end)
1942*56bb7041Schristos 	end = mem_end;
1943*56bb7041Schristos 
1944*56bb7041Schristos       copy_len = end - start;
1945*56bb7041Schristos       copy_offset = start - jp->pc;
1946*56bb7041Schristos       buf_offset = start - mem_addr;
1947*56bb7041Schristos 
1948*56bb7041Schristos       if (jp->inserted)
1949*56bb7041Schristos 	memcpy (buf + buf_offset,
1950*56bb7041Schristos 		fast_tracepoint_jump_shadow (jp) + copy_offset,
1951*56bb7041Schristos 		copy_len);
1952*56bb7041Schristos     }
1953*56bb7041Schristos 
1954*56bb7041Schristos   for (; bp != NULL; bp = bp->next)
1955*56bb7041Schristos     {
1956*56bb7041Schristos       CORE_ADDR bp_end = bp->pc + bp_size (bp);
1957*56bb7041Schristos       CORE_ADDR start, end;
1958*56bb7041Schristos       int copy_offset, copy_len, buf_offset;
1959*56bb7041Schristos 
1960*56bb7041Schristos       if (bp->raw_type != raw_bkpt_type_sw)
1961*56bb7041Schristos 	continue;
1962*56bb7041Schristos 
1963*56bb7041Schristos       gdb_assert (bp->old_data >= buf + mem_len
1964*56bb7041Schristos 		  || buf >= &bp->old_data[sizeof (bp->old_data)]);
1965*56bb7041Schristos 
1966*56bb7041Schristos       if (mem_addr >= bp_end)
1967*56bb7041Schristos 	continue;
1968*56bb7041Schristos       if (bp->pc >= mem_end)
1969*56bb7041Schristos 	continue;
1970*56bb7041Schristos 
1971*56bb7041Schristos       start = bp->pc;
1972*56bb7041Schristos       if (mem_addr > start)
1973*56bb7041Schristos 	start = mem_addr;
1974*56bb7041Schristos 
1975*56bb7041Schristos       end = bp_end;
1976*56bb7041Schristos       if (end > mem_end)
1977*56bb7041Schristos 	end = mem_end;
1978*56bb7041Schristos 
1979*56bb7041Schristos       copy_len = end - start;
1980*56bb7041Schristos       copy_offset = start - bp->pc;
1981*56bb7041Schristos       buf_offset = start - mem_addr;
1982*56bb7041Schristos 
1983*56bb7041Schristos       if (bp->inserted > 0)
1984*56bb7041Schristos 	{
1985*56bb7041Schristos 	  if (validate_inserted_breakpoint (bp))
1986*56bb7041Schristos 	    memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
1987*56bb7041Schristos 	  else
1988*56bb7041Schristos 	    disabled_one = 1;
1989*56bb7041Schristos 	}
1990*56bb7041Schristos     }
1991*56bb7041Schristos 
1992*56bb7041Schristos   if (disabled_one)
1993*56bb7041Schristos     delete_disabled_breakpoints ();
1994*56bb7041Schristos }
1995*56bb7041Schristos 
1996*56bb7041Schristos void
check_mem_write(CORE_ADDR mem_addr,unsigned char * buf,const unsigned char * myaddr,int mem_len)1997*56bb7041Schristos check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
1998*56bb7041Schristos 		 const unsigned char *myaddr, int mem_len)
1999*56bb7041Schristos {
2000*56bb7041Schristos   struct process_info *proc = current_process ();
2001*56bb7041Schristos   struct raw_breakpoint *bp = proc->raw_breakpoints;
2002*56bb7041Schristos   struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
2003*56bb7041Schristos   CORE_ADDR mem_end = mem_addr + mem_len;
2004*56bb7041Schristos   int disabled_one = 0;
2005*56bb7041Schristos 
2006*56bb7041Schristos   /* First fast tracepoint jumps, then breakpoint traps on top.  */
2007*56bb7041Schristos 
2008*56bb7041Schristos   for (; jp != NULL; jp = jp->next)
2009*56bb7041Schristos     {
2010*56bb7041Schristos       CORE_ADDR jp_end = jp->pc + jp->length;
2011*56bb7041Schristos       CORE_ADDR start, end;
2012*56bb7041Schristos       int copy_offset, copy_len, buf_offset;
2013*56bb7041Schristos 
2014*56bb7041Schristos       gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len
2015*56bb7041Schristos 		  || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
2016*56bb7041Schristos       gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len
2017*56bb7041Schristos 		  || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length);
2018*56bb7041Schristos 
2019*56bb7041Schristos       if (mem_addr >= jp_end)
2020*56bb7041Schristos 	continue;
2021*56bb7041Schristos       if (jp->pc >= mem_end)
2022*56bb7041Schristos 	continue;
2023*56bb7041Schristos 
2024*56bb7041Schristos       start = jp->pc;
2025*56bb7041Schristos       if (mem_addr > start)
2026*56bb7041Schristos 	start = mem_addr;
2027*56bb7041Schristos 
2028*56bb7041Schristos       end = jp_end;
2029*56bb7041Schristos       if (end > mem_end)
2030*56bb7041Schristos 	end = mem_end;
2031*56bb7041Schristos 
2032*56bb7041Schristos       copy_len = end - start;
2033*56bb7041Schristos       copy_offset = start - jp->pc;
2034*56bb7041Schristos       buf_offset = start - mem_addr;
2035*56bb7041Schristos 
2036*56bb7041Schristos       memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset,
2037*56bb7041Schristos 	      myaddr + buf_offset, copy_len);
2038*56bb7041Schristos       if (jp->inserted)
2039*56bb7041Schristos 	memcpy (buf + buf_offset,
2040*56bb7041Schristos 		fast_tracepoint_jump_insn (jp) + copy_offset, copy_len);
2041*56bb7041Schristos     }
2042*56bb7041Schristos 
2043*56bb7041Schristos   for (; bp != NULL; bp = bp->next)
2044*56bb7041Schristos     {
2045*56bb7041Schristos       CORE_ADDR bp_end = bp->pc + bp_size (bp);
2046*56bb7041Schristos       CORE_ADDR start, end;
2047*56bb7041Schristos       int copy_offset, copy_len, buf_offset;
2048*56bb7041Schristos 
2049*56bb7041Schristos       if (bp->raw_type != raw_bkpt_type_sw)
2050*56bb7041Schristos 	continue;
2051*56bb7041Schristos 
2052*56bb7041Schristos       gdb_assert (bp->old_data >= myaddr + mem_len
2053*56bb7041Schristos 		  || myaddr >= &bp->old_data[sizeof (bp->old_data)]);
2054*56bb7041Schristos 
2055*56bb7041Schristos       if (mem_addr >= bp_end)
2056*56bb7041Schristos 	continue;
2057*56bb7041Schristos       if (bp->pc >= mem_end)
2058*56bb7041Schristos 	continue;
2059*56bb7041Schristos 
2060*56bb7041Schristos       start = bp->pc;
2061*56bb7041Schristos       if (mem_addr > start)
2062*56bb7041Schristos 	start = mem_addr;
2063*56bb7041Schristos 
2064*56bb7041Schristos       end = bp_end;
2065*56bb7041Schristos       if (end > mem_end)
2066*56bb7041Schristos 	end = mem_end;
2067*56bb7041Schristos 
2068*56bb7041Schristos       copy_len = end - start;
2069*56bb7041Schristos       copy_offset = start - bp->pc;
2070*56bb7041Schristos       buf_offset = start - mem_addr;
2071*56bb7041Schristos 
2072*56bb7041Schristos       memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len);
2073*56bb7041Schristos       if (bp->inserted > 0)
2074*56bb7041Schristos 	{
2075*56bb7041Schristos 	  if (validate_inserted_breakpoint (bp))
2076*56bb7041Schristos 	    memcpy (buf + buf_offset, bp_opcode (bp) + copy_offset, copy_len);
2077*56bb7041Schristos 	  else
2078*56bb7041Schristos 	    disabled_one = 1;
2079*56bb7041Schristos 	}
2080*56bb7041Schristos     }
2081*56bb7041Schristos 
2082*56bb7041Schristos   if (disabled_one)
2083*56bb7041Schristos     delete_disabled_breakpoints ();
2084*56bb7041Schristos }
2085*56bb7041Schristos 
2086*56bb7041Schristos /* Delete all breakpoints, and un-insert them from the inferior.  */
2087*56bb7041Schristos 
2088*56bb7041Schristos void
delete_all_breakpoints(void)2089*56bb7041Schristos delete_all_breakpoints (void)
2090*56bb7041Schristos {
2091*56bb7041Schristos   struct process_info *proc = current_process ();
2092*56bb7041Schristos 
2093*56bb7041Schristos   while (proc->breakpoints)
2094*56bb7041Schristos     delete_breakpoint_1 (proc, proc->breakpoints);
2095*56bb7041Schristos }
2096*56bb7041Schristos 
2097*56bb7041Schristos /* Clear the "inserted" flag in all breakpoints.  */
2098*56bb7041Schristos 
2099*56bb7041Schristos void
mark_breakpoints_out(struct process_info * proc)2100*56bb7041Schristos mark_breakpoints_out (struct process_info *proc)
2101*56bb7041Schristos {
2102*56bb7041Schristos   struct raw_breakpoint *raw_bp;
2103*56bb7041Schristos 
2104*56bb7041Schristos   for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next)
2105*56bb7041Schristos     raw_bp->inserted = 0;
2106*56bb7041Schristos }
2107*56bb7041Schristos 
2108*56bb7041Schristos /* Release all breakpoints, but do not try to un-insert them from the
2109*56bb7041Schristos    inferior.  */
2110*56bb7041Schristos 
2111*56bb7041Schristos void
free_all_breakpoints(struct process_info * proc)2112*56bb7041Schristos free_all_breakpoints (struct process_info *proc)
2113*56bb7041Schristos {
2114*56bb7041Schristos   mark_breakpoints_out (proc);
2115*56bb7041Schristos 
2116*56bb7041Schristos   /* Note: use PROC explicitly instead of deferring to
2117*56bb7041Schristos      delete_all_breakpoints --- CURRENT_INFERIOR may already have been
2118*56bb7041Schristos      released when we get here.  There should be no call to
2119*56bb7041Schristos      current_process from here on.  */
2120*56bb7041Schristos   while (proc->breakpoints)
2121*56bb7041Schristos     delete_breakpoint_1 (proc, proc->breakpoints);
2122*56bb7041Schristos }
2123*56bb7041Schristos 
2124*56bb7041Schristos /* Clone an agent expression.  */
2125*56bb7041Schristos 
2126*56bb7041Schristos static struct agent_expr *
clone_agent_expr(const struct agent_expr * src_ax)2127*56bb7041Schristos clone_agent_expr (const struct agent_expr *src_ax)
2128*56bb7041Schristos {
2129*56bb7041Schristos   struct agent_expr *ax;
2130*56bb7041Schristos 
2131*56bb7041Schristos   ax = XCNEW (struct agent_expr);
2132*56bb7041Schristos   ax->length = src_ax->length;
2133*56bb7041Schristos   ax->bytes = (unsigned char *) xcalloc (ax->length, 1);
2134*56bb7041Schristos   memcpy (ax->bytes, src_ax->bytes, ax->length);
2135*56bb7041Schristos   return ax;
2136*56bb7041Schristos }
2137*56bb7041Schristos 
2138*56bb7041Schristos /* Deep-copy the contents of one breakpoint to another.  */
2139*56bb7041Schristos 
2140*56bb7041Schristos static struct breakpoint *
clone_one_breakpoint(const struct breakpoint * src,ptid_t ptid)2141*56bb7041Schristos clone_one_breakpoint (const struct breakpoint *src, ptid_t ptid)
2142*56bb7041Schristos {
2143*56bb7041Schristos   struct breakpoint *dest;
2144*56bb7041Schristos   struct raw_breakpoint *dest_raw;
2145*56bb7041Schristos 
2146*56bb7041Schristos   /* Clone the raw breakpoint.  */
2147*56bb7041Schristos   dest_raw = XCNEW (struct raw_breakpoint);
2148*56bb7041Schristos   dest_raw->raw_type = src->raw->raw_type;
2149*56bb7041Schristos   dest_raw->refcount = src->raw->refcount;
2150*56bb7041Schristos   dest_raw->pc = src->raw->pc;
2151*56bb7041Schristos   dest_raw->kind = src->raw->kind;
2152*56bb7041Schristos   memcpy (dest_raw->old_data, src->raw->old_data, MAX_BREAKPOINT_LEN);
2153*56bb7041Schristos   dest_raw->inserted = src->raw->inserted;
2154*56bb7041Schristos 
2155*56bb7041Schristos   /* Clone the high-level breakpoint.  */
2156*56bb7041Schristos   if (is_gdb_breakpoint (src->type))
2157*56bb7041Schristos     {
2158*56bb7041Schristos       struct gdb_breakpoint *gdb_dest = XCNEW (struct gdb_breakpoint);
2159*56bb7041Schristos       struct point_cond_list *current_cond;
2160*56bb7041Schristos       struct point_cond_list *new_cond;
2161*56bb7041Schristos       struct point_cond_list *cond_tail = NULL;
2162*56bb7041Schristos       struct point_command_list *current_cmd;
2163*56bb7041Schristos       struct point_command_list *new_cmd;
2164*56bb7041Schristos       struct point_command_list *cmd_tail = NULL;
2165*56bb7041Schristos 
2166*56bb7041Schristos       /* Clone the condition list.  */
2167*56bb7041Schristos       for (current_cond = ((struct gdb_breakpoint *) src)->cond_list;
2168*56bb7041Schristos 	   current_cond != NULL;
2169*56bb7041Schristos 	   current_cond = current_cond->next)
2170*56bb7041Schristos 	{
2171*56bb7041Schristos 	  new_cond = XCNEW (struct point_cond_list);
2172*56bb7041Schristos 	  new_cond->cond = clone_agent_expr (current_cond->cond);
2173*56bb7041Schristos 	  APPEND_TO_LIST (&gdb_dest->cond_list, new_cond, cond_tail);
2174*56bb7041Schristos 	}
2175*56bb7041Schristos 
2176*56bb7041Schristos       /* Clone the command list.  */
2177*56bb7041Schristos       for (current_cmd = ((struct gdb_breakpoint *) src)->command_list;
2178*56bb7041Schristos 	   current_cmd != NULL;
2179*56bb7041Schristos 	   current_cmd = current_cmd->next)
2180*56bb7041Schristos 	{
2181*56bb7041Schristos 	  new_cmd = XCNEW (struct point_command_list);
2182*56bb7041Schristos 	  new_cmd->cmd = clone_agent_expr (current_cmd->cmd);
2183*56bb7041Schristos 	  new_cmd->persistence = current_cmd->persistence;
2184*56bb7041Schristos 	  APPEND_TO_LIST (&gdb_dest->command_list, new_cmd, cmd_tail);
2185*56bb7041Schristos 	}
2186*56bb7041Schristos 
2187*56bb7041Schristos       dest = (struct breakpoint *) gdb_dest;
2188*56bb7041Schristos     }
2189*56bb7041Schristos   else if (src->type == other_breakpoint)
2190*56bb7041Schristos     {
2191*56bb7041Schristos       struct other_breakpoint *other_dest = XCNEW (struct other_breakpoint);
2192*56bb7041Schristos 
2193*56bb7041Schristos       other_dest->handler = ((struct other_breakpoint *) src)->handler;
2194*56bb7041Schristos       dest = (struct breakpoint *) other_dest;
2195*56bb7041Schristos     }
2196*56bb7041Schristos   else if (src->type == single_step_breakpoint)
2197*56bb7041Schristos     {
2198*56bb7041Schristos       struct single_step_breakpoint *ss_dest
2199*56bb7041Schristos 	= XCNEW (struct single_step_breakpoint);
2200*56bb7041Schristos 
2201*56bb7041Schristos       dest = (struct breakpoint *) ss_dest;
2202*56bb7041Schristos       /* Since single-step breakpoint is thread specific, don't copy
2203*56bb7041Schristos 	 thread id from SRC, use ID instead.  */
2204*56bb7041Schristos       ss_dest->ptid = ptid;
2205*56bb7041Schristos     }
2206*56bb7041Schristos   else
2207*56bb7041Schristos     gdb_assert_not_reached ("unhandled breakpoint type");
2208*56bb7041Schristos 
2209*56bb7041Schristos   dest->type = src->type;
2210*56bb7041Schristos   dest->raw = dest_raw;
2211*56bb7041Schristos 
2212*56bb7041Schristos   return dest;
2213*56bb7041Schristos }
2214*56bb7041Schristos 
2215*56bb7041Schristos /* See mem-break.h.  */
2216*56bb7041Schristos 
2217*56bb7041Schristos void
clone_all_breakpoints(struct thread_info * child_thread,const struct thread_info * parent_thread)2218*56bb7041Schristos clone_all_breakpoints (struct thread_info *child_thread,
2219*56bb7041Schristos 		       const struct thread_info *parent_thread)
2220*56bb7041Schristos {
2221*56bb7041Schristos   const struct breakpoint *bp;
2222*56bb7041Schristos   struct breakpoint *new_bkpt;
2223*56bb7041Schristos   struct breakpoint *bkpt_tail = NULL;
2224*56bb7041Schristos   struct raw_breakpoint *raw_bkpt_tail = NULL;
2225*56bb7041Schristos   struct process_info *child_proc = get_thread_process (child_thread);
2226*56bb7041Schristos   struct process_info *parent_proc = get_thread_process (parent_thread);
2227*56bb7041Schristos   struct breakpoint **new_list = &child_proc->breakpoints;
2228*56bb7041Schristos   struct raw_breakpoint **new_raw_list = &child_proc->raw_breakpoints;
2229*56bb7041Schristos 
2230*56bb7041Schristos   for (bp = parent_proc->breakpoints; bp != NULL; bp = bp->next)
2231*56bb7041Schristos     {
2232*56bb7041Schristos       new_bkpt = clone_one_breakpoint (bp, ptid_of (child_thread));
2233*56bb7041Schristos       APPEND_TO_LIST (new_list, new_bkpt, bkpt_tail);
2234*56bb7041Schristos       APPEND_TO_LIST (new_raw_list, new_bkpt->raw, raw_bkpt_tail);
2235*56bb7041Schristos     }
2236*56bb7041Schristos }
2237