前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Xcode忽略指定的异常断点

Xcode忽略指定的异常断点

作者头像
freesan44
发布2021-03-03 10:52:39
7880
发布2021-03-03 10:52:39
举报
文章被收录于专栏:freesan44freesan44

前言

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

解决方法

建立脚本

  1. 采用以下脚本生成ignore_specified_objc_exceptions.py文件
代码语言:javascript
复制
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中添加以下内容:
代码语言:javascript
复制
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的异常

快捷部署方法

一键部署脚本:

代码语言:javascript
复制
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

参考:这里

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 解决方法
    • 建立脚本
      • 使用方法
      • 快捷部署方法
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档