A dynamic tracer for Linux

global.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #define _GNU_SOURCE /* asprintf */
  2. #include <assert.h>
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "arch.h"
  7. #include "func.h"
  8. #include "node.h"
  9. #include "ply.h"
  10. #include "sym.h"
  11. #include "type.h"
  12. /* :map */
  13. static struct type *global_map_ktype(struct node *n)
  14. {
  15. struct node *map, *key;
  16. struct type *ktype;
  17. struct tfield *kfields, *f;
  18. int i, nargs = node_nargs(n);
  19. char *kname;
  20. map = n->expr.args;
  21. if (nargs == 2)
  22. return map->next->sym->type;
  23. ktype = calloc(1, sizeof(*ktype));
  24. assert(ktype);
  25. kfields = calloc(nargs, sizeof(*kfields));
  26. assert(kfields);
  27. for (key = map->next, f = kfields, i = 0; key; key = key->next, f++, i++) {
  28. asprintf(&f->name, "k%d", i);
  29. f->type = key->sym->type;
  30. }
  31. asprintf(&ktype->sou.name, ":%s_key", map->ident.name);
  32. ktype->ttype = T_STRUCT;
  33. ktype->sou.fields = kfields;
  34. type_add(ktype);
  35. return ktype;
  36. }
  37. static int global_map_type_infer(struct prog *prog, struct node *n)
  38. {
  39. struct node *map = n->expr.args;
  40. if (n->sym->type || !map->sym->type)
  41. return 0;
  42. assert(map->sym->type->ttype == T_MAP);
  43. /* given `m[key]` where m's type is known, infer that the
  44. * expression's type is equal to m's value type. */
  45. n->sym->type = map->sym->type->map.vtype;
  46. return 0;
  47. }
  48. static int global_map_static_verify(struct node *n)
  49. {
  50. int nargs = node_nargs(n);
  51. if (nargs < 2) {
  52. _e("%#N: map without key.\n", n);
  53. return -EINVAL;
  54. }
  55. if (n->expr.args->ntype != N_IDENT) {
  56. _e("%#N: %N is not subscriptable.\n", n, n);
  57. return -EINVAL;
  58. }
  59. return 0;
  60. }
  61. /* :assign */
  62. static int global_assign_type_infer_map(struct node *n)
  63. {
  64. struct node *map, *key;
  65. struct type *ktype;
  66. map = n->expr.args;
  67. for (key = map->next; key; key = key->next) {
  68. if (type_sizeof(key->sym->type) < 0)
  69. return 0;
  70. }
  71. map->sym->type = type_map_of(global_map_ktype(n), n->sym->type);
  72. return 0;
  73. }
  74. static int global_assign_type_infer(struct prog *prog, struct node *n)
  75. {
  76. struct node *lval, *rval;
  77. lval = n->expr.args;
  78. rval = lval->next;
  79. if (!rval->sym->type)
  80. return 0;
  81. if (!lval->sym->type) {
  82. /* given `a = b` where b's type is known but not a's,
  83. * infer that a's type must be equal to b's */
  84. lval->sym->type = rval->sym->type;
  85. if (node_is_map(lval))
  86. return global_assign_type_infer_map(lval);
  87. return 0;
  88. }
  89. if (type_compatible(lval->sym->type, rval->sym->type))
  90. return 0;
  91. _e("%#N: can't assign %N (type '%T'), to %N (type '%T').\n",
  92. n, rval, rval->sym->type, lval, lval->sym->type);
  93. return -EINVAL;
  94. }
  95. static int global_assign_static_verify(struct node *n)
  96. {
  97. struct node *lval;
  98. int nargs = node_nargs(n);
  99. if (nargs != 2) {
  100. _e("%#N: expected 2 arguments, %d given.\n", n, nargs);
  101. return -EINVAL;
  102. }
  103. lval = n->expr.args;
  104. if (node_is_map(lval) || (lval->ntype == N_IDENT))
  105. return 0;
  106. _e("%#N: %N is not assignable.\n", n, lval);
  107. return -EINVAL;
  108. }
  109. /* pid */
  110. struct type t_pid = {
  111. .ttype = T_TYPEDEF,
  112. .tdef = { .name = ":pid", .type = &t_u32 },
  113. };
  114. /* time */
  115. struct type t_time = {
  116. .ttype = T_TYPEDEF, /* TODO: should be a T_FUNC with a static
  117. * signature */
  118. .tdef = { .name = ":time", .type = &t_s64 },
  119. };
  120. static const struct func global_funcs[] = {
  121. { .name = ":block", .type = &t_void, },
  122. { .name = "+", },
  123. { .name = "-", },
  124. {
  125. .name = ":assign",
  126. .type_infer = global_assign_type_infer,
  127. .static_verify = global_assign_static_verify,
  128. },
  129. {
  130. .name = ":map",
  131. .type_infer = global_map_type_infer,
  132. .static_verify = global_map_static_verify,
  133. },
  134. { .name = "pid", .type = &t_pid, },
  135. { .name = "time", .type = &t_time, },
  136. { .name = "quantize", },
  137. { .name = NULL }
  138. };
  139. static const struct func global_num_func = {
  140. .name = ":num",
  141. .type = &t_int,
  142. };
  143. struct type t_string_array = {
  144. .ttype = T_ARRAY,
  145. .array = { .type = &t_char, .len = 64 }, /* TODO: tunable */
  146. };
  147. struct type t_string = {
  148. .ttype = T_TYPEDEF,
  149. .tdef = { .name = ":string", .type = &t_string_array },
  150. };
  151. static const struct func global_string_func = {
  152. .name = ":string",
  153. .type = &t_string,
  154. };
  155. static const struct func global_ident_func = {
  156. .name = ":ident",
  157. };
  158. static const struct func *global_sym_alloc_expr(struct node *n)
  159. {
  160. const struct func *func;
  161. int err;
  162. for (func = global_funcs; func->name; func++) {
  163. if (strcmp(func->name, n->expr.func))
  164. continue;
  165. return func;
  166. }
  167. return NULL;
  168. }
  169. int global_sym_alloc(struct prog *prog, struct node *n)
  170. {
  171. const struct func *func;
  172. struct symtab *st = prog->locals;
  173. int err;
  174. switch (n->ntype) {
  175. case N_EXPR:
  176. func = global_sym_alloc_expr(n);
  177. break;
  178. case N_IDENT:
  179. st = prog->globals;
  180. func = &global_ident_func;
  181. break;
  182. case N_NUM:
  183. func = &global_num_func;
  184. break;
  185. case N_STRING:
  186. func = &global_string_func;
  187. break;
  188. }
  189. if (!func)
  190. return -ENOENT;
  191. if (func->static_verify) {
  192. err = func->static_verify(n);
  193. if (err)
  194. return err;
  195. }
  196. n->sym = sym_alloc(st, n, func);
  197. if (func->type)
  198. /* return type is static, fill it now */
  199. n->sym->type = func->type;
  200. return 0;
  201. }
  202. int global_probe(struct prog *prog)
  203. {
  204. return 0;
  205. }
  206. struct provider global = {
  207. .name = ":",
  208. .sym_alloc = global_sym_alloc,
  209. .probe = global_probe,
  210. };
  211. __attribute__((constructor))
  212. static void global_init(void)
  213. {
  214. provider_register(&global);
  215. }