VirtualBox

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

Last change on this file since 7806 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

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