blob: 5e4c5ed05bd2455374afb8a58d178cd1c5114d40 [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
sewardje4b0bf02006-06-05 23:21:15 +000010 Copyright (C) 2000-2006 Julian Seward
njn132bfcc2005-06-04 19:16:06 +000011 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}
sewardjf349d552005-11-14 17:01:01 +000040#include "pub_core_options.h" // For VG_(clo_xml)
sewardj4eee4762006-10-14 15:51:32 +000041#include "pub_core_vkiscnums.h"
njn132bfcc2005-06-04 19:16:06 +000042
43/* ---------------------------------------------------------------------
44 Assertery.
45 ------------------------------------------------------------------ */
46
njn9450d242005-06-26 20:50:05 +000047#if defined(VGP_x86_linux)
sewardj9a699e82005-11-05 14:22:03 +000048# define GET_REAL_PC_SP_AND_FP(pc, sp, fp) \
49 asm("call m_libcassert_get_ip;" \
50 "m_libcassert_get_ip: popl %0;" \
51 "movl %%esp, %1;" \
52 "movl %%ebp, %2;" \
53 : "=r" (pc),\
54 "=r" (sp),\
njn9450d242005-06-26 20:50:05 +000055 "=r" (fp));
56#elif defined(VGP_amd64_linux)
sewardj9a699e82005-11-05 14:22:03 +000057# define GET_REAL_PC_SP_AND_FP(pc, sp, fp) \
58 asm("leaq 0(%%rip), %0;" \
59 "movq %%rsp, %1;" \
60 "movq %%rbp, %2;" \
61 : "=r" (pc),\
62 "=r" (sp),\
njn9450d242005-06-26 20:50:05 +000063 "=r" (fp));
64#elif defined(VGP_ppc32_linux)
sewardjde616ec2005-11-05 14:48:03 +000065# define GET_REAL_PC_SP_AND_FP(pc, sp, fp) \
66 asm("mflr 0;" /* r0 = lr */ \
67 "bl m_libcassert_get_ip;" /* lr = pc */ \
68 "m_libcassert_get_ip:\n" \
69 "mflr %0;" \
70 "mtlr 0;" /* restore lr */ \
71 "mr %1,1;" \
72 "mr %2,1;" \
73 : "=r" (pc), \
74 "=r" (sp), \
75 "=r" (fp) \
76 : /* reads none */ \
77 : "r0" /* trashed */ );
sewardj2c48c7b2005-11-29 13:05:56 +000078#elif defined(VGP_ppc64_linux)
79# define GET_REAL_PC_SP_AND_FP(pc, sp, fp) \
80 asm("mflr 0;" /* r0 = lr */ \
cerion297c88f2005-12-22 15:53:12 +000081 "bl .m_libcassert_get_ip;" /* lr = pc */ \
82 ".m_libcassert_get_ip:\n" \
sewardj2c48c7b2005-11-29 13:05:56 +000083 "mflr %0;" \
84 "mtlr 0;" /* restore lr */ \
85 "mr %1,1;" \
86 "mr %2,1;" \
87 : "=r" (pc), \
88 "=r" (sp), \
89 "=r" (fp) \
90 : /* reads none */ \
91 : "r0" /* trashed */ );
njn9450d242005-06-26 20:50:05 +000092#else
93# error Unknown platform
94#endif
95
96#define BACKTRACE_DEPTH 100 // nice and deep!
njn3161e802005-06-20 03:38:27 +000097
njnf39e9a32005-06-12 02:43:17 +000098/* Pull down the entire world */
99void VG_(exit)( Int status )
100{
101 (void)VG_(do_syscall1)(__NR_exit_group, status );
102 (void)VG_(do_syscall1)(__NR_exit, status );
103 /* Why are we still alive here? */
104 /*NOTREACHED*/
105 *(volatile Int *)0 = 'x';
106 vg_assert(2+2 == 5);
107}
108
njnc7561b92005-06-19 01:24:32 +0000109// Print the scheduler status.
njn49b45ba2005-07-20 02:41:31 +0000110static void pp_sched_status ( void )
111{
112 Int i;
113 VG_(printf)("\nsched status:\n");
114 VG_(printf)(" running_tid=%d\n", VG_(get_running_tid)());
115 for (i = 1; i < VG_N_THREADS; i++) {
116 if (VG_(threads)[i].status == VgTs_Empty) continue;
117 VG_(printf)( "\nThread %d: status = %s\n", i,
118 VG_(name_of_ThreadStatus)(VG_(threads)[i].status) );
119 VG_(get_and_pp_StackTrace)( i, BACKTRACE_DEPTH );
120 }
121 VG_(printf)("\n");
122}
njnc7561b92005-06-19 01:24:32 +0000123
njn132bfcc2005-06-04 19:16:06 +0000124__attribute__ ((noreturn))
sewardjacaec5f2005-08-19 16:02:59 +0000125static void report_and_quit ( const Char* report,
126 Addr ip, Addr sp, Addr fp, Addr lr )
njn132bfcc2005-06-04 19:16:06 +0000127{
njn49b45ba2005-07-20 02:41:31 +0000128 Addr stacktop;
129 Addr ips[BACKTRACE_DEPTH];
130 ThreadState *tst = VG_(get_ThreadState)( VG_(get_lwp_tid)(VG_(gettid)()) );
131
132 // If necessary, fake up an ExeContext which is of our actual real CPU
133 // state. Could cause problems if we got the panic/exception within the
134 // execontext/stack dump/symtab code. But it's better than nothing.
135 if (0 == ip && 0 == sp && 0 == fp) {
sewardj9a699e82005-11-05 14:22:03 +0000136 GET_REAL_PC_SP_AND_FP(ip, sp, fp);
njn49b45ba2005-07-20 02:41:31 +0000137 }
138
sewardj45f4e7c2005-09-27 19:20:21 +0000139 stacktop = tst->os_state.valgrind_stack_init_SP;
njn49b45ba2005-07-20 02:41:31 +0000140
sewardjdfbaa222006-01-18 04:25:20 +0000141 VG_(get_StackTrace2)(0/*tid is unknown*/,
142 ips, BACKTRACE_DEPTH, ip, sp, fp, lr, sp, stacktop);
njn49b45ba2005-07-20 02:41:31 +0000143 VG_(pp_StackTrace) (ips, BACKTRACE_DEPTH);
144
145 // Don't print this, as it's not terribly interesting and avoids a
146 // dependence on m_scheduler/, which would be crazy.
147 //VG_(printf)("\nBasic block ctr is approximately %llu\n", VG_(bbs_done) );
148
149 pp_sched_status();
njn132bfcc2005-06-04 19:16:06 +0000150 VG_(printf)("\n");
151 VG_(printf)("Note: see also the FAQ.txt in the source distribution.\n");
152 VG_(printf)("It contains workarounds to several common problems.\n");
153 VG_(printf)("\n");
154 VG_(printf)("If that doesn't help, please report this bug to: %s\n\n",
155 report);
156 VG_(printf)("In the bug report, send all the above text, the valgrind\n");
157 VG_(printf)("version, and what Linux distro you are using. Thanks.\n\n");
158 VG_(exit)(1);
njn132bfcc2005-06-04 19:16:06 +0000159}
160
161void VG_(assert_fail) ( Bool isCore, const Char* expr, const Char* file,
162 Int line, const Char* fn, const HChar* format, ... )
163{
164 va_list vargs;
165 Char buf[256];
njn132bfcc2005-06-04 19:16:06 +0000166 Char* component;
167 Char* bugs_to;
168
169 static Bool entered = False;
170 if (entered)
171 VG_(exit)(2);
172 entered = True;
173
174 va_start(vargs, format);
njnaba25b42005-06-21 04:26:24 +0000175 VG_(vsprintf) ( buf, format, vargs );
njn132bfcc2005-06-04 19:16:06 +0000176 va_end(vargs);
177
178 if (isCore) {
179 component = "valgrind";
180 bugs_to = VG_BUGS_TO;
181 } else {
182 component = VG_(details).name;
183 bugs_to = VG_(details).bug_reports_to;
184 }
185
sewardjf349d552005-11-14 17:01:01 +0000186 if (VG_(clo_xml))
187 VG_(message)(Vg_UserMsg, "</valgrindoutput>\n");
188
njn132bfcc2005-06-04 19:16:06 +0000189 // Treat vg_assert2(0, "foo") specially, as a panicky abort
190 if (VG_STREQ(expr, "0")) {
191 VG_(printf)("\n%s: %s:%d (%s): the 'impossible' happened.\n",
192 component, file, line, fn, expr );
193 } else {
194 VG_(printf)("\n%s: %s:%d (%s): Assertion '%s' failed.\n",
195 component, file, line, fn, expr );
196 }
197 if (!VG_STREQ(buf, ""))
198 VG_(printf)("%s: %s\n", component, buf );
199
sewardjacaec5f2005-08-19 16:02:59 +0000200 report_and_quit(bugs_to, 0,0,0,0);
njn132bfcc2005-06-04 19:16:06 +0000201}
202
203__attribute__ ((noreturn))
204static void panic ( Char* name, Char* report, Char* str,
sewardjacaec5f2005-08-19 16:02:59 +0000205 Addr ip, Addr sp, Addr fp, Addr lr )
njn132bfcc2005-06-04 19:16:06 +0000206{
sewardjf349d552005-11-14 17:01:01 +0000207 if (VG_(clo_xml))
208 VG_(message)(Vg_UserMsg, "</valgrindoutput>\n");
njn132bfcc2005-06-04 19:16:06 +0000209 VG_(printf)("\n%s: the 'impossible' happened:\n %s\n", name, str);
sewardjacaec5f2005-08-19 16:02:59 +0000210 report_and_quit(report, ip, sp, fp, lr);
njn132bfcc2005-06-04 19:16:06 +0000211}
212
sewardjacaec5f2005-08-19 16:02:59 +0000213void VG_(core_panic_at) ( Char* str, Addr ip, Addr sp, Addr fp, Addr lr )
njn132bfcc2005-06-04 19:16:06 +0000214{
sewardjacaec5f2005-08-19 16:02:59 +0000215 panic("valgrind", VG_BUGS_TO, str, ip, sp, fp, lr);
njn132bfcc2005-06-04 19:16:06 +0000216}
217
218void VG_(core_panic) ( Char* str )
219{
sewardjacaec5f2005-08-19 16:02:59 +0000220 VG_(core_panic_at)(str, 0,0,0,0);
njn132bfcc2005-06-04 19:16:06 +0000221}
222
223void VG_(tool_panic) ( Char* str )
224{
sewardjacaec5f2005-08-19 16:02:59 +0000225 panic(VG_(details).name, VG_(details).bug_reports_to, str, 0,0,0,0);
njn132bfcc2005-06-04 19:16:06 +0000226}
227
njn49b45ba2005-07-20 02:41:31 +0000228/* Print some helpful-ish text about unimplemented things, and give up. */
njn132bfcc2005-06-04 19:16:06 +0000229void VG_(unimplemented) ( Char* msg )
230{
sewardjf349d552005-11-14 17:01:01 +0000231 if (VG_(clo_xml))
232 VG_(message)(Vg_UserMsg, "</valgrindoutput>\n");
njn132bfcc2005-06-04 19:16:06 +0000233 VG_(message)(Vg_UserMsg, "");
234 VG_(message)(Vg_UserMsg,
235 "Valgrind detected that your program requires");
236 VG_(message)(Vg_UserMsg,
237 "the following unimplemented functionality:");
238 VG_(message)(Vg_UserMsg, " %s", msg);
239 VG_(message)(Vg_UserMsg,
240 "This may be because the functionality is hard to implement,");
241 VG_(message)(Vg_UserMsg,
242 "or because no reasonable program would behave this way,");
243 VG_(message)(Vg_UserMsg,
244 "or because nobody has yet needed it. In any case, let us know at");
245 VG_(message)(Vg_UserMsg,
246 "%s and/or try to work around the problem, if you can.", VG_BUGS_TO);
247 VG_(message)(Vg_UserMsg,
248 "");
249 VG_(message)(Vg_UserMsg,
250 "Valgrind has to exit now. Sorry. Bye!");
251 VG_(message)(Vg_UserMsg,
252 "");
njn49b45ba2005-07-20 02:41:31 +0000253 pp_sched_status();
njn132bfcc2005-06-04 19:16:06 +0000254 VG_(exit)(1);
255}
256
njn132bfcc2005-06-04 19:16:06 +0000257/*--------------------------------------------------------------------*/
258/*--- end ---*/
259/*--------------------------------------------------------------------*/
260