1 | /* $Id: dbgmod.cpp 20356 2009-06-07 13:22:52Z 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 | RTDECL(int) RTDbgModCreate(PRTDBGMOD phDbgMod, const char *pszName, RTUINTPTR cb, uint32_t fFlags)
|
---|
145 | {
|
---|
146 | /*
|
---|
147 | * Input validation and lazy initialization.
|
---|
148 | */
|
---|
149 | AssertPtrReturn(phDbgMod, VERR_INVALID_POINTER);
|
---|
150 | *phDbgMod = NIL_RTDBGMOD;
|
---|
151 | AssertPtrReturn(pszName, VERR_INVALID_POINTER);
|
---|
152 | AssertReturn(*pszName, VERR_INVALID_PARAMETER);
|
---|
153 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
154 | AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER);
|
---|
155 |
|
---|
156 | int rc = rtDbgModLazyInit();
|
---|
157 | if (RT_FAILURE(rc))
|
---|
158 | return rc;
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * Allocate a new module instance.
|
---|
162 | */
|
---|
163 | PRTDBGMODINT pDbgMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgMod));
|
---|
164 | if (!pDbgMod)
|
---|
165 | return VERR_NO_MEMORY;
|
---|
166 | pDbgMod->u32Magic = RTDBGMOD_MAGIC;
|
---|
167 | pDbgMod->cRefs = 1;
|
---|
168 | rc = RTCritSectInit(&pDbgMod->CritSect);
|
---|
169 | if (RT_SUCCESS(rc))
|
---|
170 | {
|
---|
171 | pDbgMod->pszName = RTStrDup(pszName);
|
---|
172 | if (pDbgMod->pszName)
|
---|
173 | {
|
---|
174 | rc = rtDbgModContainerCreate(pDbgMod, cb);
|
---|
175 | if (RT_SUCCESS(rc))
|
---|
176 | {
|
---|
177 | *phDbgMod = pDbgMod;
|
---|
178 | return rc;
|
---|
179 | }
|
---|
180 | RTStrFree(pDbgMod->pszName);
|
---|
181 | }
|
---|
182 | RTCritSectDelete(&pDbgMod->CritSect);
|
---|
183 | }
|
---|
184 |
|
---|
185 | RTMemFree(pDbgMod);
|
---|
186 | return rc;
|
---|
187 | }
|
---|
188 |
|
---|
189 | RTDECL(int) RTDbgModCreateFromImage(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName, uint32_t fFlags)
|
---|
190 | {
|
---|
191 | return VERR_NOT_IMPLEMENTED;
|
---|
192 | }
|
---|
193 |
|
---|
194 | RTDECL(int) RTDbgModCreateFromMap(PRTDBGMOD phDbgMod, const char *pszFilename, const char *pszName, RTUINTPTR uSubtrahend, uint32_t fFlags)
|
---|
195 | {
|
---|
196 | return VERR_NOT_IMPLEMENTED;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Destroys an module after the reference count has reached zero.
|
---|
202 | *
|
---|
203 | * @param pDbgMod The module instance.
|
---|
204 | */
|
---|
205 | static void rtDbgModDestroy(PRTDBGMODINT pDbgMod)
|
---|
206 | {
|
---|
207 | /*
|
---|
208 | * Close the debug info interpreter first, then the image interpret.
|
---|
209 | */
|
---|
210 | RTCritSectEnter(&pDbgMod->CritSect); /* paranoia */
|
---|
211 |
|
---|
212 | if (pDbgMod->pDbgVt)
|
---|
213 | {
|
---|
214 | pDbgMod->pDbgVt->pfnClose(pDbgMod);
|
---|
215 | pDbgMod->pDbgVt = NULL;
|
---|
216 | pDbgMod->pvDbgPriv = NULL;
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (pDbgMod->pImgVt)
|
---|
220 | {
|
---|
221 | pDbgMod->pImgVt->pfnClose(pDbgMod);
|
---|
222 | pDbgMod->pImgVt = NULL;
|
---|
223 | pDbgMod->pvImgPriv = NULL;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /*
|
---|
227 | * Free the resources.
|
---|
228 | */
|
---|
229 | ASMAtomicWriteU32(&pDbgMod->u32Magic, ~RTDBGMOD_MAGIC);
|
---|
230 | RTStrFree(pDbgMod->pszName);
|
---|
231 | RTStrFree(pDbgMod->pszImgFile);
|
---|
232 | RTStrFree(pDbgMod->pszDbgFile);
|
---|
233 | RTCritSectLeave(&pDbgMod->CritSect); /* paranoia */
|
---|
234 | RTCritSectDelete(&pDbgMod->CritSect);
|
---|
235 | RTMemFree(pDbgMod);
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Retains another reference to the module.
|
---|
241 | *
|
---|
242 | * @returns New reference count, UINT32_MAX on invalid handle (asserted).
|
---|
243 | *
|
---|
244 | * @param hDbgMod The module handle.
|
---|
245 | *
|
---|
246 | * @remarks Will not take any locks.
|
---|
247 | */
|
---|
248 | RTDECL(uint32_t) RTDbgModRetain(RTDBGMOD hDbgMod)
|
---|
249 | {
|
---|
250 | PRTDBGMODINT pDbgMod = hDbgMod;
|
---|
251 | RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
|
---|
252 | return ASMAtomicIncU32(&pDbgMod->cRefs);
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Release a reference to the module.
|
---|
258 | *
|
---|
259 | * When the reference count reaches zero, the module is destroyed.
|
---|
260 | *
|
---|
261 | * @returns New reference count, UINT32_MAX on invalid handle (asserted).
|
---|
262 | *
|
---|
263 | * @param hDbgMod The module handle. The NIL handle is quietly ignored
|
---|
264 | * and 0 is returned.
|
---|
265 | *
|
---|
266 | * @remarks Will not take any locks.
|
---|
267 | */
|
---|
268 | RTDECL(uint32_t) RTDbgModRelease(RTDBGMOD hDbgMod)
|
---|
269 | {
|
---|
270 | if (hDbgMod == NIL_RTDBGMOD)
|
---|
271 | return 0;
|
---|
272 | PRTDBGMODINT pDbgMod = hDbgMod;
|
---|
273 | RTDBGMOD_VALID_RETURN_RC(pDbgMod, UINT32_MAX);
|
---|
274 |
|
---|
275 | uint32_t cRefs = ASMAtomicDecU32(&pDbgMod->cRefs);
|
---|
276 | if (!cRefs)
|
---|
277 | rtDbgModDestroy(pDbgMod);
|
---|
278 | return cRefs;
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Gets the module name.
|
---|
284 | *
|
---|
285 | * @returns Pointer to a read only string containing the name.
|
---|
286 | *
|
---|
287 | * @param hDbgMod The module handle.
|
---|
288 | */
|
---|
289 | RTDECL(const char *) RTDbgModName(RTDBGMOD hDbgMod)
|
---|
290 | {
|
---|
291 | PRTDBGMODINT pDbgMod = hDbgMod;
|
---|
292 | RTDBGMOD_VALID_RETURN_RC(pDbgMod, NULL);
|
---|
293 | return pDbgMod->pszName;
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 | RTDECL(RTUINTPTR) RTDbgModImageSize(RTDBGMOD hDbgMod)
|
---|
298 | {
|
---|
299 | return 1;
|
---|
300 | }
|
---|
301 |
|
---|
302 | RTDECL(RTUINTPTR) RTDbgModSegmentSize(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg)
|
---|
303 | {
|
---|
304 | return 1;
|
---|
305 | }
|
---|
306 |
|
---|
307 | RTDECL(RTDBGSEGIDX) RTDbgModSegmentCount(RTDBGMOD hDbgMod)
|
---|
308 | {
|
---|
309 | return 1;
|
---|
310 | }
|
---|
311 |
|
---|
312 | RTDECL(int) RTDbgModSymbolAdd(RTDBGMOD hDbgMod, const char *pszSymbol, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t cb)
|
---|
313 | {
|
---|
314 | return VERR_NOT_IMPLEMENTED;
|
---|
315 | }
|
---|
316 |
|
---|
317 | RTDECL(uint32_t) RTDbgModSymbolCount(RTDBGMOD hDbgMod)
|
---|
318 | {
|
---|
319 | return 1;
|
---|
320 | }
|
---|
321 |
|
---|
322 | RTDECL(int) RTDbgModSymbolByIndex(RTDBGMOD hDbgMod, uint32_t iSymbol, PRTDBGSYMBOL pSymbol)
|
---|
323 | {
|
---|
324 | return VERR_NOT_IMPLEMENTED;
|
---|
325 | }
|
---|
326 |
|
---|
327 | RTDECL(int) RTDbgModSymbolByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGSYMBOL pSymbol)
|
---|
328 | {
|
---|
329 | return VERR_NOT_IMPLEMENTED;
|
---|
330 | }
|
---|
331 |
|
---|
332 | RTDECL(int) RTDbgModSymbolByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGSYMBOL *ppSymbol)
|
---|
333 | {
|
---|
334 | return VERR_NOT_IMPLEMENTED;
|
---|
335 | }
|
---|
336 |
|
---|
337 | RTDECL(int) RTDbgModSymbolByName(RTDBGMOD hDbgMod, const char *pszSymbol, PRTDBGSYMBOL pSymbol)
|
---|
338 | {
|
---|
339 | return VERR_NOT_IMPLEMENTED;
|
---|
340 | }
|
---|
341 |
|
---|
342 | RTDECL(int) RTDbgModSymbolByNameA(RTDBGMOD hDbgMod, const char *pszSymbol, PRTDBGSYMBOL *ppSymbol)
|
---|
343 | {
|
---|
344 | return VERR_NOT_IMPLEMENTED;
|
---|
345 | }
|
---|
346 |
|
---|
347 |
|
---|
348 | RTDECL(int) RTDbgModLineByAddr(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE pLine)
|
---|
349 | {
|
---|
350 | return VERR_NOT_IMPLEMENTED;
|
---|
351 | }
|
---|
352 |
|
---|
353 | RTDECL(int) RTDbgModLineByAddrA(RTDBGMOD hDbgMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTINTPTR poffDisp, PRTDBGLINE *ppLine)
|
---|
354 | {
|
---|
355 | return VERR_NOT_IMPLEMENTED;
|
---|
356 | }
|
---|
357 |
|
---|