A dynamic tracer for Linux

type.c 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. #include <assert.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "type.h"
  7. static void __sgr(FILE *fp, int sgr, const char *s)
  8. {
  9. if (!s)
  10. return;
  11. fprintf(fp, "\e[%dm%s\e[0m", sgr, s);
  12. }
  13. static void __bold(FILE *fp, const char *s)
  14. {
  15. __sgr(fp, 1, s);
  16. }
  17. static void __faint(FILE *fp, const char *s)
  18. {
  19. __sgr(fp, 2, s);
  20. }
  21. static void type_dump_func(struct type *t, const char *name, FILE *fp)
  22. {
  23. struct tfield *arg;
  24. type_dump(t->func.type, NULL, fp);
  25. fprintf(fp, " (*\e[1m%s\e[0m)(", name ? : "");
  26. if (!t->func.args) {
  27. __faint(fp, "void");
  28. fputc(')', fp);
  29. return;
  30. }
  31. for (arg = t->func.args; arg->type != T_VOID; arg++) {
  32. if (arg != t->func.args)
  33. fputs(", ", fp);
  34. type_dump(arg->type, NULL, fp);
  35. }
  36. fputc(')', fp);
  37. }
  38. void type_dump(struct type *t, const char *name, FILE *fp)
  39. {
  40. if (!t)
  41. goto print_void;
  42. switch (t->ttype){
  43. case T_VOID:
  44. print_void:
  45. __faint(fp, "void");
  46. fputs(name ? " " : "", fp);
  47. __bold(fp, name);
  48. break;
  49. case T_TYPEDEF:
  50. __faint(fp, t->tdef.name);
  51. fputs(name ? " " : "", fp);
  52. __bold(fp, name);
  53. break;
  54. case T_SCALAR:
  55. __faint(fp, t->scalar.name);
  56. fputs(name ? " " : "", fp);
  57. __bold(fp, name);
  58. break;
  59. case T_POINTER:
  60. type_dump(t->ptr.type, NULL, fp);
  61. fputs(" *", fp);
  62. __bold(fp, name);
  63. break;
  64. case T_ARRAY:
  65. type_dump(t->array.type, NULL, fp);
  66. fputs(name ? " " : "", fp);
  67. __bold(fp, name);
  68. fprintf(fp, "[%zu]", t->array.len);
  69. break;
  70. case T_STRUCT:
  71. fputs("struct ", fp);
  72. __faint(fp, t->sou.name);
  73. __bold(fp, name);
  74. break;
  75. case T_FUNC:
  76. type_dump_func(t, name, fp);
  77. break;
  78. case T_MAP:
  79. type_dump(t->map.vtype, NULL, fp);
  80. fputs(name ? " " : "", fp);
  81. __bold(fp, name);
  82. fputc('{', fp);
  83. type_dump(t->map.ktype, NULL, fp);
  84. fputc('}', fp);
  85. break;
  86. }
  87. }
  88. static void type_dump_decl_sou(struct type *t, FILE *fp)
  89. {
  90. struct tfield *f;
  91. type_dump(t, NULL, fp);
  92. fputs(" {\n", fp);
  93. for (f = t->sou.fields; f->type->ttype != T_VOID; f++) {
  94. fputc('\t', fp);
  95. type_dump(f->type, NULL, fp);
  96. fprintf(fp, " %s;\n", f->name);
  97. }
  98. fputs("}", fp);
  99. }
  100. void type_dump_decl(struct type *t, FILE *fp)
  101. {
  102. switch (t->ttype) {
  103. case T_TYPEDEF:
  104. fputs("typedef ", fp);
  105. type_dump(t->tdef.type, NULL, fp);
  106. fprintf(fp, " %s", t->tdef.name);
  107. break;
  108. case T_STRUCT:
  109. type_dump_decl_sou(t, fp);
  110. break;
  111. case T_VOID:
  112. case T_SCALAR:
  113. case T_POINTER:
  114. case T_ARRAY:
  115. case T_MAP:
  116. case T_FUNC:
  117. type_dump(t, NULL, fp);
  118. break;
  119. }
  120. }
  121. struct type *type_normalize(struct type *t)
  122. {
  123. while (t->ttype == T_TYPEDEF)
  124. t = t->tdef.type;
  125. return t;
  126. }
  127. int type_equal(struct type *a, struct type *b)
  128. {
  129. /* TODO */
  130. return a == b;
  131. }
  132. int type_compatible(struct type *a, struct type *b)
  133. {
  134. a = type_normalize(a);
  135. b = type_normalize(b);
  136. if (a->ttype != b->ttype)
  137. return 0;
  138. switch (a->ttype){
  139. case T_VOID:
  140. case T_SCALAR:
  141. case T_POINTER:
  142. return 1;
  143. case T_ARRAY:
  144. if (a->array.len != b->array.len)
  145. return 0;
  146. return type_compatible(a->array.type, b->array.type);
  147. case T_STRUCT:
  148. /* case T_UNION: */
  149. return !strcmp(a->sou.name, b->sou.name);
  150. case T_FUNC:
  151. return type_compatible(a->func.type, b->func.type);
  152. case T_MAP:
  153. return type_compatible(a->map.vtype, b->map.vtype);
  154. case T_TYPEDEF:
  155. assert(0);
  156. }
  157. assert(0);
  158. return 0;
  159. }
  160. static ssize_t type_alignof_struct(struct type *t)
  161. {
  162. struct tfield *f;
  163. ssize_t falign, align = -EINVAL;
  164. if (t->sou.packed)
  165. return 1;
  166. tfields_foreach(f, t->sou.fields) {
  167. falign = type_alignof(f->type);
  168. if (falign < 0)
  169. return falign;
  170. if (falign > align)
  171. align = falign;
  172. }
  173. return align;
  174. }
  175. ssize_t type_alignof(struct type *t)
  176. {
  177. if (!t)
  178. return -EINVAL;
  179. switch (t->ttype){
  180. case T_VOID:
  181. case T_SCALAR:
  182. case T_POINTER:
  183. case T_FUNC:
  184. case T_MAP:
  185. return type_sizeof(t);
  186. case T_TYPEDEF:
  187. return type_alignof(t->tdef.type);
  188. case T_ARRAY:
  189. return type_alignof(t->array.type);
  190. case T_STRUCT:
  191. return type_alignof_struct(t);
  192. }
  193. return -EINVAL;
  194. }
  195. static size_t __padding(size_t offset, size_t align)
  196. {
  197. size_t pad = align - (offset & (align - 1));
  198. return (pad == align) ? 0 : pad;
  199. }
  200. ssize_t type_offset_size_of(struct type *t, const char *field)
  201. {
  202. struct tfield *f;
  203. size_t offset = 0;
  204. ssize_t fsize, falign;
  205. assert(t->ttype == T_STRUCT);
  206. if (!t->sou.fields)
  207. return -ENOENT;
  208. tfields_foreach(f, t->sou.fields) {
  209. fsize = type_sizeof(f->type);
  210. if (fsize < 0)
  211. return fsize;
  212. falign = type_alignof(f->type);
  213. if (falign < 0)
  214. return falign;
  215. if (!t->sou.packed)
  216. offset += __padding(offset, falign);
  217. if (field && !strcmp(f->name, field))
  218. return offset;
  219. offset += fsize;
  220. }
  221. if (field)
  222. return -ENOENT;
  223. if (!t->sou.packed)
  224. offset += __padding(offset, type_alignof(t));
  225. return offset;
  226. }
  227. ssize_t type_offsetof(struct type *t, const char *field)
  228. {
  229. if (!t)
  230. return -EINVAL;
  231. return type_offset_size_of(t, field);
  232. }
  233. ssize_t type_sizeof_struct(struct type *t)
  234. {
  235. return type_offset_size_of(t, NULL);
  236. }
  237. ssize_t type_sizeof(struct type *t)
  238. {
  239. if (!t)
  240. return -EINVAL;
  241. switch (t->ttype){
  242. case T_VOID:
  243. return sizeof(void);
  244. case T_SCALAR:
  245. return t->scalar.size;
  246. case T_TYPEDEF:
  247. return type_sizeof(t->tdef.type);
  248. case T_POINTER:
  249. case T_FUNC:
  250. return sizeof(void *);
  251. case T_ARRAY:
  252. return t->array.len * type_sizeof(t->array.type);
  253. case T_STRUCT:
  254. return type_sizeof_struct(t);
  255. case T_MAP:
  256. return sizeof(int);
  257. }
  258. return -EINVAL;
  259. }
  260. int all_types_cmp(const void *_a, const void *_b)
  261. {
  262. const struct type *a = *((struct type **)_a);
  263. const struct type *b = *((struct type **)_b);
  264. return a - b;
  265. }
  266. struct type_list {
  267. struct type **types;
  268. size_t len;
  269. } all_types;
  270. #define types_foreach(_t) \
  271. for ((_t) = all_types.types; (_t) < &all_types.types[all_types.len]; (_t)++)
  272. void type_dump_decls(FILE *fp)
  273. {
  274. struct type **ti, *t;
  275. types_foreach(ti) {
  276. t = *ti;
  277. if (t->ttype == T_SCALAR)
  278. continue;
  279. type_dump_decl(t, fp);
  280. fputc('\n', fp);
  281. }
  282. }
  283. int type_add(struct type *t)
  284. {
  285. if (bsearch(t, all_types.types, all_types.len,
  286. sizeof(*all_types.types), all_types_cmp))
  287. return 0;
  288. /* type_size_set(t); */
  289. all_types.types = realloc(all_types.types,
  290. ++all_types.len * sizeof(*all_types.types));
  291. all_types.types[all_types.len - 1] = t;
  292. qsort(all_types.types, all_types.len, sizeof(*all_types.types), all_types_cmp);
  293. return 0;
  294. }
  295. int type_add_list(struct type **ts)
  296. {
  297. int err;
  298. for (; *ts; ts++) {
  299. err = type_add(*ts);
  300. if (err)
  301. return err;
  302. }
  303. return 0;
  304. }
  305. struct type *type_array_of(struct type *type, size_t len)
  306. {
  307. struct type **ti, *t;
  308. types_foreach(ti) {
  309. t = *ti;
  310. if ((t->ttype == T_ARRAY)
  311. && (t->array.type == type)
  312. && (t->array.len == len))
  313. return t;
  314. }
  315. t = calloc(1, sizeof(*t));
  316. t->ttype = T_ARRAY;
  317. t->array.type = type;
  318. t->array.len = len;
  319. type_add(t);
  320. return t;
  321. }
  322. struct type *type_map_of(struct type *ktype, struct type *vtype)
  323. {
  324. struct type **ti, *t;
  325. types_foreach(ti) {
  326. t = *ti;
  327. if ((t->ttype == T_MAP)
  328. && (t->map.ktype == ktype)
  329. && (t->map.vtype == vtype))
  330. return t;
  331. }
  332. t = calloc(1, sizeof(*t));
  333. t->ttype = T_MAP;
  334. t->map.vtype = vtype;
  335. t->map.ktype = ktype;
  336. type_add(t);
  337. return t;
  338. }
  339. struct type *type_ptr_of(struct type *type)
  340. {
  341. struct type **ti, *t;
  342. types_foreach(ti) {
  343. t = *ti;
  344. if ((t->ttype == T_POINTER)
  345. && (t->ptr.type == type))
  346. return t;
  347. }
  348. t = calloc(1, sizeof(*t));
  349. t->ttype = T_POINTER;
  350. t->ptr.type = type;
  351. type_add(t);
  352. return t;
  353. }
  354. #define is_signed(_t) (((_t)(-1)) < 0)
  355. #define builtin_scalar(_t) { \
  356. .ttype = T_SCALAR, \
  357. .scalar = { \
  358. .name = #_t, \
  359. .size = sizeof(_t), \
  360. .is_signed = is_signed(_t), \
  361. }, \
  362. }
  363. struct type t_void = { .ttype = T_VOID };
  364. #pragma GCC diagnostic ignored "-Wtype-limits"
  365. /* is_signed will generate a warning for unsigned types since the
  366. * expression can never be true. this is exactly what we're interested
  367. * in here though. it gets us out of having to specify scalar
  368. * signedness per architecture. */
  369. struct type t_char = builtin_scalar(char);
  370. struct type t_schar = builtin_scalar(signed char);
  371. struct type t_uchar = builtin_scalar(unsigned char);
  372. struct type t_short = builtin_scalar(short);
  373. struct type t_sshort = builtin_scalar(signed short);
  374. struct type t_ushort = builtin_scalar(unsigned short);
  375. struct type t_int = builtin_scalar(int);
  376. struct type t_sint = builtin_scalar(signed int);
  377. struct type t_uint = builtin_scalar(unsigned int);
  378. struct type t_long = builtin_scalar(long);
  379. struct type t_slong = builtin_scalar(signed long);
  380. struct type t_ulong = builtin_scalar(unsigned long);
  381. struct type t_llong = builtin_scalar(long long);
  382. struct type t_sllong = builtin_scalar(signed long long);
  383. struct type t_ullong = builtin_scalar(unsigned long long);
  384. #pragma GCC diagnostic pop
  385. struct type *builtin_types[] = {
  386. &t_void,
  387. &t_char, &t_schar, &t_uchar,
  388. &t_short, &t_sshort, &t_ushort,
  389. &t_int, &t_sint, &t_uint,
  390. &t_long, &t_slong, &t_ulong,
  391. &t_llong, &t_sllong, &t_ullong,
  392. NULL
  393. };
  394. __attribute__((constructor))
  395. static void type_init(void)
  396. {
  397. type_add_list(builtin_types);
  398. }