Skip to content

Commit

Permalink
Basic handling of events + exit
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonKagstrom committed Aug 10, 2023
1 parent 97e615d commit e787a1a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ else (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(LIBELF_FOUND 1)

set (MACHO_SRCS
engines/lldb-engine.cc
# engines/lldb-engine.cc
parsers/macho-parser.cc
engines/mach-engine.cc
engines/osx/mach_excServer.c
Expand Down
60 changes: 41 additions & 19 deletions src/engines/mach-engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ arch_clearBreakpoint(unsigned long addr, unsigned long old_data, unsigned long c

} // namespace


static void onSigchld(int sig);

class MachEngine : public IEngine
{
public:
Expand All @@ -100,6 +103,12 @@ class MachEngine : public IEngine
}


void childExit()
{
printf("Child has exited!\n");
mach_port_destroy(mach_task_self(), target_exception_port);
}

// Callback from the mig stuff
kern_return_t CatchMachExceptionRaise(mach_port_t exception_port,
mach_port_t thread_port,
Expand All @@ -108,7 +117,11 @@ class MachEngine : public IEngine
mach_exception_data_t codes,
mach_msg_type_number_t num_codes)
{
printf("Exc raise: %d, 0x%llx:0x%llx:0x%llx\n", exception_type, codes[0], codes[1], codes[2]);
printf("Exc raise: %d, codes(%d) 0x%llx:0x%llx\n",
exception_type,
num_codes,
codes[0],
codes[1]);
/* exception_type is defined in exception_types.h */

/* an exception may include a code and a sub-code. num_codes specifies */
Expand All @@ -121,8 +134,8 @@ class MachEngine : public IEngine
/* handling UNIX soft signal: */
/* this example clears SIGSTOP before resuming the process. */

// if (codes[2] == SIGSTOP)
// codes[2] = 0;
// if (codes[2] == SIGSTOP)
// codes[2] = 0;


x86_thread_state64_t state;
Expand Down Expand Up @@ -157,6 +170,7 @@ class MachEngine : public IEngine
#endif

printf("Breakpoint got at 0x%llx\n", state.__rip);
m_listener->onEvent(IEngine::Event(ev_breakpoint, -1, state.__rip));

if (m_instructionMap.find(state.__rip) != m_instructionMap.end())
{
Expand Down Expand Up @@ -252,6 +266,7 @@ class MachEngine : public IEngine
error("posix_spawnattr_init");
return false;
}

rv = posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED | 0x100);
if (rv != 0)
{
Expand All @@ -267,6 +282,8 @@ class MachEngine : public IEngine
return false;
}

posix_spawnattr_destroy(&attr);

auto kret = task_for_pid(mach_task_self(), m_pid, &m_task);
if (kret != KERN_SUCCESS)
{
Expand Down Expand Up @@ -310,7 +327,7 @@ class MachEngine : public IEngine
/* register the exception port with the target process */

rv = task_set_exception_ports(m_task,
EXC_MASK_ALL,
EXC_MASK_ALL & ~EXC_MASK_RESOURCE,
target_exception_port,
EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES,
THREAD_STATE_NONE);
Expand All @@ -319,6 +336,9 @@ class MachEngine : public IEngine
error("task_set_exception_ports: %d\n", rv);
return false;
}

signal(SIGCHLD, onSigchld);

rv = ptrace(PT_ATTACHEXC, m_pid, 0, 0);
if (rv != 0)
{
Expand All @@ -334,17 +354,15 @@ class MachEngine : public IEngine
{
setupAllBreakpoints();

auto rv = ptrace(PT_CONTINUE, m_pid, 0, 0);
printf("CE pt rv %d\n", rv);
// ::kill(m_pid, SIGCONT);
int status = 0;
rv = waitpid(-1, &status, WNOHANG);
if (rv == -1)
{
error("waitpid");
return false;
}
printf("waitpid: %d, 0x%x %d (pid is %d)\n", rv, status, WIFEXITED(status), m_pid);
int rv;

// task_resume(m_task);
rv = ptrace(PT_ATTACHEXC, m_pid, 0, 0);
// rv = ptrace(PT_CONTINUE, m_pid, 0, 0);
//printf("CE pt rv %d\n", rv);
// ::kill(m_pid, SIGCONT);

// printf("waitpid: %d, 0x%x %d (pid is %d)\n", rv, status, WIFEXITED(status), m_pid);


/* wait indefinitely to receive an exception message */
Expand All @@ -359,11 +377,9 @@ class MachEngine : public IEngine
MACH_MSG_TIMEOUT_NONE, /* wait indefinitely */
MACH_PORT_NULL); /* notify port, unused */

task_suspend(m_task);
/* resume all threads in the process before replying to the exception */

if (krt == KERN_SUCCESS)
{
task_suspend(m_task);

/* mach_exc_server calls catch_mach_exception_raise */

Expand All @@ -379,9 +395,12 @@ class MachEngine : public IEngine
}
else
{
m_listener->onEvent(IEngine::Event(ev_exit, 0));
return false;
printf("Some error %d?\n", krt);
}

/* resume all threads in the process before replying to the exception */
task_resume(m_task);
/* reply to the exception */

Expand Down Expand Up @@ -545,7 +564,6 @@ class MachEngine : public IEngine
}
}


private:
typedef std::unordered_map<unsigned long, unsigned long> InstructionMap_t;

Expand Down Expand Up @@ -652,6 +670,10 @@ catch_mach_exception_raise_state_identity(mach_port_t exception_port,
return MACH_RCV_INVALID_TYPE;
}

void onSigchld(int sig)
{
g_machEngine->childExit();
}

class MachEngineCreator : public IEngineFactory::IEngineCreator
{
Expand Down

0 comments on commit e787a1a

Please sign in to comment.