2017-02-15 82 views
0

PyString_InternFromString是具有以下聲明的c函數。我可以在gdb中用c字符串調用主機進程函數嗎?

PyObject *PyString_InternFromString(const char *cp) 

我在gdb中用下面的命令和輸出調用了這個函數。

(gdb) p (char *) malloc(10) 
$8 = 0xcfd020 "\210\066▒\364\177" 
(gdb) call strcpy(0xcfd020, "nihao") 
$9 = 13619232 
(gdb) p PyString_InternFromString 
$10 = {PyObject *(const char *)} 0x419158 <PyString_InternFromString> 
(gdb) break PyObject_Malloc 
Breakpoint 1 at 0x418004: PyObject_Malloc. (17 locations) 
(gdb) p 0xcfd020 
$11 = 13619232 
(gdb) p (const char*)0xcfd020 
$12 = 0xcfd020 "nihao" 
(gdb) p ((PyObject * (*)(const char *))0x419158)((const char *)0xcfd020) 
Breakpoint 1, PyString_InternFromString (cp=0x64 <error: Cannot access memory at address 0x64>) at ../Objects/stringobject.c:4783 
4783 ../Objects/stringobject.c: No such file or directory. 
The program being debugged stopped while in a function called from GDB. 
Evaluation of the expression containing the function 
(PyString_InternFromString) will be abandoned. 
When the function is done executing, GDB will silently stop. 
(gdb) bt 
#0 PyString_InternFromString (cp=0x64 <error: Cannot access memory at address 0x64>) at ../Objects/stringobject.c:4783 
#1 0x00007ff489a1a0c0 in ??() 
#2 0x7b752ef9cf7f0a00 in ??() 
#3 0x00007ff489b1b050 in ??() 
#4 0x0000000000000065 in ??() 
#5 0x0000000000000000 in ??() 
(gdb) n 

Program received signal SIGSEGV, Segmentation fault. 
PyObject_Malloc (nbytes=<optimized out>) at ../Objects/obmalloc.c:882 
882  ../Objects/obmalloc.c: No such file or directory. 
(gdb) bt 
#0 PyObject_Malloc (nbytes=<optimized out>) at ../Objects/obmalloc.c:882 
#1 PyString_FromString (str=0x64 <error: Cannot access memory at address  0x64>) at ../Objects/stringobject.c:143 
#2 PyString_InternFromString (cp=0x64 <error: Cannot access memory at address 0x64>) at ../Objects/stringobject.c:4783 
#3 0x00007ff489a1a0c0 in ??() 
#4 0x7b752ef9cf7f0a00 in ??() 
#5 0x00007ff489b1b050 in ??() 
#6 0x0000000000000065 in ??() 
#7 0x0000000000000000 in ??() 
(gdb) info locals 
bp = <optimized out> 
pool = 0x7ff489a0a370 
next = <optimized out> 
size = 9607536 
(gdb) info args 
nbytes = <optimized out> 
(gdb) f 1 
#1 PyString_FromString (str=0x64 <error: Cannot access memory at address 0x64>) at ../Objects/stringobject.c:143 
143  ../Objects/stringobject.c: No such file or directory. 
(gdb) info locals 
size = 100 
op = <optimized out> 
(gdb) info args 
str = 0x64 <error: Cannot access memory at address 0x64> 
(gdb) p ((PyObject * (*)(const char *))0x419158)(&((const char *)0xcfd020)) 
Attempt to take address of value not located in memory. 
(gdb) call strlen("nihaobuhoa") 
$13 = 10 

我沒有打電話到功能與Segmentation fault。我們可以知道Cannot access memory at address 0x64從輸出引起故障。這真令我困惑,我給PyString_InternFromString的是const char *字符串,地址爲0xcfd020,但在函數中改爲0x64。 任何人都知道爲什麼會發生這種情況?

+0

我認爲你從gdb看到的很多遺漏和令人困惑的信息是因爲你正在運行python解釋器的優化版本。如果你可以得到一個python的「調試版本」(在Ubuntu上,這是「python2.7-dbg」包),你會發現你不會得到'優化出來的'消息。 –

+0

我試過python調試版本,但沒有發生同樣的錯誤。 @MarkPlotnick –

回答

0

這是因爲GDB讀符號表PyString_InternFromString的錯誤的地址。我將python符號表轉換爲純文本文件,發現PyString_InternFromString的實際地址與gdb打印的地址不同。

0

也許你調用你的函數的方式隱藏了太多GDB的東西。

爲什麼不直接調用它直接調用它而不是調用鑄造指針功能PyString_InternFromString()?您也可以在調用函數時使用C字符串,GDB會自動調用malloc()來創建字符串。

(gdb) # reach a point where every initializations of your call (and 
subcalls) are done. For example, main. 
(gdb) b main 
(gdb) run 
(gdb) p /x PyString_InternFromString("nihao") 

注:

  1. 此調用隱含取決於它需要的一切,即直接或間接用於全局對象的初始化之前。如果是這種情況,則應在初始化之後首先中斷並執行此調用。

  2. 從GDB調用函數至少要求首次初始化C運行時,原因與第1點中所述的相同。因此,當您到達main()函數時應執行呼叫。

  3. 可以調試通過GDB調用的函數(如錯誤消息../Objects/stringobject.c: No such file or directory.所述)。下載並specify the directory to GDB,則應能夠調試它(調試信息可以是單獨的,如果這是你從OS分發庫)

相關問題