]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
perf machine: Factor creating a "live" machine out of dwarf-unwind
authorIan Rogers <irogers@google.com>
Thu, 13 Mar 2025 05:29:51 +0000 (22:29 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 28 May 2025 12:24:59 +0000 (09:24 -0300)
Factor out for use in places other than the dwarf unwinding tests for
libunwind.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Anne Macedo <retpolanne@posteo.net>
Cc: Dmitriy Vyukov <dvyukov@google.com>
Cc: Dr. David Alan Gilbert <linux@treblig.org>
Cc: Howard Chu <howardchu95@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Michael Petlan <mpetlan@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Yicong Yang <yangyicong@hisilicon.com>
Link: https://lore.kernel.org/r/20250313052952.871958-1-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/tests/dwarf-unwind.c
tools/perf/util/machine.c
tools/perf/util/machine.h

index 4803ab2d97ba678e70c9cb755abd41cac7af5af0..525c46b7971adb5e969bf5ceb32c09ae3b19d8e3 100644 (file)
@@ -15,7 +15,6 @@
 #include "symbol.h"
 #include "thread.h"
 #include "callchain.h"
-#include "util/synthetic-events.h"
 
 /* For bsearch. We try to unwind functions in shared object. */
 #include <stdlib.h>
 #define NO_TAIL_CALL_BARRIER __asm__ __volatile__("" : : : "memory");
 #endif
 
-static int mmap_handler(const struct perf_tool *tool __maybe_unused,
-                       union perf_event *event,
-                       struct perf_sample *sample,
-                       struct machine *machine)
-{
-       return machine__process_mmap2_event(machine, event, sample);
-}
-
-static int init_live_machine(struct machine *machine)
-{
-       union perf_event event;
-       pid_t pid = getpid();
-
-       memset(&event, 0, sizeof(event));
-       return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
-                                                 mmap_handler, machine, true);
-}
-
 /*
  * We need to keep these functions global, despite the
  * fact that they are used only locally in this object,
@@ -202,8 +183,12 @@ noinline int test__dwarf_unwind(struct test_suite *test __maybe_unused,
        struct machine *machine;
        struct thread *thread;
        int err = -1;
+       pid_t pid = getpid();
 
-       machine = machine__new_host();
+       callchain_param.record_mode = CALLCHAIN_DWARF;
+       dwarf_callchain_users = true;
+
+       machine = machine__new_live(/*kernel_maps=*/true, pid);
        if (!machine) {
                pr_err("Could not get machine\n");
                return -1;
@@ -214,18 +199,10 @@ noinline int test__dwarf_unwind(struct test_suite *test __maybe_unused,
                return -1;
        }
 
-       callchain_param.record_mode = CALLCHAIN_DWARF;
-       dwarf_callchain_users = true;
-
-       if (init_live_machine(machine)) {
-               pr_err("Could not init machine\n");
-               goto out;
-       }
-
        if (verbose > 1)
                machine__fprintf(machine, stderr);
 
-       thread = machine__find_thread(machine, getpid(), getpid());
+       thread = machine__find_thread(machine, pid, pid);
        if (!thread) {
                pr_err("Could not get thread\n");
                goto out;
index 2531b373f2cf7ca0aaad252d8206de4d53e16088..c5e28d15323faec13822dcaea6dada87af967a34 100644 (file)
@@ -20,6 +20,7 @@
 #include "path.h"
 #include "srcline.h"
 #include "symbol.h"
+#include "synthetic-events.h"
 #include "sort.h"
 #include "strlist.h"
 #include "target.h"
@@ -128,23 +129,57 @@ out:
        return 0;
 }
 
-struct machine *machine__new_host(void)
+static struct machine *__machine__new_host(bool kernel_maps)
 {
        struct machine *machine = malloc(sizeof(*machine));
 
-       if (machine != NULL) {
-               machine__init(machine, "", HOST_KERNEL_ID);
+       if (!machine)
+               return NULL;
 
-               if (machine__create_kernel_maps(machine) < 0)
-                       goto out_delete;
+       machine__init(machine, "", HOST_KERNEL_ID);
 
-               machine->env = &perf_env;
+       if (kernel_maps && machine__create_kernel_maps(machine) < 0) {
+               free(machine);
+               return NULL;
        }
+       machine->env = &perf_env;
+       return machine;
+}
+
+struct machine *machine__new_host(void)
+{
+       return __machine__new_host(/*kernel_maps=*/true);
+}
+
+static int mmap_handler(const struct perf_tool *tool __maybe_unused,
+                       union perf_event *event,
+                       struct perf_sample *sample,
+                       struct machine *machine)
+{
+       return machine__process_mmap2_event(machine, event, sample);
+}
 
+static int machine__init_live(struct machine *machine, pid_t pid)
+{
+       union perf_event event;
+
+       memset(&event, 0, sizeof(event));
+       return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
+                                                 mmap_handler, machine, true);
+}
+
+struct machine *machine__new_live(bool kernel_maps, pid_t pid)
+{
+       struct machine *machine = __machine__new_host(kernel_maps);
+
+       if (!machine)
+               return NULL;
+
+       if (machine__init_live(machine, pid)) {
+               machine__delete(machine);
+               return NULL;
+       }
        return machine;
-out_delete:
-       free(machine);
-       return NULL;
 }
 
 struct machine *machine__new_kallsyms(void)
index b56abec84fed1e3f1e92b6e221ef7b5d96f6624c..180b369c366c2711ff8968351429cd6867ec3452 100644 (file)
@@ -171,6 +171,7 @@ void machines__set_comm_exec(struct machines *machines, bool comm_exec);
 
 struct machine *machine__new_host(void);
 struct machine *machine__new_kallsyms(void);
+struct machine *machine__new_live(bool kernel_maps, pid_t pid);
 int machine__init(struct machine *machine, const char *root_dir, pid_t pid);
 void machine__exit(struct machine *machine);
 void machine__delete_threads(struct machine *machine);