A dynamic tracer for Linux

type.c 8.2KB

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