From nobody Fri Nov 14 18:20:02 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1760570978; cv=none; d=zohomail.com; s=zohoarc; b=EENSPnX+9FcHZrOZ8CpLL3/Ho29MZ+Tr5QZ0usowIe43JCJSyRfIX+1l6XRluZvOnZqdI0brA3O0WH4AfRj4j9wCfR1kR9c26zf/qgcEB89mZUxF0kx4Iz9ar+QrE+X//Y08CPLkGSNhncTqzNqOMAgU0w/WEng7f2ntJABtuGo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1760570978; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=8k6JklyVNT0RQ4Y2ow7qi/JNbrLA90/mWbrGNVfAAvE=; b=Wg2WJWmP0m7Rgwu7wso54b6ekxNHHYl9bVx+UAwn92oMvZu5MkT7zOGInKu+YluroqYEO+85eNhuRNM/doozVkGvWED77qNsX1nAfOx02zCvfcusBzU/3YeVsVoL0D5fsY6PFysTelEBk0seLR00h0ZSIG6dogEWxjiKzyQ53YM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 176057097836467.63426547081929; Wed, 15 Oct 2025 16:29:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v9Aux-0007vW-Pe; Wed, 15 Oct 2025 19:28:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1v9Auw-0007vI-Bo for qemu-devel@nongnu.org; Wed, 15 Oct 2025 19:28:26 -0400 Received: from mail-pg1-x532.google.com ([2607:f8b0:4864:20::532]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1v9Aur-0001SC-Nb for qemu-devel@nongnu.org; Wed, 15 Oct 2025 19:28:25 -0400 Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-b62fcddfa21so40290a12.1 for ; Wed, 15 Oct 2025 16:28:20 -0700 (PDT) Received: from pc.taild8403c.ts.net ([38.41.223.211]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29099ae78c6sm7787485ad.100.2025.10.15.16.28.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Oct 2025 16:28:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1760570899; x=1761175699; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=8k6JklyVNT0RQ4Y2ow7qi/JNbrLA90/mWbrGNVfAAvE=; b=dxmg5L/QCI7O5WENofaF9SZESwdLBobV2gHI1ex9F9elFk3wYjP8mjAK47M6s7tH5E gqEc8+mgAuY8rQgMgbsiDfY9meyulXgxkd6aZj4UqV9PEx2a03BCERVF6b6kHJ+DwAbQ NNS92iVf8hYsOpptg+sgTvHR4e2A19uXbSao7EVkmZCyQ6trL+5mSJgoP+8eq9lA6PxY t2vL3K1GkXjJd01n+cn3YyE4pnva/dLZRVYx7pcTNFI5w2FQTKc/gppWUZEp5ANbppv/ gpbH8r3DQ+R51wddZf/Gk8I14EFt1KNoLxfFP2vzbuPoYmcW2DXCMRnMnT2N/IRoykYA 2igg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760570899; x=1761175699; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8k6JklyVNT0RQ4Y2ow7qi/JNbrLA90/mWbrGNVfAAvE=; b=XXUnuaihyXy0MA0BBboqEMmoWdCOBplA90pjxgucnsb/yyqkxmuK5uwrhOhhjZGB68 XYL03UuJ4fkDH0dmLjUryS2QRio/uDeN9dHgA1V33xiHXGHnNffImYdoC9jYxEJ5dvQl 7/hEudaPDOaVL9aPLQvJZCzQmFkpxTThhkoPwmZSDeF0C2+guiRCnH9zonYJxta7BR9w DMehk+gV1HvtjlUh4RujyExIOPW0ePj5Lp4c8Wt6k+MHt4ZmjRRHCp64O4neFbFclbgY AOixuf1+Pj5c4x5aKMQiQScpADTXCHJ2h2KYrL/FBlhCIsGk+wOy9fjYoRjKjb/XrYv9 vpng== X-Gm-Message-State: AOJu0YyBQ1pGEwtB2xBCNW5uWwXv2MIsWqY1m2VFRNJjz69Y5pIA4tgb 80o2Re24f+WiCY+oRey+sYOEhEYo0BgB4Hj+pp1avFcgc8faQGLKsqLOokCtGWmcyS2MsTCRhSQ drMCj X-Gm-Gg: ASbGncvysz7eX7wh51nfnzfiVSVTsptcQEca6Ep8saVwfUNx0/OVeb7y2+aNAdfmNL8 ifPQNmGqu28r2eYonkYjWu7Y5dVyuuRQ4T4p9oyf6i2QUrZt2bvDeb6CufV/0au4U9bZDvWUSJe CjBANoaEGM5rXTLI/W6iXDT/TYGAtuQH/Z3bMiYU+7eYRqBceuLPUi2JZwUwtAn/qBexsZ0gh7Z E9ZXp//g19HQLK0vtvJzWdvFXmC74LAt2oe6IIu7JYF6nq43aLptJrSsbfmlILFf4gN9+PFidKi j8t7tJwf5QpBfVA/l+Ur3MQTEcM69nVVnEpQvFO/0Rhe0QmsCk4QccK6QCmVDZpBDQldx6mEsxG reFpuwpde8PM7aFsEOOrMsb/pAW55unf03VHYJOL03sIS41C+5momI0jQ0sZwYG74NmtFOSJilB wxLFuCnLUv1QpvFB6DHdCuyzAgqWYZF/mDvyqKNhT7YVHkMA== X-Google-Smtp-Source: AGHT+IHQmruXmSOHOyQ4ZvdcR3QGo/C4zdExXvbFVP/kDJv0VZ5kXVLq6z0vWhjHKy+U0giqVe/l1Q== X-Received: by 2002:a17:903:2381:b0:28e:7ab2:811c with SMTP id d9443c01a7336-290272e4bc1mr397959425ad.45.1760570899358; Wed, 15 Oct 2025 16:28:19 -0700 (PDT) From: Pierrick Bouvier To: qemu-devel@nongnu.org Cc: Pierrick Bouvier , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Mahmoud Mandour , Manos Pitsidianakis , Alexandre Iooss , =?UTF-8?q?Phil=20Mathieu-Daud=C3=A9?= Subject: [PATCH v2] contrib/plugins/uftrace_symbols.py: generate debug files to map symbols to source Date: Wed, 15 Oct 2025 16:28:09 -0700 Message-ID: <20251015232809.628043-1-pierrick.bouvier@linaro.org> X-Mailer: git-send-email 2.47.3 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::532; envelope-from=pierrick.bouvier@linaro.org; helo=mail-pg1-x532.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1760570984628154100 Content-Type: text/plain; charset="utf-8" Enhance uftrace_symbols.py to generate .dbg files, containing source location for every symbol present in .sym file. It allows to use `uftrace {replay,dump} --srcline` and show origin of functions, connecting trace to original source code. It was first implemented with pyelftools DWARF parser, which was way too slow (~minutes) to get locations for every symbol in the linux kernel. Thus, we use `addr2line` instead, which runs in seconds. As well, there were some bugs with latest pyelftools release, requiring to run master version, which is not installable with pip. Thus, since we now require binutils (addr2line), we can ditch pyelftools based implementation and simply rely on `nm` to get symbols information, which is faster and better. Signed-off-by: Pierrick Bouvier Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- contrib/plugins/uftrace_symbols.py | 116 +++++++++++++++++++---------- 1 file changed, 76 insertions(+), 40 deletions(-) diff --git a/contrib/plugins/uftrace_symbols.py b/contrib/plugins/uftrace_s= ymbols.py index b49e03203c8..45fb79c7a58 100755 --- a/contrib/plugins/uftrace_symbols.py +++ b/contrib/plugins/uftrace_symbols.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # -# Create symbols and mapping files for uftrace. +# Create symbols, debug and mapping files for uftrace. # # Copyright 2025 Linaro Ltd # Author: Pierrick Bouvier @@ -9,44 +9,71 @@ # SPDX-License-Identifier: GPL-2.0-or-later =20 import argparse -import elftools # pip install pyelftools import os +import subprocess =20 -from elftools.elf.elffile import ELFFile -from elftools.elf.sections import SymbolTableSection +class Symbol: + def __init__(self, name, addr, size): + self.name =3D name + # clamp addr to 48 bits, like uftrace entries + self.addr =3D addr & 0xffffffffffff + self.full_addr =3D addr + self.size =3D size =20 -def elf_func_symbols(elf): - symbol_tables =3D [(idx, s) for idx, s in enumerate(elf.iter_sections(= )) - if isinstance(s, SymbolTableSection)] - symbols =3D [] - for _, section in symbol_tables: - for _, symbol in enumerate(section.iter_symbols()): - if symbol_size(symbol) =3D=3D 0: - continue - type =3D symbol['st_info']['type'] - if type =3D=3D 'STT_FUNC' or type =3D=3D 'STT_NOTYPE': - symbols.append(symbol) - symbols.sort(key =3D lambda x: symbol_addr(x)) + def set_loc(self, file, line): + self.file =3D file + self.line =3D line + +def get_symbols(elf_file): + symbols=3D[] + try: + out =3D subprocess.check_output(['nm', '--print-size', elf_file], + stderr=3Dsubprocess.STDOUT, + text=3DTrue) + except subprocess.CalledProcessError as e: + print(e.output) + raise + out =3D out.strip().split('\n') + for line in out: + info =3D line.split(' ') + if len(info) =3D=3D 3: + # missing size information + continue + addr, size, type, name =3D info + # add only symbols from .text section + if type.lower() !=3D 't': + continue + addr =3D int(addr, 16) + size =3D int(size, 16) + symbols.append(Symbol(name, addr, size)) + symbols.sort(key =3D lambda x: x.addr) return symbols =20 -def symbol_size(symbol): - return symbol['st_size'] - -def symbol_addr(symbol): - addr =3D symbol['st_value'] - # clamp addr to 48 bits, like uftrace entries - return addr & 0xffffffffffff - -def symbol_name(symbol): - return symbol.name +def find_symbols_locations(elf_file, symbols): + addresses =3D '\n'.join([hex(x.full_addr) for x in symbols]) + try: + out =3D subprocess.check_output(['addr2line', '--exe', elf_file], + stderr=3Dsubprocess.STDOUT, + input=3Daddresses, text=3DTrue) + except subprocess.CalledProcessError as e: + print(e.output) + raise + out =3D out.strip().split('\n') + assert len(out) =3D=3D len(symbols) + for i in range(len(symbols)): + s =3D symbols[i] + file, line =3D out[i].split(':') + # addr2line may return 'line (discriminator [0-9]+)' sometimes, + # remove this to keep only line number. + line =3D line.split(' ')[0] + s.set_loc(file, line) =20 class BinaryFile: def __init__(self, path, map_offset): self.fullpath =3D os.path.realpath(path) self.map_offset =3D map_offset - with open(path, 'rb') as f: - self.elf =3D ELFFile(f) - self.symbols =3D elf_func_symbols(self.elf) + self.symbols =3D get_symbols(self.fullpath) + find_symbols_locations(self.fullpath, self.symbols) =20 def path(self): return self.fullpath @@ -56,24 +83,31 @@ def addr_start(self): =20 def addr_end(self): last_sym =3D self.symbols[-1] - return symbol_addr(last_sym) + symbol_size(last_sym) + self.map_of= fset + return last_sym.addr + last_sym.size + self.map_offset =20 def generate_symbol_file(self, prefix_symbols): binary_name =3D os.path.basename(self.fullpath) - sym_file_path =3D f'./uftrace.data/{binary_name}.sym' + sym_file_path =3D os.path.join('uftrace.data', f'{binary_name}.sym= ') print(f'{sym_file_path} ({len(self.symbols)} symbols)') with open(sym_file_path, 'w') as sym_file: # print hexadecimal addresses on 48 bits addrx =3D "0>12x" for s in self.symbols: - addr =3D symbol_addr(s) + addr =3D s.addr addr =3D f'{addr:{addrx}}' - size =3D f'{symbol_size(s):{addrx}}' - name =3D symbol_name(s) + size =3D f'{s.size:{addrx}}' if prefix_symbols: - name =3D f'{binary_name}:{name}' + name =3D f'{binary_name}:{s.name}' print(addr, size, 'T', name, file=3Dsym_file) =20 + def generate_debug_file(self): + binary_name =3D os.path.basename(self.fullpath) + dbg_file_path =3D os.path.join('uftrace.data', f'{binary_name}.dbg= ') + with open(dbg_file_path, 'w') as dbg_file: + for s in self.symbols: + print(f'F: {hex(s.addr)} {s.name}', file=3Ddbg_file) + print(f'L: {s.line} {s.file}', file=3Ddbg_file) + def parse_parameter(p): s =3D p.split(":") path =3D s[0] @@ -84,7 +118,7 @@ def parse_parameter(p): offset =3D s[1] if not offset.startswith('0x'): err =3D f'offset "{offset}" is not an hexadecimal constant. ' - err +=3D 'It should starts with "0x".' + err +=3D 'It should start with "0x".' raise ValueError(err) offset =3D int(offset, 16) return path, offset @@ -97,7 +131,7 @@ def is_from_user_mode(map_file_path): return False =20 def generate_map(binaries): - map_file_path =3D './uftrace.data/sid-0.map' + map_file_path =3D os.path.join('uftrace.data', 'sid-0.map') =20 if is_from_user_mode(map_file_path): print(f'do not overwrite {map_file_path} generated from qemu-user') @@ -124,7 +158,8 @@ def generate_map(binaries): =20 def main(): parser =3D argparse.ArgumentParser(description=3D - 'generate symbol files for uftrace') + 'generate symbol files for uftrace. ' + 'Require binutils (nm and addr2line).= ') parser.add_argument('elf_file', nargs=3D'+', help=3D'path to an ELF file. ' 'Use /path/to/file:0xdeadbeef to add a mapping off= set.') @@ -133,8 +168,8 @@ def main(): action=3Dargparse.BooleanOptionalAction) args =3D parser.parse_args() =20 - if not os.path.exists('./uftrace.data'): - os.mkdir('./uftrace.data') + if not os.path.exists('uftrace.data'): + os.mkdir('uftrace.data') =20 binaries =3D [] for file in args.elf_file: @@ -145,6 +180,7 @@ def main(): =20 for b in binaries: b.generate_symbol_file(args.prefix_symbols) + b.generate_debug_file() =20 generate_map(binaries) =20 --=20 2.47.3