前言
日常的Xcode开发中,我们时常用【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中添加以下内容:
command script import ~/Library/lldb/ignore_specified_objc_exceptions.py
~/Library/lldb/ignore_specified_objc_exceptions.py如果您将其保存在其他位置,请替换为正确的路径。
使用方法
- 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