]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/mem-break.c
Update years in copyright notice for the GDB files.
[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
844 struct regcache *regcache = get_thread_regcache (current_inferior, 1);
845
846 if (bp == NULL)
847 return 0;
848
849 /* Check if the breakpoint is unconditional. If it is,
850 the condition always evaluates to TRUE. */
851 if (bp->cond_list == NULL)
852 return 1;
853
854 /* Evaluate each condition in the breakpoint's list of conditions.
855 Return true if any of the conditions evaluates to TRUE.
856
857 If we failed to evaluate the expression, TRUE is returned. This
858 forces GDB to reevaluate the conditions. */
859 for (cl = bp->cond_list;
860 cl && !value && !err; cl = cl->next)
861 {
862 /* Evaluate the condition. */
863 err = gdb_eval_agent_expr (regcache, NULL, cl->cond, &value);
864 }
865
866 if (err)
867 return 1;
868
869 return (value != 0);
870 }
871
872 /* Add commands COMMANDS to GDBserver's breakpoint BP. */
873
874 void
875 add_commands_to_breakpoint (struct breakpoint *bp,
876 struct agent_expr *commands, int persist)
877 {
878 struct point_command_list *new_cmd;
879
880 /* Create new command. */
881 new_cmd = xcalloc (1, sizeof (*new_cmd));
882 new_cmd->cmd = commands;
883 new_cmd->persistence = persist;
884
885 /* Add commands to the list. */
886 new_cmd->next = bp->command_list;
887 bp->command_list = new_cmd;
888 }
889
890 /* Add a target-side command COMMAND to the breakpoint at ADDR. */
891
892 int
893 add_breakpoint_commands (CORE_ADDR addr, char **command, int persist)
894 {
895 struct breakpoint *bp = find_gdb_breakpoint_at (addr);
896 char *actparm = *command;
897 struct agent_expr *cmd;
898
899 if (bp == NULL)
900 return 1;
901
902 if (command == NULL)
903 return 1;
904
905 cmd = gdb_parse_agent_expr (&actparm);
906
907 if (cmd == NULL)
908 {
909 fprintf (stderr, "Command evaluation failed. "
910 "Disabling.\n");
911 return 0;
912 }
913
914 add_commands_to_breakpoint (bp, cmd, persist);
915
916 *command = actparm;
917
918 return 0;
919 }
920
921 /* Return true if there are no commands to run at this location,
922 which likely means we want to report back to GDB. */
923 int
924 gdb_no_commands_at_breakpoint (CORE_ADDR where)
925 {
926 struct breakpoint *bp = find_gdb_breakpoint_at (where);
927
928 if (bp == NULL)
929 return 0;
930
931 if (debug_threads)
932 fprintf (stderr, "at 0x%s, bp command_list is 0x%s\n",
933 paddress (where),
934 phex_nz ((uintptr_t) bp->command_list, 0));
935 return (bp->command_list == NULL);
936 }
937
938 void
939 run_breakpoint_commands (CORE_ADDR where)
940 {
941 /* Fetch registers for the current inferior. */
942 struct breakpoint *bp = find_gdb_breakpoint_at (where);
943 ULONGEST value = 0;
944 struct point_command_list *cl;
945 int err = 0;
946
947 struct regcache *regcache = get_thread_regcache (current_inferior, 1);
948
949 if (bp == NULL)
950 return;
951
952 for (cl = bp->command_list;
953 cl && !value && !err; cl = cl->next)
954 {
955 /* Run the command. */
956 err = gdb_eval_agent_expr (regcache, NULL, cl->cmd, &value);
957
958 /* If one command has a problem, stop digging the hole deeper. */
959 if (err)
960 break;
961 }
962 }
963
964 /* Return 1 if there is a breakpoint inserted in address WHERE
965 and if its condition, if it exists, is true. */
966
967 int
968 gdb_breakpoint_here (CORE_ADDR where)
969 {
970 return (find_gdb_breakpoint_at (where) != NULL);
971 }
972
973 void
974 set_reinsert_breakpoint (CORE_ADDR stop_at)
975 {
976 struct breakpoint *bp;
977
978 bp = set_breakpoint_at (stop_at, NULL);
979 bp->type = reinsert_breakpoint;
980 }
981
982 void
983 delete_reinsert_breakpoints (void)
984 {
985 struct process_info *proc = current_process ();
986 struct breakpoint *bp, **bp_link;
987
988 bp = proc->breakpoints;
989 bp_link = &proc->breakpoints;
990
991 while (bp)
992 {
993 if (bp->type == reinsert_breakpoint)
994 {
995 *bp_link = bp->next;
996 release_breakpoint (proc, bp);
997 bp = *bp_link;
998 }
999 else
1000 {
1001 bp_link = &bp->next;
1002 bp = *bp_link;
1003 }
1004 }
1005 }
1006
1007 static void
1008 uninsert_raw_breakpoint (struct raw_breakpoint *bp)
1009 {
1010 if (bp->inserted)
1011 {
1012 int err;
1013 unsigned char buf[MAX_BREAKPOINT_LEN];
1014
1015 bp->inserted = 0;
1016 /* Since there can be fast tracepoint jumps inserted in the same
1017 address range, we use `write_inferior_memory', which takes
1018 care of layering breakpoints on top of fast tracepoints, and
1019 on top of the buffer we pass it. This works because we've
1020 already unlinked the fast tracepoint jump above. Also note
1021 that we need to pass the current shadow contents, because
1022 write_inferior_memory updates any shadow memory with what we
1023 pass here, and we want that to be a nop. */
1024 memcpy (buf, bp->old_data, breakpoint_len);
1025 err = write_inferior_memory (bp->pc, buf, breakpoint_len);
1026 if (err != 0)
1027 {
1028 bp->inserted = 1;
1029
1030 if (debug_threads)
1031 fprintf (stderr,
1032 "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 fprintf (stderr,
1050 "Could not find breakpoint at 0x%s "
1051 "in list (uninserting).\n",
1052 paddress (pc));
1053 return;
1054 }
1055
1056 if (bp->inserted)
1057 uninsert_raw_breakpoint (bp);
1058 }
1059
1060 void
1061 uninsert_all_breakpoints (void)
1062 {
1063 struct process_info *proc = current_process ();
1064 struct raw_breakpoint *bp;
1065
1066 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1067 if (bp->inserted)
1068 uninsert_raw_breakpoint (bp);
1069 }
1070
1071 static void
1072 reinsert_raw_breakpoint (struct raw_breakpoint *bp)
1073 {
1074 int err;
1075
1076 if (bp->inserted)
1077 error ("Breakpoint already inserted at reinsert time.");
1078
1079 err = (*the_target->write_memory) (bp->pc, breakpoint_data,
1080 breakpoint_len);
1081 if (err == 0)
1082 bp->inserted = 1;
1083 else if (debug_threads)
1084 fprintf (stderr,
1085 "Failed to reinsert breakpoint at 0x%s (%s).\n",
1086 paddress (bp->pc), strerror (err));
1087 }
1088
1089 void
1090 reinsert_breakpoints_at (CORE_ADDR pc)
1091 {
1092 struct raw_breakpoint *bp;
1093
1094 bp = find_raw_breakpoint_at (pc);
1095 if (bp == NULL)
1096 {
1097 /* This can happen when we remove all breakpoints while handling
1098 a step-over. */
1099 if (debug_threads)
1100 fprintf (stderr,
1101 "Could not find raw breakpoint at 0x%s "
1102 "in list (reinserting).\n",
1103 paddress (pc));
1104 return;
1105 }
1106
1107 reinsert_raw_breakpoint (bp);
1108 }
1109
1110 void
1111 reinsert_all_breakpoints (void)
1112 {
1113 struct process_info *proc = current_process ();
1114 struct raw_breakpoint *bp;
1115
1116 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1117 if (!bp->inserted)
1118 reinsert_raw_breakpoint (bp);
1119 }
1120
1121 void
1122 check_breakpoints (CORE_ADDR stop_pc)
1123 {
1124 struct process_info *proc = current_process ();
1125 struct breakpoint *bp, **bp_link;
1126
1127 bp = proc->breakpoints;
1128 bp_link = &proc->breakpoints;
1129
1130 while (bp)
1131 {
1132 if (bp->raw->pc == stop_pc)
1133 {
1134 if (!bp->raw->inserted)
1135 {
1136 warning ("Hit a removed breakpoint?");
1137 return;
1138 }
1139
1140 if (bp->handler != NULL && (*bp->handler) (stop_pc))
1141 {
1142 *bp_link = bp->next;
1143
1144 release_breakpoint (proc, bp);
1145
1146 bp = *bp_link;
1147 continue;
1148 }
1149 }
1150
1151 bp_link = &bp->next;
1152 bp = *bp_link;
1153 }
1154 }
1155
1156 void
1157 set_breakpoint_data (const unsigned char *bp_data, int bp_len)
1158 {
1159 breakpoint_data = bp_data;
1160 breakpoint_len = bp_len;
1161 }
1162
1163 int
1164 breakpoint_here (CORE_ADDR addr)
1165 {
1166 return (find_raw_breakpoint_at (addr) != NULL);
1167 }
1168
1169 int
1170 breakpoint_inserted_here (CORE_ADDR addr)
1171 {
1172 struct raw_breakpoint *bp;
1173
1174 bp = find_raw_breakpoint_at (addr);
1175
1176 return (bp != NULL && bp->inserted);
1177 }
1178
1179 static int
1180 validate_inserted_breakpoint (struct raw_breakpoint *bp)
1181 {
1182 unsigned char *buf;
1183 int err;
1184
1185 gdb_assert (bp->inserted);
1186
1187 buf = alloca (breakpoint_len);
1188 err = (*the_target->read_memory) (bp->pc, buf, breakpoint_len);
1189 if (err || memcmp (buf, breakpoint_data, breakpoint_len) != 0)
1190 {
1191 /* Tag it as gone. */
1192 bp->inserted = 0;
1193 bp->shlib_disabled = 1;
1194 return 0;
1195 }
1196
1197 return 1;
1198 }
1199
1200 static void
1201 delete_disabled_breakpoints (void)
1202 {
1203 struct process_info *proc = current_process ();
1204 struct breakpoint *bp, *next;
1205
1206 for (bp = proc->breakpoints; bp != NULL; bp = next)
1207 {
1208 next = bp->next;
1209 if (bp->raw->shlib_disabled)
1210 delete_breakpoint_1 (proc, bp);
1211 }
1212 }
1213
1214 /* Check if breakpoints we inserted still appear to be inserted. They
1215 may disappear due to a shared library unload, and worse, a new
1216 shared library may be reloaded at the same address as the
1217 previously unloaded one. If that happens, we should make sure that
1218 the shadow memory of the old breakpoints isn't used when reading or
1219 writing memory. */
1220
1221 void
1222 validate_breakpoints (void)
1223 {
1224 struct process_info *proc = current_process ();
1225 struct breakpoint *bp;
1226
1227 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1228 {
1229 if (bp->raw->inserted)
1230 validate_inserted_breakpoint (bp->raw);
1231 }
1232
1233 delete_disabled_breakpoints ();
1234 }
1235
1236 void
1237 check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
1238 {
1239 struct process_info *proc = current_process ();
1240 struct raw_breakpoint *bp = proc->raw_breakpoints;
1241 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1242 CORE_ADDR mem_end = mem_addr + mem_len;
1243 int disabled_one = 0;
1244
1245 for (; jp != NULL; jp = jp->next)
1246 {
1247 CORE_ADDR bp_end = jp->pc + jp->length;
1248 CORE_ADDR start, end;
1249 int copy_offset, copy_len, buf_offset;
1250
1251 gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len
1252 || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1253
1254 if (mem_addr >= bp_end)
1255 continue;
1256 if (jp->pc >= mem_end)
1257 continue;
1258
1259 start = jp->pc;
1260 if (mem_addr > start)
1261 start = mem_addr;
1262
1263 end = bp_end;
1264 if (end > mem_end)
1265 end = mem_end;
1266
1267 copy_len = end - start;
1268 copy_offset = start - jp->pc;
1269 buf_offset = start - mem_addr;
1270
1271 if (jp->inserted)
1272 memcpy (buf + buf_offset,
1273 fast_tracepoint_jump_shadow (jp) + copy_offset,
1274 copy_len);
1275 }
1276
1277 for (; bp != NULL; bp = bp->next)
1278 {
1279 CORE_ADDR bp_end = bp->pc + breakpoint_len;
1280 CORE_ADDR start, end;
1281 int copy_offset, copy_len, buf_offset;
1282
1283 gdb_assert (bp->old_data >= buf + mem_len
1284 || buf >= &bp->old_data[sizeof (bp->old_data)]);
1285
1286 if (mem_addr >= bp_end)
1287 continue;
1288 if (bp->pc >= mem_end)
1289 continue;
1290
1291 start = bp->pc;
1292 if (mem_addr > start)
1293 start = mem_addr;
1294
1295 end = bp_end;
1296 if (end > mem_end)
1297 end = mem_end;
1298
1299 copy_len = end - start;
1300 copy_offset = start - bp->pc;
1301 buf_offset = start - mem_addr;
1302
1303 if (bp->inserted)
1304 {
1305 if (validate_inserted_breakpoint (bp))
1306 memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
1307 else
1308 disabled_one = 1;
1309 }
1310 }
1311
1312 if (disabled_one)
1313 delete_disabled_breakpoints ();
1314 }
1315
1316 void
1317 check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
1318 const unsigned char *myaddr, int mem_len)
1319 {
1320 struct process_info *proc = current_process ();
1321 struct raw_breakpoint *bp = proc->raw_breakpoints;
1322 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1323 CORE_ADDR mem_end = mem_addr + mem_len;
1324 int disabled_one = 0;
1325
1326 /* First fast tracepoint jumps, then breakpoint traps on top. */
1327
1328 for (; jp != NULL; jp = jp->next)
1329 {
1330 CORE_ADDR jp_end = jp->pc + jp->length;
1331 CORE_ADDR start, end;
1332 int copy_offset, copy_len, buf_offset;
1333
1334 gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len
1335 || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1336 gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len
1337 || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length);
1338
1339 if (mem_addr >= jp_end)
1340 continue;
1341 if (jp->pc >= mem_end)
1342 continue;
1343
1344 start = jp->pc;
1345 if (mem_addr > start)
1346 start = mem_addr;
1347
1348 end = jp_end;
1349 if (end > mem_end)
1350 end = mem_end;
1351
1352 copy_len = end - start;
1353 copy_offset = start - jp->pc;
1354 buf_offset = start - mem_addr;
1355
1356 memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset,
1357 myaddr + buf_offset, copy_len);
1358 if (jp->inserted)
1359 memcpy (buf + buf_offset,
1360 fast_tracepoint_jump_insn (jp) + copy_offset, copy_len);
1361 }
1362
1363 for (; bp != NULL; bp = bp->next)
1364 {
1365 CORE_ADDR bp_end = bp->pc + breakpoint_len;
1366 CORE_ADDR start, end;
1367 int copy_offset, copy_len, buf_offset;
1368
1369 gdb_assert (bp->old_data >= myaddr + mem_len
1370 || myaddr >= &bp->old_data[sizeof (bp->old_data)]);
1371
1372 if (mem_addr >= bp_end)
1373 continue;
1374 if (bp->pc >= mem_end)
1375 continue;
1376
1377 start = bp->pc;
1378 if (mem_addr > start)
1379 start = mem_addr;
1380
1381 end = bp_end;
1382 if (end > mem_end)
1383 end = mem_end;
1384
1385 copy_len = end - start;
1386 copy_offset = start - bp->pc;
1387 buf_offset = start - mem_addr;
1388
1389 memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len);
1390 if (bp->inserted)
1391 {
1392 if (validate_inserted_breakpoint (bp))
1393 memcpy (buf + buf_offset, breakpoint_data + copy_offset, copy_len);
1394 else
1395 disabled_one = 1;
1396 }
1397 }
1398
1399 if (disabled_one)
1400 delete_disabled_breakpoints ();
1401 }
1402
1403 /* Delete all breakpoints, and un-insert them from the inferior. */
1404
1405 void
1406 delete_all_breakpoints (void)
1407 {
1408 struct process_info *proc = current_process ();
1409
1410 while (proc->breakpoints)
1411 delete_breakpoint_1 (proc, proc->breakpoints);
1412 }
1413
1414 /* Clear the "inserted" flag in all breakpoints. */
1415
1416 void
1417 mark_breakpoints_out (struct process_info *proc)
1418 {
1419 struct raw_breakpoint *raw_bp;
1420
1421 for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next)
1422 raw_bp->inserted = 0;
1423 }
1424
1425 /* Release all breakpoints, but do not try to un-insert them from the
1426 inferior. */
1427
1428 void
1429 free_all_breakpoints (struct process_info *proc)
1430 {
1431 mark_breakpoints_out (proc);
1432
1433 /* Note: use PROC explicitly instead of deferring to
1434 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
1435 released when we get here. There should be no call to
1436 current_process from here on. */
1437 while (proc->breakpoints)
1438 delete_breakpoint_1 (proc, proc->breakpoints);
1439 }