Debugging a command line tool

Discussion in 'Mac Programming' started by wlh99, Sep 20, 2010.

  1. macrumors 6502

    Joined:
    Feb 7, 2008
    #1
    I have written a command line tool that takes input from STDIN, does stuff with it, and outputs it to STDOUT. It is run from the shell like:

    $ mytool < inputfile.txt > outputfile.txt

    Is there a way to run it like that in Xcode / instruments? The tool ussually works fine but with some input will crash with a segmentation fault.
     
  2. macrumors 6502a

    Joined:
    Apr 1, 2007
    #2
    You can set the arguments being passed in for the project in Xcode and run it from within Xcode to debug.

    But mainly it's just a point to add some measure to validate that the input is valid otherwise throw an error and notify the use and then exist gracefully or request new input.
     
  3. macrumors 6502

    Joined:
    Mar 12, 2010
    #3
    If you're running on the command line then you can use gdb to debug it. Recompile with the -g flag and if a core file is generated when it seg faults then you can run gdb on the core file, or just run gdb on the executable.

    But before you go learn a debugger, learn to use output judiciously. A few well chosen prints with the right info is extremely helpful when debugging. And make sure whatever print statement you're using actually gets written in case of a seg fault. Some print statements are buffered and won't get written.

    BTW, I'm assuming you're compiling with gcc or g++. That sort of info is helpful when asking computer questions.
     

Share This Page