admin管理员组

文章数量:1417551

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).

本文标签: cNo stackframe for googletest TestBodyStack Overflow