VirtualBox

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

最後變更 在這個檔案從41493是 41493,由 vboxsync 提交於 13 年 前

RTDbg*SymbolByAddr*: Added a flag parameter.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 52.4 KB
 
1/* $Id: dbgmod.cpp 41493 2012-05-30 13:47:41Z vboxsync $ */
2/** @file
3 * IPRT - Debug Module Interpreter.
4 */
5
6/*
7 * Copyright (C) 2009 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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#include <iprt/dbg.h>
32#include "internal/iprt.h"
33
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/avl.h>
37#include <iprt/err.h>
38#include <iprt/initterm.h>
39#include <iprt/mem.h>
40#include <iprt/once.h>
41#include <iprt/param.h>
42#include <iprt/path.h>
43#include <iprt/semaphore.h>
44#include <iprt/strcache.h>
45#include <iprt/string.h>
46#include "internal/dbgmod.h"
47#include "internal/magics.h"
48
49
50/*******************************************************************************
51* Structures and Typedefs *
52*******************************************************************************/
53/** Debug info interpreter registration record. */
54typedef struct RTDBGMODREGDBG
55{
56 /** Pointer to the next record. */
57 struct RTDBGMODREGDBG *pNext;
58 /** Pointer to the virtual function table for the interpreter. */
59 PCRTDBGMODVTDBG pVt;
60 /** Usage counter. */
61 uint32_t volatile cUsers;
62} RTDBGMODREGDBG;
63typedef RTDBGMODREGDBG *PRTDBGMODREGDBG;
64
65/** Image interpreter registration record. */
66typedef struct RTDBGMODREGIMG
67{
68 /** Pointer to the next record. */
69 struct RTDBGMODREGIMG *pNext;
70 /** Pointer to the virtual function table for the interpreter. */
71 PCRTDBGMODVTIMG pVt;
72 /** Usage counter. */
73 uint32_t volatile cUsers;
74} RTDBGMODREGIMG;
75typedef RTDBGMODREGIMG *PRTDBGMODREGIMG;
76
77
78/*******************************************************************************
79* Defined Constants And Macros *
80*******************************************************************************/
81/** Validates a debug module handle and returns rc if not valid. */
82#define RTDBGMOD_VALID_RETURN_RC(pDbgMod, rc) \
83 do { \
84 AssertPtrReturn((pDbgMod), (rc)); \
85 AssertReturn((pDbgMod)->u32Magic == RTDBGMOD_MAGIC, (rc)); \
86 AssertReturn((pDbgMod)->cRefs > 0, (rc)); \
87 } while (0)
88
89/** Locks the debug module. */
90#define RTDBGMOD_LOCK(pDbgMod) \
91 do { \
92 int rcLock = RTCritSectEnter(&(pDbgMod)->CritSect); \
93 AssertRC(rcLock); \
94 } while (0)
95
96/** Unlocks the debug module. */
97#define RTDBGMOD_UNLOCK(pDbgMod) \
98 do { \
99 int rcLock = RTCritSectLeave(&(pDbgMod)->CritSect); \
100 AssertRC(rcLock); \
101 } while (0)
102
103
104/*******************************************************************************
105* Global Variables *
106*******************************************************************************/
107/** Init once object for lazy registration of the built-in image and debug
108 * info interpreters. */
109static RTONCE g_rtDbgModOnce = RTONCE_INITIALIZER;
110/** Read/Write semaphore protecting the list of registered interpreters. */
111static RTSEMRW g_hDbgModRWSem = NIL_RTSEMRW;
112/** List of registered image interpreters. */
113static PRTDBGMODREGIMG g_pImgHead;
114/** List of registered debug infor interpreters. */
115static PRTDBGMODREGDBG g_pDbgHead;
116/** String cache for the debug info interpreters.
117 * RTSTRCACHE is thread safe. */
118DECLHIDDEN(RTSTRCACHE) g_hDbgModStrCache = NIL_RTSTRCACHE;
119
120
121
122/**
123 * Cleanup debug info interpreter globals.
124 *
125 * @param enmReason The cause of the termination.
126 * @param iStatus The meaning of this depends on enmReason.
127 * @param pvUser User argument, unused.
128 */
129static DECLCALLBACK(void) rtDbgModTermCallback(RTTERMREASON enmReason, int32_t iStatus, void *pvUser)
130{
131 NOREF(iStatus); NOREF(pvUser);
132 if (enmReason == RTTERMREASON_UNLOAD)
133 {
134 RTSemRWDestroy(g_hDbgModRWSem);
135 g_hDbgModRWSem = NIL_RTSEMRW;
136
137 RTStrCacheDestroy(g_hDbgModStrCache);
138 g_hDbgModStrCache = NIL_RTSTRCACHE;
139
140 PRTDBGMODREGDBG pDbg = g_pDbgHead;
141 g_pDbgHead = NULL;
142 while (pDbg)
143 {
144 PRTDBGMODREGDBG pNext = pDbg->pNext;
145 AssertMsg(pDbg->cUsers == 0, ("%#x %s\n", pDbg->cUsers, pDbg->pVt->pszName));
146 RTMemFree(pDbg);
147 pDbg = pNext;
148 }
149
150 PRTDBGMODREGIMG pImg = g_pImgHead;
151 g_pImgHead = NULL;
152 while (pImg)
153 {
154 PRTDBGMODREGIMG pNext = pImg->pNext;
155 AssertMsg(pImg->cUsers == 0, ("%#x %s\n", pImg->cUsers, pImg->pVt->pszName));
156 RTMemFree(pImg);
157 pImg = pNext;
158 }
159 }
160}
161
162
163/**
164 * Internal worker for register a debug interpreter.
165 *
166 * Called while owning the write lock or when locking isn't required.
167 *
168 * @returns IPRT status code.
169 * @retval VERR_NO_MEMORY
170 * @retval VERR_ALREADY_EXISTS
171 *
172 * @param pVt The virtual function table of the debug
173 * module interpreter.
174 */
175static int rtDbgModDebugInterpreterRegister(PCRTDBGMODVTDBG pVt)
176{
177 /*
178 * Search or duplicate registration.
179 */
180 PRTDBGMODREGDBG pPrev = NULL;
181 for (PRTDBGMODREGDBG pCur = g_pDbgHead; pCur; pCur = pCur->pNext)
182 {
183 if (pCur->pVt == pVt)
184 return VERR_ALREADY_EXISTS;
185 if (!strcmp(pCur->pVt->pszName, pVt->pszName))
186 return VERR_ALREADY_EXISTS;
187 pPrev = pCur;
188 }
189
190 /*
191 * Create a new record and add it to the end of the list.
192 */
193 PRTDBGMODREGDBG pReg = (PRTDBGMODREGDBG)RTMemAlloc(sizeof(*pReg));
194 if (!pReg)
195 return VERR_NO_MEMORY;
196 pReg->pVt = pVt;
197 pReg->cUsers = 0;
198 pReg->pNext = NULL;
199 if (pPrev)
200 pPrev->pNext = pReg;
201 else
202 g_pDbgHead = pReg;
203 return VINF_SUCCESS;
204}
205
206
207/**
208 * Internal worker for register a image interpreter.
209 *
210 * Called while owning the write lock or when locking isn't required.
211 *
212 * @returns IPRT status code.
213 * @retval VERR_NO_MEMORY
214 * @retval VERR_ALREADY_EXISTS
215 *
216 * @param pVt The virtual function table of the image
217 * interpreter.
218 */
219static int rtDbgModImageInterpreterRegister(PCRTDBGMODVTIMG pVt)
220{
221 /*
222 * Search or duplicate registration.
223 */
224 PRTDBGMODREGIMG pPrev = NULL;
225 for (PRTDBGMODREGIMG pCur = g_pImgHead; pCur; pCur = pCur->pNext)
226 {
227 if (pCur->pVt == pVt)
228 return VERR_ALREADY_EXISTS;
229 if (!strcmp(pCur->pVt->pszName, pVt->pszName))
230 return VERR_ALREADY_EXISTS;
231 pPrev = pCur;
232 }
233
234 /*
235 * Create a new record and add it to the end of the list.
236 */
237 PRTDBGMODREGIMG pReg = (PRTDBGMODREGIMG)RTMemAlloc(sizeof(*pReg));
238 if (!pReg)
239 return VERR_NO_MEMORY;
240 pReg->pVt = pVt;
241 pReg->cUsers = 0;
242 pReg->pNext = NULL;
243 if (pPrev)
244 pPrev->pNext = pReg;
245 else
246 g_pImgHead = pReg;
247 return VINF_SUCCESS;
248}
249
250
251/**
252 * Do-once callback that initializes the read/write semaphore and registers
253 * the built-in interpreters.
254 *
255 * @returns IPRT status code.
256 * @param pvUser1 NULL.
257 * @param pvUser2 NULL.
258 */
259static DECLCALLBACK(int) rtDbgModInitOnce(void *pvUser1, void *pvUser2)
260{
261 NOREF(pvUser1); NOREF(pvUser2);
262
263 /*
264 * Create the semaphore and string cache.
265 */
266 int rc = RTSemRWCreate(&g_hDbgModRWSem);
267 AssertRCReturn(rc, rc);
268
269 rc = RTStrCacheCreate(&g_hDbgModStrCache, "RTDBGMOD");
270 if (RT_SUCCESS(rc))
271 {
272 /*
273 * Register the interpreters.
274 */
275 rc = rtDbgModDebugInterpreterRegister(&g_rtDbgModVtDbgNm);
276 if (RT_SUCCESS(rc))
277 rc = rtDbgModDebugInterpreterRegister(&g_rtDbgModVtDbgDwarf);
278 if (RT_SUCCESS(rc))
279 rc = rtDbgModImageInterpreterRegister(&g_rtDbgModVtImgLdr);
280 if (RT_SUCCESS(rc))
281 {
282 /*
283 * Finally, register the IPRT cleanup callback.
284 */
285 rc = RTTermRegisterCallback(rtDbgModTermCallback, NULL);
286 if (RT_SUCCESS(rc))
287 return VINF_SUCCESS;
288
289 /* bail out: use the termination callback. */
290 }
291 }
292 else
293 g_hDbgModStrCache = NIL_RTSTRCACHE;
294 rtDbgModTermCallback(RTTERMREASON_UNLOAD, 0, NULL);
295 return rc;
296}
297
298
299DECLINLINE(int) rtDbgModLazyInit(void)
300{
301 return RTOnce(&g_rtDbgModOnce, rtDbgModInitOnce, NULL, NULL);
302}
303
304
305/**
306 * Creates a module based on the default debug info container.
307 *
308 * This can be used to manually load a module and its symbol. The primary user
309 * group is the debug info interpreters, which use this API to create an
310 * efficient debug info container behind the scenes and forward all queries to
311 * it once the info has been loaded.
312 *
313 * @returns IPRT status code.
314 *
315 * @param phDbgMod Where to return the module handle.
316 * @param pszName The name of the module (mandatory).
317 * @param cbSeg The size of initial segment. If zero, segments will
318 * have to be added manually using RTDbgModSegmentAdd.
319 * @param fFlags Flags reserved for future extensions, MBZ for now.
320 */
321RTDECL(int) RTDbgModCreate(PRTDBGMOD phDbgMod, const char *pszName, RTUINTPTR cbSeg, uint32_t fFlags)
322{
323 /*
324 * Input validation and lazy initialization.
325 */
326 AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
327 *phDbgMod = NIL_RTDBGMOD;
328 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
329 AssertReturn(*pszName, VERR_INVALID_PARAMETER);
330 AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER);
331
332 int rc = rtDbgModLazyInit();
333 if (RT_FAILURE(rc))
334 return rc;
335
336 /*
337 * Allocate a new module instance.
338 */
339 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
340 if (!pDbgMod)
341 return VERR_NO_MEMORY;
342 pDbgMod->u32Magic = RTDBGMOD_MAGIC;
343 pDbgMod->cRefs = 1;
344 rc = RTCritSectInit(&pDbgMod->CritSect);
345 if (RT_SUCCESS(rc))
346 {
347 pDbgMod->pszName = RTStrCacheEnter(g_hDbgModStrCache, pszName);
348 if (pDbgMod->pszName)
349 {
350 rc = rtDbgModContainerCreate(pDbgMod, cbSeg);
351 if (RT_SUCCESS(rc))
352 {
353 *phDbgMod = pDbgMod;
354 return rc;
355 }
356 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
357 }
358 RTCritSectDelete(&pDbgMod->CritSect);
359 }
360
361 RTMemFree(pDbgMod);
362 return rc;
363}
364RT_EXPORT_SYMBOL(RTDbgModCreate);
365
366
367RTDECL(int) RTDbgModCreateDeferred(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName,
368 RTUINTPTR cb, uint32_t fFlags)
369{
370 NOREF(phDbgMod); NOREF(pszFilename); NOREF(pszName); NOREF(cb); NOREF(fFlags);
371 return VERR_NOT_IMPLEMENTED;
372}
373RT_EXPORT_SYMBOL(RTDbgModCreateDeferred);
374
375
376RTDECL(int) RTDbgModCreateFromImage(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName, uint32_t fFlags)
377{
378 /*
379 * Input validation and lazy initialization.
380 */
381 AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
382 *phDbgMod = NIL_RTDBGMOD;
383 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
384 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
385 AssertPtrNullReturn(pszName, VERR_INVALID_POINTER);
386 AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER);
387
388 int rc = rtDbgModLazyInit();
389 if (RT_FAILURE(rc))
390 return rc;
391
392 if (!pszName)
393 pszName = RTPathFilename(pszFilename);
394
395 /*
396 * Allocate a new module instance.
397 */
398 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
399 if (!pDbgMod)
400 return VERR_NO_MEMORY;
401 pDbgMod->u32Magic = RTDBGMOD_MAGIC;
402 pDbgMod->cRefs = 1;
403 rc = RTCritSectInit(&pDbgMod->CritSect);
404 if (RT_SUCCESS(rc))
405 {
406 pDbgMod->pszName = RTStrCacheEnter(g_hDbgModStrCache, pszName);
407 if (pDbgMod->pszName)
408 {
409 pDbgMod->pszImgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
410 if (pDbgMod->pszImgFile)
411 {
412 /*
413 * Find an image reader which groks the file.
414 */
415 rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
416 if (RT_SUCCESS(rc))
417 {
418 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
419 PRTDBGMODREGIMG pImg;
420 for (pImg = g_pImgHead; pImg; pImg = pImg->pNext)
421 {
422 pDbgMod->pImgVt = pImg->pVt;
423 pDbgMod->pvImgPriv = NULL;
424 rc = pImg->pVt->pfnTryOpen(pDbgMod);
425 if (RT_SUCCESS(rc))
426 {
427 /*
428 * Find a debug info interpreter.
429 */
430 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
431 for (PRTDBGMODREGDBG pDbg = g_pDbgHead; pDbg; pDbg = pDbg->pNext)
432 {
433 pDbgMod->pDbgVt = pDbg->pVt;
434 pDbgMod->pvDbgPriv = NULL;
435 rc = pDbg->pVt->pfnTryOpen(pDbgMod);
436 if (RT_SUCCESS(rc))
437 {
438 /*
439 * That's it!
440 */
441 ASMAtomicIncU32(&pDbg->cUsers);
442 ASMAtomicIncU32(&pImg->cUsers);
443 RTSemRWReleaseRead(g_hDbgModRWSem);
444
445 *phDbgMod = pDbgMod;
446 return rc;
447 }
448 }
449
450 /*
451 * Image detected, but found no debug info we were
452 * able to understand.
453 */
454 /** @todo Fall back on exported symbols! */
455 pDbgMod->pImgVt->pfnClose(pDbgMod);
456 break;
457 }
458 }
459
460 /*
461 * Could it be a file containing raw debug info?
462 */
463 if (!pImg)
464 {
465 pDbgMod->pImgVt = NULL;
466 pDbgMod->pvImgPriv = NULL;
467 pDbgMod->pszDbgFile = pDbgMod->pszImgFile;
468 pDbgMod->pszImgFile = NULL;
469
470 for (PRTDBGMODREGDBG pDbg = g_pDbgHead; pDbg; pDbg = pDbg->pNext)
471 {
472 pDbgMod->pDbgVt = pDbg->pVt;
473 pDbgMod->pvDbgPriv = NULL;
474 rc = pDbg->pVt->pfnTryOpen(pDbgMod);
475 if (RT_SUCCESS(rc))
476 {
477 /*
478 * That's it!
479 */
480 ASMAtomicIncU32(&pDbg->cUsers);
481 RTSemRWReleaseRead(g_hDbgModRWSem);
482
483 *phDbgMod = pDbgMod;
484 return rc;
485 }
486 }
487
488 pDbgMod->pszImgFile = pDbgMod->pszDbgFile;
489 pDbgMod->pszDbgFile = NULL;
490 }
491
492 /* bail out */
493 RTSemRWReleaseRead(g_hDbgModRWSem);
494 }
495 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
496 }
497 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
498 }
499 RTCritSectDelete(&pDbgMod->CritSect);
500 }
501
502 RTMemFree(pDbgMod);
503 return rc;
504}
505RT_EXPORT_SYMBOL(RTDbgModCreateFromImage);
506
507
508RTDECL(int) RTDbgModCreateFromMap(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName,
509 RTUINTPTR uSubtrahend, uint32_t fFlags)
510{
511 /*
512 * Input validation and lazy initialization.
513 */
514 AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
515 *phDbgMod = NIL_RTDBGMOD;
516 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
517 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
518 AssertPtrNullReturn(pszName, VERR_INVALID_POINTER);
519 AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER);
520 AssertReturn(uSubtrahend == 0, VERR_NOT_IMPLEMENTED); /** @todo implement uSubtrahend. */
521
522 int rc = rtDbgModLazyInit();
523 if (RT_FAILURE(rc))
524 return rc;
525
526 if (!pszName)
527 pszName = RTPathFilename(pszFilename);
528
529 /*
530 * Allocate a new module instance.
531 */
532 PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
533 if (!pDbgMod)
534 return VERR_NO_MEMORY;
535 pDbgMod->u32Magic = RTDBGMOD_MAGIC;
536 pDbgMod->cRefs = 1;
537 rc = RTCritSectInit(&pDbgMod->CritSect);
538 if (RT_SUCCESS(rc))
539 {
540 pDbgMod->pszName = RTStrCacheEnter(g_hDbgModStrCache, pszName);
541 if (pDbgMod->pszName)
542 {
543 pDbgMod->pszDbgFile = RTStrCacheEnter(g_hDbgModStrCache, pszFilename);
544 if (pDbgMod->pszDbgFile)
545 {
546 /*
547 * Try the map file readers.
548 */
549 rc = RTSemRWRequestRead(g_hDbgModRWSem, RT_INDEFINITE_WAIT);
550 if (RT_SUCCESS(rc))
551 {
552 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
553 for (PRTDBGMODREGDBG pCur = g_pDbgHead; pCur; pCur = pCur->pNext)
554 {
555 if (pCur->pVt->fSupports & RT_DBGTYPE_MAP)
556 {
557 pDbgMod->pDbgVt = pCur->pVt;
558 pDbgMod->pvDbgPriv = NULL;
559 rc = pCur->pVt->pfnTryOpen(pDbgMod);
560 if (RT_SUCCESS(rc))
561 {
562 ASMAtomicIncU32(&pCur->cUsers);
563 RTSemRWReleaseRead(g_hDbgModRWSem);
564
565 *phDbgMod = pDbgMod;
566 return rc;
567 }
568 }
569 }
570
571 /* bail out */
572 RTSemRWReleaseRead(g_hDbgModRWSem);
573 }
574 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
575 }
576 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszDbgFile);
577 }
578 RTCritSectDelete(&pDbgMod->CritSect);
579 }
580
581 RTMemFree(pDbgMod);
582 return rc;
583}
584RT_EXPORT_SYMBOL(RTDbgModCreateFromMap);
585
586
587/**
588 * Destroys an module after the reference count has reached zero.
589 *
590 * @param pDbgMod The module instance.
591 */
592static void rtDbgModDestroy(PRTDBGMODINT pDbgMod)
593{
594 /*
595 * Close the debug info interpreter first, then the image interpret.
596 */
597 RTCritSectEnter(&pDbgMod->CritSect); /* paranoia */
598
599 if (pDbgMod->pDbgVt)
600 {
601 pDbgMod->pDbgVt->pfnClose(pDbgMod);
602 pDbgMod->pDbgVt = NULL;
603 pDbgMod->pvDbgPriv = NULL;
604 }
605
606 if (pDbgMod->pImgVt)
607 {
608 pDbgMod->pImgVt->pfnClose(pDbgMod);
609 pDbgMod->pImgVt = NULL;
610 pDbgMod->pvImgPriv = NULL;
611 }
612
613 /*
614 * Free the resources.
615 */
616 ASMAtomicWriteU32(&pDbgMod->u32Magic, ~RTDBGMOD_MAGIC);
617 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszName);
618 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszImgFile);
619 RTStrCacheRelease(g_hDbgModStrCache, pDbgMod->pszDbgFile);
620 RTCritSectLeave(&pDbgMod->CritSect); /* paranoia */
621 RTCritSectDelete(&pDbgMod->CritSect);
622 RTMemFree(pDbgMod);
623}
624
625
626/**
627 * Retains another reference to the module.
628 *
629 * @returns New reference count, UINT32_MAX on invalid handle (asserted).
630 *
631 * @param hDbgMod The module handle.
632 *
633 * @remarks Will not take any locks.
634 */
635RTDECL(uint32_t) RTDbgModRetain(RTDBGMOD hDbgMod)
636{
637 PRTDBGMODINT pDbgMod = hDbgMod;
638 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
639 return ASMAtomicIncU32(&pDbgMod->cRefs);
640}
641RT_EXPORT_SYMBOL(RTDbgModRetain);
642
643
644/**
645 * Release a reference to the module.
646 *
647 * When the reference count reaches zero, the module is destroyed.
648 *
649 * @returns New reference count, UINT32_MAX on invalid handle (asserted).
650 *
651 * @param hDbgMod The module handle. The NIL handle is quietly ignored
652 * and 0 is returned.
653 *
654 * @remarks Will not take any locks.
655 */
656RTDECL(uint32_t) RTDbgModRelease(RTDBGMOD hDbgMod)
657{
658 if (hDbgMod == NIL_RTDBGMOD)
659 return 0;
660 PRTDBGMODINT pDbgMod = hDbgMod;
661 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
662
663 uint32_t cRefs = ASMAtomicDecU32(&pDbgMod->cRefs);
664 if (!cRefs)
665 rtDbgModDestroy(pDbgMod);
666 return cRefs;
667}
668RT_EXPORT_SYMBOL(RTDbgModRelease);
669
670
671/**
672 * Gets the module name.
673 *
674 * @returns Pointer to a read only string containing the name.
675 *
676 * @param hDbgMod The module handle.
677 */
678RTDECL(const char *) RTDbgModName(RTDBGMOD hDbgMod)
679{
680 PRTDBGMODINT pDbgMod = hDbgMod;
681 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NULL);
682 return pDbgMod->pszName;
683}
684RT_EXPORT_SYMBOL(RTDbgModName);
685
686
687/**
688 * Converts an image relative address to a segment:offset address.
689 *
690 * @returns Segment index on success.
691 * NIL_RTDBGSEGIDX is returned if the module handle or the RVA are
692 * invalid.
693 *
694 * @param hDbgMod The module handle.
695 * @param uRva The image relative address to convert.
696 * @param poffSeg Where to return the segment offset. Optional.
697 */
698RTDECL(RTDBGSEGIDX) RTDbgModRvaToSegOff(RTDBGMOD hDbgMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
699{
700 PRTDBGMODINT pDbgMod = hDbgMod;
701 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NIL_RTDBGSEGIDX);
702 RTDBGMOD_LOCK(pDbgMod);
703
704 RTDBGSEGIDX iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, uRva, poffSeg);
705
706 RTDBGMOD_UNLOCK(pDbgMod);
707 return iSeg;
708}
709RT_EXPORT_SYMBOL(RTDbgModRvaToSegOff);
710
711
712/**
713 * Image size when mapped if segments are mapped adjacently.
714 *
715 * For ELF, PE, and Mach-O images this is (usually) a natural query, for LX and
716 * NE and such it's a bit odder and the answer may not make much sense for them.
717 *
718 * @returns Image mapped size.
719 * RTUINTPTR_MAX is returned if the handle is invalid.
720 *
721 * @param hDbgMod The module handle.
722 */
723RTDECL(RTUINTPTR) RTDbgModImageSize(RTDBGMOD hDbgMod)
724{
725 PRTDBGMODINT pDbgMod = hDbgMod;
726 RTDBGMOD_VALID_RETURN_RC(pDbgMod, RTUINTPTR_MAX);
727 RTDBGMOD_LOCK(pDbgMod);
728
729 RTUINTPTR cbImage = pDbgMod->pDbgVt->pfnImageSize(pDbgMod);
730
731 RTDBGMOD_UNLOCK(pDbgMod);
732 return cbImage;
733}
734RT_EXPORT_SYMBOL(RTDbgModImageSize);
735
736
737/**
738 * Gets the module tag value if any.
739 *
740 * @returns The tag. 0 if hDbgMod is invalid.
741 *
742 * @param hDbgMod The module handle.
743 */
744RTDECL(uint64_t) RTDbgModGetTag(RTDBGMOD hDbgMod)
745{
746 PRTDBGMODINT pDbgMod = hDbgMod;
747 RTDBGMOD_VALID_RETURN_RC(pDbgMod, 0);
748 return pDbgMod->uTag;
749}
750RT_EXPORT_SYMBOL(RTDbgModGetTag);
751
752
753/**
754 * Tags or untags the module.
755 *
756 * @returns IPRT status code.
757 * @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
758 *
759 * @param hDbgMod The module handle.
760 * @param uTag The tag value. The convention is that 0 is no tag
761 * and any other value means it's tagged. It's adviced
762 * to use some kind of unique number like an address
763 * (global or string cache for instance) to avoid
764 * collisions with other users
765 */
766RTDECL(int) RTDbgModSetTag(RTDBGMOD hDbgMod, uint64_t uTag)
767{
768 PRTDBGMODINT pDbgMod = hDbgMod;
769 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
770 RTDBGMOD_LOCK(pDbgMod);
771
772 pDbgMod->uTag = uTag;
773
774 RTDBGMOD_UNLOCK(pDbgMod);
775 return VINF_SUCCESS;
776}
777RT_EXPORT_SYMBOL(RTDbgModSetTag);
778
779
780/**
781 * Adds a segment to the module. Optional feature.
782 *
783 * This method is intended used for manually constructing debug info for a
784 * module. The main usage is from other debug info interpreters that want to
785 * avoid writing a debug info database and instead uses the standard container
786 * behind the scenes.
787 *
788 * @returns IPRT status code.
789 * @retval VERR_NOT_SUPPORTED if this feature isn't support by the debug info
790 * interpreter. This is a common return code.
791 * @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
792 * @retval VERR_DBG_ADDRESS_WRAP if uRva+cb wraps around.
793 * @retval VERR_DBG_SEGMENT_NAME_OUT_OF_RANGE if pszName is too short or long.
794 * @retval VERR_INVALID_PARAMETER if fFlags contains undefined flags.
795 * @retval VERR_DBG_SPECIAL_SEGMENT if *piSeg is a special segment.
796 * @retval VERR_DBG_INVALID_SEGMENT_INDEX if *piSeg doesn't meet expectations.
797 *
798 * @param hDbgMod The module handle.
799 * @param uRva The image relative address of the segment.
800 * @param cb The size of the segment.
801 * @param pszName The segment name. Does not normally need to be
802 * unique, although this is somewhat up to the
803 * debug interpreter to decide.
804 * @param fFlags Segment flags. Reserved for future used, MBZ.
805 * @param piSeg The segment index or NIL_RTDBGSEGIDX on input.
806 * The assigned segment index on successful return.
807 * Optional.
808 */
809RTDECL(int) RTDbgModSegmentAdd(RTDBGMOD hDbgMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName,
810 uint32_t fFlags, PRTDBGSEGIDX piSeg)
811{
812 /*
813 * Validate input.
814 */
815 PRTDBGMODINT pDbgMod = hDbgMod;
816 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
817 AssertMsgReturn(uRva + cb >= uRva, ("uRva=%RTptr cb=%RTptr\n", uRva, cb), VERR_DBG_ADDRESS_WRAP);
818 Assert(*pszName);
819 size_t cchName = strlen(pszName);
820 AssertReturn(cchName > 0, VERR_DBG_SEGMENT_NAME_OUT_OF_RANGE);
821 AssertReturn(cchName < RTDBG_SEGMENT_NAME_LENGTH, VERR_DBG_SEGMENT_NAME_OUT_OF_RANGE);
822 AssertMsgReturn(!fFlags, ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
823 AssertPtrNull(piSeg);
824 AssertMsgReturn(!piSeg || *piSeg == NIL_RTDBGSEGIDX || *piSeg <= RTDBGSEGIDX_LAST, ("%#x\n", *piSeg), VERR_DBG_SPECIAL_SEGMENT);
825
826 /*
827 * Do the deed.
828 */
829 RTDBGMOD_LOCK(pDbgMod);
830 int rc = pDbgMod->pDbgVt->pfnSegmentAdd(pDbgMod, uRva, cb, pszName, cchName, fFlags, piSeg);
831 RTDBGMOD_UNLOCK(pDbgMod);
832
833 return rc;
834
835}
836RT_EXPORT_SYMBOL(RTDbgModSegmentAdd);
837
838
839/**
840 * Gets the number of segments in the module.
841 *
842 * This is can be used to determine the range which can be passed to
843 * RTDbgModSegmentByIndex and derivatives.
844 *
845 * @returns The segment relative address.
846 * NIL_RTDBGSEGIDX if the handle is invalid.
847 *
848 * @param hDbgMod The module handle.
849 */
850RTDECL(RTDBGSEGIDX) RTDbgModSegmentCount(RTDBGMOD hDbgMod)
851{
852 PRTDBGMODINT pDbgMod = hDbgMod;
853 RTDBGMOD_VALID_RETURN_RC(pDbgMod, NIL_RTDBGSEGIDX);
854 RTDBGMOD_LOCK(pDbgMod);
855
856 RTDBGSEGIDX cSegs = pDbgMod->pDbgVt->pfnSegmentCount(pDbgMod);
857
858 RTDBGMOD_UNLOCK(pDbgMod);
859 return cSegs;
860}
861RT_EXPORT_SYMBOL(RTDbgModSegmentCount);
862
863
864/**
865 * Query information about a segment.
866 *
867 * This can be used together with RTDbgModSegmentCount to enumerate segments.
868 * The index starts a 0 and stops one below RTDbgModSegmentCount.
869 *
870 * @returns IPRT status code.
871 * @retval VERR_DBG_INVALID_SEGMENT_INDEX if iSeg is too high.
872 * @retval VERR_DBG_SPECIAL_SEGMENT if iSeg indicates a special segment.
873 * @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
874 *
875 * @param hDbgMod The module handle.
876 * @param iSeg The segment index. No special segments.
877 * @param pSegInfo Where to return the segment info. The
878 * RTDBGSEGMENT::Address member will be set to
879 * RTUINTPTR_MAX or the load address used at link time.
880 */
881RTDECL(int) RTDbgModSegmentByIndex(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
882{
883 AssertMsgReturn(iSeg <= RTDBGSEGIDX_LAST, ("%#x\n", iSeg), VERR_DBG_SPECIAL_SEGMENT);
884 PRTDBGMODINT pDbgMod = hDbgMod;
885 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
886 RTDBGMOD_LOCK(pDbgMod);
887
888 int rc = pDbgMod->pDbgVt->pfnSegmentByIndex(pDbgMod, iSeg, pSegInfo);
889
890 RTDBGMOD_UNLOCK(pDbgMod);
891 return rc;
892}
893RT_EXPORT_SYMBOL(RTDbgModSegmentByIndex);
894
895
896/**
897 * Gets the size of a segment.
898 *
899 * This is a just a wrapper around RTDbgModSegmentByIndex.
900 *
901 * @returns The segment size.
902 * RTUINTPTR_MAX is returned if either the handle and segment index are
903 * invalid.
904 *
905 * @param hDbgMod The module handle.
906 * @param iSeg The segment index. RTDBGSEGIDX_ABS is not allowed.
907 * If RTDBGSEGIDX_RVA is used, the functions returns
908 * the same value as RTDbgModImageSize.
909 */
910RTDECL(RTUINTPTR) RTDbgModSegmentSize(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg)
911{
912 if (iSeg == RTDBGSEGIDX_RVA)
913 return RTDbgModImageSize(hDbgMod);
914 RTDBGSEGMENT SegInfo;
915 int rc = RTDbgModSegmentByIndex(hDbgMod, iSeg, &SegInfo);
916 return RT_SUCCESS(rc) ? SegInfo.cb : RTUINTPTR_MAX;
917}
918RT_EXPORT_SYMBOL(RTDbgModSegmentSize);
919
920
921/**
922 * Gets the image relative address of a segment.
923 *
924 * This is a just a wrapper around RTDbgModSegmentByIndex.
925 *
926 * @returns The segment relative address.
927 * RTUINTPTR_MAX is returned if either the handle and segment index are
928 * invalid.
929 *
930 * @param hDbgMod The module handle.
931 * @param iSeg The segment index. No special segment indexes
932 * allowed (asserted).
933 */
934RTDECL(RTUINTPTR) RTDbgModSegmentRva(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg)
935{
936 RTDBGSEGMENT SegInfo;
937 int rc = RTDbgModSegmentByIndex(hDbgMod, iSeg, &SegInfo);
938 return RT_SUCCESS(rc) ? SegInfo.uRva : RTUINTPTR_MAX;
939}
940RT_EXPORT_SYMBOL(RTDbgModSegmentRva);
941
942
943/**
944 * Adds a line number to the module.
945 *
946 * @returns IPRT status code.
947 * @retval VERR_NOT_SUPPORTED if the module interpret doesn't support adding
948 * custom symbols. This is a common place occurrence.
949 * @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
950 * @retval VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE if the symbol name is too long or
951 * short.
952 * @retval VERR_DBG_INVALID_RVA if an image relative address is specified and
953 * it's not inside any of the segments defined by the module.
954 * @retval VERR_DBG_INVALID_SEGMENT_INDEX if the segment index isn't valid.
955 * @retval VERR_DBG_INVALID_SEGMENT_OFFSET if the segment offset is beyond the
956 * end of the segment.
957 * @retval VERR_DBG_ADDRESS_WRAP if off+cb wraps around.
958 * @retval VERR_INVALID_PARAMETER if the symbol flags sets undefined bits.
959 *
960 * @param hDbgMod The module handle.
961 * @param pszSymbol The symbol name.
962 * @param iSeg The segment index.
963 * @param off The segment offset.
964 * @param cb The size of the symbol. Can be zero, although this
965 * may depend somewhat on the debug interpreter.
966 * @param fFlags Symbol flags. Reserved for the future, MBZ.
967 * @param piOrdinal Where to return the symbol ordinal on success. If
968 * the interpreter doesn't do ordinals, this will be set to
969 * UINT32_MAX. Optional.
970 */
971RTDECL(int) RTDbgModSymbolAdd(RTDBGMOD hDbgMod, const char *pszSymbol, RTDBGSEGIDX iSeg, RTUINTPTR off,
972 RTUINTPTR cb, uint32_t fFlags, uint32_t *piOrdinal)
973{
974 /*
975 * Validate input.
976 */
977 PRTDBGMODINT pDbgMod = hDbgMod;
978 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
979 AssertPtr(pszSymbol);
980 size_t cchSymbol = strlen(pszSymbol);
981 AssertReturn(cchSymbol, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
982 AssertReturn(cchSymbol < RTDBG_SYMBOL_NAME_LENGTH, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
983 AssertMsgReturn( iSeg <= RTDBGSEGIDX_LAST
984 || ( iSeg >= RTDBGSEGIDX_SPECIAL_FIRST
985 && iSeg <= RTDBGSEGIDX_SPECIAL_LAST),
986 ("%#x\n", iSeg),
987 VERR_DBG_INVALID_SEGMENT_INDEX);
988 AssertMsgReturn(off + cb >= off, ("off=%RTptr cb=%RTptr\n", off, cb), VERR_DBG_ADDRESS_WRAP);
989 AssertReturn(!fFlags, VERR_INVALID_PARAMETER); /* currently reserved. */
990
991 RTDBGMOD_LOCK(pDbgMod);
992
993 /*
994 * Convert RVAs.
995 */
996 if (iSeg == RTDBGSEGIDX_RVA)
997 {
998 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
999 if (iSeg == NIL_RTDBGSEGIDX)
1000 {
1001 RTDBGMOD_UNLOCK(pDbgMod);
1002 return VERR_DBG_INVALID_RVA;
1003 }
1004 }
1005
1006 /*
1007 * Get down to business.
1008 */
1009 int rc = pDbgMod->pDbgVt->pfnSymbolAdd(pDbgMod, pszSymbol, cchSymbol, iSeg, off, cb, fFlags, piOrdinal);
1010
1011 RTDBGMOD_UNLOCK(pDbgMod);
1012 return rc;
1013}
1014RT_EXPORT_SYMBOL(RTDbgModSymbolAdd);
1015
1016
1017/**
1018 * Gets the symbol count.
1019 *
1020 * This can be used together wtih RTDbgModSymbolByOrdinal or
1021 * RTDbgModSymbolByOrdinalA to enumerate all the symbols.
1022 *
1023 * @returns The number of symbols in the module.
1024 * UINT32_MAX is returned if the module handle is invalid or some other
1025 * error occurs.
1026 *
1027 * @param hDbgMod The module handle.
1028 */
1029RTDECL(uint32_t) RTDbgModSymbolCount(RTDBGMOD hDbgMod)
1030{
1031 PRTDBGMODINT pDbgMod = hDbgMod;
1032 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
1033 RTDBGMOD_LOCK(pDbgMod);
1034
1035 uint32_t cSymbols = pDbgMod->pDbgVt->pfnSymbolCount(pDbgMod);
1036
1037 RTDBGMOD_UNLOCK(pDbgMod);
1038 return cSymbols;
1039}
1040RT_EXPORT_SYMBOL(RTDbgModSymbolCount);
1041
1042
1043/**
1044 * Queries symbol information by ordinal number.
1045 *
1046 * @returns IPRT status code.
1047 * @retval VERR_SYMBOL_NOT_FOUND if there is no symbol at the given number.
1048 * @retval VERR_DBG_NO_SYMBOLS if there aren't any symbols.
1049 * @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
1050 * @retval VERR_NOT_SUPPORTED if lookup by ordinal is not supported.
1051 *
1052 * @param hDbgMod The module handle.
1053 * @param iOrdinal The symbol ordinal number. 0-based. The highest
1054 * number is RTDbgModSymbolCount() - 1.
1055 * @param pSymInfo Where to store the symbol information.
1056 */
1057RTDECL(int) RTDbgModSymbolByOrdinal(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
1058{
1059 PRTDBGMODINT pDbgMod = hDbgMod;
1060 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1061 RTDBGMOD_LOCK(pDbgMod);
1062
1063 int rc = pDbgMod->pDbgVt->pfnSymbolByOrdinal(pDbgMod, iOrdinal, pSymInfo);
1064
1065 RTDBGMOD_UNLOCK(pDbgMod);
1066 return rc;
1067}
1068RT_EXPORT_SYMBOL(RTDbgModSymbolByOrdinal);
1069
1070
1071/**
1072 * Queries symbol information by ordinal number.
1073 *
1074 * @returns IPRT status code.
1075 * @retval VERR_DBG_NO_SYMBOLS if there aren't any symbols.
1076 * @retval VERR_NOT_SUPPORTED if lookup by ordinal is not supported.
1077 * @retval VERR_SYMBOL_NOT_FOUND if there is no symbol at the given number.
1078 * @retval VERR_NO_MEMORY if RTDbgSymbolAlloc fails.
1079 *
1080 * @param hDbgMod The module handle.
1081 * @param iOrdinal The symbol ordinal number. 0-based. The highest
1082 * number is RTDbgModSymbolCount() - 1.
1083 * @param ppSymInfo Where to store the pointer to the returned
1084 * symbol information. Always set. Free with
1085 * RTDbgSymbolFree.
1086 */
1087RTDECL(int) RTDbgModSymbolByOrdinalA(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGSYMBOL *ppSymInfo)
1088{
1089 AssertPtr(ppSymInfo);
1090 *ppSymInfo = NULL;
1091
1092 PRTDBGSYMBOL pSymInfo = RTDbgSymbolAlloc();
1093 if (!pSymInfo)
1094 return VERR_NO_MEMORY;
1095
1096 int rc = RTDbgModSymbolByOrdinal(hDbgMod, iOrdinal, pSymInfo);
1097
1098 if (RT_SUCCESS(rc))
1099 *ppSymInfo = pSymInfo;
1100 else
1101 RTDbgSymbolFree(pSymInfo);
1102 return rc;
1103}
1104RT_EXPORT_SYMBOL(RTDbgModSymbolByOrdinalA);
1105
1106
1107/**
1108 * Queries symbol information by address.
1109 *
1110 * The returned symbol is what the debug info interpreter considers the symbol
1111 * most applicable to the specified address. This usually means a symbol with an
1112 * address equal or lower than the requested.
1113 *
1114 * @returns IPRT status code.
1115 * @retval VERR_SYMBOL_NOT_FOUND if no suitable symbol was found.
1116 * @retval VERR_DBG_NO_SYMBOLS if there aren't any symbols.
1117 * @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
1118 * @retval VERR_DBG_INVALID_RVA if an image relative address is specified and
1119 * it's not inside any of the segments defined by the module.
1120 * @retval VERR_DBG_INVALID_SEGMENT_INDEX if the segment index isn't valid.
1121 * @retval VERR_DBG_INVALID_SEGMENT_OFFSET if the segment offset is beyond the
1122 * end of the segment.
1123 * @retval VERR_INVALID_PARAMETER if incorrect flags.
1124 *
1125 * @param hDbgMod The module handle.
1126 * @param iSeg The segment number.
1127 * @param off The offset into the segment.
1128 * @param fFlags Symbol search flags, see RTDBGSYMADDR_FLAGS_XXX.
1129 * @param poffDisp Where to store the distance between the
1130 * specified address and the returned symbol.
1131 * Optional.
1132 * @param pSymInfo Where to store the symbol information.
1133 */
1134RTDECL(int) RTDbgModSymbolByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
1135 PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
1136{
1137 /*
1138 * Validate input.
1139 */
1140 PRTDBGMODINT pDbgMod = hDbgMod;
1141 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1142 AssertPtrNull(poffDisp);
1143 AssertPtr(pSymInfo);
1144 AssertReturn(!(fFlags & ~RTDBGSYMADDR_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
1145
1146 RTDBGMOD_LOCK(pDbgMod);
1147
1148 /*
1149 * Convert RVAs.
1150 */
1151 if (iSeg == RTDBGSEGIDX_RVA)
1152 {
1153 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
1154 if (iSeg == NIL_RTDBGSEGIDX)
1155 {
1156 RTDBGMOD_UNLOCK(pDbgMod);
1157 return VERR_DBG_INVALID_RVA;
1158 }
1159 }
1160
1161 /*
1162 * Get down to business.
1163 */
1164 int rc = pDbgMod->pDbgVt->pfnSymbolByAddr(pDbgMod, iSeg, off, fFlags, poffDisp, pSymInfo);
1165
1166 RTDBGMOD_UNLOCK(pDbgMod);
1167 return rc;
1168}
1169RT_EXPORT_SYMBOL(RTDbgModSymbolByAddr);
1170
1171
1172/**
1173 * Queries symbol information by address.
1174 *
1175 * The returned symbol is what the debug info interpreter considers the symbol
1176 * most applicable to the specified address. This usually means a symbol with an
1177 * address equal or lower than the requested.
1178 *
1179 * @returns IPRT status code.
1180 * @retval VERR_SYMBOL_NOT_FOUND if no suitable symbol was found.
1181 * @retval VERR_DBG_NO_SYMBOLS if there aren't any symbols.
1182 * @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
1183 * @retval VERR_DBG_INVALID_RVA if an image relative address is specified and
1184 * it's not inside any of the segments defined by the module.
1185 * @retval VERR_DBG_INVALID_SEGMENT_INDEX if the segment index isn't valid.
1186 * @retval VERR_DBG_INVALID_SEGMENT_OFFSET if the segment offset is beyond the
1187 * end of the segment.
1188 * @retval VERR_NO_MEMORY if RTDbgSymbolAlloc fails.
1189 * @retval VERR_INVALID_PARAMETER if incorrect flags.
1190 *
1191 * @param hDbgMod The module handle.
1192 * @param iSeg The segment index.
1193 * @param off The offset into the segment.
1194 * @param fFlags Symbol search flags, see RTDBGSYMADDR_FLAGS_XXX.
1195 * @param poffDisp Where to store the distance between the
1196 * specified address and the returned symbol. Optional.
1197 * @param ppSymInfo Where to store the pointer to the returned
1198 * symbol information. Always set. Free with
1199 * RTDbgSymbolFree.
1200 */
1201RTDECL(int) RTDbgModSymbolByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
1202 PRTINTPTR poffDisp, PRTDBGSYMBOL *ppSymInfo)
1203{
1204 AssertPtr(ppSymInfo);
1205 *ppSymInfo = NULL;
1206
1207 PRTDBGSYMBOL pSymInfo = RTDbgSymbolAlloc();
1208 if (!pSymInfo)
1209 return VERR_NO_MEMORY;
1210
1211 int rc = RTDbgModSymbolByAddr(hDbgMod, iSeg, off, fFlags, poffDisp, pSymInfo);
1212
1213 if (RT_SUCCESS(rc))
1214 *ppSymInfo = pSymInfo;
1215 else
1216 RTDbgSymbolFree(pSymInfo);
1217 return rc;
1218}
1219RT_EXPORT_SYMBOL(RTDbgModSymbolByAddrA);
1220
1221
1222/**
1223 * Queries symbol information by symbol name.
1224 *
1225 * @returns IPRT status code.
1226 * @retval VERR_DBG_NO_SYMBOLS if there aren't any symbols.
1227 * @retval VERR_SYMBOL_NOT_FOUND if no suitable symbol was found.
1228 * @retval VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE if the symbol name is too long or
1229 * short.
1230 *
1231 * @param hDbgMod The module handle.
1232 * @param pszSymbol The symbol name.
1233 * @param pSymInfo Where to store the symbol information.
1234 */
1235RTDECL(int) RTDbgModSymbolByName(RTDBGMOD hDbgMod, const char *pszSymbol, PRTDBGSYMBOL pSymInfo)
1236{
1237 /*
1238 * Validate input.
1239 */
1240 PRTDBGMODINT pDbgMod = hDbgMod;
1241 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1242 AssertPtr(pszSymbol);
1243 size_t cchSymbol = strlen(pszSymbol);
1244 AssertReturn(cchSymbol, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
1245 AssertReturn(cchSymbol < RTDBG_SYMBOL_NAME_LENGTH, VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE);
1246 AssertPtr(pSymInfo);
1247
1248 /*
1249 * Make the query.
1250 */
1251 RTDBGMOD_LOCK(pDbgMod);
1252 int rc = pDbgMod->pDbgVt->pfnSymbolByName(pDbgMod, pszSymbol, cchSymbol, pSymInfo);
1253 RTDBGMOD_UNLOCK(pDbgMod);
1254
1255 return rc;
1256}
1257RT_EXPORT_SYMBOL(RTDbgModSymbolByName);
1258
1259
1260/**
1261 * Queries symbol information by symbol name.
1262 *
1263 * @returns IPRT status code.
1264 * @retval VERR_DBG_NO_SYMBOLS if there aren't any symbols.
1265 * @retval VERR_SYMBOL_NOT_FOUND if no suitable symbol was found.
1266 * @retval VERR_DBG_SYMBOL_NAME_OUT_OF_RANGE if the symbol name is too long or
1267 * short.
1268 * @retval VERR_NO_MEMORY if RTDbgSymbolAlloc fails.
1269 *
1270 * @param hDbgMod The module handle.
1271 * @param pszSymbol The symbol name.
1272 * @param ppSymInfo Where to store the pointer to the returned
1273 * symbol information. Always set. Free with
1274 * RTDbgSymbolFree.
1275 */
1276RTDECL(int) RTDbgModSymbolByNameA(RTDBGMOD hDbgMod, const char *pszSymbol, PRTDBGSYMBOL *ppSymInfo)
1277{
1278 AssertPtr(ppSymInfo);
1279 *ppSymInfo = NULL;
1280
1281 PRTDBGSYMBOL pSymInfo = RTDbgSymbolAlloc();
1282 if (!pSymInfo)
1283 return VERR_NO_MEMORY;
1284
1285 int rc = RTDbgModSymbolByName(hDbgMod, pszSymbol, pSymInfo);
1286
1287 if (RT_SUCCESS(rc))
1288 *ppSymInfo = pSymInfo;
1289 else
1290 RTDbgSymbolFree(pSymInfo);
1291 return rc;
1292}
1293RT_EXPORT_SYMBOL(RTDbgModSymbolByNameA);
1294
1295
1296/**
1297 * Adds a line number to the module.
1298 *
1299 * @returns IPRT status code.
1300 * @retval VERR_NOT_SUPPORTED if the module interpret doesn't support adding
1301 * custom symbols. This should be consider a normal response.
1302 * @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
1303 * @retval VERR_DBG_FILE_NAME_OUT_OF_RANGE if the file name is too longer or
1304 * empty.
1305 * @retval VERR_DBG_INVALID_RVA if an image relative address is specified and
1306 * it's not inside any of the segments defined by the module.
1307 * @retval VERR_DBG_INVALID_SEGMENT_INDEX if the segment index isn't valid.
1308 * @retval VERR_DBG_INVALID_SEGMENT_OFFSET if the segment offset is beyond the
1309 * end of the segment.
1310 * @retval VERR_INVALID_PARAMETER if the line number flags sets undefined bits.
1311 *
1312 * @param hDbgMod The module handle.
1313 * @param pszFile The file name.
1314 * @param uLineNo The line number.
1315 * @param iSeg The segment index.
1316 * @param off The segment offset.
1317 * @param piOrdinal Where to return the line number ordinal on
1318 * success. If the interpreter doesn't do ordinals,
1319 * this will be set to UINT32_MAX. Optional.
1320 */
1321RTDECL(int) RTDbgModLineAdd(RTDBGMOD hDbgMod, const char *pszFile, uint32_t uLineNo,
1322 RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t *piOrdinal)
1323{
1324 /*
1325 * Validate input.
1326 */
1327 PRTDBGMODINT pDbgMod = hDbgMod;
1328 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1329 AssertPtr(pszFile);
1330 size_t cchFile = strlen(pszFile);
1331 AssertReturn(cchFile, VERR_DBG_FILE_NAME_OUT_OF_RANGE);
1332 AssertReturn(cchFile < RTDBG_FILE_NAME_LENGTH, VERR_DBG_FILE_NAME_OUT_OF_RANGE);
1333 AssertMsgReturn( iSeg <= RTDBGSEGIDX_LAST
1334 || iSeg == RTDBGSEGIDX_RVA,
1335 ("%#x\n", iSeg),
1336 VERR_DBG_INVALID_SEGMENT_INDEX);
1337 AssertReturn(uLineNo > 0 && uLineNo < UINT32_MAX, VERR_INVALID_PARAMETER);
1338
1339 RTDBGMOD_LOCK(pDbgMod);
1340
1341 /*
1342 * Convert RVAs.
1343 */
1344 if (iSeg == RTDBGSEGIDX_RVA)
1345 {
1346 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
1347 if (iSeg == NIL_RTDBGSEGIDX)
1348 {
1349 RTDBGMOD_UNLOCK(pDbgMod);
1350 return VERR_DBG_INVALID_RVA;
1351 }
1352 }
1353
1354 /*
1355 * Get down to business.
1356 */
1357 int rc = pDbgMod->pDbgVt->pfnLineAdd(pDbgMod, pszFile, cchFile, uLineNo, iSeg, off, piOrdinal);
1358
1359 RTDBGMOD_UNLOCK(pDbgMod);
1360 return rc;
1361}
1362RT_EXPORT_SYMBOL(RTDbgModLineAdd);
1363
1364
1365/**
1366 * Gets the line number count.
1367 *
1368 * This can be used together wtih RTDbgModLineByOrdinal or RTDbgModSymbolByLineA
1369 * to enumerate all the line number information.
1370 *
1371 * @returns The number of line numbers in the module.
1372 * UINT32_MAX is returned if the module handle is invalid or some other
1373 * error occurs.
1374 *
1375 * @param hDbgMod The module handle.
1376 */
1377RTDECL(uint32_t) RTDbgModLineCount(RTDBGMOD hDbgMod)
1378{
1379 PRTDBGMODINT pDbgMod = hDbgMod;
1380 RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
1381 RTDBGMOD_LOCK(pDbgMod);
1382
1383 uint32_t cLineNumbers = pDbgMod->pDbgVt->pfnLineCount(pDbgMod);
1384
1385 RTDBGMOD_UNLOCK(pDbgMod);
1386 return cLineNumbers;
1387}
1388RT_EXPORT_SYMBOL(RTDbgModLineCount);
1389
1390
1391/**
1392 * Queries line number information by ordinal number.
1393 *
1394 * This can be used to enumerate the line numbers for the module. Use
1395 * RTDbgModLineCount() to figure the end of the ordinals.
1396 *
1397 * @returns IPRT status code.
1398 * @retval VERR_DBG_NO_LINE_NUMBERS if there aren't any line numbers.
1399 * @retval VERR_DBG_LINE_NOT_FOUND if there is no line number with that
1400 * ordinal.
1401 * @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
1402
1403 * @param hDbgMod The module handle.
1404 * @param iOrdinal The line number ordinal number.
1405 * @param pLineInfo Where to store the information about the line
1406 * number.
1407 */
1408RTDECL(int) RTDbgModLineByOrdinal(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
1409{
1410 PRTDBGMODINT pDbgMod = hDbgMod;
1411 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1412 RTDBGMOD_LOCK(pDbgMod);
1413
1414 int rc = pDbgMod->pDbgVt->pfnLineByOrdinal(pDbgMod, iOrdinal, pLineInfo);
1415
1416 RTDBGMOD_UNLOCK(pDbgMod);
1417 return rc;
1418}
1419RT_EXPORT_SYMBOL(RTDbgModLineByOrdinal);
1420
1421
1422/**
1423 * Queries line number information by ordinal number.
1424 *
1425 * This can be used to enumerate the line numbers for the module. Use
1426 * RTDbgModLineCount() to figure the end of the ordinals.
1427 *
1428 * @returns IPRT status code.
1429 * @retval VERR_DBG_NO_LINE_NUMBERS if there aren't any line numbers.
1430 * @retval VERR_DBG_LINE_NOT_FOUND if there is no line number with that
1431 * ordinal.
1432 * @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
1433 * @retval VERR_NO_MEMORY if RTDbgLineAlloc fails.
1434 *
1435 * @param hDbgMod The module handle.
1436 * @param iOrdinal The line number ordinal number.
1437 * @param ppLineInfo Where to store the pointer to the returned line
1438 * number information. Always set. Free with
1439 * RTDbgLineFree.
1440 */
1441RTDECL(int) RTDbgModLineByOrdinalA(RTDBGMOD hDbgMod, uint32_t iOrdinal, PRTDBGLINE *ppLineInfo)
1442{
1443 AssertPtr(ppLineInfo);
1444 *ppLineInfo = NULL;
1445
1446 PRTDBGLINE pLineInfo = RTDbgLineAlloc();
1447 if (!pLineInfo)
1448 return VERR_NO_MEMORY;
1449
1450 int rc = RTDbgModLineByOrdinal(hDbgMod, iOrdinal, pLineInfo);
1451
1452 if (RT_SUCCESS(rc))
1453 *ppLineInfo = pLineInfo;
1454 else
1455 RTDbgLineFree(pLineInfo);
1456 return rc;
1457}
1458RT_EXPORT_SYMBOL(RTDbgModLineByOrdinalA);
1459
1460
1461/**
1462 * Queries line number information by address.
1463 *
1464 * The returned line number is what the debug info interpreter considers the
1465 * one most applicable to the specified address. This usually means a line
1466 * number with an address equal or lower than the requested.
1467 *
1468 * @returns IPRT status code.
1469 * @retval VERR_DBG_NO_LINE_NUMBERS if there aren't any line numbers.
1470 * @retval VERR_DBG_LINE_NOT_FOUND if no suitable line number was found.
1471 * @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
1472 * @retval VERR_DBG_INVALID_RVA if an image relative address is specified and
1473 * it's not inside any of the segments defined by the module.
1474 * @retval VERR_DBG_INVALID_SEGMENT_INDEX if the segment index isn't valid.
1475 * @retval VERR_DBG_INVALID_SEGMENT_OFFSET if the segment offset is beyond the
1476 * end of the segment.
1477 *
1478 * @param hDbgMod The module handle.
1479 * @param iSeg The segment number.
1480 * @param off The offset into the segment.
1481 * @param poffDisp Where to store the distance between the
1482 * specified address and the returned symbol.
1483 * Optional.
1484 * @param pLineInfo Where to store the line number information.
1485 */
1486RTDECL(int) RTDbgModLineByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
1487{
1488 /*
1489 * Validate input.
1490 */
1491 PRTDBGMODINT pDbgMod = hDbgMod;
1492 RTDBGMOD_VALID_RETURN_RC(pDbgMod, VERR_INVALID_HANDLE);
1493 AssertPtrNull(poffDisp);
1494 AssertPtr(pLineInfo);
1495
1496 RTDBGMOD_LOCK(pDbgMod);
1497
1498 /*
1499 * Convert RVAs.
1500 */
1501 if (iSeg == RTDBGSEGIDX_RVA)
1502 {
1503 iSeg = pDbgMod->pDbgVt->pfnRvaToSegOff(pDbgMod, off, &off);
1504 if (iSeg == NIL_RTDBGSEGIDX)
1505 {
1506 RTDBGMOD_UNLOCK(pDbgMod);
1507 return VERR_DBG_INVALID_RVA;
1508 }
1509 }
1510
1511 int rc = pDbgMod->pDbgVt->pfnLineByAddr(pDbgMod, iSeg, off, poffDisp, pLineInfo);
1512
1513 RTDBGMOD_UNLOCK(pDbgMod);
1514 return rc;
1515}
1516RT_EXPORT_SYMBOL(RTDbgModLineByAddr);
1517
1518
1519/**
1520 * Queries line number information by address.
1521 *
1522 * The returned line number is what the debug info interpreter considers the
1523 * one most applicable to the specified address. This usually means a line
1524 * number with an address equal or lower than the requested.
1525 *
1526 * @returns IPRT status code.
1527 * @retval VERR_DBG_NO_LINE_NUMBERS if there aren't any line numbers.
1528 * @retval VERR_DBG_LINE_NOT_FOUND if no suitable line number was found.
1529 * @retval VERR_INVALID_HANDLE if hDbgMod is invalid.
1530 * @retval VERR_DBG_INVALID_RVA if an image relative address is specified and
1531 * it's not inside any of the segments defined by the module.
1532 * @retval VERR_DBG_INVALID_SEGMENT_INDEX if the segment index isn't valid.
1533 * @retval VERR_DBG_INVALID_SEGMENT_OFFSET if the segment offset is beyond the
1534 * end of the segment.
1535 * @retval VERR_NO_MEMORY if RTDbgLineAlloc fails.
1536 *
1537 * @param hDbgMod The module handle.
1538 * @param iSeg The segment number.
1539 * @param off The offset into the segment.
1540 * @param poffDisp Where to store the distance between the
1541 * specified address and the returned symbol.
1542 * Optional.
1543 * @param ppLineInfo Where to store the pointer to the returned line
1544 * number information. Always set. Free with
1545 * RTDbgLineFree.
1546 */
1547RTDECL(int) RTDbgModLineByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE *ppLineInfo)
1548{
1549 AssertPtr(ppLineInfo);
1550 *ppLineInfo = NULL;
1551
1552 PRTDBGLINE pLineInfo = RTDbgLineAlloc();
1553 if (!pLineInfo)
1554 return VERR_NO_MEMORY;
1555
1556 int rc = RTDbgModLineByAddr(hDbgMod, iSeg, off, poffDisp, pLineInfo);
1557
1558 if (RT_SUCCESS(rc))
1559 *ppLineInfo = pLineInfo;
1560 else
1561 RTDbgLineFree(pLineInfo);
1562 return rc;
1563}
1564RT_EXPORT_SYMBOL(RTDbgModLineByAddrA);
1565
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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