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