source: mainline/kernel/generic/src/debug/stacktrace.c

Last change on this file was 40eab9f, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 20 months ago

Print symbol names and line numbers in stacktraces for init tasks

Only useful in select few situations, but useful nonetheless.

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/*
2 * Copyright (c) 2009 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup kernel_generic_debug
30 * @{
31 */
32/** @file
33 */
34
35#include <stacktrace.h>
36#include <stdbool.h>
37#include <interrupt.h>
38#include <symtab.h>
39#include <stdio.h>
40
41#include <debug/line.h>
42
43#define STACK_FRAMES_MAX 20
44
45void stack_trace_ctx(stack_trace_ops_t *ops, stack_trace_context_t *ctx)
46{
47 int cnt = 0;
48
49 uintptr_t fp;
50 uintptr_t pc;
51
52 while ((cnt++ < STACK_FRAMES_MAX) &&
53 (ops->stack_trace_context_validate(ctx))) {
54
55 const char *symbol = NULL;
56 uintptr_t symbol_addr = 0;
57 const char *file_name = NULL;
58 const char *dir_name = NULL;
59 int line = 0;
60 int column = 0;
61
62 /*
63 * If this isn't the first frame, move pc back by one byte to read the
64 * position of the call instruction, not the return address.
65 */
66 pc = cnt == 1 ? ctx->pc : ctx->pc - 1;
67
68 if (ops->symbol_resolve &&
69 ops->symbol_resolve(pc, 0, &symbol, &symbol_addr, &file_name, &dir_name, &line, &column)) {
70
71 if (symbol == NULL)
72 symbol = "<unknown>";
73
74 if (file_name == NULL && line == 0) {
75 printf("%p: %24s()+%zu\n", (void *) ctx->fp, symbol, ctx->pc - symbol_addr);
76 } else {
77 if (file_name == NULL)
78 file_name = "<unknown>";
79 if (dir_name == NULL)
80 dir_name = "<unknown>";
81
82 printf("%p: %20s()+%zu\t %s/%s:%d:%d\n",
83 (void *) ctx->fp, symbol, ctx->pc - symbol_addr,
84 dir_name, file_name, line, column);
85 }
86 } else
87 printf("%p: %p()\n", (void *) ctx->fp, (void *) ctx->pc);
88
89 if (!ops->return_address_get(ctx, &pc))
90 break;
91
92 if (!ops->frame_pointer_prev(ctx, &fp))
93 break;
94
95 ctx->fp = fp;
96 ctx->pc = pc;
97 }
98}
99
100void stack_trace(void)
101{
102 stack_trace_context_t ctx = {
103 .fp = frame_pointer_get(),
104 .pc = program_counter_get(),
105 .istate = NULL
106 };
107
108 stack_trace_ctx(&kst_ops, &ctx);
109
110 /*
111 * Prevent the tail call optimization of the previous call by
112 * making it a non-tail call.
113 */
114 (void) frame_pointer_get();
115}
116
117void stack_trace_istate(istate_t *istate)
118{
119 stack_trace_context_t ctx = {
120 .fp = istate_get_fp(istate),
121 .pc = istate_get_pc(istate),
122 .istate = istate
123 };
124
125 if (istate_from_uspace(istate))
126 stack_trace_ctx(&ust_ops, &ctx);
127 else
128 stack_trace_ctx(&kst_ops, &ctx);
129}
130
131static bool
132resolve_kernel_address(uintptr_t addr, int op_index,
133 const char **symbol, uintptr_t *symbol_addr,
134 const char **filename, const char **dirname,
135 int *line, int *column)
136{
137 *symbol_addr = 0;
138 *symbol = symtab_name_lookup(addr, symbol_addr, &kernel_sections);
139
140 return debug_line_get_address_info(&kernel_sections, addr, op_index, filename, dirname, line, column) || *symbol_addr != 0;
141}
142
143static bool
144resolve_uspace_address(uintptr_t addr, int op_index,
145 const char **symbol, uintptr_t *symbol_addr,
146 const char **filename, const char **dirname,
147 int *line, int *column)
148{
149 if (TASK->debug_sections == NULL)
150 return false;
151
152 debug_sections_t *scs = TASK->debug_sections;
153
154 *symbol_addr = 0;
155 *symbol = symtab_name_lookup(addr, symbol_addr, scs);
156
157 return debug_line_get_address_info(scs, addr, op_index, filename, dirname, line, column) || *symbol_addr != 0;
158}
159
160stack_trace_ops_t kst_ops = {
161 .stack_trace_context_validate = kernel_stack_trace_context_validate,
162 .frame_pointer_prev = kernel_frame_pointer_prev,
163 .return_address_get = kernel_return_address_get,
164 .symbol_resolve = resolve_kernel_address,
165};
166
167stack_trace_ops_t ust_ops = {
168 .stack_trace_context_validate = uspace_stack_trace_context_validate,
169 .frame_pointer_prev = uspace_frame_pointer_prev,
170 .return_address_get = uspace_return_address_get,
171 .symbol_resolve = resolve_uspace_address,
172};
173
174/** @}
175 */
Note: See TracBrowser for help on using the repository browser.