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

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

Moved k* into kStuff.

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