1 | /* $Id: dbgmodldr.cpp 39327 2011-11-16 10:52:07Z 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/magics.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Structures and Typedefs *
|
---|
48 | *******************************************************************************/
|
---|
49 | /**
|
---|
50 | * The instance data of the RTLdr based image reader.
|
---|
51 | */
|
---|
52 | typedef struct RTDBGMODLDR
|
---|
53 | {
|
---|
54 | /** The loader handle. */
|
---|
55 | RTLDRMOD hLdrMod;
|
---|
56 | /** File handle for the image. */
|
---|
57 | RTFILE hFile;
|
---|
58 | } RTDBGMODLDR;
|
---|
59 | /** Pointer to instance data NM map reader. */
|
---|
60 | typedef RTDBGMODLDR *PRTDBGMODLDR;
|
---|
61 |
|
---|
62 |
|
---|
63 | /** @interface_method_impl{RTDBGMODVTIMG,pfnUnmapPart} */
|
---|
64 | static DECLCALLBACK(int) rtDbgModLdr_UnmapPart(PRTDBGMODINT pMod, size_t cb, void const **ppvMap)
|
---|
65 | {
|
---|
66 | NOREF(pMod); NOREF(cb);
|
---|
67 | RTMemFree((void *)*ppvMap);
|
---|
68 | *ppvMap = NULL;
|
---|
69 | return VINF_SUCCESS;
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | /** @interface_method_impl{RTDBGMODVTIMG,pfnMapPart} */
|
---|
74 | static DECLCALLBACK(int) rtDbgModLdr_MapPart(PRTDBGMODINT pMod, RTFOFF off, size_t cb, void const **ppvMap)
|
---|
75 | {
|
---|
76 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
77 |
|
---|
78 | void *pvMap = RTMemAlloc(cb);
|
---|
79 | if (!pvMap)
|
---|
80 | return VERR_NO_MEMORY;
|
---|
81 |
|
---|
82 | int rc = RTFileReadAt(pThis->hFile, off, pvMap, cb, NULL);
|
---|
83 | if (RT_SUCCESS(rc))
|
---|
84 | *ppvMap = pvMap;
|
---|
85 | else
|
---|
86 | {
|
---|
87 | RTMemFree(pvMap);
|
---|
88 | *ppvMap = NULL;
|
---|
89 | }
|
---|
90 | return rc;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /** @interface_method_impl{RTDBGMODVTIMG,pfnGetLoadedSize} */
|
---|
95 | static DECLCALLBACK(RTUINTPTR) rtDbgModLdr_GetLoadedSize(PRTDBGMODINT pMod)
|
---|
96 | {
|
---|
97 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
98 | return RTLdrSize(pThis->hLdrMod);
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | /** @interface_method_impl{RTDBGMODVTIMG,pfnLinkAddressToSegOffset} */
|
---|
103 | static DECLCALLBACK(int) rtDbgModLdr_LinkAddressToSegOffset(PRTDBGMODINT pMod, RTLDRADDR LinkAddress,
|
---|
104 | PRTDBGSEGIDX piSeg, PRTLDRADDR poffSeg)
|
---|
105 | {
|
---|
106 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
107 | return RTLdrLinkAddressToSegOffset(pThis->hLdrMod, LinkAddress, piSeg, poffSeg);
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | /** @interface_method_impl{RTDBGMODVTIMG,pfnEnumSegments} */
|
---|
112 | static DECLCALLBACK(int) rtDbgModLdr_EnumSegments(PRTDBGMODINT pMod, PFNRTLDRENUMSEGS pfnCallback, void *pvUser)
|
---|
113 | {
|
---|
114 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
115 | return RTLdrEnumSegments(pThis->hLdrMod, pfnCallback, pvUser);
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | /** @interface_method_impl{RTDBGMODVTIMG,pfnEnumDbgInfo} */
|
---|
120 | static DECLCALLBACK(int) rtDbgModLdr_EnumDbgInfo(PRTDBGMODINT pMod, PFNRTLDRENUMDBG pfnCallback, void *pvUser)
|
---|
121 | {
|
---|
122 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
123 | return RTLdrEnumDbgInfo(pThis->hLdrMod, NULL, pfnCallback, pvUser);
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | /** @interface_method_impl{RTDBGMODVTIMG,pfnClose} */
|
---|
128 | static DECLCALLBACK(int) rtDbgModLdr_Close(PRTDBGMODINT pMod)
|
---|
129 | {
|
---|
130 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
131 | AssertPtr(pThis);
|
---|
132 |
|
---|
133 | int rc = RTLdrClose(pThis->hLdrMod); AssertRC(rc);
|
---|
134 | pThis->hLdrMod = NIL_RTLDRMOD;
|
---|
135 |
|
---|
136 | rc = RTFileClose(pThis->hFile); AssertRC(rc);
|
---|
137 | pThis->hFile = NIL_RTFILE;
|
---|
138 |
|
---|
139 | RTMemFree(pThis);
|
---|
140 |
|
---|
141 | return VINF_SUCCESS;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /** @interface_method_impl{RTDBGMODVTIMG,pfnTryOpen} */
|
---|
146 | static DECLCALLBACK(int) rtDbgModLdr_TryOpen(PRTDBGMODINT pMod)
|
---|
147 | {
|
---|
148 | RTFILE hFile;
|
---|
149 | int rc = RTFileOpen(&hFile, pMod->pszImgFile, RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
|
---|
150 | if (RT_SUCCESS(rc))
|
---|
151 | {
|
---|
152 | RTLDRMOD hLdrMod;
|
---|
153 | rc = RTLdrOpen(pMod->pszImgFile, 0 /*fFlags*/, RTLDRARCH_WHATEVER, &hLdrMod);
|
---|
154 | if (RT_SUCCESS(rc))
|
---|
155 | {
|
---|
156 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)RTMemAllocZ(sizeof(RTDBGMODLDR));
|
---|
157 | if (pThis)
|
---|
158 | {
|
---|
159 | pThis->hLdrMod = hLdrMod;
|
---|
160 | pThis->hFile = hFile;
|
---|
161 | pMod->pvImgPriv = pThis;
|
---|
162 | return VINF_SUCCESS;
|
---|
163 | }
|
---|
164 |
|
---|
165 | rc = VERR_NO_MEMORY;
|
---|
166 | RTLdrClose(hLdrMod);
|
---|
167 | }
|
---|
168 |
|
---|
169 | RTFileClose(hFile);
|
---|
170 | }
|
---|
171 | return rc;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /** Virtual function table for the RTLdr based image reader. */
|
---|
176 | DECL_HIDDEN_CONST(RTDBGMODVTIMG) const g_rtDbgModVtImgLdr =
|
---|
177 | {
|
---|
178 | /*.u32Magic = */ RTDBGMODVTIMG_MAGIC,
|
---|
179 | /*.fReserved = */ 0,
|
---|
180 | /*.pszName = */ "RTLdr",
|
---|
181 | /*.pfnTryOpen = */ rtDbgModLdr_TryOpen,
|
---|
182 | /*.pfnClose = */ rtDbgModLdr_Close,
|
---|
183 | /*.pfnEnumDbgInfo = */ rtDbgModLdr_EnumDbgInfo,
|
---|
184 | /*.pfnEnumSegments = */ rtDbgModLdr_EnumSegments,
|
---|
185 | /*.pfnGetLoadedSize = */ rtDbgModLdr_GetLoadedSize,
|
---|
186 | /*.pfnLinkAddressToSegOffset = */ rtDbgModLdr_LinkAddressToSegOffset,
|
---|
187 | /*.pfnMapPart = */ rtDbgModLdr_MapPart,
|
---|
188 | /*.pfnUnmapPart = */ rtDbgModLdr_UnmapPart,
|
---|
189 |
|
---|
190 | /*.u32EndMagic = */ RTDBGMODVTIMG_MAGIC
|
---|
191 | };
|
---|
192 |
|
---|