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, 2015 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  if (target_read_reg(target,tid,target->ipregno) != ipval) {
2844  target_write_reg(target,tid,target->ipregno,ipval);
2845  if (errno) {
2846  verror("could not reset EIP after pre handlers!\n");
2847  probepoint->state = PROBE_BP_SET;
2848  probepoint_release(target,tthread,probepoint);
2849  tpc_free(tthread->tpc);
2850  tthread->tpc = (struct thread_probepoint_context *) \
2851  array_list_remove(tthread->tpc_stack);
2852  return RESULT_ERROR;
2853  }
2854  }
2855 
2857  "ip 0x%"PRIxREGVAL" restored after pre handlers at ",ipval);
2859  vdebugc(9,LA_PROBE,LF_PROBEPOINT,"\n");
2860  }
2861 
2862  /* Now we're handling actions. */
2863  probepoint->state = PROBE_BP_ACTIONHANDLING;
2864 
2865  /*
2866  * Run the simple actions now.
2867  */
2868  if ((rc = handle_simple_actions(target,tthread,probepoint)))
2869  vwarn("failed to handle %d simple actions!\n",rc);
2870 
2871  /*
2872  * Set up complex actions. The output of the setup is what code we
2873  * need to place at the breakpoint site; the min single steps before
2874  * we can replace that code with 1) the original code, if the tmp
2875  * code does not replace the orig; or 2) the breakpoint again, if
2876  * the tmp code *does* replace the orig. Or, it could be nothing.
2877  *
2878  * (If the complex action requires more than one single step, we
2879  * cannot support it unless the target is capable of pausing all
2880  * other threads (i.e., ensuring that the current thread cannot be
2881  * preempted). We need this because we can basically assume that
2882  * the first single step will not be "interrupted" after a
2883  * breakpoint unless an interrupt happened to fire at the same time
2884  * as the breakpoint. But, we can't assume that N single steps
2885  * won't be interrupted; if they are, the probepoint is storing
2886  * per-thread context, and it could get messed up bad. Plus, the
2887  * probepoint might have arbitrary code at its location, instead of
2888  * the breakpoint or original instruction.)
2889  *
2890  * If the output is nothing (i.e., no complex actions), we decide if
2891  * we need to single step (always do for SW; if HW, and no probes
2892  * are enabled, or none of the enalbed probes have posthandlers, we
2893  * can skip the SW breakpoint; OR ALSO do if we have one or more
2894  * single step actions).
2895  *
2896  * Then in the ss handler, we have to check what state we're in.
2897  * First, if we are called with no probepoint, we must be sstep'ing
2898  * for an sstep action(s). Otherwise, if probepoint->action is set,
2899  * we must be sstep'ing a complex action (or on behalf of it).
2900  * Otherwise, we must just be sstep'ing the original instr under the
2901  * breakpoint.
2902  *
2903  * In the first case, just hanlde the ss_actions list.
2904  *
2905  * In the second case, continue sstep'ing if the action requires it;
2906  * otherwise, either setup another complex action, or replace the
2907  * underlying code if the action didn't obviate it -- and single
2908  * step the orig code. Then put the breakpoint back in.
2909  *
2910  * In the third case, just put the breakpoint back in.
2911  *
2912  * In all cases, leave single step enabled if there are still
2913  * ss_actions running for the thread.
2914  */
2915  if ((stepping = handle_complex_actions(target,tthread,probepoint)) < 0)
2916  vwarn("failed to handle %d complex actions!\n",rc);
2917 
2918  /*
2919  * If there is a complex action, just setup single step actions (and
2920  * we might already be single stepping for the complex action).
2921  * handle_complex_actions also paused all other threads if we needed to.
2922  */
2923  if (tpc->tac.action) {
2924  vdebug(4,LA_PROBE,LF_PROBEPOINT,"setup complex action for ");
2926  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2927 
2928  int _isbp = 0;
2929  if (!tpc->tac.action->boosted)
2930  _isbp = 1;
2931 
2932  rc = setup_single_step_actions(target,tthread,probepoint,_isbp,stepping);
2933  if (rc == 1)
2934  rc = 0;
2935  }
2936  /*
2937  * Otherwise, if we did not perform a complex action, or if the
2938  * complex action did not require any single steps, figure out if we
2939  * should re-execute the original instructions -- and do we boost
2940  * them or replace them at the breakpoint and single step it.
2941  */
2942  else {
2943  tpc->did_orig_instr = 1;
2944 
2945  vdebug(4,LA_PROBE,LF_PROBEPOINT,"setting up post handling single step for ");
2947  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
2948  rc = setup_post_single_step(target,tthread,probepoint);
2949 
2950  if (rc == 0) {
2951  /*
2952  * No single step scheduled, but no error, so we're done
2953  * handling the probepoint in this thread; pop the stack.
2954  */
2955  if (target->blocking_thread == tthread)
2956  target->blocking_thread = NULL;
2957  probepoint_release(target,tthread,probepoint);
2958  tpc_free(tthread->tpc);
2959  tthread->tpc = (struct thread_probepoint_context *) \
2960  array_list_remove(tthread->tpc_stack);
2961 
2963  "thread %"PRIiTID" skipped orig instruction; clearing tpc!\n",
2964  tthread->tid);
2965 
2966  /*
2967  * We need to setup single step actions to happen because we
2968  * are not stepping yet, and we might need to.
2969  */
2970  rc = setup_single_step_actions(target,tthread,probepoint,1,0);
2971  if (rc == 1) {
2972  /*
2973  * Don't call target_resume; we've already called
2974  * target_singlestep.
2975  */
2976  return RESULT_SUCCESS;
2977  }
2978  }
2979  else if (rc == 1) {
2980  /*
2981  * If we need to keep single stepping, just let that happen
2982  * -- it's already been set up (or might have been setup in
2983  * setup_post_single_step).
2984  *
2985  * We need to setup single step actions to happen even if
2986  * we're already stepping (bookkeeping).
2987  */
2988  rc = setup_single_step_actions(target,tthread,probepoint,1,1);
2989 
2990  /*
2991  * Don't call target_resume; we've already called
2992  * target_singlestep.
2993  */
2994  return RESULT_SUCCESS;
2995  }
2996  else if (rc == 2) {
2997  /* The thread blocked because it could not hold the probepoint. */
2999  "thread %"PRIiTID" blocked before single step; thread"
3000  " %"PRIiTID" owned probepoint -- BUG!!!\n",
3001  tthread->tid,probepoint->tpc->thread->tid);
3002  }
3003  else if (rc < 0) {
3004  verror("could not setup single step; badness will probably ensue!\n");
3005  }
3006  }
3007 
3008  return RESULT_SUCCESS;
3009 }
3010 
3011 /*
3012  * This function handles single step events. It broadly handles a few
3013  * cases. First, are there pending single step actions; if so, stay in
3014  * single step mode after we're done. Second, try to handle any complex
3015  * actions that might be needing single steps, if there are, let that
3016  * keep going. Finally, if we're done with any complex actions, and we
3017  * still haven't run the original instruction (and if none of hte
3018  * complex actions obviated (replaced) the original one), we run the
3019  * original instruction via single step (if necessary -- it might not be
3020  * necessary if we don't have posthandlers and if the breakpoint is hw,
3021  * or if the orig instruction was boostable.
3022  */
3023 result_t probepoint_ss_handler(struct target *target,
3024  struct target_thread *tthread,
3025  struct probepoint *probepoint) {
3026  struct thread_action_context *tac,*ttac;
3027  struct action *action;
3028  tid_t tid = tthread->tid;
3029  int rc;
3030  int handled_ss_actions = 0;
3031  int keep_stepping = 0;
3032  struct thread_probepoint_context *tpc = tthread->tpc;
3033  int noreinject;
3034  handler_msg_t amsg;
3035  struct probepoint *aprobepoint;
3036  REGVAL ipval;
3037 
3038  /*
3039  * If we had to disable a hw breakpoint before we single stepped it,
3040  * reenable it now. If it's a hardware breakpoint, we own it, so
3041  * that's why we don't check that.
3042  */
3043  if (probepoint
3044  && probepoint->style == PROBEPOINT_HW
3045  && probepoint->debugregdisabled
3046  && !target->nodisablehwbponss) {
3047  /*
3048  * Reenable the hw breakpoint we just disabled.
3049  */
3050  target_enable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
3051  probepoint->debugregdisabled = 0;
3052  }
3053 
3054  /*
3055  * First, if there is no probepoint (or if there is and we have
3056  * single step actions pending) and if this thread has pending
3057  * ss_actions. If one or the other, go through that list, increment
3058  * the counts, notify the handlers, and remove any ss_actions that
3059  * are done. If all are done, end singlestep mode and resume.
3060  * Otherwise, stay in single step mode.
3061  */
3062  if (!list_empty(&tthread->ss_actions)) {
3063  handled_ss_actions = 1;
3064 
3065  /*
3066  * If this address is an enalbed breakpiont, we need to send
3067  * MSG_STEPPING_AT_BP instead. This covers the software
3068  * breakpiont case; for the hardware one, see bp_handler.
3069  */
3070  amsg = MSG_STEPPING;
3071  ipval = target_read_reg(target,tthread->tid,target->ipregno);
3072  aprobepoint = target_lookup_probepoint(target,tthread,ipval);
3073  if (aprobepoint && aprobepoint->state != PROBE_DISABLED)
3074  amsg = MSG_STEPPING_AT_BP;
3075 
3076  list_for_each_entry_safe(tac,ttac,&tthread->ss_actions,tac) {
3077  action = tac->action;
3078  ++tac->stepped;
3079 
3080  if (action->steps < 0 || tac->stepped < action->steps) {
3081  if (action->handler)
3082  action->handler(action,tthread,action->probe,action->probe->probepoint,
3083  amsg,tac->stepped,action->handler_data);
3084  }
3085  else {
3086  if (action->handler)
3087  action->handler(action,tthread,action->probe,action->probe->probepoint,
3088  MSG_SUCCESS,tac->stepped,action->handler_data);
3089 
3090  action_finish_handling(action,tac);
3091 
3092  list_del(&tac->tac);
3093  free(tac);
3094  }
3095  }
3096 
3097  if (!list_empty(&tthread->ss_actions))
3098  keep_stepping = 1;
3099  }
3100  /*
3101  * If there is no probepoint, we return to the caller without
3102  * unpausing the target.
3103  */
3104  else if (!probepoint) {
3105  vwarn("thread %"PRIiTID" unexpected single step!\n",tthread->tid);
3106  return RESULT_ERROR;
3107  }
3108 
3109  /*
3110  * If we're done handling actions and the post single step of the
3111  * original instruction, AND if we were the owners of the
3112  * probepoint, nuke the tpc state!
3113  *
3114  * BUT, we have to check if we should keep single stepping for
3115  * pending single step actions.
3116  */
3117  if (probepoint
3118  && probepoint->tpc == tpc
3119  && probepoint->state == PROBE_BP_POSTHANDLING
3120  && tpc->did_orig_instr) {
3121  noreinject = run_post_handlers(target,tthread,probepoint);
3122 
3123  if (!noreinject) {
3124  if (probepoint->style == PROBEPOINT_SW) {
3125  /* Re-inject a breakpoint for the next round */
3126  if (target_enable_sw_breakpoint(target,probepoint->thread->tid,
3127  probepoint->mmod)) {
3128  verror("could not enable sw breakpoint instrs for bp re-insert, disabling!\n");
3129  probepoint->state = PROBE_DISABLED;
3130  return RESULT_ERROR;
3131  }
3132  else
3133  probepoint->state = PROBE_BP_SET;
3134  }
3135  else if (probepoint->debugregdisabled) {
3136  /*
3137  * Enable hardware bp here!
3138  */
3139  target_enable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
3140  probepoint->debugregdisabled = 0;
3141 
3142  probepoint->state = PROBE_BP_SET;
3143  }
3144  else {
3145  probepoint->state = PROBE_BP_SET;
3146  }
3147  }
3148  else {
3149  __probepoint_remove(probepoint,0,0);
3150  }
3151 
3152  if (target_singlestep_end(target,tid))
3153  verror("could not stop single stepping target"
3154  " after failed sstep!\n");
3155 
3156  if (target->blocking_thread == tthread)
3157  target->blocking_thread = NULL;
3158  probepoint_release(target,tthread,probepoint);
3159  tpc_free(tthread->tpc);
3160  tthread->tpc = (struct thread_probepoint_context *) \
3161  array_list_remove(tthread->tpc_stack);
3162  tpc = NULL;
3163 
3165  "thread %"PRIiTID" ran orig instruction; cleared tpc!\n",
3166  tthread->tid);
3167 
3168  if (keep_stepping)
3169  goto keep_stepping;
3170  else
3171  /* We're done with this, and don't want anything else below. */
3172  return RESULT_SUCCESS;
3173  }
3174 
3175  /*
3176  * Handle complex actions. This function does everything it might
3177  * need; if it leaves an action in tpc->tac.action, we just call
3178  * target_resume and trust that it did all the setup it needs.
3179  */
3180  if (tpc)
3181  rc = handle_complex_actions(target,tthread,probepoint);
3182 
3183  /*
3184  * NOTE: after this giant if stmt, the default return case is
3185  * RESULT_SUCCESS, unless another return stmt occurs.
3186  */
3187 
3188  if (tpc && tpc->tac.action) {
3189  /*
3190  * If handle_complex_actions left something in tpc->tac.action,
3191  * we just want to return success and wait and see what happens
3192  * with the action via future debug expections.
3193  */
3194  ;
3195  }
3196  /*
3197  * If we stepped an action first, we might still need to single step
3198  * the original instruction; do that if so. Or at least attempt it
3199  * -- if the action obviated the orig instruction,
3200  * setup_post_single_step handles that case and does the right things.
3201  */
3202  else if (tpc && !tpc->did_orig_instr) {
3203  vdebug(4,LA_PROBE,LF_PROBEPOINT,"setting up post handling single step for ");
3205  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
3206  rc = setup_post_single_step(target,tthread,probepoint);
3207 
3208  if (rc == 0) {
3209  /*
3210  * No single step scheduled, but no error, so we're done
3211  * handling the probepoint in this thread; pop the stack.
3212  */
3213  if (target->blocking_thread == tthread)
3214  target->blocking_thread = NULL;
3215  probepoint_release(target,tthread,probepoint);
3216  tpc_free(tthread->tpc);
3217  tthread->tpc = (struct thread_probepoint_context *) \
3218  array_list_remove(tthread->tpc_stack);
3219 
3221  "thread %"PRIiTID" skipping orig instruction; clearing tpc!\n",
3222  tthread->tid);
3223 
3224  /*
3225  * If we need to keep stepping for single step actions, do
3226  * that here.
3227  */
3228  if (keep_stepping)
3229  goto keep_stepping;
3230  /*
3231  else {
3232  return RESULT_SUCCESS;
3233  }
3234  */
3235  }
3236  else if (rc == 1) {
3237  /* Stepping, so just return without target_resume! */
3238  tpc->did_orig_instr = 1;
3239  //return RESULT_SUCCESS;
3240  }
3241  else if (rc == 2) {
3242  /* The thread blocked because it could not hold the probepoint. */
3244  "thread %"PRIiTID" blocked before single step real; thread"
3245  " %"PRIiTID" owned probepoint\n",
3246  tthread->tid,probepoint->tpc->thread->tid);
3247  //return RESULT_SUCCESS;
3248  }
3249  else if (rc < 0) {
3250  verror("could not setup single step; badness will probably ensue!\n");
3251  //return RESULT_SUCCESS;
3252  }
3253  }
3254  /*
3255  * If we don't have any complex actions to handle, we might still
3256  * need to stay in single step mode if any singlestep actions were
3257  * in progress for this thread. So check that.
3258  *
3259  * The difference between this single step and a single step at the
3260  * probepoint is that we don't disable hardware breakpoints before
3261  * single stepping!
3262  */
3263  else if (keep_stepping) {
3264  keep_stepping:
3265  if (probepoint) {
3266  vdebug(4,LA_PROBE,LF_PROBEPOINT,"continuing single step for ");
3268  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
3269  }
3270  else
3272  "continuing single step after probepoint\n");
3273 
3274  if (target_singlestep(target,tid,0) < 0) {
3275  verror("could not keep single stepping target!\n");
3276 
3277  if (target_singlestep_end(target,tid))
3278  verror("could not stop single stepping target"
3279  " after failed sstep!\n");
3280 
3281  /*
3282  * All we can really do is notify all the single step
3283  * actions that we had an error, and nuke them, and keep
3284  * going.
3285  */
3286  list_for_each_entry_safe(tac,ttac,&tthread->ss_actions,tac) {
3287  if (tac->action->handler)
3288  tac->action->handler(tac->action,tthread,tac->action->probe,
3289  tac->action->probe->probepoint,
3290  MSG_FAILURE,tac->stepped,
3291  tac->action->handler_data);
3292 
3293  action_finish_handling(tac->action,tac);
3294 
3295  list_del(&tac->tac);
3296  free(tac);
3297  }
3298 
3299  return RESULT_ERROR;
3300  }
3301  else {
3302  /*
3303  * If single step init succeeded, let the ss handler take
3304  * over.
3305  *
3306  * Don't call target_resume after a successful target_singlestep.
3307  */
3308  if (probepoint) {
3309  vdebug(4,LA_PROBE,LF_PROBEPOINT,"sstep command succeeded for ");
3311  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
3312  }
3313  else
3315  "sstep command succeeded after probepoint\n");
3316  }
3317  }
3318  /*
3319  else if (tpc->did_orig_instr) {
3320  *
3321  * We're totally done with this probepoint.
3322  *
3323  rc = 0;
3324  goto out;
3325  }
3326  */
3327  else if (!probepoint && !tpc && handled_ss_actions && !keep_stepping) {
3329  "finished single step actions after probepoint\n");
3330 
3331  if (target_singlestep_end(target,tid))
3332  verror("could not stop single stepping target after single step"
3333  " actions after probepoint!\n");
3334  }
3335  else {
3336  verror("unexpected state! BUG?\n");
3337  return RESULT_ERROR;
3338  }
3339 
3340  return RESULT_SUCCESS;
3341 }
3342 
3343 /*
3344  * This function handles the case where a thread is singlestepping, but
3345  * it has either stepped into a new context (i.e., user into kernel), or
3346  * into a different thread. Initially, we only handle the case where
3347  * the breakpoint was just hit and the single step setup, but not run.
3348  * We do not handle the case where a single step action was running.
3349  *
3350  * This is much the same as the single step handler, BUT we abort any
3351  * running actions; we do not continue single stepping; and we unwind
3352  * all state associated with handling the breakpoint.
3353  *
3354  * NB: we cannot read any register state from the thread; another thread
3355  * or thread context (i.e., kernel instead of user) might have been
3356  * entered. We just want to unwind the previous context and fixup the
3357  * probepoint.
3358  */
3360  struct target_thread *tthread,
3361  struct probepoint *probepoint) {
3362  struct thread_action_context *tac,*ttac;
3363  struct action *action;
3364  tid_t tid = tthread->tid;
3365  struct thread_probepoint_context *tpc;
3366 
3367  tpc = tthread->tpc;
3368 
3369  /*
3370  * If we had to disable a hw breakpoint before we single stepped it,
3371  * reenable it now. If it's a hardware breakpoint, we own it, so
3372  * that's why we don't check that.
3373  */
3374  if (probepoint
3375  && probepoint->style == PROBEPOINT_HW
3376  && probepoint->debugregdisabled
3377  && !target->nodisablehwbponss) {
3378  /*
3379  * Reenable the hw breakpoint we just disabled.
3380  */
3381  target_enable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
3382  probepoint->debugregdisabled = 0;
3383  }
3384 
3385  /*
3386  * Cancel any single step actions.
3387  */
3388  if (!list_empty(&tthread->ss_actions)) {
3389  list_for_each_entry_safe(tac,ttac,&tthread->ss_actions,tac) {
3390  action = tac->action;
3391 
3392  if (action->handler)
3393  action->handler(action,tthread,action->probe,
3394  action->probe->probepoint,
3395  MSG_INTERRUPTED,tac->stepped,
3396  action->handler_data);
3397 
3398  action_finish_handling(action,tac);
3399 
3400  list_del(&tac->tac);
3401  free(tac);
3402  }
3403  }
3404 
3405  /*
3406  * If we're done handling actions and the post single step of the
3407  * original instruction, AND if we were the owners of the
3408  * probepoint, nuke the tpc state!
3409  *
3410  * BUT, we have to check if we should keep single stepping for
3411  * pending single step actions.
3412  */
3413  if (probepoint->tpc == tpc) {
3414  /*
3415  * Cancel any complex actions.
3416  */
3417  if (!list_empty(&probepoint->complex_actions)) {
3418  list_for_each_entry_safe(tac,ttac,&probepoint->complex_actions,
3419  tac) {
3420  action = tac->action;
3421 
3423  "finished %d steps; done and removing interrupted"
3424  " action at ",
3425  tpc->tac.stepped);
3426  LOGDUMPPROBEPOINT(5,LA_PROBE,LF_ACTION,probepoint);
3427  vdebugc(5,LA_PROBE,LF_ACTION,"\n");
3428 
3429  if (action->handler)
3430  action->handler(action,tthread,action->probe,
3431  action->probe->probepoint,
3432  MSG_INTERRUPTED,tac->stepped,
3433  action->handler_data);
3434 
3435  action_finish_handling(action,tac);
3436 
3437  list_del(&tac->tac);
3438  free(tac);
3439  }
3440  }
3441 
3442  if (probepoint->style == PROBEPOINT_SW) {
3443  /* Re-inject a breakpoint for the next round */
3444  if (target_enable_sw_breakpoint(target,probepoint->thread->tid,
3445  probepoint->mmod)) {
3446  verror("could not enable sw breakpoint instrs for bp re-insert, disabling!\n");
3447  probepoint->state = PROBE_DISABLED;
3448  return RESULT_ERROR;
3449  }
3450  else
3451  probepoint->state = PROBE_BP_SET;
3452  }
3453  else if (probepoint->debugregdisabled) {
3454  /*
3455  * Enable hardware bp here!
3456  */
3457  target_enable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
3458  probepoint->debugregdisabled = 0;
3459 
3460  probepoint->state = PROBE_BP_SET;
3461  }
3462  else {
3463  probepoint->state = PROBE_BP_SET;
3464  }
3465 
3466  probepoint_release(target,tthread,probepoint);
3467  }
3468  else
3469  vwarn("thread %"PRIiTID" does not own tpc (thread %"PRIiTID" does);"
3470  " cannot adjust probepoint state!!\n",
3471  tid,tpc->thread->tid);
3472 
3473  /* Do this stuff anyway. */
3474  if (target_singlestep_end(target,tid))
3475  verror("could not stop single stepping target"
3476  " after interrupted sstep!\n");
3477 
3478  if (target->blocking_thread == tthread)
3479  target->blocking_thread = NULL;
3480  tpc_free(tthread->tpc);
3481  tthread->tpc = (struct thread_probepoint_context *) \
3482  array_list_remove(tthread->tpc_stack);
3483  tpc = NULL;
3484 
3485  /*
3486  * Save it off so that probepoint_bp_handler will see it when we hit
3487  * the breakpoint again.
3488  */
3490 
3492  "thread %"PRIiTID" interrupted before running orig instruction;"
3493  " cleared tpc!\n",
3494  tthread->tid);
3495 
3496  return RESULT_SUCCESS;
3497 }
3498 
3499 /*
3500  * This function should be called for each thread that has been left
3501  * paused before target_resume completes; if bp_handler or ss_handler
3502  * cleared
3503  */
3505  struct target_thread *tthread) {
3506  struct probepoint *probepoint;
3507  struct thread_probepoint_context *tpc = tthread->tpc;
3508  int rc;
3509  tid_t tid = tthread->tid;
3510  struct thread_action_context *tac,*ttac;
3511 
3512  switch (tthread->resumeat) {
3513  case THREAD_RESUMEAT_NONE:
3514  return RESULT_ERROR;
3515  case THREAD_RESUMEAT_BPH:
3516  return probepoint_bp_handler(target,tthread,tthread->tpc->probepoint,0);
3517  case THREAD_RESUMEAT_SSR:
3518  return setup_post_single_step(target,tthread,tthread->tpc->probepoint);
3519  case THREAD_RESUMEAT_NA:
3520  /*
3521  * This could have only been interrupted comoing from the ss
3522  * handler, so we know that if it does not do an action and does
3523  * not stay in single step, we have to try to do the post single
3524  * step too (?)
3525  */
3526  probepoint = tthread->tpc->probepoint;
3527  handle_complex_actions(target,tthread,probepoint);
3528  if (!tpc->tac.action && !tpc->did_orig_instr) {
3529  vdebug(4,LA_PROBE,LF_PROBEPOINT,"setting up post handling single step for ");
3531  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
3532  rc = setup_post_single_step(target,tthread,probepoint);
3533 
3534  if (rc == 0) {
3535  /*
3536  * No single step scheduled, but no error, so we're done
3537  * handling the probepoint in this thread; pop the stack.
3538  */
3539  if (target->blocking_thread == tthread)
3540  target->blocking_thread = NULL;
3541  probepoint_release(target,tthread,probepoint);
3542  tpc_free(tthread->tpc);
3543  tthread->tpc = (struct thread_probepoint_context *) \
3544  array_list_remove(tthread->tpc_stack);
3545 
3547  "thread %"PRIiTID" skipping orig instruction; clearing tpc!\n",
3548  tthread->tid);
3549 
3550  }
3551  else if (rc == 1) {
3552  /* Stepping, so just return without target_resume! */
3553  tpc->did_orig_instr = 1;
3554  //return RESULT_SUCCESS;
3555  }
3556  else if (rc == 2) {
3557  /* The thread blocked because it could not hold the probepoint. */
3559  "thread %"PRIiTID" blocked before single step real; thread"
3560  " %"PRIiTID" owned probepoint\n",
3561  tthread->tid,probepoint->tpc->thread->tid);
3562  //return RESULT_SUCCESS;
3563  }
3564  else if (rc < 0) {
3565  verror("could not setup single step; badness will probably ensue!\n");
3566  //return RESULT_SUCCESS;
3567  }
3568  }
3569  else if (!tpc->tac.action && !tpc->tac.action->steps
3570  && !list_empty(&tthread->ss_actions)) {
3572  "continuing single step after probepoint\n");
3573 
3574  if (target_singlestep(target,tid,0) < 0) {
3575  verror("could not keep single stepping target!\n");
3576 
3577  if (target_singlestep_end(target,tid))
3578  verror("could not stop single stepping target"
3579  " after failed sstep!\n");
3580 
3581  /*
3582  * All we can really do is notify all the single step
3583  * actions that we had an error, and nuke them, and keep
3584  * going.
3585  */
3586  list_for_each_entry_safe(tac,ttac,&tthread->ss_actions,tac) {
3587  if (tac->action->handler)
3588  tac->action->handler(tac->action,tthread,
3589  tac->action->probe,
3590  tac->action->probe->probepoint,
3591  MSG_FAILURE,tac->stepped,
3592  tac->action->handler_data);
3593 
3594  action_finish_handling(tac->action,tac);
3595 
3596  list_del(&tac->tac);
3597  free(tac);
3598  }
3599 
3600  return RESULT_ERROR;
3601  }
3602  else {
3603  /*
3604  * If single step init succeeded, let the ss handler take
3605  * over.
3606  *
3607  * Don't call target_resume after a successful target_singlestep.
3608  */
3609  vdebug(4,LA_PROBE,LF_PROBEPOINT,"sstep command succeeded for resumeat ");
3611  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
3612  }
3613  }
3614  else {
3616  "finished handling after resumeat\n");
3617 
3618  if (target_singlestep_end(target,tid))
3619  verror("could not stop single stepping target after single step"
3620  " actions after probepoint!\n");
3621  }
3622  return RESULT_SUCCESS;
3623  case THREAD_RESUMEAT_PH:
3624  return RESULT_ERROR;
3625  default:
3626  return RESULT_ERROR;
3627  }
3628 }
3629 
3630 static int __remove_action(struct target *target,struct probepoint *probepoint,
3631  struct action *action) {
3632  /*
3633  * If it was boosted, it will be removed when the action is destroyed.
3634  */
3635  if (action->boosted)
3636  return 0;
3637 
3638  if (action->type == ACTION_RETURN || action->type == ACTION_CUSTOMCODE) {
3639  if (probepoint->style == PROBEPOINT_SW) {
3640  if (target_unchange_sw_breakpoint(target,probepoint->thread->tid,
3641  probepoint->mmod)) {
3642  verror("could not remove action code at the breakpoint;"
3643  " badness will probably ensue!\n");
3644  }
3645  if (target_enable_sw_breakpoint(target,probepoint->thread->tid,
3646  probepoint->mmod)) {
3647  verror("could not reenable the breakpoint;"
3648  " badness will probably ensue!\n");
3649  return -1;
3650  }
3651  }
3652  else if (probepoint->mmod) {
3653  if (target_memmod_release(target,probepoint->thread->tid,
3654  probepoint->mmod) < 0) {
3655  verror("could not remove action code at HW breakpoint;"
3656  " badness will probably ensue!\n");
3657  return -1;
3658  }
3659  else
3660  probepoint->mmod = NULL;
3661  }
3662  }
3663 
3664  probepoint->state = PROBE_ACTION_DONE;
3665 
3666  return 0;
3667 }
3668 
3669 static int __insert_action(struct target *target,struct target_thread *tthread,
3670  struct probepoint *probepoint,struct action *action) {
3671  unsigned int buflen;
3672  unsigned char *buf;
3673  REGVAL rval;
3674 
3675  /* Set EIP to the address of our code. */
3676  if (target_write_reg(target,tthread->tid,target->ipregno,action->start_addr)) {
3677  verror("could not set EIP to action's first instruction (0x%"PRIxADDR")!\n",
3678  action->start_addr);
3679  return -1;
3680  }
3681 
3682  /*
3683  * If it was boosted, it was inserted when it was scheduled.
3684  */
3685  if (action->boosted)
3686  return 0;
3687 
3688  if (action->type == ACTION_RETURN) {
3689  /*
3690  * If we have executed a prologue: if the prologue contains
3691  * a save of the frame pointer (0x55), all we have to do is
3692  * set rsp to rbp, call leaveq, and call retq.
3693  * If the prologue does not contain a save of the frame
3694  * pointer, we have to track all modifications to rsp during
3695  * the prologue, undo them, and call leaveq, and retq.
3696  */
3697  if (action->detail.ret.prologue) {
3698  if (action->detail.ret.prologue_uses_bp) {
3700  "setting ESP to EBP and returning (prologue uses EBP) at ");
3701  LOGDUMPPROBEPOINT(3,LA_PROBE,LF_ACTION,probepoint);
3702  vdebugc(3,LA_PROBE,LF_ACTION,"\n");
3703 
3704  errno = 0;
3705  rval = target_read_reg(target,tthread->tid,target->fbregno);
3706  if (errno) {
3707  verror("read EBP failed; action failed!\n");
3708  return -1;
3709  }
3710 
3711  if (target_write_reg(target,tthread->tid,target->spregno,rval)) {
3712  verror("set ESP to EBP failed; action failed and badness will ensue!\n");
3713  return -1;
3714  }
3715 
3716  buf = target->arch->full_ret_instrs;
3717  buflen = target->arch->full_ret_instrs_len;
3718  action->steps = target->arch->full_ret_instr_count;
3719  }
3720  else {
3722  "undoing prologue ESP changes (%d) and returning at ",
3723  action->detail.ret.prologue_sp_offset);
3724  LOGDUMPPROBEPOINT(3,LA_PROBE,LF_ACTION,probepoint);
3725  vdebugc(3,LA_PROBE,LF_ACTION,"\n");
3726 
3727  errno = 0;
3728  rval = target_read_reg(target,tthread->tid,target->spregno);
3729  if (errno) {
3730  verror("read ESP failed; action failed!\n");
3731  return -1;
3732  }
3733 
3734  if (target_write_reg(target,tthread->tid,target->spregno,
3735  rval + (REGVAL)-action->detail.ret.prologue_sp_offset)) {
3736  verror("undoing prologue ESP changes failed; action failed!\n");
3737  return -1;
3738  }
3739 
3740  buf = target->arch->ret_instrs;
3741  buflen = target->arch->ret_instrs_len;
3742  action->steps = target->arch->ret_instr_count;
3743  }
3744  }
3745  else {
3746  buf = target->arch->ret_instrs;
3747  buflen = target->arch->ret_instrs_len;
3748  action->steps = target->arch->ret_instr_count;
3749  }
3750  }
3751  else if (action->type == ACTION_CUSTOMCODE) {
3752  buf = action->detail.code.buf;
3753  buflen = action->detail.code.buflen;
3754  }
3755  else {
3756  verror("cannot handle unknown action type %d!\n",action->type);
3757  return -1;
3758  }
3759 
3760  if (probepoint->style == PROBEPOINT_SW) {
3761  if (target_change_sw_breakpoint(target,probepoint->thread->tid,
3762  probepoint->mmod,buf,buflen)) {
3763  verror("could not insert action code; action failed!\n");
3764  return -1;
3765  }
3766  }
3767  else {
3768  if (!probepoint->mmod) {
3769  probepoint->mmod = target_memmod_create(target,probepoint->thread->tid,
3770  probepoint->addr,0,MMT_CODE,
3771  buf,buflen,0);
3772  if (!probepoint->mmod) {
3773  verror("could not create memmod for HW breakpoint code"
3774  " at 0x%"PRIxADDR"!\n",probepoint->addr);
3775  return -1;
3776  }
3777  }
3778  else {
3779  verror("could not create memmod for HW breakpoint code"
3780  " at 0x%"PRIxADDR"!\n",probepoint->addr);
3781  return -1;
3782  }
3783  }
3784 
3785  probepoint->state = PROBE_ACTION_RUNNING;
3786 
3787  return 0;
3788 }
3789 
3790 struct action *__get_next_complex_action(struct probepoint *probepoint,
3791  struct action *current) {
3792  struct list_head *head = &probepoint->complex_actions;
3793 
3794  if (list_empty(head))
3795  return NULL;
3796  else if (!current)
3797  return list_entry(head->next,typeof(*current),action);
3798  else if (current->action.next == head)
3799  return NULL;
3800  else
3801  return list_entry(current->action.next,struct action,action);
3802 }
3803 
3804 /*
3805  * This copies code into the breakpoint as necessary and single steps as
3806  * necessary, or sets things up to handle boosted instructions if that's
3807  * what we're doing. It also handles single step events it has set up.
3808  * Basically, anything to do with complex actions, it handles.
3809  *
3810  * It does setup only; it does not resume the target; only its callers
3811  * should do that.
3812  *
3813  * If we setup a complex action, we are going to have to single step at
3814  * least one instruction (and maybe more). If we did not set up an
3815  * action, we have to replace the original instruction by the real thing
3816  * and single step -- OR if it was boostable and target supported it, we
3817  * set EIP to the boosted location, and single step there if necessary,
3818  * and mess with EIP again afterward (or let the boosted instruction JMP
3819  * back to the real instruction following the real boosted
3820  * instruction.and its effects took the place of the original
3821  * instruction (obviated it), enable single stepping if needed.
3822  *
3823  * Returns: <0 on error; 0 if no actions requiring single step were
3824  * setup; 1 if actions requiring single step were setup (and thus single
3825  * step mode was enabled).
3826  */
3827 static int handle_complex_actions(struct target *target,
3828  struct target_thread *tthread,
3829  struct probepoint *probepoint) {
3830  struct action *action,*nextaction = NULL;
3831  struct thread_probepoint_context *tpc;
3832  tid_t tid = tthread->tid;
3833  struct thread_action_context *tac,*ttac;
3834  struct thread_probepoint_context *htpc;
3835 
3836  tpc = tthread->tpc;
3837 
3838  /*
3839  * If we had paused this thread before handling an action because we
3840  * needed to hold the probepoint, but couldn't, jump straight to
3841  * that part.
3842  */
3843  if (tthread->resumeat == THREAD_RESUMEAT_NA) {
3844  action = nextaction = tpc->tac.action;
3845  goto nextaction;
3846  }
3847 
3848  /*
3849  * If we just arrived here after hitting the breakpoint, reset our
3850  * state, and try to start an action.
3851  */
3852  if (probepoint->tpc == tpc
3853  && probepoint->state == PROBE_BP_ACTIONHANDLING) {
3855  "resetting actions state after bp hit at ");
3856  LOGDUMPPROBEPOINT(3,LA_PROBE,LF_ACTION,probepoint);
3857  vdebugc(5,LA_PROBE,LF_ACTION,"\n");
3858 
3859  tpc->tac.action = NULL;
3860  tpc->tac.stepped = 0;
3861  tpc->action_obviated_orig = 0;
3862 
3863  /*
3864  * XXX: this could fail bad if whatever the current action
3865  * points to on the probepoint's list is removed from the list.
3866  * What to do???
3867  */
3868  nextaction = action = __get_next_complex_action(probepoint,NULL);
3869  if (!action) {
3870  vdebug(3,LA_PROBE,LF_ACTION,"no actions to run at ");
3871  LOGDUMPPROBEPOINT(3,LA_PROBE,LF_ACTION,probepoint);
3872  vdebugc(3,LA_PROBE,LF_ACTION,"\n");
3873 
3874  return 0;
3875  }
3876  }
3877  /*
3878  * If we need to keep stepping through this action, keep stepping.
3879  */
3880  else if (tpc->tac.action && tpc->tac.action->steps) {
3881  action = tpc->tac.action;
3882  /*
3883  * Increment the single step count no matter what.
3884  */
3885  ++tpc->tac.stepped;
3886 
3887  if (action->steps < 0 || tpc->tac.stepped < action->steps) {
3889  "did %d steps; still more at ",tpc->tac.stepped);
3890  LOGDUMPPROBEPOINT(5,LA_PROBE,LF_ACTION,probepoint);
3891  vdebugc(5,LA_PROBE,LF_ACTION,"\n");
3892 
3893  if (action->handler)
3894  action->handler(action,tthread,action->probe,probepoint,
3895  MSG_STEPPING,tpc->tac.stepped,
3896  action->handler_data);
3897  }
3898  else {
3900  "finished %d steps; done and removing action at ",
3901  tpc->tac.stepped);
3902  LOGDUMPPROBEPOINT(5,LA_PROBE,LF_ACTION,probepoint);
3903  vdebugc(5,LA_PROBE,LF_ACTION,"\n");
3904  /*
3905  * If we know the action is done because it has finished its
3906  * set amount of single steps, we need to "finish" it:
3907  */
3908  if (action->handler)
3909  action->handler(action,tthread,action->probe,probepoint,
3910  MSG_SUCCESS,tpc->tac.stepped,
3911  action->handler_data);
3912 
3913  __remove_action(target,probepoint,action);
3914 
3915  if (target_singlestep_end(target,tid))
3916  verror("could not stop single stepping target"
3917  " after single stepped action!\n");
3918 
3919  /* Grab the next one before we destroy this one. */
3920  nextaction = __get_next_complex_action(probepoint,action);
3921 
3922  action_finish_handling(action,&tpc->tac);
3923 
3924  action = nextaction;
3925 
3926  if (!nextaction) {
3927  tpc->tac.action = NULL;
3928  tpc->tac.stepped = 0;
3929  }
3930  }
3931  }
3932  /*
3933  * If we have a current action, but don't need single steps, we
3934  * don't want to do *anything* -- because we didn't initiate this
3935  * single step! In future, we could warn the user...
3936  */
3937  else if (tpc->tac.action) {
3938  vwarn("unexpected single step!\n");
3939  return 0;
3940  }
3941  else if (!tpc->tac.action) {
3942  vdebug(5,LA_PROBE,LF_ACTION,"no action being handled at ");
3943  LOGDUMPPROBEPOINT(3,LA_PROBE,LF_ACTION,probepoint);
3944  vdebugc(5,LA_PROBE,LF_ACTION,"\n");
3945  return 0;
3946  }
3947 
3948  nextaction:
3949  if (nextaction) {
3950  /*
3951  * Start this action up!
3952  */
3953 
3954  /* Clean up state, then setup next action if there is one. */
3955 
3956  if (!nextaction->boosted) {
3957  /*
3958  * We need to grab the probepoint before we run this one!
3959  */
3960  if ((htpc = probepoint_hold(target,tthread,probepoint,tpc)) != tpc) {
3961  if (probepoint_pause_handling(target,tthread,probepoint,
3962  THREAD_RESUMEAT_NA)) {
3963  verror("thread %"PRIiTID" hit nextaction when thread %"PRIiTID" already"
3964  " handling it; target does not support thread ctl!\n",
3965  tthread->tid,htpc->thread->tid);
3966  /*
3967  * There literally is nothing we can do -- we cannot
3968  * handle this breakpoint interrupt.
3969  */
3970  return -1;
3971  }
3972  else
3973  return 0;
3974  }
3975 
3976  /*
3977  * If the action requires more than one single step, has
3978  * more than one instruction, and we're in the right BPMODE,
3979  * we have to pause all the other threads.
3980  */
3981  if (target->spec->bpmode == THREAD_BPMODE_STRICT
3982  || (target->spec->bpmode == THREAD_BPMODE_SEMI_STRICT
3983  && (action->steps > 1
3984  || (action->type == ACTION_CUSTOMCODE
3985  && action->detail.code.instr_count > 1)))) {
3986  if (target_pause(target)) {
3987  vwarn("could not pause the target for blocking thread"
3988  " %"PRIiTID"!\n",tthread->tid);
3989  return -1;
3990  }
3991  target->blocking_thread = tthread;
3992  }
3993  else {
3994  if (target->blocking_thread == tthread)
3995  target->blocking_thread = NULL;
3996  }
3997 
3998  probepoint->state = PROBE_ACTION_RUNNING;
3999  }
4000  else {
4001  if (target->blocking_thread == tthread)
4002  target->blocking_thread = NULL;
4003  }
4004 
4005  __insert_action(target,tthread,probepoint,nextaction);
4006 
4007  tpc->tac.action = nextaction;
4008 
4009  tpc->action_obviated_orig |= nextaction->obviates;
4010 
4011  action = nextaction;
4012  }
4013 
4014  /*
4015  * Single step if the current action needs it.
4016  */
4017  if (tpc->tac.action && tpc->tac.action->steps) {
4018  vdebug(4,LA_PROBE,LF_PROBEPOINT,"single step for action at ");
4020  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
4021 
4022  int _isbp = 0;
4023  if (!tpc->tac.action->boosted && !tpc->tac.stepped)
4024  _isbp = 1;
4025 
4026  if (probepoint->style == PROBEPOINT_HW
4027  && _isbp
4028  && !target->nodisablehwbponss) {
4029  /*
4030  * We need to disable the hw breakpoint before we single
4031  * step because the target can't do it.
4032  */
4033  target_disable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
4034  probepoint->debugregdisabled = 1;
4035  }
4036 
4037  if (target_singlestep(target,tid,_isbp) < 0) {
4038  verror("could not keep single stepping target!\n");
4039 
4040  if (probepoint->style == PROBEPOINT_HW
4041  && _isbp
4042  && !target->nodisablehwbponss) {
4043  /*
4044  * Reenable the hw breakpoint we just disabled.
4045  */
4046  target_enable_hw_breakpoint(target,probepoint->thread->tid,probepoint->debugregnum);
4047  probepoint->debugregdisabled = 0;
4048  }
4049 
4050  if (target_singlestep_end(target,tid))
4051  verror("could not stop single stepping target"
4052  " after failed sstep!\n");
4053 
4054  /*
4055  * All we can really do is notify all the single step
4056  * actions that we had an error, and nuke them, and keep
4057  * going.
4058  */
4059  list_for_each_entry_safe(tac,ttac,&tthread->ss_actions,tac) {
4060  if (tac->action->handler)
4061  tac->action->handler(tac->action,tthread,tac->action->probe,
4062  action->probe->probepoint,
4063  MSG_FAILURE,tac->stepped,
4064  tac->action->handler_data);
4065 
4066  action_finish_handling(action,tac);
4067 
4068  list_del(&tac->tac);
4069  free(tac);
4070  }
4071 
4072  return -1;
4073  }
4074  else {
4075  /*
4076  * If single step init succeeded, let the ss handler take
4077  * over.
4078  *
4079  * Don't call target_resume after a successful target_singlestep.
4080  */
4081  vdebug(4,LA_PROBE,LF_PROBEPOINT,"sstep command succeeded for action at ");
4083  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
4084 
4085  return 0;
4086  }
4087 
4088  return 1;
4089  }
4090  else if (tpc->tac.action) {
4091  vdebug(4,LA_PROBE,LF_PROBEPOINT,"NOT single stepping for action at ");
4093  vdebugc(4,LA_PROBE,LF_PROBEPOINT,"\n");
4094  }
4095 
4096  return 0;
4097 }
4098 
4099 /*
4100  * Schedules an action to occur, given a probe handler. Actions can
4101  * only occur when a domain is paused after a probe breakpoint has been
4102  * hit. Actions can be scheduled prior to beginning probing, or they
4103  * can be scheduled at probe handler runtime.
4104  *
4105  * Some actions may preclude others in the future. For instance, a
4106  * return action depends greatly on the body of the called function NOT
4107  * being executed at all, so that %eax may be set and a 'ret'
4108  * instruction executed (so it's immediately after the 'call'
4109  * instruction; there is no state to clean up, etc. Allowing a custom
4110  * code action prior to a return action might change the validity of
4111  * this assumption.
4112  *
4113  * But for now, the only restriction is
4114  * - one return action per handle, and it will be performed after
4115  * execution of the pre handler.
4116  *
4117  * Actions do not have priorities; they are executed in the order scheduled.
4118  */
4119 int action_sched(struct probe *probe,struct action *action,
4120  action_whence_t whence,
4121  action_handler_t handler,void *handler_data) {
4122  struct action *lpc;
4123  unsigned char *code;
4124  unsigned int code_len = 0;
4125  struct probepoint *probepoint;
4126  struct target *target;
4127  int i;
4128  struct array_list *idata_list;
4129  struct inst_data *idata;
4130 
4131  if (action->probe != NULL) {
4132  verror("action already associated with probe!\n");
4133  return -1;
4134  }
4135 
4136  if (!probe->probepoint) {
4137  errno = EINVAL;
4138  verror("probe not attached to a probepoint!\n");
4139  return -1;
4140  }
4141 
4142  if (whence != ACTION_ONESHOT && whence != ACTION_REPEATPRE
4143  && whence != ACTION_REPEATPOST) {
4144  verror("unknown whence %d for action\n",whence);
4145  errno = EINVAL;
4146  return -1;
4147  }
4148 
4149  probepoint = probe->probepoint;
4150  target = probepoint->target;
4151 
4152  /*
4153  * If it's boosted, it's already set.
4154  */
4155  if (!action->boosted)
4156  action->start_addr = probepoint->addr;
4157 
4158  if (action->type == ACTION_REGMOD || action->type == ACTION_MEMMOD) {
4159  list_add_tail(&action->action,&probe->probepoint->simple_actions);
4160  }
4161  /* right now you can always enable singlestep actions */
4162  else if (action->type == ACTION_SINGLESTEP) {
4163  list_add_tail(&action->action,&probe->probepoint->ss_actions);
4164  }
4165  /* only allow one return action per probepoint */
4166  else if (action->type == ACTION_RETURN) {
4167  list_for_each_entry(lpc,&probe->probepoint->complex_actions,action) {
4168  if (lpc->type == ACTION_RETURN) {
4169  verror("probepoint already has return action\n");
4170  return -1;
4171  }
4172  else if (lpc->type == ACTION_CUSTOMCODE) {
4173  verror("probepoint already has customcode action\n");
4174  return -1;
4175  }
4176  }
4177 
4178  /* Try to disassemble and setup return from the prologue. */
4179  if (probepoint->symbol_addr
4180  && probepoint->symbol_addr != probepoint->addr) {
4181  if (probepoint->symbol_addr > probepoint->addr) {
4182  vwarn("probepoint symbol addr < probepoint addr -- bad\n");
4183  }
4184  else {
4185  action->detail.ret.prologue = 1;
4186 
4187  /* Read the prologue; if the first byte is 0x55, we're
4188  * using frame pointers and we don't need to analyze the
4189  * prologue to watch the stack grow so we can undo it.
4190  * Otherwise, read the stack growth during the prologue
4191  * so we can undo it during a return.
4192  */
4193  code_len = probepoint->addr - probepoint->symbol_addr;
4194  code = (unsigned char *)malloc(code_len);
4195  memset(code,0,code_len);
4196 
4197  if (!target_read_addr(target,probepoint->symbol_addr,
4198  code_len,code)) {
4199  vwarn("could not read prologue code; skipping disasm!\n");
4200  free(code);
4201  code = NULL;
4202  }
4203  else {
4204  /*
4205  * Check both for first instruction 0x55; any
4206  * instruction 0x55 (to catch functions that callq
4207  * elsewhere as their first instr); and disasm the
4208  * prologue to see %sp delta . Then we can pick
4209  * which method we can use later.
4210  */
4211 
4212  /*
4213  * Don't require the first instruction to be 0x55
4214  * (push %ebp); look for it at any point in the
4215  * function! We could also look for 0xc9 (leave).
4216  */
4217  if (*code == 0x55)
4218  action->detail.ret.prologue_uses_bp = 1;
4219  else if (disasm_generic(target,code,code_len,
4220  &idata_list,1) == 0) {
4221  for (i = 0; i < array_list_len(idata_list); ++i) {
4222  idata = (struct inst_data *) \
4223  array_list_item(idata_list,i);
4224  if (idata->type == 0x55) {
4225  action->detail.ret.prologue_uses_bp = 1;
4226  break;
4227  }
4228  }
4229  if (idata_list)
4230  array_list_deep_free(idata_list);
4231  }
4232  else {
4234  "could not disassemble code in range"
4235  "0x%"PRIxADDR"-0x%"PRIxADDR
4236  " for function %s!\n",
4237  probepoint->symbol_addr,probepoint->addr,
4238  probepoint->bsymbol ? probepoint->bsymbol->lsymbol->symbol->name : "<UNKNOWN>");
4239  }
4240 
4241  if (action->detail.ret.prologue_uses_bp) {
4243  "function %s: pushes EBP (can leave; ret)\n",
4244  probepoint->bsymbol ? probepoint->bsymbol->lsymbol->symbol->name : "<UNKNOWN>");
4245  }
4246 #ifdef ENABLE_DISTORM
4247 
4248  /*
4249  * Ok, now disasm the prologue.
4250  */
4251  if (disasm_get_prologue_stack_size(target,code,code_len,
4252  &action->detail.ret.prologue_sp_offset)) {
4253  verror("could not disassemble function prologue"
4254  " to track stack growth for return action!\n");
4255  }
4256  else {
4257  action->detail.ret.prologue_has_sp_offset = 1;
4258 
4260  "disassembled prologue for function %s: sp moved %d\n",
4261  probepoint->bsymbol ? probepoint->bsymbol->lsymbol->symbol->name : "<UNKNOWN>",
4262  action->detail.ret.prologue_sp_offset);
4263  }
4264 #else
4265  {
4266  verror("disasm support disabled; cannot check prologue"
4267  " to unwind stack frame function %s!\n",
4268  probepoint->bsymbol \
4269  ? probepoint->bsymbol->lsymbol->symbol->name \
4270  : "<UNKNOWN>");
4271  }
4272 #endif
4273 
4274  /* Clean up. */
4275  free(code);
4276  code = NULL;
4277  }
4278  }
4279  }
4280 
4281  /*
4282  * First check is redundant due to check in target_open, but
4283  * in case that goes away, it's here too.
4284  */
4285  if (target->spec->bpmode == THREAD_BPMODE_STRICT && !target->threadctl) {
4286  verror("cannot do return on strict target without threadctl!\n");
4287  errno = ENOTSUP;
4288  return -1;
4289  }
4290  else if (action->detail.ret.prologue && action->detail.ret.prologue_uses_bp
4291  && !action->boosted
4292  && !target->threadctl
4293  && target->arch->full_ret_instr_count > 1
4294  && target->spec->bpmode == THREAD_BPMODE_SEMI_STRICT) {
4295  if (action->detail.ret.prologue_has_sp_offset) {
4297  "cannot use multi-instr full return; but can use SP"
4298  " adjustment and single-instr return\n");
4299 
4300  action->detail.ret.prologue_uses_bp = 0;
4301  }
4302  else {
4303  verror("cannot do non-boosted, multi-instruction return"
4304  " on strict target without threadctl!\n");
4305  errno = ENOTSUP;
4306  return -1;
4307  }
4308  }
4309 
4310  /*
4311  * We do not need to manually boost return instructions; if the
4312  * target supported it and had room, these are added to the
4313  * target when we attach. BUT, return instructions ALWAYS must
4314  * be single stepped, because otherwise we don't know when we're
4315  * done returning (and thus can't call action handlers,
4316  * probepoint post handlers, garbage collect anything, etc).
4317  */
4318 
4319  list_add_tail(&action->action,&probe->probepoint->complex_actions);
4320  }
4321  /* only allow one customcode action per probepoint */
4322  else if (action->type == ACTION_CUSTOMCODE) {
4323 
4324  /*
4325  * For now, we always boost custom code so that it doesn't
4326  * interfere with the handling of the probepoint. So, we
4327  * assemble our code buf according to the flags, copy in the
4328  * user's code, and end the code frag with a special breakpoint
4329  * probepoint so that we know when we're done with the custom
4330  * code.
4331  *
4332  * We have to assemble the code buf before we ask the target for
4333  * space from its boostable memory (if there is any -- there
4334  * might not be, or might not be enough).
4335  */
4336 
4337  list_for_each_entry(lpc,&probe->probepoint->complex_actions,action) {
4338  if (lpc->type == ACTION_RETURN) {
4339  verror("probepoint already has return action\n");
4340  return -1;
4341  }
4342  else if (lpc->type == ACTION_CUSTOMCODE) {
4343  verror("probepoint already has customcode action\n");
4344  return -1;
4345  }
4346  }
4347 
4348  /* XXX: this check should really let 1-instr actions go through */
4349  if ((target->spec->bpmode == THREAD_BPMODE_STRICT
4350  || target->spec->bpmode == THREAD_BPMODE_SEMI_STRICT)
4351  && !action->boosted
4352  && !target->threadctl) {
4353  verror("cannot do return on strict target without threadctl!\n");
4354  errno = ENOTSUP;
4355  return -1;
4356  }
4357 
4358  list_add_tail(&action->action,&probe->probepoint->complex_actions);
4359  }
4360 
4361  /*
4362  * Ok, we're safe; memo-ize all the sched info into the action struct.
4363  */
4364  action->whence = whence;
4365 
4366  action->handler = handler;
4367  action->handler_data = handler_data;
4368 
4369  action->probe = probe;
4370 
4371  target_attach_action(target,action);
4372 
4373  RHOLD(action,probe);
4374 
4375  return 0;
4376 }
4377 
4378 /*
4379 void __action_cancel_in_thread(struct target_thread *tthread,
4380  struct action *action) {
4381  int i;
4382  struct thread_probepoint_context *tpc;
4383 
4384  if (!array_list_len(tthread->tpc_stack))
4385  return;
4386 
4387  for (i = 0; i < array_list_len(tthread->tpc_stack); ++i) {
4388  tpc = (struct thread_probepoint_context *) \
4389  array_list_item(tthread->tpc_stack,i);
4390 
4391  if (tpc->tac.action == action) {
4392  if (!action->isboosted)
4393  vwarn("canceling a non-boosted action running in"
4394  " thread %"PRIiTID"; badness!!!\n",tthread->tid);
4395 
4396  // We have to mark it as canceled
4397  tpc->tac.canceled = 1;
4398  tpc->tac.nextaction = \
4399  __get_next_complex_action(action->probe->probepoint,action);
4400 
4401  action_release(action);
4402  }
4403  }
4404 
4405  // XXX: but we still have the problem of what happens when an
4406  // action still has code at the breakpoint
4407 }
4408 */
4409 
4410 /*
4411  * Cancel an action.
4412  */
4413 int action_cancel(struct action *action) {
4414  REFCNT trefcnt;
4415 
4416  if (!action->probe) {
4417  verror("cannot cancel action not associated with a probe!\n");
4418  return 1;
4419  }
4420 
4421  /*
4422  * XXX: this is bad, but correct. We have to check all threads and
4423  * make sure that this action is not the one they are currently
4424  * executing; if so, we have to go mark the
4425  * thread_probepoint_context that was holding it as deleted, and
4426  * "release" the action manually. I guess this means that actions
4427  * don't really need refcnting...
4428  */
4429  /*
4430  target = action->probe->probepoint->target;
4431 
4432 
4433  if (action->probe
4434  && action->probe->probepoint
4435  && action->probe->probepoint->action == action) {
4436  verror("cannot cancel a currently-running action!\n");
4437  return 1;
4438  }
4439  */
4440 
4441  action->handler = NULL;
4442  action->handler_data = NULL;
4443 
4444  list_del(&action->action);
4445  action->probe = NULL;
4446 
4447  target_detach_action(action->target,action);
4448 
4449  RPUT(action,action,probe->probepoint,trefcnt);
4450 
4451  return 0;
4452 }
4453 
4454 /*
4455  * High-level actions that require little ASM knowledge.
4456  */
4457 struct action *action_return(REGVAL retval) {
4458  struct action *action;
4459 
4460  action = (struct action *)calloc(1,sizeof(struct action));
4461  if (!action) {
4462  verror("could not malloc action: %s\n",strerror(errno));
4463  return NULL;
4464  }
4465 
4466  action->type = ACTION_RETURN;
4467  action->whence = ACTION_UNSCHED;
4468  INIT_LIST_HEAD(&action->action);
4469 
4470  action->detail.ret.retval = retval;
4471 
4472  action->obviates = 1;
4473 
4474  RHOLD(action,action);
4475 
4476  return action;
4477 }
4478 
4479 /*
4480  * Low-level actions that require little ASM knowledge, and may or may
4481  * not be permitted.
4482  */
4483 struct action *action_singlestep(int nsteps) {
4484  struct action *action;
4485 
4486  if (nsteps == 0 || (nsteps < 0 && nsteps != SINGLESTEP_INFINITE
4487  && nsteps != SINGLESTEP_NEXTBP)) {
4488  errno = EINVAL;
4489  return NULL;
4490  }
4491 
4492  action = (struct action *)calloc(1,sizeof(struct action));
4493  if (!action) {
4494  verror("could not malloc action: %s\n",strerror(errno));
4495  return NULL;
4496  }
4497 
4498  action->type = ACTION_SINGLESTEP;
4499  action->whence = ACTION_UNSCHED;
4500  INIT_LIST_HEAD(&action->action);
4501 
4502  action->steps = nsteps;
4503 
4504  RHOLD(action,action);
4505 
4506  return action;
4507 }
4508 
4509 struct action *action_code(char *buf,uint32_t buflen,action_flag_t flags) {
4510  struct action *action;
4511 
4512  action = (struct action *)calloc(1,sizeof(struct action));
4513  if (!action) {
4514  verror("could not malloc action: %s\n",strerror(errno));
4515  return NULL;
4516  }
4517 
4518  action->type = ACTION_CUSTOMCODE;
4519  action->whence = ACTION_UNSCHED;
4520  INIT_LIST_HEAD(&action->action);
4521 
4522  action->detail.code.buf = malloc(buflen);
4523  memcpy(action->detail.code.buf,buf,buflen);
4524  action->detail.code.buflen = buflen;
4525  action->detail.code.flags = flags;
4526 
4527  RHOLD(action,action);
4528 
4529  return action;
4530 }
4531 
4533  struct action *action;
4534 
4535  action = (struct action *)calloc(1,sizeof(struct action));
4536  if (!action) {
4537  verror("could not malloc action: %s\n",strerror(errno));
4538  return NULL;
4539  }
4540 
4541  action->type = ACTION_REGMOD;
4542  action->whence = ACTION_UNSCHED;
4543  INIT_LIST_HEAD(&action->action);
4544 
4545  action->detail.regmod.regnum = regnum;
4546  action->detail.regmod.regval = regval;
4547 
4548  RHOLD(action,action);
4549 
4550  return action;
4551 }
4552 
4553 struct action *action_memmod(ADDR dest,char *data,uint32_t len) {
4554  struct action *action;
4555 
4556  action = (struct action *)calloc(1,sizeof(struct action));
4557  if (!action) {
4558  verror("could not malloc action: %s\n",strerror(errno));
4559  return NULL;
4560  }
4561 
4562  action->type = ACTION_MEMMOD;
4563  action->whence = ACTION_UNSCHED;
4564  INIT_LIST_HEAD(&action->action);
4565 
4566  action->detail.memmod.destaddr = dest;
4567  action->detail.memmod.data = malloc(len);
4568  memcpy(action->detail.memmod.data,data,len);
4569  action->detail.memmod.len = len;
4570 
4571  RHOLD(action,action);
4572 
4573  return action;
4574 }
4575 
4576 REFCNT action_release(struct action *action) {
4577  REFCNT refcnt;
4578  RPUT(action,action,action,refcnt);
4579  return refcnt;
4580 }
4581 
4582 static void action_finish_handling(struct action *action,
4583  struct thread_action_context *tac) {
4584  REFCNT trefcnt;
4585 
4586  /*
4587  * If the action is oneshot, cancel it.
4588  */
4589  if (action->whence == ACTION_ONESHOT) {
4590  if (tac)
4591  RPUT(action,action,tac,trefcnt);
4592 
4593  action_cancel(action);
4594  }
4595 }
4596 
4597 /*
4598  * Destroy an action (and cancel it first if necessary!).
4599  */
4600 REFCNT action_free(struct action *action,int force) {
4601  int retval = action->refcnt;
4602 
4603  if (action->probe) {
4604  if (action_cancel(action)) {
4605  verror("could not cancel action; cannot destroy!\n");
4606  return 1;
4607  }
4608  }
4609 
4610  if (retval) {
4611  if (!force) {
4612  vwarn("cannot free action (%d refs)!\n",retval);
4613  return retval;
4614  }
4615  else {
4616  verror("forced free action (%d refs)\n",retval);
4617  }
4618  }
4619 
4620  if (action->type == ACTION_CUSTOMCODE
4621  && action->detail.code.buf) {
4622  free(action->detail.code.buf);
4623  }
4624  else if (action->type == ACTION_MEMMOD
4625  && action->detail.memmod.len) {
4626  free(action->detail.memmod.data);
4627  }
4628 
4629  free(action);
4630 
4631  return retval;
4632 }
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:4457
#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:903
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:1800
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:2654
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:2267
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:2211
#define PROBE_SAFE_OP(probe, op)
Definition: probe.h:28
struct lsymbol * lsymbol
Definition: target.h:1017
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:4600
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:4661
result_t probepoint_ss_handler(struct target *target, struct target_thread *tthread, struct probepoint *probepoint)
Definition: probe.c:3023
#define SINGLESTEP_INFINITE
Definition: probe_api.h:871
uint32_t no_adjust_bp_ip
Definition: target_api.h:2465
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:4723
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:2418
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:1843
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
static uint64_t unsigned int i
probepoint_state_t state
Definition: probe.h:220
thread_resumeat_t
Definition: target_api.h:2069
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:1760
ADDR addr
Definition: probe.h:218
REFCNT action_release(struct action *action)
Definition: probe.c:4576
struct probepoint * target_lookup_probepoint(struct target *target, struct target_thread *tthread, ADDR addr)
Definition: target.c:4636
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:5325
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:1027
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:4768
struct action * __get_next_complex_action(struct probepoint *probepoint, struct action *current)
Definition: probe.c:3790
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:1814
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:4687
ADDR start
Definition: target.h:994
int target_detach_action(struct target *target, struct action *action)
Definition: target.c:4743
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:1053
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:1903
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:3578
int target_is_open(struct target *target)
Definition: target_api.c:1042
struct action * action_memmod(ADDR dest, char *data, uint32_t len)
Definition: probe.c:4553
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:2465
struct target_thread * thread
Definition: probe.h:242
void free(void *ptr)
Definition: debugserver.c:207
REGVAL target_read_reg(struct target *target, tid_t tid, REG reg)
Definition: target_api.c:1132
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:1822
#define SYMBOL_IS_LABEL(sym)
int(* instr_can_switch_context)(struct target *target, ADDR addr)
Definition: target_api.h:3121
tid_t tid
Definition: probe.h:344
int target_singlestep_end(struct target *target, tid_t tid)
Definition: target_api.c:1909
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:4532
struct probepoint * interrupted_ss_probepoint
Definition: target_api.h:2182
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:1884
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:4483
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:2169
int target_attach_probe(struct target *target, struct target_thread *thread, struct probe *probe)
Definition: target.c:4711
#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:2168
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:2603
result_t probepoint_resumeat_handler(struct target *target, struct target_thread *tthread)
Definition: probe.c:3504
struct target_nv_filter * pre_filter
Definition: probe.h:351
void * calloc(size_t nmemb, size_t size)
Definition: debugserver.c:200
result_t post_handler(struct probe *probe, tid_t tid, void *data, struct probe *trigger, struct probe *base)
Definition: spf.c:908
struct target * target
Definition: target_api.h:2078
#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:4509
result_t
Definition: common.h:25
action_flag_t
Definition: probe_api.h:267
int action_cancel(struct action *action)
Definition: probe.c:4413
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:2193
int target_memmod_release(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target.c:4933
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:3080
int target_attach_action(struct target *target, struct action *action)
Definition: target.c:4734
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:1046
int target_disable_sw_breakpoint(struct target *target, tid_t tid, struct target_memmod *mmod)
Definition: target_api.c:1786
#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:1877
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:2548
struct target_memmod * target_insert_sw_breakpoint(struct target *target, tid_t tid, ADDR addr)
Definition: target_api.c:1729
void tpc_free(struct thread_probepoint_context *tpc)
Definition: probe.c:2279
REG spregno
Definition: target_api.h:2507
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:2503
ADDR symbol_addr
Definition: probe.h:249
int target_unset_hw_breakpoint(struct target *target, tid_t tid, REG reg)
Definition: target_api.c:1851
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:2506
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:5348
#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:2605
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:2465
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:1836
GList * sources
Definition: probe.h:383
int id
Definition: target_api.h:2514
void * malloc(size_t size)
Definition: debugserver.c:214
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:4023
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:1060
struct target_thread * blocking_thread
Definition: target_api.h:2693
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:3359
int8_t resumeat
Definition: target_api.h:2081
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:1773
#define SYMBOL_IS_FUNC(sym)
int target_unset_hw_watchpoint(struct target *target, tid_t tid, REG reg)
Definition: target_api.c:1858
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:4119
REG ipregno
Definition: target_api.h:2508
int target_write_reg(struct target *target, tid_t tid, REG reg, REGVAL value)
Definition: target_api.c:1141
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:2452
#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