Remove pid_to_ptid
[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
YQ
1484
1485 gdb_assert (ptid_get_pid (current_ptid) == ptid_get_pid (ptid));
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
YQ
1503 if (bp->type == single_step_breakpoint
1504 && ptid_equal (((struct single_step_breakpoint *) bp)->ptid,
bec903c9 1505 ptid_of (thread)))
414a389f 1506 {
bec903c9
YQ
1507 struct thread_info *saved_thread = current_thread;
1508
1509 current_thread = thread;
414a389f 1510 *bp_link = bp->next;
8b07ae33 1511 release_breakpoint (proc, bp);
414a389f 1512 bp = *bp_link;
bec903c9 1513 current_thread = saved_thread;
414a389f
PA
1514 }
1515 else
1516 {
1517 bp_link = &bp->next;
1518 bp = *bp_link;
1519 }
d50171e4
PA
1520 }
1521}
b65d95c5 1522
d50171e4 1523static void
8b07ae33 1524uninsert_raw_breakpoint (struct raw_breakpoint *bp)
d50171e4 1525{
802e8e6d
PA
1526 if (bp->inserted < 0)
1527 {
1528 if (debug_threads)
1529 debug_printf ("Breakpoint at %s is marked insert-disabled.\n",
1530 paddress (bp->pc));
1531 }
1532 else if (bp->inserted > 0)
d50171e4
PA
1533 {
1534 int err;
1535
1536 bp->inserted = 0;
802e8e6d 1537
27165294 1538 err = the_target->remove_point (bp->raw_type, bp->pc, bp->kind, bp);
d50171e4
PA
1539 if (err != 0)
1540 {
1541 bp->inserted = 1;
611cb4a5 1542
d50171e4 1543 if (debug_threads)
802e8e6d
PA
1544 debug_printf ("Failed to uninsert raw breakpoint at 0x%s.\n",
1545 paddress (bp->pc));
d50171e4
PA
1546 }
1547 }
611cb4a5
DJ
1548}
1549
1550void
d50171e4 1551uninsert_breakpoints_at (CORE_ADDR pc)
611cb4a5 1552{
802e8e6d 1553 struct process_info *proc = current_process ();
8b07ae33 1554 struct raw_breakpoint *bp;
802e8e6d 1555 int found = 0;
611cb4a5 1556
802e8e6d
PA
1557 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1558 if ((bp->raw_type == raw_bkpt_type_sw
1559 || bp->raw_type == raw_bkpt_type_hw)
1560 && bp->pc == pc)
1561 {
1562 found = 1;
1563
1564 if (bp->inserted)
1565 uninsert_raw_breakpoint (bp);
1566 }
1567
1568 if (!found)
d50171e4
PA
1569 {
1570 /* This can happen when we remove all breakpoints while handling
1571 a step-over. */
1572 if (debug_threads)
87ce2a04
DE
1573 debug_printf ("Could not find breakpoint at 0x%s "
1574 "in list (uninserting).\n",
1575 paddress (pc));
d50171e4 1576 }
611cb4a5
DJ
1577}
1578
0fb4aa4b
PA
1579void
1580uninsert_all_breakpoints (void)
1581{
1582 struct process_info *proc = current_process ();
1583 struct raw_breakpoint *bp;
1584
1585 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
802e8e6d
PA
1586 if ((bp->raw_type == raw_bkpt_type_sw
1587 || bp->raw_type == raw_bkpt_type_hw)
1588 && bp->inserted)
0fb4aa4b
PA
1589 uninsert_raw_breakpoint (bp);
1590}
1591
2e7b624b 1592void
3b9a79ef 1593uninsert_single_step_breakpoints (struct thread_info *thread)
2e7b624b 1594{
bec903c9 1595 struct process_info *proc = get_thread_process (thread);
2e7b624b
YQ
1596 struct breakpoint *bp;
1597
1598 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1599 {
3b9a79ef
YQ
1600 if (bp->type == single_step_breakpoint
1601 && ptid_equal (((struct single_step_breakpoint *) bp)->ptid,
bec903c9 1602 ptid_of (thread)))
2e7b624b
YQ
1603 {
1604 gdb_assert (bp->raw->inserted > 0);
1605
1606 /* Only uninsert the raw breakpoint if it only belongs to a
1607 reinsert breakpoint. */
1608 if (bp->raw->refcount == 1)
bec903c9
YQ
1609 {
1610 struct thread_info *saved_thread = current_thread;
1611
1612 current_thread = thread;
1613 uninsert_raw_breakpoint (bp->raw);
1614 current_thread = saved_thread;
1615 }
2e7b624b
YQ
1616 }
1617 }
1618}
1619
d50171e4 1620static void
8b07ae33 1621reinsert_raw_breakpoint (struct raw_breakpoint *bp)
611cb4a5 1622{
d50171e4 1623 int err;
611cb4a5 1624
d50171e4 1625 if (bp->inserted)
85ba7d86 1626 return;
611cb4a5 1627
27165294 1628 err = the_target->insert_point (bp->raw_type, bp->pc, bp->kind, bp);
d50171e4
PA
1629 if (err == 0)
1630 bp->inserted = 1;
1631 else if (debug_threads)
802e8e6d
PA
1632 debug_printf ("Failed to reinsert breakpoint at 0x%s (%d).\n",
1633 paddress (bp->pc), err);
611cb4a5
DJ
1634}
1635
d50171e4
PA
1636void
1637reinsert_breakpoints_at (CORE_ADDR pc)
611cb4a5 1638{
802e8e6d 1639 struct process_info *proc = current_process ();
8b07ae33 1640 struct raw_breakpoint *bp;
802e8e6d 1641 int found = 0;
611cb4a5 1642
802e8e6d
PA
1643 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1644 if ((bp->raw_type == raw_bkpt_type_sw
1645 || bp->raw_type == raw_bkpt_type_hw)
1646 && bp->pc == pc)
1647 {
1648 found = 1;
1649
1650 reinsert_raw_breakpoint (bp);
1651 }
1652
1653 if (!found)
611cb4a5 1654 {
d50171e4
PA
1655 /* This can happen when we remove all breakpoints while handling
1656 a step-over. */
1657 if (debug_threads)
87ce2a04
DE
1658 debug_printf ("Could not find raw breakpoint at 0x%s "
1659 "in list (reinserting).\n",
1660 paddress (pc));
611cb4a5 1661 }
d50171e4
PA
1662}
1663
f79b145d 1664int
3b9a79ef 1665has_single_step_breakpoints (struct thread_info *thread)
f79b145d 1666{
bec903c9 1667 struct process_info *proc = get_thread_process (thread);
f79b145d
YQ
1668 struct breakpoint *bp, **bp_link;
1669
1670 bp = proc->breakpoints;
1671 bp_link = &proc->breakpoints;
1672
1673 while (bp)
1674 {
3b9a79ef
YQ
1675 if (bp->type == single_step_breakpoint
1676 && ptid_equal (((struct single_step_breakpoint *) bp)->ptid,
bec903c9 1677 ptid_of (thread)))
f79b145d
YQ
1678 return 1;
1679 else
1680 {
1681 bp_link = &bp->next;
1682 bp = *bp_link;
1683 }
1684 }
1685
1686 return 0;
1687}
1688
0fb4aa4b
PA
1689void
1690reinsert_all_breakpoints (void)
1691{
1692 struct process_info *proc = current_process ();
1693 struct raw_breakpoint *bp;
1694
1695 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
802e8e6d
PA
1696 if ((bp->raw_type == raw_bkpt_type_sw
1697 || bp->raw_type == raw_bkpt_type_hw)
1698 && !bp->inserted)
0fb4aa4b
PA
1699 reinsert_raw_breakpoint (bp);
1700}
1701
2e7b624b 1702void
3b9a79ef 1703reinsert_single_step_breakpoints (struct thread_info *thread)
2e7b624b 1704{
bec903c9 1705 struct process_info *proc = get_thread_process (thread);
2e7b624b
YQ
1706 struct breakpoint *bp;
1707
1708 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1709 {
3b9a79ef
YQ
1710 if (bp->type == single_step_breakpoint
1711 && ptid_equal (((struct single_step_breakpoint *) bp)->ptid,
bec903c9 1712 ptid_of (thread)))
2e7b624b
YQ
1713 {
1714 gdb_assert (bp->raw->inserted > 0);
1715
1716 if (bp->raw->refcount == 1)
bec903c9
YQ
1717 {
1718 struct thread_info *saved_thread = current_thread;
1719
1720 current_thread = thread;
1721 reinsert_raw_breakpoint (bp->raw);
1722 current_thread = saved_thread;
1723 }
2e7b624b
YQ
1724 }
1725 }
1726}
1727
d50171e4
PA
1728void
1729check_breakpoints (CORE_ADDR stop_pc)
1730{
1731 struct process_info *proc = current_process ();
1732 struct breakpoint *bp, **bp_link;
1733
1734 bp = proc->breakpoints;
1735 bp_link = &proc->breakpoints;
1736
1737 while (bp)
b65d95c5 1738 {
802e8e6d
PA
1739 struct raw_breakpoint *raw = bp->raw;
1740
1741 if ((raw->raw_type == raw_bkpt_type_sw
1742 || raw->raw_type == raw_bkpt_type_hw)
1743 && raw->pc == stop_pc)
d50171e4 1744 {
802e8e6d 1745 if (!raw->inserted)
d50171e4
PA
1746 {
1747 warning ("Hit a removed breakpoint?");
1748 return;
1749 }
1750
9aa76cd0 1751 if (bp->type == other_breakpoint)
d50171e4 1752 {
9aa76cd0
YQ
1753 struct other_breakpoint *other_bp
1754 = (struct other_breakpoint *) bp;
1755
1756 if (other_bp->handler != NULL && (*other_bp->handler) (stop_pc))
1757 {
1758 *bp_link = bp->next;
d50171e4 1759
9aa76cd0 1760 release_breakpoint (proc, bp);
d50171e4 1761
9aa76cd0
YQ
1762 bp = *bp_link;
1763 continue;
1764 }
d50171e4
PA
1765 }
1766 }
1767
1768 bp_link = &bp->next;
1769 bp = *bp_link;
b65d95c5 1770 }
611cb4a5
DJ
1771}
1772
d50171e4
PA
1773int
1774breakpoint_here (CORE_ADDR addr)
1775{
802e8e6d
PA
1776 struct process_info *proc = current_process ();
1777 struct raw_breakpoint *bp;
1778
1779 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1780 if ((bp->raw_type == raw_bkpt_type_sw
1781 || bp->raw_type == raw_bkpt_type_hw)
1782 && bp->pc == addr)
1783 return 1;
1784
1785 return 0;
d50171e4
PA
1786}
1787
1788int
1789breakpoint_inserted_here (CORE_ADDR addr)
1790{
802e8e6d 1791 struct process_info *proc = current_process ();
8b07ae33 1792 struct raw_breakpoint *bp;
d50171e4 1793
802e8e6d
PA
1794 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1795 if ((bp->raw_type == raw_bkpt_type_sw
1796 || bp->raw_type == raw_bkpt_type_hw)
1797 && bp->pc == addr
1798 && bp->inserted)
1799 return 1;
d50171e4 1800
802e8e6d 1801 return 0;
d50171e4
PA
1802}
1803
582511be
PA
1804/* See mem-break.h. */
1805
1806int
1807software_breakpoint_inserted_here (CORE_ADDR addr)
1808{
1809 struct process_info *proc = current_process ();
1810 struct raw_breakpoint *bp;
1811
1812 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1813 if (bp->raw_type == raw_bkpt_type_sw
1814 && bp->pc == addr
1815 && bp->inserted)
1816 return 1;
1817
1818 return 0;
1819}
1820
1821/* See mem-break.h. */
1822
1823int
1824hardware_breakpoint_inserted_here (CORE_ADDR addr)
1825{
1826 struct process_info *proc = current_process ();
1827 struct raw_breakpoint *bp;
1828
1829 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
1830 if (bp->raw_type == raw_bkpt_type_hw
1831 && bp->pc == addr
1832 && bp->inserted)
1833 return 1;
1834
1835 return 0;
1836}
1837
2d97cd35
AT
1838/* See mem-break.h. */
1839
1840int
3b9a79ef 1841single_step_breakpoint_inserted_here (CORE_ADDR addr)
2d97cd35
AT
1842{
1843 struct process_info *proc = current_process ();
1844 struct breakpoint *bp;
1845
1846 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
3b9a79ef 1847 if (bp->type == single_step_breakpoint
2d97cd35
AT
1848 && bp->raw->pc == addr
1849 && bp->raw->inserted)
1850 return 1;
1851
1852 return 0;
1853}
1854
d3bbe7a0
PA
1855static int
1856validate_inserted_breakpoint (struct raw_breakpoint *bp)
1857{
1858 unsigned char *buf;
1859 int err;
1860
1861 gdb_assert (bp->inserted);
802e8e6d 1862 gdb_assert (bp->raw_type == raw_bkpt_type_sw);
d3bbe7a0 1863
27165294
AT
1864 buf = (unsigned char *) alloca (bp_size (bp));
1865 err = (*the_target->read_memory) (bp->pc, buf, bp_size (bp));
1866 if (err || memcmp (buf, bp_opcode (bp), bp_size (bp)) != 0)
d3bbe7a0
PA
1867 {
1868 /* Tag it as gone. */
802e8e6d 1869 bp->inserted = -1;
d3bbe7a0
PA
1870 return 0;
1871 }
1872
1873 return 1;
1874}
1875
1876static void
1877delete_disabled_breakpoints (void)
1878{
1879 struct process_info *proc = current_process ();
1880 struct breakpoint *bp, *next;
1881
1882 for (bp = proc->breakpoints; bp != NULL; bp = next)
1883 {
1884 next = bp->next;
802e8e6d 1885 if (bp->raw->inserted < 0)
8376a3cb 1886 {
3b9a79ef 1887 /* If single_step_breakpoints become disabled, that means the
8376a3cb 1888 manipulations (insertion and removal) of them are wrong. */
3b9a79ef 1889 gdb_assert (bp->type != single_step_breakpoint);
8376a3cb
YQ
1890 delete_breakpoint_1 (proc, bp);
1891 }
d3bbe7a0
PA
1892 }
1893}
1894
1895/* Check if breakpoints we inserted still appear to be inserted. They
1896 may disappear due to a shared library unload, and worse, a new
1897 shared library may be reloaded at the same address as the
1898 previously unloaded one. If that happens, we should make sure that
1899 the shadow memory of the old breakpoints isn't used when reading or
1900 writing memory. */
1901
1902void
1903validate_breakpoints (void)
1904{
1905 struct process_info *proc = current_process ();
1906 struct breakpoint *bp;
1907
1908 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1909 {
802e8e6d
PA
1910 struct raw_breakpoint *raw = bp->raw;
1911
1912 if (raw->raw_type == raw_bkpt_type_sw && raw->inserted > 0)
1913 validate_inserted_breakpoint (raw);
d3bbe7a0
PA
1914 }
1915
1916 delete_disabled_breakpoints ();
1917}
1918
611cb4a5 1919void
f450004a 1920check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
611cb4a5 1921{
95954743 1922 struct process_info *proc = current_process ();
8b07ae33 1923 struct raw_breakpoint *bp = proc->raw_breakpoints;
fa593d66 1924 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
611cb4a5 1925 CORE_ADDR mem_end = mem_addr + mem_len;
d3bbe7a0 1926 int disabled_one = 0;
611cb4a5 1927
fa593d66
PA
1928 for (; jp != NULL; jp = jp->next)
1929 {
1930 CORE_ADDR bp_end = jp->pc + jp->length;
1931 CORE_ADDR start, end;
1932 int copy_offset, copy_len, buf_offset;
1933
6bf36717
JK
1934 gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len
1935 || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1936
fa593d66
PA
1937 if (mem_addr >= bp_end)
1938 continue;
1939 if (jp->pc >= mem_end)
1940 continue;
1941
1942 start = jp->pc;
1943 if (mem_addr > start)
1944 start = mem_addr;
1945
1946 end = bp_end;
1947 if (end > mem_end)
1948 end = mem_end;
1949
1950 copy_len = end - start;
1951 copy_offset = start - jp->pc;
1952 buf_offset = start - mem_addr;
1953
1954 if (jp->inserted)
1955 memcpy (buf + buf_offset,
1956 fast_tracepoint_jump_shadow (jp) + copy_offset,
1957 copy_len);
1958 }
1959
611cb4a5
DJ
1960 for (; bp != NULL; bp = bp->next)
1961 {
27165294 1962 CORE_ADDR bp_end = bp->pc + bp_size (bp);
611cb4a5
DJ
1963 CORE_ADDR start, end;
1964 int copy_offset, copy_len, buf_offset;
1965
802e8e6d
PA
1966 if (bp->raw_type != raw_bkpt_type_sw)
1967 continue;
1968
6bf36717
JK
1969 gdb_assert (bp->old_data >= buf + mem_len
1970 || buf >= &bp->old_data[sizeof (bp->old_data)]);
1971
611cb4a5
DJ
1972 if (mem_addr >= bp_end)
1973 continue;
1974 if (bp->pc >= mem_end)
1975 continue;
1976
1977 start = bp->pc;
1978 if (mem_addr > start)
1979 start = mem_addr;
1980
1981 end = bp_end;
1982 if (end > mem_end)
1983 end = mem_end;
1984
1985 copy_len = end - start;
1986 copy_offset = start - bp->pc;
1987 buf_offset = start - mem_addr;
1988
802e8e6d 1989 if (bp->inserted > 0)
d3bbe7a0
PA
1990 {
1991 if (validate_inserted_breakpoint (bp))
1992 memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
1993 else
1994 disabled_one = 1;
1995 }
611cb4a5 1996 }
d3bbe7a0
PA
1997
1998 if (disabled_one)
1999 delete_disabled_breakpoints ();
611cb4a5
DJ
2000}
2001
2002void
b9fd1791
PA
2003check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
2004 const unsigned char *myaddr, int mem_len)
611cb4a5 2005{
95954743 2006 struct process_info *proc = current_process ();
8b07ae33 2007 struct raw_breakpoint *bp = proc->raw_breakpoints;
fa593d66 2008 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
611cb4a5 2009 CORE_ADDR mem_end = mem_addr + mem_len;
d3bbe7a0 2010 int disabled_one = 0;
611cb4a5 2011
fa593d66
PA
2012 /* First fast tracepoint jumps, then breakpoint traps on top. */
2013
2014 for (; jp != NULL; jp = jp->next)
2015 {
2016 CORE_ADDR jp_end = jp->pc + jp->length;
2017 CORE_ADDR start, end;
2018 int copy_offset, copy_len, buf_offset;
2019
6bf36717
JK
2020 gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len
2021 || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
2022 gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len
2023 || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length);
2024
fa593d66
PA
2025 if (mem_addr >= jp_end)
2026 continue;
2027 if (jp->pc >= mem_end)
2028 continue;
2029
2030 start = jp->pc;
2031 if (mem_addr > start)
2032 start = mem_addr;
2033
2034 end = jp_end;
2035 if (end > mem_end)
2036 end = mem_end;
2037
2038 copy_len = end - start;
2039 copy_offset = start - jp->pc;
2040 buf_offset = start - mem_addr;
2041
2042 memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset,
b9fd1791 2043 myaddr + buf_offset, copy_len);
fa593d66
PA
2044 if (jp->inserted)
2045 memcpy (buf + buf_offset,
2046 fast_tracepoint_jump_insn (jp) + copy_offset, copy_len);
2047 }
2048
611cb4a5
DJ
2049 for (; bp != NULL; bp = bp->next)
2050 {
27165294 2051 CORE_ADDR bp_end = bp->pc + bp_size (bp);
611cb4a5
DJ
2052 CORE_ADDR start, end;
2053 int copy_offset, copy_len, buf_offset;
2054
802e8e6d
PA
2055 if (bp->raw_type != raw_bkpt_type_sw)
2056 continue;
2057
6bf36717
JK
2058 gdb_assert (bp->old_data >= myaddr + mem_len
2059 || myaddr >= &bp->old_data[sizeof (bp->old_data)]);
2060
611cb4a5
DJ
2061 if (mem_addr >= bp_end)
2062 continue;
2063 if (bp->pc >= mem_end)
2064 continue;
2065
2066 start = bp->pc;
2067 if (mem_addr > start)
2068 start = mem_addr;
2069
2070 end = bp_end;
2071 if (end > mem_end)
2072 end = mem_end;
2073
2074 copy_len = end - start;
2075 copy_offset = start - bp->pc;
2076 buf_offset = start - mem_addr;
2077
b9fd1791 2078 memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len);
802e8e6d 2079 if (bp->inserted > 0)
d3bbe7a0
PA
2080 {
2081 if (validate_inserted_breakpoint (bp))
27165294 2082 memcpy (buf + buf_offset, bp_opcode (bp) + copy_offset, copy_len);
d3bbe7a0
PA
2083 else
2084 disabled_one = 1;
2085 }
611cb4a5 2086 }
d3bbe7a0
PA
2087
2088 if (disabled_one)
2089 delete_disabled_breakpoints ();
611cb4a5 2090}
ae13219e 2091
95954743 2092/* Delete all breakpoints, and un-insert them from the inferior. */
ae13219e
DJ
2093
2094void
2095delete_all_breakpoints (void)
2096{
95954743
PA
2097 struct process_info *proc = current_process ();
2098
2099 while (proc->breakpoints)
8b07ae33 2100 delete_breakpoint_1 (proc, proc->breakpoints);
95954743
PA
2101}
2102
f9e39928 2103/* Clear the "inserted" flag in all breakpoints. */
95954743
PA
2104
2105void
f9e39928 2106mark_breakpoints_out (struct process_info *proc)
95954743 2107{
8b07ae33 2108 struct raw_breakpoint *raw_bp;
95954743 2109
8b07ae33
PA
2110 for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next)
2111 raw_bp->inserted = 0;
f9e39928
PA
2112}
2113
2114/* Release all breakpoints, but do not try to un-insert them from the
2115 inferior. */
2116
2117void
2118free_all_breakpoints (struct process_info *proc)
2119{
2120 mark_breakpoints_out (proc);
8b07ae33
PA
2121
2122 /* Note: use PROC explicitly instead of deferring to
2123 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
2124 released when we get here. There should be no call to
2125 current_process from here on. */
95954743 2126 while (proc->breakpoints)
8b07ae33 2127 delete_breakpoint_1 (proc, proc->breakpoints);
ae13219e 2128}
ddcbc397
DB
2129
2130/* Clone an agent expression. */
2131
2132static struct agent_expr *
2133clone_agent_expr (const struct agent_expr *src_ax)
2134{
2135 struct agent_expr *ax;
2136
8d749320 2137 ax = XCNEW (struct agent_expr);
ddcbc397 2138 ax->length = src_ax->length;
224c3ddb 2139 ax->bytes = (unsigned char *) xcalloc (ax->length, 1);
ddcbc397
DB
2140 memcpy (ax->bytes, src_ax->bytes, ax->length);
2141 return ax;
2142}
2143
2144/* Deep-copy the contents of one breakpoint to another. */
2145
2146static struct breakpoint *
bec903c9 2147clone_one_breakpoint (const struct breakpoint *src, ptid_t ptid)
ddcbc397
DB
2148{
2149 struct breakpoint *dest;
2150 struct raw_breakpoint *dest_raw;
ddcbc397
DB
2151
2152 /* Clone the raw breakpoint. */
8d749320 2153 dest_raw = XCNEW (struct raw_breakpoint);
ddcbc397
DB
2154 dest_raw->raw_type = src->raw->raw_type;
2155 dest_raw->refcount = src->raw->refcount;
2156 dest_raw->pc = src->raw->pc;
27165294 2157 dest_raw->kind = src->raw->kind;
ddcbc397
DB
2158 memcpy (dest_raw->old_data, src->raw->old_data, MAX_BREAKPOINT_LEN);
2159 dest_raw->inserted = src->raw->inserted;
2160
2161 /* Clone the high-level breakpoint. */
9aa76cd0 2162 if (is_gdb_breakpoint (src->type))
ddcbc397 2163 {
9aa76cd0
YQ
2164 struct gdb_breakpoint *gdb_dest = XCNEW (struct gdb_breakpoint);
2165 struct point_cond_list *current_cond;
2166 struct point_cond_list *new_cond;
2167 struct point_cond_list *cond_tail = NULL;
2168 struct point_command_list *current_cmd;
2169 struct point_command_list *new_cmd;
2170 struct point_command_list *cmd_tail = NULL;
2171
2172 /* Clone the condition list. */
2173 for (current_cond = ((struct gdb_breakpoint *) src)->cond_list;
2174 current_cond != NULL;
2175 current_cond = current_cond->next)
2176 {
2177 new_cond = XCNEW (struct point_cond_list);
2178 new_cond->cond = clone_agent_expr (current_cond->cond);
2179 APPEND_TO_LIST (&gdb_dest->cond_list, new_cond, cond_tail);
2180 }
2181
2182 /* Clone the command list. */
2183 for (current_cmd = ((struct gdb_breakpoint *) src)->command_list;
2184 current_cmd != NULL;
2185 current_cmd = current_cmd->next)
2186 {
2187 new_cmd = XCNEW (struct point_command_list);
2188 new_cmd->cmd = clone_agent_expr (current_cmd->cmd);
2189 new_cmd->persistence = current_cmd->persistence;
2190 APPEND_TO_LIST (&gdb_dest->command_list, new_cmd, cmd_tail);
2191 }
2192
2193 dest = (struct breakpoint *) gdb_dest;
ddcbc397 2194 }
9aa76cd0
YQ
2195 else if (src->type == other_breakpoint)
2196 {
2197 struct other_breakpoint *other_dest = XCNEW (struct other_breakpoint);
ddcbc397 2198
9aa76cd0
YQ
2199 other_dest->handler = ((struct other_breakpoint *) src)->handler;
2200 dest = (struct breakpoint *) other_dest;
2201 }
3b9a79ef 2202 else if (src->type == single_step_breakpoint)
ddcbc397 2203 {
3b9a79ef
YQ
2204 struct single_step_breakpoint *ss_dest
2205 = XCNEW (struct single_step_breakpoint);
9aa76cd0 2206
3b9a79ef
YQ
2207 dest = (struct breakpoint *) ss_dest;
2208 /* Since single-step breakpoint is thread specific, don't copy
bec903c9 2209 thread id from SRC, use ID instead. */
3b9a79ef 2210 ss_dest->ptid = ptid;
ddcbc397 2211 }
9aa76cd0
YQ
2212 else
2213 gdb_assert_not_reached ("unhandled breakpoint type");
2214
2215 dest->type = src->type;
2216 dest->raw = dest_raw;
ddcbc397
DB
2217
2218 return dest;
2219}
2220
63c40ec7 2221/* See mem-break.h. */
ddcbc397
DB
2222
2223void
63c40ec7
YQ
2224clone_all_breakpoints (struct thread_info *child_thread,
2225 const struct thread_info *parent_thread)
ddcbc397
DB
2226{
2227 const struct breakpoint *bp;
2228 struct breakpoint *new_bkpt;
2229 struct breakpoint *bkpt_tail = NULL;
2230 struct raw_breakpoint *raw_bkpt_tail = NULL;
63c40ec7
YQ
2231 struct process_info *child_proc = get_thread_process (child_thread);
2232 struct process_info *parent_proc = get_thread_process (parent_thread);
2233 struct breakpoint **new_list = &child_proc->breakpoints;
2234 struct raw_breakpoint **new_raw_list = &child_proc->raw_breakpoints;
ddcbc397 2235
63c40ec7 2236 for (bp = parent_proc->breakpoints; bp != NULL; bp = bp->next)
ddcbc397 2237 {
bec903c9 2238 new_bkpt = clone_one_breakpoint (bp, ptid_of (child_thread));
ddcbc397
DB
2239 APPEND_TO_LIST (new_list, new_bkpt, bkpt_tail);
2240 APPEND_TO_LIST (new_raw_list, new_bkpt->raw, raw_bkpt_tail);
2241 }
2242}
This page took 1.673266 seconds and 4 git commands to generate.