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