Conditions under which stepping into shared library should work in gdb?

前端 未结 3 951
甜味超标
甜味超标 2021-01-01 22:49

There are many questions related to specific errors why stepping into a shared library with gdb isn\'t working. None of them provide a systematic answer on how to confirm wh

相关标签:
3条回答
  • 2021-01-01 23:24

    Some more tests you can do on built shared library:

    1. file libmyshared-ggdb.so should report that library has debug info and not stripped.
    2. nm libmyshared-ggdb.so | grep print_from_lib should find the symbol for print_from_lib function.

    If all above tests passed try to load the library directly in gdb and find the function:

    gdb libmyshared-ggdb.so
    (gdb) info functions print_from_lib
    

    Function print_from_lib name should be printed. If not, something is wrong with gdb or gcc.

    0 讨论(0)
  • 2021-01-01 23:29

    GDB 7.11 can't reproduce this problem. This is my steps. I hope this will help you:

    1.gcc -ggdb -c -Wall -Werror -fpic myshared.c -o myshared-ggdb.o
    2.gcc -ggdb -shared -o libmyshared-ggdb.so myshared-ggdb.o
    3.gcc -ggdb main.c -lmyshared-ggdb -o app-ggdb -L.
    4.gdb ./app-ggdb
    

    In GDB,

    (gdb) set env LD_LIBRARY_PATH=.
    (gdb) b main.c:7
    Breakpoint 1 at 0x4006a5: file main.c, line 7.
    (gdb) r
    Starting program: /home/haolee/tmp/app-ggdb 
    
    Breakpoint 1, main () at main.c:7
    7       print_from_lib();
    (gdb) s
    print_from_lib () at myshared.c:5
    5       printf("Printed from shared library\n");
    (gdb) 
    

    I step into the function print_from_lib successfully.

    0 讨论(0)
  • 2021-01-01 23:32

    Your problem is self-imposed: don't do this: set step-mode on, and step will work as you expect.

    From the GDB manual:

    set step-mode
    set step-mode on
    The set step-mode on command causes the step command to stop at the first
    instruction of a function which contains no debug line information
    rather than stepping over it.
    
    This is useful in cases where you may be interested in inspecting the machine
    instructions of a function which has no symbolic info and do not want
    GDB to automatically skip over this function.
    

    You are interested in the opposite of the above -- you want to step into the print_from_lib function and avoid stopping inside the PLT jump stub and the dynamic loader's symbol resolution function.

    0 讨论(0)
提交回复
热议问题