[PATCH v2 14/15] dyndbg: write debug logs to trace instance

From: Łukasz Bartosik
Date: Thu Nov 30 2023 - 18:45:06 EST


When trace is enabled (T flag is set) and trace destination field
is set to a value in range [0..62] (TRACE_DST_MAX(63) is reserved
for writing to trace events) then debug logs will be written to a
trace instance pointed by inst[trace destination].name, e.g. when
trace destination value is 2 and inst[2].name is set to tbt then
debug logs will be written to <debugfs>/tracing/instances/tbt
instance.

Before using trace instance as a destination for writing debug
logs it has to be explicitly opened with open command.

Signed-off-by: Łukasz Bartosik <lb@xxxxxxxxxxxx>
---
lib/dynamic_debug.c | 32 ++++++++++++++++++++++++++++----
1 file changed, 28 insertions(+), 4 deletions(-)

diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c
index ae05b3728520..afcc536c2e91 100644
--- a/lib/dynamic_debug.c
+++ b/lib/dynamic_debug.c
@@ -141,6 +141,11 @@ static inline bool has_tr_default_dst(void)
return tr.default_dst != DST_NOT_SET;
}

+static inline bool is_tr_event_dst(const struct _ddebug *desc)
+{
+ return desc->ctrl.trace_dst == TRACE_DST_MAX;
+}
+
static int find_tr_instance(const char *name)
{
int idx;
@@ -1251,8 +1256,8 @@ static DEFINE_PER_CPU(struct ddebug_trace_bufs, ddebug_trace_bufs);
static DEFINE_PER_CPU(int, ddebug_trace_reserve);

__printf(3, 0)
-static void ddebug_trace(struct _ddebug *desc, const struct device *dev,
- const char *fmt, va_list args)
+static void ddebug_trace_event(struct _ddebug *desc, const struct device *dev,
+ const char *fmt, va_list args)
{
struct ddebug_trace_buf *buf;
int bufidx;
@@ -1283,6 +1288,18 @@ static void ddebug_trace(struct _ddebug *desc, const struct device *dev,
preempt_enable_notrace();
}

+__printf(2, 0)
+static void ddebug_trace_instance(struct _ddebug *desc, const char *fmt,
+ va_list *args)
+{
+ struct va_format vaf = { .fmt = fmt, .va = args};
+ struct trace_array *arr = tr.inst[get_trace_dst(desc)].arr;
+
+ WARN_ON_ONCE(!arr);
+
+ trace_array_printk(arr, 0, "%pV", &vaf);
+}
+
__printf(2, 3)
static void ddebug_printk(struct _ddebug *desc, const char *fmt, ...)
{
@@ -1295,7 +1312,11 @@ static void ddebug_printk(struct _ddebug *desc, const char *fmt, ...)
* All callers include the KERN_DEBUG prefix to keep the
* vprintk case simple; strip it out for tracing.
*/
- ddebug_trace(desc, NULL, fmt + strlen(KERN_DEBUG), args);
+ if (is_tr_event_dst(desc))
+ ddebug_trace_event(desc, NULL,
+ fmt + strlen(KERN_DEBUG), args);
+ else
+ ddebug_trace_instance(desc, fmt, &args);
va_end(args);
}

@@ -1317,7 +1338,10 @@ static void ddebug_dev_printk(struct _ddebug *desc, const struct device *dev,
va_list args;

va_start(args, fmt);
- ddebug_trace(desc, dev, fmt, args);
+ if (is_tr_event_dst(desc))
+ ddebug_trace_event(desc, dev, fmt, args);
+ else
+ ddebug_trace_instance(desc, fmt, &args);
va_end(args);
}

--
2.43.0.rc2.451.g8631bc7472-goog