一 背景:
使用python执行脚本时,有些时候需要对python不同版本的某些函数实现方法做对比,为了更好的看到python的堆栈信息,我们需要编译对应的debug版本。
二 环境准备
一台ubuntu 18.04版本的linux机器,安装make、gcc、libffi、zlib(必须)等工具和库
sudo apt-get install build-essential zlib1g-dev libncurses5-dev libgdbm-dev \
libnss3-dev libssl-dev libreadline-dev libsqlite3-dev wget curl llvm \
libffi-dev liblzma-dev python3-openssl git
三 编译
wget https://www.python.org/ftp/python/3.11.4/Python-3.11.4.tar.xz # 下载3.11.4版本的源码
tar xvf Python-3.11.4.tar.xz # 解压
cd Python-3.11.4
# --prefix=/path/to/install:指定 Python 的安装目录,请将 /path/to/install 替换为你希望的实际路径。
# --with-pydebug:启用 debug 模式,这会在 Python 中添加额外的调试支持。
# --enable-optimizations=0:禁用优化,以确保代码尽可能接近源代码,便于调试。
./configure --prefix=/path/to/install --with-pydebug --enable-optimizations=0
make -j 3
make install
四 debug调试
4.1 准备一个python脚本
import threading
import time
def my_function():
print("Function is running")
print("start debug")
t = threading.Timer(2, my_function)
t.start()
print("stop debug")
4.2 设置环境变量
export LD_LIBRARY_PATH=/path/to/install/lib:$LD_LIBRARY_PATH