mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 17:44:56 +09:00
LibCore: Implement a helper that waits for a debugger then breaks
This method can be used to force the current process to sleep, waiting for a debugger to attach. On attach, the debugger breaks at the callsite directly. This is tested on Linux and macOS, in Clion and also terminal gdb and lldb.
This commit is contained in:
parent
216667368d
commit
35c45a94d3
Notes:
sideshowbarker
2024-07-17 05:06:13 +09:00
Author: https://github.com/SebastianZaha
Commit: 35c45a94d3
Pull-request: https://github.com/SerenityOS/serenity/pull/20327
Reviewed-by: https://github.com/ADKaster ✅
Reviewed-by: https://github.com/AtkinsSJ
2 changed files with 23 additions and 0 deletions
|
@ -166,4 +166,26 @@ ErrorOr<bool> Process::is_being_debugged()
|
|||
return Error::from_string_view("Platform does not support checking for debugger"sv);
|
||||
}
|
||||
|
||||
// Forces the process to sleep until a debugger is attached, then breaks.
|
||||
void Process::wait_for_debugger_and_break()
|
||||
{
|
||||
bool should_print_process_info { true };
|
||||
for (;;) {
|
||||
auto check = Process::is_being_debugged();
|
||||
if (check.is_error()) {
|
||||
dbgln("Cannot wait for debugger: {}. Continuing.", check.release_error());
|
||||
return;
|
||||
}
|
||||
if (check.value()) {
|
||||
kill(getpid(), SIGTRAP);
|
||||
return;
|
||||
}
|
||||
if (should_print_process_info) {
|
||||
dbgln("Process {} with pid {} is sleeping, waiting for debugger.", Process::get_name(), getpid());
|
||||
should_print_process_info = false;
|
||||
}
|
||||
::usleep(100 * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ public:
|
|||
};
|
||||
static ErrorOr<void> set_name(StringView, SetThreadName = SetThreadName::No);
|
||||
|
||||
static void wait_for_debugger_and_break();
|
||||
static ErrorOr<bool> is_being_debugged();
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue