1
I have following 2 functions defined in a library:

void print_root(topo *root){
    int i;

    for (i=0; i<10; i++) {
        print_topo_arr(i,root);
    }
}

int add(int x, int y)
{
    return (x+y);
}

I can call add() without any issues from Python script from gdb. However, I get when calling

Python Exception <class 'ctypes.ArgumentError'> argument 1: <type 'exceptions.TypeError'>: wrong type: 

    lib = cdll.LoadLibrary('./libshow.so')
    try1 = gdb.parse_and_eval ("i")
    print(type(try1)) # output is: <type 'gdb.Value'>
    print(try1.type.code) # output is: 8 TYPE_CODE_INT
    print('NEW Val of i={0}'.format(try1))
    lib.add.argtypes = [c_int, c_int]
    print lib.add(try1, 4) # works without issues

    #try to get root and call print_root()
    root_py = gdb.parse_and_eval ("root")
    print(type(root_py)) # output is: <type 'gdb.Value'>

    print(root_py.type.code) # output is: 14 TYPE_CODE_PTR
    lib.print_root.argtypes = [c_void_p] 
    print lib.print_root(root_py) # wrong type error here

How can I call print_root using gdb variable root?

Root is present in gdb:

(gdb) p root
$1 = (topo *) 0x7fffffffd620
Employed Russian
  • 199,314
  • 34
  • 295
  • 362

1 Answers1

0

What you are trying to do will not work: You are loading a shared object into the GDB process (via the Python ctypes module), and try to call a function in it using a pointer obtained from GDB. That pointer is only valid within the context of the inferior process controlled by GDB. GDB itself has a totally different address space, and the root pointer is meaningless in that context.

You either need to implement the printing in Python, using the GDB API to address all values, or load all data using the GDB API, create ctypes values from that, and pass the root of that new data structure to your shared object.

On the other hand, if the shared object is already loaded into the process being debugged, you should call the function directly, using GDB, and not use the ctypes module.

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92
  • I am using a core and thus cannot invoke the function directly. I am trying to avoid implementing printing in Python since I have a lot of print functions. Is there an way of re-using the C print functions? Like auto-generating gdb macros/Python based on existing C code ? – user10876582 Jan 08 '19 at 03:59