[Linux Network Programming] Use GDB to debug programs and network programming gdb

Source: Internet
Author: User

[Linux Network Programming] Use GDB to debug programs and network programming gdb

Linux has a powerful debugging tool, GDB (GNU Debugger), which can debug C and C ++ programs.

The main functions of GDB are as follows:

To use GDB, you must add the-g option (gcc-g xxx. c) to compile the program. When this option is set, GCC adds a "wedge" to the program, which GDB can use to interact with the program.

 

Test procedure 1 in the book:

/* Name: ex02-gdb-01.c */# include <stdio. h>/* used for printf */# include <stdlib. h>/* For malloc * // * declare that the sum function is of the static int type */static int sum (int value ); /* structure used to control input and output */struct inout {int value; int result ;}; int main (int argc, char * argv []) {/* Memory application */struct inout * io = (struct inout *) malloc (sizeof (struct inout);/* determine whether the request is successful */if (NULL = io) {/* failure return */printf ("memory application failed \ n"); return-1;}/* determine whether the input parameter is correct */if (argc! = 2) {printf ("parameter input error! \ N "); return-1;}/* obtain the input parameter */io-> value = * argv [1]-'0 '; /* accumulate and sum the value */io-> result = sum (io-> value); printf ("your input value is % d, and the calculation result is: % d \ n ", io-> value, io-> result); return 0;}/* accumulate sum function */static int sum (int value) {int result = 0; int I = 0;/* accumulate value in cyclic calculation */for (I = 0; I <value; I ++) result + = (I + 1);/* return result */return result ;}

 

Enter the gcc-o test gdb-01.c-g to add it to the wedge ". Go to debugging:

Test is the name of the executable file.

 

Common GDB commands (command (abbreviated): Format and meaning ):

File: loads (executable) files. If the file name is not followed by the gdb command.

(Gdb) file gdb-01.c "/home/tyruschin/ClionProjects/gdb_learning/gdb-01.c": not in executable format: unrecognized file format (gdb) file testLoad new symbol table from "test "? (Y or n) yReading symbols from test... done.

Set args: set the input parameters (which can be set in the run command. If this parameter is not set, the parameters of the previous run command are used by default during run ), set args parameter 1 parameter 2...

(Gdb) set args 2 (gdb) runStarting program:/home/tyruschin/ClionProjects/gdb_learning/test 2 your input value is: 2 and the calculation result is: 3

Show: DISPLAY variable, show args

(gdb) set args 2 3(gdb) show argsArgument list to give program being debugged when it is started is "2 3".

List (l): list the code content of the file. List 1 indicates that 10 rows are displayed by default starting from the first line. Press the Enter key to continue printing 10 rows. list 1 and 4 indicate 1 to 4 rows are displayed, press enter again to print the following 10 rows.

Break (B): Specifies the breakpoint, line number B, or function name. The program runs on the terminal at the breakpoint and waits for the user's next operation command.

Specific Use (if multiple files generate a target execution file together, you need to specify the file name, such as B gdb-01.c: 38 ):

View breakpoint information, info break

// If no serial number is specified, all serial numbers are indicated.

Delete a specified breakpoint: delete breakpoints breakpoint number (found in info break)

Disable breakpoint: disable B breakpoint number (in this case, the Enb column in info break displays n instead of y)

Allowed breakpoint (Prohibited reverse operation): enable B breakpoint number

Clear breakpoint: clear the breakpoint line number

Run (r): run the program, run args. The args here is consistent with that in set args. When a breakpoint is reached, it is paused.

(gdb) run 3Starting program: /home/tyruschin/ClionProjects/gdb_learning/test 3Breakpoint 1, sum (value=3) at gdb-01.c:3939int result = 0;

Print (p): print the variable content. It is powerful and can be printed.AnyValid expression value.

The breakpoint is set to 28 rows. The above is an example of the p printing variable. io is a struct array, argv is a string array, and the number following @ indicates the number of prints, after the number is exceeded, the output is random ..

Whatis: variable type check, print the type of an array or variable, And whatis variable name

Ptype: variable type detection, detailed structure definition

Set: Modify the variable value. set xx = val

Display: display variable. This value is displayed every time debugging is paused.

(Gdb) run 3 Starting program:/home/tyruschin/ClionProjects/gdb_learning/test 3 Breakpoint 8, main (argc = 2, argv = 0x7fffffffde18) at gdb-01.c: 3333 printf ("your input value is: % d, and the calculation result is: % d \ n", io-> value, io-> result); 4: argc = 23: * argv = 0x7fffffffe1c6 "/home/tyruschin/ClionProjects/gdb_learning/test" 2: argv = (char **) 0x7fffffde181: argc = 2

Single-step debugging: next (n) One-step tracking, step (s) enters a function, finish returns to the called function, and continue (c) indicates that the execution continues to know that a breakpoint or end is encountered.

(Gdb) run 3The program being debugged has been started already. Start it from the beginning? (Y or n) yStarting program:/home/tyruschin/ClionProjects/gdb_learning/test 3 Breakpoint 1, main (argc = 2, argv = 0x7fffffffde18) at gdb-01.c: 1919if (NULL = io) {(gdb) s25if (argc! = 2) {(gdb) s30io-> value = * argv [1]-'0'; (gdb) s32io-> result = sum (io-> value ); (gdb) ssum (value = 3) at gdb-01.c: 3939int result = 0; (gdb) s40int I = 0; (gdb) finishRun till exit from #0 sum (value = 3) at gdb-01.c: 400x0000000000400639 in main (argc = 2, argv = 0x7fffffffde18) at gdb-01.c: 3232io-> result = sum (io-> value ); value returned is $1 = 6 (gdb) s33printf ("your input value is: % d, and the calculation result is: % d \ n", io-> Value, io-> result); (gdb) cContinuing. the value you entered is 3, and the calculation result is: 6 [Inferior 1 (process 27047) exited normally]

Q: Exit GDB ..

 

 

 

 

Appendix: Examples in the book:

/* Name: ex02-gdb-01.c */# include <stdio. h>/* used for printf */# include <stdlib. h>/* For malloc * // * declare that the sum function is of the static int type */static int sum (int value ); /* structure used to control input and output */struct inout {int value; int result ;}; int main (int argc, char * argv []) {/* Memory application */struct inout * io = (struct inout *) malloc (sizeof (struct inout);/* determine whether the request is successful */if (NULL = io) {/* failure return */printf ("memory application failed \ n"); return-1;}/* determine whether the input parameter is correct */if (argc! = 2) {printf ("parameter input error! \ N "); return-1;}/* obtain the input parameter */io-> value = * argv [1]-'0 '; /* accumulate and sum the value */io-> result = sum (io-> value); printf ("your input value is % d, and the calculation result is: % d \ n ", io-> value, io-> result); return 0;}/* accumulate sum function */static int sum (int value) {int result = 0; int I = 0;/* accumulate value in cyclic calculation */for (I = 0; I <value; I ++) result + = (I + 1);/* return result */return result ;}

  

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.