heapinfo 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 546784a7f3d841d2505b517a355afc2e1e914e5d
4
- data.tar.gz: 166efbf709763a0a5a5a68d3e0f13515839bc125
3
+ metadata.gz: fd09327165de603d3fa3350e9022d8bf642a1f7c
4
+ data.tar.gz: 5c1dd70e46c5fad709e187c25a8347be8262586e
5
5
  SHA512:
6
- metadata.gz: fb5882bc6a55e02abcfc7f11e646952ee50c1622cb8020103c6f1753163b49984d2be00e320eb0d61d601d3d30eb1a90d5dbd2b8053c83d80c9324159dc80f1e
7
- data.tar.gz: 8789e7be0d5336870fc75100df74c5ea9151c0d4a9f6f7f9553756623915a4eef70b802f4a8511443f8a90322f50dfbfc0b60c697ebaab9400d1491705142901
6
+ metadata.gz: 6827c2c7129162b716046200e71cdba7475de9edad4a1880b0aec374c3e9a5649c6a2ae934566097794671bc1c30d255e4fed9142e6a1de4b95a914f3c8c5ee1
7
+ data.tar.gz: 82bfd1db1bca104fa62d54e7c14a63ad91d2a208dfa346d8a23464141d9767e1f9d18dda21dfd106a840d2799c92c3d4dad2a78893b4bcbc224aa9c415f38692
@@ -1,9 +1,17 @@
1
1
  module HeapInfo
2
+ # Records status of an arena, including bin(s) and top chunk.
2
3
  class Arena
3
- attr_reader :fastbin, :unsorted_bin, :smallbin, :top_chunk
4
+ # @return [Array<HeapInfo::Fastbin>] Fastbins in an array.
5
+ attr_reader :fastbin
6
+ # @return [Array<HeapInfo::UnsortedBin>] The unsorted bin (only one).
7
+ attr_reader :unsorted_bin
8
+ # @return [Array<HeapInfo::Smallbin>] Smallbins in an array.
9
+ attr_reader :smallbin
10
+ # @return [HeapInfo::Chunk] Current top chunk.
11
+ attr_reader :top_chunk
4
12
  # attr_reader :largebin, :last_remainder
5
13
 
6
- # Instantiate a <tt>HeapInfo::Arena</tt> object
14
+ # Instantiate a <tt>HeapInfo::Arena</tt> object.
7
15
  #
8
16
  # @param [Integer] base Base address of arena.
9
17
  # @param [Integer] bits Either 64 or 32
@@ -14,7 +22,7 @@ module HeapInfo
14
22
  reload!
15
23
  end
16
24
 
17
- # Refresh all attributes
25
+ # Refresh all attributes.
18
26
  # Retrive data using <tt>@dumper</tt>, load bins, top chunk etc.
19
27
  # @return [HeapInfo::Arena] self
20
28
  def reload!
@@ -54,11 +62,14 @@ module HeapInfo
54
62
  attr_reader :size_t
55
63
  end
56
64
 
65
+ # Class for record fastbin type chunk.
57
66
  class Fastbin < Chunk
67
+ # @return [Integer]
58
68
  attr_reader :fd
69
+ # @return [Integer]
59
70
  attr_accessor :index
60
71
 
61
- # Instantiate a <tt>HeapInfo::Fastbin</tt> object
72
+ # Instantiate a <tt>HeapInfo::Fastbin</tt> object.
62
73
  #
63
74
  # @param [Mixed] args See <tt>HeapInfo::Chunk</tt> for more information.
64
75
  def initialize(*args)
@@ -66,18 +77,20 @@ module HeapInfo
66
77
  @fd = Helper.unpack(size_t, @data[0, @size_t])
67
78
  end
68
79
 
69
- # Mapping index of fastbin to chunk size
80
+ # Mapping index of fastbin to chunk size.
70
81
  # @return [Integer] size
71
82
  def idx_to_size
72
83
  index * size_t * 2 + size_t * 4
73
84
  end
74
85
 
75
- # For pretty inspect
76
- # @return [String] Title with color codes
86
+ # For pretty inspect.
87
+ # @return [String] Title with color codes.
77
88
  def title
78
89
  "%s%s: " % [Helper.color(Helper.class_name(self), sev: :bin), index.nil? ? nil : "[#{Helper.color("%#x" % idx_to_size)}]"]
79
90
  end
80
91
 
92
+ # Pretty inspect.
93
+ # @return [String] fastbin layouts wrapper with color codes.
81
94
  def inspect
82
95
  title + list.map do |ptr|
83
96
  next "(#{ptr})\n" if ptr.is_a? Symbol
@@ -105,10 +118,14 @@ module HeapInfo
105
118
  ret << nil
106
119
  end
107
120
 
121
+ # @param [Integer] ptr Get the <tt>fd</tt> value of chunk at <tt>ptr</tt>.
122
+ # @return [Integer] The <tt>fd</tt>.
108
123
  def fd_of(ptr)
109
124
  addr_of(ptr, 2)
110
125
  end
111
126
 
127
+ # @param [Integer] ptr Get the <tt>bk</tt> value of chunk at <tt>ptr</tt>.
128
+ # @return [Integer] The <tt>bk</tt>.
112
129
  def bk_of(ptr)
113
130
  addr_of(ptr, 3)
114
131
  end
@@ -121,21 +138,30 @@ module HeapInfo
121
138
  end
122
139
  end
123
140
 
141
+ # Class for record unsorted bin type chunk.
124
142
  class UnsortedBin < Fastbin
143
+ # @return [Integer]
125
144
  attr_reader :bk
145
+
146
+ # Instantiate a <tt>HeapInfo::UnsortedBin</tt> object.
147
+ #
148
+ # @param [Mixed] args See <tt>HeapInfo::Chunk</tt> for more information.
126
149
  def initialize(*args)
127
150
  super
128
151
  @bk = Helper.unpack(size_t, @data[@size_t, @size_t])
129
152
  end
130
153
 
131
- # size: at most extend size
132
- # size=2: bk, bk, bin, fd, fd
154
+ # @option [Integer] size At most expand size. For <tt>size = 2</tt>, the expand list would be <tt>bk, bk, bin, fd, fd</tt>.
155
+ # @return [String] unsorted bin layouts wrapper with color codes.
133
156
  def inspect(size: 2)
134
157
  list = link_list(size)
135
158
  return '' if list.size <= 1 and Helper.class_name(self) != 'UnsortedBin' # bad..
136
159
  title + pretty_list(list) + "\n"
137
160
  end
138
161
 
162
+ # Wrapper the double-linked list with color codes.
163
+ # @param [Array<Integer>] list The list from <tt>#link_list</tt>.
164
+ # @return [String] Wrapper with color codes.
139
165
  def pretty_list(list)
140
166
  center = nil
141
167
  list.map.with_index do |c, idx|
@@ -157,6 +183,11 @@ module HeapInfo
157
183
  end.join(" === ")
158
184
  end
159
185
 
186
+ # Return the double link list with bin in the center.
187
+ #
188
+ # The list will like <tt>[..., bk of bk, bk of bin, bin, fd of bin, fd of fd, ...]</tt>.
189
+ # @param [Integer] expand_size At most expand size. For <tt>size = 2</tt>, the expand list would be <tt>bk, bk, bin, fd, fd</tt>.
190
+ # @return [Array<Integer>] The linked list.
160
191
  def link_list(expand_size)
161
192
  list = [@base]
162
193
  # fd
@@ -178,9 +209,10 @@ module HeapInfo
178
209
  end
179
210
  end
180
211
 
212
+ # Class for record smallbin type chunk.
181
213
  class Smallbin < UnsortedBin
182
214
 
183
- # Mapping index of smallbin to chunk size
215
+ # Mapping index of smallbin to chunk size.
184
216
  # @return [Integer] size
185
217
  def idx_to_size
186
218
  index * size_t * 2 + size_t * 18
@@ -6,6 +6,7 @@ module HeapInfo
6
6
  @chunks = []
7
7
  end
8
8
 
9
+ # Redirect methods to private records.
9
10
  def method_missing(method_sym, *arguments, &block)
10
11
  return super unless @chunks.respond_to? method_sym
11
12
  @chunks.send(method_sym, *arguments, &block)
@@ -3,12 +3,20 @@ module HeapInfo
3
3
  module String
4
4
  # Methods to be mixed into String
5
5
  module InstanceMethods
6
+ # Convert string to a <tt>HeapInfo::Chunk</tt>.
7
+ # @option [Integer] bits 32 or 64 bit of this chunk.
8
+ # @option [Integer] base Base address will show when print the <tt>Chunk</tt> object.
9
+ # @return [HeapInfo::Chunk]
6
10
  def to_chunk(bits: 64, base: 0)
7
11
  size_t = bits / 8
8
12
  dumper = ->(addr, len) { self[addr-base, len] }
9
13
  Chunk.new(size_t, base, dumper)
10
14
  end
11
15
 
16
+ # Convert string to array of <tt>HeapInfo::Chunk</tt>.
17
+ # @option [Integer] bits 32 or 64 bit of this chunk.
18
+ # @option [Integer] base Base address will show when print the <tt>Chunk</tt> object.
19
+ # @return [HeapInfo::Chunks]
12
20
  def to_chunks(bits: 64, base: 0)
13
21
  size_t = bits / 8
14
22
  chunks = Chunks.new
data/lib/heapinfo/libc.rb CHANGED
@@ -1,30 +1,42 @@
1
1
  module HeapInfo
2
+ # Record libc's base, name, and offsets.
2
3
  class Libc < Segment
4
+
5
+ # Instantiate a <tt>HeapInfo::Libc</tt> object.
6
+ #
7
+ # @param [Mixed] args See <tt>#HeapInfo::Segment.initialize</tt> for more information.
3
8
  def initialize(*args)
4
9
  super
5
10
  @offset = {}
6
11
  end
7
12
 
13
+ # Get the offset of <tt>main_arena</tt> in libc.
14
+ # @return [Integer]
8
15
  def main_arena_offset
9
16
  return @offset[:main_arena] if @offset[:main_arena]
10
17
  return nil unless exhaust_search :main_arena
11
18
  @offset[:main_arena]
12
19
  end
13
20
 
21
+ # Get the <tt>main_arena</tt> of libc.
22
+ # @return [HeapInfo::Arena]
14
23
  def main_arena
15
24
  return @main_arena.reload! if @main_arena
16
25
  off = main_arena_offset
17
26
  return if off.nil?
18
- @main_arena = Arena.new(off + self.base, process.bits, ->(*args) { process.dump(*args) })
27
+ @main_arena = Arena.new(off + self.base, process.bits, process.method(:dump))
19
28
  end
20
29
 
30
+ # @param [Array] maps See <tt>#HeapInfo::Segment.find</tt> for more information.
31
+ # @param [String] name See <tt>#HeapInfo::Segment.find</tt> for more information.
32
+ # @param [HeapInfo::Process] process The process.
33
+ # @return [HeapInfo::Libc] libc segment found in maps.
21
34
  def self.find(maps, name, process)
22
35
  obj = super(maps, name)
23
36
  obj.send(:process=, process)
24
37
  obj
25
38
  end
26
39
 
27
-
28
40
  private
29
41
  attr_accessor :process
30
42
  # only for searching offset of main_arena now
@@ -162,7 +162,7 @@ module HeapInfo
162
162
  end
163
163
 
164
164
  def load! # try to load
165
- return if @pid
165
+ return true if @pid
166
166
  @pid = fetch_pid
167
167
  return false if @pid.nil? # still can't load
168
168
  load_info!
@@ -186,6 +186,7 @@ module HeapInfo
186
186
  end
187
187
  @dumper = Dumper.new(@info, mem_filename)
188
188
  end
189
+
189
190
  def mem_filename
190
191
  "/proc/#{pid}/mem"
191
192
  end
@@ -8,7 +8,16 @@ module HeapInfo
8
8
  # e.g. <tt>process.libc alias to process.info.libc</tt>
9
9
  EXPORT = %i(libc ld heap elf program stack bits)
10
10
 
11
- attr_reader :bits, :program, :stack, :libc, :ld
11
+ # @return [Integer] 32 or 64.
12
+ attr_reader :bits
13
+ # @return [HeapInfo::Segment]
14
+ attr_reader :program
15
+ # @return [HeapInfo::Segment]
16
+ attr_reader :stack
17
+ # @return [HeapInfo::Libc]
18
+ attr_reader :libc
19
+ # @return [HeapInfo::Segment]
20
+ attr_reader :ld
12
21
  alias :elf :program
13
22
 
14
23
  # Instantiate a <tt>ProcessInfo</tt> object
@@ -1,4 +1,5 @@
1
1
  module HeapInfo
2
+ # Record the base address and name in maps
2
3
  class Segment
3
4
 
4
5
  # Base address of segment
@@ -1,3 +1,3 @@
1
1
  module HeapInfo
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.0.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: heapinfo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - david942j