Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I cause Xcode's debugger to break programmatically?

Tags:

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.

like image 769
JacobJ Avatar asked Dec 29 '15 21:12

JacobJ


2 Answers

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

like image 26
tskulbru Avatar answered Sep 24 '22 00:09

tskulbru


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

like image 123
hyouuu Avatar answered Sep 22 '22 00:09

hyouuu