forked from reactos/sysreg2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraddr2line.c
More file actions
191 lines (149 loc) · 4.89 KB
/
raddr2line.c
File metadata and controls
191 lines (149 loc) · 4.89 KB
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
/*
* PROJECT: ReactOS System Regression Testing Utility
* LICENSE: GNU GPLv2 or any later version as published by the Free Software Foundation
* PURPOSE: Integrated raddr2line tool for getting source references from addresses
* COPYRIGHT: Copyright 2008-2009 Christoph von Wittich <christoph_vw@reactos.org>
* Copyright 2009 Colin Finck <colin@reactos.org>
*/
#include "sysreg.h"
static void RecurseModuleDirectory(const char* Directory, ModuleListEntry** LastElement)
{
char* EntryPath;
char* Period;
DIR* dir;
struct dirent* dp;
struct stat statbuf;
dir = opendir(Directory);
if(!dir)
return;
while ((dp = readdir(dir)))
{
if(*dp->d_name == '.')
continue;
EntryPath = (char*)malloc(strlen(Directory) + strlen(dp->d_name) + 2);
strcpy(EntryPath, Directory);
strcat(EntryPath, "/");
strcat(EntryPath, dp->d_name);
if(stat(EntryPath, &statbuf) == -1)
{
free(EntryPath);
continue;
}
if(statbuf.st_mode & S_IFDIR)
{
RecurseModuleDirectory(EntryPath, LastElement);
free(EntryPath);
}
else
{
Period = strchr(dp->d_name, '.');
/* A file needs to have one of the following extensions to be a valid module */
if(!Period || (strcasecmp(Period, ".exe") && strcasecmp(Period, ".dll") && strcasecmp(Period, ".sys")))
{
free(EntryPath);
continue;
}
(*LastElement)->Next = (ModuleListEntry*)malloc(sizeof(ModuleListEntry));
*LastElement = (*LastElement)->Next;
(*LastElement)->Next = NULL;
(*LastElement)->Module = (char*)malloc(strlen(dp->d_name) + 1);
strcpy((*LastElement)->Module, dp->d_name);
(*LastElement)->Path = EntryPath;
}
}
closedir(dir);
}
void InitializeModuleList()
{
ModuleListEntry* LastElement;
char TrunkOutput[256];
ModuleList = (ModuleListEntry*)malloc(sizeof(ModuleListEntry));
ModuleList->Next = NULL;
LastElement = ModuleList;
strcpy(TrunkOutput, OutputPath);
strcat(TrunkOutput, "/reactos/");
RecurseModuleDirectory(TrunkOutput, &LastElement);
}
void CleanModuleList()
{
ModuleListEntry* CurrentElement;
CurrentElement = ModuleList->Next;
while(CurrentElement)
{
free(CurrentElement->Module);
free(CurrentElement->Path);
ModuleListEntry* PreviousElement = CurrentElement;
CurrentElement = CurrentElement->Next;
free(PreviousElement);
}
free(ModuleList);
}
static ModuleListEntry* FindModule(const char* Module)
{
ModuleListEntry* CurrentElement;
CurrentElement = ModuleList->Next;
while(CurrentElement)
{
if(!strcmp(CurrentElement->Module, Module))
return CurrentElement;
CurrentElement = CurrentElement->Next;
}
return NULL;
}
bool ResolveAddressFromFile(char* Buffer, size_t BufferSize, const char* Data)
{
bool ReturnValue = false;
char* Address = NULL;
char* AddressStart;
char* Module = NULL;
char* pBuffer;
ModuleListEntry* ModuleEntry;
size_t AddressLength;
/* A resolvable backtrace line has to look like this:
<abcdefg.dll:123a>
*/
if(*Data != '<')
return false;
AddressStart = strchr(Data, ':');
if(!AddressStart)
return false;
++AddressStart;
AddressLength = strspn(AddressStart, "1234567890abcdefABCDEF");
if(!AddressLength || AddressStart[AddressLength] != '>')
return false;
/* Ok, this looks like a backtrace line we can resolve */
Module = (char*)malloc(AddressStart - Data - 1);
strncpy(Module, Data + 1, AddressStart - Data - 2);
Module[AddressStart - Data - 2] = 0;
Address = (char*)malloc(AddressLength + 1);
strncpy(Address, AddressStart, AddressLength);
Address[AddressLength] = 0;
/* Try to find the path to this module */
if ((ModuleEntry = FindModule(Module)))
{
char Command[256];
/* Run raddr2line */
sprintf(Command, "%s/host-tools/tools/rsym/raddr2line %s %s 2>/dev/null", OutputPath, ModuleEntry->Path, Address);
FILE* Process = popen(Command, "r");
if(!feof(Process))
{
pBuffer = &Buffer[AddressStart - Data + AddressLength];
strncpy(Buffer, Data, pBuffer - Buffer);
*pBuffer++ = ' ';
*pBuffer++ = '(';
if(fgets(pBuffer, BufferSize - (pBuffer - Buffer), Process))
{
pBuffer += strlen(pBuffer) - 1;
*pBuffer++ = ')';
*pBuffer++ = '>';
*pBuffer++ = '\n';
*pBuffer = 0;
ReturnValue = true;
}
}
pclose(Process);
}
free(Module);
free(Address);
return ReturnValue;
}