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/markdown.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, Markdown, 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 =
'2.15.35'
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



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

def completion_proc
  @completion_proc
end

#coreObject

access to Trepan::Core instance



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

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.



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

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.



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

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

#trace_pointObject

Trace point object



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

def trace_point
  @trace_point
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{ … }



279
280
281
282
283
284
285
# File 'lib/trepanning.rb', line 279

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

.debug_str(string, opts = DEFAULT_DEBUG_STR_SETTINGS) ⇒ Object



287
288
289
290
291
292
293
# File 'lib/trepanning.rb', line 287

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
132
133
134
135
136
# File 'app/options.rb', line 30

def self.setup_options(options, stdout=$stdout, stderr=$stderr)
    options[:highlight] = :term
    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('--basename',
                'Show basename only on source file listings') do
            options[:basename] = true
        end
        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, opts = {}, stderr = $stderr) ⇒ Object



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

def add_command_file(cmdfile, opts={}, 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, opts[:out])
end

#add_startup_filesObject



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

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
# 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.get.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
        @trace_point = TracePoint.new() do |tp|
            @core.event_processor_tp(tp)
        end
        RubyVM::Frame::get.trace_off1 = true
        @trace_point.enable
        yield block
        RubyVM::Frame::get.trace_off = true
        @trace_point.disable
        @trace_point = nil
    else
        @trace_point = TracePoint.new() do |tp|
            @core.event_processor_tp(tp)
        end
        RubyVM::Frame::get.trace_off1 = true
        @trace_point.enable
    end
end

#process_cmdfile_setting(settings) ⇒ Object



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

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

#start(enable = false) ⇒ Object

Set core’s trace-event processor to run



210
211
212
213
214
215
216
# File 'lib/trepanning.rb', line 210

def start(enable = false)
    @trace_point = TracePoint.new() do |tp|
        @core.event_processor_tp(tp)
    end
    RubyVM::Frame::get.trace_off1 = true
    @trace_point.enable if enable
end

#stopObject



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

def stop
    RubyVM::Frame::get.trace_off = true
    @trace_point.disable if @trace_point and
        @trace_point.respond_to?(:disable)
end