VirtualBox

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

Last change on this file since 78365 was 77981, checked in by vboxsync, 6 years ago

Runtime/dbgmod.cpp: Make RTDbgModCreateFromMap(), RTDbgModCreateFromImage(), rtDbgModFromPeImageOpenCallback() and rtDbgModFromMachOImageOpenDsymMachOCallback() return VERR_DBG_NO_MATCHING_INTERPRETER (like it was probably intended to) if no matching backend was found instead of just returning the error code (like VERR_EOF) of the last backend tried

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 71.7 KB
Line 
1/* $Id: dbgmod.cpp 77981 2019-04-01 18:49:03Z vboxsync $ */
2/** @file
3 * IPRT - Debug Module Interpreter.
4 */
5
6/*
7 * Copyright (C) 2009-2019 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 for (PRTDBGMODREGDBG pCur = g_pDbgHead; pCur; pCur = pCur->pNext)
416 {
417 if (pCur->pVt->fSupports & RT_DBGTYPE_MAP)
418 {
419 pDbgMod->pDbgVt = pCur->pVt;
420 pDbgMod->pvDbgPriv = NULL;
421 rc = pCur->pVt->pfnTryOpen(pDbgMod, RTLDRARCH_WHATEVER);
422 if (RT_SUCCESS(rc))
423 {
424 ASMAtomicIncU32(&pCur->cUsers);
425 RTSemRWReleaseRead(g_hDbgModRWSem);
426
427 *phDbgMod = pDbgMod;
428 return rc;
429 }
430 }
431 }
432
433 /* bail out */
434 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
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 PRTDBGMODREGIMG pImg;
839 for (pImg = g_pImgHead; pImg; pImg = pImg->pNext)
840 {
841 pDbgMod->pImgVt = pImg->pVt;
842 pDbgMod->pvImgPriv = NULL;
843 /** @todo need to specify some arch stuff here. */
844 rc = pImg->pVt->pfnTryOpen(pDbgMod, enmArch);
845 if (RT_SUCCESS(rc))
846 {
847 /*
848 * Image detected, but found no debug info we were
849 * able to understand.
850 */
851 /** @todo some generic way of matching image and debug info, flexible signature
852 * of some kind. Apple uses UUIDs, microsoft uses a UUID+age or a
853 * size+timestamp, and GNU a CRC32 (last time I checked). */
854 rc = rtDbgModOpenDebugInfoExternalToImage(pDbgMod, hDbgCfg);
855 if (RT_FAILURE(rc))
856 rc = rtDbgModOpenDebugInfoInsideImage(pDbgMod);
857 if (RT_FAILURE(rc))
858 rc = rtDbgModOpenDebugInfoExternalToImage2(pDbgMod, hDbgCfg);
859 if (RT_FAILURE(rc))
860 rc = rtDbgModCreateForExports(pDbgMod);
861 if (RT_SUCCESS(rc))
862 {
863 /*
864 * We're done!
865 */
866 ASMAtomicIncU32(&pImg->cUsers);
867 RTSemRWReleaseRead(g_hDbgModRWSem);
868
869 *phDbgMod = pDbgMod;
870 return VINF_SUCCESS;
871 }
872
873 /* Failed, close up the shop. */
874 pDbgMod->pImgVt->pfnClose(pDbgMod);
875 pDbgMod->pImgVt = NULL;
876 pDbgMod->pvImgPriv = NULL;
877 break;
878 }
879 }
880
881 /*
882 * Could it be a file containing raw debug info?
883 */
884 if (!pImg)
885 {
886 pDbgMod->pImgVt = NULL;
887 pDbgMod->pvImgPriv = NULL;
888 pDbgMod->pszDbgFile = pDbgMod->pszImgFile;
889 pDbgMod->pszImgFile = NULL;
890
891 for (PRTDBGMODREGDBG pDbg = g_pDbgHead; pDbg; pDbg = pDbg->pNext)
892 {
893 pDbgMod->pDbgVt = pDbg->pVt;
894 pDbgMod->pvDbgPriv = NULL;
895 rc = pDbg->pVt->pfnTryOpen(pDbgMod, enmArch);
896 if (RT_SUCCESS(rc))
897 {
898 /*
899 * That's it!
900 */
901 ASMAtomicIncU32(&pDbg->cUsers);
902 RTSemRWReleaseRead(g_hDbgModRWSem);
903
904 *phDbgMod = pDbgMod;
905 return rc;
906 }
907 }
908
909 pDbgMod->pszImgFile = pDbgMod->pszDbgFile;
910 pDbgMod->pszDbgFile = NULL;
911 }
912
913 /* bail out */
914 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
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 int rc2 = pImg->pVt->pfnTryOpen(pDbgMod, RTLDRARCH_WHATEVER);
983 if (RT_SUCCESS(rc2))
984 {
985 rc = rc2;
986 break;
987 }
988 pDbgMod->pImgVt = NULL;
989 Assert(pDbgMod->pvImgPriv == NULL);
990 }
991 RTSemRWReleaseRead(g_hDbgModRWSem);
992 if (RT_SUCCESS(rc))
993 {
994 /*
995 * Check the deferred info.
996 */
997 RTUINTPTR cbImage = pDbgMod->pImgVt->pfnImageSize(pDbgMod);
998 if ( pDeferred->cbImage == 0
999 || pDeferred->cbImage == cbImage)
1000 {
1001 uint32_t uTimestamp = pDeferred->u.PeImage.uTimestamp; /** @todo add method for getting the timestamp. */
1002 if ( pDeferred->u.PeImage.uTimestamp == 0
1003 || pDeferred->u.PeImage.uTimestamp == uTimestamp)
1004 {
1005 Log(("RTDbgMod: Found matching PE image '%s'\n", pszFilename));
1006
1007 /*
1008 * We found the executable image we need, now go find any
1009 * debug info associated with it. For PE images, this is
1010 * generally found in an external file, so we do a sweep
1011 * for that first.
1012 *
1013 * Then try open debug inside the module, and finally
1014 * falling back on exports.
1015 */
1016 rc = rtDbgModOpenDebugInfoExternalToImage(pDbgMod, pDeferred->hDbgCfg);
1017 if (RT_FAILURE(rc))
1018 rc = rtDbgModOpenDebugInfoInsideImage(pDbgMod);
1019 if (RT_FAILURE(rc))
1020 rc = rtDbgModCreateForExports(pDbgMod);
1021 if (RT_SUCCESS(rc))
1022 {
1023 RTStrCacheRelease(g_hDbgModStrCache, pszOldImgFile);
1024 return VINF_CALLBACK_RETURN;
1025 }
1026
1027 /* Something bad happened, just give up. */
1028 Log(("rtDbgModFromPeImageOpenCallback: rtDbgModCreateForExports failed: %Rrc\n", rc));
1029 }
1030 else
1031 {
1032 LogFlow(("rtDbgModFromPeImageOpenCallback: uTimestamp mismatch (found %#x, expected %#x) - %s\n",
1033 uTimestamp, pDeferred->u.PeImage.uTimestamp, pszFilename));
1034 rc = VERR_DBG_FILE_MISMATCH;
1035 }
1036 }
1037 else
1038 {
1039 LogFlow(("rtDbgModFromPeImageOpenCallback: cbImage mismatch (found %#x, expected %#x) - %s\n",
1040 cbImage, pDeferred->cbImage, pszFilename));
1041 rc = VERR_DBG_FILE_MISMATCH;
1042 }
1043
1044 pDbgMod->pImgVt->pfnClose(pDbgMod);
1045 pDbgMod->pImgVt = NULL;
1046 pDbgMod->pvImgPriv = NULL;
1047 }
1048 else
1049 LogFlow(("rtDbgModFromPeImageOpenCallback: Failed %Rrc - %s\n", rc, pszFilename));
1050 }
1051
1052 /* Restore image name. */
1053 pDbgMod->pszImgFile = pszOldImgFile;
1054 RTStrCacheRelease(g_hDbgModStrCache, pszNewImgFile);
1055 return rc;
1056}
1057
1058
1059/** @callback_method_impl{FNRTDBGMODDEFERRED} */
1060static DECLCALLBACK(int) rtDbgModFromPeImageDeferredCallback(PRTDBGMODINT pDbgMod, PRTDBGMODDEFERRED pDeferred)
1061{
1062 int rc;
1063
1064 Assert(pDbgMod->pszImgFile);
1065 if (!pDbgMod->pImgVt)
1066 rc = RTDbgCfgOpenPeImage(pDeferred->hDbgCfg, pDbgMod->pszImgFile,
1067 pDeferred->cbImage, pDeferred->u.PeImage.uTimestamp,
1068 rtDbgModFromPeImageOpenCallback, pDbgMod, pDeferred);
1069 else
1070 {
1071 rc = rtDbgModOpenDebugInfoExternalToImage(pDbgMod, pDeferred->hDbgCfg);
1072 if (RT_FAILURE(rc))
1073 rc = rtDbgModOpenDebugInfoInsideImage(pDbgMod);
1074 if (RT_FAILURE(rc))
1075 rc = rtDbgModCreateForExports(pDbgMod);
1076 }
1077 return rc;
1078}
1079
1080
1081RTDECL(int) RTDbgModCreateFromPeImage(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName,
1082 PRTLDRMOD phLdrMod, uint32_t cbImage, uint32_t uTimestamp, RTDBGCFG hDbgCfg)
1083{
1084 /*
1085 * Input validation and lazy initialization.
1086 */
1087 AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
1088 *phDbgMod = NIL_RTDBGMOD;
1089 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1090 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
1091 if (!pszName)
1092 pszName = RTPathFilenameEx(pszFilename, RTPATH_STR_F_STYLE_DOS);
1093 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1094 AssertPtrNullReturn(phLdrMod, VERR_INVALID_POINTER);
1095 RTLDRMOD hLdrMod = phLdrMod ? *phLdrMod : NIL_RTLDRMOD;
1096 AssertReturn(hLdrMod == NIL_RTLDRMOD || RTLdrSize(hLdrMod) != ~(size_t)0, VERR_INVALID_HANDLE);
1097
1098 int rc = rtDbgModLazyInit();
1099 if (RT_FAILURE(rc))
1100 return rc;
1101
1102 uint64_t fDbgCfg = 0;
1103 if (hDbgCfg)
1104 {
1105 rc = RTDbgCfgQueryUInt(hDbgCfg, RTDBGCFGPROP_FLAGS, &fDbgCfg);
1106 AssertRCReturn(rc, rc);
1107 }
1108
1109 /*
1110 * Allocate a new module instance.
1111 */
1112 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
1113 if (!pDbgMod)
1114 return VERR_NO_MEMORY;
1115 pDbgMod->u32Magic = RTDBGMOD_MAGIC;
1116 pDbgMod->cRefs = 1;
1117 rc = RTCritSectInit(&pDbgMod->CritSect);
1118 if (RT_SUCCESS(rc))
1119 {
1120 pDbgMod->pszName = RTStrCacheEnterLower(g_hDbgModStrCache, pszName);
1121 if (pDbgMod->pszName)
1122 {
1123 pDbgMod->pszImgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
1124 if (pDbgMod->pszImgFile)
1125 {
1126 RTStrCacheRetain(pDbgMod->pszImgFile);
1127 pDbgMod->pszImgFileSpecified = pDbgMod->pszImgFile;
1128
1129 /*
1130 * If we have a loader module, we must instantiate the loader
1131 * side of things regardless of the deferred setting.
1132 */
1133 if (hLdrMod != NIL_RTLDRMOD)
1134 {
1135 if (!cbImage)
1136 cbImage = (uint32_t)RTLdrSize(hLdrMod);
1137 pDbgMod->pImgVt = &g_rtDbgModVtImgLdr;
1138
1139 rc = rtDbgModLdrOpenFromHandle(pDbgMod, hLdrMod);
1140 }
1141 if (RT_SUCCESS(rc))
1142 {
1143 /* We now own the loader handle, so clear the caller variable. */
1144 if (phLdrMod)
1145 *phLdrMod = NIL_RTLDRMOD;
1146
1147 /*
1148 * Do it now or procrastinate?
1149 */
1150 if (!(fDbgCfg & RTDBGCFG_FLAGS_DEFERRED) || !cbImage)
1151 {
1152 RTDBGMODDEFERRED Deferred;
1153 Deferred.cbImage = cbImage;
1154 Deferred.hDbgCfg = hDbgCfg;
1155 Deferred.u.PeImage.uTimestamp = uTimestamp;
1156 rc = rtDbgModFromPeImageDeferredCallback(pDbgMod, &Deferred);
1157 }
1158 else
1159 {
1160 PRTDBGMODDEFERRED pDeferred;
1161 rc = rtDbgModDeferredCreate(pDbgMod, rtDbgModFromPeImageDeferredCallback, cbImage, hDbgCfg, 0,
1162 &pDeferred);
1163 if (RT_SUCCESS(rc))
1164 pDeferred->u.PeImage.uTimestamp = uTimestamp;
1165 }
1166 if (RT_SUCCESS(rc))
1167 {
1168 *phDbgMod = pDbgMod;
1169 return VINF_SUCCESS;
1170 }
1171
1172 /* Failed, bail out. */
1173 if (hLdrMod != NIL_RTLDRMOD)
1174 {
1175 Assert(pDbgMod->pImgVt);
1176 pDbgMod->pImgVt->pfnClose(pDbgMod);
1177 }
1178 }
1179 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
1180 }
1181 else
1182 rc = VERR_NO_STR_MEMORY;
1183 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFileSpecified);
1184 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
1185 }
1186 else
1187 rc = VERR_NO_STR_MEMORY;
1188 RTCritSectDelete(&pDbgMod->CritSect);
1189 }
1190
1191 RTMemFree(pDbgMod);
1192 return rc;
1193}
1194RT_EXPORT_SYMBOL(RTDbgModCreateFromPeImage);
1195
1196
1197
1198
1199/*
1200 *
1201 * M a c h - O I M A G E
1202 * M a c h - O I M A G E
1203 * M a c h - O I M A G E
1204 *
1205 */
1206
1207
1208/**
1209 * Argument package used when opening Mach-O images and .dSYMs files.
1210 */
1211typedef struct RTDBGMODMACHOARGS
1212{
1213 /** For use more internal use in file locator callbacks. */
1214 RTLDRARCH enmArch;
1215 /** For use more internal use in file locator callbacks. */
1216 PCRTUUID pUuid;
1217 /** For use more internal use in file locator callbacks. */
1218 bool fOpenImage;
1219} RTDBGMODMACHOARGS;
1220/** Pointer to a const segment package. */
1221typedef RTDBGMODMACHOARGS const *PCRTDBGMODMACHOARGS;
1222
1223
1224
1225/** @callback_method_impl{FNRTDBGCFGOPEN} */
1226static DECLCALLBACK(int)
1227rtDbgModFromMachOImageOpenDsymMachOCallback(RTDBGCFG hDbgCfg, const char *pszFilename, void *pvUser1, void *pvUser2)
1228{
1229 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)pvUser1;
1230 PCRTDBGMODMACHOARGS pArgs = (PCRTDBGMODMACHOARGS)pvUser2;
1231 RT_NOREF_PV(hDbgCfg);
1232
1233 Assert(!pDbgMod->pDbgVt);
1234 Assert(!pDbgMod->pvDbgPriv);
1235 Assert(!pDbgMod->pszDbgFile);
1236 Assert(!pDbgMod->pImgVt);
1237 Assert(!pDbgMod->pvDbgPriv);
1238 Assert(pDbgMod->pszImgFile);
1239 Assert(pDbgMod->pszImgFileSpecified);
1240
1241 const char *pszImgFileOrg = pDbgMod->pszImgFile;
1242 pDbgMod->pszImgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
1243 if (!pDbgMod->pszImgFile)
1244 return VERR_NO_STR_MEMORY;
1245 RTStrCacheRetain(pDbgMod->pszImgFile);
1246 pDbgMod->pszDbgFile = pDbgMod->pszImgFile;
1247
1248 /*
1249 * Try image interpreters as the dwarf file inside the dSYM bundle is a
1250 * Mach-O file with dwarf debug sections insides it and no code or data.
1251 */
1252 int rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
1253 if (RT_SUCCESS(rc))
1254 {
1255 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
1256 PRTDBGMODREGIMG pImg;
1257 for (pImg = g_pImgHead; pImg; pImg = pImg->pNext)
1258 {
1259 pDbgMod->pImgVt = pImg->pVt;
1260 pDbgMod->pvImgPriv = NULL;
1261 int rc2 = pImg->pVt->pfnTryOpen(pDbgMod, pArgs->enmArch);
1262 if (RT_SUCCESS(rc2))
1263 {
1264 rc = rc2;
1265 break;
1266 }
1267 pDbgMod->pImgVt = NULL;
1268 Assert(pDbgMod->pvImgPriv == NULL);
1269 }
1270
1271 if (RT_SUCCESS(rc))
1272 {
1273 /*
1274 * Check the UUID if one was given.
1275 */
1276 if (pArgs->pUuid)
1277 {
1278 RTUUID UuidOpened;
1279 rc = pDbgMod->pImgVt->pfnQueryProp(pDbgMod, RTLDRPROP_UUID, &UuidOpened, sizeof(UuidOpened), NULL);
1280 if (RT_SUCCESS(rc))
1281 {
1282 if (RTUuidCompare(&UuidOpened, pArgs->pUuid) != 0)
1283 rc = VERR_DBG_FILE_MISMATCH;
1284 }
1285 else if (rc == VERR_NOT_FOUND || rc == VERR_NOT_IMPLEMENTED)
1286 rc = VERR_DBG_FILE_MISMATCH;
1287 }
1288 if (RT_SUCCESS(rc))
1289 {
1290 /*
1291 * Pass it to the DWARF reader(s). Careful to restrict this or
1292 * the dbghelp wrapper may end up being overly helpful.
1293 */
1294 for (PRTDBGMODREGDBG pDbg = g_pDbgHead; pDbg; pDbg = pDbg->pNext)
1295 {
1296 if (pDbg->pVt->fSupports & (RT_DBGTYPE_DWARF | RT_DBGTYPE_STABS | RT_DBGTYPE_WATCOM))
1297
1298 {
1299 pDbgMod->pDbgVt = pDbg->pVt;
1300 pDbgMod->pvDbgPriv = NULL;
1301 rc = pDbg->pVt->pfnTryOpen(pDbgMod, pDbgMod->pImgVt->pfnGetArch(pDbgMod));
1302 if (RT_SUCCESS(rc))
1303 {
1304 /*
1305 * Got it!
1306 */
1307 ASMAtomicIncU32(&pDbg->cUsers);
1308 RTSemRWReleaseRead(g_hDbgModRWSem);
1309 RTStrCacheRelease(g_hDbgModStrCache, pszImgFileOrg);
1310 return VINF_CALLBACK_RETURN;
1311 }
1312 pDbgMod->pDbgVt = NULL;
1313 Assert(pDbgMod->pvDbgPriv == NULL);
1314 }
1315 }
1316
1317 /*
1318 * Likely fallback for when opening image.
1319 */
1320 if (pArgs->fOpenImage)
1321 {
1322 rc = rtDbgModCreateForExports(pDbgMod);
1323 if (RT_SUCCESS(rc))
1324 {
1325 /*
1326 * Done.
1327 */
1328 RTSemRWReleaseRead(g_hDbgModRWSem);
1329 RTStrCacheRelease(g_hDbgModStrCache, pszImgFileOrg);
1330 return VINF_CALLBACK_RETURN;
1331 }
1332 }
1333 }
1334
1335 pDbgMod->pImgVt->pfnClose(pDbgMod);
1336 pDbgMod->pImgVt = NULL;
1337 pDbgMod->pvImgPriv = NULL;
1338 }
1339 }
1340
1341 /* No joy. */
1342 RTSemRWReleaseRead(g_hDbgModRWSem);
1343 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
1344 pDbgMod->pszImgFile = pszImgFileOrg;
1345 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszDbgFile);
1346 pDbgMod->pszDbgFile = NULL;
1347 return rc;
1348}
1349
1350
1351static int rtDbgModFromMachOImageWorker(PRTDBGMODINT pDbgMod, RTLDRARCH enmArch, uint32_t cbImage,
1352 uint32_t cSegs, PCRTDBGSEGMENT paSegs, PCRTUUID pUuid, RTDBGCFG hDbgCfg)
1353{
1354 RT_NOREF_PV(cbImage); RT_NOREF_PV(cSegs); RT_NOREF_PV(paSegs);
1355
1356 RTDBGMODMACHOARGS Args;
1357 Args.enmArch = enmArch;
1358 Args.pUuid = pUuid && RTUuidIsNull(pUuid) ? pUuid : NULL;
1359 Args.fOpenImage = false;
1360
1361 /*
1362 * Search for the .dSYM bundle first, since that's generally all we need.
1363 */
1364 int rc = RTDbgCfgOpenDsymBundle(hDbgCfg, pDbgMod->pszImgFile, pUuid,
1365 rtDbgModFromMachOImageOpenDsymMachOCallback, pDbgMod, &Args);
1366 if (RT_FAILURE(rc))
1367 {
1368 /*
1369 * If we cannot get at the .dSYM, try the executable image.
1370 */
1371 Args.fOpenImage = true;
1372 rc = RTDbgCfgOpenMachOImage(hDbgCfg, pDbgMod->pszImgFile, pUuid,
1373 rtDbgModFromMachOImageOpenDsymMachOCallback, pDbgMod, &Args);
1374 }
1375 return rc;
1376}
1377
1378
1379/** @callback_method_impl{FNRTDBGMODDEFERRED} */
1380static DECLCALLBACK(int) rtDbgModFromMachOImageDeferredCallback(PRTDBGMODINT pDbgMod, PRTDBGMODDEFERRED pDeferred)
1381{
1382 return rtDbgModFromMachOImageWorker(pDbgMod, pDeferred->u.MachO.enmArch, pDeferred->cbImage,
1383 pDeferred->u.MachO.cSegs, pDeferred->u.MachO.aSegs,
1384 &pDeferred->u.MachO.Uuid, pDeferred->hDbgCfg);
1385}
1386
1387
1388RTDECL(int) RTDbgModCreateFromMachOImage(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName,
1389 RTLDRARCH enmArch, uint32_t cbImage, uint32_t cSegs, PCRTDBGSEGMENT paSegs,
1390 PCRTUUID pUuid, RTDBGCFG hDbgCfg, uint32_t fFlags)
1391{
1392 /*
1393 * Input validation and lazy initialization.
1394 */
1395 AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
1396 *phDbgMod = NIL_RTDBGMOD;
1397 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1398 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
1399 if (!pszName)
1400 pszName = RTPathFilenameEx(pszFilename, RTPATH_STR_F_STYLE_HOST);
1401 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1402 if (cSegs)
1403 {
1404 AssertReturn(cSegs < 1024, VERR_INVALID_PARAMETER);
1405 AssertPtrReturn(paSegs, VERR_INVALID_POINTER);
1406 AssertReturn(!cbImage, VERR_INVALID_PARAMETER);
1407 }
1408 AssertReturn(cbImage || cSegs, VERR_INVALID_PARAMETER);
1409 AssertPtrNullReturn(pUuid, VERR_INVALID_POINTER);
1410 AssertReturn(!(fFlags & ~(RTDBGMOD_F_NOT_DEFERRED)), VERR_INVALID_PARAMETER);
1411
1412 int rc = rtDbgModLazyInit();
1413 if (RT_FAILURE(rc))
1414 return rc;
1415
1416 uint64_t fDbgCfg = 0;
1417 if (hDbgCfg)
1418 {
1419 rc = RTDbgCfgQueryUInt(hDbgCfg, RTDBGCFGPROP_FLAGS, &fDbgCfg);
1420 AssertRCReturn(rc, rc);
1421 }
1422
1423 /*
1424 * Allocate a new module instance.
1425 */
1426 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
1427 if (!pDbgMod)
1428 return VERR_NO_MEMORY;
1429 pDbgMod->u32Magic = RTDBGMOD_MAGIC;
1430 pDbgMod->cRefs = 1;
1431 rc = RTCritSectInit(&pDbgMod->CritSect);
1432 if (RT_SUCCESS(rc))
1433 {
1434 pDbgMod->pszName = RTStrCacheEnterLower(g_hDbgModStrCache, pszName);
1435 if (pDbgMod->pszName)
1436 {
1437 pDbgMod->pszImgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
1438 if (pDbgMod->pszImgFile)
1439 {
1440 RTStrCacheRetain(pDbgMod->pszImgFile);
1441 pDbgMod->pszImgFileSpecified = pDbgMod->pszImgFile;
1442
1443 /*
1444 * Load it immediately?
1445 */
1446 if ( !(fDbgCfg & RTDBGCFG_FLAGS_DEFERRED)
1447 || cSegs /* for the time being. */
1448 || (!cbImage && !cSegs)
1449 || (fFlags & RTDBGMOD_F_NOT_DEFERRED) )
1450 rc = rtDbgModFromMachOImageWorker(pDbgMod, enmArch, cbImage, cSegs, paSegs, pUuid, hDbgCfg);
1451 else
1452 {
1453 /*
1454 * Procrastinate. Need image size atm.
1455 */
1456 PRTDBGMODDEFERRED pDeferred;
1457 rc = rtDbgModDeferredCreate(pDbgMod, rtDbgModFromMachOImageDeferredCallback, cbImage, hDbgCfg,
1458 RT_UOFFSETOF_DYN(RTDBGMODDEFERRED, u.MachO.aSegs[cSegs]),
1459 &pDeferred);
1460 if (RT_SUCCESS(rc))
1461 {
1462 pDeferred->u.MachO.Uuid = *pUuid;
1463 pDeferred->u.MachO.enmArch = enmArch;
1464 pDeferred->u.MachO.cSegs = cSegs;
1465 if (cSegs)
1466 memcpy(&pDeferred->u.MachO.aSegs, paSegs, cSegs * sizeof(paSegs[0]));
1467 }
1468 }
1469 if (RT_SUCCESS(rc))
1470 {
1471 *phDbgMod = pDbgMod;
1472 return VINF_SUCCESS;
1473 }
1474
1475 /* Failed, bail out. */
1476 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
1477 }
1478 else
1479 rc = VERR_NO_STR_MEMORY;
1480 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFileSpecified);
1481 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
1482 }
1483 else
1484 rc = VERR_NO_STR_MEMORY;
1485 RTCritSectDelete(&pDbgMod->CritSect);
1486 }
1487
1488 RTMemFree(pDbgMod);
1489 return rc;
1490}
1491
1492
1493
1494RT_EXPORT_SYMBOL(RTDbgModCreateFromMachOImage);
1495
1496
1497
1498/**
1499 * Destroys an module after the reference count has reached zero.
1500 *
1501 * @param pDbgMod The module instance.
1502 */
1503static void rtDbgModDestroy(PRTDBGMODINT pDbgMod)
1504{
1505 /*
1506 * Close the debug info interpreter first, then the image interpret.
1507 */
1508 RTCritSectEnter(&pDbgMod->CritSect); /* paranoia */
1509
1510 if (pDbgMod->pDbgVt)
1511 {
1512 pDbgMod->pDbgVt->pfnClose(pDbgMod);
1513 pDbgMod->pDbgVt = NULL;
1514 pDbgMod->pvDbgPriv = NULL;
1515 }
1516
1517 if (pDbgMod->pImgVt)
1518 {
1519 pDbgMod->pImgVt->pfnClose(pDbgMod);
1520 pDbgMod->pImgVt = NULL;
1521 pDbgMod->pvImgPriv = NULL;
1522 }
1523
1524 /*
1525 * Free the resources.
1526 */
1527 ASMAtomicWriteU32(&pDbgMod->u32Magic, ~RTDBGMOD_MAGIC);
1528 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
1529 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
1530 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFileSpecified);
1531 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszDbgFile);
1532 RTCritSectLeave(&pDbgMod->CritSect); /* paranoia */
1533 RTCritSectDelete(&pDbgMod->CritSect);
1534 RTMemFree(pDbgMod);
1535}
1536
1537
1538RTDECL(uint32_t) RTDbgModRetain(RTDBGMOD hDbgMod)
1539{
1540 PRTDBGMODINT pDbgMod = hDbgMod;
1541 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
1542 return ASMAtomicIncU32(&pDbgMod->cRefs);
1543}
1544RT_EXPORT_SYMBOL(RTDbgModRetain);
1545
1546
1547RTDECL(uint32_t) RTDbgModRelease(RTDBGMOD hDbgMod)
1548{
1549 if (hDbgMod == NIL_RTDBGMOD)
1550 return 0;
1551 PRTDBGMODINT pDbgMod = hDbgMod;
1552 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
1553
1554 uint32_t cRefs = ASMAtomicDecU32(&pDbgMod->cRefs);
1555 if (!cRefs)
1556 rtDbgModDestroy(pDbgMod);
1557 return cRefs;
1558}
1559RT_EXPORT_SYMBOL(RTDbgModRelease);
1560
1561
1562RTDECL(const char *) RTDbgModName(RTDBGMOD hDbgMod)
1563{
1564 PRTDBGMODINT pDbgMod = hDbgMod;
1565 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NULL);
1566 return pDbgMod->pszName;
1567}
1568RT_EXPORT_SYMBOL(RTDbgModName);
1569
1570
1571RTDECL(const char *) RTDbgModDebugFile(RTDBGMOD hDbgMod)
1572{
1573 PRTDBGMODINT pDbgMod = hDbgMod;
1574 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NULL);
1575 if (pDbgMod->fDeferred || pDbgMod->fExports)
1576 return NULL;
1577 return pDbgMod->pszDbgFile;
1578}
1579RT_EXPORT_SYMBOL(RTDbgModDebugFile);
1580
1581
1582RTDECL(const char *) RTDbgModImageFile(RTDBGMOD hDbgMod)
1583{
1584 PRTDBGMODINT pDbgMod = hDbgMod;
1585 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NULL);
1586 return pDbgMod->pszImgFileSpecified;
1587}
1588RT_EXPORT_SYMBOL(RTDbgModImageFile);
1589
1590
1591RTDECL(const char *) RTDbgModImageFileUsed(RTDBGMOD hDbgMod)
1592{
1593 PRTDBGMODINT pDbgMod = hDbgMod;
1594 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NULL);
1595 return pDbgMod->pszImgFile == pDbgMod->pszImgFileSpecified ? NULL : pDbgMod->pszImgFile;
1596}
1597RT_EXPORT_SYMBOL(RTDbgModImageFileUsed);
1598
1599
1600RTDECL(bool) RTDbgModIsDeferred(RTDBGMOD hDbgMod)
1601{
1602 PRTDBGMODINT pDbgMod = hDbgMod;
1603 RTDBGMOD_VALID_RETURN_RC(pDbgMod, false);
1604 return pDbgMod->fDeferred;
1605}
1606
1607
1608RTDECL(bool) RTDbgModIsExports(RTDBGMOD hDbgMod)
1609{
1610 PRTDBGMODINT pDbgMod = hDbgMod;
1611 RTDBGMOD_VALID_RETURN_RC(pDbgMod, false);
1612 return pDbgMod->fExports;
1613}
1614
1615
1616RTDECL(int) RTDbgModRemoveAll(RTDBGMOD hDbgMod, bool fLeaveSegments)
1617{
1618 PRTDBGMODINT pDbgMod = hDbgMod;
1619 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1620
1621 RTDBGMOD_LOCK(pDbgMod);
1622
1623 /* Only possible on container modules. */
1624 int rc = VINF_SUCCESS;
1625 if (pDbgMod->pDbgVt != &g_rtDbgModVtDbgContainer)
1626 {
1627 if (fLeaveSegments)
1628 {
1629 rc = rtDbgModContainer_LineRemoveAll(pDbgMod);
1630 if (RT_SUCCESS(rc))
1631 rc = rtDbgModContainer_SymbolRemoveAll(pDbgMod);
1632 }
1633 else
1634 rc = rtDbgModContainer_RemoveAll(pDbgMod);
1635 }
1636 else
1637 rc = VERR_ACCESS_DENIED;
1638
1639 RTDBGMOD_UNLOCK(pDbgMod);
1640 return rc;
1641}
1642
1643
1644RTDECL(RTDBGSEGIDX) RTDbgModRvaToSegOff(RTDBGMOD hDbgMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
1645{
1646 PRTDBGMODINT pDbgMod = hDbgMod;
1647 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NIL_RTDBGSEGIDX);
1648 RTDBGMOD_LOCK(pDbgMod);
1649
1650 RTDBGSEGIDX iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, uRva, poffSeg);
1651
1652 RTDBGMOD_UNLOCK(pDbgMod);
1653 return iSeg;
1654}
1655RT_EXPORT_SYMBOL(RTDbgModRvaToSegOff);
1656
1657
1658RTDECL(uint64_t) RTDbgModGetTag(RTDBGMOD hDbgMod)
1659{
1660 PRTDBGMODINT pDbgMod = hDbgMod;
1661 RTDBGMOD_VALID_RETURN_RC(pDbgMod, 0);
1662 return pDbgMod->uTag;
1663}
1664RT_EXPORT_SYMBOL(RTDbgModGetTag);
1665
1666
1667RTDECL(int) RTDbgModSetTag(RTDBGMOD hDbgMod, uint64_t uTag)
1668{
1669 PRTDBGMODINT pDbgMod = hDbgMod;
1670 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1671 RTDBGMOD_LOCK(pDbgMod);
1672
1673 pDbgMod->uTag = uTag;
1674
1675 RTDBGMOD_UNLOCK(pDbgMod);
1676 return VINF_SUCCESS;
1677}
1678RT_EXPORT_SYMBOL(RTDbgModSetTag);
1679
1680
1681RTDECL(RTUINTPTR) RTDbgModImageSize(RTDBGMOD hDbgMod)
1682{
1683 PRTDBGMODINT pDbgMod = hDbgMod;
1684 RTDBGMOD_VALID_RETURN_RC(pDbgMod, RTUINTPTR_MAX);
1685 RTDBGMOD_LOCK(pDbgMod);
1686
1687 RTUINTPTR cbImage = pDbgMod->pDbgVt->pfnImageSize(pDbgMod);
1688
1689 RTDBGMOD_UNLOCK(pDbgMod);
1690 return cbImage;
1691}
1692RT_EXPORT_SYMBOL(RTDbgModImageSize);
1693
1694
1695RTDECL(RTLDRFMT) RTDbgModImageGetFormat(RTDBGMOD hDbgMod)
1696{
1697 PRTDBGMODINT pDbgMod = hDbgMod;
1698 RTDBGMOD_VALID_RETURN_RC(pDbgMod, RTLDRFMT_INVALID);
1699 RTDBGMOD_LOCK(pDbgMod);
1700
1701 RTLDRFMT enmFmt;
1702 if ( pDbgMod->pImgVt
1703 && pDbgMod->pImgVt->pfnGetFormat)
1704 enmFmt = pDbgMod->pImgVt->pfnGetFormat(pDbgMod);
1705 else
1706 enmFmt = RTLDRFMT_INVALID;
1707
1708 RTDBGMOD_UNLOCK(pDbgMod);
1709 return enmFmt;
1710}
1711RT_EXPORT_SYMBOL(RTDbgModImageGetFormat);
1712
1713
1714RTDECL(RTLDRARCH) RTDbgModImageGetArch(RTDBGMOD hDbgMod)
1715{
1716 PRTDBGMODINT pDbgMod = hDbgMod;
1717 RTDBGMOD_VALID_RETURN_RC(pDbgMod, RTLDRARCH_INVALID);
1718 RTDBGMOD_LOCK(pDbgMod);
1719
1720 RTLDRARCH enmArch;
1721 if ( pDbgMod->pImgVt
1722 && pDbgMod->pImgVt->pfnGetArch)
1723 enmArch = pDbgMod->pImgVt->pfnGetArch(pDbgMod);
1724 else
1725 enmArch = RTLDRARCH_WHATEVER;
1726
1727 RTDBGMOD_UNLOCK(pDbgMod);
1728 return enmArch;
1729}
1730RT_EXPORT_SYMBOL(RTDbgModImageGetArch);
1731
1732
1733RTDECL(int) RTDbgModImageQueryProp(RTDBGMOD hDbgMod, RTLDRPROP enmProp, void *pvBuf, size_t cbBuf, size_t *pcbRet)
1734{
1735 PRTDBGMODINT pDbgMod = hDbgMod;
1736 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1737 AssertPtrNullReturn(pcbRet, VERR_INVALID_POINTER);
1738 RTDBGMOD_LOCK(pDbgMod);
1739
1740 int rc;
1741 if ( pDbgMod->pImgVt
1742 && pDbgMod->pImgVt->pfnQueryProp)
1743 rc = pDbgMod->pImgVt->pfnQueryProp(pDbgMod, enmProp, pvBuf, cbBuf, pcbRet);
1744 else
1745 rc = VERR_NOT_FOUND;
1746
1747 RTDBGMOD_UNLOCK(pDbgMod);
1748 return rc;
1749}
1750RT_EXPORT_SYMBOL(RTDbgModImageQueryProp);
1751
1752
1753RTDECL(int) RTDbgModSegmentAdd(RTDBGMOD hDbgMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName,
1754 uint32_t fFlags, PRTDBGSEGIDX piSeg)
1755{
1756 /*
1757 * Validate input.
1758 */
1759 PRTDBGMODINT pDbgMod = hDbgMod;
1760 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1761 AssertMsgReturn(uRva + cb >= uRva, ("uRva=%RTptr cb=%RTptr\n", uRva, cb), VERR_DBG_ADDRESS_WRAP);
1762 Assert(*pszName);
1763 size_t cchName = strlen(pszName);
1764 AssertReturn(cchName > 0, VERR_DBG_SEGMENT_NAME_OUT_OF_RANGE);
1765 AssertReturn(cchName < RTDBG_SEGMENT_NAME_LENGTH, VERR_DBG_SEGMENT_NAME_OUT_OF_RANGE);
1766 AssertMsgReturn(!fFlags, ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
1767 AssertPtrNull(piSeg);
1768 AssertMsgReturn(!piSeg || *piSeg == NIL_RTDBGSEGIDX || *piSeg <= RTDBGSEGIDX_LAST, ("%#x\n", *piSeg), VERR_DBG_SPECIAL_SEGMENT);
1769
1770 /*
1771 * Do the deed.
1772 */
1773 RTDBGMOD_LOCK(pDbgMod);
1774 int rc = pDbgMod->pDbgVt->pfnSegmentAdd(pDbgMod, uRva, cb, pszName, cchName, fFlags, piSeg);
1775 RTDBGMOD_UNLOCK(pDbgMod);
1776
1777 return rc;
1778
1779}
1780RT_EXPORT_SYMBOL(RTDbgModSegmentAdd);
1781
1782
1783RTDECL(RTDBGSEGIDX) RTDbgModSegmentCount(RTDBGMOD hDbgMod)
1784{
1785 PRTDBGMODINT pDbgMod = hDbgMod;
1786 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NIL_RTDBGSEGIDX);
1787 RTDBGMOD_LOCK(pDbgMod);
1788
1789 RTDBGSEGIDX cSegs = pDbgMod->pDbgVt->pfnSegmentCount(pDbgMod);
1790
1791 RTDBGMOD_UNLOCK(pDbgMod);
1792 return cSegs;
1793}
1794RT_EXPORT_SYMBOL(RTDbgModSegmentCount);
1795
1796
1797RTDECL(int) RTDbgModSegmentByIndex(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
1798{
1799 AssertMsgReturn(iSeg <= RTDBGSEGIDX_LAST, ("%#x\n", iSeg), VERR_DBG_SPECIAL_SEGMENT);
1800 PRTDBGMODINT pDbgMod = hDbgMod;
1801 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1802 RTDBGMOD_LOCK(pDbgMod);
1803
1804 int rc = pDbgMod->pDbgVt->pfnSegmentByIndex(pDbgMod, iSeg, pSegInfo);
1805
1806 RTDBGMOD_UNLOCK(pDbgMod);
1807 return rc;
1808}
1809RT_EXPORT_SYMBOL(RTDbgModSegmentByIndex);
1810
1811
1812RTDECL(RTUINTPTR) RTDbgModSegmentSize(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg)
1813{
1814 if (iSeg == RTDBGSEGIDX_RVA)
1815 return RTDbgModImageSize(hDbgMod);
1816 RTDBGSEGMENT SegInfo;
1817 int rc = RTDbgModSegmentByIndex(hDbgMod, iSeg, &SegInfo);
1818 return RT_SUCCESS(rc) ? SegInfo.cb : RTUINTPTR_MAX;
1819}
1820RT_EXPORT_SYMBOL(RTDbgModSegmentSize);
1821
1822
1823RTDECL(RTUINTPTR) RTDbgModSegmentRva(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg)
1824{
1825 RTDBGSEGMENT SegInfo;
1826 int rc = RTDbgModSegmentByIndex(hDbgMod, iSeg, &SegInfo);
1827 return RT_SUCCESS(rc) ? SegInfo.uRva : RTUINTPTR_MAX;
1828}
1829RT_EXPORT_SYMBOL(RTDbgModSegmentRva);
1830
1831
1832RTDECL(int) RTDbgModSymbolAdd(RTDBGMOD hDbgMod, const char *pszSymbol, RTDBGSEGIDX iSeg, RTUINTPTR off,
1833 RTUINTPTR cb, uint32_t fFlags, uint32_t *piOrdinal)
1834{
1835 /*
1836 * Validate input.
1837 */
1838 PRTDBGMODINT pDbgMod = hDbgMod;
1839 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1840 AssertPtrReturn(pszSymbol, VERR_INVALID_POINTER);
1841 size_t cchSymbol = strlen(pszSymbol);
1842 AssertReturn(cchSymbol, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
1843 AssertReturn(cchSymbol < RTDBG_SYMBOL_NAME_LENGTH, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
1844 AssertMsgReturn( iSeg <= RTDBGSEGIDX_LAST
1845 || ( iSeg >= RTDBGSEGIDX_SPECIAL_FIRST
1846 && iSeg <= RTDBGSEGIDX_SPECIAL_LAST),
1847 ("%#x\n", iSeg),
1848 VERR_DBG_INVALID_SEGMENT_INDEX);
1849 AssertMsgReturn(off + cb >= off, ("off=%RTptr cb=%RTptr\n", off, cb), VERR_DBG_ADDRESS_WRAP);
1850 AssertReturn(!(fFlags & ~RTDBGSYMBOLADD_F_VALID_MASK), VERR_INVALID_FLAGS);
1851
1852 RTDBGMOD_LOCK(pDbgMod);
1853
1854 /*
1855 * Convert RVAs.
1856 */
1857 if (iSeg == RTDBGSEGIDX_RVA)
1858 {
1859 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
1860 if (iSeg == NIL_RTDBGSEGIDX)
1861 {
1862 RTDBGMOD_UNLOCK(pDbgMod);
1863 return VERR_DBG_INVALID_RVA;
1864 }
1865 }
1866
1867 /*
1868 * Get down to business.
1869 */
1870 int rc = pDbgMod->pDbgVt->pfnSymbolAdd(pDbgMod, pszSymbol, cchSymbol, iSeg, off, cb, fFlags, piOrdinal);
1871
1872 RTDBGMOD_UNLOCK(pDbgMod);
1873 return rc;
1874}
1875RT_EXPORT_SYMBOL(RTDbgModSymbolAdd);
1876
1877
1878RTDECL(uint32_t) RTDbgModSymbolCount(RTDBGMOD hDbgMod)
1879{
1880 PRTDBGMODINT pDbgMod = hDbgMod;
1881 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
1882 RTDBGMOD_LOCK(pDbgMod);
1883
1884 uint32_t cSymbols = pDbgMod->pDbgVt->pfnSymbolCount(pDbgMod);
1885
1886 RTDBGMOD_UNLOCK(pDbgMod);
1887 return cSymbols;
1888}
1889RT_EXPORT_SYMBOL(RTDbgModSymbolCount);
1890
1891
1892RTDECL(int) RTDbgModSymbolByOrdinal(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
1893{
1894 PRTDBGMODINT pDbgMod = hDbgMod;
1895 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1896 RTDBGMOD_LOCK(pDbgMod);
1897
1898 int rc = pDbgMod->pDbgVt->pfnSymbolByOrdinal(pDbgMod, iOrdinal, pSymInfo);
1899
1900 RTDBGMOD_UNLOCK(pDbgMod);
1901 return rc;
1902}
1903RT_EXPORT_SYMBOL(RTDbgModSymbolByOrdinal);
1904
1905
1906RTDECL(int) RTDbgModSymbolByOrdinalA(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGSYMBOL *ppSymInfo)
1907{
1908 AssertPtr(ppSymInfo);
1909 *ppSymInfo = NULL;
1910
1911 PRTDBGSYMBOL pSymInfo = RTDbgSymbolAlloc();
1912 if (!pSymInfo)
1913 return VERR_NO_MEMORY;
1914
1915 int rc = RTDbgModSymbolByOrdinal(hDbgMod, iOrdinal, pSymInfo);
1916
1917 if (RT_SUCCESS(rc))
1918 *ppSymInfo = pSymInfo;
1919 else
1920 RTDbgSymbolFree(pSymInfo);
1921 return rc;
1922}
1923RT_EXPORT_SYMBOL(RTDbgModSymbolByOrdinalA);
1924
1925
1926/**
1927 * Return a segment number/name as symbol if we couldn't find any
1928 * valid symbols within the segment.
1929 */
1930DECL_NO_INLINE(static, int)
1931rtDbgModSymbolByAddrTrySegments(PRTDBGMODINT pDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
1932 PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
1933{
1934 Assert(iSeg <= RTDBGSEGIDX_LAST);
1935 RTDBGSEGMENT SegInfo;
1936 int rc = pDbgMod->pDbgVt->pfnSegmentByIndex(pDbgMod, iSeg, &SegInfo);
1937 if (RT_SUCCESS(rc))
1938 {
1939 pSymInfo->Value = 0;
1940 pSymInfo->cb = SegInfo.cb;
1941 pSymInfo->offSeg = 0;
1942 pSymInfo->iSeg = iSeg;
1943 pSymInfo->fFlags = 0;
1944 if (SegInfo.szName[0])
1945 RTStrPrintf(pSymInfo->szName, sizeof(pSymInfo->szName), "start_seg%u_%s", SegInfo.iSeg, SegInfo.szName);
1946 else
1947 RTStrPrintf(pSymInfo->szName, sizeof(pSymInfo->szName), "start_seg%u", SegInfo.iSeg);
1948 if (poffDisp)
1949 *poffDisp = off;
1950 return VINF_SUCCESS;
1951 }
1952 return VERR_SYMBOL_NOT_FOUND;
1953}
1954
1955
1956RTDECL(int) RTDbgModSymbolByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
1957 PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
1958{
1959 /*
1960 * Validate input.
1961 */
1962 PRTDBGMODINT pDbgMod = hDbgMod;
1963 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1964 AssertPtrNull(poffDisp);
1965 AssertPtr(pSymInfo);
1966 AssertReturn(!(fFlags & ~RTDBGSYMADDR_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
1967
1968 RTDBGMOD_LOCK(pDbgMod);
1969
1970 /*
1971 * Convert RVAs.
1972 */
1973 if (iSeg == RTDBGSEGIDX_RVA)
1974 {
1975 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
1976 if (iSeg == NIL_RTDBGSEGIDX)
1977 {
1978 RTDBGMOD_UNLOCK(pDbgMod);
1979 return VERR_DBG_INVALID_RVA;
1980 }
1981 }
1982
1983 /*
1984 * Get down to business.
1985 */
1986 int rc = pDbgMod->pDbgVt->pfnSymbolByAddr(pDbgMod, iSeg, off, fFlags, poffDisp, pSymInfo);
1987
1988 /* If we failed to locate a symbol, try use the specified segment as a reference. */
1989 if ( rc == VERR_SYMBOL_NOT_FOUND
1990 && iSeg <= RTDBGSEGIDX_LAST
1991 && !(fFlags & RTDBGSYMADDR_FLAGS_GREATER_OR_EQUAL))
1992 rc = rtDbgModSymbolByAddrTrySegments(pDbgMod, iSeg, off, poffDisp, pSymInfo);
1993
1994 RTDBGMOD_UNLOCK(pDbgMod);
1995 return rc;
1996}
1997RT_EXPORT_SYMBOL(RTDbgModSymbolByAddr);
1998
1999
2000RTDECL(int) RTDbgModSymbolByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
2001 PRTINTPTR poffDisp, PRTDBGSYMBOL *ppSymInfo)
2002{
2003 AssertPtr(ppSymInfo);
2004 *ppSymInfo = NULL;
2005
2006 PRTDBGSYMBOL pSymInfo = RTDbgSymbolAlloc();
2007 if (!pSymInfo)
2008 return VERR_NO_MEMORY;
2009
2010 int rc = RTDbgModSymbolByAddr(hDbgMod, iSeg, off, fFlags, poffDisp, pSymInfo);
2011
2012 if (RT_SUCCESS(rc))
2013 *ppSymInfo = pSymInfo;
2014 else
2015 RTDbgSymbolFree(pSymInfo);
2016 return rc;
2017}
2018RT_EXPORT_SYMBOL(RTDbgModSymbolByAddrA);
2019
2020
2021RTDECL(int) RTDbgModSymbolByName(RTDBGMOD hDbgMod, const char *pszSymbol, PRTDBGSYMBOL pSymInfo)
2022{
2023 /*
2024 * Validate input.
2025 */
2026 PRTDBGMODINT pDbgMod = hDbgMod;
2027 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
2028 AssertPtr(pszSymbol);
2029 size_t cchSymbol = strlen(pszSymbol);
2030 AssertReturn(cchSymbol, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
2031 AssertReturn(cchSymbol < RTDBG_SYMBOL_NAME_LENGTH, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
2032 AssertPtr(pSymInfo);
2033
2034 /*
2035 * Make the query.
2036 */
2037 RTDBGMOD_LOCK(pDbgMod);
2038 int rc = pDbgMod->pDbgVt->pfnSymbolByName(pDbgMod, pszSymbol, cchSymbol, pSymInfo);
2039 RTDBGMOD_UNLOCK(pDbgMod);
2040
2041 return rc;
2042}
2043RT_EXPORT_SYMBOL(RTDbgModSymbolByName);
2044
2045
2046RTDECL(int) RTDbgModSymbolByNameA(RTDBGMOD hDbgMod, const char *pszSymbol, PRTDBGSYMBOL *ppSymInfo)
2047{
2048 AssertPtr(ppSymInfo);
2049 *ppSymInfo = NULL;
2050
2051 PRTDBGSYMBOL pSymInfo = RTDbgSymbolAlloc();
2052 if (!pSymInfo)
2053 return VERR_NO_MEMORY;
2054
2055 int rc = RTDbgModSymbolByName(hDbgMod, pszSymbol, pSymInfo);
2056
2057 if (RT_SUCCESS(rc))
2058 *ppSymInfo = pSymInfo;
2059 else
2060 RTDbgSymbolFree(pSymInfo);
2061 return rc;
2062}
2063RT_EXPORT_SYMBOL(RTDbgModSymbolByNameA);
2064
2065
2066RTDECL(int) RTDbgModLineAdd(RTDBGMOD hDbgMod, const char *pszFile, uint32_t uLineNo,
2067 RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t *piOrdinal)
2068{
2069 /*
2070 * Validate input.
2071 */
2072 PRTDBGMODINT pDbgMod = hDbgMod;
2073 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
2074 AssertPtr(pszFile);
2075 size_t cchFile = strlen(pszFile);
2076 AssertReturn(cchFile, VERR_DBG_FILE_NAME_OUT_OF_RANGE);
2077 AssertReturn(cchFile < RTDBG_FILE_NAME_LENGTH, VERR_DBG_FILE_NAME_OUT_OF_RANGE);
2078 AssertMsgReturn( iSeg <= RTDBGSEGIDX_LAST
2079 || iSeg == RTDBGSEGIDX_RVA,
2080 ("%#x\n", iSeg),
2081 VERR_DBG_INVALID_SEGMENT_INDEX);
2082 AssertReturn(uLineNo > 0 && uLineNo < UINT32_MAX, VERR_INVALID_PARAMETER);
2083
2084 RTDBGMOD_LOCK(pDbgMod);
2085
2086 /*
2087 * Convert RVAs.
2088 */
2089 if (iSeg == RTDBGSEGIDX_RVA)
2090 {
2091 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
2092 if (iSeg == NIL_RTDBGSEGIDX)
2093 {
2094 RTDBGMOD_UNLOCK(pDbgMod);
2095 return VERR_DBG_INVALID_RVA;
2096 }
2097 }
2098
2099 /*
2100 * Get down to business.
2101 */
2102 int rc = pDbgMod->pDbgVt->pfnLineAdd(pDbgMod, pszFile, cchFile, uLineNo, iSeg, off, piOrdinal);
2103
2104 RTDBGMOD_UNLOCK(pDbgMod);
2105 return rc;
2106}
2107RT_EXPORT_SYMBOL(RTDbgModLineAdd);
2108
2109
2110RTDECL(uint32_t) RTDbgModLineCount(RTDBGMOD hDbgMod)
2111{
2112 PRTDBGMODINT pDbgMod = hDbgMod;
2113 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
2114 RTDBGMOD_LOCK(pDbgMod);
2115
2116 uint32_t cLineNumbers = pDbgMod->pDbgVt->pfnLineCount(pDbgMod);
2117
2118 RTDBGMOD_UNLOCK(pDbgMod);
2119 return cLineNumbers;
2120}
2121RT_EXPORT_SYMBOL(RTDbgModLineCount);
2122
2123
2124RTDECL(int) RTDbgModLineByOrdinal(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
2125{
2126 PRTDBGMODINT pDbgMod = hDbgMod;
2127 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
2128 RTDBGMOD_LOCK(pDbgMod);
2129
2130 int rc = pDbgMod->pDbgVt->pfnLineByOrdinal(pDbgMod, iOrdinal, pLineInfo);
2131
2132 RTDBGMOD_UNLOCK(pDbgMod);
2133 return rc;
2134}
2135RT_EXPORT_SYMBOL(RTDbgModLineByOrdinal);
2136
2137
2138RTDECL(int) RTDbgModLineByOrdinalA(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGLINE *ppLineInfo)
2139{
2140 AssertPtr(ppLineInfo);
2141 *ppLineInfo = NULL;
2142
2143 PRTDBGLINE pLineInfo = RTDbgLineAlloc();
2144 if (!pLineInfo)
2145 return VERR_NO_MEMORY;
2146
2147 int rc = RTDbgModLineByOrdinal(hDbgMod, iOrdinal, pLineInfo);
2148
2149 if (RT_SUCCESS(rc))
2150 *ppLineInfo = pLineInfo;
2151 else
2152 RTDbgLineFree(pLineInfo);
2153 return rc;
2154}
2155RT_EXPORT_SYMBOL(RTDbgModLineByOrdinalA);
2156
2157
2158RTDECL(int) RTDbgModLineByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
2159{
2160 /*
2161 * Validate input.
2162 */
2163 PRTDBGMODINT pDbgMod = hDbgMod;
2164 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
2165 AssertPtrNull(poffDisp);
2166 AssertPtr(pLineInfo);
2167
2168 RTDBGMOD_LOCK(pDbgMod);
2169
2170 /*
2171 * Convert RVAs.
2172 */
2173 if (iSeg == RTDBGSEGIDX_RVA)
2174 {
2175 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
2176 if (iSeg == NIL_RTDBGSEGIDX)
2177 {
2178 RTDBGMOD_UNLOCK(pDbgMod);
2179 return VERR_DBG_INVALID_RVA;
2180 }
2181 }
2182
2183 int rc = pDbgMod->pDbgVt->pfnLineByAddr(pDbgMod, iSeg, off, poffDisp, pLineInfo);
2184
2185 RTDBGMOD_UNLOCK(pDbgMod);
2186 return rc;
2187}
2188RT_EXPORT_SYMBOL(RTDbgModLineByAddr);
2189
2190
2191RTDECL(int) RTDbgModLineByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE *ppLineInfo)
2192{
2193 AssertPtr(ppLineInfo);
2194 *ppLineInfo = NULL;
2195
2196 PRTDBGLINE pLineInfo = RTDbgLineAlloc();
2197 if (!pLineInfo)
2198 return VERR_NO_MEMORY;
2199
2200 int rc = RTDbgModLineByAddr(hDbgMod, iSeg, off, poffDisp, pLineInfo);
2201
2202 if (RT_SUCCESS(rc))
2203 *ppLineInfo = pLineInfo;
2204 else
2205 RTDbgLineFree(pLineInfo);
2206 return rc;
2207}
2208RT_EXPORT_SYMBOL(RTDbgModLineByAddrA);
2209
2210
2211RTDECL(int) RTDbgModUnwindFrame(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTDBGUNWINDSTATE pState)
2212{
2213 /*
2214 * Validate input.
2215 */
2216 PRTDBGMODINT pDbgMod = hDbgMod;
2217 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
2218 AssertPtr(pState);
2219 AssertReturn(pState->u32Magic == RTDBGUNWINDSTATE_MAGIC, VERR_INVALID_MAGIC);
2220
2221 RTDBGMOD_LOCK(pDbgMod);
2222
2223 /*
2224 * Convert RVAs.
2225 */
2226 if (iSeg == RTDBGSEGIDX_RVA)
2227 {
2228 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
2229 if (iSeg == NIL_RTDBGSEGIDX)
2230 {
2231 RTDBGMOD_UNLOCK(pDbgMod);
2232 return VERR_DBG_INVALID_RVA;
2233 }
2234 }
2235
2236 /*
2237 * Try the debug module first, then the image.
2238 */
2239 int rc = VERR_DBG_NO_UNWIND_INFO;
2240 if (pDbgMod->pDbgVt->pfnUnwindFrame)
2241 rc = pDbgMod->pDbgVt->pfnUnwindFrame(pDbgMod, iSeg, off, pState);
2242 if ( ( rc == VERR_DBG_NO_UNWIND_INFO
2243 || rc == VERR_DBG_UNWIND_INFO_NOT_FOUND)
2244 && pDbgMod->pImgVt
2245 && pDbgMod->pImgVt->pfnUnwindFrame)
2246 {
2247 if (rc == VERR_DBG_NO_UNWIND_INFO)
2248 rc = pDbgMod->pImgVt->pfnUnwindFrame(pDbgMod, iSeg, off, pState);
2249 else
2250 {
2251 rc = pDbgMod->pImgVt->pfnUnwindFrame(pDbgMod, iSeg, off, pState);
2252 if (rc == VERR_DBG_NO_UNWIND_INFO)
2253 rc = VERR_DBG_UNWIND_INFO_NOT_FOUND;
2254 }
2255 }
2256
2257 RTDBGMOD_UNLOCK(pDbgMod);
2258 return rc;
2259
2260}
2261RT_EXPORT_SYMBOL(RTDbgModUnwindFrame);
2262
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