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