Stackdb
Stackdb is a stackable, multi-target and -level source debugger and memory forensics library.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
probe.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, 2012, 2013, 2014 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include <stdlib.h>
20 #include <string.h>
21 #include <glib.h>
22 
23 #include "common.h"
24 #include "log.h"
25 
26 #include "dwdebug.h"
27 #include "dwdebug_priv.h"
28 
29 #include "target_api.h"
30 #include "target.h"
31 
32 #include "probe_api.h"
33 #include "probe.h"
34 
35 /*
36  * Local prototypes.
37  */
38 static void action_finish_handling(struct action *action,
39  struct thread_action_context *tac);
40 
41 /*
42  * If the user doesn't supply pre/post handlers, and the probe has sinks
43  * attached to it, we invoke these handlers to pass the event to the
44  * sinks. Users that write their own handlers should call these
45  * handlers from within their handler, so that sinks can attach to their
46  * handlers if desired.
47  */
49  void *handler_data,struct probe *trigger,
50  struct probe *base) {
51  struct probe *ptmp;
52  GList *list;
53  int retval = 0;
54  int rc;
55 
56  if (probe->sinks) {
57  vdebug(5,LA_PROBE,LF_PROBE,"");
59 
60  list = probe->sinks;
61  while (list) {
62  ptmp = (struct probe *)list->data;
63 
64  /*
65  * Do this stuff regardless of if there's a prehandler or
66  * not! Why? Because if we do it for the prehandler, we
67  * have to do it for the posthandler too. Since we can't
68  * tell if we did it or didn't do it for the prehandler once
69  * we get to the posthandler, we ALWAYS have to do it!
70  */
71  PROBE_SAFE_OP_ARGS(ptmp,values_notify_phase,tid,PHASE_PRE_START);
72 
73  /*
74  * Signal each of the sinks, IF their threads match (thus a
75  * sink can act as a filter on a thread id.
76  */
77  if (ptmp->pre_handler
78  && (ptmp->tid == TID_GLOBAL
79  || ptmp->thread->tid == TID_GLOBAL
80  || ptmp->thread->tid == tid)
81  && probe_filter_check(ptmp,tid,probe,0) == 0) {
82 
83  vdebug(8,LA_PROBE,LF_PROBE,"running pre handler for ");
85 
86  rc = ptmp->pre_handler(ptmp,tid,ptmp->handler_data,probe,base);
87  if (rc == RESULT_ERROR) {
88  probe_disable(ptmp);
89  retval |= rc;
90  }
91  }
92  else {
93  vdebug(8,LA_PROBE,LF_PROBE,"not running pre handler for ");
95  vdebugc(8,LA_PROBE,LF_PROBE,"tid %d probe tid %d; sink tid %d\n",
96  tid,probe->thread->tid,ptmp->thread->tid);
97  }
98 
99 
100  PROBE_SAFE_OP_ARGS(ptmp,values_notify_phase,tid,PHASE_PRE_END);
101 
102  list = g_list_next(list);
103  }
104  }
105 
106  return retval;
107 }
108 
110  void *handler_data,struct probe *trigger,
111  struct probe *base) {
112  struct probe *ptmp;
113  GList *list;
114  int retval = 0;
115  int rc;
116 
117  if (probe->sinks) {
118  vdebug(5,LA_PROBE,LF_PROBE,"");
120 
121  list = probe->sinks;
122  while (list) {
123  ptmp = (struct probe *)list->data;
124 
125  PROBE_SAFE_OP_ARGS(ptmp,values_notify_phase,tid,PHASE_POST_START);
126 
127  /*
128  * Signal each of the sinks, IF their threads match (thus a
129  * sink can act as a filter on a thread id.
130  */
131  if (ptmp->post_handler
132  && (ptmp->tid == TID_GLOBAL
133  || ptmp->thread->tid == TID_GLOBAL
134  || ptmp->thread->tid == tid)
135  && probe_filter_check(ptmp,tid,probe,1) == 0) {
136 
137  vdebug(8,LA_PROBE,LF_PROBE,"running post handler for ");
139 
140  rc = ptmp->post_handler(ptmp,tid,ptmp->handler_data,probe,base);
141  if (rc == RESULT_ERROR) {
142  probe_disable(ptmp);
143  retval |= rc;
144  }
145  }
146  else {
147  vdebug(8,LA_PROBE,LF_PROBE,"not running post handler for ");
148  LOGDUMPPROBE(6,LA_PROBE,LF_PROBE,probe);
149  vdebugc(8,LA_PROBE,LF_PROBE,"tid %d probe tid %d; sink tid %d\n",
150  tid,probe->thread->tid,ptmp->thread->tid);
151  }
152 
153  PROBE_SAFE_OP_ARGS(ptmp,values_notify_phase,tid,PHASE_POST_END);
154 
155  list = g_list_next(list);
156  }
157  }
158 
159  return retval;
160 }
161 
162 static struct probepoint *__probepoint_create(struct target *target,ADDR addr,
163  struct memrange *range,
168  struct bsymbol *bsymbol,
169  ADDR symbol_addr) {
170  struct probepoint *probepoint;
171 
172  probepoint = (struct probepoint *)malloc(sizeof(*probepoint));
173  if (!probepoint) {
174  verror("failed to allocate a new probepoint");
175  return NULL;
176  }
177  memset(probepoint,0,sizeof(*probepoint));
178 
179  probepoint->addr = addr;
180  probepoint->range = range;
181  probepoint->target = target;
182  probepoint->thread = NULL;
183  probepoint->state = PROBE_DISABLED;
184 
185  probepoint->type = type;
186  probepoint->style = probepoint->orig_style = style;
187  probepoint->whence = whence;
188  probepoint->watchsize = watchsize;
189 
190  if (bsymbol) {
191  probepoint->bsymbol = bsymbol;
192  RHOLD(bsymbol,probepoint);
193  }
194  probepoint->symbol_addr = symbol_addr;
195 
196  probepoint->debugregnum = -1;
197 
198  INIT_LIST_HEAD(&probepoint->probes);
199  INIT_LIST_HEAD(&probepoint->simple_actions);
200  INIT_LIST_HEAD(&probepoint->ss_actions);
201  INIT_LIST_HEAD(&probepoint->complex_actions);
202 
203  if (target->ops->instr_can_switch_context) {
204  if ((probepoint->can_switch_context = \
205  target->ops->instr_can_switch_context(target,addr)) < 0) {
206  vwarn("could not determine if instr at 0x%"PRIxADDR" can switch"
207  " context; but continuing!\n",addr);
208  probepoint->can_switch_context = 0;
209  }
210  }
211 
212  vdebug(5,LA_PROBE,LF_PROBEPOINT,"created ");
214  if (probepoint->can_switch_context) {
215  vdebugc(5,LA_PROBE,LF_PROBEPOINT," (instr can switch context (0x%x)\n",
216  probepoint->can_switch_context);
217  }
218  else
220 
221  return probepoint;
222 }
223 
224 static struct probepoint *probepoint_create_break(struct target *target,
225  ADDR addr,
226  struct memrange *range,
227  probepoint_style_t style,
228  struct bsymbol *bsymbol,
229  ADDR symbol_addr) {
230  struct probepoint *probepoint = __probepoint_create(target,addr,range,
231  PROBEPOINT_BREAK,style,
232  PROBEPOINT_EXEC,0,
233  bsymbol,symbol_addr);
234  return probepoint;
235 }
236 
237 static struct probepoint *probepoint_create_watch(struct target *target,
238  ADDR addr,
239  struct memrange *range,
240  probepoint_style_t style,
241  probepoint_whence_t whence,
242  probepoint_watchsize_t watchsize,
243  struct bsymbol *bsymbol,
244  ADDR symbol_addr) {
245  if (style == PROBEPOINT_SW) {
246  verror("software watchpoints not supported right now!\n");
247  errno = EINVAL;
248  return NULL;
249  }
250 
251  return __probepoint_create(target,addr,range,PROBEPOINT_WATCH,style,
252  whence,watchsize,bsymbol,symbol_addr);
253 }
254 
255 static void probepoint_free_internal(struct probepoint *probepoint) {
256  struct probe *probe;
257  struct probe *ptmp;
258  struct action *action;
259  struct action *atmp;
260  REFCNT trefcnt;
261 
262  /* Destroy any actions it might have (probe_free does this too,
263  * but this is much more efficient.
264  */
265  list_for_each_entry_safe(action,atmp,&probepoint->simple_actions,action) {
266  action_cancel(action);
267  RPUT(action,action,probepoint,trefcnt);
268  }
269  list_for_each_entry_safe(action,atmp,&probepoint->ss_actions,action) {
270  action_cancel(action);
271  RPUT(action,action,probepoint,trefcnt);
272  }
273  list_for_each_entry_safe(action,atmp,&probepoint->complex_actions,action) {
274  action_cancel(action);
275  RPUT(action,action,probepoint,trefcnt);
276  }
277 
278  /* Destroy the probes. */
279  list_for_each_entry_safe(probe,ptmp,&probepoint->probes,probe) {
280  probe->probepoint = NULL;
281  if (probe->autofree)
282  probe_free(probe,0);
283  }
284  /* XXX: we could also go *up* the src/sink chain and destroy all the
285  * sinks... should we?
286  */
287 
288  if (probepoint->bsymbol) {
289  RPUT(probepoint->bsymbol,bsymbol,probepoint,trefcnt);
290  probepoint->bsymbol = NULL;
291  }
292 }
293 
297 static int __probepoint_remove(struct probepoint *probepoint,int force,
298  int nohashdelete) {
299  struct target *target;
300  tid_t tid,htid;
301  int ret;
302  struct thread_probepoint_context *tpc;
303  int action_did_obviate = 0;
304  int fake = 0;
305 
306  target = probepoint->target;
307 
308  /* Check if the probepoint has already been inserted; we do not want
309  * to backup a previously inserted breakpoint.
310  */
311  if (probepoint->state == PROBE_DISABLED) {
312  /* return success, the probepoint is already removed */
315  vdebugc(11,LA_PROBE,LF_PROBEPOINT," already disabled\n");
316 
317  return 0;
318  }
319 
320  /*
321  * If the style is software, and it's a watchpoint, forget it; we
322  * don't support that right now.
323  */
324  if (probepoint->style == PROBEPOINT_SW
325  && probepoint->type == PROBEPOINT_WATCH) {
326  verror("no software watchpoint support!\n");
327  errno = EINVAL;
328  return 1;
329  }
330 
331  if (!target_is_open(target)) {
333  "target is not attached; emulating probepoint removal ");
335  fake = 1;
336  }
337  else {
338  vdebug(5,LA_PROBE,LF_PROBEPOINT,"removing ");
340  }
341 
342  /*
343  * If the probepoint is not currently being handled, simply remove
344  * it. Otherwise, we have to handle complex cases!
345  */
346  if (probepoint->state == PROBE_BP_SET) {
347  vdebug(7,LA_PROBE,LF_PROBE,"doing easy removal of ");
348  LOGDUMPPROBEPOINT(7,LA_PROBE,LF_PROBE,probepoint);
349  vdebugc(7,LA_PROBE,LF_PROBE,"; removing probepoint!\n");
350  }
351  /*
352  * Handle complex stuff :).
353  */
354  else if (!force && !fake) {
355  vwarn("probepoint being handled (state %d); not forcing removal yet!\n",
356  probepoint->state);
357  errno = EAGAIN;
358  return -1;
359  }
360  else {
361  if (probepoint->state == PROBE_ACTION_RUNNING) {
362  if (!fake)
363  vwarn("forced probepoint removal while it is running action;"
364  " trying to clean up normally!\n");
365  /* We need to remove the action code, if any, reset the EIP
366  * to what it would have been if we had just hit the BP, and
367  * then do the normal breakpoint removal.
368  */
369  tpc = probepoint->tpc;
370 
371  if (!fake && target_enable_sw_breakpoint(target,tpc->thread->tid,
372  probepoint->mmod)) {
373  verror("could not reenable sw breakpoint to remove action;"
374  " badness will probably ensue!\n");
375  probepoint->state = PROBE_DISABLED;
376  }
377  else {
378  probepoint->state = PROBE_BP_SET;
379  }
380 
381  if (tpc->action_obviated_orig)
382  action_did_obviate = 1;
383 
384  /* NULL these out to be safe. */
385  tpc->tac.action = NULL;
386  tpc->tac.stepped = 0;
387  tpc->action_obviated_orig = 0;
388  }
389  else if (probepoint->state == PROBE_BP_PREHANDLING) {
391  "force probepoint removal while prehandling it;"
392  " trying to clean up normally!\n");
393  }
394  else if (probepoint->state == PROBE_BP_ACTIONHANDLING) {
396  "force probepoint removal while handling an action;"
397  " trying to clean up normally!\n");
398  }
399  else if (probepoint->state == PROBE_BP_POSTHANDLING) {
401  "force probepoint removal while posthandling it;"
402  " trying to clean up normally!\n");
403  }
404  else if (probepoint->state == PROBE_INSERTING) {
406  "forced probepoint removal while it is inserting;"
407  " trying to clean up normally!\n");
408  probepoint->state = PROBE_BP_SET;
409  }
410 
411  /*
412  * If we're doing initial BP handling, reset EIP to the
413  * probepoint addr; else if we're doing BP handling after the
414  * single step, *don't* reset IP, since we already did the
415  * original instruction. UNLESS we were executing an action
416  * that obviated the original code control flow -- then we
417  * replace the original code below, BUT DO NOT update EIP!
418  *
419  * Also cancel any singlesteps that (might) be happening! We
420  * don't quite track if we have stepped a thread, so just do it
421  * if we *might* have (i.e., if the probepoint is being handled
422  * for a thread).
423  */
424  if (probepoint->state != PROBE_DISABLED) {
425  /* Reset EIP to the right thing. */
426  if (!target->no_adjust_bp_ip
427  && (probepoint->state == PROBE_BP_PREHANDLING
428  || probepoint->state == PROBE_BP_ACTIONHANDLING
429  || probepoint->state == PROBE_ACTION_RUNNING
430  || probepoint->state == PROBE_ACTION_DONE)
431  && !action_did_obviate
432  && probepoint->type != PROBEPOINT_WATCH) {
433  /* We still must execute the original instruction. */
434  /* BUT NOT for watchpoints! We do not know anything
435  * about the original instruction.
436  */
437  tid = probepoint->thread->tid;
438 
439  if (!fake && target_write_reg(target,tid,
440  target->ipregno,probepoint->addr)) {
441  verror("could not reset IP to bp addr 0x%"PRIxADDR" for"
442  " forced breakpoint remove; badness will probably"
443  " ensue!\n",
444  probepoint->addr);
445  }
446  }
447 
448  /* Cancel any executing single step! */
449  if (probepoint->tpc) {
450  if (target_singlestep_end(target,probepoint->tpc->thread->tid))
451  verror("could not stop single stepping target tid %"PRIiTID
452  " after failed sstep for breakpoint!\n",
453  probepoint->tpc->thread->tid);
454  }
455  }
456  }
457 
458  /*
459  * If we're going to remove it, do it!
460  */
461 
462  /*
463  * If it's hardware, use the target API to remove it.
464  */
465  if (probepoint->style == PROBEPOINT_HW) {
466  /*
467  * XXX: bit of a lie here; if we're detached, we should still
468  * try to clean up hw probepoint state somewhere...
469  */
470  if (!fake && probepoint->debugregnum > -1) {
471  htid = probepoint->thread->tid;
472 
473  if (probepoint->type == PROBEPOINT_BREAK) {
474  if ((ret = target_unset_hw_breakpoint(target,htid,
475  probepoint->debugregnum))) {
476  verror("failure while removing hw breakpoint; cannot recover!\n");
477  }
478  else {
479  vdebug(4,LA_PROBE,LF_PROBEPOINT,"removed HW break ");
481  }
482  }
483  else {
484  if ((ret = target_unset_hw_watchpoint(target,htid,
485  probepoint->debugregnum))) {
486  verror("failure while removing hw watchpoint; cannot recover!\n");
487  }
488  else {
489  vdebug(4,LA_PROBE,LF_PROBEPOINT,"removed HW watch ");
491  }
492  }
493  }
494  else
495  ret = 0;
496 
497  if (ret)
498  return 1;
499  else if (!nohashdelete)
500  target_remove_probepoint(target,probepoint->thread,probepoint);
501 
502  probepoint->debugregnum = -1;
503  }
504  /*
505  * Otherwise do software. NB: we might get here twice; so make sure
506  * the probepoint is still associated with a thread. If not, don't
507  * try again to remove it from the target.
508  */
509  else if (probepoint->thread) {
510  if (!fake) {
511  /* restore the original instruction */
512  if (target_remove_sw_breakpoint(target,probepoint->thread->tid,
513  probepoint->mmod)) {
514  verror("could not remove SW break at 0x%"PRIxADDR"\n",
515  probepoint->addr);
516  return 1;
517  }
518 
519  probepoint->mmod = NULL;
520 
521  vdebug(4,LA_PROBE,LF_PROBEPOINT,"removed SW break ");
523  }
524 
525  if (!nohashdelete)
526  target_remove_probepoint(target,probepoint->thread,probepoint);
527  }
528 
529  probepoint->state = PROBE_DISABLED;
530 
531  vdebug(2,LA_PROBE,LF_PROBEPOINT,"removed ");
533  /*
534  * This is just in case it was registered with PROBEPOINT_FASTEST;
535  * we need to make sure if it gets re-registered that we make the
536  * choice of FASTEST again at that time.
537  *
538  * NB: this *must* come after calling target_remove_probepoint!
539  */
540  if (probepoint->style != probepoint->orig_style) {
541  vdebug(2,LA_PROBE,LF_PROBEPOINT,"removed (style was %d; now %d)",
542  probepoint->style,probepoint->orig_style);
543 
544  probepoint->style = probepoint->orig_style;
545  }
546  else
547  vdebug(2,LA_PROBE,LF_PROBEPOINT,"\n");
548 
549  return 0;
550 }
551 
552 static void probepoint_free(struct probepoint *probepoint) {
553  __probepoint_remove(probepoint,0,0);
554 
555  probepoint_free_internal(probepoint);
556 
557  vdebug(5,LA_PROBE,LF_PROBEPOINT,"freed ");
559 
560  free(probepoint);
561 }
562 
563 /* We need this in case the target needs to quickly remove all the
564  * probes (i.e., on a signal) -- and in that case, we have to let the
565  * target remove the probepoint from its hashtables itself.
566  *
567  * NB: this *forces* the removal of the probepoint, even if it is being
568  * handled, because we are forcing a removal from external.
569  */
570 void probepoint_free_ext(struct probepoint *probepoint) {
571  __probepoint_remove(probepoint,1,1);
572 
573  probepoint_free_internal(probepoint);
574 
575  vdebug(5,LA_PROBE,LF_PROBEPOINT,"freed (ext) ");
577 
578  free(probepoint);
579 }
580 
581 /*
582  * Note: you *must* pass the target_thread whose debug registers need to
583  * be written -- that means if TID_GLOBAL means a "real" thread on a
584  * target, like Xen, we need to modify the global thread's debug
585  * register state. For the ptrace target, where the TID_GLOBAL thread
586  * might be an alias for a real "primary" thread, we need to *not* have
587  * the global thread supplied here, but instead the real thread that is
588  * being aliased.
589  *
590  * So, this comment is just to highlight this issue for
591  * __probepoint_insert. In other functions in this probe library, once
592  * a probe is inserted, we carefully use the probepoint->thread->tid tid
593  * value for making hardware debug register state changes, if the
594  * probepoint is hardware.
595  */
596 static int __probepoint_insert(struct probepoint *probepoint,
597  struct target_thread *tthread) {
598  struct target *target;
599  tid_t tid;
600  int ret;
601  REG reg;
602 
603  target = probepoint->target;
604  tid = tthread->tid;
605 
606  /* Check if the probepoint has already been inserted; we do not want
607  * to backup a previously inserted breakpoint.
608  */
609  if (probepoint->state != PROBE_DISABLED) {
610  /* return success, the probepoint is already being managed */
613  vdebugc(11,LA_PROBE,LF_PROBEPOINT," already inserted\n");
614 
615  return 0;
616  }
617 
618  if (!target_is_open(target)) {
619  verror("target %d is not attached!\n",target->id);
620  return 1;
621  }
622 
623  vdebug(5,LA_PROBE,LF_PROBEPOINT,"inserting ");
625 
626  probepoint->state = PROBE_INSERTING;
627 
628  /*
629  * Check to see if there are any hardware resources; use them if so.
630  */
631  if (probepoint->style == PROBEPOINT_FASTEST) {
632  if ((reg = target_get_unused_debug_reg(target,tid)) > -1) {
633  probepoint->style = PROBEPOINT_HW;
634  probepoint->debugregnum = reg;
635 
636  vdebug(3,LA_PROBE,LF_PROBEPOINT,"using HW reg %d for ",reg);
638  }
639  else {
640  probepoint->style = PROBEPOINT_SW;
641 
642  vdebug(3,LA_PROBE,LF_PROBEPOINT,"using SW for FASTEST ");
644  }
645  }
646  else if (probepoint->style == PROBEPOINT_HW) {
647  if ((reg = target_get_unused_debug_reg(target,tid)) > -1) {
648  probepoint->debugregnum = reg;
649 
650  vdebug(3,LA_PROBE,LF_PROBEPOINT,"using HW reg %d for ",reg);
652  }
653  else {
654  vwarn("could not get a debug reg!\n");
655  errno = ENOMEM;
656  probepoint->state = PROBE_DISABLED;
657  return 1;
658  }
659  }
660 
661  /*
662  * If the style is software, and it's a watchpoint, forget it; we
663  * don't support that right now.
664  */
665  if (probepoint->style == PROBEPOINT_SW
666  && probepoint->type == PROBEPOINT_WATCH) {
667  verror("no software watchpoint support!\n");
668  errno = EINVAL;
669  probepoint->state = PROBE_DISABLED;
670  return 1;
671  }
672 
673  /*
674  * If it's hardware, use the target API to insert it.
675  */
676  if (probepoint->style == PROBEPOINT_HW) {
677  if (probepoint->type == PROBEPOINT_BREAK) {
678  if ((ret = target_set_hw_breakpoint(target,tid,probepoint->debugregnum,
679  probepoint->addr))) {
680  verror("failure inserting hw breakpoint!\n");
681  }
682  else {
683  vdebug(7,LA_PROBE,LF_PROBEPOINT,"inserted hw break at ");
685  }
686  }
687  else {
688  if ((ret = target_set_hw_watchpoint(target,tid,probepoint->debugregnum,
689  probepoint->addr,
690  probepoint->whence,
691  probepoint->watchsize))) {
692  verror("failure inserting hw watchpoint!\n");
693  }
694  else {
695  vdebug(7,LA_PROBE,LF_PROBEPOINT,"inserted hw watch at ");
697  }
698  }
699 
700  if (ret) {
701  probepoint->state = PROBE_DISABLED;
702  return 1;
703  }
704  }
705  /* Otherwise do software. */
706  else {
707  probepoint->mmod = target_insert_sw_breakpoint(target,tthread->tid,
708  probepoint->addr);
709  if (!probepoint->mmod) {
710  verror("could not insert sw breakpoint at 0x%"PRIxADDR"\n",
711  probepoint->addr);
712  probepoint->state = PROBE_DISABLED;
713  return 1;
714  }
715 
716  vdebug(3,LA_PROBE,LF_PROBEPOINT,"inserted SW ");
718  }
719 
720  target_insert_probepoint(target,tthread,probepoint);
721  probepoint->state = PROBE_BP_SET;
722 
723  vdebug(2,LA_PROBE,LF_PROBEPOINT,"inserted ");
725 
726  return 0;
727 }
728 
729 struct probe *probe_create(struct target *target,tid_t tid,struct probe_ops *pops,
730  const char *name,
733  void *handler_data,int autofree,int tracked) {
734  struct probe *probe;
735  struct target_thread *tthread;
736 
737  if (!(tthread = target_lookup_thread(target,tid))) {
738  verror("thread %"PRIiTID" not loaded yet?\n",tid);
739  return NULL;
740  }
741 
742  probe = (struct probe *)malloc(sizeof(*probe));
743  if (!probe) {
744  verror("failed to allocate a new probe\n");
745  return NULL;
746  }
747  memset(probe,0,sizeof(*probe));
748  probe->id = -1;
749 
750  probe->name = (name) ? strdup(name) : NULL;
751  probe->pre_handler = pre_handler;
752  probe->post_handler = post_handler;
753  probe->handler_data = handler_data;
754  probe->enabled = 0; // disabled at first
755  probe->autofree = autofree;
756  probe->tracked = tracked;
757  probe->ops = pops;
758  probe->tid = tid;
759 
760  target_attach_probe(target,tthread,probe);
761 
762  if (PROBE_SAFE_OP(probe,init)) {
763  verror("probe %s init failed, calling fini!\n",probe->name);
764  PROBE_SAFE_OP(probe,fini);
765  if (name)
766  free(probe->name);
767  free(probe);
768  return NULL;
769  }
770 
771  vdebug(5,LA_PROBE,LF_PROBE,"initialized ");
773 
774  return probe;
775 }
776 
777 int probe_free(struct probe *probe,int force) {
778  REFCNT trefcnt;
779  GList *list;
780  struct probe *sink;
781 
782  vdebug(5,LA_PROBE,LF_PROBE,"");
784 
785  if (probe->sinks && !force) {
787  "could not free probe %s with sinks remaining!\n",
788  probe->name);
789  return -1;
790  }
791  else if (probe->probepoint && !force) {
793  "could not free probe %s with probepoint remaining!\n",
794  probe->name);
795  return -1;
796  }
797 
798  if (probe->sinks) {
799  vwarn("forcefully freeing probe %s that had sinks remaining;"
800  " removing this source from those sinks!\n",
801  probe->name);
802 
803  list = probe->sinks;
804  while (list) {
805  sink = (struct probe *)list->data;
806 
807  if (!sink->sources) {
808  /* This would be weird, but just ignore it. */
809  vwarn("sink probe %s has no sources; although probe %s thinks"
810  " it's a source -- BUG!\n",sink->name,probe->name);
811  continue;
812  }
813 
814  sink->sources = g_list_remove(sink->sources,probe);
815 
816  if (!sink->sources || g_list_length(sink->sources) == 0) {
817  /* Only can call unregistered() if @probe was the last sink. */
818  if (PROBE_SAFE_OP(sink,unregistered)) {
819  verror("probe %s: unregistered failed!\n",
820  sink->name);
821  }
822  }
823 
824  /* NB: we cannot free the sink! The probe_free() function might
825  * call probe_unregister_source*(), so we cannot free a sink we
826  * might be freeing -- it might free the source we are
827  * freeing in this function right now! probe_free() can
828  * only go downwards.
829  */
830  //if (sink->autofree)
831  // return probe_free(sink,force);
832 
833  list = g_list_next(list);
834  }
835 
836  g_list_free(probe->sinks);
837  probe->sinks = NULL;
838  }
839 
840  /* If we still need to unregister, we call probe_unregister, which
841  * will call us again if the probe was an autofree probe. So, if it
842  * is an autofree probe, let probe_unregister call us again if it
843  * succeeds. If it fails, and we're forcing the free, do it anyway.
844  */
845  if (probe->probepoint || probe->sources) {
846  if (!probe_unregister(probe,force)) {
847  if (probe->autofree)
848  return 0;
849  }
850  else {
851  if (force)
852  verror("probe_unregister %s failed, forcing free to continue\n",
853  probe->name);
854  else {
855  verror("probe_unregister %s failed, not forcing!\n",probe->name);
856  return -1;
857  }
858  }
859  }
860 
861  if (probe->pre_filter) {
863  probe->pre_filter = NULL;
864  }
865  if (probe->post_filter) {
867  probe->post_filter = NULL;
868  }
869 
870  if (probe->values) {
871  PROBE_SAFE_OP(probe,values_free);
872  probe->values = NULL;
873  }
874 
875  if (PROBE_SAFE_OP(probe,fini)) {
876  verror("probe %s fini failed, aborting!\n",probe->name);
877  return -1;
878  }
879 
880  if (probe->target)
881  target_detach_probe(probe->target,probe);
882 
883  if (probe->bsymbol) {
884  RPUT(probe->bsymbol,bsymbol,probe,trefcnt);
885  probe->bsymbol = NULL;
886  }
887 
888  vdebug(5,LA_PROBE,LF_PROBE,"almost done: ");
890 
891  if (probe->name)
892  free(probe->name);
893  free(probe);
894 
895  return 0;
896 }
897 
898 void probe_rename(struct probe *probe,const char *name) {
899  vdebug(5,LA_PROBE,LF_PROBE,"renaming ");
901 
902  if (probe->name)
903  free(probe->name);
904 
905  probe->name = (name) ? strdup(name) : NULL;
906 
907  vdebugc(5,LA_PROBE,LF_PROBE," to ");
909 
910 }
911 
912 int probe_hard_disable(struct probe *probe,int force) {
913  struct probe *ptmp;
914  GList *list;
915  GList *list2;
916  int anyenabled = 0;
917  int rc = 0;
918 
919  list = probe->sources;
920  while (list) {
921  ptmp = (struct probe *)list->data;
922 
923  /* Disable recursively *if* the source doesn't have any
924  * enabled sinks.
925  */
926  if (probe_enabled(ptmp)) {
927  list2 = ptmp->sinks;
928  anyenabled = 0;
929  while (list2) {
930  if (((struct probe *)(list->data))->enabled) {
931  ++anyenabled;
932  }
933  list2 = g_list_next(list2);
934  }
935  if (!anyenabled || force) {
936  if (force) {
937  vdebug(3,LA_PROBE,LF_PROBE,"forcibly hard disabling source probe");
939  vdebug(3,LA_PROBE,LF_PROBE," although it has enabled sink!\n");
940  }
941  rc += probe_hard_disable(ptmp,force);
942  }
943  else if (anyenabled) {
944  vdebug(3,LA_PROBE,LF_PROBE,"not forcibly hard disabling source probe");
946  vdebug(3,LA_PROBE,LF_PROBE," because it has enabled sink(s)!\n");
947  ++rc;
948  }
949  list = g_list_next(list);
950  }
951  }
952 
953  if (probe_is_base(probe) && __probepoint_remove(probe->probepoint,force,0)) {
954  verror("failed to remove probepoint under probe (%d)\n!",force);
955  ++rc;
956  }
957 
958  return rc;
959 }
960 
961 int probe_hard_enable(struct probe *probe) {
962  struct probe *ptmp;
963  GList *list;
964  int rc = 0;
965 
966  /* Do it for all the sources. */
967  list = probe->sources;
968  while (list) {
969  ptmp = (struct probe *)list->data;
970 
971  rc += probe_hard_enable(ptmp);
972 
973  list = g_list_next(list);
974  }
975 
976  /* If we have a probepoint directly underneath, do it. */
977  if (probe_is_base(probe)
978  && __probepoint_insert(probe->probepoint,probe->thread)) {
979  verror("failed to insert probepoint under probe\n!");
980  ++rc;
981  }
982 
983  return rc;
984 }
985 
986 static int __probe_unregister(struct probe *probe,int force,int onlyone) {
987  struct probepoint *probepoint = probe->probepoint;
988  struct target *target = probe->target;
990  struct action *action;
991  struct action *tmp;
992  struct probe *ptmp;
993  GList *list;
994  REFCNT trefcnt;
995 
996  vdebug(5,LA_PROBE,LF_PROBE,"");
997  LOGDUMPPROBE(5,LA_PROBE,LF_PROBE,probe);
998  vdebugc(5,LA_PROBE,LF_PROBE,"(force=%d,onlyone=%d)\n",force,onlyone);
999 
1000  if (probe->sources)
1001  vdebug(5,LA_PROBE,LF_PROBE,"detaching probe %s from sources\n",probe->name);
1002 
1003  if (probe->sinks && !force) {
1004  verror("could not unregister a probe that had sinks remaining!\n");
1005  return -1;
1006  }
1007  else if (probe->sinks) {
1008  vwarn("forcefully unregistering a probe that had sinks remaining!\n");
1009  }
1010 
1011  /* Target must be paused (if it is attached!) before we do anything. */
1012  if (target_is_open(target)) {
1013  status = target_status(target);
1014  if (status != TSTATUS_PAUSED && status != TSTATUS_EXITING) {
1015  verror("target not paused (%d), cannot remove!\n",status);
1016  errno = EINVAL;
1017  return -1;
1018  }
1019  }
1020 
1021  /* Disable it (and its sources if necessary). */
1022  if (onlyone)
1023  probe_disable_one(probe);
1024  else
1025  probe_disable(probe);
1026 
1027  if (probepoint) {
1028  /* Remove the probe from the probepoint's list. */
1029  list_del(&probe->probe);
1030  probe->probepoint = NULL;
1031 
1032  /* Cancel (and possibly destroy) any actions it might have. */
1033  list_for_each_entry_safe(action,tmp,&probepoint->simple_actions,action) {
1034  action_cancel(action);
1035  RPUT(action,action,probepoint,trefcnt);
1036  }
1037  list_for_each_entry_safe(action,tmp,&probepoint->ss_actions,action) {
1038  action_cancel(action);
1039  RPUT(action,action,probepoint,trefcnt);
1040  }
1041  list_for_each_entry_safe(action,tmp,&probepoint->complex_actions,action) {
1042  action_cancel(action);
1043  RPUT(action,action,probepoint,trefcnt);
1044  }
1045  }
1046 
1047  /* If we are a source on somebody's sink list, remove ourselves! */
1048  if (probe->sinks) {
1049  list = probe->sinks;
1050  while (list) {
1051  ptmp = (struct probe *)list->data;
1052  ptmp->sources = g_list_remove(ptmp->sources,probe);
1053  list = g_list_next(list);
1054  }
1055  probe->sinks = NULL;
1056  }
1057 
1058  /* Unregister from any sources. */
1059  if (probe->sources) {
1060  list = probe->sources;
1061  while (list) {
1062  vdebug(5,LA_PROBE,LF_PROBE,"removing source\n");
1063  ptmp = (struct probe *)list->data;
1064  /* We MUST get the next ptr before calling the
1065  * probe_unregister_source* functions, because they will
1066  * remove our element!
1067  */
1068  list = g_list_next(list);
1069  /* Unregister from the sources, possibly recursively. */
1070  if (onlyone)
1071  probe_unregister_source_one(probe,ptmp,force);
1072  else
1073  probe_unregister_source(probe,ptmp,force);
1074  }
1075  g_list_free(probe->sources);
1076  probe->sources = NULL;
1077  vdebug(5,LA_PROBE,LF_PROBE,"probe sources removed\n");
1078  }
1079 
1080  if (probe->bsymbol) {
1081  bsymbol_release(probe->bsymbol);
1082  probe->bsymbol = NULL;
1083  }
1084 
1085  /* At this point, the probe is unregistered; what remains is to
1086  * remove its probepoint, if necessary, and we don't have to wait to
1087  * let the user know.
1088  */
1089  if (PROBE_SAFE_OP(probe,unregistered)) {
1090  verror("probe '%s': unregistered failed, aborting\n",probe->name);
1091  return -1;
1092  }
1093 
1094  /* Free the probe if it is an autofree probe. */
1095  if (probe->autofree)
1096  if (probe_free(probe,force))
1097  verror("could not autofree probe; continuing anyway!\n");
1098 
1099  /* If it's just a source/sink probe, we're done; otherwise, try to
1100  * remove the probepoint too if no one else is using it.
1101  */
1102  if (!probepoint)
1103  return 0;
1104 
1105  /* If this is the last probe at this probepoint, remove the
1106  * probepoint too -- IF possible, or IF forced!
1107  */
1108  if (!list_empty(&probepoint->probes))
1109  return 0;
1110 
1111  vdebug(5,LA_PROBE,LF_PROBE,"no more probes at ");
1112  LOGDUMPPROBEPOINT(5,LA_PROBE,LF_PROBE,probepoint);
1113  vdebugc(5,LA_PROBE,LF_PROBE,"; removing probepoint!\n");
1114 
1115  if (!__probepoint_remove(probepoint,force,0))
1116  probepoint_free(probepoint);
1117  else if (force) {
1118  verror("probepoint_remove failed, but force freeing!\n");
1119  target_remove_probepoint(target,probepoint->thread,probepoint);
1120  probepoint_free(probepoint);
1121  return -1;
1122  }
1123  else {
1124  verror("probepoint_remove failed; not force freeing!\n");
1125  return -1;
1126  }
1127 
1128  return 0;
1129 }
1130 
1131 /*
1132  * Unregisters a probe.
1133  * Upon successful completion, a value of 0 is returned. Otherwise, a value
1134  * of -1 is returned and the global integer variable errno is set to indicate
1135  * the error.
1136  */
1137 int probe_unregister(struct probe *probe,int force) {
1138  return __probe_unregister(probe,force,0);
1139 }
1140 
1141 int probe_unregister_one(struct probe *probe,int force) {
1142  return __probe_unregister(probe,force,1);
1143 }
1144 
1145 int probe_unregister_source(struct probe *sink,struct probe *src,int force) {
1146  target_status_t status;
1147  struct target *target = sink->target;
1148 
1149  if (!sink->sources) {
1150  verror("probe %s has no sources!\n",sink->name);
1151  return -1;
1152  }
1153 
1154  /* Target must be paused (if attached) before we do anything. */
1155  if (target_is_open(target)) {
1156  status = target_status(target);
1157  if (status != TSTATUS_PAUSED && status != TSTATUS_EXITING) {
1158  verror("target not paused (%d), cannot remove!\n",status);
1159  errno = EINVAL;
1160  return -1;
1161  }
1162  }
1163 
1164  sink->sources = g_list_remove(sink->sources,src);
1165  src->sinks = g_list_remove(src->sinks,sink);
1166 
1167  /* Do it recursively! */
1168  if (src->autofree && !src->sinks)
1169  __probe_unregister(src,force,0);
1170 
1171  if (PROBE_SAFE_OP(sink,unregistered)) {
1172  verror("probe %s: unregistered failed, aborting\n",sink->name);
1173  return -1;
1174  }
1175 
1176  /* NB: we cannot free the sink! The probe_free() function might
1177  * call probe_unregister_source*(), so we cannot free a sink we
1178  * might be freeing. probe_free() can only go downwards.
1179  */
1180  //if (sink->autofree)
1181  // return probe_free(sink,force);
1182 
1183  return 0;
1184 }
1185 
1186 int probe_unregister_source_one(struct probe *sink,struct probe *src,
1187  int force) {
1188  if (!sink->sources) {
1189  verror("probe %s has no sources!\n",sink->name);
1190  return -1;
1191  }
1192 
1193  sink->sources = g_list_remove(sink->sources,src);
1194  src->sinks = g_list_remove(src->sinks,sink);
1195 
1196  if (PROBE_SAFE_OP(sink,unregistered)) {
1197  verror("probe %s: unregistered failed, aborting\n",sink->name);
1198  return -1;
1199  }
1200 
1201  /* NB: we cannot free the sink! The probe_free() function might
1202  * call probe_unregister_source*(), so we cannot free a sink we
1203  * might be freeing. probe_free() can only go downwards.
1204  */
1205  //if (sink->autofree)
1206  // return probe_free(sink,force);
1207 
1208  return 0;
1209 }
1210 
1211 /*
1212  * This function always frees its probes, BUT the underlying probepoints
1213  * might fail to be freed. If so, we return the number of probepoints
1214  * that failed to free (note that if other probes are attached, those
1215  * probepoints are not freed and it's not an error).
1216  */
1217 int probe_unregister_batch(struct target *target,struct probe **probelist,
1218  int listlen,int force) {
1219  int i;
1220  int retval = 0;
1222 
1223  if (!probelist)
1224  return -1;
1225  if (!listlen)
1226  return 0;
1227 
1228  /* Target must be paused before we do anything. */
1229  status = target_status(target);
1230  if (status != TSTATUS_PAUSED && status != TSTATUS_EXITING) {
1231  verror("target not paused!\n");
1232  errno = EINVAL;
1233  return -1;
1234  }
1235 
1236  for (i = 0; i < listlen; ++i) {
1237  /* allow sparse lists */
1238  if (probelist[i] == NULL)
1239  continue;
1240 
1241  if (__probe_unregister(probelist[i],force,1)) {
1242  ++retval;
1243  }
1244  probelist[i] = NULL;
1245  }
1246 
1247  return retval;
1248 }
1249 
1250 struct probe *__probe_register_addr(struct probe *probe,ADDR addr,
1251  struct memrange *range,
1252  probepoint_type_t type,
1253  probepoint_style_t style,
1254  probepoint_whence_t whence,
1255  probepoint_watchsize_t watchsize,
1256  struct bsymbol *bsymbol,ADDR symbol_addr) {
1257  struct probepoint *probepoint;
1258  int created = 0;
1259  struct target *target = probe->target;
1261  loctype_t ltrc;
1262  struct location tloc;
1263  struct target_location_ctxt *tlctxt;
1264 
1265  if (type == PROBEPOINT_WATCH && style == PROBEPOINT_SW) {
1266  verror("software watchpoints are unsupported!\n");
1267  errno = EINVAL;
1268  goto errout;
1269  }
1270 
1271  /* Target must be paused before we do anything. */
1272  status = target_status(target);
1273  if (status != TSTATUS_PAUSED && status != TSTATUS_EXITING) {
1274  verror("target not paused (%d)!\n",status);
1275  errno = EINVAL;
1276  goto errout;
1277  }
1278 
1279  /* If the user has associated a bound symbol with this probe
1280  * registration, try to look up its start addr (and grab the range
1281  * if we can).
1282  */
1283  if (bsymbol) {
1284  memset(&tloc,0,sizeof(tloc));
1286  probe->thread->tid,
1287  bsymbol);
1288  ltrc = target_lsymbol_resolve_location(target,tlctxt,bsymbol->lsymbol,0,
1289  LOAD_FLAG_NONE,&tloc,NULL,
1290  (!range) ? &range : NULL);
1291  if (ltrc == LOCTYPE_ADDR)
1292  symbol_addr = LOCATION_ADDR(&tloc);
1293  target_location_ctxt_free(tlctxt);
1294  location_internal_free(&tloc);
1295  probe->bsymbol = bsymbol;
1296  RHOLD(bsymbol,probe);
1297  }
1298 
1299  /* If we don't have a range yet, get it. */
1300  if (!range) {
1301  target_find_memory_real(target,addr,NULL,NULL,&range);
1302  if (!range) {
1303  verror("could not find range for 0x%"PRIxADDR"\n",addr);
1304  goto errout;
1305  }
1306  }
1307 
1308  /* Create a probepoint if this is a new addr. */
1309  if ((probepoint = target_lookup_probepoint(target,probe->thread,addr))) {
1310  /* If the style matches for breakpoints, and if the style,
1311  * whence, and watchsize match for watchpoints, reuse it!
1312  */
1313  if (!((type == PROBEPOINT_BREAK
1314  && type == probepoint->type
1315  && ((probepoint->style == PROBEPOINT_HW
1316  && (style == PROBEPOINT_HW
1317  || style == PROBEPOINT_FASTEST))
1318  || (probepoint->style == PROBEPOINT_SW
1319  && (style == PROBEPOINT_SW
1320  || style == PROBEPOINT_FASTEST))))
1321  || (type == PROBEPOINT_WATCH
1322  && type == probepoint->type
1323  && style == probepoint->style
1324  && whence == probepoint->whence
1325  && watchsize == probepoint->watchsize))) {
1326  verror("addr 0x%"PRIxADDR" already has a probepoint with different properties!\n",addr);
1327  errno = EADDRINUSE;
1328  goto errout;
1329  }
1330  }
1331  else {
1332  if (type == PROBEPOINT_BREAK) {
1333  if (!(probepoint = probepoint_create_break(target,addr,range,style,
1334  bsymbol,symbol_addr))) {
1335  verror("could not create breakpoint for 0x%"PRIxADDR"\n",addr);
1336  goto errout;
1337  }
1338  }
1339  else {
1340  if (whence == PROBEPOINT_WAUTO)
1341  whence = PROBEPOINT_READWRITE;
1342 
1343  if (!(probepoint = probepoint_create_watch(target,addr,range,
1344  style,whence,watchsize,
1345  bsymbol,symbol_addr))) {
1346  verror("could not create watchpoint for 0x%"PRIxADDR"\n",addr);
1347  goto errout;
1348  }
1349  }
1350 
1351  created = 1;
1352  }
1353 
1354  /* Inject the probepoint. */
1355  if (__probepoint_insert(probepoint,probe->thread)) {
1356  verror("could not insert probepoint at 0x%"PRIxADDR"\n",addr);
1357  if (created)
1358  probepoint_free(probepoint);
1359  goto errout;
1360  }
1361 
1362  list_add_tail(&probe->probe,&probepoint->probes);
1363  probe->probepoint = probepoint;
1364 
1365  if (PROBE_SAFE_OP(probe,registered)) {
1366  verror("probe '%s': registered failed, aborting\n",probe->name);
1367  if (created)
1368  probepoint_free(probepoint);
1369  goto errout;
1370  }
1371 
1372  if (probe_enable(probe) && created) {
1373  probepoint_free(probepoint);
1374  goto errout;
1375  }
1376 
1377  vdebug(5,LA_PROBE,LF_PROBE,"probe %s attached to ",probe->name);
1379  vdebugc(5,LA_PROBE,LF_PROBE,"\n");
1380 
1381  return probe;
1382 
1383  errout:
1384  if (bsymbol) {
1385  bsymbol_release(bsymbol);
1386  probe->bsymbol = NULL;
1387  }
1388  if (probe->autofree)
1389  probe_free(probe,1);
1390  return NULL;
1391 }
1392 
1393 struct probe *probe_register_addr(struct probe *probe,ADDR addr,
1394  probepoint_type_t type,
1395  probepoint_style_t style,
1396  probepoint_whence_t whence,
1397  probepoint_watchsize_t watchsize,
1398  struct bsymbol *bsymbol) {
1399  return __probe_register_addr(probe,addr,NULL,type,style,whence,watchsize,
1400  bsymbol,0);
1401 }
1402 
1403 struct probe *probe_register_line(struct probe *probe,char *filename,int line,
1404  probepoint_style_t style,
1405  probepoint_whence_t whence,
1406  probepoint_watchsize_t watchsize) {
1407  struct target *target = probe->target;
1408  struct memrange *range = NULL;
1409  ADDR start = 0;
1410  ADDR alt_start = 0;
1411  ADDR probeaddr;
1412  struct bsymbol *bsymbol = NULL;
1413  struct symbol *symbol;
1414  struct target_location_ctxt *tlctxt;
1415 
1416  bsymbol = target_lookup_sym_line(target,filename,line,NULL,&probeaddr);
1417  if (!bsymbol)
1418  return NULL;
1419  symbol = lsymbol_last_symbol(bsymbol->lsymbol);
1420 
1421  /* No need to RHOLD(); __probe_register_addr() does it.
1422  * IN FACT, we need to release when we exit!
1423  */
1424 
1425  if (!SYMBOL_IS_FULL(symbol)) {
1426  verror("cannot probe a partial symbol!\n");
1427  goto errout;
1428  }
1429 
1430  if (SYMBOL_IS_FUNC(symbol) || SYMBOL_IS_LABEL(symbol)
1431  || SYMBOL_IS_BLOCK(symbol)) {
1433  probe->thread->tid,
1434  bsymbol);
1435  if (target_lsymbol_resolve_bounds(target,tlctxt,bsymbol->lsymbol,0,
1436  &start,NULL,NULL,&alt_start,NULL)) {
1437  vwarn("could not resolve base addr for symbol %s!\n",
1438  lsymbol_get_name(bsymbol->lsymbol));
1439  }
1440  target_location_ctxt_free(tlctxt);
1441 
1442  target_find_memory_real(target,probeaddr,NULL,NULL,&range);
1443  if (!range) {
1444  verror("could not find range for probeaddr 0x%"PRIxADDR"\n",
1445  probeaddr);
1446  goto errout;
1447  }
1448 
1449  probe = __probe_register_addr(probe,probeaddr,range,
1450  PROBEPOINT_BREAK,style,whence,watchsize,
1451  bsymbol,start);
1452  }
1453  else {
1454  verror("unknown symbol type '%s'!\n",
1455  SYMBOL_TYPE(bsymbol->lsymbol->symbol->type));
1456  goto errout;
1457  }
1458 
1459  bsymbol_release(bsymbol);
1460  return probe;
1461 
1462  errout:
1463  if (probe->autofree)
1464  probe_free(probe,1);
1465  if (bsymbol)
1466  bsymbol_release(bsymbol);
1467  return NULL;
1468 }
1469 
1470 struct probe *probe_register_symbol(struct probe *probe,struct bsymbol *bsymbol,
1471  probepoint_style_t style,
1472  probepoint_whence_t whence,
1473  probepoint_watchsize_t watchsize) {
1474  struct target *target = probe->target;
1475  struct memrange *range = NULL;
1476  ADDR start = 0;
1477  ADDR alt_start = 0;
1478  ADDR probeaddr = 0;
1479  unsigned int ssize;
1480  struct symbol *symbol;
1481  loctype_t ltrc;
1482  struct location tloc;
1483  struct target_location_ctxt *tlctxt = NULL;
1484  int rc;
1485 
1486  symbol = lsymbol_last_symbol(bsymbol->lsymbol);
1487 
1488  if (!SYMBOL_IS_FULL(symbol)) {
1489  verror("cannot probe a partial symbol!\n");
1490  goto errout;
1491  }
1492 
1493  /* No need to RHOLD(); __probe_register_addr() does it. */
1494 
1495  if (target->ops->probe_register_symbol) {
1496  rc = target->ops->probe_register_symbol(target,probe->thread->tid,
1497  probe,bsymbol,
1498  style,whence,watchsize);
1499  if (rc) {
1500  verror("could not register probe for symbol '%s' breakpoint!\n",
1501  bsymbol_get_name(bsymbol));
1502  goto errout;
1503  }
1504  }
1505  else if (SYMBOL_IS_FUNC(symbol) || SYMBOL_IS_LABEL(symbol)
1506  || SYMBOL_IS_BLOCK(symbol)) {
1508  probe->thread->tid,
1509  bsymbol);
1510  if (target_lsymbol_resolve_bounds(target,tlctxt,bsymbol->lsymbol,0,
1511  &start,NULL,NULL,&alt_start,NULL)) {
1512  verror("could not resolve base addr for symbol %s!\n",
1513  lsymbol_get_name(bsymbol->lsymbol));
1514  goto errout;
1515  }
1516  else if (alt_start)
1517  probeaddr = alt_start;
1518  else
1519  probeaddr = start;
1520 
1521  target_location_ctxt_free(tlctxt);
1522  tlctxt = NULL;
1523 
1524  if (!target_find_memory_real(target,probeaddr,NULL,NULL,&range)) {
1525  verror("could not find addr 0x%"PRIxADDR"!\n",probeaddr);
1526  goto errout;
1527  }
1528 
1529  probe = __probe_register_addr(probe,probeaddr,range,
1530  PROBEPOINT_BREAK,style,whence,watchsize,
1531  bsymbol,start);
1532  }
1533  else if (SYMBOL_IS_VAR(bsymbol->lsymbol->symbol)) {
1534  if (watchsize == PROBEPOINT_LAUTO) {
1536  if (ssize <= 0) {
1537  verror("bad size (%d) for type of %s!\n",
1538  ssize,bsymbol->lsymbol->symbol->name);
1539  goto errout;
1540  }
1541 
1542  watchsize = probepoint_closest_watchsize(ssize);
1543  }
1544 
1545  memset(&tloc,0,sizeof(tloc));
1547  probe->thread->tid,
1548  bsymbol);
1549  ltrc = target_lsymbol_resolve_location(target,tlctxt,bsymbol->lsymbol,0,
1550  LOAD_FLAG_NONE,&tloc,NULL,&range);
1551  if (ltrc == LOCTYPE_ADDR) {
1552  probeaddr = LOCATION_ADDR(&tloc);
1553  location_internal_free(&tloc);
1554  }
1555  else {
1556  verror("could not resolve base addr for var %s (loctype %s)!\n",
1557  bsymbol->lsymbol->symbol->name,LOCTYPE(ltrc));
1558  location_internal_free(&tloc);
1559  goto errout;
1560  }
1561 
1562  target_location_ctxt_free(tlctxt);
1563  tlctxt = NULL;
1564 
1565  if (!target_find_memory_real(target,probeaddr,NULL,NULL,&range)) {
1566  verror("could not find addr 0x%"PRIxADDR"!\n",probeaddr);
1567  goto errout;
1568  }
1569 
1570  probe = __probe_register_addr(probe,probeaddr,range,
1571  PROBEPOINT_WATCH,style,whence,watchsize,
1572  bsymbol,probeaddr);
1573  }
1574  else {
1575  verror("unknown symbol type '%s'!\n",
1576  SYMBOL_TYPE(bsymbol->lsymbol->symbol->type));
1577  goto errout;
1578  }
1579 
1580  vdebug(5,LA_PROBE,LF_PROBE,"registered probe on %s at 0x%"PRIxADDR"\n",
1581  bsymbol_get_name(bsymbol),probeaddr);
1582 
1583  return probe;
1584 
1585  errout:
1586  if (tlctxt)
1587  target_location_ctxt_free(tlctxt);
1588  if (probe->autofree)
1589  probe_free(probe,1);
1590  return NULL;
1591 }
1592 
1593 struct probe *probe_register_source(struct probe *sink,struct probe *src) {
1594  struct target *target = src->target;
1595  int held_src_bsymbol = 0;
1596  REFCNT trefcnt;
1598 
1599  /* XXX: should we do this? Steal the src's bsymbol if we don't have
1600  * one!
1601  */
1602  if (!sink->bsymbol && src->bsymbol) {
1603  sink->bsymbol = src->bsymbol;
1604  RHOLD(src->bsymbol,sink);
1605  held_src_bsymbol = 1;
1606  }
1607 
1608  /*
1609  * Don't do this for now; allow this so we can build overlay probe
1610  * hierarchies easily.
1611  */
1612  /*
1613  if (sink->target != src->target) {
1614  verror("sink %s/src %s targets different!\n",sink->name,src->name);
1615  goto errout;
1616  }
1617  */
1618 
1619  /* Target must be paused before we do anything. */
1620  status = target_status(target);
1621  if (target_status(target) != TSTATUS_PAUSED && status != TSTATUS_EXITING) {
1622  verror("target not paused!\n");
1623  errno = EINVAL;
1624  goto errout;
1625  }
1626 
1627  /* Add this sink to the src, and vice versa! */
1628  sink->sources = g_list_prepend(sink->sources,src);
1629  src->sinks = g_list_prepend(src->sinks,sink);
1630 
1631  if (PROBE_SAFE_OP(sink,registered)) {
1632  verror("probe '%s': registered failed, aborting\n",sink->name);
1633  goto errout;
1634  }
1635 
1636  /* Enable everybody downstream. */
1637  if (probe_enable(sink)) {
1638  verror("failed to enable sink probe '%s', aborting\n",sink->name);
1639  goto errout;
1640  }
1641 
1642  return sink;
1643 
1644  errout:
1645  if (held_src_bsymbol) {
1646  RPUT(sink->bsymbol,bsymbol,sink,trefcnt);
1647  sink->bsymbol = NULL;
1648  }
1649  if (sink->autofree)
1650  probe_free(sink,1);
1651  return NULL;
1652 }
1653 
1654 struct probe *probe_register_sources(struct probe *sink,struct probe *src,...) {
1655  va_list ap;
1656  struct probe *rc;
1657  struct probe *tsrc;
1658 
1659  va_start(ap,src);
1660  while ((tsrc = va_arg(ap,struct probe *))) {
1661  if (tsrc->target != sink->target) {
1662  verror("sink %s and src %s targets differ!\n",sink->name,src->name);
1663  return NULL;
1664  }
1665  }
1666  va_end(ap);
1667 
1668  if (!(rc = probe_register_source(sink,src)))
1669  return rc;
1670 
1671  va_start(ap,src);
1672  while ((tsrc = va_arg(ap,struct probe *))) {
1673  if (!(rc = probe_register_source(sink,tsrc)))
1674  return rc;
1675  /*
1676  * XXX: need to unwind registrations, too, in case of failure,
1677  * just like for probe batch registration!
1678  */
1679  }
1680  va_end(ap);
1681 
1682  return sink;
1683 }
1684 
1685 int probe_register_batch(struct target *target,tid_t tid,
1686  ADDR *addrlist,int listlen,
1688  probepoint_whence_t whence,
1689  probepoint_watchsize_t watchsize,
1692  void *handler_data,
1693  struct probe **probelist,
1694  int failureaction) {
1695  int i;
1696  int retval = 0;
1697  struct probe *probe;
1698  char *buf;
1699  target_status_t status;
1700 
1701  if (!probelist)
1702  return -1;
1703  if (!listlen)
1704  return 0;
1705 
1706  if (type == PROBEPOINT_WATCH && style == PROBEPOINT_SW) {
1707  verror("software watchpoints are unsupported!\n");
1708  errno = EINVAL;
1709  return -1;
1710  }
1711 
1712  /* Target must be paused before we do anything. */
1713  status = target_status(target);
1714  if (target_status(target) != TSTATUS_PAUSED && status != TSTATUS_EXITING) {
1715  verror("target not paused!\n");
1716  errno = EINVAL;
1717  return -1;
1718  }
1719 
1720  for (i = 0; i < listlen; ++i) {
1721  /* allow sparse lists */
1722  if (addrlist[i] == 0)
1723  continue;
1724 
1725  buf = malloc(5+1+16+1);
1726  sprintf(buf,"probe@%"PRIxADDR,addrlist[i]);
1727  probe = probe_create(target,tid,NULL,buf,pre_handler,post_handler,
1728  handler_data,0,1);
1729 
1730  if (!(probelist[i] = probe_register_addr(probe,addrlist[i],
1731  type,style,whence,watchsize,
1732  NULL))) {
1733  if (failureaction == 2) {
1734  ++retval;
1735  continue;
1736  }
1737  else if (failureaction == 1) {
1738  ++retval;
1739  goto errunreg;
1740  }
1741  else {
1742  retval = 1;
1743  goto out;
1744  }
1745  }
1746  }
1747  goto out;
1748 
1749  errunreg:
1750  for (i = 0; i < listlen; ++i) {
1751  if (addrlist[i] == 0)
1752  continue;
1753 
1754  if (probelist[i] == NULL)
1755  continue;
1756 
1757  /* Don't force this unregister. If there are dangling
1758  * probepoints, they'll have to get harvested later. This
1759  * totally should not happen here though -- the batch
1760  * registration is like a transaction.
1761  */
1762  __probe_unregister(probelist[i],0,1);
1763  }
1764 
1765  out:
1766  return retval;
1767 }
1768 
1770  if (size <= 1)
1771  return PROBEPOINT_L0;
1772  else if (size <= 2)
1773  return PROBEPOINT_L2;
1774  else {
1775 #if __WORDSIZE == 64
1776  if (size <= 4)
1777  return PROBEPOINT_L4;
1778  else
1779  return PROBEPOINT_L8;
1780 #else
1781  return PROBEPOINT_L4;
1782 #endif
1783  }
1784 }
1785 
1786 void *probe_summarize(struct probe *probe) {
1787  if (!probe->ops || !probe->ops->summarize)
1788  return NULL;
1789 
1790  return probe->ops->summarize(probe);
1791 }
1792 
1793 void *probe_summarize_tid(struct probe *probe,tid_t tid) {
1794  if (!probe->ops || !probe->ops->summarize_tid)
1795  return NULL;
1796 
1797  return probe->ops->summarize_tid(probe,tid);
1798 }
1799 
1800 int probe_disable_one(struct probe *probe) {
1801  probe->enabled = 0;
1802 
1803  if (PROBE_SAFE_OP(probe,disabled)) {
1804  verror("probe '%s': disabled failed, ignoring\n",probe->name);
1805  return -1;
1806  }
1807 
1808  return 0;
1809 }
1810 
1811 int probe_disable(struct probe *probe) {
1812  struct probe *ptmp;
1813  GList *list;
1814  GList *list2;
1815  int anyenabled = 0;
1816  int retval = 0;
1817 
1818  if (probe_disable_one(probe))
1819  return -1;
1820 
1821  if (probe->sources) {
1822  list = probe->sources;
1823  while (list) {
1824  ptmp = (struct probe *)list->data;
1825 
1826  /* Disable recursively *if* the source doesn't have any
1827  * enabled sinks.
1828  */
1829  if (probe_enabled(ptmp)) {
1830  list2 = ptmp->sinks;
1831  while (list2) {
1832  if (((struct probe *)(list->data))->enabled) {
1833  anyenabled = 1;
1834  break;
1835  }
1836  list2 = g_list_next(list2);
1837  }
1838  if (!anyenabled)
1839  retval |= probe_disable(ptmp);
1840  anyenabled = 0;
1841  }
1842  list = g_list_next(list);
1843  }
1844  }
1845 
1846  return retval;
1847 }
1848 
1849 int probe_enable_one(struct probe *probe) {
1850  probe->enabled = 1;
1851 
1852  if (PROBE_SAFE_OP(probe,enabled)) {
1853  verror("probe '%s': enabled failed, disabling!\n",probe->name);
1854  probe->enabled = 0;
1855  return -1;
1856  }
1857 
1858  return 0;
1859 }
1860 
1861 int probe_enable(struct probe *probe) {
1862  struct probe *ptmp;
1863  GList *list;
1864  GList *list2;
1865  int anyenabled = 0;
1866 
1867  if (probe->sources) {
1868  list = probe->sources;
1869  while (list) {
1870  ptmp = (struct probe *)list->data;
1871  if (!probe_enabled(ptmp))
1872  probe_enable(ptmp);
1873  list = g_list_next(list);
1874  }
1875  }
1876 
1877  probe->enabled = 1;
1878 
1879  if (PROBE_SAFE_OP(probe,enabled)) {
1880  verror("probe '%s': enabled failed, disabling\n",probe->name);
1881  probe->enabled = 0;
1882  if (probe->sources) {
1883  list = probe->sources;
1884  while (list) {
1885  ptmp = (struct probe *)list->data;
1886 
1887  /* basically do probe_disable, but don't notify probe! */
1888  if (probe_enabled(ptmp)) {
1889  list2 = ptmp->sinks;
1890  while (list2) {
1891  if (((struct probe *)(list->data))->enabled) {
1892  anyenabled = 1;
1893  break;
1894  }
1895  list2 = g_list_next(list2);
1896  }
1897  if (!anyenabled)
1898  probe_disable(ptmp);
1899  anyenabled = 0;
1900  }
1901  list = g_list_next(list);
1902  }
1903  }
1904  return -1;
1905  }
1906 
1907  return 0;
1908 }
1909 
1910 /*
1911  * Indicates whether a probe is enabled or not.
1912  * Returns a non-zero value if the probe is active, a value of 0 if the
1913  * probe is inactive, or a value of -1 if the given handle is invalid.
1914  */
1915 int probe_enabled(struct probe *probe) {
1916  return probe->enabled;
1917 }
1918 
1919 int probe_is_base(struct probe *probe) {
1920  return probe->probepoint != NULL;
1921 }
1922 
1923 int probe_num_sources(struct probe *probe) {
1924  if (probe->sources)
1925  return g_list_length(probe->sources);
1926  return 0;
1927 }
1928 
1929 int probe_num_sinks(struct probe *probe) {
1930  if (probe->sinks)
1931  return g_list_length(probe->sinks);
1932  return 0;
1933 }
1934 
1935 char *probe_name(struct probe *probe) {
1936  return probe->name;
1937 }
1938 
1939 struct bsymbol *probe_symbol(struct probe *probe) {
1940  return probe->bsymbol;
1941 }
1942 
1943 struct target*probe_target(struct probe *probe) {
1944  return probe->target;
1945 }
1946 
1947 tid_t probe_tid(struct probe *probe) {
1948  return probe->thread->tid;
1949 }
1950 
1951 void *probe_priv(struct probe *probe) {
1952  return probe->priv;
1953 }
1954 
1955 /*
1956  * Returns the address the a probe is targeting, or 0 if the probe is a
1957  * sink without a probepoint.
1958  */
1959 ADDR probe_addr(struct probe *probe) {
1960  if (probe->probepoint)
1961  return probe->probepoint->addr;
1962  return 0;
1963 }
1964 
1965 /*
1966  * Returns the type of the probe.
1967  */
1968 probepoint_type_t probe_type(struct probe *probe) {
1969  return probe->probepoint->type;
1970 }
1971 
1972 /*
1973  * Returns the style of the probe.
1974  */
1975 probepoint_style_t probe_style(struct probe *probe) {
1976  return probe->probepoint->style;
1977 }
1978 
1979 /*
1980  * Returns the whence of the probe.
1981  */
1982 probepoint_whence_t probe_whence(struct probe *probe) {
1983  return probe->probepoint->whence;
1984 }
1985 
1986 
1987 static int run_post_handlers(struct target *target,
1988  struct target_thread *tthread,
1989  struct probepoint *probepoint);
1990 static int setup_post_single_step(struct target *target,
1991  struct target_thread *tthread,
1992  struct probepoint *probepoint);
1993 static int handle_simple_actions(struct target *target,
1994  struct target_thread *tthread,
1995  struct probepoint *probepoint);
1996 static int handle_complex_actions(struct target *target,
1997  struct target_thread *tthread,
1998  struct probepoint *probepoint);
1999 
2000 /*
2001  * Runs and cleans up any simple actions that should happen at this
2002  * probepoint. We run all simple actions after the probe prehandlers
2003  * have been run, but before the singlestep of the probepoint happens.
2004  */
2005 static int handle_simple_actions(struct target *target,
2006  struct target_thread *tthread,
2007  struct probepoint *probepoint) {
2008  struct action *action;
2009  struct action *taction;
2010  int retval = 0;
2011  int rc;
2012  unsigned long rcb;
2013 
2014  list_for_each_entry_safe(action,taction,&probepoint->simple_actions,action) {
2015  rc = 0;
2016 
2017  if (!probe_enabled(action->probe)) {
2018  vdebug(3,LA_PROBE,LF_ACTION,"skipping disabled probe at ");
2019  LOGDUMPPROBEPOINT(3,LA_PROBE,LF_ACTION,probepoint);
2020  vdebugc(3,LA_PROBE,LF_ACTION,"\n");
2021  continue;
2022  }
2023 
2024  if (action->type == ACTION_REGMOD) {
2025  rc = target_write_reg(tthread->target,tthread->tid,
2026  action->detail.regmod.regnum,
2027  action->detail.regmod.regval);
2028  if (rc)
2029  vwarn("could not write reg %"PRIiREG"!\n",
2030  action->detail.regmod.regnum);
2031  else
2032  vdebug(4,LA_PROBE,LF_ACTION,"wrote 0x%"PRIxREGVAL" to %"PRIiREG"\n",
2033  action->detail.regmod.regval,action->detail.regmod.regnum);
2034  }
2035  else if (action->type == ACTION_MEMMOD) {
2036  rcb = target_write_addr(tthread->target,
2037  action->detail.memmod.destaddr,
2038  action->detail.memmod.len,
2039  (unsigned char *)action->detail.memmod.data);
2040  if (rcb != action->detail.memmod.len) {
2041  vwarn("could not write %d bytes to 0x%"PRIxADDR"!\n",
2042  action->detail.memmod.len,action->detail.memmod.destaddr);
2043  rc = 1;
2044  }
2045  else
2046  vdebug(4,LA_PROBE,LF_ACTION,"wrote %d bytes to 0x%"PRIxADDR"\n",
2047  action->detail.memmod.len,action->detail.memmod.destaddr);
2048  }
2049  else {
2050  verror("BUG: unsupported action type %d -- not doing it!\n",
2051  action->type);
2052  rc = 1;
2053  }
2054 
2055  if (rc) {
2056  ++retval;
2057  if (action->handler)
2058  action->handler(action,tthread,action->probe,probepoint,
2059  MSG_FAILURE,0,action->handler_data);
2060  }
2061  else if (action->handler)
2062  action->handler(action,tthread,action->probe,probepoint,
2063  MSG_SUCCESS,0,action->handler_data);
2064 
2065 
2066  /* cleanup oneshot actions! */
2067  action_finish_handling(action,NULL);
2068  }
2069 
2070  return retval;
2071 }
2072 /*
2073  * Returns: <0 on error; 0 if not stepping; 1 if stepping.
2074  */
2075 static int setup_single_step_actions(struct target *target,
2076  struct target_thread *tthread,
2077  struct probepoint *probepoint,
2078  int isbp,int stepping) {
2079  tid_t tid = tthread->tid;
2080  struct action *action, *taction;
2081 
2082  /*
2083  * If we need to do any single step actions, set that up if
2084  * handle_complex_actions didn't already. This should be pretty
2085  * rare, but if some injected code doesn't need single stepping
2086  * because it "notifies" when it's done via breakpoint, we can
2087  * save a lot of debug interrupts.
2088  */
2089  if (list_empty(&probepoint->ss_actions))
2090  return 0;
2091 
2092  /*
2093  * If the action was not boosted (i.e. is running at the
2094  * probepoint), and we're not already single stepping, we need
2095  * to tell the single step routine that we're stepping at a
2096  * breakpoint location.
2097  */
2098  if (!stepping) {
2099  vdebug(4,LA_PROBE,LF_PROBEPOINT,"setting up single step actions for ");
2101  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2102 
2103  if (probepoint->style == PROBEPOINT_HW
2104  && isbp
2105  && !target->nodisablehwbponss) {
2106  /*
2107  * We need to disable the hw breakpoint before we single
2108  * step because the target can't do it.
2109  */
2110  target_disable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
2111  probepoint->debugregdisabled = 1;
2112  }
2113 
2114  if (target_singlestep(target,tid,isbp) < 0) {
2115  verror("could not keep single stepping target!\n");
2116 
2117  if (probepoint->style == PROBEPOINT_HW
2118  && isbp
2119  && !target->nodisablehwbponss) {
2120  /*
2121  * Reenable the hw breakpoint we just disabled.
2122  */
2123  target_enable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
2124  probepoint->debugregdisabled = 0;
2125  }
2126 
2127  if (target_singlestep_end(target,tid))
2128  verror("could not stop single stepping target"
2129  " after failed sstep!\n");
2130 
2131  /*
2132  * All we can really do is notify all the single step
2133  * actions that we had an error, and nuke them, and keep
2134  * going.
2135  */
2136  list_for_each_entry_safe(action,taction,&probepoint->ss_actions,
2137  action) {
2138  if (action->handler)
2139  action->handler(action,tthread,action->probe,probepoint,
2140  MSG_FAILURE,0,action->handler_data);
2141 
2142  action_finish_handling(action,NULL);
2143  }
2144 
2145  stepping = -1;
2146  }
2147  else {
2148  vdebug(4,LA_PROBE,LF_PROBEPOINT,"sstep command succeeded for ");
2150  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2151 
2152  stepping = 1;
2153  }
2154  }
2155  else {
2157  "already stepping; building tacs for single step actions for ");
2159  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2160  }
2161 
2162  /*
2163  * If single step init succeeded, let the ss handler take over.
2164  * Also, take a reference to all the ss_actions actions and
2165  * place contexts for them on our thread's list.
2166  */
2167  if (stepping == 1) {
2168  list_for_each_entry_safe(action,taction,&probepoint->ss_actions,action) {
2169  struct thread_action_context *tac = \
2170  (struct thread_action_context *)calloc(1,sizeof(*tac));
2171  tac->action = action;
2172  RHOLD(action,tac);
2173  tac->stepped = 0;
2174  INIT_LIST_HEAD(&tac->tac);
2175 
2176  list_add_tail(&tac->tac,&tthread->ss_actions);
2177  }
2178  }
2179 
2180  return stepping;
2181 }
2182 
2183 static int run_post_handlers(struct target *target,
2184  struct target_thread *tthread,
2185  struct probepoint *probepoint) {
2186  int rc;
2187  int noreinject = 0;
2188  struct probe *probe;
2189  int i = 0;
2190 
2191  /*
2192  * Run post-handlers
2193  */
2194  list_for_each_entry(probe,&probepoint->probes,probe) {
2195  ++i;
2196  PROBE_SAFE_OP_ARGS(probe,values_notify_phase,tthread->tid,
2198  if (probe->enabled) {
2199  if (probe->post_handler) {
2200  vdebug(4,LA_PROBE,LF_PROBEPOINT,"running post handler at ");
2202  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2203 
2204  rc = probe->post_handler(probe,tthread->tid,probe->handler_data,probe,probe);
2205  if (rc == RESULT_ERROR)
2206  probe_disable(probe);
2207  else if (rc == RESULT_ABORT)
2208  /* don't reinject the probe! */
2209  noreinject = 1;
2210  }
2211  else if (0 && probe->sinks) {
2213  "running default probe sink post_handler at ");
2215  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2216 
2217  probe_do_sink_post_handlers(probe,tthread->tid,NULL,probe,probe);
2218  }
2219  }
2220 
2221  PROBE_SAFE_OP_ARGS(probe,values_notify_phase,tthread->tid,PHASE_POST_END);
2222  }
2223 
2224  if (i > 1 && noreinject) {
2225  vwarn("cannot skip reinjection of breakpoint because multiple probes present!\n");
2226  noreinject = 0;
2227  }
2228 
2229  return noreinject;
2230 }
2231 
2232 void probepoint_release(struct target *target,struct target_thread *tthread,
2233  struct probepoint *probepoint) {
2234  /* Only release if we owned it! */
2235  if (probepoint->tpc == tthread->tpc)
2236  probepoint->tpc = NULL;
2237 }
2238 
2239 struct thread_probepoint_context *probepoint_hold(struct target *target,
2240  struct target_thread *tthread,
2241  struct probepoint *probepoint,
2242  struct thread_probepoint_context *tpc) {
2243  if (probepoint->style == PROBEPOINT_SW
2244  && probepoint->tpc != NULL
2245  && probepoint->tpc->thread != tthread)
2246  return probepoint->tpc;
2247 
2248  probepoint->tpc = tpc;
2249  return tpc;
2250 }
2251 
2252 int probepoint_pause_handling(struct target *target,
2253  struct target_thread *tthread,
2254  struct probepoint *probepoint,
2255  thread_resumeat_t resumeat) {
2256  if (target->threadctl) {
2258  "pausing thread %"PRIiTID" (%d) until thread %"PRIiTID" finishes"
2259  " handling ",tthread->tid,resumeat,probepoint->tpc->thread->tid);
2261 
2262  tthread->resumeat = resumeat;
2263  return 0;
2264  }
2265 
2266  return -1;
2267 }
2268 
2269 static struct thread_probepoint_context *tpc_new(struct target_thread *tthread,
2270  struct probepoint *probepoint) {
2271  struct thread_probepoint_context *tpc = (struct thread_probepoint_context *) \
2272  calloc(1,sizeof(*tpc));
2273  tpc->thread = tthread;
2274  tpc->probepoint = probepoint;
2275 
2276  return tpc;
2277 }
2278 
2280  free(tpc);
2281 }
2282 
2283 /*
2284  * We will not run a single step of the real instruction in two cases.
2285  * First, if any complex action obviated the real instruction, we skip
2286  * the real instruction, and do nothing to EIP. We just run
2287  * post-handlers, if there are any.
2288  * Second, if there are no post-handlers
2289  *
2290  * Return <0 on error; 0 if we skipped a sstep because the action
2291  * obviated it; 1 if we setup a single step; 2 if we "paused" the thread
2292  * because we couldn't hold the probepoint to do the single step.
2293  */
2294 static int setup_post_single_step(struct target *target,
2295  struct target_thread *tthread,
2296  struct probepoint *probepoint) {
2297  int doit = 0;
2298  struct probe *probe;
2299  tid_t tid = tthread->tid;
2300  struct thread_probepoint_context *tpc = tthread->tpc;
2301  int noreinject;
2302  struct thread_probepoint_context *htpc;
2303 
2304  /*
2305  * We're now handling post stuff at the breakpoint.
2306  *
2307  * But, note that we don't change state to POSTHANDLING until we
2308  * know we're going to single step the real instruction. If we
2309  * don't do that, we don't change -- and we only adjust the state of
2310  * the probepoint if we own the probepoint. Otherwise we have to
2311  * pause this thread until we do own it.
2312  */
2313 
2314  if (tpc->action_obviated_orig) {
2315  vdebug(4,LA_PROBE,LF_PROBEPOINT,"skipping sstep due to action obviation of ");
2317 
2318  /* Just run the posthandlers; don't single step the orig. */
2319  noreinject = run_post_handlers(target,tthread,probepoint);
2320 
2321  if (!noreinject) {
2322  /*
2323  * If the action was boosted, we have nothing to replace.
2324  * If it wasn't, when we removed it, we put the BP back in.
2325  * So don't do anything here, because if the action was
2326  * boosted, some other thread might hold the probepoint!
2327  *
2328  * But, if we had disabled a hw breakpoint, reenable it!
2329  */
2330  if (probepoint->style == PROBEPOINT_HW
2331  && probepoint->debugregdisabled) {
2332  /*
2333  * Enable hardware bp here!
2334  */
2335  target_enable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
2336  probepoint->debugregdisabled = 0;
2337 
2338  probepoint->state = PROBE_BP_SET;
2339  }
2340  else if (probepoint->style == PROBEPOINT_SW) {
2341  target_enable_sw_breakpoint(target,probepoint->thread->tid,
2342  probepoint->mmod);
2343 
2344  probepoint->state = PROBE_BP_SET;
2345  }
2346  }
2347  else {
2348  __probepoint_remove(probepoint,0,0);
2349  }
2350 
2351  /*
2352  * Only release our hold on the probepoint if we actually held
2353  * it; if we had been running a boosted action, we might not
2354  * hold the probepoint.
2355  */
2356  probepoint_release(target,tthread,probepoint);
2357 
2358  return 0;
2359  }
2360 
2361  /*
2362  * Now that we've handled the case where an action might have
2363  * replaced the real instruction, we have to figure out if we should
2364  * single step the original instruction or not.
2365  *
2366  * If we're hardware and there are no enabled probes with
2367  * post-handlers, single step; otherwise, just proceed. If we're
2368  * software, *always* single step after replacing the original
2369  * instruction.
2370  */
2371  if (probepoint->style == PROBEPOINT_HW) {
2372  list_for_each_entry(probe,&probepoint->probes,probe) {
2373  if (probe->enabled && (probe->post_handler || probe->sinks)) {
2374  doit = 1;
2375  break;
2376  }
2377  }
2378 
2379  /*
2380  * XXX: for now, always force single step even for HW
2381  * breakpoints, since the Xen VM backend will get stuck in
2382  * an infinite loop at the breakpoint location.
2383  *
2384  * For whatever reason, this does not happen for the Linux
2385  * userspace process target. Until we know why, just always
2386  * single step here.
2387  */
2388  doit = 1;
2389 
2390  if (!doit) {
2391  probepoint->state = PROBE_BP_SET;
2392 
2393  vdebug(4,LA_PROBE,LF_PROBEPOINT,"skipping sstep for HW ");
2395  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"; no post handlers\n");
2396 
2397  /*
2398  * Don't run posthandlers because we haven't done the
2399  * breakpointed instruction! Well, this is not quite true
2400  * for watchpoints, but...
2401  */
2402  }
2403  else {
2404  /* We already own the probepoint because it's hardware. */
2405  probepoint->state = PROBE_BP_POSTHANDLING;
2406  }
2407  }
2408  /*
2409  * If we're software, replace the original.
2410  */
2411  else if (probepoint->style == PROBEPOINT_SW) {
2412  if (!target->no_adjust_bp_ip) {
2413  /* Restore the original instruction. */
2414  vdebug(4,LA_PROBE,LF_PROBEPOINT,"restoring orig instr for SW ");
2416  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2417  }
2418 
2419  doit = 1;
2420 
2421  /*
2422  * Grab hold of the probepoint, since we might have run boosted
2423  * instructions in the past. If we can't grab the probepoint,
2424  * we have to pause this thread until we can grab it.
2425  */
2426  if ((htpc = probepoint_hold(target,tthread,probepoint,tpc)) != tpc) {
2427  if (probepoint_pause_handling(target,tthread,probepoint,
2429  verror("thread %"PRIiTID" hit ss when thread %"PRIiTID" already"
2430  " handling it; target failed to ctl thread!\n",
2431  tthread->tid,htpc->thread->tid);
2432  /*
2433  * There literally is nothing we can do -- we cannot
2434  * handle this breakpoint interrupt.
2435  */
2436  return -1;
2437  }
2438  else
2439  return 2;
2440  }
2441  else {
2442  /*
2443  * If we're in BPMODE_STRICT, we have to pause all the other
2444  * threads.
2445  */
2446  if (target->spec->bpmode == THREAD_BPMODE_STRICT) {
2447  if (target_pause(target)) {
2448  vwarn("could not pause the target for blocking thread"
2449  " %"PRIiTID"!\n",tthread->tid);
2450  return -1;
2451  }
2452  target->blocking_thread = tthread;
2453  }
2454 
2455  if (target_disable_sw_breakpoint(target,probepoint->thread->tid,
2456  probepoint->mmod)) {
2457  verror("could not disable breakpoint before singlestep;"
2458  " assuming breakpoint is left in place and"
2459  " skipping single step, but badness will ensue!");
2460 
2461  if (target->blocking_thread == tthread)
2462  target->blocking_thread = NULL;
2463  probepoint->state = PROBE_BP_SET;
2464  probepoint_release(target,tthread,probepoint);
2465  tpc_free(tthread->tpc);
2466  tthread->tpc = (struct thread_probepoint_context *) \
2467  array_list_remove(tthread->tpc_stack);
2468 
2469  return -1;
2470  }
2471  else {
2472  /* We already own it. */
2473  probepoint->state = PROBE_BP_POSTHANDLING;
2474  }
2475  }
2476  }
2477 
2478  if (!doit) {
2479  /*
2480  * Only release our hold on the probepoint if we actually held
2481  * it; if we had been running a boosted action, we might not
2482  * hold the probepoint.
2483  */
2484  if (probepoint->tpc == tthread->tpc) {
2485  probepoint->state = PROBE_BP_SET;
2486  probepoint_release(target,tthread,probepoint);
2487  }
2488 
2489  return 0;
2490  }
2491 
2492  /*
2493  * Command the single step to happen. We hold the probepoint now,
2494  * so we can do anything.
2495  */
2496  vdebug(4,LA_PROBE,LF_PROBEPOINT,"doing sstep for ");
2498  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2499 
2500  if (probepoint->style == PROBEPOINT_HW && !target->nodisablehwbponss) {
2501  /*
2502  * We need to disable the hw breakpoint before we single
2503  * step because the target can't do it.
2504  */
2505  target_disable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
2506  probepoint->debugregdisabled = 1;
2507  }
2508 
2509  if (target_singlestep(target,tid,1) < 0) {
2510  verror("could not single step target after BP!\n");
2511 
2512  if (target_singlestep_end(target,tid))
2513  verror("could not stop single stepping target"
2514  " after failed sstep for breakpoint!\n");
2515 
2516  if (probepoint->style == PROBEPOINT_HW && !target->nodisablehwbponss) {
2517  /*
2518  * Reenable the hw breakpoint we just disabled.
2519  */
2520  target_enable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
2521  probepoint->debugregdisabled = 0;
2522  }
2523 
2524  if (probepoint->style != PROBEPOINT_HW) {
2525  if (target_enable_sw_breakpoint(target,probepoint->thread->tid,
2526  probepoint->mmod)) {
2527  verror("could not enable sw breakpoint after failed singlestep;"
2528  " assuming breakpoint is left in place and"
2529  " skipping single step, but badness will ensue!");
2530  }
2531  }
2532 
2533  /*
2534  * If we were supposed to single step, and it failed,
2535  * the best we can do is act like the BP is still set.
2536  */
2537  if (target->blocking_thread == tthread)
2538  target->blocking_thread = NULL;
2539  probepoint->state = PROBE_BP_SET;
2540  probepoint_release(target,tthread,probepoint);
2541  tpc_free(tthread->tpc);
2542  tthread->tpc = (struct thread_probepoint_context *) \
2543  array_list_remove(tthread->tpc_stack);
2544 
2545  return -1;
2546  }
2547  else {
2548  /*
2549  * If single step init succeeded, let the ss handler take
2550  * over.
2551  *
2552  * Don't call target_resume after a successful target_singlestep.
2553  */
2554  vdebug(4,LA_PROBE,LF_PROBEPOINT,"sstep command succeeded for ");
2556  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2557 
2558  if (probepoint->can_switch_context) {
2559  vdebug(4,LA_PROBE,LF_PROBEPOINT,"can_switch_context (%d) -- ",
2560  probepoint->can_switch_context);
2562  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2563  }
2564 
2565  return 1;
2566  }
2567 }
2568 
2569 /*
2570  * In many ways, the goal of this function is to figure out if we need
2571  * to do a single step after the breakpoint. There's lots of cases.
2572  * * If BP is SW:
2573  * - if any actions obviate (replace) the original instruction, do
2574  * not single step, just reset the state to BP_SET (AND re-insert
2575  * the BP right away) -- unless the
2576  * action requires one or more single steps; if so, do them.
2577  * - otherwise, replace the original instruction and set state to
2578  * BP_HANDLING and single step
2579  * -
2580  * * If BP is HW:
2581  * - if any actions obviate (replace) the original instruction, do
2582  * not single step, just reset the state to BP_SET -- unless the
2583  * action requires one or more single steps; if so, do them.
2584  * - otherwise, if no enabled probes have post handlers, do not
2585  * single step, just reset the state to BP_SET.
2586  * - otherwise, set state to BP_HANDLING and single step.
2587  *
2588  * NOTE: if we try to single step, we always disable hardware
2589  * breakpoints first. If the single step init fails, we re-enable
2590  * them. HW breakpoints are re-enabled in the ss handler below once
2591  * we're sure we don't need to do any more single steps.
2592  */
2593 result_t probepoint_bp_handler(struct target *target,
2594  struct target_thread *tthread,
2595  struct probepoint *probepoint,
2596  int was_stepping) {
2597  struct probe *probe;
2598  REGVAL ipval;
2599  int doit = 0;
2600  int rc;
2601  tid_t tid = tthread->tid;
2602  struct thread_probepoint_context *tpc, *htpc;
2603  int stepping = 0;
2604  struct action *action;
2605  struct thread_action_context *tac,*ttac;
2606  int already_ran_prehandlers_for_interrupted = 0;
2607 
2608  vdebug(5,LA_PROBE,LF_PROBEPOINT,"handling bp at ");
2610  vdebugc(5,LA_PROBE,LF_PROBEPOINT,"\n");
2611 
2612  /*
2613  * If the thread is already handling this probepoint, the cause is
2614  * almost certainly that some action we ran recursed, running some
2615  * code that triggered the breakpoint again.
2616  *
2617  * We keep trying to handle it if we can below...
2618  */
2619  if (tthread->tpc && tthread->tpc->probepoint == probepoint)
2620  vwarn("existing thread probepoint same as bp probepoint;"
2621  " BUG or recursion due to action?\n");
2622 
2623  /*
2624  * See docs for target_thread::interrupted_ss_probepoint, and for
2625  * probepoint_interrupted_ss_handler.
2626  */
2627  if (tthread->interrupted_ss_probepoint == probepoint) {
2628  tthread->interrupted_ss_probepoint = NULL;
2629  already_ran_prehandlers_for_interrupted = 1;
2630  }
2631 
2632  /*
2633  * If we were single stepping when we hit the breakpoint, and we hit
2634  * a hardware breakpoint, we need to fire the single step action
2635  * handlers off. If it's a software breakpoint, the trap after the
2636  * last instruction was allowed to happen before the breakpoint was
2637  * executed. But with hardware breakpoints, the single step trap
2638  * for the instruction preceding the breakpoint, and the breakpoint,
2639  * seem to happen in the same interrupt.
2640  */
2641  if (probepoint->style == PROBEPOINT_HW
2642  && was_stepping
2643  && !list_empty(&tthread->ss_actions)) {
2644  list_for_each_entry_safe(tac,ttac,&tthread->ss_actions,tac) {
2645  action = tac->action;
2646  ++tac->stepped;
2647 
2648  if (action->steps == -2
2649  || (action->steps > 0 && tac->stepped >= action->steps)) {
2650  if (action->handler)
2651  action->handler(action,tthread,action->probe,action->probe->probepoint,
2652  MSG_SUCCESS,tac->stepped,action->handler_data);
2653 
2654  action_finish_handling(action,tac);
2655 
2656  list_del(&tac->tac);
2657  free(tac);
2658  }
2659  else {
2660  if (action->handler)
2661  action->handler(action,tthread,action->probe,action->probe->probepoint,
2663  action->handler_data);
2664  }
2665  }
2666 
2667  if (list_empty(&tthread->ss_actions))
2668  was_stepping = 0;
2669  }
2670 
2671  /*
2672  * Push a new context for handling this probepoint.
2673  */
2674  tpc = tpc_new(tthread,probepoint);
2675  if (tthread->tpc) {
2676  array_list_append(tthread->tpc_stack,tthread->tpc);
2677 
2679  "already handling %d probepoints in thread %d; most recent"
2680  " (tpc %p) was ",
2681  array_list_len(tthread->tpc_stack),tthread->tid,tthread->tpc);
2683  }
2684  tthread->tpc = tpc;
2685 
2686  /*
2687  * Check: do we need to hold the probepoint? That depends on:
2688  * - is the probepoint software? hardware probepoints can always
2689  * be held, although which tpc the thread is handling may change
2690  * (i.e. if we are recursing trhough the same breakpoint again)
2691  * - do we have unboosted complex actions?
2692  * - do we have need to run the original instruction at the
2693  * probepoint?
2694  * But we can't even answer the second one until we know that the
2695  * pre-handlers haven't added any actions (which they must be able
2696  * to do), so we have no choice -- we must hold the breakpoint
2697  * before handling it at all. We may be able to release it sooner
2698  * than we think at the moment, but we can't know. The only time we
2699  * can just automatically hold the probepoint is if
2700  */
2701  if ((htpc = probepoint_hold(target,tthread,probepoint,tpc)) != tpc) {
2702  /*
2703  * If the BP is in some unset state, it might be being handled
2704  * for another thread. If not, we have a bug, because the state
2705  * is wrong, but we obviously hit the BP so it *is* set and we
2706  * just "handle" it.
2707  *
2708  * If it *is* being handled by another thread -- this could only
2709  * have happened if this thread hit the BP at about the same
2710  * time another thread did, but if we have scheduled the single
2711  * step for the other thread and executed it -- and then the
2712  * next time the target checked for debug interrupts, it checked
2713  * this thread first instead of the single-stepping thread.
2714  *
2715  * In this case, if we have thread control, we pause this thread
2716  * until we can handle the breakpoint (i.e., until nothing else
2717  * is). Of course, if we have thread control, we assume the
2718  * thread is already paused (it's at a debug interrupt, after
2719  * all); we just have to be careful not to unpause it until we
2720  * have handled its interrupt.
2721  *
2722  * If we don't have thread control, there's really nothing we
2723  * can do. Sometimes loose mode sucks :).
2724  */
2725  if (probepoint->state != PROBE_BP_SET) {
2726  if (!probepoint->tpc) {
2727  verror("probepoint state is not BP_SET; BUG!\n");
2728 
2729  probepoint->state = PROBE_BP_SET;
2730  }
2731  else if (probepoint_pause_handling(target,tthread,probepoint,
2733  verror("thread %"PRIiTID" hit bp when thread %"PRIiTID" already"
2734  " handling it; target could not pause thread!\n",
2735  tthread->tid,probepoint->tpc->thread->tid);
2736  /*
2737  * There literally is nothing we can do -- we cannot
2738  * handle this breakpoint interrupt.
2739  */
2740  return RESULT_ERROR;
2741  }
2742  else
2743  return RESULT_SUCCESS;
2744  }
2745  }
2746 
2747  /*
2748  * We own this probepoint now. BUT, we might still have to pause
2749  * other threads in the target if we have to adjust the probepoint.
2750  */
2751 
2752  /* We move into the HANDLING state while we run the pre-handlers. */
2753  probepoint->state = PROBE_BP_PREHANDLING;
2754 
2755  /*
2756  * Prepare for handling: save the original ip value in case
2757  * something bad happens during the pre-handlers.
2758  */
2759  errno = 0;
2760  ipval = target_read_reg(target,tid,target->ipregno);
2761  if (probepoint->style == PROBEPOINT_SW && errno) {
2762  verror("could not read EIP to reset it for SW probepoint; skipping!");
2763  probepoint->state = PROBE_BP_SET;
2764  probepoint_release(target,tthread,probepoint);
2765  tpc_free(tthread->tpc);
2766  tthread->tpc = (struct thread_probepoint_context *) \
2767  array_list_remove(tthread->tpc_stack);
2768  return RESULT_ERROR;
2769  }
2770 
2771  vdebug(5,LA_PROBE,LF_PROBEPOINT,"EIP is 0x%"PRIxREGVAL" at ",ipval);
2773  vdebugc(5,LA_PROBE,LF_PROBEPOINT,"\n");
2774 
2775 
2776  /* If SW bp, reset EIP and write it back *now*, because it's easy
2777  * here, and then if the user tries to read it, it's "correct".
2778  */
2779  if (!target->no_adjust_bp_ip && probepoint->style == PROBEPOINT_SW) {
2780  ipval -= target->arch->breakpoint_instrs_len;
2781  errno = 0;
2782  target_write_reg(target,tid,target->ipregno,ipval);
2783  if (errno) {
2784  verror("could not reset EIP before pre handlers; skipping!\n");
2785  probepoint->state = PROBE_BP_SET;
2786  probepoint_release(target,tthread,probepoint);
2787  tpc_free(tthread->tpc);
2788  tthread->tpc = (struct thread_probepoint_context *) \
2789  array_list_remove(tthread->tpc_stack);
2790  return RESULT_ERROR;
2791  }
2792 
2793  vdebug(4,LA_PROBE,LF_PROBEPOINT,"reset EIP to 0x%"PRIxREGVAL" for SW ",ipval);
2795  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2796  }
2797 
2798  /*
2799  * Run pre-handlers if we have encountered our breakpoint for the
2800  * first time on this pass (which means we should not have an action
2801  * set!)
2802  */
2803  if (!already_ran_prehandlers_for_interrupted) {
2804  list_for_each_entry(probe,&probepoint->probes,probe) {
2805  PROBE_SAFE_OP_ARGS(probe,values_notify_phase,tthread->tid,
2806  PHASE_PRE_START);
2807 
2808  if (probe->enabled) {
2809  if (probe->pre_handler) {
2810  vdebug(4,LA_PROBE,LF_PROBEPOINT,"running pre handler at ");
2812  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2813 
2814  rc = probe->pre_handler(probe,tid,probe->handler_data,probe,probe);
2815  if (rc == RESULT_ERROR)
2816  probe_disable(probe);
2817  }
2818  else if (0 && probe->sinks) {
2820  "running default probe sink pre_handler at ");
2822  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2823  probe_do_sink_pre_handlers(probe,tid,NULL,probe,probe);
2824  }
2825  doit = 1;
2826  }
2827 
2828  PROBE_SAFE_OP_ARGS(probe,values_notify_phase,tthread->tid,
2829  PHASE_PRE_END);
2830  }
2831  }
2832  else {
2834  "already ran pre handlers in interrupted thread %"PRIiTID" at ",
2835  tid);
2837  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2838  }
2839 
2840  /* Restore ip register if we ran a handler. */
2841  if (!target->no_adjust_bp_ip && doit) {
2842  errno = 0;
2843  target_write_reg(target,tid,target->ipregno,ipval);
2844  if (errno) {
2845  verror("could not reset EIP after pre handlers!\n");
2846  probepoint->state = PROBE_BP_SET;
2847  probepoint_release(target,tthread,probepoint);
2848  tpc_free(tthread->tpc);
2849  tthread->tpc = (struct thread_probepoint_context *) \
2850  array_list_remove(tthread->tpc_stack);
2851  return RESULT_ERROR;
2852  }
2853 
2855  "ip 0x%"PRIxREGVAL" restored after pre handlers at ",ipval);
2857  vdebugc(9,LA_PROBE,LF_PROBEPOINT,"\n");
2858  }
2859 
2860  /* Now we're handling actions. */
2861  probepoint->state = PROBE_BP_ACTIONHANDLING;
2862 
2863  /*
2864  * Run the simple actions now.
2865  */
2866  if ((rc = handle_simple_actions(target,tthread,probepoint)))
2867  vwarn("failed to handle %d simple actions!\n",rc);
2868 
2869  /*
2870  * Set up complex actions. The output of the setup is what code we
2871  * need to place at the breakpoint site; the min single steps before
2872  * we can replace that code with 1) the original code, if the tmp
2873  * code does not replace the orig; or 2) the breakpoint again, if
2874  * the tmp code *does* replace the orig. Or, it could be nothing.
2875  *
2876  * (If the complex action requires more than one single step, we
2877  * cannot support it unless the target is capable of pausing all
2878  * other threads (i.e., ensuring that the current thread cannot be
2879  * preempted). We need this because we can basically assume that
2880  * the first single step will not be "interrupted" after a
2881  * breakpoint unless an interrupt happened to fire at the same time
2882  * as the breakpoint. But, we can't assume that N single steps
2883  * won't be interrupted; if they are, the probepoint is storing
2884  * per-thread context, and it could get messed up bad. Plus, the
2885  * probepoint might have arbitrary code at its location, instead of
2886  * the breakpoint or original instruction.)
2887  *
2888  * If the output is nothing (i.e., no complex actions), we decide if
2889  * we need to single step (always do for SW; if HW, and no probes
2890  * are enabled, or none of the enalbed probes have posthandlers, we
2891  * can skip the SW breakpoint; OR ALSO do if we have one or more
2892  * single step actions).
2893  *
2894  * Then in the ss handler, we have to check what state we're in.
2895  * First, if we are called with no probepoint, we must be sstep'ing
2896  * for an sstep action(s). Otherwise, if probepoint->action is set,
2897  * we must be sstep'ing a complex action (or on behalf of it).
2898  * Otherwise, we must just be sstep'ing the original instr under the
2899  * breakpoint.
2900  *
2901  * In the first case, just hanlde the ss_actions list.
2902  *
2903  * In the second case, continue sstep'ing if the action requires it;
2904  * otherwise, either setup another complex action, or replace the
2905  * underlying code if the action didn't obviate it -- and single
2906  * step the orig code. Then put the breakpoint back in.
2907  *
2908  * In the third case, just put the breakpoint back in.
2909  *
2910  * In all cases, leave single step enabled if there are still
2911  * ss_actions running for the thread.
2912  */
2913  if ((stepping = handle_complex_actions(target,tthread,probepoint)) < 0)
2914  vwarn("failed to handle %d complex actions!\n",rc);
2915 
2916  /*
2917  * If there is a complex action, just setup single step actions (and
2918  * we might already be single stepping for the complex action).
2919  * handle_complex_actions also paused all other threads if we needed to.
2920  */
2921  if (tpc->tac.action) {
2922  vdebug(4,LA_PROBE,LF_PROBEPOINT,"setup complex action for ");
2924  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2925 
2926  int _isbp = 0;
2927  if (!tpc->tac.action->boosted)
2928  _isbp = 1;
2929 
2930  rc = setup_single_step_actions(target,tthread,probepoint,_isbp,stepping);
2931  if (rc == 1)
2932  rc = 0;
2933  }
2934  /*
2935  * Otherwise, if we did not perform a complex action, or if the
2936  * complex action did not require any single steps, figure out if we
2937  * should re-execute the original instructions -- and do we boost
2938  * them or replace them at the breakpoint and single step it.
2939  */
2940  else {
2941  tpc->did_orig_instr = 1;
2942 
2943  vdebug(4,LA_PROBE,LF_PROBEPOINT,"setting up post handling single step for ");
2945  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2946  rc = setup_post_single_step(target,tthread,probepoint);
2947 
2948  if (rc == 0) {
2949  /*
2950  * No single step scheduled, but no error, so we're done
2951  * handling the probepoint in this thread; pop the stack.
2952  */
2953  if (target->blocking_thread == tthread)
2954  target->blocking_thread = NULL;
2955  probepoint_release(target,tthread,probepoint);
2956  tpc_free(tthread->tpc);
2957  tthread->tpc = (struct thread_probepoint_context *) \
2958  array_list_remove(tthread->tpc_stack);
2959 
2961  "thread %"PRIiTID" skipped orig instruction; clearing tpc!\n",
2962  tthread->tid);
2963 
2964  /*
2965  * We need to setup single step actions to happen because we
2966  * are not stepping yet, and we might need to.
2967  */
2968  rc = setup_single_step_actions(target,tthread,probepoint,1,0);
2969  if (rc == 1) {
2970  /*
2971  * Don't call target_resume; we've already called
2972  * target_singlestep.
2973  */
2974  return RESULT_SUCCESS;
2975  }
2976  }
2977  else if (rc == 1) {
2978  /*
2979  * If we need to keep single stepping, just let that happen
2980  * -- it's already been set up (or might have been setup in
2981  * setup_post_single_step).
2982  *
2983  * We need to setup single step actions to happen even if
2984  * we're already stepping (bookkeeping).
2985  */
2986  rc = setup_single_step_actions(target,tthread,probepoint,1,1);
2987 
2988  /*
2989  * Don't call target_resume; we've already called
2990  * target_singlestep.
2991  */
2992  return RESULT_SUCCESS;
2993  }
2994  else if (rc == 2) {
2995  /* The thread blocked because it could not hold the probepoint. */
2997  "thread %"PRIiTID" blocked before single step; thread"
2998  " %"PRIiTID" owned probepoint -- BUG!!!\n",
2999  tthread->tid,probepoint->tpc->thread->tid);
3000  }
3001  else if (rc < 0) {
3002  verror("could not setup single step; badness will probably ensue!\n");
3003  }
3004  }
3005 
3006  return RESULT_SUCCESS;
3007 }
3008 
3009 /*
3010  * This function handles single step events. It broadly handles a few
3011  * cases. First, are there pending single step actions; if so, stay in
3012  * single step mode after we're done. Second, try to handle any complex
3013  * actions that might be needing single steps, if there are, let that
3014  * keep going. Finally, if we're done with any complex actions, and we
3015  * still haven't run the original instruction (and if none of hte
3016  * complex actions obviated (replaced) the original one), we run the
3017  * original instruction via single step (if necessary -- it might not be
3018  * necessary if we don't have posthandlers and if the breakpoint is hw,
3019  * or if the orig instruction was boostable.
3020  */
3021 result_t probepoint_ss_handler(struct target *target,
3022  struct target_thread *tthread,
3023  struct probepoint *probepoint) {
3024  struct thread_action_context *tac,*ttac;
3025  struct action *action;
3026  tid_t tid = tthread->tid;
3027  int rc;
3028  int handled_ss_actions = 0;
3029  int keep_stepping = 0;
3030  struct thread_probepoint_context *tpc = tthread->tpc;
3031  int noreinject;
3032  handler_msg_t amsg;
3033  struct probepoint *aprobepoint;
3034  REGVAL ipval;
3035 
3036  /*
3037  * If we had to disable a hw breakpoint before we single stepped it,
3038  * reenable it now. If it's a hardware breakpoint, we own it, so
3039  * that's why we don't check that.
3040  */
3041  if (probepoint
3042  && probepoint->style == PROBEPOINT_HW
3043  && probepoint->debugregdisabled
3044  && !target->nodisablehwbponss) {
3045  /*
3046  * Reenable the hw breakpoint we just disabled.
3047  */
3048  target_enable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
3049  probepoint->debugregdisabled = 0;
3050  }
3051 
3052  /*
3053  * First, if there is no probepoint (or if there is and we have
3054  * single step actions pending) and if this thread has pending
3055  * ss_actions. If one or the other, go through that list, increment
3056  * the counts, notify the handlers, and remove any ss_actions that
3057  * are done. If all are done, end singlestep mode and resume.
3058  * Otherwise, stay in single step mode.
3059  */
3060  if (!list_empty(&tthread->ss_actions)) {
3061  handled_ss_actions = 1;
3062 
3063  /*
3064  * If this address is an enalbed breakpiont, we need to send
3065  * MSG_STEPPING_AT_BP instead. This covers the software
3066  * breakpiont case; for the hardware one, see bp_handler.
3067  */
3068  amsg = MSG_STEPPING;
3069  ipval = target_read_reg(target,tthread->tid,target->ipregno);
3070  aprobepoint = target_lookup_probepoint(target,tthread,ipval);
3071  if (aprobepoint && aprobepoint->state != PROBE_DISABLED)
3072  amsg = MSG_STEPPING_AT_BP;
3073 
3074  list_for_each_entry_safe(tac,ttac,&tthread->ss_actions,tac) {
3075  action = tac->action;
3076  ++tac->stepped;
3077 
3078  if (action->steps < 0 || tac->stepped < action->steps) {
3079  if (action->handler)
3080  action->handler(action,tthread,action->probe,action->probe->probepoint,
3081  amsg,tac->stepped,action->handler_data);
3082  }
3083  else {
3084  if (action->handler)
3085  action->handler(action,tthread,action->probe,action->probe->probepoint,
3086  MSG_SUCCESS,tac->stepped,action->handler_data);
3087 
3088  action_finish_handling(action,tac);
3089 
3090  list_del(&tac->tac);
3091  free(tac);
3092  }
3093  }
3094 
3095  if (!list_empty(&tthread->ss_actions))
3096  keep_stepping = 1;
3097  }
3098  /*
3099  * If there is no probepoint, we return to the caller without
3100  * unpausing the target.
3101  */
3102  else if (!probepoint) {
3103  vwarn("thread %"PRIiTID" unexpected single step!\n",tthread->tid);
3104  return RESULT_ERROR;
3105  }
3106 
3107  /*
3108  * If we're done handling actions and the post single step of the
3109  * original instruction, AND if we were the owners of the
3110  * probepoint, nuke the tpc state!
3111  *
3112  * BUT, we have to check if we should keep single stepping for
3113  * pending single step actions.
3114  */
3115  if (probepoint
3116  && probepoint->tpc == tpc
3117  && probepoint->state == PROBE_BP_POSTHANDLING
3118  && tpc->did_orig_instr) {
3119  noreinject = run_post_handlers(target,tthread,probepoint);
3120 
3121  if (!noreinject) {
3122  if (probepoint->style == PROBEPOINT_SW) {
3123  /* Re-inject a breakpoint for the next round */
3124  if (target_enable_sw_breakpoint(target,probepoint->thread->tid,
3125  probepoint->mmod)) {
3126  verror("could not enable sw breakpoint instrs for bp re-insert, disabling!\n");
3127  probepoint->state = PROBE_DISABLED;
3128  return RESULT_ERROR;
3129  }
3130  else
3131  probepoint->state = PROBE_BP_SET;
3132  }
3133  else if (probepoint->debugregdisabled) {
3134  /*
3135  * Enable hardware bp here!
3136  */
3137  target_enable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
3138  probepoint->debugregdisabled = 0;
3139 
3140  probepoint->state = PROBE_BP_SET;
3141  }
3142  else {
3143  probepoint->state = PROBE_BP_SET;
3144  }
3145  }
3146  else {
3147  __probepoint_remove(probepoint,0,0);
3148  }
3149 
3150  if (target_singlestep_end(target,tid))
3151  verror("could not stop single stepping target"
3152  " after failed sstep!\n");
3153 
3154  if (target->blocking_thread == tthread)
3155  target->blocking_thread = NULL;
3156  probepoint_release(target,tthread,probepoint);
3157  tpc_free(tthread->tpc);
3158  tthread->tpc = (struct thread_probepoint_context *) \
3159  array_list_remove(tthread->tpc_stack);
3160  tpc = NULL;
3161 
3163  "thread %"PRIiTID" ran orig instruction; cleared tpc!\n",
3164  tthread->tid);
3165 
3166  if (keep_stepping)
3167  goto keep_stepping;
3168  else
3169  /* We're done with this, and don't want anything else below. */
3170  return RESULT_SUCCESS;
3171  }
3172 
3173  /*
3174  * Handle complex actions. This function does everything it might
3175  * need; if it leaves an action in tpc->tac.action, we just call
3176  * target_resume and trust that it did all the setup it needs.
3177  */
3178  if (tpc)
3179  rc = handle_complex_actions(target,tthread,probepoint);
3180 
3181  /*
3182  * NOTE: after this giant if stmt, the default return case is
3183  * RESULT_SUCCESS, unless another return stmt occurs.
3184  */
3185 
3186  if (tpc && tpc->tac.action) {
3187  /*
3188  * If handle_complex_actions left something in tpc->tac.action,
3189  * we just want to return success and wait and see what happens
3190  * with the action via future debug expections.
3191  */
3192  ;
3193  }
3194  /*
3195  * If we stepped an action first, we might still need to single step
3196  * the original instruction; do that if so. Or at least attempt it
3197  * -- if the action obviated the orig instruction,
3198  * setup_post_single_step handles that case and does the right things.
3199  */
3200  else if (tpc && !tpc->did_orig_instr) {
3201  vdebug(4,LA_PROBE,LF_PROBEPOINT,"setting up post handling single step for ");
3203  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
3204  rc = setup_post_single_step(target,tthread,probepoint);
3205 
3206  if (rc == 0) {
3207  /*
3208  * No single step scheduled, but no error, so we're done
3209  * handling the probepoint in this thread; pop the stack.
3210  */
3211  if (target->blocking_thread == tthread)
3212  target->blocking_thread = NULL;
3213  probepoint_release(target,tthread,probepoint);
3214  tpc_free(tthread->tpc);
3215  tthread->tpc = (struct thread_probepoint_context *) \
3216  array_list_remove(tthread->tpc_stack);
3217 
3219  "thread %"PRIiTID" skipping orig instruction; clearing tpc!\n",
3220  tthread->tid);
3221 
3222  /*
3223  * If we need to keep stepping for single step actions, do
3224  * that here.
3225  */
3226  if (keep_stepping)
3227  goto keep_stepping;
3228  /*
3229  else {
3230  return RESULT_SUCCESS;
3231  }
3232  */
3233  }
3234  else if (rc == 1) {
3235  /* Stepping, so just return without target_resume! */
3236  tpc->did_orig_instr = 1;
3237  //return RESULT_SUCCESS;
3238  }
3239  else if (rc == 2) {
3240  /* The thread blocked because it could not hold the probepoint. */
3242  "thread %"PRIiTID" blocked before single step real; thread"
3243  " %"PRIiTID" owned probepoint\n",
3244  tthread->tid,probepoint->tpc->thread->tid);
3245  //return RESULT_SUCCESS;
3246  }
3247  else if (rc < 0) {
3248  verror("could not setup single step; badness will probably ensue!\n");
3249  //return RESULT_SUCCESS;
3250  }
3251  }
3252  /*
3253  * If we don't have any complex actions to handle, we might still
3254  * need to stay in single step mode if any singlestep actions were
3255  * in progress for this thread. So check that.
3256  *
3257  * The difference between this single step and a single step at the
3258  * probepoint is that we don't disable hardware breakpoints before
3259  * single stepping!
3260  */
3261  else if (keep_stepping) {
3262  keep_stepping:
3263  if (probepoint) {
3264  vdebug(4,LA_PROBE,LF_PROBEPOINT,"continuing single step for ");
3266  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
3267  }
3268  else
3270  "continuing single step after probepoint\n");
3271 
3272  if (target_singlestep(target,tid,0) < 0) {
3273  verror("could not keep single stepping target!\n");
3274 
3275  if (target_singlestep_end(target,tid))
3276  verror("could not stop single stepping target"
3277  " after failed sstep!\n");
3278 
3279  /*
3280  * All we can really do is notify all the single step
3281  * actions that we had an error, and nuke them, and keep
3282  * going.
3283  */
3284  list_for_each_entry_safe(tac,ttac,&tthread->ss_actions,tac) {
3285  if (tac->action->handler)
3286  tac->action->handler(tac->action,tthread,tac->action->probe,
3287  tac->action->probe->probepoint,
3288  MSG_FAILURE,tac->stepped,
3289  tac->action->handler_data);
3290 
3291  action_finish_handling(tac->action,tac);
3292 
3293  list_del(&tac->tac);
3294  free(tac);
3295  }
3296 
3297  return RESULT_ERROR;
3298  }
3299  else {
3300  /*
3301  * If single step init succeeded, let the ss handler take
3302  * over.
3303  *
3304  * Don't call target_resume after a successful target_singlestep.
3305  */
3306  if (probepoint) {
3307  vdebug(4,LA_PROBE,LF_PROBEPOINT,"sstep command succeeded for ");
3309  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
3310  }
3311  else
3313  "sstep command succeeded after probepoint\n");
3314  }
3315  }
3316  /*
3317  else if (tpc->did_orig_instr) {
3318  *
3319  * We're totally done with this probepoint.
3320  *
3321  rc = 0;
3322  goto out;
3323  }
3324  */
3325  else if (!probepoint && !tpc && handled_ss_actions && !keep_stepping) {
3327  "finished single step actions after probepoint\n");
3328 
3329  if (target_singlestep_end(target,tid))
3330  verror("could not stop single stepping target after single step"
3331  " actions after probepoint!\n");
3332  }
3333  else {
3334  verror("unexpected state! BUG?\n");
3335  return RESULT_ERROR;
3336  }
3337 
3338  return RESULT_SUCCESS;
3339 }
3340 
3341 /*
3342  * This function handles the case where a thread is singlestepping, but
3343  * it has either stepped into a new context (i.e., user into kernel), or
3344  * into a different thread. Initially, we only handle the case where
3345  * the breakpoint was just hit and the single step setup, but not run.
3346  * We do not handle the case where a single step action was running.
3347  *
3348  * This is much the same as the single step handler, BUT we abort any
3349  * running actions; we do not continue single stepping; and we unwind
3350  * all state associated with handling the breakpoint.
3351  *
3352  * NB: we cannot read any register state from the thread; another thread
3353  * or thread context (i.e., kernel instead of user) might have been
3354  * entered. We just want to unwind the previous context and fixup the
3355  * probepoint.
3356  */
3358  struct target_thread *tthread,
3359  struct probepoint *probepoint) {
3360  struct thread_action_context *tac,*ttac;
3361  struct action *action;
3362  tid_t tid = tthread->tid;
3363  struct thread_probepoint_context *tpc;
3364 
3365  tpc = tthread->tpc;
3366 
3367  /*
3368  * If we had to disable a hw breakpoint before we single stepped it,
3369  * reenable it now. If it's a hardware breakpoint, we own it, so
3370  * that's why we don't check that.
3371  */
3372  if (probepoint
3373  && probepoint->style == PROBEPOINT_HW
3374  && probepoint->debugregdisabled
3375  && !target->nodisablehwbponss) {
3376  /*
3377  * Reenable the hw breakpoint we just disabled.
3378  */
3379  target_enable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
3380  probepoint->debugregdisabled = 0;
3381  }
3382 
3383  /*
3384  * Cancel any single step actions.
3385  */
3386  if (!list_empty(&tthread->ss_actions)) {
3387  list_for_each_entry_safe(tac,ttac,&tthread->ss_actions,tac) {
3388  action = tac->action;
3389 
3390  if (action->handler)
3391  action->handler(action,tthread,action->probe,
3392  action->probe->probepoint,
3393  MSG_INTERRUPTED,tac->stepped,
3394  action->handler_data);
3395 
3396  action_finish_handling(action,tac);
3397 
3398  list_del(&tac->tac);
3399  free(tac);
3400  }
3401  }
3402 
3403  /*
3404  * If we're done handling actions and the post single step of the
3405  * original instruction, AND if we were the owners of the
3406  * probepoint, nuke the tpc state!
3407  *
3408  * BUT, we have to check if we should keep single stepping for
3409  * pending single step actions.
3410  */
3411  if (probepoint->tpc == tpc) {
3412  /*
3413  * Cancel any complex actions.
3414  */
3415  if (!list_empty(&probepoint->complex_actions)) {
3416  list_for_each_entry_safe(tac,ttac,&probepoint->complex_actions,
3417  tac) {
3418  action = tac->action;
3419 
3421  "finished %d steps; done and removing interrupted"
3422  " action at ",
3423  tpc->tac.stepped);
3424  LOGDUMPPROBEPOINT(5,LA_PROBE,LF_ACTION,probepoint);
3425  vdebugc(5,LA_PROBE,LF_ACTION,"\n");
3426 
3427  if (action->handler)
3428  action->handler(action,tthread,action->probe,
3429  action->probe->probepoint,
3430  MSG_INTERRUPTED,tac->stepped,
3431  action->handler_data);
3432 
3433  action_finish_handling(action,tac);
3434 
3435  list_del(&tac->tac);
3436  free(tac);
3437  }
3438  }
3439 
3440  if (probepoint->style == PROBEPOINT_SW) {
3441  /* Re-inject a breakpoint for the next round */
3442  if (target_enable_sw_breakpoint(target,probepoint->thread->tid,
3443  probepoint->mmod)) {
3444  verror("could not enable sw breakpoint instrs for bp re-insert, disabling!\n");
3445  probepoint->state = PROBE_DISABLED;
3446  return RESULT_ERROR;
3447  }
3448  else
3449  probepoint->state = PROBE_BP_SET;
3450  }
3451  else if (probepoint->debugregdisabled) {
3452  /*
3453  * Enable hardware bp here!
3454  */
3455  target_enable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
3456  probepoint->debugregdisabled = 0;
3457 
3458  probepoint->state = PROBE_BP_SET;
3459  }
3460  else {
3461  probepoint->state = PROBE_BP_SET;
3462  }
3463 
3464  probepoint_release(target,tthread,probepoint);
3465  }
3466  else
3467  vwarn("thread %"PRIiTID" does not own tpc (thread %"PRIiTID" does);"
3468  " cannot adjust probepoint state!!\n",
3469  tid,tpc->thread->tid);
3470 
3471  /* Do this stuff anyway. */
3472  if (target_singlestep_end(target,tid))
3473  verror("could not stop single stepping target"
3474  " after interrupted sstep!\n");
3475 
3476  if (target->blocking_thread == tthread)
3477  target->blocking_thread = NULL;
3478  tpc_free(tthread->tpc);
3479  tthread->tpc = (struct thread_probepoint_context *) \
3480  array_list_remove(tthread->tpc_stack);
3481  tpc = NULL;
3482 
3483  /*
3484  * Save it off so that probepoint_bp_handler will see it when we hit
3485  * the breakpoint again.
3486  */
3488 
3490  "thread %"PRIiTID" interrupted before running orig instruction;"
3491  " cleared tpc!\n",
3492  tthread->tid);
3493 
3494  return RESULT_SUCCESS;
3495 }
3496 
3497 /*
3498  * This function should be called for each thread that has been left
3499  * paused before target_resume completes; if bp_handler or ss_handler
3500  * cleared
3501  */
3503  struct target_thread *tthread) {
3504  struct probepoint *probepoint;
3505  struct thread_probepoint_context *tpc = tthread->tpc;
3506  int rc;
3507  tid_t tid = tthread->tid;
3508  struct thread_action_context *tac,*ttac;
3509 
3510  switch (tthread->resumeat) {
3511  case THREAD_RESUMEAT_NONE:
3512  return RESULT_ERROR;
3513  case THREAD_RESUMEAT_BPH:
3514  return probepoint_bp_handler(target,tthread,tthread->tpc->probepoint,0);
3515  case THREAD_RESUMEAT_SSR:
3516  return setup_post_single_step(target,tthread,tthread->tpc->probepoint);
3517  case THREAD_RESUMEAT_NA:
3518  /*
3519  * This could have only been interrupted comoing from the ss
3520  * handler, so we know that if it does not do an action and does
3521  * not stay in single step, we have to try to do the post single
3522  * step too (?)
3523  */
3524  probepoint = tthread->tpc->probepoint;
3525  handle_complex_actions(target,tthread,probepoint);
3526  if (!tpc->tac.action && !tpc->did_orig_instr) {
3527  vdebug(4,LA_PROBE,LF_PROBEPOINT,"setting up post handling single step for ");
3529  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
3530  rc = setup_post_single_step(target,tthread,probepoint);
3531 
3532  if (rc == 0) {
3533  /*
3534  * No single step scheduled, but no error, so we're done
3535  * handling the probepoint in this thread; pop the stack.
3536  */
3537  if (target->blocking_thread == tthread)
3538  target->blocking_thread = NULL;
3539  probepoint_release(target,tthread,probepoint);
3540  tpc_free(tthread->tpc);
3541  tthread->tpc = (struct thread_probepoint_context *) \
3542  array_list_remove(tthread->tpc_stack);
3543 
3545  "thread %"PRIiTID" skipping orig instruction; clearing tpc!\n",
3546  tthread->tid);
3547 
3548  }
3549  else if (rc == 1) {
3550  /* Stepping, so just return without target_resume! */
3551  tpc->did_orig_instr = 1;
3552  //return RESULT_SUCCESS;
3553  }
3554  else if (rc == 2) {
3555  /* The thread blocked because it could not hold the probepoint. */
3557  "thread %"PRIiTID" blocked before single step real; thread"
3558  " %"PRIiTID" owned probepoint\n",
3559  tthread->tid,probepoint->tpc->thread->tid);
3560  //return RESULT_SUCCESS;
3561  }
3562  else if (rc < 0) {
3563  verror("could not setup single step; badness will probably ensue!\n");
3564  //return RESULT_SUCCESS;
3565  }
3566  }
3567  else if (!tpc->tac.action && !tpc->tac.action->steps
3568  && !list_empty(&tthread->ss_actions)) {
3570  "continuing single step after probepoint\n");
3571 
3572  if (target_singlestep(target,tid,0) < 0) {
3573  verror("could not keep single stepping target!\n");
3574 
3575  if (target_singlestep_end(target,tid))
3576  verror("could not stop single stepping target"
3577  " after failed sstep!\n");
3578 
3579  /*
3580  * All we can really do is notify all the single step
3581  * actions that we had an error, and nuke them, and keep
3582  * going.
3583  */
3584  list_for_each_entry_safe(tac,ttac,&tthread->ss_actions,tac) {
3585  if (tac->action->handler)
3586  tac->action->handler(tac->action,tthread,
3587  tac->action->probe,
3588  tac->action->probe->probepoint,
3589  MSG_FAILURE,tac->stepped,
3590  tac->action->handler_data);
3591 
3592  action_finish_handling(tac->action,tac);
3593 
3594  list_del(&tac->tac);
3595  free(tac);
3596  }
3597 
3598  return RESULT_ERROR;
3599  }
3600  else {
3601  /*
3602  * If single step init succeeded, let the ss handler take
3603  * over.
3604  *
3605  * Don't call target_resume after a successful target_singlestep.
3606  */
3607  vdebug(4,LA_PROBE,LF_PROBEPOINT,"sstep command succeeded for resumeat ");
3609  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
3610  }
3611  }
3612  else {
3614  "finished handling after resumeat\n");
3615 
3616  if (target_singlestep_end(target,tid))
3617  verror("could not stop single stepping target after single step"
3618  " actions after probepoint!\n");
3619  }
3620  return RESULT_SUCCESS;
3621  case THREAD_RESUMEAT_PH:
3622  return RESULT_ERROR;
3623  default:
3624  return RESULT_ERROR;
3625  }
3626 }
3627 
3628 static int __remove_action(struct target *target,struct probepoint *probepoint,
3629  struct action *action) {
3630  /*
3631  * If it was boosted, it will be removed when the action is destroyed.
3632  */
3633  if (action->boosted)
3634  return 0;
3635 
3636  if (action->type == ACTION_RETURN || action->type == ACTION_CUSTOMCODE) {
3637  if (probepoint->style == PROBEPOINT_SW) {
3638  if (target_unchange_sw_breakpoint(target,probepoint->thread->tid,
3639  probepoint->mmod)) {
3640  verror("could not remove action code at the breakpoint;"
3641  " badness will probably ensue!\n");
3642  }
3643  if (target_enable_sw_breakpoint(target,probepoint->thread->tid,
3644  probepoint->mmod)) {
3645  verror("could not reenable the breakpoint;"
3646  " badness will probably ensue!\n");
3647  return -1;
3648  }
3649  }
3650  else if (probepoint->mmod) {
3651  if (target_memmod_release(target,probepoint->thread->tid,
3652  probepoint->mmod) < 0) {
3653  verror("could not remove action code at HW breakpoint;"
3654  " badness will probably ensue!\n");
3655  return -1;
3656  }
3657  else
3658  probepoint->mmod = NULL;
3659  }
3660  }
3661 
3662  probepoint->state = PROBE_ACTION_DONE;
3663 
3664  return 0;
3665 }
3666 
3667 static int __insert_action(struct target *target,struct target_thread *tthread,
3668  struct probepoint *probepoint,struct action *action) {
3669  unsigned int buflen;
3670  unsigned char *buf;
3671  REGVAL rval;
3672 
3673  /* Set EIP to the address of our code. */
3674  if (target_write_reg(target,tthread->tid,target->ipregno,action->start_addr)) {
3675  verror("could not set EIP to action's first instruction (0x%"PRIxADDR")!\n",
3676  action->start_addr);
3677  return -1;
3678  }
3679 
3680  /*
3681  * If it was boosted, it was inserted when it was scheduled.
3682  */
3683  if (action->boosted)
3684  return 0;
3685 
3686  if (action->type == ACTION_RETURN) {
3687  /*
3688  * If we have executed a prologue: if the prologue contains
3689  * a save of the frame pointer (0x55), all we have to do is
3690  * set rsp to rbp, call leaveq, and call retq.
3691  * If the prologue does not contain a save of the frame
3692  * pointer, we have to track all modifications to rsp during
3693  * the prologue, undo them, and call leaveq, and retq.
3694  */
3695  if (action->detail.ret.prologue) {
3696  if (action->detail.ret.prologue_uses_bp) {
3698  "setting ESP to EBP and returning (prologue uses EBP) at ");
3699  LOGDUMPPROBEPOINT(3,LA_PROBE,LF_ACTION,probepoint);
3700  vdebugc(3,LA_PROBE,LF_ACTION,"\n");
3701 
3702  errno = 0;
3703  rval = target_read_reg(target,tthread->tid,target->fbregno);
3704  if (errno) {
3705  verror("read EBP failed; action failed!\n");
3706  return -1;
3707  }
3708 
3709  if (target_write_reg(target,tthread->tid,target->spregno,rval)) {
3710  verror("set ESP to EBP failed; action failed and badness will ensue!\n");
3711  return -1;
3712  }
3713 
3714  buf = target->arch->full_ret_instrs;
3715  buflen = target->arch->full_ret_instrs_len;
3716  action->steps = target->arch->full_ret_instr_count;
3717  }
3718  else {
3720  "undoing prologue ESP changes (%d) and returning at ",
3721  action->detail.ret.prologue_sp_offset);
3722  LOGDUMPPROBEPOINT(3,LA_PROBE,LF_ACTION,probepoint);
3723  vdebugc(3,LA_PROBE,LF_ACTION,"\n");
3724 
3725  errno = 0;
3726  rval = target_read_reg(target,tthread->tid,target->spregno);
3727  if (errno) {
3728  verror("read ESP failed; action failed!\n");
3729  return -1;
3730  }
3731 
3732  if (target_write_reg(target,tthread->tid,target->spregno,
3733  rval + (REGVAL)-action->detail.ret.prologue_sp_offset)) {
3734  verror("undoing prologue ESP changes failed; action failed!\n");
3735  return -1;
3736  }
3737 
3738  buf = target->arch->ret_instrs;
3739  buflen = target->arch->ret_instrs_len;
3740  action->steps = target->arch->ret_instr_count;
3741  }
3742  }
3743  else {
3744  buf = target->arch->ret_instrs;
3745  buflen = target->arch->ret_instrs_len;
3746  action->steps = target->arch->ret_instr_count;
3747  }
3748  }
3749  else if (action->type == ACTION_CUSTOMCODE) {
3750  buf = action->detail.code.buf;
3751  buflen = action->detail.code.buflen;
3752  }
3753  else {
3754  verror("cannot handle unknown action type %d!\n",action->type);
3755  return -1;
3756  }
3757 
3758  if (probepoint->style == PROBEPOINT_SW) {
3759  if (target_change_sw_breakpoint(target,probepoint->thread->tid,
3760  probepoint->mmod,buf,buflen)) {
3761  verror("could not insert action code; action failed!\n");
3762  return -1;
3763  }
3764  }
3765  else {
3766  if (!probepoint->mmod) {
3767  probepoint->mmod = target_memmod_create(target,probepoint->thread->tid,
3768  probepoint->addr,0,MMT_CODE,
3769  buf,buflen,0);
3770  if (!probepoint->mmod) {
3771  verror("could not create memmod for HW breakpoint code"
3772  " at 0x%"PRIxADDR"!\n",probepoint->addr);
3773  return -1;
3774  }
3775  }
3776  else {
3777  verror("could not create memmod for HW breakpoint code"
3778  " at 0x%"PRIxADDR"!\n",probepoint->addr);
3779  return -1;
3780  }
3781  }
3782 
3783  probepoint->state = PROBE_ACTION_RUNNING;
3784 
3785  return 0;
3786 }
3787 
3788 struct action *__get_next_complex_action(struct probepoint *probepoint,
3789  struct action *current) {
3790  struct list_head *head = &probepoint->complex_actions;
3791 
3792  if (list_empty(head))
3793  return NULL;
3794  else if (!current)
3795  return list_entry(head->next,typeof(*current),action);
3796  else if (current->action.next == head)
3797  return NULL;
3798  else
3799  return list_entry(current->action.next,struct action,action);
3800 }
3801 
3802 /*
3803  * This copies code into the breakpoint as necessary and single steps as
3804  * necessary, or sets things up to handle boosted instructions if that's
3805  * what we're doing. It also handles single step events it has set up.
3806  * Basically, anything to do with complex actions, it handles.
3807  *
3808  * It does setup only; it does not resume the target; only its callers
3809  * should do that.
3810  *
3811  * If we setup a complex action, we are going to have to single step at
3812  * least one instruction (and maybe more). If we did not set up an
3813  * action, we have to replace the original instruction by the real thing
3814  * and single step -- OR if it was boostable and target supported it, we
3815  * set EIP to the boosted location, and single step there if necessary,
3816  * and mess with EIP again afterward (or let the boosted instruction JMP
3817  * back to the real instruction following the real boosted
3818  * instruction.and its effects took the place of the original
3819  * instruction (obviated it), enable single stepping if needed.
3820  *
3821  * Returns: <0 on error; 0 if no actions requiring single step were
3822  * setup; 1 if actions requiring single step were setup (and thus single
3823  * step mode was enabled).
3824  */
3825 static int handle_complex_actions(struct target *target,
3826  struct target_thread *tthread,
3827  struct probepoint *probepoint) {
3828  struct action *action,*nextaction = NULL;
3829  struct thread_probepoint_context *tpc;
3830  tid_t tid = tthread->tid;
3831  struct thread_action_context *tac,*ttac;
3832  struct thread_probepoint_context *htpc;
3833 
3834  tpc = tthread->tpc;
3835 
3836  /*
3837  * If we had paused this thread before handling an action because we
3838  * needed to hold the probepoint, but couldn't, jump straight to
3839  * that part.
3840  */
3841  if (tthread->resumeat == THREAD_RESUMEAT_NA) {
3842  action = nextaction = tpc->tac.action;
3843  goto nextaction;
3844  }
3845 
3846  /*
3847  * If we just arrived here after hitting the breakpoint, reset our
3848  * state, and try to start an action.
3849  */
3850  if (probepoint->tpc == tpc
3851  && probepoint->state == PROBE_BP_ACTIONHANDLING) {
3853  "resetting actions state after bp hit at ");
3854  LOGDUMPPROBEPOINT(3,LA_PROBE,LF_ACTION,probepoint);
3855  vdebugc(5,LA_PROBE,LF_ACTION,"\n");
3856 
3857  tpc->tac.action = NULL;
3858  tpc->tac.stepped = 0;
3859  tpc->action_obviated_orig = 0;
3860 
3861  /*
3862  * XXX: this could fail bad if whatever the current action
3863  * points to on the probepoint's list is removed from the list.
3864  * What to do???
3865  */
3866  nextaction = action = __get_next_complex_action(probepoint,NULL);
3867  if (!action) {
3868  vdebug(3,LA_PROBE,LF_ACTION,"no actions to run at ");
3869  LOGDUMPPROBEPOINT(3,LA_PROBE,LF_ACTION,probepoint);
3870  vdebugc(3,LA_PROBE,LF_ACTION,"\n");
3871 
3872  return 0;
3873  }
3874  }
3875  /*
3876  * If we need to keep stepping through this action, keep stepping.
3877  */
3878  else if (tpc->tac.action && tpc->tac.action->steps) {
3879  action = tpc->tac.action;
3880  /*
3881  * Increment the single step count no matter what.
3882  */
3883  ++tpc->tac.stepped;
3884 
3885  if (action->steps < 0 || tpc->tac.stepped < action->steps) {
3887  "did %d steps; still more at ",tpc->tac.stepped);
3888  LOGDUMPPROBEPOINT(5,LA_PROBE,LF_ACTION,probepoint);
3889  vdebugc(5,LA_PROBE,LF_ACTION,"\n");
3890 
3891  if (action->handler)
3892  action->handler(action,tthread,action->probe,probepoint,
3893  MSG_STEPPING,tpc->tac.stepped,
3894  action->handler_data);
3895  }
3896  else {
3898  "finished %d steps; done and removing action at ",
3899  tpc->tac.stepped);
3900  LOGDUMPPROBEPOINT(5,LA_PROBE,LF_ACTION,probepoint);
3901  vdebugc(5,LA_PROBE,LF_ACTION,"\n");
3902  /*
3903  * If we know the action is done because it has finished its
3904  * set amount of single steps, we need to "finish" it:
3905  */
3906  if (action->handler)
3907  action->handler(action,tthread,action->probe,probepoint,
3908  MSG_SUCCESS,tpc->tac.stepped,
3909  action->handler_data);
3910 
3911  __remove_action(target,probepoint,action);
3912 
3913  if (target_singlestep_end(target,tid))
3914  verror("could not stop single stepping target"
3915  " after single stepped action!\n");
3916 
3917  /* Grab the next one before we destroy this one. */
3918  nextaction = __get_next_complex_action(probepoint,action);
3919 
3920  action_finish_handling(action,&tpc->tac);
3921 
3922  action = nextaction;
3923 
3924  if (!nextaction) {
3925  tpc->tac.action = NULL;
3926  tpc->tac.stepped = 0;
3927  }
3928  }
3929  }
3930  /*
3931  * If we have a current action, but don't need single steps, we
3932  * don't want to do *anything* -- because we didn't initiate this
3933  * single step! In future, we could warn the user...
3934  */
3935  else if (tpc->tac.action) {
3936  vwarn("unexpected single step!\n");
3937  return 0;
3938  }
3939  else if (!tpc->tac.action) {
3940  vdebug(5,LA_PROBE,LF_ACTION,"no action being handled at ");
3941  LOGDUMPPROBEPOINT(3,LA_PROBE,LF_ACTION,probepoint);
3942  vdebugc(5,LA_PROBE,LF_ACTION,"\n");
3943  return 0;
3944  }
3945 
3946  nextaction:
3947  if (nextaction) {
3948  /*
3949  * Start this action up!
3950  */
3951 
3952  /* Clean up state, then setup next action if there is one. */
3953 
3954  if (!nextaction->boosted) {
3955  /*
3956  * We need to grab the probepoint before we run this one!
3957  */
3958  if ((htpc = probepoint_hold(target,tthread,probepoint,tpc)) != tpc) {
3959  if (probepoint_pause_handling(target,tthread,probepoint,
3960  THREAD_RESUMEAT_NA)) {
3961  verror("thread %"PRIiTID" hit nextaction when thread %"PRIiTID" already"
3962  " handling it; target does not support thread ctl!\n",
3963  tthread->tid,htpc->thread->tid);
3964  /*
3965  * There literally is nothing we can do -- we cannot
3966  * handle this breakpoint interrupt.
3967  */
3968  return -1;
3969  }
3970  else
3971  return 0;
3972  }
3973 
3974  /*
3975  * If the action requires more than one single step, has
3976  * more than one instruction, and we're in the right BPMODE,
3977  * we have to pause all the other threads.
3978  */
3979  if (target->spec->bpmode == THREAD_BPMODE_STRICT
3980  || (target->spec->bpmode == THREAD_BPMODE_SEMI_STRICT
3981  && (action->steps > 1
3982  || (action->type == ACTION_CUSTOMCODE
3983  && action->detail.code.instr_count > 1)))) {
3984  if (target_pause(target)) {
3985  vwarn("could not pause the target for blocking thread"
3986  " %"PRIiTID"!\n",tthread->tid);
3987  return -1;
3988  }
3989  target->blocking_thread = tthread;
3990  }
3991  else {
3992  if (target->blocking_thread == tthread)
3993  target->blocking_thread = NULL;
3994  }
3995 
3996  probepoint->state = PROBE_ACTION_RUNNING;
3997  }
3998  else {
3999  if (target->blocking_thread == tthread)
4000  target->blocking_thread = NULL;
4001  }
4002 
4003  __insert_action(target,tthread,probepoint,nextaction);
4004 
4005  tpc->tac.action = nextaction;
4006 
4007  tpc->action_obviated_orig |= nextaction->obviates;
4008 
4009  action = nextaction;
4010  }
4011 
4012  /*
4013  * Single step if the current action needs it.
4014  */
4015  if (tpc->tac.action && tpc->tac.action->steps) {
4016  vdebug(4,LA_PROBE,LF_PROBEPOINT,"single step for action at ");
4018  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
4019 
4020  int _isbp = 0;
4021  if (!tpc->tac.action->boosted && !tpc->tac.stepped)
4022  _isbp = 1;
4023 
4024  if (probepoint->style == PROBEPOINT_HW
4025  && _isbp
4026  && !target->nodisablehwbponss) {
4027  /*
4028  * We need to disable the hw breakpoint before we single
4029  * step because the target can't do it.
4030  */
4031  target_disable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
4032  probepoint->debugregdisabled = 1;
4033  }
4034 
4035  if (target_singlestep(target,tid,_isbp) < 0) {
4036  verror("could not keep single stepping target!\n");
4037 
4038  if (probepoint->style == PROBEPOINT_HW
4039  && _isbp
4040  && !target->nodisablehwbponss) {
4041  /*
4042  * Reenable the hw breakpoint we just disabled.
4043  */
4044  target_enable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
4045  probepoint->debugregdisabled = 0;
4046  }
4047 
4048  if (target_singlestep_end(target,tid))
4049  verror("could not stop single stepping target"
4050  " after failed sstep!\n");
4051 
4052  /*
4053  * All we can really do is notify all the single step
4054  * actions that we had an error, and nuke them, and keep
4055  * going.
4056  */
4057  list_for_each_entry_safe(tac,ttac,&tthread->ss_actions,tac) {
4058  if (tac->action->handler)
4059  tac->action->handler(tac->action,tthread,tac->action->probe,
4060  action->probe->probepoint,
4061  MSG_FAILURE,tac->stepped,
4062  tac->action->handler_data);
4063 
4064  action_finish_handling(action,tac);
4065 
4066  list_del(&tac->tac);
4067  free(tac);
4068  }
4069 
4070  return -1;
4071  }
4072  else {
4073  /*
4074  * If single step init succeeded, let the ss handler take
4075  * over.
4076  *
4077  * Don't call target_resume after a successful target_singlestep.
4078  */
4079  vdebug(4,LA_PROBE,LF_PROBEPOINT,"sstep command succeeded for action at ");
4081  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
4082 
4083  return 0;
4084  }
4085 
4086  return 1;
4087  }
4088  else if (tpc->tac.action) {
4089  vdebug(4,LA_PROBE,LF_PROBEPOINT,"NOT single stepping for action at ");
4091  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
4092  }
4093 
4094  return 0;
4095 }
4096 
4097 /*
4098  * Schedules an action to occur, given a probe handler. Actions can
4099  * only occur when a domain is paused after a probe breakpoint has been
4100  * hit. Actions can be scheduled prior to beginning probing, or they
4101  * can be scheduled at probe handler runtime.
4102  *
4103  * Some actions may preclude others in the future. For instance, a
4104  * return action depends greatly on the body of the called function NOT
4105  * being executed at all, so that %eax may be set and a 'ret'
4106  * instruction executed (so it's immediately after the 'call'
4107  * instruction; there is no state to clean up, etc. Allowing a custom
4108  * code action prior to a return action might change the validity of
4109  * this assumption.
4110  *
4111  * But for now, the only restriction is
4112  * - one return action per handle, and it will be performed after
4113  * execution of the pre handler.
4114  *
4115  * Actions do not have priorities; they are executed in the order scheduled.
4116  */
4117 int action_sched(struct probe *probe,struct action *action,
4118  action_whence_t whence,
4119  action_handler_t handler,void *handler_data) {
4120  struct action *lpc;
4121  unsigned char *code;
4122  unsigned int code_len = 0;
4123  struct probepoint *probepoint;
4124  struct target *target;
4125  int i;
4126  struct array_list *idata_list;
4127  struct inst_data *idata;
4128 
4129  if (action->probe != NULL) {
4130  verror("action already associated with probe!\n");
4131  return -1;
4132  }
4133 
4134  if (!probe->probepoint) {
4135  errno = EINVAL;
4136  verror("probe not attached to a probepoint!\n");
4137  return -1;
4138  }
4139 
4140  if (whence != ACTION_ONESHOT && whence != ACTION_REPEATPRE
4141  && whence != ACTION_REPEATPOST) {
4142  verror("unknown whence %d for action\n",whence);
4143  errno = EINVAL;
4144  return -1;
4145  }
4146 
4147  probepoint = probe->probepoint;
4148  target = probepoint->target;
4149 
4150  /*
4151  * If it's boosted, it's already set.
4152  */
4153  if (!action->boosted)
4154  action->start_addr = probepoint->addr;
4155 
4156  if (action->type == ACTION_REGMOD || action->type == ACTION_MEMMOD) {
4157  list_add_tail(&action->action,&probe->probepoint->simple_actions);
4158  }
4159  /* right now you can always enable singlestep actions */
4160  else if (action->type == ACTION_SINGLESTEP) {
4161  list_add_tail(&action->action,&probe->probepoint->ss_actions);
4162  }
4163  /* only allow one return action per probepoint */
4164  else if (action->type == ACTION_RETURN) {
4165  list_for_each_entry(lpc,&probe->probepoint->complex_actions,action) {
4166  if (lpc->type == ACTION_RETURN) {
4167  verror("probepoint already has return action\n");
4168  return -1;
4169  }
4170  else if (lpc->type == ACTION_CUSTOMCODE) {
4171  verror("probepoint already has customcode action\n");
4172  return -1;
4173  }
4174  }
4175 
4176  /* Try to disassemble and setup return from the prologue. */
4177  if (probepoint->symbol_addr
4178  && probepoint->symbol_addr != probepoint->addr) {
4179  if (probepoint->symbol_addr > probepoint->addr) {
4180  vwarn("probepoint symbol addr < probepoint addr -- bad\n");
4181  }
4182  else {
4183  action->detail.ret.prologue = 1;
4184 
4185  /* Read the prologue; if the first byte is 0x55, we're
4186  * using frame pointers and we don't need to analyze the
4187  * prologue to watch the stack grow so we can undo it.
4188  * Otherwise, read the stack growth during the prologue
4189  * so we can undo it during a return.
4190  */
4191  code_len = probepoint->addr - probepoint->symbol_addr;
4192  code = (unsigned char *)malloc(code_len);
4193  memset(code,0,code_len);
4194 
4195  if (!target_read_addr(target,probepoint->symbol_addr,
4196  code_len,code)) {
4197  vwarn("could not read prologue code; skipping disasm!\n");
4198  free(code);
4199  code = NULL;
4200  }
4201  else {
4202  /*
4203  * Check both for first instruction 0x55; any
4204  * instruction 0x55 (to catch functions that callq
4205  * elsewhere as their first instr); and disasm the
4206  * prologue to see %sp delta . Then we can pick
4207  * which method we can use later.
4208  */
4209 
4210  /*
4211  * Don't require the first instruction to be 0x55
4212  * (push %ebp); look for it at any point in the
4213  * function! We could also look for 0xc9 (leave).
4214  */
4215  if (*code == 0x55)
4216  action->detail.ret.prologue_uses_bp = 1;
4217  else if (disasm_generic(target,code,code_len,
4218  &idata_list,1) == 0) {
4219  for (i = 0; i < array_list_len(idata_list); ++i) {
4220  idata = (struct inst_data *) \
4221  array_list_item(idata_list,i);
4222  if (idata->type == 0x55) {
4223  action->detail.ret.prologue_uses_bp = 1;
4224  break;
4225  }
4226  }
4227  if (idata_list)
4228  array_list_deep_free(idata_list);
4229  }
4230  else {
4232  "could not disassemble code in range"
4233  "0x%"PRIxADDR"-0x%"PRIxADDR
4234  " for function %s!\n",
4235  probepoint->symbol_addr,probepoint->addr,
4236  probepoint->bsymbol ? probepoint->bsymbol->lsymbol->symbol->name : "<UNKNOWN>");
4237  }
4238 
4239  if (action->detail.ret.prologue_uses_bp) {
4241  "function %s: pushes EBP (can leave; ret)\n",
4242  probepoint->bsymbol ? probepoint->bsymbol->lsymbol->symbol->name : "<UNKNOWN>");
4243  }
4244 #ifdef ENABLE_DISTORM
4245 
4246  /*
4247  * Ok, now disasm the prologue.
4248  */
4249  if (disasm_get_prologue_stack_size(target,code,code_len,
4250  &action->detail.ret.prologue_sp_offset)) {
4251  verror("could not disassemble function prologue"
4252  " to track stack growth for return action!\n");
4253  }
4254  else {
4255  action->detail.ret.prologue_has_sp_offset = 1;
4256 
4258  "disassembled prologue for function %s: sp moved %d\n",
4259  probepoint->bsymbol ? probepoint->bsymbol->lsymbol->symbol->name : "<UNKNOWN>",
4260  action->detail.ret.prologue_sp_offset);
4261  }
4262 #else
4263  {
4264  verror("disasm support disabled; cannot check prologue"
4265  " to unwind stack frame function %s!\n",
4266  probepoint->bsymbol \
4267  ? probepoint->bsymbol->lsymbol->symbol->name \
4268  : "<UNKNOWN>");
4269  }
4270 #endif
4271 
4272  /* Clean up. */
4273  free(code);
4274  code = NULL;
4275  }
4276  }
4277  }
4278 
4279  /*
4280  * First check is redundant due to check in target_open, but
4281  * in case that goes away, it's here too.
4282  */
4283  if (target->spec->bpmode == THREAD_BPMODE_STRICT && !target->threadctl) {
4284  verror("cannot do return on strict target without threadctl!\n");
4285  errno = ENOTSUP;
4286  return -1;
4287  }
4288  else if (action->detail.ret.prologue && action->detail.ret.prologue_uses_bp
4289  && !action->boosted
4290  && !target->threadctl
4291  && target->arch->full_ret_instr_count > 1
4292  && target->spec->bpmode == THREAD_BPMODE_SEMI_STRICT) {
4293  if (action->detail.ret.prologue_has_sp_offset) {
4295  "cannot use multi-instr full return; but can use SP"
4296  " adjustment and single-instr return\n");
4297 
4298  action->detail.ret.prologue_uses_bp = 0;
4299  }
4300  else {
4301  verror("cannot do non-boosted, multi-instruction return"
4302  " on strict target without threadctl!\n");
4303  errno = ENOTSUP;
4304  return -1;
4305  }
4306  }
4307 
4308  /*
4309  * We do not need to manually boost return instructions; if the
4310  * target supported it and had room, these are added to the
4311  * target when we attach. BUT, return instructions ALWAYS must
4312  * be single stepped, because otherwise we don't know when we're
4313  * done returning (and thus can't call action handlers,
4314  * probepoint post handlers, garbage collect anything, etc).
4315  */
4316 
4317  list_add_tail(&action->action,&probe->probepoint->complex_actions);
4318  }
4319  /* only allow one customcode action per probepoint */
4320  else if (action->type == ACTION_CUSTOMCODE) {
4321 
4322  /*
4323  * For now, we always boost custom code so that it doesn't
4324  * interfere with the handling of the probepoint. So, we
4325  * assemble our code buf according to the flags, copy in the
4326  * user's code, and end the code frag with a special breakpoint
4327  * probepoint so that we know when we're done with the custom
4328  * code.
4329  *
4330  * We have to assemble the code buf before we ask the target for
4331  * space from its boostable memory (if there is any -- there
4332  * might not be, or might not be enough).
4333  */
4334 
4335  list_for_each_entry(lpc,&probe->probepoint->complex_actions,action) {
4336  if (lpc->type == ACTION_RETURN) {
4337  verror("probepoint already has return action\n");
4338  return -1;
4339  }
4340  else if (lpc->type == ACTION_CUSTOMCODE) {
4341  verror("probepoint already has customcode action\n");
4342  return -1;
4343  }
4344  }
4345 
4346  /* XXX: this check should really let 1-instr actions go through */
4347  if ((target->spec->bpmode == THREAD_BPMODE_STRICT
4348  || target->spec->bpmode == THREAD_BPMODE_SEMI_STRICT)
4349  && !action->boosted
4350  && !target->threadctl) {
4351  verror("cannot do return on strict target without threadctl!\n");
4352  errno = ENOTSUP;
4353  return -1;
4354  }
4355 
4356  list_add_tail(&action->action,&probe->probepoint->complex_actions);
4357  }
4358 
4359  /*
4360  * Ok, we're safe; memo-ize all the sched info into the action struct.
4361  */
4362  action->whence = whence;
4363 
4364  action->handler = handler;
4365  action->handler_data = handler_data;
4366 
4367  action->probe = probe;
4368 
4369  target_attach_action(target,action);
4370 
4371  RHOLD(action,probe);
4372 
4373  return 0;
4374 }
4375 
4376 /*
4377 void __action_cancel_in_thread(struct target_thread *tthread,
4378  struct action *action) {
4379  int i;
4380  struct thread_probepoint_context *tpc;
4381 
4382  if (!array_list_len(tthread->tpc_stack))
4383  return;
4384 
4385  for (i = 0; i < array_list_len(tthread->tpc_stack); ++i) {
4386  tpc = (struct thread_probepoint_context *) \
4387  array_list_item(tthread->tpc_stack,i);
4388 
4389  if (tpc->tac.action == action) {
4390  if (!action->isboosted)
4391  vwarn("canceling a non-boosted action running in"
4392  " thread %"PRIiTID"; badness!!!\n",tthread->tid);
4393 
4394  // We have to mark it as canceled
4395  tpc->tac.canceled = 1;
4396  tpc->tac.nextaction = \
4397  __get_next_complex_action(action->probe->probepoint,action);
4398 
4399  action_release(action);
4400  }
4401  }
4402 
4403  // XXX: but we still have the problem of what happens when an
4404  // action still has code at the breakpoint
4405 }
4406 */
4407 
4408 /*
4409  * Cancel an action.
4410  */
4411 int action_cancel(struct action *action) {
4412  REFCNT trefcnt;
4413 
4414  if (!action->probe) {
4415  verror("cannot cancel action not associated with a probe!\n");
4416  return 1;
4417  }
4418 
4419  /*
4420  * XXX: this is bad, but correct. We have to check all threads and
4421  * make sure that this action is not the one they are currently
4422  * executing; if so, we have to go mark the
4423  * thread_probepoint_context that was holding it as deleted, and
4424  * "release" the action manually. I guess this means that actions
4425  * don't really need refcnting...
4426  */
4427  /*
4428  target = action->probe->probepoint->target;
4429 
4430 
4431  if (action->probe
4432  && action->probe->probepoint
4433  && action->probe->probepoint->action == action) {
4434  verror("cannot cancel a currently-running action!\n");
4435  return 1;
4436  }
4437  */
4438 
4439  action->handler = NULL;
4440  action->handler_data = NULL;
4441 
4442  list_del(&action->action);
4443  action->probe = NULL;
4444 
4445  target_detach_action(action->target,action);
4446 
4447  RPUT(action,action,probe->probepoint,trefcnt);
4448 
4449  return 0;
4450 }
4451 
4452 /*
4453  * High-level actions that require little ASM knowledge.
4454  */
4455 struct action *action_return(REGVAL retval) {
4456  struct action *action;
4457 
4458  action = (struct action *)calloc(1,sizeof(struct action));
4459  if (!action) {
4460  verror("could not malloc action: %s\n",strerror(errno));
4461  return NULL;
4462  }
4463 
4464  action->type = ACTION_RETURN;
4465  action->whence = ACTION_UNSCHED;
4466  INIT_LIST_HEAD(&action->action);
4467 
4468  action->detail.ret.retval = retval;
4469 
4470  action->obviates = 1;
4471 
4472  RHOLD(action,action);
4473 
4474  return action;
4475 }
4476 
4477 /*
4478  * Low-level actions that require little ASM knowledge, and may or may
4479  * not be permitted.
4480  */
4481 struct action *action_singlestep(int nsteps) {
4482  struct action *action;
4483 
4484  if (nsteps == 0 || (nsteps < 0 && nsteps != SINGLESTEP_INFINITE
4485  && nsteps != SINGLESTEP_NEXTBP)) {
4486  errno = EINVAL;
4487  return NULL;
4488  }
4489 
4490  action = (struct action *)calloc(1,sizeof(struct action));
4491  if (!action) {
4492  verror("could not malloc action: %s\n",strerror(errno));
4493  return NULL;
4494  }
4495 
4496  action->type = ACTION_SINGLESTEP;
4497  action->whence = ACTION_UNSCHED;
4498  INIT_LIST_HEAD(&action->action);
4499 
4500  action->steps = nsteps;
4501 
4502  RHOLD(action,action);
4503 
4504  return action;
4505 }
4506 
4507 struct action *action_code(char *buf,uint32_t buflen,action_flag_t flags) {
4508  struct action *action;
4509 
4510  action = (struct action *)calloc(1,sizeof(struct action));
4511  if (!action) {
4512  verror("could not malloc action: %s\n",strerror(errno));
4513  return NULL;
4514  }
4515 
4516  action->type = ACTION_CUSTOMCODE;
4517  action->whence = ACTION_UNSCHED;
4518  INIT_LIST_HEAD(&action->action);
4519 
4520  action->detail.code.buf = malloc(buflen);
4521  memcpy(action->detail.code.buf,buf,buflen);
4522  action->detail.code.buflen = buflen;
4523  action->detail.code.flags = flags;
4524 
4525  RHOLD(action,action);
4526 
4527  return action;
4528 }
4529 
4531  struct action *action;
4532 
4533  action = (struct action *)calloc(1,sizeof(struct action));
4534  if (!action) {
4535  verror("could not malloc action: %s\n",strerror(errno));
4536  return NULL;
4537  }
4538 
4539  action->type = ACTION_REGMOD;
4540  action->whence = ACTION_UNSCHED;
4541  INIT_LIST_HEAD(&action->action);
4542 
4543  action->detail.regmod.regnum = regnum;
4544  action->detail.regmod.regval = regval;
4545 
4546  RHOLD(action,action);
4547 
4548  return action;
4549 }
4550 
4551 struct action *action_memmod(ADDR dest,char *data,uint32_t len) {
4552  struct action *action;
4553 
4554  action = (struct action *)calloc(1,sizeof(struct action));
4555  if (!action) {
4556  verror("could not malloc action: %s\n",strerror(errno));
4557  return NULL;
4558  }
4559 
4560  action->type = ACTION_MEMMOD;
4561  action->whence = ACTION_UNSCHED;
4562  INIT_LIST_HEAD(&action->action);
4563 
4564  action->detail.memmod.destaddr = dest;
4565  action->detail.memmod.data = malloc(len);
4566  memcpy(action->detail.memmod.data,data,len);
4567  action->detail.memmod.len = len;
4568 
4569  RHOLD(action,action);
4570 
4571  return action;
4572 }
4573 
4574 REFCNT action_release(struct action *action) {
4575  REFCNT refcnt;
4576  RPUT(action,action,action,refcnt);
4577  return refcnt;
4578 }
4579 
4580 static void action_finish_handling(struct action *action,
4581  struct thread_action_context *tac) {
4582  REFCNT trefcnt;
4583 
4584  /*
4585  * If the action is oneshot, cancel it.
4586  */
4587  if (action->whence == ACTION_ONESHOT) {
4588  if (tac)
4589  RPUT(action,action,tac,trefcnt);
4590 
4591  action_cancel(action);
4592  }
4593 }
4594 
4595 /*
4596  * Destroy an action (and cancel it first if necessary!).
4597  */
4598 REFCNT action_free(struct action *action,int force) {
4599  int retval = action->refcnt;
4600 
4601  if (action->probe) {
4602  if (action_cancel(action)) {
4603  verror("could not cancel action; cannot destroy!\n");
4604  return 1;
4605  }
4606  }
4607 
4608  if (retval) {
4609  if (!force) {
4610  vwarn("cannot free action (%d refs)!\n",retval);
4611  return retval;
4612  }
4613  else {
4614  verror("forced free action (%d refs)\n",retval);
4615  }
4616  }
4617 
4618  if (action->type == ACTION_CUSTOMCODE
4619  && action->detail.code.buf) {
4620  free(action->detail.code.buf);
4621  }
4622  else if (action->type == ACTION_MEMMOD
4623  && action->detail.memmod.len) {
4624  free(action->detail.memmod.data);
4625  }
4626 
4627  free(action);
4628 
4629  return retval;
4630 }
action_handler_t handler
Definition: probe.h:437
#define LOGDUMPPROBEPOINT(dl, la, lt, pp)
Definition: probe.h:35
#define LOCATION_ADDR(loc)
Definition: dwdebug_priv.h:622
struct memrange * range
Definition: probe.h:252
unsigned int ret_instrs_len
Definition: arch.h:157
tid_t probe_tid(struct probe *probe)
Definition: probe.c:1947
struct action * action_return(REGVAL retval)
Definition: probe.c:4455
#define vwarnopt(level, area, flags, format,...)
Definition: log.h:37
result_t pre_handler(struct probe *probe, tid_t tid, void *data, struct probe *trigger, struct probe *base)
Definition: spf.c:849
int target_change_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod, unsigned char *code, unsigned long code_len)
Definition: target_api.c:1735
int disasm_generic(struct target *target, unsigned char *inst_buf, unsigned int buf_len, struct array_list **idata_list_saveptr, int noabort)
Definition: disasm.c:47
struct target * base
Definition: target_api.h:2614
int probe_unregister(struct probe *probe, int force)
Definition: probe.c:1137
struct bsymbol * target_lookup_sym_line(struct target *target, char *filename, int line, SMOFFSET *offset, ADDR *addr)
Definition: target.c:2237
int probe_unregister_source(struct probe *sink, struct probe *src, int force)
Definition: probe.c:1145
probepoint_style_t probe_style(struct probe *probe)
Definition: probe.c:1975
int can_switch_context
Definition: probe.h:305
thread_bpmode_t bpmode
Definition: target_api.h:2174
#define PROBE_SAFE_OP(probe, op)
Definition: probe.h:28
struct lsymbol * lsymbol
Definition: target.h:1006
probe_handler_t pre_handler
Definition: probe.h:350
int32_t tid_t
Definition: common.h:36
int probe_unregister_batch(struct target *target, struct probe **probelist, int listlen, int force)
Definition: probe.c:1217
struct probe_ops * ops
Definition: probe.h:316
struct list_head * next
Definition: list.h:52
probepoint_type_t
Definition: probe_api.h:213
REFCNT action_free(struct action *action, int force)
Definition: probe.c:4598
ADDR start_addr
Definition: probe.h:441
target_status_t
Definition: target_api.h:197
int target_insert_probepoint(struct target *target, struct target_thread *tthread, struct probepoint *probepoint)
Definition: target.c:4619
result_t probepoint_ss_handler(struct target *target, struct target_thread *tthread, struct probepoint *probepoint)
Definition: probe.c:3021
#define SINGLESTEP_INFINITE
Definition: probe_api.h:871
uint32_t no_adjust_bp_ip
Definition: target_api.h:2427
struct symbol * symbol
Definition: dwdebug.h:1010
struct probe * __probe_register_addr(struct probe *probe, ADDR addr, struct memrange *range, probepoint_type_t type, probepoint_style_t style, probepoint_whence_t whence, probepoint_watchsize_t watchsize, struct bsymbol *bsymbol, ADDR symbol_addr)
Definition: probe.c:1250
action_whence_t
Definition: probe_api.h:257
probepoint_style_t style
Definition: probe.h:223
struct bsymbol * probe_symbol(struct probe *probe)
Definition: probe.c:1939
int target_detach_probe(struct target *target, struct probe *probe)
Definition: target.c:4681
Definition: probe.h:392
Definition: log.h:190
int target_lsymbol_resolve_bounds(struct target *target, struct target_location_ctxt *tlctxt, struct lsymbol *lsymbol, ADDR base_addr, ADDR *start, ADDR *end, int *is_noncontiguous, ADDR *alt_start, ADDR *alt_end)
Definition: target.c:2387
int target_set_hw_watchpoint(struct target *target, tid_t tid, REG reg, ADDR addr, probepoint_whence_t whence, int watchsize)
Definition: target_api.c:1778
GList * sinks
Definition: probe.h:387
char * name
Definition: probe.h:314
struct target_nv_filter * post_filter
Definition: probe.h:355
#define PRIiREG
Definition: common.h:94
probepoint_state_t state
Definition: probe.h:220
thread_resumeat_t
Definition: target_api.h:2032
struct target_thread * thread
Definition: probe.h:206
int target_remove_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1695
ADDR addr
Definition: probe.h:218
REFCNT action_release(struct action *action)
Definition: probe.c:4574
struct probepoint * target_lookup_probepoint(struct target *target, struct target_thread *tthread, ADDR addr)
Definition: target.c:4594
REGVAL retval
Definition: probe.h:411
void location_internal_free(struct location *location)
Definition: location.c:347
struct target_location_ctxt * target_location_ctxt_create_from_bsymbol(struct target *target, tid_t tid, struct bsymbol *bsymbol)
Definition: target.c:5269
probepoint_whence_t
Definition: probe_api.h:234
int obviates
Definition: probe.h:435
int probe_is_base(struct probe *probe)
Definition: probe.c:1919
struct target * target
Definition: probe.h:241
#define SYMBOL_IS_FULL(sym)
uint32_t buflen
Definition: probe.h:419
int target_pause(struct target *target)
Definition: target_api.c:988
struct target_memmod * target_memmod_create(struct target *target, tid_t tid, ADDR addr, int is_phys, target_memmod_type_t mmt, unsigned char *code, unsigned int code_len, int nowrite)
Definition: target.c:4726
struct action * __get_next_complex_action(struct probepoint *probepoint, struct action *current)
Definition: probe.c:3788
result_t probe_do_sink_post_handlers(struct probe *probe, tid_t tid, void *handler_data, struct probe *trigger, struct probe *base)
Definition: probe.c:109
char * bsymbol_get_name(struct bsymbol *bsymbol)
Definition: symbol.c:62
int target_unchange_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1749
int probe_register_batch(struct target *target, tid_t tid, ADDR *addrlist, int listlen, probepoint_type_t type, probepoint_style_t style, probepoint_whence_t whence, probepoint_watchsize_t watchsize, probe_handler_t pre_handler, probe_handler_t post_handler, void *handler_data, struct probe **probelist, int failureaction)
Definition: probe.c:1685
int target_remove_probepoint(struct target *target, struct target_thread *tthread, struct probepoint *probepoint)
Definition: target.c:4645
ADDR start
Definition: target.h:983
int target_detach_action(struct target *target, struct action *action)
Definition: target.c:4701
struct list_head probe
Definition: probe.h:379
#define verror(format,...)
Definition: log.h:30
struct action * action
Definition: probe.h:198
struct target_memmod * mmod
Definition: probe.h:275
result_t probe_do_sink_pre_handlers(struct probe *probe, tid_t tid, void *handler_data, struct probe *trigger, struct probe *base)
Definition: probe.c:48
unsigned char * target_read_addr(struct target *target, ADDR addr, unsigned long length, unsigned char *buf)
Definition: target_api.c:1014
struct probe * probe_register_addr(struct probe *probe, ADDR addr, probepoint_type_t type, probepoint_style_t style, probepoint_whence_t whence, probepoint_watchsize_t watchsize, struct bsymbol *bsymbol)
Definition: probe.c:1393
Definition: list.h:51
int target_singlestep(struct target *target, tid_t tid, int isbp)
Definition: target_api.c:1838
struct symbol * symbol_get_datatype(struct symbol *symbol)
Definition: debug.c:2989
int target_find_memory_real(struct target *target, ADDR addr, struct addrspace **space_saveptr, struct memregion **region_saveptr, struct memrange **range_saveptr)
Definition: target.c:3536
int target_is_open(struct target *target)
Definition: target_api.c:1003
struct action * action_memmod(ADDR dest, char *data, uint32_t len)
Definition: probe.c:4551
void probepoint_free_ext(struct probepoint *probepoint)
Definition: probe.c:570
uint8_t * ret_instrs
Definition: arch.h:156
result_t(* probe_handler_t)(struct probe *probe, tid_t tid, void *handler_data, struct probe *trigger, struct probe *base)
Definition: probe_api.h:70
action_flag_t flags
Definition: probe.h:420
symbol_type_t type
Definition: dwdebug_priv.h:833
REGVAL regval
Definition: probe.h:425
REFCNT refcnt
Definition: probe.h:404
union action::@17 detail
struct list_head ss_actions
Definition: probe.h:270
int doit
Definition: dumptarget.c:58
probepoint_whence_t probe_whence(struct probe *probe)
Definition: probe.c:1982
void *(* summarize_tid)(struct probe *probe, tid_t tid)
Definition: probe_api.h:114
int probe_unregister_one(struct probe *probe, int force)
Definition: probe.c:1141
#define vwarn(format,...)
Definition: log.h:33
uint32_t threadctl
Definition: target_api.h:2427
struct target_thread * thread
Definition: probe.h:242
REGVAL target_read_reg(struct target *target, tid_t tid, REG reg)
Definition: target_api.c:1083
result_t probepoint_bp_handler(struct target *target, struct target_thread *tthread, struct probepoint *probepoint, int was_stepping)
Definition: probe.c:2593
ADDR probe_addr(struct probe *probe)
Definition: probe.c:1959
#define LOGDUMPPROBE(dl, la, lt, p)
Definition: probe.h:54
REG target_get_unused_debug_reg(struct target *target, tid_t tid)
Definition: target_api.c:1757
#define SYMBOL_IS_LABEL(sym)
int(* instr_can_switch_context)(struct target *target, ADDR addr)
Definition: target_api.h:3081
tid_t tid
Definition: probe.h:344
int target_singlestep_end(struct target *target, tid_t tid)
Definition: target_api.c:1844
struct probe * probe_register_source(struct probe *sink, struct probe *src)
Definition: probe.c:1593
struct target * target
Definition: probe.h:452
char * lsymbol_get_name(struct lsymbol *lsymbol)
Definition: debug.c:4732
#define list_for_each_entry(pos, head, member)
Definition: list.h:335
unsigned char * data
Definition: probe.h:429
probepoint_watchsize_t probepoint_closest_watchsize(int size)
Definition: probe.c:1769
struct action * action_regmod(REG regnum, REGVAL regval)
Definition: probe.c:4530
struct probepoint * interrupted_ss_probepoint
Definition: target_api.h:2145
int probe_unregister_source_one(struct probe *sink, struct probe *src, int force)
Definition: probe.c:1186
REFCNT bsymbol_release(struct bsymbol *bsymbol)
Definition: symbol.c:90
struct action::@17::@21 memmod
result_t(* action_handler_t)(struct action *action, struct target_thread *thread, struct probe *probe, struct probepoint *probepoint, handler_msg_t msg, int msg_detail, void *handler_data)
Definition: probe_api.h:76
int probe_hard_enable(struct probe *probe)
Definition: probe.c:961
int target_enable_hw_breakpoint(struct target *target, tid_t tid, REG dreg)
Definition: target_api.c:1819
int debugregdisabled
Definition: probe.h:300
probepoint_watchsize_t
Definition: probe_api.h:241
int probe_free(struct probe *probe, int force)
Definition: probe.c:777
int probe_disable_one(struct probe *probe)
Definition: probe.c:1800
action_whence_t whence
Definition: probe.h:407
struct action::@17::@19 code
void * probe_summarize_tid(struct probe *probe, tid_t tid)
Definition: probe.c:1793
void * probe_priv(struct probe *probe)
Definition: probe.c:1951
#define SYMBOL_IS_BLOCK(sym)
char * name
Definition: dwdebug_priv.h:788
struct list_head complex_actions
Definition: probe.h:269
#define LOGDUMPPROBE_NL(dl, la, lt, p)
Definition: probe.h:73
struct list_head tac
Definition: probe.h:202
struct action * action_singlestep(int nsteps)
Definition: probe.c:4481
struct action::@17::@20 regmod
probepoint_type_t probe_type(struct probe *probe)
Definition: probe.c:1968
#define LOGDUMPPROBEPOINT_NL(dl, la, lt, p)
Definition: probe.h:50
int probe_enable(struct probe *probe)
Definition: probe.c:1861
int len
Definition: dumptarget.c:52
#define RHOLD(x, hx)
Definition: common.h:622
#define PROBE_SAFE_OP_ARGS(probe, op,...)
Definition: probe.h:31
int probe_num_sources(struct probe *probe)
Definition: probe.c:1923
void * probe_summarize(struct probe *probe)
Definition: probe.c:1786
Definition: probe.h:308
struct thread_action_context tac
Definition: probe.h:210
int probe_enable_one(struct probe *probe)
Definition: probe.c:1849
struct probe * probe_create(struct target *target, tid_t tid, struct probe_ops *pops, const char *name, probe_handler_t pre_handler, probe_handler_t post_handler, void *handler_data, int autofree, int tracked)
Definition: probe.c:729
struct target * probe_target(struct probe *probe)
Definition: probe.c:1943
struct target_thread * thread
Definition: probe.h:343
GHashTable * values
Definition: probe.h:330
struct array_list * tpc_stack
Definition: target_api.h:2132
int target_attach_probe(struct target *target, struct target_thread *thread, struct probe *probe)
Definition: target.c:4669
#define vdebug(devel, areas, flags, format,...)
Definition: log.h:302
unsigned int full_ret_instrs_len
Definition: arch.h:161
struct thread_probepoint_context * tpc
Definition: target_api.h:2131
handler_msg_t
Definition: probe_api.h:52
struct thread_probepoint_context * tpc
Definition: probe.h:294
#define vdebugc(devel, areas, flags, format,...)
Definition: log.h:303
struct arch * arch
Definition: target_api.h:2563
result_t probepoint_resumeat_handler(struct target *target, struct target_thread *tthread)
Definition: probe.c:3502
struct target_nv_filter * pre_filter
Definition: probe.h:351
result_t post_handler(struct probe *probe, tid_t tid, void *data, struct probe *trigger, struct probe *base)
Definition: spf.c:854
struct target * target
Definition: target_api.h:2041
#define list_for_each_entry_safe(pos, n, head, member)
Definition: list.h:387
Definition: log.h:70
int probe_disable(struct probe *probe)
Definition: probe.c:1811
void *(* summarize)(struct probe *probe)
Definition: probe_api.h:112
struct action * action_code(char *buf, uint32_t buflen, action_flag_t flags)
Definition: probe.c:4507
result_t
Definition: common.h:25
action_flag_t
Definition: probe_api.h:267
int action_cancel(struct action *action)
Definition: probe.c:4411
uint32_t REGVAL
Definition: common.h:66
uint8_t enabled
Definition: probe.h:362
int steps
Definition: probe.h:440
struct list_head ss_actions
Definition: target_api.h:2156
int target_memmod_release(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target.c:4877
Definition: log.h:71
int(* probe_register_symbol)(struct target *target, tid_t tid, struct probe *probe, struct bsymbol *bsymbol, probepoint_style_t style, probepoint_whence_t whence, probepoint_watchsize_t watchsize)
Definition: target_api.h:3040
int target_attach_action(struct target *target, struct action *action)
Definition: target.c:4692
unsigned int symbol_type_full_bytesize(struct symbol *type)
Definition: debug.c:4267
target_status_t target_status(struct target *target)
Definition: target_api.c:1007
int target_disable_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1721
#define PRIiTID
Definition: common.h:37
uint8_t autofree
Definition: probe.h:364
int8_t REG
Definition: common.h:93
int target_disable_hw_breakpoint(struct target *target, tid_t tid, REG dreg)
Definition: target_api.c:1812
probepoint_style_t
Definition: probe_api.h:228
unsigned int breakpoint_instrs_len
Definition: arch.h:150
struct target * target
Definition: probe.h:342
void target_nv_filter_free(struct target_nv_filter *pf)
struct list_head action
Definition: probe.h:449
uint32_t ADDR
Definition: common.h:64
void probepoint_release(struct target *target, struct target_thread *tthread, struct probepoint *probepoint)
Definition: probe.c:2232
struct target_ops * ops
Definition: target_api.h:2510
struct target_memmod * target_insert_sw_breakpoint(struct target *target, tid_t tid, ADDR addr)
Definition: target_api.c:1664
void tpc_free(struct thread_probepoint_context *tpc)
Definition: probe.c:2279
REG spregno
Definition: target_api.h:2469
struct thread_probepoint_context * probepoint_hold(struct target *target, struct target_thread *tthread, struct probepoint *probepoint, struct thread_probepoint_context *tpc)
Definition: probe.c:2239
target_status_t status
Definition: target_api.h:2465
ADDR symbol_addr
Definition: probe.h:249
int target_unset_hw_breakpoint(struct target *target, tid_t tid, REG reg)
Definition: target_api.c:1786
uint8_t * full_ret_instrs
Definition: arch.h:160
struct bsymbol * bsymbol
Definition: probe.h:245
probepoint_watchsize_t watchsize
Definition: probe.h:232
REG fbregno
Definition: target_api.h:2468
uint32_t REFCNT
Definition: common.h:124
#define PRIxADDR
Definition: common.h:67
dis_inst_t type
Definition: disasm.h:36
void target_location_ctxt_free(struct target_location_ctxt *tlctxt)
Definition: target.c:5292
#define list_entry(ptr, type, member)
Definition: list.h:300
char * SYMBOL_TYPE(int n)
Definition: debug.c:5662
struct target_spec * spec
Definition: target_api.h:2565
int probe_filter_check(struct probe *probe, tid_t tid, struct probe *trigger, int whence)
Definition: probe_filter.c:35
struct bsymbol * bsymbol
Definition: probe.h:389
int probe_enabled(struct probe *probe)
Definition: probe.c:1915
struct probe * probe_register_sources(struct probe *sink, struct probe *src,...)
Definition: probe.c:1654
int boosted
Definition: probe.h:434
uint32_t nodisablehwbponss
Definition: target_api.h:2427
int disasm_get_prologue_stack_size(struct target *target, unsigned char *inst_buf, unsigned int buf_len, int *sp)
Definition: disasm.c:409
struct probe * probe_register_line(struct probe *probe, char *filename, int line, probepoint_style_t style, probepoint_whence_t whence, probepoint_watchsize_t watchsize)
Definition: probe.c:1403
#define RPUT(x, objtype, hx, rc)
Definition: common.h:624
REG debugregnum
Definition: probe.h:299
int id
Definition: probe.h:312
REG regnum
Definition: probe.h:424
int target_set_hw_breakpoint(struct target *target, tid_t tid, REG reg, ADDR addr)
Definition: target_api.c:1771
GList * sources
Definition: probe.h:383
int id
Definition: target_api.h:2476
void probe_rename(struct probe *probe, const char *name)
Definition: probe.c:898
void * handler_data
Definition: probe.h:438
struct action::@17::@18 ret
struct list_head simple_actions
Definition: probe.h:268
struct target_thread * target_lookup_thread(struct target *target, tid_t tid)
Definition: target.c:3981
struct list_head probes
Definition: probe.h:255
unsigned long target_write_addr(struct target *target, ADDR addr, unsigned long length, unsigned char *buf)
Definition: target_api.c:1021
struct target_thread * blocking_thread
Definition: target_api.h:2653
void * handler_data
Definition: probe.h:359
char * probe_name(struct probe *probe)
Definition: probe.c:1935
result_t probepoint_interrupted_ss_handler(struct target *target, struct target_thread *tthread, struct probepoint *probepoint)
Definition: probe.c:3357
int8_t resumeat
Definition: target_api.h:2044
void * priv
Definition: probe.h:318
struct probe * probe_register_symbol(struct probe *probe, struct bsymbol *bsymbol, probepoint_style_t style, probepoint_whence_t whence, probepoint_watchsize_t watchsize)
Definition: probe.c:1470
#define INIT_LIST_HEAD(ptr)
Definition: list.h:60
uint8_t tracked
Definition: probe.h:376
int target_enable_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1708
#define SYMBOL_IS_FUNC(sym)
int target_unset_hw_watchpoint(struct target *target, tid_t tid, REG reg)
Definition: target_api.c:1793
int probepoint_pause_handling(struct target *target, struct target_thread *tthread, struct probepoint *probepoint, thread_resumeat_t resumeat)
Definition: probe.c:2252
probepoint_style_t orig_style
Definition: probe.h:230
int action_sched(struct probe *probe, struct action *action, action_whence_t whence, action_handler_t handler, void *handler_data)
Definition: probe.c:4117
REG ipregno
Definition: target_api.h:2470
int target_write_reg(struct target *target, tid_t tid, REG reg, REGVAL value)
Definition: target_api.c:1092
struct probe * probe
Definition: probe.h:450
action_type_t type
Definition: probe.h:406
loctype_t
Definition: dwdebug.h:234
probe_handler_t post_handler
Definition: probe.h:354
#define TID_GLOBAL
Definition: target_api.h:145
probepoint_type_t type
Definition: probe.h:222
#define SYMBOL_IS_VAR(sym)
Definition: log.h:192
struct probepoint * probepoint
Definition: probe.h:347
loctype_t target_lsymbol_resolve_location(struct target *target, struct target_location_ctxt *tlctxt, struct lsymbol *lsymbol, ADDR base_addr, load_flags_t flags, struct location *o_loc, struct symbol **o_datatype, struct memrange **o_range)
Definition: target.c:2421
#define SINGLESTEP_NEXTBP
Definition: probe_api.h:872
struct probepoint * probepoint
Definition: probe.h:207
unsigned int full_ret_instr_count
Definition: arch.h:162
int probe_hard_disable(struct probe *probe, int force)
Definition: probe.c:912
#define PRIxREGVAL
Definition: common.h:72
probepoint_whence_t whence
Definition: probe.h:231
int probe_num_sinks(struct probe *probe)
Definition: probe.c:1929
unsigned int ret_instr_count
Definition: arch.h:158