njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame^] | 1 | |
| 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 | |
| 31 | #include "core.h" |
| 32 | #include "pub_core_libcbase.h" |
| 33 | #include "pub_core_libcassert.h" |
| 34 | #include "pub_core_libcprint.h" |
| 35 | #include "pub_core_main.h" |
| 36 | #include "pub_core_stacktrace.h" |
| 37 | #include "pub_core_tooliface.h" |
| 38 | |
| 39 | /* --------------------------------------------------------------------- |
| 40 | Assertery. |
| 41 | ------------------------------------------------------------------ */ |
| 42 | |
| 43 | #if defined(VGP_x86_linux) |
| 44 | # define GET_REAL_SP_AND_FP(sp, fp) \ |
| 45 | asm("movl %%esp, %0;" \ |
| 46 | "movl %%ebp, %1;" \ |
| 47 | : "=r" (sp),\ |
| 48 | "=r" (fp)); |
| 49 | #elif defined(VGP_amd64_linux) |
| 50 | # define GET_REAL_SP_AND_FP(sp, fp) \ |
| 51 | asm("movq %%rsp, %0;" \ |
| 52 | "movq %%rbp, %1;" \ |
| 53 | : "=r" (sp),\ |
| 54 | "=r" (fp)); |
| 55 | #else |
| 56 | # error Unknown platform |
| 57 | #endif |
| 58 | |
| 59 | __attribute__ ((noreturn)) |
| 60 | static void report_and_quit ( const Char* report, Addr ip, Addr sp, Addr fp ) |
| 61 | { |
| 62 | #define BACKTRACE_DEPTH 100 // nice and deep! |
| 63 | Addr stacktop, ips[BACKTRACE_DEPTH]; |
| 64 | ThreadState *tst; |
| 65 | |
| 66 | tst = VG_(get_ThreadState)( VG_(get_lwp_tid)(VG_(gettid)()) ); |
| 67 | |
| 68 | // If necessary, fake up an ExeContext which is of our actual real CPU |
| 69 | // state. Could cause problems if we got the panic/exception within the |
| 70 | // execontext/stack dump/symtab code. But it's better than nothing. |
| 71 | if (0 == ip && 0 == sp && 0 == fp) { |
| 72 | ip = (Addr)__builtin_return_address(0); |
| 73 | GET_REAL_SP_AND_FP(sp, fp); |
| 74 | } |
| 75 | |
| 76 | stacktop = tst->os_state.valgrind_stack_base + |
| 77 | tst->os_state.valgrind_stack_szB; |
| 78 | |
| 79 | VG_(get_StackTrace2)(ips, BACKTRACE_DEPTH, ip, sp, fp, sp, stacktop); |
| 80 | VG_(pp_StackTrace) (ips, BACKTRACE_DEPTH); |
| 81 | |
| 82 | VG_(printf)("\nBasic block ctr is approximately %llu\n", VG_(bbs_done) ); |
| 83 | |
| 84 | VG_(pp_sched_status)(); |
| 85 | VG_(printf)("\n"); |
| 86 | VG_(printf)("Note: see also the FAQ.txt in the source distribution.\n"); |
| 87 | VG_(printf)("It contains workarounds to several common problems.\n"); |
| 88 | VG_(printf)("\n"); |
| 89 | VG_(printf)("If that doesn't help, please report this bug to: %s\n\n", |
| 90 | report); |
| 91 | VG_(printf)("In the bug report, send all the above text, the valgrind\n"); |
| 92 | VG_(printf)("version, and what Linux distro you are using. Thanks.\n\n"); |
| 93 | VG_(exit)(1); |
| 94 | |
| 95 | #undef BACKTRACE_DEPTH |
| 96 | } |
| 97 | |
| 98 | void VG_(assert_fail) ( Bool isCore, const Char* expr, const Char* file, |
| 99 | Int line, const Char* fn, const HChar* format, ... ) |
| 100 | { |
| 101 | va_list vargs; |
| 102 | Char buf[256]; |
| 103 | Char* bufptr = buf; |
| 104 | Char* component; |
| 105 | Char* bugs_to; |
| 106 | |
| 107 | static Bool entered = False; |
| 108 | if (entered) |
| 109 | VG_(exit)(2); |
| 110 | entered = True; |
| 111 | |
| 112 | va_start(vargs, format); |
| 113 | VG_(vsprintf) ( bufptr, format, vargs ); |
| 114 | va_end(vargs); |
| 115 | |
| 116 | if (isCore) { |
| 117 | component = "valgrind"; |
| 118 | bugs_to = VG_BUGS_TO; |
| 119 | } else { |
| 120 | component = VG_(details).name; |
| 121 | bugs_to = VG_(details).bug_reports_to; |
| 122 | } |
| 123 | |
| 124 | // Treat vg_assert2(0, "foo") specially, as a panicky abort |
| 125 | if (VG_STREQ(expr, "0")) { |
| 126 | VG_(printf)("\n%s: %s:%d (%s): the 'impossible' happened.\n", |
| 127 | component, file, line, fn, expr ); |
| 128 | } else { |
| 129 | VG_(printf)("\n%s: %s:%d (%s): Assertion '%s' failed.\n", |
| 130 | component, file, line, fn, expr ); |
| 131 | } |
| 132 | if (!VG_STREQ(buf, "")) |
| 133 | VG_(printf)("%s: %s\n", component, buf ); |
| 134 | |
| 135 | report_and_quit(bugs_to, 0,0,0); |
| 136 | } |
| 137 | |
| 138 | __attribute__ ((noreturn)) |
| 139 | static void panic ( Char* name, Char* report, Char* str, |
| 140 | Addr ip, Addr sp, Addr fp ) |
| 141 | { |
| 142 | VG_(printf)("\n%s: the 'impossible' happened:\n %s\n", name, str); |
| 143 | report_and_quit(report, ip, sp, fp); |
| 144 | } |
| 145 | |
| 146 | void VG_(core_panic_at) ( Char* str, Addr ip, Addr sp, Addr fp ) |
| 147 | { |
| 148 | panic("valgrind", VG_BUGS_TO, str, ip, sp, fp); |
| 149 | } |
| 150 | |
| 151 | void VG_(core_panic) ( Char* str ) |
| 152 | { |
| 153 | VG_(core_panic_at)(str, 0,0,0); |
| 154 | } |
| 155 | |
| 156 | void VG_(tool_panic) ( Char* str ) |
| 157 | { |
| 158 | panic(VG_(details).name, VG_(details).bug_reports_to, str, 0,0,0); |
| 159 | } |
| 160 | |
| 161 | /* Print some helpful-ish text about unimplemented things, and give |
| 162 | up. */ |
| 163 | void VG_(unimplemented) ( Char* msg ) |
| 164 | { |
| 165 | VG_(message)(Vg_UserMsg, ""); |
| 166 | VG_(message)(Vg_UserMsg, |
| 167 | "Valgrind detected that your program requires"); |
| 168 | VG_(message)(Vg_UserMsg, |
| 169 | "the following unimplemented functionality:"); |
| 170 | VG_(message)(Vg_UserMsg, " %s", msg); |
| 171 | VG_(message)(Vg_UserMsg, |
| 172 | "This may be because the functionality is hard to implement,"); |
| 173 | VG_(message)(Vg_UserMsg, |
| 174 | "or because no reasonable program would behave this way,"); |
| 175 | VG_(message)(Vg_UserMsg, |
| 176 | "or because nobody has yet needed it. In any case, let us know at"); |
| 177 | VG_(message)(Vg_UserMsg, |
| 178 | "%s and/or try to work around the problem, if you can.", VG_BUGS_TO); |
| 179 | VG_(message)(Vg_UserMsg, |
| 180 | ""); |
| 181 | VG_(message)(Vg_UserMsg, |
| 182 | "Valgrind has to exit now. Sorry. Bye!"); |
| 183 | VG_(message)(Vg_UserMsg, |
| 184 | ""); |
| 185 | VG_(pp_sched_status)(); |
| 186 | VG_(exit)(1); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | /*--------------------------------------------------------------------*/ |
| 192 | /*--- end ---*/ |
| 193 | /*--------------------------------------------------------------------*/ |
| 194 | |