Monday, May 9, 2016

Using DBX Debugger on AIX -- How to pass program arguments to DBX

DBX is the default debugger in IBM AIX OS. This debugger's user interface is rather unusual compared to other debugger. It is a command line debugger just like GNU GDB. But, it has a different philosophy. The user guide for DBX is at: https://www.ibm.com/support/knowledgecenter/ssw_aix_61/com.ibm.aix.cmds2/dbx.htm. The user guide is exhaustive to be read all at once. I recommend you to focus on your goal, i.e. debugging requirements and read the user guide to suite the requirements.

Let's start with a basic requirements:

  • You have a command line program 
  • The program has several arguments that must be passed at startup time.
Now, let's look at the steps to fulfill the requirements above. Let's start with DBX philosophy. The DBX philosophy is as follows:

  1. Running dbx without any arguments in the shell only starts the debugging environment, nothing more, nothing less.
  2. Running dbx with only the executable (program) file name will load the executable into memory but doesn't run the program. This step also doesn't pass any argument(s) to the program.  
  3. DBX has the so-called "subcommands" which are "commands" that you can type in the DBX debugging environment to instruct the debugger to do something. Another way to pass subcommand to DBX is via a text file known as "command file". A command file contains subcommand and the parameters/arguments required by the subcommand.

The gist of the philosophy is: useful thing can be done mostly via DBX subcommands. The following diagram illustrates this philosophy.
Figure 1 IBM AIX DBX debugger principle of working
The DBX Command Line Interface (CLI) is similar to GNU GDB. Therefore, I'm not going to explain it here. I'll proceed to DBX Scripting Interface. The scripting interface is invoked via DBX's "-c" flag. This is the excerpt from DBX user guide:
-c CommandFileRuns the dbx subcommands in the file before reading from standard input. The specified file in the $HOME directory is processed first; then the file in the current directory is processed. The command file in the current directory overrides the command file in the $HOME directory. If the specified file does not exist in either the $HOME directory or the current directory, a warning message is displayed. The source subcommand can be used once the dbx program is started.
I'll show you how to use -c flag to pass your program arguments at the start of a DBX debugging session. The first thing to do before we can use -c flag is to prepare the Command File. If you just want to pass your program arguments, then the contents of the command file is simply the run subcommand and your program arguments. Below is an example of a valid Command File. Let's name the command file as my_cmd.
run -a 10.10.10.254 -p 8000 -n 2  
In the command file above, the program (to be debugged) arguments starts at -a, the run statement in the beginning refers to DBX run subcommand. The following diagram shows this:
Figure 2 Using run subcommand in your command file
This is the verbatim explanation for run subcommand  from DBX user guide:
run Subcommand
run [ Arguments ] [ <File ] [ >File ] [ > >File ] [ 2>File ] [ 2> >File ] [ >&File ] [ > >&File ]
The run subcommand starts the object file. The Arguments are passed as command-line arguments.
Flags
ItemDescription
<FileRedirects input so that input is received fromFile.
>FileRedirects output to File.
2>FileRedirects standard error to File.
> >FileAppends redirected output to File.
2> >FileAppends redirected standard error to File.
>&FileRedirects output and standard error to File.
> >&FileAppends output and standard error to File.
Example
To run the application with the arguments blue and 12, enter:
run blue 12

Therefore, to start DBX debugger to use my_cmd command file above, we enter this in the shell:
$ dbx -c my_cmd [your_program_name]
The -c flag instruct dbx to use my_cmd as the command file, after that you just need to enter your program executable name. Anyway, after dbx parses the command file, it runs your program as if you type run subcommand in a DBX debugging session.

That's it. I hope this post helps those who just started using DBX in AIX or other AIX-like environment.