Thread: trying to understand a program via GDB

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    144

    trying to understand a program via GDB

    I'm working through C programming: Modern Approach by K.N. King

    justify is a program that can correctly format (spacing) any sort of text. So if you write something like "hello space space space space space world" it will output "hello world". (cannot put multiple spaces as this forum is fixing it )

    The program contains three .c files:
    justify.c
    line.c
    word.c

    In order to run this program, the following is done
    ./justify <quote

    Quote is a text file containing sentences that are not formatted correctly. When you redirect the quote file into the justify program, the output is formatted with correct spacing.

    I want to analyse the program via GDB but I'm having difficulties bringing the file quote into gdb.

    What I tried:
    Code:
    gcc -g -o justify.c line.c word.c justify
    gdb ./justify
    run < quote
    after I type run < quote, program doesn't actually run, I just see this:

    (gdb) run < quote
    The program being debugged has been started already.
    Start it from the beginning? (y or n) y
    Starting program: /Users/prakagan/c_king/writing_large_programs/justify < quote
    [New Thread 0x2303 of process 17155]
    warning: unhandled dyld version (15)
    Last edited by bos1234; 07-30-2020 at 12:09 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > In order to run this program, the following is done
    > ./justify <quote
    TBH, I would add a second option to justify, namely to open the file supplied as a command line argument.

    As in
    ./justify quote

    Code:
    int main ( int argc, char *argv[] ) {
      FILE *in = stdin;  // by default
      if ( argc > 1 ) {
        in = fopen(argv[1],"r");
      }
    
      // now you read from stdin or a file
      char buff[BUFSIZ];
      while ( fgets( buff, BUFSIZ, in ) ) {
      }
    }
    To do what you want, you would have to keep editing your quote file to intermix all the gdb commands with all the actual program input.

    As soon as you hit a breakpoint, gdb is just going to try to interpret the rest of the input file as gdb commands, and that just isn't going to work.

    It's fine to share stdin between the program and gdb in an interactive session, because you know at each keystroke whether you're talking to the program or gdb.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    No problems here:
    Code:
    $ cc -g -o test test.c
    $ echo 'test' > input
    $ gdb test
    Reading symbols from test...done.
    (gdb) r < input
    Starting program ./test < input
    test
    [Inferior 1 (process 8995) exited normally]
    Test program:
    Code:
    #include <stdio.h>
    
    int main( void )
    {
      char buffer[128];
    
      scanf( "%s", buffer );
      puts( buffer );
    }
    Last edited by flp1969; 07-30-2020 at 08:25 AM.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    thanks. Seems like it doesn't work on mac high sierra. Works on ubuntu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need to understand this program.
    By md_nayeem in forum C Programming
    Replies: 1
    Last Post: 12-24-2017, 08:21 AM
  2. Help me understand the program!
    By Dmy in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2017, 01:57 PM
  3. I can't understand this program
    By tzungshianlin in forum C Programming
    Replies: 2
    Last Post: 12-11-2014, 11:30 PM
  4. Can you help me understand this program?
    By SCRIPT_KITTEH in forum C Programming
    Replies: 7
    Last Post: 07-23-2013, 04:03 AM
  5. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM

Tags for this Thread