Things are a bit different in projects that consist of multiple DLLs/shared objects. Compiling \texttt{TracyClient.cpp} into every DLL is not an option because this would result in several instances of Tracy objects lying around in the process. We instead need to pass their instances to the different DLLs to be reused there.For that, you need a \emph{profiler DLL} to which your executable and the other DLLs link. If that doesn't exist, you have to create one explicitly for Tracy\footnote{You can use the top-level Meson or CMake build scripts to get it. Make sure that the same build flags are set for both the library and your application, or you may find yourself chasing weird issues.}. This library should contain the \texttt{public/TracyClient.cpp} source file. Link the executable and all DLLs you want to profile to this DLL.If you are targeting Windows with Microsoft Visual Studio or MinGW, add the \texttt{TRACY\_IMPORTS} define to your application.
(gdb) thread apply all btThread 5 (Thread 0xffffa523b1e0 (LWP 2667747)):#0 __lll_lock_wait (futex=futex@entry=0xffffb72a09e8 <_rtld_global+2440>, private=0) at lowlevellock.c:52#1 0x0000ffffb709ccec in __GI___pthread_mutex_lock (mutex=0xffffb72a09e8 <_rtld_global+2440>) at pthread_mutex_lock.c:115#2 0x0000ffffb71cd03c in __GI__dl_addr (address=0x0, info=0xffffa523a928, mapp=0x0, symbolp=0x0) at dl-addr.c:131#3 0x0000ffffb6507ffc in tracy::InitCallstack() () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#4 0x0000ffffb65198ec in tracy::Profiler::SymbolWorker() () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#5 0x0000ffffb651cee8 in tracy::Thread::Launch(void*) () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#6 0x0000ffffb709a624 in start_thread (arg=0xffffb651ced8 <tracy::Thread::Launch(void*)>) at pthread_create.c:477#7 0x0000ffffb719549c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78Thread 4 (Thread 0xffffa5e4c1e0 (LWP 2667746)):#0 0x0000ffffb71625cc in __GI___clock_nanosleep (clock_id=<optimized out>, clock_id@entry=0, flags=flags@entry=0, req=<optimized out>, rem=0xffffa5e4b990) at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:78#1 0x0000ffffb7167f54 in __GI___nanosleep (requested_time=<optimized out>, remaining=<optimized out>) at nanosleep.c:27#2 0x0000ffffb650b8f8 in tracy::Profiler::CompressWorker() () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#3 0x0000ffffb651cee8 in tracy::Thread::Launch(void*) () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#4 0x0000ffffb709a624 in start_thread (arg=0xffffb651ced8 <tracy::Thread::Launch(void*)>) at pthread_create.c:477#5 0x0000ffffb719549c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78Thread 3 (Thread 0xffffa664d1e0 (LWP 2667745)):#0 0x0000ffffb718bf08 in __GI___poll (fds=0xffffa664b200, nfds=1, timeout=<optimized out>) at ../sysdeps/unix/sysv/linux/poll.c:41#1 0x0000ffffb64f8f58 in tracy::ListenSocket::Accept() () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#2 0x0000ffffb6518254 in tracy::Profiler::Worker() () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#3 0x0000ffffb651cee8 in tracy::Thread::Launch(void*) () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#4 0x0000ffffb709a624 in start_thread (arg=0xffffb651ced8 <tracy::Thread::Launch(void*)>) at pthread_create.c:477#5 0x0000ffffb719549c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78Thread 2 (Thread 0xffffa725e1e0 (LWP 2667744)):#0 0x0000ffffb71625cc in __GI___clock_nanosleep (clock_id=<optimized out>, clock_id@entry=0, flags=flags@entry=0, req=<optimized out>, rem=0xffffa725c410) at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:78#1 0x0000ffffb7167f54 in __GI___nanosleep (requested_time=<optimized out>, remaining=<optimized out>) at nanosleep.c:27#2 0x0000ffffb6516200 in tracy::SysTraceWorker(void*) () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#3 0x0000ffffb651cee8 in tracy::Thread::Launch(void*) () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#4 0x0000ffffb709a624 in start_thread (arg=0xffffb651ced8 <tracy::Thread::Launch(void*)>) at pthread_create.c:477#5 0x0000ffffb719549c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78Thread 1 (Thread 0xffffb7297010 (LWP 2667742)):#0 0x0000ffffb650cd04 in tracy::Profiler::CalibrateDelay() () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#1 0x0000ffffb650d798 in tracy::Profiler::Profiler() () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#2 0x0000ffffb649cbf8 in ?? () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#3 0x0000ffffb727c8b4 in call_init (l=<optimized out>, argc=argc@entry=3, argv=argv@entry=0xffffd4e46928, env=env@entry=0xffffd4e46948) at dl-init.c:72#4 0x0000ffffb727c9b4 in call_init (env=0xffffd4e46948, argv=0xffffd4e46928, argc=3, l=<optimized out>) at dl-init.c:30#5 _dl_init (main_map=0x11858f20, argc=3, argv=0xffffd4e46928, env=0xffffd4e46948) at dl-init.c:119#6 0x0000ffffb71ce07c in __GI__dl_catch_exception (exception=exception@entry=0x0, operate=operate@entry=0xffffb727fcc8 <call_dl_init>, args=0xffffd4e44970, args@entry=0xffffd4e44a00) at dl-error-skeleton.c:182#7 0x0000ffffb7280a14 in dl_open_worker (a=a@entry=0xffffd4e44bc0) at dl-open.c:758#8 0x0000ffffb71ce01c in __GI__dl_catch_exception (exception=exception@entry=0xffffd4e44ba8, operate=operate@entry=0xffffb7280540 <dl_open_worker>, args=args@entry=0xffffd4e44bc0) at dl-error-skeleton.c:208#9 0x0000ffffb72801a4 in _dl_open (file=0xffffb692d280 "/home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so", mode=-2147483646, caller_dlopen=0x63e728 <_PyImport_FindSharedFuncptr+104>, nsid=-2, argc=3, argv=0xffffd4e46928, env=0xffffd4e46948) at dl-open.c:837#10 0x0000ffffb708009c in dlopen_doit (a=a@entry=0xffffd4e44e88) at dlopen.c:66#11 0x0000ffffb71ce01c in __GI__dl_catch_exception (exception=exception@entry=0xffffd4e44e00, operate=operate@entry=0xffffb7080038 <dlopen_doit>, args=args@entry=0xffffd4e44e88) at dl-error-skeleton.c:208#12 0x0000ffffb71ce0e8 in __GI__dl_catch_error (objname=objname@entry=0x118a1890, errstring=errstring@entry=0x118a1898, mallocedp=mallocedp@entry=0x118a1888, operate=operate@entry=0xffffb7080038 <dlopen_doit>, args=args@entry=0xffffd4e44e88) at dl-error-skeleton.c:227#13 0x0000ffffb7080838 in _dlerror_run (operate=operate@entry=0xffffb7080038 <dlopen_doit>, args=args@entry=0xffffd4e44e88) at dlerror.c:170#14 0x0000ffffb7080140 in __dlopen (file=<optimized out>, mode=<optimized out>) at dlopen.c:87#15 0x000000000063e728 in _PyImport_FindSharedFuncptr ()#16 0x0000000000654610 in _PyImport_LoadDynamicModuleWithSpec ()#17 0x000000000065681c in ?? ()#18 0x000000000055ef44 in ?? ()#19 0x0000000000591ea8 in PyVectorcall_Call ()#20 0x00000000005040fc in _PyEval_EvalFrameDefault ()#21 0x00000000004fca5c in _PyEval_EvalCodeWithName ()#22 0x0000000000596454 in _PyFunction_Vectorcall ()#23 0x0000000000502e38 in _PyEval_EvalFrameDefault ()#24 0x0000000000596260 in _PyFunction_Vectorcall ()#25 0x00000000004fe828 in _PyEval_EvalFrameDefault ()#26 0x0000000000596260 in _PyFunction_Vectorcall ()#27 0x00000000004fe700 in _PyEval_EvalFrameDefault ()#28 0x0000000000596260 in _PyFunction_Vectorcall ()#29 0x00000000004fe700 in _PyEval_EvalFrameDefault ()#30 0x0000000000596260 in _PyFunction_Vectorcall ()#31 0x00000000004fe700 in _PyEval_EvalFrameDefault ()#32 0x0000000000596260 in _PyFunction_Vectorcall ()#33 0x0000000000593510 in ?? ()#34 0x0000000000593a58 in _PyObject_CallMethodIdObjArgs ()#35 0x00000000004e345c in PyImport_ImportModuleLevelObject ()#36 0x000000000050082c in _PyEval_EvalFrameDefault ()#37 0x00000000004fca5c in _PyEval_EvalCodeWithName ()#38 0x0000000000660eb0 in PyEval_EvalCode ()#39 0x000000000064cf40 in ?? ()#40 0x000000000064d00c in ?? ()#41 0x000000000064d1c0 in PyRun_StringFlags ()#42 0x000000000064d230 in PyRun_SimpleStringFlags ()#43 0x000000000069b32c in Py_RunMain ()#44 0x000000000069ba4c in Py_BytesMain ()#45 0x0000ffffb70e4e10 in __libc_start_main (main=0x59a6e4 <_start+56>, argc=3, argv=0xffffd4e46928, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=<optimized out>) at ../csu/libc-start.c:308#46 0x000000000059a6e0 in _start ()Backtrace stopped: previous frame identical to this frame (corrupt stack?)(gdb)
分析打开 tracy 时的 strace,对应的源码是什么
关注 gettid 时对应的源码是什么:
gdb --args python3 -c "import dataflow_py"(gdb) catch syscall gettid(gdb) run(gdb) bt#0 syscall () at ../sysdeps/unix/sysv/linux/aarch64/syscall.S:39#1 0x0000ffffbe93cf90 in tracy::detail::GetThreadHandleImpl() () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#2 0x0000ffffbe96c5b4 in tracy::Profiler::Profiler() () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#3 0x0000ffffbe2e7178 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=105) at /workspace/nova_dataflow/third-party/tracy/public/common/TracyStackFrames.cpp:122#4 __static_initialization_and_destruction_0 (__priority=105, __initialize_p=1) at /workspace/nova_dataflow/third-party/tracy/public/common/TracyStackFrames.cpp:122#5 _GLOBAL__sub_I.00105_TracyClient.cpp(void) () at /workspace/nova_dataflow/third-party/tracy/public/common/TracyStackFrames.cpp:122#6 0x0000ffffbf6db8b4 in call_init (l=<optimized out>, argc=argc@entry=3, argv=argv@entry=0xfffffffff128, env=env@entry=0xfffffffff148) at dl-init.c:72#7 0x0000ffffbf6db9b4 in call_init (env=0xfffffffff148, argv=0xfffffffff128, argc=3, l=<optimized out>) at dl-init.c:30#8 _dl_init (main_map=0x967fa0, argc=3, argv=0xfffffffff128, env=0xfffffffff148) at dl-init.c:119#9 0x0000ffffbf62d07c in __GI__dl_catch_exception (exception=0x0, operate=0xffffbf6decc8 <call_dl_init>, args=0xffffffffd170) at dl-error-skeleton.c:182#10 0x0000ffffbf6dfa14 in dl_open_worker (a=a@entry=0xffffffffd3c0) at dl-open.c:758#11 0x0000ffffbf62d01c in __GI__dl_catch_exception (exception=0xffffffffd3a8, operate=0xffffbf6df540 <dl_open_worker>, args=0xffffffffd3c0) at dl-error-skeleton.c:208#12 0x0000ffffbf6df1a4 in _dl_open (file=0xffffbed8c280 "/home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so", mode=-2147483646, caller_dlopen=0x63e728 <_PyImport_FindSharedFuncptr+104>, nsid=-2, argc=3, argv=0xfffffffff128, env=0xfffffffff148) at dl-open.c:837#13 0x0000ffffbf4df09c in dlopen_doit (a=a@entry=0xffffffffd688) at dlopen.c:66#14 0x0000ffffbf62d01c in __GI__dl_catch_exception (exception=exception@entry=0xffffffffd600, operate=0xffffbf4df038 <dlopen_doit>, args=0xffffffffd688) at dl-error-skeleton.c:208#15 0x0000ffffbf62d0e8 in __GI__dl_catch_error (objname=0x9b0780, errstring=0x9b0788, mallocedp=0x9b0778, operate=<optimized out>, args=<optimized out>) at dl-error-skeleton.c:227#16 0x0000ffffbf4df838 in _dlerror_run (operate=operate@entry=0xffffbf4df038 <dlopen_doit>, args=args@entry=0xffffffffd688) at dlerror.c:170#17 0x0000ffffbf4df140 in __dlopen (file=<optimized out>, mode=<optimized out>) at dlopen.c:87#18 0x000000000063e728 in _PyImport_FindSharedFuncptr ()#19 0x0000000000654610 in _PyImport_LoadDynamicModuleWithSpec ()#20 0x000000000065681c in ?? ()#21 0x000000000055ef44 in ?? ()#22 0x0000000000591ea8 in PyVectorcall_Call ()#23 0x00000000005040fc in _PyEval_EvalFrameDefault ()#24 0x00000000004fca5c in _PyEval_EvalCodeWithName ()#25 0x0000000000596454 in _PyFunction_Vectorcall ()#26 0x0000000000502e38 in _PyEval_EvalFrameDefault ()#27 0x0000000000596260 in _PyFunction_Vectorcall ()#28 0x00000000004fe828 in _PyEval_EvalFrameDefault ()#29 0x0000000000596260 in _PyFunction_Vectorcall ()#30 0x00000000004fe700 in _PyEval_EvalFrameDefault ()#31 0x0000000000596260 in _PyFunction_Vectorcall ()#32 0x00000000004fe700 in _PyEval_EvalFrameDefault ()#33 0x0000000000596260 in _PyFunction_Vectorcall ()#34 0x00000000004fe700 in _PyEval_EvalFrameDefault ()#35 0x0000000000596260 in _PyFunction_Vectorcall ()#36 0x0000000000593510 in ?? ()#37 0x0000000000593a58 in _PyObject_CallMethodIdObjArgs ()#38 0x00000000004e345c in PyImport_ImportModuleLevelObject ()#39 0x000000000050082c in _PyEval_EvalFrameDefault ()#40 0x00000000004fca5c in _PyEval_EvalCodeWithName ()#41 0x0000000000660eb0 in PyEval_EvalCode ()#42 0x000000000064cf40 in ?? ()#43 0x000000000064d00c in ?? ()#44 0x000000000064d1c0 in PyRun_StringFlags ()#45 0x000000000064d230 in PyRun_SimpleStringFlags ()#46 0x000000000069b32c in Py_RunMain ()#47 0x000000000069ba4c in Py_BytesMain ()#48 0x0000ffffbf543e10 in __libc_start_main (main=0x59a6e4 <_start+56>, argc=3, argv=0xfffffffff128, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=<optimized out>) at ../csu/libc-start.c:308#49 0x000000000059a6e0 in _start ()Backtrace stopped: previous frame identical to this frame (corrupt stack?)
重点关注
#0 syscall () at ../sysdeps/unix/sysv/linux/aarch64/syscall.S:39#1 0x0000ffffbe93cf90 in tracy::detail::GetThreadHandleImpl() () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#2 0x0000ffffbe96c5b4 in tracy::Profiler::Profiler() () from /home/admin/test/install/lib/dataflow_py.cpython-38-aarch64-linux-gnu.so#3 0x0000ffffbe2e7178 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=105) at /workspace/nova_dataflow/third-party/tracy/public/common/TracyStackFrames.cpp:122#4 __static_initialization_and_destruction_0 (__priority=105, __initialize_p=1) at /workspace/nova_dataflow/third-party/tracy/public/common/TracyStackFrames.cpp:122#5 _GLOBAL__sub_I.00105_TracyClient.cpp(void) () at /workspace/nova_dataflow/third-party/tracy/public/common/TracyStackFrames.cpp:122