Class: Byebug::InfoCommand

Inherits:
Command
  • Object
show all
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 of the 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'],
   ['thread', 6,  'List info about thread NUM',
    'If no thread number is given, we list info for all threads. "terse"' \
    ' and "verbose" options are possible. If "terse", just give summary ' \
    'thread name information. See "help info threads" for more detail '   \
    'about this summary information. If "verbose" is appended to the end' \
    ' of the command, then the entire stack trace is given for each '     \
    'thread.'],
   ['threads', 7, 'information of currently-known threads',
    'This information includes whether the thread is the current thread ' \
    '(+), it\'s suspended ($) or it\'s ignored (!), plus the thread '     \
    'number and the top stack item. If "verbose" is given then the '      \
    'entire stack frame is shown.'],
   ['variables', 1,
    'Local and instance variables of the current stack frame']
  ].map do |name, min, short_help, long_help|
  SubcmdStruct.new(name, min, short_help, long_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, short_help, long_help|
  SubcmdStruct.new(name, min, short_help, long_help)
end

Constants inherited from Command

Command::DEF_OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

commands, #find, inherited, #initialize, load_commands, #match, method_missing, options, #print_subcmds, register_setting_get, register_setting_set, register_setting_var, settings, settings_map

Constructor Details

This class inherits a constructor from Byebug::Command

Class Method Details

.help(args) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/byebug/commands/info.rb', line 398

def help(args)
  # specific subcommand help
  if args[1]
    subcmd = find(Subcommands, args[1])
    return "Invalid \"info\" subcommand \"#{args[1]}\"." unless subcmd

    str = subcmd.short_help + '.'
    if 'file' == subcmd.name and args[2]
      subsubcmd = find(InfoFileSubcommands, args[2])
      return str += "\nInvalid \"file\" attribute \"#{args[2]}\"." \
        unless subsubcmd

      str += "\n" + subsubcmd.short_help + '.'
    else
      str += "\n" + subcmd.long_help if subcmd.long_help
    end
    return str
  end

  # general help
  s = %{
    Generic command for showing things about the program being debugged.
    --
    List of info subcommands:
    --
  }
  for subcmd in Subcommands do
    s += "info #{subcmd.name} -- #{subcmd.short_help}\n"
  end
  return s
end

.help_commandObject



394
395
396
# File 'lib/byebug/commands/info.rb', line 394

def help_command
  'info'
end

Instance Method Details

#executeObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/byebug/commands/info.rb', line 89

def execute
  return print_subcmds(Subcommands) unless @match[1]

  args = @match[1].split(/[ \t]+/)
  param = args.shift
  subcmd = find(Subcommands, param)
  if subcmd
    send("info_#{subcmd.name}", *args)
  else
    errmsg "Unknown info command #{param}\n"
  end
end

#info_args(*args) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/byebug/commands/info.rb', line 102

def info_args(*args)
  unless @state.context
    print "No frame selected.\n"
    return
  end
  locals = @state.context.frame_locals(@state.frame_pos)
  args = @state.context.frame_args(@state.frame_pos)
  args.each do |name|
    s = "#{name} = #{locals[name].inspect}"
    pad_with_dots(s)
    print "#{s}\n"
  end
end

#info_breakpoints(*args) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/byebug/commands/info.rb', line 116

def info_breakpoints(*args)
  return print "\"info breakpoints\" not available here.\n" unless
    @state.context

  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 do |b|
    print "%-3d %-3s at %s:%s%s\n", b.id,
                                    b.enabled? ? 'y' : 'n',
                                    b.source,
                                    b.pos,
                                    b.expr.nil? ? '' : " if #{b.expr}"
    hits = b.hit_count
    if hits > 0
      s = (hits > 1) ? 's' : ''
      print "\tbreakpoint already hit #{hits} time#{s}\n"
    end
  end
end

#info_display(*args) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/byebug/commands/info.rb', line 144

def info_display(*args)
  unless @state.context
    print "info display not available here.\n"
    return
  end
  if @state.display.find{|d| d[0]}
    print "Auto-display expressions now in effect:\n"
    print "Num Enb Expression\n"
    n = 1
    for d in @state.display
      print "%3d: %s  %s\n", n, (d[0] ? 'y' : 'n'), d[1] if
        d[0] != nil
      n += 1
    end
  else
    print "There are no auto-display expressions now.\n"
  end
end

#info_file(*args) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/byebug/commands/info.rb', line 192

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

  param =  args[1] ? args[1] : 'basic'

  subcmd = find(InfoFileSubcommands, param)
  return errmsg "Invalid parameter #{param}\n" unless subcmd

  unless LineCache::cached?(file)
    unless LineCache::cached_script?(file)
      return print "File #{file} is not cached\n"
    end
    LineCache::cache(file, Command.settings[:reload_source_on_change])
  end

  print "File #{file}"
  info_file_path(file) if %w(all basic path).member?(subcmd.name)
  print "\n"

  info_file_lines(file) if %w(all basic lines).member?(subcmd.name)
  info_file_breakpoints(file) if %w(all breakpoints).member?(subcmd.name)
  info_file_mtime(file) if %w(all mtime).member?(subcmd.name)
  info_file_sha1(file) if %w(all sha1).member?(subcmd.name)
end

#info_file_breakpoints(file) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/byebug/commands/info.rb', line 175

def info_file_breakpoints(file)
  breakpoints = LineCache.trace_line_numbers(file)
  if breakpoints
    print "\tbreakpoint line numbers:\n"
    print columnize(breakpoints.to_a.sort, self.class.settings[:width])
  end
end

#info_file_lines(file) ⇒ Object



170
171
172
173
# File 'lib/byebug/commands/info.rb', line 170

def info_file_lines(file)
  lines = LineCache.size(file)
  print "\t %d lines\n", lines if lines
end

#info_file_mtime(file) ⇒ Object



183
184
185
186
# File 'lib/byebug/commands/info.rb', line 183

def info_file_mtime(file)
  stat = LineCache.stat(file)
  print "\t%s\n", stat.mtime if stat
end

#info_file_path(file) ⇒ Object



163
164
165
166
167
168
# File 'lib/byebug/commands/info.rb', line 163

def info_file_path(file)
  path = LineCache.path(file)
  if path != file
    print " - #{path}"
  end
end

#info_file_sha1(file) ⇒ Object



188
189
190
# File 'lib/byebug/commands/info.rb', line 188

def info_file_sha1(file)
  print "\t%s\n", LineCache.sha1(file)
end

#info_files(*args) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/byebug/commands/info.rb', line 218

def info_files(*args)
  files = LineCache::cached_files
  files += SCRIPT_LINES__.keys unless 'stat' == args[0]
  files.uniq.sort.each do |file|
    stat = LineCache::stat(file)
    path = LineCache::path(file)
    print "File %s", file
    if path and path != file
      print " - %s\n", path
    else
      print "\n"
    end
    print "\t%s\n", stat.mtime if stat
  end
end

#info_global_variables(*args) ⇒ Object



357
358
359
360
361
362
363
# File 'lib/byebug/commands/info.rb', line 357

def info_global_variables(*args)
  unless @state.context
    errmsg "info global_variables not available here.\n"
    return
  end
  var_global
end

#info_instance_variables(*args) ⇒ Object



234
235
236
237
238
239
240
241
# File 'lib/byebug/commands/info.rb', line 234

def info_instance_variables(*args)
  unless @state.context
    print "info instance_variables not available here.\n"
    return
  end
  obj = debug_eval('self')
  var_list(obj.instance_variables)
end

#info_line(*args) ⇒ Object



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

def info_line(*args)
  unless @state.context
    errmsg "info line not available here.\n"
    return
  end
  print "Line %d of \"%s\"\n",  @state.line, @state.file
end

#info_locals(*args) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/byebug/commands/info.rb', line 251

def info_locals(*args)
  unless @state.context
    errmsg "info line not available here.\n"
    return
  end
  locals = @state.context.frame_locals(@state.frame_pos)
  locals.keys.sort.each do |name|
    ### FIXME: make a common routine
    begin
      s = "#{name} = #{locals[name].inspect}"
    rescue
      begin
      s = "#{name} = #{locals[name].to_s}"
      rescue
        s = "*Error in evaluation*"
      end
    end
    pad_with_dots(s)
    print "#{s}\n"
  end
end

#info_program(*args) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/byebug/commands/info.rb', line 273

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

  print "Program stopped. "
  case @state.context.stop_reason
  when :step
    print "It stopped after stepping, next'ing or initial start.\n"
  when :breakpoint
    print("It stopped at a breakpoint.\n")
  when :catchpoint
    print("It stopped at a catchpoint.\n")
  else
    print "unknown reason: %s\n" % @state.context.stop_reason.to_s
  end
end

#info_stack(*args) ⇒ Object



298
299
300
301
302
303
304
# File 'lib/byebug/commands/info.rb', line 298

def info_stack(*args)
  if not @state.context
    errmsg "info stack not available here.\n"
    return
  end
  print_backtrace
end

#info_thread(*args) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/byebug/commands/info.rb', line 339

def info_thread(*args)
  unless args[0]
    info_threads(args[0])
    return
  end
  ok, verbose = info_thread_preamble(args[1])
  return unless ok
  c = parse_thread_num("info thread" , args[0])
  return unless c
  display_context(c, !verbose)
  if verbose and not c.ignored?
    (0...c.stack_size).each do |idx|
      print "\t"
      print_frame(idx, false, c)
    end
  end
end

#info_threads(*args) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/byebug/commands/info.rb', line 325

def info_threads(*args)
  ok, verbose = info_thread_preamble(args[0])
  return unless ok
  threads = Byebug.contexts.sort_by{|c| c.thnum}.each do |c|
    display_context(c, !verbose)
    if verbose and not c.ignored?
      (0...c.stack_size).each do |idx|
        print "\t"
        print_frame(idx, false, c)
      end
    end
  end
end

#info_variables(*args) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/byebug/commands/info.rb', line 365

def info_variables(*args)
  if not @state.context
    errmsg "info variables not available here.\n"
    return
  end
  obj = debug_eval('self')
  locals = @state.context.frame_locals(@state.frame_pos)
  locals[:self] = @state.context.frame_self(@state.frame_pos)
  locals.keys.sort.each do |name|
    next if name =~ /^__dbg_/ # skip byebug pollution
    ### FIXME: make a common routine
    begin
      s = "#{name} = #{locals[name].inspect}"
    rescue
      begin
        s = "#{name} = #{locals[name].to_s}"
      rescue
        s = "#{name} = *Error in evaluation*"
      end
    end
    pad_with_dots(s)
    s.gsub!('%', '%%')  # protect against printf format strings
    print "#{s}\n"
  end
  var_list(obj.instance_variables, obj.instance_eval{binding()})
  var_class_self
end

#regexpObject

InfoThreadSubcommands =

  [
   ['terse', 1, 'summary information'],
   ['verbose', 1, 'summary information and stack frame info'],
  ].map do |name, min, short_help, long_help|
  SubcmdStruct.new(name, min, short_help, long_help)
end unless defined?(InfoThreadSubcommands)


85
86
87
# File 'lib/byebug/commands/info.rb', line 85

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