A dynamic tracer for Linux

type.c 8.1KB

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