Remove expecting_notif parameter from getpkt_or_notif_sane_1
[binutils-gdb.git] / gdbserver / mem-break.cc
1 /* Memory breakpoint operations for the remote server for GDB.
2 Copyright (C) 2002-2023 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 struct breakpoint *bp;
980 enum bkpt_type type = Z_packet_to_bkpt_type (z_type);
981
982 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
983 if (bp->type == type && bp->raw->pc == addr
984 && (kind == -1 || bp->raw->kind == kind))
985 return (struct gdb_breakpoint *) bp;
986
987 return NULL;
988 }
989
990 static int
991 z_type_supported (char z_type)
992 {
993 return (z_type >= '0' && z_type <= '4'
994 && the_target->supports_z_point_type (z_type));
995 }
996
997 /* Create a new GDB breakpoint of type Z_TYPE at ADDR with kind KIND.
998 Returns a pointer to the newly created breakpoint on success. On
999 failure returns NULL and sets *ERR to either -1 for error, or 1 if
1000 Z_TYPE breakpoints are not supported on this target. */
1001
1002 struct gdb_breakpoint *
1003 set_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind, int *err)
1004 {
1005 struct gdb_breakpoint *bp;
1006 enum bkpt_type type;
1007 enum raw_bkpt_type raw_type;
1008
1009 if (!z_type_supported (z_type))
1010 {
1011 *err = 1;
1012 return nullptr;
1013 }
1014
1015 /* If we see GDB inserting a second code breakpoint at the same
1016 address, then either: GDB is updating the breakpoint's conditions
1017 or commands; or, the first breakpoint must have disappeared due
1018 to a shared library unload. On targets where the shared
1019 libraries are handled by userspace, like SVR4, for example,
1020 GDBserver can't tell if a library was loaded or unloaded. Since
1021 we refcount raw breakpoints, we must be careful to make sure GDB
1022 breakpoints never contribute more than one reference. if we
1023 didn't do this, in case the previous breakpoint is gone due to a
1024 shared library unload, we'd just increase the refcount of the
1025 previous breakpoint at this address, but the trap was not planted
1026 in the inferior anymore, thus the breakpoint would never be hit.
1027 Note this must be careful to not create a window where
1028 breakpoints are removed from the target, for non-stop, in case
1029 the target can poke at memory while the program is running. */
1030 if (z_type == Z_PACKET_SW_BP
1031 || z_type == Z_PACKET_HW_BP)
1032 {
1033 bp = find_gdb_breakpoint (z_type, addr, -1);
1034
1035 if (bp != NULL)
1036 {
1037 if (bp->base.raw->kind != kind)
1038 {
1039 /* A different kind than previously seen. The previous
1040 breakpoint must be gone then. */
1041 bp->base.raw->inserted = -1;
1042 delete_breakpoint ((struct breakpoint *) bp);
1043 bp = NULL;
1044 }
1045 else if (z_type == Z_PACKET_SW_BP)
1046 {
1047 /* Check if the breakpoint is actually gone from the
1048 target, due to an solib unload, for example. Might
1049 as well validate _all_ breakpoints. */
1050 validate_breakpoints ();
1051
1052 /* Breakpoints that don't pass validation are
1053 deleted. */
1054 bp = find_gdb_breakpoint (z_type, addr, -1);
1055 }
1056 }
1057 }
1058 else
1059 {
1060 /* Data breakpoints for the same address but different kind are
1061 expected. GDB doesn't merge these. The backend gets to do
1062 that if it wants/can. */
1063 bp = find_gdb_breakpoint (z_type, addr, kind);
1064 }
1065
1066 if (bp != NULL)
1067 {
1068 /* We already know about this breakpoint, there's nothing else
1069 to do - GDB's reference is already accounted for. Note that
1070 whether the breakpoint inserted is left as is - we may be
1071 stepping over it, for example, in which case we don't want to
1072 force-reinsert it. */
1073 return bp;
1074 }
1075
1076 raw_type = Z_packet_to_raw_bkpt_type (z_type);
1077 type = Z_packet_to_bkpt_type (z_type);
1078 return (struct gdb_breakpoint *) set_breakpoint (type, raw_type, addr,
1079 kind, NULL, err);
1080 }
1081
1082 /* Delete a GDB breakpoint of type Z_TYPE and kind KIND previously
1083 inserted at ADDR with set_gdb_breakpoint_at. Returns 0 on success,
1084 -1 on error, and 1 if Z_TYPE breakpoints are not supported on this
1085 target. */
1086
1087 int
1088 delete_gdb_breakpoint (char z_type, CORE_ADDR addr, int kind)
1089 {
1090 if (!z_type_supported (z_type))
1091 return 1;
1092
1093 gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, kind);
1094 if (bp == NULL)
1095 return -1;
1096
1097 /* Before deleting the breakpoint, make sure to free its condition
1098 and command lists. */
1099 clear_breakpoint_conditions_and_commands (bp);
1100 int err = delete_breakpoint ((struct breakpoint *) bp);
1101 if (err != 0)
1102 return -1;
1103
1104 return 0;
1105 }
1106
1107 /* Clear all conditions associated with a breakpoint. */
1108
1109 static void
1110 clear_breakpoint_conditions (struct gdb_breakpoint *bp)
1111 {
1112 struct point_cond_list *cond;
1113
1114 if (bp->cond_list == NULL)
1115 return;
1116
1117 cond = bp->cond_list;
1118
1119 while (cond != NULL)
1120 {
1121 struct point_cond_list *cond_next;
1122
1123 cond_next = cond->next;
1124 gdb_free_agent_expr (cond->cond);
1125 free (cond);
1126 cond = cond_next;
1127 }
1128
1129 bp->cond_list = NULL;
1130 }
1131
1132 /* Clear all commands associated with a breakpoint. */
1133
1134 static void
1135 clear_breakpoint_commands (struct gdb_breakpoint *bp)
1136 {
1137 struct point_command_list *cmd;
1138
1139 if (bp->command_list == NULL)
1140 return;
1141
1142 cmd = bp->command_list;
1143
1144 while (cmd != NULL)
1145 {
1146 struct point_command_list *cmd_next;
1147
1148 cmd_next = cmd->next;
1149 gdb_free_agent_expr (cmd->cmd);
1150 free (cmd);
1151 cmd = cmd_next;
1152 }
1153
1154 bp->command_list = NULL;
1155 }
1156
1157 void
1158 clear_breakpoint_conditions_and_commands (struct gdb_breakpoint *bp)
1159 {
1160 clear_breakpoint_conditions (bp);
1161 clear_breakpoint_commands (bp);
1162 }
1163
1164 /* Add condition CONDITION to GDBserver's breakpoint BP. */
1165
1166 static void
1167 add_condition_to_breakpoint (struct gdb_breakpoint *bp,
1168 struct agent_expr *condition)
1169 {
1170 struct point_cond_list *new_cond;
1171
1172 /* Create new condition. */
1173 new_cond = XCNEW (struct point_cond_list);
1174 new_cond->cond = condition;
1175
1176 /* Add condition to the list. */
1177 new_cond->next = bp->cond_list;
1178 bp->cond_list = new_cond;
1179 }
1180
1181 /* Add a target-side condition CONDITION to a breakpoint. */
1182
1183 int
1184 add_breakpoint_condition (struct gdb_breakpoint *bp, const char **condition)
1185 {
1186 const char *actparm = *condition;
1187 struct agent_expr *cond;
1188
1189 if (condition == NULL)
1190 return 1;
1191
1192 if (bp == NULL)
1193 return 0;
1194
1195 cond = gdb_parse_agent_expr (&actparm);
1196
1197 if (cond == NULL)
1198 {
1199 warning ("Condition evaluation failed. Assuming unconditional.");
1200 return 0;
1201 }
1202
1203 add_condition_to_breakpoint (bp, cond);
1204
1205 *condition = actparm;
1206
1207 return 1;
1208 }
1209
1210 /* Evaluate condition (if any) at breakpoint BP. Return 1 if
1211 true and 0 otherwise. */
1212
1213 static int
1214 gdb_condition_true_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
1215 {
1216 /* Fetch registers for the current inferior. */
1217 struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1218 ULONGEST value = 0;
1219 struct point_cond_list *cl;
1220 int err = 0;
1221 struct eval_agent_expr_context ctx;
1222
1223 if (bp == NULL)
1224 return 0;
1225
1226 /* Check if the breakpoint is unconditional. If it is,
1227 the condition always evaluates to TRUE. */
1228 if (bp->cond_list == NULL)
1229 return 1;
1230
1231 ctx.regcache = get_thread_regcache (current_thread, 1);
1232 ctx.tframe = NULL;
1233 ctx.tpoint = NULL;
1234
1235 /* Evaluate each condition in the breakpoint's list of conditions.
1236 Return true if any of the conditions evaluates to TRUE.
1237
1238 If we failed to evaluate the expression, TRUE is returned. This
1239 forces GDB to reevaluate the conditions. */
1240 for (cl = bp->cond_list;
1241 cl && !value && !err; cl = cl->next)
1242 {
1243 /* Evaluate the condition. */
1244 err = gdb_eval_agent_expr (&ctx, cl->cond, &value);
1245 }
1246
1247 if (err)
1248 return 1;
1249
1250 return (value != 0);
1251 }
1252
1253 int
1254 gdb_condition_true_at_breakpoint (CORE_ADDR where)
1255 {
1256 /* Only check code (software or hardware) breakpoints. */
1257 return (gdb_condition_true_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
1258 || gdb_condition_true_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
1259 }
1260
1261 /* Add commands COMMANDS to GDBserver's breakpoint BP. */
1262
1263 static void
1264 add_commands_to_breakpoint (struct gdb_breakpoint *bp,
1265 struct agent_expr *commands, int persist)
1266 {
1267 struct point_command_list *new_cmd;
1268
1269 /* Create new command. */
1270 new_cmd = XCNEW (struct point_command_list);
1271 new_cmd->cmd = commands;
1272 new_cmd->persistence = persist;
1273
1274 /* Add commands to the list. */
1275 new_cmd->next = bp->command_list;
1276 bp->command_list = new_cmd;
1277 }
1278
1279 /* Add a target-side command COMMAND to the breakpoint at ADDR. */
1280
1281 int
1282 add_breakpoint_commands (struct gdb_breakpoint *bp, const char **command,
1283 int persist)
1284 {
1285 const char *actparm = *command;
1286 struct agent_expr *cmd;
1287
1288 if (command == NULL)
1289 return 1;
1290
1291 if (bp == NULL)
1292 return 0;
1293
1294 cmd = gdb_parse_agent_expr (&actparm);
1295
1296 if (cmd == NULL)
1297 {
1298 warning ("Command evaluation failed. Disabling.");
1299 return 0;
1300 }
1301
1302 add_commands_to_breakpoint (bp, cmd, persist);
1303
1304 *command = actparm;
1305
1306 return 1;
1307 }
1308
1309 /* Return true if there are no commands to run at this location,
1310 which likely means we want to report back to GDB. */
1311
1312 static int
1313 gdb_no_commands_at_breakpoint_z_type (char z_type, CORE_ADDR addr)
1314 {
1315 struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1316
1317 if (bp == NULL)
1318 return 1;
1319
1320 threads_debug_printf ("at 0x%s, type Z%c, bp command_list is 0x%s",
1321 paddress (addr), z_type,
1322 phex_nz ((uintptr_t) bp->command_list, 0));
1323 return (bp->command_list == NULL);
1324 }
1325
1326 /* Return true if there are no commands to run at this location,
1327 which likely means we want to report back to GDB. */
1328
1329 int
1330 gdb_no_commands_at_breakpoint (CORE_ADDR where)
1331 {
1332 /* Only check code (software or hardware) breakpoints. */
1333 return (gdb_no_commands_at_breakpoint_z_type (Z_PACKET_SW_BP, where)
1334 && gdb_no_commands_at_breakpoint_z_type (Z_PACKET_HW_BP, where));
1335 }
1336
1337 /* Run a breakpoint's commands. Returns 0 if there was a problem
1338 running any command, 1 otherwise. */
1339
1340 static int
1341 run_breakpoint_commands_z_type (char z_type, CORE_ADDR addr)
1342 {
1343 /* Fetch registers for the current inferior. */
1344 struct gdb_breakpoint *bp = find_gdb_breakpoint (z_type, addr, -1);
1345 ULONGEST value = 0;
1346 struct point_command_list *cl;
1347 int err = 0;
1348 struct eval_agent_expr_context ctx;
1349
1350 if (bp == NULL)
1351 return 1;
1352
1353 ctx.regcache = get_thread_regcache (current_thread, 1);
1354 ctx.tframe = NULL;
1355 ctx.tpoint = NULL;
1356
1357 for (cl = bp->command_list;
1358 cl && !value && !err; cl = cl->next)
1359 {
1360 /* Run the command. */
1361 err = gdb_eval_agent_expr (&ctx, cl->cmd, &value);
1362
1363 /* If one command has a problem, stop digging the hole deeper. */
1364 if (err)
1365 return 0;
1366 }
1367
1368 return 1;
1369 }
1370
1371 void
1372 run_breakpoint_commands (CORE_ADDR where)
1373 {
1374 /* Only check code (software or hardware) breakpoints. If one
1375 command has a problem, stop digging the hole deeper. */
1376 if (run_breakpoint_commands_z_type (Z_PACKET_SW_BP, where))
1377 run_breakpoint_commands_z_type (Z_PACKET_HW_BP, where);
1378 }
1379
1380 /* See mem-break.h. */
1381
1382 int
1383 gdb_breakpoint_here (CORE_ADDR where)
1384 {
1385 /* Only check code (software or hardware) breakpoints. */
1386 return (find_gdb_breakpoint (Z_PACKET_SW_BP, where, -1) != NULL
1387 || find_gdb_breakpoint (Z_PACKET_HW_BP, where, -1) != NULL);
1388 }
1389
1390 void
1391 set_single_step_breakpoint (CORE_ADDR stop_at, ptid_t ptid)
1392 {
1393 struct single_step_breakpoint *bp;
1394
1395 gdb_assert (current_ptid.pid () == ptid.pid ());
1396
1397 bp = (struct single_step_breakpoint *) set_breakpoint_type_at (single_step_breakpoint,
1398 stop_at, NULL);
1399 bp->ptid = ptid;
1400 }
1401
1402 void
1403 delete_single_step_breakpoints (struct thread_info *thread)
1404 {
1405 struct process_info *proc = get_thread_process (thread);
1406 struct breakpoint *bp, **bp_link;
1407
1408 bp = proc->breakpoints;
1409 bp_link = &proc->breakpoints;
1410
1411 while (bp)
1412 {
1413 if (bp->type == single_step_breakpoint
1414 && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1415 {
1416 scoped_restore_current_thread restore_thread;
1417
1418 switch_to_thread (thread);
1419 *bp_link = bp->next;
1420 release_breakpoint (proc, bp);
1421 bp = *bp_link;
1422 }
1423 else
1424 {
1425 bp_link = &bp->next;
1426 bp = *bp_link;
1427 }
1428 }
1429 }
1430
1431 static void
1432 uninsert_raw_breakpoint (struct raw_breakpoint *bp)
1433 {
1434 if (bp->inserted < 0)
1435 {
1436 threads_debug_printf ("Breakpoint at %s is marked insert-disabled.",
1437 paddress (bp->pc));
1438 }
1439 else if (bp->inserted > 0)
1440 {
1441 int err;
1442
1443 bp->inserted = 0;
1444
1445 err = the_target->remove_point (bp->raw_type, bp->pc, bp->kind, bp);
1446 if (err != 0)
1447 {
1448 bp->inserted = 1;
1449
1450 threads_debug_printf ("Failed to uninsert raw breakpoint at 0x%s.",
1451 paddress (bp->pc));
1452 }
1453 }
1454 }
1455
1456 void
1457 uninsert_breakpoints_at (CORE_ADDR pc)
1458 {
1459 struct process_info *proc = current_process ();
1460 struct raw_breakpoint *bp;
1461 int found = 0;
1462
1463 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1464 if ((bp->raw_type == raw_bkpt_type_sw
1465 || bp->raw_type == raw_bkpt_type_hw)
1466 && bp->pc == pc)
1467 {
1468 found = 1;
1469
1470 if (bp->inserted)
1471 uninsert_raw_breakpoint (bp);
1472 }
1473
1474 if (!found)
1475 {
1476 /* This can happen when we remove all breakpoints while handling
1477 a step-over. */
1478 threads_debug_printf ("Could not find breakpoint at 0x%s "
1479 "in list (uninserting).",
1480 paddress (pc));
1481 }
1482 }
1483
1484 void
1485 uninsert_all_breakpoints (void)
1486 {
1487 struct process_info *proc = current_process ();
1488 struct raw_breakpoint *bp;
1489
1490 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1491 if ((bp->raw_type == raw_bkpt_type_sw
1492 || bp->raw_type == raw_bkpt_type_hw)
1493 && bp->inserted)
1494 uninsert_raw_breakpoint (bp);
1495 }
1496
1497 void
1498 uninsert_single_step_breakpoints (struct thread_info *thread)
1499 {
1500 struct process_info *proc = get_thread_process (thread);
1501 struct breakpoint *bp;
1502
1503 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1504 {
1505 if (bp->type == single_step_breakpoint
1506 && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1507 {
1508 gdb_assert (bp->raw->inserted > 0);
1509
1510 /* Only uninsert the raw breakpoint if it only belongs to a
1511 reinsert breakpoint. */
1512 if (bp->raw->refcount == 1)
1513 {
1514 scoped_restore_current_thread restore_thread;
1515
1516 switch_to_thread (thread);
1517 uninsert_raw_breakpoint (bp->raw);
1518 }
1519 }
1520 }
1521 }
1522
1523 static void
1524 reinsert_raw_breakpoint (struct raw_breakpoint *bp)
1525 {
1526 int err;
1527
1528 if (bp->inserted)
1529 return;
1530
1531 err = the_target->insert_point (bp->raw_type, bp->pc, bp->kind, bp);
1532 if (err == 0)
1533 bp->inserted = 1;
1534 else
1535 threads_debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).",
1536 paddress (bp->pc), err);
1537 }
1538
1539 void
1540 reinsert_breakpoints_at (CORE_ADDR pc)
1541 {
1542 struct process_info *proc = current_process ();
1543 struct raw_breakpoint *bp;
1544 int found = 0;
1545
1546 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1547 if ((bp->raw_type == raw_bkpt_type_sw
1548 || bp->raw_type == raw_bkpt_type_hw)
1549 && bp->pc == pc)
1550 {
1551 found = 1;
1552
1553 reinsert_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 threads_debug_printf ("Could not find raw breakpoint at 0x%s "
1561 "in list (reinserting).",
1562 paddress (pc));
1563 }
1564 }
1565
1566 int
1567 has_single_step_breakpoints (struct thread_info *thread)
1568 {
1569 struct process_info *proc = get_thread_process (thread);
1570 struct breakpoint *bp, **bp_link;
1571
1572 bp = proc->breakpoints;
1573 bp_link = &proc->breakpoints;
1574
1575 while (bp)
1576 {
1577 if (bp->type == single_step_breakpoint
1578 && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1579 return 1;
1580 else
1581 {
1582 bp_link = &bp->next;
1583 bp = *bp_link;
1584 }
1585 }
1586
1587 return 0;
1588 }
1589
1590 void
1591 reinsert_all_breakpoints (void)
1592 {
1593 struct process_info *proc = current_process ();
1594 struct raw_breakpoint *bp;
1595
1596 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1597 if ((bp->raw_type == raw_bkpt_type_sw
1598 || bp->raw_type == raw_bkpt_type_hw)
1599 && !bp->inserted)
1600 reinsert_raw_breakpoint (bp);
1601 }
1602
1603 void
1604 reinsert_single_step_breakpoints (struct thread_info *thread)
1605 {
1606 struct process_info *proc = get_thread_process (thread);
1607 struct breakpoint *bp;
1608
1609 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1610 {
1611 if (bp->type == single_step_breakpoint
1612 && ((struct single_step_breakpoint *) bp)->ptid == ptid_of (thread))
1613 {
1614 gdb_assert (bp->raw->inserted > 0);
1615
1616 if (bp->raw->refcount == 1)
1617 {
1618 scoped_restore_current_thread restore_thread;
1619
1620 switch_to_thread (thread);
1621 reinsert_raw_breakpoint (bp->raw);
1622 }
1623 }
1624 }
1625 }
1626
1627 void
1628 check_breakpoints (CORE_ADDR stop_pc)
1629 {
1630 struct process_info *proc = current_process ();
1631 struct breakpoint *bp, **bp_link;
1632
1633 bp = proc->breakpoints;
1634 bp_link = &proc->breakpoints;
1635
1636 while (bp)
1637 {
1638 struct raw_breakpoint *raw = bp->raw;
1639
1640 if ((raw->raw_type == raw_bkpt_type_sw
1641 || raw->raw_type == raw_bkpt_type_hw)
1642 && raw->pc == stop_pc)
1643 {
1644 if (!raw->inserted)
1645 {
1646 warning ("Hit a removed breakpoint?");
1647 return;
1648 }
1649
1650 if (bp->type == other_breakpoint)
1651 {
1652 struct other_breakpoint *other_bp
1653 = (struct other_breakpoint *) bp;
1654
1655 if (other_bp->handler != NULL && (*other_bp->handler) (stop_pc))
1656 {
1657 *bp_link = bp->next;
1658
1659 release_breakpoint (proc, bp);
1660
1661 bp = *bp_link;
1662 continue;
1663 }
1664 }
1665 }
1666
1667 bp_link = &bp->next;
1668 bp = *bp_link;
1669 }
1670 }
1671
1672 int
1673 breakpoint_here (CORE_ADDR addr)
1674 {
1675 struct process_info *proc = current_process ();
1676 struct raw_breakpoint *bp;
1677
1678 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1679 if ((bp->raw_type == raw_bkpt_type_sw
1680 || bp->raw_type == raw_bkpt_type_hw)
1681 && bp->pc == addr)
1682 return 1;
1683
1684 return 0;
1685 }
1686
1687 int
1688 breakpoint_inserted_here (CORE_ADDR addr)
1689 {
1690 struct process_info *proc = current_process ();
1691 struct raw_breakpoint *bp;
1692
1693 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1694 if ((bp->raw_type == raw_bkpt_type_sw
1695 || bp->raw_type == raw_bkpt_type_hw)
1696 && bp->pc == addr
1697 && bp->inserted)
1698 return 1;
1699
1700 return 0;
1701 }
1702
1703 /* See mem-break.h. */
1704
1705 int
1706 software_breakpoint_inserted_here (CORE_ADDR addr)
1707 {
1708 struct process_info *proc = current_process ();
1709 struct raw_breakpoint *bp;
1710
1711 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1712 if (bp->raw_type == raw_bkpt_type_sw
1713 && bp->pc == addr
1714 && bp->inserted)
1715 return 1;
1716
1717 return 0;
1718 }
1719
1720 /* See mem-break.h. */
1721
1722 int
1723 hardware_breakpoint_inserted_here (CORE_ADDR addr)
1724 {
1725 struct process_info *proc = current_process ();
1726 struct raw_breakpoint *bp;
1727
1728 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1729 if (bp->raw_type == raw_bkpt_type_hw
1730 && bp->pc == addr
1731 && bp->inserted)
1732 return 1;
1733
1734 return 0;
1735 }
1736
1737 /* See mem-break.h. */
1738
1739 int
1740 single_step_breakpoint_inserted_here (CORE_ADDR addr)
1741 {
1742 struct process_info *proc = current_process ();
1743 struct breakpoint *bp;
1744
1745 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1746 if (bp->type == single_step_breakpoint
1747 && bp->raw->pc == addr
1748 && bp->raw->inserted)
1749 return 1;
1750
1751 return 0;
1752 }
1753
1754 static int
1755 validate_inserted_breakpoint (struct raw_breakpoint *bp)
1756 {
1757 unsigned char *buf;
1758 int err;
1759
1760 gdb_assert (bp->inserted);
1761 gdb_assert (bp->raw_type == raw_bkpt_type_sw);
1762
1763 buf = (unsigned char *) alloca (bp_size (bp));
1764 err = the_target->read_memory (bp->pc, buf, bp_size (bp));
1765 if (err || memcmp (buf, bp_opcode (bp), bp_size (bp)) != 0)
1766 {
1767 /* Tag it as gone. */
1768 bp->inserted = -1;
1769 return 0;
1770 }
1771
1772 return 1;
1773 }
1774
1775 static void
1776 delete_disabled_breakpoints (void)
1777 {
1778 struct process_info *proc = current_process ();
1779 struct breakpoint *bp, *next;
1780
1781 for (bp = proc->breakpoints; bp != NULL; bp = next)
1782 {
1783 next = bp->next;
1784 if (bp->raw->inserted < 0)
1785 {
1786 /* If single_step_breakpoints become disabled, that means the
1787 manipulations (insertion and removal) of them are wrong. */
1788 gdb_assert (bp->type != single_step_breakpoint);
1789 delete_breakpoint_1 (proc, bp);
1790 }
1791 }
1792 }
1793
1794 /* Check if breakpoints we inserted still appear to be inserted. They
1795 may disappear due to a shared library unload, and worse, a new
1796 shared library may be reloaded at the same address as the
1797 previously unloaded one. If that happens, we should make sure that
1798 the shadow memory of the old breakpoints isn't used when reading or
1799 writing memory. */
1800
1801 void
1802 validate_breakpoints (void)
1803 {
1804 struct process_info *proc = current_process ();
1805 struct breakpoint *bp;
1806
1807 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1808 {
1809 struct raw_breakpoint *raw = bp->raw;
1810
1811 if (raw->raw_type == raw_bkpt_type_sw && raw->inserted > 0)
1812 validate_inserted_breakpoint (raw);
1813 }
1814
1815 delete_disabled_breakpoints ();
1816 }
1817
1818 void
1819 check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
1820 {
1821 struct process_info *proc = current_process ();
1822 struct raw_breakpoint *bp = proc->raw_breakpoints;
1823 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1824 CORE_ADDR mem_end = mem_addr + mem_len;
1825 int disabled_one = 0;
1826
1827 for (; jp != NULL; jp = jp->next)
1828 {
1829 CORE_ADDR bp_end = jp->pc + jp->length;
1830 CORE_ADDR start, end;
1831 int copy_offset, copy_len, buf_offset;
1832
1833 gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len
1834 || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1835
1836 if (mem_addr >= bp_end)
1837 continue;
1838 if (jp->pc >= mem_end)
1839 continue;
1840
1841 start = jp->pc;
1842 if (mem_addr > start)
1843 start = mem_addr;
1844
1845 end = bp_end;
1846 if (end > mem_end)
1847 end = mem_end;
1848
1849 copy_len = end - start;
1850 copy_offset = start - jp->pc;
1851 buf_offset = start - mem_addr;
1852
1853 if (jp->inserted)
1854 memcpy (buf + buf_offset,
1855 fast_tracepoint_jump_shadow (jp) + copy_offset,
1856 copy_len);
1857 }
1858
1859 for (; bp != NULL; bp = bp->next)
1860 {
1861 CORE_ADDR bp_end = bp->pc + bp_size (bp);
1862 CORE_ADDR start, end;
1863 int copy_offset, copy_len, buf_offset;
1864
1865 if (bp->raw_type != raw_bkpt_type_sw)
1866 continue;
1867
1868 gdb_assert (bp->old_data >= buf + mem_len
1869 || buf >= &bp->old_data[sizeof (bp->old_data)]);
1870
1871 if (mem_addr >= bp_end)
1872 continue;
1873 if (bp->pc >= mem_end)
1874 continue;
1875
1876 start = bp->pc;
1877 if (mem_addr > start)
1878 start = mem_addr;
1879
1880 end = bp_end;
1881 if (end > mem_end)
1882 end = mem_end;
1883
1884 copy_len = end - start;
1885 copy_offset = start - bp->pc;
1886 buf_offset = start - mem_addr;
1887
1888 if (bp->inserted > 0)
1889 {
1890 if (validate_inserted_breakpoint (bp))
1891 memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
1892 else
1893 disabled_one = 1;
1894 }
1895 }
1896
1897 if (disabled_one)
1898 delete_disabled_breakpoints ();
1899 }
1900
1901 void
1902 check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
1903 const unsigned char *myaddr, int mem_len)
1904 {
1905 struct process_info *proc = current_process ();
1906 struct raw_breakpoint *bp = proc->raw_breakpoints;
1907 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
1908 CORE_ADDR mem_end = mem_addr + mem_len;
1909 int disabled_one = 0;
1910
1911 /* First fast tracepoint jumps, then breakpoint traps on top. */
1912
1913 for (; jp != NULL; jp = jp->next)
1914 {
1915 CORE_ADDR jp_end = jp->pc + jp->length;
1916 CORE_ADDR start, end;
1917 int copy_offset, copy_len, buf_offset;
1918
1919 gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len
1920 || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1921 gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len
1922 || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length);
1923
1924 if (mem_addr >= jp_end)
1925 continue;
1926 if (jp->pc >= mem_end)
1927 continue;
1928
1929 start = jp->pc;
1930 if (mem_addr > start)
1931 start = mem_addr;
1932
1933 end = jp_end;
1934 if (end > mem_end)
1935 end = mem_end;
1936
1937 copy_len = end - start;
1938 copy_offset = start - jp->pc;
1939 buf_offset = start - mem_addr;
1940
1941 memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset,
1942 myaddr + buf_offset, copy_len);
1943 if (jp->inserted)
1944 memcpy (buf + buf_offset,
1945 fast_tracepoint_jump_insn (jp) + copy_offset, copy_len);
1946 }
1947
1948 for (; bp != NULL; bp = bp->next)
1949 {
1950 CORE_ADDR bp_end = bp->pc + bp_size (bp);
1951 CORE_ADDR start, end;
1952 int copy_offset, copy_len, buf_offset;
1953
1954 if (bp->raw_type != raw_bkpt_type_sw)
1955 continue;
1956
1957 gdb_assert (bp->old_data >= myaddr + mem_len
1958 || myaddr >= &bp->old_data[sizeof (bp->old_data)]);
1959
1960 if (mem_addr >= bp_end)
1961 continue;
1962 if (bp->pc >= mem_end)
1963 continue;
1964
1965 start = bp->pc;
1966 if (mem_addr > start)
1967 start = mem_addr;
1968
1969 end = bp_end;
1970 if (end > mem_end)
1971 end = mem_end;
1972
1973 copy_len = end - start;
1974 copy_offset = start - bp->pc;
1975 buf_offset = start - mem_addr;
1976
1977 memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len);
1978 if (bp->inserted > 0)
1979 {
1980 if (validate_inserted_breakpoint (bp))
1981 memcpy (buf + buf_offset, bp_opcode (bp) + copy_offset, copy_len);
1982 else
1983 disabled_one = 1;
1984 }
1985 }
1986
1987 if (disabled_one)
1988 delete_disabled_breakpoints ();
1989 }
1990
1991 /* Delete all breakpoints, and un-insert them from the inferior. */
1992
1993 void
1994 delete_all_breakpoints (void)
1995 {
1996 struct process_info *proc = current_process ();
1997
1998 while (proc->breakpoints)
1999 delete_breakpoint_1 (proc, proc->breakpoints);
2000 }
2001
2002 /* Clear the "inserted" flag in all breakpoints. */
2003
2004 void
2005 mark_breakpoints_out (struct process_info *proc)
2006 {
2007 struct raw_breakpoint *raw_bp;
2008
2009 for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next)
2010 raw_bp->inserted = 0;
2011 }
2012
2013 /* Release all breakpoints, but do not try to un-insert them from the
2014 inferior. */
2015
2016 void
2017 free_all_breakpoints (struct process_info *proc)
2018 {
2019 mark_breakpoints_out (proc);
2020
2021 /* Note: use PROC explicitly instead of deferring to
2022 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
2023 released when we get here. There should be no call to
2024 current_process from here on. */
2025 while (proc->breakpoints)
2026 delete_breakpoint_1 (proc, proc->breakpoints);
2027 }
2028
2029 /* Clone an agent expression. */
2030
2031 static struct agent_expr *
2032 clone_agent_expr (const struct agent_expr *src_ax)
2033 {
2034 struct agent_expr *ax;
2035
2036 ax = XCNEW (struct agent_expr);
2037 ax->length = src_ax->length;
2038 ax->bytes = (unsigned char *) xcalloc (ax->length, 1);
2039 memcpy (ax->bytes, src_ax->bytes, ax->length);
2040 return ax;
2041 }
2042
2043 /* Deep-copy the contents of one breakpoint to another. */
2044
2045 static struct breakpoint *
2046 clone_one_breakpoint (const struct breakpoint *src, ptid_t ptid)
2047 {
2048 struct breakpoint *dest;
2049 struct raw_breakpoint *dest_raw;
2050
2051 /* Clone the raw breakpoint. */
2052 dest_raw = XCNEW (struct raw_breakpoint);
2053 dest_raw->raw_type = src->raw->raw_type;
2054 dest_raw->refcount = src->raw->refcount;
2055 dest_raw->pc = src->raw->pc;
2056 dest_raw->kind = src->raw->kind;
2057 memcpy (dest_raw->old_data, src->raw->old_data, MAX_BREAKPOINT_LEN);
2058 dest_raw->inserted = src->raw->inserted;
2059
2060 /* Clone the high-level breakpoint. */
2061 if (is_gdb_breakpoint (src->type))
2062 {
2063 struct gdb_breakpoint *gdb_dest = XCNEW (struct gdb_breakpoint);
2064 struct point_cond_list *current_cond;
2065 struct point_cond_list *new_cond;
2066 struct point_cond_list *cond_tail = NULL;
2067 struct point_command_list *current_cmd;
2068 struct point_command_list *new_cmd;
2069 struct point_command_list *cmd_tail = NULL;
2070
2071 /* Clone the condition list. */
2072 for (current_cond = ((struct gdb_breakpoint *) src)->cond_list;
2073 current_cond != NULL;
2074 current_cond = current_cond->next)
2075 {
2076 new_cond = XCNEW (struct point_cond_list);
2077 new_cond->cond = clone_agent_expr (current_cond->cond);
2078 APPEND_TO_LIST (&gdb_dest->cond_list, new_cond, cond_tail);
2079 }
2080
2081 /* Clone the command list. */
2082 for (current_cmd = ((struct gdb_breakpoint *) src)->command_list;
2083 current_cmd != NULL;
2084 current_cmd = current_cmd->next)
2085 {
2086 new_cmd = XCNEW (struct point_command_list);
2087 new_cmd->cmd = clone_agent_expr (current_cmd->cmd);
2088 new_cmd->persistence = current_cmd->persistence;
2089 APPEND_TO_LIST (&gdb_dest->command_list, new_cmd, cmd_tail);
2090 }
2091
2092 dest = (struct breakpoint *) gdb_dest;
2093 }
2094 else if (src->type == other_breakpoint)
2095 {
2096 struct other_breakpoint *other_dest = XCNEW (struct other_breakpoint);
2097
2098 other_dest->handler = ((struct other_breakpoint *) src)->handler;
2099 dest = (struct breakpoint *) other_dest;
2100 }
2101 else if (src->type == single_step_breakpoint)
2102 {
2103 struct single_step_breakpoint *ss_dest
2104 = XCNEW (struct single_step_breakpoint);
2105
2106 dest = (struct breakpoint *) ss_dest;
2107 /* Since single-step breakpoint is thread specific, don't copy
2108 thread id from SRC, use ID instead. */
2109 ss_dest->ptid = ptid;
2110 }
2111 else
2112 gdb_assert_not_reached ("unhandled breakpoint type");
2113
2114 dest->type = src->type;
2115 dest->raw = dest_raw;
2116
2117 return dest;
2118 }
2119
2120 /* See mem-break.h. */
2121
2122 void
2123 clone_all_breakpoints (struct thread_info *child_thread,
2124 const struct thread_info *parent_thread)
2125 {
2126 const struct breakpoint *bp;
2127 struct breakpoint *new_bkpt;
2128 struct breakpoint *bkpt_tail = NULL;
2129 struct raw_breakpoint *raw_bkpt_tail = NULL;
2130 struct process_info *child_proc = get_thread_process (child_thread);
2131 struct process_info *parent_proc = get_thread_process (parent_thread);
2132 struct breakpoint **new_list = &child_proc->breakpoints;
2133 struct raw_breakpoint **new_raw_list = &child_proc->raw_breakpoints;
2134
2135 for (bp = parent_proc->breakpoints; bp != NULL; bp = bp->next)
2136 {
2137 new_bkpt = clone_one_breakpoint (bp, ptid_of (child_thread));
2138 APPEND_TO_LIST (new_list, new_bkpt, bkpt_tail);
2139 APPEND_TO_LIST (new_raw_list, new_bkpt->raw, raw_bkpt_tail);
2140 }
2141 }