OSDN Git Service

Use datarootdir for locales.
[pf3gnuchains/pf3gnuchains4x.git] / gdb / gdbserver / mem-break.c
1 /* Memory breakpoint operations for the remote server for GDB.
2    Copyright (C) 2002-2003, 2005, 2007-2012 Free Software Foundation,
3    Inc.
4
5    Contributed by MontaVista Software.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "server.h"
23
24 const unsigned char *breakpoint_data;
25 int breakpoint_len;
26
27 #define MAX_BREAKPOINT_LEN 8
28
29 /* GDB will never try to install multiple breakpoints at the same
30    address.  But, we need to keep track of internal breakpoints too,
31    and so we do need to be able to install multiple breakpoints at the
32    same address transparently.  We keep track of two different, and
33    closely related structures.  A raw breakpoint, which manages the
34    low level, close to the metal aspect of a breakpoint.  It holds the
35    breakpoint address, and a buffer holding a copy of the instructions
36    that would be in memory had not been a breakpoint there (we call
37    that the shadow memory of the breakpoint).  We occasionally need to
38    temporarilly uninsert a breakpoint without the client knowing about
39    it (e.g., to step over an internal breakpoint), so we keep an
40    `inserted' state associated with this low level breakpoint
41    structure.  There can only be one such object for a given address.
42    Then, we have (a bit higher level) breakpoints.  This structure
43    holds a callback to be called whenever a breakpoint is hit, a
44    high-level type, and a link to a low level raw breakpoint.  There
45    can be many high-level breakpoints at the same address, and all of
46    them will point to the same raw breakpoint, which is reference
47    counted.  */
48
49 /* The low level, physical, raw breakpoint.  */
50 struct raw_breakpoint
51 {
52   struct raw_breakpoint *next;
53
54   /* A reference count.  Each high level breakpoint referencing this
55      raw breakpoint accounts for one reference.  */
56   int refcount;
57
58   /* The breakpoint's insertion address.  There can only be one raw
59      breakpoint for a given PC.  */
60   CORE_ADDR pc;
61
62   /* The breakpoint's shadow memory.  */
63   unsigned char old_data[MAX_BREAKPOINT_LEN];
64
65   /* Non-zero if this breakpoint is currently inserted in the
66      inferior.  */
67   int inserted;
68
69   /* Non-zero if this breakpoint is currently disabled because we no
70      longer detect it as inserted.  */
71   int shlib_disabled;
72 };
73
74 /* The type of a breakpoint.  */
75 enum bkpt_type
76   {
77     /* A GDB breakpoint, requested with a Z0 packet.  */
78     gdb_breakpoint,
79
80     /* A basic-software-single-step breakpoint.  */
81     reinsert_breakpoint,
82
83     /* Any other breakpoint type that doesn't require specific
84        treatment goes here.  E.g., an event breakpoint.  */
85     other_breakpoint,
86   };
87
88 /* A high level (in gdbserver's perspective) breakpoint.  */
89 struct breakpoint
90 {
91   struct breakpoint *next;
92
93   /* The breakpoint's type.  */
94   enum bkpt_type type;
95
96   /* Link to this breakpoint's raw breakpoint.  This is always
97      non-NULL.  */
98   struct raw_breakpoint *raw;
99
100   /* Function to call when we hit this breakpoint.  If it returns 1,
101      the breakpoint shall be deleted; 0 or if this callback is NULL,
102      it will be left inserted.  */
103   int (*handler) (CORE_ADDR);
104 };
105
106 static struct raw_breakpoint *
107 find_raw_breakpoint_at (CORE_ADDR where)
108 {
109   struct process_info *proc = current_process ();
110   struct raw_breakpoint *bp;
111
112   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
113     if (bp->pc == where)
114       return bp;
115
116   return NULL;
117 }
118
119 static struct raw_breakpoint *
120 set_raw_breakpoint_at (CORE_ADDR where)
121 {
122   struct process_info *proc = current_process ();
123   struct raw_breakpoint *bp;
124   int err;
125   unsigned char buf[MAX_BREAKPOINT_LEN];
126
127   if (breakpoint_data == NULL)
128     error ("Target does not support breakpoints.");
129
130   bp = find_raw_breakpoint_at (where);
131   if (bp != NULL)
132     {
133       bp->refcount++;
134       return bp;
135     }
136
137   bp = xcalloc (1, sizeof (*bp));
138   bp->pc = where;
139   bp->refcount = 1;
140
141   /* Note that there can be fast tracepoint jumps installed in the
142      same memory range, so to get at the original memory, we need to
143      use read_inferior_memory, which masks those out.  */
144   err = read_inferior_memory (where, buf, breakpoint_len);
145   if (err != 0)
146     {
147       if (debug_threads)
148         fprintf (stderr,
149                  "Failed to read shadow memory of"
150                  " breakpoint at 0x%s (%s).\n",
151                  paddress (where), strerror (err));
152       free (bp);
153       return NULL;
154     }
155   memcpy (bp->old_data, buf, breakpoint_len);
156
157   err = (*the_target->write_memory) (where, breakpoint_data,
158                                      breakpoint_len);
159   if (err != 0)
160     {
161       if (debug_threads)
162         fprintf (stderr,
163                  "Failed to insert breakpoint at 0x%s (%s).\n",
164                  paddress (where), strerror (err));
165       free (bp);
166       return NULL;
167     }
168
169   /* Link the breakpoint in.  */
170   bp->inserted = 1;
171   bp->next = proc->raw_breakpoints;
172   proc->raw_breakpoints = bp;
173   return bp;
174 }
175
176 /* Notice that breakpoint traps are always installed on top of fast
177    tracepoint jumps.  This is even if the fast tracepoint is installed
178    at a later time compared to when the breakpoint was installed.
179    This means that a stopping breakpoint or tracepoint has higher
180    "priority".  In turn, this allows having fast and slow tracepoints
181    (and breakpoints) at the same address behave correctly.  */
182
183
184 /* A fast tracepoint jump.  */
185
186 struct fast_tracepoint_jump
187 {
188   struct fast_tracepoint_jump *next;
189
190   /* A reference count.  GDB can install more than one fast tracepoint
191      at the same address (each with its own action list, for
192      example).  */
193   int refcount;
194
195   /* The fast tracepoint's insertion address.  There can only be one
196      of these for a given PC.  */
197   CORE_ADDR pc;
198
199   /* Non-zero if this fast tracepoint jump is currently inserted in
200      the inferior.  */
201   int inserted;
202
203   /* The length of the jump instruction.  */
204   int length;
205
206   /* A poor-man's flexible array member, holding both the jump
207      instruction to insert, and a copy of the instruction that would
208      be in memory had not been a jump there (the shadow memory of the
209      tracepoint jump).  */
210   unsigned char insn_and_shadow[0];
211 };
212
213 /* Fast tracepoint FP's jump instruction to insert.  */
214 #define fast_tracepoint_jump_insn(fp) \
215   ((fp)->insn_and_shadow + 0)
216
217 /* The shadow memory of fast tracepoint jump FP.  */
218 #define fast_tracepoint_jump_shadow(fp) \
219   ((fp)->insn_and_shadow + (fp)->length)
220
221
222 /* Return the fast tracepoint jump set at WHERE.  */
223
224 static struct fast_tracepoint_jump *
225 find_fast_tracepoint_jump_at (CORE_ADDR where)
226 {
227   struct process_info *proc = current_process ();
228   struct fast_tracepoint_jump *jp;
229
230   for (jp = proc->fast_tracepoint_jumps; jp != NULL; jp = jp->next)
231     if (jp->pc == where)
232       return jp;
233
234   return NULL;
235 }
236
237 int
238 fast_tracepoint_jump_here (CORE_ADDR where)
239 {
240   struct fast_tracepoint_jump *jp = find_fast_tracepoint_jump_at (where);
241
242   return (jp != NULL);
243 }
244
245 int
246 delete_fast_tracepoint_jump (struct fast_tracepoint_jump *todel)
247 {
248   struct fast_tracepoint_jump *bp, **bp_link;
249   int ret;
250   struct process_info *proc = current_process ();
251
252   bp = proc->fast_tracepoint_jumps;
253   bp_link = &proc->fast_tracepoint_jumps;
254
255   while (bp)
256     {
257       if (bp == todel)
258         {
259           if (--bp->refcount == 0)
260             {
261               struct fast_tracepoint_jump *prev_bp_link = *bp_link;
262               unsigned char *buf;
263
264               /* Unlink it.  */
265               *bp_link = bp->next;
266
267               /* Since there can be breakpoints inserted in the same
268                  address range, we use `write_inferior_memory', which
269                  takes care of layering breakpoints on top of fast
270                  tracepoints, and on top of the buffer we pass it.
271                  This works because we've already unlinked the fast
272                  tracepoint jump above.  Also note that we need to
273                  pass the current shadow contents, because
274                  write_inferior_memory updates any shadow memory with
275                  what we pass here, and we want that to be a nop.  */
276               buf = alloca (bp->length);
277               memcpy (buf, fast_tracepoint_jump_shadow (bp), bp->length);
278               ret = write_inferior_memory (bp->pc, buf, bp->length);
279               if (ret != 0)
280                 {
281                   /* Something went wrong, relink the jump.  */
282                   *bp_link = prev_bp_link;
283
284                   if (debug_threads)
285                     fprintf (stderr,
286                              "Failed to uninsert fast tracepoint jump "
287                              "at 0x%s (%s) while deleting it.\n",
288                              paddress (bp->pc), strerror (ret));
289                   return ret;
290                 }
291
292               free (bp);
293             }
294
295           return 0;
296         }
297       else
298         {
299           bp_link = &bp->next;
300           bp = *bp_link;
301         }
302     }
303
304   warning ("Could not find fast tracepoint jump in list.");
305   return ENOENT;
306 }
307
308 void
309 inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump *jp)
310 {
311   jp->refcount++;
312 }
313
314 struct fast_tracepoint_jump *
315 set_fast_tracepoint_jump (CORE_ADDR where,
316                           unsigned char *insn, ULONGEST length)
317 {
318   struct process_info *proc = current_process ();
319   struct fast_tracepoint_jump *jp;
320   int err;
321   unsigned char *buf;
322
323   /* We refcount fast tracepoint jumps.  Check if we already know
324      about a jump at this address.  */
325   jp = find_fast_tracepoint_jump_at (where);
326   if (jp != NULL)
327     {
328       jp->refcount++;
329       return jp;
330     }
331
332   /* We don't, so create a new object.  Double the length, because the
333      flexible array member holds both the jump insn, and the
334      shadow.  */
335   jp = xcalloc (1, sizeof (*jp) + (length * 2));
336   jp->pc = where;
337   jp->length = length;
338   memcpy (fast_tracepoint_jump_insn (jp), insn, length);
339   jp->refcount = 1;
340   buf = alloca (length);
341
342   /* Note that there can be trap breakpoints inserted in the same
343      address range.  To access the original memory contents, we use
344      `read_inferior_memory', which masks out breakpoints.  */
345   err = read_inferior_memory (where, buf, length);
346   if (err != 0)
347     {
348       if (debug_threads)
349         fprintf (stderr,
350                  "Failed to read shadow memory of"
351                  " fast tracepoint at 0x%s (%s).\n",
352                  paddress (where), strerror (err));
353       free (jp);
354       return NULL;
355     }
356   memcpy (fast_tracepoint_jump_shadow (jp), buf, length);
357
358   /* Link the jump in.  */
359   jp->inserted = 1;
360   jp->next = proc->fast_tracepoint_jumps;
361   proc->fast_tracepoint_jumps = jp;
362
363   /* Since there can be trap breakpoints inserted in the same address
364      range, we use use `write_inferior_memory', which takes care of
365      layering breakpoints on top of fast tracepoints, on top of the
366      buffer we pass it.  This works because we've already linked in
367      the fast tracepoint jump above.  Also note that we need to pass
368      the current shadow contents, because write_inferior_memory
369      updates any shadow memory with what we pass here, and we want
370      that to be a nop.  */
371   err = write_inferior_memory (where, buf, length);
372   if (err != 0)
373     {
374       if (debug_threads)
375         fprintf (stderr,
376                  "Failed to insert fast tracepoint jump at 0x%s (%s).\n",
377                  paddress (where), strerror (err));
378
379       /* Unlink it.  */
380       proc->fast_tracepoint_jumps = jp->next;
381       free (jp);
382
383       return NULL;
384     }
385
386   return jp;
387 }
388
389 void
390 uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc)
391 {
392   struct fast_tracepoint_jump *jp;
393   int err;
394
395   jp = find_fast_tracepoint_jump_at (pc);
396   if (jp == NULL)
397     {
398       /* This can happen when we remove all breakpoints while handling
399          a step-over.  */
400       if (debug_threads)
401         fprintf (stderr,
402                  "Could not find fast tracepoint jump at 0x%s "
403                  "in list (uninserting).\n",
404                  paddress (pc));
405       return;
406     }
407
408   if (jp->inserted)
409     {
410       unsigned char *buf;
411
412       jp->inserted = 0;
413
414       /* Since there can be trap breakpoints inserted in the same
415          address range, we use use `write_inferior_memory', which
416          takes care of layering breakpoints on top of fast
417          tracepoints, and on top of the buffer we pass it.  This works
418          because we've already marked the fast tracepoint fast
419          tracepoint jump uninserted above.  Also note that we need to
420          pass the current shadow contents, because
421          write_inferior_memory updates any shadow memory with what we
422          pass here, and we want that to be a nop.  */
423       buf = alloca (jp->length);
424       memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
425       err = write_inferior_memory (jp->pc, buf, jp->length);
426       if (err != 0)
427         {
428           jp->inserted = 1;
429
430           if (debug_threads)
431             fprintf (stderr,
432                      "Failed to uninsert fast tracepoint jump at 0x%s (%s).\n",
433                      paddress (pc), strerror (err));
434         }
435     }
436 }
437
438 void
439 reinsert_fast_tracepoint_jumps_at (CORE_ADDR where)
440 {
441   struct fast_tracepoint_jump *jp;
442   int err;
443   unsigned char *buf;
444
445   jp = find_fast_tracepoint_jump_at (where);
446   if (jp == NULL)
447     {
448       /* This can happen when we remove breakpoints when a tracepoint
449          hit causes a tracing stop, while handling a step-over.  */
450       if (debug_threads)
451         fprintf (stderr,
452                  "Could not find fast tracepoint jump at 0x%s "
453                  "in list (reinserting).\n",
454                  paddress (where));
455       return;
456     }
457
458   if (jp->inserted)
459     error ("Jump already inserted at reinsert time.");
460
461   jp->inserted = 1;
462
463   /* Since there can be trap breakpoints inserted in the same address
464      range, we use `write_inferior_memory', which takes care of
465      layering breakpoints on top of fast tracepoints, and on top of
466      the buffer we pass it.  This works because we've already marked
467      the fast tracepoint jump inserted above.  Also note that we need
468      to pass the current shadow contents, because
469      write_inferior_memory updates any shadow memory with what we pass
470      here, and we want that to be a nop.  */
471   buf = alloca (jp->length);
472   memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
473   err = write_inferior_memory (where, buf, jp->length);
474   if (err != 0)
475     {
476       jp->inserted = 0;
477
478       if (debug_threads)
479         fprintf (stderr,
480                  "Failed to reinsert fast tracepoint jump at 0x%s (%s).\n",
481                  paddress (where), strerror (err));
482     }
483 }
484
485 struct breakpoint *
486 set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
487 {
488   struct process_info *proc = current_process ();
489   struct breakpoint *bp;
490   struct raw_breakpoint *raw;
491
492   raw = set_raw_breakpoint_at (where);
493
494   if (raw == NULL)
495     {
496       /* warn? */
497       return NULL;
498     }
499
500   bp = xcalloc (1, sizeof (struct breakpoint));
501   bp->type = other_breakpoint;
502
503   bp->raw = raw;
504   bp->handler = handler;
505
506   bp->next = proc->breakpoints;
507   proc->breakpoints = bp;
508
509   return bp;
510 }
511
512 static int
513 delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
514 {
515   struct raw_breakpoint *bp, **bp_link;
516   int ret;
517
518   bp = proc->raw_breakpoints;
519   bp_link = &proc->raw_breakpoints;
520
521   while (bp)
522     {
523       if (bp == todel)
524         {
525           if (bp->inserted)
526             {
527               struct raw_breakpoint *prev_bp_link = *bp_link;
528               unsigned char buf[MAX_BREAKPOINT_LEN];
529
530               *bp_link = bp->next;
531
532               /* Since there can be trap breakpoints inserted in the
533                  same address range, we use `write_inferior_memory',
534                  which takes care of layering breakpoints on top of
535                  fast tracepoints, and on top of the buffer we pass
536                  it.  This works because we've already unlinked the
537                  fast tracepoint jump above.  Also note that we need
538                  to pass the current shadow contents, because
539                  write_inferior_memory updates any shadow memory with
540                  what we pass here, and we want that to be a nop.  */
541               memcpy (buf, bp->old_data, breakpoint_len);
542               ret = write_inferior_memory (bp->pc, buf, breakpoint_len);
543               if (ret != 0)
544                 {
545                   /* Something went wrong, relink the breakpoint.  */
546                   *bp_link = prev_bp_link;
547
548                   if (debug_threads)
549                     fprintf (stderr,
550                              "Failed to uninsert raw breakpoint "
551                              "at 0x%s (%s) while deleting it.\n",
552                              paddress (bp->pc), strerror (ret));
553                   return ret;
554                 }
555
556             }
557           else
558             *bp_link = bp->next;
559
560           free (bp);
561           return 0;
562         }
563       else
564         {
565           bp_link = &bp->next;
566           bp = *bp_link;
567         }
568     }
569
570   warning ("Could not find raw breakpoint in list.");
571   return ENOENT;
572 }
573
574 static int
575 release_breakpoint (struct process_info *proc, struct breakpoint *bp)
576 {
577   int newrefcount;
578   int ret;
579
580   newrefcount = bp->raw->refcount - 1;
581   if (newrefcount == 0)
582     {
583       ret = delete_raw_breakpoint (proc, bp->raw);
584       if (ret != 0)
585         return ret;
586     }
587   else
588     bp->raw->refcount = newrefcount;
589
590   free (bp);
591
592   return 0;
593 }
594
595 static int
596 delete_breakpoint_1 (struct process_info *proc, struct breakpoint *todel)
597 {
598   struct breakpoint *bp, **bp_link;
599   int err;
600
601   bp = proc->breakpoints;
602   bp_link = &proc->breakpoints;
603
604   while (bp)
605     {
606       if (bp == todel)
607         {
608           *bp_link = bp->next;
609
610           err = release_breakpoint (proc, bp);
611           if (err != 0)
612             return err;
613
614           bp = *bp_link;
615           return 0;
616         }
617       else
618         {
619           bp_link = &bp->next;
620           bp = *bp_link;
621         }
622     }
623
624   warning ("Could not find breakpoint in list.");
625   return ENOENT;
626 }
627
628 int
629 delete_breakpoint (struct breakpoint *todel)
630 {
631   struct process_info *proc = current_process ();
632   return delete_breakpoint_1 (proc, todel);
633 }
634
635 static struct breakpoint *
636 find_gdb_breakpoint_at (CORE_ADDR where)
637 {
638   struct process_info *proc = current_process ();
639   struct breakpoint *bp;
640
641   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
642     if (bp->type == gdb_breakpoint && bp->raw->pc == where)
643       return bp;
644
645   return NULL;
646 }
647
648 int
649 set_gdb_breakpoint_at (CORE_ADDR where)
650 {
651   struct breakpoint *bp;
652
653   if (breakpoint_data == NULL)
654     return 1;
655
656   /* If we see GDB inserting a second breakpoint at the same address,
657      then the first breakpoint must have disappeared due to a shared
658      library unload.  On targets where the shared libraries are
659      handled by userspace, like SVR4, for example, GDBserver can't
660      tell if a library was loaded or unloaded.  Since we refcount
661      breakpoints, if we didn't do this, we'd just increase the
662      refcount of the previous breakpoint at this address, but the trap
663      was not planted in the inferior anymore, thus the breakpoint
664      would never be hit.  */
665   bp = find_gdb_breakpoint_at (where);
666   if (bp != NULL)
667     {
668       delete_gdb_breakpoint_at (where);
669
670       /* Might as well validate all other breakpoints.  */
671       validate_breakpoints ();
672     }
673
674   bp = set_breakpoint_at (where, NULL);
675   if (bp == NULL)
676     return -1;
677
678   bp->type = gdb_breakpoint;
679   return 0;
680 }
681
682 int
683 delete_gdb_breakpoint_at (CORE_ADDR addr)
684 {
685   struct breakpoint *bp;
686   int err;
687
688   if (breakpoint_data == NULL)
689     return 1;
690
691   bp = find_gdb_breakpoint_at (addr);
692   if (bp == NULL)
693     return -1;
694
695   err = delete_breakpoint (bp);
696   if (err)
697     return -1;
698
699   return 0;
700 }
701
702 int
703 gdb_breakpoint_here (CORE_ADDR where)
704 {
705   struct breakpoint *bp = find_gdb_breakpoint_at (where);
706
707   return (bp != NULL);
708 }
709
710 void
711 set_reinsert_breakpoint (CORE_ADDR stop_at)
712 {
713   struct breakpoint *bp;
714
715   bp = set_breakpoint_at (stop_at, NULL);
716   bp->type = reinsert_breakpoint;
717 }
718
719 void
720 delete_reinsert_breakpoints (void)
721 {
722   struct process_info *proc = current_process ();
723   struct breakpoint *bp, **bp_link;
724
725   bp = proc->breakpoints;
726   bp_link = &proc->breakpoints;
727
728   while (bp)
729     {
730       if (bp->type == reinsert_breakpoint)
731         {
732           *bp_link = bp->next;
733           release_breakpoint (proc, bp);
734           bp = *bp_link;
735         }
736       else
737         {
738           bp_link = &bp->next;
739           bp = *bp_link;
740         }
741     }
742 }
743
744 static void
745 uninsert_raw_breakpoint (struct raw_breakpoint *bp)
746 {
747   if (bp->inserted)
748     {
749       int err;
750       unsigned char buf[MAX_BREAKPOINT_LEN];
751
752       bp->inserted = 0;
753       /* Since there can be fast tracepoint jumps inserted in the same
754          address range, we use `write_inferior_memory', which takes
755          care of layering breakpoints on top of fast tracepoints, and
756          on top of the buffer we pass it.  This works because we've
757          already unlinked the fast tracepoint jump above.  Also note
758          that we need to pass the current shadow contents, because
759          write_inferior_memory updates any shadow memory with what we
760          pass here, and we want that to be a nop.  */
761       memcpy (buf, bp->old_data, breakpoint_len);
762       err = write_inferior_memory (bp->pc, buf, breakpoint_len);
763       if (err != 0)
764         {
765           bp->inserted = 1;
766
767           if (debug_threads)
768             fprintf (stderr,
769                      "Failed to uninsert raw breakpoint at 0x%s (%s).\n",
770                      paddress (bp->pc), strerror (err));
771         }
772     }
773 }
774
775 void
776 uninsert_breakpoints_at (CORE_ADDR pc)
777 {
778   struct raw_breakpoint *bp;
779
780   bp = find_raw_breakpoint_at (pc);
781   if (bp == NULL)
782     {
783       /* This can happen when we remove all breakpoints while handling
784          a step-over.  */
785       if (debug_threads)
786         fprintf (stderr,
787                  "Could not find breakpoint at 0x%s "
788                  "in list (uninserting).\n",
789                  paddress (pc));
790       return;
791     }
792
793   if (bp->inserted)
794     uninsert_raw_breakpoint (bp);
795 }
796
797 void
798 uninsert_all_breakpoints (void)
799 {
800   struct process_info *proc = current_process ();
801   struct raw_breakpoint *bp;
802
803   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
804     if (bp->inserted)
805       uninsert_raw_breakpoint (bp);
806 }
807
808 static void
809 reinsert_raw_breakpoint (struct raw_breakpoint *bp)
810 {
811   int err;
812
813   if (bp->inserted)
814     error ("Breakpoint already inserted at reinsert time.");
815
816   err = (*the_target->write_memory) (bp->pc, breakpoint_data,
817                                      breakpoint_len);
818   if (err == 0)
819     bp->inserted = 1;
820   else if (debug_threads)
821     fprintf (stderr,
822              "Failed to reinsert breakpoint at 0x%s (%s).\n",
823              paddress (bp->pc), strerror (err));
824 }
825
826 void
827 reinsert_breakpoints_at (CORE_ADDR pc)
828 {
829   struct raw_breakpoint *bp;
830
831   bp = find_raw_breakpoint_at (pc);
832   if (bp == NULL)
833     {
834       /* This can happen when we remove all breakpoints while handling
835          a step-over.  */
836       if (debug_threads)
837         fprintf (stderr,
838                  "Could not find raw breakpoint at 0x%s "
839                  "in list (reinserting).\n",
840                  paddress (pc));
841       return;
842     }
843
844   reinsert_raw_breakpoint (bp);
845 }
846
847 void
848 reinsert_all_breakpoints (void)
849 {
850   struct process_info *proc = current_process ();
851   struct raw_breakpoint *bp;
852
853   for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
854     if (!bp->inserted)
855       reinsert_raw_breakpoint (bp);
856 }
857
858 void
859 check_breakpoints (CORE_ADDR stop_pc)
860 {
861   struct process_info *proc = current_process ();
862   struct breakpoint *bp, **bp_link;
863
864   bp = proc->breakpoints;
865   bp_link = &proc->breakpoints;
866
867   while (bp)
868     {
869       if (bp->raw->pc == stop_pc)
870         {
871           if (!bp->raw->inserted)
872             {
873               warning ("Hit a removed breakpoint?");
874               return;
875             }
876
877           if (bp->handler != NULL && (*bp->handler) (stop_pc))
878             {
879               *bp_link = bp->next;
880
881               release_breakpoint (proc, bp);
882
883               bp = *bp_link;
884               continue;
885             }
886         }
887
888       bp_link = &bp->next;
889       bp = *bp_link;
890     }
891 }
892
893 void
894 set_breakpoint_data (const unsigned char *bp_data, int bp_len)
895 {
896   breakpoint_data = bp_data;
897   breakpoint_len = bp_len;
898 }
899
900 int
901 breakpoint_here (CORE_ADDR addr)
902 {
903   return (find_raw_breakpoint_at (addr) != NULL);
904 }
905
906 int
907 breakpoint_inserted_here (CORE_ADDR addr)
908 {
909   struct raw_breakpoint *bp;
910
911   bp = find_raw_breakpoint_at (addr);
912
913   return (bp != NULL && bp->inserted);
914 }
915
916 static int
917 validate_inserted_breakpoint (struct raw_breakpoint *bp)
918 {
919   unsigned char *buf;
920   int err;
921
922   gdb_assert (bp->inserted);
923
924   buf = alloca (breakpoint_len);
925   err = (*the_target->read_memory) (bp->pc, buf, breakpoint_len);
926   if (err || memcmp (buf, breakpoint_data, breakpoint_len) != 0)
927     {
928       /* Tag it as gone.  */
929       bp->inserted = 0;
930       bp->shlib_disabled = 1;
931       return 0;
932     }
933
934   return 1;
935 }
936
937 static void
938 delete_disabled_breakpoints (void)
939 {
940   struct process_info *proc = current_process ();
941   struct breakpoint *bp, *next;
942
943   for (bp = proc->breakpoints; bp != NULL; bp = next)
944     {
945       next = bp->next;
946       if (bp->raw->shlib_disabled)
947         delete_breakpoint_1 (proc, bp);
948     }
949 }
950
951 /* Check if breakpoints we inserted still appear to be inserted.  They
952    may disappear due to a shared library unload, and worse, a new
953    shared library may be reloaded at the same address as the
954    previously unloaded one.  If that happens, we should make sure that
955    the shadow memory of the old breakpoints isn't used when reading or
956    writing memory.  */
957
958 void
959 validate_breakpoints (void)
960 {
961   struct process_info *proc = current_process ();
962   struct breakpoint *bp;
963
964   for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
965     {
966       if (bp->raw->inserted)
967         validate_inserted_breakpoint (bp->raw);
968     }
969
970   delete_disabled_breakpoints ();
971 }
972
973 void
974 check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
975 {
976   struct process_info *proc = current_process ();
977   struct raw_breakpoint *bp = proc->raw_breakpoints;
978   struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
979   CORE_ADDR mem_end = mem_addr + mem_len;
980   int disabled_one = 0;
981
982   for (; jp != NULL; jp = jp->next)
983     {
984       CORE_ADDR bp_end = jp->pc + jp->length;
985       CORE_ADDR start, end;
986       int copy_offset, copy_len, buf_offset;
987
988       gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len
989                   || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
990
991       if (mem_addr >= bp_end)
992         continue;
993       if (jp->pc >= mem_end)
994         continue;
995
996       start = jp->pc;
997       if (mem_addr > start)
998         start = mem_addr;
999
1000       end = bp_end;
1001       if (end > mem_end)
1002         end = mem_end;
1003
1004       copy_len = end - start;
1005       copy_offset = start - jp->pc;
1006       buf_offset = start - mem_addr;
1007
1008       if (jp->inserted)
1009         memcpy (buf + buf_offset,
1010                 fast_tracepoint_jump_shadow (jp) + copy_offset,
1011                 copy_len);
1012     }
1013
1014   for (; bp != NULL; bp = bp->next)
1015     {
1016       CORE_ADDR bp_end = bp->pc + breakpoint_len;
1017       CORE_ADDR start, end;
1018       int copy_offset, copy_len, buf_offset;
1019
1020       gdb_assert (bp->old_data >= buf + mem_len
1021                   || buf >= &bp->old_data[sizeof (bp->old_data)]);
1022
1023       if (mem_addr >= bp_end)
1024         continue;
1025       if (bp->pc >= mem_end)
1026         continue;
1027
1028       start = bp->pc;
1029       if (mem_addr > start)
1030         start = mem_addr;
1031
1032       end = bp_end;
1033       if (end > mem_end)
1034         end = mem_end;
1035
1036       copy_len = end - start;
1037       copy_offset = start - bp->pc;
1038       buf_offset = start - mem_addr;
1039
1040       if (bp->inserted)
1041         {
1042           if (validate_inserted_breakpoint (bp))
1043             memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
1044           else
1045             disabled_one = 1;
1046         }
1047     }
1048
1049   if (disabled_one)
1050     delete_disabled_breakpoints ();
1051 }
1052
1053 void
1054 check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
1055                  const unsigned char *myaddr, int mem_len)
1056 {
1057   struct process_info *proc = current_process ();
1058   struct raw_breakpoint *bp = proc->raw_breakpoints;
1059   struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1060   CORE_ADDR mem_end = mem_addr + mem_len;
1061   int disabled_one = 0;
1062
1063   /* First fast tracepoint jumps, then breakpoint traps on top.  */
1064
1065   for (; jp != NULL; jp = jp->next)
1066     {
1067       CORE_ADDR jp_end = jp->pc + jp->length;
1068       CORE_ADDR start, end;
1069       int copy_offset, copy_len, buf_offset;
1070
1071       gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len
1072                   || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1073       gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len
1074                   || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length);
1075
1076       if (mem_addr >= jp_end)
1077         continue;
1078       if (jp->pc >= mem_end)
1079         continue;
1080
1081       start = jp->pc;
1082       if (mem_addr > start)
1083         start = mem_addr;
1084
1085       end = jp_end;
1086       if (end > mem_end)
1087         end = mem_end;
1088
1089       copy_len = end - start;
1090       copy_offset = start - jp->pc;
1091       buf_offset = start - mem_addr;
1092
1093       memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset,
1094               myaddr + buf_offset, copy_len);
1095       if (jp->inserted)
1096         memcpy (buf + buf_offset,
1097                 fast_tracepoint_jump_insn (jp) + copy_offset, copy_len);
1098     }
1099
1100   for (; bp != NULL; bp = bp->next)
1101     {
1102       CORE_ADDR bp_end = bp->pc + breakpoint_len;
1103       CORE_ADDR start, end;
1104       int copy_offset, copy_len, buf_offset;
1105
1106       gdb_assert (bp->old_data >= myaddr + mem_len
1107                   || myaddr >= &bp->old_data[sizeof (bp->old_data)]);
1108
1109       if (mem_addr >= bp_end)
1110         continue;
1111       if (bp->pc >= mem_end)
1112         continue;
1113
1114       start = bp->pc;
1115       if (mem_addr > start)
1116         start = mem_addr;
1117
1118       end = bp_end;
1119       if (end > mem_end)
1120         end = mem_end;
1121
1122       copy_len = end - start;
1123       copy_offset = start - bp->pc;
1124       buf_offset = start - mem_addr;
1125
1126       memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len);
1127       if (bp->inserted)
1128         {
1129           if (validate_inserted_breakpoint (bp))
1130             memcpy (buf + buf_offset, breakpoint_data + copy_offset, copy_len);
1131           else
1132             disabled_one = 1;
1133         }
1134     }
1135
1136   if (disabled_one)
1137     delete_disabled_breakpoints ();
1138 }
1139
1140 /* Delete all breakpoints, and un-insert them from the inferior.  */
1141
1142 void
1143 delete_all_breakpoints (void)
1144 {
1145   struct process_info *proc = current_process ();
1146
1147   while (proc->breakpoints)
1148     delete_breakpoint_1 (proc, proc->breakpoints);
1149 }
1150
1151 /* Clear the "inserted" flag in all breakpoints.  */
1152
1153 void
1154 mark_breakpoints_out (struct process_info *proc)
1155 {
1156   struct raw_breakpoint *raw_bp;
1157
1158   for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next)
1159     raw_bp->inserted = 0;
1160 }
1161
1162 /* Release all breakpoints, but do not try to un-insert them from the
1163    inferior.  */
1164
1165 void
1166 free_all_breakpoints (struct process_info *proc)
1167 {
1168   mark_breakpoints_out (proc);
1169
1170   /* Note: use PROC explicitly instead of deferring to
1171      delete_all_breakpoints --- CURRENT_INFERIOR may already have been
1172      released when we get here.  There should be no call to
1173      current_process from here on.  */
1174   while (proc->breakpoints)
1175     delete_breakpoint_1 (proc, proc->breakpoints);
1176 }