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