调试技术
- 调试流程:单元测试 -> 集成测试 -> 交测试部
- 分类
- 静态调试
- 动态调试
pdb 调试
- 官方文档,可选版本
pdb
: Python 调试库
常用命令 | 解释 |
---|---|
break / b | 设置断点 |
continue / c | 继续执行程序 |
list / l | 查看当前行的代码段 |
step / s | 进入函数 |
return / r | 执行代码直到从当前函数返回 |
exit / q | 中止并退出 |
next / n | 执行下一行 |
pp | 打印变量的值 |
help | 帮助 |
举例
import pdb
str1 = "aaa"
pdb.set_trace()
str2 = "bbb"
str3 = "ccc"
final = str1 + str2 + str3
print(final)
>>>
> d:pdb_test.py(6)<module>()
-> str2 = "bbb"
(Pdb) n
> d:pdb_test.py(7)<module>()
-> str3 = "ccc"
(Pdb) n
> d:pdb_test.py(8)<module>()
-> final = str1 + str2 + str3
(Pdb) n
> d:pdb_test.py(8)<module>()
-> print(final)
(Pdb) n
aaabbbccc
--Return--
> d:pdb_test.py(9)<module>()->None
-> print(final)
(Pdb)
补充
gdb
: 将 C 反汇编
#include <stdio.h>
int main() {
int a = 1, b = 2;
printf("a + b = %d
", a + b);
return 0;
}
- 将 test.c 编译为 test.exe 执行下方操作
PS D:code> gdb test
GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...done.
(gdb) disas main
Dump of assembler code for function main:
0x004015c0 <+0>: push %ebp
0x004015c1 <+1>: mov %esp,%ebp
0x004015c3 <+3>: and $0xfffffff0,%esp
0x004015c6 <+6>: sub $0x20,%esp
0x004015c9 <+9>: call 0x401690 <__main>
0x004015ce <+14>: movl $0x1,0x1c(%esp)
0x004015d6 <+22>: movl $0x2,0x18(%esp)
0x004015de <+30>: mov 0x1c(%esp),%edx
0x004015e2 <+34>: mov 0x18(%esp),%eax
0x004015e6 <+38>: add %edx,%eax
0x004015e8 <+40>: mov %eax,0x4(%esp)
0x004015ec <+44>: movl $0x404044,(%esp)
0x004015f3 <+51>: call 0x4025ac <printf>
0x004015f8 <+56>: mov $0x0,%eax
0x004015fd <+61>: leave
0x004015fe <+62>: ret
0x004015ff <+63>: nop
End of assembler dump.
(gdb) q
Pycharm 调试
- 软件有
run
,debug
两种模式 - 断点
- 在行号附近左键单击即可设置
- 程序在
debug
模式下,执行到断点所在行就会暂停
- 单步
- 程序停在断点处后,常用“单步”一步一步调试
- 除了单步
step over
,还有step into
,step into my code
等