简体   繁体   中英

Why gdb “set” command doesn't recognize global variable, must add “::” or “variable”?

I was trying to set value in gdb, I have:

int i=0;
int main(){
  ++i;
  int j=i+2;
  return 0;
}

break at "return", and "r"

(gdb) set j=4
(gdb) set i=5
Ambiguous set command "i=5": .
(gdb) set ::i=6
(gdb) set variable i=6

It's weird, "j" is local variable in main, so "set" with no problem. While "i" is a global one, seems I should add either "::" or "variable" to make it set.

I searched internet, and it says "variable" is used to set gdb/reg variables used inside debugging sesssion.

Why "i" still requires "variable"?

Your example is:

(gdb) set i=5
Ambiguous set command "i=5": .

This is not a name lookup error but rather the consequence of how gdb parses commands. (That trailing : . looks like it ought to mean something, or list something; but it just seems odd.)

set can be used to evaluate an expression, as you discovered with set j = 4 -- but this only happens because there is no gdb command starting with set j .

That is, set tries any matching subcommands first, recognizing abbreviations. And since there are multiple commands starting set i (such as set inferior-tty , set input-radix , ...), gdb doesn't know which to choose. So, it complains that the command is ambiguous.

The real command to set a variable is set variable , which is why the final try worked.

Note that other commands taking an expression can also evaluate an assignment. I tend to use p (aka print ) interactively rather than set variable just because it is shorter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM