惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
小众软件
小众软件
量子位
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Jina AI
Jina AI
T
Threat Research - Cisco Blogs
博客园_首页
The Hacker News
The Hacker News
C
Cyber Attacks, Cyber Crime and Cyber Security
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
Security Latest
Security Latest
博客园 - 叶小钗
The Last Watchdog
The Last Watchdog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
IT之家
IT之家
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
L
Lohrmann on Cybersecurity
V
V2EX
P
Proofpoint News Feed
I
Intezer
云风的 BLOG
云风的 BLOG
Spread Privacy
Spread Privacy
罗磊的独立博客
H
Help Net Security
T
Tor Project blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
Schneier on Security
Blog — PlanetScale
Blog — PlanetScale
L
LINUX DO - 热门话题
D
DataBreaches.Net
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
Simon Willison's Weblog
Simon Willison's Weblog
Latest news
Latest news
P
Proofpoint News Feed
NISL@THU
NISL@THU
Y
Y Combinator Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
P
Palo Alto Networks Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Security @ Cisco Blogs

TinyEdi

Coroutine Bazel Notes Unix related things Shared Library I dreamt for so long The Building Blocks of Transformers A note for cmake A Strory of Mixin Tablegen Language Tutorial Lit and FileCheck 比较运算符, Min, Max, Sort 和 Order IEEE 754 的 inf 比较问题 KD树与SKD树 汉诺塔问题-记录
GDB with Python
Edimetia3D · 2024-02-19 · via TinyEdi
记录

这篇文章的主要应用场景是调试Python的C/C++ Extension

  1. 同时使用pdb / gdb 进行调试. 通俗点说, 既可以break在 .py 文件中,也可以break在 .cc 文件中
  2. 在gdb中不但可以获得常规的调试信息, 还可以获得python VM 的调试信息, 例如获得python的调用栈, 访问Python局部变量等. 这将会在调试exception时(如Segmentfalut)非常有用, 这种场景下, 定位 Python VM 正运行到哪一行代码往往可以提供一些直观的重要信息.

第一步: 编译源码以获得一些辅助数据.

我们并不真的需要使用从源码编译的Python, 但是一些调试相关的辅助文件需要从源码中获得, 包括 python-gdb.py及debug symbol等.

https://www.python.org/ftp/python/https://github.com/python/cpython 获得与你当前使用Python版本一致的源码.

一般而言,可以直接使用下面的指令

mkdir build
cd build
../configure --prefix=/path/to/install
make
make install

Note:

  1. 如果有任何问题, 参考 https://github.com/python/cpython 进行编译
  2. make install 也会直接安装好pip, 如果没有,可以参考: https://pip.pypa.io/en/stable/installation/
  3. 如果调试涉及Python自身的代码, 可以使用 –with-pydebug 进行编译. 需要注意, Python 3.8 之前 ,Debug 和 Release 的 ABI 是不同的, 这意味着,为 Release 编译的 C-Extension(包括第三方的) 可能不能在Debug版本下正常工作

第二步

设置autoloadpath

在C-Extension Load之后, 用 gdb attach 到 Python 进程中, 此时你应该可以直接break 在 C-Extension 的源码中.

在gdb中
source /path/to/source/build/python-gdb.py ,这样可以使能py-bt等调试指令.

参考 python-gdb的使用说明 https://devguide.python.org/gdb/