Module: HeapInfo::Helper::ClassMethods
- Included in:
- HeapInfo::Helper
- Defined in:
- lib/heapinfo/helper.rb
Overview
Define class methods here.
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
Instance Method Summary collapse
-
#class_name(obj) ⇒ String
Retrieve pure class name(without module) of an object.
-
#color(s, sev: nil) ⇒ String
Wrapper color codes for pretty inspect.
-
#evaluate(formula, store: {}) ⇒ Integer
Safe-eval using dentaku.
-
#hex(num) ⇒ String
Convert number in hex format.
-
#integer?(str) ⇒ Boolean
For checking a string is actually an integer.
-
#parse_maps(content) ⇒ Array
Parse the contents of
/proc/[pid]/maps. -
#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.
Instance Method Details
#class_name(obj) ⇒ String
Retrieve pure class name(without module) of an object.
128 129 130 |
# File 'lib/heapinfo/helper.rb', line 128 def class_name(obj) obj.class.name.split('::').last || obj.class.name end |
#color(s, sev: nil) ⇒ String
Wrapper color codes for pretty inspect.
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/heapinfo/helper.rb', line 83 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 |
#evaluate(formula, store: {}) ⇒ Integer
Safe-eval using dentaku.
152 153 154 155 156 |
# File 'lib/heapinfo/helper.rb', line 152 def evaluate(formula, store: {}) calc = Dentaku::Calculator.new formula = formula.delete(':') calc.store(store).evaluate(formula) end |
#hex(num) ⇒ String
Convert number in hex format.
116 117 118 119 |
# File 'lib/heapinfo/helper.rb', line 116 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.
142 143 144 145 146 |
# File 'lib/heapinfo/helper.rb', line 142 def integer?(str) true if Integer(str) rescue ArgumentError, TypeError false end |
#parse_maps(content) ⇒ Array
Parse the contents of /proc/[pid]/maps.
50 51 52 53 54 55 56 57 58 |
# File 'lib/heapinfo/helper.rb', line 50 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 end.compact end |
#pidof(prog) ⇒ Integer
Get the process id from program name.
When multiple processes exist, the one with lastest start time would be returned.
27 28 29 30 31 32 33 34 |
# File 'lib/heapinfo/helper.rb', line 27 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_by(&:first).last end |
#tempfile(name) ⇒ String
Get temp filename.
161 162 163 |
# File 'lib/heapinfo/helper.rb', line 161 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.
63 64 65 |
# File 'lib/heapinfo/helper.rb', line 63 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.
106 107 108 |
# File 'lib/heapinfo/helper.rb', line 106 def unpack(size_t, data) data.unpack(size_t == 4 ? 'L*' : 'Q*')[0] end |