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