linuxubuntudebugginggdbstdin

Why my gdb can't read the file by stdin? (very weird)


This is t.c, I use gcc t.c -o t -m32 -g to compile it:

#include <stdio.h>
int main() {
    char buffer[100];
    printf("Please enter a string: ");
    scanf("%99s", buffer);
    printf("You entered: %s\n", buffer);
    return 0;
}

This is t.txt: 123

It can work well when I use pipeline or stdin in terminal:

muyigin@ysyx:~/Desktop$ cat t.txt | ./t
Please enter a string: You entered: 123
============================================
muyigin@ysyx:~/Desktop$ ./t < t.txt
Please enter a string: You entered: 123

BUT! It does't work in GDB, which seems it "can" work:

(gdb) file t
(gdb) run < t.txt
Starting program: /home/muyigin/Desktop/t1 < t.txt
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Please enter a string:

I have read many many answers since 2011, here are what I've tried:

(gdb) run < t.txt
(gdb) r < t.txt
(gdb) r < $(cat t.txt)
(gdb) r < /home/muyigin/Desktop/t.txt
(gdb) r < ~/Desktop/t.txt
(gdb) start < t.txt

And I've tried use breakpoint.

I use ubuntu22.04 in VMware 17 pro, gdb version is GNU gdb (Ubuntu 12.1-0ubuntu1~22.04.2) 12.1


Solution

  • This is most likely caused by something in your ~/.gdbinit.

    Try using gdb -nx ./t.