Can you change the current line of execution while debugging in Xcode? - xcode

The debugger lets me drag the little green line pointer, but it seems it has no effect at all and when I step it behaves as though I never dragged it.
Just wondering if the bug is:
That it's not correctly changing the current execution pointer, or
That it's allowing me to drag the thing at all.

Xcode changed from being able to drag the instruction pointer from the left, to a completely new visual item and moved it to the far right.
The hamburger icon (shown in the image below) is actually the drag handle for the current instruction pointer.

Can you change the current line of execution while debugging in Xcode?
YES, it can be changed.
It works, after you step over, but from next click.
See this example:
Assume the Green arrow is in "a"
>> NSLog(#"a");
NSLog(#"b");
NSLog(#"c");
NSLog(#"d");
NSLog(#"e");
NSLog(#"f");
It goes to some stack that will be printed when you "Step Over". Then it moves to "b".
as:
NSLog(#"a");
>> NSLog(#"b");
NSLog(#"c");
NSLog(#"d");
NSLog(#"e");
NSLog(#"f");
Then "b" goes into stack, and it will get printed. Now you change the >> to "e" by dragging, as:
NSLog(#"a");
NSLog(#"b");
NSLog(#"c");
NSLog(#"d");
>> NSLog(#"e");
NSLog(#"f");
Now "b" will get printed and the pointer will move to next statement i.e. "e".
If you again move it back or forward it will change its place but "e" will be printed, as it is already saved in some stack that is ready to print.

Related

Setting a breakpoint in the middle of a line with multiple statements

I want to have a single line of code with an if and the result on the same line. Example
if(_count > 0) return;
If I add a breakpoint on the line, it is set on the if(_count > 0):
but what I want is to have the breakpoint on the return; like so:
Is this doable?
NOTE: The closest question I could find on SO was this (which is not the same thing).
Just click on a part of the line and press F9. This will set breakpoint in the middle of the line.
EDIT
mareko posted a 3rd option that seems to work just fine and is much easier than messing around with all this line-and-character stuff. I'm going to leave this answer for completeness, but mareko's is much more practical in general.
Yes, but unfortunately, there is no drag-and-drop or anything like that. You have 2 options:
OPTION 1
Move your return to a new line:
obviously, you can leave it like that, but if you want to keep the return on the same line as the if, you can simply delete the new line and the whitespace between the if and the return -- the breakpoint should "stick" to the return as you move it around.
This is probably the easier way to do it unless you are currently debugging code that does not have edit-and-continue for whatever reason. In that case, you'll need option 2...
OPTION 2
Alternately, you can place the cursor just before the r in 'return' and then look in your status bar to see which character ("Ch") you are on. In my case, I'm on 20
now right click on the breakpoint and choose "Location..."
In the dialog box that pops up, set Character: to whatever the status bar was (20 in our example).

Is it possible to record variable changes?

I am using Unreal Engine 4 and there is a bug where the mouse is not constrained to the window.
I am trying to debug the mouse position, but it am not sure how I would do it.
Every time I set a break point at the mouse position visual studio will pause the application, it is just really hard to debug because there are so many pauses.
I am wonder if it is possible to record variable changes? For example set a special break point at a variable and visual studio will only very briefly pause the application, look at the variable and put the value into a list.
I could just log the mouse position, but I want to get away from using log all the time.
This can be done by using the "When Breakpoint is Hit" window. Set the breakpoint, right-click it and choose "When hit..."
On this window, check both the "Print a message" and the "Continue execution" checkboxes. Then update the string you want to print to include the value(s) that you want printed (variable values and expressions needs to be wrapped in {} to be interpreted as such. Setting "Continue execution" makes the debugger not break at the line, instead, it will just log and continue. The messages are printed to the Debug >> Output window at runtime.

Going back to previous line while debugging on visual studio

I am debugging a piece of code on visual studio and I forgot to note down the values I have kept a watch on. Can I go to previous line without rerunning the entire code?
There are similar questions asked on SO but in my case i haven't run through any error or exception. The code is running normally.
After pausing on a break point, right click on the line you want to "go back" to. From the menu that pops up, select "set next statement".
This will adjust the instruction pointer to continue from the specified line of code, but it will not roll back any variables or memory addresses to the values they were at before that line of code was originally executed.
It sounds like what you want to do is rewind / replay your code rather than just move to a specific line. You can move to a specific line, you can just right click and choose set next statement. Unfortunately, this won't rewind the state of the program to some past point (beyond setting the stack and doing a bit of unwinding).
To rewind/replay you need to be a bit trickier. Some options are: -
VMWare replay which will allow you to record and then go back to a certain point in time.
Intellitrace. I haven't tried it, but it allows you to replay to a point.
Which is a bit heavyweight and wont help you right now.
You can use the mouse to drag the yellow arrow pointing to the "next statement to be executed." This actually changes which statement will be executed next. It's not guaranteed to work, but as long as the code is not too complex, it could.

Is there any way to scroll beyond the last line in xcode?

I ask if there is any way to scroll beyond the last line in xcode, because I always end up pressing a lot of enters to the end of my files, so that I don't have to always type my code at the bottom of my window.
I have skimmed through the properties panel of XCode, and had a look at this forum. So far the answer looks negative, but I kept my hopes up. :)
Any ideas for tricking XCode to scroll beyond the last line? (I have XCode 4.5.2)
There is now a plugin for that !
Have a look at SCXcodeEditorInset !
Dont think there is a way to do this but if you are trying to edit the buttom of the code while having it near the top of your display, you can click the middle view button in xcode to bring up the console/watcher and then drag that up to the desirable spot. This does decrease the overall space of your editor but can sometimes be useful if you prefer this or just need to do that temporarily
What I do is, When you finished typing at the bottom, before you go to top, type some unnecessary code "asdf" as error then go where you want. you can come back clicking on top right red error panel.

Watch Expression in Xcode

Say I am debugging. Say I need to know what the value of [somevariable count]
How would I do so?
If what you want to do is know the value of the expression while program execution is halted, then do something like
> p (int)[somevariable count]
in the gdb console.
Note:
People searching for the term "watch" might be expecting an answer about being able to observe when a value changes. For that question these are some answers that are more appropriate:
Watching variables in Xcode
Xcode LLDB watchpoints
Put a breakpoint on the relevant code line.
When Xcode stops on that line, in the debug area (the bottom of the screen is splitted to two parts, look at the right one, if you don't see the bottom part, shift+cmd+Y, plus sometimes the right side or the left side are hidden and there are small buttons on the right bottom side to show them), you see all of the local and global variables.
Right click (or two fingers) that debug area, and you will see a context menu with one of the options "add expression".
Type in your expression.
Note:
above previous user's comment about the word "watch" is pretty clear to whomever comes from any other IDE but not in Xcode.

Resources