Skip to content

Commit

Permalink
Add a test client that links statically and dynamically to a library
Browse files Browse the repository at this point in the history
This shows that we somehow don't properly resolve the inlined frame
for the call to `foo` and instead only show `bar`. But this seems
to affect all tools, including perf and addr2line and happens even
in debug builds:

```
cpp-libs 73106 [005]  7235.260003:     120673             cycles:
                    15c2 std::generate_canonical<double, 53ul, std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul> >+0x192 (inlined)
                    15c2 std::__detail::_Adaptor<std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul>, double>::operator()+0x192 (inlined)
                    15c2 std::uniform_real_distribution<double>::operator()<std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul> >+0x192 (inlined)
                    15c2 std::uniform_real_distribution<double>::operator()<std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul> >+0x192 (inlined)
                    15c2 StaticLib::bar+0x192 (/home/milian/projects/kdab/hotspot/build-debug/tests/test-clients/cpp-libs/cpp-libs)
                    1391 main+0x61 (/home/milian/projects/kdab/hotspot/build-debug/tests/test-clients/cpp-libs/cpp-libs)
                   2d30f __libc_start_call_main+0x7f (/usr/lib/libc.so.6)
                   2d3c0 __libc_start_main_alias_2+0x80 (inlined)
                    10c4 _start+0x24 (/home/milian/projects/kdab/hotspot/build-debug/tests/test-clients/cpp-libs/cpp-libs)
```

The first chunk of that is seemingly correct:
```
eu-addr2line -C -f -i -e /home/milian/projects/kdab/hotspot/build-debug/tests/test-clients/cpp-libs/cpp-libs -a 15c2
0x00000000000015c2
double std::generate_canonical<double, 53ul, std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul> >(std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul>&) inlined at /usr/include/c++/11.2.0/bits/random.h:192:38 in StaticLib::bar(unsigned long) const
/usr/include/c++/11.2.0/bits/random.tcc:3369:10
std::__detail::_Adaptor<std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul>, double>::operator()()
/usr/include/c++/11.2.0/bits/random.h:192:38
double std::uniform_real_distribution<double>::operator()<std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul> >(std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul>&, std::uniform_real_distribution<double>::param_type const&)
/usr/include/c++/11.2.0/bits/random.h:1870:19
double std::uniform_real_distribution<double>::operator()<std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul> >(std::linear_congruential_engine<unsigned long, 16807ul, 0ul, 2147483647ul>&)
/usr/include/c++/11.2.0/bits/random.h:1861:34
StaticLib::bar(unsigned long) const
/home/milian/projects/kdab/hotspot/tests/test-clients/cpp-libs/staticlib.cpp:26:18
```

But then the call to `StaticLib::foo` is seemingly lost, i.e.:

```
$ objdump -S cpp-libs
...
    1388:       48 89 44 24 20          mov    %rax,0x20(%rsp)
    138d:       e8 3e 03 00 00          call   16d0 <StaticLib::foo() const>
      { return _M_insert(__f); }
    1392:       48 8d 3d e7 1c 00 00    lea    0x1ce7(%rip),%rdi        # 3080 <std::cout@GLIBCXX_3.4>
    1399:       e8 82 fc ff ff          call   1020 <std::ostream& std::ostream::_M_insert<double>(double)@plt>
    { return __ostream_insert(__out, &__c, 1); }
```

The address offset 1391 from the background should be pointing to the
`call` instruction at `138d` I guess. But there we only get:

```
$ eu-addr2line -C -f -i -e /home/milian/projects/kdab/hotspot/build-debug/tests/test-clients/cpp-libs/cpp-libs -a 1391
0x0000000000001391
main
/home/milian/projects/kdab/hotspot/tests/test-clients/cpp-libs/main.cpp:22:32
```

The output is the same also for offset `138d`. And even GDB gets it
wrong in this case:

```
(gdb) b StaticLib::bar
(gdb) c
...
#0  StaticLib::bar (this=0x7fffffffc728, max=100000800) at /home/milian/projects/kdab/hotspot/tests/test-clients/cpp-libs/staticlib.cpp:25
#1  0x0000555555555392 in main () at /home/milian/projects/kdab/hotspot/tests/test-clients/cpp-libs/main.cpp:22
```
  • Loading branch information
milianw committed Apr 15, 2022
1 parent ea57809 commit fe4fab8
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/test-clients/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ add_subdirectory(cpp-threadnames)
add_subdirectory(cpp-parallel)
add_subdirectory(c-fork)
add_subdirectory(callgraph)
add_subdirectory(cpp-libs)
6 changes: 6 additions & 0 deletions tests/test-clients/cpp-libs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
set(CMAKE_BUILD_TYPE RelWithDebInfo)

add_executable(cpp-libs main.cpp)
add_library(cpp-libs-static STATIC staticlib.cpp)
add_library(cpp-libs-shared SHARED sharedlib.cpp)
target_link_libraries(cpp-libs cpp-libs-static cpp-libs-shared)
25 changes: 25 additions & 0 deletions tests/test-clients/cpp-libs/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
SPDX-FileCopyrightText: Milian Wolff <milian.wolff@kdab.com>
SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "sharedlib.h"
#include "staticlib.h"

#include <iostream>
#include <random>

int main()
{
std::uniform_int_distribution<unsigned long> uniform(100000000, 200000000);
std::default_random_engine engine;

StaticLib staticLib(uniform(engine));
SharedLib sharedLib(uniform(engine));

std::cout << staticLib.foo() << '\n' << sharedLib.foo() << '\n';

return 0;
}
34 changes: 34 additions & 0 deletions tests/test-clients/cpp-libs/sharedlib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
SPDX-FileCopyrightText: Milian Wolff <milian.wolff@kdab.com>
SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "sharedlib.h"

#include <complex>
#include <random>

namespace {
double asdf(double a, double b)
{
return std::norm(std::complex<double>(a, b));
}
}

double SharedLib::bar(unsigned long max) const
{
std::uniform_real_distribution<double> uniform(-1E5, 1E5);
std::default_random_engine engine;
double s = 0;
for (int i = 0; i < max; ++i) {
s += asdf(uniform(engine), uniform(engine));
}
return s;
}

double SharedLib::foo() const
{
return bar(m_max);
}
22 changes: 22 additions & 0 deletions tests/test-clients/cpp-libs/sharedlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
SPDX-FileCopyrightText: Milian Wolff <milian.wolff@kdab.com>
SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
SPDX-License-Identifier: GPL-2.0-or-later
*/

#pragma once

class SharedLib
{
unsigned long m_max;
public:
SharedLib(unsigned long max)
: m_max(max)
{}

double foo() const;

private:
double bar(unsigned long) const;
};
34 changes: 34 additions & 0 deletions tests/test-clients/cpp-libs/staticlib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
SPDX-FileCopyrightText: Milian Wolff <milian.wolff@kdab.com>
SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "staticlib.h"

#include <complex>
#include <random>

namespace {
double asdf(double a, double b)
{
return std::norm(std::complex<double>(a, b));
}
}

double StaticLib::bar(unsigned long max) const
{
std::uniform_real_distribution<double> uniform(-1E5, 1E5);
std::default_random_engine engine;
double s = 0;
for (int i = 0; i < max; ++i) {
s += asdf(uniform(engine), uniform(engine));
}
return s;
}

double StaticLib::foo() const
{
return bar(m_max);
}
22 changes: 22 additions & 0 deletions tests/test-clients/cpp-libs/staticlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
SPDX-FileCopyrightText: Milian Wolff <milian.wolff@kdab.com>
SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
SPDX-License-Identifier: GPL-2.0-or-later
*/

#pragma once

class StaticLib
{
unsigned long m_max;
public:
StaticLib(unsigned long max)
: m_max(max)
{}

double foo() const;

private:
double bar(unsigned long) const;
};

0 comments on commit fe4fab8

Please sign in to comment.