IBM Presentations
Total Page:16
File Type:pdf, Size:1020Kb
Linux on System z debugging with Valgrind Christian Bornträger <[email protected]> © 2012 IBM Corporation Linux on System z debugging with Valgrind Trademarks The following are trademarks of the International Business Machines Corporation in the United States and/or other countries. AIX* IBM* PowerVM System z10 z/OS* BladeCenter* IBM eServer PR/SM WebSphere* zSeries* DataPower* IBM (logo)* Smarter Planet z9* z/VM* DB2* InfiniBand* System x* z10 BC z/VSE FICON* Parallel Sysplex* System z* z10 EC GDPS* POWER* System z9* zEnterprise HiperSockets POWER7* * Registered trademarks of IBM Corporation The following are trademarks or registered trademarks of other companies. Adobe, the Adobe logo, PostScript, and the PostScript logo are either registered trademarks or trademarks of Adobe Systems Incorporated in the United States, and/or other countries. Cell Broadband Engine is a trademark of Sony Computer Entertainment, Inc. in the United States, other countries, or both and is used under license there from. Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both. Microsoft, Windows, Windows NT, and the Windows logo are trademarks of Microsoft Corporation in the United States, other countries, or both. Windows Server and the Windows logo are trademarks of the Microsoft group of countries. InfiniBand is a trademark and service mark of the InfiniBand Trade Association. Intel, Intel logo, Intel Inside, Intel Inside logo, Intel Centrino, Intel Centrino logo, Celeron, Intel Xeon, Intel SpeedStep, Itanium, and Pentium are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States and other countries. UNIX is a registered trademark of The Open Group in the United States and other countries. Linux is a registered trademark of Linus Torvalds in the United States, other countries, or both. ITIL is a registered trademark, and a registered community trademark of the Office of Government Commerce, and is registered in the U.S. Patent and Trademark Office. IT Infrastructure Library is a registered trademark of the Central Computer and Telecommunications Agency, which is now part of the Office of Government Commerce. * All other products may be trademarks or registered trademarks of their respective companies. Notes: Performance is in Internal Throughput Rate (ITR) ratio based on measurements and projections using standard IBM benchmarks in a controlled environment. The actual throughput that any user will experience will vary depending upon considerations such as the amount of multiprogramming in the user's job stream, the I/O configuration, the storage configuration, and the workload processed. Therefore, no assurance can be given that an individual user will achieve throughput improvements equivalent to the performance ratios stated here. IBM hardware products are manufactured from new parts, or new and serviceable used parts. Regardless, our warranty terms apply. All customer examples cited or described in this presentation are presented as illustrations of the manner in which some customers have used IBM products and the results they may have achieved. Actual environmental costs and performance characteristics will vary depending on individual customer configurations and conditions. This publication was produced in the United States. IBM may not offer the products, services or features discussed in this document in other countries, and the information may be subject to change without notice. Consult your local IBM business contact for information on the product or services available in your area. All statements regarding IBM's future direction and intent are subject to change or withdrawal without notice, and represent goals and objectives only. Information about non-IBM products is obtained from the manufacturers of those products or their published announcements. IBM has not tested those products and cannot confirm the performance, compatibility, or any other claims related to non-IBM products. Questions on the capabilities of non-IBM products should be addressed to the suppliers of those products. Prices subject to change without notice. Contact your IBM representative or Business Partner for the most current pricing in your geography. 2 © 2012 IBM Corporation Linux on System z debugging with Valgrind Overview (1) ●Valgrind is one of the most prominent debugging tools for Linux ●Finds long-standing and hard to find problems ●Works on binary code ● no source code necessary (e.g. if linked against a closed library) ● No need to recompile ●Plenty of users: Firefox, OpenOffice, AbiWord, Opera, KDE, GNOME, Qt, libstdc++, MySQL, PostgreSQL, Perl, Python, PHP, Samba, RenderMan, Nasa Mars Lander software, SAS, The GIMP, Ogg Vorbis, Unreal Tournament, Medal of Honour... ●Also supports x86,power,arm and mips ●Modular system: translation framework + tools ●award-winning ● May 2008:TrollTech's „inaugural Qt Open Source Development Award for the best open source development tool“ ● July 2006: Julian Seward won a Google-O'Reilly Open Source Award for "Best Toolmaker" für his work on valgrind 3 © 2012 IBM Corporation Linux on System z debugging with Valgrind Overview (2) ●Technology is based on a JIT (Just-in-Time Compiler) ●Intermediate language allows debugging instrumentation Binary 000000008000062c <main>: valgrind stmg %r9,%r15,72(%r15) lay %r15,-80160(%r15) lhi %r12,0 kernel lhi %r10,10000 la %r9,160(%r15) lgr %r13,%r9 lgr %r11,%r9 e lghi %r2,1 brasl %r14,8000044c <malloc@plt> c lgfr %r1,%r12 a ahi %r12,1 translation f stg %r2,0(%r11) r sllg %r1,%r1,3 Replace aghi %r11,8 e pfd 2,96(%r1,%r9) into IR t brct %r10,8000064c <main+0x20> n some of i lay %r12,80160(%r15) lg %r2,0(%r13) New l aghi %r13,8 System call l brasl %r14,8000048c <free@plt> The library cgrjne %r12,%r13,8000067e <main+0x52> binary a lhi %r13,0 wrapper c lhi %r12,10000 calls by xxx lgfr %r2,%r13 ahi %r13,1 instrumentation m brasl %r14,800005c0 <stacker> brct %r12,8000069c <main+0x70> Using a e lg %r4,80272(%r15) t lmg %r9,%r15,80232(%r15) s br %r4 preload y library translation S libraries To machine code 4 © 2012 IBM Corporation Linux on System z debugging with Valgrind Overview (3) ●Usage: valgrind <program> # valgrind buggy_program ==2799== Memcheck, a memory error detector ==2799== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. ==2799== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info ==2799== Command: buggy_program ==2799== ==2799== HEAP SUMMARY: ==2799== in use at exit: 200 bytes in 2 blocks ==2799== total heap usage: 2 allocs, 0 frees, 200 bytes allocated ==2799== ==2799== LEAK SUMMARY: ==2799== definitely lost: 100 bytes in 1 blocks ==2799== indirectly lost: 0 bytes in 0 blocks ==2799== possibly lost: 0 bytes in 0 blocks ==2799== still reachable: 100 bytes in 1 blocks ==2799== suppressed: 0 bytes in 0 blocks ==2799== Rerun with --leak-check=full to see details of leaked memory [...] 5 © 2012 IBM Corporation Linux on System z debugging with Valgrind Tools ●Several tools –Memcheck (default): detects memory and data flow problems –Cachegrind: cache profiling –Massif: heap profiling –Helgrind: thread debugging ● DRD: thread debugging ● None: no debugging (for valgrind JIT testing) ● Callgrind: codeflow and profiling ●Tool can be selected with --tool=none|memcheck| cachegrind... 6 © 2012 IBM Corporation Linux on System z debugging with Valgrind Memcheck ●memcheck detects –Invalid accesses • Non-allocated memory • use-after-free • Array overruns on heap • Invalid stack areas –Use of uninitialized data –Memory leaks –Invalid free • Double free • Wrong free function (free vs. delete vs. delete[] etc.) –Invalid overlap (memcpy) –Unfortunately no array overrun on stack ●Important parameters: –--leak-check=full –--track-origins=yes 7 © 2012 IBM Corporation Linux on System z debugging with Valgrind Memcheck – memory leaks 01: #include <stdio.h> 02: #include <stdlib.h> 03: #include <string.h> 04: 05: int main() 06: { 07: int i; 08: 09: for (i=0; i < 5; i++) 10: malloc(1); 11: } cborntra@r1745045:~/valgrind-demo> valgrind --leak-check=full ./memcheck-leak [...] ==4390== ==4390== HEAP SUMMARY: ==4390== in use at exit: 5 bytes in 5 blocks ==4390== total heap usage: 5 allocs, 0 frees, 5 bytes allocated ==4390== ==4390== 4 bytes in 4 blocks are definitely lost in loss record 2 of 2 ==4390== at 0x4026B26: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-s390x- linux.so) ==4390== by 0x800005F1: main (memcheck-leak.c:10) ==4390== ==4390== LEAK SUMMARY: ==4390== definitely lost: 4 bytes in 4 blocks ==4390== indirectly lost: 0 bytes in 0 blocks ==4390== possibly lost: 0 bytes in 0 blocks ==4390== still reachable: 1 bytes in 1 blocks ==4390== suppressed: 0 bytes in 0 blocks ==4390== Reachable blocks (those to which a pointer was found) are not shown. ==4390== To see them, rerun with: --leak-check=full --show-reachable=yes [...] 8 © 2012 IBM Corporation Linux on System z debugging with Valgrind Memcheck – missing initialisation 01: #include <stdlib.h> 02: int main() 03: { 04: int a,b,c; 05: 06: a = 1; 07: c = a + b; 08: exit(c); 09: } cborntra@r1745045:~/valgrind-demo> valgrind --track-origins=yes ./memcheck-unit […] ==4471== Syscall param exit_group(status) contains uninitialised byte(s) ==4471== at 0x41008A2: _Exit (in /lib64/libc-2.4.so) ==4471== by 0x4093B43: exit (in /lib64/libc-2.4.so) ==4471== by 0x800005FF: main (memcheck-unit.c:8) ==4474== Uninitialised value was created by a stack allocation ==4474== at 0x800005D2: main (memcheck-unit.c:3) ==4471==