Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gdb trouble with stdin redirection

I'm writing a program to implement Dinic's max-flow algorithm over a network. The networks can be written either by hand or loaded from a file using stdin redirection. I've been able to use gdb to debug the program with small files (around 30 lines), but I'm having trouble when I try to debug the program with bigger files (>1000 lines). The code itself is this:

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 runs, not ok as it generates a segmentation fault (which is the reason I'm trying to use gdb), but it goes through this part of "parsing" the input file (and writing it into the output debugging 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 debugging file it's about 2000 lines, when it should be at most 1000, which is the input file length.

I repeat, this happens with "big" files, it works correct with small ones. The question would be, am I missing something when using gdb, or is this a gdb bug?

like image 855
Leandro Demarco Avatar asked Jun 01 '10 21:06

Leandro Demarco


2 Answers

Ok, I could finally get a work-around. It seems that the --args option ain't working well, at least in my case. I have gdb 6.8-debian and debian 5.0.4.

What I had to do was 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 can find this useful.

like image 112
Leandro Demarco Avatar answered Sep 18 '22 14:09

Leandro Demarco


I had the same problem and came up with the same solution to specify args in run. The option --args only can pass arguments, but but cannot do redirection of stdin which is usually (in non-debug context) redirected for you by the shell invoking the command. In the debug session your command is invoked by gdb where both argument list and redirections are specified by the value of the args variable. By using the --args option you initialize this variable (and the program file to debug as well). Just do

(gdb) show args

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

  1. Specify also the redirection in args:

    (gdb) set args --mode=NUM --verbose=LOW < tests/numterc.in
    
  2. Specify also the redirection when invoking run

like image 45
King Thrushbeard Avatar answered Sep 21 '22 14:09

King Thrushbeard