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