Class: MemDump::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/memdump/cli.rb

Instance Method Summary collapse

Instance Method Details

#cleanup_refs(dump, output) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/memdump/cli.rb', line 88

def cleanup_refs(dump, output)
    require 'memdump/cleanup_references'

    STDOUT.sync = true
    dump = MemDump::JSONDump.new(Pathname.new(dump))
    cleaned = MemDump.cleanup_references(dump)
    Pathname.new(output).open('w') do |io|
        cleaned.each do |r|
            io.puts JSON.dump(r)
        end
    end
end

#diff(source, target, output) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/memdump/cli.rb', line 19

def diff(source, target, output)
    from = MemDump::JSONDump.load(source)
    to   = MemDump::JSONDump.load(target)
    diff = from.diff(to)
    STDOUT.sync
    puts "#{diff.size} nodes are in target but not in source"
    diff = to.roots_of(diff)
    puts "#{diff.size} nodes in final dump"
    diff.save(output)
end

#dump(pid, file) ⇒ Object



13
14
15
16
# File 'lib/memdump/cli.rb', line 13

def dump(pid, file)
    file = File.expand_path(file)
    system('rbtrace', '-p', pid.to_s, '-e', "require \"objspace\"; File.open(\"#{file}\", 'w') { |io| ObjectSpace.dump_all(output: io) }")
end

#enable_allocation_trace(pid) ⇒ Object



8
9
10
# File 'lib/memdump/cli.rb', line 8

def enable_allocation_trace(pid)
    system('rbtrace', '-p', pid.to_s, '-e', "require \"objspace\"; ObjectSpace.trace_object_allocations_start")
end

#gml(dump_path, gml_path = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/memdump/cli.rb', line 31

def gml(dump_path, gml_path = nil)
    require 'memdump/convert_to_gml'

    STDOUT.sync = true
    dump_path = Pathname.new(dump_path)
    if gml_path
        gml_path = Pathname.new(gml_path)
    else
        gml_path = dump_path.sub_ext('.gml')
    end

    dump = MemDump::JSONDump.new(dump_path)
    gml_path.open('w') do |io|
        MemDump.convert_to_gml(dump, io)
    end
end

#interactive(dump) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/memdump/cli.rb', line 141

def interactive(dump)
    require 'memdump'
    require 'pry'
    dump = MemDump::JSONDump.new(Pathname.new(dump))
    if options[:load]
        dump = dump.load
    end
    dump.pry
end

#out_degree(dump) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/memdump/cli.rb', line 128

def out_degree(dump)
    dump = MemDump::JSONDump.new(Pathname.new(dump))
    min = options[:min] || 0
    sorted = dump.each_record.sort_by { |r| (r['references'] || Array.new).size }
    sorted.each do |r|
        size = (r['references'] || Array.new).size
        break if size > min
        puts "#{size} #{r}"
    end
end

#remove_node(dump, node_id) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/memdump/cli.rb', line 102

def remove_node(dump, node_id)
    require 'memdump/remove_node'

    STDOUT.sync = true
    dump = MemDump::JSONDump.new(Pathname.new(dump))
    cleaned = MemDump.remove_node(dump, node_id)
    cleaned.each do |r|
        STDOUT.puts JSON.dump(r)
    end
end

#replace_class(dump_path, output_path = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/memdump/cli.rb', line 73

def replace_class(dump_path, output_path = nil)
    require 'memdump/replace_class_address_by_name'

    STDOUT.sync = true
    dump_path = Pathname.new(dump_path)
    output_path =
        if output_path then Pathname.new(output_path)
        else dump_path
        end
    dump = MemDump::JSONDump.load(dump_path)
    dump = dump.replace_class_id_by_class_name(add_reference_to_class: options[:add_ref])
    dump.save(output_path)
end

#root_of(dump, address) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/memdump/cli.rb', line 61

def root_of(dump, address)
    require 'memdump/root_of'

    STDOUT.sync = true
    dump = MemDump::JSONDump.new(Pathname.new(dump))
    MemDump.root_of(dump, address).each do |r|
        puts JSON.dump(r)
    end
end

#stats(dump) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/memdump/cli.rb', line 114

def stats(dump)
    require 'pp'
    require 'memdump/stats'
    dump = MemDump::JSONDump.load(dump)
    unknown, by_type = dump.stats
    puts "#{unknown} objects without a known type"
    by_type.sort_by { |n, v| v }.reverse.each do |n, v|
        puts "#{n}: #{v}"
    end
end

#subgraph_of(dump, address) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/memdump/cli.rb', line 50

def subgraph_of(dump, address)
    require 'memdump/subgraph_of'

    STDOUT.sync = true
    dump = MemDump::JSONDump.new(Pathname.new(dump))
    MemDump.subgraph_of(dump, address, max_depth: options[:max_depth]).each do |r|
        puts JSON.dump(r)
    end
end