Serenity Operating System
at portability 195 lines 6.9 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 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 are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/Demangle.h> 28#include <AK/TemporaryChange.h> 29#include <Kernel/FileSystem/FileDescription.h> 30#include <Kernel/KSyms.h> 31#include <Kernel/Process.h> 32#include <Kernel/Scheduler.h> 33#include <LibELF/ELFLoader.h> 34 35namespace Kernel { 36 37static KSym* s_ksyms; 38u32 ksym_lowest_address = 0xffffffff; 39u32 ksym_highest_address = 0; 40u32 ksym_count = 0; 41bool ksyms_ready = false; 42 43static u8 parse_hex_digit(char nibble) 44{ 45 if (nibble >= '0' && nibble <= '9') 46 return nibble - '0'; 47 ASSERT(nibble >= 'a' && nibble <= 'f'); 48 return 10 + (nibble - 'a'); 49} 50 51u32 address_for_kernel_symbol(const StringView& name) 52{ 53 for (unsigned i = 0; i < ksym_count; ++i) { 54 if (!strncmp(name.characters_without_null_termination(), s_ksyms[i].name, name.length())) 55 return s_ksyms[i].address; 56 } 57 return 0; 58} 59 60const KSym* ksymbolicate(u32 address) 61{ 62 if (address < ksym_lowest_address || address > ksym_highest_address) 63 return nullptr; 64 for (unsigned i = 0; i < ksym_count; ++i) { 65 if (address < s_ksyms[i + 1].address) 66 return &s_ksyms[i]; 67 } 68 return nullptr; 69} 70 71static void load_ksyms_from_data(const ByteBuffer& buffer) 72{ 73 ksym_lowest_address = 0xffffffff; 74 ksym_highest_address = 0; 75 auto* bufptr = (const char*)buffer.data(); 76 auto* start_of_name = bufptr; 77 u32 address = 0; 78 79 for (unsigned i = 0; i < 8; ++i) 80 ksym_count = (ksym_count << 4) | parse_hex_digit(*(bufptr++)); 81 s_ksyms = static_cast<KSym*>(kmalloc_eternal(sizeof(KSym) * ksym_count)); 82 ++bufptr; // skip newline 83 84 kprintf("Loading ksyms..."); 85 86 unsigned current_ksym_index = 0; 87 88 while (bufptr < buffer.end_pointer()) { 89 for (unsigned i = 0; i < 8; ++i) 90 address = (address << 4) | parse_hex_digit(*(bufptr++)); 91 bufptr += 3; 92 start_of_name = bufptr; 93 while (*(++bufptr)) { 94 if (*bufptr == '\n') { 95 break; 96 } 97 } 98 auto& ksym = s_ksyms[current_ksym_index]; 99 ksym.address = address; 100 char* name = static_cast<char*>(kmalloc_eternal((bufptr - start_of_name) + 1)); 101 memcpy(name, start_of_name, bufptr - start_of_name); 102 name[bufptr - start_of_name] = '\0'; 103 ksym.name = name; 104 105 if (ksym.address < ksym_lowest_address) 106 ksym_lowest_address = ksym.address; 107 if (ksym.address > ksym_highest_address) 108 ksym_highest_address = ksym.address; 109 110 ++bufptr; 111 ++current_ksym_index; 112 } 113 kprintf("ok\n"); 114 ksyms_ready = true; 115} 116 117[[gnu::noinline]] void dump_backtrace_impl(u32 ebp, bool use_ksyms) 118{ 119 SmapDisabler disabler; 120#if 0 121 if (!current) { 122 //hang(); 123 return; 124 } 125#endif 126 if (use_ksyms && !ksyms_ready) { 127 hang(); 128 return; 129 } 130 struct RecognizedSymbol { 131 u32 address; 132 const KSym* ksym; 133 }; 134 int max_recognized_symbol_count = 256; 135 RecognizedSymbol recognized_symbols[max_recognized_symbol_count]; 136 int recognized_symbol_count = 0; 137 if (use_ksyms) { 138 for (u32* stack_ptr = (u32*)ebp; 139 (Process::current ? Process::current->validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2) : 1) && recognized_symbol_count < max_recognized_symbol_count; stack_ptr = (u32*)*stack_ptr) { 140 u32 retaddr = stack_ptr[1]; 141 recognized_symbols[recognized_symbol_count++] = { retaddr, ksymbolicate(retaddr) }; 142 } 143 } else { 144 for (u32* stack_ptr = (u32*)ebp; 145 (Process::current ? Process::current->validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2) : 1); stack_ptr = (u32*)*stack_ptr) { 146 u32 retaddr = stack_ptr[1]; 147 dbgprintf("%x (next: %x)\n", retaddr, stack_ptr ? (u32*)*stack_ptr : 0); 148 } 149 return; 150 } 151 ASSERT(recognized_symbol_count <= max_recognized_symbol_count); 152 for (int i = 0; i < recognized_symbol_count; ++i) { 153 auto& symbol = recognized_symbols[i]; 154 if (!symbol.address) 155 break; 156 if (!symbol.ksym) { 157 if (Process::current && Process::current->elf_loader() && Process::current->elf_loader()->has_symbols()) { 158 dbgprintf("%p %s\n", symbol.address, Process::current->elf_loader()->symbolicate(symbol.address).characters()); 159 } else { 160 dbgprintf("%p (no ELF symbols for process)\n", symbol.address); 161 } 162 continue; 163 } 164 unsigned offset = symbol.address - symbol.ksym->address; 165 if (symbol.ksym->address == ksym_highest_address && offset > 4096) 166 dbgprintf("%p\n", symbol.address); 167 else 168 dbgprintf("%p %s +%u\n", symbol.address, demangle(symbol.ksym->name).characters(), offset); 169 } 170} 171 172void dump_backtrace() 173{ 174 static bool in_dump_backtrace = false; 175 if (in_dump_backtrace) 176 return; 177 TemporaryChange change(in_dump_backtrace, true); 178 TemporaryChange disable_kmalloc_stacks(g_dump_kmalloc_stacks, false); 179 u32 ebp; 180 asm volatile("movl %%ebp, %%eax" 181 : "=a"(ebp)); 182 dump_backtrace_impl(ebp, ksyms_ready); 183} 184 185void load_ksyms() 186{ 187 auto result = VFS::the().open("/res/kernel.map", O_RDONLY, 0, VFS::the().root_custody()); 188 ASSERT(!result.is_error()); 189 auto description = result.value(); 190 auto buffer = description->read_entire_file(); 191 ASSERT(buffer); 192 load_ksyms_from_data(buffer); 193} 194 195}