Fix typo in last gdb/ChangeLog entry.
[deliverable/binutils-gdb.git] / gdb / gdbserver / mem-break.c
CommitLineData
611cb4a5 1/* Memory breakpoint operations for the remote server for GDB.
0b302171
JB
2 Copyright (C) 2002-2003, 2005, 2007-2012 Free Software Foundation,
3 Inc.
611cb4a5
DJ
4
5 Contributed by MontaVista Software.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
611cb4a5
DJ
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
611cb4a5
DJ
21
22#include "server.h"
9f3a5c85
LM
23#include "regcache.h"
24#include "ax.h"
611cb4a5 25
f450004a 26const unsigned char *breakpoint_data;
611cb4a5
DJ
27int breakpoint_len;
28
29#define MAX_BREAKPOINT_LEN 8
30
8b07ae33
PA
31/* GDB will never try to install multiple breakpoints at the same
32 address. But, we need to keep track of internal breakpoints too,
33 and so we do need to be able to install multiple breakpoints at the
34 same address transparently. We keep track of two different, and
35 closely related structures. A raw breakpoint, which manages the
36 low level, close to the metal aspect of a breakpoint. It holds the
37 breakpoint address, and a buffer holding a copy of the instructions
38 that would be in memory had not been a breakpoint there (we call
39 that the shadow memory of the breakpoint). We occasionally need to
40 temporarilly uninsert a breakpoint without the client knowing about
41 it (e.g., to step over an internal breakpoint), so we keep an
42 `inserted' state associated with this low level breakpoint
43 structure. There can only be one such object for a given address.
44 Then, we have (a bit higher level) breakpoints. This structure
45 holds a callback to be called whenever a breakpoint is hit, a
46 high-level type, and a link to a low level raw breakpoint. There
47 can be many high-level breakpoints at the same address, and all of
48 them will point to the same raw breakpoint, which is reference
49 counted. */
50
51/* The low level, physical, raw breakpoint. */
52struct raw_breakpoint
53{
54 struct raw_breakpoint *next;
55
56 /* A reference count. Each high level breakpoint referencing this
57 raw breakpoint accounts for one reference. */
58 int refcount;
59
60 /* The breakpoint's insertion address. There can only be one raw
61 breakpoint for a given PC. */
62 CORE_ADDR pc;
63
64 /* The breakpoint's shadow memory. */
65 unsigned char old_data[MAX_BREAKPOINT_LEN];
66
67 /* Non-zero if this breakpoint is currently inserted in the
68 inferior. */
69 int inserted;
d3bbe7a0
PA
70
71 /* Non-zero if this breakpoint is currently disabled because we no
72 longer detect it as inserted. */
73 int shlib_disabled;
8b07ae33
PA
74};
75
414a389f
PA
76/* The type of a breakpoint. */
77enum bkpt_type
78 {
8b07ae33
PA
79 /* A GDB breakpoint, requested with a Z0 packet. */
80 gdb_breakpoint,
81
414a389f
PA
82 /* A basic-software-single-step breakpoint. */
83 reinsert_breakpoint,
84
85 /* Any other breakpoint type that doesn't require specific
86 treatment goes here. E.g., an event breakpoint. */
87 other_breakpoint,
88 };
89
9f3a5c85
LM
90struct point_cond_list
91{
92 /* Pointer to the agent expression that is the breakpoint's
93 conditional. */
94 struct agent_expr *cond;
95
96 /* Pointer to the next condition. */
97 struct point_cond_list *next;
98};
99
8b07ae33 100/* A high level (in gdbserver's perspective) breakpoint. */
611cb4a5
DJ
101struct breakpoint
102{
103 struct breakpoint *next;
611cb4a5 104
414a389f
PA
105 /* The breakpoint's type. */
106 enum bkpt_type type;
107
9f3a5c85
LM
108 /* Pointer to the condition list that should be evaluated on
109 the target or NULL if the breakpoint is unconditional or
110 if GDB doesn't want us to evaluate the conditionals on the
111 target's side. */
112 struct point_cond_list *cond_list;
113
8b07ae33
PA
114 /* Link to this breakpoint's raw breakpoint. This is always
115 non-NULL. */
116 struct raw_breakpoint *raw;
117
b65d95c5 118 /* Function to call when we hit this breakpoint. If it returns 1,
8b07ae33
PA
119 the breakpoint shall be deleted; 0 or if this callback is NULL,
120 it will be left inserted. */
b65d95c5 121 int (*handler) (CORE_ADDR);
611cb4a5
DJ
122};
123
8b07ae33
PA
124static struct raw_breakpoint *
125find_raw_breakpoint_at (CORE_ADDR where)
126{
127 struct process_info *proc = current_process ();
128 struct raw_breakpoint *bp;
414a389f 129
8b07ae33
PA
130 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
131 if (bp->pc == where)
132 return bp;
133
134 return NULL;
135}
136
137static struct raw_breakpoint *
d50171e4 138set_raw_breakpoint_at (CORE_ADDR where)
611cb4a5 139{
95954743 140 struct process_info *proc = current_process ();
8b07ae33 141 struct raw_breakpoint *bp;
d50171e4 142 int err;
6bf36717 143 unsigned char buf[MAX_BREAKPOINT_LEN];
611cb4a5
DJ
144
145 if (breakpoint_data == NULL)
146 error ("Target does not support breakpoints.");
147
8b07ae33
PA
148 bp = find_raw_breakpoint_at (where);
149 if (bp != NULL)
150 {
151 bp->refcount++;
152 return bp;
153 }
154
d50171e4
PA
155 bp = xcalloc (1, sizeof (*bp));
156 bp->pc = where;
8b07ae33 157 bp->refcount = 1;
611cb4a5 158
fa593d66
PA
159 /* Note that there can be fast tracepoint jumps installed in the
160 same memory range, so to get at the original memory, we need to
161 use read_inferior_memory, which masks those out. */
6bf36717 162 err = read_inferior_memory (where, buf, breakpoint_len);
d50171e4
PA
163 if (err != 0)
164 {
165 if (debug_threads)
166 fprintf (stderr,
167 "Failed to read shadow memory of"
168 " breakpoint at 0x%s (%s).\n",
169 paddress (where), strerror (err));
170 free (bp);
171 return NULL;
172 }
6bf36717 173 memcpy (bp->old_data, buf, breakpoint_len);
611cb4a5 174
d50171e4
PA
175 err = (*the_target->write_memory) (where, breakpoint_data,
176 breakpoint_len);
177 if (err != 0)
178 {
179 if (debug_threads)
180 fprintf (stderr,
181 "Failed to insert breakpoint at 0x%s (%s).\n",
182 paddress (where), strerror (err));
183 free (bp);
184 return NULL;
185 }
186
187 /* Link the breakpoint in. */
188 bp->inserted = 1;
8b07ae33
PA
189 bp->next = proc->raw_breakpoints;
190 proc->raw_breakpoints = bp;
d50171e4
PA
191 return bp;
192}
193
fa593d66
PA
194/* Notice that breakpoint traps are always installed on top of fast
195 tracepoint jumps. This is even if the fast tracepoint is installed
196 at a later time compared to when the breakpoint was installed.
197 This means that a stopping breakpoint or tracepoint has higher
198 "priority". In turn, this allows having fast and slow tracepoints
199 (and breakpoints) at the same address behave correctly. */
200
201
202/* A fast tracepoint jump. */
203
204struct fast_tracepoint_jump
205{
206 struct fast_tracepoint_jump *next;
207
208 /* A reference count. GDB can install more than one fast tracepoint
209 at the same address (each with its own action list, for
210 example). */
211 int refcount;
212
213 /* The fast tracepoint's insertion address. There can only be one
214 of these for a given PC. */
215 CORE_ADDR pc;
216
217 /* Non-zero if this fast tracepoint jump is currently inserted in
218 the inferior. */
219 int inserted;
220
221 /* The length of the jump instruction. */
222 int length;
223
224 /* A poor-man's flexible array member, holding both the jump
225 instruction to insert, and a copy of the instruction that would
226 be in memory had not been a jump there (the shadow memory of the
227 tracepoint jump). */
228 unsigned char insn_and_shadow[0];
229};
230
231/* Fast tracepoint FP's jump instruction to insert. */
232#define fast_tracepoint_jump_insn(fp) \
233 ((fp)->insn_and_shadow + 0)
234
235/* The shadow memory of fast tracepoint jump FP. */
236#define fast_tracepoint_jump_shadow(fp) \
237 ((fp)->insn_and_shadow + (fp)->length)
238
239
240/* Return the fast tracepoint jump set at WHERE. */
241
242static struct fast_tracepoint_jump *
243find_fast_tracepoint_jump_at (CORE_ADDR where)
244{
245 struct process_info *proc = current_process ();
246 struct fast_tracepoint_jump *jp;
247
248 for (jp = proc->fast_tracepoint_jumps; jp != NULL; jp = jp->next)
249 if (jp->pc == where)
250 return jp;
251
252 return NULL;
253}
254
255int
256fast_tracepoint_jump_here (CORE_ADDR where)
257{
258 struct fast_tracepoint_jump *jp = find_fast_tracepoint_jump_at (where);
259
260 return (jp != NULL);
261}
262
263int
264delete_fast_tracepoint_jump (struct fast_tracepoint_jump *todel)
265{
266 struct fast_tracepoint_jump *bp, **bp_link;
267 int ret;
268 struct process_info *proc = current_process ();
269
270 bp = proc->fast_tracepoint_jumps;
271 bp_link = &proc->fast_tracepoint_jumps;
272
273 while (bp)
274 {
275 if (bp == todel)
276 {
277 if (--bp->refcount == 0)
278 {
279 struct fast_tracepoint_jump *prev_bp_link = *bp_link;
6bf36717 280 unsigned char *buf;
fa593d66
PA
281
282 /* Unlink it. */
283 *bp_link = bp->next;
284
285 /* Since there can be breakpoints inserted in the same
286 address range, we use `write_inferior_memory', which
287 takes care of layering breakpoints on top of fast
288 tracepoints, and on top of the buffer we pass it.
289 This works because we've already unlinked the fast
290 tracepoint jump above. Also note that we need to
291 pass the current shadow contents, because
292 write_inferior_memory updates any shadow memory with
293 what we pass here, and we want that to be a nop. */
6bf36717
JK
294 buf = alloca (bp->length);
295 memcpy (buf, fast_tracepoint_jump_shadow (bp), bp->length);
296 ret = write_inferior_memory (bp->pc, buf, bp->length);
fa593d66
PA
297 if (ret != 0)
298 {
299 /* Something went wrong, relink the jump. */
300 *bp_link = prev_bp_link;
301
302 if (debug_threads)
303 fprintf (stderr,
304 "Failed to uninsert fast tracepoint jump "
305 "at 0x%s (%s) while deleting it.\n",
306 paddress (bp->pc), strerror (ret));
307 return ret;
308 }
309
310 free (bp);
311 }
312
313 return 0;
314 }
315 else
316 {
317 bp_link = &bp->next;
318 bp = *bp_link;
319 }
320 }
321
322 warning ("Could not find fast tracepoint jump in list.");
323 return ENOENT;
324}
325
5c73ff4e
YQ
326void
327inc_ref_fast_tracepoint_jump (struct fast_tracepoint_jump *jp)
328{
329 jp->refcount++;
330}
331
fa593d66
PA
332struct fast_tracepoint_jump *
333set_fast_tracepoint_jump (CORE_ADDR where,
334 unsigned char *insn, ULONGEST length)
335{
336 struct process_info *proc = current_process ();
337 struct fast_tracepoint_jump *jp;
338 int err;
6bf36717 339 unsigned char *buf;
fa593d66
PA
340
341 /* We refcount fast tracepoint jumps. Check if we already know
342 about a jump at this address. */
343 jp = find_fast_tracepoint_jump_at (where);
344 if (jp != NULL)
345 {
346 jp->refcount++;
347 return jp;
348 }
349
350 /* We don't, so create a new object. Double the length, because the
351 flexible array member holds both the jump insn, and the
352 shadow. */
353 jp = xcalloc (1, sizeof (*jp) + (length * 2));
354 jp->pc = where;
355 jp->length = length;
356 memcpy (fast_tracepoint_jump_insn (jp), insn, length);
357 jp->refcount = 1;
6bf36717 358 buf = alloca (length);
fa593d66
PA
359
360 /* Note that there can be trap breakpoints inserted in the same
361 address range. To access the original memory contents, we use
362 `read_inferior_memory', which masks out breakpoints. */
6bf36717 363 err = read_inferior_memory (where, buf, length);
fa593d66
PA
364 if (err != 0)
365 {
366 if (debug_threads)
367 fprintf (stderr,
368 "Failed to read shadow memory of"
369 " fast tracepoint at 0x%s (%s).\n",
370 paddress (where), strerror (err));
371 free (jp);
372 return NULL;
373 }
6bf36717 374 memcpy (fast_tracepoint_jump_shadow (jp), buf, length);
fa593d66
PA
375
376 /* Link the jump in. */
377 jp->inserted = 1;
378 jp->next = proc->fast_tracepoint_jumps;
379 proc->fast_tracepoint_jumps = jp;
380
381 /* Since there can be trap breakpoints inserted in the same address
382 range, we use use `write_inferior_memory', which takes care of
383 layering breakpoints on top of fast tracepoints, on top of the
384 buffer we pass it. This works because we've already linked in
385 the fast tracepoint jump above. Also note that we need to pass
386 the current shadow contents, because write_inferior_memory
387 updates any shadow memory with what we pass here, and we want
388 that to be a nop. */
6bf36717 389 err = write_inferior_memory (where, buf, length);
fa593d66
PA
390 if (err != 0)
391 {
392 if (debug_threads)
393 fprintf (stderr,
394 "Failed to insert fast tracepoint jump at 0x%s (%s).\n",
395 paddress (where), strerror (err));
396
397 /* Unlink it. */
398 proc->fast_tracepoint_jumps = jp->next;
399 free (jp);
400
401 return NULL;
402 }
403
404 return jp;
405}
406
407void
408uninsert_fast_tracepoint_jumps_at (CORE_ADDR pc)
409{
410 struct fast_tracepoint_jump *jp;
411 int err;
412
413 jp = find_fast_tracepoint_jump_at (pc);
414 if (jp == NULL)
415 {
416 /* This can happen when we remove all breakpoints while handling
417 a step-over. */
418 if (debug_threads)
419 fprintf (stderr,
420 "Could not find fast tracepoint jump at 0x%s "
421 "in list (uninserting).\n",
422 paddress (pc));
423 return;
424 }
425
426 if (jp->inserted)
427 {
6bf36717
JK
428 unsigned char *buf;
429
fa593d66
PA
430 jp->inserted = 0;
431
432 /* Since there can be trap breakpoints inserted in the same
433 address range, we use use `write_inferior_memory', which
434 takes care of layering breakpoints on top of fast
435 tracepoints, and on top of the buffer we pass it. This works
436 because we've already marked the fast tracepoint fast
437 tracepoint jump uninserted above. Also note that we need to
438 pass the current shadow contents, because
439 write_inferior_memory updates any shadow memory with what we
440 pass here, and we want that to be a nop. */
6bf36717
JK
441 buf = alloca (jp->length);
442 memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
443 err = write_inferior_memory (jp->pc, buf, jp->length);
fa593d66
PA
444 if (err != 0)
445 {
446 jp->inserted = 1;
447
448 if (debug_threads)
449 fprintf (stderr,
450 "Failed to uninsert fast tracepoint jump at 0x%s (%s).\n",
451 paddress (pc), strerror (err));
452 }
453 }
454}
455
456void
457reinsert_fast_tracepoint_jumps_at (CORE_ADDR where)
458{
459 struct fast_tracepoint_jump *jp;
460 int err;
6bf36717 461 unsigned char *buf;
fa593d66
PA
462
463 jp = find_fast_tracepoint_jump_at (where);
464 if (jp == NULL)
465 {
466 /* This can happen when we remove breakpoints when a tracepoint
467 hit causes a tracing stop, while handling a step-over. */
468 if (debug_threads)
469 fprintf (stderr,
470 "Could not find fast tracepoint jump at 0x%s "
471 "in list (reinserting).\n",
472 paddress (where));
473 return;
474 }
475
476 if (jp->inserted)
477 error ("Jump already inserted at reinsert time.");
478
479 jp->inserted = 1;
480
481 /* Since there can be trap breakpoints inserted in the same address
482 range, we use `write_inferior_memory', which takes care of
483 layering breakpoints on top of fast tracepoints, and on top of
484 the buffer we pass it. This works because we've already marked
485 the fast tracepoint jump inserted above. Also note that we need
486 to pass the current shadow contents, because
487 write_inferior_memory updates any shadow memory with what we pass
488 here, and we want that to be a nop. */
6bf36717
JK
489 buf = alloca (jp->length);
490 memcpy (buf, fast_tracepoint_jump_shadow (jp), jp->length);
491 err = write_inferior_memory (where, buf, jp->length);
fa593d66
PA
492 if (err != 0)
493 {
494 jp->inserted = 0;
495
496 if (debug_threads)
497 fprintf (stderr,
498 "Failed to reinsert fast tracepoint jump at 0x%s (%s).\n",
499 paddress (where), strerror (err));
500 }
501}
502
414a389f 503struct breakpoint *
d50171e4
PA
504set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR))
505{
506 struct process_info *proc = current_process ();
507 struct breakpoint *bp;
8b07ae33 508 struct raw_breakpoint *raw;
d50171e4 509
8b07ae33 510 raw = set_raw_breakpoint_at (where);
d50171e4 511
8b07ae33 512 if (raw == NULL)
d50171e4
PA
513 {
514 /* warn? */
414a389f 515 return NULL;
d50171e4
PA
516 }
517
518 bp = xcalloc (1, sizeof (struct breakpoint));
414a389f 519 bp->type = other_breakpoint;
8b07ae33
PA
520
521 bp->raw = raw;
611cb4a5
DJ
522 bp->handler = handler;
523
95954743
PA
524 bp->next = proc->breakpoints;
525 proc->breakpoints = bp;
414a389f
PA
526
527 return bp;
611cb4a5
DJ
528}
529
8b07ae33
PA
530static int
531delete_raw_breakpoint (struct process_info *proc, struct raw_breakpoint *todel)
532{
533 struct raw_breakpoint *bp, **bp_link;
534 int ret;
535
536 bp = proc->raw_breakpoints;
537 bp_link = &proc->raw_breakpoints;
538
539 while (bp)
540 {
541 if (bp == todel)
542 {
543 if (bp->inserted)
544 {
545 struct raw_breakpoint *prev_bp_link = *bp_link;
6bf36717 546 unsigned char buf[MAX_BREAKPOINT_LEN];
8b07ae33
PA
547
548 *bp_link = bp->next;
549
fa593d66
PA
550 /* Since there can be trap breakpoints inserted in the
551 same address range, we use `write_inferior_memory',
552 which takes care of layering breakpoints on top of
553 fast tracepoints, and on top of the buffer we pass
554 it. This works because we've already unlinked the
555 fast tracepoint jump above. Also note that we need
556 to pass the current shadow contents, because
557 write_inferior_memory updates any shadow memory with
558 what we pass here, and we want that to be a nop. */
6bf36717
JK
559 memcpy (buf, bp->old_data, breakpoint_len);
560 ret = write_inferior_memory (bp->pc, buf, breakpoint_len);
8b07ae33
PA
561 if (ret != 0)
562 {
563 /* Something went wrong, relink the breakpoint. */
564 *bp_link = prev_bp_link;
565
566 if (debug_threads)
567 fprintf (stderr,
568 "Failed to uninsert raw breakpoint "
569 "at 0x%s (%s) while deleting it.\n",
570 paddress (bp->pc), strerror (ret));
571 return ret;
572 }
573
574 }
575 else
576 *bp_link = bp->next;
577
578 free (bp);
579 return 0;
580 }
581 else
582 {
583 bp_link = &bp->next;
584 bp = *bp_link;
585 }
586 }
587
588 warning ("Could not find raw breakpoint in list.");
589 return ENOENT;
590}
591
592static int
593release_breakpoint (struct process_info *proc, struct breakpoint *bp)
594{
595 int newrefcount;
596 int ret;
597
598 newrefcount = bp->raw->refcount - 1;
599 if (newrefcount == 0)
600 {
601 ret = delete_raw_breakpoint (proc, bp->raw);
602 if (ret != 0)
603 return ret;
604 }
605 else
606 bp->raw->refcount = newrefcount;
607
608 free (bp);
609
610 return 0;
611}
612
613static int
614delete_breakpoint_1 (struct process_info *proc, struct breakpoint *todel)
611cb4a5 615{
414a389f 616 struct breakpoint *bp, **bp_link;
8b07ae33 617 int err;
611cb4a5 618
414a389f
PA
619 bp = proc->breakpoints;
620 bp_link = &proc->breakpoints;
621
622 while (bp)
611cb4a5 623 {
414a389f 624 if (bp == todel)
611cb4a5 625 {
414a389f
PA
626 *bp_link = bp->next;
627
8b07ae33
PA
628 err = release_breakpoint (proc, bp);
629 if (err != 0)
630 return err;
631
632 bp = *bp_link;
633 return 0;
611cb4a5 634 }
414a389f
PA
635 else
636 {
637 bp_link = &bp->next;
638 bp = *bp_link;
639 }
611cb4a5 640 }
414a389f 641
611cb4a5 642 warning ("Could not find breakpoint in list.");
8b07ae33
PA
643 return ENOENT;
644}
645
219f2f23 646int
8b07ae33
PA
647delete_breakpoint (struct breakpoint *todel)
648{
649 struct process_info *proc = current_process ();
650 return delete_breakpoint_1 (proc, todel);
611cb4a5
DJ
651}
652
9f3a5c85 653struct breakpoint *
8b07ae33 654find_gdb_breakpoint_at (CORE_ADDR where)
611cb4a5 655{
95954743 656 struct process_info *proc = current_process ();
8b07ae33 657 struct breakpoint *bp;
611cb4a5 658
8b07ae33
PA
659 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
660 if (bp->type == gdb_breakpoint && bp->raw->pc == where)
661 return bp;
611cb4a5
DJ
662
663 return NULL;
664}
665
8b07ae33
PA
666int
667set_gdb_breakpoint_at (CORE_ADDR where)
68070c10 668{
8b07ae33
PA
669 struct breakpoint *bp;
670
671 if (breakpoint_data == NULL)
672 return 1;
673
d3bbe7a0
PA
674 /* If we see GDB inserting a second breakpoint at the same address,
675 then the first breakpoint must have disappeared due to a shared
676 library unload. On targets where the shared libraries are
677 handled by userspace, like SVR4, for example, GDBserver can't
678 tell if a library was loaded or unloaded. Since we refcount
679 breakpoints, if we didn't do this, we'd just increase the
680 refcount of the previous breakpoint at this address, but the trap
681 was not planted in the inferior anymore, thus the breakpoint
682 would never be hit. */
683 bp = find_gdb_breakpoint_at (where);
684 if (bp != NULL)
685 {
686 delete_gdb_breakpoint_at (where);
687
688 /* Might as well validate all other breakpoints. */
689 validate_breakpoints ();
690 }
691
8b07ae33
PA
692 bp = set_breakpoint_at (where, NULL);
693 if (bp == NULL)
694 return -1;
695
696 bp->type = gdb_breakpoint;
697 return 0;
698}
699
700int
701delete_gdb_breakpoint_at (CORE_ADDR addr)
702{
703 struct breakpoint *bp;
704 int err;
705
706 if (breakpoint_data == NULL)
707 return 1;
708
709 bp = find_gdb_breakpoint_at (addr);
710 if (bp == NULL)
711 return -1;
712
9f3a5c85
LM
713 /* Before deleting the breakpoint, make sure to free
714 its condition list. */
715 clear_gdb_breakpoint_conditions (addr);
8b07ae33
PA
716 err = delete_breakpoint (bp);
717 if (err)
718 return -1;
719
720 return 0;
721}
722
9f3a5c85
LM
723/* Clear all conditions associated with this breakpoint address. */
724
725void
726clear_gdb_breakpoint_conditions (CORE_ADDR addr)
727{
728 struct breakpoint *bp = find_gdb_breakpoint_at (addr);
412c89dd 729 struct point_cond_list *cond;
9f3a5c85
LM
730
731 if (bp == NULL || bp->cond_list == NULL)
732 return;
733
734 cond = bp->cond_list;
9f3a5c85
LM
735
736 while (cond != NULL)
737 {
412c89dd
LM
738 struct point_cond_list *cond_next;
739
740 cond_next = cond->next;
741 free (cond->cond->bytes);
9f3a5c85
LM
742 free (cond->cond);
743 free (cond);
412c89dd 744 cond = cond_next;
9f3a5c85
LM
745 }
746
747 bp->cond_list = NULL;
748}
749
750/* Add condition CONDITION to GDBserver's breakpoint BP. */
751
752void
753add_condition_to_breakpoint (struct breakpoint *bp,
754 struct agent_expr *condition)
755{
756 struct point_cond_list *new_cond;
757
758 /* Create new condition. */
759 new_cond = xcalloc (1, sizeof (*new_cond));
760 new_cond->cond = condition;
761
762 /* Add condition to the list. */
763 new_cond->next = bp->cond_list;
764 bp->cond_list = new_cond;
765}
766
767/* Add a target-side condition CONDITION to the breakpoint at ADDR. */
768
8b07ae33 769int
9f3a5c85
LM
770add_breakpoint_condition (CORE_ADDR addr, char **condition)
771{
772 struct breakpoint *bp = find_gdb_breakpoint_at (addr);
773 char *actparm = *condition;
774 struct agent_expr *cond;
775
776 if (bp == NULL)
777 return 1;
778
779 if (condition == NULL)
780 return 1;
781
782 cond = gdb_parse_agent_expr (&actparm);
783
784 if (cond == NULL)
785 {
786 fprintf (stderr, "Condition evaluation failed. "
787 "Assuming unconditional.\n");
788 return 0;
789 }
790
791 add_condition_to_breakpoint (bp, cond);
792
793 *condition = actparm;
794
795 return 0;
796}
797
798/* Evaluate condition (if any) at breakpoint BP. Return 1 if
799 true and 0 otherwise. */
800
801int
802gdb_condition_true_at_breakpoint (CORE_ADDR where)
8b07ae33 803{
9f3a5c85 804 /* Fetch registers for the current inferior. */
8b07ae33 805 struct breakpoint *bp = find_gdb_breakpoint_at (where);
9f3a5c85
LM
806 ULONGEST value = 0;
807 struct point_cond_list *cl;
808 int err = 0;
809
810 struct regcache *regcache = get_thread_regcache (current_inferior, 1);
811
812 if (bp == NULL)
813 return 0;
8b07ae33 814
9f3a5c85
LM
815 /* Check if the breakpoint is unconditional. If it is,
816 the condition always evaluates to TRUE. */
817 if (bp->cond_list == NULL)
818 return 1;
819
820 /* Evaluate each condition in the breakpoint's list of conditions.
821 Return true if any of the conditions evaluates to TRUE.
822
823 If we failed to evaluate the expression, TRUE is returned. This
824 forces GDB to reevaluate the conditions. */
825 for (cl = bp->cond_list;
826 cl && !value && !err; cl = cl->next)
827 {
828 /* Evaluate the condition. */
829 err = gdb_eval_agent_expr (regcache, NULL, cl->cond, &value);
830 }
831
832 if (err)
833 return 1;
834
835 return (value != 0);
836}
837
838/* Return 1 if there is a breakpoint inserted in address WHERE
839 and if its condition, if it exists, is true. */
840
841int
842gdb_breakpoint_here (CORE_ADDR where)
843{
844 return (find_gdb_breakpoint_at (where) != NULL);
68070c10
PA
845}
846
d50171e4
PA
847void
848set_reinsert_breakpoint (CORE_ADDR stop_at)
611cb4a5 849{
414a389f
PA
850 struct breakpoint *bp;
851
852 bp = set_breakpoint_at (stop_at, NULL);
414a389f 853 bp->type = reinsert_breakpoint;
611cb4a5
DJ
854}
855
856void
d50171e4 857delete_reinsert_breakpoints (void)
611cb4a5 858{
d50171e4
PA
859 struct process_info *proc = current_process ();
860 struct breakpoint *bp, **bp_link;
611cb4a5 861
d50171e4
PA
862 bp = proc->breakpoints;
863 bp_link = &proc->breakpoints;
611cb4a5 864
d50171e4
PA
865 while (bp)
866 {
414a389f
PA
867 if (bp->type == reinsert_breakpoint)
868 {
869 *bp_link = bp->next;
8b07ae33 870 release_breakpoint (proc, bp);
414a389f
PA
871 bp = *bp_link;
872 }
873 else
874 {
875 bp_link = &bp->next;
876 bp = *bp_link;
877 }
d50171e4
PA
878 }
879}
b65d95c5 880
d50171e4 881static void
8b07ae33 882uninsert_raw_breakpoint (struct raw_breakpoint *bp)
d50171e4
PA
883{
884 if (bp->inserted)
885 {
886 int err;
6bf36717 887 unsigned char buf[MAX_BREAKPOINT_LEN];
d50171e4
PA
888
889 bp->inserted = 0;
fa593d66
PA
890 /* Since there can be fast tracepoint jumps inserted in the same
891 address range, we use `write_inferior_memory', which takes
892 care of layering breakpoints on top of fast tracepoints, and
893 on top of the buffer we pass it. This works because we've
894 already unlinked the fast tracepoint jump above. Also note
895 that we need to pass the current shadow contents, because
896 write_inferior_memory updates any shadow memory with what we
897 pass here, and we want that to be a nop. */
6bf36717
JK
898 memcpy (buf, bp->old_data, breakpoint_len);
899 err = write_inferior_memory (bp->pc, buf, breakpoint_len);
d50171e4
PA
900 if (err != 0)
901 {
902 bp->inserted = 1;
611cb4a5 903
d50171e4
PA
904 if (debug_threads)
905 fprintf (stderr,
906 "Failed to uninsert raw breakpoint at 0x%s (%s).\n",
907 paddress (bp->pc), strerror (err));
908 }
909 }
611cb4a5
DJ
910}
911
912void
d50171e4 913uninsert_breakpoints_at (CORE_ADDR pc)
611cb4a5 914{
8b07ae33 915 struct raw_breakpoint *bp;
611cb4a5 916
8b07ae33 917 bp = find_raw_breakpoint_at (pc);
611cb4a5 918 if (bp == NULL)
d50171e4
PA
919 {
920 /* This can happen when we remove all breakpoints while handling
921 a step-over. */
922 if (debug_threads)
923 fprintf (stderr,
924 "Could not find breakpoint at 0x%s "
925 "in list (uninserting).\n",
926 paddress (pc));
927 return;
928 }
611cb4a5 929
d50171e4 930 if (bp->inserted)
8b07ae33 931 uninsert_raw_breakpoint (bp);
611cb4a5
DJ
932}
933
0fb4aa4b
PA
934void
935uninsert_all_breakpoints (void)
936{
937 struct process_info *proc = current_process ();
938 struct raw_breakpoint *bp;
939
940 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
941 if (bp->inserted)
942 uninsert_raw_breakpoint (bp);
943}
944
d50171e4 945static void
8b07ae33 946reinsert_raw_breakpoint (struct raw_breakpoint *bp)
611cb4a5 947{
d50171e4 948 int err;
611cb4a5 949
d50171e4 950 if (bp->inserted)
611cb4a5
DJ
951 error ("Breakpoint already inserted at reinsert time.");
952
d50171e4
PA
953 err = (*the_target->write_memory) (bp->pc, breakpoint_data,
954 breakpoint_len);
955 if (err == 0)
956 bp->inserted = 1;
957 else if (debug_threads)
958 fprintf (stderr,
959 "Failed to reinsert breakpoint at 0x%s (%s).\n",
960 paddress (bp->pc), strerror (err));
611cb4a5
DJ
961}
962
d50171e4
PA
963void
964reinsert_breakpoints_at (CORE_ADDR pc)
611cb4a5 965{
8b07ae33 966 struct raw_breakpoint *bp;
611cb4a5 967
8b07ae33 968 bp = find_raw_breakpoint_at (pc);
611cb4a5 969 if (bp == NULL)
611cb4a5 970 {
d50171e4
PA
971 /* This can happen when we remove all breakpoints while handling
972 a step-over. */
973 if (debug_threads)
974 fprintf (stderr,
8b07ae33 975 "Could not find raw breakpoint at 0x%s "
d50171e4
PA
976 "in list (reinserting).\n",
977 paddress (pc));
978 return;
611cb4a5
DJ
979 }
980
414a389f 981 reinsert_raw_breakpoint (bp);
d50171e4
PA
982}
983
0fb4aa4b
PA
984void
985reinsert_all_breakpoints (void)
986{
987 struct process_info *proc = current_process ();
988 struct raw_breakpoint *bp;
989
990 for (bp = proc->raw_breakpoints; bp != NULL; bp = bp->next)
991 if (!bp->inserted)
992 reinsert_raw_breakpoint (bp);
993}
994
d50171e4
PA
995void
996check_breakpoints (CORE_ADDR stop_pc)
997{
998 struct process_info *proc = current_process ();
999 struct breakpoint *bp, **bp_link;
1000
1001 bp = proc->breakpoints;
1002 bp_link = &proc->breakpoints;
1003
1004 while (bp)
b65d95c5 1005 {
8b07ae33 1006 if (bp->raw->pc == stop_pc)
d50171e4 1007 {
8b07ae33 1008 if (!bp->raw->inserted)
d50171e4
PA
1009 {
1010 warning ("Hit a removed breakpoint?");
1011 return;
1012 }
1013
1014 if (bp->handler != NULL && (*bp->handler) (stop_pc))
1015 {
1016 *bp_link = bp->next;
1017
8b07ae33 1018 release_breakpoint (proc, bp);
d50171e4
PA
1019
1020 bp = *bp_link;
1021 continue;
1022 }
1023 }
1024
1025 bp_link = &bp->next;
1026 bp = *bp_link;
b65d95c5 1027 }
611cb4a5
DJ
1028}
1029
1030void
f450004a 1031set_breakpoint_data (const unsigned char *bp_data, int bp_len)
611cb4a5
DJ
1032{
1033 breakpoint_data = bp_data;
1034 breakpoint_len = bp_len;
1035}
1036
d50171e4
PA
1037int
1038breakpoint_here (CORE_ADDR addr)
1039{
8b07ae33 1040 return (find_raw_breakpoint_at (addr) != NULL);
d50171e4
PA
1041}
1042
1043int
1044breakpoint_inserted_here (CORE_ADDR addr)
1045{
8b07ae33 1046 struct raw_breakpoint *bp;
d50171e4 1047
8b07ae33 1048 bp = find_raw_breakpoint_at (addr);
d50171e4 1049
8b07ae33 1050 return (bp != NULL && bp->inserted);
d50171e4
PA
1051}
1052
d3bbe7a0
PA
1053static int
1054validate_inserted_breakpoint (struct raw_breakpoint *bp)
1055{
1056 unsigned char *buf;
1057 int err;
1058
1059 gdb_assert (bp->inserted);
1060
1061 buf = alloca (breakpoint_len);
1062 err = (*the_target->read_memory) (bp->pc, buf, breakpoint_len);
1063 if (err || memcmp (buf, breakpoint_data, breakpoint_len) != 0)
1064 {
1065 /* Tag it as gone. */
1066 bp->inserted = 0;
1067 bp->shlib_disabled = 1;
1068 return 0;
1069 }
1070
1071 return 1;
1072}
1073
1074static void
1075delete_disabled_breakpoints (void)
1076{
1077 struct process_info *proc = current_process ();
1078 struct breakpoint *bp, *next;
1079
1080 for (bp = proc->breakpoints; bp != NULL; bp = next)
1081 {
1082 next = bp->next;
1083 if (bp->raw->shlib_disabled)
1084 delete_breakpoint_1 (proc, bp);
1085 }
1086}
1087
1088/* Check if breakpoints we inserted still appear to be inserted. They
1089 may disappear due to a shared library unload, and worse, a new
1090 shared library may be reloaded at the same address as the
1091 previously unloaded one. If that happens, we should make sure that
1092 the shadow memory of the old breakpoints isn't used when reading or
1093 writing memory. */
1094
1095void
1096validate_breakpoints (void)
1097{
1098 struct process_info *proc = current_process ();
1099 struct breakpoint *bp;
1100
1101 for (bp = proc->breakpoints; bp != NULL; bp = bp->next)
1102 {
1103 if (bp->raw->inserted)
1104 validate_inserted_breakpoint (bp->raw);
1105 }
1106
1107 delete_disabled_breakpoints ();
1108}
1109
611cb4a5 1110void
f450004a 1111check_mem_read (CORE_ADDR mem_addr, unsigned char *buf, int mem_len)
611cb4a5 1112{
95954743 1113 struct process_info *proc = current_process ();
8b07ae33 1114 struct raw_breakpoint *bp = proc->raw_breakpoints;
fa593d66 1115 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
611cb4a5 1116 CORE_ADDR mem_end = mem_addr + mem_len;
d3bbe7a0 1117 int disabled_one = 0;
611cb4a5 1118
fa593d66
PA
1119 for (; jp != NULL; jp = jp->next)
1120 {
1121 CORE_ADDR bp_end = jp->pc + jp->length;
1122 CORE_ADDR start, end;
1123 int copy_offset, copy_len, buf_offset;
1124
6bf36717
JK
1125 gdb_assert (fast_tracepoint_jump_shadow (jp) >= buf + mem_len
1126 || buf >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1127
fa593d66
PA
1128 if (mem_addr >= bp_end)
1129 continue;
1130 if (jp->pc >= mem_end)
1131 continue;
1132
1133 start = jp->pc;
1134 if (mem_addr > start)
1135 start = mem_addr;
1136
1137 end = bp_end;
1138 if (end > mem_end)
1139 end = mem_end;
1140
1141 copy_len = end - start;
1142 copy_offset = start - jp->pc;
1143 buf_offset = start - mem_addr;
1144
1145 if (jp->inserted)
1146 memcpy (buf + buf_offset,
1147 fast_tracepoint_jump_shadow (jp) + copy_offset,
1148 copy_len);
1149 }
1150
611cb4a5
DJ
1151 for (; bp != NULL; bp = bp->next)
1152 {
1153 CORE_ADDR bp_end = bp->pc + breakpoint_len;
1154 CORE_ADDR start, end;
1155 int copy_offset, copy_len, buf_offset;
1156
6bf36717
JK
1157 gdb_assert (bp->old_data >= buf + mem_len
1158 || buf >= &bp->old_data[sizeof (bp->old_data)]);
1159
611cb4a5
DJ
1160 if (mem_addr >= bp_end)
1161 continue;
1162 if (bp->pc >= mem_end)
1163 continue;
1164
1165 start = bp->pc;
1166 if (mem_addr > start)
1167 start = mem_addr;
1168
1169 end = bp_end;
1170 if (end > mem_end)
1171 end = mem_end;
1172
1173 copy_len = end - start;
1174 copy_offset = start - bp->pc;
1175 buf_offset = start - mem_addr;
1176
8b07ae33 1177 if (bp->inserted)
d3bbe7a0
PA
1178 {
1179 if (validate_inserted_breakpoint (bp))
1180 memcpy (buf + buf_offset, bp->old_data + copy_offset, copy_len);
1181 else
1182 disabled_one = 1;
1183 }
611cb4a5 1184 }
d3bbe7a0
PA
1185
1186 if (disabled_one)
1187 delete_disabled_breakpoints ();
611cb4a5
DJ
1188}
1189
1190void
b9fd1791
PA
1191check_mem_write (CORE_ADDR mem_addr, unsigned char *buf,
1192 const unsigned char *myaddr, int mem_len)
611cb4a5 1193{
95954743 1194 struct process_info *proc = current_process ();
8b07ae33 1195 struct raw_breakpoint *bp = proc->raw_breakpoints;
fa593d66 1196 struct fast_tracepoint_jump *jp = proc->fast_tracepoint_jumps;
611cb4a5 1197 CORE_ADDR mem_end = mem_addr + mem_len;
d3bbe7a0 1198 int disabled_one = 0;
611cb4a5 1199
fa593d66
PA
1200 /* First fast tracepoint jumps, then breakpoint traps on top. */
1201
1202 for (; jp != NULL; jp = jp->next)
1203 {
1204 CORE_ADDR jp_end = jp->pc + jp->length;
1205 CORE_ADDR start, end;
1206 int copy_offset, copy_len, buf_offset;
1207
6bf36717
JK
1208 gdb_assert (fast_tracepoint_jump_shadow (jp) >= myaddr + mem_len
1209 || myaddr >= fast_tracepoint_jump_shadow (jp) + (jp)->length);
1210 gdb_assert (fast_tracepoint_jump_insn (jp) >= buf + mem_len
1211 || buf >= fast_tracepoint_jump_insn (jp) + (jp)->length);
1212
fa593d66
PA
1213 if (mem_addr >= jp_end)
1214 continue;
1215 if (jp->pc >= mem_end)
1216 continue;
1217
1218 start = jp->pc;
1219 if (mem_addr > start)
1220 start = mem_addr;
1221
1222 end = jp_end;
1223 if (end > mem_end)
1224 end = mem_end;
1225
1226 copy_len = end - start;
1227 copy_offset = start - jp->pc;
1228 buf_offset = start - mem_addr;
1229
1230 memcpy (fast_tracepoint_jump_shadow (jp) + copy_offset,
b9fd1791 1231 myaddr + buf_offset, copy_len);
fa593d66
PA
1232 if (jp->inserted)
1233 memcpy (buf + buf_offset,
1234 fast_tracepoint_jump_insn (jp) + copy_offset, copy_len);
1235 }
1236
611cb4a5
DJ
1237 for (; bp != NULL; bp = bp->next)
1238 {
1239 CORE_ADDR bp_end = bp->pc + breakpoint_len;
1240 CORE_ADDR start, end;
1241 int copy_offset, copy_len, buf_offset;
1242
6bf36717
JK
1243 gdb_assert (bp->old_data >= myaddr + mem_len
1244 || myaddr >= &bp->old_data[sizeof (bp->old_data)]);
1245
611cb4a5
DJ
1246 if (mem_addr >= bp_end)
1247 continue;
1248 if (bp->pc >= mem_end)
1249 continue;
1250
1251 start = bp->pc;
1252 if (mem_addr > start)
1253 start = mem_addr;
1254
1255 end = bp_end;
1256 if (end > mem_end)
1257 end = mem_end;
1258
1259 copy_len = end - start;
1260 copy_offset = start - bp->pc;
1261 buf_offset = start - mem_addr;
1262
b9fd1791 1263 memcpy (bp->old_data + copy_offset, myaddr + buf_offset, copy_len);
d50171e4 1264 if (bp->inserted)
d3bbe7a0
PA
1265 {
1266 if (validate_inserted_breakpoint (bp))
1267 memcpy (buf + buf_offset, breakpoint_data + copy_offset, copy_len);
1268 else
1269 disabled_one = 1;
1270 }
611cb4a5 1271 }
d3bbe7a0
PA
1272
1273 if (disabled_one)
1274 delete_disabled_breakpoints ();
611cb4a5 1275}
ae13219e 1276
95954743 1277/* Delete all breakpoints, and un-insert them from the inferior. */
ae13219e
DJ
1278
1279void
1280delete_all_breakpoints (void)
1281{
95954743
PA
1282 struct process_info *proc = current_process ();
1283
1284 while (proc->breakpoints)
8b07ae33 1285 delete_breakpoint_1 (proc, proc->breakpoints);
95954743
PA
1286}
1287
f9e39928 1288/* Clear the "inserted" flag in all breakpoints. */
95954743
PA
1289
1290void
f9e39928 1291mark_breakpoints_out (struct process_info *proc)
95954743 1292{
8b07ae33 1293 struct raw_breakpoint *raw_bp;
95954743 1294
8b07ae33
PA
1295 for (raw_bp = proc->raw_breakpoints; raw_bp != NULL; raw_bp = raw_bp->next)
1296 raw_bp->inserted = 0;
f9e39928
PA
1297}
1298
1299/* Release all breakpoints, but do not try to un-insert them from the
1300 inferior. */
1301
1302void
1303free_all_breakpoints (struct process_info *proc)
1304{
1305 mark_breakpoints_out (proc);
8b07ae33
PA
1306
1307 /* Note: use PROC explicitly instead of deferring to
1308 delete_all_breakpoints --- CURRENT_INFERIOR may already have been
1309 released when we get here. There should be no call to
1310 current_process from here on. */
95954743 1311 while (proc->breakpoints)
8b07ae33 1312 delete_breakpoint_1 (proc, proc->breakpoints);
ae13219e 1313}
This page took 0.783151 seconds and 4 git commands to generate.