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