调试和测试之调试

调试和测试之 debug(调试)

1.debug

1.1 对 handler 进行调试

日常我们主要是编写 handler,如果想要看 handler 运行是否符合预期,可以通过 test_handler.py 进行调试

1.1.1 输出 usage

python test_handler.py

Usage:
test_handler.py /x/hello 'str_info'
test_handler.py /auth/ssologin  'ticket='
test_handler.py /report/log
test_handler.py /auth/verification
test_handler.py /main
test_handler.py /tpl
test_handler.py /apidemo/ping
test_handler.py /apidemo/hello 'str_info'
test_handler.py /x/ping

执行 test_handler.py 后会输出当前所有的 handler 及请求参数

1.1.2 debug 某个 handler

实际执行的时候,是彩色输出的

python test_handler.py /apidemo/hello meetbill

1.2 框架相关

1.2.1 查看执行目录

1.2.2 系统调用

通过 strace 可以看访问 ip 等信息

另外当 Linux 命令卡住时,可以通过 strace command 获取系统调用信息

1.2.3 Linux 下多线程查看工具 (pstree、ps、pstack)

  1. pstree 以树结构显示进程

7916 进程共启动了 16 个子线程,加上主线程是 17 个线程

  1. ps -Lf

$ ps -Lf 7916

进程共启动了 17 个线程

  1. pstack 显示每个进程的栈跟踪

1.2.4 查看 Butterfly stack trace

此功能在 handler 中进行实现,代码见 /handler/demo_stackdump

使用

例子 (butterfly/logs/stack.log 内容)

2 gdb

2.1 使用方法

2.1.1 调试进程

查看 C 调用栈

查看 Python 调用栈

2.2 官方文档

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

2.3 查看 core 保存路径

  • ulimit -c 是查看创建的核心转储的最大大小

    • 0: 不保存

    • unlimited: 不限制大小

  • cat /proc/sys/kernel/core_pattern 生成的文件路径

2.4 查看 core 文件例子

例子

可以看到程序是在停止的时候,在如下部分发生的问题

3 lsof(lists openfiles)

在 Unix 中一切(包括网络套接口)都是文件。lsof 的功能是列出打开文件(lists openfiles)

3.1 使用 -p 查看指定进程 ID 已打开的文件

lsof -p {PID}

3.2 使用 @host:port 显示基于主机与端口的连接

4 mem_top 排查内存泄露问题

将会将将日志打印在 logs/common/common.log

  • refs 回显信息:占用内存最大的 topN 变量中的元素个数

  • bytes 回显信息:占用内存最大的 topN 变量每个变量所占用的 bytes

  • types 回显信息:占用内存最大的 topN 类型变量个数

4.1 例子

5 传送门

5.1 根据 python 代码自动生成流程图

  • code2flow – 把你的 Python 和 JavaScript 代码转换为流程图。

  • pycallgraph - 这个库可以把你的 Python 应用的流程(调用图)进行可视化。

5.2 GDB 教程

5.3 mem_top

Last updated