在 Mac OS X 上捕获系统调用
Catch system calls on Mac OS X
我正在尝试使用自制程序捕获给定 PID 调用的所有系统调用(我不能使用 strace、dtruss、gdb 中的任何一个...)。所以我使用了函数
kern_return_t task_set_emulation(task_t target_port, vm_address_t routine_entry_pt, int routine_number)
在 /usr/include/mach/task.h
中声明。
我写了一个小程序来捕捉系统调用 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);
}
输出是:
address::0x106abe000, size::1024
kerr::46, thread::1295
Error::set_emulation
内核错误 46 对应于在 /usr/include/mach/kern_return.h
中描述为 "Empty thread activation (No thread linked to it)" 的宏 KERN_NOT_SUPPORTED
,甚至在我调用 write
.[=26 之前就发生了=]
我的问题是:我在这个过程中做错了什么? Kern_not_supported确实是还没实现,而不是无意义的线程问题?
XNU 中 task_set_emulation
的源代码是:
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;
}
表示不支持task_set_emulation
。
我正在尝试使用自制程序捕获给定 PID 调用的所有系统调用(我不能使用 strace、dtruss、gdb 中的任何一个...)。所以我使用了函数
kern_return_t task_set_emulation(task_t target_port, vm_address_t routine_entry_pt, int routine_number)
在 /usr/include/mach/task.h
中声明。
我写了一个小程序来捕捉系统调用 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);
}
输出是:
address::0x106abe000, size::1024
kerr::46, thread::1295
Error::set_emulation
内核错误 46 对应于在 /usr/include/mach/kern_return.h
中描述为 "Empty thread activation (No thread linked to it)" 的宏 KERN_NOT_SUPPORTED
,甚至在我调用 write
.[=26 之前就发生了=]
我的问题是:我在这个过程中做错了什么? Kern_not_supported确实是还没实现,而不是无意义的线程问题?
XNU 中 task_set_emulation
的源代码是:
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;
}
表示不支持task_set_emulation
。