【发布时间】:2021-06-17 21:19:49
【问题描述】:
我正在使用 Mach 开发 Debian GNU/Hurd。我被要求编写一个程序,给定一个 PID 和一个地址,在地址上执行 vm_read 并打印结果。
这是我写的代码:
#include <mach_error.h>
#include <mach/mig_errors.h>
#include <mach/thread_status.h>
#include <mach/processor_info.h>
#include <mach/i386/vm_param.h>
#include <stdio.h>
#include <stdlib.h>
#include <hurd.h>
#include <string.h>
int main(int argc, char * argv[]) {
if(argc != 3) {
printf ("Wrong arguments: ./vm_read PID address\n");
exit(1);
}
int res;
mach_port_t target_task = pid2task(atoi(argv[1]));
vm_address_t addr = atoi(argv[2]);
vm_offset_t *data;
mach_msg_type_number_t data_count;
res = vm_read (target_task, addr, sizeof(int), &data, &data_count);
if (res != KERN_SUCCESS) {
printf ("Error reading virtual mem (0x%x), %s \n", res,
mach_error_string(res));
exit(1);
}
printf("done\n");
for (int i=0; i<data_count; ++i){
printf("byte %d : %x\n",i,((char*)data)[i]);
}
}
它可以正常工作,但现在我被问到是否可以为 Unix/Linux 编写一个版本,为 Windows 编写另一个版本来做同样的事情。
我一直在搜索,看起来应该没有问题,因为两者都在其进程中使用虚拟内存,但我不确定权限或其他方面是否会出现并发症。
【问题讨论】:
标签: c operating-system debian virtual-memory mach