A dynamic tracer for Linux

type.c 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "type.h"
  5. #define builtin_scalar(_t) { \
  6. .ttype = T_SCALAR, \
  7. .size = sizeof(_t), \
  8. .t = { .scalar = { .name = #_t } }, \
  9. }
  10. #define builtin_pointer(_t) { \
  11. .ttype = T_POINTER, \
  12. .t = { .pointer = { .type = _t } }, \
  13. }
  14. type_t t_v = builtin_scalar(void);
  15. type_t t_vp = builtin_pointer(&t_v);
  16. type_t t_c = builtin_scalar(char);
  17. type_t t_cp = builtin_pointer(&t_c);
  18. type_t t_uc = builtin_scalar(unsigned char);
  19. type_t t_ucp = builtin_pointer(&t_uc);
  20. type_t t_s = builtin_scalar(short);
  21. type_t t_sp = builtin_pointer(&t_s);
  22. type_t t_us = builtin_scalar(unsigned short);
  23. type_t t_usp = builtin_pointer(&t_us);
  24. type_t t_i = builtin_scalar(int);
  25. type_t t_ip = builtin_pointer(&t_i);
  26. type_t t_ui = builtin_scalar(unsigned int);
  27. type_t t_uip = builtin_pointer(&t_ui);
  28. type_t t_l = builtin_scalar(long);
  29. type_t t_lp = builtin_pointer(&t_l);
  30. type_t t_ul = builtin_scalar(unsigned long);
  31. type_t t_ulp = builtin_pointer(&t_ul);
  32. type_t t_ll = builtin_scalar(long long);
  33. type_t t_llp = builtin_pointer(&t_ll);
  34. type_t t_ull = builtin_scalar(unsigned long long);
  35. type_t t_ullp = builtin_pointer(&t_ull);
  36. type_t *builtin_types[] = {
  37. &t_v, &t_vp,
  38. &t_c, &t_cp, &t_uc, &t_ucp,
  39. &t_s, &t_sp, &t_us, &t_usp,
  40. &t_i, &t_ip, &t_ui, &t_uip,
  41. &t_l, &t_lp, &t_ul, &t_ulp,
  42. &t_ll, &t_llp, &t_ull, &t_ullp,
  43. NULL
  44. };
  45. struct type_list {
  46. type_t **type;
  47. size_t len;
  48. } types;
  49. void type_dump_func(type_t *t, FILE *fp)
  50. {
  51. field_t *arg;
  52. type_dump(t->t.func.type, fp);
  53. fprintf(fp, " (*%s)(", t->t.func.name);
  54. for (arg = t->t.func.args; arg->type; arg++) {
  55. if (arg != t->t.func.args)
  56. fputs(", ", fp);
  57. type_dump(arg->type, fp);
  58. }
  59. fputc(')', fp);
  60. }
  61. void type_dump(type_t *t, FILE *fp)
  62. {
  63. switch (t->ttype){
  64. case T_TYPEDEF:
  65. fputs(t->t.tdef.name, fp);
  66. break;
  67. case T_SCALAR:
  68. fputs(t->t.scalar.name, fp);
  69. break;
  70. case T_POINTER:
  71. type_dump(t->t.pointer.type, fp);
  72. fputs(" *", fp);
  73. break;
  74. case T_ARRAY:
  75. type_dump(t->t.array.type, fp);
  76. fprintf(fp, "[%zu]", t->t.array.len);
  77. break;
  78. case T_STRUCT:
  79. fputs("struct ", fp);
  80. fputs(t->t.sou.name, fp);
  81. break;
  82. case T_UNION:
  83. fputs("union ", fp);
  84. fputs(t->t.sou.name, fp);
  85. break;
  86. case T_FUNC:
  87. type_dump_func(t, fp);
  88. break;
  89. case T_MAP:
  90. /* TODO */
  91. assert(0);
  92. break;
  93. }
  94. }
  95. void type_dump_cdecl_sou(type_t *t, FILE *fp)
  96. {
  97. field_t *f;
  98. type_dump(t, fp);
  99. fputs(" {\n", fp);
  100. for (f = t->t.sou.fields; f->type; f++) {
  101. fputc('\t', fp);
  102. type_dump(f->type, fp);
  103. fprintf(fp, " %s;\n", f->name);
  104. }
  105. fputs("}", fp);
  106. }
  107. void type_dump_cdecl(type_t *t, FILE *fp)
  108. {
  109. switch (t->ttype) {
  110. case T_TYPEDEF:
  111. fputs("typedef ", fp);
  112. type_dump(t->t.tdef.type, fp);
  113. fprintf(fp, " %s", t->t.tdef.name);
  114. break;
  115. case T_STRUCT:
  116. case T_UNION:
  117. type_dump_cdecl_sou(t, fp);
  118. break;
  119. case T_SCALAR:
  120. case T_POINTER:
  121. case T_ARRAY:
  122. case T_MAP:
  123. case T_FUNC:
  124. type_dump(t, fp);
  125. break;
  126. }
  127. }
  128. void types_dump_cdecl(FILE *fp)
  129. {
  130. size_t i;
  131. for (i = 0; i < types.len; i++) {
  132. type_t *t = types.type[i];
  133. type_dump_cdecl(t, stdout);
  134. printf(" <sz:0x%zx>\n", t->size);
  135. }
  136. }
  137. int type_equal(type_t *a, type_t *b)
  138. {
  139. /* TODO */
  140. return a == b;
  141. }
  142. void type_size_set_sou(type_t *t)
  143. {
  144. field_t *f;
  145. size_t size = 0;
  146. if (!t->t.sou.fields)
  147. return;
  148. for (f = t->t.sou.fields; f->name; f++) {
  149. /* size of all members is now known yet, abort */
  150. if (!f->type->size)
  151. return;
  152. if (!t->t.sou.packed)
  153. size += size % f->type->size;
  154. size += f->type->size;
  155. f->offset = size;
  156. }
  157. if (!t->t.sou.packed && size) {
  158. /* align complete struct to requirements of first
  159. * member */
  160. f = t->t.sou.fields;
  161. size += size % f->type->size;
  162. }
  163. t->size = size;
  164. }
  165. void type_size_set(type_t *t)
  166. {
  167. switch (t->ttype) {
  168. case T_TYPEDEF:
  169. t->size = t->t.tdef.type->size;
  170. break;
  171. case T_STRUCT:
  172. case T_UNION:
  173. type_size_set_sou(t);
  174. break;
  175. case T_FUNC:
  176. t->size = t->t.func.type->size;
  177. break;
  178. case T_SCALAR:
  179. break;
  180. case T_POINTER:
  181. t->size = sizeof(void *);
  182. break;
  183. case T_ARRAY:
  184. t->size = t->t.array.len * t->t.array.type->size;
  185. break;
  186. case T_MAP:
  187. /* TODO */
  188. assert(0);
  189. break;
  190. }
  191. }
  192. int type_add(type_t *t)
  193. {
  194. types.type = realloc(types.type, ++types.len * sizeof(*types.type));
  195. types.type[types.len - 1] = t;
  196. type_size_set(t);
  197. return 0;
  198. }
  199. int type_add_list(type_t **ts)
  200. {
  201. int err;
  202. for (; *ts; ts++) {
  203. err = type_add(*ts);
  204. if (err)
  205. return err;
  206. }
  207. return 0;
  208. }
  209. __attribute__((constructor))
  210. static void type_init(void)
  211. {
  212. type_add_list(builtin_types);
  213. }