From patchwork Thu Nov 9 23:59: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: 163645 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp786456vqs; Thu, 9 Nov 2023 16:02:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IFlmCYh6pFNG0syEJCNSvlhHFyZkDrIUEMJ37o5E2yDecpzxFH2yD88lPhryAwdOCgDI62V X-Received: by 2002:a05:6871:5826:b0:1ef:c499:174e with SMTP id oj38-20020a056871582600b001efc499174emr7856260oac.49.1699574569331; Thu, 09 Nov 2023 16:02:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699574569; cv=none; d=google.com; s=arc-20160816; b=UiW7TNofAsPd1R6bccz12kU/VxO7V7xJi83aR2z3XaBjbrO6/CTkuq0KGEawOu+Cb8 LjjL0e4Y20KDoyIHpV3dKwWoniykssAqj5BbvA2ppgdulajsq8z2Qq8/JnwtQEkG1eJo pc8RKPWUW2v1gLQio58VTFOe/EHlCzdekX91CALwupCQRS5y76mHORXEoH1RwAUL6JRz 0uuOUlhtAdAvj+IozkNWQx3vB5ONmnhIuWgo+O4AjZZlEbkm6QvTsm6SMF00ET5CPy4Y 8O7wyrG3JwZHVO6NXM5pP2PtmdNk8KLaQDWtycPdmkbNlBMLgEqkiGLMYZ65sIJknBMp hw4w== 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=mWaJqhvBxoi7J2eAsBrbo6D+bveraN1II1Qf4G2yQhQ=; fh=K//si1Hi/RYC7s39rVvmvh0YbZGmBrdWWHQg3cPF7Xc=; b=VCV23D98pSZDNykEq6Dc7h5tR2MrmLImt6VVTc3dXXf3EOhC5X6Tvr+jQJf0MybwYS v0ghlLxTMaHeuUtTV4Hjb+V4p1xkctsA06FNPnOTeHXcE/86EOPwLfdLLrjdfaX1rkYZ V/iTh0bbzWo4OTTALWqI3GceETfWk0Xd7X3c2NHGBuZ0E9uawzhc4FBlXVJMLkwN+UlE 5Lgbg518orxmlkHUqhJMNJLf9eE/qFMEjdXsO7uLnNWmQautgy6Q9W/aotWNi0vWqLYl xY5SfFEbtEETy1Y7Muy4PHVMhiOB5cn7zzizGKRf4ta2zqEawgHWCuoeub6igJo9/2dh jNgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=T8A+2BvS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id op3-20020a0568702dc300b001eacda6197dsi6943775oab.128.2023.11.09.16.02.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 16:02:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=T8A+2BvS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id C25428219C01; Thu, 9 Nov 2023 16:02:36 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345435AbjKJABn (ORCPT + 30 others); Thu, 9 Nov 2023 19:01:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345497AbjKJAA6 (ORCPT ); Thu, 9 Nov 2023 19:00:58 -0500 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BE3D49F2; Thu, 9 Nov 2023 16:00:41 -0800 (PST) Received: by mail-pj1-x1033.google.com with SMTP id 98e67ed59e1d1-2809414efa9so1374609a91.1; Thu, 09 Nov 2023 16:00:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699574441; x=1700179241; 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=mWaJqhvBxoi7J2eAsBrbo6D+bveraN1II1Qf4G2yQhQ=; b=T8A+2BvSqJDGq3/J6//XCLN+CFW9DkBCZhGzZxNdUdAUGzoD0mMNqQhxE051ogGkB+ WutjLVHyFMdoAN0NjJhgHNcVP3BGW+te93Z1hQL2THfOm0NL9zXtHNcDunH2Nn1morU2 /m1KKhSl70RvBJVNJE9eMXavO4oyKmQmUh2+ZAXro6BJxBQGHNCdvxo4zGbQN+ATQ++v CdGjup9fFyhRVpBH/prLPG7k6YPcqWO9sd7PxQ5EJXl8a7TE+rIRc87HwTG550MeQcMx nMShZN21b/QCrX6Up3/kSxUaD63JGDcZrLjhO6SHcT1RIEXpO9HmUDVfakaLgxQqsOrb EFKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699574441; x=1700179241; 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=mWaJqhvBxoi7J2eAsBrbo6D+bveraN1II1Qf4G2yQhQ=; b=L3AVkSZ2IfM9eX6ZiVijfWPhMflZA7ihtRiN4mERE7IoD+X6BURMhglzQnVDB3oINH 7vYpl72lu3Ii4p2BVqMACR6GFBqXxXHOjKTM7GKtDSGTLyhtLcIMojRYOmQzoa7lDr69 5PZPbsp+Fe1rvGuHTYDPm3CAIqLWioT3ASVBkb+PYAlkHyiFoyVLq3cqJpSoVdzb8Qni w0lNTWSjp0PVrs32ZyBPnPquU+DzCZ74QgeAPXE5K+9MPlzC2tDX1rK74dUyFXJgoFWH DlOeQzTKlJS56JDbeJfSuWdHk8PfcKtdELkvGvC2XUQJingnby0jhI3IZExJtpMT/s+A Jgjg== X-Gm-Message-State: AOJu0YzhGsWzQKYu5Tb93rHKF0dj+eggq/01awLnhLdw+BSygjDipsgK e+FKMnYXrxmlacHX9Izl/q4= X-Received: by 2002:a17:90b:314e:b0:280:39f2:2f12 with SMTP id ip14-20020a17090b314e00b0028039f22f12mr3388244pjb.26.1699574440731; Thu, 09 Nov 2023 16:00:40 -0800 (PST) Received: from bangji.corp.google.com ([2620:15c:2c0:5:d45c:ae16:d959:a5f1]) by smtp.gmail.com with ESMTPSA id k32-20020a17090a4ca300b002635db431a0sm371312pjh.45.2023.11.09.16.00.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 16:00:40 -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 , Andi Kleen , linux-trace-devel@vger.kernel.org, linux-toolchains@vger.kernel.org Subject: [PATCH 17/52] perf annotate: Implement hist_entry__get_data_type() Date: Thu, 9 Nov 2023 15:59:36 -0800 Message-ID: <20231110000012.3538610-18-namhyung@kernel.org> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog In-Reply-To: <20231110000012.3538610-1-namhyung@kernel.org> References: <20231110000012.3538610-1-namhyung@kernel.org> MIME-Version: 1.0 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 (agentk.vger.email [0.0.0.0]); Thu, 09 Nov 2023 16:02:36 -0800 (PST) X-Spam-Status: No, score=-1.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782133103633642022 X-GMAIL-MSGID: 1782133103633642022 It's the function to find out the type info from the given sample data and will be called from the hist_entry sort logic when 'type' sort key is used. It first calls objdump to disassemble the instructions and figure out information about memory access at the location. Maybe we can do it better by analyzing the instruction directly, but I'll leave it for later work. The memory access is determined by checking instruction operands to have "(" and then extract register name and offset. It'll return NULL if no data type is found. Signed-off-by: Namhyung Kim --- tools/perf/util/annotate.c | 85 ++++++++++++++++++++++++++++++++++++++ tools/perf/util/annotate.h | 4 ++ 2 files changed, 89 insertions(+) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index f0c89552087d..c08686b91861 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -25,6 +25,7 @@ #include "units.h" #include "debug.h" #include "annotate.h" +#include "annotate-data.h" #include "evsel.h" #include "evlist.h" #include "bpf-event.h" @@ -3629,3 +3630,87 @@ int annotate_get_insn_location(struct arch *arch, struct disasm_line *dl, return 0; } + +static void symbol__ensure_annotate(struct map_symbol *ms, struct evsel *evsel) +{ + struct disasm_line *dl, *tmp_dl; + struct annotation *notes; + + notes = symbol__annotation(ms->sym); + if (!list_empty(¬es->src->source)) + return; + + if (symbol__annotate(ms, evsel, notes->options, NULL) < 0) + return; + + /* remove non-insn disasm lines for simplicity */ + list_for_each_entry_safe(dl, tmp_dl, ¬es->src->source, al.node) { + if (dl->al.offset == -1) { + list_del(&dl->al.node); + free(dl); + } + } +} + +static struct disasm_line *find_disasm_line(struct symbol *sym, u64 ip) +{ + struct disasm_line *dl; + struct annotation *notes; + + notes = symbol__annotation(sym); + + list_for_each_entry(dl, ¬es->src->source, al.node) { + if (sym->start + dl->al.offset == ip) + return dl; + } + return NULL; +} + +/** + * hist_entry__get_data_type - find data type for given hist entry + * @he: hist entry + * + * This function first annotates the instruction at @he->ip and extracts + * register and offset info from it. Then it searches the DWARF debug + * info to get a variable and type information using the address, register, + * and offset. + */ +struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he) +{ + struct map_symbol *ms = &he->ms; + struct evsel *evsel = hists_to_evsel(he->hists); + struct arch *arch; + struct disasm_line *dl; + struct annotated_insn_loc loc; + struct annotated_op_loc *op_loc; + u64 ip = he->ip; + int i; + + if (ms->map == NULL || ms->sym == NULL) + return NULL; + + if (evsel__get_arch(evsel, &arch) < 0) + return NULL; + + /* Make sure it runs objdump to get disasm of the function */ + symbol__ensure_annotate(ms, evsel); + + /* + * Get a disasm to extract the location from the insn. + * This is too slow... + */ + dl = find_disasm_line(ms->sym, ip); + if (dl == NULL) + return NULL; + + if (annotate_get_insn_location(arch, dl, &loc) < 0) + return NULL; + + for_each_insn_op_loc(&loc, i, op_loc) { + if (!op_loc->mem_ref) + continue; + + return find_data_type(ms, ip, op_loc->reg, op_loc->offset); + } + return NULL; +} diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index 10eefecf49c4..06281a50ecf6 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -23,6 +23,7 @@ struct option; struct perf_sample; struct evsel; struct symbol; +struct annotated_data_type; struct ins { const char *name; @@ -481,4 +482,7 @@ struct annotated_insn_loc { int annotate_get_insn_location(struct arch *arch, struct disasm_line *dl, struct annotated_insn_loc *loc); +/* Returns a data type from the sample instruction (if any) */ +struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he); + #endif /* __PERF_ANNOTATE_H */