Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4999611rdb; Tue, 12 Dec 2023 16:13:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IHJiZdJtaPC6Do24BevWveAUhf2LBr2DmhWDslF9Cgbpn9GJGmH5aWPEzFKJnbEF6EdAB9H X-Received: by 2002:a17:902:c1ca:b0:1d0:aeda:9acf with SMTP id c10-20020a170902c1ca00b001d0aeda9acfmr3227769plc.18.1702426429829; Tue, 12 Dec 2023 16:13:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702426429; cv=none; d=google.com; s=arc-20160816; b=Wyhc7ShNwCGCRJR5M3X1uMH9wi8KWhZ08ZRyiH9WfqzKVbIOovtOZqvcqrYDBYmhrl BQAB/Is9FLVO3x+lX2LyxiJh6H29frfdPLbqN62zBS2Q9367aruo8WtRqrN0S9P+DJ05 by9zVUi7VUHbGZI4QHC0UPhyuPrp7BuIgF3GxdJm49/xIj4P/mEW41ub2IFU4YdQWAPJ Iz1kznsgN9iRcbWdobNiETD95GRnBwb3hXjMNuNcUsXgUzAa3dwtrVSkwfrTHwxe+tbr oyVU9WwyxCJZ2u6szACV5yOTmOKkkfmxh2IjOmsQmTt8/XLdzeGsIjd7EBpo/dbGoJAS 9c1g== 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=1yE8tDVZHn5Q5IZE7sgzYyBr3j80GHRhr26uqjreUGM=; fh=7Id7wL2M+oCJXD4imOzx4OKioxBXSOZqypDprm7aO4E=; b=Mly9VuwLDFcPuT15MyCoGBd/xrW0RV4HbeQekE/rMFmGEIc/JrdL+QsN59q/M6YJ/V Pu4DgYDy4gmZCzBU2bxKSm67Ab6ZWTNNHTBBBJyLiwAYa9yx+tV0mmU6bTK6XXigDw1B kdbpD/HnE3WrFpisqz2lQrux3vot1eB5BwG2hQW0n2S9tqWNJ5mENMfGsUqmMLj/qy3+ WWN6LUYyU2O/ieA1LKY7arSafjs9C1eEoV7onG+aJyG7k0PzQ8aamgJjTIfNNjPDgI4S 2bWtAupKnmEBjZLGzexQ6JiRhvmin6b72+72/QY13sc9zr1zkSjN+0gb/ucpcFoZZruC igrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=AIXJgCZr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id n7-20020a170903110700b001d32d73cdd2si2891045plh.409.2023.12.12.16.13.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:13:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=AIXJgCZr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 6768D8040ED1; Tue, 12 Dec 2023 16:13:48 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377747AbjLMANe (ORCPT + 99 others); Tue, 12 Dec 2023 19:13:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377751AbjLMAN0 (ORCPT ); Tue, 12 Dec 2023 19:13:26 -0500 Received: from mail-io1-xd2b.google.com (mail-io1-xd2b.google.com [IPv6:2607:f8b0:4864:20::d2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C5EDBC; Tue, 12 Dec 2023 16:13:30 -0800 (PST) Received: by mail-io1-xd2b.google.com with SMTP id ca18e2360f4ac-7b7039d30acso365328039f.3; Tue, 12 Dec 2023 16:13:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702426410; x=1703031210; 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=1yE8tDVZHn5Q5IZE7sgzYyBr3j80GHRhr26uqjreUGM=; b=AIXJgCZrV2whvpPckxYKmj5a7FxRulAE1yMGD1ynu8KZnXITmN269U5oa6s86wHeZk bXvWyeRRv/4o3Q1APWQt97qyY896g/dSM3wXhWGv38r6Hga4208CfC5Xf7cpUF8qruFK Ed7Cdp6g63QifG9DMcyCsrPntHsYt+WRon7gpIrgB9kUykYe91SIwFLzSuXJAavQ33AG NuofKgjHbxrW1Mxoinuv/qvqvmHHKWY6nAUrAokRGvPbpdUkkxS7ttwGvNY/QFXO9DJ6 sAdorHLdeO1k4UO4uEts5NTVenCZo/Fc9LMjZdv+N1HF7G8GZzb9iyymEW8ttF/Vsof0 BCCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702426410; x=1703031210; 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=1yE8tDVZHn5Q5IZE7sgzYyBr3j80GHRhr26uqjreUGM=; b=G8Ty/AdkU4ZTDtQ6IngdiVRHCVr/OcGCqRqd5HHlD6DM/+v0DxRGAzDtLwtZR3xY/z hnVwHcK2o5BP4SxnQEJXqMc+6luP8obUxcKH9w2gnuk6U5mILpP1yT4S87pC/iMGbMQe +QllHGLA9WZA6MXHbXtZASlgb/oqXjHiT4OQrmoEAa75U5CHEjrdDdpxz8hKJKMOFcK3 8lFnLGCb5grimgaLGszO2XLjnRHAu8cfc9cvu3Px4z1PwrXx+XwxjExXWqOXqqNHCOw/ J+ZptovqUpaK1ENExSN4bxYps913Jo9qiItFfcxfuCeUotMFdbA33KCgcUjYiNMpnDBh gqAg== X-Gm-Message-State: AOJu0YxHnH8yqIwKWhx5lJDiF0rM8hAk8IZnspY7LOh53G3Y3h7Y6TxW iH27W4Gahnjc3xtbS+tY7yA= X-Received: by 2002:a05:6e02:ef3:b0:35d:7ce5:3a55 with SMTP id j19-20020a056e020ef300b0035d7ce53a55mr7544753ilk.121.1702426409690; Tue, 12 Dec 2023 16:13:29 -0800 (PST) Received: from bangji.corp.google.com ([2620:15c:2c0:5:8251:f360:4316:214e]) by smtp.gmail.com with ESMTPSA id i11-20020a17090332cb00b001d0ab572458sm9136398plr.121.2023.12.12.16.13.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:13:28 -0800 (PST) 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 02/17] perf dwarf-regs: Add get_dwarf_regnum() Date: Tue, 12 Dec 2023 16:13:08 -0800 Message-ID: <20231213001323.718046-3-namhyung@kernel.org> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog In-Reply-To: <20231213001323.718046-1-namhyung@kernel.org> References: <20231213001323.718046-1-namhyung@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:13:48 -0800 (PST) The get_dwarf_regnum() returns a DWARF register number from a register name string according to the psABI. Also add two pseudo encodings of DWARF_REG_PC which is a register that are used by PC-relative addressing and DWARF_REG_FB which is a frame base register. They need to be handled in a special way. Cc: Masami Hiramatsu Signed-off-by: Namhyung Kim --- tools/perf/arch/x86/util/dwarf-regs.c | 38 +++++++++++++++++++++++++++ tools/perf/util/dwarf-regs.c | 34 ++++++++++++++++++++++++ tools/perf/util/include/dwarf-regs.h | 19 ++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/tools/perf/arch/x86/util/dwarf-regs.c b/tools/perf/arch/x86/util/dwarf-regs.c index 530934805710..399c4a0a29d8 100644 --- a/tools/perf/arch/x86/util/dwarf-regs.c +++ b/tools/perf/arch/x86/util/dwarf-regs.c @@ -113,3 +113,41 @@ int regs_query_register_offset(const char *name) return roff->offset; return -EINVAL; } + +struct dwarf_regs_idx { + const char *name; + int idx; +}; + +static const struct dwarf_regs_idx x86_regidx_table[] = { + { "rax", 0 }, { "eax", 0 }, { "ax", 0 }, { "al", 0 }, + { "rdx", 1 }, { "edx", 1 }, { "dx", 1 }, { "dl", 1 }, + { "rcx", 2 }, { "ecx", 2 }, { "cx", 2 }, { "cl", 2 }, + { "rbx", 3 }, { "edx", 3 }, { "bx", 3 }, { "bl", 3 }, + { "rsi", 4 }, { "esi", 4 }, { "si", 4 }, { "sil", 4 }, + { "rdi", 5 }, { "edi", 5 }, { "di", 5 }, { "dil", 5 }, + { "rbp", 6 }, { "ebp", 6 }, { "bp", 6 }, { "bpl", 6 }, + { "rsp", 7 }, { "esp", 7 }, { "sp", 7 }, { "spl", 7 }, + { "r8", 8 }, { "r8d", 8 }, { "r8w", 8 }, { "r8b", 8 }, + { "r9", 9 }, { "r9d", 9 }, { "r9w", 9 }, { "r9b", 9 }, + { "r10", 10 }, { "r10d", 10 }, { "r10w", 10 }, { "r10b", 10 }, + { "r11", 11 }, { "r11d", 11 }, { "r11w", 11 }, { "r11b", 11 }, + { "r12", 12 }, { "r12d", 12 }, { "r12w", 12 }, { "r12b", 12 }, + { "r13", 13 }, { "r13d", 13 }, { "r13w", 13 }, { "r13b", 13 }, + { "r14", 14 }, { "r14d", 14 }, { "r14w", 14 }, { "r14b", 14 }, + { "r15", 15 }, { "r15d", 15 }, { "r15w", 15 }, { "r15b", 15 }, + { "rip", DWARF_REG_PC }, +}; + +int get_arch_regnum(const char *name) +{ + unsigned int i; + + if (*name != '%') + return -EINVAL; + + for (i = 0; i < ARRAY_SIZE(x86_regidx_table); i++) + if (!strcmp(x86_regidx_table[i].name, name + 1)) + return x86_regidx_table[i].idx; + return -ENOENT; +} diff --git a/tools/perf/util/dwarf-regs.c b/tools/perf/util/dwarf-regs.c index 69cfaa5953bf..5b7f86c0063f 100644 --- a/tools/perf/util/dwarf-regs.c +++ b/tools/perf/util/dwarf-regs.c @@ -5,9 +5,12 @@ * Written by: Masami Hiramatsu */ +#include +#include #include #include #include +#include #include #ifndef EM_AARCH64 @@ -68,3 +71,34 @@ const char *get_dwarf_regstr(unsigned int n, unsigned int machine) } return NULL; } + +__weak int get_arch_regnum(const char *name __maybe_unused) +{ + return -ENOTSUP; +} + +/* Return DWARF register number from architecture register name */ +int get_dwarf_regnum(const char *name, unsigned int machine) +{ + char *regname = strdup(name); + int reg = -1; + char *p; + + if (regname == NULL) + return -EINVAL; + + /* For convenience, remove trailing characters */ + p = strpbrk(regname, " ,)"); + if (p) + *p = '\0'; + + switch (machine) { + case EM_NONE: /* Generic arch - use host arch */ + reg = get_arch_regnum(regname); + break; + default: + pr_err("ELF MACHINE %x is not supported.\n", machine); + } + free(regname); + return reg; +} diff --git a/tools/perf/util/include/dwarf-regs.h b/tools/perf/util/include/dwarf-regs.h index 7d99a084e82d..01fb25a1150a 100644 --- a/tools/perf/util/include/dwarf-regs.h +++ b/tools/perf/util/include/dwarf-regs.h @@ -2,6 +2,9 @@ #ifndef _PERF_DWARF_REGS_H_ #define _PERF_DWARF_REGS_H_ +#define DWARF_REG_PC 0xd3af9c /* random number */ +#define DWARF_REG_FB 0xd3affb /* random number */ + #ifdef HAVE_DWARF_SUPPORT const char *get_arch_regstr(unsigned int n); /* @@ -10,6 +13,22 @@ const char *get_arch_regstr(unsigned int n); * machine: ELF machine signature (EM_*) */ const char *get_dwarf_regstr(unsigned int n, unsigned int machine); + +int get_arch_regnum(const char *name); +/* + * get_dwarf_regnum - Returns DWARF regnum from register name + * name: architecture register name + * machine: ELF machine signature (EM_*) + */ +int get_dwarf_regnum(const char *name, unsigned int machine); + +#else /* HAVE_DWARF_SUPPORT */ + +static inline int get_dwarf_regnum(const char *name __maybe_unused, + unsigned int machine __maybe_unused) +{ + return -1; +} #endif #ifdef HAVE_ARCH_REGS_QUERY_REGISTER_OFFSET -- 2.43.0.472.g3155946c3a-goog