1 | /* $Id: dbgmod.cpp 20364 2009-06-08 00:17:43Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Debug Module Interpreter.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include <iprt/dbg.h>
|
---|
35 |
|
---|
36 | #include <iprt/asm.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/avl.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include <iprt/initterm.h>
|
---|
41 | #include <iprt/mem.h>
|
---|
42 | #include <iprt/once.h>
|
---|
43 | #include <iprt/param.h>
|
---|
44 | #include <iprt/semaphore.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 regisration record. */
|
---|
54 | typedef 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;
|
---|
63 | typedef RTDBGMODREGDBG *PRTDBGMODREGDBG;
|
---|
64 |
|
---|
65 | /** Image interpreter regisration record. */
|
---|
66 | typedef 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;
|
---|
75 | typedef 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. */
|
---|
109 | static RTONCE g_rtDbgModOnce = RTONCE_INITIALIZER;
|
---|
110 | /** Read/Write semaphore protecting the list of registered interpreters. */
|
---|
111 | static RTSEMRW g_hDbgModRWSem = NIL_RTSEMRW;
|
---|
112 | /** List of registered image interpreters. */
|
---|
113 | static RTDBGMODREGIMG g_pImgHead;
|
---|
114 | /** List of registered debug infor interpreters. */
|
---|
115 | static RTDBGMODREGDBG g_pDbgHead;
|
---|
116 |
|
---|
117 |
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Do-once callback that initializes the read/write semaphore and registers
|
---|
121 | * the built-in interpreters.
|
---|
122 | *
|
---|
123 | * @returns IPRT status code.
|
---|
124 | * @param pvUser1 NULL.
|
---|
125 | * @param pvUser2 NULL.
|
---|
126 | */
|
---|
127 | static DECLCALLBACK(int) rtDbgModInitOnce(void *pvUser1, void *pvUser2)
|
---|
128 | {
|
---|
129 | int rc = RTSemRWCreate(&g_hDbgModRWSem);
|
---|
130 | AssertRCReturn(rc, rc);
|
---|
131 |
|
---|
132 | /* Register them. */
|
---|
133 |
|
---|
134 | return rc;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | DECLINLINE(int) rtDbgModLazyInit(void)
|
---|
139 | {
|
---|
140 | return RTOnce(&g_rtDbgModOnce, rtDbgModInitOnce, NULL, NULL);
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Creates a module based on the default debug info container.
|
---|
146 | *
|
---|
147 | * This can be used to manually load a module and its symbol.
|
---|
148 | *
|
---|
149 | * @returns IPRT status code.
|
---|
150 | *
|
---|
151 | * @param phDbgMod Where to return the module handle.
|
---|
152 | * @param pszName The name of the module (mandatory).
|
---|
153 | * @param cb The size of the module. Must be greater than zero.
|
---|
154 | * @param fFlags Flags reserved for future extensions, MBZ for now.
|
---|
155 | */
|
---|
156 | RTDECL(int) RTDbgModCreate(PRTDBGMOD phDbgMod, const char *pszName, RTUINTPTR cb, uint32_t fFlags)
|
---|
157 | {
|
---|
158 | /*
|
---|
159 | * Input validation and lazy initialization.
|
---|
160 | */
|
---|
161 | AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
|
---|
162 | *phDbgMod = NIL_RTDBGMOD;
|
---|
163 | AssertPtrReturn(pszName, VERR_INVALID_POINTER);
|
---|
164 | AssertReturn(*pszName, VERR_INVALID_PARAMETER);
|
---|
165 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
166 | AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER);
|
---|
167 |
|
---|
168 | int rc = rtDbgModLazyInit();
|
---|
169 | if (RT_FAILURE(rc))
|
---|
170 | return rc;
|
---|
171 |
|
---|
172 | /*
|
---|
173 | * Allocate a new module instance.
|
---|
174 | */
|
---|
175 | PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
|
---|
176 | if (!pDbgMod)
|
---|
177 | return VERR_NO_MEMORY;
|
---|
178 | pDbgMod->u32Magic = RTDBGMOD_MAGIC;
|
---|
179 | pDbgMod->cRefs = 1;
|
---|
180 | rc = RTCritSectInit(&pDbgMod->CritSect);
|
---|
181 | if (RT_SUCCESS(rc))
|
---|
182 | {
|
---|
183 | pDbgMod->pszName = RTStrDup(pszName);
|
---|
184 | if (pDbgMod->pszName)
|
---|
185 | {
|
---|
186 | rc = rtDbgModContainerCreate(pDbgMod, cb);
|
---|
187 | if (RT_SUCCESS(rc))
|
---|
188 | {
|
---|
189 | *phDbgMod = pDbgMod;
|
---|
190 | return rc;
|
---|
191 | }
|
---|
192 | RTStrFree(pDbgMod->pszName);
|
---|
193 | }
|
---|
194 | RTCritSectDelete(&pDbgMod->CritSect);
|
---|
195 | }
|
---|
196 |
|
---|
197 | RTMemFree(pDbgMod);
|
---|
198 | return rc;
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | RTDECL(int) RTDbgModCreateDeferred(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName, RTUINTPTR cb, uint32_t fFlags)
|
---|
203 | {
|
---|
204 | return VERR_NOT_IMPLEMENTED;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | RTDECL(int) RTDbgModCreateFromImage(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName, uint32_t fFlags)
|
---|
209 | {
|
---|
210 | return VERR_NOT_IMPLEMENTED;
|
---|
211 | }
|
---|
212 |
|
---|
213 | RTDECL(int) RTDbgModCreateFromMap(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName, RTUINTPTR uSubtrahend, uint32_t fFlags)
|
---|
214 | {
|
---|
215 | return VERR_NOT_IMPLEMENTED;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Destroys an module after the reference count has reached zero.
|
---|
221 | *
|
---|
222 | * @param pDbgMod The module instance.
|
---|
223 | */
|
---|
224 | static void rtDbgModDestroy(PRTDBGMODINT pDbgMod)
|
---|
225 | {
|
---|
226 | /*
|
---|
227 | * Close the debug info interpreter first, then the image interpret.
|
---|
228 | */
|
---|
229 | RTCritSectEnter(&pDbgMod->CritSect); /* paranoia */
|
---|
230 |
|
---|
231 | if (pDbgMod->pDbgVt)
|
---|
232 | {
|
---|
233 | pDbgMod->pDbgVt->pfnClose(pDbgMod);
|
---|
234 | pDbgMod->pDbgVt = NULL;
|
---|
235 | pDbgMod->pvDbgPriv = NULL;
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (pDbgMod->pImgVt)
|
---|
239 | {
|
---|
240 | pDbgMod->pImgVt->pfnClose(pDbgMod);
|
---|
241 | pDbgMod->pImgVt = NULL;
|
---|
242 | pDbgMod->pvImgPriv = NULL;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /*
|
---|
246 | * Free the resources.
|
---|
247 | */
|
---|
248 | ASMAtomicWriteU32(&pDbgMod->u32Magic, ~RTDBGMOD_MAGIC);
|
---|
249 | RTStrFree(pDbgMod->pszName);
|
---|
250 | RTStrFree(pDbgMod->pszImgFile);
|
---|
251 | RTStrFree(pDbgMod->pszDbgFile);
|
---|
252 | RTCritSectLeave(&pDbgMod->CritSect); /* paranoia */
|
---|
253 | RTCritSectDelete(&pDbgMod->CritSect);
|
---|
254 | RTMemFree(pDbgMod);
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Retains another reference to the module.
|
---|
260 | *
|
---|
261 | * @returns New reference count, UINT32_MAX on invalid handle (asserted).
|
---|
262 | *
|
---|
263 | * @param hDbgMod The module handle.
|
---|
264 | *
|
---|
265 | * @remarks Will not take any locks.
|
---|
266 | */
|
---|
267 | RTDECL(uint32_t) RTDbgModRetain(RTDBGMOD hDbgMod)
|
---|
268 | {
|
---|
269 | PRTDBGMODINT pDbgMod = hDbgMod;
|
---|
270 | RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
|
---|
271 | return ASMAtomicIncU32(&pDbgMod->cRefs);
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Release a reference to the module.
|
---|
277 | *
|
---|
278 | * When the reference count reaches zero, the module is destroyed.
|
---|
279 | *
|
---|
280 | * @returns New reference count, UINT32_MAX on invalid handle (asserted).
|
---|
281 | *
|
---|
282 | * @param hDbgMod The module handle. The NIL handle is quietly ignored
|
---|
283 | * and 0 is returned.
|
---|
284 | *
|
---|
285 | * @remarks Will not take any locks.
|
---|
286 | */
|
---|
287 | RTDECL(uint32_t) RTDbgModRelease(RTDBGMOD hDbgMod)
|
---|
288 | {
|
---|
289 | if (hDbgMod == NIL_RTDBGMOD)
|
---|
290 | return 0;
|
---|
291 | PRTDBGMODINT pDbgMod = hDbgMod;
|
---|
292 | RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
|
---|
293 |
|
---|
294 | uint32_t cRefs = ASMAtomicDecU32(&pDbgMod->cRefs);
|
---|
295 | if (!cRefs)
|
---|
296 | rtDbgModDestroy(pDbgMod);
|
---|
297 | return cRefs;
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Gets the module name.
|
---|
303 | *
|
---|
304 | * @returns Pointer to a read only string containing the name.
|
---|
305 | *
|
---|
306 | * @param hDbgMod The module handle.
|
---|
307 | */
|
---|
308 | RTDECL(const char *) RTDbgModName(RTDBGMOD hDbgMod)
|
---|
309 | {
|
---|
310 | PRTDBGMODINT pDbgMod = hDbgMod;
|
---|
311 | RTDBGMOD_VALID_RETURN_RC(pDbgMod, NULL);
|
---|
312 | return pDbgMod->pszName;
|
---|
313 | }
|
---|
314 |
|
---|
315 |
|
---|
316 | RTDECL(RTUINTPTR) RTDbgModImageSize(RTDBGMOD hDbgMod)
|
---|
317 | {
|
---|
318 | return 1;
|
---|
319 | }
|
---|
320 |
|
---|
321 | RTDECL(RTUINTPTR) RTDbgModSegmentSize(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg)
|
---|
322 | {
|
---|
323 | return 1;
|
---|
324 | }
|
---|
325 |
|
---|
326 | RTDECL(RTDBGSEGIDX) RTDbgModSegmentCount(RTDBGMOD hDbgMod)
|
---|
327 | {
|
---|
328 | return 1;
|
---|
329 | }
|
---|
330 |
|
---|
331 | RTDECL(int) RTDbgModSymbolAdd(RTDBGMOD hDbgMod, const char *pszSymbol, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t cb)
|
---|
332 | {
|
---|
333 | return VERR_NOT_IMPLEMENTED;
|
---|
334 | }
|
---|
335 |
|
---|
336 | RTDECL(uint32_t) RTDbgModSymbolCount(RTDBGMOD hDbgMod)
|
---|
337 | {
|
---|
338 | return 1;
|
---|
339 | }
|
---|
340 |
|
---|
341 | RTDECL(int) RTDbgModSymbolByIndex(RTDBGMOD hDbgMod, uint32_t iSymbol, PRTDBGSYMBOL pSymbol)
|
---|
342 | {
|
---|
343 | return VERR_NOT_IMPLEMENTED;
|
---|
344 | }
|
---|
345 |
|
---|
346 | RTDECL(int) RTDbgModSymbolByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGSYMBOL pSymbol)
|
---|
347 | {
|
---|
348 | return VERR_NOT_IMPLEMENTED;
|
---|
349 | }
|
---|
350 |
|
---|
351 | RTDECL(int) RTDbgModSymbolByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGSYMBOL *ppSymbol)
|
---|
352 | {
|
---|
353 | return VERR_NOT_IMPLEMENTED;
|
---|
354 | }
|
---|
355 |
|
---|
356 | RTDECL(int) RTDbgModSymbolByName(RTDBGMOD hDbgMod, const char *pszSymbol, PRTDBGSYMBOL pSymbol)
|
---|
357 | {
|
---|
358 | return VERR_NOT_IMPLEMENTED;
|
---|
359 | }
|
---|
360 |
|
---|
361 | RTDECL(int) RTDbgModSymbolByNameA(RTDBGMOD hDbgMod, const char *pszSymbol, PRTDBGSYMBOL *ppSymbol)
|
---|
362 | {
|
---|
363 | return VERR_NOT_IMPLEMENTED;
|
---|
364 | }
|
---|
365 |
|
---|
366 |
|
---|
367 | RTDECL(int) RTDbgModLineByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE pLine)
|
---|
368 | {
|
---|
369 | return VERR_NOT_IMPLEMENTED;
|
---|
370 | }
|
---|
371 |
|
---|
372 | RTDECL(int) RTDbgModLineByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE *ppLine)
|
---|
373 | {
|
---|
374 | return VERR_NOT_IMPLEMENTED;
|
---|
375 | }
|
---|
376 |
|
---|