我正在嘗試使用dladdr。它正確定位庫,但它找不到函數名稱。我可以調用objdump,做一些小算術,並獲取我傳遞dladdr函數的地址。如果objdump可以看到它,爲什麼不能dladdr?dladdr不返回函數名稱
這裏是我的功能:
const char *FuncName(const void *pFunc)
{
Dl_info DlInfo;
int nRet;
// Lookup the name of the function given the function pointer
if ((nRet = dladdr(pFunc, &DlInfo)) != 0)
return DlInfo.dli_sname;
return NULL;
}
這裏是在我得到一個GDB成績單。
Program received signal SIGINT, Interrupt.
[Switching to Thread 0xf7f4c6c0 (LWP 28365)]
0xffffe410 in __kernel_vsyscall()
(gdb) p MatchRec8Cmp
$2 = {void (TCmp *, TWork *, TThread *)} 0xf1b62e73 <MatchRec8Cmp>
(gdb) call FuncName(MatchRec8Cmp)
$3 = 0x0
(gdb) call FuncName(0xf1b62e73)
$4 = 0x0
(gdb) b FuncName
Breakpoint 1 at 0xf44bdddb: file threads.c, line 3420.
(gdb) call FuncName(MatchRec8Cmp)
Breakpoint 1, FuncName (pFunc=0xf1b62e73) at threads.c:3420
3420 {
The program being debugged stopped while in a function called from GDB.
When the function (FuncName) is done executing, GDB will silently
stop (instead of continuing to evaluate the expression containing
the function call).
(gdb) s
3426 if ((nRet = dladdr(pFunc, &DlInfo)) != 0)
(gdb)
3427 return DlInfo.dli_sname;
(gdb) p DlInfo
$5 = {dli_fname = 0x8302e08 "/xxx/libdata.so", dli_fbase = 0xf1a43000, dli_sname = 0x0, dli_saddr = 0x0}
(gdb) p nRet
$6 = 1
(gdb) p MatchRec8Cmp - 0xf1a43000
$7 = (void (*)(TCmp *, TWork *, TThread *)) 0x11fe73
(gdb) q
The program is running. Exit anyway? (y or n) y
以下是我從objdmp
$ objdump --syms /xxx/libdata.so | grep MatchRec8Cmp
0011fe73 l F .text 00000a98 MatchRec8Cmp
果然,0011fe73 = MatchRec8Cmp得到 - 0xf1a43000。任何人都知道爲什麼dladdr無法返回dli_sname =「MatchRec8Cmp」?
我正在運行紅帽企業Linux服務器版本5.4(Tikanga)。我以前看過這個作品。也許這是我的編譯開關:
CFLAGS = -m32 -march=i686 -msse3 -ggdb3 -pipe -fno-common -fomit-frame-pointer \
-Ispio -fms-extensions -Wmissing-declarations -Wstrict-prototypes -Wunused -Wall \
-Wno-multichar -Wdisabled-optimization -Wmissing-prototypes -Wnested-externs \
-Wpointer-arith -Wextra -Wno-sign-compare -Wno-sequence-point \
-I../../../include -I/usr/local/include -fPIC \
-D$(Uname) -D_REENTRANT -D_GNU_SOURCE
我與-g嘗試它,而不是-ggdb3的,雖然我不認爲調試符號有什麼做的小精靈。
謝謝!
只是一個猜測 - 嘗試'的extern 「C」'你MatchRec8Cmp()? – YePhIcK 2012-07-30 23:55:56
值得一試,當我做objdump和func在.c文件中時,我不認爲這些名字看起來會被破壞。 – johnnycrash 2012-07-31 00:16:54
extern「C」沒有工作! :( – johnnycrash 2012-07-31 00:17:32