如何在所有投掷上设置断点?

时间:2022-09-07 22:50:52

Is there any way how we can specify a breakpoint to stop on all 'throw' statements? (symbolic breakpoint)

有没有什么方法可以指定断点来停止所有'throw'语句? (符号断点)

guard let id = UInt(idString),
      let changeset = UInt(changesetString),
      let uid = UInt(uidString)
else {
      throw OSMVectorMapDescriptionError.ElementAttributeConversionError(element: xmlNode, attributeº: nil)
}

2 个解决方案

#1


4  

Just found an alternative in Xcode 8.3: there is a "Swift Error Breakpoint" option in the menu for adding new ad-hoc breakpoints:

刚刚在Xcode 8.3中找到了一个替代方案:菜单中有一个“Swift Error Breakpoint”选项,用于添加新的ad-hoc断点:

如何在所有投掷上设置断点?

If you right-click the newly created breakpoint, you can even specify a particular Error-conforming type to break on.

如果右键单击新创建的断点,甚至可以指定特定的符合错误的断点类型。

如何在所有投掷上设置断点?

#2


22  

First thing to keep in mind is that errors thrown in Swift are not exceptions, just errors (NSError, whatever based on ErrorType, ...).

首先要记住的是,Swift中抛出的错误不是异常,只是错误(NSError,无论基于ErrorType,......)。

Second thing is, don't use try! unless you're damn sure it will not crash or if crash is what you really want.

第二件事是,不要用试试!除非你确定它不会崩溃或崩溃是你真正想要的。

Don't mix symbolic breakpoint with exception breakpoint. Different beasts.

不要将符号断点与异常断点混合使用。不同的野兽。

Back to your question ...

回到你的问题......

throw is not a symbol, thus symbolic breakpoint doesn't work for you. But there's a way ...

throw不是符号,因此符号断点对你不起作用。但有一种方法......

(lldb)br s -E swift

-E <language> ( --language-exception <language> )
  Set the breakpoint on exceptions thrown by the specified language
  (without options, on throw but not catch.)

... this is little bit misleading, because thrown errors are not exceptions. Keep this in mind. When you try to set exception breakpoint in Xcode, there's no Swift. Probably the main reason is that thrown errors are not exceptions and they didn't figure out where to put it yet (who knows).

......这有点误导,因为抛出的错误不是例外。记住这一点。当你尝试在Xcode中设置异常断点时,没有Swift。可能主要原因是抛出的错误不是例外,他们也没有弄清楚到底在哪里(谁知道)。

如何在所有投掷上设置断点?

Add it manually

手动添加

Set a breakpoint somewhere in your code, when execution pauses, just type br s -E swift in LLDB prompt and then continue.

在代码中的某处设置断点,当执行暂停时,只需在LLDB提示符下键入br s -E swift然后继续。

Add it automatically

自动添加

Set a breakpoint somewhere in your code in this way ...

以这种方式在代码中的某处设置断点...

如何在所有投掷上设置断点?

... and toggle it (on/off) when you do want to stop on throw.

...当你想要停止投掷时,切换它(开/关)。

Symbolic breakpoint

符号断点

When you do use already mentioned br s -E swift you are going to find that there's symbol for throw. Actually it's not throw, but swift_willThrow. Feel free to set symbolic breakpoint in this way ...

当你使用已经提到过的br s -E swift时,你会发现有抛出的符号。实际上它不是扔,而是swift_willThrow。随意以这种方式设置符号断点...

如何在所有投掷上设置断点?

... I do not recommend this way for now, because it can be changed in the future. But if it's enough for now, why not.

...我现在不推荐这种方式,因为将来可以更改。但如果它现在足够了,为什么不呢。

You can share your breakpoint across Xcode projects like this ...

你可以在这样的Xcode项目中分享你的断点......

如何在所有投掷上设置断点?

... secondary click, Move Breakpoint To, User. Breakpoint will be visible in all Xcode projects.

...辅助点击,将断点移动到,用户。断点将在所有Xcode项目中可见。

When you hit breakpoint, you'll end up in something like this ...

当你遇到断点时,你最终会得到这样的东西......

如何在所有投掷上设置断点?

... and you have to select previous stack frame to see where the error was thrown ...

...并且您必须选择先前的堆栈帧以查看错误被抛出的位置...

如何在所有投掷上设置断点?

#1


4  

Just found an alternative in Xcode 8.3: there is a "Swift Error Breakpoint" option in the menu for adding new ad-hoc breakpoints:

刚刚在Xcode 8.3中找到了一个替代方案:菜单中有一个“Swift Error Breakpoint”选项,用于添加新的ad-hoc断点:

如何在所有投掷上设置断点?

If you right-click the newly created breakpoint, you can even specify a particular Error-conforming type to break on.

如果右键单击新创建的断点,甚至可以指定特定的符合错误的断点类型。

如何在所有投掷上设置断点?

#2


22  

First thing to keep in mind is that errors thrown in Swift are not exceptions, just errors (NSError, whatever based on ErrorType, ...).

首先要记住的是,Swift中抛出的错误不是异常,只是错误(NSError,无论基于ErrorType,......)。

Second thing is, don't use try! unless you're damn sure it will not crash or if crash is what you really want.

第二件事是,不要用试试!除非你确定它不会崩溃或崩溃是你真正想要的。

Don't mix symbolic breakpoint with exception breakpoint. Different beasts.

不要将符号断点与异常断点混合使用。不同的野兽。

Back to your question ...

回到你的问题......

throw is not a symbol, thus symbolic breakpoint doesn't work for you. But there's a way ...

throw不是符号,因此符号断点对你不起作用。但有一种方法......

(lldb)br s -E swift

-E <language> ( --language-exception <language> )
  Set the breakpoint on exceptions thrown by the specified language
  (without options, on throw but not catch.)

... this is little bit misleading, because thrown errors are not exceptions. Keep this in mind. When you try to set exception breakpoint in Xcode, there's no Swift. Probably the main reason is that thrown errors are not exceptions and they didn't figure out where to put it yet (who knows).

......这有点误导,因为抛出的错误不是例外。记住这一点。当你尝试在Xcode中设置异常断点时,没有Swift。可能主要原因是抛出的错误不是例外,他们也没有弄清楚到底在哪里(谁知道)。

如何在所有投掷上设置断点?

Add it manually

手动添加

Set a breakpoint somewhere in your code, when execution pauses, just type br s -E swift in LLDB prompt and then continue.

在代码中的某处设置断点,当执行暂停时,只需在LLDB提示符下键入br s -E swift然后继续。

Add it automatically

自动添加

Set a breakpoint somewhere in your code in this way ...

以这种方式在代码中的某处设置断点...

如何在所有投掷上设置断点?

... and toggle it (on/off) when you do want to stop on throw.

...当你想要停止投掷时,切换它(开/关)。

Symbolic breakpoint

符号断点

When you do use already mentioned br s -E swift you are going to find that there's symbol for throw. Actually it's not throw, but swift_willThrow. Feel free to set symbolic breakpoint in this way ...

当你使用已经提到过的br s -E swift时,你会发现有抛出的符号。实际上它不是扔,而是swift_willThrow。随意以这种方式设置符号断点...

如何在所有投掷上设置断点?

... I do not recommend this way for now, because it can be changed in the future. But if it's enough for now, why not.

...我现在不推荐这种方式,因为将来可以更改。但如果它现在足够了,为什么不呢。

You can share your breakpoint across Xcode projects like this ...

你可以在这样的Xcode项目中分享你的断点......

如何在所有投掷上设置断点?

... secondary click, Move Breakpoint To, User. Breakpoint will be visible in all Xcode projects.

...辅助点击,将断点移动到,用户。断点将在所有Xcode项目中可见。

When you hit breakpoint, you'll end up in something like this ...

当你遇到断点时,你最终会得到这样的东西......

如何在所有投掷上设置断点?

... and you have to select previous stack frame to see where the error was thrown ...

...并且您必须选择先前的堆栈帧以查看错误被抛出的位置...

如何在所有投掷上设置断点?