LunaCodeGirl
LunaCodeGirl

Reputation: 5620

Symbolic breakpoints in Swift

Is it possible to set symbolic breakpoints in Swift for swift based code?

Let's say I have a class like

class AwesomeSauce {

    var ingredients: [String]

    init(useIngredients:[String]) {
        ingredients = useIngredients
    }
}

And let's say the ingredients array is sorted like

var specialSauce = AwesomeSauce(["Sugar","Spice","Everything Nice", "Chemical 'X'"])

specialSauce.ingredients.sort({ $0.length() > $1.length() })

Let's also say (because I'm not paying attention) that I sort this array in a bunch of different places throughout my project.

I want to see when ingredients is being sorted.

Is there a way I can set a symbolic breakpoint on Array.sort() or AwesomeSauce.ingredients.sort() or something like that?

I can't seem to get anything to work and I've tried various permutations of possible symbols.

Upvotes: 3

Views: 2628

Answers (1)

Martin R
Martin R

Reputation: 539675

Setting a symbolic breakpoint on Swift.Array.sort should do the trick.

Here is a sample session from the command-line with swiftc and lldb, but you can do the same within Xcode:

$ xcrun -sdk macosx swiftc -g main.swift 

$ lldb main

(lldb) target create "main"
Current executable set to 'main' (x86_64).

(lldb) b main
Breakpoint 1: where = main`main + 106 at main.swift:13, address = 0x0000000100001aea

(lldb) run
Process 8032 launched: './main' (x86_64)
7 locations added to breakpoint 1
1 location added to breakpoint 1
4 locations added to breakpoint 1
Process 8032 stopped
* thread #1: tid = 0x1c46ff, 0x0000000100001aea main`main + 106 at main.swift:13, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100001aea main`main + 106 at main.swift:13
   10       }
   11   }
   12   
-> 13   var specialSauce = AwesomeSauce(useIngredients: ["Sugar","Spice","Everything Nice", "Chemical 'X'"])
   14   
   15   specialSauce.ingredients.sort( { count($0) > count($1) })
   16   

(lldb) b Swift.Array.sort
Breakpoint 2: where = libswiftCore.dylib`Swift.Array.sort (inout Swift.Array)((A, A) -> Swift.Bool) -> (), address = 0x0000000100030160

(lldb) c
Process 8032 resuming
Process 8032 stopped
* thread #1: tid = 0x1c46ff, 0x0000000100030160 libswiftCore.dylib`Swift.Array.sort (inout Swift.Array)((A, A) -> Swift.Bool) -> (), queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
    frame #0: 0x0000000100030160 libswiftCore.dylib`Swift.Array.sort (inout Swift.Array)((A, A) -> Swift.Bool) -> ()
libswiftCore.dylib`Swift.Array.sort (inout Swift.Array)((A, A) -> Swift.Bool) -> ():
->  0x100030160 : pushq  %rbp
    0x100030161 : movq   %rsp, %rbp
    0x100030164 : pushq  %r15
    0x100030166 : pushq  %r14

(lldb) bt
* thread #1: tid = 0x1c46ff, 0x0000000100030160 libswiftCore.dylib`Swift.Array.sort (inout Swift.Array)((A, A) -> Swift.Bool) -> (), queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
  * frame #0: 0x0000000100030160 libswiftCore.dylib`Swift.Array.sort (inout Swift.Array)((A, A) -> Swift.Bool) -> ()
    frame #1: 0x0000000100001c99 main`main + 537 at main.swift:15
    frame #2: 0x00007fff8cff65c9 libdyld.dylib`start + 1

Upvotes: 3

Related Questions