diff options
Diffstat (limited to 'guides/source/debugging_rails_applications.md')
-rw-r--r-- | guides/source/debugging_rails_applications.md | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/guides/source/debugging_rails_applications.md b/guides/source/debugging_rails_applications.md index d4553e7b74..35d4f5fb8f 100644 --- a/guides/source/debugging_rails_applications.md +++ b/guides/source/debugging_rails_applications.md @@ -174,7 +174,7 @@ end Here's an example of the log generated by this method: -```shell +```bash Processing PostsController#create (for 127.0.0.1 at 2008-09-08 11:52:54) [POST] Session ID: BAh7BzoMY3NyZl9pZCIlMDY5MWU1M2I1ZDRjODBlMzkyMWI1OTg2NWQyNzViZjYiCmZsYXNoSUM6J0FjdGl vbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA=--b18cd92fba90eacf8137e5f6b3b06c4d724596a4 @@ -216,7 +216,7 @@ The debugger can also help you if you want to learn about the Rails source code Rails uses the +debugger+ gem to set breakpoints and step through live code. To install it, just run: -```shell +```bash $ gem install debugger ``` @@ -235,13 +235,13 @@ end If you see the message in the console or logs: -```shell +```bash ***** Debugger requested, but was not available: Start server with --debugger to enable ***** ``` Make sure you have started your web server with the option +--debugger+: -```shell +```bash $ rails server --debugger => Booting WEBrick => Rails 3.0.0 application starting on http://0.0.0.0:3000 @@ -259,14 +259,14 @@ If you got there by a browser request, the browser tab containing the request wi For example: -```shell +```bash @posts = Post.all (rdb:7) ``` 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?) -```shell +```bash (rdb:7) help ruby-debug help v0.10.2 Type 'help <command-name>' for help on a specific command @@ -285,7 +285,7 @@ The next command to learn is one of the most useful: +list+. You can abbreviate 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 +=>+. -```shell +```bash (rdb:7) list [1, 10] in /PathToProject/posts_controller.rb 1 class PostsController < ApplicationController @@ -302,7 +302,7 @@ This command shows you where you are in the code by printing 10 lines centered a If you repeat the +list+ command, this time using just +l+, the next ten lines of the file will be printed out. -```shell +```bash (rdb:7) l [11, 20] in /PathTo/project/app/controllers/posts_controller.rb 11 end @@ -321,7 +321,7 @@ And so on until the end of the current file. When the end of file is reached, th On the other hand, to see the previous ten lines you should type +list-+ (or +l-+) -```shell +```bash (rdb:7) l- [1, 10] in /PathToProject/posts_controller.rb 1 class PostsController < ApplicationController @@ -339,7 +339,7 @@ On the other hand, to see the previous ten lines you should type +list-+ (or +l- This way you can move inside the file, being able to see the code above and over the line you added the +debugger+. Finally, to see where you are in the code again you can type +list=+ -```shell +```bash (rdb:7) list= [1, 10] in /PathToProject/posts_controller.rb 1 class PostsController < ApplicationController @@ -362,7 +362,7 @@ The debugger creates a context when a stopping point or an event is reached. The At any time you can call the +backtrace+ command (or its alias +where+) 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 +backtrace+ will supply the answer. -```shell +```bash (rdb:5) where #0 PostsController.index at line /PathTo/project/app/controllers/posts_controller.rb:6 @@ -377,7 +377,7 @@ At any time you can call the +backtrace+ command (or its alias +where+) to print You move anywhere you want in this trace (thus changing the context) by using the +frame _n_+ command, where _n_ is the specified frame number. -```shell +```bash (rdb:5) frame 2 #2 ActionController::Base.perform_action_without_filters at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175 @@ -405,7 +405,7 @@ Any expression can be evaluated in the current context. To evaluate an expressio This example shows how you can print the instance_variables defined within the current context: -```shell +```bash @posts = Post.all (rdb:11) instance_variables ["@_response", "@action_name", "@url", "@_session", "@_cookies", "@performed_render", "@_flash", "@template", "@_params", "@before_filter_chain_aborted", "@request_origin", "@_headers", "@performed_redirect", "@_request"] @@ -413,7 +413,7 @@ This example shows how you can print the instance_variables defined within the c 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 +next+ (you'll learn more about this command later in this guide). -```shell +```bash (rdb:11) next Processing PostsController#index (for 127.0.0.1 at 2008-09-04 19:51:34) [GET] Session ID: BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA==--b16e91b992453a8cc201694d660147bba8b0fd0e @@ -424,7 +424,7 @@ respond_to do |format| And then ask again for the instance_variables: -```shell +```bash (rdb:11) instance_variables.include? "@posts" true ``` @@ -435,7 +435,7 @@ TIP: You can also step into *irb* mode with the command +irb+ (of course!). This The +var+ method is the most convenient way to show variables and their values: -```shell +```bash var (rdb:1) v[ar] const <object> show constants of object (rdb:1) v[ar] g[lobal] show global variables @@ -445,14 +445,14 @@ var This is a great way to inspect the values of the current context variables. For example: -```shell +```bash (rdb:9) var local __dbg_verbose_save => false ``` You can also inspect for an object method this way: -```shell +```bash (rdb:9) var instance Post.new @attributes = {"updated_at"=>nil, "body"=>nil, "title"=>nil, "published"=>nil, "created_at"... @attributes_cache = {} @@ -463,7 +463,7 @@ TIP: The commands +p+ (print) and +pp+ (pretty print) can be used to evaluate Ru You can use also +display+ to start watching variables. This is a good way of tracking the values of a variable while the execution goes on. -```shell +```bash (rdb:1) display @recent_comments 1: @recent_comments = ``` @@ -498,7 +498,7 @@ end TIP: You can use the debugger while using +rails console+. Just remember to +require "debugger"+ before calling the +debugger+ method. -```shell +```bash $ rails console Loading development environment (Rails 3.1.0) >> require "debugger" @@ -512,7 +512,7 @@ Loading development environment (Rails 3.1.0) With the code stopped, take a look around: -```shell +```bash (rdb:1) list [2, 9] in /PathTo/project/app/models/author.rb 2 has_one :editorial @@ -527,7 +527,7 @@ With the code stopped, take a look around: You are at the end of the line, but... was this line executed? You can inspect the instance variables. -```shell +```bash (rdb:1) var instance @attributes = {"updated_at"=>"2008-07-31 12:46:10", "id"=>"1", "first_name"=>"Bob", "las... @attributes_cache = {} @@ -535,7 +535,7 @@ You are at the end of the line, but... was this line executed? You can inspect t +@recent_comments+ hasn't been defined yet, so it's clear that this line hasn't been executed yet. Use the +next+ command to move on in the code: -```shell +```bash (rdb:1) next /PathTo/project/app/models/author.rb:12 @recent_comments @@ -560,14 +560,14 @@ You can add breakpoints dynamically with the command +break+ (or just +b+). Ther * +break file:line [if expression]+: set breakpoint in the _line_ number inside the _file_. If an _expression_ is given it must evaluated to _true_ to fire up the debugger. * +break class(.|\#)method [if expression]+: set breakpoint in _method_ (. and \# for class and instance method respectively) defined in _class_. The _expression_ works the same way as with file:line. -```shell +```bash (rdb:5) break 10 Breakpoint 1 file /PathTo/project/vendor/rails/actionpack/lib/action_controller/filters.rb, line 10 ``` Use +info breakpoints _n_+ or +info break _n_+ to list breakpoints. If you supply a number, it lists that breakpoint. Otherwise it lists all breakpoints. -```shell +```bash (rdb:5) info breakpoints Num Enb What 1 y at filters.rb:10 @@ -575,7 +575,7 @@ Num Enb What To delete breakpoints: use the command +delete _n_+ to remove the breakpoint number _n_. If no number is specified, it deletes all breakpoints that are currently active.. -```shell +```bash (rdb:5) delete 1 (rdb:5) info breakpoints No breakpoints. @@ -627,7 +627,7 @@ TIP: You can save these settings in an +.rdebugrc+ file in your home directory. Here's a good start for an +.rdebugrc+: -```shell +```bash set autolist set forcestep set listsize 25 @@ -648,7 +648,7 @@ If a Ruby object does not go out of scope, the Ruby Garbage Collector won't swee To install it run: -```shell +```bash $ gem install bleak_house ``` @@ -660,13 +660,13 @@ require 'bleak_house' if ENV['BLEAK_HOUSE'] Start a server instance with BleakHouse integration: -```shell +```bash $ RAILS_ENV=production BLEAK_HOUSE=1 ruby-bleak-house rails 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+: -```shell +```bash ** BleakHouse: working... ** BleakHouse: complete ** Bleakhouse: run 'bleak /tmp/bleak.5979.0.dump' to analyze. @@ -674,7 +674,7 @@ Make sure to run a couple hundred requests to get better data samples, then pres To analyze it, just run the listed command. The top 20 leakiest lines will be listed: -```shell +```bash 191691 total objects Final heap size 191691 filled, 220961 free Displaying top 20 most common line/class pairs |