include/ktap_types.h - ktap

Data types defined

Functions defined

Macros defined

Source code

  1. /*
  2. * ktap types definition.
  3. *
  4. * Copyright (C) 2012-2014 Jovi Zhangwei <jovi.zhangwei@gmail.com>.
  5. * Copyright (C) 2005-2014 Mike Pall.
  6. * Copyright (C) 1994-2008 Lua.org, PUC-Rio.
  7. */

  8. #ifndef __KTAP_TYPES_H__
  9. #define __KTAP_TYPES_H__

  10. #ifdef __KERNEL__
  11. #include <linux/perf_event.h>
  12. #else
  13. typedef char u8;
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdint.h>
  18. typedef int ptrdiff_t;
  19. #endif

  20. #include "../include/ktap_bc.h"

  21. /* Various VM limits. */
  22. #define KP_MAX_MEMPOOL_SIZE    10000    /* Max. mempool size(Kbytes). */
  23. #define KP_MAX_STR    512        /* Max. string length. */
  24. #define KP_MAX_STRNUM    9999        /* Max. string number. */

  25. #define KP_MAX_STRTAB    (1<<26)        /* Max. string table size. */
  26. #define KP_MAX_HBITS    26        /* Max. hash bits. */
  27. #define KP_MAX_ABITS    28        /* Max. bits of array key. */
  28. #define KP_MAX_ASIZE    ((1<<(KP_MAX_ABITS-1))+1/* Max. array part size. */
  29. #define KP_MAX_COLOSIZE    16        /* Max. elems for colocated array. */

  30. #define KP_MAX_LINE    1000        /* Max. source code line number. */
  31. #define KP_MAX_XLEVEL    200        /* Max. syntactic nesting level. */
  32. #define KP_MAX_BCINS    (1<<26)        /* Max. # of bytecode instructions. */
  33. #define KP_MAX_SLOTS    250        /* Max. # of slots in a ktap func. */
  34. #define KP_MAX_LOCVAR    200        /* Max. # of local variables. */
  35. #define KP_MAX_UPVAL    60        /* Max. # of upvalues. */

  36. #define KP_MAX_CACHED_CFUNCTION    128    /* Max. cached global cfunction */

  37. #define KP_MAX_STACK_DEPTH    50    /* Max. stack depth */

  38. /*
  39. * The first argument type of kdebug.trace_by_id()
  40. * The value is a userspace memory pointer.
  41. * Maybe embed it into the trunk file in future.
  42. */
  43. typedef struct ktap_eventdesc {
  44.     int nr;  /* the number of events id */
  45.     int *id_arr; /* events id array */
  46.     char *filter;
  47. } ktap_eventdesc_t;


  48. /* ktap option for each script */
  49. typedef struct ktap_option {
  50.     char *trunk; /* __user */
  51.     int trunk_len;
  52.     int argc;
  53.     char **argv; /* __user */
  54.     int verbose;
  55.     int trace_pid;
  56.     int workload;
  57.     int trace_cpu;
  58.     int print_timestamp;
  59.     int quiet;
  60.     int dry_run;
  61. } ktap_option_t;

  62. /*
  63. * Ioctls that can be done on a ktap fd:
  64. * todo: use _IO macro in include/uapi/asm-generic/ioctl.h
  65. */
  66. #define KTAP_CMD_IOC_VERSION        ('$' + 0)
  67. #define KTAP_CMD_IOC_RUN        ('$' + 1)
  68. #define KTAP_CMD_IOC_EXIT        ('$' + 3)

  69. #define KTAP_VERSION_MAJOR       "0"
  70. #define KTAP_VERSION_MINOR       "4"

  71. #define KTAP_VERSION    "ktap " KTAP_VERSION_MAJOR "." KTAP_VERSION_MINOR
  72. #define KTAP_AUTHOR    "Jovi Zhangwei <jovi.zhangwei@gmail.com>"
  73. #define KTAP_COPYRIGHT  KTAP_VERSION "  Copyright (C) 2012-2014, " KTAP_AUTHOR

  74. #define MYINT(s)        (s[0] - '0')
  75. #define VERSION         (MYINT(KTAP_VERSION_MAJOR) * 16 + MYINT(KTAP_VERSION_MINOR))

  76. typedef long ktap_number;
  77. typedef int ktap_instr_t;
  78. typedef union ktap_obj ktap_obj_t;

  79. struct ktap_state;
  80. typedef int (*ktap_cfunction) (struct ktap_state *ks);

  81. /* ktap_val_t is basic value type in ktap stack, for reference all objects */
  82. typedef struct ktap_val {
  83.     union {
  84.         ktap_obj_t *gc;        /* collectable objects, str/tab/... */
  85.         void *p;        /* light userdata */
  86.         ktap_cfunction f;    /* light C functions */
  87.         ktap_number n;        /* numbers */
  88.         struct {
  89.             uint16_t depth;    /* stack depth */
  90.             uint16_t skip;    /* skip stack entries */
  91.         } stack;
  92.     } val;
  93.     union {
  94.         int type;        /* type for above val */
  95.         const unsigned int *pcr;/* Overlaps PC for ktap frames.*/
  96.     };
  97. } ktap_val_t;

  98. typedef ktap_val_t *StkId;

  99. #define GCHeader ktap_obj_t *nextgc; u8 gct;

  100. typedef struct ktap_str {
  101.     GCHeader;
  102.     u8 reserved;  /* Used by lexer for fast lookup of reserved words. */
  103.     u8 extra;
  104.     unsigned int hash;
  105.     int len;  /* number of characters in string */
  106. } ktap_str_t;

  107. typedef struct ktap_upval {
  108.     GCHeader;
  109.     uint8_t closed;    /* Set if closed (i.e. uv->v == &uv->u.value). */
  110.     uint8_t immutable;    /* Immutable value. */
  111.     union {
  112.         ktap_val_t tv; /* If closed: the value itself. */
  113.         struct { /* If open: double linked list, anchored at thread. */
  114.             struct ktap_upval *prev;
  115.             struct ktap_upval *next;
  116.         };
  117.     };
  118.     ktap_val_t *v;  /* Points to stack slot (open) or above (closed). */
  119. } ktap_upval_t;


  120. typedef struct ktap_func {
  121.     GCHeader;
  122.     u8 nupvalues;
  123.     BCIns *pc;
  124.     struct ktap_proto *p;
  125.     struct ktap_upval *upvals[1];  /* list of upvalues */
  126. } ktap_func_t;

  127. typedef struct ktap_proto {
  128.     GCHeader;
  129.     uint8_t numparams;    /* Number of parameters. */
  130.     uint8_t framesize;    /* Fixed frame size. */
  131.     int sizebc;        /* Number of bytecode instructions. */
  132.     ktap_obj_t *gclist;
  133.     void *k;    /* Split constant array (points to the middle). */
  134.     void *uv;    /* Upvalue list. local slot|0x8000 or parent uv idx. */
  135.     int sizekgc;    /* Number of collectable constants. */
  136.     int sizekn;    /* Number of lua_Number constants. */
  137.     int sizept;    /* Total size including colocated arrays. */
  138.     uint8_t sizeuv;    /* Number of upvalues. */
  139.     uint8_t flags;    /* Miscellaneous flags (see below). */

  140.     /* --- The following fields are for debugging/tracebacks only --- */
  141.     ktap_str_t *chunkname;    /* Chunk name this function was defined in. */
  142.     BCLine firstline;    /* First line of the function definition. */
  143.     BCLine numline;    /* Number of lines for the function definition. */
  144.     void *lineinfo;    /* Compressed map from bytecode ins. to source line. */
  145.     void *uvinfo;    /* Upvalue names. */
  146.     void *varinfo;    /* Names and compressed extents of local variables. */
  147. } ktap_proto_t;

  148. /* Flags for prototype. */
  149. #define PROTO_CHILD        0x01    /* Has child prototypes. */
  150. #define PROTO_VARARG        0x02    /* Vararg function. */
  151. #define PROTO_FFI        0x04    /* Uses BC_KCDATA for FFI datatypes. */
  152. #define PROTO_NOJIT        0x08    /* JIT disabled for this function. */
  153. #define PROTO_ILOOP        0x10    /* Patched bytecode with ILOOP etc. */
  154. /* Only used during parsing. */
  155. #define PROTO_HAS_RETURN    0x20    /* Already emitted a return. */
  156. #define PROTO_FIXUP_RETURN    0x40    /* Need to fixup emitted returns. */
  157. /* Top bits used for counting created closures. */
  158. #define PROTO_CLCOUNT        0x20    /* Base of saturating 3 bit counter. */
  159. #define PROTO_CLC_BITS        3
  160. #define PROTO_CLC_POLY        (3*PROTO_CLCOUNT/* Polymorphic threshold. */

  161. #define PROTO_UV_LOCAL        0x8000    /* Upvalue for local slot. */
  162. #define PROTO_UV_IMMUTABLE    0x4000    /* Immutable upvalue. */

  163. #define proto_kgc(pt, idx)    (((ktap_obj_t *)(pt)->k)[idx])
  164. #define proto_bc(pt)        ((BCIns *)((char *)(pt) + sizeof(ktap_proto_t)))
  165. #define proto_bcpos(pt, pc)    ((BCPos)((pc) - proto_bc(pt)))
  166. #define proto_uv(pt)        ((uint16_t *)(pt)->uv)

  167. #define proto_chunkname(pt)    ((pt)->chunkname)
  168. #define proto_lineinfo(pt)    ((const void *)(pt)->lineinfo)
  169. #define proto_uvinfo(pt)    ((const uint8_t *)(pt)->uvinfo)
  170. #define proto_varinfo(pt)    ((const uint8_t *)(pt)->varinfo)


  171. typedef struct ktap_node_t {
  172.     ktap_val_t val; /* Value object. Must be first field. */
  173.     ktap_val_t key; /* Key object. */
  174.     struct ktap_node_t *next;  /* hash chain */
  175. } ktap_node_t;

  176. /* ktap_tab */
  177. typedef struct ktap_tab {
  178.     GCHeader;
  179. #ifdef __KERNEL__
  180.     arch_spinlock_t lock;
  181. #endif
  182.     ktap_val_t *array;    /* Array part. */
  183.     ktap_node_t *node;    /* Hash part. */
  184.     ktap_node_t *freetop; /* any free position is before this position */

  185.     uint32_t asize;        /* Size of array part (keys [0, asize-1]). */
  186.     uint32_t hmask;        /* log2 of size of `node' array */

  187.     uint32_t hnum;        /* number of all nodes */

  188.     ktap_obj_t *gclist;
  189. } ktap_tab_t;

  190. #ifdef CONFIG_KTAP_FFI
  191. typedef int csymbol_id;
  192. typedef uint64_t cdata_number;
  193. typedef struct csymbol csymbol;

  194. /* global ffi state maintained in each ktap vm instance */
  195. typedef struct ffi_state {
  196.     ktap_tab_t *ctable;
  197.     int csym_nr;
  198.     csymbol *csym_arr;
  199. } ffi_state;

  200. /* instance of csymbol */
  201. typedef struct ktap_cdata {
  202.     GCHeader;
  203.     csymbol_id id;
  204.     union {
  205.         cdata_number i;
  206.         struct {
  207.             void *addr;
  208.             int nmemb;    /* number of memory block */
  209.         } p;            /* pointer data */
  210.         void *rec;        /* struct member or union data */
  211.     } u;
  212. } ktap_cdata_t;
  213. #endif

  214. typedef struct ktap_stats {
  215.     int mem_allocated;
  216.     int nr_mem_allocate;
  217.     int events_hits;
  218.     int events_missed;
  219. } ktap_stats_t;

  220. #define KTAP_STATS(ks)    this_cpu_ptr(G(ks)->stats)


  221. #define KTAP_RUNNING    0 /* normal running state */
  222. #define KTAP_TRACE_END    1 /* running in trace_end function */
  223. #define KTAP_EXIT    2 /* normal exit, set when call exit() */
  224. #define KTAP_ERROR    3 /* error state, called by kp_error */

  225. typedef struct ktap_global_state {
  226.     void *mempool;        /* string memory pool */
  227.     void *mp_freepos;    /* free position in memory pool */
  228.     int mp_size;        /* memory pool size */
  229. #ifdef __KERNEL__
  230.     arch_spinlock_t mp_lock;/* mempool lock */
  231. #endif

  232.     ktap_str_t **strhash;    /* String hash table (hash chain anchors). */
  233.     int strmask;        /* String hash mask (size of hash table-1). */
  234.     int strnum;        /* Number of strings in hash table. */
  235. #ifdef __KERNEL__
  236.     arch_spinlock_t str_lock; /* string operation lock */
  237. #endif

  238.     ktap_val_t registry;
  239.     ktap_tab_t *gtab;    /* global table contains cfunction and args */
  240.     ktap_obj_t *allgc; /* list of all collectable objects */
  241.     ktap_upval_t uvhead; /* head of list of all open upvalues */

  242.     struct ktap_state *mainthread; /*main state */
  243.     int state; /* status of ktapvm, KTAP_RUNNING, KTAP_TRACE_END, etc */
  244. #ifdef __KERNEL__
  245.     /* reserved global percpu data */
  246.     void __percpu *percpu_state[PERF_NR_CONTEXTS];
  247.     void __percpu *percpu_print_buffer[PERF_NR_CONTEXTS];
  248.     void __percpu *percpu_temp_buffer[PERF_NR_CONTEXTS];

  249.     /* for recursion tracing check */
  250.     int __percpu *recursion_context[PERF_NR_CONTEXTS];

  251.     ktap_option_t *parm; /* ktap options */
  252.     pid_t trace_pid;
  253.     struct task_struct *trace_task;
  254.     cpumask_var_t cpumask;
  255.     struct ring_buffer *buffer;
  256.     struct dentry *trace_pipe_dentry;
  257.     struct task_struct *task;
  258.     int trace_enabled;
  259.     int wait_user; /* flag to indicat waiting user consume content */

  260.     struct list_head timers; /* timer list */
  261.     struct ktap_stats __percpu *stats; /* memory allocation stats */
  262.     struct list_head events_head; /* probe event list */

  263.     ktap_func_t *trace_end_closure; /* trace_end closure */
  264. #ifdef CONFIG_KTAP_FFI
  265.     ffi_state  ffis;
  266. #endif

  267.     /* C function table for fast call */
  268.     int nr_builtin_cfunction;
  269.     ktap_cfunction gfunc_tbl[KP_MAX_CACHED_CFUNCTION];
  270. #endif
  271. } ktap_global_state_t;


  272. typedef struct ktap_state {
  273.     ktap_global_state_t *g;    /* global state */
  274.     int stop;        /* don't enter tracing handler if stop is 1 */
  275.     StkId top;        /* stack top */
  276.     StkId func;        /* execute light C function */
  277.     StkId stack_last;    /* last stack pointer */
  278.     StkId stack;        /* ktap stack, percpu pre-reserved */
  279.     ktap_upval_t *openupval;/* opened upvals list */

  280. #ifdef __KERNEL__
  281.     /* current fired event which allocated on stack */
  282.     struct ktap_event_data *current_event;
  283. #endif
  284. } ktap_state_t;

  285. #define G(ks)   (ks->g)

  286. typedef struct ktap_rawobj {
  287.     GCHeader;
  288.     void *v;
  289. } ktap_rawobj;

  290. /*
  291. * Union of all collectable objects
  292. */
  293. union ktap_obj {
  294.     struct { GCHeader } gch;
  295.     struct ktap_str ts;
  296.     struct ktap_func fn;
  297.     struct ktap_tab h;
  298.     struct ktap_proto pt;
  299.     struct ktap_upval uv;
  300.     struct ktap_state th;  /* thread */
  301.     struct ktap_rawobj rawobj;
  302. #ifdef CONFIG_KTAP_FFI
  303.     struct ktap_cdata cd;
  304. #endif
  305. };

  306. #define gch(o)            (&(o)->gch)

  307. /* macros to convert a ktap_obj_t into a specific value */
  308. #define gco2ts(o)        (&((o)->ts))
  309. #define gco2uv(o)        (&((o)->uv))
  310. #define obj2gco(v)        ((ktap_obj_t *)(v))

  311. /* predefined values in the registry */
  312. #define KTAP_RIDX_GLOBALS    1
  313. #define KTAP_RIDX_LAST        KTAP_RIDX_GLOBALS

  314. /* ktap object types */
  315. #define KTAP_TNIL        (~0u)
  316. #define KTAP_TFALSE        (~1u)
  317. #define KTAP_TTRUE        (~2u)
  318. #define KTAP_TNUM        (~3u)
  319. #define KTAP_TLIGHTUD        (~4u)
  320. #define KTAP_TSTR        (~5u)
  321. #define KTAP_TUPVAL        (~6u)
  322. #define KTAP_TTHREAD        (~7u)
  323. #define KTAP_TPROTO        (~8u)
  324. #define KTAP_TFUNC        (~9u)
  325. #define KTAP_TCFUNC        (~10u)
  326. #define KTAP_TCDATA        (~11u)
  327. #define KTAP_TTAB        (~12u)
  328. #define KTAP_TUDATA        (~13u)

  329. /* Specfic types */
  330. #define KTAP_TEVENTSTR        (~14u) /* argstr */
  331. #define KTAP_TKSTACK        (~15u) /* stack(), not intern to string yet */
  332. #define KTAP_TKIP        (~16u) /* kernel function ip addres */
  333. #define KTAP_TUIP        (~17u) /* userspace function ip addres */

  334. /* This is just the canonical number type used in some places. */
  335. #define KTAP_TNUMX        (~18u)


  336. #define itype(o)        ((o)->type)
  337. #define setitype(o, t)        ((o)->type = (t))

  338. #define val_(o)            ((o)->val)
  339. #define gcvalue(o)        (val_(o).gc)

  340. #define nvalue(o)        (val_(o).n)
  341. #define boolvalue(o)        (KTAP_TFALSE - (o)->type)
  342. #define hvalue(o)        (&val_(o).gc->h)
  343. #define phvalue(o)        (&val_(o).gc->ph)
  344. #define clvalue(o)        (&val_(o).gc->fn)
  345. #define ptvalue(o)        (&val_(o).gc->pt)

  346. #define getstr(ts)        (const char *)((ts) + 1)
  347. #define rawtsvalue(o)        (&val_(o).gc->ts)
  348. #define svalue(o)        getstr(rawtsvalue(o))

  349. #define pvalue(o)        (&val_(o).p)
  350. #define fvalue(o)        (val_(o).f)
  351. #ifdef CONFIG_KTAP_FFI
  352. #define cdvalue(o)        (&val_(o).gc->cd)
  353. #endif

  354. #define is_nil(o)        (itype(o) == KTAP_TNIL)
  355. #define is_false(o)        (itype(o) == KTAP_TFALSE)
  356. #define is_true(o)        (itype(o) == KTAP_TTRUE)
  357. #define is_bool(o)        (is_false(o) || is_true(o))
  358. #define is_string(o)        (itype(o) == KTAP_TSTR)
  359. #define is_number(o)        (itype(o) == KTAP_TNUM)
  360. #define is_table(o)        (itype(o) == KTAP_TTAB)
  361. #define is_proto(o)        (itype(o) == KTAP_TPROTO)
  362. #define is_function(o)        (itype(o) == KTAP_TFUNC)
  363. #define is_cfunc(o)        (itype(o) == KTAP_TCFUNC)
  364. #define is_eventstr(o)        (itype(o) == KTAP_TEVENTSTR)
  365. #define is_kip(o)        (itype(o) == KTAP_TKIP)
  366. #define is_btrace(o)        (itype(o) == KTAP_TBTRACE)
  367. #ifdef CONFIG_KTAP_FFI
  368. #define is_cdata(o)        (itype(o) == KTAP_TCDATA)
  369. #endif


  370. #define set_nil(o)        ((o)->type = KTAP_TNIL)
  371. #define set_bool(o, x)        ((o)->type = KTAP_TFALSE-(uint32_t)(x))

  372. static inline void set_number(ktap_val_t *o, ktap_number n)
  373. {
  374.     setitype(o, KTAP_TNUM);
  375.     o->val.n = n;
  376. }

  377. static inline void set_string(ktap_val_t *o, const ktap_str_t *str)
  378. {
  379.     setitype(o, KTAP_TSTR);
  380.     o->val.gc = (ktap_obj_t *)str;
  381. }

  382. static inline void set_table(ktap_val_t *o, ktap_tab_t *tab)
  383. {
  384.     setitype(o, KTAP_TTAB);
  385.     o->val.gc = (ktap_obj_t *)tab;
  386. }

  387. static inline void set_proto(ktap_val_t *o, ktap_proto_t *pt)
  388. {
  389.     setitype(o, KTAP_TPROTO);
  390.     o->val.gc = (ktap_obj_t *)pt;
  391. }

  392. static inline void set_kstack(ktap_val_t *o, uint16_t depth, uint16_t skip)
  393. {
  394.     setitype(o, KTAP_TKSTACK);
  395.     o->val.stack.depth = depth;
  396.     o->val.stack.skip = skip;
  397. }

  398. static inline void set_func(ktap_val_t *o, ktap_func_t *fn)
  399. {
  400.     setitype(o, KTAP_TFUNC);
  401.     o->val.gc = (ktap_obj_t *)fn;
  402. }

  403. static inline void set_cfunc(ktap_val_t *o, ktap_cfunction fn)
  404. {
  405.     setitype(o, KTAP_TCFUNC);
  406.     o->val.f = fn;
  407. }

  408. static inline void set_eventstr(ktap_val_t *o)
  409. {
  410.     setitype(o, KTAP_TEVENTSTR);
  411. }

  412. static inline void set_ip(ktap_val_t *o, unsigned long addr)
  413. {
  414.     setitype(o, KTAP_TKIP);
  415.     o->val.n = addr;
  416. }


  417. #ifdef CONFIG_KTAP_FFI
  418. #define set_cdata(o, x)        { setitype(o, KTAP_TCDATA); (o)->val.gc = x; }
  419. #endif

  420. #define set_obj(o1, o2)        { *(o1) = *(o2); }

  421. #define incr_top(ks)        {ks->top++;}

  422. /*
  423. * KTAP_QL describes how error messages quote program elements.
  424. * CHANGE it if you want a different appearance.
  425. */
  426. #define KTAP_QL(x)      "'" x "'"
  427. #define KTAP_QS         KTAP_QL("%s")

  428. #endif /* __KTAP_TYPES_H__ */