sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | <html> |
| 2 | <head> |
| 3 | <style type="text/css"> |
| 4 | body { background-color: #ffffff; |
| 5 | color: #000000; |
| 6 | font-family: Times, Helvetica, Arial; |
| 7 | font-size: 14pt} |
| 8 | h4 { margin-bottom: 0.3em} |
| 9 | code { color: #000000; |
| 10 | font-family: Courier; |
| 11 | font-size: 13pt } |
| 12 | pre { color: #000000; |
| 13 | font-family: Courier; |
| 14 | font-size: 13pt } |
| 15 | a:link { color: #0000C0; |
| 16 | text-decoration: none; } |
| 17 | a:visited { color: #0000C0; |
| 18 | text-decoration: none; } |
| 19 | a:active { color: #0000C0; |
| 20 | text-decoration: none; } |
| 21 | </style> |
| 22 | </head> |
| 23 | |
| 24 | <body bgcolor="#ffffff"> |
| 25 | |
| 26 | <a name="title"> </a> |
sewardj | a7dc795 | 2002-03-24 11:29:13 +0000 | [diff] [blame] | 27 | <h1 align=center>Valgrind, snapshot 20020324</h1> |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 28 | |
| 29 | <center> |
| 30 | <a href="mailto:jseward@acm.org">jseward@acm.org<br> |
| 31 | <a href="http://www.muraroa.demon.co.uk">http://www.muraroa.demon.co.uk</a><br> |
| 32 | Copyright © 2000-2002 Julian Seward |
| 33 | <p> |
| 34 | Valgrind is licensed under the GNU General Public License, |
| 35 | version 2<br> |
| 36 | An open-source tool for finding memory-management problems in |
| 37 | Linux-x86 executables. |
| 38 | </center> |
| 39 | |
| 40 | <p> |
| 41 | |
| 42 | <hr width="100%"> |
| 43 | <a name="contents"></a> |
| 44 | <h2>Contents of this manual</h2> |
| 45 | |
| 46 | <h4>1 <a href="#intro">Introduction</a></h4> |
| 47 | 1.1 <a href="#whatfor">What Valgrind is for</a><br> |
| 48 | 1.2 <a href="#whatdoes">What it does with your program</a> |
| 49 | |
| 50 | <h4>2 <a href="#howtouse">How to use it, and how to make sense |
| 51 | of the results</a></h4> |
| 52 | 2.1 <a href="#starta">Getting started</a><br> |
| 53 | 2.2 <a href="#comment">The commentary</a><br> |
| 54 | 2.3 <a href="#report">Reporting of errors</a><br> |
| 55 | 2.4 <a href="#suppress">Suppressing errors</a><br> |
| 56 | 2.5 <a href="#flags">Command-line flags</a><br> |
| 57 | 2.6 <a href="#errormsgs">Explaination of error messages</a><br> |
| 58 | 2.7 <a href="#suppfiles">Writing suppressions files</a><br> |
| 59 | 2.8 <a href="#install">Building and installing</a><br> |
| 60 | 2.9 <a href="#problems">If you have problems</a><br> |
| 61 | |
| 62 | <h4>3 <a href="#machine">Details of the checking machinery</a></h4> |
| 63 | 3.1 <a href="#vvalue">Valid-value (V) bits</a><br> |
| 64 | 3.2 <a href="#vaddress">Valid-address (A) bits</a><br> |
| 65 | 3.3 <a href="#together">Putting it all together</a><br> |
| 66 | 3.4 <a href="#signals">Signals</a><br> |
| 67 | 3.5 <a href="#leaks">Memory leak detection</a><br> |
| 68 | |
| 69 | <h4>4 <a href="#limits">Limitations</a></h4> |
| 70 | |
| 71 | <h4>5 <a href="#howitworks">How it works -- a rough overview</a></h4> |
| 72 | 5.1 <a href="#startb">Getting started</a><br> |
| 73 | 5.2 <a href="#engine">The translation/instrumentation engine</a><br> |
| 74 | 5.3 <a href="#track">Tracking the status of memory</a><br> |
| 75 | 5.4 <a href="#sys_calls">System calls</a><br> |
| 76 | 5.5 <a href="#sys_signals">Signals</a><br> |
| 77 | |
| 78 | <h4>6 <a href="#example">An example</a></h4> |
| 79 | |
| 80 | <h4>7 <a href="techdocs.html">The design and implementation of Valgrind</a></h4> |
| 81 | |
| 82 | <hr width="100%"> |
| 83 | |
| 84 | <a name="intro"></a> |
| 85 | <h2>1 Introduction</h2> |
| 86 | |
| 87 | <a name="whatfor"></a> |
| 88 | <h3>1.1 What Valgrind is for</h3> |
| 89 | |
| 90 | Valgrind is a tool to help you find memory-management problems in your |
| 91 | programs. When a program is run under Valgrind's supervision, all |
| 92 | reads and writes of memory are checked, and calls to |
| 93 | malloc/new/free/delete are intercepted. As a result, Valgrind can |
| 94 | detect problems such as: |
| 95 | <ul> |
| 96 | <li>Use of uninitialised memory</li> |
| 97 | <li>Reading/writing memory after it has been free'd</li> |
| 98 | <li>Reading/writing off the end of malloc'd blocks</li> |
| 99 | <li>Reading/writing inappropriate areas on the stack</li> |
| 100 | <li>Memory leaks -- where pointers to malloc'd blocks are lost forever</li> |
| 101 | </ul> |
| 102 | |
| 103 | Problems like these can be difficult to find by other means, often |
| 104 | lying undetected for long periods, then causing occasional, |
| 105 | difficult-to-diagnose crashes. |
| 106 | |
| 107 | <p> |
| 108 | Valgrind is closely tied to details of the CPU, operating system and |
| 109 | to a less extent, compiler and basic C libraries. This makes it |
| 110 | difficult to make it portable, so I have chosen at the outset to |
| 111 | concentrate on what I believe to be a widely used platform: Red Hat |
| 112 | Linux 7.2, on x86s. I believe that it will work without significant |
| 113 | difficulty on other x86 GNU/Linux systems which use the 2.4 kernel and |
| 114 | GNU libc 2.2.X, for example SuSE 7.1 and Mandrake 8.0. Red Hat 6.2 is |
| 115 | also supported. It has worked in the past, and probably still does, |
| 116 | on RedHat 7.1 and 6.2. Note that I haven't compiled it on RedHat 7.1 |
| 117 | and 6.2 for a while, so they may no longer work now. |
| 118 | <p> |
| 119 | (Early Feb 02: after feedback from the KDE people it also works better |
| 120 | on other Linuxes). |
| 121 | <p> |
| 122 | At some point in the past, Valgrind has also worked on Red Hat 6.2 |
| 123 | (x86), thanks to the efforts of Rob Noble. |
| 124 | |
| 125 | <p> |
| 126 | Valgrind is licensed under the GNU General Public License, version |
| 127 | 2. Read the file LICENSE in the source distribution for details. |
| 128 | |
| 129 | <a name="whatdoes"> |
| 130 | <h3>1.2 What it does with your program</h3> |
| 131 | |
| 132 | Valgrind is designed to be as non-intrusive as possible. It works |
| 133 | directly with existing executables. You don't need to recompile, |
| 134 | relink, or otherwise modify, the program to be checked. Simply place |
| 135 | the word <code>valgrind</code> at the start of the command line |
| 136 | normally used to run the program. So, for example, if you want to run |
| 137 | the command <code>ls -l</code> on Valgrind, simply issue the |
| 138 | command: <code>valgrind ls -l</code>. |
| 139 | |
| 140 | <p>Valgrind takes control of your program before it starts. Debugging |
| 141 | information is read from the executable and associated libraries, so |
| 142 | that error messages can be phrased in terms of source code |
| 143 | locations. Your program is then run on a synthetic x86 CPU which |
| 144 | checks every memory access. All detected errors are written to a |
| 145 | log. When the program finishes, Valgrind searches for and reports on |
| 146 | leaked memory. |
| 147 | |
| 148 | <p>You can run pretty much any dynamically linked ELF x86 executable using |
| 149 | Valgrind. Programs run 25 to 50 times slower, and take a lot more |
| 150 | memory, than they usually would. It works well enough to run large |
| 151 | programs. For example, the Konqueror web browser from the KDE Desktop |
| 152 | Environment, version 2.1.1, runs slowly but usably on Valgrind. |
| 153 | |
| 154 | <p>Valgrind simulates every single instruction your program executes. |
| 155 | Because of this, it finds errors not only in your application but also |
| 156 | in all supporting dynamically-linked (.so-format) libraries, including |
| 157 | the GNU C library, the X client libraries, Qt, if you work with KDE, and |
| 158 | so on. That often includes libraries, for example the GNU C library, |
| 159 | which contain memory access violations, but which you cannot or do not |
| 160 | want to fix. |
| 161 | |
| 162 | <p>Rather than swamping you with errors in which you are not |
| 163 | interested, Valgrind allows you to selectively suppress errors, by |
| 164 | recording them in a suppressions file which is read when Valgrind |
| 165 | starts up. As supplied, Valgrind comes with a suppressions file |
| 166 | designed to give reasonable behaviour on Red Hat 7.2 (also 7.1 and |
| 167 | 6.2) when running text-only and simple X applications. |
| 168 | |
| 169 | <p><a href="#example">Section 6</a> shows an example of use. |
| 170 | <p> |
| 171 | <hr width="100%"> |
| 172 | |
| 173 | <a name="howtouse"></a> |
| 174 | <h2>2 How to use it, and how to make sense of the results</h2> |
| 175 | |
| 176 | <a name="starta"></a> |
| 177 | <h3>2.1 Getting started</h3> |
| 178 | |
| 179 | First off, consider whether it might be beneficial to recompile your |
| 180 | application and supporting libraries with optimisation disabled and |
| 181 | debugging info enabled (the <code>-g</code> flag). You don't have to |
| 182 | do this, but doing so helps Valgrind produce more accurate and less |
| 183 | confusing error reports. Chances are you're set up like this already, |
| 184 | if you intended to debug your program with GNU gdb, or some other |
| 185 | debugger. |
| 186 | |
| 187 | <p>Then just run your application, but place the word |
| 188 | <code>valgrind</code> in front of your usual command-line invokation. |
| 189 | Note that you should run the real (machine-code) executable here. If |
| 190 | your application is started by, for example, a shell or perl script, |
| 191 | you'll need to modify it to invoke Valgrind on the real executables. |
| 192 | Running such scripts directly under Valgrind will result in you |
| 193 | getting error reports pertaining to <code>/bin/sh</code>, |
| 194 | <code>/usr/bin/perl</code>, or whatever interpreter you're using. |
| 195 | This almost certainly isn't what you want and can be hugely confusing. |
| 196 | |
| 197 | <a name="comment"></a> |
| 198 | <h3>2.2 The commentary</h3> |
| 199 | |
| 200 | Valgrind writes a commentary, detailing error reports and other |
| 201 | significant events. The commentary goes to standard output by |
| 202 | default. This may interfere with your program, so you can ask for it |
| 203 | to be directed elsewhere. |
| 204 | |
| 205 | <p>All lines in the commentary are of the following form:<br> |
| 206 | <pre> |
| 207 | ==12345== some-message-from-Valgrind |
| 208 | </pre> |
| 209 | <p>The <code>12345</code> is the process ID. This scheme makes it easy |
| 210 | to distinguish program output from Valgrind commentary, and also easy |
| 211 | to differentiate commentaries from different processes which have |
| 212 | become merged together, for whatever reason. |
| 213 | |
| 214 | <p>By default, Valgrind writes only essential messages to the commentary, |
| 215 | so as to avoid flooding you with information of secondary importance. |
| 216 | If you want more information about what is happening, re-run, passing |
| 217 | the <code>-v</code> flag to Valgrind. |
| 218 | |
| 219 | |
| 220 | <a name="report"></a> |
| 221 | <h3>2.3 Reporting of errors</h3> |
| 222 | |
| 223 | When Valgrind detects something bad happening in the program, an error |
| 224 | message is written to the commentary. For example:<br> |
| 225 | <pre> |
| 226 | ==25832== Invalid read of size 4 |
| 227 | ==25832== at 0x8048724: BandMatrix::ReSize(int, int, int) (bogon.cpp:45) |
| 228 | ==25832== by 0x80487AF: main (bogon.cpp:66) |
| 229 | ==25832== by 0x40371E5E: __libc_start_main (libc-start.c:129) |
| 230 | ==25832== by 0x80485D1: (within /home/sewardj/newmat10/bogon) |
| 231 | ==25832== Address 0xBFFFF74C is not stack'd, malloc'd or free'd |
| 232 | </pre> |
| 233 | |
| 234 | <p>This message says that the program did an illegal 4-byte read of |
| 235 | address 0xBFFFF74C, which, as far as it can tell, is not a valid stack |
| 236 | address, nor corresponds to any currently malloc'd or free'd blocks. |
| 237 | The read is happening at line 45 of <code>bogon.cpp</code>, called |
| 238 | from line 66 of the same file, etc. For errors associated with an |
| 239 | identified malloc'd/free'd block, for example reading free'd memory, |
| 240 | Valgrind reports not only the location where the error happened, but |
| 241 | also where the associated block was malloc'd/free'd. |
| 242 | |
| 243 | <p>Valgrind remembers all error reports. When an error is detected, |
| 244 | it is compared against old reports, to see if it is a duplicate. If |
| 245 | so, the error is noted, but no further commentary is emitted. This |
| 246 | avoids you being swamped with bazillions of duplicate error reports. |
| 247 | |
| 248 | <p>If you want to know how many times each error occurred, run with |
| 249 | the <code>-v</code> option. When execution finishes, all the reports |
| 250 | are printed out, along with, and sorted by, their occurrence counts. |
| 251 | This makes it easy to see which errors have occurred most frequently. |
| 252 | |
| 253 | <p>Errors are reported before the associated operation actually |
| 254 | happens. For example, if you program decides to read from address |
| 255 | zero, Valgrind will emit a message to this effect, and the program |
| 256 | will then duly die with a segmentation fault. |
| 257 | |
| 258 | <p>In general, you should try and fix errors in the order that they |
| 259 | are reported. Not doing so can be confusing. For example, a program |
| 260 | which copies uninitialised values to several memory locations, and |
| 261 | later uses them, will generate several error messages. The first such |
| 262 | error message may well give the most direct clue to the root cause of |
| 263 | the problem. |
| 264 | |
| 265 | <a name="suppress"></a> |
| 266 | <h3>2.4 Suppressing errors</h3> |
| 267 | |
| 268 | Valgrind detects numerous problems in the base libraries, such as the |
| 269 | GNU C library, and the XFree86 client libraries, which come |
| 270 | pre-installed on your GNU/Linux system. You can't easily fix these, |
| 271 | but you don't want to see these errors (and yes, there are many!) So |
| 272 | Valgrind reads a list of errors to suppress at startup. By default |
| 273 | this file is <code>redhat72.supp</code>, located in the Valgrind |
| 274 | installation directory. |
| 275 | |
| 276 | <p>You can modify and add to the suppressions file at your leisure, or |
| 277 | write your own. Multiple suppression files are allowed. This is |
| 278 | useful if part of your project contains errors you can't or don't want |
| 279 | to fix, yet you don't want to continuously be reminded of them. |
| 280 | |
| 281 | <p>Each error to be suppressed is described very specifically, to |
| 282 | minimise the possibility that a suppression-directive inadvertantly |
| 283 | suppresses a bunch of similar errors which you did want to see. The |
| 284 | suppression mechanism is designed to allow precise yet flexible |
| 285 | specification of errors to suppress. |
| 286 | |
| 287 | <p>If you use the <code>-v</code> flag, at the end of execution, Valgrind |
| 288 | prints out one line for each used suppression, giving its name and the |
| 289 | number of times it got used. Here's the suppressions used by a run of |
| 290 | <code>ls -l</code>: |
| 291 | <pre> |
| 292 | --27579-- supp: 1 socketcall.connect(serv_addr)/__libc_connect/__nscd_getgrgid_r |
| 293 | --27579-- supp: 1 socketcall.connect(serv_addr)/__libc_connect/__nscd_getpwuid_r |
| 294 | --27579-- supp: 6 strrchr/_dl_map_object_from_fd/_dl_map_object |
| 295 | </pre> |
| 296 | |
| 297 | <a name="flags"></a> |
| 298 | <h3>2.5 Command-line flags</h3> |
| 299 | |
| 300 | You invoke Valgrind like this: |
| 301 | <pre> |
| 302 | valgrind [options-for-Valgrind] your-prog [options for your-prog] |
| 303 | </pre> |
| 304 | |
| 305 | <p>Valgrind's default settings succeed in giving reasonable behaviour |
| 306 | in most cases. Available options, in no particular order, are as |
| 307 | follows: |
| 308 | <ul> |
| 309 | <li><code>--help</code></li><br> |
| 310 | |
| 311 | <li><code>--version</code><br> |
| 312 | <p>The usual deal.</li><br><p> |
| 313 | |
| 314 | <li><code>-v --verbose</code><br> |
| 315 | <p>Be more verbose. Gives extra information on various aspects |
| 316 | of your program, such as: the shared objects loaded, the |
| 317 | suppressions used, the progress of the instrumentation engine, |
| 318 | and warnings about unusual behaviour. |
| 319 | </li><br><p> |
| 320 | |
| 321 | <li><code>-q --quiet</code><br> |
| 322 | <p>Run silently, and only print error messages. Useful if you |
| 323 | are running regression tests or have some other automated test |
| 324 | machinery. |
| 325 | </li><br><p> |
| 326 | |
| 327 | <li><code>--demangle=no</code><br> |
| 328 | <code>--demangle=yes</code> [the default] |
| 329 | <p>Disable/enable automatic demangling (decoding) of C++ names. |
| 330 | Enabled by default. When enabled, Valgrind will attempt to |
| 331 | translate encoded C++ procedure names back to something |
| 332 | approaching the original. The demangler handles symbols mangled |
| 333 | by g++ versions 2.X and 3.X. |
| 334 | |
| 335 | <p>An important fact about demangling is that function |
| 336 | names mentioned in suppressions files should be in their mangled |
| 337 | form. Valgrind does not demangle function names when searching |
| 338 | for applicable suppressions, because to do otherwise would make |
| 339 | suppressions file contents dependent on the state of Valgrind's |
| 340 | demangling machinery, and would also be slow and pointless. |
| 341 | </li><br><p> |
| 342 | |
| 343 | <li><code>--num-callers=<number></code> [default=4]<br> |
| 344 | <p>By default, Valgrind shows four levels of function call names |
| 345 | to help you identify program locations. You can change that |
| 346 | number with this option. This can help in determining the |
| 347 | program's location in deeply-nested call chains. Note that errors |
| 348 | are commoned up using only the top three function locations (the |
| 349 | place in the current function, and that of its two immediate |
| 350 | callers). So this doesn't affect the total number of errors |
| 351 | reported. |
| 352 | <p> |
| 353 | The maximum value for this is 50. Note that higher settings |
| 354 | will make Valgrind run a bit more slowly and take a bit more |
| 355 | memory, but can be useful when working with programs with |
| 356 | deeply-nested call chains. |
| 357 | </li><br><p> |
| 358 | |
| 359 | <li><code>--gdb-attach=no</code> [the default]<br> |
| 360 | <code>--gdb-attach=yes</code> |
| 361 | <p>When enabled, Valgrind will pause after every error shown, |
| 362 | and print the line |
| 363 | <br> |
| 364 | <code>---- Attach to GDB ? --- [Return/N/n/Y/y/C/c] ----</code> |
| 365 | <p> |
| 366 | Pressing <code>Ret</code>, or <code>N</code> <code>Ret</code> |
| 367 | or <code>n</code> <code>Ret</code>, causes Valgrind not to |
| 368 | start GDB for this error. |
| 369 | <p> |
| 370 | <code>Y</code> <code>Ret</code> |
| 371 | or <code>y</code> <code>Ret</code> causes Valgrind to |
| 372 | start GDB, for the program at this point. When you have |
| 373 | finished with GDB, quit from it, and the program will continue. |
| 374 | Trying to continue from inside GDB doesn't work. |
| 375 | <p> |
| 376 | <code>C</code> <code>Ret</code> |
| 377 | or <code>c</code> <code>Ret</code> causes Valgrind not to |
| 378 | start GDB, and not to ask again. |
| 379 | <p> |
| 380 | <code>--gdb-attach=yes</code> conflicts with |
| 381 | <code>--trace-children=yes</code>. You can't use them |
| 382 | together. Valgrind refuses to start up in this situation. |
| 383 | </li><br><p> |
| 384 | |
| 385 | <li><code>--partial-loads-ok=yes</code> [the default]<br> |
| 386 | <code>--partial-loads-ok=no</code> |
| 387 | <p>Controls how Valgrind handles word (4-byte) loads from |
| 388 | addresses for which some bytes are addressible and others |
| 389 | are not. When <code>yes</code> (the default), such loads |
| 390 | do not elicit an address error. Instead, the loaded V bytes |
| 391 | corresponding to the illegal addresses indicate undefined, and |
| 392 | those corresponding to legal addresses are loaded from shadow |
| 393 | memory, as usual. |
| 394 | <p> |
| 395 | When <code>no</code>, loads from partially |
| 396 | invalid addresses are treated the same as loads from completely |
| 397 | invalid addresses: an illegal-address error is issued, |
| 398 | and the resulting V bytes indicate valid data. |
| 399 | </li><br><p> |
| 400 | |
| 401 | <li><code>--sloppy-malloc=no</code> [the default]<br> |
| 402 | <code>--sloppy-malloc=yes</code> |
| 403 | <p>When enabled, all requests for malloc/calloc are rounded up |
| 404 | to a whole number of machine words -- in other words, made |
| 405 | divisible by 4. For example, a request for 17 bytes of space |
| 406 | would result in a 20-byte area being made available. This works |
| 407 | around bugs in sloppy libraries which assume that they can |
| 408 | safely rely on malloc/calloc requests being rounded up in this |
| 409 | fashion. Without the workaround, these libraries tend to |
| 410 | generate large numbers of errors when they access the ends of |
| 411 | these areas. Valgrind snapshots dated 17 Feb 2002 and later are |
| 412 | cleverer about this problem, and you should no longer need to |
| 413 | use this flag. |
| 414 | </li><br><p> |
| 415 | |
| 416 | <li><code>--trace-children=no</code> [the default]</br> |
| 417 | <code>--trace-children=yes</code> |
| 418 | <p>When enabled, Valgrind will trace into child processes. This |
| 419 | is confusing and usually not what you want, so is disabled by |
| 420 | default.</li><br><p> |
| 421 | |
| 422 | <li><code>--freelist-vol=<number></code> [default: 1000000] |
| 423 | <p>When the client program releases memory using free (in C) or |
| 424 | delete (C++), that memory is not immediately made available for |
| 425 | re-allocation. Instead it is marked inaccessible and placed in |
| 426 | a queue of freed blocks. The purpose is to delay the point at |
| 427 | which freed-up memory comes back into circulation. This |
| 428 | increases the chance that Valgrind will be able to detect |
| 429 | invalid accesses to blocks for some significant period of time |
| 430 | after they have been freed. |
| 431 | <p> |
| 432 | This flag specifies the maximum total size, in bytes, of the |
| 433 | blocks in the queue. The default value is one million bytes. |
| 434 | Increasing this increases the total amount of memory used by |
| 435 | Valgrind but may detect invalid uses of freed blocks which would |
| 436 | otherwise go undetected.</li><br><p> |
| 437 | |
| 438 | <li><code>--logfile-fd=<number></code> [default: 2, stderr] |
| 439 | <p>Specifies the file descriptor on which Valgrind communicates |
| 440 | all of its messages. The default, 2, is the standard error |
| 441 | channel. This may interfere with the client's own use of |
| 442 | stderr. To dump Valgrind's commentary in a file without using |
| 443 | stderr, something like the following works well (sh/bash |
| 444 | syntax):<br> |
| 445 | <code> |
| 446 | valgrind --logfile-fd=9 my_prog 9> logfile</code><br> |
| 447 | That is: tell Valgrind to send all output to file descriptor 9, |
| 448 | and ask the shell to route file descriptor 9 to "logfile". |
| 449 | </li><br><p> |
| 450 | |
| 451 | <li><code>--suppressions=<filename></code> [default: |
| 452 | /installation/directory/redhat72.supp] <p>Specifies an extra |
| 453 | file from which to read descriptions of errors to suppress. You |
| 454 | may use as many extra suppressions files as you |
| 455 | like.</li><br><p> |
| 456 | |
| 457 | <li><code>--leak-check=no</code> [default]<br> |
| 458 | <code>--leak-check=yes</code> |
| 459 | <p>When enabled, search for memory leaks when the client program |
| 460 | finishes. A memory leak means a malloc'd block, which has not |
| 461 | yet been free'd, but to which no pointer can be found. Such a |
| 462 | block can never be free'd by the program, since no pointer to it |
| 463 | exists. Leak checking is disabled by default |
| 464 | because it tends to generate dozens of error messages. |
| 465 | </li><br><p> |
| 466 | |
| 467 | <li><code>--show-reachable=no</code> [default]<br> |
| 468 | <code>--show-reachable=yes</code> <p>When disabled, the memory |
| 469 | leak detector only shows blocks for which it cannot find a |
| 470 | pointer to at all, or it can only find a pointer to the middle |
| 471 | of. These blocks are prime candidates for memory leaks. When |
| 472 | enabled, the leak detector also reports on blocks which it could |
| 473 | find a pointer to. Your program could, at least in principle, |
| 474 | have freed such blocks before exit. Contrast this to blocks for |
| 475 | which no pointer, or only an interior pointer could be found: |
| 476 | they are more likely to indicate memory leaks, because |
| 477 | you do not actually have a pointer to the start of the block |
| 478 | which you can hand to free(), even if you wanted to. |
| 479 | </li><br><p> |
| 480 | |
| 481 | <li><code>--leak-resolution=low</code> [default]<br> |
| 482 | <code>--leak-resolution=med</code> <br> |
| 483 | <code>--leak-resolution=high</code> |
| 484 | <p>When doing leak checking, determines how willing Valgrind is |
| 485 | to consider different backtraces the same. When set to |
| 486 | <code>low</code>, the default, only the first two entries need |
| 487 | match. When <code>med</code>, four entries have to match. When |
| 488 | <code>high</code>, all entries need to match. |
| 489 | <p> |
| 490 | For hardcore leak debugging, you probably want to use |
| 491 | <code>--leak-resolution=high</code> together with |
| 492 | <code>--num-callers=40</code> or some such large number. Note |
| 493 | however that this can give an overwhelming amount of |
| 494 | information, which is why the defaults are 4 callers and |
| 495 | low-resolution matching. |
| 496 | <p> |
| 497 | Note that the <code>--leak-resolution=</code> setting does not |
| 498 | affect Valgrind's ability to find leaks. It only changes how |
| 499 | the results are presented to you. |
| 500 | </li><br><p> |
| 501 | |
| 502 | <li><code>--workaround-gcc296-bugs=no</code> [default]<br> |
| 503 | <code>--workaround-gcc296-bugs=yes</code> <p>When enabled, |
| 504 | assume that reads and writes some small distance below the stack |
| 505 | pointer <code>%esp</code> are due to bugs in gcc 2.96, and does |
| 506 | not report them. The "small distance" is 256 bytes by default. |
| 507 | Note that gcc 2.96 is the default compiler on some popular Linux |
| 508 | distributions (RedHat 7.X, Mandrake) and so you may well need to |
| 509 | use this flag. Do not use it if you do not have to, as it can |
| 510 | cause real errors to be overlooked. A better option is to use a |
| 511 | gcc/g++ which works properly; 2.95.3 seems to be a good choice. |
| 512 | <p> |
| 513 | Unfortunately (27 Feb 02) it looks like g++ 3.0.4 is similarly |
| 514 | buggy, so you may need to issue this flag if you use 3.0.4. |
| 515 | </li><br><p> |
| 516 | |
| 517 | <li><code>--client-perms=no</code> [default]<br> |
| 518 | <code>--client-perms=yes</code> <p>An experimental feature. |
| 519 | <p> |
| 520 | When enabled, and when <code>--instrument=yes</code> (which is |
| 521 | the default), Valgrind honours client directives to set and |
| 522 | query address range permissions. This allows the client program |
| 523 | to tell Valgrind about changes in memory range permissions that |
| 524 | Valgrind would not otherwise know about, and so allows clients |
| 525 | to get Valgrind to do arbitrary custom checks. |
| 526 | <p> |
| 527 | Clients need to include the header file <code>valgrind.h</code> |
| 528 | to make this work. The macros therein have the magical property |
| 529 | that they generate code in-line which Valgrind can spot. |
| 530 | However, the code does nothing when not run on Valgrind, so you |
| 531 | are not forced to run your program on Valgrind just because you |
| 532 | use the macros in this file. |
| 533 | <p> |
| 534 | A brief description of the available macros: |
| 535 | <ul> |
| 536 | <li><code>VALGRIND_MAKE_NOACCESS</code>, |
| 537 | <code>VALGRIND_MAKE_WRITABLE</code> and |
| 538 | <code>VALGRIND_MAKE_READABLE</code>. These mark address |
| 539 | ranges as completely inaccessible, accessible but containing |
| 540 | undefined data, and accessible and containing defined data, |
| 541 | respectively. Subsequent errors may have their faulting |
| 542 | addresses described in terms of these blocks. Returns a |
| 543 | "block handle". |
| 544 | <p> |
| 545 | <li><code>VALGRIND_DISCARD</code>: At some point you may want |
| 546 | Valgrind to stop reporting errors in terms of the blocks |
| 547 | defined by the previous three macros. To do this, the above |
| 548 | macros return a small-integer "block handle". You can pass |
| 549 | this block handle to <code>VALGRIND_DISCARD</code>. After |
| 550 | doing so, Valgrind will no longer be able to relate |
| 551 | addressing errors to the user-defined block associated with |
| 552 | the handle. The permissions settings associated with the |
| 553 | handle remain in place; this just affects how errors are |
| 554 | reported, not whether they are reported. Returns 1 for an |
| 555 | invalid handle and 0 for a valid handle (although passing |
| 556 | invalid handles is harmless). |
| 557 | <p> |
| 558 | <li><code>VALGRIND_CHECK_NOACCESS</code>, |
| 559 | <code>VALGRIND_CHECK_WRITABLE</code> and |
| 560 | <code>VALGRIND_CHECK_READABLE</code>: check immediately |
| 561 | whether or not the given address range has the relevant |
| 562 | property, and if not, print an error message. Also, for the |
| 563 | convenience of the client, returns zero if the relevant |
| 564 | property holds; otherwise, the returned value is the address |
| 565 | of the first byte for which the property is not true. |
| 566 | <p> |
| 567 | <li><code>VALGRIND_CHECK_NOACCESS</code>: a quick and easy way |
| 568 | to find out whether Valgrind thinks a particular variable |
| 569 | (lvalue, to be precise) is addressible and defined. Prints |
| 570 | an error message if not. Returns no value. |
| 571 | <p> |
| 572 | <li><code>VALGRIND_MAKE_NOACCESS_STACK</code>: a highly |
| 573 | experimental feature. Similarly to |
| 574 | <code>VALGRIND_MAKE_NOACCESS</code>, this marks an address |
| 575 | range as inaccessible, so that subsequent accesses to an |
| 576 | address in the range gives an error. However, this macro |
| 577 | does not return a block handle. Instead, all annotations |
| 578 | created like this are reviewed at each client |
| 579 | <code>ret</code> (subroutine return) instruction, and those |
| 580 | which now define an address range block the client's stack |
| 581 | pointer register (<code>%esp</code>) are automatically |
| 582 | deleted. |
| 583 | <p> |
| 584 | In other words, this macro allows the client to tell |
| 585 | Valgrind about red-zones on its own stack. Valgrind |
| 586 | automatically discards this information when the stack |
| 587 | retreats past such blocks. Beware: hacky and flaky. |
| 588 | </ul> |
| 589 | </li> |
| 590 | <p> |
| 591 | As of 17 March 02 (the time of writing this), there is a small |
| 592 | problem with all of these macros, which is that I haven't |
| 593 | figured out how to make them produce sensible (always-succeeds) |
| 594 | return values when the client is run on the real CPU or on |
| 595 | Valgrind without <code>--client-perms=yes</code>. So if you |
| 596 | write client code which depends on the return values, be aware |
| 597 | that it may misbehave when not run with full Valgrindification. |
| 598 | If you always ignore the return values you should always be |
| 599 | safe. I plan to fix this. |
| 600 | </ul> |
| 601 | |
| 602 | There are also some options for debugging Valgrind itself. You |
| 603 | shouldn't need to use them in the normal run of things. Nevertheless: |
| 604 | |
| 605 | <ul> |
| 606 | |
| 607 | <li><code>--single-step=no</code> [default]<br> |
| 608 | <code>--single-step=yes</code> |
| 609 | <p>When enabled, each x86 insn is translated seperately into |
| 610 | instrumented code. When disabled, translation is done on a |
| 611 | per-basic-block basis, giving much better translations.</li><br> |
| 612 | <p> |
| 613 | |
| 614 | <li><code>--optimise=no</code><br> |
| 615 | <code>--optimise=yes</code> [default] |
| 616 | <p>When enabled, various improvements are applied to the |
| 617 | intermediate code, mainly aimed at allowing the simulated CPU's |
| 618 | registers to be cached in the real CPU's registers over several |
| 619 | simulated instructions.</li><br> |
| 620 | <p> |
| 621 | |
| 622 | <li><code>--instrument=no</code><br> |
| 623 | <code>--instrument=yes</code> [default] |
| 624 | <p>When disabled, the translations don't actually contain any |
| 625 | instrumentation.</li><br> |
| 626 | <p> |
| 627 | |
| 628 | <li><code>--cleanup=no</code><br> |
| 629 | <code>--cleanup=yes</code> [default] |
| 630 | <p>When enabled, various improvments are applied to the |
| 631 | post-instrumented intermediate code, aimed at removing redundant |
| 632 | value checks.</li><br> |
| 633 | <p> |
| 634 | |
| 635 | <li><code>--trace-syscalls=no</code> [default]<br> |
| 636 | <code>--trace-syscalls=yes</code> |
| 637 | <p>Enable/disable tracing of system call intercepts.</li><br> |
| 638 | <p> |
| 639 | |
| 640 | <li><code>--trace-signals=no</code> [default]<br> |
| 641 | <code>--trace-signals=yes</code> |
| 642 | <p>Enable/disable tracing of signal handling.</li><br> |
| 643 | <p> |
| 644 | |
| 645 | <li><code>--trace-symtab=no</code> [default]<br> |
| 646 | <code>--trace-symtab=yes</code> |
| 647 | <p>Enable/disable tracing of symbol table reading.</li><br> |
| 648 | <p> |
| 649 | |
| 650 | <li><code>--trace-malloc=no</code> [default]<br> |
| 651 | <code>--trace-malloc=yes</code> |
| 652 | <p>Enable/disable tracing of malloc/free (et al) intercepts. |
| 653 | </li><br> |
| 654 | <p> |
| 655 | |
| 656 | <li><code>--stop-after=<number></code> |
| 657 | [default: infinity, more or less] |
| 658 | <p>After <number> basic blocks have been executed, shut down |
| 659 | Valgrind and switch back to running the client on the real CPU. |
| 660 | </li><br> |
| 661 | <p> |
| 662 | |
| 663 | <li><code>--dump-error=<number></code> |
| 664 | [default: inactive] |
| 665 | <p>After the program has exited, show gory details of the |
| 666 | translation of the basic block containing the <number>'th |
| 667 | error context. When used with <code>--single-step=yes</code>, |
| 668 | can show the |
| 669 | exact x86 instruction causing an error.</li><br> |
| 670 | <p> |
| 671 | |
| 672 | <li><code>--smc-check=none</code><br> |
| 673 | <code>--smc-check=some</code> [default]<br> |
| 674 | <code>--smc-check=all</code> |
| 675 | <p>How carefully should Valgrind check for self-modifying code |
| 676 | writes, so that translations can be discarded? When |
| 677 | "none", no writes are checked. When "some", only writes |
| 678 | resulting from moves from integer registers to memory are |
| 679 | checked. When "all", all memory writes are checked, even those |
| 680 | with which are no sane program would generate code -- for |
| 681 | example, floating-point writes.</li> |
| 682 | </ul> |
| 683 | |
| 684 | |
| 685 | <a name="errormsgs"> |
| 686 | <h3>2.6 Explaination of error messages</h3> |
| 687 | |
| 688 | Despite considerable sophistication under the hood, Valgrind can only |
| 689 | really detect two kinds of errors, use of illegal addresses, and use |
| 690 | of undefined values. Nevertheless, this is enough to help you |
| 691 | discover all sorts of memory-management nasties in your code. This |
| 692 | section presents a quick summary of what error messages mean. The |
| 693 | precise behaviour of the error-checking machinery is described in |
| 694 | <a href="#machine">Section 4</a>. |
| 695 | |
| 696 | |
| 697 | <h4>2.6.1 Illegal read / Illegal write errors</h4> |
| 698 | For example: |
| 699 | <pre> |
| 700 | ==30975== Invalid read of size 4 |
| 701 | ==30975== at 0x40F6BBCC: (within /usr/lib/libpng.so.2.1.0.9) |
| 702 | ==30975== by 0x40F6B804: (within /usr/lib/libpng.so.2.1.0.9) |
| 703 | ==30975== by 0x40B07FF4: read_png_image__FP8QImageIO (kernel/qpngio.cpp:326) |
| 704 | ==30975== by 0x40AC751B: QImageIO::read() (kernel/qimage.cpp:3621) |
| 705 | ==30975== Address 0xBFFFF0E0 is not stack'd, malloc'd or free'd |
| 706 | </pre> |
| 707 | |
| 708 | <p>This happens when your program reads or writes memory at a place |
| 709 | which Valgrind reckons it shouldn't. In this example, the program did |
| 710 | a 4-byte read at address 0xBFFFF0E0, somewhere within the |
| 711 | system-supplied library libpng.so.2.1.0.9, which was called from |
| 712 | somewhere else in the same library, called from line 326 of |
| 713 | qpngio.cpp, and so on. |
| 714 | |
| 715 | <p>Valgrind tries to establish what the illegal address might relate |
| 716 | to, since that's often useful. So, if it points into a block of |
| 717 | memory which has already been freed, you'll be informed of this, and |
| 718 | also where the block was free'd at.. Likewise, if it should turn out |
| 719 | to be just off the end of a malloc'd block, a common result of |
| 720 | off-by-one-errors in array subscripting, you'll be informed of this |
| 721 | fact, and also where the block was malloc'd. |
| 722 | |
| 723 | <p>In this example, Valgrind can't identify the address. Actually the |
| 724 | address is on the stack, but, for some reason, this is not a valid |
| 725 | stack address -- it is below the stack pointer, %esp, and that isn't |
| 726 | allowed. |
| 727 | |
| 728 | <p>Note that Valgrind only tells you that your program is about to |
| 729 | access memory at an illegal address. It can't stop the access from |
| 730 | happening. So, if your program makes an access which normally would |
| 731 | result in a segmentation fault, you program will still suffer the same |
| 732 | fate -- but you will get a message from Valgrind immediately prior to |
| 733 | this. In this particular example, reading junk on the stack is |
| 734 | non-fatal, and the program stays alive. |
| 735 | |
| 736 | |
| 737 | <h4>2.6.2 Use of uninitialised values</h4> |
| 738 | For example: |
| 739 | <pre> |
sewardj | a7dc795 | 2002-03-24 11:29:13 +0000 | [diff] [blame] | 740 | ==19146== Conditional jump or move depends on uninitialised value(s) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 741 | ==19146== at 0x402DFA94: _IO_vfprintf (_itoa.h:49) |
| 742 | ==19146== by 0x402E8476: _IO_printf (printf.c:36) |
| 743 | ==19146== by 0x8048472: main (tests/manuel1.c:8) |
| 744 | ==19146== by 0x402A6E5E: __libc_start_main (libc-start.c:129) |
| 745 | </pre> |
| 746 | |
| 747 | <p>An uninitialised-value use error is reported when your program uses |
| 748 | a value which hasn't been initialised -- in other words, is undefined. |
| 749 | Here, the undefined value is used somewhere inside the printf() |
| 750 | machinery of the C library. This error was reported when running the |
| 751 | following small program: |
| 752 | <pre> |
| 753 | int main() |
| 754 | { |
| 755 | int x; |
| 756 | printf ("x = %d\n", x); |
| 757 | } |
| 758 | </pre> |
| 759 | |
| 760 | <p>It is important to understand that your program can copy around |
| 761 | junk (uninitialised) data to its heart's content. Valgrind observes |
| 762 | this and keeps track of the data, but does not complain. A complaint |
| 763 | is issued only when your program attempts to make use of uninitialised |
| 764 | data. In this example, x is uninitialised. Valgrind observes the |
| 765 | value being passed to _IO_printf and thence to |
| 766 | _IO_vfprintf, but makes no comment. However, |
| 767 | _IO_vfprintf has to examine the value of x |
| 768 | so it can turn it into the corresponding ASCII string, and it is at |
| 769 | this point that Valgrind complains. |
| 770 | |
| 771 | <p>Sources of uninitialised data tend to be: |
| 772 | <ul> |
| 773 | <li>Local variables in procedures which have not been initialised, |
| 774 | as in the example above.</li><br><p> |
| 775 | |
| 776 | <li>The contents of malloc'd blocks, before you write something |
| 777 | there. In C++, the new operator is a wrapper round malloc, so |
| 778 | if you create an object with new, its fields will be |
| 779 | uninitialised until you fill them in, which is only Right and |
| 780 | Proper.</li> |
| 781 | </ul> |
| 782 | |
| 783 | |
| 784 | |
| 785 | <h4>2.6.3 Illegal frees</h4> |
| 786 | For example: |
| 787 | <pre> |
| 788 | ==7593== Invalid free() |
| 789 | ==7593== at 0x4004FFDF: free (ut_clientmalloc.c:577) |
| 790 | ==7593== by 0x80484C7: main (tests/doublefree.c:10) |
| 791 | ==7593== by 0x402A6E5E: __libc_start_main (libc-start.c:129) |
| 792 | ==7593== by 0x80483B1: (within tests/doublefree) |
| 793 | ==7593== Address 0x3807F7B4 is 0 bytes inside a block of size 177 free'd |
| 794 | ==7593== at 0x4004FFDF: free (ut_clientmalloc.c:577) |
| 795 | ==7593== by 0x80484C7: main (tests/doublefree.c:10) |
| 796 | ==7593== by 0x402A6E5E: __libc_start_main (libc-start.c:129) |
| 797 | ==7593== by 0x80483B1: (within tests/doublefree) |
| 798 | </pre> |
| 799 | <p>Valgrind keeps track of the blocks allocated by your program with |
| 800 | malloc/new, so it can know exactly whether or not the argument to |
| 801 | free/delete is legitimate or not. Here, this test program has |
| 802 | freed the same block twice. As with the illegal read/write errors, |
| 803 | Valgrind attempts to make sense of the address free'd. If, as |
| 804 | here, the address is one which has previously been freed, you wil |
| 805 | be told that -- making duplicate frees of the same block easy to spot. |
| 806 | |
| 807 | |
| 808 | <h4>2.6.4 Passing system call parameters with inadequate |
| 809 | read/write permissions</h4> |
| 810 | |
| 811 | Valgrind checks all parameters to system calls. If a system call |
| 812 | needs to read from a buffer provided by your program, Valgrind checks |
| 813 | that the entire buffer is addressible and has valid data, ie, it is |
| 814 | readable. And if the system call needs to write to a user-supplied |
| 815 | buffer, Valgrind checks that the buffer is addressible. After the |
| 816 | system call, Valgrind updates its administrative information to |
| 817 | precisely reflect any changes in memory permissions caused by the |
| 818 | system call. |
| 819 | |
| 820 | <p>Here's an example of a system call with an invalid parameter: |
| 821 | <pre> |
| 822 | #include <stdlib.h> |
| 823 | #include <unistd.h> |
| 824 | int main( void ) |
| 825 | { |
| 826 | char* arr = malloc(10); |
| 827 | (void) write( 1 /* stdout */, arr, 10 ); |
| 828 | return 0; |
| 829 | } |
| 830 | </pre> |
| 831 | |
| 832 | <p>You get this complaint ... |
| 833 | <pre> |
| 834 | ==8230== Syscall param write(buf) lacks read permissions |
| 835 | ==8230== at 0x4035E072: __libc_write |
| 836 | ==8230== by 0x402A6E5E: __libc_start_main (libc-start.c:129) |
| 837 | ==8230== by 0x80483B1: (within tests/badwrite) |
| 838 | ==8230== by <bogus frame pointer> ??? |
| 839 | ==8230== Address 0x3807E6D0 is 0 bytes inside a block of size 10 alloc'd |
| 840 | ==8230== at 0x4004FEE6: malloc (ut_clientmalloc.c:539) |
| 841 | ==8230== by 0x80484A0: main (tests/badwrite.c:6) |
| 842 | ==8230== by 0x402A6E5E: __libc_start_main (libc-start.c:129) |
| 843 | ==8230== by 0x80483B1: (within tests/badwrite) |
| 844 | </pre> |
| 845 | |
| 846 | <p>... because the program has tried to write uninitialised junk from |
| 847 | the malloc'd block to the standard output. |
| 848 | |
| 849 | |
| 850 | <h4>2.6.5 Warning messages you might see</h4> |
| 851 | |
| 852 | Most of these only appear if you run in verbose mode (enabled by |
| 853 | <code>-v</code>): |
| 854 | <ul> |
| 855 | <li> <code>More than 50 errors detected. Subsequent errors |
| 856 | will still be recorded, but in less detail than before.</code> |
| 857 | <br> |
| 858 | After 50 different errors have been shown, Valgrind becomes |
| 859 | more conservative about collecting them. It then requires only |
| 860 | the program counters in the top two stack frames to match when |
| 861 | deciding whether or not two errors are really the same one. |
| 862 | Prior to this point, the PCs in the top four frames are required |
| 863 | to match. This hack has the effect of slowing down the |
| 864 | appearance of new errors after the first 50. The 50 constant can |
| 865 | be changed by recompiling Valgrind. |
| 866 | <p> |
| 867 | <li> <code>More than 500 errors detected. I'm not reporting any more. |
| 868 | Final error counts may be inaccurate. Go fix your |
| 869 | program!</code> |
| 870 | <br> |
| 871 | After 500 different errors have been detected, Valgrind ignores |
| 872 | any more. It seems unlikely that collecting even more different |
| 873 | ones would be of practical help to anybody, and it avoids the |
| 874 | danger that Valgrind spends more and more of its time comparing |
| 875 | new errors against an ever-growing collection. As above, the 500 |
| 876 | number is a compile-time constant. |
| 877 | <p> |
| 878 | <li> <code>Warning: client exiting by calling exit(<number>). |
| 879 | Bye!</code> |
| 880 | <br> |
| 881 | Your program has called the <code>exit</code> system call, which |
| 882 | will immediately terminate the process. You'll get no exit-time |
| 883 | error summaries or leak checks. Note that this is not the same |
| 884 | as your program calling the ANSI C function <code>exit()</code> |
| 885 | -- that causes a normal, controlled shutdown of Valgrind. |
| 886 | <p> |
| 887 | <li> <code>Warning: client switching stacks?</code> |
| 888 | <br> |
| 889 | Valgrind spotted such a large change in the stack pointer, %esp, |
| 890 | that it guesses the client is switching to a different stack. |
| 891 | At this point it makes a kludgey guess where the base of the new |
| 892 | stack is, and sets memory permissions accordingly. You may get |
| 893 | many bogus error messages following this, if Valgrind guesses |
| 894 | wrong. At the moment "large change" is defined as a change of |
| 895 | more that 2000000 in the value of the %esp (stack pointer) |
| 896 | register. |
| 897 | <p> |
| 898 | <li> <code>Warning: client attempted to close Valgrind's logfile fd <number> |
| 899 | </code> |
| 900 | <br> |
| 901 | Valgrind doesn't allow the client |
| 902 | to close the logfile, because you'd never see any diagnostic |
| 903 | information after that point. If you see this message, |
| 904 | you may want to use the <code>--logfile-fd=<number></code> |
| 905 | option to specify a different logfile file-descriptor number. |
| 906 | <p> |
| 907 | <li> <code>Warning: noted but unhandled ioctl <number></code> |
| 908 | <br> |
| 909 | Valgrind observed a call to one of the vast family of |
| 910 | <code>ioctl</code> system calls, but did not modify its |
| 911 | memory status info (because I have not yet got round to it). |
| 912 | The call will still have gone through, but you may get spurious |
| 913 | errors after this as a result of the non-update of the memory info. |
| 914 | <p> |
| 915 | <li> <code>Warning: unblocking signal <number> due to |
| 916 | sigprocmask</code> |
| 917 | <br> |
| 918 | Really just a diagnostic from the signal simulation machinery. |
| 919 | This message will appear if your program handles a signal by |
| 920 | first <code>longjmp</code>ing out of the signal handler, |
| 921 | and then unblocking the signal with <code>sigprocmask</code> |
| 922 | -- a standard signal-handling idiom. |
| 923 | <p> |
| 924 | <li> <code>Warning: bad signal number <number> in __NR_sigaction.</code> |
| 925 | <br> |
| 926 | Probably indicates a bug in the signal simulation machinery. |
| 927 | <p> |
| 928 | <li> <code>Warning: set address range perms: large range <number></code> |
| 929 | <br> |
| 930 | Diagnostic message, mostly for my benefit, to do with memory |
| 931 | permissions. |
| 932 | </ul> |
| 933 | |
| 934 | |
| 935 | <a name="suppfiles"></a> |
| 936 | <h3>2.7 Writing suppressions files</h3> |
| 937 | |
| 938 | A suppression file describes a bunch of errors which, for one reason |
| 939 | or another, you don't want Valgrind to tell you about. Usually the |
| 940 | reason is that the system libraries are buggy but unfixable, at least |
| 941 | within the scope of the current debugging session. Multiple |
| 942 | suppresions files are allowed. By default, Valgrind uses |
| 943 | <code>linux24.supp</code> in the directory where it is installed. |
| 944 | |
| 945 | <p> |
| 946 | You can ask to add suppressions from another file, by specifying |
| 947 | <code>--suppressions=/path/to/file.supp</code>. |
| 948 | |
| 949 | <p>Each suppression has the following components:<br> |
| 950 | <ul> |
| 951 | |
| 952 | <li>Its name. This merely gives a handy name to the suppression, by |
| 953 | which it is referred to in the summary of used suppressions |
| 954 | printed out when a program finishes. It's not important what |
| 955 | the name is; any identifying string will do. |
| 956 | <p> |
| 957 | |
| 958 | <li>The nature of the error to suppress. Either: |
| 959 | <code>Value1</code>, |
| 960 | <code>Value2</code>, |
sewardj | a7dc795 | 2002-03-24 11:29:13 +0000 | [diff] [blame] | 961 | <code>Value4</code> or |
| 962 | <code>Value8</code>, |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 963 | meaning an uninitialised-value error when |
sewardj | a7dc795 | 2002-03-24 11:29:13 +0000 | [diff] [blame] | 964 | using a value of 1, 2, 4 or 8 bytes. |
| 965 | Or |
| 966 | <code>Cond</code> (or its old name, <code>Value0</code>), |
| 967 | meaning use of an uninitialised CPU condition code. Or: |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 968 | <code>Addr1</code>, |
| 969 | <code>Addr2</code>, |
| 970 | <code>Addr4</code> or |
| 971 | <code>Addr8</code>, meaning an invalid address during a |
| 972 | memory access of 1, 2, 4 or 8 bytes respectively. Or |
| 973 | <code>Param</code>, |
| 974 | meaning an invalid system call parameter error. Or |
| 975 | <code>Free</code>, meaning an invalid or mismatching free.</li><br> |
| 976 | <p> |
| 977 | |
| 978 | <li>The "immediate location" specification. For Value and Addr |
| 979 | errors, is either the name of the function in which the error |
| 980 | occurred, or, failing that, the full path the the .so file |
| 981 | containing the error location. For Param errors, is the name of |
| 982 | the offending system call parameter. For Free errors, is the |
| 983 | name of the function doing the freeing (eg, <code>free</code>, |
| 984 | <code>__builtin_vec_delete</code>, etc)</li><br> |
| 985 | <p> |
| 986 | |
| 987 | <li>The caller of the above "immediate location". Again, either a |
| 988 | function or shared-object name.</li><br> |
| 989 | <p> |
| 990 | |
| 991 | <li>Optionally, one or two extra calling-function or object names, |
| 992 | for greater precision.</li> |
| 993 | </ul> |
| 994 | |
| 995 | <p> |
| 996 | Locations may be either names of shared objects or wildcards matching |
| 997 | function names. They begin <code>obj:</code> and <code>fun:</code> |
| 998 | respectively. Function and object names to match against may use the |
| 999 | wildcard characters <code>*</code> and <code>?</code>. |
| 1000 | |
| 1001 | A suppression only suppresses an error when the error matches all the |
| 1002 | details in the suppression. Here's an example: |
| 1003 | <pre> |
| 1004 | { |
| 1005 | __gconv_transform_ascii_internal/__mbrtowc/mbtowc |
| 1006 | Value4 |
| 1007 | fun:__gconv_transform_ascii_internal |
| 1008 | fun:__mbr*toc |
| 1009 | fun:mbtowc |
| 1010 | } |
| 1011 | </pre> |
| 1012 | |
| 1013 | <p>What is means is: suppress a use-of-uninitialised-value error, when |
| 1014 | the data size is 4, when it occurs in the function |
| 1015 | <code>__gconv_transform_ascii_internal</code>, when that is called |
| 1016 | from any function of name matching <code>__mbr*toc</code>, |
| 1017 | when that is called from |
| 1018 | <code>mbtowc</code>. It doesn't apply under any other circumstances. |
| 1019 | The string by which this suppression is identified to the user is |
| 1020 | __gconv_transform_ascii_internal/__mbrtowc/mbtowc. |
| 1021 | |
| 1022 | <p>Another example: |
| 1023 | <pre> |
| 1024 | { |
| 1025 | libX11.so.6.2/libX11.so.6.2/libXaw.so.7.0 |
| 1026 | Value4 |
| 1027 | obj:/usr/X11R6/lib/libX11.so.6.2 |
| 1028 | obj:/usr/X11R6/lib/libX11.so.6.2 |
| 1029 | obj:/usr/X11R6/lib/libXaw.so.7.0 |
| 1030 | } |
| 1031 | </pre> |
| 1032 | |
| 1033 | <p>Suppress any size 4 uninitialised-value error which occurs anywhere |
| 1034 | in <code>libX11.so.6.2</code>, when called from anywhere in the same |
| 1035 | library, when called from anywhere in <code>libXaw.so.7.0</code>. The |
| 1036 | inexact specification of locations is regrettable, but is about all |
| 1037 | you can hope for, given that the X11 libraries shipped with Red Hat |
| 1038 | 7.2 have had their symbol tables removed. |
| 1039 | |
| 1040 | <p>Note -- since the above two examples did not make it clear -- that |
| 1041 | you can freely mix the <code>obj:</code> and <code>fun:</code> |
| 1042 | styles of description within a single suppression record. |
| 1043 | |
| 1044 | |
| 1045 | <a name="install"></a> |
| 1046 | <h3>2.8 Building and installing</h3> |
| 1047 | At the moment, very rudimentary. |
| 1048 | |
| 1049 | <p>The tarball is set up for a standard Red Hat 7.1 (6.2) machine. To |
| 1050 | build, just do "make". No configure script, no autoconf, no nothing. |
| 1051 | |
| 1052 | <p>The files needed for installation are: valgrind.so, valgring.so, |
| 1053 | valgrind, VERSION, redhat72.supp (or redhat62.supp). You can copy |
| 1054 | these to any directory you like. However, you then need to edit the |
| 1055 | shell script "valgrind". On line 4, set the environment variable |
| 1056 | <code>VALGRIND</code> to point to the directory you have copied the |
| 1057 | installation into. |
| 1058 | |
| 1059 | |
| 1060 | <a name="problems"></a> |
| 1061 | <h3>2.9 If you have problems</h3> |
| 1062 | Mail me (<a href="mailto:jseward@acm.org">jseward@acm.org</a>). |
| 1063 | |
| 1064 | <p>See <a href="#limits">Section 4</a> for the known limitations of |
| 1065 | Valgrind, and for a list of programs which are known not to work on |
| 1066 | it. |
| 1067 | |
| 1068 | <p>The translator/instrumentor has a lot of assertions in it. They |
| 1069 | are permanently enabled, and I have no plans to disable them. If one |
| 1070 | of these breaks, please mail me! |
| 1071 | |
| 1072 | <p>If you get an assertion failure on the expression |
| 1073 | <code>chunkSane(ch)</code> in <code>vg_free()</code> in |
| 1074 | <code>vg_malloc.c</code>, this may have happened because your program |
| 1075 | wrote off the end of a malloc'd block, or before its beginning. |
| 1076 | Valgrind should have emitted a proper message to that effect before |
| 1077 | dying in this way. This is a known problem which I should fix. |
| 1078 | <p> |
| 1079 | |
| 1080 | <hr width="100%"> |
| 1081 | |
| 1082 | <a name="machine"></a> |
| 1083 | <h2>3 Details of the checking machinery</h2> |
| 1084 | |
| 1085 | Read this section if you want to know, in detail, exactly what and how |
| 1086 | Valgrind is checking. |
| 1087 | |
| 1088 | <a name="vvalue"></a> |
| 1089 | <h3>3.1 Valid-value (V) bits</h3> |
| 1090 | |
| 1091 | It is simplest to think of Valgrind implementing a synthetic Intel x86 |
| 1092 | CPU which is identical to a real CPU, except for one crucial detail. |
| 1093 | Every bit (literally) of data processed, stored and handled by the |
| 1094 | real CPU has, in the synthetic CPU, an associated "valid-value" bit, |
| 1095 | which says whether or not the accompanying bit has a legitimate value. |
| 1096 | In the discussions which follow, this bit is referred to as the V |
| 1097 | (valid-value) bit. |
| 1098 | |
| 1099 | <p>Each byte in the system therefore has a 8 V bits which accompanies |
| 1100 | it wherever it goes. For example, when the CPU loads a word-size item |
| 1101 | (4 bytes) from memory, it also loads the corresponding 32 V bits from |
| 1102 | a bitmap which stores the V bits for the process' entire address |
| 1103 | space. If the CPU should later write the whole or some part of that |
| 1104 | value to memory at a different address, the relevant V bits will be |
| 1105 | stored back in the V-bit bitmap. |
| 1106 | |
| 1107 | <p>In short, each bit in the system has an associated V bit, which |
| 1108 | follows it around everywhere, even inside the CPU. Yes, the CPU's |
| 1109 | (integer) registers have their own V bit vectors. |
| 1110 | |
| 1111 | <p>Copying values around does not cause Valgrind to check for, or |
| 1112 | report on, errors. However, when a value is used in a way which might |
| 1113 | conceivably affect the outcome of your program's computation, the |
| 1114 | associated V bits are immediately checked. If any of these indicate |
| 1115 | that the value is undefined, an error is reported. |
| 1116 | |
| 1117 | <p>Here's an (admittedly nonsensical) example: |
| 1118 | <pre> |
| 1119 | int i, j; |
| 1120 | int a[10], b[10]; |
| 1121 | for (i = 0; i < 10; i++) { |
| 1122 | j = a[i]; |
| 1123 | b[i] = j; |
| 1124 | } |
| 1125 | </pre> |
| 1126 | |
| 1127 | <p>Valgrind emits no complaints about this, since it merely copies |
| 1128 | uninitialised values from <code>a[]</code> into <code>b[]</code>, and |
| 1129 | doesn't use them in any way. However, if the loop is changed to |
| 1130 | <pre> |
| 1131 | for (i = 0; i < 10; i++) { |
| 1132 | j += a[i]; |
| 1133 | } |
| 1134 | if (j == 77) |
| 1135 | printf("hello there\n"); |
| 1136 | </pre> |
| 1137 | then Valgrind will complain, at the <code>if</code>, that the |
| 1138 | condition depends on uninitialised values. |
| 1139 | |
| 1140 | <p>Most low level operations, such as adds, cause Valgrind to |
| 1141 | use the V bits for the operands to calculate the V bits for the |
| 1142 | result. Even if the result is partially or wholly undefined, |
| 1143 | it does not complain. |
| 1144 | |
| 1145 | <p>Checks on definedness only occur in two places: when a value is |
| 1146 | used to generate a memory address, and where control flow decision |
| 1147 | needs to be made. Also, when a system call is detected, valgrind |
| 1148 | checks definedness of parameters as required. |
| 1149 | |
| 1150 | <p>If a check should detect undefinedness, and error message is |
| 1151 | issued. The resulting value is subsequently regarded as well-defined. |
| 1152 | To do otherwise would give long chains of error messages. In effect, |
| 1153 | we say that undefined values are non-infectious. |
| 1154 | |
| 1155 | <p>This sounds overcomplicated. Why not just check all reads from |
| 1156 | memory, and complain if an undefined value is loaded into a CPU register? |
| 1157 | Well, that doesn't work well, because perfectly legitimate C programs routinely |
| 1158 | copy uninitialised values around in memory, and we don't want endless complaints |
| 1159 | about that. Here's the canonical example. Consider a struct |
| 1160 | like this: |
| 1161 | <pre> |
| 1162 | struct S { int x; char c; }; |
| 1163 | struct S s1, s2; |
| 1164 | s1.x = 42; |
| 1165 | s1.c = 'z'; |
| 1166 | s2 = s1; |
| 1167 | </pre> |
| 1168 | |
| 1169 | <p>The question to ask is: how large is <code>struct S</code>, in |
| 1170 | bytes? An int is 4 bytes and a char one byte, so perhaps a struct S |
| 1171 | occupies 5 bytes? Wrong. All (non-toy) compilers I know of will |
| 1172 | round the size of <code>struct S</code> up to a whole number of words, |
| 1173 | in this case 8 bytes. Not doing this forces compilers to generate |
| 1174 | truly appalling code for subscripting arrays of <code>struct |
| 1175 | S</code>'s. |
| 1176 | |
| 1177 | <p>So s1 occupies 8 bytes, yet only 5 of them will be initialised. |
| 1178 | For the assignment <code>s2 = s1</code>, gcc generates code to copy |
| 1179 | all 8 bytes wholesale into <code>s2</code> without regard for their |
| 1180 | meaning. If Valgrind simply checked values as they came out of |
| 1181 | memory, it would yelp every time a structure assignment like this |
| 1182 | happened. So the more complicated semantics described above is |
| 1183 | necessary. This allows gcc to copy <code>s1</code> into |
| 1184 | <code>s2</code> any way it likes, and a warning will only be emitted |
| 1185 | if the uninitialised values are later used. |
| 1186 | |
| 1187 | <p>One final twist to this story. The above scheme allows garbage to |
| 1188 | pass through the CPU's integer registers without complaint. It does |
| 1189 | this by giving the integer registers V tags, passing these around in |
| 1190 | the expected way. This complicated and computationally expensive to |
| 1191 | do, but is necessary. Valgrind is more simplistic about |
| 1192 | floating-point loads and stores. In particular, V bits for data read |
| 1193 | as a result of floating-point loads are checked at the load |
| 1194 | instruction. So if your program uses the floating-point registers to |
| 1195 | do memory-to-memory copies, you will get complaints about |
| 1196 | uninitialised values. Fortunately, I have not yet encountered a |
| 1197 | program which (ab)uses the floating-point registers in this way. |
| 1198 | |
| 1199 | <a name="vaddress"></a> |
| 1200 | <h3>3.2 Valid-address (A) bits</h3> |
| 1201 | |
| 1202 | Notice that the previous section describes how the validity of values |
| 1203 | is established and maintained without having to say whether the |
| 1204 | program does or does not have the right to access any particular |
| 1205 | memory location. We now consider the latter issue. |
| 1206 | |
| 1207 | <p>As described above, every bit in memory or in the CPU has an |
| 1208 | associated valid-value (V) bit. In addition, all bytes in memory, but |
| 1209 | not in the CPU, have an associated valid-address (A) bit. This |
| 1210 | indicates whether or not the program can legitimately read or write |
| 1211 | that location. It does not give any indication of the validity or the |
| 1212 | data at that location -- that's the job of the V bits -- only whether |
| 1213 | or not the location may be accessed. |
| 1214 | |
| 1215 | <p>Every time your program reads or writes memory, Valgrind checks the |
| 1216 | A bits associated with the address. If any of them indicate an |
| 1217 | invalid address, an error is emitted. Note that the reads and writes |
| 1218 | themselves do not change the A bits, only consult them. |
| 1219 | |
| 1220 | <p>So how do the A bits get set/cleared? Like this: |
| 1221 | |
| 1222 | <ul> |
| 1223 | <li>When the program starts, all the global data areas are marked as |
| 1224 | accessible.</li><br> |
| 1225 | <p> |
| 1226 | |
| 1227 | <li>When the program does malloc/new, the A bits for the exactly the |
| 1228 | area allocated, and not a byte more, are marked as accessible. |
| 1229 | Upon freeing the area the A bits are changed to indicate |
| 1230 | inaccessibility.</li><br> |
| 1231 | <p> |
| 1232 | |
| 1233 | <li>When the stack pointer register (%esp) moves up or down, A bits |
| 1234 | are set. The rule is that the area from %esp up to the base of |
| 1235 | the stack is marked as accessible, and below %esp is |
| 1236 | inaccessible. (If that sounds illogical, bear in mind that the |
| 1237 | stack grows down, not up, on almost all Unix systems, including |
| 1238 | GNU/Linux.) Tracking %esp like this has the useful side-effect |
| 1239 | that the section of stack used by a function for local variables |
| 1240 | etc is automatically marked accessible on function entry and |
| 1241 | inaccessible on exit.</li><br> |
| 1242 | <p> |
| 1243 | |
| 1244 | <li>When doing system calls, A bits are changed appropriately. For |
| 1245 | example, mmap() magically makes files appear in the process's |
| 1246 | address space, so the A bits must be updated if mmap() |
| 1247 | succeeds.</li><br> |
| 1248 | </ul> |
| 1249 | |
| 1250 | |
| 1251 | <a name="together"></a> |
| 1252 | <h3>3.3 Putting it all together</h3> |
| 1253 | Valgrind's checking machinery can be summarised as follows: |
| 1254 | |
| 1255 | <ul> |
| 1256 | <li>Each byte in memory has 8 associated V (valid-value) bits, |
| 1257 | saying whether or not the byte has a defined value, and a single |
| 1258 | A (valid-address) bit, saying whether or not the program |
| 1259 | currently has the right to read/write that address.</li><br> |
| 1260 | <p> |
| 1261 | |
| 1262 | <li>When memory is read or written, the relevant A bits are |
| 1263 | consulted. If they indicate an invalid address, Valgrind emits |
| 1264 | an Invalid read or Invalid write error.</li><br> |
| 1265 | <p> |
| 1266 | |
| 1267 | <li>When memory is read into the CPU's integer registers, the |
| 1268 | relevant V bits are fetched from memory and stored in the |
| 1269 | simulated CPU. They are not consulted.</li><br> |
| 1270 | <p> |
| 1271 | |
| 1272 | <li>When an integer register is written out to memory, the V bits |
| 1273 | for that register are written back to memory too.</li><br> |
| 1274 | <p> |
| 1275 | |
| 1276 | <li>When memory is read into the CPU's floating point registers, the |
| 1277 | relevant V bits are read from memory and they are immediately |
| 1278 | checked. If any are invalid, an uninitialised value error is |
| 1279 | emitted. This precludes using the floating-point registers to |
| 1280 | copy possibly-uninitialised memory, but simplifies Valgrind in |
| 1281 | that it does not have to track the validity status of the |
| 1282 | floating-point registers.</li><br> |
| 1283 | <p> |
| 1284 | |
| 1285 | <li>As a result, when a floating-point register is written to |
| 1286 | memory, the associated V bits are set to indicate a valid |
| 1287 | value.</li><br> |
| 1288 | <p> |
| 1289 | |
| 1290 | <li>When values in integer CPU registers are used to generate a |
| 1291 | memory address, or to determine the outcome of a conditional |
| 1292 | branch, the V bits for those values are checked, and an error |
| 1293 | emitted if any of them are undefined.</li><br> |
| 1294 | <p> |
| 1295 | |
| 1296 | <li>When values in integer CPU registers are used for any other |
| 1297 | purpose, Valgrind computes the V bits for the result, but does |
| 1298 | not check them.</li><br> |
| 1299 | <p> |
| 1300 | |
| 1301 | <li>One the V bits for a value in the CPU have been checked, they |
| 1302 | are then set to indicate validity. This avoids long chains of |
| 1303 | errors.</li><br> |
| 1304 | <p> |
| 1305 | |
| 1306 | <li>When values are loaded from memory, valgrind checks the A bits |
| 1307 | for that location and issues an illegal-address warning if |
| 1308 | needed. In that case, the V bits loaded are forced to indicate |
| 1309 | Valid, despite the location being invalid. |
| 1310 | <p> |
| 1311 | This apparently strange choice reduces the amount of confusing |
| 1312 | information presented to the user. It avoids the |
| 1313 | unpleasant phenomenon in which memory is read from a place which |
| 1314 | is both unaddressible and contains invalid values, and, as a |
| 1315 | result, you get not only an invalid-address (read/write) error, |
| 1316 | but also a potentially large set of uninitialised-value errors, |
| 1317 | one for every time the value is used. |
| 1318 | <p> |
| 1319 | There is a hazy boundary case to do with multi-byte loads from |
| 1320 | addresses which are partially valid and partially invalid. See |
| 1321 | details of the flag <code>--partial-loads-ok</code> for details. |
| 1322 | </li><br> |
| 1323 | </ul> |
| 1324 | |
| 1325 | Valgrind intercepts calls to malloc, calloc, realloc, valloc, |
| 1326 | memalign, free, new and delete. The behaviour you get is: |
| 1327 | |
| 1328 | <ul> |
| 1329 | |
| 1330 | <li>malloc/new: the returned memory is marked as addressible but not |
| 1331 | having valid values. This means you have to write on it before |
| 1332 | you can read it.</li><br> |
| 1333 | <p> |
| 1334 | |
| 1335 | <li>calloc: returned memory is marked both addressible and valid, |
| 1336 | since calloc() clears the area to zero.</li><br> |
| 1337 | <p> |
| 1338 | |
| 1339 | <li>realloc: if the new size is larger than the old, the new section |
| 1340 | is addressible but invalid, as with malloc.</li><br> |
| 1341 | <p> |
| 1342 | |
| 1343 | <li>If the new size is smaller, the dropped-off section is marked as |
| 1344 | unaddressible. You may only pass to realloc a pointer |
| 1345 | previously issued to you by malloc/calloc/new/realloc.</li><br> |
| 1346 | <p> |
| 1347 | |
| 1348 | <li>free/delete: you may only pass to free a pointer previously |
| 1349 | issued to you by malloc/calloc/new/realloc, or the value |
| 1350 | NULL. Otherwise, Valgrind complains. If the pointer is indeed |
| 1351 | valid, Valgrind marks the entire area it points at as |
| 1352 | unaddressible, and places the block in the freed-blocks-queue. |
| 1353 | The aim is to defer as long as possible reallocation of this |
| 1354 | block. Until that happens, all attempts to access it will |
| 1355 | elicit an invalid-address error, as you would hope.</li><br> |
| 1356 | </ul> |
| 1357 | |
| 1358 | |
| 1359 | |
| 1360 | <a name="signals"></a> |
| 1361 | <h3>3.4 Signals</h3> |
| 1362 | |
| 1363 | Valgrind provides suitable handling of signals, so, provided you stick |
| 1364 | to POSIX stuff, you should be ok. Basic sigaction() and sigprocmask() |
| 1365 | are handled. Signal handlers may return in the normal way or do |
| 1366 | longjmp(); both should work ok. As specified by POSIX, a signal is |
| 1367 | blocked in its own handler. Default actions for signals should work |
| 1368 | as before. Etc, etc. |
| 1369 | |
| 1370 | <p>Under the hood, dealing with signals is a real pain, and Valgrind's |
| 1371 | simulation leaves much to be desired. If your program does |
| 1372 | way-strange stuff with signals, bad things may happen. If so, let me |
| 1373 | know. I don't promise to fix it, but I'd at least like to be aware of |
| 1374 | it. |
| 1375 | |
| 1376 | |
| 1377 | <a name="leaks"><a/> |
| 1378 | <h3>3.5 Memory leak detection</h3> |
| 1379 | |
| 1380 | Valgrind keeps track of all memory blocks issued in response to calls |
| 1381 | to malloc/calloc/realloc/new. So when the program exits, it knows |
| 1382 | which blocks are still outstanding -- have not been returned, in other |
| 1383 | words. Ideally, you want your program to have no blocks still in use |
| 1384 | at exit. But many programs do. |
| 1385 | |
| 1386 | <p>For each such block, Valgrind scans the entire address space of the |
| 1387 | process, looking for pointers to the block. One of three situations |
| 1388 | may result: |
| 1389 | |
| 1390 | <ul> |
| 1391 | <li>A pointer to the start of the block is found. This usually |
| 1392 | indicates programming sloppiness; since the block is still |
| 1393 | pointed at, the programmer could, at least in principle, free'd |
| 1394 | it before program exit.</li><br> |
| 1395 | <p> |
| 1396 | |
| 1397 | <li>A pointer to the interior of the block is found. The pointer |
| 1398 | might originally have pointed to the start and have been moved |
| 1399 | along, or it might be entirely unrelated. Valgrind deems such a |
| 1400 | block as "dubious", that is, possibly leaked, |
| 1401 | because it's unclear whether or |
| 1402 | not a pointer to it still exists.</li><br> |
| 1403 | <p> |
| 1404 | |
| 1405 | <li>The worst outcome is that no pointer to the block can be found. |
| 1406 | The block is classified as "leaked", because the |
| 1407 | programmer could not possibly have free'd it at program exit, |
| 1408 | since no pointer to it exists. This might be a symptom of |
| 1409 | having lost the pointer at some earlier point in the |
| 1410 | program.</li> |
| 1411 | </ul> |
| 1412 | |
| 1413 | Valgrind reports summaries about leaked and dubious blocks. |
| 1414 | For each such block, it will also tell you where the block was |
| 1415 | allocated. This should help you figure out why the pointer to it has |
| 1416 | been lost. In general, you should attempt to ensure your programs do |
| 1417 | not have any leaked or dubious blocks at exit. |
| 1418 | |
| 1419 | <p>The precise area of memory in which Valgrind searches for pointers |
| 1420 | is: all naturally-aligned 4-byte words for which all A bits indicate |
| 1421 | addressibility and all V bits indicated that the stored value is |
| 1422 | actually valid. |
| 1423 | |
| 1424 | <p><hr width="100%"> |
| 1425 | |
| 1426 | |
| 1427 | <a name="limits"></a> |
| 1428 | <h2>4 Limitations</h2> |
| 1429 | |
| 1430 | The following list of limitations seems depressingly long. However, |
| 1431 | most programs actually work fine. |
| 1432 | |
| 1433 | <p>Valgrind will run x86-GNU/Linux ELF dynamically linked binaries, on |
| 1434 | a kernel 2.4.X system, subject to the following constraints: |
| 1435 | |
| 1436 | <ul> |
| 1437 | <li>No MMX, SSE, SSE2, 3DNow instructions. If the translator |
| 1438 | encounters these, Valgrind will simply give up. It may be |
| 1439 | possible to add support for them at a later time. Intel added a |
| 1440 | few instructions such as "cmov" to the integer instruction set |
| 1441 | on Pentium and later processors, and these are supported. |
| 1442 | Nevertheless it's safest to think of Valgrind as implementing |
| 1443 | the 486 instruction set.</li><br> |
| 1444 | <p> |
| 1445 | |
| 1446 | <li>Multithreaded programs are not supported, since I haven't yet |
| 1447 | figured out how to do this. To be more specific, it is the |
| 1448 | "clone" system call which is not supported. A program calls |
| 1449 | "clone" to create threads. Valgrind will abort if this |
| 1450 | happens.</li><nr> |
| 1451 | <p> |
| 1452 | |
| 1453 | <li>Valgrind assumes that the floating point registers are not used |
| 1454 | as intermediaries in memory-to-memory copies, so it immediately |
| 1455 | checks V bits in floating-point loads/stores. If you want to |
| 1456 | write code which copies around possibly-uninitialised values, |
| 1457 | you must ensure these travel through the integer registers, not |
| 1458 | the FPU.</li><br> |
| 1459 | <p> |
| 1460 | |
| 1461 | <li>If your program does its own memory management, rather than |
| 1462 | using malloc/new/free/delete, it should still work, but |
| 1463 | Valgrind's error checking won't be so effective.</li><br> |
| 1464 | <p> |
| 1465 | |
| 1466 | <li>Valgrind's signal simulation is not as robust as it could be. |
| 1467 | Basic POSIX-compliant sigaction and sigprocmask functionality is |
| 1468 | supplied, but it's conceivable that things could go badly awry |
| 1469 | if you do wierd things with signals. Workaround: don't. |
| 1470 | Programs that do non-POSIX signal tricks are in any case |
| 1471 | inherently unportable, so should be avoided if |
| 1472 | possible.</li><br> |
| 1473 | <p> |
| 1474 | |
| 1475 | <li>I have no idea what happens if programs try to handle signals on |
| 1476 | an alternate stack (sigaltstack). YMMV.</li><br> |
| 1477 | <p> |
| 1478 | |
| 1479 | <li>Programs which switch stacks are not well handled. Valgrind |
| 1480 | does have support for this, but I don't have great faith in it. |
| 1481 | It's difficult -- there's no cast-iron way to decide whether a |
| 1482 | large change in %esp is as a result of the program switching |
| 1483 | stacks, or merely allocating a large object temporarily on the |
| 1484 | current stack -- yet Valgrind needs to handle the two situations |
| 1485 | differently.</li><br> |
| 1486 | <p> |
| 1487 | |
| 1488 | <li>x86 instructions, and system calls, have been implemented on |
| 1489 | demand. So it's possible, although unlikely, that a program |
| 1490 | will fall over with a message to that effect. If this happens, |
| 1491 | please mail me ALL the details printed out, so I can try and |
| 1492 | implement the missing feature.</li><br> |
| 1493 | <p> |
| 1494 | |
| 1495 | <li>x86 floating point works correctly, but floating-point code may |
| 1496 | run even more slowly than integer code, due to my simplistic |
| 1497 | approach to FPU emulation.</li><br> |
| 1498 | <p> |
| 1499 | |
| 1500 | <li>You can't Valgrind-ize statically linked binaries. Valgrind |
| 1501 | relies on the dynamic-link mechanism to gain control at |
| 1502 | startup.</li><br> |
| 1503 | <p> |
| 1504 | |
| 1505 | <li>Memory consumption of your program is majorly increased whilst |
| 1506 | running under Valgrind. This is due to the large amount of |
| 1507 | adminstrative information maintained behind the scenes. Another |
| 1508 | cause is that Valgrind dynamically translates the original |
| 1509 | executable and never throws any translation away, except in |
| 1510 | those rare cases where self-modifying code is detected. |
| 1511 | Translated, instrumented code is 8-12 times larger than the |
| 1512 | original (!) so you can easily end up with 15+ MB of |
| 1513 | translations when running (eg) a web browser. There's not a lot |
| 1514 | you can do about this -- use Valgrind on a fast machine with a lot |
| 1515 | of memory and swap space. At some point I may implement a LRU |
| 1516 | caching scheme for translations, so as to bound the maximum |
| 1517 | amount of memory devoted to them, to say 8 or 16 MB.</li> |
| 1518 | </ul> |
| 1519 | |
| 1520 | |
| 1521 | Programs which are known not to work are: |
| 1522 | |
| 1523 | <ul> |
| 1524 | <li>Netscape 4.76 works pretty well on some platforms -- quite |
| 1525 | nicely on my AMD K6-III (400 MHz). I can surf, do mail, etc, no |
| 1526 | problem. On other platforms is has been observed to crash |
| 1527 | during startup. Despite much investigation I can't figure out |
| 1528 | why.</li><br> |
| 1529 | <p> |
| 1530 | |
| 1531 | <li>kpackage (a KDE front end to rpm) dies because the CPUID |
| 1532 | instruction is unimplemented. Easy to fix.</li><br> |
| 1533 | <p> |
| 1534 | |
| 1535 | <li>knode (a KDE newsreader) tries to do multithreaded things, and |
| 1536 | fails.</li><br> |
| 1537 | <p> |
| 1538 | |
| 1539 | <li>emacs starts up but immediately concludes it is out of memory |
| 1540 | and aborts. Emacs has it's own memory-management scheme, but I |
| 1541 | don't understand why this should interact so badly with |
| 1542 | Valgrind.</li><br> |
| 1543 | <p> |
| 1544 | |
| 1545 | <li>Gimp and Gnome and GTK-based apps die early on because |
| 1546 | of unimplemented system call wrappers. (I'm a KDE user :) |
| 1547 | This wouldn't be hard to fix. |
| 1548 | </li><br> |
| 1549 | <p> |
| 1550 | |
| 1551 | <li>As a consequence of me being a KDE user, almost all KDE apps |
| 1552 | work ok -- except those which are multithreaded. |
| 1553 | </li><br> |
| 1554 | <p> |
| 1555 | </ul> |
| 1556 | |
| 1557 | |
| 1558 | <p><hr width="100%"> |
| 1559 | |
| 1560 | |
| 1561 | <a name="howitworks"></a> |
| 1562 | <h2>5 How it works -- a rough overview</h2> |
| 1563 | Some gory details, for those with a passion for gory details. You |
| 1564 | don't need to read this section if all you want to do is use Valgrind. |
| 1565 | |
| 1566 | <a name="startb"></a> |
| 1567 | <h3>5.1 Getting started</h3> |
| 1568 | |
| 1569 | Valgrind is compiled into a shared object, valgrind.so. The shell |
| 1570 | script valgrind sets the LD_PRELOAD environment variable to point to |
| 1571 | valgrind.so. This causes the .so to be loaded as an extra library to |
| 1572 | any subsequently executed dynamically-linked ELF binary, viz, the |
| 1573 | program you want to debug. |
| 1574 | |
| 1575 | <p>The dynamic linker allows each .so in the process image to have an |
| 1576 | initialisation function which is run before main(). It also allows |
| 1577 | each .so to have a finalisation function run after main() exits. |
| 1578 | |
| 1579 | <p>When valgrind.so's initialisation function is called by the dynamic |
| 1580 | linker, the synthetic CPU to starts up. The real CPU remains locked |
| 1581 | in valgrind.so for the entire rest of the program, but the synthetic |
| 1582 | CPU returns from the initialisation function. Startup of the program |
| 1583 | now continues as usual -- the dynamic linker calls all the other .so's |
| 1584 | initialisation routines, and eventually runs main(). This all runs on |
| 1585 | the synthetic CPU, not the real one, but the client program cannot |
| 1586 | tell the difference. |
| 1587 | |
| 1588 | <p>Eventually main() exits, so the synthetic CPU calls valgrind.so's |
| 1589 | finalisation function. Valgrind detects this, and uses it as its cue |
| 1590 | to exit. It prints summaries of all errors detected, possibly checks |
| 1591 | for memory leaks, and then exits the finalisation routine, but now on |
| 1592 | the real CPU. The synthetic CPU has now lost control -- permanently |
| 1593 | -- so the program exits back to the OS on the real CPU, just as it |
| 1594 | would have done anyway. |
| 1595 | |
| 1596 | <p>On entry, Valgrind switches stacks, so it runs on its own stack. |
| 1597 | On exit, it switches back. This means that the client program |
| 1598 | continues to run on its own stack, so we can switch back and forth |
| 1599 | between running it on the simulated and real CPUs without difficulty. |
| 1600 | This was an important design decision, because it makes it easy (well, |
| 1601 | significantly less difficult) to debug the synthetic CPU. |
| 1602 | |
| 1603 | |
| 1604 | <a name="engine"></a> |
| 1605 | <h3>5.2 The translation/instrumentation engine</h3> |
| 1606 | |
| 1607 | Valgrind does not directly run any of the original program's code. Only |
| 1608 | instrumented translations are run. Valgrind maintains a translation |
| 1609 | table, which allows it to find the translation quickly for any branch |
| 1610 | target (code address). If no translation has yet been made, the |
| 1611 | translator - a just-in-time translator - is summoned. This makes an |
| 1612 | instrumented translation, which is added to the collection of |
| 1613 | translations. Subsequent jumps to that address will use this |
| 1614 | translation. |
| 1615 | |
| 1616 | <p>Valgrind can optionally check writes made by the application, to |
| 1617 | see if they are writing an address contained within code which has |
| 1618 | been translated. Such a write invalidates translations of code |
| 1619 | bracketing the written address. Valgrind will discard the relevant |
| 1620 | translations, which causes them to be re-made, if they are needed |
| 1621 | again, reflecting the new updated data stored there. In this way, |
| 1622 | self modifying code is supported. In practice I have not found any |
| 1623 | Linux applications which use self-modifying-code. |
| 1624 | |
| 1625 | <p>The JITter translates basic blocks -- blocks of straight-line-code |
| 1626 | -- as single entities. To minimise the considerable difficulties of |
| 1627 | dealing with the x86 instruction set, x86 instructions are first |
| 1628 | translated to a RISC-like intermediate code, similar to sparc code, |
| 1629 | but with an infinite number of virtual integer registers. Initially |
| 1630 | each insn is translated seperately, and there is no attempt at |
| 1631 | instrumentation. |
| 1632 | |
| 1633 | <p>The intermediate code is improved, mostly so as to try and cache |
| 1634 | the simulated machine's registers in the real machine's registers over |
| 1635 | several simulated instructions. This is often very effective. Also, |
| 1636 | we try to remove redundant updates of the simulated machines's |
| 1637 | condition-code register. |
| 1638 | |
| 1639 | <p>The intermediate code is then instrumented, giving more |
| 1640 | intermediate code. There are a few extra intermediate-code operations |
| 1641 | to support instrumentation; it is all refreshingly simple. After |
| 1642 | instrumentation there is a cleanup pass to remove redundant value |
| 1643 | checks. |
| 1644 | |
| 1645 | <p>This gives instrumented intermediate code which mentions arbitrary |
| 1646 | numbers of virtual registers. A linear-scan register allocator is |
| 1647 | used to assign real registers and possibly generate spill code. All |
| 1648 | of this is still phrased in terms of the intermediate code. This |
| 1649 | machinery is inspired by the work of Reuben Thomas (MITE). |
| 1650 | |
| 1651 | <p>Then, and only then, is the final x86 code emitted. The |
| 1652 | intermediate code is carefully designed so that x86 code can be |
| 1653 | generated from it without need for spare registers or other |
| 1654 | inconveniences. |
| 1655 | |
| 1656 | <p>The translations are managed using a traditional LRU-based caching |
| 1657 | scheme. The translation cache has a default size of about 14MB. |
| 1658 | |
| 1659 | <a name="track"></a> |
| 1660 | |
| 1661 | <h3>5.3 Tracking the status of memory</h3> Each byte in the |
| 1662 | process' address space has nine bits associated with it: one A bit and |
| 1663 | eight V bits. The A and V bits for each byte are stored using a |
| 1664 | sparse array, which flexibly and efficiently covers arbitrary parts of |
| 1665 | the 32-bit address space without imposing significant space or |
| 1666 | performance overheads for the parts of the address space never |
| 1667 | visited. The scheme used, and speedup hacks, are described in detail |
| 1668 | at the top of the source file vg_memory.c, so you should read that for |
| 1669 | the gory details. |
| 1670 | |
| 1671 | <a name="sys_calls"></a> |
| 1672 | |
| 1673 | <h3>5.4 System calls</h3> |
| 1674 | All system calls are intercepted. The memory status map is consulted |
| 1675 | before and updated after each call. It's all rather tiresome. See |
| 1676 | vg_syscall_mem.c for details. |
| 1677 | |
| 1678 | <a name="sys_signals"></a> |
| 1679 | |
| 1680 | <h3>5.5 Signals</h3> |
| 1681 | All system calls to sigaction() and sigprocmask() are intercepted. If |
| 1682 | the client program is trying to set a signal handler, Valgrind makes a |
| 1683 | note of the handler address and which signal it is for. Valgrind then |
| 1684 | arranges for the same signal to be delivered to its own handler. |
| 1685 | |
| 1686 | <p>When such a signal arrives, Valgrind's own handler catches it, and |
| 1687 | notes the fact. At a convenient safe point in execution, Valgrind |
| 1688 | builds a signal delivery frame on the client's stack and runs its |
| 1689 | handler. If the handler longjmp()s, there is nothing more to be said. |
| 1690 | If the handler returns, Valgrind notices this, zaps the delivery |
| 1691 | frame, and carries on where it left off before delivering the signal. |
| 1692 | |
| 1693 | <p>The purpose of this nonsense is that setting signal handlers |
| 1694 | essentially amounts to giving callback addresses to the Linux kernel. |
| 1695 | We can't allow this to happen, because if it did, signal handlers |
| 1696 | would run on the real CPU, not the simulated one. This means the |
| 1697 | checking machinery would not operate during the handler run, and, |
| 1698 | worse, memory permissions maps would not be updated, which could cause |
| 1699 | spurious error reports once the handler had returned. |
| 1700 | |
| 1701 | <p>An even worse thing would happen if the signal handler longjmp'd |
| 1702 | rather than returned: Valgrind would completely lose control of the |
| 1703 | client program. |
| 1704 | |
| 1705 | <p>Upshot: we can't allow the client to install signal handlers |
| 1706 | directly. Instead, Valgrind must catch, on behalf of the client, any |
| 1707 | signal the client asks to catch, and must delivery it to the client on |
| 1708 | the simulated CPU, not the real one. This involves considerable |
| 1709 | gruesome fakery; see vg_signals.c for details. |
| 1710 | <p> |
| 1711 | |
| 1712 | <hr width="100%"> |
| 1713 | |
| 1714 | <a name="example"></a> |
| 1715 | <h2>6 Example</h2> |
| 1716 | This is the log for a run of a small program. The program is in fact |
| 1717 | correct, and the reported error is as the result of a potentially serious |
| 1718 | code generation bug in GNU g++ (snapshot 20010527). |
| 1719 | <pre> |
| 1720 | sewardj@phoenix:~/newmat10$ |
| 1721 | ~/Valgrind-6/valgrind -v ./bogon |
| 1722 | ==25832== Valgrind 0.10, a memory error detector for x86 RedHat 7.1. |
| 1723 | ==25832== Copyright (C) 2000-2001, and GNU GPL'd, by Julian Seward. |
| 1724 | ==25832== Startup, with flags: |
| 1725 | ==25832== --suppressions=/home/sewardj/Valgrind/redhat71.supp |
| 1726 | ==25832== reading syms from /lib/ld-linux.so.2 |
| 1727 | ==25832== reading syms from /lib/libc.so.6 |
| 1728 | ==25832== reading syms from /mnt/pima/jrs/Inst/lib/libgcc_s.so.0 |
| 1729 | ==25832== reading syms from /lib/libm.so.6 |
| 1730 | ==25832== reading syms from /mnt/pima/jrs/Inst/lib/libstdc++.so.3 |
| 1731 | ==25832== reading syms from /home/sewardj/Valgrind/valgrind.so |
| 1732 | ==25832== reading syms from /proc/self/exe |
| 1733 | ==25832== loaded 5950 symbols, 142333 line number locations |
| 1734 | ==25832== |
| 1735 | ==25832== Invalid read of size 4 |
| 1736 | ==25832== at 0x8048724: _ZN10BandMatrix6ReSizeEiii (bogon.cpp:45) |
| 1737 | ==25832== by 0x80487AF: main (bogon.cpp:66) |
| 1738 | ==25832== by 0x40371E5E: __libc_start_main (libc-start.c:129) |
| 1739 | ==25832== by 0x80485D1: (within /home/sewardj/newmat10/bogon) |
| 1740 | ==25832== Address 0xBFFFF74C is not stack'd, malloc'd or free'd |
| 1741 | ==25832== |
| 1742 | ==25832== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) |
| 1743 | ==25832== malloc/free: in use at exit: 0 bytes in 0 blocks. |
| 1744 | ==25832== malloc/free: 0 allocs, 0 frees, 0 bytes allocated. |
| 1745 | ==25832== For a detailed leak analysis, rerun with: --leak-check=yes |
| 1746 | ==25832== |
| 1747 | ==25832== exiting, did 1881 basic blocks, 0 misses. |
| 1748 | ==25832== 223 translations, 3626 bytes in, 56801 bytes out. |
| 1749 | </pre> |
| 1750 | <p>The GCC folks fixed this about a week before gcc-3.0 shipped. |
| 1751 | <hr width="100%"> |
| 1752 | <p> |
| 1753 | </body> |
| 1754 | </html> |