linux-csky.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Charlie Jenkins <charlie@rivosinc.com>
To: Ian Rogers <irogers@google.com>
Cc: "Peter Zijlstra" <peterz@infradead.org>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Arnaldo Carvalho de Melo" <acme@kernel.org>,
	"Namhyung Kim" <namhyung@kernel.org>,
	"Mark Rutland" <mark.rutland@arm.com>,
	"Alexander Shishkin" <alexander.shishkin@linux.intel.com>,
	"Jiri Olsa" <jolsa@kernel.org>,
	"Adrian Hunter" <adrian.hunter@intel.com>,
	"Kan Liang" <kan.liang@linux.intel.com>,
	"John Garry" <john.g.garry@oracle.com>,
	"Will Deacon" <will@kernel.org>,
	"James Clark" <james.clark@linaro.org>,
	"Mike Leach" <mike.leach@linaro.org>,
	"Leo Yan" <leo.yan@linux.dev>, "Guo Ren" <guoren@kernel.org>,
	"Paul Walmsley" <paul.walmsley@sifive.com>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Albert Ou" <aou@eecs.berkeley.edu>,
	"Bibo Mao" <maobibo@loongson.cn>, "Arnd Bergmann" <arnd@arndb.de>,
	"Huacai Chen" <chenhuacai@kernel.org>,
	"Catalin Marinas" <catalin.marinas@arm.com>,
	"Jiri Slaby" <jirislaby@kernel.org>,
	"Björn Töpel" <bjorn@rivosinc.com>,
	"Howard Chu" <howardchu95@gmail.com>,
	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
Subject: Re: [PATCH v2 4/7] perf thread: Add support for reading the e_machine type for a thread
Date: Mon, 10 Feb 2025 16:20:42 -0800	[thread overview]
Message-ID: <Z6qX2gUQpdSkG253@ghost> (raw)
In-Reply-To: <20250210165108.95894-5-irogers@google.com>

On Mon, Feb 10, 2025 at 08:51:05AM -0800, Ian Rogers wrote:
> 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.

Reviewed-by: Charlie Jenkins <charlie@rivosinc.com>
Tested-by: Charlie Jenkins <charlie@rivosinc.com>

> 
> Signed-off-by: Ian Rogers <irogers@google.com>
> Reviewed-by: Howard Chu <howardchu95@gmail.com>
> ---
>  tools/perf/builtin-trace.c | 41 ++++++++++++++++---------------
>  tools/perf/util/thread.c   | 50 ++++++++++++++++++++++++++++++++++++++
>  tools/perf/util/thread.h   | 14 ++++++++++-
>  3 files changed, 85 insertions(+), 20 deletions(-)
> 
> diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
> index 4b77c2ab3dba..1ae609555018 100644
> --- a/tools/perf/builtin-trace.c
> +++ b/tools/perf/builtin-trace.c
> @@ -2678,16 +2678,17 @@ static int trace__sys_enter(struct trace *trace, struct evsel *evsel,
>  	int printed = 0;
>  	struct thread *thread;
>  	int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
> -	int augmented_args_size = 0;
> +	int augmented_args_size = 0, e_machine;
>  	void *augmented_args = NULL;
>  	/* TODO: get e_machine from thread. */
> -	struct syscall *sc = trace__syscall_info(trace, evsel, EM_HOST, id);
> +	struct syscall *sc;
>  	struct thread_trace *ttrace;
>  
> -	if (sc == NULL)
> -		return -1;
> -
>  	thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
> +	e_machine = thread__e_machine(thread, trace->host);
> +	sc = trace__syscall_info(trace, evsel, e_machine, id);
> +	if (sc == NULL)
> +		goto out_put;
>  	ttrace = thread__trace(thread, trace->output);
>  	if (ttrace == NULL)
>  		goto out_put;
> @@ -2756,16 +2757,18 @@ static int trace__fprintf_sys_enter(struct trace *trace, struct evsel *evsel,
>  	struct thread *thread;
>  	int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1;
>  	/* TODO: get e_machine from thread. */
> -	struct syscall *sc = trace__syscall_info(trace, evsel, EM_HOST, id);
> +	struct syscall *sc;
>  	char msg[1024];
>  	void *args, *augmented_args = NULL;
> -	int augmented_args_size;
> +	int augmented_args_size, e_machine;
>  	size_t printed = 0;
>  
> -	if (sc == NULL)
> -		return -1;
>  
>  	thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
> +	e_machine = thread__e_machine(thread, trace->host);
> +	sc = trace__syscall_info(trace, evsel, e_machine, id);
> +	if (sc == NULL)
> +		return -1;
>  	ttrace = thread__trace(thread, trace->output);
>  	/*
>  	 * We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
> @@ -2830,15 +2833,15 @@ static int trace__sys_exit(struct trace *trace, struct evsel *evsel,
>  	bool duration_calculated = false;
>  	struct thread *thread;
>  	int id = perf_evsel__sc_tp_uint(evsel, id, sample), err = -1, callchain_ret = 0, printed = 0;
> -	int alignment = trace->args_alignment;
> -	/* TODO: get e_machine from thread. */
> -	struct syscall *sc = trace__syscall_info(trace, evsel, EM_HOST, id);
> +	int alignment = trace->args_alignment, e_machine;
> +	struct syscall *sc;
>  	struct thread_trace *ttrace;
>  
> -	if (sc == NULL)
> -		return -1;
> -
>  	thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
> +	e_machine = thread__e_machine(thread, trace->host);
> +	sc = trace__syscall_info(trace, evsel, e_machine, id);
> +	if (sc == NULL)
> +		goto out_put;
>  	ttrace = thread__trace(thread, trace->output);
>  	if (ttrace == NULL)
>  		goto out_put;
> @@ -3185,8 +3188,8 @@ static int trace__event_handler(struct trace *trace, struct evsel *evsel,
>  
>  	if (evsel == trace->syscalls.events.bpf_output) {
>  		int id = perf_evsel__sc_tp_uint(evsel, id, sample);
> -		/* TODO: get e_machine from thread. */
> -		struct syscall *sc = trace__syscall_info(trace, evsel, EM_HOST, id);
> +		int e_machine = thread ? thread__e_machine(thread, trace->host) : EM_HOST;
> +		struct syscall *sc = trace__syscall_info(trace, evsel, e_machine, id);
>  
>  		if (sc) {
>  			fprintf(trace->output, "%s(", sc->name);
> @@ -4764,6 +4767,7 @@ static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trac
>  {
>  	size_t printed = 0;
>  	struct thread_trace *ttrace = thread__priv(thread);
> +	int e_machine = thread__e_machine(thread, trace->host);
>  	double ratio;
>  
>  	if (ttrace == NULL)
> @@ -4783,8 +4787,7 @@ static size_t trace__fprintf_thread(FILE *fp, struct thread *thread, struct trac
>  	else if (fputc('\n', fp) != EOF)
>  		++printed;
>  
> -	/* TODO: get e_machine from thread. */
> -	printed += thread__dump_stats(ttrace, trace, EM_HOST, fp);
> +	printed += thread__dump_stats(ttrace, trace, e_machine, fp);
>  
>  	return printed;
>  }
> diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
> index 0ffdd52d86d7..a07446a280ed 100644
> --- a/tools/perf/util/thread.c
> +++ b/tools/perf/util/thread.c
> @@ -1,5 +1,7 @@
>  // SPDX-License-Identifier: GPL-2.0
> +#include <elf.h>
>  #include <errno.h>
> +#include <fcntl.h>
>  #include <stdlib.h>
>  #include <stdio.h>
>  #include <string.h>
> @@ -16,6 +18,7 @@
>  #include "symbol.h"
>  #include "unwind.h"
>  #include "callchain.h"
> +#include "dwarf-regs.h"
>  
>  #include <api/fs/fs.h>
>  
> @@ -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,52 @@ void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
>  	}
>  }
>  
> +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 = EM_NONE;
> +
> +	snprintf(path, sizeof(path), "/proc/%d/exe", pid);
> +	fd = open(path, O_RDONLY);
> +	if (fd >= 0) {
> +		_Static_assert(offsetof(Elf32_Ehdr, e_machine) == 18, "Unexpected offset");
> +		_Static_assert(offsetof(Elf64_Ehdr, e_machine) == 18, "Unexpected offset");
> +		if (pread(fd, &e_machine, sizeof(e_machine), 18) != sizeof(e_machine))
> +			e_machine = EM_NONE;
> +		close(fd);
> +	}
> +	return e_machine;
> +}
> +
> +uint16_t thread__e_machine(struct thread *thread, struct machine *machine)
> +{
> +	pid_t tid, pid;
> +	uint16_t e_machine = RC_CHK_ACCESS(thread)->e_machine;
> +
> +	if (e_machine != EM_NONE)
> +		return e_machine;
> +
> +	tid = thread__tid(thread);
> +	pid = thread__pid(thread);
> +	if (pid != tid) {
> +		struct thread *parent = machine__findnew_thread(machine, pid, pid);
> +
> +		if (parent) {
> +			e_machine = thread__e_machine(parent, machine);
> +			thread__set_e_machine(thread, e_machine);
> +			return e_machine;
> +		}
> +		/* Something went wrong, fallback. */
> +	}
> +	e_machine = read_proc_e_machine_for_pid(pid);
> +	if (e_machine != EM_NONE)
> +		thread__set_e_machine(thread, e_machine);
> +	else
> +		e_machine = EM_HOST;
> +	return e_machine;
> +}
> +
>  struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
>  {
>  	if (thread__pid(thread) == 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(struct thread *thread, int dep
>  	RC_CHK_ACCESS(thread)->filter_entry_depth = depth;
>  }
>  
> +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 = e_machine;
> +}
> +
> +
>  static inline bool thread__lbr_stitch_enable(const struct thread *thread)
>  {
>  	return RC_CHK_ACCESS(thread)->lbr_stitch_enable;
> -- 
> 2.48.1.502.g6dc24dfdaf-goog
> 

  reply	other threads:[~2025-02-11  0:20 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-10 16:51 [PATCH v2 0/7] perf: Support multiple system call tables in the build Ian Rogers
2025-02-10 16:51 ` [PATCH v2 1/7] perf syscalltble: Remove syscall_table.h Ian Rogers
2025-02-10 23:48   ` Charlie Jenkins
2025-02-10 16:51 ` [PATCH v2 2/7] perf trace: Reorganize syscalls Ian Rogers
2025-02-11  0:17   ` Charlie Jenkins
2025-02-10 16:51 ` [PATCH v2 3/7] perf syscalltbl: Remove struct syscalltbl Ian Rogers
2025-02-11  0:19   ` Charlie Jenkins
2025-02-11  7:48   ` Arnd Bergmann
2025-02-11 16:18     ` Ian Rogers
2025-02-11 16:34       ` Arnd Bergmann
2025-02-11 17:32         ` Ian Rogers
2025-02-10 16:51 ` [PATCH v2 4/7] perf thread: Add support for reading the e_machine type for a thread Ian Rogers
2025-02-11  0:20   ` Charlie Jenkins [this message]
2025-02-10 16:51 ` [PATCH v2 5/7] perf trace beauty: Add syscalltbl.sh generating all system call tables Ian Rogers
2025-02-11  0:22   ` Charlie Jenkins
2025-02-11  5:08     ` Ian Rogers
2025-02-11  8:08   ` Arnd Bergmann
2025-02-11 17:24     ` Ian Rogers
2025-02-11 17:53       ` Arnd Bergmann
2025-02-11 18:45         ` Ian Rogers
2025-02-12 13:59         ` David Laight
2025-02-10 16:51 ` [PATCH v2 6/7] perf syscalltbl: Use lookup table containing multiple architectures Ian Rogers
2025-02-10 23:39   ` Charlie Jenkins
2025-02-11  5:15     ` Ian Rogers
2025-02-11  0:23   ` Charlie Jenkins
2025-02-10 16:51 ` [PATCH v2 7/7] perf build: Remove Makefile.syscalls Ian Rogers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Z6qX2gUQpdSkG253@ghost \
    --to=charlie@rivosinc.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=aou@eecs.berkeley.edu \
    --cc=arnd@arndb.de \
    --cc=bjorn@rivosinc.com \
    --cc=catalin.marinas@arm.com \
    --cc=chenhuacai@kernel.org \
    --cc=guoren@kernel.org \
    --cc=howardchu95@gmail.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jirislaby@kernel.org \
    --cc=john.g.garry@oracle.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=leo.yan@linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-csky@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=maobibo@loongson.cn \
    --cc=mark.rutland@arm.com \
    --cc=mike.leach@linaro.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    --cc=peterz@infradead.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).