Building Your Own Crash Report in Swift. Think Twice Before Doing It.

Gabriel Marson
Dec 1, 2020 · 5 min read
Image for post
Image for post
Photo by Iva Rajović on Unsplash

I am writing this article in the hope to help someone convince their bosses that building a crash report for iOS is far from trivial. It’s also important to state that the following lines are entirely my opinion about this subject, so if at the end of this text you still want to do it, go for it 🤘!

Although it appears to be simple and straightforward, building a crash report will require a lot of knowledge in subjects that are not quite easy to grasp, such as threading, concurrency and memory management. Also, you, almost certainly, will have to code at low level to have access to system functions, which means, C or C++.

But before writing about these topics let’s begin with the simple stuff.

Intercepting NSExceptions

NSSetUncaughtExceptionHandler { exception in   print(exception)}

For some developers, logging these exceptions will be enough. Unfortunately, it wasn't the case for me… 😢

Now it gets complicated

That was the moment I realized that to build a more capable crash report, we must sweep throughout the stack trace, find the information we want and translate them to something that developers would actually understand. That’s because the data contained in the stack trace will often tell us what happened but not why it happened. And this brings us to our problems:

Your code will run on a crashed process

Handling signals

  • UNIX Signals: Since there are more developers that are more familiar to Unix Signals, you will find more stuff googling about this. I suggest starting from this one, but be aware that there are some events of crash in swift that are not directly translated to UNIX Signals.
  • Mach Exception Handler: This is the default error handling mechanism used by Apple crash reporter. You might even think that using this is a good idea, but this mechanism is quite complex to understand.

We also have to be very careful about how we scan memory addresses by looking for these signals.

Reading Memory

  • Its code
  • Its stack
  • Its arguments
  • Immutable global state

This caution about memory reading is important because in case something unpredicted happens, it not only will affect our own code but will also, most likely, prevent apple crash reporter from working properly.

Apple Crash Report

And this is just one problem regarding the crash reporter. Let's not even mention that we might end up in significant privacy implications if we do not follow apple guidelines about privacy data.

Symbolication and Stack Unwinding

In order to get the actual function calls we need to apply Symbolication to the addresses. I forgot to mention that there are other stuff besides functions calls on the stack trace, so some filtering would have to be done. This whole process is called stack unwinding. The following image taken from this lecture illustrates quite well the hole process.

Image for post
Image for post

And these are not all of the problems 😞

What's the verdict? Should I build my own crash report?

Anyway, I hope this article, at least, filled your curiosity about the challenges of building a capable crash report system. Most of the information contained here was gathered from these links so you should definitely take a look at them:

I will also leave some pieces of code that I’ve found across the internet and might come in handy if you want to study more about this subject.

The Startup

Medium's largest active publication, followed by +752K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store