From nobody Thu May 7 18:11:09 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5CE9AC433F5 for ; Sat, 21 May 2022 13:05:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348951AbiEUNFL (ORCPT ); Sat, 21 May 2022 09:05:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345870AbiEUNFG (ORCPT ); Sat, 21 May 2022 09:05:06 -0400 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAF525FF39 for ; Sat, 21 May 2022 06:05:03 -0700 (PDT) Received: by mail-pg1-x52c.google.com with SMTP id g184so9912211pgc.1 for ; Sat, 21 May 2022 06:05:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZetKCNDhJaesUzgnzjA4KRdWctxKfuE7ldwoX0e6QTU=; b=zEHZoKRyt2B2aOZlRdz41LA98wlXzI8nX77bvKRw0tPzdE/bDvoseAesCdyoGhqOn+ KIUYFOI0MexR+M0n69UEcU0YKM3SY8LufUNc+6d5e/ZviEfnA7CwrC1JuoDwjAT36tii L3MfMiJ45755jLXJTIiBoOboEwOaHotbVe7R8+MiIrpfpdMRLk8GA8Boz8LIi7wAu44/ YJptKFUAObrJfrDSyn8ut5sy7yvaW2uKefN7/OpX1Rfw9bY9VYSWVF4J1dLfBQBxrN+g BRY+w7lnABVbcdMzfGuWzz/EygrwpHXQIFS1+iO8lgfAihICrPSvhsnovkdNQ0CITYhz sItg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZetKCNDhJaesUzgnzjA4KRdWctxKfuE7ldwoX0e6QTU=; b=7NlykMnLu3/vKU6m2OeQwuG/oLBURYn0VQt0vlJwAsTD+rqCujDD9wo9eD7tTlVms4 TMafcx2/fPic/Fz2atGOZfaAa7lZuNXVC4UeHcpZ1sd/MXqZZ98pXw07N+GpOCp4xs36 liu+YvzAfLqNSFwUNasO/uAtX9F9Ji71JelbosD65uMqbd4iCJ83o9MGyezQtPJvM7ML xxx7M45Rm7IEkMKPGvMmi3O+p/d1XBuMdxQQSs+HaaQdtItz5cyg0SHo4QVihxwHizrw Ld3kyB+ZL3T4q/9LDCa5q+cbLfIFerQQbxR185Y5G8ZUri1Dmw+b+Z/XyH1nVTIQvNUA zt8w== X-Gm-Message-State: AOAM530yvt0KHcpoLJXvBd/flNq0UmIUq2NGUSC8S2sd8JqWbzcICJQV mWY3ksd4Y4Kx27BQi57YQr30KA== X-Google-Smtp-Source: ABdhPJwwXfZUA1hH+I0gwVmGzZ2G5eXXQ+Zbt/vL+jjMpwP5CnNPSJwojs41GV+g5wMcH7fOEUDHhw== X-Received: by 2002:a63:241:0:b0:3c5:fc11:ab56 with SMTP id 62-20020a630241000000b003c5fc11ab56mr12848322pgc.193.1653138303026; Sat, 21 May 2022 06:05:03 -0700 (PDT) Received: from leo-build-box.lan (n058152048195.netvigator.com. [58.152.48.195]) by smtp.gmail.com with ESMTPSA id c2-20020a62f842000000b0050dc7628160sm3724874pfm.58.2022.05.21.06.04.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 May 2022 06:05:02 -0700 (PDT) From: Leo Yan To: Arnaldo Carvalho de Melo , James Clark , German Gomez , Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Adrian Hunter , Ian Rogers , Eelco Chaudron , Stephen Brennan , Tanmay Jagdale , "zengshun . wu" , Al Grant , coresight@lists.linaro.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Cc: Leo Yan Subject: [PATCH v6 1/2] perf scripting python: Expose dso and map information Date: Sat, 21 May 2022 21:04:45 +0800 Message-Id: <20220521130446.4163597-2-leo.yan@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220521130446.4163597-1-leo.yan@linaro.org> References: <20220521130446.4163597-1-leo.yan@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This change adds dso build_id and corresponding map's start and end address. The info of dso build_id can be used to find dso file path, and we can validate if a branch address falls into the range of map's start and end addresses. In addition, the map's start address can be used as an offset for disassembly. Signed-off-by: Leo Yan Acked-by: Adrian Hunter --- .../scripting-engines/trace-event-python.c | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools= /perf/util/scripting-engines/trace-event-python.c index 659eb4e4b34b..adba01b7d9dd 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -755,12 +755,22 @@ static void set_regs_in_dict(PyObject *dict, } =20 static void set_sym_in_dict(PyObject *dict, struct addr_location *al, - const char *dso_field, const char *sym_field, - const char *symoff_field) + const char *dso_field, const char *dso_bid_field, + const char *dso_map_start, const char *dso_map_end, + const char *sym_field, const char *symoff_field) { + char sbuild_id[SBUILD_ID_SIZE]; + if (al->map) { pydict_set_item_string_decref(dict, dso_field, _PyUnicode_FromString(al->map->dso->name)); + build_id__sprintf(&al->map->dso->bid, sbuild_id); + pydict_set_item_string_decref(dict, dso_bid_field, + _PyUnicode_FromString(sbuild_id)); + pydict_set_item_string_decref(dict, dso_map_start, + PyLong_FromUnsignedLong(al->map->start)); + pydict_set_item_string_decref(dict, dso_map_end, + PyLong_FromUnsignedLong(al->map->end)); } if (al->sym) { pydict_set_item_string_decref(dict, sym_field, @@ -840,7 +850,8 @@ static PyObject *get_perf_sample_dict(struct perf_sampl= e *sample, (const char *)sample->raw_data, sample->raw_size)); pydict_set_item_string_decref(dict, "comm", _PyUnicode_FromString(thread__comm_str(al->thread))); - set_sym_in_dict(dict, al, "dso", "symbol", "symoff"); + set_sym_in_dict(dict, al, "dso", "dso_bid", "dso_map_start", "dso_map_end= ", + "symbol", "symoff"); =20 pydict_set_item_string_decref(dict, "callchain", callchain); =20 @@ -856,7 +867,9 @@ static PyObject *get_perf_sample_dict(struct perf_sampl= e *sample, if (addr_al) { pydict_set_item_string_decref(dict_sample, "addr_correlates_sym", PyBool_FromLong(1)); - set_sym_in_dict(dict_sample, addr_al, "addr_dso", "addr_symbol", "addr_s= ymoff"); + set_sym_in_dict(dict_sample, addr_al, "addr_dso", "addr_dso_bid", + "addr_dso_map_start", "addr_dso_map_end", + "addr_symbol", "addr_symoff"); } =20 if (sample->flags) --=20 2.25.1 From nobody Thu May 7 18:11:09 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D4440C433EF for ; Sat, 21 May 2022 13:05:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351572AbiEUNFR (ORCPT ); Sat, 21 May 2022 09:05:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345870AbiEUNFM (ORCPT ); Sat, 21 May 2022 09:05:12 -0400 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F44C606C1 for ; Sat, 21 May 2022 06:05:09 -0700 (PDT) Received: by mail-pj1-x1031.google.com with SMTP id a23-20020a17090acb9700b001df4e9f4870so9903846pju.1 for ; Sat, 21 May 2022 06:05:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vmqkmQcBrTMeraC/ViSTz7A8WYbcIZcg7RqKBqYcwVI=; b=XFQT2TEIaQV+QaQ6UzQPbUeI08EPjLJHkTIgWrOhr005wdKwgEcTf8eRgOvkhUpKUD VymVkHvrRHtgbab8NCwyMfGluZ1dAHVGB3xlQVd9VCggJ21WIYvzYmv4u9bc/e0lBCkT JgEu+JTREELrEPGOgcDvlZV/1FtY7UaBcbU4Sp9rr5lQ9RgQT4G0oMU04gXRSq5kL3FN nQPpnXuqcHsZWN5IDjkD7nak+x0+EbEpQ0f4mANUHukWyjxDIrFKskY0Qo0yP/GorCPB 7xF9uQ2xofL+V5Q/IUVdZiR3IsZl8kSITYFhWsTW8bSXM/4laaLY2L1fYEym99uqHx32 IeUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vmqkmQcBrTMeraC/ViSTz7A8WYbcIZcg7RqKBqYcwVI=; b=MXo3tA2ucu48DltSLA6Fu3H4/5+nnp1x/Rd8IwddKxp1+ZkDX4x5ewIZQ6t5AsCsvn f1q+JAKUs6pdNQOFrHBEugOkNr0uo8smACC2WYoxHwyNI16B2wolA2qt8sK42M44KlQu ejY5Imc3aes18GDnpN2kgrUuMUHbFcAdXU4RD0vffGZM8rXj+P8Yj2e84U1sQgt1kJ4q vg/sVlpaLCJKRArtQAz1QeVxHl3d2gSfQRMv9enCHwVObVKoCJ+hNhhg4zb+6m+wE4pG RWrdsZNMvMQ7v5UkGJfMZYC3RgnJI+JM7K6tSd+Vs7PdvZDzqTvNQYbjD1PW3YTqD0/w BN8g== X-Gm-Message-State: AOAM532axshgqKkOQqTU+tJv39iz7c9B7qKPAP2sc7JqS4bIeD72t9+D khvgJO6HNicEXDJOF1Oi4PkGdA== X-Google-Smtp-Source: ABdhPJxVSPIMhHoW4OmvtB18jmCsrf4MkYBdQmxGP4i4zWBovUWYEqsK/mZRODIH7/pUwyjQJ/R48g== X-Received: by 2002:a17:90a:1c02:b0:1e0:df7:31f2 with SMTP id s2-20020a17090a1c0200b001e00df731f2mr4769002pjs.222.1653138308174; Sat, 21 May 2022 06:05:08 -0700 (PDT) Received: from leo-build-box.lan (n058152048195.netvigator.com. [58.152.48.195]) by smtp.gmail.com with ESMTPSA id c2-20020a62f842000000b0050dc7628160sm3724874pfm.58.2022.05.21.06.05.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 May 2022 06:05:07 -0700 (PDT) From: Leo Yan To: Arnaldo Carvalho de Melo , James Clark , German Gomez , Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Adrian Hunter , Ian Rogers , Eelco Chaudron , Stephen Brennan , Tanmay Jagdale , "zengshun . wu" , Al Grant , coresight@lists.linaro.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Cc: Leo Yan , Tor Jeremiassen , Mathieu Poirier Subject: [PATCH v6 2/2] perf scripts python: Support Arm CoreSight trace data disassembly Date: Sat, 21 May 2022 21:04:46 +0800 Message-Id: <20220521130446.4163597-3-leo.yan@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220521130446.4163597-1-leo.yan@linaro.org> References: <20220521130446.4163597-1-leo.yan@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This commit adds python script to parse CoreSight tracing event and print out source line and disassembly, it generates readable program execution flow for easier humans inspecting. The script receives CoreSight tracing packet with below format: +------------+------------+------------+ packet(n): | addr | ip | cpu | +------------+------------+------------+ packet(n+1): | addr | ip | cpu | +------------+------------+------------+ packet::addr presents the start address of the coming branch sample, and packet::ip is the last address of the branch smple. Therefore, a code section between branches starts from packet(n)::addr and it stops at packet(n+1)::ip. As results we combines the two continuous packets to generate the address range for instructions: [ sample(n)::addr .. sample(n+1)::ip ] The script supports both objdump or llvm-objdump for disassembly with specifying option '-d'. If doesn't specify option '-d', the script simply outputs source lines and symbols. Below shows usages with llvm-objdump or objdump to output disassembly. # perf script -s scripts/python/arm-cs-trace-disasm.py -- -d llvm-objdump= -11 -k ./vmlinux ARM CoreSight Trace Data Assembler Dump ffff800008eb3198 : ffff800008eb3310: c0 38 00 35 cbnz w0, 0xffff800008eb3a28 ffff800008eb3314: 9f 3f 03 d5 dsb sy ffff800008eb3318: df 3f 03 d5 isb ffff800008eb331c: f5 5b 42 a9 ldp x21, x22, [sp, #32] ffff800008eb3320: fb 73 45 a9 ldp x27, x28, [sp, #80] ffff800008eb3324: e0 82 40 39 ldrb w0, [x23, #32] ffff800008eb3328: 60 00 00 34 cbz w0, 0xffff800008eb3334 ffff800008eb332c: e0 03 19 aa mov x0, x25 ffff800008eb3330: 8c fe ff 97 bl 0xffff800008eb2d60 main 6728/6728 [0004] 0.000000000 etm4_enable_hw+0= x198 [kernel.kallsyms] ffff800008eb2d60 : ffff800008eb2d60: 1f 20 03 d5 nop ffff800008eb2d64: 1f 20 03 d5 nop ffff800008eb2d68: 3f 23 03 d5 hint #25 ffff800008eb2d6c: 00 00 40 f9 ldr x0, [x0] ffff800008eb2d70: 9f 3f 03 d5 dsb sy ffff800008eb2d74: 00 c0 3e 91 add x0, x0, #4016 ffff800008eb2d78: 1f 00 00 b9 str wzr, [x0] ffff800008eb2d7c: bf 23 03 d5 hint #29 ffff800008eb2d80: c0 03 5f d6 ret main 6728/6728 [0004] 0.000000000 etm4_cs_lock.isr= a.0.part.0+0x20 # perf script -s scripts/python/arm-cs-trace-disasm.py -- -d objdump -k .= /vmlinux ARM CoreSight Trace Data Assembler Dump ffff800008eb3310 : ffff800008eb3310: 350038c0 cbnz w0, ffff800008eb3a28 ffff800008eb3314: d5033f9f dsb sy ffff800008eb3318: d5033fdf isb ffff800008eb331c: a9425bf5 ldp x21, x22, [sp, #32] ffff800008eb3320: a94573fb ldp x27, x28, [sp, #80] ffff800008eb3324: 394082e0 ldrb w0, [x23, #32] ffff800008eb3328: 34000060 cbz w0, ffff800008eb3334 ffff800008eb332c: aa1903e0 mov x0, x25 ffff800008eb3330: 97fffe8c bl ffff800008eb2d60 main 6728/6728 [0004] 0.000000000 etm4_enable_hw+0= x198 [kernel.kallsyms] ffff800008eb2d60 : ffff800008eb2d60: d503201f nop ffff800008eb2d64: d503201f nop ffff800008eb2d68: d503233f paciasp ffff800008eb2d6c: f9400000 ldr x0, [x0] ffff800008eb2d70: d5033f9f dsb sy ffff800008eb2d74: 913ec000 add x0, x0, #0xfb0 ffff800008eb2d78: b900001f str wzr, [x0] ffff800008eb2d7c: d50323bf autiasp ffff800008eb2d80: d65f03c0 ret main 6728/6728 [0004] 0.000000000 etm4_cs_lock.isr= a.0.part.0+0x20 Co-authored-by: Tor Jeremiassen Co-authored-by: Mathieu Poirier Co-authored-by: Al Grant Signed-off-by: Leo Yan --- .../scripts/python/arm-cs-trace-disasm.py | 272 ++++++++++++++++++ 1 file changed, 272 insertions(+) create mode 100755 tools/perf/scripts/python/arm-cs-trace-disasm.py diff --git a/tools/perf/scripts/python/arm-cs-trace-disasm.py b/tools/perf/= scripts/python/arm-cs-trace-disasm.py new file mode 100755 index 000000000000..5f57d9829956 --- /dev/null +++ b/tools/perf/scripts/python/arm-cs-trace-disasm.py @@ -0,0 +1,272 @@ +# SPDX-License-Identifier: GPL-2.0 +# arm-cs-trace-disasm.py: ARM CoreSight Trace Dump With Disassember +# +# Author: Tor Jeremiassen +# Mathieu Poirier +# Leo Yan +# Al Grant + +from __future__ import print_function +import os +from os import path +import sys +import re +from subprocess import * +from optparse import OptionParser, make_option + +from perf_trace_context import perf_set_itrace_options, \ + perf_sample_insn, perf_sample_srccode + +# Below are some example commands for using this script. +# +# Output disassembly with objdump: +# perf script -s scripts/python/arm-cs-trace-disasm.py \ +# -- -d objdump -k path/to/vmlinux +# Output disassembly with llvm-objdump: +# perf script -s scripts/python/arm-cs-trace-disasm.py \ +# -- -d llvm-objdump-11 -k path/to/vmlinux +# Output only source line and symbols: +# perf script -s scripts/python/arm-cs-trace-disasm.py + +# Command line parsing. +option_list =3D [ + # formatting options for the bottom entry of the stack + make_option("-k", "--vmlinux", dest=3D"vmlinux_name", + help=3D"Set path to vmlinux file"), + make_option("-d", "--objdump", dest=3D"objdump_name", + help=3D"Set path to objdump executable file"), + make_option("-v", "--verbose", dest=3D"verbose", + action=3D"store_true", default=3DFalse, + help=3D"Enable debugging log") +] + +parser =3D OptionParser(option_list=3Doption_list) +(options, args) =3D parser.parse_args() + +# Initialize global dicts and regular expression +disasm_cache =3D dict() +cpu_data =3D dict() +disasm_re =3D re.compile("^\s*([0-9a-fA-F]+):") +disasm_func_re =3D re.compile("^\s*([0-9a-fA-F]+)\s.*:") +cache_size =3D 64*1024 + +glb_source_file_name =3D None +glb_line_number =3D None +glb_dso =3D None + +def get_optional(perf_dict, field): + if field in perf_dict: + return perf_dict[field] + return "[unknown]" + +def get_offset(perf_dict, field): + if field in perf_dict: + return f"+0x{perf_dict[field]:x}" + return "" + +def get_dso_file_path(dso_name, dso_build_id): + if (dso_name =3D=3D "[kernel.kallsyms]" or dso_name =3D=3D "vmlinux"): + if (options.vmlinux_name): + return options.vmlinux_name; + else: + return dso_name + + if (dso_name =3D=3D "[vdso]") : + append =3D "/vdso" + else: + append =3D "/elf" + + dso_path =3D f"{os.environ['PERF_BUILDID_DIR']}/{dso_name}/{dso_build_id}= {append}" + # Replace duplicate slash chars to single slash char + dso_path =3D dso_path.replace('//', '/', 1) + return dso_path + +def read_disam(dso_fname, dso_start, start_addr, stop_addr): + addr_range =3D str(start_addr) + ":" + str(stop_addr) + ":" + dso_fname + + # Don't let the cache get too big, clear it when it hits max size + if (len(disasm_cache) > cache_size): + disasm_cache.clear(); + + if addr_range in disasm_cache: + disasm_output =3D disasm_cache[addr_range]; + else: + start_addr =3D start_addr - dso_start; + stop_addr =3D stop_addr - dso_start; + disasm =3D [ options.objdump_name, "-d", "-z", + f"--start-address=3D0x{start_addr:x}", + f"--stop-address=3D0x{stop_addr:x}" ] + disasm +=3D [ dso_fname ] + disasm_output =3D check_output(disasm).decode('utf-8').split('\n') + disasm_cache[addr_range] =3D disasm_output + + return disasm_output + +def print_disam(dso_fname, dso_start, start_addr, stop_addr): + for line in read_disam(dso_fname, dso_start, start_addr, stop_addr): + m =3D disasm_func_re.search(line) + if m is None: + m =3D disasm_re.search(line) + if m is None: + continue + print(f"\t{line}") + +def print_sample(sample): + print(f"Sample =3D {{ cpu: {sample['cpu']:04} addr: 0x{sample['addr']:016= x} " \ + f"phys_addr: 0x{sample['phys_addr']:016x} ip: 0x{sample['ip']:016x}= " \ + f"pid: {sample['pid']} tid: {sample['tid']} period: {sample['period= ']} time: {sample['time']} }}") + +def trace_begin(): + print('ARM CoreSight Trace Data Assembler Dump') + +def trace_end(): + print('End') + +def trace_unhandled(event_name, context, event_fields_dict): + print(' '.join(['%s=3D%s'%(k,str(v))for k,v in sorted(event_fields_dict.i= tems())])) + +def common_start_str(comm, sample): + sec =3D int(sample["time"] / 1000000000) + ns =3D sample["time"] % 1000000000 + cpu =3D sample["cpu"] + pid =3D sample["pid"] + tid =3D sample["tid"] + return f"{comm:>16} {pid:>5}/{tid:<5} [{cpu:04}] {sec:9}.{ns:09} " + +# This code is copied from intel-pt-events.py for printing source code +# line and symbols. +def print_srccode(comm, param_dict, sample, symbol, dso): + ip =3D sample["ip"] + if symbol =3D=3D "[unknown]": + start_str =3D common_start_str(comm, sample) + ("%x" % ip).rjust(16).lju= st(40) + else: + offs =3D get_offset(param_dict, "symoff") + start_str =3D common_start_str(comm, sample) + (symbol + offs).ljust(40) + + global glb_source_file_name + global glb_line_number + global glb_dso + + source_file_name, line_number, source_line =3D perf_sample_srccode(perf_s= cript_context) + if source_file_name: + if glb_line_number =3D=3D line_number and glb_source_file_name =3D=3D so= urce_file_name: + src_str =3D "" + else: + if len(source_file_name) > 40: + src_file =3D ("..." + source_file_name[-37:]) + " " + else: + src_file =3D source_file_name.ljust(41) + + if source_line is None: + src_str =3D src_file + str(line_number).rjust(4) + " " + else: + src_str =3D src_file + str(line_number).rjust(4) + " " + source_line + glb_dso =3D None + elif dso =3D=3D glb_dso: + src_str =3D "" + else: + src_str =3D dso + glb_dso =3D dso + + glb_line_number =3D line_number + glb_source_file_name =3D source_file_name + + print(f"{start_str}{src_str}") + +def process_event(param_dict): + global cache_size + global options + + sample =3D param_dict["sample"] + comm =3D param_dict["comm"] + + name =3D param_dict["ev_name"] + dso =3D get_optional(param_dict, "dso") + dso_bid =3D get_optional(param_dict, "dso_bid") + dso_start =3D get_optional(param_dict, "dso_map_start") + dso_end =3D get_optional(param_dict, "dso_map_end") + symbol =3D get_optional(param_dict, "symbol") + + if (options.verbose =3D=3D True): + print(f"Event type: {name}") + print_sample(sample) + + # If cannot find dso so cannot dump assembler, bail out + if (dso =3D=3D '[unknown]'): + return + + # Validate dso start and end addresses + if ((dso_start =3D=3D '[unknown]') or (dso_end =3D=3D '[unknown]')): + print(f"Failed to find valid dso map for dso {dso}") + return + + if (name[0:12] =3D=3D "instructions"): + print_srccode(comm, param_dict, sample, symbol, dso) + return + + # Don't proceed if this event is not a branch sample, . + if (name[0:8] !=3D "branches"): + return + + cpu =3D sample["cpu"] + ip =3D sample["ip"] + addr =3D sample["addr"] + + # Initialize CPU data if it's empty, and directly return back + # if this is the first tracing event for this CPU. + if (cpu_data.get(str(cpu) + 'addr') =3D=3D None): + cpu_data[str(cpu) + 'addr'] =3D addr + return + + # The format for packet is: + # + # +------------+------------+------------+ + # sample_prev: | addr | ip | cpu | + # +------------+------------+------------+ + # sample_next: | addr | ip | cpu | + # +------------+------------+------------+ + # + # We need to combine the two continuous packets to get the instruction + # range for sample_prev::cpu: + # + # [ sample_prev::addr .. sample_next::ip ] + # + # For this purose, sample_prev::addr is stored into cpu_data structure + # and read back for 'start_addr' when the new packet comes, and we need + # to use sample_next::ip to calculate 'stop_addr', plusing extra 4 for + # 'stop_addr' is for the sake of objdump so the final assembler dump can + # include last instruction for sample_next::ip. + start_addr =3D cpu_data[str(cpu) + 'addr'] + stop_addr =3D ip + 4 + + # Record for previous sample packet + cpu_data[str(cpu) + 'addr'] =3D addr + + # Handle CS_ETM_TRACE_ON packet if start_addr=3D0 and stop_addr=3D4 + if (start_addr =3D=3D 0 and stop_addr =3D=3D 4): + print(f"CPU{cpu}: CS_ETM_TRACE_ON packet is inserted") + return + + if (start_addr < int(dso_start) or start_addr > int(dso_end)): + print(f"Start address 0x{start_addr:x} is out of range [ 0x{dso_start:x}= .. 0x{dso_end:x} ] for dso {dso}") + return + + if (stop_addr < int(dso_start) or stop_addr > int(dso_end)): + print(f"Stop address 0x{stop_addr:x} is out of range [ 0x{dso_start:x} .= . 0x{dso_end:x} ] for dso {dso}") + return + + if (options.objdump_name !=3D None): + # It doesn't need to decrease virtual memory offset for disassembly + # for kernel dso, so in this case we set vm_start to zero. + if (dso =3D=3D "[kernel.kallsyms]"): + dso_vm_start =3D 0 + else: + dso_vm_start =3D int(dso_start) + + dso_fname =3D get_dso_file_path(dso, dso_bid) + if path.exists(dso_fname): + print_disam(dso_fname, dso_vm_start, start_addr, stop_addr) + else: + print(f"Failed to find dso {dso} for address range [ 0x{start_addr:x} .= . 0x{stop_addr:x} ]") + + print_srccode(comm, param_dict, sample, symbol, dso) --=20 2.25.1