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