1 /* Test cudie and subdie properties.
2    Copyright (C) 2018 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 <dwarf.h>
23 #include ELFUTILS_HEADER(dw)
24 #include <stdio.h>
25 #include <inttypes.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 
31 
32 int
main(int argc,char * argv[])33 main (int argc, char *argv[])
34 {
35   for (int i = 1; i < argc; i++)
36     {
37       printf ("file: %s\n", argv[i]);
38       int fd = open (argv[i], O_RDONLY);
39       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
40       if (dbg == NULL)
41 	{
42 	  printf ("%s not usable: %s\n", argv[i], dwarf_errmsg (-1));
43 	  return -1;
44 	}
45 
46       Dwarf_CU *cu = NULL;
47       Dwarf_Die cudie, subdie;
48       uint8_t unit_type;
49       while (dwarf_get_units (dbg, cu, &cu, NULL,
50 			      &unit_type, &cudie, &subdie) == 0)
51 	{
52 	  printf ("Got cudie: %s, unit_type: %" PRIx8 "\n",
53 		  dwarf_diename (&cudie), unit_type);
54 
55 	  int tag = dwarf_tag (&subdie);
56 	  if (unit_type == DW_UT_compile)
57 	    {
58 	      if (tag != DW_TAG_invalid)
59 		{
60 		  printf ("Not invalid: %x\n", dwarf_tag (&subdie));
61 		  return -1;
62 		}
63 	      if (dwarf_diename (&subdie) != NULL)
64 		{
65 		  printf ("Should have NULL name: %s\n",
66 			  dwarf_diename (&subdie));
67 		  return -1;
68 		}
69 	      Dwarf_Die result;
70 	      if (dwarf_siblingof (&subdie, &result) != -1)
71 		{
72 		  printf ("Should NOT have a valid sibling: %s\n",
73 			  dwarf_diename (&result));
74 		  return -1;
75 		}
76 	      if (dwarf_child (&subdie, &result) != -1)
77 		{
78 		  printf ("Should NOT have a valid child: %s\n",
79 			  dwarf_diename (&result));
80 		  return -1;
81 		}
82 	      Dwarf_Addr base, start, end;
83 	      if (dwarf_ranges (&subdie, 0, &base, &start, &end) != -1)
84 		{
85 		  printf ("Should NOT have a ranges: %s\n",
86 			  dwarf_diename (&subdie));
87 		  return -1;
88 		}
89 	      if (dwarf_cuoffset (&subdie) != (Dwarf_Off) -1)
90 		{
91 		  printf ("Should NOT have a cuoffset: %s\n",
92 			  dwarf_diename (&subdie));
93 		  return -1;
94 		}
95 	      if (dwarf_dieoffset (&subdie) != (Dwarf_Off) -1)
96 		{
97 		  printf ("Should NOT have a die offset: %s\n",
98 			  dwarf_diename (&subdie));
99 		  return -1;
100 		}
101 	      if (dwarf_getabbrev (&subdie, 0, NULL) != NULL)
102 		{
103 		  printf ("Should NOT have an abbrev: %s\n",
104 			  dwarf_diename (&subdie));
105 		  return -1;
106 		}
107 	    }
108 	  else if (unit_type == DW_UT_type)
109 	    printf ("subdie: %s\n", dwarf_diename (&subdie));
110 	  else
111 	    printf ("subdie tag: %x\n", dwarf_tag (&subdie));
112 	}
113 
114       dwarf_end (dbg);
115       close (fd);
116 
117       printf ("\n");
118     }
119 
120   return 0;
121 }
122