GDB's Practical Debugging tools

Source: Internet
Author: User
Tags thread stop

GDB it is GNU Debuger abbreviation, it is GNU published Unix through the Application debugging tool.

It is widely used in various kinds of production in-house applications. GDB and all of the debugging tools often use the same, the main features are: monitoring the value of variables, set breakpoints and single-step operation.


Note that when the source program is compiled. To compile the source program into a running file using Gcc-g or Cc-g or g++-G, then you can use GDB for debugging.

Only in this way can the generated executable file include debug information.
Refer someone else to write a simple C program, under Linux using GCC to compile the executable file, and then use GDB for debugging.

Program source code such as the following

#include <stdio.h>    int func (int n)  {          int sum=0,i;          for (i=0; i<n; i++)          {                  sum+=i;          }          return sum;  }    int main ()  {          int i;          Long result = 0;          for (I=1; i<=100; i++)          {                  result + = i;          }           printf ("result[1-100] =%d \n\r", result);         printf ("result[1-250] =%d \n\r", func);  }
use Gcc-g to generate a running file Miki, Gcc-g miki.c-o Miki
use GDB to start debugging the Miki app. For example, the following:

/span>

$gdb Miki GNU gdb (gdb) CentOS (7.0.1-42.el5.centos) Copyright (C) Software Foundation, Inc. License gplv3+: GNU GPL version 3 or later 
L 1 indicates that the first line of the source code starts with 10 rows of records. Be able to write list 1
L indicates the front and back 10 lines of the source code are displayed and can be written as List
Blank return indicates repeated last command operation
R means the program starts executing

< Span style= "Color:rgb (34,34,34); font-size:14px; line-height:24px; Text-indent:24px ">

(GDB) B Breakpoint 1 at 0x4004f2:file Miki.c, line 23. (GDB) Info b Num Type Disp Enb Address what 1 breakpoint keep y 0x00000000004004f2   In Main at miki.c:23 (gdb) I b Num Type Disp Enb Address what 1 breakpoint Keep y  0x00000000004004f2 in Main at Miki.c:23 (GDB) broke Func breakpoint 2 at 0x40049f:file Miki.c, line 5. (GDB) Info break Num Type Disp Enb Address what 1 breakpoint keep y 0x0000000000400 4f2 in Main at miki.c:23 2 breakpoint keep y 0x000000000040049f on func at Miki.c:5 (gdb) I b Num T        ype Disp Enb Address 1 breakpoint Keep y 0x00000000004004f2 in main at miki.c:23 2 Breakpoint Keep y 0x000000000040049f in Func at Miki.c:5 3 breakpoint keep y 0x000000000040049f  in func @ miki.c:2 (gdb) d 2 (gdb) I b Num Type Disp Enb Address What1 breakpoint Keep Y 0x00000000004004f2 in main at miki.c:23 3 breakpoint keep y 0x000000000040049 F in func @ miki.c:2 (gdb) Delete 3 (gdb) I b Num Type Disp Enb Address what 1 Breakpo int keep y 0x00000000004004f2 in main at Miki.c:2

Break 23 Indicates that the 23rd behavior breakpoint is set and can be abbreviated to B 23
The break Func represents the setting of the Func function entry as a breakpoint. can be abbreviated to B func
Info break represents a breakpoint on a query setting and can be simply written as I B
Delete 2 means that the second breakpoint is deleted. can be shortened to D 2

(GDB) R starting program:/u01/home/oracle/miki warning:no loadable sections found in added Symbol-file system-supplied  DSO at 0x2aaaaaaab000 Breakpoint 1, Main () at miki.c:23 printf ("result[1-10] =%d \n\r", result);  (GDB) n result[1-10] = in printf ("result[1-5] =%d \n\r", func (5));    (GDB)  Breakpoint 2, func (n=5) at miki.c:5 5 int sum=0,i; (GDB) p N $ = 5 (GDB) P i = 10922 (GDB) p sum $ -1431642112 (GDB) n 6 for (i=0; i<n; i++) (  GDB) p I $4 = 10922 (GDB) n 8 sum+=i;  (GDB) p I $0 (gdb) p sum $6 = 0 (GDB) n 6 for (i=0; i<n; i++) (GDB) p i $7 = 0 (gdb) p n $8 =  5 (GDB) p I $9 = 0 (GDB) n 8 sum+=i;                        (GDB) p I $1 (gdb) p sum $11 = 0 (GDB) n 6 for (i=0; i<n; i++) (GDB) p sum $1 (GDB) n 8  Sum+=i; (GDB) n 6 for (i=0; i<n; i++) (GDB) p sum $13 = 3 (GDB) n 8 sum+=i;  (GDB) n 6 for (i=0; i<n; i++) (GDB) n 8 sum+=i;  (GDB) C continuing.  RESULT[1-5] = exited with code 023.  (GDB)

R means the program starts executing
N means running the next statement
C means to continue running
P I means the value of the variable i is printed out
P sum means that the value of the variable sum is printed out
GDB's debugging methods I know so much.

More specific to see this document.
Debugging Tools GdB has a lot of commands, and gdb divides it into a variety of categories. The help command is simply a list of GDB's command types. Suppose you want to look at the commands in a category. The ability to use help <class> commands, such as breakpoints, to view all commands for setting breakpoints. You can also help <command> directly to see the commands.


GDB, when the command is entered. It is possible to use the first few characters of the command without having to make a full command, and of course the first few characters of the command should mark a single command. Under Linux, you can tap the TAB key two times to make up the full name of the command, assuming that there are repeated, then GDB will take its example.
Demo Example one: When entering function func, set a breakpoint.

Ability to break Func, or just b func
(GDB) b func
Breakpoint 1 at 0x8048458:file hello.c, line 10.
Demo Example two: typing B press two times tab, you will see all the B commands:
(GDB) b
BackTrace Break BT
(GDB)
Demo Example three: Just remember the function prefix, can be this:
(GDB) B make_ < Press the TAB key >
(Press the TAB key again, and you'll see:)
Make_a_section_from_file Make_environ
Make_abs_section Make_function_type
Make_blockvector Make_pointer_type
Make_cleanup Make_reference_type
Make_command make_symbol_completion_list
(GDB) B make_
GDB gives you all the examples of functions that start with make.



Demo Sample IV: When debugging a C + + program. Have the ability to function as a name. Such as:
(gdb) b ' bubble (M-?
Bubble (double,double) Bubble (int,int)
(gdb) b ' Bubble (
You can see all the overloaded functions and the number of references in C + +.

(Note: M-and "Press two tab" is a meaning)

To exit GDB, simply send a quit or a command called Q.
Execute UNIX shell program in GDB
————————————
In the GDB environment, you can run Unix's shell commands and use GDB's shell command to complete:
Shell <command string>
Call the Unix shell to run <command string> the UNIX shell defined in the environment variables shell will be used to run the <command string>. Suppose the shell is undefined. Then use Unix's standard shell:/bin/sh. (Use Command.com or cmd.exe in Windows)
Another GDB command is make:
Make <make-args>
You can run the make command in GDB to build your own program again. This command is equivalent to "shell make <make-args>".


Executing a program in GDB
————————

When GDB is started with gdb <program>, GDB searches the path path and the current folder for <program> source files. To confirm that GDB is reading the source file, you can use the L or List command to see if GDB can list the source code.


In GdB, the execution program uses the R or Run command. Execution of the program. You may need to set up the following four things.


1, program implementation of the parameters.
Set args Specifies the number of time to execute. (Example: Set args 10 20 30 40 50)
The show args command is able to see the set number of execution parameters.


2, the implementation of the environment.
Path <dir> the execution paths of the program can be set.
Show paths view the execution path of the program.
Set environment varname [=VALUE] environment variable settings. such as: Set env User=hchen
Show environment [varname] View environment variables.
3, working folder.
CD <dir> equivalent to the Shell's CD command.
The PWD displays the folder where you are currently located.
4, the input and output of the program.
Info terminal shows you the terminal mode used by your program.


Use redirection control program output.

such as: Run > outfile
A TTY command can refer to an end device that writes input and output. such as: Tty/dev/tty
To debug a program that has been executed
————————
Two methods:
1. Under UNIX, use PS to view the PID (process ID) of the program being executed, and then use the GDB <program> PID format to hook up the executing program.


2, the first with the GDB <program> related source code, and GDB. Use the Attach command in GDB to hook up the PID of the process.

Use the detach to cancel the hook process.
Pause/Resume Program execution
—————————
In the debugger, pausing program execution is a must, and GDB can conveniently pause the execution of the program.

You can set which line of the program to stop at. Under what conditions to stop, when the signal received when the stop and so on. To make it easier for you to see the variables at execution time and the process at execution time.


When the process is paused by GDB, you can use the Info program to see if it is executing. Process number, the reason for being paused.


In GDB. We can have the following pause modes: Breakpoint (breakpoint), observer (watchpoint), Snap Point (catchpoint), signal (signals), thread stop (threads Stops). If you want to restore program execution, you can use the C or Continue command.


First, set breakpoints (breakpoint)
We use the break command to set breakpoints. There are several ways to set breakpoints on the front:
Break <function>
Stops when the specified function is entered. The function name can be specified in C + + using the Class::function or functions (type,type) format.
Break <linenum>
Stops at the specified line number.


Break +offset
Break-offset
Stops at the offset line before or after the current line number. Offiset is the natural number.
Break Filename:linenum
Stop at the LineNum line of the source file filename.


Break Filename:function
Stop at the entrance of the function function of the source file filename.
Break *address
Stops at the memory address where the program executes.


Break
When the break command does not have a number of parameters. Indicates a stop at the next instruction.
Break ... if <condition>
... Can be the number of references mentioned above. Condition the condition and stops when the condition is established.

For example, in the context of the body. Ability to set break if i=100. Indicates that the program is stopped when I is 100.


When viewing breakpoints, you can use the Info command, such as the following: (Note: n indicates a breakpoint number)
info breakpoints [n]
Info break [n]
Second, set the observation point (watchpoint)
The observer generally observes whether the value of an expression (the variable is also an expression) has changed. If there is a change, stop the program immediately. We have several ways to set the Observer point:
Watch <expr>
Set an observer point for the expression (variable) expr.

Stop the program immediately when there is a change in the value of a quantity expression.
Rwatch <expr>
When an expression (variable) expr is read. Stop the program.


Awatch <expr>
When the value of an expression (variable) is read or written. Stop the program.


Info watchpoints
Lists all the observed points that are currently set.


Third, set the snap point (catchpoint)
You can set a snap point to catch some events when the program executes.

such as: Load shared libraries (dynamic link libraries) or C + + exceptions. The format of the snap point is:
Catch <event>
When the event occurs, stop the program. The event can be something of the following:
1. Throw an exception thrown by C + +. (Throw is keyword)
2. Catch an exception caught by C + +. (Catch is keyword)
3. When exec calls the system to call exec. (exec is keyword.) This feature is now only practical under HP-UX)
4. Fork calls when the system calls fork. (Fork is keyword, now this feature is only practical under HP-UX)
5. Vfork calls the system when calling Vfork. (vfork is keyword. This feature is now only practical under HP-UX)
6. Load or load <libname> load shared library (dynamic link library). (Load is keyword, now this feature is only practical under HP-UX)
7. Unload or unload <libname> uninstall shared library (dynamic link library).

(Unload for keyword, now this feature is only practical under HP-UX)
Tcatch <event>
Set the snap point only once. When the program stops, it should be deleted on its own initiative.


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

GDB's Practical Debugging tools

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.