在ARM Linux 使用 Valgrind


Linux valgrind 移植到ARM-Linux


 一、Cross-Compile/交叉編譯

(1)下載及解壓Valgrind-3.11

(2)修改confirure

  將armv7*)修改為armv7*|arm*)

(3)執行configure

./configure CC=arm-linux-gcc CPP=arm-linux-cpp CXX=arm-linux-g++  --host=arm-linux --prefix=/opt/valgrind/lib

注意:CC=arm-linux-gcc,之所以沒有像有些博客上寫的、用了絕對路徑,是因為「我已經將arm-linux-gcc 軟鏈接/soft-linke 到了實際的gcc」。

(4)make

(5)make install

二、移植到ARM開發板

注意:make install之后,編譯生成的bin/和lib/目錄存放在PC上的/opt/valgrind/lib目錄下。將此目錄下的bin/和lib/目錄單獨「復制/Copy」出來,不要Copy share/和include/目錄,因為文件有點大,而且include/和share/這兩個目錄在開發板上沒有用。

  在ARM開發板上建立「目錄-/opt/valgrind/lib/valgrind/」,將上面剛剛編譯得到的lib/目錄下的文件(.so, .a 等)放到左邊提到的這個目錄(/opt/valgrind/lib/valgrind)中。

注意:上面這個步驟非常關鍵,如果放錯位置,在運行程序valgrind的時候,會提示「failed to start 'memcheck' : No such file or directory」。

三、ARM上配置使用

1、Error:在運行「./valgrind ls」的時候依然碰到了一個錯誤,出現在「vgdb」。錯誤提示是:

[12:58:19]root@freescale ~/valgrind/valgrind/bin$ ./valgrind ls           
[12:58:19]==5978== Memcheck, a memory error detector
[12:58:19]==5978== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
[12:58:19]==5978== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
[12:58:19]==5978== Command: ls
[12:58:19]==5978== 
[12:58:20]==5978== error writing 36 bytes to shared mem /tmp/vgdb-pipe-shared-mem-vgdb-5978-by-root-on-???

在網頁failure to run on armv6 following the armv6 legacy patches suggested by bug 276897找到了一個暫時的方法,就是臨時禁用「vgdb」。

[12:54:55]root@freescale ~/valgrind/valgrind/bin$ ./valgrind --vgdb=no ls 
[12:54:55]==5976== Memcheck, a memory error detector
[12:54:55]==5976== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
[12:54:55]==5976== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
[12:54:55]==5976== Command: ls
[12:54:55]==5976== 
[12:54:56]ERROR: ld.so: object '/opt/valgrind/lib/valgrind/vgpreload_core-arm-linux.so' from LD_PRELOAD cannot be preloaded: ignored.
[12:54:56]ERROR: ld.so: object '/opt/valgrind/lib/valgrind/vgpreload_memcheck-arm-linux.so' from LD_PRELOAD cannot be preloaded: ignored.
[12:54:57]==5976== Conditional jump or move depends on uninitialised value(s)
[12:54:57]==5976==    at 0x4909C98: index (in /lib/libc-2.11.1.so)
[12:54:57]==5976== 
[12:54:58]==5976== Conditional jump or move depends on uninitialised value(s)
[12:54:58]==5976==    at 0x4909D90: strcmp (in /lib/libc-2.11.1.so)
[12:54:58]==5976==    by 0x4910377: strcoll_l (in /lib/libc-2.11.1.so)
[12:54:58]==5976== 
[12:54:58]==5976== Conditional jump or move depends on uninitialised value(s)
[12:54:58]==5976==    at 0x4909D98: strcmp (in /lib/libc-2.11.1.so)
[12:54:58]==5976==    by 0x4910377: strcoll_l (in /lib/libc-2.11.1.so)

 2、LD_PRELOAD錯誤

 [12:54:56]ERROR: ld.so: object '/opt/valgrind/lib/valgrind/vgpreload_core-arm-linux.so' from LD_PRELOAD cannot be preloaded: ignored. 

上述的錯誤,經過檢查,發現:「/opt/valgrind/lib/valgrind」缺少一些「.so/共享動態庫」。重新編譯「Valgrind」,並且用「arm-linux-strip」將文件進行精簡,順利復制。程序也順利運行。

 

四、Valgrind簡介

1、Valgrind是什么?

2、Valgrind能做什么?

(1)「badapp.c」

 

 1 #include <stdlib.h>
 2 
 3 void f(void)
 4 {
 5  int* x = malloc(10 * sizeof(int));
 6  x[10] = 0;            // problem 1: heap block overrun
 7 }                         // problem 2: memory leak -- x not freed
 8 
 9 int main(void)
10 {
11  f();
12  return 0;
13 }

 

 

(2)

[15:20:06]<valgrind/bin/valgrind --vgdb=no --leak-check=full ./bad1pp1                 
[15:20:06]==8399== Memcheck, a memory error detector
[15:20:06]==8399== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
[15:20:06]==8399== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
[15:20:06]==8399== Command: ./bad1pp1
[15:20:06]==8399== 
[15:20:07]connect fail. ip:10.167.13.207, strlen(ip):13. File:main.c, Line:696
[15:20:07]Internet Fail. File: main.c, Line: 2469
[15:20:08]==8399== Invalid write of size 4
[15:20:08]==8399==    at 0x8414: f (badapp1.c:6)
[15:20:08]==8399==    by 0x842F: main (badapp1.c:11)
[15:20:08]==8399==  Address 0x496f050 is 0 bytes after a block of size 40 alloc'd
[15:20:08]==8399==    at 0x483481C: malloc (in /opt/valgrind/lib/valgrind/vgpreload_memcheck-arm-linux.so)
[15:20:08]==8399== 
[15:20:08]==8399== 
[15:20:08]==8399== HEAP SUMMARY:
[15:20:08]==8399==     in use at exit: 40 bytes in 1 blocks
[15:20:08]==8399==   total heap usage: 1 allocs, 0 frees, 40 bytes allocated
[15:20:08]==8399== 
[15:20:08]==8399== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
[15:20:08]==8399==    at 0x483481C: malloc (in /opt/valgrind/lib/valgrind/vgpreload_memcheck-arm-linux.so)
[15:20:08]==8399== 
[15:20:08]==8399== LEAK SUMMARY:
[15:20:08]==8399==    definitely lost: 40 bytes in 1 blocks
[15:20:08]==8399==    indirectly lost: 0 bytes in 0 blocks
[15:20:08]==8399==      possibly lost: 0 bytes in 0 blocks
[15:20:08]==8399==    still reachable: 0 bytes in 0 blocks
[15:20:08]==8399==         suppressed: 0 bytes in 0 blocks
[15:20:08]==8399== 
[15:20:08]==8399== For counts of detected and suppressed errors, rerun with: -v
[15:20:08]==8399== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 11 from 5)

 

 

3、Valgrind工作原理?

4、誰開發的Valgrind?

Julian Seward, from Cambridge, UK.

5、Valgrind的名稱來源

From Nordic mythology. Originally (before release) the project was named Heimdall, after the watchman of the Nordic gods. He could "see a hundred miles by day or night, hear the grass growing, see the wool growing on a sheep's back" (etc). This would have been a great name, but it was already taken by a security package "Heimdal".

Keeping with the Nordic theme, Valgrind was chosen. Valgrind is the name of the main entrance to Valhalla (the Hall of the Chosen Slain in Asgard). Over this entrance there resides a wolf and over it there is the head of a boar and on it perches a huge eagle, whose eyes can see to the far regions of the nine worlds. Only those judged worthy by the guardians are allowed to pass through Valgrind. All others are refused entrance.

It's not short for "value grinder", although that's not a bad guess.

 

參考:Where does the name 'Valgrind' come from?

6、如何利用Valgrind結果?

 


 

參考:

1、應用 Valgrind 發現 Linux 程序的內存問題

2、Valgrind 基礎

3、Valgrind Frequently Asked Questions

4、Valgrind官網

5、Valgrind Guide

6、Valgrind Quick Start


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM