From patchwork Thu Oct 12 03:50:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 151698 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp964805vqb; Wed, 11 Oct 2023 20:53:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE5pXKqmDSp2h4sXgRIFusj3wu5k3BvUSLe8XudYreEZHH5HK9fVR6ibl5xbx3mEZnd2jsf X-Received: by 2002:a17:90a:e7c4:b0:27d:15e3:3aa9 with SMTP id kb4-20020a17090ae7c400b0027d15e33aa9mr1916920pjb.3.1697082794782; Wed, 11 Oct 2023 20:53:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697082794; cv=none; d=google.com; s=arc-20160816; b=Q35HGn3e9qbokZZVwAaT6vaGBFDnSvS0tKC8t7q3B7HqNiHg6KSEmVO+Wd1FvibnrF 4FJUAJLusCvpBYgvK4GNv/h5naQzuyNb1hfD9/w2Dy09j448K16ganV+4nhE2yIdFTkP fc/AGeTRLulCfbiqPmafF4iT4lb1YFWUEtmb8IieJ2Ls8ytMusJNRTdM1s+wyaZ2UFcQ B4yK7eLrscsoPvI1dspHegz2L5t1CMgG/mNPSp12n5W7FQ240d28cyx4NuqLuu4Igcl7 vyaMagPY+AN64Ny2FKWtz9BFH+5J77Em3bjlJ2hW9vCt7ABNw+hw0LukkqRcOFfrocRI GNYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=wwugeUz2Kizrt8bo2ssr4th1+PIs+ybthYkaJJ9iBxc=; fh=7Id7wL2M+oCJXD4imOzx4OKioxBXSOZqypDprm7aO4E=; b=oFyKWcBIOSXvcALQuAAJUM7jS5pX7B4AvOsfDt07QqfG5drmBImZoKvqgRDZCAK1/1 zeoNCaiUUotHzjbGY2M4nDCpbMTLg+76KknLp6S9Baw9n53aVhZr47zbDrHyjj0nntaU hDjKoBiWoIF6WV/bAygGdSvxHhi0vXXD2tlpUoXGCY08+p7IR9lrf6SxGzLleR3xyfeD PZA86/U70pS5KStdHz0pyL9e6Onqii4dBsDbWlL089VZJbFdBS/DozH53fv6J8lwyr7F JJIxpadY6GBTl4dsYT0J6DgustwZaUUIiWYZwyIvE1z5xdrNDDHMsVcVtFih6+R7/L66 q/jQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="fh/U7yOU"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id w22-20020a17090a8a1600b00271c377a534si552053pjn.104.2023.10.11.20.53.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 20:53:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="fh/U7yOU"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 5D1B38028F87; Wed, 11 Oct 2023 20:53:06 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377140AbjJLDwp (ORCPT + 18 others); Wed, 11 Oct 2023 23:52:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376992AbjJLDwL (ORCPT ); Wed, 11 Oct 2023 23:52:11 -0400 Received: from mail-yw1-x1132.google.com (mail-yw1-x1132.google.com [IPv6:2607:f8b0:4864:20::1132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5F08116; Wed, 11 Oct 2023 20:51:33 -0700 (PDT) Received: by mail-yw1-x1132.google.com with SMTP id 00721157ae682-5a7d532da4bso6895567b3.2; Wed, 11 Oct 2023 20:51:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697082692; x=1697687492; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=wwugeUz2Kizrt8bo2ssr4th1+PIs+ybthYkaJJ9iBxc=; b=fh/U7yOUJdScqGCTsxHDN6EaC7RXG4Di7DwztT/Vy8dYflmO6SJcSkrFQXnpDH1oGl nmdjP1LgWaO2EVjF70TBALpFbEOlhYiGnwMpvfzXgPTVuasbFK8lUaF4rDHXimvEM1ui F2jEtu8j8F0GwWPKtkzF3F7AouMZ+KDkJomVCNnrO3YNdV0UUWo6LQlV2gbTLxIYH55j bQBK0mOF+jVqOs806CMlr2P3nkDlUzMnrooiR+KsYXla9nqppo+lRj9Xjhrrq0lrCGpi FBMTHdGwHjkPYBBal2mCNfeMYW4uMRFz72sI76GCf4ZC28DOwSCe+J8Zoyip6z7c1znr XKfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697082692; x=1697687492; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=wwugeUz2Kizrt8bo2ssr4th1+PIs+ybthYkaJJ9iBxc=; b=SyEYSzBTWVdzKK+cNSJmNXnLrEoNaiVKJMgZln/5bbgS8wSYVm0SqxmdKuWqVKlj0h LVZv/TWpm3DCT76b1hCpUhVqBnab5IxcArR7uO2cqvAVz0yuCAizZs/bIl0eqlXTtAM8 pYAU0XG7VbWTpwfrTV7erVK4+RhtcW+NHgZ0JH+0xP6UYmn98cKjSaR27LK+ePr/QbRV LlT/LlZtdH5EN9dHiAgW13DcxGWhyZ6UTusEpezzzsK3wxWVh8/bc07xEZgniX7P45nw 6NTu4t9FehMsVJ9rWMqdh8qybgv95ulbu86sp/q76Oym56zk/Sd3NYV9LvuuV2VW4P5c e5Zw== X-Gm-Message-State: AOJu0YwYh34OLfFo0eRsqg4t8AN6ap1xHPmM29/KF0uG4QkD6XIJRZYU RrWpXRDzUhXqDNTeQPdZXgU= X-Received: by 2002:a05:690c:3744:b0:5a7:d4a2:cd13 with SMTP id fw4-20020a05690c374400b005a7d4a2cd13mr5373990ywb.8.1697082692066; Wed, 11 Oct 2023 20:51:32 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:42e0:b1b9:d490:2f5e:be06]) by smtp.gmail.com with ESMTPSA id w8-20020a170902d70800b001bc18e579aesm711374ply.101.2023.10.11.20.51.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 20:51:31 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa , Peter Zijlstra Cc: Ian Rogers , Adrian Hunter , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Linus Torvalds , Stephane Eranian , Masami Hiramatsu , linux-toolchains@vger.kernel.org, linux-trace-devel@vger.kernel.org Subject: [PATCH 13/48] perf annotate: Add annotate_get_insn_location() Date: Wed, 11 Oct 2023 20:50:36 -0700 Message-ID: <20231012035111.676789-14-namhyung@kernel.org> X-Mailer: git-send-email 2.42.0.655.g421f12c284-goog In-Reply-To: <20231012035111.676789-1-namhyung@kernel.org> References: <20231012035111.676789-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 11 Oct 2023 20:53:06 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779520288804389653 X-GMAIL-MSGID: 1779520288804389653 The annotate_get_insn_location() is to get the detailed information of instruction locations like registers and offset. It has source and target operands locations in an array. Each operand can have a register and an offset. The offset is meaningful when mem_ref flag is set. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate.c | 107 +++++++++++++++++++++++++++++++++++++ tools/perf/util/annotate.h | 36 +++++++++++++ 2 files changed, 143 insertions(+) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 254cc9f224f4..9d653a1e84ce 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -31,6 +31,7 @@ #include "bpf-utils.h" #include "block-range.h" #include "string2.h" +#include "dwarf-regs.h" #include "util/event.h" #include "util/sharded_mutex.h" #include "arch/common.h" @@ -3484,3 +3485,109 @@ int annotate_check_args(struct annotation_options *args) } return 0; } + +/* + * Get register number and access offset from the given instruction. + * It assumes AT&T x86 asm format like OFFSET(REG). Maybe it needs + * to revisit the format when it handles different architecture. + * Fills @reg and @offset when return 0. + */ +static int extract_reg_offset(struct arch *arch, const char *str, + struct annotated_op_loc *op_loc) +{ + char *p; + char *regname; + + if (arch->objdump.register_char == 0) + return -1; + + /* + * It should start from offset, but it's possible to skip 0 + * in the asm. So 0(%rax) should be same as (%rax). + * + * However, it also start with a segment select register like + * %gs:0x18(%rbx). In that case it should skip the part. + */ + if (*str == arch->objdump.register_char) { + while (*str && !isdigit(*str) && + *str != arch->objdump.memory_ref_char) + str++; + } + + op_loc->offset = strtol(str, &p, 0); + + p = strchr(p, arch->objdump.register_char); + if (p == NULL) + return -1; + + regname = strdup(p); + if (regname == NULL) + return -1; + + op_loc->reg = get_dwarf_regnum(regname, 0); + free(regname); + return 0; +} + +/** + * annotate_get_insn_location - Get location of instruction + * @arch: the architecture info + * @dl: the target instruction + * @loc: a buffer to save the data + * + * Get detailed location info (register and offset) in the instruction. + * It needs both source and target operand and whether it accesses a + * memory location. The offset field is meaningful only when the + * corresponding mem flag is set. + * + * Some examples on x86: + * + * mov (%rax), %rcx # src_reg = rax, src_mem = 1, src_offset = 0 + * # dst_reg = rcx, dst_mem = 0 + * + * mov 0x18, %r8 # src_reg = -1, dst_reg = r8 + */ +int annotate_get_insn_location(struct arch *arch, struct disasm_line *dl, + struct annotated_insn_loc *loc) +{ + struct ins_operands *ops; + struct annotated_op_loc *op_loc; + int i; + + if (!strcmp(dl->ins.name, "lock")) + ops = dl->ops.locked.ops; + else + ops = &dl->ops; + + if (ops == NULL) + return -1; + + memset(loc, 0, sizeof(*loc)); + + for_each_insn_op_loc(loc, i, op_loc) { + const char *insn_str = ops->source.raw; + + if (i == INSN_OP_TARGET) + insn_str = ops->target.raw; + + /* Invalidate the register by default */ + op_loc->reg = -1; + + if (insn_str == NULL) + continue; + + if (strchr(insn_str, arch->objdump.memory_ref_char)) { + op_loc->mem_ref = true; + extract_reg_offset(arch, insn_str, op_loc); + } else { + char *s = strdup(insn_str); + + if (s) { + op_loc->reg = get_dwarf_regnum(s, 0); + free(s); + } + } + } + + return 0; +} diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index c74f8f10f705..4adda492233d 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -437,4 +437,40 @@ int annotate_parse_percent_type(const struct option *opt, const char *_str, int annotate_check_args(struct annotation_options *args); +/** + * struct annotated_op_loc - Location info of instruction operand + * @reg: Register in the operand + * @offset: Memory access offset in the operand + * @mem_ref: Whether the operand accesses memory + */ +struct annotated_op_loc { + int reg; + int offset; + bool mem_ref; +}; + +enum annotated_insn_ops { + INSN_OP_SOURCE = 0, + INSN_OP_TARGET = 1, + + INSN_OP_MAX, +}; + +/** + * struct annotated_insn_loc - Location info of instruction + * @ops: Array of location info for source and target operands + */ +struct annotated_insn_loc { + struct annotated_op_loc ops[INSN_OP_MAX]; +}; + +#define for_each_insn_op_loc(insn_loc, i, op_loc) \ + for (i = INSN_OP_SOURCE, op_loc = &(insn_loc)->ops[i]; \ + i < INSN_OP_MAX; \ + i++, op_loc++) + +/* Get detailed location info in the instruction */ +int annotate_get_insn_location(struct arch *arch, struct disasm_line *dl, + struct annotated_insn_loc *loc); + #endif /* __PERF_ANNOTATE_H */