[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] | 45 | void 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 |
|
---|
[40eab9f] | 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 |
|
---|
[f6069801] | 68 | if (ops->symbol_resolve &&
|
---|
[40eab9f] | 69 | ops->symbol_resolve(pc, 0, &symbol, &symbol_addr, &file_name, &dir_name, &line, &column)) {
|
---|
[2fbb42f] | 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 | }
|
---|
[7e752b2] | 86 | } else
|
---|
| 87 | printf("%p: %p()\n", (void *) ctx->fp, (void *) ctx->pc);
|
---|
[a35b458] | 88 |
|
---|
[257ceb1] | 89 | if (!ops->return_address_get(ctx, &pc))
|
---|
[f6069801] | 90 | break;
|
---|
[a35b458] | 91 |
|
---|
[257ceb1] | 92 | if (!ops->frame_pointer_prev(ctx, &fp))
|
---|
[f6069801] | 93 | break;
|
---|
[a35b458] | 94 |
|
---|
[257ceb1] | 95 | ctx->fp = fp;
|
---|
| 96 | ctx->pc = pc;
|
---|
[f6069801] | 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | void stack_trace(void)
|
---|
| 101 | {
|
---|
[257ceb1] | 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);
|
---|
[f6069801] | 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 |
|
---|
| 117 | void stack_trace_istate(istate_t *istate)
|
---|
| 118 | {
|
---|
[257ceb1] | 119 | stack_trace_context_t ctx = {
|
---|
| 120 | .fp = istate_get_fp(istate),
|
---|
| 121 | .pc = istate_get_pc(istate),
|
---|
| 122 | .istate = istate
|
---|
| 123 | };
|
---|
[a35b458] | 124 |
|
---|
[f6069801] | 125 | if (istate_from_uspace(istate))
|
---|
[257ceb1] | 126 | stack_trace_ctx(&ust_ops, &ctx);
|
---|
[f6069801] | 127 | else
|
---|
[257ceb1] | 128 | stack_trace_ctx(&kst_ops, &ctx);
|
---|
[f6069801] | 129 | }
|
---|
| 130 |
|
---|
[257ceb1] | 131 | static bool
|
---|
[2fbb42f] | 132 | resolve_kernel_address(uintptr_t addr, int op_index,
|
---|
[001957b6] | 133 | const char **symbol, uintptr_t *symbol_addr,
|
---|
| 134 | const char **filename, const char **dirname,
|
---|
| 135 | int *line, int *column)
|
---|
[f6069801] | 136 | {
|
---|
[2fbb42f] | 137 | *symbol_addr = 0;
|
---|
[40eab9f] | 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 |
|
---|
| 143 | static bool
|
---|
| 144 | resolve_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);
|
---|
[2fbb42f] | 156 |
|
---|
[40eab9f] | 157 | return debug_line_get_address_info(scs, addr, op_index, filename, dirname, line, column) || *symbol_addr != 0;
|
---|
[f6069801] | 158 | }
|
---|
| 159 |
|
---|
| 160 | stack_trace_ops_t kst_ops = {
|
---|
[257ceb1] | 161 | .stack_trace_context_validate = kernel_stack_trace_context_validate,
|
---|
[f6069801] | 162 | .frame_pointer_prev = kernel_frame_pointer_prev,
|
---|
| 163 | .return_address_get = kernel_return_address_get,
|
---|
[2fbb42f] | 164 | .symbol_resolve = resolve_kernel_address,
|
---|
[f6069801] | 165 | };
|
---|
| 166 |
|
---|
| 167 | stack_trace_ops_t ust_ops = {
|
---|
[257ceb1] | 168 | .stack_trace_context_validate = uspace_stack_trace_context_validate,
|
---|
[f6069801] | 169 | .frame_pointer_prev = uspace_frame_pointer_prev,
|
---|
| 170 | .return_address_get = uspace_return_address_get,
|
---|
[40eab9f] | 171 | .symbol_resolve = resolve_uspace_address,
|
---|
[f6069801] | 172 | };
|
---|
| 173 |
|
---|
| 174 | /** @}
|
---|
| 175 | */
|
---|