libdtrace buffered output

╄→尐↘猪︶ㄣ 提交于 2019-12-05 18:17:34

Buffered output seems to be broken on OSX. The actions seem to be somehow executed in the consumer's context in some twisted way. ustack() doesn't work at all, for example. copyinstr() on the other hand seems to function properly.

You can circumvent output buffering yet still get mostly the same result by using pipe:

int fds [2];

if (pipe (fds) != 0)
    assert (0);

int flags = fcntl (fds [0], F_GETFL, 0);
assert (flags != -1);
fcntl (fds [0], F_SETFL, flags | O_NONBLOCK);

FILE *faux_stdout = fdopen (fds [1], "a");
assert (faux_stdout);

while(dtrace_work(g_dtp, faux_stdout, chew, chewrec, NULL) == DTRACE_WORKSTATUS_OKAY) {
    char buf [1024];
    for (;;) {
        ssize_t num_read = read (fds [0], buf, sizeof (buf));
        if (num_read <= 0)
            break;
        /* process your buffer here */
        fwrite (buf, 1, num_read, stdout);
    }
    dtrace_sleep(g_dtp);
}

Error handling is left as an exercise to the reader.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!