VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/DBGFAddrSpace.cpp@ 69221

Last change on this file since 69221 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 45.1 KB
Line 
1/* $Id: DBGFAddrSpace.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
2/** @file
3 * DBGF - Debugger Facility, Address Space Management.
4 */
5
6/*
7 * Copyright (C) 2008-2017 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/** @page pg_dbgf_addr_space DBGFAddrSpace - Address Space Management
20 *
21 * What's an address space? It's mainly a convenient way of stuffing
22 * module segments and ad-hoc symbols together. It will also help out
23 * when the debugger gets extended to deal with user processes later.
24 *
25 * There are two standard address spaces that will always be present:
26 * - The physical address space.
27 * - The global virtual address space.
28 *
29 * Additional address spaces will be added and removed at runtime for
30 * guest processes. The global virtual address space will be used to
31 * track the kernel parts of the OS, or at least the bits of the kernel
32 * that is part of all address spaces (mac os x and 4G/4G patched linux).
33 *
34 */
35
36
37/*********************************************************************************************************************************
38* Header Files *
39*********************************************************************************************************************************/
40#define LOG_GROUP LOG_GROUP_DBGF
41#include <VBox/vmm/dbgf.h>
42#include <VBox/vmm/hm.h>
43#include <VBox/vmm/pdmapi.h>
44#include <VBox/vmm/mm.h>
45#ifdef VBOX_WITH_RAW_MODE
46# include <VBox/vmm/patm.h>
47#endif
48#include "DBGFInternal.h"
49#include <VBox/vmm/uvm.h>
50#include <VBox/vmm/vm.h>
51#include <VBox/err.h>
52#include <VBox/log.h>
53
54#include <iprt/asm.h>
55#include <iprt/assert.h>
56#include <iprt/ctype.h>
57#include <iprt/env.h>
58#include <iprt/mem.h>
59#include <iprt/path.h>
60#include <iprt/param.h>
61
62
63/*********************************************************************************************************************************
64* Structures and Typedefs *
65*********************************************************************************************************************************/
66/**
67 * Address space database node.
68 */
69typedef struct DBGFASDBNODE
70{
71 /** The node core for DBGF::AsHandleTree, the key is the address space handle. */
72 AVLPVNODECORE HandleCore;
73 /** The node core for DBGF::AsPidTree, the key is the process id. */
74 AVLU32NODECORE PidCore;
75 /** The node core for DBGF::AsNameSpace, the string is the address space name. */
76 RTSTRSPACECORE NameCore;
77
78} DBGFASDBNODE;
79/** Pointer to an address space database node. */
80typedef DBGFASDBNODE *PDBGFASDBNODE;
81
82
83/**
84 * For dbgfR3AsLoadImageOpenData and dbgfR3AsLoadMapOpenData.
85 */
86typedef struct DBGFR3ASLOADOPENDATA
87{
88 const char *pszModName;
89 RTGCUINTPTR uSubtrahend;
90 uint32_t fFlags;
91 RTDBGMOD hMod;
92} DBGFR3ASLOADOPENDATA;
93
94#if 0 /* unused */
95/**
96 * Callback for dbgfR3AsSearchPath and dbgfR3AsSearchEnvPath.
97 *
98 * @returns VBox status code. If success, then the search is completed.
99 * @param pszFilename The file name under evaluation.
100 * @param pvUser The user argument.
101 */
102typedef int FNDBGFR3ASSEARCHOPEN(const char *pszFilename, void *pvUser);
103/** Pointer to a FNDBGFR3ASSEARCHOPEN. */
104typedef FNDBGFR3ASSEARCHOPEN *PFNDBGFR3ASSEARCHOPEN;
105#endif
106
107
108/*********************************************************************************************************************************
109* Defined Constants And Macros *
110*********************************************************************************************************************************/
111/** Locks the address space database for writing. */
112#define DBGF_AS_DB_LOCK_WRITE(pUVM) \
113 do { \
114 int rcSem = RTSemRWRequestWrite((pUVM)->dbgf.s.hAsDbLock, RT_INDEFINITE_WAIT); \
115 AssertRC(rcSem); \
116 } while (0)
117
118/** Unlocks the address space database after writing. */
119#define DBGF_AS_DB_UNLOCK_WRITE(pUVM) \
120 do { \
121 int rcSem = RTSemRWReleaseWrite((pUVM)->dbgf.s.hAsDbLock); \
122 AssertRC(rcSem); \
123 } while (0)
124
125/** Locks the address space database for reading. */
126#define DBGF_AS_DB_LOCK_READ(pUVM) \
127 do { \
128 int rcSem = RTSemRWRequestRead((pUVM)->dbgf.s.hAsDbLock, RT_INDEFINITE_WAIT); \
129 AssertRC(rcSem); \
130 } while (0)
131
132/** Unlocks the address space database after reading. */
133#define DBGF_AS_DB_UNLOCK_READ(pUVM) \
134 do { \
135 int rcSem = RTSemRWReleaseRead((pUVM)->dbgf.s.hAsDbLock); \
136 AssertRC(rcSem); \
137 } while (0)
138
139
140
141/**
142 * Initializes the address space parts of DBGF.
143 *
144 * @returns VBox status code.
145 * @param pUVM The user mode VM handle.
146 */
147int dbgfR3AsInit(PUVM pUVM)
148{
149 Assert(pUVM->pVM);
150
151 /*
152 * Create the semaphore.
153 */
154 int rc = RTSemRWCreate(&pUVM->dbgf.s.hAsDbLock);
155 AssertRCReturn(rc, rc);
156
157 /*
158 * Create the debugging config instance and set it up, defaulting to
159 * deferred loading in order to keep things fast.
160 */
161 rc = RTDbgCfgCreate(&pUVM->dbgf.s.hDbgCfg, NULL, true /*fNativePaths*/);
162 AssertRCReturn(rc, rc);
163 rc = RTDbgCfgChangeUInt(pUVM->dbgf.s.hDbgCfg, RTDBGCFGPROP_FLAGS, RTDBGCFGOP_PREPEND,
164 RTDBGCFG_FLAGS_DEFERRED);
165 AssertRCReturn(rc, rc);
166
167 static struct
168 {
169 RTDBGCFGPROP enmProp;
170 const char *pszEnvName;
171 const char *pszCfgName;
172 } const s_aProps[] =
173 {
174 { RTDBGCFGPROP_FLAGS, "VBOXDBG_FLAGS", "Flags" },
175 { RTDBGCFGPROP_PATH, "VBOXDBG_PATH", "Path" },
176 { RTDBGCFGPROP_SUFFIXES, "VBOXDBG_SUFFIXES", "Suffixes" },
177 { RTDBGCFGPROP_SRC_PATH, "VBOXDBG_SRC_PATH", "SrcPath" },
178 };
179 PCFGMNODE pCfgDbgf = CFGMR3GetChild(CFGMR3GetRootU(pUVM), "/DBGF");
180 for (unsigned i = 0; i < RT_ELEMENTS(s_aProps); i++)
181 {
182 char szEnvValue[8192];
183 rc = RTEnvGetEx(RTENV_DEFAULT, s_aProps[i].pszEnvName, szEnvValue, sizeof(szEnvValue), NULL);
184 if (RT_SUCCESS(rc))
185 {
186 rc = RTDbgCfgChangeString(pUVM->dbgf.s.hDbgCfg, s_aProps[i].enmProp, RTDBGCFGOP_PREPEND, szEnvValue);
187 if (RT_FAILURE(rc))
188 return VMR3SetError(pUVM, rc, RT_SRC_POS,
189 "DBGF Config Error: %s=%s -> %Rrc", s_aProps[i].pszEnvName, szEnvValue, rc);
190 }
191 else if (rc != VERR_ENV_VAR_NOT_FOUND)
192 return VMR3SetError(pUVM, rc, RT_SRC_POS,
193 "DBGF Config Error: Error querying env.var. %s: %Rrc", s_aProps[i].pszEnvName, rc);
194
195 char *pszCfgValue;
196 rc = CFGMR3QueryStringAllocDef(pCfgDbgf, s_aProps[i].pszCfgName, &pszCfgValue, NULL);
197 if (RT_FAILURE(rc))
198 return VMR3SetError(pUVM, rc, RT_SRC_POS,
199 "DBGF Config Error: Querying /DBGF/%s -> %Rrc", s_aProps[i].pszCfgName, rc);
200 if (pszCfgValue)
201 {
202 rc = RTDbgCfgChangeString(pUVM->dbgf.s.hDbgCfg, s_aProps[i].enmProp, RTDBGCFGOP_PREPEND, pszCfgValue);
203 if (RT_FAILURE(rc))
204 return VMR3SetError(pUVM, rc, RT_SRC_POS,
205 "DBGF Config Error: /DBGF/%s=%s -> %Rrc", s_aProps[i].pszCfgName, pszCfgValue, rc);
206 }
207 }
208
209 /*
210 * Prepend the NoArch and VBoxDbgSyms directories to the path.
211 */
212 char szPath[RTPATH_MAX];
213 rc = RTPathAppPrivateNoArch(szPath, sizeof(szPath));
214 AssertRCReturn(rc, rc);
215#ifdef RT_OS_DARWIN
216 rc = RTPathAppend(szPath, sizeof(szPath), "../Resources/VBoxDbgSyms/");
217#else
218 rc = RTDbgCfgChangeString(pUVM->dbgf.s.hDbgCfg, RTDBGCFGPROP_PATH, RTDBGCFGOP_PREPEND, szPath);
219 AssertRCReturn(rc, rc);
220
221 rc = RTPathAppend(szPath, sizeof(szPath), "VBoxDbgSyms/");
222#endif
223 AssertRCReturn(rc, rc);
224 rc = RTDbgCfgChangeString(pUVM->dbgf.s.hDbgCfg, RTDBGCFGPROP_PATH, RTDBGCFGOP_PREPEND, szPath);
225 AssertRCReturn(rc, rc);
226
227 /*
228 * Create the standard address spaces.
229 */
230 RTDBGAS hDbgAs;
231 rc = RTDbgAsCreate(&hDbgAs, 0, RTGCPTR_MAX, "Global");
232 AssertRCReturn(rc, rc);
233 rc = DBGFR3AsAdd(pUVM, hDbgAs, NIL_RTPROCESS);
234 AssertRCReturn(rc, rc);
235 pUVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(DBGF_AS_GLOBAL)] = hDbgAs;
236
237 RTDbgAsRetain(hDbgAs);
238 pUVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(DBGF_AS_KERNEL)] = hDbgAs;
239
240 rc = RTDbgAsCreate(&hDbgAs, 0, RTGCPHYS_MAX, "Physical");
241 AssertRCReturn(rc, rc);
242 rc = DBGFR3AsAdd(pUVM, hDbgAs, NIL_RTPROCESS);
243 AssertRCReturn(rc, rc);
244 pUVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(DBGF_AS_PHYS)] = hDbgAs;
245
246 rc = RTDbgAsCreate(&hDbgAs, 0, RTRCPTR_MAX, "HyperRawMode");
247 AssertRCReturn(rc, rc);
248 rc = DBGFR3AsAdd(pUVM, hDbgAs, NIL_RTPROCESS);
249 AssertRCReturn(rc, rc);
250 pUVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(DBGF_AS_RC)] = hDbgAs;
251 RTDbgAsRetain(hDbgAs);
252 pUVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(DBGF_AS_RC_AND_GC_GLOBAL)] = hDbgAs;
253
254 rc = RTDbgAsCreate(&hDbgAs, 0, RTR0PTR_MAX, "HyperRing0");
255 AssertRCReturn(rc, rc);
256 rc = DBGFR3AsAdd(pUVM, hDbgAs, NIL_RTPROCESS);
257 AssertRCReturn(rc, rc);
258 pUVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(DBGF_AS_R0)] = hDbgAs;
259
260 return VINF_SUCCESS;
261}
262
263
264/**
265 * Callback used by dbgfR3AsTerm / RTAvlPVDestroy to release an address space.
266 *
267 * @returns 0.
268 * @param pNode The address space database node.
269 * @param pvIgnore NULL.
270 */
271static DECLCALLBACK(int) dbgfR3AsTermDestroyNode(PAVLPVNODECORE pNode, void *pvIgnore)
272{
273 PDBGFASDBNODE pDbNode = (PDBGFASDBNODE)pNode;
274 RTDbgAsRelease((RTDBGAS)pDbNode->HandleCore.Key);
275 pDbNode->HandleCore.Key = NIL_RTDBGAS;
276 /* Don't bother freeing it here as MM will free it soon and MM is much at
277 it when doing it wholesale instead of piecemeal. */
278 NOREF(pvIgnore);
279 return 0;
280}
281
282
283/**
284 * Terminates the address space parts of DBGF.
285 *
286 * @param pUVM The user mode VM handle.
287 */
288void dbgfR3AsTerm(PUVM pUVM)
289{
290 /*
291 * Create the semaphore.
292 */
293 int rc = RTSemRWDestroy(pUVM->dbgf.s.hAsDbLock);
294 AssertRC(rc);
295 pUVM->dbgf.s.hAsDbLock = NIL_RTSEMRW;
296
297 /*
298 * Release all the address spaces.
299 */
300 RTAvlPVDestroy(&pUVM->dbgf.s.AsHandleTree, dbgfR3AsTermDestroyNode, NULL);
301 for (size_t i = 0; i < RT_ELEMENTS(pUVM->dbgf.s.ahAsAliases); i++)
302 {
303 RTDbgAsRelease(pUVM->dbgf.s.ahAsAliases[i]);
304 pUVM->dbgf.s.ahAsAliases[i] = NIL_RTDBGAS;
305 }
306
307 /*
308 * Release the reference to the debugging config.
309 */
310 rc = RTDbgCfgRelease(pUVM->dbgf.s.hDbgCfg);
311 AssertRC(rc);
312}
313
314
315/**
316 * Relocates the RC address space.
317 *
318 * @param pUVM The user mode VM handle.
319 * @param offDelta The relocation delta.
320 */
321void dbgfR3AsRelocate(PUVM pUVM, RTGCUINTPTR offDelta)
322{
323 /*
324 * We will relocate the raw-mode context modules by offDelta if they have
325 * been injected into the DBGF_AS_RC map.
326 */
327 if ( pUVM->dbgf.s.afAsAliasPopuplated[DBGF_AS_ALIAS_2_INDEX(DBGF_AS_RC)]
328 && offDelta != 0)
329 {
330 RTDBGAS hAs = pUVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(DBGF_AS_RC)];
331
332 /* Take a snapshot of the modules as we might have overlapping
333 addresses between the previous and new mapping. */
334 RTDbgAsLockExcl(hAs);
335 uint32_t cModules = RTDbgAsModuleCount(hAs);
336 if (cModules > 0 && cModules < _4K)
337 {
338 struct DBGFASRELOCENTRY
339 {
340 RTDBGMOD hDbgMod;
341 RTRCPTR uOldAddr;
342 } *paEntries = (struct DBGFASRELOCENTRY *)RTMemTmpAllocZ(sizeof(paEntries[0]) * cModules);
343 if (paEntries)
344 {
345 /* Snapshot. */
346 for (uint32_t i = 0; i < cModules; i++)
347 {
348 paEntries[i].hDbgMod = RTDbgAsModuleByIndex(hAs, i);
349 AssertLogRelMsg(paEntries[i].hDbgMod != NIL_RTDBGMOD, ("iModule=%#x\n", i));
350
351 RTDBGASMAPINFO aMappings[1] = { { 0, 0 } };
352 uint32_t cMappings = 1;
353 int rc = RTDbgAsModuleQueryMapByIndex(hAs, i, &aMappings[0], &cMappings, 0 /*fFlags*/);
354 if (RT_SUCCESS(rc) && cMappings == 1 && aMappings[0].iSeg == NIL_RTDBGSEGIDX)
355 paEntries[i].uOldAddr = (RTRCPTR)aMappings[0].Address;
356 else
357 AssertLogRelMsgFailed(("iModule=%#x rc=%Rrc cMappings=%#x.\n", i, rc, cMappings));
358 }
359
360 /* Unlink them. */
361 for (uint32_t i = 0; i < cModules; i++)
362 {
363 int rc = RTDbgAsModuleUnlink(hAs, paEntries[i].hDbgMod);
364 AssertLogRelMsg(RT_SUCCESS(rc), ("iModule=%#x rc=%Rrc hDbgMod=%p\n", i, rc, paEntries[i].hDbgMod));
365 }
366
367 /* Link them at the new locations. */
368 for (uint32_t i = 0; i < cModules; i++)
369 {
370 RTRCPTR uNewAddr = paEntries[i].uOldAddr + offDelta;
371 int rc = RTDbgAsModuleLink(hAs, paEntries[i].hDbgMod, uNewAddr,
372 RTDBGASLINK_FLAGS_REPLACE);
373 AssertLogRelMsg(RT_SUCCESS(rc),
374 ("iModule=%#x rc=%Rrc hDbgMod=%p %RRv -> %RRv\n", i, rc, paEntries[i].hDbgMod,
375 paEntries[i].uOldAddr, uNewAddr));
376 RTDbgModRelease(paEntries[i].hDbgMod);
377 }
378
379 RTMemTmpFree(paEntries);
380 }
381 else
382 AssertLogRelMsgFailed(("No memory for %#x modules.\n", cModules));
383 }
384 else
385 AssertLogRelMsgFailed(("cModules=%#x\n", cModules));
386 RTDbgAsUnlockExcl(hAs);
387 }
388}
389
390
391/**
392 * Gets the IPRT debugging configuration handle (no refs retained).
393 *
394 * @returns Config handle or NIL_RTDBGCFG.
395 * @param pUVM The user mode VM handle.
396 */
397VMMR3DECL(RTDBGCFG) DBGFR3AsGetConfig(PUVM pUVM)
398{
399 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NIL_RTDBGCFG);
400 return pUVM->dbgf.s.hDbgCfg;
401}
402
403
404/**
405 * Adds the address space to the database.
406 *
407 * @returns VBox status code.
408 * @param pUVM The user mode VM handle.
409 * @param hDbgAs The address space handle. The reference of the caller
410 * will NOT be consumed.
411 * @param ProcId The process id or NIL_RTPROCESS.
412 */
413VMMR3DECL(int) DBGFR3AsAdd(PUVM pUVM, RTDBGAS hDbgAs, RTPROCESS ProcId)
414{
415 /*
416 * Input validation.
417 */
418 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
419 const char *pszName = RTDbgAsName(hDbgAs);
420 if (!pszName)
421 return VERR_INVALID_HANDLE;
422 uint32_t cRefs = RTDbgAsRetain(hDbgAs);
423 if (cRefs == UINT32_MAX)
424 return VERR_INVALID_HANDLE;
425
426 /*
427 * Allocate a tracking node.
428 */
429 int rc = VERR_NO_MEMORY;
430 PDBGFASDBNODE pDbNode = (PDBGFASDBNODE)MMR3HeapAllocU(pUVM, MM_TAG_DBGF_AS, sizeof(*pDbNode));
431 if (pDbNode)
432 {
433 pDbNode->HandleCore.Key = hDbgAs;
434 pDbNode->PidCore.Key = ProcId;
435 pDbNode->NameCore.pszString = pszName;
436 pDbNode->NameCore.cchString = strlen(pszName);
437 DBGF_AS_DB_LOCK_WRITE(pUVM);
438 if (RTStrSpaceInsert(&pUVM->dbgf.s.AsNameSpace, &pDbNode->NameCore))
439 {
440 if (RTAvlPVInsert(&pUVM->dbgf.s.AsHandleTree, &pDbNode->HandleCore))
441 {
442 DBGF_AS_DB_UNLOCK_WRITE(pUVM);
443 return VINF_SUCCESS;
444 }
445
446 /* bail out */
447 RTStrSpaceRemove(&pUVM->dbgf.s.AsNameSpace, pszName);
448 }
449 DBGF_AS_DB_UNLOCK_WRITE(pUVM);
450 MMR3HeapFree(pDbNode);
451 }
452 RTDbgAsRelease(hDbgAs);
453 return rc;
454}
455
456
457/**
458 * Delete an address space from the database.
459 *
460 * The address space must not be engaged as any of the standard aliases.
461 *
462 * @returns VBox status code.
463 * @retval VERR_SHARING_VIOLATION if in use as an alias.
464 * @retval VERR_NOT_FOUND if not found in the address space database.
465 *
466 * @param pUVM The user mode VM handle.
467 * @param hDbgAs The address space handle. Aliases are not allowed.
468 */
469VMMR3DECL(int) DBGFR3AsDelete(PUVM pUVM, RTDBGAS hDbgAs)
470{
471 /*
472 * Input validation. Retain the address space so it can be released outside
473 * the lock as well as validated.
474 */
475 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
476 if (hDbgAs == NIL_RTDBGAS)
477 return VINF_SUCCESS;
478 uint32_t cRefs = RTDbgAsRetain(hDbgAs);
479 if (cRefs == UINT32_MAX)
480 return VERR_INVALID_HANDLE;
481 RTDbgAsRelease(hDbgAs);
482
483 DBGF_AS_DB_LOCK_WRITE(pUVM);
484
485 /*
486 * You cannot delete any of the aliases.
487 */
488 for (size_t i = 0; i < RT_ELEMENTS(pUVM->dbgf.s.ahAsAliases); i++)
489 if (pUVM->dbgf.s.ahAsAliases[i] == hDbgAs)
490 {
491 DBGF_AS_DB_UNLOCK_WRITE(pUVM);
492 return VERR_SHARING_VIOLATION;
493 }
494
495 /*
496 * Ok, try remove it from the database.
497 */
498 PDBGFASDBNODE pDbNode = (PDBGFASDBNODE)RTAvlPVRemove(&pUVM->dbgf.s.AsHandleTree, hDbgAs);
499 if (!pDbNode)
500 {
501 DBGF_AS_DB_UNLOCK_WRITE(pUVM);
502 return VERR_NOT_FOUND;
503 }
504 RTStrSpaceRemove(&pUVM->dbgf.s.AsNameSpace, pDbNode->NameCore.pszString);
505 if (pDbNode->PidCore.Key != NIL_RTPROCESS)
506 RTAvlU32Remove(&pUVM->dbgf.s.AsPidTree, pDbNode->PidCore.Key);
507
508 DBGF_AS_DB_UNLOCK_WRITE(pUVM);
509
510 /*
511 * Free the resources.
512 */
513 RTDbgAsRelease(hDbgAs);
514 MMR3HeapFree(pDbNode);
515
516 return VINF_SUCCESS;
517}
518
519
520/**
521 * Changes an alias to point to a new address space.
522 *
523 * Not all the aliases can be changed, currently it's only DBGF_AS_GLOBAL
524 * and DBGF_AS_KERNEL.
525 *
526 * @returns VBox status code.
527 * @param pUVM The user mode VM handle.
528 * @param hAlias The alias to change.
529 * @param hAliasFor The address space hAlias should be an alias for. This
530 * can be an alias. The caller's reference to this address
531 * space will NOT be consumed.
532 */
533VMMR3DECL(int) DBGFR3AsSetAlias(PUVM pUVM, RTDBGAS hAlias, RTDBGAS hAliasFor)
534{
535 /*
536 * Input validation.
537 */
538 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
539 AssertMsgReturn(DBGF_AS_IS_ALIAS(hAlias), ("%p\n", hAlias), VERR_INVALID_PARAMETER);
540 AssertMsgReturn(!DBGF_AS_IS_FIXED_ALIAS(hAlias), ("%p\n", hAlias), VERR_INVALID_PARAMETER);
541 RTDBGAS hRealAliasFor = DBGFR3AsResolveAndRetain(pUVM, hAliasFor);
542 if (hRealAliasFor == NIL_RTDBGAS)
543 return VERR_INVALID_HANDLE;
544
545 /*
546 * Make sure the handle has is already in the database.
547 */
548 int rc = VERR_NOT_FOUND;
549 DBGF_AS_DB_LOCK_WRITE(pUVM);
550 if (RTAvlPVGet(&pUVM->dbgf.s.AsHandleTree, hRealAliasFor))
551 {
552 /*
553 * Update the alias table and release the current address space.
554 */
555 RTDBGAS hAsOld;
556 ASMAtomicXchgHandle(&pUVM->dbgf.s.ahAsAliases[DBGF_AS_ALIAS_2_INDEX(hAlias)], hRealAliasFor, &hAsOld);
557 uint32_t cRefs = RTDbgAsRelease(hAsOld);
558 Assert(cRefs > 0); Assert(cRefs != UINT32_MAX); NOREF(cRefs);
559 rc = VINF_SUCCESS;
560 }
561 DBGF_AS_DB_UNLOCK_WRITE(pUVM);
562
563 return rc;
564}
565
566
567/**
568 * @callback_method_impl{FNPDMR3ENUM}
569 */
570static DECLCALLBACK(int) dbgfR3AsLazyPopulateR0Callback(PVM pVM, const char *pszFilename, const char *pszName,
571 RTUINTPTR ImageBase, size_t cbImage, PDMLDRCTX enmCtx, void *pvArg)
572{
573 NOREF(pVM); NOREF(cbImage);
574
575 /* Only ring-0 modules. */
576 if (enmCtx == PDMLDRCTX_RING_0)
577 {
578 RTDBGMOD hDbgMod;
579 int rc = RTDbgModCreateFromImage(&hDbgMod, pszFilename, pszName, RTLDRARCH_HOST, pVM->pUVM->dbgf.s.hDbgCfg);
580 if (RT_SUCCESS(rc))
581 {
582 rc = RTDbgAsModuleLink((RTDBGAS)pvArg, hDbgMod, ImageBase, 0 /*fFlags*/);
583 if (RT_FAILURE(rc))
584 LogRel(("DBGF: Failed to link module \"%s\" into DBGF_AS_R0 at %RTptr: %Rrc\n",
585 pszName, ImageBase, rc));
586 }
587 else
588 LogRel(("DBGF: RTDbgModCreateFromImage failed with rc=%Rrc for module \"%s\" (%s)\n",
589 rc, pszName, pszFilename));
590 }
591 return VINF_SUCCESS;
592}
593
594
595/**
596 * @callback_method_impl{FNPDMR3ENUM}
597 */
598static DECLCALLBACK(int) dbgfR3AsLazyPopulateRCCallback(PVM pVM, const char *pszFilename, const char *pszName,
599 RTUINTPTR ImageBase, size_t cbImage, PDMLDRCTX enmCtx, void *pvArg)
600{
601 NOREF(pVM); NOREF(cbImage);
602
603 /* Only raw-mode modules. */
604 if (enmCtx == PDMLDRCTX_RAW_MODE)
605 {
606 RTDBGMOD hDbgMod;
607 int rc = RTDbgModCreateFromImage(&hDbgMod, pszFilename, pszName, RTLDRARCH_X86_32, pVM->pUVM->dbgf.s.hDbgCfg);
608 if (RT_SUCCESS(rc))
609 {
610 rc = RTDbgAsModuleLink((RTDBGAS)pvArg, hDbgMod, ImageBase, 0 /*fFlags*/);
611 if (RT_FAILURE(rc))
612 LogRel(("DBGF: Failed to link module \"%s\" into DBGF_AS_RC at %RTptr: %Rrc\n",
613 pszName, ImageBase, rc));
614 }
615 else
616 LogRel(("DBGF: RTDbgModCreateFromImage failed with rc=%Rrc for module \"%s\" (%s)\n",
617 rc, pszName, pszFilename));
618 }
619 return VINF_SUCCESS;
620}
621
622
623/**
624 * Lazily populates the specified address space.
625 *
626 * @param pUVM The user mode VM handle.
627 * @param hAlias The alias.
628 */
629static void dbgfR3AsLazyPopulate(PUVM pUVM, RTDBGAS hAlias)
630{
631 DBGF_AS_DB_LOCK_WRITE(pUVM);
632 uintptr_t iAlias = DBGF_AS_ALIAS_2_INDEX(hAlias);
633 if (!pUVM->dbgf.s.afAsAliasPopuplated[iAlias])
634 {
635 RTDBGAS hDbgAs = pUVM->dbgf.s.ahAsAliases[iAlias];
636 if (hAlias == DBGF_AS_R0 && pUVM->pVM)
637 PDMR3LdrEnumModules(pUVM->pVM, dbgfR3AsLazyPopulateR0Callback, hDbgAs);
638 else if (hAlias == DBGF_AS_RC && pUVM->pVM && !HMIsEnabled(pUVM->pVM))
639 {
640 LogRel(("DBGF: Lazy init of RC address space\n"));
641 PDMR3LdrEnumModules(pUVM->pVM, dbgfR3AsLazyPopulateRCCallback, hDbgAs);
642#ifdef VBOX_WITH_RAW_MODE
643 PATMR3DbgPopulateAddrSpace(pUVM->pVM, hDbgAs);
644#endif
645 }
646 else if (hAlias == DBGF_AS_PHYS && pUVM->pVM)
647 {
648 /** @todo Lazy load pc and vga bios symbols or the EFI stuff. */
649 }
650
651 pUVM->dbgf.s.afAsAliasPopuplated[iAlias] = true;
652 }
653 DBGF_AS_DB_UNLOCK_WRITE(pUVM);
654}
655
656
657/**
658 * Resolves the address space handle into a real handle if it's an alias.
659 *
660 * @returns Real address space handle. NIL_RTDBGAS if invalid handle.
661 *
662 * @param pUVM The user mode VM handle.
663 * @param hAlias The possibly address space alias.
664 *
665 * @remarks Doesn't take any locks.
666 */
667VMMR3DECL(RTDBGAS) DBGFR3AsResolve(PUVM pUVM, RTDBGAS hAlias)
668{
669 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
670 AssertCompileNS(NIL_RTDBGAS == (RTDBGAS)0);
671
672 uintptr_t iAlias = DBGF_AS_ALIAS_2_INDEX(hAlias);
673 if (iAlias < DBGF_AS_COUNT)
674 ASMAtomicReadHandle(&pUVM->dbgf.s.ahAsAliases[iAlias], &hAlias);
675 return hAlias;
676}
677
678
679/**
680 * Resolves the address space handle into a real handle if it's an alias,
681 * and retains whatever it is.
682 *
683 * @returns Real address space handle. NIL_RTDBGAS if invalid handle.
684 *
685 * @param pUVM The user mode VM handle.
686 * @param hAlias The possibly address space alias.
687 */
688VMMR3DECL(RTDBGAS) DBGFR3AsResolveAndRetain(PUVM pUVM, RTDBGAS hAlias)
689{
690 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
691 AssertCompileNS(NIL_RTDBGAS == (RTDBGAS)0);
692
693 uint32_t cRefs;
694 uintptr_t iAlias = DBGF_AS_ALIAS_2_INDEX(hAlias);
695 if (iAlias < DBGF_AS_COUNT)
696 {
697 if (DBGF_AS_IS_FIXED_ALIAS(hAlias))
698 {
699 /* Perform lazy address space population. */
700 if (!pUVM->dbgf.s.afAsAliasPopuplated[iAlias])
701 dbgfR3AsLazyPopulate(pUVM, hAlias);
702
703 /* Won't ever change, no need to grab the lock. */
704 hAlias = pUVM->dbgf.s.ahAsAliases[iAlias];
705 cRefs = RTDbgAsRetain(hAlias);
706 }
707 else
708 {
709 /* May change, grab the lock so we can read it safely. */
710 DBGF_AS_DB_LOCK_READ(pUVM);
711 hAlias = pUVM->dbgf.s.ahAsAliases[iAlias];
712 cRefs = RTDbgAsRetain(hAlias);
713 DBGF_AS_DB_UNLOCK_READ(pUVM);
714 }
715 }
716 else
717 /* Not an alias, just retain it. */
718 cRefs = RTDbgAsRetain(hAlias);
719
720 return cRefs != UINT32_MAX ? hAlias : NIL_RTDBGAS;
721}
722
723
724/**
725 * Query an address space by name.
726 *
727 * @returns Retained address space handle if found, NIL_RTDBGAS if not.
728 *
729 * @param pUVM The user mode VM handle.
730 * @param pszName The name.
731 */
732VMMR3DECL(RTDBGAS) DBGFR3AsQueryByName(PUVM pUVM, const char *pszName)
733{
734 /*
735 * Validate the input.
736 */
737 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NIL_RTDBGAS);
738 AssertPtrReturn(pszName, NIL_RTDBGAS);
739 AssertReturn(*pszName, NIL_RTDBGAS);
740
741 /*
742 * Look it up in the string space and retain the result.
743 */
744 RTDBGAS hDbgAs = NIL_RTDBGAS;
745 DBGF_AS_DB_LOCK_READ(pUVM);
746
747 PRTSTRSPACECORE pNode = RTStrSpaceGet(&pUVM->dbgf.s.AsNameSpace, pszName);
748 if (pNode)
749 {
750 PDBGFASDBNODE pDbNode = RT_FROM_MEMBER(pNode, DBGFASDBNODE, NameCore);
751 hDbgAs = (RTDBGAS)pDbNode->HandleCore.Key;
752 uint32_t cRefs = RTDbgAsRetain(hDbgAs);
753 if (RT_UNLIKELY(cRefs == UINT32_MAX))
754 hDbgAs = NIL_RTDBGAS;
755 }
756
757 DBGF_AS_DB_UNLOCK_READ(pUVM);
758 return hDbgAs;
759}
760
761
762/**
763 * Query an address space by process ID.
764 *
765 * @returns Retained address space handle if found, NIL_RTDBGAS if not.
766 *
767 * @param pUVM The user mode VM handle.
768 * @param ProcId The process ID.
769 */
770VMMR3DECL(RTDBGAS) DBGFR3AsQueryByPid(PUVM pUVM, RTPROCESS ProcId)
771{
772 /*
773 * Validate the input.
774 */
775 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NIL_RTDBGAS);
776 AssertReturn(ProcId != NIL_RTPROCESS, NIL_RTDBGAS);
777
778 /*
779 * Look it up in the PID tree and retain the result.
780 */
781 RTDBGAS hDbgAs = NIL_RTDBGAS;
782 DBGF_AS_DB_LOCK_READ(pUVM);
783
784 PAVLU32NODECORE pNode = RTAvlU32Get(&pUVM->dbgf.s.AsPidTree, ProcId);
785 if (pNode)
786 {
787 PDBGFASDBNODE pDbNode = RT_FROM_MEMBER(pNode, DBGFASDBNODE, PidCore);
788 hDbgAs = (RTDBGAS)pDbNode->HandleCore.Key;
789 uint32_t cRefs = RTDbgAsRetain(hDbgAs);
790 if (RT_UNLIKELY(cRefs == UINT32_MAX))
791 hDbgAs = NIL_RTDBGAS;
792 }
793 DBGF_AS_DB_UNLOCK_READ(pUVM);
794
795 return hDbgAs;
796}
797
798#if 0 /* unused */
799
800/**
801 * Searches for the file in the path.
802 *
803 * The file is first tested without any path modification, then we walk the path
804 * looking in each directory.
805 *
806 * @returns VBox status code.
807 * @param pszFilename The file to search for.
808 * @param pszPath The search path.
809 * @param pfnOpen The open callback function.
810 * @param pvUser User argument for the callback.
811 */
812static int dbgfR3AsSearchPath(const char *pszFilename, const char *pszPath, PFNDBGFR3ASSEARCHOPEN pfnOpen, void *pvUser)
813{
814 char szFound[RTPATH_MAX];
815
816 /* Check the filename length. */
817 size_t const cchFilename = strlen(pszFilename);
818 if (cchFilename >= sizeof(szFound))
819 return VERR_FILENAME_TOO_LONG;
820 const char *pszName = RTPathFilename(pszFilename);
821 if (!pszName)
822 return VERR_IS_A_DIRECTORY;
823 size_t const cchName = strlen(pszName);
824
825 /*
826 * Try default location first.
827 */
828 memcpy(szFound, pszFilename, cchFilename + 1);
829 int rc = pfnOpen(szFound, pvUser);
830 if (RT_SUCCESS(rc))
831 return rc;
832
833 /*
834 * Walk the search path.
835 */
836 const char *psz = pszPath;
837 while (*psz)
838 {
839 /* Skip leading blanks - no directories with leading spaces, thank you. */
840 while (RT_C_IS_BLANK(*psz))
841 psz++;
842
843 /* Find the end of this element. */
844 const char *pszNext;
845 const char *pszEnd = strchr(psz, ';');
846 if (!pszEnd)
847 pszEnd = pszNext = strchr(psz, '\0');
848 else
849 pszNext = pszEnd + 1;
850 if (pszEnd != psz)
851 {
852 size_t const cch = pszEnd - psz;
853 if (cch + 1 + cchName < sizeof(szFound))
854 {
855 /** @todo RTPathCompose, RTPathComposeN(). This code isn't right
856 * for 'E:' on DOS systems. It may also create unwanted double slashes. */
857 memcpy(szFound, psz, cch);
858 szFound[cch] = '/';
859 memcpy(szFound + cch + 1, pszName, cchName + 1);
860 int rc2 = pfnOpen(szFound, pvUser);
861 if (RT_SUCCESS(rc2))
862 return rc2;
863 if ( rc2 != rc
864 && ( rc == VERR_FILE_NOT_FOUND
865 || rc == VERR_OPEN_FAILED))
866 rc = rc2;
867 }
868 }
869
870 /* advance */
871 psz = pszNext;
872 }
873
874 /*
875 * Walk the path once again, this time do a depth search.
876 */
877 /** @todo do a depth search using the specified path. */
878
879 /* failed */
880 return rc;
881}
882
883
884/**
885 * Same as dbgfR3AsSearchEnv, except that the path is taken from the environment.
886 *
887 * If the environment variable doesn't exist, the current directory is searched
888 * instead.
889 *
890 * @returns VBox status code.
891 * @param pszFilename The filename.
892 * @param pszEnvVar The environment variable name.
893 * @param pfnOpen The open callback function.
894 * @param pvUser User argument for the callback.
895 */
896static int dbgfR3AsSearchEnvPath(const char *pszFilename, const char *pszEnvVar, PFNDBGFR3ASSEARCHOPEN pfnOpen, void *pvUser)
897{
898 int rc;
899 char *pszPath = RTEnvDupEx(RTENV_DEFAULT, pszEnvVar);
900 if (pszPath)
901 {
902 rc = dbgfR3AsSearchPath(pszFilename, pszPath, pfnOpen, pvUser);
903 RTStrFree(pszPath);
904 }
905 else
906 rc = dbgfR3AsSearchPath(pszFilename, ".", pfnOpen, pvUser);
907 return rc;
908}
909
910
911/**
912 * Same as dbgfR3AsSearchEnv, except that the path is taken from the DBGF config
913 * (CFGM).
914 *
915 * Nothing is done if the CFGM variable isn't set.
916 *
917 * @returns VBox status code.
918 * @param pUVM The user mode VM handle.
919 * @param pszFilename The filename.
920 * @param pszCfgValue The name of the config variable (under /DBGF/).
921 * @param pfnOpen The open callback function.
922 * @param pvUser User argument for the callback.
923 */
924static int dbgfR3AsSearchCfgPath(PUVM pUVM, const char *pszFilename, const char *pszCfgValue,
925 PFNDBGFR3ASSEARCHOPEN pfnOpen, void *pvUser)
926{
927 char *pszPath;
928 int rc = CFGMR3QueryStringAllocDef(CFGMR3GetChild(CFGMR3GetRootU(pUVM), "/DBGF"), pszCfgValue, &pszPath, NULL);
929 if (RT_FAILURE(rc))
930 return rc;
931 if (!pszPath)
932 return VERR_FILE_NOT_FOUND;
933 rc = dbgfR3AsSearchPath(pszFilename, pszPath, pfnOpen, pvUser);
934 MMR3HeapFree(pszPath);
935 return rc;
936}
937
938#endif /* unused */
939
940
941/**
942 * Load symbols from an executable module into the specified address space.
943 *
944 * If an module exist at the specified address it will be replaced by this
945 * call, otherwise a new module is created.
946 *
947 * @returns VBox status code.
948 *
949 * @param pUVM The user mode VM handle.
950 * @param hDbgAs The address space.
951 * @param pszFilename The filename of the executable module.
952 * @param pszModName The module name. If NULL, then then the file name
953 * base is used (no extension or nothing).
954 * @param enmArch The desired architecture, use RTLDRARCH_WHATEVER if
955 * it's not relevant or known.
956 * @param pModAddress The load address of the module.
957 * @param iModSeg The segment to load, pass NIL_RTDBGSEGIDX to load
958 * the whole image.
959 * @param fFlags Flags reserved for future extensions, must be 0.
960 */
961VMMR3DECL(int) DBGFR3AsLoadImage(PUVM pUVM, RTDBGAS hDbgAs, const char *pszFilename, const char *pszModName, RTLDRARCH enmArch,
962 PCDBGFADDRESS pModAddress, RTDBGSEGIDX iModSeg, uint32_t fFlags)
963{
964 /*
965 * Validate input
966 */
967 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
968 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
969 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
970 AssertReturn(DBGFR3AddrIsValid(pUVM, pModAddress), VERR_INVALID_PARAMETER);
971 AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER);
972 RTDBGAS hRealAS = DBGFR3AsResolveAndRetain(pUVM, hDbgAs);
973 if (hRealAS == NIL_RTDBGAS)
974 return VERR_INVALID_HANDLE;
975
976 RTDBGMOD hDbgMod;
977 int rc = RTDbgModCreateFromImage(&hDbgMod, pszFilename, pszModName, enmArch, pUVM->dbgf.s.hDbgCfg);
978 if (RT_SUCCESS(rc))
979 {
980 rc = DBGFR3AsLinkModule(pUVM, hRealAS, hDbgMod, pModAddress, iModSeg, 0);
981 if (RT_FAILURE(rc))
982 RTDbgModRelease(hDbgMod);
983 }
984
985 RTDbgAsRelease(hRealAS);
986 return rc;
987}
988
989
990/**
991 * Load symbols from a map file into a module at the specified address space.
992 *
993 * If an module exist at the specified address it will be replaced by this
994 * call, otherwise a new module is created.
995 *
996 * @returns VBox status code.
997 *
998 * @param pUVM The user mode VM handle.
999 * @param hDbgAs The address space.
1000 * @param pszFilename The map file.
1001 * @param pszModName The module name. If NULL, then then the file name
1002 * base is used (no extension or nothing).
1003 * @param pModAddress The load address of the module.
1004 * @param iModSeg The segment to load, pass NIL_RTDBGSEGIDX to load
1005 * the whole image.
1006 * @param uSubtrahend Value to to subtract from the symbols in the map
1007 * file. This is useful for the linux System.map and
1008 * /proc/kallsyms.
1009 * @param fFlags Flags reserved for future extensions, must be 0.
1010 */
1011VMMR3DECL(int) DBGFR3AsLoadMap(PUVM pUVM, RTDBGAS hDbgAs, const char *pszFilename, const char *pszModName,
1012 PCDBGFADDRESS pModAddress, RTDBGSEGIDX iModSeg, RTGCUINTPTR uSubtrahend, uint32_t fFlags)
1013{
1014 /*
1015 * Validate input
1016 */
1017 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1018 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1019 AssertReturn(*pszFilename, VERR_INVALID_PARAMETER);
1020 AssertReturn(DBGFR3AddrIsValid(pUVM, pModAddress), VERR_INVALID_PARAMETER);
1021 AssertReturn(fFlags == 0, VERR_INVALID_PARAMETER);
1022 RTDBGAS hRealAS = DBGFR3AsResolveAndRetain(pUVM, hDbgAs);
1023 if (hRealAS == NIL_RTDBGAS)
1024 return VERR_INVALID_HANDLE;
1025
1026 RTDBGMOD hDbgMod;
1027 int rc = RTDbgModCreateFromMap(&hDbgMod, pszFilename, pszModName, uSubtrahend, pUVM->dbgf.s.hDbgCfg);
1028 if (RT_SUCCESS(rc))
1029 {
1030 rc = DBGFR3AsLinkModule(pUVM, hRealAS, hDbgMod, pModAddress, iModSeg, 0);
1031 if (RT_FAILURE(rc))
1032 RTDbgModRelease(hDbgMod);
1033 }
1034
1035 RTDbgAsRelease(hRealAS);
1036 return rc;
1037}
1038
1039
1040/**
1041 * Wrapper around RTDbgAsModuleLink, RTDbgAsModuleLinkSeg and DBGFR3AsResolve.
1042 *
1043 * @returns VBox status code.
1044 * @param pUVM The user mode VM handle.
1045 * @param hDbgAs The address space handle.
1046 * @param hMod The module handle.
1047 * @param pModAddress The link address.
1048 * @param iModSeg The segment to link, NIL_RTDBGSEGIDX for the entire image.
1049 * @param fFlags Flags to pass to the link functions, see RTDBGASLINK_FLAGS_*.
1050 */
1051VMMR3DECL(int) DBGFR3AsLinkModule(PUVM pUVM, RTDBGAS hDbgAs, RTDBGMOD hMod, PCDBGFADDRESS pModAddress,
1052 RTDBGSEGIDX iModSeg, uint32_t fFlags)
1053{
1054 /*
1055 * Input validation.
1056 */
1057 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1058 AssertReturn(DBGFR3AddrIsValid(pUVM, pModAddress), VERR_INVALID_PARAMETER);
1059 RTDBGAS hRealAS = DBGFR3AsResolveAndRetain(pUVM, hDbgAs);
1060 if (hRealAS == NIL_RTDBGAS)
1061 return VERR_INVALID_HANDLE;
1062
1063 /*
1064 * Do the job.
1065 */
1066 int rc;
1067 if (iModSeg == NIL_RTDBGSEGIDX)
1068 rc = RTDbgAsModuleLink(hRealAS, hMod, pModAddress->FlatPtr, fFlags);
1069 else
1070 rc = RTDbgAsModuleLinkSeg(hRealAS, hMod, iModSeg, pModAddress->FlatPtr, fFlags);
1071
1072 RTDbgAsRelease(hRealAS);
1073 return rc;
1074}
1075
1076
1077/**
1078 * Wrapper around RTDbgAsModuleByName and RTDbgAsModuleUnlink.
1079 *
1080 * Unlinks all mappings matching the given module name.
1081 *
1082 * @returns VBox status code.
1083 * @param pUVM The user mode VM handle.
1084 * @param hDbgAs The address space handle.
1085 * @param pszModName The name of the module to unlink.
1086 */
1087VMMR3DECL(int) DBGFR3AsUnlinkModuleByName(PUVM pUVM, RTDBGAS hDbgAs, const char *pszModName)
1088{
1089 /*
1090 * Input validation.
1091 */
1092 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1093 RTDBGAS hRealAS = DBGFR3AsResolveAndRetain(pUVM, hDbgAs);
1094 if (hRealAS == NIL_RTDBGAS)
1095 return VERR_INVALID_HANDLE;
1096
1097 /*
1098 * Do the job.
1099 */
1100 RTDBGMOD hMod;
1101 int rc = RTDbgAsModuleByName(hRealAS, pszModName, 0, &hMod);
1102 if (RT_SUCCESS(rc))
1103 {
1104 for (;;)
1105 {
1106 rc = RTDbgAsModuleUnlink(hRealAS, hMod);
1107 RTDbgModRelease(hMod);
1108 if (RT_FAILURE(rc))
1109 break;
1110 rc = RTDbgAsModuleByName(hRealAS, pszModName, 0, &hMod);
1111 if (RT_FAILURE_NP(rc))
1112 {
1113 if (rc == VERR_NOT_FOUND)
1114 rc = VINF_SUCCESS;
1115 break;
1116 }
1117 }
1118 }
1119
1120 RTDbgAsRelease(hRealAS);
1121 return rc;
1122}
1123
1124
1125/**
1126 * Adds the module name to the symbol name.
1127 *
1128 * @param pSymbol The symbol info (in/out).
1129 * @param hMod The module handle.
1130 */
1131static void dbgfR3AsSymbolJoinNames(PRTDBGSYMBOL pSymbol, RTDBGMOD hMod)
1132{
1133 /* Figure the lengths, adjust them if the result is too long. */
1134 const char *pszModName = RTDbgModName(hMod);
1135 size_t cchModName = strlen(pszModName);
1136 size_t cchSymbol = strlen(pSymbol->szName);
1137 if (cchModName + 1 + cchSymbol >= sizeof(pSymbol->szName))
1138 {
1139 if (cchModName >= sizeof(pSymbol->szName) / 4)
1140 cchModName = sizeof(pSymbol->szName) / 4;
1141 if (cchModName + 1 + cchSymbol >= sizeof(pSymbol->szName))
1142 cchSymbol = sizeof(pSymbol->szName) - cchModName - 2;
1143 Assert(cchModName + 1 + cchSymbol < sizeof(pSymbol->szName));
1144 }
1145
1146 /* Do the moving and copying. */
1147 memmove(&pSymbol->szName[cchModName + 1], &pSymbol->szName[0], cchSymbol + 1);
1148 memcpy(&pSymbol->szName[0], pszModName, cchModName);
1149 pSymbol->szName[cchModName] = '!';
1150}
1151
1152
1153/**
1154 * Query a symbol by address.
1155 *
1156 * The returned symbol is the one we consider closes to the specified address.
1157 *
1158 * @returns VBox status code. See RTDbgAsSymbolByAddr.
1159 *
1160 * @param pUVM The user mode VM handle.
1161 * @param hDbgAs The address space handle.
1162 * @param pAddress The address to lookup.
1163 * @param fFlags One of the RTDBGSYMADDR_FLAGS_XXX flags.
1164 * @param poffDisp Where to return the distance between the returned
1165 * symbol and pAddress. Optional.
1166 * @param pSymbol Where to return the symbol information. The returned
1167 * symbol name will be prefixed by the module name as
1168 * far as space allows.
1169 * @param phMod Where to return the module handle. Optional.
1170 */
1171VMMR3DECL(int) DBGFR3AsSymbolByAddr(PUVM pUVM, RTDBGAS hDbgAs, PCDBGFADDRESS pAddress, uint32_t fFlags,
1172 PRTGCINTPTR poffDisp, PRTDBGSYMBOL pSymbol, PRTDBGMOD phMod)
1173{
1174 /*
1175 * Implement the special address space aliases the lazy way.
1176 */
1177 if (hDbgAs == DBGF_AS_RC_AND_GC_GLOBAL)
1178 {
1179 int rc = DBGFR3AsSymbolByAddr(pUVM, DBGF_AS_RC, pAddress, fFlags, poffDisp, pSymbol, phMod);
1180 if (RT_FAILURE(rc))
1181 rc = DBGFR3AsSymbolByAddr(pUVM, DBGF_AS_GLOBAL, pAddress, fFlags, poffDisp, pSymbol, phMod);
1182 return rc;
1183 }
1184
1185 /*
1186 * Input validation.
1187 */
1188 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1189 AssertReturn(DBGFR3AddrIsValid(pUVM, pAddress), VERR_INVALID_PARAMETER);
1190 AssertPtrNullReturn(poffDisp, VERR_INVALID_POINTER);
1191 AssertPtrReturn(pSymbol, VERR_INVALID_POINTER);
1192 AssertPtrNullReturn(phMod, VERR_INVALID_POINTER);
1193 if (poffDisp)
1194 *poffDisp = 0;
1195 if (phMod)
1196 *phMod = NIL_RTDBGMOD;
1197 RTDBGAS hRealAS = DBGFR3AsResolveAndRetain(pUVM, hDbgAs);
1198 if (hRealAS == NIL_RTDBGAS)
1199 return VERR_INVALID_HANDLE;
1200
1201 /*
1202 * Do the lookup.
1203 */
1204 RTDBGMOD hMod;
1205 int rc = RTDbgAsSymbolByAddr(hRealAS, pAddress->FlatPtr, fFlags, poffDisp, pSymbol, &hMod);
1206 if (RT_SUCCESS(rc))
1207 {
1208 dbgfR3AsSymbolJoinNames(pSymbol, hMod);
1209 if (!phMod)
1210 RTDbgModRelease(hMod);
1211 }
1212
1213 return rc;
1214}
1215
1216
1217/**
1218 * Convenience function that combines RTDbgSymbolDup and DBGFR3AsSymbolByAddr.
1219 *
1220 * @returns Pointer to the symbol on success. This must be free using
1221 * RTDbgSymbolFree(). NULL is returned if not found or any error
1222 * occurs.
1223 *
1224 * @param pUVM The user mode VM handle.
1225 * @param hDbgAs See DBGFR3AsSymbolByAddr.
1226 * @param pAddress See DBGFR3AsSymbolByAddr.
1227 * @param fFlags See DBGFR3AsSymbolByAddr.
1228 * @param poffDisp See DBGFR3AsSymbolByAddr.
1229 * @param phMod See DBGFR3AsSymbolByAddr.
1230 */
1231VMMR3DECL(PRTDBGSYMBOL) DBGFR3AsSymbolByAddrA(PUVM pUVM, RTDBGAS hDbgAs, PCDBGFADDRESS pAddress, uint32_t fFlags,
1232 PRTGCINTPTR poffDisp, PRTDBGMOD phMod)
1233{
1234 RTDBGSYMBOL SymInfo;
1235 int rc = DBGFR3AsSymbolByAddr(pUVM, hDbgAs, pAddress, fFlags, poffDisp, &SymInfo, phMod);
1236 if (RT_SUCCESS(rc))
1237 return RTDbgSymbolDup(&SymInfo);
1238 return NULL;
1239}
1240
1241
1242/**
1243 * Query a symbol by name.
1244 *
1245 * The symbol can be prefixed by a module name pattern to scope the search. The
1246 * pattern is a simple string pattern with '*' and '?' as wild chars. See
1247 * RTStrSimplePatternMatch().
1248 *
1249 * @returns VBox status code. See RTDbgAsSymbolByAddr.
1250 *
1251 * @param pUVM The user mode VM handle.
1252 * @param hDbgAs The address space handle.
1253 * @param pszSymbol The symbol to search for, maybe prefixed by a
1254 * module pattern.
1255 * @param pSymbol Where to return the symbol information.
1256 * The returned symbol name will be prefixed by
1257 * the module name as far as space allows.
1258 * @param phMod Where to return the module handle. Optional.
1259 */
1260VMMR3DECL(int) DBGFR3AsSymbolByName(PUVM pUVM, RTDBGAS hDbgAs, const char *pszSymbol,
1261 PRTDBGSYMBOL pSymbol, PRTDBGMOD phMod)
1262{
1263 /*
1264 * Implement the special address space aliases the lazy way.
1265 */
1266 if (hDbgAs == DBGF_AS_RC_AND_GC_GLOBAL)
1267 {
1268 int rc = DBGFR3AsSymbolByName(pUVM, DBGF_AS_RC, pszSymbol, pSymbol, phMod);
1269 if (RT_FAILURE(rc))
1270 rc = DBGFR3AsSymbolByName(pUVM, DBGF_AS_GLOBAL, pszSymbol, pSymbol, phMod);
1271 return rc;
1272 }
1273
1274 /*
1275 * Input validation.
1276 */
1277 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1278 AssertPtrReturn(pSymbol, VERR_INVALID_POINTER);
1279 AssertPtrNullReturn(phMod, VERR_INVALID_POINTER);
1280 if (phMod)
1281 *phMod = NIL_RTDBGMOD;
1282 RTDBGAS hRealAS = DBGFR3AsResolveAndRetain(pUVM, hDbgAs);
1283 if (hRealAS == NIL_RTDBGAS)
1284 return VERR_INVALID_HANDLE;
1285
1286
1287 /*
1288 * Do the lookup.
1289 */
1290 RTDBGMOD hMod;
1291 int rc = RTDbgAsSymbolByName(hRealAS, pszSymbol, pSymbol, &hMod);
1292 if (RT_SUCCESS(rc))
1293 {
1294 dbgfR3AsSymbolJoinNames(pSymbol, hMod);
1295 if (!phMod)
1296 RTDbgModRelease(hMod);
1297 }
1298
1299 return rc;
1300}
1301
1302
1303VMMR3DECL(int) DBGFR3AsLineByAddr(PUVM pUVM, RTDBGAS hDbgAs, PCDBGFADDRESS pAddress,
1304 PRTGCINTPTR poffDisp, PRTDBGLINE pLine, PRTDBGMOD phMod)
1305{
1306 /*
1307 * Implement the special address space aliases the lazy way.
1308 */
1309 if (hDbgAs == DBGF_AS_RC_AND_GC_GLOBAL)
1310 {
1311 int rc = DBGFR3AsLineByAddr(pUVM, DBGF_AS_RC, pAddress, poffDisp, pLine, phMod);
1312 if (RT_FAILURE(rc))
1313 rc = DBGFR3AsLineByAddr(pUVM, DBGF_AS_GLOBAL, pAddress, poffDisp, pLine, phMod);
1314 return rc;
1315 }
1316
1317 /*
1318 * Input validation.
1319 */
1320 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1321 AssertReturn(DBGFR3AddrIsValid(pUVM, pAddress), VERR_INVALID_PARAMETER);
1322 AssertPtrNullReturn(poffDisp, VERR_INVALID_POINTER);
1323 AssertPtrReturn(pLine, VERR_INVALID_POINTER);
1324 AssertPtrNullReturn(phMod, VERR_INVALID_POINTER);
1325 if (poffDisp)
1326 *poffDisp = 0;
1327 if (phMod)
1328 *phMod = NIL_RTDBGMOD;
1329 RTDBGAS hRealAS = DBGFR3AsResolveAndRetain(pUVM, hDbgAs);
1330 if (hRealAS == NIL_RTDBGAS)
1331 return VERR_INVALID_HANDLE;
1332
1333 /*
1334 * Do the lookup.
1335 */
1336 return RTDbgAsLineByAddr(hRealAS, pAddress->FlatPtr, poffDisp, pLine, phMod);
1337}
1338
1339
1340VMMR3DECL(PRTDBGLINE) DBGFR3AsLineByAddrA(PUVM pUVM, RTDBGAS hDbgAs, PCDBGFADDRESS pAddress,
1341 PRTGCINTPTR poffDisp, PRTDBGMOD phMod)
1342{
1343 RTDBGLINE Line;
1344 int rc = DBGFR3AsLineByAddr(pUVM, hDbgAs, pAddress, poffDisp, &Line, phMod);
1345 if (RT_SUCCESS(rc))
1346 return RTDbgLineDup(&Line);
1347 return NULL;
1348}
1349
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