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