PDA

View Full Version : Debugging a command line tool




wlh99
Sep 20, 2010, 03:49 PM
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.



Hansr
Sep 20, 2010, 04:33 PM
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.

willieva
Sep 20, 2010, 08:00 PM
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.