bool                    *wrapped;
 };
 
-static void arm_spe_set_timestamp(struct auxtrace_record *itr,
-                                 struct evsel *evsel)
-{
-       struct arm_spe_recording *ptr;
-       struct perf_pmu *arm_spe_pmu;
-       struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
-       u64 user_bits = 0, bit;
-
-       ptr = container_of(itr, struct arm_spe_recording, itr);
-       arm_spe_pmu = ptr->arm_spe_pmu;
-
-       if (term)
-               user_bits = term->val.cfg_chg;
-
-       bit = perf_pmu__format_bits(&arm_spe_pmu->format, "ts_enable");
-
-       /* Skip if user has set it */
-       if (bit & user_bits)
-               return;
-
-       evsel->core.attr.config |= bit;
-}
-
 static size_t
 arm_spe_info_priv_size(struct auxtrace_record *itr __maybe_unused,
                       struct evlist *evlist __maybe_unused)
         */
        if (!perf_cpu_map__empty(cpus)) {
                evsel__set_sample_bit(arm_spe_evsel, CPU);
-               arm_spe_set_timestamp(itr, arm_spe_evsel);
+               evsel__set_config_if_unset(arm_spe_pmu, arm_spe_evsel,
+                                          "ts_enable", 1);
        }
 
        /*
 
        return err;
 }
 
-static void intel_pt_config_sample_mode(struct perf_pmu *intel_pt_pmu,
-                                       struct evsel *evsel)
-{
-       u64 user_bits = 0, bits;
-       struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
-
-       if (term)
-               user_bits = term->val.cfg_chg;
-
-       bits = perf_pmu__format_bits(&intel_pt_pmu->format, "psb_period");
-
-       /* Did user change psb_period */
-       if (bits & user_bits)
-               return;
-
-       /* Set psb_period to 0 */
-       evsel->core.attr.config &= ~bits;
-}
-
 static void intel_pt_min_max_sample_sz(struct evlist *evlist,
                                       size_t *min_sz, size_t *max_sz)
 {
                return 0;
 
        if (opts->auxtrace_sample_mode)
-               intel_pt_config_sample_mode(intel_pt_pmu, intel_pt_evsel);
+               evsel__set_config_if_unset(intel_pt_pmu, intel_pt_evsel,
+                                          "psb_period", 0);
 
        err = intel_pt_validate_config(intel_pt_pmu, intel_pt_evsel);
        if (err)
 
        ((((src) >> (pos)) & ((1ull << (size)) - 1)) << (63 - ((pos) + (size) - 1)))
 
 u64 evsel__bitfield_swap_branch_flags(u64 value);
+void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel,
+                               const char *config_name, u64 val);
+
 #endif /* __PERF_EVSEL_H */
 
 #include "strbuf.h"
 #include "fncache.h"
 #include "pmu-hybrid.h"
+#include "util/evsel_config.h"
 
 struct perf_pmu perf_pmu__fake;
 
        return pmu && pmu->auxtrace;
 }
 
+/*
+ * Set @config_name to @val as long as the user hasn't already set or cleared it
+ * by passing a config term on the command line.
+ *
+ * @val is the value to put into the bits specified by @config_name rather than
+ * the bit pattern. It is shifted into position by this function, so to set
+ * something to true, pass 1 for val rather than a pre shifted value.
+ */
+#define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask))
+void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel,
+                               const char *config_name, u64 val)
+{
+       u64 user_bits = 0, bits;
+       struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG);
+
+       if (term)
+               user_bits = term->val.cfg_chg;
+
+       bits = perf_pmu__format_bits(&pmu->format, config_name);
+
+       /* Do nothing if the user changed the value */
+       if (bits & user_bits)
+               return;
+
+       /* Otherwise replace it */
+       evsel->core.attr.config &= ~bits;
+       evsel->core.attr.config |= field_prep(bits, val);
+}
+
 struct perf_pmu *perf_pmu__find(const char *name)
 {
        struct perf_pmu *pmu;