blob: b6e4c16377c708027c1c8c52914106fb71e3ca93 [file] [log] [blame]
Ingo Molnar8637c092006-07-03 00:24:38 -07001/*
2 * kernel/stacktrace.c
3 *
4 * Stack trace management functions
5 *
6 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
7 */
8#include <linux/sched.h>
Ingo Molnar9212ddb2008-12-25 11:21:20 +01009#include <linux/kernel.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040010#include <linux/export.h>
Ingo Molnar8637c092006-07-03 00:24:38 -070011#include <linux/kallsyms.h>
12#include <linux/stacktrace.h>
13
14void print_stack_trace(struct stack_trace *trace, int spaces)
15{
Vegard Nossuma5a242d2008-06-13 11:00:14 +020016 int i;
Ingo Molnar8637c092006-07-03 00:24:38 -070017
Johannes Bergbfeeeeb2008-05-12 21:21:14 +020018 if (WARN_ON(!trace->entries))
19 return;
20
Ingo Molnar8637c092006-07-03 00:24:38 -070021 for (i = 0; i < trace->nr_entries; i++) {
Vegard Nossuma5a242d2008-06-13 11:00:14 +020022 printk("%*c", 1 + spaces, ' ');
23 print_ip_sym(trace->entries[i]);
Ingo Molnar8637c092006-07-03 00:24:38 -070024 }
25}
Ingo Molnar85946982008-06-27 21:20:17 +020026EXPORT_SYMBOL_GPL(print_stack_trace);
Ingo Molnar8637c092006-07-03 00:24:38 -070027
Joonsoo Kim9a92a6c2014-12-12 16:55:58 -080028int snprint_stack_trace(char *buf, size_t size,
29 struct stack_trace *trace, int spaces)
30{
31 int i;
32 unsigned long ip;
33 int generated;
34 int total = 0;
35
36 if (WARN_ON(!trace->entries))
37 return 0;
38
39 for (i = 0; i < trace->nr_entries; i++) {
40 ip = trace->entries[i];
41 generated = snprintf(buf, size, "%*c[<%p>] %pS\n",
42 1 + spaces, ' ', (void *) ip, (void *) ip);
43
44 total += generated;
45
46 /* Assume that generated isn't a negative number */
47 if (generated >= size) {
48 buf += size;
49 size = 0;
50 } else {
51 buf += generated;
52 size -= generated;
53 }
54 }
55
56 return total;
57}
58EXPORT_SYMBOL_GPL(snprint_stack_trace);
59
Ingo Molnar9212ddb2008-12-25 11:21:20 +010060/*
Masami Hiramatsuc624d332011-06-08 16:09:27 +090061 * Architectures that do not implement save_stack_trace_tsk or
62 * save_stack_trace_regs get this weak alias and a once-per-bootup warning
63 * (whenever this facility is utilized - for example by procfs):
Ingo Molnar9212ddb2008-12-25 11:21:20 +010064 */
65__weak void
66save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
67{
68 WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
69}
Masami Hiramatsuc624d332011-06-08 16:09:27 +090070
71__weak void
72save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
73{
74 WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
75}