VirtualBox

source: vbox/trunk/src/VBox/VMM/DBGFSym.cpp@ 30441

Last change on this file since 30441 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 33.3 KB
Line 
1/* $Id: DBGFSym.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, Symbol Management.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_DBGF
23#if defined(RT_OS_WINDOWS) && 0 //defined(DEBUG_bird) // enabled this is you want to debug win32 guests, the hypervisor of EFI.
24# include <Windows.h>
25# define _IMAGEHLP64
26# include <DbgHelp.h>
27# define HAVE_DBGHELP /* if doing guest stuff, this can be nice. */
28#endif
29/** @todo Only use DBGHELP for reading modules since it doesn't do all we want (relocations), or is way to slow in some cases (add symbol)! */
30#include <VBox/dbgf.h>
31#include <VBox/mm.h>
32#include <VBox/pdmapi.h>
33#include "DBGFInternal.h"
34#include <VBox/vm.h>
35#include <VBox/err.h>
36#include <VBox/log.h>
37
38#include <iprt/assert.h>
39#include <iprt/path.h>
40#include <iprt/ctype.h>
41#include <iprt/env.h>
42#include <iprt/param.h>
43#ifndef HAVE_DBGHELP
44# include <iprt/avl.h>
45# include <iprt/string.h>
46#endif
47
48#include <stdio.h> /* for fopen(). */ /** @todo use iprt/stream.h! */
49#include <stdlib.h>
50
51
52/*******************************************************************************
53* Internal Functions *
54*******************************************************************************/
55#ifdef HAVE_DBGHELP
56static DECLCALLBACK(int) dbgfR3EnumModules(PVM pVM, const char *pszFilename, const char *pszName,
57 RTUINTPTR ImageBase, size_t cbImage, bool fRC, void *pvArg);
58static int win32Error(PVM pVM);
59#endif
60
61
62/*******************************************************************************
63* Structures and Typedefs *
64*******************************************************************************/
65#ifndef HAVE_DBGHELP
66/* later */
67typedef struct DBGFMOD *PDBGFMOD;
68
69/**
70 * Internal represenation of a symbol.
71 */
72typedef struct DBGFSYM
73{
74 /** Node core with the symbol address range. */
75 AVLRGCPTRNODECORE Core;
76 /** Pointer to the module this symbol is associated with. */
77 PDBGFMOD pModule;
78 /** Pointer to the next symbol in with this name. */
79 struct DBGFSYM *pNext;
80 /** Symbol name. */
81 char szName[1];
82} DBGFSYM, *PDBGFSYM;
83
84/**
85 * Symbol name space node.
86 */
87typedef struct DBGFSYMSPACE
88{
89 /** Node core with the symbol name.
90 * (it's allocated in the same block as this struct) */
91 RTSTRSPACECORE Core;
92 /** Pointer to the first symbol with this name (LIFO). */
93 PDBGFSYM pSym;
94} DBGFSYMSPACE, *PDBGFSYMSPACE;
95
96#endif
97
98
99
100/*******************************************************************************
101* Internal Functions *
102*******************************************************************************/
103#ifndef HAVE_DBGHELP
104
105/**
106 * Initializes the symbol tree.
107 */
108static int dbgfR3SymbolInit(PVM pVM)
109{
110 PDBGFSYM pSym = (PDBGFSYM)MMR3HeapAlloc(pVM, MM_TAG_DBGF_SYMBOL, sizeof(*pSym));
111 if (pSym)
112 {
113 pSym->Core.Key = 0;
114 pSym->Core.KeyLast = ~0;
115 pSym->pModule = NULL;
116 pSym->szName[0] = '\0';
117 if (RTAvlrGCPtrInsert(&pVM->dbgf.s.SymbolTree, &pSym->Core))
118 return VINF_SUCCESS;
119 AssertReleaseMsgFailed(("Failed to insert %RGv-%RGv!\n", pSym->Core.Key, pSym->Core.KeyLast));
120 return VERR_INTERNAL_ERROR;
121 }
122 return VERR_NO_MEMORY;
123}
124
125
126/**
127 * Insert a record into the symbol tree.
128 */
129static int dbgfR3SymbolInsert(PVM pVM, const char *pszName, RTGCPTR Address, size_t cb, PDBGFMOD pModule)
130{
131 /*
132 * Make the address space node.
133 */
134 size_t cchName = strlen(pszName) + 1;
135 PDBGFSYM pSym = (PDBGFSYM)MMR3HeapAlloc(pVM, MM_TAG_DBGF_SYMBOL, RT_OFFSETOF(DBGFSYM, szName[cchName]));
136 if (pSym)
137 {
138 pSym->Core.Key = Address;
139 pSym->Core.KeyLast = Address + cb;
140 pSym->pModule = pModule;
141 memcpy(pSym->szName, pszName, cchName);
142
143 PDBGFSYM pOld = (PDBGFSYM)RTAvlrGCPtrRangeGet(&pVM->dbgf.s.SymbolTree, (RTGCPTR)Address);
144 if (pOld)
145 {
146 pSym->Core.KeyLast = pOld->Core.KeyLast;
147 if (pOld->Core.Key == pSym->Core.Key)
148 {
149 pOld = (PDBGFSYM)RTAvlrGCPtrRemove(&pVM->dbgf.s.SymbolTree, (RTGCPTR)Address);
150 AssertRelease(pOld);
151 MMR3HeapFree(pOld);
152 }
153 else
154 pOld->Core.KeyLast = Address - 1;
155 if (RTAvlrGCPtrInsert(&pVM->dbgf.s.SymbolTree, &pSym->Core))
156 {
157 /*
158 * Make the name space node.
159 */
160 PDBGFSYMSPACE pName = (PDBGFSYMSPACE)RTStrSpaceGet(pVM->dbgf.s.pSymbolSpace, pszName);
161 if (!pName)
162 {
163 /* make new symbol space node. */
164 pName = (PDBGFSYMSPACE)MMR3HeapAlloc(pVM, MM_TAG_DBGF_SYMBOL, sizeof(*pName) + cchName);
165 if (pName)
166 {
167 pName->Core.pszString = (char *)memcpy(pName + 1, pszName, cchName);
168 pName->pSym = pSym;
169 if (RTStrSpaceInsert(pVM->dbgf.s.pSymbolSpace, &pName->Core))
170 return VINF_SUCCESS;
171 }
172 else
173 return VINF_SUCCESS;
174 }
175 else
176 {
177 /* Add to existing symbol name. */
178 pSym->pNext = pName->pSym;
179 pName->pSym = pSym;
180 return VINF_SUCCESS;
181 }
182 }
183 AssertReleaseMsgFailed(("Failed to insert %RGv-%RGv!\n", pSym->Core.Key, pSym->Core.KeyLast));
184 }
185 else
186 AssertMsgFailed(("pOld! %RGv %s\n", pSym->Core.Key, pszName));
187 return VERR_INTERNAL_ERROR;
188
189 }
190 return VERR_NO_MEMORY;
191}
192
193
194/**
195 * Get nearest symbol.
196 * @returns NULL if no symbol was the for that address.
197 */
198static PDBGFSYM dbgfR3SymbolGetAddr(PVM pVM, RTGCPTR Address)
199{
200 PDBGFSYM pSym = (PDBGFSYM)RTAvlrGCPtrRangeGet(&pVM->dbgf.s.SymbolTree, Address);
201 Assert(pSym);
202 if (pSym && pSym->szName[0])
203 return pSym;
204 return NULL;
205}
206
207
208/**
209 * Get first symbol.
210 * @returns NULL if no symbol by that name.
211 */
212static PDBGFSYM dbgfR3SymbolGetName(PVM pVM, const char *pszSymbol)
213{
214 PDBGFSYMSPACE pName = (PDBGFSYMSPACE)RTStrSpaceGet(pVM->dbgf.s.pSymbolSpace, pszSymbol);
215 if (pName)
216 return pName->pSym;
217 return NULL;
218}
219
220#endif
221
222
223/**
224 * Strips all kind of spaces from head and tail of a string.
225 */
226static char *dbgfR3Strip(char *psz)
227{
228 while (*psz && RT_C_IS_SPACE(*psz))
229 psz++;
230 char *psz2 = strchr(psz, '\0') - 1;
231 while (psz2 >= psz && RT_C_IS_SPACE(*psz2))
232 *psz2-- = '\0';
233 return psz;
234}
235
236
237/**
238 * Initialize the debug info for a VM.
239 *
240 * This will check the CFGM for any symbols or symbol files
241 * which needs loading.
242 *
243 * @returns VBox status code.
244 * @param pVM The VM handle.
245 */
246int dbgfR3SymInit(PVM pVM)
247{
248 int rc;
249
250 /*
251 * Initialize the symbol table.
252 */
253 pVM->dbgf.s.pSymbolSpace = (PRTSTRSPACE)MMR3HeapAllocZ(pVM, MM_TAG_DBGF_SYMBOL, sizeof(*pVM->dbgf.s.pSymbolSpace));
254 AssertReturn(pVM->dbgf.s.pSymbolSpace, VERR_NO_MEMORY);
255
256#ifndef HAVE_DBGHELP
257 /* modules & lines later */
258 rc = dbgfR3SymbolInit(pVM);
259 if (RT_FAILURE(rc))
260 return rc;
261 pVM->dbgf.s.fSymInited = true;
262#endif
263
264 /*
265 * Check if there are 'loadsyms' commands in the configuration.
266 */
267 PCFGMNODE pNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "/DBGF/loadsyms/");
268 if (pNode)
269 {
270 /*
271 * Enumerate the commands.
272 */
273 for (PCFGMNODE pCmdNode = CFGMR3GetFirstChild(pNode);
274 pCmdNode;
275 pCmdNode = CFGMR3GetNextChild(pCmdNode))
276 {
277 char szCmdName[128];
278 CFGMR3GetName(pCmdNode, &szCmdName[0], sizeof(szCmdName));
279
280 /* File */
281 char *pszFilename;
282 rc = CFGMR3QueryStringAlloc(pCmdNode, "Filename", &pszFilename);
283 AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'File' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
284
285 /* Delta (optional) */
286 RTGCINTPTR offDelta;
287 rc = CFGMR3QueryGCPtrS(pNode, "Delta", &offDelta);
288 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
289 offDelta = 0;
290 else
291 AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'Delta' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
292
293 /* Module (optional) */
294 char *pszModule;
295 rc = CFGMR3QueryStringAlloc(pCmdNode, "Module", &pszModule);
296 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
297 pszModule = NULL;
298 else
299 AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'Module' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
300
301 /* Module (optional) */
302 RTGCUINTPTR ModuleAddress;
303 rc = CFGMR3QueryGCPtrU(pNode, "ModuleAddress", &ModuleAddress);
304 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
305 ModuleAddress = 0;
306 else
307 AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'ModuleAddress' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
308
309 /* Image size (optional) */
310 RTGCUINTPTR cbModule;
311 rc = CFGMR3QueryGCPtrU(pNode, "ModuleSize", &cbModule);
312 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
313 cbModule = 0;
314 else
315 AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'ModuleAddress' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
316
317
318 /*
319 * Execute the command.
320 */
321 rc = DBGFR3ModuleLoad(pVM, pszFilename, offDelta, pszModule, ModuleAddress, cbModule);
322 AssertMsgRCReturn(rc, ("pszFilename=%s offDelta=%RGv pszModule=%s ModuleAddress=%RGv cbModule=%RGv\n",
323 pszFilename, offDelta, pszModule, ModuleAddress, cbModule), rc);
324
325 MMR3HeapFree(pszModule);
326 MMR3HeapFree(pszFilename);
327 }
328 }
329
330 /*
331 * Check if there are any 'symadd' commands in the configuration.
332 */
333
334 return VINF_SUCCESS;
335}
336
337
338/**
339 * We delay certain
340 * Initialize the debug info for a VM.
341 */
342int dbgfR3SymLazyInit(PVM pVM)
343{
344 if (pVM->dbgf.s.fSymInited)
345 return VINF_SUCCESS;
346#ifdef HAVE_DBGHELP
347 if (SymInitialize(pVM, NULL, FALSE))
348 {
349 pVM->dbgf.s.fSymInited = true;
350 SymSetOptions(SYMOPT_LOAD_LINES | SYMOPT_AUTO_PUBLICS | SYMOPT_ALLOW_ABSOLUTE_SYMBOLS);
351
352 /*
353 * Enumerate all modules loaded by PDM and add them to the symbol database.
354 */
355 PDMR3LdrEnumModules(pVM, dbgfR3EnumModules, NULL);
356 return VINF_SUCCESS;
357 }
358 return win32Error(pVM);
359#else
360 return VINF_SUCCESS;
361#endif
362}
363
364
365#ifdef HAVE_DBGHELP
366/**
367 * Module enumeration callback function.
368 *
369 * @returns VBox status.
370 * Failure will stop the search and return the return code.
371 * Warnings will be ignored and not returned.
372 * @param pVM VM Handle.
373 * @param pszFilename Module filename.
374 * @param pszName Module name. (short and unique)
375 * @param ImageBase Address where to executable image is loaded.
376 * @param cbImage Size of the executable image.
377 * @param fRC Set if guest context, clear if host context.
378 * @param pvArg User argument.
379 */
380static DECLCALLBACK(int) dbgfR3EnumModules(PVM pVM, const char *pszFilename, const char *pszName,
381 RTUINTPTR ImageBase, size_t cbImage, bool fRC, void *pvArg)
382{
383 DWORD64 LoadedImageBase = SymLoadModule64(pVM, NULL, (char *)(void *)pszFilename,
384 (char *)(void *)pszName, ImageBase, (DWORD)cbImage);
385 if (!LoadedImageBase)
386 Log(("SymLoadModule64(,,%s,,) -> lasterr=%d\n", pszFilename, GetLastError()));
387 else
388 Log(("Loaded debuginfo for %s - %s %llx\n", pszName, pszFilename, LoadedImageBase));
389
390 return VINF_SUCCESS;
391}
392#endif
393
394
395/**
396 * Terminate the debug info repository for the specified VM.
397 *
398 * @returns VBox status.
399 * @param pVM VM Handle.
400 */
401int dbgfR3SymTerm(PVM pVM)
402{
403#ifdef HAVE_DBGHELP
404 if (pVM->dbgf.s.fSymInited)
405 SymCleanup(pVM);
406 pVM->dbgf.s.fSymInited = false;
407 return VINF_SUCCESS;
408#else
409 pVM->dbgf.s.SymbolTree = 0; /* MM cleans up allocations */
410 pVM->dbgf.s.fSymInited = false;
411 return VINF_SUCCESS;
412#endif
413}
414
415
416/** Symbol file type.. */
417typedef enum SYMFILETYPE
418{
419 SYMFILETYPE_UNKNOWN,
420 SYMFILETYPE_LD_MAP,
421 SYMFILETYPE_MS_MAP,
422 SYMFILETYPE_OBJDUMP,
423 SYMFILETYPE_LINUX_SYSTEM_MAP,
424 SYMFILETYPE_PDB,
425 SYMFILETYPE_DBG,
426 SYMFILETYPE_MZ,
427 SYMFILETYPE_ELF
428} SYMFILETYPE, *PSYMFILETYPE;
429
430
431
432/**
433 * Probe the type of a symbol information file.
434 *
435 * @returns The file type.
436 * @param pFile File handle.
437 */
438SYMFILETYPE dbgfR3ModuleProbe(FILE *pFile)
439{
440 char szHead[4096];
441 size_t cchHead = fread(szHead, 1, sizeof(szHead) - 1, pFile);
442 if (cchHead > 0)
443 {
444 szHead[cchHead] = '\0';
445 if (strstr(szHead, "Preferred load address is"))
446 return SYMFILETYPE_MS_MAP;
447
448 if ( strstr(szHead, "Archive member included because of")
449 || strstr(szHead, "Memory Configuration")
450 || strstr(szHead, "Linker script and memory map"))
451 return SYMFILETYPE_LD_MAP;
452
453 if ( RT_C_IS_XDIGIT(szHead[0])
454 && RT_C_IS_XDIGIT(szHead[1])
455 && RT_C_IS_XDIGIT(szHead[2])
456 && RT_C_IS_XDIGIT(szHead[3])
457 && RT_C_IS_XDIGIT(szHead[4])
458 && RT_C_IS_XDIGIT(szHead[5])
459 && RT_C_IS_XDIGIT(szHead[6])
460 && RT_C_IS_XDIGIT(szHead[7])
461 && szHead[8] == ' '
462 && RT_C_IS_ALPHA(szHead[9])
463 && szHead[10] == ' '
464 && (RT_C_IS_ALPHA(szHead[11]) || szHead[11] == '_' || szHead[11] == '$')
465 )
466 return SYMFILETYPE_LINUX_SYSTEM_MAP;
467
468 if ( RT_C_IS_XDIGIT(szHead[0])
469 && RT_C_IS_XDIGIT(szHead[1])
470 && RT_C_IS_XDIGIT(szHead[2])
471 && RT_C_IS_XDIGIT(szHead[3])
472 && RT_C_IS_XDIGIT(szHead[4])
473 && RT_C_IS_XDIGIT(szHead[5])
474 && RT_C_IS_XDIGIT(szHead[6])
475 && RT_C_IS_XDIGIT(szHead[7])
476 && RT_C_IS_XDIGIT(szHead[8])
477 && RT_C_IS_XDIGIT(szHead[9])
478 && RT_C_IS_XDIGIT(szHead[10])
479 && RT_C_IS_XDIGIT(szHead[11])
480 && RT_C_IS_XDIGIT(szHead[12])
481 && RT_C_IS_XDIGIT(szHead[13])
482 && RT_C_IS_XDIGIT(szHead[14])
483 && RT_C_IS_XDIGIT(szHead[15])
484 && szHead[16] == ' '
485 && RT_C_IS_ALPHA(szHead[17])
486 && szHead[18] == ' '
487 && (RT_C_IS_ALPHA(szHead[19]) || szHead[19] == '_' || szHead[19] == '$')
488 )
489 return SYMFILETYPE_LINUX_SYSTEM_MAP;
490
491 if (strstr(szHead, "Microsoft C/C++ MSF") == szHead)
492 return SYMFILETYPE_PDB;
493
494 if (strstr(szHead, "ELF") == szHead + 1)
495 return SYMFILETYPE_ELF;
496
497 if ( strstr(szHead, "MZ") == szHead
498 || strstr(szHead, "PE") == szHead
499 || strstr(szHead, "LE") == szHead
500 || strstr(szHead, "LX") == szHead
501 || strstr(szHead, "NE") == szHead)
502 return SYMFILETYPE_MZ;
503
504
505 if (strstr(szHead, "file format"))
506 return SYMFILETYPE_OBJDUMP;
507 }
508
509 return SYMFILETYPE_UNKNOWN;
510}
511
512
513static int dbgfR3LoadLinuxSystemMap(PVM pVM, FILE *pFile, RTGCUINTPTR ModuleAddress, RTGCUINTPTR AddressDelta)
514{
515 char szLine[4096];
516 while (fgets(szLine, sizeof(szLine), pFile))
517 {
518 /* parse the line: <address> <type> <name> */
519 const char *psz = dbgfR3Strip(szLine);
520 char *pszEnd = NULL;
521 uint64_t u64Address;
522 int rc = RTStrToUInt64Ex(psz, &pszEnd, 16, &u64Address);
523 RTGCUINTPTR Address = u64Address;
524 if ( RT_SUCCESS(rc)
525 && (*pszEnd == ' ' || *pszEnd == '\t')
526 && Address == u64Address
527 && u64Address != 0
528 && u64Address != (RTGCUINTPTR)~0)
529 {
530 pszEnd++;
531 if ( RT_C_IS_ALPHA(*pszEnd)
532 && (pszEnd[1] == ' ' || pszEnd[1] == '\t'))
533 {
534 psz = dbgfR3Strip(pszEnd + 2);
535 if (*psz)
536 {
537 int rc2 = DBGFR3SymbolAdd(pVM, ModuleAddress, Address + AddressDelta, 0, psz);
538 if (RT_FAILURE(rc2))
539 Log2(("DBGFR3SymbolAdd(,, %RGv, 0, '%s') -> %Rrc\n", Address, psz, rc2));
540 }
541 }
542 }
543 }
544 return VINF_SUCCESS;
545}
546
547/**
548 * Tries to open the file using the image search paths.
549 *
550 * This is currently a quick hack and the only way to specifying the path is by setting
551 * VBOXDBG_IMAGE_PATH in the environment. It uses semicolon as separator everywhere.
552 *
553 * @returns VBox status code.
554 * @param pVM The VM handle.
555 * @param pszFilename The name of the file to locate and open.
556 * @param pszFound Where to return the actual filename.
557 * @param cchFound The buffer size.
558 * @param ppFile Where to return the opened file.
559 */
560int dbgfR3ModuleLocateAndOpen(PVM pVM, const char *pszFilename, char *pszFound, size_t cchFound, FILE **ppFile)
561{
562 /* Check the filename length. */
563 size_t const cchFilename = strlen(pszFilename);
564 if (cchFilename >= cchFound)
565 return VERR_FILENAME_TOO_LONG;
566 const char *pszName = RTPathFilename(pszFilename);
567 if (!pszName)
568 return VERR_IS_A_DIRECTORY;
569 size_t const cchName = strlen(pszName);
570
571 /*
572 * Try default location first.
573 */
574 memcpy(pszFound, pszFilename, cchFilename + 1);
575 FILE *pFile = *ppFile = fopen(pszFound, "rb");
576 if (pFile)
577 return VINF_SUCCESS;
578
579 /*
580 * Walk the search path.
581 */
582 char *pszFreeMe = RTEnvDupEx(RTENV_DEFAULT, "VBOXDBG_IMAGE_PATH");
583 const char *psz = pszFreeMe ? pszFreeMe : ".";
584 while (*psz)
585 {
586 /* Skip leading blanks - no directories with leading spaces, thank you. */
587 while (RT_C_IS_BLANK(*psz))
588 psz++;
589
590 /* Fine the end of this element. */
591 const char *pszNext;
592 const char *pszEnd = strchr(psz, ';');
593 if (!pszEnd)
594 pszEnd = pszNext = strchr(psz, '\0');
595 else
596 pszNext = pszEnd + 1;
597 if (pszEnd != psz)
598 {
599 size_t const cch = pszEnd - psz;
600 if (cch + 1 + cchName < cchFound)
601 {
602 /** @todo RTPathCompose, RTPathComposeN(). This code isn't right
603 * for 'E:' on DOS systems. It may also create unwanted double slashes. */
604 memcpy(pszFound, psz, cch);
605 pszFound[cch] = '/';
606 memcpy(pszFound + cch + 1, pszName, cchName + 1);
607 *ppFile = pFile = fopen(pszFound, "rb");
608 if (pFile)
609 {
610 RTStrFree(pszFreeMe);
611 return VINF_SUCCESS;
612 }
613 }
614
615 /** @todo do a depth search using the specified path. */
616 }
617
618 /* advance */
619 psz = pszNext;
620 }
621
622 /* not found */
623 RTStrFree(pszFreeMe);
624 return VERR_OPEN_FAILED;
625}
626
627
628/**
629 * Load debug info, optionally related to a specific module.
630 *
631 * @returns VBox status.
632 * @param pVM VM Handle.
633 * @param pszFilename Path to the file containing the symbol information.
634 * This can be the executable image, a flat symbol file of some kind or stripped debug info.
635 * @param AddressDelta The value to add to the loaded symbols.
636 * @param pszName Short hand name for the module. If not related to a module specify NULL.
637 * @param ModuleAddress Address which the image is loaded at. This will be used to reference the module other places in the api.
638 * Ignored when pszName is NULL.
639 * @param cbImage Size of the image.
640 * Ignored when pszName is NULL.
641 */
642VMMR3DECL(int) DBGFR3ModuleLoad(PVM pVM, const char *pszFilename, RTGCUINTPTR AddressDelta, const char *pszName, RTGCUINTPTR ModuleAddress, unsigned cbImage)
643{
644 /*
645 * Lazy init.
646 */
647 if (!pVM->dbgf.s.fSymInited)
648 {
649 int rc = dbgfR3SymLazyInit(pVM);
650 if (RT_FAILURE(rc))
651 return rc;
652 }
653
654 /*
655 * Open the load file.
656 */
657 FILE *pFile = NULL;
658 char szFoundFile[RTPATH_MAX];
659 int rc = dbgfR3ModuleLocateAndOpen(pVM, pszFilename, szFoundFile, sizeof(szFoundFile), &pFile);
660 if (pFile)
661 {
662 /*
663 * Probe the file type.
664 */
665 SYMFILETYPE enmType = dbgfR3ModuleProbe(pFile);
666 if (enmType != SYMFILETYPE_UNKNOWN)
667 {
668 /*
669 * Add the module.
670 */
671 if (pszName)
672 {
673 #ifdef HAVE_DBGHELP
674 /** @todo arg! checkout the inserting of modules and then loading them again.... Or just the module representation.... */
675 DWORD64 ImageBase = SymLoadModule64(pVM, NULL, (char *)(void *)szFoundFile, (char *)(void *)pszName, ModuleAddress, cbImage);
676 if (!ImageBase)
677 ImageBase = SymLoadModule64(pVM, NULL, (char *)(void *)pszName, (char *)(void *)pszName, ModuleAddress, cbImage);
678 if (ImageBase)
679 {
680 AssertMsg(ModuleAddress == 0 || ModuleAddress == ImageBase, ("ModuleAddres=%RGv ImageBase=%llx\n", ModuleAddress, ImageBase));
681 ModuleAddress = ImageBase;
682 }
683 else
684 rc = win32Error(pVM);
685 #else
686 rc = VERR_NOT_IMPLEMENTED;
687 #endif
688 }
689 if (RT_SUCCESS(rc))
690 {
691 /*
692 * Seek to the start of the file.
693 */
694 rc = fseek(pFile, 0, SEEK_SET);
695 Assert(!rc);
696
697 /*
698 * Process the specific.
699 */
700 switch (enmType)
701 {
702 case SYMFILETYPE_LINUX_SYSTEM_MAP:
703 rc = dbgfR3LoadLinuxSystemMap(pVM, pFile, ModuleAddress, AddressDelta);
704 break;
705
706 case SYMFILETYPE_PDB:
707 case SYMFILETYPE_DBG:
708 case SYMFILETYPE_MZ:
709 #ifdef HAVE_DBGHELP
710 /* done it all above! */
711 break;
712 #endif
713 case SYMFILETYPE_LD_MAP:
714 case SYMFILETYPE_MS_MAP:
715 case SYMFILETYPE_OBJDUMP:
716 case SYMFILETYPE_ELF:
717 rc = VERR_NOT_SUPPORTED;
718 break;
719
720 default:
721 AssertFailed();
722 rc = VERR_INTERNAL_ERROR;
723 break;
724 } /* file switch. */
725 } /* module added successfully. */
726 } /* format identified */
727 else
728 rc = VERR_NOT_SUPPORTED;
729 /** @todo check for read errors */
730 fclose(pFile);
731 }
732 return rc;
733}
734
735
736/**
737 * Interface used by PDMR3LdrRelocate for telling us that a GC module has been relocated.
738 *
739 * @param pVM The VM handle.
740 * @param OldImageBase The old image base.
741 * @param NewImageBase The new image base.
742 * @param cbImage The image size.
743 * @param pszFilename The image filename.
744 * @param pszName The module name.
745 */
746VMMR3DECL(void) DBGFR3ModuleRelocate(PVM pVM, RTGCUINTPTR OldImageBase, RTGCUINTPTR NewImageBase, RTGCUINTPTR cbImage,
747 const char *pszFilename, const char *pszName)
748{
749#ifdef HAVE_DBGHELP
750 if (pVM->dbgf.s.fSymInited)
751 {
752 if (!SymUnloadModule64(pVM, OldImageBase))
753 Log(("SymUnloadModule64(,%RGv) failed, lasterr=%d\n", OldImageBase, GetLastError()));
754
755 DWORD ImageSize = (DWORD)cbImage; Assert(ImageSize == cbImage);
756 DWORD64 LoadedImageBase = SymLoadModule64(pVM, NULL, (char *)(void *)pszFilename, (char *)(void *)pszName, NewImageBase, ImageSize);
757 if (!LoadedImageBase)
758 Log(("SymLoadModule64(,,%s,,) -> lasterr=%d (relocate)\n", pszFilename, GetLastError()));
759 else
760 Log(("Reloaded debuginfo for %s - %s %llx\n", pszName, pszFilename, LoadedImageBase));
761 }
762#else
763
764#endif
765}
766
767
768/**
769 * Adds a symbol to the debug info manager.
770 *
771 * @returns VBox status.
772 * @param pVM VM Handle.
773 * @param ModuleAddress Module address. Use 0 if no module.
774 * @param SymbolAddress Symbol address
775 * @param cbSymbol Size of the symbol. Use 0 if info not available.
776 * @param pszSymbol Symbol name.
777 */
778VMMR3DECL(int) DBGFR3SymbolAdd(PVM pVM, RTGCUINTPTR ModuleAddress, RTGCUINTPTR SymbolAddress, RTUINT cbSymbol, const char *pszSymbol)
779{
780 /*
781 * Validate.
782 */
783 if (!pszSymbol || !*pszSymbol)
784 {
785 AssertMsgFailed(("No symbol name!\n"));
786 return VERR_INVALID_PARAMETER;
787 }
788
789 /*
790 * Lazy init.
791 */
792 if (!pVM->dbgf.s.fSymInited)
793 {
794 int rc = dbgfR3SymLazyInit(pVM);
795 if (RT_FAILURE(rc))
796 return rc;
797 }
798
799#ifdef HAVE_DBGHELP
800 if (SymAddSymbol(pVM, ModuleAddress, (char *)(void *)pszSymbol, SymbolAddress, cbSymbol, 0))
801 return VINF_SUCCESS;
802 return win32Error(pVM);
803#else
804 /** @todo module lookup. */
805 return dbgfR3SymbolInsert(pVM, pszSymbol, SymbolAddress, cbSymbol, NULL);
806#endif
807}
808
809
810/**
811 * Find symbol by address (nearest).
812 *
813 * @returns VBox status.
814 * @param pVM VM handle.
815 * @param Address Address.
816 * @param poffDisplacement Where to store the symbol displacement from Address.
817 * @param pSymbol Where to store the symbol info.
818 */
819VMMR3DECL(int) DBGFR3SymbolByAddr(PVM pVM, RTGCUINTPTR Address, PRTGCINTPTR poffDisplacement, PDBGFSYMBOL pSymbol)
820{
821 /*
822 * Lazy init.
823 */
824 if (!pVM->dbgf.s.fSymInited)
825 {
826 int rc = dbgfR3SymLazyInit(pVM);
827 if (RT_FAILURE(rc))
828 return rc;
829 }
830
831 /*
832 * Look it up.
833 */
834#ifdef HAVE_DBGHELP
835 char achBuffer[sizeof(IMAGEHLP_SYMBOL64) + DBGF_SYMBOL_NAME_LENGTH * sizeof(TCHAR) + sizeof(ULONG64)];
836 PIMAGEHLP_SYMBOL64 pSym = (PIMAGEHLP_SYMBOL64)&achBuffer[0];
837 pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
838 pSym->MaxNameLength = DBGF_SYMBOL_NAME_LENGTH;
839
840 if (SymGetSymFromAddr64(pVM, Address, (PDWORD64)poffDisplacement, pSym))
841 {
842 pSymbol->Value = (RTGCUINTPTR)pSym->Address;
843 pSymbol->cb = pSym->Size;
844 pSymbol->fFlags = pSym->Flags;
845 strcpy(pSymbol->szName, pSym->Name);
846 return VINF_SUCCESS;
847 }
848 //return win32Error(pVM);
849
850#else
851
852 PDBGFSYM pSym = dbgfR3SymbolGetAddr(pVM, Address);
853 if (pSym)
854 {
855 pSymbol->Value = pSym->Core.Key;
856 pSymbol->cb = pSym->Core.KeyLast - pSym->Core.Key + 1;
857 pSymbol->fFlags = 0;
858 pSymbol->szName[0] = '\0';
859 strncat(pSymbol->szName, pSym->szName, sizeof(pSymbol->szName) - 1);
860 if (poffDisplacement)
861 *poffDisplacement = Address - pSymbol->Value;
862 return VINF_SUCCESS;
863 }
864
865#endif
866
867 /*
868 * Try PDM.
869 */
870 if (MMHyperIsInsideArea(pVM, Address))
871 {
872 char szModName[64];
873 RTRCPTR RCPtrMod;
874 char szNearSym1[260];
875 RTRCPTR RCPtrNearSym1;
876 char szNearSym2[260];
877 RTRCPTR RCPtrNearSym2;
878 int rc = PDMR3LdrQueryRCModFromPC(pVM, Address,
879 &szModName[0], sizeof(szModName), &RCPtrMod,
880 &szNearSym1[0], sizeof(szNearSym1), &RCPtrNearSym1,
881 &szNearSym2[0], sizeof(szNearSym2), &RCPtrNearSym2);
882 if (RT_SUCCESS(rc) && szNearSym1[0])
883 {
884 pSymbol->Value = RCPtrNearSym1;
885 pSymbol->cb = RCPtrNearSym2 > RCPtrNearSym1 ? RCPtrNearSym2 - RCPtrNearSym1 : 0;
886 pSymbol->fFlags = 0;
887 pSymbol->szName[0] = '\0';
888 strncat(pSymbol->szName, szNearSym1, sizeof(pSymbol->szName) - 1);
889 if (poffDisplacement)
890 *poffDisplacement = Address - pSymbol->Value;
891 return VINF_SUCCESS;
892 }
893 }
894
895 return VERR_SYMBOL_NOT_FOUND;
896}
897
898
899/**
900 * Find symbol by name (first).
901 *
902 * @returns VBox status.
903 * @param pVM VM handle.
904 * @param pszSymbol Symbol name.
905 * @param pSymbol Where to store the symbol info.
906 */
907VMMR3DECL(int) DBGFR3SymbolByName(PVM pVM, const char *pszSymbol, PDBGFSYMBOL pSymbol)
908{
909 /*
910 * Lazy init.
911 */
912 if (!pVM->dbgf.s.fSymInited)
913 {
914 int rc = dbgfR3SymLazyInit(pVM);
915 if (RT_FAILURE(rc))
916 return rc;
917 }
918
919 /*
920 * Look it up.
921 */
922#ifdef HAVE_DBGHELP
923 char achBuffer[sizeof(IMAGEHLP_SYMBOL64) + DBGF_SYMBOL_NAME_LENGTH * sizeof(TCHAR) + sizeof(ULONG64)];
924 PIMAGEHLP_SYMBOL64 pSym = (PIMAGEHLP_SYMBOL64)&achBuffer[0];
925 pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
926 pSym->MaxNameLength = DBGF_SYMBOL_NAME_LENGTH;
927
928 if (SymGetSymFromName64(pVM, (char *)(void *)pszSymbol, pSym))
929 {
930 pSymbol->Value = (RTGCUINTPTR)pSym->Address;
931 pSymbol->cb = pSym->Size;
932 pSymbol->fFlags = pSym->Flags;
933 strcpy(pSymbol->szName, pSym->Name);
934 return VINF_SUCCESS;
935 }
936 return win32Error(pVM);
937#else
938
939 PDBGFSYM pSym = dbgfR3SymbolGetName(pVM, pszSymbol);
940 if (pSym)
941 {
942 pSymbol->Value = pSym->Core.Key;
943 pSymbol->cb = pSym->Core.KeyLast - pSym->Core.Key + 1;
944 pSymbol->fFlags = 0;
945 pSymbol->szName[0] = '\0';
946 strncat(pSymbol->szName, pSym->szName, sizeof(pSymbol->szName) - 1);
947 return VINF_SUCCESS;
948 }
949
950 return VERR_SYMBOL_NOT_FOUND;
951#endif
952}
953
954
955/**
956 * Find line by address (nearest).
957 *
958 * @returns VBox status.
959 * @param pVM VM handle.
960 * @param Address Address.
961 * @param poffDisplacement Where to store the line displacement from Address.
962 * @param pLine Where to store the line info.
963 */
964VMMR3DECL(int) DBGFR3LineByAddr(PVM pVM, RTGCUINTPTR Address, PRTGCINTPTR poffDisplacement, PDBGFLINE pLine)
965{
966 /*
967 * Lazy init.
968 */
969 if (!pVM->dbgf.s.fSymInited)
970 {
971 int rc = dbgfR3SymLazyInit(pVM);
972 if (RT_FAILURE(rc))
973 return rc;
974 }
975
976 /*
977 * Look it up.
978 */
979#ifdef HAVE_DBGHELP
980 IMAGEHLP_LINE64 Line = {0};
981 DWORD off = 0;
982 Line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
983 if (SymGetLineFromAddr64(pVM, Address, &off, &Line))
984 {
985 if (poffDisplacement)
986 *poffDisplacement = (long)off;
987 pLine->Address = (RTGCUINTPTR)Line.Address;
988 pLine->uLineNo = Line.LineNumber;
989 pLine->szFilename[0] = '\0';
990 strncat(pLine->szFilename, Line.FileName, sizeof(pLine->szFilename));
991 return VINF_SUCCESS;
992 }
993 return win32Error(pVM);
994#else
995 return VERR_NOT_IMPLEMENTED;
996#endif
997}
998
999
1000/**
1001 * Duplicates a line.
1002 *
1003 * @returns VBox status code.
1004 * @param pVM The VM handle.
1005 * @param pLine The line to duplicate.
1006 */
1007static PDBGFLINE dbgfR3LineDup(PVM pVM, PCDBGFLINE pLine)
1008{
1009 size_t cb = strlen(pLine->szFilename) + RT_OFFSETOF(DBGFLINE, szFilename[1]);
1010 PDBGFLINE pDup = (PDBGFLINE)MMR3HeapAlloc(pVM, MM_TAG_DBGF_LINE_DUP, cb);
1011 if (pDup)
1012 memcpy(pDup, pLine, cb);
1013 return pDup;
1014}
1015
1016
1017/**
1018 * Find line by address (nearest), allocate return buffer.
1019 *
1020 * @returns Pointer to the line. Must be freed using DBGFR3LineFree().
1021 * @returns NULL if the line was not found or if we're out of memory.
1022 * @param pVM VM handle.
1023 * @param Address Address.
1024 * @param poffDisplacement Where to store the line displacement from Address.
1025 */
1026VMMR3DECL(PDBGFLINE) DBGFR3LineByAddrAlloc(PVM pVM, RTGCUINTPTR Address, PRTGCINTPTR poffDisplacement)
1027{
1028 DBGFLINE Line;
1029 int rc = DBGFR3LineByAddr(pVM, Address, poffDisplacement, &Line);
1030 if (RT_FAILURE(rc))
1031 return NULL;
1032 return dbgfR3LineDup(pVM, &Line);
1033}
1034
1035
1036/**
1037 * Frees a line returned by DBGFR3LineByAddressAlloc().
1038 *
1039 * @param pLine Pointer to the line.
1040 */
1041VMMR3DECL(void) DBGFR3LineFree(PDBGFLINE pLine)
1042{
1043 if (pLine)
1044 MMR3HeapFree(pLine);
1045}
1046
1047
1048#ifdef HAVE_DBGHELP
1049
1050//static BOOL CALLBACK win32EnumModulesCallback(PSTR ModuleName, DWORD64 BaseOfDll, PVOID UserContext)
1051//{
1052// Log(("dbg: module: %08llx %s\n", ModuleName, BaseOfDll));
1053// return TRUE;
1054//}
1055
1056static int win32Error(PVM pVM)
1057{
1058 int rc = GetLastError();
1059 Log(("Lasterror=%d\n", rc));
1060
1061 //SymEnumerateModules64(pVM, win32EnumModulesCallback, NULL);
1062
1063 return VERR_GENERAL_FAILURE;
1064}
1065#endif
1066
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette