mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:17:42 +00:00
Kernel: Update cryptically-named functions related to symbolication
This commit is contained in:
parent
348e209eb5
commit
dc7340332d
6 changed files with 71 additions and 73 deletions
|
@ -133,7 +133,7 @@ void* kmalloc_impl(size_t size)
|
||||||
Kernel::InterruptDisabler disabler;
|
Kernel::InterruptDisabler disabler;
|
||||||
++g_kmalloc_call_count;
|
++g_kmalloc_call_count;
|
||||||
|
|
||||||
if (g_dump_kmalloc_stacks && Kernel::ksyms_ready) {
|
if (g_dump_kmalloc_stacks && Kernel::g_kernel_symbols_available) {
|
||||||
dbg() << "kmalloc(" << size << ")";
|
dbg() << "kmalloc(" << size << ")";
|
||||||
Kernel::dump_backtrace();
|
Kernel::dump_backtrace();
|
||||||
}
|
}
|
||||||
|
|
101
Kernel/KSyms.cpp
101
Kernel/KSyms.cpp
|
@ -34,11 +34,12 @@
|
||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
static KSym* s_ksyms;
|
FlatPtr g_lowest_kernel_symbol_address = 0xffffffff;
|
||||||
u32 ksym_lowest_address = 0xffffffff;
|
FlatPtr g_highest_kernel_symbol_address = 0;
|
||||||
u32 ksym_highest_address = 0;
|
bool g_kernel_symbols_available = false;
|
||||||
u32 ksym_count = 0;
|
|
||||||
bool ksyms_ready = false;
|
static KernelSymbol* s_symbols;
|
||||||
|
static size_t s_symbol_count = 0;
|
||||||
|
|
||||||
static u8 parse_hex_digit(char nibble)
|
static u8 parse_hex_digit(char nibble)
|
||||||
{
|
{
|
||||||
|
@ -50,43 +51,44 @@ static u8 parse_hex_digit(char nibble)
|
||||||
|
|
||||||
u32 address_for_kernel_symbol(const StringView& name)
|
u32 address_for_kernel_symbol(const StringView& name)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < ksym_count; ++i) {
|
for (size_t i = 0; i < s_symbol_count; ++i) {
|
||||||
if (!strncmp(name.characters_without_null_termination(), s_ksyms[i].name, name.length()))
|
if (!strncmp(name.characters_without_null_termination(), s_symbols[i].name, name.length()))
|
||||||
return s_ksyms[i].address;
|
return s_symbols[i].address;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const KSym* ksymbolicate(u32 address)
|
const KernelSymbol* symbolicate_kernel_address(u32 address)
|
||||||
{
|
{
|
||||||
if (address < ksym_lowest_address || address > ksym_highest_address)
|
if (address < g_lowest_kernel_symbol_address || address > g_highest_kernel_symbol_address)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
for (unsigned i = 0; i < ksym_count; ++i) {
|
for (unsigned i = 0; i < s_symbol_count; ++i) {
|
||||||
if (address < s_ksyms[i + 1].address)
|
if (address < s_symbols[i + 1].address)
|
||||||
return &s_ksyms[i];
|
return &s_symbols[i];
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void load_ksyms_from_data(const ByteBuffer& buffer)
|
static void load_kernel_sybols_from_data(const ByteBuffer& buffer)
|
||||||
{
|
{
|
||||||
ksym_lowest_address = 0xffffffff;
|
g_lowest_kernel_symbol_address = 0xffffffff;
|
||||||
ksym_highest_address = 0;
|
g_highest_kernel_symbol_address = 0;
|
||||||
|
|
||||||
auto* bufptr = (const char*)buffer.data();
|
auto* bufptr = (const char*)buffer.data();
|
||||||
auto* start_of_name = bufptr;
|
auto* start_of_name = bufptr;
|
||||||
u32 address = 0;
|
FlatPtr address = 0;
|
||||||
|
|
||||||
for (unsigned i = 0; i < 8; ++i)
|
for (size_t i = 0; i < 8; ++i)
|
||||||
ksym_count = (ksym_count << 4) | parse_hex_digit(*(bufptr++));
|
s_symbol_count = (s_symbol_count << 4) | parse_hex_digit(*(bufptr++));
|
||||||
s_ksyms = static_cast<KSym*>(kmalloc_eternal(sizeof(KSym) * ksym_count));
|
s_symbols = static_cast<KernelSymbol*>(kmalloc_eternal(sizeof(KernelSymbol) * s_symbol_count));
|
||||||
++bufptr; // skip newline
|
++bufptr; // skip newline
|
||||||
|
|
||||||
klog() << "Loading ksyms...";
|
klog() << "Loading kernel symbol table...";
|
||||||
|
|
||||||
unsigned current_ksym_index = 0;
|
size_t current_symbol_index = 0;
|
||||||
|
|
||||||
while (bufptr < buffer.end_pointer()) {
|
while (bufptr < buffer.end_pointer()) {
|
||||||
for (unsigned i = 0; i < 8; ++i)
|
for (size_t i = 0; i < 8; ++i)
|
||||||
address = (address << 4) | parse_hex_digit(*(bufptr++));
|
address = (address << 4) | parse_hex_digit(*(bufptr++));
|
||||||
bufptr += 3;
|
bufptr += 3;
|
||||||
start_of_name = bufptr;
|
start_of_name = bufptr;
|
||||||
|
@ -95,23 +97,22 @@ static void load_ksyms_from_data(const ByteBuffer& buffer)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto& ksym = s_ksyms[current_ksym_index];
|
auto& ksym = s_symbols[current_symbol_index];
|
||||||
ksym.address = address;
|
ksym.address = address;
|
||||||
char* name = static_cast<char*>(kmalloc_eternal((bufptr - start_of_name) + 1));
|
char* name = static_cast<char*>(kmalloc_eternal((bufptr - start_of_name) + 1));
|
||||||
memcpy(name, start_of_name, bufptr - start_of_name);
|
memcpy(name, start_of_name, bufptr - start_of_name);
|
||||||
name[bufptr - start_of_name] = '\0';
|
name[bufptr - start_of_name] = '\0';
|
||||||
ksym.name = name;
|
ksym.name = name;
|
||||||
|
|
||||||
if (ksym.address < ksym_lowest_address)
|
if (ksym.address < g_lowest_kernel_symbol_address)
|
||||||
ksym_lowest_address = ksym.address;
|
g_lowest_kernel_symbol_address = ksym.address;
|
||||||
if (ksym.address > ksym_highest_address)
|
if (ksym.address > g_highest_kernel_symbol_address)
|
||||||
ksym_highest_address = ksym.address;
|
g_highest_kernel_symbol_address = ksym.address;
|
||||||
|
|
||||||
++bufptr;
|
++bufptr;
|
||||||
++current_ksym_index;
|
++current_symbol_index;
|
||||||
}
|
}
|
||||||
klog() << "ok";
|
g_kernel_symbols_available = true;
|
||||||
ksyms_ready = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[[gnu::noinline]] void dump_backtrace_impl(u32 ebp, bool use_ksyms)
|
[[gnu::noinline]] void dump_backtrace_impl(u32 ebp, bool use_ksyms)
|
||||||
|
@ -123,7 +124,7 @@ static void load_ksyms_from_data(const ByteBuffer& buffer)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if (use_ksyms && !ksyms_ready) {
|
if (use_ksyms && !g_kernel_symbols_available) {
|
||||||
hang();
|
hang();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -133,32 +134,32 @@ static void load_ksyms_from_data(const ByteBuffer& buffer)
|
||||||
elf_bundle = Process::current->elf_bundle();
|
elf_bundle = Process::current->elf_bundle();
|
||||||
|
|
||||||
struct RecognizedSymbol {
|
struct RecognizedSymbol {
|
||||||
u32 address;
|
FlatPtr address;
|
||||||
const KSym* ksym;
|
const KernelSymbol* symbol { nullptr };
|
||||||
};
|
};
|
||||||
int max_recognized_symbol_count = 256;
|
size_t max_recognized_symbol_count = 256;
|
||||||
RecognizedSymbol recognized_symbols[max_recognized_symbol_count];
|
RecognizedSymbol recognized_symbols[max_recognized_symbol_count];
|
||||||
int recognized_symbol_count = 0;
|
size_t recognized_symbol_count = 0;
|
||||||
if (use_ksyms) {
|
if (use_ksyms) {
|
||||||
for (u32* stack_ptr = (u32*)ebp;
|
for (FlatPtr* stack_ptr = (FlatPtr*)ebp;
|
||||||
(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) {
|
(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) {
|
||||||
u32 retaddr = stack_ptr[1];
|
FlatPtr retaddr = stack_ptr[1];
|
||||||
recognized_symbols[recognized_symbol_count++] = { retaddr, ksymbolicate(retaddr) };
|
recognized_symbols[recognized_symbol_count++] = { retaddr, symbolicate_kernel_address(retaddr) };
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (u32* stack_ptr = (u32*)ebp;
|
for (FlatPtr* stack_ptr = (FlatPtr*)ebp;
|
||||||
(Process::current ? Process::current->validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2) : 1); stack_ptr = (u32*)*stack_ptr) {
|
(Process::current ? Process::current->validate_read_from_kernel(VirtualAddress(stack_ptr), sizeof(void*) * 2) : 1); stack_ptr = (u32*)*stack_ptr) {
|
||||||
u32 retaddr = stack_ptr[1];
|
FlatPtr retaddr = stack_ptr[1];
|
||||||
dbg() << String::format("%x", retaddr) << " (next: " << String::format("%x", (stack_ptr ? (u32*)*stack_ptr : 0)) << ")";
|
dbg() << String::format("%x", retaddr) << " (next: " << String::format("%x", (stack_ptr ? (u32*)*stack_ptr : 0)) << ")";
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ASSERT(recognized_symbol_count <= max_recognized_symbol_count);
|
ASSERT(recognized_symbol_count <= max_recognized_symbol_count);
|
||||||
for (int i = 0; i < recognized_symbol_count; ++i) {
|
for (size_t i = 0; i < recognized_symbol_count; ++i) {
|
||||||
auto& symbol = recognized_symbols[i];
|
auto& symbol = recognized_symbols[i];
|
||||||
if (!symbol.address)
|
if (!symbol.address)
|
||||||
break;
|
break;
|
||||||
if (!symbol.ksym) {
|
if (!symbol.symbol) {
|
||||||
if (elf_bundle && elf_bundle->elf_loader->has_symbols()) {
|
if (elf_bundle && elf_bundle->elf_loader->has_symbols()) {
|
||||||
dbg() << String::format("%p", symbol.address) << " " << elf_bundle->elf_loader->symbolicate(symbol.address);
|
dbg() << String::format("%p", symbol.address) << " " << elf_bundle->elf_loader->symbolicate(symbol.address);
|
||||||
} else {
|
} else {
|
||||||
|
@ -166,11 +167,11 @@ static void load_ksyms_from_data(const ByteBuffer& buffer)
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
unsigned offset = symbol.address - symbol.ksym->address;
|
size_t offset = symbol.address - symbol.symbol->address;
|
||||||
if (symbol.ksym->address == ksym_highest_address && offset > 4096)
|
if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096)
|
||||||
dbg() << String::format("%p", symbol.address);
|
dbg() << String::format("%p", symbol.address);
|
||||||
else
|
else
|
||||||
dbg() << String::format("%p", symbol.address) << " " << demangle(symbol.ksym->name) << " +" << offset;
|
dbg() << String::format("%p", symbol.address) << " " << demangle(symbol.symbol->name) << " +" << offset;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,20 +182,20 @@ void dump_backtrace()
|
||||||
return;
|
return;
|
||||||
TemporaryChange change(in_dump_backtrace, true);
|
TemporaryChange change(in_dump_backtrace, true);
|
||||||
TemporaryChange disable_kmalloc_stacks(g_dump_kmalloc_stacks, false);
|
TemporaryChange disable_kmalloc_stacks(g_dump_kmalloc_stacks, false);
|
||||||
u32 ebp;
|
FlatPtr ebp;
|
||||||
asm volatile("movl %%ebp, %%eax"
|
asm volatile("movl %%ebp, %%eax"
|
||||||
: "=a"(ebp));
|
: "=a"(ebp));
|
||||||
dump_backtrace_impl(ebp, ksyms_ready);
|
dump_backtrace_impl(ebp, g_kernel_symbols_available);
|
||||||
}
|
}
|
||||||
|
|
||||||
void load_ksyms()
|
void load_kernel_symbol_table()
|
||||||
{
|
{
|
||||||
auto result = VFS::the().open("/res/kernel.map", O_RDONLY, 0, VFS::the().root_custody());
|
auto result = VFS::the().open("/res/kernel.map", O_RDONLY, 0, VFS::the().root_custody());
|
||||||
ASSERT(!result.is_error());
|
ASSERT(!result.is_error());
|
||||||
auto description = result.value();
|
auto description = result.value();
|
||||||
auto buffer = description->read_entire_file();
|
auto buffer = description->read_entire_file();
|
||||||
ASSERT(buffer);
|
ASSERT(buffer);
|
||||||
load_ksyms_from_data(buffer);
|
load_kernel_sybols_from_data(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,23 +26,22 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/String.h>
|
#include <AK/Forward.h>
|
||||||
#include <AK/Vector.h>
|
|
||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
struct KSym {
|
struct KernelSymbol {
|
||||||
u32 address;
|
u32 address;
|
||||||
const char* name;
|
const char* name;
|
||||||
};
|
};
|
||||||
|
|
||||||
u32 address_for_kernel_symbol(const StringView& name);
|
u32 address_for_kernel_symbol(const StringView& name);
|
||||||
const KSym* ksymbolicate(u32 address);
|
const KernelSymbol* symbolicate_kernel_address(u32 address);
|
||||||
void load_ksyms();
|
void load_kernel_symbol_table();
|
||||||
|
|
||||||
extern bool ksyms_ready;
|
extern bool g_kernel_symbols_available;
|
||||||
extern u32 ksym_lowest_address;
|
extern FlatPtr g_lowest_kernel_symbol_address;
|
||||||
extern u32 ksym_highest_address;
|
extern FlatPtr g_highest_kernel_symbol_address;
|
||||||
|
|
||||||
void dump_backtrace();
|
void dump_backtrace();
|
||||||
|
|
||||||
|
|
|
@ -1495,9 +1495,9 @@ void Process::crash(int signal, u32 eip)
|
||||||
ASSERT(!is_dead());
|
ASSERT(!is_dead());
|
||||||
ASSERT(Process::current == this);
|
ASSERT(Process::current == this);
|
||||||
|
|
||||||
if (eip >= 0xc0000000 && ksyms_ready) {
|
if (eip >= 0xc0000000 && g_kernel_symbols_available) {
|
||||||
auto* ksym = ksymbolicate(eip);
|
auto* symbol = symbolicate_kernel_address(eip);
|
||||||
dbg() << "\033[31;1m" << String::format("%p", eip) << " " << (ksym ? demangle(ksym->name) : "(k?)") << " +" << (ksym ? eip - ksym->address : 0) << "\033[0m\n";
|
dbg() << "\033[31;1m" << String::format("%p", eip) << " " << (symbol ? demangle(symbol->name) : "(k?)") << " +" << (symbol ? eip - symbol->address : 0) << "\033[0m\n";
|
||||||
} else if (auto elf_bundle = this->elf_bundle()) {
|
} else if (auto elf_bundle = this->elf_bundle()) {
|
||||||
dbg() << "\033[31;1m" << String::format("%p", eip) << " " << elf_bundle->elf_loader->symbolicate(eip) << "\033[0m\n";
|
dbg() << "\033[31;1m" << String::format("%p", eip) << " " << elf_bundle->elf_loader->symbolicate(eip) << "\033[0m\n";
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -775,7 +775,7 @@ String Thread::backtrace(ProcessInspectionHandle&) const
|
||||||
|
|
||||||
struct RecognizedSymbol {
|
struct RecognizedSymbol {
|
||||||
u32 address;
|
u32 address;
|
||||||
const KSym* ksym;
|
const KernelSymbol* symbol { nullptr };
|
||||||
};
|
};
|
||||||
|
|
||||||
static bool symbolicate(const RecognizedSymbol& symbol, const Process& process, StringBuilder& builder, Process::ELFBundle* elf_bundle)
|
static bool symbolicate(const RecognizedSymbol& symbol, const Process& process, StringBuilder& builder, Process::ELFBundle* elf_bundle)
|
||||||
|
@ -784,7 +784,7 @@ static bool symbolicate(const RecognizedSymbol& symbol, const Process& process,
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
bool mask_kernel_addresses = !process.is_superuser();
|
bool mask_kernel_addresses = !process.is_superuser();
|
||||||
if (!symbol.ksym) {
|
if (!symbol.symbol) {
|
||||||
if (!is_user_address(VirtualAddress(symbol.address))) {
|
if (!is_user_address(VirtualAddress(symbol.address))) {
|
||||||
builder.append("0xdeadc0de\n");
|
builder.append("0xdeadc0de\n");
|
||||||
} else {
|
} else {
|
||||||
|
@ -795,11 +795,11 @@ static bool symbolicate(const RecognizedSymbol& symbol, const Process& process,
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
unsigned offset = symbol.address - symbol.ksym->address;
|
unsigned offset = symbol.address - symbol.symbol->address;
|
||||||
if (symbol.ksym->address == ksym_highest_address && offset > 4096) {
|
if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096) {
|
||||||
builder.appendf("%p\n", mask_kernel_addresses ? 0xdeadc0de : symbol.address);
|
builder.appendf("%p\n", mask_kernel_addresses ? 0xdeadc0de : symbol.address);
|
||||||
} else {
|
} else {
|
||||||
builder.appendf("%p %s +%u\n", mask_kernel_addresses ? 0xdeadc0de : symbol.address, demangle(symbol.ksym->name).characters(), offset);
|
builder.appendf("%p %s +%u\n", mask_kernel_addresses ? 0xdeadc0de : symbol.address, demangle(symbol.symbol->name).characters(), offset);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -814,7 +814,7 @@ String Thread::backtrace_impl() const
|
||||||
: "=a"(start_frame));
|
: "=a"(start_frame));
|
||||||
} else {
|
} else {
|
||||||
start_frame = frame_ptr();
|
start_frame = frame_ptr();
|
||||||
recognized_symbols.append({ tss().eip, ksymbolicate(tss().eip) });
|
recognized_symbols.append({ tss().eip, symbolicate_kernel_address(tss().eip) });
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& process = const_cast<Process&>(this->process());
|
auto& process = const_cast<Process&>(this->process());
|
||||||
|
@ -829,11 +829,11 @@ String Thread::backtrace_impl() const
|
||||||
|
|
||||||
if (is_user_range(VirtualAddress(stack_ptr), sizeof(FlatPtr) * 2)) {
|
if (is_user_range(VirtualAddress(stack_ptr), sizeof(FlatPtr) * 2)) {
|
||||||
copy_from_user(&retaddr, &((FlatPtr*)stack_ptr)[1]);
|
copy_from_user(&retaddr, &((FlatPtr*)stack_ptr)[1]);
|
||||||
recognized_symbols.append({ retaddr, ksymbolicate(retaddr) });
|
recognized_symbols.append({ retaddr, symbolicate_kernel_address(retaddr) });
|
||||||
copy_from_user(&stack_ptr, (FlatPtr*)stack_ptr);
|
copy_from_user(&stack_ptr, (FlatPtr*)stack_ptr);
|
||||||
} else {
|
} else {
|
||||||
memcpy(&retaddr, &((FlatPtr*)stack_ptr)[1], sizeof(FlatPtr));
|
memcpy(&retaddr, &((FlatPtr*)stack_ptr)[1], sizeof(FlatPtr));
|
||||||
recognized_symbols.append({ retaddr, ksymbolicate(retaddr) });
|
recognized_symbols.append({ retaddr, symbolicate_kernel_address(retaddr) });
|
||||||
memcpy(&stack_ptr, (FlatPtr*)stack_ptr, sizeof(FlatPtr));
|
memcpy(&stack_ptr, (FlatPtr*)stack_ptr, sizeof(FlatPtr));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -305,9 +305,7 @@ void init_stage2()
|
||||||
|
|
||||||
Process::current->set_root_directory(VFS::the().root_custody());
|
Process::current->set_root_directory(VFS::the().root_custody());
|
||||||
|
|
||||||
dbg() << "Load ksyms";
|
load_kernel_symbol_table();
|
||||||
load_ksyms();
|
|
||||||
dbg() << "Loaded ksyms";
|
|
||||||
|
|
||||||
int error;
|
int error;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue