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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
// Convert DMD CodeView debug information to PDB files
// Copyright (c) 2009-2010 by Rainer Schuetze, All Rights Reserved
//
// License for redistribution is given by the Artistic License 2.0
// see file LICENSE for further details
#include "PEImage.h"
#include "cv2pdb.h"
#include "symutil.h"
#include <direct.h>
double
#include "../VERSION"
;
#ifdef UNICODE
#define T_toupper towupper
#define T_getdcwd _wgetdcwd
#define T_strlen wcslen
#define T_strcpy wcscpy
#define T_strcat wcscat
#define T_strstr wcsstr
#define T_strncmp wcsncmp
#define T_strtoul wcstoul
#define T_strtod wcstod
#define T_strrchr wcsrchr
#define T_unlink _wremove
#define T_main wmain
#define SARG "%S"
#define T_stat _wstat
#else
#define T_toupper toupper
#define T_getdcwd _getdcwd
#define T_strlen strlen
#define T_strcpy strcpy
#define T_strcat strcat
#define T_strstr strstr
#define T_strncmp strncmp
#define T_strtoul strtoul
#define T_strtod strtod
#define T_strrchr strrchr
#define T_unlink unlink
#define T_main main
#define SARG "%s"
#define T_stat stat
#endif
void fatal(const char *message, ...)
{
va_list argptr;
va_start(argptr, message);
vprintf(message, argptr);
va_end(argptr);
printf("\n");
exit(1);
}
void makefullpath(TCHAR* pdbname)
{
TCHAR* pdbstart = pdbname;
TCHAR fullname[260];
TCHAR* pfullname = fullname;
if (!pdbname || T_strlen(pdbname) < 2)
{
return;
}
// If the path starts with "\\\\", it is considered to be a full path, such as UNC path, VolumeGUID path: "\\\\?\\Volume"
if (pdbname[0] == '\\' && pdbname[1] == '\\')
{
return;
}
int drive = 0;
if (pdbname[0] && pdbname[1] == ':')
{
if (pdbname[2] == '\\' || pdbname[2] == '/')
return;
drive = T_toupper (pdbname[0]) - 'A' + 1;
pdbname += 2;
}
else
{
drive = _getdrive();
}
if (*pdbname != '\\' && *pdbname != '/')
{
T_getdcwd(drive, pfullname, sizeof(fullname)/sizeof(fullname[0]) - 2);
pfullname += T_strlen(pfullname);
if (pfullname[-1] != '\\')
*pfullname++ = '\\';
}
else
{
*pfullname++ = 'a' - 1 + drive;
*pfullname++ = ':';
}
T_strcpy(pfullname, pdbname);
T_strcpy(pdbstart, fullname);
for(TCHAR*p = pdbstart; *p; p++)
if (*p == '/')
*p = '\\';
// remove relative parts "./" and "../"
while (TCHAR* p = T_strstr (pdbstart, TEXT("\\.\\")))
T_strcpy(p, p + 2);
while (TCHAR* p = T_strstr (pdbstart, TEXT("\\..\\")))
{
for (TCHAR* q = p - 1; q >= pdbstart; q--)
if (*q == '\\')
{
T_strcpy(q, p + 3);
break;
}
}
}
TCHAR* changeExtension(TCHAR* dbgname, const TCHAR* exename, const TCHAR* ext)
{
T_strcpy(dbgname, exename);
TCHAR *pDot = T_strrchr(dbgname, '.');
if (!pDot || pDot <= T_strrchr(dbgname, '/') || pDot <= T_strrchr(dbgname, '\\'))
T_strcat(dbgname, ext);
else
T_strcpy(pDot, ext);
return dbgname;
}
int T_main(int argc, TCHAR* argv[])
{
double Dversion = 2.072;
const TCHAR* pdbref = 0;
DebugLevel debug = DebugLevel{};
CoInitialize(nullptr);
while (argc > 1 && argv[1][0] == '-')
{
argv++;
argc--;
if (argv[0][1] == '-')
break;
if (argv[0][1] == 'D')
Dversion = T_strtod(argv[0] + 2, 0);
else if (argv[0][1] == 'C')
Dversion = 0;
else if (argv[0][1] == 'n')
demangleSymbols = false;
else if (argv[0][1] == 'e')
useTypedefEnum = true;
else if (!T_strncmp(&argv[0][1], TEXT("debug"), 5)) // debug[level]
{
debug = (DebugLevel)T_strtoul(&argv[0][6], 0, 0);
if (!debug) {
debug = DbgBasic;
}
fprintf(stderr, "Debug set to %x\n", debug);
}
else if (argv[0][1] == 's' && argv[0][2])
dotReplacementChar = (char)argv[0][2];
else if (argv[0][1] == 'p' && argv[0][2])
pdbref = argv[0] + 2;
else
fatal("unknown option: " SARG, argv[0]);
}
if (argc < 2)
{
printf("Convert DMD CodeView/DWARF debug information to PDB files, Version %.02f\n", VERSION);
printf("Copyright (c) 2009-2012 by Rainer Schuetze, All Rights Reserved\n");
printf("\n");
printf("License for redistribution is given by the Artistic License 2.0\n");
printf("see file LICENSE for further details\n");
printf("\n");
printf("usage: " SARG " [-D<version>|-C|-n|-e|-s<C>|-p<embedded-pdb>] <exe-file> [new-exe-file] [pdb-file]\n", argv[0]);
return -1;
}
PEImage exe, dbg, *img = NULL;
TCHAR dbgname[MAX_PATH];
if (!exe.loadExe(argv[1]))
fatal(SARG ": %s", argv[1], exe.getLastError());
if (exe.countCVEntries() || exe.hasDWARF())
img = &exe;
else
{
// try DBG file alongside executable
changeExtension(dbgname, argv[1], TEXT(".dbg"));
struct _stat buffer;
if (T_stat(dbgname, &buffer) != 0)
fatal(SARG ": no codeview debug entries found", argv[1]);
if (!dbg.loadExe(dbgname))
fatal(SARG ": %s", dbgname, dbg.getLastError());
if (dbg.countCVEntries() == 0)
fatal(SARG ": no codeview debug entries found", dbgname);
img = &dbg;
}
CV2PDB cv2pdb(*img, debug);
cv2pdb.Dversion = Dversion;
cv2pdb.initLibraries();
TCHAR* outname = argv[1];
if (argc > 2 && argv[2][0])
outname = argv[2];
TCHAR pdbname[260];
if (argc > 3)
T_strcpy (pdbname, argv[3]);
else
{
T_strcpy (pdbname, outname);
TCHAR *pDot = T_strrchr (pdbname, '.');
if (!pDot || pDot <= T_strrchr (pdbname, '/') || pDot <= T_strrchr (pdbname, '\\'))
T_strcat (pdbname, TEXT(".pdb"));
else
T_strcpy (pDot, TEXT(".pdb"));
}
makefullpath(pdbname);
T_unlink(pdbname);
if(!cv2pdb.openPDB(pdbname, pdbref))
fatal(SARG ": %s", pdbname, cv2pdb.getLastError());
if(exe.hasDWARF())
{
if(!exe.relocateDebugLineInfo(0x400000))
fatal(SARG ": %s", argv[1], cv2pdb.getLastError());
if(!cv2pdb.createDWARFModules())
fatal(SARG ": %s", pdbname, cv2pdb.getLastError());
if(!cv2pdb.addDWARFTypes())
fatal(SARG ": %s", pdbname, cv2pdb.getLastError());
if(!cv2pdb.addDWARFLines())
fatal(SARG ": %s", pdbname, cv2pdb.getLastError());
if (!cv2pdb.addDWARFPublics())
fatal(SARG ": %s", pdbname, cv2pdb.getLastError());
if (!cv2pdb.writeDWARFImage(outname))
fatal(SARG ": %s", outname, cv2pdb.getLastError());
}
else
{
if (!cv2pdb.initSegMap())
fatal(SARG ": %s", argv[1], cv2pdb.getLastError());
if (!cv2pdb.initGlobalSymbols())
fatal(SARG ": %s", argv[1], cv2pdb.getLastError());
if (!cv2pdb.initGlobalTypes())
fatal(SARG ": %s", argv[1], cv2pdb.getLastError());
if (!cv2pdb.createModules())
fatal(SARG ": %s", pdbname, cv2pdb.getLastError());
if (!cv2pdb.addTypes())
fatal(SARG ": %s", pdbname, cv2pdb.getLastError());
if (!cv2pdb.addSymbols())
fatal(SARG ": %s", pdbname, cv2pdb.getLastError());
if (!cv2pdb.addSrcLines())
fatal(SARG ": %s", pdbname, cv2pdb.getLastError());
if (!cv2pdb.addPublics())
fatal(SARG ": %s", pdbname, cv2pdb.getLastError());
if (!exe.isDBG())
if (!cv2pdb.writeImage(outname, exe))
fatal(SARG ": %s", outname, cv2pdb.getLastError());
}
return 0;
}
|