28.C language gdb debugging


Programmers write in the preparation of the program can not be plain sailing, gcc compiler syntax errors can be found in the program code, but can not find the business logic errors, the debugger program is one of the elements of software development. There are many ways to debug a program, for example, can run step printf statement tracking program and display the values ​​of variables, this chapter introduces a powerful debugging tools gdb.

A, gdb installation

CentOS system, log in as root server, execute the following command to install or upgrade.

yum  -y  install  gdb 

Note that if your server does not install gdb, the above command will install the latest version of gdb, if already installed gdb, will be updated to the latest version of gdb, therefore, the implementation of the above command no matter how many times, no problems.

Install gdb, provided that the server must access the Internet.

Second, pre-commissioning preparation

When using gcc compiler source code, compiled executable file will not contain source code, if you plan compiled programs can be debugged, compile time to add -g parameters such as:

 gcc -g -o book113 book113.c 

Enter gdb book113 at the command prompt, you can debug book113 procedure.

 gdb book113 

Third, the basic debugging command

command Command Command Description
set args Set the parameters of the main program. For example: ./ book119 /oracle/c/book1.c /tmp/book1.c parameter setting method: gdb book119 (gdb) set args /oracle/c/book1.c /tmp/book1.c
break b Set breakpoints, breakpoint B 20 indicates on line 20 is provided, you may be provided a plurality of breakpoints.
run r Start running the program, the program runs to the breakpoint will stop, if not at a breakpoint in the program has been run down.
next n The current line statement, if the statement is a function call, will not enter the internal function execution.
step s The current line statement, if the statement is a function call, then enter the function executes the first statement of them. Note, if the function is a function library function or provided by third parties, with s also get in, because there is no source code, if it is your custom function, as long as the source code can go.
print p Display of variable values, for example: p name indicates the value of the display variable name.
continue c Continue to run the program until the next breakpoint is encountered.
set varname=v Set the value of a variable, the program assuming there are two variables: int ii; char name [21]; set ii = 10 ii is the value to 10; set name = "beauty" name of the value to "beauty", noted not strcpy.
quit q Quit gdb environment.

Note that, in the context gdb, gdb executed commands may be selected by the up and down cursor keys.

Fourth, homework

gdb debugging programmer must master the method, to try it, figure out the usage of each command.

Nine, copyright notice

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Published 29 original articles · won praise 2 · Views 662

Guess you like

Origin blog.csdn.net/m0_45133894/article/details/104656682