Imported Upstream version 0.155
[platform/upstream/elfutils.git] / tests / get-pubnames.c
1 /* Copyright (C) 2002, 2005 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 static int globcnt;
30
31 static int
32 callback (Dwarf *dbg, Dwarf_Global *gl, void *arg __attribute__ ((unused)))
33 {
34   int result = DWARF_CB_OK;
35
36   printf (" [%2d] \"%s\", die: %llu, cu: %llu\n",
37           globcnt++, gl->name, (unsigned long long int) gl->die_offset,
38           (unsigned long long int) gl->cu_offset);
39
40   Dwarf_Die cu_die;
41   const char *cuname;
42   if (dwarf_offdie (dbg, gl->cu_offset, &cu_die) == NULL
43       || (cuname = dwarf_diename (&cu_die)) == NULL)
44     {
45       puts ("failed to get CU die");
46       result = DWARF_CB_ABORT;
47     }
48   else
49     printf ("CU name: \"%s\"\n", cuname);
50
51   const char *diename;
52   Dwarf_Die die;
53   if (dwarf_offdie (dbg, gl->die_offset, &die) == NULL
54       || (diename = dwarf_diename (&die)) == NULL)
55     {
56       puts ("failed to get object die");
57       result = DWARF_CB_ABORT;
58     }
59   else
60     printf ("object name: \"%s\"\n", diename);
61
62   return result;
63 }
64
65
66 int
67 main (int argc, char *argv[])
68 {
69   int result = 0;
70   int cnt;
71
72   for (cnt = 1; cnt < argc; ++cnt)
73     {
74       int fd = open (argv[cnt], O_RDONLY);
75       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
76       if (dbg == NULL)
77         {
78           printf ("%s not usable: %s\n", argv[cnt], dwarf_errmsg (-1));
79           result = 1;
80           close (fd);
81           continue;
82         }
83
84       globcnt = 0;
85
86       if (dwarf_getpubnames (dbg, callback, NULL, 0) != 0)
87         {
88           printf ("dwarf_get_pubnames didn't return zero: %s\n",
89                   dwarf_errmsg (-1));
90           result = 1;
91         }
92
93       dwarf_end (dbg);
94       close (fd);
95     }
96
97   return result;
98 }