Module: HeapInfo::Helper
- Defined in:
- lib/heapinfo/helper.rb
Overview
Some helper functions.
Constant Summary collapse
- COLOR_CODE =
Color codes for pretty print.
{ esc_m: "\e[0m", normal_s: "\e[38;5;209m", # red integer: "\e[38;5;153m", # light blue fatal: "\e[38;5;197m", # dark red bin: "\e[38;5;120m", # light green klass: "\e[38;5;155m", sym: "\e[38;5;230m" }.freeze
Class Method Summary collapse
-
.auxv_of(pid) ⇒ String
Fetch the content of /proc/+pid+/auxv.
-
.class_name(obj) ⇒ String
Retrieve pure class name(without module) of an object.
-
.color(s, sev: nil) ⇒ String
Wrapper color codes for pretty inspect.
-
.color_hex(num) ⇒ String
Combines Helper.hex and Helper.color.
-
.evaluate(formula, store: {}) ⇒ Integer
Safe-eval using dentaku.
-
.exe_of(pid) ⇒ String
Fetch the content of /proc/+pid+/exe.
-
.hex(num) ⇒ String
Convert number in hex format.
-
.integer?(str) ⇒ Boolean
For checking a string is actually an integer.
-
.maps_of(pid) ⇒ String
Fetch the content of /proc/+pid+/maps.
-
.parse_maps(content) ⇒ Array
Parse the contents of /proc/[pid]/maps.
-
.parsed_maps(pid) ⇒ Array
Combines Helper.parse_maps and Helper.maps_of.
-
.pidof(prog) ⇒ Integer
Get the process id from program name.
-
.tempfile(name) ⇒ String
Get temp filename.
-
.toggle_color(on: false) ⇒ void
enable / disable the color function.
-
.unpack(size_t, data) ⇒ Integer
Unpack strings to integer.
Class Method Details
.auxv_of(pid) ⇒ String
Fetch the content of /proc/+pid+/auxv.
|
|
# File 'lib/heapinfo/helper.rb', line 13
|
.class_name(obj) ⇒ String
Retrieve pure class name(without module) of an object.
170 171 172 |
# File 'lib/heapinfo/helper.rb', line 170 def class_name(obj) obj.class.name.split('::').last || obj.class.name end |
.color(s, sev: nil) ⇒ String
Wrapper color codes for pretty inspect.
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/heapinfo/helper.rb', line 112 def color(s, sev: nil) s = s.to_s return s if @disable_color cc = COLOR_CODE color = if cc.key?(sev) then cc[sev] elsif integer?(s) then cc[:integer] # integers else cc[:normal_s] # normal string end "#{color}#{s.sub(cc[:esc_m], color)}#{cc[:esc_m]}" end |
.color_hex(num) ⇒ String
159 160 161 |
# File 'lib/heapinfo/helper.rb', line 159 def color_hex(num) color(hex(num)) end |
.evaluate(formula, store: {}) ⇒ Integer
Safe-eval using dentaku.
194 195 196 197 198 |
# File 'lib/heapinfo/helper.rb', line 194 def evaluate(formula, store: {}) calc = Dentaku::Calculator.new formula = formula.delete(':') calc.store(store).evaluate(formula) end |
.exe_of(pid) ⇒ String
Fetch the content of /proc/+pid+/exe.
|
|
# File 'lib/heapinfo/helper.rb', line 13
|
.hex(num) ⇒ String
Convert number in hex format.
146 147 148 149 150 |
# File 'lib/heapinfo/helper.rb', line 146 def hex(num) return format('0x%x', num) if num >= 0 format('-0x%x', -num) end |
.integer?(str) ⇒ Boolean
For checking a string is actually an integer.
184 185 186 187 188 |
# File 'lib/heapinfo/helper.rb', line 184 def integer?(str) true if Integer(str) rescue ArgumentError, TypeError false end |
.maps_of(pid) ⇒ String
Fetch the content of /proc/+pid+/maps.
|
|
# File 'lib/heapinfo/helper.rb', line 13
|
.parse_maps(content) ⇒ Array
Parse the contents of /proc/[pid]/maps.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/heapinfo/helper.rb', line 65 def parse_maps(content) lines = content.split("\n") lines.map do |line| s = line.scan(%r{^([0-9a-f]+)-([0-9a-f]+)\s([rwxp-]{4})[^/|\[]*([/|\[].+)?$})[0] next nil if s.nil? s[0], s[1] = s[0, 2].map { |h| h.to_i(16) } s[3] ||= '' # some segments don't have a name s end.compact end |
.parsed_maps(pid) ⇒ Array
Combines parse_maps and maps_of.
85 86 87 |
# File 'lib/heapinfo/helper.rb', line 85 def parsed_maps(pid) parse_maps(maps_of(pid)) end |
.pidof(prog) ⇒ Integer
Get the process id from program name.
When multiple processes exist, the one with the last start time would be returned.
42 43 44 45 46 47 48 49 50 |
# File 'lib/heapinfo/helper.rb', line 42 def pidof(prog) info = %x(ps -o pid=,lstart= --pid `pidof #{Shellwords.escape(prog)}` 2>/dev/null).lines.map do |l| pid, time = l.split(' ', 2) [Time.parse(time), pid.to_i] end return nil if info.empty? # process not exists yet info.max.last end |
.tempfile(name) ⇒ String
Get temp filename.
203 204 205 |
# File 'lib/heapinfo/helper.rb', line 203 def tempfile(name) Dir::Tmpname.create(name, HeapInfo::TMP_DIR) {} end |
.toggle_color(on: false) ⇒ void
This method returns an undefined value.
enable / disable the color function.
92 93 94 |
# File 'lib/heapinfo/helper.rb', line 92 def toggle_color(on: false) @disable_color = !on end |
.unpack(size_t, data) ⇒ Integer
Unpack strings to integer.
Like the p32 and p64 in pwntools.
136 137 138 |
# File 'lib/heapinfo/helper.rb', line 136 def unpack(size_t, data) data.unpack(size_t == 4 ? 'L*' : 'Q*')[0] end |