VirtualBox

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

Last change on this file since 45578 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 36.5 KB
Line 
1/* $Id: DBGFSym.cpp 44528 2013-02-04 14:27:54Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, Symbol Management.
4 */
5
6/*
7 * Copyright (C) 2006-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_DBGF
23#if defined(RT_OS_WINDOWS) && 0 //defined(DEBUG_bird) // enabled this is you want to debug win32 guests, the hypervisor of EFI.
24# include <Windows.h>
25# define _IMAGEHLP64
26# include <DbgHelp.h>
27# define HAVE_DBGHELP /* if doing guest stuff, this can be nice. */
28#endif
29/** @todo Only use DBGHELP for reading modules since it doesn't do all we want (relocations), or is way to slow in some cases (add symbol)! */
30#include <VBox/vmm/dbgf.h>
31#include <VBox/vmm/mm.h>
32#include <VBox/vmm/pdmapi.h>
33#include "DBGFInternal.h"
34#include <VBox/vmm/vm.h>
35#include <VBox/vmm/uvm.h>
36#include <VBox/err.h>
37#include <VBox/log.h>
38
39#include <iprt/assert.h>
40#include <iprt/path.h>
41#include <iprt/ctype.h>
42#include <iprt/env.h>
43#include <iprt/param.h>
44#ifndef HAVE_DBGHELP
45# include <iprt/avl.h>
46# include <iprt/string.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 representation 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 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 && RT_C_IS_SPACE(*psz))
230 psz++;
231 char *psz2 = strchr(psz, '\0') - 1;
232 while (psz2 >= psz && RT_C_IS_SPACE(*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 Pointer to the VM.
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 /** @todo symbol search path setup. */
266
267 /*
268 * Check if there are 'loadsyms' commands in the configuration.
269 */
270 PCFGMNODE pNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "/DBGF/loadsyms/");
271 if (pNode)
272 {
273 /*
274 * Enumerate the commands.
275 */
276 for (PCFGMNODE pCmdNode = CFGMR3GetFirstChild(pNode);
277 pCmdNode;
278 pCmdNode = CFGMR3GetNextChild(pCmdNode))
279 {
280 char szCmdName[128];
281 CFGMR3GetName(pCmdNode, &szCmdName[0], sizeof(szCmdName));
282
283 /* File */
284 char *pszFilename;
285 rc = CFGMR3QueryStringAlloc(pCmdNode, "Filename", &pszFilename);
286 AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'File' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
287
288 /* Delta (optional) */
289 RTGCINTPTR offDelta;
290 rc = CFGMR3QueryGCPtrS(pNode, "Delta", &offDelta);
291 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
292 offDelta = 0;
293 else
294 AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'Delta' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
295
296 /* Module (optional) */
297 char *pszModule;
298 rc = CFGMR3QueryStringAlloc(pCmdNode, "Module", &pszModule);
299 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
300 pszModule = NULL;
301 else
302 AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'Module' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
303
304 /* Module (optional) */
305 RTGCUINTPTR ModuleAddress;
306 rc = CFGMR3QueryGCPtrU(pNode, "ModuleAddress", &ModuleAddress);
307 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
308 ModuleAddress = 0;
309 else
310 AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'ModuleAddress' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
311
312 /* Image size (optional) */
313 RTGCUINTPTR cbModule;
314 rc = CFGMR3QueryGCPtrU(pNode, "ModuleSize", &cbModule);
315 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
316 cbModule = 0;
317 else
318 AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'ModuleAddress' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
319
320
321 /*
322 * Execute the command.
323 */
324 rc = DBGFR3ModuleLoad(pVM->pUVM, pszFilename, offDelta, pszModule, ModuleAddress, cbModule);
325 AssertMsgRCReturn(rc, ("pszFilename=%s offDelta=%RGv pszModule=%s ModuleAddress=%RGv cbModule=%RGv\n",
326 pszFilename, offDelta, pszModule, ModuleAddress, cbModule), rc);
327
328 MMR3HeapFree(pszModule);
329 MMR3HeapFree(pszFilename);
330 }
331 }
332
333 /*
334 * Check if there are 'loadmap' commands in the configuration.
335 */
336 pNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "/DBGF/loadmap/");
337 if (pNode)
338 {
339 /*
340 * Enumerate the commands.
341 */
342 for (PCFGMNODE pCmdNode = CFGMR3GetFirstChild(pNode);
343 pCmdNode;
344 pCmdNode = CFGMR3GetNextChild(pCmdNode))
345 {
346 char szCmdName[128];
347 CFGMR3GetName(pCmdNode, &szCmdName[0], sizeof(szCmdName));
348
349 /* File */
350 char *pszFilename;
351 rc = CFGMR3QueryStringAlloc(pCmdNode, "Filename", &pszFilename);
352 AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'File' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
353
354 /* Address. */
355 RTGCPTR GCPtrAddr;
356 rc = CFGMR3QueryGCPtrUDef(pNode, "Address", &GCPtrAddr, 0);
357 AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'Address' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
358 DBGFADDRESS ModAddr;
359 DBGFR3AddrFromFlat(pVM->pUVM, &ModAddr, GCPtrAddr);
360
361 /* Name (optional) */
362 char *pszModName;
363 rc = CFGMR3QueryStringAllocDef(pCmdNode, "Name", &pszModName, NULL);
364 AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'Name' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
365
366 /* Subtrahend (optional) */
367 RTGCPTR offSubtrahend;
368 rc = CFGMR3QueryGCPtrDef(pNode, "Subtrahend", &offSubtrahend, 0);
369 AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'Subtrahend' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
370
371 /* Segment (optional) */
372 uint32_t iSeg;
373 rc = CFGMR3QueryU32Def(pNode, "Segment", &iSeg, UINT32_MAX);
374 AssertMsgRCReturn(rc, ("rc=%Rrc querying the 'Segment' attribute of '/DBGF/loadsyms/%s'!\n", rc, szCmdName), rc);
375
376 /*
377 * Execute the command.
378 */
379 rc = DBGFR3AsLoadMap(pVM->pUVM, DBGF_AS_GLOBAL, pszFilename, pszModName, &ModAddr,
380 iSeg == UINT32_MAX ? NIL_RTDBGSEGIDX : iSeg, offSubtrahend, 0 /*fFlags*/);
381 AssertMsgRCReturn(rc, ("pszFilename=%s pszModName=%s ModAddr=%RGv offSubtrahend=%#x iSeg=%#x\n",
382 pszFilename, pszModName, ModAddr.FlatPtr, offSubtrahend, iSeg), rc);
383
384 MMR3HeapFree(pszModName);
385 MMR3HeapFree(pszFilename);
386 }
387 }
388
389 /*
390 * Check if there are any 'symadd' commands in the configuration.
391 */
392
393 return VINF_SUCCESS;
394}
395
396
397/**
398 * We delay certain
399 * Initialize the debug info for a VM.
400 */
401int dbgfR3SymLazyInit(PVM pVM)
402{
403 if (pVM->dbgf.s.fSymInited)
404 return VINF_SUCCESS;
405#ifdef HAVE_DBGHELP
406 if (SymInitialize(pVM, NULL, FALSE))
407 {
408 pVM->dbgf.s.fSymInited = true;
409 SymSetOptions(SYMOPT_LOAD_LINES | SYMOPT_AUTO_PUBLICS | SYMOPT_ALLOW_ABSOLUTE_SYMBOLS);
410
411 /*
412 * Enumerate all modules loaded by PDM and add them to the symbol database.
413 */
414 PDMR3LdrEnumModules(pVM, dbgfR3EnumModules, NULL);
415 return VINF_SUCCESS;
416 }
417 return win32Error(pVM);
418#else
419 return VINF_SUCCESS;
420#endif
421}
422
423
424#ifdef HAVE_DBGHELP
425/**
426 * Module enumeration callback function.
427 *
428 * @returns VBox status.
429 * Failure will stop the search and return the return code.
430 * Warnings will be ignored and not returned.
431 * @param pVM Pointer to the VM.
432 * @param pszFilename Module filename.
433 * @param pszName Module name. (short and unique)
434 * @param ImageBase Address where to executable image is loaded.
435 * @param cbImage Size of the executable image.
436 * @param fRC Set if guest context, clear if host context.
437 * @param pvArg User argument.
438 */
439static DECLCALLBACK(int) dbgfR3EnumModules(PVM pVM, const char *pszFilename, const char *pszName,
440 RTUINTPTR ImageBase, size_t cbImage, bool fRC, void *pvArg)
441{
442 DWORD64 LoadedImageBase = SymLoadModule64(pVM, NULL, (char *)(void *)pszFilename,
443 (char *)(void *)pszName, ImageBase, (DWORD)cbImage);
444 if (!LoadedImageBase)
445 Log(("SymLoadModule64(,,%s,,) -> lasterr=%d\n", pszFilename, GetLastError()));
446 else
447 Log(("Loaded debuginfo for %s - %s %llx\n", pszName, pszFilename, LoadedImageBase));
448
449 return VINF_SUCCESS;
450}
451#endif
452
453
454/**
455 * Terminate the debug info repository for the specified VM.
456 *
457 * @returns VBox status.
458 * @param pVM Pointer to the VM.
459 */
460int dbgfR3SymTerm(PVM pVM)
461{
462#ifdef HAVE_DBGHELP
463 if (pVM->dbgf.s.fSymInited)
464 SymCleanup(pVM);
465 pVM->dbgf.s.fSymInited = false;
466 return VINF_SUCCESS;
467#else
468 pVM->dbgf.s.SymbolTree = 0; /* MM cleans up allocations */
469 pVM->dbgf.s.fSymInited = false;
470 return VINF_SUCCESS;
471#endif
472}
473
474
475/** Symbol file type.. */
476typedef enum SYMFILETYPE
477{
478 SYMFILETYPE_UNKNOWN,
479 SYMFILETYPE_LD_MAP,
480 SYMFILETYPE_MS_MAP,
481 SYMFILETYPE_OBJDUMP,
482 SYMFILETYPE_LINUX_SYSTEM_MAP,
483 SYMFILETYPE_PDB,
484 SYMFILETYPE_DBG,
485 SYMFILETYPE_MZ,
486 SYMFILETYPE_ELF
487} SYMFILETYPE, *PSYMFILETYPE;
488
489
490
491/**
492 * Probe the type of a symbol information file.
493 *
494 * @returns The file type.
495 * @param pFile File handle.
496 */
497SYMFILETYPE dbgfR3ModuleProbe(FILE *pFile)
498{
499 char szHead[4096];
500 size_t cchHead = fread(szHead, 1, sizeof(szHead) - 1, pFile);
501 if (cchHead > 0)
502 {
503 szHead[cchHead] = '\0';
504 if (strstr(szHead, "Preferred load address is"))
505 return SYMFILETYPE_MS_MAP;
506
507 if ( strstr(szHead, "Archive member included because of")
508 || strstr(szHead, "Memory Configuration")
509 || strstr(szHead, "Linker script and memory map"))
510 return SYMFILETYPE_LD_MAP;
511
512 if ( RT_C_IS_XDIGIT(szHead[0])
513 && RT_C_IS_XDIGIT(szHead[1])
514 && RT_C_IS_XDIGIT(szHead[2])
515 && RT_C_IS_XDIGIT(szHead[3])
516 && RT_C_IS_XDIGIT(szHead[4])
517 && RT_C_IS_XDIGIT(szHead[5])
518 && RT_C_IS_XDIGIT(szHead[6])
519 && RT_C_IS_XDIGIT(szHead[7])
520 && szHead[8] == ' '
521 && RT_C_IS_ALPHA(szHead[9])
522 && szHead[10] == ' '
523 && (RT_C_IS_ALPHA(szHead[11]) || szHead[11] == '_' || szHead[11] == '$')
524 )
525 return SYMFILETYPE_LINUX_SYSTEM_MAP;
526
527 if ( RT_C_IS_XDIGIT(szHead[0])
528 && RT_C_IS_XDIGIT(szHead[1])
529 && RT_C_IS_XDIGIT(szHead[2])
530 && RT_C_IS_XDIGIT(szHead[3])
531 && RT_C_IS_XDIGIT(szHead[4])
532 && RT_C_IS_XDIGIT(szHead[5])
533 && RT_C_IS_XDIGIT(szHead[6])
534 && RT_C_IS_XDIGIT(szHead[7])
535 && RT_C_IS_XDIGIT(szHead[8])
536 && RT_C_IS_XDIGIT(szHead[9])
537 && RT_C_IS_XDIGIT(szHead[10])
538 && RT_C_IS_XDIGIT(szHead[11])
539 && RT_C_IS_XDIGIT(szHead[12])
540 && RT_C_IS_XDIGIT(szHead[13])
541 && RT_C_IS_XDIGIT(szHead[14])
542 && RT_C_IS_XDIGIT(szHead[15])
543 && szHead[16] == ' '
544 && RT_C_IS_ALPHA(szHead[17])
545 && szHead[18] == ' '
546 && (RT_C_IS_ALPHA(szHead[19]) || szHead[19] == '_' || szHead[19] == '$')
547 )
548 return SYMFILETYPE_LINUX_SYSTEM_MAP;
549
550 if (strstr(szHead, "Microsoft C/C++ MSF") == szHead)
551 return SYMFILETYPE_PDB;
552
553 if (strstr(szHead, "ELF") == szHead + 1)
554 return SYMFILETYPE_ELF;
555
556 if ( strstr(szHead, "MZ") == szHead
557 || strstr(szHead, "PE") == szHead
558 || strstr(szHead, "LE") == szHead
559 || strstr(szHead, "LX") == szHead
560 || strstr(szHead, "NE") == szHead)
561 return SYMFILETYPE_MZ;
562
563
564 if (strstr(szHead, "file format"))
565 return SYMFILETYPE_OBJDUMP;
566 }
567
568 return SYMFILETYPE_UNKNOWN;
569}
570
571
572static int dbgfR3LoadLinuxSystemMap(PVM pVM, FILE *pFile, RTGCUINTPTR ModuleAddress, RTGCUINTPTR AddressDelta)
573{
574 char szLine[4096];
575 while (fgets(szLine, sizeof(szLine), pFile))
576 {
577 /* parse the line: <address> <type> <name> */
578 const char *psz = dbgfR3Strip(szLine);
579 char *pszEnd = NULL;
580 uint64_t u64Address;
581 int rc = RTStrToUInt64Ex(psz, &pszEnd, 16, &u64Address);
582 RTGCUINTPTR Address = u64Address;
583 if ( RT_SUCCESS(rc)
584 && (*pszEnd == ' ' || *pszEnd == '\t')
585 && Address == u64Address
586 && u64Address != 0
587 && u64Address != (RTGCUINTPTR)~0)
588 {
589 pszEnd++;
590 if ( RT_C_IS_ALPHA(*pszEnd)
591 && (pszEnd[1] == ' ' || pszEnd[1] == '\t'))
592 {
593 psz = dbgfR3Strip(pszEnd + 2);
594 if (*psz)
595 {
596 int rc2 = DBGFR3SymbolAdd(pVM, ModuleAddress, Address + AddressDelta, 0, psz);
597 if (RT_FAILURE(rc2))
598 Log2(("DBGFR3SymbolAdd(,, %RGv, 0, '%s') -> %Rrc\n", Address, psz, rc2));
599 }
600 }
601 }
602 }
603 return VINF_SUCCESS;
604}
605
606/**
607 * Tries to open the file using the image search paths.
608 *
609 * This is currently a quick hack and the only way to specifying the path is by setting
610 * VBOXDBG_IMAGE_PATH in the environment. It uses semicolon as separator everywhere.
611 *
612 * @returns VBox status code.
613 * @param pVM Pointer to the VM.
614 * @param pszFilename The name of the file to locate and open.
615 * @param pszFound Where to return the actual filename.
616 * @param cchFound The buffer size.
617 * @param ppFile Where to return the opened file.
618 */
619int dbgfR3ModuleLocateAndOpen(PVM pVM, const char *pszFilename, char *pszFound, size_t cchFound, FILE **ppFile)
620{
621 NOREF(pVM);
622
623 /* Check the filename length. */
624 size_t const cchFilename = strlen(pszFilename);
625 if (cchFilename >= cchFound)
626 return VERR_FILENAME_TOO_LONG;
627 const char *pszName = RTPathFilename(pszFilename);
628 if (!pszName)
629 return VERR_IS_A_DIRECTORY;
630 size_t const cchName = strlen(pszName);
631
632 /*
633 * Try default location first.
634 */
635 memcpy(pszFound, pszFilename, cchFilename + 1);
636 FILE *pFile = *ppFile = fopen(pszFound, "rb");
637 if (pFile)
638 return VINF_SUCCESS;
639
640 /*
641 * Walk the search path.
642 */
643 char *pszFreeMe = RTEnvDupEx(RTENV_DEFAULT, "VBOXDBG_IMAGE_PATH");
644 const char *psz = pszFreeMe ? pszFreeMe : ".";
645 while (*psz)
646 {
647 /* Skip leading blanks - no directories with leading spaces, thank you. */
648 while (RT_C_IS_BLANK(*psz))
649 psz++;
650
651 /* Fine the end of this element. */
652 const char *pszNext;
653 const char *pszEnd = strchr(psz, ';');
654 if (!pszEnd)
655 pszEnd = pszNext = strchr(psz, '\0');
656 else
657 pszNext = pszEnd + 1;
658 if (pszEnd != psz)
659 {
660 size_t const cch = pszEnd - psz;
661 if (cch + 1 + cchName < cchFound)
662 {
663 /** @todo RTPathCompose, RTPathComposeN(). This code isn't right
664 * for 'E:' on DOS systems. It may also create unwanted double slashes. */
665 memcpy(pszFound, psz, cch);
666 pszFound[cch] = '/';
667 memcpy(pszFound + cch + 1, pszName, cchName + 1);
668 *ppFile = pFile = fopen(pszFound, "rb");
669 if (pFile)
670 {
671 RTStrFree(pszFreeMe);
672 return VINF_SUCCESS;
673 }
674 }
675
676 /** @todo do a depth search using the specified path. */
677 }
678
679 /* advance */
680 psz = pszNext;
681 }
682
683 /* not found */
684 RTStrFree(pszFreeMe);
685 return VERR_OPEN_FAILED;
686}
687
688
689/**
690 * Load debug info, optionally related to a specific module.
691 *
692 * @returns VBox status.
693 * @param pUVM The user mode VM handle.
694 * @param pszFilename Path to the file containing the symbol information.
695 * This can be the executable image, a flat symbol file of some kind or stripped debug info.
696 * @param AddressDelta The value to add to the loaded symbols.
697 * @param pszName Short hand name for the module. If not related to a module specify NULL.
698 * @param ModuleAddress Address which the image is loaded at. This will be used to reference the module other places in the api.
699 * Ignored when pszName is NULL.
700 * @param cbImage Size of the image.
701 * Ignored when pszName is NULL.
702 */
703VMMR3DECL(int) DBGFR3ModuleLoad(PUVM pUVM, const char *pszFilename, RTGCUINTPTR AddressDelta, const char *pszName,
704 RTGCUINTPTR ModuleAddress, unsigned cbImage)
705{
706 NOREF(cbImage);
707
708 /*
709 * Lazy init.
710 */
711 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
712 PVM pVM = pUVM->pVM;
713 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
714 if (!pVM->dbgf.s.fSymInited)
715 {
716 int rc = dbgfR3SymLazyInit(pVM);
717 if (RT_FAILURE(rc))
718 return rc;
719 }
720
721 /*
722 * Open the load file.
723 */
724 FILE *pFile = NULL;
725 char szFoundFile[RTPATH_MAX];
726 int rc = dbgfR3ModuleLocateAndOpen(pVM, pszFilename, szFoundFile, sizeof(szFoundFile), &pFile);
727 if (pFile)
728 {
729 /*
730 * Probe the file type.
731 */
732 SYMFILETYPE enmType = dbgfR3ModuleProbe(pFile);
733 if (enmType != SYMFILETYPE_UNKNOWN)
734 {
735 /*
736 * Add the module.
737 */
738 if (pszName)
739 {
740 #ifdef HAVE_DBGHELP
741 /** @todo arg! checkout the inserting of modules and then loading them again.... Or just the module representation.... */
742 DWORD64 ImageBase = SymLoadModule64(pVM, NULL, (char *)(void *)szFoundFile, (char *)(void *)pszName, ModuleAddress, cbImage);
743 if (!ImageBase)
744 ImageBase = SymLoadModule64(pVM, NULL, (char *)(void *)pszName, (char *)(void *)pszName, ModuleAddress, cbImage);
745 if (ImageBase)
746 {
747 AssertMsg(ModuleAddress == 0 || ModuleAddress == ImageBase, ("ModuleAddres=%RGv ImageBase=%llx\n", ModuleAddress, ImageBase));
748 ModuleAddress = ImageBase;
749 }
750 else
751 rc = win32Error(pVM);
752 #else
753 rc = VERR_NOT_IMPLEMENTED;
754 #endif
755 }
756 if (RT_SUCCESS(rc))
757 {
758 /*
759 * Seek to the start of the file.
760 */
761 rc = fseek(pFile, 0, SEEK_SET);
762 Assert(!rc);
763
764 /*
765 * Process the specific.
766 */
767 switch (enmType)
768 {
769 case SYMFILETYPE_LINUX_SYSTEM_MAP:
770 rc = dbgfR3LoadLinuxSystemMap(pVM, pFile, ModuleAddress, AddressDelta);
771 break;
772
773 case SYMFILETYPE_PDB:
774 case SYMFILETYPE_DBG:
775 case SYMFILETYPE_MZ:
776 #ifdef HAVE_DBGHELP
777 /* done it all above! */
778 break;
779 #endif
780 case SYMFILETYPE_LD_MAP:
781 case SYMFILETYPE_MS_MAP:
782 case SYMFILETYPE_OBJDUMP:
783 case SYMFILETYPE_ELF:
784 rc = VERR_NOT_SUPPORTED;
785 break;
786
787 default:
788 AssertFailed();
789 rc = VERR_INTERNAL_ERROR;
790 break;
791 } /* file switch. */
792 } /* module added successfully. */
793 } /* format identified */
794 else
795 rc = VERR_NOT_SUPPORTED;
796 /** @todo check for read errors */
797 fclose(pFile);
798 }
799 return rc;
800}
801
802
803/**
804 * Interface used by PDMR3LdrRelocate for telling us that a GC module has been relocated.
805 *
806 * @param pVM Pointer to the VM.
807 * @param OldImageBase The old image base.
808 * @param NewImageBase The new image base.
809 * @param cbImage The image size.
810 * @param pszFilename The image filename.
811 * @param pszName The module name.
812 */
813VMMR3_INT_DECL(void) DBGFR3ModuleRelocate(PVM pVM, RTGCUINTPTR OldImageBase, RTGCUINTPTR NewImageBase, RTGCUINTPTR cbImage,
814 const char *pszFilename, const char *pszName)
815{
816#ifdef HAVE_DBGHELP
817 if (pVM->dbgf.s.fSymInited)
818 {
819 if (!SymUnloadModule64(pVM, OldImageBase))
820 Log(("SymUnloadModule64(,%RGv) failed, lasterr=%d\n", OldImageBase, GetLastError()));
821
822 DWORD ImageSize = (DWORD)cbImage; Assert(ImageSize == cbImage);
823 DWORD64 LoadedImageBase = SymLoadModule64(pVM, NULL, (char *)(void *)pszFilename, (char *)(void *)pszName, NewImageBase, ImageSize);
824 if (!LoadedImageBase)
825 Log(("SymLoadModule64(,,%s,,) -> lasterr=%d (relocate)\n", pszFilename, GetLastError()));
826 else
827 Log(("Reloaded debuginfo for %s - %s %llx\n", pszName, pszFilename, LoadedImageBase));
828 }
829#else
830 NOREF(pVM); NOREF(OldImageBase); NOREF(NewImageBase); NOREF(cbImage); NOREF(pszFilename); NOREF(pszName);
831#endif
832}
833
834
835/**
836 * Adds a symbol to the debug info manager.
837 *
838 * @returns VBox status.
839 * @param pVM Pointer to the VM.
840 * @param ModuleAddress Module address. Use 0 if no module.
841 * @param SymbolAddress Symbol address
842 * @param cbSymbol Size of the symbol. Use 0 if info not available.
843 * @param pszSymbol Symbol name.
844 */
845VMMR3_INT_DECL(int) DBGFR3SymbolAdd(PVM pVM, RTGCUINTPTR ModuleAddress, RTGCUINTPTR SymbolAddress, RTUINT cbSymbol,
846 const char *pszSymbol)
847{
848 /*
849 * Validate.
850 */
851 if (!pszSymbol || !*pszSymbol)
852 {
853 AssertMsgFailed(("No symbol name!\n"));
854 return VERR_INVALID_PARAMETER;
855 }
856
857 /*
858 * Lazy init.
859 */
860 if (!pVM->dbgf.s.fSymInited)
861 {
862 int rc = dbgfR3SymLazyInit(pVM);
863 if (RT_FAILURE(rc))
864 return rc;
865 }
866
867#ifdef HAVE_DBGHELP
868 if (SymAddSymbol(pVM, ModuleAddress, (char *)(void *)pszSymbol, SymbolAddress, cbSymbol, 0))
869 return VINF_SUCCESS;
870 return win32Error(pVM);
871#else
872 NOREF(ModuleAddress); /** @todo module lookup. */
873 return dbgfR3SymbolInsert(pVM, pszSymbol, SymbolAddress, cbSymbol, NULL);
874#endif
875}
876
877
878/**
879 * Find symbol by address (nearest).
880 *
881 * @returns VBox status.
882 * @param pVM Pointer to the VM.
883 * @param Address Address.
884 * @param poffDisplacement Where to store the symbol displacement from Address.
885 * @param pSymbol Where to store the symbol info.
886 */
887VMMR3_INT_DECL(int) DBGFR3SymbolByAddr(PVM pVM, RTGCUINTPTR Address, PRTGCINTPTR poffDisplacement, PDBGFSYMBOL pSymbol)
888{
889 /*
890 * Lazy init.
891 */
892 if (!pVM->dbgf.s.fSymInited)
893 {
894 int rc = dbgfR3SymLazyInit(pVM);
895 if (RT_FAILURE(rc))
896 return rc;
897 }
898
899 /*
900 * Look it up.
901 */
902#ifdef HAVE_DBGHELP
903 char achBuffer[sizeof(IMAGEHLP_SYMBOL64) + DBGF_SYMBOL_NAME_LENGTH * sizeof(TCHAR) + sizeof(ULONG64)];
904 PIMAGEHLP_SYMBOL64 pSym = (PIMAGEHLP_SYMBOL64)&achBuffer[0];
905 pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
906 pSym->MaxNameLength = DBGF_SYMBOL_NAME_LENGTH;
907
908 if (SymGetSymFromAddr64(pVM, Address, (PDWORD64)poffDisplacement, pSym))
909 {
910 pSymbol->Value = (RTGCUINTPTR)pSym->Address;
911 pSymbol->cb = pSym->Size;
912 pSymbol->fFlags = pSym->Flags;
913 strcpy(pSymbol->szName, pSym->Name);
914 return VINF_SUCCESS;
915 }
916 //return win32Error(pVM);
917
918#else
919
920 PDBGFSYM pSym = dbgfR3SymbolGetAddr(pVM, Address);
921 if (pSym)
922 {
923 pSymbol->Value = pSym->Core.Key;
924 pSymbol->cb = pSym->Core.KeyLast - pSym->Core.Key + 1;
925 pSymbol->fFlags = 0;
926 pSymbol->szName[0] = '\0';
927 strncat(pSymbol->szName, pSym->szName, sizeof(pSymbol->szName) - 1);
928 if (poffDisplacement)
929 *poffDisplacement = Address - pSymbol->Value;
930 return VINF_SUCCESS;
931 }
932
933#endif
934
935 /*
936 * Try PDM.
937 */
938 if (MMHyperIsInsideArea(pVM, Address))
939 {
940 char szModName[64];
941 RTRCPTR RCPtrMod;
942 char szNearSym1[260];
943 RTRCPTR RCPtrNearSym1;
944 char szNearSym2[260];
945 RTRCPTR RCPtrNearSym2;
946 int rc = PDMR3LdrQueryRCModFromPC(pVM, Address,
947 &szModName[0], sizeof(szModName), &RCPtrMod,
948 &szNearSym1[0], sizeof(szNearSym1), &RCPtrNearSym1,
949 &szNearSym2[0], sizeof(szNearSym2), &RCPtrNearSym2);
950 if (RT_SUCCESS(rc) && szNearSym1[0])
951 {
952 pSymbol->Value = RCPtrNearSym1;
953 pSymbol->cb = RCPtrNearSym2 > RCPtrNearSym1 ? RCPtrNearSym2 - RCPtrNearSym1 : 0;
954 pSymbol->fFlags = 0;
955 pSymbol->szName[0] = '\0';
956 strncat(pSymbol->szName, szNearSym1, sizeof(pSymbol->szName) - 1);
957 if (poffDisplacement)
958 *poffDisplacement = Address - pSymbol->Value;
959 return VINF_SUCCESS;
960 }
961 }
962
963 return VERR_SYMBOL_NOT_FOUND;
964}
965
966
967/**
968 * Find symbol by name (first).
969 *
970 * @returns VBox status.
971 * @param pVM Pointer to the VM.
972 * @param pszSymbol Symbol name.
973 * @param pSymbol Where to store the symbol info.
974 */
975VMMR3_INT_DECL(int) DBGFR3SymbolByName(PVM pVM, const char *pszSymbol, PDBGFSYMBOL pSymbol)
976{
977 /*
978 * Lazy init.
979 */
980 if (!pVM->dbgf.s.fSymInited)
981 {
982 int rc = dbgfR3SymLazyInit(pVM);
983 if (RT_FAILURE(rc))
984 return rc;
985 }
986
987 /*
988 * Look it up.
989 */
990#ifdef HAVE_DBGHELP
991 char achBuffer[sizeof(IMAGEHLP_SYMBOL64) + DBGF_SYMBOL_NAME_LENGTH * sizeof(TCHAR) + sizeof(ULONG64)];
992 PIMAGEHLP_SYMBOL64 pSym = (PIMAGEHLP_SYMBOL64)&achBuffer[0];
993 pSym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64);
994 pSym->MaxNameLength = DBGF_SYMBOL_NAME_LENGTH;
995
996 if (SymGetSymFromName64(pVM, (char *)(void *)pszSymbol, pSym))
997 {
998 pSymbol->Value = (RTGCUINTPTR)pSym->Address;
999 pSymbol->cb = pSym->Size;
1000 pSymbol->fFlags = pSym->Flags;
1001 strcpy(pSymbol->szName, pSym->Name);
1002 return VINF_SUCCESS;
1003 }
1004 return win32Error(pVM);
1005#else
1006
1007 PDBGFSYM pSym = dbgfR3SymbolGetName(pVM, pszSymbol);
1008 if (pSym)
1009 {
1010 pSymbol->Value = pSym->Core.Key;
1011 pSymbol->cb = pSym->Core.KeyLast - pSym->Core.Key + 1;
1012 pSymbol->fFlags = 0;
1013 pSymbol->szName[0] = '\0';
1014 strncat(pSymbol->szName, pSym->szName, sizeof(pSymbol->szName) - 1);
1015 return VINF_SUCCESS;
1016 }
1017
1018 return VERR_SYMBOL_NOT_FOUND;
1019#endif
1020}
1021
1022
1023/**
1024 * Find line by address (nearest).
1025 *
1026 * @returns VBox status.
1027 * @param pUVM The user mode VM handle.
1028 * @param Address Address.
1029 * @param poffDisplacement Where to store the line displacement from Address.
1030 * @param pLine Where to store the line info.
1031 */
1032VMMR3DECL(int) DBGFR3LineByAddr(PUVM pUVM, RTGCUINTPTR Address, PRTGCINTPTR poffDisplacement, PDBGFLINE pLine)
1033{
1034 /*
1035 * Lazy init.
1036 */
1037 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1038 PVM pVM = pUVM->pVM;
1039 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1040 if (!pVM->dbgf.s.fSymInited)
1041 {
1042 int rc = dbgfR3SymLazyInit(pVM);
1043 if (RT_FAILURE(rc))
1044 return rc;
1045 }
1046
1047 /*
1048 * Look it up.
1049 */
1050#ifdef HAVE_DBGHELP
1051 IMAGEHLP_LINE64 Line = {0};
1052 DWORD off = 0;
1053 Line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
1054 if (SymGetLineFromAddr64(pVM, Address, &off, &Line))
1055 {
1056 if (poffDisplacement)
1057 *poffDisplacement = (long)off;
1058 pLine->Address = (RTGCUINTPTR)Line.Address;
1059 pLine->uLineNo = Line.LineNumber;
1060 pLine->szFilename[0] = '\0';
1061 strncat(pLine->szFilename, Line.FileName, sizeof(pLine->szFilename));
1062 return VINF_SUCCESS;
1063 }
1064 return win32Error(pVM);
1065#else
1066 NOREF(pVM); NOREF(Address); NOREF(poffDisplacement); NOREF(pLine);
1067 return VERR_NOT_IMPLEMENTED;
1068#endif
1069}
1070
1071
1072/**
1073 * Duplicates a line.
1074 *
1075 * @returns VBox status code.
1076 * @param pUVM The user mode VM handle.
1077 * @param pLine The line to duplicate.
1078 */
1079static PDBGFLINE dbgfR3LineDup(PUVM pUVM, PCDBGFLINE pLine)
1080{
1081 size_t cb = strlen(pLine->szFilename) + RT_OFFSETOF(DBGFLINE, szFilename[1]);
1082 PDBGFLINE pDup = (PDBGFLINE)MMR3HeapAllocU(pUVM, MM_TAG_DBGF_LINE_DUP, cb);
1083 if (pDup)
1084 memcpy(pDup, pLine, cb);
1085 return pDup;
1086}
1087
1088
1089/**
1090 * Find line by address (nearest), allocate return buffer.
1091 *
1092 * @returns Pointer to the line. Must be freed using DBGFR3LineFree().
1093 * @returns NULL if the line was not found or if we're out of memory.
1094 * @param pUVM The user mode VM handle.
1095 * @param Address Address.
1096 * @param poffDisplacement Where to store the line displacement from Address.
1097 */
1098VMMR3DECL(PDBGFLINE) DBGFR3LineByAddrAlloc(PUVM pUVM, RTGCUINTPTR Address, PRTGCINTPTR poffDisplacement)
1099{
1100 DBGFLINE Line;
1101 int rc = DBGFR3LineByAddr(pUVM, Address, poffDisplacement, &Line);
1102 if (RT_FAILURE(rc))
1103 return NULL;
1104 return dbgfR3LineDup(pUVM, &Line);
1105}
1106
1107
1108/**
1109 * Frees a line returned by DBGFR3LineByAddressAlloc().
1110 *
1111 * @param pLine Pointer to the line.
1112 */
1113VMMR3_INT_DECL(void) DBGFR3LineFree(PDBGFLINE pLine)
1114{
1115 if (pLine)
1116 MMR3HeapFree(pLine);
1117}
1118
1119
1120#ifdef HAVE_DBGHELP
1121
1122//static BOOL CALLBACK win32EnumModulesCallback(PSTR ModuleName, DWORD64 BaseOfDll, PVOID UserContext)
1123//{
1124// Log(("dbg: module: %08llx %s\n", ModuleName, BaseOfDll));
1125// return TRUE;
1126//}
1127
1128static int win32Error(PVM pVM)
1129{
1130 int rc = GetLastError();
1131 Log(("Lasterror=%d\n", rc));
1132
1133 //SymEnumerateModules64(pVM, win32EnumModulesCallback, NULL);
1134
1135 return VERR_GENERAL_FAILURE;
1136}
1137#endif
1138
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