Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print and use constants with gdb (through Xcode)?

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 
, and I have done
set $mf = (int)[e modifierFlags]
on the gdb prompt. Now I want to do
p $mf & NSControlKeyMask
and gdb is telling me 'No symbol "NSControlKeyMask" in current context.'

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.

like image 380
phi Avatar asked May 14 '09 17:05

phi


People also ask

What is print command in GDB?

The usual way to examine data in your program is with the print command (abbreviated p ), or its synonym inspect . It evaluates and prints the value of an expression of the language your program is written in (see section Using GDB with Different Languages).

How to see variable values in Xcode?

When your app pauses at a breakpoint, hover over a variable in your source code to view its current value. If the variable is an image or other type that isn't expressible as text, click the Quick Look button at the upper-right to see a preview of the variable.

How to watch a variable in Xcode?

When you find your variable, right-click on it and choose “Watch”. Once that's done you can continue your program as normal, and anywhere the variable is read from or written to Xcode will pause and you can use the debug navigator to step through the call stack to figure out what happened.

Does Xcode have GDB?

Since Mavericks 10.9, Xcode 5 no longer installs gdb by default and not globally. For general notes about installing FPC and Lazarus see Installing Lazarus on macOS. About using gdb see GDB Debugger Tips.


1 Answers

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;
}

Save that program to a file named test.cpp and compile it with this command:

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.

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.

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.

like image 94
Dan Moulding Avatar answered Sep 28 '22 16:09

Dan Moulding