Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / html / debugging_rails_applications.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5 <title>Debugging Rails Applications</title>
6 <!--[if lt IE 8]>
7 <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
8 <![endif]-->
9 <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
10 <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
11 <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
12 <style type="text/css">
13 div#container {
14 max-width: 900px;
15 padding-bottom: 3em;
16 }
17
18 div#content {
19 margin-left: 200px;
20 }
21
22 div#container.notoc {
23 max-width: 600px;
24 }
25
26 .notoc div#content {
27 margin-left: 0;
28 }
29
30 pre {
31 line-height: 1.4em;
32 }
33
34 #content p tt {
35 background: #eeeeee;
36 border: solid 1px #cccccc;
37 padding: 3px;
38 }
39
40 dt {
41 font-weight: bold;
42 }
43
44 #content dt tt {
45 font-size: 10pt;
46 }
47
48 dd {
49 margin-left: 3em;
50 }
51
52 #content dt tt, #content pre tt {
53 background: none;
54 padding: 0;
55 border: 0;
56 }
57
58 #content .olist ol {
59 margin-left: 2em;
60 }
61
62 #header {
63 position: relative;
64 max-width: 840px;
65 margin-left: auto;
66 margin-right: auto;
67 }
68
69 #header.notoc {
70 max-width: 580px;
71 }
72
73 #logo {
74 position: absolute;
75 left: 10px;
76 top: 10px;
77 width: 110px;
78 height: 140px;
79 }
80
81 div#header h1#site_title {
82 background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
83 position: absolute;
84 width: 392px;
85 height: 55px;
86 left: 145px;
87 top: 20px;
88 margin: 0;
89 padding: 0;
90 }
91
92 #site_title span {
93 display: none;
94 }
95
96 #site_title_tagline {
97 display: none;
98 }
99
100 ul#navMain {
101 position: absolute;
102 margin: 0;
103 padding: 0;
104 top: 97px;
105 left: 145px;
106 }
107
108 .left-floaty, .right-floaty {
109 padding: 15px;
110 }
111
112 .admonitionblock,
113 .tableblock {
114 margin-left: 1em;
115 margin-right: 1em;
116 margin-top: 0.25em;
117 margin-bottom: 1em;
118 }
119
120 .admonitionblock .icon {
121 padding-right: 8px;
122 }
123
124 .admonitionblock .content {
125 border: solid 1px #ffda78;
126 background: #fffebd;
127 padding: 10px;
128 padding-top: 8px;
129 padding-bottom: 8px;
130 }
131
132 .admonitionblock .title {
133 font-size: 140%;
134 margin-bottom: 0.5em;
135 }
136
137 .tableblock table {
138 border: solid 1px #aaaaff;
139 background: #f0f0ff;
140 }
141
142 .tableblock th {
143 background: #e0e0e0;
144 }
145
146 .tableblock th,
147 .tableblock td {
148 padding: 3px;
149 padding-left: 5px;
150 padding-right: 5px;
151 }
152
153 .sidebarblock {
154 margin-top: 0.25em;
155 margin: 1em;
156 border: solid 1px #ccccbb;
157 padding: 8px;
158 background: #ffffe0;
159 }
160
161 .sidebarblock .sidebar-title {
162 font-size: 140%;
163 font-weight: 600;
164 margin-bottom: 0.3em;
165 }
166
167 .sidebarblock .sidebar-content > .para:last-child > p {
168 margin-bottom: 0;
169 }
170
171 .sidebarblock .sidebar-title a {
172 text-decoration: none;
173 }
174
175 .sidebarblock .sidebar-title a:hover {
176 text-decoration: underline;
177 }
178
179 </style>
180 </head>
181 <body>
182 <div id="header" >
183 <div id="logo">
184 <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
185 </div>
186
187 <h1 id="site_title"><span>Ruby on Rails</span></h1>
188 <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
189
190 <ul id="navMain">
191 <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
192 <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
193 </ul>
194 </div>
195
196 <div id="container">
197
198 <div id="sidebar">
199 <h2>Chapters</h2>
200 <ol>
201 <li>
202 <a href="#_view_helpers_for_debugging">View Helpers for Debugging</a>
203 <ul>
204
205 <li><a href="#_debug">debug</a></li>
206
207 <li><a href="#_to_yaml">to_yaml</a></li>
208
209 <li><a href="#_inspect">inspect</a></li>
210
211 <li><a href="#_debugging_javascript">Debugging Javascript</a></li>
212
213 </ul>
214 </li>
215 <li>
216 <a href="#_the_logger">The Logger</a>
217 <ul>
218
219 <li><a href="#_what_is_the_logger">What is The Logger?</a></li>
220
221 <li><a href="#_log_levels">Log Levels</a></li>
222
223 <li><a href="#_sending_messages">Sending Messages</a></li>
224
225 </ul>
226 </li>
227 <li>
228 <a href="#_debugging_with_ruby_debug">Debugging with ruby-debug</a>
229 <ul>
230
231 <li><a href="#_setup">Setup</a></li>
232
233 <li><a href="#_the_shell">The Shell</a></li>
234
235 <li><a href="#_the_context">The Context</a></li>
236
237 <li><a href="#_threads">Threads</a></li>
238
239 <li><a href="#_inspecting_variables">Inspecting Variables</a></li>
240
241 <li><a href="#_step_by_step">Step by Step</a></li>
242
243 <li><a href="#_breakpoints">Breakpoints</a></li>
244
245 <li><a href="#_catching_exceptions">Catching Exceptions</a></li>
246
247 <li><a href="#_resuming_execution">Resuming Execution</a></li>
248
249 <li><a href="#_editing">Editing</a></li>
250
251 <li><a href="#_quitting">Quitting</a></li>
252
253 <li><a href="#_settings">Settings</a></li>
254
255 </ul>
256 </li>
257 <li>
258 <a href="#_debugging_memory_leaks">Debugging Memory Leaks</a>
259 <ul>
260
261 <li><a href="#_bleakhouse">BleakHouse</a></li>
262
263 <li><a href="#_valgrind">Valgrind</a></li>
264
265 </ul>
266 </li>
267 <li>
268 <a href="#_plugins_for_debugging">Plugins for Debugging</a>
269 </li>
270 <li>
271 <a href="#_references">References</a>
272 </li>
273 <li>
274 <a href="#_changelog">Changelog</a>
275 </li>
276 </ol>
277 </div>
278
279 <div id="content">
280 <h1>Debugging Rails Applications</h1>
281 <div id="preamble">
282 <div class="sectionbody">
283 <div class="para"><p>This guide introduces techniques for debugging Ruby on Rails applications. By referring to this guide, you will be able to:</p></div>
284 <div class="ilist"><ul>
285 <li>
286 <p>
287 Understand the purpose of debugging
288 </p>
289 </li>
290 <li>
291 <p>
292 Track down problems and issues in your application that your tests aren't identifying
293 </p>
294 </li>
295 <li>
296 <p>
297 Learn the different ways of debugging
298 </p>
299 </li>
300 <li>
301 <p>
302 Analyze the stack trace
303 </p>
304 </li>
305 </ul></div>
306 </div>
307 </div>
308 <h2 id="_view_helpers_for_debugging">1. View Helpers for Debugging</h2>
309 <div class="sectionbody">
310 <div class="para"><p>One common task is to inspect the contents of a variable. In Rails, you can do this with three methods:</p></div>
311 <div class="ilist"><ul>
312 <li>
313 <p>
314 <tt>debug</tt>
315 </p>
316 </li>
317 <li>
318 <p>
319 <tt>to_yaml</tt>
320 </p>
321 </li>
322 <li>
323 <p>
324 <tt>inspect</tt>
325 </p>
326 </li>
327 </ul></div>
328 <h3 id="_debug">1.1. debug</h3>
329 <div class="para"><p>The <tt>debug</tt> helper will return a &lt;pre&gt;-tag that renders the object using the YAML format. This will generate human-readable data from any object. For example, if you have this code in a view:</p></div>
330 <div class="listingblock">
331 <div class="content"><!-- Generator: GNU source-highlight 2.9
332 by Lorenzo Bettini
333 http://www.lorenzobettini.it
334 http://www.gnu.org/software/src-highlite -->
335 <pre><tt>&lt;%= debug @post %&gt;
336 <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
337 <span style="font-weight: bold"><span style="color: #0000FF">&lt;b&gt;</span></span>Title:<span style="font-weight: bold"><span style="color: #0000FF">&lt;/b&gt;</span></span>
338 &lt;%=h @post.title %&gt;
339 <span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
340 </tt></pre></div></div>
341 <div class="para"><p>You'll see something like this:</p></div>
342 <div class="listingblock">
343 <div class="content">
344 <pre><tt>--- !ruby/object:Post
345 attributes:
346 updated_at: 2008-09-05 22:55:47
347 body: It's a very helpful guide for debugging your Rails app.
348 title: Rails debugging guide
349 published: t
350 id: "1"
351 created_at: 2008-09-05 22:55:47
352 attributes_cache: {}
353
354
355 Title: Rails debugging guide</tt></pre>
356 </div></div>
357 <h3 id="_to_yaml">1.2. to_yaml</h3>
358 <div class="para"><p>Displaying an instance variable, or any other object or method, in yaml format can be achieved this way:</p></div>
359 <div class="listingblock">
360 <div class="content"><!-- Generator: GNU source-highlight 2.9
361 by Lorenzo Bettini
362 http://www.lorenzobettini.it
363 http://www.gnu.org/software/src-highlite -->
364 <pre><tt>&lt;%= simple_format @post.to_yaml %&gt;
365 <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
366 <span style="font-weight: bold"><span style="color: #0000FF">&lt;b&gt;</span></span>Title:<span style="font-weight: bold"><span style="color: #0000FF">&lt;/b&gt;</span></span>
367 &lt;%=h @post.title %&gt;
368 <span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
369 </tt></pre></div></div>
370 <div class="para"><p>The <tt>to_yaml</tt> method converts the method to YAML format leaving it more readable, and then the <tt>simple_format</tt> helper is used to render each line as in the console. This is how <tt>debug</tt> method does its magic.</p></div>
371 <div class="para"><p>As a result of this, you will have something like this in your view:</p></div>
372 <div class="listingblock">
373 <div class="content">
374 <pre><tt>--- !ruby/object:Post
375 attributes:
376 updated_at: 2008-09-05 22:55:47
377 body: It's a very helpful guide for debugging your Rails app.
378 title: Rails debugging guide
379 published: t
380 id: "1"
381 created_at: 2008-09-05 22:55:47
382 attributes_cache: {}
383
384 Title: Rails debugging guide</tt></pre>
385 </div></div>
386 <h3 id="_inspect">1.3. inspect</h3>
387 <div class="para"><p>Another useful method for displaying object values is <tt>inspect</tt>, especially when working with arrays or hashes. This will print the object value as a string. For example:</p></div>
388 <div class="listingblock">
389 <div class="content"><!-- Generator: GNU source-highlight 2.9
390 by Lorenzo Bettini
391 http://www.lorenzobettini.it
392 http://www.gnu.org/software/src-highlite -->
393 <pre><tt>&lt;%= [1, 2, 3, 4, 5].inspect %&gt;
394 <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
395 <span style="font-weight: bold"><span style="color: #0000FF">&lt;b&gt;</span></span>Title:<span style="font-weight: bold"><span style="color: #0000FF">&lt;/b&gt;</span></span>
396 &lt;%=h @post.title %&gt;
397 <span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
398 </tt></pre></div></div>
399 <div class="para"><p>Will be rendered as follows:</p></div>
400 <div class="listingblock">
401 <div class="content">
402 <pre><tt>[1, 2, 3, 4, 5]
403
404 Title: Rails debugging guide</tt></pre>
405 </div></div>
406 <h3 id="_debugging_javascript">1.4. Debugging Javascript</h3>
407 <div class="para"><p>Rails has built-in support to debug RJS, to active it, set <tt>ActionView::Base.debug_rjs</tt> to <em>true</em>, this will specify whether RJS responses should be wrapped in a try/catch block that alert()s the caught exception (and then re-raises it).</p></div>
408 <div class="para"><p>To enable it, add the following in the <tt>Rails::Initializer do |config|</tt> block inside <tt>environment.rb</tt>:</p></div>
409 <div class="listingblock">
410 <div class="content"><!-- Generator: GNU source-highlight 2.9
411 by Lorenzo Bettini
412 http://www.lorenzobettini.it
413 http://www.gnu.org/software/src-highlite -->
414 <pre><tt>config<span style="color: #990000">.</span>action_view<span style="color: #990000">[:</span>debug_rjs<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
415 </tt></pre></div></div>
416 <div class="para"><p>Or, at any time, setting <tt>ActionView::Base.debug_rjs</tt> to <em>true</em>:</p></div>
417 <div class="listingblock">
418 <div class="content"><!-- Generator: GNU source-highlight 2.9
419 by Lorenzo Bettini
420 http://www.lorenzobettini.it
421 http://www.gnu.org/software/src-highlite -->
422 <pre><tt>ActionView<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>debug_rjs <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
423 </tt></pre></div></div>
424 <div class="admonitionblock">
425 <table><tr>
426 <td class="icon">
427 <img src="./images/icons/tip.png" alt="Tip" />
428 </td>
429 <td class="content">For more information on debugging javascript refer to <a href="http://getfirebug.com/">Firebug</a>, the popular debugger for Firefox.</td>
430 </tr></table>
431 </div>
432 </div>
433 <h2 id="_the_logger">2. The Logger</h2>
434 <div class="sectionbody">
435 <div class="para"><p>It can also be useful to save information to log files at runtime. Rails maintains a separate log file for each runtime environment.</p></div>
436 <h3 id="_what_is_the_logger">2.1. What is The Logger?</h3>
437 <div class="para"><p>Rails makes use of Ruby's standard <tt>logger</tt> to write log information. You can also substitute another logger such as <tt>Log4R</tt> if you wish.</p></div>
438 <div class="para"><p>You can specify an alternative logger in your <tt>environment.rb</tt> or any environment file:</p></div>
439 <div class="listingblock">
440 <div class="content"><!-- Generator: GNU source-highlight 2.9
441 by Lorenzo Bettini
442 http://www.lorenzobettini.it
443 http://www.gnu.org/software/src-highlite -->
444 <pre><tt>ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>logger <span style="color: #990000">=</span> Logger<span style="color: #990000">.</span>new<span style="color: #990000">(</span>STDOUT<span style="color: #990000">)</span>
445 ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>logger <span style="color: #990000">=</span> Log4r<span style="color: #990000">::</span>Logger<span style="color: #990000">.</span>new<span style="color: #990000">(</span><span style="color: #FF0000">"Application Log"</span><span style="color: #990000">)</span>
446 </tt></pre></div></div>
447 <div class="para"><p>Or in the <tt>Initializer</tt> section, add <em>any</em> of the following</p></div>
448 <div class="listingblock">
449 <div class="content"><!-- Generator: GNU source-highlight 2.9
450 by Lorenzo Bettini
451 http://www.lorenzobettini.it
452 http://www.gnu.org/software/src-highlite -->
453 <pre><tt>config<span style="color: #990000">.</span>logger <span style="color: #990000">=</span> Logger<span style="color: #990000">.</span>new<span style="color: #990000">(</span>STDOUT<span style="color: #990000">)</span>
454 config<span style="color: #990000">.</span>logger <span style="color: #990000">=</span> Log4r<span style="color: #990000">::</span>Logger<span style="color: #990000">.</span>new<span style="color: #990000">(</span><span style="color: #FF0000">"Application Log"</span><span style="color: #990000">)</span>
455 </tt></pre></div></div>
456 <div class="admonitionblock">
457 <table><tr>
458 <td class="icon">
459 <img src="./images/icons/tip.png" alt="Tip" />
460 </td>
461 <td class="content">By default, each log is created under <tt>RAILS_ROOT/log/</tt> and the log file name is <tt>environment_name.log</tt>.</td>
462 </tr></table>
463 </div>
464 <h3 id="_log_levels">2.2. Log Levels</h3>
465 <div class="para"><p>When something is logged it's printed into the corresponding log if the log level of the message is equal or higher than the configured log level. If you want to know the current log level you can call the <tt>ActiveRecord::Base.logger.level</tt> method.</p></div>
466 <div class="para"><p>The available log levels are: <tt>:debug</tt>, <tt>:info</tt>, <tt>:warn</tt>, <tt>:error</tt>, and <tt>:fatal</tt>, corresponding to the log level numbers from 0 up to 4 respectively. To change the default log level, use</p></div>
467 <div class="listingblock">
468 <div class="content"><!-- Generator: GNU source-highlight 2.9
469 by Lorenzo Bettini
470 http://www.lorenzobettini.it
471 http://www.gnu.org/software/src-highlite -->
472 <pre><tt>config<span style="color: #990000">.</span>log_level <span style="color: #990000">=</span> Logger<span style="color: #990000">::</span>WARN <span style="font-style: italic"><span style="color: #9A1900"># In any environment initializer, or</span></span>
473 ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>logger<span style="color: #990000">.</span>level <span style="color: #990000">=</span> <span style="color: #993399">0</span> <span style="font-style: italic"><span style="color: #9A1900"># at any time</span></span>
474 </tt></pre></div></div>
475 <div class="para"><p>This is useful when you want to log under development or staging, but you don't want to flood your production log with unnecessary information.</p></div>
476 <div class="admonitionblock">
477 <table><tr>
478 <td class="icon">
479 <img src="./images/icons/tip.png" alt="Tip" />
480 </td>
481 <td class="content">The default Rails log level is <tt>info</tt> in production mode and <tt>debug</tt> in development and test mode.</td>
482 </tr></table>
483 </div>
484 <h3 id="_sending_messages">2.3. Sending Messages</h3>
485 <div class="para"><p>To write in the current log use the <tt>logger.(debug|info|warn|error|fatal)</tt> method from within a controller, model or mailer:</p></div>
486 <div class="listingblock">
487 <div class="content"><!-- Generator: GNU source-highlight 2.9
488 by Lorenzo Bettini
489 http://www.lorenzobettini.it
490 http://www.gnu.org/software/src-highlite -->
491 <pre><tt>logger<span style="color: #990000">.</span>debug <span style="color: #FF0000">"Person attributes hash: #{@person.attributes.inspect}"</span>
492 logger<span style="color: #990000">.</span>info <span style="color: #FF0000">"Processing the request..."</span>
493 logger<span style="color: #990000">.</span>fatal <span style="color: #FF0000">"Terminating application, raised unrecoverable error!!!"</span>
494 </tt></pre></div></div>
495 <div class="para"><p>Here's an example of a method instrumented with extra logging:</p></div>
496 <div class="listingblock">
497 <div class="content"><!-- Generator: GNU source-highlight 2.9
498 by Lorenzo Bettini
499 http://www.lorenzobettini.it
500 http://www.gnu.org/software/src-highlite -->
501 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsController <span style="color: #990000">&lt;</span> ApplicationController
502 <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
503
504 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
505 <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>new<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>post<span style="color: #990000">])</span>
506 logger<span style="color: #990000">.</span>debug <span style="color: #FF0000">"New post: #{@post.attributes.inspect}"</span>
507 logger<span style="color: #990000">.</span>debug <span style="color: #FF0000">"Post should be valid: #{@post.valid?}"</span>
508
509 <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>save
510 flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">'Post was successfully created.'</span>
511 logger<span style="color: #990000">.</span>debug <span style="color: #FF0000">"The post was saved and now is the user is going to be redirected..."</span>
512 redirect_to<span style="color: #990000">(</span><span style="color: #009900">@post</span><span style="color: #990000">)</span>
513 <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
514 render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"new"</span>
515 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
516 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
517
518 <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
519 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
520 </tt></pre></div></div>
521 <div class="para"><p>Here's an example of the log generated by this method:</p></div>
522 <div class="listingblock">
523 <div class="content">
524 <pre><tt>Processing PostsController#create (for 127.0.0.1 at 2008-09-08 11:52:54) [POST]
525 Session ID: BAh7BzoMY3NyZl9pZCIlMDY5MWU1M2I1ZDRjODBlMzkyMWI1OTg2NWQyNzViZjYiCmZsYXNoSUM6J0FjdGl
526 vbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA=--b18cd92fba90eacf8137e5f6b3b06c4d724596a4
527 Parameters: {"commit"=&gt;"Create", "post"=&gt;{"title"=&gt;"Debugging Rails",
528 "body"=&gt;"I'm learning how to print in logs!!!", "published"=&gt;"0"},
529 "authenticity_token"=&gt;"2059c1286e93402e389127b1153204e0d1e275dd", "action"=&gt;"create", "controller"=&gt;"posts"}
530 New post: {"updated_at"=&gt;nil, "title"=&gt;"Debugging Rails", "body"=&gt;"I'm learning how to print in logs!!!",
531 "published"=&gt;false, "created_at"=&gt;nil}
532 Post should be valid: true
533 Post Create (0.000443) INSERT INTO "posts" ("updated_at", "title", "body", "published",
534 "created_at") VALUES('2008-09-08 14:52:54', 'Debugging Rails',
535 'I''m learning how to print in logs!!!', 'f', '2008-09-08 14:52:54')
536 The post was saved and now is the user is going to be redirected...
537 Redirected to #&lt;Post:0x20af760&gt;
538 Completed in 0.01224 (81 reqs/sec) | DB: 0.00044 (3%) | 302 Found [http://localhost/posts]</tt></pre>
539 </div></div>
540 <div class="para"><p>Adding extra logging like this makes it easy to search for unexpected or unusual behavior in your logs. If you add extra logging, be sure to make sensible use of log levels, to avoid filling your production logs with useless trivia.</p></div>
541 </div>
542 <h2 id="_debugging_with_ruby_debug">3. Debugging with ruby-debug</h2>
543 <div class="sectionbody">
544 <div class="para"><p>When your code is behaving in unexpected ways, you can try printing to logs or the console to diagnose the problem. Unfortunately, there are times when this sort of error tracking is not effective in finding the root cause of a problem. When you actually need to journey into your running source code, the debugger is your best companion.</p></div>
545 <div class="para"><p>The debugger can also help you if you want to learn about the Rails source code but don't know where to start. Just debug any request to your application and use this guide to learn how to move from the code you have written deeper into Rails code.</p></div>
546 <h3 id="_setup">3.1. Setup</h3>
547 <div class="para"><p>The debugger used by Rails, <tt>ruby-debug</tt>, comes as a gem. To install it, just run:</p></div>
548 <div class="listingblock">
549 <div class="content"><!-- Generator: GNU source-highlight 2.9
550 by Lorenzo Bettini
551 http://www.lorenzobettini.it
552 http://www.gnu.org/software/src-highlite -->
553 <pre><tt>$ sudo gem install ruby-debug
554 </tt></pre></div></div>
555 <div class="para"><p>In case you want to download a particular version or get the source code, refer to the <a href="http://rubyforge.org/projects/ruby-debug/">project's page on rubyforge</a>.</p></div>
556 <div class="para"><p>Rails has had built-in support for ruby-debug since Rails 2.0. Inside any Rails application you can invoke the debugger by calling the <tt>debugger</tt> method.</p></div>
557 <div class="para"><p>Here's an example:</p></div>
558 <div class="listingblock">
559 <div class="content"><!-- Generator: GNU source-highlight 2.9
560 by Lorenzo Bettini
561 http://www.lorenzobettini.it
562 http://www.gnu.org/software/src-highlite -->
563 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PeopleController <span style="color: #990000">&lt;</span> ApplicationController
564 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> new
565 debugger
566 <span style="color: #009900">@person</span> <span style="color: #990000">=</span> Person<span style="color: #990000">.</span>new
567 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
568 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
569 </tt></pre></div></div>
570 <div class="para"><p>If you see the message in the console or logs:</p></div>
571 <div class="listingblock">
572 <div class="content">
573 <pre><tt>***** Debugger requested, but was not available: Start server with --debugger to enable *****</tt></pre>
574 </div></div>
575 <div class="para"><p>Make sure you have started your web server with the option <tt>&#8212;debugger</tt>:</p></div>
576 <div class="listingblock">
577 <div class="content"><!-- Generator: GNU source-highlight 2.9
578 by Lorenzo Bettini
579 http://www.lorenzobettini.it
580 http://www.gnu.org/software/src-highlite -->
581 <pre><tt><span style="color: #990000">~</span>/PathTo/rails_project$ script/server --debugger
582 <span style="color: #990000">=&gt;</span> Booting Mongrel <span style="color: #990000">(</span>use <span style="color: #FF0000">'script/server webrick'</span> to force WEBrick<span style="color: #990000">)</span>
583 <span style="color: #990000">=&gt;</span> Rails <span style="color: #993399">2.2</span><span style="color: #990000">.</span><span style="color: #993399">0</span> application starting on http<span style="color: #990000">://</span><span style="color: #993399">0.0</span><span style="color: #990000">.</span><span style="color: #993399">0.0</span><span style="color: #990000">:</span><span style="color: #993399">3000</span>
584 <span style="color: #990000">=&gt;</span> Debugger enabled
585 <span style="color: #990000">...</span>
586 </tt></pre></div></div>
587 <div class="admonitionblock">
588 <table><tr>
589 <td class="icon">
590 <img src="./images/icons/tip.png" alt="Tip" />
591 </td>
592 <td class="content">In development mode, you can dynamically <tt>require 'ruby-debug'</tt> instead of restarting the server, if it was started without <tt>&#8212;debugger</tt>.</td>
593 </tr></table>
594 </div>
595 <div class="para"><p>In order to use Rails debugging you'll need to be running either <strong>WEBrick</strong> or <strong>Mongrel</strong>. For the moment, no alternative servers are supported.</p></div>
596 <h3 id="_the_shell">3.2. The Shell</h3>
597 <div class="para"><p>As soon as your application calls the <tt>debugger</tt> method, the debugger will be started in a debugger shell inside the terminal window where you launched your application server, and you will be placed at ruby-debug's prompt <tt>(rdb:n)</tt>. The <em>n</em> is the thread number. The prompt will also show you the next line of code that is waiting to run.</p></div>
598 <div class="para"><p>If you got there by a browser request, the browser tab containing the request will be hung until the debugger has finished and the trace has finished processing the entire request.</p></div>
599 <div class="para"><p>For example:</p></div>
600 <div class="listingblock">
601 <div class="content">
602 <pre><tt>@posts = Post.find(:all)
603 (rdb:7)</tt></pre>
604 </div></div>
605 <div class="para"><p>Now it's time to explore and dig into your application. A good place to start is by asking the debugger for help&#8230; so type: <tt>help</tt> (You didn't see that coming, right?)</p></div>
606 <div class="listingblock">
607 <div class="content">
608 <pre><tt>(rdb:7) help
609 ruby-debug help v0.10.2
610 Type 'help &lt;command-name&gt;' for help on a specific command
611
612 Available commands:
613 backtrace delete enable help next quit show trace
614 break disable eval info p reload source undisplay
615 catch display exit irb pp restart step up
616 condition down finish list ps save thread var
617 continue edit frame method putl set tmate where</tt></pre>
618 </div></div>
619 <div class="admonitionblock">
620 <table><tr>
621 <td class="icon">
622 <img src="./images/icons/tip.png" alt="Tip" />
623 </td>
624 <td class="content">To view the help menu for any command use <tt>help &lt;command-name&gt;</tt> in active debug mode. For example: <em><tt>help var</tt></em></td>
625 </tr></table>
626 </div>
627 <div class="para"><p>The next command to learn is one of the most useful: <tt>list</tt>. You can also abbreviate ruby-debug commands by supplying just enough letters to distinguish them from other commands, so you can also use <tt>l</tt> for the <tt>list</tt> command.</p></div>
628 <div class="para"><p>This command shows you where you are in the code by printing 10 lines centered around the current line; the current line in this particular case is line 6 and is marked by <tt>&#8658;</tt>.</p></div>
629 <div class="listingblock">
630 <div class="content">
631 <pre><tt>(rdb:7) list
632 [1, 10] in /PathToProject/posts_controller.rb
633 1 class PostsController &lt; ApplicationController
634 2 # GET /posts
635 3 # GET /posts.xml
636 4 def index
637 5 debugger
638 =&gt; 6 @posts = Post.find(:all)
639 7
640 8 respond_to do |format|
641 9 format.html # index.html.erb
642 10 format.xml { render :xml =&gt; @posts }</tt></pre>
643 </div></div>
644 <div class="para"><p>If you repeat the <tt>list</tt> command, this time using just <tt>l</tt>, the next ten lines of the file will be printed out.</p></div>
645 <div class="listingblock">
646 <div class="content">
647 <pre><tt>(rdb:7) l
648 [11, 20] in /PathTo/project/app/controllers/posts_controller.rb
649 11 end
650 12 end
651 13
652 14 # GET /posts/1
653 15 # GET /posts/1.xml
654 16 def show
655 17 @post = Post.find(params[:id])
656 18
657 19 respond_to do |format|
658 20 format.html # show.html.erb</tt></pre>
659 </div></div>
660 <div class="para"><p>And so on until the end of the current file. When the end of file is reached, the <tt>list</tt> command will start again from the beginning of the file and continue again up to the end, treating the file as a circular buffer.</p></div>
661 <h3 id="_the_context">3.3. The Context</h3>
662 <div class="para"><p>When you start debugging your application, you will be placed in different contexts as you go through the different parts of the stack.</p></div>
663 <div class="para"><p>ruby-debug creates a content when a stopping point or an event is reached. The context has information about the suspended program which enables a debugger to inspect the frame stack, evaluate variables from the perspective of the debugged program, and contains information about the place where the debugged program is stopped.</p></div>
664 <div class="para"><p>At any time you can call the <tt>backtrace</tt> command (or its alias <tt>where</tt>) to print the backtrace of the application. This can be very helpful to know how you got where you are. If you ever wondered about how you got somewhere in your code, then <tt>backtrace</tt> will supply the answer.</p></div>
665 <div class="listingblock">
666 <div class="content">
667 <pre><tt>(rdb:5) where
668 #0 PostsController.index
669 at line /PathTo/project/app/controllers/posts_controller.rb:6
670 #1 Kernel.send
671 at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175
672 #2 ActionController::Base.perform_action_without_filters
673 at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175
674 #3 ActionController::Filters::InstanceMethods.call_filters(chain#ActionController::Fil...,...)
675 at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/filters.rb:617
676 ...</tt></pre>
677 </div></div>
678 <div class="para"><p>You move anywhere you want in this trace (thus changing the context) by using the <tt>frame <em>n</em></tt> command, where <em>n</em> is the specified frame number.</p></div>
679 <div class="listingblock">
680 <div class="content">
681 <pre><tt>(rdb:5) frame 2
682 #2 ActionController::Base.perform_action_without_filters
683 at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175</tt></pre>
684 </div></div>
685 <div class="para"><p>The available variables are the same as if you were running the code line by line. After all, that's what debugging is.</p></div>
686 <div class="para"><p>Moving up and down the stack frame: You can use <tt>up [n]</tt> (<tt>u</tt> for abbreviated) and <tt>down [n]</tt> commands in order to change the context <em>n</em> frames up or down the stack respectively. <em>n</em> defaults to one. Up in this case is towards higher-numbered stack frames, and down is towards lower-numbered stack frames.</p></div>
687 <h3 id="_threads">3.4. Threads</h3>
688 <div class="para"><p>The debugger can list, stop, resume and switch between running threads by using the command <tt>thread</tt> (or the abbreviated <tt>th</tt>). This command has a handful of options:</p></div>
689 <div class="ilist"><ul>
690 <li>
691 <p>
692 <tt>thread</tt> shows the current thread.
693 </p>
694 </li>
695 <li>
696 <p>
697 <tt>thread list</tt> is used to list all threads and their statuses. The plus + character and the number indicates the current thread of execution.
698 </p>
699 </li>
700 <li>
701 <p>
702 <tt>thread stop <em>n</em></tt> stop thread <em>n</em>.
703 </p>
704 </li>
705 <li>
706 <p>
707 <tt>thread resume <em>n</em></tt> resumes thread <em>n</em>.
708 </p>
709 </li>
710 <li>
711 <p>
712 <tt>thread switch <em>n</em></tt> switches the current thread context to <em>n</em>.
713 </p>
714 </li>
715 </ul></div>
716 <div class="para"><p>This command is very helpful, among other occasions, when you are debugging concurrent threads and need to verify that there are no race conditions in your code.</p></div>
717 <h3 id="_inspecting_variables">3.5. Inspecting Variables</h3>
718 <div class="para"><p>Any expression can be evaluated in the current context. To evaluate an expression, just type it!</p></div>
719 <div class="para"><p>This example shows how you can print the instance_variables defined within the current context:</p></div>
720 <div class="listingblock">
721 <div class="content">
722 <pre><tt>@posts = Post.find(:all)
723 (rdb:11) instance_variables
724 ["@_response", "@action_name", "@url", "@_session", "@_cookies", "@performed_render", "@_flash", "@template", "@_params", "@before_filter_chain_aborted", "@request_origin", "@_headers", "@performed_redirect", "@_request"]</tt></pre>
725 </div></div>
726 <div class="para"><p>As you may have figured out, all of the variables that you can access from a controller are displayed. This list is dynamically updated as you execute code. For example, run the next line using <tt>next</tt> (you'll learn more about this command later in this guide).</p></div>
727 <div class="listingblock">
728 <div class="content">
729 <pre><tt>(rdb:11) next
730 Processing PostsController#index (for 127.0.0.1 at 2008-09-04 19:51:34) [GET]
731 Session ID: BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA==--b16e91b992453a8cc201694d660147bba8b0fd0e
732 Parameters: {"action"=&gt;"index", "controller"=&gt;"posts"}
733 /PathToProject/posts_controller.rb:8
734 respond_to do |format|</tt></pre>
735 </div></div>
736 <div class="para"><p>And then ask again for the instance_variables:</p></div>
737 <div class="listingblock">
738 <div class="content">
739 <pre><tt>(rdb:11) instance_variables.include? "@posts"
740 true</tt></pre>
741 </div></div>
742 <div class="para"><p>Now <tt>@posts</tt> is a included in the instance variables, because the line defining it was executed.</p></div>
743 <div class="admonitionblock">
744 <table><tr>
745 <td class="icon">
746 <img src="./images/icons/tip.png" alt="Tip" />
747 </td>
748 <td class="content">You can also step into <strong>irb</strong> mode with the command <tt>irb</tt> (of course!). This way an irb session will be started within the context you invoked it. But be warned: this is an experimental feature.</td>
749 </tr></table>
750 </div>
751 <div class="para"><p>The <tt>var</tt> method is the most convenient way to show variables and their values:</p></div>
752 <div class="listingblock">
753 <div class="content">
754 <pre><tt>var
755 (rdb:1) v[ar] const &lt;object&gt; show constants of object
756 (rdb:1) v[ar] g[lobal] show global variables
757 (rdb:1) v[ar] i[nstance] &lt;object&gt; show instance variables of object
758 (rdb:1) v[ar] l[ocal] show local variables</tt></pre>
759 </div></div>
760 <div class="para"><p>This is a great way to inspect the values of the current context variables. For example:</p></div>
761 <div class="listingblock">
762 <div class="content">
763 <pre><tt>(rdb:9) var local
764 __dbg_verbose_save =&gt; false</tt></pre>
765 </div></div>
766 <div class="para"><p>You can also inspect for an object method this way:</p></div>
767 <div class="listingblock">
768 <div class="content">
769 <pre><tt>(rdb:9) var instance Post.new
770 @attributes = {"updated_at"=&gt;nil, "body"=&gt;nil, "title"=&gt;nil, "published"=&gt;nil, "created_at"...
771 @attributes_cache = {}
772 @new_record = true</tt></pre>
773 </div></div>
774 <div class="admonitionblock">
775 <table><tr>
776 <td class="icon">
777 <img src="./images/icons/tip.png" alt="Tip" />
778 </td>
779 <td class="content">The commands <tt>p</tt> (print) and <tt>pp</tt> (pretty print) can be used to evaluate Ruby expressions and display the value of variables to the console.</td>
780 </tr></table>
781 </div>
782 <div class="para"><p>You can use also <tt>display</tt> to start watching variables. This is a good way of tracking the values of a variable while the execution goes on.</p></div>
783 <div class="listingblock">
784 <div class="content">
785 <pre><tt>(rdb:1) display @recent_comments
786 1: @recent_comments =</tt></pre>
787 </div></div>
788 <div class="para"><p>The variables inside the displaying list will be printed with their values after you move in the stack. To stop displaying a variable use <tt>undisplay <em>n</em></tt> where <em>n</em> is the variable number (1 in the last example).</p></div>
789 <h3 id="_step_by_step">3.6. Step by Step</h3>
790 <div class="para"><p>Now you should know where you are in the running trace and be able to print the available variables. But lets continue and move on with the application execution.</p></div>
791 <div class="para"><p>Use <tt>step</tt> (abbreviated <tt>s</tt>) to continue running your program until the next logical stopping point and return control to ruby-debug.</p></div>
792 <div class="admonitionblock">
793 <table><tr>
794 <td class="icon">
795 <img src="./images/icons/tip.png" alt="Tip" />
796 </td>
797 <td class="content">You can also use <tt>step+ <em>n</em></tt> and <tt>step- <em>n</em></tt> to move forward or backward <em>n</em> steps respectively.</td>
798 </tr></table>
799 </div>
800 <div class="para"><p>You may also use <tt>next</tt> which is similar to step, but function or method calls that appear within the line of code are executed without stopping. As with step, you may use plus sign to move <em>n</em> steps.</p></div>
801 <div class="para"><p>The difference between <tt>next</tt> and <tt>step</tt> is that <tt>step</tt> stops at the next line of code executed, doing just a single step, while <tt>next</tt> moves to the next line without descending inside methods.</p></div>
802 <div class="para"><p>For example, consider this block of code with an included <tt>debugger</tt> statement:</p></div>
803 <div class="listingblock">
804 <div class="content"><!-- Generator: GNU source-highlight 2.9
805 by Lorenzo Bettini
806 http://www.lorenzobettini.it
807 http://www.gnu.org/software/src-highlite -->
808 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Author <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
809 has_one <span style="color: #990000">:</span>editorial
810 has_many <span style="color: #990000">:</span>comments
811
812 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> find_recent_comments<span style="color: #990000">(</span>limit <span style="color: #990000">=</span> <span style="color: #993399">10</span><span style="color: #990000">)</span>
813 debugger
814 <span style="color: #009900">@recent_comments</span> <span style="color: #990000">||=</span> comments<span style="color: #990000">.</span>find<span style="color: #990000">(</span>
815 <span style="color: #990000">:</span>all<span style="color: #990000">,</span>
816 <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">.</span>week<span style="color: #990000">.</span>ago<span style="color: #990000">],</span>
817 <span style="color: #990000">:</span>limit <span style="color: #990000">=&gt;</span> limit
818 <span style="color: #990000">)</span>
819 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
820 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
821 </tt></pre></div></div>
822 <div class="admonitionblock">
823 <table><tr>
824 <td class="icon">
825 <img src="./images/icons/tip.png" alt="Tip" />
826 </td>
827 <td class="content">You can use ruby-debug while using script/console. Just remember to <tt>require "ruby-debug"</tt> before calling the <tt>debugger</tt> method.</td>
828 </tr></table>
829 </div>
830 <div class="listingblock">
831 <div class="content">
832 <pre><tt>/PathTo/project $ script/console
833 Loading development environment (Rails 2.1.0)
834 &gt;&gt; require "ruby-debug"
835 =&gt; []
836 &gt;&gt; author = Author.first
837 =&gt; #&lt;Author id: 1, first_name: "Bob", last_name: "Smith", created_at: "2008-07-31 12:46:10", updated_at: "2008-07-31 12:46:10"&gt;
838 &gt;&gt; author.find_recent_comments
839 /PathTo/project/app/models/author.rb:11
840 )</tt></pre>
841 </div></div>
842 <div class="para"><p>With the code stopped, take a look around:</p></div>
843 <div class="listingblock">
844 <div class="content">
845 <pre><tt>(rdb:1) list
846 [6, 15] in /PathTo/project/app/models/author.rb
847 6 debugger
848 7 @recent_comments ||= comments.find(
849 8 :all,
850 9 :conditions =&gt; ["created_at &gt; ?", 1.week.ago],
851 10 :limit =&gt; limit
852 =&gt; 11 )
853 12 end
854 13 end</tt></pre>
855 </div></div>
856 <div class="para"><p>You are at the end of the line, but&#8230; was this line executed? You can inspect the instance variables.</p></div>
857 <div class="listingblock">
858 <div class="content">
859 <pre><tt>(rdb:1) var instance
860 @attributes = {"updated_at"=&gt;"2008-07-31 12:46:10", "id"=&gt;"1", "first_name"=&gt;"Bob", "las...
861 @attributes_cache = {}</tt></pre>
862 </div></div>
863 <div class="para"><p><tt>@recent_comments</tt> hasn't been defined yet, so it's clear that this line hasn't been executed yet. Use the <tt>next</tt> command to move on in the code:</p></div>
864 <div class="listingblock">
865 <div class="content">
866 <pre><tt>(rdb:1) next
867 /PathTo/project/app/models/author.rb:12
868 @recent_comments
869 (rdb:1) var instance
870 @attributes = {"updated_at"=&gt;"2008-07-31 12:46:10", "id"=&gt;"1", "first_name"=&gt;"Bob", "las...
871 @attributes_cache = {}
872 @comments = []
873 @recent_comments = []</tt></pre>
874 </div></div>
875 <div class="para"><p>Now you can see that the <tt>@comments</tt> relationship was loaded and @recent_comments defined because the line was executed.</p></div>
876 <div class="para"><p>If you want to go deeper into the stack trace you can move single <tt>steps</tt>, through your calling methods and into Rails code. This is one of the best ways to find bugs in your code, or perhaps in Ruby or Rails.</p></div>
877 <h3 id="_breakpoints">3.7. Breakpoints</h3>
878 <div class="para"><p>A breakpoint makes your application stop whenever a certain point in the program is reached. The debugger shell is invoked in that line.</p></div>
879 <div class="para"><p>You can add breakpoints dynamically with the command <tt>break</tt> (or just <tt>b</tt>). There are 3 possible ways of adding breakpoints manually:</p></div>
880 <div class="ilist"><ul>
881 <li>
882 <p>
883 <tt>break line</tt>: set breakpoint in the <em>line</em> in the current source file.
884 </p>
885 </li>
886 <li>
887 <p>
888 <tt>break file:line [if expression]</tt>: set breakpoint in the <em>line</em> number inside the <em>file</em>. If an <em>expression</em> is given it must evaluated to <em>true</em> to fire up the debugger.
889 </p>
890 </li>
891 <li>
892 <p>
893 <tt>break class(.|#)method [if expression]</tt>: set breakpoint in <em>method</em> (. and # for class and instance method respectively) defined in <em>class</em>. The <em>expression</em> works the same way as with file:line.
894 </p>
895 </li>
896 </ul></div>
897 <div class="listingblock">
898 <div class="content">
899 <pre><tt>(rdb:5) break 10
900 Breakpoint 1 file /PathTo/project/vendor/rails/actionpack/lib/action_controller/filters.rb, line 10</tt></pre>
901 </div></div>
902 <div class="para"><p>Use <tt>info breakpoints <em>n</em></tt> or <tt>info break <em>n</em></tt> to list breakpoints. If you supply a number, it lists that breakpoint. Otherwise it lists all breakpoints.</p></div>
903 <div class="listingblock">
904 <div class="content">
905 <pre><tt>(rdb:5) info breakpoints
906 Num Enb What
907 1 y at filters.rb:10</tt></pre>
908 </div></div>
909 <div class="para"><p>To delete breakpoints: use the command <tt>delete <em>n</em></tt> to remove the breakpoint number <em>n</em>. If no number is specified, it deletes all breakpoints that are currently active..</p></div>
910 <div class="listingblock">
911 <div class="content">
912 <pre><tt>(rdb:5) delete 1
913 (rdb:5) info breakpoints
914 No breakpoints.</tt></pre>
915 </div></div>
916 <div class="para"><p>You can also enable or disable breakpoints:</p></div>
917 <div class="ilist"><ul>
918 <li>
919 <p>
920 <tt>enable breakpoints</tt>: allow a list <em>breakpoints</em> or all of them if no list is specified, to stop your program. This is the default state when you create a breakpoint.
921 </p>
922 </li>
923 <li>
924 <p>
925 <tt>disable breakpoints</tt>: the <em>breakpoints</em> will have no effect on your program.
926 </p>
927 </li>
928 </ul></div>
929 <h3 id="_catching_exceptions">3.8. Catching Exceptions</h3>
930 <div class="para"><p>The command <tt>catch exception-name</tt> (or just <tt>cat exception-name</tt>) can be used to intercept an exception of type <em>exception-name</em> when there would otherwise be is no handler for it.</p></div>
931 <div class="para"><p>To list all active catchpoints use <tt>catch</tt>.</p></div>
932 <h3 id="_resuming_execution">3.9. Resuming Execution</h3>
933 <div class="para"><p>There are two ways to resume execution of an application that is stopped in the debugger:</p></div>
934 <div class="ilist"><ul>
935 <li>
936 <p>
937 <tt>continue</tt> [line-specification] (or <tt>c</tt>): resume program execution, at the address where your script last stopped; any breakpoints set at that address are bypassed. The optional argument line-specification allows you to specify a line number to set a one-time breakpoint which is deleted when that breakpoint is reached.
938 </p>
939 </li>
940 <li>
941 <p>
942 <tt>finish</tt> [frame-number] (or <tt>fin</tt>): execute until the selected stack frame returns. If no frame number is given, the application will run until the currently selected frame returns. The currently selected frame starts out the most-recent frame or 0 if no frame positioning (e.g up, down or frame) has been performed. If a frame number is given it will run until the specified frame returns.
943 </p>
944 </li>
945 </ul></div>
946 <h3 id="_editing">3.10. Editing</h3>
947 <div class="para"><p>Two commands allow you to open code from the debugger into an editor:</p></div>
948 <div class="ilist"><ul>
949 <li>
950 <p>
951 <tt>edit [file:line]</tt>: edit <em>file</em> using the editor specified by the EDITOR environment variable. A specific <em>line</em> can also be given.
952 </p>
953 </li>
954 <li>
955 <p>
956 <tt>tmate <em>n</em></tt> (abbreviated <tt>tm</tt>): open the current file in TextMate. It uses n-th frame if <em>n</em> is specified.
957 </p>
958 </li>
959 </ul></div>
960 <h3 id="_quitting">3.11. Quitting</h3>
961 <div class="para"><p>To exit the debugger, use the <tt>quit</tt> command (abbreviated <tt>q</tt>), or its alias <tt>exit</tt>.</p></div>
962 <div class="para"><p>A simple quit tries to terminate all threads in effect. Therefore your server will be stopped and you will have to start it again.</p></div>
963 <h3 id="_settings">3.12. Settings</h3>
964 <div class="para"><p>There are some settings that can be configured in ruby-debug to make it easier to debug your code. Here are a few of the available options:</p></div>
965 <div class="ilist"><ul>
966 <li>
967 <p>
968 <tt>set reload</tt>: Reload source code when changed.
969 </p>
970 </li>
971 <li>
972 <p>
973 <tt>set autolist</tt>: Execute <tt>list</tt> command on every breakpoint.
974 </p>
975 </li>
976 <li>
977 <p>
978 <tt>set listsize <em>n</em></tt>: Set number of source lines to list by default to <em>n</em>.
979 </p>
980 </li>
981 <li>
982 <p>
983 <tt>set forcestep</tt>: Make sure the <tt>next</tt> and <tt>step</tt> commands always move to a new line
984 </p>
985 </li>
986 </ul></div>
987 <div class="para"><p>You can see the full list by using <tt>help set</tt>. Use <tt>help set <em>subcommand</em></tt> to learn about a particular <tt>set</tt> command.</p></div>
988 <div class="admonitionblock">
989 <table><tr>
990 <td class="icon">
991 <img src="./images/icons/tip.png" alt="Tip" />
992 </td>
993 <td class="content">You can include any number of these configuration lines inside a <tt>.rdebugrc</tt> file in your HOME directory. ruby-debug will read this file every time it is loaded. and configure itself accordingly.</td>
994 </tr></table>
995 </div>
996 <div class="para"><p>Here's a good start for an <tt>.rdebugrc</tt>:</p></div>
997 <div class="listingblock">
998 <div class="content">
999 <pre><tt>set autolist
1000 set forcestep
1001 set listsize 25</tt></pre>
1002 </div></div>
1003 </div>
1004 <h2 id="_debugging_memory_leaks">4. Debugging Memory Leaks</h2>
1005 <div class="sectionbody">
1006 <div class="para"><p>A Ruby application (on Rails or not), can leak memory - either in the Ruby code or at the C code level.</p></div>
1007 <div class="para"><p>In this section, you will learn how to find and fix such leaks by using Bleak House and Valgrind debugging tools.</p></div>
1008 <h3 id="_bleakhouse">4.1. BleakHouse</h3>
1009 <div class="para"><p><a href="http://github.com/fauna/bleak_house/tree/master">BleakHouse</a> is a library for finding memory leaks.</p></div>
1010 <div class="para"><p>If a Ruby object does not go out of scope, the Ruby Garbage Collector won't sweep it since it is referenced somewhere. Leaks like this can grow slowly and your application will consume more and more memory, gradually affecting the overall system performance. This tool will help you find leaks on the Ruby heap.</p></div>
1011 <div class="para"><p>To install it run:</p></div>
1012 <div class="listingblock">
1013 <div class="content">
1014 <pre><tt>sudo gem install bleak_house</tt></pre>
1015 </div></div>
1016 <div class="para"><p>Then setup you application for profiling. Then add the following at the bottom of config/environment.rb:</p></div>
1017 <div class="listingblock">
1018 <div class="content"><!-- Generator: GNU source-highlight 2.9
1019 by Lorenzo Bettini
1020 http://www.lorenzobettini.it
1021 http://www.gnu.org/software/src-highlite -->
1022 <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'bleak_house'</span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> ENV<span style="color: #990000">[</span><span style="color: #FF0000">'BLEAK_HOUSE'</span><span style="color: #990000">]</span>
1023 </tt></pre></div></div>
1024 <div class="para"><p>Start a server instance with BleakHouse integration:</p></div>
1025 <div class="listingblock">
1026 <div class="content">
1027 <pre><tt>RAILS_ENV=production BLEAK_HOUSE=1 ruby-bleak-house ./script/server</tt></pre>
1028 </div></div>
1029 <div class="para"><p>Make sure to run a couple hundred requests to get better data samples, then press <tt>CTRL-C</tt>. The server will stop and Bleak House will produce a dumpfile in <tt>/tmp</tt>:</p></div>
1030 <div class="listingblock">
1031 <div class="content">
1032 <pre><tt>** BleakHouse: working...
1033 ** BleakHouse: complete
1034 ** Bleakhouse: run 'bleak /tmp/bleak.5979.0.dump' to analyze.</tt></pre>
1035 </div></div>
1036 <div class="para"><p>To analyze it, just run the listed command. The top 20 leakiest lines will be listed:</p></div>
1037 <div class="listingblock">
1038 <div class="content">
1039 <pre><tt> 191691 total objects
1040 Final heap size 191691 filled, 220961 free
1041 Displaying top 20 most common line/class pairs
1042 89513 __null__:__null__:__node__
1043 41438 __null__:__null__:String
1044 2348 /opt/local//lib/ruby/site_ruby/1.8/rubygems/specification.rb:557:Array
1045 1508 /opt/local//lib/ruby/gems/1.8/specifications/gettext-1.90.0.gemspec:14:String
1046 1021 /opt/local//lib/ruby/gems/1.8/specifications/heel-0.2.0.gemspec:14:String
1047 951 /opt/local//lib/ruby/site_ruby/1.8/rubygems/version.rb:111:String
1048 935 /opt/local//lib/ruby/site_ruby/1.8/rubygems/specification.rb:557:String
1049 834 /opt/local//lib/ruby/site_ruby/1.8/rubygems/version.rb:146:Array
1050 ...</tt></pre>
1051 </div></div>
1052 <div class="para"><p>This way you can find where your application is leaking memory and fix it.</p></div>
1053 <div class="para"><p>If <a href="http://github.com/fauna/bleak_house/tree/master">BleakHouse</a> doesn't report any heap growth but you still have memory growth, you might have a broken C extension, or real leak in the interpreter. In that case, try using Valgrind to investigate further.</p></div>
1054 <h3 id="_valgrind">4.2. Valgrind</h3>
1055 <div class="para"><p><a href="http://valgrind.org/">Valgrind</a> is a Linux-only application for detecting C-based memory leaks and race conditions.</p></div>
1056 <div class="para"><p>There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. For example, a C extension in the interpreter calls <tt>malloc()</tt> but is doesn't properly call <tt>free()</tt>, this memory won't be available until the app terminates.</p></div>
1057 <div class="para"><p>For further information on how to install Valgrind and use with Ruby, refer to <a href="http://blog.evanweaver.com/articles/2008/02/05/valgrind-and-ruby/">Valgrind and Ruby</a> by Evan Weaver.</p></div>
1058 </div>
1059 <h2 id="_plugins_for_debugging">5. Plugins for Debugging</h2>
1060 <div class="sectionbody">
1061 <div class="para"><p>There are some Rails plugins to help you to find errors and debug your application. Here is a list of useful plugins for debugging:</p></div>
1062 <div class="ilist"><ul>
1063 <li>
1064 <p>
1065 <a href="http://github.com/drnic/rails-footnotes/tree/master">Footnotes</a>: Every Rails page has footnotes that give request information and link back to your source via TextMate.
1066 </p>
1067 </li>
1068 <li>
1069 <p>
1070 <a href="http://github.com/ntalbott/query_trace/tree/master">Query Trace</a>: Adds query origin tracing to your logs.
1071 </p>
1072 </li>
1073 <li>
1074 <p>
1075 <a href="http://github.com/dan-manges/query_stats/tree/master">Query Stats</a>: A Rails plugin to track database queries.
1076 </p>
1077 </li>
1078 <li>
1079 <p>
1080 <a href="http://code.google.com/p/query-reviewer/">Query Reviewer</a>: This rails plugin not only runs "EXPLAIN" before each of your select queries in development, but provides a small DIV in the rendered output of each page with the summary of warnings for each query that it analyzed.
1081 </p>
1082 </li>
1083 <li>
1084 <p>
1085 <a href="http://github.com/rails/exception_notification/tree/master">Exception Notifier</a>: Provides a mailer object and a default set of templates for sending email notifications when errors occur in a Rails application.
1086 </p>
1087 </li>
1088 <li>
1089 <p>
1090 <a href="http://github.com/defunkt/exception_logger/tree/master">Exception Logger</a>: Logs your Rails exceptions in the database and provides a funky web interface to manage them.
1091 </p>
1092 </li>
1093 </ul></div>
1094 </div>
1095 <h2 id="_references">6. References</h2>
1096 <div class="sectionbody">
1097 <div class="ilist"><ul>
1098 <li>
1099 <p>
1100 <a href="http://www.datanoise.com/ruby-debug">ruby-debug Homepage</a>
1101 </p>
1102 </li>
1103 <li>
1104 <p>
1105 <a href="http://www.sitepoint.com/article/debug-rails-app-ruby-debug/">Article: Debugging a Rails application with ruby-debug</a>
1106 </p>
1107 </li>
1108 <li>
1109 <p>
1110 <a href="http://brian.maybeyoureinsane.net/blog/2007/05/07/ruby-debug-basics-screencast/">ruby-debug Basics screencast</a>
1111 </p>
1112 </li>
1113 <li>
1114 <p>
1115 <a href="http://railscasts.com/episodes/54-debugging-with-ruby-debug">Ryan Bate's ruby-debug screencast</a>
1116 </p>
1117 </li>
1118 <li>
1119 <p>
1120 <a href="http://railscasts.com/episodes/24-the-stack-trace">Ryan Bate's stack trace screencast</a>
1121 </p>
1122 </li>
1123 <li>
1124 <p>
1125 <a href="http://railscasts.com/episodes/56-the-logger">Ryan Bate's logger screencast</a>
1126 </p>
1127 </li>
1128 <li>
1129 <p>
1130 <a href="http://bashdb.sourceforge.net/ruby-debug.html">Debugging with ruby-debug</a>
1131 </p>
1132 </li>
1133 <li>
1134 <p>
1135 <a href="http://cheat.errtheblog.com/s/rdebug/">ruby-debug cheat sheet</a>
1136 </p>
1137 </li>
1138 <li>
1139 <p>
1140 <a href="http://wiki.rubyonrails.org/rails/pages/HowtoConfigureLogging">Ruby on Rails Wiki: How to Configure Logging</a>
1141 </p>
1142 </li>
1143 <li>
1144 <p>
1145 <a href="http://blog.evanweaver.com/files/doc/fauna/bleak_house/files/README.html">Bleak House Documentation</a>
1146 </p>
1147 </li>
1148 </ul></div>
1149 </div>
1150 <h2 id="_changelog">7. Changelog</h2>
1151 <div class="sectionbody">
1152 <div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/5">Lighthouse ticket</a></p></div>
1153 <div class="ilist"><ul>
1154 <li>
1155 <p>
1156 November 3, 2008: Accepted for publication. Added RJS, memory leaks and plugins chapters by <a href="../authors.html#miloops">Emilio Tagua</a>
1157 </p>
1158 </li>
1159 <li>
1160 <p>
1161 October 19, 2008: Copy editing pass by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
1162 </p>
1163 </li>
1164 <li>
1165 <p>
1166 September 16, 2008: initial version by <a href="../authors.html#miloops">Emilio Tagua</a>
1167 </p>
1168 </li>
1169 </ul></div>
1170 </div>
1171
1172 </div>
1173 </div>
1174 </body>
1175 </html>