[PATCH v4 4/5] perf callchain: Move callchain option parsing out of builtin

Ian Rogers
2026-03-17T03:06:21Z

record_opts__parse_callchain, record_callchain_opt,
record_parse_callchain_opt are shared by builtin-record, builtin-top
and builtin-trace, they are declared in callchain.h. Move the
declarations to callchain.c for consistency with the header.

Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-record.c | 59 -----------------------------------
tools/perf/util/callchain.c | 61 +++++++++++++++++++++++++++++++++++++
2 files changed, 61 insertions(+), 59 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 40917a0be238..e65cd7bf1477 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -2975,65 +2975,6 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
return status;
}

-static void callchain_debug(struct callchain_param *callchain)
-{
- static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" };
-
- pr_debug("callchain: type %s\n", str[callchain->record_mode]);
-
- if (callchain->record_mode == CALLCHAIN_DWARF)
- pr_debug("callchain: stack dump size %d\n",
- callchain->dump_size);
-}
-
-int record_opts__parse_callchain(struct record_opts *record,
- struct callchain_param *callchain,
- const char *arg, bool unset)
-{
- int ret;
- callchain->enabled = !unset;
-
- /* --no-call-graph */
- if (unset) {
- callchain->record_mode = CALLCHAIN_NONE;
- pr_debug("callchain: disabled\n");
- return 0;
- }
-
- ret = parse_callchain_record_opt(arg, callchain);
- if (!ret) {
- /* Enable data address sampling for DWARF unwind. */
- if (callchain->record_mode == CALLCHAIN_DWARF &&
- !record->record_data_mmap_set)
- record->record_data_mmap = true;
- callchain_debug(callchain);
- }
-
- return ret;
-}
-
-int record_parse_callchain_opt(const struct option *opt,
- const char *arg,
- int unset)
-{
- return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset);
-}
-
-int record_callchain_opt(const struct option *opt,
- const char *arg __maybe_unused,
- int unset __maybe_unused)
-{
- struct callchain_param *callchain = opt->value;
-
- callchain->enabled = true;
-
- if (callchain->record_mode == CALLCHAIN_NONE)
- callchain->record_mode = CALLCHAIN_FP;
-
- callchain_debug(callchain);
- return 0;
-}
-
static int perf_record_config(const char *var, const char *value, void *cb)
{
struct record *rec = cb;
diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c
index 8ff0898799ee..1203d9d23fda 100644
--- a/tools/perf/util/callchain.c
+++ b/tools/perf/util/callchain.c
@@ -16,6 +16,7 @@
#include <stdbool.h>
#include <errno.h>
#include <math.h>
+#include <subcmd/parse-options.h>
#include <linux/string.h>
#include <linux/zalloc.h>

@@ -30,6 +31,7 @@
#include "map.h"
#include "callchain.h"
#include "branch.h"
+#include "record.h"
#include "symbol.h"
#include "thread.h"
#include "util.h"
@@ -328,6 +330,65 @@ int parse_callchain_record(const char *arg, struct callchain_param *param)
return ret;
}

+static void callchain_debug(const struct callchain_param *callchain)
+{
+ static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" };
+
+ pr_debug("callchain: type %s\n", str[callchain->record_mode]);
+
+ if (callchain->record_mode == CALLCHAIN_DWARF)
+ pr_debug("callchain: stack dump size %d\n",
+ callchain->dump_size);
+}
+
+int record_opts__parse_callchain(struct record_opts *record,
+ struct callchain_param *callchain,
+ const char *arg, bool unset)
+{
+ int ret;
+ callchain->enabled = !unset;
+
+ /* --no-call-graph */
+ if (unset) {
+ callchain->record_mode = CALLCHAIN_NONE;
+ pr_debug("callchain: disabled\n");
+ return 0;
+ }
+
+ ret = parse_callchain_record_opt(arg, callchain);
+ if (!ret) {
+ /* Enable data address sampling for DWARF unwind. */
+ if (callchain->record_mode == CALLCHAIN_DWARF &&
+ !record->record_data_mmap_set)
+ record->record_data_mmap = true;
+ callchain_debug(callchain);
+ }
+
+ return ret;
+}
+
+int record_parse_callchain_opt(const struct option *opt,
+ const char *arg,
+ int unset)
+{
+ return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset);
+}
+
+int record_callchain_opt(const struct option *opt,
+ const char *arg __maybe_unused,
+ int unset __maybe_unused)
+{
+ struct callchain_param *callchain = opt->value;
+
+ callchain->enabled = true;
+
+ if (callchain->record_mode == CALLCHAIN_NONE)
+ callchain->record_mode = CALLCHAIN_FP;
+
+ callchain_debug(callchain);
+ return 0;
+}
+
int perf_callchain_config(const char *var, const char *value)
{
char *endptr;
--
2.53.0.851.ga537e3e6e9-goog