From nobody Thu Dec 18 10:18:11 2025 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3C632505D2 for ; Wed, 19 Mar 2025 05:08:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742360885; cv=none; b=O3zSt5IWbj9K6h+E3RJ/svroh978QCqUdQkFNBmPc8t/0LBkbzU4ch6IrhpSZ3a+iVVFFhvKvGyw6QZv8Sp8iMnhiQPGCxGsidMWe3UjDGvWvtVDm11OMSPdoydn0ccXzu1srzX6JBvropLVAsi/MYAQAN0BPIJEI+dzJLY9jSU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742360885; c=relaxed/simple; bh=46X5snho+NC9xyHiugqSCdg1Jz8LKx2nORti21ecVVM=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=dhDYfXy9j7FiQKbsDqXBWOf3dIveIl7nWGXriuNENT9qbtONZsKrghLHg+S1c5RHGR1CfJopz4RzhCa+mZFOyj9RkZS9iSj4PkbEdWVlgqzL5Y2obo/R3Qx7FdamFwZBZp9Ll5r2ZuJuH1DPiNFiBi4aVcYFGzn1hPPjRFLqO2s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Q1waJ9VI; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Q1waJ9VI" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e549de22484so9533337276.2 for ; Tue, 18 Mar 2025 22:08:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1742360882; x=1742965682; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=OrHbMMe03j2h6U4ZyDmCNi4vPHSyOLjKCo4G6hr5nEQ=; b=Q1waJ9VIrrw5XBGmFQ9Sjcu6SlYpSjWSPBRF4VQPW0Ig/ykFVspjxEREEsM4p4zJO+ r5EYPnNS3SfUQSJIZAVdRyKiHja0jNoQY6UpMV3iS9NysKHeyEEb9FhAkOQC8L/5fgcz 0ouSlGS6QFtiz/AjcQ3CZpp98pTXaoY8Jkv3nAhXjwQiLqFujHlrUBUeWCWUyRAuakqP zbFFiMweDr25NwH3qPVpLXg7ApfVAuyXMqas/cWYw1GD/EjRAhKrpDFSORWF6ffZwrHP 24u3APRb4k0+t62aiz8Vdqxg2giZBn0sWPjs9hno9H1XT9wqcJp17m3QuNrQsoen3HlW w0cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742360882; x=1742965682; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OrHbMMe03j2h6U4ZyDmCNi4vPHSyOLjKCo4G6hr5nEQ=; b=EbTPtQuhvXEZ9y9LeHche40wJmPyVt837Mf0w3MkMWonain7aZN2cFxpV+4IXoEqoN kxcBW6xZ570jW05QcuN+arWWGeShKAd8hm21R8EHdMOJ4y6ivQlqIX1tEsrfOvPs3IYU RFfwmdYSwA5rcxJAD85/8PdsZJLXe7kDm2bjkuBWPf9AkwpmIvEbUMOYcDQszSLLW27L cJSiXo8X+pvw5OqDUx5NCkWVbevCPxWOvkF2nvRk5FIROQFdRrYHiBCEgVdFnvbtc2up d7epZocXHR/fa2puBtPiOUkUgk3HBbn08Y11Q3CBMSQHEx2cSUonK+hkUWNI/J3DESIe zscA== X-Forwarded-Encrypted: i=1; AJvYcCUGkgsQVUr3/qs4/m/jFkPzjjLqcnYu+pgNVZ3gHvoV9pCQ56emhFvQqkf3W5IfSJMC0uFzLN7ReThV5II=@vger.kernel.org X-Gm-Message-State: AOJu0YyhWaDfqS96f7ZFWHvNXnvHktk4f3glSOdGxSQrifnDzXFm5YBC nU9cityDsDG6DC5jokMtrKJxs8YufoI+xdzS+D86vtJlQjCOU75M46dIEZlrb3G6826TE9bO5Gm FEfoEQg== X-Google-Smtp-Source: AGHT+IFx9Z0x08GER++C8nD4jZUCyngaSw/7/gURiTevmG+iiTK/bE0OxP7IhHFOsqWZ82+12GwC/FPcL7kJ X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:8a11:10b5:af90:6031]) (user=irogers job=sendgmr) by 2002:a25:f609:0:b0:e63:72df:76f1 with SMTP id 3f1490d57ef6-e667b440409mr802276.6.1742360881675; Tue, 18 Mar 2025 22:08:01 -0700 (PDT) Date: Tue, 18 Mar 2025 22:07:34 -0700 In-Reply-To: <20250319050741.269828-1-irogers@google.com> Message-Id: <20250319050741.269828-8-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250319050741.269828-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.rc1.451.g8f38331e32-goog Subject: [PATCH v7 07/14] perf thread: Add support for reading the e_machine type for a thread From: Ian Rogers To: Ian Rogers , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Kan Liang , John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , guoren , Paul Walmsley , Palmer Dabbelt , Albert Ou , Charlie Jenkins , Bibo Mao , Huacai Chen , Catalin Marinas , Jiri Slaby , "=?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?=" , Howard Chu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-arm-kernel@lists.infradead.org, "linux-csky@vger.kernel.org" , linux-riscv@lists.infradead.org, Arnd Bergmann Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" First try to read the e_machine from the dsos associated with the thread's maps. If live use the executable from /proc/pid/exe and read the e_machine from the ELF header. On failure use EM_HOST. Change builtin-trace syscall functions to pass e_machine from the thread rather than EM_HOST, so that in later patches when syscalltbl can use the e_machine the system calls are specific to the architecture. Signed-off-by: Ian Rogers --- tools/perf/builtin-trace.c | 43 ++++++++++---------- tools/perf/util/thread.c | 80 ++++++++++++++++++++++++++++++++++++++ tools/perf/util/thread.h | 14 ++++++- 3 files changed, 115 insertions(+), 22 deletions(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 3017291242cf..1c080d95c1e2 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -2731,16 +2731,16 @@ static int trace__sys_enter(struct trace *trace, st= ruct evsel *evsel, int printed =3D 0; struct thread *thread; int id =3D perf_evsel__sc_tp_uint(evsel, id, sample), err =3D -1; - int augmented_args_size =3D 0; + int augmented_args_size =3D 0, e_machine; void *augmented_args =3D NULL; - /* TODO: get e_machine from thread. */ - struct syscall *sc =3D trace__syscall_info(trace, evsel, EM_HOST, id); + struct syscall *sc; struct thread_trace *ttrace; =20 - if (sc =3D=3D NULL) - return -1; - thread =3D machine__findnew_thread(trace->host, sample->pid, sample->tid); + e_machine =3D thread__e_machine(thread, trace->host); + sc =3D trace__syscall_info(trace, evsel, e_machine, id); + if (sc =3D=3D NULL) + goto out_put; ttrace =3D thread__trace(thread, trace); if (ttrace =3D=3D NULL) goto out_put; @@ -2808,17 +2808,18 @@ static int trace__fprintf_sys_enter(struct trace *t= race, struct evsel *evsel, struct thread_trace *ttrace; struct thread *thread; int id =3D perf_evsel__sc_tp_uint(evsel, id, sample), err =3D -1; - /* TODO: get e_machine from thread. */ - struct syscall *sc =3D trace__syscall_info(trace, evsel, EM_HOST, id); + struct syscall *sc; char msg[1024]; void *args, *augmented_args =3D NULL; - int augmented_args_size; + int augmented_args_size, e_machine; size_t printed =3D 0; =20 - if (sc =3D=3D NULL) - return -1; =20 thread =3D machine__findnew_thread(trace->host, sample->pid, sample->tid); + e_machine =3D thread__e_machine(thread, trace->host); + sc =3D trace__syscall_info(trace, evsel, e_machine, id); + if (sc =3D=3D NULL) + return -1; ttrace =3D thread__trace(thread, trace); /* * We need to get ttrace just to make sure it is there when syscall__scnp= rintf_args() @@ -2883,15 +2884,15 @@ static int trace__sys_exit(struct trace *trace, str= uct evsel *evsel, bool duration_calculated =3D false; struct thread *thread; int id =3D perf_evsel__sc_tp_uint(evsel, id, sample), err =3D -1, callcha= in_ret =3D 0, printed =3D 0; - int alignment =3D trace->args_alignment; - /* TODO: get e_machine from thread. */ - struct syscall *sc =3D trace__syscall_info(trace, evsel, EM_HOST, id); + int alignment =3D trace->args_alignment, e_machine; + struct syscall *sc; struct thread_trace *ttrace; =20 - if (sc =3D=3D NULL) - return -1; - thread =3D machine__findnew_thread(trace->host, sample->pid, sample->tid); + e_machine =3D thread__e_machine(thread, trace->host); + sc =3D trace__syscall_info(trace, evsel, e_machine, id); + if (sc =3D=3D NULL) + goto out_put; ttrace =3D thread__trace(thread, trace); if (ttrace =3D=3D NULL) goto out_put; @@ -3238,8 +3239,8 @@ static int trace__event_handler(struct trace *trace, = struct evsel *evsel, =20 if (evsel =3D=3D trace->syscalls.events.bpf_output) { int id =3D perf_evsel__sc_tp_uint(evsel, id, sample); - /* TODO: get e_machine from thread. */ - struct syscall *sc =3D trace__syscall_info(trace, evsel, EM_HOST, id); + int e_machine =3D thread ? thread__e_machine(thread, trace->host) : EM_H= OST; + struct syscall *sc =3D trace__syscall_info(trace, evsel, e_machine, id); =20 if (sc) { fprintf(trace->output, "%s(", sc->name); @@ -4889,6 +4890,7 @@ static size_t trace__fprintf_thread(FILE *fp, struct = thread *thread, struct trac { size_t printed =3D 0; struct thread_trace *ttrace =3D thread__priv(thread); + int e_machine =3D thread__e_machine(thread, trace->host); double ratio; =20 if (ttrace =3D=3D NULL) @@ -4908,8 +4910,7 @@ static size_t trace__fprintf_thread(FILE *fp, struct = thread *thread, struct trac else if (fputc('\n', fp) !=3D EOF) ++printed; =20 - /* TODO: get e_machine from thread. */ - printed +=3D thread__dump_stats(ttrace, trace, EM_HOST, fp); + printed +=3D thread__dump_stats(ttrace, trace, e_machine, fp); =20 return printed; } diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 0ffdd52d86d7..89585f53c1d5 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -1,5 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 +#include #include +#include #include #include #include @@ -16,6 +18,7 @@ #include "symbol.h" #include "unwind.h" #include "callchain.h" +#include "dwarf-regs.h" =20 #include =20 @@ -51,6 +54,7 @@ struct thread *thread__new(pid_t pid, pid_t tid) thread__set_ppid(thread, -1); thread__set_cpu(thread, -1); thread__set_guest_cpu(thread, -1); + thread__set_e_machine(thread, EM_NONE); thread__set_lbr_stitch_enable(thread, false); INIT_LIST_HEAD(thread__namespaces_list(thread)); INIT_LIST_HEAD(thread__comm_list(thread)); @@ -423,6 +427,82 @@ void thread__find_cpumode_addr_location(struct thread = *thread, u64 addr, } } =20 +static uint16_t read_proc_e_machine_for_pid(pid_t pid) +{ + char path[6 /* "/proc/" */ + 11 /* max length of pid */ + 5 /* "/exe\0" *= /]; + int fd; + uint16_t e_machine =3D EM_NONE; + + snprintf(path, sizeof(path), "/proc/%d/exe", pid); + fd =3D open(path, O_RDONLY); + if (fd >=3D 0) { + _Static_assert(offsetof(Elf32_Ehdr, e_machine) =3D=3D 18, "Unexpected of= fset"); + _Static_assert(offsetof(Elf64_Ehdr, e_machine) =3D=3D 18, "Unexpected of= fset"); + if (pread(fd, &e_machine, sizeof(e_machine), 18) !=3D sizeof(e_machine)) + e_machine =3D EM_NONE; + close(fd); + } + return e_machine; +} + +static int thread__e_machine_callback(struct map *map, void *machine) +{ + struct dso *dso =3D map__dso(map); + + _Static_assert(0 =3D=3D EM_NONE, "Unexpected EM_NONE"); + if (!dso) + return EM_NONE; + + return dso__e_machine(dso, machine); +} + +uint16_t thread__e_machine(struct thread *thread, struct machine *machine) +{ + pid_t tid, pid; + uint16_t e_machine =3D RC_CHK_ACCESS(thread)->e_machine; + + if (e_machine !=3D EM_NONE) + return e_machine; + + tid =3D thread__tid(thread); + pid =3D thread__pid(thread); + if (pid !=3D tid) { + struct thread *parent =3D machine__findnew_thread(machine, pid, pid); + + if (parent) { + e_machine =3D thread__e_machine(parent, machine); + thread__set_e_machine(thread, e_machine); + return e_machine; + } + /* Something went wrong, fallback. */ + } + /* Reading on the PID thread. First try to find from the maps. */ + e_machine =3D maps__for_each_map(thread__maps(thread), + thread__e_machine_callback, + machine); + if (e_machine =3D=3D EM_NONE) { + /* Maps failed, perhaps we're live with map events disabled. */ + bool is_live =3D machine->machines =3D=3D NULL; + + if (!is_live) { + /* Check if the session has a data file. */ + struct perf_session *session =3D container_of(machine->machines, + struct perf_session, + machines); + + is_live =3D !!session->data; + } + /* Read from /proc/pid/exe if live. */ + if (is_live) + e_machine =3D read_proc_e_machine_for_pid(pid); + } + if (e_machine !=3D EM_NONE) + thread__set_e_machine(thread, e_machine); + else + e_machine =3D EM_HOST; + return e_machine; +} + struct thread *thread__main_thread(struct machine *machine, struct thread = *thread) { if (thread__pid(thread) =3D=3D thread__tid(thread)) diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index 6cbf6eb2812e..cd574a896418 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -60,7 +60,11 @@ DECLARE_RC_STRUCT(thread) { struct srccode_state srccode_state; bool filter; int filter_entry_depth; - + /** + * @e_machine: The ELF EM_* associated with the thread. EM_NONE if not + * computed. + */ + uint16_t e_machine; /* LBR call stack stitch */ bool lbr_stitch_enable; struct lbr_stitch *lbr_stitch; @@ -302,6 +306,14 @@ static inline void thread__set_filter_entry_depth(stru= ct thread *thread, int dep RC_CHK_ACCESS(thread)->filter_entry_depth =3D depth; } =20 +uint16_t thread__e_machine(struct thread *thread, struct machine *machine); + +static inline void thread__set_e_machine(struct thread *thread, uint16_t e= _machine) +{ + RC_CHK_ACCESS(thread)->e_machine =3D e_machine; +} + + static inline bool thread__lbr_stitch_enable(const struct thread *thread) { return RC_CHK_ACCESS(thread)->lbr_stitch_enable; --=20 2.49.0.rc1.451.g8f38331e32-goog