html tool

2016年10月9日星期日

gdb core 调试 - python调用c库

[popexizhi:
发现gdb 可以调试python 程序,不错可以测试一下,下面是使用so生成core和调试方式
]

源地址:http://blog.csdn.net/linxuping/article/details/33305289

test.c :
#include   
#include 

  
int foo(int a, int b)  
{  
  printf("Your input %i and %i\n", a, b);  
  fflush(stdout);
  abort();
  return a + b;  
} 

---------------------------------------------
编译 gcc -g -o test.so -shared -fPIC test.c 
[popexizhi: 
-fPIC 作用于编译阶段,告诉编译器产生与位置无关代码(Position-Independent Code),
  则产生的代码中,没有绝对地址,全部使用相对地址,故而代码可以被加载器加载到内存的任意
  位置,都可以正确的执行。这正是共享库所要求的,共享库被加载时,在内存的位置不是固定的。

gcc -shared -fPIC -o 1.so 1.c
这里有一个-fPIC参数
PIC就是position independent code
PIC使.so文件的代码段变为真正意义上的共享
如果不加-fPIC,则加载.so文件的代码段时,代码段引用的数据对象需要重定位, 重定位会修改代码段的内容,这就造成每个使用这个.so文件代码段的进程在内核里都会生成这个.so文件代码段的copy.每个copy都不一样,取决于 这个.so文件代码段和数据段内存映射的位置
(参考:http://blog.sina.com.cn/s/blog_54f82cc201011op1.html)
]
-----------------------------------------------
test.py :
import ctypes
ll = ctypes.cdll.LoadLibrary
lib = ll("./test.so")
lib.foo(1,3)

------------------------------------------------
运行
linxp@ubuntu:~/test$ python test.py 
Your input 1 and 3
Aborted (core dumped)
-----------------------------------------------------
gdb
inxp@ubuntu:~/test$ gdb python core GNU gdb (GDB) 7.6.1-ubuntu Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later 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 "i686-linux-gnu". For bug reporting instructions, please see: ... Reading symbols from /usr/bin/python2.7...(no debugging symbols found)...done. warning: core file may not match specified executable file. [New LWP 11988] warning: Can't read pathname for load map: Input/output error. [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1". Core was generated by `python test2.py'. Program terminated with signal 6, Aborted. #0  0xb76f4424 in __kernel_vsyscall () (gdb) bt #0  0xb76f4424 in __kernel_vsyscall () #1  0xb753daff in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56 #2  0xb7541083 in __GI_abort () at abort.c:90 #3  0xb76e65fe in foo (a=1, b=3) at test2.c:8 #4  0xb76ee3c6 in ffi_call_SYSV () from /usr/lib/i386-linux-gnu/libffi.so.6 #5  0xb76ee14b in ffi_call () from /usr/lib/i386-linux-gnu/libffi.so.6 #6  0xb71d5827 in _ctypes_callproc () from /usr/lib/python2.7/lib-dynload/_ctypes.i386-linux-gnu.so #7  0xb71d71a3 in ?? () from /usr/lib/python2.7/lib-dynload/_ctypes.i386-linux-gnu.so #8  0x080c1e45 in PyEval_EvalFrameEx () #9  0x080c8702 in PyEval_EvalCodeEx () #10 0x081aed77 in PyEval_EvalCode () #11 0x080a9be7 in ?? () #12 0x080aa4fd in PyRun_FileExFlags () #13 0x080aadc0 in PyRun_SimpleFileExFlags () #14 0x080abb1d in Py_Main () #15 0x080abbfc in main ()
(gdb) disassemble foo Dump of assembler code for function foo:    0xb76e65bb <+0>: push   %ebp    0xb76e65bc <+1>: mov    %esp,%ebp    0xb76e65be <+3>: push   %ebx    0xb76e65bf <+4>: sub    $0x14,%esp    0xb76e65c2 <+7>: call   0xb76e6490 <__x86 .get_pc_thunk.bx="">    0xb76e65c7 <+12>: add    $0x1a39,%ebx    0xb76e65cd <+18>: mov    0xc(%ebp),%eax    0xb76e65d0 <+21>: mov    %eax,0x8(%esp)    0xb76e65d4 <+25>: mov    0x8(%ebp),%eax    0xb76e65d7 <+28>: mov    %eax,0x4(%esp)    0xb76e65db <+32>: lea    -0x19ec(%ebx),%eax    0xb76e65e1 <+38>: mov    %eax,(%esp)    0xb76e65e4 <+41>: call   0xb76e6440    0xb76e65e9 <+46>: mov    -0xc(%ebx),%eax    0xb76e65ef <+52>: mov    (%eax),%eax    0xb76e65f1 <+54>: mov    %eax,(%esp)    0xb76e65f4 <+57>: call   0xb76e6450    0xb76e65f9 <+62>: call   0xb76e6480 End of assembler dump.

没有评论:

发表评论