Change the execution of a program
———————
Once you use GDB to hang the debugger, and when the program is running, you can dynamically change the current debugger's running circuit or its variable value in GDB based on your own debugging ideas, a powerful feature that allows you to better debug your program, for example, You can walk through all the branches of the program in one run of the program.
Modifying the value of a variable
Modify the variable values that are run by the debugger, which is easy to implement in GDB, and can be done using the GDB Print command. For example,
(gdb) print x=4
x=4 This expression is a C + + syntax, meaning to change the value of the variable x to 4, if your current debugging language is Pascal, then you can use Pascal's syntax: X:=4.
at some point, it is possible that your variables conflict with the parameters in gdb, such as:
(gdb) whatis width
type = Double
(GDB) p width
$ =
(GDB) set width=47
Invalid syntax in expression.
Because, set width is GDB's command, so there is a "Invalid syntax in expression" setting error, at this point, you can use the set var command to tell Gdb,width not your gdb parameters, but the program's variable name, such as:
(GDB) Set Var width=47
Also, there may be cases where GDB does not report this error, so it is safe to use the GDB command in set var format when you change the value of the program variable.
second, jump implementation
In general, the debugger executes sequentially according to the order in which the program code is run. GDB provides the ability to perform disorderly execution, that is, GDB can modify the order in which programs are executed, allowing the program to perform random jumps. This feature can be completed by GDB's Jump command:
Jump <linespec>
Specifies the run point of the next statement. <linespce> can be the line number of the file, can be file:line format, can be +num this offset format. The table begins where the next run statement starts.
Jump <address>
The <address> here is the memory address of the line of code.
Note that the jump command does not change the contents of the current stack, so when you jump from one function to another, when the function is returned to the stack operation will inevitably occur, the result is very strange, and even generate program core Dump. So it's best to jump in the same function.
People familiar with the Assembly know that when the program is running, a register is used to hold the memory address where the current code resides. So, the jump command changes the value in this register. You can then use the "set $pc" to change the address of the jump execution. Such as:
Set $pc = 0x485
third, generate the signal quantity
Using the Singal command, you can generate a semaphore to the program being debugged. Such as: Interrupt signal CTRL + C. This is very convenient for the debugging of the program, you can set a breakpoint anywhere in the program running, and in the breakpoint with GDB to generate a semaphore, this precise place to produce a signal is very advantageous to debug the program.
The syntax is: the system semaphore of signal <singal>,unix usually from 1 to 15. So the value of <singal> is also in this range.
Unlike the shell's Kill command, the system's kill command signals to the debugger that it is intercepted by GDB, and a single command sends a signal directly to the debugger.
four, force function return
If your debug breakpoint is in a function, and there are statements that are not executed. You can use the return command to force a function to ignore a statement that has not yet been executed and returns.
Return
Return <expression>
Use the return command to cancel execution of the current function and return immediately, and if <EXPRESSION> is specified, the value of the expression is considered to be the returned value of the function.
v. Force call function
Call <expr>
You can use a function in an expression to force the function to be invoked. and displays the return value of the function, if the function return value is void, then it is not displayed.
Another similar command can also complete this function--print,print can be followed by the expression, so you can also use him to invoke the function, the difference between print and call is, if the function returned Void,call is not displayed, print shows the function return value, And the value is stored in historical data.
Using GDB in different languages
——————————
GDB supports the following languages: C, C + +, Fortran, PASCAL, Java, Chill, assembly, and Modula-2. In general, GDB will determine the debug language of course according to the program you are debugging, for example, if you find the filename suffix ". C", GDB will consider it a C program. FileName suffix is ". C,. CC,. CP,. cpp,. cxx,. C + +, GDB is considered a C + + program. And then the suffix is ". F,. F ", GDB will think it is a FORTRAN program, and, if the suffix is". S,. S "will be thought of as assembly language.
That is, GDB will set its own locale according to the language of the program you are debugging, and let GDB's commands change as the locale changes. For example, when some GDB commands need to use expressions or variables, the syntax of these expressions or variables is completely changed according to the current locale. For example, the syntax for pointers in C/s + + is *p, while in Modula-2 it is p^. And, if your current program is compiled with several different languages, GDB can automatically switch the locale to different languages during the debugging process. This kind of functionality that changes with the language environment is a design that is thoughtful to the developer.
Here are a few commands related to the GDB locale:
Show language
View the current locale. If GDB is not able to recognize the programming language you are debugging, C is considered the default environment.
Info frame
View the program language of the current function.
Info source
View the program language for the current file.
If GDB does not detect the current program language, you can also manually set the current program language. You can do this by using the Set Language command.
When the set language command does not follow anything, you can view the types of languages 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
You can set the current locale by following the set language followed by the list of program language names.
Postscript
--
GDB is a powerful command line debugging tool. You know the power of the command line is that it can form a sequence of execution to form a script. Unix software is all command line, which provides a great convenience for the development of the program, the advantage of command-line software is that they can be easily integrated together, using a few simple existing tools command, you can make a very powerful function.
So the software under UNIX is more organically integrated than the software under Windows, each of which has its own strengths and is combined into more powerful functions. and the graphics software under Windows basically is their own battalion, can not call each other, very unfavorable to the integration of various software. Here is not to do with Windows to do a comparison, the so-called "inch, the ruler has a short", graphical tools or there is not as good as the command line place. (see this sentence, I hope you will never think that I am "despise graphic interface", and I argue)
I'm writing this article based on the version of 5.1.1 GdB, so maybe some features have been modified or more powerful. And, I write very hastily, write more briefly, and, which I have seen a lot of typos (I use Wubi, so you can not understand the typo), so I am here to the error in my article to express my deepest apologies.
The functions of GDB listed in the article, I just listed some of the GDB with the command and use methods, in fact, I only talk about the function of only about 60% of GDB, detailed documentation, or please check GDB's help and use manuals, perhaps, over time, if I am free, I'll write an advanced use of GDB.
I personally like GDB's automatic debugging function, this function is really powerful, just imagine, I write a script under UNIX, let the script automatically compile my program, be automatically debugged, and report the results, debugging success, automatic checkin Source library. A command, compile with debugging with checkin, how cool ah. GDB is only the current support for automated debugging is not very mature, can only achieve semi-automatic, sincerely expect GDB's automated debugging functions mature.
If you are interested in GDB or other technical issues, you are welcome to discuss the exchange with me. I am currently mainly in the UNIX product software development, so, under the UNIX software development is more familiar, of course, not only is the technology, software engineering implementation, software design, system analysis, project management I also have a little experience. Welcome everyone to talk to me, (QQ is: 753640,MSN is: haoel@hotmail.com)