-
Notifications
You must be signed in to change notification settings - Fork 1
/
diff.py
executable file
·113 lines (77 loc) · 2.65 KB
/
diff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
import glob
import shutil
import os
import datetime
import sys
import re
import code
import pprint
class MemoryMapping:
def __init__(self, address_start, address_end, perm_r, perm_w, perm_x, perm_s, perm_p, offset, dev, inode, pathname):
self.address_start = address_start
self.address_end = address_end
self.perm_read = perm_r
self.perm_write = perm_w
self.perm_execute = perm_x
self.perm_shared = perm_s
self.perm_private = perm_p
self.offset = offset
self.dev = dev
self.inode = inode
self.pathname = pathname
RE_STATUS_FILE = re.compile(r'^([a-zA-Z_]+):\t+(.+)$')
RE_SMAPS_MEM_REGION = re.compile(r'([a-f0-9]+)-([a-f0-9]+ (.)(.)(.)(.) ([a-f0-9]+ ([a-f0-9]{2}:[a-f0-9]{2}) (\d+) +(\S+)?')
def strings(s):
return s.split('\x00')
def read(path):
return open(path).read().strip()
def parse_smaps_file(smaps_file):
for line in read(smaps_file).splitlines():
m = RE_SMAPS_MEM_REGION.match(line)
if m:
offset_start = int(m.group(1), 16)
offset_end = int(m.group(2), 16)
perm_r = m.group(3) == 'r'
perm_w = m.group(4) == 'w'
perm_x = m.group(5) == 'x'
perm_s = m.group(6) == 's'
perm_p = m.group(6) == 'p'
offset = int(m.group(7), 16)
dev = m.group(8)
inode = int(m.group(9))
if m.group(10) == None:
pathname = None
else:
pathname = m.group(10)
print(m.group(10))
memory_mapping = MemoryMapping(offset_start, offset_end, perm_r, perm_w, perm_x, perm_s, perm_p, offset, dev, inode, pathname)
def load_proc_entry(proc_entry):
cmdline = strings(read(proc_entry + '/cmdline'))
smaps = parse_smaps_file(proc_entry + '/smaps')
return {
'cmdline': cmdline,
'smaps': smaps,
}
def unarchive_snap(archive):
if not os.path.exists(archive.replace('.tar.gz', '')):
shutil.unpack_archive(archive)
def load_snap(snap_path):
snap = {}
snap['cmdline'] = read(snap_path + '/proc/cmdline')
snap['processes'] = {}
for proc_entry in glob.glob(snap_path + '/proc/[0-9]*'):
pid = proc_entry.replace(snap_path + '/proc/', '')
proc = load_proc_entry(proc_entry)
snap['processes'][pid] = proc
#pprint.pprint(snap)
try:
a = sys.argv[1]
b = sys.argv[2]
except:
print('Usage: ' + sys.argv[0] + ' <memory_snap_1> <memory_snap_2>')
sys.exit(1)
unarchive_snap(a)
snap_a = a.replace('.tar.gz', '')
a = load_snap(snap_a)
#b = load_snap(b)