Received: by 2002:ab2:7903:0:b0:1fb:b500:807b with SMTP id a3csp1095358lqj; Mon, 3 Jun 2024 09:58:51 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUZACXcyhqrZ7uXwcohYxZtGUovS+DiTHqEIiWpoVPwc3uDEBYzXSFv4xNJzdaAbitlbdakkRCOz0iGD0ZMOqipC7Dr5NsYilw16bXBnA== X-Google-Smtp-Source: AGHT+IEkcPd+fgog/jmrwMMGsvM2wXbFSYBIvMkeB5Wwke8mSU20xAEDSbd+uCeEoynvDXOMYJRj X-Received: by 2002:a0c:ff28:0:b0:6af:f74f:e759 with SMTP id 6a1803df08f44-6aff74fe824mr2975036d6.32.1717433931463; Mon, 03 Jun 2024 09:58:51 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717433931; cv=pass; d=google.com; s=arc-20160816; b=qTSWg620DuOGelBlb300aCny/1j00C3mo5gOGignT7IORmW0gZ52fUmhjg6vLMlu0r oq3Jl0NP/ysCwPVKIvG9TT6eDnPTZHRzENc/jNHVbUbgLgDfBn2xKRm8s837Iay2T7Lv W1NK9CgO5q/i8Mjc92r/acX6loqFBJAkKJOAcCJT8/1zl1Ck0+YWdnJDC5e2xj/Vi68G chGybia2YkdL6dkzSugIeVOhGRwxlTr/VYlUWBCZ2K+F00VUrI+6HP9FUdt8SW1BnC6K VUFF7TYmlP1lNDQlg1a5GI0bHvDU+WQHQsBw7Lgdwmw2rWX1iAbNnM6xrlipqDvDcMMM s17A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:organization:from :content-language:references:cc:to:subject:user-agent:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:date:message-id :dkim-signature; bh=6usI3od9LJkEZq1JKw5OevMYDBOCMlwl6uQyvDGIYGI=; fh=K1fdw3LK8GNlLaGtqigXsliYaVnjjxbm1DUBDE0Goco=; b=nkcAWsSKYq/OWLawdaEQ4z8scDltvmqyKqZKm3SfAx+VgTrMIVTSXSZs7vmWYfvFlb htWpee1fFSoq75oq3LNkjhLXluymyeF+9X9GhPaOmTajI1u7+1f1lXxGcOt+iSk+Uli8 6O2j1QUBc1P551VT5dVzzRQvVTTF94myEKAy0P4Y8kLJXIpn4zUi5puC/9w07pBgNDG2 mdsKPlhKJeo622GZoYzP+rhgTM3hPkcHsZGOGDSX40pXUClRStZFf5MRWjyTTBGIFfCO lrgDTSQLq5J9+KXuREL8h8XT/i6nCIF/13KE1pcG4kg3BKJs2WtCrnKqVDDUmrOvpx3I Tbiw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=bqsiot7F; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-199470-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199470-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 6a1803df08f44-6ae4b417d4esi90592246d6.421.2024.06.03.09.58.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jun 2024 09:58:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-199470-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=bqsiot7F; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-199470-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199470-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 83EE01C222B7 for ; Mon, 3 Jun 2024 16:58:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7E9F51369B8; Mon, 3 Jun 2024 16:58:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="bqsiot7F" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC64D1369A0; Mon, 3 Jun 2024 16:58:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717433902; cv=none; b=Wxzlcu+06p6D28xWWVvW0fTxHamPyFNVSizGzYhikgr4652X2ZgJUnUPgID2r1o/8LFFxYligXBg6sY7UkOv29nRdtFjbLF+AGVi82TJbAH1PBxjoS2P7/4Og8z0dZJ+2t4kJGQNSTKF6fOVnNmfT7uftsaBUcuLtl3wV3WOPKU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717433902; c=relaxed/simple; bh=ojs+BBl2wRyth1o2ZiKYXRCu771Apw2r+/u830cNuhg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Dg1DuPOHK6m6rjcu7yi7iNTCYMa+wJmoGUbji2MHejWaesJRKRd+8EGpKvLdZkChgw/0W3FnU2SoO30Daj6RLjA+ih/CWzzlhtnNOw2BILu8LztI1VkT95g8VWWL7hRkHbsWDA83uZ3n6E+ayKkeWke7eYOXOQc+WArhhK8bq1w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=bqsiot7F; arc=none smtp.client-ip=192.198.163.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1717433901; x=1748969901; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=ojs+BBl2wRyth1o2ZiKYXRCu771Apw2r+/u830cNuhg=; b=bqsiot7FH5IcIeorC6+gGNHyAmyffDCbfJUGvmFM0uBknqqc7JKgqgdQ 1vwWIyGb8FiY6GHAk5TJ+M9BPhmYOMUVVjjq5jK2lHdLQ2VyB1LzJYaxJ NS4Tu9oo+Yl2LssEJuI13rqvIhmh9sTkxyY3xQiZSlsM0Ni+sOJDfH6hh YLhk6PPplNSYgd0UAnPGe9ZmxUMnDnM2oAUgbPQe09xE1E4MtDKwbvJpP i54SB72TyU1seOfXLwe52HexMQighhRmBg2HSKVrWT8Qo3BmPqloHPA0g eiwyNrPwcoiatrAafUOPCDdZymeYQHR+OdPsfA9Mbdo9PJ9n256x33z/Y Q==; X-CSE-ConnectionGUID: keV536WxRyuwa6TkNmzOWQ== X-CSE-MsgGUID: rvSv6e7/RUi1/21vMLvJwA== X-IronPort-AV: E=McAfee;i="6600,9927,11092"; a="13748171" X-IronPort-AV: E=Sophos;i="6.08,212,1712646000"; d="scan'208";a="13748171" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa113.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jun 2024 09:58:19 -0700 X-CSE-ConnectionGUID: smcFJOSgQqWWsoHaoyB43g== X-CSE-MsgGUID: kVdT+ElDSQ2YskJCGdLL+Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,212,1712646000"; d="scan'208";a="37005745" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO [10.0.2.15]) ([10.94.248.18]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jun 2024 09:58:15 -0700 Message-ID: Date: Mon, 3 Jun 2024 19:58:11 +0300 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH V3 11/14] tools/perf: Add support to use libcapstone in powerpc To: Ian Rogers , Athira Rajeev , namhyung@kernel.org Cc: acme@kernel.org, jolsa@kernel.org, segher@kernel.crashing.org, christophe.leroy@csgroup.eu, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, akanksha@linux.ibm.com, maddy@linux.ibm.com, kjain@linux.ibm.com, disgoel@linux.vnet.ibm.com References: <20240601060941.13692-1-atrajeev@linux.vnet.ibm.com> <20240601060941.13692-12-atrajeev@linux.vnet.ibm.com> Content-Language: en-US From: Adrian Hunter Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 3/06/24 19:30, Ian Rogers wrote: > On Fri, May 31, 2024 at 11:10 PM Athira Rajeev > wrote: >> >> Now perf uses the capstone library to disassemble the instructions in >> x86. capstone is used (if available) for perf annotate to speed up. >> Currently it only supports x86 architecture. Patch includes changes to >> enable this in powerpc. For now, only for data type sort keys, this >> method is used and only binary code (raw instruction) is read. This is >> because powerpc approach to understand instructions and reg fields uses >> raw instruction. The "cs_disasm" is currently not enabled. While >> attempting to do cs_disasm, observation is that some of the instructions >> were not identified (ex: extswsli, maddld) and it had to fallback to use >> objdump. Hence enabling "cs_disasm" is added in comment section as a >> TODO for powerpc. >> >> Signed-off-by: Athira Rajeev >> --- >> tools/perf/util/disasm.c | 148 ++++++++++++++++++++++++++++++++++++++- >> 1 file changed, 146 insertions(+), 2 deletions(-) >> >> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c >> index d8b357055302..915508d2e197 100644 >> --- a/tools/perf/util/disasm.c >> +++ b/tools/perf/util/disasm.c >> @@ -1540,12 +1540,18 @@ static int open_capstone_handle(struct annotate_args *args, bool is_64bit, >> { >> struct annotation_options *opt = args->options; >> cs_mode mode = is_64bit ? CS_MODE_64 : CS_MODE_32; >> + int ret; >> >> /* TODO: support more architectures */ >> - if (!arch__is(args->arch, "x86")) >> + if ((!arch__is(args->arch, "x86")) && (!arch__is(args->arch, "powerpc"))) >> return -1; >> >> - if (cs_open(CS_ARCH_X86, mode, handle) != CS_ERR_OK) >> + if (arch__is(args->arch, "x86")) >> + ret = cs_open(CS_ARCH_X86, mode, handle); >> + else >> + ret = cs_open(CS_ARCH_PPC, mode, handle); >> + >> + if (ret != CS_ERR_OK) >> return -1; > > There looks to be a pretty/more robust capstone_init function in > print_insn.c, should we factor this code out and recycle: > https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/print_insn.c?h=perf-tools-next#n40 On a slightly related note, there is a compile error been around for a while in util/disasm.c on Ubuntu 22.04 In file included from /usr/include/capstone/capstone.h:279, from util/disasm.c:1354: /usr/include/capstone/bpf.h:94:14: error: ‘bpf_insn’ defined as wrong kind of tag 94 | typedef enum bpf_insn { | ^~~~~~~~ > > Thanks, > Ian > >> if (!opt->disassembler_style || >> @@ -1635,6 +1641,139 @@ static void print_capstone_detail(cs_insn *insn, char *buf, size_t len, >> } >> } >> >> +static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *sym, >> + struct annotate_args *args) >> +{ >> + struct annotation *notes = symbol__annotation(sym); >> + struct map *map = args->ms.map; >> + struct dso *dso = map__dso(map); >> + struct nscookie nsc; >> + u64 start = map__rip_2objdump(map, sym->start); >> + u64 end = map__rip_2objdump(map, sym->end); >> + u64 len = end - start; >> + u64 offset; >> + int i, fd, count; >> + bool is_64bit = false; >> + bool needs_cs_close = false; >> + u8 *buf = NULL; >> + struct find_file_offset_data data = { >> + .ip = start, >> + }; >> + csh handle; >> + char disasm_buf[512]; >> + struct disasm_line *dl; >> + u32 *line; >> + >> + if (args->options->objdump_path) >> + return -1; >> + >> + nsinfo__mountns_enter(dso->nsinfo, &nsc); >> + fd = open(filename, O_RDONLY); >> + nsinfo__mountns_exit(&nsc); >> + if (fd < 0) >> + return -1; >> + >> + if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, >> + &is_64bit) == 0) >> + goto err; >> + >> + if (open_capstone_handle(args, is_64bit, &handle) < 0) >> + goto err; >> + >> + needs_cs_close = true; >> + >> + buf = malloc(len); >> + if (buf == NULL) >> + goto err; >> + >> + count = pread(fd, buf, len, data.offset); >> + close(fd); >> + fd = -1; >> + >> + if ((u64)count != len) >> + goto err; >> + >> + line = (u32 *)buf; >> + >> + /* add the function address and name */ >> + scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", >> + start, sym->name); >> + >> + args->offset = -1; >> + args->line = disasm_buf; >> + args->line_nr = 0; >> + args->fileloc = NULL; >> + args->ms.sym = sym; >> + >> + dl = disasm_line__new(args); >> + if (dl == NULL) >> + goto err; >> + >> + annotation_line__add(&dl->al, ¬es->src->source); >> + >> + /* >> + * TODO: enable disassm for powerpc >> + * count = cs_disasm(handle, buf, len, start, len, &insn); >> + * >> + * For now, only binary code is saved in disassembled line >> + * to be used in "type" and "typeoff" sort keys. Each raw code >> + * is 32 bit instruction. So use "len/4" to get the number of >> + * entries. >> + */ >> + count = len/4; >> + >> + for (i = 0, offset = 0; i < count; i++) { >> + args->offset = offset; >> + sprintf(args->line, "%x", line[i]); >> + >> + dl = disasm_line__new(args); >> + if (dl == NULL) >> + goto err; >> + >> + annotation_line__add(&dl->al, ¬es->src->source); >> + >> + offset += 4; >> + } >> + >> + /* It failed in the middle */ >> + if (offset != len) { >> + struct list_head *list = ¬es->src->source; >> + >> + /* Discard all lines and fallback to objdump */ >> + while (!list_empty(list)) { >> + dl = list_first_entry(list, struct disasm_line, al.node); >> + >> + list_del_init(&dl->al.node); >> + disasm_line__free(dl); >> + } >> + count = -1; >> + } >> + >> +out: >> + if (needs_cs_close) >> + cs_close(&handle); >> + free(buf); >> + return count < 0 ? count : 0; >> + >> +err: >> + if (fd >= 0) >> + close(fd); >> + if (needs_cs_close) { >> + struct disasm_line *tmp; >> + >> + /* >> + * It probably failed in the middle of the above loop. >> + * Release any resources it might add. >> + */ >> + list_for_each_entry_safe(dl, tmp, ¬es->src->source, al.node) { >> + list_del(&dl->al.node); >> + free(dl); >> + } >> + } >> + count = -1; >> + goto out; >> +} >> + >> static int symbol__disassemble_capstone(char *filename, struct symbol *sym, >> struct annotate_args *args) >> { >> @@ -1987,6 +2126,11 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args) >> err = symbol__disassemble_dso(symfs_filename, sym, args); >> if (err == 0) >> goto out_remove_tmp; >> +#ifdef HAVE_LIBCAPSTONE_SUPPORT >> + err = symbol__disassemble_capstone_powerpc(symfs_filename, sym, args); >> + if (err == 0) >> + goto out_remove_tmp; >> +#endif >> } >> } >> >> -- >> 2.43.0 >>