Debian Codegraph @

gdbserver/mem-break.cc

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