Talk to GdB together (sixth: GDB Change program execution Environment)

Source: Internet
Author: User
Tags gdb debugger


Ladies and gentlemen, crossing, the last time we were talking about GDB's ability to view information, and how to use GDB to see how the program executes

Information. In this one, we continue to introduce GDB's debugging capabilities: Changing the program execution environment. Of course, we'll also show you how to use GDB.

Change the program execution environment.

Gossip Hugh. Words return to the positive turn.

Let's talk gdb together!


We all know that the execution of the program requires an environment to execute. For example, allocate memory from the system, let the program read some data, and so on. We answer

Come down and say how to change the execution environment of the program. This allows the program to be executed according to our requirements. In the Change program execution Environment

The most common use is the value of the change variable.


As an excellent debugger, GBD provides the ability to change variables. This feature is implemented with special commands: Set Variable ival=0.

The example indicates that the value of the variable ival is changed to 0. Here we illustrate by example.

#include <stdio.h>int main () {    int A, b;    A = 3;    b = 5;    if (a < b)        printf ("A is not then B \ n");    else        printf ("A is larger then B \ n");    return 0;}


1. Tap the code. turn on Vim. Enter the program above and save it to the M.c file.

2. Compile the program. input in Terminal: Gcc-g m.c-o S

3. Execute the procedure.

Enter:./s in the terminal to get the following execution results:

A is and then B


You can see that a has a value of 3,b of 5, so a is significantly smaller than B. Let's say we want to look at the execution result of a greater than B. What to do? Change a

Value, make it larger than B, and then compile execution again. This crossing is right, but this method is relatively time consuming.

We can

In order to change the value of a during program execution, you can see the result of a greater than B. Detailed operations such as the following:


Input in Terminal: GDB s //using GDB debugger

(GDB) Start //Start Debugging

Temporary breakpoint 1 at 0x8048426:file M.C, line 6.

Starting program:xxx/s


Temporary breakpoint 1, Main () at M.c:6

6 A = 3;

(GDB) n //Single Step debugging

7 B = 5;

(GDB) //Enter a return. Continue single-Step debugging

9 if (a < b)

(GDB) Set Variable a=8 //Change the value of a to 8, larger than the value of B

(GDB) n //Single Step debugging

printf ("A is larger then B \ n"); //Show result of a greater than B

(GDB)


You can see the result of "A is greater than B" from the result of debugging. Through this example. We can be clear. In the course of program execution. Such as

If you want to validate a result, you can manually change the value of the variable. Let the program execute according to our requirements, and then be able to verify the execution of the program

The result is not what we want.


When the program is executing, changing the value of the variable in the program can be called a very powerful function, because it changes the executing

The value of the program, if we find the cause of the error in the program. The usual practice is to change the error and then compile the validation. With this feature, you can

The validation is changed directly during the debugging process, thus eliminating the hassle of compiling. Save time.


Crossing, here's what we're going to do about GDB today. I want to know the funeral and listen to tell.


Talk to GdB together (sixth: GDB Change program execution Environment)

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.