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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
|
/* $Id: RTDbgSymSrv.cpp $ */
/** @file
* IPRT - Debug Symbol Server.
*/
/*
* Copyright (C) 2021-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
* in the VirtualBox distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/assert.h>
#include <iprt/buildconfig.h>
#include <iprt/err.h>
#include <iprt/dir.h>
#include <iprt/env.h>
#include <iprt/file.h>
#include <iprt/log.h>
#include <iprt/getopt.h>
#include <iprt/http.h>
#include <iprt/http-server.h>
#include <iprt/initterm.h>
#include <iprt/string.h>
#include <iprt/stream.h>
#include <iprt/message.h>
#include <iprt/path.h>
#include <iprt/process.h>
#include <iprt/thread.h>
#include <iprt/pipe.h>
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
static DECLCALLBACK(int) dbgSymSrvOpen(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq, void **ppvHandle);
static DECLCALLBACK(int) dbgSymSrvRead(PRTHTTPCALLBACKDATA pData, void *pvHandle, void *pvBuf, size_t cbBuf, size_t *pcbRead);
static DECLCALLBACK(int) dbgSymSrvClose(PRTHTTPCALLBACKDATA pData, void *pvHandle);
static DECLCALLBACK(int) dbgSymSrvQueryInfo(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq, PRTFSOBJINFO pObjInfo, char **ppszMIMEHint);
static DECLCALLBACK(int) dbgSymSrvDestroy(PRTHTTPCALLBACKDATA pData);
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/** Flag whether the server was interrupted. */
static bool g_fCanceled = false;
/** The symbol cache absolute root. */
static const char *g_pszSymCacheRoot = NULL;
/** The path to the pdb.exe. */
static const char *g_pszPdbExe = NULL;
/** Symbol server to forward requests to if not found locally. */
static const char *g_pszSymSrvFwd = NULL;
#ifndef RT_OS_WINDOWS
/** The WINEPREFIX to use. */
static const char *g_pszWinePrefix = NULL;
/** The path to the wine binary to use for pdb.exe. */
static const char *g_pszWinePath = NULL;
#endif
/** Verbositity level. */
//static uint32_t g_iLogLevel = 99;
/** Server callbacks. */
static RTHTTPSERVERCALLBACKS g_SrvCallbacks =
{
dbgSymSrvOpen,
dbgSymSrvRead,
dbgSymSrvClose,
dbgSymSrvQueryInfo,
NULL,
NULL,
dbgSymSrvDestroy
};
/**
* Resolves (and validates) a given URL to an absolute (local) path.
*
* @returns VBox status code.
* @param pszUrl URL to resolve.
* @param ppszPathAbs Where to store the resolved absolute path on success.
* Needs to be free'd with RTStrFree().
* @param ppszPathAbsXml Where to store the resolved absolute path for the converted XML
* file. Needs to be free'd with RTStrFree().
*/
static int rtDbgSymSrvPathResolve(const char *pszUrl, char **ppszPathAbs, char **ppszPathAbsXml)
{
/* The URL needs to start with /download/symbols/. */
if (strncmp(pszUrl, "/download/symbols/", sizeof("/download/symbols/") - 1))
return VERR_NOT_FOUND;
pszUrl += sizeof("/download/symbols/") - 1;
/* Construct absolute path. */
char *pszPathAbs = NULL;
if (RTStrAPrintf(&pszPathAbs, "%s/%s", g_pszSymCacheRoot, pszUrl) <= 0)
return VERR_NO_MEMORY;
if (ppszPathAbsXml)
{
char *pszPathAbsXml = NULL;
if (RTStrAPrintf(&pszPathAbsXml, "%s/%s.xml", g_pszSymCacheRoot, pszUrl) <= 0)
return VERR_NO_MEMORY;
*ppszPathAbsXml = pszPathAbsXml;
}
*ppszPathAbs = pszPathAbs;
return VINF_SUCCESS;
}
static int rtDbgSymSrvFwdDownload(const char *pszUrl, char *pszPathAbs)
{
RTPrintf("'%s' not in local cache, fetching from '%s'\n", pszPathAbs, g_pszSymSrvFwd);
char *pszFilename = RTPathFilename(pszPathAbs);
char chStart = *pszFilename;
*pszFilename = '\0';
int rc = RTDirCreateFullPath(pszPathAbs, 0766);
if (!RTDirExists(pszPathAbs))
{
Log(("Error creating cache dir '%s': %Rrc\n", pszPathAbs, rc));
return rc;
}
*pszFilename = chStart;
char szUrl[_2K];
RTHTTP hHttp;
rc = RTHttpCreate(&hHttp);
if (RT_SUCCESS(rc))
{
RTHttpUseSystemProxySettings(hHttp);
RTHttpSetFollowRedirects(hHttp, 8);
static const char * const s_apszHeaders[] =
{
"User-Agent: Microsoft-Symbol-Server/6.6.0999.9",
"Pragma: no-cache",
};
rc = RTHttpSetHeaders(hHttp, RT_ELEMENTS(s_apszHeaders), s_apszHeaders);
if (RT_SUCCESS(rc))
{
RTStrPrintf(szUrl, sizeof(szUrl), "%s/%s", g_pszSymSrvFwd, pszUrl + sizeof("/download/symbols/") - 1);
/** @todo Use some temporary file name and rename it after the operation
* since not all systems support read-deny file sharing
* settings. */
RTPrintf("Downloading '%s' to '%s'...\n", szUrl, pszPathAbs);
rc = RTHttpGetFile(hHttp, szUrl, pszPathAbs);
if (RT_FAILURE(rc))
{
RTFileDelete(pszPathAbs);
RTPrintf("%Rrc on URL '%s'\n", rc, szUrl);
}
if (rc == VERR_HTTP_NOT_FOUND)
{
/* Try the compressed version of the file. */
pszPathAbs[strlen(pszPathAbs) - 1] = '_';
szUrl[strlen(szUrl) - 1] = '_';
RTPrintf("Downloading '%s' to '%s'...\n", szUrl, pszPathAbs);
rc = RTHttpGetFile(hHttp, szUrl, pszPathAbs);
#if 0 /** @todo */
if (RT_SUCCESS(rc))
rc = rtDbgCfgUnpackMsCacheFile(pThis, pszPathAbs, pszFilename);
else
#endif
{
RTPrintf("%Rrc on URL '%s'\n", rc, pszPathAbs);
RTFileDelete(pszPathAbs);
}
}
}
RTHttpDestroy(hHttp);
}
return rc;
}
static int rtDbgSymSrvConvertToGhidraXml(char *pszPath, const char *pszFilename)
{
RTPrintf("Converting '%s' to ghidra XML into '%s'\n", pszPath, pszFilename);
/*
* Figuring out the argument list for the platform specific way to call pdb.exe.
*/
#ifdef RT_OS_WINDOWS
RTENV hEnv = RTENV_DEFAULT;
RTPathChangeToDosSlashes(pszPath, false /*fForce*/);
const char *papszArgs[] =
{
g_pszPdbExe,
pszPath,
NULL
};
#else
const char *papszArgs[] =
{
g_pszWinePath,
g_pszPdbExe,
pszPath,
NULL
};
RTENV hEnv;
{
int rc = RTEnvCreate(&hEnv);
if (RT_SUCCESS(rc))
{
rc = RTEnvSetEx(hEnv, "WINEPREFIX", g_pszWinePrefix);
if (RT_SUCCESS(rc))
rc = RTEnvSetEx(hEnv, "WINEDEBUG", "-all");
if (RT_FAILURE(rc))
{
RTEnvDestroy(hEnv);
return rc;
}
}
}
#endif
RTPIPE hPipeR, hPipeW;
int rc = RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE);
if (RT_SUCCESS(rc))
{
RTHANDLE Handle;
Handle.enmType = RTHANDLETYPE_PIPE;
Handle.u.hPipe = hPipeW;
/*
* Do the conversion.
*/
RTPROCESS hChild;
RTFILE hFile;
rc = RTFileOpen(&hFile, pszFilename, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE); AssertRC(rc);
rc = RTProcCreateEx(papszArgs[0], papszArgs, hEnv,
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
RTPROC_FLAGS_NO_WINDOW | RTPROC_FLAGS_HIDDEN | RTPROC_FLAGS_SEARCH_PATH,
#else
RTPROC_FLAGS_SEARCH_PATH,
#endif
NULL /*phStdIn*/, &Handle, NULL /*phStdErr*/,
NULL /*pszAsUser*/, NULL /*pszPassword*/, NULL /*pvExtraData*/,
&hChild);
if (RT_SUCCESS(rc))
{
rc = RTPipeClose(hPipeW); AssertRC(rc);
for (;;)
{
char szOutput[_4K];
size_t cbRead;
rc = RTPipeReadBlocking(hPipeR, &szOutput[0], sizeof(szOutput), &cbRead);
if (RT_FAILURE(rc))
{
Assert(rc == VERR_BROKEN_PIPE);
break;
}
rc = RTFileWrite(hFile, &szOutput[0], cbRead, NULL /*pcbWritten*/); AssertRC(rc);
}
rc = RTPipeClose(hPipeR); AssertRC(rc);
RTPROCSTATUS ProcStatus;
rc = RTProcWait(hChild, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus);
if (RT_SUCCESS(rc))
{
if ( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
&& ProcStatus.iStatus == 0)
{
if (RTPathExists(pszPath))
{
RTPrintf("Successfully unpacked '%s' to '%s'.\n", pszPath, pszFilename);
rc = VINF_SUCCESS;
}
else
{
RTPrintf("Successfully ran unpacker on '%s', but '%s' is missing!\n", pszPath, pszFilename);
rc = VERR_FILE_NOT_FOUND;
}
}
else
{
RTPrintf("Unpacking '%s' failed: iStatus=%d enmReason=%d\n",
pszPath, ProcStatus.iStatus, ProcStatus.enmReason);
rc = VERR_ZIP_CORRUPTED;
}
}
else
RTPrintf("Error waiting for process: %Rrc\n", rc);
RTFileClose(hFile);
}
else
RTPrintf("Error starting unpack process '%s': %Rrc\n", papszArgs[0], rc);
}
#ifndef RT_OS_WINDOWS
RTEnvDestroy(hEnv);
#endif
return rc;
}
static DECLCALLBACK(int) dbgSymSrvOpen(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq, void **ppvHandle)
{
RT_NOREF(pData);
char *pszPathAbs = NULL;
char *pszPathAbsXml = NULL;
int rc = rtDbgSymSrvPathResolve(pReq->pszUrl, &pszPathAbs, &pszPathAbsXml);
if (RT_SUCCESS(rc))
{
RTFILE hFile;
if ( g_pszPdbExe
&& RTPathExists(pszPathAbsXml))
{
RTPrintf("Opening '%s'\n", pszPathAbsXml);
rc = RTFileOpen(&hFile, pszPathAbsXml, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
}
else
{
RTPrintf("Opening '%s'\n", pszPathAbs);
rc = RTFileOpen(&hFile, pszPathAbs, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
}
if (RT_SUCCESS(rc))
*ppvHandle = hFile;
RTStrFree(pszPathAbs);
RTStrFree(pszPathAbsXml);
}
LogFlowFuncLeaveRC(rc);
return rc;
}
static DECLCALLBACK(int) dbgSymSrvRead(PRTHTTPCALLBACKDATA pData, void *pvHandle, void *pvBuf, size_t cbBuf, size_t *pcbRead)
{
RT_NOREF(pData);
return RTFileRead((RTFILE)pvHandle, pvBuf, cbBuf, pcbRead);
}
static DECLCALLBACK(int) dbgSymSrvClose(PRTHTTPCALLBACKDATA pData, void *pvHandle)
{
RT_NOREF(pData);
return RTFileClose((RTFILE)pvHandle);
}
static DECLCALLBACK(int) dbgSymSrvQueryInfo(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq, PRTFSOBJINFO pObjInfo, char **ppszMIMEHint)
{
RT_NOREF(pData, ppszMIMEHint);
char *pszPathAbs = NULL;
char *pszPathAbsXml = NULL;
int rc = rtDbgSymSrvPathResolve(pReq->pszUrl, &pszPathAbs, &pszPathAbsXml);
if (RT_SUCCESS(rc))
{
if ( !RTPathExists(pszPathAbs)
&& g_pszSymSrvFwd)
rc = rtDbgSymSrvFwdDownload(pReq->pszUrl, pszPathAbs);
if ( RT_SUCCESS(rc)
&& RTPathExists(pszPathAbs))
{
const char *pszFile = pszPathAbs;
if (g_pszPdbExe)
{
if (!RTPathExists(pszPathAbsXml))
rc = rtDbgSymSrvConvertToGhidraXml(pszPathAbs, pszPathAbsXml);
if (RT_SUCCESS(rc))
pszFile = pszPathAbsXml;
}
if ( RT_SUCCESS(rc)
&& RTPathExists(pszFile))
{
rc = RTPathQueryInfo(pszFile, pObjInfo, RTFSOBJATTRADD_NOTHING);
if (RT_SUCCESS(rc))
{
if (!RTFS_IS_FILE(pObjInfo->Attr.fMode))
rc = VERR_NOT_SUPPORTED;
}
}
else
rc = VERR_FILE_NOT_FOUND;
}
else
rc = VERR_FILE_NOT_FOUND;
RTStrFree(pszPathAbs);
RTStrFree(pszPathAbsXml);
}
LogFlowFuncLeaveRC(rc);
return rc;
}
static DECLCALLBACK(int) dbgSymSrvDestroy(PRTHTTPCALLBACKDATA pData)
{
RTPrintf("%s\n", __FUNCTION__);
RT_NOREF(pData);
return VINF_SUCCESS;
}
/**
* Display the version of the server program.
*
* @returns exit code.
*/
static RTEXITCODE rtDbgSymSrvVersion(void)
{
RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
return RTEXITCODE_SUCCESS;
}
/**
* Shows the usage of the cache program.
*
* @returns Exit code.
* @param pszArg0 Program name.
*/
static RTEXITCODE rtDbgSymSrvUsage(const char *pszArg0)
{
RTPrintf("Usage: %s --address <interface> --port <port> --sym-cache <symbol cache root> --pdb-exe <ghidra pdb.exe path>\n"
"\n"
"Options:\n"
" -a, --address\n"
" The interface to listen on, default is localhost.\n"
" -p, --port\n"
" The port to listen on, default is 80.\n"
" -c, --sym-cache\n"
" The absolute path of the symbol cache.\n"
" -x, --pdb-exe\n"
" The path of Ghidra's pdb.exe to convert PDB files to XML on the fly.\n"
" -f, --sym-srv-forward\n"
" The symbol server to forward requests to if a file is not in the local cache\n"
#ifndef RT_OS_WINDOWS
" -w, --wine-prefix\n"
" The prefix of the wine environment to use which has msdia140.dll set up for pdb.exe.\n"
" -b, --wine-bin\n"
" The wine binary path to run pdb.exe with.\n"
#endif
, RTPathFilename(pszArg0));
return RTEXITCODE_SUCCESS;
}
int main(int argc, char **argv)
{
int rc = RTR3InitExe(argc, &argv, 0);
if (RT_FAILURE(rc))
return RTMsgInitFailure(rc);
/*
* Parse the command line.
*/
static RTGETOPTDEF const s_aOptions[] =
{
{ "--address", 'a', RTGETOPT_REQ_STRING },
{ "--port", 'p', RTGETOPT_REQ_UINT16 },
{ "--sym-cache", 'c', RTGETOPT_REQ_STRING },
{ "--pdb-exe", 'x', RTGETOPT_REQ_STRING },
{ "--sym-srv-forward", 'f', RTGETOPT_REQ_STRING },
#ifndef RT_OS_WINDOWS
{ "--wine-prefix", 'w', RTGETOPT_REQ_STRING },
{ "--wine-bin", 'b', RTGETOPT_REQ_STRING },
#endif
{ "--help", 'h', RTGETOPT_REQ_NOTHING },
{ "--version", 'V', RTGETOPT_REQ_NOTHING },
};
RTGETOPTSTATE State;
rc = RTGetOptInit(&State, argc, argv, &s_aOptions[0], RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
if (RT_FAILURE(rc))
return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTGetOptInit failed: %Rrc", rc);
const char *pszAddress = "localhost";
uint16_t uPort = 80;
RTGETOPTUNION ValueUnion;
int chOpt;
while ((chOpt = RTGetOpt(&State, &ValueUnion)) != 0)
{
switch (chOpt)
{
case 'a':
pszAddress = ValueUnion.psz;
break;
case 'p':
uPort = ValueUnion.u16;
break;
case 'c':
g_pszSymCacheRoot = ValueUnion.psz;
break;
case 'x':
g_pszPdbExe = ValueUnion.psz;
break;
case 'f':
g_pszSymSrvFwd = ValueUnion.psz;
break;
#ifndef RT_OS_WINDOWS
case 'w':
g_pszWinePrefix = ValueUnion.psz;
break;
case 'b':
g_pszWinePath = ValueUnion.psz;
break;
#endif
case 'h':
return rtDbgSymSrvUsage(argv[0]);
case 'V':
return rtDbgSymSrvVersion();
default:
return RTGetOptPrintError(chOpt, &ValueUnion);
}
}
if (!g_pszSymCacheRoot)
return RTMsgErrorExit(RTEXITCODE_FAILURE, "The symbol cache root needs to be set");
RTHTTPSERVER hHttpSrv;
rc = RTHttpServerCreate(&hHttpSrv, pszAddress, uPort, &g_SrvCallbacks,
NULL, 0);
if (RT_SUCCESS(rc))
{
RTPrintf("Starting HTTP server at %s:%RU16 ...\n", pszAddress, uPort);
RTPrintf("Root directory is '%s'\n", g_pszSymCacheRoot);
RTPrintf("Running HTTP server ...\n");
for (;;)
{
RTThreadSleep(1000);
if (g_fCanceled)
break;
}
RTPrintf("Stopping HTTP server ...\n");
int rc2 = RTHttpServerDestroy(hHttpSrv);
if (RT_SUCCESS(rc))
rc = rc2;
RTPrintf("Stopped HTTP server\n");
}
else
return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTHttpServerCreate failed: %Rrc", rc);
return RTEXITCODE_SUCCESS;
}
|