如何使用gdb打印和使用常量(通过Xcode)? [英] How to print and use constants with gdb (through Xcode)?
问题描述
我正在使用xcode-gdb调试Cocoa应用程序.我在一个转折点,我想查看一些可可常量的值(即NSControlKeyMask),并使用当前堆栈帧中的值进行一些测试.具体来说,我在
I am debugging a Cocoa application using xcode-gdb. I am at a break point and I want view the value of some Cocoa constants (ie NSControlKeyMask) and to do some test with the values in the current stackframe. Specifically I am in
- (void) keyDown:(NSEvent *) e
中,并且我已经在gdb提示符下完成了
, and I have done
set $mf = (int)[e modifierFlags]
.现在我想做
p $mf & NSControlKeyMask
和gdb告诉我在当前上下文中没有符号"NSControlKeyMask"."
and gdb is telling me 'No symbol "NSControlKeyMask" in current context.'
更新:
Xcode具有"修复并继续文本"功能.因此,我使用了具有此功能的Dan M.和n8gray解决方案,因此不必为每个常量创建代理.
UPDATE:
Xcode has the "Fix and Continue text" feature. So I used Dan M. and n8gray solution with this feature so I don't have to make a proxy of every constant.
推荐答案
如果没有使用给定类型实际实例化任何变量,则gcc不会生成相应符号的调试信息.然后,如果您询问gdb有关这种类型的信息,它将不知道您在说什么,因为该类型没有调试信息,并且会提示您当前上下文中没有符号"错误.
If no variables are actually instantiated with a given type, then the debug information for the corresponding symbols doesn't wind up getting generated by gcc. Then, if you ask gdb about such a type, it doesn't know what you are talking about because there is no debug information for that type, and it will give you the "No symbol in current context" error.
解决此问题的方法通常是在代码中的某个位置显式添加一个相关类型的虚拟变量.这是一个简单的示例,您可以测试一下以了解我在说什么:
A workaround to this problem would normally be to explicitly add a dummy variable, of the type in question, somewhere in the code. Here is a simple example that you can test to see what I'm talking about:
enum an_enum_type {
foo,
bar,
baz
};
int main (int argc, char *argv [])
{
return baz;
}
将该程序保存到名为test.cpp的文件中,并使用以下命令进行编译:
Save that program to a file named test.cpp and compile it with this command:
g++ -o test -g -O0 test.cpp
然后在gdb下运行它,并输入"p/x baz".您将收到在当前上下文中没有符号baz"错误.
Then run it under gdb and type "p /x baz". You will get the "No symbol baz in current context" error.
现在尝试使用此修改版本,该版本添加了枚举类型的虚拟变量:
Now try it with this modified version that has added a dummy variable, of the enum type:
enum an_enum_type {
foo,
bar,
baz
};
an_enum_type dummy;
int main (int argc, char *argv [])
{
return baz;
}
使用与之前相同的命令进行编译,并在gdb下运行.这次,当您键入"p/x baz"时,您会得到"0x2"作为答案,我认为这是您要解决的问题.
Compile with the same command as before and run under gdb. This time when you type "p /x baz" you'll get "0x2" as the answer, which I think is what you are shooting for in your question.
我已经调查过了,问题是NSEvent.h头文件没有给包含 NSControlKeyMask
的枚举命名,而是一个匿名枚举.因此,无法创建该类型的变量(虚拟或其他).因此,我看不到让编译器为该类型生成调试信息的任何方法.我认为您只需要依赖头文件中 NSControlKeyMask
的定义即可.
I've looked into it, and the problem is that the NSEvent.h header file doesn't give a name to the enum that contains NSControlKeyMask
-- it's an anonymous enum. So there is no way to create a variable of that type (dummy or otherwise). So, I don't see any way of getting the compiler to generate the debug information for that type. I think you're just going to have to rely on the definition of NSControlKeyMask
from the header file.
这篇关于如何使用gdb打印和使用常量(通过Xcode)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!