Xcode忽略指定的異常斷點

前言

日常的Xcode開發(fā)中,我們時常用【All Exceptions Breakpoint】來定位一些異常情況的斷點
但該功能同時也會在@try@catch或者在使用諸如CoreData或Accessibility之類的異常大量框架時,很難追蹤到您實際遇到的異常


解決方法

建立腳本

  1. 采用以下腳本生成ignore_specified_objc_exceptions.py文件
import lldb
import re
import shlex

# This script allows Xcode to selectively ignore Obj-C exceptions
# based on any selector on the NSException instance

def getRegister(target):
    if target.triple.startswith('x86_64'):
        return "rdi"
    elif target.triple.startswith('i386'):
        return "eax"
    elif target.triple.startswith('arm64'):
        return "x0"
    else:
        return "r0"

def callMethodOnException(frame, register, method):
    return frame.EvaluateExpression("(NSString *)[(NSException *)${0} {1}]".format(register, method)).GetObjectDescription()

def filterException(debugger, user_input, result, unused):
    target = debugger.GetSelectedTarget()
    frame = target.GetProcess().GetSelectedThread().GetFrameAtIndex(0)

    if frame.symbol.name != 'objc_exception_throw':
        # We can't handle anything except objc_exception_throw
        return None

    filters = shlex.split(user_input)

    register = getRegister(target)


    for filter in filters:
        method, regexp_str = filter.split(":", 1)
        value = callMethodOnException(frame, register, method)

        if value is None:
            output = "Unable to grab exception from register {0} with method {1}; skipping...".format(register, method)
            result.PutCString(output)
            result.flush()
            continue

        regexp = re.compile(regexp_str)

        if regexp.match(value):
            output = "Skipping exception because exception's {0} ({1}) matches {2}".format(method, value, regexp_str)
            result.PutCString(output)
            result.flush()

            # If we tell the debugger to continue before this script finishes,
            # Xcode gets into a weird state where it won't refuse to quit LLDB,
            # so we set async so the script terminates and hands control back to Xcode
            debugger.SetAsync(True)
            debugger.HandleCommand("continue")
            return None

    return None

def __lldb_init_module(debugger, unused):
    debugger.HandleCommand('command script add --function ignore_specified_objc_exceptions.filterException ignore_specified_objc_exceptions')
  1. 將此腳本放在某處(例如~/Library/lldb/ignore_specified_objc_exceptions.py
  2. 在~/.lldbinit中添加以下內(nèi)容:
command script import ~/Library/lldb/ignore_specified_objc_exceptions.py

~/Library/lldb/ignore_specified_objc_exceptions.py如果您將其保存在其他位置,請?zhí)鎿Q為正確的路徑。

使用方法

  1. All Exceptions Breakpoint選擇Objective-C
  2. Debugger Command中添加以下語句:
    ignore_specified_objc_exceptions className:DTRpcException name: NSAccessibilityException
    這樣就忽略類名為DTRpcException或者name為NSAccessibilityException的異常

快捷部署方法

一鍵部署腳本:

mkdir -p ~/Library/lldb
curl -L https://gist.github.com/chendo/6759305/raw/ignore_specified_objc_exceptions.py > ~/Library/lldb/ignore_specified_objc_exceptions.py
echo "command script import ~/Library/lldb/ignore_specified_objc_exceptions.py" >> ~/.lldbinit

參考:這里

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容