blob: ecaf6b6ac0441c843e0ba93a26ab8c5fb13ed429 [file] [log] [blame]
njn132bfcc2005-06-04 19:16:06 +00001
2/*--------------------------------------------------------------------*/
3/*--- Assertions and panics. m_libcassert.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
10 Copyright (C) 2000-2005 Julian Seward
11 jseward@acm.org
12
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
njnc7561b92005-06-19 01:24:32 +000031#include "pub_core_basics.h"
32#include "pub_core_threadstate.h"
njn132bfcc2005-06-04 19:16:06 +000033#include "pub_core_libcbase.h"
34#include "pub_core_libcassert.h"
35#include "pub_core_libcprint.h"
njnf39e9a32005-06-12 02:43:17 +000036#include "pub_core_libcproc.h"
njnc7561b92005-06-19 01:24:32 +000037#include "pub_core_main.h" // for VG_(bbs_done) -- stupid!
38#include "pub_core_options.h" // for VG_(bbs_done) -- stupid!
njn132bfcc2005-06-04 19:16:06 +000039#include "pub_core_stacktrace.h"
njn9abd6082005-06-17 21:31:45 +000040#include "pub_core_syscall.h"
njn132bfcc2005-06-04 19:16:06 +000041#include "pub_core_tooliface.h"
njnf39e9a32005-06-12 02:43:17 +000042#include "vki_unistd.h"
njn132bfcc2005-06-04 19:16:06 +000043
44/* ---------------------------------------------------------------------
45 Assertery.
46 ------------------------------------------------------------------ */
47
48#if defined(VGP_x86_linux)
49# define GET_REAL_SP_AND_FP(sp, fp) \
50 asm("movl %%esp, %0;" \
51 "movl %%ebp, %1;" \
52 : "=r" (sp),\
53 "=r" (fp));
54#elif defined(VGP_amd64_linux)
55# define GET_REAL_SP_AND_FP(sp, fp) \
56 asm("movq %%rsp, %0;" \
57 "movq %%rbp, %1;" \
58 : "=r" (sp),\
59 "=r" (fp));
60#else
61# error Unknown platform
62#endif
63
njnf39e9a32005-06-12 02:43:17 +000064/* Pull down the entire world */
65void VG_(exit)( Int status )
66{
67 (void)VG_(do_syscall1)(__NR_exit_group, status );
68 (void)VG_(do_syscall1)(__NR_exit, status );
69 /* Why are we still alive here? */
70 /*NOTREACHED*/
71 *(volatile Int *)0 = 'x';
72 vg_assert(2+2 == 5);
73}
74
njnc7561b92005-06-19 01:24:32 +000075// Print the scheduler status.
76static void pp_sched_status ( void )
77{
78 Int i;
79 VG_(printf)("\nsched status:\n");
80 VG_(printf)(" running_tid=%d\n", VG_(get_running_tid)());
81 for (i = 1; i < VG_N_THREADS; i++) {
82 if (VG_(threads)[i].status == VgTs_Empty) continue;
83 VG_(printf)( "\nThread %d: status = %s\n", i,
84 VG_(name_of_ThreadStatus)(VG_(threads)[i].status) );
85 VG_(get_and_pp_StackTrace)( i, VG_(clo_backtrace_size) );
86 }
87 VG_(printf)("\n");
88}
89
njn132bfcc2005-06-04 19:16:06 +000090__attribute__ ((noreturn))
91static void report_and_quit ( const Char* report, Addr ip, Addr sp, Addr fp )
92{
93 #define BACKTRACE_DEPTH 100 // nice and deep!
94 Addr stacktop, ips[BACKTRACE_DEPTH];
95 ThreadState *tst;
96
97 tst = VG_(get_ThreadState)( VG_(get_lwp_tid)(VG_(gettid)()) );
98
99 // If necessary, fake up an ExeContext which is of our actual real CPU
100 // state. Could cause problems if we got the panic/exception within the
101 // execontext/stack dump/symtab code. But it's better than nothing.
102 if (0 == ip && 0 == sp && 0 == fp) {
103 ip = (Addr)__builtin_return_address(0);
104 GET_REAL_SP_AND_FP(sp, fp);
105 }
106
107 stacktop = tst->os_state.valgrind_stack_base +
108 tst->os_state.valgrind_stack_szB;
109
110 VG_(get_StackTrace2)(ips, BACKTRACE_DEPTH, ip, sp, fp, sp, stacktop);
111 VG_(pp_StackTrace) (ips, BACKTRACE_DEPTH);
112
113 VG_(printf)("\nBasic block ctr is approximately %llu\n", VG_(bbs_done) );
114
njnc7561b92005-06-19 01:24:32 +0000115 pp_sched_status();
njn132bfcc2005-06-04 19:16:06 +0000116 VG_(printf)("\n");
117 VG_(printf)("Note: see also the FAQ.txt in the source distribution.\n");
118 VG_(printf)("It contains workarounds to several common problems.\n");
119 VG_(printf)("\n");
120 VG_(printf)("If that doesn't help, please report this bug to: %s\n\n",
121 report);
122 VG_(printf)("In the bug report, send all the above text, the valgrind\n");
123 VG_(printf)("version, and what Linux distro you are using. Thanks.\n\n");
124 VG_(exit)(1);
125
126 #undef BACKTRACE_DEPTH
127}
128
129void VG_(assert_fail) ( Bool isCore, const Char* expr, const Char* file,
130 Int line, const Char* fn, const HChar* format, ... )
131{
132 va_list vargs;
133 Char buf[256];
134 Char* bufptr = buf;
135 Char* component;
136 Char* bugs_to;
137
138 static Bool entered = False;
139 if (entered)
140 VG_(exit)(2);
141 entered = True;
142
143 va_start(vargs, format);
144 VG_(vsprintf) ( bufptr, format, vargs );
145 va_end(vargs);
146
147 if (isCore) {
148 component = "valgrind";
149 bugs_to = VG_BUGS_TO;
150 } else {
151 component = VG_(details).name;
152 bugs_to = VG_(details).bug_reports_to;
153 }
154
155 // Treat vg_assert2(0, "foo") specially, as a panicky abort
156 if (VG_STREQ(expr, "0")) {
157 VG_(printf)("\n%s: %s:%d (%s): the 'impossible' happened.\n",
158 component, file, line, fn, expr );
159 } else {
160 VG_(printf)("\n%s: %s:%d (%s): Assertion '%s' failed.\n",
161 component, file, line, fn, expr );
162 }
163 if (!VG_STREQ(buf, ""))
164 VG_(printf)("%s: %s\n", component, buf );
165
166 report_and_quit(bugs_to, 0,0,0);
167}
168
169__attribute__ ((noreturn))
170static void panic ( Char* name, Char* report, Char* str,
171 Addr ip, Addr sp, Addr fp )
172{
173 VG_(printf)("\n%s: the 'impossible' happened:\n %s\n", name, str);
174 report_and_quit(report, ip, sp, fp);
175}
176
177void VG_(core_panic_at) ( Char* str, Addr ip, Addr sp, Addr fp )
178{
179 panic("valgrind", VG_BUGS_TO, str, ip, sp, fp);
180}
181
182void VG_(core_panic) ( Char* str )
183{
184 VG_(core_panic_at)(str, 0,0,0);
185}
186
187void VG_(tool_panic) ( Char* str )
188{
189 panic(VG_(details).name, VG_(details).bug_reports_to, str, 0,0,0);
190}
191
192/* Print some helpful-ish text about unimplemented things, and give
193 up. */
194void VG_(unimplemented) ( Char* msg )
195{
196 VG_(message)(Vg_UserMsg, "");
197 VG_(message)(Vg_UserMsg,
198 "Valgrind detected that your program requires");
199 VG_(message)(Vg_UserMsg,
200 "the following unimplemented functionality:");
201 VG_(message)(Vg_UserMsg, " %s", msg);
202 VG_(message)(Vg_UserMsg,
203 "This may be because the functionality is hard to implement,");
204 VG_(message)(Vg_UserMsg,
205 "or because no reasonable program would behave this way,");
206 VG_(message)(Vg_UserMsg,
207 "or because nobody has yet needed it. In any case, let us know at");
208 VG_(message)(Vg_UserMsg,
209 "%s and/or try to work around the problem, if you can.", VG_BUGS_TO);
210 VG_(message)(Vg_UserMsg,
211 "");
212 VG_(message)(Vg_UserMsg,
213 "Valgrind has to exit now. Sorry. Bye!");
214 VG_(message)(Vg_UserMsg,
215 "");
njnc7561b92005-06-19 01:24:32 +0000216 pp_sched_status();
njn132bfcc2005-06-04 19:16:06 +0000217 VG_(exit)(1);
218}
219
220
221
222/*--------------------------------------------------------------------*/
223/*--- end ---*/
224/*--------------------------------------------------------------------*/
225