VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMGuruMeditation.cpp@ 19585

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

Enabled ring 0 stack dump for win32 only

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 19.5 KB
Line 
1/* $Id: VMMGuruMeditation.cpp 19585 2009-05-11 14:09:55Z vboxsync $ */
2/** @file
3 * VMM - The Virtual Machine Monitor, Guru Meditation Code.
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* Header Files *
24*******************************************************************************/
25#define LOG_GROUP LOG_GROUP_VMM
26#include <VBox/vmm.h>
27#include <VBox/pdmapi.h>
28#include <VBox/trpm.h>
29#include <VBox/dbgf.h>
30#include "VMMInternal.h"
31#include <VBox/vm.h>
32
33#include <VBox/err.h>
34#include <VBox/param.h>
35#include <VBox/version.h>
36#include <VBox/hwaccm.h>
37#include <iprt/assert.h>
38#include <iprt/time.h>
39#include <iprt/stream.h>
40#include <iprt/string.h>
41#include <iprt/stdarg.h>
42
43
44/*******************************************************************************
45* Structures and Typedefs *
46*******************************************************************************/
47/**
48 * Structure to pass to DBGFR3Info() and for doing all other
49 * output during fatal dump.
50 */
51typedef struct VMMR3FATALDUMPINFOHLP
52{
53 /** The helper core. */
54 DBGFINFOHLP Core;
55 /** The release logger instance. */
56 PRTLOGGER pRelLogger;
57 /** The saved release logger flags. */
58 RTUINT fRelLoggerFlags;
59 /** The logger instance. */
60 PRTLOGGER pLogger;
61 /** The saved logger flags. */
62 RTUINT fLoggerFlags;
63 /** The saved logger destination flags. */
64 RTUINT fLoggerDestFlags;
65 /** Whether to output to stderr or not. */
66 bool fStdErr;
67} VMMR3FATALDUMPINFOHLP, *PVMMR3FATALDUMPINFOHLP;
68/** Pointer to a VMMR3FATALDUMPINFOHLP structure. */
69typedef const VMMR3FATALDUMPINFOHLP *PCVMMR3FATALDUMPINFOHLP;
70
71
72/**
73 * Print formatted string.
74 *
75 * @param pHlp Pointer to this structure.
76 * @param pszFormat The format string.
77 * @param ... Arguments.
78 */
79static DECLCALLBACK(void) vmmR3FatalDumpInfoHlp_pfnPrintf(PCDBGFINFOHLP pHlp, const char *pszFormat, ...)
80{
81 va_list args;
82 va_start(args, pszFormat);
83 pHlp->pfnPrintfV(pHlp, pszFormat, args);
84 va_end(args);
85}
86
87
88/**
89 * Print formatted string.
90 *
91 * @param pHlp Pointer to this structure.
92 * @param pszFormat The format string.
93 * @param args Argument list.
94 */
95static DECLCALLBACK(void) vmmR3FatalDumpInfoHlp_pfnPrintfV(PCDBGFINFOHLP pHlp, const char *pszFormat, va_list args)
96{
97 PCVMMR3FATALDUMPINFOHLP pMyHlp = (PCVMMR3FATALDUMPINFOHLP)pHlp;
98
99 if (pMyHlp->pRelLogger)
100 {
101 va_list args2;
102 va_copy(args2, args);
103 RTLogLoggerV(pMyHlp->pRelLogger, pszFormat, args2);
104 va_end(args2);
105 }
106 if (pMyHlp->pLogger)
107 {
108 va_list args2;
109 va_copy(args2, args);
110 RTLogLoggerV(pMyHlp->pLogger, pszFormat, args);
111 va_end(args2);
112 }
113 if (pMyHlp->fStdErr)
114 {
115 va_list args2;
116 va_copy(args2, args);
117 RTStrmPrintfV(g_pStdErr, pszFormat, args);
118 va_end(args2);
119 }
120}
121
122
123/**
124 * Initializes the fatal dump output helper.
125 *
126 * @param pHlp The structure to initialize.
127 */
128static void vmmR3FatalDumpInfoHlpInit(PVMMR3FATALDUMPINFOHLP pHlp)
129{
130 memset(pHlp, 0, sizeof(*pHlp));
131
132 pHlp->Core.pfnPrintf = vmmR3FatalDumpInfoHlp_pfnPrintf;
133 pHlp->Core.pfnPrintfV = vmmR3FatalDumpInfoHlp_pfnPrintfV;
134
135 /*
136 * The loggers.
137 */
138 pHlp->pRelLogger = RTLogRelDefaultInstance();
139#ifndef LOG_ENABLED
140 if (!pHlp->pRelLogger)
141#endif
142 pHlp->pLogger = RTLogDefaultInstance();
143
144 if (pHlp->pRelLogger)
145 {
146 pHlp->fRelLoggerFlags = pHlp->pRelLogger->fFlags;
147 pHlp->pRelLogger->fFlags &= ~(RTLOGFLAGS_BUFFERED | RTLOGFLAGS_DISABLED);
148 }
149
150 if (pHlp->pLogger)
151 {
152 pHlp->fLoggerFlags = pHlp->pLogger->fFlags;
153 pHlp->fLoggerDestFlags = pHlp->pLogger->fDestFlags;
154 pHlp->pLogger->fFlags &= ~(RTLOGFLAGS_BUFFERED | RTLOGFLAGS_DISABLED);
155#ifndef DEBUG_sandervl
156 pHlp->pLogger->fDestFlags |= RTLOGDEST_DEBUGGER;
157#endif
158 }
159
160 /*
161 * Check if we need write to stderr.
162 */
163#ifdef DEBUG_sandervl
164 pHlp->fStdErr = false; /* takes too long to display here */
165#else
166 pHlp->fStdErr = (!pHlp->pRelLogger || !(pHlp->pRelLogger->fDestFlags & (RTLOGDEST_STDOUT | RTLOGDEST_STDERR)))
167 && (!pHlp->pLogger || !(pHlp->pLogger->fDestFlags & (RTLOGDEST_STDOUT | RTLOGDEST_STDERR)));
168#endif
169}
170
171
172/**
173 * Deletes the fatal dump output helper.
174 *
175 * @param pHlp The structure to delete.
176 */
177static void vmmR3FatalDumpInfoHlpDelete(PVMMR3FATALDUMPINFOHLP pHlp)
178{
179 if (pHlp->pRelLogger)
180 {
181 RTLogFlush(pHlp->pRelLogger);
182 pHlp->pRelLogger->fFlags = pHlp->fRelLoggerFlags;
183 }
184
185 if (pHlp->pLogger)
186 {
187 RTLogFlush(pHlp->pLogger);
188 pHlp->pLogger->fFlags = pHlp->fLoggerFlags;
189 pHlp->pLogger->fDestFlags = pHlp->fLoggerDestFlags;
190 }
191}
192
193
194/**
195 * Dumps the VM state on a fatal error.
196 *
197 * @param pVM VM Handle.
198 * @param pVCpu VMCPU Handle.
199 * @param rcErr VBox status code.
200 */
201VMMR3DECL(void) VMMR3FatalDump(PVM pVM, PVMCPU pVCpu, int rcErr)
202{
203 /*
204 * Create our output helper and sync it with the log settings.
205 * This helper will be used for all the output.
206 */
207 VMMR3FATALDUMPINFOHLP Hlp;
208 PCDBGFINFOHLP pHlp = &Hlp.Core;
209 vmmR3FatalDumpInfoHlpInit(&Hlp);
210
211 /*
212 * Header.
213 */
214 pHlp->pfnPrintf(pHlp,
215 "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
216 "!!\n"
217 "!! Guru Meditation %d (%Rrc)\n"
218 "!!\n",
219 rcErr, rcErr);
220
221 /*
222 * Continue according to context.
223 */
224 bool fDoneHyper = false;
225 switch (rcErr)
226 {
227 /*
228 * Hypervisor errors.
229 */
230 case VERR_VMM_RING0_ASSERTION:
231 case VINF_EM_DBG_HYPER_ASSERTION:
232 {
233 const char *pszMsg1 = VMMR3GetRZAssertMsg1(pVM);
234 while (pszMsg1 && *pszMsg1 == '\n')
235 pszMsg1++;
236 const char *pszMsg2 = VMMR3GetRZAssertMsg2(pVM);
237 while (pszMsg2 && *pszMsg2 == '\n')
238 pszMsg2++;
239 pHlp->pfnPrintf(pHlp,
240 "%s"
241 "%s",
242 pszMsg1,
243 pszMsg2);
244 if ( !pszMsg2
245 || !*pszMsg2
246 || strchr(pszMsg2, '\0')[-1] != '\n')
247 pHlp->pfnPrintf(pHlp, "\n");
248 pHlp->pfnPrintf(pHlp, "!!\n");
249 /* fall thru */
250 }
251 case VERR_TRPM_DONT_PANIC:
252 case VERR_TRPM_PANIC:
253 case VINF_EM_RAW_STALE_SELECTOR:
254 case VINF_EM_RAW_IRET_TRAP:
255 case VINF_EM_DBG_HYPER_BREAKPOINT:
256 case VINF_EM_DBG_HYPER_STEPPED:
257 {
258 /*
259 * Active trap? This is only of partial interest when in hardware
260 * assisted virtualization mode, thus the different messages.
261 */
262 uint32_t uEIP = CPUMGetHyperEIP(pVCpu);
263 TRPMEVENT enmType;
264 uint8_t u8TrapNo = 0xce;
265 RTGCUINT uErrorCode = 0xdeadface;
266 RTGCUINTPTR uCR2 = 0xdeadface;
267 int rc2 = TRPMQueryTrapAll(pVCpu, &u8TrapNo, &enmType, &uErrorCode, &uCR2);
268 if (!HWACCMR3IsActive(pVM))
269 {
270 if (RT_SUCCESS(rc2))
271 pHlp->pfnPrintf(pHlp,
272 "!! TRAP=%02x ERRCD=%RGv CR2=%RGv EIP=%RX32 Type=%d\n",
273 u8TrapNo, uErrorCode, uCR2, uEIP, enmType);
274 else
275 pHlp->pfnPrintf(pHlp,
276 "!! EIP=%RX32 NOTRAP\n",
277 uEIP);
278 }
279 else if (RT_SUCCESS(rc2))
280 pHlp->pfnPrintf(pHlp,
281 "!! ACTIVE TRAP=%02x ERRCD=%RGv CR2=%RGv PC=%RGr Type=%d (Guest!)\n",
282 u8TrapNo, uErrorCode, uCR2, CPUMGetGuestRIP(pVCpu), enmType);
283
284 /*
285 * The hypervisor dump is not relevant when we're in VT-x/AMD-V mode.
286 */
287 if (HWACCMR3IsActive(pVM))
288 {
289 pHlp->pfnPrintf(pHlp, "\n");
290#if defined(RT_OS_WINDOWS) && HC_ARCH_BITS == 32
291 /* Callstack. */
292 PCDBGFSTACKFRAME pFirstFrame;
293 DBGFADDRESS eip, ebp, esp;
294
295 eip.fFlags = DBGFADDRESS_FLAGS_RING0 | DBGFADDRESS_FLAGS_VALID;
296#if HC_ARCH_BITS == 64
297 eip.FlatPtr = eip.off = pVCpu->vmm.s.CallHostR0JmpBuf.rip;
298#else
299 eip.FlatPtr = eip.off = pVCpu->vmm.s.CallHostR0JmpBuf.eip;
300#endif
301 eip.Sel = DBGF_SEL_FLAT;
302 ebp.fFlags = DBGFADDRESS_FLAGS_RING0 | DBGFADDRESS_FLAGS_VALID;
303 ebp.FlatPtr = ebp.off = pVCpu->vmm.s.CallHostR0JmpBuf.SavedEbp;
304 ebp.Sel = DBGF_SEL_FLAT;
305 esp.fFlags = DBGFADDRESS_FLAGS_RING0 | DBGFADDRESS_FLAGS_VALID;
306 esp.Sel = DBGF_SEL_FLAT;
307 esp.FlatPtr = esp.off = pVCpu->vmm.s.CallHostR0JmpBuf.SavedEsp;
308
309 rc2 = DBGFR3StackWalkBeginEx(pVM, pVCpu->idCpu, DBGFCODETYPE_RING0, &ebp, &esp, &eip,
310 DBGFRETURNTYPE_INVALID, &pFirstFrame);
311 if (RT_SUCCESS(rc2))
312 {
313 pHlp->pfnPrintf(pHlp,
314 "!!\n"
315 "!! Call Stack:\n"
316 "!!\n"
317 "EBP Ret EBP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP Symbol [line]\n");
318 for (PCDBGFSTACKFRAME pFrame = pFirstFrame;
319 pFrame;
320 pFrame = DBGFR3StackWalkNext(pFrame))
321 {
322 pHlp->pfnPrintf(pHlp,
323 "%08RX32 %08RX32 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
324 (uint32_t)pFrame->AddrFrame.off,
325 (uint32_t)pFrame->AddrReturnFrame.off,
326 (uint32_t)pFrame->AddrReturnPC.Sel,
327 (uint32_t)pFrame->AddrReturnPC.off,
328 pFrame->Args.au32[0],
329 pFrame->Args.au32[1],
330 pFrame->Args.au32[2],
331 pFrame->Args.au32[3]);
332 pHlp->pfnPrintf(pHlp, " %RTsel:%08RGv", pFrame->AddrPC.Sel, pFrame->AddrPC.off);
333 if (pFrame->pSymPC)
334 {
335 RTGCINTPTR offDisp = pFrame->AddrPC.FlatPtr - pFrame->pSymPC->Value;
336 if (offDisp > 0)
337 pHlp->pfnPrintf(pHlp, " %s+%llx", pFrame->pSymPC->szName, (int64_t)offDisp);
338 else if (offDisp < 0)
339 pHlp->pfnPrintf(pHlp, " %s-%llx", pFrame->pSymPC->szName, -(int64_t)offDisp);
340 else
341 pHlp->pfnPrintf(pHlp, " %s", pFrame->pSymPC->szName);
342 }
343 if (pFrame->pLinePC)
344 pHlp->pfnPrintf(pHlp, " [%s @ 0i%d]", pFrame->pLinePC->szFilename, pFrame->pLinePC->uLineNo);
345 pHlp->pfnPrintf(pHlp, "\n");
346 }
347 DBGFR3StackWalkEnd(pFirstFrame);
348 }
349#endif /* defined(RT_OS_WINDOWS) && HC_ARCH_BITS == 32 */
350 }
351 else
352 {
353 /*
354 * Try figure out where eip is.
355 */
356 /* core code? */
357 if (uEIP - (RTGCUINTPTR)pVM->vmm.s.pvCoreCodeRC < pVM->vmm.s.cbCoreCode)
358 pHlp->pfnPrintf(pHlp,
359 "!! EIP is in CoreCode, offset %#x\n",
360 uEIP - (RTGCUINTPTR)pVM->vmm.s.pvCoreCodeRC);
361 else
362 { /* ask PDM */ /** @todo ask DBGFR3Sym later? */
363 char szModName[64];
364 RTRCPTR RCPtrMod;
365 char szNearSym1[260];
366 RTRCPTR RCPtrNearSym1;
367 char szNearSym2[260];
368 RTRCPTR RCPtrNearSym2;
369 int rc = PDMR3LdrQueryRCModFromPC(pVM, uEIP,
370 &szModName[0], sizeof(szModName), &RCPtrMod,
371 &szNearSym1[0], sizeof(szNearSym1), &RCPtrNearSym1,
372 &szNearSym2[0], sizeof(szNearSym2), &RCPtrNearSym2);
373 if (RT_SUCCESS(rc))
374 pHlp->pfnPrintf(pHlp,
375 "!! EIP in %s (%RRv) at rva %x near symbols:\n"
376 "!! %RRv rva %RRv off %08x %s\n"
377 "!! %RRv rva %RRv off -%08x %s\n",
378 szModName, RCPtrMod, (unsigned)(uEIP - RCPtrMod),
379 RCPtrNearSym1, RCPtrNearSym1 - RCPtrMod, (unsigned)(uEIP - RCPtrNearSym1), szNearSym1,
380 RCPtrNearSym2, RCPtrNearSym2 - RCPtrMod, (unsigned)(RCPtrNearSym2 - uEIP), szNearSym2);
381 else
382 pHlp->pfnPrintf(pHlp,
383 "!! EIP is not in any code known to VMM!\n");
384 }
385
386 /* Disassemble the instruction. */
387 char szInstr[256];
388 rc2 = DBGFR3DisasInstrEx(pVM, pVCpu->idCpu, 0, 0, DBGF_DISAS_FLAGS_CURRENT_HYPER, &szInstr[0], sizeof(szInstr), NULL);
389 if (RT_SUCCESS(rc2))
390 pHlp->pfnPrintf(pHlp,
391 "!! %s\n", szInstr);
392
393 /* Dump the hypervisor cpu state. */
394 pHlp->pfnPrintf(pHlp,
395 "!!\n"
396 "!!\n"
397 "!!\n");
398 rc2 = DBGFR3Info(pVM, "cpumhyper", "verbose", pHlp);
399 fDoneHyper = true;
400
401 /* Callstack. */
402 PCDBGFSTACKFRAME pFirstFrame;
403 rc2 = DBGFR3StackWalkBegin(pVM, pVCpu->idCpu, DBGFCODETYPE_HYPER, &pFirstFrame);
404 if (RT_SUCCESS(rc2))
405 {
406 pHlp->pfnPrintf(pHlp,
407 "!!\n"
408 "!! Call Stack:\n"
409 "!!\n"
410 "EBP Ret EBP Ret CS:EIP Arg0 Arg1 Arg2 Arg3 CS:EIP Symbol [line]\n");
411 for (PCDBGFSTACKFRAME pFrame = pFirstFrame;
412 pFrame;
413 pFrame = DBGFR3StackWalkNext(pFrame))
414 {
415 pHlp->pfnPrintf(pHlp,
416 "%08RX32 %08RX32 %04RX32:%08RX32 %08RX32 %08RX32 %08RX32 %08RX32",
417 (uint32_t)pFrame->AddrFrame.off,
418 (uint32_t)pFrame->AddrReturnFrame.off,
419 (uint32_t)pFrame->AddrReturnPC.Sel,
420 (uint32_t)pFrame->AddrReturnPC.off,
421 pFrame->Args.au32[0],
422 pFrame->Args.au32[1],
423 pFrame->Args.au32[2],
424 pFrame->Args.au32[3]);
425 pHlp->pfnPrintf(pHlp, " %RTsel:%08RGv", pFrame->AddrPC.Sel, pFrame->AddrPC.off);
426 if (pFrame->pSymPC)
427 {
428 RTGCINTPTR offDisp = pFrame->AddrPC.FlatPtr - pFrame->pSymPC->Value;
429 if (offDisp > 0)
430 pHlp->pfnPrintf(pHlp, " %s+%llx", pFrame->pSymPC->szName, (int64_t)offDisp);
431 else if (offDisp < 0)
432 pHlp->pfnPrintf(pHlp, " %s-%llx", pFrame->pSymPC->szName, -(int64_t)offDisp);
433 else
434 pHlp->pfnPrintf(pHlp, " %s", pFrame->pSymPC->szName);
435 }
436 if (pFrame->pLinePC)
437 pHlp->pfnPrintf(pHlp, " [%s @ 0i%d]", pFrame->pLinePC->szFilename, pFrame->pLinePC->uLineNo);
438 pHlp->pfnPrintf(pHlp, "\n");
439 }
440 DBGFR3StackWalkEnd(pFirstFrame);
441 }
442
443 /* raw stack */
444 pHlp->pfnPrintf(pHlp,
445 "!!\n"
446 "!! Raw stack (mind the direction). pbEMTStackRC=%RRv pbEMTStackBottomRC=%RRv\n"
447 "!!\n"
448 "%.*Rhxd\n",
449 pVCpu->vmm.s.pbEMTStackRC, pVCpu->vmm.s.pbEMTStackBottomRC,
450 VMM_STACK_SIZE, pVCpu->vmm.s.pbEMTStackR3);
451 } /* !HWACCMR3IsActive */
452 break;
453 }
454
455 default:
456 {
457 break;
458 }
459
460 } /* switch (rcErr) */
461
462
463 /*
464 * Generic info dumper loop.
465 */
466 static struct
467 {
468 const char *pszInfo;
469 const char *pszArgs;
470 } const aInfo[] =
471 {
472 { "mappings", NULL },
473 { "hma", NULL },
474 { "cpumguest", "verbose" },
475 { "cpumguestinstr", "verbose" },
476 { "cpumhyper", "verbose" },
477 { "cpumhost", "verbose" },
478 { "mode", "all" },
479 { "cpuid", "verbose" },
480 { "gdt", NULL },
481 { "ldt", NULL },
482 //{ "tss", NULL },
483 { "ioport", NULL },
484 { "mmio", NULL },
485 { "phys", NULL },
486 //{ "pgmpd", NULL }, - doesn't always work at init time...
487 { "timers", NULL },
488 { "activetimers", NULL },
489 { "handlers", "phys virt hyper stats" },
490 { "cfgm", NULL },
491 };
492 for (unsigned i = 0; i < RT_ELEMENTS(aInfo); i++)
493 {
494 if (fDoneHyper && !strcmp(aInfo[i].pszInfo, "cpumhyper"))
495 continue;
496 pHlp->pfnPrintf(pHlp,
497 "!!\n"
498 "!! {%s, %s}\n"
499 "!!\n",
500 aInfo[i].pszInfo, aInfo[i].pszArgs);
501 DBGFR3Info(pVM, aInfo[i].pszInfo, aInfo[i].pszArgs, pHlp);
502 }
503
504 /* done */
505 pHlp->pfnPrintf(pHlp,
506 "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
507
508
509 /*
510 * Delete the output instance (flushing and restoring of flags).
511 */
512 vmmR3FatalDumpInfoHlpDelete(&Hlp);
513}
514
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