blob: d0c4e98b53ed015370920b1ba343d67de27224b8 [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"
njn49b45ba2005-07-20 02:41:31 +000032#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"
njn49b45ba2005-07-20 02:41:31 +000036#include "pub_core_libcproc.h" // For VG_(gettid)()
37#include "pub_core_stacktrace.h"
njn9abd6082005-06-17 21:31:45 +000038#include "pub_core_syscall.h"
njne070c202005-06-20 23:58:15 +000039#include "pub_core_tooliface.h" // For VG_(details).{name,bug_reports_to}
njnf39e9a32005-06-12 02:43:17 +000040#include "vki_unistd.h"
njn132bfcc2005-06-04 19:16:06 +000041
42/* ---------------------------------------------------------------------
43 Assertery.
44 ------------------------------------------------------------------ */
45
njn9450d242005-06-26 20:50:05 +000046#if defined(VGP_x86_linux)
sewardj9a699e82005-11-05 14:22:03 +000047# define GET_REAL_PC_SP_AND_FP(pc, sp, fp) \
48 asm("call m_libcassert_get_ip;" \
49 "m_libcassert_get_ip: popl %0;" \
50 "movl %%esp, %1;" \
51 "movl %%ebp, %2;" \
52 : "=r" (pc),\
53 "=r" (sp),\
njn9450d242005-06-26 20:50:05 +000054 "=r" (fp));
55#elif defined(VGP_amd64_linux)
sewardj9a699e82005-11-05 14:22:03 +000056# define GET_REAL_PC_SP_AND_FP(pc, sp, fp) \
57 asm("leaq 0(%%rip), %0;" \
58 "movq %%rsp, %1;" \
59 "movq %%rbp, %2;" \
60 : "=r" (pc),\
61 "=r" (sp),\
njn9450d242005-06-26 20:50:05 +000062 "=r" (fp));
63#elif defined(VGP_ppc32_linux)
sewardjde616ec2005-11-05 14:48:03 +000064# define GET_REAL_PC_SP_AND_FP(pc, sp, fp) \
65 asm("mflr 0;" /* r0 = lr */ \
66 "bl m_libcassert_get_ip;" /* lr = pc */ \
67 "m_libcassert_get_ip:\n" \
68 "mflr %0;" \
69 "mtlr 0;" /* restore lr */ \
70 "mr %1,1;" \
71 "mr %2,1;" \
72 : "=r" (pc), \
73 "=r" (sp), \
74 "=r" (fp) \
75 : /* reads none */ \
76 : "r0" /* trashed */ );
njn9450d242005-06-26 20:50:05 +000077#else
78# error Unknown platform
79#endif
80
81#define BACKTRACE_DEPTH 100 // nice and deep!
njn3161e802005-06-20 03:38:27 +000082
njnf39e9a32005-06-12 02:43:17 +000083/* Pull down the entire world */
84void VG_(exit)( Int status )
85{
86 (void)VG_(do_syscall1)(__NR_exit_group, status );
87 (void)VG_(do_syscall1)(__NR_exit, status );
88 /* Why are we still alive here? */
89 /*NOTREACHED*/
90 *(volatile Int *)0 = 'x';
91 vg_assert(2+2 == 5);
92}
93
njnc7561b92005-06-19 01:24:32 +000094// Print the scheduler status.
njn49b45ba2005-07-20 02:41:31 +000095static void pp_sched_status ( void )
96{
97 Int i;
98 VG_(printf)("\nsched status:\n");
99 VG_(printf)(" running_tid=%d\n", VG_(get_running_tid)());
100 for (i = 1; i < VG_N_THREADS; i++) {
101 if (VG_(threads)[i].status == VgTs_Empty) continue;
102 VG_(printf)( "\nThread %d: status = %s\n", i,
103 VG_(name_of_ThreadStatus)(VG_(threads)[i].status) );
104 VG_(get_and_pp_StackTrace)( i, BACKTRACE_DEPTH );
105 }
106 VG_(printf)("\n");
107}
njnc7561b92005-06-19 01:24:32 +0000108
njn132bfcc2005-06-04 19:16:06 +0000109__attribute__ ((noreturn))
sewardjacaec5f2005-08-19 16:02:59 +0000110static void report_and_quit ( const Char* report,
111 Addr ip, Addr sp, Addr fp, Addr lr )
njn132bfcc2005-06-04 19:16:06 +0000112{
njn49b45ba2005-07-20 02:41:31 +0000113 Addr stacktop;
114 Addr ips[BACKTRACE_DEPTH];
115 ThreadState *tst = VG_(get_ThreadState)( VG_(get_lwp_tid)(VG_(gettid)()) );
116
117 // If necessary, fake up an ExeContext which is of our actual real CPU
118 // state. Could cause problems if we got the panic/exception within the
119 // execontext/stack dump/symtab code. But it's better than nothing.
120 if (0 == ip && 0 == sp && 0 == fp) {
sewardj9a699e82005-11-05 14:22:03 +0000121 GET_REAL_PC_SP_AND_FP(ip, sp, fp);
njn49b45ba2005-07-20 02:41:31 +0000122 }
123
sewardj45f4e7c2005-09-27 19:20:21 +0000124 stacktop = tst->os_state.valgrind_stack_init_SP;
njn49b45ba2005-07-20 02:41:31 +0000125
sewardjacaec5f2005-08-19 16:02:59 +0000126 VG_(get_StackTrace2)(ips, BACKTRACE_DEPTH, ip, sp, fp, lr, sp, stacktop);
njn49b45ba2005-07-20 02:41:31 +0000127 VG_(pp_StackTrace) (ips, BACKTRACE_DEPTH);
128
129 // Don't print this, as it's not terribly interesting and avoids a
130 // dependence on m_scheduler/, which would be crazy.
131 //VG_(printf)("\nBasic block ctr is approximately %llu\n", VG_(bbs_done) );
132
133 pp_sched_status();
njn132bfcc2005-06-04 19:16:06 +0000134 VG_(printf)("\n");
135 VG_(printf)("Note: see also the FAQ.txt in the source distribution.\n");
136 VG_(printf)("It contains workarounds to several common problems.\n");
137 VG_(printf)("\n");
138 VG_(printf)("If that doesn't help, please report this bug to: %s\n\n",
139 report);
140 VG_(printf)("In the bug report, send all the above text, the valgrind\n");
141 VG_(printf)("version, and what Linux distro you are using. Thanks.\n\n");
142 VG_(exit)(1);
njn132bfcc2005-06-04 19:16:06 +0000143}
144
145void VG_(assert_fail) ( Bool isCore, const Char* expr, const Char* file,
146 Int line, const Char* fn, const HChar* format, ... )
147{
148 va_list vargs;
149 Char buf[256];
njn132bfcc2005-06-04 19:16:06 +0000150 Char* component;
151 Char* bugs_to;
152
153 static Bool entered = False;
154 if (entered)
155 VG_(exit)(2);
156 entered = True;
157
158 va_start(vargs, format);
njnaba25b42005-06-21 04:26:24 +0000159 VG_(vsprintf) ( buf, format, vargs );
njn132bfcc2005-06-04 19:16:06 +0000160 va_end(vargs);
161
162 if (isCore) {
163 component = "valgrind";
164 bugs_to = VG_BUGS_TO;
165 } else {
166 component = VG_(details).name;
167 bugs_to = VG_(details).bug_reports_to;
168 }
169
170 // Treat vg_assert2(0, "foo") specially, as a panicky abort
171 if (VG_STREQ(expr, "0")) {
172 VG_(printf)("\n%s: %s:%d (%s): the 'impossible' happened.\n",
173 component, file, line, fn, expr );
174 } else {
175 VG_(printf)("\n%s: %s:%d (%s): Assertion '%s' failed.\n",
176 component, file, line, fn, expr );
177 }
178 if (!VG_STREQ(buf, ""))
179 VG_(printf)("%s: %s\n", component, buf );
180
sewardjacaec5f2005-08-19 16:02:59 +0000181 report_and_quit(bugs_to, 0,0,0,0);
njn132bfcc2005-06-04 19:16:06 +0000182}
183
184__attribute__ ((noreturn))
185static void panic ( Char* name, Char* report, Char* str,
sewardjacaec5f2005-08-19 16:02:59 +0000186 Addr ip, Addr sp, Addr fp, Addr lr )
njn132bfcc2005-06-04 19:16:06 +0000187{
188 VG_(printf)("\n%s: the 'impossible' happened:\n %s\n", name, str);
sewardjacaec5f2005-08-19 16:02:59 +0000189 report_and_quit(report, ip, sp, fp, lr);
njn132bfcc2005-06-04 19:16:06 +0000190}
191
sewardjacaec5f2005-08-19 16:02:59 +0000192void VG_(core_panic_at) ( Char* str, Addr ip, Addr sp, Addr fp, Addr lr )
njn132bfcc2005-06-04 19:16:06 +0000193{
sewardjacaec5f2005-08-19 16:02:59 +0000194 panic("valgrind", VG_BUGS_TO, str, ip, sp, fp, lr);
njn132bfcc2005-06-04 19:16:06 +0000195}
196
197void VG_(core_panic) ( Char* str )
198{
sewardjacaec5f2005-08-19 16:02:59 +0000199 VG_(core_panic_at)(str, 0,0,0,0);
njn132bfcc2005-06-04 19:16:06 +0000200}
201
202void VG_(tool_panic) ( Char* str )
203{
sewardjacaec5f2005-08-19 16:02:59 +0000204 panic(VG_(details).name, VG_(details).bug_reports_to, str, 0,0,0,0);
njn132bfcc2005-06-04 19:16:06 +0000205}
206
njn49b45ba2005-07-20 02:41:31 +0000207/* Print some helpful-ish text about unimplemented things, and give up. */
njn132bfcc2005-06-04 19:16:06 +0000208void VG_(unimplemented) ( Char* msg )
209{
210 VG_(message)(Vg_UserMsg, "");
211 VG_(message)(Vg_UserMsg,
212 "Valgrind detected that your program requires");
213 VG_(message)(Vg_UserMsg,
214 "the following unimplemented functionality:");
215 VG_(message)(Vg_UserMsg, " %s", msg);
216 VG_(message)(Vg_UserMsg,
217 "This may be because the functionality is hard to implement,");
218 VG_(message)(Vg_UserMsg,
219 "or because no reasonable program would behave this way,");
220 VG_(message)(Vg_UserMsg,
221 "or because nobody has yet needed it. In any case, let us know at");
222 VG_(message)(Vg_UserMsg,
223 "%s and/or try to work around the problem, if you can.", VG_BUGS_TO);
224 VG_(message)(Vg_UserMsg,
225 "");
226 VG_(message)(Vg_UserMsg,
227 "Valgrind has to exit now. Sorry. Bye!");
228 VG_(message)(Vg_UserMsg,
229 "");
njn49b45ba2005-07-20 02:41:31 +0000230 pp_sched_status();
njn132bfcc2005-06-04 19:16:06 +0000231 VG_(exit)(1);
232}
233
njn132bfcc2005-06-04 19:16:06 +0000234/*--------------------------------------------------------------------*/
235/*--- end ---*/
236/*--------------------------------------------------------------------*/
237