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