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

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

reorg.

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