Class: Byebug::InfoCommand

Inherits:
Command
  • Object
show all
Includes:
Columnize
Defined in:
lib/byebug/commands/info.rb

Overview

Implements byebug “info” command.

Constant Summary collapse

Subcommands =
[
   ['args'              , 1, 'Argument variables of current stack frame'  ],
   ['breakpoints'       , 1, 'Status of user-settable breakpoints',
                             'Without argument, list info about all '   \
                             'breakpoints. With an integer argument, '  \
                             'list info on that breakpoint.'              ],
   ['catch'             , 3, 'Exceptions that can be caught in the '    \
                             'current stack frame'                        ],
   ['display'           , 2, 'Expressions to display when program stops'  ],
   ['file'              , 4, 'Info about a particular file read in',
                             'After the file name is supplied, you can' \
                             'list file attributes that you wish to '   \
                             'see. Attributes include: "all", "basic",' \
                             ' "breakpoint", "lines", "mtime", "path" ' \
                             'and "sha1".'                                ],
   ['files'             , 5, 'File names and timestamps of files read in' ],
   ['global_variables'  , 2, 'Global variables'                           ],
   ['instance_variables', 2, 'Instance variables in current stack frame'  ],
   ['line'              , 2, 'Line number and file name of current '    \
                             'position in source file'                    ],
   ['locals'            , 2, 'Local variables of the current stack frame' ],
   ['program'           , 2, 'Execution status of the program'            ],
   ['stack'             , 2, 'Backtrace of the stack'                     ],
   ['variables'         , 1, 'Local and instance variables of the '     \
                             'current stack frame'                        ]
].map do |name, min, help|
  Subcmd.new(name, min, help)
end
InfoFileSubcommands =
[
  ['all'        , 1, 'All file information available - breakpoints, '   \
                     'lines, mtime, path and sha1'                        ],
  ['basic'      , 2, 'basic information - path, number of lines'          ],
  ['breakpoints', 2, 'Show trace line numbers',
                     'These are the line number where a breakpoint '    \
                     'can be set.'                                        ],
  ['lines'      , 1, 'Show number of lines in the file'                   ],
  ['mtime'      , 1, 'Show modification time of file'                     ],
  ['path'       , 4, 'Show full file path name for file'                  ],
  ['sha1'       , 1, 'Show SHA1 hash of contents of the file'             ]
].map do |name, min, help|
  Subcmd.new(name, min, help)
end

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

command_exists?, commands, find, format_subcmd, format_subcmds, inherited, #initialize, load_commands, #match, register_setting_get, register_setting_set, register_setting_var, settings, settings_map, terminal_width

Constructor Details

This class inherits a constructor from Byebug::Command

Class Method Details

.descriptionObject



276
277
278
279
280
# File 'lib/byebug/commands/info.rb', line 276

def description
  %{info[ subcommand]

    Generic command for showing things about the program being debugged.}
end

.help(args) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/byebug/commands/info.rb', line 282

def help(args)
  return description + format_subcmds unless args and args[1]

  return format_subcmd(args[1]) unless 'file' == args[1] and args[2]

  str = subcmd.short_help + '.'
  subsubcmd = Command.find(InfoFileSubcommands, args[2])
  if subsubcmd
    str += "\nInvalid \"file\" attribute \"#{args[2]}\"."
  else
    str += "\n" + subsubcmd.short_help + '.'
  end

  return str
end

.namesObject



272
273
274
# File 'lib/byebug/commands/info.rb', line 272

def names
  %w(info)
end

Instance Method Details

#executeObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/byebug/commands/info.rb', line 70

def execute
  return print InfoCommand.help(nil) unless @match[1]

  args = @match[1].split(/[ \t]+/)
  param = args.shift
  subcmd = Command.find(Subcommands, param)
  return errmsg "Unknown info command #{param}\n" unless subcmd

  if @state.context
    send("info_#{subcmd.name}", *args)
  else
    errmsg "info_#{subcmd.name} not available without a context.\n"
  end
end

#info_args(*args) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/byebug/commands/info.rb', line 85

def info_args(*args)
  locals = @state.context.frame_locals
  args = @state.context.frame_args
  return if args == [[:rest]]

  args.map do |_, name|
    s = "#{name} = #{locals[name].inspect}"
    pad_with_dots(s)
    print "#{s}\n"
  end
end

#info_breakpoints(*args) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/byebug/commands/info.rb', line 109

def info_breakpoints(*args)
  return print "No breakpoints.\n" if Byebug.breakpoints.empty?

  brkpts = Byebug.breakpoints.sort_by{|b| b.id}
  unless args.empty?
    indices = args.map{|a| a.to_i}
    brkpts = brkpts.select{|b| indices.member?(b.id)}
    return errmsg "No breakpoints found among list given.\n" if
      brkpts.empty?
  end
  print "Num Enb What\n"
  brkpts.each { |b| info_breakpoint(b) }
end

#info_display(*args) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/byebug/commands/info.rb', line 123

def info_display(*args)
  return print "There are no auto-display expressions now.\n" unless
    @state.display.find{|d| d[0]}

  print "Auto-display expressions now in effect:\n" \
        "Num Enb Expression\n"
  n = 1
  for d in @state.display
    print "%3d: %s  %s\n" % [n, (d[0] ? 'y' : 'n'), d[1]]
    n += 1
  end
end

#info_file(*args) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/byebug/commands/info.rb', line 169

def info_file(*args)
  return info_files unless args[0]

  subcmd = Command.find(InfoFileSubcommands, args[1] || 'basic')
  return errmsg "Invalid parameter #{args[1]}\n" unless subcmd

  if %w(all basic).member?(subcmd.name)
    info_file_path(args[0])
    info_file_lines(args[0])
    if subcmd.name == 'all'
      info_file_breakpoints(args[0])
      info_file_mtime(args[0])
      info_file_sha1(args[0])
    end
  else
    print "File #{args[0]}\n" if subcmd.name != 'path'
    send("info_file_#{subcmd.name}", args[0])
  end
end

#info_files(*args) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/byebug/commands/info.rb', line 189

def info_files(*args)
  files = SCRIPT_LINES__.keys
  files.uniq.sort.each do |file|
    info_file_path(file)
    info_file_mtime(file)
  end
end

#info_global_variables(*args) ⇒ Object



257
258
259
# File 'lib/byebug/commands/info.rb', line 257

def info_global_variables(*args)
  var_global
end

#info_instance_variables(*args) ⇒ Object



197
198
199
200
# File 'lib/byebug/commands/info.rb', line 197

def info_instance_variables(*args)
  obj = bb_eval('self')
  var_list(obj.instance_variables)
end

#info_line(*args) ⇒ Object



202
203
204
# File 'lib/byebug/commands/info.rb', line 202

def info_line(*args)
  print "Line #{@state.line} of \"#{@state.file}\"\n"
end

#info_locals(*args) ⇒ Object



206
207
208
209
# File 'lib/byebug/commands/info.rb', line 206

def info_locals(*args)
  locals = @state.context.frame_locals
  print_hash(locals)
end

#info_program(*args) ⇒ Object



242
243
244
245
246
247
248
249
250
251
# File 'lib/byebug/commands/info.rb', line 242

def info_program(*args)
  if @state.context.dead?
    print "The program crashed.\n"
    print "Exception: #{Byebug.last_exception.inspect}\n" if Byebug.last_exception
    return
  end

  print "Program stopped. "
  info_stop_reason @state.context.stop_reason
end

#info_stack(*args) ⇒ Object



253
254
255
# File 'lib/byebug/commands/info.rb', line 253

def info_stack(*args)
  print_backtrace
end

#info_variables(*args) ⇒ Object



261
262
263
264
265
266
267
268
269
# File 'lib/byebug/commands/info.rb', line 261

def info_variables(*args)
  locals = @state.context.frame_locals
  locals[:self] = @state.context.frame_self(@state.frame_pos)
  print_hash(locals)

  obj = bb_eval('self')
  var_list(obj.instance_variables, obj.instance_eval{binding()})
  var_class_self
end

#regexpObject



66
67
68
# File 'lib/byebug/commands/info.rb', line 66

def regexp
  /^\s* i(?:nfo)? (?:\s+(.+))? \s*$/x
end