Class: HeapInfo::Segment

Inherits:
Object
  • Object
show all
Defined in:
lib/heapinfo/segment.rb

Overview

Record the base address and name in maps

Direct Known Subclasses

Libc

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, name) ⇒ Segment

Instantiate a HeapInfo::Segment object

Parameters:

  • base (Integer) —

    Base address

  • name (String) —

    Name of segment



16
17
18
19
# File 'lib/heapinfo/segment.rb', line 16

def initialize(base, name)
  @base = base
  @name = name
end

Instance Attribute Details

#base ⇒ Object (readonly)

Base address of segment



10
11
12
# File 'lib/heapinfo/segment.rb', line 10

def base
  @base
end

#name ⇒ Object (readonly)

Name of segment



12
13
14
# File 'lib/heapinfo/segment.rb', line 12

def name
  @name
end

Class Method Details

.find(maps, pattern) ⇒ HeapInfo::Segment?

Helper for creating a HeapInfo::Segment.

Search the specific pattern in maps and return a HeapInfo::Segment object.

Parameters:

  • maps (Array) —

    maps is in form of the return value of Helper.parse_maps.

  • pattern (Regexp, String) —

    The segment name want to match in maps. If String is given, the pattern is matched as a substring.

Returns:



47
48
49
50
51
52
53
54
55
# File 'lib/heapinfo/segment.rb', line 47

def self.find(maps, pattern)
  return Nil.instance if pattern.nil?

  needs = maps.select { |m| pattern.is_a?(Regexp) ? m[3] =~ pattern : m[3].include?(pattern) }
  # Use the last heap segment when multiple of them present
  return new(needs.map(&:first).max, needs[0][3]) if pattern == '[heap]'

  new(needs.map(&:first).min, needs[0][3]) unless needs.empty?
end

Instance Method Details

#coerce(other) ⇒ (Object, Integer)

To support addr - h.libc. Treat all operations are manipulating on base.

Parameters:

  • other (Object) —

    Any object.

Returns:

  • ((Object, Integer))


34
35
36
# File 'lib/heapinfo/segment.rb', line 34

def coerce(other)
  [other, base]
end

#to_s ⇒ String

Hook #to_s for pretty printing

Returns:

  • (String) —

    Information of name and base address wrapper with color codes.



23
24
25
# File 'lib/heapinfo/segment.rb', line 23

def to_s
  format("%-28s\tbase @ #{Helper.color(format('%#x', base))}\n", Helper.color(name.split('/')[-1]))
end