I was asked how to let a gdb controlled slave read from stdin, and couldn’t remember how it was done, so I wrote the following little test program, figuring that muscle memory would remind me once I had gdb running.
#include <stdio.h> int main() { size_t rc = 0; char buf[2]; do { rc = fread( buf, 1, 1, stdin ); if ( rc == 1 ) { printf( "%c\n", buf[0] ); } } while ( rc ); return 0; }
Turns out the answer is just the gdb run command, which can specify stdin for the program. Here’s a demo session that shows this in action
(gdb) b main Breakpoint 1 at 0x40061e: file x.c, line 5. (gdb) run < x.c Starting program: /home/pjoot/tmp/readit/a.out < x.c Breakpoint 1, main () at x.c:5 5 size_t rc = 0; Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-225.0.4.el8_8.6.x86_64 (gdb) n 9 rc = fread( buf, 1, 1, stdin ); (gdb) n 10 if ( rc == 1 ) { (gdb) p rc $1 = 1 (gdb) p buf[0] $2 = 35 '#' (gdb) n 11 printf( "%c\n", buf[0] ); (gdb) # 13 } while ( rc ); (gdb) 9 rc = fread( buf, 1, 1, stdin ); (gdb) 10 if ( rc == 1 ) { (gdb) p rc $3 = 1 (gdb) p buf[0] $4 = 105 'i'
The x.c input that I passed to run, was the source of the program that I debugging.