1 /* Copyright (C) 2002, 2004, 2005, 2007 Red Hat, Inc.
2    This file is part of elfutils.
3    Written by Ulrich Drepper <drepper@redhat.com>, 2002.
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 <fcntl.h>
23 #include <libelf.h>
24 #include ELFUTILS_HEADER(dw)
25 #include <stdio.h>
26 #include <unistd.h>
27 
28 
29 int
main(int argc,char * argv[])30 main (int argc, char *argv[])
31 {
32   int result = 0;
33   int cnt;
34 
35   for (cnt = 1; cnt < argc; ++cnt)
36     {
37       int fd = open (argv[cnt], O_RDONLY);
38 
39       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
40       if (dbg == NULL)
41 	{
42 	  printf ("%s not usable\n", argv[cnt]);
43 	  result = 1;
44 	  if (fd != -1)
45 	    close (fd);
46 	  continue;
47 	}
48 
49       Dwarf_Off o = 0;
50       Dwarf_Off ncu;
51       Dwarf_Off ao;
52       size_t cuhl;
53       uint8_t asz;
54       uint8_t osz;
55       while (dwarf_nextcu (dbg, o, &ncu, &cuhl, &ao, &asz, &osz) == 0)
56 	{
57 	  printf ("cuhl = %zu, o = %llu, asz = %hhu, osz = %hhu, ncu = %llu\n",
58 		  cuhl, (unsigned long long int) ao,
59 		  asz, osz, (unsigned long long int) ncu);
60 
61 	  Dwarf_Die die_mem;
62 	  Dwarf_Die *die = dwarf_offdie (dbg, o + cuhl, &die_mem);
63 	  if (die == NULL)
64 	    {
65 	      printf ("%s: cannot get CU die\n", argv[cnt]);
66 	      result = 1;
67 	      break;
68 	    }
69 
70 	  Dwarf_Files *files;
71 	  size_t nfiles;
72 	  if (dwarf_getsrcfiles (die, &files, &nfiles) != 0)
73 	    {
74 	      printf ("%s: cannot get files\n", argv[cnt]);
75 	      result = 1;
76 	      break;
77 	    }
78 
79 	  const char *const *dirs;
80 	  size_t ndirs;
81 	  if (dwarf_getsrcdirs (files, &dirs, &ndirs) != 0)
82 	    {
83 	      printf ("%s: cannot get include directories\n", argv[cnt]);
84 	      result = 1;
85 	      break;
86 	    }
87 
88 	  if (dirs[0] == NULL)
89 	    puts (" dirs[0] = (null)");
90 	  else
91 	    printf (" dirs[0] = \"%s\"\n", dirs[0]);
92 	  for (size_t i = 1; i < ndirs; ++i)
93 	    printf (" dirs[%zu] = \"%s\"\n", i, dirs[i]);
94 
95 	  for (size_t i = 0; i < nfiles; ++i)
96 	    printf (" file[%zu] = \"%s\"\n", i,
97 		    dwarf_filesrc (files, i, NULL, NULL));
98 
99 	  o = ncu;
100 	}
101 
102       dwarf_end (dbg);
103       close (fd);
104     }
105 
106   return result;
107 }
108