28

I'm looking for a way to cause the XCode debugger to break programmatically from Swift code if my iOS program is running in the debugger. This would be similar to the way System.Diagnostics.Debugger.Break() works in the Visual Studio environment. Is this possible? The point would be for any developer that hits a particular section of code to break, but not to have a fatal error that causes code execution to stop permanently.

Edit: This is a little different than the user asking to "enable" a breakpoint (though the answer provided in that question is really what I was looking for). I'm also still looking for something that can be done in Swift without interop, bridging headers, and such.

JacobJ
  • 3,677
  • 3
  • 28
  • 32
  • Possible duplicate of [XCode: Programmatically enable a symbolic breakpoint?](http://stackoverflow.com/questions/34031957/xcode-programmatically-enable-a-symbolic-breakpoint) – Robotic Cat Dec 29 '15 at 22:19
  • Perhaps `assert` or `precondition` should suffice. – pronebird Aug 02 '21 at 10:28

3 Answers3

32

For Swift, raise(SIGINT) works for me.

I have this function:

func fail(desc: String) {
  #if DEBUG
    print("assertFail:\(desc)")
    raise(SIGINT)
  #endif
}

For DEBUG macro setup see here: In absence of preprocessor macros, is there a way to define practical scheme specific flags at project level in Xcode project

Community
  • 1
  • 1
hyouuu
  • 2,471
  • 2
  • 27
  • 37
  • 2
    Works in swift 2.2, Thanks @hyouuu – Chris Gunawardena Jul 14 '16 at 18:24
  • 1
    It's important to print something before raising signal. Because long time later, it's hard to figure out what caused the signal. AFAIK, debugger does not always locate the point of where signal raised as signals are supposed to come from other processes. Printing `#filename` and `#line` would be helpful. – eonil Jul 17 '20 at 12:49
7

Putting asm("svc 0") in your code will stop your running application if debugging through xcode. See: https://stackoverflow.com/a/34078247/215400

Community
  • 1
  • 1
tskulbru
  • 2,336
  • 1
  • 20
  • 38
3

I'm not 100% positive there isn't a built in one - but you can create what you want yourself using Symbolic Breakpoints in the XCode UI.

1) Create a new class to represent your Debugger Break.

@interface MYDebuggerBreak : NSObject

+(void)fireUpDebugger;

@end


@implementation MyDebuggerBreak

+(void)fireUpDebugger {
    // Do Nothing
}

@end

2) Add a Symbolic Breakpoint to a method on that Class

[MYDebuggerBreak fireUpDebugger]

This is a bit roundabout, you could also put a breakpoint directly into the line "fireUpDebugger" since you control the code. Symbolic Breakpoints are more useful if you want to stop on a method call for something you don't control.

KirkSpaziani
  • 1,962
  • 12
  • 14
  • This is a bit different than what I'm trying to accomplish in that it only affects the IDE in which the breakpoint was set. I want everyone debugging this code to be forced to break at this point. – JacobJ Dec 29 '15 at 21:49
  • Do you want them to start manually debugging when it hits? Or are you just collecting stacktraces for when that happens? – KirkSpaziani Dec 29 '15 at 21:50
  • It's a useful thing to add to code (generally temporarily) when trying to track down rare or intermittent issues, and you need to see the state of everything when it happens. Otherwise, I know I can always just dump a stack trace to the console. I've also used it in the C# world to highlight when certain programming mistakes were made (like Dispose methods not being called on a class marked Disposable). – JacobJ Dec 29 '15 at 21:53