1 /* A variant of get-files test that uses dwarf_next_lines.
2    Copyright (C) 2002, 2004, 2005, 2007, 2014, 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 <fcntl.h>
23 #include <inttypes.h>
24 #include <libelf.h>
25 #include ELFUTILS_HEADER(dw)
26 #include <stdio.h>
27 #include <unistd.h>
28 
29 
30 int
main(int argc,char * argv[])31 main (int argc, char *argv[])
32 {
33   int result = 0;
34   int cnt;
35 
36   for (cnt = 1; cnt < argc; ++cnt)
37     {
38       int fd = open (argv[cnt], O_RDONLY);
39 
40       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
41       if (dbg == NULL)
42 	{
43 	  printf ("%s not usable\n", argv[cnt]);
44 	  result = 1;
45 	  if (fd != -1)
46 	    close (fd);
47 	  continue;
48 	}
49 
50       Dwarf_Off off;
51       Dwarf_Off next_off = 0;
52       Dwarf_CU *cu = NULL;
53       Dwarf_Files *files;
54       size_t nfiles;
55       int res;
56       while ((res = dwarf_next_lines (dbg, off = next_off, &next_off, &cu,
57 				      &files, &nfiles, NULL, NULL)) == 0)
58 	{
59 	  printf ("off = %" PRIu64 "\n", off);
60 
61 	  const char *const *dirs;
62 	  size_t ndirs;
63 	  if (dwarf_getsrcdirs (files, &dirs, &ndirs) != 0)
64 	    {
65 	      printf ("%s: cannot get include directories\n", argv[cnt]);
66 	      result = 1;
67 	      break;
68 	    }
69 
70 	  if (dirs[0] == NULL)
71 	    puts (" dirs[0] = (null)");
72 	  else
73 	    printf (" dirs[0] = \"%s\"\n", dirs[0]);
74 	  for (size_t i = 1; i < ndirs; ++i)
75 	    printf (" dirs[%zu] = \"%s\"\n", i, dirs[i]);
76 
77 	  for (size_t i = 0; i < nfiles; ++i)
78 	    printf (" file[%zu] = \"%s\"\n", i,
79 		    dwarf_filesrc (files, i, NULL, NULL));
80 	}
81 
82       if (res < 0)
83 	{
84 	  printf ("dwarf_next_lines failed: %s\n", dwarf_errmsg (-1));
85 	  result = 1;
86 	}
87 
88       dwarf_end (dbg);
89       close (fd);
90     }
91 
92   return result;
93 }
94