rbx-trepanning 0.2.0-universal-rubinius-2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (312) hide show
  1. data/.gitignore +7 -0
  2. data/ChangeLog +2967 -0
  3. data/LICENSE +25 -0
  4. data/Makefile +13 -0
  5. data/NEWS +105 -0
  6. data/README.textile +34 -0
  7. data/Rakefile +244 -0
  8. data/THANKS +14 -0
  9. data/app/.gitignore +2 -0
  10. data/app/breakpoint.rb +242 -0
  11. data/app/brkptmgr.rb +153 -0
  12. data/app/client.rb +71 -0
  13. data/app/cmd_parse.kpeg +242 -0
  14. data/app/cmd_parse.rb +209 -0
  15. data/app/cmd_parser.rb +2083 -0
  16. data/app/complete.rb +79 -0
  17. data/app/condition.rb +22 -0
  18. data/app/default.rb +71 -0
  19. data/app/display.rb +186 -0
  20. data/app/eventbuffer.rb +147 -0
  21. data/app/file.rb +24 -0
  22. data/app/frame.rb +120 -0
  23. data/app/irb.rb +113 -0
  24. data/app/iseq.rb +188 -0
  25. data/app/method.rb +178 -0
  26. data/app/mock.rb +13 -0
  27. data/app/options.rb +154 -0
  28. data/app/rbx-llvm.rb +163 -0
  29. data/app/run.rb +92 -0
  30. data/app/util.rb +99 -0
  31. data/app/validate.rb +30 -0
  32. data/bin/.gitignore +2 -0
  33. data/bin/trepanx +69 -0
  34. data/data/.gitignore +2 -0
  35. data/data/irbrc +41 -0
  36. data/interface/.gitignore +2 -0
  37. data/interface/client.rb +84 -0
  38. data/interface/comcodes.rb +20 -0
  39. data/interface/script.rb +112 -0
  40. data/interface/server.rb +147 -0
  41. data/interface/user.rb +158 -0
  42. data/interface.rb +109 -0
  43. data/io/.gitignore +3 -0
  44. data/io/input.rb +151 -0
  45. data/io/null_output.rb +46 -0
  46. data/io/string_array.rb +155 -0
  47. data/io/tcpclient.rb +129 -0
  48. data/io/tcpfns.rb +33 -0
  49. data/io/tcpserver.rb +141 -0
  50. data/io.rb +148 -0
  51. data/lib/.gitignore +2 -0
  52. data/lib/trepanning.rb +467 -0
  53. data/processor/.gitignore +3 -0
  54. data/processor/Makefile +7 -0
  55. data/processor/breakpoint.rb +167 -0
  56. data/processor/command/.gitignore +2 -0
  57. data/processor/command/alias.rb +65 -0
  58. data/processor/command/backtrace.rb +68 -0
  59. data/processor/command/base/.gitignore +2 -0
  60. data/processor/command/base/subcmd.rb +226 -0
  61. data/processor/command/base/submgr.rb +185 -0
  62. data/processor/command/base/subsubcmd.rb +125 -0
  63. data/processor/command/base/subsubmgr.rb +196 -0
  64. data/processor/command/break.rb +78 -0
  65. data/processor/command/complete.rb +39 -0
  66. data/processor/command/condition.rb +64 -0
  67. data/processor/command/continue.rb +61 -0
  68. data/processor/command/delete.rb +44 -0
  69. data/processor/command/directory.rb +51 -0
  70. data/processor/command/disable.rb +71 -0
  71. data/processor/command/disassemble.rb +180 -0
  72. data/processor/command/display.rb +84 -0
  73. data/processor/command/down.rb +54 -0
  74. data/processor/command/edit.rb +74 -0
  75. data/processor/command/enable.rb +43 -0
  76. data/processor/command/eval.rb +93 -0
  77. data/processor/command/exit.rb +83 -0
  78. data/processor/command/finish.rb +80 -0
  79. data/processor/command/frame.rb +93 -0
  80. data/processor/command/help/.gitignore +1 -0
  81. data/processor/command/help/README +10 -0
  82. data/processor/command/help/command.txt +58 -0
  83. data/processor/command/help/examples.txt +16 -0
  84. data/processor/command/help/filename.txt +40 -0
  85. data/processor/command/help/location.txt +37 -0
  86. data/processor/command/help/suffixes.txt +17 -0
  87. data/processor/command/help.rb +228 -0
  88. data/processor/command/info.rb +30 -0
  89. data/processor/command/info_subcmd/.gitignore +3 -0
  90. data/processor/command/info_subcmd/breakpoints.rb +103 -0
  91. data/processor/command/info_subcmd/files.rb +219 -0
  92. data/processor/command/info_subcmd/frame.rb +68 -0
  93. data/processor/command/info_subcmd/line.rb +75 -0
  94. data/processor/command/info_subcmd/locals.rb +22 -0
  95. data/processor/command/info_subcmd/macro.rb +62 -0
  96. data/processor/command/info_subcmd/method.rb +71 -0
  97. data/processor/command/info_subcmd/program.rb +51 -0
  98. data/processor/command/info_subcmd/ruby.rb +64 -0
  99. data/processor/command/info_subcmd/source.rb +75 -0
  100. data/processor/command/info_subcmd/stack.rb +25 -0
  101. data/processor/command/info_subcmd/variables.rb +35 -0
  102. data/processor/command/info_subcmd/variables_subcmd/.gitignore +2 -0
  103. data/processor/command/info_subcmd/variables_subcmd/class.rb +42 -0
  104. data/processor/command/info_subcmd/variables_subcmd/constant.rb +42 -0
  105. data/processor/command/info_subcmd/variables_subcmd/globals.rb +69 -0
  106. data/processor/command/info_subcmd/variables_subcmd/instance.rb +42 -0
  107. data/processor/command/info_subcmd/variables_subcmd/locals.rb +80 -0
  108. data/processor/command/kill.rb +79 -0
  109. data/processor/command/list.rb +234 -0
  110. data/processor/command/macro.rb +86 -0
  111. data/processor/command/next.rb +67 -0
  112. data/processor/command/nexti.rb +59 -0
  113. data/processor/command/parsetree.rb +51 -0
  114. data/processor/command/pr.rb +37 -0
  115. data/processor/command/ps.rb +40 -0
  116. data/processor/command/restart.rb +60 -0
  117. data/processor/command/save.rb +58 -0
  118. data/processor/command/server.rb +72 -0
  119. data/processor/command/set.rb +47 -0
  120. data/processor/command/set_subcmd/.gitignore +2 -0
  121. data/processor/command/set_subcmd/abbrev.rb +25 -0
  122. data/processor/command/set_subcmd/auto.rb +33 -0
  123. data/processor/command/set_subcmd/auto_subcmd/.gitignore +2 -0
  124. data/processor/command/set_subcmd/auto_subcmd/dis.rb +33 -0
  125. data/processor/command/set_subcmd/auto_subcmd/eval.rb +53 -0
  126. data/processor/command/set_subcmd/auto_subcmd/irb.rb +33 -0
  127. data/processor/command/set_subcmd/auto_subcmd/list.rb +33 -0
  128. data/processor/command/set_subcmd/basename.rb +25 -0
  129. data/processor/command/set_subcmd/confirm.rb +24 -0
  130. data/processor/command/set_subcmd/debug.rb +26 -0
  131. data/processor/command/set_subcmd/debug_subcmd/.gitignore +2 -0
  132. data/processor/command/set_subcmd/debug_subcmd/dbgr.rb +36 -0
  133. data/processor/command/set_subcmd/debug_subcmd/skip.rb +23 -0
  134. data/processor/command/set_subcmd/debug_subcmd/step.rb +23 -0
  135. data/processor/command/set_subcmd/different.rb +61 -0
  136. data/processor/command/set_subcmd/hidelevel.rb +62 -0
  137. data/processor/command/set_subcmd/highlight.rb +39 -0
  138. data/processor/command/set_subcmd/kernelstep.rb +60 -0
  139. data/processor/command/set_subcmd/max.rb +26 -0
  140. data/processor/command/set_subcmd/max_subcmd/.gitignore +2 -0
  141. data/processor/command/set_subcmd/max_subcmd/list.rb +49 -0
  142. data/processor/command/set_subcmd/max_subcmd/stack.rb +50 -0
  143. data/processor/command/set_subcmd/max_subcmd/string.rb +76 -0
  144. data/processor/command/set_subcmd/max_subcmd/width.rb +49 -0
  145. data/processor/command/set_subcmd/reload.rb +42 -0
  146. data/processor/command/set_subcmd/substitute.rb +24 -0
  147. data/processor/command/set_subcmd/substitute_subcmd/.gitignore +3 -0
  148. data/processor/command/set_subcmd/substitute_subcmd/path.rb +56 -0
  149. data/processor/command/set_subcmd/timer.rb +58 -0
  150. data/processor/command/set_subcmd/trace.rb +37 -0
  151. data/processor/command/set_subcmd/trace_subcmd/.gitignore +2 -0
  152. data/processor/command/set_subcmd/trace_subcmd/buffer.rb +42 -0
  153. data/processor/command/set_subcmd/trace_subcmd/print.rb +41 -0
  154. data/processor/command/shell.rb +131 -0
  155. data/processor/command/show.rb +39 -0
  156. data/processor/command/show_subcmd/.gitignore +3 -0
  157. data/processor/command/show_subcmd/abbrev.rb +20 -0
  158. data/processor/command/show_subcmd/aliases.rb +46 -0
  159. data/processor/command/show_subcmd/args.rb +25 -0
  160. data/processor/command/show_subcmd/auto.rb +28 -0
  161. data/processor/command/show_subcmd/auto_subcmd/.gitignore +3 -0
  162. data/processor/command/show_subcmd/auto_subcmd/dis.rb +37 -0
  163. data/processor/command/show_subcmd/auto_subcmd/eval.rb +24 -0
  164. data/processor/command/show_subcmd/auto_subcmd/irb.rb +23 -0
  165. data/processor/command/show_subcmd/auto_subcmd/list.rb +22 -0
  166. data/processor/command/show_subcmd/basename.rb +20 -0
  167. data/processor/command/show_subcmd/confirm.rb +18 -0
  168. data/processor/command/show_subcmd/debug.rb +26 -0
  169. data/processor/command/show_subcmd/debug_subcmd/.gitignore +3 -0
  170. data/processor/command/show_subcmd/debug_subcmd/dbgr.rb +21 -0
  171. data/processor/command/show_subcmd/debug_subcmd/skip.rb +22 -0
  172. data/processor/command/show_subcmd/debug_subcmd/step.rb +22 -0
  173. data/processor/command/show_subcmd/different.rb +26 -0
  174. data/processor/command/show_subcmd/directories.rb +22 -0
  175. data/processor/command/show_subcmd/hidelevel.rb +41 -0
  176. data/processor/command/show_subcmd/highlight.rb +25 -0
  177. data/processor/command/show_subcmd/kernelstep.rb +34 -0
  178. data/processor/command/show_subcmd/max.rb +27 -0
  179. data/processor/command/show_subcmd/max_subcmd/.gitignore +2 -0
  180. data/processor/command/show_subcmd/max_subcmd/list.rb +38 -0
  181. data/processor/command/show_subcmd/max_subcmd/stack.rb +36 -0
  182. data/processor/command/show_subcmd/max_subcmd/string.rb +42 -0
  183. data/processor/command/show_subcmd/max_subcmd/width.rb +37 -0
  184. data/processor/command/show_subcmd/reload.rb +18 -0
  185. data/processor/command/show_subcmd/timer.rb +18 -0
  186. data/processor/command/show_subcmd/trace.rb +27 -0
  187. data/processor/command/show_subcmd/trace_subcmd/.gitignore +2 -0
  188. data/processor/command/show_subcmd/trace_subcmd/buffer.rb +64 -0
  189. data/processor/command/show_subcmd/trace_subcmd/print.rb +23 -0
  190. data/processor/command/show_subcmd/version.rb +23 -0
  191. data/processor/command/source.rb +133 -0
  192. data/processor/command/step.rb +97 -0
  193. data/processor/command/tbreak.rb +20 -0
  194. data/processor/command/unalias.rb +49 -0
  195. data/processor/command/undisplay.rb +63 -0
  196. data/processor/command/up.rb +89 -0
  197. data/processor/command.rb +173 -0
  198. data/processor/default.rb +64 -0
  199. data/processor/disassemble.rb +59 -0
  200. data/processor/display.rb +53 -0
  201. data/processor/eval.rb +97 -0
  202. data/processor/eventbuf.rb +101 -0
  203. data/processor/frame.rb +265 -0
  204. data/processor/help.rb +94 -0
  205. data/processor/hook.rb +134 -0
  206. data/processor/list.rb +123 -0
  207. data/processor/load_cmds.rb +253 -0
  208. data/processor/location.rb +228 -0
  209. data/processor/mock.rb +138 -0
  210. data/processor/msg.rb +74 -0
  211. data/processor/running.rb +244 -0
  212. data/processor/stepping.rb +135 -0
  213. data/processor/subcmd.rb +136 -0
  214. data/processor/validate.rb +379 -0
  215. data/processor/virtual.rb +33 -0
  216. data/processor.rb +404 -0
  217. data/rbx-trepanning.gemspec +39 -0
  218. data/sample/.gitignore +2 -0
  219. data/sample/list-terminal-colors.rb +139 -0
  220. data/sample/rocky-dot-trepanxrc +14 -0
  221. data/sample/rocky-trepanx-colors.rb +46 -0
  222. data/test/data/.gitignore +1 -0
  223. data/test/data/enable.right +36 -0
  224. data/test/data/fname-with-blank.cmd +6 -0
  225. data/test/data/fname-with-blank.right +6 -0
  226. data/test/data/inline-call.cmd +6 -0
  227. data/test/data/inline-call.right +14 -0
  228. data/test/data/quit-Xdebug.right +3 -0
  229. data/test/data/quit.cmd +5 -0
  230. data/test/data/quit.right +3 -0
  231. data/test/data/quit2.cmd +6 -0
  232. data/test/data/quit2.right +3 -0
  233. data/test/example/.gitignore +2 -0
  234. data/test/example/debugger-stop.rb +16 -0
  235. data/test/example/factorial.rb +10 -0
  236. data/test/example/fname with blank.rb +1 -0
  237. data/test/example/gcd-server.rb +22 -0
  238. data/test/example/gcd.rb +19 -0
  239. data/test/example/goto2goto.rb +11 -0
  240. data/test/example/inline-call.rb +23 -0
  241. data/test/example/null.rb +1 -0
  242. data/test/example/thread1.rb +3 -0
  243. data/test/functional/.gitignore +3 -0
  244. data/test/functional/fn_helper.rb +112 -0
  245. data/test/functional/test-break-name.rb +52 -0
  246. data/test/functional/test-break.rb +85 -0
  247. data/test/functional/test-eval.rb +115 -0
  248. data/test/functional/test-finish.rb +70 -0
  249. data/test/functional/test-fn_helper.rb +43 -0
  250. data/test/functional/test-list.rb +56 -0
  251. data/test/functional/test-next-bug.rb +49 -0
  252. data/test/functional/test-next.rb +101 -0
  253. data/test/functional/test-recursive-bt.rb +94 -0
  254. data/test/functional/test-step.rb +272 -0
  255. data/test/functional/test-step2.rb +35 -0
  256. data/test/functional/test-tbreak.rb +41 -0
  257. data/test/integration/.gitignore +3 -0
  258. data/test/integration/file-diff.rb +89 -0
  259. data/test/integration/helper.rb +81 -0
  260. data/test/integration/test-fname-with-blank.rb +16 -0
  261. data/test/integration/test-inline-call.rb +20 -0
  262. data/test/integration/test-quit.rb +47 -0
  263. data/test/unit/.gitignore +3 -0
  264. data/test/unit/cmd-helper.rb +50 -0
  265. data/test/unit/mock-helper.rb +10 -0
  266. data/test/unit/test-app-brkpt.rb +31 -0
  267. data/test/unit/test-app-brkptmgr.rb +51 -0
  268. data/test/unit/test-app-cmd_parse.rb +97 -0
  269. data/test/unit/test-app-cmd_parser.rb +23 -0
  270. data/test/unit/test-app-complete.rb +39 -0
  271. data/test/unit/test-app-condition.rb +18 -0
  272. data/test/unit/test-app-display.rb +22 -0
  273. data/test/unit/test-app-iseq.rb +64 -0
  274. data/test/unit/test-app-method.rb +54 -0
  275. data/test/unit/test-app-options.rb +94 -0
  276. data/test/unit/test-app-run.rb +14 -0
  277. data/test/unit/test-app-util.rb +44 -0
  278. data/test/unit/test-app-validate.rb +18 -0
  279. data/test/unit/test-base-cmd.rb +45 -0
  280. data/test/unit/test-base-subcmd.rb +57 -0
  281. data/test/unit/test-base-submgr.rb +24 -0
  282. data/test/unit/test-base-subsubcmd.rb +17 -0
  283. data/test/unit/test-bin-trepanx.rb +48 -0
  284. data/test/unit/test-cmd-alias.rb +48 -0
  285. data/test/unit/test-cmd-break.rb +66 -0
  286. data/test/unit/test-cmd-edit.rb +34 -0
  287. data/test/unit/test-cmd-exit.rb +27 -0
  288. data/test/unit/test-cmd-finish.rb +31 -0
  289. data/test/unit/test-cmd-help.rb +104 -0
  290. data/test/unit/test-cmd-kill.rb +54 -0
  291. data/test/unit/test-cmd-parse_list_cmd.rb +36 -0
  292. data/test/unit/test-cmd-source.rb +34 -0
  293. data/test/unit/test-cmd-step.rb +29 -0
  294. data/test/unit/test-command.rb +45 -0
  295. data/test/unit/test-completion.rb +48 -0
  296. data/test/unit/test-intf-user.rb +46 -0
  297. data/test/unit/test-io-input.rb +27 -0
  298. data/test/unit/test-io-tcp.rb +33 -0
  299. data/test/unit/test-io-tcpclient.rb +54 -0
  300. data/test/unit/test-io-tcpfns.rb +17 -0
  301. data/test/unit/test-io-tcpserver.rb +50 -0
  302. data/test/unit/test-proc-eval.rb +35 -0
  303. data/test/unit/test-proc-frame.rb +81 -0
  304. data/test/unit/test-proc-help.rb +16 -0
  305. data/test/unit/test-proc-hook.rb +30 -0
  306. data/test/unit/test-proc-list.rb +55 -0
  307. data/test/unit/test-proc-load_cmds.rb +51 -0
  308. data/test/unit/test-proc-location.rb +67 -0
  309. data/test/unit/test-proc-main.rb +95 -0
  310. data/test/unit/test-proc-validate.rb +139 -0
  311. data/test/unit/test-subcmd-help.rb +43 -0
  312. metadata +545 -0
data/app/irb.rb ADDED
@@ -0,0 +1,113 @@
1
+ # Copyright (C) 2010 Rocky Bernstein <rockyb@rubyforge.net>
2
+ # This code comes more or less from ruby-debug.
3
+ require 'irb'
4
+ module IRB # :nodoc:
5
+ module ExtendCommand # :nodoc:
6
+ # FIXME: should we read these out of a directory to
7
+ # make this more user-customizable?
8
+
9
+ unless defined? Continue
10
+ # A base command class that resume execution
11
+ class DebuggerResumeCommand
12
+ def self.execute(conf, *opts)
13
+ name =
14
+ if self.name =~ /IRB::ExtendCommand::(\S+)/
15
+ $1.downcase
16
+ else
17
+ 'unknown'
18
+ end
19
+ $trepan_args = opts
20
+ $trepan_command =
21
+ if $trepan_irb_statements
22
+ $trepan_irb_statements
23
+ else
24
+ ([name] + opts).join(' ')
25
+ end
26
+
27
+ throw :IRB_EXIT, name.to_sym
28
+ end
29
+ end
30
+
31
+ class Continue < DebuggerResumeCommand ; end
32
+ class Finish < DebuggerResumeCommand ; end
33
+ class Next < DebuggerResumeCommand ; end
34
+ class Quit < DebuggerResumeCommand ; end
35
+ class Step < DebuggerResumeCommand ; end
36
+
37
+ # Issues a comamnd to the debugger without continuing
38
+ # execution.
39
+ class Dbgr
40
+ def self.execute(conf, *opts)
41
+ $trepan_command =
42
+ if opts.size == 1 && opts[0].is_a?(String)
43
+ $trepan_args = opts[0]
44
+ else
45
+ opts.join(' ')
46
+ end
47
+ dbg_cmdproc = conf.workspace.instance_variable_get('@dbg_cmdproc')
48
+ dbg_cmdproc.run_command($trepan_command)
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ if defined?(ExtendCommandBundle)
55
+ # New irb Commands which are the same name as their debugger
56
+ # counterpart
57
+ %w(Dbgr Finish Step).each do |name|
58
+ command = name.downcase
59
+ sym = name.to_sym
60
+ ExtendCommandBundle.def_extend_command command, sym
61
+ end
62
+ # New irb Commands which are the slightly different from their
63
+ # debugger counterpart
64
+ [['cont', :Continue],
65
+ ['ne', :Next],
66
+ ['q', :Quit]].each do |command, sym|
67
+ ExtendCommandBundle.def_extend_command command, sym
68
+ end
69
+ end
70
+
71
+ def self.start_session(binding, dbg_cmdproc, conf={})
72
+ unless @__initialized
73
+
74
+ # Set to run the standard trepanx IRB profile
75
+ irbrc = File.expand_path(File.join(File.dirname(__FILE__),
76
+ %w(.. data irbrc)))
77
+ ENV['IRBRC'] = irbrc
78
+
79
+ args = ARGV.dup
80
+ ARGV.replace([])
81
+ IRB.setup(nil)
82
+ ARGV.replace(args)
83
+
84
+ # If the user has a IRB profile, run that now.
85
+ if ENV['TREPANX_IRB']
86
+ ENV['IRBRC'] = ENV['TREPANX_IRB']
87
+ @CONF[:RC_NAME_GENERATOR]=nil
88
+ IRB.run_config
89
+ end
90
+
91
+ @__initialized = true
92
+ end
93
+
94
+ workspace = WorkSpace.new(binding)
95
+
96
+ irb = Irb.new(workspace)
97
+
98
+ @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
99
+ @CONF[:MAIN_CONTEXT] = irb.context
100
+ conf.each {|k, v| @CONF[k] = v}
101
+ # A copy of this back_trace_limit is already active. How?
102
+ IRB.CurrentContext.back_trace_limit = @CONF[:BACK_TRACE_LIMIT]
103
+
104
+ catch(:IRB_EXIT) do
105
+ irb.eval_input
106
+ end
107
+ end
108
+ end
109
+
110
+ if __FILE__ == $0
111
+ # Demo it.
112
+ IRB.start_session(binding, nil) if ARGV.size > 0
113
+ end
data/app/iseq.rb ADDED
@@ -0,0 +1,188 @@
1
+ # Module for working with instruction sequences.
2
+ class Trepan
3
+ module ISeq
4
+ OP_GOTO = Rubinius::InstructionSet.opcodes_map[:goto]
5
+ OP_GOTO_IF_TRUE = Rubinius::InstructionSet.opcodes_map[:goto_if_true]
6
+ OP_GOTO_IF_FALSE = Rubinius::InstructionSet.opcodes_map[:goto_if_false]
7
+ OP_RET = Rubinius::InstructionSet.opcodes_map[:ret]
8
+ OP_YIELD_STACK = Rubinius::InstructionSet.opcodes_map[:yield_stack]
9
+
10
+ module_function
11
+
12
+ # Returns prefix string to indicate whether a breakpoint has been
13
+ # set at this ip and or whether we are currently stopped at this ip.
14
+ def disasm_prefix(ip, frame_ip, cm)
15
+ prefix = cm.breakpoint?(ip) ? 'B' : ' '
16
+ prefix +=
17
+ if ip == frame_ip
18
+ '-->'
19
+ else
20
+ ' '
21
+ end
22
+ end
23
+
24
+ def goto_op?(cm, ip)
25
+ [OP_GOTO, OP_GOTO_IF_TRUE, OP_GOTO_IF_FALSE].member?(cm.iseq[ip])
26
+ end
27
+
28
+ def goto_between(cm, start, fin)
29
+
30
+ iseq = cm.iseq
31
+
32
+ i = start
33
+ while i < fin
34
+ op = iseq[i]
35
+ case op
36
+ when OP_GOTO
37
+ return next_interesting(cm, iseq[i + 1]) # goto target
38
+ when OP_RET
39
+ return -2
40
+ when OP_GOTO_IF_TRUE, OP_GOTO_IF_FALSE
41
+ return [next_interesting(cm, iseq[i + 1]),
42
+ next_interesting(cm, i + 2)] # target and next ip
43
+ when nil
44
+ return -1
45
+ else
46
+ # Rubinius is getting an error here sometimes. Need to figure
47
+ # out what's wrong.
48
+ begin
49
+ op = Rubinius::InstructionSet[op]
50
+ rescue TypeError
51
+ return -1
52
+ end
53
+ i += (op.arg_count + 1)
54
+ end
55
+ end
56
+
57
+ if fin == cm.lines.last
58
+ return -1
59
+ else
60
+ return next_interesting(cm, fin)
61
+ end
62
+ end
63
+
64
+ def ip_ranges_for_line(lines, line)
65
+ result = []
66
+ in_range = false
67
+ start_ip = nil
68
+ total = lines.size
69
+ i = 1
70
+ while i < total
71
+ cur_line = lines.at(i)
72
+ if cur_line == line
73
+ start_ip = lines.at(i-1)
74
+ in_range = true
75
+ elsif cur_line > line && in_range
76
+ result << [start_ip, lines.at(i-1)]
77
+ start_ip = nil
78
+ in_range = false
79
+ end
80
+ i += 2
81
+ end
82
+ if in_range && start_ip
83
+ result << [start_ip, lines.at(total-1)]
84
+ end
85
+ result
86
+ end
87
+
88
+ # Return range of ips covering ip. There will be only one of these.
89
+ # We surround this in another list to match the format of
90
+ # ip_ranges_for_line.
91
+ def ip_ranges_for_ip(lines, ip)
92
+ total = lines.size
93
+ i = 0
94
+ while i < total and lines.at(i) <= ip
95
+ i += 2
96
+ end
97
+ [[lines.at(i-2), lines.at(i)]]
98
+ end
99
+
100
+ def next_interesting(cm, ip)
101
+ pop = Rubinius::InstructionSet.opcodes_map[:pop]
102
+
103
+ if cm.iseq[ip] == pop
104
+ return ip + 1
105
+ end
106
+
107
+ return ip
108
+ end
109
+
110
+ # start is assumed to be on a tail code "synthesized" line, which
111
+ # has line number 0. Follow opcodes until we get to a line that is
112
+ # not 0.
113
+ def tail_code_line(cm, start)
114
+
115
+ iseq = cm.iseq
116
+
117
+ i = start
118
+ fin = cm.lines.last
119
+ while i < fin
120
+ line_no = cm.line_from_ip(i)
121
+ return line_no if line_no > 0
122
+ op = iseq[i]
123
+ case op
124
+ when OP_GOTO_IF_TRUE, OP_GOTO_IF_FALSE, OP_GOTO
125
+ when OP_GOTO
126
+ i = iseq[i+1]
127
+ when OP_RET
128
+ return -2
129
+ when nil
130
+ return -1
131
+ else
132
+ # Rubinius is getting an error here sometimes. Need to figure
133
+ # out what's wrong.
134
+ begin
135
+ op = Rubinius::InstructionSet[op]
136
+ rescue TypeError
137
+ return -1
138
+ end
139
+ i += (op.arg_count + 1)
140
+ end
141
+ end
142
+ return 0
143
+ end
144
+
145
+ def yield_or_return_between(cm, start, fin)
146
+ iseq = cm.iseq
147
+ ips = []
148
+ i = start
149
+ while i < fin
150
+ op = iseq[i]
151
+ if [OP_RET, OP_YIELD_STACK].member?(op)
152
+ ips << i
153
+ end
154
+ op = Rubinius::InstructionSet[op]
155
+ i += (op.arg_count + 1)
156
+ end
157
+ return ips
158
+ end
159
+
160
+ end
161
+ end
162
+ if __FILE__ == $0
163
+ vm_locations = Rubinius::VM.backtrace(0, true)
164
+ call_loc = vm_locations[1]
165
+ cm = call_loc.method
166
+ puts cm.decode
167
+ ips = nil
168
+ puts '-' * 20
169
+ 0.upto((cm.lines.last+1)/2) do |i|
170
+ ip = cm.lines[i*2]
171
+ unless -1 == ip
172
+ ips = Trepan::ISeq.yield_or_return_between(cm, ip, cm.lines.last)
173
+ puts "return: #{ips.inspect}"
174
+ break
175
+ end
176
+ end
177
+ puts '-' * 20
178
+ 0.upto((cm.lines.last+1)/2) do |i|
179
+ ip = cm.lines[i*2]
180
+ unless -1 == ip
181
+ ips = Trepan::ISeq.goto_between(cm, ip, cm.lines.last)
182
+ puts "goto: #{ips.inspect}"
183
+ break
184
+ end
185
+ end
186
+ puts Trepan::ISeq.disasm_prefix(ips[0], ips[0], cm)
187
+ p Trepan::ISeq.disasm_prefix(10, ips[0], cm)
188
+ end
data/app/method.rb ADDED
@@ -0,0 +1,178 @@
1
+ # Copyright (C) 2010, 2013 Rocky Bernstein <rockyb@rubyforge.net>
2
+ module Trepanning
3
+ module Method
4
+
5
+ module_function
6
+ ## FIXME: until the next two routines find their way back into
7
+ ## Rubinius::CompiledMethod...
8
+ ##
9
+ # Locates the instruction address (IP) of the first instruction on
10
+ # the specified line if is in CompiledMethod cm only, or nil if no
11
+ # match for the specified line is found.
12
+ #
13
+ # @return [Fixnum, NilClass] returns
14
+ # nil if nothing is found, else the first IP for the line
15
+ def locate_line_in_cm(line, cm=self)
16
+ cm.lines.each_with_index do |l, i|
17
+ next unless (i&1 == 1)
18
+ if (l == line)
19
+ # Found target line - return first IP
20
+ return cm.lines[i-1]
21
+ elsif l > line
22
+ return nil
23
+ end
24
+ end
25
+ return nil
26
+ end
27
+
28
+ ## FIXME: Try using Routine in Rubinius now.
29
+ ##
30
+ # Locates the CompiledMethod and instruction address (IP) of the
31
+ # first instruction on the specified line. This method recursively
32
+ # examines child compiled methods until an exact match for the
33
+ # searched line is found. It returns both the matching
34
+ # CompiledMethod and the IP of the first instruction on the
35
+ # requested line, or nil if no match for the specified line is
36
+ # found.
37
+ #
38
+ # @return [(Rubinius::CompiledMethod, Fixnum), NilClass] returns
39
+ # nil if nothing is found, else an array of size 2 containing the method
40
+ # the line was found in and the IP pointing there.
41
+ def locate_line(line, cm=self)
42
+ ## p [cm, lines_of_method(cm)]
43
+ ip = locate_line_in_cm(line, cm)
44
+ return cm, ip if ip
45
+
46
+ # Didn't find line in this CM, so check if a contained
47
+ # CM encompasses the line searched for
48
+ cm.child_methods.each do |child|
49
+ if res = locate_line(line, child)
50
+ return res
51
+ end
52
+ end
53
+
54
+ # No child method is a match - fail
55
+ return nil
56
+ end
57
+
58
+ def lines_of_method(cm)
59
+ lines = []
60
+ cm.lines.each_with_index do |l, i|
61
+ lines << l if (i&1 == 1)
62
+ end
63
+ return lines
64
+ end
65
+
66
+ # Return true if ip is the start of some instruction in meth.
67
+ # FIXME: be more stringent.
68
+ def valid_ip?(cm, ip)
69
+ size = cm.lines.size
70
+ ip >= 0 && ip < cm.lines[size-1]
71
+ end
72
+
73
+ # Returns a CompiledMethod for the specified line. We search the
74
+ # current method +meth+ and then up the parent scope. If we hit
75
+ # the top and we can't find +line+ that way, then we
76
+ # reverse the search from the top and search down. This will add
77
+ # all siblings of ancestors of +meth+.
78
+ def find_method_with_line(cm, line)
79
+ unless cm.kind_of?(Rubinius::CompiledMethod)
80
+ return nil
81
+ end
82
+
83
+ lines = lines_of_method(cm)
84
+ return cm if lines.member?(line)
85
+ scope = cm.scope
86
+ return nil unless scope and scope.current_script
87
+ cm = scope.current_script.compiled_code
88
+ lines = lines_of_method(cm)
89
+ until lines.member?(line) do
90
+ child = scope
91
+ scope = scope.parent
92
+ unless scope
93
+ # child is the top-most scope. Search down from here.
94
+ cm = child.current_script.compiled_code
95
+ pair = locate_line(line, cm)
96
+ ## pair = cm.locate_line(line)
97
+ return pair ? pair[0] : nil
98
+ end
99
+ cm = scope.current_script.compiled_code
100
+ lines = lines_of_method(cm)
101
+ end
102
+ return cm
103
+ end
104
+
105
+ def top_scope(cm)
106
+ scope = cm.scope
107
+ return unless scope
108
+ while true do
109
+ scope = scope.parent
110
+ break unless scope && scope.current_script
111
+ cm = scope.current_script.compiled_code
112
+ end
113
+ cm
114
+ end
115
+ end
116
+
117
+ end
118
+
119
+ module Rubinius
120
+
121
+ class Location
122
+ def next_ip
123
+ ip_on_current? ? ip : ip + 1
124
+ end
125
+ end
126
+
127
+ class CompiledMethod < Executable
128
+ ##
129
+ # Returns the address (IP) of the first instruction in this
130
+ # CompiledMethod that is on the specified line but not before the
131
+ # given, or the address of the first instruction on the next code
132
+ # line after the specified line if there are no instructions on
133
+ # the requested line. This method only looks at instructions
134
+ # within the current CompiledMethod; see #locate_line for an
135
+ # alternate method that also searches inside the child
136
+ # CompiledMethods.
137
+
138
+ #
139
+ # @return [Fixnum] the address of the first instruction
140
+ def first_ip_on_line_after(line, start_ip)
141
+ i = 0
142
+ last_i = @lines.size - 1
143
+ while i < last_i
144
+ ip = @lines.at(i)
145
+ cur_line = @lines.at(i+1)
146
+ if cur_line >= line and ip >= start_ip
147
+ return ip
148
+ end
149
+ i += 2
150
+ end
151
+ nil
152
+ end
153
+ end
154
+ end
155
+
156
+
157
+ if __FILE__ == $0
158
+ include Trepanning::Method
159
+ require "#{File.dirname(__FILE__)}/../lib/trepanning"
160
+
161
+ line = __LINE__
162
+ def find_line(line) # :nodoc
163
+ cm = Rubinius::VM.backtrace(0)[0].method
164
+ p lines_of_method(cm)
165
+ p find_method_with_line(cm, line)
166
+ end
167
+
168
+ cm = Rubinius::VM.backtrace(0)[0].method
169
+ p lines_of_method(cm)
170
+ find_line(line)
171
+ p find_method_with_line(cm, line+2)
172
+ puts "top scope: #{top_scope(cm)}"
173
+ ip = locate_line( __LINE__, cm)[1]
174
+ puts "Line #{__LINE__} has ip #{ip}"
175
+ [-1, 0, 10, ip, 10000].each do |i|
176
+ puts "IP #{i} is %svalid" % (valid_ip?(cm, i) ? '' : 'not ')
177
+ end
178
+ end
data/app/mock.rb ADDED
@@ -0,0 +1,13 @@
1
+ # Copyright (C) 2010 Rocky Bernstein <rockyb@rubyforge.net>
2
+ class Trepan
3
+ attr_accessor :trace_filter # Procs/Methods we ignore.
4
+ def initialize(opts={})
5
+ @trace_filter = []
6
+ end
7
+ class MockCore
8
+ attr_accessor :dbgr
9
+ attr_reader :core
10
+ def event; 'line' end
11
+ end
12
+ end
13
+
data/app/options.rb ADDED
@@ -0,0 +1,154 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ # Copyright (C) 2010-2011, 2013 Rocky Bernstein <rockyb@rubyforge.net>
4
+ #=== Summary
5
+ # Parses command-line options.
6
+
7
+ require 'optparse'
8
+ class Trepan
9
+ require 'rubygems'; require 'require_relative'
10
+ require_relative 'default'
11
+
12
+ VERSION = '0.2.0'
13
+ PROGRAM = 'trepanx' unless defined? Trepan::PROGRAM
14
+
15
+ def self.show_version
16
+ "#{PROGRAM}, version #{VERSION}"
17
+ end
18
+
19
+ def self.copy_default_options
20
+ options = {}
21
+ DEFAULT_CMDLINE_SETTINGS.each do |key, value|
22
+ begin
23
+ options[key] = value.clone
24
+ rescue TypeError
25
+ options[key] = value
26
+ end
27
+ end
28
+ options
29
+ end
30
+
31
+ def self.setup_options(options, stdout=$stdout, stderr=$stderr)
32
+ OptionParser.new do |opts|
33
+ opts.banner = <<EOB
34
+ #{show_version}
35
+ Usage: #{PROGRAM} [options] [[--] <script.rb> <script.rb parameters>]
36
+ EOB
37
+ opts.separator ''
38
+ opts.separator 'Options:'
39
+ opts.on('--client',
40
+ 'Connect to out-of-process program') do
41
+ if options[:server]
42
+ stderr.puts '--server option previously given. --client option ignored.'
43
+ else
44
+ options[:client] = true
45
+ end
46
+ end
47
+ opts.on('-c', '--command FILE', String,
48
+ 'Execute debugger commands from FILE') do |cmdfile|
49
+ if File.readable?(cmdfile)
50
+ options[:cmdfiles] << cmdfile
51
+ elsif File.exists?(cmdfile)
52
+ stderr.puts "Command file '#{cmdfile}' is not readable. Option ignored."
53
+ else
54
+ stderr.puts "Command file '#{cmdfile}' does not exist."
55
+ end
56
+ end
57
+ opts.on('--cd DIR', String, 'Change current directory to DIR') do |dir|
58
+ if File.directory?(dir)
59
+ if File.executable?(dir)
60
+ options[:chdir] = dir
61
+ else
62
+ stderr.puts "Can't cd to #{dir}. Option --cd ignored."
63
+ end
64
+ else
65
+ stderr.puts "\"#{dir}\" is not a directory. Option --cd ignored."
66
+ end
67
+ end
68
+ opts.on('--basename',
69
+ 'Show only file basename in file locations') do
70
+ options[:basename] = true
71
+ end
72
+ opts.on('-d', '--debug', 'Set $DEBUG=true') do
73
+ $DEBUG = true
74
+ end
75
+ opts.on('--[no-]highlight',
76
+ 'Use [no] syntax highlight output') do |v|
77
+ options[:highlight] = ((v) ? :term : nil)
78
+ end
79
+ opts.on('-h', '--host NAME', String,
80
+ 'Host or IP used in TCP connections for --server or --client. ' +
81
+ "Default is #{DEFAULT_SETTINGS[:host].inspect}.") do
82
+ |name_or_ip|
83
+ options[:host] = name_or_ip
84
+ end
85
+ opts.on('-I', '--include PATH', String, 'Add PATH to $LOAD_PATH') do
86
+ |path|
87
+ $LOAD_PATH.unshift(path)
88
+ end
89
+ opts.on('--nx',
90
+ "Do not run debugger initialization file #{CMD_INITFILE}") do
91
+ options[:nx] = true
92
+ end
93
+ opts.on('-p', '--port NUMBER', Integer,
94
+ 'Port number used in TCP connections for --server or --client. ' +
95
+ "Default is #{DEFAULT_SETTINGS[:port]}.") do
96
+ |num|
97
+ options[:port] = num
98
+ end
99
+ opts.on('--[no-]readline',
100
+ 'Try [not] GNU Readline') do |v|
101
+ options[:readline] = v
102
+ end
103
+ opts.on('-r', '--require SCRIPT', String,
104
+ 'Require the library, before executing your script') do |name|
105
+ if name == 'debug'
106
+ stderr.puts "ruby-debug is not compatible with Ruby's 'debug' library. This option is ignored."
107
+ else
108
+ require name
109
+ end
110
+ end
111
+ opts.on('-s', '--server',
112
+ 'Set up for out-of-process debugging') do
113
+ if options[:client]
114
+ stderr.puts '--client option previously given. --server option ignored.'
115
+ else
116
+ options[:server] = true
117
+ end
118
+ end
119
+ opts.on('-x', '--trace', 'Turn on line tracing') do
120
+ options[:traceprint] = true
121
+ options[:nx] = true
122
+ end
123
+ opts.separator ''
124
+ opts.on_tail('-?', '--help', 'Show this message') do
125
+ options[:help] = true
126
+ stdout.puts opts
127
+ exit
128
+ end
129
+ opts.on_tail('-v', '--version',
130
+ 'print the version') do
131
+ options[:version] = true
132
+ stdout.puts show_version
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ if __FILE__ == $0
139
+ opts = {}
140
+ options ={}
141
+ [%w(--help), %w(--version)].each do |o|
142
+ options = Trepan::copy_default_options
143
+ opts = Trepan::setup_options(options)
144
+ rest = opts.parse o
145
+ p options
146
+ puts '=' * 10
147
+ end
148
+ rest = opts.parse! ARGV
149
+ puts opts
150
+ puts '=' * 10
151
+ p options
152
+ puts '=' * 10
153
+ p Trepan::DEFAULT_CMDLINE_SETTINGS
154
+ end