1 <html> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 4 <title>8.DRD: a thread error detector</title> 5 <link rel="stylesheet" href="vg_basic.css" type="text/css"> 6 <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> 7 <link rel="home" href="index.html" title="Valgrind Documentation"> 8 <link rel="up" href="manual.html" title="Valgrind User Manual"> 9 <link rel="prev" href="hg-manual.html" title="7.Helgrind: a thread error detector"> 10 <link rel="next" href="ms-manual.html" title="9.Massif: a heap profiler"> 11 </head> 12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13 <div><table class="nav" width="100%" cellspacing="3" cellpadding="3" border="0" summary="Navigation header"><tr> 14 <td width="22px" align="center" valign="middle"><a accesskey="p" href="hg-manual.html"><img src="images/prev.png" width="18" height="21" border="0" alt="Prev"></a></td> 15 <td width="25px" align="center" valign="middle"><a accesskey="u" href="manual.html"><img src="images/up.png" width="21" height="18" border="0" alt="Up"></a></td> 16 <td width="31px" align="center" valign="middle"><a accesskey="h" href="index.html"><img src="images/home.png" width="27" height="20" border="0" alt="Up"></a></td> 17 <th align="center" valign="middle">Valgrind User Manual</th> 18 <td width="22px" align="center" valign="middle"><a accesskey="n" href="ms-manual.html"><img src="images/next.png" width="18" height="21" border="0" alt="Next"></a></td> 19 </tr></table></div> 20 <div class="chapter" title="8.DRD: a thread error detector"> 21 <div class="titlepage"><div><div><h2 class="title"> 22 <a name="drd-manual"></a>8.DRD: a thread error detector</h2></div></div></div> 23 <div class="toc"> 24 <p><b>Table of Contents</b></p> 25 <dl> 26 <dt><span class="sect1"><a href="drd-manual.html#drd-manual.overview">8.1. Overview</a></span></dt> 27 <dd><dl> 28 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.mt-progr-models">8.1.1. Multithreaded Programming Paradigms</a></span></dt> 29 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.pthreads-model">8.1.2. POSIX Threads Programming Model</a></span></dt> 30 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.mt-problems">8.1.3. Multithreaded Programming Problems</a></span></dt> 31 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.data-race-detection">8.1.4. Data Race Detection</a></span></dt> 32 </dl></dd> 33 <dt><span class="sect1"><a href="drd-manual.html#drd-manual.using-drd">8.2. Using DRD</a></span></dt> 34 <dd><dl> 35 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.options">8.2.1. DRD Command-line Options</a></span></dt> 36 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.data-races">8.2.2. Detected Errors: Data Races</a></span></dt> 37 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.lock-contention">8.2.3. Detected Errors: Lock Contention</a></span></dt> 38 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.api-checks">8.2.4. Detected Errors: Misuse of the POSIX threads API</a></span></dt> 39 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.clientreqs">8.2.5. Client Requests</a></span></dt> 40 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.gnome">8.2.6. Debugging GNOME Programs</a></span></dt> 41 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.qt">8.2.7. Debugging Qt Programs</a></span></dt> 42 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.boost.thread">8.2.8. Debugging Boost.Thread Programs</a></span></dt> 43 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.openmp">8.2.9. Debugging OpenMP Programs</a></span></dt> 44 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.cust-mem-alloc">8.2.10. DRD and Custom Memory Allocators</a></span></dt> 45 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.drd-versus-memcheck">8.2.11. DRD Versus Memcheck</a></span></dt> 46 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.resource-requirements">8.2.12. Resource Requirements</a></span></dt> 47 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.effective-use">8.2.13. Hints and Tips for Effective Use of DRD</a></span></dt> 48 </dl></dd> 49 <dt><span class="sect1"><a href="drd-manual.html#drd-manual.Pthreads">8.3. Using the POSIX Threads API Effectively</a></span></dt> 50 <dd><dl> 51 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.mutex-types">8.3.1. Mutex types</a></span></dt> 52 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.condvar">8.3.2. Condition variables</a></span></dt> 53 <dt><span class="sect2"><a href="drd-manual.html#drd-manual.pctw">8.3.3. pthread_cond_timedwait and timeouts</a></span></dt> 54 </dl></dd> 55 <dt><span class="sect1"><a href="drd-manual.html#drd-manual.limitations">8.4. Limitations</a></span></dt> 56 <dt><span class="sect1"><a href="drd-manual.html#drd-manual.feedback">8.5. Feedback</a></span></dt> 57 </dl> 58 </div> 59 <p>To use this tool, you must specify 60 <code class="option">--tool=drd</code> 61 on the Valgrind command line.</p> 62 <div class="sect1" title="8.1.Overview"> 63 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> 64 <a name="drd-manual.overview"></a>8.1.Overview</h2></div></div></div> 65 <p> 66 DRD is a Valgrind tool for detecting errors in multithreaded C and C++ 67 programs. The tool works for any program that uses the POSIX threading 68 primitives or that uses threading concepts built on top of the POSIX threading 69 primitives. 70 </p> 71 <div class="sect2" title="8.1.1.Multithreaded Programming Paradigms"> 72 <div class="titlepage"><div><div><h3 class="title"> 73 <a name="drd-manual.mt-progr-models"></a>8.1.1.Multithreaded Programming Paradigms</h3></div></div></div> 74 <p> 75 There are two possible reasons for using multithreading in a program: 76 </p> 77 <div class="itemizedlist"><ul class="itemizedlist" type="disc"> 78 <li class="listitem"><p> 79 To model concurrent activities. Assigning one thread to each activity 80 can be a great simplification compared to multiplexing the states of 81 multiple activities in a single thread. This is why most server software 82 and embedded software is multithreaded. 83 </p></li> 84 <li class="listitem"><p> 85 To use multiple CPU cores simultaneously for speeding up 86 computations. This is why many High Performance Computing (HPC) 87 applications are multithreaded. 88 </p></li> 89 </ul></div> 90 <p> 91 </p> 92 <p> 93 Multithreaded programs can use one or more of the following programming 94 paradigms. Which paradigm is appropriate depends e.g. on the application type. 95 Some examples of multithreaded programming paradigms are: 96 </p> 97 <div class="itemizedlist"><ul class="itemizedlist" type="disc"> 98 <li class="listitem"><p> 99 Locking. Data that is shared over threads is protected from concurrent 100 accesses via locking. E.g. the POSIX threads library, the Qt library 101 and the Boost.Thread library support this paradigm directly. 102 </p></li> 103 <li class="listitem"><p> 104 Message passing. No data is shared between threads, but threads exchange 105 data by passing messages to each other. Examples of implementations of 106 the message passing paradigm are MPI and CORBA. 107 </p></li> 108 <li class="listitem"><p> 109 Automatic parallelization. A compiler converts a sequential program into 110 a multithreaded program. The original program may or may not contain 111 parallelization hints. One example of such parallelization hints is the 112 OpenMP standard. In this standard a set of directives are defined which 113 tell a compiler how to parallelize a C, C++ or Fortran program. OpenMP 114 is well suited for computational intensive applications. As an example, 115 an open source image processing software package is using OpenMP to 116 maximize performance on systems with multiple CPU 117 cores. GCC supports the 118 OpenMP standard from version 4.2.0 on. 119 </p></li> 120 <li class="listitem"><p> 121 Software Transactional Memory (STM). Any data that is shared between 122 threads is updated via transactions. After each transaction it is 123 verified whether there were any conflicting transactions. If there were 124 conflicts, the transaction is aborted, otherwise it is committed. This 125 is a so-called optimistic approach. There is a prototype of the Intel C++ 126 Compiler available that supports STM. Research about the addition of 127 STM support to GCC is ongoing. 128 </p></li> 129 </ul></div> 130 <p> 131 </p> 132 <p> 133 DRD supports any combination of multithreaded programming paradigms as 134 long as the implementation of these paradigms is based on the POSIX 135 threads primitives. DRD however does not support programs that use 136 e.g. Linux' futexes directly. Attempts to analyze such programs with 137 DRD will cause DRD to report many false positives. 138 </p> 139 </div> 140 <div class="sect2" title="8.1.2.POSIX Threads Programming Model"> 141 <div class="titlepage"><div><div><h3 class="title"> 142 <a name="drd-manual.pthreads-model"></a>8.1.2.POSIX Threads Programming Model</h3></div></div></div> 143 <p> 144 POSIX threads, also known as Pthreads, is the most widely available 145 threading library on Unix systems. 146 </p> 147 <p> 148 The POSIX threads programming model is based on the following abstractions: 149 </p> 150 <div class="itemizedlist"><ul class="itemizedlist" type="disc"> 151 <li class="listitem"><p> 152 A shared address space. All threads running within the same 153 process share the same address space. All data, whether shared or 154 not, is identified by its address. 155 </p></li> 156 <li class="listitem"><p> 157 Regular load and store operations, which allow to read values 158 from or to write values to the memory shared by all threads 159 running in the same process. 160 </p></li> 161 <li class="listitem"><p> 162 Atomic store and load-modify-store operations. While these are 163 not mentioned in the POSIX threads standard, most 164 microprocessors support atomic memory operations. 165 </p></li> 166 <li class="listitem"><p> 167 Threads. Each thread represents a concurrent activity. 168 </p></li> 169 <li class="listitem"><p> 170 Synchronization objects and operations on these synchronization 171 objects. The following types of synchronization objects have been 172 defined in the POSIX threads standard: mutexes, condition variables, 173 semaphores, reader-writer synchronization objects, barriers and 174 spinlocks. 175 </p></li> 176 </ul></div> 177 <p> 178 </p> 179 <p> 180 Which source code statements generate which memory accesses depends on 181 the <span class="emphasis"><em>memory model</em></span> of the programming language being 182 used. There is not yet a definitive memory model for the C and C++ 183 languages. For a draft memory model, see also the document 184 <a class="ulink" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2338.html" target="_top"> 185 WG21/N2338: Concurrency memory model compiler consequences</a>. 186 </p> 187 <p> 188 For more information about POSIX threads, see also the Single UNIX 189 Specification version 3, also known as 190 <a class="ulink" href="http://www.opengroup.org/onlinepubs/000095399/idx/threads.html" target="_top"> 191 IEEE Std 1003.1</a>. 192 </p> 193 </div> 194 <div class="sect2" title="8.1.3.Multithreaded Programming Problems"> 195 <div class="titlepage"><div><div><h3 class="title"> 196 <a name="drd-manual.mt-problems"></a>8.1.3.Multithreaded Programming Problems</h3></div></div></div> 197 <p> 198 Depending on which multithreading paradigm is being used in a program, 199 one or more of the following problems can occur: 200 </p> 201 <div class="itemizedlist"><ul class="itemizedlist" type="disc"> 202 <li class="listitem"><p> 203 Data races. One or more threads access the same memory location without 204 sufficient locking. Most but not all data races are programming errors 205 and are the cause of subtle and hard-to-find bugs. 206 </p></li> 207 <li class="listitem"><p> 208 Lock contention. One thread blocks the progress of one or more other 209 threads by holding a lock too long. 210 </p></li> 211 <li class="listitem"><p> 212 Improper use of the POSIX threads API. Most implementations of the POSIX 213 threads API have been optimized for runtime speed. Such implementations 214 will not complain on certain errors, e.g. when a mutex is being unlocked 215 by another thread than the thread that obtained a lock on the mutex. 216 </p></li> 217 <li class="listitem"><p> 218 Deadlock. A deadlock occurs when two or more threads wait for 219 each other indefinitely. 220 </p></li> 221 <li class="listitem"><p> 222 False sharing. If threads that run on different processor cores 223 access different variables located in the same cache line 224 frequently, this will slow down the involved threads a lot due 225 to frequent exchange of cache lines. 226 </p></li> 227 </ul></div> 228 <p> 229 </p> 230 <p> 231 Although the likelihood of the occurrence of data races can be reduced 232 through a disciplined programming style, a tool for automatic 233 detection of data races is a necessity when developing multithreaded 234 software. DRD can detect these, as well as lock contention and 235 improper use of the POSIX threads API. 236 </p> 237 </div> 238 <div class="sect2" title="8.1.4.Data Race Detection"> 239 <div class="titlepage"><div><div><h3 class="title"> 240 <a name="drd-manual.data-race-detection"></a>8.1.4.Data Race Detection</h3></div></div></div> 241 <p> 242 The result of load and store operations performed by a multithreaded program 243 depends on the order in which memory operations are performed. This order is 244 determined by: 245 </p> 246 <div class="orderedlist"><ol class="orderedlist" type="1"> 247 <li class="listitem"><p> 248 All memory operations performed by the same thread are performed in 249 <span class="emphasis"><em>program order</em></span>, that is, the order determined by the 250 program source code and the results of previous load operations. 251 </p></li> 252 <li class="listitem"><p> 253 Synchronization operations determine certain ordering constraints on 254 memory operations performed by different threads. These ordering 255 constraints are called the <span class="emphasis"><em>synchronization order</em></span>. 256 </p></li> 257 </ol></div> 258 <p> 259 The combination of program order and synchronization order is called the 260 <span class="emphasis"><em>happens-before relationship</em></span>. This concept was first 261 defined by S. Adve et al in the paper <span class="emphasis"><em>Detecting data races on weak 262 memory systems</em></span>, ACM SIGARCH Computer Architecture News, v.19 n.3, 263 p.234-243, May 1991. 264 </p> 265 <p> 266 Two memory operations <span class="emphasis"><em>conflict</em></span> if both operations are 267 performed by different threads, refer to the same memory location and at least 268 one of them is a store operation. 269 </p> 270 <p> 271 A multithreaded program is <span class="emphasis"><em>data-race free</em></span> if all 272 conflicting memory accesses are ordered by synchronization 273 operations. 274 </p> 275 <p> 276 A well known way to ensure that a multithreaded program is data-race 277 free is to ensure that a locking discipline is followed. It is e.g. 278 possible to associate a mutex with each shared data item, and to hold 279 a lock on the associated mutex while the shared data is accessed. 280 </p> 281 <p> 282 All programs that follow a locking discipline are data-race free, but not all 283 data-race free programs follow a locking discipline. There exist multithreaded 284 programs where access to shared data is arbitrated via condition variables, 285 semaphores or barriers. As an example, a certain class of HPC applications 286 consists of a sequence of computation steps separated in time by barriers, and 287 where these barriers are the only means of synchronization. Although there are 288 many conflicting memory accesses in such applications and although such 289 applications do not make use mutexes, most of these applications do not 290 contain data races. 291 </p> 292 <p> 293 There exist two different approaches for verifying the correctness of 294 multithreaded programs at runtime. The approach of the so-called Eraser 295 algorithm is to verify whether all shared memory accesses follow a consistent 296 locking strategy. And the happens-before data race detectors verify directly 297 whether all interthread memory accesses are ordered by synchronization 298 operations. While the last approach is more complex to implement, and while it 299 is more sensitive to OS scheduling, it is a general approach that works for 300 all classes of multithreaded programs. An important advantage of 301 happens-before data race detectors is that these do not report any false 302 positives. 303 </p> 304 <p> 305 DRD is based on the happens-before algorithm. 306 </p> 307 </div> 308 </div> 309 <div class="sect1" title="8.2.Using DRD"> 310 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> 311 <a name="drd-manual.using-drd"></a>8.2.Using DRD</h2></div></div></div> 312 <div class="sect2" title="8.2.1.DRD Command-line Options"> 313 <div class="titlepage"><div><div><h3 class="title"> 314 <a name="drd-manual.options"></a>8.2.1.DRD Command-line Options</h3></div></div></div> 315 <p>The following command-line options are available for controlling the 316 behavior of the DRD tool itself:</p> 317 <div class="variablelist"> 318 <a name="drd.opts.list"></a><dl> 319 <dt><span class="term"> 320 <code class="option">--check-stack-var=<yes|no> [default: no]</code> 321 </span></dt> 322 <dd><p> 323 Controls whether DRD detects data races on stack 324 variables. Verifying stack variables is disabled by default because 325 most programs do not share stack variables over threads. 326 </p></dd> 327 <dt><span class="term"> 328 <code class="option">--exclusive-threshold=<n> [default: off]</code> 329 </span></dt> 330 <dd><p> 331 Print an error message if any mutex or writer lock has been 332 held longer than the time specified in milliseconds. This 333 option enables the detection of lock contention. 334 </p></dd> 335 <dt><span class="term"> 336 <code class="option"> 337 --first-race-only=<yes|no> [default: no] 338 </code> 339 </span></dt> 340 <dd><p> 341 Whether to report only the first data race that has been detected on a 342 memory location or all data races that have been detected on a memory 343 location. 344 </p></dd> 345 <dt><span class="term"> 346 <code class="option"> 347 --free-is-write=<yes|no> [default: no] 348 </code> 349 </span></dt> 350 <dd><p> 351 Whether to report accessing freed memory as a race. Helps to detect 352 memory accesses that occur after memory has been freed but might cause 353 DRD to run slightly slower. 354 </p></dd> 355 <dt><span class="term"> 356 <code class="option"> 357 --report-signal-unlocked=<yes|no> [default: yes] 358 </code> 359 </span></dt> 360 <dd><p> 361 Whether to report calls to 362 <code class="function">pthread_cond_signal</code> and 363 <code class="function">pthread_cond_broadcast</code> where the mutex 364 associated with the signal through 365 <code class="function">pthread_cond_wait</code> or 366 <code class="function">pthread_cond_timed_wait</code>is not locked at 367 the time the signal is sent. Sending a signal without holding 368 a lock on the associated mutex is a common programming error 369 which can cause subtle race conditions and unpredictable 370 behavior. There exist some uncommon synchronization patterns 371 however where it is safe to send a signal without holding a 372 lock on the associated mutex. 373 </p></dd> 374 <dt><span class="term"> 375 <code class="option">--segment-merging=<yes|no> [default: yes]</code> 376 </span></dt> 377 <dd><p> 378 Controls segment merging. Segment merging is an algorithm to 379 limit memory usage of the data race detection 380 algorithm. Disabling segment merging may improve the accuracy 381 of the so-called 'other segments' displayed in race reports 382 but can also trigger an out of memory error. 383 </p></dd> 384 <dt><span class="term"> 385 <code class="option">--segment-merging-interval=<n> [default: 10]</code> 386 </span></dt> 387 <dd><p> 388 Perform segment merging only after the specified number of new 389 segments have been created. This is an advanced configuration option 390 that allows to choose whether to minimize DRD's memory usage by 391 choosing a low value or to let DRD run faster by choosing a slightly 392 higher value. The optimal value for this parameter depends on the 393 program being analyzed. The default value works well for most programs. 394 </p></dd> 395 <dt><span class="term"> 396 <code class="option">--shared-threshold=<n> [default: off]</code> 397 </span></dt> 398 <dd><p> 399 Print an error message if a reader lock has been held longer 400 than the specified time (in milliseconds). This option enables 401 the detection of lock contention. 402 </p></dd> 403 <dt><span class="term"> 404 <code class="option">--show-confl-seg=<yes|no> [default: yes]</code> 405 </span></dt> 406 <dd><p> 407 Show conflicting segments in race reports. Since this 408 information can help to find the cause of a data race, this 409 option is enabled by default. Disabling this option makes the 410 output of DRD more compact. 411 </p></dd> 412 <dt><span class="term"> 413 <code class="option">--show-stack-usage=<yes|no> [default: no]</code> 414 </span></dt> 415 <dd><p> 416 Print stack usage at thread exit time. When a program creates a large 417 number of threads it becomes important to limit the amount of virtual 418 memory allocated for thread stacks. This option makes it possible to 419 observe how much stack memory has been used by each thread of the the 420 client program. Note: the DRD tool itself allocates some temporary 421 data on the client thread stack. The space necessary for this 422 temporary data must be allocated by the client program when it 423 allocates stack memory, but is not included in stack usage reported by 424 DRD. 425 </p></dd> 426 </dl> 427 </div> 428 <p> 429 The following options are available for monitoring the behavior of the 430 client program: 431 </p> 432 <div class="variablelist"> 433 <a name="drd.debugopts.list"></a><dl> 434 <dt><span class="term"> 435 <code class="option">--trace-addr=<address> [default: none]</code> 436 </span></dt> 437 <dd><p> 438 Trace all load and store activity for the specified 439 address. This option may be specified more than once. 440 </p></dd> 441 <dt><span class="term"> 442 <code class="option">--trace-alloc=<yes|no> [default: no]</code> 443 </span></dt> 444 <dd><p> 445 Trace all memory allocations and deallocations. May produce a huge 446 amount of output. 447 </p></dd> 448 <dt><span class="term"> 449 <code class="option">--trace-barrier=<yes|no> [default: no]</code> 450 </span></dt> 451 <dd><p> 452 Trace all barrier activity. 453 </p></dd> 454 <dt><span class="term"> 455 <code class="option">--trace-cond=<yes|no> [default: no]</code> 456 </span></dt> 457 <dd><p> 458 Trace all condition variable activity. 459 </p></dd> 460 <dt><span class="term"> 461 <code class="option">--trace-fork-join=<yes|no> [default: no]</code> 462 </span></dt> 463 <dd><p> 464 Trace all thread creation and all thread termination events. 465 </p></dd> 466 <dt><span class="term"> 467 <code class="option">--trace-mutex=<yes|no> [default: no]</code> 468 </span></dt> 469 <dd><p> 470 Trace all mutex activity. 471 </p></dd> 472 <dt><span class="term"> 473 <code class="option">--trace-rwlock=<yes|no> [default: no]</code> 474 </span></dt> 475 <dd><p> 476 Trace all reader-writer lock activity. 477 </p></dd> 478 <dt><span class="term"> 479 <code class="option">--trace-semaphore=<yes|no> [default: no]</code> 480 </span></dt> 481 <dd><p> 482 Trace all semaphore activity. 483 </p></dd> 484 </dl> 485 </div> 486 </div> 487 <div class="sect2" title="8.2.2.Detected Errors: Data Races"> 488 <div class="titlepage"><div><div><h3 class="title"> 489 <a name="drd-manual.data-races"></a>8.2.2.Detected Errors: Data Races</h3></div></div></div> 490 <p> 491 DRD prints a message every time it detects a data race. Please keep 492 the following in mind when interpreting DRD's output: 493 </p> 494 <div class="itemizedlist"><ul class="itemizedlist" type="disc"> 495 <li class="listitem"><p> 496 Every thread is assigned a <span class="emphasis"><em>thread ID</em></span> by the DRD 497 tool. A thread ID is a number. Thread ID's start at one and are never 498 recycled. 499 </p></li> 500 <li class="listitem"><p> 501 The term <span class="emphasis"><em>segment</em></span> refers to a consecutive 502 sequence of load, store and synchronization operations, all 503 issued by the same thread. A segment always starts and ends at a 504 synchronization operation. Data race analysis is performed 505 between segments instead of between individual load and store 506 operations because of performance reasons. 507 </p></li> 508 <li class="listitem"><p> 509 There are always at least two memory accesses involved in a data 510 race. Memory accesses involved in a data race are called 511 <span class="emphasis"><em>conflicting memory accesses</em></span>. DRD prints a 512 report for each memory access that conflicts with a past memory 513 access. 514 </p></li> 515 </ul></div> 516 <p> 517 </p> 518 <p> 519 Below you can find an example of a message printed by DRD when it 520 detects a data race: 521 </p> 522 <pre class="programlisting"> 523 $ valgrind --tool=drd --read-var-info=yes drd/tests/rwlock_race 524 ... 525 ==9466== Thread 3: 526 ==9466== Conflicting load by thread 3 at 0x006020b8 size 4 527 ==9466== at 0x400B6C: thread_func (rwlock_race.c:29) 528 ==9466== by 0x4C291DF: vg_thread_wrapper (drd_pthread_intercepts.c:186) 529 ==9466== by 0x4E3403F: start_thread (in /lib64/libpthread-2.8.so) 530 ==9466== by 0x53250CC: clone (in /lib64/libc-2.8.so) 531 ==9466== Location 0x6020b8 is 0 bytes inside local var "s_racy" 532 ==9466== declared at rwlock_race.c:18, in frame #0 of thread 3 533 ==9466== Other segment start (thread 2) 534 ==9466== at 0x4C2847D: pthread_rwlock_rdlock* (drd_pthread_intercepts.c:813) 535 ==9466== by 0x400B6B: thread_func (rwlock_race.c:28) 536 ==9466== by 0x4C291DF: vg_thread_wrapper (drd_pthread_intercepts.c:186) 537 ==9466== by 0x4E3403F: start_thread (in /lib64/libpthread-2.8.so) 538 ==9466== by 0x53250CC: clone (in /lib64/libc-2.8.so) 539 ==9466== Other segment end (thread 2) 540 ==9466== at 0x4C28B54: pthread_rwlock_unlock* (drd_pthread_intercepts.c:912) 541 ==9466== by 0x400B84: thread_func (rwlock_race.c:30) 542 ==9466== by 0x4C291DF: vg_thread_wrapper (drd_pthread_intercepts.c:186) 543 ==9466== by 0x4E3403F: start_thread (in /lib64/libpthread-2.8.so) 544 ==9466== by 0x53250CC: clone (in /lib64/libc-2.8.so) 545 ... 546 </pre> 547 <p> 548 The above report has the following meaning: 549 </p> 550 <div class="itemizedlist"><ul class="itemizedlist" type="disc"> 551 <li class="listitem"><p> 552 The number in the column on the left is the process ID of the 553 process being analyzed by DRD. 554 </p></li> 555 <li class="listitem"><p> 556 The first line ("Thread 3") tells you the thread ID for 557 the thread in which context the data race has been detected. 558 </p></li> 559 <li class="listitem"><p> 560 The next line tells which kind of operation was performed (load or 561 store) and by which thread. On the same line the start address and the 562 number of bytes involved in the conflicting access are also displayed. 563 </p></li> 564 <li class="listitem"><p> 565 Next, the call stack of the conflicting access is displayed. If 566 your program has been compiled with debug information 567 (<code class="option">-g</code>), this call stack will include file names and 568 line numbers. The two 569 bottommost frames in this call stack (<code class="function">clone</code> 570 and <code class="function">start_thread</code>) show how the NPTL starts 571 a thread. The third frame 572 (<code class="function">vg_thread_wrapper</code>) is added by DRD. The 573 fourth frame (<code class="function">thread_func</code>) is the first 574 interesting line because it shows the thread entry point, that 575 is the function that has been passed as the third argument to 576 <code class="function">pthread_create</code>. 577 </p></li> 578 <li class="listitem"><p> 579 Next, the allocation context for the conflicting address is 580 displayed. For dynamically allocated data the allocation call 581 stack is shown. For static variables and stack variables the 582 allocation context is only shown when the option 583 <code class="option">--read-var-info=yes</code> has been 584 specified. Otherwise DRD will print <code class="computeroutput">Allocation 585 context: unknown</code>. 586 </p></li> 587 <li class="listitem"> 588 <p> 589 A conflicting access involves at least two memory accesses. For 590 one of these accesses an exact call stack is displayed, and for 591 the other accesses an approximate call stack is displayed, 592 namely the start and the end of the segments of the other 593 accesses. This information can be interpreted as follows: 594 </p> 595 <div class="orderedlist"><ol class="orderedlist" type="1"> 596 <li class="listitem"><p> 597 Start at the bottom of both call stacks, and count the 598 number stack frames with identical function name, file 599 name and line number. In the above example the three 600 bottommost frames are identical 601 (<code class="function">clone</code>, 602 <code class="function">start_thread</code> and 603 <code class="function">vg_thread_wrapper</code>). 604 </p></li> 605 <li class="listitem"><p> 606 The next higher stack frame in both call stacks now tells 607 you between in which source code region the other memory 608 access happened. The above output tells that the other 609 memory access involved in the data race happened between 610 source code lines 28 and 30 in file 611 <code class="computeroutput">rwlock_race.c</code>. 612 </p></li> 613 </ol></div> 614 <p> 615 </p> 616 </li> 617 </ul></div> 618 <p> 619 </p> 620 </div> 621 <div class="sect2" title="8.2.3.Detected Errors: Lock Contention"> 622 <div class="titlepage"><div><div><h3 class="title"> 623 <a name="drd-manual.lock-contention"></a>8.2.3.Detected Errors: Lock Contention</h3></div></div></div> 624 <p> 625 Threads must be able to make progress without being blocked for too long by 626 other threads. Sometimes a thread has to wait until a mutex or reader-writer 627 synchronization object is unlocked by another thread. This is called 628 <span class="emphasis"><em>lock contention</em></span>. 629 </p> 630 <p> 631 Lock contention causes delays. Such delays should be as short as 632 possible. The two command line options 633 <code class="literal">--exclusive-threshold=<n></code> and 634 <code class="literal">--shared-threshold=<n></code> make it possible to 635 detect excessive lock contention by making DRD report any lock that 636 has been held longer than the specified threshold. An example: 637 </p> 638 <pre class="programlisting"> 639 $ valgrind --tool=drd --exclusive-threshold=10 drd/tests/hold_lock -i 500 640 ... 641 ==10668== Acquired at: 642 ==10668== at 0x4C267C8: pthread_mutex_lock (drd_pthread_intercepts.c:395) 643 ==10668== by 0x400D92: main (hold_lock.c:51) 644 ==10668== Lock on mutex 0x7fefffd50 was held during 503 ms (threshold: 10 ms). 645 ==10668== at 0x4C26ADA: pthread_mutex_unlock (drd_pthread_intercepts.c:441) 646 ==10668== by 0x400DB5: main (hold_lock.c:55) 647 ... 648 </pre> 649 <p> 650 The <code class="literal">hold_lock</code> test program holds a lock as long as 651 specified by the <code class="literal">-i</code> (interval) argument. The DRD 652 output reports that the lock acquired at line 51 in source file 653 <code class="literal">hold_lock.c</code> and released at line 55 was held during 654 503 ms, while a threshold of 10 ms was specified to DRD. 655 </p> 656 </div> 657 <div class="sect2" title="8.2.4.Detected Errors: Misuse of the POSIX threads API"> 658 <div class="titlepage"><div><div><h3 class="title"> 659 <a name="drd-manual.api-checks"></a>8.2.4.Detected Errors: Misuse of the POSIX threads API</h3></div></div></div> 660 <p> 661 DRD is able to detect and report the following misuses of the POSIX 662 threads API: 663 </p> 664 <div class="itemizedlist"><ul class="itemizedlist" type="disc"> 665 <li class="listitem"><p> 666 Passing the address of one type of synchronization object 667 (e.g. a mutex) to a POSIX API call that expects a pointer to 668 another type of synchronization object (e.g. a condition 669 variable). 670 </p></li> 671 <li class="listitem"><p> 672 Attempts to unlock a mutex that has not been locked. 673 </p></li> 674 <li class="listitem"><p> 675 Attempts to unlock a mutex that was locked by another thread. 676 </p></li> 677 <li class="listitem"><p> 678 Attempts to lock a mutex of type 679 <code class="literal">PTHREAD_MUTEX_NORMAL</code> or a spinlock 680 recursively. 681 </p></li> 682 <li class="listitem"><p> 683 Destruction or deallocation of a locked mutex. 684 </p></li> 685 <li class="listitem"><p> 686 Sending a signal to a condition variable while no lock is held 687 on the mutex associated with the condition variable. 688 </p></li> 689 <li class="listitem"><p> 690 Calling <code class="function">pthread_cond_wait</code> on a mutex 691 that is not locked, that is locked by another thread or that 692 has been locked recursively. 693 </p></li> 694 <li class="listitem"><p> 695 Associating two different mutexes with a condition variable 696 through <code class="function">pthread_cond_wait</code>. 697 </p></li> 698 <li class="listitem"><p> 699 Destruction or deallocation of a condition variable that is 700 being waited upon. 701 </p></li> 702 <li class="listitem"><p> 703 Destruction or deallocation of a locked reader-writer synchronization 704 object. 705 </p></li> 706 <li class="listitem"><p> 707 Attempts to unlock a reader-writer synchronization object that was not 708 locked by the calling thread. 709 </p></li> 710 <li class="listitem"><p> 711 Attempts to recursively lock a reader-writer synchronization object 712 exclusively. 713 </p></li> 714 <li class="listitem"><p> 715 Attempts to pass the address of a user-defined reader-writer 716 synchronization object to a POSIX threads function. 717 </p></li> 718 <li class="listitem"><p> 719 Attempts to pass the address of a POSIX reader-writer synchronization 720 object to one of the annotations for user-defined reader-writer 721 synchronization objects. 722 </p></li> 723 <li class="listitem"><p> 724 Reinitialization of a mutex, condition variable, reader-writer 725 lock, semaphore or barrier. 726 </p></li> 727 <li class="listitem"><p> 728 Destruction or deallocation of a semaphore or barrier that is 729 being waited upon. 730 </p></li> 731 <li class="listitem"><p> 732 Missing synchronization between barrier wait and barrier destruction. 733 </p></li> 734 <li class="listitem"><p> 735 Exiting a thread without first unlocking the spinlocks, mutexes or 736 reader-writer synchronization objects that were locked by that thread. 737 </p></li> 738 <li class="listitem"><p> 739 Passing an invalid thread ID to <code class="function">pthread_join</code> 740 or <code class="function">pthread_cancel</code>. 741 </p></li> 742 </ul></div> 743 <p> 744 </p> 745 </div> 746 <div class="sect2" title="8.2.5.Client Requests"> 747 <div class="titlepage"><div><div><h3 class="title"> 748 <a name="drd-manual.clientreqs"></a>8.2.5.Client Requests</h3></div></div></div> 749 <p> 750 Just as for other Valgrind tools it is possible to let a client program 751 interact with the DRD tool through client requests. In addition to the 752 client requests several macros have been defined that allow to use the 753 client requests in a convenient way. 754 </p> 755 <p> 756 The interface between client programs and the DRD tool is defined in 757 the header file <code class="literal"><valgrind/drd.h></code>. The 758 available macros and client requests are: 759 </p> 760 <div class="itemizedlist"><ul class="itemizedlist" type="disc"> 761 <li class="listitem"><p> 762 The macro <code class="literal">DRD_GET_VALGRIND_THREADID</code> and the 763 corresponding client 764 request <code class="varname">VG_USERREQ__DRD_GET_VALGRIND_THREAD_ID</code>. 765 Query the thread ID that has been assigned by the Valgrind core to the 766 thread executing this client request. Valgrind's thread ID's start at 767 one and are recycled in case a thread stops. 768 </p></li> 769 <li class="listitem"><p> 770 The macro <code class="literal">DRD_GET_DRD_THREADID</code> and the corresponding 771 client request <code class="varname">VG_USERREQ__DRD_GET_DRD_THREAD_ID</code>. 772 Query the thread ID that has been assigned by DRD to the thread 773 executing this client request. These are the thread ID's reported by DRD 774 in data race reports and in trace messages. DRD's thread ID's start at 775 one and are never recycled. 776 </p></li> 777 <li class="listitem"><p> 778 The macros <code class="literal">DRD_IGNORE_VAR(x)</code>, 779 <code class="literal">ANNOTATE_TRACE_MEMORY(&x)</code> and the corresponding 780 client request <code class="varname">VG_USERREQ__DRD_START_SUPPRESSION</code>. Some 781 applications contain intentional races. There exist e.g. applications 782 where the same value is assigned to a shared variable from two different 783 threads. It may be more convenient to suppress such races than to solve 784 these. This client request allows to suppress such races. 785 </p></li> 786 <li class="listitem"><p> 787 The macro <code class="literal">DRD_STOP_IGNORING_VAR(x)</code> and the 788 corresponding client request 789 <code class="varname">VG_USERREQ__DRD_FINISH_SUPPRESSION</code>. Tell DRD 790 to no longer ignore data races for the address range that was suppressed 791 either via the macro <code class="literal">DRD_IGNORE_VAR(x)</code> or via the 792 client request <code class="varname">VG_USERREQ__DRD_START_SUPPRESSION</code>. 793 </p></li> 794 <li class="listitem"><p> 795 The macro <code class="literal">DRD_TRACE_VAR(x)</code>. Trace all load and store 796 activity for the address range starting at <code class="literal">&x</code> and 797 occupying <code class="literal">sizeof(x)</code> bytes. When DRD reports a data 798 race on a specified variable, and it's not immediately clear which 799 source code statements triggered the conflicting accesses, it can be 800 very helpful to trace all activity on the offending memory location. 801 </p></li> 802 <li class="listitem"><p> 803 The macro <code class="literal">ANNOTATE_TRACE_MEMORY(&x)</code>. Trace all 804 load and store activity that touches at least the single byte at the 805 address <code class="literal">&x</code>. 806 </p></li> 807 <li class="listitem"><p> 808 The client request <code class="varname">VG_USERREQ__DRD_START_TRACE_ADDR</code>, 809 which allows to trace all load and store activity for the specified 810 address range. 811 </p></li> 812 <li class="listitem"><p> 813 The client 814 request <code class="varname">VG_USERREQ__DRD_STOP_TRACE_ADDR</code>. Do no longer 815 trace load and store activity for the specified address range. 816 </p></li> 817 <li class="listitem"><p> 818 The macro <code class="literal">ANNOTATE_HAPPENS_BEFORE(addr)</code> tells DRD to 819 insert a mark. Insert this macro just after an access to the variable at 820 the specified address has been performed. 821 </p></li> 822 <li class="listitem"><p> 823 The macro <code class="literal">ANNOTATE_HAPPENS_AFTER(addr)</code> tells DRD that 824 the next access to the variable at the specified address should be 825 considered to have happened after the access just before the latest 826 <code class="literal">ANNOTATE_HAPPENS_BEFORE(addr)</code> annotation that 827 references the same variable. The purpose of these two macros is to tell 828 DRD about the order of inter-thread memory accesses implemented via 829 atomic memory operations. See 830 also <code class="literal">drd/tests/annotate_smart_pointer.cpp</code> for an 831 example. 832 </p></li> 833 <li class="listitem"><p> 834 The macro <code class="literal">ANNOTATE_RWLOCK_CREATE(rwlock)</code> tells DRD 835 that the object at address <code class="literal">rwlock</code> is a 836 reader-writer synchronization object that is not a 837 <code class="literal">pthread_rwlock_t</code> synchronization object. See 838 also <code class="literal">drd/tests/annotate_rwlock.c</code> for an example. 839 </p></li> 840 <li class="listitem"><p> 841 The macro <code class="literal">ANNOTATE_RWLOCK_DESTROY(rwlock)</code> tells DRD 842 that the reader-writer synchronization object at 843 address <code class="literal">rwlock</code> has been destroyed. 844 </p></li> 845 <li class="listitem"><p> 846 The macro <code class="literal">ANNOTATE_WRITERLOCK_ACQUIRED(rwlock)</code> tells 847 DRD that a writer lock has been acquired on the reader-writer 848 synchronization object at address <code class="literal">rwlock</code>. 849 </p></li> 850 <li class="listitem"><p> 851 The macro <code class="literal">ANNOTATE_READERLOCK_ACQUIRED(rwlock)</code> tells 852 DRD that a reader lock has been acquired on the reader-writer 853 synchronization object at address <code class="literal">rwlock</code>. 854 </p></li> 855 <li class="listitem"><p> 856 The macro <code class="literal">ANNOTATE_RWLOCK_ACQUIRED(rwlock, is_w)</code> 857 tells DRD that a writer lock (when <code class="literal">is_w != 0</code>) or that 858 a reader lock (when <code class="literal">is_w == 0</code>) has been acquired on 859 the reader-writer synchronization object at 860 address <code class="literal">rwlock</code>. 861 </p></li> 862 <li class="listitem"><p> 863 The macro <code class="literal">ANNOTATE_WRITERLOCK_RELEASED(rwlock)</code> tells 864 DRD that a writer lock has been released on the reader-writer 865 synchronization object at address <code class="literal">rwlock</code>. 866 </p></li> 867 <li class="listitem"><p> 868 The macro <code class="literal">ANNOTATE_READERLOCK_RELEASED(rwlock)</code> tells 869 DRD that a reader lock has been released on the reader-writer 870 synchronization object at address <code class="literal">rwlock</code>. 871 </p></li> 872 <li class="listitem"><p> 873 The macro <code class="literal">ANNOTATE_RWLOCK_RELEASED(rwlock, is_w)</code> 874 tells DRD that a writer lock (when <code class="literal">is_w != 0</code>) or that 875 a reader lock (when <code class="literal">is_w == 0</code>) has been released on 876 the reader-writer synchronization object at 877 address <code class="literal">rwlock</code>. 878 </p></li> 879 <li class="listitem"><p> 880 The macro <code class="literal">ANNOTATE_BARRIER_INIT(barrier, count, 881 reinitialization_allowed)</code> tells DRD that a new barrier object 882 at the address <code class="literal">barrier</code> has been initialized, 883 that <code class="literal">count</code> threads participate in each barrier and 884 also whether or not barrier reinitialization without intervening 885 destruction should be reported as an error. See 886 also <code class="literal">drd/tests/annotate_barrier.c</code> for an example. 887 </p></li> 888 <li class="listitem"><p> 889 The macro <code class="literal">ANNOTATE_BARRIER_DESTROY(barrier)</code> 890 tells DRD that a barrier object is about to be destroyed. 891 </p></li> 892 <li class="listitem"><p> 893 The macro <code class="literal">ANNOTATE_BARRIER_WAIT_BEFORE(barrier)</code> 894 tells DRD that waiting for a barrier will start. 895 </p></li> 896 <li class="listitem"><p> 897 The macro <code class="literal">ANNOTATE_BARRIER_WAIT_AFTER(barrier)</code> 898 tells DRD that waiting for a barrier has finished. 899 </p></li> 900 <li class="listitem"><p> 901 The macro <code class="literal">ANNOTATE_BENIGN_RACE_SIZED(addr, size, 902 descr)</code> tells DRD that any races detected on the specified 903 address are benign and hence should not be 904 reported. The <code class="literal">descr</code> argument is ignored but can be 905 used to document why data races on <code class="literal">addr</code> are benign. 906 </p></li> 907 <li class="listitem"><p> 908 The macro <code class="literal">ANNOTATE_BENIGN_RACE_STATIC(var, descr)</code> 909 tells DRD that any races detected on the specified static variable are 910 benign and hence should not be reported. The <code class="literal">descr</code> 911 argument is ignored but can be used to document why data races 912 on <code class="literal">var</code> are benign. Note: this macro can only be 913 used in C++ programs and not in C programs. 914 </p></li> 915 <li class="listitem"><p> 916 The macro <code class="literal">ANNOTATE_IGNORE_READS_BEGIN</code> tells 917 DRD to ignore all memory loads performed by the current thread. 918 </p></li> 919 <li class="listitem"><p> 920 The macro <code class="literal">ANNOTATE_IGNORE_READS_END</code> tells 921 DRD to stop ignoring the memory loads performed by the current thread. 922 </p></li> 923 <li class="listitem"><p> 924 The macro <code class="literal">ANNOTATE_IGNORE_WRITES_BEGIN</code> tells 925 DRD to ignore all memory stores performed by the current thread. 926 </p></li> 927 <li class="listitem"><p> 928 The macro <code class="literal">ANNOTATE_IGNORE_WRITES_END</code> tells 929 DRD to stop ignoring the memory stores performed by the current thread. 930 </p></li> 931 <li class="listitem"><p> 932 The macro <code class="literal">ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN</code> tells 933 DRD to ignore all memory accesses performed by the current thread. 934 </p></li> 935 <li class="listitem"><p> 936 The macro <code class="literal">ANNOTATE_IGNORE_READS_AND_WRITES_END</code> tells 937 DRD to stop ignoring the memory accesses performed by the current thread. 938 </p></li> 939 <li class="listitem"><p> 940 The macro <code class="literal">ANNOTATE_NEW_MEMORY(addr, size)</code> tells 941 DRD that the specified memory range has been allocated by a custom 942 memory allocator in the client program and that the client program 943 will start using this memory range. 944 </p></li> 945 <li class="listitem"><p> 946 The macro <code class="literal">ANNOTATE_THREAD_NAME(name)</code> tells DRD to 947 associate the specified name with the current thread and to include this 948 name in the error messages printed by DRD. 949 </p></li> 950 <li class="listitem"><p> 951 The macros <code class="literal">VALGRIND_MALLOCLIKE_BLOCK</code> and 952 <code class="literal">VALGRIND_FREELIKE_BLOCK</code> from the Valgrind core are 953 implemented; they are described in 954 <a class="xref" href="manual-core-adv.html#manual-core-adv.clientreq" title="3.1.The Client Request mechanism">The Client Request mechanism</a>. 955 </p></li> 956 </ul></div> 957 <p> 958 </p> 959 <p> 960 Note: if you compiled Valgrind yourself, the header file 961 <code class="literal"><valgrind/drd.h></code> will have been installed in 962 the directory <code class="literal">/usr/include</code> by the command 963 <code class="literal">make install</code>. If you obtained Valgrind by 964 installing it as a package however, you will probably have to install 965 another package with a name like <code class="literal">valgrind-devel</code> 966 before Valgrind's header files are available. 967 </p> 968 </div> 969 <div class="sect2" title="8.2.6.Debugging GNOME Programs"> 970 <div class="titlepage"><div><div><h3 class="title"> 971 <a name="drd-manual.gnome"></a>8.2.6.Debugging GNOME Programs</h3></div></div></div> 972 <p> 973 GNOME applications use the threading primitives provided by the 974 <code class="computeroutput">glib</code> and 975 <code class="computeroutput">gthread</code> libraries. These libraries 976 are built on top of POSIX threads, and hence are directly supported by 977 DRD. Please keep in mind that you have to call 978 <code class="function">g_thread_init</code> before creating any threads, or 979 DRD will report several data races on glib functions. See also the 980 <a class="ulink" href="http://library.gnome.org/devel/glib/stable/glib-Threads.html" target="_top">GLib 981 Reference Manual</a> for more information about 982 <code class="function">g_thread_init</code>. 983 </p> 984 <p> 985 One of the many facilities provided by the <code class="literal">glib</code> 986 library is a block allocator, called <code class="literal">g_slice</code>. You 987 have to disable this block allocator when using DRD by adding the 988 following to the shell environment variables: 989 <code class="literal">G_SLICE=always-malloc</code>. See also the <a class="ulink" href="http://library.gnome.org/devel/glib/stable/glib-Memory-Slices.html" target="_top">GLib 990 Reference Manual</a> for more information. 991 </p> 992 </div> 993 <div class="sect2" title="8.2.7.Debugging Qt Programs"> 994 <div class="titlepage"><div><div><h3 class="title"> 995 <a name="drd-manual.qt"></a>8.2.7.Debugging Qt Programs</h3></div></div></div> 996 <p> 997 The Qt library is the GUI library used by the KDE project. Currently 998 there are two versions of the Qt library in use: Qt3 by KDE 3 and Qt4 999 by KDE 4. If possible, use Qt4 instead of Qt3. Qt3 is no longer 1000 supported, and there are known problems with multithreading support in 1001 Qt3. As an example, using QString objects in more than one thread will 1002 trigger race reports (this has been confirmed by Trolltech -- see also 1003 Trolltech task <a class="ulink" href="http://trolltech.com/developer/task-tracker/index_html" target="_top">#206152</a>). 1004 </p> 1005 <p> 1006 Qt4 applications are supported by DRD, but only if the 1007 <code class="literal">libqt4-debuginfo</code> package has been installed. Some 1008 of the synchronization and threading primitives in Qt4 bypass the 1009 POSIX threads library, and DRD can only intercept these if symbol 1010 information for the Qt4 library is available. DRD won't tell you if it 1011 has not been able to load the Qt4 debug information, but a huge number 1012 of data races will be reported on data protected via 1013 <code class="literal">QMutex</code> objects. 1014 </p> 1015 </div> 1016 <div class="sect2" title="8.2.8.Debugging Boost.Thread Programs"> 1017 <div class="titlepage"><div><div><h3 class="title"> 1018 <a name="drd-manual.boost.thread"></a>8.2.8.Debugging Boost.Thread Programs</h3></div></div></div> 1019 <p> 1020 The Boost.Thread library is the threading library included with the 1021 cross-platform Boost Libraries. This threading library is an early 1022 implementation of the upcoming C++0x threading library. 1023 </p> 1024 <p> 1025 Applications that use the Boost.Thread library should run fine under DRD. 1026 </p> 1027 <p> 1028 More information about Boost.Thread can be found here: 1029 </p> 1030 <div class="itemizedlist"><ul class="itemizedlist" type="disc"> 1031 <li class="listitem"><p> 1032 Anthony Williams, <a class="ulink" href="http://www.boost.org/doc/libs/1_37_0/doc/html/thread.html" target="_top">Boost.Thread</a> 1033 Library Documentation, Boost website, 2007. 1034 </p></li> 1035 <li class="listitem"><p> 1036 Anthony Williams, <a class="ulink" href="http://www.ddj.com/cpp/211600441" target="_top">What's New in Boost 1037 Threads?</a>, Recent changes to the Boost Thread library, 1038 Dr. Dobbs Magazine, October 2008. 1039 </p></li> 1040 </ul></div> 1041 <p> 1042 </p> 1043 </div> 1044 <div class="sect2" title="8.2.9.Debugging OpenMP Programs"> 1045 <div class="titlepage"><div><div><h3 class="title"> 1046 <a name="drd-manual.openmp"></a>8.2.9.Debugging OpenMP Programs</h3></div></div></div> 1047 <p> 1048 OpenMP stands for <span class="emphasis"><em>Open Multi-Processing</em></span>. The OpenMP 1049 standard consists of a set of compiler directives for C, C++ and Fortran 1050 programs that allows a compiler to transform a sequential program into a 1051 parallel program. OpenMP is well suited for HPC applications and allows to 1052 work at a higher level compared to direct use of the POSIX threads API. While 1053 OpenMP ensures that the POSIX API is used correctly, OpenMP programs can still 1054 contain data races. So it definitely makes sense to verify OpenMP programs 1055 with a thread checking tool. 1056 </p> 1057 <p> 1058 DRD supports OpenMP shared-memory programs generated by GCC. GCC 1059 supports OpenMP since version 4.2.0. GCC's runtime support 1060 for OpenMP programs is provided by a library called 1061 <code class="literal">libgomp</code>. The synchronization primitives implemented 1062 in this library use Linux' futex system call directly, unless the 1063 library has been configured with the 1064 <code class="literal">--disable-linux-futex</code> option. DRD only supports 1065 libgomp libraries that have been configured with this option and in 1066 which symbol information is present. For most Linux distributions this 1067 means that you will have to recompile GCC. See also the script 1068 <code class="literal">drd/scripts/download-and-build-gcc</code> in the 1069 Valgrind source tree for an example of how to compile GCC. You will 1070 also have to make sure that the newly compiled 1071 <code class="literal">libgomp.so</code> library is loaded when OpenMP programs 1072 are started. This is possible by adding a line similar to the 1073 following to your shell startup script: 1074 </p> 1075 <pre class="programlisting"> 1076 export LD_LIBRARY_PATH=~/gcc-4.4.0/lib64:~/gcc-4.4.0/lib: 1077 </pre> 1078 <p> 1079 As an example, the test OpenMP test program 1080 <code class="literal">drd/tests/omp_matinv</code> triggers a data race 1081 when the option -r has been specified on the command line. The data 1082 race is triggered by the following code: 1083 </p> 1084 <pre class="programlisting"> 1085 #pragma omp parallel for private(j) 1086 for (j = 0; j < rows; j++) 1087 { 1088 if (i != j) 1089 { 1090 const elem_t factor = a[j * cols + i]; 1091 for (k = 0; k < cols; k++) 1092 { 1093 a[j * cols + k] -= a[i * cols + k] * factor; 1094 } 1095 } 1096 } 1097 </pre> 1098 <p> 1099 The above code is racy because the variable <code class="literal">k</code> has 1100 not been declared private. DRD will print the following error message 1101 for the above code: 1102 </p> 1103 <pre class="programlisting"> 1104 $ valgrind --tool=drd --check-stack-var=yes --read-var-info=yes drd/tests/omp_matinv 3 -t 2 -r 1105 ... 1106 Conflicting store by thread 1/1 at 0x7fefffbc4 size 4 1107 at 0x4014A0: gj.omp_fn.0 (omp_matinv.c:203) 1108 by 0x401211: gj (omp_matinv.c:159) 1109 by 0x40166A: invert_matrix (omp_matinv.c:238) 1110 by 0x4019B4: main (omp_matinv.c:316) 1111 Location 0x7fefffbc4 is 0 bytes inside local var "k" 1112 declared at omp_matinv.c:160, in frame #0 of thread 1 1113 ... 1114 </pre> 1115 <p> 1116 In the above output the function name <code class="function">gj.omp_fn.0</code> 1117 has been generated by GCC from the function name 1118 <code class="function">gj</code>. The allocation context information shows that the 1119 data race has been caused by modifying the variable <code class="literal">k</code>. 1120 </p> 1121 <p> 1122 Note: for GCC versions before 4.4.0, no allocation context information is 1123 shown. With these GCC versions the most usable information in the above output 1124 is the source file name and the line number where the data race has been 1125 detected (<code class="literal">omp_matinv.c:203</code>). 1126 </p> 1127 <p> 1128 For more information about OpenMP, see also 1129 <a class="ulink" href="http://openmp.org/" target="_top">openmp.org</a>. 1130 </p> 1131 </div> 1132 <div class="sect2" title="8.2.10.DRD and Custom Memory Allocators"> 1133 <div class="titlepage"><div><div><h3 class="title"> 1134 <a name="drd-manual.cust-mem-alloc"></a>8.2.10.DRD and Custom Memory Allocators</h3></div></div></div> 1135 <p> 1136 DRD tracks all memory allocation events that happen via the 1137 standard memory allocation and deallocation functions 1138 (<code class="function">malloc</code>, <code class="function">free</code>, 1139 <code class="function">new</code> and <code class="function">delete</code>), via entry 1140 and exit of stack frames or that have been annotated with Valgrind's 1141 memory pool client requests. DRD uses memory allocation and deallocation 1142 information for two purposes: 1143 </p> 1144 <div class="itemizedlist"><ul class="itemizedlist" type="disc"> 1145 <li class="listitem"><p> 1146 To know where the scope ends of POSIX objects that have not been 1147 destroyed explicitly. It is e.g. not required by the POSIX 1148 threads standard to call 1149 <code class="function">pthread_mutex_destroy</code> before freeing the 1150 memory in which a mutex object resides. 1151 </p></li> 1152 <li class="listitem"><p> 1153 To know where the scope of variables ends. If e.g. heap memory 1154 has been used by one thread, that thread frees that memory, and 1155 another thread allocates and starts using that memory, no data 1156 races must be reported for that memory. 1157 </p></li> 1158 </ul></div> 1159 <p> 1160 </p> 1161 <p> 1162 It is essential for correct operation of DRD that the tool knows about 1163 memory allocation and deallocation events. When analyzing a client program 1164 with DRD that uses a custom memory allocator, either instrument the custom 1165 memory allocator with the <code class="literal">VALGRIND_MALLOCLIKE_BLOCK</code> 1166 and <code class="literal">VALGRIND_FREELIKE_BLOCK</code> macros or disable the 1167 custom memory allocator. 1168 </p> 1169 <p> 1170 As an example, the GNU libstdc++ library can be configured 1171 to use standard memory allocation functions instead of memory pools by 1172 setting the environment variable 1173 <code class="literal">GLIBCXX_FORCE_NEW</code>. For more information, see also 1174 the <a class="ulink" href="http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html" target="_top">libstdc++ 1175 manual</a>. 1176 </p> 1177 </div> 1178 <div class="sect2" title="8.2.11.DRD Versus Memcheck"> 1179 <div class="titlepage"><div><div><h3 class="title"> 1180 <a name="drd-manual.drd-versus-memcheck"></a>8.2.11.DRD Versus Memcheck</h3></div></div></div> 1181 <p> 1182 It is essential for correct operation of DRD that there are no memory 1183 errors such as dangling pointers in the client program. Which means that 1184 it is a good idea to make sure that your program is Memcheck-clean 1185 before you analyze it with DRD. It is possible however that some of 1186 the Memcheck reports are caused by data races. In this case it makes 1187 sense to run DRD before Memcheck. 1188 </p> 1189 <p> 1190 So which tool should be run first? In case both DRD and Memcheck 1191 complain about a program, a possible approach is to run both tools 1192 alternatingly and to fix as many errors as possible after each run of 1193 each tool until none of the two tools prints any more error messages. 1194 </p> 1195 </div> 1196 <div class="sect2" title="8.2.12.Resource Requirements"> 1197 <div class="titlepage"><div><div><h3 class="title"> 1198 <a name="drd-manual.resource-requirements"></a>8.2.12.Resource Requirements</h3></div></div></div> 1199 <p> 1200 The requirements of DRD with regard to heap and stack memory and the 1201 effect on the execution time of client programs are as follows: 1202 </p> 1203 <div class="itemizedlist"><ul class="itemizedlist" type="disc"> 1204 <li class="listitem"><p> 1205 When running a program under DRD with default DRD options, 1206 between 1.1 and 3.6 times more memory will be needed compared to 1207 a native run of the client program. More memory will be needed 1208 if loading debug information has been enabled 1209 (<code class="literal">--read-var-info=yes</code>). 1210 </p></li> 1211 <li class="listitem"><p> 1212 DRD allocates some of its temporary data structures on the stack 1213 of the client program threads. This amount of data is limited to 1214 1 - 2 KB. Make sure that thread stacks are sufficiently large. 1215 </p></li> 1216 <li class="listitem"><p> 1217 Most applications will run between 20 and 50 times slower under 1218 DRD than a native single-threaded run. The slowdown will be most 1219 noticeable for applications which perform frequent mutex lock / 1220 unlock operations. 1221 </p></li> 1222 </ul></div> 1223 <p> 1224 </p> 1225 </div> 1226 <div class="sect2" title="8.2.13.Hints and Tips for Effective Use of DRD"> 1227 <div class="titlepage"><div><div><h3 class="title"> 1228 <a name="drd-manual.effective-use"></a>8.2.13.Hints and Tips for Effective Use of DRD</h3></div></div></div> 1229 <p> 1230 The following information may be helpful when using DRD: 1231 </p> 1232 <div class="itemizedlist"><ul class="itemizedlist" type="disc"> 1233 <li class="listitem"><p> 1234 Make sure that debug information is present in the executable 1235 being analyzed, such that DRD can print function name and line 1236 number information in stack traces. Most compilers can be told 1237 to include debug information via compiler option 1238 <code class="option">-g</code>. 1239 </p></li> 1240 <li class="listitem"><p> 1241 Compile with option <code class="option">-O1</code> instead of 1242 <code class="option">-O0</code>. This will reduce the amount of generated 1243 code, may reduce the amount of debug info and will speed up 1244 DRD's processing of the client program. For more information, 1245 see also <a class="xref" href="manual-core.html#manual-core.started" title="2.2.Getting started">Getting started</a>. 1246 </p></li> 1247 <li class="listitem"><p> 1248 If DRD reports any errors on libraries that are part of your 1249 Linux distribution like e.g. <code class="literal">libc.so</code> or 1250 <code class="literal">libstdc++.so</code>, installing the debug packages 1251 for these libraries will make the output of DRD a lot more 1252 detailed. 1253 </p></li> 1254 <li class="listitem"> 1255 <p> 1256 When using C++, do not send output from more than one thread to 1257 <code class="literal">std::cout</code>. Doing so would not only 1258 generate multiple data race reports, it could also result in 1259 output from several threads getting mixed up. Either use 1260 <code class="function">printf</code> or do the following: 1261 </p> 1262 <div class="orderedlist"><ol class="orderedlist" type="1"> 1263 <li class="listitem"><p>Derive a class from <code class="literal">std::ostreambuf</code> 1264 and let that class send output line by line to 1265 <code class="literal">stdout</code>. This will avoid that individual 1266 lines of text produced by different threads get mixed 1267 up.</p></li> 1268 <li class="listitem"><p>Create one instance of <code class="literal">std::ostream</code> 1269 for each thread. This makes stream formatting settings 1270 thread-local. Pass a per-thread instance of the class 1271 derived from <code class="literal">std::ostreambuf</code> to the 1272 constructor of each instance. </p></li> 1273 <li class="listitem"><p>Let each thread send its output to its own instance of 1274 <code class="literal">std::ostream</code> instead of 1275 <code class="literal">std::cout</code>.</p></li> 1276 </ol></div> 1277 <p> 1278 </p> 1279 </li> 1280 </ul></div> 1281 <p> 1282 </p> 1283 </div> 1284 </div> 1285 <div class="sect1" title="8.3.Using the POSIX Threads API Effectively"> 1286 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> 1287 <a name="drd-manual.Pthreads"></a>8.3.Using the POSIX Threads API Effectively</h2></div></div></div> 1288 <div class="sect2" title="8.3.1.Mutex types"> 1289 <div class="titlepage"><div><div><h3 class="title"> 1290 <a name="drd-manual.mutex-types"></a>8.3.1.Mutex types</h3></div></div></div> 1291 <p> 1292 The Single UNIX Specification version two defines the following four 1293 mutex types (see also the documentation of <a class="ulink" href="http://www.opengroup.org/onlinepubs/007908799/xsh/pthread_mutexattr_settype.html" target="_top"><code class="function">pthread_mutexattr_settype</code></a>): 1294 </p> 1295 <div class="itemizedlist"><ul class="itemizedlist" type="disc"> 1296 <li class="listitem"><p> 1297 <span class="emphasis"><em>normal</em></span>, which means that no error checking 1298 is performed, and that the mutex is non-recursive. 1299 </p></li> 1300 <li class="listitem"><p> 1301 <span class="emphasis"><em>error checking</em></span>, which means that the mutex 1302 is non-recursive and that error checking is performed. 1303 </p></li> 1304 <li class="listitem"><p> 1305 <span class="emphasis"><em>recursive</em></span>, which means that a mutex may be 1306 locked recursively. 1307 </p></li> 1308 <li class="listitem"><p> 1309 <span class="emphasis"><em>default</em></span>, which means that error checking 1310 behavior is undefined, and that the behavior for recursive 1311 locking is also undefined. Or: portable code must neither 1312 trigger error conditions through the Pthreads API nor attempt to 1313 lock a mutex of default type recursively. 1314 </p></li> 1315 </ul></div> 1316 <p> 1317 </p> 1318 <p> 1319 In complex applications it is not always clear from beforehand which 1320 mutex will be locked recursively and which mutex will not be locked 1321 recursively. Attempts lock a non-recursive mutex recursively will 1322 result in race conditions that are very hard to find without a thread 1323 checking tool. So either use the error checking mutex type and 1324 consistently check the return value of Pthread API mutex calls, or use 1325 the recursive mutex type. 1326 </p> 1327 </div> 1328 <div class="sect2" title="8.3.2.Condition variables"> 1329 <div class="titlepage"><div><div><h3 class="title"> 1330 <a name="drd-manual.condvar"></a>8.3.2.Condition variables</h3></div></div></div> 1331 <p> 1332 A condition variable allows one thread to wake up one or more other 1333 threads. Condition variables are often used to notify one or more 1334 threads about state changes of shared data. Unfortunately it is very 1335 easy to introduce race conditions by using condition variables as the 1336 only means of state information propagation. A better approach is to 1337 let threads poll for changes of a state variable that is protected by 1338 a mutex, and to use condition variables only as a thread wakeup 1339 mechanism. See also the source file 1340 <code class="computeroutput">drd/tests/monitor_example.cpp</code> for an 1341 example of how to implement this concept in C++. The monitor concept 1342 used in this example is a well known and very useful concept -- see 1343 also Wikipedia for more information about the <a class="ulink" href="http://en.wikipedia.org/wiki/Monitor_(synchronization)" target="_top">monitor</a> 1344 concept. 1345 </p> 1346 </div> 1347 <div class="sect2" title="8.3.3.pthread_cond_timedwait and timeouts"> 1348 <div class="titlepage"><div><div><h3 class="title"> 1349 <a name="drd-manual.pctw"></a>8.3.3.pthread_cond_timedwait and timeouts</h3></div></div></div> 1350 <p> 1351 Historically the function 1352 <code class="function">pthread_cond_timedwait</code> only allowed the 1353 specification of an absolute timeout, that is a timeout independent of 1354 the time when this function was called. However, almost every call to 1355 this function expresses a relative timeout. This typically happens by 1356 passing the sum of 1357 <code class="computeroutput">clock_gettime(CLOCK_REALTIME)</code> and a 1358 relative timeout as the third argument. This approach is incorrect 1359 since forward or backward clock adjustments by e.g. ntpd will affect 1360 the timeout. A more reliable approach is as follows: 1361 </p> 1362 <div class="itemizedlist"><ul class="itemizedlist" type="disc"> 1363 <li class="listitem"><p> 1364 When initializing a condition variable through 1365 <code class="function">pthread_cond_init</code>, specify that the timeout of 1366 <code class="function">pthread_cond_timedwait</code> will use the clock 1367 <code class="literal">CLOCK_MONOTONIC</code> instead of 1368 <code class="literal">CLOCK_REALTIME</code>. You can do this via 1369 <code class="computeroutput">pthread_condattr_setclock(..., 1370 CLOCK_MONOTONIC)</code>. 1371 </p></li> 1372 <li class="listitem"><p> 1373 When calling <code class="function">pthread_cond_timedwait</code>, pass 1374 the sum of 1375 <code class="computeroutput">clock_gettime(CLOCK_MONOTONIC)</code> 1376 and a relative timeout as the third argument. 1377 </p></li> 1378 </ul></div> 1379 <p> 1380 See also 1381 <code class="computeroutput">drd/tests/monitor_example.cpp</code> for an 1382 example. 1383 </p> 1384 </div> 1385 </div> 1386 <div class="sect1" title="8.4.Limitations"> 1387 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> 1388 <a name="drd-manual.limitations"></a>8.4.Limitations</h2></div></div></div> 1389 <p>DRD currently has the following limitations:</p> 1390 <div class="itemizedlist"><ul class="itemizedlist" type="disc"> 1391 <li class="listitem"><p> 1392 DRD, just like Memcheck, will refuse to start on Linux 1393 distributions where all symbol information has been removed from 1394 <code class="filename">ld.so</code>. This is e.g. the case for the PPC editions 1395 of openSUSE and Gentoo. You will have to install the glibc debuginfo 1396 package on these platforms before you can use DRD. See also openSUSE 1397 bug <a class="ulink" href="http://bugzilla.novell.com/show_bug.cgi?id=396197" target="_top"> 1398 396197</a> and Gentoo bug <a class="ulink" href="http://bugs.gentoo.org/214065" target="_top">214065</a>. 1399 </p></li> 1400 <li class="listitem"><p> 1401 With gcc 4.4.3 and before, DRD may report data races on the C++ 1402 class <code class="literal">std::string</code> in a multithreaded program. This is 1403 a know <code class="literal">libstdc++</code> issue -- see also GCC bug 1404 <a class="ulink" href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40518" target="_top">40518</a> 1405 for more information. 1406 </p></li> 1407 <li class="listitem"><p> 1408 When address tracing is enabled, no information on atomic stores 1409 will be displayed. 1410 </p></li> 1411 <li class="listitem"><p> 1412 If you compile the DRD source code yourself, you need GCC 3.0 or 1413 later. GCC 2.95 is not supported. 1414 </p></li> 1415 <li class="listitem"><p> 1416 Of the two POSIX threads implementations for Linux, only the 1417 NPTL (Native POSIX Thread Library) is supported. The older 1418 LinuxThreads library is not supported. 1419 </p></li> 1420 </ul></div> 1421 </div> 1422 <div class="sect1" title="8.5.Feedback"> 1423 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> 1424 <a name="drd-manual.feedback"></a>8.5.Feedback</h2></div></div></div> 1425 <p> 1426 If you have any comments, suggestions, feedback or bug reports about 1427 DRD, feel free to either post a message on the Valgrind users mailing 1428 list or to file a bug report. See also <a class="ulink" href="http://www.valgrind.org/" target="_top">http://www.valgrind.org/</a> for more information. 1429 </p> 1430 </div> 1431 </div> 1432 <div> 1433 <br><table class="nav" width="100%" cellspacing="3" cellpadding="2" border="0" summary="Navigation footer"> 1434 <tr> 1435 <td rowspan="2" width="40%" align="left"> 1436 <a accesskey="p" href="hg-manual.html"><<7.Helgrind: a thread error detector</a></td> 1437 <td width="20%" align="center"><a accesskey="u" href="manual.html">Up</a></td> 1438 <td rowspan="2" width="40%" align="right"><a accesskey="n" href="ms-manual.html">9.Massif: a heap profiler>></a> 1439 </td> 1440 </tr> 1441 <tr><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td></tr> 1442 </table> 1443 </div> 1444 </body> 1445 </html> 1446