Skip to main content

Advanced Debugging with Xcode and LLDB - WWDC 2018




Advanced Debugging Tips and Tricks:

  • Injecting Code at runtime
    We can change values of variables at runtime while debugging (here variable_name is a Bool)

    expression variable_name = false 

    Here we are injecting value at runtime, so no need to run again to see the effect. (Other eg.,  expression animator.delegate = self)
  • Under Xcode > Preferences > Behaviours > Running > Pauses > Check Show Tab Named, In order to show new tab while a breakpoint is hit. We can set other behaviours as well.
  • We can add symbolic breakpoints on methods in frameworks. like

    This is in objective c because UIKit is in objective c.
  • In order to find the values/parameter of a function inside framework, we can use

    For expression -[UILabel setText:]

    po $arg1

    <UILabel ****>

    po (SEL)$arg2
      "setText:"

    po $arg3
    "0 ft"
  • We can set symbolic breakpoints in 2 ways
    1. By clicking plus in bottom left and choosing symbolic (Cons: Sets BP in all places)
    2. By adding a breakpoint > Editing it to specify a debugger command.  (Pros: we can set at a desired method, for eg., after completion of some task)

        Here --one-shot is breakpoint is executed only once. (Dependent Breakpoint)

  • Skipping code execution after compilation.
    for example if i want to skip line 10 in editor and continue execution at line after line 12. There are 2 ways.

    1. Keep BreakPoint at line 10 and Drag the Thread (Instruction Pointer) to desired line 12.
    2. Add breakpoint at line 10 with debugger command "thread jump --by 2"

    Use Case:
    Jump execution of a function at line 10 and execute the same function with different parameters
  • Use CustomDebugStringConvertible protocol to add our own format of debug description by implementing debugDescription property and returning desired format.
  • po is programatic description of object. p is description of format specified by LLDB.
  • watchpoint watchpoint for a variable means, a breakpoint is set for a variable and it is called/execution paused when the value is changed next time.
  • To execute swift code in objc in LLDB

    Swift code: self.view.recursiveDescription()
    The above code throws error as the function is not strictly defined. so we run same code in objc as it is dynamic language.

    For Objc way:

    expression -l objc  -O -- [`self.view` recursiveDescription]
    objc = to tell we are giving objc code even though we are in swift lldb frame
    -O = to give debug description
    -- = to tell no more options
    `` = are to inherit variable from swift frame to objc
  • Creating shortcuts for lengthy commands.

    command alias ALIAS_NAME expression -l objc  -O -- 

    The above command creates shortcut, so next time.
    ALIAS_NAME [`self.view` recursiveDescription]
  • Printing variable from memory address 2 ways.
    1. po 7x99890s0s
    2. po unsafeBitCast(7x99890s0s, to: CustomViewType.self)

    unsafeBitCast is more useful as you can print property names and functions on it, like
    po unsafeBitCast(7x99890s0s, to: CustomViewType.self).frame
  • Assigning/Modifying view properties from LLDB
    po unsafeBitCast(7x99890s0s, to: CustomViewType.self).center.y = 100

    This gets updated only after "expression CATransaction.flush()" (as we are paused in debugger to run po command, this command tell core animation to update ui)

View Debugging Tips
  • To click through the view hold command while click.
    For eg., to select a view which is beneath other full screen view.
  • Windows are shown as multiple root level objects on left hand side of view hierarchy. 
  • e is shortcut for expression.
  • we can create backtraces for views in views hierarchy, by settings.
    Scheme > Edit Scheme > Diagnostics > Logging > Check - Malloc Stack > Select - All allocations and free history




Try: 

po classification

Creates new simpler context with frame of variables so that we can inspect variable values.


po variable_name
prints : "Mercury"

po frame variable variable_name
prints : "(String) variable_name  = Mercury"

Comments

Popular posts from this blog

Agile Overview

Agile Manifesto: Many processes like Scrum, Kanban, Lean, Extreme Programming(XP), Crystal Clear, Agile Unified Process, Dynamic Systems Development Method, Feature Driven Development, Agile Modeling  what they were doing is same so they came up with Agile Manifesto which is set of 4 Values and 12 principals. The 4 Values are: Individuals and Interactions Over Process and tools: As process and tools avoid direct interactions the agile manifesto values Interactions. Working Software Over  Documentation: Giving value to Woking software than the document which describes the software. Customer collaboration Over Contract: We should not stick to contract, as technology changes. Responding to change over plan We should unfollow plans and de prioritise tasks which take longer time  The 12 Principles of Agile Manifesto: Underlying Agile Concepts: Short Feedback Loops Just in Time Requirements and Design Software does not need blue prints like before b...

User defined settings iOS

With User defined settings  we can set values specific to build configuration. Creating User Defined Settings Select Target > Build Settings Click Plus icon, present beside the search bar Using User Defined Settings when we need to access the values set in User defined build settings we can do it in two ways  From info.plist $(NEW_SETTING) From Code let value = Bundle.main.infoDictionary?["NEW_SETTING"] as! String

Cloning repositories using SSH

Using the Secure Shell (SSH) protocol, you can connect and authenticate to remote servers and services. With SSH keys, you can connect to Version Control Account (BitBucket/GitHub..) accounts without supplying your username or password at each visit.  We need to generate SSH key pair, Add the private key to ssh agent and public key to Version control account. 1. Generating SSH key pair Open  Terminal  and paste the text below, substituting in your email address or some unique key like employee Id. ssh-keygen -t rsa -b 4096 -C " your_email@example.com " This creates a new ssh key, using the provided email as a label. This generates public/private rsa key pair. When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location. 2.   Adding your SSH key to the ssh-agent Start the ssh-agent in the background. eval "$(ssh-agent -s)" If you're using macOS Sierra 10.12.2 or l...