mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 20:37:34 +00:00
ptrace: Stop a traced thread when it exists from execve
This was a missing feature in the PT_TRACEME command. This feature allows the tracer to interact with the tracee before the tracee has started executing its program. It will be useful for automatically inserting a breakpoint at a debugged program's entry point.
This commit is contained in:
parent
4568a628f9
commit
0431712660
3 changed files with 58 additions and 16 deletions
|
@ -90,19 +90,42 @@ void run_child_and_attach(char** argv)
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dbg() << "debugee should continue until before execve exit";
|
||||||
|
if (ptrace(PT_CONTINUE, g_pid, 0, 0) == -1) {
|
||||||
|
perror("continue");
|
||||||
|
}
|
||||||
|
|
||||||
// we want to continue until the exit from the 'execve' sycsall
|
// we want to continue until the exit from the 'execve' sycsall
|
||||||
// we do this to ensure that when we start debugging the process,
|
// we do this to ensure that when we start debugging the process,
|
||||||
// it executes the target image, and not the forked image of the debugger
|
// it executes the target image, and not the forked image of the debugger
|
||||||
// NOTE: we only need to do this when we are debugging a new process (i.e not attaching to a process that's already running!)
|
// NOTE: we only need to do this when we are debugging a new process (i.e not attaching to a process that's already running!)
|
||||||
if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) {
|
// if (ptrace(PT_SYSCALL, g_pid, 0, 0) == -1) {
|
||||||
perror("syscall");
|
// perror("syscall");
|
||||||
exit(1);
|
// exit(1);
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
|
if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
|
||||||
perror("wait_pid");
|
perror("wait_pid");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
// dbg() << "debugee should continue until after execve exit";
|
||||||
|
// sleep(3);
|
||||||
|
|
||||||
|
// if (ptrace(PT_CONTINUE, g_pid, 0, 0) == -1) {
|
||||||
|
// perror("continue");
|
||||||
|
// }
|
||||||
|
|
||||||
|
// sleep(10);
|
||||||
|
|
||||||
|
// if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
|
||||||
|
// perror("wait_pid");
|
||||||
|
// exit(1);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// dbg() << "debugee should already be running";
|
||||||
|
// if (ptrace(PT_CONTINUE, g_pid, 0, 0) == -1) {
|
||||||
|
// perror("continue");
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
VirtualAddress get_entry_point(int pid)
|
VirtualAddress get_entry_point(int pid)
|
||||||
|
@ -138,9 +161,23 @@ int main(int argc, char** argv)
|
||||||
auto entry_point = get_entry_point(g_pid);
|
auto entry_point = get_entry_point(g_pid);
|
||||||
dbg() << "entry point:" << entry_point;
|
dbg() << "entry point:" << entry_point;
|
||||||
|
|
||||||
|
uint32_t data = ptrace(PT_PEEK, g_pid, (void*)entry_point.as_ptr(), 0);
|
||||||
|
|
||||||
|
// u8* as_bytes = reinterpret_cast<u8*>(&data);
|
||||||
|
// as_bytes[0] = 0xcc;
|
||||||
|
dbg() << "peeked data:" << (void*)data;
|
||||||
|
data = (data & ~(uint32_t)0xff) | 0xcc;
|
||||||
|
data = 0xccccccc;
|
||||||
|
|
||||||
|
if (ptrace(PT_POKE, g_pid, (void*)entry_point.as_ptr(), data) < 0) {
|
||||||
|
perror("poke");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
dbg() << "continuting";
|
||||||
if (ptrace(PT_CONTINUE, g_pid, 0, 0) == -1) {
|
if (ptrace(PT_CONTINUE, g_pid, 0, 0) == -1) {
|
||||||
perror("continue");
|
perror("continue");
|
||||||
}
|
}
|
||||||
|
dbg() << "continued";
|
||||||
|
|
||||||
// wait for breakpoint
|
// wait for breakpoint
|
||||||
if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
|
if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
|
||||||
|
@ -156,20 +193,14 @@ int main(int argc, char** argv)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("eip:0x%x\n", regs.eip);
|
dbg() << "eip after breakpoint: " << (void*)regs.eip;
|
||||||
|
|
||||||
uint32_t data = ptrace(PT_PEEK, g_pid, (void*)regs.eip, 0);
|
|
||||||
printf("peeked data: 0x%x\n", data);
|
|
||||||
|
|
||||||
if (ptrace(PT_POKE, g_pid, (void*)regs.eip, data) < 0) {
|
|
||||||
perror("poke");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ptrace(PT_CONTINUE, g_pid, 0, 0) == -1) {
|
if (ptrace(PT_CONTINUE, g_pid, 0, 0) == -1) {
|
||||||
perror("continue");
|
perror("continue");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// wait for end
|
||||||
|
|
||||||
if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
|
if (waitpid(g_pid, nullptr, WSTOPPED) != g_pid) {
|
||||||
perror("waitpid");
|
perror("waitpid");
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
int main(int, char**)
|
int main(int, char**)
|
||||||
{
|
{
|
||||||
printf("before breakpoint\n");
|
printf("Debuggee main\n");
|
||||||
asm("int3");
|
int s = 0;
|
||||||
printf("after breakpoint\n");
|
for (int i = 0; i < 10; ++i) {
|
||||||
|
s++;
|
||||||
|
}
|
||||||
|
printf("s: %d\n", s);
|
||||||
|
// asm("int3");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1243,6 +1243,13 @@ int Process::exec(String path, Vector<String> arguments, Vector<String> environm
|
||||||
if (rc < 0)
|
if (rc < 0)
|
||||||
return rc;
|
return rc;
|
||||||
|
|
||||||
|
if (m_wait_for_tracer_at_next_execve) {
|
||||||
|
ASSERT(Thread::current->state() == Thread::State::Skip1SchedulerPass);
|
||||||
|
// State::Skip1SchedulerPass is irrelevant since we block the thread
|
||||||
|
Thread::current->set_state(Thread::State::Running);
|
||||||
|
Thread::current->send_urgent_signal_to_self(SIGSTOP);
|
||||||
|
}
|
||||||
|
|
||||||
if (Process::current == this) {
|
if (Process::current == this) {
|
||||||
Scheduler::yield();
|
Scheduler::yield();
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue