From unknown Mon Jun 23 18:30:33 2025 X-Loop: help-debbugs@gnu.org Subject: bug#12328: Give more-useful info on a fatal error. Resent-From: Paul Eggert Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 02 Sep 2012 00:29:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 12328 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch To: 12328@debbugs.gnu.org Cc: Juanma Barranquero , Eli Zaretskii X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.134654570421076 (code B ref -1); Sun, 02 Sep 2012 00:29:02 +0000 Received: (at submit) by debbugs.gnu.org; 2 Sep 2012 00:28:24 +0000 Received: from localhost ([127.0.0.1]:33418 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1T7y35-0005Ts-KG for submit@debbugs.gnu.org; Sat, 01 Sep 2012 20:28:24 -0400 Received: from eggs.gnu.org ([208.118.235.92]:49260) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1T7y31-0005Ti-KL for submit@debbugs.gnu.org; Sat, 01 Sep 2012 20:28:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T7y1e-00024Q-0L for submit@debbugs.gnu.org; Sat, 01 Sep 2012 20:26:55 -0400 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.2 Received: from lists.gnu.org ([208.118.235.17]:43077) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T7y1d-00024F-T5 for submit@debbugs.gnu.org; Sat, 01 Sep 2012 20:26:53 -0400 Received: from eggs.gnu.org ([208.118.235.92]:49783) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T7y1c-0005II-6z for bug-gnu-emacs@gnu.org; Sat, 01 Sep 2012 20:26:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T7y1a-00023z-7X for bug-gnu-emacs@gnu.org; Sat, 01 Sep 2012 20:26:52 -0400 Received: from smtp.cs.ucla.edu ([131.179.128.62]:42459) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T7y1Z-00023l-TC; Sat, 01 Sep 2012 20:26:50 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp.cs.ucla.edu (Postfix) with ESMTP id 05AEEA6000F; Sat, 1 Sep 2012 17:26:48 -0700 (PDT) X-Virus-Scanned: amavisd-new at smtp.cs.ucla.edu Received: from smtp.cs.ucla.edu ([127.0.0.1]) by localhost (smtp.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id bE1fL7ExKvse; Sat, 1 Sep 2012 17:26:46 -0700 (PDT) Received: from [192.168.1.3] (pool-108-23-119-2.lsanca.fios.verizon.net [108.23.119.2]) by smtp.cs.ucla.edu (Postfix) with ESMTPSA id 3050139E8007; Sat, 1 Sep 2012 17:26:46 -0700 (PDT) Message-ID: <5042A7C9.800@cs.ucla.edu> Date: Sat, 01 Sep 2012 17:26:49 -0700 From: Paul Eggert Organization: UCLA Computer Science Department User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120827 Thunderbird/15.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 208.118.235.17 X-Spam-Score: -6.2 (------) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: debbugs-submit-bounces@debbugs.gnu.org Errors-To: debbugs-submit-bounces@debbugs.gnu.org X-Spam-Score: -6.2 (------) Tags: patch Here's a patch I'm planning to install into Emacs, to improve the quality of its output after it crashes, by outputting a backtrace and a text string describing the fatal error. This patch assumes the patch I submitted for Bug#12316; see the updated version in . I don't know of any extra implications for Windows due to this patch, but just in case I'm CC'ing this to Eli and Juanma. === modified file 'doc/emacs/ChangeLog' --- doc/emacs/ChangeLog 2012-08-28 16:01:59 +0000 +++ doc/emacs/ChangeLog 2012-09-01 23:55:29 +0000 @@ -1,3 +1,8 @@ +2012-09-01 Paul Eggert + + Give more-useful info on a fatal error. + * trouble.texi (Crashing): New section, documenting this. + 2012-08-24 Michael Albinus * cmdargs.texi (General Variables): Setting === modified file 'doc/emacs/emacs.texi' --- doc/emacs/emacs.texi 2012-08-07 03:31:53 +0000 +++ doc/emacs/emacs.texi 2012-09-01 23:55:29 +0000 @@ -1136,6 +1136,7 @@ * Screen Garbled:: Garbage on the screen. * Text Garbled:: Garbage in the text. * Memory Full:: How to cope when you run out of memory. +* Crashing:: What Emacs does when it crashes. * After a Crash:: Recovering editing in an Emacs session that crashed. * Emergency Escape:: What to do if Emacs stops responding. === modified file 'doc/emacs/trouble.texi' --- doc/emacs/trouble.texi 2012-05-27 01:25:06 +0000 +++ doc/emacs/trouble.texi 2012-09-01 23:55:29 +0000 @@ -149,6 +149,7 @@ * Screen Garbled:: Garbage on the screen. * Text Garbled:: Garbage in the text. * Memory Full:: How to cope when you run out of memory. +* Crashing:: What Emacs does when it crashes. * After a Crash:: Recovering editing in an Emacs session that crashed. * Emergency Escape:: What to do if Emacs stops responding. @end menu @@ -277,6 +278,44 @@ out of memory, because the buffer menu needs a fair amount of memory itself, and the reserve supply may not be enough. +@node Crashing +@subsection When Emacs Crashes + + Emacs is not supposed to crash, but if it does, before it exits it +reports some information about the crash to the standard error stream +@code{stderr}. This report may be useful to someone who later debugs +the same version of Emacs on the same platform. The format of this +report depends on the platform, and some platforms support backtraces. +Here is an example, generated on x86-64 GNU/Linux with version 2.15 of +the GNU C Library: + +@example +Fatal error 11: Segmentation fault +Backtrace: +emacs[0x5094e4] +emacs[0x4ed3e6] +emacs[0x4ed504] +/lib64/libpthread.so.0[0x375220efe0] +/lib64/libpthread.so.0(read+0xe)[0x375220e08e] +emacs[0x509af6] +emacs[0x5acc26] +emacs[0x5adbfb] +emacs[0x56566b] +emacs[0x59bac3] +emacs[0x565151] +... +@end example + +@noindent +The number @samp{11} is the system signal number that corresponds to +the problem, a segmentation fault here. The hexadecimal program +addresses can be useful in debugging sessions. For example, the GDB +command @samp{list *0x509af6} prints the source-code lines +corresponding to the @samp{emacs[0x509af6]} entry in the backtrace. + +The three dots at the end indicate that Emacs suppressed further +backtrace entries, in the interest of brevity. + @node After a Crash @subsection Recovery After a Crash === modified file 'etc/ChangeLog' --- etc/ChangeLog 2012-09-01 01:04:26 +0000 +++ etc/ChangeLog 2012-09-01 23:55:29 +0000 @@ -1,5 +1,8 @@ 2012-09-01 Paul Eggert + Give more-useful info on a fatal error. + * NEWS: Document the change. + Better seeds for (random). * NEWS: Document new behavior of (random), (random "string"). === modified file 'etc/NEWS' --- etc/NEWS 2012-09-01 01:04:26 +0000 +++ etc/NEWS 2012-09-01 23:55:29 +0000 @@ -97,6 +97,11 @@ file-attributes and format-time-string, have been changed accordingly. Old-format time stamps are still accepted. +** Emacs now generates backtraces on fatal errors. +On encountering a fatal error, Emacs now outputs a textual description +of the fatal signal, and a short backtrace on platforms like glibc +that support backtraces. + ** New functions `system-users', `system-groups' return lists of the user name, group names known to the system (where possible). === modified file 'src/ChangeLog' --- src/ChangeLog 2012-09-01 22:55:39 +0000 +++ src/ChangeLog 2012-09-01 23:55:29 +0000 @@ -1,5 +1,22 @@ 2012-09-01 Paul Eggert + Give more-useful info on a fatal error. + * alloc.c [ENABLE_CHECKING]: Do not include . + (die) [ENABLE_CHECKING]: Call fatal_error_backtrace instead + of doing the work ourselves. + * emacs.c (fatal_error_signal): Let fatal_error_backtrace + do most of the work. + (fatal_error_backtrace): New function, taken from the guts + of the old fatal_error_signal, but with a new option to output + a backtrace. + (shut_down_emacs) [!DOS_NT]: Use strsignal to give more-useful + info about the signal than just its number. + * lisp.h (fatal_error_backtrace, emacs_backtrace): New decls. + * sysdep.c: Include + (emacs_backtrace): New function, taken partly from the previous + code of the 'die' function. + (emacs_abort): Call fatal_error_backtrace rather than abort. + Simplify redefinition of 'abort' (Bug#12316). Do not try to redefine the 'abort' function. Instead, redo the code so that it calls 'emacs_abort' rather than 'abort'. === modified file 'src/alloc.c' --- src/alloc.c 2012-08-31 16:21:56 +0000 +++ src/alloc.c 2012-09-01 13:44:44 +0000 @@ -6685,21 +6685,14 @@ #ifdef ENABLE_CHECKING -# include - 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); - emacs_abort (); + fatal_error_backtrace (SIGABRT, INT_MAX); } #endif === modified file 'src/emacs.c' --- src/emacs.c 2012-09-01 13:39:22 +0000 +++ src/emacs.c 2012-09-01 13:44:44 +0000 @@ -298,6 +298,14 @@ fatal_error_signal (int sig) { SIGNAL_THREAD_CHECK (sig); + fatal_error_backtrace (sig, 10); +} + +/* Report a fatal error due to signal SIG, output a backtrace of at + most BACKTRACE_LIMIT lines, and exit. */ +_Noreturn void +fatal_error_backtrace (int sig, int backtrace_limit) +{ fatal_error_code = sig; signal (sig, SIG_DFL); @@ -312,6 +320,7 @@ Fkill_emacs (make_number (sig)); shut_down_emacs (sig, 0, Qnil); + emacs_backtrace (backtrace_limit); } /* Signal the same code; this time it will really be fatal. @@ -323,6 +332,9 @@ #endif kill (getpid (), fatal_error_code); + + /* This shouldn't be executed, but it prevents a warning. */ + exit (1); } #ifdef SIGDANGER @@ -1992,7 +2004,7 @@ { reset_all_sys_modes (); if (sig && sig != SIGTERM) - fprintf (stderr, "Fatal error (%d)", sig); + fprintf (stderr, "Fatal error %d: %s", sig, strsignal (sig)); } } #else === modified file 'src/lisp.h' --- src/lisp.h 2012-09-01 01:32:53 +0000 +++ src/lisp.h 2012-09-01 13:44:44 +0000 @@ -3265,6 +3265,7 @@ #ifdef FLOAT_CATCH_SIGILL extern void fatal_error_signal (int); #endif +extern _Noreturn void fatal_error_backtrace (int, int); extern Lisp_Object Qkill_emacs; #if HAVE_SETLOCALE void fixup_locale (void); @@ -3407,6 +3408,7 @@ extern EMACS_INT get_random (void); extern void seed_random (void *, ptrdiff_t); extern void init_random (void); +extern void emacs_backtrace (int); extern _Noreturn void emacs_abort (void) NO_INLINE; extern int emacs_open (const char *, int, int); extern int emacs_close (int); === modified file 'src/sysdep.c' --- src/sysdep.c 2012-09-01 01:32:53 +0000 +++ src/sysdep.c 2012-09-01 23:55:29 +0000 @@ -21,6 +21,7 @@ #define SYSTIME_INLINE EXTERN_INLINE +#include #include #include #include @@ -1856,12 +1857,30 @@ } #endif +/* If a backtrace is available, output the top lines of it to stderr. + Do not output more than BACKTRACE_LIMIT or BACKTRACE_LIMIT_MAX lines. + This function may be called from a signal handler, so it should + not invoke async-unsafe functions like malloc. */ +void +emacs_backtrace (int backtrace_limit) +{ + enum { BACKTRACE_LIMIT_MAX = 500 }; + void *buffer[BACKTRACE_LIMIT_MAX + 1]; + int bounded_limit = min (backtrace_limit, BACKTRACE_LIMIT_MAX); + int npointers = backtrace (buffer, bounded_limit + 1); + if (npointers) + ignore_value (write (STDERR_FILENO, "\nBacktrace:\n", 12)); + backtrace_symbols_fd (buffer, bounded_limit, STDERR_FILENO); + if (bounded_limit < npointers) + ignore_value (write (STDERR_FILENO, "...\n", 4)); +} + #ifndef HAVE_NTGUI /* Using emacs_abort lets GDB return from a breakpoint here. */ void emacs_abort (void) { - (abort) (); + fatal_error_backtrace (SIGABRT, 10); } #endif From unknown Mon Jun 23 18:30:33 2025 MIME-Version: 1.0 X-Mailer: MIME-tools 5.428 (Entity 5.428) X-Loop: help-debbugs@gnu.org From: help-debbugs@gnu.org (GNU bug Tracking System) To: Paul Eggert Subject: bug#12328: closed (Re: Give more-useful info on a fatal error.) Message-ID: References: <504648CE.6080602@cs.ucla.edu> <5042A7C9.800@cs.ucla.edu> X-Gnu-PR-Message: they-closed 12328 X-Gnu-PR-Package: emacs X-Gnu-PR-Keywords: patch Reply-To: 12328@debbugs.gnu.org Date: Tue, 04 Sep 2012 18:31:02 +0000 Content-Type: multipart/mixed; boundary="----------=_1346783462-21971-1" This is a multi-part message in MIME format... ------------=_1346783462-21971-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Your bug report #12328: Give more-useful info on a fatal error. which was filed against the emacs package, has been closed. The explanation is attached below, along with your original report. If you require more details, please reply to 12328@debbugs.gnu.org. --=20 12328: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=3D12328 GNU Bug Tracking System Contact help-debbugs@gnu.org with problems ------------=_1346783462-21971-1 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit Received: (at 12328-done) by debbugs.gnu.org; 4 Sep 2012 18:30:48 +0000 Received: from localhost ([127.0.0.1]:38800 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1T8xtf-0005hx-NR for submit@debbugs.gnu.org; Tue, 04 Sep 2012 14:30:48 -0400 Received: from smtp.cs.ucla.edu ([131.179.128.62]:59934) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1T8xtY-0005hl-Ek for 12328-done@debbugs.gnu.org; Tue, 04 Sep 2012 14:30:45 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp.cs.ucla.edu (Postfix) with ESMTP id ACB5A39E8018; Tue, 4 Sep 2012 11:30:37 -0700 (PDT) X-Virus-Scanned: amavisd-new at smtp.cs.ucla.edu Received: from smtp.cs.ucla.edu ([127.0.0.1]) by localhost (smtp.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id OnYGDA0u4LYt; Tue, 4 Sep 2012 11:30:37 -0700 (PDT) Received: from [192.168.1.3] (pool-108-23-119-2.lsanca.fios.verizon.net [108.23.119.2]) by smtp.cs.ucla.edu (Postfix) with ESMTPSA id 5D44439E8008; Tue, 4 Sep 2012 11:30:37 -0700 (PDT) Message-ID: <504648CE.6080602@cs.ucla.edu> Date: Tue, 04 Sep 2012 11:30:38 -0700 From: Paul Eggert Organization: UCLA Computer Science Department User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120827 Thunderbird/15.0 MIME-Version: 1.0 To: 12328-done@debbugs.gnu.org Subject: Re: Give more-useful info on a fatal error. References: <5042A7C9.800@cs.ucla.edu> In-Reply-To: <5042A7C9.800@cs.ucla.edu> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Score: -2.1 (--) X-Debbugs-Envelope-To: 12328-done Cc: Juanma Barranquero , Eli Zaretskii X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: debbugs-submit-bounces@debbugs.gnu.org Errors-To: debbugs-submit-bounces@debbugs.gnu.org X-Spam-Score: -2.1 (--) I installed this patch into the trunk as bzr 109879 and am marking it as done. ------------=_1346783462-21971-1 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit Received: (at submit) by debbugs.gnu.org; 2 Sep 2012 00:28:24 +0000 Received: from localhost ([127.0.0.1]:33418 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1T7y35-0005Ts-KG for submit@debbugs.gnu.org; Sat, 01 Sep 2012 20:28:24 -0400 Received: from eggs.gnu.org ([208.118.235.92]:49260) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1T7y31-0005Ti-KL for submit@debbugs.gnu.org; Sat, 01 Sep 2012 20:28:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T7y1e-00024Q-0L for submit@debbugs.gnu.org; Sat, 01 Sep 2012 20:26:55 -0400 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.2 Received: from lists.gnu.org ([208.118.235.17]:43077) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T7y1d-00024F-T5 for submit@debbugs.gnu.org; Sat, 01 Sep 2012 20:26:53 -0400 Received: from eggs.gnu.org ([208.118.235.92]:49783) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T7y1c-0005II-6z for bug-gnu-emacs@gnu.org; Sat, 01 Sep 2012 20:26:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T7y1a-00023z-7X for bug-gnu-emacs@gnu.org; Sat, 01 Sep 2012 20:26:52 -0400 Received: from smtp.cs.ucla.edu ([131.179.128.62]:42459) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T7y1Z-00023l-TC; Sat, 01 Sep 2012 20:26:50 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp.cs.ucla.edu (Postfix) with ESMTP id 05AEEA6000F; Sat, 1 Sep 2012 17:26:48 -0700 (PDT) X-Virus-Scanned: amavisd-new at smtp.cs.ucla.edu Received: from smtp.cs.ucla.edu ([127.0.0.1]) by localhost (smtp.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id bE1fL7ExKvse; Sat, 1 Sep 2012 17:26:46 -0700 (PDT) Received: from [192.168.1.3] (pool-108-23-119-2.lsanca.fios.verizon.net [108.23.119.2]) by smtp.cs.ucla.edu (Postfix) with ESMTPSA id 3050139E8007; Sat, 1 Sep 2012 17:26:46 -0700 (PDT) Message-ID: <5042A7C9.800@cs.ucla.edu> Date: Sat, 01 Sep 2012 17:26:49 -0700 From: Paul Eggert Organization: UCLA Computer Science Department User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/20120827 Thunderbird/15.0 MIME-Version: 1.0 To: bug-gnu-emacs@gnu.org Subject: Give more-useful info on a fatal error. Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 208.118.235.17 X-Spam-Score: -6.2 (------) X-Debbugs-Envelope-To: submit Cc: Juanma Barranquero , Eli Zaretskii X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: debbugs-submit-bounces@debbugs.gnu.org Errors-To: debbugs-submit-bounces@debbugs.gnu.org X-Spam-Score: -6.2 (------) Tags: patch Here's a patch I'm planning to install into Emacs, to improve the quality of its output after it crashes, by outputting a backtrace and a text string describing the fatal error. This patch assumes the patch I submitted for Bug#12316; see the updated version in . I don't know of any extra implications for Windows due to this patch, but just in case I'm CC'ing this to Eli and Juanma. === modified file 'doc/emacs/ChangeLog' --- doc/emacs/ChangeLog 2012-08-28 16:01:59 +0000 +++ doc/emacs/ChangeLog 2012-09-01 23:55:29 +0000 @@ -1,3 +1,8 @@ +2012-09-01 Paul Eggert + + Give more-useful info on a fatal error. + * trouble.texi (Crashing): New section, documenting this. + 2012-08-24 Michael Albinus * cmdargs.texi (General Variables): Setting === modified file 'doc/emacs/emacs.texi' --- doc/emacs/emacs.texi 2012-08-07 03:31:53 +0000 +++ doc/emacs/emacs.texi 2012-09-01 23:55:29 +0000 @@ -1136,6 +1136,7 @@ * Screen Garbled:: Garbage on the screen. * Text Garbled:: Garbage in the text. * Memory Full:: How to cope when you run out of memory. +* Crashing:: What Emacs does when it crashes. * After a Crash:: Recovering editing in an Emacs session that crashed. * Emergency Escape:: What to do if Emacs stops responding. === modified file 'doc/emacs/trouble.texi' --- doc/emacs/trouble.texi 2012-05-27 01:25:06 +0000 +++ doc/emacs/trouble.texi 2012-09-01 23:55:29 +0000 @@ -149,6 +149,7 @@ * Screen Garbled:: Garbage on the screen. * Text Garbled:: Garbage in the text. * Memory Full:: How to cope when you run out of memory. +* Crashing:: What Emacs does when it crashes. * After a Crash:: Recovering editing in an Emacs session that crashed. * Emergency Escape:: What to do if Emacs stops responding. @end menu @@ -277,6 +278,44 @@ out of memory, because the buffer menu needs a fair amount of memory itself, and the reserve supply may not be enough. +@node Crashing +@subsection When Emacs Crashes + + Emacs is not supposed to crash, but if it does, before it exits it +reports some information about the crash to the standard error stream +@code{stderr}. This report may be useful to someone who later debugs +the same version of Emacs on the same platform. The format of this +report depends on the platform, and some platforms support backtraces. +Here is an example, generated on x86-64 GNU/Linux with version 2.15 of +the GNU C Library: + +@example +Fatal error 11: Segmentation fault +Backtrace: +emacs[0x5094e4] +emacs[0x4ed3e6] +emacs[0x4ed504] +/lib64/libpthread.so.0[0x375220efe0] +/lib64/libpthread.so.0(read+0xe)[0x375220e08e] +emacs[0x509af6] +emacs[0x5acc26] +emacs[0x5adbfb] +emacs[0x56566b] +emacs[0x59bac3] +emacs[0x565151] +... +@end example + +@noindent +The number @samp{11} is the system signal number that corresponds to +the problem, a segmentation fault here. The hexadecimal program +addresses can be useful in debugging sessions. For example, the GDB +command @samp{list *0x509af6} prints the source-code lines +corresponding to the @samp{emacs[0x509af6]} entry in the backtrace. + +The three dots at the end indicate that Emacs suppressed further +backtrace entries, in the interest of brevity. + @node After a Crash @subsection Recovery After a Crash === modified file 'etc/ChangeLog' --- etc/ChangeLog 2012-09-01 01:04:26 +0000 +++ etc/ChangeLog 2012-09-01 23:55:29 +0000 @@ -1,5 +1,8 @@ 2012-09-01 Paul Eggert + Give more-useful info on a fatal error. + * NEWS: Document the change. + Better seeds for (random). * NEWS: Document new behavior of (random), (random "string"). === modified file 'etc/NEWS' --- etc/NEWS 2012-09-01 01:04:26 +0000 +++ etc/NEWS 2012-09-01 23:55:29 +0000 @@ -97,6 +97,11 @@ file-attributes and format-time-string, have been changed accordingly. Old-format time stamps are still accepted. +** Emacs now generates backtraces on fatal errors. +On encountering a fatal error, Emacs now outputs a textual description +of the fatal signal, and a short backtrace on platforms like glibc +that support backtraces. + ** New functions `system-users', `system-groups' return lists of the user name, group names known to the system (where possible). === modified file 'src/ChangeLog' --- src/ChangeLog 2012-09-01 22:55:39 +0000 +++ src/ChangeLog 2012-09-01 23:55:29 +0000 @@ -1,5 +1,22 @@ 2012-09-01 Paul Eggert + Give more-useful info on a fatal error. + * alloc.c [ENABLE_CHECKING]: Do not include . + (die) [ENABLE_CHECKING]: Call fatal_error_backtrace instead + of doing the work ourselves. + * emacs.c (fatal_error_signal): Let fatal_error_backtrace + do most of the work. + (fatal_error_backtrace): New function, taken from the guts + of the old fatal_error_signal, but with a new option to output + a backtrace. + (shut_down_emacs) [!DOS_NT]: Use strsignal to give more-useful + info about the signal than just its number. + * lisp.h (fatal_error_backtrace, emacs_backtrace): New decls. + * sysdep.c: Include + (emacs_backtrace): New function, taken partly from the previous + code of the 'die' function. + (emacs_abort): Call fatal_error_backtrace rather than abort. + Simplify redefinition of 'abort' (Bug#12316). Do not try to redefine the 'abort' function. Instead, redo the code so that it calls 'emacs_abort' rather than 'abort'. === modified file 'src/alloc.c' --- src/alloc.c 2012-08-31 16:21:56 +0000 +++ src/alloc.c 2012-09-01 13:44:44 +0000 @@ -6685,21 +6685,14 @@ #ifdef ENABLE_CHECKING -# include - 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); - emacs_abort (); + fatal_error_backtrace (SIGABRT, INT_MAX); } #endif === modified file 'src/emacs.c' --- src/emacs.c 2012-09-01 13:39:22 +0000 +++ src/emacs.c 2012-09-01 13:44:44 +0000 @@ -298,6 +298,14 @@ fatal_error_signal (int sig) { SIGNAL_THREAD_CHECK (sig); + fatal_error_backtrace (sig, 10); +} + +/* Report a fatal error due to signal SIG, output a backtrace of at + most BACKTRACE_LIMIT lines, and exit. */ +_Noreturn void +fatal_error_backtrace (int sig, int backtrace_limit) +{ fatal_error_code = sig; signal (sig, SIG_DFL); @@ -312,6 +320,7 @@ Fkill_emacs (make_number (sig)); shut_down_emacs (sig, 0, Qnil); + emacs_backtrace (backtrace_limit); } /* Signal the same code; this time it will really be fatal. @@ -323,6 +332,9 @@ #endif kill (getpid (), fatal_error_code); + + /* This shouldn't be executed, but it prevents a warning. */ + exit (1); } #ifdef SIGDANGER @@ -1992,7 +2004,7 @@ { reset_all_sys_modes (); if (sig && sig != SIGTERM) - fprintf (stderr, "Fatal error (%d)", sig); + fprintf (stderr, "Fatal error %d: %s", sig, strsignal (sig)); } } #else === modified file 'src/lisp.h' --- src/lisp.h 2012-09-01 01:32:53 +0000 +++ src/lisp.h 2012-09-01 13:44:44 +0000 @@ -3265,6 +3265,7 @@ #ifdef FLOAT_CATCH_SIGILL extern void fatal_error_signal (int); #endif +extern _Noreturn void fatal_error_backtrace (int, int); extern Lisp_Object Qkill_emacs; #if HAVE_SETLOCALE void fixup_locale (void); @@ -3407,6 +3408,7 @@ extern EMACS_INT get_random (void); extern void seed_random (void *, ptrdiff_t); extern void init_random (void); +extern void emacs_backtrace (int); extern _Noreturn void emacs_abort (void) NO_INLINE; extern int emacs_open (const char *, int, int); extern int emacs_close (int); === modified file 'src/sysdep.c' --- src/sysdep.c 2012-09-01 01:32:53 +0000 +++ src/sysdep.c 2012-09-01 23:55:29 +0000 @@ -21,6 +21,7 @@ #define SYSTIME_INLINE EXTERN_INLINE +#include #include #include #include @@ -1856,12 +1857,30 @@ } #endif +/* If a backtrace is available, output the top lines of it to stderr. + Do not output more than BACKTRACE_LIMIT or BACKTRACE_LIMIT_MAX lines. + This function may be called from a signal handler, so it should + not invoke async-unsafe functions like malloc. */ +void +emacs_backtrace (int backtrace_limit) +{ + enum { BACKTRACE_LIMIT_MAX = 500 }; + void *buffer[BACKTRACE_LIMIT_MAX + 1]; + int bounded_limit = min (backtrace_limit, BACKTRACE_LIMIT_MAX); + int npointers = backtrace (buffer, bounded_limit + 1); + if (npointers) + ignore_value (write (STDERR_FILENO, "\nBacktrace:\n", 12)); + backtrace_symbols_fd (buffer, bounded_limit, STDERR_FILENO); + if (bounded_limit < npointers) + ignore_value (write (STDERR_FILENO, "...\n", 4)); +} + #ifndef HAVE_NTGUI /* Using emacs_abort lets GDB return from a breakpoint here. */ void emacs_abort (void) { - (abort) (); + fatal_error_backtrace (SIGABRT, 10); } #endif ------------=_1346783462-21971-1--