Gdb Learning (1) [version 2] And gdb learning Version 2

Source: Internet
Author: User

Gdb Learning (1) [version 2] And gdb learning Version 2
Overview

Gdb is short for GNU debugger and a programming debugging tool.

 

Function

1. Start the program and run the program as needed according to user-defined requirements.

2. The program to be debugged can be stopped at the breakpoint specified by the user (the breakpoint can be a conditional expression ).

3. When the program stops, you can check what happens in the program.

4. dynamically change the execution environment of the program.

 

 

Example:

Gcc-Wall-g simple. c-o simple

# Be sure to add the-g option to generate debugging information.

 

Start gdb: gdb [-q] [executable-file] [core-file]

Gdb simple

# Start gdb

 

List (l)

# View the program's ten lines of code. You can enter the list Command consecutively to view all the code in the file.

 

Break (B) n

# Set a breakpoint in line n

 

Info break (I B)

# Viewing breakpoint settings

 

Break function-name (B function-name)

# Set a breakpoint at the function entry

 

Run (r)

# Start the program and pause it at the breakpoint

 

Step (s)

# One-Step tracking

 

Print I (p I)

# Print the current I value

 

Until

# Skip Loop

 

Continue (c)

# Continue to run until the next breakpoint. If it continues until the end of the program, the program end information is printed.

 

Enter (Press enter)

# Indicates using the previous command

 

Finish

# Exit the current function and print the return value of the function.

 

Delete n

# Delete breakpoint n

 

Quit (q)

# Exit the entire gdb debugging program

 

 

Run the program

1. run (r) # run the program until the first breakpoint or program ends.

2. run arg1 arg2... # Run the program and add parameters, such as r a B c d...

 

 

View Source Code

List (l)-view the source code of the last 10 lines

List fun-name-view fun function source code

List file: fun-name-view the fun function source code in the file

For example: l search. c: seq_search # If only one file package contains the seq_search function, you do not need to add "search. c :"

 

List [m, n]

List 2nd-view source code from 10th rows to rows

List, 15-view up to 15th lines of source code

 

Set breakpoints and observation points

Break [line-num]

Break [fun-name]

Break [file-name: line-num] # B search. c: 4

Break [file-name: fun-name]

Break if <condition>-the Program stops when the condition is set.

E. g. break if I = 10

 

Info break (I B)-view breakpoints

Watch expr-once the expr value changes, the program stops. [Set observation points]

E. g. watch I # once the I value changes, the program is suspended

 

Delete n (d n)-delete breakpoint n # d 1d 1 2 3

 

Single-step debugging

Continue (c)

-Run to the next breakpoint

 

Step (s)

-One-step tracking, entering the function, similar to step in Vs [but does not enter a function without source code provided]

 

Next (n)

-One-step tracking, which does not enter the function, is similar to step out in VC.

 

Finish

-Run the program until the current function is complete and return. The stack address, return value, and parameter value returned by the function are printed. But does not jump out of the main function.

 

Until

-Run the program until you exit the loop body. If no bounce occurs at a time, execute the next

 

Return [value]

-Stop the current function and let the function return [execution of the Dynamic Adjustment Program] in advance to return the value to the caller, which is equivalent to step return.

 

Set variable var-name = value

-Change the value of a variable.


Summary of common gdb commands

Command

Function

Run (r)

Run and start the program

List (l)

List 10 Source programs

Break (B)

Set breakpoints

Info break (I B)

Print breakpoint details

Continue (c)

Continue to run the program until the next breakpoint

Watch

Set observation points

Step (s)

Single-step tracking, similar to step in VC

Next (n)

Single-step tracking, similar to step out in VC

Finish

Run the program until the current function returns.

Until (u)

Jump out of the loop (sometimes twice)

Print (p)

View runtime variables and expressions

Help

Print help information

 

Appendix-

 

# Makefile

CC = gcc

CFLAGS =-Wall-g

 

BIN = sample main

 

. PHONY: all clean

 

All: $ (BIN)

 

%. O: %. c

$ (CC) $ (CFLAGS)-o $ @-c $ <

 

Sample: sample. o

 

Main: main. o search. o

$ (CC) $ (CFLAGS)-o $ @ $ ^

 

Clean:

-Rm-rf $ (BIN) *. o

 


This book describes in detail how to use GDB and its command-It is a book abo in LINUX.

. Rar] examples] -gdb printprintbar_src.zip]-Examples]-a book about gdb debugging. If you want to learn more about programming in linux, you can refer to [gdb

The gdb debugging must be gdb aout? If an error occurs in a program, aout cannot be generated. I want to use gdb to debug and find the error. How can this problem be solved?

Unix systems do not have the suffix. The executable files a and a. out are the same.
Gdb debugging:
1. Enter the gdb Console for gdb a. out.
2. set args [arg_list] (set the parameter, that is, the argv string in main (int argc, char ** argv). This parameter is not required)
3. Set breakpoints
1): B file name: number of rows, such as: B test. c: 20
2): B function name, for example, B main
3): view the breakpoint info B
4. Start the program r or run
5. One-step debugging of s/step (when the function enters)
6. Multi-step debugging n [number of running lines] (when the function is not entered) default step
7. DISPLAY variable value p [variable name]
8. view the stack bt [number of top lines of the stack] 5 rows by default
9. Exit the GDB Console quit
.....
The simplest debugging is just a few steps.
GDB has a lot of online debugging materials. You can search for them.
In addition, if you want to learn UNIX/LINUX programming, we recommend a very practical introductory book "unix/linux Programming Practice tutorial" published by Bruce Molay at Tsinghua University, translated by Yang Zongyuan and Huang Haitao. Very classic. As long as you have the foundation of c, you can learn. In addition, we can get to know the unix/linux system and compile practical mini programs.

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.