PageRenderTime 63ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/dwarves.h

https://github.com/dtatulea/struct_map
C Header | 1129 lines | 795 code | 147 blank | 187 comment | 61 complexity | ebcb2d771f86d1a34ecd583164e6df37 MD5 | raw file
  1. #ifndef _DWARVES_H_
  2. #define _DWARVES_H_ 1
  3. /*
  4. Copyright (C) 2006 Mandriva Conectiva S.A.
  5. Copyright (C) 2006..2009 Arnaldo Carvalho de Melo <acme@redhat.com>
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation.
  9. */
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <obstack.h>
  13. #include <dwarf.h>
  14. #include <elfutils/libdwfl.h>
  15. #include "dutil.h"
  16. #include "list.h"
  17. #include "rbtree.h"
  18. #include "strings.h"
  19. struct cu;
  20. enum load_steal_kind {
  21. LSK__KEEPIT,
  22. LSK__STOLEN,
  23. LSK__STOP_LOADING,
  24. };
  25. /** struct conf_load - load configuration
  26. * @extra_dbg_info - keep original debugging format extra info
  27. * (e.g. DWARF's decl_{line,file}, id, etc)
  28. * @fixup_silly_bitfields - Fixup silly things such as "int foo:32;"
  29. * @get_addr_info - wheter to load DW_AT_location and other addr info
  30. */
  31. struct conf_load {
  32. enum load_steal_kind (*steal)(struct cu *self,
  33. struct conf_load *conf);
  34. void *cookie;
  35. char *format_path;
  36. bool extra_dbg_info;
  37. bool fixup_silly_bitfields;
  38. bool get_addr_info;
  39. };
  40. /** struct conf_fprintf - hints to the __fprintf routines
  41. *
  42. * @flat_arrays - a->foo[10][2] becomes a->foo[20]
  43. * @classes_as_structs - class f becomes struct f, CTF doesn't have a "class"
  44. */
  45. struct conf_fprintf {
  46. const char *prefix;
  47. const char *suffix;
  48. int32_t type_spacing;
  49. int32_t name_spacing;
  50. uint32_t base_offset;
  51. uint8_t indent;
  52. uint8_t expand_types:1;
  53. uint8_t expand_pointers:1;
  54. uint8_t rel_offset:1;
  55. uint8_t emit_stats:1;
  56. uint8_t suppress_comments:1;
  57. uint8_t suppress_offset_comment:1;
  58. uint8_t show_decl_info:1;
  59. uint8_t show_only_data_members:1;
  60. uint8_t no_semicolon:1;
  61. uint8_t show_first_biggest_size_base_type_member:1;
  62. uint8_t flat_arrays:1;
  63. uint8_t no_parm_names:1;
  64. uint8_t classes_as_structs:1;
  65. uint8_t hex_fmt:1;
  66. };
  67. struct cus {
  68. struct list_head cus;
  69. };
  70. struct cus *cus__new(void);
  71. void cus__delete(struct cus *self);
  72. int cus__load_file(struct cus *self, struct conf_load *conf,
  73. const char *filename);
  74. int cus__load_files(struct cus *self, struct conf_load *conf,
  75. char *filenames[]);
  76. int cus__load_dir(struct cus *self, struct conf_load *conf,
  77. const char *dirname, const char *filename_mask,
  78. const int recursive);
  79. void cus__add(struct cus *self, struct cu *cu);
  80. void cus__print_error_msg(const char *progname, const struct cus *cus,
  81. const char *filename, const int err);
  82. struct cu *cus__find_cu_by_name(const struct cus *self, const char *name);
  83. struct tag *cus__find_struct_by_name(const struct cus *self, struct cu **cu,
  84. const char *name, const int include_decls,
  85. uint16_t *id);
  86. struct function *cus__find_function_at_addr(const struct cus *self,
  87. uint64_t addr, struct cu **cu);
  88. void cus__for_each_cu(struct cus *self, int (*iterator)(struct cu *cu,
  89. void *cookie),
  90. void *cookie,
  91. struct cu *(*filter)(struct cu *cu));
  92. struct ptr_table {
  93. void **entries;
  94. uint32_t nr_entries;
  95. uint32_t allocated_entries;
  96. };
  97. struct function;
  98. struct tag;
  99. struct cu;
  100. struct variable;
  101. /* Same as DW_LANG, so that we don't have to include dwarf.h in CTF */
  102. enum dwarf_languages {
  103. LANG_C89 = 0x01, /* ISO C:1989 */
  104. LANG_C = 0x02, /* C */
  105. LANG_Ada83 = 0x03, /* ISO Ada:1983 */
  106. LANG_C_plus_plus = 0x04, /* ISO C++:1998 */
  107. LANG_Cobol74 = 0x05, /* ISO Cobol:1974 */
  108. LANG_Cobol85 = 0x06, /* ISO Cobol:1985 */
  109. LANG_Fortran77 = 0x07, /* ISO FORTRAN 77 */
  110. LANG_Fortran90 = 0x08, /* ISO Fortran 90 */
  111. LANG_Pascal83 = 0x09, /* ISO Pascal:1983 */
  112. LANG_Modula2 = 0x0a, /* ISO Modula-2:1996 */
  113. LANG_Java = 0x0b, /* Java */
  114. LANG_C99 = 0x0c, /* ISO C:1999 */
  115. LANG_Ada95 = 0x0d, /* ISO Ada:1995 */
  116. LANG_Fortran95 = 0x0e, /* ISO Fortran 95 */
  117. LANG_PL1 = 0x0f, /* ISO PL/1:1976 */
  118. LANG_Objc = 0x10, /* Objective-C */
  119. LANG_ObjC_plus_plus = 0x11, /* Objective-C++ */
  120. LANG_UPC = 0x12, /* Unified Parallel C */
  121. LANG_D = 0x13, /* D */
  122. };
  123. /** struct debug_fmt_ops - specific to the underlying debug file format
  124. *
  125. * @function__name - will be called by function__name(), giving a chance to
  126. * formats such as CTF to get this from some other place
  127. * than the global strings table. CTF does this by storing
  128. * GElf_Sym->st_name in function->name, and by using
  129. * function->name as an index into the .strtab ELF section.
  130. * @variable__name - will be called by variable__name(), see @function_name
  131. * cu__delete - called at cu__delete(), to give a chance to formats such as
  132. * CTF to keep the .strstab ELF section available till the cu is
  133. * deleted. See @function__name
  134. */
  135. struct debug_fmt_ops {
  136. const char *name;
  137. int (*init)(void);
  138. void (*exit)(void);
  139. int (*load_file)(struct cus *self,
  140. struct conf_load *conf,
  141. const char *filename);
  142. const char *(*tag__decl_file)(const struct tag *self,
  143. const struct cu *cu);
  144. uint32_t (*tag__decl_line)(const struct tag *self,
  145. const struct cu *cu);
  146. unsigned long long (*tag__orig_id)(const struct tag *self,
  147. const struct cu *cu);
  148. unsigned long long (*tag__orig_type)(const struct tag *self,
  149. const struct cu *cu);
  150. void (*tag__free_orig_info)(struct tag *self,
  151. struct cu *cu);
  152. const char *(*function__name)(struct function *self,
  153. const struct cu *cu);
  154. const char *(*variable__name)(const struct variable *self,
  155. const struct cu *cu);
  156. const char *(*strings__ptr)(const struct cu *self, strings_t s);
  157. void (*cu__delete)(struct cu *self);
  158. };
  159. struct cu {
  160. struct list_head node;
  161. struct list_head tags;
  162. struct list_head tool_list; /* To be used by tools such as ctracer */
  163. struct ptr_table types_table;
  164. struct ptr_table functions_table;
  165. struct ptr_table tags_table;
  166. struct rb_root functions;
  167. char *name;
  168. char *filename;
  169. void *priv;
  170. struct obstack obstack;
  171. struct debug_fmt_ops *dfops;
  172. Elf *elf;
  173. Dwfl_Module *dwfl;
  174. uint32_t cached_symtab_nr_entries;
  175. uint8_t addr_size;
  176. uint8_t extra_dbg_info:1;
  177. uint8_t has_addr_info:1;
  178. uint8_t uses_global_strings:1;
  179. uint16_t language;
  180. unsigned long nr_inline_expansions;
  181. size_t size_inline_expansions;
  182. uint32_t nr_functions_changed;
  183. uint32_t nr_structures_changed;
  184. size_t max_len_changed_item;
  185. size_t function_bytes_added;
  186. size_t function_bytes_removed;
  187. int build_id_len;
  188. unsigned char build_id[0];
  189. };
  190. struct cu *cu__new(const char *name, uint8_t addr_size,
  191. const unsigned char *build_id, int build_id_len,
  192. const char *filename);
  193. void cu__delete(struct cu *self);
  194. const char *cu__string(const struct cu *self, strings_t s);
  195. static inline int cu__cache_symtab(struct cu *self)
  196. {
  197. int err = dwfl_module_getsymtab(self->dwfl);
  198. if (err > 0)
  199. self->cached_symtab_nr_entries = dwfl_module_getsymtab(self->dwfl);
  200. return err;
  201. }
  202. static inline __pure bool cu__is_c_plus_plus(const struct cu *self)
  203. {
  204. return self->language == LANG_C_plus_plus;
  205. }
  206. /**
  207. * cu__for_each_cached_symtab_entry - iterate thru the cached symtab entries
  208. * @cu: struct cu instance
  209. * @id: uint32_t tag id
  210. * @pos: struct GElf_Sym iterator
  211. * @name: char pointer where the symbol_name will be stored
  212. */
  213. #define cu__for_each_cached_symtab_entry(cu, id, pos, name) \
  214. for (id = 1, \
  215. name = dwfl_module_getsym(cu->dwfl, id, &sym, NULL); \
  216. id < cu->cached_symtab_nr_entries; \
  217. ++id, name = dwfl_module_getsym(cu->dwfl, id, &sym, NULL))
  218. /**
  219. * cu__for_each_type - iterate thru all the type tags
  220. * @cu: struct cu instance to iterate
  221. * @id: uint16_t tag id
  222. * @pos: struct tag iterator
  223. *
  224. * See cu__table_nullify_type_entry and users for the reason for
  225. * the NULL test (hint: CTF Unknown types)
  226. */
  227. #define cu__for_each_type(cu, id, pos) \
  228. for (id = 1; id < cu->types_table.nr_entries; ++id) \
  229. if (!(pos = cu->types_table.entries[id])) \
  230. continue; \
  231. else
  232. /**
  233. * cu__for_each_struct - iterate thru all the struct tags
  234. * @cu: struct cu instance to iterate
  235. * @pos: struct class iterator
  236. * @id: uint16_t tag id
  237. */
  238. #define cu__for_each_struct(cu, id, pos) \
  239. for (id = 1; id < cu->types_table.nr_entries; ++id) \
  240. if (!(pos = tag__class(cu->types_table.entries[id])) || \
  241. !tag__is_struct(class__tag(pos))) \
  242. continue; \
  243. else
  244. /**
  245. * cu__for_each_function - iterate thru all the function tags
  246. * @cu: struct cu instance to iterate
  247. * @pos: struct function iterator
  248. * @id: uint32_t tag id
  249. */
  250. #define cu__for_each_function(cu, id, pos) \
  251. for (id = 0; id < cu->functions_table.nr_entries; ++id) \
  252. if (!(pos = tag__function(cu->functions_table.entries[id]))) \
  253. continue; \
  254. else
  255. /**
  256. * cu__for_each_variable - iterate thru all the global variable tags
  257. * @cu: struct cu instance to iterate
  258. * @pos: struct tag iterator
  259. * @id: uint32_t tag id
  260. */
  261. #define cu__for_each_variable(cu, id, pos) \
  262. for (id = 0; id < cu->tags_table.nr_entries; ++id) \
  263. if (!(pos = cu->tags_table.entries[id]) || \
  264. !tag__is_variable(pos)) \
  265. continue; \
  266. else
  267. int cu__add_tag(struct cu *self, struct tag *tag, long *id);
  268. int cu__table_add_tag(struct cu *self, struct tag *tag, long *id);
  269. int cu__table_nullify_type_entry(struct cu *self, uint32_t id);
  270. struct tag *cu__find_base_type_by_name(const struct cu *self, const char *name,
  271. uint16_t *id);
  272. struct tag *cu__find_base_type_by_sname_and_size(const struct cu *self,
  273. strings_t name,
  274. uint16_t bit_size,
  275. uint16_t *idp);
  276. struct tag *cu__find_enumeration_by_sname_and_size(const struct cu *self,
  277. strings_t sname,
  278. uint16_t bit_size,
  279. uint16_t *idp);
  280. struct tag *cu__find_first_typedef_of_type(const struct cu *self,
  281. const uint16_t type);
  282. struct tag *cu__find_function_by_name(const struct cu *cu, const char *name);
  283. struct tag *cu__find_struct_by_sname(const struct cu *self, strings_t sname,
  284. const int include_decls, uint16_t *idp);
  285. struct function *cu__find_function_at_addr(const struct cu *self,
  286. uint64_t addr);
  287. struct tag *cu__function(const struct cu *self, const uint32_t id);
  288. struct tag *cu__tag(const struct cu *self, const uint32_t id);
  289. struct tag *cu__type(const struct cu *self, const uint16_t id);
  290. struct tag *cu__find_struct_by_name(const struct cu *cu, const char *name,
  291. const int include_decls, uint16_t *id);
  292. bool cu__same_build_id(const struct cu *self, const struct cu *other);
  293. void cu__account_inline_expansions(struct cu *self);
  294. int cu__for_all_tags(struct cu *self,
  295. int (*iterator)(struct tag *tag,
  296. struct cu *cu, void *cookie),
  297. void *cookie);
  298. /** struct tag - basic representation of a debug info element
  299. * @priv - extra data, for instance, DWARF offset, id, decl_{file,line}
  300. * @top_level -
  301. */
  302. struct tag {
  303. struct list_head node;
  304. uint16_t type;
  305. uint16_t tag;
  306. bool visited;
  307. bool top_level;
  308. uint16_t recursivity_level;
  309. void *priv;
  310. };
  311. void tag__delete(struct tag *self, struct cu *cu);
  312. static inline int tag__is_enumeration(const struct tag *self)
  313. {
  314. return self->tag == DW_TAG_enumeration_type;
  315. }
  316. static inline int tag__is_namespace(const struct tag *self)
  317. {
  318. return self->tag == DW_TAG_namespace;
  319. }
  320. static inline int tag__is_struct(const struct tag *self)
  321. {
  322. return self->tag == DW_TAG_structure_type ||
  323. self->tag == DW_TAG_interface_type ||
  324. self->tag == DW_TAG_class_type;
  325. }
  326. static inline int tag__is_typedef(const struct tag *self)
  327. {
  328. return self->tag == DW_TAG_typedef;
  329. }
  330. static inline int tag__is_union(const struct tag *self)
  331. {
  332. return self->tag == DW_TAG_union_type;
  333. }
  334. static inline int tag__is_const(const struct tag *self)
  335. {
  336. return self->tag == DW_TAG_const_type;
  337. }
  338. static inline bool tag__is_variable(const struct tag *self)
  339. {
  340. return self->tag == DW_TAG_variable;
  341. }
  342. static inline bool tag__is_volatile(const struct tag *self)
  343. {
  344. return self->tag == DW_TAG_volatile_type;
  345. }
  346. static inline bool tag__has_namespace(const struct tag *self)
  347. {
  348. return tag__is_struct(self) ||
  349. tag__is_union(self) ||
  350. tag__is_namespace(self) ||
  351. tag__is_enumeration(self);
  352. }
  353. /**
  354. * tag__is_tag_type - is this tag derived from the 'type' class?
  355. * @tag - tag queried
  356. */
  357. static inline int tag__is_type(const struct tag *self)
  358. {
  359. return tag__is_union(self) ||
  360. tag__is_struct(self) ||
  361. tag__is_typedef(self) ||
  362. tag__is_enumeration(self);
  363. }
  364. /**
  365. * tag__is_tag_type - is this one of the possible types for a tag?
  366. * @tag - tag queried
  367. */
  368. static inline int tag__is_tag_type(const struct tag *self)
  369. {
  370. return tag__is_type(self) ||
  371. self->tag == DW_TAG_array_type ||
  372. self->tag == DW_TAG_base_type ||
  373. self->tag == DW_TAG_const_type ||
  374. self->tag == DW_TAG_pointer_type ||
  375. self->tag == DW_TAG_ptr_to_member_type ||
  376. self->tag == DW_TAG_reference_type ||
  377. self->tag == DW_TAG_subroutine_type ||
  378. self->tag == DW_TAG_volatile_type;
  379. }
  380. static inline const char *tag__decl_file(const struct tag *self,
  381. const struct cu *cu)
  382. {
  383. if (cu->dfops && cu->dfops->tag__decl_file)
  384. return cu->dfops->tag__decl_file(self, cu);
  385. return NULL;
  386. }
  387. static inline uint32_t tag__decl_line(const struct tag *self,
  388. const struct cu *cu)
  389. {
  390. if (cu->dfops && cu->dfops->tag__decl_line)
  391. return cu->dfops->tag__decl_line(self, cu);
  392. return 0;
  393. }
  394. static inline unsigned long long tag__orig_id(const struct tag *self,
  395. const struct cu *cu)
  396. {
  397. if (cu->dfops && cu->dfops->tag__orig_id)
  398. return cu->dfops->tag__orig_id(self, cu);
  399. return 0;
  400. }
  401. static inline unsigned long long tag__orig_type(const struct tag *self,
  402. const struct cu *cu)
  403. {
  404. if (cu->dfops && cu->dfops->tag__orig_type)
  405. return cu->dfops->tag__orig_type(self, cu);
  406. return 0;
  407. }
  408. static inline void tag__free_orig_info(struct tag *self, struct cu *cu)
  409. {
  410. if (cu->dfops && cu->dfops->tag__free_orig_info)
  411. cu->dfops->tag__free_orig_info(self, cu);
  412. }
  413. size_t tag__fprintf_decl_info(const struct tag *self,
  414. const struct cu *cu, FILE *fp);
  415. size_t tag__fprintf(struct tag *self, const struct cu *cu,
  416. const struct conf_fprintf *conf, FILE *fp);
  417. const char *tag__name(const struct tag *self, const struct cu *cu,
  418. char *bf, size_t len, const struct conf_fprintf *conf);
  419. void tag__not_found_die(const char *file, int line, const char *func);
  420. #define tag__assert_search_result(tag) \
  421. do { if (!tag) tag__not_found_die(__FILE__,\
  422. __LINE__, __func__); } while (0)
  423. size_t tag__size(const struct tag *self, const struct cu *cu);
  424. size_t tag__nr_cachelines(const struct tag *self, const struct cu *cu);
  425. struct tag *tag__follow_typedef(const struct tag *tag, const struct cu *cu);
  426. size_t __tag__id_not_found_fprintf(FILE *fp, uint16_t id,
  427. const char *fn, int line);
  428. #define tag__id_not_found_fprintf(fp, id) \
  429. __tag__id_not_found_fprintf(fp, id, __func__, __LINE__)
  430. int __tag__has_type_loop(const struct tag *self, const struct tag *type,
  431. char *bf, size_t len, FILE *fp,
  432. const char *fn, int line);
  433. #define tag__has_type_loop(self, type, bf, len, fp) \
  434. __tag__has_type_loop(self, type, bf, len, fp, __func__, __LINE__)
  435. struct ptr_to_member_type {
  436. struct tag tag;
  437. uint16_t containing_type;
  438. };
  439. static inline struct ptr_to_member_type *
  440. tag__ptr_to_member_type(const struct tag *self)
  441. {
  442. return (struct ptr_to_member_type *)self;
  443. }
  444. /** struct namespace - base class for enums, structs, unions, typedefs, etc
  445. *
  446. * @sname - for clones, for instance, where we can't always add a new string
  447. * @tags - class_member, enumerators, etc
  448. * @shared_tags: if this bit is set, don't free the entries in @tags
  449. */
  450. struct namespace {
  451. struct tag tag;
  452. strings_t name;
  453. uint16_t nr_tags;
  454. uint8_t shared_tags;
  455. char * sname;
  456. struct list_head tags;
  457. };
  458. static inline struct namespace *tag__namespace(const struct tag *self)
  459. {
  460. return (struct namespace *)self;
  461. }
  462. void namespace__delete(struct namespace *self, struct cu *cu);
  463. /**
  464. * namespace__for_each_tag - iterate thru all the tags
  465. * @self: struct namespace instance to iterate
  466. * @pos: struct tag iterator
  467. */
  468. #define namespace__for_each_tag(self, pos) \
  469. list_for_each_entry(pos, &(self)->tags, node)
  470. /**
  471. * namespace__for_each_tag_safe_reverse - safely iterate thru all the tags, in reverse order
  472. * @self: struct namespace instance to iterate
  473. * @pos: struct tag iterator
  474. * @n: struct class_member temp iterator
  475. */
  476. #define namespace__for_each_tag_safe_reverse(self, pos, n) \
  477. list_for_each_entry_safe_reverse(pos, n, &(self)->tags, node)
  478. void namespace__add_tag(struct namespace *self, struct tag *tag);
  479. struct ip_tag {
  480. struct tag tag;
  481. uint64_t addr;
  482. };
  483. struct inline_expansion {
  484. struct ip_tag ip;
  485. size_t size;
  486. uint64_t high_pc;
  487. };
  488. static inline struct inline_expansion *
  489. tag__inline_expansion(const struct tag *self)
  490. {
  491. return (struct inline_expansion *)self;
  492. }
  493. struct label {
  494. struct ip_tag ip;
  495. strings_t name;
  496. };
  497. static inline struct label *tag__label(const struct tag *self)
  498. {
  499. return (struct label *)self;
  500. }
  501. static inline const char *label__name(const struct label *self,
  502. const struct cu *cu)
  503. {
  504. return cu__string(cu, self->name);
  505. }
  506. enum vlocation {
  507. LOCATION_UNKNOWN,
  508. LOCATION_LOCAL,
  509. LOCATION_GLOBAL,
  510. LOCATION_REGISTER,
  511. LOCATION_OPTIMIZED
  512. } __attribute__((packed));
  513. struct variable {
  514. struct ip_tag ip;
  515. strings_t name;
  516. uint8_t external:1;
  517. uint8_t declaration:1;
  518. enum vlocation location;
  519. struct hlist_node tool_hnode;
  520. };
  521. static inline struct variable *tag__variable(const struct tag *self)
  522. {
  523. return (struct variable *)self;
  524. }
  525. const char *variable__name(const struct variable *self, const struct cu *cu);
  526. const char *variable__type_name(const struct variable *self,
  527. const struct cu *cu, char *bf, size_t len);
  528. struct lexblock {
  529. struct ip_tag ip;
  530. struct list_head tags;
  531. uint32_t size;
  532. uint16_t nr_inline_expansions;
  533. uint16_t nr_labels;
  534. uint16_t nr_variables;
  535. uint16_t nr_lexblocks;
  536. uint32_t size_inline_expansions;
  537. };
  538. static inline struct lexblock *tag__lexblock(const struct tag *self)
  539. {
  540. return (struct lexblock *)self;
  541. }
  542. void lexblock__delete(struct lexblock *self, struct cu *cu);
  543. struct function;
  544. void lexblock__add_inline_expansion(struct lexblock *self,
  545. struct inline_expansion *exp);
  546. void lexblock__add_label(struct lexblock *self, struct label *label);
  547. void lexblock__add_lexblock(struct lexblock *self, struct lexblock *child);
  548. void lexblock__add_tag(struct lexblock *self, struct tag *tag);
  549. void lexblock__add_variable(struct lexblock *self, struct variable *var);
  550. size_t lexblock__fprintf(const struct lexblock *self, const struct cu *cu,
  551. struct function *function, uint16_t indent,
  552. const struct conf_fprintf *conf, FILE *fp);
  553. struct parameter {
  554. struct tag tag;
  555. strings_t name;
  556. };
  557. static inline struct parameter *tag__parameter(const struct tag *self)
  558. {
  559. return (struct parameter *)self;
  560. }
  561. static inline const char *parameter__name(const struct parameter *self,
  562. const struct cu *cu)
  563. {
  564. return cu__string(cu, self->name);
  565. }
  566. /*
  567. * tag.tag can be DW_TAG_subprogram_type or DW_TAG_subroutine_type.
  568. */
  569. struct ftype {
  570. struct tag tag;
  571. struct list_head parms;
  572. uint16_t nr_parms;
  573. uint8_t unspec_parms; /* just one bit is needed */
  574. };
  575. static inline struct ftype *tag__ftype(const struct tag *self)
  576. {
  577. return (struct ftype *)self;
  578. }
  579. void ftype__delete(struct ftype *self, struct cu *cu);
  580. /**
  581. * ftype__for_each_parameter - iterate thru all the parameters
  582. * @self: struct ftype instance to iterate
  583. * @pos: struct parameter iterator
  584. */
  585. #define ftype__for_each_parameter(self, pos) \
  586. list_for_each_entry(pos, &(self)->parms, tag.node)
  587. /**
  588. * ftype__for_each_parameter_safe - safely iterate thru all the parameters
  589. * @self: struct ftype instance to iterate
  590. * @pos: struct parameter iterator
  591. * @n: struct parameter temp iterator
  592. */
  593. #define ftype__for_each_parameter_safe(self, pos, n) \
  594. list_for_each_entry_safe(pos, n, &(self)->parms, tag.node)
  595. /**
  596. * ftype__for_each_parameter_safe_reverse - safely iterate thru all the parameters, in reverse order
  597. * @self: struct ftype instance to iterate
  598. * @pos: struct parameter iterator
  599. * @n: struct parameter temp iterator
  600. */
  601. #define ftype__for_each_parameter_safe_reverse(self, pos, n) \
  602. list_for_each_entry_safe_reverse(pos, n, &(self)->parms, tag.node)
  603. void ftype__add_parameter(struct ftype *self, struct parameter *parm);
  604. size_t ftype__fprintf(const struct ftype *self, const struct cu *cu,
  605. const char *name, const int inlined,
  606. const int is_pointer, const int type_spacing,
  607. const struct conf_fprintf *conf, FILE *fp);
  608. size_t ftype__fprintf_parms(const struct ftype *self,
  609. const struct cu *cu, int indent,
  610. const struct conf_fprintf *conf, FILE *fp);
  611. int ftype__has_parm_of_type(const struct ftype *self, const uint16_t target,
  612. const struct cu *cu);
  613. struct function {
  614. struct ftype proto;
  615. struct lexblock lexblock;
  616. struct rb_node rb_node;
  617. strings_t name;
  618. strings_t linkage_name;
  619. uint32_t cu_total_size_inline_expansions;
  620. uint16_t cu_total_nr_inline_expansions;
  621. uint8_t inlined:2;
  622. uint8_t abstract_origin:1;
  623. uint8_t external:1;
  624. uint8_t accessibility:2; /* DW_ACCESS_{public,protected,private} */
  625. uint8_t virtuality:2; /* DW_VIRTUALITY_{none,virtual,pure_virtual} */
  626. int32_t vtable_entry;
  627. struct list_head vtable_node;
  628. /* fields used by tools */
  629. union {
  630. struct list_head tool_node;
  631. struct hlist_node tool_hnode;
  632. };
  633. void *priv;
  634. };
  635. static inline struct function *tag__function(const struct tag *self)
  636. {
  637. return (struct function *)self;
  638. }
  639. static inline struct tag *function__tag(const struct function *self)
  640. {
  641. return (struct tag *)self;
  642. }
  643. void function__delete(struct function *self, struct cu *cu);
  644. static __pure inline int tag__is_function(const struct tag *self)
  645. {
  646. return self->tag == DW_TAG_subprogram;
  647. }
  648. /**
  649. * function__for_each_parameter - iterate thru all the parameters
  650. * @self: struct function instance to iterate
  651. * @pos: struct parameter iterator
  652. */
  653. #define function__for_each_parameter(self, pos) \
  654. ftype__for_each_parameter(&self->proto, pos)
  655. const char *function__name(struct function *self, const struct cu *cu);
  656. static inline const char *function__linkage_name(const struct function *self,
  657. const struct cu *cu)
  658. {
  659. return cu__string(cu, self->linkage_name);
  660. }
  661. size_t function__fprintf_stats(const struct tag *tag_self,
  662. const struct cu *cu,
  663. const struct conf_fprintf *conf,
  664. FILE *fp);
  665. const char *function__prototype(const struct function *self,
  666. const struct cu *cu, char *bf, size_t len);
  667. static __pure inline uint64_t function__addr(const struct function *self)
  668. {
  669. return self->lexblock.ip.addr;
  670. }
  671. static __pure inline uint32_t function__size(const struct function *self)
  672. {
  673. return self->lexblock.size;
  674. }
  675. static inline int function__declared_inline(const struct function *self)
  676. {
  677. return (self->inlined == DW_INL_declared_inlined ||
  678. self->inlined == DW_INL_declared_not_inlined);
  679. }
  680. static inline int function__inlined(const struct function *self)
  681. {
  682. return (self->inlined == DW_INL_inlined ||
  683. self->inlined == DW_INL_declared_inlined);
  684. }
  685. /* struct class_member - struct, union, class member
  686. *
  687. * @bit_offset - offset in bits from the start of the struct
  688. * @bit_size - cached bit size, can be smaller than the integral type if in a bitfield
  689. * @byte_offset - offset in bytes from the start of the struct
  690. * @byte_size - cached byte size, integral type byte size for bitfields
  691. * @bitfield_offset - offset in the current bitfield
  692. * @bitfield_offset - size in the current bitfield
  693. * @bit_hole - If there is a bit hole before the next one (or the end of the struct)
  694. * @bitfield_end - Is this the last entry in a bitfield?
  695. * @accessibility - DW_ACCESS_{public,protected,private}
  696. * @virtuality - DW_VIRTUALITY_{none,virtual,pure_virtual}
  697. * @hole - If there is a hole before the next one (or the end of the struct)
  698. */
  699. struct class_member {
  700. struct tag tag;
  701. strings_t name;
  702. uint32_t bit_offset;
  703. uint32_t bit_size;
  704. uint32_t byte_offset;
  705. size_t byte_size;
  706. uint8_t bitfield_offset;
  707. uint8_t bitfield_size;
  708. uint8_t bit_hole;
  709. uint8_t bitfield_end:1;
  710. uint8_t visited:1;
  711. uint8_t accessibility:2;
  712. uint8_t virtuality:2;
  713. uint16_t hole;
  714. };
  715. void class_member__delete(struct class_member *self, struct cu *cu);
  716. static inline struct class_member *tag__class_member(const struct tag *self)
  717. {
  718. return (struct class_member *)self;
  719. }
  720. static inline const char *class_member__name(const struct class_member *self,
  721. const struct cu *cu)
  722. {
  723. return cu__string(cu, self->name);
  724. }
  725. static __pure inline int tag__is_class_member(const struct tag *self)
  726. {
  727. return self->tag == DW_TAG_member;
  728. }
  729. /**
  730. * struct type - base type for enumerations, structs and unions
  731. *
  732. * @nr_members: number of DW_TAG_member entries
  733. * @nr_tags: number of tags
  734. */
  735. struct type {
  736. struct namespace namespace;
  737. struct list_head node;
  738. uint32_t size;
  739. int32_t size_diff;
  740. uint16_t nr_members;
  741. uint8_t declaration; /* only one bit used */
  742. uint8_t definition_emitted:1;
  743. uint8_t fwd_decl_emitted:1;
  744. uint8_t resized:1;
  745. };
  746. static inline struct class *type__class(const struct type *self)
  747. {
  748. return (struct class *)self;
  749. }
  750. void type__delete(struct type *self, struct cu *cu);
  751. /**
  752. * type__for_each_tag - iterate thru all the tags
  753. * @self: struct type instance to iterate
  754. * @pos: struct tag iterator
  755. */
  756. #define type__for_each_tag(self, pos) \
  757. list_for_each_entry(pos, &(self)->namespace.tags, node)
  758. /**
  759. * type__for_each_enumerator - iterate thru the enumerator entries
  760. * @self: struct type instance to iterate
  761. * @pos: struct enumerator iterator
  762. */
  763. #define type__for_each_enumerator(self, pos) \
  764. struct list_head *__type__for_each_enumerator_head = \
  765. (self)->namespace.shared_tags ? \
  766. (self)->namespace.tags.next : \
  767. &(self)->namespace.tags; \
  768. list_for_each_entry(pos, __type__for_each_enumerator_head, tag.node)
  769. /**
  770. * type__for_each_enumerator_safe_reverse - safely iterate thru the enumerator entries, in reverse order
  771. * @self: struct type instance to iterate
  772. * @pos: struct enumerator iterator
  773. * @n: struct enumerator temp iterator
  774. */
  775. #define type__for_each_enumerator_safe_reverse(self, pos, n) \
  776. if ((self)->namespace.shared_tags) /* Do nothing */ ; else \
  777. list_for_each_entry_safe_reverse(pos, n, &(self)->namespace.tags, tag.node)
  778. /**
  779. * type__for_each_member - iterate thru the entries that use space
  780. * (data members and inheritance entries)
  781. * @self: struct type instance to iterate
  782. * @pos: struct class_member iterator
  783. */
  784. #define type__for_each_member(self, pos) \
  785. list_for_each_entry(pos, &(self)->namespace.tags, tag.node) \
  786. if (!(pos->tag.tag == DW_TAG_member || \
  787. pos->tag.tag == DW_TAG_inheritance)) \
  788. continue; \
  789. else
  790. /**
  791. * type__for_each_data_member - iterate thru the data member entries
  792. * @self: struct type instance to iterate
  793. * @pos: struct class_member iterator
  794. */
  795. #define type__for_each_data_member(self, pos) \
  796. list_for_each_entry(pos, &(self)->namespace.tags, tag.node) \
  797. if (pos->tag.tag != DW_TAG_member) \
  798. continue; \
  799. else
  800. /**
  801. * type__for_each_member_safe - safely iterate thru the entries that use space
  802. * (data members and inheritance entries)
  803. * @self: struct type instance to iterate
  804. * @pos: struct class_member iterator
  805. * @n: struct class_member temp iterator
  806. */
  807. #define type__for_each_member_safe(self, pos, n) \
  808. list_for_each_entry_safe(pos, n, &(self)->namespace.tags, tag.node) \
  809. if (pos->tag.tag != DW_TAG_member) \
  810. continue; \
  811. else
  812. /**
  813. * type__for_each_data_member_safe - safely iterate thru the data member entries
  814. * @self: struct type instance to iterate
  815. * @pos: struct class_member iterator
  816. * @n: struct class_member temp iterator
  817. */
  818. #define type__for_each_data_member_safe(self, pos, n) \
  819. list_for_each_entry_safe(pos, n, &(self)->namespace.tags, tag.node) \
  820. if (pos->tag.tag != DW_TAG_member) \
  821. continue; \
  822. else
  823. /**
  824. * type__for_each_tag_safe_reverse - safely iterate thru all tags in a type, in reverse order
  825. * @self: struct type instance to iterate
  826. * @pos: struct class_member iterator
  827. * @n: struct class_member temp iterator
  828. */
  829. #define type__for_each_tag_safe_reverse(self, pos, n) \
  830. list_for_each_entry_safe_reverse(pos, n, &(self)->namespace.tags, tag.node)
  831. void type__add_member(struct type *self, struct class_member *member);
  832. struct class_member *
  833. type__find_first_biggest_size_base_type_member(struct type *self,
  834. const struct cu *cu);
  835. struct class_member *type__find_member_by_name(const struct type *self,
  836. const struct cu *cu,
  837. const char *name);
  838. uint32_t type__nr_members_of_type(const struct type *self, const uint16_t type);
  839. struct class_member *type__last_member(struct type *self);
  840. size_t typedef__fprintf(const struct tag *tag_self, const struct cu *cu,
  841. const struct conf_fprintf *conf, FILE *fp);
  842. static inline struct type *tag__type(const struct tag *self)
  843. {
  844. return (struct type *)self;
  845. }
  846. struct class {
  847. struct type type;
  848. struct list_head vtable;
  849. uint16_t nr_vtable_entries;
  850. uint8_t nr_holes;
  851. uint8_t nr_bit_holes;
  852. uint16_t padding;
  853. uint8_t bit_padding;
  854. void *priv;
  855. };
  856. static inline struct class *tag__class(const struct tag *self)
  857. {
  858. return (struct class *)self;
  859. }
  860. static inline struct tag *class__tag(const struct class *self)
  861. {
  862. return (struct tag *)self;
  863. }
  864. struct class *class__clone(const struct class *from,
  865. const char *new_class_name, struct cu *cu);
  866. void class__delete(struct class *self, struct cu *cu);
  867. static inline struct list_head *class__tags(struct class *self)
  868. {
  869. return &self->type.namespace.tags;
  870. }
  871. static __pure inline const char *namespace__name(const struct namespace *self,
  872. const struct cu *cu)
  873. {
  874. return self->sname ?: cu__string(cu, self->name);
  875. }
  876. static __pure inline const char *type__name(const struct type *self,
  877. const struct cu *cu)
  878. {
  879. return namespace__name(&self->namespace, cu);
  880. }
  881. static __pure inline const char *class__name(struct class *self,
  882. const struct cu *cu)
  883. {
  884. return type__name(&self->type, cu);
  885. }
  886. static inline int class__is_struct(const struct class *self)
  887. {
  888. return tag__is_struct(&self->type.namespace.tag);
  889. }
  890. void class__find_holes(struct class *self);
  891. int class__has_hole_ge(const struct class *self, const uint16_t size);
  892. size_t class__fprintf(struct class *self, const struct cu *cu,
  893. const struct conf_fprintf *conf, FILE *fp);
  894. void class__add_vtable_entry(struct class *self, struct function *vtable_entry);
  895. static inline struct class_member *
  896. class__find_member_by_name(const struct class *self,
  897. const struct cu *cu, const char *name)
  898. {
  899. return type__find_member_by_name(&self->type, cu, name);
  900. }
  901. static inline uint16_t class__nr_members(const struct class *self)
  902. {
  903. return self->type.nr_members;
  904. }
  905. static inline uint32_t class__size(const struct class *self)
  906. {
  907. return self->type.size;
  908. }
  909. static inline int class__is_declaration(const struct class *self)
  910. {
  911. return self->type.declaration;
  912. }
  913. const struct class_member *class__find_bit_hole(const struct class *self,
  914. const struct class_member *trailer,
  915. const uint16_t bit_hole_size);
  916. enum base_type_float_type {
  917. BT_FP_SINGLE = 1,
  918. BT_FP_DOUBLE,
  919. BT_FP_CMPLX,
  920. BT_FP_CMPLX_DBL,
  921. BT_FP_CMPLX_LDBL,
  922. BT_FP_LDBL,
  923. BT_FP_INTVL,
  924. BT_FP_INTVL_DBL,
  925. BT_FP_INTVL_LDBL,
  926. BT_FP_IMGRY,
  927. BT_FP_IMGRY_DBL,
  928. BT_FP_IMGRY_LDBL
  929. };
  930. struct base_type {
  931. struct tag tag;
  932. strings_t name;
  933. uint16_t bit_size;
  934. uint8_t name_has_encoding:1;
  935. uint8_t is_signed:1;
  936. uint8_t is_bool:1;
  937. uint8_t is_varargs:1;
  938. uint8_t float_type:4;
  939. };
  940. static inline struct base_type *tag__base_type(const struct tag *self)
  941. {
  942. return (struct base_type *)self;
  943. }
  944. static inline uint16_t base_type__size(const struct tag *self)
  945. {
  946. return tag__base_type(self)->bit_size / 8;
  947. }
  948. const char *base_type__name(const struct base_type *self, const struct cu *cu,
  949. char *bf, size_t len);
  950. void base_type_name_to_size_table__init(struct strings *strings);
  951. size_t base_type__name_to_size(struct base_type *self, struct cu *cu);
  952. struct array_type {
  953. struct tag tag;
  954. uint32_t *nr_entries;
  955. uint8_t dimensions;
  956. bool is_vector;
  957. };
  958. static inline struct array_type *tag__array_type(const struct tag *self)
  959. {
  960. return (struct array_type *)self;
  961. }
  962. struct enumerator {
  963. struct tag tag;
  964. strings_t name;
  965. uint32_t value;
  966. };
  967. static inline const char *enumerator__name(const struct enumerator *self,
  968. const struct cu *cu)
  969. {
  970. return cu__string(cu, self->name);
  971. }
  972. void enumeration__delete(struct type *self, struct cu *cu);
  973. void enumeration__add(struct type *self, struct enumerator *enumerator);
  974. size_t enumeration__fprintf(const struct tag *tag_self, const struct cu *cu,
  975. const struct conf_fprintf *conf, FILE *fp);
  976. int dwarves__init(uint16_t user_cacheline_size);
  977. void dwarves__exit(void);
  978. const char *dwarf_tag_name(const uint32_t tag);
  979. struct argp_state;
  980. void dwarves_print_version(FILE *fp, struct argp_state *state);
  981. #endif /* _DWARVES_H_ */