• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Test program for dwarf_peel_type. Peels type of top-level vars.
2    Copyright (C) 2017 Red Hat, Inc.
3    This file is part of elfutils.
4 
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    elfutils is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 
22 #include <assert.h>
23 #include <argp.h>
24 #include <inttypes.h>
25 #include <fcntl.h>
26 #include ELFUTILS_HEADER(dw)
27 #include ELFUTILS_HEADER(dwfl)
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <dwarf.h>
31 
32 #include "../libdw/known-dwarf.h"
33 
34 static const char *
dwarf_tag_string(unsigned int tag)35 dwarf_tag_string (unsigned int tag)
36 {
37   switch (tag)
38     {
39 #define DWARF_ONE_KNOWN_DW_TAG(NAME, CODE) case CODE: return #NAME;
40       DWARF_ALL_KNOWN_DW_TAG
41 #undef DWARF_ONE_KNOWN_DW_TAG
42     default:
43       return NULL;
44     }
45 }
46 
47 void
print_var_raw_type(Dwarf_Die * var)48 print_var_raw_type (Dwarf_Die *var)
49 {
50   Dwarf_Attribute attr_mem;
51   Dwarf_Die type_mem;
52   Dwarf_Die *type;
53   const char *name = dwarf_diename (var);
54 
55   type = dwarf_formref_die (dwarf_attr (var, DW_AT_type, &attr_mem),
56 			    &type_mem);
57   if (type != NULL)
58     {
59       /* Test twice, once with a separate result DIE. Then with the
60 	 DIE itself. The resulting tag should be the same. */
61       Dwarf_Die result_mem;
62       Dwarf_Die *result = &result_mem;
63       int res = dwarf_peel_type (type, result);
64       if (res < 0)
65         printf ("%s error peeling type: %s\n", name, dwarf_errmsg (-1));
66       else if (res > 0)
67 	printf ("%s missing DW_TAG_TYPE, could peel further: %s\n",
68 		name, dwarf_tag_string (dwarf_tag (result)));
69       else
70 	{
71 	  int tag = dwarf_tag (result);
72 	  printf ("%s raw type %s\n", name, dwarf_tag_string (tag));
73 	  res = dwarf_peel_type (type, type);
74 	  if (res < 0)
75 	    printf ("%s cannot peel type itself: %s\n", name,
76 		    dwarf_errmsg (-1));
77 	  else if (res > 0)
78 	printf ("%s missing DW_TAG_TYPE, could peel type further: %s\n",
79 		name, dwarf_tag_string (dwarf_tag (type)));
80 	  else if (dwarf_tag (type) != tag)
81 	    printf ("%s doesn't resolve the same: %s != %s\n", name,
82 		    dwarf_tag_string (tag),
83 		    dwarf_tag_string (dwarf_tag (type)));
84 	}
85     }
86   else
87     printf ("%s has no type.\n", name);
88 }
89 
90 int
main(int argc,char * argv[])91 main (int argc, char *argv[])
92 {
93 
94   int remaining;
95   Dwfl *dwfl;
96   (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining,
97                      &dwfl);
98   assert (dwfl != NULL);
99 
100   Dwarf_Die *cu = NULL;
101   Dwarf_Addr dwbias;
102   while ((cu = dwfl_nextcu (dwfl, cu, &dwbias)) != NULL)
103     {
104       Dwarf_Die die_mem;
105       Dwarf_Die *die = &die_mem;
106       dwarf_child (cu, &die_mem);
107 
108       while (1)
109 	{
110 	  if (dwarf_tag (die) == DW_TAG_variable)
111 	    print_var_raw_type (die);
112 
113 	  if (dwarf_siblingof (die, &die_mem) != 0)
114 	    break;
115 	}
116     }
117 
118   dwfl_end (dwfl);
119 }
120