3 Copyright (C) 1985, 2000-2015 Free Software Foundation, Inc.
4 See the end of the file for license conditions.
7 [People who debug Emacs on Windows using Microsoft debuggers should
8 read the Windows-specific section near the end of this document.]
10 ** When you debug Emacs with GDB, you should start GDB in the directory
11 where the Emacs executable was made (the 'src' directory in the Emacs
12 source tree). That directory has a .gdbinit file that defines various
13 "user-defined" commands for debugging Emacs. (These commands are
14 described below under "Examining Lisp object values" and "Debugging
15 Emacs Redisplay problems".)
17 Some GDB versions by default do not automatically load .gdbinit files
18 in the directory where you invoke GDB. With those versions of GDB,
19 you will see a warning when GDB starts, like this:
21 warning: File ".../src/.gdbinit" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
23 There are several ways to overcome that difficulty, they are all
24 described in the node "Auto-loading safe path" in the GDB user
25 manual. If nothing else helps, type "source /path/to/.gdbinit RET" at
26 the GDB prompt, to unconditionally load the GDB init file.
28 ** When you are trying to analyze failed assertions or backtraces, it
29 is essential to compile Emacs with flags suitable for debugging.
30 With GCC 4.8 or later, you can invoke 'make' with CFLAGS="-Og -g3".
31 With older GCC or non-GCC compilers, you can use CFLAGS="-O0 -g3".
32 With GCC and higher optimization levels such as -O2, the
33 -fno-omit-frame-pointer and -fno-crossjumping options are often
34 essential. The latter prevents GCC from using the same abort call for
35 all assertions in a given function, rendering the stack backtrace
36 useless for identifying the specific failed assertion.
37 Some versions of GCC support recent versions of the DWARF standard for
38 debugging info, but default to older versions; for example, they could
39 support -gdwarf-4 compiler option (for DWARF v4), but default to
40 version 2 of the DWARF standard. For best results in debugging
41 abilities, find out the highest version of DWARF your GCC can support,
42 and use the corresponding -gdwarf-N switch instead of just -g (you
43 will still need -g3, as in "-gdwarf-4 -g3").
45 ** It is a good idea to run Emacs under GDB (or some other suitable
46 debugger) *all the time*. Then, when Emacs crashes, you will be able
47 to debug the live process, not just a core dump. (This is especially
48 important on systems which don't support core files, and instead print
49 just the registers and some stack addresses.)
51 ** If Emacs hangs, or seems to be stuck in some infinite loop, typing
52 "kill -TSTP PID", where PID is the Emacs process ID, will cause GDB to
53 kick in, provided that you run under GDB.
55 ** Getting control to the debugger
57 `Fsignal' is a very useful place to put a breakpoint in. All Lisp
58 errors go through there. If you are only interested in errors that
59 would fire the debugger, breaking at `maybe_call_debugger' is useful.
61 It is useful, when debugging, to have a guaranteed way to return to
62 the debugger at any time. When using X, this is easy: type C-z at the
63 window where Emacs is running under GDB, and it will stop Emacs just
64 as it would stop any ordinary program. When Emacs is running in a
65 terminal, things are not so easy.
67 The src/.gdbinit file in the Emacs distribution arranges for SIGINT
68 (C-g in Emacs) to be passed to Emacs and not give control back to GDB.
69 On modern POSIX systems, you can override that with this command:
71 handle SIGINT stop nopass
73 After this `handle' command, SIGINT will return control to GDB. If
74 you want the C-g to cause a QUIT within Emacs as well, omit the `nopass'.
76 A technique that can work when `handle SIGINT' does not is to store
77 the code for some character into the variable stop_character. Thus,
79 set stop_character = 29
81 makes Control-] (decimal code 29) the stop character.
82 Typing Control-] will cause immediate stop. You cannot
83 use the set command until the inferior process has been started.
84 Put a breakpoint early in `main', or suspend the Emacs,
85 to get an opportunity to do the set command.
87 Another technique for get control to the debugger is to put a
88 breakpoint in some rarely used function. One such convenient function
89 is Fredraw_display, which you can invoke at will interactively with
90 "M-x redraw-display RET".
92 When Emacs is running in a terminal, it is sometimes useful to use a separate
93 terminal for the debug session. This can be done by starting Emacs as usual,
94 then attaching to it from gdb with the `attach' command which is explained in
95 the node "Attach" of the GDB manual.
97 On MS-Windows, you can start Emacs in its own separate terminal by
98 setting the new-console option before running Emacs under GDB:
100 (gdb) set new-console 1
103 ** Examining Lisp object values.
105 When you have a live process to debug, and it has not encountered a
106 fatal error, you can use the GDB command `pr'. First print the value
107 in the ordinary way, with the `p' command. Then type `pr' with no
108 arguments. This calls a subroutine which uses the Lisp printer.
110 You can also use `pp value' to print the emacs value directly.
112 To see the current value of a Lisp Variable, use `pv variable'.
114 Note: It is not a good idea to try `pr', `pp', or `pv' if you know that Emacs
115 is in deep trouble: its stack smashed (e.g., if it encountered SIGSEGV
116 due to stack overflow), or crucial data structures, such as `obarray',
117 corrupted, etc. In such cases, the Emacs subroutine called by `pr'
118 might make more damage, like overwrite some data that is important for
119 debugging the original problem.
121 Also, on some systems it is impossible to use `pr' if you stopped
122 Emacs while it was inside `select'. This is in fact what happens if
123 you stop Emacs while it is waiting. In such a situation, don't try to
124 use `pr'. Instead, use `s' to step out of the system call. Then
125 Emacs will be between instructions and capable of handling `pr'.
127 If you can't use `pr' command, for whatever reason, you can use the
128 `xpr' command to print out the data type and value of the last data
134 You may also analyze data values using lower-level commands. Use the
135 `xtype' command to print out the data type of the last data value.
136 Once you know the data type, use the command that corresponds to that
137 type. Here are these commands:
139 xint xptr xwindow xmarker xoverlay xmiscfree xintfwd xboolfwd xobjfwd
140 xbufobjfwd xkbobjfwd xbuflocal xbuffer xsymbol xstring xvector xframe
141 xwinconfig xcompiled xcons xcar xcdr xsubr xprocess xfloat xscrollbar
142 xchartable xsubchartable xboolvector xhashtable xlist xcoding
143 xcharset xfontset xfont xbytecode
145 Each one of them applies to a certain type or class of types.
146 (Some of these types are not visible in Lisp, because they exist only
149 Each x... command prints some information about the value, and
150 produces a GDB value (subsequently available in $) through which you
151 can get at the rest of the contents.
153 In general, most of the rest of the contents will be additional Lisp
154 objects which you can examine in turn with the x... commands.
156 Even with a live process, these x... commands are useful for
157 examining the fields in a buffer, window, process, frame or marker.
158 Here's an example using concepts explained in the node "Value History"
159 of the GDB manual to print values associated with the variable
160 called frame. First, use these commands:
164 b set_frame_buffer_list
167 Then Emacs hits the breakpoint:
174 $2 = (struct frame *) 0x8560258
184 Now we can use `pr' to print the frame parameters:
186 (gdb) pp $->param_alist
187 ((background-mode . light) (display-type . color) [...])
189 The Emacs C code heavily uses macros defined in lisp.h. So suppose
190 we want the address of the l-value expression near the bottom of
191 `add_command_key' from keyboard.c:
193 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
195 XVECTOR is a macro, so GDB only knows about it if Emacs has been compiled with
196 preprocessor macro information. GCC provides this if you specify the options
197 `-gdwarf-N' (where N is 2 or higher) and `-g3'. In this case, GDB can
198 evaluate expressions like "p XVECTOR (this_command_keys)".
200 When this information isn't available, you can use the xvector command in GDB
201 to get the same result. Here is how:
203 (gdb) p this_command_keys
206 $2 = (struct Lisp_Vector *) 0x411000
208 (gdb) p $->contents[this_command_key_count]
211 $4 = (int *) 0x411008
213 Here's a related example of macros and the GDB `define' command.
214 There are many Lisp vectors such as `recent_keys', which contains the
215 last 300 keystrokes. We can print this Lisp vector
220 But this may be inconvenient, since `recent_keys' is much more verbose
221 than `C-h l'. We might want to print only the last 10 elements of
222 this vector. `recent_keys' is updated in keyboard.c by the command
224 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
226 So we define a GDB command `xvector-elts', so the last 10 keystrokes
229 xvector-elts recent_keys recent_keys_index 10
231 where you can define xvector-elts as follows:
239 p $foo->contents[$arg1-($i++)]
242 document xvector-elts
243 Prints a range of elements of a Lisp vector.
245 prints `i' elements of the vector `v' ending at the index `n'.
248 ** Getting Lisp-level backtrace information within GDB
250 The most convenient way is to use the `xbacktrace' command. This
251 shows the names of the Lisp functions that are currently active.
253 If that doesn't work (e.g., because the `backtrace_list' structure is
254 corrupted), type "bt" at the GDB prompt, to produce the C-level
255 backtrace, and look for stack frames that call Ffuncall. Select them
256 one by one in GDB, by typing "up N", where N is the appropriate number
257 of frames to go up, and in each frame that calls Ffuncall type this:
262 This will print the name of the Lisp function called by that level
265 By printing the remaining elements of args, you can see the argument
266 values. Here's how to print the first argument:
271 If you do not have a live process, you can use xtype and the other
272 x... commands such as xsymbol to get such information, albeit less
273 conveniently. For example:
278 and, assuming that "xtype" says that args[0] is a symbol:
282 ** Debugging Emacs redisplay problems
284 If you configured Emacs with --enable-checking='glyphs', you can use redisplay
285 tracing facilities from a running Emacs session.
287 The command "M-x trace-redisplay RET" will produce a trace of what redisplay
288 does on the standard error stream. This is very useful for understanding the
289 code paths taken by the display engine under various conditions, especially if
290 some redisplay optimizations produce wrong results. (You know that redisplay
291 optimizations might be involved if "M-x redraw-display RET", or even just
292 typing "M-x", causes Emacs to correct the bad display.) Since the cursor
293 blinking feature triggers periodic redisplay cycles, we recommend disabling
294 `blink-cursor-mode' before invoking `trace-redisplay', so that you have less
295 clutter in the trace. You can also have up to 30 last trace messages dumped to
296 standard error by invoking the `dump-redisplay-history' command.
298 To find the code paths which were taken by the display engine, search xdisp.c
299 for the trace messages you see.
301 The command `dump-glyph-matrix' is useful for producing on standard error
302 stream a full dump of the selected window's glyph matrix. See the function's
303 doc string for more details. If you are debugging redisplay issues in
304 text-mode frames, you may find the command `dump-frame-glyph-matrix' useful.
306 Other commands useful for debugging redisplay are `dump-glyph-row' and
309 If you run Emacs under GDB, you can print the contents of any glyph matrix by
310 just calling that function with the matrix as its argument. For example, the
311 following command will print the contents of the current matrix of the window
312 whose pointer is in `w':
314 (gdb) p dump_glyph_matrix (w->current_matrix, 2)
316 (The second argument 2 tells dump_glyph_matrix to print the glyphs in
319 The Emacs display code includes special debugging code, but it is normally
320 disabled. Configuring Emacs with --enable-checking='yes,glyphs' enables it.
322 Building Emacs like that activates many assertions which scrutinize
323 display code operation more than Emacs does normally. (To see the
324 code which tests these assertions, look for calls to the `eassert'
325 macros.) Any assertion that is reported to fail should be investigated.
327 When you debug display problems running emacs under X, you can use
328 the `ff' command to flush all pending display updates to the screen.
330 The src/.gdbinit file defines many useful commands for dumping redisplay
331 related data structures in a terse and user-friendly format:
333 `ppt' prints value of PT, narrowing, and gap in current buffer.
334 `pit' dumps the current display iterator `it'.
335 `pwin' dumps the current window 'win'.
336 `prow' dumps the current glyph_row `row'.
337 `pg' dumps the current glyph `glyph'.
338 `pgi' dumps the next glyph.
339 `pgrow' dumps all glyphs in current glyph_row `row'.
340 `pcursor' dumps current output_cursor.
342 The above commands also exist in a version with an `x' suffix which takes an
343 object of the relevant type as argument. For example, `pgrowx' dumps all
344 glyphs in its argument, which must be of type `struct glyph_row'.
346 Since redisplay is performed by Emacs very frequently, you need to place your
347 breakpoints cleverly to avoid hitting them all the time, when the issue you are
348 debugging did not (yet) happen. Here are some useful techniques for that:
350 . Put a breakpoint at `Fredraw_display' before running Emacs. Then do
351 whatever is required to reproduce the bad display, and invoke "M-x
352 redraw-display". The debugger will kick in, and you can set or enable
353 breakpoints in strategic places, knowing that the bad display will be
354 redrawn from scratch.
356 . For debugging incorrect cursor position, a good place to put a breakpoint is
357 in `set_cursor_from_row'. The first time this function is called as part of
358 `redraw-display', Emacs is redrawing the minibuffer window, which is usually
359 not what you want; type "continue" to get to the call you want. In general,
360 always make sure `set_cursor_from_row' is called for the right window and
361 buffer by examining the value of w->contents: it should be the buffer whose
362 display you are debugging.
364 . `set_cursor_from_row' is also a good place to look at the contents of a
365 screen line (a.k.a. "glyph row"), by means of the `pgrow' GDB command. Of
366 course, you need first to make sure the cursor is on the screen line which
367 you want to investigate. If you have set a breakpoint in `Fredraw_display',
368 as advised above, move cursor to that line before invoking `redraw-display'.
370 . If the problem happens only at some specific buffer position or for some
371 specific rarely-used character, you can make your breakpoints conditional on
372 those values. The display engine maintains the buffer and string position
373 it is processing in the it->current member; for example, the buffer
374 character position is in it->current.pos.charpos. Most redisplay functions
375 accept a pointer to a 'struct it' object as their argument, so you can make
376 conditional breakpoints in those functions, like this:
378 (gdb) break x_produce_glyphs if it->current.pos.charpos == 1234
380 For conditioning on the character being displayed, use it->c or
383 . You can also make the breakpoints conditional on what object is being used
384 for producing glyphs for display. The it->method member has the value
385 GET_FROM_BUFFER for displaying buffer contents, GET_FROM_STRING for
386 displaying a Lisp string (e.g., a `display' property or an overlay string),
387 GET_FROM_IMAGE for displaying an image, etc. See `enum it_method' in
388 dispextern.h for the full list of values.
390 ** Following longjmp call.
392 Recent versions of glibc (2.4+?) encrypt stored values for setjmp/longjmp which
393 prevents GDB from being able to follow a longjmp call using `next'. To
394 disable this protection you need to set the environment variable
395 LD_POINTER_GUARD to 0.
397 ** Using GDB in Emacs
399 Debugging with GDB in Emacs offers some advantages over the command line (See
400 the GDB Graphical Interface node of the Emacs manual). There are also some
401 features available just for debugging Emacs:
403 1) The command gud-pp is available on the tool bar (the `pp' icon) and
404 allows the user to print the s-expression of the variable at point,
407 2) Pressing `p' on a component of a watch expression that is a lisp object
408 in the speedbar prints its s-expression in the GUD buffer.
410 3) The STOP button on the tool bar is adjusted so that it sends SIGTSTP
411 instead of the usual SIGINT.
413 4) The command gud-pv has the global binding 'C-x C-a C-v' and prints the
414 value of the lisp variable at point.
416 ** Debugging what happens while preloading and dumping Emacs
418 Debugging `temacs' is useful when you want to establish whether a
419 problem happens in an undumped Emacs. To run `temacs' under a
420 debugger, type "gdb temacs", then start it with `r -batch -l loadup'.
422 If you need to debug what happens during dumping, start it with `r -batch -l
423 loadup dump' instead. For debugging the bootstrap dumping, use "loadup
424 bootstrap" instead of "loadup dump".
426 If temacs actually succeeds when running under GDB in this way, do not
427 try to run the dumped Emacs, because it was dumped with the GDB
430 ** If you encounter X protocol errors
432 The X server normally reports protocol errors asynchronously,
433 so you find out about them long after the primitive which caused
434 the error has returned.
436 To get clear information about the cause of an error, try evaluating
437 (x-synchronize t). That puts Emacs into synchronous mode, where each
438 Xlib call checks for errors before it returns. This mode is much
439 slower, but when you get an error, you will see exactly which call
440 really caused the error.
442 You can start Emacs in a synchronous mode by invoking it with the -xrm
445 emacs -xrm "emacs.synchronous: true"
447 Setting a breakpoint in the function `x_error_quitter' and looking at
448 the backtrace when Emacs stops inside that function will show what
449 code causes the X protocol errors.
451 Some bugs related to the X protocol disappear when Emacs runs in a
452 synchronous mode. To track down those bugs, we suggest the following
455 - Run Emacs under a debugger and put a breakpoint inside the
456 primitive function which, when called from Lisp, triggers the X
457 protocol errors. For example, if the errors happen when you
458 delete a frame, put a breakpoint inside `Fdelete_frame'.
460 - When the breakpoint breaks, step through the code, looking for
461 calls to X functions (the ones whose names begin with "X" or
464 - Insert calls to `XSync' before and after each call to the X
465 functions, like this:
467 XSync (f->output_data.x->display_info->display, 0);
469 where `f' is the pointer to the `struct frame' of the selected
470 frame, normally available via XFRAME (selected_frame). (Most
471 functions which call X already have some variable that holds the
472 pointer to the frame, perhaps called `f' or `sf', so you shouldn't
475 If your debugger can call functions in the program being debugged,
476 you should be able to issue the calls to `XSync' without recompiling
477 Emacs. For example, with GDB, just type:
479 call XSync (f->output_data.x->display_info->display, 0)
481 before and immediately after the suspect X calls. If your
482 debugger does not support this, you will need to add these pairs
483 of calls in the source and rebuild Emacs.
485 Either way, systematically step through the code and issue these
486 calls until you find the first X function called by Emacs after
487 which a call to `XSync' winds up in the function
488 `x_error_quitter'. The first X function call for which this
489 happens is the one that generated the X protocol error.
491 - You should now look around this offending X call and try to figure
492 out what is wrong with it.
494 ** If Emacs causes errors or memory leaks in your X server
496 You can trace the traffic between Emacs and your X server with a tool
497 like xmon, available at ftp://ftp.x.org/contrib/devel_tools/.
499 Xmon can be used to see exactly what Emacs sends when X protocol errors
500 happen. If Emacs causes the X server memory usage to increase you can
501 use xmon to see what items Emacs creates in the server (windows,
502 graphical contexts, pixmaps) and what items Emacs delete. If there
503 are consistently more creations than deletions, the type of item
504 and the activity you do when the items get created can give a hint where
507 ** If the symptom of the bug is that Emacs fails to respond
509 Don't assume Emacs is `hung'--it may instead be in an infinite loop.
510 To find out which, make the problem happen under GDB and stop Emacs
511 once it is not responding. (If Emacs is using X Windows directly, you
512 can stop Emacs by typing C-z at the GDB job. On MS-Windows, run Emacs
513 as usual, and then attach GDB to it -- that will usually interrupt
514 whatever Emacs is doing and let you perform the steps described
517 Then try stepping with `step'. If Emacs is hung, the `step' command
518 won't return. If it is looping, `step' will return.
520 If this shows Emacs is hung in a system call, stop it again and
521 examine the arguments of the call. If you report the bug, it is very
522 important to state exactly where in the source the system call is, and
523 what the arguments are.
525 If Emacs is in an infinite loop, try to determine where the loop
526 starts and ends. The easiest way to do this is to use the GDB command
527 `finish'. Each time you use it, Emacs resumes execution until it
528 exits one stack frame. Keep typing `finish' until it doesn't
529 return--that means the infinite loop is in the stack frame which you
530 just tried to finish.
532 Stop Emacs again, and use `finish' repeatedly again until you get back
533 to that frame. Then use `next' to step through that frame. By
534 stepping, you will see where the loop starts and ends. Also, examine
535 the data being used in the loop and try to determine why the loop does
536 not exit when it should.
538 On GNU and Unix systems, you can also trying sending Emacs SIGUSR2,
539 which, if `debug-on-event' has its default value, will cause Emacs to
540 attempt to break it out of its current loop and into the Lisp
541 debugger. This feature is useful when a C-level debugger is not
542 conveniently available.
544 ** If certain operations in Emacs are slower than they used to be, here
545 is some advice for how to find out why.
547 Stop Emacs repeatedly during the slow operation, and make a backtrace
548 each time. Compare the backtraces looking for a pattern--a specific
549 function that shows up more often than you'd expect.
551 If you don't see a pattern in the C backtraces, get some Lisp
552 backtrace information by typing "xbacktrace" or by looking at Ffuncall
553 frames (see above), and again look for a pattern.
555 When using X, you can stop Emacs at any time by typing C-z at GDB.
556 When not using X, you can do this with C-g. On non-Unix platforms,
557 such as MS-DOS, you might need to press C-BREAK instead.
559 ** If GDB does not run and your debuggers can't load Emacs.
561 On some systems, no debugger can load Emacs with a symbol table,
562 perhaps because they all have fixed limits on the number of symbols
563 and Emacs exceeds the limits. Here is a method that can be used
564 in such an extremity. Do
573 :r -l loadup (or whatever)
575 It is necessary to refer to the file `nmout' to convert
576 numeric addresses into symbols and vice versa.
578 It is useful to be running under a window system.
579 Then, if Emacs becomes hopelessly wedged, you can create another
580 window to do kill -9 in. kill -ILL is often useful too, since that
581 may make Emacs dump core or return to adb.
583 ** Debugging incorrect screen updating on a text terminal.
585 To debug Emacs problems that update the screen wrong, it is useful
586 to have a record of what input you typed and what Emacs sent to the
587 screen. To make these records, do
589 (open-dribble-file "~/.dribble")
590 (open-termscript "~/.termscript")
592 The dribble file contains all characters read by Emacs from the
593 terminal, and the termscript file contains all characters it sent to
594 the terminal. The use of the directory `~/' prevents interference
597 If you have irreproducible display problems, put those two expressions
598 in your ~/.emacs file. When the problem happens, exit the Emacs that
599 you were running, kill it, and rename the two files. Then you can start
600 another Emacs without clobbering those files, and use it to examine them.
602 An easy way to see if too much text is being redrawn on a terminal is to
603 evaluate `(setq inverse-video t)' before you try the operation you think
604 will cause too much redrawing. This doesn't refresh the screen, so only
605 newly drawn text is in inverse video.
609 If you encounter bugs whereby Emacs built with LessTif grabs all mouse
610 and keyboard events, or LessTif menus behave weirdly, it might be
611 helpful to set the `DEBUGSOURCES' and `DEBUG_FILE' environment
612 variables, so that one can see what LessTif was doing at this point.
615 export DEBUGSOURCES="RowColumn.c:MenuShell.c:MenuUtil.c"
616 export DEBUG_FILE=/usr/tmp/LESSTIF_TRACE
619 causes LessTif to print traces from the three named source files to a
620 file in `/usr/tmp' (that file can get pretty large). The above should
621 be typed at the shell prompt before invoking Emacs, as shown by the
624 Running GDB from another terminal could also help with such problems.
625 You can arrange for GDB to run on one machine, with the Emacs display
626 appearing on another. Then, when the bug happens, you can go back to
627 the machine where you started GDB and use the debugger from there.
629 ** Debugging problems which happen in GC
631 The array `last_marked' (defined on alloc.c) can be used to display up
632 to 500 last objects marked by the garbage collection process.
633 Whenever the garbage collector marks a Lisp object, it records the
634 pointer to that object in the `last_marked' array, which is maintained
635 as a circular buffer. The variable `last_marked_index' holds the
636 index into the `last_marked' array one place beyond where the pointer
637 to the very last marked object is stored.
639 The single most important goal in debugging GC problems is to find the
640 Lisp data structure that got corrupted. This is not easy since GC
641 changes the tag bits and relocates strings which make it hard to look
642 at Lisp objects with commands such as `pr'. It is sometimes necessary
643 to convert Lisp_Object variables into pointers to C struct's manually.
645 Use the `last_marked' array and the source to reconstruct the sequence
646 that objects were marked. In general, you need to correlate the
647 values recorded in the `last_marked' array with the corresponding
648 stack frames in the backtrace, beginning with the innermost frame.
649 Some subroutines of `mark_object' are invoked recursively, others loop
650 over portions of the data structure and mark them as they go. By
651 looking at the code of those routines and comparing the frames in the
652 backtrace with the values in `last_marked', you will be able to find
653 connections between the values in `last_marked'. E.g., when GC finds
654 a cons cell, it recursively marks its car and its cdr. Similar things
655 happen with properties of symbols, elements of vectors, etc. Use
656 these connections to reconstruct the data structure that was being
657 marked, paying special attention to the strings and names of symbols
658 that you encounter: these strings and symbol names can be used to grep
659 the sources to find out what high-level symbols and global variables
660 are involved in the crash.
662 Once you discover the corrupted Lisp object or data structure, grep
663 the sources for its uses and try to figure out what could cause the
664 corruption. If looking at the sources doesn't help, you could try
665 setting a watchpoint on the corrupted data, and see what code modifies
666 it in some invalid way. (Obviously, this technique is only useful for
667 data that is modified only very rarely.)
669 It is also useful to look at the corrupted object or data structure in
670 a fresh Emacs session and compare its contents with a session that you
673 ** Debugging problems with non-ASCII characters
675 If you experience problems which seem to be related to non-ASCII
676 characters, such as \201 characters appearing in the buffer or in your
677 files, set the variable byte-debug-flag to t. This causes Emacs to do
678 some extra checks, such as look for broken relations between byte and
679 character positions in buffers and strings; the resulting diagnostics
680 might pinpoint the cause of the problem.
682 ** Debugging the TTY (non-windowed) version
684 The most convenient method of debugging the character-terminal display
685 is to do that on a window system such as X. Begin by starting an
686 xterm window, then type these commands inside that window:
691 Let's say these commands print "/dev/ttyp4" and "xterm", respectively.
693 Now start Emacs (the normal, windowed-display session, i.e. without
694 the `-nw' option), and invoke "M-x gdb RET emacs RET" from there. Now
695 type these commands at GDB's prompt:
697 (gdb) set args -nw -t /dev/ttyp4
698 (gdb) set environment TERM xterm
701 The debugged Emacs should now start in no-window mode with its display
702 directed to the xterm window you opened above.
704 Similar arrangement is possible on a character terminal by using the
707 On MS-Windows, you can start Emacs in its own separate terminal by
708 setting the new-console option before running Emacs under GDB:
710 (gdb) set new-console 1
713 ** Running Emacs built with malloc debugging packages
715 If Emacs exhibits bugs that seem to be related to use of memory
716 allocated off the heap, it might be useful to link Emacs with a
717 special debugging library, such as Electric Fence (a.k.a. efence) or
718 GNU Checker, which helps find such problems.
720 Emacs compiled with such packages might not run without some hacking,
721 because Emacs replaces the system's memory allocation functions with
722 its own versions, and because the dumping process might be
723 incompatible with the way these packages use to track allocated
724 memory. Here are some of the changes you might find necessary:
726 - Edit configure, to set system_malloc and CANNOT_DUMP to "yes".
728 - Configure with a different --prefix= option. If you use GCC,
729 version 2.7.2 is preferred, as some malloc debugging packages
730 work a lot better with it than with 2.95 or later versions.
732 - Type "make" then "make -k install".
734 - If required, invoke the package-specific command to prepare
735 src/temacs for execution.
739 (Note that this runs `temacs' instead of the usual `emacs' executable.
740 This avoids problems with dumping Emacs mentioned above.)
742 Some malloc debugging libraries might print lots of false alarms for
743 bitfields used by Emacs in some data structures. If you want to get
744 rid of the false alarms, you will have to hack the definitions of
745 these data structures on the respective headers to remove the `:N'
746 bitfield definitions (which will cause each such field to use a full
749 ** How to recover buffer contents from an Emacs core dump file
751 The file etc/emacs-buffer.gdb defines a set of GDB commands for
752 recovering the contents of Emacs buffers from a core dump file. You
753 might also find those commands useful for displaying the list of
754 buffers in human-readable format from within the debugger.
756 ** Some suggestions for debugging on MS Windows:
758 (written by Marc Fleischeuers, Geoff Voelker and Andrew Innes)
760 To debug Emacs with Microsoft Visual C++, you either start emacs from
761 the debugger or attach the debugger to a running emacs process.
763 To start emacs from the debugger, you can use the file bin/debug.bat.
764 The Microsoft Developer studio will start and under Project, Settings,
765 Debug, General you can set the command-line arguments and Emacs's
766 startup directory. Set breakpoints (Edit, Breakpoints) at Fsignal and
767 other functions that you want to examine. Run the program (Build,
768 Start debug). Emacs will start and the debugger will take control as
769 soon as a breakpoint is hit.
771 You can also attach the debugger to an already running Emacs process.
772 To do this, start up the Microsoft Developer studio and select Build,
773 Start debug, Attach to process. Choose the Emacs process from the
774 list. Send a break to the running process (Debug, Break) and you will
775 find that execution is halted somewhere in user32.dll. Open the stack
776 trace window and go up the stack to w32_msg_pump. Now you can set
777 breakpoints in Emacs (Edit, Breakpoints). Continue the running Emacs
778 process (Debug, Step out) and control will return to Emacs, until a
781 To examine the contents of a Lisp variable, you can use the function
782 'debug_print'. Right-click on a variable, select QuickWatch (it has
783 an eyeglass symbol on its button in the toolbar), and in the text
784 field at the top of the window, place 'debug_print(' and ')' around
785 the expression. Press 'Recalculate' and the output is sent to stderr,
786 and to the debugger via the OutputDebugString routine. The output
787 sent to stderr should be displayed in the console window that was
788 opened when the emacs.exe executable was started. The output sent to
789 the debugger should be displayed in the 'Debug' pane in the Output
790 window. If Emacs was started from the debugger, a console window was
791 opened at Emacs' startup; this console window also shows the output of
794 For example, start and run Emacs in the debugger until it is waiting
795 for user input. Then click on the `Break' button in the debugger to
796 halt execution. Emacs should halt in `ZwUserGetMessage' waiting for
797 an input event. Use the `Call Stack' window to select the procedure
798 `w32_msp_pump' up the call stack (see below for why you have to do
799 this). Open the QuickWatch window and enter
800 "debug_print(Vexec_path)". Evaluating this expression will then print
801 out the contents of the Lisp variable `exec-path'.
803 If QuickWatch reports that the symbol is unknown, then check the call
804 stack in the `Call Stack' window. If the selected frame in the call
805 stack is not an Emacs procedure, then the debugger won't recognize
806 Emacs symbols. Instead, select a frame that is inside an Emacs
807 procedure and try using `debug_print' again.
809 If QuickWatch invokes debug_print but nothing happens, then check the
810 thread that is selected in the debugger. If the selected thread is
811 not the last thread to run (the "current" thread), then it cannot be
812 used to execute debug_print. Use the Debug menu to select the current
813 thread and try using debug_print again. Note that the debugger halts
814 execution (e.g., due to a breakpoint) in the context of the current
815 thread, so this should only be a problem if you've explicitly switched
818 It is also possible to keep appropriately masked and typecast Lisp
819 symbols in the Watch window, this is more convenient when steeping
820 though the code. For instance, on entering apply_lambda, you can
821 watch (struct Lisp_Symbol *) (0xfffffff & args[0]).
823 Optimizations often confuse the MS debugger. For example, the
824 debugger will sometimes report wrong line numbers, e.g., when it
825 prints the backtrace for a crash. It is usually best to look at the
826 disassembly to determine exactly what code is being run--the
827 disassembly will probably show several source lines followed by a
828 block of assembler for those lines. The actual point where Emacs
829 crashes will be one of those source lines, but not necessarily the one
830 that the debugger reports.
832 Another problematic area with the MS debugger is with variables that
833 are stored in registers: it will sometimes display wrong values for
834 those variables. Usually you will not be able to see any value for a
835 register variable, but if it is only being stored in a register
836 temporarily, you will see an old value for it. Again, you need to
837 look at the disassembly to determine which registers are being used,
838 and look at those registers directly, to see the actual current values
842 This file is part of GNU Emacs.
844 GNU Emacs is free software: you can redistribute it and/or modify
845 it under the terms of the GNU General Public License as published by
846 the Free Software Foundation, either version 3 of the License, or
847 (at your option) any later version.
849 GNU Emacs is distributed in the hope that it will be useful,
850 but WITHOUT ANY WARRANTY; without even the implied warranty of
851 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
852 GNU General Public License for more details.
854 You should have received a copy of the GNU General Public License
855 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
860 paragraph-separate: "[
\f]*$"