source: trunk/kStuff/kDbg/kDbgModule.cpp@ 3539

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

hacking

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.4 KB
Line 
1/* $Id: kDbgModule.cpp 3539 2007-08-23 03:00:10Z bird $ */
2/** @file
3 * kDbg - The Debug Info Reader, Module API.
4 */
5
6/*
7 * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net>
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 <k/kDefs.h>
32#include <k/kTypes.h>
33#include "kDbg.h"
34#include "kLdr.h"
35#include "kDbgInternal.h"
36
37#include <kLdrModMZ.h>
38#include <kLdrModPE.h>
39
40
41/*******************************************************************************
42* Global Variables *
43*******************************************************************************/
44
45
46
47KDBG_DECL(int) kDbgModuleOpenFilePart(PKDBGHLPFILE pFile, int64_t off, int64_t cb, PKDBGMOD *ppDbgMod)
48{
49#if K_OS == K_OS_WINDOWS
50 /*
51 * If we're on Windows, let DbgHelp have a go first.
52 */
53 if (!off)
54 {
55 int rc = kdbgModWinDbgHelpOpen(pFile, ppDbgMod);
56 if (!rc)
57 return 0;
58 }
59#endif
60
61 /*
62 * Probe the file for signatures we recognize.
63 */
64 int64_t offHdr = 0;
65 union
66 {
67 uint32_t au32[4];
68 uint16_t au16[8];
69 uint8_t ab[16];
70 char
71 } Buf;
72 rc = kDbgHlpReadAt(pFile, &Buf, sizeof(Buf), off);
73
74}
75
76
77KDBG_DECL(int) kDbgModuleOpenFile(PKDBGHLPFILE pFile, PKDBGMOD *ppDbgMod)
78{
79 return kDbgModuleOpenFilePart(pFile, 0, INT64_MAX, ppDbgMod);
80}
81
82
83KDBG_DECL(int) kDbgModuleOpenkLdrMod(PKLDRMOD pLdrMod, PKDBGMOD *ppDbgMod)
84{
85 /*
86 * Enumerate the debug info, if nothing found check for a matching .pdb,
87 * .dbg, .sym or .map file.
88 */
89
90}
91
92
93/**
94 * Opens the debug info for a specified executable module.
95 *
96 * @returns IPRT status code.
97 * @param pszModulePath The path to the executable module.
98 * @param ppDbgMod Where to store the debug module handle.
99 */
100KDBG_DECL(int) kDbgModuleOpen(const char *pszModulePath, PKDBGMOD *ppDbgMod)
101{
102 /*
103 * Validate input.
104 */
105 kDbgAssertPtrReturn(pszModulePath, KDBG_ERR_INVALID_POINTER);
106 kDbgAssertMsgReturn(*pszModulePath, ("%p\n", pszModulePath), KDBG_ERR_INVALID_PARAMETER);
107 kDbgAssertPtrReturn(ppDbgMod, KDBG_ERR_INVALID_POINTER);
108 *ppDbgMod = NULL;
109
110 /*
111 * Open the file and see if we can read it.
112 */
113 PKDBGHLPFILE pFile;
114 int rc = kDbgHlpOpenRO(pszModulePath, &pFile);
115 if (rc)
116 return rc;
117 rc = kDbgModuleOpenFile(pFile, ppDbgMod);
118 if (rc)
119 {
120 kDbgHlpClose(pFile);
121
122 /*
123 * Let kLdr have a shot at it, if it's a binary it may contain
124 * some debug sections or a link to an external debug file.
125 */
126 PKLDRMOD pLdrMod;
127 int rc2 = kLdrModOpen(pszModulePath, *pLdrMod);
128 if (!rc2)
129 {
130 rc = kDbgModuleOpenkLdrMod(pLdrMod, ppDbgMod);
131 if (rc)
132 kLdrModClose(pLdrMod);
133 }
134 }
135
136 return rc;
137}
138
139
140/**
141 * Validates a debug module handle.
142 * All necessary asserting will be taken care of here.
143 *
144 * @returns True / false.
145 * @param pMod The debug module handle.
146 */
147KDBG_INLINE(bool) kdbgModIsValid(PKDBGMOD pMod)
148{
149 kDbgAssertPtrReturn(pMod, false);
150 kDbgAssertMsgReturn(pMod->u32Magic == KDBGMOD_MAGIC, ("%#x", pMod->u32Magic), false);
151 kDbgAssertPtrReturn(pMod->pOps, false);
152 return true;
153}
154
155
156/**
157 * Closes the module.
158 *
159 * @returns IPRT status code.
160 * @param pMod The module handle.
161 */
162KDBG_DECL(int) kDbgModuleClose(PKDBGMOD pMod)
163{
164 if (!kdbgModIsValid(pMod))
165 return KDBG_ERR_INVALID_PARAMETER;
166
167 int rc = pMod->pOps->pfnClose(pMod);
168 if (!rc)
169 kDbgHlpFree(pMod);
170 return rc;
171}
172
173
174/**
175 * Gets a symbol by segment:offset.
176 * This will be approximated to the nearest symbol if there is no exact match.
177 *
178 * @returns IPRT status code.
179 * @param pMod The module.
180 * @param iSegment The segment this offset is relative to.
181 * The -1 segment is special, it means that the addres is relative to
182 * the image base. The image base is where the first bit of the image
183 * is mapped during load.
184 * @param off The offset into the segment.
185 * @param pSym Where to store the symbol details.
186 */
187KDBG_DECL(int) kDbgModuleQuerySymbol(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGSYMBOL pSym)
188{
189 if (!kdbgModIsValid(pMod))
190 return KDBG_ERR_INVALID_PARAMETER;
191 kDbgAssertPtrReturn(pSym, KDBG_ERR_INVALID_POINTER);
192
193 return pMod->pOps->pfnQuerySymbol(pMod, iSegment, off, pSym);
194}
195
196
197/**
198 * Gets & allocates a symbol by segment:offset.
199 * This will be approximated to the nearest symbol if there is no exact match.
200 *
201 * @returns IPRT status code.
202 * @param pMod The module.
203 * @param iSegment The segment this offset is relative to.
204 * The -1 segment is special, it means that the addres is relative to
205 * the image base. The image base is where the first bit of the image
206 * is mapped during load.
207 * @param off The offset into the segment.
208 * @param ppSym Where to store the pointer to the symbol info.
209 * Free the returned symbol using kDbgSymbolFree().
210 */
211KDBG_DECL(int) kDbgModuleQuerySymbolA(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PPKDBGSYMBOL ppSym)
212{
213 kDbgAssertPtrReturn(ppSym, KDBG_ERR_INVALID_POINTER);
214
215 KDBGSYMBOL Sym;
216 int rc = kDbgModuleQuerySymbol(pMod, iSegment, off, &Sym);
217 if (!rc)
218 {
219 *ppSym = kDbgSymbolDup(&Sym);
220 if (!*ppSym)
221 rc = KDBG_ERR_NO_MEMORY;
222 }
223 else
224 *ppSym = NULL;
225 return rc;
226}
227
228
229/**
230 * Gets a line number entry by segment:offset.
231 * This will be approximated to the nearest line number there is no exact match.
232 *
233 * @returns IPRT status code.
234 * @param pMod The module.
235 * @param iSegment The segment this offset is relative to.
236 * The -1 segment is special, it means that the addres is relative to
237 * the image base. The image base is where the first bit of the image
238 * is mapped during load.
239 * @param off The offset into the segment.
240 * @param pLine Where to store the line number details.
241 */
242KDBG_DECL(int) kDbgModuleQueryLine(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGLINE pLine)
243{
244 if (!kdbgModIsValid(pMod))
245 return KDBG_ERR_INVALID_PARAMETER;
246 kDbgAssertPtrReturn(pLine, KDBG_ERR_INVALID_POINTER);
247
248 return pMod->pOps->pfnQueryLine(pMod, iSegment, off, pLine);
249}
250
251
252/**
253 * Gets & allocates a line number entry by segment:offset.
254 * This will be approximated to the nearest line number there is no exact match.
255 *
256 * @returns IPRT status code.
257 * @param pMod The module.
258 * @param iSegment The segment this offset is relative to.
259 * The -1 segment is special, it means that the addres is relative to
260 * the image base. The image base is where the first bit of the image
261 * is mapped during load.
262 * @param off The offset into the segment.
263 * @param ppLine Where to store the pointer to the line number info.
264 * Free the returned line number using kDbgLineFree().
265 */
266KDBG_DECL(int) kDbgModuleQueryLineA(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PPKDBGLINE ppLine)
267{
268 kDbgAssertPtrReturn(ppLine, KDBG_ERR_INVALID_POINTER);
269
270 KDBGLINE Line;
271 int rc = kDbgModuleQueryLine(pMod, iSegment, off, &Line);
272 if (!rc)
273 {
274 *ppLine = kDbgLineDup(&Line);
275 if (!*ppLine)
276 rc = KDBG_ERR_NO_MEMORY;
277 }
278 else
279 *ppLine = NULL;
280 return rc;
281}
282
Note: See TracBrowser for help on using the repository browser.