如何检查系统调用 terminated/why 是否终止?

How do I check that the sys call terminated/why it terminates?

以下代码的输出只是“hello!”,这令人困惑,因为当我让 qemu 启动 xv6 时,一切都编译得很好,但显然函数 getiocounts 出了点问题。

我是系统调用的新手,所以我可能遗漏了一些明显的东西。请帮忙!谢谢!

#include "types.h"
#include "stat.h"
#include "user.h"
#include "iostat.h"

int main(int argc, char** argv) {
    struct iostat* io;
    io =(struct iostat* ) malloc(sizeof(int)*2);
    int r=0;
    printf(1, "hello!");
    printf(1, "%d", getiocounts(io));
    r = io->readcount;
    printf(1, "hello!");
    printf(1, "%d", r);
    exit();
}
int sys_getiocounts( struct iostat* io){

    if(argptr(1, (void*)&io, sizeof(*io)) < 0)
        return -1;
    io->readcount = myproc()->r;
    io->writecount = myproc()->w;
    return 0;
}

您的内核代码中存在一些错误:

  • 你的系统调用定义不正确:它的参数原型应该是(void)
  • 你的参数读取错误:必须读取第一个参数:argptr( 0, ...
int sys_getiocounts( void ){

    struct iostat* io;

    if(argptr(0, (void*) &io, sizeof *io) < 0)
        return -1;

    io->readcount = myproc()->r;
    io->writecount = myproc()->w;
    return 0;
}

并且您的用户代码在分配上可以更清晰:

struct iostat* io = malloc(sizeof *io );
....
getiocounts(io);

甚至没有分配:

struct iostat io;
...
getiocounts(&io);