1132bfccd21960e462352175f8553a5bdce8a210cnjn
2132bfccd21960e462352175f8553a5bdce8a210cnjn/*--------------------------------------------------------------------*/
3132bfccd21960e462352175f8553a5bdce8a210cnjn/*--- Assertions and panics.                        m_libcassert.c ---*/
4132bfccd21960e462352175f8553a5bdce8a210cnjn/*--------------------------------------------------------------------*/
5132bfccd21960e462352175f8553a5bdce8a210cnjn
6132bfccd21960e462352175f8553a5bdce8a210cnjn/*
7132bfccd21960e462352175f8553a5bdce8a210cnjn   This file is part of Valgrind, a dynamic binary instrumentation
8132bfccd21960e462352175f8553a5bdce8a210cnjn   framework.
9132bfccd21960e462352175f8553a5bdce8a210cnjn
10ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes   Copyright (C) 2000-2017 Julian Seward
11132bfccd21960e462352175f8553a5bdce8a210cnjn      jseward@acm.org
12132bfccd21960e462352175f8553a5bdce8a210cnjn
13132bfccd21960e462352175f8553a5bdce8a210cnjn   This program is free software; you can redistribute it and/or
14132bfccd21960e462352175f8553a5bdce8a210cnjn   modify it under the terms of the GNU General Public License as
15132bfccd21960e462352175f8553a5bdce8a210cnjn   published by the Free Software Foundation; either version 2 of the
16132bfccd21960e462352175f8553a5bdce8a210cnjn   License, or (at your option) any later version.
17132bfccd21960e462352175f8553a5bdce8a210cnjn
18132bfccd21960e462352175f8553a5bdce8a210cnjn   This program is distributed in the hope that it will be useful, but
19132bfccd21960e462352175f8553a5bdce8a210cnjn   WITHOUT ANY WARRANTY; without even the implied warranty of
20132bfccd21960e462352175f8553a5bdce8a210cnjn   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21132bfccd21960e462352175f8553a5bdce8a210cnjn   General Public License for more details.
22132bfccd21960e462352175f8553a5bdce8a210cnjn
23132bfccd21960e462352175f8553a5bdce8a210cnjn   You should have received a copy of the GNU General Public License
24132bfccd21960e462352175f8553a5bdce8a210cnjn   along with this program; if not, write to the Free Software
25132bfccd21960e462352175f8553a5bdce8a210cnjn   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26132bfccd21960e462352175f8553a5bdce8a210cnjn   02111-1307, USA.
27132bfccd21960e462352175f8553a5bdce8a210cnjn
28132bfccd21960e462352175f8553a5bdce8a210cnjn   The GNU General Public License is contained in the file COPYING.
29132bfccd21960e462352175f8553a5bdce8a210cnjn*/
30132bfccd21960e462352175f8553a5bdce8a210cnjn
31c7561b931e249acf3768ead77638545b0ccaa8f1njn#include "pub_core_basics.h"
324cfea4f9480393ed6799db463b2e0fb8865a1a2fsewardj#include "pub_core_vki.h"
33745fc459ce4ce9264d8844bf1ab695d235a4ce01sewardj#include "pub_core_vkiscnums.h"
3449b45ba90382fd313e40555559f5f5aaa23033b4njn#include "pub_core_threadstate.h"
35180a7500bf2464d5b16cddb5618b91fb3f095998philippe#include "pub_core_gdbserver.h"
364f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe#include "pub_core_aspacemgr.h"
37132bfccd21960e462352175f8553a5bdce8a210cnjn#include "pub_core_libcbase.h"
38132bfccd21960e462352175f8553a5bdce8a210cnjn#include "pub_core_libcassert.h"
39132bfccd21960e462352175f8553a5bdce8a210cnjn#include "pub_core_libcprint.h"
4049b45ba90382fd313e40555559f5f5aaa23033b4njn#include "pub_core_libcproc.h"      // For VG_(gettid)()
4138a74d2cc4670e3eb559adff51a376cd6ec98005philippe#include "pub_core_machine.h"
4238a74d2cc4670e3eb559adff51a376cd6ec98005philippe#include "pub_core_stacks.h"
4349b45ba90382fd313e40555559f5f5aaa23033b4njn#include "pub_core_stacktrace.h"
449abd608244d8123868d027f616fa928156615d5anjn#include "pub_core_syscall.h"
45e070c20d9f3e651f478bba6bdb6fc34aa1f9514dnjn#include "pub_core_tooliface.h"     // For VG_(details).{name,bug_reports_to}
46f349d55d40891b890d0da268cfb9f092af0fc8f4sewardj#include "pub_core_options.h"       // For VG_(clo_xml)
47132bfccd21960e462352175f8553a5bdce8a210cnjn
48132bfccd21960e462352175f8553a5bdce8a210cnjn/* ---------------------------------------------------------------------
49132bfccd21960e462352175f8553a5bdce8a210cnjn   Assertery.
50132bfccd21960e462352175f8553a5bdce8a210cnjn   ------------------------------------------------------------------ */
51132bfccd21960e462352175f8553a5bdce8a210cnjn
528eb8bab992e3998c33770b0cdb16059a8b918a06sewardj#if defined(VGP_x86_linux) || defined(VGP_x86_darwin) \
538eb8bab992e3998c33770b0cdb16059a8b918a06sewardj    || defined(VGP_x86_solaris)
5459570ffbe31930ab4d678754daaeec0715117a3dsewardj#  define GET_STARTREGS(srP)                              \
5559570ffbe31930ab4d678754daaeec0715117a3dsewardj      { UInt eip, esp, ebp;                               \
5659570ffbe31930ab4d678754daaeec0715117a3dsewardj        __asm__ __volatile__(                             \
5759570ffbe31930ab4d678754daaeec0715117a3dsewardj           "call 0f;"                                     \
5859570ffbe31930ab4d678754daaeec0715117a3dsewardj           "0: popl %0;"                                  \
5959570ffbe31930ab4d678754daaeec0715117a3dsewardj           "movl %%esp, %1;"                              \
6059570ffbe31930ab4d678754daaeec0715117a3dsewardj           "movl %%ebp, %2;"                              \
6159570ffbe31930ab4d678754daaeec0715117a3dsewardj           : "=r" (eip), "=r" (esp), "=r" (ebp)           \
6259570ffbe31930ab4d678754daaeec0715117a3dsewardj           : /* reads none */                             \
6359570ffbe31930ab4d678754daaeec0715117a3dsewardj           : "memory"                                     \
6459570ffbe31930ab4d678754daaeec0715117a3dsewardj        );                                                \
6559570ffbe31930ab4d678754daaeec0715117a3dsewardj        (srP)->r_pc = (ULong)eip;                         \
6659570ffbe31930ab4d678754daaeec0715117a3dsewardj        (srP)->r_sp = (ULong)esp;                         \
6759570ffbe31930ab4d678754daaeec0715117a3dsewardj        (srP)->misc.X86.r_ebp = ebp;                      \
6859570ffbe31930ab4d678754daaeec0715117a3dsewardj      }
698eb8bab992e3998c33770b0cdb16059a8b918a06sewardj#elif defined(VGP_amd64_linux) || defined(VGP_amd64_darwin) \
708eb8bab992e3998c33770b0cdb16059a8b918a06sewardj      || defined(VGP_amd64_solaris)
7159570ffbe31930ab4d678754daaeec0715117a3dsewardj#  define GET_STARTREGS(srP)                              \
7259570ffbe31930ab4d678754daaeec0715117a3dsewardj      { ULong rip, rsp, rbp;                              \
7359570ffbe31930ab4d678754daaeec0715117a3dsewardj        __asm__ __volatile__(                             \
7459570ffbe31930ab4d678754daaeec0715117a3dsewardj           "leaq 0(%%rip), %0;"                           \
7559570ffbe31930ab4d678754daaeec0715117a3dsewardj           "movq %%rsp, %1;"                              \
7659570ffbe31930ab4d678754daaeec0715117a3dsewardj           "movq %%rbp, %2;"                              \
7759570ffbe31930ab4d678754daaeec0715117a3dsewardj           : "=r" (rip), "=r" (rsp), "=r" (rbp)           \
7859570ffbe31930ab4d678754daaeec0715117a3dsewardj           : /* reads none */                             \
7959570ffbe31930ab4d678754daaeec0715117a3dsewardj           : "memory"                                     \
8059570ffbe31930ab4d678754daaeec0715117a3dsewardj        );                                                \
8159570ffbe31930ab4d678754daaeec0715117a3dsewardj        (srP)->r_pc = rip;                                \
8259570ffbe31930ab4d678754daaeec0715117a3dsewardj        (srP)->r_sp = rsp;                                \
8359570ffbe31930ab4d678754daaeec0715117a3dsewardj        (srP)->misc.AMD64.r_rbp = rbp;                    \
8459570ffbe31930ab4d678754daaeec0715117a3dsewardj      }
856e9de463ef677f093e9f24f126e1b11c28cf59fdsewardj#elif defined(VGP_ppc32_linux)
8659570ffbe31930ab4d678754daaeec0715117a3dsewardj#  define GET_STARTREGS(srP)                              \
8759570ffbe31930ab4d678754daaeec0715117a3dsewardj      { UInt cia, r1, lr;                                 \
8859570ffbe31930ab4d678754daaeec0715117a3dsewardj        __asm__ __volatile__(                             \
8959570ffbe31930ab4d678754daaeec0715117a3dsewardj           "mflr 0;"                   /* r0 = lr */      \
907f3c1f251860f760950fcbd5c8da91ed4ed35f52florian           "bl 0f;"                    /* lr = pc */      \
917f3c1f251860f760950fcbd5c8da91ed4ed35f52florian           "0:\n"                                         \
9259570ffbe31930ab4d678754daaeec0715117a3dsewardj           "mflr %0;"                  /* %0 = pc */      \
9359570ffbe31930ab4d678754daaeec0715117a3dsewardj           "mtlr 0;"                   /* restore lr */   \
9459570ffbe31930ab4d678754daaeec0715117a3dsewardj           "mr %1,1;"                  /* %1 = r1 */      \
9559570ffbe31930ab4d678754daaeec0715117a3dsewardj           "mr %2,0;"                  /* %2 = lr */      \
9659570ffbe31930ab4d678754daaeec0715117a3dsewardj           : "=r" (cia), "=r" (r1), "=r" (lr)             \
9759570ffbe31930ab4d678754daaeec0715117a3dsewardj           : /* reads none */                             \
9859570ffbe31930ab4d678754daaeec0715117a3dsewardj           : "r0" /* trashed */                           \
9959570ffbe31930ab4d678754daaeec0715117a3dsewardj        );                                                \
100f5f1e12bd89408917c1ffeb22ec23a1fd11b7a23sewardj        (srP)->r_pc = (ULong)cia;                         \
101f5f1e12bd89408917c1ffeb22ec23a1fd11b7a23sewardj        (srP)->r_sp = (ULong)r1;                          \
102f5f1e12bd89408917c1ffeb22ec23a1fd11b7a23sewardj        (srP)->misc.PPC32.r_lr = lr;                      \
10359570ffbe31930ab4d678754daaeec0715117a3dsewardj      }
104cae0cc22b83ffb260ee8379e92099c5a701944cbcarll#elif defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux)
10559570ffbe31930ab4d678754daaeec0715117a3dsewardj#  define GET_STARTREGS(srP)                              \
10659570ffbe31930ab4d678754daaeec0715117a3dsewardj      { ULong cia, r1, lr;                                \
10759570ffbe31930ab4d678754daaeec0715117a3dsewardj        __asm__ __volatile__(                             \
10859570ffbe31930ab4d678754daaeec0715117a3dsewardj           "mflr 0;"                   /* r0 = lr */      \
1097f3c1f251860f760950fcbd5c8da91ed4ed35f52florian           "bl 0f;"                    /* lr = pc */      \
1107f3c1f251860f760950fcbd5c8da91ed4ed35f52florian           "0:\n"                                         \
11159570ffbe31930ab4d678754daaeec0715117a3dsewardj           "mflr %0;"                  /* %0 = pc */      \
11259570ffbe31930ab4d678754daaeec0715117a3dsewardj           "mtlr 0;"                   /* restore lr */   \
11359570ffbe31930ab4d678754daaeec0715117a3dsewardj           "mr %1,1;"                  /* %1 = r1 */      \
11459570ffbe31930ab4d678754daaeec0715117a3dsewardj           "mr %2,0;"                  /* %2 = lr */      \
11559570ffbe31930ab4d678754daaeec0715117a3dsewardj           : "=r" (cia), "=r" (r1), "=r" (lr)             \
11659570ffbe31930ab4d678754daaeec0715117a3dsewardj           : /* reads none */                             \
11759570ffbe31930ab4d678754daaeec0715117a3dsewardj           : "r0" /* trashed */                           \
11859570ffbe31930ab4d678754daaeec0715117a3dsewardj        );                                                \
119f5f1e12bd89408917c1ffeb22ec23a1fd11b7a23sewardj        (srP)->r_pc = cia;                                \
120f5f1e12bd89408917c1ffeb22ec23a1fd11b7a23sewardj        (srP)->r_sp = r1;                                 \
121f5f1e12bd89408917c1ffeb22ec23a1fd11b7a23sewardj        (srP)->misc.PPC64.r_lr = lr;                      \
12259570ffbe31930ab4d678754daaeec0715117a3dsewardj      }
12359570ffbe31930ab4d678754daaeec0715117a3dsewardj#elif defined(VGP_arm_linux)
12459570ffbe31930ab4d678754daaeec0715117a3dsewardj#  define GET_STARTREGS(srP)                              \
125fa5ce5616a17e79828fbc79f30b02b5085151e3csewardj      { UInt block[6];                                    \
12659570ffbe31930ab4d678754daaeec0715117a3dsewardj        __asm__ __volatile__(                             \
12759570ffbe31930ab4d678754daaeec0715117a3dsewardj           "str r15, [%0, #+0];"                          \
12859570ffbe31930ab4d678754daaeec0715117a3dsewardj           "str r14, [%0, #+4];"                          \
12959570ffbe31930ab4d678754daaeec0715117a3dsewardj           "str r13, [%0, #+8];"                          \
13059570ffbe31930ab4d678754daaeec0715117a3dsewardj           "str r12, [%0, #+12];"                         \
13159570ffbe31930ab4d678754daaeec0715117a3dsewardj           "str r11, [%0, #+16];"                         \
132fa5ce5616a17e79828fbc79f30b02b5085151e3csewardj           "str r7,  [%0, #+20];"                         \
13359570ffbe31930ab4d678754daaeec0715117a3dsewardj           : /* out */                                    \
13459570ffbe31930ab4d678754daaeec0715117a3dsewardj           : /* in */ "r"(&block[0])                      \
13559570ffbe31930ab4d678754daaeec0715117a3dsewardj           : /* trash */ "memory"                         \
13659570ffbe31930ab4d678754daaeec0715117a3dsewardj        );                                                \
13759570ffbe31930ab4d678754daaeec0715117a3dsewardj        (srP)->r_pc = block[0] - 8;                       \
138d474ab25fa26da6adf1f1fd4199379887eb377f6sewardj        (srP)->misc.ARM.r14 = block[1];                   \
139d474ab25fa26da6adf1f1fd4199379887eb377f6sewardj        (srP)->r_sp = block[2];                           \
14059570ffbe31930ab4d678754daaeec0715117a3dsewardj        (srP)->misc.ARM.r12 = block[3];                   \
14159570ffbe31930ab4d678754daaeec0715117a3dsewardj        (srP)->misc.ARM.r11 = block[4];                   \
142fa5ce5616a17e79828fbc79f30b02b5085151e3csewardj        (srP)->misc.ARM.r7  = block[5];                   \
14359570ffbe31930ab4d678754daaeec0715117a3dsewardj      }
144f0c1250e324f6684757c6a15545366447ef1d64fsewardj#elif defined(VGP_arm64_linux)
145f0c1250e324f6684757c6a15545366447ef1d64fsewardj#  define GET_STARTREGS(srP)                              \
146f0c1250e324f6684757c6a15545366447ef1d64fsewardj      { ULong block[4];                                   \
147f0c1250e324f6684757c6a15545366447ef1d64fsewardj        __asm__ __volatile__(                             \
148f0c1250e324f6684757c6a15545366447ef1d64fsewardj           "adr x19, 0;"                                  \
149f0c1250e324f6684757c6a15545366447ef1d64fsewardj           "str x19, [%0, #+0];"   /* pc */               \
150f0c1250e324f6684757c6a15545366447ef1d64fsewardj           "mov x19, sp;"                                 \
151f0c1250e324f6684757c6a15545366447ef1d64fsewardj           "str x19, [%0, #+8];"   /* sp */               \
152f0c1250e324f6684757c6a15545366447ef1d64fsewardj           "str x29, [%0, #+16];"  /* fp */               \
153f0c1250e324f6684757c6a15545366447ef1d64fsewardj           "str x30, [%0, #+24];"  /* lr */               \
154f0c1250e324f6684757c6a15545366447ef1d64fsewardj           : /* out */                                    \
155f0c1250e324f6684757c6a15545366447ef1d64fsewardj           : /* in */ "r"(&block[0])                      \
156f0c1250e324f6684757c6a15545366447ef1d64fsewardj           : /* trash */ "memory","x19"                   \
157f0c1250e324f6684757c6a15545366447ef1d64fsewardj        );                                                \
158f0c1250e324f6684757c6a15545366447ef1d64fsewardj        (srP)->r_pc = block[0];                           \
159f0c1250e324f6684757c6a15545366447ef1d64fsewardj        (srP)->r_sp = block[1];                           \
160f0c1250e324f6684757c6a15545366447ef1d64fsewardj        (srP)->misc.ARM64.x29 = block[2];                 \
161f0c1250e324f6684757c6a15545366447ef1d64fsewardj        (srP)->misc.ARM64.x30 = block[3];                 \
162f0c1250e324f6684757c6a15545366447ef1d64fsewardj      }
163b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj#elif defined(VGP_s390x_linux)
164b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj#  define GET_STARTREGS(srP)                              \
165b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj      { ULong ia, sp, fp, lr;                             \
166b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj        __asm__ __volatile__(                             \
167b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj           "bras %0,0f;"                                  \
168b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj           "0: lgr %1,15;"                                \
169b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj           "lgr %2,11;"                                   \
170b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj           "lgr %3,14;"                                   \
171b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj           : "=r" (ia), "=r" (sp),"=r" (fp),"=r" (lr)     \
172b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj           /* no read & clobber */                        \
173b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj        );                                                \
174b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj        (srP)->r_pc = ia;                                 \
175b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj        (srP)->r_sp = sp;                                 \
176b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj        (srP)->misc.S390X.r_fp = fp;                      \
177b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj        (srP)->misc.S390X.r_lr = lr;                      \
178b5b87408c0c99f9f6938d8cd921e2a5f420577c4sewardj      }
1795db15403e889d4db339b342bc2a824ef0bfaa654sewardj#elif defined(VGP_mips32_linux)
1805db15403e889d4db339b342bc2a824ef0bfaa654sewardj#  define GET_STARTREGS(srP)                              \
1815db15403e889d4db339b342bc2a824ef0bfaa654sewardj      { UInt pc, sp, fp, ra, gp;                          \
1825db15403e889d4db339b342bc2a824ef0bfaa654sewardj      asm("move $8, $31;"             /* t0 = ra */       \
1837f3c1f251860f760950fcbd5c8da91ed4ed35f52florian          "bal 0f;"                   /* ra = pc */       \
1847f3c1f251860f760950fcbd5c8da91ed4ed35f52florian          "0:\n"                                          \
1855db15403e889d4db339b342bc2a824ef0bfaa654sewardj          "move %0, $31;"                                 \
1865db15403e889d4db339b342bc2a824ef0bfaa654sewardj          "move $31, $8;"             /* restore lr */    \
1875db15403e889d4db339b342bc2a824ef0bfaa654sewardj          "move %1, $29;"                                 \
1885db15403e889d4db339b342bc2a824ef0bfaa654sewardj          "move %2, $30;"                                 \
1895db15403e889d4db339b342bc2a824ef0bfaa654sewardj          "move %3, $31;"                                 \
1905db15403e889d4db339b342bc2a824ef0bfaa654sewardj          "move %4, $28;"                                 \
1915db15403e889d4db339b342bc2a824ef0bfaa654sewardj          : "=r" (pc),                                    \
1925db15403e889d4db339b342bc2a824ef0bfaa654sewardj            "=r" (sp),                                    \
1935db15403e889d4db339b342bc2a824ef0bfaa654sewardj            "=r" (fp),                                    \
1945db15403e889d4db339b342bc2a824ef0bfaa654sewardj            "=r" (ra),                                    \
1955db15403e889d4db339b342bc2a824ef0bfaa654sewardj            "=r" (gp)                                     \
1965db15403e889d4db339b342bc2a824ef0bfaa654sewardj          : /* reads none */                              \
1975db15403e889d4db339b342bc2a824ef0bfaa654sewardj          : "$8" /* trashed */ );                         \
1985db15403e889d4db339b342bc2a824ef0bfaa654sewardj        (srP)->r_pc = (ULong)pc - 8;                      \
1995db15403e889d4db339b342bc2a824ef0bfaa654sewardj        (srP)->r_sp = (ULong)sp;                          \
2005db15403e889d4db339b342bc2a824ef0bfaa654sewardj        (srP)->misc.MIPS32.r30 = (ULong)fp;               \
2015db15403e889d4db339b342bc2a824ef0bfaa654sewardj        (srP)->misc.MIPS32.r31 = (ULong)ra;               \
2025db15403e889d4db339b342bc2a824ef0bfaa654sewardj        (srP)->misc.MIPS32.r28 = (ULong)gp;               \
2035db15403e889d4db339b342bc2a824ef0bfaa654sewardj      }
2044df0bfc0614379192c780c944415dc420d9cfe8epetarj#elif defined(VGP_mips64_linux)
2054df0bfc0614379192c780c944415dc420d9cfe8epetarj#  define GET_STARTREGS(srP)                              \
2067f3c1f251860f760950fcbd5c8da91ed4ed35f52florian      { ULong pc, sp, fp, ra, gp;                         \
2074df0bfc0614379192c780c944415dc420d9cfe8epetarj      asm("move $8, $31;"             /* t0 = ra */       \
2087f3c1f251860f760950fcbd5c8da91ed4ed35f52florian          "bal 0f;"                   /* ra = pc */       \
2097f3c1f251860f760950fcbd5c8da91ed4ed35f52florian          "0:\n"                                          \
2104df0bfc0614379192c780c944415dc420d9cfe8epetarj          "move %0, $31;"                                 \
2114df0bfc0614379192c780c944415dc420d9cfe8epetarj          "move $31, $8;"             /* restore lr */    \
2124df0bfc0614379192c780c944415dc420d9cfe8epetarj          "move %1, $29;"                                 \
2134df0bfc0614379192c780c944415dc420d9cfe8epetarj          "move %2, $30;"                                 \
2144df0bfc0614379192c780c944415dc420d9cfe8epetarj          "move %3, $31;"                                 \
2154df0bfc0614379192c780c944415dc420d9cfe8epetarj          "move %4, $28;"                                 \
2164df0bfc0614379192c780c944415dc420d9cfe8epetarj          : "=r" (pc),                                    \
2174df0bfc0614379192c780c944415dc420d9cfe8epetarj            "=r" (sp),                                    \
2184df0bfc0614379192c780c944415dc420d9cfe8epetarj            "=r" (fp),                                    \
2194df0bfc0614379192c780c944415dc420d9cfe8epetarj            "=r" (ra),                                    \
2204df0bfc0614379192c780c944415dc420d9cfe8epetarj            "=r" (gp)                                     \
2214df0bfc0614379192c780c944415dc420d9cfe8epetarj          : /* reads none */                              \
2224df0bfc0614379192c780c944415dc420d9cfe8epetarj          : "$8" /* trashed */ );                         \
2234df0bfc0614379192c780c944415dc420d9cfe8epetarj        (srP)->r_pc = (ULong)pc - 8;                      \
2244df0bfc0614379192c780c944415dc420d9cfe8epetarj        (srP)->r_sp = (ULong)sp;                          \
2254df0bfc0614379192c780c944415dc420d9cfe8epetarj        (srP)->misc.MIPS64.r30 = (ULong)fp;               \
2264df0bfc0614379192c780c944415dc420d9cfe8epetarj        (srP)->misc.MIPS64.r31 = (ULong)ra;               \
2274df0bfc0614379192c780c944415dc420d9cfe8epetarj        (srP)->misc.MIPS64.r28 = (ULong)gp;               \
2284df0bfc0614379192c780c944415dc420d9cfe8epetarj      }
2299450d245afa7cd6e49f2734c2a621047df0d01ecnjn#else
2309450d245afa7cd6e49f2734c2a621047df0d01ecnjn#  error Unknown platform
2319450d245afa7cd6e49f2734c2a621047df0d01ecnjn#endif
2329450d245afa7cd6e49f2734c2a621047df0d01ecnjn
2339450d245afa7cd6e49f2734c2a621047df0d01ecnjn#define BACKTRACE_DEPTH    100         // nice and deep!
2343161e808ce76595c60447d041715a3ece1f9665dnjn
235180a7500bf2464d5b16cddb5618b91fb3f095998philippe__attribute__ ((__noreturn__))
236180a7500bf2464d5b16cddb5618b91fb3f095998philippestatic void exit_wrk( Int status, Bool gdbserver_call_allowed)
237f39e9a36dca9642668a66c6b054f81c88650bcb9njn{
238180a7500bf2464d5b16cddb5618b91fb3f095998philippe   static Bool exit_called = False;
239180a7500bf2464d5b16cddb5618b91fb3f095998philippe   // avoid recursive exit during gdbserver call.
240180a7500bf2464d5b16cddb5618b91fb3f095998philippe
241180a7500bf2464d5b16cddb5618b91fb3f095998philippe   if (gdbserver_call_allowed && !exit_called) {
242180a7500bf2464d5b16cddb5618b91fb3f095998philippe      const ThreadId atid = 1; // Arbitrary tid used to call/terminate gdbsrv.
243180a7500bf2464d5b16cddb5618b91fb3f095998philippe      exit_called = True;
244ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes      if (status != 0
245ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes          && VgdbStopAtiS(VgdbStopAt_ValgrindAbExit, VG_(clo_vgdb_stop_at))) {
246180a7500bf2464d5b16cddb5618b91fb3f095998philippe         if (VG_(gdbserver_init_done)()) {
247180a7500bf2464d5b16cddb5618b91fb3f095998philippe            VG_(umsg)("(action at valgrind abnormal exit) vgdb me ... \n");
248180a7500bf2464d5b16cddb5618b91fb3f095998philippe            VG_(gdbserver) (atid);
249180a7500bf2464d5b16cddb5618b91fb3f095998philippe         } else {
250ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes            VG_(umsg)("(action at valgrind abnormal exit)\n"
251ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes                      "valgrind exit is too early => vgdb not yet usable\n");
252180a7500bf2464d5b16cddb5618b91fb3f095998philippe         }
253180a7500bf2464d5b16cddb5618b91fb3f095998philippe      }
254180a7500bf2464d5b16cddb5618b91fb3f095998philippe      if (VG_(gdbserver_init_done)()) {
255180a7500bf2464d5b16cddb5618b91fb3f095998philippe         // Always terminate the gdbserver when Valgrind exits, so as
256180a7500bf2464d5b16cddb5618b91fb3f095998philippe         // to e.g. cleanup the FIFOs.
257180a7500bf2464d5b16cddb5618b91fb3f095998philippe         VG_(gdbserver_exit) (atid,
258180a7500bf2464d5b16cddb5618b91fb3f095998philippe                              status == 0 ? VgSrc_ExitProcess : VgSrc_FatalSig);
259180a7500bf2464d5b16cddb5618b91fb3f095998philippe      }
260180a7500bf2464d5b16cddb5618b91fb3f095998philippe   }
261180a7500bf2464d5b16cddb5618b91fb3f095998philippe   exit_called = True;
262180a7500bf2464d5b16cddb5618b91fb3f095998philippe
263421c26e30de64ccf441ec398c1f8beda7afbb47cflorian   VG_(exit_now) (status);
264421c26e30de64ccf441ec398c1f8beda7afbb47cflorian}
265421c26e30de64ccf441ec398c1f8beda7afbb47cflorian
266421c26e30de64ccf441ec398c1f8beda7afbb47cflorian/* Call the appropriate system call and nothing else. This function should
267421c26e30de64ccf441ec398c1f8beda7afbb47cflorian   be called in places where the dependencies of VG_(exit) need to be
268421c26e30de64ccf441ec398c1f8beda7afbb47cflorian   avoided. */
269421c26e30de64ccf441ec398c1f8beda7afbb47cflorian__attribute__ ((__noreturn__))
270421c26e30de64ccf441ec398c1f8beda7afbb47cflorianvoid VG_(exit_now)( Int status )
271421c26e30de64ccf441ec398c1f8beda7afbb47cflorian{
2725b6b8faafbfa52635d444dd58b107d3a4081c86cnjn#if defined(VGO_linux)
273f39e9a36dca9642668a66c6b054f81c88650bcb9njn   (void)VG_(do_syscall1)(__NR_exit_group, status );
2748eb8bab992e3998c33770b0cdb16059a8b918a06sewardj#elif defined(VGO_darwin) || defined(VGO_solaris)
275f39e9a36dca9642668a66c6b054f81c88650bcb9njn   (void)VG_(do_syscall1)(__NR_exit, status );
2765b6b8faafbfa52635d444dd58b107d3a4081c86cnjn#else
2775b6b8faafbfa52635d444dd58b107d3a4081c86cnjn#  error Unknown OS
2785b6b8faafbfa52635d444dd58b107d3a4081c86cnjn#endif
279f39e9a36dca9642668a66c6b054f81c88650bcb9njn   /*NOTREACHED*/
2804eeb0e50fd10c08a396c6d3c53f296287a47d9c3njn   // We really shouldn't reach here.  Just in case we do, use some very crude
2814eeb0e50fd10c08a396c6d3c53f296287a47d9c3njn   // methods to force abort
2824eeb0e50fd10c08a396c6d3c53f296287a47d9c3njn   __builtin_trap();
2834eeb0e50fd10c08a396c6d3c53f296287a47d9c3njn   *(volatile Int*)0 = 'x';
284f39e9a36dca9642668a66c6b054f81c88650bcb9njn}
285f39e9a36dca9642668a66c6b054f81c88650bcb9njn
286180a7500bf2464d5b16cddb5618b91fb3f095998philippe/* Pull down the entire world */
287180a7500bf2464d5b16cddb5618b91fb3f095998philippevoid VG_(exit)( Int status )
288180a7500bf2464d5b16cddb5618b91fb3f095998philippe{
289180a7500bf2464d5b16cddb5618b91fb3f095998philippe   exit_wrk (status, True);
290180a7500bf2464d5b16cddb5618b91fb3f095998philippe}
291180a7500bf2464d5b16cddb5618b91fb3f095998philippe
292180a7500bf2464d5b16cddb5618b91fb3f095998philippe/* Pull down the entire world */
293180a7500bf2464d5b16cddb5618b91fb3f095998philippevoid VG_(client_exit)( Int status )
294180a7500bf2464d5b16cddb5618b91fb3f095998philippe{
295180a7500bf2464d5b16cddb5618b91fb3f095998philippe   exit_wrk (status, False);
296180a7500bf2464d5b16cddb5618b91fb3f095998philippe}
297180a7500bf2464d5b16cddb5618b91fb3f095998philippe
298ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughesstatic void print_thread_state (Bool stack_usage,
299ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes                                const HChar* prefix, ThreadId i)
300ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes{
301ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes   VgStack *stack
302ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes      = (VgStack*)VG_(threads)[i].os_state.valgrind_stack_base;
303ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes
304ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes   VG_(printf)("\n%sThread %d: status = %s (lwpid %d)\n", prefix, i,
305ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes               VG_(name_of_ThreadStatus)(VG_(threads)[i].status),
306ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes               VG_(threads)[i].os_state.lwpid);
307ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes   if (VG_(threads)[i].status != VgTs_Empty)
308ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes      VG_(get_and_pp_StackTrace)( i, BACKTRACE_DEPTH );
309ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes   if (stack_usage && VG_(threads)[i].client_stack_highest_byte != 0 ) {
310ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes      Addr start, end;
311ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes
312ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes      start = end = 0;
313ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes      VG_(stack_limits)(VG_(get_SP)(i), &start, &end);
314ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes      if (start != end)
315ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes         VG_(printf)("%sclient stack range: [%p %p] client SP: %p\n",
316ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes                     prefix,
317ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes                     (void*)start, (void*)end, (void*)VG_(get_SP)(i));
318ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes      else
319ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes         VG_(printf)("%sclient stack range: ??????? client SP: %p\n",
320ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes                     prefix,
321ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes                     (void*)VG_(get_SP)(i));
322ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes   }
323ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes   if (stack_usage && stack != 0)
324ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes      VG_(printf)
325ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes         ("%svalgrind stack top usage: %lu of %lu\n",
326ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes          prefix,
327ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes          VG_(clo_valgrind_stacksize)
328ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes          - VG_(am_get_VgStack_unused_szB) (stack,
329ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes                                            VG_(clo_valgrind_stacksize)),
330ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes          (SizeT) VG_(clo_valgrind_stacksize));
331ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes}
332180a7500bf2464d5b16cddb5618b91fb3f095998philippe
333c7561b931e249acf3768ead77638545b0ccaa8f1njn// Print the scheduler status.
3344f6f336badda2171d6e842cca3de63b53f4c9f0bphilippestatic void show_sched_status_wrk ( Bool host_stacktrace,
33538a74d2cc4670e3eb559adff51a376cd6ec98005philippe                                    Bool stack_usage,
3364f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe                                    Bool exited_threads,
337518850bf0da07ed3e2244e307268ae0fd80e93a8florian                                    const UnwindStartRegs* startRegsIN)
33849b45ba90382fd313e40555559f5f5aaa23033b4njn{
33949b45ba90382fd313e40555559f5f5aaa23033b4njn   Int i;
3404f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe   if (host_stacktrace) {
3414f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      const Bool save_clo_xml = VG_(clo_xml);
3424f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      Addr stacktop;
3434f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      Addr ips[BACKTRACE_DEPTH];
3444f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      Int  n_ips;
3454f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      ThreadState *tst
3464f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe         = VG_(get_ThreadState)( VG_(lwpid_to_vgtid)( VG_(gettid)() ) );
3474f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe
3484f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      // If necessary, fake up an ExeContext which is of our actual real CPU
3494f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      // state.  Could cause problems if we got the panic/exception within the
3504f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      // execontext/stack dump/symtab code.  But it's better than nothing.
3514f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      UnwindStartRegs startRegs;
3524f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      VG_(memset)(&startRegs, 0, sizeof(startRegs));
3534f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe
3544f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      if (startRegsIN == NULL) {
3554f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe         GET_STARTREGS(&startRegs);
3564f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      } else {
3574f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe         startRegs = *startRegsIN;
3584f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      }
3594f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe
3604f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      stacktop = tst->os_state.valgrind_stack_init_SP;
3614f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe
3624f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      n_ips =
3634f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe         VG_(get_StackTrace_wrk)(
3644f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe            0/*tid is unknown*/,
3654f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe            ips, BACKTRACE_DEPTH,
3664f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe            NULL/*array to dump SP values in*/,
3674f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe            NULL/*array to dump FP values in*/,
3684f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe            &startRegs, stacktop
3694f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe         );
3704f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      VG_(printf)("\nhost stacktrace:\n");
3714f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      VG_(clo_xml) = False;
3724f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      VG_(pp_StackTrace) (ips, n_ips);
3734f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe      VG_(clo_xml) = save_clo_xml;
3744f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe   }
3754f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe
37649b45ba90382fd313e40555559f5f5aaa23033b4njn   VG_(printf)("\nsched status:\n");
377a0664b9ca67b594bd6f570a61d3301167a24750cElliott Hughes   if (VG_(threads) == NULL) {
378a0664b9ca67b594bd6f570a61d3301167a24750cElliott Hughes      VG_(printf)("  scheduler not yet initialised\n");
379a0664b9ca67b594bd6f570a61d3301167a24750cElliott Hughes   } else {
380a0664b9ca67b594bd6f570a61d3301167a24750cElliott Hughes      VG_(printf)("  running_tid=%u\n", VG_(get_running_tid)());
381a0664b9ca67b594bd6f570a61d3301167a24750cElliott Hughes      for (i = 1; i < VG_N_THREADS; i++) {
382a0664b9ca67b594bd6f570a61d3301167a24750cElliott Hughes         VgStack *stack
383a0664b9ca67b594bd6f570a61d3301167a24750cElliott Hughes            = (VgStack*)VG_(threads)[i].os_state.valgrind_stack_base;
384a0664b9ca67b594bd6f570a61d3301167a24750cElliott Hughes         /* If a thread slot was never used (yet), valgrind_stack_base is 0.
385a0664b9ca67b594bd6f570a61d3301167a24750cElliott Hughes            If a thread slot is used by a thread or was used by a thread which
386a0664b9ca67b594bd6f570a61d3301167a24750cElliott Hughes            has exited, then valgrind_stack_base points to the stack base. */
387a0664b9ca67b594bd6f570a61d3301167a24750cElliott Hughes         if (VG_(threads)[i].status == VgTs_Empty
388a0664b9ca67b594bd6f570a61d3301167a24750cElliott Hughes             && (!exited_threads || stack == 0)) continue;
389ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes         print_thread_state(stack_usage, "", i);
390ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes         if (VG_(inner_threads) != NULL) {
391ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes            /* An inner V has informed us (the outer) of its thread array.
392ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes               Report the inner guest stack trace. */
393ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes            UInt inner_tid;
394ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes
395ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes            for (inner_tid = 1; inner_tid < VG_N_THREADS; inner_tid++) {
396ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes               if (VG_(threads)[i].os_state.lwpid
397ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes                   == VG_(inner_threads)[inner_tid].os_state.lwpid) {
398ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes                  ThreadState* save_outer_vg_threads = VG_(threads);
399a0664b9ca67b594bd6f570a61d3301167a24750cElliott Hughes
400ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes                  VG_(threads) = VG_(inner_threads);
401ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes                  print_thread_state(stack_usage, "INNER ", inner_tid);
402ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes                  VG_(threads) = save_outer_vg_threads;
403ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes                  break;
404ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes               }
405ed39800a83baf5bffbe391f3974eb2af0f415f80Elliott Hughes            }
406a0664b9ca67b594bd6f570a61d3301167a24750cElliott Hughes         }
40738a74d2cc4670e3eb559adff51a376cd6ec98005philippe      }
40849b45ba90382fd313e40555559f5f5aaa23033b4njn   }
40949b45ba90382fd313e40555559f5f5aaa23033b4njn   VG_(printf)("\n");
41049b45ba90382fd313e40555559f5f5aaa23033b4njn}
411c7561b931e249acf3768ead77638545b0ccaa8f1njn
4124f6f336badda2171d6e842cca3de63b53f4c9f0bphilippevoid VG_(show_sched_status) ( Bool host_stacktrace,
41338a74d2cc4670e3eb559adff51a376cd6ec98005philippe                              Bool stack_usage,
4144f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe                              Bool exited_threads)
4154f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe{
4164f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe   show_sched_status_wrk (host_stacktrace,
41738a74d2cc4670e3eb559adff51a376cd6ec98005philippe                          stack_usage,
4184f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe                          exited_threads,
4194f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe                          NULL);
4204f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe}
4214f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe
422132bfccd21960e462352175f8553a5bdce8a210cnjn__attribute__ ((noreturn))
423bbd9a21ca8518fc34cd7b6254a61d72f82495120florianstatic void report_and_quit ( const HChar* report,
424518850bf0da07ed3e2244e307268ae0fd80e93a8florian                              const UnwindStartRegs* startRegsIN )
425132bfccd21960e462352175f8553a5bdce8a210cnjn{
4264f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe   show_sched_status_wrk (True,  // host_stacktrace
42738a74d2cc4670e3eb559adff51a376cd6ec98005philippe                          False, // stack_usage
4284f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe                          False, // exited_threads
4294f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe                          startRegsIN);
430b8329f0ce649067dc5d9d74f8392c6a0ab8d2d40njn   VG_(printf)(
431b8329f0ce649067dc5d9d74f8392c6a0ab8d2d40njn      "\n"
432b8329f0ce649067dc5d9d74f8392c6a0ab8d2d40njn      "Note: see also the FAQ in the source distribution.\n"
433b8329f0ce649067dc5d9d74f8392c6a0ab8d2d40njn      "It contains workarounds to several common problems.\n"
434b8329f0ce649067dc5d9d74f8392c6a0ab8d2d40njn      "In particular, if Valgrind aborted or crashed after\n"
435b8329f0ce649067dc5d9d74f8392c6a0ab8d2d40njn      "identifying problems in your program, there's a good chance\n"
436b8329f0ce649067dc5d9d74f8392c6a0ab8d2d40njn      "that fixing those problems will prevent Valgrind aborting or\n"
437b8329f0ce649067dc5d9d74f8392c6a0ab8d2d40njn      "crashing, especially if it happened in m_mallocfree.c.\n"
438b8329f0ce649067dc5d9d74f8392c6a0ab8d2d40njn      "\n"
439b8329f0ce649067dc5d9d74f8392c6a0ab8d2d40njn      "If that doesn't help, please report this bug to: %s\n\n"
440b8329f0ce649067dc5d9d74f8392c6a0ab8d2d40njn      "In the bug report, send all the above text, the valgrind\n"
441b8329f0ce649067dc5d9d74f8392c6a0ab8d2d40njn      "version, and what OS and version you are using.  Thanks.\n\n",
442b8329f0ce649067dc5d9d74f8392c6a0ab8d2d40njn      report);
443132bfccd21960e462352175f8553a5bdce8a210cnjn   VG_(exit)(1);
444132bfccd21960e462352175f8553a5bdce8a210cnjn}
445132bfccd21960e462352175f8553a5bdce8a210cnjn
446bbd9a21ca8518fc34cd7b6254a61d72f82495120florianvoid VG_(assert_fail) ( Bool isCore, const HChar* expr, const HChar* file,
447bbd9a21ca8518fc34cd7b6254a61d72f82495120florian                        Int line, const HChar* fn, const HChar* format, ... )
448132bfccd21960e462352175f8553a5bdce8a210cnjn{
44974054f4eac95d1ff81274b9d08be662494210134florian   va_list vargs, vargs_copy;
450bbd9a21ca8518fc34cd7b6254a61d72f82495120florian   const HChar* component;
451bbd9a21ca8518fc34cd7b6254a61d72f82495120florian   const HChar* bugs_to;
4525869dedfc08dd385b3672b51becbb713f5c8931aflorian   UInt written;
453132bfccd21960e462352175f8553a5bdce8a210cnjn
454132bfccd21960e462352175f8553a5bdce8a210cnjn   static Bool entered = False;
455132bfccd21960e462352175f8553a5bdce8a210cnjn   if (entered)
456745fc459ce4ce9264d8844bf1ab695d235a4ce01sewardj      VG_(exit)(2);
457132bfccd21960e462352175f8553a5bdce8a210cnjn   entered = True;
458132bfccd21960e462352175f8553a5bdce8a210cnjn
459132bfccd21960e462352175f8553a5bdce8a210cnjn   if (isCore) {
460132bfccd21960e462352175f8553a5bdce8a210cnjn      component = "valgrind";
461132bfccd21960e462352175f8553a5bdce8a210cnjn      bugs_to   = VG_BUGS_TO;
462132bfccd21960e462352175f8553a5bdce8a210cnjn   } else {
463132bfccd21960e462352175f8553a5bdce8a210cnjn      component = VG_(details).name;
464132bfccd21960e462352175f8553a5bdce8a210cnjn      bugs_to   = VG_(details).bug_reports_to;
465132bfccd21960e462352175f8553a5bdce8a210cnjn   }
466132bfccd21960e462352175f8553a5bdce8a210cnjn
467f349d55d40891b890d0da268cfb9f092af0fc8f4sewardj   if (VG_(clo_xml))
468738856f99eea33d86ce91dcb1d6cd5b151e307casewardj      VG_(printf_xml)("</valgrindoutput>\n");
469f349d55d40891b890d0da268cfb9f092af0fc8f4sewardj
470132bfccd21960e462352175f8553a5bdce8a210cnjn   // Treat vg_assert2(0, "foo") specially, as a panicky abort
471132bfccd21960e462352175f8553a5bdce8a210cnjn   if (VG_STREQ(expr, "0")) {
472132bfccd21960e462352175f8553a5bdce8a210cnjn      VG_(printf)("\n%s: %s:%d (%s): the 'impossible' happened.\n",
4738a7b41b41b46c0aa7bd4b6678b82285437e7f08cnjn                  component, file, line, fn );
474132bfccd21960e462352175f8553a5bdce8a210cnjn   } else {
475132bfccd21960e462352175f8553a5bdce8a210cnjn      VG_(printf)("\n%s: %s:%d (%s): Assertion '%s' failed.\n",
476132bfccd21960e462352175f8553a5bdce8a210cnjn                  component, file, line, fn, expr );
477132bfccd21960e462352175f8553a5bdce8a210cnjn   }
47874054f4eac95d1ff81274b9d08be662494210134florian
47974054f4eac95d1ff81274b9d08be662494210134florian   /* Check whether anything will be written */
48074054f4eac95d1ff81274b9d08be662494210134florian   HChar buf[5];
48174054f4eac95d1ff81274b9d08be662494210134florian   va_start(vargs, format);
48274054f4eac95d1ff81274b9d08be662494210134florian   va_copy(vargs_copy, vargs);
48374054f4eac95d1ff81274b9d08be662494210134florian   written = VG_(vsnprintf) ( buf, sizeof(buf), format, vargs );
48474054f4eac95d1ff81274b9d08be662494210134florian   va_end(vargs);
48574054f4eac95d1ff81274b9d08be662494210134florian
48674054f4eac95d1ff81274b9d08be662494210134florian   if (written > 0) {
48774054f4eac95d1ff81274b9d08be662494210134florian      VG_(printf)("%s: ", component);
48874054f4eac95d1ff81274b9d08be662494210134florian      VG_(vprintf)(format, vargs_copy);
48974054f4eac95d1ff81274b9d08be662494210134florian      VG_(printf)("\n");
49074054f4eac95d1ff81274b9d08be662494210134florian   }
491132bfccd21960e462352175f8553a5bdce8a210cnjn
49259570ffbe31930ab4d678754daaeec0715117a3dsewardj   report_and_quit(bugs_to, NULL);
493132bfccd21960e462352175f8553a5bdce8a210cnjn}
494132bfccd21960e462352175f8553a5bdce8a210cnjn
495132bfccd21960e462352175f8553a5bdce8a210cnjn__attribute__ ((noreturn))
496bbd9a21ca8518fc34cd7b6254a61d72f82495120florianstatic void panic ( const HChar* name, const HChar* report, const HChar* str,
497518850bf0da07ed3e2244e307268ae0fd80e93a8florian                    const UnwindStartRegs* startRegs )
498132bfccd21960e462352175f8553a5bdce8a210cnjn{
499f349d55d40891b890d0da268cfb9f092af0fc8f4sewardj   if (VG_(clo_xml))
500738856f99eea33d86ce91dcb1d6cd5b151e307casewardj      VG_(printf_xml)("</valgrindoutput>\n");
501132bfccd21960e462352175f8553a5bdce8a210cnjn   VG_(printf)("\n%s: the 'impossible' happened:\n   %s\n", name, str);
50259570ffbe31930ab4d678754daaeec0715117a3dsewardj   report_and_quit(report, startRegs);
503132bfccd21960e462352175f8553a5bdce8a210cnjn}
504132bfccd21960e462352175f8553a5bdce8a210cnjn
505518850bf0da07ed3e2244e307268ae0fd80e93a8florianvoid VG_(core_panic_at) ( const HChar* str, const UnwindStartRegs* startRegs )
506132bfccd21960e462352175f8553a5bdce8a210cnjn{
50759570ffbe31930ab4d678754daaeec0715117a3dsewardj   panic("valgrind", VG_BUGS_TO, str, startRegs);
508132bfccd21960e462352175f8553a5bdce8a210cnjn}
509132bfccd21960e462352175f8553a5bdce8a210cnjn
510bbd9a21ca8518fc34cd7b6254a61d72f82495120florianvoid VG_(core_panic) ( const HChar* str )
511132bfccd21960e462352175f8553a5bdce8a210cnjn{
51259570ffbe31930ab4d678754daaeec0715117a3dsewardj   VG_(core_panic_at)(str, NULL);
513132bfccd21960e462352175f8553a5bdce8a210cnjn}
514132bfccd21960e462352175f8553a5bdce8a210cnjn
515bbd9a21ca8518fc34cd7b6254a61d72f82495120florianvoid VG_(tool_panic) ( const HChar* str )
516132bfccd21960e462352175f8553a5bdce8a210cnjn{
51759570ffbe31930ab4d678754daaeec0715117a3dsewardj   panic(VG_(details).name, VG_(details).bug_reports_to, str, NULL);
518132bfccd21960e462352175f8553a5bdce8a210cnjn}
519132bfccd21960e462352175f8553a5bdce8a210cnjn
52049b45ba90382fd313e40555559f5f5aaa23033b4njn/* Print some helpful-ish text about unimplemented things, and give up. */
5218eb8bab992e3998c33770b0cdb16059a8b918a06sewardjvoid VG_(unimplemented) ( const HChar* format, ... )
522132bfccd21960e462352175f8553a5bdce8a210cnjn{
5238eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   va_list vargs;
5248eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   HChar msg[256];
5258eb8bab992e3998c33770b0cdb16059a8b918a06sewardj
5268eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   va_start(vargs, format);
5278eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   VG_(vsnprintf)(msg, sizeof(msg), format, vargs);
5288eb8bab992e3998c33770b0cdb16059a8b918a06sewardj   va_end(vargs);
5298eb8bab992e3998c33770b0cdb16059a8b918a06sewardj
530f349d55d40891b890d0da268cfb9f092af0fc8f4sewardj   if (VG_(clo_xml))
531738856f99eea33d86ce91dcb1d6cd5b151e307casewardj      VG_(printf_xml)("</valgrindoutput>\n");
532738856f99eea33d86ce91dcb1d6cd5b151e307casewardj   VG_(umsg)("\n");
533738856f99eea33d86ce91dcb1d6cd5b151e307casewardj   VG_(umsg)("Valgrind detected that your program requires\n");
534738856f99eea33d86ce91dcb1d6cd5b151e307casewardj   VG_(umsg)("the following unimplemented functionality:\n");
535738856f99eea33d86ce91dcb1d6cd5b151e307casewardj   VG_(umsg)("   %s\n", msg);
536738856f99eea33d86ce91dcb1d6cd5b151e307casewardj   VG_(umsg)("This may be because the functionality is hard to implement,\n");
537738856f99eea33d86ce91dcb1d6cd5b151e307casewardj   VG_(umsg)("or because no reasonable program would behave this way,\n");
538738856f99eea33d86ce91dcb1d6cd5b151e307casewardj   VG_(umsg)("or because nobody has yet needed it.  "
539738856f99eea33d86ce91dcb1d6cd5b151e307casewardj             "In any case, let us know at\n");
540738856f99eea33d86ce91dcb1d6cd5b151e307casewardj   VG_(umsg)("%s and/or try to work around the problem, if you can.\n",
541738856f99eea33d86ce91dcb1d6cd5b151e307casewardj             VG_BUGS_TO);
542738856f99eea33d86ce91dcb1d6cd5b151e307casewardj   VG_(umsg)("\n");
543738856f99eea33d86ce91dcb1d6cd5b151e307casewardj   VG_(umsg)("Valgrind has to exit now.  Sorry.  Bye!\n");
544738856f99eea33d86ce91dcb1d6cd5b151e307casewardj   VG_(umsg)("\n");
5454f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe   VG_(show_sched_status)(False,  // host_stacktrace
54638a74d2cc4670e3eb559adff51a376cd6ec98005philippe                          False,  // stack_usage
5474f6f336badda2171d6e842cca3de63b53f4c9f0bphilippe                          False); // exited_threads
548132bfccd21960e462352175f8553a5bdce8a210cnjn   VG_(exit)(1);
549132bfccd21960e462352175f8553a5bdce8a210cnjn}
550132bfccd21960e462352175f8553a5bdce8a210cnjn
551132bfccd21960e462352175f8553a5bdce8a210cnjn/*--------------------------------------------------------------------*/
552132bfccd21960e462352175f8553a5bdce8a210cnjn/*--- end                                                          ---*/
553132bfccd21960e462352175f8553a5bdce8a210cnjn/*--------------------------------------------------------------------*/
554