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的首字母

 

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