VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/dbg/dbgmod.cpp@ 76452

Last change on this file since 76452 was 75237, checked in by vboxsync, 6 years ago

IPRT: Added support for reading MAPSYM output.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 71.5 KB
Line 
1/* $Id: dbgmod.cpp 75237 2018-11-02 21:12:17Z vboxsync $ */
2/** @file
3 * IPRT - Debug Module Interpreter.
4 */
5
6/*
7 * Copyright (C) 2009-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_DBG
32#include <iprt/dbg.h>
33#include "internal/iprt.h"
34
35#include <iprt/alloca.h>
36#include <iprt/asm.h>
37#include <iprt/assert.h>
38#include <iprt/avl.h>
39#include <iprt/err.h>
40#include <iprt/initterm.h>
41#include <iprt/log.h>
42#include <iprt/mem.h>
43#include <iprt/once.h>
44#include <iprt/param.h>
45#include <iprt/path.h>
46#include <iprt/semaphore.h>
47#include <iprt/strcache.h>
48#include <iprt/string.h>
49#include <iprt/uuid.h>
50#include "internal/dbgmod.h"
51#include "internal/magics.h"
52
53
54/*********************************************************************************************************************************
55* Structures and Typedefs *
56*********************************************************************************************************************************/
57/** Debug info interpreter registration record. */
58typedef struct RTDBGMODREGDBG
59{
60 /** Pointer to the next record. */
61 struct RTDBGMODREGDBG *pNext;
62 /** Pointer to the virtual function table for the interpreter. */
63 PCRTDBGMODVTDBG pVt;
64 /** Usage counter. */
65 uint32_t volatile cUsers;
66} RTDBGMODREGDBG;
67typedef RTDBGMODREGDBG *PRTDBGMODREGDBG;
68
69/** Image interpreter registration record. */
70typedef struct RTDBGMODREGIMG
71{
72 /** Pointer to the next record. */
73 struct RTDBGMODREGIMG *pNext;
74 /** Pointer to the virtual function table for the interpreter. */
75 PCRTDBGMODVTIMG pVt;
76 /** Usage counter. */
77 uint32_t volatile cUsers;
78} RTDBGMODREGIMG;
79typedef RTDBGMODREGIMG *PRTDBGMODREGIMG;
80
81
82/*********************************************************************************************************************************
83* Defined Constants And Macros *
84*********************************************************************************************************************************/
85/** Validates a debug module handle and returns rc if not valid. */
86#define RTDBGMOD_VALID_RETURN_RC(pDbgMod, rc) \
87 do { \
88 AssertPtrReturn((pDbgMod), (rc)); \
89 AssertReturn((pDbgMod)->u32Magic == RTDBGMOD_MAGIC, (rc)); \
90 AssertReturn((pDbgMod)->cRefs > 0, (rc)); \
91 } while (0)
92
93/** Locks the debug module. */
94#define RTDBGMOD_LOCK(pDbgMod) \
95 do { \
96 int rcLock = RTCritSectEnter(&(pDbgMod)->CritSect); \
97 AssertRC(rcLock); \
98 } while (0)
99
100/** Unlocks the debug module. */
101#define RTDBGMOD_UNLOCK(pDbgMod) \
102 do { \
103 int rcLock = RTCritSectLeave(&(pDbgMod)->CritSect); \
104 AssertRC(rcLock); \
105 } while (0)
106
107
108/*********************************************************************************************************************************
109* Global Variables *
110*********************************************************************************************************************************/
111/** Init once object for lazy registration of the built-in image and debug
112 * info interpreters. */
113static RTONCE g_rtDbgModOnce = RTONCE_INITIALIZER;
114/** Read/Write semaphore protecting the list of registered interpreters. */
115static RTSEMRW g_hDbgModRWSem = NIL_RTSEMRW;
116/** List of registered image interpreters. */
117static PRTDBGMODREGIMG g_pImgHead;
118/** List of registered debug infor interpreters. */
119static PRTDBGMODREGDBG g_pDbgHead;
120/** String cache for the debug info interpreters.
121 * RTSTRCACHE is thread safe. */
122DECLHIDDEN(RTSTRCACHE) g_hDbgModStrCache = NIL_RTSTRCACHE;
123
124
125
126
127
128/**
129 * Cleanup debug info interpreter globals.
130 *
131 * @param enmReason The cause of the termination.
132 * @param iStatus The meaning of this depends on enmReason.
133 * @param pvUser User argument, unused.
134 */
135static DECLCALLBACK(void) rtDbgModTermCallback(RTTERMREASON enmReason, int32_t iStatus, void *pvUser)
136{
137 NOREF(iStatus); NOREF(pvUser);
138 if (enmReason == RTTERMREASON_UNLOAD)
139 {
140 RTSemRWDestroy(g_hDbgModRWSem);
141 g_hDbgModRWSem = NIL_RTSEMRW;
142
143 RTStrCacheDestroy(g_hDbgModStrCache);
144 g_hDbgModStrCache = NIL_RTSTRCACHE;
145
146 PRTDBGMODREGDBG pDbg = g_pDbgHead;
147 g_pDbgHead = NULL;
148 while (pDbg)
149 {
150 PRTDBGMODREGDBG pNext = pDbg->pNext;
151 AssertMsg(pDbg->cUsers == 0, ("%#x %s\n", pDbg->cUsers, pDbg->pVt->pszName));
152 RTMemFree(pDbg);
153 pDbg = pNext;
154 }
155
156 PRTDBGMODREGIMG pImg = g_pImgHead;
157 g_pImgHead = NULL;
158 while (pImg)
159 {
160 PRTDBGMODREGIMG pNext = pImg->pNext;
161 AssertMsg(pImg->cUsers == 0, ("%#x %s\n", pImg->cUsers, pImg->pVt->pszName));
162 RTMemFree(pImg);
163 pImg = pNext;
164 }
165 }
166}
167
168
169/**
170 * Internal worker for register a debug interpreter.
171 *
172 * Called while owning the write lock or when locking isn't required.
173 *
174 * @returns IPRT status code.
175 * @retval VERR_NO_MEMORY
176 * @retval VERR_ALREADY_EXISTS
177 *
178 * @param pVt The virtual function table of the debug
179 * module interpreter.
180 */
181static int rtDbgModDebugInterpreterRegister(PCRTDBGMODVTDBG pVt)
182{
183 /*
184 * Search or duplicate registration.
185 */
186 PRTDBGMODREGDBG pPrev = NULL;
187 for (PRTDBGMODREGDBG pCur = g_pDbgHead; pCur; pCur = pCur->pNext)
188 {
189 if (pCur->pVt == pVt)
190 return VERR_ALREADY_EXISTS;
191 if (!strcmp(pCur->pVt->pszName, pVt->pszName))
192 return VERR_ALREADY_EXISTS;
193 pPrev = pCur;
194 }
195
196 /*
197 * Create a new record and add it to the end of the list.
198 */
199 PRTDBGMODREGDBG pReg = (PRTDBGMODREGDBG)RTMemAlloc(sizeof(*pReg));
200 if (!pReg)
201 return VERR_NO_MEMORY;
202 pReg->pVt = pVt;
203 pReg->cUsers = 0;
204 pReg->pNext = NULL;
205 if (pPrev)
206 pPrev->pNext = pReg;
207 else
208 g_pDbgHead = pReg;
209 return VINF_SUCCESS;
210}
211
212
213/**
214 * Internal worker for register a image interpreter.
215 *
216 * Called while owning the write lock or when locking isn't required.
217 *
218 * @returns IPRT status code.
219 * @retval VERR_NO_MEMORY
220 * @retval VERR_ALREADY_EXISTS
221 *
222 * @param pVt The virtual function table of the image
223 * interpreter.
224 */
225static int rtDbgModImageInterpreterRegister(PCRTDBGMODVTIMG pVt)
226{
227 /*
228 * Search or duplicate registration.
229 */
230 PRTDBGMODREGIMG pPrev = NULL;
231 for (PRTDBGMODREGIMG pCur = g_pImgHead; pCur; pCur = pCur->pNext)
232 {
233 if (pCur->pVt == pVt)
234 return VERR_ALREADY_EXISTS;
235 if (!strcmp(pCur->pVt->pszName, pVt->pszName))
236 return VERR_ALREADY_EXISTS;
237 pPrev = pCur;
238 }
239
240 /*
241 * Create a new record and add it to the end of the list.
242 */
243 PRTDBGMODREGIMG pReg = (PRTDBGMODREGIMG)RTMemAlloc(sizeof(*pReg));
244 if (!pReg)
245 return VERR_NO_MEMORY;
246 pReg->pVt = pVt;
247 pReg->cUsers = 0;
248 pReg->pNext = NULL;
249 if (pPrev)
250 pPrev->pNext = pReg;
251 else
252 g_pImgHead = pReg;
253 return VINF_SUCCESS;
254}
255
256
257/**
258 * Do-once callback that initializes the read/write semaphore and registers
259 * the built-in interpreters.
260 *
261 * @returns IPRT status code.
262 * @param pvUser NULL.
263 */
264static DECLCALLBACK(int) rtDbgModInitOnce(void *pvUser)
265{
266 NOREF(pvUser);
267
268 /*
269 * Create the semaphore and string cache.
270 */
271 int rc = RTSemRWCreate(&g_hDbgModRWSem);
272 AssertRCReturn(rc, rc);
273
274 rc = RTStrCacheCreate(&g_hDbgModStrCache, "RTDBGMOD");
275 if (RT_SUCCESS(rc))
276 {
277 /*
278 * Register the interpreters.
279 */
280 rc = rtDbgModDebugInterpreterRegister(&g_rtDbgModVtDbgNm);
281 if (RT_SUCCESS(rc))
282 rc = rtDbgModDebugInterpreterRegister(&g_rtDbgModVtDbgMapSym);
283 if (RT_SUCCESS(rc))
284 rc = rtDbgModDebugInterpreterRegister(&g_rtDbgModVtDbgDwarf);
285 if (RT_SUCCESS(rc))
286 rc = rtDbgModDebugInterpreterRegister(&g_rtDbgModVtDbgCodeView);
287#ifdef RT_OS_WINDOWS
288 if (RT_SUCCESS(rc))
289 rc = rtDbgModDebugInterpreterRegister(&g_rtDbgModVtDbgDbgHelp);
290#endif
291 if (RT_SUCCESS(rc))
292 rc = rtDbgModImageInterpreterRegister(&g_rtDbgModVtImgLdr);
293 if (RT_SUCCESS(rc))
294 {
295 /*
296 * Finally, register the IPRT cleanup callback.
297 */
298 rc = RTTermRegisterCallback(rtDbgModTermCallback, NULL);
299 if (RT_SUCCESS(rc))
300 return VINF_SUCCESS;
301
302 /* bail out: use the termination callback. */
303 }
304 }
305 else
306 g_hDbgModStrCache = NIL_RTSTRCACHE;
307 rtDbgModTermCallback(RTTERMREASON_UNLOAD, 0, NULL);
308 return rc;
309}
310
311
312/**
313 * Performs lazy init of our global variables.
314 * @returns IPRT status code.
315 */
316DECLINLINE(int) rtDbgModLazyInit(void)
317{
318 return RTOnce(&g_rtDbgModOnce, rtDbgModInitOnce, NULL);
319}
320
321
322RTDECL(int) RTDbgModCreate(PRTDBGMOD phDbgMod, const char *pszName, RTUINTPTR cbSeg, uint32_t fFlags)
323{
324 /*
325 * Input validation and lazy initialization.
326 */
327 AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
328 *phDbgMod = NIL_RTDBGMOD;
329 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
330 AssertReturn(*pszName, VERR_INVALID_PARAMETER);
331 AssertReturn(fFlags == 0 || fFlags == RTDBGMOD_F_NOT_DEFERRED, VERR_INVALID_PARAMETER);
332
333 int rc = rtDbgModLazyInit();
334 if (RT_FAILURE(rc))
335 return rc;
336
337 /*
338 * Allocate a new module instance.
339 */
340 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
341 if (!pDbgMod)
342 return VERR_NO_MEMORY;
343 pDbgMod->u32Magic = RTDBGMOD_MAGIC;
344 pDbgMod->cRefs = 1;
345 rc = RTCritSectInit(&pDbgMod->CritSect);
346 if (RT_SUCCESS(rc))
347 {
348 pDbgMod->pszImgFileSpecified = RTStrCacheEnter(g_hDbgModStrCache, pszName);
349 pDbgMod->pszName = RTStrCacheEnterLower(g_hDbgModStrCache, RTPathFilenameEx(pszName, RTPATH_STR_F_STYLE_DOS));
350 if (pDbgMod->pszName)
351 {
352 rc = rtDbgModContainerCreate(pDbgMod, cbSeg);
353 if (RT_SUCCESS(rc))
354 {
355 *phDbgMod = pDbgMod;
356 return rc;
357 }
358 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
359 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
360 }
361 RTCritSectDelete(&pDbgMod->CritSect);
362 }
363
364 RTMemFree(pDbgMod);
365 return rc;
366}
367RT_EXPORT_SYMBOL(RTDbgModCreate);
368
369
370RTDECL(int) RTDbgModCreateFromMap(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName,
371 RTUINTPTR uSubtrahend, RTDBGCFG hDbgCfg)
372{
373 RT_NOREF_PV(hDbgCfg);
374
375 /*
376 * Input validation and lazy initialization.
377 */
378 AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
379 *phDbgMod = NIL_RTDBGMOD;
380 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
381 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
382 AssertPtrNullReturn(pszName, VERR_INVALID_POINTER);
383 AssertReturn(uSubtrahend == 0, VERR_NOT_IMPLEMENTED); /** @todo implement uSubtrahend. */
384
385 int rc = rtDbgModLazyInit();
386 if (RT_FAILURE(rc))
387 return rc;
388
389 if (!pszName)
390 pszName = RTPathFilenameEx(pszFilename, RTPATH_STR_F_STYLE_DOS);
391
392 /*
393 * Allocate a new module instance.
394 */
395 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
396 if (!pDbgMod)
397 return VERR_NO_MEMORY;
398 pDbgMod->u32Magic = RTDBGMOD_MAGIC;
399 pDbgMod->cRefs = 1;
400 rc = RTCritSectInit(&pDbgMod->CritSect);
401 if (RT_SUCCESS(rc))
402 {
403 pDbgMod->pszName = RTStrCacheEnterLower(g_hDbgModStrCache, pszName);
404 if (pDbgMod->pszName)
405 {
406 pDbgMod->pszDbgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
407 if (pDbgMod->pszDbgFile)
408 {
409 /*
410 * Try the map file readers.
411 */
412 rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
413 if (RT_SUCCESS(rc))
414 {
415 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
416 for (PRTDBGMODREGDBG pCur = g_pDbgHead; pCur; pCur = pCur->pNext)
417 {
418 if (pCur->pVt->fSupports & RT_DBGTYPE_MAP)
419 {
420 pDbgMod->pDbgVt = pCur->pVt;
421 pDbgMod->pvDbgPriv = NULL;
422 rc = pCur->pVt->pfnTryOpen(pDbgMod, RTLDRARCH_WHATEVER);
423 if (RT_SUCCESS(rc))
424 {
425 ASMAtomicIncU32(&pCur->cUsers);
426 RTSemRWReleaseRead(g_hDbgModRWSem);
427
428 *phDbgMod = pDbgMod;
429 return rc;
430 }
431 }
432 }
433
434 /* bail out */
435 RTSemRWReleaseRead(g_hDbgModRWSem);
436 }
437 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
438 }
439 else
440 rc = VERR_NO_STR_MEMORY;
441 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszDbgFile);
442 }
443 else
444 rc = VERR_NO_STR_MEMORY;
445 RTCritSectDelete(&pDbgMod->CritSect);
446 }
447
448 RTMemFree(pDbgMod);
449 return rc;
450}
451RT_EXPORT_SYMBOL(RTDbgModCreateFromMap);
452
453
454
455/*
456 *
457 * E x e c u t a b l e I m a g e F i l e s
458 * E x e c u t a b l e I m a g e F i l e s
459 * E x e c u t a b l e I m a g e F i l e s
460 *
461 */
462
463
464/**
465 * Opens debug information for an image.
466 *
467 * @returns IPRT status code
468 * @param pDbgMod The debug module structure.
469 *
470 * @note This will generally not look for debug info stored in external
471 * files. rtDbgModFromPeImageExtDbgInfoCallback can help with that.
472 */
473static int rtDbgModOpenDebugInfoInsideImage(PRTDBGMODINT pDbgMod)
474{
475 AssertReturn(!pDbgMod->pDbgVt, VERR_DBG_MOD_IPE);
476 AssertReturn(pDbgMod->pImgVt, VERR_DBG_MOD_IPE);
477
478 int rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
479 if (RT_SUCCESS(rc))
480 {
481 for (PRTDBGMODREGDBG pDbg = g_pDbgHead; pDbg; pDbg = pDbg->pNext)
482 {
483 pDbgMod->pDbgVt = pDbg->pVt;
484 pDbgMod->pvDbgPriv = NULL;
485 rc = pDbg->pVt->pfnTryOpen(pDbgMod, pDbgMod->pImgVt->pfnGetArch(pDbgMod));
486 if (RT_SUCCESS(rc))
487 {
488 /*
489 * That's it!
490 */
491 ASMAtomicIncU32(&pDbg->cUsers);
492 RTSemRWReleaseRead(g_hDbgModRWSem);
493 return VINF_SUCCESS;
494 }
495
496 pDbgMod->pDbgVt = NULL;
497 Assert(pDbgMod->pvDbgPriv == NULL);
498 }
499 RTSemRWReleaseRead(g_hDbgModRWSem);
500 }
501
502 return VERR_DBG_NO_MATCHING_INTERPRETER;
503}
504
505
506/** @callback_method_impl{FNRTDBGCFGOPEN} */
507static DECLCALLBACK(int) rtDbgModExtDbgInfoOpenCallback(RTDBGCFG hDbgCfg, const char *pszFilename, void *pvUser1, void *pvUser2)
508{
509 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)pvUser1;
510 PCRTLDRDBGINFO pDbgInfo = (PCRTLDRDBGINFO)pvUser2;
511 RT_NOREF_PV(pDbgInfo); /** @todo consider a more direct search for a interpreter. */
512 RT_NOREF_PV(hDbgCfg);
513
514 Assert(!pDbgMod->pDbgVt);
515 Assert(!pDbgMod->pvDbgPriv);
516 Assert(!pDbgMod->pszDbgFile);
517 Assert(pDbgMod->pImgVt);
518
519 /*
520 * Set the debug file name and try possible interpreters.
521 */
522 pDbgMod->pszDbgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
523
524 int rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
525 if (RT_SUCCESS(rc))
526 {
527 for (PRTDBGMODREGDBG pDbg = g_pDbgHead; pDbg; pDbg = pDbg->pNext)
528 {
529 pDbgMod->pDbgVt = pDbg->pVt;
530 pDbgMod->pvDbgPriv = NULL;
531 rc = pDbg->pVt->pfnTryOpen(pDbgMod, pDbgMod->pImgVt->pfnGetArch(pDbgMod));
532 if (RT_SUCCESS(rc))
533 {
534 /*
535 * Got it!
536 */
537 ASMAtomicIncU32(&pDbg->cUsers);
538 RTSemRWReleaseRead(g_hDbgModRWSem);
539 return VINF_CALLBACK_RETURN;
540 }
541
542 pDbgMod->pDbgVt = NULL;
543 Assert(pDbgMod->pvDbgPriv == NULL);
544 }
545 RTSemRWReleaseRead(g_hDbgModRWSem);
546 }
547
548 /* No joy. */
549 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszDbgFile);
550 pDbgMod->pszDbgFile = NULL;
551 return rc;
552}
553
554
555/**
556 * Argument package used by rtDbgModOpenDebugInfoExternalToImage.
557 */
558typedef struct RTDBGMODOPENDIETI
559{
560 PRTDBGMODINT pDbgMod;
561 RTDBGCFG hDbgCfg;
562} RTDBGMODOPENDIETI;
563
564
565/** @callback_method_impl{FNRTLDRENUMDBG} */
566static DECLCALLBACK(int)
567rtDbgModOpenDebugInfoExternalToImageCallback(RTLDRMOD hLdrMod, PCRTLDRDBGINFO pDbgInfo, void *pvUser)
568{
569 RTDBGMODOPENDIETI *pArgs = (RTDBGMODOPENDIETI *)pvUser;
570 RT_NOREF_PV(hLdrMod);
571
572 Assert(pDbgInfo->enmType > RTLDRDBGINFOTYPE_INVALID && pDbgInfo->enmType < RTLDRDBGINFOTYPE_END);
573 const char *pszExtFile = pDbgInfo->pszExtFile;
574 if (!pszExtFile)
575 {
576 /*
577 * If a external debug type comes without a file name, calculate a
578 * likely debug filename for it. (Hack for NT4 drivers.)
579 */
580 const char *pszExt = NULL;
581 if (pDbgInfo->enmType == RTLDRDBGINFOTYPE_CODEVIEW_DBG)
582 pszExt = ".dbg";
583 else if ( pDbgInfo->enmType == RTLDRDBGINFOTYPE_CODEVIEW_PDB20
584 || pDbgInfo->enmType == RTLDRDBGINFOTYPE_CODEVIEW_PDB70)
585 pszExt = ".pdb";
586 if (pszExt && pArgs->pDbgMod->pszName)
587 {
588 size_t cchName = strlen(pArgs->pDbgMod->pszName);
589 char *psz = (char *)alloca(cchName + strlen(pszExt) + 1);
590 if (psz)
591 {
592 memcpy(psz, pArgs->pDbgMod->pszName, cchName + 1);
593 RTPathStripSuffix(psz);
594 pszExtFile = strcat(psz, pszExt);
595 }
596 }
597
598 if (!pszExtFile)
599 {
600 Log2(("rtDbgModOpenDebugInfoExternalToImageCallback: enmType=%d\n", pDbgInfo->enmType));
601 return VINF_SUCCESS;
602 }
603 }
604
605 /*
606 * Switch on type and call the appropriate search function.
607 */
608 int rc;
609 switch (pDbgInfo->enmType)
610 {
611 case RTLDRDBGINFOTYPE_CODEVIEW_PDB70:
612 rc = RTDbgCfgOpenPdb70(pArgs->hDbgCfg, pszExtFile,
613 &pDbgInfo->u.Pdb70.Uuid,
614 pDbgInfo->u.Pdb70.uAge,
615 rtDbgModExtDbgInfoOpenCallback, pArgs->pDbgMod, (void *)pDbgInfo);
616 break;
617
618 case RTLDRDBGINFOTYPE_CODEVIEW_PDB20:
619 rc = RTDbgCfgOpenPdb20(pArgs->hDbgCfg, pszExtFile,
620 pDbgInfo->u.Pdb20.cbImage,
621 pDbgInfo->u.Pdb20.uTimestamp,
622 pDbgInfo->u.Pdb20.uAge,
623 rtDbgModExtDbgInfoOpenCallback, pArgs->pDbgMod, (void *)pDbgInfo);
624 break;
625
626 case RTLDRDBGINFOTYPE_CODEVIEW_DBG:
627 rc = RTDbgCfgOpenDbg(pArgs->hDbgCfg, pszExtFile,
628 pDbgInfo->u.Dbg.cbImage,
629 pDbgInfo->u.Dbg.uTimestamp,
630 rtDbgModExtDbgInfoOpenCallback, pArgs->pDbgMod, (void *)pDbgInfo);
631 break;
632
633 case RTLDRDBGINFOTYPE_DWARF_DWO:
634 rc = RTDbgCfgOpenDwo(pArgs->hDbgCfg, pszExtFile,
635 pDbgInfo->u.Dwo.uCrc32,
636 rtDbgModExtDbgInfoOpenCallback, pArgs->pDbgMod, (void *)pDbgInfo);
637 break;
638
639 default:
640 Log(("rtDbgModOpenDebugInfoExternalToImageCallback: Don't know how to handle enmType=%d and pszFileExt=%s\n",
641 pDbgInfo->enmType, pszExtFile));
642 return VERR_DBG_TODO;
643 }
644 if (RT_SUCCESS(rc))
645 {
646 LogFlow(("RTDbgMod: Successfully opened external debug info '%s' for '%s'\n",
647 pArgs->pDbgMod->pszDbgFile, pArgs->pDbgMod->pszImgFile));
648 return VINF_CALLBACK_RETURN;
649 }
650 Log(("rtDbgModOpenDebugInfoExternalToImageCallback: '%s' (enmType=%d) for '%s' -> %Rrc\n",
651 pszExtFile, pDbgInfo->enmType, pArgs->pDbgMod->pszImgFile, rc));
652 return rc;
653}
654
655
656/**
657 * Opens debug info listed in the image that is stored in a separate file.
658 *
659 * @returns IPRT status code
660 * @param pDbgMod The debug module.
661 * @param hDbgCfg The debug config. Can be NIL.
662 */
663static int rtDbgModOpenDebugInfoExternalToImage(PRTDBGMODINT pDbgMod, RTDBGCFG hDbgCfg)
664{
665 Assert(!pDbgMod->pDbgVt);
666
667 RTDBGMODOPENDIETI Args;
668 Args.pDbgMod = pDbgMod;
669 Args.hDbgCfg = hDbgCfg;
670 int rc = pDbgMod->pImgVt->pfnEnumDbgInfo(pDbgMod, rtDbgModOpenDebugInfoExternalToImageCallback, &Args);
671 if (RT_SUCCESS(rc) && pDbgMod->pDbgVt)
672 return VINF_SUCCESS;
673
674 LogFlow(("rtDbgModOpenDebugInfoExternalToImage: rc=%Rrc\n", rc));
675 return VERR_NOT_FOUND;
676}
677
678
679/** @callback_method_impl{FNRTDBGCFGOPEN} */
680static DECLCALLBACK(int) rtDbgModExtDbgInfoOpenCallback2(RTDBGCFG hDbgCfg, const char *pszFilename, void *pvUser1, void *pvUser2)
681{
682 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)pvUser1;
683 RT_NOREF_PV(pvUser2); /** @todo image matching string or smth. */
684 RT_NOREF_PV(hDbgCfg);
685
686 Assert(!pDbgMod->pDbgVt);
687 Assert(!pDbgMod->pvDbgPriv);
688 Assert(!pDbgMod->pszDbgFile);
689 Assert(pDbgMod->pImgVt);
690
691 /*
692 * Set the debug file name and try possible interpreters.
693 */
694 pDbgMod->pszDbgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
695
696 int rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
697 if (RT_SUCCESS(rc))
698 {
699 for (PRTDBGMODREGDBG pDbg = g_pDbgHead; pDbg; pDbg = pDbg->pNext)
700 {
701 pDbgMod->pDbgVt = pDbg->pVt;
702 pDbgMod->pvDbgPriv = NULL;
703 rc = pDbg->pVt->pfnTryOpen(pDbgMod, pDbgMod->pImgVt->pfnGetArch(pDbgMod));
704 if (RT_SUCCESS(rc))
705 {
706 /*
707 * Got it!
708 */
709 ASMAtomicIncU32(&pDbg->cUsers);
710 RTSemRWReleaseRead(g_hDbgModRWSem);
711 return VINF_CALLBACK_RETURN;
712 }
713 pDbgMod->pDbgVt = NULL;
714 Assert(pDbgMod->pvDbgPriv == NULL);
715 }
716 }
717
718 /* No joy. */
719 RTSemRWReleaseRead(g_hDbgModRWSem);
720 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszDbgFile);
721 pDbgMod->pszDbgFile = NULL;
722 return rc;
723}
724
725
726/**
727 * Opens external debug info that is not listed in the image.
728 *
729 * @returns IPRT status code
730 * @param pDbgMod The debug module.
731 * @param hDbgCfg The debug config. Can be NIL.
732 */
733static int rtDbgModOpenDebugInfoExternalToImage2(PRTDBGMODINT pDbgMod, RTDBGCFG hDbgCfg)
734{
735 int rc;
736 Assert(!pDbgMod->pDbgVt);
737 Assert(pDbgMod->pImgVt);
738
739 /*
740 * Figure out what to search for based on the image format.
741 */
742 const char *pszzExts = NULL;
743 RTLDRFMT enmFmt = pDbgMod->pImgVt->pfnGetFormat(pDbgMod);
744 switch (enmFmt)
745 {
746 case RTLDRFMT_MACHO:
747 {
748 RTUUID Uuid;
749 PRTUUID pUuid = &Uuid;
750 rc = pDbgMod->pImgVt->pfnQueryProp(pDbgMod, RTLDRPROP_UUID, &Uuid, sizeof(Uuid), NULL);
751 if (RT_FAILURE(rc))
752 pUuid = NULL;
753
754 rc = RTDbgCfgOpenDsymBundle(hDbgCfg, pDbgMod->pszImgFile, pUuid,
755 rtDbgModExtDbgInfoOpenCallback2, pDbgMod, NULL /*pvUser2*/);
756 if (RT_SUCCESS(rc))
757 return VINF_SUCCESS;
758 break;
759 }
760
761#if 0 /* Will be links in the image if these apply. .map readers for PE or ELF we don't have. */
762 case RTLDRFMT_ELF:
763 pszzExts = ".debug\0.dwo\0";
764 break;
765 case RTLDRFMT_PE:
766 pszzExts = ".map\0";
767 break;
768#endif
769#if 0 /* Haven't implemented .sym or .map file readers for OS/2 yet. */
770 case RTLDRFMT_LX:
771 pszzExts = ".sym\0.map\0";
772 break;
773#endif
774 default:
775 rc = VERR_NOT_IMPLEMENTED;
776 break;
777 }
778
779 NOREF(pszzExts);
780#if 0 /* Later */
781 if (pszzExts)
782 {
783
784 }
785#endif
786
787 LogFlow(("rtDbgModOpenDebugInfoExternalToImage2: rc=%Rrc\n", rc));
788 return VERR_NOT_FOUND;
789}
790
791
792RTDECL(int) RTDbgModCreateFromImage(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName,
793 RTLDRARCH enmArch, RTDBGCFG hDbgCfg)
794{
795 /*
796 * Input validation and lazy initialization.
797 */
798 AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
799 *phDbgMod = NIL_RTDBGMOD;
800 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
801 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
802 AssertPtrNullReturn(pszName, VERR_INVALID_POINTER);
803 AssertReturn(enmArch > RTLDRARCH_INVALID && enmArch < RTLDRARCH_END, VERR_INVALID_PARAMETER);
804
805 int rc = rtDbgModLazyInit();
806 if (RT_FAILURE(rc))
807 return rc;
808
809 if (!pszName)
810 pszName = RTPathFilenameEx(pszFilename, RTPATH_STR_F_STYLE_DOS);
811
812 /*
813 * Allocate a new module instance.
814 */
815 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
816 if (!pDbgMod)
817 return VERR_NO_MEMORY;
818 pDbgMod->u32Magic = RTDBGMOD_MAGIC;
819 pDbgMod->cRefs = 1;
820 rc = RTCritSectInit(&pDbgMod->CritSect);
821 if (RT_SUCCESS(rc))
822 {
823 pDbgMod->pszName = RTStrCacheEnterLower(g_hDbgModStrCache, pszName);
824 if (pDbgMod->pszName)
825 {
826 pDbgMod->pszImgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
827 if (pDbgMod->pszImgFile)
828 {
829 RTStrCacheRetain(pDbgMod->pszImgFile);
830 pDbgMod->pszImgFileSpecified = pDbgMod->pszImgFile;
831
832 /*
833 * Find an image reader which groks the file.
834 */
835 rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
836 if (RT_SUCCESS(rc))
837 {
838 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
839 PRTDBGMODREGIMG pImg;
840 for (pImg = g_pImgHead; pImg; pImg = pImg->pNext)
841 {
842 pDbgMod->pImgVt = pImg->pVt;
843 pDbgMod->pvImgPriv = NULL;
844 /** @todo need to specify some arch stuff here. */
845 rc = pImg->pVt->pfnTryOpen(pDbgMod, enmArch);
846 if (RT_SUCCESS(rc))
847 {
848 /*
849 * Image detected, but found no debug info we were
850 * able to understand.
851 */
852 /** @todo some generic way of matching image and debug info, flexible signature
853 * of some kind. Apple uses UUIDs, microsoft uses a UUID+age or a
854 * size+timestamp, and GNU a CRC32 (last time I checked). */
855 rc = rtDbgModOpenDebugInfoExternalToImage(pDbgMod, hDbgCfg);
856 if (RT_FAILURE(rc))
857 rc = rtDbgModOpenDebugInfoInsideImage(pDbgMod);
858 if (RT_FAILURE(rc))
859 rc = rtDbgModOpenDebugInfoExternalToImage2(pDbgMod, hDbgCfg);
860 if (RT_FAILURE(rc))
861 rc = rtDbgModCreateForExports(pDbgMod);
862 if (RT_SUCCESS(rc))
863 {
864 /*
865 * We're done!
866 */
867 ASMAtomicIncU32(&pImg->cUsers);
868 RTSemRWReleaseRead(g_hDbgModRWSem);
869
870 *phDbgMod = pDbgMod;
871 return VINF_SUCCESS;
872 }
873
874 /* Failed, close up the shop. */
875 pDbgMod->pImgVt->pfnClose(pDbgMod);
876 pDbgMod->pImgVt = NULL;
877 pDbgMod->pvImgPriv = NULL;
878 break;
879 }
880 }
881
882 /*
883 * Could it be a file containing raw debug info?
884 */
885 if (!pImg)
886 {
887 pDbgMod->pImgVt = NULL;
888 pDbgMod->pvImgPriv = NULL;
889 pDbgMod->pszDbgFile = pDbgMod->pszImgFile;
890 pDbgMod->pszImgFile = NULL;
891
892 for (PRTDBGMODREGDBG pDbg = g_pDbgHead; pDbg; pDbg = pDbg->pNext)
893 {
894 pDbgMod->pDbgVt = pDbg->pVt;
895 pDbgMod->pvDbgPriv = NULL;
896 rc = pDbg->pVt->pfnTryOpen(pDbgMod, enmArch);
897 if (RT_SUCCESS(rc))
898 {
899 /*
900 * That's it!
901 */
902 ASMAtomicIncU32(&pDbg->cUsers);
903 RTSemRWReleaseRead(g_hDbgModRWSem);
904
905 *phDbgMod = pDbgMod;
906 return rc;
907 }
908 }
909
910 pDbgMod->pszImgFile = pDbgMod->pszDbgFile;
911 pDbgMod->pszDbgFile = NULL;
912 }
913
914 /* bail out */
915 RTSemRWReleaseRead(g_hDbgModRWSem);
916 }
917 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFileSpecified);
918 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
919 }
920 else
921 rc = VERR_NO_STR_MEMORY;
922 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
923 }
924 else
925 rc = VERR_NO_STR_MEMORY;
926 RTCritSectDelete(&pDbgMod->CritSect);
927 }
928
929 RTMemFree(pDbgMod);
930 return rc;
931}
932RT_EXPORT_SYMBOL(RTDbgModCreateFromImage);
933
934
935
936
937
938/*
939 *
940 * P E I M A G E
941 * P E I M A G E
942 * P E I M A G E
943 *
944 */
945
946
947
948/** @callback_method_impl{FNRTDBGCFGOPEN} */
949static DECLCALLBACK(int) rtDbgModFromPeImageOpenCallback(RTDBGCFG hDbgCfg, const char *pszFilename, void *pvUser1, void *pvUser2)
950{
951 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)pvUser1;
952 PRTDBGMODDEFERRED pDeferred = (PRTDBGMODDEFERRED)pvUser2;
953 LogFlow(("rtDbgModFromPeImageOpenCallback: %s\n", pszFilename));
954 RT_NOREF_PV(hDbgCfg);
955
956 Assert(pDbgMod->pImgVt == NULL);
957 Assert(pDbgMod->pvImgPriv == NULL);
958 Assert(pDbgMod->pDbgVt == NULL);
959 Assert(pDbgMod->pvDbgPriv == NULL);
960
961 /*
962 * Replace the image file name while probing it.
963 */
964 const char *pszNewImgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
965 if (!pszNewImgFile)
966 return VERR_NO_STR_MEMORY;
967 const char *pszOldImgFile = pDbgMod->pszImgFile;
968 pDbgMod->pszImgFile = pszNewImgFile;
969
970 /*
971 * Find an image reader which groks the file.
972 */
973 int rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
974 if (RT_SUCCESS(rc))
975 {
976 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
977 PRTDBGMODREGIMG pImg;
978 for (pImg = g_pImgHead; pImg; pImg = pImg->pNext)
979 {
980 pDbgMod->pImgVt = pImg->pVt;
981 pDbgMod->pvImgPriv = NULL;
982 rc = pImg->pVt->pfnTryOpen(pDbgMod, RTLDRARCH_WHATEVER);
983 if (RT_SUCCESS(rc))
984 break;
985 pDbgMod->pImgVt = NULL;
986 Assert(pDbgMod->pvImgPriv == NULL);
987 }
988 RTSemRWReleaseRead(g_hDbgModRWSem);
989 if (RT_SUCCESS(rc))
990 {
991 /*
992 * Check the deferred info.
993 */
994 RTUINTPTR cbImage = pDbgMod->pImgVt->pfnImageSize(pDbgMod);
995 if ( pDeferred->cbImage == 0
996 || pDeferred->cbImage == cbImage)
997 {
998 uint32_t uTimestamp = pDeferred->u.PeImage.uTimestamp; /** @todo add method for getting the timestamp. */
999 if ( pDeferred->u.PeImage.uTimestamp == 0
1000 || pDeferred->u.PeImage.uTimestamp == uTimestamp)
1001 {
1002 Log(("RTDbgMod: Found matching PE image '%s'\n", pszFilename));
1003
1004 /*
1005 * We found the executable image we need, now go find any
1006 * debug info associated with it. For PE images, this is
1007 * generally found in an external file, so we do a sweep
1008 * for that first.
1009 *
1010 * Then try open debug inside the module, and finally
1011 * falling back on exports.
1012 */
1013 rc = rtDbgModOpenDebugInfoExternalToImage(pDbgMod, pDeferred->hDbgCfg);
1014 if (RT_FAILURE(rc))
1015 rc = rtDbgModOpenDebugInfoInsideImage(pDbgMod);
1016 if (RT_FAILURE(rc))
1017 rc = rtDbgModCreateForExports(pDbgMod);
1018 if (RT_SUCCESS(rc))
1019 {
1020 RTStrCacheRelease(g_hDbgModStrCache, pszOldImgFile);
1021 return VINF_CALLBACK_RETURN;
1022 }
1023
1024 /* Something bad happened, just give up. */
1025 Log(("rtDbgModFromPeImageOpenCallback: rtDbgModCreateForExports failed: %Rrc\n", rc));
1026 }
1027 else
1028 {
1029 LogFlow(("rtDbgModFromPeImageOpenCallback: uTimestamp mismatch (found %#x, expected %#x) - %s\n",
1030 uTimestamp, pDeferred->u.PeImage.uTimestamp, pszFilename));
1031 rc = VERR_DBG_FILE_MISMATCH;
1032 }
1033 }
1034 else
1035 {
1036 LogFlow(("rtDbgModFromPeImageOpenCallback: cbImage mismatch (found %#x, expected %#x) - %s\n",
1037 cbImage, pDeferred->cbImage, pszFilename));
1038 rc = VERR_DBG_FILE_MISMATCH;
1039 }
1040
1041 pDbgMod->pImgVt->pfnClose(pDbgMod);
1042 pDbgMod->pImgVt = NULL;
1043 pDbgMod->pvImgPriv = NULL;
1044 }
1045 else
1046 LogFlow(("rtDbgModFromPeImageOpenCallback: Failed %Rrc - %s\n", rc, pszFilename));
1047 }
1048
1049 /* Restore image name. */
1050 pDbgMod->pszImgFile = pszOldImgFile;
1051 RTStrCacheRelease(g_hDbgModStrCache, pszNewImgFile);
1052 return rc;
1053}
1054
1055
1056/** @callback_method_impl{FNRTDBGMODDEFERRED} */
1057static DECLCALLBACK(int) rtDbgModFromPeImageDeferredCallback(PRTDBGMODINT pDbgMod, PRTDBGMODDEFERRED pDeferred)
1058{
1059 int rc;
1060
1061 Assert(pDbgMod->pszImgFile);
1062 if (!pDbgMod->pImgVt)
1063 rc = RTDbgCfgOpenPeImage(pDeferred->hDbgCfg, pDbgMod->pszImgFile,
1064 pDeferred->cbImage, pDeferred->u.PeImage.uTimestamp,
1065 rtDbgModFromPeImageOpenCallback, pDbgMod, pDeferred);
1066 else
1067 {
1068 rc = rtDbgModOpenDebugInfoExternalToImage(pDbgMod, pDeferred->hDbgCfg);
1069 if (RT_FAILURE(rc))
1070 rc = rtDbgModOpenDebugInfoInsideImage(pDbgMod);
1071 if (RT_FAILURE(rc))
1072 rc = rtDbgModCreateForExports(pDbgMod);
1073 }
1074 return rc;
1075}
1076
1077
1078RTDECL(int) RTDbgModCreateFromPeImage(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName,
1079 PRTLDRMOD phLdrMod, uint32_t cbImage, uint32_t uTimestamp, RTDBGCFG hDbgCfg)
1080{
1081 /*
1082 * Input validation and lazy initialization.
1083 */
1084 AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
1085 *phDbgMod = NIL_RTDBGMOD;
1086 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1087 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
1088 if (!pszName)
1089 pszName = RTPathFilenameEx(pszFilename, RTPATH_STR_F_STYLE_DOS);
1090 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1091 AssertPtrNullReturn(phLdrMod, VERR_INVALID_POINTER);
1092 RTLDRMOD hLdrMod = phLdrMod ? *phLdrMod : NIL_RTLDRMOD;
1093 AssertReturn(hLdrMod == NIL_RTLDRMOD || RTLdrSize(hLdrMod) != ~(size_t)0, VERR_INVALID_HANDLE);
1094
1095 int rc = rtDbgModLazyInit();
1096 if (RT_FAILURE(rc))
1097 return rc;
1098
1099 uint64_t fDbgCfg = 0;
1100 if (hDbgCfg)
1101 {
1102 rc = RTDbgCfgQueryUInt(hDbgCfg, RTDBGCFGPROP_FLAGS, &fDbgCfg);
1103 AssertRCReturn(rc, rc);
1104 }
1105
1106 /*
1107 * Allocate a new module instance.
1108 */
1109 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
1110 if (!pDbgMod)
1111 return VERR_NO_MEMORY;
1112 pDbgMod->u32Magic = RTDBGMOD_MAGIC;
1113 pDbgMod->cRefs = 1;
1114 rc = RTCritSectInit(&pDbgMod->CritSect);
1115 if (RT_SUCCESS(rc))
1116 {
1117 pDbgMod->pszName = RTStrCacheEnterLower(g_hDbgModStrCache, pszName);
1118 if (pDbgMod->pszName)
1119 {
1120 pDbgMod->pszImgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
1121 if (pDbgMod->pszImgFile)
1122 {
1123 RTStrCacheRetain(pDbgMod->pszImgFile);
1124 pDbgMod->pszImgFileSpecified = pDbgMod->pszImgFile;
1125
1126 /*
1127 * If we have a loader module, we must instantiate the loader
1128 * side of things regardless of the deferred setting.
1129 */
1130 if (hLdrMod != NIL_RTLDRMOD)
1131 {
1132 if (!cbImage)
1133 cbImage = (uint32_t)RTLdrSize(hLdrMod);
1134 pDbgMod->pImgVt = &g_rtDbgModVtImgLdr;
1135
1136 rc = rtDbgModLdrOpenFromHandle(pDbgMod, hLdrMod);
1137 }
1138 if (RT_SUCCESS(rc))
1139 {
1140 /* We now own the loader handle, so clear the caller variable. */
1141 if (phLdrMod)
1142 *phLdrMod = NIL_RTLDRMOD;
1143
1144 /*
1145 * Do it now or procrastinate?
1146 */
1147 if (!(fDbgCfg & RTDBGCFG_FLAGS_DEFERRED) || !cbImage)
1148 {
1149 RTDBGMODDEFERRED Deferred;
1150 Deferred.cbImage = cbImage;
1151 Deferred.hDbgCfg = hDbgCfg;
1152 Deferred.u.PeImage.uTimestamp = uTimestamp;
1153 rc = rtDbgModFromPeImageDeferredCallback(pDbgMod, &Deferred);
1154 }
1155 else
1156 {
1157 PRTDBGMODDEFERRED pDeferred;
1158 rc = rtDbgModDeferredCreate(pDbgMod, rtDbgModFromPeImageDeferredCallback, cbImage, hDbgCfg, 0,
1159 &pDeferred);
1160 if (RT_SUCCESS(rc))
1161 pDeferred->u.PeImage.uTimestamp = uTimestamp;
1162 }
1163 if (RT_SUCCESS(rc))
1164 {
1165 *phDbgMod = pDbgMod;
1166 return VINF_SUCCESS;
1167 }
1168
1169 /* Failed, bail out. */
1170 if (hLdrMod != NIL_RTLDRMOD)
1171 {
1172 Assert(pDbgMod->pImgVt);
1173 pDbgMod->pImgVt->pfnClose(pDbgMod);
1174 }
1175 }
1176 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
1177 }
1178 else
1179 rc = VERR_NO_STR_MEMORY;
1180 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFileSpecified);
1181 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
1182 }
1183 else
1184 rc = VERR_NO_STR_MEMORY;
1185 RTCritSectDelete(&pDbgMod->CritSect);
1186 }
1187
1188 RTMemFree(pDbgMod);
1189 return rc;
1190}
1191RT_EXPORT_SYMBOL(RTDbgModCreateFromPeImage);
1192
1193
1194
1195
1196/*
1197 *
1198 * M a c h - O I M A G E
1199 * M a c h - O I M A G E
1200 * M a c h - O I M A G E
1201 *
1202 */
1203
1204
1205/**
1206 * Argument package used when opening Mach-O images and .dSYMs files.
1207 */
1208typedef struct RTDBGMODMACHOARGS
1209{
1210 /** For use more internal use in file locator callbacks. */
1211 RTLDRARCH enmArch;
1212 /** For use more internal use in file locator callbacks. */
1213 PCRTUUID pUuid;
1214 /** For use more internal use in file locator callbacks. */
1215 bool fOpenImage;
1216} RTDBGMODMACHOARGS;
1217/** Pointer to a const segment package. */
1218typedef RTDBGMODMACHOARGS const *PCRTDBGMODMACHOARGS;
1219
1220
1221
1222/** @callback_method_impl{FNRTDBGCFGOPEN} */
1223static DECLCALLBACK(int)
1224rtDbgModFromMachOImageOpenDsymMachOCallback(RTDBGCFG hDbgCfg, const char *pszFilename, void *pvUser1, void *pvUser2)
1225{
1226 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)pvUser1;
1227 PCRTDBGMODMACHOARGS pArgs = (PCRTDBGMODMACHOARGS)pvUser2;
1228 RT_NOREF_PV(hDbgCfg);
1229
1230 Assert(!pDbgMod->pDbgVt);
1231 Assert(!pDbgMod->pvDbgPriv);
1232 Assert(!pDbgMod->pszDbgFile);
1233 Assert(!pDbgMod->pImgVt);
1234 Assert(!pDbgMod->pvDbgPriv);
1235 Assert(pDbgMod->pszImgFile);
1236 Assert(pDbgMod->pszImgFileSpecified);
1237
1238 const char *pszImgFileOrg = pDbgMod->pszImgFile;
1239 pDbgMod->pszImgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
1240 if (!pDbgMod->pszImgFile)
1241 return VERR_NO_STR_MEMORY;
1242 RTStrCacheRetain(pDbgMod->pszImgFile);
1243 pDbgMod->pszDbgFile = pDbgMod->pszImgFile;
1244
1245 /*
1246 * Try image interpreters as the dwarf file inside the dSYM bundle is a
1247 * Mach-O file with dwarf debug sections insides it and no code or data.
1248 */
1249 int rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
1250 if (RT_SUCCESS(rc))
1251 {
1252 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
1253 PRTDBGMODREGIMG pImg;
1254 for (pImg = g_pImgHead; pImg; pImg = pImg->pNext)
1255 {
1256 pDbgMod->pImgVt = pImg->pVt;
1257 pDbgMod->pvImgPriv = NULL;
1258 rc = pImg->pVt->pfnTryOpen(pDbgMod, pArgs->enmArch);
1259 if (RT_SUCCESS(rc))
1260 break;
1261 pDbgMod->pImgVt = NULL;
1262 Assert(pDbgMod->pvImgPriv == NULL);
1263 }
1264
1265 if (RT_SUCCESS(rc))
1266 {
1267 /*
1268 * Check the UUID if one was given.
1269 */
1270 if (pArgs->pUuid)
1271 {
1272 RTUUID UuidOpened;
1273 rc = pDbgMod->pImgVt->pfnQueryProp(pDbgMod, RTLDRPROP_UUID, &UuidOpened, sizeof(UuidOpened), NULL);
1274 if (RT_SUCCESS(rc))
1275 {
1276 if (RTUuidCompare(&UuidOpened, pArgs->pUuid) != 0)
1277 rc = VERR_DBG_FILE_MISMATCH;
1278 }
1279 else if (rc == VERR_NOT_FOUND || rc == VERR_NOT_IMPLEMENTED)
1280 rc = VERR_DBG_FILE_MISMATCH;
1281 }
1282 if (RT_SUCCESS(rc))
1283 {
1284 /*
1285 * Pass it to the DWARF reader(s). Careful to restrict this or
1286 * the dbghelp wrapper may end up being overly helpful.
1287 */
1288 for (PRTDBGMODREGDBG pDbg = g_pDbgHead; pDbg; pDbg = pDbg->pNext)
1289 {
1290 if (pDbg->pVt->fSupports & (RT_DBGTYPE_DWARF | RT_DBGTYPE_STABS | RT_DBGTYPE_WATCOM))
1291
1292 {
1293 pDbgMod->pDbgVt = pDbg->pVt;
1294 pDbgMod->pvDbgPriv = NULL;
1295 rc = pDbg->pVt->pfnTryOpen(pDbgMod, pDbgMod->pImgVt->pfnGetArch(pDbgMod));
1296 if (RT_SUCCESS(rc))
1297 {
1298 /*
1299 * Got it!
1300 */
1301 ASMAtomicIncU32(&pDbg->cUsers);
1302 RTSemRWReleaseRead(g_hDbgModRWSem);
1303 RTStrCacheRelease(g_hDbgModStrCache, pszImgFileOrg);
1304 return VINF_CALLBACK_RETURN;
1305 }
1306 pDbgMod->pDbgVt = NULL;
1307 Assert(pDbgMod->pvDbgPriv == NULL);
1308 }
1309 }
1310
1311 /*
1312 * Likely fallback for when opening image.
1313 */
1314 if (pArgs->fOpenImage)
1315 {
1316 rc = rtDbgModCreateForExports(pDbgMod);
1317 if (RT_SUCCESS(rc))
1318 {
1319 /*
1320 * Done.
1321 */
1322 RTSemRWReleaseRead(g_hDbgModRWSem);
1323 RTStrCacheRelease(g_hDbgModStrCache, pszImgFileOrg);
1324 return VINF_CALLBACK_RETURN;
1325 }
1326 }
1327 }
1328
1329 pDbgMod->pImgVt->pfnClose(pDbgMod);
1330 pDbgMod->pImgVt = NULL;
1331 pDbgMod->pvImgPriv = NULL;
1332 }
1333 }
1334
1335 /* No joy. */
1336 RTSemRWReleaseRead(g_hDbgModRWSem);
1337 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
1338 pDbgMod->pszImgFile = pszImgFileOrg;
1339 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszDbgFile);
1340 pDbgMod->pszDbgFile = NULL;
1341 return rc;
1342}
1343
1344
1345static int rtDbgModFromMachOImageWorker(PRTDBGMODINT pDbgMod, RTLDRARCH enmArch, uint32_t cbImage,
1346 uint32_t cSegs, PCRTDBGSEGMENT paSegs, PCRTUUID pUuid, RTDBGCFG hDbgCfg)
1347{
1348 RT_NOREF_PV(cbImage); RT_NOREF_PV(cSegs); RT_NOREF_PV(paSegs);
1349
1350 RTDBGMODMACHOARGS Args;
1351 Args.enmArch = enmArch;
1352 Args.pUuid = pUuid && RTUuidIsNull(pUuid) ? pUuid : NULL;
1353 Args.fOpenImage = false;
1354
1355 /*
1356 * Search for the .dSYM bundle first, since that's generally all we need.
1357 */
1358 int rc = RTDbgCfgOpenDsymBundle(hDbgCfg, pDbgMod->pszImgFile, pUuid,
1359 rtDbgModFromMachOImageOpenDsymMachOCallback, pDbgMod, &Args);
1360 if (RT_FAILURE(rc))
1361 {
1362 /*
1363 * If we cannot get at the .dSYM, try the executable image.
1364 */
1365 Args.fOpenImage = true;
1366 rc = RTDbgCfgOpenMachOImage(hDbgCfg, pDbgMod->pszImgFile, pUuid,
1367 rtDbgModFromMachOImageOpenDsymMachOCallback, pDbgMod, &Args);
1368 }
1369 return rc;
1370}
1371
1372
1373/** @callback_method_impl{FNRTDBGMODDEFERRED} */
1374static DECLCALLBACK(int) rtDbgModFromMachOImageDeferredCallback(PRTDBGMODINT pDbgMod, PRTDBGMODDEFERRED pDeferred)
1375{
1376 return rtDbgModFromMachOImageWorker(pDbgMod, pDeferred->u.MachO.enmArch, pDeferred->cbImage,
1377 pDeferred->u.MachO.cSegs, pDeferred->u.MachO.aSegs,
1378 &pDeferred->u.MachO.Uuid, pDeferred->hDbgCfg);
1379}
1380
1381
1382RTDECL(int) RTDbgModCreateFromMachOImage(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName,
1383 RTLDRARCH enmArch, uint32_t cbImage, uint32_t cSegs, PCRTDBGSEGMENT paSegs,
1384 PCRTUUID pUuid, RTDBGCFG hDbgCfg, uint32_t fFlags)
1385{
1386 /*
1387 * Input validation and lazy initialization.
1388 */
1389 AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
1390 *phDbgMod = NIL_RTDBGMOD;
1391 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1392 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
1393 if (!pszName)
1394 pszName = RTPathFilenameEx(pszFilename, RTPATH_STR_F_STYLE_HOST);
1395 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1396 if (cSegs)
1397 {
1398 AssertReturn(cSegs < 1024, VERR_INVALID_PARAMETER);
1399 AssertPtrReturn(paSegs, VERR_INVALID_POINTER);
1400 AssertReturn(!cbImage, VERR_INVALID_PARAMETER);
1401 }
1402 AssertReturn(cbImage || cSegs, VERR_INVALID_PARAMETER);
1403 AssertPtrNullReturn(pUuid, VERR_INVALID_POINTER);
1404 AssertReturn(!(fFlags & ~(RTDBGMOD_F_NOT_DEFERRED)), VERR_INVALID_PARAMETER);
1405
1406 int rc = rtDbgModLazyInit();
1407 if (RT_FAILURE(rc))
1408 return rc;
1409
1410 uint64_t fDbgCfg = 0;
1411 if (hDbgCfg)
1412 {
1413 rc = RTDbgCfgQueryUInt(hDbgCfg, RTDBGCFGPROP_FLAGS, &fDbgCfg);
1414 AssertRCReturn(rc, rc);
1415 }
1416
1417 /*
1418 * Allocate a new module instance.
1419 */
1420 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
1421 if (!pDbgMod)
1422 return VERR_NO_MEMORY;
1423 pDbgMod->u32Magic = RTDBGMOD_MAGIC;
1424 pDbgMod->cRefs = 1;
1425 rc = RTCritSectInit(&pDbgMod->CritSect);
1426 if (RT_SUCCESS(rc))
1427 {
1428 pDbgMod->pszName = RTStrCacheEnterLower(g_hDbgModStrCache, pszName);
1429 if (pDbgMod->pszName)
1430 {
1431 pDbgMod->pszImgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
1432 if (pDbgMod->pszImgFile)
1433 {
1434 RTStrCacheRetain(pDbgMod->pszImgFile);
1435 pDbgMod->pszImgFileSpecified = pDbgMod->pszImgFile;
1436
1437 /*
1438 * Load it immediately?
1439 */
1440 if ( !(fDbgCfg & RTDBGCFG_FLAGS_DEFERRED)
1441 || cSegs /* for the time being. */
1442 || (!cbImage && !cSegs)
1443 || (fFlags & RTDBGMOD_F_NOT_DEFERRED) )
1444 rc = rtDbgModFromMachOImageWorker(pDbgMod, enmArch, cbImage, cSegs, paSegs, pUuid, hDbgCfg);
1445 else
1446 {
1447 /*
1448 * Procrastinate. Need image size atm.
1449 */
1450 PRTDBGMODDEFERRED pDeferred;
1451 rc = rtDbgModDeferredCreate(pDbgMod, rtDbgModFromMachOImageDeferredCallback, cbImage, hDbgCfg,
1452 RT_UOFFSETOF_DYN(RTDBGMODDEFERRED, u.MachO.aSegs[cSegs]),
1453 &pDeferred);
1454 if (RT_SUCCESS(rc))
1455 {
1456 pDeferred->u.MachO.Uuid = *pUuid;
1457 pDeferred->u.MachO.enmArch = enmArch;
1458 pDeferred->u.MachO.cSegs = cSegs;
1459 if (cSegs)
1460 memcpy(&pDeferred->u.MachO.aSegs, paSegs, cSegs * sizeof(paSegs[0]));
1461 }
1462 }
1463 if (RT_SUCCESS(rc))
1464 {
1465 *phDbgMod = pDbgMod;
1466 return VINF_SUCCESS;
1467 }
1468
1469 /* Failed, bail out. */
1470 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
1471 }
1472 else
1473 rc = VERR_NO_STR_MEMORY;
1474 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFileSpecified);
1475 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
1476 }
1477 else
1478 rc = VERR_NO_STR_MEMORY;
1479 RTCritSectDelete(&pDbgMod->CritSect);
1480 }
1481
1482 RTMemFree(pDbgMod);
1483 return rc;
1484}
1485
1486
1487
1488RT_EXPORT_SYMBOL(RTDbgModCreateFromMachOImage);
1489
1490
1491
1492/**
1493 * Destroys an module after the reference count has reached zero.
1494 *
1495 * @param pDbgMod The module instance.
1496 */
1497static void rtDbgModDestroy(PRTDBGMODINT pDbgMod)
1498{
1499 /*
1500 * Close the debug info interpreter first, then the image interpret.
1501 */
1502 RTCritSectEnter(&pDbgMod->CritSect); /* paranoia */
1503
1504 if (pDbgMod->pDbgVt)
1505 {
1506 pDbgMod->pDbgVt->pfnClose(pDbgMod);
1507 pDbgMod->pDbgVt = NULL;
1508 pDbgMod->pvDbgPriv = NULL;
1509 }
1510
1511 if (pDbgMod->pImgVt)
1512 {
1513 pDbgMod->pImgVt->pfnClose(pDbgMod);
1514 pDbgMod->pImgVt = NULL;
1515 pDbgMod->pvImgPriv = NULL;
1516 }
1517
1518 /*
1519 * Free the resources.
1520 */
1521 ASMAtomicWriteU32(&pDbgMod->u32Magic, ~RTDBGMOD_MAGIC);
1522 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
1523 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
1524 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFileSpecified);
1525 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszDbgFile);
1526 RTCritSectLeave(&pDbgMod->CritSect); /* paranoia */
1527 RTCritSectDelete(&pDbgMod->CritSect);
1528 RTMemFree(pDbgMod);
1529}
1530
1531
1532RTDECL(uint32_t) RTDbgModRetain(RTDBGMOD hDbgMod)
1533{
1534 PRTDBGMODINT pDbgMod = hDbgMod;
1535 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
1536 return ASMAtomicIncU32(&pDbgMod->cRefs);
1537}
1538RT_EXPORT_SYMBOL(RTDbgModRetain);
1539
1540
1541RTDECL(uint32_t) RTDbgModRelease(RTDBGMOD hDbgMod)
1542{
1543 if (hDbgMod == NIL_RTDBGMOD)
1544 return 0;
1545 PRTDBGMODINT pDbgMod = hDbgMod;
1546 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
1547
1548 uint32_t cRefs = ASMAtomicDecU32(&pDbgMod->cRefs);
1549 if (!cRefs)
1550 rtDbgModDestroy(pDbgMod);
1551 return cRefs;
1552}
1553RT_EXPORT_SYMBOL(RTDbgModRelease);
1554
1555
1556RTDECL(const char *) RTDbgModName(RTDBGMOD hDbgMod)
1557{
1558 PRTDBGMODINT pDbgMod = hDbgMod;
1559 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NULL);
1560 return pDbgMod->pszName;
1561}
1562RT_EXPORT_SYMBOL(RTDbgModName);
1563
1564
1565RTDECL(const char *) RTDbgModDebugFile(RTDBGMOD hDbgMod)
1566{
1567 PRTDBGMODINT pDbgMod = hDbgMod;
1568 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NULL);
1569 if (pDbgMod->fDeferred || pDbgMod->fExports)
1570 return NULL;
1571 return pDbgMod->pszDbgFile;
1572}
1573RT_EXPORT_SYMBOL(RTDbgModDebugFile);
1574
1575
1576RTDECL(const char *) RTDbgModImageFile(RTDBGMOD hDbgMod)
1577{
1578 PRTDBGMODINT pDbgMod = hDbgMod;
1579 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NULL);
1580 return pDbgMod->pszImgFileSpecified;
1581}
1582RT_EXPORT_SYMBOL(RTDbgModImageFile);
1583
1584
1585RTDECL(const char *) RTDbgModImageFileUsed(RTDBGMOD hDbgMod)
1586{
1587 PRTDBGMODINT pDbgMod = hDbgMod;
1588 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NULL);
1589 return pDbgMod->pszImgFile == pDbgMod->pszImgFileSpecified ? NULL : pDbgMod->pszImgFile;
1590}
1591RT_EXPORT_SYMBOL(RTDbgModImageFileUsed);
1592
1593
1594RTDECL(bool) RTDbgModIsDeferred(RTDBGMOD hDbgMod)
1595{
1596 PRTDBGMODINT pDbgMod = hDbgMod;
1597 RTDBGMOD_VALID_RETURN_RC(pDbgMod, false);
1598 return pDbgMod->fDeferred;
1599}
1600
1601
1602RTDECL(bool) RTDbgModIsExports(RTDBGMOD hDbgMod)
1603{
1604 PRTDBGMODINT pDbgMod = hDbgMod;
1605 RTDBGMOD_VALID_RETURN_RC(pDbgMod, false);
1606 return pDbgMod->fExports;
1607}
1608
1609
1610RTDECL(int) RTDbgModRemoveAll(RTDBGMOD hDbgMod, bool fLeaveSegments)
1611{
1612 PRTDBGMODINT pDbgMod = hDbgMod;
1613 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1614
1615 RTDBGMOD_LOCK(pDbgMod);
1616
1617 /* Only possible on container modules. */
1618 int rc = VINF_SUCCESS;
1619 if (pDbgMod->pDbgVt != &g_rtDbgModVtDbgContainer)
1620 {
1621 if (fLeaveSegments)
1622 {
1623 rc = rtDbgModContainer_LineRemoveAll(pDbgMod);
1624 if (RT_SUCCESS(rc))
1625 rc = rtDbgModContainer_SymbolRemoveAll(pDbgMod);
1626 }
1627 else
1628 rc = rtDbgModContainer_RemoveAll(pDbgMod);
1629 }
1630 else
1631 rc = VERR_ACCESS_DENIED;
1632
1633 RTDBGMOD_UNLOCK(pDbgMod);
1634 return rc;
1635}
1636
1637
1638RTDECL(RTDBGSEGIDX) RTDbgModRvaToSegOff(RTDBGMOD hDbgMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
1639{
1640 PRTDBGMODINT pDbgMod = hDbgMod;
1641 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NIL_RTDBGSEGIDX);
1642 RTDBGMOD_LOCK(pDbgMod);
1643
1644 RTDBGSEGIDX iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, uRva, poffSeg);
1645
1646 RTDBGMOD_UNLOCK(pDbgMod);
1647 return iSeg;
1648}
1649RT_EXPORT_SYMBOL(RTDbgModRvaToSegOff);
1650
1651
1652RTDECL(uint64_t) RTDbgModGetTag(RTDBGMOD hDbgMod)
1653{
1654 PRTDBGMODINT pDbgMod = hDbgMod;
1655 RTDBGMOD_VALID_RETURN_RC(pDbgMod, 0);
1656 return pDbgMod->uTag;
1657}
1658RT_EXPORT_SYMBOL(RTDbgModGetTag);
1659
1660
1661RTDECL(int) RTDbgModSetTag(RTDBGMOD hDbgMod, uint64_t uTag)
1662{
1663 PRTDBGMODINT pDbgMod = hDbgMod;
1664 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1665 RTDBGMOD_LOCK(pDbgMod);
1666
1667 pDbgMod->uTag = uTag;
1668
1669 RTDBGMOD_UNLOCK(pDbgMod);
1670 return VINF_SUCCESS;
1671}
1672RT_EXPORT_SYMBOL(RTDbgModSetTag);
1673
1674
1675RTDECL(RTUINTPTR) RTDbgModImageSize(RTDBGMOD hDbgMod)
1676{
1677 PRTDBGMODINT pDbgMod = hDbgMod;
1678 RTDBGMOD_VALID_RETURN_RC(pDbgMod, RTUINTPTR_MAX);
1679 RTDBGMOD_LOCK(pDbgMod);
1680
1681 RTUINTPTR cbImage = pDbgMod->pDbgVt->pfnImageSize(pDbgMod);
1682
1683 RTDBGMOD_UNLOCK(pDbgMod);
1684 return cbImage;
1685}
1686RT_EXPORT_SYMBOL(RTDbgModImageSize);
1687
1688
1689RTDECL(RTLDRFMT) RTDbgModImageGetFormat(RTDBGMOD hDbgMod)
1690{
1691 PRTDBGMODINT pDbgMod = hDbgMod;
1692 RTDBGMOD_VALID_RETURN_RC(pDbgMod, RTLDRFMT_INVALID);
1693 RTDBGMOD_LOCK(pDbgMod);
1694
1695 RTLDRFMT enmFmt;
1696 if ( pDbgMod->pImgVt
1697 && pDbgMod->pImgVt->pfnGetFormat)
1698 enmFmt = pDbgMod->pImgVt->pfnGetFormat(pDbgMod);
1699 else
1700 enmFmt = RTLDRFMT_INVALID;
1701
1702 RTDBGMOD_UNLOCK(pDbgMod);
1703 return enmFmt;
1704}
1705RT_EXPORT_SYMBOL(RTDbgModImageGetFormat);
1706
1707
1708RTDECL(RTLDRARCH) RTDbgModImageGetArch(RTDBGMOD hDbgMod)
1709{
1710 PRTDBGMODINT pDbgMod = hDbgMod;
1711 RTDBGMOD_VALID_RETURN_RC(pDbgMod, RTLDRARCH_INVALID);
1712 RTDBGMOD_LOCK(pDbgMod);
1713
1714 RTLDRARCH enmArch;
1715 if ( pDbgMod->pImgVt
1716 && pDbgMod->pImgVt->pfnGetArch)
1717 enmArch = pDbgMod->pImgVt->pfnGetArch(pDbgMod);
1718 else
1719 enmArch = RTLDRARCH_WHATEVER;
1720
1721 RTDBGMOD_UNLOCK(pDbgMod);
1722 return enmArch;
1723}
1724RT_EXPORT_SYMBOL(RTDbgModImageGetArch);
1725
1726
1727RTDECL(int) RTDbgModImageQueryProp(RTDBGMOD hDbgMod, RTLDRPROP enmProp, void *pvBuf, size_t cbBuf, size_t *pcbRet)
1728{
1729 PRTDBGMODINT pDbgMod = hDbgMod;
1730 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1731 AssertPtrNullReturn(pcbRet, VERR_INVALID_POINTER);
1732 RTDBGMOD_LOCK(pDbgMod);
1733
1734 int rc;
1735 if ( pDbgMod->pImgVt
1736 && pDbgMod->pImgVt->pfnQueryProp)
1737 rc = pDbgMod->pImgVt->pfnQueryProp(pDbgMod, enmProp, pvBuf, cbBuf, pcbRet);
1738 else
1739 rc = VERR_NOT_FOUND;
1740
1741 RTDBGMOD_UNLOCK(pDbgMod);
1742 return rc;
1743}
1744RT_EXPORT_SYMBOL(RTDbgModImageQueryProp);
1745
1746
1747RTDECL(int) RTDbgModSegmentAdd(RTDBGMOD hDbgMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName,
1748 uint32_t fFlags, PRTDBGSEGIDX piSeg)
1749{
1750 /*
1751 * Validate input.
1752 */
1753 PRTDBGMODINT pDbgMod = hDbgMod;
1754 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1755 AssertMsgReturn(uRva + cb >= uRva, ("uRva=%RTptr cb=%RTptr\n", uRva, cb), VERR_DBG_ADDRESS_WRAP);
1756 Assert(*pszName);
1757 size_t cchName = strlen(pszName);
1758 AssertReturn(cchName > 0, VERR_DBG_SEGMENT_NAME_OUT_OF_RANGE);
1759 AssertReturn(cchName < RTDBG_SEGMENT_NAME_LENGTH, VERR_DBG_SEGMENT_NAME_OUT_OF_RANGE);
1760 AssertMsgReturn(!fFlags, ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
1761 AssertPtrNull(piSeg);
1762 AssertMsgReturn(!piSeg || *piSeg == NIL_RTDBGSEGIDX || *piSeg <= RTDBGSEGIDX_LAST, ("%#x\n", *piSeg), VERR_DBG_SPECIAL_SEGMENT);
1763
1764 /*
1765 * Do the deed.
1766 */
1767 RTDBGMOD_LOCK(pDbgMod);
1768 int rc = pDbgMod->pDbgVt->pfnSegmentAdd(pDbgMod, uRva, cb, pszName, cchName, fFlags, piSeg);
1769 RTDBGMOD_UNLOCK(pDbgMod);
1770
1771 return rc;
1772
1773}
1774RT_EXPORT_SYMBOL(RTDbgModSegmentAdd);
1775
1776
1777RTDECL(RTDBGSEGIDX) RTDbgModSegmentCount(RTDBGMOD hDbgMod)
1778{
1779 PRTDBGMODINT pDbgMod = hDbgMod;
1780 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NIL_RTDBGSEGIDX);
1781 RTDBGMOD_LOCK(pDbgMod);
1782
1783 RTDBGSEGIDX cSegs = pDbgMod->pDbgVt->pfnSegmentCount(pDbgMod);
1784
1785 RTDBGMOD_UNLOCK(pDbgMod);
1786 return cSegs;
1787}
1788RT_EXPORT_SYMBOL(RTDbgModSegmentCount);
1789
1790
1791RTDECL(int) RTDbgModSegmentByIndex(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
1792{
1793 AssertMsgReturn(iSeg <= RTDBGSEGIDX_LAST, ("%#x\n", iSeg), VERR_DBG_SPECIAL_SEGMENT);
1794 PRTDBGMODINT pDbgMod = hDbgMod;
1795 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1796 RTDBGMOD_LOCK(pDbgMod);
1797
1798 int rc = pDbgMod->pDbgVt->pfnSegmentByIndex(pDbgMod, iSeg, pSegInfo);
1799
1800 RTDBGMOD_UNLOCK(pDbgMod);
1801 return rc;
1802}
1803RT_EXPORT_SYMBOL(RTDbgModSegmentByIndex);
1804
1805
1806RTDECL(RTUINTPTR) RTDbgModSegmentSize(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg)
1807{
1808 if (iSeg == RTDBGSEGIDX_RVA)
1809 return RTDbgModImageSize(hDbgMod);
1810 RTDBGSEGMENT SegInfo;
1811 int rc = RTDbgModSegmentByIndex(hDbgMod, iSeg, &SegInfo);
1812 return RT_SUCCESS(rc) ? SegInfo.cb : RTUINTPTR_MAX;
1813}
1814RT_EXPORT_SYMBOL(RTDbgModSegmentSize);
1815
1816
1817RTDECL(RTUINTPTR) RTDbgModSegmentRva(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg)
1818{
1819 RTDBGSEGMENT SegInfo;
1820 int rc = RTDbgModSegmentByIndex(hDbgMod, iSeg, &SegInfo);
1821 return RT_SUCCESS(rc) ? SegInfo.uRva : RTUINTPTR_MAX;
1822}
1823RT_EXPORT_SYMBOL(RTDbgModSegmentRva);
1824
1825
1826RTDECL(int) RTDbgModSymbolAdd(RTDBGMOD hDbgMod, const char *pszSymbol, RTDBGSEGIDX iSeg, RTUINTPTR off,
1827 RTUINTPTR cb, uint32_t fFlags, uint32_t *piOrdinal)
1828{
1829 /*
1830 * Validate input.
1831 */
1832 PRTDBGMODINT pDbgMod = hDbgMod;
1833 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1834 AssertPtrReturn(pszSymbol, VERR_INVALID_POINTER);
1835 size_t cchSymbol = strlen(pszSymbol);
1836 AssertReturn(cchSymbol, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
1837 AssertReturn(cchSymbol < RTDBG_SYMBOL_NAME_LENGTH, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
1838 AssertMsgReturn( iSeg <= RTDBGSEGIDX_LAST
1839 || ( iSeg >= RTDBGSEGIDX_SPECIAL_FIRST
1840 && iSeg <= RTDBGSEGIDX_SPECIAL_LAST),
1841 ("%#x\n", iSeg),
1842 VERR_DBG_INVALID_SEGMENT_INDEX);
1843 AssertMsgReturn(off + cb >= off, ("off=%RTptr cb=%RTptr\n", off, cb), VERR_DBG_ADDRESS_WRAP);
1844 AssertReturn(!(fFlags & ~RTDBGSYMBOLADD_F_VALID_MASK), VERR_INVALID_FLAGS);
1845
1846 RTDBGMOD_LOCK(pDbgMod);
1847
1848 /*
1849 * Convert RVAs.
1850 */
1851 if (iSeg == RTDBGSEGIDX_RVA)
1852 {
1853 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
1854 if (iSeg == NIL_RTDBGSEGIDX)
1855 {
1856 RTDBGMOD_UNLOCK(pDbgMod);
1857 return VERR_DBG_INVALID_RVA;
1858 }
1859 }
1860
1861 /*
1862 * Get down to business.
1863 */
1864 int rc = pDbgMod->pDbgVt->pfnSymbolAdd(pDbgMod, pszSymbol, cchSymbol, iSeg, off, cb, fFlags, piOrdinal);
1865
1866 RTDBGMOD_UNLOCK(pDbgMod);
1867 return rc;
1868}
1869RT_EXPORT_SYMBOL(RTDbgModSymbolAdd);
1870
1871
1872RTDECL(uint32_t) RTDbgModSymbolCount(RTDBGMOD hDbgMod)
1873{
1874 PRTDBGMODINT pDbgMod = hDbgMod;
1875 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
1876 RTDBGMOD_LOCK(pDbgMod);
1877
1878 uint32_t cSymbols = pDbgMod->pDbgVt->pfnSymbolCount(pDbgMod);
1879
1880 RTDBGMOD_UNLOCK(pDbgMod);
1881 return cSymbols;
1882}
1883RT_EXPORT_SYMBOL(RTDbgModSymbolCount);
1884
1885
1886RTDECL(int) RTDbgModSymbolByOrdinal(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
1887{
1888 PRTDBGMODINT pDbgMod = hDbgMod;
1889 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1890 RTDBGMOD_LOCK(pDbgMod);
1891
1892 int rc = pDbgMod->pDbgVt->pfnSymbolByOrdinal(pDbgMod, iOrdinal, pSymInfo);
1893
1894 RTDBGMOD_UNLOCK(pDbgMod);
1895 return rc;
1896}
1897RT_EXPORT_SYMBOL(RTDbgModSymbolByOrdinal);
1898
1899
1900RTDECL(int) RTDbgModSymbolByOrdinalA(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGSYMBOL *ppSymInfo)
1901{
1902 AssertPtr(ppSymInfo);
1903 *ppSymInfo = NULL;
1904
1905 PRTDBGSYMBOL pSymInfo = RTDbgSymbolAlloc();
1906 if (!pSymInfo)
1907 return VERR_NO_MEMORY;
1908
1909 int rc = RTDbgModSymbolByOrdinal(hDbgMod, iOrdinal, pSymInfo);
1910
1911 if (RT_SUCCESS(rc))
1912 *ppSymInfo = pSymInfo;
1913 else
1914 RTDbgSymbolFree(pSymInfo);
1915 return rc;
1916}
1917RT_EXPORT_SYMBOL(RTDbgModSymbolByOrdinalA);
1918
1919
1920/**
1921 * Return a segment number/name as symbol if we couldn't find any
1922 * valid symbols within the segment.
1923 */
1924DECL_NO_INLINE(static, int)
1925rtDbgModSymbolByAddrTrySegments(PRTDBGMODINT pDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
1926 PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
1927{
1928 Assert(iSeg <= RTDBGSEGIDX_LAST);
1929 RTDBGSEGMENT SegInfo;
1930 int rc = pDbgMod->pDbgVt->pfnSegmentByIndex(pDbgMod, iSeg, &SegInfo);
1931 if (RT_SUCCESS(rc))
1932 {
1933 pSymInfo->Value = 0;
1934 pSymInfo->cb = SegInfo.cb;
1935 pSymInfo->offSeg = 0;
1936 pSymInfo->iSeg = iSeg;
1937 pSymInfo->fFlags = 0;
1938 if (SegInfo.szName[0])
1939 RTStrPrintf(pSymInfo->szName, sizeof(pSymInfo->szName), "start_seg%u_%s", SegInfo.iSeg, SegInfo.szName);
1940 else
1941 RTStrPrintf(pSymInfo->szName, sizeof(pSymInfo->szName), "start_seg%u", SegInfo.iSeg);
1942 if (poffDisp)
1943 *poffDisp = off;
1944 return VINF_SUCCESS;
1945 }
1946 return VERR_SYMBOL_NOT_FOUND;
1947}
1948
1949
1950RTDECL(int) RTDbgModSymbolByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
1951 PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
1952{
1953 /*
1954 * Validate input.
1955 */
1956 PRTDBGMODINT pDbgMod = hDbgMod;
1957 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1958 AssertPtrNull(poffDisp);
1959 AssertPtr(pSymInfo);
1960 AssertReturn(!(fFlags & ~RTDBGSYMADDR_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
1961
1962 RTDBGMOD_LOCK(pDbgMod);
1963
1964 /*
1965 * Convert RVAs.
1966 */
1967 if (iSeg == RTDBGSEGIDX_RVA)
1968 {
1969 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
1970 if (iSeg == NIL_RTDBGSEGIDX)
1971 {
1972 RTDBGMOD_UNLOCK(pDbgMod);
1973 return VERR_DBG_INVALID_RVA;
1974 }
1975 }
1976
1977 /*
1978 * Get down to business.
1979 */
1980 int rc = pDbgMod->pDbgVt->pfnSymbolByAddr(pDbgMod, iSeg, off, fFlags, poffDisp, pSymInfo);
1981
1982 /* If we failed to locate a symbol, try use the specified segment as a reference. */
1983 if ( rc == VERR_SYMBOL_NOT_FOUND
1984 && iSeg <= RTDBGSEGIDX_LAST
1985 && !(fFlags & RTDBGSYMADDR_FLAGS_GREATER_OR_EQUAL))
1986 rc = rtDbgModSymbolByAddrTrySegments(pDbgMod, iSeg, off, poffDisp, pSymInfo);
1987
1988 RTDBGMOD_UNLOCK(pDbgMod);
1989 return rc;
1990}
1991RT_EXPORT_SYMBOL(RTDbgModSymbolByAddr);
1992
1993
1994RTDECL(int) RTDbgModSymbolByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
1995 PRTINTPTR poffDisp, PRTDBGSYMBOL *ppSymInfo)
1996{
1997 AssertPtr(ppSymInfo);
1998 *ppSymInfo = NULL;
1999
2000 PRTDBGSYMBOL pSymInfo = RTDbgSymbolAlloc();
2001 if (!pSymInfo)
2002 return VERR_NO_MEMORY;
2003
2004 int rc = RTDbgModSymbolByAddr(hDbgMod, iSeg, off, fFlags, poffDisp, pSymInfo);
2005
2006 if (RT_SUCCESS(rc))
2007 *ppSymInfo = pSymInfo;
2008 else
2009 RTDbgSymbolFree(pSymInfo);
2010 return rc;
2011}
2012RT_EXPORT_SYMBOL(RTDbgModSymbolByAddrA);
2013
2014
2015RTDECL(int) RTDbgModSymbolByName(RTDBGMOD hDbgMod, const char *pszSymbol, PRTDBGSYMBOL pSymInfo)
2016{
2017 /*
2018 * Validate input.
2019 */
2020 PRTDBGMODINT pDbgMod = hDbgMod;
2021 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
2022 AssertPtr(pszSymbol);
2023 size_t cchSymbol = strlen(pszSymbol);
2024 AssertReturn(cchSymbol, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
2025 AssertReturn(cchSymbol < RTDBG_SYMBOL_NAME_LENGTH, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
2026 AssertPtr(pSymInfo);
2027
2028 /*
2029 * Make the query.
2030 */
2031 RTDBGMOD_LOCK(pDbgMod);
2032 int rc = pDbgMod->pDbgVt->pfnSymbolByName(pDbgMod, pszSymbol, cchSymbol, pSymInfo);
2033 RTDBGMOD_UNLOCK(pDbgMod);
2034
2035 return rc;
2036}
2037RT_EXPORT_SYMBOL(RTDbgModSymbolByName);
2038
2039
2040RTDECL(int) RTDbgModSymbolByNameA(RTDBGMOD hDbgMod, const char *pszSymbol, PRTDBGSYMBOL *ppSymInfo)
2041{
2042 AssertPtr(ppSymInfo);
2043 *ppSymInfo = NULL;
2044
2045 PRTDBGSYMBOL pSymInfo = RTDbgSymbolAlloc();
2046 if (!pSymInfo)
2047 return VERR_NO_MEMORY;
2048
2049 int rc = RTDbgModSymbolByName(hDbgMod, pszSymbol, pSymInfo);
2050
2051 if (RT_SUCCESS(rc))
2052 *ppSymInfo = pSymInfo;
2053 else
2054 RTDbgSymbolFree(pSymInfo);
2055 return rc;
2056}
2057RT_EXPORT_SYMBOL(RTDbgModSymbolByNameA);
2058
2059
2060RTDECL(int) RTDbgModLineAdd(RTDBGMOD hDbgMod, const char *pszFile, uint32_t uLineNo,
2061 RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t *piOrdinal)
2062{
2063 /*
2064 * Validate input.
2065 */
2066 PRTDBGMODINT pDbgMod = hDbgMod;
2067 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
2068 AssertPtr(pszFile);
2069 size_t cchFile = strlen(pszFile);
2070 AssertReturn(cchFile, VERR_DBG_FILE_NAME_OUT_OF_RANGE);
2071 AssertReturn(cchFile < RTDBG_FILE_NAME_LENGTH, VERR_DBG_FILE_NAME_OUT_OF_RANGE);
2072 AssertMsgReturn( iSeg <= RTDBGSEGIDX_LAST
2073 || iSeg == RTDBGSEGIDX_RVA,
2074 ("%#x\n", iSeg),
2075 VERR_DBG_INVALID_SEGMENT_INDEX);
2076 AssertReturn(uLineNo > 0 && uLineNo < UINT32_MAX, VERR_INVALID_PARAMETER);
2077
2078 RTDBGMOD_LOCK(pDbgMod);
2079
2080 /*
2081 * Convert RVAs.
2082 */
2083 if (iSeg == RTDBGSEGIDX_RVA)
2084 {
2085 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
2086 if (iSeg == NIL_RTDBGSEGIDX)
2087 {
2088 RTDBGMOD_UNLOCK(pDbgMod);
2089 return VERR_DBG_INVALID_RVA;
2090 }
2091 }
2092
2093 /*
2094 * Get down to business.
2095 */
2096 int rc = pDbgMod->pDbgVt->pfnLineAdd(pDbgMod, pszFile, cchFile, uLineNo, iSeg, off, piOrdinal);
2097
2098 RTDBGMOD_UNLOCK(pDbgMod);
2099 return rc;
2100}
2101RT_EXPORT_SYMBOL(RTDbgModLineAdd);
2102
2103
2104RTDECL(uint32_t) RTDbgModLineCount(RTDBGMOD hDbgMod)
2105{
2106 PRTDBGMODINT pDbgMod = hDbgMod;
2107 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
2108 RTDBGMOD_LOCK(pDbgMod);
2109
2110 uint32_t cLineNumbers = pDbgMod->pDbgVt->pfnLineCount(pDbgMod);
2111
2112 RTDBGMOD_UNLOCK(pDbgMod);
2113 return cLineNumbers;
2114}
2115RT_EXPORT_SYMBOL(RTDbgModLineCount);
2116
2117
2118RTDECL(int) RTDbgModLineByOrdinal(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
2119{
2120 PRTDBGMODINT pDbgMod = hDbgMod;
2121 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
2122 RTDBGMOD_LOCK(pDbgMod);
2123
2124 int rc = pDbgMod->pDbgVt->pfnLineByOrdinal(pDbgMod, iOrdinal, pLineInfo);
2125
2126 RTDBGMOD_UNLOCK(pDbgMod);
2127 return rc;
2128}
2129RT_EXPORT_SYMBOL(RTDbgModLineByOrdinal);
2130
2131
2132RTDECL(int) RTDbgModLineByOrdinalA(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGLINE *ppLineInfo)
2133{
2134 AssertPtr(ppLineInfo);
2135 *ppLineInfo = NULL;
2136
2137 PRTDBGLINE pLineInfo = RTDbgLineAlloc();
2138 if (!pLineInfo)
2139 return VERR_NO_MEMORY;
2140
2141 int rc = RTDbgModLineByOrdinal(hDbgMod, iOrdinal, pLineInfo);
2142
2143 if (RT_SUCCESS(rc))
2144 *ppLineInfo = pLineInfo;
2145 else
2146 RTDbgLineFree(pLineInfo);
2147 return rc;
2148}
2149RT_EXPORT_SYMBOL(RTDbgModLineByOrdinalA);
2150
2151
2152RTDECL(int) RTDbgModLineByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
2153{
2154 /*
2155 * Validate input.
2156 */
2157 PRTDBGMODINT pDbgMod = hDbgMod;
2158 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
2159 AssertPtrNull(poffDisp);
2160 AssertPtr(pLineInfo);
2161
2162 RTDBGMOD_LOCK(pDbgMod);
2163
2164 /*
2165 * Convert RVAs.
2166 */
2167 if (iSeg == RTDBGSEGIDX_RVA)
2168 {
2169 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
2170 if (iSeg == NIL_RTDBGSEGIDX)
2171 {
2172 RTDBGMOD_UNLOCK(pDbgMod);
2173 return VERR_DBG_INVALID_RVA;
2174 }
2175 }
2176
2177 int rc = pDbgMod->pDbgVt->pfnLineByAddr(pDbgMod, iSeg, off, poffDisp, pLineInfo);
2178
2179 RTDBGMOD_UNLOCK(pDbgMod);
2180 return rc;
2181}
2182RT_EXPORT_SYMBOL(RTDbgModLineByAddr);
2183
2184
2185RTDECL(int) RTDbgModLineByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE *ppLineInfo)
2186{
2187 AssertPtr(ppLineInfo);
2188 *ppLineInfo = NULL;
2189
2190 PRTDBGLINE pLineInfo = RTDbgLineAlloc();
2191 if (!pLineInfo)
2192 return VERR_NO_MEMORY;
2193
2194 int rc = RTDbgModLineByAddr(hDbgMod, iSeg, off, poffDisp, pLineInfo);
2195
2196 if (RT_SUCCESS(rc))
2197 *ppLineInfo = pLineInfo;
2198 else
2199 RTDbgLineFree(pLineInfo);
2200 return rc;
2201}
2202RT_EXPORT_SYMBOL(RTDbgModLineByAddrA);
2203
2204
2205RTDECL(int) RTDbgModUnwindFrame(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTDBGUNWINDSTATE pState)
2206{
2207 /*
2208 * Validate input.
2209 */
2210 PRTDBGMODINT pDbgMod = hDbgMod;
2211 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
2212 AssertPtr(pState);
2213 AssertReturn(pState->u32Magic == RTDBGUNWINDSTATE_MAGIC, VERR_INVALID_MAGIC);
2214
2215 RTDBGMOD_LOCK(pDbgMod);
2216
2217 /*
2218 * Convert RVAs.
2219 */
2220 if (iSeg == RTDBGSEGIDX_RVA)
2221 {
2222 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
2223 if (iSeg == NIL_RTDBGSEGIDX)
2224 {
2225 RTDBGMOD_UNLOCK(pDbgMod);
2226 return VERR_DBG_INVALID_RVA;
2227 }
2228 }
2229
2230 /*
2231 * Try the debug module first, then the image.
2232 */
2233 int rc = VERR_DBG_NO_UNWIND_INFO;
2234 if (pDbgMod->pDbgVt->pfnUnwindFrame)
2235 rc = pDbgMod->pDbgVt->pfnUnwindFrame(pDbgMod, iSeg, off, pState);
2236 if ( ( rc == VERR_DBG_NO_UNWIND_INFO
2237 || rc == VERR_DBG_UNWIND_INFO_NOT_FOUND)
2238 && pDbgMod->pImgVt
2239 && pDbgMod->pImgVt->pfnUnwindFrame)
2240 {
2241 if (rc == VERR_DBG_NO_UNWIND_INFO)
2242 rc = pDbgMod->pImgVt->pfnUnwindFrame(pDbgMod, iSeg, off, pState);
2243 else
2244 {
2245 rc = pDbgMod->pImgVt->pfnUnwindFrame(pDbgMod, iSeg, off, pState);
2246 if (rc == VERR_DBG_NO_UNWIND_INFO)
2247 rc = VERR_DBG_UNWIND_INFO_NOT_FOUND;
2248 }
2249 }
2250
2251 RTDBGMOD_UNLOCK(pDbgMod);
2252 return rc;
2253
2254}
2255RT_EXPORT_SYMBOL(RTDbgModUnwindFrame);
2256
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