c++ - No stackframe for google-test TestBody - Stack Overflow

I've added stacktraces to our gtest use.However with -O2 the TestBody is disappearing from the st

I've added stacktraces to our gtest use. However with -O2 the TestBody is disappearing from the stackprinting which often makes this useless as the most important call site is not shown. Any idea why or how to circumvent it?

Example

#include <iostream>
#include <stacktrace>
#include "gtest/gtest.h"

void foo()
{
    std::cout << std::stacktrace::current() << '\n';
}

TEST(example, base)
{
    foo();
}

int
main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

Results in this (desired, with -O0)

[ RUN      ] example.base
   0# foo() at /app/example.cpp:7
   1# example_base_Test::TestBody() at /app/example.cpp:12
   2# void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) at :0
   3# void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) at :0
   ...

But with -O2 the #1 disappears

[ RUN      ] example.base
   0# foo() at /app/example.cpp:7
   1# void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) at :0
   2# void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) at :0
   3# testing::Test::Run() at :0
   ...

This example on godbolt

Edit: I have tried with __attribute__((noinline)) but not succeeded yet:

void ::testing::internal::HandleSehExceptionsInMethodIfSupported<::testing::Test, void>(::testing::Test*, void (::testing::Test::*)(), char const*) 
     __attribute__((noinline));

I've added stacktraces to our gtest use. However with -O2 the TestBody is disappearing from the stackprinting which often makes this useless as the most important call site is not shown. Any idea why or how to circumvent it?

Example

#include <iostream>
#include <stacktrace>
#include "gtest/gtest.h"

void foo()
{
    std::cout << std::stacktrace::current() << '\n';
}

TEST(example, base)
{
    foo();
}

int
main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

Results in this (desired, with -O0)

[ RUN      ] example.base
   0# foo() at /app/example.cpp:7
   1# example_base_Test::TestBody() at /app/example.cpp:12
   2# void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) at :0
   3# void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) at :0
   ...

But with -O2 the #1 disappears

[ RUN      ] example.base
   0# foo() at /app/example.cpp:7
   1# void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) at :0
   2# void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) at :0
   3# testing::Test::Run() at :0
   ...

This example on godbolt

Edit: I have tried with __attribute__((noinline)) but not succeeded yet:

void ::testing::internal::HandleSehExceptionsInMethodIfSupported<::testing::Test, void>(::testing::Test*, void (::testing::Test::*)(), char const*) 
     __attribute__((noinline));
Share Improve this question edited Jan 31 at 10:17 mbschenkel asked Jan 31 at 8:56 mbschenkelmbschenkel 1,9052 gold badges18 silver badges43 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 1

Optimizer has inlined the code, so there is no frame on a stack. So no data to print.
You have to disable some optimizations to see full call-stack.

Try add compiler options: -fno-omit-frame-pointer -fno-inline -fno-optimize-sibling-calls.

Live demo.

Disclaimer: I didn'ty test if all of this flags are required (most probably all are required).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745273655a4619902.html

相关推荐

  • c++ - No stackframe for google-test TestBody - Stack Overflow

    I've added stacktraces to our gtest use.However with -O2 the TestBody is disappearing from the st

    1小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信