PYTHON 代码,尤其是别人写的代码看不懂。怎么办? 其实PYTHON中也提供了类似于C语言中用于debug 的 gdb。它叫做pdb。结合本人自己的学习,进行简单的举例,以做备忘和补偿学习。
首先参考资料:
1、
2、
以 shadowsocks 的 local.py 代码为例子,演示相应的基本命令使用。
local.py 代码:
#!/usr/bin/env python# -*- coding: utf-8 -*-## Copyright 2012-2015 clowwindy## Licensed under the Apache License, Version 2.0 (the "License"); you may# not use this file except in compliance with the License. You may obtain# a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the# License for the specific language governing permissions and limitations# under the License.from __future__ import absolute_import, division, print_function, \ with_statementimport sysimport osimport loggingimport signalsys.path.insert(0, os.path.join(os.path.dirname(__file__), '../'))from shadowsocks import shell, daemon, eventloop, tcprelay, udprelay, asyncdnsdef main(): shell.check_python() # fix py2exe if hasattr(sys, "frozen") and sys.frozen in \ ("windows_exe", "console_exe"): p = os.path.dirname(os.path.abspath(sys.executable)) os.chdir(p) config = shell.get_config(True) daemon.daemon_exec(config) try: logging.info("starting local at %s:%d" % (config['local_address'], config['local_port'])) dns_resolver = asyncdns.DNSResolver() tcp_server = tcprelay.TCPRelay(config, dns_resolver, True) udp_server = udprelay.UDPRelay(config, dns_resolver, True) loop = eventloop.EventLoop() dns_resolver.add_to_loop(loop) tcp_server.add_to_loop(loop) udp_server.add_to_loop(loop) def handler(signum, _): logging.warn('received SIGQUIT, doing graceful shutting down..') tcp_server.close(next_tick=True) udp_server.close(next_tick=True) signal.signal(getattr(signal, 'SIGQUIT', signal.SIGTERM), handler) def int_handler(signum, _): sys.exit(1) signal.signal(signal.SIGINT, int_handler) daemon.set_user(config.get('user', None)) loop.run() except Exception as e: shell.print_exception(e) sys.exit(1)if __name__ == '__main__': main()
为了配合测试,写了一个假的配置文件config.json:
{ "server":"127.0j.0.1", "server_port":8388, "local_port":10808, "password":"bgt56yhn", "timeout":600, "method":null}
一、如何使用pdb 进行调试和获取帮助
1、脚本启动时,即载入pdb 调试信息
python -m pdb scriptfile [arg] #此中情况,程序在代码的第一行设置了一个断点
2、更改脚本加入pdb 调试信息
import pdbpdb.set_trace() # 在程序某处设置断点
OK ,我们这里为了图简单,就不去更改local.py 源代码了,直接使用第一种方法去调试
$ python -m pdb local.py -c config.json> /home/test/python/shadowsocks/shadowsocks/local.py(18)()-> from __future__ import absolute_import, division, print_function, \(Pdb) helpDocumented commands (type help ):========================================EOF bt cont enable jump pp run unt a c continue exit l q s until alias cl d h list quit step up args clear debug help n r tbreak w b commands disable ignore next restart u whatisbreak condition down j p return unalias where Miscellaneous help topics:==========================exec pdbUndocumented commands:======================retval rv
从以上结果也可以看出,默认代码的第一行为断点(只是一个假象的断点,显示断点指令是看不到的),程序停留在此处。
在pdb 状态下,使用help 指令可以获取pdb的帮助信息。
二、n(next)
n(next) 输入的时候,可以执行代码的下一行。
(Pdb) n> /home/test/python/shadowsocks/shadowsocks/local.py(21)()-> import sys(Pdb) n> /home/test/python/shadowsocks/shadowsocks/local.py(22) ()-> import os(Pdb) n> /home/test/python/shadowsocks/shadowsocks/local.py(23) ()-> import logging(Pdb) # 此处为空白,按了一个回车键> /home/test/python/shadowsocks/shadowsocks/local.py(24) ()-> import signal(Pdb) # 此处为空白,按了一个回车键> /home/test/python/shadowsocks/shadowsocks/local.py(26) ()-> sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../'))(Pdb)
注意:
一个很牛的特性是你可以单击回车键来执行以前的命令(在上面的例子中执行的指令为n)。三、s(step) 、 b(break) 和 c(continue) 指令
s(step) 输入的时候,可以进入这行代码中的相关函数去执行
b num 输入的时候,是在某行(num)上设置一个断点。若直接输入b ,则显示所有的断点
本来,打算讲s(step)指令和 n(next)指令放到一起,做个比较。不过我更感觉s(step) 指令应该和b(break) 及 c(continue)结合起来一起用,这样感觉效率上更高。
以实际操作去说话,我想在main()函数上打一个断点,然后直接走到这个断点,最后进入main函数。
前提,我知道了 main() 函数位于 72行(函数位于文件中的哪一行,这个靠自己了)
> /home/test/python/shadowsocks/shadowsocks/local.py(18)()-> from __future__ import absolute_import, division, print_function, \(Pdb) b 72 # 在72 行设置一个断点Breakpoint 1 at /home/test/python/shadowsocks/shadowsocks/local.py:72(Pdb) b # 显示所有的断点Num Type Disp Enb Where1 breakpoint keep yes at /home/test/python/shadowsocks/shadowsocks/local.py:72(Pdb) c # 直接走到这个断点处> /home/test/python/shadowsocks/shadowsocks/local.py(72) ()-> main()(Pdb) s # s ,进入main 函数--Call--> /home/dexin/python/shadowsocks/shadowsocks/local.py(30)main()-> def main():(Pdb) l 25 26 sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../')) 27 from shadowsocks import shell, daemon, eventloop, tcprelay, udprelay, asyncdns 28 29 30 -> def main(): 31 shell.check_python() 32 33 # fix py2exe 34 if hasattr(sys, "frozen") and sys.frozen in \ 35 ("windows_exe", "console_exe"):(Pdb) n # n 移动到下一行> /home/dexin/python/shadowsocks/shadowsocks/local.py(31)main()-> shell.check_python()(Pdb) s # s 进入到 check_python 函数--Call--> /home/dexin/python/shadowsocks/shadowsocks/shell.py(35)check_python()-> def check_python():(Pdb)
以上调试中,用的了 l(list) 指令,这个指令的意思为显示代码。默认什么参数也没有的情况下。
显示当前行上下共11行代码。
四、clear num 清除先前设置的断点 ,这里的num 为第几个断点的意思
(Pdb) b 34Breakpoint 1 at /home/test/python/shadowsocks/shadowsocks/local.py:34(Pdb) bNum Type Disp Enb Where1 breakpoint keep yes at /home/test/python/shadowsocks/shadowsocks/local.py:34(Pdb) l 36 p = os.path.dirname(os.path.abspath(sys.executable)) 37 os.chdir(p) 38 39 config = shell.get_config(True) 40 41 daemon.daemon_exec(config) 42 43 try: 44 logging.info("starting local at %s:%d" % 45 (config['local_address'], config['local_port'])) 46 (Pdb) b 41Breakpoint 2 at /home/dexin/python/shadowsocks/shadowsocks/local.py:41(Pdb) b # 显示所有的断点Num Type Disp Enb Where1 breakpoint keep yes at /home/dexin/python/shadowsocks/shadowsocks/local.py:342 breakpoint keep yes at /home/dexin/python/shadowsocks/shadowsocks/local.py:41(Pdb) clear 1 # 清除第一个断点Deleted breakpoint 1(Pdb) bNum Type Disp Enb Where2 breakpoint keep yes at /home/dexin/python/shadowsocks/shadowsocks/local.py:41(Pdb)
五、p(print) 打印
这个指令的功能主要用于打印程序中的变量值
(Pdb) n> /home/test/python/shadowsocks/shadowsocks/shell.py(37)check_python()-> if info[0] == 2 and not info[1] >= 6:(Pdb) l 32 verbose = 0 33 34 35 def check_python(): 36 info = sys.version_info 37 -> if info[0] == 2 and not info[1] >= 6: 38 print('Python 2.6+ required') 39 sys.exit(1) 40 elif info[0] == 3 and not info[1] >= 3: 41 print('Python 3.3+ required') 42 sys.exit(1)(Pdb) p info # 打印变量值sys.version_info(major=2, minor=7, micro=6, releaselevel='final', serial=0)
六、动态调整变量的值
(Pdb) n> /home/test/python/shadowsocks/shadowsocks/shell.py(37)check_python()-> if info[0] == 2 and not info[1] >= 6:(Pdb) l 32 verbose = 0 33 34 35 def check_python(): 36 info = sys.version_info 37 -> if info[0] == 2 and not info[1] >= 6: 38 print('Python 2.6+ required') 39 sys.exit(1) 40 elif info[0] == 3 and not info[1] >= 3: 41 print('Python 3.3+ required') 42 sys.exit(1)(Pdb) p infosys.version_info(major=2, minor=7, micro=6, releaselevel='final', serial=0)(Pdb) info = (11,22,33)(Pdb) p info(11, 22, 33)
七、q(quit)退出
(Pdb) quit