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.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, 2012, 2013, 2014 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #ifndef __PROBE_H__
20 #define __PROBE_H__
21 
22 #include "common.h"
23 #include "list.h"
24 #include "probe_api.h"
25 
26 #include <glib.h>
27 
28 #define PROBE_SAFE_OP(probe,op) (((probe)->ops && (probe)->ops->op) \
29  ? (probe)->ops->op((probe)) \
30  : 0)
31 #define PROBE_SAFE_OP_ARGS(probe,op,...) (((probe)->ops && (probe)->ops->op) \
32  ? (probe)->ops->op((probe), ## __VA_ARGS__) \
33  : 0)
34 
35 #define LOGDUMPPROBEPOINT(dl,la,lt,pp) \
36  if ((pp)->bsymbol && (pp)->symbol_addr) { \
37  vdebugc((dl),(la),(lt),"probepoint(0x%"PRIxADDR" %s:%+d) ", \
38  (pp)->addr,(pp)->bsymbol->lsymbol->symbol->name, \
39  (pp)->symbol_addr - (pp)->addr); \
40  } \
41  else if ((pp)->bsymbol) { \
42  vdebugc((dl),(la),(lt),"probepoint(0x%"PRIxADDR" %s) ", \
43  (pp)->addr,(pp)->bsymbol->lsymbol->symbol->name); \
44  } \
45  else { \
46  vdebugc((dl),(la),(lt),"probepoint(0x%"PRIxADDR") ", \
47  (pp)->addr); \
48  }
49 
50 #define LOGDUMPPROBEPOINT_NL(dl,la,lt,p) \
51  LOGDUMPPROBEPOINT((dl),(la),(lt),(p)); \
52  vdebugc((dl),(la),(lt),"\n");
53 
54 #define LOGDUMPPROBE(dl,la,lt,p) \
55  vdebugc((dl),(la),(lt),"probe(%s) ",probe->name); \
56  if ((p)->bsymbol) { \
57  vdebugc((dl),(la),(lt),"(on %s) ", \
58  (p)->bsymbol->lsymbol->symbol->name); \
59  } \
60  else { \
61  vdebugc((dl),(la),(lt),"(on <UNKNOWN>) "); \
62  } \
63  if ((p)->probepoint) { \
64  LOGDUMPPROBEPOINT(dl,la,lt,(p)->probepoint); \
65  } \
66  if ((p)->sources) { \
67  vdebugc((dl),(la),(lt)," (%d sources)",g_list_length((p)->sources)); \
68  } \
69  if ((p)->sinks) { \
70  vdebugc((dl),(la),(lt)," (%d sinks)",g_list_length((p)->sinks)); \
71  }
72 
73 #define LOGDUMPPROBE_NL(dl,la,lt,p) \
74  LOGDUMPPROBE((dl),(la),(lt),(p)); \
75  vdebugc((dl),(la),(lt),"\n");
76 
77 /*
78  * probepoint_state_t -- various states of a probe point. Probepoint
79  * state is about how target state is altered to support the
80  * probepoint. For instance, a probepoint may be set, handling, or
81  * disabled (inserting/removing are very temporary states, and are
82  * meaningless since this library is not reentrant).
83  *
84  * Probepoints *must* be handled atomically -- we only can single step
85  * one instruction after the breakpoint is hit. This means that any
86  * action that happens before the one single step (or replaces it) must
87  * be able to do all its dirty work before/during that one single step.
88  * The result of that single step is that the breakpoint *must* be
89  * replaced -- before any other threads have a chance to hit the
90  * breakpoint.
91  *
92  * Fire-and-forget actions, like a command to single step, or a complex
93  * action, like custom code, are still permitted -- but the custom code
94  * action must be a call or jump that is single stepped.
95  */
96 typedef enum {
97  PROBE_INSERTING = 1, /* domain quiescing prior to breakpoint insertion */
98  PROBE_BP_SET, /* breakpoint in place */
99  PROBE_BP_PREHANDLING, /* handling a breakpoint's pre handlers */
100  PROBE_BP_ACTIONHANDLING,/* handling a breakpoint's actions */
101  PROBE_BP_POSTHANDLING, /* handling a breakpoint's post handlers */
102  PROBE_REMOVING, /* domain quiescing prior to breakpoint removal */
103  PROBE_DISABLED, /* breakpoint removal completed */
104  PROBE_ACTION_RUNNING, /* executing an action */
105  PROBE_ACTION_DONE, /* finished an action */
107 
108 /*
109  * Prototypes of the standard breakpoint and single step debug event
110  * handlers. Target implementers probably want to set the bp_handler
111  * and ss_handler of the `struct target_ops` representing their target
112  * type to these functions. Without them, the standard probe API is
113  * useless, unless the target implements similar semantics.
114  */
116  struct target_thread *tthread,
117  struct probepoint *probepoint,
118  int was_stepping);
120  struct target_thread *tthread,
121  struct probepoint *probepoint);
123  struct target_thread *tthread,
124  struct probepoint *probepoint);
125 /*
126  * For targets providing thread control, the probe API may choose to
127  * "pause" the handling of a thread A at a debug exception, if another
128  * thread B already owns the probepoint (i.e., is handling and modifying
129  * it). The thread will not be unpaused, but the probe API will try to
130  * handle it in this function. The idea is that target implementers
131  * should call it just before the target is resumed for additional
132  * monitoring.
133  */
135  struct target_thread *tthread);
136 
137 /*
138  * Local prototypes.
139  */
140 struct probe *__probe_register_addr(struct probe *probe,ADDR addr,
141  struct memrange *range,
142  probepoint_type_t type,
143  probepoint_style_t style,
144  probepoint_whence_t whence,
145  probepoint_watchsize_t watchsize,
146  struct bsymbol *bsymbol,ADDR symbol_addr);
147 
148 /*
149  * A probe point is the address site where an actual break/watch point
150  * is registered. We associate multiple logical probes with a single
151  * probe point.
152  *
153  * Currently, the model is that hardware probepoints are per-thread, and
154  * are only "alive" in the thread that requested them. However,
155  * software probepoints are shared, since memory is shared, amongst
156  * threads. BUT, how this is realized is target-specific. Some targets
157  * are in control of all threads in the target (i.e., ptrace), and they
158  * must pause all threads in the process BEFORE single-stepping past a
159  * probepoint, or handling its actions -- since another thread could
160  * "miss" a breakpoint if the breakpoint is changed into the real
161  * instruction and another thread runs the real thing before the single
162  * step can occur. But in other targets, like the Xen target, we cannot
163  * pause threads. The only thing we can do is disable interrupts while
164  * the single step happens (and this won't disable NMIs, of course).
165  * This effectively pauses all other threads, unless we are single
166  * stepping the context switch itself, or an NMI occurs.
167  *
168  * In any case, the *probepoint* infrastructure does *not* maintain
169  * per-thread probepoint state, nor probe action state. To support
170  * THREAD_BPMODE_STRICT, targets must guarantee that when a probepoint
171  * is being handled, only the thread that hit it first will execute
172  * until we are finished handling the probepoint for that thread.
173  * See also the documentation for THREAD_BPMODE_SEMI_STRICT and
174  * THREAD_BPMODE_LOOSE for less safe modes of operation.
175  *
176  * BUT, we do need to handle multiple probepoint and action states per
177  * thread. Threads could hit one probepoint, and then hit another
178  * before completely handling of the previous one. And we need to track
179  * action contexts per thread probepoint context, AND for single step
180  * actions, we need to track those on a per-thread basis.
181  *
182  * To support these goals, we have the thread_probepoint_context; a new
183  * one of these is pushed onto the thread's probepoint context stack
184  * each time a probepoint is hit by a thread. We also have the
185  * thread_action_context struct, which keeps track of which action is
186  * executing at a probepoint context, AND for single step actions, how
187  * many steps have executed for that action (note that single step
188  * thread_action_contexts are not probepoint-specific -- they accumulate
189  * on a thread's list, and the thread stays in single step mode until
190  * all actions have finished. Handling single step actions is separate
191  * from handling probepoint contexts.
192  */
193 
194 struct target_thread;
195 struct action;
196 
198  struct action *action;
199  int stepped;
200 
201  /* If this context is on a list, this is the element's next/prev ptrs. */
202  struct list_head tac;
203 };
204 
208 
209  /* The currently executing complex action. */
211 
214 };
215 
216 struct probepoint {
217  /* Location of the probe point */
219 
221 
224  /*
225  * If the user registered the probepoint with PROBEPOINT_FASTEST, we
226  * need to save the original style so if it is
227  * unregistered/re-registered, we can still make the FASTEST choice,
228  * instead of sticking with what we got at last registration.
229  */
233 
234  /*
235  * The target context (target,thread) this probe point is associated
236  * with. @thread is only valid if the probepoint is a hardware
237  * probepoint, since right now, only hardware debug registers are
238  * per-thread (i.e., software breakpoints are not, since
239  * fundamentally, threads, share code pages).
240  */
241  struct target *target;
243 
244  /* If this probepoint is associated with a symbol, save it! */
245  struct bsymbol *bsymbol;
246  /* If we have a symbol, save its resolved base addr (which may be
247  * different than the addr above, of course.
248  */
250 
251  /* Always save off which memrange the probe is in. */
252  struct memrange *range;
253 
254  /* list of probes at this probe-point */
256 
257  /*
258  * Lists of actions that may be executed at this probepoint.
259  * Simple actions are REGMOD and MEMMOD, for now.
260  * Complex actions are RETURN, SINGLESTEP, and CUSTOMCODE, for now.
261  *
262  * Simple actions can all be run immediately post-breakpoint,
263  * pre-singlestep, because they just quickly change machine state.
264  *
265  * Complex actions either run back-to-back, or there is only one of
266  * them.
267  */
271 
272  /*
273  * The target_memmod supporting this probepoint.
274  */
276 
277  /*
278  * If we ever have to change the instruction at the probepoint
279  * address while we handle it, or disable the hw breakpoint
280  * register, this is the thread probepoint context struct with the
281  * info we need -- and says which thread "holds" this probepoint.
282  * Only one thread can be adjusting a probepoint at once.
283  *
284  * We have to "share" the probepoint anytime we can't boost either
285  * the one instruction we have to single step, or if we had to run
286  * action code at the breakpoint instead of somewhere else in the
287  * target.
288  *
289  * So, this field is only valid when @state is PROBE_BP_PREHANDLING,
290  * PROBE_BP_ACTIONHANDLING, PROBE_BP_POSTHANDLING, and
291  * PROBE_ACTION_RUNNING (if the action is not boosted -- i.e., is
292  * happening inline at the breakpoint).
293  */
295 
296  /* If this is a hardware-assisted probepoint, this is the debug
297  * register number.
298  */
301 
302  /* If the instruction at this probepoint might context switch, mark
303  * it here. We try to set this in __probepoint_create.
304  */
306 };
307 
308 struct probe {
309  /*
310  * This is a per-target id.
311  */
312  int id;
313 
314  char *name;
315 
316  struct probe_ops *ops;
317 
318  void *priv;
319 
320  /*
321  * This is controlled by the functions in probe_value.c . It is
322  * always a hash of tid_t to <something>. But the <something> might
323  * either be a struct probe_value * directly, OR it could be a
324  * GSList * stack of struct probe_value * (this handles reentrant
325  * symbols).
326  *
327  * In any case, it is controlled by probe_ops for this probe. Users
328  * must access it only through the probe_value*() functions.
329  */
330  GHashTable *values;
331 
332  /*
333  * The target context this probe is associated with.
334  *
335  * NB: we store the tid the probe was created with separately from
336  * the thread the target_lookup_thread(target,tid) gave us --
337  * because TID_GLOBAL might map to a real thread with a tid that is
338  * real, and is not TID_GLOBAL. We need to know if this was
339  * *supposed* to be a TID_GLOBAL probe so we can do pre/post handler
340  * filtering appropriately.
341  */
342  struct target *target;
345 
346  /* The target probe-point */
348 
349  /* User handler to run before probe-point is executed */
352 
353  /* User handler to run after probe-point is executed */
356 
358 
360 
361  /* True when the vmprobe is enabled */
362  uint8_t enabled;
363 
364  uint8_t autofree;
365 
366  /*
367  * True when the target is tracking this probe; such probes will be
368  * automatically freed on target_free().
369  *
370  * This is useful for cleanup on unexpected target exit, or
371  * application crash. BUT, if you are building probe libraries and
372  * use lower-level probes to build higher-level probes, you should
373  * only track the higher-level probes; those should free/unregister
374  * the probes they use internally themselves.
375  */
376  uint8_t tracked;
377 
378  /* Link to the probe list */
379  struct list_head probe;
380 
381  /* A list of probes we listen to.
382  */
383  GList *sources;
384 
385  /* A list of "listening" probes.
386  */
387  GList *sinks;
388 
389  struct bsymbol *bsymbol;
390 };
391 
392 struct action {
393  /*
394  * This is a per-target id.
395  */
396  int id;
397 
398  /*
399  * Since actions can be on shared probepoints, and might be being
400  * performed on multiple threads at once (i.e., one thread starts a
401  * single step action, is switched out, and another thread starts
402  * the same single step action before the first thread can finish)
403  */
405 
408 
409  union {
410  struct {
412  int8_t prologue:1,
416  } ret;
417  struct {
418  unsigned char *buf;
419  uint32_t buflen;
421  unsigned int instr_count;
422  } code;
423  struct {
426  } regmod;
427  struct {
429  unsigned char *data;
430  uint32_t len;
431  } memmod;
432  } detail;
433 
434  int boosted;
435  int obviates;
436 
439 
440  int steps;
442 
443  /*
444  * An action can only be attached to one probepoint at a time.
445  *
446  * An action is on a probepoint's list, but it is added for a probe
447  * attached to that probepoint.
448  */
450  struct probe *probe;
451 
452  struct target *target;
453 };
454 
455 /* The target_api code needs to call this, but we don't want it exposed
456  * to users. So it's here.
457  */
459 
460 #endif /* __PROBE_H__ */
action_handler_t handler
Definition: probe.h:437
struct memrange * range
Definition: probe.h:252
int can_switch_context
Definition: probe.h:305
uint32_t len
Definition: probe.h:430
void probepoint_free_ext(struct probepoint *probepoint)
Definition: probe.c:570
probe_handler_t pre_handler
Definition: probe.h:350
int32_t tid_t
Definition: common.h:36
struct probe_ops * ops
Definition: probe.h:316
probepoint_type_t
Definition: probe_api.h:213
ADDR start_addr
Definition: probe.h:441
int id
Definition: probe.h:396
action_whence_t
Definition: probe_api.h:257
probepoint_style_t style
Definition: probe.h:223
Definition: probe.h:392
GList * sinks
Definition: probe.h:387
char * name
Definition: probe.h:314
struct target_nv_filter * post_filter
Definition: probe.h:355
probepoint_state_t state
Definition: probe.h:220
struct target_thread * thread
Definition: probe.h:206
ADDR addr
Definition: probe.h:218
probepoint_state_t
Definition: probe.h:96
REGVAL retval
Definition: probe.h:411
probepoint_whence_t
Definition: probe_api.h:234
int obviates
Definition: probe.h:435
struct target * target
Definition: probe.h:241
uint32_t buflen
Definition: probe.h:419
result_t probepoint_bp_handler(struct target *target, struct target_thread *tthread, struct probepoint *probepoint, int was_stepping)
Definition: probe.c:2593
ADDR destaddr
Definition: probe.h:428
struct action * action
Definition: probe.h:198
struct target_memmod * mmod
Definition: probe.h:275
Definition: list.h:51
struct target_nv_filter * thread_filter
Definition: probe.h:357
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
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 prologue_sp_offset
Definition: probe.h:415
result_t probepoint_ss_handler(struct target *target, struct target_thread *tthread, struct probepoint *probepoint)
Definition: probe.c:3023
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
struct target_thread * thread
Definition: probe.h:242
tid_t tid
Definition: probe.h:344
struct target * target
Definition: probe.h:452
unsigned char * data
Definition: probe.h:429
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 debugregdisabled
Definition: probe.h:300
probepoint_watchsize_t
Definition: probe_api.h:241
action_whence_t whence
Definition: probe.h:407
result_t probepoint_interrupted_ss_handler(struct target *target, struct target_thread *tthread, struct probepoint *probepoint)
Definition: probe.c:3359
struct action::@17::@19 code
unsigned char * buf
Definition: probe.h:418
unsigned int instr_count
Definition: probe.h:421
struct list_head complex_actions
Definition: probe.h:269
struct list_head tac
Definition: probe.h:202
struct action::@17::@20 regmod
Definition: probe.h:308
struct thread_action_context tac
Definition: probe.h:210
struct target_thread * thread
Definition: probe.h:343
GHashTable * values
Definition: probe.h:330
int8_t prologue_has_sp_offset
Definition: probe.h:412
struct thread_probepoint_context * tpc
Definition: probe.h:294
struct target_nv_filter * pre_filter
Definition: probe.h:351
int8_t prologue_uses_bp
Definition: probe.h:412
result_t
Definition: common.h:25
action_flag_t
Definition: probe_api.h:267
uint32_t REGVAL
Definition: common.h:66
uint8_t enabled
Definition: probe.h:362
int steps
Definition: probe.h:440
uint8_t autofree
Definition: probe.h:364
int8_t REG
Definition: common.h:93
probepoint_style_t
Definition: probe_api.h:228
struct target * target
Definition: probe.h:342
uint32_t ADDR
Definition: common.h:64
ADDR symbol_addr
Definition: probe.h:249
struct bsymbol * bsymbol
Definition: probe.h:245
probepoint_watchsize_t watchsize
Definition: probe.h:232
result_t probepoint_resumeat_handler(struct target *target, struct target_thread *tthread)
Definition: probe.c:3504
uint32_t REFCNT
Definition: common.h:124
struct bsymbol * bsymbol
Definition: probe.h:389
int boosted
Definition: probe.h:434
REG debugregnum
Definition: probe.h:299
int id
Definition: probe.h:312
REG regnum
Definition: probe.h:424
GList * sources
Definition: probe.h:383
void * handler_data
Definition: probe.h:438
struct action::@17::@18 ret
struct list_head simple_actions
Definition: probe.h:268
struct list_head probes
Definition: probe.h:255
void * handler_data
Definition: probe.h:359
void * priv
Definition: probe.h:318
action_type_t
Definition: probe_api.h:249
uint8_t tracked
Definition: probe.h:376
probepoint_style_t orig_style
Definition: probe.h:230
struct probe * probe
Definition: probe.h:450
action_type_t type
Definition: probe.h:406
probe_handler_t post_handler
Definition: probe.h:354
probepoint_type_t type
Definition: probe.h:222
struct probepoint * probepoint
Definition: probe.h:347
int8_t prologue
Definition: probe.h:412
struct probepoint * probepoint
Definition: probe.h:207
probepoint_whence_t whence
Definition: probe.h:231