GDB printf调试时的奇怪输出 [英] GDB printf strange output when debugging

查看:192
本文介绍了GDB printf调试时的奇怪输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在退出该行时未显示printf的输出?但是在某些时候它确实打印了第16行。

Why is the output of the printf is not show when stepping out the line? But at some point it did print line 16.

c文件:

#include<stdio.h>

void nextfunc(){
    int ctr;    
    for(ctr = 0; ctr<3; ctr++){
        printf("print ctr = %d",ctr);
    }
    printf("last print");
}
void main(){

    int x;
    printf("input x: ");
    scanf("%d",&x);
    printf("\nprint 2");
    printf("\nprint 3");
    nextfunc();
}

GDB:

    (gdb) break main
    Breakpoint 1 at 0x8048479: file file5.c, line 14.
    (gdb) break nextfunc
    Breakpoint 2 at 0x804843a: file file5.c, line 6.
    (gdb) run
    Starting program: /home/charmae/workspace/AVT/file5 

    Breakpoint 1, main () at file5.c:14
    14      printf("input x: ");
    (gdb) s
    15      scanf("%d",&x);
    (gdb) s
    input x: 4
    16      printf("\nprint 2");
    (gdb) s

    17      printf("\nprint 3");
    (gdb) s
    print 2
    18      nextfunc();
    (gdb) s

    Breakpoint 2, nextfunc () at file5.c:6
    6       for(ctr = 0; ctr<3; ctr++){
    (gdb) s
    7           printf("print ctr = %d",ctr);
    (gdb) s
    6       for(ctr = 0; ctr<3; ctr++){
    (gdb) s
    7           printf("print ctr = %d",ctr);
    (gdb) s
    6       for(ctr = 0; ctr<3; ctr++){
    (gdb) s
    7           printf("print ctr = %d",ctr);
    (gdb) s
    6       for(ctr = 0; ctr<3; ctr++){
    (gdb) s
    9       printf("last print");
    (gdb) s
    10  }
    (gdb) s
    main () at file5.c:19
    19  }
    (gdb) s
    0x0014a113 in __libc_start_main () from /lib/i386-linux-gnu/libc.so.6
    (gdb) s
    Single stepping until exit from function __libc_start_main,
    which has no line number information.
    print 3print ctr = 0print ctr = 1print ctr = 2last print[Inferior 1 (process 2578) exited with code 012]


推荐答案

尽管缓冲了 stdout 的输出。这意味着它将保存在临时缓冲区中,直到缓冲区已满,正在打印换行符或调用 fflush(stdout)函数。 stdout 也会在程序结束时自动刷新。

Output though stdout is buffered. That means it is saved in a temporary buffer either until the buffer is full, there is a newline being printed or the function fflush(stdout) is called. stdout is flushed automatically also when the program ends.

输出错误位置的原因在GDB中的原因就是这种缓冲。您应该在 printf 格式字符串的末尾添加换行符,或显式调用 fflush

The reason your output is printed "on the wrong place" in GDB is because of this buffering. you should either add newlines to the end of the printf format string, or explicitly call fflush.

这篇关于GDB printf调试时的奇怪输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆