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