Debug GCC programs using gdb in Linux programming tools

Source: Internet
Author: User

The Linux programming tool contains a GNU debugging program called gdb. gdb is a powerful debugger used to debug C and C ++ programs. it enables you to observe the internal structure and memory usage of the program while the program is running. the following are some functions provided by gdb:
It enables you to monitor the value of variables in your program.
It enables you to set breakpoints so that the program stops running on the specified code line.
It enables you to execute your code in one row.

Type gdb on the command line and press the Enter key to run gdb. If everything works properly, gdb will be started and you will see similar content on the screen:

 
 
  1. GNU gdb 5.0   
  2. Copyright 2000 Free Software Foundation, Inc.   
  3. GDB is free software, covered by the GNU General Public License, and you are   
  4. welcome to change it and/or distribute copies of it under certain conditions.   
  5. Type "show copying" to see the conditions.   
  6. There is absolutely no warranty for GDB. Type "show warranty" for details.   
  7. This GDB was configured as "i386-redhat-linux".   
  8. (gdb)  

After you start the Linux programming tool gdb, you can specify many options on the command line. You can also run gdb in the following ways:
When you run gdb in this way, you can directly specify the program to be debugged. this tells gdb to mount the executable file named fname. you can also use gdb to check a core file generated due to program termination exceptions, or connect it to a running program. you can refer to the gdb guide or Type gdb-h on the command line to get a simple list of instructions on these options.

Linux programming tool gdb compile Code for Debugging (Compiling Code for Debugging)
To make Linux programming tool gdb work normally, you must make your program contain debugging information during compilation. the debugging information includes the type of each variable in your program, the address ing in the executable file, and the source code line number. gdb uses this information to associate the source code with the machine code.
Enable the debugging option with the-g option during compilation.

Gdb basic commands

Gdb supports many commands to enable different functions. these commands are loaded from simple files to complex commands that allow you to check the stack content you are calling. Table 27.1 lists some of the commands you will use when debugging with gdb. for details about how to use gdb, refer to the Guide page of gdb.

Description
File to load the executable file to be debugged.
Kill to terminate the program being debugged.
List lists part of the source code that generates the execution file.
Next, execute a line of source code but do not enter the function.
Step: execute a line of source code and enter the function.
Run to execute the program currently being debugged
Quit terminate gdb
Watch enables you to monitor the value of a variable, regardless of when it is changed.
Print display expression value
Break sets breakpoints in the code, which will cause the program to be suspended when it is executed here.
Make enables you to re-generate executable files without exiting gdb.
Shell enables you to run UNIX shell commands without leaving gdb.

Article Reprinted from network management: http://www.bitscn.com/ OS /linuxbc/201005/186755_2.html

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.