Linux programming basics-GDB (Getting Started)

Source: Internet
Author: User

Debugging is an indispensable task in the development process. In Linux programming, gdb is usually used to debug our c/c ++ program. Because gdb can introduce many things, I will introduce several articles in detail here. I will introduce some articles on the Internet in great detail and deserve to be added to my favorites: Use GDB to debug programs (1), (2), (3), and (4), (5), (6), and (7 ).

Preparations

During program compilation, the function name and variable name will be replaced by the corresponding memory address, but these memory addresses are not readable for us. We often need to find the actual address based on the function name and variable name to read its value.

To facilitate debugging, gcc adds a-g parameter. during compilation, you can add the corresponding symbol information in the Assembly.

Gcc-g hello. c-o hello

Start GDB

The following methods are used to start GDB:

  1. Gdb <program> uses gdb to associate a program. You can run the run Command to start the associated program process.
  2. Gdb <program> core uses gdb to debug the core file. The core is the file generated after the core is dumped after the program is illegally executed.
  3. Gdb <program> <PID> uses gdb to debug the currently running process, similar to "attach to process" in.

These methods can also be used in combination. For example, you can use gdb <program> to associate the program and attach <pid> to debug the currently running process.

Introduction to the GDB Environment

First, we will introduce the GDB environment. GDB is also a Shell interactive environment. In GDB, the process can see its prompt: (gdb)

(Gdb)Break

In gdb, you also want to have the following smart notification functions for:

  1. When you enter a command, you can use only the first few characters of the command.
  2. After entering the first few characters, you can use the Tab key to automatically complete
  3. If there are many commands starting with a few characters, the Tab key cannot be fully supplemented. If you press the TAB key again, all commands starting with this character can be displayed.
  4. You can use the Enter key to repeatedly execute commands (commonly used for single-step tracking)
  5. You can use the up/down key to switch the history command

For example, if you set the breakpoint command to break, you can directly enter B. You can also enter B and press the Tab key twice to query all the commands starting with B (if there is only one, you can directly complete the command ):

(Gdb) B
Backtrace bookmark break-range bt

This smart prompt can also be used to automatically associate the function name when the breakpoint is set. For example, if you want to set a breakpoint for the main function, when you enter B mai and press the Tab key, it will automatically complete as main

(Gdb) B main

In addition, you can use the help <command> command to view the help information of unfamiliar commands, similar to man in shell, which is very convenient.

Exit GDB

The method for exiting gdb is relatively simple. Simply enter the q (quit) command in Shell:

(Gdb) q
Tianfang>

Due to space limitations, this article will introduce how to set breakpoints in the next article.

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.