VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/dbg/dbgmodghidra.cpp@ 100931

Last change on this file since 100931 was 100931, checked in by vboxsync, 16 months ago

IPRT/dbg: Early PDB support.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.8 KB
Line 
1/* $Id: dbgmodghidra.cpp 100931 2023-08-21 23:11:01Z vboxsync $ */
2/** @file
3 * IPRT - Debug Info Reader for Ghidra XML files created with createPdbXmlFiles.bat/pdb.exe.
4 */
5
6/*
7 * Copyright (C) 2021-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/dbg.h>
42#include "internal/iprt.h"
43
44#include <iprt/err.h>
45#include <iprt/ctype.h>
46#include <iprt/mem.h>
47#include <iprt/sort.h>
48#include <iprt/stream.h>
49#include <iprt/string.h>
50#include <iprt/cpp/xml.h>
51#include "internal/dbgmod.h"
52
53
54/*********************************************************************************************************************************
55* Structures and Typedefs *
56*********************************************************************************************************************************/
57/**
58 * Temporary segment data.
59 */
60typedef struct RTDBGMODGHIDRASEG
61{
62 const char *pszNumber;
63 RTUINTPTR uRva;
64} RTDBGMODGHIDRASEG;
65typedef RTDBGMODGHIDRASEG *PRTDBGMODGHIDRASEG;
66typedef const RTDBGMODGHIDRASEG *PCRTDBGMODGHIDRASEG;
67
68
69/** @interface_method_impl{RTDBGMODVTDBG,pfnUnwindFrame} */
70static DECLCALLBACK(int) rtDbgModGhidra_UnwindFrame(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTDBGUNWINDSTATE pState)
71{
72 RT_NOREF(pMod, iSeg, off, pState);
73 return VERR_DBG_NO_UNWIND_INFO;
74}
75
76
77/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByAddr} */
78static DECLCALLBACK(int) rtDbgModGhidra_LineByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
79 PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
80{
81 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
82 return RTDbgModLineByAddr(hCnt, iSeg, off, poffDisp, pLineInfo);
83}
84
85
86/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByOrdinal} */
87static DECLCALLBACK(int) rtDbgModGhidra_LineByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
88{
89 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
90 return RTDbgModLineByOrdinal(hCnt, iOrdinal, pLineInfo);
91}
92
93
94/** @interface_method_impl{RTDBGMODVTDBG,pfnLineCount} */
95static DECLCALLBACK(uint32_t) rtDbgModGhidra_LineCount(PRTDBGMODINT pMod)
96{
97 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
98 return RTDbgModLineCount(hCnt);
99}
100
101
102/** @interface_method_impl{RTDBGMODVTDBG,pfnLineAdd} */
103static DECLCALLBACK(int) rtDbgModGhidra_LineAdd(PRTDBGMODINT pMod, const char *pszFile, size_t cchFile, uint32_t uLineNo,
104 uint32_t iSeg, RTUINTPTR off, uint32_t *piOrdinal)
105{
106 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
107 Assert(!pszFile[cchFile]); NOREF(cchFile);
108 return RTDbgModLineAdd(hCnt, pszFile, uLineNo, iSeg, off, piOrdinal);
109}
110
111
112/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByAddr} */
113static DECLCALLBACK(int) rtDbgModGhidra_SymbolByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
114 PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
115{
116 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
117 return RTDbgModSymbolByAddr(hCnt, iSeg, off, fFlags, poffDisp, pSymInfo);
118}
119
120
121/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByName} */
122static DECLCALLBACK(int) rtDbgModGhidra_SymbolByName(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
123 PRTDBGSYMBOL pSymInfo)
124{
125 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
126 Assert(!pszSymbol[cchSymbol]); NOREF(cchSymbol);
127 return RTDbgModSymbolByName(hCnt, pszSymbol, pSymInfo);
128}
129
130
131/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByOrdinal} */
132static DECLCALLBACK(int) rtDbgModGhidra_SymbolByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
133{
134 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
135 return RTDbgModSymbolByOrdinal(hCnt, iOrdinal, pSymInfo);
136}
137
138
139/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolCount} */
140static DECLCALLBACK(uint32_t) rtDbgModGhidra_SymbolCount(PRTDBGMODINT pMod)
141{
142 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
143 return RTDbgModSymbolCount(hCnt);
144}
145
146
147/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolAdd} */
148static DECLCALLBACK(int) rtDbgModGhidra_SymbolAdd(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
149 RTDBGSEGIDX iSeg, RTUINTPTR off, RTUINTPTR cb, uint32_t fFlags,
150 uint32_t *piOrdinal)
151{
152 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
153 Assert(!pszSymbol[cchSymbol]); NOREF(cchSymbol);
154 return RTDbgModSymbolAdd(hCnt, pszSymbol, iSeg, off, cb, fFlags, piOrdinal);
155}
156
157
158/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentByIndex} */
159static DECLCALLBACK(int) rtDbgModGhidra_SegmentByIndex(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
160{
161 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
162 return RTDbgModSegmentByIndex(hCnt, iSeg, pSegInfo);
163}
164
165
166/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentCount} */
167static DECLCALLBACK(RTDBGSEGIDX) rtDbgModGhidra_SegmentCount(PRTDBGMODINT pMod)
168{
169 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
170 return RTDbgModSegmentCount(hCnt);
171}
172
173
174/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentAdd} */
175static DECLCALLBACK(int) rtDbgModGhidra_SegmentAdd(PRTDBGMODINT pMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName,
176 size_t cchName, uint32_t fFlags, PRTDBGSEGIDX piSeg)
177{
178 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
179 Assert(!pszName[cchName]); NOREF(cchName);
180 return RTDbgModSegmentAdd(hCnt, uRva, cb, pszName, fFlags, piSeg);
181}
182
183
184/** @interface_method_impl{RTDBGMODVTDBG,pfnImageSize} */
185static DECLCALLBACK(RTUINTPTR) rtDbgModGhidra_ImageSize(PRTDBGMODINT pMod)
186{
187 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
188 return RTDbgModImageSize(hCnt);
189}
190
191
192/** @interface_method_impl{RTDBGMODVTDBG,pfnRvaToSegOff} */
193static DECLCALLBACK(RTDBGSEGIDX) rtDbgModGhidra_RvaToSegOff(PRTDBGMODINT pMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
194{
195 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
196 return RTDbgModRvaToSegOff(hCnt, uRva, poffSeg);
197}
198
199
200/** @interface_method_impl{RTDBGMODVTDBG,pfnClose} */
201static DECLCALLBACK(int) rtDbgModGhidra_Close(PRTDBGMODINT pMod)
202{
203 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
204 RTDbgModRelease(hCnt);
205 pMod->pvDbgPriv = NULL;
206 return VINF_SUCCESS;
207}
208
209
210/**
211 * Returns the table with the given name from the given table list.
212 *
213 * @returns Pointer to the XML element node containing the given table or NULL if not found.
214 * @param pelmTables Pointer to the node containing the tables.
215 * @param pszName The table name to look for.
216 */
217static const xml::ElementNode *rtDbgModGhidraGetTableByName(const xml::ElementNode *pelmTables, const char *pszName)
218{
219 xml::NodesLoop nl1(*pelmTables, "table");
220 const xml::ElementNode *pelmTbl;
221 while ((pelmTbl = nl1.forAllNodes()))
222 {
223 const char *pszTblName = NULL;
224
225 if ( pelmTbl->getAttributeValue("name", &pszTblName)
226 && !strcmp(pszTblName, pszName))
227 return pelmTbl;
228 }
229
230 return NULL;
231}
232
233
234/**
235 * Adds the symbols from the given \"Symbols\" table.
236 *
237 * @returns IPRT status code.
238 * @param hCnt Debug module container handle.
239 * @param elmTbl Reference to the XML node containing the symbols.
240 */
241static int rtDbgModGhidraXmlParseSymbols(RTDBGMOD hCnt, const xml::ElementNode &elmTbl)
242{
243 xml::NodesLoop nlSym(elmTbl, "symbol");
244 const xml::ElementNode *pelmSym;
245 while ((pelmSym = nlSym.forAllNodes()))
246 {
247 /* Only parse Function and PublicSymbol tags. */
248 const char *pszTag = NULL;
249 if ( pelmSym->getAttributeValue("tag", &pszTag)
250 && ( !strcmp(pszTag, "PublicSymbol")
251 || !strcmp(pszTag, "Function")))
252 {
253 const char *pszSymName = NULL;
254 if ( !pelmSym->getAttributeValue("undecorated", &pszSymName)
255 || *pszSymName == '\0')
256 pelmSym->getAttributeValue("name", &pszSymName);
257
258 if ( pszSymName
259 && strlen(pszSymName) < RTDBG_SYMBOL_NAME_LENGTH)
260 {
261 uint64_t u64Addr = 0;
262 uint64_t u64Length = 0;
263 if ( pelmSym->getAttributeValue("address", &u64Addr)
264 && pelmSym->getAttributeValue("length", &u64Length))
265 {
266 int rc = RTDbgModSymbolAdd(hCnt, pszSymName, RTDBGSEGIDX_RVA, u64Addr, u64Length, 0 /*fFlags*/, NULL);
267 if ( RT_FAILURE(rc)
268 && rc != VERR_DBG_DUPLICATE_SYMBOL
269 && rc != VERR_DBG_ADDRESS_CONFLICT
270 && rc != VERR_DBG_INVALID_RVA) /* (don't be too strict) */
271 return rc;
272 }
273 }
274 }
275 }
276
277 return VINF_SUCCESS;
278}
279
280
281/**
282 * Adds the symbols from the given \"functions\" table.
283 *
284 * @returns IPRT status code.
285 * @param hCnt Debug module container handle.
286 * @param elmTbl Reference to the XML node containing the symbols.
287 */
288static int rtDbgModGhidraXmlParseFunctions(RTDBGMOD hCnt, const xml::ElementNode &elmTbl)
289{
290 xml::NodesLoop nlFun(elmTbl, "function");
291 const xml::ElementNode *pelmFun;
292 while ((pelmFun = nlFun.forAllNodes()))
293 {
294 xml::NodesLoop nlLn(*pelmFun, "line_number");
295 const xml::ElementNode *pelmLn;
296 while ((pelmLn = nlLn.forAllNodes()))
297 {
298 const char *pszFile = NULL;
299 uint32_t uLineNo = 0;
300 uint64_t off = 0;
301 if ( pelmLn->getAttributeValue("source_file", &pszFile)
302 && pelmLn->getAttributeValue("start", &uLineNo)
303 && pelmLn->getAttributeValue("addr", &off))
304 {
305 int rc = RTDbgModLineAdd(hCnt, pszFile, uLineNo, RTDBGSEGIDX_RVA, off, NULL /*piOrdinal*/);
306 if ( RT_FAILURE(rc)
307 && rc != VERR_DBG_DUPLICATE_SYMBOL
308 && rc != VERR_DBG_ADDRESS_CONFLICT
309 && rc != VERR_DBG_INVALID_RVA) /* (don't be too strict) */
310 return rc;
311 }
312 }
313 }
314
315 return VINF_SUCCESS;
316}
317
318
319/**
320 * @copydoc FNRTSORTCMP
321 */
322static DECLCALLBACK(int) rtDbgModGhidraSegmentsSortCmp(void const *pvElement1, void const *pvElement2, void *pvUser)
323{
324 RT_NOREF(pvUser);
325 PCRTDBGMODGHIDRASEG pSeg1 = (PCRTDBGMODGHIDRASEG)pvElement1;
326 PCRTDBGMODGHIDRASEG pSeg2 = (PCRTDBGMODGHIDRASEG)pvElement2;
327
328 if (pSeg1->uRva > pSeg2->uRva)
329 return 1;
330 if (pSeg1->uRva < pSeg2->uRva)
331 return -1;
332
333 return 0;
334}
335
336
337/**
338 * Adds the segments in the given \"SegmentMap\" table.
339 *
340 * @returns IPRT status code.
341 * @param hCnt Debug module container handle.
342 * @param elmTblSeg Reference to the XML node containing the segments.
343 */
344static int rtDbgModGhidraSegmentsAdd(RTDBGMOD hCnt, const xml::ElementNode &elmTblSeg)
345{
346 RTDBGMODGHIDRASEG aSegments[32];
347 uint32_t idxSeg = 0;
348
349 xml::NodesLoop nl1(elmTblSeg, "segment");
350 const xml::ElementNode *pelmSeg;
351 while ( (pelmSeg = nl1.forAllNodes())
352 && idxSeg < RT_ELEMENTS(aSegments))
353 {
354 const char *pszNumber = NULL;
355 RTUINTPTR uRva = 0;
356
357 if ( pelmSeg->getAttributeValue("number", &pszNumber)
358 && pelmSeg->getAttributeValue("address", &uRva))
359 {
360 aSegments[idxSeg].pszNumber = pszNumber;
361 aSegments[idxSeg].uRva = uRva;
362 idxSeg++;
363 }
364 }
365
366 /* Sort the segments by RVA so it is possible to deduce segment sizes. */
367 RTSortShell(&aSegments[0], idxSeg, sizeof(aSegments[0]), rtDbgModGhidraSegmentsSortCmp, NULL);
368
369 for (uint32_t i = 0; i < idxSeg - 1; i++)
370 {
371 int rc = RTDbgModSegmentAdd(hCnt, aSegments[i].uRva, aSegments[i + 1].uRva - aSegments[i].uRva,
372 aSegments[i].pszNumber, 0 /*fFlags*/, NULL);
373 if (RT_FAILURE(rc))
374 return rc;
375 }
376
377 /* Last segment for which we assume a size of 0 right now. */
378 int rc = RTDbgModSegmentAdd(hCnt, aSegments[idxSeg - 1].uRva, 0,
379 aSegments[idxSeg - 1].pszNumber, 0 /*fFlags*/, NULL);
380 if (RT_FAILURE(rc))
381 return rc;
382
383 return rc;
384}
385
386
387/**
388 * Load the symbols from an XML document.
389 *
390 * @returns IPRT status code.
391 * @param hCnt Debug module container handle.
392 * @param a_pDoc Pointer to the XML document.
393 */
394static int rtDbgModGhidraXmlParse(RTDBGMOD hCnt, xml::Document *a_pDoc)
395{
396 /*
397 * Get the root element and check whether it looks like a valid Ghidra XML.
398 */
399 const xml::ElementNode *pelmRoot = a_pDoc->getRootElement();
400 if ( !pelmRoot
401 || strcmp(pelmRoot->getName(), "pdb") != 0)
402 return VERR_DBG_NO_MATCHING_INTERPRETER;
403
404 const xml::ElementNode *pelmTables = pelmRoot->findChildElement("tables");
405 if (!pelmTables)
406 return VERR_DBG_NO_MATCHING_INTERPRETER;
407
408 const xml::ElementNode *pelmTbl = rtDbgModGhidraGetTableByName(pelmTables, "SegmentMap");
409 if (pelmTbl)
410 {
411 int rc = rtDbgModGhidraSegmentsAdd(hCnt, *pelmTbl);
412 if (RT_SUCCESS(rc))
413 {
414 pelmTbl = rtDbgModGhidraGetTableByName(pelmTables, "Symbols");
415 if (pelmTbl)
416 {
417 rc = rtDbgModGhidraXmlParseSymbols(hCnt, *pelmTbl);
418 if (RT_SUCCESS(rc))
419 {
420 pelmTbl = pelmRoot->findChildElement("functions"); /* Might not be there. */
421 if (pelmTbl)
422 rc = rtDbgModGhidraXmlParseFunctions(hCnt, *pelmTbl);
423 return rc;
424 }
425 }
426 }
427 }
428
429 return VERR_DBG_NO_MATCHING_INTERPRETER;
430}
431
432
433/** @interface_method_impl{RTDBGMODVTDBG,pfnTryOpen} */
434static DECLCALLBACK(int) rtDbgModGhidra_TryOpen(PRTDBGMODINT pMod, RTLDRARCH enmArch, RTDBGCFG hDbgCfg)
435{
436 RT_NOREF(enmArch); RT_NOREF_PV(hDbgCfg);
437
438 /*
439 * Fend off images.
440 */
441 if (!pMod->pszDbgFile)
442 return VERR_DBG_NO_MATCHING_INTERPRETER;
443 pMod->pvDbgPriv = NULL;
444
445 /*
446 * Try open the file and create an instance.
447 */
448 xml::Document Doc;
449 {
450 xml::XmlFileParser Parser;
451 try
452 {
453 Parser.read(pMod->pszDbgFile, Doc);
454 }
455 catch (xml::XmlError &rErr)
456 {
457 RT_NOREF(rErr);
458 return VERR_DBG_NO_MATCHING_INTERPRETER;
459 }
460 catch (xml::EIPRTFailure &rErr)
461 {
462 return rErr.rc();
463 }
464 }
465
466 RTDBGMOD hCnt;
467 int rc = RTDbgModCreate(&hCnt, pMod->pszName, 0 /*cbSeg*/, 0 /*fFlags*/);
468 if (RT_SUCCESS(rc))
469 {
470 /*
471 * Hand the xml doc over to the common code.
472 */
473 try
474 {
475 rc = rtDbgModGhidraXmlParse(hCnt, &Doc);
476 if (RT_SUCCESS(rc))
477 {
478 pMod->pvDbgPriv = hCnt;
479 return VINF_SUCCESS;
480 }
481 }
482 catch (RTCError &rXcpt) // includes all XML exceptions
483 {
484 RT_NOREF(rXcpt);
485 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
486 }
487 RTDbgModRelease(hCnt);
488 }
489
490 return rc;
491}
492
493
494
495/** Virtual function table for the Ghidra XML file reader. */
496DECL_HIDDEN_CONST(RTDBGMODVTDBG) const g_rtDbgModVtDbgGhidra =
497{
498 /*.u32Magic = */ RTDBGMODVTDBG_MAGIC,
499 /*.fSupports = */ RT_DBGTYPE_OTHER | RT_DBGTYPE_MAP,
500 /*.pszName = */ "ghidra",
501 /*.pfnTryOpen = */ rtDbgModGhidra_TryOpen,
502 /*.pfnClose = */ rtDbgModGhidra_Close,
503
504 /*.pfnRvaToSegOff = */ rtDbgModGhidra_RvaToSegOff,
505 /*.pfnImageSize = */ rtDbgModGhidra_ImageSize,
506
507 /*.pfnSegmentAdd = */ rtDbgModGhidra_SegmentAdd,
508 /*.pfnSegmentCount = */ rtDbgModGhidra_SegmentCount,
509 /*.pfnSegmentByIndex = */ rtDbgModGhidra_SegmentByIndex,
510
511 /*.pfnSymbolAdd = */ rtDbgModGhidra_SymbolAdd,
512 /*.pfnSymbolCount = */ rtDbgModGhidra_SymbolCount,
513 /*.pfnSymbolByOrdinal = */ rtDbgModGhidra_SymbolByOrdinal,
514 /*.pfnSymbolByName = */ rtDbgModGhidra_SymbolByName,
515 /*.pfnSymbolByAddr = */ rtDbgModGhidra_SymbolByAddr,
516
517 /*.pfnLineAdd = */ rtDbgModGhidra_LineAdd,
518 /*.pfnLineCount = */ rtDbgModGhidra_LineCount,
519 /*.pfnLineByOrdinal = */ rtDbgModGhidra_LineByOrdinal,
520 /*.pfnLineByAddr = */ rtDbgModGhidra_LineByAddr,
521
522 /*.pfnUnwindFrame = */ rtDbgModGhidra_UnwindFrame,
523
524 /*.u32EndMagic = */ RTDBGMODVTDBG_MAGIC
525};
526
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette