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 python3
# SPDX-FileCopyrightText: 2023 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later
"""
Extract line & function information from addresses (found in back-traces) using addr2line.
Example:
addr2line_backtrace.py --exe=/path/to/blender error.log
Piping from the standard-input is also supported:
cat error.log | addr2line_backtrace.py --exe=blender.bin
The text is printed to the standard output.
"""
import argparse
import multiprocessing
import os
import re
import subprocess
import sys
import time
from typing import (
List,
Optional,
Sequence,
Tuple,
)
RE_ADDR = re.compile("\\[(0x[A-Fa-f0-9]+)\\]")
IS_ATTY = sys.stdout.isatty()
def value_as_percentage(value_partial: int, value_final: int) -> str:
percent = 0.0 if (value_final == 0) else (value_partial / value_final)
return "{:-6.2f}%".format(percent * 100)
if IS_ATTY:
def progress_output(value_partial: int, value_final: int, info: str) -> None:
sys.stdout.write("\r\033[K[{:s}]: {:s}".format(value_as_percentage(value_partial, value_final), info))
else:
def progress_output(value_partial: int, value_final: int, info: str) -> None:
sys.stdout.write("[{:s}]: {:s}\n".format(value_as_percentage(value_partial, value_final), info))
def find_gitroot(filepath_reference: str) -> Optional[str]:
path = filepath_reference
path_prev = ""
found = False
while not (found := os.path.exists(os.path.join(path, ".git"))) and path != path_prev:
path_prev = path
path = os.path.dirname(path)
if found:
return path
return None
def addr2line_fn(arg_pair: Tuple[Tuple[str, str, bool], Sequence[str]]) -> Sequence[Tuple[str, str]]:
shared_args, addr_list = arg_pair
(exe, base_path, time_command) = shared_args
cmd = (
"addr2line",
*addr_list,
"--functions",
"--demangle",
"--exe=" + exe,
)
if time_command:
time_beg = time.time()
output = subprocess.check_output(cmd).rstrip().decode("utf-8", errors="surrogateescape")
output_lines = output.split("\n")
result: List[Tuple[str, str]] = []
while output_lines:
# Swap (function, line), to (line, function).
output_lines_for_addr = output_lines[:2]
assert len(output_lines_for_addr) == 2
del output_lines[:2]
line_list = []
for line in output_lines_for_addr:
if base_path and line.startswith(base_path):
line = "." + os.sep + line[len(base_path):]
line_list.append(line)
output = ": ".join(reversed(line_list))
if time_command:
time_end = time.time()
output = "{:s} ({:.2f})".format(output, time_end - time_beg)
result.append((addr_list[len(result)], output))
return result
def argparse_create() -> argparse.ArgumentParser:
import argparse
# When `--help` or no arguments are given, print this help.
epilog = "This is typically used from the output of a stack-trace on Linux/Unix."
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description=__doc__,
epilog=epilog,
)
parser.add_argument(
"--exe",
dest="exe",
metavar='EXECUTABLE',
required=True,
help="Path to the binary.",
)
parser.add_argument(
"--base",
dest="base",
metavar='BASE_PATH',
default="",
required=False,
help="Base path.",
)
parser.add_argument(
"--time",
dest="time_command",
action='store_true',
required=False,
help="Time addr2line (useful for checking on especially slow lookup).",
)
parser.add_argument(
"--jobs",
dest="jobs",
type=int,
default=4,
help=(
"The number of processes to use. "
"Defaults to 4 to prevent using too much memory, 1 is single threaded (useful for debugging)."
),
required=False,
)
parser.add_argument(
"backtraces",
nargs="*",
help="Back-trace files to scan for addresses.",
)
return parser
def addr2line_for_filedata(
exe: str,
base_path: str,
time_command: bool,
jobs: int,
backtrace_data: str,
) -> None:
addr_set = set()
for match in RE_ADDR.finditer(backtrace_data):
addr = match.group(1)
addr_set.add(addr)
shared_args = exe, base_path, time_command
if jobs >= len(addr_set):
addr2line_args = [(shared_args, [addr]) for addr in addr_set]
else:
addr2line_args = [(shared_args, []) for _ in range(jobs)]
# Avoid using consecutive addresses in chunks since slower lookups are likely to be groups.
for i, addr in enumerate(addr_set):
addr2line_args[i % jobs][1].append(addr)
addr_map = {}
addr_done = 0
addr_len = len(addr_set)
with multiprocessing.Pool(jobs) as pool:
for i, result_list in enumerate(pool.imap_unordered(addr2line_fn, addr2line_args), 1):
for (addr, result) in result_list:
progress_output(addr_done, addr_len, "{:d} of {:d}".format(addr_done, addr_len))
addr_map[addr] = result
addr_done += 1
if IS_ATTY:
print()
def re_replace_fn(match: re.Match[str]) -> str:
addr = match.group(1)
return "{:s} ({:s})".format(addr_map[addr], addr)
backtrace_data_updated = RE_ADDR.sub(re_replace_fn, backtrace_data)
sys.stdout.write(backtrace_data_updated)
sys.stdout.write("\n")
def main() -> None:
args = argparse_create().parse_args()
jobs = args.jobs
if jobs <= 0:
jobs = multiprocessing.cpu_count()
base_path = args.base
if not base_path:
base_test = find_gitroot(os.getcwd())
if base_test is not None:
base_path = base_test
if base_path:
base_path = base_path.rstrip(os.sep) + os.sep
if args.backtraces:
for backtrace_filepath in args.backtraces:
try:
with open(backtrace_filepath, 'r', encoding="utf-8", errors="surrogateescape") as fh:
bactrace_data = fh.read()
except Exception as ex:
print("Filed to open {!r}, {:s}".format(backtrace_filepath, str(ex)))
continue
addr2line_for_filedata(args.exe, base_path, args.time_command, jobs, bactrace_data)
else:
bactrace_data = sys.stdin.read()
addr2line_for_filedata(args.exe, base_path, args.time_command, jobs, bactrace_data)
if __name__ == "__main__":
main()
|