通過valgrind 輸出的位移地址定位源碼行號

來源:互聯網
上載者:User

有時用valgrind定位記憶體泄露問題時當記憶體泄露的位置在動態庫(so)中時, 輸出的調用棧為問號"???"並且沒有指明源碼的行號.即使嘗試了加 -g 的編譯參數並且程式退出前不執行dlclose,也無濟於事.

==29941== 17 bytes in 1 blocks are definitely lost in loss record 29 of 197==29941==    at 0x402A185: malloc (vg_replace_malloc.c:292)==29941==    by 0x4048585: ??? (in /home/xxx/ElastosRDKforDevice/Targets/rdk/x86.gnu.linux.dbg/bin/Elastos.Runtime.so)==29941==    by 0x40799F9: ??? (in /home/xxx/ElastosRDKforDevice/Targets/rdk/x86.gnu.linux.dbg/bin/Elastos.Runtime.so)==29941==    by 0x407AE2A: ??? (in /home/xxx/ElastosRDKforDevice/Targets/rdk/x86.gnu.linux.dbg/bin/Elastos.Runtime.so)==29941==    by 0x407ACC0: ??? (in /home/xxx/ElastosRDKforDevice/Targets/rdk/x86.gnu.linux.dbg/bin/Elastos.Runtime.so)==29941==    by 0x407ACF9: ??? (in /home/xxx/ElastosRDKforDevice/Targets/rdk/x86.gnu.linux.dbg/bin/Elastos.Runtime.so)==29941==    by 0x400ED76: call_init.part.0 (dl-init.c:78)==29941==    by 0x400EE63: _dl_init (dl-init.c:36)==29941==    by 0x400110E: ??? (in /lib/i386-linux-gnu/ld-2.19.so)

這種情況下通過每行的位移地址配合addr2line也可以定位到具體的程式碼:

1. 重新運行valgrind命令執行記憶體泄露的檢查工作.

2. 查看valgrind log中的thread Id, 如上"29941"

3. 在程式退出之前 cat /proc/29941/maps 檔案, 可以看到載入動態庫的資訊

04038000-040ab000 r-xp 00000000 08:05 6560496    /home/xxx/ElastosRDKforDevice/Targets/rdk/x86.gnu.linux.dbg/bin/Elastos.Runtime.so040ab000-040ac000 r--p 00073000 08:05 6560496    /home/xxx/ElastosRDKforDevice/Targets/rdk/x86.gnu.linux.dbg/bin/Elastos.Runtime.so040ac000-040ad000 rw-p 00074000 08:05 6560496    /home/xxx/ElastosRDKforDevice/Targets/rdk/x86.gnu.linux.dbg/bin/Elastos.Runtime.so
我們關心的是 r-xp 這一行, 最前邊的 04038000 就是這個動態庫載入的 base 地址.

4. 然後用發生記憶體泄露的地址0x4048585減去base地址0x4038000, 得到 0x10585 的相對位移地址.

5. 利用addr2line工具

addr2line -e ./Elastos.Runtime.so 0x10585

得到具體源碼行號:

/home/xxx/ElastosRDKforDevice/Sources/ElastosSources/Elastos/Runtime/Library/eltypes/elstring/elsharedbuf.cpp:9

打完收工.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.