Debug program with GDB (vii)

Source: Internet
Author: User
Tags locale semaphore
Debug Program with GDB (vii)Category: Programming tools 2003-07-12 16:42 26275 people read review (16) Favorite report Language Pascal Fortran UNIX scheme C + +


Change the execution of a program
———————

Once you use GDB to hang up the debugger, when the program is running, you can change the running line of the currently debugged program or the value of its variable dynamically in GDB according to your debugging idea, this powerful function can let you debug your program better, for example, You can go through all the branches of the program in one run of the program.


First, modify the value of the variable

    Modifying the variable values that are run by the debugger is easy to implement in GDB and is done using GDB's Print command. such as:
   
        (gdb) print x=4
   
& nbsp;   x=4 This expression is the syntax for C/s + +, meaning to change the value of 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:
   
  & nbsp;     (gdb) whatis width
        type = Double
        (GDB) p width
        $4 = +
& nbsp;       (GDB) set width=47
        Invalid Syntax in expression.

Because the set width is a gdb command, there is a "Invalid syntax in expression" setting error, 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 situations where GDB does not report this error, so it is safe to use the GDB command in the set var format when you change the value of the program variable.

second, jump execution

In general, the debugger executes sequentially in the order in which the program code is run. GDB provides the ability to execute in a disorderly order, that is, GDB can modify the order in which the program executes, allowing the program to perform random jumps. This feature can be done by GDB's Jump command:

Jump <linespec>
Specifies the run point of the next statement. The <linespce> can be the line number of the file, which can be the File:line format, which can be the +num offset format. The table is 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 program stack, so when you skip from one function to another, it is inevitable that an error will occur when the function is returned with a stack operation, and the result may be very strange, even if the program core Dump is generated. So it's best to jump in the same function.

People familiar with the Assembly know that when the program runs, 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 set $PC to change the address of the jump execution. Such as:

Set $pc = 0x485


iii. generation of signal volume

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 debugging the program, you can set the breakpoint at any point in the program run, and in this breakpoint with GDB to produce a semaphore, this precisely in a place to produce a signal very advantageous to the program debugging.

The syntax is: the system semaphore of the signal <singal>,unix is usually from 1 to 15. So the <singal> value is also in this range.

Unlike the kill command of the shell, the kill command of the system is intercepted by GDB when signaled to the debug program, and a signal issued by the single command is sent directly to the debugged program.

Iv. Mandatory function return

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

Return
Return <expression>
Use the return command to cancel the execution of the current function and return immediately, if <EXPRESSION> is specified, and the value of the expression is considered to be the return value of the function.


v. Force call function

Call <expr>
A function can be used in an expression to achieve the purpose of forcing the function to be called. 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 be done with this function--print,print can be followed by the expression, so you can also use him to invoke the function, print and call is different, if the function returned Void,call is not displayed, print shows the function return value, And the value is stored in the 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, such as: find the filename suffix ". C", GDB will consider it a C program. The filename suffix is ". C,. CC,. CP,. cpp,. cxx,. C + +, GDB will be considered a C + + program. The suffix is ". F,. F ", GDB will consider it to be a FORTRAN program, and the suffix is". S, ". S "will be considered to be assembly language.

In other words, GDB will set its own locale based on 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 changed entirely 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 by several different languages, GDB can automatically switch the locale to different languages during debugging. This function, which changes with the language environment, is a thoughtful design for developers.


Here are a few commands related to the GDB locale:

Show language
View the current locale. If GDB does not recognize the programming language you are debugging, then 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 using the Set Language command.

When the set language command does not follow anything, you can see what kind of language GDB supports:

(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 then set the current locale by following the set language followed by the listed program language name.

Postscript
--

GDB is a powerful command-line debugging tool. You know the power of the command line is that it can form an execution sequence and form a script. The software under UNIX is full of command line, which gives the program development a great convenience, the advantage of the command line software is that they can be very easy to integrate together, using a few simple tools of the command, you can make a very powerful function.

So the software under UNIX is more organically integrated than the software under Windows, each exerting its own merits and combining it into a more powerful function. and the graphics software under Windows is basically the respective battalion, cannot call each other, very unfavorable to the mutual integration of various software. Here is not what to compare with Windows, the so-called "inch, ruler is short", graphical tools are not as good as the command line place. (see this sentence, I hope you never think I was "despise the graphical interface", and I contradicting)

I am writing this article based on the version of 5.1.1 GdB, so there may be some features that have been modified or more powerful. And, I write very hasty, write relatively simple, and, I have seen a lot of typos (I use Wubi, so you do not understand the typo), so, I am here on the mistakes of my words are extremely apologetic.

The GDB features listed in the article, I just listed some of the use of GDB's command and usage, in fact, I only talk about the features of the GDB only about 60% of all the features, detailed documentation, or please see GDB's help and manuals, perhaps, over time, if I am free, Let me write another article about GDB's advanced use.

I personally like GDB's automatic debugging function, this function is really very powerful, 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-automation, sincerely expect the automation of GDB debugging function of maturity.

If you are interested in GDB or any other technical issues, you are welcome to discuss the exchange with me. I am currently mainly in the UNIX under the development of product software, so, UNIX software development is familiar with, of course, not only technology, software engineering implementation, software design, system analysis, project management I also have a little experience. Welcome everyone to contact me, (QQ is: 753640,MSN is: haoel@hotmail.com)

<-previous Page

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

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.