2018-12-25 00:59:10 +03:00
|
|
|
#include "KSyms.h"
|
|
|
|
#include "Process.h"
|
|
|
|
#include "Scheduler.h"
|
2019-05-23 17:42:13 +03:00
|
|
|
#include <AK/ELF/ELFLoader.h>
|
2019-05-18 18:31:52 +03:00
|
|
|
#include <AK/TemporaryChange.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
2018-12-25 00:59:10 +03:00
|
|
|
|
|
|
|
static KSym* s_ksyms;
|
|
|
|
dword ksym_lowest_address;
|
|
|
|
dword ksym_highest_address;
|
|
|
|
dword ksym_count;
|
|
|
|
bool ksyms_ready;
|
|
|
|
|
|
|
|
static byte parse_hex_digit(char nibble)
|
|
|
|
{
|
|
|
|
if (nibble >= '0' && nibble <= '9')
|
|
|
|
return nibble - '0';
|
|
|
|
ASSERT(nibble >= 'a' && nibble <= 'f');
|
|
|
|
return 10 + (nibble - 'a');
|
|
|
|
}
|
|
|
|
|
|
|
|
const KSym* ksymbolicate(dword address)
|
|
|
|
{
|
|
|
|
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 15:47:22 +03:00
|
|
|
ksym_lowest_address = 0xffffffff;
|
|
|
|
ksym_highest_address = 0;
|
2018-12-25 00:59:10 +03:00
|
|
|
auto* bufptr = (const char*)buffer.pointer();
|
|
|
|
auto* start_of_name = bufptr;
|
|
|
|
dword address = 0;
|
|
|
|
|
|
|
|
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 20:52:12 +03:00
|
|
|
kprintf("Loading ksyms...");
|
2018-12-25 00:59:10 +03: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 20:52:12 +03:00
|
|
|
kprintf("ok\n");
|
2018-12-25 00:59:10 +03:00
|
|
|
ksyms_ready = true;
|
|
|
|
}
|
|
|
|
|
2019-02-24 16:51:48 +03:00
|
|
|
[[gnu::noinline]] void dump_backtrace_impl(dword ebp, bool use_ksyms)
|
2018-12-25 00:59:10 +03:00
|
|
|
{
|
|
|
|
if (!current) {
|
2019-03-24 00:03:17 +03:00
|
|
|
//hang();
|
2018-12-25 00:59:10 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (use_ksyms && !ksyms_ready) {
|
2019-02-15 14:30:48 +03:00
|
|
|
hang();
|
2018-12-25 00:59:10 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct RecognizedSymbol {
|
|
|
|
dword address;
|
|
|
|
const KSym* ksym;
|
|
|
|
};
|
2019-04-16 00:50:25 +03:00
|
|
|
int max_recognized_symbol_count = 256;
|
|
|
|
RecognizedSymbol recognized_symbols[max_recognized_symbol_count];
|
|
|
|
int recognized_symbol_count = 0;
|
2018-12-25 00:59:10 +03:00
|
|
|
if (use_ksyms) {
|
2019-06-07 13:56:50 +03:00
|
|
|
for (dword* stack_ptr = (dword*)ebp; current->process().validate_read_from_kernel(VirtualAddress((dword)stack_ptr)); stack_ptr = (dword*)*stack_ptr) {
|
2019-01-31 19:31:23 +03:00
|
|
|
dword retaddr = stack_ptr[1];
|
2019-04-30 15:47:22 +03:00
|
|
|
recognized_symbols[recognized_symbol_count++] = { retaddr, ksymbolicate(retaddr) };
|
2018-12-25 00:59:10 +03:00
|
|
|
}
|
2019-04-16 00:50:25 +03:00
|
|
|
} else {
|
2019-06-07 13:56:50 +03:00
|
|
|
for (dword* stack_ptr = (dword*)ebp; current->process().validate_read_from_kernel(VirtualAddress((dword)stack_ptr)); stack_ptr = (dword*)*stack_ptr) {
|
2019-01-31 19:31:23 +03:00
|
|
|
dword retaddr = stack_ptr[1];
|
2019-04-16 00:50:25 +03:00
|
|
|
dbgprintf("%x (next: %x)\n", retaddr, stack_ptr ? (dword*)*stack_ptr : 0);
|
2018-12-25 00:59:10 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-04-16 00:50:25 +03:00
|
|
|
ASSERT(recognized_symbol_count < max_recognized_symbol_count);
|
2019-01-31 19:31:23 +03:00
|
|
|
size_t bytes_needed = 0;
|
2019-04-16 00:50:25 +03:00
|
|
|
for (int i = 0; i < recognized_symbol_count; ++i) {
|
|
|
|
auto& symbol = recognized_symbols[i];
|
2019-04-30 15:47:22 +03:00
|
|
|
bytes_needed += (symbol.ksym ? strlen(symbol.ksym->name) : 0) + 8 + 16;
|
2018-12-25 00:59:10 +03:00
|
|
|
}
|
2019-04-16 00:50:25 +03:00
|
|
|
for (int i = 0; i < recognized_symbol_count; ++i) {
|
|
|
|
auto& symbol = recognized_symbols[i];
|
2019-04-30 15:47:22 +03:00
|
|
|
if (!symbol.address)
|
|
|
|
break;
|
|
|
|
if (!symbol.ksym) {
|
2019-05-18 15:48:53 +03:00
|
|
|
if (current->process().elf_loader() && current->process().elf_loader()->has_symbols()) {
|
2019-05-16 18:18:25 +03:00
|
|
|
dbgprintf("%p %s\n", symbol.address, current->process().elf_loader()->symbolicate(symbol.address).characters());
|
|
|
|
} else {
|
2019-05-18 15:48:53 +03:00
|
|
|
dbgprintf("%p (no ELF symbols for process)\n", symbol.address);
|
2019-05-16 18:18:25 +03:00
|
|
|
}
|
2019-04-30 15:47:22 +03:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-25 00:59:10 +03:00
|
|
|
unsigned offset = symbol.address - symbol.ksym->address;
|
2019-04-30 15:47:22 +03:00
|
|
|
if (symbol.ksym->address == ksym_highest_address && offset > 4096)
|
|
|
|
dbgprintf("%p\n", symbol.address);
|
|
|
|
else
|
|
|
|
dbgprintf("%p %s +%u\n", symbol.address, symbol.ksym->name, offset);
|
2018-12-25 00:59:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 14:41:16 +03:00
|
|
|
void dump_backtrace()
|
2019-02-24 16:51:48 +03:00
|
|
|
{
|
2019-05-18 18:31:52 +03:00
|
|
|
static bool in_dump_backtrace = false;
|
|
|
|
if (in_dump_backtrace) {
|
|
|
|
dbgprintf("dump_backtrace() called from within itself, what the hell is going on!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TemporaryChange change(in_dump_backtrace, true);
|
2019-02-24 16:51:48 +03:00
|
|
|
dword ebp;
|
2019-06-07 12:43:58 +03:00
|
|
|
asm volatile("movl %%ebp, %%eax"
|
|
|
|
: "=a"(ebp));
|
2019-05-16 14:41:16 +03:00
|
|
|
dump_backtrace_impl(ebp, ksyms_ready);
|
2019-02-24 16:51:48 +03:00
|
|
|
}
|
|
|
|
|
2018-12-25 00:59:10 +03:00
|
|
|
void init_ksyms()
|
|
|
|
{
|
|
|
|
ksyms_ready = false;
|
|
|
|
ksym_lowest_address = 0xffffffff;
|
|
|
|
ksym_highest_address = 0;
|
|
|
|
ksym_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void load_ksyms()
|
|
|
|
{
|
2019-05-30 19:58:59 +03:00
|
|
|
auto result = VFS::the().open("/kernel.map", 0, 0, VFS::the().root_custody());
|
2019-03-07 00:14:31 +03:00
|
|
|
ASSERT(!result.is_error());
|
2019-06-13 23:03:04 +03:00
|
|
|
auto description = result.value();
|
|
|
|
auto buffer = description->read_entire_file();
|
2019-03-07 00:14:31 +03:00
|
|
|
ASSERT(buffer);
|
|
|
|
load_ksyms_from_data(buffer);
|
2018-12-25 00:59:10 +03:00
|
|
|
}
|