Userland: Use /proc/kernel_base to determine the kernel base address
This removes all the hard-coded kernel base addresses from userspace tools. One downside for this is that e.g. Profiler no longer uses a different color for kernel symbols when run as a non-root user.
This commit is contained in:
parent
6115258a5c
commit
60d6137e73
Notes:
sideshowbarker
2024-07-18 08:33:52 +09:00
Author: https://github.com/gunnarbeutner Commit: https://github.com/SerenityOS/serenity/commit/60d6137e734 Pull-request: https://github.com/SerenityOS/serenity/pull/8937
9 changed files with 63 additions and 48 deletions
|
@ -49,5 +49,5 @@ set(SOURCES
|
|||
)
|
||||
|
||||
serenity_app(HackStudio ICON app-hack-studio)
|
||||
target_link_libraries(HackStudio LibWeb LibMarkdown LibGUI LibCpp LibGfx LibCore LibVT LibDebug LibX86 LibDiff LibShell LibRegex)
|
||||
target_link_libraries(HackStudio LibWeb LibMarkdown LibGUI LibCpp LibGfx LibCore LibVT LibDebug LibX86 LibDiff LibShell LibSymbolication LibRegex)
|
||||
add_dependencies(HackStudio CppLanguageServer)
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <AK/StringBuilder.h>
|
||||
#include <LibDebug/DebugSession.h>
|
||||
#include <LibELF/Image.h>
|
||||
#include <LibSymbolication/Symbolication.h>
|
||||
#include <LibX86/Disassembler.h>
|
||||
#include <LibX86/ELFSymbolProvider.h>
|
||||
#include <ctype.h>
|
||||
|
@ -30,14 +31,9 @@ DisassemblyModel::DisassemblyModel(const Debug::DebugSession& debug_session, con
|
|||
OwnPtr<ELF::Image> kernel_elf;
|
||||
const ELF::Image* elf = nullptr;
|
||||
|
||||
// FIXME: Use /proc for this
|
||||
#if ARCH(I386)
|
||||
FlatPtr kernel_base = 0xc0000000;
|
||||
#else
|
||||
FlatPtr kernel_base = 0x2000000000;
|
||||
#endif
|
||||
auto maybe_kernel_base = Symbolication::kernel_base();
|
||||
|
||||
if (containing_function.value().address_low >= kernel_base) {
|
||||
if (maybe_kernel_base.has_value() && containing_function.value().address_low >= maybe_kernel_base.value()) {
|
||||
auto file_or_error = MappedFile::map("/boot/Kernel.debug");
|
||||
if (file_or_error.is_error())
|
||||
return;
|
||||
|
|
|
@ -19,4 +19,4 @@ set(SOURCES
|
|||
)
|
||||
|
||||
serenity_app(Profiler ICON app-profiler)
|
||||
target_link_libraries(Profiler LibGUI LibDesktop LibX86)
|
||||
target_link_libraries(Profiler LibGUI LibDesktop LibX86 LibSymbolication)
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <AK/MappedFile.h>
|
||||
#include <LibELF/Image.h>
|
||||
#include <LibGUI/Painter.h>
|
||||
#include <LibSymbolication/Symbolication.h>
|
||||
#include <LibX86/Disassembler.h>
|
||||
#include <LibX86/ELFSymbolProvider.h>
|
||||
#include <ctype.h>
|
||||
|
@ -40,13 +41,8 @@ DisassemblyModel::DisassemblyModel(Profile& profile, ProfileNode& node)
|
|||
OwnPtr<ELF::Image> kernel_elf;
|
||||
const ELF::Image* elf;
|
||||
FlatPtr base_address = 0;
|
||||
// FIXME: Use /proc for this
|
||||
#if ARCH(I386)
|
||||
FlatPtr kernel_base = 0xc0000000;
|
||||
#else
|
||||
FlatPtr kernel_base = 0x2000000000;
|
||||
#endif
|
||||
if (m_node.address() >= kernel_base) {
|
||||
auto maybe_kernel_base = Symbolication::kernel_base();
|
||||
if (maybe_kernel_base.has_value() && m_node.address() >= maybe_kernel_base.value()) {
|
||||
if (!m_kernel_file) {
|
||||
auto file_or_error = MappedFile::map("/boot/Kernel.debug");
|
||||
if (file_or_error.is_error())
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
#include <AK/RefPtr.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibELF/Image.h>
|
||||
#include <LibSymbolication/Symbolication.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace Profiler {
|
||||
|
@ -301,12 +302,7 @@ Result<NonnullOwnPtr<Profile>, String> Profile::load_from_perfcore_file(const St
|
|||
continue;
|
||||
}
|
||||
|
||||
// FIXME: Use /proc for this
|
||||
#if ARCH(I386)
|
||||
FlatPtr kernel_base = 0xc0000000;
|
||||
#else
|
||||
FlatPtr kernel_base = 0x2000000000;
|
||||
#endif
|
||||
auto maybe_kernel_base = Symbolication::kernel_base();
|
||||
|
||||
auto* stack = perf_event.get_ptr("stack");
|
||||
VERIFY(stack);
|
||||
|
@ -318,7 +314,7 @@ Result<NonnullOwnPtr<Profile>, String> Profile::load_from_perfcore_file(const St
|
|||
FlyString object_name;
|
||||
String symbol;
|
||||
|
||||
if (ptr >= kernel_base) {
|
||||
if (maybe_kernel_base.has_value() && ptr >= maybe_kernel_base.value()) {
|
||||
if (kernel_elf) {
|
||||
symbol = kernel_elf->symbolicate(ptr, &offset);
|
||||
} else {
|
||||
|
@ -345,7 +341,7 @@ Result<NonnullOwnPtr<Profile>, String> Profile::load_from_perfcore_file(const St
|
|||
continue;
|
||||
|
||||
FlatPtr innermost_frame_address = event.frames.at(1).address;
|
||||
event.in_kernel = innermost_frame_address >= kernel_base;
|
||||
event.in_kernel = maybe_kernel_base.has_value() && innermost_frame_address >= maybe_kernel_base.value();
|
||||
|
||||
events.append(move(event));
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include "Profile.h"
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibGUI/FileIconProvider.h>
|
||||
#include <LibSymbolication/Symbolication.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
@ -105,13 +106,8 @@ GUI::Variant ProfileModel::data(const GUI::ModelIndex& index, GUI::ModelRole rol
|
|||
if (node->is_root()) {
|
||||
return GUI::FileIconProvider::icon_for_executable(node->process().executable);
|
||||
}
|
||||
// FIXME: Use /proc for this
|
||||
#if ARCH(I386)
|
||||
FlatPtr kernel_base = 0xc0000000;
|
||||
#else
|
||||
FlatPtr kernel_base = 0x2000000000;
|
||||
#endif
|
||||
if (node->address() >= kernel_base)
|
||||
auto maybe_kernel_base = Symbolication::kernel_base();
|
||||
if (maybe_kernel_base.has_value() && node->address() >= maybe_kernel_base.value())
|
||||
return m_kernel_frame_icon;
|
||||
return m_user_frame_icon;
|
||||
}
|
||||
|
|
|
@ -21,6 +21,42 @@ struct CachedELF {
|
|||
|
||||
static HashMap<String, OwnPtr<CachedELF>> s_cache;
|
||||
|
||||
enum class KernelBaseState {
|
||||
Uninitialized,
|
||||
Valid,
|
||||
Invalid,
|
||||
};
|
||||
|
||||
static FlatPtr s_kernel_base;
|
||||
static KernelBaseState s_kernel_base_state = KernelBaseState::Uninitialized;
|
||||
|
||||
Optional<FlatPtr> kernel_base()
|
||||
{
|
||||
if (s_kernel_base_state == KernelBaseState::Uninitialized) {
|
||||
auto file = Core::File::open("/proc/kernel_base", Core::OpenMode::ReadOnly);
|
||||
if (file.is_error()) {
|
||||
s_kernel_base_state = KernelBaseState::Invalid;
|
||||
return {};
|
||||
}
|
||||
auto kernel_base_str = String { file.value()->read_all(), NoChomp };
|
||||
#if ARCH(I386)
|
||||
using AddressType = u32;
|
||||
#else
|
||||
using AddressType = u64;
|
||||
#endif
|
||||
auto maybe_kernel_base = kernel_base_str.to_uint<AddressType>();
|
||||
if (!maybe_kernel_base.has_value()) {
|
||||
s_kernel_base_state = KernelBaseState::Invalid;
|
||||
return {};
|
||||
}
|
||||
s_kernel_base = maybe_kernel_base.value();
|
||||
s_kernel_base_state = KernelBaseState::Valid;
|
||||
}
|
||||
if (s_kernel_base_state == KernelBaseState::Invalid)
|
||||
return {};
|
||||
return s_kernel_base;
|
||||
}
|
||||
|
||||
Optional<Symbol> symbolicate(String const& path, FlatPtr address)
|
||||
{
|
||||
if (!s_cache.contains(path)) {
|
||||
|
@ -81,16 +117,14 @@ Vector<Symbol> symbolicate_thread(pid_t pid, pid_t tid)
|
|||
Vector<FlatPtr> stack;
|
||||
Vector<RegionWithSymbols> regions;
|
||||
|
||||
regions.append(RegionWithSymbols {
|
||||
// FIXME: Use /proc for this
|
||||
#if ARCH(I386)
|
||||
.base = 0xc0000000,
|
||||
#else
|
||||
.base = 0x2000000000,
|
||||
#endif
|
||||
.size = 0x3fffffff,
|
||||
.path = "/boot/Kernel.debug",
|
||||
.is_relative = false });
|
||||
if (auto maybe_kernel_base = kernel_base(); maybe_kernel_base.has_value()) {
|
||||
regions.append(RegionWithSymbols {
|
||||
.base = maybe_kernel_base.value(),
|
||||
.size = 0x3fffffff,
|
||||
.path = "/boot/Kernel.debug",
|
||||
.is_relative = false,
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
auto stack_path = String::formatted("/proc/{}/stacks/{}", pid, tid);
|
||||
|
|
|
@ -18,6 +18,7 @@ struct Symbol {
|
|||
Vector<Debug::DebugInfo::SourcePosition> source_positions;
|
||||
};
|
||||
|
||||
Optional<FlatPtr> kernel_base();
|
||||
Vector<Symbol> symbolicate_thread(pid_t pid, pid_t tid);
|
||||
Optional<Symbol> symbolicate(String const& path, FlatPtr address);
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/DirIterator.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibSymbolication/Symbolication.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -44,13 +45,8 @@ int main(int argc, char** argv)
|
|||
auto frame_number = symbols.size() - 1;
|
||||
for (auto& symbol : symbols) {
|
||||
// Make kernel stack frames stand out.
|
||||
// FIXME: Use /proc for this
|
||||
#if ARCH(I386)
|
||||
FlatPtr kernel_base = 0xc0000000;
|
||||
#else
|
||||
FlatPtr kernel_base = 0x2000000000;
|
||||
#endif
|
||||
int color = symbol.address < kernel_base ? 35 : 31;
|
||||
auto maybe_kernel_base = Symbolication::kernel_base();
|
||||
int color = maybe_kernel_base.has_value() && symbol.address < maybe_kernel_base.value() ? 35 : 31;
|
||||
out("{:3}: \033[{};1m{:p}\033[0m | ", frame_number, color, symbol.address);
|
||||
if (!symbol.name.is_empty())
|
||||
out("{} ", symbol.name);
|
||||
|
|
Loading…
Add table
Reference in a new issue