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

解決方法
建立腳本
- 采用以下腳本生成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')
- 將此腳本放在某處(例如~/Library/lldb/ignore_specified_objc_exceptions.py)
- 在~/.lldbinit中添加以下內(nèi)容:
command script import ~/Library/lldb/ignore_specified_objc_exceptions.py
~/Library/lldb/ignore_specified_objc_exceptions.py如果您將其保存在其他位置,請?zhí)鎿Q為正確的路徑。
使用方法
- All Exceptions Breakpoint選擇Objective-C
- 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
參考:這里