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