byebug 11.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (132) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +897 -0
  3. data/CONTRIBUTING.md +58 -0
  4. data/GUIDE.md +1806 -0
  5. data/LICENSE +23 -0
  6. data/README.md +199 -0
  7. data/exe/byebug +6 -0
  8. data/ext/byebug/breakpoint.c +517 -0
  9. data/ext/byebug/byebug.c +905 -0
  10. data/ext/byebug/byebug.h +143 -0
  11. data/ext/byebug/context.c +673 -0
  12. data/ext/byebug/extconf.rb +12 -0
  13. data/ext/byebug/locker.c +96 -0
  14. data/ext/byebug/threads.c +230 -0
  15. data/lib/byebug.rb +3 -0
  16. data/lib/byebug/attacher.rb +48 -0
  17. data/lib/byebug/breakpoint.rb +111 -0
  18. data/lib/byebug/command.rb +111 -0
  19. data/lib/byebug/command_list.rb +34 -0
  20. data/lib/byebug/commands.rb +40 -0
  21. data/lib/byebug/commands/break.rb +112 -0
  22. data/lib/byebug/commands/catch.rb +78 -0
  23. data/lib/byebug/commands/condition.rb +55 -0
  24. data/lib/byebug/commands/continue.rb +68 -0
  25. data/lib/byebug/commands/debug.rb +38 -0
  26. data/lib/byebug/commands/delete.rb +55 -0
  27. data/lib/byebug/commands/disable.rb +33 -0
  28. data/lib/byebug/commands/disable/breakpoints.rb +42 -0
  29. data/lib/byebug/commands/disable/display.rb +43 -0
  30. data/lib/byebug/commands/display.rb +66 -0
  31. data/lib/byebug/commands/down.rb +45 -0
  32. data/lib/byebug/commands/edit.rb +69 -0
  33. data/lib/byebug/commands/enable.rb +33 -0
  34. data/lib/byebug/commands/enable/breakpoints.rb +42 -0
  35. data/lib/byebug/commands/enable/display.rb +43 -0
  36. data/lib/byebug/commands/finish.rb +57 -0
  37. data/lib/byebug/commands/frame.rb +57 -0
  38. data/lib/byebug/commands/help.rb +64 -0
  39. data/lib/byebug/commands/history.rb +39 -0
  40. data/lib/byebug/commands/info.rb +37 -0
  41. data/lib/byebug/commands/info/breakpoints.rb +65 -0
  42. data/lib/byebug/commands/info/display.rb +49 -0
  43. data/lib/byebug/commands/info/file.rb +80 -0
  44. data/lib/byebug/commands/info/line.rb +35 -0
  45. data/lib/byebug/commands/info/program.rb +49 -0
  46. data/lib/byebug/commands/interrupt.rb +34 -0
  47. data/lib/byebug/commands/irb.rb +50 -0
  48. data/lib/byebug/commands/kill.rb +45 -0
  49. data/lib/byebug/commands/list.rb +159 -0
  50. data/lib/byebug/commands/method.rb +53 -0
  51. data/lib/byebug/commands/next.rb +40 -0
  52. data/lib/byebug/commands/pry.rb +41 -0
  53. data/lib/byebug/commands/quit.rb +42 -0
  54. data/lib/byebug/commands/restart.rb +64 -0
  55. data/lib/byebug/commands/save.rb +72 -0
  56. data/lib/byebug/commands/set.rb +79 -0
  57. data/lib/byebug/commands/show.rb +45 -0
  58. data/lib/byebug/commands/skip.rb +85 -0
  59. data/lib/byebug/commands/source.rb +40 -0
  60. data/lib/byebug/commands/step.rb +40 -0
  61. data/lib/byebug/commands/thread.rb +34 -0
  62. data/lib/byebug/commands/thread/current.rb +37 -0
  63. data/lib/byebug/commands/thread/list.rb +43 -0
  64. data/lib/byebug/commands/thread/resume.rb +45 -0
  65. data/lib/byebug/commands/thread/stop.rb +43 -0
  66. data/lib/byebug/commands/thread/switch.rb +46 -0
  67. data/lib/byebug/commands/tracevar.rb +54 -0
  68. data/lib/byebug/commands/undisplay.rb +51 -0
  69. data/lib/byebug/commands/untracevar.rb +36 -0
  70. data/lib/byebug/commands/up.rb +45 -0
  71. data/lib/byebug/commands/var.rb +37 -0
  72. data/lib/byebug/commands/var/all.rb +41 -0
  73. data/lib/byebug/commands/var/args.rb +39 -0
  74. data/lib/byebug/commands/var/const.rb +49 -0
  75. data/lib/byebug/commands/var/global.rb +37 -0
  76. data/lib/byebug/commands/var/instance.rb +39 -0
  77. data/lib/byebug/commands/var/local.rb +39 -0
  78. data/lib/byebug/commands/where.rb +53 -0
  79. data/lib/byebug/context.rb +157 -0
  80. data/lib/byebug/core.rb +115 -0
  81. data/lib/byebug/errors.rb +29 -0
  82. data/lib/byebug/frame.rb +185 -0
  83. data/lib/byebug/helpers/bin.rb +47 -0
  84. data/lib/byebug/helpers/eval.rb +126 -0
  85. data/lib/byebug/helpers/file.rb +63 -0
  86. data/lib/byebug/helpers/frame.rb +75 -0
  87. data/lib/byebug/helpers/parse.rb +75 -0
  88. data/lib/byebug/helpers/path.rb +40 -0
  89. data/lib/byebug/helpers/reflection.rb +19 -0
  90. data/lib/byebug/helpers/string.rb +33 -0
  91. data/lib/byebug/helpers/thread.rb +67 -0
  92. data/lib/byebug/helpers/toggle.rb +62 -0
  93. data/lib/byebug/helpers/var.rb +54 -0
  94. data/lib/byebug/history.rb +130 -0
  95. data/lib/byebug/interface.rb +146 -0
  96. data/lib/byebug/interfaces/local_interface.rb +44 -0
  97. data/lib/byebug/interfaces/remote_interface.rb +50 -0
  98. data/lib/byebug/interfaces/script_interface.rb +33 -0
  99. data/lib/byebug/interfaces/test_interface.rb +67 -0
  100. data/lib/byebug/option_setter.rb +95 -0
  101. data/lib/byebug/printers/base.rb +68 -0
  102. data/lib/byebug/printers/plain.rb +44 -0
  103. data/lib/byebug/printers/texts/base.yml +115 -0
  104. data/lib/byebug/printers/texts/plain.yml +33 -0
  105. data/lib/byebug/processors/command_processor.rb +173 -0
  106. data/lib/byebug/processors/control_processor.rb +24 -0
  107. data/lib/byebug/processors/post_mortem_processor.rb +18 -0
  108. data/lib/byebug/processors/script_processor.rb +49 -0
  109. data/lib/byebug/remote.rb +85 -0
  110. data/lib/byebug/remote/client.rb +57 -0
  111. data/lib/byebug/remote/server.rb +47 -0
  112. data/lib/byebug/runner.rb +198 -0
  113. data/lib/byebug/setting.rb +79 -0
  114. data/lib/byebug/settings/autoirb.rb +29 -0
  115. data/lib/byebug/settings/autolist.rb +29 -0
  116. data/lib/byebug/settings/autopry.rb +29 -0
  117. data/lib/byebug/settings/autosave.rb +17 -0
  118. data/lib/byebug/settings/basename.rb +16 -0
  119. data/lib/byebug/settings/callstyle.rb +20 -0
  120. data/lib/byebug/settings/fullpath.rb +16 -0
  121. data/lib/byebug/settings/histfile.rb +20 -0
  122. data/lib/byebug/settings/histsize.rb +20 -0
  123. data/lib/byebug/settings/linetrace.rb +22 -0
  124. data/lib/byebug/settings/listsize.rb +21 -0
  125. data/lib/byebug/settings/post_mortem.rb +27 -0
  126. data/lib/byebug/settings/savefile.rb +20 -0
  127. data/lib/byebug/settings/stack_on_error.rb +15 -0
  128. data/lib/byebug/settings/width.rb +20 -0
  129. data/lib/byebug/source_file_formatter.rb +71 -0
  130. data/lib/byebug/subcommands.rb +54 -0
  131. data/lib/byebug/version.rb +8 -0
  132. metadata +199 -0
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "byebug/processors/command_processor"
4
+
5
+ module Byebug
6
+ #
7
+ # Processes commands when there's not program running
8
+ #
9
+ class ControlProcessor < CommandProcessor
10
+ #
11
+ # Available commands
12
+ #
13
+ def commands
14
+ super.select(&:allow_in_control)
15
+ end
16
+
17
+ #
18
+ # Prompt shown before reading a command.
19
+ #
20
+ def prompt
21
+ "(byebug:ctrl) "
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "byebug/processors/command_processor"
4
+
5
+ module Byebug
6
+ #
7
+ # Processes commands in post_mortem mode
8
+ #
9
+ class PostMortemProcessor < CommandProcessor
10
+ def commands
11
+ super.select(&:allow_in_post_mortem)
12
+ end
13
+
14
+ def prompt
15
+ "(byebug:post_mortem) "
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "byebug/processors/command_processor"
4
+
5
+ module Byebug
6
+ #
7
+ # Processes commands from a file
8
+ #
9
+ class ScriptProcessor < CommandProcessor
10
+ #
11
+ # Available commands
12
+ #
13
+ def commands
14
+ super.select(&:allow_in_control)
15
+ end
16
+
17
+ def repl
18
+ while (input = interface.read_command(prompt))
19
+ safely do
20
+ command = command_list.match(input)
21
+ raise CommandNotFound.new(input) unless command
22
+
23
+ command.new(self, input).execute
24
+ end
25
+ end
26
+ end
27
+
28
+ def after_repl
29
+ super
30
+
31
+ interface.close
32
+ end
33
+
34
+ #
35
+ # Prompt shown before reading a command.
36
+ #
37
+ def prompt
38
+ "(byebug:ctrl) "
39
+ end
40
+
41
+ private
42
+
43
+ def without_exceptions
44
+ yield
45
+ rescue StandardError
46
+ nil
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "socket"
4
+ require "byebug/processors/control_processor"
5
+ require "byebug/remote/server"
6
+ require "byebug/remote/client"
7
+
8
+ #
9
+ # Remote debugging functionality.
10
+ #
11
+ module Byebug
12
+ # Port number used for remote debugging
13
+ PORT = 8989 unless defined?(PORT)
14
+
15
+ class << self
16
+ # If in remote mode, wait for the remote connection
17
+ attr_accessor :wait_connection
18
+
19
+ # The actual port that the server is started at
20
+ def actual_port
21
+ server.actual_port
22
+ end
23
+
24
+ # The actual port that the control server is started at
25
+ def actual_control_port
26
+ control.actual_port
27
+ end
28
+
29
+ #
30
+ # Interrupts the current thread
31
+ #
32
+ def interrupt
33
+ current_context.interrupt
34
+ end
35
+
36
+ #
37
+ # Starts the remote server main thread
38
+ #
39
+ def start_server(host = nil, port = PORT)
40
+ start_control(host, port.zero? ? 0 : port + 1)
41
+
42
+ server.start(host, port)
43
+ end
44
+
45
+ #
46
+ # Starts the remote server control thread
47
+ #
48
+ def start_control(host = nil, port = PORT + 1)
49
+ control.start(host, port)
50
+ end
51
+
52
+ #
53
+ # Connects to the remote byebug
54
+ #
55
+ def start_client(host = "localhost", port = PORT)
56
+ client.start(host, port)
57
+ end
58
+
59
+ def parse_host_and_port(host_port_spec)
60
+ location = host_port_spec.split(":")
61
+ location[1] ? [location[0], location[1].to_i] : ["localhost", location[0]]
62
+ end
63
+
64
+ private
65
+
66
+ def client
67
+ @client ||= Remote::Client.new(Context.interface)
68
+ end
69
+
70
+ def server
71
+ @server ||= Remote::Server.new(wait_connection: wait_connection) do |s|
72
+ Context.interface = RemoteInterface.new(s)
73
+ end
74
+ end
75
+
76
+ def control
77
+ @control ||= Remote::Server.new(wait_connection: false) do |s|
78
+ context = Byebug.current_context
79
+ interface = RemoteInterface.new(s)
80
+
81
+ ControlProcessor.new(context, interface).process_commands
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "socket"
4
+
5
+ module Byebug
6
+ module Remote
7
+ #
8
+ # Client for remote debugging
9
+ #
10
+ class Client
11
+ attr_reader :interface, :socket
12
+
13
+ def initialize(interface)
14
+ @interface = interface
15
+ @socket = nil
16
+ end
17
+
18
+ #
19
+ # Connects to the remote byebug
20
+ #
21
+ def start(host = "localhost", port = PORT)
22
+ connect_at(host, port)
23
+
24
+ while (line = socket.gets)
25
+ case line
26
+ when /^PROMPT (.*)$/
27
+ input = interface.read_command(Regexp.last_match[1])
28
+ break unless input
29
+
30
+ socket.puts input
31
+ when /^CONFIRM (.*)$/
32
+ input = interface.readline(Regexp.last_match[1])
33
+ break unless input
34
+
35
+ socket.puts input
36
+ else
37
+ interface.puts line
38
+ end
39
+ end
40
+
41
+ socket.close
42
+ end
43
+
44
+ def started?
45
+ !socket.nil?
46
+ end
47
+
48
+ private
49
+
50
+ def connect_at(host, port)
51
+ interface.puts "Connecting to byebug server at #{host}:#{port}..."
52
+ @socket = TCPSocket.new(host, port)
53
+ interface.puts "Connected."
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "socket"
4
+
5
+ module Byebug
6
+ module Remote
7
+ #
8
+ # Server for remote debugging
9
+ #
10
+ class Server
11
+ attr_reader :actual_port, :wait_connection
12
+
13
+ def initialize(wait_connection:, &block)
14
+ @thread = nil
15
+ @wait_connection = wait_connection
16
+ @main_loop = block
17
+ end
18
+
19
+ #
20
+ # Start the remote debugging server
21
+ #
22
+ def start(host, port)
23
+ return if @thread
24
+
25
+ if wait_connection
26
+ mutex = Mutex.new
27
+ proceed = ConditionVariable.new
28
+ end
29
+
30
+ server = TCPServer.new(host, port)
31
+ @actual_port = server.addr[1]
32
+
33
+ yield if block_given?
34
+
35
+ @thread = DebugThread.new do
36
+ while (session = server.accept)
37
+ @main_loop.call(session)
38
+
39
+ mutex.synchronize { proceed.signal } if wait_connection
40
+ end
41
+ end
42
+
43
+ mutex.synchronize { proceed.wait(mutex) } if wait_connection
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,198 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+ require "English"
5
+ require "byebug/core"
6
+ require "byebug/version"
7
+ require "byebug/helpers/bin"
8
+ require "byebug/helpers/parse"
9
+ require "byebug/helpers/string"
10
+ require "byebug/option_setter"
11
+ require "byebug/processors/control_processor"
12
+
13
+ module Byebug
14
+ #
15
+ # Responsible for starting the debugger when started from the command line.
16
+ #
17
+ class Runner
18
+ include Helpers::BinHelper
19
+ include Helpers::ParseHelper
20
+ include Helpers::StringHelper
21
+
22
+ #
23
+ # Special working modes that don't actually start the debugger.
24
+ #
25
+ attr_reader :help, :version, :remote
26
+
27
+ #
28
+ # Signals that we should exit after the debugged program is finished.
29
+ #
30
+ attr_accessor :quit
31
+
32
+ #
33
+ # Signals that we should stop before program starts
34
+ #
35
+ attr_accessor :stop
36
+
37
+ #
38
+ # Signals that we should run rc scripts before program starts
39
+ #
40
+ attr_writer :init_script
41
+
42
+ #
43
+ # @param stop [Boolean] Whether the runner should stop right before
44
+ # starting the program.
45
+ #
46
+ # @param quit [Boolean] Whether the runner should quit right after
47
+ # finishing the program.
48
+ #
49
+ def initialize(stop = true, quit = true)
50
+ @stop = stop
51
+ @quit = quit
52
+ end
53
+
54
+ def help=(text)
55
+ @help ||= text
56
+
57
+ interface.puts("#{text}\n")
58
+ end
59
+
60
+ def version=(number)
61
+ @version ||= number
62
+
63
+ interface.puts prettify <<-VERSION
64
+ Running byebug #{number}
65
+ VERSION
66
+ end
67
+
68
+ def remote=(host_and_port)
69
+ @remote ||= Byebug.parse_host_and_port(host_and_port)
70
+
71
+ Byebug.start_client(*@remote)
72
+ end
73
+
74
+ def init_script
75
+ defined?(@init_script) ? @init_script : true
76
+ end
77
+
78
+ #
79
+ # Usage banner.
80
+ #
81
+ def banner
82
+ prettify <<-BANNER
83
+ byebug #{Byebug::VERSION}
84
+
85
+ Usage: byebug [options] <script.rb> -- <script.rb parameters>
86
+ BANNER
87
+ end
88
+
89
+ #
90
+ # Starts byebug to debug a program.
91
+ #
92
+ def run
93
+ Byebug.mode = :standalone
94
+
95
+ option_parser.order!($ARGV)
96
+ return if non_script_option? || error_in_script?
97
+
98
+ $PROGRAM_NAME = program
99
+
100
+ Byebug.run_init_script if init_script
101
+
102
+ loop do
103
+ debug_program
104
+
105
+ break if quit
106
+
107
+ ControlProcessor.new(nil, interface).process_commands
108
+ end
109
+ end
110
+
111
+ def interface
112
+ @interface ||= Context.interface
113
+ end
114
+
115
+ #
116
+ # Processes options passed from the command line.
117
+ #
118
+ def option_parser
119
+ @option_parser ||= OptionParser.new(banner, 25) do |opts|
120
+ opts.banner = banner
121
+
122
+ OptionSetter.new(self, opts).setup
123
+ end
124
+ end
125
+
126
+ def program
127
+ @program ||= begin
128
+ candidate = which($ARGV.shift)
129
+
130
+ if [which("ruby"), RbConfig.ruby].include?(candidate)
131
+ which($ARGV.shift)
132
+ else
133
+ candidate
134
+ end
135
+ end
136
+ end
137
+
138
+ #
139
+ # An option that doesn't need a script specified was given
140
+ #
141
+ def non_script_option?
142
+ version || help || remote
143
+ end
144
+
145
+ #
146
+ # There is an error with the specified script
147
+ #
148
+ def error_in_script?
149
+ no_script? || non_existing_script? || invalid_script?
150
+ end
151
+
152
+ #
153
+ # No script to debug specified
154
+ #
155
+ def no_script?
156
+ return false unless $ARGV.empty?
157
+
158
+ print_error("You must specify a program to debug")
159
+ true
160
+ end
161
+
162
+ #
163
+ # Extracts debugged program from command line args.
164
+ #
165
+ def non_existing_script?
166
+ return false if program
167
+
168
+ print_error("The script doesn't exist")
169
+ true
170
+ end
171
+
172
+ #
173
+ # Checks the debugged script has correct syntax
174
+ #
175
+ def invalid_script?
176
+ return false if syntax_valid?(File.read(program))
177
+
178
+ print_error("The script has incorrect syntax")
179
+ true
180
+ end
181
+
182
+ #
183
+ # Debugs a script only if syntax checks okay.
184
+ #
185
+ def debug_program
186
+ error = Byebug.debug_load(program, stop)
187
+ puts "#{error}\n#{error.backtrace}" if error
188
+ end
189
+
190
+ #
191
+ # Prints an error message and a help string
192
+ #
193
+ def print_error(msg)
194
+ interface.errmsg(msg)
195
+ interface.puts(option_parser.help)
196
+ end
197
+ end
198
+ end