Class: ElfUtils::Section::DebugInfo

Inherits:
Base
  • Object
show all
Defined in:
lib/elf_utils/section/debug_info.rb

Defined Under Namespace

Classes: CompilationUnit, DebugStrOffsetsRef, DebugStrRef, Die, DieRef, Header

Instance Attribute Summary

Attributes inherited from Base

#header

Instance Method Summary collapse

Methods inherited from Base

#addr, #alloc?, #bytes, #flags, #initialize, #inspect, #load_segment, #name, #offset, #relocate, #relocation_offset, #size, #symbol, #symbols, #to_range

Constructor Details

This class inherits a constructor from ElfUtils::Section::Base

Instance Method Details

#compilation_unit(arg) ⇒ Object Also known as: cu

get a compilation unit



25
26
27
28
29
30
31
32
33
# File 'lib/elf_utils/section/debug_info.rb', line 25

def compilation_unit(arg)
  # XXX do this in a way where we don't need to load all the CUs
  case arg
  when String
    cus.find { |cu| cu.root_die[:name] == arg }
  when Integer
    cus.find { |cu| cu.offset == arg }
  end
end

#compilation_unit_for_offset(offset) ⇒ Object Also known as: cu_for_offset

return the CompilationUnit that contains the provided offset



37
38
39
# File 'lib/elf_utils/section/debug_info.rb', line 37

def compilation_unit_for_offset(offset)
  cus.find { |cu| cu.die_range.include?(offset) }
end

#compilation_unitsObject Also known as: cus



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/elf_utils/section/debug_info.rb', line 3

def compilation_units
  @cus ||= begin
    offset = 0
    Header.with_endian(@file.endian).unpack_all(bytes).map do |header|
      # PERF: we freeze the header to prevent CTypes::Union from
      # re-encoding the header when accessing the inner value.  This is
      # the common performance work-around for union change tracking.
      #
      # The proper fix is probably to break up the convenience Header union
      # and just parse the unit header directly, then parse the proper
      # header version.
      header.freeze
      cu = CompilationUnit.new(@file, offset, header.inner)
      offset += (cu.format == :dwarf32) ? 4 : 12
      offset += cu.size
      cu
    end
  end
end

#die(offset) ⇒ Object



67
68
69
70
71
# File 'lib/elf_utils/section/debug_info.rb', line 67

def die(offset)
  cu = compilation_unit_for_offset(offset) or
    raise Error, "No CU found containing offset %x" % offset
  cu.die(offset)
end

#dies(top: false, root: false) ⇒ Object

get a enumerator to iterate through the Dies present in all CompilationUnits

By default, the Enumerator returned by this method will yield every Die in every CompilationUnit in the order they were declared in. If ‘top` is set to true, the Enumerator will instead only yield those Dies that are children of the root Die in each CompilationUnit.

Parameters:

  • top (Boolean) (defaults to: false)

    set to true to only return the children of the root die

  • root (Boolean) (defaults to: false)

    set to true to yield the root DIE of each CompilationUnit prior to its children



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/elf_utils/section/debug_info.rb', line 54

def dies(top: false, root: false)
  Enumerator.new do |yielder|
    cus.each do |cu|
      if top
        yielder << cu.root_die if root
        cu.root_die.children.each { |die| yielder << die }
      else
        cu.dies.each { |die| yielder << die }
      end
    end
  end
end

#type(name) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/elf_utils/section/debug_info.rb', line 73

def type(name)
  compilation_units.each do |cu|
    type = cu.type(name)
    return type if type
  end
  nil
end

#type_die(name) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/elf_utils/section/debug_info.rb', line 81

def type_die(name)
  compilation_units.each do |cu|
    cu.dies.each do |die|
      return die if die.type_name == name
    end
  end
  nil
end