aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/source/debugging_rails_applications.txt
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-11-06 01:10:30 +0530
committerPratik Naik <pratiknaik@gmail.com>2008-11-06 01:10:30 +0530
commit396d599e24cbfa15c62b20fab8cc02cdba046ce3 (patch)
tree11dd8368e2674342098af21136893bee491c44ff /railties/doc/guides/source/debugging_rails_applications.txt
parent32089cbcc9ca3fb935f783e7a7ef2b60b7d43006 (diff)
downloadrails-396d599e24cbfa15c62b20fab8cc02cdba046ce3.tar.gz
rails-396d599e24cbfa15c62b20fab8cc02cdba046ce3.tar.bz2
rails-396d599e24cbfa15c62b20fab8cc02cdba046ce3.zip
Update guides from docrails
Diffstat (limited to 'railties/doc/guides/source/debugging_rails_applications.txt')
-rw-r--r--railties/doc/guides/source/debugging_rails_applications.txt110
1 files changed, 103 insertions, 7 deletions
diff --git a/railties/doc/guides/source/debugging_rails_applications.txt b/railties/doc/guides/source/debugging_rails_applications.txt
index 24eb0c0431..b45473fc14 100644
--- a/railties/doc/guides/source/debugging_rails_applications.txt
+++ b/railties/doc/guides/source/debugging_rails_applications.txt
@@ -31,7 +31,6 @@ The `debug` helper will return a <pre>-tag that renders the object using the YAM
You'll see something like this:
-[source, log]
----------------------------------------------------------------------------
--- !ruby/object:Post
attributes:
@@ -64,7 +63,6 @@ The `to_yaml` method converts the method to YAML format leaving it more readable
As a result of this, you will have something like this in your view:
-[source, log]
----------------------------------------------------------------------------
--- !ruby/object:Post
attributes:
@@ -94,13 +92,33 @@ Another useful method for displaying object values is `inspect`, especially when
Will be rendered as follows:
-[source, log]
----------------------------------------------------------------------------
[1, 2, 3, 4, 5]
Title: Rails debugging guide
----------------------------------------------------------------------------
+=== Debugging Javascript
+
+Rails has built-in support to debug RJS, to active it, set `ActionView::Base.debug_rjs` to _true_, 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).
+
+To enable it, add the following in the `Rails::Initializer do |config|` block inside +environment.rb+:
+
+[source, ruby]
+----------------------------------------------------------------------------
+config.action_view[:debug_rjs] = true
+----------------------------------------------------------------------------
+
+Or, at any time, setting `ActionView::Base.debug_rjs` to _true_:
+
+[source, ruby]
+----------------------------------------------------------------------------
+ActionView::Base.debug_rjs = true
+----------------------------------------------------------------------------
+
+[TIP]
+For more information on debugging javascript refer to link:http://getfirebug.com/[Firebug], the popular debugger for Firefox.
+
== The Logger
It can also be useful to save information to log files at runtime. Rails maintains a separate log file for each runtime environment.
@@ -183,7 +201,6 @@ end
Here's an example of the log generated by this method:
-[source, log]
----------------------------------------------------------------------------
Processing PostsController#create (for 127.0.0.1 at 2008-09-08 11:52:54) [POST]
Session ID: BAh7BzoMY3NyZl9pZCIlMDY5MWU1M2I1ZDRjODBlMzkyMWI1OTg2NWQyNzViZjYiCmZsYXNoSUM6J0FjdGl
@@ -237,7 +254,6 @@ end
If you see the message in the console or logs:
-[source, log]
----------------------------------------------------------------------------
***** Debugger requested, but was not available: Start server with --debugger to enable *****
----------------------------------------------------------------------------
@@ -271,7 +287,7 @@ For example:
(rdb:7)
----------------------------------------------------------------------------
-Now it's time to play and dig into your application. A good place to start is by asking the debugger for help... so type: `help` (You didn't see that coming, right?)
+Now it's time to explore and dig into your application. A good place to start is by asking the debugger for help... so type: `help` (You didn't see that coming, right?)
----------------------------------------------------------------------------
(rdb:7) help
@@ -610,13 +626,91 @@ You can include any number of these configuration lines inside a `.rdebugrc` fil
Here's a good start for an `.rdebugrc`:
-[source, log]
----------------------------------------------------------------------------
set autolist
set forcestep
set listsize 25
----------------------------------------------------------------------------
+== Debugging Memory Leaks
+
+A Ruby application (on Rails or not), can leak memory - either in the Ruby code or at the C code level.
+
+In this section, you will learn how to find and fix such leaks by using Bleak House and Valgrind debugging tools.
+
+=== BleakHouse
+
+link:http://github.com/fauna/bleak_house/tree/master[BleakHouse] is a library for finding memory leaks.
+
+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.
+
+To install it run:
+
+----------------------------------------------------------------------------
+sudo gem install bleak_house
+----------------------------------------------------------------------------
+
+Then setup you application for profiling. Then add the following at the bottom of config/environment.rb:
+
+[source, ruby]
+----------------------------------------------------------------------------
+require 'bleak_house' if ENV['BLEAK_HOUSE']
+----------------------------------------------------------------------------
+
+Start a server instance with BleakHouse integration:
+
+----------------------------------------------------------------------------
+RAILS_ENV=production BLEAK_HOUSE=1 ruby-bleak-house ./script/server
+----------------------------------------------------------------------------
+
+Make sure to run a couple hundred requests to get better data samples, then press `CTRL-C`. The server will stop and Bleak House will produce a dumpfile in `/tmp`:
+
+----------------------------------------------------------------------------
+** BleakHouse: working...
+** BleakHouse: complete
+** Bleakhouse: run 'bleak /tmp/bleak.5979.0.dump' to analyze.
+----------------------------------------------------------------------------
+
+To analyze it, just run the listed command. The top 20 leakiest lines will be listed:
+
+----------------------------------------------------------------------------
+ 191691 total objects
+ Final heap size 191691 filled, 220961 free
+ Displaying top 20 most common line/class pairs
+ 89513 __null__:__null__:__node__
+ 41438 __null__:__null__:String
+ 2348 /opt/local//lib/ruby/site_ruby/1.8/rubygems/specification.rb:557:Array
+ 1508 /opt/local//lib/ruby/gems/1.8/specifications/gettext-1.90.0.gemspec:14:String
+ 1021 /opt/local//lib/ruby/gems/1.8/specifications/heel-0.2.0.gemspec:14:String
+ 951 /opt/local//lib/ruby/site_ruby/1.8/rubygems/version.rb:111:String
+ 935 /opt/local//lib/ruby/site_ruby/1.8/rubygems/specification.rb:557:String
+ 834 /opt/local//lib/ruby/site_ruby/1.8/rubygems/version.rb:146:Array
+ ...
+----------------------------------------------------------------------------
+
+This way you can find where your application is leaking memory and fix it.
+
+If link:http://github.com/fauna/bleak_house/tree/master[BleakHouse] 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.
+
+=== Valgrind
+
+link:http://valgrind.org/[Valgrind] is a Linux-only application for detecting C-based memory leaks and race conditions.
+
+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 `malloc()` but is doesn't properly call `free()`, this memory won't be available until the app terminates.
+
+For further information on how to install Valgrind and use with Ruby, refer to link:http://blog.evanweaver.com/articles/2008/02/05/valgrind-and-ruby/[Valgrind and Ruby] by Evan Weaver.
+
+== Plugins for Debugging
+
+There are some Rails plugins to help you to find errors and debug your application. Here is a list of useful plugins for debugging:
+
+* link:http://github.com/drnic/rails-footnotes/tree/master[Footnotes]: Every Rails page has footnotes that link give request information and link back to your source via TextMate.
+* link:http://github.com/ntalbott/query_trace/tree/master[Query Trace]: Adds query origin tracing to your logs.
+* link:http://github.com/dan-manges/query_stats/tree/master[Query Stats]: A Rails plugin to track database queries.
+* link:http://code.google.com/p/query-reviewer/[Query Reviewer]: 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.
+* link:http://github.com/rails/exception_notification/tree/master[Exception Notifier]: Provides a mailer object and a default set of templates for sending email notifications when errors occur in a Rails application.
+* link:http://github.com/defunkt/exception_logger/tree/master[Exception Logger]: Logs your Rails exceptions in the database and provides a funky web interface to manage them.
+
== References
* link:http://www.datanoise.com/ruby-debug[ruby-debug Homepage]
@@ -628,10 +722,12 @@ set listsize 25
* link:http://bashdb.sourceforge.net/ruby-debug.html[Debugging with ruby-debug]
* link:http://cheat.errtheblog.com/s/rdebug/[ruby-debug cheat sheet]
* link:http://wiki.rubyonrails.org/rails/pages/HowtoConfigureLogging[Ruby on Rails Wiki: How to Configure Logging]
+* link:http://blog.evanweaver.com/files/doc/fauna/bleak_house/files/README.html[Bleak House Documentation]
== Changelog ==
http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/5[Lighthouse ticket]
+* November 3, 2008: Accepted for publication. Added RJS, memory leaks and plugins chapters by link:../authors.html#miloops[Emilio Tagua]
* October 19, 2008: Copy editing pass by link:../authors.html#mgunderloy[Mike Gunderloy]
* September 16, 2008: initial version by link:../authors.html#miloops[Emilio Tagua]