blob: 188d016243d9d4404a1fae3e0a61a0360e9f662c [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Copyright (C) 2002, 2004 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02002 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00003 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
4
Mark Wielaardde2ed972012-06-05 17:15:16 +02005 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.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00009
Mark Wielaardde2ed972012-06-05 17:15:16 +020010 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000011 WITHOUT ANY WARRANTY; without even the implied warranty of
Mark Wielaardde2ed972012-06-05 17:15:16 +020012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000014
Mark Wielaardde2ed972012-06-05 17:15:16 +020015 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/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000017
Roland McGrathd7f8d0c2005-11-17 02:32:03 +000018#ifdef HAVE_CONFIG_H
19# include <config.h>
20#endif
21
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000022#include <fcntl.h>
23#include <inttypes.h>
24#include <libelf.h>
Roland McGrathd7f8d0c2005-11-17 02:32:03 +000025#include ELFUTILS_HEADER(dw)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000026#include <stdio.h>
Mark Wielaardc24b9002017-07-21 21:23:07 +020027#include <string.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000028#include <unistd.h>
29
30
31int
32main (int argc, char *argv[])
33{
34 int result = 0;
35 int cnt;
36
37 for (cnt = 1; cnt < argc; ++cnt)
38 {
39 int fd = open (argv[cnt], O_RDONLY);
40
41 Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
42 if (dbg == NULL)
43 {
44 printf ("%s not usable: %s\n", argv[cnt], dwarf_errmsg (-1));
45 close (fd);
46 continue;
47 }
48
49 Dwarf_Off cuoff = 0;
50 Dwarf_Off old_cuoff = 0;
51 size_t hsize;
52 Dwarf_Off ao;
53 uint8_t asz;
54 uint8_t osz;
55 while (dwarf_nextcu (dbg, cuoff, &cuoff, &hsize, &ao, &asz, &osz) == 0)
56 {
57 printf ("cuhl = %zu, o = %llu, asz = %hhu, osz = %hhu, ncu = %llu\n",
58 hsize, (unsigned long long int) ao,
59 asz, osz, (unsigned long long int) cuoff);
60
61 /* Get the DIE for the CU. */
62 Dwarf_Die die;
63 if (dwarf_offdie (dbg, old_cuoff + hsize, &die) == NULL)
64 {
65 /* Something went wrong. */
66 printf ("%s: cannot get CU die\n", argv[cnt]);
67 result = 1;
68 break;
69 }
70 old_cuoff = cuoff;
71
72 Dwarf_Lines *lb;
73 size_t nlb;
74 if (dwarf_getsrclines (&die, &lb, &nlb) != 0)
75 {
76 printf ("%s: cannot get lines\n", argv[cnt]);
77 result = 1;
78 break;
79 }
80
81 printf (" %zu lines\n", nlb);
82
83 for (size_t i = 0; i < nlb; ++i)
84 {
85 Dwarf_Line *l = dwarf_onesrcline (lb, i);
86 if (l == NULL)
87 {
88 printf ("%s: cannot get individual line\n", argv[cnt]);
89 result = 1;
90 break;
91 }
92
93 Dwarf_Addr addr;
94 if (dwarf_lineaddr (l, &addr) != 0)
95 addr = 0;
96 const char *file = dwarf_linesrc (l, NULL, NULL);
97 int line;
98 if (dwarf_lineno (l, &line) != 0)
99 line = 0;
100
101 printf ("%" PRIx64 ": %s:%d:", (uint64_t) addr,
102 file ?: "???", line);
103
Mark Wielaardc24b9002017-07-21 21:23:07 +0200104 /* Getting the file path through the Dwarf_Files should
105 result in the same path. */
106 Dwarf_Files *files;
107 size_t idx;
108 if (dwarf_line_file (l, &files, &idx) != 0)
109 {
110 printf ("%s: cannot get file from line (%zd): %s\n",
111 argv[cnt], i, dwarf_errmsg (-1));
112 result = 1;
113 break;
114 }
115 const char *path = dwarf_filesrc (files, idx, NULL, NULL);
116 if ((path == NULL && file != NULL)
117 || (path != NULL && file == NULL)
118 || (strcmp (file, path) != 0))
119 {
120 printf ("%s: line %zd srcline (%s) != file srcline (%s)\n",
121 argv[cnt], i, file ?: "???", path ?: "???");
122 result = 1;
123 break;
124 }
125
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000126 int column;
127 if (dwarf_linecol (l, &column) != 0)
128 column = 0;
129 if (column >= 0)
130 printf ("%d:", column);
131
132 bool is_stmt;
133 if (dwarf_linebeginstatement (l, &is_stmt) != 0)
134 is_stmt = false;
135 bool end_sequence;
136 if (dwarf_lineendsequence (l, &end_sequence) != 0)
137 end_sequence = false;
138 bool basic_block;
139 if (dwarf_lineblock (l, &basic_block) != 0)
140 basic_block = false;
141 bool prologue_end;
142 if (dwarf_lineprologueend (l, &prologue_end) != 0)
143 prologue_end = false;
144 bool epilogue_begin;
145 if (dwarf_lineepiloguebegin (l, &epilogue_begin) != 0)
146 epilogue_begin = false;
147
148 printf (" is_stmt:%s, end_seq:%s, bb:%s, prologue:%s, epilogue:%s\n",
149 is_stmt ? "yes" : "no", end_sequence ? "yes" : "no",
150 basic_block ? "yes" : "no", prologue_end ? "yes" : "no",
151 epilogue_begin ? "yes" : "no");
152 }
153 }
154
155 dwarf_end (dbg);
156 close (fd);
157 }
158
159 return result;
160}