/
dwarf.cc
209 lines (152 loc) · 3.74 KB
/
dwarf.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "dwarf.hh"
#include <utils.hh>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
using namespace kcov;
DwarfParser::DwarfParser() :
m_fd(-1),
m_dwarf(NULL)
{
}
DwarfParser::~DwarfParser()
{
close();
}
void DwarfParser::forEachLine(IFileParser::ILineListener& listener)
{
if (!m_dwarf)
return;
Dwarf_Off offset = 0;
Dwarf_Off lastOffset = 0;
size_t headerSize;
/* Iterate over the headers */
while (dwarf_nextcu(m_dwarf, offset, &offset, &headerSize, 0, 0, 0) == 0) {
Dwarf_Lines* lines;
Dwarf_Files *files;
size_t lineCount;
size_t fileCount;
Dwarf_Die die;
unsigned int i;
if (dwarf_offdie(m_dwarf, lastOffset + headerSize, &die) == NULL)
continue;
lastOffset = offset;
/* Get the source lines */
if (dwarf_getsrclines(&die, &lines, &lineCount) != 0)
continue;
/* And the files */
if (dwarf_getsrcfiles(&die, &files, &fileCount) != 0)
continue;
const char *const *srcDirs;
size_t ndirs = 0;
/* Lookup the compilation path */
if (dwarf_getsrcdirs(files, &srcDirs, &ndirs) != 0)
continue;
if (ndirs == 0)
continue;
/* Iterate through the source lines */
for (i = 0; i < lineCount; i++) {
Dwarf_Line *line;
int lineNr = 0;
const char* lineSource;
Dwarf_Word mtime, len;
bool isCode;
Dwarf_Addr addr;
if ( !(line = dwarf_onesrcline(lines, i)) )
continue;
if (dwarf_lineno(line, &lineNr) != 0)
continue;
if (!(lineSource = dwarf_linesrc(line, &mtime, &len)) )
continue;
if (dwarf_linebeginstatement(line, &isCode) != 0)
continue;
if (dwarf_lineaddr(line, &addr) != 0)
continue;
// Invalid line number?
if (lineNr == 0)
continue;
// Non-code?
if (!isCode)
continue;
listener.onLine(fullPath(srcDirs, lineSource), lineNr, addr);
}
}
}
void DwarfParser::forAddress(IFileParser::ILineListener& listener, uint64_t address)
{
if (!m_dwarf)
return;
Dwarf_Off offset = 0;
Dwarf_Off lastOffset = 0;
size_t headerSize;
if (!m_dwarf)
return;
/* Iterate over the headers */
while (dwarf_nextcu(m_dwarf, offset, &offset, &headerSize, 0, 0, 0) == 0) {
Dwarf_Die die;
if (dwarf_offdie(m_dwarf, lastOffset + headerSize, &die) == NULL) {
lastOffset = offset;
continue;
}
lastOffset = offset;
Dwarf_Line *line;
Dwarf_Files *files;
size_t fileCount;
line = dwarf_getsrc_die(&die, address);
if (!line)
continue;
/* And the files */
if (dwarf_getsrcfiles(&die, &files, &fileCount) != 0)
continue;
const char* lineSource;
Dwarf_Word mtime, len;
if (!(lineSource = dwarf_linesrc(line, &mtime, &len)) )
return;
const char *const *srcDirs;
size_t ndirs = 0;
/* Lookup the compilation path */
if (dwarf_getsrcdirs(files, &srcDirs, &ndirs) != 0)
continue;
int lineNr;
if (dwarf_lineno(line, &lineNr) != 0)
continue;
listener.onLine(fullPath(srcDirs, lineSource), lineNr, address);
}
}
std::string DwarfParser::fullPath(const char *const *srcDirs, const std::string &filename)
{
/* Use the full compilation path unless the source already
* has an absolute path */
std::string fullFilePath;
std::string filePath = filename;
std::string dir = srcDirs[0] == NULL ? "" : srcDirs[0];
fullFilePath = dir_concat(dir, filename);
if (filename[0] != '/')
filePath = fullFilePath;
return filePath;
}
bool DwarfParser::open(const std::string& filename)
{
close();
m_fd = ::open(filename.c_str(), O_RDONLY);
if (m_fd < 0)
return false;
/* Initialize libdwarf from the file descriptor */
m_dwarf = dwarf_begin(m_fd, DWARF_C_READ);
if (!m_dwarf) {
::close(m_fd);
m_fd = -1;
return false;
}
return true;
}
void DwarfParser::close()
{
if (m_dwarf)
dwarf_end(m_dwarf);
if (m_fd >= 0)
::close(m_fd);
m_fd = -1;
m_dwarf = NULL;
}