A dynamic tracer for Linux

type.h 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #ifndef _PLY_TYPE_H
  2. #define _PLY_TYPE_H
  3. #include <stddef.h>
  4. #include <stdio.h>
  5. struct ttdef {
  6. char *name;
  7. struct type *type;
  8. };
  9. struct tscalar {
  10. size_t size;
  11. int is_signed:1;
  12. char *name;
  13. };
  14. struct tptr {
  15. struct type *type;
  16. };
  17. struct tarray {
  18. struct type *type;
  19. size_t len;
  20. };
  21. struct tmap {
  22. struct type *vtype;
  23. struct type *ktype;
  24. size_t len;
  25. };
  26. struct tfield {
  27. char *name;
  28. struct type *type;
  29. };
  30. #define tfields_foreach(_f, _fields) \
  31. for ((_f) = (_fields); (_f)->type; (_f)++)
  32. struct tfield *tfields_get(struct tfield *fields, const char *name);
  33. struct tstruct {
  34. char *name;
  35. struct tfield *fields;
  36. int packed:1;
  37. };
  38. struct tfunc {
  39. struct type *type;
  40. struct tfield *args;
  41. int vargs:1;
  42. int aggregation:1;
  43. };
  44. enum ttype {
  45. T_VOID,
  46. T_TYPEDEF,
  47. T_SCALAR,
  48. T_POINTER,
  49. T_ARRAY,
  50. T_MAP,
  51. T_STRUCT,
  52. /* T_UNION, TODO */
  53. T_FUNC,
  54. };
  55. struct type {
  56. enum ttype ttype;
  57. union {
  58. struct ttdef tdef;
  59. struct tscalar scalar;
  60. struct tptr ptr;
  61. struct tarray array;
  62. struct tmap map;
  63. struct tstruct sou;
  64. struct tfunc func;
  65. };
  66. };
  67. int type_equal (struct type *a, struct type *b);
  68. int type_compatible(struct type *a, struct type *b);
  69. void type_dump (struct type *t, const char *name, FILE *fp);
  70. void type_dump_decl(struct type *t, FILE *fp);
  71. void type_dump_decls(FILE *fp);
  72. ssize_t type_alignof(struct type *t);
  73. ssize_t type_offsetof(struct type *t, const char *field);
  74. ssize_t type_sizeof(struct type *t);
  75. int type_add(struct type *t);
  76. int type_add_list(struct type **ts);
  77. struct type *type_array_of(struct type *type, size_t len);
  78. struct type *type_map_of(struct type *ktype, struct type *vtype);
  79. struct type *type_ptr_of(struct type *type);
  80. /* built-in types */
  81. extern struct type t_void;
  82. extern struct type t_char;
  83. extern struct type t_schar;
  84. extern struct type t_uchar;
  85. extern struct type t_short;
  86. extern struct type t_sshort;
  87. extern struct type t_ushort;
  88. extern struct type t_int;
  89. extern struct type t_sint;
  90. extern struct type t_uint;
  91. extern struct type t_long;
  92. extern struct type t_slong;
  93. extern struct type t_ulong;
  94. extern struct type t_llong;
  95. extern struct type t_sllong;
  96. extern struct type t_ullong;
  97. /* helpers */
  98. static inline int type_nargs(struct type *t)
  99. {
  100. struct tfield *f;
  101. int nargs = 0;
  102. if (!t->func.args)
  103. return 0;
  104. for (f = t->func.args; f->type; f++, nargs++);
  105. return nargs;
  106. }
  107. static inline struct type *type_base(struct type *t)
  108. {
  109. while (t->ttype == T_TYPEDEF)
  110. t = t->tdef.type;
  111. return t;
  112. }
  113. #endif /* _PLY_TYPE_H */