Lesson 6 GDB debugging (I) and gdb debugging

Source: Internet
Author: User
Tags gdb debugger

Lesson 6 GDB debugging (I) and gdb debugging
1 Preface:

1. After learning the previous Makefile knowledge, beginners are full of confidence and feel that they should learn well not only to master the language writing, but also to learn the corresponding tools to increase development efficiency. Sometimes the code we write goes through different execution results than we expected. So we need to debug it. Speaking of debugging experienced Development shoes, we know that the VS integration platform is well-known in the downstream of windows. In linux, what tools can we use to compile C and C ++ for debugging, here, I recommend GDB, which is also the GNU debugger for debugging.

2 knowledge points:

2.1gdb has the following functions:

2.1.1. Start the program;

2.1.2. debugging can be performed through breakpoints;

2.1.3. You can view the running status when the breakpoint is stopped;

2.1.4. dynamically change the execution environment, including the system environment, system variables, and local variables;

2.2gdb commands

2.2.1 file <filename>: load the executable file ;;

2.2.2 r: Short for run, which indicates running the program. If a breakpoint exists, it will be paused at the first break point;

2.2.3 l: Short for list, which lists the relevant function code or line code (including other files). It may seem like a maximum of 10 lines. You can also use l: to display 1 to 10 lines;

2.2.4 B function or B num: B is short for breakpoint. It indicates that a function is called a breakpoint, or a breakpoint is called at the num row number. It can also be used in the cycle: B if I = 10 indicates that it is paused when I = 10 in the loop;

2.2.5 d num: Delete the breakpoint where the number starts from 1. If it is not specified, delete all;

2.2.6 s: Execute the next statement, Step Into (one-Step tracking), and enter the function if there is a function;

2.2.7 n: Execute the next statement, Step Over (single-Step tracking). If a function exists, it will not enter the function;

2.2.8 p: Short for print to print the value of a variable;

2.2.9 I: the abbreviation of info, which can display a lot of information;

2.2.10 q: Short for quit, indicating to exit the current command;

2.2.11 help <name>: displays the instructions in the gdb command manual;

2.2.12 until: exit the current function;

2.2.13 finish: If the return value is returned even before the current function is completed, the returned value is displayed;

3 principle:

3.1 add debugging information (such as breakpoints and output debugging information) during compilation, execute gdb debugging, and the program will pause where there is a breakpoint in the order of debugging information, when debugging information is output, it is convenient for programmers to debug the program and observe everything the program is running. The following is a simple example to illustrate gcc compilation, gdb startup, and debugging steps.

4 practice:

4. 1. Create a simple gdbtest. c file with the following content:

#include <stdio.h>int main(void){     int a = 1;     int b = 2;     int c = a + b;     printf("result :%d",c);        return 0;      }

4. 2. Compile as follows:

Gcc-Wall-g gdbtest. c-o gdbtest #-g parameter

4. 3. Start gdb and load the gdbtest executable file:

Gdb # directly type the gdb command in the current gdbtest. c directory to enter the debugger.
File gdbtest # input this command under the gdb command to load the file, and output Reading symbols from gdbtest... done to indicate that the loading is complete.

4. Run the command l to view the file. The code is listed and the row is marked. Remember the row number or function name to break the breakpoint;

 

4. 5. Use the format: B function (row number) to open a breakpoint. Enter the command: B main or: B 3. The effect is the same!

4. 6. if you run the r command, the program will be paused at the breakpoint. In this case, you can run the s command to execute the next step (the n command is also executed by the function, and the s command will enter the function ), after the disconnection is paused, run the c command. You can use p to print the variable information and view the related variables:

. You can also use the command disassemble main to view the assembly code of the main function. This application is more advanced !, Use the q command when debugging is not required to exit.

In this lesson, we will first talk about gdb and simple motion gdb for simple debugging. Continue tomorrow !! 1


In linux, the l command in the gdb debugger is not clear about its usage.

Many, but the general commands are basically the same; understand the basic structure of Linux directories, how to name various hardware (I was confused by its naming rules for Hard Disk Partitions when I first came into contact with Linux), and how users interact with hardware through the Linux kernel; you can also find a Linux release, such as Ubuntu, which is currently quite popular, to familiarize yourself with how to configure its various services. If you are later engaged in development in Linux, then you can learn how to program in Linux, C/C ++, and so on, master the basic usage of the GCC/G ++ compiler, the GDB debugger, and learn to write Makefile to manage your project; recommended books: Linux Standard learning tutorial (for Red Hat Enterprise Linux 4, I have mentioned that the basic commands are similar, the configuration file is different in different releases in some places), "UNIX/Linux Programming Practice tutorial" (that is, Understanding UNIX/Linux Programming), and "UNIX environment advanced Programming" (that is, Advan Ced Programming In The UNIX Environment ). The first two articles are used for getting started. Now I have seen the second one. The last one has not been read yet. It is very thick. The comments are classic! If you want to learn more, find a direction, such as application layer development, driver layer development, and kernel research. Finally, I wish you success!
In addition, the webmaster group has products to buy, Which is cheap and guaranteed.

In linux, the l command in the gdb debugger is not clear about its usage.

If the list Command adds a row number, it does not print the content after 10th, But prints the content around 10th rows, that is, the content from 10th-10/2 rows to 10 + 10/2 rows.
The help list in gdb is described in detail.

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.