forked from acmel/dwarves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regtest
executable file
·282 lines (253 loc) · 8.06 KB
/
regtest
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#! /usr/bin/python
# -*- python -*-
# -*- coding: utf-8 -*-
# tuna - Application Tuning GUI
# Copyright (C) 2009 Arnaldo Carvalho de Melo
# Arnaldo Carvalho de Melo <[email protected]>
#
# This application is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2.
#
# This application is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
import filecmp, getopt, os, posix, signal, sys, tempfile
regtest_output_dir = "/media/tb/pahole/regtest/"
regtest_obj_dir = "/media/tb/debuginfo/usr/lib/debug/"
tools = {"pahole": { "dwarf": "--flat_arrays --show_private_classes --fixup_silly_bitfields --first_obj_only --classes_as_structs" }}
all_formats = ("ctf", "dwarf")
formats = all_formats
len_debug_dir = len(regtest_obj_dir)
verbose = 1
# Turn this on when testing CTF generated files
use_options = False
def diff_file(from_filename, to_filename):
fd, diff_filename = tempfile.mkstemp()
command = 'diff -up "%s" "%s" > %s' % (from_filename,
to_filename, diff_filename)
if verbose > 1:
print command
try:
os.system(command)
os.system("vim %s" % diff_filename)
finally:
os.unlink(diff_filename)
def dir_has_no_diffs(dirname):
return os.access(os.path.join(dirname, ".no_diffs"), os.F_OK)
def set_dir_has_no_diffs(dirname):
f = file(os.path.join(dirname, ".no_diffs"), "w")
f.close()
def reset_dir_has_no_diffs(dirname):
os.unlink(os.path.join(dirname, ".no_diffs"))
def diff_dir(from_dir, to_dir, dir = None, recursive = True):
if dir:
from_dir = os.path.join(from_dir, dir)
to_dir = os.path.join(to_dir, dir)
print "\r%-130s" % from_dir
sys.stdout.flush()
diff = filecmp.dircmp(from_dir, to_dir)
if not dir_has_no_diffs(to_dir):
diff_files = diff.diff_files
if diff_files:
diff_files.sort()
print "\n %s" % from_dir
sys.stdout.flush()
for f in diff_files:
diff_file(os.path.join(from_dir, f),
os.path.join(to_dir, f))
else:
set_dir_has_no_diffs(to_dir)
if not recursive:
return
common_dirs = diff.common_dirs
if not common_dirs:
return
common_dirs.sort()
for dir in common_dirs:
diff_dir(from_dir, to_dir, dir)
def do_diff_dwarfs2ctfs():
diff_dir(os.path.join(regtest_output_dir, "after", "pahole", "dwarf"),
os.path.join(regtest_output_dir, "after", "pahole", "ctf"))
def do_diff_dwarfs():
diff_dir(os.path.join(regtest_output_dir, "before", "pahole", "dwarf"),
os.path.join(regtest_output_dir, "after", "pahole", "dwarf"))
def do_tool(tool, before_after, format, dirname, fname,
prepend_obj_dir = False):
if prepend_obj_dir:
fname += ".debug"
fixed_dirname = dirname
else:
fixed_dirname = dirname[len_debug_dir:]
tool_output_dir = os.path.join(regtest_output_dir,
before_after, tool, format,
fixed_dirname)
obj_path = os.path.join(dirname, fname)
if prepend_obj_dir:
obj_path = os.path.join(regtest_obj_dir, obj_path)
if os.path.islink(obj_path) or os.path.isdir(obj_path):
return
try:
os.makedirs(tool_output_dir)
except:
pass
if dir_has_no_diffs(tool_output_dir):
reset_dir_has_no_diffs(tool_output_dir)
output_file = os.path.join(tool_output_dir, fname[:-6])
if use_options and tools[tool].has_key(format):
options = tools[tool][format]
else:
options = ""
command = '%s -F %s %s %s > "%s"' % (tool, format, options,
obj_path, output_file)
if verbose > 1:
print command
sys.stdout.flush()
elif verbose > 0:
print "%s: %s" % (format,
os.path.join(fixed_dirname, fname[:-6]))
os.system(command)
def do_tool_on_files(arg, dirname, fnames, prepend_obj_dir = False):
if dirname.find("/.") >= 0:
return
tool, before_after = arg
for fname in fnames:
if not prepend_obj_dir and fname[-6:] != ".debug":
continue
for format in formats:
do_tool(tool, before_after, format, dirname, fname,
prepend_obj_dir)
def do_tools(before_after):
for tool in tools.keys():
os.path.walk(regtest_obj_dir, do_tool_on_files, (tool, before_after))
def do_ctf(dirname, fname, prepend_obj_dir = False):
if prepend_obj_dir:
fname += ".debug"
fixed_dirname = dirname
else:
fixed_dirname = dirname[len_debug_dir:]
obj_path = os.path.join(dirname, fname)
if prepend_obj_dir:
obj_path = os.path.join(regtest_obj_dir, obj_path)
if os.path.islink(obj_path) or os.path.isdir(obj_path):
return
command = 'pahole -Z "%s" 2> /dev/null' % obj_path
if verbose > 1:
print command
elif verbose > 0:
print os.path.join(fixed_dirname, fname[:-6])
os.system(command)
def do_ctf_on_files(arg, dirname, fnames, prepend_obj_dir = False):
if dirname.find("/.") >= 0:
return
for fname in fnames:
if not prepend_obj_dir and fname[-6:] != ".debug":
continue
do_ctf(dirname, fname, prepend_obj_dir)
def do_ctfs():
os.path.walk(regtest_obj_dir, do_ctf_on_files, None)
def sig_exit(sig_number, stack_frame):
sys.exit(1)
def listdebugs(dirname):
fnames = []
for fname in os.listdir(os.path.join(regtest_obj_dir, dirname)):
if fname[-6:] != ".debug":
continue
obj_path = os.path.join(regtest_obj_dir, dirname, fname)
if os.path.islink(obj_path) or os.path.isdir(obj_path):
continue
fnames.append(fname[:-6])
return fnames
def usage():
print 'Usage: regtest [OPTIONS]'
fmt = '\t%-20s %s'
print fmt % ('-h, --help', 'Give this help list')
print fmt % ('-a, --after', 'Generate new output')
print fmt % ('-b, --before', 'Generate old output')
print fmt % ('-c, --ctf_diff', 'Diff between DWARF and CTF for new output')
print fmt % ('-C, --ctf_encode', 'Encode CTF into object files')
print fmt % ('-d, --diff', 'Diff between old and new output')
print fmt % ('-f, --formats', 'formats used (default: %s)' ','.join(formats))
def main(argv):
global formats
for sig in (signal.SIGHUP, signal.SIGINT, signal.SIGTERM):
signal.signal(sig, sig_exit)
try:
short = "habcCdf:"
long = ("help", "after", "before", "ctf_diff", "ctf_encode",
"diff", "formats")
opts, args = getopt.getopt(sys.argv[1:], short, long)
except getopt.GetoptError, err:
usage()
print str(err)
sys.exit(2)
for o, a in opts:
if o in ("-h", "--help"):
usage()
return
elif o in ("-f", "--formats"):
formats = a.split(',')
elif o in ("-a", "--after",
"-b", "--before",
"-c", "--ctf_diff",
"-C", "--ctf_encode",
"-d", "--diff"):
if len(args) > 0:
dirname = args[0]
if len(args) > 1:
fnames = args[1:]
elif o in ('-a', '--after',
'-b', '--before',
'-C', '--ctf_encode'):
fnames = listdebugs(dirname)
if o in ('-b', '--before', '-a', '--after'):
if o in ('-b', '--before'):
when = 'before'
else:
when = 'after'
if len(args) > 0:
for tool in tools.keys():
arg = (tool, when)
do_tool_on_files(arg, dirname, fnames, True)
else:
do_tools(when)
elif o in ('-d', '--diff'):
if len(args) > 0:
from_dir = os.path.join(regtest_output_dir,
"before", "pahole",
"dwarf", dirname)
to_dir = os.path.join(regtest_output_dir,
"after", "pahole",
"dwarf", dirname)
if len(args) > 1:
for fname in fnames:
diff_file(os.path.join(from_dir, fname),
os.path.join(to_dir, fname))
else:
diff_dir(from_dir, to_dir, recursive = False)
else:
do_diff_dwarfs()
elif o in ('-C', 'ctf'):
if len(args) > 0:
do_ctf_on_files(None, dirname, fnames, True)
else:
do_ctfs()
elif o in ('-c', 'ctf_diff'):
if len(args) > 0:
from_dir = os.path.join(regtest_output_dir,
"after", "pahole",
"dwarf", dirname)
to_dir = os.path.join(regtest_output_dir,
"after", "pahole",
"ctf", dirname)
if len(args) > 1:
for fname in fnames:
diff_file(os.path.join(from_dir, fname),
os.path.join(to_dir, fname))
else:
diff_dir(from_dir, to_dir, recursive = False)
else:
do_diff_dwarfs2ctfs()
if __name__ == '__main__':
main(sys.argv)