Xcode - 在断言上调用堆栈跟踪?

发布于 2024-08-22 16:28:13 字数 227 浏览 10 评论 0原文

现在,当我的断言之一在 Xcode 中被触发时,我会收到断言消息和堆栈转储,其中充满了对我来说意义不大的数字。

为了获得调用堆栈的跟踪,它需要我调试应用程序,并将其运行到发生断言的位置,并希望它再次断言。对于 100% 可重现的错误来说,这并不是什么大问题,但仍然是浪费时间。

如果每次断言被命中时我都能得到调用堆栈跟踪,那就更好了。

如何定义一个断言宏来转储 Xcode 中的调用堆栈跟踪?

Right now when one of my asserts is triggered in Xcode, I get the assert message, and a dump of the stack, which is full of numbers that are not very meaningful to me.

In order to get a trace of the call stack, it requires me to debug the application, and run it up to the point where the assert occurred, and hope that it asserts again. For bugs that are 100% reproducible, this isn't too huge of a problem, but is still a waste of time.

It would be much better if I got a call stack trace every time an assert is hit.

How do you define an assert macro that will dump a call stack trace in Xcode?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反目相谮 2024-08-29 16:28:13

NSThread 有一个名为 callStackSymbols 的类方法(并且 NSException 有一个同名的实例方法)。抱歉,我不经常使用异常,也不经常使用断言(不为这两个事实感到自豪),所以我不确定断言宏应该做什么。

#define AssertWithStackSymbols(x) \
do { \
    if (!(x)) { \
        NSLog (@"%s failed assertion\n%@", #x, [NSThread callStackSymbols]); \
        abort(); \
    } \
} while(0) 

或者,正如 KennyTM 善意指出的那样,您可以使用 backtrace_symbols。甚至有一种方法可以将符号直接输出到文件描述符,backtrace_symbols_fd

#define AssertWithStackSymbols(x) \
do { \
    if (!(x)) { \
        void *stack[128]; \
        int count; \
        fputs (#x " failed assertion.\n", stderr); \
        count = backtrace (stack, sizeof stack / sizeof (void *)); \
        backtrace_symbols_fd (stack, count, STDERR_FILENO); \
    } \
while (0)

NSThread has a class method called callStackSymbols (and NSException has an instance method of the same name). Sorry, I don't regularly use exceptions and don't regularly use asserts either (not proud of either fact), so I'm not sure what the assertion macro should do.

#define AssertWithStackSymbols(x) \
do { \
    if (!(x)) { \
        NSLog (@"%s failed assertion\n%@", #x, [NSThread callStackSymbols]); \
        abort(); \
    } \
} while(0) 

Or, as KennyTM kindly pointed out, you can use backtrace_symbols. There is even a method that outputs the symbols directly to a file descriptor, backtrace_symbols_fd.

#define AssertWithStackSymbols(x) \
do { \
    if (!(x)) { \
        void *stack[128]; \
        int count; \
        fputs (#x " failed assertion.\n", stderr); \
        count = backtrace (stack, sizeof stack / sizeof (void *)); \
        backtrace_symbols_fd (stack, count, STDERR_FILENO); \
    } \
while (0)
苏璃陌 2024-08-29 16:28:13

在 iOS 4.x 上你可以使用 [NSThread callStackSymbols] 。

On iOS 4.x you can use [NSThread callStackSymbols] so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文