vterminate.cc revision 132720
1132720Skan// Verbose terminate_handler -*- C++ -*-
2132720Skan
3132720Skan// Copyright (C) 2001, 2002, 2004 Free Software Foundation
4132720Skan//
5132720Skan// This file is part of the GNU ISO C++ Library.  This library is free
6132720Skan// software; you can redistribute it and/or modify it under the
7132720Skan// terms of the GNU General Public License as published by the
8132720Skan// Free Software Foundation; either version 2, or (at your option)
9132720Skan// any later version.
10132720Skan
11132720Skan// This library is distributed in the hope that it will be useful,
12132720Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
13132720Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14132720Skan// GNU General Public License for more details.
15132720Skan
16132720Skan// You should have received a copy of the GNU General Public License along
17132720Skan// with this library; see the file COPYING.  If not, write to the Free
18132720Skan// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19132720Skan// USA.
20132720Skan
21132720Skan// As a special exception, you may use this file as part of a free software
22132720Skan// library without restriction.  Specifically, if other files instantiate
23132720Skan// templates or use macros or inline functions from this file, or you compile
24132720Skan// this file and link it with other files to produce an executable, this
25132720Skan// file does not by itself cause the resulting executable to be covered by
26132720Skan// the GNU General Public License.  This exception does not however
27132720Skan// invalidate any other reasons why the executable file might be covered by
28132720Skan// the GNU General Public License.
29132720Skan
30132720Skan#include <bits/c++config.h>
31132720Skan#include <cstdlib>
32132720Skan#include <exception>
33132720Skan#include <exception_defines.h>
34132720Skan#include <cxxabi.h>
35132720Skan# include <cstdio>
36132720Skan
37132720Skanusing namespace std;
38132720Skanusing namespace abi;
39132720Skan
40132720Skannamespace __gnu_cxx
41132720Skan{
42132720Skan  // A replacement for the standard terminate_handler which prints
43132720Skan  // more information about the terminating exception (if any) on
44132720Skan  // stderr.
45132720Skan  void __verbose_terminate_handler()
46132720Skan  {
47132720Skan    static bool terminating;
48132720Skan    if (terminating)
49132720Skan      {
50132720Skan	fputs("terminate called recursively\n", stderr);
51132720Skan	abort ();
52132720Skan      }
53132720Skan    terminating = true;
54132720Skan
55132720Skan    // Make sure there was an exception; terminate is also called for an
56132720Skan    // attempt to rethrow when there is no suitable exception.
57132720Skan    type_info *t = __cxa_current_exception_type();
58132720Skan    if (t)
59132720Skan      {
60132720Skan	// Note that "name" is the mangled name.
61132720Skan	char const *name = t->name();
62132720Skan	{
63132720Skan	  int status = -1;
64132720Skan	  char *dem = 0;
65132720Skan
66132720Skan	  dem = __cxa_demangle(name, 0, 0, &status);
67132720Skan
68132720Skan	  fputs("terminate called after throwing an instance of '", stderr);
69132720Skan	  if (status == 0)
70132720Skan	    fputs(dem, stderr);
71132720Skan	  else
72132720Skan	    fputs(name, stderr);
73132720Skan	  fputs("'\n", stderr);
74132720Skan
75132720Skan	  if (status == 0)
76132720Skan	    free(dem);
77132720Skan	}
78132720Skan
79132720Skan	// If the exception is derived from std::exception, we can
80132720Skan	// give more information.
81132720Skan	try { __throw_exception_again; }
82132720Skan#ifdef __EXCEPTIONS
83132720Skan	catch (exception &exc)
84132720Skan	  {
85132720Skan	    char const *w = exc.what();
86132720Skan	    fputs("  what():  ", stderr);
87132720Skan	    fputs(w, stderr);
88132720Skan	    fputs("\n", stderr);
89132720Skan          }
90132720Skan#endif
91132720Skan	catch (...) { }
92132720Skan      }
93132720Skan    else
94132720Skan      fputs("terminate called without an active exception\n", stderr);
95132720Skan
96132720Skan    abort();
97132720Skan  }
98132720Skan} // namespace __gnu_cxx
99