Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / html / debugging_rails_applications.html
diff --git a/vendor/rails/railties/doc/guides/html/debugging_rails_applications.html b/vendor/rails/railties/doc/guides/html/debugging_rails_applications.html
new file mode 100644 (file)
index 0000000..0653caa
--- /dev/null
@@ -0,0 +1,1175 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
+<head>
+       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+       <title>Debugging Rails Applications</title>
+       <!--[if lt IE 8]>
+       <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+       <![endif]-->
+       <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+       <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+       <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
+       <style type="text/css">
+               div#container {
+       max-width: 900px;
+       padding-bottom: 3em;
+}
+
+div#content {
+       margin-left: 200px;
+}
+
+div#container.notoc {
+       max-width: 600px;
+}
+
+.notoc div#content {
+       margin-left: 0;
+}
+
+pre {
+       line-height: 1.4em;
+}
+
+#content p tt {
+       background: #eeeeee;
+       border: solid 1px #cccccc;
+       padding: 3px;
+}
+
+dt {
+       font-weight: bold;
+}
+
+#content dt tt {
+       font-size: 10pt;
+}
+
+dd {
+       margin-left: 3em;
+}
+
+#content dt tt, #content pre tt {
+       background: none;
+       padding: 0;
+       border: 0;
+}
+
+#content .olist ol {
+       margin-left: 2em;
+}
+
+#header {
+       position: relative;
+       max-width: 840px;
+       margin-left: auto;
+       margin-right: auto;
+}
+
+#header.notoc {
+       max-width: 580px;
+}
+
+#logo {
+       position: absolute;
+       left: 10px;
+       top: 10px;
+       width: 110px;
+       height: 140px;
+}
+
+div#header h1#site_title {
+       background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
+       position: absolute;
+       width: 392px;
+       height: 55px;
+       left: 145px;
+       top: 20px;
+       margin: 0;
+       padding: 0;
+}
+
+#site_title span {
+       display: none;
+}
+
+#site_title_tagline {
+       display: none;
+}
+
+ul#navMain {
+       position: absolute;
+       margin: 0;
+       padding: 0;
+       top: 97px;
+       left: 145px;
+}
+
+.left-floaty, .right-floaty {
+       padding: 15px;
+}
+
+.admonitionblock,
+.tableblock {
+       margin-left: 1em;
+       margin-right: 1em;
+       margin-top: 0.25em;
+       margin-bottom: 1em;
+}
+
+.admonitionblock .icon {
+       padding-right: 8px;
+}
+
+.admonitionblock .content {
+       border: solid 1px #ffda78;
+       background: #fffebd;
+       padding: 10px;
+       padding-top: 8px;
+       padding-bottom: 8px;
+}
+
+.admonitionblock .title {
+       font-size: 140%;
+       margin-bottom: 0.5em;
+}
+
+.tableblock table {
+       border: solid 1px #aaaaff;
+       background: #f0f0ff;
+}
+
+.tableblock th {
+       background: #e0e0e0;
+}
+
+.tableblock th,
+.tableblock td {
+       padding: 3px;
+       padding-left: 5px;
+       padding-right: 5px;
+}
+
+.sidebarblock {
+       margin-top: 0.25em;
+       margin: 1em;
+       border: solid 1px #ccccbb;
+       padding: 8px;
+       background: #ffffe0;
+}
+
+.sidebarblock .sidebar-title {
+       font-size: 140%;
+       font-weight: 600;
+       margin-bottom: 0.3em;
+}
+
+.sidebarblock .sidebar-content > .para:last-child > p {
+       margin-bottom: 0;
+}
+
+.sidebarblock .sidebar-title a {
+       text-decoration: none;
+}
+
+.sidebarblock .sidebar-title a:hover {
+       text-decoration: underline;
+}
+
+       </style>
+</head>
+<body>
+       <div id="header" >
+               <div id="logo">
+                       <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+               </div>
+               
+               <h1 id="site_title"><span>Ruby on Rails</span></h1>
+               <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+
+               <ul id="navMain">
+                       <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+                       <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+               </ul>
+       </div>
+
+       <div id="container">
+               
+               <div id="sidebar">
+                       <h2>Chapters</h2>
+                       <ol>
+                                       <li>
+                                       <a href="#_view_helpers_for_debugging">View Helpers for Debugging</a>
+                                               <ul>
+                                               
+                                                       <li><a href="#_debug">debug</a></li>
+                                               
+                                                       <li><a href="#_to_yaml">to_yaml</a></li>
+                                               
+                                                       <li><a href="#_inspect">inspect</a></li>
+                                               
+                                                       <li><a href="#_debugging_javascript">Debugging Javascript</a></li>
+                                               
+                                               </ul>
+                                       </li>
+                                       <li>
+                                       <a href="#_the_logger">The Logger</a>
+                                               <ul>
+                                               
+                                                       <li><a href="#_what_is_the_logger">What is The Logger?</a></li>
+                                               
+                                                       <li><a href="#_log_levels">Log Levels</a></li>
+                                               
+                                                       <li><a href="#_sending_messages">Sending Messages</a></li>
+                                               
+                                               </ul>
+                                       </li>
+                                       <li>
+                                       <a href="#_debugging_with_ruby_debug">Debugging with ruby-debug</a>
+                                               <ul>
+                                               
+                                                       <li><a href="#_setup">Setup</a></li>
+                                               
+                                                       <li><a href="#_the_shell">The Shell</a></li>
+                                               
+                                                       <li><a href="#_the_context">The Context</a></li>
+                                               
+                                                       <li><a href="#_threads">Threads</a></li>
+                                               
+                                                       <li><a href="#_inspecting_variables">Inspecting Variables</a></li>
+                                               
+                                                       <li><a href="#_step_by_step">Step by Step</a></li>
+                                               
+                                                       <li><a href="#_breakpoints">Breakpoints</a></li>
+                                               
+                                                       <li><a href="#_catching_exceptions">Catching Exceptions</a></li>
+                                               
+                                                       <li><a href="#_resuming_execution">Resuming Execution</a></li>
+                                               
+                                                       <li><a href="#_editing">Editing</a></li>
+                                               
+                                                       <li><a href="#_quitting">Quitting</a></li>
+                                               
+                                                       <li><a href="#_settings">Settings</a></li>
+                                               
+                                               </ul>
+                                       </li>
+                                       <li>
+                                       <a href="#_debugging_memory_leaks">Debugging Memory Leaks</a>
+                                               <ul>
+                                               
+                                                       <li><a href="#_bleakhouse">BleakHouse</a></li>
+                                               
+                                                       <li><a href="#_valgrind">Valgrind</a></li>
+                                               
+                                               </ul>
+                                       </li>
+                                       <li>
+                                       <a href="#_plugins_for_debugging">Plugins for Debugging</a>
+                                       </li>
+                                       <li>
+                                       <a href="#_references">References</a>
+                                       </li>
+                                       <li>
+                                       <a href="#_changelog">Changelog</a>
+                                       </li>
+                       </ol>
+               </div>
+               
+               <div id="content">
+                               <h1>Debugging Rails Applications</h1>
+                       <div id="preamble">\r
+<div class="sectionbody">\r
+<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>\r
+<div class="ilist"><ul>\r
+<li>\r
+<p>\r
+Understand the purpose of debugging\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Track down problems and issues in your application that your tests aren't identifying\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Learn the different ways of debugging\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+Analyze the stack trace\r
+</p>\r
+</li>\r
+</ul></div>\r
+</div>\r
+</div>\r
+<h2 id="_view_helpers_for_debugging">1. View Helpers for Debugging</h2>\r
+<div class="sectionbody">\r
+<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>\r
+<div class="ilist"><ul>\r
+<li>\r
+<p>\r
+<tt>debug</tt>\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<tt>to_yaml</tt>\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<tt>inspect</tt>\r
+</p>\r
+</li>\r
+</ul></div>\r
+<h3 id="_debug">1.1. debug</h3>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content"><!-- Generator: GNU source-highlight 2.9\r
+by Lorenzo Bettini\r
+http://www.lorenzobettini.it\r
+http://www.gnu.org/software/src-highlite -->\r
+<pre><tt>&lt;%= debug @post %&gt;\r
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>\r
+  <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>\r
+  &lt;%=h @post.title %&gt;\r
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>\r
+</tt></pre></div></div>\r
+<div class="para"><p>You'll see something like this:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>--- !ruby/object:Post\r
+attributes:\r
+  updated_at: 2008-09-05 22:55:47\r
+  body: It's a very helpful guide for debugging your Rails app.\r
+  title: Rails debugging guide\r
+  published: t\r
+  id: "1"\r
+  created_at: 2008-09-05 22:55:47\r
+attributes_cache: {}\r
+\r
+\r
+Title: Rails debugging guide</tt></pre>\r
+</div></div>\r
+<h3 id="_to_yaml">1.2. to_yaml</h3>\r
+<div class="para"><p>Displaying an instance variable, or any other object or method, in yaml format can be achieved this way:</p></div>\r
+<div class="listingblock">\r
+<div class="content"><!-- Generator: GNU source-highlight 2.9\r
+by Lorenzo Bettini\r
+http://www.lorenzobettini.it\r
+http://www.gnu.org/software/src-highlite -->\r
+<pre><tt>&lt;%= simple_format @post.to_yaml %&gt;\r
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>\r
+  <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>\r
+  &lt;%=h @post.title %&gt;\r
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>\r
+</tt></pre></div></div>\r
+<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>\r
+<div class="para"><p>As a result of this, you will have something like this in your view:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>--- !ruby/object:Post\r
+attributes:\r
+updated_at: 2008-09-05 22:55:47\r
+body: It's a very helpful guide for debugging your Rails app.\r
+title: Rails debugging guide\r
+published: t\r
+id: "1"\r
+created_at: 2008-09-05 22:55:47\r
+attributes_cache: {}\r
+\r
+Title: Rails debugging guide</tt></pre>\r
+</div></div>\r
+<h3 id="_inspect">1.3. inspect</h3>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content"><!-- Generator: GNU source-highlight 2.9\r
+by Lorenzo Bettini\r
+http://www.lorenzobettini.it\r
+http://www.gnu.org/software/src-highlite -->\r
+<pre><tt>&lt;%= [1, 2, 3, 4, 5].inspect %&gt;\r
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>\r
+  <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>\r
+  &lt;%=h @post.title %&gt;\r
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>\r
+</tt></pre></div></div>\r
+<div class="para"><p>Will be rendered as follows:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>[1, 2, 3, 4, 5]\r
+\r
+Title: Rails debugging guide</tt></pre>\r
+</div></div>\r
+<h3 id="_debugging_javascript">1.4. Debugging Javascript</h3>\r
+<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>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content"><!-- Generator: GNU source-highlight 2.9\r
+by Lorenzo Bettini\r
+http://www.lorenzobettini.it\r
+http://www.gnu.org/software/src-highlite -->\r
+<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>\r
+</tt></pre></div></div>\r
+<div class="para"><p>Or, at any time, setting <tt>ActionView::Base.debug_rjs</tt> to <em>true</em>:</p></div>\r
+<div class="listingblock">\r
+<div class="content"><!-- Generator: GNU source-highlight 2.9\r
+by Lorenzo Bettini\r
+http://www.lorenzobettini.it\r
+http://www.gnu.org/software/src-highlite -->\r
+<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>\r
+</tt></pre></div></div>\r
+<div class="admonitionblock">\r
+<table><tr>\r
+<td class="icon">\r
+<img src="./images/icons/tip.png" alt="Tip" />\r
+</td>\r
+<td class="content">For more information on debugging javascript refer to <a href="http://getfirebug.com/">Firebug</a>, the popular debugger for Firefox.</td>\r
+</tr></table>\r
+</div>\r
+</div>\r
+<h2 id="_the_logger">2. The Logger</h2>\r
+<div class="sectionbody">\r
+<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>\r
+<h3 id="_what_is_the_logger">2.1. What is The Logger?</h3>\r
+<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>\r
+<div class="para"><p>You can specify an alternative logger in your <tt>environment.rb</tt> or any environment file:</p></div>\r
+<div class="listingblock">\r
+<div class="content"><!-- Generator: GNU source-highlight 2.9\r
+by Lorenzo Bettini\r
+http://www.lorenzobettini.it\r
+http://www.gnu.org/software/src-highlite -->\r
+<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>\r
+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>\r
+</tt></pre></div></div>\r
+<div class="para"><p>Or in the <tt>Initializer</tt> section, add <em>any</em> of the following</p></div>\r
+<div class="listingblock">\r
+<div class="content"><!-- Generator: GNU source-highlight 2.9\r
+by Lorenzo Bettini\r
+http://www.lorenzobettini.it\r
+http://www.gnu.org/software/src-highlite -->\r
+<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>\r
+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>\r
+</tt></pre></div></div>\r
+<div class="admonitionblock">\r
+<table><tr>\r
+<td class="icon">\r
+<img src="./images/icons/tip.png" alt="Tip" />\r
+</td>\r
+<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>\r
+</tr></table>\r
+</div>\r
+<h3 id="_log_levels">2.2. Log Levels</h3>\r
+<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>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content"><!-- Generator: GNU source-highlight 2.9\r
+by Lorenzo Bettini\r
+http://www.lorenzobettini.it\r
+http://www.gnu.org/software/src-highlite -->\r
+<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>\r
+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>\r
+</tt></pre></div></div>\r
+<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>\r
+<div class="admonitionblock">\r
+<table><tr>\r
+<td class="icon">\r
+<img src="./images/icons/tip.png" alt="Tip" />\r
+</td>\r
+<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>\r
+</tr></table>\r
+</div>\r
+<h3 id="_sending_messages">2.3. Sending Messages</h3>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content"><!-- Generator: GNU source-highlight 2.9\r
+by Lorenzo Bettini\r
+http://www.lorenzobettini.it\r
+http://www.gnu.org/software/src-highlite -->\r
+<pre><tt>logger<span style="color: #990000">.</span>debug <span style="color: #FF0000">"Person attributes hash: #{@person.attributes.inspect}"</span>\r
+logger<span style="color: #990000">.</span>info <span style="color: #FF0000">"Processing the request..."</span>\r
+logger<span style="color: #990000">.</span>fatal <span style="color: #FF0000">"Terminating application, raised unrecoverable error!!!"</span>\r
+</tt></pre></div></div>\r
+<div class="para"><p>Here's an example of a method instrumented with extra logging:</p></div>\r
+<div class="listingblock">\r
+<div class="content"><!-- Generator: GNU source-highlight 2.9\r
+by Lorenzo Bettini\r
+http://www.lorenzobettini.it\r
+http://www.gnu.org/software/src-highlite -->\r
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsController <span style="color: #990000">&lt;</span> ApplicationController\r
+  <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>\r
+\r
+  <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create\r
+    <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>\r
+    logger<span style="color: #990000">.</span>debug <span style="color: #FF0000">"New post: #{@post.attributes.inspect}"</span>\r
+    logger<span style="color: #990000">.</span>debug <span style="color: #FF0000">"Post should be valid: #{@post.valid?}"</span>\r
+\r
+    <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>save\r
+      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>\r
+      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>\r
+      redirect_to<span style="color: #990000">(</span><span style="color: #009900">@post</span><span style="color: #990000">)</span>\r
+    <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>\r
+      render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"new"</span>\r
+    <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>\r
+  <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>\r
+\r
+  <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>\r
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>\r
+</tt></pre></div></div>\r
+<div class="para"><p>Here's an example of the log generated by this method:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>Processing PostsController#create (for 127.0.0.1 at 2008-09-08 11:52:54) [POST]\r
+  Session ID: BAh7BzoMY3NyZl9pZCIlMDY5MWU1M2I1ZDRjODBlMzkyMWI1OTg2NWQyNzViZjYiCmZsYXNoSUM6J0FjdGl\r
+vbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA=--b18cd92fba90eacf8137e5f6b3b06c4d724596a4\r
+  Parameters: {"commit"=&gt;"Create", "post"=&gt;{"title"=&gt;"Debugging Rails",\r
+ "body"=&gt;"I'm learning how to print in logs!!!", "published"=&gt;"0"},\r
+ "authenticity_token"=&gt;"2059c1286e93402e389127b1153204e0d1e275dd", "action"=&gt;"create", "controller"=&gt;"posts"}\r
+New post: {"updated_at"=&gt;nil, "title"=&gt;"Debugging Rails", "body"=&gt;"I'm learning how to print in logs!!!",\r
+ "published"=&gt;false, "created_at"=&gt;nil}\r
+Post should be valid: true\r
+  Post Create (0.000443)   INSERT INTO "posts" ("updated_at", "title", "body", "published",\r
+ "created_at") VALUES('2008-09-08 14:52:54', 'Debugging Rails',\r
+ 'I''m learning how to print in logs!!!', 'f', '2008-09-08 14:52:54')\r
+The post was saved and now is the user is going to be redirected...\r
+Redirected to #&lt;Post:0x20af760&gt;\r
+Completed in 0.01224 (81 reqs/sec) | DB: 0.00044 (3%) | 302 Found [http://localhost/posts]</tt></pre>\r
+</div></div>\r
+<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>\r
+</div>\r
+<h2 id="_debugging_with_ruby_debug">3. Debugging with ruby-debug</h2>\r
+<div class="sectionbody">\r
+<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>\r
+<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>\r
+<h3 id="_setup">3.1. Setup</h3>\r
+<div class="para"><p>The debugger used by Rails, <tt>ruby-debug</tt>, comes as a gem. To install it, just run:</p></div>\r
+<div class="listingblock">\r
+<div class="content"><!-- Generator: GNU source-highlight 2.9\r
+by Lorenzo Bettini\r
+http://www.lorenzobettini.it\r
+http://www.gnu.org/software/src-highlite -->\r
+<pre><tt>$ sudo gem install ruby-debug\r
+</tt></pre></div></div>\r
+<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>\r
+<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>\r
+<div class="para"><p>Here's an example:</p></div>\r
+<div class="listingblock">\r
+<div class="content"><!-- Generator: GNU source-highlight 2.9\r
+by Lorenzo Bettini\r
+http://www.lorenzobettini.it\r
+http://www.gnu.org/software/src-highlite -->\r
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PeopleController <span style="color: #990000">&lt;</span> ApplicationController\r
+  <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> new\r
+    debugger\r
+    <span style="color: #009900">@person</span> <span style="color: #990000">=</span> Person<span style="color: #990000">.</span>new\r
+  <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>\r
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>\r
+</tt></pre></div></div>\r
+<div class="para"><p>If you see the message in the console or logs:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>***** Debugger requested, but was not available: Start server with --debugger to enable *****</tt></pre>\r
+</div></div>\r
+<div class="para"><p>Make sure you have started your web server with the option <tt>&#8212;debugger</tt>:</p></div>\r
+<div class="listingblock">\r
+<div class="content"><!-- Generator: GNU source-highlight 2.9\r
+by Lorenzo Bettini\r
+http://www.lorenzobettini.it\r
+http://www.gnu.org/software/src-highlite -->\r
+<pre><tt><span style="color: #990000">~</span>/PathTo/rails_project$ script/server --debugger\r
+<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>\r
+<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>\r
+<span style="color: #990000">=&gt;</span> Debugger enabled\r
+<span style="color: #990000">...</span>\r
+</tt></pre></div></div>\r
+<div class="admonitionblock">\r
+<table><tr>\r
+<td class="icon">\r
+<img src="./images/icons/tip.png" alt="Tip" />\r
+</td>\r
+<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>\r
+</tr></table>\r
+</div>\r
+<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>\r
+<h3 id="_the_shell">3.2. The Shell</h3>\r
+<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>\r
+<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>\r
+<div class="para"><p>For example:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>@posts = Post.find(:all)\r
+(rdb:7)</tt></pre>\r
+</div></div>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:7) help\r
+ruby-debug help v0.10.2\r
+Type 'help &lt;command-name&gt;' for help on a specific command\r
+\r
+Available commands:\r
+backtrace  delete   enable  help    next  quit     show    trace\r
+break      disable  eval    info    p     reload   source  undisplay\r
+catch      display  exit    irb     pp    restart  step    up\r
+condition  down     finish  list    ps    save     thread  var\r
+continue   edit     frame   method  putl  set      tmate   where</tt></pre>\r
+</div></div>\r
+<div class="admonitionblock">\r
+<table><tr>\r
+<td class="icon">\r
+<img src="./images/icons/tip.png" alt="Tip" />\r
+</td>\r
+<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>\r
+</tr></table>\r
+</div>\r
+<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>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:7) list\r
+[1, 10] in /PathToProject/posts_controller.rb\r
+   1  class PostsController &lt; ApplicationController\r
+   2    # GET /posts\r
+   3    # GET /posts.xml\r
+   4    def index\r
+   5      debugger\r
+=&gt; 6      @posts = Post.find(:all)\r
+   7\r
+   8      respond_to do |format|\r
+   9        format.html # index.html.erb\r
+   10        format.xml  { render :xml =&gt; @posts }</tt></pre>\r
+</div></div>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:7) l\r
+[11, 20] in /PathTo/project/app/controllers/posts_controller.rb\r
+   11      end\r
+   12    end\r
+   13\r
+   14    # GET /posts/1\r
+   15    # GET /posts/1.xml\r
+   16    def show\r
+   17      @post = Post.find(params[:id])\r
+   18\r
+   19      respond_to do |format|\r
+   20        format.html # show.html.erb</tt></pre>\r
+</div></div>\r
+<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>\r
+<h3 id="_the_context">3.3. The Context</h3>\r
+<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>\r
+<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>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:5) where\r
+    #0 PostsController.index\r
+       at line /PathTo/project/app/controllers/posts_controller.rb:6\r
+    #1 Kernel.send\r
+       at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175\r
+    #2 ActionController::Base.perform_action_without_filters\r
+       at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175\r
+    #3 ActionController::Filters::InstanceMethods.call_filters(chain#ActionController::Fil...,...)\r
+       at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/filters.rb:617\r
+...</tt></pre>\r
+</div></div>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:5) frame 2\r
+#2 ActionController::Base.perform_action_without_filters\r
+       at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175</tt></pre>\r
+</div></div>\r
+<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>\r
+<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>\r
+<h3 id="_threads">3.4. Threads</h3>\r
+<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>\r
+<div class="ilist"><ul>\r
+<li>\r
+<p>\r
+<tt>thread</tt> shows the current thread.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<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.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<tt>thread stop <em>n</em></tt> stop thread <em>n</em>.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<tt>thread resume <em>n</em></tt> resumes thread <em>n</em>.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<tt>thread switch <em>n</em></tt> switches the current thread context to <em>n</em>.\r
+</p>\r
+</li>\r
+</ul></div>\r
+<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>\r
+<h3 id="_inspecting_variables">3.5. Inspecting Variables</h3>\r
+<div class="para"><p>Any expression can be evaluated in the current context. To evaluate an expression, just type it!</p></div>\r
+<div class="para"><p>This example shows how you can print the instance_variables defined within the current context:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>@posts = Post.find(:all)\r
+(rdb:11) instance_variables\r
+["@_response", "@action_name", "@url", "@_session", "@_cookies", "@performed_render", "@_flash", "@template", "@_params", "@before_filter_chain_aborted", "@request_origin", "@_headers", "@performed_redirect", "@_request"]</tt></pre>\r
+</div></div>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:11) next\r
+Processing PostsController#index (for 127.0.0.1 at 2008-09-04 19:51:34) [GET]\r
+  Session ID: BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA==--b16e91b992453a8cc201694d660147bba8b0fd0e\r
+  Parameters: {"action"=&gt;"index", "controller"=&gt;"posts"}\r
+/PathToProject/posts_controller.rb:8\r
+respond_to do |format|</tt></pre>\r
+</div></div>\r
+<div class="para"><p>And then ask again for the instance_variables:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:11) instance_variables.include? "@posts"\r
+true</tt></pre>\r
+</div></div>\r
+<div class="para"><p>Now <tt>@posts</tt> is a included in the instance variables, because the line defining it was executed.</p></div>\r
+<div class="admonitionblock">\r
+<table><tr>\r
+<td class="icon">\r
+<img src="./images/icons/tip.png" alt="Tip" />\r
+</td>\r
+<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>\r
+</tr></table>\r
+</div>\r
+<div class="para"><p>The <tt>var</tt> method is the most convenient way to show variables and their values:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>var\r
+(rdb:1) v[ar] const &lt;object&gt;            show constants of object\r
+(rdb:1) v[ar] g[lobal]                  show global variables\r
+(rdb:1) v[ar] i[nstance] &lt;object&gt;       show instance variables of object\r
+(rdb:1) v[ar] l[ocal]                   show local variables</tt></pre>\r
+</div></div>\r
+<div class="para"><p>This is a great way to inspect the values of the current context variables. For example:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:9) var local\r
+  __dbg_verbose_save =&gt; false</tt></pre>\r
+</div></div>\r
+<div class="para"><p>You can also inspect for an object method this way:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:9) var instance Post.new\r
+@attributes = {"updated_at"=&gt;nil, "body"=&gt;nil, "title"=&gt;nil, "published"=&gt;nil, "created_at"...\r
+@attributes_cache = {}\r
+@new_record = true</tt></pre>\r
+</div></div>\r
+<div class="admonitionblock">\r
+<table><tr>\r
+<td class="icon">\r
+<img src="./images/icons/tip.png" alt="Tip" />\r
+</td>\r
+<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>\r
+</tr></table>\r
+</div>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:1) display @recent_comments\r
+1: @recent_comments =</tt></pre>\r
+</div></div>\r
+<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>\r
+<h3 id="_step_by_step">3.6. Step by Step</h3>\r
+<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>\r
+<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>\r
+<div class="admonitionblock">\r
+<table><tr>\r
+<td class="icon">\r
+<img src="./images/icons/tip.png" alt="Tip" />\r
+</td>\r
+<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>\r
+</tr></table>\r
+</div>\r
+<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>\r
+<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>\r
+<div class="para"><p>For example, consider this block of code with an included <tt>debugger</tt> statement:</p></div>\r
+<div class="listingblock">\r
+<div class="content"><!-- Generator: GNU source-highlight 2.9\r
+by Lorenzo Bettini\r
+http://www.lorenzobettini.it\r
+http://www.gnu.org/software/src-highlite -->\r
+<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\r
+  has_one <span style="color: #990000">:</span>editorial\r
+  has_many <span style="color: #990000">:</span>comments\r
+\r
+  <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>\r
+    debugger\r
+    <span style="color: #009900">@recent_comments</span> <span style="color: #990000">||=</span> comments<span style="color: #990000">.</span>find<span style="color: #990000">(</span>\r
+      <span style="color: #990000">:</span>all<span style="color: #990000">,</span>\r
+      <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>\r
+      <span style="color: #990000">:</span>limit <span style="color: #990000">=&gt;</span> limit\r
+    <span style="color: #990000">)</span>\r
+  <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>\r
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>\r
+</tt></pre></div></div>\r
+<div class="admonitionblock">\r
+<table><tr>\r
+<td class="icon">\r
+<img src="./images/icons/tip.png" alt="Tip" />\r
+</td>\r
+<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>\r
+</tr></table>\r
+</div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>/PathTo/project $ script/console\r
+Loading development environment (Rails 2.1.0)\r
+&gt;&gt; require "ruby-debug"\r
+=&gt; []\r
+&gt;&gt; author = Author.first\r
+=&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;\r
+&gt;&gt; author.find_recent_comments\r
+/PathTo/project/app/models/author.rb:11\r
+)</tt></pre>\r
+</div></div>\r
+<div class="para"><p>With the code stopped, take a look around:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:1) list\r
+[6, 15] in /PathTo/project/app/models/author.rb\r
+   6      debugger\r
+   7      @recent_comments ||= comments.find(\r
+   8        :all,\r
+   9        :conditions =&gt; ["created_at &gt; ?", 1.week.ago],\r
+   10        :limit =&gt; limit\r
+=&gt; 11      )\r
+   12    end\r
+   13  end</tt></pre>\r
+</div></div>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:1) var instance\r
+@attributes = {"updated_at"=&gt;"2008-07-31 12:46:10", "id"=&gt;"1", "first_name"=&gt;"Bob", "las...\r
+@attributes_cache = {}</tt></pre>\r
+</div></div>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:1) next\r
+/PathTo/project/app/models/author.rb:12\r
+@recent_comments\r
+(rdb:1) var instance\r
+@attributes = {"updated_at"=&gt;"2008-07-31 12:46:10", "id"=&gt;"1", "first_name"=&gt;"Bob", "las...\r
+@attributes_cache = {}\r
+@comments = []\r
+@recent_comments = []</tt></pre>\r
+</div></div>\r
+<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>\r
+<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>\r
+<h3 id="_breakpoints">3.7. Breakpoints</h3>\r
+<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>\r
+<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>\r
+<div class="ilist"><ul>\r
+<li>\r
+<p>\r
+<tt>break line</tt>: set breakpoint in the <em>line</em> in the current source file.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<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.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<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.\r
+</p>\r
+</li>\r
+</ul></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:5) break 10\r
+Breakpoint 1 file /PathTo/project/vendor/rails/actionpack/lib/action_controller/filters.rb, line 10</tt></pre>\r
+</div></div>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:5) info breakpoints\r
+Num Enb What\r
+  1 y   at filters.rb:10</tt></pre>\r
+</div></div>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>(rdb:5) delete 1\r
+(rdb:5) info breakpoints\r
+No breakpoints.</tt></pre>\r
+</div></div>\r
+<div class="para"><p>You can also enable or disable breakpoints:</p></div>\r
+<div class="ilist"><ul>\r
+<li>\r
+<p>\r
+<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.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<tt>disable breakpoints</tt>: the <em>breakpoints</em> will have no effect on your program.\r
+</p>\r
+</li>\r
+</ul></div>\r
+<h3 id="_catching_exceptions">3.8. Catching Exceptions</h3>\r
+<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>\r
+<div class="para"><p>To list all active catchpoints use <tt>catch</tt>.</p></div>\r
+<h3 id="_resuming_execution">3.9. Resuming Execution</h3>\r
+<div class="para"><p>There are two ways to resume execution of an application that is stopped in the debugger:</p></div>\r
+<div class="ilist"><ul>\r
+<li>\r
+<p>\r
+<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.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<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.\r
+</p>\r
+</li>\r
+</ul></div>\r
+<h3 id="_editing">3.10. Editing</h3>\r
+<div class="para"><p>Two commands allow you to open code from the debugger into an editor:</p></div>\r
+<div class="ilist"><ul>\r
+<li>\r
+<p>\r
+<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.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<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.\r
+</p>\r
+</li>\r
+</ul></div>\r
+<h3 id="_quitting">3.11. Quitting</h3>\r
+<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>\r
+<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>\r
+<h3 id="_settings">3.12. Settings</h3>\r
+<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>\r
+<div class="ilist"><ul>\r
+<li>\r
+<p>\r
+<tt>set reload</tt>: Reload source code when changed.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<tt>set autolist</tt>: Execute <tt>list</tt> command on every breakpoint.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<tt>set listsize <em>n</em></tt>: Set number of source lines to list by default to <em>n</em>.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<tt>set forcestep</tt>: Make sure the <tt>next</tt> and <tt>step</tt> commands always move to a new line\r
+</p>\r
+</li>\r
+</ul></div>\r
+<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>\r
+<div class="admonitionblock">\r
+<table><tr>\r
+<td class="icon">\r
+<img src="./images/icons/tip.png" alt="Tip" />\r
+</td>\r
+<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>\r
+</tr></table>\r
+</div>\r
+<div class="para"><p>Here's a good start for an <tt>.rdebugrc</tt>:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>set autolist\r
+set forcestep\r
+set listsize 25</tt></pre>\r
+</div></div>\r
+</div>\r
+<h2 id="_debugging_memory_leaks">4. Debugging Memory Leaks</h2>\r
+<div class="sectionbody">\r
+<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>\r
+<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>\r
+<h3 id="_bleakhouse">4.1. BleakHouse</h3>\r
+<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>\r
+<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>\r
+<div class="para"><p>To install it run:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>sudo gem install bleak_house</tt></pre>\r
+</div></div>\r
+<div class="para"><p>Then setup you application for profiling. Then add the following at the bottom of config/environment.rb:</p></div>\r
+<div class="listingblock">\r
+<div class="content"><!-- Generator: GNU source-highlight 2.9\r
+by Lorenzo Bettini\r
+http://www.lorenzobettini.it\r
+http://www.gnu.org/software/src-highlite -->\r
+<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>\r
+</tt></pre></div></div>\r
+<div class="para"><p>Start a server instance with BleakHouse integration:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>RAILS_ENV=production BLEAK_HOUSE=1 ruby-bleak-house ./script/server</tt></pre>\r
+</div></div>\r
+<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>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>** BleakHouse: working...\r
+** BleakHouse: complete\r
+** Bleakhouse: run 'bleak /tmp/bleak.5979.0.dump' to analyze.</tt></pre>\r
+</div></div>\r
+<div class="para"><p>To analyze it, just run the listed command. The top 20 leakiest lines will be listed:</p></div>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><tt>  191691 total objects\r
+  Final heap size 191691 filled, 220961 free\r
+  Displaying top 20 most common line/class pairs\r
+  89513 __null__:__null__:__node__\r
+  41438 __null__:__null__:String\r
+  2348 /opt/local//lib/ruby/site_ruby/1.8/rubygems/specification.rb:557:Array\r
+  1508 /opt/local//lib/ruby/gems/1.8/specifications/gettext-1.90.0.gemspec:14:String\r
+  1021 /opt/local//lib/ruby/gems/1.8/specifications/heel-0.2.0.gemspec:14:String\r
+   951 /opt/local//lib/ruby/site_ruby/1.8/rubygems/version.rb:111:String\r
+   935 /opt/local//lib/ruby/site_ruby/1.8/rubygems/specification.rb:557:String\r
+   834 /opt/local//lib/ruby/site_ruby/1.8/rubygems/version.rb:146:Array\r
+  ...</tt></pre>\r
+</div></div>\r
+<div class="para"><p>This way you can find where your application is leaking memory and fix it.</p></div>\r
+<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>\r
+<h3 id="_valgrind">4.2. Valgrind</h3>\r
+<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>\r
+<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>\r
+<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>\r
+</div>\r
+<h2 id="_plugins_for_debugging">5. Plugins for Debugging</h2>\r
+<div class="sectionbody">\r
+<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>\r
+<div class="ilist"><ul>\r
+<li>\r
+<p>\r
+<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.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<a href="http://github.com/ntalbott/query_trace/tree/master">Query Trace</a>: Adds query origin tracing to your logs.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<a href="http://github.com/dan-manges/query_stats/tree/master">Query Stats</a>: A Rails plugin to track database queries.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<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.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<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.\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<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.\r
+</p>\r
+</li>\r
+</ul></div>\r
+</div>\r
+<h2 id="_references">6. References</h2>\r
+<div class="sectionbody">\r
+<div class="ilist"><ul>\r
+<li>\r
+<p>\r
+<a href="http://www.datanoise.com/ruby-debug">ruby-debug Homepage</a>\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<a href="http://www.sitepoint.com/article/debug-rails-app-ruby-debug/">Article: Debugging a Rails application with ruby-debug</a>\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<a href="http://brian.maybeyoureinsane.net/blog/2007/05/07/ruby-debug-basics-screencast/">ruby-debug Basics screencast</a>\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<a href="http://railscasts.com/episodes/54-debugging-with-ruby-debug">Ryan Bate's ruby-debug screencast</a>\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<a href="http://railscasts.com/episodes/24-the-stack-trace">Ryan Bate's stack trace screencast</a>\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<a href="http://railscasts.com/episodes/56-the-logger">Ryan Bate's logger screencast</a>\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<a href="http://bashdb.sourceforge.net/ruby-debug.html">Debugging with ruby-debug</a>\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<a href="http://cheat.errtheblog.com/s/rdebug/">ruby-debug cheat sheet</a>\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<a href="http://wiki.rubyonrails.org/rails/pages/HowtoConfigureLogging">Ruby on Rails Wiki: How to Configure Logging</a>\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+<a href="http://blog.evanweaver.com/files/doc/fauna/bleak_house/files/README.html">Bleak House Documentation</a>\r
+</p>\r
+</li>\r
+</ul></div>\r
+</div>\r
+<h2 id="_changelog">7. Changelog</h2>\r
+<div class="sectionbody">\r
+<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/5">Lighthouse ticket</a></p></div>\r
+<div class="ilist"><ul>\r
+<li>\r
+<p>\r
+November 3, 2008: Accepted for publication. Added RJS, memory leaks and plugins chapters by <a href="../authors.html#miloops">Emilio Tagua</a>\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+October 19, 2008: Copy editing pass by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>\r
+</p>\r
+</li>\r
+<li>\r
+<p>\r
+September 16, 2008: initial version by <a href="../authors.html#miloops">Emilio Tagua</a>\r
+</p>\r
+</li>\r
+</ul></div>\r
+</div>\r
+
+               </div>
+       </div>
+</body>
+</html>