xref: /linux/kernel/stacktrace.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
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>
9 #include <linux/kernel.h>
10 #include <linux/export.h>
11 #include <linux/kallsyms.h>
12 #include <linux/stacktrace.h>
13 
14 void print_stack_trace(struct stack_trace *trace, int spaces)
15 {
16 	int i;
17 
18 	if (WARN_ON(!trace->entries))
19 		return;
20 
21 	for (i = 0; i < trace->nr_entries; i++) {
22 		printk("%*c", 1 + spaces, ' ');
23 		print_ip_sym(trace->entries[i]);
24 	}
25 }
26 EXPORT_SYMBOL_GPL(print_stack_trace);
27 
28 int 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 }
58 EXPORT_SYMBOL_GPL(snprint_stack_trace);
59 
60 /*
61  * 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):
64  */
65 __weak void
66 save_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 }
70 
71 __weak void
72 save_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 }
76