How to use GDB debugging under Linux

Source: Internet
Author: User
Tags clear screen

GDB is a very useful debugging tool under Linux, although it is a command-line mode debugging tool, but it is powerful enough to you can not imagine, here is a brief introduction to GDB under common commands.

Start by compiling the executable file (the test.c here is a simple program that asks for the first n items).

Gcc-g test.c-o Test (-G option tells GCC to add debugging information when compiling the program).
Then you can do that.

GDB test

Then you will see a lot of information on the screen, roughly speaking, some of GDB's version information description, but it is not useful for your debugging program, so you can add-q option, do not output them.

Gdb-q Test

[Email protected]:~$ gdb-   from

Have you ever felt that the world is a lot of clean.

You can also enter GDB mode before loading the file.

[Email protected]:~$ gdb-   from

OK, now it's time to start debugging, but I also want to see what my Code does, and GDB provides a command to get your program to show up.

(GDB)//List defaults to display 10 rows at a time1 #include <stdio.h>2int func (IntN3{4IntI5int sum=0;6for (i=0;i<n;i++)7{8 sum+=I9}10Returnsum; (GDB)//Direct input Enter repeat last command, display the next 10 lines11}12IntMain ()13{ int n;  printf (" Please enter the value of n ");  scanf ("%d",&N);  printf ("1+2+. +%d=%d", N,func (n));  return 0;  (gdb)                

The list default parameters can be viewed with show listsize, and can be changed with set listsize <count> if you feel that there are too many or too few 10 rows.

The list can also add other parameters, such as:
List 5,10 shows the code from line 5th to line 10th;

The list func shows the code around the Func function, and the display range is related to the list parameter;

List test.c:5,10 Displays the code for the source file test.c lines 5th through 10th, which is typically used to debug a program with multiple source files.

GDB also supports string lookups, search str, starting with the current line, looking forward to strings with Str;

Reverse-search str, starting at the current line and looking backwards for a string with Str.

Now your screen should be full, right? Want to empty the screen, but still in gdb inside Ah, how to do? In fact, GDB also supports running Linux commands, you can enter the shell in the GDB prompt, and then enter the command you need.

(GDB) Shell clear

This also can achieve the effect of clear screen.

Look at the code of the program, feel that the 6th line of code may be a bit of a problem, now I need to set a breakpoint, let the program stop before the 6th line.

66  

The following line of information, 1 shows that I set this breakpoint is the first breakpoint, the breakpoint where the memory address is 0X80484C8, it is on the 6th line of the file test.c.

GDB can also set breakpoints in conditional expressions.

If n==67   

The meaning of this breakpoint is that if the value of N is 6, the program runs to line 7th to stop.

Of course, you can also set a breakpoint directly at a function, and the direct break function name will do.

Then we want to see the breakpoint information set, you can use the info Breakpoints command.

(GDB) Info breakpointsnum     Type           Disp Enb Address    what1       breakpoint     keep y in   func at TEST.C:6 infunc at test.c:7if n==6 in Func attest.c:5     

Num represents the number of the breakpoint, the type of the breakpoint that represents the breakpoint, the second breakpoint type, plus the condition, disp indicates whether the break point has been lost after execution, dis is yes, keep is not, ENB indicates whether the current breakpoint is valid, Y is yes, n is no Address indicates the memory location where the breakpoint is located, and what indicates where it is located.

If the program does not need to be paused at that breakpoint, there are two ways to invalidate the breakpoint, or to delete the breakpoint directly.

1(GDB) info breakpointsnum     Type           Disp Enb Address    what1       breakpoint     keep N in   func At test.c:6 withfunc at test.c:7if n==6 in Func attest.c:5    

As you can see, the ENB of the first breakpoint becomes n, indicating that the breakpoint is invalid and you can use the Enable command if recovery is required. It is important to note that the parameter following the disable is the number of the breakpoint. Instead of the line number.

To delete the breakpoint directly, you can use the clear command and the delete command.

61  

The arguments following the clear command are the line numbers that set the breakpoint, and the function name of the breakpoint can also be set after the clear parameter.

The parameter after the delete command is the number of the breakpoint, you can delete more than one breakpoint at a time, and the breakpoint number is separated by a space; if there are no parameters after the delete, the breakpoint is deleted by default, and a prompt is given to choose whether to operate.

The breakpoint is set, and now it's ready to be debugged.

(GDB) Run//Start executing the procedure starting program:/home/wang/Test Please enter the value of n 10Breakpoint1, func (n=At test.c:6//Set the first breakpoint, the program pauses on line 6th6for (i=0;i<n;i++) (GDB)Continue//Keep the program running until the next breakpoint or endContinuing.breakpoint2, func (n=At test.c:8//The second breakpoint is set to stop at i==68 sum+=i; (gdb) Print I//Print the value of I with the Print command $1 =6(gdb) Print sum$2 =15(GDB) Next// continue execution of the next statement and execute only one bar. 6 for (i=0;i<n;i++) (gdb) Next 8 sum+=i; (gdb) print i$3 = 7(GDB) continue continuing. 1+. +10=45[inferior 1 (Process 23636) exited normally] (GDB) Quit // exit GDB Debug  

There are a lot of commands on it, and here's how it's used.

Run and start running the program;

Continue, the command to continue running the program while the program is paused;

Print variable name or expression that prints the variable or the value of the expression. Whatis the variable name or expression to display the data type of the variable or expression.

The print variable = value, which can also assign a value to the corresponding variable, and a set variable variable = value. Functions and is assigned the same value as print.

Next, proceed to the next statement; another command step, similar to this, is that when the next statement encounters a function call, next does not track into the function, but continues to execute the following statement, and the step command tracks the inside of the function.

(GDB) runstarting program:/home/wang/1, Main () at test.c:        scanf ("%d",&  printf ("1+2+. +%d=%d"//0;(gdb)            

(GDB) runstarting program:/home/wang/Test Breakpoint 1, Main () at test.c:scanf ("%d",&N);(gdb) n Please enter the value of n17 printf ("1+2+. +%d=%d", N,func (n));(gdb) Step //Step command Trace entered the Func function func (n=) at test.c:5< c17>5 int sum=0;(gdb)            

There are also Nexti and Stepi commands, both of which are stepping through a machine instruction, such as (i=0;i<n;i++) the statement needs to enter multiple Nexti to execute, and the two differences are the same as above.

Quit, exit GDB debugging, if you want to quit in debugging, you can enter the command directly, you will be prompted to choose whether to exit. Kill command to end the debugging of the current program (does not exit GDB).

is active.    

How to use GDB debugging under Linux

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.