source: mainline/kernel/generic/src/debug/stacktrace.c@ 2fbb42f

topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2fbb42f was 2fbb42f, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 20 months ago

Add printing of file names and line numbers in stacktraces

Uses DWARF ¾/5 .debug_line section (originally intended
to only support DWARF 5, but GCC emits lower versioned sections
regardless of command line arguments on some platforms for some
reason, so generic support it is).

Also adds a whole lot of definitions for various DWARF constants
I expect to be useful in the future.

The quickest way to test this probably is to run test fault1
in kernel console.

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[f6069801]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
[174156fd]29/** @addtogroup kernel_generic_debug
[f6069801]30 * @{
31 */
32/** @file
33 */
34
35#include <stacktrace.h>
[525c5ac]36#include <stdbool.h>
[f6069801]37#include <interrupt.h>
38#include <symtab.h>
[bab75df6]39#include <stdio.h>
[f6069801]40
[2fbb42f]41#include <debug/line.h>
42
[e2a0d76]43#define STACK_FRAMES_MAX 20
[f6069801]44
[7e752b2]45void stack_trace_ctx(stack_trace_ops_t *ops, stack_trace_context_t *ctx)
[f6069801]46{
47 int cnt = 0;
[2fbb42f]48
[257ceb1]49 uintptr_t fp;
50 uintptr_t pc;
[a35b458]51
[e2a0d76]52 while ((cnt++ < STACK_FRAMES_MAX) &&
53 (ops->stack_trace_context_validate(ctx))) {
[2fbb42f]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
[f6069801]62 if (ops->symbol_resolve &&
[2fbb42f]63 ops->symbol_resolve(ctx->pc, 0, &symbol, &symbol_addr, &file_name, &dir_name, &line, &column)) {
64
65 if (symbol == NULL)
66 symbol = "<unknown>";
67
68 if (file_name == NULL && line == 0) {
69 printf("%p: %24s()+%zu\n", (void *) ctx->fp, symbol, ctx->pc - symbol_addr);
70 } else {
71 if (file_name == NULL)
72 file_name = "<unknown>";
73 if (dir_name == NULL)
74 dir_name = "<unknown>";
75
76 printf("%p: %20s()+%zu\t %s/%s:%d:%d\n",
77 (void *) ctx->fp, symbol, ctx->pc - symbol_addr,
78 dir_name, file_name, line, column);
79 }
[7e752b2]80 } else
81 printf("%p: %p()\n", (void *) ctx->fp, (void *) ctx->pc);
[a35b458]82
[257ceb1]83 if (!ops->return_address_get(ctx, &pc))
[f6069801]84 break;
[a35b458]85
[257ceb1]86 if (!ops->frame_pointer_prev(ctx, &fp))
[f6069801]87 break;
[a35b458]88
[257ceb1]89 ctx->fp = fp;
90 ctx->pc = pc;
[f6069801]91 }
92}
93
94void stack_trace(void)
95{
[257ceb1]96 stack_trace_context_t ctx = {
97 .fp = frame_pointer_get(),
98 .pc = program_counter_get(),
99 .istate = NULL
100 };
101
102 stack_trace_ctx(&kst_ops, &ctx);
[f6069801]103
104 /*
105 * Prevent the tail call optimization of the previous call by
106 * making it a non-tail call.
107 */
108 (void) frame_pointer_get();
109}
110
111void stack_trace_istate(istate_t *istate)
112{
[257ceb1]113 stack_trace_context_t ctx = {
114 .fp = istate_get_fp(istate),
115 .pc = istate_get_pc(istate),
116 .istate = istate
117 };
[a35b458]118
[f6069801]119 if (istate_from_uspace(istate))
[257ceb1]120 stack_trace_ctx(&ust_ops, &ctx);
[f6069801]121 else
[257ceb1]122 stack_trace_ctx(&kst_ops, &ctx);
[f6069801]123}
124
[257ceb1]125static bool
[2fbb42f]126resolve_kernel_address(uintptr_t addr, int op_index,
127 const char **symbol, uintptr_t *symbol_addr,
128 const char **filename, const char **dirname,
129 int *line, int *column)
[f6069801]130{
[2fbb42f]131 *symbol_addr = 0;
132 *symbol = symtab_name_lookup(addr, symbol_addr);
133
134 return debug_line_get_address_info(addr, op_index, filename, dirname, line, column) || *symbol_addr != 0;
[f6069801]135}
136
137stack_trace_ops_t kst_ops = {
[257ceb1]138 .stack_trace_context_validate = kernel_stack_trace_context_validate,
[f6069801]139 .frame_pointer_prev = kernel_frame_pointer_prev,
140 .return_address_get = kernel_return_address_get,
[2fbb42f]141 .symbol_resolve = resolve_kernel_address,
[f6069801]142};
143
144stack_trace_ops_t ust_ops = {
[257ceb1]145 .stack_trace_context_validate = uspace_stack_trace_context_validate,
[f6069801]146 .frame_pointer_prev = uspace_frame_pointer_prev,
147 .return_address_get = uspace_return_address_get,
148 .symbol_resolve = NULL
149};
150
151/** @}
152 */
Note: See TracBrowser for help on using the repository browser.