A dynamic tracer for Linux

type.c 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. if (t->sou.packed)
  153. return 1;
  154. tfields_foreach(f, t->sou.fields) {
  155. falign = type_alignof(f->type);
  156. if (falign < 0)
  157. return falign;
  158. if (falign > align)
  159. align = falign;
  160. }
  161. return align;
  162. }
  163. ssize_t type_alignof(struct type *t)
  164. {
  165. switch (t->ttype){
  166. case T_VOID:
  167. case T_SCALAR:
  168. case T_POINTER:
  169. case T_FUNC:
  170. case T_MAP:
  171. return type_sizeof(t);
  172. case T_TYPEDEF:
  173. return type_alignof(t->tdef.type);
  174. case T_ARRAY:
  175. return type_alignof(t->array.type);
  176. case T_STRUCT:
  177. return type_alignof_struct(t);
  178. }
  179. return -EINVAL;
  180. }
  181. ssize_t type_offset_size_of(struct type *t, const char *field)
  182. {
  183. struct tfield *f;
  184. size_t offset = 0;
  185. ssize_t fsize, falign;
  186. assert(t->ttype == T_STRUCT);
  187. if (!t->sou.fields)
  188. return -ENOENT;
  189. tfields_foreach(f, t->sou.fields) {
  190. fsize = type_sizeof(f->type);
  191. if (fsize < 0)
  192. return fsize;
  193. falign = type_alignof(f->type);
  194. if (falign < 0)
  195. return falign;
  196. if (!t->sou.packed && (falign > 1))
  197. offset += falign - (offset % falign);
  198. if (field && !strcmp(f->name, field))
  199. return offset;
  200. offset += fsize;
  201. }
  202. if (field)
  203. return -ENOENT;
  204. if (!t->sou.packed && offset && (falign > 1))
  205. offset += falign - (offset % falign);
  206. return offset;
  207. }
  208. ssize_t type_offsetof(struct type *t, const char *field)
  209. {
  210. return type_offset_size_of(t, field);
  211. }
  212. ssize_t type_sizeof_struct(struct type *t)
  213. {
  214. return type_offset_size_of(t, NULL);
  215. }
  216. ssize_t type_sizeof(struct type *t)
  217. {
  218. switch (t->ttype){
  219. case T_VOID:
  220. return sizeof(void);
  221. case T_SCALAR:
  222. return t->scalar.size;
  223. case T_TYPEDEF:
  224. return type_sizeof(t->tdef.type);
  225. case T_POINTER:
  226. case T_FUNC:
  227. return sizeof(void *);
  228. case T_ARRAY:
  229. return t->array.len * type_sizeof(t->array.type);
  230. case T_STRUCT:
  231. return type_sizeof_struct(t);
  232. case T_MAP:
  233. return sizeof(int);
  234. }
  235. return -EINVAL;
  236. }
  237. int all_types_cmp(const void *_a, const void *_b)
  238. {
  239. const struct type *a = *((struct type **)_a);
  240. const struct type *b = *((struct type **)_b);
  241. return a - b;
  242. }
  243. struct type_list {
  244. struct type **types;
  245. size_t len;
  246. } all_types;
  247. #define types_foreach(_t) \
  248. for ((_t) = all_types.types[0]; (_t) <= all_types.types[all_types.len]; (_t)++)
  249. int type_add(struct type *t)
  250. {
  251. if (bsearch(t, all_types.types, all_types.len,
  252. sizeof(*all_types.types), all_types_cmp))
  253. return 0;
  254. /* type_size_set(t); */
  255. all_types.types = realloc(all_types.types,
  256. ++all_types.len * sizeof(*all_types.types));
  257. all_types.types[all_types.len - 1] = t;
  258. qsort(all_types.types, all_types.len, sizeof(*all_types.types), all_types_cmp);
  259. return 0;
  260. }
  261. int type_add_list(struct type **ts)
  262. {
  263. int err;
  264. for (; *ts; ts++) {
  265. err = type_add(*ts);
  266. if (err)
  267. return err;
  268. }
  269. return 0;
  270. }
  271. struct type *type_array_of(struct type *type, size_t len)
  272. {
  273. struct type *t;
  274. types_foreach(t) {
  275. if ((t->ttype == T_ARRAY)
  276. && (t->array.type == type)
  277. && (t->array.len == len))
  278. return t;
  279. }
  280. t = calloc(1, sizeof(*t));
  281. t->ttype = T_ARRAY;
  282. t->array.type = type;
  283. t->array.len = len;
  284. type_add(t);
  285. return t;
  286. }
  287. struct type *type_map_of(struct type *ktype, struct type *vtype)
  288. {
  289. struct type *t;
  290. types_foreach(t) {
  291. if ((t->ttype == T_MAP)
  292. && (t->map.ktype == ktype)
  293. && (t->map.vtype == vtype))
  294. return t;
  295. }
  296. t = calloc(1, sizeof(*t));
  297. t->ttype = T_MAP;
  298. t->map.vtype = vtype;
  299. t->map.ktype = ktype;
  300. type_add(t);
  301. return t;
  302. }
  303. struct type *type_ptr_of(struct type *type)
  304. {
  305. struct type *t;
  306. types_foreach(t) {
  307. if ((t->ttype == T_POINTER)
  308. && (t->ptr.type == type))
  309. return t;
  310. }
  311. t = calloc(1, sizeof(*t));
  312. t->ttype = T_POINTER;
  313. t->ptr.type = type;
  314. type_add(t);
  315. return t;
  316. }
  317. #define is_signed(_t) (((_t)(-1)) < 0)
  318. #define builtin_scalar(_t) { \
  319. .ttype = T_SCALAR, \
  320. .scalar = { \
  321. .name = #_t, \
  322. .size = sizeof(_t), \
  323. .is_signed = is_signed(_t), \
  324. }, \
  325. }
  326. struct type t_void = { .ttype = T_VOID };
  327. #pragma GCC diagnostic ignored "-Wtype-limits"
  328. /* is_signed will generate a warning for unsigned types since the
  329. * expression can never be true. this is exactly what we're interested
  330. * in here though. it gets us out of having to specify scalar
  331. * signedness per architecture. */
  332. struct type t_char = builtin_scalar(char);
  333. struct type t_schar = builtin_scalar(signed char);
  334. struct type t_uchar = builtin_scalar(unsigned char);
  335. struct type t_short = builtin_scalar(short);
  336. struct type t_sshort = builtin_scalar(signed short);
  337. struct type t_ushort = builtin_scalar(unsigned short);
  338. struct type t_int = builtin_scalar(int);
  339. struct type t_sint = builtin_scalar(signed int);
  340. struct type t_uint = builtin_scalar(unsigned int);
  341. struct type t_long = builtin_scalar(long);
  342. struct type t_slong = builtin_scalar(signed long);
  343. struct type t_ulong = builtin_scalar(unsigned long);
  344. struct type t_llong = builtin_scalar(long long);
  345. struct type t_sllong = builtin_scalar(signed long long);
  346. struct type t_ullong = builtin_scalar(unsigned long long);
  347. #pragma GCC diagnostic pop
  348. struct type *builtin_types[] = {
  349. &t_void,
  350. &t_char, &t_schar, &t_uchar,
  351. &t_short, &t_sshort, &t_ushort,
  352. &t_int, &t_sint, &t_uint,
  353. &t_long, &t_slong, &t_ulong,
  354. &t_llong, &t_sllong, &t_ullong,
  355. NULL
  356. };
  357. __attribute__((constructor))
  358. static void type_init(void)
  359. {
  360. type_add_list(builtin_types);
  361. }