test.c :
#include <stdlib.h>
#include <stdio.h>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
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)
linxp@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 <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 "i686-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
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 <printf@plt>
0xb76e65e9 <+46>: mov -0xc(%ebx),%eax
0xb76e65ef <+52>: mov (%eax),%eax
0xb76e65f1 <+54>: mov %eax,(%esp)
0xb76e65f4 <+57>: call 0xb76e6450 <fflush@plt>
0xb76e65f9 <+62>: call 0xb76e6480 <abort@plt>
End of assembler dump.
from objdump -D test.so:
000005bb <foo>:
5bb: 55 push %ebp
5bc: 89 e5 mov %esp,%ebp
5be: 53 push %ebx
5bf: 83 ec 14 sub $0x14,%esp
5c2: e8 c9 fe ff ff call 490 <__x86.get_pc_thunk.bx>
5c7: 81 c3 39 1a 00 00 add $0x1a39,%ebx
5cd: 8b 45 0c mov 0xc(%ebp),%eax
5d0: 89 44 24 08 mov %eax,0x8(%esp)
5d4: 8b 45 08 mov 0x8(%ebp),%eax
5d7: 89 44 24 04 mov %eax,0x4(%esp)
5db: 8d 83 14 e6 ff ff lea -0x19ec(%ebx),%eax
5e1: 89 04 24 mov %eax,(%esp)
5e4: e8 57 fe ff ff call 440 <printf@plt>
5e9: 8b 83 f4 ff ff ff mov -0xc(%ebx),%eax
5ef: 8b 00 mov (%eax),%eax
5f1: 89 04 24 mov %eax,(%esp)
5f4: e8 57 fe ff ff call 450 <fflush@plt>
5f9: e8 82 fe ff ff call 480 <abort@plt>