HackStudio: Add progress bar to Debugger initialization

During Debugger initialization, most of the time is spent creating
DebugInfo objects for the libraries that the program has loaded.
This commit is contained in:
Itamar 2023-02-19 21:48:45 +02:00 committed by Andreas Kling
parent 0896c03744
commit 6e5b1f5819
Notes: sideshowbarker 2024-07-18 04:38:32 +09:00
5 changed files with 39 additions and 16 deletions

View file

@ -21,9 +21,10 @@ void Debugger::initialize(
DeprecatedString source_root,
Function<HasControlPassedToUser(PtraceRegisters const&)> on_stop_callback,
Function<void()> on_continue_callback,
Function<void()> on_exit_callback)
Function<void()> on_exit_callback,
Function<void(float)> on_initialization_progress)
{
s_the = new Debugger(source_root, move(on_stop_callback), move(on_continue_callback), move(on_exit_callback));
s_the = new Debugger(source_root, move(on_stop_callback), move(on_continue_callback), move(on_exit_callback), move(on_initialization_progress));
}
bool Debugger::is_initialized()
@ -35,11 +36,13 @@ Debugger::Debugger(
DeprecatedString source_root,
Function<HasControlPassedToUser(PtraceRegisters const&)> on_stop_callback,
Function<void()> on_continue_callback,
Function<void()> on_exit_callback)
Function<void()> on_exit_callback,
Function<void(float)> on_initialization_progress)
: m_source_root(source_root)
, m_on_stopped_callback(move(on_stop_callback))
, m_on_continue_callback(move(on_continue_callback))
, m_on_exit_callback(move(on_exit_callback))
, m_on_initialization_progress(move(on_initialization_progress))
{
pthread_mutex_init(&m_ui_action_mutex, nullptr);
pthread_cond_init(&m_ui_action_cond, nullptr);
@ -137,13 +140,13 @@ Debugger::CreateDebugSessionResult Debugger::create_debug_session()
return m_child_setup_callback();
return ErrorOr<void> {};
};
auto debug_session = Debug::DebugSession::exec_and_attach(m_executable_path, m_source_root, move(child_setup_callback));
auto debug_session = Debug::DebugSession::exec_and_attach(m_executable_path, m_source_root, move(child_setup_callback), move(m_on_initialization_progress));
VERIFY(!!debug_session);
return { debug_session.release_nonnull(), Debug::DebugSession::Running };
}
if (m_pid_to_attach.has_value()) {
auto debug_session = Debug::DebugSession::attach(m_pid_to_attach.value(), m_source_root);
auto debug_session = Debug::DebugSession::attach(m_pid_to_attach.value(), m_source_root, move(m_on_initialization_progress));
VERIFY(!!debug_session);
return { debug_session.release_nonnull(), Debug::DebugSession::Stopped };
}

View file

@ -29,7 +29,8 @@ public:
DeprecatedString source_root,
Function<HasControlPassedToUser(PtraceRegisters const&)> on_stop_callback,
Function<void()> on_continue_callback,
Function<void()> on_exit_callback);
Function<void()> on_exit_callback,
Function<void(float)> on_initialization_progress);
static bool is_initialized();
@ -94,7 +95,8 @@ private:
DeprecatedString source_root,
Function<HasControlPassedToUser(PtraceRegisters const&)> on_stop_callback,
Function<void()> on_continue_callback,
Function<void()> on_exit_callback);
Function<void()> on_exit_callback,
Function<void(float)> on_initialization_progress);
Debug::DebugInfo::SourcePosition create_source_position(DeprecatedString const& file, size_t line);
@ -130,6 +132,7 @@ private:
Function<void()> m_on_continue_callback;
Function<void()> m_on_exit_callback;
Function<ErrorOr<void>()> m_child_setup_callback;
Function<void(float)> m_on_initialization_progress;
};
}

View file

@ -1090,6 +1090,10 @@ void HackStudioWidget::initialize_debugger()
GUI::MessageBox::show(window(), "Program Exited"sv, "Debugger"sv, GUI::MessageBox::Type::Information);
});
GUI::Application::the()->event_loop().wake();
},
[this](float progress) {
if (GUI::Application::the()->active_window())
GUI::Application::the()->active_window()->set_progress(progress * 100);
});
}

View file

@ -17,10 +17,10 @@
namespace Debug {
DebugSession::DebugSession(pid_t pid, DeprecatedString source_root)
DebugSession::DebugSession(pid_t pid, DeprecatedString source_root, Function<void(float)> on_initialization_progress)
: m_debuggee_pid(pid)
, m_source_root(source_root)
, m_on_initialization_progress(move(on_initialization_progress))
{
}
@ -55,7 +55,8 @@ void DebugSession::for_each_loaded_library(Function<IterationDecision(LoadedLibr
OwnPtr<DebugSession> DebugSession::exec_and_attach(DeprecatedString const& command,
DeprecatedString source_root,
Function<ErrorOr<void>()> setup_child)
Function<ErrorOr<void>()> setup_child,
Function<void(float)> on_initialization_progress)
{
auto pid = fork();
@ -114,7 +115,7 @@ OwnPtr<DebugSession> DebugSession::exec_and_attach(DeprecatedString const& comma
return {};
}
auto debug_session = adopt_own(*new DebugSession(pid, source_root));
auto debug_session = adopt_own(*new DebugSession(pid, source_root, move(on_initialization_progress)));
// Continue until breakpoint before entry point of main program
int wstatus = debug_session->continue_debuggee_and_wait();
@ -129,7 +130,7 @@ OwnPtr<DebugSession> DebugSession::exec_and_attach(DeprecatedString const& comma
return debug_session;
}
OwnPtr<DebugSession> DebugSession::attach(pid_t pid, DeprecatedString source_root)
OwnPtr<DebugSession> DebugSession::attach(pid_t pid, DeprecatedString source_root, Function<void(float)> on_initialization_progress)
{
if (ptrace(PT_ATTACH, pid, 0, 0) < 0) {
perror("PT_ATTACH");
@ -142,7 +143,7 @@ OwnPtr<DebugSession> DebugSession::attach(pid_t pid, DeprecatedString source_roo
return {};
}
auto debug_session = adopt_own(*new DebugSession(pid, source_root));
auto debug_session = adopt_own(*new DebugSession(pid, source_root, move(on_initialization_progress)));
// At this point, libraries should have been loaded
debug_session->update_loaded_libs();
@ -467,7 +468,17 @@ void DebugSession::update_loaded_libs()
return DeprecatedString::formatted("/usr/lib/{}", lib_name);
};
ScopeGuard progress_guard([this]() {
m_on_initialization_progress(0);
});
size_t vm_entry_index = 0;
vm_entries.for_each([&](auto& entry) {
++vm_entry_index;
if (m_on_initialization_progress)
m_on_initialization_progress(vm_entry_index / static_cast<float>(vm_entries.size()));
// TODO: check that region is executable
auto vm_name = entry.as_object().get_deprecated_string("name"sv).value();

View file

@ -27,8 +27,8 @@ namespace Debug {
class DebugSession : public ProcessInspector {
public:
static OwnPtr<DebugSession> exec_and_attach(DeprecatedString const& command, DeprecatedString source_root = {}, Function<ErrorOr<void>()> setup_child = {});
static OwnPtr<DebugSession> attach(pid_t pid, DeprecatedString source_root = {});
static OwnPtr<DebugSession> exec_and_attach(DeprecatedString const& command, DeprecatedString source_root = {}, Function<ErrorOr<void>()> setup_child = {}, Function<void(float)> on_initialization_progress = {});
static OwnPtr<DebugSession> attach(pid_t pid, DeprecatedString source_root = {}, Function<void(float)> on_initialization_progress = {});
virtual ~DebugSession() override;
@ -131,7 +131,7 @@ public:
};
private:
explicit DebugSession(pid_t, DeprecatedString source_root);
explicit DebugSession(pid_t, DeprecatedString source_root, Function<void(float)> on_initialization_progress = {});
// x86 breakpoint instruction "int3"
static constexpr u8 BREAKPOINT_INSTRUCTION = 0xcc;
@ -147,6 +147,8 @@ private:
// Maps from library name to LoadedLibrary object
HashMap<DeprecatedString, NonnullOwnPtr<LoadedLibrary>> m_loaded_libraries;
Function<void(float)> m_on_initialization_progress;
};
template<typename Callback>