Skip to content

Commit

Permalink
stacktrace: introduce snprint_stack_trace for buffer output
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Joonsoo Kim <[email protected]>
Cc: Mel Gorman <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Minchan Kim <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: Michal Nazarewicz <[email protected]>
Cc: Jungsoo Son <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Joonsoo Kim <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
JoonsooKim authored and torvalds committed Dec 13, 2014
1 parent dbc8358 commit 9a92a6c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/linux/stacktrace.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef __LINUX_STACKTRACE_H
#define __LINUX_STACKTRACE_H

#include <linux/types.h>

struct task_struct;
struct pt_regs;

Expand All @@ -20,6 +22,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, size_t size,
struct stack_trace *trace, int spaces);

#ifdef CONFIG_USER_STACKTRACE_SUPPORT
extern void save_stack_trace_user(struct stack_trace *trace);
Expand All @@ -32,6 +36,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, size, trace, spaces) do { } while (0)
#endif

#endif
32 changes: 32 additions & 0 deletions kernel/stacktrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,38 @@ void print_stack_trace(struct stack_trace *trace, int spaces)
}
EXPORT_SYMBOL_GPL(print_stack_trace);

int snprint_stack_trace(char *buf, size_t size,
struct stack_trace *trace, int spaces)
{
int i;
unsigned long ip;
int generated;
int total = 0;

if (WARN_ON(!trace->entries))
return 0;

for (i = 0; i < trace->nr_entries; i++) {
ip = trace->entries[i];
generated = snprintf(buf, size, "%*c[<%p>] %pS\n",
1 + spaces, ' ', (void *) ip, (void *) ip);

total += generated;

/* Assume that generated isn't a negative number */
if (generated >= size) {
buf += size;
size = 0;
} else {
buf += generated;
size -= generated;
}
}

return total;
}
EXPORT_SYMBOL_GPL(snprint_stack_trace);

/*
* Architectures that do not implement save_stack_trace_tsk or
* save_stack_trace_regs get this weak alias and a once-per-bootup warning
Expand Down

0 comments on commit 9a92a6c

Please sign in to comment.