A dynamic tracer for Linux

global.c 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 (n->sym->type || !map->sym->type)
  42. return 0;
  43. assert(map->sym->type->ttype == T_MAP);
  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. lval = n->expr.args;
  75. rval = lval->next;
  76. if (!rval->sym->type)
  77. return 0;
  78. if (!lval->sym->type) {
  79. /* given `a = b` where b's type is known but not a's,
  80. * infer that a's type must be equal to b's */
  81. lval->sym->type = rval->sym->type;
  82. if (node_is_map(lval))
  83. return global_assign_type_infer_map(lval);
  84. return 0;
  85. }
  86. if (type_compatible(lval->sym->type, rval->sym->type))
  87. return 0;
  88. _e("%#N: can't assign %N (type '%T'), to %N (type '%T').\n",
  89. n, rval, rval->sym->type, lval, lval->sym->type);
  90. return -EINVAL;
  91. }
  92. static int global_assign_static_validate(const struct func *func, struct node *n)
  93. {
  94. struct node *lval;
  95. lval = n->expr.args;
  96. if (node_is_map(lval) || (lval->ntype == N_IDENT))
  97. return 0;
  98. _e("%#N: can't assign a value to %N.\n", n, lval);
  99. return -EINVAL;
  100. }
  101. /* pid */
  102. struct type t_pid = {
  103. .ttype = T_TYPEDEF,
  104. .tdef = { .name = ":pid", .type = &t_u32 },
  105. };
  106. struct type t_pid_func = {
  107. .ttype = T_FUNC,
  108. .func = { .type = &t_pid },
  109. };
  110. /* time */
  111. struct type t_time = {
  112. .ttype = T_TYPEDEF, /* TODO: should be a T_FUNC with a static
  113. * signature */
  114. .tdef = { .name = ":time", .type = &t_s64 },
  115. };
  116. struct type t_time_func = {
  117. .ttype = T_FUNC,
  118. .func = { .type = &t_time },
  119. };
  120. struct type t_block_func = {
  121. .ttype = T_FUNC,
  122. .func = { .type = &t_void, .vargs = 1 },
  123. };
  124. struct tfield f_2args[] = {
  125. { .type = &t_void },
  126. { .type = &t_void },
  127. { .type = NULL }
  128. };
  129. struct type t_2args_func = {
  130. .ttype = T_FUNC,
  131. .func = { .type = &t_void, .args = f_2args },
  132. };
  133. struct tfield f_1arg[] = {
  134. { .type = &t_void },
  135. { .type = NULL }
  136. };
  137. struct type t_1arg_func = {
  138. .ttype = T_FUNC,
  139. .func = { .type = &t_void, .args = f_1arg },
  140. };
  141. static const struct func global_funcs[] = {
  142. {
  143. .name = ":block",
  144. .type = &t_block_func,
  145. .static_ret = 1,
  146. },
  147. {
  148. .name = "+",
  149. .type = &t_2args_func,
  150. },
  151. {
  152. .name = "-",
  153. .type = &t_2args_func,
  154. },
  155. {
  156. .name = "=",
  157. .type = &t_2args_func,
  158. .type_infer = global_assign_type_infer,
  159. .static_validate = global_assign_static_validate,
  160. },
  161. {
  162. .name = "{}",
  163. /* .type = t_map_func, */
  164. .type_infer = global_map_type_infer,
  165. .static_validate = global_map_static_validate,
  166. },
  167. {
  168. .name = "pid",
  169. .type = &t_pid_func,
  170. .static_ret = 1,
  171. },
  172. {
  173. .name = "time",
  174. .type = &t_time_func,
  175. .static_ret = 1,
  176. },
  177. {
  178. .name = "quantize",
  179. .type = &t_1arg_func,
  180. },
  181. { .name = NULL }
  182. };
  183. static struct type *global_num_type(struct node *n)
  184. {
  185. if (n->num.unsignd) {
  186. if (n->num.u64 <= INT_MAX)
  187. return &t_int;
  188. else if (n->num.u64 <= UINT_MAX)
  189. return &t_uint;
  190. else if (n->num.u64 <= LONG_MAX)
  191. return &t_long;
  192. else if (n->num.u64 <= ULONG_MAX)
  193. return &t_ulong;
  194. else if (n->num.u64 <= LLONG_MAX)
  195. return &t_llong;
  196. else if (n->num.u64 <= ULLONG_MAX)
  197. return &t_ullong;
  198. } else {
  199. if (n->num.s64 >= INT_MIN && n->num.s64 <= INT_MAX)
  200. return &t_int;
  201. else if (n->num.s64 >= LONG_MIN && n->num.s64 <= LONG_MAX)
  202. return &t_long;
  203. else if (n->num.s64 >= LLONG_MIN && n->num.s64 <= LLONG_MAX)
  204. return &t_llong;
  205. }
  206. assert(0);
  207. return NULL;
  208. }
  209. static const struct func global_num_func = {
  210. .name = ":num",
  211. };
  212. struct type t_string_array = {
  213. .ttype = T_ARRAY,
  214. .array = { .type = &t_char, .len = 64 }, /* TODO: tunable */
  215. };
  216. struct type t_string = {
  217. .ttype = T_TYPEDEF,
  218. .tdef = { .name = ":string", .type = &t_string_array },
  219. };
  220. static const struct func global_string_func = {
  221. .name = ":string",
  222. .type = &t_string,
  223. .static_ret = 1,
  224. };
  225. static const struct func global_ident_func = {
  226. .name = ":ident",
  227. };
  228. static const struct func *global_sym_alloc_expr(struct node *n)
  229. {
  230. const struct func *func;
  231. int err;
  232. for (func = global_funcs; func->name; func++) {
  233. if (strcmp(func->name, n->expr.func))
  234. continue;
  235. return func;
  236. }
  237. return NULL;
  238. }
  239. int global_sym_alloc(struct prog *prog, struct node *n)
  240. {
  241. const struct func *func;
  242. struct symtab *st = prog->locals;
  243. int err;
  244. switch (n->ntype) {
  245. case N_EXPR:
  246. func = global_sym_alloc_expr(n);
  247. break;
  248. case N_IDENT:
  249. st = prog->globals;
  250. func = &global_ident_func;
  251. break;
  252. case N_NUM:
  253. func = &global_num_func;
  254. break;
  255. case N_STRING:
  256. func = &global_string_func;
  257. break;
  258. }
  259. if (!func)
  260. return -ENOENT;
  261. err = func_static_validate(func, n);
  262. if (err)
  263. return err;
  264. n->sym = sym_alloc(st, n, func);
  265. if (n->ntype == N_NUM)
  266. n->sym->type = global_num_type(n);
  267. else if (func->static_ret)
  268. n->sym->type = func_return_type(func);
  269. return 0;
  270. }
  271. int global_probe(struct prog *prog)
  272. {
  273. return 0;
  274. }
  275. struct provider global = {
  276. .name = ":",
  277. .sym_alloc = global_sym_alloc,
  278. .probe = global_probe,
  279. };
  280. __attribute__((constructor))
  281. static void global_init(void)
  282. {
  283. provider_register(&global);
  284. }