diff options
Diffstat (limited to 'guides/source/debugging_rails_applications.md')
-rw-r--r-- | guides/source/debugging_rails_applications.md | 66 |
1 files changed, 33 insertions, 33 deletions
diff --git a/guides/source/debugging_rails_applications.md b/guides/source/debugging_rails_applications.md index a2e7a9ed97..10257e55bc 100644 --- a/guides/source/debugging_rails_applications.md +++ b/guides/source/debugging_rails_applications.md @@ -21,9 +21,9 @@ One common task is to inspect the contents of a variable. In Rails, you can do t ### `debug` -The `debug` helper will return a <pre>-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: +The `debug` helper will return a \<pre>-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: -```html +```html+erb <%= debug @post %> <p> <b>Title:</b> @@ -52,7 +52,7 @@ Title: Rails debugging guide Displaying an instance variable, or any other object or method, in YAML format can be achieved this way: -```html +```html+erb <%= simple_format @post.to_yaml %> <p> <b>Title:</b> @@ -82,7 +82,7 @@ Title: Rails debugging guide Another useful method for displaying object values is `inspect`, especially when working with arrays or hashes. This will print the object value as a string. For example: -```html +```html+erb <%= [1, 2, 3, 4, 5].inspect %> <p> <b>Title:</b> @@ -174,7 +174,7 @@ end Here's an example of the log generated by this method: -```bash +``` Processing PostsController#create (for 127.0.0.1 at 2008-09-08 11:52:54) [POST] Session ID: BAh7BzoMY3NyZl9pZCIlMDY5MWU1M2I1ZDRjODBlMzkyMWI1OTg2NWQyNzViZjYiCmZsYXNoSUM6J0FjdGl vbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA=--b18cd92fba90eacf8137e5f6b3b06c4d724596a4 @@ -235,7 +235,7 @@ end If you see the message in the console or logs: -```bash +``` ***** Debugger requested, but was not available: Start server with --debugger to enable ***** ``` @@ -266,7 +266,7 @@ For example: 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?) -```bash +``` (rdb:7) help ruby-debug help v0.10.2 Type 'help <command-name>' for help on a specific command @@ -279,13 +279,13 @@ condition down finish list ps save thread var continue edit frame method putl set tmate where ``` -TIP: To view the help menu for any command use `help <command-name>` in active debug mode. For example: _`help var`_ +TIP: To view the help menu for any command use `help <command-name>` in active debug mode. For example: _`help var`_ The next command to learn is one of the most useful: `list`. You can abbreviate any debugging command by supplying just enough letters to distinguish them from other commands, so you can also use `l` for the `list` command. 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 `=>`. -```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. -```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-`) -```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=` -```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. -```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. -```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: -```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). -```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: -```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: -```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: -```bash +``` (rdb:9) var local __dbg_verbose_save => false ``` You can also inspect for an object method this way: -```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. -```bash +``` (rdb:1) display @recent_comments 1: @recent_comments = ``` @@ -476,7 +476,7 @@ Now you should know where you are in the running trace and be able to print the Use `step` (abbreviated `s`) to continue running your program until the next logical stopping point and return control to the debugger. -TIP: You can also use `step<plus> n` and `step- n` to move forward or backward `n` steps respectively. +TIP: You can also use `step+ n` and `step- n` to move forward or backward `n` steps respectively. You may also use `next` 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 _n_ steps. @@ -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. -```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: -```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. -```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: -```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. -```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. -```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.. -```bash +``` (rdb:5) delete 1 (rdb:5) info breakpoints No breakpoints. @@ -596,8 +596,8 @@ To list all active catchpoints use `catch`. There are two ways to resume execution of an application that is stopped in the debugger: -* `continue` [line-specification] (or `c`): 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. -* `finish` [frame-number] (or `fin`): 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. +* `continue` [line-specification] \(or `c`): 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. +* `finish` [frame-number] \(or `fin`): 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. ### Editing @@ -666,7 +666,7 @@ $ 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`: -```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: -```bash +``` 191691 total objects Final heap size 191691 filled, 220961 free Displaying top 20 most common line/class pairs |