swift - Swift 中的符号断点

标签 swift lldb

是否可以在 Swift 中为基于 swift 的代码设置符号断点?

假设我有一个类

class AwesomeSauce {

    var ingredients: [String]

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

假设 ingredients 数组的排序方式如下

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

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

我们还假设(因为我没有注意)我在整个项目的许多不同位置对这个数组进行排序。

我想查看 ingredients 何时被排序。

有没有办法在 Array.sort()AwesomeSauce.ingredients.sort() 或类似的东西上设置符号断点?

我似乎无法让任何东西起作用,我已经尝试了各种可能符号的排列。

最佳答案

Swift.Array.sort 上设置符号断点应该可以解决问题。

这是命令行中使用 swiftc 和 lldb 的示例 session , 但您可以在 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

关于swift - Swift 中的符号断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31414922/

相关文章:

macos - 我如何知道谁在 macOS 上调用了该进程?

ios - 用户无法删除追加后提交的文本中的字符

json - 无法在 Alamofire 和 Sync 中将值 JSON 转换为 AnyObject

objective-c - 我无法调用 swift 文件中的方法

swift - self.tabBarController?.selectedIndex 并在另一个 View Controller 中执行功能

objective-c - LLDB 等同于 gdb 的 future-break?

ios - X代码/LLDB : po $eax and po $r0 only display ints for __cxa_throw exceptions?

objective-c - 防止未使用的符号被剥离

ios - EXC_BAD_ACCESS code=2 Xcode 中自定义类初始化错误

ios - RX Swift - API 的并行执行