2020-01-18 09:38:21 +01: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 14:55:07 +01:00
|
|
|
#include <AK/Demangle.h>
|
2019-05-18 17:31:52 +02:00
|
|
|
#include <AK/TemporaryChange.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
2019-11-06 13:42:38 +01:00
|
|
|
#include <Kernel/KSyms.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/Scheduler.h>
|
|
|
|
#include <LibELF/ELFLoader.h>
|
2018-12-24 22:59:10 +01:00
|
|
|
|
|
|
|
static KSym* s_ksyms;
|
2019-11-06 13:36:37 +01:00
|
|
|
u32 ksym_lowest_address = 0xffffffff;
|
|
|
|
u32 ksym_highest_address = 0;
|
|
|
|
u32 ksym_count = 0;
|
|
|
|
bool ksyms_ready = false;
|
2018-12-24 22:59:10 +01:00
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
static u8 parse_hex_digit(char nibble)
|
2018-12-24 22:59:10 +01:00
|
|
|
{
|
|
|
|
if (nibble >= '0' && nibble <= '9')
|
|
|
|
return nibble - '0';
|
|
|
|
ASSERT(nibble >= 'a' && nibble <= 'f');
|
|
|
|
return 10 + (nibble - 'a');
|
|
|
|
}
|
|
|
|
|
2020-01-16 22:04:44 +01:00
|
|
|
u32 address_for_kernel_symbol(const StringView& name)
|
2019-11-28 21:30:20 +01:00
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < ksym_count; ++i) {
|
2020-01-16 22:04:44 +01:00
|
|
|
if (!strncmp(name.characters_without_null_termination(), s_ksyms[i].name, name.length()))
|
2019-11-28 21:30:20 +01:00
|
|
|
return s_ksyms[i].address;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
const KSym* ksymbolicate(u32 address)
|
2018-12-24 22:59:10 +01:00
|
|
|
{
|
|
|
|
if (address < ksym_lowest_address || address > ksym_highest_address)
|
|
|
|
return nullptr;
|
|
|
|
for (unsigned i = 0; i < ksym_count; ++i) {
|
|
|
|
if (address < s_ksyms[i + 1].address)
|
|
|
|
return &s_ksyms[i];
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void load_ksyms_from_data(const ByteBuffer& buffer)
|
|
|
|
{
|
2019-04-30 14:47:22 +02:00
|
|
|
ksym_lowest_address = 0xffffffff;
|
|
|
|
ksym_highest_address = 0;
|
2019-09-30 08:57:01 +02:00
|
|
|
auto* bufptr = (const char*)buffer.data();
|
2018-12-24 22:59:10 +01:00
|
|
|
auto* start_of_name = bufptr;
|
2019-07-03 21:17:35 +02:00
|
|
|
u32 address = 0;
|
2018-12-24 22:59:10 +01:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < 8; ++i)
|
|
|
|
ksym_count = (ksym_count << 4) | parse_hex_digit(*(bufptr++));
|
|
|
|
s_ksyms = static_cast<KSym*>(kmalloc_eternal(sizeof(KSym) * ksym_count));
|
|
|
|
++bufptr; // skip newline
|
|
|
|
|
2019-02-06 18:52:12 +01:00
|
|
|
kprintf("Loading ksyms...");
|
2018-12-24 22:59:10 +01:00
|
|
|
|
|
|
|
unsigned current_ksym_index = 0;
|
|
|
|
|
|
|
|
while (bufptr < buffer.end_pointer()) {
|
|
|
|
for (unsigned i = 0; i < 8; ++i)
|
|
|
|
address = (address << 4) | parse_hex_digit(*(bufptr++));
|
|
|
|
bufptr += 3;
|
|
|
|
start_of_name = bufptr;
|
|
|
|
while (*(++bufptr)) {
|
|
|
|
if (*bufptr == '\n') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto& ksym = s_ksyms[current_ksym_index];
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (ksym.address < ksym_lowest_address)
|
|
|
|
ksym_lowest_address = ksym.address;
|
|
|
|
if (ksym.address > ksym_highest_address)
|
|
|
|
ksym_highest_address = ksym.address;
|
|
|
|
|
|
|
|
++bufptr;
|
|
|
|
++current_ksym_index;
|
|
|
|
}
|
2019-02-06 18:52:12 +01:00
|
|
|
kprintf("ok\n");
|
2018-12-24 22:59:10 +01:00
|
|
|
ksyms_ready = true;
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
[[gnu::noinline]] void dump_backtrace_impl(u32 ebp, bool use_ksyms)
|
2018-12-24 22:59:10 +01:00
|
|
|
{
|
2020-01-05 18:00:15 +01:00
|
|
|
SmapDisabler disabler;
|
|
|
|
#if 0
|
2018-12-24 22:59:10 +01:00
|
|
|
if (!current) {
|
2019-03-23 22:03:17 +01:00
|
|
|
//hang();
|
2018-12-24 22:59:10 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-01-05 18:00:15 +01:00
|
|
|
#endif
|
2018-12-24 22:59:10 +01:00
|
|
|
if (use_ksyms && !ksyms_ready) {
|
2019-02-15 12:30:48 +01:00
|
|
|
hang();
|
2018-12-24 22:59:10 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct RecognizedSymbol {
|
2019-07-03 21:17:35 +02:00
|
|
|
u32 address;
|
2018-12-24 22:59:10 +01:00
|
|
|
const KSym* ksym;
|
|
|
|
};
|
2019-04-15 23:50:25 +02:00
|
|
|
int max_recognized_symbol_count = 256;
|
|
|
|
RecognizedSymbol recognized_symbols[max_recognized_symbol_count];
|
|
|
|
int recognized_symbol_count = 0;
|
2018-12-24 22:59:10 +01:00
|
|
|
if (use_ksyms) {
|
2020-01-05 18:00:15 +01:00
|
|
|
for (u32* stack_ptr = (u32*)ebp;
|
2020-01-20 13:06:41 +01:00
|
|
|
(current ? current->process().validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2) : 1) && recognized_symbol_count < max_recognized_symbol_count; stack_ptr = (u32*)*stack_ptr) {
|
2019-07-03 21:17:35 +02:00
|
|
|
u32 retaddr = stack_ptr[1];
|
2019-04-30 14:47:22 +02:00
|
|
|
recognized_symbols[recognized_symbol_count++] = { retaddr, ksymbolicate(retaddr) };
|
2018-12-24 22:59:10 +01:00
|
|
|
}
|
2019-04-15 23:50:25 +02:00
|
|
|
} else {
|
2020-01-05 18:00:15 +01:00
|
|
|
for (u32* stack_ptr = (u32*)ebp;
|
2020-01-20 13:06:41 +01:00
|
|
|
(current ? current->process().validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2) : 1); stack_ptr = (u32*)*stack_ptr) {
|
2019-07-03 21:17:35 +02:00
|
|
|
u32 retaddr = stack_ptr[1];
|
|
|
|
dbgprintf("%x (next: %x)\n", retaddr, stack_ptr ? (u32*)*stack_ptr : 0);
|
2018-12-24 22:59:10 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-07-29 06:02:22 +12:00
|
|
|
ASSERT(recognized_symbol_count <= max_recognized_symbol_count);
|
2019-04-15 23:50:25 +02:00
|
|
|
for (int i = 0; i < recognized_symbol_count; ++i) {
|
|
|
|
auto& symbol = recognized_symbols[i];
|
2019-04-30 14:47:22 +02:00
|
|
|
if (!symbol.address)
|
|
|
|
break;
|
|
|
|
if (!symbol.ksym) {
|
2020-01-05 18:00:15 +01:00
|
|
|
if (current && current->process().elf_loader() && current->process().elf_loader()->has_symbols()) {
|
2019-05-16 17:18:25 +02:00
|
|
|
dbgprintf("%p %s\n", symbol.address, current->process().elf_loader()->symbolicate(symbol.address).characters());
|
|
|
|
} else {
|
2019-05-18 14:48:53 +02:00
|
|
|
dbgprintf("%p (no ELF symbols for process)\n", symbol.address);
|
2019-05-16 17:18:25 +02:00
|
|
|
}
|
2019-04-30 14:47:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-24 22:59:10 +01:00
|
|
|
unsigned offset = symbol.address - symbol.ksym->address;
|
2019-04-30 14:47:22 +02:00
|
|
|
if (symbol.ksym->address == ksym_highest_address && offset > 4096)
|
|
|
|
dbgprintf("%p\n", symbol.address);
|
|
|
|
else
|
2019-11-29 14:55:07 +01:00
|
|
|
dbgprintf("%p %s +%u\n", symbol.address, demangle(symbol.ksym->name).characters(), offset);
|
2018-12-24 22:59:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 13:41:16 +02:00
|
|
|
void dump_backtrace()
|
2019-02-24 14:51:48 +01:00
|
|
|
{
|
2019-05-18 17:31:52 +02:00
|
|
|
static bool in_dump_backtrace = false;
|
2019-11-08 17:36:29 +01:00
|
|
|
if (in_dump_backtrace)
|
2019-05-18 17:31:52 +02:00
|
|
|
return;
|
|
|
|
TemporaryChange change(in_dump_backtrace, true);
|
2019-08-07 20:35:41 +02:00
|
|
|
TemporaryChange disable_kmalloc_stacks(g_dump_kmalloc_stacks, false);
|
2019-07-03 21:17:35 +02:00
|
|
|
u32 ebp;
|
2019-06-07 11:43:58 +02:00
|
|
|
asm volatile("movl %%ebp, %%eax"
|
|
|
|
: "=a"(ebp));
|
2019-05-16 13:41:16 +02:00
|
|
|
dump_backtrace_impl(ebp, ksyms_ready);
|
2019-02-24 14:51:48 +01:00
|
|
|
}
|
|
|
|
|
2018-12-24 22:59:10 +01:00
|
|
|
void load_ksyms()
|
|
|
|
{
|
2020-01-19 01:04:48 +03:00
|
|
|
auto result = VFS::the().open("/res/kernel.map", O_RDONLY, 0, VFS::the().root_custody());
|
2019-03-06 22:14:31 +01:00
|
|
|
ASSERT(!result.is_error());
|
2019-06-13 22:03:04 +02:00
|
|
|
auto description = result.value();
|
|
|
|
auto buffer = description->read_entire_file();
|
2019-03-06 22:14:31 +01:00
|
|
|
ASSERT(buffer);
|
|
|
|
load_ksyms_from_data(buffer);
|
2018-12-24 22:59:10 +01:00
|
|
|
}
|