我是在C代码中使用管道的新手,所以我有一些令人困惑的情况需要您的帮助,

我需要使用opt -print-callgraph foo.bc输出函数调用图。

默认情况下,调用图将位于stderr中,并且我可以使用opt -print-callgraph foo.bc 2>call_graph.txt将stderr重定向到文件。

我需要使用一个c程序,给foo.bc作为参数并生成call_graph.txt

假设已编译的二进制文件名为prog,则在运行./prog foo.bc之后,我可以正确获取call_graph.txt。

但是我的问题是,如果我将prog的标准输出重定向到文件,
prog foo.bc > output.txt,output.txt包含很多乱码。 output.txt可能是一个二进制文件。

我不知道代码中的管道是什么,或者我应该在哪里清理标准输出。

这是我的代码:

int main(int argc, char *argv[]){

    /* pipe to connect opt's stderr and our stdin */
    int pipe_callgraph[2];

    /* pid of opt */
    int opt_pid;
    char *bc_file;

    /* create pipe and check if pipe succeeded */
    if (pipe(pipe_callgraph) < 0) {
        perror("pipe");
        return 1;
    }

    /* create child process */
    opt_pid = fork();
    if (!opt_pid) { /* child process, to spawn opt */

        /* close the read end, since opt only write */
        close(pipe_callgraph[PIPE_READ]);

        /* bind pipe to stderr, and check */
        if (dup2(pipe_callgraph[PIPE_WRITE], STDERR_FILENO) < 0) {
            perror("dup2 pipe_callgraph");
            return 1;
        }

        /* print something to stderr */
        fprintf(stderr, "This is child, just before spawning opt with %s.\n", bc_file);

        /* spawn opt */
        if (execl("/usr/local/bin/opt", "opt", "-print-callgraph", bc_file, (char *)NULL) < 0) {
            perror("execl opt");
            return 1;
        }

        /* unreachable code */
        return 0;
    }

    /* parent process */

    /* close the write end, since we only read */
    close(pipe_callgraph[PIPE_WRITE]);

    /* since we don't need stdin, we simply replace stdin with the pipe */
    if (dup2(pipe_callgraph[PIPE_READ], STDIN_FILENO) < 0) {
        perror("dup2 pipe_callgraph");
        return 1;
    }

    FILE *fp = fopen("call_graph.txt", "w");
    char c = '\0';
    while (scanf("%c", &c) >= 1) {
        fprintf(fp, "%c", c);
    }
    fclose(fp);


和output.txt像这样:



=============编辑===============

如果我更改这些代码

FILE *fp = fopen("call_graph.txt", "w");
char c = '\0';
while (scanf("%c", &c) >= 1) {
    fprintf(fp, "%c", c);
}
fclose(fp);




FILE *fp = fopen("call_graph.txt", "w");
fclose(fp);


output.txt仍然包含乱码

如果我注释掉这两行,则不会出现乱码。

以及是否像这样更改:

fflush(stdout);
FILE *fp = fopen("call_graph.txt", "w");
fclose(fp);


没有乱码。

因此,我以这种方式冲洗了stdout多次:

fflush(stdout);
FILE *fp = fopen("call_graph.txt", "w");
char c = getchar();
while (c >= 1) {
    fflush(stdout);
    fprintf(fp, "%c", c);
    fflush(stdout);
    c = getchar();
}
fflush(stdout);
fclose(fp);


在output.txt中仍然有乱码

最佳答案

我发现了问题,这是因为opt -print-callgraph foo.bc将调用图打印到stderr,而foo.bc的内容打印到stdout,因此我需要使用>/dev/null捕获stdout。

乱码与管道无关。

09-04 16:57
查看更多