source: trunk/kProfile/dbgmodule.cpp@ 3526

Last change on this file since 3526 was 3526, checked in by bird, 18 years ago

made it build again.

File size: 8.9 KB
Line 
1/* $Id: $ */
2/** @file
3 *
4 * kProfile Mark 2 - Debug Info Reader
5 *
6 * Copyright (c) 2006 knut st. osmundsen <bird-src-spam@anduin.net.de>
7 *
8 *
9 * This file is part of kLIBC.
10 *
11 * kLIBC is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * kLIBC is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kLIBC; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#ifdef KDBG_USE_IPRT
32# include <iprt/file.h>
33# include <iprt/string.h>
34# include <iprt/err.h>
35# include <iprt/assert.h>
36# include <iprt/alloc.h>
37#else
38#endif
39
40#include "kDbgBase.h"
41#include "DBGInternal.h"
42#include <kLdrModMZ.h>
43#include <kLdrModPE.h>
44
45
46
47/**
48 * Opens the debug info for a specified executable module.
49 *
50 * @returns IPRT status code.
51 * @param pszModulePath The path to the executable module.
52 * @param ppDbgMod Where to store the debug module handle.
53 */
54RTDECL(int) RTDbgModuleOpen(const char *pszModulePath, PRTDBGMOD *ppDbgMod)
55{
56 /*
57 * Validate input.
58 */
59 AssertMsgReturn(VALID_PTR(pszModulePath), ("%p\n", pszModulePath), VERR_INVALID_POINTER);
60 AssertMsgReturn(*pszModulePath, ("%p\n", pszModulePath), VERR_INVALID_PARAMETER);
61 AssertMsgReturn(VALID_PTR(ppDbgMod), ("%p\n", ppDbgMod), VERR_INVALID_POINTER);
62 *ppDbgMod = NULL;
63
64 /*
65 * The file and try figure out the format.
66 */
67 RTFILE File;
68 int rc = RTFileOpen(&File, pszModulePath, RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
69 if (RT_FAILURE(rc))
70 return rc;
71
72 RTFOFF offHdr = 0;
73 union
74 {
75 uint32_t au32[4];
76 uint16_t au16[8];
77 uint8_t ab[16];
78 } Buf;
79 rc = RTFileRead(File, &Buf, sizeof(Buf), NULL);
80 if ( RT_SUCCESS(rc)
81 && Buf.au16[0] == IMAGE_DOS_SIGNATURE)
82 {
83 /* new header? */
84 uint32_t offNewHeader;
85 rc = RTFileReadAt(File, KDBG_OFFSETOF(IMAGE_DOS_HEADER, e_lfanew), &offNewHeader, sizeof(offNewHeader), NULL);
86 if (RT_SUCCESS(rc) && offNewHeader)
87 {
88 offHdr = offNewHeader;
89 rc = RTFileReadAt(File, offNewHeader, &Buf, sizeof(Buf), NULL);
90 }
91 }
92 if (RT_SUCCESS(rc))
93 {
94 if (Buf.au16[0] == IMAGE_DOS_SIGNATURE)
95 rc = VERR_NOT_SUPPORTED;
96#ifdef IMAGE_NE_SIGNATURE
97 else if (Buf.au16[0] == IMAGE_NE_SIGNATURE)
98 rc = VERR_NOT_SUPPORTED;
99#endif
100#ifdef IMAGE_LX_SIGNATURE
101 else if (Buf.au16[0] == IMAGE_LX_SIGNATURE)
102 rc = VERR_NOT_SUPPORTED;
103#endif
104#ifdef IMAGE_LE_SIGNATURE
105 else if (Buf.au16[0] == IMAGE_LE_SIGNATURE)
106 rc = VERR_NOT_SUPPORTED;
107#endif
108#ifdef IMAGE_ELF_SIGNATURE
109 else if (Buf.au32[0] == IMAGE_ELF_SIGNATURE)
110 rc = VERR_NOT_SUPPORTED;
111#endif
112#ifdef IMAGE_NT_SIGNATURE
113 else if (Buf.au32[0] == IMAGE_NT_SIGNATURE)
114 rc = rtDbgModPEOpen(File, offHdr, pszModulePath, ppDbgMod);
115#endif
116 /** @todo there are a number of text file formats too which I want to support. */
117 else
118 rc = VERR_NOT_SUPPORTED;
119
120 }
121
122 if (RT_FAILURE(rc))
123 RTFileClose(File);
124 return rc;
125}
126
127
128/**
129 * Validates a debug module handle.
130 * All necessary asserting will be taken care of here.
131 *
132 * @returns True / false.
133 * @param pMod The debug module handle.
134 */
135DECLINLINE(bool) rtDbgModIsValid(PRTDBGMOD pMod)
136{
137 AssertMsgReturn(VALID_PTR(pMod), ("%p", pMod), false);
138 AssertMsgReturn(pMod->u32Magic == RTDBGMOD_MAGIC, ("%RX32", pMod->u32Magic), false);
139 AssertMsgReturn(VALID_PTR(pMod->pOps), ("%p", pMod->pOps), false);
140 return true;
141}
142
143
144/**
145 * Closes the module.
146 *
147 * @returns IPRT status code.
148 * @param pMod The module handle.
149 */
150RTDECL(int) RTDbgModuleClose(PRTDBGMOD pMod)
151{
152 if (!rtDbgModIsValid(pMod))
153 return VERR_INVALID_PARAMETER;
154
155 int rc = pMod->pOps->pfnClose(pMod);
156 if (RT_SUCCESS(rc))
157 RTMemFree(pMod);
158 return rc;
159}
160
161
162/**
163 * Gets a symbol by segment:offset.
164 * This will be approximated to the nearest symbol if there is no exact match.
165 *
166 * @returns IPRT status code.
167 * @param pMod The module.
168 * @param iSegment The segment this offset is relative to.
169 * The -1 segment is special, it means that the addres is relative to
170 * the image base. The image base is where the first bit of the image
171 * is mapped during load.
172 * @param off The offset into the segment.
173 * @param pSym Where to store the symbol details.
174 */
175RTDECL(int) RTDbgModuleQuerySymbol(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PRTDBGSYMBOL pSym)
176{
177 if (!rtDbgModIsValid(pMod))
178 return VERR_INVALID_PARAMETER;
179 AssertMsgReturn(VALID_PTR(pSym), ("%p", pSym), VERR_INVALID_POINTER);
180
181 return pMod->pOps->pfnQuerySymbol(pMod, iSegment, off, pSym);
182}
183
184
185/**
186 * Gets & allocates a symbol by segment:offset.
187 * This will be approximated to the nearest symbol if there is no exact match.
188 *
189 * @returns IPRT status code.
190 * @param pMod The module.
191 * @param iSegment The segment this offset is relative to.
192 * The -1 segment is special, it means that the addres is relative to
193 * the image base. The image base is where the first bit of the image
194 * is mapped during load.
195 * @param off The offset into the segment.
196 * @param ppSym Where to store the pointer to the symbol info.
197 * Free the returned symbol using RTDbgSymbolFree().
198 */
199RTDECL(int) RTDbgModuleQuerySymbolA(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PPRTDBGSYMBOL ppSym)
200{
201 AssertMsgReturn(VALID_PTR(ppSym), ("%p\n", ppSym), VERR_INVALID_POINTER);
202
203 RTDBGSYMBOL Sym;
204 int rc = RTDbgModuleQuerySymbol(pMod, iSegment, off, &Sym);
205 if (RT_SUCCESS(rc))
206 {
207 *ppSym = RTDbgSymbolDup(&Sym);
208 if (!*ppSym)
209 rc = VERR_NO_MEMORY;
210 }
211 else
212 *ppSym = NULL;
213 return rc;
214}
215
216
217/**
218 * Gets a line number entry by segment:offset.
219 * This will be approximated to the nearest line number there is no exact match.
220 *
221 * @returns IPRT status code.
222 * @param pMod The module.
223 * @param iSegment The segment this offset is relative to.
224 * The -1 segment is special, it means that the addres is relative to
225 * the image base. The image base is where the first bit of the image
226 * is mapped during load.
227 * @param off The offset into the segment.
228 * @param pLine Where to store the line number details.
229 */
230RTDECL(int) RTDbgModuleQueryLine(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PRTDBGLINE pLine)
231{
232 if (!rtDbgModIsValid(pMod))
233 return VERR_INVALID_PARAMETER;
234 AssertMsgReturn(VALID_PTR(pLine), ("%p", pLine), VERR_INVALID_POINTER);
235
236 return pMod->pOps->pfnQueryLine(pMod, iSegment, off, pLine);
237}
238
239
240/**
241 * Gets & allocates a line number entry by segment:offset.
242 * This will be approximated to the nearest line number there is no exact match.
243 *
244 * @returns IPRT status code.
245 * @param pMod The module.
246 * @param iSegment The segment this offset is relative to.
247 * The -1 segment is special, it means that the addres is relative to
248 * the image base. The image base is where the first bit of the image
249 * is mapped during load.
250 * @param off The offset into the segment.
251 * @param ppLine Where to store the pointer to the line number info.
252 * Free the returned line number using RTDbgLineFree().
253 */
254RTDECL(int) RTDbgModuleQueryLineA(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PPRTDBGLINE ppLine)
255{
256 AssertMsgReturn(VALID_PTR(ppLine), ("%p\n", ppLine), VERR_INVALID_POINTER);
257
258 RTDBGLINE Line;
259 int rc = RTDbgModuleQueryLine(pMod, iSegment, off, &Line);
260 if (RT_SUCCESS(rc))
261 {
262 *ppLine = RTDbgLineDup(&Line);
263 if (!*ppLine)
264 rc = VERR_NO_MEMORY;
265 }
266 else
267 *ppLine = NULL;
268 return rc;
269}
270
Note: See TracBrowser for help on using the repository browser.