valgrind arm-linux 交叉編譯
Valgrind是用於構建動態分析工具的儀器框架。 它附帶了一組工具,每個工具都執行某種除錯,分析或類似任務,可幫助您改進程式。
Valgrind的架構採用模組化設計,因此可以輕鬆建立新工具,而不會干擾現有結構。
開始工作前,有兩項資訊不得不看,那就是平臺和工具概述,雖然百度查了一些,但畢竟不如官方的準確:
平臺支援,我的 ARM-v7 是支援的
http://valgrind.org/info/platforms.html
工具概述:
http://valgrind.org/info/tools.html
標準配置提供了許多有用的工具。
-
Memcheck是一個記憶體錯誤檢測器。 它可以幫助您使程式,尤其是那些用C和C ++編寫的程式更加正確。
-
Cachegrind是快取和分支預測分析器。 它可以幫助您使程式執行得更快。
-
Callgrind是一個生成快取分析器的呼叫圖。 它與Cachegrind有一些重疊,但也收集了Cachegrind沒有的一些資訊。
-
Helgrind是一個執行緒錯誤檢測器。 它可以幫助您使多執行緒程式更正確。
-
DRD也是執行緒錯誤檢測器。 它與Helgrind類似,但使用不同的分析技術,因此可能會發現不同的問題。
-
Massif是一個堆分析器。 它可以幫助您使程式使用更少的記憶體。
-
DHAT是一種不同型別的堆分析器。 它可以幫助您瞭解塊壽命,塊利用率和佈局效率低下的問題。
-
SGcheck是一種實驗工具,可以檢測堆疊和全域性陣列的溢位。 它的功能與Memcheck的功能互補:SGcheck發現Memcheck無法解決的問題,反之亦然。
-
BBV是一個實驗性的SimPoint基本塊向量生成器。 它對進行計算機體系結構研究和開發的人很有用。
其中官方解釋到:
Memcheck檢測記憶體管理問題,主要針對C和C ++程式。Memcheck執行程式比正常情況慢大約10-30倍
Cachegrind執行程式比正常情況慢大約20-100倍。
Massif執行程式比正常情況慢20倍
1.下載原始碼
http://valgrind.org/
2 解壓後進行配置:
./configure --prefix=/home/sun/share/install --host=arm-buildroot-linux-uclibcgnueabi
配置報錯:
checking for a supported CPU... no (arm) configure: error: Unsupported host architecture. Sorry
檢視官方網站首頁,發現對 ARM-LINUX 是支援的
It also includes three experimental tools: a stack/global array overrun detector, a second heap profiler that examines how heap blocks are used, and a SimPoint basic block vector generator. It runs on the following platforms: X86/Linux, AMD64/Linux, ARM/Linux, ARM64/Linux, PPC32/Linux, PPC64/Linux, PPC64LE/Linux, S390X/Linux, MIPS32/Linux, MIPS64/Linux, X86/Solaris, AMD64/Solaris, ARM/Android (2.3.x and later), ARM64/Android, X86/Android (4.0 and later), MIPS32/Android, X86/Darwin and AMD64/Darwin (Mac OS X 10.12).
修改 configure 檔案:
將 armv7a* 改為 arm* 再次配置就不會報錯了
修改前:
修改後:
3.編譯
make -j4 make install
會生成四個目錄: bin lib share include
4.我的板子空間非常小,所以需要刪除不需要的工具,只留下記憶體檢查工具,
需要刪除 lib/valgrind 目錄下的檔案 以及 整個 share 目錄,最後精簡到 12M 左右:
sun@machine:~/share/install$ du -sh bin include/ lib/ 520Kbin 2.1Minclude/ 10Mlib/
精簡後的 lib/valgrind 目錄下所有檔案:
32bit-core-valgrind-s1.xml32bit-sse.xmlarm-with-vfpv3.xml 32bit-core-valgrind-s2.xmlarm-core-valgrind-s1.xmldefault.supp 32bit-core.xmlarm-core-valgrind-s2.xmlgetoff-arm-linux 32bit-linux-valgrind-s1.xmlarm-core.xmlmemcheck-arm-linux 32bit-linux-valgrind-s2.xmlarm-vfpv3-valgrind-s1.xmlvgpreload_core-arm-linux.so 32bit-linux.xmlarm-vfpv3-valgrind-s2.xmlvgpreload_memcheck-arm-linux.so 32bit-sse-valgrind-s1.xmlarm-vfpv3.xml 32bit-sse-valgrind-s2.xmlarm-with-vfpv3-valgrind.xml
檔名含有 ARM-LINUX 字樣的檔案資訊:
sun@machine:~/share/install/lib/valgrind$ file *arm-linux* getoff-arm-linux:ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-uClibc.so.0, with debug_info, not stripped memcheck-arm-linux:ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, with debug_info, not stripped vgpreload_core-arm-linux.so:ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, with debug_info, not stripped vgpreload_memcheck-arm-linux.so: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, with debug_info, not stripped
5.對於執行環境的要求:
http://valgrind.org/docs/manual/dist.readme-packagers.html
7. README_PACKAGERS Greetings, packaging person!This information is aimed at people building binary distributions of Valgrind. Thanks for taking the time and effort to make a binary distribution of Valgrind.The following notes may save you some trouble. -- If your toolchain (compiler, linker) support lto, using the configure option --enable-lto=yes will produce a smaller/faster valgrind (up to 10%). -- Do not ship your Linux distro with a completely stripped /lib/ld.so.At least leave the debugging symbol names on -- line number info isn't necessary.If you don't want to leave symbols on ld.so, alternatively you can have your distro install ld.so's debuginfo package by default, or make ld.so.debuginfo be a requirement of your Valgrind RPM/DEB/whatever. Reason for this is that Valgrind's Memcheck tool needs to intercept calls to, and provide replacements for, some symbols in ld.so at startup (most importantly strlen).If it cannot do that, Memcheck shows a large number of false positives due to the highly optimised strlen (etc) routines in ld.so.This has caused some trouble in the past.As of version 3.3.0, on some targets (ppc32-linux, ppc64-linux), Memcheck will simply stop at startup (and print an error message) if such symbols are not present, because it is infeasible to continue. It's not like this is going to cost you much space.We only need the symbols for ld.so (a few K at most).Not the debug info and not any debuginfo or extra symbols for any other libraries. -- (Unfortunate but true) When you configure to build with the --prefix=/foo/bar/xyzzy option, the prefix /foo/bar/xyzzy gets baked into valgrind.The consequence is that you _must_ install valgrind at the location specified in the prefix.If you don't, it may appear to work, but will break doing some obscure things, particularly doing fork() and exec(). So you can't build a relocatable RPM / whatever from Valgrind. -- Don't strip the debug info off lib/valgrind/$platform/vgpreload*.so in the installation tree.Either Valgrind won't work at all, or it will still work if you do, but will generate less helpful error messages.Here's an example: Mismatched free() / delete / delete [] at 0x40043249: free (vg_clientfuncs.c:171) by 0x4102BB4E: QGArray::~QGArray(void) (tools/qgarray.cpp:149) by 0x4C261C41: PptDoc::~PptDoc(void) (include/qmemarray.h:60) by 0x4C261F0E: PptXml::~PptXml(void) (pptxml.cc:44) Address 0x4BB292A8 is 0 bytes inside a block of size 64 alloc'd at 0x4004318C: __builtin_vec_new (vg_clientfuncs.c:152) by 0x4C21BC15: KLaola::readSBStream(int) const (klaola.cc:314) by 0x4C21C155: KLaola::stream(KLaola::OLENode const *) (klaola.cc:416) by 0x4C21788F: OLEFilter::convert(QCString const &) (olefilter.cc:272) This tells you that some memory allocated with new[] was freed with free(). Mismatched free() / delete / delete [] at 0x40043249: (inside vgpreload_memcheck.so) by 0x4102BB4E: QGArray::~QGArray(void) (tools/qgarray.cpp:149) by 0x4C261C41: PptDoc::~PptDoc(void) (include/qmemarray.h:60) by 0x4C261F0E: PptXml::~PptXml(void) (pptxml.cc:44) Address 0x4BB292A8 is 0 bytes inside a block of size 64 alloc'd at 0x4004318C: (inside vgpreload_memcheck.so) by 0x4C21BC15: KLaola::readSBStream(int) const (klaola.cc:314) by 0x4C21C155: KLaola::stream(KLaola::OLENode const *) (klaola.cc:416) by 0x4C21788F: OLEFilter::convert(QCString const &) (olefilter.cc:272) This isn't so helpful.Although you can tell there is a mismatch, the names of the allocating and deallocating functions are no longer visible.The same kind of thing occurs in various other messages from valgrind. -- Don't strip symbols from lib/valgrind/* in the installation tree. Doing so will likely cause problems.Removing the line number info is probably OK (at least for some of the files in that directory), although that has not been tested by the Valgrind developers. -- Please test the final installation works by running it on something huge.I suggest checking that it can start and exit successfully both Firefox and OpenOffice.org.I use these as test programs, and I know they fairly thoroughly exercise Valgrind.The command lines to use are: valgrind -v --trace-children=yes firefox valgrind -v --trace-children=yes soffice If you find any more hints/tips for packaging, please report it as a bugreport. See http://www.valgrind.org for details. View Code
裡面提到五個重要的事項:
1 - 如果您的工具鏈(編譯器,連結器)支援lto,則使用configure 選項--enable-lto = yes將產生更小/更快的valgrind 2 - 不要使用完全剝離的Linux發行版/lib/ld.so。(比如上面的getoff-arm-linux 就連結了 /lib/ld-uClibc.so.0, 那就要求我們的開發板根目錄下的/lib/ld-uClibc.so.0 必須是 not stripped) 3 在開發板上面執行的目錄必須與 --prefix = 指定的目錄完全一致 4 - 不要將除錯資訊從lib / valgrind / $ platform / vgpreload * .so中刪除(比如上面的vgpreload_core-arm-linux.so 和 vgpreload_memcheck-arm-linux.so 必須是 not stripped) 5 - 不要在安裝樹中從lib / valgrind / *中刪除符號。(比如上面的getoff-arm-linux 和 memcheck-arm-linux必須是 not stripped)
第 4、5 兩條就要求我們 lib/valgrind/ 目錄下的任何檔案都必須是 not stripped,這樣才能保證程式可靠
將所有精簡後的目錄放到開發板的指定目錄(即 --prefix=/home/sun/share/install 這個目錄),如果你忘記了當時編譯的目錄是哪個,請檢視 lib/pkgconfig/ valgrind.pc
檔案內有絕對路徑的說明:如 prefix=/h ome/sun/sha re/install
sun@machine:~/share/install/lib/pkgconfig$ cat valgrind.pc prefix=/home/sun/share/install exec_prefix=${prefix} libdir=${exec_prefix}/lib includedir=${prefix}/include/valgrind arch=arm os=linux platform=arm-linux valt_load_address=0x58000000 Name: Valgrind Description: A dynamic binary instrumentation framework Version: 3.14.0 Requires: Libs: -L${libdir}/valgrind -lcoregrind-arm-linux -lvex-arm-linux -lgcc Cflags: -I${includedir}
一般情況下都可以成功執行,偶爾adb push 進去後不能執行,如果已經確定在開發板上面的路徑與 prefix 指定的路徑完全一致了,但依然報以下錯誤:
valgrind: failed to start tool 'memcheck' for platform 'arm-linux': No such file or directory
那就需要指定庫的路徑了:
VALGRIND_LIB=/home/sun/share/install/lib ./valgrind/home/sun/share/install/bin/valgrind --help
如果可以正常執行,那就設定到環境變數,只在當前終端有效,如果開啟另一個終端,則需要再次設定,如果終端關閉或板子重啟,同樣需要再次設定:
export VALGRIND_LIB=/home/test/valgrind/lib/valgrind
設定後就可以直接運行了 ./valgrind/home/sun/share/install/bin/valgrind --help 可以成功執行
./valgrind/home/sun/share/install/bin/valgrind ls 報錯
錯誤資訊是:內部錯誤,valgrind 段錯誤退出了
來來回回折騰了好幾天,交叉工具鏈都重新制作了,用新版本的uclibc,依然報錯
替換核心版本後 依然報錯
換成幾個舊版本的valgrind 依然報錯
最後又重新檢視 valgrind 對平臺的CPU架構的支援 , 才發現一開始就由於疏忽錯過了重要資訊
當前
Valgrind支援以下平臺:
- x86 / Linux: 最高可包括SSSE3,但不高 - 沒有SSE4,AVX,AVX2。 此目標現在處於維護模式..
- AMD64 / Linux: 包括AVX2在內。 這是主要的開發目標,並且往往得到很好的支援。
- PPC32 / Linux,PPC64 / Linux,PPC64LE / Linux: 包括Power8在內。
- S390X / Linux: 支援。
- ARM / Linux: 自ARMv7起支援。
- ARM64 / Linux: ARMv8支援。
- MIPS32 / Linux,MIPS64 / Linux: 支援。
- X86 / Solaris,AMD64 / Solaris,X86 / illumos,AMD64 / illumos :自Solaris 11以來受支援。
- X86 / Darwin(10.10,10.11),AMD64 / Darwin(10.10,10.11): 支援。
- ARM / Android,ARM64 / Android,MIPS32 / Android,X86 / Android: 支援。
在Linux上,您必須執行核心3.0或更高版本,以及glibc 2.5.X或更高版本 。 在Mac OS X上,您必須執行10.9.x或更高版本。
移植計劃
Valgrind 3.X擁有支援多平臺的基礎設施。 平臺是特定的(CPU,OS)配對,例如x86 / Linux或AMD64 / Linux。
維護每個埠需要付出很多努力,比大多數其他程式要多。 Valgrind很脆弱,因為它的作用很低階。 此外,每個平臺埠都有特定於CPU的程式碼,特定於作業系統的程式碼和特定於平臺的程式碼,並且難以測試所有組合。
因此,我們只能證明支援廣泛使用的平臺。 與NetBSD或GCC不同,我們對Valgrind在已知領域的每個平臺上工作都不感興趣:維護負擔過高。 因此,將Valgrind移植到不同的平臺並不僅僅是一項技術練習:您還需要做出一個令人信服的案例,即努力是值得的,並且至少在可預見的未來,埠將得到適當的支援。
Windows不在考慮之中,因為移植到它需要進行如此多的更改,它幾乎就是一個單獨的專案。 (但是,Valgrind + Wine可以通過一些努力來實現。)此外,非開源作業系統很難處理; 能夠看到作業系統和相關的(libc)原始碼使事情變得更容易。 但是,Valgrind可以與Wine結合使用,這意味著可以在Valgrind下執行Windows程式。
此訊息說明了我們的移植原理。 我們一如既往地採用靈活的方法,如果您有任何意見,我們有興趣聽取您的意見/移植需求。
我的核心是 4.4.110 支援的,但C庫是uclibc valgrind不支援,所以無法執行