Detailed explanation of GDB debugger (2)

xiaoxiao2021-03-05  25

View stack information

-----

When the program is stopped, the first thing you need to do is to check where to stop. When your program calls a function, the address, function parameters, and the local variables in the function will be pressed into the "stack". You can use the GDB command to see the information in the current stack.

Here is some GDB commands for viewing the function call stack information:

Backtrace

BT

Print all the information of the current function call stack. Such as:

(GDB) BT

# 0 func (n = 250) At tst.c: 6

# 1 0x08048524 in main (argc = 1, argv = 0xBfffff674) At tst.c: 30

# 2 0x400409ed in __libc_start_main () from /lib/libc.so.6

From the above you can see the call stack information of the function: __ libc_start_main -> main () -> func ()

Backtrace

BT

n is a positive integer that means that only the stack information of the N layer on the top of the stack.

Backtrace <-n>

BT <-n>

-n table a negative integer, indicating that only the stack information of the N layer under the stack.

If you want to see a layer of information, you need to switch the current stack, in general, the program is stopped, the top of the stack is the current stack, if you want to check the details of the stack, first you want to do it first. Switch the current stack.

Frame

F

n is an integer starting from 0 and is a layer number in the stack. For example: frame 0, represents the top of the stack, FRAME 1, indicating the second layer of the stack.

Up

Indicates that the upper surface of the stack moves the N layer, and may not be hit N, indicating that the upward movement is moved.

Down

Indicates that the N layer is moved to the stack, and may not beat N, indicating a downward movement.

The above command will print out information of the moving stack layer. If you don't want it to make it. You can use these three commands:

SELECT-FRAME corresponds to the Frame command.

Up-Silently corresponds to the UP command.

Down-Silently corresponds to the Down command.

View the current stack of information, you can use the following GDB command:

Frame or F

This information will be printed: The stack layer number, the current function name, the function parameter value, the file and the line number where the function is located, the function being executed.

INFO FRAME

Info f

This command prints the information of more detailed current stack layers, but most of them are within the internal address of the runtime. For example, the function address, the address of the call function, the address called function, the current function is written by, the function parameter address, the value, the address, and the like of local variables, etc. Such as:

(GDB) Info F

Stack level 0, frame at 0xBffff5d4:

EIP = 0x804845d in func (tst.c: 6); Saved EIP 0x8048524

Called by frame at 0xBfffFF60C

Source Language C.

Arglist at 0xBfffFf5D4, Args: n = 250

Locals AT 0xBfffff5D4, Previous Frame's SP IS 0x0

Saved registers:

EBP AT 0XBFFFF5D4, EIP AT 0xBffff5D8

Info args

Print the parameter name and its value of the current function.

Info locals

Print all local variables and their values ​​in the current function.

INFO CATCH

Print an exception handling information in the current function.

View the source program

-----

First, display source code

GDB can print out the source code of the debugprue, of course, be sure to add -g parameters when compiling, and compile the source program information into the execution file. Otherwise, you can't see the source program. When the program stops, the GDB will report that the program stopped on the first few lines of the file. You can use the list command to print the source code of the program. Let's take a look at the GDB command to view the source code.

List

Displays the source program around the Linenum line.

List

Displays the source program of the function called function name FUNCTION.

List

Displays the source program behind the current line.

List -

Displays the source program in front of the current row.

Generally, print the top 5 rows of the current row, if the display function is on the top 2 line 8 line, the default is 10 lines, of course, you can customize the range of display, use the following command to set a display source program The number of rows.

Set listsize

Set the number of rows that displays the source code.

Show Listsize

View the current ListSize settings.

The List command has the following usage:

List ,

Displays the source code between the first line to the LAST line.

List,

Displays the source code from the current line to the LAST line.

List

The source code is displayed later.

Generally speaking, you can follow the following parameters after List:

line number.

< Offset> The positive offset of the current line number.

<-offset> The negative shift of the current line number.

Which file is of which file.

Function Name.

which function in which file is.

<* Address> The address of the statement of the program runs in memory.

Second, search source code

Not only that, GDB also provides commands for source code search:

Forward-Search

Search

Search in front.

Reverse-search

All search.

Among them, is the regular expression, and the main string matching mode, about the regular expression, I will not talk about it here, please check the relevant information.

Third, the path to specify the source file

At some point, in the execution program compiled with -g, just includes the name of the source file, no path name. GDB provides commands that allow you to specify the path to the source file to search for GDB.

Directory

Dir

Add a source file path to the front of the current path. If you want to specify multiple paths, you can use ":", you can use ":" and Windows.

Directory

Clear all custom source file search path information.

Show Directories

Displays the defined source file search path.

Fourth, the memory of the source code

You can use the Info Line command to view the address of the source code in memory. Info Line, you can follow the "Row", "Function Name", "File Name", "File Name: Function Name", this command prints the memory address of the specified source code at runtime, such as:

(GDB) Info line tst.c: func

Line 5 of "TST.C" Starts at Address 0x8048456 and ends at 0x804845d . There is a command (disassemble) You can view the current execution of the source program, this command will Put the directive DUMP in the current memory. As shown below, the assembly code of the function FUNC is viewed.

(GDB) Disassemble Func

Dump of assembler code for function func:

0x8048450 : push% EBP

0x8048451 : MOV% ESP,% EBP

0x8048453 : SUB $ 0x18,% ESP

0x8048456 : MOVL $ 0x0, 0xfffffffc (% EBP)

0x804845D : MOVL $ 0x1, 0xfffffff8 (% EBP)

0x8048464 : MOV 0xfffffff8 (% EBP),% EAX

0x8048467 : CMP 0x8 (% EBP),% EAX

0x804846a : jle 0x8048470

0x804846C : jmp 0x8048480

0x804846e : MOV% ESI,% ESI

0x8048470 : MOV 0xfffffff8 (% EBP),% EAX

0x8048473 : add% EAX, 0xffffff (% EBP)

0x8048476 : INCL 0xfffffff8 (% EBP)

0x8048479 : jmp 0x8048464

0x804847B : NOP

0x804847C : Lea 0x0 (% ESI, 1),% ESI

0x8048480 : MOV 0xfffffffc (% EBP),% EDX

0x8048483 : MOV% EDX,% EAX

0x8048485 : jmp 0x8048487

0x8048487 : MOV% EBP,% ESP

0x8048489 : POP% EBP

0x804848a : RET

End of assembler dump.

View runtime data

-------

When you debug the program, when the program is stopped, you can use the print command (the shortup command is P), or syncing the command inspect to view the current program running data. The format of the print command is:

PRINT

PRINT /

is an expression, the expression of the language you debugging (GDB can debug multiple programming languages), is the format of the output, for example, if you want to output an expression in the format of the expression So. / X. First, expressions

PRINT and many GDB commands, you can accept an expression, and GDB calculates this expression according to the data running in the current program. Since it is an expression, it can be a CONST constant, variable, function, etc. in the current program. content. Unfortunately, GDB can't use the macro you defined in the program.

The expression of the expression should be the syntax of the language currently debugged, since C / C is a public type of language, so the examples in this article are about C / C . (And I will introduce later, I will introduce later)

In the expression, there are several operators supported by GDB, which can be used in any language.

@

It is an operator related to the array that will be described in more detail later.

::

Specify a variable in a file or a function.

{}

A object indicating a type of Type pointing to memory address .

Second, program variables

In GDB, you can view the values ​​of the following three variables at any time:

1. Global variables (visible all files)

2, static global variable (visible to the current document)

3, local variable (current Scope visible)

If your local variables and global variables have collisions (which is the reintegration), the local variable hides the global variable, that is, if a global variable and the local variable in a function, if the current stop point In the function, the value of the variable displayed with PRINT is the value of the local variable in the function. If you want to view the value of global variables, you can use the "::" operator:

FILE :: Variable

Function :: Variable

You can specify the variable you want to view in this form, which file is in or in the function. For example, check the value of the global variable X in the file f2.c:

GDB) P 'f2.c' :: x

Of course, "::" Operations and C conflicts, GDB can automatically identify "::" whether C operators, so you don't have to worry that there will be exceptions when debugging C programs.

In addition, it is important to note that if your program is compiled, the optimized option is turned on, then some variables may not be accessed when using GDB to be optimized, or the value of the value is incorrect. This is normal, because the optimizer will delete your program, organize your program's order, eliminate some meaningless variables, so when you debug this program, the runtime instruction and you write instructions. Different, there will be no results you think. When this is done, it is necessary to turn the compilation optimization when the compiler is compiled. In general, almost all compilers support compilation optimized switches, for example, GNU's C / C compiler GCC, you can use the "-gstabs" option to solve this problem. For parameters for the compiler, please check the instructions documentation for the compiler.

Third, array

Sometimes you need to see a value of a continuous memory space. For example, a parameter of an array or the size of the data that is dynamically allocated. You can use the "@" operator, "@" operator, "@" is the value of the first memory address, "@", you want to see the length of memory. For example, there is such a statement in your program: int * array = (int *) Malloc (len * sizeof (int));

So, during the GDB debugging process, you can display this dynamic array with the following command:

P * array @ le

@ 的 边 The value of the first address of the array is the content pointed to by the variable Array. The right side is the length of the data, which is saved in the variable LEN, its output result, about the following:

(GDB) P * array @ le

$ 1 = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40}

If it is a static array, you can display all the contents of all the data in the array can be displayed directly with the Print array name.

Fourth, output format

In general, GDB outputs the value of variables according to the type of variable. But you can also customize the format of the output of GDB. For example, you want to output an integer hexadecimal, or binary to view the situation in this integer variable. To do this, you can use GDB data display format:

x Display variables in hexadecimal format.

d Display variables by decimal format.

u Display unsigned integer in the hexadecimal format.

o Display variables in eight-en-binary format.

t Press binary format to display variables.

a Display variables in hexadecimal format.

c Press the character format display variable.

f Display variables in floating point.

(GDB) P i

$ 21 = 101

(GDB) P / A i

$ 22 = 0x65

(GDB) P / C i

$ 23 = 101 'e'

(GDB) P / F i

$ 24 = 1.41531145e-43

(GDB) P / x i

$ 25 = 0x65

(GDB) P / T i

$ 26 = 1100101

Five, check the memory

You can use the examine command (short-write Yes x) to see the value in the memory address. The syntax of the x command is as follows:

X /

n, f, u is an optional parameter.

n is a positive integer that represents the length of the memory, that is, the content of several addresses from the current address.

f represents the format displayed, see above. If the address refers to a string, the format can be s, if the ten is the command address, then the format can be i.

u Represents the number of bytes requested from the current address, if not specified, the GDB defaults to 4 Bytes. u Parameters can be replaced by the following characters, b represents the single-byte, h represents the double byte, w represents the four-byte, g represents eight bytes. When we specify the length of the byte length, the GDB will start from the memory address, read and write the specified byte, and take it as a value.

represents a memory address.

The three parameters of n / f / u can be used together. E.g:

Command: x / 3uh 0x54320 is represented, from the memory address 0x54320 read content, H indicates that the double-byte is one unit, and 3 represents three units, u means that it is displayed in hexadecimal display.

Six, automatic display

You can set some of the automatically displayed variables, or when the program is stopped, or when you are single-step tracking, these variables are automatically displayed. The associated GDB command is Display.

Display Display /

Display /

EXPR is an expression, FMT represents the format of the display, and addr represents the memory address. When you set one or more expressions with Display, as long as your program is stopped, GDB will automatically display these these The value of the expression.

Format I and S are also supported by Display, a very useful command is:

Display / i $ PC

$ PC is a gdb environment variable, indicating the address of the command, / i indicates the output format as the machine instruction code, which is compilation. So when the program is stopped, the source code and the machine command code correspond to the case, which is a very interesting feature.

Here are some GDB commands related to Display:

Undisplay

Delete Display

Delete automatic display, DNUMS means the set automatic explicit number. If you are deleted at the same time, the number can be separated by a space. If you want to delete a range number, you can use the minus sign (eg 2-5)

Disable Display

ENABLE DISPLAY

Disable and ENALBE do not delete automatic display settings, but just make it fails and recovered.

Info Display

View the automatic display of Display settings. GDB will play a form and report how many automatic display settings set in the debug, which includes, set numbers, expressions, whether eNABLE.

7. Set display options

There are more options on the display in GDB. Here I will only exist of most common options.

Code

Set Print Address

Set Print Address ON

Open the address and output, when the program displays the function information, GDB will display the parameter address of the function. The system defaults to open, such as:

(GDB) F

# 0 set_quotes (lq = 0x34c78 "<<", rq = 0x34c88 ">>")

AT INPUT.C: 530

530 if (LQUOTE! = Def_lquote)

Set Print Address Off

Close the parameter address of the function display, such as:

(GDB) Set Print Addr OFF

(GDB) F

# 0 set_quotes (lq = "<<", rq = ">>") at input.c: 530

530 if (LQUOTE! = Def_lquote)

Show Print Address

Check if the current address display option is open.

Set Print Array

Set Print Array ON

Open the array display, when the array is turned on, each element occupies a row, if it is not opened, each element is separated by a comma. This option is closed by default. The two commands related to it are as follows, I will not say more.

Set Print Array Off

Show Print Array

Set Print Elements

This option is mainly setting array. If your array is too big, you can specify a to specify the maximum length of the data display. When the length is reached, GDB will no longer display it. . If set to 0, it is not limited. Show Print Elements

Check the option information for Print Elements.

Set Print Null-Stop

If this option is turned on, then when the string is displayed, the end is displayed. This option defaults to OFF.

Set Print Pretty on

If you open the PrintF pretty option, then it is more beautiful when the GDB displays the structure. Such as:

$ 1 = {

NEXT = 0x0,

Flags = {

Sweet = 1,

Sour = 1

}

MEAT = 0x54 "PORK"

}

Set Print Pretty OFF

Turn off the Printf Pretty this option, the GDB displays the structure as follows:

$ 1 = {Next = 0x0, Flags = {SWEET = 1, Sour = 1}, meat = 0x54 "pork"}

Show Print Pretty

View how GDB displays structures.

Set print sevenbit-strings

Set the character display, press "/ nn" format display, if the string or character data is displayed / NNN, such as "/ 065".

Show print sevenbit-strings

View the character display switch is open.

Set Print Union

When the display structure is displayed, it is explicitly in which the consortium data is displayed. For example, there is the following data structure:

Typedef enum {tree, bug} species;

Typedef enum {BIG_TREE, Acorn, Seedling} Tree_Forms

Typedef enum {catterpillar, cocoon, butterfly}

Bug_Forms;

Struct thing {

Species it;

Union {

Tree_Forms Tree;

Bug_Forms bug;

} form;

}

Struct thing foo = {Tree, {acorn}};

When this switch is turned on, after executing the P foo command, it will be displayed as follows:

$ 1 = {It = Tree, Form = {Tree = acorn, bug = cocoon}}}

When the switch is turned off, then the P foo command is executed, it will be displayed as follows:

$ 1 = {It = Tree, Form = {...}}

Show Print Union

Viewing the display of the consortium data

Set Print Object

In C , if an object pointer points to its partial class, if the option is opened, the GDB will automatically display the output according to the rule called by the virtual method. If this option is turned off, GDB regards the virtual function table. This option is OFF by default.

Show Print Object

View the settings for the object option.

Set Print Static-Members

This option indicates that when the content in a C object is to display the static data member. The default is ON. Show print static-members

Check the static data member option settings.

Set Print VTBL

When this option is opened, GDB will display the virtual function table with a comparison format. Its default is closed.

Show Print VTBL

View the options for virtual functions display format.

Eight, history

When you use the GDB's print to view the data running data, you will be recorded by GDB. GDB will make every print command for your way in a way in $ 1, $ 2, $ 3 ...... Thus, you can use this number to access the previous expressions, such as $ 1. The benefits of this feature are that if you have previously entered a relatively long expression, if you want to view the value of this expression, you can use history to access, save your duplicate input.

Nine, GDB environment variables

You can define your own variables in the GDB debug environment to save running data in some debuggers. To define a variable of a GDB is simple. Use the GDB's set command. GDB's environment variables are the same as UNIX, is also a head. Such as:

SET $ foo = * Object_ptr

When using environment variables, GDB creates this variable when you first use, and in later use, it assigns it directly. Environment variables have no type, you can define any type to the environment variable. Includes structures and arrays.

SHOW Convenience

This command views all environment variables currently set.

This is a relatively powerful function, environmental variable, and program variable interaction, which will make program debugging and convenient. E.g:

Set $ ​​i = 0

Print Bar [$ I ] -> Contents

So, when you don't have to, Print Bar [0] -> Contents, Print Bar [1] -> Contents Enter the command. After entering such a command, only the knock back, repeat the previous statement, the environment variable will be automatically accumulated, thereby completing the function of outputting one by one.

Ten, check the register

To view the value of the register, it is easy, you can use the following command:

Info registers

View the situation of the register. (Except floating point registers)

Info all-registers

View all registers. (Including floating point registers)

Info registers

View the situation of the specified register.

The program is placed in the register, such as the currently running instruction address (IP), the current stack address (SP) of the program, and the like. You can also use the print command to access the register, just add a $ symbol before the register name. Such as: p $ EIP.

Change the execution of the program

-------

Once you use the GDB to hang up the debugger, when the program runs, you can dynamically change the current debugger running line or the value of its variable according to your own debugging ideas, this powerful function can make you Better debugging your program, for example, you can run all the branches of the program in the program.

First, modify the variable value

Modify the variable value when the debugger is running, it is easy to implement in GDB, and you can complete it using GDB's print command. Such as:

(GDB) Print x = 4

X = 4 This expression is the syntax of C / C , meaning that the value of the variable x is modified to 4, if your current debugging is Pascal, then you can use Pascal's grammar: x: = 4. At some point, it is very likely that your variables and parameters in GDB conflicts, such as:

(GDB) WHATIS ​​WIDTH

TYPE = DOUBLE

(GDB) P Width

$ 4 = 13

(GDB) set width = 47

Invalid Syntax in Expression.

Because set width is the command of GDB, "Invalid Syntax In Expression" appears error, at this time, you can use the set var command to tell GDB, Width is not your GDB parameter, but the program's variable name, Such as:

(GDB) set var width = 47

In addition, there may be some cases, GDB does not report such an error, so the insurance starts, preferably when you change the program variable, it is best to use the set var format GDB command.

Second, jump execution

In general, the debugging procedure will be executed in accordance with the running order of the program code. GDB provides the function of chaos, that is, GDB can modify the execution order of the program, allowing the program to execute cashed. This feature can be over the jump command of GDB:

Jump

Specifies the operating point of the next statement. can be the line number of the file, which can be file: line format, which can be NUM's offset format. The table is the next operational statement where to start.

Jump

The

is the memory address of the code line.

Note that the jump command will not change the content in the current program stack, so when you jump from a function to another, the function will have errors when the function runs back, and the possible results are still very strange. Even generation of program core dump. So it is best to jump in the same function.

People who are familiar with assembly know that when the program is run, there is a register to save the memory address where the current code is located. Therefore, the JUMP command is also changing the value in this register. Thus, you can use the "SET $ PC" to change the address of the jump execution. Such as:

SET $ PC = 0x485

Third, generate semaphore

Use the Singal command to generate a signal to be debugged. Such as: Interrupt Signal Ctrl C. This is very convenient for debugging of the program, setting breakpoints in any position running, and generating a semaphore in this breakpoint, which accurately generates a debugging of the signal at some point.

The syntax is: Signal , UNIX system semaphore usually from 1 to 15. So is also in this range.

Different Single Commands and Shell's Kill commands, when the system's kill command signals to the debugged program, it is intercepted by GDB, and a signal from the SINGLE command is sent directly to the debugged program.

Fourth, the mandatory function returns

If your debug break is in a function, and there is still a statement that is not executed. You can use the return command to force the function ignore the statements that have not yet been executed and return.

Return

Return

Use the return command to cancel the execution of the current function and return it immediately. If is specified, the value of the expression is recognized as the return value of the function. V. Forced call functions

Call

The expression can be a function in the expression to achieve the purpose of the forced call function. And display the return value of the function, if the function returns the value is Void, then it is not displayed.

Another similar command can also complete this feature --Print, which can be followed by the expression, so it can also be used to call the function, the varint, and call are different. If the function returns Void, Call does not display, Print The display function returns the value and stores the value into historical data.

Use GDB in different languages

------------

GDB supports the following languages: C, C , Fortran, Pascal, Java, Chill, Assembly, and Modula-2. Generally speaking, GDB will determine the debug language according to the program you debug, such as: Discovering the file name suffix ".c", GDB will consider a C program. The file name suffix is ​​".c, .cc, .cp, .cpp, .cxx, .c ", GDB will consider a C program. The suffix is ​​".f, .f", GDB will consider the Fortran program, and if the suffix is ​​".s, .s" will consider the assembly language.

That is, GDB will set their own language environment based on the language of the program you debug, and let GDB's commands change to change the language environment. For example, some GDB commands require expression or variables, the syntax of these expressions or variables is completely changed according to the current locale. For example, the syntax of the pointer in C / C is * P, and in the modula-2 is P ^. Also, if your current program is compiled by several different languages, the GDB can automatically switch the locale according to different languages ​​during the debugging. This function of changing the language environment is really a design of the developer.

Here are a few commands related to the GDB locale:

SHOW LANGUAGE

View the current locale. If GDB does not know the programming language you debug, then the C language is considered a default environment.

INFO FRAME

View the program language of the current function.

Info Source

View the program language of the current file.

If the GDB does not detect the current programming, you can also manually set the current program language. Use the set language command to do.

When you don't follow the set language command, you can view the language type supported by GDB:

(GDB) Set Language

The Currently Understood Settings Are:

Local or Auto Automatic Setting Based on Source File

C Use the c language

C Use THE C Language

ASM Use the asm language

Chill Use the Chill Language

Fortran Use the fortran language

Java Use the Java Language

MODULA-2 Use The Modula-2 Language

Pascal Use the Pascal Language

Scheme Use the scheme language

So you can keep up with the list of quilted programs that are listed after Set Language to set the current locale.

postscript--

GDB is a powerful command line debugging tool. Everyone knows the power of the command line that it can form a sequence to form a script. The software under UNIX is the command line. This gives the program development for great convenience, the advantage of the command line software is that they can be very easy to integrate, using a few simple tools, You can make a very powerful feature.

So the software under UNIX is more organically combined than the software under Windows, and each exerts each of the strengths and combines a stronger function. The graphics software under Windows is basically the camp, which cannot be called, which is very disadvantageous to integrate each other. It is not what comparable to Windows, the so-called "inch", the feature is short ", the graphics tool is still a place that is not as good as the command line. (When you see this sentence, I hope that you will never think that I am "despise the graphical interface", and I raise my bar)

I am writing this article based on the version 5.1.1, so some functions have been modified, or there is a more powerful feature. Moreover, I wrote very rush, it is more written, and I have seen many of the wrong words (I use five, so the wrong word makes you can't understand), so I am here to express my mistake in my text. Apologize.

When the function of the GDB in the text, I just collected some of the commands and usage methods of GDB, in fact, I only explain the features of all GDBs, more documents, or see it, please check GDB's help and use manual, perhaps, after a while, if I have time, I will write a GDB's advanced use.

I personally like the function of automatic debugging of GDB. This feature is really powerful. I am really powerful. I write a script under UNIX, let the script automatically compile my program, automatically debug, and report the results, debugging, automatic Checkin source code library. A order, compile with commissioning with Checkin, more cool. Just GDB is not very mature for automated debugging, only semi-automatic, and sincerely expects the maturity of GDB automation debugging.

If you are interested in GDB or other technical issues, welcome to discuss communication with me. I am currently the development of product software under UNIX, so it is familiar with software development under UNIX. When it is not only technique, it is also slightly personal for software engineering implementation, software design, system analysis, and project management. Welcome everyone to find me, (QQ is: 753640, MSN is: haoel@hotmail.com)

转载请注明原文地址:https://www.9cbs.com/read-32024.html

New Post(0)