Setting Variables from Within GDB/DDD/Eclipse

In some cases, it is useful to set the value of a variable using the debugger, in the midst of stepping through a program. This way you can quickly answer "what if" questions that arise when you are hypothesizing various sources for a bug.

In GDB, you can set values very easily, for example,

(gdb) set x = 12

will change the current value of x to 12.

There appears to be no mouse-based way to do this in DDD, but again you can issue any GDB command from within DDD in the DDD Console Window.

In Eclipse, go to the Variables view, right-click the variable whose value you want to set, and select Change Value. A pop-up window will then allow you to fill in the new value.

You can set the command-line arguments for your program via GDB's set args command. However, there is no advantage to this over the method described in Chapter 1, which simply uses the new arguments when you invoke GDB's run command. These two methods are completely equivalent. It is not the case, for instance, that

(gdb) set args 1 52 19 11

would immediately change argv[1] to 1, argv[2] to 52, and so on. Those changes would not occur until the next time you issue the run command.

GDB has the info args command, which you can use to check the arguments of the current function. DDD provides this when you click Data | Display Arguments.