summaryrefslogtreecommitdiffstats
path: root/runtime/stack.c
blob: 758013cb16458d63ef86549e5c13a363c954ac10 (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
#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;

#ifdef __x86_64__
static void __stp_stack_print (unsigned long *stack, int verbose, int levels)
{
	unsigned long addr;

	if (verbose)
		_stp_printf ("trace for %d (%s)\n", current->pid, current->comm);

	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);
		}
	}
	_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);
			else
				_stp_sprintf (str, "0x%lx ", 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 ("\n");
		ebp = *(unsigned long *)ebp;
	}
#else
	while (valid_stack_ptr(tinfo, stack)) {
		addr = *stack++;
		if (_stp_kta (addr)) {
			_stp_symbol_sprint (addr);
			_stp_string_cat ("\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;
	}
}

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 trace buffer.
 * @param verbose Verbosity:
 */

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

String _stp_stack_sprint (String str, int verbose, int levels)
{
  unsigned long stack;
  __stp_stack_sprint (str, &stack, verbose, levels);
  _stp_log ("sss: str=%s\n", str->buf);
  return str;
}

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