Xcode忽略指定的异常断点

简介: 日常的Xcode开发中,我们时常用【All Exceptions Breakpoint】来定位一些异常情况的断点但该功能同时也会在@try@catch或者在使用诸如CoreData或Accessibility之类的异常大量框架时,很难追踪到您实际遇到的异常

前言


日常的Xcode开发中,我们时常用【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中添加以下内容:


command script import ~/Library/lldb/ignore_specified_objc_exceptions.py


~/Library/lldb/ignore_specified_objc_exceptions.py如果您将其保存在其他位置,请替换为正确的路径。

使用方法


  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


目录
相关文章
|
iOS开发
Xcode断点 中断不正常 每次断点都进入汇编
Xcode断点 中断不正常 每次断点都进入汇编
702 0
|
iOS开发
Xcode断点没有跳到断点的代码行
不知道是什么原因改变设置了,打断点时没有自动跳到断点的代码行上。 打一个断点 结果跳到了debug视图(汇编代码) 解决办法: Always Show Disassemly 取消Always Show Disassemly 跳到代码行 Always Show Disassemly  这玩意是什么,汇编看来不学是用不到了 - 如果有什么疑问,可以在评论区一起讨论; - 如果有什么不正确的地方,欢迎指导! > 注:本文首发于iHTCboy's blog,如若转载,请注明来源。
1528 0
|
iOS开发 NoSQL API
xcode反汇编调试iOS模拟器程序(四)自动断点应用之NSNotificationCenter
知道怎么查看后,先看看有什么实际应用,拿NSNotificationCenter来做实践吧。 首先在某个容易进入的地方加断点,并停在那,例如main函数。
1355 0
|
jenkins Unix 持续交付
个人记录jenkins编译ios过程 xcode是9.4.1
个人记录jenkins编译ios过程 xcode是9.4.1
444 2
|
Linux 数据安全/隐私保护 iOS开发
如何使用 Xcode 打包导出 IPA 文件并进行 iOS 应用内测,无需支付苹果开发者账号费用?
如何使用 Xcode 打包导出 IPA 文件并进行 iOS 应用内测,无需支付苹果开发者账号费用?