Viresh Singh


XCode Advance Debugging

Have you ever stuck in situation while fixing some bug or even during the development that for simulating some scenario in code, you modify the existing code temporarily to achieve the scenario?

Well this is very common and what cost you more is to recompile the code and to restart the debugging to make the modified code take affect.

This becomes even severe when again and again you need to modify something and restart the debugging again and again.

If your app is a simple app, it might not be a problem for you but consider an app which requires login and you need to navigate though various screen and do various action to actually reach the situation or screen where actually your modified code can be executed.

You can achieve this without recompiling & restarting your debugging and within same debugging session, yes you can achieve that!

Lets try it. Consider the below code where I need to change the value of flag just to enter the first if condition to debug some issue.

print("Hello, I am Inside If")
print("Hello, I am Inside Else")
  1. Just put a breakpoint on if condition and Run the application.

2. Once breakpoint is hit, right click the breakpoint and select Edit Breakpoint.

3. Tap on Add Action, and enter the below code in Debugger Command field. (If debugger command option is not by default select in combo box, change it and select Debugger Command manually and enter the below code)

expression flag = true

4. Also, check the option ‘Automatically continue after evaluating Actions’. Selecting this option will allow debugger to continue after evaluating the expression provided without pausing the control even when breakpoints are enabled. You can also choose to keep it unselected.

5. Continue the paused breakpoint for execution, and from next execution onwards, this flag will be automatically set to false as per the above expression without pausing for breakpoint and even without re-compiling the code.

This was just a very simple example of how to insert an expression with breakpoints. Real life situation may not just depend on one flag, probably you need to insert multiple statements to be executed.

You can even configure multi line expressions for more complex scenarios but just remember each line of code should have expression preceded.

For example, if I just want to print a log whenever value of this flag is changed by this custom breakpoint, I can update my Debugger command to:

expression flag = false
expression print("Value of Flag changed to false by Debugger")

Once you run again, log will be printed each time the statement is executed.

Skipping the Set of Instructions:

Apart from inserting the instructions, you can also configure your breakpoint to skip a line or number of lines temporary for debugging. This may be required when you want to skip an instruction or to replace a instruction by some other temporary instruction or for changing the values passed to a method call.

Say, we have below code

func someOtherMethod(){
//Some other code
//Some other code
func sayHello(to name:String)
print("Hello \(name)")

Now, while calling sayHello method, we want to call it with some other value and not “Viresh”. So, to do so, we can actually skip this instruction and insert a new instruction with new value.

  1. Just put a breakpoint onself.sayHello(to:”Viresh”) and run your app.
  2. Once breakpoint is hit, right click and Edit breakpoint and select Add action.(The way we just did above).
  3. Add the below expression in Debugger Command.

This statement will cause the debugger to skip the instruction where this breakpoint was applied.

5. Now add a new debugger command to actually execute in place of skipped instruction. Just press + available in Action row of edit breakpoint pop up and add the instruction. Whole breakpoint should look like below:

6. Just continue the breakpoint and you will see the changes take effect without recompiling or without restarting the debugging.

So, what is happening here is that we are replacing the current instruction by a new instruction for this debugging session.

Few important point to remember:

  • You can add multiple lines of code in debugger command either in same debugger command or by adding multiple debugger commands.
  • While editing the breakpoint, debugging should be active and should be paused on the breakpoint you are editing.
  • If you disable the breakpoint, instructions provided in debugger will not take effect, you can see them action only when breakpoints are enabled.

Happy Debugging!

More by Viresh Singh

Topics of interest

More Related Stories