GDB Debug Program

Source: Internet
Author: User

Writing test2.c

Compiling into an executable file

gcc test2.c-g-o test2
GDB test2

Go to GDB debugging

Run command

Run command format

Run <arg1...argn>

Where run can be abbreviated to most of the instructions in R,gdb.

Execute in GDB

Run Lvyahui Blog

You can see the output

Running run again does not need to pass parameters again, it uses the parameters passed by the last call to the Run command

You can see the default parameters

You can execute the run with parameters again or modify the default parameters using set AGRs

Breakpoint Set Breakpoint
Break <line-number>

You can view line numbers in the Vim editor or view line numbers with the LIST/L directive

Edit code file test1.c

Compile good test1

gcc test1.c-g-o test1
GDB test1

Entering the GDB environment

Look at the code first, then set a breakpoint, then run, and let the continue continue execution.

Add a breakpoint before a method

Break can add a short point directly before the method

Break Fun_name

In this debug execution

Delete Breakpoint

See below how to delete the previously set short point

Delete Breakpoint <point-number> #删除指定断点 # or delete breakpoints  #删除所有断点

Where delete can be shortened to D

The clear command can also be used to clear breakpoints

Set a breakpoint by an expression
if expression

Follow the above method to set the breakpoint, found that failed, prompted no symbol Var in the current context

At first, I thought it was. Settings need to be set in run, so set a breakpoint on line 13, run, when stopped at 13 lines, and then set a conditional breakpoint on line 15th, but

Finally found that the problem remains. Obviously the problem is not here, and then went online to check the next, at compile time add a-gstabs+ option

gcc -g-gstabs+-c test1.cgcc test1.o-o test1

Re-enter GDB debugging

You can set this conditional breakpoint on the discovery.

You can use Readelf to see two different ways to compile the symbol table for the generated. o file differs.

Run take a look under

But performing a discovery breakpoint does not work

Although I do not know why, but obviously before because the compiler did compile optimization, but I obviously did not add any-ox option AH. Later changed the i==50 to i>=50, found that can stop.

Really depressed, I how can be this value?

If it is a multi-file program, break can also specify a file to add a breakpoint, in the following format

Break <filename:line-number><filename:fun-name>#例如break test1.c:  Break Test1.c:main
Disable and enable breakpoints

Format

Disable/enable Breakpoint num

Observation point

The point of view can also act as a breakpoint.

Edit Test3.c File

#include <stdio.h>int sum(inta) {    intI,res =0;  for(i=0; I < a;i++) {res+=i; } return res;intMain (void) {intA =Ten; intAsum =sum(a); printf ("asum is%d \ n", asum); Return0;}

Compile

gcc -g-gstabs+-wall test3.c-o test3

The watch command needs to be used after running the program, so we need to add a breakpoint first to stop the program.

Delete breakpoint, continue execution

View Running data

The print command is often used to view data at run time, and once again, you need to know how to step into a function and run to the next breakpoint

    • next--executes a line of source code but does not enter the inside of the function.
    • step--executes a line of source code and goes inside the function.
    • Continue-executes to the next pause point or to the end of the program.

Debug Test1 Program again

Format of print

For

Print <expression><expression>

Where expression can be a variety of forms

An expression

P (-6) *3+result

Function call

P Func (5)

Array

P *[email protected]_length

Writing test4.c

#include <stdio.h>#include<string.h>intMain (void) {intArr[] = {1,2,3,4,5}; intLen =4, I; int* ARR2 = (int*) malloc (len * sizeof) (int));  for(i=0; i<len;i++) {Arr2[i]= i*2; }     Free(ARR2); Return0;}

Go to GDB debugging

Automatic display of variables
Display/FMT expr

The characters in the control format are much more similar to printf, and do not repeat here

The above is a personal summary of some of GDB's basic content, more content will be mentioned in the following blog

GDB Debug Program

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.