Use GDB debugging procedures (seven)

zhaozj2021-02-08  238

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 modified to 4 values ​​of the variable x, if your current debug language is PASCAL, then you can use PASCAL syntax: x: = 4. At some point, it is very likely that your variables and parameters in GDB conflicts, such as: (GDB) 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, some cases, GDB does not report such an error, so the insurance starts, it is best to use the set var format when you change the program variable.

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 immediately, if is specified, the value of the expression is recognized as the return value of the function. V. Forced call functions

The Call can be a function 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 Views the current locale. If GDB does not know the programming language you debug, then the C language is considered a default environment. Info frame is a program language of the current function. Info Source is a program language for 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 the set language command is not followed, 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 is listed in the set language Program language names come to set up 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 for the integration of various software. 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 I will never think that I am "despise the graphical interface", and I raise the bar.) I am writing this article written by the version 5.1.1, so there may be some functions Has been modified or more powerful. 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-automated, 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 more familiar to software development under UNIX, of course, not just technology, implementation, software design, system analysis, project management, I also have a slight experience. Welcome everyone to find me, (QQ is: 753640, MSN is: haoel@hotmail.com) <- Previous Page

(All rights reserved, please indicate the author and the source when reproduced)

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

New Post(0)