A dynamic tracer for Linux

type.c 9.3KB

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