|
| 1 | +Debugging Native Extensions with Debug Symbols |
| 2 | +============================================== |
| 3 | + |
| 4 | +dd-trace-py is built with debug symbols by default, and packaged separately from the main wheel files to reduce the size of the primary distribution packages. |
| 5 | + |
| 6 | +Debug Symbol Files |
| 7 | +------------------ |
| 8 | + |
| 9 | +The project generates debug symbols during the build process: |
| 10 | + |
| 11 | +- **Linux**: ``.debug`` files (using ``objcopy --only-keep-debug``) |
| 12 | +- **macOS**: ``.dSYM`` bundles (using ``dsymutil``) |
| 13 | + |
| 14 | +These debug symbols are extracted from the main wheels and packaged into separate `.zip` files with the naming convention: |
| 15 | + |
| 16 | +:: |
| 17 | + |
| 18 | + {original-wheel-name}-debug-symbols.zip |
| 19 | + |
| 20 | +For example: |
| 21 | + |
| 22 | +- ``ddtrace-1.20.0-cp39-cp39-linux_x86_64.whl`` → ``ddtrace-1.20.0-cp39-cp39-linux_x86_64-debug-symbols.zip`` |
| 23 | +- ``ddtrace-1.20.0-cp39-cp39-macosx_10_9_x86_64.whl`` → ``ddtrace-1.20.0-cp39-cp39-macosx_10_9_x86_64-debug-symbols.zip`` |
| 24 | + |
| 25 | +Build Process |
| 26 | +------------- |
| 27 | + |
| 28 | +The debug symbols are handled automatically during the CI build process: |
| 29 | + |
| 30 | +1. Wheels are built with debug symbols included |
| 31 | +2. Debug symbols are extracted using the ``scripts/extract_debug_symbols.py`` script |
| 32 | +3. Debug symbols are removed from the main wheel to reduce size |
| 33 | +4. Separate debug symbol packages are created and uploaded as artifacts |
| 34 | + |
| 35 | +Usage |
| 36 | +----- |
| 37 | + |
| 38 | +To use debug symbols for debugging or crash analysis: |
| 39 | + |
| 40 | +1. Download the appropriate debug symbol package for your platform and Python version |
| 41 | +2. Extract the debug symbol files to the same directory as the corresponding `.so` files. |
| 42 | + Typically, the site-packages directory where ddtrace is installed. |
| 43 | +3. Your debugger or crash analysis tool should automatically find the debug symbols |
| 44 | +4. To view assembly with code side by side, you also need the source code, and |
| 45 | + set substitute paths in your debugger to the source code directory. For example, |
| 46 | + for ``_stack_v2.cpython-313-x86_64-linux-gnu.so`` is compiled from |
| 47 | + echion as specified in ``ddtrace/internal/datadog/profiling/stack_v2/CMakeLists.txt``. |
| 48 | + So you first need to check out the echion repository and checkout the commit hash. |
| 49 | + Then, set substitute paths in gdb to the echion source code directory. |
| 50 | + Typically, if you run ``dias /m <symbol>`` in gdb, it will tell you the full |
| 51 | + file path of the source code as the following: |
| 52 | + |
| 53 | + .. code-block:: bash |
| 54 | +
|
| 55 | + (gdb) disas /m Frame::read |
| 56 | + Dump of assembler code for function _ZN5Frame4readEP19_PyInterpreterFramePS1_: |
| 57 | + 269 /project/build/cmake.linux-x86_64-cpython-313/ddtrace.internal.datadog.profiling.stack_v2._stack_v2/_deps/echion-src/echion/frame.cc: No such file or directory. |
| 58 | + 0x000000000000ece4 <+0>: push %r12 |
| 59 | + 0x000000000000ece6 <+2>: mov %rdi,%r8 |
| 60 | + 0x000000000000ece9 <+5>: push %rbp |
| 61 | + 0x000000000000ecea <+6>: mov %rsi,%rbp |
| 62 | + 0x000000000000eced <+9>: push %rbx |
| 63 | + 0x000000000000ecee <+10>: sub $0x60,%rsp |
| 64 | +
|
| 65 | + 270 in /project/build/cmake.linux-x86_64-cpython-313/ddtrace.internal.datadog.profiling.stack_v2._stack_v2/_deps/echion-src/echion/frame.cc |
| 66 | + 271 in /project/build/cmake.linux-x86_64-cpython-313/ddtrace.internal.datadog.profiling.stack_v2._stack_v2/_deps/echion-src/echion/frame.cc |
| 67 | +
|
| 68 | + Then you can set substitute paths in gdb to the echion source code directory |
| 69 | + |
| 70 | + .. code-block:: bash |
| 71 | +
|
| 72 | + (gdb) set substitute-path /project/build/cmake.linux-x86_64-cpython-313/ddtrace.internal.datadog.profiling.stack_v2._stack_v2/_deps/echion-src/echion /path/to/echion/source/code |
| 73 | +
|
| 74 | + Run ``dias /m Frame::read`` again to see the assembly with code side by side. |
| 75 | + |
| 76 | + .. code-block:: bash |
| 77 | +
|
| 78 | + (gdb) disas /m Frame::read |
| 79 | + Dump of assembler code for function _ZN5Frame4readEP19_PyInterpreterFramePS1_: |
| 80 | + warning: Source file is more recent than executable. |
| 81 | + 269 { |
| 82 | + 0x000000000000ece4 <+0>: push %r12 |
| 83 | + 0x000000000000ece6 <+2>: mov %rdi,%r8 |
| 84 | + 0x000000000000ece9 <+5>: push %rbp |
| 85 | + 0x000000000000ecea <+6>: mov %rsi,%rbp |
| 86 | + 0x000000000000eced <+9>: push %rbx |
| 87 | + 0x000000000000ecee <+10>: sub $0x60,%rsp |
| 88 | +
|
| 89 | + 270 #if PY_VERSION_HEX >= 0x030b0000 |
| 90 | + 271 _PyInterpreterFrame iframe; |
| 91 | +
|
| 92 | + 272 #if PY_VERSION_HEX >= 0x030d0000 |
| 93 | + 273 // From Python versions 3.13, f_executable can have objects other than |
| 94 | + 274 // code objects for an internal frame. We need to skip some frames if |
| 95 | + 275 // its f_executable is not code as suggested here: |
| 96 | + 276 // https://github.com/python/cpython/issues/100987#issuecomment-1485556487 |
| 97 | + 277 PyObject f_executable; |
| 98 | +
|
| 99 | + 278 |
| 100 | + 279 for (; frame_addr; frame_addr = frame_addr->previous) |
| 101 | + 0x000000000000ecf7 <+19>: test %r8,%r8 |
| 102 | + 0x000000000000ecfa <+22>: je 0xed91 <_ZN5Frame4readEP19_PyInterpreterFramePS1_+173> |
| 103 | + 0x000000000000ed88 <+164>: mov 0x8(%rbx),%r8 |
| 104 | + 0x000000000000ed8c <+168>: jmp 0xecf7 <_ZN5Frame4readEP19_PyInterpreterFramePS1_+19> |
| 105 | +
|
| 106 | + On lldb, you can find the source code full path by running ``image lookup -n Frame::read --verbose``, |
| 107 | + and set the source code path using ``settings set target.source-map <expected-path> <actual-path>``. |
0 commit comments