Class: Byebug::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/byebug/runner.rb

Overview

Responsible for starting the debugger when started from the command line.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stop = true, quit = true) ⇒ Runner

starting the program.

finishing the program.

Parameters:

  • stop (Boolean) (defaults to: true)

    Whether the runner should stop right before

  • quit (Boolean) (defaults to: true)

    Whether the runner should quit right after


22
23
24
# File 'lib/byebug/runner.rb', line 22

def initialize(stop = true, quit = true)
  @stop, @quit = stop, quit
end

Instance Attribute Details

#helpObject

Special working modes that don't actually start the debugger.


13
14
15
# File 'lib/byebug/runner.rb', line 13

def help
  @help
end

#remoteObject

Special working modes that don't actually start the debugger.


13
14
15
# File 'lib/byebug/runner.rb', line 13

def remote
  @remote
end

#versionObject

Special working modes that don't actually start the debugger.


13
14
15
# File 'lib/byebug/runner.rb', line 13

def version
  @version
end

Instance Method Details

Usage banner.


29
30
31
32
33
34
35
36
37
# File 'lib/byebug/runner.rb', line 29

def banner
  <<-EOB.gsub(/^ {8}/, '')

      byebug #{Byebug::VERSION}

      Usage: byebug [options] <script.rb> -- <script.rb parameters>

  EOB
end

#check_syntax(program_name) ⇒ Object

Exits and outputs error message if syntax of the given program is invalid.


52
53
54
55
56
57
58
# File 'lib/byebug/runner.rb', line 52

def check_syntax(program_name)
  output = `ruby -c "#{program_name}" 2>&1`
  return unless $CHILD_STATUS.exitstatus != 0

  Byebug.errmsg(output)
  exit($CHILD_STATUS.exitstatus)
end

#debug_programObject

Debugs a script only if syntax checks okay.


42
43
44
45
46
47
# File 'lib/byebug/runner.rb', line 42

def debug_program
  check_syntax($PROGRAM_NAME)

  status = Byebug.debug_load($PROGRAM_NAME, @stop)
  Byebug.puts "#{status}\n#{status.backtrace}" if status
end

#prepare_optionsObject


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
# File 'lib/byebug/runner.rb', line 93

def prepare_options
  OptionParser.new(banner, 25) do |opts|
    opts.banner = banner

    opts.on '-d', '--debug', 'Set $DEBUG=true' do
      $DEBUG = true
    end

    opts.on('-I', '--include list', 'Add to paths to $LOAD_PATH') do |list|
      $LOAD_PATH.push(list.split(':')).flatten!
    end

    opts.on '-m', '--[no-]post-mortem', 'Use post-mortem mode' do |v|
      Setting[:post_mortem] = v
    end

    opts.on '-q', '--[no-]quit', 'Quit when script finishes' do |v|
      @quit = v
    end

    opts.on '-x', '--[no-]rc', 'Run byebug initialization file' do |v|
      Byebug.run_init_script if v
    end

    opts.on '-s', '--[no-]stop', 'Stop when script is loaded' do |v|
      @stop = v
    end

    opts.on '-r', '--require file', 'Require library before script' do |lib|
      require lib
    end

    opts.on '-R', '--remote [host:]port', 'Remote debug [host:]port' do |p|
      self.remote = Byebug.parse_host_and_port(p)
    end

    opts.on '-t', '--[no-]trace', 'Turn on line tracing' do |v|
      Setting[:linetrace] = v
    end

    opts.on '-v', '--version', 'Print program version' do
      self.version = VERSION
    end

    opts.on('-h', '--help', 'Display this message') do
      self.help = opts.help
    end
  end
end

#runObject

Starts byebug to debug a program


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/byebug/runner.rb', line 63

def run
  prepare_options.order!($ARGV)

  if version
    Byebug.puts("\n  Running byebug #{version}\n")
    return
  end

  if help
    Byebug.puts("#{help}\n")
    return
  end

  if remote
    Byebug.start_client(*remote)
    return
  end

  Byebug.setup_cmd_line_args

  loop do
    debug_program

    break if @quit

    processor = Byebug::ControlCommandProcessor.new
    processor.process_commands
  end
end