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