debugger2 1.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (183) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.travis.yml +3 -0
  4. data/AUTHORS +10 -0
  5. data/CHANGELOG.md +65 -0
  6. data/CONTRIBUTING.md +1 -0
  7. data/Gemfile +3 -0
  8. data/LICENSE +23 -0
  9. data/OLDER_CHANGELOG +334 -0
  10. data/OLD_CHANGELOG +5655 -0
  11. data/OLD_README +122 -0
  12. data/README.md +108 -0
  13. data/Rakefile +78 -0
  14. data/bin/rdebug +397 -0
  15. data/debugger2.gemspec +29 -0
  16. data/doc/.cvsignore +42 -0
  17. data/doc/Makefile.am +63 -0
  18. data/doc/emacs-notes.txt +38 -0
  19. data/doc/hanoi.rb +35 -0
  20. data/doc/primes.rb +28 -0
  21. data/doc/rdebug-emacs.texi +1030 -0
  22. data/doc/ruby-debug.texi +3791 -0
  23. data/doc/test-tri2.rb +18 -0
  24. data/doc/tri3.rb +8 -0
  25. data/doc/triangle.rb +12 -0
  26. data/emacs/Makefile.am +130 -0
  27. data/emacs/rdebug-annotate.el +385 -0
  28. data/emacs/rdebug-breaks.el +407 -0
  29. data/emacs/rdebug-cmd.el +92 -0
  30. data/emacs/rdebug-core.el +502 -0
  31. data/emacs/rdebug-dbg.el +62 -0
  32. data/emacs/rdebug-error.el +79 -0
  33. data/emacs/rdebug-fns.el +111 -0
  34. data/emacs/rdebug-frames.el +230 -0
  35. data/emacs/rdebug-gud.el +242 -0
  36. data/emacs/rdebug-help.el +104 -0
  37. data/emacs/rdebug-info.el +83 -0
  38. data/emacs/rdebug-layouts.el +180 -0
  39. data/emacs/rdebug-locring.el +118 -0
  40. data/emacs/rdebug-output.el +106 -0
  41. data/emacs/rdebug-regexp.el +118 -0
  42. data/emacs/rdebug-secondary.el +260 -0
  43. data/emacs/rdebug-shortkey.el +175 -0
  44. data/emacs/rdebug-source.el +568 -0
  45. data/emacs/rdebug-track.el +392 -0
  46. data/emacs/rdebug-varbuf.el +150 -0
  47. data/emacs/rdebug-vars.el +125 -0
  48. data/emacs/rdebug-watch.el +132 -0
  49. data/emacs/rdebug.el +326 -0
  50. data/emacs/test/elk-test.el +242 -0
  51. data/emacs/test/test-annotate.el +103 -0
  52. data/emacs/test/test-cmd.el +116 -0
  53. data/emacs/test/test-core.el +104 -0
  54. data/emacs/test/test-fns.el +65 -0
  55. data/emacs/test/test-frames.el +62 -0
  56. data/emacs/test/test-gud.el +35 -0
  57. data/emacs/test/test-indent.el +58 -0
  58. data/emacs/test/test-regexp.el +144 -0
  59. data/emacs/test/test-shortkey.el +61 -0
  60. data/ext/ruby_debug/breakpoint.c +630 -0
  61. data/ext/ruby_debug/extconf.rb +11 -0
  62. data/ext/ruby_debug/ruby_debug.c +2203 -0
  63. data/ext/ruby_debug/ruby_debug.h +151 -0
  64. data/lib/debugger.rb +5 -0
  65. data/lib/debugger/version.rb +5 -0
  66. data/lib/debugger2.rb +6 -0
  67. data/lib/ruby-debug-base.rb +307 -0
  68. data/lib/ruby-debug.rb +176 -0
  69. data/lib/ruby-debug/command.rb +227 -0
  70. data/lib/ruby-debug/commands/breakpoints.rb +153 -0
  71. data/lib/ruby-debug/commands/catchpoint.rb +55 -0
  72. data/lib/ruby-debug/commands/condition.rb +49 -0
  73. data/lib/ruby-debug/commands/continue.rb +38 -0
  74. data/lib/ruby-debug/commands/control.rb +107 -0
  75. data/lib/ruby-debug/commands/display.rb +120 -0
  76. data/lib/ruby-debug/commands/edit.rb +48 -0
  77. data/lib/ruby-debug/commands/enable.rb +202 -0
  78. data/lib/ruby-debug/commands/eval.rb +176 -0
  79. data/lib/ruby-debug/commands/finish.rb +42 -0
  80. data/lib/ruby-debug/commands/frame.rb +301 -0
  81. data/lib/ruby-debug/commands/help.rb +56 -0
  82. data/lib/ruby-debug/commands/info.rb +467 -0
  83. data/lib/ruby-debug/commands/irb.rb +123 -0
  84. data/lib/ruby-debug/commands/jump.rb +66 -0
  85. data/lib/ruby-debug/commands/kill.rb +51 -0
  86. data/lib/ruby-debug/commands/list.rb +94 -0
  87. data/lib/ruby-debug/commands/method.rb +84 -0
  88. data/lib/ruby-debug/commands/quit.rb +39 -0
  89. data/lib/ruby-debug/commands/reload.rb +40 -0
  90. data/lib/ruby-debug/commands/save.rb +90 -0
  91. data/lib/ruby-debug/commands/set.rb +223 -0
  92. data/lib/ruby-debug/commands/show.rb +247 -0
  93. data/lib/ruby-debug/commands/skip.rb +35 -0
  94. data/lib/ruby-debug/commands/source.rb +36 -0
  95. data/lib/ruby-debug/commands/stepping.rb +81 -0
  96. data/lib/ruby-debug/commands/threads.rb +189 -0
  97. data/lib/ruby-debug/commands/tmate.rb +36 -0
  98. data/lib/ruby-debug/commands/trace.rb +57 -0
  99. data/lib/ruby-debug/commands/variables.rb +199 -0
  100. data/lib/ruby-debug/debugger.rb +5 -0
  101. data/lib/ruby-debug/helper.rb +69 -0
  102. data/lib/ruby-debug/interface.rb +232 -0
  103. data/lib/ruby-debug/processor.rb +474 -0
  104. data/man/rdebug.1 +241 -0
  105. data/old_scripts/Makefile.am +14 -0
  106. data/old_scripts/README.md +2 -0
  107. data/old_scripts/autogen.sh +4 -0
  108. data/old_scripts/configure.ac +12 -0
  109. data/old_scripts/rdbg.rb +33 -0
  110. data/old_scripts/runner.sh +7 -0
  111. data/old_scripts/svn2cl_usermap +3 -0
  112. data/test/.cvsignore +1 -0
  113. data/test/breakpoints_test.rb +366 -0
  114. data/test/conditions_test.rb +77 -0
  115. data/test/continue_test.rb +28 -0
  116. data/test/display_test.rb +143 -0
  117. data/test/edit_test.rb +55 -0
  118. data/test/eval_test.rb +94 -0
  119. data/test/examples/breakpoint1.rb +15 -0
  120. data/test/examples/breakpoint2.rb +7 -0
  121. data/test/examples/conditions.rb +4 -0
  122. data/test/examples/continue.rb +4 -0
  123. data/test/examples/display.rb +5 -0
  124. data/test/examples/edit.rb +3 -0
  125. data/test/examples/edit2.rb +3 -0
  126. data/test/examples/eval.rb +4 -0
  127. data/test/examples/finish.rb +20 -0
  128. data/test/examples/frame.rb +31 -0
  129. data/test/examples/help.rb +2 -0
  130. data/test/examples/info.rb +48 -0
  131. data/test/examples/info2.rb +3 -0
  132. data/test/examples/irb.rb +6 -0
  133. data/test/examples/jump.rb +14 -0
  134. data/test/examples/kill.rb +2 -0
  135. data/test/examples/list.rb +12 -0
  136. data/test/examples/method.rb +15 -0
  137. data/test/examples/post_mortem.rb +19 -0
  138. data/test/examples/quit.rb +2 -0
  139. data/test/examples/reload.rb +6 -0
  140. data/test/examples/restart.rb +6 -0
  141. data/test/examples/save.rb +3 -0
  142. data/test/examples/set.rb +3 -0
  143. data/test/examples/set_annotate.rb +12 -0
  144. data/test/examples/settings.rb +1 -0
  145. data/test/examples/show.rb +2 -0
  146. data/test/examples/source.rb +3 -0
  147. data/test/examples/stepping.rb +21 -0
  148. data/test/examples/thread.rb +32 -0
  149. data/test/examples/tmate.rb +10 -0
  150. data/test/examples/trace.rb +7 -0
  151. data/test/examples/trace_threads.rb +20 -0
  152. data/test/examples/variables.rb +26 -0
  153. data/test/finish_test.rb +49 -0
  154. data/test/frame_test.rb +140 -0
  155. data/test/help_test.rb +51 -0
  156. data/test/info_test.rb +326 -0
  157. data/test/irb_test.rb +82 -0
  158. data/test/jump_test.rb +70 -0
  159. data/test/kill_test.rb +49 -0
  160. data/test/list_test.rb +147 -0
  161. data/test/method_test.rb +72 -0
  162. data/test/post_mortem_test.rb +25 -0
  163. data/test/quit_test.rb +56 -0
  164. data/test/reload_test.rb +47 -0
  165. data/test/restart_test.rb +145 -0
  166. data/test/save_test.rb +94 -0
  167. data/test/set_test.rb +183 -0
  168. data/test/show_test.rb +294 -0
  169. data/test/source_test.rb +46 -0
  170. data/test/stepping_test.rb +122 -0
  171. data/test/support/breakpoint.rb +12 -0
  172. data/test/support/context.rb +14 -0
  173. data/test/support/matchers.rb +67 -0
  174. data/test/support/mocha_extensions.rb +71 -0
  175. data/test/support/processor.rb +7 -0
  176. data/test/support/test_dsl.rb +206 -0
  177. data/test/support/test_interface.rb +66 -0
  178. data/test/test_helper.rb +9 -0
  179. data/test/thread_test.rb +124 -0
  180. data/test/tmate_test.rb +45 -0
  181. data/test/trace_test.rb +156 -0
  182. data/test/variables_test.rb +116 -0
  183. metadata +319 -0
@@ -0,0 +1,3791 @@
1
+ \input texinfo @c -*-texinfo-*-
2
+ @setfilename ruby-debug.info
3
+
4
+ @set DBG ruby-debug
5
+ @set ttrdebug @code{rdebug}
6
+ @set ttDBG @code{@value{DBG}}
7
+ @set Emacs @sc{gnu} Emacs
8
+
9
+ @c Karl Berry informs me that this will add straight quotes in
10
+ @c typewriter text.
11
+ @c See the "Inserting Quote Characters" node in the Texinfo manual
12
+ @set txicodequoteundirected
13
+ @set txicodequotebacktick
14
+ @set RDEBUG_VERSION 0.11.6
15
+
16
+ @macro Example {}
17
+ @iftex
18
+ @cartouche
19
+ @end iftex
20
+ @smallexample
21
+ @end macro
22
+
23
+ @macro EndExample {}
24
+ @iftex
25
+ @end cartouche
26
+ @end iftex
27
+ @end smallexample
28
+ @end macro
29
+
30
+ @macro DBG {}
31
+ @value{DBG}
32
+ @end macro
33
+
34
+ @macro ttDBG {}
35
+ @value{ttrdebug}
36
+ @end macro
37
+
38
+ @c How to show optional variables.
39
+ @macro ovar{varname}
40
+ @r{[}@var{\varname\}@r{]}
41
+ @end macro
42
+
43
+ @settitle ruby-debug
44
+ @setchapternewpage odd
45
+ @c %**end of header
46
+
47
+ @include version.texi
48
+
49
+ @finalout
50
+
51
+ @c THIS MANUAL REQUIRES TEXINFO 4.0 OR LATER.
52
+
53
+ @c This is a dir.info fragment to support semi-automated addition of
54
+ @c manuals to an info tree.
55
+ @dircategory Programming & development tools.
56
+ @direntry
57
+ * ruby-debug: (ruby-debug). Ruby Debugger
58
+ @end direntry
59
+
60
+ @titlepage
61
+ @title Debugging with @code{ruby-debug}
62
+ @sp 1
63
+ @subtitle @value{EDITION} Edition
64
+ @subtitle @value{UPDATED-MONTH}
65
+ @author Rocky Bernstein, Kent Sibilev, and Mark Moseley
66
+ @page
67
+ @ifset WHERETO
68
+ @tex
69
+ {\parskip=0pt
70
+ \hfill (Send bugs and comments on ruby-debug to fill in...)\par
71
+ \hfill {\it Debugging with {\tt ruby-debug}\par
72
+ \hfill \TeX{}info \texinfoversion\par
73
+ }
74
+ @end tex
75
+ @end ifset
76
+ @end titlepage
77
+ @page
78
+
79
+ @ifnottex
80
+ @node Top, Summary, (dir), (dir)
81
+ @top Debugging with ruby-debug
82
+
83
+ This file describes ruby-debug, the Ruby Debugger,
84
+ version @value{RDEBUG_VERSION}
85
+
86
+ This is the @value{EDITION} Edition, @value{UPDATED}
87
+ @c Copyright (C) 2007 ...
88
+
89
+ @menu
90
+ * Summary:: Overview of Debugger with sample sessions
91
+ * Invocation:: Getting in and out
92
+ * Debugger Command Reference:: rdebug command reference
93
+ * Post-Mortem Debugging:: Debugging on an uncaught exception
94
+ * Debugger Module and Class:: ruby-debug's Debugger module and class
95
+
96
+ Appendix
97
+ * Using from GitHub::
98
+
99
+ Indexes (nodes containing large menus)
100
+ * Class Module Index:: An item for Class, Module, and Methods.
101
+ * Command Index:: An item for each command name.
102
+ * General Index:: An item for each concept.
103
+ @end menu
104
+
105
+ @end ifnottex
106
+
107
+ @contents
108
+
109
+ @node Summary
110
+ @chapter Summary of @code{ruby-debug}
111
+
112
+ The purpose of a debugger such as @DBG{} is to allow you to see what is
113
+ going on ``inside'' a Ruby program while it executes.
114
+
115
+ @ttDBG{} can do four main kinds of things (plus other things in support of
116
+ these) to help you catch bugs in the act:
117
+
118
+ @itemize @bullet
119
+ @item
120
+ Start your script, specifying anything that might affect its behavior.
121
+
122
+ @item
123
+ Make your script stop on specified conditions.
124
+
125
+ @item
126
+ Examine what has happened, when your script has stopped.
127
+
128
+ @item
129
+ Change things in your script, so you can experiment with correcting the
130
+ effects of one bug and go on to learn about another.
131
+ @end itemize
132
+
133
+ Although you can use @value{ttrdebug} to invoke your Ruby programs via
134
+ a debugger at the outset, there are other ways to use and enter the
135
+ debugger.
136
+
137
+ @menu
138
+ * First Sample Session:: A Simple Sample @code{rdebug} session
139
+ * Second Sample Session:: Second Session Delving a little deeper @code{rdebug} session
140
+ * Unit Testing Session:: Using the debugger in unit testing
141
+ * Debugger.start with a block:: Using the Debugger.start with a block
142
+ * Debugging Oddities:: How debugging Ruby may be different...
143
+ @end menu
144
+
145
+ @node First Sample Session
146
+ @section The First Sample @code{rdebug} Session (@code{list}, @code{display}, @code{print}, and @code{quit})
147
+
148
+ You can use this manual at your leisure to read all about @value{ttDBG}.
149
+ However, a handful of commands are enough to get started using the
150
+ debugger. The following sections illustrates these commands.
151
+
152
+ @iftex
153
+ In this sample session, we emphasize user input like this: @b{input},
154
+ to make it easier to pick out from the surrounding output.
155
+ @end iftex
156
+
157
+ Below is Ruby code to compute a triangle number of a given
158
+ length.@footnote{There are of course shorter ways to define @code{triangle}
159
+ such as:
160
+ @smallexample
161
+ def triangle(n) (n * (n+1)) / 2 end
162
+ @end smallexample
163
+ The code we use in this example and the next is more for pedagogical
164
+ purposes than how to write short Ruby code.}
165
+
166
+
167
+ @smallexample
168
+ $ @b{rdebug triangle.rb}
169
+ triangle.rb:4 def hanoi(n,a,b,c)
170
+ (rdb:1) @b{list}
171
+ [-1, 8] in ./triangle.rb
172
+ 1 #!/usr/bin/env ruby
173
+ 2 # Compute the n'th triangle number - the hard way
174
+ 3 # triangle(n) == (n * (n+1)) / 2
175
+ => 4 def triangle(n)
176
+ 5 tri = 0
177
+ 6 0.upto(n) do |i|
178
+ 7 tri += i
179
+ 8 end
180
+ (rdb:1) @b{l}
181
+ [9, 18] in ./triangle.rb
182
+ 9 tri
183
+ 10 end
184
+ 11
185
+ 12 t = triangle(3)
186
+ 13 puts t
187
+ (rdb:1) @b{list 1,100}
188
+ [1, 100] in ./triangle.rb
189
+ 1 #!/usr/bin/env ruby
190
+ 2 # Compute the n'th triangle number - the hard way
191
+ 3 # triangle(n) == (n * (n+1)) / 2
192
+ => 4 def triangle(n)
193
+ 5 tri = 0
194
+ 6 0.upto(n) do |i|
195
+ 7 tri += i
196
+ 8 end
197
+ 9 tri
198
+ 10 end
199
+ 11
200
+ 12 t = triangle(3)
201
+ 13 puts t
202
+ (rdb:1)
203
+ @end smallexample
204
+
205
+ @noindent
206
+
207
+ There are lots of command options, but we don't need them for now. See
208
+ @ref{rdebug command-line options} for a full list of command options.
209
+
210
+ Position information consists of a filename and line number,
211
+ e.g.@: @code{triangle.rb:4}. We are currently stopped before the first
212
+ executable line of the program; this is line 4 of
213
+ @code{triangle.rb}. If you are used to less dynamic languages and have
214
+ used debuggers for more statically compiled languages like C, C++, or
215
+ Java, it may seem odd to be stopped before a function definition. But
216
+ in Ruby line 4 is executed, the name @code{triangle} (probably) does
217
+ not exist so issuing a method call of @code{triangle} will raise a
218
+ ``method not found'' error.
219
+
220
+ @DBG{}'s prompt is @code{(rdb:@emph{n})}. The @emph{n} is the thread
221
+ number. Here it is 1 which is usually the case for the main thread. If
222
+ the program has died and you are in post-mortem debugging, there is no
223
+ thread number. In this situation, the string @code{post-mortem} is
224
+ used in place of a thread number. If the program has terminated
225
+ normally, the string this position will be @code{ctrl}. The commands
226
+ which are available change depending on the program state.
227
+
228
+ The first command, @code{list} (@pxref{List}), prints 10 lines
229
+ centered around the current line; the current line here is line 4 and
230
+ is marked by @code{=>}, so the range the debugger would like to show
231
+ is -1..8. However since there aren't 5 lines before the current line,
232
+ those additional lines---``lines'' -1 and 0---are dropped and we print
233
+ the remaining 8 lines. The @code{list} command can be abbreviated
234
+ with @code{l} which is what we use next. Notice that when we use this
235
+ a second time, we continue listing from the place we last left
236
+ off. The desired range of lines this time is lines 9 to 18; but since
237
+ the program ends at line 13, only the remaining 5 lines are shown.
238
+
239
+ If you want to set how many lines to print by default rather than use
240
+ the initial number of lines, 10, use the @code{set listsize} command
241
+ (@pxref{Listsize}). To see the entire program in one shot, we gave an
242
+ explicit starting and ending line number.
243
+
244
+ If you use a front-end to the debugger such as the Emacs interface,
245
+ @c (@pxref{GNU Emacs})
246
+ you probably won't use @code{list} all that much.
247
+
248
+ Now let us step through the program.
249
+
250
+ @smallexample
251
+ (rdb:1) @b{step}
252
+ triangle.rb:12
253
+ t = triangle(3)
254
+ (rdb:1) @b{@key{<RET>}}
255
+ triangle.rb:5
256
+ tri = 0
257
+ (rdb:1) @b{p tri}
258
+ nil
259
+ (rdb:1) @b{step}
260
+ triangle.rb:6
261
+ 0.upto(n) do |i|
262
+ (rdb:1) @b{p tri}
263
+ 0
264
+ @end smallexample
265
+
266
+ The first @kbd{step} command (@pxref{Step}) runs the script one
267
+ executable unit. The second command we entered was just hitting the
268
+ return key; @ttDBG{} remembers the last command you entered was
269
+ @code{step}, so it runs that last command again.
270
+
271
+ One way to print the values of variables uses @code{p}. (Of course,
272
+ there are of course lots of other ways too.). When we look at the
273
+ value of @code{tri} the first time, we see it is @code{nil}. Again we
274
+ are stopped @emph{before} the assignment on line 5, and this variable
275
+ hasn't been set previously. However after issuing another ``step''
276
+ command we see that the value is 0 as expected.
277
+
278
+ However if every time we stop we want to see the value of @code{tri}
279
+ to see how things are going stop, there is a better way by setting a
280
+ display expression (@pxref{DisplayCommands}).
281
+
282
+ @smallexample
283
+ (rdb:1) @b{display tri}
284
+ 1: tri = 0
285
+ @end smallexample
286
+
287
+ Now let us run the program until we return from the function. However
288
+ we'll want to see which lines get run.
289
+
290
+ @smallexample
291
+ (rdb:1) @b{display i}
292
+ 2: i =
293
+ (rdb:1) @b{set linetrace on}
294
+ line tracing is on.
295
+ (rdb:1) @b{finish}
296
+ Tracing(1):triangle.rb:7 tri += i
297
+ 1: tri = 0
298
+ 2: i = 0
299
+ Tracing(1):triangle.rb:7 tri += i
300
+ 1: tri = 0
301
+ 2: i = 1
302
+ Tracing(1):triangle.rb:7 tri += i
303
+ 1: tri = 1
304
+ 2: i = 2
305
+ Tracing(1):triangle.rb:7 tri += i
306
+ 1: tri = 3
307
+ 2: i = 3
308
+ Tracing(1):triangle.rb:9 tri
309
+ 1: tri = 6
310
+ 2: i = 3
311
+ 6
312
+ Tracing(1):triangle.rb:13 puts t
313
+ 1: tri =
314
+ 2: i =
315
+ 1: tri =
316
+ 2: i =
317
+ triangle.rb:13
318
+ puts t
319
+ (rdb:1) @b{quit}
320
+ Really quit? (y/n) @b{y}
321
+ @end smallexample
322
+
323
+ So far, so good. A you can see from the above to get out of the
324
+ debugger, one can issue a @code{quit} command. (@code{q} and
325
+ @code{exit} are just as good. If you want to quit without being
326
+ prompted, suffix the command with an exclamation mark, e.g.\@code{q!}.
327
+
328
+ @node Second Sample Session
329
+ @section Sample Session 2: Delving Deeper (@code{where}, @code{frame}, @code{restart}, @code{autoeval}, @code{break}, @code{ps})
330
+
331
+ In this section we'll introduce breakpoints, the call stack and
332
+ restarting. So far we've been doing pretty good in that we've not
333
+ encountered a bug to fix. Let's try another simple example. Okay
334
+ here's the program.
335
+
336
+ Below we will debug a simple Ruby program to solve the classic Towers
337
+ of Hanoi puzzle. It is augmented by the bane of programming: some
338
+ command-parameter processing with error checking.
339
+
340
+ @smallexample
341
+ $ @b{rdebug hanoi.rb}
342
+ hanoi.rb:3 def hanoi(n,a,b,c)
343
+ (rdb:1) @b{list 1,100}
344
+ [1, 100] in ./hanoi.rb
345
+ 1 #!/usr/bin/ruby
346
+ 2
347
+ => 3 def hanoi(n,a,b,c)
348
+ 4 if n-1 > 0
349
+ 5 hanoi(n-1, a, c, b)
350
+ 6 end
351
+ 7 puts "Move disk %s to %s" % [a, b]
352
+ 8 if n-1 > 0
353
+ 9 hanoi(n-1, c, b, a)
354
+ 10 end
355
+ 11 end
356
+ 12
357
+ 13 i_args=ARGV.length
358
+ 14 if i_args > 1
359
+ 15 puts "*** Need number of disks or no parameter"
360
+ 16 exit 1
361
+ 17 end
362
+ 18
363
+ 19 n=3
364
+ 20
365
+ 21 if i_args > 0
366
+ 22 begin
367
+ 23 n = ARGV[0].to_i
368
+ 24 rescue ValueError, msg
369
+ 25 print "** Expecting an integer, got: %s" % ARGV[0].to_s
370
+ 26 exit 2
371
+ 27 end
372
+ 28 end
373
+ 29
374
+ 30 if n < 1 or n > 100
375
+ 31 puts "*** number of disks should be between 1 and 100"
376
+ 32 exit 2
377
+ 33 end
378
+ 34
379
+ 35 hanoi(n, :a, :b, :c)
380
+ (rdb:1)
381
+ @end smallexample
382
+
383
+ Recall in the first section I said that before the @code{def} is run
384
+ the method it names is undefined. Let's check that out. First let's
385
+ see what private methods we can call before running @code{def hanoi}
386
+
387
+ @smallexample
388
+ (rdb:1) @b{set autoeval on}
389
+ autoeval is on.
390
+ (rdb:1) @b{private_methods}
391
+ [:require_relative, :Digest, :default_src_encoding, :debug_program, ...
392
+ @end smallexample
393
+
394
+ The @code{set autoeval} (@pxref{Autoeval}) command causes any commands
395
+ that are not normally understood to be debugger commands to get
396
+ evaluated as though they were Ruby commands. I use this a lot, so I
397
+ set this by putting it the command file @code{.rdebugrc},
398
+ @pxref{Command Files}, that gets read when @code{ruby-debug} starts.
399
+
400
+ As showing the list output of @code{private_methods}, I find this kind
401
+ of list unwieldy. What you are supposed to notice here is that
402
+ method @code{hanoi} is not in this list. When you ask
403
+ @code{ruby-debug} for a list of method names via @code{method
404
+ instance}, it doesn't show output in this way; @code{ruby-debug} can
405
+ sort and put into columns lists like this using the print command, @code{ps}.
406
+
407
+
408
+ @smallexample
409
+ (rdb:1) @b{ps private_methods}
410
+ Array debug_program p spawn
411
+ Complex default_src_encoding pp sprintf
412
+ Digest eval print srand
413
+ Float exec printf syscall
414
+ Integer exit proc system
415
+ Pathname exit! process_options test
416
+ Rational fail putc throw
417
+ String fork puts trace_var
418
+ __callee__ format raise trap
419
+ __method__ gem rand untrace_var
420
+ ` gets readline warn
421
+ abort global_variables readlines whence_file
422
+ at_exit initialize remove_instance_variable y
423
+ autoload initialize_copy require
424
+ autoload? iterator? require_relative
425
+ binding lambda select
426
+ block_given? load set_trace_func
427
+ caller local_variables singleton_method_added
428
+ catch loop singleton_method_removed
429
+ dbg_print method_missing singleton_method_undefined
430
+ dbg_puts open sleep
431
+ @end smallexample
432
+
433
+ Now let's see what happens after stepping:
434
+
435
+ @smallexample
436
+ (rdb:1) @b{private_methods.member?(:hanoi)}
437
+ false
438
+ (rdb:1) @b{step}
439
+ hanoi.rb:13
440
+ i_args=ARGV.length
441
+ (rdb:1) @b{private_methods.member?(:hanoi)}
442
+ true
443
+ (rdb:1)
444
+ @end smallexample
445
+
446
+ Okay, now where were we?
447
+
448
+ @smallexample
449
+ (rdb:1) @b{list}
450
+ [8, 17] in ./hanoi.rb
451
+ 8 if n-1 > 0
452
+ 9 hanoi(n-1, c, b, a)
453
+ 10 end
454
+ 11 end
455
+ 12
456
+ => 13 i_args=ARGV.length
457
+ 14 if i_args > 1
458
+ 15 puts "*** Need number of disks or no parameter"
459
+ 16 exit 1
460
+ 17 end
461
+ (rdb:1) @b{ARGV}
462
+ []
463
+ @end smallexample
464
+
465
+ Ooops. We forgot to specify any parameters to this program. Let's try
466
+ again. We can use the @code{restart} command here.
467
+
468
+ @smallexample
469
+ (rdb:1) @b{restart 3}
470
+ Re exec'ing:
471
+ /usr/bin/rdebug hanoi.rb 3
472
+ hanoi.rb:3
473
+ def hanoi(n,a,b,c)
474
+ (rdb:1) @b{break 4}
475
+ Breakpoint 1 file hanoi.rb, line 4
476
+ (rdb:1) @b{continue}
477
+ Breakpoint 1 at hanoi.rb:4
478
+ ./hanoi.rb:4 if n-1 > 0
479
+ (rdb:1) @b{display n}
480
+ 1: n = 3
481
+ (rdb:1) @b{display a}
482
+ 2: a = a
483
+ (rdb:1) @b{undisplay 2}
484
+ (rdb:1) @b{display a.inspect}
485
+ 3: a.inspect = :a
486
+ (rdb:1) @b{display b.inspect}
487
+ 4: b.inspect = :b
488
+ (rdb:1) @b{continue}
489
+ Breakpoint 1 at hanoi.rb:4
490
+ 1: n = 2
491
+ 3: a.inspect = :a
492
+ 4: b.inspect = :c
493
+ ./hanoi.rb:4
494
+ if n-1 > 0
495
+ (rdb:1) @b{c}
496
+ Breakpoint 1 at hanoi.rb:4
497
+ 1: n = 1
498
+ 3: a.inspect = :a
499
+ 4: b.inspect = :b
500
+ ./hanoi.rb:4
501
+ if n-1 > 0
502
+ (rdb:1) @b{where}
503
+ --> #0 Object.hanoi(n#Fixnum, a#Symbol, b#Symbol, c#Symbol) at line hanoi.rb:4
504
+ #1 Object.hanoi(n#Fixnum, a#Symbol, b#Symbol, c#Symbol) at line hanoi.rb:5
505
+ #2 Object.hanoi(n#Fixnum, a#Symbol, b#Symbol, c#Symbol) at line hanoi.rb:5
506
+ #3 at line hanoi.rb:35
507
+ (rdb:1)
508
+ @end smallexample
509
+
510
+ In the above we added a new command, @code{break}
511
+ (@pxref{Breakpoints}) which indicates to go into the debugger just
512
+ before that line of code is run. And @code{continue} resumes
513
+ execution. Notice the difference between @code{display a} and
514
+ @code{display a.inspect}. An implied string conversion is performed on
515
+ the expression after it is evaluated. To remove a display expression
516
+ we used @code{undisplay} is used. If we give a display number, just
517
+ that display expression is removed.
518
+
519
+ Above we also used a new command @code{where} (@pxref{Backtrace} to
520
+ show the call stack. In the above situation, starting from the bottom
521
+ line we see we called the hanoi from line 35 of the file
522
+ @code{hanoi.rb} and the hanoi method called itself two more times at
523
+ line 5.
524
+
525
+ In the call stack we show the file line position in the same format
526
+ when we stop at a line. Also we see the names of the parameters and
527
+ the types that those parameters @emph{currently} have. It's possible
528
+ that when the program was called the parameter had a different type,
529
+ since the types of variables can change dynamically. You alter the
530
+ style of what to show in the trace (@pxref{Callstyle}).
531
+
532
+ Let's explore a little more. Now were were we?
533
+
534
+ @smallexample
535
+ (rdb:1) @b{list}
536
+ 1 #!/usr/bin/ruby
537
+ 2
538
+ 3 def hanoi(n,a,b,c)
539
+ => 4 if n-1 > 0
540
+ 5 hanoi(n-1, a, c, b)
541
+ 6 end
542
+ 7 puts "Move disk %s to %s" % [a, b]
543
+ 8 if n-1 > 0
544
+ (rdb:1) @b{undisplay}
545
+ Clear all expressions? (y/n) @b{y}
546
+ (rdb:1) @b{i_args}
547
+ NameError Exception: undefined local variable or method `i_args' for main:Object
548
+ (rdb:1) @b{frame -1}
549
+ #3 at line hanoi.rb:35
550
+ (rdb:1) @b{i_args}
551
+ 1
552
+ (rdb:1) @b{p n}
553
+ 3
554
+ (rdb:1) @b{down 2}
555
+ #2 Object.hanoi(n#Fixnum, a#Symbol, b#Symbol, c#Symbol) at line hanoi.rb:5
556
+ (rdb:1) @b{p n}
557
+ 2
558
+ @end smallexample
559
+
560
+ Notice in the above to get the value of variable @code{n}, I have to
561
+ use a print command like @code{p n}; If I entered just @code{n}, that
562
+ would be taken to mean the debugger command ``next''. In the current
563
+ scope, variable @code{i_args} is not defined. However I can change to
564
+ the top-most frame by using the @code{frame} command. Just as with
565
+ arrays, -1 means the last one. Alternatively using frame number 3
566
+ would have been the same thing; so would issuing @code{up 3}.
567
+
568
+ Note that in the outside frame 3, the value of @code{i_args} can be
569
+ shown. Also note that the value of variable @code{n} is different.
570
+
571
+ @node Unit Testing Session
572
+ @section Using the debugger in unit testing (@code{ruby-debug/debugger}, @code{Debugger.start})
573
+
574
+ In the previous sessions we've been calling the debugger right at the
575
+ outset. I confess that this mode of operation is usually not how I use
576
+ the debugger.
577
+
578
+ There are a number of situations where calling the debugger at the outset is
579
+ impractical for a couple of reasons.
580
+
581
+ @enumerate
582
+ @item
583
+ The debugger just doesn't work when run at the outset. By necessity
584
+ any debugging changes to the behavior or the program in slight and
585
+ subtle ways, and sometimes this can hinder finding bugs.
586
+ @item
587
+ There's a lot of code which that needs to get run before the part you
588
+ want to inspect. Running this code takes time and you don't want the
589
+ overhead of the debugger.
590
+ @end enumerate
591
+
592
+ In this section we'll show how to enter the code in the middle
593
+ of your program, while delving more into the debugger operation.
594
+
595
+ In this section we will also use unit testing. Using unit tests will
596
+ greatly reduce the amount of debugging needed, while at the same time,
597
+ will increase the quality of your program.
598
+
599
+ What we'll do is take the @code{triangle} code from the first session
600
+ and write a unit test for that. In a sense we did write a mini-test
601
+ for the program which was basically the last line where we printed the
602
+ value of triangle(3). This test however wasn't automated: the
603
+ implication is that someone would look at the output and verify that
604
+ what was printed is what was expected.
605
+
606
+ And before we can turn that into something that can be
607
+ @code{required}, we probably want to remove that output. However I
608
+ like to keep in that line so that when I look at the file, I have an
609
+ example of how to run it. Therefore we will conditionally run this
610
+ line if that file is invoked directly, but skip it if it is
611
+ not.@footnote{@code{rdebug} resets @code{$0} to try to make things
612
+ like this work.}
613
+ @smallexample
614
+ if __FILE__ == $0
615
+ t = triangle(3)
616
+ puts t
617
+ end
618
+ @end smallexample
619
+
620
+ Let's call this file @code{tri2.rb}.
621
+
622
+ Okay, we're now ready to write our unit test. We'll use
623
+ @code{"test/unit"} which comes with the standard Ruby distribution.
624
+ Here's the test code; it should be in the same directory as tri2.rb.
625
+
626
+ @smallexample
627
+ #!/usr/bin/env ruby
628
+ require 'test/unit'
629
+ require_relative './tri2.rb'
630
+
631
+ class TestTri < Test::Unit::TestCase
632
+ def test_basic
633
+ solutions = []
634
+ 0.upto(5) do |i|
635
+ solutions << triangle(i)
636
+ end
637
+ assert_equal([0, 1, 3, 6, 10, 15], solutions,
638
+ 'Testing the first 5 triangle numbers')
639
+ end
640
+ end
641
+ @end smallexample
642
+
643
+ If you run it will work. However if you run @code{rdebug} initially,
644
+ you will not get into the test, because @code{test/unit} wants to be
645
+ the main program. So here is a situation where one may need to modify
646
+ the program to add an explicit @emph{entry} into the
647
+ debugger.@footnote{For some versions of rake and @code{rdebug} you can
648
+ in fact set a breakpoint after running @code{rdebug}
649
+ initially. Personally though I find it much simpler and more reliable
650
+ to modify the code as shown here.}
651
+
652
+ One way to do this is to add the following before the place you want
653
+ to stop:
654
+ @smallexample
655
+ require 'ruby-debug'
656
+ debugger
657
+ @end smallexample
658
+
659
+ Let's add this code just after entering @code{test_basic}:
660
+ @smallexample
661
+ ...
662
+ def test_basic
663
+ @b{require "ruby-debug"}
664
+ @b{debugger}
665
+ solutions = []
666
+ ...
667
+ @end smallexample
668
+
669
+ Now we run the program..
670
+ @smallexample
671
+ $ @b{ruby test-tri.rb}
672
+ Loaded suite test-tri
673
+ Started
674
+ test-tri.rb:9
675
+ solutions = []
676
+ (rdb:1)
677
+ @end smallexample
678
+ and we see that we are stopped at line 9 just before the
679
+ initialization of the list @code{solutions}.
680
+
681
+ Now let's see where we are...
682
+
683
+ @smallexample
684
+ (rdb:1) @b{where}
685
+ --> #0 TestTri.test_basic at line /home/rocky/ruby/test-tri.rb:9
686
+ (rdb:1)
687
+ @end smallexample
688
+
689
+ Something seems wrong here; @code{TestTri.test_basic} indicates that
690
+ we are in class @code{TestTri} in method @code{test_basic}. However we
691
+ don't see the call to this like we did in the last example when we
692
+ used the @code{where} command. This is because the debugger really
693
+ didn't spring into existence until after we already entered that
694
+ method, and Ruby doesn't keep call stack information around in a
695
+ way that will give the information we show when running @code{where}.
696
+
697
+ If we want call stack information, we have to turn call-stack tracking
698
+ on @emph{beforehand}. This is done by adding @code{Debugger.start}.
699
+
700
+ Here's what our test program looks like so after we modify it to start
701
+ tracking calls from the outset
702
+
703
+ @smallexample
704
+ #!/usr/bin/env ruby
705
+ require 'test/unit'
706
+ require 'tri2.rb'
707
+ require 'ruby-debug'
708
+ @b{Debugger.start}
709
+
710
+ class TestTri < Test::Unit::TestCase
711
+ def test_basic
712
+ @b{debugger}
713
+ solutions = []
714
+ 0.upto(5) do |i|
715
+ solutions << triangle(i)
716
+ end
717
+ assert_equal([0, 1, 3, 6, 10, 15], solutions,
718
+ "Testing the first 5 triangle numbers")
719
+ end
720
+ end
721
+ @end smallexample
722
+
723
+ Now when we run this:
724
+ @smallexample
725
+ $ @b{ruby test-tri2.rb}
726
+ Loaded suite test-tri2
727
+ Started
728
+ test-tri2.rb:11
729
+ solutions = []
730
+ (rdb:1) @b{where}
731
+ --> #0 TestTri.test_basic at line test-tri2.rb:11
732
+ #1 MiniTest::Unit::TestCase.run(runner#MiniTest::Unit)
733
+ at line /usr/local/lib/ruby/1.9.1/minitest/unit.rb:458
734
+ #2 MiniTest::Unit.run_test_suites
735
+ at line /usr/local/lib/ruby/1.9.1/minitest/unit.rb:426
736
+ #3 MiniTest::Unit.run
737
+ at line /usr/local/lib/ruby/1.9.1/minitest/unit.rb:393
738
+ #4 at line /usr/local/lib/ruby/1.9.1/minitest/unit.rb:334
739
+ (rdb:1)
740
+ @end smallexample
741
+
742
+ Much better. But again let me emphasize that the parameter types are
743
+ those of the corresponding variables that @emph{currently} exist, and
744
+ this might have changed since the time when the call was made.
745
+
746
+ @node Debugger.start with a block
747
+ @section Using the @code{Debugger.start} with a block
748
+
749
+ We saw that @code{Debugger.start()} and @code{Debugger.stop()} allow
750
+ fine-grain control over where the debugger tracking should occur.
751
+
752
+ Rather than use an explicit @code{stop()}, you can also pass a block
753
+ to the @code{start()} method. This causes @code{start()} to run and
754
+ then @code{yield} to that block. When the block is finished,
755
+ @code{stop()} is run. In other words, this wraps a
756
+ @code{Debugger.start()} and @code{Debugger.stop()} around the block of
757
+ code. But it also has a side benefit of ensuring that in the presence
758
+ of an uncaught exception @code{stop} is run, without having to
759
+ explicitly use @code{begin} ... @code{ensure Debugger.stop() end}.
760
+
761
+ For example, in Ruby Rails you might want to debug code in one of the
762
+ controllers without causing any slowdown to any other code. And
763
+ this can be done by wrapping the controller in a @code{start()} with a
764
+ block; when the method wrapped this way finishes the debugger is
765
+ turned off, and the application proceeds at regular speed.
766
+
767
+ Of course, inside the block you will probably want to enter the
768
+ debugger using @code{Debugger.debugger()}, otherwise there would
769
+ little point in using the @code{start}. For example, you can do this
770
+ in @code{irb}:
771
+
772
+ @smallexample
773
+ $ @b{irb}
774
+ irb(main):001:0> @b{require 'ruby-debug'}
775
+ => true
776
+ irb(main):002:0> @b{def foo}
777
+ irb(main):003:1> @b{x=1}
778
+ irb(main):004:1> @b{puts 'foo'}
779
+ irb(main):005:1> @b{end}
780
+ => nil
781
+ irb(main):006:0> @b{Debugger.start@{debugger; foo@}}
782
+ (irb):3
783
+
784
+ (rdb:1) @b{s}
785
+ (irb):4
786
+
787
+ (rdb:1) @b{p x}
788
+ 1
789
+ (rdb:1) @b{s}
790
+ foo
791
+ => true
792
+ irb(main):007:0>
793
+ @end smallexample
794
+
795
+ There is a counter inside of @code{Debugger.start} method to make sure
796
+ that this works when another @code{Debugger.start} method is called
797
+ inside of outer one. However if you are stopped inside the debugger,
798
+ issuing another @code{debugger} call will not have any effect even if
799
+ it is nested inside another @code{Debugger.start}.
800
+
801
+ @node Debugging Oddities
802
+ @section How debugging Ruby may be different than debugging other Languages
803
+
804
+ If you are used to debugging in other languages like C, C++, Perl,
805
+ Java or even Bash@footnote{this is just an excuse to put in a
806
+ shameless plug for my bash debugger @url{http://bashdb.sf.net}}, there
807
+ may be a number of things that seem or feel a little bit different and
808
+ may confuse you. A number of these things aren't oddities of the
809
+ debugger per se, so much as a difference in how Ruby works compared to
810
+ those other languages. Because Ruby works a little differently from
811
+ those other languages, writing a debugger has to also be a little
812
+ different as well if it is to be useful.
813
+
814
+ In this respect, using the debugger may help you understand Ruby
815
+ better.
816
+
817
+ We've already seen two examples of such differences. One difference is
818
+ the fact that we stop on method definitions or @code{def}'s and that is
819
+ because these are in fact executable statements. In other compiled
820
+ languages this would not happen because that's already been done when
821
+ you compile the program (or in Perl when it scans in the program). The
822
+ other difference we saw was in our inability to show call stack parameter
823
+ types without having made arrangements for the debugger to track
824
+ this. In other languages call stack information is usually available
825
+ without asking assistance of the debugger.@footnote{However in C, and
826
+ C++ generally you have to ask the compiler to add such information.}
827
+
828
+ In this section we'll consider some other things that might throw
829
+ off new users to Ruby who are familiar with other languages and
830
+ debugging in them.
831
+
832
+ @menu
833
+ * Stack No Longer Shows Scope Nesting::
834
+ * Bouncing Around in Blocks (e.g. Iterators)::
835
+ * No Parameter Values in a Call Stack::
836
+ * Lines You Can Stop At::
837
+ @end menu
838
+
839
+ @node Stack No Longer Shows Scope Nesting
840
+ @subsection Stack No Longer Shows Scope Nesting
841
+ In the Ruby 1.8 ruby-debug, backtraces will show more stack frames
842
+ that you might find in other languages, such as C. In the Ruby 1.9
843
+ ruby-debug, this is no longer the case.
844
+
845
+ @node Bouncing Around in Blocks (e.g. Iterators)
846
+ @subsection Bouncing Around in Blocks (e.g.@: Iterators)
847
+
848
+ When debugging languages with coroutines like Python and Ruby, a
849
+ method call may not necessarily go to the first statement after the
850
+ method header. It's possible the call will continue after a
851
+ @code{yield} statement from a prior call.
852
+
853
+ @smallexample
854
+ 1 #!/usr/bin/env ruby
855
+ 2 # Enumerator for primes
856
+ 3 class SievePrime
857
+ 4 @@@@odd_primes = []
858
+ 5 def self.next_prime(&block)
859
+ 6 candidate = 2
860
+ 7 yield candidate
861
+ 8 not_prime = false
862
+ 9 candidate += 1
863
+ 10 while true do
864
+ 11 @@@@odd_primes.each do |p|
865
+ 12 not_prime = (0 == (candidate % p))
866
+ 13 break if not_prime
867
+ 14 end
868
+ 15 unless not_prime
869
+ 16 @@@@odd_primes << candidate
870
+ 17 yield candidate
871
+ 18 end
872
+ 19 candidate += 2
873
+ 20 end
874
+ 21 end
875
+ 22 end
876
+ 23 SievePrime.next_prime do |prime|
877
+ 24 puts prime
878
+ 25 break if prime > 10
879
+ 26 end
880
+ @end smallexample
881
+
882
+ @smallexample
883
+ $ @b{rdebug primes.rb}
884
+ primes.rb:3
885
+ class SievePrime
886
+ (rdb:1) @b{set linetrace on}
887
+ line tracing is on.
888
+ (rdb:1) @b{step 9}
889
+ Tracing(1):primes.rb:4 @@odd_primes = []
890
+ Tracing(1):primes.rb:5 def self.next_prime(&block)
891
+ Tracing(1):primes.rb:23 SievePrime.next_prime do |prime|
892
+ Tracing(1):primes.rb:6 candidate = 2
893
+ Tracing(1):primes.rb:7 yield candidate
894
+ Tracing(1):primes.rb:24 puts prime
895
+ 2
896
+ Tracing(1):primes.rb:25 break if prime > 10
897
+ Tracing(1):primes.rb:8 not_prime = false
898
+ Tracing(1):primes.rb:9 candidate += 1
899
+ primes.rb:9
900
+ candidate += 1
901
+ (rdb:1)
902
+ @end smallexample
903
+
904
+ The loop between lines 23--26 gets interleaved between those of
905
+ @code{Sieve::next_prime}, lines 6--19 above.
906
+
907
+ A similar kind of thing can occur in debugging programs with many threads.
908
+
909
+ @node No Parameter Values in a Call Stack
910
+ @subsection No Parameter Values in a Call Stack
911
+ In traditional debuggers in a call stack you can generally see the
912
+ names of the parameters and the values that were passed in.
913
+
914
+ Ruby is a very dynamic language and it tries to be efficient within
915
+ the confines of the language definition. Values generally aren't taken
916
+ out of a variable or expression and pushed onto a stack. Instead a new
917
+ scope created and the parameters are given initial values. Parameter
918
+ passing is by @emph{reference}, not by value as it is say Algol, C, or
919
+ Perl. During the execution of a method, parameter values can
920
+ change---and often do. In fact even the @emph{class} of the object can
921
+ change.
922
+
923
+ So at present, the name of the parameter shown. The call-style setting
924
+ @pxref{Callstyle} can be used to set whether the name is shown or the
925
+ name and the @emph{current} class of the object.
926
+
927
+ It has been contemplated that a style might be added which saves on
928
+ call shorter ``scalar'' types of values and the class name.
929
+
930
+ @node Lines You Can Stop At
931
+ @subsection Lines You Can Stop At
932
+ As with the duplicate stops per control (e.g.@: @code{if} statement),
933
+ until tools like debuggers get more traction among core ruby
934
+ developers there are going to be weirdness. Here we describe the
935
+ stopping locations which effects the breakpoint line numbers you can
936
+ stop at.
937
+
938
+ Consider the following little Ruby program.
939
+
940
+ @smallexample
941
+ 'Yes it does' =~ /
942
+ (Yes) \s+
943
+ it \s+
944
+ does
945
+ /ix
946
+ puts $1
947
+ @end smallexample
948
+
949
+ The stopping points that Ruby records are the last two lines, lines 5
950
+ and 6.
951
+
952
+ Inside @code{ruby-debug} you an get a list of stoppable lines for a
953
+ file using the @code{info file} command with the attribute
954
+ @code{breakpoints}.
955
+
956
+ @ifset FINISHED
957
+ To be continued...
958
+
959
+ @itemize @bullet
960
+ @item more complex example with objects, pretty printing and irb.
961
+ @item line tracing and non-interactive tracing.
962
+ @item mixing in Debugger.debug with rdebug
963
+ @item post-mortem debugging and setting up for that
964
+ @item threading?
965
+ @item references to videos
966
+ @end itemize
967
+ @end ifset
968
+
969
+ @node Invocation
970
+ @chapter Getting in and out
971
+
972
+ @menu
973
+ * Starting the debugger:: How to enter the debugger
974
+ * Command Files:: Command files
975
+ * Quitting the debugger:: How to leave the debugger (quit, kill)
976
+ * Calling from Program:: Calling the debugger from inside your program
977
+ @end menu
978
+
979
+ It is also possible to enter the debugger when you have an uncaught
980
+ exception. See See also @ref{Post-Mortem Debugging}.
981
+
982
+ @node Starting the debugger
983
+ @section Starting the debugger
984
+
985
+ Although one can enter @DBG{} via Emacs (described in a later section)
986
+ and possibly others interfaces, probably the most familiar thing to do
987
+ is invoke the debugger from a command line.
988
+
989
+ A wrapper shell script called @code{rdebug} basically @code{require}'s
990
+ the gem package @code{ruby-debug} and then loads @code{rdebug}.
991
+
992
+ @smallexample
993
+ rdebug [rdebug-options] [--] @var{ruby-script} @var{ruby-script-arguments...}
994
+ @end smallexample
995
+
996
+ If you don't need to pass dash options to your program which might get
997
+ confused with the debugger options, then you don't need to add the
998
+ @option{--}.
999
+
1000
+ To get a brief list of options and descriptions, use the @code{--help}
1001
+ option.
1002
+
1003
+ @smallexample
1004
+ $ @b{rdebug --help}
1005
+ rdebug @value{RDEBUG_VERSION}
1006
+ Usage: rdebug [options] <script.rb> -- <script.rb parameters>
1007
+
1008
+ Options:
1009
+ -A, --annotate LEVEL Set annotation level
1010
+ -c, --client Connect to remote debugger
1011
+ --cport PORT Port used for control commands
1012
+ -d, --debug Set $DEBUG=true
1013
+ --emacs Activates full Emacs support
1014
+ --emacs-basic Activates basic Emacs mode
1015
+ -h, --host HOST Host name used for remote debugging
1016
+ -I, --include PATH Add PATH to $LOAD_PATH
1017
+ --keep-frame-binding Keep frame bindings
1018
+ -m, --post-mortem Activate post-mortem mode
1019
+ --no-control Do not automatically start control thread
1020
+ --no-quit Do not quit when script finishes
1021
+ --no-rewrite-program Do not set $0 to the program being debugged
1022
+ --no-stop Do not stop when script is loaded
1023
+ -nx Not run debugger initialization files (e.g. .rdebugrc
1024
+ -p, --port PORT Port used for remote debugging
1025
+ -r, --require SCRIPT Require the library, before executing your script
1026
+ --restart-script FILE Name of the script file to run. Erased after read
1027
+ --script FILE Name of the script file to run
1028
+ -s, --server Listen for remote connections
1029
+ -w, --wait Wait for a client connection, implies -s option
1030
+ -x, --trace Turn on line tracing
1031
+
1032
+ Common options:
1033
+ --verbose Turn on verbose mode
1034
+ --help Show this message
1035
+ --version Print the version
1036
+ -v Print version number, then turn on verbose mode
1037
+ @end smallexample
1038
+
1039
+ Options for the @code{rdebug} are shown in the following list.
1040
+
1041
+ @menu
1042
+ * rdebug command-line options:: Options you can pass to rdebug
1043
+ * rdebug default options:: How to Set Default Command-Line Options
1044
+ @end menu
1045
+
1046
+ @node rdebug command-line options
1047
+ @subsection Options you can pass to rdebug
1048
+
1049
+ You can run @DBG{} in various alternative modes---for example, as a
1050
+ program that interacts directly with the program in the same process
1051
+ on the same computer or via a socket to another process possibly on a
1052
+ different computer.
1053
+
1054
+ Many options appear as a long option name, such as @option{--help}, and
1055
+ a short one letter option name, such as @option{-h}. A double dash
1056
+ (@option{--} is used to separate options which go to @code{rdebug} from
1057
+ options that are intended to go to your Ruby script. Options (if any)
1058
+ to @code{rdebug} should come first. If there is no possibility of the
1059
+ Ruby script to be debugged getting confused with @code{rdebug}'s
1060
+ option the double dash can be omitted.
1061
+
1062
+ @table @code
1063
+ @item --help
1064
+ @cindex @option{-h}
1065
+ @cindex @option{--help}
1066
+ This option causes @ttDBG{} to print some basic help and exit.
1067
+
1068
+ @item -v | --version
1069
+ @cindex @option{-v}
1070
+ This option causes @ttDBG{} to print its version number and exit.
1071
+
1072
+ @item -A | --annotate @var{level}
1073
+ @cindex @option{-A}
1074
+ @cindex @option{--annotation} @var{level}
1075
+ Set gdb-style annotation @var{level}, a number. Additional information is output
1076
+ automatically when program state is changed. This can be used by
1077
+ front-ends such as GNU Emacs to post this updated information without
1078
+ having to poll for it.
1079
+ @item -c | --client
1080
+ @cindex @option{-c}
1081
+ @cindex @option{--client}
1082
+ Connect to remote debugger. The remote debugger should have been set
1083
+ up previously our you will get a connection error and @code{rdebug}
1084
+ will terminate.
1085
+
1086
+ @item --cport @var{port}
1087
+ @cindex @option{--cport} @var{port}
1088
+ Port used for control commands.
1089
+
1090
+ @item --debug
1091
+ @cindex @option{--debug}
1092
+ Set @code{$DEBUG} to @code{true}. This option is compatible with
1093
+ Ruby's.
1094
+
1095
+ @item --emacs
1096
+ Activates GNU Emacs mode.
1097
+ @c @pxref{GNU Emacs}.
1098
+ Debugger output is tagged in such a way to allow GNU Emacs to track
1099
+ where you are in the code.
1100
+
1101
+ @item --emacs-basic
1102
+ Activates full GNU Emacs mode.
1103
+ @c (@pxref{GNU Emacs}).
1104
+ This is the equivalent of setting the options @option{--emacs-basic},
1105
+ @code{annotate=3}, @option{--no-stop}, @option{-no-control} and
1106
+ @option{--post-mortem}.
1107
+
1108
+ @item -h | --host @var{host-address}
1109
+ Connect host address for remote debugging.
1110
+
1111
+ @item -I --include @var{PATH}
1112
+ @cindex @option{-I} @var{PATH}
1113
+ @cindex @option{--include} @var{PATH}
1114
+ Add @var{PATH} to @code{$LOAD_PATH}
1115
+
1116
+ @item --keep-frame-binding
1117
+ @cindex @option{--keep-frame-binding}
1118
+ Bindings are used to set the proper environment in evaluating
1119
+ expression inside the debugger. Under normal circumstances, I don't
1120
+ believe most people will ever need this option.
1121
+
1122
+ By default, the debugger doesn't create binding object for each frame
1123
+ when the frame is created, i.e. when a call is performed. Creating a
1124
+ binding is an expensive operation and has been a major source of
1125
+ performance problems.
1126
+
1127
+ Instead, the debugger creates a binding when there is a need to
1128
+ evaluate expressions. The artificial binding that is created might be
1129
+ different from the real one. In particular, in performing constant
1130
+ and module name resolution.
1131
+
1132
+ However it's still possible to restore the old, slower behavior by
1133
+ using this option or by setting @code{Debugger.keep_frame_binding =
1134
+ true}. There are two possibilities for which you might want to use
1135
+ this option.
1136
+
1137
+ First, if you think there's a bug in the evaluation of variables, you
1138
+ might want to set this to see if this corrects things.
1139
+
1140
+ Second, since the internal structures that are used here @code{FRAME}
1141
+ and @code{SCOPE} are not part of the Ruby specification, it is
1142
+ possible they can change with newer releases; so here this option this
1143
+ may offer a remedy. (But you'll probably also have to hack the C code
1144
+ since it's likely under this scenario that ruby-debug will no longer
1145
+ compile.) In fact, in Ruby 1.9 these structures have changed and that
1146
+ is partly why this debugger doesn't work on Ruby 1.9.
1147
+
1148
+ @item -m | --post-mortem
1149
+ @cindex @option{-m}
1150
+ @cindex @option{--post-mortem}
1151
+ If your program raises an exception that isn't caught you can enter
1152
+ the debugger for inspection of what went wrong. You may also want to
1153
+ use this option in conjunction with @option{--no-stop}. See also
1154
+ @ref{Post-Mortem Debugging}.
1155
+
1156
+ @item --no-control
1157
+ @cindex @option{--no-control}
1158
+ Do not automatically start control thread.
1159
+
1160
+ @item --no-quit
1161
+ @cindex @option{--no-quit}
1162
+ Restart the debugger when your program terminates normally.
1163
+
1164
+ @item --no-rewrite-program
1165
+ @cindex @option{--no-rewrite-program}
1166
+ Normally @code{rdebug} will reset the program name @code{$0} from its
1167
+ name to the debugged program, and set the its name in variable
1168
+ @code{$RDEBUG_0}. In the unlikely even you don't want this use this option.
1169
+
1170
+ @item --no-stop
1171
+ @cindex @option{--no-stop}
1172
+ Normally the @code{rdebug} stops before executing the first
1173
+ statement. If instead you want it to start running initially and will
1174
+ perhaps break it later in the running, use this options.
1175
+
1176
+ @item -p | --port @var{port}
1177
+ @cindex @option{-p} @var{port}
1178
+ @cindex @option{--port} @var{port}
1179
+ Port used for remote debugging.
1180
+
1181
+ @item -r | --require @var{library}
1182
+ @cindex @option{-r}
1183
+ @cindex @option{--require}
1184
+ Require the library, before executing your script. However if the
1185
+ library happened to be @code{debug}, we'll just ignore the require
1186
+ (since we're already a debugger). This option is compatible with Ruby's.
1187
+
1188
+ @item --script @var{file}
1189
+ @cindex @option{--script}
1190
+ Require the library, before executing your script. However if the
1191
+ library hap-pend to be @code{debug}, we'll just ignore the require
1192
+ (since we're already a debugger). This option is compatible with Ruby's.
1193
+
1194
+ @item -s | --server
1195
+ @cindex @option{-s}
1196
+ @cindex @option{--server}
1197
+ Debug the program but listen for remote connections on the default
1198
+ port or port set up via the @option{--port} option. See also @option{--wait}.
1199
+
1200
+ @item -w | --wait
1201
+ @cindex @option{-w}
1202
+ @cindex @option{--wait}
1203
+ Debug the program but stop waiting for a client connection first. This
1204
+ option automatically sets @option{--server} option.
1205
+
1206
+ @item -x | --trace
1207
+ @cindex @option{-x}
1208
+ @cindex @option{--trace}
1209
+ Turn on line tracing. Running @command{rdebug --trace @emph{rubyscript.rb}}
1210
+ is much like running: @command{ruby -rtracer @emph{rubyscript.rb}}
1211
+
1212
+ If all you want to do however is get a linetrace, @code{tracer}, not
1213
+ @code{rdebug}, may be faster:
1214
+ @smallexample
1215
+ $ @b{time ruby -rtracer gcd.rb 34 21 > /dev/null}
1216
+
1217
+ real 0m0.266s
1218
+ user 0m0.008s
1219
+ sys 0m0.000s
1220
+ $ @b{time rdebug --trace gcd.rb 34 21 > /dev/null}
1221
+
1222
+ real 0m0.875s
1223
+ user 0m0.448s
1224
+ sys 0m0.056s
1225
+ $
1226
+ @end smallexample
1227
+
1228
+ @end table
1229
+
1230
+ @node rdebug default options
1231
+ @subsection How to Set Default Command-Line Options
1232
+
1233
+ @DBG{} has many command-line options; it seems that some people want
1234
+ to set them differently from the our defaults. For example, some
1235
+ people may want @option{--no-quit --no-control} to be the default
1236
+ behavior. One could write a wrapper script or set a shell alias to
1237
+ handle this. @DBG{} has another way to do this as well. Before
1238
+ processing command options if the file @code{$HOME/.rdboptrc} is found
1239
+ (or, on Windows, @code{$HOME/rdbopt.ini}, it is loaded.
1240
+ If you want to set the defaults in some other way, you
1241
+ can put Ruby code here and set variable @code{options} which is an
1242
+ OpenStruct. For example here's how you'd set @option{-no-quit} and
1243
+ change the default control port to 5000.
1244
+
1245
+ @smallexample
1246
+ # This file contains how you want the default options to ruby-debug
1247
+ # to be set. Any Ruby code can be put here.
1248
+ #
1249
+ # debugger # Uncomment if you want to debug rdebug!
1250
+ options.control = false
1251
+ options.port = 5000
1252
+ puts "rocky's rdboptrc run"
1253
+ @end smallexample
1254
+
1255
+ Here are the default values in @code{options}
1256
+ @smallexample
1257
+ #<OpenStruct server=false, client=false, frame_bind=false, cport=8990, tracing=false, nx=false, post_mortem=false, port=8989, verbose_long=false, annotate=nil, control=true, restart_script=nil, quit=true, no_rewrite_program=false, stop=true, script=nil, host=nil, wait=false>
1258
+ @end smallexample
1259
+
1260
+
1261
+ @node Command Files
1262
+ @section Command files
1263
+
1264
+ @cindex command files
1265
+ A command file for @DBG{} is a file of lines that are @DBG{}
1266
+ commands. Comments (lines starting with @kbd{#}) may also be included.
1267
+ An empty line in a command file does nothing; it does not mean to repeat
1268
+ the last command, as it would from the terminal.
1269
+
1270
+ @cindex init file
1271
+ @cindex @file{.rdebugrc}
1272
+ When you start @value{DBG}, it automatically executes commands from its
1273
+ @dfn{init files}, normally called @file{.rdebugrc}.
1274
+
1275
+ On some configurations of @value{DBG}, the init file may be known by a
1276
+ different name. In particular on MS-Windows (but not cygwin)
1277
+ @file{rdebug.ini} is used.
1278
+
1279
+ During startup, @DBG{} does the following:
1280
+
1281
+ @enumerate
1282
+ @item
1283
+ Processes command line options and operands.
1284
+
1285
+ @item
1286
+ Reads the init file in your current directory, if any, and failing
1287
+ that the home directory. The home directory is the directory named in
1288
+ the @code{HOME} or @code{HOMEPATH} environment variable.
1289
+
1290
+ Thus, you can have more than one init file, one generic in your home
1291
+ directory, and another, specific to the program you are debugging, in
1292
+ the directory where you invoke @DBG{}.
1293
+
1294
+ @item
1295
+ Reads command files specified by the @samp{--script} option.
1296
+ @end enumerate
1297
+
1298
+ You can also request the execution of a command file with the
1299
+ @code{source} command, @pxref{Source}.
1300
+
1301
+ @node Quitting the debugger
1302
+ @section Quitting the debugger
1303
+
1304
+ @cindex interrupt
1305
+ An interrupt (often @kbd{C-c}) does not exit from @value{DBG}, but
1306
+ rather terminates the action of any @DBG command that is in
1307
+ progress and returns to @value{DBG} command level. Inside a debugger
1308
+ command interpreter, use @code{quit} command (@pxref{Control, ,Quitting
1309
+ the debugger}).
1310
+
1311
+ There way to terminate the debugger is to use the @code{kill}
1312
+ command. This does more forceful @code{kill -9}. It can be used in
1313
+ cases where @code{quit} doesn't work.
1314
+
1315
+ @node Calling from Program
1316
+ @section Calling the debugger from inside your Ruby program
1317
+
1318
+ Running a program from the debugger adds a bit of overhead and slows
1319
+ down your program a little.
1320
+
1321
+ Furthermore, by necessity, debuggers change the operation of the
1322
+ program they are debugging. And this can lead to unexpected and
1323
+ unwanted differences. It has happened so often that the term
1324
+ ``Heisenbugs'' (see @url{http://en.wikipedia.org/wiki/Heisenbug}) was
1325
+ coined to describe the situation where the addition of the use of a
1326
+ debugger (among other possibilities) changes behavior of the program
1327
+ so that the bug doesn't manifest itself anymore.
1328
+
1329
+ There is another way to get into the debugger which adds no overhead
1330
+ or slowdown until you reach the point at which you want to start
1331
+ debugging. However here you must change the script and make an
1332
+ explicit call to the debugger. Because the debugger isn't involved
1333
+ before the first call, there is no overhead and the script will run
1334
+ at the same speed as if there were no debugger.
1335
+
1336
+ There are three parts to calling the debugger from inside the script,
1337
+ ``requiring'' the debugger code, telling the debugger to start
1338
+ tracking things and then making the call calling the debugger to
1339
+ stop.
1340
+
1341
+ To get the debugger class accessible from your Ruby program:
1342
+
1343
+ @smallexample
1344
+ require 'ruby-debug'
1345
+ @end smallexample
1346
+ After @code{require 'ruby-debug'}, it's possible to set some of the
1347
+ debugger variables influence preferences. For example if you want to
1348
+ have @ttDBG run a @code{list} command every time it stops you set the
1349
+ variable @code{Debugger.settings[:autolist]}. @pxref{Debugger.settings} has a
1350
+ list of variable settings and the default values. Debugger settings
1351
+ can also be set in @code{.rdebugrc} as debugger
1352
+ commands. @pxref{Command Files}
1353
+
1354
+ To tell the debugger to start tracking things:
1355
+
1356
+ @smallexample
1357
+ Debugger.start
1358
+ @end smallexample
1359
+
1360
+ There is also a @code{Debugger.stop} to turn off debugger tracking. If
1361
+ speed is crucial, you may want to start and stop this around certain
1362
+ sections of code. Alternatively, instead of issuing an explicit
1363
+ @code{Debugger.stop} you can add a block to the @code{Debugger.start}
1364
+ and debugging is turned on for that block. If the block of code raises
1365
+ an uncaught exception that would cause the block to terminate, the
1366
+ @code{stop} will occur. See @ref{Debugger.start with a block}.
1367
+
1368
+ And finally to enter the debugger:
1369
+
1370
+ @smallexample
1371
+ debugger
1372
+ @end smallexample
1373
+
1374
+ As indicated above, when @code{debugger} is run a @code{.rdebugrc}
1375
+ profile is read if that file exists.
1376
+
1377
+ You may want to do enter the debugger at several points in the program
1378
+ where there is a problem you want to investigate. And since
1379
+ @code{debugger} is just a method call it's possible enclose it in a
1380
+ conditional expression, for example:
1381
+ @smallexample
1382
+ debugger if 'bar' == foo and 20 == iter_count
1383
+ @end smallexample
1384
+
1385
+ Although each step does a very specific thing which offers great
1386
+ flexibility, in order to make getting into the debugger easier the
1387
+ three steps have been rolled into one command:
1388
+ @smallexample
1389
+ require "ruby-debug/debugger"
1390
+ @end smallexample
1391
+
1392
+ @node Debugger Command Reference
1393
+ @chapter @code{ruby-debug} Command Reference
1394
+
1395
+ @menu
1396
+ * Command Interfaces:: The kinds of interface used to interact with ruby-debug
1397
+ * Command Syntax:: How to give commands to the ruby-debug
1398
+ * Command Output:: How rdebug presents its output
1399
+ * Help:: How to ask for help (help)
1400
+ * Control:: Controlling the debugger (quit, restart, interrupt)
1401
+ * DisplayCommands:: Executing expressions on stop (display, undisplay)
1402
+ * PrintCommands:: Evaluating and Printing Expressions (p, pp, ps, pp, irb)
1403
+ * PrintVars:: Printing Variables (var)
1404
+ * List:: Examining Program Source Files (list)
1405
+ * Edit:: Editing source files (edit)
1406
+ * FrameCommands:: Examining the stack frame (where, up, down, frame)
1407
+ * Stopping:: Stopping and continuing (break, watch, step, cont...)
1408
+ * ruby-debug settings:: rdebug-settings (set args, set autoeval, ...)
1409
+ * Program Information:: Program Status (info)
1410
+ @end menu
1411
+
1412
+ @node Command Interfaces
1413
+ @section Command Interfaces
1414
+ There are several ways one can talk to @code{ruby-debug} and get
1415
+ results. The simplest way is via a command-line interface directly
1416
+ talking to the debugger. This is referred to below as a ``Local
1417
+ Interface''. It's also possible to run the debugger and set up a port
1418
+ by which some other process can connect and control the debug
1419
+ session. This is called a ``Remote Interface''. When you want to gain
1420
+ access to a remote interface you need to run @code{ruby-debug} using a
1421
+ ``Control Interface''. This interface might not be the same process as
1422
+ the process running the debugged program and might not even be
1423
+ running on the same computer.
1424
+
1425
+ Other front-ends may use one of these and build on top and provide
1426
+ other (richer) interfaces. Although many of the commands are available
1427
+ on all interfaces some are not. Most of the time in this manual when
1428
+ we talk about issuing commands describing the responses elicited,
1429
+ we'll assume we are working with the local interface.
1430
+
1431
+ @node Command Syntax
1432
+ @section Command Syntax
1433
+ Usually a command is put on a single line. There is no limit on how long
1434
+ it can be. It starts with a command name, which is followed by
1435
+ arguments whose meaning depends on the command name. For example, the
1436
+ command @code{step} accepts an argument which is the number of times to
1437
+ step, as in @code{step 5}. You can also use the @code{step} command with no
1438
+ arguments. Some commands do not allow any arguments.
1439
+
1440
+ Multiple commands can be put on a line by separating each with a
1441
+ semicolon (@code{;}). You can disable the meaning of a semicolon to
1442
+ separate commands by escaping it with a backslash.
1443
+
1444
+ For example, if you have @code{autoeval} (@ref{Autoeval}) set, you
1445
+ might want to enter the following code to compute the 5th Fibonacci
1446
+ number:
1447
+ @smallexample
1448
+ # Compute the 5 Fibonaci number
1449
+ (rdb:1) set autoeval on
1450
+ (rdb:1) fib1=0; fib2=1; 5.times @{|temp| temp=fib1; fib1=fib2; fib2 += temp @}
1451
+ SyntaxError Exception: compile error
1452
+ /usr/bin/irb:10: syntax error, unexpected $end, expecting '@}'
1453
+ 5.times @{|temp| temp=fib1
1454
+ ^
1455
+ (rdb:1) fib1=0\; fib2=1\; 5.times @{|temp| temp=fib1\; fib1=fib2\; fib2 += temp @}
1456
+ 5
1457
+ (rdb:1) fib2
1458
+ fib2
1459
+ 8
1460
+ @end smallexample
1461
+ You might also consider using the @code{irb} command, @ref{irb}, and
1462
+ then you won't have to escape semicolons.
1463
+
1464
+ A blank line as input (typing just @key{<RET>}) means to repeat the
1465
+ previous command.
1466
+
1467
+ In the ``local'' interface, the Ruby Readline module is used. It
1468
+ handles line editing and retrieval of previous commands. Up arrow, for
1469
+ example moves to the previous debugger command; down arrow moves to
1470
+ the next more recent command (provided you are not already at the last
1471
+ command). Command history is saved in file @code{.rdebug_hist}. A
1472
+ limit is put on the history size. You can see this with the @code{show
1473
+ history size} command. See @ref{History} for history parameters.
1474
+
1475
+ @node Command Output
1476
+ @section Command Output
1477
+
1478
+ In the command-line interface, when @code{ruby-debug} is waiting for
1479
+ input it presents a prompt of the form
1480
+ @code{(rdb:}@emph{x}@code{)}. If debugging locally, @emph{x} will be
1481
+ the thread number. Usual the main thread is 1, so often you'll see
1482
+ @code{(rdb:1)}. In the control interface though @emph{x} will be
1483
+ @code{ctrl} and in post-mortem debugging @code{post-mortem}.
1484
+
1485
+ In the local interface, whenever @code{ruby-debug} gives an error
1486
+ message such as for an invalid command, or an invalid location
1487
+ position, it will generally preface the message with
1488
+ @code{***}. However if annotation mode is on that the message is put
1489
+ in a @code{begin-error} annotation and no @code{***} appears.
1490
+
1491
+ @node Help
1492
+ @section Getting help (@samp{help})
1493
+ @cindex on-line documentation
1494
+ @menu
1495
+ * Help for Subcommands::
1496
+ @end menu
1497
+
1498
+ Once inside @code{ruby-debug} you can always ask it for information on
1499
+ its commands, using the command @code{help}.
1500
+
1501
+ @table @code
1502
+ @kindex h @r{(@code{help})}
1503
+ @kindex help @ovar{command-name}
1504
+ @item help
1505
+ @itemx h
1506
+ You can use @code{help} (abbreviated @code{h}) with no arguments to
1507
+ display a short list of named classes of commands:
1508
+
1509
+ @flushleft
1510
+ @smallexample
1511
+ (rdb:1) @b{help}
1512
+ ruby-debug help v@value{RDEBUG_VERSION}
1513
+ Type 'help <command-name>' for help on a specific command
1514
+
1515
+ Available commands:
1516
+ backtrace delete enable help list ps save thread where
1517
+ break disable eval info method putl set trace
1518
+ catch display exit irb next quit show undisplay
1519
+ condition down finish jump p reload source up
1520
+ continue edit frame kill pp restart step var
1521
+ @end smallexample
1522
+ @end flushleft
1523
+ @c the above line break eliminates huge line overfull...
1524
+
1525
+ @end table
1526
+
1527
+ @table @code
1528
+ @item help @var{command}
1529
+ With a command name as @code{help} argument, @DBG displays short
1530
+ information on how to use that command.
1531
+
1532
+ @smallexample
1533
+ (rdb:1) @b{help list}
1534
+ ruby-debug help v@value{RDEBUG_VERSION}
1535
+ l[ist] list forward
1536
+ l[ist] - list backward
1537
+ l[ist] = list current line
1538
+ l[ist] nn-mm list given lines
1539
+ * NOTE - to turn on autolist, use 'set autolist'
1540
+ (rdb:1)
1541
+ @end smallexample
1542
+ @end table
1543
+
1544
+ @node Help for Subcommands
1545
+ @subsection Help on Subcommands
1546
+ A number of commands have many sub-parameters or
1547
+ @emph{subcommands}. These include @code{info}, @code{set},
1548
+ @code{show}, @code{enable} and @code{disable}.
1549
+
1550
+ When you ask for help for one of these commands, you will get help for
1551
+ all of the subcommands that that command offers. Sometimes you may
1552
+ want help that subcommand and to do this just follow the command with
1553
+ its subcommand name. For example @code{help set annotate} will just
1554
+ give help about the annotate command. Furthermore it will give longer
1555
+ help than the summary information that appears when you ask for
1556
+ help. You don't need to list the full subcommand name, but just enough
1557
+ of the letters to make that subcommand distinct from others will
1558
+ do. For example, @code{help set an} is the same as @code{help set annotate}.
1559
+
1560
+ Some examples follow.
1561
+ @example
1562
+ (rdb:1) @b{help info}
1563
+ Generic command for showing things about the program being debugged.
1564
+ --
1565
+ List of info subcommands:
1566
+ --
1567
+ info args -- Argument variables of current stack frame
1568
+ info breakpoints -- Status of user-settable breakpoints
1569
+ info catch -- Exceptions that can be caught in the current stack frame
1570
+ info display -- Expressions to display when program stops
1571
+ info file -- Info about a particular file read in
1572
+ info files -- File names and timestamps of files read in
1573
+ info global_variables -- Global variables
1574
+ info instance_variables -- Instance variables of the current stack frame
1575
+ info line -- Line number and file name of current position in source file
1576
+ info locals -- Local variables of the current stack frame
1577
+ info program -- Execution status of the program
1578
+ info stack -- Backtrace of the stack
1579
+ info thread -- List info about thread NUM
1580
+ info threads -- information of currently-known threads
1581
+ info variables -- Local and instance variables of the current stack frame
1582
+ @end example
1583
+
1584
+ @example
1585
+ (rdb:1) @b{help info breakpoints}
1586
+ Status of user-settable breakpoints.
1587
+ Without argument, list info about all breakpoints. With an
1588
+ integer argument, list info on that breakpoint.
1589
+ @end example
1590
+
1591
+ @example
1592
+ (rdb:1) @b{help info br}
1593
+ Status of user-settable breakpoints.
1594
+ Without argument, list info about all breakpoints. With an
1595
+ integer argument, list info on that breakpoint.
1596
+ @end example
1597
+
1598
+ @node Control
1599
+ @section Controlling the debugger (@samp{quit}, @samp{restart}, @samp{interrupt}, @samp{source})
1600
+
1601
+ @menu
1602
+ * Quit:: Quitting the debugger (quit)
1603
+ * Restart:: Restarting Script execution (restart)
1604
+ * Interrupt:: Interrupting the debugger (interrupt)
1605
+ * Source:: Running Debugger commands (source)
1606
+ @end menu
1607
+
1608
+ @node Quit
1609
+ @subsection Quit (@samp{quit})
1610
+ @table @code
1611
+ @kindex quit @r{[}unconditionally@r{]}
1612
+ @kindex q @r{(@code{quit})}
1613
+ @item quit @r{[}unconditionally@r{]}
1614
+ @item exit
1615
+ @itemx q
1616
+
1617
+ To exit @value{DBG}, use the @code{quit} command (abbreviated
1618
+ @code{q}), or alias @code{exit}.
1619
+
1620
+ A simple @code{quit} tries to terminate all threads in effect.
1621
+
1622
+ Normally if you are in an interactive session, this command will
1623
+ prompt to ask if you really want to quit. If you don't want any
1624
+ questions asked, enter the ``unconditionally''.
1625
+
1626
+ @end table
1627
+
1628
+ @node Restart
1629
+ @subsection Restart (@samp{restart})
1630
+ @table @code
1631
+ @kindex restart @r{[}@var{program args}@r{]}
1632
+ @kindex R @r{(@code{restart})}
1633
+ @item restart
1634
+ @itemx R
1635
+
1636
+ Restart the program. This is a re-exec - all debugger state is
1637
+ lost. If command arguments are passed those are used. Otherwise the
1638
+ last program arguments used in the last invocation are used.
1639
+
1640
+ In not all cases will you be able to restart the program. First, the
1641
+ program should have been invoked at the outset rather than having been
1642
+ called from inside your program or invoked as a result of post-mortem
1643
+ handling.
1644
+
1645
+ Also, since this relies on the the OS @code{exec} call, this command
1646
+ is available only if your OS supports that @code{exec}; OSX for
1647
+ example does not (yet).
1648
+ @end table
1649
+
1650
+ @node Interrupt
1651
+ @subsection Interrupt (@samp{interrupt})
1652
+ @table @code
1653
+ @kindex interrupt
1654
+ @kindex i
1655
+ @item interrupt
1656
+ @itemx i
1657
+ Interrupt the program. Useful if there are multiple threads running.
1658
+ @end table
1659
+
1660
+ @node Source
1661
+ @subsection Running Debugger Commands (@samp{source})
1662
+ @table @code
1663
+ @kindex source @var{filename}
1664
+ @item source @var{filename}
1665
+ Execute the command file @var{filename}.
1666
+
1667
+ The lines in a command file are executed sequentially. They are not
1668
+ printed as they are executed. If there is an error, execution
1669
+ proceeds to the next command in the file. For information about
1670
+ command files that get run automatically on startup, @pxref{Command Files}.
1671
+ @end table
1672
+
1673
+ @node DisplayCommands
1674
+ @section Executing expressions on stop (@samp{display}, @samp{undisplay})
1675
+ @cindex automatic display
1676
+ @cindex display of expressions
1677
+
1678
+ If you find that you want to print the value of an expression
1679
+ frequently (to see how it changes), you might want to add it to the
1680
+ @dfn{automatic display list} so that @value{DBG} evaluates a statement
1681
+ each time your program stops or the statement is shown in line tracing.
1682
+ Each expression added to the list is given a number to identify it; to
1683
+ remove an expression from the list, you specify that number. The
1684
+ automatic display looks like this:
1685
+
1686
+ @smallexample
1687
+ (rdb:1) display n
1688
+ 1: n = 3
1689
+ @end smallexample
1690
+
1691
+ @noindent
1692
+ This display shows item numbers, expressions and their current values.
1693
+ If the expression is undefined or illegal the expression will be
1694
+ printed but no value will appear.
1695
+
1696
+ @smallexample
1697
+ (rdb:1) display undefined_variable
1698
+ 2: undefined_variable =
1699
+ (rdb:1) display 1/0
1700
+ 3: 1/0 =
1701
+ @end smallexample
1702
+
1703
+ Note: this command uses @code{to_s} to in expressions; for example an
1704
+ array @code{[1, 2]} will appear as @code{12}. For some datatypes like
1705
+ an Array, you may want to call the @code{inspect} method, for example
1706
+ @code{display ARGV.inspect} rather than @code{display ARGV}.
1707
+
1708
+ @table @code
1709
+ @kindex display @ovar{expr}
1710
+ @item display @var{expr}
1711
+ Add the expression @var{expr} to the list of expressions to display
1712
+ each time your program stops or a line is printed when linetracing is
1713
+ on (@pxref{DisplayCommands}).
1714
+
1715
+ @item display
1716
+ Display the current values of the expressions on the list, just as is
1717
+ done when your program stops.
1718
+
1719
+ @kindex undisplay @ovar{num}
1720
+ @item undisplay @ovar{num}
1721
+ @itemx delete display @var{num}
1722
+ Remove item number @var{num} from the list of expressions to display.
1723
+
1724
+ @kindex info display
1725
+ @item info display
1726
+ Show all display expressions
1727
+
1728
+ @ifset GDB_COMPLETED
1729
+ @code{undisplay} does not repeat if you press @key{RET} after using it.
1730
+ (Otherwise you would just get the error @samp{No display number @dots{}}.)
1731
+ @end ifset
1732
+
1733
+ @kindex disable display
1734
+ @item disable display @var{dnums}@dots{}
1735
+ Disable the display of item numbers @var{dnums}. A disabled display
1736
+ item is not printed automatically, but is not forgotten. It may be
1737
+ enabled again later.
1738
+
1739
+ @kindex enable display
1740
+ @item enable display @var{dnums}@dots{}
1741
+ Enable display of item numbers @var{dnums}. It becomes effective once
1742
+ again in auto display of its expression, until you specify otherwise.
1743
+
1744
+ @end table
1745
+
1746
+ @node PrintCommands
1747
+ @section Evaluating and Printing Expressions (@samp{p}, @samp{pp}, @samp{putl}, @samp{ps}, @samp{irb})
1748
+
1749
+ One way to examine and change data in your script is with the
1750
+ @code{eval} command (abbreviated @code{p}). A similar command is
1751
+ @code{pp} which tries to pretty print the result. Finally @code{irb} is
1752
+ useful when you anticipate examining or changing a number of things,
1753
+ and prefer not to have to preface each command, but rather work as one
1754
+ does in @code{irb}.
1755
+
1756
+ @menu
1757
+ * eval:: eval or print an expression (eval, p)
1758
+ * pp:: pretty print an expression (pp, ps, putl)
1759
+ * irb:: running irb using the current context
1760
+ @end menu
1761
+
1762
+ @node eval
1763
+ @subsection Printing an expression (@samp{eval}, @samp{p})
1764
+ @table @code
1765
+ @kindex eval @var{expr}
1766
+ @kindex p @r{(@code{eval})}
1767
+ @item eval @var{expr}
1768
+ @itemx p @var{expr}
1769
+
1770
+ Use @code{eval} or @code{p} to evaluate a Ruby expression, @var{expr},
1771
+ same as you would if you were in @code{irb}. If there are many expressions
1772
+ you want to look at, you may want to go into irb from the debugger.
1773
+ @smallexample
1774
+ @group
1775
+ (rdb:p) p n
1776
+ 3
1777
+ (rdb:1) p "the value of n is #@{n@}"
1778
+ "the value of n is 3"
1779
+ (rdb:1)
1780
+ @end group
1781
+ @end smallexample
1782
+ @end table
1783
+
1784
+ @node pp
1785
+ @subsection Pretty-Printing an expression (@samp{pp}, @samp{putl}, @samp{ps}))
1786
+ @table @code
1787
+ @item pp
1788
+ @kindex pp @var{expr}
1789
+ Evaluates and pretty-prints @var{expr}
1790
+ @smallexample
1791
+ @group
1792
+ (rdb:1) @b{p $LOAD_PATH}
1793
+ ["/home/rocky/lib/ruby", "/usr/lib/ruby/site_ruby/1.8", "/usr/lib/ruby/site_ruby/1.8/i586-linux", "/usr/lib/ruby/1.8"]
1794
+ (rdb:1) @b{pp $LOAD_PATH}
1795
+ ["/home/rocky/lib/ruby",
1796
+ "/usr/lib/ruby/site_ruby/1.8",
1797
+ "/usr/lib/ruby/site_ruby/1.8/i586-linux",
1798
+ "/usr/lib/ruby/1.8"]
1799
+ @end group
1800
+ @end smallexample
1801
+ @kindex putl
1802
+ @item putl
1803
+ If the value you want to print is an array, sometimes a columnized
1804
+ list looks nicer:
1805
+ @smallexample
1806
+ @group
1807
+ (rdb:1) @b{putl $LOAD_PATH}
1808
+ /home/rocky/lib/ruby /usr/lib/ruby/site_ruby/1.8
1809
+ /usr/lib/ruby/site_ruby/1.8/i586-linux /usr/lib/ruby/1.8
1810
+ @end group
1811
+ @end smallexample
1812
+
1813
+ Note however that entries are sorted to run down first rather than
1814
+ across. So in the example above the second entry in the list is
1815
+ @code{/usr/lib/ruby/site_ruby/1.8/i586-linux} and the @emph{third} entry is
1816
+ @code{/usr/lib/ruby/site_ruby/1.8}.
1817
+
1818
+ If the value is not an array @code{putl} will just call pretty-print.
1819
+ @kindex ps
1820
+ @item ps
1821
+ Sometimes you may want to print the array not only columnized, but
1822
+ sorted as well. The list of debugger help commands appears this way,
1823
+ and so does the output of the @code{method} commands.
1824
+
1825
+ @smallexample
1826
+ @group
1827
+ (rdb:1) ps Kernel.private_methods
1828
+ Digest initialize y
1829
+ Pathname initialize_copy
1830
+ Rational location_of_caller
1831
+ active_gem_with_options method_added
1832
+ alias_method method_removed
1833
+ append_features method_undefined
1834
+ attr module_function
1835
+ attr_accessor private
1836
+ attr_reader protected
1837
+ attr_writer public
1838
+ class_variable_get remove_class_variable
1839
+ class_variable_set remove_const
1840
+ define_method remove_instance_variable
1841
+ extend_object remove_method
1842
+ extended singleton_method_added
1843
+ gcd singleton_method_removed
1844
+ gem_original_require singleton_method_undefined
1845
+ include timeout
1846
+ included undef_method
1847
+ @end group
1848
+ @end smallexample
1849
+
1850
+ If the value is not an array, @code{ps} will just call pretty-print.
1851
+ See also the @code{methods}.
1852
+ @end table
1853
+
1854
+ @node irb
1855
+ @subsection Run irb (@samp{irb})
1856
+ @table @code
1857
+ @kindex irb
1858
+ @item irb
1859
+ Run an interactive ruby session (@code{irb}) with the bindings
1860
+ environment set to the state you are in the program.
1861
+
1862
+ When you leave irb and go back to the debugger command prompt we show
1863
+ again the file, line and text position of the program in the same way
1864
+ as when entered the debugger. If you issue a @command{list} without
1865
+ location information, the default location used is the current line
1866
+ rather than the position may have gotten updated via a prior
1867
+ @command{list} command.
1868
+ @smallexample
1869
+ triangle.rb:4
1870
+ def triangle(n)
1871
+ (rdb:1) @b{list}
1872
+ [-1, 8] in /home/rocky/ruby/triangle.rb
1873
+ 1 #!/usr/bin/env ruby
1874
+ 2 # Compute the n'th triangle number - the hard way
1875
+ 3 # triangle(n) == (n * (n+1)) / 2
1876
+ => 4 def triangle(n)
1877
+ 5 tri = 0
1878
+ 6 0.upto(n) do |i|
1879
+ 7 tri += i
1880
+ 8 end
1881
+ @b{irb}
1882
+ >> @b{(0..6).inject@{|sum, i| sum +=i@}}
1883
+ => 21
1884
+ >> @b{exit}
1885
+ triangle.rb:4
1886
+ def triangle(n)
1887
+ (rdb:1) @b{list # Note we get the same line range as before going into irb}
1888
+ [-1, 8] in /home/rocky/ruby/triangle.rb
1889
+ 1 #!/usr/bin/env ruby
1890
+ 2 # Compute the n'th triangle number - the hard way
1891
+ 3 # triangle(n) == (n * (n+1)) / 2
1892
+ => 4 def triangle(n)
1893
+ 5 tri = 0
1894
+ 6 0.upto(n) do |i|
1895
+ 7 tri += i
1896
+ 8 end
1897
+ @end smallexample
1898
+
1899
+ @end table
1900
+
1901
+ @node PrintVars
1902
+ @section Printing Variables (@samp{var}, @samp{method})
1903
+
1904
+ @table @code
1905
+ @item var const @var{object}
1906
+ @kindex var const @var{expr}
1907
+ Show the constants of @var{object}. This is basically listing
1908
+ variables and their values in @var{object}@code{.constant}.
1909
+ @item var instance @var{object}
1910
+ @kindex var instance @var{expr}
1911
+ Show the instance variables of @var{object}. This is basically listing
1912
+ @var{object}@code{.instance_variables}.
1913
+ @item info instance_variables
1914
+ @kindex info instance_variables
1915
+ Show instance_variables of @code{@@self}
1916
+ @item info locals
1917
+ @kindex info locals
1918
+ Show local variables
1919
+ @item info globals
1920
+ @kindex info globals
1921
+ Show global variables
1922
+ @item info variables
1923
+ @kindex info variables
1924
+ Show local and instance variables of @code{@@self}
1925
+ @item method instance @var{object}
1926
+ @kindex method instance @var{object}
1927
+ Show methods of @var{object}. Basically this is the same as running
1928
+ @code{ps object.instance_methods(false)} on @var{object}.
1929
+ @item method iv @var{object}
1930
+ @kindex method iv @var{object}
1931
+ Show method instance variables of @var{object}. Basically this is the same as running
1932
+ @smallexample
1933
+ obj.instance_variables.each do |v|
1934
+ puts "%s = %s\n" % [v, obj.instance_variable_get(v)]
1935
+ end
1936
+ @end smallexample
1937
+ on @var{object}.
1938
+ @item signature @var{object}
1939
+ @kindex method signature @var{object}
1940
+ Show procedure signature of method @var{object}.
1941
+ @emph{This command is available only if the nodewrap is installed.}
1942
+ @smallexample
1943
+ def mymethod(a, b=5, &bock)
1944
+ end
1945
+ (rdb:1) @b{method sig mymethod}
1946
+ Mine#mymethod(a, b=5, &bock)
1947
+ @end smallexample
1948
+ on @var{object}.
1949
+ @item method @var{class-or-module}
1950
+ @kindex method @var{class-or-module}
1951
+ Show methods of the class or module, @var{class-or-module}. Basically
1952
+ this is the same as running @code{ps object.methods} on @var{class-or-module}.
1953
+ on @var{class-or-module}.
1954
+ @end table
1955
+
1956
+ @node List
1957
+ @section Examining Program Source Files (@samp{list})
1958
+
1959
+ @cindex current line
1960
+ @value{DBG} can print parts of your script's source. When your script
1961
+ stops, @value{DBG} spontaneously prints the line where it stopped and
1962
+ the text of that line. Likewise, when you select a stack frame
1963
+ (@pxref{Selection}) @value{DBG} prints the line where execution in
1964
+ that frame has stopped. Implicitly there is a default line
1965
+ location. Each time a list command is run that implicit location is
1966
+ updated, so that running several list commands in succession shows a
1967
+ contiguous block of program text.
1968
+
1969
+ You can print other portions of source files by giving an explicit
1970
+ position as a parameter to the list command.
1971
+
1972
+ If you use @value{DBG} through its Emacs interface, you may prefer to
1973
+ use Emacs facilities to view source.
1974
+ @c @pxref{GNU Emacs}.
1975
+
1976
+ @kindex list @ovar{line-number}
1977
+ @kindex l @r{(@code{list})}
1978
+ To print lines from a source file, use the @code{list} command
1979
+ (abbreviated @code{l}). By default, ten lines are printed. Fewer may
1980
+ appear if there fewer lines before or after the current line to center
1981
+ the listing around.
1982
+
1983
+ There are several ways to specify what part of the file you want to print.
1984
+ Here are the forms of the @code{list} command.
1985
+
1986
+ @table @code
1987
+ @item list @var{line-number}
1988
+ @itemx l @var{line-number}
1989
+ Print lines centered around line number @var{line-number} in the
1990
+ current source file.
1991
+
1992
+ @item list
1993
+ @itemx l
1994
+ Print more lines. If the last lines printed were printed with a
1995
+ @code{list} command, this prints lines following the last lines
1996
+ printed; however, if the last line printed was a solitary line printed
1997
+ as part of displaying a stack frame (@pxref{Frames}), this prints lines
1998
+ centered around that line.
1999
+
2000
+ @item list -
2001
+ @itemx l -
2002
+ Print lines just before the lines last printed.
2003
+ @item list @var{first}-@var{last}
2004
+ Print lines between @var{first} and @var{last} inclusive.
2005
+
2006
+ @item list =
2007
+ Print lines centered around where the script is stopped.
2008
+ @end table
2009
+
2010
+ Repeating a @code{list} command with @key{RET} discards the argument,
2011
+ so it is equivalent to typing just @code{list}. This is more useful
2012
+ than listing the same lines again. An exception is made for an
2013
+ argument of @samp{-}; that argument is preserved in repetition so that
2014
+ each repetition moves up in the source file.
2015
+
2016
+ @node Edit
2017
+ @section Editing Source files (@samp{edit})
2018
+
2019
+ To edit the lines in a source file, use the @code{edit} command. The
2020
+ editing program of your choice is invoked with the current line set to
2021
+ the active line in the program. Alternatively, you can give a line
2022
+ specification to specify what part of the file you want to print if
2023
+ you want to see other parts of the program.
2024
+
2025
+ You can customize to use any editor you want by using the
2026
+ @code{EDITOR} environment variable. The only restriction is that your
2027
+ editor (say @code{ex}), recognizes the following command-line syntax:
2028
+ @smallexample
2029
+ ex +@var{number} file
2030
+ @end smallexample
2031
+ The optional numeric value +@var{number} specifies the number of the
2032
+ line in the file where to start editing. For example, to configure
2033
+ @value{DBG} to use the @code{vi} editor, you could use these commands
2034
+ with the @code{sh} shell:
2035
+ @smallexample
2036
+ EDITOR=/usr/bin/vi
2037
+ export EDITOR
2038
+ gdb @dots{}
2039
+ @end smallexample
2040
+ or in the @code{csh} shell,
2041
+ @smallexample
2042
+ setenv EDITOR /usr/bin/vi
2043
+ gdb @dots{}
2044
+ @end smallexample
2045
+
2046
+ @table @code
2047
+ @kindex edit @ovar{line-specification}
2048
+ @item edit @ovar{line specification}
2049
+ Edit line specification using the editor specified by the
2050
+ @code{EDITOR} environment variable.
2051
+ @end table
2052
+
2053
+ @node FrameCommands
2054
+ @section Examining the Stack Frame (@samp{where}, @samp{up}, @samp{down}, @samp{frame})
2055
+
2056
+ When your script has stopped, one thing you'll probably want to know
2057
+ is where it stopped and some idea of how it got there.
2058
+
2059
+ @cindex call stack
2060
+ Each time your script performs a function or sends a message to a
2061
+ method, or enters a block, information about this action is saved.
2062
+ The frame stack then is this a history of the blocks that got you to
2063
+ the point that you are currently stopped at.@footnote{More accurately
2064
+ we should call this a ``block stack''; but we'll use the name that is
2065
+ more commonly used. And internally in Ruby, there is ``FRAME''
2066
+ structure which is yet slightly different.}
2067
+
2068
+ @cindex selected block
2069
+ One entry in call stack is @dfn{selected} by @DBG{} and many
2070
+ @DBG commands refer implicitly to the selected block. In
2071
+ particular, whenever you ask @DBG to list lines without giving
2072
+ a line number or location the value is found in the selected frame.
2073
+ There are special @DBG commands to select whichever frame you
2074
+ are interested in. @xref{Selection, ,Selecting a frame}.
2075
+
2076
+ When your program stops, @DBG{} automatically selects the
2077
+ currently executing frame and describes it briefly, similar to the
2078
+ @code{frame} command.
2079
+
2080
+ After switching frames, when you issue a @code{list} command without
2081
+ any position information, the position used is location in the frame
2082
+ that you just switched between, rather than a location that got
2083
+ updated via a prior @code{list} command.
2084
+
2085
+ @menu
2086
+ * Frames:: Stack frames
2087
+ * Backtrace:: Backtraces (where)
2088
+ * Selection:: Selecting a frame (up, down, frame)
2089
+
2090
+ @end menu
2091
+
2092
+ @node Frames
2093
+ @subsection Stack frames
2094
+
2095
+ @cindex frame, definition
2096
+ @cindex stack frame
2097
+ The block stack is divided up into contiguous pieces called @dfn{stack
2098
+ frames}, @dfn{frames}, or @dfn{blocks} for short; each frame/block has
2099
+ a scope associated with it; It contains a line number and the
2100
+ source-file name that the line refers. If the frame/block is the beginning
2101
+ of a method or function it also contains the function name.
2102
+
2103
+ @cindex initial frame
2104
+ @cindex outermost frame
2105
+ @cindex innermost frame
2106
+ When your script is started, the stack has only one frame, that of the
2107
+ function @code{main}. This is called the @dfn{initial} frame or the
2108
+ @dfn{outermost} frame. Each time a function is called, a new frame is
2109
+ made. Each time a function returns, the frame for that function invocation
2110
+ is eliminated. If a function is recursive, there can be many frames for
2111
+ the same function. The frame for the function in which execution is
2112
+ actually occurring is called the @dfn{innermost} frame. This is the most
2113
+ recently created of all the stack frames that still exist.
2114
+
2115
+ @cindex frame number
2116
+ @value{DBG} assigns numbers to all existing stack frames, starting with
2117
+ zero for the innermost frame, one for the frame that called it,
2118
+ and so on upward. These numbers do not really exist in your script;
2119
+ they are assigned by @value{DBG} to give you a way of designating stack
2120
+ frames in @value{DBG} commands.
2121
+
2122
+ @node Backtrace
2123
+ @subsection Backtraces (@samp{where})
2124
+
2125
+ @cindex backtraces
2126
+ @cindex tracebacks
2127
+ @cindex stack traces
2128
+ A backtrace is essentially the same as the call stack: a summary of
2129
+ how your script got where it is. It shows one line per frame, for
2130
+ many frames, starting with the place that you are stopped at (frame
2131
+ zero), followed by its caller (frame one), and on up the stack.
2132
+
2133
+ @table @code
2134
+ @kindex where
2135
+ @kindex w @r{(@code{where})}
2136
+ @itemx where
2137
+ Print the entire stack frame; @code{info stack} is an alias for this command.
2138
+ Each frame is numbered and can be referred to in the @code{frame}
2139
+ command; @code{up} and @code{down} add or subtract respectively to
2140
+ frame numbers shown. The position of the current frame is marked with
2141
+ @code{-->}.
2142
+
2143
+ @smallexample
2144
+ (rdb:1) where
2145
+ --> #0 Object.gcd(a#Fixnum, b#Fixnum) at line /tmp/gcd.rb:6
2146
+ #1 at line /tmp/gcd.rb:19
2147
+ @end smallexample
2148
+
2149
+ @ifset FINISHED
2150
+ @item backtrace @var{n}
2151
+ @itemx bt @var{n}
2152
+ @itemx where @var{n}
2153
+ @itemx T @var{n}
2154
+ Similar, but print only the innermost @var{n} frames.
2155
+
2156
+ @item backtrace -@var{n}
2157
+ @itemx bt -@var{n}
2158
+ @itemx where -@var{n}
2159
+ @itemx T -@var{n}
2160
+ Similar, but print only the outermost @var{n} frames.
2161
+ @end ifset
2162
+ @end table
2163
+
2164
+ @node Selection
2165
+ @subsection Selecting a frame (@samp{up}, @samp{down}, @samp{frame})
2166
+
2167
+ Commands for listing source code in your script work on whichever
2168
+ stack frame is selected at the moment. Here are the commands for
2169
+ selecting a stack frame; all of them finish by printing a brief
2170
+ description of the stack frame just selected.
2171
+
2172
+ @table @code
2173
+ @kindex up @ovar{n}
2174
+ @item up @ovar{n}
2175
+ Move @var{n} frames up the stack. For positive numbers @var{n}, this
2176
+ advances toward the outermost frame, to higher frame numbers, to
2177
+ frames that have existed longer. Using a negative @var{n} is the same thing
2178
+ as issuing a @code{down} command of the absolute value of the @var{n}.
2179
+ Using zero for @var{n} does no frame adjustment, but since the current
2180
+ position is redisplayed, it may trigger a resynchronization if there is
2181
+ a front end also watching over things.
2182
+
2183
+ @var{n} defaults to one. You may abbreviate @code{up} as @code{u}.
2184
+
2185
+ @kindex down @ovar{n}
2186
+ @item down @ovar{n}
2187
+ Move @var{n} frames down the stack. For positive numbers @var{n}, this
2188
+ advances toward the innermost frame, to lower frame numbers, to frames
2189
+ that were created more recently. Using a negative @var{n} is the same
2190
+ as issuing a @code{up} command of the absolute value of the @var{n}.
2191
+ Using zero for @var{n} does no frame adjustment, but since the current
2192
+ position is redisplayed, it may trigger a resynchronization if there is
2193
+ a front end also watching over things.
2194
+
2195
+ @var{n} defaults to one.
2196
+ @end table
2197
+
2198
+ @table @code
2199
+ @kindex frame @r{[}@ovar{n} @r{[}thread @var{thread-num}@r{]}@r{]}
2200
+ @cindex current stack frame
2201
+ @item frame @ovar{n} @r{[}thread @var{thread-num}@r{]}
2202
+ The @code{frame} command allows you to move from one stack frame to
2203
+ another, and to print the stack frame you select. @var{n} is the the
2204
+ stack frame number or 0 if no frame number is given; @code{frame 0}
2205
+ then will always show the current and most recent stack frame.
2206
+
2207
+ If a negative number is given, counting is from the other end of the
2208
+ stack frame, so @code{frame -1} shows the least-recent, outermost or
2209
+ most ``main'' stack frame.
2210
+
2211
+ Without an argument, @code{frame} prints the current stack
2212
+ frame. Since the current position is redisplayed, it may trigger a
2213
+ resynchronization if there is a front end also watching over
2214
+ things.
2215
+
2216
+ If a thread number is given then we set the context for evaluating
2217
+ expressions to that frame of that thread.
2218
+ @end table
2219
+
2220
+ @node Stopping
2221
+ @section Stopping and Resuming Execution
2222
+
2223
+ One important use of a debugger is to stop your program @emph{before}
2224
+ it terminates, so that if your script runs into trouble you can
2225
+ investigate and find out why. However should your script accidentally
2226
+ continue to termination, it can be arranged for @DBG to not to leave
2227
+ the debugger without your explicit instruction. That way, you can
2228
+ restart the program using the same command arguments.
2229
+
2230
+ Inside @value{DBG}, your script may stop for any of several reasons,
2231
+ such as a signal, a breakpoint, or reaching a new line after a
2232
+ debugger command such as @code{step}. You may then examine and
2233
+ change variables, set new breakpoints or remove old ones, and then
2234
+ continue execution.
2235
+
2236
+ @menu
2237
+ * Breakpoints:: Breakpoints (break, catch, delete)
2238
+ * Disabling:: Disabling breakpoints (disable, enable)
2239
+ * Conditions:: Break conditions (condition)
2240
+ * Resuming Execution:: Resuming execution (continue, step, next, finish)
2241
+ @end menu
2242
+
2243
+ @node Breakpoints
2244
+ @subsection Breakpoints (@samp{break}, @samp{catch}, @samp{delete})
2245
+
2246
+ @cindex breakpoints
2247
+ A @dfn{breakpoint} makes your script stop whenever a certain point in
2248
+ the program is reached. For each breakpoint, you can add conditions to
2249
+ control in finer detail whether your script stops.
2250
+
2251
+ You specify the place where your script should stop with the
2252
+ @code{break} command and its variants.
2253
+
2254
+ @cindex breakpoint numbers
2255
+ @cindex numbers for breakpoints
2256
+ @value{ttDBG} assigns a number to each breakpoint when
2257
+ you create it; these numbers are successive integers starting with
2258
+ one. In many of the commands for controlling various features of
2259
+ breakpoints you use the breakpoint number to say which breakpoint you
2260
+ want to change. Each breakpoint may be @dfn{enabled} or
2261
+ @dfn{disabled}; if disabled, it has no effect on your script until you
2262
+ enable it again.
2263
+
2264
+
2265
+ @table @code
2266
+ @kindex break @ovar{location}
2267
+ @kindex b @r{(@code{break})}
2268
+ @item break
2269
+ Set a breakpoint at the current line.
2270
+
2271
+ @item break @var{linenum}
2272
+ Set a breakpoint at line @var{linenum} in the current source file.
2273
+ The current source file is the last file whose source text was printed.
2274
+ The breakpoint will stop your script just before it executes any of the
2275
+ code on that line.
2276
+
2277
+ @item break @var{filename}:@var{linenum}
2278
+ Set a breakpoint at line @var{linenum} in source file @var{filename}.
2279
+
2280
+ What may be a little tricky when specifying the filename is getting
2281
+ the name recognized by the debugger. If you get a message the message
2282
+ ``@code{No source file named ...}'', then you may need to qualify the
2283
+ name more fully. To see what files are loaded you can use the @code{info
2284
+ files} or @code{info file} commands. If you want the name @code{rdebug} thinks
2285
+ of as the current file, use @code{info line}.
2286
+
2287
+ Here's an example:
2288
+ @example
2289
+ $ @b{rdebug ~/ruby/gcd.rb 3 5}
2290
+ /home/rocky/ruby/gcd.rb:4 # Note this is the file name
2291
+ def gcd(a, b)
2292
+ (rdb:1) @b{break gcd.rb:6}
2293
+ *** No source file named gcd.rb
2294
+ (rdb:1) @b{info line}
2295
+ Line 4 of "/home/rocky/ruby/gcd.rb"
2296
+ (rdb:1) @b{break /home/rocky/ruby/gcd.rb:6}
2297
+ Breakpoint 1 file /home/rocky/ruby/gcd.rb, line 6
2298
+ (rdb:1) @b{break ~/ruby/gcd.rb:10} # tilde expansion also works
2299
+ Breakpoint 2 file /home/rocky/ruby/gcd.rb, line 10
2300
+ (rdb:1) @b{info file gcd.rb}
2301
+ File gcd.rb is not cached
2302
+ (rdb:1) @b{info file /home/rocky/ruby/gcd.rb}
2303
+ File /home/rocky/ruby/gcd.rb
2304
+ 19 lines
2305
+ @end example
2306
+
2307
+ @item break @var{class}:@var{method}
2308
+ Set a breakpoint in class @var{class} method @var{method}. You can
2309
+ also use a period @code{.} instead of a colon @code{:}. Note that two
2310
+ colons @code{::} are not used. Also note a class @emph{must} be
2311
+ specified here. If the method you want to stop in is in the main class
2312
+ (i.e. the class that @code{self} belongs to at the start of the
2313
+ program), then use the name @code{Object}.
2314
+
2315
+ @kindex catch @ovar{exception} @r{[} on | 1 | off | 0 @r{]}
2316
+ @kindex cat @r{(@code{catch})}
2317
+ @item catch @ovar{exception} @r{[} on | 1 | off | 0 @r{]}
2318
+ Set catchpoint to an exception. Without an exception name show catchpoints.
2319
+
2320
+ With an ``on'' or ``off'' parameter, turn handling the exception on or
2321
+ off. To delete all exceptions type ``catch off''.
2322
+
2323
+ @cindex delete breakpoints
2324
+ @kindex delete @ovar{breakpoints}
2325
+ @kindex del @r{(@code{delete})}
2326
+ @item delete @ovar{breakpoints}
2327
+ Delete the breakpoints specified as arguments.
2328
+
2329
+ If no argument is specified, delete all breakpoints (@DBG asks
2330
+ confirmation. You can abbreviate this command as @code{del}.
2331
+ @kindex info breakpoints
2332
+ @cindex @code{$_} and @code{info breakpoints}
2333
+ @item info breakpoints @ovar{n}
2334
+ @itemx info break @ovar{n}
2335
+ Print a table of all breakpoints set and not deleted, with the
2336
+ following columns for each breakpoint:
2337
+
2338
+ @table @emph
2339
+ @item Breakpoint Numbers (@samp{Num})
2340
+ @item Enabled or Disabled (@samp{Enb})
2341
+ Enabled breakpoints are marked with @samp{1}. @samp{0} marks breakpoints
2342
+ that are disabled (not enabled).
2343
+ @item File and Line (@samp{file:line})
2344
+ The filename and line number inside that file where of breakpoint in
2345
+ the script. The file and line are separated with a colon.
2346
+ @item Condition
2347
+ A condition (an arithmetic expression) which when true causes the
2348
+ breakpoint to take effect.
2349
+ @end table
2350
+ @noindent
2351
+ If a breakpoint is conditional, @code{info break} shows the condition on
2352
+ the line following the affected breakpoint; breakpoint commands, if any,
2353
+ are listed after that.
2354
+
2355
+ @code{info break} with a breakpoint number @var{n} as argument lists
2356
+ only that breakpoint.
2357
+
2358
+ Examples:
2359
+ @example
2360
+ (rdb:1) @b{info break}
2361
+ Breakpoints at following places:
2362
+ Num Enb What
2363
+ 1 y gcd.rb:3
2364
+ 2 y gcb.rb:28 if n > 1
2365
+ (rdb:1) @b{info break 2}
2366
+ 2 y gcb.rb:28 if n > 1
2367
+ @end example
2368
+ @end table
2369
+
2370
+ @node Disabling
2371
+ @subsection Disabling breakpoints (@samp{disable}, @samp{enable})
2372
+
2373
+ Rather than deleting a breakpoint, you might
2374
+ prefer to @dfn{disable} it. This makes the breakpoint inoperative as if
2375
+ it had been deleted, but remembers the information on the breakpoint so
2376
+ that you can @dfn{enable} it again later.
2377
+
2378
+ You disable and enable breakpoints and catchpoints with the
2379
+ @code{enable} and @code{disable} commands, optionally specifying one
2380
+ or more breakpoint numbers as arguments. Use @code{info break} to
2381
+ print a list of breakpoints and catchpoints if you do not know which
2382
+ numbers to use.
2383
+
2384
+ A breakpoint or catchpoint can have any different
2385
+ states of enablement:
2386
+
2387
+ @itemize @bullet
2388
+ @item
2389
+ Enabled. The breakpoint stops your program. A breakpoint set
2390
+ with the @code{break} command starts out in this state.
2391
+ @item
2392
+ Disabled. The breakpoint has no effect on your program.
2393
+ @end itemize
2394
+
2395
+ You can use the following commands to enable or disable breakpoints
2396
+ and catchpoints:
2397
+
2398
+ @table @code
2399
+ @kindex disable breakpoints
2400
+ @item disable @var{breakpoints}
2401
+ Disable the specified breakpoints---or all breakpoints, if none are
2402
+ listed. A disabled breakpoint has no effect but is not forgotten. All
2403
+ options such as ignore-counts, conditions and commands are remembered in
2404
+ case the breakpoint is enabled again later. You may abbreviate
2405
+ @code{disable} as @code{dis}.
2406
+
2407
+ @kindex enable breakpoints
2408
+ @item enable @var{breakpoints}
2409
+ Enable the specified breakpoints (or all defined breakpoints). They
2410
+ become effective once again in stopping your program.
2411
+
2412
+ @end table
2413
+
2414
+ Breakpoints that you set are initially enabled; subsequently, they
2415
+ become disabled or enabled only when you use one of the commands
2416
+ above. (The command @code{until} can set and delete a breakpoint of
2417
+ its own, but it does not change the state of your other breakpoints;
2418
+ see @ref{Resuming Execution, ,Resuming Execution}.)
2419
+
2420
+ @node Conditions
2421
+ @subsection Break conditions (@samp{condition})
2422
+ @cindex conditional breakpoints
2423
+ @cindex breakpoint conditions
2424
+
2425
+ The simplest sort of breakpoint breaks every time your script reaches
2426
+ a specified place. You can also specify a @dfn{condition} for a
2427
+ breakpoint. A condition is just a Ruby expression.
2428
+
2429
+ Break conditions can be specified when a breakpoint is set, by using
2430
+ @samp{if} in the arguments to the @code{break} command. A breakpoint
2431
+ with a condition evaluates the expression each time your script
2432
+ reaches it, and your script stops only if the condition is
2433
+ @emph{true}. They can also be changed at any time
2434
+ with the @code{condition} command.
2435
+
2436
+ @ifset FINISHED
2437
+ You can also use the @code{if} keyword with the @code{watch} command.
2438
+ The @code{catch} command does not recognize the @code{if} keyword;
2439
+ @code{condition} is the only way to impose a further condition on a
2440
+ catchpoint.
2441
+ @end ifset
2442
+
2443
+ @table @code
2444
+ @kindex condition
2445
+ @item condition @var{bnum} @var{expression}
2446
+ Specify @var{expression} as the break condition for breakpoint
2447
+ @var{bnum}. After you set a condition, breakpoint @var{bnum} stops
2448
+ your program only if the value of @var{expression} is true (nonzero).
2449
+
2450
+ @item condition @var{bnum}
2451
+ Remove the condition from breakpoint number @var{bnum}. It becomes
2452
+ an ordinary unconditional breakpoint.
2453
+ @end table
2454
+
2455
+ @ifset FINISHED
2456
+ When you use @code{condition}, @DBG checks @var{expression}
2457
+ immediately for syntactic correctness, and to determine whether
2458
+ symbols in it have referents in the context of your breakpoint. If
2459
+ @var{expression} uses symbols not referenced in the context of the
2460
+ breakpoint, @DBG prints an error message:
2461
+
2462
+ @example
2463
+ No symbol "foo" in current context.
2464
+ @end example
2465
+ @end ifset
2466
+
2467
+ @noindent
2468
+ The debugger does not actually evaluate @var{expression} at the time
2469
+ the @code{condition} command (or a command that sets a breakpoint with
2470
+ a condition, like @code{break if @dots{}}) is given, however.
2471
+
2472
+ Examples;
2473
+ @example
2474
+ condition 1 x>5 # Stop on breakpoint 0 only if x>5 is true.
2475
+ condition 1 # Change that! Unconditionally stop on breakpoint 1.
2476
+ @end example
2477
+
2478
+ @node Resuming Execution
2479
+ @subsection Resuming Execution (@samp{step}, @samp{next}, @samp{finish}, @samp{continue}, @samp{jump})
2480
+
2481
+ A typical technique for using stepping is to set a breakpoint
2482
+ (@pxref{Breakpoints}) at the beginning of the function or the section
2483
+ of your script where a problem is believed to lie, run your script
2484
+ until it stops at that breakpoint, and then step through the suspect
2485
+ area, examining the variables that are interesting, until you see the
2486
+ problem happen.
2487
+
2488
+ @cindex stepping
2489
+ @cindex continuing
2490
+ @cindex resuming execution
2491
+ @dfn{Continuing} means resuming program execution until your script
2492
+ completes normally. In contrast, @dfn{stepping} means executing just
2493
+ one more ``step'' of your script, where ``step'' may mean either one
2494
+ line of source code. Either when continuing or when stepping,
2495
+ your script may stop even sooner, due to a breakpoint or a signal.
2496
+
2497
+ @menu
2498
+ * Step:: running the next statement (step)
2499
+ * Next:: running the next statement skipping over functions (next)
2500
+ * Finish:: running until the return of a function or ``source'' (finish)
2501
+ * Continue:: continuing execution (continue)
2502
+ * Jump:: jumping to a new line (jump)
2503
+ @end menu
2504
+
2505
+ @node Step
2506
+ @subsubsection Step (@samp{step})
2507
+ @table @code
2508
+ @kindex step @r{[}+@r{]} @ovar{count}
2509
+ @kindex s @r{(@code{step})}
2510
+ @item step @r{[}+-@r{]} @ovar{count}
2511
+ Continue running your program until the next logical stopping point
2512
+ and return control to @value{DBG}. This command is abbreviated
2513
+ @code{s}.
2514
+
2515
+ Like, the programming Lisp, Ruby tends implemented in a highly
2516
+ expression-oriented manner. Therefore things that in other languages
2517
+ that may appear to be a single statement are implemented in Ruby as
2518
+ several expressions. For example, in an ``if'' statement or looping
2519
+ statements a stop is made after the expression is evaluated but before
2520
+ the test on the expression is made.
2521
+
2522
+ So it is common that a lines in the program will have several stopping
2523
+ points where in other debuggers of other languages there would be only
2524
+ one. Or you may have several statements listed on a line.
2525
+
2526
+ When stepping it is not uncommon to want to go to a different line on
2527
+ each step. If you want to make sure that on a step you go to a
2528
+ @emph{different} position, add a plus sign (@samp{+}).
2529
+
2530
+ @emph{Note: step+ with a number count is not the same as issuing
2531
+ count step+ commands. Instead it uses count-1 step commands followed
2532
+ by a step+ command. For example, @code{step+ 3} is the
2533
+ same as @code{step; step; step+}, not @code{step+; step+; step+}}
2534
+
2535
+ If you find yourself generally wanting to use @code{step+} rather
2536
+ than @code{step}, you may want to consider using @code{set
2537
+ forcestep}, (@pxref{Forcestep}).
2538
+
2539
+ If you have @code{forcestep} set on but want to temporarily disable it
2540
+ for the next step command, append a minus, or @code{step-}.
2541
+
2542
+ With a count, @code{step} will continue running as normal, but do so
2543
+ @var{count} times. If a breakpoint is reached, or a signal not
2544
+ related to stepping occurs before @var{count} steps, stepping stops
2545
+ right away.
2546
+ @end table
2547
+
2548
+ @node Next
2549
+ @subsubsection Next (@samp{next})
2550
+ @table @code
2551
+ @kindex next @r{[}+-@r{]} @ovar{count}
2552
+ @kindex n @r{(@code{next})}
2553
+ @item next @r{[}+@r{]} @ovar{count}
2554
+ This is similar to @code{step}, but function or method calls that
2555
+ appear within the line of code are executed without stopping. As with
2556
+ step, if you want to make sure that on a step you go to a
2557
+ @emph{different} position, add a plus sign (@samp{+}). Similarly,
2558
+ appending a minus disables a @code{forcestep} temporarily, and an
2559
+ argument @var{count} is a repeat count, as for @code{step}.
2560
+ @end table
2561
+
2562
+ @node Finish
2563
+ @subsubsection Finish (@samp{finish})
2564
+ @table @code
2565
+ @kindex finish @ovar{frame-number}
2566
+ @item finish @ovar{frame-number}
2567
+ Execute until selected stack frame returns. If no frame number is
2568
+ given, we run until the currently selected frame returns. The
2569
+ currently selected frame starts out the most-recent frame or 0 if no
2570
+ frame positioning (e.g@: @code{up}, @code{down} or @code{frame}) has
2571
+ been performed. If a frame number is given we run until @var{frame} frames
2572
+ returns.
2573
+
2574
+ If you want instead to terminate the program and debugger entirely,
2575
+ use @code{quit} (@pxref{Quitting the debugger, ,Quitting the debugger}).
2576
+
2577
+ @end table
2578
+
2579
+ @node Continue
2580
+ @subsubsection Continue (@samp{continue})
2581
+ @table @code
2582
+ @kindex continue @ovar{line-specification}
2583
+ @kindex c @r{(@code{continue})}
2584
+ @item continue @ovar{line-specification}
2585
+ @itemx c @ovar{line-specification}
2586
+ Resume program execution, at the address where your script last
2587
+ stopped; any breakpoints set at that address are bypassed.
2588
+
2589
+ The optional argument @var{line-specification} allows you to specify a
2590
+ line number to set a one-time breakpoint which is deleted when that
2591
+ breakpoint is reached.
2592
+
2593
+ Should the program stop before that breakpoint is reached, for
2594
+ example, perhaps another breakpoint is reached first, in
2595
+ a listing of the breakpoints you won't see this entry in the list of
2596
+ breakpoints.
2597
+ @end table
2598
+
2599
+ @node Jump
2600
+ @subsubsection Jump (@samp{jump})
2601
+ @table @code
2602
+ @kindex jump @r{[}+-@r{]} @ovar{line}
2603
+ @item jump @r{[}+-@r{]} @ovar{line}
2604
+ Change the next line to execute to the given line number.
2605
+ @end table
2606
+
2607
+ @node ruby-debug settings
2608
+ @section ruby-debug settings (@samp{set args}, @samp{set autoeval}..)
2609
+
2610
+ You can alter the way ruby-debug interacts with you using @code{set}
2611
+ commands.
2612
+
2613
+ The various parameters to @code{set} are given below. Each parameter
2614
+ name needs to to be only enough to make it unique. For example
2615
+ @code{set force} is a suitable abbreviation for @code{set forcestep}.
2616
+ The letter case is not important, so @code{set FORCE} or @code{set
2617
+ Force} are also suitable abbreviations.
2618
+
2619
+ Many @code{set} commands are either ``on'' or ``off'', and you can
2620
+ indicate which way you want set by supplying the corresponding
2621
+ word. The number 1 can be used for ``on'' and 0 for ``off''. If none
2622
+ of these is given, we will assume ``on''. A deprecated way of turning
2623
+ something off is by prefacing it with ``no''.
2624
+
2625
+ Each @code{set} command has a corresponding @code{show} command which
2626
+ allows you to see the current value.
2627
+
2628
+ @menu
2629
+ * Args:: Annotation Level
2630
+ * Autoeval:: Evaluate unrecognized commands
2631
+ * Autolist:: Execute ``list'' command on every breakpoint
2632
+ * Autoirb:: Invoke IRB on every stop
2633
+ * Autoreload:: Reload source code when changed
2634
+ * Basename:: Report file basename only showing file names
2635
+ * Callstyle:: Show Report file basename only showing file names
2636
+ * Forcestep:: Make sure 'next/step' commands always move to a new line
2637
+ * Fullpath:: Display full file names in frames
2638
+ * History:: Generic command for showing command history parameters.
2639
+ * Keepframebindings:: Save frame binding on each call
2640
+ * Linetrace:: line execution tracing
2641
+ * Linetrace+:: line tracing style
2642
+ * Listsize:: Number of lines to try to show in a 'list' command
2643
+ * Post-mortem:: Whether post-mortem handling is in effect.
2644
+ * Trace:: Display stack trace when 'eval' raises exception
2645
+ * Width:: Number of characters the debugger thinks are in a line
2646
+ @end menu
2647
+
2648
+ @node Args
2649
+ @subsection Set/Show args
2650
+
2651
+ @table @code
2652
+ @kindex set args @ovar{parameters}
2653
+ @item set args @ovar{parameters}
2654
+ Specify the arguments to be used if your program is rerun. If
2655
+ @code{set args} has no arguments, @code{restart} executes your program
2656
+ with no arguments. Once you have run your program with arguments,
2657
+ using @code{set args} before the next @code{restart} is the only way to run
2658
+ it again without arguments.
2659
+
2660
+ @kindex show args
2661
+ @item show args
2662
+ Show the arguments to give your program when it is started.
2663
+ @end table
2664
+
2665
+ @node Autoeval
2666
+ @subsection Set/Show auto-eval
2667
+
2668
+ @table @code
2669
+ @kindex set autoeval @r{[} on | 1 | off | 0 @r{]}
2670
+ @item set autoeval @r{[} on | 1 | off | 0 @r{]}
2671
+ Specify that debugger input that isn't recognized as a command should
2672
+ be passed to Ruby for evaluation (using the current debugged program
2673
+ namespace). Note however that we @emph{first} check input to see if it
2674
+ is a debugger command and @emph{only} if it is not do we consider it
2675
+ as Ruby code. This means for example that if you have variable called
2676
+ @code{n} and you want to see its value, you could use @code{p n},
2677
+ because just entering @code{n} will be interpreted as the debugger
2678
+ ``next'' command.
2679
+
2680
+ See also @ref{irb} and @ref{Autoirb}.
2681
+
2682
+ When autoeval is set on, you'll get a different error message when you
2683
+ invalid commands are encountered. Here's a session fragment to show
2684
+ the difference
2685
+ @smallexample
2686
+ (rdb:1) @b{stepp}
2687
+ Unknown command
2688
+ (rdb:1) @b{set autoeval on}
2689
+ autoeval is on.
2690
+ (rdb:1) @b{stepp}
2691
+ NameError Exception: undefined local variable or method `stepp' for ...
2692
+ @end smallexample
2693
+
2694
+ @kindex show autoeval
2695
+ @item show args
2696
+ Shows whether Ruby evaluation of debugger input should occur or not.
2697
+ @end table
2698
+
2699
+ @node Autolist
2700
+ @subsection Execute ``list'' command on every breakpoint
2701
+
2702
+ @node Autoirb
2703
+ @subsection Set/Show auto-irb
2704
+
2705
+ @table @code
2706
+ @kindex set autoirb @r{[} on | 1 | off | 0 @r{]}
2707
+ @item set autoirb @r{[} on | 1 | off | 0 @r{]}
2708
+
2709
+ When your program stops, normally you go into a debugger command loop
2710
+ looking for debugger commands. If instead you would like to directly
2711
+ go into an irb shell, set this on. See also @ref{Autoeval} or
2712
+ @ref{irb} if you tend to use debugger commands but still want Ruby
2713
+ evaluation occasionally.
2714
+
2715
+ @kindex show autoirb
2716
+ @item show autoirb
2717
+ Shows whether the debugger will go into irb on stop or not.
2718
+ @end table
2719
+
2720
+ @node Autoreload
2721
+ @subsection Set/Show auto-reload
2722
+ @table @code
2723
+ @kindex set autoreload @r{[} on | 1 | off | 0 @r{]}
2724
+ Set this on if the debugger should check to see if the source has
2725
+ changed since the last time it reread in the file if it has.
2726
+ @end table
2727
+
2728
+ @node Basename
2729
+ @subsection Set/Show basename
2730
+
2731
+ @table @code
2732
+ @kindex set basename @r{[} on | 1 | off | 0 @r{]}
2733
+ @item set basename @r{[} on | 1 | off | 0 @r{]}
2734
+ Source filenames are shown as the shorter ``basename''
2735
+ only. (Directory paths are omitted). This is useful in running the
2736
+ regression tests and may useful in showing debugger examples as in
2737
+ this text. You may also just want less verbose filename display.
2738
+
2739
+ By default filenames are shown as with their full path.
2740
+
2741
+ @kindex show basename
2742
+ @item show basename
2743
+ Shows the whether filename display shows just the file basename or not.
2744
+ @end table
2745
+
2746
+ @node Callstyle
2747
+ @subsection Set/Show call style
2748
+
2749
+ @table @code
2750
+ @ifset FINISHED
2751
+ @kindex set callstyle @r{[} short | last | tracked @r{]}
2752
+ @item set forcestep @r{[} short | last | tracked @r{]}
2753
+ @else
2754
+ @kindex set callstyle @r{[} short | last
2755
+ @item set forcestep @r{[} short | last
2756
+ @end ifset
2757
+
2758
+ Sets how you want call parameters displayed; @code{short} shows just
2759
+ the parameter names;
2760
+ @ifset FINISHED
2761
+ @code{last} shows the parameter names and the
2762
+ class of these variables as they currently exist. Note the type could
2763
+ have changed between when the call and its current
2764
+ values.
2765
+ @end ifset
2766
+ @code{tracked} is the most accurate but this adds
2767
+ overhead. On every call, scalar values of the parameters get
2768
+ saved. For non-scalar values the class is saved.
2769
+ @end table
2770
+
2771
+ @node Forcestep
2772
+ @subsection Set/Show Forces Different Line Step/Next
2773
+
2774
+ @table @code
2775
+ @kindex set forcestep @r{[} on | 1 | off | 0 @r{]}
2776
+ @item set forcestep @r{[} on | 1 | off | 0 @r{]}
2777
+
2778
+ Due to the interpretive, expression-oriented nature of the Ruby
2779
+ Language and implementation, each line often contains many possible
2780
+ stopping points, while in a debugger it is often desired to treat each
2781
+ line as an individual stepping unit.
2782
+
2783
+ Setting forcestep on will cause each @code{step} or @code{next}
2784
+ command to stop at a different line number. See also @ref{Step} and
2785
+ @ref{Next}.
2786
+
2787
+ @kindex show forcestep
2788
+ @item show forcestep
2789
+ Shows whether forcestep is in effect or not.
2790
+ @end table
2791
+
2792
+ @node Fullpath
2793
+ @subsection Set/Show Frame full path
2794
+
2795
+ @node History
2796
+ @subsection Command History Parameters
2797
+ @table @code
2798
+ @item show commands
2799
+ @kindex show commands
2800
+ Display the last ten commands in the command history.
2801
+
2802
+ @item show commands @var{n}
2803
+ @kindex show commands @var{n}
2804
+ Print ten commands centered on command number @var{n}.
2805
+
2806
+ @item show history filename
2807
+ @kindex show history filename
2808
+ Show the filename in which to record the command history
2809
+ (the list of previous commands of which a record is kept).
2810
+
2811
+ @item set history save @r{[} on | 1 | off | 0 @r{]}
2812
+ @kindex set history save @r{[} on | 1 | off | 0 @r{]}
2813
+ Set whether to save the history on exit.
2814
+
2815
+ @item show history save
2816
+ @kindex show history save
2817
+ Show saving of the history record on exit.
2818
+
2819
+ @item set history size @var{number}
2820
+ @kindex set history size @var{number}
2821
+ Set the maximum number of commands to save in the history.
2822
+
2823
+ @item show history size
2824
+ @kindex show history size
2825
+ Show the size of the command history, i.e. the number of previous
2826
+ commands to keep a record of.
2827
+ @end table
2828
+
2829
+ @node Keepframebindings
2830
+ @subsection Save frame binding on each call
2831
+
2832
+ @node Linetrace
2833
+ @subsection Set/Show Line tracing
2834
+
2835
+ @table @code
2836
+ @kindex set linetrace @r{[} on | 1 | off | 0 @r{]}
2837
+ @item set linetrace @r{[} on | 1 | off | 0 @r{]}
2838
+
2839
+ Setting linetrace on will cause lines to be shown before run.
2840
+
2841
+ @kindex show linetrace
2842
+ @item show linetrace
2843
+ Shows whether line tracing is in effect or not.
2844
+ @end table
2845
+
2846
+ @node Linetrace+
2847
+ @subsection Set/Show Line tracing style
2848
+
2849
+ @table @code
2850
+ @kindex set linetrace+ @r{[} on | 1 | off | 0 @r{]}
2851
+ @item set linetrace+ @r{[} on | 1 | off | 0 @r{]}
2852
+
2853
+ Setting linetrace+ on will cause consecutive trace lines not to be a
2854
+ duplicate of the preceding line-trace line. Note however that this
2855
+ setting doesn't by itself turn on or off line tracing.
2856
+
2857
+ @kindex show linetrace+
2858
+ @item show linetrace
2859
+ Shows whether the line tracing style is to show all lines or remove
2860
+ duplicates linetrace lines when it is a repeat of the previous line.
2861
+ @end table
2862
+
2863
+ @node Listsize
2864
+ @subsection Set/Show lines in a List command
2865
+
2866
+ @table @code
2867
+ @kindex set listsize @var{number-of-lines}
2868
+ @item set listsize @var{number-of-lines}
2869
+ Set number of lines to try to show in a @code{list} command.
2870
+ @kindex show listsize
2871
+ @item show listsize
2872
+ Shows the list-size setting.
2873
+ @end table
2874
+
2875
+ @node Post-mortem
2876
+ @subsection Show Post-mortem handling
2877
+ @table @code
2878
+ @kindex show post-mortem
2879
+ Shows wither post-mortem debugging is in effect. Right now we don't
2880
+ have the ability to change the state inside the debugger.
2881
+ @end table
2882
+
2883
+ @node Trace
2884
+ @subsection Display stack trace when 'eval' raises exception
2885
+
2886
+ @node Width
2887
+ @subsection Set/Show Line width
2888
+
2889
+ @table @code
2890
+ @kindex set width @var{column-width}
2891
+ @item set width @var{column-width}
2892
+ Set number of characters the debugger thinks are in a line.
2893
+ We also change OS environment variable @code{COLUMNS}.
2894
+ @kindex show width
2895
+ @item show width
2896
+ Shows the current width setting.
2897
+ @end table
2898
+
2899
+ @node Program Information
2900
+ @section Program Information (@samp{info})
2901
+
2902
+ This @code{info} command (abbreviated @code{i}) is for describing the
2903
+ state of your program. For example, you can list the current
2904
+ parameters with @code{info args}, or list the breakpoints you have set
2905
+ with @code{info breakpoints} or @code{info watchpoints}. You can get
2906
+ a complete list of the @code{info} sub-commands with @w{@code{help
2907
+ info}}.
2908
+
2909
+ @table @code
2910
+ @kindex info args
2911
+
2912
+ @item info args
2913
+ Method arguments of the current stack frame.
2914
+ @kindex info breakpoints
2915
+
2916
+ @item info breakpoints
2917
+ Status of user-settable breakpoints
2918
+ @kindex info display
2919
+
2920
+ @item info display
2921
+ All display expressions.
2922
+ @kindex info files
2923
+
2924
+ @item info files
2925
+ Source files in the program.
2926
+ @kindex info file
2927
+
2928
+ @item info file @var{filename} @ovar{all|lines|mtime|sha1}
2929
+ Information about a specific file. Parameter @code{lines} gives the
2930
+ number of lines in the file, @code{mtime} shows the modification time
2931
+ of the file (if available), @code{sha1} computes a SHA1 has of the
2932
+ data of the file. @code{all} gives all of the above information.
2933
+
2934
+ @kindex info line
2935
+ @item info line
2936
+ Line number and file name of current position in source.
2937
+ @kindex info locals
2938
+ @item info locals
2939
+ Local variables of the current stack frame.
2940
+ @kindex info program
2941
+ @item info program
2942
+ Display information about the status of your program: whether it is
2943
+ running or not and why it stopped. If an unhandled exception occurred,
2944
+ the exception class and @code{to_s} method is called.
2945
+ @kindex info stack
2946
+ @item info stack
2947
+ Backtrace of the stack. An alias for @code{where}. @xref{Backtrace}.
2948
+ @kindex info thread
2949
+ @item info thread @ovar{thread-number} @r{[} terse | verbose@r{]}
2950
+ If no thread number is given, we list info for all
2951
+ threads. @code{terse} and @code{verbose} options are possible. If terse,
2952
+ just give summary thread name information. See information under @code{info threads} for
2953
+ more detail about this summary information.
2954
+
2955
+ If @code{verbose} is appended to the end of the command, then the entire
2956
+ stack trace is given for each thread.
2957
+
2958
+ @kindex info threads @r{[} terse | verbose@r{]}
2959
+ @item info threads
2960
+
2961
+ List information about currently-known threads. This information
2962
+ includes whether the thread is current (+), if it is suspended ($), or
2963
+ ignored (!); the thread number and the top stack item. If
2964
+ @code{verbose} is given then the entire stack frame is shown. Here is
2965
+ an example:
2966
+
2967
+ @smallexample
2968
+ (rdb:7) info threads
2969
+ 1 #<Thread:0xb7d08704 sleep> ./test/thread1.rb:27
2970
+ !2 #<Debugger::DebugThread:0xb7782e4c sleep>
2971
+ 3 #<Thread:0xb777e220 sleep> ./test/thread1.rb:11
2972
+ 4 #<Thread:0xb777e144 sleep> ./test/thread1.rb:11
2973
+ 5 #<Thread:0xb777e07c sleep> ./test/thread1.rb:11
2974
+ 6 #<Thread:0xb777dfb4 sleep> ./test/thread1.rb:11
2975
+ + 7 #<Thread:0xb777deec run> ./test/thread1.rb:14
2976
+ (rdb:1)
2977
+ @end smallexample
2978
+
2979
+ Thread 7 is the current thread since it has a plus sign in front. Thread 2 is
2980
+ ignored since it has a @code{!}. A ``verbose'' listing of the above:
2981
+
2982
+ @smallexample
2983
+ (rdb:7) info threads verbose
2984
+ 1 #<Thread:0xb7d08704 sleep>
2985
+ #0 Integer.join at line test/thread1.rb:27
2986
+ #1 at line test/thread1.rb:27
2987
+ !2 #<Debugger::DebugThread:0xb7782e4c sleep>
2988
+ 3 #<Thread:0xb777e220 sleep>
2989
+ #0 sleep(count#Fixnum) at line test/thread1.rb:11
2990
+ #1 Object.fn(count#Fixnum, i#Fixnum) at line test/thread1.rb:11
2991
+ #2 at line test/thread1.rb:23
2992
+ 4 #<Thread:0xb777e144 sleep>
2993
+ #0 sleep(count#Fixnum) at line test/thread1.rb:11
2994
+ #1 Object.fn(count#Fixnum, i#Fixnum) at line test/thread1.rb:11
2995
+ #2 at line test/thread1.rb:23
2996
+ 5 #<Thread:0xb777e07c sleep>
2997
+ #0 sleep(count#Fixnum) at line test/thread1.rb:11
2998
+ #1 Object.fn(count#Fixnum, i#Fixnum) at line test/thread1.rb:11
2999
+ #2 at line test/thread1.rb:23
3000
+ 6 #<Thread:0xb777dfb4 sleep>
3001
+ #0 sleep(count#Fixnum) at line test/thread1.rb:11
3002
+ #1 Object.fn(count#Fixnum, i#Fixnum) at line test/thread1.rb:11
3003
+ #2 at line test/thread1.rb:23
3004
+ + 7 #<Thread:0xb777deec run>
3005
+ #0 Object.fn(count#Fixnum, i#Fixnum) at line test/thread1.rb:14
3006
+ #1 at line test/thread1.rb:23
3007
+ @end smallexample
3008
+
3009
+ @kindex info variables
3010
+ @item info variables
3011
+ Local and instance variables.
3012
+ @end table
3013
+
3014
+ @node Post-Mortem Debugging
3015
+ @chapter Post-Mortem Debugging
3016
+ @cindex post-mortem debugging
3017
+
3018
+ It is also to possible enter the debugger when you have an uncaught
3019
+ exception that is about to terminate our program. This is called
3020
+ @emph{post-mortem debugging}. In this state many, of the debugger commands
3021
+ for examining variables and moving around in the stack still
3022
+ work. However some commands, such as those which imply a continuation
3023
+ of running code, no longer work.
3024
+
3025
+ The most reliable way to set up post-mortem debugging is to use the
3026
+ @option{--post-mortem} option in invoking @code{rdebug}. See @ref{rdebug
3027
+ command-line options}. This traps/wraps at the debugger ``load'' of
3028
+ your Ruby script. When this is done, your program is stopped after
3029
+ the exception takes place, but before the stack has been
3030
+ unraveled. (Alas, it would be nice to if one could allow resetting the
3031
+ exception and continuing, but details of code in Ruby 1.8's
3032
+ @code{eval.c} prevent this.)
3033
+
3034
+ If however you haven't invoked @code{rdebug} at the outset, but
3035
+ instead call @code{ruby-debug} from inside your program, to set up
3036
+ post-mortem debugging set the @code{post_mortem} key in
3037
+ @code{Debugger.start}. Here's an example modified from
3038
+ @url{http://www.datanoise.com/articles/2006/12/20/post-mortem-debugging}:
3039
+
3040
+ @smallexample
3041
+ $ @b{cat t.rb }
3042
+ require 'rubygems'
3043
+ require 'ruby-debug' ; Debugger.start(:post_mortem => true)
3044
+
3045
+ def t1
3046
+ raise 'test'
3047
+ end
3048
+ def t2
3049
+ t1
3050
+ end
3051
+ t2
3052
+
3053
+ $ @b{ruby t.rb }
3054
+ t.rb:8: raise 'test'
3055
+ (rdb:post-mortem) @b{l=}
3056
+ [3, 12] in t.rb
3057
+ 3
3058
+ 4 Debugger.start
3059
+ 5 Debugger.post_mortem
3060
+ 6
3061
+ 7 def t1
3062
+ => 8 raise 'test'
3063
+ 9 end
3064
+ 10 def t2
3065
+ 11 t1
3066
+ 12 end
3067
+ (rdb:post-mortem)
3068
+ @end smallexample
3069
+
3070
+ Alternatively you can call @code{Debugger.post_mortem()} after rdebug has
3071
+ been started. The @code{post_mortem()} method can be called in two
3072
+ ways. Called without a block, it installs a global @code{at_exit()} hook
3073
+ that intercepts exceptions not handled by your Ruby script. In
3074
+ contrast to using the @option{--post-mortem} option, when this hook
3075
+ occurs after the call stack has been rolled back. (I'm not sure if
3076
+ this in fact makes any difference operationally; I'm just stating it
3077
+ because that's how it works.)
3078
+
3079
+ If you know that a particular block of code raises an exception you
3080
+ can enable post-mortem mode by wrapping this block inside a
3081
+ @code{Debugger.post_mortem} block
3082
+
3083
+ @smallexample
3084
+ def offender
3085
+ 1/0
3086
+ end
3087
+ ...
3088
+ require "ruby-gems"
3089
+ require "ruby-debug"
3090
+ Debugger.post_mortem do
3091
+ ...
3092
+ offender
3093
+ ...
3094
+ end
3095
+ @end smallexample
3096
+
3097
+ Once inside the debugger in post-mortem debugging, the prompt should
3098
+ be @code{(rdb:post-mortem)}.
3099
+
3100
+ @node Debugger Module and Class
3101
+ @chapter The Debugger Module and Class
3102
+
3103
+ @menu
3104
+ * Debugger Module:: ruby-debug's Debugger module
3105
+ * Debugger Class:: Debugger class
3106
+ * Kernel routines:: Routines added to Kernel
3107
+ @end menu
3108
+
3109
+ @node Debugger Module
3110
+ @section The Debugger Module
3111
+
3112
+ @menu
3113
+ * Debugger.run::
3114
+ @ifset LATER
3115
+ * Debugger.post-mortem::
3116
+ @end ifset
3117
+ * Debugger.context::
3118
+ * Debugger.settings::
3119
+ @end menu
3120
+
3121
+ @node Debugger.run
3122
+ @subsection @code{Debugger.start}, @code{Debugger.started?}, @code{Debugger.stop}, @code{Debugger.run_script}
3123
+
3124
+ In order to provide better debugging information regarding the stack
3125
+ frame(s) across all threads, ruby-debug has to intercept each call,
3126
+ save some information and on return remove it. Possibly, in Ruby 1.9
3127
+ possibly this will not be needed. Therefore one has to issue call to
3128
+ indicate start saving information and another call to stop. Of course,
3129
+ If you call ruby-debug from the outset via @code{rdebug} this is done
3130
+ for you.
3131
+
3132
+ @table @code
3133
+ @item Debugger.start(@ovar{options}) @ovar{block}
3134
+ @vindex @code{Debugger.start(options)}
3135
+ @vindex @code{Debugger.start(block)}
3136
+ Turn on add additional instrumentation code to facilitate debugging. A
3137
+ system even table hook is installed and some variables are set up to
3138
+ access thread frames.
3139
+
3140
+ This needs to be done before entering the debugger; therefore a call
3141
+ to the debugger issue a @code{Debugger.start} call if necessary.
3142
+
3143
+ If called without a block, @code{Debugger.start} returns @code{true} if
3144
+ the debugger was already started. But if you want to know if the
3145
+ debugger has already been started @code{Debugger.started?} can tell
3146
+ you.
3147
+
3148
+ If a block is given, the debugger is started and @code{yields} to
3149
+ block. When the block is finished executing, the debugger stopped with
3150
+ the @code{Debugger.stop method}. You will probably want to put a call
3151
+ to @code{debugger} somwhere inside that block
3152
+
3153
+ But if you want to completely stop debugger, you must call
3154
+ @code{Debugger.stop} as many times as you called Debugger.start
3155
+ method.
3156
+
3157
+ The first time Debugger.start is called there is also some additional
3158
+ setup to make the @code{restart} command work. In particular, @code{$0} and
3159
+ @code{ARGV} are used to set internal debugger variables.
3160
+
3161
+ Therefore you should make try to make sure that when
3162
+ @code{Debugger.start} is called neither of these variables has been
3163
+ modified. If instead you don't want this behavior you can pass an
3164
+ options has and set the @code{:init} key to @code{false}. That is
3165
+ @smallexample
3166
+ Debugger.start(:init => false) # or Debugger.start(@{:init => false@})
3167
+ @end smallexample
3168
+
3169
+ If you want post-mortem debugging, you can also supply
3170
+ @code{:post_mortem => true} in @code{Debugger.start}.
3171
+
3172
+ @item Debugger.started?
3173
+ @vindex @code{Debugger.started?}
3174
+ Boolean. Return @code{true} if debugger has been started.
3175
+
3176
+ @item Debugger.stop
3177
+ @vindex @code{Debugger.stop}
3178
+ Turn off instrumentation to allow debugging. Return @code{true} is returned
3179
+ if the debugger is disabled, otherwise it returns @code{false}.
3180
+ @emph{Note that if you want to stop debugger, you must call Debugger.stop
3181
+ as many times as you called the @code{Debugger.start} method.}
3182
+
3183
+ @item Debugger.run_script(@var{debugger-command-file}, out = handler.interface)
3184
+ @vindex @code{Debugger.run_script}
3185
+ Reads/runs the given file containing debugger commands. @code{.rdebugrc} is run this way.
3186
+
3187
+ @item Debugger.last_exception
3188
+ @vindex @code{Debugger.last_exception}
3189
+ If not @code{nil}, this contains @code{$!} from the last exception.
3190
+
3191
+ @end table
3192
+
3193
+ @node Debugger.context
3194
+ @subsection @code{Debugger.context}
3195
+ As mentioned previously, @code{Debugger.start} instruments additional
3196
+ information to be obtained about the current block/frame stack. Here
3197
+ we describe these additional @code{Debugger.context} methods.
3198
+
3199
+ Were a frame position is indicated, it is optional. The top or current frame
3200
+ position (position zero) is used if none is given.
3201
+
3202
+ @table @code
3203
+ @item Debugger.context.frame_args @ovar{frame-position=0}
3204
+ @vindex @code{Debugger.context.frame_args}
3205
+ If track_frame_args? is true, return information saved about call
3206
+ arguments (if any saved) for the given frame position.
3207
+
3208
+ @item Debugger.context.frame_args_info @ovar{frame-position=0}
3209
+ @vindex @code{Debugger.context.frame_args_info}
3210
+
3211
+ @item Debugger.context.frame_class @ovar{frame-position=0}
3212
+ @vindex @code{Debugger.context.frame_args_info}
3213
+ Return the class of the current frame stack.
3214
+
3215
+ @item Debugger.context.frame_file @ovar{frame-position=0}
3216
+ @vindex @code{Debugger.context.frame_file}
3217
+ Return the filename of the location of the indicated frame position.
3218
+
3219
+ @item Debugger.context.frame_id @ovar{frame-position=0}
3220
+ @vindex @code{Debugger.context.frame_id}
3221
+ Same as @code{Debugger.context.method}.
3222
+
3223
+ @item Debugger.context.frame_line @ovar{frame-position=0}
3224
+ @vindex @code{Debugger.context.frame_line}
3225
+ Return the filename of the location of the indicated frame position.
3226
+
3227
+ @item Debugger.context.frame_method @ovar{frame-position=0}
3228
+ @vindex @code{Debugger.context.frame_method}
3229
+ Symbol of the method name of the indicated frame position.
3230
+
3231
+ @item Debugger.context.stack_size
3232
+ @vindex @code{Debugger.context.stack_size}
3233
+ Return the number the size of the frame stack. Note this may be less
3234
+ that the actual frame stack size if debugger recording
3235
+ (@code{Debugger.start}) was turned on at after some blocks were added
3236
+ and not finished when the @code{Debugger.start} was issued.
3237
+ @end table
3238
+
3239
+ @node Debugger.settings
3240
+ @subsection @code{Debugger.settings}
3241
+ @vindex @code{Debugger.settings}
3242
+ Symbols listed here are keys into the Array @code{Debugger.settings}.
3243
+ These can be set any time after the @code{ruby-debug} is loaded. For example:
3244
+ @smallexample
3245
+ require "ruby-debug/debugger"
3246
+ Debugger.settings[:autoeval] = true # try eval on unknown debugger commands
3247
+ Debugger.listsize = 20 # Show 20 lines in a list command
3248
+ @end smallexample
3249
+
3250
+ @table @code
3251
+ @item :argv
3252
+ Array of String. @code{argv[0]} is the debugged program name and
3253
+ @code{argv[1..-1]} are the command arguments to it.
3254
+ @item :autoeval
3255
+ Boolean. True if auto autoeval on. @xref{Autoeval}.
3256
+ @item :autoirb
3257
+ Fixnum: 1 if on or 0 if off. @xref{Autoirb}.
3258
+ @item :autolist
3259
+ Fixnum: 1 if on or 0 if off.
3260
+ @item :basename
3261
+ Boolean. True if basename on. @xref{Basename}.
3262
+ @item :callstyle
3263
+ Symbol: @code{:short} or @code{:last}. @xref{Callstyle}.
3264
+ @item :debuggertesting
3265
+ Boolean. True if currently testing the debugger.
3266
+ @item :force_stepping
3267
+ Boolean. True if stepping should go to a line different from the last
3268
+ step. @xref{Forcestep}.
3269
+ @item :full_path
3270
+ Boolean. @xref{Fullpath}.
3271
+ @item :listsize
3272
+ Fixnum. Number of lines to show in a @code{list} command. @xref{Listsize}.
3273
+ @item :reload_source_on_change
3274
+ Boolean. True if we should reread the source every time it changes. @xref{Autoreload}.
3275
+ @item :stack_trace_on_error
3276
+ Boolean. True if we should produce a stack trace on error. @xref{Trace}.
3277
+ @item :width
3278
+ Fixnum. Number of characters the debugger thinks are in a line. @xref{Width}.
3279
+ @end table
3280
+
3281
+ @node Debugger Class
3282
+ @section The @code{Debugger} Class
3283
+ @menu
3284
+ * Debugger.Breakpoint:: Debugger::Breakpoint
3285
+ * Debugger.Context:: Debugger::Context
3286
+ * Debugger.Command:: Debugger::Command
3287
+ @end menu
3288
+
3289
+ @table @code
3290
+ @item add_breakpoint(file, line, expr)
3291
+ @vindex @code{Debugger.add_breakpoint}
3292
+ Adds a breakpoint in file @var{file}, at line @var{line}. If
3293
+ @var{expr} is not nil, it is evaluated and a breakpoint takes effect
3294
+ at the indicated position when that expression is true. You should
3295
+ verify that @var{expr} is syntactically valid or a @code{SyntaxError}
3296
+ exception, and unless your code handles this the debugged program may
3297
+ terminate.
3298
+
3299
+ @item remove_breakpoint(bpnum)
3300
+ @vindex @code{Debugger.remove_breakpoint}
3301
+ When a breakpoint is added, it is assigned a number as a way to
3302
+ uniquely identify it. (There can be more than one breakpoint on a
3303
+ given line.) To remove a breakpoint, use @code{remove_breakpoint} with
3304
+ breakpoint number @var{bpnum}.
3305
+
3306
+ @item breakpoints
3307
+ @vindex @code{Debugger.breakpoints}
3308
+ Return a list of the breakpoints that have been added but not removed.
3309
+ @end table
3310
+
3311
+ @node Debugger.Breakpoint
3312
+ @subsection The @code{Debugger::Breakpoint} Class
3313
+ Breakpoint are objects in the @code{Debugger::Breakpoint} class.
3314
+ @table @code
3315
+ @item enabled?
3316
+ @vindex @code{Debugger::Breakpoints.enabled?}
3317
+ Returns whether breakpoint is enabled or not.
3318
+
3319
+ @item enabled=
3320
+ @vindex @code{Debugger::Breakpoints.enabled=}
3321
+ Sets whether breakpoint is enabled or not.
3322
+
3323
+ @item expr
3324
+ @vindex @code{Debugger::Breakpoints.expr}
3325
+ Expression which has to be true at the point where the breakpoint is
3326
+ set before we stop.
3327
+
3328
+ @item expr=
3329
+ @vindex @code{Debugger::Breakpoints.expr=}
3330
+
3331
+ @item hit_condition
3332
+ @item hit_condition=
3333
+ @vindex @code{Debugger::Breakpoints.condition}
3334
+ @vindex @code{Debugger::Breakpoints.condition=}
3335
+
3336
+ @item hit_count
3337
+ @vindex @code{Debugger::Breakpoints.hit_count}
3338
+ Returns the hit count of the breakpoint.
3339
+
3340
+ @item hit_value
3341
+ @vindex @code{Debugger::Breakpoints.hit_value}
3342
+ Returns the hit value of the breakpoint.
3343
+
3344
+ @item hit_value=
3345
+ @vindex @code{Debugger::Breakpoints.hit_value=}
3346
+ Sets the hit value of the breakpoint.
3347
+
3348
+ @item id
3349
+ @cindex @code{Debugger::Breakpoints.id}
3350
+ A numeric name for the breakpoint which is used in listing breakpoints
3351
+ and removing, enabling or disabling the breakpoint
3352
+
3353
+ @item pos
3354
+ @vindex @code{Debugger::Breakpoints.pos=}
3355
+ Returns the line number of this breakpoint.
3356
+ @item pos=
3357
+ @vindex @code{Debugger::Breakpoints.pos=}
3358
+ Sets the line number of this breakpoint.
3359
+
3360
+ @item source
3361
+ @vindex @code{Debugger::Breakpoints.source}
3362
+ Returns the file name in which the breakpoint occurs.
3363
+
3364
+ @item source=
3365
+ @vindex @code{Debugger::Breakpoints.source=}
3366
+ Sets the file name in which the breakpoint occurs.
3367
+ @end table
3368
+
3369
+ @node Debugger.Context
3370
+ @subsection The @code{Debugger::Context} Class
3371
+ Callbacks in @code{Debugger:Context} get called when a stopping point
3372
+ or an event is reached. It has information about the suspended program
3373
+ which enable a debugger to inspect the frame stack, evaluate variables
3374
+ from the perspective of the debugged program, and contains information
3375
+ about the place the debugged program is stopped.
3376
+
3377
+ @table @code
3378
+ @item at_line(@var{file}, @var{line})
3379
+ @vindex Debugger::Context::at_line(@var{file}, @var{line})
3380
+ This routine is called when the debugger encounters a ``line'' event for
3381
+ which it has been indicated we want to stop at, such as by hitting a
3382
+ breakpoint or by some sort of stepping.
3383
+
3384
+ @item at_return(@var{file}, @var{line})
3385
+ @vindex Debugger::Context::at_return(@var{file}, @var{line})
3386
+ This routine is called when the debugger encounters a ``return'' event for
3387
+ which it has been indicated we want to stop at, such as by hitting a
3388
+ @code{finish} statement.
3389
+
3390
+ @item debug_load(@var{file}, @var{stop-initially})
3391
+ @vindex Debugger::Context::debug_load(@var{file}, @var{stop-initially})
3392
+ This method should be used to debug a file. If the file terminates
3393
+ normally, @code{nil} is returned. If not a backtrace is returned.
3394
+
3395
+ The @var{stop-initially} parameter indicates whether the program
3396
+ should stop after loading. If an explicit call to the debugger is in
3397
+ the debugged program, you may want to set this @code{false}.
3398
+ @end table
3399
+
3400
+ @node Debugger.Command
3401
+ @subsection The @code{Debugger::Command} Class
3402
+
3403
+ Each command you run is in fact its own class. Should you want to extend
3404
+ ruby-debug, it's pretty easy to do since after all ruby-debug is Ruby.
3405
+
3406
+ Each @code{Debugger#Command} class should have the a @code{regexp}
3407
+ method. This method returns regular expression for command-line
3408
+ strings that match your command. It's up to you to make sure this
3409
+ regular expression doesn't conflict with another one. If it does, it's
3410
+ undefined which one will get matched and run
3411
+
3412
+ In addition the instance needs these methods:
3413
+ @table @code
3414
+ @item execute
3415
+ Code which gets run when you type a command (string) that matches the
3416
+ commands regular expression.
3417
+ @item help
3418
+ A string which gets displayed when folks as for help on that command
3419
+ @item help_command
3420
+ A name used the help system uses to show what commands are available.
3421
+ @end table
3422
+
3423
+ Here's a small example of a new command:
3424
+ @smallexample
3425
+ module Debugger
3426
+ class MyCommand < Command
3427
+ def regexp
3428
+ /^\s*me$/ # Regexp that will match your command
3429
+ end
3430
+
3431
+ def execute
3432
+ puts "hi" # What you want to happen when your command runs
3433
+ end
3434
+ class << self
3435
+ def help_command
3436
+ 'me' # String name of command
3437
+ end
3438
+ def help(cmd)
3439
+ # Some sort of help text.
3440
+ %@{This does whatever it is I want to do@}
3441
+ end
3442
+ end
3443
+ end
3444
+ @end smallexample
3445
+
3446
+ Now here's an example of how you can load/use it:
3447
+ @smallexample
3448
+ require 'rubygems'
3449
+ require 'ruby-debug'
3450
+ require '/tmp/mycmd.rb' # or wherever
3451
+ Debugger.start
3452
+ x=1
3453
+ debugger
3454
+ y=2
3455
+ @end smallexample
3456
+
3457
+ And now an example of invoking it:
3458
+ @smallexample
3459
+ ruby /tmp/testit.rb:
3460
+ /tmp/testit.rb:7
3461
+ y=2
3462
+ (rdb:1) help
3463
+ ruby-debug help v0.10.3
3464
+ Type 'help <command-name>' for help on a specific command
3465
+ Available commands:
3466
+ backtrace delete enable help method putl set trace
3467
+ break disable eval info next quit show undisplay
3468
+ catch display exit irb p reload source up
3469
+ condition down finish list pp restart step var
3470
+ continue edit frame me ps save thread where
3471
+ ^^ This is you
3472
+
3473
+ (rdb:1) help me
3474
+ This does whatever it is I want to do
3475
+ (rdb:1) me
3476
+ hi
3477
+ (rdb:1)
3478
+ @end smallexample
3479
+
3480
+ @node Kernel routines
3481
+ @section Additions to @code{Kernel}
3482
+
3483
+ @table @code
3484
+
3485
+ @item debugger @ovar{steps=1}
3486
+ @vindex @code{Kernel::debugger}
3487
+ Enters the debugger in the current thread after a stepping @var{steps} line-event steps.
3488
+ Before entering the debugger startup script is read.
3489
+
3490
+ Setting @var{steps} to 0 will cause a break in the debugger subroutine
3491
+ and not wait for eany line event to occur. This could be useful you
3492
+ want to stop right after the last statement in some scope.
3493
+
3494
+ Consider this example:
3495
+ @smallexample
3496
+ $ cat scope-test.rb
3497
+
3498
+ require 'rubygems'
3499
+ require 'ruby-debug' ; Debugger.start
3500
+ 1.times do
3501
+ a = 1
3502
+ debugger # implied steps=1
3503
+ end
3504
+ y = 1
3505
+
3506
+ $ scope-test.rb:8
3507
+ y = 1
3508
+ (rdb:1) p a
3509
+ NameError Exception: undefined local variable or method `a' for main:Object
3510
+ (rdb:1)
3511
+ @end smallexample
3512
+ The debugger will get at the line event which follows @samp{a=1}. This
3513
+ is outside the @code{do} block scope where @var{a} is defined. If
3514
+ instead you want to stop before leaving the @code{do} loop it is
3515
+ possibly to stop right inside the @code{debugger}; call with 0 zero parameter:
3516
+ @smallexample
3517
+ $ cat scope-test.rb
3518
+
3519
+ require 'rubygems'
3520
+ require 'ruby-debug' ; Debugger.start
3521
+ 1.times do
3522
+ a = 1
3523
+ debugger(0)
3524
+ end
3525
+ y = 1
3526
+
3527
+ $ scope-test.rb:8
3528
+ ../lib/ruby-debug-base.rb:175
3529
+ Debugger.current_context.stop_frame = 0
3530
+ (rdb:1) where
3531
+ --> #0 Kernel.debugger(steps#Fixnum) at line ../lib/ruby-debug-base.rb:175
3532
+ #1 at line scope-test.rb:6
3533
+ #2 at line scope-test.rb:4
3534
+ (rdb:1) up
3535
+ #1 at line scope-test.rb:6
3536
+ (rdb:1) p a
3537
+ 1
3538
+ (rdb:1)
3539
+ @end smallexample
3540
+ As seen above you will have to position the frame up one to be back in
3541
+ your debugged program rather than in the debugger.
3542
+
3543
+ @item breakpoint @ovar{steps=1}
3544
+ @vindex @code{Kernel::breakpoint}
3545
+ An alias for debugger.
3546
+
3547
+ @item binding_n @ovar{n=0}
3548
+ @vindex @code{Kernel::binding_n}
3549
+ Returns a @samp{binding()} for the @var{n}-th call frame. Note however
3550
+ that you need to first call @samp{Debugger.start} before issuing this
3551
+ call.
3552
+
3553
+ @end table
3554
+
3555
+ @node Using from GitHub
3556
+ @appendix Building and Installing from the GitHub Repository
3557
+
3558
+ Here are Unix-centric instructions. If you have Microsoft Windows or
3559
+ OSX some of the below may need adjusting.
3560
+
3561
+ @menu
3562
+ * Prerequisites::
3563
+ * Package Checkout::
3564
+ * Trying Out::
3565
+ * Running Regression Tests::
3566
+ * Building the Documentation and Emacs files::
3567
+ * Building for Microsoft Windows::
3568
+ @end menu
3569
+
3570
+ @node Prerequisites
3571
+ @section Prerequisites: To build the package you'll need at a minimum:
3572
+
3573
+ @itemize @bullet
3574
+ @item
3575
+ Ruby (of course). Currently only version 1.8.6 and above but not
3576
+ version 1.9.@emph{x} work.
3577
+ @item
3578
+ Ruby development headers. This typically includes a file called @file{ruby.h}
3579
+ @item
3580
+ A C compiler like GNU C (@code{gcc})
3581
+ @item
3582
+ Rake
3583
+ @item
3584
+ Subversion (@code{svn}).
3585
+ @end itemize
3586
+
3587
+ If you want to build the documentation and install Emacs files, you'll
3588
+ also need:
3589
+
3590
+ @itemize @bullet
3591
+ @item
3592
+ a POSIX shell like bash
3593
+ @item
3594
+ autoconf
3595
+ @item
3596
+ automake
3597
+ @item
3598
+ GNU Make
3599
+ @item
3600
+ texinfo
3601
+ @end itemize
3602
+
3603
+ @node Package Checkout
3604
+ @section Basic Package Checkout and Installation
3605
+
3606
+ Check out the trunk of repository following the instructions at
3607
+ @url{http://rubyforge.org/scm/?group_id=1900} For example on a Unixy system,
3608
+ this may work:
3609
+
3610
+ @smallexample
3611
+ mkdir ruby-debug
3612
+ cd ruby-debug
3613
+ svn checkout svn://rubyforge.org/var/svn/ruby-debug/trunk trunk
3614
+ @end smallexample
3615
+
3616
+ In order to make the Ruby gems, @code{ruby-debug} and
3617
+ @code{ruby-debug-base}, get yourself into the trunk directory after
3618
+ the code has been checked out and run:
3619
+
3620
+ @smallexample
3621
+ cd trunk # This is the same trunk checked out above.
3622
+ rake package
3623
+ @end smallexample
3624
+
3625
+ If all goes well you should have some gem files put in the directory
3626
+ @code{pkg}. Use the gem command to install that.
3627
+
3628
+ @smallexample
3629
+ sudo gem install ruby-debug-*.gem # See gem help for other possibilities
3630
+ @end smallexample
3631
+
3632
+ If all goes well the rdebug script has been installed ruby-debug is
3633
+ now ready to run. But if everything goes well you might want to run
3634
+ the built-in regression tests to make sure everything is okay.
3635
+ See step 3 below.
3636
+
3637
+ If the gem install didn't work,'t there may be a problem with your C
3638
+ compiler or the Ruby headers are not installed.
3639
+
3640
+ @node Trying Out
3641
+ @section Trying Out without Installing
3642
+
3643
+ You don't have to build a gem file to try out ruby debug. In fact when
3644
+ developing new features for ruby-debug, developers often you want to
3645
+ try it out @emph{before} installing. If you have a problem in the latter
3646
+ part of step 1 you may want to try this approach since we go into a
3647
+ little more detail as to what happens under the covers when you do the
3648
+ gem install.
3649
+
3650
+ Run (from trunk)
3651
+ @smallexample
3652
+ rake lib
3653
+ @end smallexample
3654
+
3655
+ This creates a Makefile and builds the ruby-debug shared library. (On
3656
+ Unix the name is @code{ruby_debug.so}).
3657
+
3658
+ Once this is done you can run the debugger as you would rdebug using the
3659
+ script @code{runner.sh}. For example (again from trunk)
3660
+
3661
+ @smallexample
3662
+ ./runner.sh ~/my-ruby-program.rb
3663
+ @end smallexample
3664
+
3665
+ @node Running Regression Tests
3666
+ @section Running the Regression Tests
3667
+
3668
+ We've put together some basic tests to make sure ruby-debug is doing
3669
+ what we think it should do. To run these (from @code{trunk}):
3670
+
3671
+ @smallexample
3672
+ rake test
3673
+ @end smallexample
3674
+
3675
+ If you didn't build the ruby-debug shared library and skipped step 2,
3676
+ don't worry @code{rake test} will do step 2 for you. You should see a
3677
+ line that ends something like:
3678
+
3679
+ @smallexample
3680
+ Finished in 2.767579 seconds.
3681
+
3682
+ 12 tests, 35 assertions, 0 failures, 0 errors
3683
+ @end smallexample
3684
+
3685
+ The number of seconds, tests, and assertions may be different from the
3686
+ above. However you @emph{should} see exactly ``0 failures, 0 errors.''
3687
+
3688
+ @node Building the Documentation and Emacs files
3689
+ @section Building the Documentation and Testing/Installing Emacs Files
3690
+
3691
+ Of course, I recommend you read the ruby-debug manual that comes with
3692
+ the package. If you have the prerequisites described above, run this
3693
+ once:
3694
+ @smallexample
3695
+ sh ./autogen.sh
3696
+ @end smallexample
3697
+
3698
+ Then run:
3699
+ @smallexample
3700
+ ./configure
3701
+ make
3702
+ make test # Runs Emacs regression tests
3703
+ sudo make install # Or arrange to do this as root
3704
+ @end smallexample
3705
+
3706
+ @node Building for Microsoft Windows
3707
+ @section Building for Microsoft Windows
3708
+
3709
+ Microsoft Windows is ``special'' and building @code{ruby-debug-base}
3710
+ on it requires extra care. A problem here seems to be that the
3711
+ ``One-click'' install is compiled using Microsoft Visual Studio C, version 6
3712
+ which is not sold anymore and is rather old.
3713
+
3714
+ Instead I suggest building via mingw/msys.
3715
+ @url{http://eigenclass.org/hiki.rb?cmd=view&p=cross+compiling+rcovrt&key=mingw}
3716
+ has instructions on how to do. Some amendments to these instructions.
3717
+
3718
+ First, those instructions are a little GNU/Linux centric. If you are
3719
+ using Ubuntu or Debian, then this should be the easiest to follow the
3720
+ instructions. On Ubuntu or Debian there is a mingw3 Debian
3721
+ package. Installing that will give you the cross compiler that is a
3722
+ prerequisite. Alternatively if you are running MS Windows I notice
3723
+ that cygwin also has a mingw package. Or possibly you could use MinGW
3724
+ directly. For other OS's you might have to build a cross-compiler,
3725
+ i.e. gcc which emits win32 code and can create a win32 DLL.
3726
+
3727
+ After you have a cross compiler you need to download the Ruby source
3728
+ and basically build a ruby interpreter. The cross-compile.sh script
3729
+ works although when I downloaded it, it had lots of blank space at the
3730
+ beginning which will mess up the Unix magic interpretation. That is
3731
+ remove the blanks in front of @code{#/bin/sh}.
3732
+
3733
+ On my system, this script fails in running @code{make ruby} because the
3734
+ fake.rb that got created needed to have a small change:
3735
+
3736
+ @smallexample
3737
+
3738
+ ALT_SEPARATOR = "\"; \
3739
+ @end smallexample
3740
+ should be:
3741
+ @smallexample
3742
+ ALT_SEPARATOR = "\\"; \
3743
+ @end smallexample
3744
+
3745
+ After fixing this, run @code{make ruby}. Also, I needed to run
3746
+ @code{make rubyw}.
3747
+
3748
+ And then @code{make install} as indicated.
3749
+
3750
+ Once all of that's in place, the place you want be is in
3751
+ @code{ruby-debug/trunk/ext/win32}, not @code{ruby-debug/ext}.
3752
+
3753
+ So let's say you've installed the cross-compiled install ruby in
3754
+ @code{/usr/local/ruby-mingw32/}. Here then are the commands to build @code{ruby-debug-base-}@emph{xxx}@code{-mswin32.gem}:
3755
+ @smallexample
3756
+ cd .../ruby-debug/trunk/ext/win32
3757
+ ruby -I /usr/local/ruby-mingw32/lib/ruby/1.8/i386-mingw32 ../extconf.rb
3758
+ make # Not rake
3759
+ cd ../.. # back in ruby-debug/trunk
3760
+ rake win32_gem
3761
+ @end smallexample
3762
+
3763
+ @node Class Module Index
3764
+ @unnumbered Class, Module Method Index
3765
+ @printindex vr
3766
+
3767
+ @node Command Index
3768
+ @unnumbered Command Index
3769
+ @printindex ky
3770
+
3771
+ @node General Index
3772
+ @unnumbered General Index
3773
+ @printindex cp
3774
+
3775
+ @tex
3776
+ % I think something like @colophon should be in texinfo. In the
3777
+ % meantime:
3778
+ \long\def\colophon{\hbox to0pt{}\vfill
3779
+ \centerline{The body of this manual is set in}
3780
+ \centerline{\fontname\tenrm,}
3781
+ \centerline{with headings in {\bf\fontname\tenbf}}
3782
+ \centerline{and examples in {\tt\fontname\tentt}.}
3783
+ \centerline{{\it\fontname\tenit\/},}
3784
+ \centerline{{\bf\fontname\tenbf}, and}
3785
+ \centerline{{\sl\fontname\tensl\/}}
3786
+ \centerline{are used for emphasis.}\vfill}
3787
+ \page\colophon
3788
+ % Blame: doc@cygnus.com, 1991.
3789
+ @end tex
3790
+
3791
+ @bye