是否有相当于 Ruby 的 shell 脚本的 xtrace 选项?

Kat*_*ryn 5 ruby

在调试 shell 脚本时,我发现使用xtraceon运行会很有帮助:

-x    xtrace        Print commands  and  parameter
                    assignments when they are exe-
                    cuted, preceded by  the  value
                    of PS4.
Run Code Online (Sandbox Code Playgroud)

例如:

$ set -x
$ s='Say again?'
+ s='Say again?'

# Other commands that might mess with the value of $s

$ echo $s
+ echo Say 'again?'
Say again?
Run Code Online (Sandbox Code Playgroud)

我知道,Ruby有交互式调试程序,如byebug,但我正在寻找的东西,将很容易打开用于记录自动化脚本。

我确实找到了一个xtrace gem,但它与 PHP 格式有关。

我还看到有一个Tracer 类和一个TracePoint 类,它们似乎提供了一种在执行语句时打印语句的方法。但是我还没有找到任何打印变量值的方法(而不仅仅是变量名):

$ ruby -r tracer trace.rb 
#0:/usr/local/Cellar/ruby/2.4.1_1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:Kernel:<:       return gem_original_require(path)
#0:trace.rb:1::-: s='Say again?'
#0:trace.rb:2::-: puts s
Say again?
Run Code Online (Sandbox Code Playgroud)

我想让倒数第二行读:

#0:trace.rb:2::-: puts 'Say again?'
Run Code Online (Sandbox Code Playgroud)

这可能吗?或者 Ruby 有更好的方法吗?

Kat*_*ryn 1

我能够构建一个模块,或多或少可以满足我的需求:

require 'pry'

=begin
  We are always one line behind because the value of assignment comes
  _after_ the trace is called. Without this, every assignment would look
  like:

      x = 1 #=> {:x=>nil}

   It would be nice if the trace happened after assignment, but what can 
  you do?  
=end 

module Xtrace
  # Only run the trace on the main file, not on require'd files.
  # Possible room for expansion later.
  @files = {$0 => Pry::Code.from_file($0)}

  def Xtrace.print_trace
    if @prev_line then
      if @files[@path] then
        line = @files[@path].around(@prev_line, 0).chomp

        # When calling a method, don't make it look like it's being defined.
        line.gsub!(/^\s*def\s*\b/, '') if @event == :call

        values = []
        @bind.local_variables.each do |v|
          values << {v => @bind.local_variable_get(v)} if line =~ /\b#{v}\b/
        end
        STDERR.printf "%5s: %s", @prev_line, line
        STDERR.printf " #=> %s", values.join(', ') unless values.empty?
        STDERR.printf "\n"
      end
    end
  end

  @xtrace = TracePoint.trace(:line, :call) do |tp|
    tp.disable 
    @bind=tp.binding
    Xtrace.print_trace

    # Other than the binding, everything we need to print comes from the
    # previous trace call.
    @prev_line = tp.lineno
    @event=tp.event
    @path=tp.path
    tp.enable
  end    

  # Need to print the trace one last time after the last line of code. 
  at_exit do
    # It doesn't matter what we do in this last line. Any statement works.
    # Also, it's a bit inconvenient that the disable command is itself traced.
    @xtrace.disable
  end
end
Run Code Online (Sandbox Code Playgroud)

如果将其放入名为 xtrace.rb 的文件中并放入库加载路径中,则可以通过添加 .rb 来开始跟踪require 'xtrace'。它打印每行的行号和执行的方法调用、实际代码以及该行中任何局部变量的值。对于简单的阶乘函数,输出可能如下所示:

    3: def factorial(n)
    8: puts factorial(3)
    3: factorial(n) #=> {:n=>3}
    4:   return 1 if n <= 1 #=> {:n=>3}
    5:   return n*factorial(n-1) #=> {:n=>2}
    3: factorial(n) #=> {:n=>2}
    4:   return 1 if n <= 1 #=> {:n=>2}
    5:   return n*factorial(n-1) #=> {:n=>1}
    3: factorial(n) #=> {:n=>1}
    4:   return 1 if n <= 1
6
Run Code Online (Sandbox Code Playgroud)

目前,它只查看局部变量。它还只跟踪执行的文件,而不跟踪任何加载的文件。目前还没有办法启用或禁用跟踪。跟踪在您需要模块时开始,在执行时结束。跟踪输出进入 STDERR 并且格式是硬编码的。

如果您使用此模块,请注意不要泄露密码、API 密钥或 PII 等敏感信息