Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 我可以用gdb中的c字符串调用主机进程函数吗?_Python 2.7_Memory_Gdb - Fatal编程技术网

Python 2.7 我可以用gdb中的c字符串调用主机进程函数吗?

Python 2.7 我可以用gdb中的c字符串调用主机进程函数吗?,python-2.7,memory,gdb,Python 2.7,Memory,Gdb,PyString\u InternFromString是一个具有以下声明的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_Inte

PyString\u InternFromString
是一个具有以下声明的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
(gdb)p(char*)malloc(10)
$8=0xcfd020“\210\066▒\364\177"
(gdb)调用strcpy(0xcfd020,“nihao”)
$9 = 13619232
(gdb)p PyString_InternFromString
$10={PyObject*(const char*)}0x419158
(gdb)断开PyObject_Malloc
断点1位于0x418004:PyObject\u Malloc。(17个地点)
(gdb)p 0xcfd020
$11 = 13619232
(gdb)p(常量字符*)0xcfd020
$12=0xcfd020“nihao”
(gdb)p((PyObject*(*)(const char*))0x419158)((const char*)0xcfd020)
断点1,PyString_InternFromString(cp=0x64)位于../Objects/stringobject.c:4783
4783../Objects/stringobject.c:没有这样的文件或目录。
正在调试的程序在从GDB调用的函数中停止。
对包含函数的表达式的求值
(PyString_InternFromString)将被放弃。
当函数完成执行时,GDB将静默停止。
(gdb)英国电信
#0位于../Objects/stringobject.c:4783的PyString_InternFromString(cp=0x64)
#1 0x00007ff489a1a0c0英寸??()
#2 0x7b752ef9cf7f0a00英寸??()
#3 0x00007ff489b1b050英寸??()
#4 0x0000000000000065英寸??()
#5 0x0000000000000000英寸??()
(gdb)n
程序接收信号SIGSEGV,分段故障。
PyObject_Malloc(nbytes=)位于../Objects/obmalloc.c:882
882../Objects/obmalloc.c:没有这样的文件或目录。
(gdb)英国电信
#0位于../Objects/obmalloc.c:882的PyObject_Malloc(n字节=)
#1 PyString_FromString(str=0x64)位于../Objects/stringobject.c:143
#2位于../Objects/stringobject.c:4783的PyString_InternFromString(cp=0x64)
#3 0x00007ff489a1a0c0英寸??()
#4 0x7b752ef9cf7f0a00英寸??()
#5 0x00007ff489b1b050英寸??()
#6 0x0000000000000065英寸??()
#7 0x0000000000000000英寸??()
(gdb)本地信息
bp=
池=0x7ff489a0a370
下一步=
尺寸=9607536
(gdb)信息参数
n字节=
(gdb)f 1
#1 PyString_FromString(str=0x64)位于../Objects/stringobject.c:143
143../Objects/stringobject.c:没有这样的文件或目录。
(gdb)本地信息
尺寸=100
op=
(gdb)信息参数
str=0x64
(gdb)p((PyObject*(*)(const char*))0x419158)(&((const char*)0xcfd020))
尝试获取不在内存中的值的地址。
(gdb)致电斯特伦(“nihaobuhoa”)
$13 = 10
我无法调用带有分段错误的函数。我们可以了解到,
无法访问地址0x64处的内存导致输出故障。这让我非常困惑,我给
PyString\u InternFromString
的是地址
0xcfd020
处的
const char*
字符串,但在函数中改为
0x64

有人知道为什么会这样吗

也许您调用函数的方式对GDB隐藏了太多东西

与其调用函数
PyString\u 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")
注:

  • 此调用隐式地依赖于它所需的所有内容的预先初始化,即直接或间接使用的全局对象。如果是这种情况,您应该首先中断并在初始化之后执行此调用

  • 从GDB调用函数至少需要首先初始化C运行时,原因与第1点中所述的相同。因此,您应该在到达
    main()
    函数时执行调用

  • 可以调试通过GDB调用的函数(如错误消息
    。/Objects/stringobject.c所述:没有这样的文件或目录。
    )。下载它,然后您应该能够调试它(如果这是一个来自您的OS发行版的库,那么调试信息可能是独立的)


  • 这是因为gdb从符号表中读取了PyString_InternFromString的错误地址。我将python符号表转储到一个纯文本文件中,发现PyString_InternFromString的实际地址与从gdb打印的地址不同。

    我认为您从gdb中看到的许多缺失和混乱的信息是因为您正在运行一个优化版本的python解释器。如果你能得到python的“调试版本”(在Ubuntu上,这是包
    python2.7-dbg
    ),你会发现你不会得到
    优化的消息@马克普洛尼克