summaryrefslogtreecommitdiffstats
path: root/runtime/stack.c
blob: f06475dc769960695ee0523840d3f684d9d7c7a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#ifndef _STACK_C_ /* -*- linux-c -*- */
#define _STACK_C_


/** @file stack.c
 * @brief Stack Tracing Functions
 */

/** @addtogroup stack Stack Tracing Functions
 * @{
 */

#include "sym.c"

static int (*_stp_kta)(unsigned long addr)=(void *)KTA;


struct frame_head {
	struct frame_head * ebp;
	unsigned long ret;
} __attribute__((packed));

static struct frame_head *
dump_backtrace(struct frame_head * head)
{
	_stp_printf ("db: %lx\n", head->ret);

	/* frame pointers should strictly progress back up the stack
	 * (towards higher addresses) */
	if (head >= head->ebp)
		return NULL;

	return head->ebp;
}

static int pages_present(struct frame_head * head)
{
	struct mm_struct * mm = current->mm;

	/* FIXME: only necessary once per page */
	if (!check_user_page_readable(mm, (unsigned long)head))
		return 0;

	return check_user_page_readable(mm, (unsigned long)(head + 1));
}

static int valid_kernel_stack(struct frame_head * head, struct pt_regs * regs)
{
	unsigned long headaddr = (unsigned long)head;
	unsigned long stack = (unsigned long)regs;
	unsigned long stack_base = (stack & ~(THREAD_SIZE - 1)) + THREAD_SIZE;
	_stp_log ("%lx %lx %lx\n", headaddr, stack, stack_base);
	return headaddr < stack_base;
}

void
x86_backtrace(struct pt_regs * const regs, unsigned int depth)
{
	struct frame_head *head;

#ifdef CONFIG_X86_64
	head = (struct frame_head *)regs->rbp;
#else
	head = (struct frame_head *)regs->ebp;
#endif

	if (!user_mode(regs)) {
		_stp_log ("kernel mode\n");
		while (depth-- && valid_kernel_stack(head, regs))
			head = dump_backtrace(head);
		_stp_print_flush();
		return;
	}

#ifdef CONFIG_SMP
	if (!spin_trylock(&current->mm->page_table_lock))
		return;
#endif

	while (depth-- && head && pages_present(head))
		head = dump_backtrace(head);

#ifdef CONFIG_SMP
	spin_unlock(&current->mm->page_table_lock);
#endif
	_stp_print_flush();
}


#ifdef __x86_64__
static void __stp_stack_print (unsigned long *stack, int verbose, int levels)
{
	unsigned long addr;
	while (((long) stack & (THREAD_SIZE-1)) != 0) {
		addr = *stack;
		if (_stp_kta(addr)) {
			if (verbose) {
				_stp_symbol_print (addr);
				_stp_print ("\n");
			} else
				_stp_printf ("0x%lx ", addr);
		}
		stack++;
	}
	_stp_print_flush();
}


static void __stp_stack_sprint (String str, unsigned long *stack, int verbose, int levels)
{
	unsigned long addr;
	while (((long) stack & (THREAD_SIZE-1)) != 0) {
		addr = *stack++;
		if (_stp_kta(addr)) {
			if (verbose) {
				_stp_symbol_sprint (str, addr);
				_stp_sprintf (str, "\n");
			} else
				_stp_sprintf (str, "0x%lx\n", addr);
		}
	}
}

#else  /* i386 */

static inline int valid_stack_ptr (struct thread_info *tinfo, void *p)
{
	return	p > (void *)tinfo &&
		p < (void *)tinfo + THREAD_SIZE - 3;
}

static inline unsigned long _stp_print_context_stack (
	struct thread_info *tinfo,
	unsigned long *stack, 
	unsigned long ebp )
{
	unsigned long addr;

#ifdef	CONFIG_FRAME_POINTER
	while (valid_stack_ptr(tinfo, (void *)ebp)) {
		addr = *(unsigned long *)(ebp + 4);
		_stp_symbol_print (addr);
		_stp_print_cstr("\n");
		ebp = *(unsigned long *)ebp;
	}
#else
	while (valid_stack_ptr(tinfo, stack)) {
		addr = *stack++;
		if (_stp_kta (addr)) {
			_stp_symbol_print (addr);
			_stp_print_cstr ("\n");
		}
	}
#endif
	_stp_print_flush();
	return ebp;
}

static inline unsigned long _stp_sprint_context_stack (
	String str,
	struct thread_info *tinfo,
	unsigned long *stack, 
	unsigned long ebp )
{
	unsigned long addr;

#ifdef	CONFIG_FRAME_POINTER
	while (valid_stack_ptr(tinfo, (void *)ebp)) {
		addr = *(unsigned long *)(ebp + 4);
		_stp_symbol_sprint (str, addr);
		_stp_string_cat (str, "\n");
		ebp = *(unsigned long *)ebp;
	}
#else
	while (valid_stack_ptr(tinfo, stack)) {
		addr = *stack++;
		if (_stp_kta (addr)) {
			_stp_symbol_sprint (str, addr);
			_stp_string_cat (str, "\n");
		}
	}
#endif
	return ebp;
}

static void __stp_stack_print (unsigned long *stack, int verbose, int levels)
{
	unsigned long ebp;

	/* Grab ebp right from our regs */
	asm ("movl %%ebp, %0" : "=r" (ebp) : );

	while (stack) {
		struct thread_info *context = (struct thread_info *)
			((unsigned long)stack & (~(THREAD_SIZE - 1)));
		ebp = _stp_print_context_stack (context, stack, ebp);
		stack = (unsigned long*)context->previous_esp;
	}
	_stp_print_flush ();
}

static void __stp_stack_sprint (String str, unsigned long *stack, int verbose, int levels)
{
	unsigned long ebp;

	/* Grab ebp right from our regs */
	asm ("movl %%ebp, %0" : "=r" (ebp) : );

	while (stack) {
		struct thread_info *context = (struct thread_info *)
			((unsigned long)stack & (~(THREAD_SIZE - 1)));
		ebp = _stp_sprint_context_stack (str, context, stack, ebp);
		stack = (unsigned long*)context->previous_esp;
	}
}

#endif /* i386 */

/** Print stack dump.
 * Prints a stack dump to the print buffer.
 * @param verbose Verbosity
 * @param levels Number of levels to trace.
 * @todo Implement verbosity and levels parameters.
 * @bug levels parameter is not functional
 */

void _stp_stack_jprint (int verbose, int levels)
{
  unsigned long stack;
  __stp_stack_print (&stack, verbose, levels);
}

void _stp_stack_print (struct pt_regs *regs, int verbose, int levels)
{
	if (verbose) {
		_stp_printf ("trace for %d (%s)\n", current->pid, current->comm);
		_stp_symbol_print (regs->rip);
		_stp_print ("\n");
	} else
		_stp_printf ("0x%lx ", regs->rip);

	__stp_stack_print ((unsigned long *)regs->rsp, verbose, levels);
}

/** Writes stack dump to a String
 *
 * @param str String
 * @param verbose Verbosity
 * @param levels Number of levels to trace.
 * @returns Same String as was input.
 * @todo Implement verbosity and levels parameters.
 * @bug levels parameter is not functional
 */

String _stp_stack_sprint (String str, int verbose, int levels)
{
  unsigned long stack;
  __stp_stack_sprint (str, &stack, verbose, levels);
  return str;
}

/** @} */
#endif /* _STACK_C_ */