通过gdb调试工具,如何进入第三方提供的动态链接库中的对外接口函数,这个很重要啊。能做到吗?

lgmsyy 2014-04-11 11:06:47
我自己编写的函数通过 GDB 的s命令我可以跟踪进入函数体myint,


int main(int argc, char* argv[]){

int a = myint();

return a;
}

int myint(){

int a=0;
a=a+1;
return a;
}


但如果myint函数是第三方提供的动态链接的接口函数,那么我如何进入到这个函数体中呢(对方没提供源代码)。

int main(int argc, char* argv[]){

//加载 第三方动态链接库
dlopen("./libTest.so", RTLD_LAZY); //汇编跟踪(s 或者 si )在此就跟踪不下去了。
int a = myint();//第三方 动态链接库提供的函数

return a;
}

也就是当加载第三方动态链接库的时候提示:
si 0x400a72 <main+78> callq 0x4008f0 <dlopen@plt>
si 0x00000000004008f0 in dlopen@plt ()
[ No Source Available ]

...全文
477 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-04-11
  • 打赏
  • 举报
回复
DLOPEN Section: Linux Programmer's Manual (3) Updated: 2003-11-17 -------------------------------------------------------------------------------- NAME dladdr, dlclose, dlerror, dlopen, dlsym, dlvsym - programming interface to dynamic linking loader SYNOPSIS #include <dlfcn.h> void *dlopen(const char *filename, int flag); char *dlerror(void); void *dlsym(void *handle, const char *symbol); int dlclose(void *handle); DESCRIPTION The four functions dlopen(), dlsym(), dlclose(), dlerror() implement the interface to the dynamic linking loader. dlerror The function dlerror() returns a human readable string describing the most recent error that occurred from any of the dl routines (dlopen, dlsym or dlclose) since the last call to dlerror(). It returns NULL if no errors have occurred since initialization or since it was last called. dlopen The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for the dynamic library. If filename is NULL, then the returned handle is for the main program. If filename contains a slash ("/"), then it is interpreted as a (relative or absolute) pathname. Otherwise, the dynamic linker searches for the library as follows (see ld.so(8) for further details): o (ELF only) If the executable file for the calling program contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag, then the directories listed in the DT_RPATH tag are searched. o If the environment variable LD_LIBRARY_PATH is defined to contain a colon-separated list of directories, then these are searched. (As a security measure this variable is ignored for set-UID and set-GID programs.) o (ELF only) If the executable file for the calling program contains a DT_RUNPATH tag, then the directories listed in that tag are searched. o The cache file /etc/ld.so.cache (maintained by ldconfig(8)) is checked to see whether it contains an entry for filename. o The directories /lib and /usr/lib are searched (in that order). If the library has dependencies on other shared libraries, then these are also automatically loaded by the dynamic linker using the same rules. (This process may occur recursively, if those libraries in turn have dependencies, and so on.) The value of flag can be either RTLD_LAZY or RTLD_NOW. When RTLD_NOW is specified, or the environment variable LD_BIND_NOW is set to a non-empty string, all undefined symbols in the library are resolved before dlopen() returns. If this cannot be done, an error is returned. Otherwise binding is lazy: symbol values are first resolved when needed. Optionally, RTLD_GLOBAL may be or'ed into flag, in which case the external symbols defined in the library will be made available for symbol resolution of subsequently loaded libraries. (The converse of RTLD_GLOBAL is RTLD_LOCAL. This is the default.) If filename is a NULL pointer, then the returned handle is for the main program. When given to dlsym(), this handle causes a search for a symbol in the main program, followed by all shared libraries loaded at program startup, and then all shared libraries loaded by dlopen() with the flag RTLD_GLOBAL. External references in the library are resolved using the libraries in that library's dependency list and any other libraries previously opened with the RTLD_GLOBAL flag. If the executable was linked with the flag "-rdynamic" (or, synonymously, "--export-dynamic"), then the global symbols in the executable will also be used to resolve references in a dynamically loaded library. If the same library is loaded again with dlopen(), the same file handle is returned. The dl library maintains reference counts for library handles, so a dynamic library is not deallocated until dlclose() has been called on it as many times as dlopen() has succeeded on it. The _init routine, if present, is only called once. But a subsequent call with RTLD_NOW may force symbol resolution for a library earlier loaded with RTLD_LAZY. If dlopen() fails for any reason, it returns NULL. dlsym The function dlsym() takes a "handle" of a dynamic library returned by dlopen and the NUL-terminated symbol name, returning the address where that symbol is loaded into memory. If the symbol is not found, in the specified library or any of the libraries that were automatically loaded by dlopen() when that library was loaded, dlsym() returns NULL. (The search performed by dlsym() is breadth first through the dependency tree of these libraries.) Since the value of the symbol could actually be NULL (so that a NULL return from dlsym() need not indicate an error), the correct way to test for an error is to call dlerror() to clear any old error conditions, then call dlsym(), and then call dlerror() again, saving its return value into a variable, and check whether this saved value is not NULL. There are two special pseudo-handles, RTLD_DEFAULT and RTLD_NEXT. The former will find the first occurrence of the desired symbol using the default library search order. The latter will find the next occurrence of a function in the search order after the current library. This allows one to provide a wrapper around a function in another shared library. dlclose The function dlclose() decrements the reference count on the dynamic library handle handle. If the reference count drops to zero and no other loaded libraries use symbols in it, then the dynamic library is unloaded. The function dlclose() returns 0 on success, and non-zero on error.
赵4老师 2014-04-11
  • 打赏
  • 举报
回复
仅供参考:
EXAMPLE
Load the math library, and print the cosine of 2.0: 
#include <stdio.h>
#include <dlfcn.h>

int main(int argc, char **argv) {
    void *handle;
    double (*cosine)(double);
    char *error;

    handle = dlopen ("libm.so", RTLD_LAZY);
    if (!handle) {
        fprintf (stderr, "%s\n", dlerror());
        exit(1);
    }

    dlerror();    /* Clear any existing error */
    *(void **) (&cosine) = dlsym(handle, "cos");
    if ((error = dlerror()) != NULL)  {
        fprintf (stderr, "%s\n", error);
        exit(1);
    }

    printf ("%f\n", (*cosine)(2.0));
    dlclose(handle);
    return 0;
}

If this program were in a file named "foo.c", you would build the program with the following command: 

gcc -rdynamic -o foo foo.c -ldl 

Libraries exporting _init() and _fini() will want to be compiled as follows, using bar.c as the example name: 

gcc -shared -nostartfiles -o bar bar.c 

lgmsyy 2014-04-11
  • 打赏
  • 举报
回复
你好,我的目的的确是为了是跟踪myint,但是我现在根本调试不到这个函数,也就是说通过GDB si调试的时候,进不去myint函数。而且 加载SO这行根本也过去不。即便我 b myint,然后再进行SI,问题依旧的。
引用 6 楼 zhao4zhong1 的回复:
你没必要跟踪这行(./libTest.so如何一步步加载)。 dlopen("./libTest.so", RTLD_LAZY); //汇编跟踪(s 或者 si )在此就跟踪不下去了。 你应该跟踪这行(myint函数到底干了些啥) int a = myint();//第三方 动态链接库提供的函数
赵4老师 2014-04-11
  • 打赏
  • 举报
回复
你没必要跟踪这行(./libTest.so如何一步步加载)。 dlopen("./libTest.so", RTLD_LAZY); //汇编跟踪(s 或者 si )在此就跟踪不下去了。 你应该跟踪这行(myint函数到底干了些啥) int a = myint();//第三方 动态链接库提供的函数
lgmsyy 2014-04-11
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
VS IDE指Windows下的VS20XX 我相信你这个问题到看雪论坛随便那么一问,必有答案。
我的环境是LINUX的,并且我的动态链接库不是DLL,而是SO的,只能在LINUX下运行
赵4老师 2014-04-11
  • 打赏
  • 举报
回复
VS IDE指Windows下的VS20XX 我相信你这个问题到看雪论坛随便那么一问,必有答案。
lgmsyy 2014-04-11
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
在asm窗口中step不行吗? 换VS IDE?
当开始调试到加载动态链接库的时候,就提示以下了,而且一直继续下去 0x00000000004008fb in dlopen@plt () (gdb) si 0x0000000000400870 in ?? () (gdb) si 0x0000000000400876 in ?? () (gdb) si 0x00007ffff7def200 in ?? () from /lib64/ld-linux-x86-64.so.2 (gdb) si 0x00007ffff7def204 in ?? () from /lib64/ld-linux-x86-64.so.2 ......一直继续下去
lgmsyy 2014-04-11
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
在asm窗口中step不行吗? 换VS IDE?
asm 不行 , VS IDE? 没用过。Eclipse吗?
赵4老师 2014-04-11
  • 打赏
  • 举报
回复
在asm窗口中step不行吗? 换VS IDE?

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧