You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
16 KiB
HTML

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE html>
<html lang="en-US" dir="ltr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Valgrind # Valgrind is a suite of tools designed to provide debugging and profiling tools to make your programs more correct and detect some runtime issues (“4. Memcheck: A Memory Error Detector,” #ref-valgrind). The most used of these tools is Memcheck, which can detect many memory-related errors that are common in C and C&#43;&#43; programs and that can lead to crashes and unpredictable behavior (for example, unfreed memory buffers). To run Valgrind on your program:">
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#ffffff">
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#343a40">
<meta name="color-scheme" content="light dark"><meta property="og:title" content="Valgrind" />
<meta property="og:description" content="Valgrind # Valgrind is a suite of tools designed to provide debugging and profiling tools to make your programs more correct and detect some runtime issues (“4. Memcheck: A Memory Error Detector,” #ref-valgrind). The most used of these tools is Memcheck, which can detect many memory-related errors that are common in C and C&#43;&#43; programs and that can lead to crashes and unpredictable behavior (for example, unfreed memory buffers). To run Valgrind on your program:" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://doc.cyub.vip/cs341/en/chap2/Valgrind/" /><meta property="article:section" content="chap2" />
<title>Valgrind | System Programming</title>
<link rel="manifest" href="/cs341/manifest.json">
<link rel="icon" href="/cs341/favicon.png" >
<link rel="stylesheet" href="/cs341/book.min.0356a007b16d20385fd8a4fa025ec8be3122d726acbce111d6af7545fd9a6d9a.css" integrity="sha256-A1agB7FtIDhf2KT6Al7IvjEi1yasvOER1q91Rf2abZo=" crossorigin="anonymous">
<script defer src="/cs341/flexsearch.min.js"></script>
<script defer src="/cs341/en.search.min.25e65b45853c7470253af707592ba1bc73827661c6424245ee2246bfa49316c8.js" integrity="sha256-JeZbRYU8dHAlOvcHWSuhvHOCdmHGQkJF7iJGv6STFsg=" crossorigin="anonymous"></script>
<script defer src="/cs341/sw.min.26457b123904e37c0084a63054d7f37a2a820ad60cda19209104bfc0b2c496b0.js" integrity="sha256-JkV7EjkE43wAhKYwVNfzeiqCCtYM2hkgkQS/wLLElrA=" crossorigin="anonymous"></script>
<!--
Made with Book Theme
https://github.com/alex-shpak/hugo-book
-->
</head>
<body dir="ltr">
<input type="checkbox" class="hidden toggle" id="menu-control" />
<input type="checkbox" class="hidden toggle" id="toc-control" />
<main class="container flex">
<aside class="book-menu">
<div class="book-menu-content">
<nav>
<h2 class="book-brand">
<a class="flex align-center" href="/cs341/en/"><span>System Programming</span>
</a>
</h2>
<div class="book-search">
<input type="text" id="book-search-input" placeholder="Search" aria-label="Search" maxlength="64" data-hotkeys="s/" />
<div class="book-search-spinner hidden"></div>
<ul id="book-search-results"></ul>
</div>
<ul class="book-languages">
<li>
<input type="checkbox" id="languages" class="toggle" />
<label for="languages" class="flex justify-between">
<a role="button" class="flex align-center">
<img src="/cs341/svg/translate.svg" class="book-icon" alt="Languages" />
English
</a>
</label>
<ul>
<li>
<a href="https://doc.cyub.vip/cs341/cn/">
chinese
</a>
</li>
</ul>
</li>
</ul>
<ul>
<li>
<p><strong>
<a href="/cs341/en/chap1/">1. Introduction</a></strong></p>
<ul>
<li>
<a href="/cs341/en/chap1/Authors/">Authors</a></li>
</ul>
</li>
<li>
<p><strong>
<a href="/cs341/en/chap2/">2. Background</a></strong></p>
<ul>
<li>
<a href="/cs341/en/chap2/Systems_Architecture/">Systems Architecture</a></li>
<li>
<a href="/cs341/en/chap2/Debugging_Environments/">Debugging and Environments</a></li>
<li>
<a href="/cs341/en/chap2/Valgrind/"class=active>Valgrind</a></li>
<li>
<a href="/cs341/en/chap2/GDB/">GDB</a></li>
<li>
<a href="/cs341/en/chap2/Homework0/">Homework 0</a></li>
</ul>
</li>
<li>
<p><strong>
<a href="/cs341/en/chap3/">3. The C Programming Language</a></strong></p>
</li>
<li>
<p><strong>
<a href="/cs341/en/chap4/">4. Processes</a></strong></p>
</li>
<li>
<p><strong>
<a href="/cs341/en/chap5/">5. Memory Allocators</a></strong></p>
</li>
<li>
<p><strong>
<a href="/cs341/en/chap6/">6. Threads</a></strong></p>
</li>
<li>
<p><strong>
<a href="/cs341/en/chap7/">7. Synchronization</a></strong></p>
<ul>
<li>
<a href="/cs341/en/chap7/Mutex/">Mutex</a></li>
<li>
<a href="/cs341/en/chap7/Semaphore/">Semaphore</a></li>
<li>
<a href="/cs341/en/chap7/Condition_Variables/">Condition Variables</a></li>
</ul>
</li>
</ul>
</nav>
<script>(function(){var e=document.querySelector("aside .book-menu-content");addEventListener("beforeunload",function(){localStorage.setItem("menu.scrollTop",e.scrollTop)}),e.scrollTop=localStorage.getItem("menu.scrollTop")})()</script>
</div>
</aside>
<div class="book-page">
<header class="book-header">
<div class="flex align-center justify-between">
<label for="menu-control">
<img src="/cs341/svg/menu.svg" class="book-icon" alt="Menu" />
</label>
<strong>Valgrind</strong>
<label for="toc-control">
</label>
</div>
</header>
<article class="markdown"><h1 id="valgrind">
Valgrind
<a class="anchor" href="#valgrind">#</a>
</h1>
<p>Valgrind is a suite of tools designed to provide debugging and profiling tools to make your programs more correct and detect some runtime issues (“4. Memcheck: A Memory Error Detector,” #ref-valgrind). The most used of these tools is Memcheck, which can detect many memory-related errors that are common in C and C++ programs and that can lead to crashes and unpredictable behavior (for example, unfreed memory buffers). To run Valgrind on your program:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>valgrind --leak-check<span style="color:#f92672">=</span>full --show-leak-kinds<span style="color:#f92672">=</span>all myprogram arg1 arg2
</span></span></code></pre></div><p>Arguments are optional and the default tool that will run is Memcheck. The output will be presented in the form: the number of allocations, frees, and errors. Suppose we have a simple program like this:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdlib.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">void</span> <span style="color:#a6e22e">dummy_function</span>() {
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">int</span><span style="color:#f92672">*</span> x <span style="color:#f92672">=</span> <span style="color:#a6e22e">malloc</span>(<span style="color:#ae81ff">10</span> <span style="color:#f92672">*</span> <span style="color:#66d9ef">sizeof</span>(<span style="color:#66d9ef">int</span>));
</span></span><span style="display:flex;"><span> x[<span style="color:#ae81ff">10</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>; <span style="color:#75715e">// error 1: Out of bounds write, as you can see here we write to an out of bound memory address.
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>} <span style="color:#75715e">// error 2: Memory Leak, x is allocated at function exit.
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>(<span style="color:#66d9ef">void</span>) {
</span></span><span style="display:flex;"><span> <span style="color:#a6e22e">dummy_function</span>();
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>This program compiles and runs with no errors. Lets see what Valgrind will output.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> Memcheck, a memory error detector
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> Copyright <span style="color:#f92672">(</span>C<span style="color:#f92672">)</span> 2002-2015, and GNU GPL<span style="color:#e6db74">&#39;d, by Julian Seward et al.
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">==29515== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">==29515== Command: ./a
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">==29515==
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">==29515== Invalid write of size 4
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">==29515== at 0x400544: dummy_function (in /home/rafi/projects/exocpp/a)
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">==29515== by 0x40055A: main (in /home/rafi/projects/exocpp/a)
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">==29515== Address 0x5203068 is 0 bytes after a block of size 40 alloc&#39;</span>d
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> at 0x4C2DB8F: malloc <span style="color:#f92672">(</span>in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so<span style="color:#f92672">)</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> by 0x400537: dummy_function <span style="color:#f92672">(</span>in /home/rafi/projects/exocpp/a<span style="color:#f92672">)</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> by 0x40055A: main <span style="color:#f92672">(</span>in /home/rafi/projects/exocpp/a<span style="color:#f92672">)</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> HEAP SUMMARY:
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> in use at exit: <span style="color:#ae81ff">40</span> bytes in <span style="color:#ae81ff">1</span> blocks
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> total heap usage: <span style="color:#ae81ff">1</span> allocs, <span style="color:#ae81ff">0</span> frees, <span style="color:#ae81ff">40</span> bytes allocated
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> LEAK SUMMARY:
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> definitely lost: <span style="color:#ae81ff">40</span> bytes in <span style="color:#ae81ff">1</span> blocks
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> indirectly lost: <span style="color:#ae81ff">0</span> bytes in <span style="color:#ae81ff">0</span> blocks
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> possibly lost: <span style="color:#ae81ff">0</span> bytes in <span style="color:#ae81ff">0</span> blocks
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> still reachable: <span style="color:#ae81ff">0</span> bytes in <span style="color:#ae81ff">0</span> blocks
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> suppressed: <span style="color:#ae81ff">0</span> bytes in <span style="color:#ae81ff">0</span> blocks
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> Rerun with --leak-check<span style="color:#f92672">=</span>full to see details of leaked memory
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> For counts of detected and suppressed errors, rerun with: -v
</span></span><span style="display:flex;"><span><span style="color:#f92672">==</span>29515<span style="color:#f92672">==</span> ERROR SUMMARY: <span style="color:#ae81ff">1</span> errors from <span style="color:#ae81ff">1</span> contexts <span style="color:#f92672">(</span>suppressed: <span style="color:#ae81ff">0</span> from 0<span style="color:#f92672">)</span>
</span></span></code></pre></div><p><strong>Invalid write:</strong> It detected our heap block overrun, writing outside of an allocated block.</p>
<p><strong>Definitely lost:</strong> Memory leak — you probably forgot to free a memory block.</p>
<p>Valgrind is a effective tool to check for errors at runtime. C is special when it comes to such behavior, so after compiling your program you can use Valgrind to fix errors that your compiler may miss and that usually happens when your program is running.</p>
<p>For more information, you can refer to the manual<sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup></p>
<div class="footnotes" role="doc-endnotes">
<hr>
<ol>
<li id="fn:1">
<p>“4. Memcheck: A Memory Error Detector.” n.d. <em>Valgrind</em>.
<a href="http://valgrind.org/docs/manual/mc-manual.html">http://valgrind.org/docs/manual/mc-manual.html</a>.&#160;<a href="#fnref:1" class="footnote-backref" role="doc-backlink">&#x21a9;&#xfe0e;</a></p>
</li>
</ol>
</div>
</article>
<footer class="book-footer">
<div class="flex flex-wrap justify-between">
</div>
<script>(function(){function e(e){const t=window.getSelection(),n=document.createRange();n.selectNodeContents(e),t.removeAllRanges(),t.addRange(n)}document.querySelectorAll("pre code").forEach(t=>{t.addEventListener("click",function(){if(window.getSelection().toString())return;e(t.parentElement),navigator.clipboard&&navigator.clipboard.writeText(t.parentElement.textContent)})})})()</script>
</footer>
<div class="book-comments">
</div>
<label for="menu-control" class="hidden book-menu-overlay"></label>
</div>
</main>
</body>
</html>