6. Debugger commands
(rdb:9) help
ruby-debug help v1.6.2
Type 'help <command-name>' for help on a specific command
Available commands:
backtrace
delete
enable
help
list
pry
restart
source
undisplay
break
disable
eval
info
method
ps
save
start
up
catch
display
exit
irb
next
putl
set
step
var
condition
down
finish
jump
p
quit
show
thread
where
continue
edit
frame
kill
pp
reload
skip
trace
7. Program info
(rdb:4) help info
Generic command for showing things about the program being debugged.
-List of info subcommands:
-info args -- Argument variables of current stack frame
info breakpoints -- Status of user-settable breakpoints
info catch -- Exceptions that can be caught in the current stack frame
info display -- Expressions to display when program stops
info file -- Info about a particular file read in
info files -- File names and timestamps of files read in
info global_variables -- Global variables
info instance_variables -- Instance variables of the current stack frame
info line -- Line number and file name of current position in source file
info locals -- Local variables of the current stack frame
info program -- Execution status of the program
info stack -- Backtrace of the stack
info thread -- List info about thread NUM
info threads -- information of currently-known threads
info variables -- Local and instance variables of the current stack frame
8. Listing
(rdb:1) help list
l[ist]
list forward
l[ist] -
list backward
l[ist] =
list current line
l[ist] nn-mm
list given lines
NOTE - to turn on autolist, use 'set autolist' -> .rdebugrc
9. Listing: in debug
12
@user = User.find(params[:id])
13
debugger
=> 14
15
16
@microposts = @user.microposts.paginate(page: params[:page])
end
10. Listing: l 23,25
l 23,25
[23, 25] in */sample_app_rails_4/app/controllers/users_controller.rb
23
if @user.save
24
sign_in @user
25
flash[:success] = "Welcome to the Sample App!"
11. Breakpoints
(rdb:4) help break
b[reak] file:line [if expr]
b[reak] class(.|#)method [if expr]
set breakpoint to some position, (optionally) if expr == true
(rdb:4) help delete
del[ete][ nnn...]
delete some or all breakpoints
12. Set breakpoint
(rdb:4) b ApplicationHelper#full_title
Breakpoint 1 at ApplicationHelper::full_title
(rdb:4) b 18
Breakpoint 2 file */sample_app_rails_4/app/controllers/users_controller.rb, line 18
(rdb:4) info b
Num Enb What
1 y
at ApplicationHelper:full_title
2 y
at */sample_app_rails_4/app/controllers/users_controller.rb:18
13. Navigate
(rdb:4) help step
s[tep][+-]?[ nnn]
step (into methods) once or nnn times
'+' forces to move to another line.
'-' is the opposite of '+' and disables the force_stepping setting.
(rdb:4) help next
n[ext][+-]?[ nnn]
step over once or nnn times,
'+' forces to move to another line.
'-' is the opposite of '+' and disables the force_stepping setting.
(rdb:4) help up
up[count]
move to higher frame
14. Stop at breakpoint
(rdb:4) c
Breakpoint 1 at ApplicationHelper:full_title
[-1, 8] in */sample_app_rails_4/app/helpers/application_helper.rb
1
module ApplicationHelper
2
3
=> 4
5
# Returns the full title on a per-page basis.
def full_title(page_title)
base_title = "Ruby on Rails Tutorial Sample App"
15. Display
(rdb:4) help display
disp[lay] <expression>
add expression into display expression list
disp[lay]
display expression list
(rdb:4) help undisplay
undisp[lay][ nnn]
Cancel some expressions to be displayed when program stops.
18. Conditional breakpoint
(rdb:8) b ApplicationHelper#full_title if page_title=="Alexander"
Breakpoint 4 at ApplicationHelper::full_title
(rdb:8) c
Breakpoint 1 at ApplicationHelper:full_title
19. Variables
(rdb:8) help var
v[ar] cl[ass]
show class variables of self
v[ar] co[nst] <object>
show constants of object
v[ar] g[lobal]
show global variables
v[ar] i[nstance] <object>
show instance variables of object. You may pass object id's hex as well.
v[ar] l[ocal]
show local variables
25. jazz_hands
jazz_hands is an opinionated set of consolerelated gems and a bit of glue:
pry, awesome_print, hirb, pry-rails, pry-doc, pry-git,
pry-remote, pry-debugger, pry-stack_explorer, coolline,
coderay
37. Querying logs
# cat log/development.log|grep GET|tail -n 2
Started GET "/assets/users.js?body=1" for 127.0.0.1 at 2013-11-21 23:32:01 +0200
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-11-21 23:32:01 +0200
# cat log/development.log|grep GET|wc -l
574
38. config.log_formatter
Defines the formatter of the Rails logger.
Defaults to
ActiveSupport::Logger::SimpleFormatter
production defaults to
for all modes,
Logger::Formatter.
module ActiveSupport
class Logger < ::Logger
# Simple formatter which only displays the message.
class SimpleFormatter < ::Logger::Formatter
# This method is invoked when a log event occurs
def call(severity, timestamp, progname, msg)
"#{String === msg ? msg : msg.inspect}n"
end
end
40. config.log_tags
See
ActionDispatch::Request
methods.
config.log_tags = [ :fullpath ]
[/users/1] Started GET "/users/1" for 127.0.0.1 at 2013-11-...
[/users/1]
ActiveRecord::SchemaMigration Load (0.1ms)
SELECT ...
[/assets/application.css?body=1] Started GET "/assets/application.css?body=1" for 127...
41. config.logger
Accepts a logger conforming to the interface of Log4r or the default Ruby Logger class.
Defaults to
ActiveSupport::Logger,
with auto flushing off in production mode.
44. All routes
> puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new)
Prefix Verb
URI Pattern
Controller#Action
following_user GET
/users/:id/following(.:format) users#following
followers_user GET
/users/:id/followers(.:format) users#followers
users GET
/users(.:format)
users#index
/users(.:format)
users#create
/users/new(.:format)
users#new
/users/:id/edit(.:format)
users#edit
/users/:id(.:format)
users#show
PATCH
/users/:id(.:format)
users#update
PUT
/users/:id(.:format)
users#update
POST
new_user GET
edit_user GET
user GET
DELETE /users/:id(.:format)
sessions POST
new_session GET
/sessions(.:format)
sessions#create
/sessions/new(.:format)
sessions#new
session DELETE /sessions/:id(.:format)
microposts POST
/microposts(.:format)
micropost DELETE /microposts/:id(.:format)
relationships POST
/relationships(.:format)
relationship DELETE /relationships/:id(.:format)
root GET
signup GET
users#destroy
sessions#destroy
microposts#create
microposts#destroy
relationships#create
relationships#destroy
/
static_pages#home
/signup(.:format)
users#new
45. Routes for controller
> puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, 'microposts')
Prefix Verb
microposts POST
URI Pattern
Controller#Action
/microposts(.:format)
microposts#create
micropost DELETE /microposts/:id(.:format) microposts#destroy
46. Filtering routes: GET
> puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, 'users').lines.grep(/GET/).join
following_user GET
/users/:id/following(.:format) users#following
followers_user GET
/users/:id/followers(.:format) users#followers
users GET
new_user GET
edit_user GET
user GET
signup GET
/users(.:format)
users#index
/users/new(.:format)
users#new
/users/:id/edit(.:format)
users#edit
/users/:id(.:format)
users#show
/signup(.:format)
users#new
51. Get requests
> app.get '/users/1/edit'
Started GET "/users/1/edit" for 127.0.0.1 at 2013-11-26 23:24:18 +0200
Processing by UsersController#edit as HTML
Parameters: {"id"=>"1"}
Redirected to http://localhost:3000/signin
Filter chain halted as :signed_in_user rendered or redirected
Completed 302 Found in 3ms (ActiveRecord: 0.4ms)
> app.response.body
=> "<html><body>You are being <a href="http://localhost:3000/signin">redirected</a>.</body></html>"
> app.get_via_redirect '/users/1/edit'
Started GET "/users/1/edit" for 127.0.0.1 at 2013-11-26 23:26:44 +0200
Redirected to http://localhost:3000/signin
...
Started GET "/signin" for 127.0.0.1 at 2013-11-26 23:26:44 +0200
52. Session cookies
> app.cookies
=> #<Rack::Test::CookieJar...
> app.cookies.to_hash
=> {"_sample_app_session"=>"RC9j...
app.cookies - received/sent cookies
53. Post requests: signin
> app.response.body.lines.grep /csrf-token/
=> ["<meta content="n+9uCcG2JJmgwhnNcp4s9jTwOU55RAPOdtAHWstcpKQ=" name="csrf-token" />n"]
> app.post '/sessions', :authenticity_token => 'n+9uCcG2JJmgwhnNcp4s9jTwOU55RAPOdtAHWstcpKQ=',
'session[email]' => 'asdf@asdf.com', 'session[password]' => '123456'
Started POST "/sessions" for 127.0.0.1 at 2013-11-26 23:33:01 +0200
Processing by SessionsController#create as HTML
Parameters: {"authenticity_token"=>"n+9uCcG2JJmgwhnNcp4s9jTwOU55RAPOdtAHWstcpKQ=", "session"=>{"email"
=>"asdf@asdf.com", "password"=>"[FILTERED]"}}
Redirected to http://localhost:3000/users/1/edit
Completed 302 Found in 281ms (ActiveRecord: 7.2ms)
app.post_via_redirect
54. Access to restricted resource
> app.get '/users/1/edit'
Started GET "/users/1/edit" for 127.0.0.1 at 2013-11-26 23:38:47 +0200
Processing by UsersController#edit as HTML
Completed 200 OK in 41ms (Views: 35.7ms | ActiveRecord: 0.8ms)
61. CLI tools
Making HTTP requests with curl
●
●
●
●
●
●
-s silent
-v verbose
-c save cookie
-b use cookie
-data POST data
-data-urlencode URL-encode POST data
62. Access restricted area
curl -s -v http://localhost:3000/users/1/edit > /dev/null
> GET /users/1/edit HTTP/1.1
< HTTP/1.1 302 Found
< Location: http://localhost:3000/signin
64. Sign in
curl -s -v --data "session[email]=asdf@asdf.com;session[password]=123456"
--data-urlencode "authenticity_token=/t/IoUQxKVEL+KR2/HsnxTKmnALUA99jIr/LvjlgPKs="
-b hello_cookies -c cookies
http://localhost:3000/sessions > /dev/null
> POST /sessions HTTP/1.1
< HTTP/1.1 302 Found
< Location: http://localhost:3000/users/1
65. Successful access to restricted area
curl -s -v http://localhost:3000/users/1/edit -b cookies > /dev/null
> GET /users/1/edit HTTP/1.1
< HTTP/1.1 200 OK