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