Mention `pv variable' to print value of Lisp variables.
[bpt/emacs.git] / etc / DEBUG
CommitLineData
a933dad1 1Debugging GNU Emacs
bfd6d01a 2Copyright (C) 1985, 2000, 2001, 2002, 2003, 2004,
5b0d63bc 3 2005, 2006 Free Software Foundation, Inc.
a933dad1
DL
4
5 Permission is granted to anyone to make or distribute verbatim copies
6 of this document as received, in any medium, provided that the
7 copyright notice and permission notice are preserved,
8 and that the distributor grants the recipient permission
9 for further redistribution as permitted by this notice.
10
11 Permission is granted to distribute modified versions
12 of this document, or of portions of it,
13 under the above conditions, provided also that they
14 carry prominent notices stating who last changed them.
15
437368fe
EZ
16[People who debug Emacs on Windows using native Windows debuggers
17should read the Windows-specific section near the end of this
18document.]
19
42a3c627 20** When you debug Emacs with GDB, you should start it in the directory
7483daa1
RS
21where the executable was made. That directory has a .gdbinit file
22that defines various "user-defined" commands for debugging Emacs.
42a3c627 23
70d9a9cd
DK
24** When you are trying to analyze failed assertions, it will be
25essential to compile Emacs either completely without optimizations or
26at least (when using GCC) with the -fno-crossjumping option. Failure
27to do so may make the compiler recycle the same abort call for all
28assertions in a given function, rendering the stack backtrace useless
29for identifying the specific failed assertion.
30
42a3c627 31** It is a good idea to run Emacs under GDB (or some other suitable
437368fe
EZ
32debugger) *all the time*. Then, when Emacs crashes, you will be able
33to debug the live process, not just a core dump. (This is especially
34important on systems which don't support core files, and instead print
35just the registers and some stack addresses.)
36
42a3c627 37** If Emacs hangs, or seems to be stuck in some infinite loop, typing
437368fe
EZ
38"kill -TSTP PID", where PID is the Emacs process ID, will cause GDB to
39kick in, provided that you run under GDB.
40
41** Getting control to the debugger
a933dad1 42
3102e429 43`Fsignal' is a very useful place to put a breakpoint in.
a933dad1
DL
44All Lisp errors go through there.
45
3102e429 46It is useful, when debugging, to have a guaranteed way to return to
eb55f651 47the debugger at any time. When using X, this is easy: type C-z at the
3102e429
EZ
48window where Emacs is running under GDB, and it will stop Emacs just
49as it would stop any ordinary program. When Emacs is running in a
50terminal, things are not so easy.
51
52The src/.gdbinit file in the Emacs distribution arranges for SIGINT
53(C-g in Emacs) to be passed to Emacs and not give control back to GDB.
54On modern POSIX systems, you can override that with this command:
55
7718638c 56 handle SIGINT stop nopass
3102e429
EZ
57
58After this `handle' command, SIGINT will return control to GDB. If
59you want the C-g to cause a QUIT within Emacs as well, omit the
60`nopass'.
61
62A technique that can work when `handle SIGINT' does not is to store
63the code for some character into the variable stop_character. Thus,
a933dad1
DL
64
65 set stop_character = 29
66
67makes Control-] (decimal code 29) the stop character.
68Typing Control-] will cause immediate stop. You cannot
69use the set command until the inferior process has been started.
70Put a breakpoint early in `main', or suspend the Emacs,
71to get an opportunity to do the set command.
72
8ef597fe
NR
73When Emacs is running in a terminal, it is useful to use a separate terminal
74for the debug session. This can be done by starting Emacs as usual, then
75attaching to it from gdb with the `attach' command which is explained in the
76node "Attach" of the GDB manual.
77
a933dad1
DL
78** Examining Lisp object values.
79
80When you have a live process to debug, and it has not encountered a
81fatal error, you can use the GDB command `pr'. First print the value
82in the ordinary way, with the `p' command. Then type `pr' with no
83arguments. This calls a subroutine which uses the Lisp printer.
84
1ce9f40a
KS
85You can also use `pp value' to print the emacs value directly.
86
11eced2f
KS
87To see the current value of a Lisp Variable, use `pv variable'.
88
89Note: It is not a good idea to try `pr', `pp', or `pv' if you know that Emacs
1ce9f40a
KS
90is in deep trouble: its stack smashed (e.g., if it encountered SIGSEGV
91due to stack overflow), or crucial data structures, such as `obarray',
437368fe
EZ
92corrupted, etc. In such cases, the Emacs subroutine called by `pr'
93might make more damage, like overwrite some data that is important for
94debugging the original problem.
95
3102e429
EZ
96Also, on some systems it is impossible to use `pr' if you stopped
97Emacs while it was inside `select'. This is in fact what happens if
98you stop Emacs while it is waiting. In such a situation, don't try to
99use `pr'. Instead, use `s' to step out of the system call. Then
100Emacs will be between instructions and capable of handling `pr'.
a933dad1 101
11eced2f
KS
102If you can't use `pr' command, for whatever reason, you can use the
103`xpr' command to print out the data type and value of the last data
104value, For example:
105
106 p it->object
107 xpr
108
109You may also analyze data values using lower-level commands. Use the
110`xtype' command to print out the data type of the last data value.
111Once you know the data type, use the command that corresponds to that
112type. Here are these commands:
a933dad1
DL
113
114 xint xptr xwindow xmarker xoverlay xmiscfree xintfwd xboolfwd xobjfwd
115 xbufobjfwd xkbobjfwd xbuflocal xbuffer xsymbol xstring xvector xframe
116 xwinconfig xcompiled xcons xcar xcdr xsubr xprocess xfloat xscrollbar
117
118Each one of them applies to a certain type or class of types.
119(Some of these types are not visible in Lisp, because they exist only
120internally.)
121
122Each x... command prints some information about the value, and
123produces a GDB value (subsequently available in $) through which you
124can get at the rest of the contents.
125
437368fe 126In general, most of the rest of the contents will be additional Lisp
a933dad1
DL
127objects which you can examine in turn with the x... commands.
128
437368fe
EZ
129Even with a live process, these x... commands are useful for
130examining the fields in a buffer, window, process, frame or marker.
131Here's an example using concepts explained in the node "Value History"
aa1f38cd
NR
132of the GDB manual to print values associated with the variable
133called frame. First, use these commands:
437368fe
EZ
134
135 cd src
136 gdb emacs
aa1f38cd 137 b set_frame_buffer_list
177c0ea7 138 r -q
437368fe 139
8ef597fe 140Then Emacs hits the breakpoint:
437368fe
EZ
141
142 (gdb) p frame
aa1f38cd 143 $1 = 139854428
11eced2f 144 (gdb) xpr
437368fe
EZ
145 Lisp_Vectorlike
146 PVEC_FRAME
aa1f38cd 147 $2 = (struct frame *) 0x8560258
11eced2f 148 "emacs@localhost"
437368fe
EZ
149 (gdb) p *$
150 $3 = {
aa1f38cd
NR
151 size = 1073742931,
152 next = 0x85dfe58,
153 name = 140615219,
437368fe
EZ
154 [...]
155 }
437368fe 156
11eced2f
KS
157Now we can use `pr' to print the frame parameters:
158
159 (gdb) pp $->param_alist
160 ((background-mode . light) (display-type . color) [...])
437368fe 161
437368fe
EZ
162
163The Emacs C code heavily uses macros defined in lisp.h. So suppose
164we want the address of the l-value expression near the bottom of
165`add_command_key' from keyboard.c:
166
167 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
168
f6e405a2
NR
169XVECTOR is a macro, so GDB only knows about it if Emacs has been compiled with
170preprocessor macro information. GCC provides this if you specify the options
171`-gdwarf-2' and `-g3'. In this case, GDB can evaluate expressions like
172"p XVECTOR (this_command_keys)".
437368fe 173
f6e405a2
NR
174When this information isn't available, you can use the xvector command in GDB
175to get the same result. Here is how:
437368fe
EZ
176
177 (gdb) p this_command_keys
178 $1 = 1078005760
179 (gdb) xvector
180 $2 = (struct Lisp_Vector *) 0x411000
181 0
182 (gdb) p $->contents[this_command_key_count]
183 $3 = 1077872640
184 (gdb) p &$
185 $4 = (int *) 0x411008
186
187Here's a related example of macros and the GDB `define' command.
188There are many Lisp vectors such as `recent_keys', which contains the
189last 100 keystrokes. We can print this Lisp vector
190
191p recent_keys
192pr
193
194But this may be inconvenient, since `recent_keys' is much more verbose
195than `C-h l'. We might want to print only the last 10 elements of
196this vector. `recent_keys' is updated in keyboard.c by the command
197
198 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
199
200So we define a GDB command `xvector-elts', so the last 10 keystrokes
177c0ea7 201are printed by
437368fe
EZ
202
203 xvector-elts recent_keys recent_keys_index 10
204
205where you can define xvector-elts as follows:
206
207 define xvector-elts
208 set $i = 0
209 p $arg0
210 xvector
211 set $foo = $
212 while $i < $arg2
177c0ea7 213 p $foo->contents[$arg1-($i++)]
437368fe
EZ
214 pr
215 end
216 document xvector-elts
217 Prints a range of elements of a Lisp vector.
218 xvector-elts v n i
219 prints `i' elements of the vector `v' ending at the index `n'.
220 end
221
222** Getting Lisp-level backtrace information within GDB
223
3102e429
EZ
224The most convenient way is to use the `xbacktrace' command. This
225shows the names of the Lisp functions that are currently active.
437368fe
EZ
226
227If that doesn't work (e.g., because the `backtrace_list' structure is
228corrupted), type "bt" at the GDB prompt, to produce the C-level
229backtrace, and look for stack frames that call Ffuncall. Select them
230one by one in GDB, by typing "up N", where N is the appropriate number
231of frames to go up, and in each frame that calls Ffuncall type this:
232
233 p *args
234 pr
235
236This will print the name of the Lisp function called by that level
237of function calling.
238
239By printing the remaining elements of args, you can see the argument
240values. Here's how to print the first argument:
177c0ea7 241
437368fe
EZ
242 p args[1]
243 pr
244
245If you do not have a live process, you can use xtype and the other
246x... commands such as xsymbol to get such information, albeit less
247conveniently. For example:
248
249 p *args
250 xtype
251
252and, assuming that "xtype" says that args[0] is a symbol:
253
177c0ea7 254 xsymbol
437368fe 255
11eced2f
KS
256** Debugging Emacs Redisplay problems
257
258The src/.gdbinit file defines many useful commands for dumping redisplay
259related data structures in a terse and user-friendly format:
260
261 `ppt' prints value of PT, narrowing, and gap in current buffer.
262 `pit' dumps the current display iterator `it'.
263 `pwin' dumps the current window 'win'.
264 `prow' dumps the current glyph_row `row'.
265 `pg' dumps the current glyph `glyph'.
266 `pgi' dumps the next glyph.
267 `pgrow' dumps all glyphs in current glyph_row `row'.
268 `pcursor' dumps current output_cursor.
269
270The above commands also exist in a version with an `x' suffix which
271takes an object of the relevant type as argument.
272
c167547b
NR
273** Using GDB in Emacs
274
275Debugging with GDB in Emacs offers some advantages over the command line (See
276the GDB Graphical Interface node of the Emacs manual). There are also some
277features available just for debugging Emacs:
278
2791) The command gud-pp isavailable on the tool bar (the `pp' icon) and allows
280 the user to print the s-expression of the variable at point, in the GUD
281 buffer.
282
2832) Pressing `p' on a component of a watch expression that is a lisp object
284 in the speedbar prints its s-expression in the GUD buffer.
285
2863) The STOP button on the tool bar is adjusted so that it sends SIGTSTP
287 instead of the usual SIGINT.
288
2894) The command gud-pv has the global binding 'C-x C-a C-v' and prints the
290 value of the lisp variable at point.
291
437368fe
EZ
292** Debugging what happens while preloading and dumping Emacs
293
294Type `gdb temacs' and start it with `r -batch -l loadup dump'.
295
296If temacs actually succeeds when running under GDB in this way, do not
297try to run the dumped Emacs, because it was dumped with the GDB
298breakpoints in it.
299
300** Debugging `temacs'
301
302Debugging `temacs' is useful when you want to establish whether a
303problem happens in an undumped Emacs. To run `temacs' under a
304debugger, type "gdb temacs", then start it with `r -batch -l loadup'.
305
306** If you encounter X protocol errors
307
308Try evaluating (x-synchronize t). That puts Emacs into synchronous
309mode, where each Xlib call checks for errors before it returns. This
310mode is much slower, but when you get an error, you will see exactly
311which call really caused the error.
312
ec4054f0
EZ
313You can start Emacs in a synchronous mode by invoking it with the -xrm
314option, like this:
315
9031cdf2 316 emacs -xrm "emacs.synchronous: true"
ec4054f0
EZ
317
318Setting a breakpoint in the function `x_error_quitter' and looking at
319the backtrace when Emacs stops inside that function will show what
320code causes the X protocol errors.
321
1d3adc26
EZ
322Some bugs related to the X protocol disappear when Emacs runs in a
323synchronous mode. To track down those bugs, we suggest the following
324procedure:
325
326 - Run Emacs under a debugger and put a breakpoint inside the
327 primitive function which, when called from Lisp, triggers the X
328 protocol errors. For example, if the errors happen when you
329 delete a frame, put a breakpoint inside `Fdelete_frame'.
330
331 - When the breakpoint breaks, step through the code, looking for
332 calls to X functions (the ones whose names begin with "X" or
333 "Xt" or "Xm").
334
335 - Insert calls to `XSync' before and after each call to the X
336 functions, like this:
337
338 XSync (f->output_data.x->display_info->display, 0);
339
340 where `f' is the pointer to the `struct frame' of the selected
341 frame, normally available via XFRAME (selected_frame). (Most
342 functions which call X already have some variable that holds the
343 pointer to the frame, perhaps called `f' or `sf', so you shouldn't
344 need to compute it.)
345
346 If your debugger can call functions in the program being debugged,
347 you should be able to issue the calls to `XSync' without recompiling
348 Emacs. For example, with GDB, just type:
349
350 call XSync (f->output_data.x->display_info->display, 0)
351
352 before and immediately after the suspect X calls. If your
353 debugger does not support this, you will need to add these pairs
354 of calls in the source and rebuild Emacs.
355
356 Either way, systematically step through the code and issue these
357 calls until you find the first X function called by Emacs after
358 which a call to `XSync' winds up in the function
359 `x_error_quitter'. The first X function call for which this
360 happens is the one that generated the X protocol error.
361
362 - You should now look around this offending X call and try to figure
363 out what is wrong with it.
364
46def989
JD
365** If Emacs causes errors or memory leaks in your X server
366
367You can trace the traffic between Emacs and your X server with a tool
368like xmon, available at ftp://ftp.x.org/contrib/devel_tools/.
369
370Xmon can be used to see exactly what Emacs sends when X protocol errors
371happen. If Emacs causes the X server memory usage to increase you can
372use xmon to see what items Emacs creates in the server (windows,
373graphical contexts, pixmaps) and what items Emacs delete. If there
374are consistently more creations than deletions, the type of item
375and the activity you do when the items get created can give a hint where
376to start debugging.
377
437368fe
EZ
378** If the symptom of the bug is that Emacs fails to respond
379
380Don't assume Emacs is `hung'--it may instead be in an infinite loop.
381To find out which, make the problem happen under GDB and stop Emacs
382once it is not responding. (If Emacs is using X Windows directly, you
383can stop Emacs by typing C-z at the GDB job.) Then try stepping with
384`step'. If Emacs is hung, the `step' command won't return. If it is
385looping, `step' will return.
386
387If this shows Emacs is hung in a system call, stop it again and
388examine the arguments of the call. If you report the bug, it is very
389important to state exactly where in the source the system call is, and
390what the arguments are.
391
392If Emacs is in an infinite loop, try to determine where the loop
393starts and ends. The easiest way to do this is to use the GDB command
394`finish'. Each time you use it, Emacs resumes execution until it
395exits one stack frame. Keep typing `finish' until it doesn't
396return--that means the infinite loop is in the stack frame which you
397just tried to finish.
398
399Stop Emacs again, and use `finish' repeatedly again until you get back
400to that frame. Then use `next' to step through that frame. By
401stepping, you will see where the loop starts and ends. Also, examine
402the data being used in the loop and try to determine why the loop does
403not exit when it should.
404
405** If certain operations in Emacs are slower than they used to be, here
406is some advice for how to find out why.
407
408Stop Emacs repeatedly during the slow operation, and make a backtrace
409each time. Compare the backtraces looking for a pattern--a specific
410function that shows up more often than you'd expect.
411
412If you don't see a pattern in the C backtraces, get some Lisp
413backtrace information by typing "xbacktrace" or by looking at Ffuncall
414frames (see above), and again look for a pattern.
415
416When using X, you can stop Emacs at any time by typing C-z at GDB.
417When not using X, you can do this with C-g. On non-Unix platforms,
418such as MS-DOS, you might need to press C-BREAK instead.
419
a933dad1
DL
420** If GDB does not run and your debuggers can't load Emacs.
421
422On some systems, no debugger can load Emacs with a symbol table,
423perhaps because they all have fixed limits on the number of symbols
424and Emacs exceeds the limits. Here is a method that can be used
425in such an extremity. Do
426
427 nm -n temacs > nmout
428 strip temacs
429 adb temacs
430 0xd:i
431 0xe:i
432 14:i
433 17:i
434 :r -l loadup (or whatever)
435
436It is necessary to refer to the file `nmout' to convert
437numeric addresses into symbols and vice versa.
438
439It is useful to be running under a window system.
440Then, if Emacs becomes hopelessly wedged, you can create
441another window to do kill -9 in. kill -ILL is often
442useful too, since that may make Emacs dump core or return
443to adb.
444
445
446** Debugging incorrect screen updating.
447
448To debug Emacs problems that update the screen wrong, it is useful
449to have a record of what input you typed and what Emacs sent to the
450screen. To make these records, do
451
452(open-dribble-file "~/.dribble")
453(open-termscript "~/.termscript")
454
455The dribble file contains all characters read by Emacs from the
456terminal, and the termscript file contains all characters it sent to
457the terminal. The use of the directory `~/' prevents interference
458with any other user.
459
460If you have irreproducible display problems, put those two expressions
461in your ~/.emacs file. When the problem happens, exit the Emacs that
462you were running, kill it, and rename the two files. Then you can start
463another Emacs without clobbering those files, and use it to examine them.
125f929e
MB
464
465An easy way to see if too much text is being redrawn on a terminal is to
466evaluate `(setq inverse-video t)' before you try the operation you think
467will cause too much redrawing. This doesn't refresh the screen, so only
468newly drawn text is in inverse video.
437368fe 469
3f715e77
EZ
470The Emacs display code includes special debugging code, but it is
471normally disabled. You can enable it by building Emacs with the
472pre-processing symbol GLYPH_DEBUG defined. Here's one easy way,
473suitable for Unix and GNU systems, to build such a debugging version:
474
475 MYCPPFLAGS='-DGLYPH_DEBUG=1' make
476
477Building Emacs like that activates many assertions which scrutinize
478display code operation more than Emacs does normally. (To see the
479code which tests these assertions, look for calls to the `xassert'
480macros.) Any assertion that is reported to fail should be
481investigated.
482
483Building with GLYPH_DEBUG defined also defines several helper
484functions which can help debugging display code. One such function is
485`dump_glyph_matrix'. If you run Emacs under GDB, you can print the
486contents of any glyph matrix by just calling that function with the
487matrix as its argument. For example, the following command will print
488the contents of the current matrix of the window whose pointer is in
489`w':
490
491 (gdb) p dump_glyph_matrix (w->current_matrix, 2)
492
493(The second argument 2 tells dump_glyph_matrix to print the glyphs in
494a long form.) You can dump the selected window's current glyph matrix
495interactively with "M-x dump-glyph-matrix RET"; see the documentation
496of this function for more details.
497
498Several more functions for debugging display code are available in
71d6b459
EZ
499Emacs compiled with GLYPH_DEBUG defined; type "C-h f dump- TAB" and
500"C-h f trace- TAB" to see the full list.
3f715e77 501
1ce9f40a
KS
502When you debug display problems running emacs under X, you can use
503the `ff' command to flush all pending display updates to the screen.
504
437368fe
EZ
505
506** Debugging LessTif
507
508If you encounter bugs whereby Emacs built with LessTif grabs all mouse
509and keyboard events, or LessTif menus behave weirdly, it might be
510helpful to set the `DEBUGSOURCES' and `DEBUG_FILE' environment
511variables, so that one can see what LessTif was doing at this point.
512For instance
177c0ea7 513
6806e867 514 export DEBUGSOURCES="RowColumn.c:MenuShell.c:MenuUtil.c"
437368fe 515 export DEBUG_FILE=/usr/tmp/LESSTIF_TRACE
2aa25884 516 emacs &
437368fe
EZ
517
518causes LessTif to print traces from the three named source files to a
2aa25884
EZ
519file in `/usr/tmp' (that file can get pretty large). The above should
520be typed at the shell prompt before invoking Emacs, as shown by the
521last line above.
437368fe
EZ
522
523Running GDB from another terminal could also help with such problems.
524You can arrange for GDB to run on one machine, with the Emacs display
525appearing on another. Then, when the bug happens, you can go back to
526the machine where you started GDB and use the debugger from there.
527
528
437368fe
EZ
529** Debugging problems which happen in GC
530
f46cc673
EZ
531The array `last_marked' (defined on alloc.c) can be used to display up
532to 500 last objects marked by the garbage collection process.
62578de5 533Whenever the garbage collector marks a Lisp object, it records the
1a527e27
EZ
534pointer to that object in the `last_marked' array, which is maintained
535as a circular buffer. The variable `last_marked_index' holds the
536index into the `last_marked' array one place beyond where the pointer
537to the very last marked object is stored.
437368fe
EZ
538
539The single most important goal in debugging GC problems is to find the
540Lisp data structure that got corrupted. This is not easy since GC
541changes the tag bits and relocates strings which make it hard to look
542at Lisp objects with commands such as `pr'. It is sometimes necessary
543to convert Lisp_Object variables into pointers to C struct's manually.
437368fe 544
1a527e27
EZ
545Use the `last_marked' array and the source to reconstruct the sequence
546that objects were marked. In general, you need to correlate the
547values recorded in the `last_marked' array with the corresponding
548stack frames in the backtrace, beginning with the innermost frame.
549Some subroutines of `mark_object' are invoked recursively, others loop
550over portions of the data structure and mark them as they go. By
551looking at the code of those routines and comparing the frames in the
552backtrace with the values in `last_marked', you will be able to find
553connections between the values in `last_marked'. E.g., when GC finds
554a cons cell, it recursively marks its car and its cdr. Similar things
555happen with properties of symbols, elements of vectors, etc. Use
556these connections to reconstruct the data structure that was being
557marked, paying special attention to the strings and names of symbols
558that you encounter: these strings and symbol names can be used to grep
559the sources to find out what high-level symbols and global variables
560are involved in the crash.
561
562Once you discover the corrupted Lisp object or data structure, grep
563the sources for its uses and try to figure out what could cause the
564corruption. If looking at the sources doesn;t help, you could try
565setting a watchpoint on the corrupted data, and see what code modifies
566it in some invalid way. (Obviously, this technique is only useful for
567data that is modified only very rarely.)
568
569It is also useful to look at the corrupted object or data structure in
570a fresh Emacs session and compare its contents with a session that you
571are debugging.
437368fe 572
93699019
EZ
573** Debugging problems with non-ASCII characters
574
575If you experience problems which seem to be related to non-ASCII
576characters, such as \201 characters appearing in the buffer or in your
577files, set the variable byte-debug-flag to t. This causes Emacs to do
578some extra checks, such as look for broken relations between byte and
579character positions in buffers and strings; the resulting diagnostics
580might pinpoint the cause of the problem.
581
036cb5a2
EZ
582** Debugging the TTY (non-windowed) version
583
584The most convenient method of debugging the character-terminal display
585is to do that on a window system such as X. Begin by starting an
586xterm window, then type these commands inside that window:
587
588 $ tty
589 $ echo $TERM
590
591Let's say these commands print "/dev/ttyp4" and "xterm", respectively.
592
593Now start Emacs (the normal, windowed-display session, i.e. without
594the `-nw' option), and invoke "M-x gdb RET emacs RET" from there. Now
595type these commands at GDB's prompt:
596
597 (gdb) set args -nw -t /dev/ttyp4
598 (gdb) set environment TERM xterm
599 (gdb) run
600
601The debugged Emacs should now start in no-window mode with its display
602directed to the xterm window you opened above.
603
e039053d
EZ
604Similar arrangement is possible on a character terminal by using the
605`screen' package.
606
19cf8f36
EZ
607** Running Emacs built with malloc debugging packages
608
609If Emacs exhibits bugs that seem to be related to use of memory
610allocated off the heap, it might be useful to link Emacs with a
611special debugging library, such as Electric Fence (a.k.a. efence) or
612GNU Checker, which helps find such problems.
613
614Emacs compiled with such packages might not run without some hacking,
615because Emacs replaces the system's memory allocation functions with
616its own versions, and because the dumping process might be
617incompatible with the way these packages use to track allocated
618memory. Here are some of the changes you might find necessary
619(SYSTEM-NAME and MACHINE-NAME are the names of your OS- and
620CPU-specific headers in the subdirectories of `src'):
621
622 - In src/s/SYSTEM-NAME.h add "#define SYSTEM_MALLOC".
623
624 - In src/m/MACHINE-NAME.h add "#define CANNOT_DUMP" and
625 "#define CANNOT_UNEXEC".
626
627 - Configure with a different --prefix= option. If you use GCC,
628 version 2.7.2 is preferred, as some malloc debugging packages
629 work a lot better with it than with 2.95 or later versions.
630
631 - Type "make" then "make -k install".
632
633 - If required, invoke the package-specific command to prepare
634 src/temacs for execution.
635
636 - cd ..; src/temacs
637
638(Note that this runs `temacs' instead of the usual `emacs' executable.
639This avoids problems with dumping Emacs mentioned above.)
640
641Some malloc debugging libraries might print lots of false alarms for
642bitfields used by Emacs in some data structures. If you want to get
643rid of the false alarms, you will have to hack the definitions of
644these data structures on the respective headers to remove the `:N'
645bitfield definitions (which will cause each such field to use a full
646int).
647
270bf00e
EZ
648** How to recover buffer contents from an Emacs core dump file
649
650The file etc/emacs-buffer.gdb defines a set of GDB commands for
651recovering the contents of Emacs buffers from a core dump file. You
652might also find those commands useful for displaying the list of
653buffers in human-readable format from within the debugger.
654
437368fe
EZ
655** Some suggestions for debugging on MS Windows:
656
657 (written by Marc Fleischeuers, Geoff Voelker and Andrew Innes)
658
3102e429 659To debug Emacs with Microsoft Visual C++, you either start emacs from
961e2394
EZ
660the debugger or attach the debugger to a running emacs process.
661
662To start emacs from the debugger, you can use the file bin/debug.bat.
663The Microsoft Developer studio will start and under Project, Settings,
3102e429 664Debug, General you can set the command-line arguments and Emacs's
437368fe
EZ
665startup directory. Set breakpoints (Edit, Breakpoints) at Fsignal and
666other functions that you want to examine. Run the program (Build,
667Start debug). Emacs will start and the debugger will take control as
668soon as a breakpoint is hit.
669
3102e429 670You can also attach the debugger to an already running Emacs process.
437368fe
EZ
671To do this, start up the Microsoft Developer studio and select Build,
672Start debug, Attach to process. Choose the Emacs process from the
673list. Send a break to the running process (Debug, Break) and you will
674find that execution is halted somewhere in user32.dll. Open the stack
675trace window and go up the stack to w32_msg_pump. Now you can set
676breakpoints in Emacs (Edit, Breakpoints). Continue the running Emacs
677process (Debug, Step out) and control will return to Emacs, until a
678breakpoint is hit.
679
3102e429 680To examine the contents of a Lisp variable, you can use the function
437368fe
EZ
681'debug_print'. Right-click on a variable, select QuickWatch (it has
682an eyeglass symbol on its button in the toolbar), and in the text
683field at the top of the window, place 'debug_print(' and ')' around
684the expression. Press 'Recalculate' and the output is sent to stderr,
685and to the debugger via the OutputDebugString routine. The output
686sent to stderr should be displayed in the console window that was
687opened when the emacs.exe executable was started. The output sent to
688the debugger should be displayed in the 'Debug' pane in the Output
689window. If Emacs was started from the debugger, a console window was
690opened at Emacs' startup; this console window also shows the output of
691'debug_print'.
692
693For example, start and run Emacs in the debugger until it is waiting
694for user input. Then click on the `Break' button in the debugger to
695halt execution. Emacs should halt in `ZwUserGetMessage' waiting for
696an input event. Use the `Call Stack' window to select the procedure
697`w32_msp_pump' up the call stack (see below for why you have to do
698this). Open the QuickWatch window and enter
699"debug_print(Vexec_path)". Evaluating this expression will then print
3102e429 700out the contents of the Lisp variable `exec-path'.
437368fe
EZ
701
702If QuickWatch reports that the symbol is unknown, then check the call
703stack in the `Call Stack' window. If the selected frame in the call
704stack is not an Emacs procedure, then the debugger won't recognize
705Emacs symbols. Instead, select a frame that is inside an Emacs
706procedure and try using `debug_print' again.
707
708If QuickWatch invokes debug_print but nothing happens, then check the
709thread that is selected in the debugger. If the selected thread is
710not the last thread to run (the "current" thread), then it cannot be
711used to execute debug_print. Use the Debug menu to select the current
712thread and try using debug_print again. Note that the debugger halts
713execution (e.g., due to a breakpoint) in the context of the current
714thread, so this should only be a problem if you've explicitly switched
715threads.
716
3102e429 717It is also possible to keep appropriately masked and typecast Lisp
437368fe
EZ
718symbols in the Watch window, this is more convenient when steeping
719though the code. For instance, on entering apply_lambda, you can
720watch (struct Lisp_Symbol *) (0xfffffff & args[0]).
961e2394
EZ
721
722Optimizations often confuse the MS debugger. For example, the
723debugger will sometimes report wrong line numbers, e.g., when it
724prints the backtrace for a crash. It is usually best to look at the
725disassembly to determine exactly what code is being run--the
726disassembly will probably show several source lines followed by a
727block of assembler for those lines. The actual point where Emacs
728crashes will be one of those source lines, but not neccesarily the one
729that the debugger reports.
730
731Another problematic area with the MS debugger is with variables that
732are stored in registers: it will sometimes display wrong values for
733those variables. Usually you will not be able to see any value for a
734register variable, but if it is only being stored in a register
735temporarily, you will see an old value for it. Again, you need to
736look at the disassembly to determine which registers are being used,
737and look at those registers directly, to see the actual current values
738of these variables.
ab5796a9
MB
739
740;;; arch-tag: fbf32980-e35d-481f-8e4c-a2eca2586e6b