我编写了一个lldb脚本,让您可以用更简单的语法 Select 性地忽略Objective-C异常,它可以处理OS X、iOS模拟器以及32位和64位ARM.
Installation
- 将此脚本放入
~/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
保存在其他位置,请将其替换为正确的路径.
Usage
- 在Xcode中,添加断点以捕获所有Objective-C个异常
- 编辑断点并使用以下命令添加调试器命令:
- 这将忽略
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/.