wolfpld / tracy

Frame profiler
https://tracy.nereid.pl/
Other
9.15k stars 633 forks source link

Compiling issue for Android #153

Closed SuperWangKai closed 3 years ago

SuperWangKai commented 3 years ago

Hi,

Thank you for this great solution!

I am integrating Tracy into Urho3D.

However, I find a compiling issue for Android platforms-

D:\AndroidDev\AndroidSDK\ndk\22.0.7026061\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi18 --gcc-toolchain=D:/AndroidDev/AndroidSDK/ndk/22.0.7026061/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=D:/AndroidDev/AndroidSDK/ndk/22.0.7026061/toolchains/llvm/prebuilt/windows-x86_64/sysroot  -DNOMINMAX=1 -DTRACY_ENABLE=1 -DURHO3D_ANGELSCRIPT -DURHO3D_FILEWATCHER -DURHO3D_IK -DURHO3D_LOGGING -DURHO3D_LUA -DURHO3D_NAVIGATION -DURHO3D_NETWORK -DURHO3D_PHYSICS -DURHO3D_PROFILING -DURHO3D_STATIC_DEFINE -DURHO3D_THREADING -DURHO3D_URHO2D -DURHO3D_WEBP -DWIN32_LEAN_AND_MEAN -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_SECURE_NO_DEPRECATE -D_USE_MATH_DEFINES  -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security   -std=c++11 -Wno-invalid-offsetof -Qunused-arguments -fcolor-diagnostics -Wno-argument-outside-range -O0 -fno-limit-debug-info  -fPIC -fvisibility=hidden -fvisibility-inlines-hidden -MD -MT Source/ThirdParty/Tracy/CMakeFiles/Tracy.dir/client/TracySysTrace.cpp.o -MF Source\ThirdParty\Tracy\CMakeFiles\Tracy.dir\client\TracySysTrace.cpp.o.d -o Source/ThirdParty/Tracy/CMakeFiles/Tracy.dir/client/TracySysTrace.cpp.o -c ../../../../../../Source/ThirdParty/Tracy/client/TracySysTrace.cpp
../../../../../../Source/ThirdParty/Tracy/client/TracySysTrace.cpp:638:21: error: use of undeclared identifier '__NR_perf_event_open'
    return syscall( __NR_perf_event_open, hw_event, pid, cpu, group_fd, flags );
                    ^
1 error generated.
[18/945] Building CXX object Source/ThirdParty/Tracy/CMakeFiles/Tracy.dir/client/TracyProfiler.cpp.o
ninja: build stopped: subcommand failed.

__NR_perf_event_open is not found. NDK 21.0.6113669 and 22.0.7026061 have been tried, no luck.

Best regards!

wolfpld commented 3 years ago

It seems you are missing some critical includes in the compiler parameters. The following works for me:

{android}/Sdk/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe -target armv7-none-linux-androideabi18 --sysroot {android}/Sdk/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot -march=armv7-a -I{android}/Sdk/ndk/21.3.6528147/sources/android/cpufeatures -I{android}Sdk/ndk/21.3.6528147/sources/cxx-stl/llvm-libc++/include -I{android}/Sdk/ndk/21.3.6528147/sources/cxx-stl/llvm-libc++abi/include -nostdinc++ -DANDROID -D__ANDROID__ -DTRACY_ENABLE -std=c++11 -c TracyClient.cpp -o TracyClient.o
SuperWangKai commented 3 years ago

Thank you for the reply.

I have added includes as you did, somehow, issue still exists...

   [7/85] Building CXX object Source/ThirdParty/Tracy/CMakeFiles/Tracy.dir/client/TracySysTrace.cpp.o
  FAILED: Source/ThirdParty/Tracy/CMakeFiles/Tracy.dir/client/TracySysTrace.cpp.o
  D:\AndroidDev\AndroidSDK\ndk\21.0.6113669\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi18 --gcc-toolchain=D:/AndroidDev/AndroidSDK/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=D:/AndroidDev/AndroidSDK/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64/sysroot  -DANDROID -DNOMINMAX=1 -DTRACY_ENABLE=1 -DTRACY_FILESELECTOR=0 -DTRACY_ON_DEMAND=1 -DURHO3D_ANGELSCRIPT -DURHO3D_FILEWATCHER -DURHO3D_IK -DURHO3D_LOGGING -DURHO3D_LUA -DURHO3D_NAVIGATION -DURHO3D_NETWORK -DURHO3D_PHYSICS -DURHO3D_PROFILING -DURHO3D_STATIC_DEFINE -DURHO3D_THREADING -DURHO3D_URHO2D -DURHO3D_WEBP -D__ANDROID__ -ID:/AndroidDev/AndroidSDK/ndk/21.0.6113669/sources/android/cpufeatures -ID:/AndroidDev/AndroidSDK/ndk/21.0.6113669/sources/cxx-stl/llvm-libc++/include -ID:/AndroidDev/AndroidSDK/ndk/21.0.6113669/sources/cxx-stl/llvm-libc++abi/include -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security   -std=c++11 -Wno-invalid-offsetof -Qunused-arguments -fcolor-diagnostics -Wno-argument-outside-range -O0 -fno-limit-debug-info  -fPIC -fvisibility=hidden -fvisibility-inlines-hidden   -nostdinc++ -MD -MT Source/ThirdParty/Tracy/CMakeFiles/Tracy.dir/client/TracySysTrace.cpp.o -MF Source\ThirdParty\Tracy\CMakeFiles\Tracy.dir\client\TracySysTrace.cpp.o.d -o Source/ThirdParty/Tracy/CMakeFiles/Tracy.dir/client/TracySysTrace.cpp.o -c ../../../../../../Source/ThirdParty/Tracy/client/TracySysTrace.cpp
  ../../../../../../Source/ThirdParty/Tracy/client/TracySysTrace.cpp:638:21: error: use of undeclared identifier '__NR_perf_event_open'
      return syscall( __NR_perf_event_open, hw_event, pid, cpu, group_fd, flags );
                      ^
  1 error generated.
wolfpld commented 3 years ago

I don't know why this would fail. In my ndk installation __NR_perf_event_open is present as follows:

[15:32 wolf@oberon:...ppData/Local/Android/Sdk]% grep __NR_perf_event_open * -r
ndk/21.3.6528147/sysroot/usr/include/arm-linux-androideabi/asm/unistd-common.h:#define __NR_perf_event_open (__NR_SYSCALL_BASE + 364)
ndk/21.3.6528147/sysroot/usr/include/asm-generic/unistd.h:#define __NR_perf_event_open 241
ndk/21.3.6528147/sysroot/usr/include/bits/glibc-syscalls.h:#if defined(__NR_perf_event_open)
ndk/21.3.6528147/sysroot/usr/include/bits/glibc-syscalls.h:  #define SYS_perf_event_open __NR_perf_event_open
ndk/21.3.6528147/sysroot/usr/include/i686-linux-android/asm/unistd_32.h:#define __NR_perf_event_open 336
ndk/21.3.6528147/sysroot/usr/include/i686-linux-android/asm/unistd_64.h:#define __NR_perf_event_open 298
ndk/21.3.6528147/sysroot/usr/include/i686-linux-android/asm/unistd_x32.h:#define __NR_perf_event_open (__X32_SYSCALL_BIT + 298)
ndk/21.3.6528147/sysroot/usr/include/x86_64-linux-android/asm/unistd_32.h:#define __NR_perf_event_open 336
ndk/21.3.6528147/sysroot/usr/include/x86_64-linux-android/asm/unistd_64.h:#define __NR_perf_event_open 298
ndk/21.3.6528147/sysroot/usr/include/x86_64-linux-android/asm/unistd_x32.h:#define __NR_perf_event_open (__X32_SYSCALL_BIT + 298)
ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/arm-linux-androideabi/asm/unistd-common.h:#define __NR_perf_event_open (__NR_SYSCALL_BASE + 364)
ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/asm-generic/unistd.h:#define __NR_perf_event_open 241
ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/bits/glibc-syscalls.h:#if defined(__NR_perf_event_open)
ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/bits/glibc-syscalls.h:  #define SYS_perf_event_open __NR_perf_event_open
ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/i686-linux-android/asm/unistd_32.h:#define __NR_perf_event_open 336
ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/i686-linux-android/asm/unistd_64.h:#define __NR_perf_event_open 298
ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/i686-linux-android/asm/unistd_x32.h:#define __NR_perf_event_open (__X32_SYSCALL_BIT + 298)
ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/x86_64-linux-android/asm/unistd_32.h:#define __NR_perf_event_open 336
ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/x86_64-linux-android/asm/unistd_64.h:#define __NR_perf_event_open 298
ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/x86_64-linux-android/asm/unistd_x32.h:#define __NR_perf_event_open (__X32_SYSCALL_BIT + 298)
ndk-bundle/sysroot/usr/include/arm-linux-androideabi/asm/unistd-common.h:#define __NR_perf_event_open (__NR_SYSCALL_BASE + 364)
ndk-bundle/sysroot/usr/include/asm-generic/unistd.h:#define __NR_perf_event_open 241
ndk-bundle/sysroot/usr/include/bits/glibc-syscalls.h:#if defined(__NR_perf_event_open)
ndk-bundle/sysroot/usr/include/bits/glibc-syscalls.h:  #define SYS_perf_event_open __NR_perf_event_open
ndk-bundle/sysroot/usr/include/i686-linux-android/asm/unistd_32.h:#define __NR_perf_event_open 336
ndk-bundle/sysroot/usr/include/i686-linux-android/asm/unistd_64.h:#define __NR_perf_event_open 298
ndk-bundle/sysroot/usr/include/i686-linux-android/asm/unistd_x32.h:#define __NR_perf_event_open (__X32_SYSCALL_BIT + 298)
ndk-bundle/sysroot/usr/include/x86_64-linux-android/asm/unistd_32.h:#define __NR_perf_event_open 336
ndk-bundle/sysroot/usr/include/x86_64-linux-android/asm/unistd_64.h:#define __NR_perf_event_open 298
ndk-bundle/sysroot/usr/include/x86_64-linux-android/asm/unistd_x32.h:#define __NR_perf_event_open (__X32_SYSCALL_BIT + 298)
ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/arm-linux-androideabi/asm/unistd-common.h:#define __NR_perf_event_open (__NR_SYSCALL_BASE + 364)
ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/asm-generic/unistd.h:#define __NR_perf_event_open 241
ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/bits/glibc-syscalls.h:#if defined(__NR_perf_event_open)
ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/bits/glibc-syscalls.h:  #define SYS_perf_event_open __NR_perf_event_open
ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/i686-linux-android/asm/unistd_32.h:#define __NR_perf_event_open 336
ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/i686-linux-android/asm/unistd_64.h:#define __NR_perf_event_open 298
ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/i686-linux-android/asm/unistd_x32.h:#define __NR_perf_event_open (__X32_SYSCALL_BIT + 298)
ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/x86_64-linux-android/asm/unistd_32.h:#define __NR_perf_event_open 336
ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/x86_64-linux-android/asm/unistd_64.h:#define __NR_perf_event_open 298
ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/x86_64-linux-android/asm/unistd_x32.h:#define __NR_perf_event_open (__X32_SYSCALL_BIT + 298)
SuperWangKai commented 3 years ago

Yes, it is similar on my system. However, I really have no idea which header should be used.

wolfpld commented 3 years ago

Can you check if adding #include <sys/syscall.h> fixes the problem? It should be required, according to the man page.

SuperWangKai commented 3 years ago

Thank you @wolfpld !

After adding #include <sys/syscall.h> in TracySysTrace.cpp, the compiling issue is solved! image

However, now here is the undefined tracy::rpmalloc_initialize() link issue for Android after fixing the compling issue -

Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\armeabi-v7a\lib04_StaticScene.so
  FAILED: ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/lib04_StaticScene.so
  cmd.exe /C "cd . && D:\AndroidDev\AndroidSDK\ndk\21.0.6113669\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi18 --gcc-toolchain=D:/AndroidDev/AndroidSDK/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=D:/AndroidDev/AndroidSDK/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security   -std=c++11 -Wno-invalid-offsetof -Qunused-arguments -fcolor-diagnostics -Wno-argument-outside-range -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,lib04_StaticScene.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\armeabi-v7a\lib04_StaticScene.so Samples/04_StaticScene/CMakeFiles/04_StaticScene.dir/StaticScene.cpp.o  {urho_home}/android/urho3d-lib/build/tree/Debug/armeabi-v7a/lib/libUrho3D.a -ldl -llog -landroid -lGLESv1_CM -lGLESv2 -latomic -lm && cd ."
  ../../../../../../Source/ThirdParty/Tracy/client/TracyProfiler.cpp:123: error: undefined reference to 'tracy::rpmalloc_initialize()'
  clang++: error: linker command failed with exit code 1 (use -v to see invocation)

The integration for Urho3D already worked very well on Windows before I met the issues on Android.

Thanks, Kai

SuperWangKai commented 3 years ago

Forum post in parallel universe -https://discourse.urho3d.io/t/tracy-profiler-integration-for-urho3d/6627

wolfpld commented 3 years ago

Does a467ef4c fix the issue?

SuperWangKai commented 3 years ago

Thank you @wolfpld for the commit. The changes works. In my case, rprealloc function also leads linking issue, so I also changed the signature with TRACY_API -https://github.com/wolfpld/tracy/blob/a467ef4c2b2ffe8047a08c802b363d23c473b66b/client/tracy_rpmalloc.hpp#L226


I’m using the STATIC lib of Urho3D, and lib Tracy is supposed to live inside of lib Urho3D. (Edit:)However, since Tracy is used everywhere in the Samples by including headers and inserting macros, some how, some of the symbols are just got elimiated. However, Tracy is used in the Samples by including header #include "Tracy/Tracy.hpp" and currently using these macros -

#define URHO3D_PROFILE(name) ZoneScopedN(#name)
#define URHO3D_PROFILE_FRAME() FrameMark
#define URHO3D_PROFILE_THREAD(name) tracy::SetThreadName(name)
#define URHO3D_PROFILE_FUNCTION() ZoneScopedN(__FUNCTION__)

Some how, some of the symbols are just got elimiated.

It seems I have three choices -

  1. Export all missed functions of Tracy, just like this commit (https://github.com/wolfpld/tracy/commit/a467ef4c2b2ffe8047a08c802b363d23c473b66b) is doing. But I don’t know the full list in which we include all necessary functions.
    1. Add lib Tracy to applications’ dependencis instead of just linking lib Urho3D. It is not the usual way we handle third-party libraries in Urho but still practical.
    2. I wonder if there is a way to ask the build toolchain to keep the symbols of Tracy and not optimize them from the lib Urho3D, instead delay the optimization till the application building phase?
wolfpld commented 3 years ago

In my case, rprealloc function also leads linking issue,

Fixed in 3d37c686.

I’m using the STATIC lib of Urho3D, and lib Tracy is supposed to live inside of lib Urho3D.

This configuration was not taken into consideration before. So in a way, it's good that you encountered these issues, as fixing them increases the robustness of the library.

Export all missed functions of Tracy, just like this commit (a467ef4) is doing.

This is the way to go. For more information refer to https://gcc.gnu.org/wiki/Visibility.

SuperWangKai commented 3 years ago

Thank you @wolfpld !

It's now running on Android as expected.

There are two extra issues -

  1. It seems there is no HAVE_DL_ITERATE_PHDR for the Android platform I'm building, I have to comment out #define HAVE_DL_ITERATE_PHDR 1 in libbacktrace\config.h (so a dummy implemetation of dl_iterate_phdr is used) to make it compile:
{ndk}\21.0.6113669\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi18 --gcc-toolchain={ndk}/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64 --sysroot={ndk}/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64/sysroot  -DTRACY_ENABLE=1 -DURHO3D_ANGELSCRIPT -DURHO3D_FILEWATCHER -DURHO3D_IK -DURHO3D_LOGGING -DURHO3D_LUA -DURHO3D_NAVIGATION -DURHO3D_NETWORK -DURHO3D_PHYSICS -DURHO3D_PROFILING -DURHO3D_STATIC_DEFINE -DURHO3D_THREADING -DURHO3D_URHO2D -DURHO3D_WEBP  -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security   -std=c++11 -Wno-invalid-offsetof -Qunused-arguments -fcolor-diagnostics -Wno-argument-outside-range -O0 -fno-limit-debug-info  -fPIC -fvisibility=hidden -fvisibility-inlines-hidden -MD -MT Source/ThirdParty/Tracy/CMakeFiles/Tracy.dir/libbacktrace/elf.cpp.o -MF Source\ThirdParty\Tracy\CMakeFiles\Tracy.dir\libbacktrace\elf.cpp.o.d -o Source/ThirdParty/Tracy/CMakeFiles/Tracy.dir/libbacktrace/elf.cpp.o -c ../../../../../../Source/ThirdParty/Tracy/libbacktrace/elf.cpp
  ../../../../../../Source/ThirdParty/Tracy/libbacktrace/elf.cpp:3483:3: error: use of undeclared identifier 'dl_iterate_phdr'
    dl_iterate_phdr (phdr_callback, (void *) &pd);
  1. It is not easy to seperate the client source files from the other parts of the library and integrate. When dealing with backtrace folder, I really have no idea what files are useful for a certain platform. e.g. I met these errors when building for Visual Studio: image

After studying the implementation, it is related to the TRACY_HAS_CALLSTACK level for each platform. I wonder if I can get more information about cherry picking source files from Tracy for each platform?

-Kai

wolfpld commented 3 years ago

I wonder if I can get more information about cherry picking source files from Tracy for each platform?

You should compile TracyClient.cpp (and only this file).

SuperWangKai commented 3 years ago

Thank you very much. I'd like to close the issue.