如何使用 gdb(通过 Xcode)打印和使用常量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/864729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print and use constants with gdb (through Xcode)?
提问by phi
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
我正在使用 xcode-gdb 调试 Cocoa 应用程序。我正处于断点处,我想查看一些 Cocoa 常量(即 NSControlKeyMask)的值,并对当前堆栈帧中的值进行一些测试。具体来说我在
- (void) keyDown:(NSEvent *) e,我已经完成了
set $mf = (int)[e modifierFlags]在 gdb 提示符下。现在我想做
p $mf & NSControlKeyMask并且 gdb 告诉我“当前上下文中没有符号“NSControlKeyMask”。
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.
更新:
Xcode 具有“修复并继续文本”功能。所以我使用了具有此功能的 Dan M. 和 n8gray 解决方案,这样我就不必为每个常量制作代理。
回答by Dan Moulding
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.
如果没有变量实际使用给定类型实例化,则相应符号的调试信息最终不会由 gcc 生成。然后,如果你向 gdb 询问这种类型,它不知道你在说什么,因为没有该类型的调试信息,它会给你“当前上下文中没有符号”错误。
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;
}
Save that program to a file named test.cpp and compile it with this command:
将该程序保存到名为 test.cpp 的文件中并使用以下命令编译它:
g++ -o test -g -O0 test.cpp
Then run it under gdb and type "p /x baz". You will get the "No symbol baz in current context" error.
然后在 gdb 下运行它并输入“p /x baz”。您将收到“当前上下文中没有符号 baz”错误。
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;
}
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.
使用与之前相同的命令编译并在 gdb 下运行。这次当您输入“p /x baz”时,您将得到“0x2”作为答案,我认为这就是您在问题中所寻求的。
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.
我已经研究过了,问题是 NSEvent.h 头文件没有给包含的枚举命名NSControlKeyMask
——它是一个匿名枚举。因此无法创建该类型的变量(虚拟或其他)。所以,我没有看到任何让编译器为该类型生成调试信息的方法。我认为您将不得不依赖NSControlKeyMask
头文件中的定义。
回答by Nathan Fellman
If you compile with gcc you can use the -g3
switch to get the highest level of debug info. Quoting from the section on -g
in the gcc manual:
如果您使用 gcc 编译,您可以使用-g3
开关来获得最高级别的调试信息。引用gcc 手册中的部分-g
:
-glevel Request debugging information and also use level to specify how much information. The default level is 2. Level 0 produces no debug information at all. Thus, -g0 negates -g. Level 1 produces minimal information, enough for making backtraces in parts of the program that you don't plan to debug. This includes descriptions of functions and external variables, but no information about local variables and no line numbers. Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro expansion when you use -g3.
So if you compile with -g3
you shouldbe able to expand constants and macros in gdb.
所以如果你用编译,-g3
你应该能够在 gdb 中扩展常量和宏。
回答by n8gray
As Dan M. discovered, you probably can't get this to work in a straightforward way. Instead, what you could do is put something like this in one of your files:
正如 Dan M. 发现的那样,您可能无法以直接的方式使其工作。相反,您可以做的是将这样的内容放入您的一个文件中:
int myNSControlKeyMask = NSControlKeyMask;
int myNSOptionKeyMask = NSOptionKeyMask;
...
Then at least you can use symbolic names in gdb without having to look up the corresponding values in the .h file.
那么至少你可以在 gdb 中使用符号名称而不必在 .h 文件中查找相应的值。
回答by sigjuice
NSControlKeyMask is most likely a macro and invisible to the debugger. You need to look in the appropriate .h file. Place the cursor over the text NSControlKeyMask in the editor and try command+double-click to jump to its definition.
NSControlKeyMask 很可能是一个宏并且对调试器不可见。您需要查看相应的 .h 文件。将光标放在编辑器中的文本 NSControlKeyMask 上,然后尝试命令+双击以跳转到其定义。
回答by Andres
I seem to be getting the same problem in a bunch of C++ code that is being called from Obj-C in an iPhone app. It's giving me the
No symbol "a" in current context.
error, where a is an int. I tried the -g3 compiler flag with no success. I find it hard to believe gdb doesn't know the type of an int. SDK 3.0, but then again, gdb was printing completely erroneous values when it could find variable in the program.
我似乎在 iPhone 应用程序中从 Obj-C 调用的一堆 C++ 代码中遇到了同样的问题。
在当前上下文中,它给了我No 符号“a”。
错误,其中 a 是 int。我尝试了 -g3 编译器标志但没有成功。我发现很难相信 gdb 不知道 int 的类型。SDK 3.0,但话又说回来,当 gdb 在程序中找到变量时,它打印出完全错误的值。