Windows环境使用 gdb 调试python c混合代码

1. 前言

Python作为一门脚本语言,让程序员的开发效率变得非常高。但相对来说,程序执行的效率就比不上C、C++等编译型语言了。对此,可以做一个“妥协”,即把效率影响较大的函数用C来实现,Python代码中调用这些函数,实现开发效率和运行效率的平衡。

但是python调用C不便于对C进行调试,因此可以借鉴linux系统gdb工具对python中C子代码进行调试,以满足简单的代码调试;

工具:python,gdb(一般安装Dev C++会自动安装gdb.exe,在Dev C++的安装路径下:..\Dev-Cpp\MinGW64\bin,只需将该路径添加到环境变量中)

(本人为入门级超级菜鸟,因此本文仅作为gdb调试python中C子代码的入门级介绍,后续学习中...)

2. 实例

话不多说,看实例操作。

Python代码gdb.py如下(下述代码实现Python代码调用C函数,并通过结构体进行参数传递。)

import numpy
from ctypes import *

hh = numpy.ctypeslib.load_library('./C/struc.dll','dll')
class person_Info(Structure):
    _fields_ = [('x',c_char_p),             # 传递字符串
                ('y',c_int),
                ('z',c_float)]
person_liu = person_Info(bytes('liu',encoding='utf-8'), 25, 65.0)
person_h = person_Info()
person_h.x = bytes('ut',encoding='utf-8')  # 传递字符串(str转bytes)
person_h.y = c_int(26)                     # bytes 转str:str(b, encoding = 'utf8')
person_h.z = c_float(57.8)
hh.fun.restype = person_Info
r = hh.fun(person_h)
t = hh.fun(person_liu)

C子函数struc.c如下

/* Replace "struc.h"with the name of your header */
#include "struc.h" 
// .h文件名
#include <windows.h>
#include <stdio.h>
DLLIMPORT struct person_Info fun(struct person_Info uk)
{    
printf("%s\n%d\n%f\n",uk.a, uk.b, uk.c);   
 uk.c = uk.c + 1;    
 uk.a = "hu";    
 printf("%s\n%d\n%f\n",uk.a, uk.b, uk.c);    
 printf("===========================\n");
    return uk;
}

 

①在python主文件gdb.py的路径下打开命令窗口,输入  gdb python,该处作用为启动一个python程序

PS G:\Code_materials\testCodes\20191113_gdb> gdb python
GNU gdb (GDB) 7.8.1
Copyright (C) 2014 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 python...(no debugging symbols found)...done.
(gdb)

②输入 b fun,在函数fun处加入断点,yes确认,会提示加入成功,

(gdb) b fun
No symbol table is loaded.  Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (fun) pending.
(gdb)

③输入 run gdb.py,运行Python主代码,可以看到代码在断点处停下,可以输入 list 查看C代码,fun断点处也就是8行处,输入n,实现单步调试运行,

(gdb) run gdb.py
Starting program: D:\Program Files (x86)\Anaconda3\python.exe gdb.py
[New Thread 15960.0x24f4]
[New Thread 15960.0x3d70]
[New Thread 15960.0x475c]
[New Thread 15960.0x3fd8]
warning: Temporarily disabling breakpoints for unloaded shared library "G:\Code_materials\testCodes\20191113_gdb\C\struc.dll"

Breakpoint 1, fun (uk=...) at struc.c:8
8       printf("%s\n%d\n%f\n",uk.a, uk.b, uk.c);
(gdb) n
[New Thread 15960.0x4cd0]
ut
26
57.799999
9        uk.c = uk.c + 1;
(gdb) n
10       uk.a = "hu";
(gdb) n
11       printf("%s\n%d\n%f\n",uk.a, uk.b, uk.c);
(gdb) n
hu
26
58.799999
12       printf("===========================\n");
(gdb)

 

 

基本调试指令

设置断点: (gdb) b 8 #断点设置该文件的第8行(b即break的首字母) 
显示所有断点:(gdb) b #b命令,没有参数,显示所有断点 
删除断点:(gdb) cl 2 #删除第2个断点 (clear的首字母)
Step Over:(gdb) n #单步执行,next的首字母 
Step Into:(gdb) s #step的首字母 
Setp Return:(gdb) r #return的首字母 
Resume:(gdb) c #continue的首字母 
Run to Line:(gdb) j 10 #运行到地10行,jump的首字母
(gdb) p param #查看当前param变量值 
(gdb) l #查看运行到某处代码 
(gdb) a #查看全部栈内变量
(gdb) h #帮助,help的首字母 
(gdb) q #退出,quit的首字母

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章