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