Re: [PATCH] printk: inject caller information into the body of message

From: Steven Rostedt
Date: Thu Sep 13 2018 - 21:12:58 EST


On Thu, 13 Sep 2018 16:12:54 +0900
Sergey Senozhatsky <sergey.senozhatsky.work@xxxxxxxxx> wrote:
Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@xxxxxxxxx>
> ---
> include/linux/seq_buf.h | 35 +++++++++++++++++++++++++++++++
> lib/seq_buf.c | 46 +++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 81 insertions(+)
>
> diff --git a/include/linux/seq_buf.h b/include/linux/seq_buf.h
> index aa5deb041c25..5e9a5ff9a440 100644
> --- a/include/linux/seq_buf.h
> +++ b/include/linux/seq_buf.h
> @@ -23,6 +23,36 @@ struct seq_buf {
> loff_t readpos;
> };
>
> +#define __SEQ_BUF_INITIALIZER(buf, length) { \
> + .buffer = (buf), \
> + .size = (length), \
> + .len = 0, \
> + .readpos = 0, }

Nit, but the end bracket '}' should be on it's own line. Even when
part of a macro.

> +
> +#ifdef CONFIG_PRINTK
> +#define __PR_LINE_BUF_SZ 80
> +#else
> +#define __PR_LINE_BUF_SZ 0
> +#endif
> +
> +struct pr_line {
> + struct seq_buf sb;
> + char *level;
> +};
> +
> +#define DEFINE_PR_LINE(lev, name) \
> + char __line[__PR_LINE_BUF_SZ]; \

To protect against name space collision could you use:

char __line_##name[__PR_LINE_BUF_SZ];

> + struct pr_line name = { \
> + .sb = __SEQ_BUF_INITIALIZER(__line, __PR_LINE_BUF_SZ), \
> + .level = lev, \
> + }
> +
> +#define DEFINE_PR_LINE_BUF(lev, name, buf, sz) \
> + struct pr_line name = { \
> + .sb = __SEQ_BUF_INITIALIZER(buf, (sz)), \
> + .level = lev, \
> + }
> +
> static inline void seq_buf_clear(struct seq_buf *s)
> {
> s->len = 0;
> @@ -131,4 +161,9 @@ extern int
> seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary);
> #endif
>
> +extern __printf(2, 0)
> +int vpr_line(struct pr_line *pl, const char *fmt, va_list args);
> +extern __printf(2, 3)
> +int pr_line(struct pr_line *pl, const char *fmt, ...);
> +extern void pr_line_flush(struct pr_line *pl);
> #endif /* _LINUX_SEQ_BUF_H */
> diff --git a/lib/seq_buf.c b/lib/seq_buf.c
> index 11f2ae0f9099..29bc4f24b83e 100644
> --- a/lib/seq_buf.c
> +++ b/lib/seq_buf.c
> @@ -324,3 +324,49 @@ int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, int cnt)
> s->readpos += cnt;
> return cnt;
> }
> +
> +int vpr_line(struct pr_line *pl, const char *fmt, va_list args)
> +{
> + struct seq_buf *s = &pl->sb;
> + int ret, len;
> +
> + if (fmt[0] == '\n') {
> + pr_line_flush(pl);
> + return 0;
> + }
> +
> + ret = seq_buf_vprintf(s, fmt, args);
> +
> + len = seq_buf_used(s);
> + if (len && s->buffer[len - 1] == '\n')
> + pr_line_flush(pl);
> +
> + return ret;
> +}
> +EXPORT_SYMBOL(vpr_line);
> +
> +int pr_line(struct pr_line *pl, const char *fmt, ...)
> +{
> + va_list ap;
> + int ret;
> +
> + va_start(ap, fmt);
> + ret = vpr_line(pl, fmt, ap);
> + va_end(ap);
> +
> + return ret;
> +}
> +EXPORT_SYMBOL(pr_line);
> +
> +void pr_line_flush(struct pr_line *pl)
> +{
> + struct seq_buf *s = &pl->sb;
> + int len = seq_buf_used(s);
> +
> + if (!len)
> + return;
> +
> + printk("%s%.*s", pl->level, len, s->buffer);
> + seq_buf_clear(s);
> +}
> +EXPORT_SYMBOL(pr_line_flush);

The rest looks fine to me.

Acked-by: Steven Rostedt (VMware) <rostedt@xxxxxxxxxxx>

-- Steve