File: debugging.texi

package info (click to toggle)
elisp-manual-ja 20-2.5-jp-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,996 kB
  • ctags: 239
  • sloc: lisp: 2,837; perl: 182; makefile: 45; sh: 16
file content (1247 lines) | stat: -rw-r--r-- 60,325 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1998 Free Software Foundation, Inc. 
@c See the file elisp.texi for copying conditions.
@setfilename ../info/debugging
@node Debugging, Read and Print, Advising Functions, Top
@c @chapter Debugging Lisp Programs
@chapter Lisp$B%W%m%0%i%`$N%G%P%C%0(B

@c   There are three ways to investigate a problem in an Emacs Lisp program,
@c depending on what you are doing with the program when the problem appears.
Emacs Lisp$B%W%m%0%i%`$NLdBjE@$rD4$Y$k$K$O!"(B
$BLdBj$,H/@8$7$?$H$-$K$I$N$h$&$K%W%m%0%i%`$r;H$C$F$$$k$+$K0MB8$7$F!"(B
3$B$D$NJ}K!$,$"$j$^$9!#(B

@itemize @bullet
@item
@c If the problem occurs when you run the program, you can use a Lisp
@c debugger to investigate what is happening during execution.  In addition
@c to the ordinary debugger, Emacs comes with a source level debugger,
@c Edebug.  This chapter describes both of them.
$B%W%m%0%i%`$r<B9T$7$?$H$-$KLdBj$,H/@8$7$?>l9g$K$O!"(B
$B<B9TCf$K2?$,5/$3$C$F$$$k$+$rD4$Y$k$?$a$K(BLisp$B%G%P%C%,$r;H$($k!#(B
$BDL>o$N%G%P%C%,$K2C$($F!"(BEmacs$B$K$O%=!<%9%l%Y%k%G%P%C%,(Bedebug$B$b$"$k!#(B
$BK\>O$G$ON><T$K$D$$$F=R$Y$k!#(B

@item
@c If the problem is syntactic, so that Lisp cannot even read the program,
@c you can use the Emacs facilities for editing Lisp to localize it.
$B9=J8E*$JLdBj$,$"$k$?$a$K(BLisp$B$,%W%m%0%i%`$rFI$_<h$l$J$$$H$-$K$O!"(B
Emacs$B$N(BLisp$BJT=85!G=$r;H$C$F$=$N>l=j$rFCDj$9$k!#(B

@item
@c If the problem occurs when trying to compile the program with the byte
@c compiler, you need to know how to examine the compiler's input buffer.
$B%W%m%0%i%`$r%P%$%H%3%s%Q%$%i$G%3%s%Q%$%k$9$k$H$-$KLdBj$,H/@8$9$k$H$-$K$O!"(B
$B%3%s%Q%$%i$NF~NO%P%C%U%!$ND4$YJ}$rCN$kI,MW$,$"$k!#(B
@end itemize

@menu
* Debugger::            How the Emacs Lisp debugger is implemented.
* Edebug::		A source-level Emacs Lisp debugger.
* Syntax Errors::       How to find syntax errors.
* Compilation Errors::  How to find errors that show up in byte compilation.
@end menu

@c   Another useful debugging tool is the dribble file.  When a dribble
@c file is open, Emacs copies all keyboard input characters to that file.
@c Afterward, you can examine the file to find out what input was used.
@c @xref{Terminal Input}.
$BB>$NM-MQ$J%G%P%C%0%D!<%k$O!"%I%j%V%k%U%!%$%k$G$9!#(B
$B%I%j%V%k%U%!%$%k$r%*!<%W%s$7$F$"$k$H!"(B
Emacs$B$O$9$Y$F$N%-!<%\!<%IF~NO$r$3$N%U%!%$%k$K%3%T!<$7$^$9!#(B
$B$"$H$G$3$N%U%!%$%k$rD4$Y$l$P!"$I$s$JF~NO$,$"$C$?$+$o$+$j$^$9!#(B
@xref{Terminal Input}$B!#(B

@c   For debugging problems in terminal descriptions, the
@c @code{open-termscript} function can be useful.  @xref{Terminal Output}.
$BC<Kv@_Dj$K4X$7$?LdBj$r2r7h$9$k$K$O!"(B
$B4X?t(B@code{open-termscript}$B$,M-MQ$G$9!#(B
@xref{Terminal Output}$B!#(B

@node Debugger
@c @section The Lisp Debugger
@section Lisp$B%G%P%C%,(B
@c @cindex debugger
@c @cindex Lisp debugger
@c @cindex break
@cindex $B%G%P%C%,(B
@cindex Lisp$B%G%P%C%,(B
@cindex $B%V%l!<%/(B

@c   The ordinary @dfn{Lisp debugger} provides the ability to suspend
@c evaluation of a form.  While evaluation is suspended (a state that is
@c commonly known as a @dfn{break}), you may examine the run time stack,
@c examine the values of local or global variables, or change those values.
@c Since a break is a recursive edit, all the usual editing facilities of
@c Emacs are available; you can even run programs that will enter the
@c debugger recursively.  @xref{Recursive Editing}.
$BDL>o$N(B@dfn{Lisp$B%G%P%C%,(B}$B$O!"%U%)!<%`$NI>2A$r0l;~Dd;_$9$k5!G=$rDs6!$7$^$9!#(B
$BI>2A$r0l;~Dd;_$7$F$$$k$"$$$@!J(B@dfn{$B%V%l!<%/(B}$B!J(Bbreak$B!K$H8F$P$l$k>uBV!K$O!"(B
$B<B9T;~%9%?%C%/$rD4$Y$?$j!"%m!<%+%k$d%0%m!<%P%kJQ?t$NCM$rD4$Y$?$j!"(B
$B$=$l$i$NCM$rJQ99$G$-$^$9!#(B
$B%V%l!<%/$O:F5"JT=8$J$N$G!"(BEmacs$B$NDL>o$NJT=85!G=$9$Y$F$r;H$($^$9!#(B
$B%G%P%C%,$r:F5"E*$K5/F0$9$k$h$&$J%W%m%0%i%`$r<B9T$9$k$3$H$5$($G$-$^$9!#(B
@xref{Recursive Editing}$B!#(B

@menu
* Error Debugging::       Entering the debugger when an error happens.
* Infinite Loops::	  Stopping and debugging a program that doesn't exit.
* Function Debugging::    Entering it when a certain function is called.
* Explicit Debug::        Entering it at a certain point in the program.
* Using Debugger::        What the debugger does; what you see while in it.
* Debugger Commands::     Commands used while in the debugger.
* Invoking the Debugger:: How to call the function @code{debug}.
* Internals of Debugger:: Subroutines of the debugger, and global variables.
@end menu

@node Error Debugging
@c @subsection Entering the Debugger on an Error
@subsection $B%(%i!<$K$h$k%G%P%C%,$N5/F0(B
@c @cindex error debugging
@c @cindex debugging errors
@cindex $B%(%i!<$N%G%P%C%0(B
@cindex $B%G%P%C%0!"%(%i!<(B

@c   The most important time to enter the debugger is when a Lisp error
@c happens.  This allows you to investigate the immediate causes of the
@c error.
$B%G%P%C%,$XF~$k$b$C$H$b=EMW$J;~4|$O!"(BLisp$B%(%i!<$,H/@8$7$?$H$-$G$9!#(B
$B$3$l$K$h$j!"%(%i!<$ND>@\860x$rD4$Y$k$3$H$,$G$-$^$9!#(B

@c   However, entry to the debugger is not a normal consequence of an
@c error.  Many commands frequently cause Lisp errors when invoked
@c inappropriately (such as @kbd{C-f} at the end of the buffer), and during
@c ordinary editing it would be very inconvenient to enter the debugger
@c each time this happens.  So if you want errors to enter the debugger, set
@c the variable @code{debug-on-error} to non-@code{nil}.  (The command
@c @code{toggle-debug-on-error} provides an easy way to do this.)
$B$7$+$7!"%G%P%C%,$KF~$k$N$O!"%(%i!<$NDL>o$N5"7k$G$O$"$j$^$;$s!#(B
$BB?$/$N%3%^%s%I$O(B
$B!J%P%C%U%!$NKvHx$G(B@kbd{C-f}$B$r;H$&$J$I$N!KITE,@Z$K5/F0$5$l$k$H(B
$B$7$P$7$P(BLisp$B%(%i!<$r@8$8$^$9$,!"(B
$BDL>o$NJT=8$G$O$=$N$?$S$K%G%P%C%,$KF~$C$?$N$G$O$H$F$bITJX$G$9!#(B
$B$=$N$?$a!"%(%i!<$K$h$C$F%G%P%C%,$KF~$j$?$$>l9g$K$O!"(B
$BJQ?t(B@code{debug-on-error}$B$K(B@code{nil}$B0J30$r@_Dj$7$^$9!#(B
$B!J%3%^%s%I(B@code{toggle-debug-on-error}$B$O$3$l$r4JC1$K9T$&!#!K(B

@defopt debug-on-error
@c This variable determines whether the debugger is called when an error is
@c signaled and not handled.  If @code{debug-on-error} is @code{t}, all
@c kinds of errors call the debugger (except those listed in
@c @code{debug-ignored-errors}).  If it is @code{nil}, none call the
@c debugger.
$B$3$NJQ?t$O!"%(%i!<$,DLCN$5$l=hM}$5$l$J$$$H$-$K(B
$B%G%P%C%,$r8F$S=P$9$+$I$&$+7hDj$9$k!#(B
@code{debug-on-error}$B$,(B@code{t}$B$G$"$k$H!"(B
$B$9$Y$F$N<oN`$N%(%i!<!J(B@code{debug-ignored-errors}$B$K;XDj$7$?$b$N$r=|$/!K(B
$B$O%G%P%C%,$r8F$S=P$9!#(B
@code{nil}$B$G$"$k$H%G%P%C%,$r8F$S=P$5$J$$!#(B

@c The value can also be a list of error conditions that should call the
@c debugger.  For example, if you set it to the list
@c @code{(void-variable)}, then only errors about a variable that has no
@c value invoke the debugger.
$B$=$NCM$O%G%P%C%,$r8F$S=P$9%(%i!<>r7o$N%j%9%H$G$b$h$$!#(B
$B$?$H$($P!"%j%9%H(B@code{(void-variable)}$B$K$9$k$H!"(B
$B!VCM$r;}$?$J$$JQ?t$K4X$7$?%(%i!<!W$N$_$,%G%P%C%,$r5/F0$9$k!#(B

@c When this variable is non-@code{nil}, Emacs does not create an error
@c handler around process filter functions and sentinels.  Therefore,
@c errors in these functions also invoke the debugger.  @xref{Processes}.
$B$3$NJQ?t$,(B@code{nil}$B0J30$G$"$k$H!"(B
Emacs$B$O%W%m%;%9$N%U%#%k%?4X?t$dHVJ<$KBP$7$F%(%i!<%O%s%I%i$r:n@.$7$J$$!#(B
$B$7$?$,$C$F!"$3$l$i$N4X?t$G$N%(%i!<$b%G%P%C%,$r5/F0$9$k!#(B
@pxref{Processes}$B!#(B
@end defopt

@defopt debug-ignored-errors
@c This variable specifies certain kinds of errors that should not enter
@c the debugger.  Its value is a list of error condition symbols and/or
@c regular expressions.  If the error has any of those condition symbols,
@c or if the error message matches any of the regular expressions, then
@c that error does not enter the debugger, regardless of the value of
@c @code{debug-on-error}.
$B$3$NJQ?t$O!"%G%P%C%,$KF~$i$J$$%(%i!<$N<oN`$r;XDj$9$k!#(B
$B$=$NCM$O%(%i!<>r7o%7%s%\%k$d@55,I=8=$N%j%9%H$G$"$k!#(B
$B%(%i!<$K$3$l$i$N>r7o%7%s%\%k$,4^$^$l$k$+(B
$B%(%i!<%a%C%;!<%8$,@55,I=8=$N(B1$B$D$K0lCW$9$k>l9g$K$O!"(B
@code{debug-on-error}$B$NCM$K4X$o$i$:(B
$BEv3:%(%i!<$G$O%G%P%C%,$KF~$i$J$$!#(B

@c The normal value of this variable lists several errors that happen often
@c during editing but rarely result from bugs in Lisp programs.  However,
@c ``rarely'' is not ``never''; if your program fails with an error that
@c matches this list, you will need to change this list in order to debug
@c the error.  The easiest way is usually to set
@c @code{debug-ignored-errors} to @code{nil}.
$B$3$NJQ?t$NDL>o$NCM$O!"(B
$BJT=8$G$O$7$P$7$PH/@8$9$k$,!"(BLisp$B%W%m%0%i%`$N%P%0$G$O$[$H$s$IH/@8$7$J$$$h$&$J(B
$B%(%i!<72$N%j%9%H$G$"$k!#(B
$B$7$+$7!"!X$[$H$s$I!Y$O!X$1$C$7$F!Y$G$O$J$$!#(B
$B$3$N%j%9%H$K0lCW$9$k$h$&$J%(%i!<$GFI<T$N%W%m%0%i%`$,<:GT$9$k>l9g!"(B
$B%(%i!<$r%G%P%C%0$9$k$K$O$3$N%j%9%H$rJQ99$9$kI,MW$,$"$k!#(B
$B$b$C$H$b4JC1$JJ}K!$O!"(B@code{debug-ignored-errors}$B$K(B@code{nil}$B$r(B
$B@_Dj$9$k$3$H$G$"$k!#(B
@end defopt

@defopt debug-on-signal
@c Normally, errors that are caught by @code{condition-case} never run the
@c debugger, even if @code{debug-on-error} is non-@code{nil}.  In other
@c words, @code{condition-case} gets a chance to handle the error before
@c the debugger gets a chance.
$BDL>o!"(B@code{condition-case}$B$GJaB*$7$?%(%i!<$O!"(B
$B$?$H$((B@code{debug-on-error}$B$,(B@code{nil}$B0J30$G$"$C$F$b!"(B
$B$1$C$7$F%G%P%C%,$r5/F0$7$J$$!#(B
$B$$$$$+$($l$P!"%G%P%C%,$r5/F0$9$k$^$($K!"(B
@code{condition-case}$B$O%(%i!<=hM}$N5!2q$rF@$k$N$G$"$k!#(B

@c If you set @code{debug-on-signal} to a non-@code{nil} value, then the
@c debugger gets the first chance at every error; an error will invoke the
@c debugger regardless of any @code{condition-case}, if it fits the
@c criteria specified by the values of @code{debug-on-error} and
@c @code{debug-ignored-errors}.
@code{debug-on-signal}$B$K(B@code{nil}$B0J30$NCM$r@_Dj$9$k$H!"(B
$B3F%(%i!<$4$H$K%G%P%C%,$,$^$:5!2q$rF@$k!#(B
@code{debug-on-error}$B$H(B@code{debug-ignored-errors}$B$NCM$G;XDj$5$l$k(B
$B>r7o$K0lCW$9$l$P!"(B@code{condition-case}$B$K4X$o$i$:(B
$B%(%i!<$O%G%P%C%,$r5/F0$9$k!#(B

@c @strong{Warning:} This variable is strong medicine!  Various parts of
@c Emacs handle errors in the normal course of affairs, and you may not
@c even realize that errors happen there.  If you set
@c @code{debug-on-signal} to a non-@code{nil} value, those errors will
@c enter the debugger.
@strong{$B7Y9p!'(B}@code{ }
$B$3$NJQ?t$O6/NO$J=hJ}$G$"$k!*(B@code{ }
Emacs$B$N$5$^$6$^$JItJ,$G$ODL>o$NF0:n$H$7$F%(%i!<$r=hM}$7!"(B
$BFI<T$K$O%(%i!<$,H/@8$7$?$3$H$5$($o$+$i$J$$!#(B
@code{debug-on-signal}$B$K(B@code{nil}$B0J30$NCM$r@_Dj$9$k$H(B
$B$=$l$i$N%(%i!<$G%G%P%C%,$KF~$k!#(B

@c @strong{Warning:} @code{debug-on-signal} has no effect when
@c @code{debug-on-error} is @code{nil}.
@strong{$B7Y9p!'(B}@code{ }
@code{debug-on-error}$B$,(B@code{nil}$B$G$"$k$H!"(B
@code{debug-on-signal}$B$O0UL#$r;}$?$J$$!#(B
@end defopt

@c   To debug an error that happens during loading of the @file{.emacs}
@c file, use the option @samp{--debug-init}, which binds
@c @code{debug-on-error} to @code{t} while loading @file{.emacs}, and
@c bypasses the @code{condition-case} which normally catches errors in the
@c init file.
$B%U%!%$%k(B@file{.emacs}$B$r%m!<%ICf$KH/@8$9$k%(%i!<$r%G%P%C%0$9$k$K$O!"(B
$B%*%W%7%g%s(B@samp{--debug-init}$B$r;H$$$^$9!#(B
$B$3$l$K$h$j!"(B@file{.emacs}$B$N%m!<%ICf$O(B@code{debug-on-error}$B$r(B@code{t}$B$KB+G{$7!"(B
$B=i4|2=%U%!%$%k$G$N%(%i!<$rJaB*$9$k(B@code{condition-case}$B$r1*2s$7$^$9!#(B

@c   If your @file{.emacs} file sets @code{debug-on-error}, the effect may
@c not last past the end of loading @file{.emacs}.  (This is an undesirable
@c byproduct of the code that implements the @samp{--debug-init} command
@c line option.)  The best way to make @file{.emacs} set
@c @code{debug-on-error} permanently is with @code{after-init-hook}, like
@c this:
$BFI<T$N%U%!%$%k(B@file{.emacs}$B$G(B@code{debug-on-error}$B$r@_Dj$7$F$b!"(B
$B$=$N8z2L$O(B@file{.emacs}$B$N%m!<%I$r=*$k$H;}B3$7$^$;$s!#(B
$B!J$3$l$O%3%^%s%I9T%*%W%7%g%s(B@samp{--debug-init}$B$N<BAu$K$*$1$k(B
$B9%$^$7$/$J$$FC@-$G$"$k!#!K(B
@file{.emacs}$B$G(B@code{debug-on-error}$B$r915WE*$K@_Dj$9$k:GNI$NJ}K!$O!"(B
$B$D$.$N$h$&$K!"(B@code{after-init-hook}$B$rMQ$$$k$3$H$G$9!#(B

@example
(add-hook 'after-init-hook
          '(lambda () (setq debug-on-error t)))
@end example

@node Infinite Loops
@c @subsection Debugging Infinite Loops
@subsection $BL58B%k!<%W$N%G%P%C%0(B
@c @cindex infinite loops
@c @cindex loops, infinite
@c @cindex quitting from infinite loop
@c @cindex stopping an infinite loop
@cindex $BL58B%k!<%W(B
@cindex $B%k!<%W!"L58B(B
@cindex $BL58B%k!<%W$NCfCG(B
@cindex $BCfCG!"L58B%k!<%W(B
@cindex $BL58B%k!<%W$NDd;_(B
@cindex $BDd;_!"L58B%k!<%W(B

@c   When a program loops infinitely and fails to return, your first
@c problem is to stop the loop.  On most operating systems, you can do this
@c with @kbd{C-g}, which causes quit.
$B%W%m%0%i%`$,L58B$K%k!<%W$7La$C$F$3$J$$$H$-$K$O!"(B
$B$^$:!"%k!<%W$rDd;_$9$kI,MW$,$"$j$^$9!#(B
$B$[$H$s$I$N%*%Z%l!<%F%#%s%0%7%9%F%`$G$O!"(B
$BCfCG$r0UL#$9$k(B@kbd{C-g}$B$r;H$$$^$9!#(B

@c   Ordinary quitting gives no information about why the program was
@c looping.  To get more information, you can set the variable
@c @code{debug-on-quit} to non-@code{nil}.  Quitting with @kbd{C-g} is not
@c considered an error, and @code{debug-on-error} has no effect on the
@c handling of @kbd{C-g}.  Likewise, @code{debug-on-quit} has no effect on
@c errors.
$BIaDL$KCfCG$7$?$N$G$O!"(B
$B%W%m%0%i%`$,L58B%k!<%W$7$?M}M3$K4X$9$k>pJs$OF@$i$l$^$;$s!#(B
$B$h$j>\$7$$>pJs$rF@$k$K$O!"(B
$BJQ?t(B@code{debug-on-quit}$B$K(B@code{nil}$B0J30$r@_Dj$7$^$9!#(B
@kbd{C-g}$B$K$h$kCfCG$O%(%i!<$H$O07$o$J$$$?$a!"(B
@kbd{C-g}$B$N=hM}$K4X$7$F(B@code{debug-on-error}$B$O$J$s$N8z2L$b$"$j$^$;$s!#(B
$BF1MM$K!"(B@code{debug-on-quit}$B$O%(%i!<$K4X$7$F$J$s$N8z2L$b$"$j$^$;$s!#(B

@c   Once you have the debugger running in the middle of the infinite loop,
@c you can proceed from the debugger using the stepping commands.  If you
@c step through the entire loop, you will probably get enough information
@c to solve the problem.
$BL58B%k!<%W$NESCf$G%G%P%C%,$r5/F0$G$-$l$P!"(B
$B%G%P%C%,$G%9%F%C%W<B9T%3%^%s%I$r;H$C$F@h$X?J$a$^$9!#(B
$B%k!<%W$R$H$^$o$j$r%9%F%C%W<B9T$9$l$P!"(B
$BLdBj$r2r7h$9$k$K==J,$J>pJs$rF@$i$l$k$O$:$G$9!#(B

@defopt debug-on-quit
@c This variable determines whether the debugger is called when @code{quit}
@c is signaled and not handled.  If @code{debug-on-quit} is non-@code{nil},
@c then the debugger is called whenever you quit (that is, type @kbd{C-g}).
@c If @code{debug-on-quit} is @code{nil}, then the debugger is not called
@c when you quit.  @xref{Quitting}.
$B$3$NJQ?t$O!"(B@code{quit}$B$,DLCN$5$l=hM}$5$l$J$+$C$?>l9g$K!"(B
$B%G%P%C%,$r8F$S=P$9$+$I$&$+$r7hDj$9$k!#(B
@code{debug-on-quit}$B$,(B@code{nil}$B0J30$G$"$k>l9g!"(B
$B!J(B@kbd{C-g}$B$rBG$C$F!KCfCG$9$k$H%G%P%C%,$r8F$S=P$9!#(B
@code{debug-on-quit}$B$,(B@code{nil}$B$G$"$k$H!"(B
$BCfCG$7$F$b%G%P%C%,$r8F$S=P$5$J$$!#(B
@pxref{Quitting}$B!#(B
@end defopt

@node Function Debugging
@c @subsection Entering the Debugger on a Function Call
@subsection $B4X?t8F$S=P$7;~$N%G%P%C%,$N5/F0(B
@c @cindex function call debugging
@c @cindex debugging specific functions
@cindex $B4X?t8F$S=P$7$N%G%P%C%0(B
@cindex $B%G%P%C%0!"4X?t8F$S=P$7(B
@cindex $BFCDj$N4X?t$N%G%P%C%0(B
@cindex $B%G%P%C%0!"FCDj$N4X?t(B

@c   To investigate a problem that happens in the middle of a program, one
@c useful technique is to enter the debugger whenever a certain function is
@c called.  You can do this to the function in which the problem occurs,
@c and then step through the function, or you can do this to a function
@c called shortly before the problem, step quickly over the call to that
@c function, and then step through its caller.
$B%W%m%0%i%`$NESCf$GH/@8$9$kLdBjE@$rD4$Y$k$?$a$N(B1$B$D$NM-MQ$J5;K!$O!"(B
$B$"$k4X?t$r8F$S=P$9$?$S$K%G%P%C%,$KF~$k$3$H$G$9!#(B
$BLdBj$r@8$8$k4X?t$KBP$7$F$3$N$h$&$K$7$F$*$-!"(B
$BEv3:4X?t$r%9%F%C%W<B9T$9$k$+!"$"$k$$$O!"(B
$BLdBj$,H/@8$9$kD>A0$K8F$P$l$k4X?t$KBP$7$F$3$N$h$&$K$7$F$*$-!"(B
$B$=$N4X?t$N8F$S=P$7$r=*$($F$+$i!"8F$S=P$7B&$r%9%F%C%W<B9T$7$^$9!#(B

@c @deffn Command debug-on-entry function-name
@deffn $B%3%^%s%I(B debug-on-entry function-name
@c This function requests @var{function-name} to invoke the debugger each time
@c it is called.  It works by inserting the form @code{(debug 'debug)} into
@c the function definition as the first form.
$B$3$N4X?t$O!"(B@var{function-name}$B$,8F$S=P$5$l$k$?$S$K(B
$B%G%P%C%,$r5/F0$9$k$h$&$K$9$k!#(B
$BEv3:4X?t$NDj5A$N:G=i$N%U%)!<%`$H$7$F(B
$B%U%)!<%`(B@code{(debug 'debug)}$B$rA^F~$9$k$3$H$G$3$l$r9T$&!#(B

@c Any function defined as Lisp code may be set to break on entry,
@c regardless of whether it is interpreted code or compiled code.  If the
@c function is a command, it will enter the debugger when called from Lisp
@c and when called interactively (after the reading of the arguments).  You
@c can't debug primitive functions (i.e., those written in C) this way.
Lisp$B%3!<%I$GDj5A$7$?G$0U$N4X?t$O!"(B
$B2r<a<B9T%3!<%I$G$"$m$&$H%3%s%Q%$%k:Q$_$N%3!<%I$G$"$m$&$H!"(B
$B4X?t$KF~$k$H$-$K%V%l!<%/$9$k$h$&$K$G$-$k!#(B
$B4X?t$,%3%^%s%I$G$"$k$H!"(BLisp$B$+$i8F$P$l$?$H$-$d(B
$BBPOCE*$K8F$P$l$?$H$-$K!J0z?t$rFI$_<h$C$F$+$i!K%G%P%C%,$KF~$k!#(B
$B!J(BC$B$G=q$$$?!K4pK\4X?t$O!"$3$NJ}K!$G$O%G%P%C%0$G$-$J$$!#(B

@c When @code{debug-on-entry} is called interactively, it prompts for
@c @var{function-name} in the minibuffer.  If the function is already set
@c up to invoke the debugger on entry, @code{debug-on-entry} does nothing.
@c @code{debug-on-entry} always returns @var{function-name}.
@code{debug-on-entry}$B$rBPOCE*$K8F$S=P$9$H!"(B
$B%_%K%P%C%U%!$G(B@var{function-name}$B$rLd$$9g$o$;$k!#(B
$B$=$N4X?t$,$9$G$K8F$S=P$7;~$K%G%P%C%,$r5/F0$9$k$h$&$K$J$C$F$$$k$H!"(B
@code{debug-on-entry}$B$O$J$K$b$7$J$$!#(B
@code{debug-on-entry}$B$O$D$M$K(B@var{function-name}$B$rJV$9!#(B

@c @strong{Note:} if you redefine a function after using
@c @code{debug-on-entry} on it, the code to enter the debugger is discarded
@c by the redefinition.  In effect, redefining the function cancels
@c the break-on-entry feature for that function.
@strong{$BCm0U!'(B}@code{ }
@code{debug-on-entry}$B$r;H$C$?$"$H$KEv3:4X?t$r:FDj5A$9$k$H!"(B
$B%G%P%C%,$KF~$k$?$a$N%3!<%I$,$J$/$J$k!#(B
$B<B<AE*$K$O!"4X?t$r:FDj5A$9$k$H8F$S=P$7;~$K%V%l!<%/$9$k5!G=$r(B
$B<h$j>C$9$3$H$K$J$k!#(B

@example
@group
(defun fact (n)
  (if (zerop n) 1
      (* n (fact (1- n)))))
     @result{} fact
@end group
@group
(debug-on-entry 'fact)
     @result{} fact
@end group
@group
(fact 3)
@end group

@group
------ Buffer: *Backtrace* ------
Entering:
* fact(3)
  eval-region(4870 4878 t)
  byte-code("...")
  eval-last-sexp(nil)
  (let ...)
  eval-insert-last-sexp(nil)
* call-interactively(eval-insert-last-sexp)
------ Buffer: *Backtrace* ------
@end group

@group
(symbol-function 'fact)
     @result{} (lambda (n)
          (debug (quote debug))
          (if (zerop n) 1 (* n (fact (1- n)))))
@end group
@end example
@end deffn

@c @deffn Command cancel-debug-on-entry function-name
@deffn $B%3%^%s%I(B cancel-debug-on-entry function-name
@c This function undoes the effect of @code{debug-on-entry} on
@c @var{function-name}.  When called interactively, it prompts for
@c @var{function-name} in the minibuffer.  If @var{function-name} is
@c @code{nil} or the empty string, it cancels break-on-entry for all
@c functions.
$B$3$N4X?t$O!"(B@var{function-name}$B$KBP$9$k(B@code{debug-on-entry}$B$N8z2L(B
$B!J8F$S=P$7;~$K%V%l!<%/$9$k!K$r<h$j>C$9!#(B
$BBPOCE*$K8F$S=P$9$H!"(B
$B%_%K%P%C%U%!$G(B@var{function-name}$B$rLd$$9g$o$;$k!#(B
@var{function-name}$B$,(B@code{nil}$B$G$"$C$?$j6uJ8;zNs$G$"$k$H!"(B
$B$9$Y$F$N4X?t$K$D$$$F!"8F$S=P$7;~$K%V%l!<%/$9$k$3$H$r<h$j>C$9!#(B

@c Calling @code{cancel-debug-on-entry} does nothing to a function which is
@c not currently set up to break on entry.  It always returns
@c @var{function-name}.
$B8F$S=P$7;~$K%V%l!<%/$9$k@_Dj$r$7$F$$$J$$4X?t$KBP$7$F(B
@code{cancel-debug-on-entry}$B$r8F$S=P$7$F$b$J$K$b$7$J$$!#(B
$B$D$M$K(B@var{function-name}$B$rJV$9!#(B
@end deffn

@node Explicit Debug
@c @subsection Explicit Entry to the Debugger
@subsection $B%G%P%C%,$NL@<(E*$J5/F0(B

@c   You can cause the debugger to be called at a certain point in your
@c program by writing the expression @code{(debug)} at that point.  To do
@c this, visit the source file, insert the text @samp{(debug)} at the
@c proper place, and type @kbd{C-M-x}.  @strong{Warning:} if you do this
@c for temporary debugging purposes, be sure to undo this insertion before
@c you save the file!
$BFI<T$N%W%m%0%i%`$K<0(B@code{(debug)}$B$r=q$/$H!"(B
$B$=$N2U=j$G%G%P%C%,$r8F$S=P$9$3$H$,$G$-$^$9!#(B
$B$D$^$j!"%=!<%9%U%!%$%k$rK,Ld$7$FE,Ev$J2U=j$K%F%-%9%H(B@samp{(debug)}$B$rA^F~$7!"(B
@kbd{C-M-x}$B$HBG$A$^$9!#(B
@strong{$B7Y9p!'(B}@code{ }
$B0l;~E*$J%G%P%C%0L\E*$G$3$l$r9T$&>l9g$K$O!"(B
$B%U%!%$%k$rJ]B8$9$k$^$($K$3$NA^F~2U=j$r$b$H$KLa$9$3$H!*(B

@c   The place where you insert @samp{(debug)} must be a place where an
@c additional form can be evaluated and its value ignored.  (If the value
@c of @code{(debug)} isn't ignored, it will alter the execution of the
@c program!)  The most common suitable places are inside a @code{progn} or
@c an implicit @code{progn} (@pxref{Sequencing}).
@samp{(debug)}$B$rA^F~$9$k2U=j$O!"(B
$BM>J,$J%U%)!<%`$rI>2A$7$F$b$=$NCM$rL5;k$G$-$k>l=j$G$J$1$l$P$J$j$^$;$s!#(B
$B!J(B@code{(debug)}$B$NCM$,L5;k$5$l$J$$$H!"(B
$B%W%m%0%i%`$N<B9T$rJQ$($F$7$^$&!*!K(B
$B$b$C$H$bE,$7$?0lHLE*$J>l=j$O(B@code{progn}$B$d0EL[$N(B@code{progn}$B$NFbB&$G$9(B
$B!J(B@pxref{Sequencing}$B!K!#(B

@node Using Debugger
@c @subsection Using the Debugger
@subsection $B%G%P%C%,$N;H$$J}(B

@c   When the debugger is entered, it displays the previously selected
@c buffer in one window and a buffer named @samp{*Backtrace*} in another
@c window.  The backtrace buffer contains one line for each level of Lisp
@c function execution currently going on.  At the beginning of this buffer
@c is a message describing the reason that the debugger was invoked (such
@c as the error message and associated data, if it was invoked due to an
@c error).
$B%G%P%C%,$KF~$k$H!"$=$l$^$GA*Br$7$F$$$?%P%C%U%!$r$"$k%&%#%s%I%&$K!"(B
@samp{*Backtrace*}$B$H$$$&L>A0$N%P%C%U%!$rJL$N%&%#%s%I%&$KI=<($7$^$9!#(B
$B%P%C%/%H%l!<%9%P%C%U%!$G$O!"3F9T$O8=:_<B9TCf$N(BLisp$B4X?t$N3F%l%Y%k$G$9!#(B
$B$3$N%P%C%U%!$N@hF,$K$O!"%G%P%C%,$r5/F0$9$k$K;j$C$?M}M3(B
$B!J%(%i!<$G5/F0$5$l$?$H$-$K$O%(%i!<%a%C%;!<%8$H4XO"%G!<%?!K(B
$B$rI=$9%a%C%;!<%8$,$"$j$^$9!#(B

@c   The backtrace buffer is read-only and uses a special major mode,
@c Debugger mode, in which letters are defined as debugger commands.  The
@c usual Emacs editing commands are available; thus, you can switch windows
@c to examine the buffer that was being edited at the time of the error,
@c switch buffers, visit files, or do any other sort of editing.  However,
@c the debugger is a recursive editing level (@pxref{Recursive Editing})
@c and it is wise to go back to the backtrace buffer and exit the debugger
@c (with the @kbd{q} command) when you are finished with it.  Exiting
@c the debugger gets out of the recursive edit and kills the backtrace
@c buffer.
$B%P%C%/%H%l!<%9%P%C%U%!$OFI$_=P$7@lMQ$G$"$j!"(B
$B3FJ8;z$r%G%P%C%,%3%^%s%I$G$"$k$HDj5A$7$?(B
$BFCJL$J%a%8%c!<%b!<%I!"(Bdebugger$B%b!<%I$r;H$$$^$9!#(B
Emacs$B$NDL>o$NJT=8%3%^%s%I$b;H$($^$9!#(B
$B$7$?$,$C$F!"%(%i!<H/@8;~$KJT=8$7$F$$$?%P%C%U%!$rD4$Y$k$?$a$K%&%#%s%I%&$r(B
$B@Z$jBX$($?$j!"%P%C%U%!$r@Z$jBX$($?$j!"%U%!%$%k$rK,$l$?$j!"(B
$B$=$NB>$N$I$s$JJT=8$G$b$G$-$^$9!#(B
$B$7$+$7!"%G%P%C%,$O:F5"JT=8%l%Y%k!J(B@pxref{Recursive Editing}$B!K$G$"$k$N$G!"(B
$B%G%P%C%0$r=*$($k$H$-$K$O!"%P%C%/%H%l!<%9%P%C%U%!$KLa$C$F$+$i(B
$B%G%P%C%,$r!J%3%^%s%I(B@kbd{q}$B$G!K=*N;$9$k$N$,8-$$J}K!$G$9!#(B
$B%G%P%C%,$r=*N;$9$k$H!":F5"JT=8$+$iH4$1%P%C%/%H%l!<%9%P%C%U%!$r:o=|$7$^$9!#(B

@c @cindex current stack frame
@cindex $B%+%l%s%H%9%?%C%/%U%l!<%`(B
@c   The backtrace buffer shows you the functions that are executing and
@c their argument values.  It also allows you to specify a stack frame by
@c moving point to the line describing that frame.  (A stack frame is the
@c place where the Lisp interpreter records information about a particular
@c invocation of a function.)  The frame whose line point is on is
@c considered the @dfn{current frame}.  Some of the debugger commands
@c operate on the current frame.
$B%P%C%/%H%l!<%9%P%C%U%!$G$O!"<B9TCf$N4X?t$H$=$N0z?t$NCM$rI=<($7$^$9!#(B
$B$^$?!"%9%?%C%/%U%l!<%`$r5-=R$9$k9T$X%]%$%s%H$r0\F0$9$k$3$H$G(B
$B%9%?%C%/%U%l!<%`$r;XDj$G$-$^$9!#(B
$B!J%9%?%C%/%U%l!<%`$H$O!"(BLisp$B%$%s%?!<%W%j%?$,4X?t$N5/F0$K4X$9$k>pJs$r(B
$B5-O?$7$F$*$/>l=j$G$"$k!#!K(B
$B%]%$%s%H$,$"$k9T$KBP1~$9$k%U%l!<%`$r(B@dfn{$B%+%l%s%H%U%l!<%`(B}$B!J(Bcurrent frame$B!K$H(B
$B8F$S$^$9!#(B
$B%G%P%C%,$N$"$k<o$N%3%^%s%I$O%+%l%s%H%U%l!<%`$K:nMQ$7$^$9!#(B

@c   The debugger itself must be run byte-compiled, since it makes
@c assumptions about how many stack frames are used for the debugger
@c itself.  These assumptions are false if the debugger is running
@c interpreted.
$B%G%P%C%,<+?H$O%P%$%H%3%s%Q%$%k$7$?$b$N$r<B9T$9$kI,MW$,$"$j$^$9!#(B
$B$H$$$&$N$O!"%G%P%C%,<+?H$,;HMQ$9$k%9%?%C%/%U%l!<%`$N%5%$%:$r(B
$B2>Dj$7$F$$$k$+$i$G$9!#(B
$B2r<a<B9T$@$H$3$N2>Dj$,@.$jN)$A$^$;$s!#(B

@need 3000

@node Debugger Commands
@c @subsection Debugger Commands
@subsection $B%G%P%C%,%3%^%s%I(B
@c @cindex debugger command list
@cindex $B%G%P%C%,%3%^%s%I0lMw(B

@c   Inside the debugger (in Debugger mode), these special commands are
@c available in addition to the usual cursor motion commands.  (Keep in
@c mind that all the usual facilities of Emacs, such as switching windows
@c or buffers, are still available.)
$B%G%P%C%,Fb!J(Bdebugger$B%b!<%I!K$G$O!"(B
$BDL>o$N%+!<%=%k0\F0%3%^%s%I$K2C$($F0J2<$NFCJL$J%3%^%s%I$r;H$($^$9!#(B
$B!J%&%#%s%I%&$d%P%C%U%!$N@Z$jBX$($J$I$N(BEmacs$B$NDL>o$N5!G=$b;H$($k$3$H$KN10U!#!K(B

@c   The most important use of debugger commands is for stepping through
@c code, so that you can see how control flows.  The debugger can step
@c through the control structures of an interpreted function, but cannot do
@c so in a byte-compiled function.  If you would like to step through a
@c byte-compiled function, replace it with an interpreted definition of the
@c same function.  (To do this, visit the source for the function and type
@c @kbd{C-M-x} on its definition.)
$B%G%P%C%,%3%^%s%I$N$b$C$H$b=EMW$J;H$$J}$O%9%F%C%W<B9T$G$"$j!"(B
$B$3$l$K$h$j@)8f$NN.$l$rD4$Y$k$3$H$G$9!#(B
$B%G%P%C%,$O!"2r<a<B9THG$N4X?t$N@)8f9=B$$r%9%F%C%W<B9T$G$-$^$9$,!"(B
$B%P%$%H%3%s%Q%$%k$7$?4X?t$G$O$G$-$^$;$s!#(B
$B%P%$%H%3%s%Q%$%k$7$?4X?t$r%9%F%C%W<B9T$7$?$$>l9g$K$O!"(B
$BF1$84X?t$r2r<a<B9THG$NDj5A$KCV$-49$($kI,MW$,$"$j$^$9!#(B
$B!J$3$l$K$O!"4X?t$N%=!<%9$rK,$l$F!"$=$NDj5AFb$G(B@kbd{C-M-x}$B$HBG$D!#!K(B

@c   Here is a list of Debugger mode commands:
debugger$B%b!<%I$N%3%^%s%I0lMw$r0J2<$K<($7$^$9!#(B

@table @kbd
@item c
@c Exit the debugger and continue execution.  When continuing is possible,
@c it resumes execution of the program as if the debugger had never been
@c entered (aside from any side-effects that you caused by changing
@c variable values or data structures while inside the debugger).
$B%G%P%C%,$r=*N;$7<B9T$r7QB3$9$k!#(B
$B7QB32DG=$G$"$l$P!"(B
$B!J%G%P%C%,Fb$G9T$C$?JQ?tCM$d%G!<%?9=B$$KBP$9$kJQ99$J$I$NI{:nMQ$r=|$$$F!K(B
$B%G%P%C%,$r5/F0$7$J$+$C$?$+$N$h$&$K%W%m%0%i%`$N<B9T$r:F3+$9$k!#(B

@c Continuing is possible after entry to the debugger due to function entry
@c or exit, explicit invocation, or quitting.  You cannot continue if the
@c debugger was entered because of an error.
$B7QB3$,2DG=$J$N$O!"(B
$B4X?t8F$S=P$7;~$d=*N;;~!"L@<(E*$J5/F0!"CfCG$K$h$j%G%P%C%,$KF~$C$?>l9g$G$"$k!#(B
$B%(%i!<$,860x$G%G%P%C%,$,5/F0$5$l$?$H$-$K$O7QB3$G$-$J$$!#(B

@item d
@c Continue execution, but enter the debugger the next time any Lisp
@c function is called.  This allows you to step through the
@c subexpressions of an expression, seeing what values the subexpressions
@c compute, and what else they do.
$B<B9T$r7QB3$9$k$,!"G$0U$N(BLisp$B4X?t$r8F$S=P$9$H%G%P%C%,$KF~$k!#(B
$B$3$l$K$h$j!"<0$NItJ,<0$r%9%F%C%W<B9T$7$F(B
$BItJ,<0$,7W;;$9$kCM$d$=$NF0:n$rD4$Y$k$3$H$,$G$-$k!#(B

@c The stack frame made for the function call which enters the debugger in
@c this way will be flagged automatically so that the debugger will be
@c called again when the frame is exited.  You can use the @kbd{u} command
@c to cancel this flag.
$B$3$N$h$&$K$7$F%G%P%C%,$r5/F0$7$?4X?t8F$S=P$7$N%9%?%C%/%U%l!<%`$K$O(B
$B<+F0E*$K0u$,IU$-!"$=$N%9%?%C%/$+$iH4$1$k$H%G%P%C%,$,$U$?$?$S8F$S=P$5$l$k!#(B
$B$3$N0u$r>C$9$K$O%3%^%s%I(B@kbd{u}$B$r;H$&!#(B

@item b
@c Flag the current frame so that the debugger will be entered when the
@c frame is exited.  Frames flagged in this way are marked with stars
@c in the backtrace buffer.
$B%U%l!<%`$+$iH4$1$k$H%G%P%C%,$KF~$k$h$&$K%+%l%s%H%U%l!<%`$K0u$rIU$1$k!#(B
$B$3$N$h$&$K0u$rIU$1$?%U%l!<%`$K$O!"%P%C%/%H%l!<%9%P%C%U%!$G$O@10u$,IU$/!#(B

@item u
@c Don't enter the debugger when the current frame is exited.  This
@c cancels a @kbd{b} command on that frame.  The visible effect is to
@c remove the star from the line in the backtrace buffer.
$B%+%l%s%H%U%l!<%`$+$iH4$1$k$H$-$K%G%P%C%,$KF~$i$J$$!#(B
$B$3$l$O!"Ev3:%U%l!<%`$KBP$9$k%3%^%s%I(B@kbd{b}$B$r<h$j>C$9!#(B
$B;k3PE*$K$O%P%C%/%H%l!<%9%P%C%U%!$NEv3:9T$+$i@10u$,<h$i$l$k!#(B

@item e
@c Read a Lisp expression in the minibuffer, evaluate it, and print the
@c value in the echo area.  The debugger alters certain important
@c variables, and the current buffer, as part of its operation; @kbd{e}
@c temporarily restores their values from outside the debugger, so you can
@c examine and change them.  This makes the debugger more transparent.  By
@c contrast, @kbd{M-:} does nothing special in the debugger; it shows you
@c the variable values within the debugger.
$B%_%K%P%C%U%!$G(BLisp$B<0$rFI$_<h$j!"$=$l$rI>2A$7!"$=$NCM$r%(%3!<NN0h$KI=<($9$k!#(B
$B$3$NA`:n$N0l4D$H$7$F!"%G%P%C%,$O=EMW$J$"$k<o$NJQ?t$d(B
$B%+%l%s%H%P%C%U%!$rJQ99$9$k!#(B
@kbd{e}$B$O$=$l$i$NCM$r%G%P%C%,$N30B&$NCM$K0l;~E*$KI|85$9$k$N$G!"(B
$B$=$l$i$rD4$Y$?$jJQ99$7$?$j$G$-$k!#(B
$B$3$l$K$h$j%G%P%C%,$OF)2aE*$K$J$k!#(B
$BBP>HE*$K!"(B@kbd{M-:}$B$O%G%P%C%,Fb$GFCJL$J$3$H$O9T$o$J$$!#(B
$B%G%P%C%,Fb$G$NJQ?tCM$rI=<($9$k!#(B

@item R
@c Like @kbd{e}, but also save the result of evaluation in the
@c buffer @samp{*Debugger-record*}.
@kbd{e}$B$HF1MM$G$"$k$,!"(B
$B%P%C%U%!(B@samp{*Debugger-record*}$B$G$NI>2A7k2L$bJ]B8$9$k!#(B

@item q
@c Terminate the program being debugged; return to top-level Emacs
@c command execution.
$B%G%P%C%0Cf$N%W%m%0%i%`$r=*N;$9$k!#(B
Emacs$B$N%H%C%W%l%Y%k$N%3%^%s%I<B9T$XLa$k!#(B

@c If the debugger was entered due to a @kbd{C-g} but you really want
@c to quit, and not debug, use the @kbd{q} command.
@kbd{C-g}$B$G%G%P%C%,$XF~$C$?$,!"(B
$B<B:]$K$OCfCG$7$?$$$N$G$"$C$F%G%P%C%0$O$7$?$/$J$$>l9g$K$O(B
$B%3%^%s%I(B@kbd{q}$B$r;H$&!#(B

@item r
@c Return a value from the debugger.  The value is computed by reading an
@c expression with the minibuffer and evaluating it.
$B%G%P%C%,$+$iCM$r;XDj$7$FLa$k!#(B
$B$=$NCM$O!"%_%K%P%C%U%!$G<0$rFI$_<h$j!"$=$l$rI>2A$7$FF@$k!#(B

@c The @kbd{r} command is useful when the debugger was invoked due to exit
@c from a Lisp call frame (as requested with @kbd{b} or by entering the
@c frame with @kbd{d}); then the value specified in the @kbd{r} command is
@c used as the value of that frame.  It is also useful if you call
@c @code{debug} and use its return value.  Otherwise, @kbd{r} has the same
@c effect as @kbd{c}, and the specified return value does not matter.
$B!J(B@kbd{b}$B$G;XDj$7$?$j(B@kbd{d}$B$G%U%l!<%`$KF~$k$3$H$G!K(B
Lisp$B$N8F$S=P$7%U%l!<%`$+$iH4$1$G$?$?$a$K%G%P%C%,$,5/F0$5$l$?>l9g$K!"(B
$B%3%^%s%I(B@kbd{r}$B$OM-MQ$G$"$k!#(B
$B%3%^%s%I(B@kbd{r}$B$G;XDj$7$?CM$O!"Ev3:%U%l!<%`$NCM$H$7$F;H$o$l$k!#(B
$B$3$N%3%^%s%I$O!"(B@code{debug}$B$r8F$S=P$7$F$=$NLa$jCM$r;H$&>l9g$K$bM-MQ$G$"$k!#(B
$B$5$b$J$1$l$P!"(B@kbd{r}$B$O(B@kbd{c}$B$HF1$88z2L$G$"$j!";XDj$7$?La$jCM$O4X78$J$$!#(B

@c You can't use @kbd{r} when the debugger was entered due to an error.
$B%(%i!<$G%G%P%C%,$KF~$C$?>l9g$K$O(B@kbd{r}$B$O;H$($J$$!#(B
@end table

@node Invoking the Debugger
@c @subsection Invoking the Debugger
@subsection $B%G%P%C%,$N5/F0(B

@c   Here we describe in full detail the function @code{debug} that is used
@c to invoke the debugger.
$B$3$3$G$O!"%G%P%C%,$r5/F0$9$k$?$a$K;H$o$l$k4X?t(B@code{debug}$B$N>\:Y$r=R$Y$^$9!#(B

@defun debug &rest debugger-args
@c This function enters the debugger.  It switches buffers to a buffer
@c named @samp{*Backtrace*} (or @samp{*Backtrace*<2>} if it is the second
@c recursive entry to the debugger, etc.), and fills it with information
@c about the stack of Lisp function calls.  It then enters a recursive
@c edit, showing the backtrace buffer in Debugger mode.
$B$3$N4X?t$O%G%P%C%,$KF~$k!#(B
@samp{*Backtrace*}$B!J$"$k$$$O%G%P%C%,$NBh(B2$B%l%Y%k$K:F5"E*$KF~$k$H(B
@samp{*Backtrace*<2>}$B$J$I!K$H$$$&L>A0$N%P%C%U%!$K@Z$jBX$(!"(B
Lisp$B4X?t$N8F$S=P$7%9%?%C%/$K4X$9$k>pJs$G$3$N%P%C%U%!$rK~$?$9!#(B
$B$=$7$F:F5"JT=8$KF~$j(Bdebugger$B%b!<%I$N%P%C%/%H%l!<%9%P%C%U%!$rI=<($9$k!#(B

@c The Debugger mode @kbd{c} and @kbd{r} commands exit the recursive edit;
@c then @code{debug} switches back to the previous buffer and returns to
@c whatever called @code{debug}.  This is the only way the function
@c @code{debug} can return to its caller.
debugger$B%b!<%I$N%3%^%s%I(B@kbd{c}$B$d(B@kbd{r}$B$G:F5"JT=8$+$iH4$1$@$7!"(B
@code{debug}$B$O$=$l0JA0$N%P%C%U%!$K@Z$jBX$((B
@code{debug}$B$r8F$S=P$7$?$H$3$m$XLa$k!#(B
$B$3$l$O!"4X?t(B@code{debug}$B$,8F$S=P$7B&$XLa$kM#0l$N<jCJ$G$"$k!#(B

@c The use of the @var{debugger-args} is that @code{debug} displays the
@c rest of its arguments at the top of the @samp{*Backtrace*} buffer, so
@c that the user can see them.  Except as described below, this is the
@c @emph{only} way these arguments are used.
@var{debugger-args}$B$N;H$$ES$O!"(B
@code{debug}$B$,0z?t$N;D$j$r%P%C%U%!(B@samp{*Backtrace*}$B$N@hF,$KI=<($7!"(B
$B%f!<%6!<$,FI$a$k$h$&$K$9$k$3$H$G$"$k!#(B
$B0J2<$K=R$Y$k>l9g$r=|$$$F!"$3$l$,$3$l$i$N0z?t$N(B@emph{$BM#0l(B}$B$NMQES$G$"$k!#(B

@c However, certain values for first argument to @code{debug} have a
@c special significance.  (Normally, these values are used only by the
@c internals of Emacs, and not by programmers calling @code{debug}.)  Here
@c is a table of these special values:
@code{debug}$B$NBh(B1$B0z?t$,FCDj$NCM$r;}$D>l9g!"FCJL$J0UL#$,$"$k!#(B
$B!JDL>o!"$3$l$i$NCM$O(BEmacs$BFbIt$GMQ$$$k$@$1$G$"$j!"(B
$B%W%m%0%i%^$,(B@code{debug}$B$r8F$V$H$-$K$O;H$o$J$$!#!K(B
$B0J2<$K$3$l$i$NFCJL$JCM$r<($9!#(B

@table @code
@item lambda
@c @cindex @code{lambda} in debug
@cindex $B%G%P%C%0$N(B@code{lambda}
@cindex @code{lambda}$B!"%G%P%C%0(B
@c A first argument of @code{lambda} means @code{debug} was called because
@c of entry to a function when @code{debug-on-next-call} was
@c non-@code{nil}.  The debugger displays @samp{Entering:} as a line of
@c text at the top of the buffer.
$BBh(B1$B0z?t$,(B@code{lambda}$B$G$"$k$H!"(B
@code{debug-on-next-call}$B$,(B@code{nil}$B0J30$G$"$k$?$a$K(B
$B4X?t$KF~$k$H$-$K(B@code{debug}$B$r8F$S=P$7$?$3$H$r0UL#$9$k!#(B
$B%G%P%C%,$O%P%C%U%!$N@hF,$K%F%-%9%H9T(B@samp{Entering:}$B$rI=<($9$k!#(B

@item debug
@c @code{debug} as first argument indicates a call to @code{debug} because
@c of entry to a function that was set to debug on entry.  The debugger
@c displays @samp{Entering:}, just as in the @code{lambda} case.  It also
@c marks the stack frame for that function so that it will invoke the
@c debugger when exited.
$BBh(B1$B0z?t$,(B@code{debug}$B$G$"$k$H!"(B
$B4X?t$KF~$k$H$-$K%G%P%C%,$r5/F0$9$k$h$&$K$J$C$F$$$?$?$a$K(B
@code{debug}$B$r8F$S=P$7$?$3$H$r<($9!#(B
$B%G%P%C%,$O!"(B@code{lambda}$B$N>l9g$HF1MM$K!"(B@samp{Entering:}$B$rI=<($9$k!#(B
$B$5$i$K!"Ev3:4X?t$N%9%?%C%/%U%l!<%`$K4X?t$+$i(B
$BLa$k$H$-$K%G%P%C%,$r5/F0$9$k$h$&$K0u$rIU$1$k!#(B

@item t
@c When the first argument is @code{t}, this indicates a call to
@c @code{debug} due to evaluation of a list form when
@c @code{debug-on-next-call} is non-@code{nil}.  The debugger displays the
@c following as the top line in the buffer:
$BBh(B1$B0z?t$,(B@code{t}$B$G$"$k$H!"(B
@code{debug-on-next-call}$B$,(B@code{nil}$B0J30$G$"$k$H$-$K(B
$B%U%)!<%`$NJB$S$rI>2A$7$?$?$a$K(B
@code{debug}$B$r8F$S=P$7$?$3$H$r<($9!#(B
$B%G%P%C%,$O%P%C%U%!$N@hF,9T$K$D$.$N9T$rI=<($9$k!#(B

@smallexample
Beginning evaluation of function call form:
@end smallexample

@item exit
@c When the first argument is @code{exit}, it indicates the exit of a stack
@c frame previously marked to invoke the debugger on exit.  The second
@c argument given to @code{debug} in this case is the value being returned
@c from the frame.  The debugger displays @samp{Return value:} in the top
@c line of the buffer, followed by the value being returned.
$BBh(B1$B0z?t$,(B@code{exit}$B$G$"$k$H!"(B
$B%9%?%C%/%U%l!<%`$+$iH4$1$k$H$-$K%G%P%C%,$r8F$S=P$9$h$&$K0u$r(B
$BIU$1$?%9%?%C%/%U%l!<%`$+$iH4$1$?$3$H$r<($9!#(B
$B$3$N>l9g!"(B@code{debug}$B$NBh(B2$B0z?t$O%U%l!<%`$+$i$NLa$jCM$G$"$k!#(B
$B%G%P%C%,$O%P%C%U%!$N@hF,9T$K(B@samp{Return value:}$B$KB3$1$FLa$jCM$rI=<($9$k!#(B

@item error
@c @cindex @code{error} in debug
@cindex $B%G%P%C%0$N(B@code{error}
@cindex @code{error}$B!"%G%P%C%0(B
@c When the first argument is @code{error}, the debugger indicates that
@c it is being entered because an error or @code{quit} was signaled and not
@c handled, by displaying @samp{Signaling:} followed by the error signaled
@c and any arguments to @code{signal}.  For example,
$BBh(B1$B0z?t$,(B@code{error}$B$G$"$k$H!"(B
$B%(%i!<$d(B@code{quit}$B$,DLCN$5$l$?$,=hM}$5$l$J$$$?$a$K%G%P%C%,$KF~$C$?$3$H$r<($7!"(B
@samp{Signaling:}$B$KB3$1$FDLCN$5$l$?%(%i!<$H(B@code{signal}$B$N0z?t$rI=<($9$k!#(B
$B$?$H$($P$D$.$N$H$*$j!#(B

@example
@group
(let ((debug-on-error t))
  (/ 1 0))
@end group

@group
------ Buffer: *Backtrace* ------
Signaling: (arith-error)
  /(1 0)
...
------ Buffer: *Backtrace* ------
@end group
@end example

@c If an error was signaled, presumably the variable
@c @code{debug-on-error} is non-@code{nil}.  If @code{quit} was signaled,
@c then presumably the variable @code{debug-on-quit} is non-@code{nil}.
$B%(%i!<$,DLCN$5$l$?$H$-$K$O!"(B
$BJQ?t(B@code{debug-on-error}$B$O(B@code{nil}$B0J30$G$"$k$O$:$G$"$k!#(B
@code{quit}$B$,DLCN$5$l$?$H$-$K$O!"(B
$BJQ?t(B@code{debug-on-quit}$B$O(B@code{nil}$B0J30$G$"$k$O$:$G$"$k!#(B

@item nil
@c Use @code{nil} as the first of the @var{debugger-args} when you want
@c to enter the debugger explicitly.  The rest of the @var{debugger-args}
@c are printed on the top line of the buffer.  You can use this feature to
@c display messages---for example, to remind yourself of the conditions
@c under which @code{debug} is called.
$BL@<(E*$K%G%P%C%,$KF~$k$H$-$K$O!"(B
@var{debugger-args}$B$N@hF,$H$7$F(B@code{nil}$B$r;H$&!#(B
@var{debugger-args}$B$N;D$j$O%P%C%U%!$N@hF,9T$KI=<($5$l$k!#(B
$B$3$N5!G=$rMQ$$$F%a%C%;!<%8$rI=<($G$-!"(B
$B$?$H$($P!"(B@code{debug}$B$r8F$S=P$7$?>r7o$N3P$(=q$-$K$9$k!#(B
@end table
@end defun

@node Internals of Debugger
@c @subsection Internals of the Debugger
@subsection $B%G%P%C%,$NFbIt(B

@c   This section describes functions and variables used internally by the
@c debugger.
$BK\@a$G$O!"%G%P%C%,$,FbItE*$K;HMQ$9$k4X?t$dJQ?t$K$D$$$F=R$Y$^$9!#(B

@defvar debugger
@c The value of this variable is the function to call to invoke the
@c debugger.  Its value must be a function of any number of arguments (or,
@c more typically, the name of a function).  Presumably this function will
@c enter some kind of debugger.  The default value of the variable is
@c @code{debug}.
$B$3$NJQ?t$NCM$O!"%G%P%C%,$r5/F0$9$k$?$a$K8F$S=P$94X?t$G$"$k!#(B
$B$=$NCM$O!"2DJQ8D?t$N0z?t$r<h$k4X?t!J$"$k$$$OE57?E*$K$O4X?tL>!K$G$"$k$3$H!#(B
$B$=$N4X?t$G$J$s$i$+$N%G%P%C%,$KF~$k$H2>Dj$9$k!#(B
$B$3$NJQ?t$N%G%U%)%k%HCM$O(B@code{debug}$B!#(B

@c The first argument that Lisp hands to the function indicates why it
@c was called.  The convention for arguments is detailed in the description
@c of @code{debug}.
Lisp$B$,4X?t$KEO$9:G=i$N0z?t$G!"8F$S=P$7$?M}M3$rI=$9!#(B
$B0z?t$N5,Ls$O(B@code{debug}$B$K5-=R$7$F$"$k!#(B
@end defvar

@c @deffn Command backtrace
@deffn $B%3%^%s%I(B backtrace
@c @cindex run time stack
@c @cindex call stack
@cindex $B<B9T;~%9%?%C%/(B
@cindex $B8F$S=P$7%9%?%C%/(B
@c This function prints a trace of Lisp function calls currently active.
@c This is the function used by @code{debug} to fill up the
@c @samp{*Backtrace*} buffer.  It is written in C, since it must have access
@c to the stack to determine which function calls are active.  The return
@c value is always @code{nil}.
$B$3$N4X?t$O!"8=:_3h@-$J(BLisp$B4X?t8F$S=P$7$N%H%l!<%9$rI=<($9$k!#(B
$B$3$l$O!"(B@code{debug}$B$,%P%C%U%!(B@samp{*Backtrace*}$B$r(B
$BK~$?$9$?$a$KMQ$$$k4X?t$G$"$k!#(B
$B$I$N4X?t8F$S=P$7$,3h@-$G$"$k$+$r(B
$BH=CG$9$k$?$a$K%9%?%C%/$r;2>H$9$kI,MW$,$"$k$?$a(BC$B$G=q$$$F$"$k!#(B
$BLa$jCM$O$D$M$K(B@code{nil}$B!#(B

@c In the following example, a Lisp expression calls @code{backtrace}
@c explicitly.  This prints the backtrace to the stream
@c @code{standard-output}: in this case, to the buffer
@c @samp{backtrace-output}.  Each line of the backtrace represents one
@c function call.  The line shows the values of the function's arguments if
@c they are all known.  If they are still being computed, the line says so.
@c The arguments of special forms are elided.
$B0J2<$NNc$G$O!"(BLisp$B<0$GL@<(E*$K(B@code{backtrace}$B$r8F$S=P$9!#(B
$B$3$l$K$h$j!"%P%C%/%H%l!<%9$r%9%H%j!<%`(B@code{standard-output}$B$K=PNO$9$k!#(B
$B$3$3$G$O%P%C%U%!(B@samp{backtrace-output}$B$K=PNO$9$k!#(B
$B%P%C%/%H%l!<%9$N3F9T$O!"(B1$B$D$N4X?t8F$S=P$7$rI=$9!#(B
$B4X?t$N0z?tCM$9$Y$F$,H=$l$P$=$l$i$r9T$KI=<($9$k!#(B
$B$=$l$i$,7W;;ESCf$G$"$l$P!"$=$N;]$r9T$KI=<($9$k!#(B
$B%9%Z%7%c%k%U%)!<%`$N0z?t$O>JN,$9$k!#(B

@smallexample
@group
(with-output-to-temp-buffer "backtrace-output"
  (let ((var 1))
    (save-excursion
      (setq var (eval '(progn
                         (1+ var)
                         (list 'testing (backtrace))))))))

     @result{} nil
@end group

@group
----------- Buffer: backtrace-output ------------
  backtrace()
  (list ...computing arguments...)
@end group
  (progn ...)
  eval((progn (1+ var) (list (quote testing) (backtrace))))
  (setq ...)
  (save-excursion ...)
  (let ...)
  (with-output-to-temp-buffer ...)
  eval-region(1973 2142 #<buffer *scratch*>)
  byte-code("...  for eval-print-last-sexp ...")
@group
  eval-print-last-sexp(nil)
* call-interactively(eval-print-last-sexp)
----------- Buffer: backtrace-output ------------
@end group
@end smallexample

@c The character @samp{*} indicates a frame whose debug-on-exit flag is
@c set.
$BJ8;z(B@samp{*}$B$O!"(B
$BH4$1=P$k$H$-$K%G%P%C%,$r5/F0$9$k0u$,IU$$$F$$$k%U%l!<%`$rI=$9!#(B
@end deffn

@ignore @c Not worth mentioning
@defopt stack-trace-on-error
@cindex stack trace
This variable controls whether Lisp automatically displays a
backtrace buffer after every error that is not handled.  A quit signal
counts as an error for this variable.  If it is non-@code{nil} then a
backtrace is shown in a pop-up buffer named @samp{*Backtrace*} on every
error.  If it is @code{nil}, then a backtrace is not shown.

When a backtrace is shown, that buffer is not selected.  If either
@code{debug-on-quit} or @code{debug-on-error} is also non-@code{nil}, then
a backtrace is shown in one buffer, and the debugger is popped up in
another buffer with its own backtrace.

We consider this feature to be obsolete and superseded by the debugger
itself.
@end defopt
@end ignore

@defvar debug-on-next-call
@c @cindex @code{eval}, and debugging
@c @cindex @code{apply}, and debugging
@c @cindex @code{funcall}, and debugging
@cindex @code{eval}$B$H%G%P%C%0(B
@cindex @code{apply}$B$H%G%P%C%0(B
@cindex @code{funcall}$B$H%G%P%C%0(B
@c If this variable is non-@code{nil}, it says to call the debugger before
@c the next @code{eval}, @code{apply} or @code{funcall}.  Entering the
@c debugger sets @code{debug-on-next-call} to @code{nil}.
$B$3$NJQ?t$,(B@code{nil}$B0J30$G$"$k$H!"(B
$B$D$.$K(B@code{eval}$B!"(B@code{apply}$B!"(B@code{funcall}$B$r8F$S=P$9$^$($K(B
$B%G%P%C%,$r8F$S=P$9$3$H$r;XDj$9$k!#(B
$B%G%P%C%,$KF~$k$H(B@code{debug-on-next-call}$B$r(B@code{nil}$B$K@_Dj$9$k!#(B

@c The @kbd{d} command in the debugger works by setting this variable.
$B%G%P%C%,$N%3%^%s%I(B@kbd{d}$B$O!"$3$NJQ?t$r@_Dj$9$k$3$H$GF0:n$9$k!#(B
@end defvar

@defun backtrace-debug level flag
@c This function sets the debug-on-exit flag of the stack frame @var{level}
@c levels down the stack, giving it the value @var{flag}.  If @var{flag} is
@c non-@code{nil}, this will cause the debugger to be entered when that
@c frame later exits.  Even a nonlocal exit through that frame will enter
@c the debugger.
$B$3$N4X?t$O!"(B@var{level}$B$N?<$5$N%9%?%C%/%U%l!<%`$K(B
$BCM(B@var{flag}$B$K1~$8$F%U%l!<%`$+$iH4$1=P$k$H$-$N%G%P%C%,8F$S=P$7$N0u$rIU$1$k!#(B
@var{flag}$B$,(B@code{nil}$B0J30$G$"$k$H!"(B
$B$N$A$KEv3:%U%l!<%`$+$iH4$1$k$H%G%P%C%,$KF~$k!#(B
$BHs%m!<%+%k$JC&=P$GEv3:%U%l!<%`$+$iH4$1$k$H$-$K$b%G%P%C%,$KF~$k!#(B

@c This function is used only by the debugger.
$B$3$N4X?t$O%G%P%C%,$N$_$,;HMQ$9$k!#(B
@end defun

@defvar command-debug-status
@c This variable records the debugging status of the current interactive
@c command.  Each time a command is called interactively, this variable is
@c bound to @code{nil}.  The debugger can set this variable to leave
@c information for future debugger invocations during the same command
@c invocation.
$B$3$NJQ?t$O!"8=:_$NBPOCE*%3%^%s%I$N%G%P%C%0>u67$r5-O?$9$k!#(B
$B%3%^%s%I$,BPOCE*$K8F$S=P$5$l$k$?$S$K!"(B
$B$3$NJQ?t$O(B@code{nil}$B$KB+G{$5$l$k!#(B
$B%G%P%C%,$O$3$NJQ?t$K@_Dj$9$k$3$H$G!"(B
$BF1$8%3%^%s%I$N5/F0Cf$K%G%P%C%,$,>-Mh5/F0$5$l$?>l9g$KHw$($F(B
$B>pJs$r;D$9$3$H$,$G$-$k!#(B

@c The advantage, for the debugger, of using this variable rather than an
@c ordinary global variable is that the data will never carry over to a
@c subsequent command invocation.
$B%G%P%C%,$K$H$C$F$O!"DL>o$N%0%m!<%P%kJQ?t$G$O$J$/$3$NJQ?t$r;H$&MxE@$O!"(B
$B0J9_$N%3%^%s%I5/F0$K%G!<%?$,7+$j1[$5$J$$$3$H$G$"$k!#(B
@end defvar

@defun backtrace-frame frame-number
@c The function @code{backtrace-frame} is intended for use in Lisp
@c debuggers.  It returns information about what computation is happening
@c in the stack frame @var{frame-number} levels down.
$B4X?t(B@code{backtrace-frame}$B$O!"(BLisp$B%G%P%C%,$G;H$&$3$H$r0U?^$7$F$$$k!#(B
$B?<$5(B@var{frame-number}$B$N%9%?%C%/%U%l!<%`$G?J9TCf$N7W;;$K4X$9$k>pJs$rJV$9!#(B

@c If that frame has not evaluated the arguments yet (or is a special
@c form), the value is @code{(nil @var{function} @var{arg-forms}@dots{})}.
$BEv3:%U%l!<%`$G0z?t$NI>2A$r40N;$7$F$$$J$1$l$P!J$"$k$$$O%9%Z%7%c%k%U%)!<%`!K!"(B
$BCM$O(B@code{(nil @var{function} @var{arg-forms}@dots{})}$B!#(B

@c If that frame has evaluated its arguments and called its function
@c already, the value is @code{(t @var{function}
@c @var{arg-values}@dots{})}.
$BEv3:%U%l!<%`$G0z?t$NI>2A$r40N;$74X?t$r8F$S=P$7$F$$$l$P!"(B
$BCM$O(B@code{(t @var{function} @var{arg-values}@dots{})}$B!#(B

@c In the return value, @var{function} is whatever was supplied as the
@c @sc{car} of the evaluated list, or a @code{lambda} expression in the
@c case of a macro call.  If the function has a @code{&rest} argument, that
@c is represented as the tail of the list @var{arg-values}.
$BLa$jCM$K$*$$$F!"(B@var{function}$B$OI>2A$7$?%j%9%H$N(B@sc{car}$B$G$"$k$+!"(B
$B%^%/%m8F$S=P$7$G$O(B@code{lambda}$B<0$G$"$k!#(B
$B4X?t$K0z?t(B@code{&rest}$B$,$"$l$P!"%j%9%H(B@var{arg-values}$B$N;D$j$GI=8=$5$l$k!#(B

@c If @var{frame-number} is out of range, @code{backtrace-frame} returns
@c @code{nil}.
@var{frame-number}$B$,HO0O30$G$"$k$H!"(B@code{backtrace-frame}$B$O(B@code{nil}$B$rJV$9!#(B
@end defun

@include edebug.texi

@node Syntax Errors
@c @section Debugging Invalid Lisp Syntax
@section $BIT@5$J(BLisp$B9=J8$N%G%P%C%0(B

@c   The Lisp reader reports invalid syntax, but cannot say where the real
@c problem is.  For example, the error ``End of file during parsing'' in
@c evaluating an expression indicates an excess of open parentheses (or
@c square brackets).  The reader detects this imbalance at the end of the
@c file, but it cannot figure out where the close parenthesis should have
@c been.  Likewise, ``Invalid read syntax: ")"'' indicates an excess close
@c parenthesis or missing open parenthesis, but does not say where the
@c missing parenthesis belongs.  How, then, to find what to change?
Lisp$B%j!<%@$OIT@5$J9=J8$rJs9p$7$^$9$,!"$I$3$KLdBj$,$"$k$+$OJs9p$G$-$^$;$s!#(B
$B$?$H$($P!"<0$rI>2ACf$N%(%i!<!X(BEnd of file during parsing$B!Y(B
$B!J9=J82r@OCf$K%U%!%$%k$N=*$j!K$O!"3+$-3g8L!J$"$k$$$O3+$-3Q3g8L!K$,(B
$BB?$9$.$k$3$H$rI=$7$^$9!#(B
Lisp$B%j!<%@$O3g8L$,BP1~$7$F$$$J$$$3$H$r%U%!%$%k$NKvHx$G8!=P$7$^$9$,!"(B
$B$I$3$KJD$83g8L$,$"$k$Y$-$+$OH=CG$G$-$^$;$s!#(B
$BF1MM$K!"!X(BInvalid read syntax: ")"$B!Y!JIT@5$J9=J8!'(B")"$B!K$O(B
$BJD$83g8L$,B?$9$.$k$+3+$-3g8L$,B-$j$J$$$3$H$rI=$7$^$9$,!"(B
$B$I$3$K3g8L$,B-$j$J$$$+$OH=CG$G$-$^$;$s!#(B
$B$=$l$G$O!"$I$3$rJQ99$9$Y$-$+$I$N$h$&$KD4$Y$k$N$G$7$g$&!)(B

@c   If the problem is not simply an imbalance of parentheses, a useful
@c technique is to try @kbd{C-M-e} at the beginning of each defun, and see
@c if it goes to the place where that defun appears to end.  If it does
@c not, there is a problem in that defun.
$BLdBj$,C1=c$J3g8L$NHsBP1~$G$J$1$l$P!"(B
$B3F4X?tDj5A$N@hF,$G(B@kbd{C-M-e}$B$r;n$7!"(B
$B4X?tDj5A$NKvHx$K0\F0$9$k$+$I$&$+$r$_$k$N$OM-MQ$J5;K!$G$9!#(B
$B@5$7$/0\F0$7$J$1$l$P!"$=$N4X?t$KLdBj$,$"$j$^$9!#(B

@c   However, unmatched parentheses are the most common syntax errors in
@c Lisp, and we can give further advice for those cases.  (In addition,
@c just moving point through the code with Show Paren mode enabled might
@c find the mismatch.)
Lisp$B$K$h$/$"$k9=J8%(%i!<$O3g8L$NHsBP1~$J$N$G!"(B
$B$3$l$i$N>lLL$K$D$$$F>\$7$$=u8@$r=R$Y$F$*$-$^$9!#(B
$B!J$5$i$K!"BP1~3g8LI=<(%b!<%I$r%*%s$K$7$F%]%$%s%H$r0\F0$9$k$HHsBP1~$r(B
$BC5$7$d$9$$!#!K(B

@menu
* Excess Open::     How to find a spurious open paren or missing close.
* Excess Close::    How to find a spurious close paren or missing open.
@end menu

@node Excess Open
@c @subsection Excess Open Parentheses
@subsection $B3+$-3g8L$N2a>j(B

@c   The first step is to find the defun that is unbalanced.  If there is
@c an excess open parenthesis, the way to do this is to insert a
@c close parenthesis at the end of the file and type @kbd{C-M-b}
@c (@code{backward-sexp}).  This will move you to the beginning of the
@c defun that is unbalanced.  (Then type @kbd{C-@key{SPC} C-_ C-u
@c C-@key{SPC}} to set the mark there, undo the insertion of the
@c close parenthesis, and finally return to the mark.)
$B:G=i$N<j=g$O!"3g8L$,BP1~$7$F$$$J$$4X?tDj5A$rC5$9$3$H$G$9!#(B
$B3+$-3g8L$,2a>j$G$"$l$P!"%U%!%$%k$NKvHx$KJD$83g8L$rA^F~$7(B
@kbd{C-M-b}$B!J(B@code{backward-sexp}$B!K$rBG$A$^$9!#(B
$B$3$&$9$k$H!"3g8L$,BP1~$7$F$$$J$$4X?tDj5A$N@hF,$X0\F0$7$^$9!#(B
$B!J$=$&$7$?$i!"(B@kbd{C-@key{SPC} C-_ C-u C-@key{SPC}}$B$HBG$C$F!"(B
$BEv3:2U=j$K%^!<%/$r@_Dj$7$F$+$iJD$83g8L$NA^F~$r<h$j>C$7!"(B
$B:G=*E*$K%^!<%/$XLa$k!#!K(B

@c   The next step is to determine precisely what is wrong.  There is no
@c way to be sure of this except by studying the program, but often the
@c existing indentation is a clue to where the parentheses should have
@c been.  The easiest way to use this clue is to reindent with @kbd{C-M-q}
@c and see what moves.  @strong{But don't do this yet!}  Keep reading,
@c first.
$B$D$.$N<j=g$O!"$J$K$,0-$$$+@53N$KH=CG$9$k$3$H$G$9!#(B
$B%W%m%0%i%`$rD4$Y$k0J30$K$3$l$r3N<B$K9T$&J}K!$O$"$j$^$;$s$,!"(B
$B$7$P$7$P!"4{B8$N;z2<$2$,3g8L$N$"$j$+$?$rM=A[$9$k80$K$J$j$^$9!#(B
$B$3$l$rMxMQ$9$k$b$C$H$b4JC1$JJ}K!$O(B@kbd{C-M-q}$B$G;z2<$2$7D>$7!"(B
$B$I$N$h$&$K$J$k$+8+$k$3$H$G$9!#(B
@strong{$B$^$@$d$i$J$$$G$/$@$5$$!*(B}@code{ }
$B$^$:FI$_?J$s$F$/$@$5$$!#(B

@c   Before you do this, make sure the defun has enough close parentheses.
@c Otherwise, @kbd{C-M-q} will get an error, or will reindent all the rest
@c of the file until the end.  So move to the end of the defun and insert a
@c close parenthesis there.  Don't use @kbd{C-M-e} to move there, since
@c that too will fail to work until the defun is balanced.
$B$3$l$r9T$&$^$($K!"4X?tDj5A$K=<J,$J?t$NJD$83g8L$,$"$k$3$H$r3NG'$7$F$/$@$5$$!#(B
$B$5$b$J$$$H!"(B@kbd{C-M-q}$B$,%(%i!<$K$J$C$?$j!"(B
$B%U%!%$%k$NKvHx$^$G$r;z2<$2$7D>$7$F$7$^$$$^$9!#(B
$B$G$9$+$i!"4X?tDj5A$NKvHx$X0\F0$7$FJD$83g8L$rA^F~$7$F$*$-$^$9!#(B
@kbd{C-M-e}$B$r;H$C$F0\F0$7$J$$$G$/$@$5$$!#(B
$B$H$$$&$N$O!"4X?tDj5A$N3g8L$NBP1~$,<h$l$F$$$J$$$H<:GT$9$k$+$i$G$9!#(B

@c   Now you can go to the beginning of the defun and type @kbd{C-M-q}.
@c Usually all the lines from a certain point to the end of the function
@c will shift to the right.  There is probably a missing close parenthesis,
@c or a superfluous open parenthesis, near that point.  (However, don't
@c assume this is true; study the code to make sure.)  Once you have found
@c the discrepancy, undo the @kbd{C-M-q} with @kbd{C-_}, since the old
@c indentation is probably appropriate to the intended parentheses.
$B4X?tDj5A$N@hF,$X0\F0$7$F(B@kbd{C-M-q}$B$rBG$A$^$9!#(B
$BDL>o!"$"$k>l=j$+$i4X?t$NKvHx$^$G$N9T$,1&$X$:$l$^$9!#(B
$B$=$N>l=j$N6a$/$G!"JD$83g8L$,B-$j$J$+$C$?$j3+$-3g8L$,B?$9$.$k$N$G$9!#(B
$B!J$7$+$7!"$3$l$,@5$7$$$H2>Dj$7$F$O$J$i$J$$!#(B
$B%3!<%I$rD4$Y$F3NG'$9$k$3$H!#!K(B
$BIT6q9g2U=j$,$_$D$+$C$?$J$i$P!"(B
$B0U?^$7$?3g8L$KBP$7$F$O8E$$;z2<$2$,E,$7$F$$$k$G$7$g$&$+$i(B
@kbd{C-_}$B$G(B@kbd{C-M-q}$B$r%"%s%I%%$7$^$9!#(B

@c   After you think you have fixed the problem, use @kbd{C-M-q} again.  If
@c the old indentation actually fit the intended nesting of parentheses,
@c and you have put back those parentheses, @kbd{C-M-q} should not change
@c anything.
$BLdBj$r2r7h$G$-$?$H;W$C$?$i!":FEY(B@kbd{C-M-q}$B$r;H$$$^$9!#(B
$B8E$$;z2<$2$,0U?^$7$?3g8L$NF~$l;R$KBP1~$7$F$$$F!"(B
$BI,MW$J3g8L$rA^F~$G$-$F$$$k$J$i$P!"(B
@kbd{C-M-q}$B$O$J$K$bJQ$($J$$$O$:$G$9!#(B

@node Excess Close
@c @subsection Excess Close Parentheses
@subsection $BJD$83g8L$N2a>j(B

@c   To deal with an excess close parenthesis, first insert an open
@c parenthesis at the beginning of the file, back up over it, and type
@c @kbd{C-M-f} to find the end of the unbalanced defun.  (Then type
@c @kbd{C-@key{SPC} C-_ C-u C-@key{SPC}} to set the mark there, undo the
@c insertion of the open parenthesis, and finally return to the mark.)
$B2a>j$JJD$83g8L$KBP=h$9$k$K$O!"$^$:!"%U%!%$%k$N@hF,$K3+$-3g8L$rA^F~$7!"(B
$B$=$N3g8L$N$^$($G(B@kbd{C-M-f}$B$rBG$C$F!"(B
$B3g8L$,BP1~$7$F$$$J$$4X?tDj5A$NKvHx$rC5$7$^$9!#(B
$B!J$=$7$F!"(B@kbd{C-@key{SPC} C-_ C-u C-@key{SPC}}$B$HBG$C$F!"(B
$BEv3:2U=j$K%^!<%/$r@_Dj$7$F3+$-3g8L$NA^F~$r%"%s%I%%$7!"(B
$B:G=*E*$K%^!<%/$XLa$k!#!K(B

@c   Then find the actual matching close parenthesis by typing @kbd{C-M-f}
@c at the beginning of that defun.  This will leave you somewhere short of
@c the place where the defun ought to end.  It is possible that you will
@c find a spurious close parenthesis in that vicinity.
$B$=$N4X?tDj5A$N@hF,$G(B@kbd{C-M-f}$B$HBG$C$F!"(B
$B<B:]$KBP1~$7$F$$$kJD$83g8L$rC5$7$^$9!#(B
$B$3$l$K$h$j!"4X?tDj5A$,=*$k$Y$->l=j$h$j<jA0$N2U=j$K0\F0$9$k$O$:$G$9!#(B
$B$3$NIU6a$KM>J,$JJD$83g8L$,$_$D$+$k$3$H$b$"$k$G$7$g$&!#(B

@c   If you don't see a problem at that point, the next thing to do is to
@c type @kbd{C-M-q} at the beginning of the defun.  A range of lines will
@c probably shift left; if so, the missing open parenthesis or spurious
@c close parenthesis is probably near the first of those lines.  (However,
@c don't assume this is true; study the code to make sure.)  Once you have
@c found the discrepancy, undo the @kbd{C-M-q} with @kbd{C-_}, since the
@c old indentation is probably appropriate to the intended parentheses.
$B$=$N>l=j$KLdBj$,$J$1$l$P!"$D$.$K$9$Y$-$3$H$O!"(B
$B4X?tDj5A$N@hF,$G(B@kbd{C-M-q}$B$HBG$D$3$H$G$9!#(B
$B$"$kHO0O$N9T$,:8$K$:$l$k$G$7$g$&!#(B
$B$b$7$=$&$J$i$P!"3+$-3g8L$,B-$j$J$$$+M>J,$JJD$83g8L$O!"(B
$B$=$N$h$&$J9T$N@hF,IU6a$K$"$k$G$7$g$&!#(B
$B!J$7$+$7!"$3$l$,@5$7$$$H2>Dj$7$F$O$J$i$J$$!#(B
$B%3!<%I$rD4$Y$F3NG'$9$k$3$H!#!K(B
$BIT6q9g2U=j$,$_$D$+$C$?$J$i$P!"(B
$B0U?^$7$?3g8L$KBP$7$F$O8E$$;z2<$2$,E,$7$F$$$k$G$7$g$&$+$i(B
@kbd{C-_}$B$G(B@kbd{C-M-q}$B$r%"%s%I%%$7$^$9!#(B

@c   After you think you have fixed the problem, use @kbd{C-M-q} again.  If
@c the old indentation actually fit the intended nesting of parentheses,
@c and you have put back those parentheses, @kbd{C-M-q} should not change
@c anything.
$BLdBj$r2r7h$G$-$?$H;W$C$?$i!":FEY(B@kbd{C-M-q}$B$r;H$$$^$9!#(B
$B8E$$;z2<$2$,0U?^$7$?3g8L$NF~$l;R$KBP1~$7$F$$$F!"(B
$BI,MW$J3g8L$rA^F~$G$-$F$$$k$J$i$P!"(B
@kbd{C-M-q}$B$O$J$K$bJQ$($J$$$O$:$G$9!#(B

@node Compilation Errors
@c @section Debugging Problems in Compilation
@section $B%3%s%Q%$%k;~$NLdBj$N%G%P%C%0(B

@c   When an error happens during byte compilation, it is normally due to
@c invalid syntax in the program you are compiling.  The compiler prints a
@c suitable error message in the @samp{*Compile-Log*} buffer, and then
@c stops.  The message may state a function name in which the error was
@c found, or it may not.  Either way, here is how to find out where in the
@c file the error occurred.
$B%P%$%H%3%s%Q%$%k;~$K%(%i!<$,H/@8$7$?$H$-$O!"(B
$BDL>o!"FI<T$,%3%s%Q%$%k$7$F$$$k%W%m%0%i%`$NIT@5$J9=J8$K860x$,$"$j$^$9!#(B
$B%3%s%Q%$%i$O%P%C%U%!(B@samp{*Compile-Log*}$B$KE,@Z$J%(%i!<%a%C%;!<%8$r(B
$BI=<($7$F$+$iDd;_$7$^$9!#(B
$B%a%C%;!<%8$K$O%(%i!<$H$J$C$?4X?t$NL>A0$,$"$C$?$j$J$+$C$?$j$7$^$9!#(B
$B$$$:$l$K$7$F$b!"$D$.$N$h$&$K$7$F%U%!%$%k$N$I$3$G%(%i!<$,@8$8$?$+$rD4$Y$^$9!#(B

@c   What you should do is switch to the buffer @w{@samp{ *Compiler Input*}}.
@c (Note that the buffer name starts with a space, so it does not show
@c up in @kbd{M-x list-buffers}.)  This buffer contains the program being
@c compiled, and point shows how far the byte compiler was able to read.
$B$^$:!"%P%C%U%!(B@samp{ *Compiler Input*}$B$K@Z$jBX$($^$9!#(B
$B!J%P%C%U%!L>$,6uGr$G;O$^$j!"$=$N$?$a!"(B
@kbd{M-x list-buffers}$B$G$OI=<($5$l$J$$$3$H$KCm0U!#!K(B
$B$3$N%P%C%U%!$K$O%3%s%Q%$%k$7$?%W%m%0%i%`$,F~$C$F$$$F!"(B
$B%]%$%s%H0LCV$O%P%$%H%3%s%Q%$%i$,$I$3$^$GFI$_<h$C$?$+$rI=$7$^$9!#(B

@c   If the error was due to invalid Lisp syntax, point shows exactly where
@c the invalid syntax was @emph{detected}.  The cause of the error is not
@c necessarily near by!  Use the techniques in the previous section to find
@c the error.
$B%(%i!<$N860x$,IT@5$J(BLisp$B9=J8$G$"$k$J$i$P!"(B
$B%]%$%s%H0LCV$,IT@59=J8$r(B@emph{$B8!=P$7$?(B}$B2U=j$r@53N$KI=$7$^$9!#(B
$B%(%i!<860x$,6a$/$K$"$k$H$O8B$j$^$;$s!*(B@code{ }
$B%(%i!<$rC5$9$?$a$KA0@a$NJ}K!$r;H$C$F$/$@$5$$!#(B

@c   If the error was detected while compiling a form that had been read
@c successfully, then point is located at the end of the form.  In this
@c case, this technique can't localize the error precisely, but can still
@c show you which function to check.
$B@5$7$/FI$_<h$C$?%U%)!<%`$N%3%s%Q%$%kCf$K%(%i!<$r8!=P$7$?$H$-$K$O!"(B
$B%]%$%s%H$O$=$N%U%)!<%`$NKvHx$K0LCV$7$F$$$^$9!#(B
$B$3$N>l9g!"$3$NJ}K!$G$O%(%i!<2U=j$r@53N$KH=JL$G$-$^$;$s$,!"(B
$B$I$N4X?t$r3NG'$9$Y$-$+$r<($7$F$$$^$9!#(B