Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4999852rdb; Tue, 12 Dec 2023 16:14:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IG73WF5xJQpuZAJFP4gpyVEudEc1zl0Q+zBVmQe+MebanfrAETNLjmYOzFajMrb3kNdL2wu X-Received: by 2002:a05:6a00:4643:b0:6cd:f18e:176 with SMTP id kp3-20020a056a00464300b006cdf18e0176mr7407974pfb.27.1702426462846; Tue, 12 Dec 2023 16:14:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702426462; cv=none; d=google.com; s=arc-20160816; b=NH7COO8OeXIJzDkgZymTBnbCM7evvuV0K8bBRC5F3gnQdfQj25kyvt0jkBCkOimuyZ b2qV5aWWjZgxALNA2cIHjerDByUxQN6sbQ6vur2+h/hFO3L3VfJBnbzbwLfktd7FByTk uKI6b5oDLwyP06NIYnPFvfZ/u34qDOjychHHsxENeqbPLnj8dxUDc/FFe/DwOpnfbJzD QLoi6XZ9SNAcrvweUtYIIF6v+8Ahs1PSJHI9WI/K2xcjE4y77dsDGHmtOKNxPrFNWDAS Z08I9CejjzSq89eF4qDyBkLCWPTHvBRY9P3AYNxxgk85bX5iKWIleM955CkbpvQ0T3KW AQhQ== 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=hu4FhpBOZPILh8fziHco9keVgUrcb3i/LhY1Cwkj/nE=; fh=7Id7wL2M+oCJXD4imOzx4OKioxBXSOZqypDprm7aO4E=; b=m95y3k9gTnrO4bsCGiV0sw5QH7A3r1TmtJ1cLqcCZ8rnaYlFn/3jJ9jlxyqas0Mccg wXgCf1KRJrPlDTaE9O5rDrGOeAI2sdyi/3g4Jdy4D2gObKMsnvF9DPtNreUnK63dqOYd HiXbD32/tRM/MArrBvTV6lgUpEFX8SAIu/NLNRtG/RnfpUB/mGtvEvq8+PbF5VUln+C2 M+b10ylt+w607bztcYd6nTCY205GtzcIl1WA/OlPtDyT6r2KpS526p/C/wa/d0eozDPJ cCYhTChMXdvcVXfUZscbQCin0hwyA+hUv8r5zwNEtx1HEAt7yD+dMX5vqr9LbKgf1gTQ 21Ig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=WaKMdEEy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id l190-20020a6325c7000000b005c219415020si8330576pgl.262.2023.12.12.16.14.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:14:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=WaKMdEEy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 A7AA6804A9F3; Tue, 12 Dec 2023 16:14:15 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377909AbjLMANs (ORCPT + 99 others); Tue, 12 Dec 2023 19:13:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377912AbjLMANi (ORCPT ); Tue, 12 Dec 2023 19:13:38 -0500 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62378112; Tue, 12 Dec 2023 16:13:39 -0800 (PST) Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1d098b87eeeso56326195ad.0; Tue, 12 Dec 2023 16:13:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702426419; x=1703031219; 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=hu4FhpBOZPILh8fziHco9keVgUrcb3i/LhY1Cwkj/nE=; b=WaKMdEEylrJ6/OUpJlgn+WROF56jYzmLhFgq57A2N68icSBiLr2PMH5kNQHHRjlaaL IvFwM9Dgfx7Ur7Zrb/iasLd/2lRWDI6cawB9s9fn1mRmdDTLlTnfAsn0/Dzau7w2jF4s EdLKU1vILCSeDwjJ8pZAvoIIV6QOuo5lY2q0XBAs87o1Rr9WQLk2MQLRniZlhgM61CUf Iy1ZWoQrPuQnYFRmL3F7M2dmUuGi+GSr1MfUZw/psQF7KsThFIvfC2IE11idVZh0iwLs d7MlcXlypTFJguNkFEVYKZ19RUQe6exS3Db+BYFIv7we/UwtADMKJv2ZxN7CiB6XNeca t/Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702426419; x=1703031219; 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=hu4FhpBOZPILh8fziHco9keVgUrcb3i/LhY1Cwkj/nE=; b=MGEF55/xozsd+iYHHfa6p47feVjr5s7lJnoJe4Fuh0/dGJ3/mNUXvmASbG1uhGbytv WoToWFj6JTLhniClOiRFIECs20SjsQOdBkqJIRKHkyUPzM71RyHDc2jwY/K3O5jm3KZ9 hOOlzHkc4nfBgl3Zx7U+gvo3yd3oSLNCxfNQBirjuzbUJj/QLGz+4C20ySYrv4ftAccz 9CIEdvsNLghjiaiNDVvCgcO5jmht9g5z7ivDSM5H5Vf5Yg4us1O8jUoY1YdNJAXLG2ny Gp82CaDf2QX4XQMQoXrvIrZGe70WITX92S6lv2RAzsbewh/VQvkzSG1xSXke6n2QI5G9 EyNQ== X-Gm-Message-State: AOJu0Yzb0cS7rg+d9lVU+grYaBPAbz3nIti4tZ6B3ZPP0z4r8JsK+pR6 XgZW9r8q0bkbqgXDhYPdyYQ= X-Received: by 2002:a17:903:452:b0:1d0:7d9e:e727 with SMTP id iw18-20020a170903045200b001d07d9ee727mr6181201plb.14.1702426418656; Tue, 12 Dec 2023 16:13:38 -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.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:13:38 -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 07/17] perf annotate: Implement hist_entry__get_data_type() Date: Tue, 12 Dec 2023 16:13:13 -0800 Message-ID: <20231213001323.718046-8-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.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 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]); Tue, 12 Dec 2023 16:14:15 -0800 (PST) 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 | 88 ++++++++++++++++++++++++++++++++++++++ tools/perf/util/annotate.h | 4 ++ 2 files changed, 92 insertions(+) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 7c597440dc2e..8673eac4b9df 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" @@ -3625,3 +3626,90 @@ 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, 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 (!symbol_conf.init_annotation) + 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 25ae8893d4f9..6c75b2832286 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; @@ -475,4 +476,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 */ -- 2.43.0.472.g3155946c3a-goog