Problem with gdb with stdin redirection

I am writing a program for implementing the Dinic max-flow algorithm over a network. Networks can be written either manually or loaded from a file using stdin redirection. I was able to use gdb to debug a program with small files (about 30 lines), but I have problems trying to debug a program with large files (> 1000 lines). The code itself is as follows:

uint32_t read_lines = 0; while(!feof(stdin)) { err = fscanf(stdin, "%u %u %u\n", &n1, &n2, &c); if (err != 3) { printf("read_lines=%u\n", read_lines); /*for debugging purposes*/ } read_lines += 1; /* write to debug file */ fprintf(debug, "line %u: %u %u %u\n", read_lines, n1, n2, c); } 

If I run the program without gdb, it starts, not normal, because it generates a segmentation error (for this reason I try to use gdb), but it goes through this part of the "parsing" of the input file (and write it to the debug output file) . However, if I type:

 gdb --args ./dinic --mode=NUM --verbose=LOW (gdb) b 61 (gdb) run < tests/numterc.in 

I get:

 (gdb) Program exited with 01 code. 

and when I open the debug file about 2000 lines, when it should be no more than 1000, this is the length of the input file.

I repeat, this happens with "large" files, it works correctly with small ones. The question will be, am I missing something when using gdb, or is it a gdb error?

+6
c redirect stdin gdb
source share
2 answers

Well, I could finally find a job. The -args option doesn't seem to work well, at least in my case. I have gdb 6.8-debian and debian 5.0.4.

I needed to run gdb without the -args option:

 $gdb ./dinic (gdb) b 61 (gdb) run --mode=NUM --verbose=LOW < tests/numterc.in 

and it worked well. Maybe someone might find this helpful.

+7
source share

I had the same problem and came up with the same solution to point args to run. The --args option can only pass arguments, but cannot redirect stdin , which is usually (in a context without debugging) redirected for you by the shell invoking the command. In a debugging session, your command is called by gdb , where both argument lists and redirects are set by the value of the args variable. Using the --args , you initialize this variable (and debug the program file as well). Just do

 (gdb) show args 

and this should be initialized to --mode=NUM --verbose=LOW in your case. But not a redirect, so you specify them with run, which overrides args! So you have two options:

  • Also specify the redirection in args:

     (gdb) set args --mode=NUM --verbose=LOW < tests/numterc.in 
  • Also specify the redirection when calling run

+2
source share

All Articles