如何调试 iOS 扩展 (.appex)? [英] How to debug an iOS extension (.appex)?

查看:65
本文介绍了如何调试 iOS 扩展 (.appex)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使日志打印出现在 Xcode 的 lldb 调试器 中?

How to make log prints appear in Xcode's lldb debugger from extension?

推荐答案

简单回答:

  • 不会打印日志消息,但是您可以在断点处停止,然后使用 lldb 打印所有内容.
  • log messages are not printed, however you can stop at breakpoints, and then print everything using lldb.
  1. 运行您的应用
  2. 在应用运行时,转到调试 ->通过 PID 或名称附加到进程

  1. 写下您的扩展程序的名称(因为 bundle-id 不起作用!?),然后点击附加".

  1. 然后以您可以在设备上执行此操作的任何方式运行您的扩展程序.
  2. 等待 Xcode 的调试器在断点处停止扩展,但有些可能需要调用 waitForDebugger(这是一个自定义函数,请参见下面的逻辑).
  1. Then run your extension with any way you can do this on your device.
  2. Wait for Xcode's debugger to stop the extension at breakpoint, but some may need to call waitForDebugger (which is a custom function, see logic below).

等待调试器示例

public static func isDebuggerAttached() -> Bool {
    // Buffer for "sysctl(...)" call's result.
    var info = kinfo_proc()
    // Counts buffer's size in bytes (like C/C++'s `sizeof`).
    var size = MemoryLayout.stride(ofValue: info)
    // Tells we want info about own process.
    var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
    // Call the API (and assert success).
    let junk = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0)
    assert(junk == 0, "sysctl failed")
    // Finally, checks if debugger's flag is present yet.
    return (info.kp_proc.p_flag & P_TRACED) != 0
}

@discardableResult
public static func waitForDebugger(_ timeout: Int = 30000) -> Bool {
    var now: UInt64 = DispatchTime.now().uptimeNanoseconds
    let begin = now
    repeat {
        if isDebuggerAttached() {
            // Wait a little bit longer,
            // because early breakpoints may still not work.
            Thread.sleep(forTimeInterval: 3.0)
            return true
        }
        Thread.sleep(forTimeInterval: 0.1)
        now = DispatchTime.now().uptimeNanoseconds
    } while Double(now - begin) / 1000000.0 < Double(timeout);
    return false;
}

这篇关于如何调试 iOS 扩展 (.appex)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆