Search code examples
macossystem-callsmachxnu

Catch system calls on Mac OS X


I'm trying to catch all systems-calls called by a given PID with a self-made program (I cant use any of strace, dtruss, gdb...). So i used the function
kern_return_t task_set_emulation(task_t target_port, vm_address_t routine_entry_pt, int routine_number) declared in /usr/include/mach/task.h .
I've written a little program to catch the syscall write :

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <mach/mach.h>
#include <mach/mach_vm.h>

void do_exit(char *msg)
{ 
  printf("Error::%s\n", msg);
  exit(42);
}

int main(void)
{ 
  mach_port_t the_task;
  mach_vm_address_t address;
  mach_vm_size_t size;
  mach_port_t the_thread;
  kern_return_t kerr;

  //Initialisation
  address = 0;
  size = 1ul * 1024;
  the_task = mach_task_self(); //Get the current program task
  kerr = mach_vm_allocate(the_task, &address, size, VM_MEMORY_MALLOC); //Allocate a new address for the test
  if (kerr != KERN_SUCCESS)
  { do_exit("vm_allocate"); }
  printf("address::%llx, size::%llu\n", address, size); //debug

  //Process
  kerr = task_set_emulation(the_task, address, SYS_write); //About to catch write syscalls
  the_thread = mach_thread_self(); //Verify if a thread is opened (even if it's obvious)
  printf("kerr::%d, thread::%d\n", kerr, the_thread); //debug
  if (kerr != KERN_SUCCESS)
  { do_exit("set_emulation"); }

  //Use some writes for the example
  write(1, "Bonjour\n", 8);
  write(1, "Bonjour\n", 8);
}

The Output is :

address::0x106abe000, size::1024
kerr::46, thread::1295
Error::set_emulation

The kernel error 46 corresponds to the macro KERN_NOT_SUPPORTED described as an "Empty thread activation (No thread linked to it)" in /usr/include/mach/kern_return.h, and happend even before i'm calling write.
My question is: What did I do wrong in this process? Kern_not_supported does mean that it's not implemented yet, instead of a meaningless thread problem?


Solution

  • The source code in XNU for the task_set_emulation is:

    kern_return_t
    task_set_emulation(                                                                                                                                                         
        __unused task_t     task,
        __unused vm_offset_t    routine_entry_pt,
        __unused int        routine_number)
    {
        return KERN_NOT_SUPPORTED;
    }
    

    Which means task_set_emulation is not supported.