1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
|
/*********************************************************
* Copyright (C) 2006 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation version 2.1 and no later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*********************************************************/
/*
* panic.c --
*
* Module to encapsulate common Panic behaviors.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#ifdef _WIN32
# include <process.h>
# include <Windows.h>
#else // Posix
# include <unistd.h>
#endif // Win32 vs Posix
#include "vmware.h"
#include "productState.h"
#include "vm_version.h"
#include "log.h"
#include "panic.h"
#include "msg.h"
#include "str.h"
#include "config.h"
#include "util.h"
#if defined(_WIN32) || !defined(VMX86_TOOLS)
#include "coreDump.h"
#endif
#ifdef _WIN32
#include "win32u.h"
#endif
typedef enum {
PanicBreakLevel_Never,
PanicBreakLevel_IfDebuggerAttached,
PanicBreakLevel_Always
} PanicBreakLevel;
static struct PanicState {
Bool msgPostOnPanic;
Bool coreDumpOnPanic;
Bool loopOnPanic;
int coreDumpFlags; /* Memorize for clients without init func */
PanicBreakLevel breakOnPanic; /* XXX: should this be DEVEL only? */
char *coreDumpFile;
} panicState = { TRUE, TRUE }; /* defaults in lieu of Panic_Init() */
/*
*-----------------------------------------------------------------------------
*
* Panic_Init --
*
* Inits the panic module.
*
* Results:
* void
*
* Side effects:
* Sets panic state.
*
*-----------------------------------------------------------------------------
*/
void
Panic_Init(void)
{
panicState.coreDumpOnPanic = Config_GetBool(TRUE, "coreDumpOnPanic");
panicState.loopOnPanic = Config_GetBool(FALSE, "panic.loopOnPanic");
panicState.breakOnPanic = Config_GetLong(PanicBreakLevel_Never,
"panic.breakOnPanic");
panicState.coreDumpFlags = Config_GetLong(0, "coreDumpFlags");
}
/*
*----------------------------------------------------------------------
*
* Panic_SetPanicMsgPost --
*
* Allow the Msg_Post() on panic to be suppressed. If passed FALSE,
* then any subsequent Panics will refrain from posting the "VMWARE
* Panic:" message.
*
* Results:
* void.
*
* Side effects:
* Enables/Disables Msg_Post on Panic().
*
*----------------------------------------------------------------------
*/
void
Panic_SetPanicMsgPost(Bool postMsg)
{
panicState.msgPostOnPanic = postMsg;
}
/*
*----------------------------------------------------------------------
*
* Panic_GetPanicMsgPost --
* Returns panicState.msgPostOnPanic
*
* Results:
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
Bool
Panic_GetPanicMsgPost(void)
{
return panicState.msgPostOnPanic;
}
/*
*----------------------------------------------------------------------
*
* Panic_SetCoreDumpOnPanic --
*
* Allow the core dump on panic to be suppressed. If passed FALSE,
* then any subsequent Panics will not attempt to dump core.
*
* Results:
* void.
*
* Side effects:
* Enables/Disables core dump on Panic().
*
* Bugs:
* This really should act like Panic_Loop and Panic_Break, and just
* be Panic_CoreDumpOnPanic, and not have to export the state back
* out. That requires this module being the one that knows how to
* actually carry out the core dump, which seems like a good thing
* anyway. (Then Panic_Panic can do it too.)
*
*----------------------------------------------------------------------
*/
void
Panic_SetCoreDumpOnPanic(Bool dumpCore)
{
panicState.coreDumpOnPanic = dumpCore;
}
/*
*-----------------------------------------------------------------------------
*
* Panic_GetCoreDumpOnPanic --
*
* Returns whether panic should attempt to dump core.
*
* Results:
* TRUE if panics should dump core.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
Bool
Panic_GetCoreDumpOnPanic(void)
{
return panicState.coreDumpOnPanic;
}
/*
*-----------------------------------------------------------------------------
*
* Panic_GetCoreDumpFlags --
*
* Return the core dump flags. The reason this module knows about this
* is because it's available to everyone who wants to know, and has an
* init function which is a convenient time to read configuration info.
* Putting the same functionality elsewhere is harder.
*
* Results:
* flags if set; 0 by default.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
int
Panic_GetCoreDumpFlags(void)
{
return panicState.coreDumpFlags;
}
/*
*-----------------------------------------------------------------------------
*
* Panic_SetCoreDumpFlags --
*
* Although the core dump flags are read at init time, we may want to
* update this value later. This is especially true because the default
* value is read before the VM config is established.
*
* Results:
* None.
*
* Side effects:
* Updates global struct panicState.
*
*-----------------------------------------------------------------------------
*/
void
Panic_SetCoreDumpFlags(int flags) // IN
{
panicState.coreDumpFlags = flags;
}
/*
*-----------------------------------------------------------------------------
*
* Panic_LoopOnPanic --
*
* Loop until debugger intervention, if so configured.
*
* Results:
* void, eventually.
*
* Side effects:
* Stall for time.
*
*-----------------------------------------------------------------------------
*/
void
Panic_LoopOnPanic(void)
{
if (panicState.loopOnPanic) {
fprintf(stderr, "Looping pid=%d\n", (int)getpid());
while (panicState.loopOnPanic) {
sleep(1);
}
}
}
/*
*-----------------------------------------------------------------------------
*
* Panic_BreakOnPanic --
*
* Attract the attention of a nearby debugger.
*
* Results:
* void, eventually.
*
* Side effects:
* DebugBreak.
*
*-----------------------------------------------------------------------------
*/
void
Panic_BreakOnPanic(void)
{
#ifdef _WIN32
if (Panic_GetBreakOnPanic()) {
Warning("Panic: breaking into debugger\n");
DebugBreak();
}
#else
/* XXX: Linux experts: is there any equivalent of attaching a JIT debugger? */
#endif
}
/*
*-----------------------------------------------------------------------------
*
* Panic_SetBreakOnPanic --
*
* Allow the debug breakpoint on panic to be suppressed. If passed FALSE,
* then any subsequent Panics will not attempt to attract a debugger.
*
* Results:
* void.
*
* Side effects:
* Enables/Disables break into debugger on Panic().
*
*-----------------------------------------------------------------------------
*/
void
Panic_SetBreakOnPanic(Bool breakOnPanic)
{
panicState.breakOnPanic = breakOnPanic ? PanicBreakLevel_Always
: PanicBreakLevel_Never;
}
/*
*-----------------------------------------------------------------------------
*
* Panic_GetBreakOnPanic --
*
* Whether or not we should break into the debugger on the current
* panic iteration.
*
* Results:
* TRUE if a break is in order, FALSE otherwise
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
Bool
Panic_GetBreakOnPanic(void)
{
Bool shouldBreak = FALSE;
switch (panicState.breakOnPanic) {
case PanicBreakLevel_Never:
break;
case PanicBreakLevel_IfDebuggerAttached:
#ifdef _WIN32
{
typedef BOOL (*pfnIsDebuggerPresent)(void);
HMODULE kernelLibrary = Win32U_LoadLibrary("kernel32.dll");
if (kernelLibrary != NULL) {
pfnIsDebuggerPresent IsDebuggerPresentFn =
(pfnIsDebuggerPresent) GetProcAddress(kernelLibrary, "IsDebuggerPresent");
if (IsDebuggerPresentFn != NULL) {
shouldBreak = IsDebuggerPresentFn();
}
FreeLibrary(kernelLibrary);
}
}
#else
/* XXX: Linux experts? How do you know if you're being ptraced? */
#endif
break;
default:
case PanicBreakLevel_Always:
shouldBreak = TRUE;
break;
}
return shouldBreak;
}
/*
*-----------------------------------------------------------------------------
*
* Panic_SetCoreDumpFileName --
*
* Record the filename of a core dump file so that a subsequent
* Panic_PostPanicMsg can mention it by name.
*
* Pass NULL to say there's no core file; pass the empty string to
* say there's a core file but you don't know where; pass the name
* of the core file if you know it.
*
* Results:
* void
*
* Side effects:
* malloc; overwrites panicState.coreDumpFile
*
*-----------------------------------------------------------------------------
*/
void
Panic_SetCoreDumpFileName(const char *fileName)
{
if (panicState.coreDumpFile) {
free(panicState.coreDumpFile);
}
if (fileName) {
panicState.coreDumpFile = strdup(fileName);
} else {
panicState.coreDumpFile = NULL;
}
}
/*
*-----------------------------------------------------------------------------
*
* Panic_GetCoreDumpFileName --
*
* Returns the core dump filename if set.
*
* Results:
* coredump filename if set, NULL otherwise.
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
const char *
Panic_GetCoreDumpFileName(void)
{
return panicState.coreDumpFile;
}
/*
*-----------------------------------------------------------------------------
*
* Panic_Panic --
*
* Panic, possibly core dump
*
* A nice default implementation that basic Panic can call if you don't
* want to write your own. The VMX of course has its own.
*
* TODO: Figure out how to trigger the Mac OS X Crash Reporter.
*
* Results:
* None.
*
* Side effects:
* Death.
*
*-----------------------------------------------------------------------------
*/
void
Panic_Panic(const char *format,
va_list args)
{
char buf[1024];
static int count = 0;
Str_Vsnprintf(buf, sizeof buf, format, args);
/*
* Write the message to stderr first, so there's always
* some sort of record.
* Don't try to do anything fancy, since this is before
* panic loop detection. In particular, try not to call
* any of our functions (that may call Panic()).
*/
fputs(buf, stderr);
#ifdef _WIN32
/*
* This would nominally be Win32U_OutputDebugString. However,
* OutputDebugString is unusual in that the W version converts
* to local encoding and calls the A version.
*
* Since any such conversion is risky (read: can Panic) and
* we haven't yet hit the loop detection, we will conservatively
* dump UTF-8 via the A version.
*/
OutputDebugStringA(buf);
#endif
/*
* Make sure Panic gets logged
*/
Log_DisableThrottling();
/*
* Panic loop detection:
* first time - do the whole report and shutdown sequence
* second time - log and exit
* beyond second time - just exit
*/
switch (count++) {
case 0:
break;
case 1:
Log("%s", buf);
Log("Panic loop\n");
default:
fprintf(stderr, "Panic loop\n");
Util_ExitProcessAbruptly(1);
NOT_REACHED();
}
#ifdef _WIN32
/*
* Output again, in a way that we hope localizes correctly. Since
* we are converting, this can Panic, so it must run after loop
* detection.
*/
Win32U_OutputDebugString(buf);
#endif
/*
* Log panic information, and make sure we don't remove
* the log file on exit.
*/
Log("%s", buf);
Util_Backtrace(0);
Log_SetAlwaysKeep(TRUE);
/*
* Do the debugging steps early before we have a chance
* to double panic.
*/
Panic_DumpGuiResources();
#if defined(_WIN32) || !defined(VMX86_TOOLS)
if (Panic_GetCoreDumpOnPanic()) {
CoreDump_CoreDump();
}
#endif
Panic_LoopOnPanic();
/*
* Show pretty panic dialog.
* This is where things can go badly wrong.
*/
Panic_PostPanicMsg(buf);
/*
* Bye
*/
exit(-1);
NOT_REACHED();
}
/*
*-----------------------------------------------------------------------------
*
* Panic_DumpGuiResources --
*
* Dumps userlevel resources used by the current process.
*
* Results:
* void
*
* Side effects:
* Logs.
*
*-----------------------------------------------------------------------------
*/
void
Panic_DumpGuiResources(void)
{
#ifdef _WIN32
HANDLE hUser = Win32U_GetModuleHandle("user32.dll");
if (hUser) {
typedef DWORD (WINAPI *fnGetGuiResources)(HANDLE, DWORD);
fnGetGuiResources pGetGuiResources =
(fnGetGuiResources) GetProcAddress(hUser, "GetGuiResources");
if (pGetGuiResources) {
Warning("Win32 object usage: GDI %d, USER %d\n",
pGetGuiResources(GetCurrentProcess(), GR_GDIOBJECTS),
pGetGuiResources(GetCurrentProcess(), GR_USEROBJECTS));
}
}
#endif
}
|