Can I get a stack trace for Objective C ++ exceptions?

I am developing an iOS application that has recently grown on a large C ++ base. C ++ is not my forte and I'm upset about exceptions. What I'm looking for is a way to get the stack path to the site of the (not processed) exception. I will say that the “unprocessed” qualifier is optional; I would agree to crack any exception as a last resort, although ideal exceptions are not handled.

What I am getting now is useless. Assuming I don't have matching exception handlers above the call, and I'm doing something like

std::vector<int> my_vector; my_vector.at(40) = 2; // Throws std::out_of_range 

The application will be broken down into main() , and I will get a log message that says: "terminate called throwing exception". Not healthy.

Putting the common try / catch blocks above in callstack doesn't help either, as the column expands during exception handling to the point of the catch block, leaving me unaware of the actual origin of the exception. This also applies to providing my own terminate_handler . Statements are more useful, but they require me to anticipate error conditions to some extent, which I cannot always do. I would still like the debugger to be able to go in even if an unforeseen exception makes it behind my anticipatory assert() s.

What I want to avoid is to wrap all calls that might throw an exception in the try / catch block to get a stack trace to the error. At runtime, I'm really not interested in catching these exceptions. When they occur, it means that there is a fatal flaw in the execution of the program, and there is no way that this can continue in normal mode. I just want to be notified so that I can identify the cause and fix the problem so that it does not recur.


In Objective-C, I can put a symbolic breakpoint on objc_exception_throw , and anytime I screw something, I immediately break execution and get a nice stack trace so that I know where the problem is. Very helpful.

I understand that this behavior is really only useful because of the philosophical difference in handling exceptions between the two languages. Objective-C exceptions are intended only to indicate fatal errors. The task of routine error handling is performed using error return codes. This means that any Objective-C exception is a great candidate for a breakpoint for the developer.

C ++ seems to be used for Exception. They are used to handle both fatal errors and routine errors (at least in the third-party libraries that I use). This means that I might not want to break into every exception thrown in C ++, but I still find utility if I cannot break only exceptions without handling.

+6
source share
3 answers

You can quickly set the gap in all C ++ throw conditions in Xcode:

  • cmd+6
  • " + " → Add exception breakpoint
    • C ++ -> std::out_of_range
    • In the cast

Update

If you have many filters, you can:

  • Create a symbolic breakpoint
  • Character = __cxa_throw (may vary depending on the std library)
  • Action> Debugger Command = bt
  • Continue automatically after eval = On

The bt command registers backtracing. Configured in this way, it will automatically continue.

So, it will simply register the backtrace of each thrown exception - when your program terminates due to an unhandled exception, the prompts will be among the final registered backtracks (often this is the last, unless the library is re-created).

+5
source

In the application that I get for debugging with many C ++ exceptions, I leave “Catch C ++ Exceptions on Throw” until I get to the point of the application where it will throw an exception, then I turn on this option and usually the next exception is what I'm looking for. This will break several levels deeper than the error, but the stack is not damaged so you can understand what is happening.

+1
source

Check PLCrashReporter . We use it with our application (which is heavily dependent on C ++), and it creates stack traces even for C ++ code.

The only problem that may occur when using assembly procedures that were not originally written for iOS (the Apple compiler uses R7 to store the stack frame to track backward characters that does not match the official ARM EBI)

0
source

Source: https://habr.com/ru/post/909683/


All Articles