VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/dbg/dbgmodldr.cpp@ 46161

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

First part of loading .dSYM bundles.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1/* $Id: dbgmodldr.cpp 46161 2013-05-19 13:31:13Z vboxsync $ */
2/** @file
3 * IPRT - Debug Module Image Interpretation by RTLdr.
4 */
5
6/*
7 * Copyright (C) 2011 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/assert.h>
35#include <iprt/err.h>
36#include <iprt/file.h>
37#include <iprt/ldr.h>
38#include <iprt/mem.h>
39#include <iprt/param.h>
40#include <iprt/path.h>
41#include <iprt/string.h>
42#include "internal/dbgmod.h"
43#include "internal/ldr.h"
44#include "internal/magics.h"
45
46
47/*******************************************************************************
48* Structures and Typedefs *
49*******************************************************************************/
50/**
51 * The instance data of the RTLdr based image reader.
52 */
53typedef struct RTDBGMODLDR
54{
55 /** The loader handle. */
56 RTLDRMOD hLdrMod;
57} RTDBGMODLDR;
58/** Pointer to instance data NM map reader. */
59typedef RTDBGMODLDR *PRTDBGMODLDR;
60
61
62
63/** @interface_method_impl{RTDBGMODVTIMG,pfnGetFormat} */
64static DECLCALLBACK(RTLDRFMT) rtDbgModLdr_GetFormat(PRTDBGMODINT pMod)
65{
66 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
67 return RTLdrGetFormat(pThis->hLdrMod);
68}
69
70
71/** @interface_method_impl{RTDBGMODVTIMG,pfnUnmapPart} */
72static DECLCALLBACK(int) rtDbgModLdr_UnmapPart(PRTDBGMODINT pMod, size_t cb, void const **ppvMap)
73{
74 NOREF(pMod); NOREF(cb);
75 RTMemFree((void *)*ppvMap);
76 *ppvMap = NULL;
77 return VINF_SUCCESS;
78}
79
80
81/** @interface_method_impl{RTDBGMODVTIMG,pfnMapPart} */
82static DECLCALLBACK(int) rtDbgModLdr_MapPart(PRTDBGMODINT pMod, uint32_t iDbgInfo, RTFOFF off, size_t cb, void const **ppvMap)
83{
84 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
85
86 void *pvMap = RTMemAlloc(cb);
87 if (!pvMap)
88 return VERR_NO_MEMORY;
89
90 int rc = rtLdrReadAt(pThis->hLdrMod, pvMap, iDbgInfo, off, cb);
91 if (RT_SUCCESS(rc))
92 *ppvMap = pvMap;
93 else
94 {
95 RTMemFree(pvMap);
96 *ppvMap = NULL;
97 }
98 return rc;
99}
100
101
102/** @interface_method_impl{RTDBGMODVTIMG,pfnGetLoadedSize} */
103static DECLCALLBACK(RTUINTPTR) rtDbgModLdr_GetLoadedSize(PRTDBGMODINT pMod)
104{
105 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
106 return RTLdrSize(pThis->hLdrMod);
107}
108
109
110/** @interface_method_impl{RTDBGMODVTIMG,pfnRvaToSegOffset} */
111static DECLCALLBACK(int) rtDbgModLdr_RvaToSegOffset(PRTDBGMODINT pMod, RTLDRADDR uRva,
112 PRTDBGSEGIDX piSeg, PRTLDRADDR poffSeg)
113{
114 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
115 return RTLdrRvaToSegOffset(pThis->hLdrMod, uRva, piSeg, poffSeg);
116}
117
118
119/** @interface_method_impl{RTDBGMODVTIMG,pfnLinkAddressToSegOffset} */
120static DECLCALLBACK(int) rtDbgModLdr_LinkAddressToSegOffset(PRTDBGMODINT pMod, RTLDRADDR LinkAddress,
121 PRTDBGSEGIDX piSeg, PRTLDRADDR poffSeg)
122{
123 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
124 return RTLdrLinkAddressToSegOffset(pThis->hLdrMod, LinkAddress, piSeg, poffSeg);
125}
126
127
128/** @interface_method_impl{RTDBGMODVTIMG,pfnEnumSegments} */
129static DECLCALLBACK(int) rtDbgModLdr_EnumSymbols(PRTDBGMODINT pMod, uint32_t fFlags, RTLDRADDR BaseAddress,
130 PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
131{
132 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
133 return RTLdrEnumSymbols(pThis->hLdrMod, fFlags, NULL /*pvBits*/, BaseAddress, pfnCallback, pvUser);
134}
135
136
137/** @interface_method_impl{RTDBGMODVTIMG,pfnEnumSegments} */
138static DECLCALLBACK(int) rtDbgModLdr_EnumSegments(PRTDBGMODINT pMod, PFNRTLDRENUMSEGS pfnCallback, void *pvUser)
139{
140 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
141 return RTLdrEnumSegments(pThis->hLdrMod, pfnCallback, pvUser);
142}
143
144
145/** @interface_method_impl{RTDBGMODVTIMG,pfnEnumDbgInfo} */
146static DECLCALLBACK(int) rtDbgModLdr_EnumDbgInfo(PRTDBGMODINT pMod, PFNRTLDRENUMDBG pfnCallback, void *pvUser)
147{
148 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
149 return RTLdrEnumDbgInfo(pThis->hLdrMod, NULL, pfnCallback, pvUser);
150}
151
152
153/** @interface_method_impl{RTDBGMODVTIMG,pfnClose} */
154static DECLCALLBACK(int) rtDbgModLdr_Close(PRTDBGMODINT pMod)
155{
156 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
157 AssertPtr(pThis);
158
159 int rc = RTLdrClose(pThis->hLdrMod); AssertRC(rc);
160 pThis->hLdrMod = NIL_RTLDRMOD;
161
162 RTMemFree(pThis);
163
164 return VINF_SUCCESS;
165}
166
167
168/** @interface_method_impl{RTDBGMODVTIMG,pfnTryOpen} */
169static DECLCALLBACK(int) rtDbgModLdr_TryOpen(PRTDBGMODINT pMod, RTLDRARCH enmArch)
170{
171 RTLDRMOD hLdrMod;
172 int rc = RTLdrOpen(pMod->pszImgFile, RTLDR_O_FOR_DEBUG, enmArch, &hLdrMod);
173 if (RT_SUCCESS(rc))
174 {
175 rc = rtDbgModLdrOpenFromHandle(pMod, hLdrMod);
176 if (RT_FAILURE(rc))
177 RTLdrClose(hLdrMod);
178 }
179 return rc;
180}
181
182
183/** Virtual function table for the RTLdr based image reader. */
184DECL_HIDDEN_CONST(RTDBGMODVTIMG) const g_rtDbgModVtImgLdr =
185{
186 /*.u32Magic = */ RTDBGMODVTIMG_MAGIC,
187 /*.fReserved = */ 0,
188 /*.pszName = */ "RTLdr",
189 /*.pfnTryOpen = */ rtDbgModLdr_TryOpen,
190 /*.pfnClose = */ rtDbgModLdr_Close,
191 /*.pfnEnumDbgInfo = */ rtDbgModLdr_EnumDbgInfo,
192 /*.pfnEnumSegments = */ rtDbgModLdr_EnumSegments,
193 /*.pfnEnumSymbols = */ rtDbgModLdr_EnumSymbols,
194 /*.pfnGetLoadedSize = */ rtDbgModLdr_GetLoadedSize,
195 /*.pfnLinkAddressToSegOffset = */ rtDbgModLdr_LinkAddressToSegOffset,
196 /*.pfnRvaToSegOffset= */ rtDbgModLdr_RvaToSegOffset,
197 /*.pfnMapPart = */ rtDbgModLdr_MapPart,
198 /*.pfnUnmapPart = */ rtDbgModLdr_UnmapPart,
199 /*.pfnGetFormat = */ rtDbgModLdr_GetFormat,
200
201 /*.u32EndMagic = */ RTDBGMODVTIMG_MAGIC
202};
203
204
205/**
206 * Open PE-image trick.
207 *
208 * @returns IPRT status code
209 * @param pDbgMod The debug module instance.
210 * @param hLdrMod The module to open a image debug backend for.
211 */
212DECLHIDDEN(int) rtDbgModLdrOpenFromHandle(PRTDBGMODINT pDbgMod, RTLDRMOD hLdrMod)
213{
214 PRTDBGMODLDR pThis = (PRTDBGMODLDR)RTMemAllocZ(sizeof(RTDBGMODLDR));
215 if (!pThis)
216 return VERR_NO_MEMORY;
217
218 pThis->hLdrMod = hLdrMod;
219 pDbgMod->pvImgPriv = pThis;
220 return VINF_SUCCESS;
221}
222
Note: See TracBrowser for help on using the repository browser.

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