2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-11-29 13:55:07 +00:00
|
|
|
#include <AK/Demangle.h>
|
2019-05-18 15:31:52 +00:00
|
|
|
#include <AK/TemporaryChange.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
2019-11-06 12:42:38 +00:00
|
|
|
#include <Kernel/KSyms.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/Scheduler.h>
|
2020-04-11 18:24:07 +00:00
|
|
|
#include <LibELF/Loader.h>
|
2018-12-24 21:59:10 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-04-08 11:30:50 +00:00
|
|
|
FlatPtr g_lowest_kernel_symbol_address = 0xffffffff;
|
|
|
|
FlatPtr g_highest_kernel_symbol_address = 0;
|
|
|
|
bool g_kernel_symbols_available = false;
|
|
|
|
|
|
|
|
static KernelSymbol* s_symbols;
|
|
|
|
static size_t s_symbol_count = 0;
|
2018-12-24 21:59:10 +00:00
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
static u8 parse_hex_digit(char nibble)
|
2018-12-24 21:59:10 +00:00
|
|
|
{
|
|
|
|
if (nibble >= '0' && nibble <= '9')
|
|
|
|
return nibble - '0';
|
|
|
|
ASSERT(nibble >= 'a' && nibble <= 'f');
|
|
|
|
return 10 + (nibble - 'a');
|
|
|
|
}
|
|
|
|
|
2020-01-16 21:04:44 +00:00
|
|
|
u32 address_for_kernel_symbol(const StringView& name)
|
2019-11-28 20:30:20 +00:00
|
|
|
{
|
2020-04-08 11:30:50 +00:00
|
|
|
for (size_t i = 0; i < s_symbol_count; ++i) {
|
|
|
|
if (!strncmp(name.characters_without_null_termination(), s_symbols[i].name, name.length()))
|
|
|
|
return s_symbols[i].address;
|
2019-11-28 20:30:20 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:30:50 +00:00
|
|
|
const KernelSymbol* symbolicate_kernel_address(u32 address)
|
2018-12-24 21:59:10 +00:00
|
|
|
{
|
2020-04-08 11:30:50 +00:00
|
|
|
if (address < g_lowest_kernel_symbol_address || address > g_highest_kernel_symbol_address)
|
2018-12-24 21:59:10 +00:00
|
|
|
return nullptr;
|
2020-04-08 11:30:50 +00:00
|
|
|
for (unsigned i = 0; i < s_symbol_count; ++i) {
|
|
|
|
if (address < s_symbols[i + 1].address)
|
|
|
|
return &s_symbols[i];
|
2018-12-24 21:59:10 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:30:50 +00:00
|
|
|
static void load_kernel_sybols_from_data(const ByteBuffer& buffer)
|
2018-12-24 21:59:10 +00:00
|
|
|
{
|
2020-04-08 11:30:50 +00:00
|
|
|
g_lowest_kernel_symbol_address = 0xffffffff;
|
|
|
|
g_highest_kernel_symbol_address = 0;
|
|
|
|
|
2019-09-30 06:57:01 +00:00
|
|
|
auto* bufptr = (const char*)buffer.data();
|
2018-12-24 21:59:10 +00:00
|
|
|
auto* start_of_name = bufptr;
|
2020-04-08 11:30:50 +00:00
|
|
|
FlatPtr address = 0;
|
2018-12-24 21:59:10 +00:00
|
|
|
|
2020-04-08 11:30:50 +00:00
|
|
|
for (size_t i = 0; i < 8; ++i)
|
|
|
|
s_symbol_count = (s_symbol_count << 4) | parse_hex_digit(*(bufptr++));
|
|
|
|
s_symbols = static_cast<KernelSymbol*>(kmalloc_eternal(sizeof(KernelSymbol) * s_symbol_count));
|
2018-12-24 21:59:10 +00:00
|
|
|
++bufptr; // skip newline
|
|
|
|
|
2020-04-08 11:30:50 +00:00
|
|
|
klog() << "Loading kernel symbol table...";
|
2018-12-24 21:59:10 +00:00
|
|
|
|
2020-04-08 11:30:50 +00:00
|
|
|
size_t current_symbol_index = 0;
|
2018-12-24 21:59:10 +00:00
|
|
|
|
|
|
|
while (bufptr < buffer.end_pointer()) {
|
2020-04-08 11:30:50 +00:00
|
|
|
for (size_t i = 0; i < 8; ++i)
|
2018-12-24 21:59:10 +00:00
|
|
|
address = (address << 4) | parse_hex_digit(*(bufptr++));
|
|
|
|
bufptr += 3;
|
|
|
|
start_of_name = bufptr;
|
|
|
|
while (*(++bufptr)) {
|
|
|
|
if (*bufptr == '\n') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 11:30:50 +00:00
|
|
|
auto& ksym = s_symbols[current_symbol_index];
|
2018-12-24 21:59:10 +00:00
|
|
|
ksym.address = address;
|
|
|
|
char* name = static_cast<char*>(kmalloc_eternal((bufptr - start_of_name) + 1));
|
|
|
|
memcpy(name, start_of_name, bufptr - start_of_name);
|
|
|
|
name[bufptr - start_of_name] = '\0';
|
|
|
|
ksym.name = name;
|
|
|
|
|
2020-04-08 11:30:50 +00:00
|
|
|
if (ksym.address < g_lowest_kernel_symbol_address)
|
|
|
|
g_lowest_kernel_symbol_address = ksym.address;
|
|
|
|
if (ksym.address > g_highest_kernel_symbol_address)
|
|
|
|
g_highest_kernel_symbol_address = ksym.address;
|
2018-12-24 21:59:10 +00:00
|
|
|
|
|
|
|
++bufptr;
|
2020-04-08 11:30:50 +00:00
|
|
|
++current_symbol_index;
|
2018-12-24 21:59:10 +00:00
|
|
|
}
|
2020-04-08 11:30:50 +00:00
|
|
|
g_kernel_symbols_available = true;
|
2018-12-24 21:59:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 09:43:25 +00:00
|
|
|
NEVER_INLINE void dump_backtrace_impl(u32 ebp, bool use_ksyms)
|
2018-12-24 21:59:10 +00:00
|
|
|
{
|
2020-01-05 17:00:15 +00:00
|
|
|
SmapDisabler disabler;
|
|
|
|
#if 0
|
2018-12-24 21:59:10 +00:00
|
|
|
if (!current) {
|
2019-03-23 21:03:17 +00:00
|
|
|
//hang();
|
2018-12-24 21:59:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-01-05 17:00:15 +00:00
|
|
|
#endif
|
2020-04-08 11:30:50 +00:00
|
|
|
if (use_ksyms && !g_kernel_symbols_available) {
|
2019-02-15 11:30:48 +00:00
|
|
|
hang();
|
2018-12-24 21:59:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-03-02 09:40:40 +00:00
|
|
|
|
|
|
|
OwnPtr<Process::ELFBundle> elf_bundle;
|
|
|
|
if (Process::current)
|
|
|
|
elf_bundle = Process::current->elf_bundle();
|
|
|
|
|
2018-12-24 21:59:10 +00:00
|
|
|
struct RecognizedSymbol {
|
2020-04-08 11:30:50 +00:00
|
|
|
FlatPtr address;
|
|
|
|
const KernelSymbol* symbol { nullptr };
|
2018-12-24 21:59:10 +00:00
|
|
|
};
|
2020-04-08 11:30:50 +00:00
|
|
|
size_t max_recognized_symbol_count = 256;
|
2019-04-15 21:50:25 +00:00
|
|
|
RecognizedSymbol recognized_symbols[max_recognized_symbol_count];
|
2020-04-08 11:30:50 +00:00
|
|
|
size_t recognized_symbol_count = 0;
|
2018-12-24 21:59:10 +00:00
|
|
|
if (use_ksyms) {
|
2020-04-08 11:30:50 +00:00
|
|
|
for (FlatPtr* stack_ptr = (FlatPtr*)ebp;
|
2020-02-17 14:04:27 +00:00
|
|
|
(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) {
|
2020-04-08 11:30:50 +00:00
|
|
|
FlatPtr retaddr = stack_ptr[1];
|
|
|
|
recognized_symbols[recognized_symbol_count++] = { retaddr, symbolicate_kernel_address(retaddr) };
|
2018-12-24 21:59:10 +00:00
|
|
|
}
|
2019-04-15 21:50:25 +00:00
|
|
|
} else {
|
2020-04-08 11:30:50 +00:00
|
|
|
for (FlatPtr* stack_ptr = (FlatPtr*)ebp;
|
2020-02-17 14:04:27 +00:00
|
|
|
(Process::current ? Process::current->validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2) : 1); stack_ptr = (u32*)*stack_ptr) {
|
2020-04-08 11:30:50 +00:00
|
|
|
FlatPtr retaddr = stack_ptr[1];
|
2020-02-24 15:12:10 +00:00
|
|
|
dbg() << String::format("%x", retaddr) << " (next: " << String::format("%x", (stack_ptr ? (u32*)*stack_ptr : 0)) << ")";
|
2018-12-24 21:59:10 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-07-28 18:02:22 +00:00
|
|
|
ASSERT(recognized_symbol_count <= max_recognized_symbol_count);
|
2020-04-08 11:30:50 +00:00
|
|
|
for (size_t i = 0; i < recognized_symbol_count; ++i) {
|
2019-04-15 21:50:25 +00:00
|
|
|
auto& symbol = recognized_symbols[i];
|
2019-04-30 12:47:22 +00:00
|
|
|
if (!symbol.address)
|
|
|
|
break;
|
2020-04-08 11:30:50 +00:00
|
|
|
if (!symbol.symbol) {
|
2020-03-02 09:40:40 +00:00
|
|
|
if (elf_bundle && elf_bundle->elf_loader->has_symbols()) {
|
|
|
|
dbg() << String::format("%p", symbol.address) << " " << elf_bundle->elf_loader->symbolicate(symbol.address);
|
2019-05-16 15:18:25 +00:00
|
|
|
} else {
|
2020-02-24 15:12:10 +00:00
|
|
|
dbg() << String::format("%p", symbol.address) << " (no ELF symbols for process)";
|
2019-05-16 15:18:25 +00:00
|
|
|
}
|
2019-04-30 12:47:22 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-08 11:30:50 +00:00
|
|
|
size_t offset = symbol.address - symbol.symbol->address;
|
|
|
|
if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096)
|
2020-02-24 15:12:10 +00:00
|
|
|
dbg() << String::format("%p", symbol.address);
|
2019-04-30 12:47:22 +00:00
|
|
|
else
|
2020-04-08 11:30:50 +00:00
|
|
|
dbg() << String::format("%p", symbol.address) << " " << demangle(symbol.symbol->name) << " +" << offset;
|
2018-12-24 21:59:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 11:41:16 +00:00
|
|
|
void dump_backtrace()
|
2019-02-24 13:51:48 +00:00
|
|
|
{
|
2019-05-18 15:31:52 +00:00
|
|
|
static bool in_dump_backtrace = false;
|
2019-11-08 16:36:29 +00:00
|
|
|
if (in_dump_backtrace)
|
2019-05-18 15:31:52 +00:00
|
|
|
return;
|
|
|
|
TemporaryChange change(in_dump_backtrace, true);
|
2019-08-07 18:35:41 +00:00
|
|
|
TemporaryChange disable_kmalloc_stacks(g_dump_kmalloc_stacks, false);
|
2020-04-08 11:30:50 +00:00
|
|
|
FlatPtr ebp;
|
2019-06-07 09:43:58 +00:00
|
|
|
asm volatile("movl %%ebp, %%eax"
|
|
|
|
: "=a"(ebp));
|
2020-04-08 11:30:50 +00:00
|
|
|
dump_backtrace_impl(ebp, g_kernel_symbols_available);
|
2019-02-24 13:51:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:30:50 +00:00
|
|
|
void load_kernel_symbol_table()
|
2018-12-24 21:59:10 +00:00
|
|
|
{
|
2020-01-18 22:04:48 +00:00
|
|
|
auto result = VFS::the().open("/res/kernel.map", O_RDONLY, 0, VFS::the().root_custody());
|
2019-03-06 21:14:31 +00:00
|
|
|
ASSERT(!result.is_error());
|
2019-06-13 20:03:04 +00:00
|
|
|
auto description = result.value();
|
|
|
|
auto buffer = description->read_entire_file();
|
2019-03-06 21:14:31 +00:00
|
|
|
ASSERT(buffer);
|
2020-04-08 11:30:50 +00:00
|
|
|
load_kernel_sybols_from_data(buffer);
|
2018-12-24 21:59:10 +00:00
|
|
|
}
|
2020-02-16 00:27:42 +00:00
|
|
|
|
|
|
|
}
|