Class: Trepan

Inherits:
Object
  • Object
show all
Defined in:
app/mock.rb,
app/core.rb,
app/util.rb,
app/frame.rb,
app/thread.rb,
app/default.rb,
app/options.rb,
app/brkptmgr.rb,
app/complete.rb,
app/cmd_parse.rb,
app/condition.rb,
app/breakpoint.rb,
lib/trepanning.rb,
app/disassemble.rb

Overview

Copyright © 2010 Rocky Bernstein <[email protected]> Splits String dissasemble_str into an array for for each line. Line(s) that Fixnum pc_offset matches at the beginning of the line will have prefix “–> ” added, otherwise the line will have “ ” added to the beginning. This array is returned.

Defined Under Namespace

Modules: CmdParser, Complete, Condition, Disassemble, Frame, ThreadHelper, Util Classes: Breakpoint, BreakpointMgr, Core, MockCore, MockDebugger

Constant Summary collapse

CMD_INITFILE_BASE =
if RUBY_PLATFORM =~ /mswin/
  # Of course MS Windows has to be different
  HOME_DIR     =  (ENV['HOME'] || 
                   ENV['HOMEDRIVE'].to_s + ENV['HOMEPATH'].to_s).to_s
  'trepan.ini'
else
  HOME_DIR = ENV['HOME'].to_s
  '.trepanrc'
end
CMD_INITFILE =
File.join(HOME_DIR, CMD_INITFILE_BASE)
VERSION =
'0.1.6'
PROGRAM =
'trepan'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ Trepan

Returns a new instance of Trepan.



5
6
7
8
# File 'app/mock.rb', line 5

def initialize(opts={})
  @trace_filter = []
  @intf = []
end

Instance Attribute Details

#completion_procObject (readonly)

GNU Readline completion proc



28
29
30
# File 'lib/trepanning.rb', line 28

def completion_proc
  @completion_proc
end

#coreObject

access to Trepan::Core instance



29
30
31
# File 'lib/trepanning.rb', line 29

def core
  @core
end

#intfObject

Returns the value of attribute intf.



4
5
6
# File 'app/mock.rb', line 4

def intf
  @intf
end

#restart_argvObject

Array. The way the outside world interfaces with us. An array, so that interfaces can be stacked.



33
34
35
# File 'lib/trepanning.rb', line 33

def restart_argv
  @restart_argv
end

#settingsObject (readonly)

How to restart us, empty or nil. Note: restart_argv is typically C’s **argv, not Ruby’s ARGV. So restart_argv is $0.



37
38
39
# File 'lib/trepanning.rb', line 37

def settings
  @settings
end

#trace_filterObject

Procs/Methods we ignore.



3
4
5
# File 'app/mock.rb', line 3

def trace_filter
  @trace_filter
end

Class Method Details

.copy_default_optionsObject



18
19
20
21
22
23
24
25
26
27
28
# File 'app/options.rb', line 18

def self.copy_default_options
  options = {}
  DEFAULT_CMDLINE_SETTINGS.each do |key, value|
    begin 
      options[key] = value.clone
    rescue TypeError
      options[key] = value
    end
  end
  options
end

.debug(opts = {}, &block) ⇒ Object

You can run:

require 'trepanning'

Trepan.debug

See debugger for options that can be passed. By default :hide_stack is set.

Likewise for mydbg.debugger{ … }



281
282
283
284
285
286
287
288
289
# File 'lib/trepanning.rb', line 281

def self.debug(opts={}, &block)
  opts = {:hide_stack => false}.merge(opts)
  unless defined?($trepanning) && $trepanning.is_a?(Trepan)
    $trepanning = Trepan.new(opts)
  end
  tf = $trepanning.trace_filter
  tf << self.method(:debugger) unless tf.member? self.method(:debugger)
  $trepanning.debugger(opts, &block)
end

.debug_str(string, opts = DEFAULT_DEBUG_STR_SETTINGS) ⇒ Object



291
292
293
294
295
296
297
# File 'lib/trepanning.rb', line 291

def self.debug_str(string, opts = DEFAULT_DEBUG_STR_SETTINGS)
  $trepanning = Trepan.new(opts) unless 
    $trepanning && $trepanning.is_a?(Trepan)
  $trepanning.core.processor.settings[:different] = false
  # Perhaps we should do a remap file to string right here? 
  $trepanning.debugger(opts) { eval(string) }
end

.setup_options(options, stdout = $stdout, stderr = $stderr) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
92
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
# File 'app/options.rb', line 30

def self.setup_options(options, stdout=$stdout, stderr=$stderr)
  OptionParser.new do |opts|
    opts.banner = <<EOB
#{show_version}
Usage: #{PROGRAM} [options] [[--] <script.rb> <script.rb parameters>]
EOB
    opts.separator ''
    opts.separator 'Options:'
    opts.on('--client',
            'Connect to out-of-process program') do
      if options[:server]
        stderr.puts '--server option previously given. --client option ignored.'
      else
        options[:client] = true
      end
    end
    opts.on('-c', '--command FILE', String, 
            'Execute debugger commands from FILE') do |cmdfile| 
      if File.readable?(cmdfile)
        options[:cmdfiles] << cmdfile
      elsif File.exists?(cmdfile)
          stderr.puts "Command file '#{cmdfile}' is not readable. Option ignored."
      else
        stderr.puts "Command file '#{cmdfile}' does not exist."
      end
    end
    opts.on('--cd DIR', String, 'Change current directory to DIR') do |dir| 
      if File.directory?(dir)
        if File.executable?(dir)
          options[:chdir] = dir
        else
          stderr.puts "Can't cd to #{dir}. Option --cd ignored."
        end
      else
        stderr.puts "\"#{dir}\" is not a directory. Option --cd ignored."
      end
    end
    opts.on('-d', '--debug', "Set $DEBUG=true") {$DEBUG = true}
    opts.on('--[no-]highlight',
            'Use [no] syntax highlight output') do |v|
      options[:highlight] = ((v) ? :term : nil)
    end
    opts.on('-h', '--host NAME', String, 
            'Host or IP used in TCP connections for --server or --client. ' + 
            "Default is #{DEFAULT_SETTINGS[:host].inspect}.") do 
      |name_or_ip| 
      options[:host] = name_or_ip
    end
    opts.on('-I', '--include PATH', String, 'Add PATH to $LOAD_PATH') do 
      |path|
      $LOAD_PATH.unshift(path)
    end
    opts.on('--nx',
            "Do not run debugger initialization file #{CMD_INITFILE}") do
      options[:nx] = true
    end
    opts.on('-p', '--port NUMBER', Integer, 
            'Port number used in TCP connections for --server or --client. ' + 
            "Default is #{DEFAULT_SETTINGS[:port]}.") do 
      |num| 
      options[:port] = num
    end
    # opts.on('-m', '--post-mortem', 'Activate post-mortem mode') do 
    #   options[:post_mortem] = true
    # end
    opts.on('--[no-]readline',
            'Try [not] GNU Readline') do |v|
      options[:readline] = v
    end
    opts.on('-r', '--require SCRIPT', String,
            'Require the library, before executing your script') do |name|
      if name == 'debug'
        stderr.puts "ruby-debug is not compatible with Ruby's 'debug' library. This option is ignored."
      else
        require name
      end
    end
    opts.on('-s', '--server',
            'Set up for out-of-process debugging') do
      if options[:client]
        stderr.puts '--client option previously given. --server option ignored.'
      else
        options[:server] = true
      end
    end
    opts.on('-x', '--trace', 'Turn on line tracing') do
      options[:traceprint] = true
      options[:nx] = true
    end
    opts.separator ''
    opts.on_tail('-?', '--help', 'Show this message') do
      options[:help] = true
      stdout.puts opts
      exit 
    end
    opts.on_tail('-v', '--version', 
                 'print the version') do
      options[:version] = true
      stdout.puts show_version
    end
  end
end

.show_versionObject



14
15
16
# File 'app/options.rb', line 14

def self.show_version
  "#{PROGRAM}, version #{VERSION}"
end

Instance Method Details

#add_command_file(cmdfile, stderr = $stderr) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/trepanning.rb', line 226

def add_command_file(cmdfile, stderr=$stderr)
  unless File.readable?(cmdfile)
    if File.exists?(cmdfile)
      stderr.puts "Command file '#{cmdfile}' is not readable."
      return
    else
      stderr.puts "Command file '#{cmdfile}' does not exist."
      return
    end
  end
  @intf << Trepan::ScriptInterface.new(cmdfile)
end

#add_startup_filesObject



239
240
241
242
243
244
245
246
247
248
# File 'lib/trepanning.rb', line 239

def add_startup_files()
  seen = {}
  cwd_initfile = File.join('.', Trepan::CMD_INITFILE_BASE)
  [cwd_initfile, Trepan::CMD_INITFILE].each do |initfile|
    full_initfile_path = File.expand_path(initfile)
    next if seen[full_initfile_path]
    add_command_file(full_initfile_path) if File.readable?(full_initfile_path)
    seen[full_initfile_path] = true
  end
end

#completion_method(last_token, leading = Readline.line_buffer) ⇒ Object

The method is called when we want to do debugger command completion such as called from GNU Readline with <TAB>.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/trepanning.rb', line 119

def completion_method(last_token, leading=Readline.line_buffer)
  completion = @core.processor.complete(leading, last_token)
  if 1 == completion.size 
    completion_token = completion[0]
    if last_token.end_with?(' ')
      if last_token.rstrip == completion_token 
        # There is nothing more to complete
        []
      else
        []
      end
    else
      [completion_token]
    end
  else
    # We have multiple completions. Get the last token so that will
    # be presented as a list of completions.
    completion
  end
end

#debugger(opts = {}, &block) ⇒ Object

:immediate - boolean. If true, immediate stop rather than wait

                       for an event

:hide_stack - boolean. If true, omit stack frames before the
                       debugger call

:debugme    - boolean. Allow tracing into this routine. You
                       generally won't want this. It slows things
                       down horribly.


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/trepanning.rb', line 178

def debugger(opts={}, &block)
  # FIXME: one option we may want to pass is the initial trace filter.
  if opts[:hide_stack]
    @core.processor.hidelevels[Thread.current] = 
      RubyVM::Frame.current.stack_size
  end
  # unless defined?(PROG_UNRESOLVED_SCRIPT)
  #  # We may later do more sophisticated things...
  #  Trepan.const_set('PROG_UNRESOLVED_SCRIPT', RubyVM::OS_ARGV.index($0) ? $0 : nil)
  # end
  th = Thread.current
  if block
    start
    ret = block.call
    stop
    return ret
  elsif opts[:immediate]
    # Stop immediately after this method returns. But if opts[:debugme]
    # is set, we can stop in this method.
    RubyVM::Frame::current.trace_off = true unless opts[:debugme]
    @trace_filter.set_trace_func(@core.event_proc) 
    Trace.event_masks[0] |= @core.step_events
    @core.debugger(1) 
  else
    RubyVM::Frame::current.trace_off = true unless opts[:debugme]

    @trace_filter.set_trace_func(@core.event_proc)
    Trace.event_masks[0] |= @core.step_events

    # Set to stop on the next event after this returns.
    @core.step_count = opts[:step_count] || 0
  end
end

#process_cmdfile_setting(settings) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/trepanning.rb', line 250

def process_cmdfile_setting(settings)
  settings[:cmdfiles].each do |item|
    cmdfile, opts = 
      if item.kind_of?(Array)
        item
      else
        [item, {}]
      end
    add_command_file(cmdfile, opts)
  end if settings.member?(:cmdfiles)
end

#startObject

Set core’s trace-event processor to run



213
214
215
# File 'lib/trepanning.rb', line 213

def start
  @trace_filter.add_trace_func(@core.event_proc)
end

#stop(opts = {}) ⇒ Object

Remove all of our trace events



218
219
220
221
222
223
224
# File 'lib/trepanning.rb', line 218

def stop(opts={})
  # FIXME: should do something in the middle when
  # we have the ability to remove *our* specific hook
  # @trace_filter.set_trace_func(nil)
  # @trace_filter.remove_trace_func
  clear_trace_func
end