From 6f6a2dc3198fcdd0deef5871ed9ede86f9c4eb9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Holz?= Date: Thu, 4 Jan 2024 02:03:08 +0100 Subject: [PATCH] HackStudio: Correctly handle invalid addresses during backtrace creation This fixes a bug where we previously would hang if we couldn't find a library containing the current program counter. --- .../HackStudio/Debugger/BacktraceModel.cpp | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/Userland/DevTools/HackStudio/Debugger/BacktraceModel.cpp b/Userland/DevTools/HackStudio/Debugger/BacktraceModel.cpp index e03fa63624..71f17bbb06 100644 --- a/Userland/DevTools/HackStudio/Debugger/BacktraceModel.cpp +++ b/Userland/DevTools/HackStudio/Debugger/BacktraceModel.cpp @@ -39,22 +39,26 @@ Vector BacktraceModel::create_backtrace(Debug::Proces size_t frame_index = 0; do { auto lib = inspector.library_at(current_program_counter); - if (!lib) - continue; - // After the first frame, current_instruction holds the return address from the function call. - // We need to go back to the 'call' instruction to get accurate source position information. - if (frame_index > 0) - --current_program_counter; - ByteString name = lib->debug_info->elf().symbolicate(current_program_counter - lib->base_address); - if (name.is_empty()) { - dbgln("BacktraceModel: couldn't find containing function for address: {:p} (library={})", current_program_counter, lib->name); - name = ""; + if (lib) { + // After the first frame, current_instruction holds the return address from the function call. + // We need to go back to the 'call' instruction to get accurate source position information. + if (frame_index > 0) + --current_program_counter; + ByteString name = lib->debug_info->elf().symbolicate(current_program_counter - lib->base_address); + if (name.is_empty()) { + dbgln("BacktraceModel: couldn't find containing function for address: {:p} (library={})", current_program_counter, lib->name); + name = ""; + } + + auto source_position = lib->debug_info->get_source_position(current_program_counter - lib->base_address); + + frames.append({ name, current_program_counter, current_frame_pointer, source_position }); + } else { + dbgln("BacktraceModel: couldn't find containing library for address: {:p}", current_program_counter); + frames.append({ "", current_program_counter, current_frame_pointer, {} }); } - auto source_position = lib->debug_info->get_source_position(current_program_counter - lib->base_address); - - frames.append({ name, current_program_counter, current_frame_pointer, source_position }); auto frame_info = Debug::StackFrameUtils::get_info(inspector, current_frame_pointer); VERIFY(frame_info.has_value()); current_program_counter = frame_info.value().return_address;