Re: [RFC PATCH v6 1/9] tracing: Add array printing helpers

Steven Rostedt
2014-12-08T15:42:10Z

On Fri, 5 Dec 2014 19:04:12 +0000
"Javi Merino" <javi.merino@arm.com> wrote:

> +static const char *
> +ftrace_print_array_seq(struct trace_seq *p, const void *buf, int buf_len,
> + bool (*iterator)(struct trace_seq *p, const char *prefix,
> + const void **buf, int *buf_len))
> +{
> + const char *ret = trace_seq_buffer_ptr(p);
> + const char *prefix = "";
> +
> + trace_seq_putc(p, '{');
> +
> + while (iterator(p, prefix, &buf, &buf_len))
> + prefix = ",";
> +
> + trace_seq_putc(p, '}');
> + trace_seq_putc(p, 0);
> +
> + return ret;
> +}
> +
> +#define DEFINE_PRINT_ARRAY(type, printk_type, format) \
> +static bool \
> +ftrace_print_array_iterator_##type(struct trace_seq *p, const char *prefix, \
> + const void **buf, int *buf_len) \
> +{ \
> + const type *__src = *buf; \
> + \
> + if (*buf_len < sizeof(*__src)) \
> + return false; \
> + \
> + trace_seq_printf(p, "%s" format, prefix, (printk_type)*__src++); \
> + \
> + *buf = __src; \
> + *buf_len -= sizeof(*__src); \
> + \
> + return true; \
> +} \
> + \
> +const char *ftrace_print_##type##_array_seq( \
> + struct trace_seq *p, const type *buf, int count) \
> +{ \
> + return ftrace_print_array_seq(p, buf, (count) * sizeof(type), \
> + ftrace_print_array_iterator_##type); \
> +} \
> + \
> +EXPORT_SYMBOL(ftrace_print_##type##_array_seq)
> +
> +DEFINE_PRINT_ARRAY(u8, unsigned int, "0x%x");
> +DEFINE_PRINT_ARRAY(u16, unsigned int, "0x%x");
> +DEFINE_PRINT_ARRAY(u32, unsigned int, "0x%x");
> +DEFINE_PRINT_ARRAY(u64, unsigned long long, "0x%llx");
> +

I would really like to avoid adding a bunch of macros for each type.
Can't we have something like this:

ftrace_print_array(struct trace_seq *p, void *buf, int buf_len,
int size)
{
char *prefix = "";
void *ptr = buf;

while (ptr < buf + buf_len) {
switch(size) {
case 8:
trace_seq_printf("%s0x%x", prefix,
*(unsigned char *)ptr);
break;
case 16:
trace_seq_printf("%s0x%x", prefix,
*(unsigned short *)ptr);
break;
case 32:
trace_seq_printf("%s0x%x", prefix,
*(unsigned int *)ptr);
break;
case 64:
trace_seq_printf("%s0x%llx", prefix,
*(unsigned long long *)ptr);
break;
default:
BUG();
}
prefix = ",";
ptr += size;
}

}

We probably could even make the "BUG()" into a build bug, with a little
work.

-- Steve

> int ftrace_raw_output_prep(struct trace_iterator *iter,
> struct trace_event *trace_event)
> {