forked from thanks4opensource/regbits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunittest.py
executable file
·159 lines (141 loc) · 5.25 KB
/
unittest.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
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
#!/usr/bin/env python3
# regbits: C++ templates for type-safe bit manipulation
# Copyright (C) 2019,2020 Mark R. Rubin
#
# This file is part of regbits.
#
# The regbits program 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, either version 3 of the
# License, or (at your option) any later version.
#
# The regbits program 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.
#
# You should have received a copy of the GNU General Public
# License (LICENSE.txt) along with the regbits program. If not, see
# <https://www.gnu.org/licenses/gpl.html>
import os
import sys
architecture = sys.argv[1]
compile_flags = sys.argv[2]
outs = []
funcs = []
found_run = False
with open(sys.argv[3], 'r') as file:
for line in file:
if 'do_test(' in line:
funcs.append(line.split()[0].split('(')[1])
for filename in [name for name in sys.argv[4:] if name.endswith('.out')]:
with open(filename, 'r') as file:
lines = file.readlines()
outs.append((os.path.basename(filename), lines))
sizes = []
times = []
print()
for (name, lines) in outs:
sizes.append(lines[0].split()[1])
times.append(lines[2].split()[1])
if outs:
for line in range(3, len(outs[0][1])):
matched = True
current = outs[0][1][line]
for out in range(1, len(outs)):
if outs[out][1][line] != current:
matched = False
break
current = outs[out][1][line]
if not matched:
print('\t', funcs[int(outs[0][1][line].split()[0])])
for out in range(len(outs)):
print("%-16s\t%s" % (outs[out][0], outs[out][1][line]), end='')
print()
dmp_names = [name for name in sys.argv[4:] if name.endswith('.o.dmp')]
if not dmp_names: sys.exit(0)
dump_sizes = {}
funcs = set()
for dmp_name in dmp_names:
name_sizes = {}
with open(dmp_name, 'r') as file:
size = -1
for line in file:
if line.endswith('>:\n'):
func = line.split()[1]
func = func[1:-len('>:')]
if '(' in func:
func = func[:func.find('(')]
splits = line.split()
if splits[-2] == '[clone':
func += splits[-1][:-3]
funcs.add(func)
size = 0
elif line == '\n' and size > 0:
name_sizes[func] = size
size = -1
elif size >= 0 and len(line) > 15:
if architecture == 'arm':
size += 2 if line[15] == " " else 4
elif architecture == 'intel':
size += len(line[10:30].split())
name_sizes[func] = size
dump_sizes[os.path.basename(dmp_name)[:-len('.o.dmp')]] = name_sizes
print('compile:', " ".join(compile_flags.split()))
for name in ('regbits', 'struct', 'raw', 'bitfield'):
if name in dump_sizes:
print(" %8s" % name, end='')
print()
func_list = list(funcs)
func_list.sort()
for func in func_list:
best = 0xffffffff
for name in ('regbits', 'struct', 'raw', 'bitfield'):
if name in dump_sizes and func in dump_sizes[name]:
if dump_sizes[name][func] < best:
best = dump_sizes[name][func]
for name in ('regbits', 'struct', 'raw', 'bitfield'):
if name in dump_sizes and func in dump_sizes[name]:
size = dump_sizes[name][func]
print( "%8d%s"
% (size, " *" if size == best else " "),
end='')
else:
print(" ", end='')
print(" %s" % func)
print('compile:', " ".join(compile_flags.split()))
for name in ('regbits', 'struct', 'raw', 'bitfield'):
if name in dump_sizes:
print(" %8s" % name, end='')
print()
func_list = list(funcs)
func_list.sort()
for func in func_list:
best = 0xffffffff
for name in ('regbits', 'struct', 'raw', 'bitfield'):
if name in dump_sizes and func in dump_sizes[name]:
if dump_sizes[name][func] < best:
best = dump_sizes[name][func]
if ( func in dump_sizes['regbits']
and func in dump_sizes['struct' ]
and dump_sizes['regbits'][func]
> dump_sizes['struct' ][func] ) \
or ( func in dump_sizes['regbits']
and func in dump_sizes['raw' ]
and dump_sizes['regbits'][func]
> dump_sizes['raw' ][func]):
for name in ('regbits', 'struct', 'raw', 'bitfield'):
if name in dump_sizes and func in dump_sizes[name]:
size = dump_sizes[name][func]
print( "%8d%s"
% (size, " *" if size == best else " "),
end='')
else:
print(" ", end='')
print(" %s" % func)
if outs:
print()
print('compile:', " ".join(compile_flags.split()))
for ndx in range(len(sizes)):
print( "%-16s\tsize: %s\ttime: %s"
% (outs[ndx][0], sizes[ndx], times[ndx]))