unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Stefan Monnier <monnier@IRO.UMontreal.CA>
Cc: Juanma Barranquero <lekktu@gmail.com>,
	Eli Zaretskii <eliz@gnu.org>,
	emacs-devel@gnu.org
Subject: C backtraces for Emacs
Date: Wed, 22 Aug 2012 02:24:31 -0700	[thread overview]
Message-ID: <5034A54F.6020006@cs.ucla.edu> (raw)
In-Reply-To: <jwvzk5okufw.fsf-monnier+emacs@gnu.org>

On 08/21/2012 10:50 AM, Stefan Monnier wrote:
> If someone wants to *additionally* provide a patch to use glibc's
> `backtrace' in our `eassert' that's fine

I've prepared a patch to do this additional work, enclosed below.  The
key part is at the end; the rest is boilerplate for non-GNUish
systems.  I've tested this on Fedora 15, Solaris 11, and Solaris 10.

I'll hold off installing this to give Eli and Juanma a heads-up, as it
creates a new substitute header <execinfo.h> on non-GNUish systems,
and the Windows build will need to do something similar.  If I understand
things correctly Windows already does backtraces in a different way, so
its execinfo.h can just be a copy of execinfo.in.h.

=== modified file 'ChangeLog'
--- ChangeLog	2012-08-22 06:55:44 +0000
+++ ChangeLog	2012-08-22 08:59:10 +0000
@@ -1,3 +1,11 @@
+2012-08-22  Paul Eggert  <eggert@cs.ucla.edu>
+
+	On assertion failure, print backtrace if available.
+	Merge from gnulib, incorporating:
+	2012-08-22 execinfo: new module
+	* lib/gnulib.mk, m4/gnulib-comp.m4: Regenerate.
+	* lib/execinfo.c, lib/execinfo.in.h, m4/execinfo.m4: New files.
+
 2012-08-22  Glenn Morris  <rgm@gnu.org>
 
 	* Makefile.in (install-arch-dep): If NO_BIN_LINK is non-null,

=== modified file 'admin/ChangeLog'
--- admin/ChangeLog	2012-08-16 21:58:44 +0000
+++ admin/ChangeLog	2012-08-22 08:59:10 +0000
@@ -1,3 +1,8 @@
+2012-08-22  Paul Eggert  <eggert@cs.ucla.edu>
+
+	On assertion failure, print backtrace if available.
+	* merge-gnulib (GNULIB_MODULES): Add execinfo.
+
 2012-08-16  Paul Eggert  <eggert@cs.ucla.edu>
 
 	Use ASCII tests for character types.

=== modified file 'admin/merge-gnulib'
--- admin/merge-gnulib	2012-08-16 21:58:44 +0000
+++ admin/merge-gnulib	2012-08-22 08:59:10 +0000
@@ -28,7 +28,7 @@
 GNULIB_MODULES='
   alloca-opt c-ctype c-strcase
   careadlinkat crypto/md5 crypto/sha1 crypto/sha256 crypto/sha512
-  dtoastr dtotimespec dup2 environ
+  dtoastr dtotimespec dup2 environ execinfo
   filemode getloadavg getopt-gnu gettime gettimeofday
   ignore-value intprops largefile lstat
   manywarnings mktime pselect pthread_sigmask readlink

=== added file 'lib/execinfo.c'
--- lib/execinfo.c	1970-01-01 00:00:00 +0000
+++ lib/execinfo.c	2012-08-22 08:59:10 +0000
@@ -0,0 +1,3 @@
+#include <config.h>
+#define _GL_EXECINFO_INLINE _GL_EXTERN_INLINE
+#include "execinfo.h"

=== added file 'lib/execinfo.in.h'
--- lib/execinfo.in.h	1970-01-01 00:00:00 +0000
+++ lib/execinfo.in.h	2012-08-22 08:59:10 +0000
@@ -0,0 +1,54 @@
+/* Information about executables.
+
+   Copyright (C) 2012 Free Software Foundation, Inc.
+
+   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 3 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.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Paul Eggert.  */
+
+#ifndef _GL_EXECINFO_H
+#define _GL_EXECINFO_H
+
+_GL_INLINE_HEADER_BEGIN
+#ifndef _GL_EXECINFO_INLINE
+# define _GL_EXECINFO_INLINE _GL_INLINE
+#endif
+
+_GL_EXECINFO_INLINE int
+backtrace (void **buffer, int size)
+{
+  (void) buffer;
+  (void) size;
+  return 0;
+}
+
+_GL_EXECINFO_INLINE char **
+backtrace_symbols (void *const *buffer, int size)
+{
+  (void) buffer;
+  (void) size;
+  return 0;
+}
+
+_GL_EXECINFO_INLINE void
+backtrace_symbols_fd (void *const *buffer, int size, int fd)
+{
+  (void) buffer;
+  (void) size;
+  (void) fd;
+}
+
+_GL_INLINE_HEADER_END
+
+#endif

=== modified file 'lib/gnulib.mk'
--- lib/gnulib.mk	2012-08-19 23:31:24 +0000
+++ lib/gnulib.mk	2012-08-22 08:59:10 +0000
@@ -21,7 +21,7 @@
 # the same distribution terms as the rest of that program.
 #
 # Generated by gnulib-tool.
-# Reproduce by: gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=errno --avoid=fcntl --avoid=fcntl-h --avoid=fstat --avoid=msvc-inval --avoid=msvc-nothrow --avoid=raise --avoid=select --avoid=sigprocmask --avoid=sys_types --avoid=threadlib --makefile-name=gnulib.mk --conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files alloca-opt c-ctype c-strcase careadlinkat crypto/md5 crypto/sha1 crypto/sha256 crypto/sha512 dtoastr dtotimespec dup2 environ filemode getloadavg getopt-gnu gettime gettimeofday ignore-value intprops largefile lstat manywarnings mktime pselect pthread_sigmask readlink socklen stat-time stdalign stdarg stdbool stdio strftime strtoimax strtoumax symlink sys_stat sys_time tim
 e timespec-add timespec-sub utimens warnings
+# Reproduce by: gnulib-tool --import --dir=. --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=errno --avoid=fcntl --avoid=fcntl-h --avoid=fstat --avoid=msvc-inval --avoid=msvc-nothrow --avoid=raise --avoid=select --avoid=sigprocmask --avoid=sys_types --avoid=threadlib --makefile-name=gnulib.mk --conditional-dependencies --no-libtool --macro-prefix=gl --no-vc-files alloca-opt c-ctype c-strcase careadlinkat crypto/md5 crypto/sha1 crypto/sha256 crypto/sha512 dtoastr dtotimespec dup2 environ execinfo filemode getloadavg getopt-gnu gettime gettimeofday ignore-value intprops largefile lstat manywarnings mktime pselect pthread_sigmask readlink socklen stat-time stdalign stdarg stdbool stdio strftime strtoimax strtoumax symlink sys_stat sys
 _time time timespec-add timespec-sub utimens warnings
 
 
 MOSTLYCLEANFILES += core *.stackdump
@@ -150,6 +150,31 @@
 
 ## end   gnulib module dup2
 
+## begin gnulib module execinfo
+
+BUILT_SOURCES += $(EXECINFO_H)
+
+# We need the following in order to create <execinfo.h> when the system
+# doesn't have one that works.
+if GL_GENERATE_EXECINFO_H
+execinfo.h: execinfo.in.h $(top_builddir)/config.status
+	$(AM_V_GEN)rm -f $@-t $@ && \
+	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
+	  cat $(srcdir)/execinfo.in.h; \
+	} > $@-t && \
+	mv $@-t $@
+else
+execinfo.h: $(top_builddir)/config.status
+	rm -f $@
+endif
+MOSTLYCLEANFILES += execinfo.h execinfo.h-t
+
+EXTRA_DIST += execinfo.c execinfo.in.h
+
+EXTRA_libgnu_a_SOURCES += execinfo.c
+
+## end   gnulib module execinfo
+
 ## begin gnulib module filemode
 
 libgnu_a_SOURCES += filemode.c

=== added file 'm4/execinfo.m4'
--- m4/execinfo.m4	1970-01-01 00:00:00 +0000
+++ m4/execinfo.m4	2012-08-22 08:59:10 +0000
@@ -0,0 +1,21 @@
+# Check for GNU-style execinfo.h.
+
+dnl Copyright 2012 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_EXECINFO_H],
+[
+  AC_CHECK_HEADERS_ONCE([execinfo.h])
+
+  if test $ac_cv_header_execinfo_h = yes; then
+    EXECINFO_H=''
+  else
+    EXECINFO_H='execinfo.h'
+    AC_LIBOBJ([execinfo])
+  fi
+
+  AC_SUBST([EXECINFO_H])
+  AM_CONDITIONAL([GL_GENERATE_EXECINFO_H], [test -n "$EXECINFO_H"])
+])

=== modified file 'm4/gnulib-comp.m4'
--- m4/gnulib-comp.m4	2012-08-19 23:31:24 +0000
+++ m4/gnulib-comp.m4	2012-08-22 08:59:10 +0000
@@ -53,6 +53,7 @@
   # Code from module dtotimespec:
   # Code from module dup2:
   # Code from module environ:
+  # Code from module execinfo:
   # Code from module extensions:
   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
   # Code from module extern-inline:
@@ -152,6 +153,7 @@
   gl_UNISTD_MODULE_INDICATOR([dup2])
   gl_ENVIRON
   gl_UNISTD_MODULE_INDICATOR([environ])
+  gl_EXECINFO_H
   AC_REQUIRE([gl_EXTERN_INLINE])
   gl_FILEMODE
   gl_GETLOADAVG
@@ -534,6 +536,8 @@
   lib/dtoastr.c
   lib/dtotimespec.c
   lib/dup2.c
+  lib/execinfo.c
+  lib/execinfo.in.h
   lib/filemode.c
   lib/filemode.h
   lib/ftoastr.c
@@ -605,6 +609,7 @@
   m4/clock_time.m4
   m4/dup2.m4
   m4/environ.m4
+  m4/execinfo.m4
   m4/extensions.m4
   m4/extern-inline.m4
   m4/filemode.m4

=== modified file 'src/ChangeLog'
--- src/ChangeLog	2012-08-22 07:20:42 +0000
+++ src/ChangeLog	2012-08-22 08:59:10 +0000
@@ -1,5 +1,9 @@
 2012-08-22  Paul Eggert  <eggert@cs.ucla.edu>
 
+	On assertion failure, print backtrace if available.
+	* alloc.c [ENABLE_CHECKING]: Include <execinfo.h>.
+	(die) [ENABLE_CHECKING]: Print a backtrace if available.
+
 	* fontset.c (FONTSET_ADD): Return void, not Lisp_Object.
 	Otherwise, the compiler complains about (A?B:C) where B is void
 	and C is Lisp_Object.  This fixes an incompatibility with Sun C	5.12.

=== modified file 'src/alloc.c'
--- src/alloc.c	2012-08-21 23:39:56 +0000
+++ src/alloc.c	2012-08-22 08:59:10 +0000
@@ -6681,13 +6681,21 @@
 }
 
 #ifdef ENABLE_CHECKING
+
+# include <execinfo.h>
+
 bool suppress_checking;
 
 void
 die (const char *msg, const char *file, int line)
 {
+  enum { NPOINTERS_MAX = 500 };
+  void *buffer[NPOINTERS_MAX];
+  int npointers;
   fprintf (stderr, "\r\n%s:%d: Emacs fatal error: %s\r\n",
 	   file, line, msg);
+  npointers = backtrace (buffer, NPOINTERS_MAX);
+  backtrace_symbols_fd (buffer, npointers, STDERR_FILENO);
   abort ();
 }
 #endif




  reply	other threads:[~2012-08-22  9:24 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-17 23:48 inlinable functions instead of macros Stefan Monnier
2012-08-17 23:59 ` Paul Eggert
2012-08-18 22:15   ` Daniel Colascione
2012-08-18 22:55     ` John Yates
2012-08-19  4:22       ` Richard Stallman
2012-08-21 17:50   ` Stefan Monnier
2012-08-22  9:24     ` Paul Eggert [this message]
2012-08-22 16:01       ` C backtraces for Emacs Eli Zaretskii
2012-08-22 16:39         ` Paul Eggert
2012-08-23  0:40         ` Daniel Colascione
2012-08-23 16:55           ` Eli Zaretskii
2012-08-24 10:48       ` Paul Eggert
2012-08-25  4:41         ` Paul Eggert
2012-08-25  5:57           ` Eli Zaretskii
2012-08-22  9:28     ` inlinable functions instead of macros Paul Eggert
2012-08-22  9:48       ` Andreas Schwab
2012-08-22 14:31       ` Stefan Monnier
2012-08-24 21:37         ` Paul Eggert
2012-08-25  1:38           ` Tom Tromey
2012-08-25  2:58             ` Paul Eggert
2012-08-25  5:35               ` Eli Zaretskii
2012-08-25 20:13                 ` Tom Tromey
2012-08-26  4:42                   ` Paul Eggert
2012-08-25  2:05           ` Stefan Monnier
2012-08-26  5:31             ` Paul Eggert
2012-08-22 16:03       ` Eli Zaretskii
2012-08-18 19:08 ` Richard Stallman
2012-08-18 20:57   ` Paul Eggert
2012-08-18 21:03     ` Eli Zaretskii
2012-08-18 21:31       ` Paul Eggert
2012-08-19  2:45         ` Eli Zaretskii
2012-08-18 21:14   ` Óscar Fuentes
2012-08-19 17:28 ` Florian Weimer
2012-08-20 20:38 ` Sam Steingold

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5034A54F.6020006@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=lekktu@gmail.com \
    --cc=monnier@IRO.UMontreal.CA \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).