blob: 0ab5cc74faabb3a9737ed428b756a27c9de2172b [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"
njn9450d242005-06-26 20:50:05 +000032//zz hash 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"
sewardj01515882005-06-25 15:22:10 +000036//zz hash inklood "pub_cor_libcproc.h" // For VG_(gettid)()
37//zz hash inklood "pub_cor_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 0 //zz
47#if defined(VGP_x86_linux)
48# define GET_REAL_SP_AND_FP(sp, fp) \
49 asm("movl %%esp, %0;" \
50 "movl %%ebp, %1;" \
51 : "=r" (sp),\
52 "=r" (fp));
53#elif defined(VGP_amd64_linux)
54# define GET_REAL_SP_AND_FP(sp, fp) \
55 asm("movq %%rsp, %0;" \
56 "movq %%rbp, %1;" \
57 : "=r" (sp),\
58 "=r" (fp));
59#elif defined(VGP_ppc32_linux)
60# define GET_REAL_SP_AND_FP(sp, fp) \
61 asm("mr %0,1;" \
62 "mr %1,1;" \
63 : "=r" (sp),\
64 "=r" (fp));
65#else
66# error Unknown platform
67#endif
68
69#define BACKTRACE_DEPTH 100 // nice and deep!
70#endif //zz
njn3161e802005-06-20 03:38:27 +000071
njnf39e9a32005-06-12 02:43:17 +000072/* Pull down the entire world */
73void VG_(exit)( Int status )
74{
75 (void)VG_(do_syscall1)(__NR_exit_group, status );
76 (void)VG_(do_syscall1)(__NR_exit, status );
77 /* Why are we still alive here? */
78 /*NOTREACHED*/
79 *(volatile Int *)0 = 'x';
80 vg_assert(2+2 == 5);
81}
82
njnc7561b92005-06-19 01:24:32 +000083// Print the scheduler status.
njn9450d242005-06-26 20:50:05 +000084//zz static void pp_sched_status ( void )
85//zz {
86//zz Int i;
87//zz VG_(printf)("\nsched status:\n");
88//zz VG_(printf)(" running_tid=%d\n", VG_(get_running_tid)());
89//zz for (i = 1; i < VG_N_THREADS; i++) {
90//zz if (VG_(threads)[i].status == VgTs_Empty) continue;
91//zz VG_(printf)( "\nThread %d: status = %s\n", i,
92//zz VG_(name_of_ThreadStatus)(VG_(threads)[i].status) );
sewardj01515882005-06-25 15:22:10 +000093//zz VG_(get_and_pp_StackTrace)( i, BACKTRACE_DEPTH );
njn9450d242005-06-26 20:50:05 +000094//zz }
95//zz VG_(printf)("\n");
96//zz }
njnc7561b92005-06-19 01:24:32 +000097
njn132bfcc2005-06-04 19:16:06 +000098__attribute__ ((noreturn))
99static void report_and_quit ( const Char* report, Addr ip, Addr sp, Addr fp )
100{
sewardj01515882005-06-25 15:22:10 +0000101//zz Addr stacktop;
102//zz Addr ips[BACKTRACE_DEPTH];
103//zz ThreadState *tst;
104//zz
105//zz tst = VG_(get_ThreadState)( VG_(get_lwp_tid)(VG_(gettid)()) );
106//zz
107//zz // If necessary, fake up an ExeContext which is of our actual real CPU
108//zz // state. Could cause problems if we got the panic/exception within the
109//zz // execontext/stack dump/symtab code. But it's better than nothing.
110//zz if (0 == ip && 0 == sp && 0 == fp) {
111//zz ip = (Addr)__builtin_return_address(0);
112//zz GET_REAL_SP_AND_FP(sp, fp);
113//zz }
114//zz
115//zz stacktop = tst->os_state.valgrind_stack_base +
116//zz tst->os_state.valgrind_stack_szB;
117//zz
118//zz VG_(get_StackTrace2)(ips, BACKTRACE_DEPTH, ip, sp, fp, sp, stacktop);
119//zz VG_(pp_StackTrace) (ips, BACKTRACE_DEPTH);
njn9450d242005-06-26 20:50:05 +0000120//zz
121//zz // Don't print this, as it's not terribly interesting and avoids a
122//zz // dependence on m_scheduler/, which would be crazy.
123//zz //VG_(printf)("\nBasic block ctr is approximately %llu\n", VG_(bbs_done) );
124//zz
125//zz pp_sched_status();
njn132bfcc2005-06-04 19:16:06 +0000126 VG_(printf)("\n");
127 VG_(printf)("Note: see also the FAQ.txt in the source distribution.\n");
128 VG_(printf)("It contains workarounds to several common problems.\n");
129 VG_(printf)("\n");
130 VG_(printf)("If that doesn't help, please report this bug to: %s\n\n",
131 report);
132 VG_(printf)("In the bug report, send all the above text, the valgrind\n");
133 VG_(printf)("version, and what Linux distro you are using. Thanks.\n\n");
134 VG_(exit)(1);
njn132bfcc2005-06-04 19:16:06 +0000135}
136
137void VG_(assert_fail) ( Bool isCore, const Char* expr, const Char* file,
138 Int line, const Char* fn, const HChar* format, ... )
139{
140 va_list vargs;
141 Char buf[256];
njn132bfcc2005-06-04 19:16:06 +0000142 Char* component;
143 Char* bugs_to;
144
145 static Bool entered = False;
146 if (entered)
147 VG_(exit)(2);
148 entered = True;
149
150 va_start(vargs, format);
njnaba25b42005-06-21 04:26:24 +0000151 VG_(vsprintf) ( buf, format, vargs );
njn132bfcc2005-06-04 19:16:06 +0000152 va_end(vargs);
153
154 if (isCore) {
155 component = "valgrind";
156 bugs_to = VG_BUGS_TO;
157 } else {
158 component = VG_(details).name;
159 bugs_to = VG_(details).bug_reports_to;
160 }
161
162 // Treat vg_assert2(0, "foo") specially, as a panicky abort
163 if (VG_STREQ(expr, "0")) {
164 VG_(printf)("\n%s: %s:%d (%s): the 'impossible' happened.\n",
165 component, file, line, fn, expr );
166 } else {
167 VG_(printf)("\n%s: %s:%d (%s): Assertion '%s' failed.\n",
168 component, file, line, fn, expr );
169 }
170 if (!VG_STREQ(buf, ""))
171 VG_(printf)("%s: %s\n", component, buf );
172
173 report_and_quit(bugs_to, 0,0,0);
174}
175
176__attribute__ ((noreturn))
177static void panic ( Char* name, Char* report, Char* str,
178 Addr ip, Addr sp, Addr fp )
179{
180 VG_(printf)("\n%s: the 'impossible' happened:\n %s\n", name, str);
181 report_and_quit(report, ip, sp, fp);
182}
183
184void VG_(core_panic_at) ( Char* str, Addr ip, Addr sp, Addr fp )
185{
186 panic("valgrind", VG_BUGS_TO, str, ip, sp, fp);
187}
188
189void VG_(core_panic) ( Char* str )
190{
191 VG_(core_panic_at)(str, 0,0,0);
192}
193
194void VG_(tool_panic) ( Char* str )
195{
196 panic(VG_(details).name, VG_(details).bug_reports_to, str, 0,0,0);
197}
198
199/* Print some helpful-ish text about unimplemented things, and give
200 up. */
201void VG_(unimplemented) ( Char* msg )
202{
203 VG_(message)(Vg_UserMsg, "");
204 VG_(message)(Vg_UserMsg,
205 "Valgrind detected that your program requires");
206 VG_(message)(Vg_UserMsg,
207 "the following unimplemented functionality:");
208 VG_(message)(Vg_UserMsg, " %s", msg);
209 VG_(message)(Vg_UserMsg,
210 "This may be because the functionality is hard to implement,");
211 VG_(message)(Vg_UserMsg,
212 "or because no reasonable program would behave this way,");
213 VG_(message)(Vg_UserMsg,
214 "or because nobody has yet needed it. In any case, let us know at");
215 VG_(message)(Vg_UserMsg,
216 "%s and/or try to work around the problem, if you can.", VG_BUGS_TO);
217 VG_(message)(Vg_UserMsg,
218 "");
219 VG_(message)(Vg_UserMsg,
220 "Valgrind has to exit now. Sorry. Bye!");
221 VG_(message)(Vg_UserMsg,
222 "");
njn9450d242005-06-26 20:50:05 +0000223//zz pp_sched_status();
njn132bfcc2005-06-04 19:16:06 +0000224 VG_(exit)(1);
225}
226
227
228
229/*--------------------------------------------------------------------*/
230/*--- end ---*/
231/*--------------------------------------------------------------------*/
232