Refactor clone_all_breakpoints
[deliverable/binutils-gdb.git] / gdb / gdbserver / mem-break.c
1 /* Memory breakpoint operations for the remote server for GDB.
2 Copyright (C) 2002-2016 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
25 #define MAX_BREAKPOINT_LEN 8
26
27 /* Helper macro used in loops that append multiple items to a singly-linked
28 list instead of inserting items at the head of the list, as, say, in the
29 breakpoint lists. LISTPP is a pointer to the pointer that is the head of
30 the new list. ITEMP is a pointer to the item to be added to the list.
31 TAILP must be defined to be the same type as ITEMP, and initialized to
32 NULL. */
33
34 #define APPEND_TO_LIST(listpp, itemp, tailp) \
35 do \
36 { \
37 if ((tailp) == NULL) \
38 *(listpp) = (itemp); \
39 else \
40 (tailp)->next = (itemp); \
41 (tailp) = (itemp); \
42 } \
43 while (0)
44
45 /* GDB will never try to install multiple breakpoints at the same
46 address. However, we can see GDB requesting to insert a breakpoint
47 at an address is had already inserted one previously in a few
48 situations.
49
50 - The RSP documentation on Z packets says that to avoid potential
51 problems with duplicate packets, the operations should be
52 implemented in an idempotent way.
53
54 - A breakpoint is set at ADDR, an address in a shared library.
55 Then the shared library is unloaded. And then another, unrelated,
56 breakpoint at ADDR is set. There is not breakpoint removal request
57 between the first and the second breakpoint.
58
59 - When GDB wants to update the target-side breakpoint conditions or
60 commands, it re-inserts the breakpoint, with updated
61 conditions/commands associated.
62
63 Also, we need to keep track of internal breakpoints too, so we do
64 need to be able to install multiple breakpoints at the same address
65 transparently.
66
67 We keep track of two different, and closely related structures. A
68 raw breakpoint, which manages the low level, close to the metal
69 aspect of a breakpoint. It holds the breakpoint address, and for
70 software breakpoints, a buffer holding a copy of the instructions
71 that would be in memory had not been a breakpoint there (we call
72 that the shadow memory of the breakpoint). We occasionally need to
73 temporarilly uninsert a breakpoint without the client knowing about
74 it (e.g., to step over an internal breakpoint), so we keep an
75 `inserted' state associated with this low level breakpoint
76 structure. There can only be one such object for a given address.
77 Then, we have (a bit higher level) breakpoints. This structure
78 holds a callback to be called whenever a breakpoint is hit, a
79 high-level type, and a link to a low level raw breakpoint. There
80 can be many high-level breakpoints at the same address, and all of
81 them will point to the same raw breakpoint, which is reference
82 counted. */
83
84 /* The low level, physical, raw breakpoint. */
85 struct raw_breakpoint
86 {
87 struct raw_breakpoint *next;
88
89 /* The low level type of the breakpoint (software breakpoint,
90 watchpoint, etc.) */
91 enum raw_bkpt_type raw_type;
92
93 /* A reference count. Each high level breakpoint referencing this
94 raw breakpoint accounts for one reference. */
95 int refcount;
96
97 /* The breakpoint's insertion address. There can only be one raw
98 breakpoint for a given PC. */
99 CORE_ADDR pc;
100
101 /* The breakpoint's kind. This is target specific. Most
102 architectures only use one specific instruction for breakpoints, while
103 others may use more than one. E.g., on ARM, we need to use different
104 breakpoint instructions on Thumb, Thumb-2, and ARM code. Likewise for
105 hardware breakpoints -- some architectures (including ARM) need to
106 setup debug registers differently depending on mode. */
107 int kind;
108
109 /* The breakpoint's shadow memory. */
110 unsigned char old_data[MAX_BREAKPOINT_LEN];
111
112 /* Positive if this breakpoint is currently inserted in the
113 inferior. Negative if it was, but we've detected that it's now
114 gone. Zero if not inserted. */
115 int inserted;
116 };
117
118 /* The type of a breakpoint. */
119 enum bkpt_type
120 {
121 /* A GDB breakpoint, requested with a Z0 packet. */
122 gdb_breakpoint_Z0,
123
124 /* A GDB hardware breakpoint, requested with a Z1 packet. */
125 gdb_breakpoint_Z1,
126
127 /* A GDB write watchpoint, requested with a Z2 packet. */
128 gdb_breakpoint_Z2,
129
130 /* A GDB read watchpoint, requested with a Z3 packet. */
131 gdb_breakpoint_Z3,
132
133 /* A GDB access watchpoint, requested with a Z4 packet. */
134 gdb_breakpoint_Z4,
135
136 /* A basic-software-single-step breakpoint. */
137 reinsert_breakpoint,
138
139 /* Any other breakpoint type that doesn't require specific
140 treatment goes here. E.g., an event breakpoint. */
141 other_breakpoint,
142 };
143
144 struct point_cond_list
145 {
146 /* Pointer to the agent expression that is the breakpoint's
147 conditional. */
148 struct agent_expr *cond;
149
150 /* Pointer to the next condition. */
151 struct point_cond_list *next;
152 };
153
154 struct point_command_list
155 {
156 /* Pointer to the agent expression that is the breakpoint's
157 commands. */
158 struct agent_expr *cmd;
159
160 /* Flag that is true if this command should run even while GDB is
161 disconnected. */
162 int persistence;
163
164 /* Pointer to the next command. */
165 struct point_command_list *next;
166 };
167
168 /* A high level (in gdbserver's perspective) breakpoint. */
169 struct breakpoint
170 {
171 struct breakpoint *next;
172
173 /* The breakpoint's type. */
174 enum bkpt_type type;
175
176 /* Link to this breakpoint's raw breakpoint. This is always
177 non-NULL. */
178 struct raw_breakpoint *raw;
179 };
180
181 /* Breakpoint requested by GDB. */
182
183 struct gdb_breakpoint
184 {
185 struct breakpoint base;
186
187 /* Pointer to the condition list that should be evaluated on
188 the target or NULL if the breakpoint is unconditional or
189 if GDB doesn't want us to evaluate the conditionals on the
190 target's side. */
191 struct point_cond_list *cond_list;
192
193 /* Point to the list of commands to run when this is hit. */
194 struct point_command_list *command_list;
195 };
196
197 /* Breakpoint used by GDBserver. */
198
199 struct other_breakpoint
200 {
201 struct breakpoint base;
202
203 /* Function to call when we hit this breakpoint. If it returns 1,
204 the breakpoint shall be deleted; 0 or if this callback is NULL,
205 it will be left inserted. */
206 int (*handler) (CORE_ADDR);
207 };
208
209 /* Reinsert breakpoint. */
210
211 struct reinsert_breakpoint
212 {
213 struct breakpoint base;
214 };
215
216 /* Return the breakpoint size from its kind. */
217
218 static int
219 bp_size (struct raw_breakpoint *bp)
220 {
221 int size = 0;
222
223 the_target->sw_breakpoint_from_kind (bp->kind, &size);
224 return size;
225 }
226
227 /* Return the breakpoint opcode from its kind. */
228
229 static const gdb_byte *
230 bp_opcode (struct raw_breakpoint *bp)
231 {
232 int size = 0;
233
234 return the_target->sw_breakpoint_from_kind (bp->kind, &size);
235 }
236
237 /* See mem-break.h. */
238
239 enum target_hw_bp_type
240 raw_bkpt_type_to_target_hw_bp_type (enum raw_bkpt_type raw_type)
241 {
242 switch (raw_type)
243 {
244 case raw_bkpt_type_hw:
245 return hw_execute;
246 case raw_bkpt_type_write_wp:
247 return hw_write;
248 case raw_bkpt_type_read_wp:
249 return hw_read;
250 case raw_bkpt_type_access_wp:
251 return hw_access;
252 default:
253 internal_error (__FILE__, __LINE__,
254 "bad raw breakpoint type %d", (int) raw_type);
255 }
256 }
257
258 /* See mem-break.h. */
259
260 static enum bkpt_type
261 Z_packet_to_bkpt_type (char z_type)
262 {
263 gdb_assert ('0' <= z_type && z_type <= '4');
264
265 return (enum bkpt_type) (gdb_breakpoint_Z0 + (z_type - '0'));
266 }
267
268 /* See mem-break.h. */
269
270 enum raw_bkpt_type
271 Z_packet_to_raw_bkpt_type (char z_type)
272 {
273 switch (z_type)
274 {
275 case Z_PACKET_SW_BP:
276 return raw_bkpt_type_sw;
277 case Z_PACKET_HW_BP:
278 return raw_bkpt_type_hw;
279 case Z_PACKET_WRITE_WP:
280 return raw_bkpt_type_write_wp;
281 case Z_PACKET_READ_WP:
282 return raw_bkpt_type_read_wp;
283 case Z_PACKET_ACCESS_WP:
284 return raw_bkpt_type_access_wp;
285 default:
286 gdb_assert_not_reached ("unhandled Z packet type.");
287 }
288 }
289
290 /* Return true if breakpoint TYPE is a GDB breakpoint. */
291
292 static int
293 is_gdb_breakpoint (enum bkpt_type type)
294 {
295 return (type == gdb_breakpoint_Z0
296 || type == gdb_breakpoint_Z1
297 || type == gdb_breakpoint_Z2
298 || type == gdb_breakpoint_Z3
299 || type == gdb_breakpoint_Z4);
300 }
301
302 int
303 any_persistent_commands (void)
304 {
305 struct process_info *proc = current_process ();
306 struct breakpoint *bp;
307 struct point_command_list *cl;
308
309 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
310 {
311 if (is_gdb_breakpoint (bp->type))
312 {
313 struct gdb_breakpoint *gdb_bp = (struct gdb_breakpoint *) bp;
314
315 for (cl = gdb_bp->command_list; cl != NULL; cl = cl->next)
316 if (cl->persistence)
317 return 1;
318 }
319 }
320
321 return 0;
322 }
323
324 /* Find low-level breakpoint of type TYPE at address ADDR that is not
325 insert-disabled. Returns NULL if not found. */
326
327 static struct raw_breakpoint *
328 find_enabled_raw_code_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type)
329 {
330 struct process_info *proc = current_process ();
331 struct raw_breakpoint *bp;
332
333 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
334 if (bp->pc == addr
335 && bp->raw_type == type
336 && bp->inserted >= 0)
337 return bp;
338
339 return NULL;
340 }
341
342 /* Find low-level breakpoint of type TYPE at address ADDR. Returns
343 NULL if not found. */
344
345 static struct raw_breakpoint *
346 find_raw_breakpoint_at (CORE_ADDR addr, enum raw_bkpt_type type, int kind)
347 {
348 struct process_info *proc = current_process ();
349 struct raw_breakpoint *bp;
350
351 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
352 if (bp->pc == addr && bp->raw_type == type && bp->kind == kind)
353 return bp;
354
355 return NULL;
356 }
357
358 /* See mem-break.h. */
359
360 int
361 insert_memory_breakpoint (struct raw_breakpoint *bp)
362 {
363 unsigned char buf[MAX_BREAKPOINT_LEN];
364 int err;
365
366 /* Note that there can be fast tracepoint jumps installed in the
367 same memory range, so to get at the original memory, we need to
368 use read_inferior_memory, which masks those out. */
369 err = read_inferior_memory (bp->pc, buf, bp_size (bp));
370 if (err != 0)
371 {
372 if (debug_threads)
373 debug_printf ("Failed to read shadow memory of"
374 " breakpoint at 0x%s (%s).\n",
375 paddress (bp->pc), strerror (err));
376 }
377 else
378 {
379 memcpy (bp->old_data, buf, bp_size (bp));
380
381 err = (*the_target->write_memory) (bp->pc, bp_opcode (bp),
382 bp_size (bp));
383 if (err != 0)
384 {
385 if (debug_threads)
386 debug_printf ("Failed to insert breakpoint at 0x%s (%s).\n",
387 paddress (bp->pc), strerror (err));
388 }
389 }
390 return err != 0 ? -1 : 0;
391 }
392
393 /* See mem-break.h */
394
395 int
396 remove_memory_breakpoint (struct raw_breakpoint *bp)
397 {
398 unsigned char buf[MAX_BREAKPOINT_LEN];
399 int err;
400
401 /* Since there can be trap breakpoints inserted in the same address
402 range, we use `write_inferior_memory', which takes care of
403 layering breakpoints on top of fast tracepoints, and on top of
404 the buffer we pass it. This works because the caller has already
405 either unlinked the breakpoint or marked it uninserted. Also
406 note that we need to pass the current shadow contents, because
407 write_inferior_memory updates any shadow memory with what we pass
408 here, and we want that to be a nop. */
409 memcpy (buf, bp->old_data, bp_size (bp));
410 err = write_inferior_memory (bp->pc, buf, bp_size (bp));
411 if (err != 0)
412 {
413 if (debug_threads)
414 debug_printf ("Failed to uninsert raw breakpoint "
415 "at 0x%s (%s) while deleting it.\n",
416 paddress (bp->pc), strerror (err));
417 }
418 return err != 0 ? -1 : 0;
419 }
420
421 /* Set a RAW breakpoint of type TYPE and kind KIND at WHERE. On
422 success, a pointer to the new breakpoint is returned. On failure,
423 returns NULL and writes the error code to *ERR. */
424
425 static struct raw_breakpoint *
426 set_raw_breakpoint_at (enum raw_bkpt_type type, CORE_ADDR where, int kind,
427 int *err)
428 {
429 struct process_info *proc = current_process ();
430 struct raw_breakpoint *bp;
431 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
432
433 if (type == raw_bkpt_type_sw || type == raw_bkpt_type_hw)
434 {
435 bp = find_enabled_raw_code_breakpoint_at (where, type);
436 if (bp != NULL && bp->kind != kind)
437 {
438 /* A different kind than previously seen. The previous
439 breakpoint must be gone then. */
440 if (debug_threads)
441 debug_printf ("Inconsistent breakpoint kind? Was %d, now %d.\n",
442 bp->kind, kind);
443 bp->inserted = -1;
444 bp = NULL;
445 }
446 }
447 else
448 bp = find_raw_breakpoint_at (where, type, kind);
449
450 if (bp == NULL)
451 {
452 bp = XCNEW (struct raw_breakpoint);
453 bp->pc = where;
454 bp->kind = kind;
455 bp->raw_type = type;
456 make_cleanup (xfree, bp);
457 }
458
459 if (!bp->inserted)
460 {
461 *err = the_target->insert_point (bp->raw_type, bp->pc, bp->kind, bp);
462 if (*err != 0)
463 {
464 if (debug_threads)
465 debug_printf ("Failed to insert breakpoint at 0x%s (%d).\n",
466 paddress (where), *err);
467
468 do_cleanups (old_chain);
469 return NULL;
470 }
471
472 bp->inserted = 1;
473 }
474
475 discard_cleanups (old_chain);
476
477 /* Link the breakpoint in, if this is the first reference. */
478 if (++bp->refcount == 1)
479 {
480 bp->next = proc->raw_breakpoints;
481 proc->raw_breakpoints = bp;
482 }
483 return bp;
484 }
485
486 /* Notice that breakpoint traps are always installed on top of fast
487 tracepoint jumps. This is even if the fast tracepoint is installed
488 at a later time compared to when the breakpoint was installed.
489 This means that a stopping breakpoint or tracepoint has higher
490 "priority". In turn, this allows having fast and slow tracepoints
491 (and breakpoints) at the same address behave correctly. */
492
493
494 /* A fast tracepoint jump. */
495
496 struct fast_tracepoint_jump
497 {
498 struct fast_tracepoint_jump *next;
499
500 /* A reference count. GDB can install more than one fast tracepoint
501 at the same address (each with its own action list, for
502 example). */
503 int refcount;
504
505 /* The fast tracepoint's insertion address. There can only be one
506 of these for a given PC. */
507 CORE_ADDR pc;
508
509 /* Non-zero if this fast tracepoint jump is currently inserted in
510 the inferior. */
511 int inserted;
512
513 /* The length of the jump instruction. */
514 int length;
515
516 /* A poor-man's flexible array member, holding both the jump
517 instruction to insert, and a copy of the instruction that would
518 be in memory had not been a jump there (the shadow memory of the
519 tracepoint jump). */
520 unsigned char insn_and_shadow[0];
521 };
522
523 /* Fast tracepoint FP's jump instruction to insert. */
524 #define fast_tracepoint_jump_insn(fp) \
525 ((fp)->insn_and_shadow + 0)
526
527 /* The shadow memory of fast tracepoint jump FP. */
528 #define fast_tracepoint_jump_shadow(fp) \
529 ((fp)->insn_and_shadow + (fp)->length)
530
531
532 /* Return the fast tracepoint jump set at WHERE. */
533
534 static struct fast_tracepoint_jump *
535 find_fast_tracepoint_jump_at (CORE_ADDR where)
536 {
537 struct process_info *proc = current_process ();
538 struct fast_tracepoint_jump *jp;
539
540 for (jp = proc->fast_tracepoint_jumps; jp != NULL; jp = jp->next)
541 if (jp->pc == where)
542 return jp;
543
544 return NULL;
545 }
546
547 int
548 fast_tracepoint_jump_here (CORE_ADDR where)
549 {
550 struct fast_tracepoint_jump *jp = find_fast_tracepoint_jump_at (where);
551
552 return (jp != NULL);
553 }
554
555 int
556 delete_fast_tracepoint_jump (struct fast_tracepoint_jump *todel)
557 {
558 struct fast_tracepoint_jump *bp, **bp_link;
559 int ret;
560 struct process_info *proc = current_process ();
561
562 bp = proc->fast_tracepoint_jumps;
563 bp_link = &proc->fast_tracepoint_jumps;
564
565 while (bp)
566 {
567 if (bp == todel)
568 {
569 if (--bp->refcount == 0)
570 {
571 struct fast_tracepoint_jump *prev_bp_link = *bp_link;
572 unsigned char *buf;
573
574 /* Unlink it. */
575 *bp_link = bp->next;
576
577 /* Since there can be breakpoints inserted in the same
578 address range, we use `write_inferior_memory', which
579 takes care of layering breakpoints on top of fast
580 tracepoints, and on top of the buffer we pass it.
581 This works because we've already unlinked the fast
582 tracepoint jump above. Also note that we need to
583 pass the current shadow contents, because
584 write_inferior_memory updates any shadow memory with
585 what we pass here, and we want that to be a nop. */
586 buf = (unsigned char *) alloca (bp->length);
587 memcpy (buf, fast_tracepoint_jump_shadow (bp), bp->length);
588 ret = write_inferior_memory (bp->pc, buf, bp->length);
589 if (ret != 0)
590 {
591 /* Something went wrong, relink the jump. */
592 *bp_link = prev_bp_link;
593
594 if (debug_threads)
595 debug_printf ("Failed to uninsert fast tracepoint jump "
596 "at 0x%s (%s) while deleting it.\n",
597 paddress (bp->pc), strerror (ret));
598 return ret;
599 }
600
601 free (bp);
602 }
603
604 return 0;
605 }
606 else
607 {
608 bp_link = &bp->next;
609 bp = *bp_link;
610 }
611 }
612
613 warning ("Could not find fast tracepoint jump in list.");
614 return ENOENT;
615 }
616
617 void
618 inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump *jp)
619 {
620 jp->refcount++;
621 }
622
623 struct fast_tracepoint_jump *
624 set_fast_tracepoint_jump (CORE_ADDR where,
625 unsigned char *insn, ULONGEST length)
626 {
627 struct process_info *proc = current_process ();
628 struct fast_tracepoint_jump *jp;
629 int err;
630 unsigned char *buf;
631
632 /* We refcount fast tracepoint jumps. Check if we already know
633 about a jump at this address. */
634 jp = find_fast_tracepoint_jump_at (where);
635 if (jp != NULL)
636 {
637 jp->refcount++;
638 return jp;
639 }
640
641 /* We don't, so create a new object. Double the length, because the
642 flexible array member holds both the jump insn, and the
643 shadow. */
644 jp = (struct fast_tracepoint_jump *) xcalloc (1, sizeof (*jp) + (length * 2));
645 jp->pc = where;
646 jp->length = length;
647 memcpy (fast_tracepoint_jump_insn (jp), insn, length);
648 jp->refcount = 1;
649 buf = (unsigned char *) alloca (length);
650
651 /* Note that there can be trap breakpoints inserted in the same
652 address range. To access the original memory contents, we use
653 `read_inferior_memory', which masks out breakpoints. */
654 err = read_inferior_memory (where, buf, length);
655 if (err != 0)
656 {
657 if (debug_threads)
658 debug_printf ("Failed to read shadow memory of"
659 " fast tracepoint at 0x%s (%s).\n",
660 paddress (where), strerror (err));
661 free (jp);
662 return NULL;
663 }
664 memcpy (fast_tracepoint_jump_shadow (jp), buf, length);
665
666 /* Link the jump in. */
667 jp->inserted = 1;
668 jp->next = proc->fast_tracepoint_jumps;
669 proc->fast_tracepoint_jumps = jp;
670
671 /* Since there can be trap breakpoints inserted in the same address
672 range, we use use `write_inferior_memory', which takes care of
673 layering breakpoints on top of fast tracepoints, on top of the
674 buffer we pass it. This works because we've already linked in
675 the fast tracepoint jump above. Also note that we need to pass
676 the current shadow contents, because write_inferior_memory
677 updates any shadow memory with what we pass here, and we want
678 that to be a nop. */
679 err = write_inferior_memory (where, buf, length);
680 if (err != 0)
681 {
682 if (debug_threads)
683 debug_printf ("Failed to insert fast tracepoint jump at 0x%s (%s).\n",
684 paddress (where), strerror (err));
685
686 /* Unlink it. */
687 proc->fast_tracepoint_jumps = jp->next;
688 free (jp);
689
690 return NULL;
691 }
692
693 return jp;
694 }
695
696 void
697 uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc)
698 {
699 struct fast_tracepoint_jump *jp;
700 int err;
701
702 jp = find_fast_tracepoint_jump_at (pc);
703 if (jp == NULL)
704 {
705 /* This can happen when we remove all breakpoints while handling
706 a step-over. */
707 if (debug_threads)
708 debug_printf ("Could not find fast tracepoint jump at 0x%s "
709 "in list (uninserting).\n",
710 paddress (pc));
711 return;
712 }
713
714 if (jp->inserted)
715 {
716 unsigned char *buf;
717
718 jp->inserted = 0;
719
720 /* Since there can be trap breakpoints inserted in the same
721 address range, we use use `write_inferior_memory', which
722 takes care of layering breakpoints on top of fast
723 tracepoints, and on top of the buffer we pass it. This works
724 because we've already marked the fast tracepoint fast
725 tracepoint jump uninserted above. Also note that we need to
726 pass the current shadow contents, because
727 write_inferior_memory updates any shadow memory with what we
728 pass here, and we want that to be a nop. */
729 buf = (unsigned char *) alloca (jp->length);
730 memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
731 err = write_inferior_memory (jp->pc, buf, jp->length);
732 if (err != 0)
733 {
734 jp->inserted = 1;
735
736 if (debug_threads)
737 debug_printf ("Failed to uninsert fast tracepoint jump at"
738 " 0x%s (%s).\n",
739 paddress (pc), strerror (err));
740 }
741 }
742 }
743
744 void
745 reinsert_fast_tracepoint_jumps_at (CORE_ADDR where)
746 {
747 struct fast_tracepoint_jump *jp;
748 int err;
749 unsigned char *buf;
750
751 jp = find_fast_tracepoint_jump_at (where);
752 if (jp == NULL)
753 {
754 /* This can happen when we remove breakpoints when a tracepoint
755 hit causes a tracing stop, while handling a step-over. */
756 if (debug_threads)
757 debug_printf ("Could not find fast tracepoint jump at 0x%s "
758 "in list (reinserting).\n",
759 paddress (where));
760 return;
761 }
762
763 if (jp->inserted)
764 error ("Jump already inserted at reinsert time.");
765
766 jp->inserted = 1;
767
768 /* Since there can be trap breakpoints inserted in the same address
769 range, we use `write_inferior_memory', which takes care of
770 layering breakpoints on top of fast tracepoints, and on top of
771 the buffer we pass it. This works because we've already marked
772 the fast tracepoint jump inserted above. Also note that we need
773 to pass the current shadow contents, because
774 write_inferior_memory updates any shadow memory with what we pass
775 here, and we want that to be a nop. */
776 buf = (unsigned char *) alloca (jp->length);
777 memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
778 err = write_inferior_memory (where, buf, jp->length);
779 if (err != 0)
780 {
781 jp->inserted = 0;
782
783 if (debug_threads)
784 debug_printf ("Failed to reinsert fast tracepoint jump at"
785 " 0x%s (%s).\n",
786 paddress (where), strerror (err));
787 }
788 }
789
790 /* Set a high-level breakpoint of type TYPE, with low level type
791 RAW_TYPE and kind KIND, at WHERE. On success, a pointer to the new
792 breakpoint is returned. On failure, returns NULL and writes the
793 error code to *ERR. HANDLER is called when the breakpoint is hit.
794 HANDLER should return 1 if the breakpoint should be deleted, 0
795 otherwise. */
796
797 static struct breakpoint *
798 set_breakpoint (enum bkpt_type type, enum raw_bkpt_type raw_type,
799 CORE_ADDR where, int kind,
800 int (*handler) (CORE_ADDR), int *err)
801 {
802 struct process_info *proc = current_process ();
803 struct breakpoint *bp;
804 struct raw_breakpoint *raw;
805
806 raw = set_raw_breakpoint_at (raw_type, where, kind, err);
807
808 if (raw == NULL)
809 {
810 /* warn? */
811 return NULL;
812 }
813
814 if (is_gdb_breakpoint (type))
815 {
816 struct gdb_breakpoint *gdb_bp = XCNEW (struct gdb_breakpoint);
817
818 bp = (struct breakpoint *) gdb_bp;
819 gdb_assert (handler == NULL);
820 }
821 else if (type == other_breakpoint)
822 {
823 struct other_breakpoint *other_bp = XCNEW (struct other_breakpoint);
824
825 other_bp->handler = handler;
826 bp = (struct breakpoint *) other_bp;
827 }
828 else if (type == reinsert_breakpoint)
829 {
830 struct reinsert_breakpoint *reinsert_bp
831 = XCNEW (struct reinsert_breakpoint);
832
833 bp = (struct breakpoint *) reinsert_bp;
834 }
835 else
836 gdb_assert_not_reached ("unhandled breakpoint type");
837
838 bp->type = type;
839 bp->raw = raw;
840
841 bp->next = proc->breakpoints;
842 proc->breakpoints = bp;
843
844 return bp;
845 }
846
847 /* Set breakpoint of TYPE on address WHERE with handler HANDLER. */
848
849 static struct breakpoint *
850 set_breakpoint_type_at (enum bkpt_type type, CORE_ADDR where,
851 int (*handler) (CORE_ADDR))
852 {
853 int err_ignored;
854 CORE_ADDR placed_address = where;
855 int breakpoint_kind = target_breakpoint_kind_from_pc (&placed_address);
856
857 return set_breakpoint (type, raw_bkpt_type_sw,
858 placed_address, breakpoint_kind, handler,
859 &err_ignored);
860 }
861
862 /* See mem-break.h */
863
864 struct breakpoint *
865 set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
866 {
867 return set_breakpoint_type_at (other_breakpoint, where, handler);
868 }
869
870
871 static int
872 delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
873 {
874 struct raw_breakpoint *bp, **bp_link;
875 int ret;
876
877 bp = proc->raw_breakpoints;
878 bp_link = &proc->raw_breakpoints;
879
880 while (bp)
881 {
882 if (bp == todel)
883 {
884 if (bp->inserted > 0)
885 {
886 struct raw_breakpoint *prev_bp_link = *bp_link;
887
888 *bp_link = bp->next;
889
890 ret = the_target->remove_point (bp->raw_type, bp->pc, bp->kind,
891 bp);
892 if (ret != 0)
893 {
894 /* Something went wrong, relink the breakpoint. */
895 *bp_link = prev_bp_link;
896
897 if (debug_threads)
898 debug_printf ("Failed to uninsert raw breakpoint "
899 "at 0x%s while deleting it.\n",
900 paddress (bp->pc));
901 return ret;
902 }
903 }
904 else
905 *bp_link = bp->next;
906
907 free (bp);
908 return 0;
909 }
910 else
911 {
912 bp_link = &bp->next;
913 bp = *bp_link;
914 }
915 }
916
917 warning ("Could not find raw breakpoint in list.");
918 return ENOENT;
919 }
920
921 static int
922 release_breakpoint (struct process_info *proc, struct breakpoint *bp)
923 {
924 int newrefcount;
925 int ret;
926
927 newrefcount = bp->raw->refcount - 1;
928 if (newrefcount == 0)
929 {
930 ret = delete_raw_breakpoint (proc, bp->raw);
931 if (ret != 0)
932 return ret;
933 }
934 else
935 bp->raw->refcount = newrefcount;
936
937 free (bp);
938
939 return 0;
940 }
941
942 static int
943 delete_breakpoint_1 (struct process_info *proc, struct breakpoint *todel)
944 {
945 struct breakpoint *bp, **bp_link;
946 int err;
947
948 bp = proc->breakpoints;
949 bp_link = &proc->breakpoints;
950
951 while (bp)
952 {
953 if (bp == todel)
954 {
955 *bp_link = bp->next;
956
957 err = release_breakpoint (proc, bp);
958 if (err != 0)
959 return err;
960
961 bp = *bp_link;
962 return 0;
963 }
964 else
965 {
966 bp_link = &bp->next;
967 bp = *bp_link;
968 }
969 }
970
971 warning ("Could not find breakpoint in list.");
972 return ENOENT;
973 }
974
975 int
976 delete_breakpoint (struct breakpoint *todel)
977 {
978 struct process_info *proc = current_process ();
979 return delete_breakpoint_1 (proc, todel);
980 }
981
982 /* Locate a GDB breakpoint of type Z_TYPE and kind KIND placed at
983 address ADDR and return a pointer to its structure. If KIND is -1,
984 the breakpoint's kind is ignored. */
985
986 static struct gdb_breakpoint *
987 find_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind)
988 {
989 struct process_info *proc = current_process ();
990 struct breakpoint *bp;
991 enum bkpt_type type = Z_packet_to_bkpt_type (z_type);
992
993 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
994 if (bp->type == type && bp->raw->pc == addr
995 && (kind == -1 || bp->raw->kind == kind))
996 return (gdb_breakpoint *) bp;
997
998 return NULL;
999 }
1000
1001 static int
1002 z_type_supported (char z_type)
1003 {
1004 return (z_type >= '0' && z_type <= '4'
1005 && the_target->supports_z_point_type != NULL
1006 && the_target->supports_z_point_type (z_type));
1007 }
1008
1009 /* Create a new GDB breakpoint of type Z_TYPE at ADDR with kind KIND.
1010 Returns a pointer to the newly created breakpoint on success. On
1011 failure returns NULL and sets *ERR to either -1 for error, or 1 if
1012 Z_TYPE breakpoints are not supported on this target. */
1013
1014 static struct gdb_breakpoint *
1015 set_gdb_breakpoint_1 (char z_type, CORE_ADDR addr, int kind, int *err)
1016 {
1017 struct gdb_breakpoint *bp;
1018 enum bkpt_type type;
1019 enum raw_bkpt_type raw_type;
1020
1021 /* If we see GDB inserting a second code breakpoint at the same
1022 address, then either: GDB is updating the breakpoint's conditions
1023 or commands; or, the first breakpoint must have disappeared due
1024 to a shared library unload. On targets where the shared
1025 libraries are handled by userspace, like SVR4, for example,
1026 GDBserver can't tell if a library was loaded or unloaded. Since
1027 we refcount raw breakpoints, we must be careful to make sure GDB
1028 breakpoints never contribute more than one reference. if we
1029 didn't do this, in case the previous breakpoint is gone due to a
1030 shared library unload, we'd just increase the refcount of the
1031 previous breakpoint at this address, but the trap was not planted
1032 in the inferior anymore, thus the breakpoint would never be hit.
1033 Note this must be careful to not create a window where
1034 breakpoints are removed from the target, for non-stop, in case
1035 the target can poke at memory while the program is running. */
1036 if (z_type == Z_PACKET_SW_BP
1037 || z_type == Z_PACKET_HW_BP)
1038 {
1039 bp = find_gdb_breakpoint (z_type, addr, -1);
1040
1041 if (bp != NULL)
1042 {
1043 if (bp->base.raw->kind != kind)
1044 {
1045 /* A different kind than previously seen. The previous
1046 breakpoint must be gone then. */
1047 bp->base.raw->inserted = -1;
1048 delete_breakpoint ((struct breakpoint *) bp);
1049 bp = NULL;
1050 }
1051 else if (z_type == Z_PACKET_SW_BP)
1052 {
1053 /* Check if the breakpoint is actually gone from the
1054 target, due to an solib unload, for example. Might
1055 as well validate _all_ breakpoints. */
1056 validate_breakpoints ();
1057
1058 /* Breakpoints that don't pass validation are
1059 deleted. */
1060 bp = find_gdb_breakpoint (z_type, addr, -1);
1061 }
1062 }
1063 }
1064 else
1065 {
1066 /* Data breakpoints for the same address but different kind are
1067 expected. GDB doesn't merge these. The backend gets to do
1068 that if it wants/can. */
1069 bp = find_gdb_breakpoint (z_type, addr, kind);
1070 }
1071
1072 if (bp != NULL)
1073 {
1074 /* We already know about this breakpoint, there's nothing else
1075 to do - GDB's reference is already accounted for. Note that
1076 whether the breakpoint inserted is left as is - we may be
1077 stepping over it, for example, in which case we don't want to
1078 force-reinsert it. */
1079 return bp;
1080 }
1081
1082 raw_type = Z_packet_to_raw_bkpt_type (z_type);
1083 type = Z_packet_to_bkpt_type (z_type);
1084 return (struct gdb_breakpoint *) set_breakpoint (type, raw_type, addr,
1085 kind, NULL, err);
1086 }
1087
1088 static int
1089 check_gdb_bp_preconditions (char z_type, int *err)
1090 {
1091 /* As software/memory breakpoints work by poking at memory, we need
1092 to prepare to access memory. If that operation fails, we need to
1093 return error. Seeing an error, if this is the first breakpoint
1094 of that type that GDB tries to insert, GDB would then assume the
1095 breakpoint type is supported, but it may actually not be. So we
1096 need to check whether the type is supported at all before
1097 preparing to access memory. */
1098 if (!z_type_supported (z_type))
1099 {
1100 *err = 1;
1101 return 0;
1102 }
1103
1104 return 1;
1105 }
1106
1107 /* See mem-break.h. This is a wrapper for set_gdb_breakpoint_1 that
1108 knows to prepare to access memory for Z0 breakpoints. */
1109
1110 struct gdb_breakpoint *
1111 set_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind, int *err)
1112 {
1113 struct gdb_breakpoint *bp;
1114
1115 if (!check_gdb_bp_preconditions (z_type, err))
1116 return NULL;
1117
1118 /* If inserting a software/memory breakpoint, need to prepare to
1119 access memory. */
1120 if (z_type == Z_PACKET_SW_BP)
1121 {
1122 if (prepare_to_access_memory () != 0)
1123 {
1124 *err = -1;
1125 return NULL;
1126 }
1127 }
1128
1129 bp = set_gdb_breakpoint_1 (z_type, addr, kind, err);
1130
1131 if (z_type == Z_PACKET_SW_BP)
1132 done_accessing_memory ();
1133
1134 return bp;
1135 }
1136
1137 /* Delete a GDB breakpoint of type Z_TYPE and kind KIND previously
1138 inserted at ADDR with set_gdb_breakpoint_at. Returns 0 on success,
1139 -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
1140 target. */
1141
1142 static int
1143 delete_gdb_breakpoint_1 (char z_type, CORE_ADDR addr, int kind)
1144 {
1145 struct gdb_breakpoint *bp;
1146 int err;
1147
1148 bp = find_gdb_breakpoint (z_type, addr, kind);
1149 if (bp == NULL)
1150 return -1;
1151
1152 /* Before deleting the breakpoint, make sure to free its condition
1153 and command lists. */
1154 clear_breakpoint_conditions_and_commands (bp);
1155 err = delete_breakpoint ((struct breakpoint *) bp);
1156 if (err != 0)
1157 return -1;
1158
1159 return 0;
1160 }
1161
1162 /* See mem-break.h. This is a wrapper for delete_gdb_breakpoint that
1163 knows to prepare to access memory for Z0 breakpoints. */
1164
1165 int
1166 delete_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind)
1167 {
1168 int ret;
1169
1170 if (!check_gdb_bp_preconditions (z_type, &ret))
1171 return ret;
1172
1173 /* If inserting a software/memory breakpoint, need to prepare to
1174 access memory. */
1175 if (z_type == Z_PACKET_SW_BP)
1176 {
1177 int err;
1178
1179 err = prepare_to_access_memory ();
1180 if (err != 0)
1181 return -1;
1182 }
1183
1184 ret = delete_gdb_breakpoint_1 (z_type, addr, kind);
1185
1186 if (z_type == Z_PACKET_SW_BP)
1187 done_accessing_memory ();
1188
1189 return ret;
1190 }
1191
1192 /* Clear all conditions associated with a breakpoint. */
1193
1194 static void
1195 clear_breakpoint_conditions (struct gdb_breakpoint *bp)
1196 {
1197 struct point_cond_list *cond;
1198
1199 if (bp->cond_list == NULL)
1200 return;
1201
1202 cond = bp->cond_list;
1203
1204 while (cond != NULL)
1205 {
1206 struct point_cond_list *cond_next;
1207
1208 cond_next = cond->next;
1209 gdb_free_agent_expr (cond->cond);
1210 free (cond);
1211 cond = cond_next;
1212 }
1213
1214 bp->cond_list = NULL;
1215 }
1216
1217 /* Clear all commands associated with a breakpoint. */
1218
1219 static void
1220 clear_breakpoint_commands (struct gdb_breakpoint *bp)
1221 {
1222 struct point_command_list *cmd;
1223
1224 if (bp->command_list == NULL)
1225 return;
1226
1227 cmd = bp->command_list;
1228
1229 while (cmd != NULL)
1230 {
1231 struct point_command_list *cmd_next;
1232
1233 cmd_next = cmd->next;
1234 gdb_free_agent_expr (cmd->cmd);
1235 free (cmd);
1236 cmd = cmd_next;
1237 }
1238
1239 bp->command_list = NULL;
1240 }
1241
1242 void
1243 clear_breakpoint_conditions_and_commands (struct gdb_breakpoint *bp)
1244 {
1245 clear_breakpoint_conditions (bp);
1246 clear_breakpoint_commands (bp);
1247 }
1248
1249 /* Add condition CONDITION to GDBserver's breakpoint BP. */
1250
1251 static void
1252 add_condition_to_breakpoint (struct gdb_breakpoint *bp,
1253 struct agent_expr *condition)
1254 {
1255 struct point_cond_list *new_cond;
1256
1257 /* Create new condition. */
1258 new_cond = XCNEW (struct point_cond_list);
1259 new_cond->cond = condition;
1260
1261 /* Add condition to the list. */
1262 new_cond->next = bp->cond_list;
1263 bp->cond_list = new_cond;
1264 }
1265
1266 /* Add a target-side condition CONDITION to a breakpoint. */
1267
1268 int
1269 add_breakpoint_condition (struct gdb_breakpoint *bp, char **condition)
1270 {
1271 char *actparm = *condition;
1272 struct agent_expr *cond;
1273
1274 if (condition == NULL)
1275 return 1;
1276
1277 if (bp == NULL)
1278 return 0;
1279
1280 cond = gdb_parse_agent_expr (&actparm);
1281
1282 if (cond == NULL)
1283 {
1284 fprintf (stderr, "Condition evaluation failed. "
1285 "Assuming unconditional.\n");
1286 return 0;
1287 }
1288
1289 add_condition_to_breakpoint (bp, cond);
1290
1291 *condition = actparm;
1292
1293 return 1;
1294 }
1295
1296 /* Evaluate condition (if any) at breakpoint BP. Return 1 if
1297 true and 0 otherwise. */
1298
1299 static int
1300 gdb_condition_true_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
1301 {
1302 /* Fetch registers for the current inferior. */
1303 struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1304 ULONGEST value = 0;
1305 struct point_cond_list *cl;
1306 int err = 0;
1307 struct eval_agent_expr_context ctx;
1308
1309 if (bp == NULL)
1310 return 0;
1311
1312 /* Check if the breakpoint is unconditional. If it is,
1313 the condition always evaluates to TRUE. */
1314 if (bp->cond_list == NULL)
1315 return 1;
1316
1317 ctx.regcache = get_thread_regcache (current_thread, 1);
1318 ctx.tframe = NULL;
1319 ctx.tpoint = NULL;
1320
1321 /* Evaluate each condition in the breakpoint's list of conditions.
1322 Return true if any of the conditions evaluates to TRUE.
1323
1324 If we failed to evaluate the expression, TRUE is returned. This
1325 forces GDB to reevaluate the conditions. */
1326 for (cl = bp->cond_list;
1327 cl && !value && !err; cl = cl->next)
1328 {
1329 /* Evaluate the condition. */
1330 err = gdb_eval_agent_expr (&ctx, cl->cond, &value);
1331 }
1332
1333 if (err)
1334 return 1;
1335
1336 return (value != 0);
1337 }
1338
1339 int
1340 gdb_condition_true_at_breakpoint (CORE_ADDR where)
1341 {
1342 /* Only check code (software or hardware) breakpoints. */
1343 return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
1344 || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
1345 }
1346
1347 /* Add commands COMMANDS to GDBserver's breakpoint BP. */
1348
1349 static void
1350 add_commands_to_breakpoint (struct gdb_breakpoint *bp,
1351 struct agent_expr *commands, int persist)
1352 {
1353 struct point_command_list *new_cmd;
1354
1355 /* Create new command. */
1356 new_cmd = XCNEW (struct point_command_list);
1357 new_cmd->cmd = commands;
1358 new_cmd->persistence = persist;
1359
1360 /* Add commands to the list. */
1361 new_cmd->next = bp->command_list;
1362 bp->command_list = new_cmd;
1363 }
1364
1365 /* Add a target-side command COMMAND to the breakpoint at ADDR. */
1366
1367 int
1368 add_breakpoint_commands (struct gdb_breakpoint *bp, char **command,
1369 int persist)
1370 {
1371 char *actparm = *command;
1372 struct agent_expr *cmd;
1373
1374 if (command == NULL)
1375 return 1;
1376
1377 if (bp == NULL)
1378 return 0;
1379
1380 cmd = gdb_parse_agent_expr (&actparm);
1381
1382 if (cmd == NULL)
1383 {
1384 fprintf (stderr, "Command evaluation failed. "
1385 "Disabling.\n");
1386 return 0;
1387 }
1388
1389 add_commands_to_breakpoint (bp, cmd, persist);
1390
1391 *command = actparm;
1392
1393 return 1;
1394 }
1395
1396 /* Return true if there are no commands to run at this location,
1397 which likely means we want to report back to GDB. */
1398
1399 static int
1400 gdb_no_commands_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
1401 {
1402 struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1403
1404 if (bp == NULL)
1405 return 1;
1406
1407 if (debug_threads)
1408 debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s\n",
1409 paddress (addr), z_type,
1410 phex_nz ((uintptr_t) bp->command_list, 0));
1411 return (bp->command_list == NULL);
1412 }
1413
1414 /* Return true if there are no commands to run at this location,
1415 which likely means we want to report back to GDB. */
1416
1417 int
1418 gdb_no_commands_at_breakpoint (CORE_ADDR where)
1419 {
1420 /* Only check code (software or hardware) breakpoints. */
1421 return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
1422 && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
1423 }
1424
1425 /* Run a breakpoint's commands. Returns 0 if there was a problem
1426 running any command, 1 otherwise. */
1427
1428 static int
1429 run_breakpoint_commands_z_type (char z_type, CORE_ADDR addr)
1430 {
1431 /* Fetch registers for the current inferior. */
1432 struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1433 ULONGEST value = 0;
1434 struct point_command_list *cl;
1435 int err = 0;
1436 struct eval_agent_expr_context ctx;
1437
1438 if (bp == NULL)
1439 return 1;
1440
1441 ctx.regcache = get_thread_regcache (current_thread, 1);
1442 ctx.tframe = NULL;
1443 ctx.tpoint = NULL;
1444
1445 for (cl = bp->command_list;
1446 cl && !value && !err; cl = cl->next)
1447 {
1448 /* Run the command. */
1449 err = gdb_eval_agent_expr (&ctx, cl->cmd, &value);
1450
1451 /* If one command has a problem, stop digging the hole deeper. */
1452 if (err)
1453 return 0;
1454 }
1455
1456 return 1;
1457 }
1458
1459 void
1460 run_breakpoint_commands (CORE_ADDR where)
1461 {
1462 /* Only check code (software or hardware) breakpoints. If one
1463 command has a problem, stop digging the hole deeper. */
1464 if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP, where))
1465 run_breakpoint_commands_z_type (Z_PACKET_HW_BP, where);
1466 }
1467
1468 /* See mem-break.h. */
1469
1470 int
1471 gdb_breakpoint_here (CORE_ADDR where)
1472 {
1473 /* Only check code (software or hardware) breakpoints. */
1474 return (find_gdb_breakpoint (Z_PACKET_SW_BP, where, -1) != NULL
1475 || find_gdb_breakpoint (Z_PACKET_HW_BP, where, -1) != NULL);
1476 }
1477
1478 void
1479 set_reinsert_breakpoint (CORE_ADDR stop_at)
1480 {
1481 struct breakpoint *bp;
1482
1483 bp = set_breakpoint_type_at (reinsert_breakpoint, stop_at, NULL);
1484 }
1485
1486 void
1487 delete_reinsert_breakpoints (void)
1488 {
1489 struct process_info *proc = current_process ();
1490 struct breakpoint *bp, **bp_link;
1491
1492 bp = proc->breakpoints;
1493 bp_link = &proc->breakpoints;
1494
1495 while (bp)
1496 {
1497 if (bp->type == reinsert_breakpoint)
1498 {
1499 *bp_link = bp->next;
1500 release_breakpoint (proc, bp);
1501 bp = *bp_link;
1502 }
1503 else
1504 {
1505 bp_link = &bp->next;
1506 bp = *bp_link;
1507 }
1508 }
1509 }
1510
1511 static void
1512 uninsert_raw_breakpoint (struct raw_breakpoint *bp)
1513 {
1514 if (bp->inserted < 0)
1515 {
1516 if (debug_threads)
1517 debug_printf ("Breakpoint at %s is marked insert-disabled.\n",
1518 paddress (bp->pc));
1519 }
1520 else if (bp->inserted > 0)
1521 {
1522 int err;
1523
1524 bp->inserted = 0;
1525
1526 err = the_target->remove_point (bp->raw_type, bp->pc, bp->kind, bp);
1527 if (err != 0)
1528 {
1529 bp->inserted = 1;
1530
1531 if (debug_threads)
1532 debug_printf ("Failed to uninsert raw breakpoint at 0x%s.\n",
1533 paddress (bp->pc));
1534 }
1535 }
1536 }
1537
1538 void
1539 uninsert_breakpoints_at (CORE_ADDR pc)
1540 {
1541 struct process_info *proc = current_process ();
1542 struct raw_breakpoint *bp;
1543 int found = 0;
1544
1545 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1546 if ((bp->raw_type == raw_bkpt_type_sw
1547 || bp->raw_type == raw_bkpt_type_hw)
1548 && bp->pc == pc)
1549 {
1550 found = 1;
1551
1552 if (bp->inserted)
1553 uninsert_raw_breakpoint (bp);
1554 }
1555
1556 if (!found)
1557 {
1558 /* This can happen when we remove all breakpoints while handling
1559 a step-over. */
1560 if (debug_threads)
1561 debug_printf ("Could not find breakpoint at 0x%s "
1562 "in list (uninserting).\n",
1563 paddress (pc));
1564 }
1565 }
1566
1567 void
1568 uninsert_all_breakpoints (void)
1569 {
1570 struct process_info *proc = current_process ();
1571 struct raw_breakpoint *bp;
1572
1573 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1574 if ((bp->raw_type == raw_bkpt_type_sw
1575 || bp->raw_type == raw_bkpt_type_hw)
1576 && bp->inserted)
1577 uninsert_raw_breakpoint (bp);
1578 }
1579
1580 void
1581 uninsert_reinsert_breakpoints (void)
1582 {
1583 struct process_info *proc = current_process ();
1584 struct breakpoint *bp;
1585
1586 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1587 {
1588 if (bp->type == reinsert_breakpoint)
1589 {
1590 gdb_assert (bp->raw->inserted > 0);
1591
1592 /* Only uninsert the raw breakpoint if it only belongs to a
1593 reinsert breakpoint. */
1594 if (bp->raw->refcount == 1)
1595 uninsert_raw_breakpoint (bp->raw);
1596 }
1597 }
1598 }
1599
1600 static void
1601 reinsert_raw_breakpoint (struct raw_breakpoint *bp)
1602 {
1603 int err;
1604
1605 if (bp->inserted)
1606 return;
1607
1608 err = the_target->insert_point (bp->raw_type, bp->pc, bp->kind, bp);
1609 if (err == 0)
1610 bp->inserted = 1;
1611 else if (debug_threads)
1612 debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).\n",
1613 paddress (bp->pc), err);
1614 }
1615
1616 void
1617 reinsert_breakpoints_at (CORE_ADDR pc)
1618 {
1619 struct process_info *proc = current_process ();
1620 struct raw_breakpoint *bp;
1621 int found = 0;
1622
1623 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1624 if ((bp->raw_type == raw_bkpt_type_sw
1625 || bp->raw_type == raw_bkpt_type_hw)
1626 && bp->pc == pc)
1627 {
1628 found = 1;
1629
1630 reinsert_raw_breakpoint (bp);
1631 }
1632
1633 if (!found)
1634 {
1635 /* This can happen when we remove all breakpoints while handling
1636 a step-over. */
1637 if (debug_threads)
1638 debug_printf ("Could not find raw breakpoint at 0x%s "
1639 "in list (reinserting).\n",
1640 paddress (pc));
1641 }
1642 }
1643
1644 int
1645 has_reinsert_breakpoints (struct process_info *proc)
1646 {
1647 struct breakpoint *bp, **bp_link;
1648
1649 bp = proc->breakpoints;
1650 bp_link = &proc->breakpoints;
1651
1652 while (bp)
1653 {
1654 if (bp->type == reinsert_breakpoint)
1655 return 1;
1656 else
1657 {
1658 bp_link = &bp->next;
1659 bp = *bp_link;
1660 }
1661 }
1662
1663 return 0;
1664 }
1665
1666 void
1667 reinsert_all_breakpoints (void)
1668 {
1669 struct process_info *proc = current_process ();
1670 struct raw_breakpoint *bp;
1671
1672 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1673 if ((bp->raw_type == raw_bkpt_type_sw
1674 || bp->raw_type == raw_bkpt_type_hw)
1675 && !bp->inserted)
1676 reinsert_raw_breakpoint (bp);
1677 }
1678
1679 void
1680 reinsert_reinsert_breakpoints (void)
1681 {
1682 struct process_info *proc = current_process ();
1683 struct breakpoint *bp;
1684
1685 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1686 {
1687 if (bp->type == reinsert_breakpoint)
1688 {
1689 gdb_assert (bp->raw->inserted > 0);
1690
1691 if (bp->raw->refcount == 1)
1692 reinsert_raw_breakpoint (bp->raw);
1693 }
1694 }
1695 }
1696
1697 void
1698 check_breakpoints (CORE_ADDR stop_pc)
1699 {
1700 struct process_info *proc = current_process ();
1701 struct breakpoint *bp, **bp_link;
1702
1703 bp = proc->breakpoints;
1704 bp_link = &proc->breakpoints;
1705
1706 while (bp)
1707 {
1708 struct raw_breakpoint *raw = bp->raw;
1709
1710 if ((raw->raw_type == raw_bkpt_type_sw
1711 || raw->raw_type == raw_bkpt_type_hw)
1712 && raw->pc == stop_pc)
1713 {
1714 if (!raw->inserted)
1715 {
1716 warning ("Hit a removed breakpoint?");
1717 return;
1718 }
1719
1720 if (bp->type == other_breakpoint)
1721 {
1722 struct other_breakpoint *other_bp
1723 = (struct other_breakpoint *) bp;
1724
1725 if (other_bp->handler != NULL && (*other_bp->handler) (stop_pc))
1726 {
1727 *bp_link = bp->next;
1728
1729 release_breakpoint (proc, bp);
1730
1731 bp = *bp_link;
1732 continue;
1733 }
1734 }
1735 }
1736
1737 bp_link = &bp->next;
1738 bp = *bp_link;
1739 }
1740 }
1741
1742 int
1743 breakpoint_here (CORE_ADDR addr)
1744 {
1745 struct process_info *proc = current_process ();
1746 struct raw_breakpoint *bp;
1747
1748 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1749 if ((bp->raw_type == raw_bkpt_type_sw
1750 || bp->raw_type == raw_bkpt_type_hw)
1751 && bp->pc == addr)
1752 return 1;
1753
1754 return 0;
1755 }
1756
1757 int
1758 breakpoint_inserted_here (CORE_ADDR addr)
1759 {
1760 struct process_info *proc = current_process ();
1761 struct raw_breakpoint *bp;
1762
1763 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1764 if ((bp->raw_type == raw_bkpt_type_sw
1765 || bp->raw_type == raw_bkpt_type_hw)
1766 && bp->pc == addr
1767 && bp->inserted)
1768 return 1;
1769
1770 return 0;
1771 }
1772
1773 /* See mem-break.h. */
1774
1775 int
1776 software_breakpoint_inserted_here (CORE_ADDR addr)
1777 {
1778 struct process_info *proc = current_process ();
1779 struct raw_breakpoint *bp;
1780
1781 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1782 if (bp->raw_type == raw_bkpt_type_sw
1783 && bp->pc == addr
1784 && bp->inserted)
1785 return 1;
1786
1787 return 0;
1788 }
1789
1790 /* See mem-break.h. */
1791
1792 int
1793 hardware_breakpoint_inserted_here (CORE_ADDR addr)
1794 {
1795 struct process_info *proc = current_process ();
1796 struct raw_breakpoint *bp;
1797
1798 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1799 if (bp->raw_type == raw_bkpt_type_hw
1800 && bp->pc == addr
1801 && bp->inserted)
1802 return 1;
1803
1804 return 0;
1805 }
1806
1807 /* See mem-break.h. */
1808
1809 int
1810 reinsert_breakpoint_inserted_here (CORE_ADDR addr)
1811 {
1812 struct process_info *proc = current_process ();
1813 struct breakpoint *bp;
1814
1815 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1816 if (bp->type == reinsert_breakpoint
1817 && bp->raw->pc == addr
1818 && bp->raw->inserted)
1819 return 1;
1820
1821 return 0;
1822 }
1823
1824 static int
1825 validate_inserted_breakpoint (struct raw_breakpoint *bp)
1826 {
1827 unsigned char *buf;
1828 int err;
1829
1830 gdb_assert (bp->inserted);
1831 gdb_assert (bp->raw_type == raw_bkpt_type_sw);
1832
1833 buf = (unsigned char *) alloca (bp_size (bp));
1834 err = (*the_target->read_memory) (bp->pc, buf, bp_size (bp));
1835 if (err || memcmp (buf, bp_opcode (bp), bp_size (bp)) != 0)
1836 {
1837 /* Tag it as gone. */
1838 bp->inserted = -1;
1839 return 0;
1840 }
1841
1842 return 1;
1843 }
1844
1845 static void
1846 delete_disabled_breakpoints (void)
1847 {
1848 struct process_info *proc = current_process ();
1849 struct breakpoint *bp, *next;
1850
1851 for (bp = proc->breakpoints; bp != NULL; bp = next)
1852 {
1853 next = bp->next;
1854 if (bp->raw->inserted < 0)
1855 {
1856 /* If reinsert_breakpoints become disabled, that means the
1857 manipulations (insertion and removal) of them are wrong. */
1858 gdb_assert (bp->type != reinsert_breakpoint);
1859 delete_breakpoint_1 (proc, bp);
1860 }
1861 }
1862 }
1863
1864 /* Check if breakpoints we inserted still appear to be inserted. They
1865 may disappear due to a shared library unload, and worse, a new
1866 shared library may be reloaded at the same address as the
1867 previously unloaded one. If that happens, we should make sure that
1868 the shadow memory of the old breakpoints isn't used when reading or
1869 writing memory. */
1870
1871 void
1872 validate_breakpoints (void)
1873 {
1874 struct process_info *proc = current_process ();
1875 struct breakpoint *bp;
1876
1877 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1878 {
1879 struct raw_breakpoint *raw = bp->raw;
1880
1881 if (raw->raw_type == raw_bkpt_type_sw && raw->inserted > 0)
1882 validate_inserted_breakpoint (raw);
1883 }
1884
1885 delete_disabled_breakpoints ();
1886 }
1887
1888 void
1889 check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
1890 {
1891 struct process_info *proc = current_process ();
1892 struct raw_breakpoint *bp = proc->raw_breakpoints;
1893 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1894 CORE_ADDR mem_end = mem_addr + mem_len;
1895 int disabled_one = 0;
1896
1897 for (; jp != NULL; jp = jp->next)
1898 {
1899 CORE_ADDR bp_end = jp->pc + jp->length;
1900 CORE_ADDR start, end;
1901 int copy_offset, copy_len, buf_offset;
1902
1903 gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len
1904 || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1905
1906 if (mem_addr >= bp_end)
1907 continue;
1908 if (jp->pc >= mem_end)
1909 continue;
1910
1911 start = jp->pc;
1912 if (mem_addr > start)
1913 start = mem_addr;
1914
1915 end = bp_end;
1916 if (end > mem_end)
1917 end = mem_end;
1918
1919 copy_len = end - start;
1920 copy_offset = start - jp->pc;
1921 buf_offset = start - mem_addr;
1922
1923 if (jp->inserted)
1924 memcpy (buf + buf_offset,
1925 fast_tracepoint_jump_shadow (jp) + copy_offset,
1926 copy_len);
1927 }
1928
1929 for (; bp != NULL; bp = bp->next)
1930 {
1931 CORE_ADDR bp_end = bp->pc + bp_size (bp);
1932 CORE_ADDR start, end;
1933 int copy_offset, copy_len, buf_offset;
1934
1935 if (bp->raw_type != raw_bkpt_type_sw)
1936 continue;
1937
1938 gdb_assert (bp->old_data >= buf + mem_len
1939 || buf >= &bp->old_data[sizeof (bp->old_data)]);
1940
1941 if (mem_addr >= bp_end)
1942 continue;
1943 if (bp->pc >= mem_end)
1944 continue;
1945
1946 start = bp->pc;
1947 if (mem_addr > start)
1948 start = mem_addr;
1949
1950 end = bp_end;
1951 if (end > mem_end)
1952 end = mem_end;
1953
1954 copy_len = end - start;
1955 copy_offset = start - bp->pc;
1956 buf_offset = start - mem_addr;
1957
1958 if (bp->inserted > 0)
1959 {
1960 if (validate_inserted_breakpoint (bp))
1961 memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
1962 else
1963 disabled_one = 1;
1964 }
1965 }
1966
1967 if (disabled_one)
1968 delete_disabled_breakpoints ();
1969 }
1970
1971 void
1972 check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
1973 const unsigned char *myaddr, int mem_len)
1974 {
1975 struct process_info *proc = current_process ();
1976 struct raw_breakpoint *bp = proc->raw_breakpoints;
1977 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1978 CORE_ADDR mem_end = mem_addr + mem_len;
1979 int disabled_one = 0;
1980
1981 /* First fast tracepoint jumps, then breakpoint traps on top. */
1982
1983 for (; jp != NULL; jp = jp->next)
1984 {
1985 CORE_ADDR jp_end = jp->pc + jp->length;
1986 CORE_ADDR start, end;
1987 int copy_offset, copy_len, buf_offset;
1988
1989 gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len
1990 || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1991 gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len
1992 || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length);
1993
1994 if (mem_addr >= jp_end)
1995 continue;
1996 if (jp->pc >= mem_end)
1997 continue;
1998
1999 start = jp->pc;
2000 if (mem_addr > start)
2001 start = mem_addr;
2002
2003 end = jp_end;
2004 if (end > mem_end)
2005 end = mem_end;
2006
2007 copy_len = end - start;
2008 copy_offset = start - jp->pc;
2009 buf_offset = start - mem_addr;
2010
2011 memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset,
2012 myaddr + buf_offset, copy_len);
2013 if (jp->inserted)
2014 memcpy (buf + buf_offset,
2015 fast_tracepoint_jump_insn (jp) + copy_offset, copy_len);
2016 }
2017
2018 for (; bp != NULL; bp = bp->next)
2019 {
2020 CORE_ADDR bp_end = bp->pc + bp_size (bp);
2021 CORE_ADDR start, end;
2022 int copy_offset, copy_len, buf_offset;
2023
2024 if (bp->raw_type != raw_bkpt_type_sw)
2025 continue;
2026
2027 gdb_assert (bp->old_data >= myaddr + mem_len
2028 || myaddr >= &bp->old_data[sizeof (bp->old_data)]);
2029
2030 if (mem_addr >= bp_end)
2031 continue;
2032 if (bp->pc >= mem_end)
2033 continue;
2034
2035 start = bp->pc;
2036 if (mem_addr > start)
2037 start = mem_addr;
2038
2039 end = bp_end;
2040 if (end > mem_end)
2041 end = mem_end;
2042
2043 copy_len = end - start;
2044 copy_offset = start - bp->pc;
2045 buf_offset = start - mem_addr;
2046
2047 memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len);
2048 if (bp->inserted > 0)
2049 {
2050 if (validate_inserted_breakpoint (bp))
2051 memcpy (buf + buf_offset, bp_opcode (bp) + copy_offset, copy_len);
2052 else
2053 disabled_one = 1;
2054 }
2055 }
2056
2057 if (disabled_one)
2058 delete_disabled_breakpoints ();
2059 }
2060
2061 /* Delete all breakpoints, and un-insert them from the inferior. */
2062
2063 void
2064 delete_all_breakpoints (void)
2065 {
2066 struct process_info *proc = current_process ();
2067
2068 while (proc->breakpoints)
2069 delete_breakpoint_1 (proc, proc->breakpoints);
2070 }
2071
2072 /* Clear the "inserted" flag in all breakpoints. */
2073
2074 void
2075 mark_breakpoints_out (struct process_info *proc)
2076 {
2077 struct raw_breakpoint *raw_bp;
2078
2079 for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next)
2080 raw_bp->inserted = 0;
2081 }
2082
2083 /* Release all breakpoints, but do not try to un-insert them from the
2084 inferior. */
2085
2086 void
2087 free_all_breakpoints (struct process_info *proc)
2088 {
2089 mark_breakpoints_out (proc);
2090
2091 /* Note: use PROC explicitly instead of deferring to
2092 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
2093 released when we get here. There should be no call to
2094 current_process from here on. */
2095 while (proc->breakpoints)
2096 delete_breakpoint_1 (proc, proc->breakpoints);
2097 }
2098
2099 /* Clone an agent expression. */
2100
2101 static struct agent_expr *
2102 clone_agent_expr (const struct agent_expr *src_ax)
2103 {
2104 struct agent_expr *ax;
2105
2106 ax = XCNEW (struct agent_expr);
2107 ax->length = src_ax->length;
2108 ax->bytes = (unsigned char *) xcalloc (ax->length, 1);
2109 memcpy (ax->bytes, src_ax->bytes, ax->length);
2110 return ax;
2111 }
2112
2113 /* Deep-copy the contents of one breakpoint to another. */
2114
2115 static struct breakpoint *
2116 clone_one_breakpoint (const struct breakpoint *src)
2117 {
2118 struct breakpoint *dest;
2119 struct raw_breakpoint *dest_raw;
2120
2121 /* Clone the raw breakpoint. */
2122 dest_raw = XCNEW (struct raw_breakpoint);
2123 dest_raw->raw_type = src->raw->raw_type;
2124 dest_raw->refcount = src->raw->refcount;
2125 dest_raw->pc = src->raw->pc;
2126 dest_raw->kind = src->raw->kind;
2127 memcpy (dest_raw->old_data, src->raw->old_data, MAX_BREAKPOINT_LEN);
2128 dest_raw->inserted = src->raw->inserted;
2129
2130 /* Clone the high-level breakpoint. */
2131 if (is_gdb_breakpoint (src->type))
2132 {
2133 struct gdb_breakpoint *gdb_dest = XCNEW (struct gdb_breakpoint);
2134 struct point_cond_list *current_cond;
2135 struct point_cond_list *new_cond;
2136 struct point_cond_list *cond_tail = NULL;
2137 struct point_command_list *current_cmd;
2138 struct point_command_list *new_cmd;
2139 struct point_command_list *cmd_tail = NULL;
2140
2141 /* Clone the condition list. */
2142 for (current_cond = ((struct gdb_breakpoint *) src)->cond_list;
2143 current_cond != NULL;
2144 current_cond = current_cond->next)
2145 {
2146 new_cond = XCNEW (struct point_cond_list);
2147 new_cond->cond = clone_agent_expr (current_cond->cond);
2148 APPEND_TO_LIST (&gdb_dest->cond_list, new_cond, cond_tail);
2149 }
2150
2151 /* Clone the command list. */
2152 for (current_cmd = ((struct gdb_breakpoint *) src)->command_list;
2153 current_cmd != NULL;
2154 current_cmd = current_cmd->next)
2155 {
2156 new_cmd = XCNEW (struct point_command_list);
2157 new_cmd->cmd = clone_agent_expr (current_cmd->cmd);
2158 new_cmd->persistence = current_cmd->persistence;
2159 APPEND_TO_LIST (&gdb_dest->command_list, new_cmd, cmd_tail);
2160 }
2161
2162 dest = (struct breakpoint *) gdb_dest;
2163 }
2164 else if (src->type == other_breakpoint)
2165 {
2166 struct other_breakpoint *other_dest = XCNEW (struct other_breakpoint);
2167
2168 other_dest->handler = ((struct other_breakpoint *) src)->handler;
2169 dest = (struct breakpoint *) other_dest;
2170 }
2171 else if (src->type == reinsert_breakpoint)
2172 {
2173 struct reinsert_breakpoint *reinsert_dest
2174 = XCNEW (struct reinsert_breakpoint);
2175
2176 dest = (struct breakpoint *) reinsert_dest;
2177 }
2178 else
2179 gdb_assert_not_reached ("unhandled breakpoint type");
2180
2181 dest->type = src->type;
2182 dest->raw = dest_raw;
2183
2184 return dest;
2185 }
2186
2187 /* See mem-break.h. */
2188
2189 void
2190 clone_all_breakpoints (struct thread_info *child_thread,
2191 const struct thread_info *parent_thread)
2192 {
2193 const struct breakpoint *bp;
2194 struct breakpoint *new_bkpt;
2195 struct breakpoint *bkpt_tail = NULL;
2196 struct raw_breakpoint *raw_bkpt_tail = NULL;
2197 struct process_info *child_proc = get_thread_process (child_thread);
2198 struct process_info *parent_proc = get_thread_process (parent_thread);
2199 struct breakpoint **new_list = &child_proc->breakpoints;
2200 struct raw_breakpoint **new_raw_list = &child_proc->raw_breakpoints;
2201
2202 for (bp = parent_proc->breakpoints; bp != NULL; bp = bp->next)
2203 {
2204 new_bkpt = clone_one_breakpoint (bp);
2205 APPEND_TO_LIST (new_list, new_bkpt, bkpt_tail);
2206 APPEND_TO_LIST (new_raw_list, new_bkpt->raw, raw_bkpt_tail);
2207 }
2208 }
This page took 0.105453 seconds and 4 git commands to generate.