VirtualBox

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

Last change on this file since 46164 was 46164, checked in by vboxsync, 12 years ago

More exteran .dSYM and .dwo bundles/files changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 55.0 KB
Line 
1/* $Id: dbgmod.cpp 46164 2013-05-19 16:58:01Z vboxsync $ */
2/** @file
3 * IPRT - Debug Module Interpreter.
4 */
5
6/*
7 * Copyright (C) 2009-2012 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 "internal/dbgmod.h"
50#include "internal/magics.h"
51
52
53/*******************************************************************************
54* Structures and Typedefs *
55*******************************************************************************/
56/** Debug info interpreter registration record. */
57typedef struct RTDBGMODREGDBG
58{
59 /** Pointer to the next record. */
60 struct RTDBGMODREGDBG *pNext;
61 /** Pointer to the virtual function table for the interpreter. */
62 PCRTDBGMODVTDBG pVt;
63 /** Usage counter. */
64 uint32_t volatile cUsers;
65} RTDBGMODREGDBG;
66typedef RTDBGMODREGDBG *PRTDBGMODREGDBG;
67
68/** Image interpreter registration record. */
69typedef struct RTDBGMODREGIMG
70{
71 /** Pointer to the next record. */
72 struct RTDBGMODREGIMG *pNext;
73 /** Pointer to the virtual function table for the interpreter. */
74 PCRTDBGMODVTIMG pVt;
75 /** Usage counter. */
76 uint32_t volatile cUsers;
77} RTDBGMODREGIMG;
78typedef RTDBGMODREGIMG *PRTDBGMODREGIMG;
79
80
81/*******************************************************************************
82* Defined Constants And Macros *
83*******************************************************************************/
84/** Validates a debug module handle and returns rc if not valid. */
85#define RTDBGMOD_VALID_RETURN_RC(pDbgMod, rc) \
86 do { \
87 AssertPtrReturn((pDbgMod), (rc)); \
88 AssertReturn((pDbgMod)->u32Magic == RTDBGMOD_MAGIC, (rc)); \
89 AssertReturn((pDbgMod)->cRefs > 0, (rc)); \
90 } while (0)
91
92/** Locks the debug module. */
93#define RTDBGMOD_LOCK(pDbgMod) \
94 do { \
95 int rcLock = RTCritSectEnter(&(pDbgMod)->CritSect); \
96 AssertRC(rcLock); \
97 } while (0)
98
99/** Unlocks the debug module. */
100#define RTDBGMOD_UNLOCK(pDbgMod) \
101 do { \
102 int rcLock = RTCritSectLeave(&(pDbgMod)->CritSect); \
103 AssertRC(rcLock); \
104 } while (0)
105
106
107/*******************************************************************************
108* Global Variables *
109*******************************************************************************/
110/** Init once object for lazy registration of the built-in image and debug
111 * info interpreters. */
112static RTONCE g_rtDbgModOnce = RTONCE_INITIALIZER;
113/** Read/Write semaphore protecting the list of registered interpreters. */
114static RTSEMRW g_hDbgModRWSem = NIL_RTSEMRW;
115/** List of registered image interpreters. */
116static PRTDBGMODREGIMG g_pImgHead;
117/** List of registered debug infor interpreters. */
118static PRTDBGMODREGDBG g_pDbgHead;
119/** String cache for the debug info interpreters.
120 * RTSTRCACHE is thread safe. */
121DECLHIDDEN(RTSTRCACHE) g_hDbgModStrCache = NIL_RTSTRCACHE;
122
123
124
125
126
127/**
128 * Cleanup debug info interpreter globals.
129 *
130 * @param enmReason The cause of the termination.
131 * @param iStatus The meaning of this depends on enmReason.
132 * @param pvUser User argument, unused.
133 */
134static DECLCALLBACK(void) rtDbgModTermCallback(RTTERMREASON enmReason, int32_t iStatus, void *pvUser)
135{
136 NOREF(iStatus); NOREF(pvUser);
137 if (enmReason == RTTERMREASON_UNLOAD)
138 {
139 RTSemRWDestroy(g_hDbgModRWSem);
140 g_hDbgModRWSem = NIL_RTSEMRW;
141
142 RTStrCacheDestroy(g_hDbgModStrCache);
143 g_hDbgModStrCache = NIL_RTSTRCACHE;
144
145 PRTDBGMODREGDBG pDbg = g_pDbgHead;
146 g_pDbgHead = NULL;
147 while (pDbg)
148 {
149 PRTDBGMODREGDBG pNext = pDbg->pNext;
150 AssertMsg(pDbg->cUsers == 0, ("%#x %s\n", pDbg->cUsers, pDbg->pVt->pszName));
151 RTMemFree(pDbg);
152 pDbg = pNext;
153 }
154
155 PRTDBGMODREGIMG pImg = g_pImgHead;
156 g_pImgHead = NULL;
157 while (pImg)
158 {
159 PRTDBGMODREGIMG pNext = pImg->pNext;
160 AssertMsg(pImg->cUsers == 0, ("%#x %s\n", pImg->cUsers, pImg->pVt->pszName));
161 RTMemFree(pImg);
162 pImg = pNext;
163 }
164 }
165}
166
167
168/**
169 * Internal worker for register a debug interpreter.
170 *
171 * Called while owning the write lock or when locking isn't required.
172 *
173 * @returns IPRT status code.
174 * @retval VERR_NO_MEMORY
175 * @retval VERR_ALREADY_EXISTS
176 *
177 * @param pVt The virtual function table of the debug
178 * module interpreter.
179 */
180static int rtDbgModDebugInterpreterRegister(PCRTDBGMODVTDBG pVt)
181{
182 /*
183 * Search or duplicate registration.
184 */
185 PRTDBGMODREGDBG pPrev = NULL;
186 for (PRTDBGMODREGDBG pCur = g_pDbgHead; pCur; pCur = pCur->pNext)
187 {
188 if (pCur->pVt == pVt)
189 return VERR_ALREADY_EXISTS;
190 if (!strcmp(pCur->pVt->pszName, pVt->pszName))
191 return VERR_ALREADY_EXISTS;
192 pPrev = pCur;
193 }
194
195 /*
196 * Create a new record and add it to the end of the list.
197 */
198 PRTDBGMODREGDBG pReg = (PRTDBGMODREGDBG)RTMemAlloc(sizeof(*pReg));
199 if (!pReg)
200 return VERR_NO_MEMORY;
201 pReg->pVt = pVt;
202 pReg->cUsers = 0;
203 pReg->pNext = NULL;
204 if (pPrev)
205 pPrev->pNext = pReg;
206 else
207 g_pDbgHead = pReg;
208 return VINF_SUCCESS;
209}
210
211
212/**
213 * Internal worker for register a image interpreter.
214 *
215 * Called while owning the write lock or when locking isn't required.
216 *
217 * @returns IPRT status code.
218 * @retval VERR_NO_MEMORY
219 * @retval VERR_ALREADY_EXISTS
220 *
221 * @param pVt The virtual function table of the image
222 * interpreter.
223 */
224static int rtDbgModImageInterpreterRegister(PCRTDBGMODVTIMG pVt)
225{
226 /*
227 * Search or duplicate registration.
228 */
229 PRTDBGMODREGIMG pPrev = NULL;
230 for (PRTDBGMODREGIMG pCur = g_pImgHead; pCur; pCur = pCur->pNext)
231 {
232 if (pCur->pVt == pVt)
233 return VERR_ALREADY_EXISTS;
234 if (!strcmp(pCur->pVt->pszName, pVt->pszName))
235 return VERR_ALREADY_EXISTS;
236 pPrev = pCur;
237 }
238
239 /*
240 * Create a new record and add it to the end of the list.
241 */
242 PRTDBGMODREGIMG pReg = (PRTDBGMODREGIMG)RTMemAlloc(sizeof(*pReg));
243 if (!pReg)
244 return VERR_NO_MEMORY;
245 pReg->pVt = pVt;
246 pReg->cUsers = 0;
247 pReg->pNext = NULL;
248 if (pPrev)
249 pPrev->pNext = pReg;
250 else
251 g_pImgHead = pReg;
252 return VINF_SUCCESS;
253}
254
255
256/**
257 * Do-once callback that initializes the read/write semaphore and registers
258 * the built-in interpreters.
259 *
260 * @returns IPRT status code.
261 * @param pvUser NULL.
262 */
263static DECLCALLBACK(int) rtDbgModInitOnce(void *pvUser)
264{
265 NOREF(pvUser);
266
267 /*
268 * Create the semaphore and string cache.
269 */
270 int rc = RTSemRWCreate(&g_hDbgModRWSem);
271 AssertRCReturn(rc, rc);
272
273 rc = RTStrCacheCreate(&g_hDbgModStrCache, "RTDBGMOD");
274 if (RT_SUCCESS(rc))
275 {
276 /*
277 * Register the interpreters.
278 */
279 rc = rtDbgModDebugInterpreterRegister(&g_rtDbgModVtDbgNm);
280 if (RT_SUCCESS(rc))
281 rc = rtDbgModDebugInterpreterRegister(&g_rtDbgModVtDbgDwarf);
282#ifdef RT_OS_WINDOWS
283 if (RT_SUCCESS(rc))
284 rc = rtDbgModDebugInterpreterRegister(&g_rtDbgModVtDbgDbgHelp);
285#endif
286 if (RT_SUCCESS(rc))
287 rc = rtDbgModImageInterpreterRegister(&g_rtDbgModVtImgLdr);
288 if (RT_SUCCESS(rc))
289 {
290 /*
291 * Finally, register the IPRT cleanup callback.
292 */
293 rc = RTTermRegisterCallback(rtDbgModTermCallback, NULL);
294 if (RT_SUCCESS(rc))
295 return VINF_SUCCESS;
296
297 /* bail out: use the termination callback. */
298 }
299 }
300 else
301 g_hDbgModStrCache = NIL_RTSTRCACHE;
302 rtDbgModTermCallback(RTTERMREASON_UNLOAD, 0, NULL);
303 return rc;
304}
305
306
307/**
308 * Performs lazy init of our global variables.
309 * @returns IPRT status code.
310 */
311DECLINLINE(int) rtDbgModLazyInit(void)
312{
313 return RTOnce(&g_rtDbgModOnce, rtDbgModInitOnce, NULL);
314}
315
316
317RTDECL(int) RTDbgModCreate(PRTDBGMOD phDbgMod, const char *pszName, RTUINTPTR cbSeg, uint32_t fFlags)
318{
319 /*
320 * Input validation and lazy initialization.
321 */
322 AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
323 *phDbgMod = NIL_RTDBGMOD;
324 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
325 AssertReturn(*pszName, VERR_INVALID_PARAMETER);
326 AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER);
327
328 int rc = rtDbgModLazyInit();
329 if (RT_FAILURE(rc))
330 return rc;
331
332 /*
333 * Allocate a new module instance.
334 */
335 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
336 if (!pDbgMod)
337 return VERR_NO_MEMORY;
338 pDbgMod->u32Magic = RTDBGMOD_MAGIC;
339 pDbgMod->cRefs = 1;
340 rc = RTCritSectInit(&pDbgMod->CritSect);
341 if (RT_SUCCESS(rc))
342 {
343 pDbgMod->pszImgFileSpecified = RTStrCacheEnter(g_hDbgModStrCache, pszName);
344 pDbgMod->pszName = RTStrCacheEnterLower(g_hDbgModStrCache, RTPathFilenameEx(pszName, RTPATH_STR_F_STYLE_DOS));
345 if (pDbgMod->pszName)
346 {
347 rc = rtDbgModContainerCreate(pDbgMod, cbSeg);
348 if (RT_SUCCESS(rc))
349 {
350 *phDbgMod = pDbgMod;
351 return rc;
352 }
353 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
354 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
355 }
356 RTCritSectDelete(&pDbgMod->CritSect);
357 }
358
359 RTMemFree(pDbgMod);
360 return rc;
361}
362RT_EXPORT_SYMBOL(RTDbgModCreate);
363
364
365RTDECL(int) RTDbgModCreateFromMap(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName,
366 RTUINTPTR uSubtrahend, RTDBGCFG hDbgCfg)
367{
368 /*
369 * Input validation and lazy initialization.
370 */
371 AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
372 *phDbgMod = NIL_RTDBGMOD;
373 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
374 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
375 AssertPtrNullReturn(pszName, VERR_INVALID_POINTER);
376 AssertReturn(uSubtrahend == 0, VERR_NOT_IMPLEMENTED); /** @todo implement uSubtrahend. */
377
378 int rc = rtDbgModLazyInit();
379 if (RT_FAILURE(rc))
380 return rc;
381
382 if (!pszName)
383 pszName = RTPathFilenameEx(pszFilename, RTPATH_STR_F_STYLE_DOS);
384
385 /*
386 * Allocate a new module instance.
387 */
388 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
389 if (!pDbgMod)
390 return VERR_NO_MEMORY;
391 pDbgMod->u32Magic = RTDBGMOD_MAGIC;
392 pDbgMod->cRefs = 1;
393 rc = RTCritSectInit(&pDbgMod->CritSect);
394 if (RT_SUCCESS(rc))
395 {
396 pDbgMod->pszName = RTStrCacheEnterLower(g_hDbgModStrCache, pszName);
397 if (pDbgMod->pszName)
398 {
399 pDbgMod->pszDbgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
400 if (pDbgMod->pszDbgFile)
401 {
402 /*
403 * Try the map file readers.
404 */
405 rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
406 if (RT_SUCCESS(rc))
407 {
408 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
409 for (PRTDBGMODREGDBG pCur = g_pDbgHead; pCur; pCur = pCur->pNext)
410 {
411 if (pCur->pVt->fSupports & RT_DBGTYPE_MAP)
412 {
413 pDbgMod->pDbgVt = pCur->pVt;
414 pDbgMod->pvDbgPriv = NULL;
415 rc = pCur->pVt->pfnTryOpen(pDbgMod, RTLDRARCH_WHATEVER);
416 if (RT_SUCCESS(rc))
417 {
418 ASMAtomicIncU32(&pCur->cUsers);
419 RTSemRWReleaseRead(g_hDbgModRWSem);
420
421 *phDbgMod = pDbgMod;
422 return rc;
423 }
424 }
425 }
426
427 /* bail out */
428 RTSemRWReleaseRead(g_hDbgModRWSem);
429 }
430 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
431 }
432 else
433 rc = VERR_NO_STR_MEMORY;
434 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszDbgFile);
435 }
436 else
437 rc = VERR_NO_STR_MEMORY;
438 RTCritSectDelete(&pDbgMod->CritSect);
439 }
440
441 RTMemFree(pDbgMod);
442 return rc;
443}
444RT_EXPORT_SYMBOL(RTDbgModCreateFromMap);
445
446
447
448/*
449 *
450 * E x e c u t a b l e I m a g e F i l e s
451 * E x e c u t a b l e I m a g e F i l e s
452 * E x e c u t a b l e I m a g e F i l e s
453 *
454 */
455
456
457/**
458 * Opens debug information for an image.
459 *
460 * @returns IPRT status code
461 * @param pDbgMod The debug module structure.
462 *
463 * @note This will generally not look for debug info stored in external
464 * files. rtDbgModFromPeImageExtDbgInfoCallback can help with that.
465 */
466static int rtDbgModOpenDebugInfoInsideImage(PRTDBGMODINT pDbgMod)
467{
468 AssertReturn(!pDbgMod->pDbgVt, VERR_DBG_MOD_IPE);
469 AssertReturn(pDbgMod->pImgVt, VERR_DBG_MOD_IPE);
470
471 int rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
472 if (RT_SUCCESS(rc))
473 {
474 for (PRTDBGMODREGDBG pDbg = g_pDbgHead; pDbg; pDbg = pDbg->pNext)
475 {
476 pDbgMod->pDbgVt = pDbg->pVt;
477 pDbgMod->pvDbgPriv = NULL;
478 rc = pDbg->pVt->pfnTryOpen(pDbgMod, pDbgMod->pImgVt->pfnGetArch(pDbgMod));
479 if (RT_SUCCESS(rc))
480 {
481 /*
482 * That's it!
483 */
484 ASMAtomicIncU32(&pDbg->cUsers);
485 RTSemRWReleaseRead(g_hDbgModRWSem);
486 return VINF_SUCCESS;
487 }
488 pDbgMod->pDbgVt = NULL;
489 Assert(pDbgMod->pvDbgPriv == NULL);
490 }
491 RTSemRWReleaseRead(g_hDbgModRWSem);
492 }
493
494 return VERR_DBG_NO_MATCHING_INTERPRETER;
495}
496
497
498/** @callback_method_impl{FNRTDBGCFGOPEN} */
499static DECLCALLBACK(int) rtDbgModExtDbgInfoOpenCallback(RTDBGCFG hDbgCfg, const char *pszFilename, void *pvUser1, void *pvUser2)
500{
501 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)pvUser1;
502 PCRTLDRDBGINFO pDbgInfo = (PCRTLDRDBGINFO)pvUser2;
503 NOREF(pDbgInfo); /** @todo consider a more direct search for a interpreter. */
504
505 Assert(!pDbgMod->pDbgVt);
506 Assert(!pDbgMod->pvDbgPriv);
507 Assert(!pDbgMod->pszDbgFile);
508 Assert(pDbgMod->pImgVt);
509
510 /*
511 * Set the debug file name and try possible interpreters.
512 */
513 pDbgMod->pszDbgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
514
515 int rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
516 if (RT_SUCCESS(rc))
517 {
518 for (PRTDBGMODREGDBG pDbg = g_pDbgHead; pDbg; pDbg = pDbg->pNext)
519 {
520 pDbgMod->pDbgVt = pDbg->pVt;
521 pDbgMod->pvDbgPriv = NULL;
522 rc = pDbg->pVt->pfnTryOpen(pDbgMod, pDbgMod->pImgVt->pfnGetArch(pDbgMod));
523 if (RT_SUCCESS(rc))
524 {
525 /*
526 * Got it!
527 */
528 ASMAtomicIncU32(&pDbg->cUsers);
529 RTSemRWReleaseRead(g_hDbgModRWSem);
530 return VINF_CALLBACK_RETURN;
531 }
532 pDbgMod->pDbgVt = NULL;
533 Assert(pDbgMod->pvDbgPriv == NULL);
534 }
535 }
536
537 /* No joy. */
538 RTSemRWReleaseRead(g_hDbgModRWSem);
539 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszDbgFile);
540 pDbgMod->pszDbgFile = NULL;
541 return rc;
542}
543
544
545/**
546 * Argument package used by rtDbgModOpenDebugInfoExternalToImage.
547 */
548typedef struct RTDBGMODOPENDIETI
549{
550 PRTDBGMODINT pDbgMod;
551 RTDBGCFG hDbgCfg;
552} RTDBGMODOPENDIETI;
553
554
555/** @callback_method_impl{FNRTLDRENUMDBG} */
556static DECLCALLBACK(int)
557rtDbgModOpenDebugInfoExternalToImageCallback(RTLDRMOD hLdrMod, PCRTLDRDBGINFO pDbgInfo, void *pvUser)
558{
559 RTDBGMODOPENDIETI *pArgs = (RTDBGMODOPENDIETI *)pvUser;
560
561 Assert(pDbgInfo->enmType > RTLDRDBGINFOTYPE_INVALID && pDbgInfo->enmType < RTLDRDBGINFOTYPE_END);
562 const char *pszExtFile = pDbgInfo->pszExtFile;
563 if (!pszExtFile)
564 {
565 /*
566 * If a external debug type comes without a file name, calculate a
567 * likely debug filename for it. (Hack for NT4 drivers.)
568 */
569 const char *pszExt = NULL;
570 if (pDbgInfo->enmType == RTLDRDBGINFOTYPE_CODEVIEW_DBG)
571 pszExt = ".dbg";
572 else if ( pDbgInfo->enmType == RTLDRDBGINFOTYPE_CODEVIEW_PDB20
573 || pDbgInfo->enmType == RTLDRDBGINFOTYPE_CODEVIEW_PDB70)
574 pszExt = ".pdb";
575 if (pszExt && pArgs->pDbgMod->pszName)
576 {
577 size_t cchName = strlen(pArgs->pDbgMod->pszName);
578 char *psz = (char *)alloca(cchName + strlen(pszExt) + 1);
579 if (psz)
580 {
581 memcpy(psz, pArgs->pDbgMod->pszName, cchName + 1);
582 RTPathStripExt(psz);
583 pszExtFile = strcat(psz, pszExt);
584 }
585 }
586
587 if (!pszExtFile)
588 {
589 Log2(("rtDbgModOpenDebugInfoExternalToImageCallback: enmType=%d\n", pDbgInfo->enmType));
590 return VINF_SUCCESS;
591 }
592 }
593
594 /*
595 * Switch on type and call the appropriate search function.
596 */
597 int rc;
598 switch (pDbgInfo->enmType)
599 {
600 case RTLDRDBGINFOTYPE_CODEVIEW_PDB70:
601 rc = RTDbgCfgOpenPdb70(pArgs->hDbgCfg, pszExtFile,
602 &pDbgInfo->u.Pdb70.Uuid,
603 pDbgInfo->u.Pdb70.uAge,
604 rtDbgModExtDbgInfoOpenCallback, pArgs->pDbgMod, (void *)pDbgInfo);
605 break;
606
607 case RTLDRDBGINFOTYPE_CODEVIEW_PDB20:
608 rc = RTDbgCfgOpenPdb20(pArgs->hDbgCfg, pszExtFile,
609 pDbgInfo->u.Pdb20.cbImage,
610 pDbgInfo->u.Pdb20.uTimestamp,
611 pDbgInfo->u.Pdb20.uAge,
612 rtDbgModExtDbgInfoOpenCallback, pArgs->pDbgMod, (void *)pDbgInfo);
613 break;
614
615 case RTLDRDBGINFOTYPE_CODEVIEW_DBG:
616 rc = RTDbgCfgOpenDbg(pArgs->hDbgCfg, pszExtFile,
617 pDbgInfo->u.Dbg.cbImage,
618 pDbgInfo->u.Dbg.uTimestamp,
619 rtDbgModExtDbgInfoOpenCallback, pArgs->pDbgMod, (void *)pDbgInfo);
620 break;
621
622 case RTLDRDBGINFOTYPE_DWARF_DWO:
623 rc = RTDbgCfgOpenDwo(pArgs->hDbgCfg, pszExtFile,
624 pDbgInfo->u.Dwo.uCrc32,
625 rtDbgModExtDbgInfoOpenCallback, pArgs->pDbgMod, (void *)pDbgInfo);
626 break;
627
628 default:
629 Log(("rtDbgModOpenDebugInfoExternalToImageCallback: Don't know how to handle enmType=%d and pszFileExt=%s\n",
630 pDbgInfo->enmType, pszExtFile));
631 return VERR_DBG_TODO;
632 }
633 if (RT_SUCCESS(rc))
634 {
635 LogFlow(("RTDbgMod: Successfully opened external debug info '%s' for '%s'\n",
636 pArgs->pDbgMod->pszDbgFile, pArgs->pDbgMod->pszImgFile));
637 return VINF_CALLBACK_RETURN;
638 }
639 Log(("rtDbgModOpenDebugInfoExternalToImageCallback: '%s' (enmType=%d) for '%s' -> %Rrc\n",
640 pszExtFile, pDbgInfo->enmType, pArgs->pDbgMod->pszImgFile, rc));
641 return rc;
642}
643
644
645/**
646 * Opens debug info listed in the image that is stored in a separate file.
647 *
648 * @returns IPRT status code
649 * @param pDbgMod The debug module.
650 * @param hDbgCfg The debug config. Can be NIL.
651 */
652static int rtDbgModOpenDebugInfoExternalToImage(PRTDBGMODINT pDbgMod, RTDBGCFG hDbgCfg)
653{
654 Assert(!pDbgMod->pDbgVt);
655
656 RTDBGMODOPENDIETI Args;
657 Args.pDbgMod = pDbgMod;
658 Args.hDbgCfg = hDbgCfg;
659 int rc = pDbgMod->pImgVt->pfnEnumDbgInfo(pDbgMod, rtDbgModOpenDebugInfoExternalToImageCallback, &Args);
660 if (RT_SUCCESS(rc) && pDbgMod->pDbgVt)
661 return VINF_SUCCESS;
662
663 LogFlow(("rtDbgModOpenDebugInfoExternalToImage: rc=%Rrc\n", rc));
664 return VERR_NOT_FOUND;
665}
666
667
668/** @callback_method_impl{FNRTDBGCFGOPEN} */
669static DECLCALLBACK(int) rtDbgModExtDbgInfoOpenCallback2(RTDBGCFG hDbgCfg, const char *pszFilename, void *pvUser1, void *pvUser2)
670{
671 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)pvUser1;
672 NOREF(pvUser2); /** @todo image matching string or smth. */
673
674 Assert(!pDbgMod->pDbgVt);
675 Assert(!pDbgMod->pvDbgPriv);
676 Assert(!pDbgMod->pszDbgFile);
677 Assert(pDbgMod->pImgVt);
678
679 /*
680 * Set the debug file name and try possible interpreters.
681 */
682 pDbgMod->pszDbgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
683
684 int rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
685 if (RT_SUCCESS(rc))
686 {
687 for (PRTDBGMODREGDBG pDbg = g_pDbgHead; pDbg; pDbg = pDbg->pNext)
688 {
689 pDbgMod->pDbgVt = pDbg->pVt;
690 pDbgMod->pvDbgPriv = NULL;
691 rc = pDbg->pVt->pfnTryOpen(pDbgMod, pDbgMod->pImgVt->pfnGetArch(pDbgMod));
692 if (RT_SUCCESS(rc))
693 {
694 /*
695 * Got it!
696 */
697 ASMAtomicIncU32(&pDbg->cUsers);
698 RTSemRWReleaseRead(g_hDbgModRWSem);
699 return VINF_CALLBACK_RETURN;
700 }
701 pDbgMod->pDbgVt = NULL;
702 Assert(pDbgMod->pvDbgPriv == NULL);
703 }
704 }
705
706 /* No joy. */
707 RTSemRWReleaseRead(g_hDbgModRWSem);
708 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszDbgFile);
709 pDbgMod->pszDbgFile = NULL;
710 return rc;
711}
712
713
714/**
715 * Opens external debug info that is not listed in the image.
716 *
717 * @returns IPRT status code
718 * @param pDbgMod The debug module.
719 * @param hDbgCfg The debug config. Can be NIL.
720 */
721static int rtDbgModOpenDebugInfoExternalToImage2(PRTDBGMODINT pDbgMod, RTDBGCFG hDbgCfg)
722{
723 int rc;
724 Assert(!pDbgMod->pDbgVt);
725 Assert(pDbgMod->pImgVt);
726
727 /*
728 * Figure out what to search for based on the image format.
729 */
730 const char *pszzExts = NULL;
731 RTLDRFMT enmFmt = pDbgMod->pImgVt->pfnGetFormat(pDbgMod);
732 switch (enmFmt)
733 {
734 case RTLDRFMT_MACHO:
735 {
736 rc = RTDbgCfgOpenDsymBundle(hDbgCfg, pDbgMod->pszImgFile, NULL /**@todo pUuid*/,
737 rtDbgModExtDbgInfoOpenCallback2, pDbgMod, NULL /*pvUser2*/);
738 if (RT_SUCCESS(rc))
739 return VINF_SUCCESS;
740 break;
741 }
742
743#if 0 /* Will be links in the image if these apply. .map readers for PE or ELF we don't have. */
744 case RTLDRFMT_ELF:
745 pszzExts = ".debug\0.dwo\0";
746 break;
747 case RTLDRFMT_PE:
748 pszzExts = ".map\0";
749 break;
750#endif
751#if 0 /* Haven't implemented .sym or .map file readers for OS/2 yet. */
752 case RTLDRFMT_LX:
753 pszzExts = ".sym\0.map\0";
754 break;
755#endif
756 default:
757 break;
758 }
759
760 NOREF(pszzExts);
761#if 0 /* Later */
762 if (pszzExts)
763 {
764
765 }
766#endif
767
768 LogFlow(("rtDbgModOpenDebugInfoExternalToImage2: rc=%Rrc\n", rc));
769 return VERR_NOT_FOUND;
770}
771
772
773RTDECL(int) RTDbgModCreateFromImage(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName,
774 RTLDRARCH enmArch, RTDBGCFG hDbgCfg)
775{
776 /*
777 * Input validation and lazy initialization.
778 */
779 AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
780 *phDbgMod = NIL_RTDBGMOD;
781 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
782 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
783 AssertPtrNullReturn(pszName, VERR_INVALID_POINTER);
784 AssertReturn(enmArch > RTLDRARCH_INVALID && enmArch < RTLDRARCH_END, VERR_INVALID_PARAMETER);
785
786 int rc = rtDbgModLazyInit();
787 if (RT_FAILURE(rc))
788 return rc;
789
790 if (!pszName)
791 pszName = RTPathFilenameEx(pszFilename, RTPATH_STR_F_STYLE_DOS);
792
793 /*
794 * Allocate a new module instance.
795 */
796 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
797 if (!pDbgMod)
798 return VERR_NO_MEMORY;
799 pDbgMod->u32Magic = RTDBGMOD_MAGIC;
800 pDbgMod->cRefs = 1;
801 rc = RTCritSectInit(&pDbgMod->CritSect);
802 if (RT_SUCCESS(rc))
803 {
804 pDbgMod->pszName = RTStrCacheEnterLower(g_hDbgModStrCache, pszName);
805 if (pDbgMod->pszName)
806 {
807 pDbgMod->pszImgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
808 if (pDbgMod->pszImgFile)
809 {
810 RTStrCacheRetain(pDbgMod->pszImgFile);
811 pDbgMod->pszImgFileSpecified = pDbgMod->pszImgFile;
812
813 /*
814 * Find an image reader which groks the file.
815 */
816 rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
817 if (RT_SUCCESS(rc))
818 {
819 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
820 PRTDBGMODREGIMG pImg;
821 for (pImg = g_pImgHead; pImg; pImg = pImg->pNext)
822 {
823 pDbgMod->pImgVt = pImg->pVt;
824 pDbgMod->pvImgPriv = NULL;
825 /** @todo need to specify some arch stuff here. */
826 rc = pImg->pVt->pfnTryOpen(pDbgMod, enmArch);
827 if (RT_SUCCESS(rc))
828 {
829 /*
830 * Image detected, but found no debug info we were
831 * able to understand.
832 */
833 /** @todo some generic way of matching image and debug info, flexible signature
834 * of some kind. Apple uses UUIDs, microsoft uses a UUID+age or a
835 * size+timestamp, and GNU a CRC32 (last time I checked). */
836 rc = rtDbgModOpenDebugInfoExternalToImage(pDbgMod, hDbgCfg);
837 if (RT_FAILURE(rc))
838 rc = rtDbgModOpenDebugInfoInsideImage(pDbgMod);
839 if (RT_FAILURE(rc))
840 rc = rtDbgModOpenDebugInfoExternalToImage2(pDbgMod, hDbgCfg);
841 if (RT_FAILURE(rc))
842 rc = rtDbgModCreateForExports(pDbgMod);
843 if (RT_SUCCESS(rc))
844 {
845 *phDbgMod = pDbgMod;
846 return VINF_SUCCESS;
847 }
848
849 /* Failed, close up the shop. */
850 pDbgMod->pImgVt->pfnClose(pDbgMod);
851 pDbgMod->pImgVt = NULL;
852 pDbgMod->pvImgPriv = NULL;
853 break;
854 }
855 }
856
857 /*
858 * Could it be a file containing raw debug info?
859 */
860 if (!pImg)
861 {
862 pDbgMod->pImgVt = NULL;
863 pDbgMod->pvImgPriv = NULL;
864 pDbgMod->pszDbgFile = pDbgMod->pszImgFile;
865 pDbgMod->pszImgFile = NULL;
866
867 for (PRTDBGMODREGDBG pDbg = g_pDbgHead; pDbg; pDbg = pDbg->pNext)
868 {
869 pDbgMod->pDbgVt = pDbg->pVt;
870 pDbgMod->pvDbgPriv = NULL;
871 rc = pDbg->pVt->pfnTryOpen(pDbgMod, enmArch);
872 if (RT_SUCCESS(rc))
873 {
874 /*
875 * That's it!
876 */
877 ASMAtomicIncU32(&pDbg->cUsers);
878 RTSemRWReleaseRead(g_hDbgModRWSem);
879
880 *phDbgMod = pDbgMod;
881 return rc;
882 }
883 }
884
885 pDbgMod->pszImgFile = pDbgMod->pszDbgFile;
886 pDbgMod->pszDbgFile = NULL;
887 }
888
889 /* bail out */
890 RTSemRWReleaseRead(g_hDbgModRWSem);
891 }
892 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFileSpecified);
893 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
894 }
895 else
896 rc = VERR_NO_STR_MEMORY;
897 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
898 }
899 else
900 rc = VERR_NO_STR_MEMORY;
901 RTCritSectDelete(&pDbgMod->CritSect);
902 }
903
904 RTMemFree(pDbgMod);
905 return rc;
906}
907RT_EXPORT_SYMBOL(RTDbgModCreateFromImage);
908
909
910
911
912
913/*
914 *
915 * P E I M A G E
916 * P E I M A G E
917 * P E I M A G E
918 *
919 */
920
921
922
923/** @callback_method_impl{FNRTDBGCFGOPEN} */
924static DECLCALLBACK(int) rtDbgModFromPeImageOpenCallback(RTDBGCFG hDbgCfg, const char *pszFilename, void *pvUser1, void *pvUser2)
925{
926 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)pvUser1;
927 PRTDBGMODDEFERRED pDeferred = (PRTDBGMODDEFERRED)pvUser2;
928 LogFlow(("rtDbgModFromPeImageOpenCallback: %s\n", pszFilename));
929
930 Assert(pDbgMod->pImgVt == NULL);
931 Assert(pDbgMod->pvImgPriv == NULL);
932 Assert(pDbgMod->pDbgVt == NULL);
933 Assert(pDbgMod->pvDbgPriv == NULL);
934
935 /*
936 * Replace the image file name while probing it.
937 */
938 const char *pszNewImgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
939 if (!pszNewImgFile)
940 return VERR_NO_STR_MEMORY;
941 const char *pszOldImgFile = pDbgMod->pszImgFile;
942 pDbgMod->pszImgFile = pszNewImgFile;
943
944 /*
945 * Find an image reader which groks the file.
946 */
947 int rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
948 if (RT_SUCCESS(rc))
949 {
950 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
951 PRTDBGMODREGIMG pImg;
952 for (pImg = g_pImgHead; pImg; pImg = pImg->pNext)
953 {
954 pDbgMod->pImgVt = pImg->pVt;
955 pDbgMod->pvImgPriv = NULL;
956 rc = pImg->pVt->pfnTryOpen(pDbgMod, RTLDRARCH_WHATEVER);
957 if (RT_SUCCESS(rc))
958 break;
959 pDbgMod->pImgVt = NULL;
960 Assert(pDbgMod->pvImgPriv == NULL);
961 }
962 RTSemRWReleaseRead(g_hDbgModRWSem);
963 if (RT_SUCCESS(rc))
964 {
965 /*
966 * Check the deferred info.
967 */
968 RTUINTPTR cbImage = pDbgMod->pImgVt->pfnImageSize(pDbgMod);
969 if ( pDeferred->cbImage == 0
970 || pDeferred->cbImage == cbImage)
971 {
972 uint32_t uTimestamp = pDeferred->u.PeImage.uTimestamp; /** @todo add method for getting the timestamp. */
973 if ( pDeferred->u.PeImage.uTimestamp == 0
974 || pDeferred->u.PeImage.uTimestamp == uTimestamp)
975 {
976 Log(("RTDbgMod: Found matching PE image '%s'\n", pszFilename));
977
978 /*
979 * We found the executable image we need, now go find any
980 * debug info associated with it. For PE images, this is
981 * generally found in an external file, so we do a sweep
982 * for that first.
983 *
984 * Then try open debug inside the module, and finally
985 * falling back on exports.
986 */
987 rc = rtDbgModOpenDebugInfoExternalToImage(pDbgMod, pDeferred->hDbgCfg);
988 if (RT_FAILURE(rc))
989 rc = rtDbgModOpenDebugInfoInsideImage(pDbgMod);
990 if (RT_FAILURE(rc))
991 rc = rtDbgModCreateForExports(pDbgMod);
992 if (RT_SUCCESS(rc))
993 {
994 RTStrCacheRelease(g_hDbgModStrCache, pszOldImgFile);
995 return VINF_CALLBACK_RETURN;
996 }
997
998 /* Something bad happened, just give up. */
999 Log(("rtDbgModFromPeImageOpenCallback: rtDbgModCreateForExports failed: %Rrc\n", rc));
1000 }
1001 else
1002 {
1003 LogFlow(("rtDbgModFromPeImageOpenCallback: uTimestamp mismatch (found %#x, expected %#x) - %s\n",
1004 uTimestamp, pDeferred->u.PeImage.uTimestamp, pszFilename));
1005 rc = VERR_DBG_FILE_MISMATCH;
1006 }
1007 }
1008 else
1009 {
1010 LogFlow(("rtDbgModFromPeImageOpenCallback: cbImage mismatch (found %#x, expected %#x) - %s\n",
1011 cbImage, pDeferred->cbImage, pszFilename));
1012 rc = VERR_DBG_FILE_MISMATCH;
1013 }
1014
1015 pDbgMod->pImgVt->pfnClose(pDbgMod);
1016 pDbgMod->pImgVt = NULL;
1017 pDbgMod->pvImgPriv = NULL;
1018 }
1019 else
1020 LogFlow(("rtDbgModFromPeImageOpenCallback: Failed %Rrc - %s\n", rc, pszFilename));
1021 }
1022
1023 /* Restore image name. */
1024 pDbgMod->pszImgFile = pszOldImgFile;
1025 RTStrCacheRelease(g_hDbgModStrCache, pszNewImgFile);
1026 return rc;
1027}
1028
1029
1030/** @callback_method_impl{FNRTDBGMODDEFERRED} */
1031static DECLCALLBACK(int) rtDbgModFromPeImageDeferredCallback(PRTDBGMODINT pDbgMod, PRTDBGMODDEFERRED pDeferred)
1032{
1033 int rc;
1034
1035 Assert(pDbgMod->pszImgFile);
1036 if (!pDbgMod->pImgVt)
1037 rc = RTDbgCfgOpenPeImage(pDeferred->hDbgCfg, pDbgMod->pszImgFile,
1038 pDeferred->cbImage, pDeferred->u.PeImage.uTimestamp,
1039 rtDbgModFromPeImageOpenCallback, pDbgMod, pDeferred);
1040 else
1041 {
1042 rc = rtDbgModOpenDebugInfoExternalToImage(pDbgMod, pDeferred->hDbgCfg);
1043 if (RT_FAILURE(rc))
1044 rc = rtDbgModOpenDebugInfoInsideImage(pDbgMod);
1045 if (RT_FAILURE(rc))
1046 rc = rtDbgModCreateForExports(pDbgMod);
1047 }
1048 return rc;
1049}
1050
1051
1052RTDECL(int) RTDbgModCreateFromPeImage(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName, RTLDRMOD hLdrMod,
1053 uint32_t cbImage, uint32_t uTimestamp, RTDBGCFG hDbgCfg)
1054{
1055 /*
1056 * Input validation and lazy initialization.
1057 */
1058 AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
1059 *phDbgMod = NIL_RTDBGMOD;
1060 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1061 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
1062 if (!pszName)
1063 pszName = RTPathFilenameEx(pszFilename, RTPATH_STR_F_STYLE_DOS);
1064 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1065 AssertReturn(hLdrMod == NIL_RTLDRMOD || RTLdrSize(hLdrMod) != ~(size_t)0, VERR_INVALID_HANDLE);
1066
1067 int rc = rtDbgModLazyInit();
1068 if (RT_FAILURE(rc))
1069 return rc;
1070
1071 uint64_t fDbgCfg = 0;
1072 if (hDbgCfg)
1073 {
1074 rc = RTDbgCfgQueryUInt(hDbgCfg, RTDBGCFGPROP_FLAGS, &fDbgCfg);
1075 AssertRCReturn(rc, rc);
1076 }
1077
1078 /*
1079 * Allocate a new module instance.
1080 */
1081 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
1082 if (!pDbgMod)
1083 return VERR_NO_MEMORY;
1084 pDbgMod->u32Magic = RTDBGMOD_MAGIC;
1085 pDbgMod->cRefs = 1;
1086 rc = RTCritSectInit(&pDbgMod->CritSect);
1087 if (RT_SUCCESS(rc))
1088 {
1089 pDbgMod->pszName = RTStrCacheEnterLower(g_hDbgModStrCache, pszName);
1090 if (pDbgMod->pszName)
1091 {
1092 pDbgMod->pszImgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
1093 if (pDbgMod->pszImgFile)
1094 {
1095 RTStrCacheRetain(pDbgMod->pszImgFile);
1096 pDbgMod->pszImgFileSpecified = pDbgMod->pszImgFile;
1097
1098 /*
1099 * If we have a loader module, we must instantiate the loader
1100 * side of things regardless of the deferred setting.
1101 */
1102 if (hLdrMod != NIL_RTLDRMOD)
1103 {
1104 if (!cbImage)
1105 cbImage = (uint32_t)RTLdrSize(hLdrMod);
1106 pDbgMod->pImgVt = &g_rtDbgModVtImgLdr;
1107
1108 rc = rtDbgModLdrOpenFromHandle(pDbgMod, hLdrMod);
1109 }
1110 if (RT_SUCCESS(rc))
1111 {
1112 /*
1113 * Do it now or procrastinate?
1114 */
1115 if (!(fDbgCfg & RTDBGCFG_FLAGS_DEFERRED) || !cbImage)
1116 {
1117 RTDBGMODDEFERRED Deferred;
1118 Deferred.cbImage = cbImage;
1119 Deferred.hDbgCfg = hDbgCfg;
1120 Deferred.u.PeImage.uTimestamp = uTimestamp;
1121 rc = rtDbgModFromPeImageDeferredCallback(pDbgMod, &Deferred);
1122 }
1123 else
1124 {
1125 PRTDBGMODDEFERRED pDeferred;
1126 rc = rtDbgModDeferredCreate(pDbgMod, rtDbgModFromPeImageDeferredCallback, cbImage, hDbgCfg, &pDeferred);
1127 if (RT_SUCCESS(rc))
1128 pDeferred->u.PeImage.uTimestamp = uTimestamp;
1129 }
1130 if (RT_SUCCESS(rc))
1131 {
1132 *phDbgMod = pDbgMod;
1133 return VINF_SUCCESS;
1134 }
1135
1136 /* Failed, bail out. */
1137 if (hLdrMod != NIL_RTLDRMOD)
1138 {
1139 Assert(pDbgMod->pImgVt);
1140 pDbgMod->pImgVt->pfnClose(pDbgMod);
1141 }
1142 }
1143 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
1144 }
1145 else
1146 rc = VERR_NO_STR_MEMORY;
1147 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFileSpecified);
1148 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
1149 }
1150 else
1151 rc = VERR_NO_STR_MEMORY;
1152 RTCritSectDelete(&pDbgMod->CritSect);
1153 }
1154
1155 RTMemFree(pDbgMod);
1156 return rc;
1157}
1158RT_EXPORT_SYMBOL(RTDbgModCreateFromPeImage);
1159
1160
1161/**
1162 * Destroys an module after the reference count has reached zero.
1163 *
1164 * @param pDbgMod The module instance.
1165 */
1166static void rtDbgModDestroy(PRTDBGMODINT pDbgMod)
1167{
1168 /*
1169 * Close the debug info interpreter first, then the image interpret.
1170 */
1171 RTCritSectEnter(&pDbgMod->CritSect); /* paranoia */
1172
1173 if (pDbgMod->pDbgVt)
1174 {
1175 pDbgMod->pDbgVt->pfnClose(pDbgMod);
1176 pDbgMod->pDbgVt = NULL;
1177 pDbgMod->pvDbgPriv = NULL;
1178 }
1179
1180 if (pDbgMod->pImgVt)
1181 {
1182 pDbgMod->pImgVt->pfnClose(pDbgMod);
1183 pDbgMod->pImgVt = NULL;
1184 pDbgMod->pvImgPriv = NULL;
1185 }
1186
1187 /*
1188 * Free the resources.
1189 */
1190 ASMAtomicWriteU32(&pDbgMod->u32Magic, ~RTDBGMOD_MAGIC);
1191 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
1192 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
1193 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFileSpecified);
1194 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszDbgFile);
1195 RTCritSectLeave(&pDbgMod->CritSect); /* paranoia */
1196 RTCritSectDelete(&pDbgMod->CritSect);
1197 RTMemFree(pDbgMod);
1198}
1199
1200
1201RTDECL(uint32_t) RTDbgModRetain(RTDBGMOD hDbgMod)
1202{
1203 PRTDBGMODINT pDbgMod = hDbgMod;
1204 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
1205 return ASMAtomicIncU32(&pDbgMod->cRefs);
1206}
1207RT_EXPORT_SYMBOL(RTDbgModRetain);
1208
1209
1210RTDECL(uint32_t) RTDbgModRelease(RTDBGMOD hDbgMod)
1211{
1212 if (hDbgMod == NIL_RTDBGMOD)
1213 return 0;
1214 PRTDBGMODINT pDbgMod = hDbgMod;
1215 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
1216
1217 uint32_t cRefs = ASMAtomicDecU32(&pDbgMod->cRefs);
1218 if (!cRefs)
1219 rtDbgModDestroy(pDbgMod);
1220 return cRefs;
1221}
1222RT_EXPORT_SYMBOL(RTDbgModRelease);
1223
1224
1225RTDECL(const char *) RTDbgModName(RTDBGMOD hDbgMod)
1226{
1227 PRTDBGMODINT pDbgMod = hDbgMod;
1228 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NULL);
1229 return pDbgMod->pszName;
1230}
1231RT_EXPORT_SYMBOL(RTDbgModName);
1232
1233
1234RTDECL(const char *) RTDbgModDebugFile(RTDBGMOD hDbgMod)
1235{
1236 PRTDBGMODINT pDbgMod = hDbgMod;
1237 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NULL);
1238 if (pDbgMod->fDeferred || pDbgMod->fExports)
1239 return NULL;
1240 return pDbgMod->pszDbgFile;
1241}
1242RT_EXPORT_SYMBOL(RTDbgModDebugFile);
1243
1244
1245RTDECL(const char *) RTDbgModImageFile(RTDBGMOD hDbgMod)
1246{
1247 PRTDBGMODINT pDbgMod = hDbgMod;
1248 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NULL);
1249 return pDbgMod->pszImgFileSpecified;
1250}
1251RT_EXPORT_SYMBOL(RTDbgModImageFile);
1252
1253
1254RTDECL(const char *) RTDbgModImageFileUsed(RTDBGMOD hDbgMod)
1255{
1256 PRTDBGMODINT pDbgMod = hDbgMod;
1257 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NULL);
1258 return pDbgMod->pszImgFile == pDbgMod->pszImgFileSpecified ? NULL : pDbgMod->pszImgFile;
1259}
1260RT_EXPORT_SYMBOL(RTDbgModImageFileUsed);
1261
1262
1263RTDECL(bool) RTDbgModIsDeferred(RTDBGMOD hDbgMod)
1264{
1265 PRTDBGMODINT pDbgMod = hDbgMod;
1266 RTDBGMOD_VALID_RETURN_RC(pDbgMod, false);
1267 return pDbgMod->fDeferred;
1268}
1269
1270
1271RTDECL(bool) RTDbgModIsExports(RTDBGMOD hDbgMod)
1272{
1273 PRTDBGMODINT pDbgMod = hDbgMod;
1274 RTDBGMOD_VALID_RETURN_RC(pDbgMod, false);
1275 return pDbgMod->fExports;
1276}
1277
1278
1279RTDECL(int) RTDbgModRemoveAll(RTDBGMOD hDbgMod, bool fLeaveSegments)
1280{
1281 PRTDBGMODINT pDbgMod = hDbgMod;
1282 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1283
1284 RTDBGMOD_LOCK(pDbgMod);
1285
1286 /* Only possible on container modules. */
1287 int rc = VINF_SUCCESS;
1288 if (pDbgMod->pDbgVt != &g_rtDbgModVtDbgContainer)
1289 {
1290 if (fLeaveSegments)
1291 {
1292 rc = rtDbgModContainer_LineRemoveAll(pDbgMod);
1293 if (RT_SUCCESS(rc))
1294 rc = rtDbgModContainer_SymbolRemoveAll(pDbgMod);
1295 }
1296 else
1297 rc = rtDbgModContainer_RemoveAll(pDbgMod);
1298 }
1299 else
1300 rc = VERR_ACCESS_DENIED;
1301
1302 RTDBGMOD_UNLOCK(pDbgMod);
1303 return rc;
1304}
1305
1306
1307RTDECL(RTDBGSEGIDX) RTDbgModRvaToSegOff(RTDBGMOD hDbgMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
1308{
1309 PRTDBGMODINT pDbgMod = hDbgMod;
1310 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NIL_RTDBGSEGIDX);
1311 RTDBGMOD_LOCK(pDbgMod);
1312
1313 RTDBGSEGIDX iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, uRva, poffSeg);
1314
1315 RTDBGMOD_UNLOCK(pDbgMod);
1316 return iSeg;
1317}
1318RT_EXPORT_SYMBOL(RTDbgModRvaToSegOff);
1319
1320
1321RTDECL(RTUINTPTR) RTDbgModImageSize(RTDBGMOD hDbgMod)
1322{
1323 PRTDBGMODINT pDbgMod = hDbgMod;
1324 RTDBGMOD_VALID_RETURN_RC(pDbgMod, RTUINTPTR_MAX);
1325 RTDBGMOD_LOCK(pDbgMod);
1326
1327 RTUINTPTR cbImage = pDbgMod->pDbgVt->pfnImageSize(pDbgMod);
1328
1329 RTDBGMOD_UNLOCK(pDbgMod);
1330 return cbImage;
1331}
1332RT_EXPORT_SYMBOL(RTDbgModImageSize);
1333
1334
1335RTDECL(uint64_t) RTDbgModGetTag(RTDBGMOD hDbgMod)
1336{
1337 PRTDBGMODINT pDbgMod = hDbgMod;
1338 RTDBGMOD_VALID_RETURN_RC(pDbgMod, 0);
1339 return pDbgMod->uTag;
1340}
1341RT_EXPORT_SYMBOL(RTDbgModGetTag);
1342
1343
1344RTDECL(int) RTDbgModSetTag(RTDBGMOD hDbgMod, uint64_t uTag)
1345{
1346 PRTDBGMODINT pDbgMod = hDbgMod;
1347 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1348 RTDBGMOD_LOCK(pDbgMod);
1349
1350 pDbgMod->uTag = uTag;
1351
1352 RTDBGMOD_UNLOCK(pDbgMod);
1353 return VINF_SUCCESS;
1354}
1355RT_EXPORT_SYMBOL(RTDbgModSetTag);
1356
1357
1358RTDECL(int) RTDbgModSegmentAdd(RTDBGMOD hDbgMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName,
1359 uint32_t fFlags, PRTDBGSEGIDX piSeg)
1360{
1361 /*
1362 * Validate input.
1363 */
1364 PRTDBGMODINT pDbgMod = hDbgMod;
1365 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1366 AssertMsgReturn(uRva + cb >= uRva, ("uRva=%RTptr cb=%RTptr\n", uRva, cb), VERR_DBG_ADDRESS_WRAP);
1367 Assert(*pszName);
1368 size_t cchName = strlen(pszName);
1369 AssertReturn(cchName > 0, VERR_DBG_SEGMENT_NAME_OUT_OF_RANGE);
1370 AssertReturn(cchName < RTDBG_SEGMENT_NAME_LENGTH, VERR_DBG_SEGMENT_NAME_OUT_OF_RANGE);
1371 AssertMsgReturn(!fFlags, ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
1372 AssertPtrNull(piSeg);
1373 AssertMsgReturn(!piSeg || *piSeg == NIL_RTDBGSEGIDX || *piSeg <= RTDBGSEGIDX_LAST, ("%#x\n", *piSeg), VERR_DBG_SPECIAL_SEGMENT);
1374
1375 /*
1376 * Do the deed.
1377 */
1378 RTDBGMOD_LOCK(pDbgMod);
1379 int rc = pDbgMod->pDbgVt->pfnSegmentAdd(pDbgMod, uRva, cb, pszName, cchName, fFlags, piSeg);
1380 RTDBGMOD_UNLOCK(pDbgMod);
1381
1382 return rc;
1383
1384}
1385RT_EXPORT_SYMBOL(RTDbgModSegmentAdd);
1386
1387
1388RTDECL(RTDBGSEGIDX) RTDbgModSegmentCount(RTDBGMOD hDbgMod)
1389{
1390 PRTDBGMODINT pDbgMod = hDbgMod;
1391 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NIL_RTDBGSEGIDX);
1392 RTDBGMOD_LOCK(pDbgMod);
1393
1394 RTDBGSEGIDX cSegs = pDbgMod->pDbgVt->pfnSegmentCount(pDbgMod);
1395
1396 RTDBGMOD_UNLOCK(pDbgMod);
1397 return cSegs;
1398}
1399RT_EXPORT_SYMBOL(RTDbgModSegmentCount);
1400
1401
1402RTDECL(int) RTDbgModSegmentByIndex(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
1403{
1404 AssertMsgReturn(iSeg <= RTDBGSEGIDX_LAST, ("%#x\n", iSeg), VERR_DBG_SPECIAL_SEGMENT);
1405 PRTDBGMODINT pDbgMod = hDbgMod;
1406 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1407 RTDBGMOD_LOCK(pDbgMod);
1408
1409 int rc = pDbgMod->pDbgVt->pfnSegmentByIndex(pDbgMod, iSeg, pSegInfo);
1410
1411 RTDBGMOD_UNLOCK(pDbgMod);
1412 return rc;
1413}
1414RT_EXPORT_SYMBOL(RTDbgModSegmentByIndex);
1415
1416
1417RTDECL(RTUINTPTR) RTDbgModSegmentSize(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg)
1418{
1419 if (iSeg == RTDBGSEGIDX_RVA)
1420 return RTDbgModImageSize(hDbgMod);
1421 RTDBGSEGMENT SegInfo;
1422 int rc = RTDbgModSegmentByIndex(hDbgMod, iSeg, &SegInfo);
1423 return RT_SUCCESS(rc) ? SegInfo.cb : RTUINTPTR_MAX;
1424}
1425RT_EXPORT_SYMBOL(RTDbgModSegmentSize);
1426
1427
1428RTDECL(RTUINTPTR) RTDbgModSegmentRva(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg)
1429{
1430 RTDBGSEGMENT SegInfo;
1431 int rc = RTDbgModSegmentByIndex(hDbgMod, iSeg, &SegInfo);
1432 return RT_SUCCESS(rc) ? SegInfo.uRva : RTUINTPTR_MAX;
1433}
1434RT_EXPORT_SYMBOL(RTDbgModSegmentRva);
1435
1436
1437RTDECL(int) RTDbgModSymbolAdd(RTDBGMOD hDbgMod, const char *pszSymbol, RTDBGSEGIDX iSeg, RTUINTPTR off,
1438 RTUINTPTR cb, uint32_t fFlags, uint32_t *piOrdinal)
1439{
1440 /*
1441 * Validate input.
1442 */
1443 PRTDBGMODINT pDbgMod = hDbgMod;
1444 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1445 AssertPtr(pszSymbol);
1446 size_t cchSymbol = strlen(pszSymbol);
1447 AssertReturn(cchSymbol, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
1448 AssertReturn(cchSymbol < RTDBG_SYMBOL_NAME_LENGTH, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
1449 AssertMsgReturn( iSeg <= RTDBGSEGIDX_LAST
1450 || ( iSeg >= RTDBGSEGIDX_SPECIAL_FIRST
1451 && iSeg <= RTDBGSEGIDX_SPECIAL_LAST),
1452 ("%#x\n", iSeg),
1453 VERR_DBG_INVALID_SEGMENT_INDEX);
1454 AssertMsgReturn(off + cb >= off, ("off=%RTptr cb=%RTptr\n", off, cb), VERR_DBG_ADDRESS_WRAP);
1455 AssertReturn(!fFlags, VERR_INVALID_PARAMETER); /* currently reserved. */
1456
1457 RTDBGMOD_LOCK(pDbgMod);
1458
1459 /*
1460 * Convert RVAs.
1461 */
1462 if (iSeg == RTDBGSEGIDX_RVA)
1463 {
1464 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
1465 if (iSeg == NIL_RTDBGSEGIDX)
1466 {
1467 RTDBGMOD_UNLOCK(pDbgMod);
1468 return VERR_DBG_INVALID_RVA;
1469 }
1470 }
1471
1472 /*
1473 * Get down to business.
1474 */
1475 int rc = pDbgMod->pDbgVt->pfnSymbolAdd(pDbgMod, pszSymbol, cchSymbol, iSeg, off, cb, fFlags, piOrdinal);
1476
1477 RTDBGMOD_UNLOCK(pDbgMod);
1478 return rc;
1479}
1480RT_EXPORT_SYMBOL(RTDbgModSymbolAdd);
1481
1482
1483RTDECL(uint32_t) RTDbgModSymbolCount(RTDBGMOD hDbgMod)
1484{
1485 PRTDBGMODINT pDbgMod = hDbgMod;
1486 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
1487 RTDBGMOD_LOCK(pDbgMod);
1488
1489 uint32_t cSymbols = pDbgMod->pDbgVt->pfnSymbolCount(pDbgMod);
1490
1491 RTDBGMOD_UNLOCK(pDbgMod);
1492 return cSymbols;
1493}
1494RT_EXPORT_SYMBOL(RTDbgModSymbolCount);
1495
1496
1497RTDECL(int) RTDbgModSymbolByOrdinal(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
1498{
1499 PRTDBGMODINT pDbgMod = hDbgMod;
1500 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1501 RTDBGMOD_LOCK(pDbgMod);
1502
1503 int rc = pDbgMod->pDbgVt->pfnSymbolByOrdinal(pDbgMod, iOrdinal, pSymInfo);
1504
1505 RTDBGMOD_UNLOCK(pDbgMod);
1506 return rc;
1507}
1508RT_EXPORT_SYMBOL(RTDbgModSymbolByOrdinal);
1509
1510
1511RTDECL(int) RTDbgModSymbolByOrdinalA(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGSYMBOL *ppSymInfo)
1512{
1513 AssertPtr(ppSymInfo);
1514 *ppSymInfo = NULL;
1515
1516 PRTDBGSYMBOL pSymInfo = RTDbgSymbolAlloc();
1517 if (!pSymInfo)
1518 return VERR_NO_MEMORY;
1519
1520 int rc = RTDbgModSymbolByOrdinal(hDbgMod, iOrdinal, pSymInfo);
1521
1522 if (RT_SUCCESS(rc))
1523 *ppSymInfo = pSymInfo;
1524 else
1525 RTDbgSymbolFree(pSymInfo);
1526 return rc;
1527}
1528RT_EXPORT_SYMBOL(RTDbgModSymbolByOrdinalA);
1529
1530
1531RTDECL(int) RTDbgModSymbolByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
1532 PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
1533{
1534 /*
1535 * Validate input.
1536 */
1537 PRTDBGMODINT pDbgMod = hDbgMod;
1538 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1539 AssertPtrNull(poffDisp);
1540 AssertPtr(pSymInfo);
1541 AssertReturn(!(fFlags & ~RTDBGSYMADDR_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
1542
1543 RTDBGMOD_LOCK(pDbgMod);
1544
1545 /*
1546 * Convert RVAs.
1547 */
1548 if (iSeg == RTDBGSEGIDX_RVA)
1549 {
1550 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
1551 if (iSeg == NIL_RTDBGSEGIDX)
1552 {
1553 RTDBGMOD_UNLOCK(pDbgMod);
1554 return VERR_DBG_INVALID_RVA;
1555 }
1556 }
1557
1558 /*
1559 * Get down to business.
1560 */
1561 int rc = pDbgMod->pDbgVt->pfnSymbolByAddr(pDbgMod, iSeg, off, fFlags, poffDisp, pSymInfo);
1562
1563 RTDBGMOD_UNLOCK(pDbgMod);
1564 return rc;
1565}
1566RT_EXPORT_SYMBOL(RTDbgModSymbolByAddr);
1567
1568
1569RTDECL(int) RTDbgModSymbolByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
1570 PRTINTPTR poffDisp, PRTDBGSYMBOL *ppSymInfo)
1571{
1572 AssertPtr(ppSymInfo);
1573 *ppSymInfo = NULL;
1574
1575 PRTDBGSYMBOL pSymInfo = RTDbgSymbolAlloc();
1576 if (!pSymInfo)
1577 return VERR_NO_MEMORY;
1578
1579 int rc = RTDbgModSymbolByAddr(hDbgMod, iSeg, off, fFlags, poffDisp, pSymInfo);
1580
1581 if (RT_SUCCESS(rc))
1582 *ppSymInfo = pSymInfo;
1583 else
1584 RTDbgSymbolFree(pSymInfo);
1585 return rc;
1586}
1587RT_EXPORT_SYMBOL(RTDbgModSymbolByAddrA);
1588
1589
1590RTDECL(int) RTDbgModSymbolByName(RTDBGMOD hDbgMod, const char *pszSymbol, PRTDBGSYMBOL pSymInfo)
1591{
1592 /*
1593 * Validate input.
1594 */
1595 PRTDBGMODINT pDbgMod = hDbgMod;
1596 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1597 AssertPtr(pszSymbol);
1598 size_t cchSymbol = strlen(pszSymbol);
1599 AssertReturn(cchSymbol, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
1600 AssertReturn(cchSymbol < RTDBG_SYMBOL_NAME_LENGTH, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
1601 AssertPtr(pSymInfo);
1602
1603 /*
1604 * Make the query.
1605 */
1606 RTDBGMOD_LOCK(pDbgMod);
1607 int rc = pDbgMod->pDbgVt->pfnSymbolByName(pDbgMod, pszSymbol, cchSymbol, pSymInfo);
1608 RTDBGMOD_UNLOCK(pDbgMod);
1609
1610 return rc;
1611}
1612RT_EXPORT_SYMBOL(RTDbgModSymbolByName);
1613
1614
1615RTDECL(int) RTDbgModSymbolByNameA(RTDBGMOD hDbgMod, const char *pszSymbol, PRTDBGSYMBOL *ppSymInfo)
1616{
1617 AssertPtr(ppSymInfo);
1618 *ppSymInfo = NULL;
1619
1620 PRTDBGSYMBOL pSymInfo = RTDbgSymbolAlloc();
1621 if (!pSymInfo)
1622 return VERR_NO_MEMORY;
1623
1624 int rc = RTDbgModSymbolByName(hDbgMod, pszSymbol, pSymInfo);
1625
1626 if (RT_SUCCESS(rc))
1627 *ppSymInfo = pSymInfo;
1628 else
1629 RTDbgSymbolFree(pSymInfo);
1630 return rc;
1631}
1632RT_EXPORT_SYMBOL(RTDbgModSymbolByNameA);
1633
1634
1635RTDECL(int) RTDbgModLineAdd(RTDBGMOD hDbgMod, const char *pszFile, uint32_t uLineNo,
1636 RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t *piOrdinal)
1637{
1638 /*
1639 * Validate input.
1640 */
1641 PRTDBGMODINT pDbgMod = hDbgMod;
1642 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1643 AssertPtr(pszFile);
1644 size_t cchFile = strlen(pszFile);
1645 AssertReturn(cchFile, VERR_DBG_FILE_NAME_OUT_OF_RANGE);
1646 AssertReturn(cchFile < RTDBG_FILE_NAME_LENGTH, VERR_DBG_FILE_NAME_OUT_OF_RANGE);
1647 AssertMsgReturn( iSeg <= RTDBGSEGIDX_LAST
1648 || iSeg == RTDBGSEGIDX_RVA,
1649 ("%#x\n", iSeg),
1650 VERR_DBG_INVALID_SEGMENT_INDEX);
1651 AssertReturn(uLineNo > 0 && uLineNo < UINT32_MAX, VERR_INVALID_PARAMETER);
1652
1653 RTDBGMOD_LOCK(pDbgMod);
1654
1655 /*
1656 * Convert RVAs.
1657 */
1658 if (iSeg == RTDBGSEGIDX_RVA)
1659 {
1660 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
1661 if (iSeg == NIL_RTDBGSEGIDX)
1662 {
1663 RTDBGMOD_UNLOCK(pDbgMod);
1664 return VERR_DBG_INVALID_RVA;
1665 }
1666 }
1667
1668 /*
1669 * Get down to business.
1670 */
1671 int rc = pDbgMod->pDbgVt->pfnLineAdd(pDbgMod, pszFile, cchFile, uLineNo, iSeg, off, piOrdinal);
1672
1673 RTDBGMOD_UNLOCK(pDbgMod);
1674 return rc;
1675}
1676RT_EXPORT_SYMBOL(RTDbgModLineAdd);
1677
1678
1679RTDECL(uint32_t) RTDbgModLineCount(RTDBGMOD hDbgMod)
1680{
1681 PRTDBGMODINT pDbgMod = hDbgMod;
1682 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
1683 RTDBGMOD_LOCK(pDbgMod);
1684
1685 uint32_t cLineNumbers = pDbgMod->pDbgVt->pfnLineCount(pDbgMod);
1686
1687 RTDBGMOD_UNLOCK(pDbgMod);
1688 return cLineNumbers;
1689}
1690RT_EXPORT_SYMBOL(RTDbgModLineCount);
1691
1692
1693RTDECL(int) RTDbgModLineByOrdinal(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
1694{
1695 PRTDBGMODINT pDbgMod = hDbgMod;
1696 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1697 RTDBGMOD_LOCK(pDbgMod);
1698
1699 int rc = pDbgMod->pDbgVt->pfnLineByOrdinal(pDbgMod, iOrdinal, pLineInfo);
1700
1701 RTDBGMOD_UNLOCK(pDbgMod);
1702 return rc;
1703}
1704RT_EXPORT_SYMBOL(RTDbgModLineByOrdinal);
1705
1706
1707RTDECL(int) RTDbgModLineByOrdinalA(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGLINE *ppLineInfo)
1708{
1709 AssertPtr(ppLineInfo);
1710 *ppLineInfo = NULL;
1711
1712 PRTDBGLINE pLineInfo = RTDbgLineAlloc();
1713 if (!pLineInfo)
1714 return VERR_NO_MEMORY;
1715
1716 int rc = RTDbgModLineByOrdinal(hDbgMod, iOrdinal, pLineInfo);
1717
1718 if (RT_SUCCESS(rc))
1719 *ppLineInfo = pLineInfo;
1720 else
1721 RTDbgLineFree(pLineInfo);
1722 return rc;
1723}
1724RT_EXPORT_SYMBOL(RTDbgModLineByOrdinalA);
1725
1726
1727RTDECL(int) RTDbgModLineByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
1728{
1729 /*
1730 * Validate input.
1731 */
1732 PRTDBGMODINT pDbgMod = hDbgMod;
1733 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1734 AssertPtrNull(poffDisp);
1735 AssertPtr(pLineInfo);
1736
1737 RTDBGMOD_LOCK(pDbgMod);
1738
1739 /*
1740 * Convert RVAs.
1741 */
1742 if (iSeg == RTDBGSEGIDX_RVA)
1743 {
1744 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
1745 if (iSeg == NIL_RTDBGSEGIDX)
1746 {
1747 RTDBGMOD_UNLOCK(pDbgMod);
1748 return VERR_DBG_INVALID_RVA;
1749 }
1750 }
1751
1752 int rc = pDbgMod->pDbgVt->pfnLineByAddr(pDbgMod, iSeg, off, poffDisp, pLineInfo);
1753
1754 RTDBGMOD_UNLOCK(pDbgMod);
1755 return rc;
1756}
1757RT_EXPORT_SYMBOL(RTDbgModLineByAddr);
1758
1759
1760RTDECL(int) RTDbgModLineByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE *ppLineInfo)
1761{
1762 AssertPtr(ppLineInfo);
1763 *ppLineInfo = NULL;
1764
1765 PRTDBGLINE pLineInfo = RTDbgLineAlloc();
1766 if (!pLineInfo)
1767 return VERR_NO_MEMORY;
1768
1769 int rc = RTDbgModLineByAddr(hDbgMod, iSeg, off, poffDisp, pLineInfo);
1770
1771 if (RT_SUCCESS(rc))
1772 *ppLineInfo = pLineInfo;
1773 else
1774 RTDbgLineFree(pLineInfo);
1775 return rc;
1776}
1777RT_EXPORT_SYMBOL(RTDbgModLineByAddrA);
1778
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