Sha256: 263cdbfb763f253e5428ea24ffb0d8c9782edd87a93b2925002257d162783591

Contents?: true

Size: 1.39 KB

Versions: 12

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require "byebug/command"

module Byebug
  #
  # Show (and possibily stop) at every line that changes a global variable.
  #
  class TracevarCommand < Command
    def self.regexp
      /^\s* tr(?:acevar)? (?: \s+ (\S+))?  # (variable-name)?
                          (?: \s+ (stop|nostop))?
       \s*$/x
    end

    def self.description
      <<-DESCRIPTION
        tr[acevar] <variable> [[no]stop]

        #{short_description}

        If "stop" is specified, execution will stop every time the variable
        changes its value. If nothing or "nostop" is specified, execution won't
        stop, changes will just be logged in byebug's output.
      DESCRIPTION
    end

    def self.short_description
      "Enables tracing of a global variable"
    end

    def execute
      var = @match[1]
      return errmsg(pr("trace.errors.needs_global_variable")) unless var

      unless global_variables.include?(:"#{var}")
        return errmsg(pr("trace.errors.var_is_not_global", name: var))
      end

      stop = @match[2] && @match[2] !~ /nostop/

      instance_eval do
        trace_var(:"#{var}") { |val| on_change(var, val, stop) }
      end

      puts pr("trace.messages.success", var: var)
    end

    private

    def on_change(name, value, stop)
      puts pr("trace.messages.on_change", name: name, value: value)

      context.step_out(1, false) if stop
    end
  end
end

Version data entries

12 entries across 12 versions & 5 rubygems

Version Path
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/byebug-10.0.2/lib/byebug/commands/tracevar.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/byebug-10.0.2/lib/byebug/commands/tracevar.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/byebug-10.0.2/lib/byebug/commands/tracevar.rb
dadapush_client-1.0.1 vendor/bundle/ruby/2.3.0/gems/byebug-10.0.2/lib/byebug/commands/tracevar.rb
jets-0.5.5 vendor/lambdagem/bundled/gems/ruby/2.5.0/gems/byebug-10.0.0/lib/byebug/commands/tracevar.rb
jets-0.5.4 vendor/lambdagem/bundled/gems/ruby/2.5.0/gems/byebug-10.0.0/lib/byebug/commands/tracevar.rb
jets-0.5.3 vendor/lambdagem/bundled/gems/ruby/2.5.0/gems/byebug-10.0.0/lib/byebug/commands/tracevar.rb
jets-0.5.2 vendor/lambdagem/bundled/gems/ruby/2.5.0/gems/byebug-10.0.0/lib/byebug/commands/tracevar.rb
jets-0.5.1 vendor/lambdagem/bundled/gems/ruby/2.5.0/gems/byebug-10.0.0/lib/byebug/commands/tracevar.rb
byebug-10.0.2 lib/byebug/commands/tracevar.rb
byebug-10.0.1 lib/byebug/commands/tracevar.rb
byebug-10.0.0 lib/byebug/commands/tracevar.rb