A dynamic tracer for Linux

global.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. #define _GNU_SOURCE /* asprintf */
  2. #include <assert.h>
  3. #include <errno.h>
  4. #include <limits.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "arch.h"
  8. #include "func.h"
  9. #include "node.h"
  10. #include "ply.h"
  11. #include "sym.h"
  12. #include "type.h"
  13. /* :map */
  14. static struct type *global_map_ktype(struct node *n)
  15. {
  16. struct node *map, *key;
  17. struct type *ktype;
  18. struct tfield *kfields, *f;
  19. int i, nargs = node_nargs(n);
  20. char *kname;
  21. map = n->expr.args;
  22. if (nargs == 2)
  23. return map->next->sym->type;
  24. ktype = calloc(1, sizeof(*ktype));
  25. assert(ktype);
  26. kfields = calloc(nargs, sizeof(*kfields));
  27. assert(kfields);
  28. for (key = map->next, f = kfields, i = 0; key; key = key->next, f++, i++) {
  29. asprintf(&f->name, "k%d", i);
  30. f->type = key->sym->type;
  31. }
  32. asprintf(&ktype->sou.name, ":%s_key", map->ident.name);
  33. ktype->ttype = T_STRUCT;
  34. ktype->sou.fields = kfields;
  35. type_add(ktype);
  36. return ktype;
  37. }
  38. static int global_map_type_infer(const struct func *func, struct node *n)
  39. {
  40. struct node *map = n->expr.args;
  41. if (!map->sym->type)
  42. return 0;
  43. /* TODO validate key against known type */
  44. /* given `m[key]` where m's type is known, infer that the
  45. * expression's type is equal to m's value type. */
  46. n->sym->type = map->sym->type->map.vtype;
  47. return 0;
  48. }
  49. static int global_map_static_validate(const struct func *func, struct node *n)
  50. {
  51. if (n->expr.args->ntype != N_IDENT) {
  52. _e("%#N: trying to lookup a key in %N, which is not a map.\n",
  53. n, n);
  54. return -EINVAL;
  55. }
  56. return 0;
  57. }
  58. /* :assign */
  59. static int global_assign_type_infer_map(struct node *n)
  60. {
  61. struct node *map, *key;
  62. struct type *ktype;
  63. map = n->expr.args;
  64. for (key = map->next; key; key = key->next) {
  65. if (type_sizeof(key->sym->type) < 0)
  66. return 0;
  67. }
  68. map->sym->type = type_map_of(global_map_ktype(n), n->sym->type);
  69. return 0;
  70. }
  71. static int global_assign_type_infer(const struct func *func, struct node *n)
  72. {
  73. struct node *lval, *rval;
  74. if (n->sym->type)
  75. return 0;
  76. lval = n->expr.args;
  77. rval = lval->next;
  78. if (!rval->sym->type)
  79. return 0;
  80. if (!lval->sym->type) {
  81. /* given `a = b` where b's type is known but not a's,
  82. * infer that a's type must be equal to b's */
  83. lval->sym->type = rval->sym->type;
  84. /* TODO do we need assignment expressions? */
  85. n->sym->type = &t_void;
  86. if (node_is_map(lval))
  87. return global_assign_type_infer_map(lval);
  88. return 0;
  89. }
  90. if (type_compatible(lval->sym->type, rval->sym->type))
  91. return 0;
  92. _e("%#N: can't assign %N (type '%T'), to %N (type '%T').\n",
  93. n, rval, rval->sym->type, lval, lval->sym->type);
  94. return -EINVAL;
  95. }
  96. static int global_assign_static_validate(const struct func *func, struct node *n)
  97. {
  98. struct node *lval;
  99. lval = n->expr.args;
  100. if (node_is_map(lval) || (lval->ntype == N_IDENT))
  101. return 0;
  102. _e("%#N: can't assign a value to %N.\n", n, lval);
  103. return -EINVAL;
  104. }
  105. /* :binop */
  106. static int global_binop_type_infer(const struct func *func, struct node *n)
  107. {
  108. struct node *lval, *rval;
  109. if (n->sym->type)
  110. return 0;
  111. lval = n->expr.args;
  112. rval = lval->next;
  113. if (!lval->sym->type || !rval->sym->type)
  114. return 0;
  115. if (type_equal(lval->sym->type, rval->sym->type)) {
  116. n->sym->type = lval->sym->type;
  117. return 0;
  118. }
  119. /* TODO handle integer promotion */
  120. return 0;
  121. }
  122. /* :binop */
  123. static int global_quantize_type_infer(const struct func *func, struct node *n)
  124. {
  125. struct node *arg;
  126. struct type *t;
  127. arg = n->expr.args;
  128. if (n->sym->type || !arg->sym->type)
  129. return 0;
  130. t = type_base(arg->sym->type);
  131. if (t->ttype != T_SCALAR) {
  132. _e("%#N: can't quantize non-scalar value %N (type '%T').\n",
  133. n, arg, arg->sym->type);
  134. return -EINVAL;
  135. }
  136. n->sym->type = type_array_of(arg->sym->type, type_sizeof(t) * 8);
  137. return 0;
  138. }
  139. /* pid */
  140. struct type t_pid = {
  141. .ttype = T_TYPEDEF,
  142. .tdef = { .name = ":pid", .type = &t_u32 },
  143. };
  144. struct type t_pid_func = {
  145. .ttype = T_FUNC,
  146. .func = { .type = &t_pid },
  147. };
  148. /* time */
  149. struct type t_time = {
  150. .ttype = T_TYPEDEF, /* TODO: should be a T_FUNC with a static
  151. * signature */
  152. .tdef = { .name = ":time", .type = &t_s64 },
  153. };
  154. struct type t_time_func = {
  155. .ttype = T_FUNC,
  156. .func = { .type = &t_time },
  157. };
  158. struct type t_block_func = {
  159. .ttype = T_FUNC,
  160. .func = { .type = &t_void, .vargs = 1 },
  161. };
  162. struct tfield f_2args[] = {
  163. { .type = &t_void },
  164. { .type = &t_void },
  165. { .type = NULL }
  166. };
  167. struct type t_2args_func = {
  168. .ttype = T_FUNC,
  169. .func = { .type = &t_void, .args = f_2args },
  170. };
  171. struct tfield f_1arg[] = {
  172. { .type = &t_void },
  173. { .type = NULL }
  174. };
  175. struct type t_1arg_func = {
  176. .ttype = T_FUNC,
  177. .func = { .type = &t_void, .args = f_1arg },
  178. };
  179. static const struct func global_funcs[] = {
  180. {
  181. .name = ":block",
  182. .type = &t_block_func,
  183. .static_ret = 1,
  184. },
  185. {
  186. .name = "+",
  187. .type = &t_2args_func,
  188. .type_infer = global_binop_type_infer,
  189. },
  190. {
  191. .name = "-",
  192. .type = &t_2args_func,
  193. .type_infer = global_binop_type_infer,
  194. },
  195. {
  196. .name = "=",
  197. .type = &t_2args_func,
  198. .type_infer = global_assign_type_infer,
  199. .static_validate = global_assign_static_validate,
  200. },
  201. {
  202. .name = "{}",
  203. /* .type = t_map_func, */
  204. .type_infer = global_map_type_infer,
  205. .static_validate = global_map_static_validate,
  206. },
  207. {
  208. .name = "pid",
  209. .type = &t_pid_func,
  210. .static_ret = 1,
  211. },
  212. {
  213. .name = "time",
  214. .type = &t_time_func,
  215. .static_ret = 1,
  216. },
  217. {
  218. .name = "quantize",
  219. .type = &t_1arg_func,
  220. .type_infer = global_quantize_type_infer,
  221. },
  222. { .name = NULL }
  223. };
  224. static struct type *global_num_type(struct node *n)
  225. {
  226. if (n->num.unsignd) {
  227. if (n->num.u64 <= INT_MAX)
  228. return &t_int;
  229. else if (n->num.u64 <= UINT_MAX)
  230. return &t_uint;
  231. else if (n->num.u64 <= LONG_MAX)
  232. return &t_long;
  233. else if (n->num.u64 <= ULONG_MAX)
  234. return &t_ulong;
  235. else if (n->num.u64 <= LLONG_MAX)
  236. return &t_llong;
  237. else if (n->num.u64 <= ULLONG_MAX)
  238. return &t_ullong;
  239. } else {
  240. if (n->num.s64 >= INT_MIN && n->num.s64 <= INT_MAX)
  241. return &t_int;
  242. else if (n->num.s64 >= LONG_MIN && n->num.s64 <= LONG_MAX)
  243. return &t_long;
  244. else if (n->num.s64 >= LLONG_MIN && n->num.s64 <= LLONG_MAX)
  245. return &t_llong;
  246. }
  247. assert(0);
  248. return NULL;
  249. }
  250. static const struct func global_num_func = {
  251. .name = ":num",
  252. };
  253. struct type t_string_array = {
  254. .ttype = T_ARRAY,
  255. .array = { .type = &t_char, .len = 64 }, /* TODO: tunable */
  256. };
  257. struct type t_string = {
  258. .ttype = T_TYPEDEF,
  259. .tdef = { .name = ":string", .type = &t_string_array },
  260. };
  261. static const struct func global_string_func = {
  262. .name = ":string",
  263. .type = &t_string,
  264. .static_ret = 1,
  265. };
  266. static const struct func global_ident_func = {
  267. .name = ":ident",
  268. };
  269. static const struct func *global_sym_alloc_expr(struct node *n)
  270. {
  271. const struct func *func;
  272. int err;
  273. for (func = global_funcs; func->name; func++) {
  274. if (strcmp(func->name, n->expr.func))
  275. continue;
  276. return func;
  277. }
  278. return NULL;
  279. }
  280. int global_sym_alloc(struct prog *prog, struct node *n)
  281. {
  282. const struct func *func;
  283. struct symtab *st = prog->locals;
  284. int err;
  285. switch (n->ntype) {
  286. case N_EXPR:
  287. func = global_sym_alloc_expr(n);
  288. break;
  289. case N_IDENT:
  290. st = prog->globals;
  291. func = &global_ident_func;
  292. break;
  293. case N_NUM:
  294. func = &global_num_func;
  295. break;
  296. case N_STRING:
  297. func = &global_string_func;
  298. break;
  299. }
  300. if (!func)
  301. return -ENOENT;
  302. err = func_static_validate(func, n);
  303. if (err)
  304. return err;
  305. n->sym = sym_alloc(st, n, func);
  306. if (n->ntype == N_NUM)
  307. n->sym->type = global_num_type(n);
  308. else if (func->static_ret)
  309. n->sym->type = func_return_type(func);
  310. return 0;
  311. }
  312. int global_probe(struct prog *prog)
  313. {
  314. return 0;
  315. }
  316. struct provider global = {
  317. .name = ":",
  318. .sym_alloc = global_sym_alloc,
  319. .probe = global_probe,
  320. };
  321. __attribute__((constructor))
  322. static void global_init(void)
  323. {
  324. provider_register(&global);
  325. }