在 C 中的 darwin 上,task_for_pid 总是返回(os/kern)失败

发布于 2024-11-19 11:21:37 字数 911 浏览 2 评论 0原文

由于某些原因,我无法从 task_for_pid() 中得到任何结果 我找不到太多信息,但我试图将其附加到另一个进程并搜索其内存,但每次我尝试使用 task_for_pid 时,都会得到相同的 (os/kern) 失败错误。

#include <stdio.h>
#include <mach/mach_traps.h>
#include <mach/mach_init.h>

int main(int argc, char* argv[])
{
mach_port_name_t task;
printf("%d\n", argv[1]);
int pid = atoi(argv[1]);
printf("%d\n%d\n", pid, current_task());
int error = task_for_pid(2055, 24269, &task);
printf("%x\n", task);
if (error)
{
printf("task_for_pid return error:\n %s\n", mach_error_string(error));
} else {
printf("Get the process %d's task port : %x\n", pid, task);
}
return 0;
}

输出看起来像:

gcc -o test test.c;./test 24269
803206115
24269
2055
0
task_for_pid return error:
 (os/kern) failure

有什么想法可以解释为什么我从来没有得到过任务吗? 我以 root 身份运行它。

正如 Adam Rosenfield 所说,它确实在标头中指出它已过时,但如果这是真的,我仍然可以使用旧版本的工具链来编译和运行它吗?或者它被什么取代了?有人知道吗?

For some reasons I cannot get anything to come out of task_for_pid()
I can't find very much information but what I am trying to do it attach to another process and search its memory, but every time I try to use task_for_pid, I get the same (os/kern) failure error.

#include <stdio.h>
#include <mach/mach_traps.h>
#include <mach/mach_init.h>

int main(int argc, char* argv[])
{
mach_port_name_t task;
printf("%d\n", argv[1]);
int pid = atoi(argv[1]);
printf("%d\n%d\n", pid, current_task());
int error = task_for_pid(2055, 24269, &task);
printf("%x\n", task);
if (error)
{
printf("task_for_pid return error:\n %s\n", mach_error_string(error));
} else {
printf("Get the process %d's task port : %x\n", pid, task);
}
return 0;
}

Output looks like:

gcc -o test test.c;./test 24269
803206115
24269
2055
0
task_for_pid return error:
 (os/kern) failure

Any idea's as to why im not getting a task, ever?
I am running it as root.

As Adam Rosenfield said, it does say in the header that it is obsolete, but if thats true, could I still compile and run it with an older version of the toolchain? or what has it been replaced with? does anyone know?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满栀 2024-11-26 11:21:38
  1. 您确定您正在以 root 身份运行吗?
  2. 您确定进程 24269 仍在运行吗?

我在 Mac OS X 10.6.8 上使用任何正在运行的进程运行此代码(使用 sudo)都没有问题:

#include <stdio.h>
#include <stdlib.h>
#include <mach/mach_traps.h>
#include <mach/mach_init.h>
#include <mach/mach_error.h>

int main(int argc, char* argv[])
{
    task_t task;
    pid_t pid = argc >= 2 ? atoi(argv[1]) : 1;
    kern_return_t error = task_for_pid(current_task(), pid, &task);
    printf("%d -> %x [%d - %s]\n", pid, task, error, mach_error_string(error));
    return error;
}

例如,这是我的 pid 182 (Dock) 结果

$ sudo ./task_for_pid 182
182 -> 413 [0 - (os/kern) successful]
  1. Are you sure you are running as root?
  2. Are you sure the process 24269 is still running?

I have no problem running this code (with sudo) on Mac OS X 10.6.8 with any running process:

#include <stdio.h>
#include <stdlib.h>
#include <mach/mach_traps.h>
#include <mach/mach_init.h>
#include <mach/mach_error.h>

int main(int argc, char* argv[])
{
    task_t task;
    pid_t pid = argc >= 2 ? atoi(argv[1]) : 1;
    kern_return_t error = task_for_pid(current_task(), pid, &task);
    printf("%d -> %x [%d - %s]\n", pid, task, error, mach_error_string(error));
    return error;
}

For example, here is my result with pid 182 (Dock)

$ sudo ./task_for_pid 182
182 -> 413 [0 - (os/kern) successful]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文