Class: Google::Cloud::Debugger::Breakpoint::Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/debugger/breakpoint/variable.rb

Overview

Variable

Represents a variable or an argument possibly of a compound object type. Note how the following variables are represented:

A simple Variable:

  x = 5
  # Captured variable:
  # { name: "x", value: "5", type: "Integer" }

A Compound Variable:

  class T
    attr_accessor :m1, :m2
    ...
  end
  v = T.new(1, "2")
  # Captured variable:
  # {
  #   name: "v",
  #   type: "T",
  #   members: [
  #     { name: "@m1", value: "1", type: "Integer" },
  #     { name: "@m2", value: "2", type: "String" }
  #   ]
  # }

A Hash object:

  hash = { a: 1, b: :two }
  # Captured variable:
  # {
  #   name: "hash",
  #   type: "Hash",
  #   members: [
  #     { name: "a", value: "1", type: "Integer" },
  #     { name: "b", value: ":2", type: "Symbol" }
  #   ]
  # }

An Array object:

  ary = [1, nil]
  # Captured variable:
  # {
  #   name: "ary",
  #   type: "Array",
  #   members: [
  #     { name: "[0]", value: "1", type: "Integer" },
  #     { name: "[1]", value: "nil", type: "NilClass" }
  #   ]
  # }

Constant Summary collapse

MAX_DEPTH =

Max depth to convert on compound variables

3
MAX_MEMBERS =

Max number of member variables to evaluate in compound variables

1000
MAX_STRING_LENGTH =

Max length on variable inspect results. Truncate extra and replace with ellipsis.

500

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_rb_var(source, name: nil, depth: MAX_DEPTH, var_table: nil, limit: nil) ⇒ Google::Cloud::Debugger::Breakpoint::Variable

Convert a Ruby variable into a Google::Cloud::Debugger::Breakpoint::Variable object. If a variable table is provided, it will store all the subsequently created compound variables into the variable table for sharing.

Examples:

Simple variable conversion

x = 3.0
var = Google::Cloud::Debugger::Breakpoint::Variable.from_rb_var \
        x, name: "x"
var.name  #=> "x"
var.value #=> "3.0"
var.type  #=> "Float"

Hash conversion

hash = {a: 1.0, b: :two}
var = Google::Cloud::Debugger::Breakpoint::Variable.from_rb_var \
        hash, name: "hash"
var.name  #=> "hash"
var.type  #=> "Hash"
var.members[0].name  #=> "a"
var.members[0].value #=> "1.0"
var.members[0].type  #=> "Float"
var.members[1].name  #=> "b"
var.members[1].value #=> ":two"
var.members[1].type  #=> "Symbol"

Custom compound variable conversion

foo = Foo.new(a: 1.0, b: [])
foo.inspect  #=> "#<Foo:0xXXXXXX @a=1.0, @b=[]>"
var = Google::Cloud::Debugger::Breakpoint::Variable.from_rb_var \
        foo, name: "foo"
var.name  #=> "foo"
var.type  #=> "Foo"
var.members[0].name  #=> "@a"
var.members[0].value #=> "1.0"
var.members[0].type  #=> "Float"
var.members[1].name  #=> "@b"
var.members[1].value #=> "[]"
var.members[1].type  #=> "Array"

Use variable table for shared compound variables

hash = {a: 1.0}
ary = [hash, hash]
var_table = Google::Cloud::Debugger::Breakpoint::VariableTable.new
var = Google::Cloud::Debugger::Breakpoint::Variable.from_rb_var \
        ary, name: "ary", var_table: var_table
var.name            #=> "ary"
var.var_table_index #=> 0
var_table[0].type   #=> "Array"
var_table[0].members[0].name            #=> "[0]"
var_table[0].members[0].var_table_index #=> 1
var_table[0].members[1].name            #=> "[1]"
var_table[0].members[1].var_table_index #=> 1
var_table[1].type #=> "Hash"
var_table[1].members[0].name #=> "a"
var_table[1].members[0].type #=> "Float"
var_table[1].members[0].value #=> "1.0"

Parameters:

  • source (Any)

    Source Ruby variable to convert from

  • name (String) (defaults to: nil)

    Name of the varaible

  • depth (Integer) (defaults to: MAX_DEPTH)

    Number of levels to evaluate in compound variables. Default to MAX_DEPTH

  • var_table (Breakpoint::VariableTable) (defaults to: nil)

    A variable table to store shared compound variables. Optional.

  • limit (Integer) (defaults to: nil)

    Maximum number of bytes this conversion should take. This include nested compound member variables' conversions.

Returns:



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/google/cloud/debugger/breakpoint/variable.rb', line 237

def self.from_rb_var source, name: nil, depth: MAX_DEPTH,
                     var_table: nil, limit: nil
  return source if source.is_a? Variable

  if limit && limit < MIN_REQUIRED_SIZE
    return buffer_full_variable var_table
  end

  # If source is a non-empty Array or Hash, or source has instance
  # variables, evaluate source as a compound variable.
  if compound_var?(source) && depth.positive?
    from_compound_var source, name: name, depth: depth,
                              var_table: var_table, limit: limit
  else
    from_primitive_var source, name: name, limit: limit
  end
rescue StandardError
  new.tap do |var|
    var.name = name.to_s if name
    var.set_error_state FAIL_CONVERSION_MSG
    var.source_var = source
  end
end

Instance Method Details

#payload_sizeInteger

Calculate the bytesize of all the attributes and that of the member variables.

Returns:

  • (Integer)

    The total payload size of this variable in bytes.



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/google/cloud/debugger/breakpoint/variable.rb', line 508

def payload_size
  unless @payload_size
    @payload_size = name.to_s.bytesize +
                    type.to_s.bytesize +
                    value.to_s.bytesize

    unless members.nil?
      @payload_size = members.inject @payload_size do |sum, member|
        sum + member.payload_size
      end
    end
  end

  @payload_size
end

#set_error_state(message, refers_to: StatusMessage::VARIABLE_VALUE) ⇒ Object

Set this variable to an error state by setting the status field



473
474
475
476
477
478
479
# File 'lib/google/cloud/debugger/breakpoint/variable.rb', line 473

def set_error_state message, refers_to: StatusMessage::VARIABLE_VALUE
  @status = StatusMessage.new.tap do |s|
    s.is_error = true
    s.refers_to = refers_to
    s.description = message
  end
end

#to_grpcObject

Exports the Variable to a Google::Cloud::Debugger::V2::Variable object.



459
460
461
462
463
464
465
466
467
468
469
# File 'lib/google/cloud/debugger/breakpoint/variable.rb', line 459

def to_grpc
  return nil if empty?
  Google::Cloud::Debugger::V2::Variable.new(
    name: name.to_s,
    value: value.to_s,
    type: type.to_s,
    var_table_index: var_table_index_to_grpc,
    members: members_to_grpc || [],
    status: status_to_grpc
  )
end

#total_sizeInteger

Calculate the total bytesize of all the attributes and that of the member variables, plus references into other variables in the variable table.

Returns:

  • (Integer)

    The total payload size of this variable in bytes.



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/google/cloud/debugger/breakpoint/variable.rb', line 487

def total_size
  unless @total_size
    vars = [self, *(unique_members || [])]

    @total_size = vars.inject payload_size do |sum, var|
      if var.var_table && var.var_table_index
        sum + var.var_table[var.var_table_index].total_size
      else
        sum
      end
    end
  end

  @total_size
end