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