1 | /* $Id: dbgmodnm.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Debug Map Reader For NM Like Mapfiles.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/dbg.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/err.h>
|
---|
35 | #include <iprt/ctype.h>
|
---|
36 | #include <iprt/mem.h>
|
---|
37 | #include <iprt/stream.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include "internal/dbgmod.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /*********************************************************************************************************************************
|
---|
43 | * Structures and Typedefs *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 | /**
|
---|
46 | * Instance data.
|
---|
47 | */
|
---|
48 | typedef struct RTDBGMODNM
|
---|
49 | {
|
---|
50 | /** The debug container containing doing the real work. */
|
---|
51 | RTDBGMOD hCnt;
|
---|
52 | } RTDBGMODNM;
|
---|
53 | /** Pointer to instance data NM map reader. */
|
---|
54 | typedef RTDBGMODNM *PRTDBGMODNM;
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 | /** @interface_method_impl{RTDBGMODVTDBG,pfnLineByAddr} */
|
---|
59 | static DECLCALLBACK(int) rtDbgModNm_LineByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
|
---|
60 | PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
|
---|
61 | {
|
---|
62 | PRTDBGMODNM pThis = (PRTDBGMODNM)pMod->pvDbgPriv;
|
---|
63 | return RTDbgModLineByAddr(pThis->hCnt, iSeg, off, poffDisp, pLineInfo);
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | /** @interface_method_impl{RTDBGMODVTDBG,pfnLineByOrdinal} */
|
---|
68 | static DECLCALLBACK(int) rtDbgModNm_LineByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
|
---|
69 | {
|
---|
70 | PRTDBGMODNM pThis = (PRTDBGMODNM)pMod->pvDbgPriv;
|
---|
71 | return RTDbgModLineByOrdinal(pThis->hCnt, iOrdinal, pLineInfo);
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | /** @interface_method_impl{RTDBGMODVTDBG,pfnLineCount} */
|
---|
76 | static DECLCALLBACK(uint32_t) rtDbgModNm_LineCount(PRTDBGMODINT pMod)
|
---|
77 | {
|
---|
78 | PRTDBGMODNM pThis = (PRTDBGMODNM)pMod->pvDbgPriv;
|
---|
79 | return RTDbgModLineCount(pThis->hCnt);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | /** @interface_method_impl{RTDBGMODVTDBG,pfnLineAdd} */
|
---|
84 | static DECLCALLBACK(int) rtDbgModNm_LineAdd(PRTDBGMODINT pMod, const char *pszFile, size_t cchFile, uint32_t uLineNo,
|
---|
85 | uint32_t iSeg, RTUINTPTR off, uint32_t *piOrdinal)
|
---|
86 | {
|
---|
87 | PRTDBGMODNM pThis = (PRTDBGMODNM)pMod->pvDbgPriv;
|
---|
88 | Assert(!pszFile[cchFile]); NOREF(cchFile);
|
---|
89 | return RTDbgModLineAdd(pThis->hCnt, pszFile, uLineNo, iSeg, off, piOrdinal);
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByAddr} */
|
---|
94 | static DECLCALLBACK(int) rtDbgModNm_SymbolByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
|
---|
95 | PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
|
---|
96 | {
|
---|
97 | PRTDBGMODNM pThis = (PRTDBGMODNM)pMod->pvDbgPriv;
|
---|
98 | return RTDbgModSymbolByAddr(pThis->hCnt, iSeg, off, fFlags, poffDisp, pSymInfo);
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByName} */
|
---|
103 | static DECLCALLBACK(int) rtDbgModNm_SymbolByName(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
|
---|
104 | PRTDBGSYMBOL pSymInfo)
|
---|
105 | {
|
---|
106 | PRTDBGMODNM pThis = (PRTDBGMODNM)pMod->pvDbgPriv;
|
---|
107 | Assert(!pszSymbol[cchSymbol]); NOREF(cchSymbol);
|
---|
108 | return RTDbgModSymbolByName(pThis->hCnt, pszSymbol, pSymInfo);
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByOrdinal} */
|
---|
113 | static DECLCALLBACK(int) rtDbgModNm_SymbolByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
|
---|
114 | {
|
---|
115 | PRTDBGMODNM pThis = (PRTDBGMODNM)pMod->pvDbgPriv;
|
---|
116 | return RTDbgModSymbolByOrdinal(pThis->hCnt, iOrdinal, pSymInfo);
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolCount} */
|
---|
121 | static DECLCALLBACK(uint32_t) rtDbgModNm_SymbolCount(PRTDBGMODINT pMod)
|
---|
122 | {
|
---|
123 | PRTDBGMODNM pThis = (PRTDBGMODNM)pMod->pvDbgPriv;
|
---|
124 | return RTDbgModSymbolCount(pThis->hCnt);
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolAdd} */
|
---|
129 | static DECLCALLBACK(int) rtDbgModNm_SymbolAdd(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
|
---|
130 | RTDBGSEGIDX iSeg, RTUINTPTR off, RTUINTPTR cb, uint32_t fFlags,
|
---|
131 | uint32_t *piOrdinal)
|
---|
132 | {
|
---|
133 | PRTDBGMODNM pThis = (PRTDBGMODNM)pMod->pvDbgPriv;
|
---|
134 | Assert(!pszSymbol[cchSymbol]); NOREF(cchSymbol);
|
---|
135 | return RTDbgModSymbolAdd(pThis->hCnt, pszSymbol, iSeg, off, cb, fFlags, piOrdinal);
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentByIndex} */
|
---|
140 | static DECLCALLBACK(int) rtDbgModNm_SegmentByIndex(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
|
---|
141 | {
|
---|
142 | PRTDBGMODNM pThis = (PRTDBGMODNM)pMod->pvDbgPriv;
|
---|
143 | return RTDbgModSegmentByIndex(pThis->hCnt, iSeg, pSegInfo);
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentCount} */
|
---|
148 | static DECLCALLBACK(RTDBGSEGIDX) rtDbgModNm_SegmentCount(PRTDBGMODINT pMod)
|
---|
149 | {
|
---|
150 | PRTDBGMODNM pThis = (PRTDBGMODNM)pMod->pvDbgPriv;
|
---|
151 | return RTDbgModSegmentCount(pThis->hCnt);
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentAdd} */
|
---|
156 | static DECLCALLBACK(int) rtDbgModNm_SegmentAdd(PRTDBGMODINT pMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName,
|
---|
157 | size_t cchName, uint32_t fFlags, PRTDBGSEGIDX piSeg)
|
---|
158 | {
|
---|
159 | PRTDBGMODNM pThis = (PRTDBGMODNM)pMod->pvDbgPriv;
|
---|
160 | Assert(!pszName[cchName]); NOREF(cchName);
|
---|
161 | return RTDbgModSegmentAdd(pThis->hCnt, uRva, cb, pszName, fFlags, piSeg);
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /** @interface_method_impl{RTDBGMODVTDBG,pfnImageSize} */
|
---|
166 | static DECLCALLBACK(RTUINTPTR) rtDbgModNm_ImageSize(PRTDBGMODINT pMod)
|
---|
167 | {
|
---|
168 | PRTDBGMODNM pThis = (PRTDBGMODNM)pMod->pvDbgPriv;
|
---|
169 | return RTDbgModImageSize(pThis->hCnt);
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /** @interface_method_impl{RTDBGMODVTDBG,pfnRvaToSegOff} */
|
---|
174 | static DECLCALLBACK(RTDBGSEGIDX) rtDbgModNm_RvaToSegOff(PRTDBGMODINT pMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
|
---|
175 | {
|
---|
176 | PRTDBGMODNM pThis = (PRTDBGMODNM)pMod->pvDbgPriv;
|
---|
177 | return RTDbgModRvaToSegOff(pThis->hCnt, uRva, poffSeg);
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | /** @interface_method_impl{RTDBGMODVTDBG,pfnClose} */
|
---|
182 | static DECLCALLBACK(int) rtDbgModNm_Close(PRTDBGMODINT pMod)
|
---|
183 | {
|
---|
184 | PRTDBGMODNM pThis = (PRTDBGMODNM)pMod->pvDbgPriv;
|
---|
185 | RTDbgModRelease(pThis->hCnt);
|
---|
186 | pThis->hCnt = NIL_RTDBGMOD;
|
---|
187 | RTMemFree(pThis);
|
---|
188 | return VINF_SUCCESS;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Scans a NM-like map file.
|
---|
194 | *
|
---|
195 | * This implements both passes to avoid code duplication.
|
---|
196 | *
|
---|
197 | * @returns IPRT status code.
|
---|
198 | * @param pThis The instance data.
|
---|
199 | * @param pStrm The stream.
|
---|
200 | * @param fAddSymbols false in the first pass, true in the second.
|
---|
201 | */
|
---|
202 | static int rtDbgModNmScanFile(PRTDBGMODNM pThis, PRTSTREAM pStrm, bool fAddSymbols)
|
---|
203 | {
|
---|
204 | /*
|
---|
205 | * Try parse the stream.
|
---|
206 | */
|
---|
207 | RTUINTPTR SegZeroRva = fAddSymbols ? RTDbgModSegmentRva(pThis->hCnt, 0/*iSeg*/) : 0;
|
---|
208 | char szSym[RTDBG_SYMBOL_NAME_LENGTH] = "";
|
---|
209 | size_t cchMod = 0;
|
---|
210 | size_t offSym = 0;
|
---|
211 | unsigned cchAddr = 0;
|
---|
212 | uint64_t u64Low = UINT64_MAX;
|
---|
213 | uint64_t u64High = 0;
|
---|
214 | int fWithType = -1;
|
---|
215 | char szLine[512];
|
---|
216 | int rc;
|
---|
217 | while (RT_SUCCESS(rc = RTStrmGetLine(pStrm, szLine, sizeof(szLine))))
|
---|
218 | {
|
---|
219 | char chType;
|
---|
220 | if (RT_C_IS_XDIGIT(szLine[0]))
|
---|
221 | {
|
---|
222 | /*
|
---|
223 | * This is really what C was made for, string parsing.
|
---|
224 | */
|
---|
225 | /* The symbol value (address). */
|
---|
226 | uint64_t u64Addr;
|
---|
227 | char *psz;
|
---|
228 | rc = RTStrToUInt64Ex(szLine, &psz, 16, &u64Addr);
|
---|
229 | if (rc != VWRN_TRAILING_CHARS)
|
---|
230 | return VERR_DBG_NOT_NM_MAP_FILE;
|
---|
231 |
|
---|
232 | /* Check the address width. */
|
---|
233 | if (cchAddr == 0)
|
---|
234 | cchAddr = psz == &szLine[8] ? 8 : 16;
|
---|
235 | if (psz != &szLine[cchAddr])
|
---|
236 | return VERR_DBG_NOT_NM_MAP_FILE;
|
---|
237 |
|
---|
238 | /* Get the type and check for single space before symbol. */
|
---|
239 | char *pszName;
|
---|
240 | if (fWithType < 0)
|
---|
241 | fWithType = RT_C_IS_BLANK(szLine[cchAddr + 2]) ? 1 : 0; /* have type? Linux 2.4 /proc/ksyms doesn't. */
|
---|
242 | if (fWithType)
|
---|
243 | {
|
---|
244 | chType = szLine[cchAddr + 1];
|
---|
245 | pszName = &szLine[cchAddr + 3];
|
---|
246 | if ( RT_C_IS_BLANK(chType)
|
---|
247 | || !RT_C_IS_BLANK(szLine[cchAddr + 2])
|
---|
248 | || RT_C_IS_BLANK(szLine[cchAddr + 3]))
|
---|
249 | return VERR_DBG_NOT_NM_MAP_FILE;
|
---|
250 | }
|
---|
251 | else
|
---|
252 | {
|
---|
253 | chType = 'T';
|
---|
254 | pszName = &szLine[cchAddr + 1];
|
---|
255 | }
|
---|
256 |
|
---|
257 | /* Find the end of the symbol name. */
|
---|
258 | char *pszNameEnd = pszName;
|
---|
259 | char ch;
|
---|
260 | while ((ch = *pszNameEnd) != '\0' && !RT_C_IS_SPACE(ch))
|
---|
261 | pszNameEnd++;
|
---|
262 |
|
---|
263 | /* Any module name (linux /proc/kallsyms) following in brackets? */
|
---|
264 | char *pszModName = pszNameEnd;
|
---|
265 | char *pszModNameEnd = pszModName;
|
---|
266 | if (*pszModName)
|
---|
267 | {
|
---|
268 | *pszModName++ = '\0';
|
---|
269 | pszModNameEnd = pszModName = RTStrStripL(pszModName);
|
---|
270 | if (*pszModName != '\0')
|
---|
271 | {
|
---|
272 | if (*pszModName != '[')
|
---|
273 | return VERR_DBG_NOT_LINUX_KALLSYMS;
|
---|
274 | pszModNameEnd = ++pszModName;
|
---|
275 | while ((ch = *pszModNameEnd) != '\0' && ch != ']')
|
---|
276 | pszModNameEnd++;
|
---|
277 | if (ch != ']')
|
---|
278 | return VERR_DBG_NOT_LINUX_KALLSYMS;
|
---|
279 | char *pszEnd = pszModNameEnd + 1;
|
---|
280 | if ((size_t)(pszModNameEnd - pszModName) >= 128) /* lazy bird */
|
---|
281 | return VERR_DBG_NOT_LINUX_KALLSYMS;
|
---|
282 | *pszModNameEnd = '\0';
|
---|
283 | if (*pszEnd)
|
---|
284 | pszEnd = RTStrStripL(pszEnd);
|
---|
285 | if (*pszEnd)
|
---|
286 | return VERR_DBG_NOT_LINUX_KALLSYMS;
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | /*
|
---|
291 | * Did the module change? Then update the symbol prefix.
|
---|
292 | */
|
---|
293 | if ( cchMod != (size_t)(pszModNameEnd - pszModName)
|
---|
294 | || memcmp(pszModName, szSym, cchMod))
|
---|
295 | {
|
---|
296 | cchMod = pszModNameEnd - pszModName;
|
---|
297 | if (cchMod == 0)
|
---|
298 | offSym = 0;
|
---|
299 | else
|
---|
300 | {
|
---|
301 | memcpy(szSym, pszModName, cchMod);
|
---|
302 | szSym[cchMod] = '.';
|
---|
303 | offSym = cchMod + 1;
|
---|
304 | }
|
---|
305 | szSym[offSym] = '\0';
|
---|
306 | }
|
---|
307 |
|
---|
308 | /*
|
---|
309 | * Validate the type and add the symbol if it's a type we care for.
|
---|
310 | */
|
---|
311 | uint32_t fFlags = 0;
|
---|
312 | RTDBGSEGIDX iSegSym = 0;
|
---|
313 | switch (chType)
|
---|
314 | {
|
---|
315 | /* absolute */
|
---|
316 | case 'a':
|
---|
317 | case '?': /* /proc/kallsyms */
|
---|
318 | iSegSym = RTDBGSEGIDX_ABS;
|
---|
319 | /// @todo fFlags |= RTDBG_SYM_FLAGS_LOCAL;
|
---|
320 | break;
|
---|
321 | case 'A':
|
---|
322 | iSegSym = RTDBGSEGIDX_ABS;
|
---|
323 | /// @todo fFlags |= RTDBG_SYM_FLAGS_PUBLIC;
|
---|
324 | break;
|
---|
325 |
|
---|
326 | case 'b':
|
---|
327 | /// @todo fFlags |= RTDBG_SYM_FLAGS_LOCAL;
|
---|
328 | break;
|
---|
329 | case 'B':
|
---|
330 | /// @todo fFlags |= RTDBG_SYM_FLAGS_PUBLIC;
|
---|
331 | break;
|
---|
332 |
|
---|
333 | case 'c':
|
---|
334 | /// @todo fFlags |= RTDBG_SYM_FLAGS_LOCAL | RTDBG_SYM_FLAGS_COMMON;
|
---|
335 | break;
|
---|
336 | case 'C':
|
---|
337 | /// @todo fFlags |= RTDBG_SYM_FLAGS_PUBLIC | RTDBG_SYM_FLAGS_COMMON;
|
---|
338 | break;
|
---|
339 |
|
---|
340 | case 'd':
|
---|
341 | /// @todo fFlags |= RTDBG_SYM_FLAGS_LOCAL;
|
---|
342 | break;
|
---|
343 | case 'D':
|
---|
344 | /// @todo fFlags |= RTDBG_SYM_FLAGS_PUBLIC;
|
---|
345 | break;
|
---|
346 |
|
---|
347 | case 'g':
|
---|
348 | /// @todo fFlags |= RTDBG_SYM_FLAGS_LOCAL;
|
---|
349 | break;
|
---|
350 | case 'G':
|
---|
351 | /// @todo fFlags |= RTDBG_SYM_FLAGS_PUBLIC;
|
---|
352 | break;
|
---|
353 |
|
---|
354 | case 'i':
|
---|
355 | /// @todo fFlags |= RTDBG_SYM_FLAGS_LOCAL;
|
---|
356 | break;
|
---|
357 | case 'I':
|
---|
358 | /// @todo fFlags |= RTDBG_SYM_FLAGS_PUBLIC;
|
---|
359 | break;
|
---|
360 |
|
---|
361 | case 'r':
|
---|
362 | /// @todo fFlags |= RTDBG_SYM_FLAGS_LOCAL | RTDBG_SYM_FLAGS_CONST;
|
---|
363 | break;
|
---|
364 | case 'R':
|
---|
365 | /// @todo fFlags |= RTDBG_SYM_FLAGS_PUBLIC | RTDBG_SYM_FLAGS_CONST;
|
---|
366 | break;
|
---|
367 |
|
---|
368 | case 's':
|
---|
369 | /// @todo fFlags |= RTDBG_SYM_FLAGS_LOCAL;
|
---|
370 | break;
|
---|
371 | case 'S':
|
---|
372 | /// @todo fFlags |= RTDBG_SYM_FLAGS_PUBLIC;
|
---|
373 | break;
|
---|
374 |
|
---|
375 | case 't':
|
---|
376 | /// @todo fFlags |= RTDBG_SYM_FLAGS_LOCAL | RTDBG_SYM_FLAGS_TEXT;
|
---|
377 | break;
|
---|
378 | case 'T':
|
---|
379 | /// @todo fFlags |= RTDBG_SYM_FLAGS_PUBLIC | RTDBG_SYM_FLAGS_TEXT;
|
---|
380 | break;
|
---|
381 |
|
---|
382 | case 'w':
|
---|
383 | /// @todo fFlags |= RTDBG_SYM_FLAGS_WEAK | RTDBG_SYM_FLAGS_LOCAL; //???
|
---|
384 | break;
|
---|
385 | case 'W':
|
---|
386 | /// @todo fFlags |= RTDBG_SYM_FLAGS_WEAK | RTDBG_SYM_FLAGS_PUBLIC;
|
---|
387 | break;
|
---|
388 |
|
---|
389 | case 'N': /* debug */
|
---|
390 | case 'n':
|
---|
391 | case '-': /* stabs */
|
---|
392 | case 'u': /* undefined (/proc/kallsyms) */
|
---|
393 | case 'U':
|
---|
394 | case 'v': /* weakext */
|
---|
395 | case 'V':
|
---|
396 | iSegSym = NIL_RTDBGSEGIDX;
|
---|
397 | break;
|
---|
398 |
|
---|
399 | default:
|
---|
400 | return VERR_DBG_NOT_NM_MAP_FILE;
|
---|
401 | }
|
---|
402 |
|
---|
403 | if (iSegSym != NIL_RTDBGSEGIDX)
|
---|
404 | {
|
---|
405 | if (fAddSymbols)
|
---|
406 | {
|
---|
407 | size_t cchName = pszNameEnd - pszName;
|
---|
408 | if (cchName >= sizeof(szSym) - offSym)
|
---|
409 | cchName = sizeof(szSym) - offSym - 1;
|
---|
410 | memcpy(&szSym[offSym], pszName, cchName + 1);
|
---|
411 | if (iSegSym == 0)
|
---|
412 | rc = RTDbgModSymbolAdd(pThis->hCnt, szSym, iSegSym, u64Addr - SegZeroRva, 0/*cb*/, fFlags, NULL);
|
---|
413 | else
|
---|
414 | rc = RTDbgModSymbolAdd(pThis->hCnt, szSym, iSegSym, u64Addr, 0/*cb*/, fFlags, NULL);
|
---|
415 | if ( RT_FAILURE(rc)
|
---|
416 | && rc != VERR_DBG_DUPLICATE_SYMBOL
|
---|
417 | && rc != VERR_DBG_ADDRESS_CONFLICT) /* (don't be too strict) */
|
---|
418 | return rc;
|
---|
419 | }
|
---|
420 |
|
---|
421 | /* Track segment span. */
|
---|
422 | if (iSegSym == 0)
|
---|
423 | {
|
---|
424 | if (u64Low > u64Addr)
|
---|
425 | u64Low = u64Addr;
|
---|
426 | if (u64High < u64Addr)
|
---|
427 | u64High = u64Addr;
|
---|
428 | }
|
---|
429 | }
|
---|
430 | }
|
---|
431 | else
|
---|
432 | {
|
---|
433 | /*
|
---|
434 | * This is either a blank line or a symbol without an address.
|
---|
435 | */
|
---|
436 | RTStrStripR(szLine);
|
---|
437 | if (szLine[0])
|
---|
438 | {
|
---|
439 | size_t cch = strlen(szLine);
|
---|
440 | if (cchAddr == 0)
|
---|
441 | cchAddr = cch < 16+3 || szLine[8+1] != ' ' ? 8 : 16;
|
---|
442 | if (cch < cchAddr+3+1)
|
---|
443 | return VERR_DBG_NOT_NM_MAP_FILE;
|
---|
444 | chType = szLine[cchAddr + 1];
|
---|
445 | if ( chType != 'U'
|
---|
446 | && chType != 'w')
|
---|
447 | return VERR_DBG_NOT_NM_MAP_FILE;
|
---|
448 | char *pszType = RTStrStripL(szLine);
|
---|
449 | if (pszType != &szLine[cchAddr + 1])
|
---|
450 | return VERR_DBG_NOT_NM_MAP_FILE;
|
---|
451 | if (!RT_C_IS_BLANK(szLine[cchAddr + 2]))
|
---|
452 | return VERR_DBG_NOT_NM_MAP_FILE;
|
---|
453 | }
|
---|
454 | /* else: blank - ignored */
|
---|
455 | }
|
---|
456 | }
|
---|
457 |
|
---|
458 | /*
|
---|
459 | * The final segment.
|
---|
460 | */
|
---|
461 | if (rc == VERR_EOF)
|
---|
462 | {
|
---|
463 | if (fAddSymbols)
|
---|
464 | rc = VINF_SUCCESS;
|
---|
465 | else
|
---|
466 | {
|
---|
467 | if ( u64Low != UINT64_MAX
|
---|
468 | || u64High != 0)
|
---|
469 | rc = RTDbgModSegmentAdd(pThis->hCnt, u64Low, u64High - u64Low + 1, "main", 0, NULL);
|
---|
470 | else /* No sensible symbols... throw an error instead? */
|
---|
471 | rc = RTDbgModSegmentAdd(pThis->hCnt, 0, 0, "main", 0, NULL);
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 | return rc;
|
---|
476 | }
|
---|
477 |
|
---|
478 |
|
---|
479 | /** @interface_method_impl{RTDBGMODVTDBG,pfnTryOpen} */
|
---|
480 | static DECLCALLBACK(int) rtDbgModNm_TryOpen(PRTDBGMODINT pMod, RTLDRARCH enmArch)
|
---|
481 | {
|
---|
482 | NOREF(enmArch);
|
---|
483 |
|
---|
484 | /*
|
---|
485 | * Fend off images.
|
---|
486 | */
|
---|
487 | if ( !pMod->pszDbgFile
|
---|
488 | || pMod->pImgVt)
|
---|
489 | return VERR_DBG_NO_MATCHING_INTERPRETER;
|
---|
490 |
|
---|
491 | /*
|
---|
492 | * Try open the file and create an instance.
|
---|
493 | */
|
---|
494 | PRTSTREAM pStrm;
|
---|
495 | int rc = RTStrmOpen(pMod->pszDbgFile, "r", &pStrm);
|
---|
496 | if (RT_SUCCESS(rc))
|
---|
497 | {
|
---|
498 | PRTDBGMODNM pThis = (PRTDBGMODNM)RTMemAlloc(sizeof(*pThis));
|
---|
499 | if (pThis)
|
---|
500 | {
|
---|
501 | rc = RTDbgModCreate(&pThis->hCnt, pMod->pszName, 0 /*cbSeg*/, 0 /*fFlags*/);
|
---|
502 | if (RT_SUCCESS(rc))
|
---|
503 | {
|
---|
504 | /*
|
---|
505 | * Scan the file twice, first to figure the segment
|
---|
506 | * sizes, then to add the symbol.
|
---|
507 | */
|
---|
508 | rc = rtDbgModNmScanFile(pThis, pStrm, false /*fAddSymbols*/);
|
---|
509 | if (RT_SUCCESS(rc))
|
---|
510 | rc = RTStrmRewind(pStrm);
|
---|
511 | if (RT_SUCCESS(rc))
|
---|
512 | rc = rtDbgModNmScanFile(pThis, pStrm, true /*fAddSymbols*/);
|
---|
513 | if (RT_SUCCESS(rc))
|
---|
514 | {
|
---|
515 | RTStrmClose(pStrm);
|
---|
516 | pMod->pvDbgPriv = pThis;
|
---|
517 | return rc;
|
---|
518 | }
|
---|
519 | }
|
---|
520 | RTDbgModRelease(pThis->hCnt);
|
---|
521 | }
|
---|
522 | else
|
---|
523 | rc = VERR_NO_MEMORY;
|
---|
524 | RTStrmClose(pStrm);
|
---|
525 | }
|
---|
526 | return rc;
|
---|
527 | }
|
---|
528 |
|
---|
529 |
|
---|
530 |
|
---|
531 | /** Virtual function table for the NM-like map file reader. */
|
---|
532 | DECL_HIDDEN_CONST(RTDBGMODVTDBG) const g_rtDbgModVtDbgNm =
|
---|
533 | {
|
---|
534 | /*.u32Magic = */ RTDBGMODVTDBG_MAGIC,
|
---|
535 | /*.fSupports = */ RT_DBGTYPE_MAP,
|
---|
536 | /*.pszName = */ "nm",
|
---|
537 | /*.pfnTryOpen = */ rtDbgModNm_TryOpen,
|
---|
538 | /*.pfnClose = */ rtDbgModNm_Close,
|
---|
539 |
|
---|
540 | /*.pfnRvaToSegOff = */ rtDbgModNm_RvaToSegOff,
|
---|
541 | /*.pfnImageSize = */ rtDbgModNm_ImageSize,
|
---|
542 |
|
---|
543 | /*.pfnSegmentAdd = */ rtDbgModNm_SegmentAdd,
|
---|
544 | /*.pfnSegmentCount = */ rtDbgModNm_SegmentCount,
|
---|
545 | /*.pfnSegmentByIndex = */ rtDbgModNm_SegmentByIndex,
|
---|
546 |
|
---|
547 | /*.pfnSymbolAdd = */ rtDbgModNm_SymbolAdd,
|
---|
548 | /*.pfnSymbolCount = */ rtDbgModNm_SymbolCount,
|
---|
549 | /*.pfnSymbolByOrdinal = */ rtDbgModNm_SymbolByOrdinal,
|
---|
550 | /*.pfnSymbolByName = */ rtDbgModNm_SymbolByName,
|
---|
551 | /*.pfnSymbolByAddr = */ rtDbgModNm_SymbolByAddr,
|
---|
552 |
|
---|
553 | /*.pfnLineAdd = */ rtDbgModNm_LineAdd,
|
---|
554 | /*.pfnLineCount = */ rtDbgModNm_LineCount,
|
---|
555 | /*.pfnLineByOrdinal = */ rtDbgModNm_LineByOrdinal,
|
---|
556 | /*.pfnLineByAddr = */ rtDbgModNm_LineByAddr,
|
---|
557 |
|
---|
558 | /*.u32EndMagic = */ RTDBGMODVTDBG_MAGIC
|
---|
559 | };
|
---|
560 |
|
---|