How to print value of a constant/macro while debugging in Xcode? - iphone

While debugging an iOS application, I know how to print values of objects using :
print "variable name"
po "variable name"
p "integer Variables"
I wanted to know how to print value of a constant while debugging in Xcode?
Is there any command that prints value of a constant? Because, if I use the above commands, the Xcode returns an error saying
error: use of undeclared identifier
Thanks.

Macros (what you get when you #define something) are the domain of the language preprocessor. They are expanded and the expanded value is used when compiling your code.
The debugger doesn't parse your source file, it works off of what's in the binary. So no, you won't be able to view the value of #define macros in the debugger.

Old question, but nowadays compiling with -g3 (GCC) or -fdebug-macro (Clang) will generate debug information for such preprocessor macros.

Related

Add shortcuts for LLDB debugger in Xcode for Swift debugging [duplicate]

I have a large Swift project that's technically a mixed project, as it has a small amount of Objective-C code. But whenever I drop into LLDB, the expression evaluator is expecting Objective-C syntax. I can use Swift with e -l swift -- but this is tedious to type every time.
Can I default the LLDB expression evaluator to Swift?
There is a target level setting to force the language:
(lldb) settings set target.language swift
Or you can make an alias for swift specific expressions:
command alias es expression -l swift --

Setting a symbolic breakpoint with Swift and lldb

How does one set a symbolic breakpoint in lldb when using Swift? For example, sometimes I use:
(lldb) b set -F '-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded]'
But this no longer works:
Breakpoint 2: no locations (pending). WARNING: Unable to resolve
breakpoint to any actual locations.
I've also tried
(lldb) b set -F 'UIView.updateConstraintsIfNeeded()'
(lldb) b set -F 'UIView.updateConstraintsIfNeeded'
But no love. I guess the question comes down to what lldb considers a "fully qualified function name" when using Swift. The docs say:
-F ( --fullname )
Set the breakpoint by fully qualified function names. For C++ this
means namespaces and all arguments, and for Objective C this means
a full function prototype with class and selector. Can be repeated
multiple times to make one breakpoint for multiple names.
What about Swift?
When lldb is setting breakpoints any of the fancier matching breakpoints (including -F), it needs to know the target language since the kinds of matching it does depends on that. By default, lldb chooses the language of the current frame, but you can override that. So for instance to break on an ObjC symbol when you are stopped in a Swift frame, do:
(lldb) break set -F '-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded]' -L objc
Breakpoint 3: where = UIKit`-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded], address = 0x0000000100963acc
Or you can use the -n break option, which doesn't try to understand the symbols it matches, but just does a wide search for that string in the symbol names.
Note, in this case you need to break on the ObjC symbol not the way it appears in Swift, because the swift side is really just a shim into objc, and there isn't really a swift side symbol to hook onto.
In the lldb console, you can give a partial name and get automatic lookup using regex:
br set -r updateConstraintsIfNeeded\]
The result is:
Breakpoint 4: where = UIKit`-[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded]
In current versions of Xcode, you can also use a partial name in the "add a symbolic breakpoint" UI, because there is now code completion to assist you.

Netbeans didn't catch error for function that should have return something, not having a return statment

I was surprised to find the compiler gave no warnings or errors when compiling source code containing a function that specified a return value in its signature but didn't actually return anything
e.g.
int foo()
{
}
How can Netbeans be beefed up so it caches these things?
According to this question this is undefined behaviour but I would've thought this is something easy for an IDE to check for.
Also, is there a way to have it displays number of warnings at the bottom of the output window after compiling? For example the way it is by default, a warning could be buried in the amount of irrelevant information and easily missed.
How can Netbeans be beefed up so it caches these things?
Right click to project -> C++ Compiler set Warning Level to More Warnings (below Basic Options).
(Same applies to C Compiler settings)
Or:
Add -Wall to compiler flags.
For a maximum of warnings you can add these: -Wall -Wextra -pedantic
Also, is there a way to have it displays number of warnings at the bottom of the output window after compiling?
I guess there's no number yet, but possible this can help you: How to display all compile errors in Netbeans as a task list?

Getting the warning " ISO C90 forbids variable-size array" in gcc compiler while compiling C90 code

I am compiling my C90 c code in gcc . I am getting the warningISO C90 forbids variable-size array while making the declaration like
int symbols[nc];
Where nc is integer whose value is read from the input file. The values on the input files are varied so i can't keep a constant value. How can I get rid of it? Is it indeed necessary to resolve this warning or we can simply ignore it?
Thanks in advance.
You get that warning because C90 does not support variable length arrays.
You'll either have to switch gcc to C99 mode (which does support vla) , by using the -std=c99 or std=gnu99 command line flag, or rewrite your code to dynamically allocate memory or use a fixed size array.
The warning just tells you that you're not conforming to C90 in this case, but it's otherwise safe. Ignoring a warning should really not be an option though.

iPhone debugging: variables are not up to date?

I've only recently began using the debugger extensively, so I'm not sure if this is a limitation.
When I debug on the iPhone, the variables aren't up to date unless I explicitly view it (ctrl+click -> view variable as expression). Is there a way to view actual variables without viewing explicitly?
Can you clarify your question? You should only be viewing data while the program is stopped -- examining data while it's running, if it works at all, is much less useful. Make sure to set a breakpoint, and then examine data once you've hit the breakpoint.
An alternative to using Xcode's built-in debugging features is to use the gdb console. Type ⌘-Shift-R, or select "Debugging Console" from the menu to open the console. Then, you can type commands like:
# View a variable
print var
# View this object's member variable
print self->memberVar
# Ask an Objective-C object to print itself:
print-object self
You should use print with primitive types (int, char*, etc.) and POD types (structs); you should use print-object with Objective-C objects (NSString, etc.). For more information about print and print-object, type
help print
help print-object
You can also use the abbreviations p and po for print and print-object respectively.