This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

verbose terminate() on by default, pass 2


This takes into account some suggestions:

 - verbose termination implementation in libsupc++, not libstdc++
 - verbose termination implementation in its own translation unit
 - handler definitions in their own translation units
 - no fprintf
 - more verbose text, no `...' quoting

Testsuite shows no regressions.  Testing with

    #include <exception>
    int main() { throw 42; }

and "g++" DTRT when run.  Testing with "gcc foo.cc -lsupc++" also DTRT
when run, and does not require libstdc++.so at runtime.

I also experimented with GNU ld's handy (and wrongly documented, grumble)
-M option.  When using .a's, this shows which file from the archive is
being pulled in, and which file+symbol is causing it.  With the current
patch, we see what we expect:  eh_terminate requires eh_term_handler,
which then causes this chain:

    Is pulled in                       Because of
libsupc++.a(eh_term_handler.o)  libsupc++.a(eh_terminate.o) (__cxxabiv1::__terminate_handler)
libsupc++.a(vterminate.o)       libsupc++.a(eh_term_handler.o) (__gnu_cxx::__verbose_terminate_handler())
libsupc++.a(cxa_demangle.o)     libsupc++.a(vterminate.o) (__cxa_demangle)
libsupc++.a(dyn-string.o)       libsupc++.a(cxa_demangle.o) (__cxa_dyn_string_init)
libsupc++.a(eh_type.o)          libsupc++.a(vterminate.o) (__cxa_current_exception_type)


I then edited eh_term_handler.cc to create "the embedded aoliva scenario,"
in which the handler is initialized to std::abort instead of the verbose
implementation.  Using the testcase above with "gcc foo.cc -lsupc++"
continues to DTRT, and produced a map of

    Is pulled in                       Because of
libsupc++.a(eh_term_handler.o)  libsupc++.a(eh_terminate.o) (__cxxabiv1::__terminate_handler)

i.e., no verbose stuff, no demangler, no strings, no I/O.


I'm willing to make more changes, but I'd like to get this checked in and out
of my increasingly-fragile tree before playing any more.  Barring objections,
I'll check this in sometime tomorrow before leaving home to forage for food.
(Guess who forgot to buy groceries before the stores and restaurants closed
for Christmas?)

Also planned:  doc updates (obviously), and some comment in
eh_term_handler.cc saying, "to go back to the lower-overhead silent-death
handler, include <cstdlib> and put std::abort on the right hand side of
the equals sign".



2002-12-24  Phil Edwards  <pme@gcc.gnu.org>

	* src/vterminate.cc:  Move to...
	* libsupc++/vterminate.cc:  ...here.  New file.  Replace fprintf with
	writestr macro.  Slight reword to explanatory text.
	* libsupc++/eh_terminate.cc (__cxxabiv1::__terminate_handler,
	__cxxabiv1::__unexpected_handler):  Break definitions out to...
	* libsupc++/eh_unex_handler.cc:  ...here (new file), and...
	* libsupc++/eh_term_handler.cc:  ...here (new file).  Initialize
	__terminate_handler with __gnu_cxx::__verbose_terminate_handler
	instead of std::abort.

	* libsupc++/Makefile.am (sources), src/Makefile.am (sources):  Update.
	* libsupc++/Makefile.in, src/Makefile.in:  Regenerate.


Index: src/vterminate.cc
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/libstdc++-v3/src/vterminate.cc,v
retrieving revision 1.7
diff -u -3 -p -r1.7 vterminate.cc
--- src/vterminate.cc	18 Dec 2002 16:31:35 -0000	1.7
+++ src/vterminate.cc	24 Dec 2002 00:45:14 -0000
@@ -27,12 +27,20 @@
 // invalidate any other reasons why the executable file might be covered by
 // the GNU General Public License.
 
+#include <bits/c++config.h>
 #include <cstdlib>
-#include <cstdio>
 #include <exception>
 #include <exception_defines.h>
 #include <cxxabi.h>
 
+#ifdef _GLIBCPP_HAVE_UNISTD_H
+# include <unistd.h>
+# define writestr(str)  write(2, str, sizeof(str) - 1)
+#else
+# include <cstdio>
+# define writestr(str)  std::fputs(str, stderr)
+#endif
+
 using namespace std;
 using namespace abi;
 
@@ -57,8 +65,12 @@ namespace __gnu_cxx
 	  
 	  dem = __cxa_demangle(name, 0, 0, &status);
 
-	  fprintf(stderr, "terminate called after throwing a `%s'\n", 
-		  status == 0 ? dem : name);
+	  writestr("terminate called after throwing a `");
+	  if (status == 0)
+	    writestr(dem);
+	  else
+	    writestr(name);
+	  writestr("'\n");
 
 	  if (status == 0)
 	    free(dem);
@@ -69,12 +81,17 @@ namespace __gnu_cxx
 	try { __throw_exception_again; }
 #ifdef __EXCEPTIONS
 	catch (exception &exc)
-	  { fprintf(stderr, "  what(): %s\n", exc.what()); }
+	  {
+	    char const *w = exc.what();
+	    writestr("  what(): ");
+	    writestr(w);
+	    writestr("\n");
+          }
 #endif
 	catch (...) { }
       }
     else
-      fprintf(stderr, "terminate called without an active exception\n");
+      writestr("terminate called without an active exception\n");
     
     abort();
   }
Index: src/Makefile.am
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/libstdc++-v3/src/Makefile.am,v
retrieving revision 1.114
diff -u -3 -p -r1.114 Makefile.am
--- src/Makefile.am	16 Dec 2002 19:02:01 -0000	1.114
+++ src/Makefile.am	25 Dec 2002 01:55:19 -0000
@@ -143,7 +143,6 @@ sources = \
 	string-inst.cc \
 	strstream.cc \
 	valarray-inst.cc \
-	vterminate.cc \
 	wstring-inst.cc \
 	${target_sources} \
 	${target_sources_extra}
Index: libsupc++/vterminate.cc
===================================================================
RCS file: libsupc++/vterminate.cc
diff -N libsupc++/vterminate.cc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ libsupc++/vterminate.cc	25 Dec 2002 01:54:21 -0000
@@ -0,0 +1,98 @@
+// Verbose terminate_handler -*- C++ -*-
+
+// Copyright (C) 2001, 2002 Free Software Foundation
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// As a special exception, you may use this file as part of a free software
+// library without restriction.  Specifically, if other files instantiate
+// templates or use macros or inline functions from this file, or you compile
+// this file and link it with other files to produce an executable, this
+// file does not by itself cause the resulting executable to be covered by
+// the GNU General Public License.  This exception does not however
+// invalidate any other reasons why the executable file might be covered by
+// the GNU General Public License.
+
+#include <bits/c++config.h>
+#include <cstdlib>
+#include <exception>
+#include <exception_defines.h>
+#include <cxxabi.h>
+
+#ifdef _GLIBCPP_HAVE_UNISTD_H
+# include <unistd.h>
+# define writestr(str)  write(2, str, sizeof(str) - 1)
+#else
+# include <cstdio>
+# define writestr(str)  std::fputs(str, stderr)
+#endif
+
+using namespace std;
+using namespace abi;
+
+namespace __gnu_cxx
+{
+  /* A replacement for the standard terminate_handler which prints
+   more information about the terminating exception (if any) on
+   stderr.  */
+  void __verbose_terminate_handler()
+  {
+    // Make sure there was an exception; terminate is also called for an
+    // attempt to rethrow when there is no suitable exception.
+    type_info *t = __cxa_current_exception_type();
+    if (t)
+      {
+	char const *name = t->name();
+	// Note that "name" is the mangled name.
+	
+	{
+	  int status = -1;
+	  char *dem = 0;
+	  
+	  dem = __cxa_demangle(name, 0, 0, &status);
+
+	  writestr("terminate called after throwing an instance of '");
+	  if (status == 0)
+	    writestr(dem);
+	  else
+	    writestr(name);
+	  writestr("'\n");
+
+	  if (status == 0)
+	    free(dem);
+	}
+
+	// If the exception is derived from std::exception, we can give more
+	// information.
+	try { __throw_exception_again; }
+#ifdef __EXCEPTIONS
+	catch (exception &exc)
+	  {
+	    char const *w = exc.what();
+	    writestr("  what():  ");
+	    writestr(w);
+	    writestr("\n");
+          }
+#endif
+	catch (...) { }
+      }
+    else
+      writestr("terminate called without an active exception\n");
+    
+    abort();
+  }
+} // namespace __gnu_cxx
Index: libsupc++/eh_terminate.cc
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/libstdc++-v3/libsupc++/eh_terminate.cc,v
retrieving revision 1.2
diff -u -3 -p -r1.2 eh_terminate.cc
--- libsupc++/eh_terminate.cc	9 Jun 2001 06:49:13 -0000	1.2
+++ libsupc++/eh_terminate.cc	25 Dec 2002 02:37:01 -0000
@@ -1,5 +1,5 @@
 // -*- C++ -*- std::terminate, std::unexpected and friends.
-// Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 
+// Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
 // Free Software Foundation
 //
 // This file is part of GNU CC.
@@ -35,10 +35,6 @@
 #include "exception_defines.h"
 
 using namespace __cxxabiv1;
-
-/* The current installed user handlers.  */
-std::terminate_handler __cxxabiv1::__terminate_handler = std::abort;
-std::unexpected_handler __cxxabiv1::__unexpected_handler = std::terminate;
 
 void
 __cxxabiv1::__terminate (std::terminate_handler handler)
Index: libsupc++/eh_term_handler.cc
===================================================================
RCS file: libsupc++/eh_term_handler.cc
diff -N libsupc++/eh_term_handler.cc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ libsupc++/eh_term_handler.cc	25 Dec 2002 02:47:26 -0000
@@ -0,0 +1,35 @@
+// -*- C++ -*- std::terminate handler
+// Copyright (C) 2002 Free Software Foundation
+//
+// This file is part of GNU CC.
+//
+// GNU CC is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// GNU CC is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with GNU CC; see the file COPYING.  If not, write to
+// the Free Software Foundation, 59 Temple Place - Suite 330,
+// Boston, MA 02111-1307, USA. 
+
+// As a special exception, you may use this file as part of a free software
+// library without restriction.  Specifically, if other files instantiate
+// templates or use macros or inline functions from this file, or you compile
+// this file and link it with other files to produce an executable, this
+// file does not by itself cause the resulting executable to be covered by
+// the GNU General Public License.  This exception does not however
+// invalidate any other reasons why the executable file might be covered by
+// the GNU General Public License.
+
+#include "unwind-cxx.h"
+
+/* The current installed user handler.  */
+std::terminate_handler __cxxabiv1::__terminate_handler =
+                                       __gnu_cxx::__verbose_terminate_handler;
+
Index: libsupc++/eh_unex_handler.cc
===================================================================
RCS file: libsupc++/eh_unex_handler.cc
diff -N libsupc++/eh_unex_handler.cc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ libsupc++/eh_unex_handler.cc	25 Dec 2002 02:36:45 -0000
@@ -0,0 +1,34 @@
+// -*- C++ -*- std::unexpected handler
+// Copyright (C) 2002 Free Software Foundation
+//
+// This file is part of GNU CC.
+//
+// GNU CC is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// GNU CC is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with GNU CC; see the file COPYING.  If not, write to
+// the Free Software Foundation, 59 Temple Place - Suite 330,
+// Boston, MA 02111-1307, USA. 
+
+// As a special exception, you may use this file as part of a free software
+// library without restriction.  Specifically, if other files instantiate
+// templates or use macros or inline functions from this file, or you compile
+// this file and link it with other files to produce an executable, this
+// file does not by itself cause the resulting executable to be covered by
+// the GNU General Public License.  This exception does not however
+// invalidate any other reasons why the executable file might be covered by
+// the GNU General Public License.
+
+#include "unwind-cxx.h"
+
+/* The current installed user handler.  */
+std::unexpected_handler __cxxabiv1::__unexpected_handler = std::terminate;
+
Index: libsupc++/Makefile.am
===================================================================
RCS file: /home/pme/Repositories/GCC/gcc/libstdc++-v3/libsupc++/Makefile.am,v
retrieving revision 1.37
diff -u -3 -p -r1.37 Makefile.am
--- libsupc++/Makefile.am	16 Dec 2002 19:02:00 -0000	1.37
+++ libsupc++/Makefile.am	25 Dec 2002 02:38:16 -0000
@@ -81,9 +81,11 @@ sources = \
 	eh_exception.cc \
 	eh_globals.cc \
 	eh_personality.cc \
+	eh_term_handler.cc \
 	eh_terminate.cc \
 	eh_throw.cc \
 	eh_type.cc \
+	eh_unex_handler.cc \
 	guard.cc \
 	new_handler.cc \
 	new_op.cc \
@@ -93,7 +95,8 @@ sources = \
 	pure.cc \
 	tinfo.cc \
 	tinfo2.cc \
-	vec.cc
+	vec.cc \
+	vterminate.cc
 
 libsupc___la_SOURCES = $(sources) $(c_sources)
 libsupc__convenience_la_SOURCES = $(sources) $(c_sources)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]