2012-08-31 05:21:24 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2019-03-26 22:46:15 +08:00
|
|
|
import subprocess
|
2012-08-31 05:21:24 +08:00
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-08-31 05:21:24 +08:00
|
|
|
def extract_exe_symbol_names(arch, exe_path, match_str):
|
|
|
|
command = 'dsymutil --arch %s -s "%s" | grep "%s" | colrm 1 69' % (
|
|
|
|
arch, exe_path, match_str)
|
2019-03-26 22:46:15 +08:00
|
|
|
(command_exit_status, command_output) = subprocess.getstatusoutput(command)
|
2012-08-31 05:21:24 +08:00
|
|
|
if command_exit_status == 0:
|
|
|
|
if command_output:
|
|
|
|
return command_output[0:-1].split("'\n")
|
|
|
|
else:
|
2019-03-22 02:27:40 +08:00
|
|
|
print('error: command returned no output')
|
2012-08-31 05:21:24 +08:00
|
|
|
else:
|
2019-03-22 02:27:40 +08:00
|
|
|
print('error: command failed with exit status %i\n command: %s' % (command_exit_status, command))
|
2012-08-31 05:21:24 +08:00
|
|
|
return list()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-08-31 05:21:24 +08:00
|
|
|
def verify_api(all_args):
|
|
|
|
'''Verify the API in the specified library is valid given one or more binaries.'''
|
|
|
|
usage = "usage: verify_api --library <path> [ --library <path> ...] executable1 [executable2 ...]"
|
|
|
|
description = '''Verify the API in the specified library is valid given one or more binaries.
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-08-31 05:21:24 +08:00
|
|
|
Example:
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-08-31 05:21:24 +08:00
|
|
|
verify_api.py --library ~/Documents/src/lldb/build/Debug/LLDB.framework/LLDB --arch x86_64 /Applications/Xcode.app/Contents/PlugIns/DebuggerLLDB.ideplugin/Contents/MacOS/DebuggerLLDB --api-regex lldb
|
|
|
|
'''
|
|
|
|
parser = optparse.OptionParser(
|
|
|
|
description=description,
|
|
|
|
prog='verify_api',
|
|
|
|
usage=usage)
|
|
|
|
parser.add_option(
|
|
|
|
'-v',
|
|
|
|
'--verbose',
|
|
|
|
action='store_true',
|
|
|
|
dest='verbose',
|
|
|
|
help='display verbose debug info',
|
|
|
|
default=False)
|
|
|
|
parser.add_option(
|
|
|
|
'-a',
|
|
|
|
'--arch',
|
|
|
|
type='string',
|
|
|
|
action='append',
|
|
|
|
dest='archs',
|
|
|
|
help='architecure to use when checking the api')
|
|
|
|
parser.add_option(
|
|
|
|
'-r',
|
|
|
|
'--api-regex',
|
|
|
|
type='string',
|
|
|
|
dest='api_regex_str',
|
|
|
|
help='Exclude any undefined symbols that do not match this regular expression when searching for missing APIs.')
|
|
|
|
parser.add_option(
|
|
|
|
'-l',
|
|
|
|
'--library',
|
|
|
|
type='string',
|
|
|
|
action='append',
|
|
|
|
dest='libraries',
|
|
|
|
help='Specify one or more libraries that will contain all needed APIs for the executables.')
|
|
|
|
(options, args) = parser.parse_args(all_args)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-08-31 05:21:24 +08:00
|
|
|
api_external_symbols = list()
|
|
|
|
if options.archs:
|
|
|
|
for arch in options.archs:
|
|
|
|
for library in options.libraries:
|
|
|
|
external_symbols = extract_exe_symbol_names(
|
|
|
|
arch, library, "( SECT EXT)")
|
|
|
|
if external_symbols:
|
|
|
|
for external_symbol in external_symbols:
|
|
|
|
api_external_symbols.append(external_symbol)
|
|
|
|
else:
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
2019-03-22 02:27:40 +08:00
|
|
|
print('error: must specify one or more architectures with the --arch option')
|
2012-08-31 05:21:24 +08:00
|
|
|
sys.exit(4)
|
|
|
|
if options.verbose:
|
2019-03-22 02:27:40 +08:00
|
|
|
print("API symbols:")
|
2012-08-31 05:21:24 +08:00
|
|
|
for (i, external_symbol) in enumerate(api_external_symbols):
|
2019-03-22 02:27:40 +08:00
|
|
|
print("[%u] %s" % (i, external_symbol))
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-08-31 05:21:24 +08:00
|
|
|
api_regex = None
|
|
|
|
if options.api_regex_str:
|
|
|
|
api_regex = re.compile(options.api_regex_str)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-08-31 05:21:24 +08:00
|
|
|
for arch in options.archs:
|
|
|
|
for exe_path in args:
|
2019-03-22 02:27:40 +08:00
|
|
|
print('Verifying (%s) "%s"...' % (arch, exe_path))
|
2012-08-31 05:21:24 +08:00
|
|
|
exe_errors = 0
|
|
|
|
undefined_symbols = extract_exe_symbol_names(
|
|
|
|
arch, exe_path, "( UNDF EXT)")
|
|
|
|
for undefined_symbol in undefined_symbols:
|
|
|
|
if api_regex:
|
|
|
|
match = api_regex.search(undefined_symbol)
|
|
|
|
if not match:
|
|
|
|
if options.verbose:
|
2019-03-22 02:27:40 +08:00
|
|
|
print('ignoring symbol: %s' % (undefined_symbol))
|
2012-08-31 05:21:24 +08:00
|
|
|
continue
|
|
|
|
if undefined_symbol in api_external_symbols:
|
|
|
|
if options.verbose:
|
2019-03-22 02:27:40 +08:00
|
|
|
print('verified symbol: %s' % (undefined_symbol))
|
2012-08-31 05:21:24 +08:00
|
|
|
else:
|
2019-03-22 02:27:40 +08:00
|
|
|
print('missing symbol: %s' % (undefined_symbol))
|
2012-08-31 05:21:24 +08:00
|
|
|
exe_errors += 1
|
|
|
|
if exe_errors:
|
2019-03-22 02:27:40 +08:00
|
|
|
print('error: missing %u API symbols from %s' % (exe_errors, options.libraries))
|
2012-08-31 05:21:24 +08:00
|
|
|
else:
|
2019-03-22 02:27:40 +08:00
|
|
|
print('success')
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2012-08-31 05:21:24 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
verify_api(sys.argv[1:])
|