跳至内容
Linux命令

Linux命令

python调试

参考Linux 下如何调试 Python? - hoxis - 博客园

  • 在命令行,将程序作为参数传递来调试,“… -m pdb …py”,—— 命令行传pdb方式

    python3 -m pdb test1.py

1
2
3
4
root@autodl-container-c00844b981-267cf857:~/autodl-tmp/test_script# python3 -m pdb test1.py
> /root/autodl-tmp/test_script/test1.py(3)<module>()
-> def add(a, b):
(Pdb) _<-光标在此处
  • 在代码中import pdb,写pdb.set_trace()代码 —— import pdb 方式

    import pdb

    pdb.set_trace()

    python3 test1.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# -*- coding: utf-8 -*-
import pdb
def add(a, b):
    return a + b

if __name__ == '__main__':
    print("===start===")
    pdb.set_trace()
    c = add(1, 3)
    print("===end===")
    
======= 命令行情况如下 ========
root@autodl-container-c00844b981-267cf857:~/autodl-tmp/test_script# python3 test1.py
===start===
> /root/autodl-tmp/test_script/test1.py(9)<module>()
-> c = add(1, 3)
(Pdb) 
  • 进入调试后常用指令

    (Pdb) l (“小写L指令”:查看)(若连续输入两次,则第二次返回[EOF])

    (Pdb) p 变量名 (查看当前可查变量)

    (Pdb)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
===========	命令行传pdb

root@autodl-container-c00844b981-267cf857:~/autodl-tmp/test_script# python3 -m pdb test1.py
> /root/autodl-tmp/test_script/test1.py(3)<module>()
-> def add(a, b):
(Pdb) 1
  1    # -*- coding: utf-8 -*- 
  2
  3 -> def add(a, b):
  4    return a + b
  5
  6    if __name == '__main__':
  7    print("==start==")
  8    c = add(1, 3)
  9    print("==end==")
[EOF]
(Pdb) b 8

Breakpoint 1 at /root/autodl-tmp/test_script/test1.py:8
(Pdb) b 9

Breakpoint 2 at /root/autodl-tmp/test_script/test1.py:9
(Pdb) b

Num Type    Disp Enb    Where
  1    breakpoint    keep yes    at /root/autodl-tmp/test_script/test1.py:8
  2    breakpoint    keep yes    at /root/autodl-tmp/test_script/test1.py:9
(Pdb) c
===start===
> /root/autodl-tmp/test_script/test1.py(8)<module>()
-> c = add(1, 3)
(Pdb) c
> /root/autodl-tmp/test_script/test1.py(9)<module>()
-> print("===end==")
(Pdb) p c
  4
(Pdb) p a
*** NameError: name 'a' is not defined
(Pdb) p b
*** NameError: name 'b' is not defined
(Pdb) s
===end===
--Return-
> /root/autodl-tmp/test_script/test1.py(9)<module>()->None
-> print("===end==")
(Pdb) s
--Return-
> <string>(1)<module>()->None
(Pdb) s
> /root/miniconda3/lib/python3.12/bdb.py(604)run()
-> self.guitting = True
(Pdb) s

The program finished and will be restarted

C编译调试

  • gcc -g test.c -o test 将 test.c 编译为可调式、可执行文件 test(.exe)

Vim乱记

x删除光标所在处的字符

u撤销一次操作(除u以外)

yy复制光标所在行

p新增一行并写入剪贴板内容

最后更新于