162306a36Sopenharmony_ci#!/usr/bin/env python3 262306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0 362306a36Sopenharmony_ci# 462306a36Sopenharmony_ci# Usage: unwcheck.py FILE 562306a36Sopenharmony_ci# 662306a36Sopenharmony_ci# This script checks the unwind info of each function in file FILE 762306a36Sopenharmony_ci# and verifies that the sum of the region-lengths matches the total 862306a36Sopenharmony_ci# length of the function. 962306a36Sopenharmony_ci# 1062306a36Sopenharmony_ci# Based on a shell/awk script originally written by Harish Patil, 1162306a36Sopenharmony_ci# which was converted to Perl by Matthew Chapman, which was converted 1262306a36Sopenharmony_ci# to Python by David Mosberger. 1362306a36Sopenharmony_ci# 1462306a36Sopenharmony_ciimport os 1562306a36Sopenharmony_ciimport re 1662306a36Sopenharmony_ciimport sys 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ciif len(sys.argv) != 2: 1962306a36Sopenharmony_ci print("Usage: %s FILE" % sys.argv[0]) 2062306a36Sopenharmony_ci sys.exit(2) 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_cireadelf = os.getenv("READELF", "readelf") 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_cistart_pattern = re.compile("<([^>]*)>: \[0x([0-9a-f]+)-0x([0-9a-f]+)\]") 2562306a36Sopenharmony_cirlen_pattern = re.compile(".*rlen=([0-9]+)") 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_cidef check_func (func, slots, rlen_sum): 2862306a36Sopenharmony_ci if slots != rlen_sum: 2962306a36Sopenharmony_ci global num_errors 3062306a36Sopenharmony_ci num_errors += 1 3162306a36Sopenharmony_ci if not func: func = "[%#x-%#x]" % (start, end) 3262306a36Sopenharmony_ci print("ERROR: %s: %lu slots, total region length = %lu" % (func, slots, rlen_sum)) 3362306a36Sopenharmony_ci return 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_cinum_funcs = 0 3662306a36Sopenharmony_cinum_errors = 0 3762306a36Sopenharmony_cifunc = False 3862306a36Sopenharmony_cislots = 0 3962306a36Sopenharmony_cirlen_sum = 0 4062306a36Sopenharmony_cifor line in os.popen("%s -u %s" % (readelf, sys.argv[1])): 4162306a36Sopenharmony_ci m = start_pattern.match(line) 4262306a36Sopenharmony_ci if m: 4362306a36Sopenharmony_ci check_func(func, slots, rlen_sum) 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci func = m.group(1) 4662306a36Sopenharmony_ci start = int(m.group(2), 16) 4762306a36Sopenharmony_ci end = int(m.group(3), 16) 4862306a36Sopenharmony_ci slots = 3 * (end - start) / 16 4962306a36Sopenharmony_ci rlen_sum = 0 5062306a36Sopenharmony_ci num_funcs += 1 5162306a36Sopenharmony_ci else: 5262306a36Sopenharmony_ci m = rlen_pattern.match(line) 5362306a36Sopenharmony_ci if m: 5462306a36Sopenharmony_ci rlen_sum += int(m.group(1)) 5562306a36Sopenharmony_cicheck_func(func, slots, rlen_sum) 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ciif num_errors == 0: 5862306a36Sopenharmony_ci print("No errors detected in %u functions." % num_funcs) 5962306a36Sopenharmony_cielse: 6062306a36Sopenharmony_ci if num_errors > 1: 6162306a36Sopenharmony_ci err="errors" 6262306a36Sopenharmony_ci else: 6362306a36Sopenharmony_ci err="error" 6462306a36Sopenharmony_ci print("%u %s detected in %u functions." % (num_errors, err, num_funcs)) 6562306a36Sopenharmony_ci sys.exit(1) 66