Search code examples
cgdbfopen

Print file contents opened with fopen in GDB?


I am attempting to print the contents of a file opened with fopen in C within GDB.

For example, take the following C code:

#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *file;
    file = fopen("/etc/passwd", "r");
    fclose(file);
    return 0;
}

I would like to be able to view the contents of this file when running the compiled executable in GDB. Thankyou!


Solution

  • I am attempting to print the contents of a file opened with fopen in C within GDB.

    You need to have libc which is compiled with debug info. For example, on Linux, using GLIBC and with libc6-dbg installed, using your example program, modified to actually read the file (merely fopening and immediately fcloseing the file doesn't read anything):

    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        FILE *file;
        file = fopen("/etc/passwd", "r");
        int ch = fgetc(file);              /* force a read */
        fclose(file);
        return 0;
    }
    
    (gdb) start
    Temporary breakpoint 1 at 0x115d: file t.c, line 6.
    Starting program: /tmp/a.out
    
    Temporary breakpoint 1, main () at t.c:6
    6           file = fopen("/etc/passwd", "r");
    (gdb) n
    7           int ch = fgetc(file);
    (gdb) n
    8           fclose(file);
    (gdb) p *file
    $1 = {_flags = -72539000, _IO_read_ptr = 0x555555559481 "oot:x:0:0:root:/root:/bin/bash\n"..., _IO_read_end = 0x55555555a1c6 "", ..., _IO_read_base = 0x555555559480 "root:x:0:0:root:/root:/bin/bash\n"...
    

    Here you can see that the data which will be returned by subsequent reads is pointed to by file->_IO_read_ptr, and the entire buffer is pointed by file->_IO_read_base.

    The members will depend on which libc you use, and the amount of buffered data (if any) will depend on buffering that the stream was opened with.