signal(SIGTRAP) not work in swift5.0

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {


// Override point for customization after application launch.


//crash


crashHandle { (crashInfoArr) in


for info in crashInfoArr{



print("CrashIno- \(info)")


}


}


return true


}



func crashHandle(crashContentAction:@escaping ([String])->Void){


DispatchQueue.global().async {


if CrashManager.readAllCrashInfo().count > 0 {



crashContentAction(CrashManager.readAllCrashInfo())


}



CrashManager.deleteAllCrashFile()


}



registerSignalHandler()



registerUncaughtExceptionHandler()


}



func SignalExceptionHandler(signal:Int32) -> Void


{



var mstr = String()


mstr += "Stack:\n"



mstr = mstr.appendingFormat("slideAdress:0x%0x\r\n", calculate())


for symbol in Thread.callStackSymbols {


mstr = mstr.appendingFormat("%@\r\n", symbol)


}



print("Swift- exception: \(mstr)")


CrashManager.saveCrash(appendPathStr: .signalCrashPath, exceptionInfo: mstr)


exit(signal)


}




func registerSignalHandler()


{




signal(SIGABRT, SignalExceptionHandler)


signal(SIGSEGV, SignalExceptionHandler)


signal(SIGBUS, SignalExceptionHandler)


signal(SIGTRAP, SignalExceptionHandler)


signal(SIGILL, SignalExceptionHandler)




signal(SIGHUP, SignalExceptionHandler)


signal(SIGINT, SignalExceptionHandler)


signal(SIGQUIT, SignalExceptionHandler)


signal(SIGFPE, SignalExceptionHandler)


signal(SIGPIPE, SignalExceptionHandler)



}



class ViewController: UIViewController {


var name:String!


override func viewDidLoad() {


super.viewDidLoad()


print("\(name!)")


}


I called registerSignalHandler() in the AppDelegate.


but SignalExceptionHandler is not get any action.


swift is 5.0


this is why

Replies

You are probably a beginner here, so a few rules on the forum:

- ask a precise and clear question. What is your question here ?

- please, edit your code properly, avoiding all this blank lines that make very long posts

- use the code formatter tool <>

- provide complete code: where is crashManager defined ?

- don't duplicate posts


Code should be presentd like this:


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Override point for customization after application launch.
        //crash
        crashHandle { (crashInfoArr) in
            for info in crashInfoArr{
              print("CrashIno- \(info)")
            }
        }
       return true
 }

func crashHandle(crashContentAction:@escaping ([String])->Void){

    DispatchQueue.global().async {
        if CrashManager.readAllCrashInfo().count > 0 {
            crashContentAction(CrashManager.readAllCrashInfo())
        }
        CrashManager.deleteAllCrashFile()
    }

    registerSignalHandler()
    registerUncaughtExceptionHandler()
}

Oi vey! It’s not safe to write a signal handler in Swift. That’s because signal handlers must be async signal safe [1], which means that they’re not allowed to allocate memory, and Swift does not let you prevent such allocations [2].

Taking a step back, you seem to be implementing your own crash reporter. This is impossible to do correctly. I explain why in my Implementing Your Own Crash Reporter post.

I strongly recommend that you avoid going down this path, and instead stick with the Apple crash reporter.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] Technically you’re trying to catch synchronous signals, but you still need to be async signal safe because these signals can arise in code that you don’t control, like the implementation of

malloc
.

[2] … currently. At some point in the future this may be possible. If you’re interested in the details, see the Ownership Manifesto.

It’s not safe to write a signal handler in Swift.

Oh, just to clarify, it is possible to catch signals using Swift. The trick is to create a Dispatch signal source (

DispatchSource.makeSignalSource(signal:)
). This works great for general signal handling, but it won’t help folks trying to implement a crash reporter.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"