Xcode Server: Enabling Code Diagnostic Tools

Published by Shashikant Jagtap on

Xcode comes up with some cool tools that can be used to detect some crazy bugs that are hard to catch with manual or functional automated testing. The memory and threading related issues are hard to find unless they made the impact on the application. In my previous post on activating code diagnostic tools on iOS CI, we have seen that how to enable Apple’s code diagnostic tools on CI Server like TravisCI or any other third party CI server which uses xcodebuild command line tool. In this short post, how we can enable those tools using Apple’s own Continuous Integration server a.k.a Xcode Server.

Xcode Code Diagnostic Tools

Just to recap of code diagnostic tools, Xcode has four different types of runtime tools also known as code diagnostic tools to find the issues. These tools are

  • Address Sanitizer

Address Sanitizer a.k.a ASan reports the memory related issues like memory corruption and other memory-related security vulnerability issues. You can read more about the Address Sanitizer here.

  • Thread Sanitizer

Thread Sanitizer a.k.a TSan detects data races problems. It also detects other thread related issues like thread leaks. There is detailed documentation about Thread Sanitizer on Apple’s official documentation here.

  • Main Thread Checker

The Main Thread Checker is new tool launched with Xcode 9 which detects the invalid use of Apple’s frameworks like UIKit, AppKit etc that supposed to be used from main thread but accidentally used in the background thread.

  • Undefined Behaviour Sanitizer

Undefines Behaviour Sanitizer a.k.a UBSan detects undefined behaviours in the code at runtime. The behaviours like dividing by zero, loading memory from a misaligned pointer, or dereferencing a null pointer. You can read more about UBSan here.

Apple has brief documentation of all these tools on the official documentation page of code diagnostic.

We can enable all those tools from Xcode Scheme setting by Edit Scheme-> Run/Test-> Diagnostics

You can see that all those tools can be activated by using the checkboxes. One thing to note that, you cannot enable the thread sanitizer and address sanitizer at the same time.

Enabling Code Diagnostics on Xcode Server

Since Xcode 9, Xcode Server is inbuilt with Xcode application and there isn’t any need to get macOS server to start the Xcode Server. We can start the server right from the Xcode and start creating the Bots.

In Xcode 9, while creating the Bot, we have a tab called Arguments  and within arguments, we can find the section to add additional arguments Arguments passed to xcodebuild

We can use that section to enable the code diagnostics tools.

We can pass different arguments to enable address sanitizer, thread sanitizer or undefined behaviour sanitizer. In the image above we have enabled the thread sanitizer and undefined behaviour sanitizer.  Now that, our run and test phase with TSan and UBSan enabled.

Conclusion

Xcode Server has given us cool option to pass additional arguments to xcodebuild  which can be used to enable Xcode code diagnostics tools. We can explicitly pass arguments even if we are not passed in the scheme. It would be the great idea to enable those tools on Continuous Integration environments on Xcode Server to catch memory and threading issues. So don’t wait, if you are using Xcode Server enable ASan, TSan, UBSan and Main Thread Checker right now!