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