A dynamic tracer for Linux

global.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. node_error(n, stderr, "map without key");
  53. return -EINVAL;
  54. }
  55. if (n->expr.args->ntype != N_IDENT) {
  56. node_error(n, stderr, "map must be identifier");
  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. if (lval->ntype == N_IDENT) {
  92. node_error(n, stderr, "conflicting types for '%s'",
  93. lval->ident.name);
  94. } else {
  95. node_error(n, stderr, "conflicting types for '%s'",
  96. lval->expr.args->ident.name);
  97. }
  98. return -EINVAL;
  99. }
  100. static int global_assign_static_verify(struct node *n)
  101. {
  102. struct node *lval;
  103. int nargs = node_nargs(n);
  104. if (nargs != 2) {
  105. node_error(n, stderr, "assignment operator expects 2 arguments,"
  106. " %d given", nargs);
  107. return -EINVAL;
  108. }
  109. lval = n->expr.args;
  110. if (node_is_map(lval) || (lval->ntype == N_IDENT))
  111. return 0;
  112. node_error(n, stderr, "assignment destination must be a variable or map");
  113. return -EINVAL;
  114. }
  115. /* pid */
  116. struct type t_pid = {
  117. .ttype = T_TYPEDEF,
  118. .tdef = { .name = ":pid", .type = &t_u32 },
  119. };
  120. /* time */
  121. struct type t_time = {
  122. .ttype = T_TYPEDEF, /* TODO: should be a T_FUNC with a static
  123. * signature */
  124. .tdef = { .name = ":time", .type = &t_s64 },
  125. };
  126. static const struct func global_funcs[] = {
  127. { .name = ":block", .type = &t_void, },
  128. { .name = "+", },
  129. { .name = "-", },
  130. {
  131. .name = ":assign",
  132. .type_infer = global_assign_type_infer,
  133. .static_verify = global_assign_static_verify,
  134. },
  135. {
  136. .name = ":map",
  137. .type_infer = global_map_type_infer,
  138. .static_verify = global_map_static_verify,
  139. },
  140. { .name = "pid", .type = &t_pid, },
  141. { .name = "time", .type = &t_time, },
  142. { .name = "quantize", },
  143. { .name = NULL }
  144. };
  145. static const struct func global_num_func = {
  146. .name = ":num",
  147. .type = &t_int,
  148. };
  149. struct type t_string_array = {
  150. .ttype = T_ARRAY,
  151. .array = { .type = &t_char, .len = 64 }, /* TODO: tunable */
  152. };
  153. struct type t_string = {
  154. .ttype = T_TYPEDEF,
  155. .tdef = { .name = ":string", .type = &t_string_array },
  156. };
  157. static const struct func global_string_func = {
  158. .name = ":string",
  159. .type = &t_string,
  160. };
  161. static const struct func global_ident_func = {
  162. .name = ":ident",
  163. };
  164. static const struct func *global_sym_alloc_expr(struct node *n)
  165. {
  166. const struct func *func;
  167. int err;
  168. for (func = global_funcs; func->name; func++) {
  169. if (strcmp(func->name, n->expr.func))
  170. continue;
  171. return func;
  172. }
  173. return NULL;
  174. }
  175. int global_sym_alloc(struct prog *prog, struct node *n)
  176. {
  177. const struct func *func;
  178. struct symtab *st = prog->locals;
  179. int err;
  180. switch (n->ntype) {
  181. case N_EXPR:
  182. func = global_sym_alloc_expr(n);
  183. break;
  184. case N_IDENT:
  185. st = prog->globals;
  186. func = &global_ident_func;
  187. break;
  188. case N_NUM:
  189. func = &global_num_func;
  190. break;
  191. case N_STRING:
  192. func = &global_string_func;
  193. break;
  194. }
  195. if (!func)
  196. return -ENOENT;
  197. if (func->static_verify) {
  198. err = func->static_verify(n);
  199. if (err)
  200. return err;
  201. }
  202. n->sym = sym_alloc(st, n, func);
  203. if (func->type)
  204. /* return type is static, fill it now */
  205. n->sym->type = func->type;
  206. return 0;
  207. }
  208. int global_probe(struct prog *prog)
  209. {
  210. return 0;
  211. }
  212. struct provider global = {
  213. .name = ":",
  214. .sym_alloc = global_sym_alloc,
  215. .probe = global_probe,
  216. };
  217. __attribute__((constructor))
  218. static void global_init(void)
  219. {
  220. provider_register(&global);
  221. }