centos7 下用 gdb调试python代码

首先根据https://wiki.python.org/moin/DebuggingWithGdb上去操作

1. sudo yum -y install yum-utils

2. sudo debuginfo-install glibc

此时报错

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.nju.edu.cn
 * extras: mirrors.njupt.edu.cn
 * updates: mirrors.nju.edu.cn
Could not find debuginfo for main pkg: glibc-2.17-55.el7.x86_64
Could not find debuginfo pkg for dependency package nss-softokn-freebl-3.15.4-2.el7.x86_64
No debuginfo packages available to install

解决:
vim /etc/yum.repos.d/CentOS-Debuginfo.repo
有时候这个文件是不存在的,需要创建文件
但是有时候这个文件存在,就只需要改写enabled=0变成enabled=1
它的内容是:

# CentOS-Debug.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#

# All debug packages from all the various CentOS-5 releases
# are merged into a single repo, split by BaseArch
#
# Note: packages in the debuginfo repo are currently not signed
#

[debug]
name=CentOS-7 - Debuginfo
baseurl=http://debuginfo.centos.org/7/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7
enabled=1

当写这个文件时,最后发现我是直接复制网上的内容,他的系统是centos6,所有我修改了内容将6改成了7,问题才得以解决

然后再次执行
sudo debuginfo-install glibc,还是报错:

Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7


Invalid GPG Key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7: No key found in given key data

解决:
https://github.com/xcp-ng/xcp-ng-release/blob/master/src/common/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7

vim /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7它的内容是:

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v2.0.22 (GNU/Linux)

mQENBFPE+90BCAChR7lmZuMhY3IXdMSwAFXwnJUMWJbwX1p2OR7XV/YLkKEZ+a0T
xnaEKwe9WDb7k0jUaMkeAu9ACtHKmnGrp5eCI7AJv5gF7GT1k615xyvCyGSCFTaw
VidWtXAK3RF1qKalNCj9sz8utjYVe8UXRcvHU20TlK/J1Z64Ths52UUI9kBWJN+i
QuruxEsMCyylmXeodUfQrFG+zifreCyQ0BYZEiH+6EtIzlPeCOjgjz2AWov19q/H
KXyUoiJpy40NWhEV8dazbpxrsiD5Tlc+bLjhpDTa7aPhhhUjJaSIEP3HBE01+0rz
nf00VrwQlsipRUYuv+8T5nlQ16l8IqknNLbDABEBAAG0PkNlbnRPUy03IERlYnVn
IChDZW50T1MtNyBEZWJ1Z2luZm8gUlBNUykgPHNlY3VyaXR5QGNlbnRvcy5vcmc+
iQE+BBMBAgAoBQJTxPvdAhsDBQkcMgSABgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIX
gAAKCRDQ8lo8tnksOWRFB/9PzxN2l6KSLWHPJX8OD4B7npVoouM+mscK4oHjXSRI
y2u2O2tT2hmcQzan29dahk1hsnyLdQlNSo642rV4ykbOdW9iszCLrw4ZwuQmpm5c
qeHMUPntwz9MFvn9memh85kZuxdFnAHmaxmqNl+5LEPNmmWyfX7TjGyG1Oi2xd7o
yT7lKnVXyLdY8E62RKTuQ/BNnAFpmUVgLbeKVq6qggZYgPOLt7GpLfpCm63aDZoX
OzJM75RqCtiK0n1BSBdUiVQ8+2XXYqRejFOEZa5LrLNDLFfqYA0YgpN/ournTKWt
QLHycvaoJrXZzF2EQjLiU2hHx8vS3bTR0lZsv8tUBHXG
=2CYo
-----END PGP PUBLIC KEY BLOCK-----

我将段内容直接复制到/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7文件中,还是报错
解决:
注意格式
centos7 下用 gdb调试python代码_第1张图片

如果是直接复制上去
centos7 下用 gdb调试python代码_第2张图片
中间有空格,问题不能解决,需要把空格删掉

最后问题解决:

[root@localhost ~]# sudo debuginfo-install glibc
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.nju.edu.cn
 * extras: mirrors.njupt.edu.cn
 * updates: mirrors.nju.edu.cn
--> Running transaction check
---> Package glibc-debuginfo.x86_64 0:2.17-55.el7 will be installed
--> Processing Dependency: glibc-debuginfo-common = 2.17-55.el7 for package: glibc-debuginfo-2.17-55.el7.x86_64
---> Package nss-softokn-debuginfo.x86_64 0:3.15.4-2.el7 will be installed
---> Package yum-plugin-auto-update-debug-info.noarch 0:1.1.31-45.el7 will be installed
--> Running transaction check
---> Package glibc-debuginfo-common.x86_64 0:2.17-55.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================================================================
 Package                                                         Arch                                 Version                                        Repository                           Size
===============================================================================================================================================================================================
Installing:
 glibc-debuginfo                                                 x86_64                               2.17-55.el7                                    debug                               9.2 M
 nss-softokn-debuginfo                                           x86_64                               3.15.4-2.el7                                   debug                               1.7 M
 yum-plugin-auto-update-debug-info                               noarch                               1.1.31-45.el7                                  base                                 27 k
Installing for dependencies:
 glibc-debuginfo-common                                          x86_64                               2.17-55.el7                                    debug                               8.6 M

Transaction Summary
===============================================================================================================================================================================================
Install  3 Packages (+1 Dependent package)

Total size: 19 M
Installed size: 112 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/debug/packages/glibc-debuginfo-common-2.17-55.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID b6792c39: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7
Importing GPG key 0xB6792C39:
 Userid     : "CentOS-7 Debug (CentOS-7 Debuginfo RPMS) "
 Fingerprint: 759d 690f 6099 2d52 6a35 8cbd d0f2 5a3c b679 2c39
 Package    : centos-release-7-0.1406.el7.centos.2.3.x86_64 (@anaconda)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : glibc-debuginfo-common-2.17-55.el7.x86_64                                                                                                                                   1/4 
  Installing : glibc-debuginfo-2.17-55.el7.x86_64                                                                                                                                          2/4 
  Installing : yum-plugin-auto-update-debug-info-1.1.31-45.el7.noarch                                                                                                                      3/4 
  Installing : nss-softokn-debuginfo-3.15.4-2.el7.x86_64                                                                                                                                   4/4 
  Verifying  : glibc-debuginfo-common-2.17-55.el7.x86_64                                                                                                                                   1/4 
  Verifying  : glibc-debuginfo-2.17-55.el7.x86_64                                                                                                                                          2/4 
  Verifying  : nss-softokn-debuginfo-3.15.4-2.el7.x86_64                                                                                                                                   3/4 
  Verifying  : yum-plugin-auto-update-debug-info-1.1.31-45.el7.noarch                                                                                                                      4/4 

Installed:
  glibc-debuginfo.x86_64 0:2.17-55.el7                  nss-softokn-debuginfo.x86_64 0:3.15.4-2.el7                  yum-plugin-auto-update-debug-info.noarch 0:1.1.31-45.el7                 

Dependency Installed:
  glibc-debuginfo-common.x86_64 0:2.17-55.el7                                                                                                                                                  

Complete!

3. sudo yum -y install gdb python-debuginfo

安装完成后,当我去执行gdb python,出现以内容

...
warning: the debug information found in "/usr/lib/debug//usr/bin/python2.7.debug" does not match "/usr/bin/python2.7" (CRC mismatch).


warning: the debug information found in "/usr/lib/debug/usr/bin/python2.7.debug" does not match "/usr/bin/python2.7" (CRC mismatch).

Reading symbols from /usr/bin/python2.7...(no debugging symbols found)...done.
(no debugging symbols found)...done.
Missing separate debuginfos, use: debuginfo-install python-2.7.5-16.el7.x86_64

执行:

debuginfo-install glibc-2.17-106.el7_2.8.x86_64 glibc-2.17-55.el7.x86_64 keyutils-libs-1.5.8-3.el7.x86_64 krb5-libs-1.11.3-49.el7.x86_64 libcom_err-1.42.9-4.el7libconfig-1.4.9-5.el7.x86_64 libffi-3.0.13-11.el7.x86_64 libselinux-2.2.2-6.el7.x86_64 libuui-16.el7.x86_64 libxml2-2.9.1-5.el7.x86_64 ncurses-libs-5.9-13.20130511.el7.x86_64 openssl-lib-12.el7.x86_64 pcre-8.32-12.el7.x86_64 pyOpenSSL-0.13.1-3.el7.x86_64 python-netifaces-0.10.4-6_64 python-netifaces-0.5-4.el7.x86_64 python-psutil-2.2.1-1.el7.x86_64 python-twisted-core-1el7.x86_64 python-zope-interface-4.0.5-4.el7.x86_64 readline-6.2-9.el7.x86_64 xz-libs-5.1.2-87.x86_64 zlib-1.2.7-13.el7.x86_64

以上是当我去执行gdb python -p 进程号时给的提示
还报错

...
/usr/lib64/python2.7/site-packages/_psutil_linux.so...(no debugging symbols found)...done.
(no debugging symbols found)...done.
Loaded symbols for /usr/lib64/python2.7/site-packages/_psutil_linux.so
Reading symbols from /usr/lib64/python2.7/site-packages/_psutil_posix.so...Reading symbols from /usr/lib64/python2.7/site-packages/_psutil_posix.so...(no debugging symbols found)...done.
(no debugging symbols found)...done.
Loaded symbols for /usr/lib64/python2.7/site-packages/_psutil_posix.so
0x00007f6d0576a2c3 in epoll_wait () at ../sysdeps/unix/syscall-template.S:81
81  T_PSEUDO (SYSCALL_SYMBOL, SYSCALL_NAME, SYSCALL_NARGS)
Missing separate debuginfos, use: debuginfo-install python-psutil-2.2.1-1.el7.x86_64

我就直接卸载原有的python-debuginfo的rpm包,去http://debuginfo.centos.org/7/x86_64/找到与自己机器上python版本匹配的rpm包下载后重装,就可以了,我感觉这个问题忽视掉也可以,应该是不影响的,到目前为止的是用还是正常

当我在gdb中想要执行py-listpy-bt总是显示

Undefined command: "py-list". Try "help"

解决:
我是在网上随便找个libpython.py文件,下载到本地
https://github.com/python/cpython/blob/master/Tools/gdb/libpython.py
然后执行:

(gdb) python
> import sys
# 注意这里填写的是libpython.py位于的文件夹名,例如/root,不需要写上libpython.py
>sys.path.insert(0, '/path/to/libpython.py')
> import libpython
>end
(gdb)

然后又想用pystackpystackv两个宏可用来查看python内部的栈情况,但是两者一样报错

Undefined command: .
解决:
1. 首先下载gdbinit文件使用这个扩展脚本来使用pystack、pystackv
curl -o .gdbinit http://svn.python.org/projects/python/trunk/Misc/gdbinit
2. 给刚才的就东西打个补丁
将文件中的
if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx
替换为:
if $pc > PyEval_EvalFrameEx && $pc < PyEval_EvalCodeEx && $fp != 0
注意只是在pystack 和pystackv处替换即可。

你可能感兴趣的:(gdb)