Stackdb
Stackdb is a stackable, multi-target and -level source debugger and memory forensics library.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dwdebug.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, 2012, 2013, 2014 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #ifndef __LIBDWDEBUG_H__
20 #define __LIBDWDEBUG_H__
21 
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <glib.h>
25 #include <wchar.h>
26 #include <sys/types.h>
27 #include <regex.h>
28 
29 #include <elfutils/libdw.h>
30 #include <elfutils/libdwfl.h>
31 #include <elfutils/libebl.h>
32 
33 #include "debugpred.h"
34 #include "list.h"
35 #include "alist.h"
36 #include "rfilter.h"
37 #include "config.h"
38 #include "log.h"
39 #include "output.h"
40 #include "common.h"
41 #include "clfit.h"
42 
43 #if ELFUTILS_NO_VERSION_H
44 #define _INT_ELFUTILS_VERSION ELFUTILS_BIN_VERSION
45 #else
46 #include "elfutils/version.h"
47 #define _INT_ELFUTILS_VERSION _ELFUTILS_VERSION
48 #endif
49 
50 #define DWDEBUG_DEF_DELIM "."
51 
59 struct binfile;
60 struct binfile_ops;
61 struct binfile_instance;
62 
66 struct debugfile;
67 struct debugfile_ops;
68 struct debugfile_load_opts;
69 struct scope;
70 struct symbol;
71 struct lsymbol;
72 
76 struct symdict;
77 
83 void dwdebug_init(void);
84 void dwdebug_fini(void);
85 
86 void dwdebug_evict(struct debugfile *debugfile);
87 void dwdebug_evict_all(void);
88 
89 /*
90  * Our default load strategy is to always load the basic info for each
91  * CU; to load all aranges info (address range info for each CU); to
92  * load all pubnames info (gives per-CU offsets for pubnames); and to
93  * load full CUs. The flags below restrict what portions of the CUs we
94  * try to load, and can be combined with debugfile_load_opts rfilters on
95  * per-CU filenames (and on per-symbol names if PUBNAMES is set --
96  * although only the pubnames will be filtered; any of their dependent
97  * symbols will not be filtered).
98  */
99 typedef enum {
101  /* These control which CUs/DIEs are loaded.
102  * _CUHEADERS says to load only the CU headers.
103  * _PUBNAMES says to load all CU headers, but after that, only load
104  * any of the symbols in the debug_pubnames section, and their
105  * dependencies.
106  */
110  /*
111  * If this is set, all scope ranges will be added to the primary
112  * range fast lookup struct. This makes loading slow, but should
113  * result in the fastest possible lookups.
114  */
116  /*
117  * By default, declaration symbols that are defined are replaced
118  * with those definition symbols (i.e., they are deleted, and
119  * anything that referenced them is changed to use the definition
120  * symbol instead).
121  *
122  * This simplifies the view of the program the user sees; but
123  * sometimes the user might *want* to access the declaration
124  * symbol (or the defn/decl resolution code might make mistakes!);
125  * hence, this option exists.
126  */
128  /* This forces partial symbol loading, instead of the default full. */
130  /* This flag specifies that we will try to promote all per-CU types
131  * to be pubtypes, and then check if each symbol's type (if it has
132  * one) is already a "pubtype" and is equivalent to our type (well,
133  * the equivalence check is controlled by the next flag!). If we
134  * find a match, we set the symbol's datatype_ref field to the
135  * per-CU offset (so we can load the real thing if we ever *want*
136  * to), but we set the datatype field to point to the "pubtype" type
137  * symbol AND take a reference to it.
138  */
140  /* If the previous flag was set, if this is NOT set, we only check
141  * name/datacode_type matches; if it IS set, we check all type
142  * fields (including members and their types, recursively) for
143  * equivalence).
144  */
147 
148 typedef enum {
156 } symbol_type_t;
157 char *SYMBOL_TYPE(int n);
158 
159 typedef enum {
164 char *SYMBOL_SOURCE(int n);
165 
166 typedef enum {
170 } load_type_t;
171 
172 /*
173  * In the symbol struct, these fields share a 32-bit int, divided this
174  * way. If you add more symbol types, or load types, adjust these
175  * accordingly!
176  *
177  * Ok, that comment is no longer accurate. You just have to check the
178  * bitfield usage in struct symbol before changing these. We don't want
179  * to go over 64 bits total used there in all the bitfields.
180  */
181 #define LOAD_TYPE_BITS 2
182 #define SYMBOL_TYPE_BITS 3
183 #define SYMBOL_SOURCE_BITS 2
184 #define DATATYPE_CODE_BITS 4
185 #define SRCLINE_BITS 20
186 
187 /* We use this enum type for filtering during symbol searching, when the
188  * caller might accept multiple different symbol types.
189  */
190 typedef enum {
198  /*
199  * We don't distinguish "kinds" of variables, but it is useful to do
200  * so for searching! So this is nasty but ok.
201  */
207 
208 typedef enum {
226 char *DATATYPE(int n);
227 
231 struct location;
232 struct location_ops;
233 struct location_ctxt;
234 typedef enum {
247  /* add here */
249 } loctype_t;
250 
251 /*
252  * Provided so that regular users can call (l)symbol_resolve_location
253  * and still have a valid output location value, without having to know
254  * the details of the location struct (which are gory).
255  */
256 struct location *location_create(void);
257 void location_free(struct location *location);
258 
259 /*
260  * These match the dwarf encoding codes, for base types, up to 32.
261  * After that, they are what we need them to be -- i.e., special base
262  * types. These special base types need to be loaded by custom loaders
263  * in whatever is using dwdebug; probably target/, in a specific
264  * backend.
265  */
266 typedef enum {
281 
282  /*
283  * Dynamic, untyped, or partially-typed languages may have a notion
284  * of variables with dynamic types.
285  *
286  * But rather than create a custom encoding for this use, we prefer
287  * that DATATYPE_DYNAMIC is used.
288  */
289 
290  /*
291  * Some languages have a notion of string types that are not defined
292  * by another kind of base type.
293  */
295  /*
296  * Some languages have a notion of associative arrays, hashes,
297  * dictionaries, etc. This is that stuff.
298  */
300 } encoding_t;
301 
305 /*
306  * This is the only function users should call to obtain debugfiles. It
307  * tries to share already-loaded debugfiles based off a global cache;
308  * handles the case where the user passes in a file with debuginfo
309  * embedded in it, OR the case when they pass in an executable or
310  * library (by attempting to track down the debuginfo file).
311  *
312  * There are several scenarios in which we want to load a debugfile.
313  * 1) We have a filename, and we want to load the debuginfo that it
314  * contains, or that it points to (via symlink, or internal
315  * content).
316  * 2a) We have a binfile_instance with a binfile associated with it.
317  * In this case, we must first open a copy of the binfile
318  * associated with the instance data, UNLESS the binfile is already
319  * associated with this exact instance (if that is true, we just
320  * return the binfile).
321  * 2b) We have a binfile_instance with a filename, and no associated
322  * binfile. We just open the filename against the instance.
323  * 3) We have a binfile; just load debuginfo from that file, or from
324  * the file that that binfile points to.
325  */
326 struct debugfile *debugfile_from_file(char *filename,char *root_prefix,
327  struct array_list *debugfile_load_opts_list);
328 struct debugfile *debugfile_from_instance(struct binfile_instance *bfinst,
329  struct array_list *debugfile_load_opts_list);
331  struct array_list *debugfile_load_opts_list);
332 /*
333  * Search the debugfile path @DFPATH (if NULL, use default PATH) for a
334  * filepath @filename, possibly in a root_prefix dir @root_prefix, and
335  * try the @debug_postfix if @filename is not found directly (if
336  * @debug_postfix is NULL, ".debug" will be used). If @buf is not NULL,
337  * we write as much of the found path as possible (given @buflen) into
338  * @buf. On success, a valid path is returned; on error, NULL is
339  * returned and ESRCH is set. If the user did not supply @buf, a buffer
340  * is allocated.
341  */
342 char *debugfile_search_path(char *filename,char *root_prefix,char *debug_postfix,
343  const char *DFPATH[],char *buf,int buflen);
345 struct debugfile_load_opts *debugfile_load_opts_parse(char *optstr);
346 int debugfile_load_opts_checklist(struct array_list *opts_list,char *name,
347  struct debugfile_load_opts **match_saveptr);
349 char *debugfile_get_name(struct debugfile *debugfile);
351 int debugfile_filename_info(char *filename,char **realfilename,
352  char **name,char **version);
353 void debugfile_dump(struct debugfile *debugfile,struct dump_info *ud,
354  int types,int globals,int symtabs,int elfsymtab,
355  int doranges);
356 //REFCNT debugfile_release(struct debugfile *debugfile);
357 
361 /*
362  * Create a symbol. If @name_copy is set, @name will be copied into new
363  * memory; else not. @offset is a value that functions as a pointer to
364  * the raw location of the symbol description in its original file. If
365  * @full is set, this will be a full symbol, and all memory for its
366  * @symtype is allocated. Otherwise, only a partial symbol is created.
367  */
369  char *name,int name_copy,SMOFFSET offset,
370  load_type_t loadtype,struct scope *scope);
371 /*
372  * Returns the name of the symbol. Might be modified if it was an
373  * inline instance (in which case call symbol_get_name_inline() to get
374  * the inlined function's real name), or if it was an enum/struct/union
375  * (in which case we prefix the enum/struct/union part -- see
376  * symbol_build_extname).
377  */
378 char *symbol_get_name(struct symbol *symbol);
379 /*
380  * If this is an inline instance, we try to return the real name of the
381  * inlined function -- instead of the modified, unique inline instance
382  * name.
383  */
384 char *symbol_get_name_inline(struct symbol *symbol);
385 
386 char *symbol_get_srcfile(struct symbol *symbol);
387 int symbol_get_srcline(struct symbol *symbol);
388 char *symbol_get_compdirname(struct symbol *symbol);
391 /*
392  * symbol_get_datatype returns the "meaningful" datatype of symbol --
393  * i.e., it follows inline info, skips type qualifiers).
394  * symbol_get_datatype_real follows inline info, but does not skip type
395  * qualifiers.
396  *
397  * These functions do not hold a ref to the return value.
398  */
399 struct symbol *symbol_get_datatype(struct symbol *symbol);
402 
403 int symbol_is_synthetic(struct symbol *symbol);
404 int symbol_is_external(struct symbol *symbol);
405 int symbol_is_definition(struct symbol *symbol);
406 int symbol_is_declaration(struct symbol *symbol);
407 int symbol_is_decldefined(struct symbol *symbol);
408 int symbol_is_prototyped(struct symbol *symbol);
409 int symbol_is_parameter(struct symbol *symbol);
410 int symbol_is_member(struct symbol *symbol);
411 int symbol_is_enumerator(struct symbol *symbol);
413 int symbol_has_addr(struct symbol *symbol);
415 int symbol_is_inlined(struct symbol *symbol);
416 int symbol_is_declinline(struct symbol *symbol);
417 int symbol_is_bitsize(struct symbol *symbol);
418 int symbol_is_bytesize(struct symbol *symbol);
419 uint32_t symbol_get_bytesize(struct symbol *symbol);
420 uint32_t symbol_get_bitsize(struct symbol *symbol);
421 uint32_t symbol_get_bitoffset(struct symbol *symbol);
422 uint32_t symbol_get_bitctbytes(struct symbol *symbol);
425 struct symbol *symbol_find_parent(struct symbol *symbol);
426 struct symbol *symbol_find_root(struct symbol *symbol);
427 int symbol_contains_addr(struct symbol *symbol,ADDR obj_addr);
428 int symbol_type_equal(struct symbol *t1,struct symbol *t2,
429  GHashTable *eqcache,GHashTable *updated_datatype_refs);
430 int symbol_type_is_char(struct symbol *type);
431 unsigned int symbol_type_array_bytesize(struct symbol *type);
432 /* Return either type_array_bytesize or type_bytesize */
433 unsigned int symbol_type_full_bytesize(struct symbol *type);
434 struct symbol *symbol_type_skip_ptrs(struct symbol *type);
436 
437 void symbol_dump(struct symbol *symbol,struct dump_info *ud);
438 void symbol_type_dump(struct symbol *symbol,struct dump_info *ud);
439 void symbol_function_dump(struct symbol *symbol,struct dump_info *ud);
440 void symbol_var_dump(struct symbol *symbol,struct dump_info *ud);
441 
442 /*
443  * Returns a new GSList * with the ordered member symbols of @symbol.
444  * i.e., the function's arguments; the struct's members; an enumerated
445  * type's enumerators. The symbols on the list must be released with
446  * symbol_release when you finish with them.
447  */
449  symbol_type_flag_t flags);
450 /*
451  * Returns a new GSList * with the member symbols of @symbol. The
452  * symbols on the list must be released with symbol_release when you
453  * finish with them.
454  */
455 GSList *symbol_get_members(struct symbol *symbol,
456  symbol_type_flag_t flags);
457 
458 /*
459  * Returns LOCTYPE_REG, LOCTYPE_ADDR, or LOCTYPE_MEMBER_OFFSET, on success;
460  * LOCTYPE_UNKNOWN on error (or if the location really is
461  * LOCTYPE_UNKNOWN), or -LOCTYPE_X on error resolving a specific kind of
462  * location. If LOCTYPE_UNKNOWN or negative, errno should be set.
463  *
464  * Some locations cannot be resolved without @lctxt->lops. Also, if
465  * they can (i.e., they are already of LOCTYPE_ADDR or LOCTYPE_REG or
466  * LOCTYPE_MEMBER_OFFSET), the return values will not be relocated if
467  * @lctxt->lops is not set.
468  *
469  * Also note that lsymbol_resolve_location() does not allow you to
470  * resolve an offset. The only reason that symbol_resolve_location and
471  * location_resolve() do provide an offset outparam is because single
472  * symbols/locations are sometimes offsets.
473  */
474 
476  struct location_ctxt *lctxt,
477  struct location *o_loc);
479  struct location_ctxt *lctxt,
480  ADDR *start,ADDR *end,int *is_noncontiguous,
481  ADDR *o_alt_start,ADDR *o_alt_end);
483  struct location_ctxt *lctxt,
484  struct location *o_loc);
485 int lsymbol_resolve_bounds(struct lsymbol *lsymbol,ADDR base_addr,
486  struct location_ctxt *lctxt,
487  ADDR *start,ADDR *end,int *is_noncontiguous,
488  ADDR *alt_start,ADDR *alt_end);
489 
490 /*
491  * Holds a ref to @symbol. This is useful for users because some
492  * functions (i.e., symbol_get_datatype) do not hold a ref to their
493  * return value. Lookup functions tend to hold refs,
494  */
495 void symbol_hold(struct symbol *symbol);
496 /*
497  * Releases a reference to the symbol and tries to free it.
498  */
500 
501 
502 /* If we already know that @member is a member of @symbol -- i.e., it is
503  * a nested var in a struct or function, or a function/function type
504  * param, etc... we can just clone @parent's chain and extend it by one
505  * to include the child.
506  *
507  * Users might sometimes wish to call this function to obtain an lsymbol
508  * structure, but they shouldn't need to.
509  *
510  * You must call lsymbol_release() to unhold the return value.
511  */
512 struct lsymbol *lsymbol_create_from_member(struct lsymbol *parent,
513  struct symbol *member);
514 /*
515  * Clones the non-inlineinstance part of @lsymbol.
516  *
517  * You must call lsymbol_release() to unhold the return value.
518  */
520 /*
521  * Creates an lsymbol from an existing symbol. This is a best-effort
522  * process that may not lead to what you want. Why? Suppose that you
523  * lookup 'struct mystruct.mymember'. In this case, your lsymbol will
524  * have two items on the chain; one for 'struct mystruct', and one for
525  * 'mymember'. If you pass the 'mymember' symbol into this function,
526  * you will get the same chain.
527  *
528  * However, if you lookup 'ms.mymember', where 'ms' is an instance of
529  * 'mystruct', your lsymbol chain will have the two symbols for 'ms' and
530  * 'mymember'. Then if you pass the symbol for 'mymember' to this
531  * function, you will still get the 'struct mystruct', 'mymember' chain.
532  *
533  * This result is expected and happens because we cannot trace up
534  * instance chains. We cannot know *which* instance of 'struct
535  * mystruct' you want to have as the parent of 'mymember'.
536  *
537  * You must call lsymbol_release() to unhold the return value.
538  */
540 char *lsymbol_get_name(struct lsymbol *lsymbol);
541 struct symbol *lsymbol_get_symbol(struct lsymbol *lsymbol);
542 /*
543  * Returns the deepest non-inline-instance symbol in the lookup chain.
544  * So if you want the first function that contains one (or more nested)
545  * inline instance, you can use this function.
546  */
548 void lsymbol_dump(struct lsymbol *lsymbol,struct dump_info *ud);
549 /*
550  * Releases the reference to the lsymbol, and frees it if its refcnt is
551  * 0.
552  */
554 
558 /*
559  * Find the symbol table corresponding to the supplied PC.
560  */
561 struct scope *scope_lookup_addr(struct scope *scope,ADDR pc);
562 
566 /*
567  * Look up a (possibly delimited with @delim) symbol named @name in
568  * @debugfile. @flags contains symbol_type_flag_t flags controlling
569  * which kind of symbols we will return. If @flags == SYMBOL_TYPE_FLAG_NONE,
570  * we will return the first match of any symbol type on the given name;
571  * _NONE is a wildcard. If @flags is the wildcard, or has any of
572  * SYMBOL_TYPE_FLAG_VAR, SYMBOL_TYPE_FLAG_FUNC, SYMBOL_TYPE_FLAG_LABEL
573  * set, we will first consult the global names (functions or variables)
574  * in the debugfile. If no matches result from that, and @flags is the
575  * wildcard or SYMBOL_TYPE_FLAG_TYPE, we consult the global types table.
576  *
577  * If the global tables don't have our symbol, we scan through the
578  * symbol tables for each source file in the debugfile (each source file
579  * from the original compilation gets its own top-level symbol table).
580  * This part of the search is tricky; why? Because we want to return
581  * the "best" match, and the "best" match for a var/function is its
582  * definition -- not a declaration (i.e., an 'extern ...' reference).
583  * We may encounter lots of declarations before we find the definition,
584  * so it's kind of wasteful. Anyway... if we find a type match, we
585  * return it right away. If we find a declaration, we return it right
586  * away. If we find a definition, and never find a subsequent
587  * definition or type, we return the first definition we found.
588  *
589  * If this function returns an lsymbol, it takes a reference to the
590  * lsymbol itself, AND to lsymbol->symbol, and to each symbol in
591  * lsymbol->chain. lsymbol_release is the correct way to release these
592  * refs.
593  *
594  * If you know which debugfile contains your symbol, this is fastest.
595  */
597  char *name,const char *delim,
598  struct rfilter *srcfile_filter,
599  symbol_type_flag_t flags);
600 
601 /*
602  * Lookup an offset of a member in a variable (of type struct/union), or
603  * in a type of struct/union.
604  */
606  const char *name,const char *delim);
607 
608 /*
609  * Lookup an offset of a member in a variable (of type struct/union), or
610  * in a type of struct/union.
611  */
613  const char *name,const char *delim);
614 
615 /*
616  * We return a list of all matching symbols in the debugfile, that are
617  * either globals, or are at the "top level" of a CU. In other words,
618  * we do not look for nested symbols. That would be very slow...
619  *
620  * If @globals_only is set and @srcfile_filter is NULL, we only search
621  * the globals or types/shared_types hashes in the debugfile; we do not
622  * examine each CU's symtab.
623  *
624  * If @globals is set and @srcfile_filter is NOT NULL, we will search
625  * through all CUs that match srcfile_filter, but only return symbol
626  * names that match IF they are global symbols.
627  *
628  * If @globals is not set and @srcfile_filter is not set, we go through
629  * all CUs.
630  *
631  * Our search and return values will be restricted by which @flags
632  * symbol type flags you set; you can set a combination; if you set it
633  * to 0 (SYMBOL_TYPE_FLAG_NONE), we do not restrict our symbol search or
634  * return values.
635  *
636  * The returned GSList must be freed using g_list_free; if any of the
637  * debugfile/symtab hashtables is modified later (i.e., the debugfile is
638  * only partially loaded), this list may not be used.
639  *
640  * The items on this list are struct symbol *, and if you want to use
641  * them, you MUST RHOLD() them! This function does not do it for
642  * you!
643  */
645  struct rfilter *symbol_filter,
646  symbol_type_flag_t flags,
647  struct rfilter *srcfile_filter);
648 
649 /*
650  * Look up a specific address and find its symbol.
651  *
652  * This function attempts to trace back the lookup chain as best as
653  * possible; it builds up the lsymbol's lookup chain in reverse. BUT,
654  * it can only do this if the symbol at @addr is not a struct member,
655  * nor a param of a function type. Why? Because those symbols are
656  * members of a type -- not at instance -- so we don't know how to trace
657  * back further than the struct or function type, respectively.
658  *
659  * However, this should never bite us; lookups by address should only
660  * return functions or global (or static local) variables; these can
661  * always be looked up.
662  *
663  * So, we can basically return hierarchies of nested vars and function
664  * symbols.
665  *
666  * If this function returns an lsymbol, it takes a reference to the
667  * lsymbol itself, AND to lsymbol->symbol, and to each symbol in
668  * lsymbol->chain. lsymbol_release is the correct way to release these
669  * refs.
670  */
672 
674  char *filename,int line);
676  char *filename,ADDR addr);
678  ADDR addr,char **filename,int *line);
680  char *filename,int line,
681  SMOFFSET *offset,ADDR *addr);
682 
683 /* Look up one symbol in a symbol table by name.
684  *
685  * If this function returns an lsymbol, it takes a reference to the
686  * lsymbol itself, AND to lsymbol->symbol, and to each symbol in
687  * lsymbol->chain. lsymbol_release is the correct way to release these
688  * refs.
689  */
690 
691 struct lsymbol *lsymbol_lookup_sym(struct lsymbol *lsymbol,
692  const char *name,const char *delim);
693 
694 struct symbol *symbol_get_sym(struct symbol *symbol,const char *name,
695  symbol_type_flag_t flags);
696 struct lsymbol *symbol_lookup_sym(struct symbol *symbol,
697  const char *name,const char *delim);
698 
699 /*
700  * Returns a clone of @lsymbol, with reference taken, and if @newchild
701  * is not NULL, @newchild is appended
702  */
703 struct lsymbol *lsymbol_clone(struct lsymbol *lsymbol,struct symbol *newchild);
704 
705 /*
706  * @symbol may be either a SYMBOL_TYPE_TYPE, a SYMBOL_TYPE_FUNC, or
707  * a SYMBOL_TYPE_VAR. This function is really about returning an instance
708  * symbol, BUT we allow it to take an instance symbol that is either a
709  * function, so we can get the type symbol for an arg, or a
710  * struct/union, so we can get the type symbol for a member. Sugar.
711  * Hopefully not too confusing.
712  *
713  * SYMBOL_TYPE_TYPE:
714  * DATATYPE_FUNC: find an arg matching @member.
715  * DATATYPE_ENUM: find a matching enumerator.
716  * DATATYPE_(STRUCT|UNION): find a matching member. If @symbol
717  * contains anonymous members, we recurse into those in a BFS.
718  * SYMBOL_TYPE_FUNC: find an arg matching @member.
719  * SYMBOL_VAR:
720  * DATATYPE_(STRUCT|UNION): find a matching member. If @symbol
721  * contains anonymous members, we recurse into those in a BFS.
722  *
723  * If this function returns a symbol, it takes a reference to it.
724  * symbol_release is the correct way to release these refs.
725  */
726 struct symbol *symbol_get_one_member(struct symbol *symbol,char *member);
727 /*
728  * Given a starting symbol, searches its member hierarchy according to
729  * the given delimited string of member variables.
730  *
731  * If this function returns a symbol, it takes a reference to it.
732  * symbol_release is the correct way to release these refs.
733  */
734 struct symbol *symbol_get_member(struct symbol *symbol,char *memberlist,
735  const char *delim);
736 
737 /*
738  * Returns a real, valid type for the symbol. If it is an inline
739  * instance, we skip to the abstract origin root and use that datatype.
740  * If not, for now we just return @symbol->type.
741  *
742  * If this function returns a symbol, it takes a reference to it.
743  * symbol_release is the correct way to release these refs.
744  */
745 struct symbol *symbol_get_datatype(struct symbol *symbol);
746 /*
747  * Given an IP (as an object-relative address), check and see if this
748  * symbol is currently visible (in scope). To do this we, check if the
749  * IP is in the symtab's range; or if it is in any child symtab's range
750  * where no symbol in that symtab overrides the primary symbol name!
751  */
752 int symbol_visible_at_ip(struct symbol *symbol,ADDR ip);
753 
762 };
763 
764 /*
765  * Only support DWARF debugfiles right now; but also support fake
766  * debuginfo from ELF symtabs if the ELF files don't have DWARF
767  * debuginfo; that way they can use the same interfaces for symbol
768  * lookup, etc.
769  */
770 typedef enum {
776 
777 typedef enum {
783 
784 struct debugfile {
785  /*
786  * The debugfile type and flags.
787  */
790 
791  /* Our reference count. */
793  /* Our weak reference count. */
794  //REFCNT refcntw;
795 
796  /*
797  * debugfile backend type-specific ops and state.
798  */
800  void *priv;
801 
802  /* Save the options we were loaded with, forever. */
804 
805  /*
806  * The binfile we are extracting debuginfo and ELF info from.
807  */
808  struct binfile *binfile;
809 
810  /*
811  * This should be a copy of @binfile->filename; this way, we still
812  * have the name of the file even if binfile is binfile_close()d or
813  * binfile_free()d.
814  *
815  * This is a unique ID used for caching.
816  */
817  char *filename;
818 
819  /*
820  * This is a unique integer ID when we need one (XML SOAP server);
821  * just comes from ++debugfile_id_idx .
822  */
823  int id;
824 
825  /*
826  * The binfile that pointed us to @binfile; if set, it is likely a
827  * stripped executable, library, or object file that points to a
828  * non-stripped, or debuginfo-only, binfile.
829  */
830  struct binfile *binfile_pointing;
831 
832  /*
833  * The debug string table for this file. All debuginfo string pointers are
834  * checked for presence in this table before freeing.
835  *
836  * This table persists until the debugfile is freed.
837  */
838  char *dbg_strtab;
839 
840  /*
841  * The debug location table for this file.
842  *
843  * This table is only live while the file is being processed.
844  */
845  char *loctab;
846 
847  /*
848  * The range table for this file.
849  *
850  * This table is only live while the file is being processed.
851  */
852  char *rangetab;
853 
854  /*
855  * The line table for this file.
856  *
857  * This table is only live while the file is being processed.
858  */
859  char *linetab;
860 
861  /*
862  * The frame table for this file (CFA). It is live until
863  * debugfile_free, since we interpret it on-demand.
864  */
865  char *frametab;
866 
867  /* Table lengths -- moved here for struct packing. */
868  unsigned int dbg_strtablen;
869  unsigned int loctablen;
870  unsigned int rangetablen;
871  unsigned int linetablen;
872  unsigned int frametablen;
873 
874  /*
875  * Each srcfile in a debugfile gets its own SYMBOL_TYPE_ROOT symbol.
876  *
877  * h(srcfile) -> struct symbol *
878  *
879  * (Well, except for when srcfiles are included multiple times in a
880  * build; see next hash. Those srcfile/root symtab pairs are moved
881  * into the table below.)
882  */
883  GHashTable *srcfiles;
884 
885  /*
886  * Some srcfiles are included multiple times at different points in
887  * the build (should be mostly assembly source files).
888  *
889  * h(srcfile) -> struct array_list * -> struct symbol *
890  */
891  GHashTable *srcfiles_multiuse;
892 
893  /*
894  * Each CU srcfile gets its own SYMBOL_TYPE_ROOT symbol. This is a
895  * map between CU offsets (i.e., in aranges and pubnames) and symbols.
896  *
897  * h(offset) -> struct symbol *
898  */
899  GHashTable *cuoffsets;
900 
901  /*
902  * Assume that, per-debug-info file, type names are unique -- i.e.,
903  * they are not per-srcfile foreach srcfile in the debugfile.
904  *
905  * For kernel modules, we can probably shortcut this and share the
906  * main exe type table with all modules. I am less convinced we can
907  * do this for user-space libraries and the main exe -- especially
908  * for typedefs.
909  *
910  * Let's do it for now and see what breaks.
911  */
912  /* h(typename) -> struct symbol * */
913  GHashTable *types;
914 
915  /*
916  * This hashtable holds types that have been shared across srcfiles.
917  */
918  GHashTable *shared_types;
919 
920  /*
921  * Each global var/function (i.e., not declared as static, and not
922  * local to a function) symbol is also in this table. Note: when
923  * cleaning up a struct debugfile, we should free this table first,
924  * before srcfiles, and not free the values until freeing the
925  * per-srcfile symtabs.
926  *
927  * h(identifier) = struct symbol *
928  */
929  GHashTable *globals;
930 
931  /*
932  * Since types and instances can be declared in one CU, but defined
933  * in another CU, as we load, we populate this hashtable with lists
934  * of declared, undefined symbols. Then, when we load a global
935  * instance symbol, or a "global" type, we try to resolve them.
936  */
937  GHashTable *decllists;
938 
939  /*
940  * When we *do* resolve a declaration to some definition symbol, we
941  * have to save the fact that we did so somewhere, because we copy
942  * mem from the defn to decl. We would have liked to have
943  * RHOLD(decl,defn), but we cannot store the defn pointer in the
944  * decl symbol -- no room. We cannot guarantee we could trace to
945  * the debugfile (the other obvious place to store the fact that
946  * decl holds defn) during symbol_free, because the parent hierarchy
947  * may already be being torn down.
948  *
949  * So, the only strategy we are left with is when a defn is used for
950  * the first time by a decl, take a ref on the defn and store that
951  * in this table, and only release the ref when the debugfile is
952  * destroyed. This could be wasteful if we ever wanted to free
953  * parts of debugfiles but not their entirety; but we don't support
954  * that for now so it doesn't matter.
955  */
956  GHashTable *decldefnsused;
957 
958  /*
959  * Any symbol that has a fixed address location gets an entry in
960  * this table. ELF symbols from the ELF symtab may also be in this
961  * table, but debuginfo symbols always take precedence over them.
962  *
963  * h(address) = struct symbol *
964  */
965  GHashTable *addresses;
966 
967  /*
968  * Any symbol in the pubnames table for any CUs in this debugfile is
969  * in here, with a pointer to its global DIE offset (i.e., from
970  * start of debug_info section. This tells us what we need to load
971  * IF we don't already have @identifier.
972  *
973  * h(identifier) = global_die_offset
974  */
975  GHashTable *pubnames;
976 
978 
979  GHashTable *srclines;
980  GHashTable *srcaddrlines;
981 };
982 
986 };
987 
988 /*
989  * We return "elaborated" symbols from symbol lookups, and store location
990  * resolution info in here too. An elaborated symbol has the symbol's
991  * chain if it is a nested symbol after a symbol lookup was performed.
992  * After a symbol load has been performed, it also has the memory region
993  * it currently exists in, the id the region had when the symbol was
994  * loaded (so we know if we have to actually reload it later or not, OR
995  * re-resolve its addr), and the resolved address it is at. Since not
996  * all symbols may have addresses (i.e., they may be in registers, OR
997  * their location may change depending on the value of the PC), we also
998  * set an addr_valid bit only if addr is set to something real.
999  */
1000 struct lsymbol {
1001  /* Our refcnt. */
1003  /* Our weak reference count. */
1004  //REFCNT refcntw;
1005 
1006  /*
1007  * If it is not a nested symbol, only the symbol itself. Otherwise,
1008  * the deepest nested symbol.
1009  */
1010  struct symbol *symbol;
1011  /*
1012  * Contains a top-to-bottom list of symbols that have a hierarchical
1013  * relationship. This list should have @symbol at the end if it
1014  * exists and has elements.
1015  */
1017 };
1018 
1019 #endif
int symbol_is_bitsize(struct symbol *symbol)
Definition: debug.c:3059
int symbol_has_unspecified_parameters(struct symbol *symbol)
Definition: debug.c:3050
struct symbol * symbol_find_parent(struct symbol *symbol)
Definition: debug.c:3106
char * SYMBOL_TYPE(int n)
Definition: debug.c:5662
encoding_t
Definition: dwdebug.h:266
GHashTable * types
Definition: dwdebug.h:913
SMOFFSET die_offset
Definition: dwdebug.h:985
int symbol_is_external(struct symbol *symbol)
Definition: debug.c:3018
int debugfile_lookup_line_addr(struct debugfile *debugfile, char *filename, ADDR addr)
Definition: debug.c:411
int symbol_is_definition(struct symbol *symbol)
Definition: debug.c:3021
int symbol_is_inlined(struct symbol *symbol)
Definition: debug.c:3053
int symbol_type_flags_match(struct symbol *symbol, symbol_type_flag_t flags)
Definition: debug.c:3102
GHashTable * cuoffsets
Definition: dwdebug.h:899
GHashTable * decldefnsused
Definition: dwdebug.h:956
struct debugfile * debugfile_from_binfile(struct binfile *binfile, struct array_list *debugfile_load_opts_list)
Definition: debug.c:1805
int symbol_has_addr(struct symbol *symbol)
Definition: debug.c:3047
uint32_t symbol_get_bitoffset(struct symbol *symbol)
Definition: debug.c:3085
char * linetab
Definition: dwdebug.h:859
struct symbol * symbol_get_datatype(struct symbol *symbol)
Definition: debug.c:2989
SMOFFSET cu_offset
Definition: dwdebug.h:984
struct symbol * symbol
Definition: dwdebug.h:1010
load_type_t
Definition: dwdebug.h:166
debugfile_type_flag_t
Definition: dwdebug.h:777
GHashTable * srcfiles
Definition: dwdebug.h:883
int debugfile_filename_info(char *filename, char **realfilename, char **name, char **version)
GHashTable * shared_types
Definition: dwdebug.h:918
uint32_t symbol_get_bytesize(struct symbol *symbol)
Definition: debug.c:3065
unsigned int linetablen
Definition: dwdebug.h:871
OFFSET lsymbol_offsetof(struct lsymbol *lsymbol, const char *name, const char *delim)
Definition: debug.c:364
GHashTable * srcaddrlines
Definition: dwdebug.h:980
int symbol_is_decldefined(struct symbol *symbol)
Definition: debug.c:3029
int symbol_is_prototyped(struct symbol *symbol)
Definition: debug.c:3032
struct debugfile_load_opts * opts
Definition: dwdebug.h:803
struct debugfile_load_opts * debugfile_load_opts_parse(char *optstr)
Definition: debug.c:1194
int debugfile_type_flags_t
Definition: dwdebug.h:782
int32_t SMOFFSET
Definition: common.h:100
int lsymbol_resolve_bounds(struct lsymbol *lsymbol, ADDR base_addr, struct location_ctxt *lctxt, ADDR *start, ADDR *end, int *is_noncontiguous, ADDR *alt_start, ADDR *alt_end)
Definition: location.c:1566
symbol_source_t
Definition: dwdebug.h:159
struct lsymbol * lsymbol_create_from_symbol(struct symbol *symbol)
Definition: debug.c:4722
Pvoid_t clrange_t
Definition: clfit.h:34
char * lsymbol_get_name(struct lsymbol *lsymbol)
Definition: debug.c:4732
GSList * debugfile_match_syms(struct debugfile *debugfile, struct rfilter *symbol_filter, symbol_type_flag_t flags, struct rfilter *srcfile_filter)
Definition: debug.c:1034
int32_t OFFSET
Definition: common.h:65
int id
Definition: dwdebug.h:823
int symbol_is_declinline(struct symbol *symbol)
Definition: debug.c:3056
debugfile_load_flags_t flags
Definition: dwdebug.h:761
char * rangetab
Definition: dwdebug.h:852
int symbol_is_member(struct symbol *symbol)
Definition: debug.c:3038
void lsymbol_dump(struct lsymbol *lsymbol, struct dump_info *ud)
Definition: debug.c:5622
int symbol_type_equal(struct symbol *t1, struct symbol *t2, GHashTable *eqcache, GHashTable *updated_datatype_refs)
Definition: debug.c:3801
GSList * symbol_get_members(struct symbol *symbol, symbol_type_flag_t flags)
Definition: debug.c:3146
int debugfile_load_opts_checklist(struct array_list *opts_list, char *name, struct debugfile_load_opts **match_saveptr)
Definition: debug.c:1244
GHashTable * globals
Definition: dwdebug.h:929
symbol_type_t type
Definition: dwdebug_priv.h:833
char * DATATYPE(int n)
Definition: debug.c:5684
char * dbg_strtab
Definition: dwdebug.h:838
int symbol_resolve_bounds(struct symbol *symbol, struct location_ctxt *lctxt, ADDR *start, ADDR *end, int *is_noncontiguous, ADDR *o_alt_start, ADDR *o_alt_end)
Definition: location.c:1047
loctype_t symbol_resolve_location(struct symbol *symbol, struct location_ctxt *lctxt, struct location *o_loc)
Definition: location.c:546
debugfile_load_flags_t
Definition: dwdebug.h:99
symbol_type_t
Definition: dwdebug.h:148
OFFSET symbol_offsetof(struct symbol *symbol, const char *name, const char *delim)
Definition: debug.c:314
unsigned int dbg_strtablen
Definition: dwdebug.h:868
struct lsymbol * debugfile_lookup_addr(struct debugfile *debugfile, ADDR addr)
Definition: debug.c:667
void symbol_type_dump(struct symbol *symbol, struct dump_info *ud)
Definition: debug.c:5328
REFCNT refcnt
Definition: dwdebug.h:792
void location_free(struct location *location)
Definition: location.c:359
struct debugfile_ops * ops
Definition: dwdebug.h:799
unsigned int frametablen
Definition: dwdebug.h:872
struct symbol * symbol_get_one_member(struct symbol *symbol, char *member)
Definition: debug.c:4127
debugfile_type_t
Definition: dwdebug.h:770
struct debugfile * debugfile_from_instance(struct binfile_instance *bfinst, struct array_list *debugfile_load_opts_list)
Definition: debug.c:1726
struct lsymbol * lsymbol_create_noninline(struct lsymbol *lsymbol)
Definition: debug.c:4788
symbol_type_t symbol_get_type(struct symbol *symbol)
Definition: debug.c:2962
struct symbol * symbol_get_inline_origin(struct symbol *symbol)
Definition: debug.c:4162
uint32_t offset
Definition: dwdebug_priv.h:906
struct dt_argp_state opts
Definition: dumptarget.c:111
char * symbol_get_compdirname(struct symbol *symbol)
Definition: debug.c:2949
void debugfile_load_opts_free(struct debugfile_load_opts *opts)
Definition: debug.c:1275
struct symbol * symbol_get_member(struct symbol *symbol, char *memberlist, const char *delim)
Definition: debug.c:4136
struct lsymbol * debugfile_lookup_sym(struct debugfile *debugfile, char *name, const char *delim, struct rfilter *srcfile_filter, symbol_type_flag_t flags)
Definition: debug.c:1021
GHashTable * srclines
Definition: dwdebug.h:979
char * name
Definition: dwdebug_priv.h:788
struct symbol * lsymbol_get_symbol(struct lsymbol *lsymbol)
Definition: debug.c:4738
char * debugfile_get_name(struct debugfile *debugfile)
Definition: debug.c:1286
struct lsymbol * debugfile_lookup_sym_line(struct debugfile *debugfile, char *filename, int line, SMOFFSET *offset, ADDR *addr)
Definition: debug.c:491
struct debugfile * debugfile_from_file(char *filename, char *root_prefix, struct array_list *debugfile_load_opts_list)
Definition: debug.c:1584
datatype_code_t
Definition: dwdebug.h:208
void dwdebug_init(void)
Definition: debug.c:83
symbol_source_t symbol_get_source(struct symbol *symbol)
Definition: debug.c:2966
struct symbol * symbol_find_root(struct symbol *symbol)
Definition: debug.c:3119
struct symbol * symbol_type_skip_qualifiers(struct symbol *type)
Definition: debug.c:4191
REFCNT symbol_release(struct symbol *symbol)
Definition: debug.c:4318
struct lsymbol * lsymbol_clone(struct lsymbol *lsymbol, struct symbol *newchild)
Definition: debug.c:369
void symbol_dump(struct symbol *symbol, struct dump_info *ud)
Definition: debug.c:5572
int symbol_is_declaration(struct symbol *symbol)
Definition: debug.c:3026
int symbol_get_srcline(struct symbol *symbol)
Definition: debug.c:2937
struct location * location_create(void)
Definition: location.c:39
struct rfilter * debugfile_filter
Definition: dwdebug.h:758
char * loctab
Definition: dwdebug.h:845
GHashTable * srcfiles_multiuse
Definition: dwdebug.h:891
int symbol_is_inlineinstance(struct symbol *symbol)
Definition: debug.c:3044
struct binfile * binfile
Definition: dwdebug.h:808
char * frametab
Definition: dwdebug.h:865
uint32_t symbol_get_bitctbytes(struct symbol *symbol)
Definition: debug.c:3090
struct lsymbol * symbol_lookup_sym(struct symbol *symbol, const char *name, const char *delim)
Definition: debug.c:264
char * SYMBOL_SOURCE(int n)
Definition: debug.c:5675
void dwdebug_evict_all(void)
Definition: debug.c:130
unsigned int symbol_type_full_bytesize(struct symbol *type)
Definition: debug.c:4267
struct binfile * binfile_pointing
Definition: dwdebug.h:830
struct symbol * symbol_type_skip_ptrs(struct symbol *type)
Definition: debug.c:4209
struct array_list * debugfile_get_loaded_debugfiles()
Definition: debug.c:1810
loctype_t lsymbol_resolve_location(struct lsymbol *lsymbol, ADDR base_addr, struct location_ctxt *lctxt, struct location *o_loc)
Definition: location.c:1266
REFCNT refcnt
Definition: dwdebug.h:1002
struct symbol * lsymbol_get_noninline_parent_symbol(struct lsymbol *lsymbol)
Definition: debug.c:4742
uint32_t ADDR
Definition: common.h:64
struct symbol * symbol_get_sym(struct symbol *symbol, const char *name, symbol_type_flag_t flags)
Definition: debug.c:177
clrange_t ranges
Definition: dwdebug.h:977
struct scope * scope_lookup_addr(struct scope *scope, ADDR pc)
Definition: scope.c:528
void dwdebug_fini(void)
Definition: debug.c:143
struct rfilter * srcfile_filter
Definition: dwdebug.h:759
void * priv
Definition: dwdebug.h:800
struct array_list * debugfile_lookup_addrs_line(struct debugfile *debugfile, char *filename, int line)
Definition: debug.c:387
struct rfilter * symbol_filter
Definition: dwdebug.h:760
void symbol_hold(struct symbol *symbol)
struct lsymbol * lsymbol_create_from_member(struct lsymbol *parent, struct symbol *member)
Definition: debug.c:4646
char * symbol_get_srcfile(struct symbol *symbol)
Definition: debug.c:2941
char * filename
Definition: dwdebug.h:817
symbol_source_t source
Definition: dwdebug_priv.h:836
struct symbol * symbol_get_datatype_real(struct symbol *symbol)
Definition: debug.c:2970
uint32_t REFCNT
Definition: common.h:124
GHashTable * addresses
Definition: dwdebug.h:965
char * symbol_get_name(struct symbol *symbol)
Definition: debug.c:2587
int symbol_contains_addr(struct symbol *symbol, ADDR obj_addr)
Definition: debug.c:4063
int symbol_type_is_char(struct symbol *type)
Definition: debug.c:4224
GHashTable * pubnames
Definition: dwdebug.h:975
debugfile_type_flags_t flags
Definition: dwdebug.h:789
void symbol_var_dump(struct symbol *symbol, struct dump_info *ud)
Definition: debug.c:5059
uint32_t symbol_get_bitsize(struct symbol *symbol)
Definition: debug.c:3079
int symbol_visible_at_ip(struct symbol *symbol, ADDR ip)
int symbol_is_bytesize(struct symbol *symbol)
Definition: debug.c:3062
char * symbol_get_name_inline(struct symbol *symbol)
Definition: debug.c:2600
struct array_list * chain
Definition: dwdebug.h:1016
symbol_type_flag_t
Definition: dwdebug.h:190
void dwdebug_evict(struct debugfile *debugfile)
Definition: debug.c:115
int symbol_is_enumerator(struct symbol *symbol)
Definition: debug.c:3041
int symbol_is_synthetic(struct symbol *symbol)
Definition: debug.c:3015
struct lsymbol * lsymbol_lookup_sym(struct lsymbol *lsymbol, const char *name, const char *delim)
Definition: debug.c:299
unsigned int rangetablen
Definition: dwdebug.h:870
unsigned int symbol_type_array_bytesize(struct symbol *type)
Definition: debug.c:4239
int symbol_is_parameter(struct symbol *symbol)
Definition: debug.c:3035
char * debugfile_search_path(char *filename, char *root_prefix, char *debug_postfix, const char *DFPATH[], char *buf, int buflen)
Definition: debug.c:1510
unsigned int loctablen
Definition: dwdebug.h:869
ADDR symbol_get_addr(struct symbol *symbol)
Definition: debug.c:3096
void symbol_function_dump(struct symbol *symbol, struct dump_info *ud)
Definition: debug.c:5203
GHashTable * decllists
Definition: dwdebug.h:937
REFCNT lsymbol_release(struct lsymbol *lsymbol)
Definition: debug.c:4805
char * debugfile_get_version(struct debugfile *debugfile)
Definition: debug.c:1290
GSList * symbol_get_ordered_members(struct symbol *symbol, symbol_type_flag_t flags)
Definition: debug.c:3128
loctype_t
Definition: dwdebug.h:234
debugfile_type_t type
Definition: dwdebug.h:788
int debugfile_lookup_filename_line_addr(struct debugfile *debugfile, ADDR addr, char **filename, int *line)
Definition: debug.c:437
void debugfile_dump(struct debugfile *debugfile, struct dump_info *ud, int types, int globals, int symtabs, int elfsymtab, int doranges)
Definition: debug.c:4907
struct symbol * symbol_create(symbol_type_t symtype, symbol_source_t source, char *name, int name_copy, SMOFFSET offset, load_type_t loadtype, struct scope *scope)
Definition: debug.c:2550