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