7

Is there a way to test if a convenience variable has been set in gdb?

For example:

(gdb) if $_exitcode == 0
 >quit
 >end
Invalid type combination in equality test.
(gdb) p $_exitcode
$1 = void

$_exitcode is void because it is not set until the program terminates. The closest available construct is init-if-undefined, but this would require setting the variable to some sentinel value and testing against that.

4

Since normal process exit code is somewhere in between 0 and 255, I suggest following:

init-if-undefined $_exitcode = -1
if ($_exitcode != -1)
  quit
end
2

Im having the same problem.. you can't check if a variable has been set or not as far as I know in GDB.. you could run it through python, probably, and have the whole script run that way, but I am unsure of it the python scripts in GDB are persistent or running all the time. You could do something like..

init-if-undefined $_exitcode = 1
if $_exitcode == 0
quit
end
end
0

You can define a command in gdb do do what you want as shown:

(gdb) define CheckDefined
Type commands for definition of "CheckDefined".
End with a line saying just "end".
>set $CheckDefined_DefinedOr1 = $arg0
>init-if-undefined $CheckDefined_DefinedOr1 = 1
>set $CheckDefined_DefinedOr2 = $arg0
>init-if-undefined $CheckDefined_DefinedOr2 = 2
>set $arg1 = ($CheckDefined_DefinedOr1 == $CheckDefined_DefinedOr2)
>end

Now that you have defined CheckDefined, you can use it as shown, to check whether a given convenience variable, in this case $fluffy, is defined, storing the result of the check in $fluffyIsDefined:

(gdb) CheckDefined $fluffy $fluffyIsDefined
(gdb) print $fluffyIsDefined
$17 = 0

If you now define the previously undefined variable, $fluffy, CheckDefined will yield a different result:

(gdb) set $fluffy = 92
(gdb) CheckDefined $fluffy $fluffyIsDefined
(gdb) print $fluffyIsDefined
$18 = 1

This use of the function has the advantage that you don't have to clobber the variable that you want to check. So to rewrite your original construct:

(gdb) CheckDefined $_exitcode $exitCodeIsDefined
(gdb) if (! $exitCodeIsDefined)
 >quit
 >end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.