1 | /* $Id: dbgmodldr.cpp 38547 2011-08-26 12:58:47Z 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 | RTMemFree((void *)*ppvMap);
|
---|
67 | *ppvMap = NULL;
|
---|
68 | return VINF_SUCCESS;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | /** @interface_method_impl{RTDBGMODVTIMG,pfnMapPart} */
|
---|
73 | static 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 = RTFileReadAt(pThis->hFile, off, pvMap, cb, NULL);
|
---|
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} */
|
---|
94 | static 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,pfnEnumSegments} */
|
---|
102 | static DECLCALLBACK(int) rtDbgModLdr_EnumSegments(PRTDBGMODINT pMod, PFNRTLDRENUMSEGS pfnCallback, void *pvUser)
|
---|
103 | {
|
---|
104 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
105 | return RTLdrEnumSegments(pThis->hLdrMod, pfnCallback, pvUser);
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | /** @interface_method_impl{RTDBGMODVTIMG,pfnEnumDbgInfo} */
|
---|
110 | static DECLCALLBACK(int) rtDbgModLdr_EnumDbgInfo(PRTDBGMODINT pMod, PFNRTLDRENUMDBG pfnCallback, void *pvUser)
|
---|
111 | {
|
---|
112 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
113 | return RTLdrEnumDbgInfo(pThis->hLdrMod, NULL, pfnCallback, pvUser);
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /** @interface_method_impl{RTDBGMODVTIMG,pfnClose} */
|
---|
118 | static DECLCALLBACK(int) rtDbgModLdr_Close(PRTDBGMODINT pMod)
|
---|
119 | {
|
---|
120 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
|
---|
121 | AssertPtr(pThis);
|
---|
122 |
|
---|
123 | int rc = RTLdrClose(pThis->hLdrMod); AssertRC(rc);
|
---|
124 | pThis->hLdrMod = NIL_RTLDRMOD;
|
---|
125 |
|
---|
126 | rc = RTFileClose(pThis->hFile); AssertRC(rc);
|
---|
127 | pThis->hFile = NIL_RTFILE;
|
---|
128 |
|
---|
129 | RTMemFree(pThis);
|
---|
130 |
|
---|
131 | return VINF_SUCCESS;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | /** @interface_method_impl{RTDBGMODVTIMG,pfnTryOpen} */
|
---|
136 | static DECLCALLBACK(int) rtDbgModLdr_TryOpen(PRTDBGMODINT pMod)
|
---|
137 | {
|
---|
138 | RTFILE hFile;
|
---|
139 | int rc = RTFileOpen(&hFile, pMod->pszImgFile, RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
|
---|
140 | if (RT_SUCCESS(rc))
|
---|
141 | {
|
---|
142 | RTLDRMOD hLdrMod;
|
---|
143 | rc = RTLdrOpen(pMod->pszImgFile, 0 /*fFlags*/, RTLDRARCH_WHATEVER, &hLdrMod);
|
---|
144 | if (RT_SUCCESS(rc))
|
---|
145 | {
|
---|
146 | PRTDBGMODLDR pThis = (PRTDBGMODLDR)RTMemAllocZ(sizeof(RTDBGMODLDR));
|
---|
147 | if (pThis)
|
---|
148 | {
|
---|
149 | pThis->hLdrMod = hLdrMod;
|
---|
150 | pThis->hFile = hFile;
|
---|
151 | pMod->pvImgPriv = pThis;
|
---|
152 | return VINF_SUCCESS;
|
---|
153 | }
|
---|
154 |
|
---|
155 | rc = VERR_NO_MEMORY;
|
---|
156 | RTLdrClose(hLdrMod);
|
---|
157 | }
|
---|
158 |
|
---|
159 | RTFileClose(hFile);
|
---|
160 | }
|
---|
161 | return rc;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /** Virtual function table for the RTLdr based image reader. */
|
---|
166 | DECL_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 | /*.pfnGetLoadedSize = */ rtDbgModLdr_GetLoadedSize,
|
---|
176 | /*.pfnMapPart = */ rtDbgModLdr_MapPart,
|
---|
177 | /*.pfnUnmapPart = */ rtDbgModLdr_UnmapPart,
|
---|
178 |
|
---|
179 | /*.u32EndMagic = */ RTDBGMODVTIMG_MAGIC
|
---|
180 | };
|
---|
181 |
|
---|
182 |
|
---|