Imported Upstream version 0.153
[platform/upstream/elfutils.git] / tests / show-abbrev.c
1 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005 Red Hat, Inc.
2    This file is part of Red Hat elfutils.
3    Written by Ulrich Drepper <drepper@redhat.com>, 1998.
4
5    Red Hat elfutils is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by the
7    Free Software Foundation; version 2 of the License.
8
9    Red Hat elfutils is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License along
15    with Red Hat elfutils; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
17
18    Red Hat elfutils is an included package of the Open Invention Network.
19    An included package of the Open Invention Network is a package for which
20    Open Invention Network licensees cross-license their patents.  No patent
21    license is granted, either expressly or impliedly, by designation as an
22    included package.  Should you wish to participate in the Open Invention
23    Network licensing program, please visit www.openinventionnetwork.com
24    <http://www.openinventionnetwork.com>.  */
25
26 #include <config.h>
27
28 #include <fcntl.h>
29 #include <inttypes.h>
30 #include ELFUTILS_HEADER(dw)
31 #include <stdio.h>
32 #include <unistd.h>
33
34
35 int
36 main (int argc, char *argv[])
37 {
38   int cnt;
39
40   for (cnt = 1; cnt < argc; ++cnt)
41     {
42       int fd = open (argv[cnt], O_RDONLY);
43       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
44       if  (dbg == NULL)
45         {
46           printf ("%s not usable: %s\n", argv[cnt], dwarf_errmsg (-1));
47           close  (fd);
48           continue;
49         }
50
51       Dwarf_Off cuoff = 0;
52       Dwarf_Off old_cuoff = 0;
53       size_t hsize;
54       while (dwarf_nextcu (dbg, cuoff, &cuoff, &hsize, NULL, NULL, NULL) == 0)
55         {
56           /* Get the DIE for the CU.  */
57           Dwarf_Die die;
58           if (dwarf_offdie (dbg, old_cuoff + hsize, &die) == NULL)
59             /* Something went wrong.  */
60             break;
61
62           Dwarf_Off offset = 0;
63
64           while (1)
65             {
66               size_t length;
67               Dwarf_Abbrev *abbrev = dwarf_getabbrev (&die, offset, &length);
68               if (abbrev == NULL || abbrev == DWARF_END_ABBREV)
69                 /* End of the list.  */
70                 break;
71
72               unsigned tag = dwarf_getabbrevtag (abbrev);
73               if (tag == 0)
74                 {
75                   printf ("dwarf_getabbrevtag at offset %llu returned error: %s\n",
76                           (unsigned long long int) offset,
77                           dwarf_errmsg (-1));
78                   break;
79                 }
80
81               unsigned code = dwarf_getabbrevcode (abbrev);
82               if (code == 0)
83                 {
84                   printf ("dwarf_getabbrevcode at offset %llu returned error: %s\n",
85                           (unsigned long long int) offset,
86                           dwarf_errmsg (-1));
87                   break;
88                 }
89
90               int children = dwarf_abbrevhaschildren (abbrev);
91               if (children < 0)
92                 {
93                   printf ("dwarf_abbrevhaschildren at offset %llu returned error: %s\n",
94                           (unsigned long long int) offset,
95                           dwarf_errmsg (-1));
96                   break;
97                 }
98
99               printf ("abbrev[%llu]: code = %u, tag = %u, children = %d\n",
100                       (unsigned long long int) offset, code, tag, children);
101
102               size_t attrcnt;
103               if (dwarf_getattrcnt (abbrev, &attrcnt) != 0)
104                 {
105                   printf ("dwarf_getattrcnt at offset %llu returned error: %s\n",
106                           (unsigned long long int) offset,
107                           dwarf_errmsg (-1));
108                   break;
109                 }
110
111               unsigned int attr_num;
112               unsigned int attr_form;
113               Dwarf_Off aboffset;
114               size_t j;
115               for (j = 0; j < attrcnt; ++j)
116                 if (dwarf_getabbrevattr (abbrev, j, &attr_num, &attr_form,
117                                          &aboffset))
118                   printf ("dwarf_getabbrevattr for abbrev[%llu] and index %zu failed\n",
119                           (unsigned long long int) offset, j);
120                 else
121                   printf ("abbrev[%llu]: attr[%zu]: code = %u, form = %u, offset = %" PRIu64 "\n",
122                           (unsigned long long int) offset, j, attr_num,
123                           attr_form, (uint64_t) aboffset);
124
125               offset += length;
126             }
127
128           old_cuoff = cuoff;
129         }
130
131       if (dwarf_end (dbg) != 0)
132         printf ("dwarf_end failed for %s: %s\n", argv[cnt],
133                 dwarf_errmsg (-1));
134
135       close (fd);
136     }
137
138   return 0;
139 }