A dynamic tracer for Linux

type.c 9.0KB

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