如何记录在Xcode中显示为运行时问题的警告? [英] How to log a warning that shows up as a runtime issue in Xcode?

查看:141
本文介绍了如何记录在Xcode中显示为运行时问题的警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xcode 8或9开始显示运行时问题.您会在窗口顶部看到一个紫色图标,并在Issue Navigator中看到一个列表,该列表旁边是编译警告和错误之类的构建时问题.

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.

我所看到的运行时问题是由系统库创建的.我自己的应用程序代码是否有办法生成这些代码?

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

推荐答案

是的!如果您执行清除程序捕获的操作,则将看到这些内容,例如在启用Thread Sanitizer的情况下在后台线程中执行某些UI操作.布局不明确并在视图调试器中暂停也是使这种情况发生的一种方法.无论哪种方式,在Apple的库中看到这种情况都不是一件好事……

更新:我研究了如何发出这些警告,并且LLDB本质上在一组魔术函数上设置了一个断点(

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.

这篇关于如何记录在Xcode中显示为运行时问题的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆