[Aptitude-svn-commit] r4319 - in branches/aptitude-0.3/aptitude: . src src/generic/util tests

Daniel Burrows dburrows at costa.debian.org
Tue Sep 27 23:14:04 UTC 2005


Author: dburrows
Date: Tue Sep 27 23:14:01 2005
New Revision: 4319

Added:
   branches/aptitude-0.3/aptitude/src/generic/util/exception.cc
Modified:
   branches/aptitude-0.3/aptitude/ChangeLog
   branches/aptitude-0.3/aptitude/configure.ac
   branches/aptitude-0.3/aptitude/src/generic/util/Makefile.am
   branches/aptitude-0.3/aptitude/src/generic/util/exception.h
   branches/aptitude-0.3/aptitude/src/main.cc
   branches/aptitude-0.3/aptitude/tests/Makefile.am
Log:
Add support for backtracing the program when creating an exception

Modified: branches/aptitude-0.3/aptitude/ChangeLog
==============================================================================
--- branches/aptitude-0.3/aptitude/ChangeLog	(original)
+++ branches/aptitude-0.3/aptitude/ChangeLog	Tue Sep 27 23:14:01 2005
@@ -1,5 +1,10 @@
 2005-09-27  Daniel Burrows  <dburrows at debian.org>
 
+	* configure.ac, src/generic/util/Makefile.am, src/generic/util/exception.cc, src/generic/util/exception.h, src/main.cc, tests/Makefile.am:
+
+	  Add support for backtracing the program when creating an
+	  exception on systems that support it.  (currently rather broken)
+
 	* src/download.cc:
 
 	  Remove the assumption that stdin is line-buffered; explicitly

Modified: branches/aptitude-0.3/aptitude/configure.ac
==============================================================================
--- branches/aptitude-0.3/aptitude/configure.ac	(original)
+++ branches/aptitude-0.3/aptitude/configure.ac	Tue Sep 27 23:14:01 2005
@@ -45,6 +45,8 @@
 
 AC_CHECK_HEADER(apt-pkg/init.h, , [AC_MSG_ERROR([Can't find the APT header files -- please install libapt-pkg-dev])])
 
+AC_CHECK_HEADER(execinfo.h, [AC_DEFINE([HAVE_EXECINFO_H], [], [Define if the execinfo file (for self-backtracing) is available])])
+
 HASH_INC=""
 HASH_CLS=""
 

Modified: branches/aptitude-0.3/aptitude/src/generic/util/Makefile.am
==============================================================================
--- branches/aptitude-0.3/aptitude/src/generic/util/Makefile.am	(original)
+++ branches/aptitude-0.3/aptitude/src/generic/util/Makefile.am	Tue Sep 27 23:14:01 2005
@@ -8,6 +8,7 @@
 libgeneric_util_a_SOURCES = \
 	dense_setset.h \
 	event_queue.h \
+	exception.cc \
 	exception.h \
 	immset.h \
 	mut_fun.h \

Added: branches/aptitude-0.3/aptitude/src/generic/util/exception.cc
==============================================================================
--- (empty file)
+++ branches/aptitude-0.3/aptitude/src/generic/util/exception.cc	Tue Sep 27 23:14:01 2005
@@ -0,0 +1,51 @@
+// exception.cc
+//
+//   Copyright (C) 2005 Daniel Burrows
+//
+//   This program 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 of
+//   the License, or (at your option) any later version.
+//
+//   This program 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 program; see the file COPYING.  If not, write to
+//   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+//   Boston, MA 02111-1307, USA.
+
+#include "exception.h"
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifdef HAVE_EXECINFO_H
+
+#include <execinfo.h>
+
+Exception::Exception()
+{
+  void * backtrace_array[100];
+  int symbol_count = backtrace(backtrace_array, sizeof(backtrace_array)/sizeof(backtrace_array[0]));
+  char **symbols = backtrace_symbols(backtrace_array, symbol_count);
+
+  for(int i = 0; i < symbol_count; ++i)
+    {
+      bt += symbols[i];
+      bt += "\n";
+    }
+
+  free(symbols);
+}
+
+#else
+
+Exception::Exception()
+{
+}
+
+#endif

Modified: branches/aptitude-0.3/aptitude/src/generic/util/exception.h
==============================================================================
--- branches/aptitude-0.3/aptitude/src/generic/util/exception.h	(original)
+++ branches/aptitude-0.3/aptitude/src/generic/util/exception.h	Tue Sep 27 23:14:01 2005
@@ -19,17 +19,27 @@
 //
 // A generic exception class supporting std::string error messages
 // (unlike std::exception, which only supports const char* error
-// messages).
+// messages).  It also implicitly generates a backtrace when it is
+// created.  (NB: throwing this sort of exception when malloc() is
+// unavailable is a Bad Idea, and it's expensive to copy these around)
 
 #ifndef EXCEPTION_H
 #define EXCEPTION_H
 
 #include <string>
+#include <vector>
 
 class Exception
 {
+  /** A textual listing of the symbols on the stack at the time that
+   *  the exception is created.
+   */
+  std::string bt;
 public:
-  virtual std::string errmsg() const=0;
+  Exception();
+
+  std::string get_backtrace() const { return bt; }
+  virtual std::string errmsg() const = 0;
   virtual ~Exception() {}
 };
 

Modified: branches/aptitude-0.3/aptitude/src/main.cc
==============================================================================
--- branches/aptitude-0.3/aptitude/src/main.cc	(original)
+++ branches/aptitude-0.3/aptitude/src/main.cc	Tue Sep 27 23:14:01 2005
@@ -477,6 +477,10 @@
       catch(const Exception &e)
 	{
 	  fprintf(stderr, _("Uncaught exception: %s\n"), e.errmsg().c_str());
+
+	  std::string backtrace = e.get_backtrace();
+	  if(!backtrace.empty())
+	    fprintf(stderr, _("Backtrace:\n%s"), backtrace.c_str());
 	  return -1;
 	}
     }
@@ -514,6 +518,10 @@
   catch(const Exception &e)
     {
       fprintf(stderr, _("Uncaught exception: %s\n"), e.errmsg().c_str());
+
+      std::string backtrace = e.get_backtrace();
+      if(!backtrace.empty())
+	fprintf(stderr, _("Backtrace:\n%s"), backtrace.c_str());
       return -1;
     }
 

Modified: branches/aptitude-0.3/aptitude/tests/Makefile.am
==============================================================================
--- branches/aptitude-0.3/aptitude/tests/Makefile.am	(original)
+++ branches/aptitude-0.3/aptitude/tests/Makefile.am	Tue Sep 27 23:14:01 2005
@@ -1,7 +1,7 @@
 MAINTAINERCLEANFILES = Makefile.in
 
 INCLUDES = -Wall @WERROR@ -I$(top_srcdir) -I$(top_srcdir)/src -I$(srcdir)
-LDADD = ../src/generic/apt/libgeneric-apt.a ../src/generic/util/libgeneric-util.a ../src/generic/problemresolver/dummy_universe.o -lcppunit
+LDADD = ../src/generic/apt/libgeneric-apt.a ../src/generic/problemresolver/dummy_universe.o ../src/generic/util/libgeneric-util.a -lcppunit
 
 check_PROGRAMS = test
 



More information about the Aptitude-svn-commit mailing list