1 | /* $Id: RTAssertShouldPanic-vbox.cpp 37233 2011-05-27 13:31:57Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Assertions, generic RTAssertShouldPanic.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <iprt/assert.h>
|
---|
32 | #include <iprt/env.h>
|
---|
33 | #include <iprt/err.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 |
|
---|
36 | /** @def VBOX_RTASSERT_WITH_GDB
|
---|
37 | * Enables the 'gdb' VBOX_ASSERT option.
|
---|
38 | */
|
---|
39 | #if defined(DOXYGEN_RUNNING) \
|
---|
40 | || ( !defined(VBOX_RTASSERT_WITH_GDB) \
|
---|
41 | && !defined(IN_GUEST) \
|
---|
42 | && !defined(RT_OS_OS2) \
|
---|
43 | && !defined(RT_OS_WINDOWS))
|
---|
44 | # define VBOX_RTASSERT_WITH_GDB
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | #ifdef VBOX_RTASSERT_WITH_GDB
|
---|
48 | # include <iprt/process.h>
|
---|
49 | # include <iprt/path.h>
|
---|
50 | # include <iprt/thread.h>
|
---|
51 | # include <iprt/asm.h>
|
---|
52 | #endif
|
---|
53 |
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Worker that we can wrap with error variable saving and restoring.
|
---|
57 | */
|
---|
58 | static bool rtAssertShouldPanicWorker(void)
|
---|
59 | {
|
---|
60 | /*
|
---|
61 | * Check for the VBOX_ASSERT variable.
|
---|
62 | */
|
---|
63 | const char *psz = RTEnvGet("VBOX_ASSERT");
|
---|
64 |
|
---|
65 | /* not defined => default behaviour. */
|
---|
66 | if (!psz)
|
---|
67 | return true;
|
---|
68 |
|
---|
69 | /* 'breakpoint' or 'panic' means default behaviour. */
|
---|
70 | if (!strcmp(psz, "breakpoint") || !strcmp(psz, "panic"))
|
---|
71 | return true;
|
---|
72 |
|
---|
73 | #ifdef VBOX_RTASSERT_WITH_GDB
|
---|
74 | /* 'gdb' - means try launch a gdb session in xterm. */
|
---|
75 | if (!strcmp(psz, "gdb"))
|
---|
76 | {
|
---|
77 | /* Did we already fire up gdb? If so, just hit the breakpoint. */
|
---|
78 | static bool volatile s_fAlreadyLaunchedGdb = false;
|
---|
79 | if (ASMAtomicUoReadBool(&s_fAlreadyLaunchedGdb))
|
---|
80 | return true;
|
---|
81 |
|
---|
82 | /* Try find a suitable terminal program. */
|
---|
83 | const char *pszTerm = RTEnvGet("VBOX_ASSERT_TERM");
|
---|
84 | if ( !pszTerm
|
---|
85 | || !RTPathExists(pszTerm))
|
---|
86 | {
|
---|
87 | pszTerm = "/usr/bin/gnome-terminal";
|
---|
88 | if (!RTPathExists(pszTerm))
|
---|
89 | {
|
---|
90 | pszTerm = "/usr/X11R6/bin/xterm";
|
---|
91 | if (!RTPathExists(pszTerm))
|
---|
92 | {
|
---|
93 | pszTerm ="/usr/bin/xterm";
|
---|
94 | if (!RTPathExists(pszTerm))
|
---|
95 | return true;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | /* And find gdb. */
|
---|
101 | const char *pszGdb = RTEnvGet("VBOX_ASSERT_GDB");
|
---|
102 | if ( !pszGdb
|
---|
103 | || !RTPathExists(pszGdb))
|
---|
104 | {
|
---|
105 | pszGdb = "/usr/bin/gdb";
|
---|
106 | if (!RTPathExists(pszGdb))
|
---|
107 | pszGdb = "gdb";
|
---|
108 | }
|
---|
109 |
|
---|
110 | /* Try spawn the process. */
|
---|
111 | char szCmd[512];
|
---|
112 | size_t cch = RTStrPrintf(szCmd, sizeof(szCmd), "%s -p %d ", pszGdb, RTProcSelf());
|
---|
113 | if (cch < sizeof(szCmd))
|
---|
114 | {
|
---|
115 | char *pszExecName = &szCmd[cch];
|
---|
116 | if (!RTProcGetExecutablePath(pszExecName, sizeof(szCmd) - cch))
|
---|
117 | *pszExecName = '\0';
|
---|
118 | }
|
---|
119 | const char *apszArgs[] =
|
---|
120 | {
|
---|
121 | pszTerm,
|
---|
122 | "-e",
|
---|
123 | szCmd,
|
---|
124 | NULL
|
---|
125 | };
|
---|
126 | RTPROCESS Process;
|
---|
127 | int rc = RTProcCreate(apszArgs[0], &apszArgs[0], RTENV_DEFAULT, 0, &Process);
|
---|
128 | if (RT_FAILURE(rc))
|
---|
129 | return false;
|
---|
130 |
|
---|
131 | ASMAtomicWriteBool(&s_fAlreadyLaunchedGdb, true);
|
---|
132 |
|
---|
133 | /* Wait for gdb to attach. */
|
---|
134 | RTThreadSleep(15000);
|
---|
135 | return true;
|
---|
136 | }
|
---|
137 | #endif
|
---|
138 |
|
---|
139 | /* '*' - don't hit the breakpoint. */
|
---|
140 | return false;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | RTDECL(bool) RTAssertShouldPanic(void)
|
---|
145 | {
|
---|
146 | /*
|
---|
147 | * Check if panicing is excluded by the the RTAssert settings first.
|
---|
148 | */
|
---|
149 | if (!RTAssertMayPanic())
|
---|
150 | return false;
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * Preserve error state variables.
|
---|
154 | */
|
---|
155 | RTERRVARS SavedErrVars;
|
---|
156 | RTErrVarsSave(&SavedErrVars);
|
---|
157 |
|
---|
158 | bool fRc = rtAssertShouldPanicWorker();
|
---|
159 |
|
---|
160 | RTErrVarsRestore(&SavedErrVars);
|
---|
161 | return fRc;
|
---|
162 | }
|
---|
163 |
|
---|