VirtualBox

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

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

Implemented debug module that uses the exported symbols. This will work on all hosts. :-)

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