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