使用 Xcode 的所有异常断点时忽略某些异常 [英] Ignore certain exceptions when using Xcode's All Exceptions breakpoint

查看:36
本文介绍了使用 Xcode 的所有异常断点时忽略某些异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Xcode 中配置了一个 All Exceptions 断点:

有时 Xcode 会停在如下一行:

[managedObjectContext save:&error];

具有以下回溯:

但是如果您单击继续",程序会继续运行,就好像什么也没发生一样.

如何忽略这些正常"异常,但仍然让调试器在我自己的代码中停止异常?

(我知道发生这种情况是因为 Core Data 在内部抛出并捕获异常,并且 Xcode 只是尊重我在抛出异常时暂停程序的请求.但是,我想忽略这些,以便我可以重新开始调试我自己的代码!)

版主:这类似于 获取脚本和更多细节.

I have an All Exceptions breakpoint configured in Xcode:

Sometimes Xcode will stop on a line like:

[managedObjectContext save:&error];

with the following backtrace:

but the program continues on as if nothing happened if you click Continue.

How can I ignore these "normal" exceptions, but still have the debugger stop on exceptions in my own code?

(I understand that this happens because Core Data internally throws and catches exceptions, and that Xcode is simply honoring my request to pause the program whenever an exception is thrown. However, I want to ignore these so I can get back to debugging my own code!)

Moderators: this is similar to "Xcode 4 exception breakpoint filtering", but I think that question takes too long to get around to the point and doesn't have any useful answers. Can they be linked?

解决方案

I wrote an lldb script that lets you selectively ignore Objective-C exceptions with a much simpler syntax, and it handles both OS X, iOS Simulator, and both 32bit and 64bit ARM.

Installation

  1. Put this script in ~/Library/lldb/ignore_specified_objc_exceptions.py or somewhere useful.

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. Add the following to ~/.lldbinit:

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

    replacing ~/Library/lldb/ignore_specified_objc_exceptions.py with the correct path if you saved it somewhere else.

Usage

  • In Xcode, add a breakpoint to catch all Objective-C exceptions
  • Edit the breakpoint and add a Debugger Command with the following command: ignore_specified_objc_exceptions name:NSAccessibilityException className:NSSomeException
  • This will ignore exceptions where NSException -name matches NSAccessibilityException OR -className matches NSSomeException

It should look something like this:

In your case, you would use ignore_specified_objc_exceptions className:_NSCoreData

See http://chen.do/blog/2013/09/30/selectively-ignoring-objective-c-exceptions-in-xcode/ for the script and more details.

这篇关于使用 Xcode 的所有异常断点时忽略某些异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆