aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggert2019-04-21 23:15:42 -0700
committerPaul Eggert2019-04-21 23:16:48 -0700
commitb20d8a932240d5332b4f44b95b2bc1d6fc74c7b0 (patch)
treec4966bf6b1443f3bf82680a9ab27d1b87f5d9871
parent1ea048f6e02e56891278a040a5b21678c661764a (diff)
downloademacs-b20d8a932240d5332b4f44b95b2bc1d6fc74c7b0.tar.gz
emacs-b20d8a932240d5332b4f44b95b2bc1d6fc74c7b0.zip
Mention AddressSanitizer etc. in etc/DEBUG
* etc/DEBUG: Modernize for AddressSanitizer etc. * etc/NEWS: Defer to etc/DEBUG for this.
-rw-r--r--etc/DEBUG126
-rw-r--r--etc/NEWS3
2 files changed, 82 insertions, 47 deletions
diff --git a/etc/DEBUG b/etc/DEBUG
index d401d0be901..717553871a7 100644
--- a/etc/DEBUG
+++ b/etc/DEBUG
@@ -12,24 +12,21 @@ debugging techniques.
12*** Configuring Emacs for debugging 12*** Configuring Emacs for debugging
13 13
14It is best to configure and build Emacs with special options that will 14It is best to configure and build Emacs with special options that will
15make the debugging easier. Here's the configure-time options we 15make the debugging easier. Here are the configure-time options we
16recommend (they are in addition to any other options you might need, 16recommend (they are in addition to any other options you might need,
17such as --prefix): 17such as --prefix):
18 18
19 ./configure --enable-checking='yes,glyphs' --enable-check-lisp-object-type \ 19 ./configure --enable-checking='yes,glyphs' --enable-check-lisp-object-type \
20 CFLAGS='-O0 -g3' 20 CFLAGS='-O0 -g3'
21 21
22The CFLAGS value is important: debugging optimized code can be very 22The -O0 flag is important, as debugging optimized code can be hard.
23hard. (If the problem only happens with optimized code, you may need 23If the problem happens only with optimized code, you may need to
24to enable optimizations. If that happens, try using -Og first, 24enable optimizations. If that happens, try using -Og first instead of
25instead of -O2, as the former will disable some optimizations that 25-O2, as -Og disables some optimizations that make debugging some code
26make debugging some code exceptionally hard.) 26exceptionally hard.
27 27
28Modern versions of GCC support more elaborate debug info that is 28Older versions of GCC may need more than just the -g3 flag. For more,
29available by just using the -g3 compiler switch. Try using -gdwarf-4 29search for "analyze failed assertions" below.
30in addition to -g3, and if that fails, try -gdwarf-3. This is
31especially important if you have to debug optimized code. More info
32about this is available below; search for "analyze failed assertions".
33 30
34The 2 --enable-* switches are optional. They don't have any effect on 31The 2 --enable-* switches are optional. They don't have any effect on
35debugging with GDB, but will compile additional code that might catch 32debugging with GDB, but will compile additional code that might catch
@@ -184,20 +181,15 @@ Good luck!
184 181
185** When you are trying to analyze failed assertions or backtraces, it 182** When you are trying to analyze failed assertions or backtraces, it
186is essential to compile Emacs with flags suitable for debugging. 183is essential to compile Emacs with flags suitable for debugging.
187With GCC 4.8 or later, you can invoke 'make' with CFLAGS="-Og -g3". 184With GCC 4.8 or later, you can invoke 'make' with CFLAGS="-O0 -g3".
188With older GCC or non-GCC compilers, you can use CFLAGS="-O0 -g3". 185With older GCC, you can use CFLAGS="-O0 -g3 -gdwarf-4", replacing "4"
186by the highest version of DWARF that your compiler supports;
187with non-GCC compilers, "-O0 -g3" may be the best you can do.
189With GCC and higher optimization levels such as -O2, the 188With GCC and higher optimization levels such as -O2, the
190-fno-omit-frame-pointer and -fno-crossjumping options are often 189-fno-omit-frame-pointer and -fno-crossjumping options are often
191essential. The latter prevents GCC from using the same abort call for 190essential. The latter prevents GCC from using the same abort call for
192all assertions in a given function, rendering the stack backtrace 191all assertions in a given function, rendering the stack backtrace
193useless for identifying the specific failed assertion. 192useless for identifying the specific failed assertion.
194Some versions of GCC support recent versions of the DWARF standard for
195debugging info, but default to older versions; for example, they could
196support -gdwarf-4 compiler option (for DWARF v4), but default to
197version 2 of the DWARF standard. For best results in debugging
198abilities, find out the highest version of DWARF your GCC can support,
199and use the corresponding -gdwarf-N switch instead of just -g (you
200will still need -g3, as in "-gdwarf-4 -g3").
201 193
202** It is a good idea to run Emacs under GDB (or some other suitable 194** It is a good idea to run Emacs under GDB (or some other suitable
203debugger) *all the time*. Then, when Emacs crashes, you will be able 195debugger) *all the time*. Then, when Emacs crashes, you will be able
@@ -923,41 +915,83 @@ setting the new-console option before running Emacs under GDB:
923 (gdb) set new-console 1 915 (gdb) set new-console 1
924 (gdb) run 916 (gdb) run
925 917
926** Running Emacs built with malloc debugging packages 918** Running Emacs with undefined-behavior sanitization
927 919
928If Emacs exhibits bugs that seem to be related to use of memory 920Building Emacs with undefined-behavior sanitization can help debug
929allocated off the heap, it might be useful to link Emacs with a 921integer overflow and other undefined behavior in C code. To use
930special debugging library, such as Electric Fence (a.k.a. efence) or 922UndefinedBehaviorSanitizer with GCC and similar compilers, append
931GNU Checker, which helps find such problems. 923'-fsanitize=undefined' to CFLAGS, either when running 'configure' or
924running 'make'. For example:
932 925
933Emacs compiled with such packages might not run without some hacking, 926 ./configure CFLAGS='-O0 -g3 -fsanitize=undefined'
934because Emacs replaces the system's memory allocation functions with
935its own versions, and because the dumping process might be
936incompatible with the way these packages use to track allocated
937memory. Here are some of the changes you might find necessary:
938 927
939 - Make sure unexec is disabled, e.g., './configure --without-unexec'. 928You may need to append '-static-libubsan' to CFLAGS if your version of
929GCC is installed in an unusual location.
940 930
941 - Configure with a different --prefix= option. If you use GCC, 931When using GDB to debug an executable with undefined-behavior
942 version 2.7.2 is preferred, as some malloc debugging packages 932sanitization, the GDB command:
943 work a lot better with it than with 2.95 or later versions.
944 933
945 - Type "make" then "make -k install". 934 (gdb) rbreak ^__ubsan_handle_
946 935
947 - If required, invoke the package-specific command to prepare 936will let you gain control when an error is detected and before
948 src/temacs for execution. 937UndefinedBehaviorSanitizer outputs to stderr or terminates the
938program.
949 939
950 - cd ..; src/temacs 940** Running Emacs with address sanitization
951 941
952(Note that this runs 'temacs' instead of the usual 'emacs' executable. 942Building Emacs with address sanitization can help debug memory-use
953This avoids problems with dumping Emacs mentioned above.) 943problems. To use AddressSanitizer with GCC and similar compilers,
944append '-fsanitize=address' to CFLAGS, either when running 'configure'
945or running 'make'. Configure, build and run Emacs with
946ASAN_OPTIONS='detect_leaks=0' in the environment to suppress
947diagnostics of minor memory leaks in Emacs. For example:
954 948
955Some malloc debugging libraries might print lots of false alarms for 949 export ASAN_OPTIONS='detect_leaks=0'
956bitfields used by Emacs in some data structures. If you want to get 950 ./configure CFLAGS='-O0 -g3 -fsanitize=address'
957rid of the false alarms, you will have to hack the definitions of 951 make
958these data structures on the respective headers to remove the ':N' 952 src/emacs
959bitfield definitions (which will cause each such field to use a full 953
960int). 954You may need to append '-static-libasan' to CFLAGS if your version of
955GCC is installed in an unusual location.
956
957When using GDB to debug an executable with address sanitization, the
958GDB command:
959
960 (gdb) rbreak ^__asan_report_
961
962will let you gain control when an error is detected and before
963AddressSanitizer outputs to stderr or terminates the program.
964
965Address sanitization is incompatible with undefined-behavior
966sanitization, unfortunately. Address sanitization is also
967incompatible with the --with-dumping=unexec option of 'configure'.
968
969** Running Emacs under Valgrind
970
971Valgrind <http://valgrind.org/> is free software that can be useful
972when debugging low-level Emacs problems. Unlike GCC sanitizers,
973Valgrind does not need you to compile Emacs with special debugging
974flags, so it can be helpful in investigating problems that vanish when
975Emacs is recompiled with debugging enabled. However, by default
976Valgrind generates many false alarms with Emacs, and you will need to
977maintain a suppressions file to suppress these false alarms and use
978Valgrind effectively. For example, you might invoke Valgrind this
979way:
980
981 valgrind --suppressions=valgrind.supp ./emacs
982
983where valgrind.supp contains groups of lines like the following, which
984suppresses some Valgrind false alarms during Emacs garbage collection:
985
986 {
987 Fgarbage_collect Cond - conservative garbage collection
988 Memcheck:Cond
989 ...
990 fun:Fgarbage_collect
991 }
992
993Unfortunately Valgrind suppression files tend to be system-dependent,
994so you will need to keep one around that matches your system.
961 995
962** How to recover buffer contents from an Emacs core dump file 996** How to recover buffer contents from an Emacs core dump file
963 997
diff --git a/etc/NEWS b/etc/NEWS
index 051063171e0..b444fc59677 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -93,7 +93,8 @@ change to one of the data structures that it relies on.
93** The configure options '--enable-checking=conslist' and 93** The configure options '--enable-checking=conslist' and
94'--enable-checking=xmallocoverrun' have been withdrawn. The former 94'--enable-checking=xmallocoverrun' have been withdrawn. The former
95made Emacs irredeemably slow, and the latter made it crash. Neither 95made Emacs irredeemably slow, and the latter made it crash. Neither
96option was useful with modern debugging tools. 96option was useful with modern debugging tools such as AddressSanitizer
97(see etc/DEBUG).
97 98
98--- 99---
99** Emacs now requires GTK 2.24 and GTK 3.10 for the GTK 2 and GTK 3 100** Emacs now requires GTK 2.24 and GTK 3.10 for the GTK 2 and GTK 3