Thursday, May 13, 2010

strings - smart and useful

Someone gives you up a binary a.out and says he has added some line like this
printf("Hello Universe");
Now the changes may be much bigger then the example. During your testing you do not get the desired result as was expected after his changes.
How do you confirm that he has not misplaced his binaries and handed you a wrong one?
So, if there is a string involved in the change; then you can use the strings command and confirm it.
$ strings a.out | grep Universe
Hello Universe


Better explanation on why to use strings is given here as http://www.oreillynet.com/pub/a/oreilly/linux/news/linuxnut_0800.html

'''

Sometimes you really want to see inside a binary file. Maybe there isn't a manpage and you're looking for usage information, or perhaps you're looking for information about who wrote a program or what application a file is associated with.

The strings command is perfect for that purpose--it searches through a file looking for sequences of printable character strings and writes them to standard output. You can pipe the output through a pager like more, or if you are looking for particular text, you can pipe the output to the grep command.

'''

Thursday, May 6, 2010

perl trace; a very useful debug utility

I was given a perl script and asked to fix the code where a particular option was not behaving as expected. The code was huge.
What's the best/faster way to locate the problem ?

Obviously I had to use a debugger; just like any other programming language perl too has it's own debbuger.
By giving perl the -d switch at command line will invoke the perl debugger.
The best option to understand the code flow and reach to the problematic option is enable trace while debugging.
perl help describes trace like this :
t [expr] Toggle trace [trace expr]

Once you enable trace; all the statements executed along with their line numbers will be displayed on the screen. It is similar to viewing code flow in a shell script by using a "-x" switch.
It is much better than using step/next not knowing where it is leading to and when it will end. So, get an overview of the code flow with t(trace) and make debugging easier.