Re: [PATCH v2 5/7] stacktrace: introduce snprint_stack_trace for buffer output
From: Joonsoo Kim
Date: Sun Nov 23 2014 - 21:54:23 EST
On Fri, Nov 21, 2014 at 03:37:59PM -0800, Andrew Morton wrote:
> On Fri, 21 Nov 2014 17:14:04 +0900 Joonsoo Kim <iamjoonsoo.kim@xxxxxxx> wrote:
>
> > Current stacktrace only have the function for console output.
> > page_owner that will be introduced in following patch needs to print
> > the output of stacktrace into the buffer for our own output format
> > so so new function, snprint_stack_trace(), is needed.
> >
> > ...
> >
> > --- a/include/linux/stacktrace.h
> > +++ b/include/linux/stacktrace.h
> > @@ -20,6 +20,8 @@ extern void save_stack_trace_tsk(struct task_struct *tsk,
> > struct stack_trace *trace);
> >
> > extern void print_stack_trace(struct stack_trace *trace, int spaces);
> > +extern int snprint_stack_trace(char *buf, int buf_len,
> > + struct stack_trace *trace, int spaces);
> >
> > #ifdef CONFIG_USER_STACKTRACE_SUPPORT
> > extern void save_stack_trace_user(struct stack_trace *trace);
> > @@ -32,6 +34,7 @@ extern void save_stack_trace_user(struct stack_trace *trace);
> > # define save_stack_trace_tsk(tsk, trace) do { } while (0)
> > # define save_stack_trace_user(trace) do { } while (0)
> > # define print_stack_trace(trace, spaces) do { } while (0)
> > +# define snprint_stack_trace(buf, len, trace, spaces) do { } while (0)
>
> Doing this with macros instead of C functions is pretty crappy - it
> defeats typechecking and can lead to unused-var warnings when the
> feature is disabled.
>
> Fixing this might not be practical if struct stack_trace isn't
> available, dunno.
struct stack_trace is defined only if CONFIG_STACKTRACE, and,
most call sites seems to be defined only if CONFIG_STACKTRACE.
I guess that removing all of them would works fine, but, dunno. :)
>
> > --- a/kernel/stacktrace.c
> > +++ b/kernel/stacktrace.c
> > @@ -25,6 +25,30 @@ void print_stack_trace(struct stack_trace *trace, int spaces)
> > }
> > EXPORT_SYMBOL_GPL(print_stack_trace);
> >
> > +int snprint_stack_trace(char *buf, int buf_len, struct stack_trace *trace,
> > + int spaces)
> > +{
> > + int i, printed;
> > + unsigned long ip;
> > + int ret = 0;
> > +
> > + if (WARN_ON(!trace->entries))
> > + return 0;
> > +
> > + for (i = 0; i < trace->nr_entries && buf_len; i++) {
> > + ip = trace->entries[i];
> > + printed = snprintf(buf, buf_len, "%*c[<%p>] %pS\n",
> > + 1 + spaces, ' ', (void *) ip, (void *) ip);
> > +
> > + buf_len -= printed;
> > + ret += printed;
> > + buf += printed;
> > + }
> > +
> > + return ret;
> > +}
>
> I'm not liking this much. The behaviour when the output buffer is too
> small is scary. snprintf() will return "the number of characters which
> would be generated for the given input", so local variable `buf_len'
> will go negative and we pass a negative int into snprintf()'s `size_t
> size'. snprintf() says "goody, lots and lots of buffer!" and your
> machine crashes.
>
> buf_len should be a size_t and snprint_stack_trace() will need to be
> changed to handle this.
Okay. I will fix overflow problem. And, current implementation doesn't
comply snprint* functions sementic that returns generated string
length rather than printed string length. I will fix it, too.
Thanks.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/