Hi could someone help me create a debug mode to be run in UNIX with C programmin
ID: 3754260 • Letter: H
Question
Hi could someone help me create a debug mode to be run in UNIX with C programming? It should be with command line arguments, but I don't fully understand how to implement this. Basically we need to use command line arguments in the main c code, so that the user has an option to run debug mode. This means, the user should be able to invoke a debug option within the code via the command line in UNIX. So if the homework name is record (after linking it), if I type in ./test debug, then it should run in debug mode. I think we are supposed to use command-line arguments with something like this:
#include
main (int arg c, char *arg v[])
{
code to check if “debug” was entered. If entered, then output will print something on the screen.
if it is not entered, then error message and try again.
}
This program may be invoked by typing the executable name (for example, ./test) or with the option debug. (e.g. ./test debug). Anything else such as ./test debug bug or ./test hello should give an error. The format of the error must be similar to the one you get when you type cp in UNIX.
A global variable, debugmode must be used to keep track of how the program was invoked.
I hope that makes sense.
I could really use a sample to help me understand this portion, so I can implement it on the larger portion of the assignment.
Thanks in advance!
Explanation / Answer
Let us create the following C program that calculates and prints the factorial of a number. However this C program contains some errors in it for our debugging purpose.
Output:-
Let us debug it while reviewing the most useful commands in gdb.
Step 1. Compile the C program with debugging option -g :- Compile your C program with -g option. This allows the compiler to collect the debugging information.
Note: The above command creates a.out file which will be used for debugging as shown below.
Step 2. Launch gdb - Launch the C debugger (gdb) as shown below:-
Step 3. Set up a break point inside C program
Other formats:
Places break point in the C program, where you suspect errors. While executing the program, the debugger will stop at the break point, and gives you the prompt to debug.
So before starting up the program, let us place the following break point in our program.
Step 4. Execute the C program in gdb debugger -
run [args]
You can start running the program using the run command in the gdb debugger. You can also give command line arguments to the program via run args. The example program we used here does not requires any command line arguments so let us give run, and start the program execution.
Once you executed the C program, it would execute until the first break point, and give you the prompt for debugging.
You can use various gdb commands to debug the C program as explained in the sections below.
Step 5. Printing the variable values inside gdb debugger
As you see above, in the factorial.c, we have not initialized the variable j. So, it gets garbage value resulting in a big numbers as factorial values.
Fix this issue by initializing variable j with 1, compile the C program and execute it again.
Even after this fix there seems to be some problem in the factorial.c program, as it still gives wrong factorial value.
So, place the break point in 10th line, and continue as explained in the next section.
Step 6. Continue, stepping over and in – gdb commands :- There are three kind of gdb operations you can choose when the program stops at a break point. They are continuing until the next break point, stepping in, or stepping over the next program lines.
By continuing or stepping through you could have found that the issue is because we have not used the <= in the ‘for loop’ condition checking. So changing that from < to <= will solve the issue.
Please let me know in case of any clarifications required. Thanks!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.