source: trunk/kProfile/dbgmodule.cpp@ 3524

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

kProfile Mark II. Some early/old code.

File size: 8.8 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#include <iprt/file.h>
32#include <iprt/string.h>
33#include <iprt/err.h>
34#include <iprt/assert.h>
35#include <iprt/alloc.h>
36
37#include "DBGInternal.h"
38#include "internal/ldrMZ.h"
39#include "internal/ldrPE.h"
40
41
42
43/**
44 * Opens the debug info for a specified executable module.
45 *
46 * @returns IPRT status code.
47 * @param pszModulePath The path to the executable module.
48 * @param ppDbgMod Where to store the debug module handle.
49 */
50RTDECL(int) RTDbgModuleOpen(const char *pszModulePath, PRTDBGMOD *ppDbgMod)
51{
52 /*
53 * Validate input.
54 */
55 AssertMsgReturn(VALID_PTR(pszModulePath), ("%p\n", pszModulePath), VERR_INVALID_POINTER);
56 AssertMsgReturn(*pszModulePath, ("%p\n", pszModulePath), VERR_INVALID_PARAMETER);
57 AssertMsgReturn(VALID_PTR(ppDbgMod), ("%p\n", ppDbgMod), VERR_INVALID_POINTER);
58 *ppDbgMod = NULL;
59
60 /*
61 * The file and try figure out the format.
62 */
63 RTFILE File;
64 int rc = RTFileOpen(&File, pszModulePath, RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
65 if (RT_FAILURE(rc))
66 return rc;
67
68 RTFOFF offHdr = 0;
69 union
70 {
71 uint32_t au32[4];
72 uint16_t au16[8];
73 uint8_t ab[16];
74 } Buf;
75 rc = RTFileRead(File, &Buf, sizeof(Buf), NULL);
76 if ( RT_SUCCESS(rc)
77 && Buf.au16[0] == IMAGE_DOS_SIGNATURE)
78 {
79 /* new header? */
80 uint32_t offNewHeader;
81 rc = RTFileReadAt(File, OFFSETOF(IMAGE_DOS_HEADER, e_lfanew), &offNewHeader, sizeof(offNewHeader), NULL);
82 if (RT_SUCCESS(rc) && offNewHeader)
83 {
84 offHdr = offNewHeader;
85 rc = RTFileReadAt(File, offNewHeader, &Buf, sizeof(Buf), NULL);
86 }
87 }
88 if (RT_SUCCESS(rc))
89 {
90 if (Buf.au16[0] == IMAGE_DOS_SIGNATURE)
91 rc = VERR_NOT_SUPPORTED;
92#ifdef IMAGE_NE_SIGNATURE
93 else if (Buf.au16[0] == IMAGE_NE_SIGNATURE)
94 rc = VERR_NOT_SUPPORTED;
95#endif
96#ifdef IMAGE_LX_SIGNATURE
97 else if (Buf.au16[0] == IMAGE_LX_SIGNATURE)
98 rc = VERR_NOT_SUPPORTED;
99#endif
100#ifdef IMAGE_LE_SIGNATURE
101 else if (Buf.au16[0] == IMAGE_LE_SIGNATURE)
102 rc = VERR_NOT_SUPPORTED;
103#endif
104#ifdef IMAGE_ELF_SIGNATURE
105 else if (Buf.au32[0] == IMAGE_ELF_SIGNATURE)
106 rc = VERR_NOT_SUPPORTED;
107#endif
108#ifdef IMAGE_NT_SIGNATURE
109 else if (Buf.au32[0] == IMAGE_NT_SIGNATURE)
110 rc = rtDbgModPEOpen(File, offHdr, pszModulePath, ppDbgMod);
111#endif
112 /** @todo there are a number of text file formats too which I want to support. */
113 else
114 rc = VERR_NOT_SUPPORTED;
115
116 }
117
118 if (RT_FAILURE(rc))
119 RTFileClose(File);
120 return rc;
121}
122
123
124/**
125 * Validates a debug module handle.
126 * All necessary asserting will be taken care of here.
127 *
128 * @returns True / false.
129 * @param pMod The debug module handle.
130 */
131DECLINLINE(bool) rtDbgModIsValid(PRTDBGMOD pMod)
132{
133 AssertMsgReturn(VALID_PTR(pMod), ("%p", pMod), false);
134 AssertMsgReturn(pMod->u32Magic == RTDBGMOD_MAGIC, ("%RX32", pMod->u32Magic), false);
135 AssertMsgReturn(VALID_PTR(pMod->pOps), ("%p", pMod->pOps), false);
136 return true;
137}
138
139
140/**
141 * Closes the module.
142 *
143 * @returns IPRT status code.
144 * @param pMod The module handle.
145 */
146RTDECL(int) RTDbgModuleClose(PRTDBGMOD pMod)
147{
148 if (!rtDbgModIsValid(pMod))
149 return VERR_INVALID_PARAMETER;
150
151 int rc = pMod->pOps->pfnClose(pMod);
152 if (RT_SUCCESS(rc))
153 RTMemFree(pMod);
154 return rc;
155}
156
157
158/**
159 * Gets a symbol by segment:offset.
160 * This will be approximated to the nearest symbol if there is no exact match.
161 *
162 * @returns IPRT status code.
163 * @param pMod The module.
164 * @param iSegment The segment this offset is relative to.
165 * The -1 segment is special, it means that the addres is relative to
166 * the image base. The image base is where the first bit of the image
167 * is mapped during load.
168 * @param off The offset into the segment.
169 * @param pSym Where to store the symbol details.
170 */
171RTDECL(int) RTDbgModuleQuerySymbol(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PRTDBGSYMBOL pSym)
172{
173 if (!rtDbgModIsValid(pMod))
174 return VERR_INVALID_PARAMETER;
175 AssertMsgReturn(VALID_PTR(pSym), ("%p", pSym), VERR_INVALID_POINTER);
176
177 return pMod->pOps->pfnQuerySymbol(pMod, iSegment, off, pSym);
178}
179
180
181/**
182 * Gets & allocates a symbol by segment:offset.
183 * This will be approximated to the nearest symbol if there is no exact match.
184 *
185 * @returns IPRT status code.
186 * @param pMod The module.
187 * @param iSegment The segment this offset is relative to.
188 * The -1 segment is special, it means that the addres is relative to
189 * the image base. The image base is where the first bit of the image
190 * is mapped during load.
191 * @param off The offset into the segment.
192 * @param ppSym Where to store the pointer to the symbol info.
193 * Free the returned symbol using RTDbgSymbolFree().
194 */
195RTDECL(int) RTDbgModuleQuerySymbolA(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PPRTDBGSYMBOL ppSym)
196{
197 AssertMsgReturn(VALID_PTR(ppSym), ("%p\n", ppSym), VERR_INVALID_POINTER);
198
199 RTDBGSYMBOL Sym;
200 int rc = RTDbgModuleQuerySymbol(pMod, iSegment, off, &Sym);
201 if (RT_SUCCESS(rc))
202 {
203 *ppSym = RTDbgSymbolDup(&Sym);
204 if (!*ppSym)
205 rc = VERR_NO_MEMORY;
206 }
207 else
208 *ppSym = NULL;
209 return rc;
210}
211
212
213/**
214 * Gets a line number entry by segment:offset.
215 * This will be approximated to the nearest line number there is no exact match.
216 *
217 * @returns IPRT status code.
218 * @param pMod The module.
219 * @param iSegment The segment this offset is relative to.
220 * The -1 segment is special, it means that the addres is relative to
221 * the image base. The image base is where the first bit of the image
222 * is mapped during load.
223 * @param off The offset into the segment.
224 * @param pLine Where to store the line number details.
225 */
226RTDECL(int) RTDbgModuleQueryLine(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PRTDBGLINE pLine)
227{
228 if (!rtDbgModIsValid(pMod))
229 return VERR_INVALID_PARAMETER;
230 AssertMsgReturn(VALID_PTR(pLine), ("%p", pLine), VERR_INVALID_POINTER);
231
232 return pMod->pOps->pfnQueryLine(pMod, iSegment, off, pLine);
233}
234
235
236/**
237 * Gets & allocates a line number entry by segment:offset.
238 * This will be approximated to the nearest line number there is no exact match.
239 *
240 * @returns IPRT status code.
241 * @param pMod The module.
242 * @param iSegment The segment this offset is relative to.
243 * The -1 segment is special, it means that the addres is relative to
244 * the image base. The image base is where the first bit of the image
245 * is mapped during load.
246 * @param off The offset into the segment.
247 * @param ppLine Where to store the pointer to the line number info.
248 * Free the returned line number using RTDbgLineFree().
249 */
250RTDECL(int) RTDbgModuleQueryLineA(PRTDBGMOD pMod, int32_t iSegment, RTUINTPTR off, PPRTDBGLINE ppLine)
251{
252 AssertMsgReturn(VALID_PTR(ppLine), ("%p\n", ppLine), VERR_INVALID_POINTER);
253
254 RTDBGLINE Line;
255 int rc = RTDbgModuleQueryLine(pMod, iSegment, off, &Line);
256 if (RT_SUCCESS(rc))
257 {
258 *ppLine = RTDbgLineDup(&Line);
259 if (!*ppLine)
260 rc = VERR_NO_MEMORY;
261 }
262 else
263 *ppLine = NULL;
264 return rc;
265}
266
Note: See TracBrowser for help on using the repository browser.