summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-04-08 13:30:50 +0200
committerAndreas Kling <kling@serenityos.org>2020-04-08 17:19:46 +0200
commitdc7340332d94a9e0716ebe7e7d3fcfd6fe4fca13 (patch)
tree5517dee6adffc743f86c03f0981544eb3a06671e
parent348e209eb527d94da5bd51d5660d9f662c952a54 (diff)
downloadserenity-dc7340332d94a9e0716ebe7e7d3fcfd6fe4fca13.zip
Kernel: Update cryptically-named functions related to symbolication
-rw-r--r--Kernel/Heap/kmalloc.cpp2
-rw-r--r--Kernel/KSyms.cpp101
-rw-r--r--Kernel/KSyms.h15
-rw-r--r--Kernel/Process.cpp6
-rw-r--r--Kernel/Thread.cpp16
-rw-r--r--Kernel/init.cpp4
6 files changed, 71 insertions, 73 deletions
diff --git a/Kernel/Heap/kmalloc.cpp b/Kernel/Heap/kmalloc.cpp
index 239dcf57e3..2cf707bf4a 100644
--- a/Kernel/Heap/kmalloc.cpp
+++ b/Kernel/Heap/kmalloc.cpp
@@ -133,7 +133,7 @@ void* kmalloc_impl(size_t size)
Kernel::InterruptDisabler disabler;
++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 << ")";
Kernel::dump_backtrace();
}
diff --git a/Kernel/KSyms.cpp b/Kernel/KSyms.cpp
index 0dbf06fa99..0512cd4022 100644
--- a/Kernel/KSyms.cpp
+++ b/Kernel/KSyms.cpp
@@ -34,11 +34,12 @@
namespace Kernel {
-static KSym* s_ksyms;
-u32 ksym_lowest_address = 0xffffffff;
-u32 ksym_highest_address = 0;
-u32 ksym_count = 0;
-bool ksyms_ready = false;
+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;
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)
{
- for (unsigned i = 0; i < ksym_count; ++i) {
- if (!strncmp(name.characters_without_null_termination(), s_ksyms[i].name, name.length()))
- return s_ksyms[i].address;
+ 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;
}
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;
- for (unsigned i = 0; i < ksym_count; ++i) {
- if (address < s_ksyms[i + 1].address)
- return &s_ksyms[i];
+ for (unsigned i = 0; i < s_symbol_count; ++i) {
+ if (address < s_symbols[i + 1].address)
+ return &s_symbols[i];
}
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;
- ksym_highest_address = 0;
+ g_lowest_kernel_symbol_address = 0xffffffff;
+ g_highest_kernel_symbol_address = 0;
+
auto* bufptr = (const char*)buffer.data();
auto* start_of_name = bufptr;
- u32 address = 0;
+ FlatPtr 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));
+ 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));
++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()) {
- for (unsigned i = 0; i < 8; ++i)
+ for (size_t i = 0; i < 8; ++i)
address = (address << 4) | parse_hex_digit(*(bufptr++));
bufptr += 3;
start_of_name = bufptr;
@@ -95,23 +97,22 @@ static void load_ksyms_from_data(const ByteBuffer& buffer)
break;
}
}
- auto& ksym = s_ksyms[current_ksym_index];
+ auto& ksym = s_symbols[current_symbol_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;
+ 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;
++bufptr;
- ++current_ksym_index;
+ ++current_symbol_index;
}
- klog() << "ok";
- ksyms_ready = true;
+ g_kernel_symbols_available = true;
}
[[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;
}
#endif
- if (use_ksyms && !ksyms_ready) {
+ if (use_ksyms && !g_kernel_symbols_available) {
hang();
return;
}
@@ -133,32 +134,32 @@ static void load_ksyms_from_data(const ByteBuffer& buffer)
elf_bundle = Process::current->elf_bundle();
struct RecognizedSymbol {
- u32 address;
- const KSym* ksym;
+ FlatPtr address;
+ const KernelSymbol* symbol { nullptr };
};
- int max_recognized_symbol_count = 256;
+ size_t max_recognized_symbol_count = 256;
RecognizedSymbol recognized_symbols[max_recognized_symbol_count];
- int recognized_symbol_count = 0;
+ size_t recognized_symbol_count = 0;
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) {
- u32 retaddr = stack_ptr[1];
- recognized_symbols[recognized_symbol_count++] = { retaddr, ksymbolicate(retaddr) };
+ FlatPtr retaddr = stack_ptr[1];
+ recognized_symbols[recognized_symbol_count++] = { retaddr, symbolicate_kernel_address(retaddr) };
}
} 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) {
- 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)) << ")";
}
return;
}
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];
if (!symbol.address)
break;
- if (!symbol.ksym) {
+ if (!symbol.symbol) {
if (elf_bundle && elf_bundle->elf_loader->has_symbols()) {
dbg() << String::format("%p", symbol.address) << " " << elf_bundle->elf_loader->symbolicate(symbol.address);
} else {
@@ -166,11 +167,11 @@ static void load_ksyms_from_data(const ByteBuffer& buffer)
}
continue;
}
- unsigned offset = symbol.address - symbol.ksym->address;
- if (symbol.ksym->address == ksym_highest_address && offset > 4096)
+ size_t offset = symbol.address - symbol.symbol->address;
+ if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096)
dbg() << String::format("%p", symbol.address);
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;
TemporaryChange change(in_dump_backtrace, true);
TemporaryChange disable_kmalloc_stacks(g_dump_kmalloc_stacks, false);
- u32 ebp;
+ FlatPtr ebp;
asm volatile("movl %%ebp, %%eax"
: "=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());
ASSERT(!result.is_error());
auto description = result.value();
auto buffer = description->read_entire_file();
ASSERT(buffer);
- load_ksyms_from_data(buffer);
+ load_kernel_sybols_from_data(buffer);
}
}
diff --git a/Kernel/KSyms.h b/Kernel/KSyms.h
index 2ff3c23a69..102ae06c18 100644
--- a/Kernel/KSyms.h
+++ b/Kernel/KSyms.h
@@ -26,23 +26,22 @@
#pragma once
-#include <AK/String.h>
-#include <AK/Vector.h>
+#include <AK/Forward.h>
namespace Kernel {
-struct KSym {
+struct KernelSymbol {
u32 address;
const char* name;
};
u32 address_for_kernel_symbol(const StringView& name);
-const KSym* ksymbolicate(u32 address);
-void load_ksyms();
+const KernelSymbol* symbolicate_kernel_address(u32 address);
+void load_kernel_symbol_table();
-extern bool ksyms_ready;
-extern u32 ksym_lowest_address;
-extern u32 ksym_highest_address;
+extern bool g_kernel_symbols_available;
+extern FlatPtr g_lowest_kernel_symbol_address;
+extern FlatPtr g_highest_kernel_symbol_address;
void dump_backtrace();
diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp
index 28aca99bcb..337a27a3c5 100644
--- a/Kernel/Process.cpp
+++ b/Kernel/Process.cpp
@@ -1495,9 +1495,9 @@ void Process::crash(int signal, u32 eip)
ASSERT(!is_dead());
ASSERT(Process::current == this);
- if (eip >= 0xc0000000 && ksyms_ready) {
- auto* ksym = ksymbolicate(eip);
- dbg() << "\033[31;1m" << String::format("%p", eip) << " " << (ksym ? demangle(ksym->name) : "(k?)") << " +" << (ksym ? eip - ksym->address : 0) << "\033[0m\n";
+ if (eip >= 0xc0000000 && g_kernel_symbols_available) {
+ auto* symbol = symbolicate_kernel_address(eip);
+ 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()) {
dbg() << "\033[31;1m" << String::format("%p", eip) << " " << elf_bundle->elf_loader->symbolicate(eip) << "\033[0m\n";
} else {
diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp
index 563e0c02ba..d126cde5ed 100644
--- a/Kernel/Thread.cpp
+++ b/Kernel/Thread.cpp
@@ -775,7 +775,7 @@ String Thread::backtrace(ProcessInspectionHandle&) const
struct RecognizedSymbol {
u32 address;
- const KSym* ksym;
+ const KernelSymbol* symbol { nullptr };
};
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;
bool mask_kernel_addresses = !process.is_superuser();
- if (!symbol.ksym) {
+ if (!symbol.symbol) {
if (!is_user_address(VirtualAddress(symbol.address))) {
builder.append("0xdeadc0de\n");
} else {
@@ -795,11 +795,11 @@ static bool symbolicate(const RecognizedSymbol& symbol, const Process& process,
}
return true;
}
- unsigned offset = symbol.address - symbol.ksym->address;
- if (symbol.ksym->address == ksym_highest_address && offset > 4096) {
+ unsigned offset = symbol.address - symbol.symbol->address;
+ if (symbol.symbol->address == g_highest_kernel_symbol_address && offset > 4096) {
builder.appendf("%p\n", mask_kernel_addresses ? 0xdeadc0de : symbol.address);
} 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;
}
@@ -814,7 +814,7 @@ String Thread::backtrace_impl() const
: "=a"(start_frame));
} else {
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());
@@ -829,11 +829,11 @@ String Thread::backtrace_impl() const
if (is_user_range(VirtualAddress(stack_ptr), sizeof(FlatPtr) * 2)) {
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);
} else {
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));
}
}
diff --git a/Kernel/init.cpp b/Kernel/init.cpp
index ddb622f40f..f6f6c7353d 100644
--- a/Kernel/init.cpp
+++ b/Kernel/init.cpp
@@ -305,9 +305,7 @@ void init_stage2()
Process::current->set_root_directory(VFS::the().root_custody());
- dbg() << "Load ksyms";
- load_ksyms();
- dbg() << "Loaded ksyms";
+ load_kernel_symbol_table();
int error;