A dynamic tracer for Linux

type.c 9.3KB

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