1139823Simp// Verbose terminate_handler -*- C++ -*-
2157170Srwatson
3157170Srwatson// Copyright (C) 2001, 2002, 2004, 2005 Free Software Foundation
4139923Srwatson//
5157170Srwatson// This file is part of the GNU ISO C++ Library.  This library is free
611819Sjulian// software; you can redistribute it and/or modify it under the
711819Sjulian// terms of the GNU General Public License as published by the
811819Sjulian// Free Software Foundation; either version 2, or (at your option)
911819Sjulian// any later version.
1011819Sjulian
1111819Sjulian// This library is distributed in the hope that it will be useful,
1211819Sjulian// but WITHOUT ANY WARRANTY; without even the implied warranty of
1311819Sjulian// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1411819Sjulian// GNU General Public License for more details.
15165899Srwatson
16165899Srwatson// You should have received a copy of the GNU General Public License along
17165899Srwatson// with this library; see the file COPYING.  If not, write to the Free
18165899Srwatson// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19165899Srwatson// USA.
20165899Srwatson
21165899Srwatson// As a special exception, you may use this file as part of a free software
22165899Srwatson// library without restriction.  Specifically, if other files instantiate
23165899Srwatson// templates or use macros or inline functions from this file, or you compile
24165899Srwatson// this file and link it with other files to produce an executable, this
25165899Srwatson// file does not by itself cause the resulting executable to be covered by
26165899Srwatson// the GNU General Public License.  This exception does not however
27165899Srwatson// invalidate any other reasons why the executable file might be covered by
28165899Srwatson// the GNU General Public License.
29165899Srwatson
30165899Srwatson#include <bits/c++config.h>
31165899Srwatson
32165899Srwatson#if _GLIBCXX_HOSTED
33165899Srwatson#include <cstdlib>
34165899Srwatson#include <exception>
35165899Srwatson#include <exception_defines.h>
36165899Srwatson#include <cxxabi.h>
37165899Srwatson# include <cstdio>
38165899Srwatson
39165899Srwatsonusing namespace std;
40165899Srwatsonusing namespace abi;
41165899Srwatson
4211819Sjulian_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
4311819Sjulian
4411819Sjulian  // A replacement for the standard terminate_handler which prints
4511819Sjulian  // more information about the terminating exception (if any) on
4611819Sjulian  // stderr.
4711819Sjulian  void __verbose_terminate_handler()
4811819Sjulian  {
4911819Sjulian    static bool terminating;
5011819Sjulian    if (terminating)
5111819Sjulian      {
5211819Sjulian	fputs("terminate called recursively\n", stderr);
5311819Sjulian	abort ();
5411819Sjulian      }
5511819Sjulian    terminating = true;
5611819Sjulian
5711819Sjulian    // Make sure there was an exception; terminate is also called for an
5811819Sjulian    // attempt to rethrow when there is no suitable exception.
5911819Sjulian    type_info *t = __cxa_current_exception_type();
6011819Sjulian    if (t)
6111819Sjulian      {
6212057Sjulian	// Note that "name" is the mangled name.
6311819Sjulian	char const *name = t->name();
6411819Sjulian	{
65116189Sobrien	  int status = -1;
66116189Sobrien	  char *dem = 0;
67116189Sobrien
6811819Sjulian	  dem = __cxa_demangle(name, 0, 0, &status);
6911819Sjulian
7011819Sjulian	  fputs("terminate called after throwing an instance of '", stderr);
7111819Sjulian	  if (status == 0)
7211819Sjulian	    fputs(dem, stderr);
7311819Sjulian	  else
7459604Sobrien	    fputs(name, stderr);
7513266Swollman	  fputs("'\n", stderr);
7611819Sjulian
7711819Sjulian	  if (status == 0)
7811819Sjulian	    free(dem);
7911819Sjulian	}
8011819Sjulian
8111819Sjulian	// If the exception is derived from std::exception, we can
8211819Sjulian	// give more information.
8311819Sjulian	try { __throw_exception_again; }
8411819Sjulian#ifdef __EXCEPTIONS
8511819Sjulian	catch (exception &exc)
8611819Sjulian	  {
8725652Sjhay	    char const *w = exc.what();
8813266Swollman	    fputs("  what():  ", stderr);
89180816Strhodes	    fputs(w, stderr);
9011819Sjulian	    fputs("\n", stderr);
9126965Sjhay          }
9225652Sjhay#endif
93180816Strhodes	catch (...) { }
9411819Sjulian      }
9526965Sjhay    else
9613266Swollman      fputs("terminate called without an active exception\n", stderr);
97180816Strhodes
9813266Swollman    abort();
9926965Sjhay  }
10025652Sjhay
101180816Strhodes_GLIBCXX_END_NAMESPACE
10211819Sjulian
103139442Srwatson#endif
104139442Srwatson