Valgrind: Difference between revisions

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
imported>Frap
No edit summary
Put back latest macOS support, which has also been extended.
 
Line 1: Line 1:
{{short description|Programming tool for profiling, memory debugging and memory leak detection}}
{{short description|Programming tool for profiling, memory debugging and memory leak detection}}
{{about|software|the mythical gate|Valhalla}}
{{about|software|the mythical gate|Valhalla}}
{{Use dmy dates|date=October 2025}}
{{Infobox software
{{Infobox software
| logo = Valgrind logo.png
| logo = Valgrind logo.png
| logo caption = Valgrind logo
| logo caption = Valgrind logo
| logo_size = 200px
| logo_size = 200px
| logo_alt =  
| logo_alt =
| screenshot = <!-- Image name is enough -->
| screenshot = <!-- Image name is enough -->
| caption =  
| caption =
| screenshot_size =  
| screenshot_size =
| screenshot_alt =  
| screenshot_alt =
| collapsible =  
| collapsible =
| author = [[Julian Seward]], Nicholas Nethercote<ref>{{Cite web |title=AUTHORS |url=https://valgrind.org/docs/manual/dist.authors.html |access-date=2022-09-19 |website=valgrind.org}}</ref>
| author = [[Julian Seward]], Nicholas Nethercote<ref>{{Cite web |title=authors |url=https://valgrind.org/docs/manual/dist.authors.html |access-date=19 September 2022 |website=valgrind.org}}</ref>
| developer = Valgrind Development Team<ref>{{Cite web|url=https://valgrind.org/info/developers.html|title = Valgrind: The Developers}}</ref>
| developer = Valgrind Development Team<ref>{{Cite web |title=The Valgrind Developers |url=https://valgrind.org/info/developers.html |access-date=28 October 2025 |website=valgrind.org}}</ref>
| released = Jul 27, 2002<ref>{{Cite web |title=Twenty years of Valgrind |date=27 July 2022 |url=https://nnethercote.github.io/2022/07/27/twenty-years-of-valgrind.html |access-date=2023-08-04 }}</ref>
| released = 27 July 2002<ref>{{Cite web |last=Nethercote |first=Nicholas |date=27 July 2022 |title=Twenty years of Valgrind |url=https://nnethercote.github.io/2022/07/27/twenty-years-of-valgrind.html |access-date=4 August 2023 |website=nnethercote.github.io}}</ref>
| discontinued =  
| discontinued =
| latest release version =  
| latest release version = {{wikidata|property|preferred|references|edit|Q924041|P348|P548=Q2804309}}
| latest release date = <!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} -->
| latest release date = {{wikidata|qualifier|preferred|single|Q924041|P348|P548=Q2804309|P577}}
| latest preview version =  
| latest preview version =
| latest preview date = <!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} -->
| latest preview date = <!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} -->
| status =  
| status =
| programming language = [[C (programming language)|C]]
| programming language = [[C (programming language)|C]]
| operating_system = [[Linux]]<br/>{{nowrap|[[FreeBSD]]}}<br/>{{nowrap|[[Solaris (operating system)|Solaris]]}}<br/>[[Android (operating system)|Android]]<ref name="relnotes">[https://valgrind.org/docs/manual/dist.news.html Valgrind release notes]</ref>
| operating_system = [[Linux]]<br />{{nowrap|[[FreeBSD]]}}<br />{{nowrap|[[Solaris (operating system)|Solaris]]}}<br />{{nowrap|[[macOS]]}}<br />[[Android (operating system)|Android]]<ref name="os and platform">{{Cite mailing list |last=Wielaard |first=Mark |title=Valgrind-3.26.0 is available |mailing-list=Valgrind-announce |date=25 November 2025 |url=https://sourceforge.net/p/valgrind/mailman/message/59251046/ |access-date=19 December 2025 |quote=This release supports X86/Linux, AMD64/Linux, ARM32/Linux, ARM64/Linux, PPC32/Linux, PPC64BE/Linux, PPC64LE/Linux, S390X/Linux, MIPS32/Linux, MIPS64/Linux, RISCV64/Linux, ARM/Android, ARM64/Android, MIPS32/Android, X86/Android, X86/Solaris, AMD64/Solaris, AMD64/MacOSX 10.12, X86/FreeBSD, AMD64/FreeBSD and ARM64/FreeBSD There is also preliminary support for X86/macOS 10.13, AMD64/macOS 10.13 and nanoMIPS/Linux.}}</ref>
| platform = [[x86]], [[x86-64]], ARM, MIPS, POWER, [[RISC-V]], s390x
| platform = [[x86]], [[x86-64]], ARM, MIPS, POWER, [[RISC-V]], s390x<ref name="os and platform" />
| size =  
| size =
| genre = [[Profiler (computer science)|Profiler]], [[Memory debugger]]
| genre = [[Profiler (computer science)|Profiler]], [[Memory debugger]]
| license = [[GNU General Public License]]
| license = [[GNU General Public License]] version 3
| website = {{URL|https://www.valgrind.org/}}
| website = {{URL|https://www.valgrind.org/}}
}}
}}
'''Valgrind''' ({{IPAc-en|ˈ|v|æ|l|ɡ|r|ɪ|n|d}})<ref>{{Cite web |title=Valgrind |url=https://valgrind.org/docs/manual/faq.html#faq.pronounce |access-date=2023-05-04 |website=valgrind.org}}</ref> is a [[programming tool]] for [[memory debugger|memory debugging]], [[memory leak]] detection, and [[profiling (computer programming)|profiling]].
'''Valgrind''' ({{IPAc-en|ˈ|v|æ|l|ɡ|r|ɪ|n|d}})<ref>{{Cite web |title=Valgrind Frequently Asked Questions |url=https://valgrind.org/docs/manual/faq.html#faq.pronounce |access-date=4 May 2023 |website=valgrind.org |at=1.1 How do you pronounce "Valgrind"? |quote=The "Val" as in the word "value". The "grind" is pronounced with a short 'i' -- ie. "grinned" (rhymes with "tinned") rather than "grined" (rhymes with "find").}}</ref> is a [[programming tool]] for [[memory debugger|memory debugging]], [[memory leak]] detection, and [[profiling (computer programming)|profiling]].


Valgrind was originally designed to be a [[Free software|freely licensed]] memory debugging tool for [[Linux]] on [[x86]], but has since evolved to become a generic framework for creating dynamic analysis tools such as checkers and profilers.
Valgrind was originally designed to be a [[Free software|freely licensed]] memory debugging tool for [[Linux]] on [[x86]], but has since evolved to become a generic framework for creating dynamic analysis tools such as checkers and profilers.
Line 35: Line 36:
Valgrind is in essence a [[virtual machine]] using [[just-in-time compilation]] techniques, including [[dynamic recompilation]]. Nothing from the original program ever gets run directly on the host [[Central processing unit|processor]]. Instead, Valgrind first translates the program into a temporary, simpler form called [[intermediate representation]] (IR), which is a processor-neutral, [[static single assignment form]]-based form. After the conversion, a tool (see below) is free to do whatever transformations it would like on the IR, before Valgrind translates the IR back into machine code and lets the host processor run it. Valgrind recompiles [[binary code]] to run on host and target (or simulated) CPUs of the same architecture. It also includes a [[GNU Debugger|GDB]] stub to allow debugging of the target program as it runs in Valgrind, with "monitor commands" that allow querying the Valgrind tool for various information.
Valgrind is in essence a [[virtual machine]] using [[just-in-time compilation]] techniques, including [[dynamic recompilation]]. Nothing from the original program ever gets run directly on the host [[Central processing unit|processor]]. Instead, Valgrind first translates the program into a temporary, simpler form called [[intermediate representation]] (IR), which is a processor-neutral, [[static single assignment form]]-based form. After the conversion, a tool (see below) is free to do whatever transformations it would like on the IR, before Valgrind translates the IR back into machine code and lets the host processor run it. Valgrind recompiles [[binary code]] to run on host and target (or simulated) CPUs of the same architecture. It also includes a [[GNU Debugger|GDB]] stub to allow debugging of the target program as it runs in Valgrind, with "monitor commands" that allow querying the Valgrind tool for various information.


A considerable amount of performance is lost in these transformations (and usually, the code the tool inserts); usually, code run with Valgrind and the "none" tool (which does nothing to the IR) runs at 20% to 25% of the speed of the normal program.<ref>[https://valgrind.org/info/about.html Valgrind homepage]</ref><ref>[https://valgrind.org/docs/manual/valgrind_manual.pdf Valgrind Manual]</ref>
A considerable amount of performance is lost in these transformations (and usually, the code the tool inserts); usually, code run with Valgrind and the "none" tool (which does nothing to the IR) runs at 20% to 25% of the speed of the normal program.<ref>{{Cite web |title=About Valgrind |url=https://valgrind.org/info/about.html |access-date=28 October 2025 |website=valgrind.org}}</ref><ref>{{Cite web |title=Valgrind Manual |url=https://valgrind.org/docs/manual/valgrind_manual.pdf |website=valgrind.org}}</ref>


==Tools==
==Tools==
Line 43: Line 44:
There are multiple tools included with Valgrind (and several external ones). The default (and most used) tool is ''Memcheck''. Memcheck inserts extra [[instrumentation (computer programming)|instrumentation]] code around almost all instructions, which keeps track of the ''validity'' (all unallocated memory starts as invalid or "undefined", until it is initialized into a deterministic state, possibly from other memory) and ''addressability'' (whether the memory address in question points to an allocated, non-freed memory block), stored in the so-called ''V bits'' and ''A bits'' respectively. As data is moved around or manipulated, the instrumentation code keeps track of the A and V bits, so they are always correct on a single-bit level.
There are multiple tools included with Valgrind (and several external ones). The default (and most used) tool is ''Memcheck''. Memcheck inserts extra [[instrumentation (computer programming)|instrumentation]] code around almost all instructions, which keeps track of the ''validity'' (all unallocated memory starts as invalid or "undefined", until it is initialized into a deterministic state, possibly from other memory) and ''addressability'' (whether the memory address in question points to an allocated, non-freed memory block), stored in the so-called ''V bits'' and ''A bits'' respectively. As data is moved around or manipulated, the instrumentation code keeps track of the A and V bits, so they are always correct on a single-bit level.


In addition, Memcheck replaces the standard [https://en.cppreference.com/w/cpp/memory/new/operator_new C++ allocators] and [[C dynamic memory allocation|C memory allocator]] with its own implementation, which also includes ''memory guards'' around all allocated blocks (with the A bits set to "invalid"). This feature enables Memcheck to detect [[off-by-one error]]s where a program reads or writes outside an allocated block by a small amount. The problems Memcheck can detect and warn about include the following:
In addition, Memcheck replaces the standard [[new and delete (C++)|C++ allocators]] and [[C dynamic memory allocation|C memory allocator]] with its own implementation, which also includes ''memory guards'' around all allocated blocks (with the A bits set to "invalid"). This feature enables Memcheck to detect [[off-by-one error]]s where a program reads or writes outside an allocated block by a small amount. The problems Memcheck can detect and warn about include the following:
* Reading uninitialized memory
* Reading uninitialized memory
* Reading/writing invalid memory which may be
* Reading/writing invalid memory which may be
Line 60: Line 61:
* Use of <code>realloc</code> with a size of zero
* Use of <code>realloc</code> with a size of zero


The price of this is lost performance. Programs running under Memcheck usually run 20–30 times slower<ref>{{Cite web|url=https://valgrind.org/docs/manual/quick-start.html#quick-start.mcrun|title = Valgrind}}</ref> than running outside Valgrind and use more memory (there is a memory penalty per allocation). Thus, few developers run their code under Memcheck (or any other Valgrind tool) all the time. They most commonly use such tools either to trace down some specific bug, or to verify that there are no latent bugs (of the kind Memcheck can detect) in the code.
The price of this is lost performance. Programs running under Memcheck usually run 20–30 times slower<ref>{{Cite web |title=The Valgrind Quick Start Guide |url=https://valgrind.org/docs/manual/quick-start.html#quick-start.mcrun |access-date=28 October 2025 |at=3. Running your program under Memcheck}}</ref> than running outside Valgrind and use more memory (there is a memory penalty per allocation). Thus, few developers run their code under Memcheck (or any other Valgrind tool) all the time. They most commonly use such tools either to trace down some specific bug, or to verify that there are no latent bugs (of the kind Memcheck can detect) in the code.


=== Core errors ===
=== Core errors ===
Part of the core of Valgrind always has to perform some checking on [[file descriptor]]s (for instance to prevent the test executable from affecting Valgrind's log file or other output files). Checking can also be done for more general user errors affecting file descriptors. The kinds of errors that are detected are
Part of the core of Valgrind always has to perform some checking on [[file descriptor]]s (for instance to prevent the test executable from affecting Valgrind's log file or other output files). Checking can also be done for more general user errors affecting file descriptors. The kinds of errors that are detected are
* closing a file descriptor that is not open
* closing a file descriptor that is not open
* file descriptors that are not closed when the test executable exits
* file descriptors that are not closed when the test executable exits
Line 74: Line 74:


===Other tools===
===Other tools===
In addition to Memcheck, Valgrind has several other tools:<ref>[https://valgrind.org/info/tools.html Valgrind main tool list]</ref>
In addition to Memcheck, Valgrind has several other tools:<ref>{{Cite web |title=Valgrind's Tool Suite |url=https://valgrind.org/info/tools.html |access-date=28 October 2025 |website=valgrind.org}}</ref>
*''None'', runs the code in the virtual machine without performing any analysis and thus has the smallest possible CPU and memory overhead of all tools. Since Valgrind itself provides a trace back from a [[segmentation fault]], the ''none'' tool provides this traceback at minimal overhead.
* ''None'', runs the code in the virtual machine without performing any analysis and thus has the smallest possible CPU and memory overhead of all tools. Since Valgrind itself provides a trace back from a [[segmentation fault]], the ''none'' tool provides this traceback at minimal overhead.
*''Addrcheck'', similar to Memcheck but with much smaller CPU and memory overhead, thus catching fewer types of bugs. Addrcheck has been removed as of version 3.2.0.<ref>{{Cite web|url=https://valgrind.org/docs/manual/dist.news.old.html|title = Valgrind}}</ref>
* ''Addrcheck'', similar to Memcheck but with much smaller CPU and memory overhead, thus catching fewer types of bugs. Addrcheck has been removed as of version 3.2.0.<ref>{{Cite mailing list |last=Seward |first=Julian |title=Valgrind-3.2.0 for {x86, amd64, ppc32, ppc64}-linux is available |mailing-list=Valgrind-developers |date=8 June 2006 |url=https://sourceforge.net/p/valgrind/mailman/message/15487648/ |access-date=28 October 2025}}</ref>
*''Massif'', a [[Dynamic memory allocation|heap]] [[profiling (computer programming)|profiler]]. The separate [[Graphical user interface|GUI]] massif-visualizer visualizes output from Massif.
* ''Massif'', a [[Dynamic memory allocation|heap]] [[profiling (computer programming)|profiler]]. The separate [[Graphical user interface|GUI]] massif-visualizer visualizes output from Massif.
*''Helgrind'' and ''DRD'', detect [[race condition]]s in [[thread (computing)|multithreaded code]]
* ''Helgrind'' and ''DRD'', detect [[race condition]]s in [[thread (computing)|multithreaded code]]
*''Cachegrind'', a [[CPU cache|cache]] profiler. The separate GUI KCacheGrind visualizes output from Cachegrind.
* ''Cachegrind'', a [[CPU cache|cache]] profiler. The separate GUI KCacheGrind visualizes output from Cachegrind.
*''Callgrind'', a [[call graph]] analyzer created by Josef Weidendorfer, added to Valgrind as of version 3.2.0. KCacheGrind can visualize output from Callgrind.
* ''Callgrind'', a [[call graph]] analyzer created by Josef Weidendorfer, added to Valgrind as of version 3.2.0. KCacheGrind can visualize output from Callgrind.
*''DHAT'', dynamic heap analysis tool which analyzes how much memory is allocated and for how long, as well as patterns of memory usage.
* ''DHAT'', dynamic heap analysis tool which analyzes how much memory is allocated and for how long, as well as patterns of memory usage.
*''exp-bbv'', a performance simulator that extrapolates performance from a small sample set.
* ''exp-bbv'', a performance simulator that extrapolates performance from a small sample set.
''exp-sgcheck'' (named ''exp-ptrcheck'' prior to version 3.7), was removed in version 3.16.0. It was an experimental tool to find stack and global array overrun errors, which Memcheck cannot find.
''exp-sgcheck'' (named ''exp-ptrcheck'' prior to version 3.7), was removed in version 3.16.0. It was an experimental tool to find stack and global array overrun errors, which Memcheck cannot find.


There are also several externally developed tools available. One such tool is ThreadSanitizer, another detector of [[race condition]]s.<ref>{{Cite web|url=https://valgrind.org/downloads/variants.html|title = Valgrind: Variants / Patches}}</ref><ref>K Serebryany, T Iskhodzhanov, ''[http://data-race-test.googlecode.com/files/ThreadSanitizer.pdf ThreadSanitizer–data race detection in practice]'', Proceedings of the Workshop on Binary Instrumentation and Applications WBIA'09</ref>
There are also several externally developed tools available. One such tool is ThreadSanitizer, another detector of [[race condition]]s.<ref>{{Cite web |title=Variants and Patches |url=https://valgrind.org/downloads/variants.html |access-date=28 October 2025 |website=valgrind.org}}</ref><ref>{{cite conference |last1=Serebryany |first1=Konstantin |last2=Iskhodzhanov |first2=Timur |date=2009 |title=ThreadSanitizer–data race detection in practice |url=http://data-race-test.googlecode.com/files/ThreadSanitizer.pdf |conference=Proceedings of the Workshop on Binary Instrumentation and Applications WBIA |archive-url=https://web.archive.org/web/20110711111515/http://data-race-test.googlecode.com/files/ThreadSanitizer.pdf |archive-date=11 July 2011 |access-date=28 October 2025 |url-status=dead}}</ref>


==Platforms supported==
==Platforms supported==
As of version 3.4.0, Valgrind supports [[Linux]] on [[x86]], [[x86-64]] and [[PowerPC]]. Support for Linux on [[ARM architecture family|ARMv7]] (used for example in certain [[smartphone]]s) was added in version 3.6.0.<ref>[http://talk.maemo.org/showthread.php?t=65335 ARM/Linux port]</ref> Support for [[Solaris (operating system)|Solaris]] was added in version 3.11.0.<ref name="relnotes" /> Support for {{nowrap|[[macOS|OS X]]}} was added in version 3.5.0.<ref>[http://blog.mozilla.com/nnethercote/2009/05/28/mac-os-x-now-supported-on-the-valgrind-trunk/ OS X port]</ref> Support for [[FreeBSD]] x86 and amd64 was added in version 3.18.0. Support for FreeBSD aarch64 was added in version 3.23.0. There are unofficial ports to other [[Unix-like]] platforms (like [[OpenBSD]],<ref>[http://ports.su/devel/valgrind Valgrind OpenBSD port]</ref> [[NetBSD]]<ref>{{Cite web |url=http://vg4nbsd.berlios.de/ |title=Valgrind NetBSD port |access-date=2006-01-28 |archive-url=https://web.archive.org/web/20060209063503/http://vg4nbsd.berlios.de/ |archive-date=2006-02-09 |url-status=dead }}</ref> and QNX<ref>{{Cite web |title=foundry27 : View Release |url=https://community.qnx.com/sf/frs/do/viewRelease/projects.valgrind/frs.valgrind.valgrind_3_10;jsessionid=OEBZSSQUfLf5gV+W-ht7wdVW.tf |access-date=2024-05-24 |website=community.qnx.com}}</ref>). From version 3.7.0 the ARM/[[Android (operating system)|Android]] platform support was added.<ref name="relnotes" />
As of version 3.4.0, Valgrind supports [[Linux]] on [[x86]], [[x86-64]] and [[PowerPC]].<ref>{{Cite mailing list |last=Seward |first=Julian |title=Valgrind-3.4.0 is available |mailing-list=Valgrind-announce |date=3 January 2009 |url=https://sourceforge.net/p/valgrind/mailman/message/21236083/ |access-date=28 October 2025}}</ref> Support for Linux on [[ARM architecture family|ARMv7]] (used for example in certain [[smartphone]]s) was added in version 3.6.0.<ref>{{Cite web |author=lma |date=9 November 2010 |title=Valgrind 3.6.0 supports ARMv7 |url=https://talk.maemo.org/showthread.php?t=65335 |access-date=28 October 2025 |website=talk.maemo.org}}</ref><ref>{{Cite mailing list |last=Seward |first=Julian |title=Valgrind-3.6.0 and Valkyrie-2.0.0 are available |mailing-list=Valgrind-announce |date=21 October 2010 |url=https://sourceforge.net/p/valgrind/mailman/message/26469254/ |access-date=28 October 2025}}</ref> From version 3.7.0 the ARM/[[Android (operating system)|Android]] platform support was added.<ref>{{Cite mailing list |last=Seward |first=Julian |title=Valgrind-3.7.0 is available |mailing-list=Valgrind-announce |date=7 November 2011 |url=https://sourceforge.net/p/valgrind/mailman/message/28363976/ |access-date=28 October 2025}}</ref> Support for [[Solaris (operating system)|Solaris]] was added in version 3.11.0.<ref>{{Cite mailing list |last=Seward |first=Julian |title=Valgrind-3.11.0 is available |date=23 September 2015 |url=https://sourceforge.net/p/valgrind/mailman/message/34484105/ |access-date=28 October 2025}}</ref> Support for {{nowrap|[[macOS|OS X]]}} was added in version 3.5.0.<ref>{{Cite web |last=Nethercote |first=Nicholas |date=28 May 2009 |title=Mac OS X now supported on the Valgrind trunk |url=https://blog.mozilla.org/nnethercote/2009/05/28/mac-os-x-now-supported-on-the-valgrind-trunk/ |access-date=28 October 2025 |website=blog.mozilla.org}}</ref> Support for [[FreeBSD]] x86 and amd64 was added in version 3.18.0.<ref>{{Cite mailing list |last=Seward |first=Julian |title=Valgrind-3.18.1 is available |mailing-list=Valgrind-announce |date=18 November 2021 |url=https://sourceforge.net/p/valgrind/mailman/message/37386446/ |access-date=28 October 2025}}</ref> Support for FreeBSD aarch64 was added in version 3.23.0.<ref>{{Cite mailing list |last=Wielaard |first=Mark |title=Valgrind-3.23.0 is available |mailing-list=Valgrind-announce |date=26 April 2024 |url=https://sourceforge.net/p/valgrind/mailman/message/58765284/ |access-date=28 October 2025}}</ref>


Since version 3.9.0 there is support for Linux on [[MIPS architecture#MIPS32/MIPS64|MIPS64]] little and big endian, for MIPS DSP ASE on [[MIPS32]], for [[s390x]] Decimal Floating Point instructions, for [[POWER8]] ([[Power ISA#Power ISA v.2.07|Power ISA 2.07]]) instructions, for Intel [[Advanced Vector Extensions|AVX2]] instructions, for Intel Transactional Synchronization Extensions, both RTM and HLE and initial support for Hardware Transactional Memory on POWER.<ref name="Valgrind News" />
Since version 3.9.0 there is support for Linux on [[MIPS architecture#MIPS32/MIPS64|MIPS64]] little and big endian, for MIPS DSP ASE on [[MIPS32]], for [[s390x]] Decimal Floating Point instructions, for [[POWER8]] ([[Power ISA#Power ISA v.2.07|Power ISA 2.07]]) instructions, for Intel [[Advanced Vector Extensions|AVX2]] instructions, for Intel Transactional Synchronization Extensions, both RTM and HLE and initial support for Hardware Transactional Memory on POWER.<ref>{{Cite mailing list |last=Seward |first=Julian |title=Valgrind-3.9.0 is available |mailing-list=Valgrind-announce |date=1 November 2013 |url=https://sourceforge.net/p/valgrind/mailman/message/31588025/ |access-date=28 October 2025}}</ref>


[[RISC-V]] since version 3.25.0.<ref name="relnotes" />
[[RISC-V]] 64bit since version 3.25.0.<ref>{{Cite mailing list |last=Wielaard |first=Mark |title=Valgrind-3.25.0 is available |mailing-list=Valgrind-announce |date=25 April 2025 |url=https://sourceforge.net/p/valgrind/mailman/message/59177043/ |access-date=28 October 2025}}</ref>
 
Support for macOS 10.13 was improved and support for macOS 10.14<ref>{{Cite web |title=Thread: [Valgrind-developers] [valgrind] Darwin: add macOS 10.14 support {{!}} Valgrind, an open-source memory debugger |url=https://sourceforge.net/p/valgrind/mailman/valgrind-developers/thread/20251209130832.546E34BA540C@sourceware.org/#msg59270654 |access-date=2025-12-20 |website=sourceforge.net}}</ref>, 10.15<ref>{{Cite web |title=[Valgrind-developers] [valgrind] Darwin: add macOS 10.15 support {{!}} Valgrind, an open-source memory debugger |url=https://sourceforge.net/p/valgrind/mailman/message/59273959/ |access-date=2025-12-20 |website=sourceforge.net}}</ref> and 11<ref>{{Cite web |title=[Valgrind-developers] [valgrind] Bug 513533 - Support macOS 11.0 (Big Sur) {{!}} Valgrind, an open-source memory debugger |url=https://sourceforge.net/p/valgrind/mailman/message/59275073/ |access-date=2025-12-20 |website=sourceforge.net}}</ref> were added during the development of Valgrind 3.27.
 
There are unofficial ports to other [[Unix-like]] platforms (like [[OpenBSD]],<ref>{{Cite web |title=devel/valgrind |url=http://ports.su/devel/valgrind |access-date=28 October 2025 |website=ports.su}}</ref> [[NetBSD]]<ref>{{Cite web |title=Valgrind NetBSD port |url=http://vg4nbsd.berlios.de/ |url-status=dead |archive-url=https://web.archive.org/web/20060209063503/http://vg4nbsd.berlios.de/ |archive-date=9 February 2006 |access-date=28 January 2006 |website=vg4nbsd.berlios.de}}</ref> and QNX<ref>{{Cite web |title=Release: rel9580 |url=https://community.qnx.com/sf/frs/do/viewRelease/projects.valgrind/frs.valgrind.valgrind_3_10;jsessionid=OEBZSSQUfLf5gV+W-ht7wdVW.tf |access-date=24 May 2024 |website=community.qnx.com}}</ref>).


== History and development ==
== History and development ==


The name Valgrind refers to the main entrance to [[Valhalla]] in [[Norse mythology]].<ref>[http://www.valgrind.org/docs/manual/faq.html#faq.whence Valgrind FAQ]</ref><ref>{{cite web| url=https://www.voluspa.org/grimnismal21-25.htm| title=Grímnismál| website=Völuspá.org}}</ref> During development (before release) the project was named [[Heimdall]]; however, the name would have conflicted with a security package.
The name Valgrind refers to the main entrance to [[Valhalla]] in [[Norse mythology]].<ref name="whence">{{Cite web |title=Valgrind |url=http://www.valgrind.org/docs/manual/faq.html#faq.whence |access-date=28 October 2025 |website=valgrind.org |at=1.2. Where does the name "Valgrind" come from? |quote=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).}}</ref><ref>{{cite web |title=Grímnismál |url=https://www.voluspa.org/grimnismal21-25.htm |access-date=28 October 2025 |website=Völuspá.org}}</ref> During development (before release) the project was named [[Heimdall]]; however, the name would have conflicted with a security package.<ref name="whence" />


The original author of Valgrind is [[Julian Seward]], who in 2006 won a [[Google-O'Reilly Open Source Award]] for his work on Valgrind.<ref>[https://valgrind.org/gallery/awards.html valgrind.org's list of awards]</ref><ref>[http://code.google.com/events/osa-hall-of-fame.html Google-O'Reilly Open Source Awards – Hall of Fame]</ref>
The original author of Valgrind is [[Julian Seward]], who in 2006 won a [[Google-O'Reilly Open Source Award]] for his work on Valgrind.<ref>{{Cite web |title=Awards |url=https://valgrind.org/gallery/awards.html |access-date=28 October 2025 |website=valgrind.org}}</ref><ref>{{Cite web |title=Google-O'Reilly Open Source Awards – Hall of Fame {{!}} Open Source Programs Office |url=https://developers.google.com/open-source/osa |access-date=28 October 2025 |website=Google for Developers |language=en}}</ref>


Several others have also made significant contributions, including Nicholas Nethercote, Bart Van Assche, Florian Krohm, Tom Hughes, Philippe Waroquiers, Mark Wielaard, Paul Floyd, Petar Jovanovic, Carl Love, Cerion Armour-Brown and Ivo Raisr.<ref>[https://valgrind.org/info/developers.html The Valgrind Developers]</ref>
Several others have also made significant contributions, including Nicholas Nethercote, Bart Van Assche, Florian Krohm, Tom Hughes, Philippe Waroquiers, Mark Wielaard, Paul Floyd, Petar Jovanovic, Carl Love, Cerion Armour-Brown and Ivo Raisr.<ref>{{Cite web |title=The Valgrind Developers |url=https://valgrind.org/info/developers.html |access-date=28 October 2025 |website=valgrind.org}}</ref>


It is used by a number of Linux-based projects.<ref>[https://valgrind.org/gallery/users.html valgrind.org's list of users]</ref>
It is used by a number of Linux-based projects.<ref>{{Cite web |title=Projects Using Valgrind |url=https://valgrind.org/gallery/users.html |access-date=28 October 2025 |website=valgrind.org}}</ref>


==Limitations of Memcheck==
==Limitations of Memcheck==
In addition to the performance penalty, an important limitation of Memcheck is its inability to detect all cases of bounds errors in the use of static or stack-allocated data.<ref>[http://www.valgrind.org/docs/manual/faq.html#faq.overruns Valgrind FAQ]</ref> The following code will pass the ''Memcheck'' tool in Valgrind without incident, despite containing the errors described in the comments:
In addition to the performance penalty, an important limitation of Memcheck is its inability to detect all cases of bounds errors in the use of static or stack-allocated data.<ref>{{Cite web |title=Valgrind Frequently Asked Questions |url=http://www.valgrind.org/docs/manual/faq.html#faq.overruns |access-date=28 October 2025 |website=valgrind.org |at=4.6. Why doesn't Memcheck find the array overruns in this program?}}</ref> The following code will pass the ''Memcheck'' tool in Valgrind without incident, despite containing the errors described in the comments:


<syntaxhighlight lang="c">
<syntaxhighlight lang="c">
Line 116: Line 120:
   Static[5] = 0;  /* Error - Static[0] to Static[4] exist, Static[5] is out of bounds */
   Static[5] = 0;  /* Error - Static[0] to Static[4] exist, Static[5] is out of bounds */
   Stack [5] = 0;  /* Error - Stack[0] to Stack[4] exist, Stack[5] is out of bounds */
   Stack [5] = 0;  /* Error - Stack[0] to Stack[4] exist, Stack[5] is out of bounds */
 
 
   return 0;
   return 0;
}
}
Line 137: Line 141:


==References==
==References==
* {{cite journal | title = Valgrind: A framework for heavyweight dynamic binary instrumentation |first1=Nicholas|last1=Nethercote |first2=Julian|last2=Seward| journal = ACM SIGPLAN Notices |date=10 June 2007 |volume=42 |issue=6 |pages=89–100 | publisher = ACM |doi=10.1145/1273442.1250746 | url =
* {{cite journal |last1=Nethercote |first1=Nicholas |last2=Seward |first2=Julian |date=10 June 2007 |title=Valgrind: A framework for heavyweight dynamic binary instrumentation |url=http://portal.acm.org/citation.cfm?id=1250734.1250746&coll=GUIDE&dl=GUIDE&CFID=8869850&CFTOKEN=10168503 |journal=ACM SIGPLAN Notices |publisher=ACM |volume=42 |issue=6 |pages=89–100 |doi=10.1145/1273442.1250746 |url-access=subscription}}
http://portal.acm.org/citation.cfm?id=1250734.1250746&coll=GUIDE&dl=GUIDE&CFID=8869850&CFTOKEN=10168503 |url-access=subscription }}
* {{cite journal |last1=Seward |first1=Julian |last2=Nethercote |first2=Nicholas |date=10 April 2005 |title=Using Valgrind to detect undefined value errors with bit-precision |url=http://portal.acm.org/citation.cfm?id=1247362&dl=GUIDE&coll=GUIDE&CFID=8869850&CFTOKEN=10168503 |journal=Proceedings of the USENIX Annual Technical Conference 2005 |publisher=USENIX Association |page=2}}<!-- doi=10.5555/1247360.1247362 raise error -->
* {{cite journal | title = Using Valgrind to detect undefined value errors with bit-precision |first1=Julian|last1=Seward |first2=Nicholas|last2=Nethercote | journal = Proceedings of the USENIX Annual Technical Conference 2005 |date=10 April 2005|page=2| publisher = USENIX Association | url = http://portal.acm.org/citation.cfm?id=1247362&dl=GUIDE&coll=GUIDE&CFID=8869850&CFTOKEN=10168503 }}
* {{cite book |last1=Seward |first1=Julian |url=http://www.network-theory.co.uk/valgrind/manual/ |title=Valgrind 3.3 – Advanced Debugging and Profiling for GNU/Linux applications |last2=Nethercote |first2=Nicholas |last3=Weidendorfer |first3=Josef |author4=Valgrind Development Team |date=March 2008 |publisher=Network Theory Ltd |isbn=978-0-9546120-5-4 |pages=164 pages |oclc=506228360 |ol=26010839M |access-date=25 June 2008 |archive-url=https://web.archive.org/web/20130818131906/http://www.network-theory.co.uk/valgrind/manual/ |archive-date=18 August 2013 |url-status=dead}}
* {{cite book
  | first1 = J. | last1 = Seward
  | first2 = N. | last2 = Nethercote
  | first3 = J. | last3 = Weidendorfer
  | author4 = Valgrind Development Team
  | title = Valgrind 3.3 – Advanced Debugging and Profiling for GNU/Linux applications
  | publisher = Network Theory Ltd
  | date = March 2008
  | pages = 164 pages
  | url = http://www.network-theory.co.uk/valgrind/manual/
  | isbn = 978-0-9546120-5-4 }}


==External links==
== External links ==
{{Wikibooks|Valgrind}}
{{Wikibooks|Valgrind}}
* {{Official website|http://www.valgrind.org/}}
* {{Official website|http://www.valgrind.org/}}
{{Linux}}


[[Category:Debuggers]]
[[Category:Debuggers]]

Latest revision as of 15:17, 20 December 2025

Template:Short description Script error: No such module "about". Template:Use dmy dates Script error: No such module "Infobox".Template:Template other Script error: No such module "Check for unknown parameters".Script error: No such module "Check for conflicting parameters". Valgrind (Template:IPAc-en)[1] is a programming tool for memory debugging, memory leak detection, and profiling.

Valgrind was originally designed to be a freely licensed memory debugging tool for Linux on x86, but has since evolved to become a generic framework for creating dynamic analysis tools such as checkers and profilers.

Overview

Valgrind is in essence a virtual machine using just-in-time compilation techniques, including dynamic recompilation. Nothing from the original program ever gets run directly on the host processor. Instead, Valgrind first translates the program into a temporary, simpler form called intermediate representation (IR), which is a processor-neutral, static single assignment form-based form. After the conversion, a tool (see below) is free to do whatever transformations it would like on the IR, before Valgrind translates the IR back into machine code and lets the host processor run it. Valgrind recompiles binary code to run on host and target (or simulated) CPUs of the same architecture. It also includes a GDB stub to allow debugging of the target program as it runs in Valgrind, with "monitor commands" that allow querying the Valgrind tool for various information.

A considerable amount of performance is lost in these transformations (and usually, the code the tool inserts); usually, code run with Valgrind and the "none" tool (which does nothing to the IR) runs at 20% to 25% of the speed of the normal program.[2][3]

Tools

Script error: No such module "Labelled list hatnote".

Memcheck

There are multiple tools included with Valgrind (and several external ones). The default (and most used) tool is Memcheck. Memcheck inserts extra instrumentation code around almost all instructions, which keeps track of the validity (all unallocated memory starts as invalid or "undefined", until it is initialized into a deterministic state, possibly from other memory) and addressability (whether the memory address in question points to an allocated, non-freed memory block), stored in the so-called V bits and A bits respectively. As data is moved around or manipulated, the instrumentation code keeps track of the A and V bits, so they are always correct on a single-bit level.

In addition, Memcheck replaces the standard C++ allocators and C memory allocator with its own implementation, which also includes memory guards around all allocated blocks (with the A bits set to "invalid"). This feature enables Memcheck to detect off-by-one errors where a program reads or writes outside an allocated block by a small amount. The problems Memcheck can detect and warn about include the following:

  • Reading uninitialized memory
  • Reading/writing invalid memory which may be
    • memory that has been free'd
    • memory outside of malloc'd blocks
    • memory below the stack pointer
  • Use of incorrect parameters for system calls
  • Unsafe overlapping memory copies with mem* and str* functions
  • Memory leaks
  • Mismatched allocations and deallocations which may be
    • mixing C and C++ e.g., malloc and delete
    • mixing scalar and array e.g., new and delete[]
    • sized deallocation not the same size as allocation
    • aligned deallocation not the same alignment as allocation
  • Use of incorrect alignment
  • Use of realloc with a size of zero

The price of this is lost performance. Programs running under Memcheck usually run 20–30 times slower[4] than running outside Valgrind and use more memory (there is a memory penalty per allocation). Thus, few developers run their code under Memcheck (or any other Valgrind tool) all the time. They most commonly use such tools either to trace down some specific bug, or to verify that there are no latent bugs (of the kind Memcheck can detect) in the code.

Core errors

Part of the core of Valgrind always has to perform some checking on file descriptors (for instance to prevent the test executable from affecting Valgrind's log file or other output files). Checking can also be done for more general user errors affecting file descriptors. The kinds of errors that are detected are

  • closing a file descriptor that is not open
  • file descriptors that are not closed when the test executable exits
  • use of a file descriptor that was never created or was closed already

Starting with Valgrind 3.24 these errors are handled by Valgrind in the same way as other errors. That means that you can generate and use suppressions with them.

Valgrind 3.25 added a feature where you can change the behaviour of functions that create file descriptors. The default behaviour is the same as POSIX, which will return the lowest available file descriptor, potentially recycling closed file descriptors. There is the risk that the test executable will accidentally and erroneously use such a recycled file descriptor. Valgrind's --modify-fds option changes the behaviour to no longer respect the POSIX standard. Instead it will try to create a new file descriptor for each request.

Other tools

In addition to Memcheck, Valgrind has several other tools:[5]

  • None, runs the code in the virtual machine without performing any analysis and thus has the smallest possible CPU and memory overhead of all tools. Since Valgrind itself provides a trace back from a segmentation fault, the none tool provides this traceback at minimal overhead.
  • Addrcheck, similar to Memcheck but with much smaller CPU and memory overhead, thus catching fewer types of bugs. Addrcheck has been removed as of version 3.2.0.[6]
  • Massif, a heap profiler. The separate GUI massif-visualizer visualizes output from Massif.
  • Helgrind and DRD, detect race conditions in multithreaded code
  • Cachegrind, a cache profiler. The separate GUI KCacheGrind visualizes output from Cachegrind.
  • Callgrind, a call graph analyzer created by Josef Weidendorfer, added to Valgrind as of version 3.2.0. KCacheGrind can visualize output from Callgrind.
  • DHAT, dynamic heap analysis tool which analyzes how much memory is allocated and for how long, as well as patterns of memory usage.
  • exp-bbv, a performance simulator that extrapolates performance from a small sample set.

exp-sgcheck (named exp-ptrcheck prior to version 3.7), was removed in version 3.16.0. It was an experimental tool to find stack and global array overrun errors, which Memcheck cannot find.

There are also several externally developed tools available. One such tool is ThreadSanitizer, another detector of race conditions.[7][8]

Platforms supported

As of version 3.4.0, Valgrind supports Linux on x86, x86-64 and PowerPC.[9] Support for Linux on ARMv7 (used for example in certain smartphones) was added in version 3.6.0.[10][11] From version 3.7.0 the ARM/Android platform support was added.[12] Support for Solaris was added in version 3.11.0.[13] Support for OS X was added in version 3.5.0.[14] Support for FreeBSD x86 and amd64 was added in version 3.18.0.[15] Support for FreeBSD aarch64 was added in version 3.23.0.[16]

Since version 3.9.0 there is support for Linux on MIPS64 little and big endian, for MIPS DSP ASE on MIPS32, for s390x Decimal Floating Point instructions, for POWER8 (Power ISA 2.07) instructions, for Intel AVX2 instructions, for Intel Transactional Synchronization Extensions, both RTM and HLE and initial support for Hardware Transactional Memory on POWER.[17]

RISC-V 64bit since version 3.25.0.[18]

Support for macOS 10.13 was improved and support for macOS 10.14[19], 10.15[20] and 11[21] were added during the development of Valgrind 3.27.

There are unofficial ports to other Unix-like platforms (like OpenBSD,[22] NetBSD[23] and QNX[24]).

History and development

The name Valgrind refers to the main entrance to Valhalla in Norse mythology.[25][26] During development (before release) the project was named Heimdall; however, the name would have conflicted with a security package.[25]

The original author of Valgrind is Julian Seward, who in 2006 won a Google-O'Reilly Open Source Award for his work on Valgrind.[27][28]

Several others have also made significant contributions, including Nicholas Nethercote, Bart Van Assche, Florian Krohm, Tom Hughes, Philippe Waroquiers, Mark Wielaard, Paul Floyd, Petar Jovanovic, Carl Love, Cerion Armour-Brown and Ivo Raisr.[29]

It is used by a number of Linux-based projects.[30]

Limitations of Memcheck

In addition to the performance penalty, an important limitation of Memcheck is its inability to detect all cases of bounds errors in the use of static or stack-allocated data.[31] The following code will pass the Memcheck tool in Valgrind without incident, despite containing the errors described in the comments:

int Static[5];

int func(void)
{
  int Stack[5];

  Static[5] = 0;  /* Error - Static[0] to Static[4] exist, Static[5] is out of bounds */
  Stack [5] = 0;  /* Error - Stack[0] to Stack[4] exist, Stack[5] is out of bounds */

  return 0;
}

The inability to detect all errors involving the access of stack allocated data is especially noteworthy since certain types of stack errors make software vulnerable to the classic stack smashing exploit.

See also

Script error: No such module "Portal".

Notes

<templatestyles src="Reflist/styles.css" />

  1. Script error: No such module "citation/CS1".
  2. Script error: No such module "citation/CS1".
  3. Script error: No such module "citation/CS1".
  4. Script error: No such module "citation/CS1".
  5. Script error: No such module "citation/CS1".
  6. Script error: No such module "citation/CS1".
  7. Script error: No such module "citation/CS1".
  8. Script error: No such module "citation/CS1".
  9. Script error: No such module "citation/CS1".
  10. Script error: No such module "citation/CS1".
  11. Script error: No such module "citation/CS1".
  12. Script error: No such module "citation/CS1".
  13. Script error: No such module "citation/CS1".
  14. Script error: No such module "citation/CS1".
  15. Script error: No such module "citation/CS1".
  16. Script error: No such module "citation/CS1".
  17. Script error: No such module "citation/CS1".
  18. Script error: No such module "citation/CS1".
  19. Script error: No such module "citation/CS1".
  20. Script error: No such module "citation/CS1".
  21. Script error: No such module "citation/CS1".
  22. Script error: No such module "citation/CS1".
  23. Script error: No such module "citation/CS1".
  24. Script error: No such module "citation/CS1".
  25. a b Script error: No such module "citation/CS1".
  26. Script error: No such module "citation/CS1".
  27. Script error: No such module "citation/CS1".
  28. Script error: No such module "citation/CS1".
  29. Script error: No such module "citation/CS1".
  30. Script error: No such module "citation/CS1".
  31. Script error: No such module "citation/CS1".

Script error: No such module "Check for unknown parameters".

References

  • Script error: No such module "Citation/CS1".
  • Script error: No such module "Citation/CS1".
  • Script error: No such module "citation/CS1".

External links

Template:Sister project

  • Script error: No such module "Official website".Script error: No such module "Check for unknown parameters".