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