Getrusage 内联汇编

Getrusage inline assembly

我正在尝试使用套接字将 getrusage 功能实现到我的客户端服务器程序中,所有这些都在 FreeBSD 上 运行ning。我想打印出处理器时间使用情况和内存使用情况。

我已尝试执行以下代码,但得到的输出是 Illegal instrucion (Core dumped)

int getrusage(int who, struct rusage *usage){

    int errorcode;
    __asm__(
    "syscall"
    : "=a" (errorcode)
    : "a" (117), "D"  (who), "S" (usage)  //const Sysgetrusage : scno = 117
    : "memory"
    );

    if (errorcode<0) {
        printf("error");
    }
    return 1;  
}

更新: 我已经尝试 运行 这个但是我得到零值或一些随机数值或负数值。知道我错过了什么吗?

int getrusage(int who, struct rusage *usage){

    int errorcode;
    __asm__("push [=11=];"
                "push %2;"
                "push %1;"
                "movl 7, %%eax;"
                "int [=11=]x80;"
                :"=r"(errorcode)
                :"D"(who),"S"(usage)
                :"%eax"
        );

    if (errorcode<0) {
        printf("error");
    }
    return 1;

}

我更愿意使用系统调用write,但它给了我一个编译warning: passing arg 1 of 'strlen' makes pointer from integer without a cast

编辑:(这是现在的工作代码,关于评论)

struct rusage usage;
getrusage(RUSAGE_SELF,&usage);
char tmp[300];

write(i, "Memory: ", 7);
sprintf (tmp, "%ld", usage.ru_maxrss);
write(i, tmp, strlen(tmp));
write(i, "Time: ", 5);
sprintf (tmp, "%lds", usage.ru_utime.tv_sec);
write(i, tmp, strlen(tmp));
sprintf (tmp, "%ldms", usage.ru_utime.tv_usec);
write(i, tmp, strlen(tmp));

知道哪里出了问题吗?

您收到 illegal instruction 错误的原因是因为 SYSCALL 指令仅适用于 64 位 FreeBSD 运行 64 位程序。这是一个严重的问题,因为您的一条评论表明您的代码在 32 位 FreeBSD 上是 运行。

在正常情况下,您不需要编写自己的 getrusage,因为它是该平台上 C 库 (libc) 的一部分。看来您的任务是使用内联汇编来完成它。


64 位 FreeBSD 和 SYSCALL 指令

由于 SYSCALL 破坏了 RCX 和 [=72= 的内容,因此您的 64 位代码中存在一些错误]R11。您的代码可能会工作,但将来可能会失败,尤其是当程序扩展并且您启用优化时。以下更改将这 2 个寄存器添加到破坏列表中:

int errorcode;
    __asm__(
        "syscall"
        : "=a" (errorcode)
        : "a" (117), "D"  (who), "S" (usage)  //const Sysgetrusage : scno = 117
        : "memory", "rcx", "r11"
);

使用 memory 破坏器会导致生成低效代码,所以我只在必要时才使用它。随着您成为专家,可以消除对 memory 破坏的需要。如果不允许我使用 getrusage:

C 库版本,我会使用如下函数
int getrusage(int who, struct rusage *usage){

    int errorcode;
    __asm__(
        "syscall"
        : "=a"(errorcode), "=m"(*usage)
        : "0"(117), "D"(who), "S"(usage)
        : "rcx", "r11"
    );

    if (errorcode<0) {
        printf("error");
    }
    return errorcode;
}

这使用内存操作数作为输出约束并删除 memory 破坏。由于编译器知道 rusage 结构有多大并且 =m 表示输出约束修改了内存,我们不需要 memory 破坏器。


通过 Int 0x80 的 32 位 FreeBSD 系统调用

正如评论和更新代码中提到的,要在 FreeBSD 中使用 32 位代码进行系统调用,您必须使用 int 0x80。这在 FreeBSD System Calls Convention 中有描述。参数从右到左压入堆栈,在压入最后一个参数后,您必须通过将任何 4 字节值压入堆栈来在堆栈上分配 4 个字节。

您编辑的代码有一些错误。首先,将额外的 4 个字节压入其余参数之前。你需要推动它。您需要在 int 0x80 之后调整堆栈,以有效回收传递的参数使用的堆栈 space。您将三个 4 字节的值压入堆栈,因此需要在 int 0x80.

之后向 ESP 添加 12

您还需要一个 memory 破坏,因为编译器根本不知道您实际上修改了内存。这是因为您完成约束的方式会修改变量 usage 中的数据,但编译器不知道是什么。

int 0x80 的 return 值将在 EAX 中,但您使用约束 =r。它应该是 =a,因为 return 值将在 EAX 中被 returned。由于使用 =a 告诉编译器 EAX 已被破坏,因此您无需再将其列为破坏者。

修改后的代码可能看起来像:

int getrusage(int who, struct rusage *usage){

    int errorcode;
    __asm__("push %2;"
            "push %1;"
            "push [=12=];"
            "movl 7, %%eax;"
            "int [=12=]x80;"
            "add , %%esp"
            :"=a"(errorcode)
            :"D"(who),"S"(usage)
            :"memory"
            );

    if (errorcode<0) {
        printf("error");
    }
    return errorcode;
}

另一种可以用更高级的技术编写的方法是:

int getrusage(int who, struct rusage *usage){

    int errorcode;
    __asm__("push %[usage]\n\t"
            "push %[who]\n\t"
            "push %%eax\n\t"
            "int [=13=]x80\n\t"
            "add , %%esp"
            :"=a"(errorcode), "=m"(*usage)
            :"0"(117), [who]"ri"(who), [usage]"r"(usage)
            :"cc" /* Don't need this with x86 inline asm but use for clarity */
        );

    if (errorcode<0) {
        printf("error");
    }
    return errorcode;
}

这使用标签(usagewho)来标识每个参数,而不是使用 %3%4 等数字位置。这使得内联汇编更容易理解。由于任何 4 字节的值都可以在 int 0x80 之前被压入堆栈,我们可以通过简单地压入任何寄存器的内容来节省几个字节。在这种情况下,我使用了 %%eax。这使用 =m 约束,就像我在 64 位示例中所做的那样。

有关扩展内联汇编程序的更多信息,请参见 GCC documentation