c/c++基礎(二十六) gdb調試so

來源:互聯網
上載者:User

當自己開發了一個so檔案,如何調試呢。

對於如何編寫一個so,請參考文章:http://blog.csdn.net/zz7zz7zz/article/details/41448987

對於如何進行gdb調試,請參考文章:http://blog.csdn.net/zz7zz7zz/article/details/41654457


我們的源檔案分別如下:

filea.c

#include <stdio.h>void fun1(){printf("i am from filea fun1 \n");printf("i am from filea fun11 \n");printf("i am from filea fun12 \n");}

fileb.c

#include <stdio.h>void fun2(){printf("i am from fileb fun2 \n");printf("i am from fileb fun21 \n");printf("i am from fileb fun22 \n");}

CLoadSo.h

#ifndef _CLOADSO_H#define _CLOADSO_H#ifdef _cplusplusextern "C" {#endifvoid fun1();void fun2();#ifdef _cplusplus}#endif#endif

CLoadSo.c

#include <stdio.h>#include <dlfcn.h>#include "CLoadSo.h"int main(int argc,char **argv){void *soHandle;int (*fun)();char *errorMsg;soHandle=dlopen("first.so",RTLD_LAZY);errorMsg=dlerror();printf("A1---------loadSo  is %s \n",soHandle ? "success" : "failed");if(errorMsg){printf("A2--------loadSo error , Msg is: %s \n",errorMsg);return -1;}fun=dlsym(soHandle,"fun1");errorMsg=dlerror();printf("B1---------fun1 , fun1 is %s \n",fun ? "success" : "Null");if(fun){fun();}if(errorMsg){printf("B2---------fun1 error , Msg is: %s \n",errorMsg);}fun=dlsym(soHandle,"fun2");errorMsg=dlerror();printf("B3---------fun2 , fun2 is %s \n",fun ? "success" : "Null");if(fun){fun();}if(errorMsg){printf("B4---------fun2 error , Msg is: %s \n",errorMsg);}fun=dlsym(soHandle,"fun3");errorMsg=dlerror();printf("B5---------fun3 , fun3 is %s \n",fun ? "success": "Null");if(fun){fun();}if(!errorMsg){printf("B6---------fun3 error , Msg is: %s \n",errorMsg);}dlclose(soHandle);printf("C---------close LoadSo \n");return 0;}

用命令  gcc -Wall -fpic -g -shared filea.c fileb.c -o first.so 產生first.so檔案

用命令 gcc -g -Wall CLoadSo.c -o CLoadSo -ldl產生可執行檔CLoadSo


gdb步驟如下(請注意觀察備註陳述式):




結果:成功進入到了so的fun1中

大概步驟:

1.啟動gdb調試:gdb CLoadSo

2.打斷點 gdb) : break 28

3.執行 gdb) : run

4.將動態庫的符號讀入gdb,為了你能找到變數和函數名 gdb)sharedlibrary first.so

5.s進入函數,l列出原始碼,n逐步執行,直到結束.




代碼地址:http://download.csdn.net/detail/zz7zz7zz/8311533




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.