1 #ifndef __KTAP_TYPES_H__
2 #define __KTAP_TYPES_H__
5 #include <linux/perf_event.h>
15 * The first argument type of kdebug.probe_by_id()
16 * The value is a userspace memory pointer.
18 typedef struct ktap_eventdef_info {
24 typedef struct ktap_parm {
25 char *trunk; /* __user */
28 char **argv; /* __user */
38 * Ioctls that can be done on a ktap fd:
39 * todo: use _IO macro in include/uapi/asm-generic/ioctl.h
41 #define KTAP_CMD_IOC_VERSION ('$' + 0)
42 #define KTAP_CMD_IOC_RUN ('$' + 1)
43 #define KTAP_CMD_IOC_EXIT ('$' + 3)
45 #define KTAP_ENV "_ENV"
47 #define KTAP_VERSION_MAJOR "0"
48 #define KTAP_VERSION_MINOR "4"
50 #define KTAP_VERSION "ktap " KTAP_VERSION_MAJOR "." KTAP_VERSION_MINOR
51 #define KTAP_AUTHOR "Jovi Zhangwei <jovi.zhangwei@gmail.com>"
52 #define KTAP_COPYRIGHT KTAP_VERSION " Copyright (C) 2012-2013, " KTAP_AUTHOR
54 #define MYINT(s) (s[0] - '0')
55 #define VERSION (MYINT(KTAP_VERSION_MAJOR) * 16 + MYINT(KTAP_VERSION_MINOR))
56 #define FORMAT 0 /* this is the official format */
58 #define KTAP_SIGNATURE "\033ktap"
60 /* data to catch conversion errors */
61 #define KTAPC_TAIL "\x19\x93\r\n\x1a\n"
63 /* size in bytes of header of binary files */
64 #define KTAPC_HEADERSIZE (sizeof(KTAP_SIGNATURE) - sizeof(char) + 2 + \
65 6 + sizeof(KTAPC_TAIL) - sizeof(char))
67 typedef long ktap_number;
68 #define kp_number2int(i, n) ((i) = (int)(n))
70 typedef int ktap_instruction;
72 typedef union ktap_gcobject ktap_gcobject;
74 #define CommonHeader ktap_gcobject *next; u8 tt;
76 typedef union ktap_string {
77 int dummy; /* ensures maximum alignment for strings */
80 u8 extra; /* reserved words for short strings; "has hash" for longs */
82 size_t len; /* number of characters in string */
84 /* short string is stored here, just after tsv */
89 typedef int (*ktap_cfunction) (struct ktap_state *ks);
91 typedef struct ktap_value {
93 ktap_gcobject *gc; /* collectable objects */
94 void *p; /* light userdata */
96 ktap_cfunction f; /* light C functions */
97 ktap_number n; /* numbers */
102 typedef ktap_value * StkId;
106 * Description of an upvalue for function prototypes
108 typedef struct ktap_upvaldesc {
109 ktap_string *name; /* upvalue name (for debug information) */
110 u8 instack; /* whether it is in stack */
111 u8 idx; /* index of upvalue (in stack or in outer function's list) */
115 * Description of a local variable for function prototypes
116 * (used for debug information)
118 typedef struct ktap_locvar {
119 ktap_string *varname;
120 int startpc; /* first point where variable is active */
121 int endpc; /* first point where variable is dead */
125 typedef struct ktap_upval {
127 ktap_value *v; /* points to stack or to its own value */
129 ktap_value value; /* the value (when closed) */
130 struct { /* double linked list (when open) */
131 struct ktap_upval *prev;
132 struct ktap_upval *next;
138 #define KTAP_MAX_STACK_ENTRIES 100
140 typedef struct ktap_btrace {
142 unsigned int nr_entries;
143 /* entries stored in here, after nr_entries */
146 typedef struct ktap_closure {
149 struct ktap_proto *p;
150 struct ktap_upval *upvals[1]; /* list of upvalues */
151 ktap_gcobject *gclist;
154 typedef struct ktap_proto {
156 ktap_value *k; /* constants used by the function */
157 ktap_instruction *code;
158 struct ktap_proto **p; /* functions defined inside the function */
159 int *lineinfo; /* map from opcodes to source lines (debug information) */
160 struct ktap_locvar *locvars; /* information about local variables (debug information) */
161 struct ktap_upvaldesc *upvalues; /* upvalue information */
162 ktap_closure *cache; /* last created closure with this prototype */
163 ktap_string *source; /* used for debug information */
164 int sizeupvalues; /* size of 'upvalues' */
165 int sizek; /* size of `k' */
168 int sizep; /* size of `p' */
172 u8 numparams; /* number of fixed parameters */
174 u8 maxstacksize; /* maximum stack used by this function */
179 * information about a call
181 typedef struct ktap_callinfo {
182 StkId func; /* function index in the stack */
183 StkId top; /* top for this function */
184 struct ktap_callinfo *prev, *next; /* dynamic call link */
185 short nresults; /* expected number of results from this function */
189 struct { /* only for ktap functions */
190 StkId base; /* base for this function */
191 const unsigned int *savedpc;
193 struct { /* only for C functions */
194 int ctx; /* context info. in case of yields */
204 typedef struct ktap_tkey {
205 struct ktap_tnode *next; /* for chaining */
210 typedef struct ktap_tnode {
216 typedef struct ktap_stat_data {
223 typedef struct ktap_tab {
226 arch_spinlock_t lock;
228 u8 flags; /* 1<<p means tagmethod(p) is not present */
229 u8 lsizenode; /* log2 of size of `node' array */
230 int sizearray; /* size of `array' array */
231 ktap_value *array; /* array part */
233 ktap_tnode *lastfree; /* any free position is before this position */
235 int with_stats; /* for aggregation table: ptable */
236 ktap_stat_data *sd_arr;
237 ktap_stat_data *sd_rec;
239 ktap_tnode *sorted; /* sorted table, with linked node list */
240 ktap_tnode *sort_head;
242 ktap_gcobject *gclist;
245 #define lmod(s,size) ((int)((s) & ((size)-1)))
248 typedef struct ktap_ptab {
250 ktap_tab **tbl; /* percpu table */
254 typedef struct ktap_stringtable {
255 ktap_gcobject **hash;
260 #ifdef CONFIG_KTAP_FFI
261 typedef int csymbol_id;
262 typedef struct csymbol csymbol;
264 /* global ffi state maintained in each ktap vm instance */
265 typedef struct ffi_state {
271 /* instance of csymbol */
272 typedef struct ktap_cdata {
277 void *p; /* pointer address */
278 void *st; /* struct member data */
283 typedef struct ktap_stats {
291 #define KTAP_STATS(ks) this_cpu_ptr(G(ks)->stats)
294 KTAP_PERCPU_DATA_STATE,
295 KTAP_PERCPU_DATA_STACK,
296 KTAP_PERCPU_DATA_BUFFER,
297 KTAP_PERCPU_DATA_BUFFER2,
298 KTAP_PERCPU_DATA_BTRACE,
303 typedef struct ktap_global_state {
304 ktap_stringtable strt; /* hash table for strings */
306 unsigned int seed; /* randonized seed for hashes */
308 ktap_gcobject *allgc; /* list of all collectable objects */
310 ktap_upval uvhead; /* head of double-linked list of all open upvalues */
312 struct ktap_state *mainthread;
314 /* global percpu data(like stack) */
315 void __percpu *pcpu_data[KTAP_PERCPU_DATA_MAX][PERF_NR_CONTEXTS];
317 int __percpu *recursion_context[PERF_NR_CONTEXTS];
319 arch_spinlock_t str_lock; /* string opertion lock */
323 struct task_struct *trace_task;
324 cpumask_var_t cpumask;
325 struct ring_buffer *buffer;
326 struct dentry *trace_pipe_dentry;
327 int nr_builtin_cfunction;
328 ktap_value *cfunction_tbl;
329 struct task_struct *task;
331 struct list_head timers;
332 struct list_head probe_events_head;
335 ktap_closure *trace_end_closure;
336 struct ktap_stats __percpu *stats;
337 struct kmem_cache *pevent_cache;
338 #ifdef CONFIG_KTAP_FFI
345 typedef struct ktap_state {
347 ktap_global_state *g;
351 const unsigned long *oldpc;
354 ktap_gcobject *openupval;
355 ktap_callinfo baseci;
357 /* list of temp collectable objects, free when thread exit */
358 ktap_gcobject *gclist;
361 struct ktap_event *current_event;
366 typedef struct gcheader {
371 * Union of all collectable objects
373 union ktap_gcobject {
374 gcheader gch; /* common header */
375 union ktap_string ts;
376 struct ktap_closure cl;
380 struct ktap_upval uv;
381 struct ktap_state th; /* thread */
382 struct ktap_btrace bt; /* backtrace object */
383 #ifdef CONFIG_KTAP_FFI
384 struct ktap_cdata cd;
388 #define gch(o) (&(o)->gch)
390 /* macros to convert a GCObject into a specific value */
391 #define rawgco2ts(o) (&((o)->ts))
393 #define gco2ts(o) (&rawgco2ts(o)->tsv)
394 #define gco2uv(o) (&((o)->uv))
395 #define obj2gco(v) ((ktap_gcobject *)(v))
396 #define check_exp(c, e) (e)
399 /* predefined values in the registry */
400 #define KTAP_RIDX_MAINTHREAD 1
401 #define KTAP_RIDX_GLOBALS 2
402 #define KTAP_RIDX_LAST KTAP_RIDX_GLOBALS
404 #define KTAP_TNONE (-1)
407 #define KTAP_TBOOLEAN 1
408 #define KTAP_TLIGHTUSERDATA 2
409 #define KTAP_TNUMBER 3
410 #define KTAP_TSTRING 4
411 #define KTAP_TSHRSTR (KTAP_TSTRING | (0 << 4)) /* short strings */
412 #define KTAP_TLNGSTR (KTAP_TSTRING | (1 << 4)) /* long strings */
413 #define KTAP_TTABLE 5
414 #define KTAP_TFUNCTION 6
415 #define KTAP_TCLOSURE (KTAP_TFUNCTION | (0 << 4)) /* closure */
416 #define KTAP_TCFUNCTION (KTAP_TFUNCTION | (1 << 4)) /* light C function */
417 #define KTAP_TTHREAD 7
418 #define KTAP_TPROTO 8
419 #define KTAP_TUPVAL 9
420 #define KTAP_TEVENT 10
421 #define KTAP_TBTRACE 11
422 #define KTAP_TPTABLE 12
423 #define KTAP_TSTATDATA 13
424 #define KTAP_TCDATA 14
426 * type number is ok so far, but it may collide later between
427 * 16+ and | (1 << 4), so be careful on this.
430 #define ttype(o) ((o->type) & 0x3F)
431 #define settype(obj, t) ((obj)->type = (t))
433 /* raw type tag of a TValue */
434 #define rttype(o) ((o)->type)
436 /* tag with no variants (bits 0-3) */
437 #define novariant(x) ((x) & 0x0F)
439 /* type tag of a TValue with no variants (bits 0-3) */
440 #define ttypenv(o) (novariant(rttype(o)))
442 #define val_(o) ((o)->val)
443 #define gcvalue(o) (val_(o).gc)
445 #define bvalue(o) (val_(o).b)
446 #define nvalue(o) (val_(o).n)
447 #define hvalue(o) (&val_(o).gc->h)
448 #define phvalue(o) (&val_(o).gc->ph)
449 #define clvalue(o) (&val_(o).gc->cl)
451 #define getstr(ts) (const char *)((ts) + 1)
452 #define eqshrstr(a, b) ((a) == (b))
453 #define rawtsvalue(o) (&val_(o).gc->ts)
454 #define svalue(o) getstr(rawtsvalue(o))
456 #define pvalue(o) (&val_(o).p)
457 #define sdvalue(o) ((ktap_stat_data *)val_(o).p)
458 #define fvalue(o) (val_(o).f)
459 #define evalue(o) (val_(o).p)
460 #define btvalue(o) (&val_(o).gc->bt)
461 #define cdvalue(o) (&val_(o).gc->cd)
463 #define is_nil(o) ((o)->type == KTAP_TNIL)
464 #define is_boolean(o) ((o)->type == KTAP_TBOOLEAN)
465 #define is_false(o) (is_nil(o) || (is_boolean(o) && bvalue(o) == 0))
466 #define is_shrstring(o) ((o)->type == KTAP_TSHRSTR)
467 #define is_string(o) (((o)->type & 0x0F) == KTAP_TSTRING)
468 #define is_number(o) ((o)->type == KTAP_TNUMBER)
469 #define is_table(o) ((o)->type == KTAP_TTABLE)
470 #define is_ptable(o) ((o)->type == KTAP_TPTABLE)
471 #define is_statdata(o) ((o)->type == KTAP_TSTATDATA)
472 #define is_event(o) ((o)->type == KTAP_TEVENT)
473 #define is_btrace(o) ((o)->type == KTAP_TBTRACE)
474 #define is_needclone(o) is_btrace(o)
475 #ifdef CONFIG_KTAP_FFI
476 #define is_cdata(o) ((o)->type == KTAP_TCDATA)
480 #define set_nil(obj) \
481 { ktap_value *io = (obj); io->val.n = 0; settype(io, KTAP_TNIL); }
483 #define set_boolean(obj, x) \
484 { ktap_value *io = (obj); io->val.b = (x); settype(io, KTAP_TBOOLEAN); }
486 #define set_number(obj, x) \
487 { ktap_value *io = (obj); io->val.n = (x); settype(io, KTAP_TNUMBER); }
489 #define set_statdata(obj, x) \
490 { ktap_value *io = (obj); \
491 io->val.p = (x); settype(io, KTAP_TSTATDATA); }
493 #define set_string(obj, x) \
494 { ktap_value *io = (obj); \
495 ktap_string *x_ = (x); \
496 io->val.gc = (ktap_gcobject *)x_; settype(io, x_->tsv.tt); }
498 #define set_closure(obj, x) \
499 { ktap_value *io = (obj); \
500 io->val.gc = (ktap_gcobject *)x; settype(io, KTAP_TCLOSURE); }
502 #define set_cfunction(obj, x) \
503 { ktap_value *io = (obj); val_(io).f = (x); settype(io, KTAP_TCFUNCTION); }
505 #define set_table(obj, x) \
506 { ktap_value *io = (obj); \
507 val_(io).gc = (ktap_gcobject *)(x); settype(io, KTAP_TTABLE); }
509 #define set_ptable(obj, x) \
510 { ktap_value *io = (obj); \
511 val_(io).gc = (ktap_gcobject *)(x); settype(io, KTAP_TPTABLE); }
513 #define set_thread(obj, x) \
514 { ktap_value *io = (obj); \
515 val_(io).gc = (ktap_gcobject *)(x); settype(io, KTAP_TTHREAD); }
517 #define set_event(obj, x) \
518 { ktap_value *io = (obj); val_(io).p = (x); settype(io, KTAP_TEVENT); }
520 #define set_btrace(obj, x) \
521 { ktap_value *io = (obj); \
522 val_(io).gc = (ktap_gcobject *)(x); settype(io, KTAP_TBTRACE); }
524 #ifdef CONFIG_KTAP_FFI
525 #define set_cdata(obj, x) \
526 { ktap_value *io=(obj); \
527 val_(io).gc = (ktap_gcobject *)(x); settype(io, KTAP_TCDATA); }
530 #define set_obj(obj1, obj2) \
531 { const ktap_value *io2 = (obj2); ktap_value *io1 = (obj1); \
532 io1->val = io2->val; io1->type = io2->type; }
534 #define rawequalobj(t1, t2) \
535 (((t1)->type == (t2)->type) && kp_equalobjv(NULL, t1, t2))
537 #define incr_top(ks) {ks->top++;}
539 #define NUMADD(a, b) ((a) + (b))
540 #define NUMSUB(a, b) ((a) - (b))
541 #define NUMMUL(a, b) ((a) * (b))
542 #define NUMDIV(a, b) ((a) / (b))
543 #define NUMUNM(a) (-(a))
544 #define NUMEQ(a, b) ((a) == (b))
545 #define NUMLT(a, b) ((a) < (b))
546 #define NUMLE(a, b) ((a) <= (b))
547 #define NUMISNAN(a) (!NUMEQ((a), (a)))
549 /* todo: floor and pow in kernel */
550 #define NUMMOD(a, b) ((a) % (b))
551 #define NUMPOW(a, b) (pow(a, b))
553 #define ktap_assert(s)
555 #define kp_realloc(ks, v, osize, nsize, t) \
556 ((v) = (t *)kp_reallocv(ks, v, osize * sizeof(t), nsize * sizeof(t)))
558 #define kp_error(ks, args...) \
560 kp_printf(ks, "error: "args); \
566 #define G(ks) (ks->g)
568 void kp_printf(ktap_state *ks, const char *fmt, ...);
569 extern void __kp_puts(ktap_state *ks, const char *str);
570 extern void __kp_bputs(ktap_state *ks, const char *str);
572 #define kp_puts(ks, str) ({ \
573 static const char *trace_printk_fmt \
574 __attribute__((section("__trace_printk_fmt"))) = \
575 __builtin_constant_p(str) ? str : NULL; \
577 if (__builtin_constant_p(str)) \
578 __kp_bputs(ks, trace_printk_fmt); \
580 __kp_puts(ks, str); \
585 * this is used for ktapc tstring operation, tstring need G(ks)->strt
586 * and G(ks)->seed, so ktapc need to init those field
588 #define G(ks) (&dummy_global_state)
589 extern ktap_global_state dummy_global_state;
591 #define kp_printf(ks, args...) printf(args)
592 #define kp_puts(ks, str) printf("%s", str)
593 #define kp_exit(ks) exit(EXIT_FAILURE)
597 #define __maybe_unused __attribute__((unused))
600 * KTAP_QL describes how error messages quote program elements.
601 * CHANGE it if you want a different appearance.
603 #define KTAP_QL(x) "'" x "'"
604 #define KTAP_QS KTAP_QL("%s")
606 #define STRINGIFY(type) #type
608 #endif /* __KTAP_TYPES_H__ */