perf thread: Add support for reading the e_machine type for a thread

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 <irogers@google.com>
Reviewed-by: Namhyung Kim <namhyung@kernel.org>
Acked-by: Arnaldo Carvalho de Melo <acme@kernel.org>
Link: https://lore.kernel.org/r/20250319050741.269828-8-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
This commit is contained in:
Ian Rogers 2025-03-18 22:07:34 -07:00 committed by Namhyung Kim
parent afffec6f03
commit 70351029b5
3 changed files with 115 additions and 22 deletions

View file

@ -2731,16 +2731,16 @@ 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);
if (ttrace == NULL)
goto out_put;
@ -2808,17 +2808,18 @@ static int trace__fprintf_sys_enter(struct trace *trace, struct evsel *evsel,
struct thread_trace *ttrace;
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);
/*
* We need to get ttrace just to make sure it is there when syscall__scnprintf_args()
@ -2883,15 +2884,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);
if (ttrace == NULL)
goto out_put;
@ -3238,8 +3239,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);
@ -4889,6 +4890,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)
@ -4908,8 +4910,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;
}

View file

@ -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,82 @@ 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;
}
static int thread__e_machine_callback(struct map *map, void *machine)
{
struct dso *dso = map__dso(map);
_Static_assert(0 == 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 = 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. */
}
/* Reading on the PID thread. First try to find from the maps. */
e_machine = maps__for_each_map(thread__maps(thread),
thread__e_machine_callback,
machine);
if (e_machine == EM_NONE) {
/* Maps failed, perhaps we're live with map events disabled. */
bool is_live = machine->machines == NULL;
if (!is_live) {
/* Check if the session has a data file. */
struct perf_session *session = container_of(machine->machines,
struct perf_session,
machines);
is_live = !!session->data;
}
/* Read from /proc/pid/exe if live. */
if (is_live)
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))

View file

@ -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;