VirtualBox

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

Last change on this file since 19190 was 19190, checked in by vboxsync, 16 years ago

Oops, didn't mean to enable it, just make it build again.

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