Watching variables in Xcode - objective-c

I'm trying to watch a variable with Xcode. I'm following the instructions in here by pausing at a breakpoint, selecting Run > Variables View > .... but with the exception of "Enable Data Formatters" the rest of the options are all greyed out. Any ideas?
I'm using Xcode version 3.1.3.

I haven't gotten watchpoints created from the Run menu to work for me either, unfortunately. One thing to be aware of is that when a variable goes out of scope, the watchpoint may become invalid.
If you don't mind getting a little more in-depth, you can use some low-level gdb commands to set a watchpoint for the address of the memory itself. For example, in the guide you linked to, they show how to watch the variable path which is a pointer with the value 0xbfffeb70. To manually set a watchpoint for that address, click in the debugger console (where the debugging output is printed) after the "(gdb)" prompt and type something like this:
watch *((int*)0xbfffeb70)
The cryptic syntax is necessary because gdb expects inputs as C expressions. For a little more detail, visit this link and jump to the section titled "Using hardware watchpoints". (I'm testing on an Intel machine, not sure how PowerPC handles it.) When you set watchpoints this way, Xcode will alert you with a drop-down sheet when a watchpoint is reached and tell you how the value was changed, and gdb will print the same info in the console.

I just ran into this problem. Here is a solution: right click on the variable name and select "View variable in window" from the menu which appears. It should be near the bottom.

Add a breakpoint. Right click in the watch list of the debug area and choose "Add expression..."
If you are getting a different menu, you have to click off of the currently highlighted variable so that nothing is highlighted when you right click.

The Answers given here only work if you use the gdb compiler. For those of you who are looking for an option to set a watchpoint with the lldb compiler I have bad news:
It's not working jet (XCode 4.3.2 with lldb 3.1) even though the lldb docs say you can.
Check out this Email. The lldb commands compared to the gdbs can be found here

I was trying to figure this out in XCode 5. I finally found a "Variables view" button at the bottom right of the output console. It's the little rectangle that will be gray on the left, white on the right if it's not enabled. I'm not sure if this is in XCode 3, but I expect most people have upgraded anyway.

Related

How do you watch or evaluate an expression in xcode like visual studio's immediate window?

In MS visual studio we just right click add watch.
How does one do this in Xcode?
Use the po command in the Debug area
Set up a breakpoint on the relevant area of code, then when the program stops at the breakpoint, enter commands in the Console in the Debug Area.
The relevant command is po (print object) followed by the expression you want to evaluate.
If the Debug window is not visible in XCode, you can show it via the top menu:
'View' -> 'Debug Area' -> 'Activate Console' (XCode v8.x)
Example
To evaluate an expression like var1/var2 where var1 and var2 are both doubles, enter the following in the Console:
po var1/var2
The Console will return something like:
(double) $2 = 3.085 [no Objective-C description available]
Showing object properties
You can also return a particular property of an object currently used in the code at that breakpoint:
po [bankInfo city]
And it will return something like:
(id) $4 = 0x000069e8 Testville
Note though that the Console doesn't seem to like the dot notation and prefers the square brackets when applicable. For example, this returns an error for me:
po bankInfo.city
I hope this is what you've been looking for.
Gabe's answer is almost there but missing one crucial detail: Select Debugger Output . By default the bottom option is set to Target Output, so the po command doesn't show you anything.
Here is a sandwich app from a tutorial I'm debugging:
Being an xcode newbie and coming from a MS Visual Studio Background, I wanted exactly what the OP was looking for. While playing around from reading Gabe's answer I selected Debugger Output and got what I wanted.
My seniors told to use NSLog(#variable)..........
Set some breakpoints in the begginning of the looping and functions. Once u click on the breakpoint(one similar to arrow) button in the editor window the "Build and debug tool" will get enabled. You can then go to the debugger by clicking the debugger icon. on the right of the debugger window variables will be visible select self->then the instance variable u r going to set watch point.Right click on that and select "watch variable".A trigger point will be set and you will be notified with the value of the variable when changed.
As fas as i understand you would like to see when a variable is changing. For this make a breakpoint and right click on it and choose Edit Breakpoint. A window will appear:
Make sure you choose the right action like Debugger Command or Log Message and check the tick down at the options Automatically continue after evaluating. So you get some kind of action (e.g. logging, sound, etc) and wont stop at the breakpoint.
If you want to know when the variable changes, use a "watch":
Set a breakpoint somewhere in the class in question;
Run the app in the debugger and let it stop at your breakpoint; and
Go to the "Variables" view in the left side of the bottom "Debug" panel and right click on the property in question and choose "Watch".
For example, here, I've stopped at a breakpoint in viewDidLoad, and added a "watch" for total:
(This is Swift, but the same is true for Objective-C, too.)
Now, if I "continue" execution (), Xcode will pause whenever this property changes and I can see at what line of code total is changing, the stack trace to get to that point, etc.

View variable data while debugging in Objective-C

I come from a .Net world so I'm used to just hovering over a variable while debugging and seeing what its value is.
In Objective-C I am incredibly confused on how to do that.
If I hover over it, I get a small popup with lots of information...that doesn't help me at all.
For example, I have an object called "myServer" and it is an instance of a "Server" that I have created through CoreData. One of its properties is "root" which is a simple NSString.
I cannot for the LIFE of me figure out how to view what the value of [myServer root] is.
Can some please give me some advice on this?
In the gdb console, type
po [myServer root]
I like to use GDB from the command line. Open a terminal and type
gdb
attach <your process name>
(be sure your program was built with debugging symbols). Then, when your variable name is in scope (e.g. when you break somewhere relevant) type
po variableName
to view its contents.
Another nice way to deal with this is to log directly from a breakpoint.
To do this, create a breakpoint after the value you want to see has been set, then edit it. Add a breakpoint action of 'log', and put the expression you want logged within a pair of # symbols. Check the box to the right, ensuring that the breakpoint doesn't actually cause a stop. The value will be output to the debugger console on doing a run & debug.
Doing it this way you (a) don't clutter your source, (b) can dis/enable the breakpoint at will according to your immediate needs, and (c) don't need to stop execution.
This and other very handy xcode tips can be culled from Joar Wingfors' 'Debugging with Xcode' talk.

Xcode missing inline test results

Everywhere there are pretty pictures of failing tests shown inline in the code editor, like in Peepcodes Objective-C for Rubyist screencast and in apples own technical documentation:
(source: apple.com)
When I build my test-target, all I get is a little red icon down in the right corner, stating something went wrong. When clicking on it, I get the Build Results, where I can start to hunt for test results.
Do anyone have a clue on what´s wrong?
Have a look at your Xcode preferences. Under the Building tab you want to change your settings for Message Bubbles.
This works for Xcode 3.1 which it looks like the image you've shown. Xcode 3.2 has a different style of bubble and doesn't have this preference.
Press "Cmd =" to travel between build results, you should see a warning.
Also, that specific warning seems like it's from the static analyzer - you turn that on by going to project preferences and checkmarking "run static analyzer" or by using the "Build and Analyze" option.
Try Cmd-Shift-H to show all message bubbles.

How to use NSzombie in xcode? [duplicate]

I have an app that is crashing with no error tracing. I can see part of what is going on if I debug, but can't figure out which object is "zombie-ing".
Does anybody know how to enable NSZombie in Xcode 4?
Environment variables are now part of the "scheme".
To edit the scheme and turn on zombies:
In the "Product" menu, select "Scheme" > "Edit Scheme...".
Go to the "Run Foo.app" stage in the left panel, and the "Arguments" tab on the right.
Add NSZombieEnabled to the "Environment Variables" section and set the value to YES, as you could in Xcode 3.
In Xcode 4.1 and above, there's also a checkbox on the "Diagnostics" tab of the "Run" stage to "Enable Zombie Objects".
With Xcode 6.4:
I encountered the same problem with troubleshooting EXC_BAD_ACCESS and had hard time to find the setting with Xcode 4.2 (the latest one that comes with iOS5 SDK). Apple keeps on moving things and the settings are no longer where they used to be.
Fortunately, I've found it and it works for the device, not just Simulator. You need to open the Product menu in the Xcode, select Edit scheme and then choose the Diagnostics tab. There you have "Enable Zombie Objects". Once selected and run in debugger will point you to the double released object! Enjoy!
In short
Product->Edit Scheme->Diagnostics-> Click Enable Zombie Objects
Product > Profile will launch Instruments and then you there should be a "Trace Template" named "Zombies". However this trace template is only available if the current build destination is the simulator - it will not be available if you have the destination set to your iOS device.
Also another thing to note is that there is no actual Zombies instrument in the instrument library. The zombies trace template actually consists of the Allocations instrument with the "Enable NSZombie detection" launch configuration set.
It's a simple matter of setting an environment variable on your executable (NSZombieEnabled = YES), and then running/debugging your app as normal.If you message a zombie, your app will crash/break to debugger and NSLog a message for you.
For more information, check out this CocoaDev page: http://www.cocoadev.com/index.pl?NSZombieEnabled
Also, this process will become much easier with the release of 10.6 and the next versions of Xcode and Instruments. Just saying'. =)
Product > Profile will pop up Instruments. Select zombies from the panel and go nuts.
Go to Product - Scheme - edit scheme - Arguments - Environment Variables set NSZombieEnabled = YES
In xcode 4.2
Goto, Product -> edit scheme -> click Run yourappname.app -> Diagonostics -> Enable Zombie object.
Here's a video and explaination how to use Instruments and NSZombie to find and fix memory crashes on iOS:
http://www.markj.net/iphone-memory-debug-nszombie/
As of Xcode 3.2.5 and Snow Leopard (Mac OS X 10.6), you can run your code through the Zombies instrument: Run > Run with Performance Tool > Zombies. That allows you to see particular objects and their retain counts on a timeline.
In Xcode 4.5.2 goto Product -> Edit Scheme -> and Under the Diagnostics tab check the check box in between Objective C and Enable Zombie Objects and Click on OK
To enable Zombie logging double-click the executable in the executables group of your Xcode project. At this point click the Arguments tab and in the Variables to be set in the environment: section, make a variable called NSZombieEnabled and set its value to YES.
In XCode 4.0: To detect NSZombie in Instruments, select the Simulator as your target (can't detect NSZomboe on device). Run Instruments (CMD+I) and select "Zombies" trace template. Enjoy.
In the preferences of your executable add the environment variable NSZombieEnabled and set the value to YES.
in ur XCODE (4.3) next the play button :) (run)
select : edit scheme
the scheme management window will open
click on the Arguments tab
you should see : 1- Arguments passed on launch
2- environment variables
inside the the (2- environment variables) place
Name: NSZombieEnabled
Value: YES
And its done....
NSZombieEnabled is used for Debugging BAD_ACCESS,
enable the NSZombiesEnabled environment variable from Xcode’s schemes sheet.
Click on Product⇒Edit Scheme to open the sheet and set the Enable Zombie Objects check box
this video will help you to see what i'm trying to say.

Watching variables in SSIS during debug

I have a project in SSIS and I've added an Execute SQL Task which sends its result out to a variable. I wanted to confirm the value because I was worried that it would try to write it out as a resultset object rather than an actual integer (in this case I'm returning a COUNT).
My first thought was just to run it in debug mode and add the global variable to my Watch window. Unfortunately, when I right-click on the Watch window, the option to "Add Variable" is greyed out. What am I missing here?
I've gotten around confirming that my variable is set correctly, so I'm not interested in methods like putting a script in to do a MsgBox with the value or anything like that. For future reference I'd like to be able to watch variables in debug mode. If there are some kind of constraints on that then I'd like to know the what and why of it all if anyone knows.
The help is woefully inadequate on this one and every "tutorial" that I can find just says, "Add the variable to the Watch window and debug" as though there should never be a problem doing that.
Thanks for any insight!
I believe you can only add variables to the Watch window while the debugger is stopped on a breakpoint. If you set a breakpoint on a step, you should be able to enter variables into the Watch window when the breakpoint is hit. You can select the first empty row in the Watch window and enter the variable name (you may or may not get some Intellisense there, I can't remember how well that works.)
Drag the variable from Variables pane to Watch pane and voila!
I know this is very old and possibly talking about an older version of Visual studio and so this might not have been an option before but anyway, my way would be when at a breakpoint use the locals window to see all current variable values ( Debug >> Windows >> Locals )
Visual Studio 2013: Yes to both adding to the watch windows during debugging and dragging variables or typing them in without "user::". But before any of that would work I also needed to go to Tools > Options, then Debugging > General and had to scroll right down to the bottom of the right hand pane to be able to tick "Use Managed Compatibility Mode". Then I had to stop and restart debugging. Finally the above advice worked. Many thanks to the above and to this article: Visual Studio 2015 Debugging: Can't expand local variables?