Linux C (Hello World) program debugging

Source: Internet
Author: User

Debugging of the program (the GDB tool must be installed first to execute the command as root:sudo apt-get install gdb)

The debugging of the program is a very important link, the Windows IDE those powerful debugging features, Linux with what to compare it, of course, in my opinion, the omnipotent Linux can do the debugging work, even more, that is, after installing the development tools, Another tool integrated into Linux, GDB.
It is a program debugging tool under the Unix/linux developed and released by the GNU organization, although it does not have a graphical and friendly interface, but it is exceptionally powerful enough to rival some of the other commercially available IDE environments.
At this point, the debugging of the pair is like an executable file, rather than the ". C" End of the source code file, that is, the source files, need to be compiled after GCC to build the elder brother executable file to use GDB debugging.
The following is a description of its use, as above, we refer to a source program:
[Email protected] programs]$ vim smallest.c

Find the minimal between 2 INT number
#include <stdio.h>
int min (int x, int y);
int main ()
{
int num1,num2,min_num;
printf ("Please Input the first number:\n");
scanf ("%d", &num1);
printf ("Please Input the second number:\n");
scanf ("%d", &num2);
Min_num=min (NUM1,NUM2);
printf ("The Minimal one is%d\n", min_num);
}

int min (int a, int b)
{
if (a<b)
return A;
Else
return b;
}
~

The program has been written on the above, let's look at how to produce a compiled file with debugging information, here we want to use the GCC's-g parameter, to add some debugging information in the compilation file.
[Email protected] programs]$ gcc-g smallest.c-o Smallest
[[email protected] programs]$ ls
Hello_world hello_world.c Smallest smallest.c
From the above we can see that there is a smallest generated, if you want to verify the change after adding the parameter G, then you can not add G compile once, and then compare its size, you will find that after the addition of G, the resulting compilation file volume will increase, so when we do software development, Debugging information should be added in the initial design, the late, the application of these debugging information removed, of course, if you want to retain the software's own debugging functions, it will be retained.
Anyway
When you run a compiled file using the GDB command, you can see the following information, like other tools with some explanatory text, some copyrights, versions, and other descriptions, and finally (GDB) is its environment prompt, similar to the shell prompt, to alert the user, enter the command later.
[Email protected] programs]$ gdb Smallest
GNU gdb Fedora (6.8-27.EL5)
Copyright (C) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later This was free software:you was free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "Show copying"
and "Show warranty" for details.
This GDB is configured as "I386-redhat-linux-gnu" ...
(GDB)

Here are some common gdb debug commands
List or L for listing source programs
Each time the list or L will display 10 lines of source code, or specify the listed line number to view the source program "list line number"
When using this parameter, make sure the source program is not removed or moved, otherwise it cannot be viewed.
Break/b set breakpoints, you can specify a breakpoint line number, or function name.
Info Break Displays breakpoint information
Run Running the program
Print viewer values for the and variables of the corresponding expression when the program is run
Next step to run the program without entering the function call
Step step to run the program, and make a function call
Continue continue executing function until function ends or next breakpoint

The following is a smallest.c program to explain the use of several common GDB commands
---LIST/L, the following example
[Email protected] programs]$ gdb Smallest
GNU gdb Fedora (6.8-27.EL5)
Copyright (C) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later This was free software:you was free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "Show copying"
and "Show warranty" for details.
This GDB is configured as "I386-redhat-linux-gnu" ...
(GDB) L
1//find the minimal between 2 INT number
2 #include <stdio.h>
3 int min (int x, int y);
4 int Main ()
5 {
6 int num1,num2,min_num;
7 printf ("Please Input the first number:\n");
8 scanf ("%d", &num1);
9 printf ("Please Input the second number:\n");
Ten scanf ("%d", &num2);
(GDB) List 12
7 printf ("Please Input the first number:\n");
8 scanf ("%d", &num1);
9 printf ("Please Input the second number:\n");
Ten scanf ("%d", &num2);
Min_num=min (NUM1,NUM2);
printf ("The Minimal one is%d\n", min_num);
13}
14
int min (int a, int b)
16 {
(GDB)
You can see it list/l function, it is very simple, suitable for Manual Check program syntax errors, multiple commas, less a semicolon, it can be seen from this, of course, this error, GCC will tell you.

-----break/b, set the points as follows:

(GDB) B 12
Breakpoint 1 at 0x8048448:file smallest.c, line 12.
(gdb) Break min
Breakpoint 2 at 0x804846a:file smallest.c, line 17.
(GDB)
And then we'll see the next order.
-----info break/b displaying breakpoint information

(GDB) Info break
Num Type Disp Enb Address What
1 breakpoint Keep Y 0x08048448 in main at Smallest.c:12
2 Breakpoint Keep y 0x0804846a in min at smallest.c:17
(GDB) Info b
Num Type Disp Enb Address What
1 breakpoint Keep Y 0x08048448 in main at Smallest.c:12
2 Breakpoint Keep y 0x0804846a in min at smallest.c:17
(GDB)

As we can see from the above, the info break prints out the two breakpoints we set earlier, one in the 12th line of the main function and the other in the Min function.

You can run the program below.
-----RUN/R, run the program.
You can enter an R, or you can run the program by losing all run:
(GDB) R
Starting program:/home/bruce/programs/smallest
Please Input the first number:
12
Please Input the second number:
14

Breakpoint 2, Min (a=12, b=14) at smallest.c:17
if (a<b)
(GDB)
As you can see, stop at the min function at the breakpoint set by our line monitoring.

Let's step through the function,
-----step, Single Step execution
The example is followed by the above example to continue the execution of the
Breakpoint 2, Min (a=12, b=14) at smallest.c:17
if (a<b)
(GDB) Step
return A;
(GDB) Step
21}
(GDB) Step

Breakpoint 1, Main () at Smallest.c:12
printf ("The Minimal one is%d\n", min_num);
(GDB) Step
The minimal one is 12
13}
(GDB)
As you can see, when you execute step, you follow the steps in order, and enter the function body execution at the breakpoint of the Min function, until the end, and of course we can change to next to execute, but next does not enter the function body inside, the function as an execution step to execute.

The above is a few common gdb internal commands, there is a place to mention that when we execute to the breakpoint, in order to facilitate testing, we can in this place for the defined variable assignment, just enter at the GDB prompt: Set variable name = value
Then break into the continue and carry on.

This article introduces Linux under the basic methods of editing, compiling and debugging programs, using a combination of VIM/VI, GCC and gdb to carry out C program design, in which there are more esoteric, looking forward to everyone in the use of the time to find, this article is only a brief introduction, for reference.
OK, about the beginning of the Linux C language development, here it is!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.