#!/usr/bin/env python3 """Source lines of the samples whose innermost frame is in FUNCTION (a substring), with the innermost inlined frame's file:line: where inside a function the time goes. scripts/lines.py BINARY SAMPLES FUNCTION [--top N] """ import argparse import subprocess from collections import Counter parser = argparse.ArgumentParser() parser.add_argument("binary") parser.add_argument("samples") parser.add_argument("function") parser.add_argument("--top", type=int, default=25) args = parser.parse_args() leaves = [line.split()[0] for line in open(args.samples) if line.strip()] counts = Counter(leaves) out = subprocess.run( ["addr2line", "-f", "-i", "-C", "-a", "-e", args.binary], input="\n".join(f"0x{a}" for a in counts) + "\n", capture_output=True, text=True, ).stdout.splitlines() where, current, i = Counter(), None, 0 while i < len(out): if out[i].startswith("0x"): current, first = format(int(out[i], 16), "x"), True i += 1 continue function, line = out[i], out[i + 1] if first: chain_head = (function, line) first = False if args.function in function: where[f"{line.split('/')[-1]} {function[:60]}"] += counts[current] i += 2 total = len(leaves) for place, n in where.most_common(args.top): print(f"{100 * n / total:5.2f}% {place}")