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