Debugging a command line tool

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

  1. wlh99 macrumors 6502

    Feb 7, 2008
    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. Hansr macrumors 6502a

    Apr 1, 2007
    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. willieva macrumors 6502

    Mar 12, 2010
    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