2005-08-23 Roland McGrath <roland@redhat.com>
[platform/upstream/elfutils.git] / tests / get-files.c
1 /* Copyright (C) 2002, 2004, 2005 Red Hat, Inc.
2    Written by Ulrich Drepper <drepper@redhat.com>, 2002.
3
4    This program is Open Source software; you can redistribute it and/or
5    modify it under the terms of the Open Software License version 1.0 as
6    published by the Open Source Initiative.
7
8    You should have received a copy of the Open Software License along
9    with this program; if not, you may obtain a copy of the Open Software
10    License version 1.0 from http://www.opensource.org/licenses/osl.php or
11    by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
12    3001 King Ranch Road, Ukiah, CA 95482.   */
13
14 #include <fcntl.h>
15 #include <libelf.h>
16 #include <libdw.h>
17 #include <stdio.h>
18 #include <unistd.h>
19
20
21 int
22 main (int argc, char *argv[])
23 {
24   int result = 0;
25   int cnt;
26
27   for (cnt = 1; cnt < argc; ++cnt)
28     {
29       int fd = open (argv[cnt], O_RDONLY);
30
31       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
32       if (dbg == NULL)
33         {
34           printf ("%s not usable\n", argv[cnt]);
35           result = 1;
36           if (fd != -1)
37             close (fd);
38           continue;
39         }
40
41       Dwarf_Off o = 0;
42       Dwarf_Off ncu;
43       Dwarf_Off ao;
44       size_t cuhl;
45       uint8_t asz;
46       uint8_t osz;
47       while (dwarf_nextcu (dbg, o, &ncu, &cuhl, &ao, &asz, &osz) == 0)
48         {
49           printf ("cuhl = %zu, o = %llu, asz = %hhu, osz = %hhu, ncu = %llu\n",
50                   cuhl, (unsigned long long int) ao,
51                   asz, osz, (unsigned long long int) ncu);
52
53           Dwarf_Die die_mem;
54           Dwarf_Die *die = dwarf_offdie (dbg, o + cuhl, &die_mem);
55           if (die == NULL)
56             {
57               printf ("%s: cannot get CU die\n", argv[cnt]);
58               result = 1;
59               break;
60             }
61
62           Dwarf_Files *files;
63           size_t nfiles;
64           if (dwarf_getsrcfiles (die, &files, &nfiles) != 0)
65             {
66               printf ("%s: cannot get files\n", argv[cnt]);
67               result = 1;
68               break;
69             }
70
71           for (size_t i = 0; i < nfiles; ++i)
72             printf (" file[%zu] = \"%s\"\n", i,
73                     dwarf_filesrc (files, i, NULL, NULL));
74
75           o = ncu;
76         }
77
78       dwarf_end (dbg);
79       close (fd);
80     }
81
82   return result;
83 }