rbx-trepanning 0.0.5-universal-rubinius-1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (266) hide show
  1. data/ChangeLog +2002 -0
  2. data/LICENSE +25 -0
  3. data/NEWS +31 -0
  4. data/README.textile +34 -0
  5. data/Rakefile +165 -0
  6. data/THANKS +14 -0
  7. data/app/breakpoint.rb +219 -0
  8. data/app/breakpoint.rbc +3551 -0
  9. data/app/brkptmgr.rb +140 -0
  10. data/app/brkptmgr.rbc +2903 -0
  11. data/app/client.rb +61 -0
  12. data/app/client.rbc +1225 -0
  13. data/app/complete.rb +52 -0
  14. data/app/complete.rbc +1288 -0
  15. data/app/default.rb +71 -0
  16. data/app/default.rbc +1132 -0
  17. data/app/display.rb +148 -0
  18. data/app/display.rbc +2578 -0
  19. data/app/frame.rb +98 -0
  20. data/app/frame.rbc +1808 -0
  21. data/app/irb.rb +111 -0
  22. data/app/irb.rbc +2094 -0
  23. data/app/iseq.rb +117 -0
  24. data/app/iseq.rbc +2192 -0
  25. data/app/llvm.rbc +2478 -0
  26. data/app/method.rb +173 -0
  27. data/app/method.rbc +2492 -0
  28. data/app/method_name.rbc +2467 -0
  29. data/app/mock.rb +13 -0
  30. data/app/mock.rbc +398 -0
  31. data/app/options.rb +137 -0
  32. data/app/options.rbc +2898 -0
  33. data/app/rbx-llvm.rb +165 -0
  34. data/app/rbx-llvm.rbc +2478 -0
  35. data/app/run.rb +86 -0
  36. data/app/run.rbc +1244 -0
  37. data/app/util.rb +49 -0
  38. data/app/util.rbc +1146 -0
  39. data/app/validate.rb +30 -0
  40. data/app/validate.rbc +676 -0
  41. data/bin/trepan.compiled.rbc +1043 -0
  42. data/bin/trepanx +69 -0
  43. data/bin/trepanx.compiled.rbc +1049 -0
  44. data/data/irbrc +41 -0
  45. data/data/irbrc.compiled.rbc +640 -0
  46. data/interface/base_intf.rb +105 -0
  47. data/interface/base_intf.rbc +1899 -0
  48. data/interface/client.rb +80 -0
  49. data/interface/client.rbc +1072 -0
  50. data/interface/comcodes.rb +20 -0
  51. data/interface/comcodes.rbc +385 -0
  52. data/interface/script.rb +104 -0
  53. data/interface/script.rbc +1642 -0
  54. data/interface/server.rb +143 -0
  55. data/interface/server.rbc +2213 -0
  56. data/interface/user.rb +150 -0
  57. data/interface/user.rbc +2867 -0
  58. data/io/base_io.rb +148 -0
  59. data/io/base_io.rbc +2111 -0
  60. data/io/input.rb +135 -0
  61. data/io/input.rbc +2528 -0
  62. data/io/null_output.rb +42 -0
  63. data/io/null_output.rbc +730 -0
  64. data/io/string_array.rb +156 -0
  65. data/io/string_array.rbc +2466 -0
  66. data/io/tcpclient.rb +129 -0
  67. data/io/tcpclient.rbc +2419 -0
  68. data/io/tcpfns.rb +33 -0
  69. data/io/tcpfns.rbc +694 -0
  70. data/io/tcpserver.rb +141 -0
  71. data/io/tcpserver.rbc +2638 -0
  72. data/lib/trepanning.rb +474 -0
  73. data/lib/trepanning.rbc +7705 -0
  74. data/lib/trepanning2.rb +441 -0
  75. data/processor/breakpoint.rb +164 -0
  76. data/processor/command/alias.rb +55 -0
  77. data/processor/command/backtrace.rb +76 -0
  78. data/processor/command/base/cmd.rb +173 -0
  79. data/processor/command/base/subcmd.rb +229 -0
  80. data/processor/command/base/submgr.rb +182 -0
  81. data/processor/command/base/subsubcmd.rb +103 -0
  82. data/processor/command/base/subsubmgr.rb +195 -0
  83. data/processor/command/break.rb +100 -0
  84. data/processor/command/complete.rb +37 -0
  85. data/processor/command/continue.rb +91 -0
  86. data/processor/command/delete.rb +30 -0
  87. data/processor/command/directory.rb +51 -0
  88. data/processor/command/disassemble.rb +145 -0
  89. data/processor/command/display.rb +82 -0
  90. data/processor/command/down.rb +54 -0
  91. data/processor/command/eval.rb +51 -0
  92. data/processor/command/exit.rb +62 -0
  93. data/processor/command/finish.rb +80 -0
  94. data/processor/command/frame.rb +89 -0
  95. data/processor/command/help.rb +251 -0
  96. data/processor/command/info.rb +28 -0
  97. data/processor/command/info_subcmd/breakpoints.rb +73 -0
  98. data/processor/command/info_subcmd/files.rb +209 -0
  99. data/processor/command/info_subcmd/line.rb +86 -0
  100. data/processor/command/info_subcmd/method.rb +71 -0
  101. data/processor/command/info_subcmd/program.rb +49 -0
  102. data/processor/command/info_subcmd/ruby.rb +62 -0
  103. data/processor/command/info_subcmd/variables.rb +48 -0
  104. data/processor/command/irb.rb +128 -0
  105. data/processor/command/kill.rb +70 -0
  106. data/processor/command/list.rb +300 -0
  107. data/processor/command/macro.rb +76 -0
  108. data/processor/command/next.rb +66 -0
  109. data/processor/command/nexti.rb +59 -0
  110. data/processor/command/pr.rb +38 -0
  111. data/processor/command/ps.rb +40 -0
  112. data/processor/command/restart.rb +60 -0
  113. data/processor/command/server.rb +72 -0
  114. data/processor/command/set.rb +47 -0
  115. data/processor/command/set_subcmd/auto.rb +27 -0
  116. data/processor/command/set_subcmd/auto_subcmd/dis.rb +33 -0
  117. data/processor/command/set_subcmd/auto_subcmd/eval.rb +54 -0
  118. data/processor/command/set_subcmd/auto_subcmd/irb.rb +34 -0
  119. data/processor/command/set_subcmd/auto_subcmd/list.rb +34 -0
  120. data/processor/command/set_subcmd/basename.rb +25 -0
  121. data/processor/command/set_subcmd/confirm.rb +24 -0
  122. data/processor/command/set_subcmd/debug.rb +26 -0
  123. data/processor/command/set_subcmd/debug_subcmd/dbgr.rb +36 -0
  124. data/processor/command/set_subcmd/debug_subcmd/skip.rb +23 -0
  125. data/processor/command/set_subcmd/debug_subcmd/step.rb +23 -0
  126. data/processor/command/set_subcmd/different.rb +59 -0
  127. data/processor/command/set_subcmd/hidelevel.rb +62 -0
  128. data/processor/command/set_subcmd/highlight.rb +33 -0
  129. data/processor/command/set_subcmd/kernelstep.rb +60 -0
  130. data/processor/command/set_subcmd/max.rb +26 -0
  131. data/processor/command/set_subcmd/max_subcmd/list.rb +49 -0
  132. data/processor/command/set_subcmd/max_subcmd/stack.rb +50 -0
  133. data/processor/command/set_subcmd/max_subcmd/string.rb +76 -0
  134. data/processor/command/set_subcmd/max_subcmd/width.rb +49 -0
  135. data/processor/command/set_subcmd/substitute.rb +24 -0
  136. data/processor/command/set_subcmd/substitute_subcmd/path.rb +56 -0
  137. data/processor/command/set_subcmd/trace.rb +36 -0
  138. data/processor/command/set_subcmd/trace_subcmd/print.rb +57 -0
  139. data/processor/command/show.rb +38 -0
  140. data/processor/command/show_subcmd/alias.rb +42 -0
  141. data/processor/command/show_subcmd/args.rb +25 -0
  142. data/processor/command/show_subcmd/auto.rb +28 -0
  143. data/processor/command/show_subcmd/auto_subcmd/dis.rb +37 -0
  144. data/processor/command/show_subcmd/auto_subcmd/eval.rb +28 -0
  145. data/processor/command/show_subcmd/auto_subcmd/irb.rb +23 -0
  146. data/processor/command/show_subcmd/auto_subcmd/list.rb +22 -0
  147. data/processor/command/show_subcmd/basename.rb +20 -0
  148. data/processor/command/show_subcmd/confirm.rb +18 -0
  149. data/processor/command/show_subcmd/debug.rb +26 -0
  150. data/processor/command/show_subcmd/debug_subcmd/dbgr.rb +21 -0
  151. data/processor/command/show_subcmd/debug_subcmd/skip.rb +22 -0
  152. data/processor/command/show_subcmd/debug_subcmd/step.rb +22 -0
  153. data/processor/command/show_subcmd/different.rb +26 -0
  154. data/processor/command/show_subcmd/hidelevel.rb +41 -0
  155. data/processor/command/show_subcmd/highlight.rb +24 -0
  156. data/processor/command/show_subcmd/kernelstep.rb +34 -0
  157. data/processor/command/show_subcmd/max.rb +27 -0
  158. data/processor/command/show_subcmd/max_subcmd/list.rb +38 -0
  159. data/processor/command/show_subcmd/max_subcmd/stack.rb +36 -0
  160. data/processor/command/show_subcmd/max_subcmd/string.rb +42 -0
  161. data/processor/command/show_subcmd/max_subcmd/width.rb +37 -0
  162. data/processor/command/show_subcmd/trace.rb +27 -0
  163. data/processor/command/show_subcmd/trace_subcmd/print.rb +38 -0
  164. data/processor/command/show_subcmd/version.rb +23 -0
  165. data/processor/command/source.rb +126 -0
  166. data/processor/command/step.rb +84 -0
  167. data/processor/command/tbreak.rb +19 -0
  168. data/processor/command/unalias.rb +44 -0
  169. data/processor/command/undisplay.rb +59 -0
  170. data/processor/command/up.rb +86 -0
  171. data/processor/default.rb +58 -0
  172. data/processor/disassemble.rb +46 -0
  173. data/processor/display.rb +18 -0
  174. data/processor/eval.rb +96 -0
  175. data/processor/frame.rb +211 -0
  176. data/processor/help.rb +72 -0
  177. data/processor/hook.rb +133 -0
  178. data/processor/load_cmds.rb +185 -0
  179. data/processor/location.rb +137 -0
  180. data/processor/main.rb +412 -0
  181. data/processor/mock.rb +144 -0
  182. data/processor/msg.rb +43 -0
  183. data/processor/running.rb +234 -0
  184. data/processor/stepping.rb +114 -0
  185. data/processor/subcmd.rb +161 -0
  186. data/processor/validate.rb +355 -0
  187. data/sample/list-terminal-colors.rb +139 -0
  188. data/sample/list-terminal-colors.rbc +2318 -0
  189. data/sample/rocky-dot-trepanxrc +14 -0
  190. data/sample/rocky-trepanx-colors.rb +47 -0
  191. data/sample/rocky-trepanx-colors.rbc +530 -0
  192. data/test/data/enable.right +36 -0
  193. data/test/data/fname-with-blank.cmd +6 -0
  194. data/test/data/fname-with-blank.right +4 -0
  195. data/test/data/inline-call.cmd +6 -0
  196. data/test/data/inline-call.right +13 -0
  197. data/test/data/quit-Xdebug.right +3 -0
  198. data/test/data/quit.cmd +5 -0
  199. data/test/data/quit.right +2 -0
  200. data/test/data/quit2.cmd +6 -0
  201. data/test/data/quit2.right +3 -0
  202. data/test/data/step-bug.cmd +11 -0
  203. data/test/data/step-bug.right +3 -0
  204. data/test/example/fname with blank.rb +1 -0
  205. data/test/example/gcd-server.rb +22 -0
  206. data/test/example/gcd.rb +19 -0
  207. data/test/example/inline-call.rb +23 -0
  208. data/test/example/null.rb +1 -0
  209. data/test/example/step-bug.rb +14 -0
  210. data/test/example/thread1.rb +3 -0
  211. data/test/functional/fn_helper.rb +112 -0
  212. data/test/functional/test-break-name.rb +52 -0
  213. data/test/functional/test-break.rb +51 -0
  214. data/test/functional/test-finish.rb +70 -0
  215. data/test/functional/test-fn_helper.rb +43 -0
  216. data/test/functional/test-list.rb +55 -0
  217. data/test/functional/test-next-bug.rb +49 -0
  218. data/test/functional/test-next.rb +101 -0
  219. data/test/functional/test-step.rb +272 -0
  220. data/test/functional/test-step2.rb +35 -0
  221. data/test/functional/test-tbreak.rb +41 -0
  222. data/test/integration/file-diff.rb +89 -0
  223. data/test/integration/helper.rb +78 -0
  224. data/test/integration/skip-test-step-bug.rb +17 -0
  225. data/test/integration/test-fname-with-blank.rb +16 -0
  226. data/test/integration/test-inline-call.rb +20 -0
  227. data/test/integration/test-quit.rb +47 -0
  228. data/test/unit/cmd-helper.rb +50 -0
  229. data/test/unit/mock-helper.rb +10 -0
  230. data/test/unit/test-app-brkpt.rb +29 -0
  231. data/test/unit/test-app-brkptmgr.rb +51 -0
  232. data/test/unit/test-app-iseq.rb +64 -0
  233. data/test/unit/test-app-method.rb +54 -0
  234. data/test/unit/test-app-options.rb +82 -0
  235. data/test/unit/test-app-run.rb +16 -0
  236. data/test/unit/test-app-util.rb +27 -0
  237. data/test/unit/test-app-validate.rb +18 -0
  238. data/test/unit/test-base-cmd.rb +47 -0
  239. data/test/unit/test-base-subcmd.rb +61 -0
  240. data/test/unit/test-base-submgr.rb +24 -0
  241. data/test/unit/test-bin-trepanx.rb +48 -0
  242. data/test/unit/test-cmd-alias.rb +48 -0
  243. data/test/unit/test-cmd-break.rb +22 -0
  244. data/test/unit/test-cmd-exit.rb +27 -0
  245. data/test/unit/test-cmd-finish.rb +27 -0
  246. data/test/unit/test-cmd-help.rb +104 -0
  247. data/test/unit/test-cmd-kill.rb +47 -0
  248. data/test/unit/test-cmd-source.rb +34 -0
  249. data/test/unit/test-cmd-step.rb +29 -0
  250. data/test/unit/test-completion.rb +38 -0
  251. data/test/unit/test-intf-user.rb +46 -0
  252. data/test/unit/test-io-input.rb +27 -0
  253. data/test/unit/test-io-tcp.rb +33 -0
  254. data/test/unit/test-io-tcpclient.rb +54 -0
  255. data/test/unit/test-io-tcpfns.rb +17 -0
  256. data/test/unit/test-io-tcpserver.rb +50 -0
  257. data/test/unit/test-proc-eval.rb +36 -0
  258. data/test/unit/test-proc-frame.rb +79 -0
  259. data/test/unit/test-proc-help.rb +16 -0
  260. data/test/unit/test-proc-hook.rb +30 -0
  261. data/test/unit/test-proc-load_cmds.rb +50 -0
  262. data/test/unit/test-proc-location.rb +53 -0
  263. data/test/unit/test-proc-main.rb +99 -0
  264. data/test/unit/test-proc-validate.rb +91 -0
  265. data/test/unit/test-subcmd-help.rb +47 -0
  266. metadata +398 -0
@@ -0,0 +1,441 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright (C) 2011 Rocky Bernstein <rockyb@rubyforge.net>
3
+ require 'readline'
4
+ require 'compiler/iseq'
5
+
6
+ require 'rubygems'; require 'require_relative'
7
+ require_relative '../app/frame'
8
+ require_relative '../app/util' # get_dollar_0
9
+ require_relative '../processor/main'
10
+ require_relative '../app/breakpoint'
11
+ require_relative '../app/default' # default debugger settings
12
+ require_relative '../app/breakpoint'
13
+ require_relative '../interface/user' # user interface (includes I/O)
14
+ require_relative '../interface/client' # client interface (remote debugging)
15
+ require_relative '../interface/server' # server interface (remote debugging)
16
+ require_relative '../io/null_output'
17
+
18
+ #
19
+ # The Rubinius Trepan debugger.
20
+ #
21
+ # This debugger is wired into the debugging APIs provided by Rubinius.
22
+ #
23
+
24
+ class Trepan
25
+ attr_accessor :breakpoint # Breakpoint. The current breakpoint we are
26
+ # stopped at or nil if none.
27
+ attr_accessor :intf # Array. The way the outside world
28
+ # interfaces with us. An array, so that
29
+ # interfaces with us. An array, so that
30
+ # interfaces can be stacked.
31
+ attr_accessor :restart_argv # How to restart us, empty or nil.
32
+ # Note: restart_argv is typically C's
33
+ # **argv, not Ruby's ARGV. So
34
+ # restart_argv[0] is $0.
35
+ attr_reader :settings # Hash[:symbol] of things you can configure
36
+ attr_reader :deferred_breakpoints
37
+ attr_reader :processor
38
+
39
+ # Create a new debugger object. The debugger starts up a thread
40
+ # which is where the command line interface executes from. Other
41
+ # threads that you wish to debug are told that their debugging
42
+ # thread is the debugger thread. This is how the debugger is handed
43
+ # control of execution.
44
+ #
45
+ def initialize(settings={})
46
+ @breakpoint = nil
47
+ @settings = DEFAULT_SETTINGS.merge(settings)
48
+ @input = @settings[:input] || STDIN
49
+ @output = @settings[:output] || STDOUT
50
+
51
+ cmdproc_settings = {:highlight => @settings[:highlight]}
52
+
53
+ @processor = CmdProcessor.new(self, cmdproc_settings)
54
+ completion_proc = Proc.new do |str|
55
+ completed_ary = @processor.complete(Readline.line_buffer, true)
56
+ Readline.line_buffer = '' unless completed_ary.empty?
57
+ completed_ary
58
+ end
59
+
60
+ @intf =
61
+ if @settings[:server]
62
+ opts = Trepan::ServerInterface::DEFAULT_INIT_CONNECTION_OPTS.dup
63
+ opts[:port] = @settings[:port] if @settings[:port]
64
+ opts[:host] = @settings[:host] if @settings[:host]
65
+ puts("starting debugger in out-of-process mode port at " +
66
+ "#{opts[:host]}:#{opts[:port]}")
67
+ [Trepan::ServerInterface.new(nil, nil, opts)]
68
+ elsif @settings[:client]
69
+ opts = Trepan::ClientInterface::DEFAULT_INIT_CONNECTION_OPTS.dup
70
+ opts[:port] = @settings[:port] if @settings[:port]
71
+ opts[:host] = @settings[:host] if @settings[:host]
72
+ opts[:complete] = completion_proc
73
+ [Trepan::ClientInterface.new(nil, nil, nil, nil, opts)]
74
+ else
75
+ opts = {:complete => completion_proc}
76
+ [Trepan::UserInterface.new(@input, @output, opts)]
77
+ end
78
+
79
+ process_cmdfile_setting(@settings)
80
+ if @settings[:initial_dir]
81
+ Dir.chdir(@settings[:initial_dir])
82
+ else
83
+ @settings[:initial_dir] = Dir.pwd
84
+ end
85
+ @initial_dir = @settings[:initial_dir]
86
+ @restart_argv = @settings[:restart_argv]
87
+
88
+ ## FIXME: Delete these and use the ones in processor/default instead.
89
+ @variables = {
90
+ :show_bytecode => false,
91
+ }
92
+
93
+ @history_path = File.expand_path("~/.trepanx")
94
+
95
+ if File.exists?(@history_path)
96
+ File.readlines(@history_path).each_with_index do |line, i|
97
+ break if i > 50 # FIXME: remove constant 50
98
+ Readline::HISTORY << line.strip
99
+ end
100
+ @history_io = File.new(@history_path, "a")
101
+ else
102
+ @history_io = File.new(@history_path, "w")
103
+ end
104
+
105
+ @history_io.sync = true
106
+
107
+ @processor.dbgr = self
108
+ @deferred_breakpoints = []
109
+ @thread = nil
110
+ @frames = []
111
+
112
+ unless @settings[:client]
113
+ ## FIXME: put in fn
114
+ ## m = Rubinius::Loader.method(:debugger).executable.inspect
115
+ meth = Rubinius::VM.backtrace(0)[0].method
116
+ @processor.ignore_methods[meth] = 'next'
117
+ @processor.ignore_methods[method(:debugger)] = 'step'
118
+
119
+ @loaded_hook = proc { |file|
120
+ check_deferred_breakpoints
121
+ }
122
+
123
+ @added_hook = proc { |mod, name, exec|
124
+ check_deferred_breakpoints
125
+ }
126
+
127
+ # Use a few Rubinius-specific hooks to trigger checking
128
+ # for deferred breakpoints.
129
+
130
+ Rubinius::CodeLoader.loaded_hook.add @loaded_hook
131
+ Rubinius.add_method_hook.add @added_hook
132
+
133
+ end
134
+
135
+ # Run user debugger command startup files.
136
+ add_startup_files unless @settings[:nx]
137
+ add_command_file(@settings[:restore_profile]) if
138
+ @settings[:restore_profile] && File.readable?(@settings[:restore_profile])
139
+ end
140
+
141
+ ## HACK to skip over loader code. Until I find something better...
142
+ def skip_loader
143
+ cmds =
144
+ if @settings[:skip_loader] == :Xdebug
145
+ ['continue Rubinius::CodeLoader#load_script',
146
+ 'continue 67',
147
+ # 'set kernelstep off', # eventually would like 'on'
148
+ 'step', 'set hidelevel -1'
149
+ ]
150
+ else
151
+ ['next', 'next',
152
+ # 'set kernelstep off', # eventually would like 'on'
153
+ 'set hidelevel -1',
154
+ 'step', ]
155
+ end
156
+
157
+ input = Trepan::StringArrayInput.open(cmds)
158
+ startup = Trepan::ScriptInterface.new('startup',
159
+ Trepan::OutputNull.new(nil),
160
+ :input => input)
161
+ @intf << startup
162
+ end
163
+
164
+
165
+ attr_reader :variables, :current_frame, :breakpoints
166
+ attr_reader :vm_locations, :history_io, :debugee_thread
167
+
168
+ def self.global(settings={})
169
+ @global ||= new(settings)
170
+ end
171
+
172
+ def self.start(settings={})
173
+ settings = {:immediate => false, :offset => 1}.merge(settings)
174
+ global(settings).start(settings)
175
+ end
176
+
177
+ # This is simplest API point. This starts up the debugger in the caller
178
+ # of this method to begin debugging.
179
+ #
180
+ def self.here(settings={})
181
+ global(settings).start(:offset => 1)
182
+ end
183
+
184
+ # Startup the debugger, skipping back +offset+ frames. This lets you start
185
+ # the debugger straight into callers method.
186
+ #
187
+ def start(settings = {:immediate => false})
188
+ @settings = @settings.merge(settings)
189
+ skip_loader if @settings[:skip_loader]
190
+ spinup_thread
191
+ @debugee_thread = @thread
192
+ if @settings[:hide_level]
193
+ @processor.hidelevels[@thread] = @settings[:hide_level]
194
+ end
195
+
196
+ process_cmdfile_setting(settings)
197
+
198
+ # Feed info to the debugger thread!
199
+ locs = Rubinius::VM.backtrace(@settings[:offset] + 1, true)
200
+
201
+ method = Rubinius::CompiledMethod.of_sender
202
+
203
+ event = settings[:immediate] ? 'debugger-call' : 'start'
204
+ bp = Breakpoint.new('<start>', method, 0, 0, 0, {:event => event} )
205
+ channel = Rubinius::Channel.new
206
+
207
+ @local_channel.send Rubinius::Tuple[bp, Thread.current, channel, locs]
208
+
209
+ # wait for the debugger to release us
210
+ channel.receive
211
+
212
+ Thread.current.set_debugger_thread @thread
213
+ self
214
+ end
215
+ # ruby-debug compatibility
216
+ alias debugger start
217
+
218
+ def stop(settings = {})
219
+ # Nothing for now...
220
+ end
221
+
222
+ def add_command_file(cmdfile, opts={}, stderr=$stderr)
223
+ unless File.readable?(cmdfile)
224
+ if File.exists?(cmdfile)
225
+ stderr.puts "Command file '#{cmdfile}' is not readable."
226
+ return
227
+ else
228
+ stderr.puts "Command file '#{cmdfile}' does not exist."
229
+ return
230
+ end
231
+ end
232
+ @intf << Trepan::ScriptInterface.new(cmdfile, @output, opts)
233
+ end
234
+
235
+ def add_startup_files()
236
+ seen = {}
237
+ cwd_initfile = File.join('.', Trepan::CMD_INITFILE_BASE)
238
+ [cwd_initfile, Trepan::CMD_INITFILE].each do |initfile|
239
+ full_initfile_path = File.expand_path(initfile)
240
+ next if seen[full_initfile_path]
241
+ add_command_file(full_initfile_path) if File.readable?(full_initfile_path)
242
+ seen[full_initfile_path] = true
243
+ end
244
+ end
245
+
246
+ def process_cmdfile_setting(settings)
247
+ settings[:cmdfiles].each do |item|
248
+ cmdfile, opts =
249
+ if item.kind_of?(Array)
250
+ item
251
+ else
252
+ [item, {}]
253
+ end
254
+ add_command_file(cmdfile, opts)
255
+ end if settings.member?(:cmdfiles)
256
+ end
257
+
258
+ # Stop and wait for a debuggee thread to send us info about
259
+ # stopping at a breakpoint.
260
+ #
261
+ def listen(step_into=false)
262
+ @breakpoint = nil
263
+ while true
264
+ if @channel
265
+ if step_into
266
+ @channel << :step
267
+ else
268
+ @channel << true
269
+ end
270
+ end
271
+
272
+ # Wait for someone to stop
273
+ @breakpoint, @debugee_thread, @channel, @vm_locations =
274
+ @local_channel.receive
275
+
276
+ # Uncache all frames since we stopped at a new place
277
+ @frames = []
278
+
279
+ set_frame(0)
280
+
281
+ if @breakpoint
282
+ # Some breakpoints are frame specific. Check for this. hit!
283
+ # also removes the breakpoint if it was temporary and hit.
284
+ break if @breakpoint.hit!(@vm_locations.first.variables)
285
+ else
286
+ @processor.step_bp.remove! if @processor.step_bp
287
+ break
288
+ end
289
+ end
290
+
291
+ event =
292
+ if @breakpoint
293
+ @breakpoint.event || 'brkpt'
294
+ else
295
+ # Evan assures me that the only way the breakpoint can be nil
296
+ # is if we are stepping and enter a function.
297
+ 'step-call'
298
+ end
299
+ @processor.instance_variable_set('@event', event)
300
+
301
+ if @variables[:show_bytecode]
302
+ decode_one
303
+ end
304
+
305
+ end
306
+
307
+ def frame(num)
308
+ @frames[num] ||= Frame.new(self, num, @vm_locations[num])
309
+ end
310
+
311
+ def set_frame(num)
312
+ @current_frame = frame(num)
313
+ end
314
+
315
+ def each_frame(start=0)
316
+ start = start.number if start.kind_of?(Frame)
317
+
318
+ start.upto(@vm_locations.size-1) do |idx|
319
+ yield frame(idx)
320
+ end
321
+ end
322
+
323
+ def add_deferred_breakpoint(klass_name, which, name, line)
324
+ dbp = Trepanning::DeferredBreakpoint.new(self, @current_frame, klass_name, which, name,
325
+ line, @deferred_breakpoints)
326
+ @deferred_breakpoints << dbp
327
+ # @processor.brkpts << dbp
328
+ end
329
+
330
+ def check_deferred_breakpoints
331
+ @deferred_breakpoints.delete_if do |bp|
332
+ bp.resolve!
333
+ end
334
+ end
335
+
336
+ def send_between(exec, start, fin)
337
+ ss = Rubinius::InstructionSet.opcodes_map[:send_stack]
338
+ sm = Rubinius::InstructionSet.opcodes_map[:send_method]
339
+ sb = Rubinius::InstructionSet.opcodes_map[:send_stack_with_block]
340
+
341
+ iseq = exec.iseq
342
+
343
+ fin = iseq.size if fin < 0
344
+
345
+ i = start
346
+ while i < fin
347
+ op = iseq[i]
348
+ case op
349
+ when ss, sm, sb
350
+ return exec.literals[iseq[i + 1]]
351
+ else
352
+ op = Rubinius::InstructionSet[op]
353
+ i += (op.arg_count + 1)
354
+ end
355
+ end
356
+
357
+ return nil
358
+ end
359
+
360
+ def show_code(line=@current_frame.line)
361
+ path = @current_frame.method.active_path
362
+ str = @processor.line_at(path, line)
363
+ unless str.nil?
364
+ # if @variables[:highlight]
365
+ # fin = @current_frame.method.first_ip_on_line(line + 1)
366
+ # name = send_between(@current_frame.method, @current_frame.ip, fin)
367
+
368
+ # if name
369
+ # str = str.gsub name.to_s, "\033[0;4m#{name}\033[0m"
370
+ # end
371
+ # end
372
+ # info "#{line}: #{str}"
373
+ else
374
+ show_bytecode(line)
375
+ end
376
+ end
377
+
378
+ def decode_one
379
+ ip = @current_frame.next_ip
380
+
381
+ meth = @current_frame.method
382
+ decoder = Rubinius::InstructionDecoder.new(meth.iseq)
383
+ partial = decoder.decode_between(ip, ip+1)
384
+
385
+ partial.each do |ins|
386
+ op = ins.shift
387
+
388
+ ins.each_index do |i|
389
+ case op.args[i]
390
+ when :literal
391
+ ins[i] = meth.literals[ins[i]].inspect
392
+ when :local
393
+ if meth.local_names
394
+ ins[i] = meth.local_names[ins[i]]
395
+ end
396
+ end
397
+ end
398
+
399
+ puts "=> ip #{ip} = #{op.opcode} #{ins.join(', ')}"
400
+ end
401
+ end
402
+
403
+ def spinup_thread
404
+ return if @thread
405
+
406
+ @local_channel = Rubinius::Channel.new
407
+
408
+ @thread = Thread.new do
409
+ begin
410
+ listen
411
+ rescue Exception => e
412
+ e.render("Listening")
413
+ break
414
+ end
415
+
416
+ @processor.process_commands
417
+
418
+ end
419
+
420
+ @thread.setup_control!(@local_channel)
421
+ end
422
+
423
+ private :spinup_thread
424
+
425
+ end
426
+
427
+ module Kernel
428
+ # A simpler way of calling Trepan.start
429
+ def debugger(settings = {})
430
+ settings = {:immediate => false, :offset => 2}.merge(settings)
431
+ Trepan.start(settings)
432
+ end
433
+ alias breakpoint debugger unless respond_to?(:breakpoint)
434
+ end
435
+
436
+ if __FILE__ == $0
437
+ if ARGV.size > 0
438
+ debugger
439
+ x = 1
440
+ end
441
+ end
@@ -0,0 +1,164 @@
1
+ # Copyright (C) 2010 Rocky Bernstein <rockyb@rubyforge.net>
2
+ require 'rubygems'; require 'require_relative'
3
+ require_relative '../app/brkptmgr'
4
+ class Trepan
5
+
6
+ class CmdProcessor
7
+
8
+ attr_reader :brkpts # BreakpointManager.
9
+
10
+ attr_reader :brkpt # Breakpoint. If we are stopped at a
11
+ # breakpoint this is the one we
12
+ # found. (There may be other
13
+ # breakpoints that would have caused a stop
14
+ # as well; this is just one of them).
15
+ # If no breakpoint stop this is nil.
16
+
17
+ def breakpoint_initialize
18
+ @brkpts = BreakpointMgr.new
19
+ @brkpt = nil
20
+ end
21
+
22
+ def breakpoint?
23
+ @brkpt = @dbgr.breakpoint
24
+ return !!@brkpt && %w(tbrkpt brkpt).member?(@brkpt.event)
25
+ end
26
+
27
+ def breakpoint_find(bpnum, show_errmsg = true)
28
+ if 0 == @brkpts.size
29
+ errmsg('No breakpoints set.') if show_errmsg
30
+ return nil
31
+ elsif bpnum > brkpts.size || bpnum < 1
32
+ errmsg('Breakpoint number %d is out of range 1..%d' %
33
+ [bpnum, brkpts.size]) if show_errmsg
34
+ return nil
35
+ end
36
+ bp = @brkpts[bpnum]
37
+ unless bp
38
+ errmsg "Unknown breakpoint '#{bpnum}'" if show_errmsg
39
+ return nil
40
+ end
41
+ bp
42
+ end
43
+
44
+ def set_breakpoint_method(descriptor, meth, line=nil, ip=nil,
45
+ opts={:event => 'brkpt',:temp => false})
46
+ cm =
47
+ if meth.kind_of?(Method) || meth.kind_of?(UnboundMethod)
48
+ meth.executable
49
+ else
50
+ meth
51
+ end
52
+
53
+ unless cm.kind_of?(Rubinius::CompiledMethod)
54
+ errmsg "Unsupported method type: #{cm.class}"
55
+ return nil
56
+ end
57
+
58
+ if line
59
+ ip = cm.first_ip_on_line(line)
60
+
61
+ unless ip
62
+ errmsg "Unknown line '#{line}' in method '#{cm.name}'"
63
+ return nil
64
+ end
65
+ elsif !ip
66
+ line = cm.first_line
67
+ ip = 0
68
+ end
69
+
70
+ # def lines without code will have value -1.
71
+ ip = 0 if -1 == ip
72
+
73
+ bp = @brkpts.add(descriptor, cm, ip, line, @brkpts.max+1, opts)
74
+ bp.activate
75
+ msg("Set %sbreakpoint #{bp.id}: #{meth.name}() at #{bp.location}" %
76
+ (opts[:temp] ? 'temporary ' : ''))
77
+ return bp
78
+ end
79
+
80
+ # MRI 1.9.2 code
81
+ # def breakpoint_find(bpnum, show_errmsg = true)
82
+ # if 0 == @brkpts.size
83
+ # errmsg('No breakpoints set.') if show_errmsg
84
+ # return nil
85
+ # elsif bpnum > @brkpts.max || bpnum < 1
86
+ # errmsg('Breakpoint number %d is out of range 1..%d' %
87
+ # [bpnum, @brkpts.max]) if show_errmsg
88
+ # return nil
89
+ # end
90
+ # bp = @brkpts[bpnum]
91
+ # if bp
92
+ # return bp
93
+ # else
94
+ # errmsg('Breakpoint number %d previously deleted.' %
95
+ # bpnum) if show_errmsg
96
+ # return nil
97
+ # end
98
+ # end
99
+
100
+ # # Does whatever needs to be done to set a breakpoint
101
+ # def breakpoint_line(line_number, iseq, temp=false)
102
+ # # FIXME: handle breakpoint conditions.
103
+ # iseq = iseq.child_iseqs.detect do |iseq|
104
+ # iseq.lineoffsets.keys.member?(line_number)
105
+ # end
106
+ # offset =
107
+ # if iseq
108
+ # # FIXME
109
+ # iseq.line2offsets(line_number)[1] || iseq.line2offsets(line_number)[0]
110
+ # else
111
+ # nil
112
+ # end
113
+ # unless offset
114
+ # place = "in #{iseq.source_container.join(' ')} " if iseq
115
+ # errmsg("No line #{line_number} found #{place}for breakpoint.")
116
+ # return nil
117
+ # end
118
+ # @brkpts.add(iseq, offset, :temp => temp)
119
+ # end
120
+
121
+ # def breakpoint_offset(offset, iseq, temp=false)
122
+ # # FIXME: handle breakpoint conditions.
123
+ # unless iseq.offsetlines.keys.member?(offset)
124
+ # errmsg("Offset #{offset} not found in #{iseq.name} for breakpoint.")
125
+ # return nil
126
+ # end
127
+ # @brkpts.add(iseq, offset, :temp => temp, :type => 'offset')
128
+ # end
129
+
130
+ # Delete a breakpoint given its breakpoint number.
131
+ # FIXME: use do_enable
132
+ def delete_breakpoint_by_number(bpnum, do_enable=true)
133
+ bp = breakpoint_find(bpnum)
134
+ return false unless bp
135
+ delete_breakpoint(bp)
136
+ end
137
+
138
+ def delete_breakpoint(bp)
139
+ @brkpts.delete_by_brkpt(bp)
140
+ return true
141
+ end
142
+
143
+ # MRI 1.9.2 code
144
+ # # Enable or disable a breakpoint given its breakpoint number.
145
+ # def en_disable_breakpoint_by_number(bpnum, do_enable=true)
146
+ # bp = breakpoint_find(bpnum)
147
+ # return false unless bp
148
+
149
+ # enable_disable = do_enable ? 'en' : 'dis'
150
+ # if bp.enabled? == do_enable
151
+ # errmsg('Breakpoint %d previously %sabled.' %
152
+ # [bpnum, enable_disable])
153
+ # return false
154
+ # end
155
+ # bp.enabled = do_enable
156
+ # return true
157
+ # end
158
+ end
159
+ end
160
+
161
+ if __FILE__ == $0
162
+ cmdproc = Trepan::CmdProcessor.new
163
+ cmdproc.breakpoint_initialize
164
+ end