GDB

GDB set variable command

Manipulating the program

    Syntax:
                set variable<variable_name>=<value>
    Example:
                set variable inputvar=5

By using the set variable command in gdb, we will able to manipulating the programs.

Program

#include<stdio.h>

#define MAX_VAL 5

main()
{
int inputvar;    //Declared the variable

if( MAX_VAL == inputvar )
{
printf( "\nTRUE:Output is b=%d\n\n",inputvar );
}
else
{
printf( "\nFALSE:Output is b=%d\n\n",inputvar );
}
}

Output#1
  • Run the program with gdb "gdb sample.out -q" command
  • '-q' for disable the gdb info prints
  • Output of above program is "FALSE:Output is b=0". Here, we are not manipulating the program.

Output#2
  • Start the program with "gdb sample.out -q" command.
  • Set the break point at line number 10.
  • Run the program with 'r' command.
  • Once gdb entering into the program, we will set the variable value by using "set variable" command.
  • Now we set the variable value. i.e, set variable inputvar=5
  • Continue the program with 'c' command.
  • Now, value have changed & 'if' condition also true. So output is TRUE:Output is b=5

Note:
        When we are using the set variable command we need to hold(set breakpoint at any position) the program.