VirtualBox

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

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

EFI: typo (uninitialized variable)

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