VirtualBox

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

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

More exteran .dSYM and .dwo bundles/files changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Id: dbgmodldr.cpp 46164 2013-05-19 16:58:01Z 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,pfnGetArch} */
64static DECLCALLBACK(RTLDRARCH) rtDbgModLdr_GetArch(PRTDBGMODINT pMod)
65{
66 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
67 return RTLdrGetArch(pThis->hLdrMod);
68}
69
70
71/** @interface_method_impl{RTDBGMODVTIMG,pfnGetFormat} */
72static DECLCALLBACK(RTLDRFMT) rtDbgModLdr_GetFormat(PRTDBGMODINT pMod)
73{
74 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
75 return RTLdrGetFormat(pThis->hLdrMod);
76}
77
78
79/** @interface_method_impl{RTDBGMODVTIMG,pfnUnmapPart} */
80static DECLCALLBACK(int) rtDbgModLdr_UnmapPart(PRTDBGMODINT pMod, size_t cb, void const **ppvMap)
81{
82 NOREF(pMod); NOREF(cb);
83 RTMemFree((void *)*ppvMap);
84 *ppvMap = NULL;
85 return VINF_SUCCESS;
86}
87
88
89/** @interface_method_impl{RTDBGMODVTIMG,pfnMapPart} */
90static DECLCALLBACK(int) rtDbgModLdr_MapPart(PRTDBGMODINT pMod, uint32_t iDbgInfo, RTFOFF off, size_t cb, void const **ppvMap)
91{
92 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
93
94 void *pvMap = RTMemAlloc(cb);
95 if (!pvMap)
96 return VERR_NO_MEMORY;
97
98 int rc = rtLdrReadAt(pThis->hLdrMod, pvMap, iDbgInfo, off, cb);
99 if (RT_SUCCESS(rc))
100 *ppvMap = pvMap;
101 else
102 {
103 RTMemFree(pvMap);
104 *ppvMap = NULL;
105 }
106 return rc;
107}
108
109
110/** @interface_method_impl{RTDBGMODVTIMG,pfnGetLoadedSize} */
111static DECLCALLBACK(RTUINTPTR) rtDbgModLdr_GetLoadedSize(PRTDBGMODINT pMod)
112{
113 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
114 return RTLdrSize(pThis->hLdrMod);
115}
116
117
118/** @interface_method_impl{RTDBGMODVTIMG,pfnRvaToSegOffset} */
119static DECLCALLBACK(int) rtDbgModLdr_RvaToSegOffset(PRTDBGMODINT pMod, RTLDRADDR uRva,
120 PRTDBGSEGIDX piSeg, PRTLDRADDR poffSeg)
121{
122 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
123 return RTLdrRvaToSegOffset(pThis->hLdrMod, uRva, piSeg, poffSeg);
124}
125
126
127/** @interface_method_impl{RTDBGMODVTIMG,pfnLinkAddressToSegOffset} */
128static DECLCALLBACK(int) rtDbgModLdr_LinkAddressToSegOffset(PRTDBGMODINT pMod, RTLDRADDR LinkAddress,
129 PRTDBGSEGIDX piSeg, PRTLDRADDR poffSeg)
130{
131 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
132 return RTLdrLinkAddressToSegOffset(pThis->hLdrMod, LinkAddress, piSeg, poffSeg);
133}
134
135
136/** @interface_method_impl{RTDBGMODVTIMG,pfnEnumSegments} */
137static DECLCALLBACK(int) rtDbgModLdr_EnumSymbols(PRTDBGMODINT pMod, uint32_t fFlags, RTLDRADDR BaseAddress,
138 PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
139{
140 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
141 return RTLdrEnumSymbols(pThis->hLdrMod, fFlags, NULL /*pvBits*/, BaseAddress, pfnCallback, pvUser);
142}
143
144
145/** @interface_method_impl{RTDBGMODVTIMG,pfnEnumSegments} */
146static DECLCALLBACK(int) rtDbgModLdr_EnumSegments(PRTDBGMODINT pMod, PFNRTLDRENUMSEGS pfnCallback, void *pvUser)
147{
148 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
149 return RTLdrEnumSegments(pThis->hLdrMod, pfnCallback, pvUser);
150}
151
152
153/** @interface_method_impl{RTDBGMODVTIMG,pfnEnumDbgInfo} */
154static DECLCALLBACK(int) rtDbgModLdr_EnumDbgInfo(PRTDBGMODINT pMod, PFNRTLDRENUMDBG pfnCallback, void *pvUser)
155{
156 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
157 return RTLdrEnumDbgInfo(pThis->hLdrMod, NULL, pfnCallback, pvUser);
158}
159
160
161/** @interface_method_impl{RTDBGMODVTIMG,pfnClose} */
162static DECLCALLBACK(int) rtDbgModLdr_Close(PRTDBGMODINT pMod)
163{
164 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
165 AssertPtr(pThis);
166
167 int rc = RTLdrClose(pThis->hLdrMod); AssertRC(rc);
168 pThis->hLdrMod = NIL_RTLDRMOD;
169
170 RTMemFree(pThis);
171
172 return VINF_SUCCESS;
173}
174
175
176/** @interface_method_impl{RTDBGMODVTIMG,pfnTryOpen} */
177static DECLCALLBACK(int) rtDbgModLdr_TryOpen(PRTDBGMODINT pMod, RTLDRARCH enmArch)
178{
179 RTLDRMOD hLdrMod;
180 int rc = RTLdrOpen(pMod->pszImgFile, RTLDR_O_FOR_DEBUG, enmArch, &hLdrMod);
181 if (RT_SUCCESS(rc))
182 {
183 rc = rtDbgModLdrOpenFromHandle(pMod, hLdrMod);
184 if (RT_FAILURE(rc))
185 RTLdrClose(hLdrMod);
186 }
187 return rc;
188}
189
190
191/** Virtual function table for the RTLdr based image reader. */
192DECL_HIDDEN_CONST(RTDBGMODVTIMG) const g_rtDbgModVtImgLdr =
193{
194 /*.u32Magic = */ RTDBGMODVTIMG_MAGIC,
195 /*.fReserved = */ 0,
196 /*.pszName = */ "RTLdr",
197 /*.pfnTryOpen = */ rtDbgModLdr_TryOpen,
198 /*.pfnClose = */ rtDbgModLdr_Close,
199 /*.pfnEnumDbgInfo = */ rtDbgModLdr_EnumDbgInfo,
200 /*.pfnEnumSegments = */ rtDbgModLdr_EnumSegments,
201 /*.pfnEnumSymbols = */ rtDbgModLdr_EnumSymbols,
202 /*.pfnGetLoadedSize = */ rtDbgModLdr_GetLoadedSize,
203 /*.pfnLinkAddressToSegOffset = */ rtDbgModLdr_LinkAddressToSegOffset,
204 /*.pfnRvaToSegOffset= */ rtDbgModLdr_RvaToSegOffset,
205 /*.pfnMapPart = */ rtDbgModLdr_MapPart,
206 /*.pfnUnmapPart = */ rtDbgModLdr_UnmapPart,
207 /*.pfnGetFormat = */ rtDbgModLdr_GetFormat,
208 /*.pfnGetArch = */ rtDbgModLdr_GetArch,
209
210 /*.u32EndMagic = */ RTDBGMODVTIMG_MAGIC
211};
212
213
214/**
215 * Open PE-image trick.
216 *
217 * @returns IPRT status code
218 * @param pDbgMod The debug module instance.
219 * @param hLdrMod The module to open a image debug backend for.
220 */
221DECLHIDDEN(int) rtDbgModLdrOpenFromHandle(PRTDBGMODINT pDbgMod, RTLDRMOD hLdrMod)
222{
223 PRTDBGMODLDR pThis = (PRTDBGMODLDR)RTMemAllocZ(sizeof(RTDBGMODLDR));
224 if (!pThis)
225 return VERR_NO_MEMORY;
226
227 pThis->hLdrMod = hLdrMod;
228 pDbgMod->pvImgPriv = pThis;
229 return VINF_SUCCESS;
230}
231
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