Debugging skills with Xcode ( Part 2 )

Debugging skills with Xcode ( Part 2 )

Debugger as the name implies digging into the code. Developers needs to go an extra level deeper inside the code to find out whats happening behind the scenes of the device or simulator. Xcode provides a lot of debugging feature both in the form of user interface and commands. One such feature we have already seen in the last article i.e breakpoints.

There are a lot of commands which you can find useful during debugging. In the Debug area of Xcode run the help command, It will list all the commands supported by LLDB compiler. help command in the debugger will print all the lldb commands and show on screen. Let’s talk about some of them below:

Updating the UI on breakpoint 

(lldb) print view

(UIView?) $R0 = 0x00007fdebae1a270 {

 UIKit.UIResponder = {

  ObjectiveC.NSObject = {}

 }

(lldb) e $R0!.backgroundColor = UIColor.red 

(lldb) e CATransaction.flush()

This will be helpful when you are working with designer and want to show the screen how it will look like. Alternatively you can run any command and the simulator behaves like that. e.g unhiding views e $R0!.isHidden = false

po & p (print)

Both evaluates an expression on the current thread. po returned value with formatting controlled by the type's author whereas p returns data in LLDB's default formatting. p command gives some more system information with memory address / Registers holding data as well.

(lldb) po password

▿ Optional<UITextField>

(lldb) po password.text

▿ Optional<String>

 - some : "MyPassword"

(lldb) print password

(UITextField?) $R2 = 0x00007fe553411b40 {

 UIKit.UIControl = {

  UIKit.UIView = {

   UIKit.UIResponder = {

    ObjectiveC.NSObject = {}

   }

  }

 }

}

(lldb) print lblPassword.text

(String?) $R3 = "Password"

(lldb) e -O -- password

▿ Optional<UITextField>

Expression command ‘e -O -- name’ is aliased as po (for print object)

Print Variations 

There are many different formats that you can specify for the print command. They are written in the style print/<fmt>, or simply p/<fmt>. 

Hexadecimal:  (lldb) p/x 16 Output: 0x10

Binary (the t stands for two):  (lldb) p/t 16 Output: 0b00000000000000000000000000010000

String : p/s

Character : p/c

Managing Breakpoints 

Creating Breakpoints

(lldb) breakpoint set --file ViewController.swift -l 35

Create a breakpoint in ViewController.swift file at line 35

disable/ enable breakpoints (retrieve the breakpoint id’s from the list of breakpoint. cmd breakpoint list)

lldb) breakpoint list

Current breakpoints:

1: file = '/Users/anikotha/Library/Autosave Information/Testing_Commands/Testing_Commands/ViewController.swift', line = 21, exact_match = 0, locations = 1, resolved = 1, hit count = 1

 1.1: where = Testing_Commands`Testing_Commands.ViewController.viewDidLoad() -> () + 291 at ViewController.swift:21, address = 0x00000001077843b3, resolved, hit count = 1 

2: file = 'ViewController.swift', line = 26, exact_match = 0, locations = 1, resolved = 1, hit count = 0

 2.1: where = Testing_Commands`Testing_Commands.ViewController.didReceiveMemoryWarning() -> () + 12 at ViewController.swift:26, address = 0x00000001077845dc, resolved, hit count = 0 

(lldb) breakpoint delete 2

1 breakpoints deleted; 0 breakpoint locations disabled.

(lldb) breakpoint list

Current breakpoints:

1: file = '/Users/anikotha/Library/Autosave Information/Testing_Commands/Testing_Commands/ViewController.swift', line = 21, exact_match = 0, locations = 1, resolved = 1, hit count = 1

 1.1: where = Testing_Commands`Testing_Commands.ViewController.viewDidLoad() -> () + 291 at ViewController.swift:21, address = 0x00000001077843b3, resolved, hit count = 1 

(lldb) breakpoint delete 1

1 breakpoints deleted; 0 breakpoint locations disabled.

(lldb) breakpoint disable <breakPointID>

Stepping the Flow Control of a breakpoint

n — Next Line 

s - step in

finish - step out if step in already

c - continue the program

thread return NO — If you want to come out of the function without executing the rest of the lines of code.

LLDB And Python

(lldb) script import os

(lldb) script os.system("open http://www.apple.com/“)

(lldb) script os.getpid() // To get the pid of current process. Open the activity monitor you will the lldb-rpc-server process name associated with this pid.

For more details on the os module.

http://www.pythonforbeginners.com/os/pythons-os-module

For more details on the lldb debugger. Please follow the below link:

http://lldb.llvm.org/index.html

expression and watchpoint are some of the other very useful commands. WWDC 2018 Advanced debugging video (https://developer.apple.com/videos/play/wwdc2018/412/)  also explains that.

Like
Reply

To view or add a comment, sign in

More articles by Anil K.

  • Debugging skills with Xcode ( Part 1 )

    In this article, we will focus on some of the basic utilities provided by Xcode to help us in debugging source code…

Others also viewed

Explore content categories