VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/dbg/dbgmodcontainer.cpp@ 73895

Last change on this file since 73895 was 73494, checked in by vboxsync, 6 years ago

IPRT: Added single stack frame unwind function to RTDbgMod and RTLdr, copying over the PoC from DBGFRStack.cpp. bugref:3897

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.3 KB
Line 
1/* $Id: dbgmodcontainer.cpp 73494 2018-08-04 19:41:30Z vboxsync $ */
2/** @file
3 * IPRT - Debug Info Container.
4 */
5
6/*
7 * Copyright (C) 2009-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/dbg.h>
32#include "internal/iprt.h"
33
34#include <iprt/avl.h>
35#include <iprt/err.h>
36#include <iprt/mem.h>
37#define RTDBGMODCNT_WITH_MEM_CACHE
38#ifdef RTDBGMODCNT_WITH_MEM_CACHE
39# include <iprt/memcache.h>
40#endif
41#include <iprt/string.h>
42#include <iprt/strcache.h>
43#include "internal/dbgmod.h"
44
45
46/*********************************************************************************************************************************
47* Structures and Typedefs *
48*********************************************************************************************************************************/
49/**
50 * Symbol entry.
51 */
52typedef struct RTDBGMODCTNSYMBOL
53{
54 /** The address core. */
55 AVLRUINTPTRNODECORE AddrCore;
56 /** The name space core. */
57 RTSTRSPACECORE NameCore;
58 /** The ordinal number core. */
59 AVLU32NODECORE OrdinalCore;
60 /** The segment index. */
61 RTDBGSEGIDX iSeg;
62 /** The symbol flags. */
63 uint32_t fFlags;
64 /** The symbol size.
65 * This may be zero while the range in AddrCore indicates 0. */
66 RTUINTPTR cb;
67} RTDBGMODCTNSYMBOL;
68/** Pointer to a symbol entry in the debug info container. */
69typedef RTDBGMODCTNSYMBOL *PRTDBGMODCTNSYMBOL;
70/** Pointer to a const symbol entry in the debug info container. */
71typedef RTDBGMODCTNSYMBOL const *PCRTDBGMODCTNSYMBOL;
72
73/**
74 * Line number entry.
75 */
76typedef struct RTDBGMODCTNLINE
77{
78 /** The address core.
79 * The Key is the address of the line number. */
80 AVLUINTPTRNODECORE AddrCore;
81 /** The ordinal number core. */
82 AVLU32NODECORE OrdinalCore;
83 /** Pointer to the file name (in string cache). */
84 const char *pszFile;
85 /** The line number. */
86 uint32_t uLineNo;
87 /** The segment index. */
88 RTDBGSEGIDX iSeg;
89} RTDBGMODCTNLINE;
90/** Pointer to a line number entry. */
91typedef RTDBGMODCTNLINE *PRTDBGMODCTNLINE;
92/** Pointer to const a line number entry. */
93typedef RTDBGMODCTNLINE const *PCRTDBGMODCTNLINE;
94
95/**
96 * Segment entry.
97 */
98typedef struct RTDBGMODCTNSEGMENT
99{
100 /** The symbol address space tree. */
101 AVLRUINTPTRTREE SymAddrTree;
102 /** The line number address space tree. */
103 AVLUINTPTRTREE LineAddrTree;
104 /** The segment offset. */
105 RTUINTPTR off;
106 /** The segment size. */
107 RTUINTPTR cb;
108 /** The segment flags. */
109 uint32_t fFlags;
110 /** The segment name. */
111 const char *pszName;
112} RTDBGMODCTNSEGMENT;
113/** Pointer to a segment entry in the debug info container. */
114typedef RTDBGMODCTNSEGMENT *PRTDBGMODCTNSEGMENT;
115/** Pointer to a const segment entry in the debug info container. */
116typedef RTDBGMODCTNSEGMENT const *PCRTDBGMODCTNSEGMENT;
117
118/**
119 * Instance data.
120 */
121typedef struct RTDBGMODCTN
122{
123 /** The name space. */
124 RTSTRSPACE Names;
125 /** Tree containing any absolute addresses. */
126 AVLRUINTPTRTREE AbsAddrTree;
127 /** Tree organizing the symbols by ordinal number. */
128 AVLU32TREE SymbolOrdinalTree;
129 /** Tree organizing the line numbers by ordinal number. */
130 AVLU32TREE LineOrdinalTree;
131 /** Segment table. */
132 PRTDBGMODCTNSEGMENT paSegs;
133 /** The number of segments in the segment table. */
134 RTDBGSEGIDX cSegs;
135 /** The image size. 0 means unlimited. */
136 RTUINTPTR cb;
137 /** The next symbol ordinal. */
138 uint32_t iNextSymbolOrdinal;
139 /** The next line number ordinal. */
140 uint32_t iNextLineOrdinal;
141#ifdef RTDBGMODCNT_WITH_MEM_CACHE
142 /** Line number allocator.
143 * Using a cache is a bit overkill since we normally won't free them, but
144 * it's a construct that exists and does the job relatively efficiently. */
145 RTMEMCACHE hLineNumAllocator;
146#endif
147} RTDBGMODCTN;
148/** Pointer to instance data for the debug info container. */
149typedef RTDBGMODCTN *PRTDBGMODCTN;
150
151
152
153/** @interface_method_impl{RTDBGMODVTDBG,pfnUnwindFrame} */
154static DECLCALLBACK(int)
155rtDbgModContainer_UnwindFrame(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTDBGUNWINDSTATE pState)
156{
157 RT_NOREF(pMod, iSeg, off, pState);
158 return VERR_DBG_NO_UNWIND_INFO;
159}
160
161
162/**
163 * Fills in a RTDBGSYMBOL structure.
164 *
165 * @returns VINF_SUCCESS.
166 * @param pMySym Our internal symbol representation.
167 * @param pExtSym The external symbol representation.
168 */
169DECLINLINE(int) rtDbgModContainerReturnSymbol(PCRTDBGMODCTNSYMBOL pMySym, PRTDBGSYMBOL pExtSym)
170{
171 pExtSym->Value = pMySym->AddrCore.Key;
172 pExtSym->offSeg = pMySym->AddrCore.Key;
173 pExtSym->iSeg = pMySym->iSeg;
174 pExtSym->fFlags = pMySym->fFlags;
175 pExtSym->cb = pMySym->cb;
176 pExtSym->iOrdinal = pMySym->OrdinalCore.Key;
177 Assert(pMySym->NameCore.cchString < sizeof(pExtSym->szName));
178 memcpy(pExtSym->szName, pMySym->NameCore.pszString, pMySym->NameCore.cchString + 1);
179 return VINF_SUCCESS;
180}
181
182
183
184/** @copydoc RTDBGMODVTDBG::pfnLineByAddr */
185static DECLCALLBACK(int) rtDbgModContainer_LineByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
186 PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
187{
188 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
189
190 /*
191 * Validate the input address.
192 */
193 AssertMsgReturn(iSeg < pThis->cSegs,
194 ("iSeg=%#x cSegs=%#x\n", iSeg, pThis->cSegs),
195 VERR_DBG_INVALID_SEGMENT_INDEX);
196 AssertMsgReturn(off < pThis->paSegs[iSeg].cb,
197 ("off=%RTptr cbSeg=%RTptr\n", off, pThis->paSegs[iSeg].cb),
198 VERR_DBG_INVALID_SEGMENT_OFFSET);
199
200 /*
201 * Lookup the nearest line number with an address less or equal to the specified address.
202 */
203 PAVLUINTPTRNODECORE pAvlCore = RTAvlUIntPtrGetBestFit(&pThis->paSegs[iSeg].LineAddrTree, off, false /*fAbove*/);
204 if (!pAvlCore)
205 return pThis->iNextLineOrdinal
206 ? VERR_DBG_LINE_NOT_FOUND
207 : VERR_DBG_NO_LINE_NUMBERS;
208 PCRTDBGMODCTNLINE pMyLine = RT_FROM_MEMBER(pAvlCore, RTDBGMODCTNLINE const, AddrCore);
209 pLineInfo->Address = pMyLine->AddrCore.Key;
210 pLineInfo->offSeg = pMyLine->AddrCore.Key;
211 pLineInfo->iSeg = iSeg;
212 pLineInfo->uLineNo = pMyLine->uLineNo;
213 pLineInfo->iOrdinal = pMyLine->OrdinalCore.Key;
214 strcpy(pLineInfo->szFilename, pMyLine->pszFile);
215 if (poffDisp)
216 *poffDisp = off - pMyLine->AddrCore.Key;
217 return VINF_SUCCESS;
218}
219
220
221/** @copydoc RTDBGMODVTDBG::pfnLineByOrdinal */
222static DECLCALLBACK(int) rtDbgModContainer_LineByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
223{
224 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
225
226 /*
227 * Look it up.
228 */
229 if (iOrdinal >= pThis->iNextLineOrdinal)
230 return pThis->iNextLineOrdinal
231 ? VERR_DBG_LINE_NOT_FOUND
232 : VERR_DBG_NO_LINE_NUMBERS;
233 PAVLU32NODECORE pAvlCore = RTAvlU32Get(&pThis->LineOrdinalTree, iOrdinal);
234 AssertReturn(pAvlCore, VERR_DBG_LINE_NOT_FOUND);
235 PCRTDBGMODCTNLINE pMyLine = RT_FROM_MEMBER(pAvlCore, RTDBGMODCTNLINE const, OrdinalCore);
236 pLineInfo->Address = pMyLine->AddrCore.Key;
237 pLineInfo->offSeg = pMyLine->AddrCore.Key;
238 pLineInfo->iSeg = pMyLine->iSeg;
239 pLineInfo->uLineNo = pMyLine->uLineNo;
240 pLineInfo->iOrdinal = pMyLine->OrdinalCore.Key;
241 strcpy(pLineInfo->szFilename, pMyLine->pszFile);
242 return VINF_SUCCESS;
243}
244
245
246/** @copydoc RTDBGMODVTDBG::pfnLineCount */
247static DECLCALLBACK(uint32_t) rtDbgModContainer_LineCount(PRTDBGMODINT pMod)
248{
249 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
250
251 /* Note! The ordinal numbers are 0-based. */
252 return pThis->iNextLineOrdinal;
253}
254
255
256/** @copydoc RTDBGMODVTDBG::pfnLineAdd */
257static DECLCALLBACK(int) rtDbgModContainer_LineAdd(PRTDBGMODINT pMod, const char *pszFile, size_t cchFile, uint32_t uLineNo,
258 uint32_t iSeg, RTUINTPTR off, uint32_t *piOrdinal)
259{
260 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
261
262 /*
263 * Validate the input address.
264 */
265 AssertMsgReturn(iSeg < pThis->cSegs, ("iSeg=%#x cSegs=%#x\n", iSeg, pThis->cSegs),
266 VERR_DBG_INVALID_SEGMENT_INDEX);
267 AssertMsgReturn(off <= pThis->paSegs[iSeg].cb, ("off=%RTptr cbSeg=%RTptr\n", off, pThis->paSegs[iSeg].cb),
268 VERR_DBG_INVALID_SEGMENT_OFFSET);
269
270 /*
271 * Create a new entry.
272 */
273#ifdef RTDBGMODCNT_WITH_MEM_CACHE
274 PRTDBGMODCTNLINE pLine = (PRTDBGMODCTNLINE)RTMemCacheAlloc(pThis->hLineNumAllocator);
275#else
276 PRTDBGMODCTNLINE pLine = (PRTDBGMODCTNLINE)RTMemAllocZ(sizeof(*pLine));
277#endif
278 if (!pLine)
279 return VERR_NO_MEMORY;
280 pLine->AddrCore.Key = off;
281 pLine->OrdinalCore.Key = pThis->iNextLineOrdinal;
282 pLine->uLineNo = uLineNo;
283 pLine->iSeg = iSeg;
284 pLine->pszFile = RTStrCacheEnterN(g_hDbgModStrCache, pszFile, cchFile);
285 int rc;
286 if (pLine->pszFile)
287 {
288 if (RTAvlUIntPtrInsert(&pThis->paSegs[iSeg].LineAddrTree, &pLine->AddrCore))
289 {
290 if (RTAvlU32Insert(&pThis->LineOrdinalTree, &pLine->OrdinalCore))
291 {
292 if (piOrdinal)
293 *piOrdinal = pThis->iNextLineOrdinal;
294 pThis->iNextLineOrdinal++;
295 return VINF_SUCCESS;
296 }
297
298 rc = VERR_INTERNAL_ERROR_5;
299 RTAvlUIntPtrRemove(&pThis->paSegs[iSeg].LineAddrTree, pLine->AddrCore.Key);
300 }
301
302 /* bail out */
303 rc = VERR_DBG_ADDRESS_CONFLICT;
304 RTStrCacheRelease(g_hDbgModStrCache, pLine->pszFile);
305 }
306 else
307 rc = VERR_NO_MEMORY;
308#ifdef RTDBGMODCNT_WITH_MEM_CACHE
309 RTMemCacheFree(pThis->hLineNumAllocator, pLine);
310#else
311 RTMemFree(pLine);
312#endif
313 return rc;
314}
315
316
317/** @copydoc RTDBGMODVTDBG::pfnSymbolByAddr */
318static DECLCALLBACK(int) rtDbgModContainer_SymbolByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
319 PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
320{
321 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
322
323 /*
324 * Validate the input address.
325 */
326 AssertMsgReturn( iSeg == RTDBGSEGIDX_ABS
327 || iSeg < pThis->cSegs,
328 ("iSeg=%#x cSegs=%#x\n", iSeg, pThis->cSegs),
329 VERR_DBG_INVALID_SEGMENT_INDEX);
330 AssertMsgReturn( iSeg >= RTDBGSEGIDX_SPECIAL_FIRST
331 || off <= pThis->paSegs[iSeg].cb,
332 ("off=%RTptr cbSeg=%RTptr\n", off, pThis->paSegs[iSeg].cb),
333 VERR_DBG_INVALID_SEGMENT_OFFSET);
334
335 /*
336 * Lookup the nearest symbol with an address less or equal to the specified address.
337 */
338 PAVLRUINTPTRNODECORE pAvlCore = RTAvlrUIntPtrGetBestFit( iSeg == RTDBGSEGIDX_ABS
339 ? &pThis->AbsAddrTree
340 : &pThis->paSegs[iSeg].SymAddrTree,
341 off,
342 fFlags == RTDBGSYMADDR_FLAGS_GREATER_OR_EQUAL /*fAbove*/);
343 if (!pAvlCore)
344 return VERR_SYMBOL_NOT_FOUND;
345 PCRTDBGMODCTNSYMBOL pMySym = RT_FROM_MEMBER(pAvlCore, RTDBGMODCTNSYMBOL const, AddrCore);
346 if (poffDisp)
347 *poffDisp = off - pMySym->AddrCore.Key;
348 return rtDbgModContainerReturnSymbol(pMySym, pSymInfo);
349}
350
351
352/** @copydoc RTDBGMODVTDBG::pfnSymbolByName */
353static DECLCALLBACK(int) rtDbgModContainer_SymbolByName(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol, PRTDBGSYMBOL pSymInfo)
354{
355 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
356 NOREF(cchSymbol);
357
358 /*
359 * Look it up in the name space.
360 */
361 PRTSTRSPACECORE pStrCore = RTStrSpaceGet(&pThis->Names, pszSymbol);
362 if (!pStrCore)
363 return VERR_SYMBOL_NOT_FOUND;
364 PCRTDBGMODCTNSYMBOL pMySym = RT_FROM_MEMBER(pStrCore, RTDBGMODCTNSYMBOL const, NameCore);
365 return rtDbgModContainerReturnSymbol(pMySym, pSymInfo);
366}
367
368
369/** @copydoc RTDBGMODVTDBG::pfnSymbolByOrdinal */
370static DECLCALLBACK(int) rtDbgModContainer_SymbolByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
371{
372 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
373
374 /*
375 * Look it up in the ordinal tree.
376 */
377 if (iOrdinal >= pThis->iNextSymbolOrdinal)
378 return pThis->iNextSymbolOrdinal
379 ? VERR_DBG_NO_SYMBOLS
380 : VERR_SYMBOL_NOT_FOUND;
381 PAVLU32NODECORE pAvlCore = RTAvlU32Get(&pThis->SymbolOrdinalTree, iOrdinal);
382 AssertReturn(pAvlCore, VERR_SYMBOL_NOT_FOUND);
383 PCRTDBGMODCTNSYMBOL pMySym = RT_FROM_MEMBER(pAvlCore, RTDBGMODCTNSYMBOL const, OrdinalCore);
384 return rtDbgModContainerReturnSymbol(pMySym, pSymInfo);
385}
386
387
388/** @copydoc RTDBGMODVTDBG::pfnSymbolCount */
389static DECLCALLBACK(uint32_t) rtDbgModContainer_SymbolCount(PRTDBGMODINT pMod)
390{
391 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
392
393 /* Note! The ordinal numbers are 0-based. */
394 return pThis->iNextSymbolOrdinal;
395}
396
397
398/** @copydoc RTDBGMODVTDBG::pfnSymbolAdd */
399static DECLCALLBACK(int) rtDbgModContainer_SymbolAdd(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
400 RTDBGSEGIDX iSeg, RTUINTPTR off, RTUINTPTR cb, uint32_t fFlags,
401 uint32_t *piOrdinal)
402{
403 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
404
405 /*
406 * Address validation. The other arguments have already been validated.
407 */
408 AssertMsgReturn( iSeg == RTDBGSEGIDX_ABS
409 || iSeg < pThis->cSegs,
410 ("iSeg=%#x cSegs=%#x\n", iSeg, pThis->cSegs),
411 VERR_DBG_INVALID_SEGMENT_INDEX);
412 AssertMsgReturn( iSeg >= RTDBGSEGIDX_SPECIAL_FIRST
413 || off <= pThis->paSegs[iSeg].cb,
414 ("off=%RTptr cb=%RTptr cbSeg=%RTptr\n", off, cb, pThis->paSegs[iSeg].cb),
415 VERR_DBG_INVALID_SEGMENT_OFFSET);
416
417 /* Be a little relaxed wrt to the symbol size. */
418 int rc = VINF_SUCCESS;
419 if (iSeg != RTDBGSEGIDX_ABS && off + cb > pThis->paSegs[iSeg].cb)
420 {
421 cb = pThis->paSegs[iSeg].cb - off;
422 rc = VINF_DBG_ADJUSTED_SYM_SIZE;
423 }
424
425 /*
426 * Create a new entry.
427 */
428 PRTDBGMODCTNSYMBOL pSymbol = (PRTDBGMODCTNSYMBOL)RTMemAllocZ(sizeof(*pSymbol));
429 if (!pSymbol)
430 return VERR_NO_MEMORY;
431
432 pSymbol->AddrCore.Key = off;
433 pSymbol->AddrCore.KeyLast = off + (cb ? cb - 1 : 0);
434 pSymbol->OrdinalCore.Key = pThis->iNextSymbolOrdinal;
435 pSymbol->iSeg = iSeg;
436 pSymbol->cb = cb;
437 pSymbol->fFlags = fFlags;
438 pSymbol->NameCore.pszString = RTStrCacheEnterN(g_hDbgModStrCache, pszSymbol, cchSymbol);
439 if (pSymbol->NameCore.pszString)
440 {
441 if (RTStrSpaceInsert(&pThis->Names, &pSymbol->NameCore))
442 {
443 PAVLRUINTPTRTREE pAddrTree = iSeg == RTDBGSEGIDX_ABS
444 ? &pThis->AbsAddrTree
445 : &pThis->paSegs[iSeg].SymAddrTree;
446 if (RTAvlrUIntPtrInsert(pAddrTree, &pSymbol->AddrCore))
447 {
448 if (RTAvlU32Insert(&pThis->SymbolOrdinalTree, &pSymbol->OrdinalCore))
449 {
450 if (piOrdinal)
451 *piOrdinal = pThis->iNextSymbolOrdinal;
452 pThis->iNextSymbolOrdinal++;
453 return rc;
454 }
455
456 /* bail out */
457 rc = VERR_INTERNAL_ERROR_5;
458 RTAvlrUIntPtrRemove(pAddrTree, pSymbol->AddrCore.Key);
459 }
460 else
461 rc = VERR_DBG_ADDRESS_CONFLICT;
462 RTStrSpaceRemove(&pThis->Names, pSymbol->NameCore.pszString);
463 }
464 else
465 rc = VERR_DBG_DUPLICATE_SYMBOL;
466 RTStrCacheRelease(g_hDbgModStrCache, pSymbol->NameCore.pszString);
467 }
468 else
469 rc = VERR_NO_MEMORY;
470 RTMemFree(pSymbol);
471 return rc;
472}
473
474
475/** @copydoc RTDBGMODVTDBG::pfnSegmentByIndex */
476static DECLCALLBACK(int) rtDbgModContainer_SegmentByIndex(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
477{
478 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
479 if (iSeg >= pThis->cSegs)
480 return VERR_DBG_INVALID_SEGMENT_INDEX;
481 pSegInfo->Address = RTUINTPTR_MAX;
482 pSegInfo->uRva = pThis->paSegs[iSeg].off;
483 pSegInfo->cb = pThis->paSegs[iSeg].cb;
484 pSegInfo->fFlags = pThis->paSegs[iSeg].fFlags;
485 pSegInfo->iSeg = iSeg;
486 strcpy(pSegInfo->szName, pThis->paSegs[iSeg].pszName);
487 return VINF_SUCCESS;
488}
489
490
491/** @copydoc RTDBGMODVTDBG::pfnSegmentCount */
492static DECLCALLBACK(RTDBGSEGIDX) rtDbgModContainer_SegmentCount(PRTDBGMODINT pMod)
493{
494 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
495 return pThis->cSegs;
496}
497
498
499/** @copydoc RTDBGMODVTDBG::pfnSegmentAdd */
500static DECLCALLBACK(int) rtDbgModContainer_SegmentAdd(PRTDBGMODINT pMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName, size_t cchName,
501 uint32_t fFlags, PRTDBGSEGIDX piSeg)
502{
503 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
504
505 /*
506 * Input validation (the bits the caller cannot do).
507 */
508 /* Overlapping segments are not yet supported. Will use flags to deal with it if it becomes necessary. */
509 RTUINTPTR uRvaLast = uRva + RT_MAX(cb, 1) - 1;
510 RTUINTPTR uRvaLastMax = uRvaLast;
511 RTDBGSEGIDX iSeg = pThis->cSegs;
512 while (iSeg-- > 0)
513 {
514 RTUINTPTR uCurRva = pThis->paSegs[iSeg].off;
515 RTUINTPTR uCurRvaLast = uCurRva + RT_MAX(pThis->paSegs[iSeg].cb, 1) - 1;
516 if ( uRva <= uCurRvaLast
517 && uRvaLast >= uCurRva
518 && ( /* HACK ALERT! Allow empty segments to share space (bios/watcom, elf). */
519 (cb != 0 && pThis->paSegs[iSeg].cb != 0)
520 || ( cb == 0
521 && uRva != uCurRva
522 && uRva != uCurRvaLast)
523 || ( pThis->paSegs[iSeg].cb == 0
524 && uCurRva != uRva
525 && uCurRva != uRvaLast)
526 )
527 )
528 AssertMsgFailedReturn(("uRva=%RTptr uRvaLast=%RTptr (cb=%RTptr) \"%s\";\n"
529 "uRva=%RTptr uRvaLast=%RTptr (cb=%RTptr) \"%s\" iSeg=%#x\n",
530 uRva, uRvaLast, cb, pszName,
531 uCurRva, uCurRvaLast, pThis->paSegs[iSeg].cb, pThis->paSegs[iSeg].pszName, iSeg),
532 VERR_DBG_SEGMENT_INDEX_CONFLICT);
533 if (uRvaLastMax < uCurRvaLast)
534 uRvaLastMax = uCurRvaLast;
535 }
536 /* Strict ordered segment addition at the moment. */
537 iSeg = pThis->cSegs;
538 AssertMsgReturn(!piSeg || *piSeg == NIL_RTDBGSEGIDX || *piSeg == iSeg,
539 ("iSeg=%#x *piSeg=%#x\n", iSeg, *piSeg),
540 VERR_DBG_INVALID_SEGMENT_INDEX);
541
542 /*
543 * Add an entry to the segment table, extending it if necessary.
544 */
545 if (!(iSeg % 8))
546 {
547 void *pvSegs = RTMemRealloc(pThis->paSegs, sizeof(RTDBGMODCTNSEGMENT) * (iSeg + 8));
548 if (!pvSegs)
549 return VERR_NO_MEMORY;
550 pThis->paSegs = (PRTDBGMODCTNSEGMENT)pvSegs;
551 }
552
553 pThis->paSegs[iSeg].SymAddrTree = NULL;
554 pThis->paSegs[iSeg].LineAddrTree = NULL;
555 pThis->paSegs[iSeg].off = uRva;
556 pThis->paSegs[iSeg].cb = cb;
557 pThis->paSegs[iSeg].fFlags = fFlags;
558 pThis->paSegs[iSeg].pszName = RTStrCacheEnterN(g_hDbgModStrCache, pszName, cchName);
559 if (pThis->paSegs[iSeg].pszName)
560 {
561 if (piSeg)
562 *piSeg = iSeg;
563 pThis->cSegs++;
564 pThis->cb = uRvaLastMax + 1;
565 if (!pThis->cb)
566 pThis->cb = RTUINTPTR_MAX;
567 return VINF_SUCCESS;
568 }
569 return VERR_NO_MEMORY;
570}
571
572
573/** @copydoc RTDBGMODVTDBG::pfnImageSize */
574static DECLCALLBACK(RTUINTPTR) rtDbgModContainer_ImageSize(PRTDBGMODINT pMod)
575{
576 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
577 return pThis->cb;
578}
579
580
581/** @copydoc RTDBGMODVTDBG::pfnRvaToSegOff */
582static DECLCALLBACK(RTDBGSEGIDX) rtDbgModContainer_RvaToSegOff(PRTDBGMODINT pMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
583{
584 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
585 PCRTDBGMODCTNSEGMENT paSeg = pThis->paSegs;
586 uint32_t const cSegs = pThis->cSegs;
587#if 0
588 if (cSegs <= 7)
589#endif
590 {
591 /*
592 * Linear search.
593 */
594 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
595 {
596 RTUINTPTR offSeg = uRva - paSeg[iSeg].off;
597 if (offSeg < paSeg[iSeg].cb)
598 {
599 if (poffSeg)
600 *poffSeg = offSeg;
601 return iSeg;
602 }
603 }
604 }
605#if 0 /** @todo binary search doesn't work if we've got empty segments in the list */
606 else
607 {
608 /*
609 * Binary search.
610 */
611 uint32_t iFirst = 0;
612 uint32_t iLast = cSegs - 1;
613 for (;;)
614 {
615 uint32_t iSeg = iFirst + (iLast - iFirst) / 2;
616 RTUINTPTR offSeg = uRva - paSeg[iSeg].off;
617 if (offSeg < paSeg[iSeg].cb)
618 {
619#if 0 /* Enable if we change the above test. */
620 if (offSeg == 0 && paSeg[iSeg].cb == 0)
621 while ( iSeg > 0
622 && paSeg[iSeg - 1].cb == 0
623 && paSeg[iSeg - 1].off == uRva)
624 iSeg--;
625#endif
626
627 if (poffSeg)
628 *poffSeg = offSeg;
629 return iSeg;
630 }
631
632 /* advance */
633 if (uRva < paSeg[iSeg].off)
634 {
635 /* between iFirst and iSeg. */
636 if (iSeg == iFirst)
637 break;
638 iLast = iSeg - 1;
639 }
640 else
641 {
642 /* between iSeg and iLast. paSeg[iSeg].cb == 0 ends up here too. */
643 if (iSeg == iLast)
644 break;
645 iFirst = iSeg + 1;
646 }
647 }
648 }
649#endif
650
651 /* Invalid. */
652 return NIL_RTDBGSEGIDX;
653}
654
655
656/** Destroy a line number node. */
657static DECLCALLBACK(int) rtDbgModContainer_DestroyTreeLineNode(PAVLU32NODECORE pNode, void *pvUser)
658{
659 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pvUser;
660 PRTDBGMODCTNLINE pLine = RT_FROM_MEMBER(pNode, RTDBGMODCTNLINE, OrdinalCore);
661 RTStrCacheRelease(g_hDbgModStrCache, pLine->pszFile);
662 pLine->pszFile = NULL;
663#ifdef RTDBGMODCNT_WITH_MEM_CACHE
664 RTMemCacheFree(pThis->hLineNumAllocator, pLine);
665#else
666 RTMemFree(pLine); NOREF(pThis);
667#endif
668 return 0;
669}
670
671
672/** Destroy a symbol node. */
673static DECLCALLBACK(int) rtDbgModContainer_DestroyTreeNode(PAVLRUINTPTRNODECORE pNode, void *pvUser)
674{
675 PRTDBGMODCTNSYMBOL pSym = RT_FROM_MEMBER(pNode, RTDBGMODCTNSYMBOL, AddrCore);
676 RTStrCacheRelease(g_hDbgModStrCache, pSym->NameCore.pszString);
677 pSym->NameCore.pszString = NULL;
678 RTMemFree(pSym);
679 NOREF(pvUser);
680 return 0;
681}
682
683
684/** @copydoc RTDBGMODVTDBG::pfnClose */
685static DECLCALLBACK(int) rtDbgModContainer_Close(PRTDBGMODINT pMod)
686{
687 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
688
689 /*
690 * Destroy the symbols and instance data.
691 */
692 for (uint32_t iSeg = 0; iSeg < pThis->cSegs; iSeg++)
693 {
694 RTAvlrUIntPtrDestroy(&pThis->paSegs[iSeg].SymAddrTree, rtDbgModContainer_DestroyTreeNode, NULL);
695 RTStrCacheRelease(g_hDbgModStrCache, pThis->paSegs[iSeg].pszName);
696 pThis->paSegs[iSeg].pszName = NULL;
697 }
698
699 RTAvlrUIntPtrDestroy(&pThis->AbsAddrTree, rtDbgModContainer_DestroyTreeNode, NULL);
700 pThis->Names = NULL;
701
702#ifdef RTDBGMODCNT_WITH_MEM_CACHE
703 RTMemCacheDestroy(pThis->hLineNumAllocator);
704 pThis->hLineNumAllocator = NIL_RTMEMCACHE;
705#else
706 RTAvlU32Destroy(&pThis->LineOrdinalTree, rtDbgModContainer_DestroyTreeLineNode, pThis);
707#endif
708
709 RTMemFree(pThis->paSegs);
710 pThis->paSegs = NULL;
711
712 RTMemFree(pThis);
713
714 return VINF_SUCCESS;
715}
716
717
718/** @copydoc RTDBGMODVTDBG::pfnTryOpen */
719static DECLCALLBACK(int) rtDbgModContainer_TryOpen(PRTDBGMODINT pMod, RTLDRARCH enmArch)
720{
721 NOREF(pMod); NOREF(enmArch);
722 return VERR_INTERNAL_ERROR_5;
723}
724
725
726
727/** Virtual function table for the debug info container. */
728DECL_HIDDEN_CONST(RTDBGMODVTDBG) const g_rtDbgModVtDbgContainer =
729{
730 /*.u32Magic = */ RTDBGMODVTDBG_MAGIC,
731 /*.fSupports = */ 0, /* (Don't call my TryOpen, please.) */
732 /*.pszName = */ "container",
733 /*.pfnTryOpen = */ rtDbgModContainer_TryOpen,
734 /*.pfnClose = */ rtDbgModContainer_Close,
735
736 /*.pfnRvaToSegOff = */ rtDbgModContainer_RvaToSegOff,
737 /*.pfnImageSize = */ rtDbgModContainer_ImageSize,
738
739 /*.pfnSegmentAdd = */ rtDbgModContainer_SegmentAdd,
740 /*.pfnSegmentCount = */ rtDbgModContainer_SegmentCount,
741 /*.pfnSegmentByIndex = */ rtDbgModContainer_SegmentByIndex,
742
743 /*.pfnSymbolAdd = */ rtDbgModContainer_SymbolAdd,
744 /*.pfnSymbolCount = */ rtDbgModContainer_SymbolCount,
745 /*.pfnSymbolByOrdinal = */ rtDbgModContainer_SymbolByOrdinal,
746 /*.pfnSymbolByName = */ rtDbgModContainer_SymbolByName,
747 /*.pfnSymbolByAddr = */ rtDbgModContainer_SymbolByAddr,
748
749 /*.pfnLineAdd = */ rtDbgModContainer_LineAdd,
750 /*.pfnLineCount = */ rtDbgModContainer_LineCount,
751 /*.pfnLineByOrdinal = */ rtDbgModContainer_LineByOrdinal,
752 /*.pfnLineByAddr = */ rtDbgModContainer_LineByAddr,
753
754 /*.pfnUnwindFrame = */ rtDbgModContainer_UnwindFrame,
755
756 /*.u32EndMagic = */ RTDBGMODVTDBG_MAGIC
757};
758
759
760
761/**
762 * Special container operation for removing all symbols.
763 *
764 * @returns IPRT status code.
765 * @param pMod The module instance.
766 */
767DECLHIDDEN(int) rtDbgModContainer_SymbolRemoveAll(PRTDBGMODINT pMod)
768{
769 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
770
771 for (uint32_t iSeg = 0; iSeg < pThis->cSegs; iSeg++)
772 {
773 RTAvlrUIntPtrDestroy(&pThis->paSegs[iSeg].SymAddrTree, rtDbgModContainer_DestroyTreeNode, NULL);
774 Assert(pThis->paSegs[iSeg].SymAddrTree == NULL);
775 }
776
777 RTAvlrUIntPtrDestroy(&pThis->AbsAddrTree, rtDbgModContainer_DestroyTreeNode, NULL);
778 Assert(pThis->AbsAddrTree == NULL);
779
780 pThis->Names = NULL;
781 pThis->iNextSymbolOrdinal = 0;
782
783 return VINF_SUCCESS;
784}
785
786
787/**
788 * Special container operation for removing all line numbers.
789 *
790 * @returns IPRT status code.
791 * @param pMod The module instance.
792 */
793DECLHIDDEN(int) rtDbgModContainer_LineRemoveAll(PRTDBGMODINT pMod)
794{
795 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
796
797 for (uint32_t iSeg = 0; iSeg < pThis->cSegs; iSeg++)
798 pThis->paSegs[iSeg].LineAddrTree = NULL;
799
800 RTAvlU32Destroy(&pThis->LineOrdinalTree, rtDbgModContainer_DestroyTreeLineNode, pThis);
801 Assert(pThis->LineOrdinalTree == NULL);
802
803 pThis->iNextLineOrdinal = 0;
804
805 return VINF_SUCCESS;
806}
807
808
809/**
810 * Special container operation for removing everything.
811 *
812 * @returns IPRT status code.
813 * @param pMod The module instance.
814 */
815DECLHIDDEN(int) rtDbgModContainer_RemoveAll(PRTDBGMODINT pMod)
816{
817 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
818
819 rtDbgModContainer_LineRemoveAll(pMod);
820 rtDbgModContainer_SymbolRemoveAll(pMod);
821
822 for (uint32_t iSeg = 0; iSeg < pThis->cSegs; iSeg++)
823 {
824 RTStrCacheRelease(g_hDbgModStrCache, pThis->paSegs[iSeg].pszName);
825 pThis->paSegs[iSeg].pszName = NULL;
826 }
827
828 pThis->cSegs = 0;
829 pThis->cb = 0;
830
831 return VINF_SUCCESS;
832}
833
834
835/**
836 * Creates a generic debug info container and associates it with the module.
837 *
838 * @returns IPRT status code.
839 * @param pMod The module instance.
840 * @param cbSeg The size of the initial segment. 0 if segments are to be
841 * created manually later on.
842 */
843int rtDbgModContainerCreate(PRTDBGMODINT pMod, RTUINTPTR cbSeg)
844{
845 PRTDBGMODCTN pThis = (PRTDBGMODCTN)RTMemAlloc(sizeof(*pThis));
846 if (!pThis)
847 return VERR_NO_MEMORY;
848
849 pThis->Names = NULL;
850 pThis->AbsAddrTree = NULL;
851 pThis->SymbolOrdinalTree = NULL;
852 pThis->LineOrdinalTree = NULL;
853 pThis->paSegs = NULL;
854 pThis->cSegs = 0;
855 pThis->cb = 0;
856 pThis->iNextSymbolOrdinal = 0;
857 pThis->iNextLineOrdinal = 0;
858
859 pMod->pDbgVt = &g_rtDbgModVtDbgContainer;
860 pMod->pvDbgPriv = pThis;
861
862#ifdef RTDBGMODCNT_WITH_MEM_CACHE
863 int rc = RTMemCacheCreate(&pThis->hLineNumAllocator, sizeof(RTDBGMODCTNLINE), sizeof(void *), UINT32_MAX,
864 NULL /*pfnCtor*/, NULL /*pfnDtor*/, NULL /*pvUser*/, 0 /*fFlags*/);
865#else
866 int rc = VINF_SUCCESS;
867#endif
868 if (RT_SUCCESS(rc))
869 {
870 /*
871 * Add the initial segment.
872 */
873 if (cbSeg)
874 rc = rtDbgModContainer_SegmentAdd(pMod, 0, cbSeg, "default", sizeof("default") - 1, 0, NULL);
875 if (RT_SUCCESS(rc))
876 return rc;
877
878#ifdef RTDBGMODCNT_WITH_MEM_CACHE
879 RTMemCacheDestroy(pThis->hLineNumAllocator);
880#endif
881 }
882
883 RTMemFree(pThis);
884 pMod->pDbgVt = NULL;
885 pMod->pvDbgPriv = NULL;
886 return rc;
887}
888
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