Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log a warning that shows up as a runtime issue in Xcode?

Tags:

Xcode 8 or 9 started displaying runtime issues. You see a purple icon at the top of the window, and a list in Issue Navigator, next to buildtime issues like compilation warnings and errors.

enter image description here

enter image description here

The runtime issues I've seen are created by the system libraries. Is there a way for my own application code to generate these?

like image 911
Rob N Avatar asked Sep 22 '17 20:09

Rob N


People also ask

How do I check logs in Xcode?

In the Xcode menu hit Run - Console. This is where NSLog / print / printf etc statements output. The key command is Command + Shift + R.

What is Xcode for Mac?

Xcode is a complete developer toolset for creating apps for Mac, iPhone, iPad, Apple Watch, and Apple TV. Xcode brings user interface design, coding, testing, debugging, and submitting to the App Store into a unified workflow.


2 Answers

Yes! You'll see these if you do something that a sanitizer catches, like performing certain UI operations a background thread with Thread Sanitizer enabled. Having an ambiguous layout and pausing in the view debugger is also a way to get this to occur. Either way, seeing this occur in Apple's libraries isn't a good thing…

UPDATE: I looked into how these warnings are hit, and LLDB essentially sets a breakpoint on a set of magic functions (__asan::AsanDie(), __tsan_on_report, __ubsan_on_report, __main_thread_checker_on_report) inside of a sanitizer dynamic library that are called when issues occur. If you define your own version of these functions and call them, you'll get the warning to show up. First, create the symbol somewhere in your project:

void __main_thread_checker_on_report(char *message) { } 

Then you can trigger it at will, just by putting this code anywhere in your application (you probably want to hide it behind a macro):

((void (*)(char *))dlsym(dlopen(NULL, RTLD_LAZY), "__main_thread_checker_on_report")))("This will show up as a warning") 

Needless to say, this will almost certainly not play nicely with the actual sanitizer if you choose to use it. You should probably compile the above conditionally based on whether you are using a sanitizer or not.

like image 110
saagarjha Avatar answered Sep 30 '22 17:09

saagarjha


In XCode 8.3 and earlier you can use set breakpoint into any method of UIKit class like setNeedsDisplay() like below.

Also there is library in objective-c steipete class in which #import <objc/runtime.h> is used.

enter image description here

But in Xcode 9 below library Xcode.app/Contenets/Developer/usr/lib/libMainThreadChecker.dylib is available, Which handle for any relevant issues potentially performed out-of-main thread at runtime.

like image 20
Jaydeep Vora Avatar answered Sep 30 '22 16:09

Jaydeep Vora