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
|
#!/usr/bin/env python2
"""Verify that a ChromeOS sub-tree was built with a particular compiler"""
from __future__ import print_function
import argparse
import os
import sys
from cros_utils import command_executer
COMPILERS = ['gcc', 'clang']
COMPILER_STRINGS = {'gcc': 'GNU C', 'clang': 'clang version'}
ERR_NO_DEBUG_INFO = 1024
def UsageError(parser, message):
"""Output error message and help/usage info."""
print('ERROR: %s' % message)
parser.print_help()
sys.exit(0)
def CreateTmpDwarfFile(filename, dwarf_file, cmd_executer):
"""Create temporary dwarfdump file, to be parsed later."""
cmd = ('readelf --debug-dump=info %s | grep -A5 DW_AT_producer > %s' %
(filename, dwarf_file))
retval = cmd_executer.RunCommand(cmd, print_to_console=False)
return retval
def FindAllFiles(root_dir, cmd_executer):
"""Create a list of all the *.debug and *.dwp files to be checked."""
file_list = []
tmp_list = [
os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(root_dir)
for f in fnmatch.filter(files, '*.debug')
]
for f in tmp_list:
if 'build-id' not in f:
file_list.append(f)
tmp_list = [
os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(root_dir)
for f in fnmatch.filter(files, '*.dwp')
]
file_list += tmp_list
return file_list
def VerifyArgs(compiler, filename, tmp_dir, root_dir, options, parser):
"""Verify that the option values and combinations are valid."""
if options.filename and options.all_files:
UsageError(parser, 'Cannot use both --file and --all_files.')
if options.filename and options.root_dir:
UsageError(parser, 'Cannot use both --file and --root_dir.')
if options.all_files and not options.root_dir:
UsageError(parser, 'Missing --root_dir option.')
if options.root_dir and not options.all_files:
UsageError(parser, 'Missing --all_files option.')
if not options.filename and not options.all_files:
UsageError(parser, 'Must specify either --file or --all_files.')
# Verify that the values specified are valid.
if filename:
if not os.path.exists(filename):
UsageError(parser, 'Cannot find %s' % filename)
compiler = options.compiler.lower()
if compiler not in COMPILERS:
UsageError(parser, '%s is not a valid compiler (gcc or clang).' % compiler)
if root_dir and not os.path.exists(root_dir):
UsageError(parser, '%s does not exist.' % root_dir)
if not os.path.exists(tmp_dir):
os.makedirs(tmp_dir)
def CheckFile(filename, compiler, tmp_dir, options, cmd_executer):
"""Verify the information in a single file."""
print('Checking %s' % filename)
# Verify that file contains debug information.
cmd = 'readelf -SW %s | grep debug_info' % filename
retval = cmd_executer.RunCommand(cmd, print_to_console=False)
if retval != 0:
print('No debug info in this file. Unable to verify compiler.')
# There's no debug info in this file, so skip it.
return ERR_NO_DEBUG_INFO
tmp_name = os.path.basename(filename) + '.dwarf'
dwarf_file = os.path.join(tmp_dir, tmp_name)
status = CreateTmpDwarfFile(filename, dwarf_file, cmd_executer)
if status != 0:
print('Unable to create dwarf file for %s (status: %d).' %
(filename, status))
return status
comp_str = COMPILER_STRINGS[compiler]
retval = 0
with open(dwarf_file, 'r') as in_file:
lines = in_file.readlines()
looking_for_name = False
for line in lines:
if 'DW_AT_producer' in line:
looking_for_name = False
if 'GNU AS' in line:
continue
if comp_str not in line:
looking_for_name = True
retval = 1
elif looking_for_name:
if 'DW_AT_name' in line:
words = line.split(':')
bad_file = words[-1]
print('FAIL: %s was not compiled with %s.' %
(bad_file.rstrip(), compiler))
looking_for_name = False
elif 'DW_TAG_' in line:
looking_for_name = False
if not options.keep_file:
os.remove(dwarf_file)
return retval
def Main(argv):
cmd_executer = command_executer.GetCommandExecuter()
parser = argparse.ArgumentParser()
parser.add_argument(
'--file', dest='filename', help='Name of file to be verified.')
parser.add_argument(
'--compiler',
dest='compiler',
required=True,
help='Desired compiler (gcc or clang)')
parser.add_argument(
'--tmp_dir',
dest='tmp_dir',
help='Directory in which to put dwarf dump file.'
' Defaults to /tmp')
parser.add_argument(
'--keep_file',
dest='keep_file',
default=False,
action='store_true',
help='Do not delete dwarf file when done.')
parser.add_argument(
'--all_files',
dest='all_files',
default=False,
action='store_true',
help='Find and check ALL .debug/.dwp files '
'in subtree. Must be used with --root_dir '
'(and NOT with --file).')
parser.add_argument(
'--root_dir',
dest='root_dir',
help='Root of subtree in which to look for '
'files. Must be used with --all_files, and'
' not with --file.')
options = parser.parse_args(argv)
compiler = options.compiler
filename = None
if options.filename:
filename = os.path.realpath(os.path.expanduser(options.filename))
tmp_dir = '/tmp'
if options.tmp_dir:
tmp_dir = os.path.realpath(os.path.expanduser(options.tmp_dir))
root_dir = None
if options.root_dir:
root_dir = os.path.realpath(os.path.expanduser(options.root_dir))
VerifyArgs(compiler, filename, tmp_dir, root_dir, options, parser)
file_list = []
if filename:
file_list.append(filename)
else:
file_list = FindAllFiles(root_dir, cmd_executer)
bad_files = []
unknown_files = []
all_pass = True
for f in file_list:
result = CheckFile(f, compiler, tmp_dir, options, cmd_executer)
if result == ERR_NO_DEBUG_INFO:
unknown_files.append(f)
all_pass = False
elif result != 0:
bad_files.append(f)
all_pass = False
if all_pass:
print('\n\nSUCCESS: All compilation units were compiled with %s.\n' %
compiler)
return 0
else:
if len(bad_files) == 0:
print(
'\n\n*Mostly* SUCCESS: All files that could be checked were compiled '
'with %s.' % compiler)
print(
'\n\nUnable to verify the following files (no debug info in them):\n')
for f in unknown_files:
print(f)
else:
print('\n\nFAILED: The following files were not compiled with %s:\n' %
compiler)
for f in bad_files:
print(f)
if len(unknown_files) > 0:
print(
'\n\nUnable to verify the following files (no debug info in them):\n'
)
for f in unknown_files:
print(f)
return 1
if __name__ == '__main__':
sys.exit(Main(sys.argv[1:]))
|