A dynamic tracer for Linux

type.c 8.3KB

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