typedef int (*Func_CallFunc)(char* pSrcName,char* pSrcPath,char* pOutName,int* iRecordCount,char* pMsg);
pCallFunc = (Func_CallFunc)dlsym(pLibHandle,"CallFunc");
if(pCallFunc == NULL)
{
sprintf(pMsg,"打開庫[%s]函式[CallFunc]失敗.ERR:%s",pLibName,dlerror());
return -1;
}
到這里都正常。呼叫元件里的CallFunc函式報錯
Program received signal SIGSEGV, Segmentation fault.
0x00007fffef9393ea in CallFunc (pSrcName=<error reading variable: Cannot access memory at address 0x7fffffcf4108>,
pSrcPath=<error reading variable: Cannot access memory at address 0x7fffffcf4100>,
pOutName=<error reading variable: Cannot access memory at address 0x7fffffcf40f8>,
iRecordCount=<error reading variable: Cannot access memory at address 0x7fffffcf40f0>,
pMsg=<error reading variable: Cannot access memory at address 0x7fffffcf40e8>) at PGWDecode.cpp:54
uj5u.com熱心網友回復:
pLibHandle = dlopen(pLibName,RTLD_LAZY);if(pLibHandle == NULL)
{
sprintf(pMsg,"打開庫[%s]失敗.ERR:%s",pLibName,dlerror());
return -1;
}
pCallFunc = (Func_CallFunc)dlsym(pLibHandle,"CallFunc");
if(pCallFunc == NULL)
{
sprintf(pMsg,"打開庫[%s]函式[CallFunc]失敗.ERR:%s",pLibName,dlerror());
return -1;
}
int ftplib::CallLibFunc(char* pSrcName,char* pSrcPath,char* pOutName,int* iRecordCount,char* pMsg)
{
return pCallFunc(pSrcName,pSrcPath,pOutName,iRecordCount,pMsg);
}
gdb 報錯資訊
ftplib::CallLibFunc (pSrcName=0x7fffffff8b60 "bss96.ecld.com_DATA_TEST30_BB000000003300.dat",
pSrcPath=0x7fffffff9f80 "/home/bill/data/collect/data/collect_tmp0/12155", pOutName=0x685884 "", iRecordCount=0x7fffffffa698,
pMsg=0x7fffffff9980 "") at /home/bill/src_smz/collect_multiprocess/ftplib.cpp:48
return pCallFunc(pSrcName,pSrcPath,pOutName,iRecordCount,pMsg);
Program received signal SIGSEGV, Segmentation fault.
0x00007fffef9393ea in CallFunc (pSrcName=<error reading variable: Cannot access memory at address 0x7fffffcf4108>,
pSrcPath=<error reading variable: Cannot access memory at address 0x7fffffcf4100>,
pOutName=<error reading variable: Cannot access memory at address 0x7fffffcf40f8>,
iRecordCount=<error reading variable: Cannot access memory at address 0x7fffffcf40f0>,
pMsg=<error reading variable: Cannot access memory at address 0x7fffffcf40e8>) at PGWDecode.cpp:54
54 {
uj5u.com熱心網友回復:
看起來像傳參的問題,關鍵應該是看呼叫部分吧uj5u.com熱心網友回復:
使用gdb單步除錯進入函式,檢查呼叫約定(引數傳遞方式,引數個數,堆疊是否平衡,引數型別,引數取值范圍,……)呼叫約定 https://msdn.microsoft.com/zh-cn/magazine/9b372w95.aspx
uj5u.com熱心網友回復:
我手動寫了一個test程式,沒有任何業務邏輯g++ -g test.c
./a.out ./libPGWDecode.so a.txt . 就能正常呼叫libPGWDecode.so這個元件
用我們的應用程式就報上面的錯誤。
但是我們應用程式呼叫其他動態庫也能成功,就這個動態庫有問題。測驗程式代碼如下
#include <dlfcn.h>
#include <string.h>
#include <stdio.h>
//初始化庫環境
typedef int (*Func_InitEnv)(char* pMsg);
//執行轉換函式
typedef int (*Func_CallFunc)(char* pSrcName,char* pSrcPath,char* pOutName,int* iRecordCount,char* pMsg);
//釋放庫環境
typedef int (*Func_UnInitEnv)(char* pMsg);
void* pLibHandle; //動態庫句柄
Func_InitEnv pInit; //初始化函式句柄
Func_CallFunc pCallFunc; //執行函式句柄
Func_UnInitEnv pUnInit; //釋放函式句柄
int InitFtpLib(char* pLibName,char* pMsg)
{
pLibHandle = dlopen(pLibName,RTLD_LAZY);
if(pLibHandle == NULL)
{
sprintf(pMsg,"打開庫[%s]失敗.ERR:%s",pLibName,dlerror());
return -1;
}
pInit = (Func_InitEnv)dlsym(pLibHandle,"InitEnv");
if(pInit == NULL)
{
sprintf(pMsg,"打開庫[%s]函式[InitEnv]失敗.ERR:%s",pLibName,dlerror());
return -1;
}
pUnInit = (Func_InitEnv)dlsym(pLibHandle,"UnInitEnv");
if(pUnInit == NULL)
{
sprintf(pMsg,"打開庫[%s]函式[UnInitEnv]失敗.ERR:%s",pLibName,dlerror());
return -1;
}
pCallFunc = (Func_CallFunc)dlsym(pLibHandle,"CallFunc");
if(pCallFunc == NULL)
{
sprintf(pMsg,"打開庫[%s]函式[CallFunc]失敗.ERR:%s",pLibName,dlerror());
return -1;
}
//執行初始化
return pInit(pMsg);
}
int CallLibFunc(char* pSrcName,char* pSrcPath,char* pOutName,int* iRecordCount,char* pMsg)
{
return pCallFunc(pSrcName,pSrcPath,pOutName,iRecordCount,pMsg);
}
int UnInitFtpLib(char* pMsg)
{
return pUnInit(pMsg);
}
//char* libname,char* pSrcName,char* pSrcPath
//a.out libgetrecordnum.so GetRecordNum.cpp ./
int main(int argc,char **argv)
{
char msg[256];
char outname[256];
int count = 0;
if(argc < 3)
{
printf("輸入引數有誤:需要3個引數\n");
return 0;
}
count = 0;
if(-1 == InitFtpLib(argv[1],msg))
{
printf("%s\n",msg);
return 0;
}
CallLibFunc(argv[2],argv[3],outname,&count,msg);
UnInitFtpLib(msg);
printf("outname=%s,recordcount=%d,msg=%s\n",outname,count,msg);
return 0;
}
uj5u.com熱心網友回復:
你好,怎么查看?
uj5u.com熱心網友回復:
匯編功底不深還真看不懂。
你干脆用“創建子行程,命令列傳參進你的test程式,修改你的test程式將結果資料保存到臨時文本檔案或發送到標準輸出,讀取結果檔案或讀取重定向后標準輸出,得到呼叫結果”的辦法隔離直接呼叫出錯問題。
uj5u.com熱心網友回復:
請問樓主這個問題解決了嗎,我也遇到了樣的問題。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/250306.html
標籤:C語言
上一篇:求幫助,初學C語言輸入三個數沒反應,輸入十個數時,直接結束
下一篇:vs2017輸出字符為亂碼?
