DEBUG 31 KB

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