Received: by 2002:a89:413:0:b0:1fd:dba5:e537 with SMTP id m19csp1097655lqs; Fri, 14 Jun 2024 15:16:55 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWQyhZFKb9sYUC4e2qpRoXhpDdy47ujAexrNBoWAp7aDMMqVCCU+0k4nZrhAtSLf5B8ZJmAzXjPznqGYmoLSdgM2Wj7Dyf/A23/WSzHDg== X-Google-Smtp-Source: AGHT+IGJ1UR+F4tA8SppprGl9HlkbzZgIHLD0JJpbgO0NwjAxD8DKbjQtAq4SSm5wdnZEj0Eyju/ X-Received: by 2002:a05:6358:e497:b0:19f:3ade:eb78 with SMTP id e5c5f4694b2df-19fb5039d19mr461258155d.28.1718403415009; Fri, 14 Jun 2024 15:16:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718403414; cv=pass; d=google.com; s=arc-20160816; b=TZfaIRJntpCTj3ZLwfk9NYuRdeMyqjwV0W02ezCOe9xDfarL2J2rXnv5i/wge+GaI/ tNumwUQ8FhD+9TGOV5wYlv7r/2+1/JUeI2STicA8eL5VJsR6g2oOlzrsnpk9f0Ak7QP0 Zbc7mIveXeKecJhOl3zHyHokSW8SxYhO1/T67f5uDdXR2vutrn26Pt2yNryDPTrXOIMC RXUVDXCZMz3zuTItv6eF8z+V+YGqOdB4VPpb/ZDttAU2MSMT5YxiVy6p+FxK7/fR60QJ DRn/S58WzDRoxG4SbbuuWZF5CGwPWQYYC59mdDqxvl2QgjWamy+L/4O6lgolXxW0v6Q3 dU6w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=nMhp8ksQP47hNoOLyKczGfyhfX0UNSuKW/1x8tc/LWo=; fh=0PTwb2dctJdgxxdpEHLw2xTRIlYCAL+sOxDjzSKR2y4=; b=ZmuedsRw/RNdfW7kO9ya4ys1fomhn03BLFRzAnggZyNxbZqslHsshis0cWm0v/uIGe DHU42BBDmpc6TZH324Z3xQYusqNhqqJftrX1bNd1k8vShKU34apaOM5pBWrT8n7VDBKG 8Z1FOt7sBn0W+YYeSP6JLbwxOS+R0gYTM9D63VWQsBTBCYhzRo74+Cth+YXdcsUP5ser G/jYsvvEhzZoW1qdqkNSIMG5a3sdEYKH/Q9bPT5yKoiQJ5YnQXBPTf/TIlrwyfpZ/4TG WtGpb6mKkCj0yyK2nWd0R7V3/Ix1lpB0GQHk15NUKCXZCmxGEqcFp5fiExFin7IZzj5I kE5w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=ZE1pRZIl; arc=pass (i=1 dkim=pass dkdomain=ibm.com dmarc=pass fromdomain=linux.vnet.ibm.com); spf=pass (google.com: domain of linux-kernel+bounces-215339-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-215339-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-6fedd4f410dsi4301753a12.45.2024.06.14.15.16.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 15:16:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-215339-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=ZE1pRZIl; arc=pass (i=1 dkim=pass dkdomain=ibm.com dmarc=pass fromdomain=linux.vnet.ibm.com); spf=pass (google.com: domain of linux-kernel+bounces-215339-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-215339-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id A52E0B24CFC for ; Fri, 14 Jun 2024 17:27:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7C9A119EEC5; Fri, 14 Jun 2024 17:27:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="ZE1pRZIl" Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (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 A9E4819CCE9; Fri, 14 Jun 2024 17:27:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718386029; cv=none; b=YEt6rIqZU+wkX7U0k4AZeuv6k1lNmZA3/Px71AC5aYJ+lAvbBVcc8b/NwH5500nFCEPfYi12LHl8W/NesggfIIUyzgi3tyLFAQLWs+luJ7vNLSElXUz8+0UoqLxUzEFdI7+yPt1b8l8JgN1Ordl2M2WNuO/wZ3eHgrd/h7NDHmI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718386029; c=relaxed/simple; bh=VwQEjpvbrgC4ij4kAyO2nlIKkFx8XCcxzHH6k0U1M7E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GMEwyp9SFj9up6tQ1++AEWqkXgM+ZF1p7S3KiI9fnerx8LcNT6NGUKuxzB7FYc3tdy/d6xGVOGCQULuAeUjnHyMOjH0xsUqBIRzDk7cfeUcARIaReJR0vlb0UctxdeC4W6LZKmXJ3HA3BcmXlaKxWsjAEVyl3APgmNbiTwxMwEc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.vnet.ibm.com; spf=none smtp.mailfrom=linux.vnet.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=ZE1pRZIl; arc=none smtp.client-ip=148.163.156.1 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.vnet.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.vnet.ibm.com Received: from pps.filterd (m0356517.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 45EHQwEW029727; Fri, 14 Jun 2024 17:26:58 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=pp1; bh=nMhp8ksQP47hN oOLyKczGfyhfX0UNSuKW/1x8tc/LWo=; b=ZE1pRZIlhz8edJM2E6tLkv7sq+09V nLRyaMNCb6d3RHLcO3UnTua3iLZX1xcLWUP6vGvgnF5P+HmCAYFw953dSHvGlnII HOgHPqYTu0HwQGO1RjSwFADlqgAbWo86B3mayEslXb4ESGbqc5HQ30JowB0r4slR fkQTM8dApR5NauLB12WfNqTTH57Hngxb9+sIQ6nChjTzeN8lDRI/KuEChndBk5E+ ATNx5kMNUtoS+D8MkvPcDacb5QYVswZpxYHsekgjA5/OqQzl3Cj0rIs7DL1B+Jok U8p/F7T2IW2sb0FsGooWK6qQ2hOnL+s7aSZnfrAH3RPapGiXEiaqJWJzQ== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3yrryk05m1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 14 Jun 2024 17:26:58 +0000 (GMT) Received: from m0356517.ppops.net (m0356517.ppops.net [127.0.0.1]) by pps.reinject (8.18.0.8/8.18.0.8) with ESMTP id 45EHQvFK029708; Fri, 14 Jun 2024 17:26:57 GMT Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3yrryk05kv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 14 Jun 2024 17:26:57 +0000 (GMT) Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 45EH0GEC020066; Fri, 14 Jun 2024 17:26:56 GMT Received: from smtprelay06.fra02v.mail.ibm.com ([9.218.2.230]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3yn34nud3b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 14 Jun 2024 17:26:56 +0000 Received: from smtpav03.fra02v.mail.ibm.com (smtpav03.fra02v.mail.ibm.com [10.20.54.102]) by smtprelay06.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 45EHQolf30409400 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 14 Jun 2024 17:26:52 GMT Received: from smtpav03.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 80DC620040; Fri, 14 Jun 2024 17:26:50 +0000 (GMT) Received: from smtpav03.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B378D20043; Fri, 14 Jun 2024 17:26:47 +0000 (GMT) Received: from localhost.localdomain (unknown [9.43.82.18]) by smtpav03.fra02v.mail.ibm.com (Postfix) with ESMTP; Fri, 14 Jun 2024 17:26:47 +0000 (GMT) From: Athira Rajeev To: acme@kernel.org, jolsa@kernel.org, adrian.hunter@intel.com, irogers@google.com, namhyung@kernel.org, segher@kernel.crashing.org, christophe.leroy@csgroup.eu Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, akanksha@linux.ibm.com, maddy@linux.ibm.com, atrajeev@linux.vnet.ibm.com, kjain@linux.ibm.com, disgoel@linux.vnet.ibm.com Subject: [V4 03/16] tools/perf: Add support to capture and parse raw instruction in powerpc using dso__data_read_offset utility Date: Fri, 14 Jun 2024 22:56:18 +0530 Message-Id: <20240614172631.56803-4-atrajeev@linux.vnet.ibm.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20240614172631.56803-1-atrajeev@linux.vnet.ibm.com> References: <20240614172631.56803-1-atrajeev@linux.vnet.ibm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-GUID: xwj9z1BnmB5dP9fXttT3RN-6pJyr9K12 X-Proofpoint-ORIG-GUID: -fMqKB81Jv7XvQj0ZoPR_epoGfPe9fBF X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-06-14_15,2024-06-14_03,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 clxscore=1015 malwarescore=0 priorityscore=1501 lowpriorityscore=0 bulkscore=0 spamscore=0 mlxscore=0 adultscore=0 suspectscore=0 phishscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405170001 definitions=main-2406140119 Add support to capture and parse raw instruction in powerpc. Currently, the perf tool infrastructure uses two ways to disassemble and understand the instruction. One is objdump and other option is via libcapstone. Currently, the perf tool infrastructure uses "--no-show-raw-insn" option with "objdump" while disassemble. Example from powerpc with this option for an instruction address is: Snippet from: objdump --start-address=
--stop-address=
-d --no-show-raw-insn -C c0000000010224b4: lwz r10,0(r9) This line "lwz r10,0(r9)" is parsed to extract instruction name, registers names and offset. Also to find whether there is a memory reference in the operands, "memory_ref_char" field of objdump is used. For x86, "(" is used as memory_ref_char to tackle instructions of the form "mov (%rax), %rcx". In case of powerpc, not all instructions using "(" are the only memory instructions. Example, above instruction can also be of extended form (X form) "lwzx r10,0,r19". Inorder to easy identify the instruction category and extract the source/target registers, patch adds support to use raw instruction for powerpc. Approach used is to read the raw instruction directly from the DSO file using "dso__data_read_offset" utility which is already implemented in perf infrastructure in "util/dso.c". Example: 38 01 81 e8 ld r4,312(r1) Here "38 01 81 e8" is the raw instruction representation. In powerpc, this translates to instruction form: "ld RT,DS(RA)" and binary code as: | 58 | RT | RA | DS | | ------------------------------------- 0 6 11 16 30 31 Function "symbol__disassemble_dso" is updated to read raw instruction directly from DSO using dso__data_read_offset utility. In case of above example, this captures: line: 38 01 81 e8 Signed-off-by: Athira Rajeev --- tools/perf/util/disasm.c | 98 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c index b5fe3a7508bb..f19496133bf0 100644 --- a/tools/perf/util/disasm.c +++ b/tools/perf/util/disasm.c @@ -1586,6 +1586,91 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym, } #endif +static int symbol__disassemble_dso(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); + u64 start = map__rip_2objdump(map, sym->start); + u64 end = map__rip_2objdump(map, sym->end); + u64 len = end - start; + u64 offset; + int i, count; + u8 *buf = NULL; + char disasm_buf[512]; + struct disasm_line *dl; + u32 *line; + + /* Return if objdump is specified explicitly */ + if (args->options->objdump_path) + return -1; + + pr_debug("Reading raw instruction from : %s using dso__data_read_offset\n", filename); + + buf = malloc(len); + if (buf == NULL) + goto err; + + count = dso__data_read_offset(dso, NULL, sym->start, buf, len); + + line = (u32 *)buf; + + if ((u64)count != len) + goto err; + + /* 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); + + /* Each raw instruction is 4 byte */ + 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: + free(buf); + return count < 0 ? count : 0; + +err: + count = -1; + goto out; +} /* * Possibly create a new version of line with tabs expanded. Returns the * existing or new line, storage is updated if a new line is allocated. If @@ -1710,6 +1795,19 @@ int symbol__disassemble(struct symbol *sym, struct annotate_args *args) strcpy(symfs_filename, tmp); } + /* + * For powerpc data type profiling, use the dso__data_read_offset + * to read raw instruction directly and interpret the binary code + * to understand instructions and register fields. For sort keys as + * type and typeoff, disassemble to mnemonic notation is + * not required in case of powerpc. + */ + if (arch__is(args->arch, "powerpc")) { + err = symbol__disassemble_dso(symfs_filename, sym, args); + if (err == 0) + goto out_remove_tmp; + } + #ifdef HAVE_LIBCAPSTONE_SUPPORT err = symbol__disassemble_capstone(symfs_filename, sym, args); if (err == 0) -- 2.43.0