From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: phillip.lord@russet.org.uk (Phillip Lord) Newsgroups: gmane.emacs.devel Subject: Debugging Emacs Date: Fri, 27 Nov 2015 17:23:11 +0000 Message-ID: <87r3jbicg0.fsf@russet.org.uk> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1448645021 4970 80.91.229.3 (27 Nov 2015 17:23:41 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 27 Nov 2015 17:23:41 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Nov 27 18:23:34 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1a2MkA-0005XP-6Z for ged-emacs-devel@m.gmane.org; Fri, 27 Nov 2015 18:23:34 +0100 Original-Received: from localhost ([::1]:57832 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a2MkC-0001bh-Tm for ged-emacs-devel@m.gmane.org; Fri, 27 Nov 2015 12:23:36 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:42099) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a2Mjw-0001a3-CP for emacs-devel@gnu.org; Fri, 27 Nov 2015 12:23:21 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a2Mjs-0007p5-BT for emacs-devel@gnu.org; Fri, 27 Nov 2015 12:23:20 -0500 Original-Received: from cheviot22.ncl.ac.uk ([128.240.234.22]:53910) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a2Mjs-0007os-2o for emacs-devel@gnu.org; Fri, 27 Nov 2015 12:23:16 -0500 Original-Received: from smtpauth-vm.ncl.ac.uk ([10.8.233.129] helo=smtpauth.ncl.ac.uk) by cheviot22.ncl.ac.uk with esmtp (Exim 4.63) (envelope-from ) id 1a2Mjn-0005N8-El for emacs-devel@gnu.org; Fri, 27 Nov 2015 17:23:11 +0000 Original-Received: from jangai.ncl.ac.uk ([10.66.67.223] helo=localhost) by smtpauth.ncl.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.63) (envelope-from ) id 1a2Mjn-0004bL-GF for emacs-devel@gnu.org; Fri, 27 Nov 2015 17:23:11 +0000 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 128.240.234.22 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:195391 Archived-At: I debugged Emacs core the other day for the first time. As I have no experience with C, GDB or any of the Emacs tooling, I found this an uphill struggle, so I have written up a short "how-to-debug" Emacs file. It starts from a slightly earlier place than the current DEBUG file, and is a step by step guide. The existing DEBUG file, I found pretty dense. I was wondering if it is worth including first? Also, if my approach to debugging is a good one, in case I am doing something daft. Comments welcome. ** How to Debug Emacs C core The following is a brief introduction on how to debug the C core of Emacs. It assumes reasonable programming knowledge, but not particular knowledge of the tooling, other than an understanding of how to build Emacs. This is not a normative guide -- there are many ways to use these tools to debug Emacs. It's a "hello world" guide. It describes one way that should work. *** Preparing Emacs to be debugged To debug Emacs, you need to build it from the tarball source file. First, Emacs should be compiled with the correct flags to ensure that it can be debugged cleanly. This is achieve with the configure script which should be launched with the following command line. ./configure --enable-checking --enable-check-lisp-object-type --config-cache CFLAGS="-Og -g3" Strictly, only the CFLAGS part is necessary for debugging. The two "--enable" flags will pick up some errors for you, and the --config-cache makes configure run more quickly second time around (useful if you are switching between branches in git). Then to build type. make You do not need to install Emacs. It runs fine from a source build. *** Preparing GDB We will debug with GDB. Emacs comes with a nice set of configuration files for working with GDB, with many useful functions. To use this, it needs to be loaded first. Unfortunately, by default GDB disallows this, as it's a security risk. So, you need to add the following line to your .gdbinit file. add-auto-load-safe-path path/emacs/src/.gdbinit This line will is actually printed out by GDB at start up (along with a lot of other text), so you can copy it from there. *** Preparing your debugging Emacs We will debug Emacs using Emacs of course. You can debug and use the same installation of Emacs if you choose. Or you can use an older, stable release. Debugging with GDB uses quite a bit of screen space. If you do not have a relatively large screen, then it decrease the font size to about the smallest you can cope with. Now open one of the source files of Emacs, found in the emacs/src directory. *** Starting GDB Start GDB directly from Emacs. If you do this from a buffer with a Emacs source file, the it will start in the correct directory, which has to be the src of Emacs, so that GDB will read the .gdbinit file described earlier. M-x gdb Change "bootstrap-emacs" to "emacs". Next we will open lots of windows, most of which you will need. Unfortunately, if you are reading this file in the same Emacs, you may no longer be able to the file, but you can switch back to it as normal. Now type, M-x gdb-many-windows If you want to restore this window configuration later, then M-x gdb-restore-windows will work. One unfortunate side effect of the .gdbinit script is that it's hard to stop Emacs when it has started, so we add a breakpoint somewhere in C in a function that is not likely to be called. This will enable us to get Emacs to stop. So, open the file floatfns.c and find the "log" function (search for "Flog"). You can set a breakpoint using the GDB menu, or by clicking in the fringe. If you want to save this breakpoint for later use, try: save breakpoint bplog.txt in the GDB window. You can source it later. *** Starting and Debugging Emacs Now start Emacs. In the GBD window, type: run -Q (the -Q gets passed straight to Emacs. Add other options if you want). GDB will now produce lots of output. Next type "continue" in the GDB window. Emacs will now start. In this Emacs (not the one you launched GDB with) try M-: (log 10) This Emacs should now hang, and floatfns.c should have popped up. The first argument to the log function is called, prosaically, "arg". We can print the value of this, using the print command. (gdb) print arg $1 = { i = 42 } Unfortunately, because it's actually a LispObject, that is now terrible useful. If you want to see the "real" value, then try pp arg instead. This prints to the window "*input/output of emacs*" where you should see 10 Another useful function is ppt which brings point (gdb) ppt BUF PT: 192[196] of 1..192[196] GAP: 192[196] SZ=2000 There are many more, and emacs/src/.gbdinit is worth reading in full.