GDB: Create local variable?

南笙酒味 提交于 2021-02-07 18:42:38

问题


I'm using Xcode's debugger. While stopped at a breakpoint, is there a command I can type in the GDB command prompt to create a local variable? If so, how? Please provide an example.

I know I can do it in the code and then recompile the program, but I'm looking for a faster way.


回答1:


It's mentioned in the comments, but not as an answer: if you don't need to reference the variable in your code but just want to do some ad-hoc investigation, you can use Convenience Variables Start your variable with a $:

(gdb) set $foo = method_that_makes_something()
(gdb) set $bar = 15
(gdb) p $bar
$4 = 15

You'll notice when you print things it's prefixed with a numeric variable - you can use these to refer to that value later as well:

(gdb) p $4
$5 = 15

To reiterate: this doesn't actually affect the program's stack or anything, as that would break a lot of things, but it's useful if you just need a local playground, some loop variables, etc.




回答2:


Since a local variable would require stack space and the (compiled) code is tied to the stack layout, no you can't.

Comparing this with scripting languages is not quite appropriate.




回答3:


Values printed by the print command are saved in the GDB "value history". This allows you to refer to them in other expressions.

For example, suppose you have just printed a pointer to a structure and want to see the contents of the structure. It suffices to type

p *$


来源:https://stackoverflow.com/questions/8275135/gdb-create-local-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!