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