* mem-break.c (gdb_no_commands_at_breakpoint): Fix cast from
[deliverable/binutils-gdb.git] / gdb / gdbserver / mem-break.c
... / ...
CommitLineData
1/* Memory breakpoint operations for the remote server for GDB.
2 Copyright (C) 2002-2003, 2005, 2007-2012 Free Software Foundation,
3 Inc.
4
5 Contributed by MontaVista Software.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22#include "server.h"
23#include "regcache.h"
24#include "ax.h"
25#include <stdint.h>
26
27const unsigned char *breakpoint_data;
28int breakpoint_len;
29
30#define MAX_BREAKPOINT_LEN 8
31
32/* GDB will never try to install multiple breakpoints at the same
33 address. But, we need to keep track of internal breakpoints too,
34 and so we do need to be able to install multiple breakpoints at the
35 same address transparently. We keep track of two different, and
36 closely related structures. A raw breakpoint, which manages the
37 low level, close to the metal aspect of a breakpoint. It holds the
38 breakpoint address, and a buffer holding a copy of the instructions
39 that would be in memory had not been a breakpoint there (we call
40 that the shadow memory of the breakpoint). We occasionally need to
41 temporarilly uninsert a breakpoint without the client knowing about
42 it (e.g., to step over an internal breakpoint), so we keep an
43 `inserted' state associated with this low level breakpoint
44 structure. There can only be one such object for a given address.
45 Then, we have (a bit higher level) breakpoints. This structure
46 holds a callback to be called whenever a breakpoint is hit, a
47 high-level type, and a link to a low level raw breakpoint. There
48 can be many high-level breakpoints at the same address, and all of
49 them will point to the same raw breakpoint, which is reference
50 counted. */
51
52/* The low level, physical, raw breakpoint. */
53struct raw_breakpoint
54{
55 struct raw_breakpoint *next;
56
57 /* A reference count. Each high level breakpoint referencing this
58 raw breakpoint accounts for one reference. */
59 int refcount;
60
61 /* The breakpoint's insertion address. There can only be one raw
62 breakpoint for a given PC. */
63 CORE_ADDR pc;
64
65 /* The breakpoint's shadow memory. */
66 unsigned char old_data[MAX_BREAKPOINT_LEN];
67
68 /* Non-zero if this breakpoint is currently inserted in the
69 inferior. */
70 int inserted;
71
72 /* Non-zero if this breakpoint is currently disabled because we no
73 longer detect it as inserted. */
74 int shlib_disabled;
75};
76
77/* The type of a breakpoint. */
78enum bkpt_type
79 {
80 /* A GDB breakpoint, requested with a Z0 packet. */
81 gdb_breakpoint,
82
83 /* A basic-software-single-step breakpoint. */
84 reinsert_breakpoint,
85
86 /* Any other breakpoint type that doesn't require specific
87 treatment goes here. E.g., an event breakpoint. */
88 other_breakpoint,
89 };
90
91struct point_cond_list
92{
93 /* Pointer to the agent expression that is the breakpoint's
94 conditional. */
95 struct agent_expr *cond;
96
97 /* Pointer to the next condition. */
98 struct point_cond_list *next;
99};
100
101struct point_command_list
102{
103 /* Pointer to the agent expression that is the breakpoint's
104 commands. */
105 struct agent_expr *cmd;
106
107 /* Flag that is true if this command should run even while GDB is
108 disconnected. */
109 int persistence;
110
111 /* Pointer to the next command. */
112 struct point_command_list *next;
113};
114
115/* A high level (in gdbserver's perspective) breakpoint. */
116struct breakpoint
117{
118 struct breakpoint *next;
119
120 /* The breakpoint's type. */
121 enum bkpt_type type;
122
123 /* Pointer to the condition list that should be evaluated on
124 the target or NULL if the breakpoint is unconditional or
125 if GDB doesn't want us to evaluate the conditionals on the
126 target's side. */
127 struct point_cond_list *cond_list;
128
129 /* Point to the list of commands to run when this is hit. */
130 struct point_command_list *command_list;
131
132 /* Link to this breakpoint's raw breakpoint. This is always
133 non-NULL. */
134 struct raw_breakpoint *raw;
135
136 /* Function to call when we hit this breakpoint. If it returns 1,
137 the breakpoint shall be deleted; 0 or if this callback is NULL,
138 it will be left inserted. */
139 int (*handler) (CORE_ADDR);
140};
141
142int
143any_persistent_commands ()
144{
145 struct process_info *proc = current_process ();
146 struct breakpoint *bp;
147 struct point_command_list *cl;
148
149 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
150 {
151 for (cl = bp->command_list; cl != NULL; cl = cl->next)
152 if (cl->persistence)
153 return 1;
154 }
155
156 return 0;
157}
158
159static struct raw_breakpoint *
160find_raw_breakpoint_at (CORE_ADDR where)
161{
162 struct process_info *proc = current_process ();
163 struct raw_breakpoint *bp;
164
165 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
166 if (bp->pc == where)
167 return bp;
168
169 return NULL;
170}
171
172static struct raw_breakpoint *
173set_raw_breakpoint_at (CORE_ADDR where)
174{
175 struct process_info *proc = current_process ();
176 struct raw_breakpoint *bp;
177 int err;
178 unsigned char buf[MAX_BREAKPOINT_LEN];
179
180 if (breakpoint_data == NULL)
181 error ("Target does not support breakpoints.");
182
183 bp = find_raw_breakpoint_at (where);
184 if (bp != NULL)
185 {
186 bp->refcount++;
187 return bp;
188 }
189
190 bp = xcalloc (1, sizeof (*bp));
191 bp->pc = where;
192 bp->refcount = 1;
193
194 /* Note that there can be fast tracepoint jumps installed in the
195 same memory range, so to get at the original memory, we need to
196 use read_inferior_memory, which masks those out. */
197 err = read_inferior_memory (where, buf, breakpoint_len);
198 if (err != 0)
199 {
200 if (debug_threads)
201 fprintf (stderr,
202 "Failed to read shadow memory of"
203 " breakpoint at 0x%s (%s).\n",
204 paddress (where), strerror (err));
205 free (bp);
206 return NULL;
207 }
208 memcpy (bp->old_data, buf, breakpoint_len);
209
210 err = (*the_target->write_memory) (where, breakpoint_data,
211 breakpoint_len);
212 if (err != 0)
213 {
214 if (debug_threads)
215 fprintf (stderr,
216 "Failed to insert breakpoint at 0x%s (%s).\n",
217 paddress (where), strerror (err));
218 free (bp);
219 return NULL;
220 }
221
222 /* Link the breakpoint in. */
223 bp->inserted = 1;
224 bp->next = proc->raw_breakpoints;
225 proc->raw_breakpoints = bp;
226 return bp;
227}
228
229/* Notice that breakpoint traps are always installed on top of fast
230 tracepoint jumps. This is even if the fast tracepoint is installed
231 at a later time compared to when the breakpoint was installed.
232 This means that a stopping breakpoint or tracepoint has higher
233 "priority". In turn, this allows having fast and slow tracepoints
234 (and breakpoints) at the same address behave correctly. */
235
236
237/* A fast tracepoint jump. */
238
239struct fast_tracepoint_jump
240{
241 struct fast_tracepoint_jump *next;
242
243 /* A reference count. GDB can install more than one fast tracepoint
244 at the same address (each with its own action list, for
245 example). */
246 int refcount;
247
248 /* The fast tracepoint's insertion address. There can only be one
249 of these for a given PC. */
250 CORE_ADDR pc;
251
252 /* Non-zero if this fast tracepoint jump is currently inserted in
253 the inferior. */
254 int inserted;
255
256 /* The length of the jump instruction. */
257 int length;
258
259 /* A poor-man's flexible array member, holding both the jump
260 instruction to insert, and a copy of the instruction that would
261 be in memory had not been a jump there (the shadow memory of the
262 tracepoint jump). */
263 unsigned char insn_and_shadow[0];
264};
265
266/* Fast tracepoint FP's jump instruction to insert. */
267#define fast_tracepoint_jump_insn(fp) \
268 ((fp)->insn_and_shadow + 0)
269
270/* The shadow memory of fast tracepoint jump FP. */
271#define fast_tracepoint_jump_shadow(fp) \
272 ((fp)->insn_and_shadow + (fp)->length)
273
274
275/* Return the fast tracepoint jump set at WHERE. */
276
277static struct fast_tracepoint_jump *
278find_fast_tracepoint_jump_at (CORE_ADDR where)
279{
280 struct process_info *proc = current_process ();
281 struct fast_tracepoint_jump *jp;
282
283 for (jp = proc->fast_tracepoint_jumps; jp != NULL; jp = jp->next)
284 if (jp->pc == where)
285 return jp;
286
287 return NULL;
288}
289
290int
291fast_tracepoint_jump_here (CORE_ADDR where)
292{
293 struct fast_tracepoint_jump *jp = find_fast_tracepoint_jump_at (where);
294
295 return (jp != NULL);
296}
297
298int
299delete_fast_tracepoint_jump (struct fast_tracepoint_jump *todel)
300{
301 struct fast_tracepoint_jump *bp, **bp_link;
302 int ret;
303 struct process_info *proc = current_process ();
304
305 bp = proc->fast_tracepoint_jumps;
306 bp_link = &proc->fast_tracepoint_jumps;
307
308 while (bp)
309 {
310 if (bp == todel)
311 {
312 if (--bp->refcount == 0)
313 {
314 struct fast_tracepoint_jump *prev_bp_link = *bp_link;
315 unsigned char *buf;
316
317 /* Unlink it. */
318 *bp_link = bp->next;
319
320 /* Since there can be breakpoints inserted in the same
321 address range, we use `write_inferior_memory', which
322 takes care of layering breakpoints on top of fast
323 tracepoints, and on top of the buffer we pass it.
324 This works because we've already unlinked the fast
325 tracepoint jump above. Also note that we need to
326 pass the current shadow contents, because
327 write_inferior_memory updates any shadow memory with
328 what we pass here, and we want that to be a nop. */
329 buf = alloca (bp->length);
330 memcpy (buf, fast_tracepoint_jump_shadow (bp), bp->length);
331 ret = write_inferior_memory (bp->pc, buf, bp->length);
332 if (ret != 0)
333 {
334 /* Something went wrong, relink the jump. */
335 *bp_link = prev_bp_link;
336
337 if (debug_threads)
338 fprintf (stderr,
339 "Failed to uninsert fast tracepoint jump "
340 "at 0x%s (%s) while deleting it.\n",
341 paddress (bp->pc), strerror (ret));
342 return ret;
343 }
344
345 free (bp);
346 }
347
348 return 0;
349 }
350 else
351 {
352 bp_link = &bp->next;
353 bp = *bp_link;
354 }
355 }
356
357 warning ("Could not find fast tracepoint jump in list.");
358 return ENOENT;
359}
360
361void
362inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump *jp)
363{
364 jp->refcount++;
365}
366
367struct fast_tracepoint_jump *
368set_fast_tracepoint_jump (CORE_ADDR where,
369 unsigned char *insn, ULONGEST length)
370{
371 struct process_info *proc = current_process ();
372 struct fast_tracepoint_jump *jp;
373 int err;
374 unsigned char *buf;
375
376 /* We refcount fast tracepoint jumps. Check if we already know
377 about a jump at this address. */
378 jp = find_fast_tracepoint_jump_at (where);
379 if (jp != NULL)
380 {
381 jp->refcount++;
382 return jp;
383 }
384
385 /* We don't, so create a new object. Double the length, because the
386 flexible array member holds both the jump insn, and the
387 shadow. */
388 jp = xcalloc (1, sizeof (*jp) + (length * 2));
389 jp->pc = where;
390 jp->length = length;
391 memcpy (fast_tracepoint_jump_insn (jp), insn, length);
392 jp->refcount = 1;
393 buf = alloca (length);
394
395 /* Note that there can be trap breakpoints inserted in the same
396 address range. To access the original memory contents, we use
397 `read_inferior_memory', which masks out breakpoints. */
398 err = read_inferior_memory (where, buf, length);
399 if (err != 0)
400 {
401 if (debug_threads)
402 fprintf (stderr,
403 "Failed to read shadow memory of"
404 " fast tracepoint at 0x%s (%s).\n",
405 paddress (where), strerror (err));
406 free (jp);
407 return NULL;
408 }
409 memcpy (fast_tracepoint_jump_shadow (jp), buf, length);
410
411 /* Link the jump in. */
412 jp->inserted = 1;
413 jp->next = proc->fast_tracepoint_jumps;
414 proc->fast_tracepoint_jumps = jp;
415
416 /* Since there can be trap breakpoints inserted in the same address
417 range, we use use `write_inferior_memory', which takes care of
418 layering breakpoints on top of fast tracepoints, on top of the
419 buffer we pass it. This works because we've already linked in
420 the fast tracepoint jump above. Also note that we need to pass
421 the current shadow contents, because write_inferior_memory
422 updates any shadow memory with what we pass here, and we want
423 that to be a nop. */
424 err = write_inferior_memory (where, buf, length);
425 if (err != 0)
426 {
427 if (debug_threads)
428 fprintf (stderr,
429 "Failed to insert fast tracepoint jump at 0x%s (%s).\n",
430 paddress (where), strerror (err));
431
432 /* Unlink it. */
433 proc->fast_tracepoint_jumps = jp->next;
434 free (jp);
435
436 return NULL;
437 }
438
439 return jp;
440}
441
442void
443uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc)
444{
445 struct fast_tracepoint_jump *jp;
446 int err;
447
448 jp = find_fast_tracepoint_jump_at (pc);
449 if (jp == NULL)
450 {
451 /* This can happen when we remove all breakpoints while handling
452 a step-over. */
453 if (debug_threads)
454 fprintf (stderr,
455 "Could not find fast tracepoint jump at 0x%s "
456 "in list (uninserting).\n",
457 paddress (pc));
458 return;
459 }
460
461 if (jp->inserted)
462 {
463 unsigned char *buf;
464
465 jp->inserted = 0;
466
467 /* Since there can be trap breakpoints inserted in the same
468 address range, we use use `write_inferior_memory', which
469 takes care of layering breakpoints on top of fast
470 tracepoints, and on top of the buffer we pass it. This works
471 because we've already marked the fast tracepoint fast
472 tracepoint jump uninserted above. Also note that we need to
473 pass the current shadow contents, because
474 write_inferior_memory updates any shadow memory with what we
475 pass here, and we want that to be a nop. */
476 buf = alloca (jp->length);
477 memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
478 err = write_inferior_memory (jp->pc, buf, jp->length);
479 if (err != 0)
480 {
481 jp->inserted = 1;
482
483 if (debug_threads)
484 fprintf (stderr,
485 "Failed to uninsert fast tracepoint jump at 0x%s (%s).\n",
486 paddress (pc), strerror (err));
487 }
488 }
489}
490
491void
492reinsert_fast_tracepoint_jumps_at (CORE_ADDR where)
493{
494 struct fast_tracepoint_jump *jp;
495 int err;
496 unsigned char *buf;
497
498 jp = find_fast_tracepoint_jump_at (where);
499 if (jp == NULL)
500 {
501 /* This can happen when we remove breakpoints when a tracepoint
502 hit causes a tracing stop, while handling a step-over. */
503 if (debug_threads)
504 fprintf (stderr,
505 "Could not find fast tracepoint jump at 0x%s "
506 "in list (reinserting).\n",
507 paddress (where));
508 return;
509 }
510
511 if (jp->inserted)
512 error ("Jump already inserted at reinsert time.");
513
514 jp->inserted = 1;
515
516 /* Since there can be trap breakpoints inserted in the same address
517 range, we use `write_inferior_memory', which takes care of
518 layering breakpoints on top of fast tracepoints, and on top of
519 the buffer we pass it. This works because we've already marked
520 the fast tracepoint jump inserted above. Also note that we need
521 to pass the current shadow contents, because
522 write_inferior_memory updates any shadow memory with what we pass
523 here, and we want that to be a nop. */
524 buf = alloca (jp->length);
525 memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
526 err = write_inferior_memory (where, buf, jp->length);
527 if (err != 0)
528 {
529 jp->inserted = 0;
530
531 if (debug_threads)
532 fprintf (stderr,
533 "Failed to reinsert fast tracepoint jump at 0x%s (%s).\n",
534 paddress (where), strerror (err));
535 }
536}
537
538struct breakpoint *
539set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
540{
541 struct process_info *proc = current_process ();
542 struct breakpoint *bp;
543 struct raw_breakpoint *raw;
544
545 raw = set_raw_breakpoint_at (where);
546
547 if (raw == NULL)
548 {
549 /* warn? */
550 return NULL;
551 }
552
553 bp = xcalloc (1, sizeof (struct breakpoint));
554 bp->type = other_breakpoint;
555
556 bp->raw = raw;
557 bp->handler = handler;
558
559 bp->next = proc->breakpoints;
560 proc->breakpoints = bp;
561
562 return bp;
563}
564
565static int
566delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
567{
568 struct raw_breakpoint *bp, **bp_link;
569 int ret;
570
571 bp = proc->raw_breakpoints;
572 bp_link = &proc->raw_breakpoints;
573
574 while (bp)
575 {
576 if (bp == todel)
577 {
578 if (bp->inserted)
579 {
580 struct raw_breakpoint *prev_bp_link = *bp_link;
581 unsigned char buf[MAX_BREAKPOINT_LEN];
582
583 *bp_link = bp->next;
584
585 /* Since there can be trap breakpoints inserted in the
586 same address range, we use `write_inferior_memory',
587 which takes care of layering breakpoints on top of
588 fast tracepoints, and on top of the buffer we pass
589 it. This works because we've already unlinked the
590 fast tracepoint jump above. Also note that we need
591 to pass the current shadow contents, because
592 write_inferior_memory updates any shadow memory with
593 what we pass here, and we want that to be a nop. */
594 memcpy (buf, bp->old_data, breakpoint_len);
595 ret = write_inferior_memory (bp->pc, buf, breakpoint_len);
596 if (ret != 0)
597 {
598 /* Something went wrong, relink the breakpoint. */
599 *bp_link = prev_bp_link;
600
601 if (debug_threads)
602 fprintf (stderr,
603 "Failed to uninsert raw breakpoint "
604 "at 0x%s (%s) while deleting it.\n",
605 paddress (bp->pc), strerror (ret));
606 return ret;
607 }
608
609 }
610 else
611 *bp_link = bp->next;
612
613 free (bp);
614 return 0;
615 }
616 else
617 {
618 bp_link = &bp->next;
619 bp = *bp_link;
620 }
621 }
622
623 warning ("Could not find raw breakpoint in list.");
624 return ENOENT;
625}
626
627static int
628release_breakpoint (struct process_info *proc, struct breakpoint *bp)
629{
630 int newrefcount;
631 int ret;
632
633 newrefcount = bp->raw->refcount - 1;
634 if (newrefcount == 0)
635 {
636 ret = delete_raw_breakpoint (proc, bp->raw);
637 if (ret != 0)
638 return ret;
639 }
640 else
641 bp->raw->refcount = newrefcount;
642
643 free (bp);
644
645 return 0;
646}
647
648static int
649delete_breakpoint_1 (struct process_info *proc, struct breakpoint *todel)
650{
651 struct breakpoint *bp, **bp_link;
652 int err;
653
654 bp = proc->breakpoints;
655 bp_link = &proc->breakpoints;
656
657 while (bp)
658 {
659 if (bp == todel)
660 {
661 *bp_link = bp->next;
662
663 err = release_breakpoint (proc, bp);
664 if (err != 0)
665 return err;
666
667 bp = *bp_link;
668 return 0;
669 }
670 else
671 {
672 bp_link = &bp->next;
673 bp = *bp_link;
674 }
675 }
676
677 warning ("Could not find breakpoint in list.");
678 return ENOENT;
679}
680
681int
682delete_breakpoint (struct breakpoint *todel)
683{
684 struct process_info *proc = current_process ();
685 return delete_breakpoint_1 (proc, todel);
686}
687
688struct breakpoint *
689find_gdb_breakpoint_at (CORE_ADDR where)
690{
691 struct process_info *proc = current_process ();
692 struct breakpoint *bp;
693
694 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
695 if (bp->type == gdb_breakpoint && bp->raw->pc == where)
696 return bp;
697
698 return NULL;
699}
700
701int
702set_gdb_breakpoint_at (CORE_ADDR where)
703{
704 struct breakpoint *bp;
705
706 if (breakpoint_data == NULL)
707 return 1;
708
709 /* If we see GDB inserting a second breakpoint at the same address,
710 then the first breakpoint must have disappeared due to a shared
711 library unload. On targets where the shared libraries are
712 handled by userspace, like SVR4, for example, GDBserver can't
713 tell if a library was loaded or unloaded. Since we refcount
714 breakpoints, if we didn't do this, we'd just increase the
715 refcount of the previous breakpoint at this address, but the trap
716 was not planted in the inferior anymore, thus the breakpoint
717 would never be hit. */
718 bp = find_gdb_breakpoint_at (where);
719 if (bp != NULL)
720 {
721 delete_gdb_breakpoint_at (where);
722
723 /* Might as well validate all other breakpoints. */
724 validate_breakpoints ();
725 }
726
727 bp = set_breakpoint_at (where, NULL);
728 if (bp == NULL)
729 return -1;
730
731 bp->type = gdb_breakpoint;
732 return 0;
733}
734
735int
736delete_gdb_breakpoint_at (CORE_ADDR addr)
737{
738 struct breakpoint *bp;
739 int err;
740
741 if (breakpoint_data == NULL)
742 return 1;
743
744 bp = find_gdb_breakpoint_at (addr);
745 if (bp == NULL)
746 return -1;
747
748 /* Before deleting the breakpoint, make sure to free
749 its condition list. */
750 clear_gdb_breakpoint_conditions (addr);
751 err = delete_breakpoint (bp);
752 if (err)
753 return -1;
754
755 return 0;
756}
757
758/* Clear all conditions associated with this breakpoint address. */
759
760void
761clear_gdb_breakpoint_conditions (CORE_ADDR addr)
762{
763 struct breakpoint *bp = find_gdb_breakpoint_at (addr);
764 struct point_cond_list *cond;
765
766 if (bp == NULL || bp->cond_list == NULL)
767 return;
768
769 cond = bp->cond_list;
770
771 while (cond != NULL)
772 {
773 struct point_cond_list *cond_next;
774
775 cond_next = cond->next;
776 free (cond->cond->bytes);
777 free (cond->cond);
778 free (cond);
779 cond = cond_next;
780 }
781
782 bp->cond_list = NULL;
783}
784
785/* Add condition CONDITION to GDBserver's breakpoint BP. */
786
787void
788add_condition_to_breakpoint (struct breakpoint *bp,
789 struct agent_expr *condition)
790{
791 struct point_cond_list *new_cond;
792
793 /* Create new condition. */
794 new_cond = xcalloc (1, sizeof (*new_cond));
795 new_cond->cond = condition;
796
797 /* Add condition to the list. */
798 new_cond->next = bp->cond_list;
799 bp->cond_list = new_cond;
800}
801
802/* Add a target-side condition CONDITION to the breakpoint at ADDR. */
803
804int
805add_breakpoint_condition (CORE_ADDR addr, char **condition)
806{
807 struct breakpoint *bp = find_gdb_breakpoint_at (addr);
808 char *actparm = *condition;
809 struct agent_expr *cond;
810
811 if (bp == NULL)
812 return 1;
813
814 if (condition == NULL)
815 return 1;
816
817 cond = gdb_parse_agent_expr (&actparm);
818
819 if (cond == NULL)
820 {
821 fprintf (stderr, "Condition evaluation failed. "
822 "Assuming unconditional.\n");
823 return 0;
824 }
825
826 add_condition_to_breakpoint (bp, cond);
827
828 *condition = actparm;
829
830 return 0;
831}
832
833/* Evaluate condition (if any) at breakpoint BP. Return 1 if
834 true and 0 otherwise. */
835
836int
837gdb_condition_true_at_breakpoint (CORE_ADDR where)
838{
839 /* Fetch registers for the current inferior. */
840 struct breakpoint *bp = find_gdb_breakpoint_at (where);
841 ULONGEST value = 0;
842 struct point_cond_list *cl;
843 int err = 0;
844
845 struct regcache *regcache = get_thread_regcache (current_inferior, 1);
846
847 if (bp == NULL)
848 return 0;
849
850 /* Check if the breakpoint is unconditional. If it is,
851 the condition always evaluates to TRUE. */
852 if (bp->cond_list == NULL)
853 return 1;
854
855 /* Evaluate each condition in the breakpoint's list of conditions.
856 Return true if any of the conditions evaluates to TRUE.
857
858 If we failed to evaluate the expression, TRUE is returned. This
859 forces GDB to reevaluate the conditions. */
860 for (cl = bp->cond_list;
861 cl && !value && !err; cl = cl->next)
862 {
863 /* Evaluate the condition. */
864 err = gdb_eval_agent_expr (regcache, NULL, cl->cond, &value);
865 }
866
867 if (err)
868 return 1;
869
870 return (value != 0);
871}
872
873/* Add commands COMMANDS to GDBserver's breakpoint BP. */
874
875void
876add_commands_to_breakpoint (struct breakpoint *bp,
877 struct agent_expr *commands, int persist)
878{
879 struct point_command_list *new_cmd;
880
881 /* Create new command. */
882 new_cmd = xcalloc (1, sizeof (*new_cmd));
883 new_cmd->cmd = commands;
884 new_cmd->persistence = persist;
885
886 /* Add commands to the list. */
887 new_cmd->next = bp->command_list;
888 bp->command_list = new_cmd;
889}
890
891/* Add a target-side command COMMAND to the breakpoint at ADDR. */
892
893int
894add_breakpoint_commands (CORE_ADDR addr, char **command, int persist)
895{
896 struct breakpoint *bp = find_gdb_breakpoint_at (addr);
897 char *actparm = *command;
898 struct agent_expr *cmd;
899
900 if (bp == NULL)
901 return 1;
902
903 if (command == NULL)
904 return 1;
905
906 cmd = gdb_parse_agent_expr (&actparm);
907
908 if (cmd == NULL)
909 {
910 fprintf (stderr, "Command evaluation failed. "
911 "Disabling.\n");
912 return 0;
913 }
914
915 add_commands_to_breakpoint (bp, cmd, persist);
916
917 *command = actparm;
918
919 return 0;
920}
921
922/* Return true if there are no commands to run at this location,
923 which likely means we want to report back to GDB. */
924int
925gdb_no_commands_at_breakpoint (CORE_ADDR where)
926{
927 struct breakpoint *bp = find_gdb_breakpoint_at (where);
928
929 if (bp == NULL)
930 return 0;
931
932 if (debug_threads)
933 fprintf (stderr, "at 0x%s, bp command_list is 0x%lx\n",
934 paddress (where), (long) (uintptr_t) bp->command_list);
935 return (bp->command_list == NULL);
936}
937
938void
939run_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
967int
968gdb_breakpoint_here (CORE_ADDR where)
969{
970 return (find_gdb_breakpoint_at (where) != NULL);
971}
972
973void
974set_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
982void
983delete_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
1007static void
1008uninsert_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
1038void
1039uninsert_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
1060void
1061uninsert_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
1071static void
1072reinsert_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
1089void
1090reinsert_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
1110void
1111reinsert_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
1121void
1122check_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
1156void
1157set_breakpoint_data (const unsigned char *bp_data, int bp_len)
1158{
1159 breakpoint_data = bp_data;
1160 breakpoint_len = bp_len;
1161}
1162
1163int
1164breakpoint_here (CORE_ADDR addr)
1165{
1166 return (find_raw_breakpoint_at (addr) != NULL);
1167}
1168
1169int
1170breakpoint_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
1179static int
1180validate_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
1200static void
1201delete_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
1221void
1222validate_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
1236void
1237check_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
1316void
1317check_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
1405void
1406delete_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
1416void
1417mark_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
1428void
1429free_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}
This page took 0.027138 seconds and 4 git commands to generate.