VirtualBox

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

Last change on this file since 26550 was 26152, checked in by vboxsync, 15 years ago

VMM: pdm.h and @copydoc cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 33.5 KB
Line 
1/* $Id: DBGFSym.cpp 26152 2010-02-02 16:00:35Z 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, 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 <VBox/mm.h>
36#include <VBox/pdmapi.h>
37#include "DBGFInternal.h"
38#include <VBox/vm.h>
39#include <VBox/err.h>
40#include <VBox/log.h>
41
42#include <iprt/assert.h>
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 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 && RT_C_IS_SPACE(*psz))
233 psz++;
234 char *psz2 = strchr(psz, '\0') - 1;
235 while (psz2 >= psz && RT_C_IS_SPACE(*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 DWORD64 LoadedImageBase = SymLoadModule64(pVM, NULL, (char *)(void *)pszFilename,
388 (char *)(void *)pszName, ImageBase, (DWORD)cbImage);
389 if (!LoadedImageBase)
390 Log(("SymLoadModule64(,,%s,,) -> lasterr=%d\n", pszFilename, GetLastError()));
391 else
392 Log(("Loaded debuginfo for %s - %s %llx\n", pszName, pszFilename, LoadedImageBase));
393
394 return VINF_SUCCESS;
395}
396#endif
397
398
399/**
400 * Terminate the debug info repository for the specified VM.
401 *
402 * @returns VBox status.
403 * @param pVM VM Handle.
404 */
405int dbgfR3SymTerm(PVM pVM)
406{
407#ifdef HAVE_DBGHELP
408 if (pVM->dbgf.s.fSymInited)
409 SymCleanup(pVM);
410 pVM->dbgf.s.fSymInited = false;
411 return VINF_SUCCESS;
412#else
413 pVM->dbgf.s.SymbolTree = 0; /* MM cleans up allocations */
414 pVM->dbgf.s.fSymInited = false;
415 return VINF_SUCCESS;
416#endif
417}
418
419
420/** Symbol file type.. */
421typedef enum SYMFILETYPE
422{
423 SYMFILETYPE_UNKNOWN,
424 SYMFILETYPE_LD_MAP,
425 SYMFILETYPE_MS_MAP,
426 SYMFILETYPE_OBJDUMP,
427 SYMFILETYPE_LINUX_SYSTEM_MAP,
428 SYMFILETYPE_PDB,
429 SYMFILETYPE_DBG,
430 SYMFILETYPE_MZ,
431 SYMFILETYPE_ELF
432} SYMFILETYPE, *PSYMFILETYPE;
433
434
435
436/**
437 * Probe the type of a symbol information file.
438 *
439 * @returns The file type.
440 * @param pFile File handle.
441 */
442SYMFILETYPE dbgfR3ModuleProbe(FILE *pFile)
443{
444 char szHead[4096];
445 size_t cchHead = fread(szHead, 1, sizeof(szHead) - 1, pFile);
446 if (cchHead > 0)
447 {
448 szHead[cchHead] = '\0';
449 if (strstr(szHead, "Preferred load address is"))
450 return SYMFILETYPE_MS_MAP;
451
452 if ( strstr(szHead, "Archive member included because of")
453 || strstr(szHead, "Memory Configuration")
454 || strstr(szHead, "Linker script and memory map"))
455 return SYMFILETYPE_LD_MAP;
456
457 if ( RT_C_IS_XDIGIT(szHead[0])
458 && RT_C_IS_XDIGIT(szHead[1])
459 && RT_C_IS_XDIGIT(szHead[2])
460 && RT_C_IS_XDIGIT(szHead[3])
461 && RT_C_IS_XDIGIT(szHead[4])
462 && RT_C_IS_XDIGIT(szHead[5])
463 && RT_C_IS_XDIGIT(szHead[6])
464 && RT_C_IS_XDIGIT(szHead[7])
465 && szHead[8] == ' '
466 && RT_C_IS_ALPHA(szHead[9])
467 && szHead[10] == ' '
468 && (RT_C_IS_ALPHA(szHead[11]) || szHead[11] == '_' || szHead[11] == '$')
469 )
470 return SYMFILETYPE_LINUX_SYSTEM_MAP;
471
472 if ( RT_C_IS_XDIGIT(szHead[0])
473 && RT_C_IS_XDIGIT(szHead[1])
474 && RT_C_IS_XDIGIT(szHead[2])
475 && RT_C_IS_XDIGIT(szHead[3])
476 && RT_C_IS_XDIGIT(szHead[4])
477 && RT_C_IS_XDIGIT(szHead[5])
478 && RT_C_IS_XDIGIT(szHead[6])
479 && RT_C_IS_XDIGIT(szHead[7])
480 && RT_C_IS_XDIGIT(szHead[8])
481 && RT_C_IS_XDIGIT(szHead[9])
482 && RT_C_IS_XDIGIT(szHead[10])
483 && RT_C_IS_XDIGIT(szHead[11])
484 && RT_C_IS_XDIGIT(szHead[12])
485 && RT_C_IS_XDIGIT(szHead[13])
486 && RT_C_IS_XDIGIT(szHead[14])
487 && RT_C_IS_XDIGIT(szHead[15])
488 && szHead[16] == ' '
489 && RT_C_IS_ALPHA(szHead[17])
490 && szHead[18] == ' '
491 && (RT_C_IS_ALPHA(szHead[19]) || szHead[19] == '_' || szHead[19] == '$')
492 )
493 return SYMFILETYPE_LINUX_SYSTEM_MAP;
494
495 if (strstr(szHead, "Microsoft C/C++ MSF") == szHead)
496 return SYMFILETYPE_PDB;
497
498 if (strstr(szHead, "ELF") == szHead + 1)
499 return SYMFILETYPE_ELF;
500
501 if ( strstr(szHead, "MZ") == szHead
502 || strstr(szHead, "PE") == szHead
503 || strstr(szHead, "LE") == szHead
504 || strstr(szHead, "LX") == szHead
505 || strstr(szHead, "NE") == szHead)
506 return SYMFILETYPE_MZ;
507
508
509 if (strstr(szHead, "file format"))
510 return SYMFILETYPE_OBJDUMP;
511 }
512
513 return SYMFILETYPE_UNKNOWN;
514}
515
516
517static int dbgfR3LoadLinuxSystemMap(PVM pVM, FILE *pFile, RTGCUINTPTR ModuleAddress, RTGCUINTPTR AddressDelta)
518{
519 char szLine[4096];
520 while (fgets(szLine, sizeof(szLine), pFile))
521 {
522 /* parse the line: <address> <type> <name> */
523 const char *psz = dbgfR3Strip(szLine);
524 char *pszEnd = NULL;
525 uint64_t u64Address;
526 int rc = RTStrToUInt64Ex(psz, &pszEnd, 16, &u64Address);
527 RTGCUINTPTR Address = u64Address;
528 if ( RT_SUCCESS(rc)
529 && (*pszEnd == ' ' || *pszEnd == '\t')
530 && Address == u64Address
531 && u64Address != 0
532 && u64Address != (RTGCUINTPTR)~0)
533 {
534 pszEnd++;
535 if ( RT_C_IS_ALPHA(*pszEnd)
536 && (pszEnd[1] == ' ' || pszEnd[1] == '\t'))
537 {
538 psz = dbgfR3Strip(pszEnd + 2);
539 if (*psz)
540 {
541 int rc2 = DBGFR3SymbolAdd(pVM, ModuleAddress, Address + AddressDelta, 0, psz);
542 if (RT_FAILURE(rc2))
543 Log2(("DBGFR3SymbolAdd(,, %RGv, 0, '%s') -> %Rrc\n", Address, psz, rc2));
544 }
545 }
546 }
547 }
548 return VINF_SUCCESS;
549}
550
551/**
552 * Tries to open the file using the image search paths.
553 *
554 * This is currently a quick hack and the only way to specifying the path is by setting
555 * VBOXDBG_IMAGE_PATH in the environment. It uses semicolon as separator everywhere.
556 *
557 * @returns VBox status code.
558 * @param pVM The VM handle.
559 * @param pszFilename The name of the file to locate and open.
560 * @param pszFound Where to return the actual filename.
561 * @param cchFound The buffer size.
562 * @param ppFile Where to return the opened file.
563 */
564int dbgfR3ModuleLocateAndOpen(PVM pVM, const char *pszFilename, char *pszFound, size_t cchFound, FILE **ppFile)
565{
566 /* Check the filename length. */
567 size_t const cchFilename = strlen(pszFilename);
568 if (cchFilename >= cchFound)
569 return VERR_FILENAME_TOO_LONG;
570 const char *pszName = RTPathFilename(pszFilename);
571 if (!pszName)
572 return VERR_IS_A_DIRECTORY;
573 size_t const cchName = strlen(pszName);
574
575 /*
576 * Try default location first.
577 */
578 memcpy(pszFound, pszFilename, cchFilename + 1);
579 FILE *pFile = *ppFile = fopen(pszFound, "rb");
580 if (pFile)
581 return VINF_SUCCESS;
582
583 /*
584 * Walk the search path.
585 */
586 char *pszFreeMe = RTEnvDupEx(RTENV_DEFAULT, "VBOXDBG_IMAGE_PATH");
587 const char *psz = pszFreeMe ? pszFreeMe : ".";
588 while (*psz)
589 {
590 /* Skip leading blanks - no directories with leading spaces, thank you. */
591 while (RT_C_IS_BLANK(*psz))
592 psz++;
593
594 /* Fine the end of this element. */
595 const char *pszNext;
596 const char *pszEnd = strchr(psz, ';');
597 if (!pszEnd)
598 pszEnd = pszNext = strchr(psz, '\0');
599 else
600 pszNext = pszEnd + 1;
601 if (pszEnd != psz)
602 {
603 size_t const cch = pszEnd - psz;
604 if (cch + 1 + cchName < cchFound)
605 {
606 /** @todo RTPathCompose, RTPathComposeN(). This code isn't right
607 * for 'E:' on DOS systems. It may also create unwanted double slashes. */
608 memcpy(pszFound, psz, cch);
609 pszFound[cch] = '/';
610 memcpy(pszFound + cch + 1, pszName, cchName + 1);
611 *ppFile = pFile = fopen(pszFound, "rb");
612 if (pFile)
613 {
614 RTStrFree(pszFreeMe);
615 return VINF_SUCCESS;
616 }
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 RTStrFree(pszFreeMe);
628 return VERR_OPEN_FAILED;
629}
630
631
632/**
633 * Load debug info, optionally related to a specific module.
634 *
635 * @returns VBox status.
636 * @param pVM VM Handle.
637 * @param pszFilename Path to the file containing the symbol information.
638 * This can be the executable image, a flat symbol file of some kind or stripped debug info.
639 * @param AddressDelta The value to add to the loaded symbols.
640 * @param pszName Short hand name for the module. If not related to a module specify NULL.
641 * @param ModuleAddress Address which the image is loaded at. This will be used to reference the module other places in the api.
642 * Ignored when pszName is NULL.
643 * @param cbImage Size of the image.
644 * Ignored when pszName is NULL.
645 */
646VMMR3DECL(int) DBGFR3ModuleLoad(PVM pVM, const char *pszFilename, RTGCUINTPTR AddressDelta, const char *pszName, RTGCUINTPTR ModuleAddress, unsigned cbImage)
647{
648 /*
649 * Lazy init.
650 */
651 if (!pVM->dbgf.s.fSymInited)
652 {
653 int rc = dbgfR3SymLazyInit(pVM);
654 if (RT_FAILURE(rc))
655 return rc;
656 }
657
658 /*
659 * Open the load file.
660 */
661 FILE *pFile = NULL;
662 char szFoundFile[RTPATH_MAX];
663 int rc = dbgfR3ModuleLocateAndOpen(pVM, pszFilename, szFoundFile, sizeof(szFoundFile), &pFile);
664 if (pFile)
665 {
666 /*
667 * Probe the file type.
668 */
669 SYMFILETYPE enmType = dbgfR3ModuleProbe(pFile);
670 if (enmType != SYMFILETYPE_UNKNOWN)
671 {
672 /*
673 * Add the module.
674 */
675 if (pszName)
676 {
677 #ifdef HAVE_DBGHELP
678 /** @todo arg! checkout the inserting of modules and then loading them again.... Or just the module representation.... */
679 DWORD64 ImageBase = SymLoadModule64(pVM, NULL, (char *)(void *)szFoundFile, (char *)(void *)pszName, ModuleAddress, cbImage);
680 if (!ImageBase)
681 ImageBase = SymLoadModule64(pVM, NULL, (char *)(void *)pszName, (char *)(void *)pszName, ModuleAddress, cbImage);
682 if (ImageBase)
683 {
684 AssertMsg(ModuleAddress == 0 || ModuleAddress == ImageBase, ("ModuleAddres=%RGv ImageBase=%llx\n", ModuleAddress, ImageBase));
685 ModuleAddress = ImageBase;
686 }
687 else
688 rc = win32Error(pVM);
689 #else
690 rc = VERR_NOT_IMPLEMENTED;
691 #endif
692 }
693 if (RT_SUCCESS(rc))
694 {
695 /*
696 * Seek to the start of the file.
697 */
698 rc = fseek(pFile, 0, SEEK_SET);
699 Assert(!rc);
700
701 /*
702 * Process the specific.
703 */
704 switch (enmType)
705 {
706 case SYMFILETYPE_LINUX_SYSTEM_MAP:
707 rc = dbgfR3LoadLinuxSystemMap(pVM, pFile, ModuleAddress, AddressDelta);
708 break;
709
710 case SYMFILETYPE_PDB:
711 case SYMFILETYPE_DBG:
712 case SYMFILETYPE_MZ:
713 #ifdef HAVE_DBGHELP
714 /* done it all above! */
715 break;
716 #endif
717 case SYMFILETYPE_LD_MAP:
718 case SYMFILETYPE_MS_MAP:
719 case SYMFILETYPE_OBJDUMP:
720 case SYMFILETYPE_ELF:
721 rc = VERR_NOT_SUPPORTED;
722 break;
723
724 default:
725 AssertFailed();
726 rc = VERR_INTERNAL_ERROR;
727 break;
728 } /* file switch. */
729 } /* module added successfully. */
730 } /* format identified */
731 else
732 rc = VERR_NOT_SUPPORTED;
733 /** @todo check for read errors */
734 fclose(pFile);
735 }
736 return rc;
737}
738
739
740/**
741 * Interface used by PDMR3LdrRelocate for telling us that a GC module has been relocated.
742 *
743 * @param pVM The VM handle.
744 * @param OldImageBase The old image base.
745 * @param NewImageBase The new image base.
746 * @param cbImage The image size.
747 * @param pszFilename The image filename.
748 * @param pszName The module name.
749 */
750VMMR3DECL(void) DBGFR3ModuleRelocate(PVM pVM, RTGCUINTPTR OldImageBase, RTGCUINTPTR NewImageBase, RTGCUINTPTR cbImage,
751 const char *pszFilename, const char *pszName)
752{
753#ifdef HAVE_DBGHELP
754 if (pVM->dbgf.s.fSymInited)
755 {
756 if (!SymUnloadModule64(pVM, OldImageBase))
757 Log(("SymUnloadModule64(,%RGv) failed, lasterr=%d\n", OldImageBase, GetLastError()));
758
759 DWORD ImageSize = (DWORD)cbImage; Assert(ImageSize == cbImage);
760 DWORD64 LoadedImageBase = SymLoadModule64(pVM, NULL, (char *)(void *)pszFilename, (char *)(void *)pszName, NewImageBase, ImageSize);
761 if (!LoadedImageBase)
762 Log(("SymLoadModule64(,,%s,,) -> lasterr=%d (relocate)\n", pszFilename, GetLastError()));
763 else
764 Log(("Reloaded debuginfo for %s - %s %llx\n", pszName, pszFilename, LoadedImageBase));
765 }
766#else
767
768#endif
769}
770
771
772/**
773 * Adds a symbol to the debug info manager.
774 *
775 * @returns VBox status.
776 * @param pVM VM Handle.
777 * @param ModuleAddress Module address. Use 0 if no module.
778 * @param SymbolAddress Symbol address
779 * @param cbSymbol Size of the symbol. Use 0 if info not available.
780 * @param pszSymbol Symbol name.
781 */
782VMMR3DECL(int) DBGFR3SymbolAdd(PVM pVM, RTGCUINTPTR ModuleAddress, RTGCUINTPTR SymbolAddress, RTUINT cbSymbol, const char *pszSymbol)
783{
784 /*
785 * Validate.
786 */
787 if (!pszSymbol || !*pszSymbol)
788 {
789 AssertMsgFailed(("No symbol name!\n"));
790 return VERR_INVALID_PARAMETER;
791 }
792
793 /*
794 * Lazy init.
795 */
796 if (!pVM->dbgf.s.fSymInited)
797 {
798 int rc = dbgfR3SymLazyInit(pVM);
799 if (RT_FAILURE(rc))
800 return rc;
801 }
802
803#ifdef HAVE_DBGHELP
804 if (SymAddSymbol(pVM, ModuleAddress, (char *)(void *)pszSymbol, SymbolAddress, cbSymbol, 0))
805 return VINF_SUCCESS;
806 return win32Error(pVM);
807#else
808 /** @todo module lookup. */
809 return dbgfR3SymbolInsert(pVM, pszSymbol, SymbolAddress, cbSymbol, NULL);
810#endif
811}
812
813
814/**
815 * Find symbol by address (nearest).
816 *
817 * @returns VBox status.
818 * @param pVM VM handle.
819 * @param Address Address.
820 * @param poffDisplacement Where to store the symbol displacement from Address.
821 * @param pSymbol Where to store the symbol info.
822 */
823VMMR3DECL(int) DBGFR3SymbolByAddr(PVM pVM, RTGCUINTPTR Address, PRTGCINTPTR poffDisplacement, PDBGFSYMBOL pSymbol)
824{
825 /*
826 * Lazy init.
827 */
828 if (!pVM->dbgf.s.fSymInited)
829 {
830 int rc = dbgfR3SymLazyInit(pVM);
831 if (RT_FAILURE(rc))
832 return rc;
833 }
834
835 /*
836 * Look it up.
837 */
838#ifdef HAVE_DBGHELP
839 char achBuffer[sizeof(IMAGEHLP_SYMBOL64) + DBGF_SYMBOL_NAME_LENGTH * sizeof(TCHAR) + sizeof(ULONG64)];
840 PIMAGEHLP_SYMBOL64 pSym = (PIMAGEHLP_SYMBOL64)&achBuffer[0];
841 pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
842 pSym->MaxNameLength = DBGF_SYMBOL_NAME_LENGTH;
843
844 if (SymGetSymFromAddr64(pVM, Address, (PDWORD64)poffDisplacement, pSym))
845 {
846 pSymbol->Value = (RTGCUINTPTR)pSym->Address;
847 pSymbol->cb = pSym->Size;
848 pSymbol->fFlags = pSym->Flags;
849 strcpy(pSymbol->szName, pSym->Name);
850 return VINF_SUCCESS;
851 }
852 //return win32Error(pVM);
853
854#else
855
856 PDBGFSYM pSym = dbgfR3SymbolGetAddr(pVM, Address);
857 if (pSym)
858 {
859 pSymbol->Value = pSym->Core.Key;
860 pSymbol->cb = pSym->Core.KeyLast - pSym->Core.Key + 1;
861 pSymbol->fFlags = 0;
862 pSymbol->szName[0] = '\0';
863 strncat(pSymbol->szName, pSym->szName, sizeof(pSymbol->szName) - 1);
864 if (poffDisplacement)
865 *poffDisplacement = Address - pSymbol->Value;
866 return VINF_SUCCESS;
867 }
868
869#endif
870
871 /*
872 * Try PDM.
873 */
874 if (MMHyperIsInsideArea(pVM, Address))
875 {
876 char szModName[64];
877 RTRCPTR RCPtrMod;
878 char szNearSym1[260];
879 RTRCPTR RCPtrNearSym1;
880 char szNearSym2[260];
881 RTRCPTR RCPtrNearSym2;
882 int rc = PDMR3LdrQueryRCModFromPC(pVM, Address,
883 &szModName[0], sizeof(szModName), &RCPtrMod,
884 &szNearSym1[0], sizeof(szNearSym1), &RCPtrNearSym1,
885 &szNearSym2[0], sizeof(szNearSym2), &RCPtrNearSym2);
886 if (RT_SUCCESS(rc) && szNearSym1[0])
887 {
888 pSymbol->Value = RCPtrNearSym1;
889 pSymbol->cb = RCPtrNearSym2 > RCPtrNearSym1 ? RCPtrNearSym2 - RCPtrNearSym1 : 0;
890 pSymbol->fFlags = 0;
891 pSymbol->szName[0] = '\0';
892 strncat(pSymbol->szName, szNearSym1, sizeof(pSymbol->szName) - 1);
893 if (poffDisplacement)
894 *poffDisplacement = Address - pSymbol->Value;
895 return VINF_SUCCESS;
896 }
897 }
898
899 return VERR_SYMBOL_NOT_FOUND;
900}
901
902
903/**
904 * Find symbol by name (first).
905 *
906 * @returns VBox status.
907 * @param pVM VM handle.
908 * @param pszSymbol Symbol name.
909 * @param pSymbol Where to store the symbol info.
910 */
911VMMR3DECL(int) DBGFR3SymbolByName(PVM pVM, const char *pszSymbol, PDBGFSYMBOL pSymbol)
912{
913 /*
914 * Lazy init.
915 */
916 if (!pVM->dbgf.s.fSymInited)
917 {
918 int rc = dbgfR3SymLazyInit(pVM);
919 if (RT_FAILURE(rc))
920 return rc;
921 }
922
923 /*
924 * Look it up.
925 */
926#ifdef HAVE_DBGHELP
927 char achBuffer[sizeof(IMAGEHLP_SYMBOL64) + DBGF_SYMBOL_NAME_LENGTH * sizeof(TCHAR) + sizeof(ULONG64)];
928 PIMAGEHLP_SYMBOL64 pSym = (PIMAGEHLP_SYMBOL64)&achBuffer[0];
929 pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
930 pSym->MaxNameLength = DBGF_SYMBOL_NAME_LENGTH;
931
932 if (SymGetSymFromName64(pVM, (char *)(void *)pszSymbol, pSym))
933 {
934 pSymbol->Value = (RTGCUINTPTR)pSym->Address;
935 pSymbol->cb = pSym->Size;
936 pSymbol->fFlags = pSym->Flags;
937 strcpy(pSymbol->szName, pSym->Name);
938 return VINF_SUCCESS;
939 }
940 return win32Error(pVM);
941#else
942
943 PDBGFSYM pSym = dbgfR3SymbolGetName(pVM, pszSymbol);
944 if (pSym)
945 {
946 pSymbol->Value = pSym->Core.Key;
947 pSymbol->cb = pSym->Core.KeyLast - pSym->Core.Key + 1;
948 pSymbol->fFlags = 0;
949 pSymbol->szName[0] = '\0';
950 strncat(pSymbol->szName, pSym->szName, sizeof(pSymbol->szName) - 1);
951 return VINF_SUCCESS;
952 }
953
954 return VERR_SYMBOL_NOT_FOUND;
955#endif
956}
957
958
959/**
960 * Find line by address (nearest).
961 *
962 * @returns VBox status.
963 * @param pVM VM handle.
964 * @param Address Address.
965 * @param poffDisplacement Where to store the line displacement from Address.
966 * @param pLine Where to store the line info.
967 */
968VMMR3DECL(int) DBGFR3LineByAddr(PVM pVM, RTGCUINTPTR Address, PRTGCINTPTR poffDisplacement, PDBGFLINE pLine)
969{
970 /*
971 * Lazy init.
972 */
973 if (!pVM->dbgf.s.fSymInited)
974 {
975 int rc = dbgfR3SymLazyInit(pVM);
976 if (RT_FAILURE(rc))
977 return rc;
978 }
979
980 /*
981 * Look it up.
982 */
983#ifdef HAVE_DBGHELP
984 IMAGEHLP_LINE64 Line = {0};
985 DWORD off = 0;
986 Line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
987 if (SymGetLineFromAddr64(pVM, Address, &off, &Line))
988 {
989 if (poffDisplacement)
990 *poffDisplacement = (long)off;
991 pLine->Address = (RTGCUINTPTR)Line.Address;
992 pLine->uLineNo = Line.LineNumber;
993 pLine->szFilename[0] = '\0';
994 strncat(pLine->szFilename, Line.FileName, sizeof(pLine->szFilename));
995 return VINF_SUCCESS;
996 }
997 return win32Error(pVM);
998#else
999 return VERR_NOT_IMPLEMENTED;
1000#endif
1001}
1002
1003
1004/**
1005 * Duplicates a line.
1006 *
1007 * @returns VBox status code.
1008 * @param pVM The VM handle.
1009 * @param pLine The line to duplicate.
1010 */
1011static PDBGFLINE dbgfR3LineDup(PVM pVM, PCDBGFLINE pLine)
1012{
1013 size_t cb = strlen(pLine->szFilename) + RT_OFFSETOF(DBGFLINE, szFilename[1]);
1014 PDBGFLINE pDup = (PDBGFLINE)MMR3HeapAlloc(pVM, MM_TAG_DBGF_LINE_DUP, cb);
1015 if (pDup)
1016 memcpy(pDup, pLine, cb);
1017 return pDup;
1018}
1019
1020
1021/**
1022 * Find line by address (nearest), allocate return buffer.
1023 *
1024 * @returns Pointer to the line. Must be freed using DBGFR3LineFree().
1025 * @returns NULL if the line was not found or if we're out of memory.
1026 * @param pVM VM handle.
1027 * @param Address Address.
1028 * @param poffDisplacement Where to store the line displacement from Address.
1029 */
1030VMMR3DECL(PDBGFLINE) DBGFR3LineByAddrAlloc(PVM pVM, RTGCUINTPTR Address, PRTGCINTPTR poffDisplacement)
1031{
1032 DBGFLINE Line;
1033 int rc = DBGFR3LineByAddr(pVM, Address, poffDisplacement, &Line);
1034 if (RT_FAILURE(rc))
1035 return NULL;
1036 return dbgfR3LineDup(pVM, &Line);
1037}
1038
1039
1040/**
1041 * Frees a line returned by DBGFR3LineByAddressAlloc().
1042 *
1043 * @param pLine Pointer to the line.
1044 */
1045VMMR3DECL(void) DBGFR3LineFree(PDBGFLINE pLine)
1046{
1047 if (pLine)
1048 MMR3HeapFree(pLine);
1049}
1050
1051
1052#ifdef HAVE_DBGHELP
1053
1054//static BOOL CALLBACK win32EnumModulesCallback(PSTR ModuleName, DWORD64 BaseOfDll, PVOID UserContext)
1055//{
1056// Log(("dbg: module: %08llx %s\n", ModuleName, BaseOfDll));
1057// return TRUE;
1058//}
1059
1060static int win32Error(PVM pVM)
1061{
1062 int rc = GetLastError();
1063 Log(("Lasterror=%d\n", rc));
1064
1065 //SymEnumerateModules64(pVM, win32EnumModulesCallback, NULL);
1066
1067 return VERR_GENERAL_FAILURE;
1068}
1069#endif
1070
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