1 /* Test program for dwarf_aggregate_size. Prints size of top-level vars.
2    Copyright (C) 2014 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 void
print_var_type_size(Dwarf_Die * var)33 print_var_type_size (Dwarf_Die *var)
34 {
35   Dwarf_Attribute attr_mem;
36   Dwarf_Die type_mem;
37   Dwarf_Die *type;
38   const char *name = dwarf_diename (var);
39 
40   type = dwarf_formref_die (dwarf_attr (var, DW_AT_type, &attr_mem),
41 			    &type_mem);
42   if (type != NULL)
43     {
44       Dwarf_Word size;
45       if (dwarf_aggregate_size (type, &size) < 0)
46         printf ("%s no size: %s\n", name, dwarf_errmsg (-1));
47       else
48 	printf ("%s size %" PRIu64 "\n", name, size);
49     }
50   else
51     printf ("%s has no type.\n", name);
52 }
53 
54 int
main(int argc,char * argv[])55 main (int argc, char *argv[])
56 {
57 
58   int remaining;
59   Dwfl *dwfl;
60   (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining,
61                      &dwfl);
62   assert (dwfl != NULL);
63 
64   Dwarf_Die *cu = NULL;
65   Dwarf_Addr dwbias;
66   while ((cu = dwfl_nextcu (dwfl, cu, &dwbias)) != NULL)
67     {
68       Dwarf_Die die_mem;
69       Dwarf_Die *die = &die_mem;
70       dwarf_child (cu, &die_mem);
71 
72       while (1)
73 	{
74 	  if (dwarf_tag (die) == DW_TAG_variable)
75 	    print_var_type_size (die);
76 
77 	  if (dwarf_siblingof (die, &die_mem) != 0)
78 	    break;
79 	}
80     }
81 
82   dwfl_end (dwfl);
83 }
84