Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug argument based C program with gdb

Tags:

c++

c

gdb

I have c++ program which I run by passing string with it.

g++ -o a main.cpp -lpthread

and execute it with ./a "Good nice"

But how I debug it with gdb? main.cpp calling functions from other files which are included in it.

gdb ./a "Good nice"

takes "--" as files and says no such file!

I want to debug line by line!

like image 737
user123 Avatar asked Aug 17 '13 07:08

user123


People also ask

How to debug a C program using gdb?

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

How do I pass arguments to a GDB program?

You can pass arguments if the program needs some command-line arguments to be passed to it: $ (gdb) run arg1 arg2 Debugging with GDB lets you investigate the core file as well. The core file contains information about when a program crashed.

What is GNU Debugger in C?

GNU or GDB debugger is an application for finding out how your C or C++ program runs or for analyzing the moment the program crashes. You can perform many useful tasks with GDB: run the program, stop the program under specific conditions, analyze the situation, make modifications, and test new changes.

How to debug a program in C language?

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.


2 Answers

Use the --args option of gdb:

gdb --args ./a "Good nice"

Also add the -g option to your compiler call, because otherwise gdb won't be able to connect your executable with your source code:

g++ -g -o a main.cpp -lpthread
like image 127
cmaster - reinstate monica Avatar answered Sep 22 '22 19:09

cmaster - reinstate monica


Use gdb without argument

gdb ./a

Then in gdb, before running the program

set args "Good nice"

And you can see what arguments you set, use

show args

See here for detail.

like image 34
Yu Hao Avatar answered Sep 21 '22 19:09

Yu Hao