怎么读取python中的gdb_python gdb

参考资料:

https://wiki.python.org/moin/DebuggingWithGdb

https://blog.csdn.net/Gamish/article/details/81632862

1 安装:sudo apt-get install gdb python2.7-dbg

2 调试:若脚本在A目录,则要在A目录执行

gdb python

3 从Ubuntu10.10开始,系统为安全考虑,默认阻止一个进程检查和修改另一个进程,除非前者是后者的父进程

阻止操作由 ptrace_scope 实现,当 ptrace_scope = 1 时,gdb 在调试运行中的进程时,会产生如下报错:

Attaching to process xxx

Could not attach to process. If your uid matches the uid of the target process,

check the setting of/proc/sys/kernel/yama/ptrace_scope, or try again asthe root user.

For more details, see/etc/sysctl.d/10-ptrace.conf

ptrace: Operation not permitted.

在配置文件 /etc/sysctl.d/10-ptrace.conf里有这么一句话

The PTRACE scope is ignored when a user has CAP_SYS_PTRACE, so “sudo strace -fp $PID” will work as before.

所以为了使ptrace_scope不起作用,需要拥有CAP_SYS_PTRACE

如何使 docker 容器具有 CAP_SYS_PTRACE ?

docker 使用 –privileged 和 –cap-add 、–cap-drop 来控制容器的权限, 如果是–privileged启动,容器将获得最大的cap,如果不是,就需要用 –cap-add 、–cap-drop 来增加或删除。

获得 CAP_SYS_PTRACE 的命令:docker run -it --cap-add SYS_PTRACE imagesname /bin/bash

至此,gdb -p PID 可正常调试。

============

起进程

1 python3 -m compileall sdf.py

2 cd __pycache__

3 python3 sdf.cpython-35.pyc

开始gdb

1 ps afx | grep sdf

2 gdb python3 -p 30031

3 py-bt

============ 原来py-list没成功,报找不到文件,是以为环境有两个版本的python,gdb时没有指定python版本

起进程

1 python3 -m sdf.py

开始gdb

1 ps afx | grep sdf

2 gdb python3 -p 30031

3 py-bt

============

起进程

1 python2.7 -m sdf.py

开始gdb

1 ps afx | grep sdf

2 gdb python2.7 -p 30031

3 py-bt

=========

如果安装的是GNU的gdb,就需要打开gdb后手动载入libpython.py脚本【可以在真正gdb前,手动载入一些模块】

(gdb) python

> import sys

>sys.path.insert(0, '/path/to/libpython.py' )

> import libpython

>end

(gdb)

============

py-list查看当前python应用程序上下文

py-bt 查看当前python应用程序调用堆栈

py-bt-full 查看当前python应用程序调用堆栈,并且显示每个frame的详细情况

py-print 查看python变量

py-locals 查看当前的scope的变量

py-up 查看上一个frame

py-down 查看下一个frame

info threads

thread 37

thread apply all py-list

py-list START

py-down

py-up

py-print x

py-locals

(gdb) python

>aa=1

>bb=2

>cc=aa+bb

>print(cc)

>end

3

(gdb)

使用generate-core-file命令生成一个coredump文件,之后可以用gdb-core来打开coredump

封装了gdb,让python调试更方便的一个git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值