我编写了一个 lldb 脚本,它可以让您使用更简单的语法选择性地忽略 Objective-C 异常,它可以处理 OS X、iOS 模拟器以及 32 位和 64 位 ARM。
安装
- 把这个脚本
~/Library/lldb/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')
将以下内容添加到~/.lldbinit
:
command script import ~/Library/lldb/ignore_specified_objc_exceptions.py
~/Library/lldb/ignore_specified_objc_exceptions.py
如果您将其保存在其他地方,请替换为正确的路径。
用法
- 在 Xcode 中,添加断点以捕获所有Objective-C异常
- 编辑断点并使用以下命令添加调试器命令:
ignore_specified_objc_exceptions name:NSAccessibilityException className:NSSomeException
- 这将忽略
NSException
-name
匹配NSAccessibilityException
或-className
匹配的异常NSSomeException
它应该看起来像这样:
在你的情况下,你会使用ignore_specified_objc_exceptions className:_NSCoreData
有关脚本和更多详细信息,请参见http://chen.do/blog/2013/09/30/selectively-ignoring-objective-c-exceptions-in-xcode/。