VirtualBox

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

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

dbgmodcontainer.cpp: Fix for crash during solaris detection (COMMON symbols are reported as ABS symbols in solaris).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.4 KB
Line 
1/* $Id: dbgmodcontainer.cpp 46215 2013-05-22 12:39:14Z vboxsync $ */
2/** @file
3 * IPRT - Debug Info Container.
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#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 * Fills in a RTDBGSYMBOL structure.
154 *
155 * @returns VINF_SUCCESS.
156 * @param pMySym Our internal symbol representation.
157 * @param pExtSym The external symbol representation.
158 */
159DECLINLINE(int) rtDbgModContainerReturnSymbol(PCRTDBGMODCTNSYMBOL pMySym, PRTDBGSYMBOL pExtSym)
160{
161 pExtSym->Value = pMySym->AddrCore.Key;
162 pExtSym->offSeg = pMySym->AddrCore.Key;
163 pExtSym->iSeg = pMySym->iSeg;
164 pExtSym->fFlags = pMySym->fFlags;
165 pExtSym->cb = pMySym->cb;
166 pExtSym->iOrdinal = pMySym->OrdinalCore.Key;
167 Assert(pMySym->NameCore.cchString < sizeof(pExtSym->szName));
168 memcpy(pExtSym->szName, pMySym->NameCore.pszString, pMySym->NameCore.cchString + 1);
169 return VINF_SUCCESS;
170}
171
172
173
174/** @copydoc RTDBGMODVTDBG::pfnLineByAddr */
175static DECLCALLBACK(int) rtDbgModContainer_LineByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
176 PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
177{
178 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
179
180 /*
181 * Validate the input address.
182 */
183 AssertMsgReturn(iSeg < pThis->cSegs,
184 ("iSeg=%#x cSegs=%#x\n", pThis->cSegs),
185 VERR_DBG_INVALID_SEGMENT_INDEX);
186 AssertMsgReturn(off < pThis->paSegs[iSeg].cb,
187 ("off=%RTptr cbSeg=%RTptr\n", off, pThis->paSegs[iSeg].cb),
188 VERR_DBG_INVALID_SEGMENT_OFFSET);
189
190 /*
191 * Lookup the nearest line number with an address less or equal to the specified address.
192 */
193 PAVLUINTPTRNODECORE pAvlCore = RTAvlUIntPtrGetBestFit(&pThis->paSegs[iSeg].LineAddrTree, off, false /*fAbove*/);
194 if (!pAvlCore)
195 return pThis->iNextLineOrdinal
196 ? VERR_DBG_LINE_NOT_FOUND
197 : VERR_DBG_NO_LINE_NUMBERS;
198 PCRTDBGMODCTNLINE pMyLine = RT_FROM_MEMBER(pAvlCore, RTDBGMODCTNLINE const, AddrCore);
199 pLineInfo->Address = pMyLine->AddrCore.Key;
200 pLineInfo->offSeg = pMyLine->AddrCore.Key;
201 pLineInfo->iSeg = iSeg;
202 pLineInfo->uLineNo = pMyLine->uLineNo;
203 pLineInfo->iOrdinal = pMyLine->OrdinalCore.Key;
204 strcpy(pLineInfo->szFilename, pMyLine->pszFile);
205 if (poffDisp)
206 *poffDisp = off - pMyLine->AddrCore.Key;
207 return VINF_SUCCESS;
208}
209
210
211/** @copydoc RTDBGMODVTDBG::pfnLineByOrdinal */
212static DECLCALLBACK(int) rtDbgModContainer_LineByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
213{
214 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
215
216 /*
217 * Look it up.
218 */
219 if (iOrdinal >= pThis->iNextLineOrdinal)
220 return pThis->iNextLineOrdinal
221 ? VERR_DBG_LINE_NOT_FOUND
222 : VERR_DBG_NO_LINE_NUMBERS;
223 PAVLU32NODECORE pAvlCore = RTAvlU32Get(&pThis->LineOrdinalTree, iOrdinal);
224 AssertReturn(pAvlCore, VERR_DBG_LINE_NOT_FOUND);
225 PCRTDBGMODCTNLINE pMyLine = RT_FROM_MEMBER(pAvlCore, RTDBGMODCTNLINE const, OrdinalCore);
226 pLineInfo->Address = pMyLine->AddrCore.Key;
227 pLineInfo->offSeg = pMyLine->AddrCore.Key;
228 pLineInfo->iSeg = pMyLine->iSeg;
229 pLineInfo->uLineNo = pMyLine->uLineNo;
230 pLineInfo->iOrdinal = pMyLine->OrdinalCore.Key;
231 strcpy(pLineInfo->szFilename, pMyLine->pszFile);
232 return VINF_SUCCESS;
233}
234
235
236/** @copydoc RTDBGMODVTDBG::pfnLineCount */
237static DECLCALLBACK(uint32_t) rtDbgModContainer_LineCount(PRTDBGMODINT pMod)
238{
239 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
240
241 /* Note! The ordinal numbers are 0-based. */
242 return pThis->iNextLineOrdinal;
243}
244
245
246/** @copydoc RTDBGMODVTDBG::pfnLineAdd */
247static DECLCALLBACK(int) rtDbgModContainer_LineAdd(PRTDBGMODINT pMod, const char *pszFile, size_t cchFile, uint32_t uLineNo,
248 uint32_t iSeg, RTUINTPTR off, uint32_t *piOrdinal)
249{
250 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
251
252 /*
253 * Validate the input address.
254 */
255 AssertMsgReturn(iSeg < pThis->cSegs, ("iSeg=%#x cSegs=%#x\n", pThis->cSegs),
256 VERR_DBG_INVALID_SEGMENT_INDEX);
257 AssertMsgReturn(off < pThis->paSegs[iSeg].cb, ("off=%RTptr cbSeg=%RTptr\n", off, pThis->paSegs[iSeg].cb),
258 VERR_DBG_INVALID_SEGMENT_OFFSET);
259
260 /*
261 * Create a new entry.
262 */
263#ifdef RTDBGMODCNT_WITH_MEM_CACHE
264 PRTDBGMODCTNLINE pLine = (PRTDBGMODCTNLINE)RTMemCacheAlloc(pThis->hLineNumAllocator);
265#else
266 PRTDBGMODCTNLINE pLine = (PRTDBGMODCTNLINE)RTMemAllocZ(sizeof(*pLine));
267#endif
268 if (!pLine)
269 return VERR_NO_MEMORY;
270 pLine->AddrCore.Key = off;
271 pLine->OrdinalCore.Key = pThis->iNextLineOrdinal;
272 pLine->uLineNo = uLineNo;
273 pLine->iSeg = iSeg;
274 pLine->pszFile = RTStrCacheEnterN(g_hDbgModStrCache, pszFile, cchFile);
275 int rc;
276 if (pLine->pszFile)
277 {
278 if (RTAvlUIntPtrInsert(&pThis->paSegs[iSeg].LineAddrTree, &pLine->AddrCore))
279 {
280 if (RTAvlU32Insert(&pThis->LineOrdinalTree, &pLine->OrdinalCore))
281 {
282 if (piOrdinal)
283 *piOrdinal = pThis->iNextLineOrdinal;
284 pThis->iNextLineOrdinal++;
285 return VINF_SUCCESS;
286 }
287
288 rc = VERR_INTERNAL_ERROR_5;
289 RTAvlUIntPtrRemove(&pThis->paSegs[iSeg].LineAddrTree, pLine->AddrCore.Key);
290 }
291
292 /* bail out */
293 rc = VERR_DBG_ADDRESS_CONFLICT;
294 RTStrCacheRelease(g_hDbgModStrCache, pLine->pszFile);
295 }
296 else
297 rc = VERR_NO_MEMORY;
298 RTMemFree(pLine);
299 return rc;
300}
301
302
303/** @copydoc RTDBGMODVTDBG::pfnSymbolByAddr */
304static DECLCALLBACK(int) rtDbgModContainer_SymbolByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
305 PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
306{
307 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
308
309 /*
310 * Validate the input address.
311 */
312 AssertMsgReturn( iSeg == RTDBGSEGIDX_ABS
313 || iSeg < pThis->cSegs,
314 ("iSeg=%#x cSegs=%#x\n", pThis->cSegs),
315 VERR_DBG_INVALID_SEGMENT_INDEX);
316 AssertMsgReturn( iSeg >= RTDBGSEGIDX_SPECIAL_FIRST
317 || off <= pThis->paSegs[iSeg].cb,
318 ("off=%RTptr cbSeg=%RTptr\n", off, pThis->paSegs[iSeg].cb),
319 VERR_DBG_INVALID_SEGMENT_OFFSET);
320
321 /*
322 * Lookup the nearest symbol with an address less or equal to the specified address.
323 */
324 PAVLRUINTPTRNODECORE pAvlCore = RTAvlrUIntPtrGetBestFit( iSeg == RTDBGSEGIDX_ABS
325 ? &pThis->AbsAddrTree
326 : &pThis->paSegs[iSeg].SymAddrTree,
327 off,
328 fFlags == RTDBGSYMADDR_FLAGS_GREATER_OR_EQUAL /*fAbove*/);
329 if (!pAvlCore)
330 return VERR_SYMBOL_NOT_FOUND;
331 PCRTDBGMODCTNSYMBOL pMySym = RT_FROM_MEMBER(pAvlCore, RTDBGMODCTNSYMBOL const, AddrCore);
332 if (poffDisp)
333 *poffDisp = off - pMySym->AddrCore.Key;
334 return rtDbgModContainerReturnSymbol(pMySym, pSymInfo);
335}
336
337
338/** @copydoc RTDBGMODVTDBG::pfnSymbolByName */
339static DECLCALLBACK(int) rtDbgModContainer_SymbolByName(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol, PRTDBGSYMBOL pSymInfo)
340{
341 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
342 NOREF(cchSymbol);
343
344 /*
345 * Look it up in the name space.
346 */
347 PRTSTRSPACECORE pStrCore = RTStrSpaceGet(&pThis->Names, pszSymbol);
348 if (!pStrCore)
349 return VERR_SYMBOL_NOT_FOUND;
350 PCRTDBGMODCTNSYMBOL pMySym = RT_FROM_MEMBER(pStrCore, RTDBGMODCTNSYMBOL const, NameCore);
351 return rtDbgModContainerReturnSymbol(pMySym, pSymInfo);
352}
353
354
355/** @copydoc RTDBGMODVTDBG::pfnSymbolByOrdinal */
356static DECLCALLBACK(int) rtDbgModContainer_SymbolByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
357{
358 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
359
360 /*
361 * Look it up in the ordinal tree.
362 */
363 if (iOrdinal >= pThis->iNextSymbolOrdinal)
364 return pThis->iNextSymbolOrdinal
365 ? VERR_DBG_NO_SYMBOLS
366 : VERR_SYMBOL_NOT_FOUND;
367 PAVLU32NODECORE pAvlCore = RTAvlU32Get(&pThis->SymbolOrdinalTree, iOrdinal);
368 AssertReturn(pAvlCore, VERR_SYMBOL_NOT_FOUND);
369 PCRTDBGMODCTNSYMBOL pMySym = RT_FROM_MEMBER(pAvlCore, RTDBGMODCTNSYMBOL const, OrdinalCore);
370 return rtDbgModContainerReturnSymbol(pMySym, pSymInfo);
371}
372
373
374/** @copydoc RTDBGMODVTDBG::pfnSymbolCount */
375static DECLCALLBACK(uint32_t) rtDbgModContainer_SymbolCount(PRTDBGMODINT pMod)
376{
377 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
378
379 /* Note! The ordinal numbers are 0-based. */
380 return pThis->iNextSymbolOrdinal;
381}
382
383
384/** @copydoc RTDBGMODVTDBG::pfnSymbolAdd */
385static DECLCALLBACK(int) rtDbgModContainer_SymbolAdd(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
386 RTDBGSEGIDX iSeg, RTUINTPTR off, RTUINTPTR cb, uint32_t fFlags,
387 uint32_t *piOrdinal)
388{
389 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
390
391 /*
392 * Address validation. The other arguments have already been validated.
393 */
394 AssertMsgReturn( iSeg == RTDBGSEGIDX_ABS
395 || iSeg < pThis->cSegs,
396 ("iSeg=%#x cSegs=%#x\n", pThis->cSegs),
397 VERR_DBG_INVALID_SEGMENT_INDEX);
398 AssertMsgReturn( iSeg >= RTDBGSEGIDX_SPECIAL_FIRST
399 || off <= pThis->paSegs[iSeg].cb,
400 ("off=%RTptr cb=%RTptr cbSeg=%RTptr\n", off, cb, pThis->paSegs[iSeg].cb),
401 VERR_DBG_INVALID_SEGMENT_OFFSET);
402
403 /* Be a little relaxed wrt to the symbol size. */
404 int rc = VINF_SUCCESS;
405 if (iSeg != RTDBGSEGIDX_ABS && off + cb > pThis->paSegs[iSeg].cb)
406 {
407 cb = pThis->paSegs[iSeg].cb - off;
408 rc = VINF_DBG_ADJUSTED_SYM_SIZE;
409 }
410
411 /*
412 * Create a new entry.
413 */
414 PRTDBGMODCTNSYMBOL pSymbol = (PRTDBGMODCTNSYMBOL)RTMemAllocZ(sizeof(*pSymbol));
415 if (!pSymbol)
416 return VERR_NO_MEMORY;
417
418 pSymbol->AddrCore.Key = off;
419 pSymbol->AddrCore.KeyLast = off + (cb ? cb - 1 : 0);
420 pSymbol->OrdinalCore.Key = pThis->iNextSymbolOrdinal;
421 pSymbol->iSeg = iSeg;
422 pSymbol->cb = cb;
423 pSymbol->fFlags = fFlags;
424 pSymbol->NameCore.pszString = RTStrCacheEnterN(g_hDbgModStrCache, pszSymbol, cchSymbol);
425 if (pSymbol->NameCore.pszString)
426 {
427 if (RTStrSpaceInsert(&pThis->Names, &pSymbol->NameCore))
428 {
429 PAVLRUINTPTRTREE pAddrTree = iSeg == RTDBGSEGIDX_ABS
430 ? &pThis->AbsAddrTree
431 : &pThis->paSegs[iSeg].SymAddrTree;
432 if (RTAvlrUIntPtrInsert(pAddrTree, &pSymbol->AddrCore))
433 {
434 if (RTAvlU32Insert(&pThis->SymbolOrdinalTree, &pSymbol->OrdinalCore))
435 {
436 if (piOrdinal)
437 *piOrdinal = pThis->iNextSymbolOrdinal;
438 pThis->iNextSymbolOrdinal++;
439 return rc;
440 }
441
442 /* bail out */
443 rc = VERR_INTERNAL_ERROR_5;
444 RTAvlrUIntPtrRemove(pAddrTree, pSymbol->AddrCore.Key);
445 }
446 else
447 rc = VERR_DBG_ADDRESS_CONFLICT;
448 RTStrSpaceRemove(&pThis->Names, pSymbol->NameCore.pszString);
449 }
450 else
451 rc = VERR_DBG_DUPLICATE_SYMBOL;
452 RTStrCacheRelease(g_hDbgModStrCache, pSymbol->NameCore.pszString);
453 }
454 else
455 rc = VERR_NO_MEMORY;
456 RTMemFree(pSymbol);
457 return rc;
458}
459
460
461/** @copydoc RTDBGMODVTDBG::pfnSegmentByIndex */
462static DECLCALLBACK(int) rtDbgModContainer_SegmentByIndex(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
463{
464 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
465 if (iSeg >= pThis->cSegs)
466 return VERR_DBG_INVALID_SEGMENT_INDEX;
467 pSegInfo->Address = RTUINTPTR_MAX;
468 pSegInfo->uRva = pThis->paSegs[iSeg].off;
469 pSegInfo->cb = pThis->paSegs[iSeg].cb;
470 pSegInfo->fFlags = pThis->paSegs[iSeg].fFlags;
471 pSegInfo->iSeg = iSeg;
472 strcpy(pSegInfo->szName, pThis->paSegs[iSeg].pszName);
473 return VINF_SUCCESS;
474}
475
476
477/** @copydoc RTDBGMODVTDBG::pfnSegmentCount */
478static DECLCALLBACK(RTDBGSEGIDX) rtDbgModContainer_SegmentCount(PRTDBGMODINT pMod)
479{
480 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
481 return pThis->cSegs;
482}
483
484
485/** @copydoc RTDBGMODVTDBG::pfnSegmentAdd */
486static DECLCALLBACK(int) rtDbgModContainer_SegmentAdd(PRTDBGMODINT pMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName, size_t cchName,
487 uint32_t fFlags, PRTDBGSEGIDX piSeg)
488{
489 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
490
491 /*
492 * Input validation (the bits the caller cannot do).
493 */
494 /* Overlapping segments are not yet supported. Will use flags to deal with it if it becomes necessary. */
495 RTUINTPTR uRvaLast = uRva + RT_MAX(cb, 1) - 1;
496 RTUINTPTR uRvaLastMax = uRvaLast;
497 RTDBGSEGIDX iSeg = pThis->cSegs;
498 while (iSeg-- > 0)
499 {
500 RTUINTPTR uCurRva = pThis->paSegs[iSeg].off;
501 RTUINTPTR uCurRvaLast = uCurRva + RT_MAX(pThis->paSegs[iSeg].cb, 1) - 1;
502 if ( uRva <= uCurRvaLast
503 && uRvaLast >= uCurRva
504 && ( /* HACK ALERT! Allow empty segments to share space (bios/watcom, elf). */
505 (cb != 0 && pThis->paSegs[iSeg].cb != 0)
506 || ( cb == 0
507 && uRva != uCurRva
508 && uRva != uCurRvaLast)
509 || ( pThis->paSegs[iSeg].cb == 0
510 && uCurRva != uRva
511 && uCurRva != uRvaLast)
512 )
513 )
514 AssertMsgFailedReturn(("uRva=%RTptr uRvaLast=%RTptr (cb=%RTptr) \"%s\";\n"
515 "uRva=%RTptr uRvaLast=%RTptr (cb=%RTptr) \"%s\" iSeg=%#x\n",
516 uRva, uRvaLast, cb, pszName,
517 uCurRva, uCurRvaLast, pThis->paSegs[iSeg].cb, pThis->paSegs[iSeg].pszName, iSeg),
518 VERR_DBG_SEGMENT_INDEX_CONFLICT);
519 if (uRvaLastMax < uCurRvaLast)
520 uRvaLastMax = uCurRvaLast;
521 }
522 /* Strict ordered segment addition at the moment. */
523 iSeg = pThis->cSegs;
524 AssertMsgReturn(!piSeg || *piSeg == NIL_RTDBGSEGIDX || *piSeg == iSeg,
525 ("iSeg=%#x *piSeg=%#x\n", iSeg, *piSeg),
526 VERR_DBG_INVALID_SEGMENT_INDEX);
527
528 /*
529 * Add an entry to the segment table, extending it if necessary.
530 */
531 if (!(iSeg % 8))
532 {
533 void *pvSegs = RTMemRealloc(pThis->paSegs, sizeof(RTDBGMODCTNSEGMENT) * (iSeg + 8));
534 if (!pvSegs)
535 return VERR_NO_MEMORY;
536 pThis->paSegs = (PRTDBGMODCTNSEGMENT)pvSegs;
537 }
538
539 pThis->paSegs[iSeg].SymAddrTree = NULL;
540 pThis->paSegs[iSeg].LineAddrTree = NULL;
541 pThis->paSegs[iSeg].off = uRva;
542 pThis->paSegs[iSeg].cb = cb;
543 pThis->paSegs[iSeg].fFlags = fFlags;
544 pThis->paSegs[iSeg].pszName = RTStrCacheEnterN(g_hDbgModStrCache, pszName, cchName);
545 if (pThis->paSegs[iSeg].pszName)
546 {
547 if (piSeg)
548 *piSeg = iSeg;
549 pThis->cSegs++;
550 pThis->cb = uRvaLastMax + 1;
551 if (!pThis->cb)
552 pThis->cb = RTUINTPTR_MAX;
553 return VINF_SUCCESS;
554 }
555 return VERR_NO_MEMORY;
556}
557
558
559/** @copydoc RTDBGMODVTDBG::pfnImageSize */
560static DECLCALLBACK(RTUINTPTR) rtDbgModContainer_ImageSize(PRTDBGMODINT pMod)
561{
562 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
563 return pThis->cb;
564}
565
566
567/** @copydoc RTDBGMODVTDBG::pfnRvaToSegOff */
568static DECLCALLBACK(RTDBGSEGIDX) rtDbgModContainer_RvaToSegOff(PRTDBGMODINT pMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
569{
570 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
571 PCRTDBGMODCTNSEGMENT paSeg = pThis->paSegs;
572 uint32_t const cSegs = pThis->cSegs;
573 if (cSegs <= 7)
574 {
575 /*
576 * Linear search.
577 */
578 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
579 {
580 RTUINTPTR offSeg = uRva - paSeg[iSeg].off;
581 if (offSeg < paSeg[iSeg].cb)
582 {
583 if (poffSeg)
584 *poffSeg = offSeg;
585 return iSeg;
586 }
587 }
588 }
589 else
590 {
591 /*
592 * Binary search.
593 */
594 uint32_t iFirst = 0;
595 uint32_t iLast = cSegs - 1;
596 for (;;)
597 {
598 uint32_t iSeg = iFirst + (iLast - iFirst) / 2;
599 RTUINTPTR offSeg = uRva - paSeg[iSeg].off;
600 if (offSeg < paSeg[iSeg].cb)
601 {
602#if 0 /* Enable if we change the above test. */
603 if (offSeg == 0 && paSeg[iSeg].cb == 0)
604 while ( iSeg > 0
605 && paSeg[iSeg - 1].cb == 0
606 && paSeg[iSeg - 1].off == uRva)
607 iSeg--;
608#endif
609
610 if (poffSeg)
611 *poffSeg = offSeg;
612 return iSeg;
613 }
614
615 /* advance */
616 if (uRva < paSeg[iSeg].off)
617 {
618 /* between iFirst and iSeg. */
619 if (iSeg == iFirst)
620 break;
621 iLast = iSeg - 1;
622 }
623 else
624 {
625 /* between iSeg and iLast. paSeg[iSeg].cb == 0 ends up here too. */
626 if (iSeg == iLast)
627 break;
628 iFirst = iSeg + 1;
629 }
630 }
631 }
632
633 /* Invalid. */
634 return NIL_RTDBGSEGIDX;
635}
636
637
638/** Destroy a line number node. */
639static DECLCALLBACK(int) rtDbgModContainer_DestroyTreeLineNode(PAVLU32NODECORE pNode, void *pvUser)
640{
641 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pvUser;
642 PRTDBGMODCTNLINE pLine = RT_FROM_MEMBER(pNode, RTDBGMODCTNLINE, OrdinalCore);
643 RTStrCacheRelease(g_hDbgModStrCache, pLine->pszFile);
644 pLine->pszFile = NULL;
645#ifdef RTDBGMODCNT_WITH_MEM_CACHE
646 RTMemCacheFree(pThis->hLineNumAllocator, pLine);
647#else
648 RTMemFree(pLine); NOREF(pThis);
649#endif
650 return 0;
651}
652
653
654/** Destroy a symbol node. */
655static DECLCALLBACK(int) rtDbgModContainer_DestroyTreeNode(PAVLRUINTPTRNODECORE pNode, void *pvUser)
656{
657 PRTDBGMODCTNSYMBOL pSym = RT_FROM_MEMBER(pNode, RTDBGMODCTNSYMBOL, AddrCore);
658 RTStrCacheRelease(g_hDbgModStrCache, pSym->NameCore.pszString);
659 pSym->NameCore.pszString = NULL;
660 RTMemFree(pSym);
661 NOREF(pvUser);
662 return 0;
663}
664
665
666/** @copydoc RTDBGMODVTDBG::pfnClose */
667static DECLCALLBACK(int) rtDbgModContainer_Close(PRTDBGMODINT pMod)
668{
669 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
670
671 /*
672 * Destroy the symbols and instance data.
673 */
674 for (uint32_t iSeg = 0; iSeg < pThis->cSegs; iSeg++)
675 {
676 RTAvlrUIntPtrDestroy(&pThis->paSegs[iSeg].SymAddrTree, rtDbgModContainer_DestroyTreeNode, NULL);
677 RTStrCacheRelease(g_hDbgModStrCache, pThis->paSegs[iSeg].pszName);
678 pThis->paSegs[iSeg].pszName = NULL;
679 }
680
681 RTAvlrUIntPtrDestroy(&pThis->AbsAddrTree, rtDbgModContainer_DestroyTreeNode, NULL);
682 pThis->Names = NULL;
683
684#ifdef RTDBGMODCNT_WITH_MEM_CACHE
685 RTMemCacheDestroy(pThis->hLineNumAllocator);
686 pThis->hLineNumAllocator = NIL_RTMEMCACHE;
687#else
688 RTAvlU32Destroy(&pThis->LineOrdinalTree, rtDbgModContainer_DestroyTreeLineNode, pThis);
689#endif
690
691 RTMemFree(pThis->paSegs);
692 pThis->paSegs = NULL;
693
694 RTMemFree(pThis);
695
696 return VINF_SUCCESS;
697}
698
699
700/** @copydoc RTDBGMODVTDBG::pfnTryOpen */
701static DECLCALLBACK(int) rtDbgModContainer_TryOpen(PRTDBGMODINT pMod, RTLDRARCH enmArch)
702{
703 NOREF(pMod); NOREF(enmArch);
704 return VERR_INTERNAL_ERROR_5;
705}
706
707
708
709/** Virtual function table for the debug info container. */
710DECL_HIDDEN_CONST(RTDBGMODVTDBG) const g_rtDbgModVtDbgContainer =
711{
712 /*.u32Magic = */ RTDBGMODVTDBG_MAGIC,
713 /*.fSupports = */ 0, /* (Don't call my TryOpen, please.) */
714 /*.pszName = */ "container",
715 /*.pfnTryOpen = */ rtDbgModContainer_TryOpen,
716 /*.pfnClose = */ rtDbgModContainer_Close,
717
718 /*.pfnRvaToSegOff = */ rtDbgModContainer_RvaToSegOff,
719 /*.pfnImageSize = */ rtDbgModContainer_ImageSize,
720
721 /*.pfnSegmentAdd = */ rtDbgModContainer_SegmentAdd,
722 /*.pfnSegmentCount = */ rtDbgModContainer_SegmentCount,
723 /*.pfnSegmentByIndex = */ rtDbgModContainer_SegmentByIndex,
724
725 /*.pfnSymbolAdd = */ rtDbgModContainer_SymbolAdd,
726 /*.pfnSymbolCount = */ rtDbgModContainer_SymbolCount,
727 /*.pfnSymbolByOrdinal = */ rtDbgModContainer_SymbolByOrdinal,
728 /*.pfnSymbolByName = */ rtDbgModContainer_SymbolByName,
729 /*.pfnSymbolByAddr = */ rtDbgModContainer_SymbolByAddr,
730
731 /*.pfnLineAdd = */ rtDbgModContainer_LineAdd,
732 /*.pfnLineCount = */ rtDbgModContainer_LineCount,
733 /*.pfnLineByOrdinal = */ rtDbgModContainer_LineByOrdinal,
734 /*.pfnLineByAddr = */ rtDbgModContainer_LineByAddr,
735
736 /*.u32EndMagic = */ RTDBGMODVTDBG_MAGIC
737};
738
739
740
741/**
742 * Special container operation for removing all symbols.
743 *
744 * @returns IPRT status code.
745 * @param pMod The module instance.
746 */
747DECLHIDDEN(int) rtDbgModContainer_SymbolRemoveAll(PRTDBGMODINT pMod)
748{
749 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
750
751 for (uint32_t iSeg = 0; iSeg < pThis->cSegs; iSeg++)
752 {
753 RTAvlrUIntPtrDestroy(&pThis->paSegs[iSeg].SymAddrTree, rtDbgModContainer_DestroyTreeNode, NULL);
754 Assert(pThis->paSegs[iSeg].SymAddrTree == NULL);
755 }
756
757 RTAvlrUIntPtrDestroy(&pThis->AbsAddrTree, rtDbgModContainer_DestroyTreeNode, NULL);
758 Assert(pThis->AbsAddrTree == NULL);
759
760 pThis->Names = NULL;
761 pThis->iNextSymbolOrdinal = 0;
762
763 return VINF_SUCCESS;
764}
765
766
767/**
768 * Special container operation for removing all line numbers.
769 *
770 * @returns IPRT status code.
771 * @param pMod The module instance.
772 */
773DECLHIDDEN(int) rtDbgModContainer_LineRemoveAll(PRTDBGMODINT pMod)
774{
775 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
776
777 for (uint32_t iSeg = 0; iSeg < pThis->cSegs; iSeg++)
778 pThis->paSegs[iSeg].LineAddrTree = NULL;
779
780 RTAvlU32Destroy(&pThis->LineOrdinalTree, rtDbgModContainer_DestroyTreeLineNode, pThis);
781 Assert(pThis->LineOrdinalTree == NULL);
782
783 pThis->iNextLineOrdinal = 0;
784
785 return VINF_SUCCESS;
786}
787
788
789/**
790 * Special container operation for removing everything.
791 *
792 * @returns IPRT status code.
793 * @param pMod The module instance.
794 */
795DECLHIDDEN(int) rtDbgModContainer_RemoveAll(PRTDBGMODINT pMod)
796{
797 PRTDBGMODCTN pThis = (PRTDBGMODCTN)pMod->pvDbgPriv;
798
799 rtDbgModContainer_LineRemoveAll(pMod);
800 rtDbgModContainer_SymbolRemoveAll(pMod);
801
802 for (uint32_t iSeg = 0; iSeg < pThis->cSegs; iSeg++)
803 {
804 RTStrCacheRelease(g_hDbgModStrCache, pThis->paSegs[iSeg].pszName);
805 pThis->paSegs[iSeg].pszName = NULL;
806 }
807
808 pThis->cSegs = 0;
809 pThis->cb = 0;
810
811 return VINF_SUCCESS;
812}
813
814
815/**
816 * Creates a generic debug info container and associates it with the module.
817 *
818 * @returns IPRT status code.
819 * @param pMod The module instance.
820 * @param cbSeg The size of the initial segment. 0 if segments are to be
821 * created manually later on.
822 */
823int rtDbgModContainerCreate(PRTDBGMODINT pMod, RTUINTPTR cbSeg)
824{
825 PRTDBGMODCTN pThis = (PRTDBGMODCTN)RTMemAlloc(sizeof(*pThis));
826 if (!pThis)
827 return VERR_NO_MEMORY;
828
829 pThis->Names = NULL;
830 pThis->AbsAddrTree = NULL;
831 pThis->SymbolOrdinalTree = NULL;
832 pThis->LineOrdinalTree = NULL;
833 pThis->paSegs = NULL;
834 pThis->cSegs = 0;
835 pThis->cb = 0;
836 pThis->iNextSymbolOrdinal = 0;
837 pThis->iNextLineOrdinal = 0;
838
839 pMod->pDbgVt = &g_rtDbgModVtDbgContainer;
840 pMod->pvDbgPriv = pThis;
841
842#ifdef RTDBGMODCNT_WITH_MEM_CACHE
843 int rc = RTMemCacheCreate(&pThis->hLineNumAllocator, sizeof(RTDBGMODCTNLINE), sizeof(void *), UINT32_MAX,
844 NULL /*pfnCtor*/, NULL /*pfnDtor*/, NULL /*pvUser*/, 0 /*fFlags*/);
845#else
846 int rc = VINF_SUCCESS;
847#endif
848 if (RT_SUCCESS(rc))
849 {
850 /*
851 * Add the initial segment.
852 */
853 if (cbSeg)
854 rc = rtDbgModContainer_SegmentAdd(pMod, 0, cbSeg, "default", sizeof("default") - 1, 0, NULL);
855 if (RT_SUCCESS(rc))
856 return rc;
857
858#ifdef RTDBGMODCNT_WITH_MEM_CACHE
859 RTMemCacheDestroy(pThis->hLineNumAllocator);
860#endif
861 }
862
863 RTMemFree(pThis);
864 pMod->pDbgVt = NULL;
865 pMod->pvDbgPriv = NULL;
866 return rc;
867}
868
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