mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 04:17:35 +00: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
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue