blob: 318a750b127c50d59e93c83981efa89b8376dbae [file] [log] [blame]
barte3e54df2008-06-12 15:20:42 +00001<?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
njn7e5d4ed2009-07-30 02:57:52 +000011<option>--tool=drd</option>
barte3e54df2008-06-12 15:20:42 +000012on the Valgrind command line.</para>
13
bart68bac812008-06-27 14:56:06 +000014
barte3e54df2008-06-12 15:20:42 +000015<sect1 id="drd-manual.overview" xreflabel="Overview">
njn05a89172009-07-29 02:36:21 +000016<title>Overview</title>
barte3e54df2008-06-12 15:20:42 +000017
18<para>
19DRD is a Valgrind tool for detecting errors in multithreaded C and C++
bartdd742f22009-07-19 19:50:54 +000020programs. The tool works for any program that uses the POSIX threading
21primitives or that uses threading concepts built on top of the POSIX threading
22primitives.
barte3e54df2008-06-12 15:20:42 +000023</para>
24
bart68bac812008-06-27 14:56:06 +000025<sect2 id="drd-manual.mt-progr-models" xreflabel="MT-progr-models">
bart5a5fe0c2008-06-15 12:22:37 +000026<title>Multithreaded Programming Paradigms</title>
barte3e54df2008-06-12 15:20:42 +000027
bart5a5fe0c2008-06-15 12:22:37 +000028<para>
bartdd742f22009-07-19 19:50:54 +000029There are two possible reasons for using multithreading in a program:
bart5a5fe0c2008-06-15 12:22:37 +000030<itemizedlist>
31 <listitem>
32 <para>
bartdd742f22009-07-19 19:50:54 +000033 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.
bart5a5fe0c2008-06-15 12:22:37 +000037 </para>
38 </listitem>
39 <listitem>
40 <para>
bartdd742f22009-07-19 19:50:54 +000041 To use multiple CPU cores simultaneously for speeding up
42 computations. This is why many High Performance Computing (HPC)
43 applications are multithreaded.
bart5a5fe0c2008-06-15 12:22:37 +000044 </para>
45 </listitem>
46</itemizedlist>
47</para>
barte3e54df2008-06-12 15:20:42 +000048
bart5a5fe0c2008-06-15 12:22:37 +000049<para>
bartdd742f22009-07-19 19:50:54 +000050Multithreaded programs can use one or more of the following programming
njn2d6d5032009-08-07 05:23:31 +000051paradigms. Which paradigm is appropriate depends e.g. on the application type.
bart4ac853b2009-01-02 13:29:32 +000052Some examples of multithreaded programming paradigms are:
bart5a5fe0c2008-06-15 12:22:37 +000053<itemizedlist>
54 <listitem>
55 <para>
bartdd742f22009-07-19 19:50:54 +000056 Locking. Data that is shared over threads is protected from concurrent
njn2d6d5032009-08-07 05:23:31 +000057 accesses via locking. E.g. the POSIX threads library, the Qt library
bartdd742f22009-07-19 19:50:54 +000058 and the Boost.Thread library support this paradigm directly.
bart5a5fe0c2008-06-15 12:22:37 +000059 </para>
60 </listitem>
61 <listitem>
62 <para>
bartdd742f22009-07-19 19:50:54 +000063 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.
bart5a5fe0c2008-06-15 12:22:37 +000066 </para>
67 </listitem>
68 <listitem>
69 <para>
bartdd742f22009-07-19 19:50:54 +000070 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
njn7316df22009-08-04 01:16:01 +000078 cores. GCC supports the
bartdd742f22009-07-19 19:50:54 +000079 OpenMP standard from version 4.2.0 on.
bart4ac853b2009-01-02 13:29:32 +000080 </para>
81 </listitem>
82 <listitem>
83 <para>
bartdd742f22009-07-19 19:50:54 +000084 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
njn2d6d5032009-08-07 05:23:31 +000088 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.
bart5a5fe0c2008-06-15 12:22:37 +000091 </para>
92 </listitem>
bart5a5fe0c2008-06-15 12:22:37 +000093</itemizedlist>
94</para>
barte3e54df2008-06-12 15:20:42 +000095
bart5a5fe0c2008-06-15 12:22:37 +000096<para>
bart68bac812008-06-27 14:56:06 +000097DRD supports any combination of multithreaded programming paradigms as
98long as the implementation of these paradigms is based on the POSIX
99threads primitives. DRD however does not support programs that use
100e.g. Linux' futexes directly. Attempts to analyze such programs with
bart4ac853b2009-01-02 13:29:32 +0000101DRD will cause DRD to report many false positives.
bart68bac812008-06-27 14:56:06 +0000102</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>
111POSIX threads, also known as Pthreads, is the most widely available
112threading library on Unix systems.
bart5a5fe0c2008-06-15 12:22:37 +0000113</para>
barte3e54df2008-06-12 15:20:42 +0000114
bart5a5fe0c2008-06-15 12:22:37 +0000115<para>
bart68bac812008-06-27 14:56:06 +0000116The 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>
bart4ac853b2009-01-02 13:29:32 +0000134 Atomic store and load-modify-store operations. While these are
135 not mentioned in the POSIX threads standard, most
bartdd742f22009-07-19 19:50:54 +0000136 microprocessors support atomic memory operations.
bart68bac812008-06-27 14:56:06 +0000137 </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
bartdd742f22009-07-19 19:50:54 +0000147 objects. The following types of synchronization objects have been
148 defined in the POSIX threads standard: mutexes, condition variables,
barta617d112009-07-27 17:43:39 +0000149 semaphores, reader-writer synchronization objects, barriers and
150 spinlocks.
bart68bac812008-06-27 14:56:06 +0000151 </para>
152 </listitem>
153</itemizedlist>
bart5a5fe0c2008-06-15 12:22:37 +0000154</para>
155
bart68bac812008-06-27 14:56:06 +0000156<para>
157Which source code statements generate which memory accesses depends on
bartdd742f22009-07-19 19:50:54 +0000158the <emphasis>memory model</emphasis> of the programming language being
159used. There is not yet a definitive memory model for the C and C++
160languages. 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">
162WG21/N2338: Concurrency memory model compiler consequences</ulink>.
bart68bac812008-06-27 14:56:06 +0000163</para>
164
165<para>
166For more information about POSIX threads, see also the Single UNIX
167Specification version 3, also known as
bartdd742f22009-07-19 19:50:54 +0000168<ulink url="http://www.opengroup.org/onlinepubs/000095399/idx/threads.html">
bart68bac812008-06-27 14:56:06 +0000169IEEE 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>
bart4ac853b2009-01-02 13:29:32 +0000179Depending on which multithreading paradigm is being used in a program,
180one or more of the following problems can occur:
bart68bac812008-06-27 14:56:06 +0000181<itemizedlist>
182 <listitem>
183 <para>
bartdd742f22009-07-19 19:50:54 +0000184 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.
bart68bac812008-06-27 14:56:06 +0000187 </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>
bartdd742f22009-07-19 19:50:54 +0000197 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.
bart68bac812008-06-27 14:56:06 +0000201 </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>
221Although the likelihood of the occurrence of data races can be reduced
bart4ac853b2009-01-02 13:29:32 +0000222through a disciplined programming style, a tool for automatic
223detection of data races is a necessity when developing multithreaded
224software. DRD can detect these, as well as lock contention and
225improper use of the POSIX threads API.
bart68bac812008-06-27 14:56:06 +0000226</para>
227
228</sect2>
229
230
barte8cdb362008-12-22 07:10:44 +0000231<sect2 id="drd-manual.data-race-detection" xreflabel="data-race-detection">
232<title>Data Race Detection</title>
bart68bac812008-06-27 14:56:06 +0000233
234<para>
bartdd742f22009-07-19 19:50:54 +0000235The result of load and store operations performed by a multithreaded program
236depends on the order in which memory operations are performed. This order is
237determined 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>
254The combination of program order and synchronization order is called the
255<emphasis>happens-before relationship</emphasis>. This concept was first
njn2d6d5032009-08-07 05:23:31 +0000256defined by S. Adve et al in the paper <emphasis>Detecting data races on weak
bartdd742f22009-07-19 19:50:54 +0000257memory systems</emphasis>, ACM SIGARCH Computer Architecture News, v.19 n.3,
258p.234-243, May 1991.
bart68bac812008-06-27 14:56:06 +0000259</para>
260
261<para>
bartdd742f22009-07-19 19:50:54 +0000262Two memory operations <emphasis>conflict</emphasis> if both operations are
263performed by different threads, refer to the same memory location and at least
264one of them is a store operation.
265</para>
266
267<para>
268A multithreaded program is <emphasis>data-race free</emphasis> if all
269conflicting memory accesses are ordered by synchronization
270operations.
bart68bac812008-06-27 14:56:06 +0000271</para>
272
273<para>
274A well known way to ensure that a multithreaded program is data-race
275free is to ensure that a locking discipline is followed. It is e.g.
276possible to associate a mutex with each shared data item, and to hold
277a lock on the associated mutex while the shared data is accessed.
278</para>
279
280<para>
bartdd742f22009-07-19 19:50:54 +0000281All programs that follow a locking discipline are data-race free, but not all
282data-race free programs follow a locking discipline. There exist multithreaded
283programs where access to shared data is arbitrated via condition variables,
284semaphores or barriers. As an example, a certain class of HPC applications
285consists of a sequence of computation steps separated in time by barriers, and
286where these barriers are the only means of synchronization. Although there are
287many conflicting memory accesses in such applications and although such
288applications do not make use mutexes, most of these applications do not
289contain data races.
bart68bac812008-06-27 14:56:06 +0000290</para>
291
292<para>
bartdd742f22009-07-19 19:50:54 +0000293There exist two different approaches for verifying the correctness of
294multithreaded programs at runtime. The approach of the so-called Eraser
295algorithm is to verify whether all shared memory accesses follow a consistent
296locking strategy. And the happens-before data race detectors verify directly
297whether all interthread memory accesses are ordered by synchronization
298operations. While the last approach is more complex to implement, and while it
299is more sensitive to OS scheduling, it is a general approach that works for
300all classes of multithreaded programs. An important advantage of
301happens-before data race detectors is that these do not report any false
302positives.
bart68bac812008-06-27 14:56:06 +0000303</para>
304
305<para>
barte8cdb362008-12-22 07:10:44 +0000306DRD is based on the happens-before algorithm.
bart68bac812008-06-27 14:56:06 +0000307</para>
308
309</sect2>
310
311
barte3e54df2008-06-12 15:20:42 +0000312</sect1>
313
314
bart68bac812008-06-27 14:56:06 +0000315<sect1 id="drd-manual.using-drd" xreflabel="Using DRD">
316<title>Using DRD</title>
317
njna3311642009-08-10 01:29:14 +0000318<sect2 id="drd-manual.options" xreflabel="DRD Command-line Options">
319<title>DRD Command-line Options</title>
barte3e54df2008-06-12 15:20:42 +0000320
bart68bac812008-06-27 14:56:06 +0000321<para>The following command-line options are available for controlling the
322behavior of the DRD tool itself:</para>
barte3e54df2008-06-12 15:20:42 +0000323
324<!-- start of xi:include in the manpage -->
325<variablelist id="drd.opts.list">
bart68bac812008-06-27 14:56:06 +0000326 <varlistentry>
327 <term>
328 <option><![CDATA[--check-stack-var=<yes|no> [default: no]]]></option>
329 </term>
330 <listitem>
331 <para>
njn2d6d5032009-08-07 05:23:31 +0000332 Controls whether DRD detects data races on stack
bartdd742f22009-07-19 19:50:54 +0000333 variables. Verifying stack variables is disabled by default because
334 most programs do not share stack variables over threads.
bart68bac812008-06-27 14:56:06 +0000335 </para>
336 </listitem>
337 </varlistentry>
338 <varlistentry>
339 <term>
340 <option><![CDATA[--exclusive-threshold=<n> [default: off]]]></option>
341 </term>
342 <listitem>
343 <para>
bart4ac853b2009-01-02 13:29:32 +0000344 Print an error message if any mutex or writer lock has been
bartdd742f22009-07-19 19:50:54 +0000345 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.
bart68bac812008-06-27 14:56:06 +0000361 </para>
362 </listitem>
363 </varlistentry>
364 <varlistentry>
365 <term>
bart8f0b0d72008-06-28 16:47:22 +0000366 <option>
367 <![CDATA[--report-signal-unlocked=<yes|no> [default: yes]]]>
368 </option>
369 </term>
370 <listitem>
371 <para>
372 Whether to report calls to
njn2d6d5032009-08-07 05:23:31 +0000373 <function>pthread_cond_signal</function> and
374 <function>pthread_cond_broadcast</function> where the mutex
bart4ac853b2009-01-02 13:29:32 +0000375 associated with the signal through
njn2d6d5032009-08-07 05:23:31 +0000376 <function>pthread_cond_wait</function> or
377 <function>pthread_cond_timed_wait</function>is not locked at
bart8f0b0d72008-06-28 16:47:22 +0000378 the time the signal is sent. Sending a signal without holding
379 a lock on the associated mutex is a common programming error
380 which can cause subtle race conditions and unpredictable
381 behavior. There exist some uncommon synchronization patterns
382 however where it is safe to send a signal without holding a
383 lock on the associated mutex.
384 </para>
385 </listitem>
386 </varlistentry>
387 <varlistentry>
388 <term>
bart68bac812008-06-27 14:56:06 +0000389 <option><![CDATA[--segment-merging=<yes|no> [default: yes]]]></option>
390 </term>
391 <listitem>
392 <para>
393 Controls segment merging. Segment merging is an algorithm to
394 limit memory usage of the data race detection
395 algorithm. Disabling segment merging may improve the accuracy
396 of the so-called 'other segments' displayed in race reports
397 but can also trigger an out of memory error.
398 </para>
399 </listitem>
400 </varlistentry>
401 <varlistentry>
402 <term>
bartdd742f22009-07-19 19:50:54 +0000403 <option><![CDATA[--segment-merging-interval=<n> [default: 10]]]></option>
404 </term>
405 <listitem>
406 <para>
407 Perform segment merging only after the specified number of new
408 segments have been created. This is an advanced configuration option
409 that allows to choose whether to minimize DRD's memory usage by
410 choosing a low value or to let DRD run faster by choosing a slightly
411 higher value. The optimal value for this parameter depends on the
412 program being analyzed. The default value works well for most programs.
413 </para>
414 </listitem>
415 </varlistentry>
416 <varlistentry>
417 <term>
bart68bac812008-06-27 14:56:06 +0000418 <option><![CDATA[--shared-threshold=<n> [default: off]]]></option>
419 </term>
420 <listitem>
421 <para>
bart4ac853b2009-01-02 13:29:32 +0000422 Print an error message if a reader lock has been held longer
423 than the specified time (in milliseconds). This option enables
bartdd742f22009-07-19 19:50:54 +0000424 the detection of lock contention.
bart68bac812008-06-27 14:56:06 +0000425 </para>
426 </listitem>
427 </varlistentry>
428 <varlistentry>
429 <term>
430 <option><![CDATA[--show-confl-seg=<yes|no> [default: yes]]]></option>
431 </term>
432 <listitem>
433 <para>
434 Show conflicting segments in race reports. Since this
435 information can help to find the cause of a data race, this
436 option is enabled by default. Disabling this option makes the
437 output of DRD more compact.
438 </para>
439 </listitem>
440 </varlistentry>
441 <varlistentry>
442 <term>
443 <option><![CDATA[--show-stack-usage=<yes|no> [default: no]]]></option>
444 </term>
445 <listitem>
446 <para>
bartdd742f22009-07-19 19:50:54 +0000447 Print stack usage at thread exit time. When a program creates a large
448 number of threads it becomes important to limit the amount of virtual
449 memory allocated for thread stacks. This option makes it possible to
450 observe how much stack memory has been used by each thread of the the
451 client program. Note: the DRD tool itself allocates some temporary
452 data on the client thread stack. The space necessary for this
453 temporary data must be allocated by the client program when it
454 allocates stack memory, but is not included in stack usage reported by
455 DRD.
bart68bac812008-06-27 14:56:06 +0000456 </para>
457 </listitem>
458 </varlistentry>
barte3e54df2008-06-12 15:20:42 +0000459</variablelist>
460<!-- end of xi:include in the manpage -->
461
462<!-- start of xi:include in the manpage -->
bart68bac812008-06-27 14:56:06 +0000463<para>
464The following options are available for monitoring the behavior of the
bart4ac853b2009-01-02 13:29:32 +0000465client program:
bart68bac812008-06-27 14:56:06 +0000466</para>
467
barte3e54df2008-06-12 15:20:42 +0000468<variablelist id="drd.debugopts.list">
bart68bac812008-06-27 14:56:06 +0000469 <varlistentry>
470 <term>
471 <option><![CDATA[--trace-addr=<address> [default: none]]]></option>
472 </term>
473 <listitem>
474 <para>
475 Trace all load and store activity for the specified
476 address. This option may be specified more than once.
477 </para>
478 </listitem>
479 </varlistentry>
480 <varlistentry>
481 <term>
482 <option><![CDATA[--trace-barrier=<yes|no> [default: no]]]></option>
483 </term>
484 <listitem>
485 <para>
486 Trace all barrier activity.
487 </para>
488 </listitem>
489 </varlistentry>
490 <varlistentry>
491 <term>
492 <option><![CDATA[--trace-cond=<yes|no> [default: no]]]></option>
493 </term>
494 <listitem>
495 <para>
496 Trace all condition variable activity.
497 </para>
498 </listitem>
499 </varlistentry>
500 <varlistentry>
501 <term>
502 <option><![CDATA[--trace-fork-join=<yes|no> [default: no]]]></option>
503 </term>
504 <listitem>
505 <para>
506 Trace all thread creation and all thread termination events.
507 </para>
508 </listitem>
509 </varlistentry>
510 <varlistentry>
511 <term>
512 <option><![CDATA[--trace-mutex=<yes|no> [default: no]]]></option>
513 </term>
514 <listitem>
515 <para>
516 Trace all mutex activity.
517 </para>
518 </listitem>
519 </varlistentry>
520 <varlistentry>
521 <term>
522 <option><![CDATA[--trace-rwlock=<yes|no> [default: no]]]></option>
523 </term>
524 <listitem>
525 <para>
526 Trace all reader-writer lock activity.
527 </para>
528 </listitem>
529 </varlistentry>
530 <varlistentry>
531 <term>
532 <option><![CDATA[--trace-semaphore=<yes|no> [default: no]]]></option>
533 </term>
534 <listitem>
535 <para>
536 Trace all semaphore activity.
537 </para>
538 </listitem>
539 </varlistentry>
barte3e54df2008-06-12 15:20:42 +0000540</variablelist>
541<!-- end of xi:include in the manpage -->
542
bart68bac812008-06-27 14:56:06 +0000543</sect2>
barte3e54df2008-06-12 15:20:42 +0000544
bart5a5fe0c2008-06-15 12:22:37 +0000545
bart68bac812008-06-27 14:56:06 +0000546<sect2 id="drd-manual.data-races" xreflabel="Data Races">
bart88f11412008-07-03 07:08:04 +0000547<title>Detected Errors: Data Races</title>
bart8f0b0d72008-06-28 16:47:22 +0000548
549<para>
bart4ac853b2009-01-02 13:29:32 +0000550DRD prints a message every time it detects a data race. Please keep
551the following in mind when interpreting DRD's output:
bart8f0b0d72008-06-28 16:47:22 +0000552<itemizedlist>
553 <listitem>
554 <para>
bartdd742f22009-07-19 19:50:54 +0000555 Every thread is assigned a <emphasis>thread ID</emphasis> by the DRD
556 tool. A thread ID is a number. Thread ID's start at one and are never
557 recycled.
bart8f0b0d72008-06-28 16:47:22 +0000558 </para>
559 </listitem>
560 <listitem>
561 <para>
562 The term <emphasis>segment</emphasis> refers to a consecutive
563 sequence of load, store and synchronization operations, all
564 issued by the same thread. A segment always starts and ends at a
565 synchronization operation. Data race analysis is performed
566 between segments instead of between individual load and store
567 operations because of performance reasons.
568 </para>
569 </listitem>
570 <listitem>
571 <para>
572 There are always at least two memory accesses involved in a data
573 race. Memory accesses involved in a data race are called
574 <emphasis>conflicting memory accesses</emphasis>. DRD prints a
575 report for each memory access that conflicts with a past memory
576 access.
577 </para>
578 </listitem>
579</itemizedlist>
580</para>
581
582<para>
583Below you can find an example of a message printed by DRD when it
584detects a data race:
585</para>
586<programlisting><![CDATA[
barte2b98232009-07-22 18:13:21 +0000587$ valgrind --tool=drd --read-var-info=yes drd/tests/rwlock_race
bart8f0b0d72008-06-28 16:47:22 +0000588...
589==9466== Thread 3:
bartdd742f22009-07-19 19:50:54 +0000590==9466== Conflicting load by thread 3 at 0x006020b8 size 4
bart8f0b0d72008-06-28 16:47:22 +0000591==9466== at 0x400B6C: thread_func (rwlock_race.c:29)
592==9466== by 0x4C291DF: vg_thread_wrapper (drd_pthread_intercepts.c:186)
593==9466== by 0x4E3403F: start_thread (in /lib64/libpthread-2.8.so)
594==9466== by 0x53250CC: clone (in /lib64/libc-2.8.so)
595==9466== Location 0x6020b8 is 0 bytes inside local var "s_racy"
596==9466== declared at rwlock_race.c:18, in frame #0 of thread 3
bartdd742f22009-07-19 19:50:54 +0000597==9466== Other segment start (thread 2)
bart8f0b0d72008-06-28 16:47:22 +0000598==9466== at 0x4C2847D: pthread_rwlock_rdlock* (drd_pthread_intercepts.c:813)
599==9466== by 0x400B6B: thread_func (rwlock_race.c:28)
600==9466== by 0x4C291DF: vg_thread_wrapper (drd_pthread_intercepts.c:186)
601==9466== by 0x4E3403F: start_thread (in /lib64/libpthread-2.8.so)
602==9466== by 0x53250CC: clone (in /lib64/libc-2.8.so)
bartdd742f22009-07-19 19:50:54 +0000603==9466== Other segment end (thread 2)
bart8f0b0d72008-06-28 16:47:22 +0000604==9466== at 0x4C28B54: pthread_rwlock_unlock* (drd_pthread_intercepts.c:912)
605==9466== by 0x400B84: thread_func (rwlock_race.c:30)
606==9466== by 0x4C291DF: vg_thread_wrapper (drd_pthread_intercepts.c:186)
607==9466== by 0x4E3403F: start_thread (in /lib64/libpthread-2.8.so)
608==9466== by 0x53250CC: clone (in /lib64/libc-2.8.so)
609...
610]]></programlisting>
611
612<para>
613The above report has the following meaning:
614<itemizedlist>
615 <listitem>
616 <para>
617 The number in the column on the left is the process ID of the
618 process being analyzed by DRD.
619 </para>
620 </listitem>
621 <listitem>
622 <para>
bartdd742f22009-07-19 19:50:54 +0000623 The first line ("Thread 3") tells you the thread ID for
624 the thread in which context the data race has been detected.
bart8f0b0d72008-06-28 16:47:22 +0000625 </para>
626 </listitem>
627 <listitem>
628 <para>
bartdd742f22009-07-19 19:50:54 +0000629 The next line tells which kind of operation was performed (load or
630 store) and by which thread. On the same line the start address and the
631 number of bytes involved in the conflicting access are also displayed.
bart8f0b0d72008-06-28 16:47:22 +0000632 </para>
633 </listitem>
634 <listitem>
635 <para>
636 Next, the call stack of the conflicting access is displayed. If
njn2d6d5032009-08-07 05:23:31 +0000637 your program has been compiled with debug information
638 (<option>-g</option>), this call stack will include file names and
639 line numbers. The two
bart8f0b0d72008-06-28 16:47:22 +0000640 bottommost frames in this call stack (<function>clone</function>
bart4ac853b2009-01-02 13:29:32 +0000641 and <function>start_thread</function>) show how the NPTL starts
642 a thread. The third frame
643 (<function>vg_thread_wrapper</function>) is added by DRD. The
644 fourth frame (<function>thread_func</function>) is the first
645 interesting line because it shows the thread entry point, that
646 is the function that has been passed as the third argument to
njn2d6d5032009-08-07 05:23:31 +0000647 <function>pthread_create</function>.
bart8f0b0d72008-06-28 16:47:22 +0000648 </para>
649 </listitem>
650 <listitem>
651 <para>
652 Next, the allocation context for the conflicting address is
bart4ac853b2009-01-02 13:29:32 +0000653 displayed. For dynamically allocated data the allocation call
654 stack is shown. For static variables and stack variables the
655 allocation context is only shown when the option
njn7e5d4ed2009-07-30 02:57:52 +0000656 <option>--read-var-info=yes</option> has been
bart8f0b0d72008-06-28 16:47:22 +0000657 specified. Otherwise DRD will print <computeroutput>Allocation
bart4ac853b2009-01-02 13:29:32 +0000658 context: unknown</computeroutput>.
bart8f0b0d72008-06-28 16:47:22 +0000659 </para>
660 </listitem>
661 <listitem>
662 <para>
663 A conflicting access involves at least two memory accesses. For
664 one of these accesses an exact call stack is displayed, and for
665 the other accesses an approximate call stack is displayed,
666 namely the start and the end of the segments of the other
667 accesses. This information can be interpreted as follows:
668 <orderedlist>
669 <listitem>
670 <para>
671 Start at the bottom of both call stacks, and count the
672 number stack frames with identical function name, file
673 name and line number. In the above example the three
674 bottommost frames are identical
675 (<function>clone</function>,
676 <function>start_thread</function> and
677 <function>vg_thread_wrapper</function>).
678 </para>
679 </listitem>
680 <listitem>
681 <para>
682 The next higher stack frame in both call stacks now tells
683 you between in which source code region the other memory
684 access happened. The above output tells that the other
685 memory access involved in the data race happened between
686 source code lines 28 and 30 in file
687 <computeroutput>rwlock_race.c</computeroutput>.
688 </para>
689 </listitem>
690 </orderedlist>
691 </para>
692 </listitem>
693</itemizedlist>
694</para>
695
bart68bac812008-06-27 14:56:06 +0000696</sect2>
bart5a5fe0c2008-06-15 12:22:37 +0000697
698
bart68bac812008-06-27 14:56:06 +0000699<sect2 id="drd-manual.lock-contention" xreflabel="Lock Contention">
bart88f11412008-07-03 07:08:04 +0000700<title>Detected Errors: Lock Contention</title>
bart8f0b0d72008-06-28 16:47:22 +0000701
702<para>
barta617d112009-07-27 17:43:39 +0000703Threads must be able to make progress without being blocked for too long by
704other threads. Sometimes a thread has to wait until a mutex or reader-writer
705synchronization object is unlocked by another thread. This is called
bart4ac853b2009-01-02 13:29:32 +0000706<emphasis>lock contention</emphasis>.
bart8f0b0d72008-06-28 16:47:22 +0000707</para>
708
709<para>
bart4ac853b2009-01-02 13:29:32 +0000710Lock contention causes delays. Such delays should be as short as
711possible. The two command line options
bart8f0b0d72008-06-28 16:47:22 +0000712<literal>--exclusive-threshold=&lt;n&gt;</literal> and
713<literal>--shared-threshold=&lt;n&gt;</literal> make it possible to
bart4ac853b2009-01-02 13:29:32 +0000714detect excessive lock contention by making DRD report any lock that
715has been held longer than the specified threshold. An example:
bart8f0b0d72008-06-28 16:47:22 +0000716</para>
717<programlisting><![CDATA[
bartef1b9722008-07-04 15:34:23 +0000718$ valgrind --tool=drd --exclusive-threshold=10 drd/tests/hold_lock -i 500
bart8f0b0d72008-06-28 16:47:22 +0000719...
720==10668== Acquired at:
721==10668== at 0x4C267C8: pthread_mutex_lock (drd_pthread_intercepts.c:395)
722==10668== by 0x400D92: main (hold_lock.c:51)
723==10668== Lock on mutex 0x7fefffd50 was held during 503 ms (threshold: 10 ms).
724==10668== at 0x4C26ADA: pthread_mutex_unlock (drd_pthread_intercepts.c:441)
725==10668== by 0x400DB5: main (hold_lock.c:55)
726...
727]]></programlisting>
728
729<para>
730The <literal>hold_lock</literal> test program holds a lock as long as
731specified by the <literal>-i</literal> (interval) argument. The DRD
732output reports that the lock acquired at line 51 in source file
733<literal>hold_lock.c</literal> and released at line 55 was held during
734503 ms, while a threshold of 10 ms was specified to DRD.
735</para>
736
bart68bac812008-06-27 14:56:06 +0000737</sect2>
bart5a5fe0c2008-06-15 12:22:37 +0000738
739
bart68bac812008-06-27 14:56:06 +0000740<sect2 id="drd-manual.api-checks" xreflabel="API Checks">
bart88f11412008-07-03 07:08:04 +0000741<title>Detected Errors: Misuse of the POSIX threads API</title>
bart8f0b0d72008-06-28 16:47:22 +0000742
743<para>
744 DRD is able to detect and report the following misuses of the POSIX
745 threads API:
746 <itemizedlist>
747 <listitem>
748 <para>
749 Passing the address of one type of synchronization object
750 (e.g. a mutex) to a POSIX API call that expects a pointer to
751 another type of synchronization object (e.g. a condition
752 variable).
753 </para>
754 </listitem>
755 <listitem>
756 <para>
bart4ac853b2009-01-02 13:29:32 +0000757 Attempts to unlock a mutex that has not been locked.
bart8f0b0d72008-06-28 16:47:22 +0000758 </para>
759 </listitem>
760 <listitem>
761 <para>
bart4ac853b2009-01-02 13:29:32 +0000762 Attempts to unlock a mutex that was locked by another thread.
bart8f0b0d72008-06-28 16:47:22 +0000763 </para>
764 </listitem>
765 <listitem>
766 <para>
bart4ac853b2009-01-02 13:29:32 +0000767 Attempts to lock a mutex of type
bart8f0b0d72008-06-28 16:47:22 +0000768 <literal>PTHREAD_MUTEX_NORMAL</literal> or a spinlock
769 recursively.
770 </para>
771 </listitem>
772 <listitem>
773 <para>
774 Destruction or deallocation of a locked mutex.
775 </para>
776 </listitem>
777 <listitem>
778 <para>
779 Sending a signal to a condition variable while no lock is held
bartdd742f22009-07-19 19:50:54 +0000780 on the mutex associated with the condition variable.
bart8f0b0d72008-06-28 16:47:22 +0000781 </para>
782 </listitem>
783 <listitem>
784 <para>
njn2d6d5032009-08-07 05:23:31 +0000785 Calling <function>pthread_cond_wait</function> on a mutex
bart8f0b0d72008-06-28 16:47:22 +0000786 that is not locked, that is locked by another thread or that
787 has been locked recursively.
788 </para>
789 </listitem>
790 <listitem>
791 <para>
792 Associating two different mutexes with a condition variable
njn2d6d5032009-08-07 05:23:31 +0000793 through <function>pthread_cond_wait</function>.
bart8f0b0d72008-06-28 16:47:22 +0000794 </para>
795 </listitem>
796 <listitem>
797 <para>
798 Destruction or deallocation of a condition variable that is
799 being waited upon.
800 </para>
801 </listitem>
802 <listitem>
803 <para>
barta617d112009-07-27 17:43:39 +0000804 Destruction or deallocation of a locked reader-writer synchronization
805 object.
bart8f0b0d72008-06-28 16:47:22 +0000806 </para>
807 </listitem>
808 <listitem>
809 <para>
barta617d112009-07-27 17:43:39 +0000810 Attempts to unlock a reader-writer synchronization object that was not
811 locked by the calling thread.
bart8f0b0d72008-06-28 16:47:22 +0000812 </para>
813 </listitem>
814 <listitem>
815 <para>
barta617d112009-07-27 17:43:39 +0000816 Attempts to recursively lock a reader-writer synchronization object
817 exclusively.
818 </para>
819 </listitem>
820 <listitem>
821 <para>
822 Attempts to pass the address of a user-defined reader-writer
823 synchronization object to a POSIX threads function.
824 </para>
825 </listitem>
826 <listitem>
827 <para>
828 Attempts to pass the address of a POSIX reader-writer synchronization
829 object to one of the annotations for user-defined reader-writer
830 synchronization objects.
bart8f0b0d72008-06-28 16:47:22 +0000831 </para>
832 </listitem>
833 <listitem>
834 <para>
835 Reinitialization of a mutex, condition variable, reader-writer
836 lock, semaphore or barrier.
837 </para>
838 </listitem>
839 <listitem>
840 <para>
841 Destruction or deallocation of a semaphore or barrier that is
842 being waited upon.
843 </para>
844 </listitem>
845 <listitem>
846 <para>
bart776a91e2009-02-22 09:29:07 +0000847 Missing synchronization between barrier wait and barrier destruction.
848 </para>
849 </listitem>
850 <listitem>
851 <para>
barta617d112009-07-27 17:43:39 +0000852 Exiting a thread without first unlocking the spinlocks, mutexes or
853 reader-writer synchronization objects that were locked by that thread.
bart8f0b0d72008-06-28 16:47:22 +0000854 </para>
855 </listitem>
bart291bb5e2009-08-15 13:31:41 +0000856 <listitem>
857 <para>
858 Passing an invalid thread ID to <function>pthread_join</function>
859 or <function>pthread_cancel</function>.
860 </para>
861 </listitem>
bart8f0b0d72008-06-28 16:47:22 +0000862 </itemizedlist>
863</para>
864
bart68bac812008-06-27 14:56:06 +0000865</sect2>
bart5a5fe0c2008-06-15 12:22:37 +0000866
867
bart68bac812008-06-27 14:56:06 +0000868<sect2 id="drd-manual.clientreqs" xreflabel="Client requests">
bart5a5fe0c2008-06-15 12:22:37 +0000869<title>Client Requests</title>
870
871<para>
bartdd742f22009-07-19 19:50:54 +0000872Just as for other Valgrind tools it is possible to let a client program
873interact with the DRD tool through client requests. In addition to the
njn2d6d5032009-08-07 05:23:31 +0000874client requests several macros have been defined that allow to use the
bartdd742f22009-07-19 19:50:54 +0000875client requests in a convenient way.
bart1e7f2782008-07-01 13:43:44 +0000876</para>
877
878<para>
879The interface between client programs and the DRD tool is defined in
880the header file <literal>&lt;valgrind/drd.h&gt;</literal>. The
njn2d6d5032009-08-07 05:23:31 +0000881available macros and client requests are:
bart1e7f2782008-07-01 13:43:44 +0000882<itemizedlist>
883 <listitem>
884 <para>
bartdd742f22009-07-19 19:50:54 +0000885 The macro <literal>DRD_GET_VALGRIND_THREADID</literal> and the
886 corresponding client
887 request <varname>VG_USERREQ__DRD_GET_VALGRIND_THREAD_ID</varname>.
888 Query the thread ID that has been assigned by the Valgrind core to the
889 thread executing this client request. Valgrind's thread ID's start at
890 one and are recycled in case a thread stops.
bart1e7f2782008-07-01 13:43:44 +0000891 </para>
892 </listitem>
893 <listitem>
894 <para>
bartdd742f22009-07-19 19:50:54 +0000895 The macro <literal>DRD_GET_DRD_THREADID</literal> and the corresponding
896 client request <varname>VG_USERREQ__DRD_GET_DRD_THREAD_ID</varname>.
897 Query the thread ID that has been assigned by DRD to the thread
898 executing this client request. These are the thread ID's reported by DRD
899 in data race reports and in trace messages. DRD's thread ID's start at
900 one and are never recycled.
bart1e7f2782008-07-01 13:43:44 +0000901 </para>
902 </listitem>
903 <listitem>
904 <para>
njn2d6d5032009-08-07 05:23:31 +0000905 The macros <literal>DRD_IGNORE_VAR(x)</literal>,
bartdd742f22009-07-19 19:50:54 +0000906 <literal>ANNOTATE_TRACE_MEMORY(&amp;x)</literal> and the corresponding
907 client request <varname>VG_USERREQ__DRD_START_SUPPRESSION</varname>. Some
908 applications contain intentional races. There exist e.g. applications
909 where the same value is assigned to a shared variable from two different
910 threads. It may be more convenient to suppress such races than to solve
911 these. This client request allows to suppress such races.
bart1e7f2782008-07-01 13:43:44 +0000912 </para>
913 </listitem>
914 <listitem>
915 <para>
bart07ec5932009-07-26 15:58:25 +0000916 The macro <literal>DRD_STOP_IGNORING_VAR(x)</literal> and the
917 corresponding client request
918 <varname>VG_USERREQ__DRD_FINISH_SUPPRESSION</varname>. Tell DRD
bartdd742f22009-07-19 19:50:54 +0000919 to no longer ignore data races for the address range that was suppressed
bart07ec5932009-07-26 15:58:25 +0000920 either via the macro <literal>DRD_IGNORE_VAR(x)</literal> or via the
921 client request <varname>VG_USERREQ__DRD_START_SUPPRESSION</varname>.
bart1e7f2782008-07-01 13:43:44 +0000922 </para>
923 </listitem>
924 <listitem>
925 <para>
njn2d6d5032009-08-07 05:23:31 +0000926 The macros <literal>DRD_TRACE_VAR(x)</literal>,
bartdd742f22009-07-19 19:50:54 +0000927 <literal>ANNOTATE_TRACE_MEMORY(&amp;x)</literal>
928 and the corresponding client request
bart1e7f2782008-07-01 13:43:44 +0000929 <varname>VG_USERREQ__DRD_START_TRACE_ADDR</varname>. Trace all
bartdd742f22009-07-19 19:50:54 +0000930 load and store activity on the specified address range. When DRD reports
931 a data race on a specified variable, and it's not immediately clear
932 which source code statements triggered the conflicting accesses, it can
933 be very helpful to trace all activity on the offending memory location.
bart1e7f2782008-07-01 13:43:44 +0000934 </para>
935 </listitem>
936 <listitem>
937 <para>
bartdd742f22009-07-19 19:50:54 +0000938 The client
939 request <varname>VG_USERREQ__DRD_STOP_TRACE_ADDR</varname>. Do no longer
bart1e7f2782008-07-01 13:43:44 +0000940 trace load and store activity for the specified address range.
bart1e7f2782008-07-01 13:43:44 +0000941 </para>
942 </listitem>
bartdd742f22009-07-19 19:50:54 +0000943 <listitem>
944 <para>
945 The macro <literal>ANNOTATE_HAPPENS_BEFORE(addr)</literal> tells DRD to
946 insert a mark. Insert this macro just after an access to the variable at
947 the specified address has been performed.
948 </para>
949 </listitem>
950 <listitem>
951 <para>
952 The macro <literal>ANNOTATE_HAPPENS_AFTER(addr)</literal> tells DRD that
953 the next access to the variable at the specified address should be
954 considered to have happened after the access just before the latest
955 <literal>ANNOTATE_HAPPENS_BEFORE(addr)</literal> annotation that
njn2d6d5032009-08-07 05:23:31 +0000956 references the same variable. The purpose of these two macros is to
bartdd742f22009-07-19 19:50:54 +0000957 tell DRD about the order of inter-thread memory accesses implemented via
958 atomic memory operations.
959 </para>
960 </listitem>
961 <listitem>
962 <para>
963 The macro <literal>ANNOTATE_RWLOCK_CREATE(rwlock)</literal> tells DRD
964 that the object at address <literal>rwlock</literal> is a
965 reader-writer synchronization object that is not a
966 <literal>pthread_rwlock_t</literal> synchronization object.
967 </para>
968 </listitem>
969 <listitem>
970 <para>
971 The macro <literal>ANNOTATE_RWLOCK_DESTROY(rwlock)</literal> tells DRD
972 that the reader-writer synchronization object at
973 address <literal>rwlock</literal> has been destroyed.
974 </para>
975 </listitem>
976 <listitem>
977 <para>
978 The macro <literal>ANNOTATE_WRITERLOCK_ACQUIRED(rwlock)</literal> tells
979 DRD that a writer lock has been acquired on the reader-writer
980 synchronization object at address <literal>rwlock</literal>.
981 </para>
982 </listitem>
983 <listitem>
984 <para>
985 The macro <literal>ANNOTATE_READERLOCK_ACQUIRED(rwlock)</literal> tells
986 DRD that a reader lock has been acquired on the reader-writer
987 synchronization object at address <literal>rwlock</literal>.
988 </para>
989 </listitem>
990 <listitem>
991 <para>
992 The macro <literal>ANNOTATE_RWLOCK_ACQUIRED(rwlock, is_w)</literal>
993 tells DRD that a writer lock (when <literal>is_w != 0</literal>) or that
994 a reader lock (when <literal>is_w == 0</literal>) has been acquired on
995 the reader-writer synchronization object at
996 address <literal>rwlock</literal>.
997 </para>
998 </listitem>
999 <listitem>
1000 <para>
1001 The macro <literal>ANNOTATE_WRITERLOCK_RELEASED(rwlock)</literal> tells
1002 DRD that a writer lock has been released on the reader-writer
1003 synchronization object at address <literal>rwlock</literal>.
1004 </para>
1005 </listitem>
1006 <listitem>
1007 <para>
1008 The macro <literal>ANNOTATE_READERLOCK_RELEASED(rwlock)</literal> tells
1009 DRD that a reader lock has been released on the reader-writer
1010 synchronization object at address <literal>rwlock</literal>.
1011 </para>
1012 </listitem>
1013 <listitem>
1014 <para>
1015 The macro <literal>ANNOTATE_RWLOCK_RELEASED(rwlock, is_w)</literal>
1016 tells DRD that a writer lock (when <literal>is_w != 0</literal>) or that
1017 a reader lock (when <literal>is_w == 0</literal>) has been released on
1018 the reader-writer synchronization object at
1019 address <literal>rwlock</literal>.
1020 </para>
1021 </listitem>
1022 <listitem>
1023 <para>
1024 The macro <literal>ANNOTATE_BENIGN_RACE(addr, descr)</literal> tells
1025 DRD that any races detected on the specified address are benign and
1026 hence should not be reported. The <literal>descr</literal> argument is
1027 ignored but can be used to document why data races
1028 on <literal>addr</literal> are benign.
1029 </para>
1030 </listitem>
1031 <listitem>
1032 <para>
1033 The macro <literal>ANNOTATE_IGNORE_READS_BEGIN</literal> tells
1034 DRD to ignore all memory loads performed by the current thread.
1035 </para>
1036 </listitem>
1037 <listitem>
1038 <para>
1039 The macro <literal>ANNOTATE_IGNORE_READS_END</literal> tells
1040 DRD to stop ignoring the memory loads performed by the current thread.
1041 </para>
1042 </listitem>
1043 <listitem>
1044 <para>
1045 The macro <literal>ANNOTATE_IGNORE_WRITES_BEGIN</literal> tells
1046 DRD to ignore all memory stores performed by the current thread.
1047 </para>
1048 </listitem>
1049 <listitem>
1050 <para>
1051 The macro <literal>ANNOTATE_IGNORE_WRITES_END</literal> tells
1052 DRD to stop ignoring the memory stores performed by the current thread.
1053 </para>
1054 </listitem>
1055 <listitem>
1056 <para>
1057 The macro <literal>ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN</literal> tells
1058 DRD to ignore all memory accesses performed by the current thread.
1059 </para>
1060 </listitem>
1061 <listitem>
1062 <para>
1063 The macro <literal>ANNOTATE_IGNORE_READS_AND_WRITES_END</literal> tells
1064 DRD to stop ignoring the memory accesses performed by the current thread.
1065 </para>
1066 </listitem>
1067 <listitem>
1068 <para>
1069 The macro <literal>ANNOTATE_NEW_MEMORY(addr, size)</literal> tells
1070 DRD that the specified memory range has been allocated by a custom
1071 memory allocator in the client program and that the client program
1072 will start using this memory range.
1073 </para>
1074 </listitem>
1075 <listitem>
1076 <para>
1077 The macro <literal>ANNOTATE_THREAD_NAME(name)</literal> tells DRD to
1078 associate the specified name with the current thread and to include this
1079 name in the error messages printed by DRD.
1080 </para>
1081 </listitem>
njnf0f90372009-08-10 01:34:27 +00001082 <listitem>
1083 <para>
1084 The macros <literal>VALGRIND_MALLOCLIKE_BLOCK</literal> and
1085 <literal>VALGRIND_FREELIKE_BLOCK</literal> from the Valgrind core are
1086 implemented; they are described in
1087 <xref linkend="manual-core-adv.clientreq"/>.
1088 </para>
1089 </listitem>
bart1e7f2782008-07-01 13:43:44 +00001090</itemizedlist>
1091</para>
1092
1093<para>
barta617d112009-07-27 17:43:39 +00001094For an example of how to use the annotations for user-defined reader-writer
1095synchronization objects, see
bart291bb5e2009-08-15 13:31:41 +00001096also the source file <literal>drd/tests/annotate_rwlock.c</literal> in the
1097Valgrind source archive. And an example of how to
1098use the <literal>ANNOTATE_HAPPENS_BEFORE</literal> and
1099the <literal>ANNOTATE_HAPPENS_AFTER</literal> annotations can be found
1100in the source code of the <ulink url="http://code.google.com/chromium/">Chromium</ulink>
1101web browser.
barta617d112009-07-27 17:43:39 +00001102</para>
1103
1104<para>
bart1e7f2782008-07-01 13:43:44 +00001105Note: if you compiled Valgrind yourself, the header file
1106<literal>&lt;valgrind/drd.h&gt;</literal> will have been installed in
1107the directory <literal>/usr/include</literal> by the command
1108<literal>make install</literal>. If you obtained Valgrind by
1109installing it as a package however, you will probably have to install
1110another package with a name like <literal>valgrind-devel</literal>
bartdd742f22009-07-19 19:50:54 +00001111before Valgrind's header files are available.
bart5a5fe0c2008-06-15 12:22:37 +00001112</para>
1113
bart68bac812008-06-27 14:56:06 +00001114</sect2>
bart5a5fe0c2008-06-15 12:22:37 +00001115
1116
bart9ab8cac2008-07-07 18:38:17 +00001117<sect2 id="drd-manual.gnome" xreflabel="GNOME">
bart76ca62c2008-12-17 19:10:06 +00001118<title>Debugging GNOME Programs</title>
bart9ab8cac2008-07-07 18:38:17 +00001119
1120<para>
1121GNOME applications use the threading primitives provided by the
barte2624a92008-07-28 14:55:38 +00001122<computeroutput>glib</computeroutput> and
1123<computeroutput>gthread</computeroutput> libraries. These libraries
1124are built on top of POSIX threads, and hence are directly supported by
1125DRD. Please keep in mind that you have to call
njn2d6d5032009-08-07 05:23:31 +00001126<function>g_thread_init</function> before creating any threads, or
barte2624a92008-07-28 14:55:38 +00001127DRD will report several data races on glib functions. See also the
1128<ulink
1129url="http://library.gnome.org/devel/glib/stable/glib-Threads.html">GLib
1130Reference Manual</ulink> for more information about
njn2d6d5032009-08-07 05:23:31 +00001131<function>g_thread_init</function>.
bart9ab8cac2008-07-07 18:38:17 +00001132</para>
1133
bart39cf2682008-07-10 06:27:52 +00001134<para>
1135One of the many facilities provided by the <literal>glib</literal>
1136library is a block allocator, called <literal>g_slice</literal>. You
1137have to disable this block allocator when using DRD by adding the
1138following to the shell environment variables:
1139<literal>G_SLICE=always-malloc</literal>. See also the <ulink
1140url="http://library.gnome.org/devel/glib/stable/glib-Memory-Slices.html">GLib
1141Reference Manual</ulink> for more information.
1142</para>
1143
bart9ab8cac2008-07-07 18:38:17 +00001144</sect2>
1145
1146
1147<sect2 id="drd-manual.qt" xreflabel="Qt">
bart76ca62c2008-12-17 19:10:06 +00001148<title>Debugging Qt Programs</title>
bart9ab8cac2008-07-07 18:38:17 +00001149
1150<para>
1151The Qt library is the GUI library used by the KDE project. Currently
1152there are two versions of the Qt library in use: Qt3 by KDE 3 and Qt4
1153by KDE 4. If possible, use Qt4 instead of Qt3. Qt3 is no longer
1154supported, and there are known problems with multithreading support in
1155Qt3. As an example, using QString objects in more than one thread will
1156trigger race reports (this has been confirmed by Trolltech -- see also
barte2624a92008-07-28 14:55:38 +00001157Trolltech task <ulink
1158url="http://trolltech.com/developer/task-tracker/index_html">#206152</ulink>).
bart9ab8cac2008-07-07 18:38:17 +00001159</para>
1160
1161<para>
1162Qt4 applications are supported by DRD, but only if the
1163<literal>libqt4-debuginfo</literal> package has been installed. Some
1164of the synchronization and threading primitives in Qt4 bypass the
1165POSIX threads library, and DRD can only intercept these if symbol
1166information for the Qt4 library is available. DRD won't tell you if it
1167has not been able to load the Qt4 debug information, but a huge number
1168of data races will be reported on data protected via
1169<literal>QMutex</literal> objects.
1170</para>
1171
1172</sect2>
1173
1174
bart76ca62c2008-12-17 19:10:06 +00001175<sect2 id="drd-manual.boost.thread" xreflabel="Boost.Thread">
1176<title>Debugging Boost.Thread Programs</title>
1177
1178<para>
bartc57312c2008-12-17 19:15:58 +00001179The Boost.Thread library is the threading library included with the
1180cross-platform Boost Libraries. This threading library is an early
1181implementation of the upcoming C++0x threading library.
1182</para>
1183
1184<para>
1185Applications that use the Boost.Thread library should run fine under DRD.
bart76ca62c2008-12-17 19:10:06 +00001186</para>
1187
1188<para>
1189More information about Boost.Thread can be found here:
1190<itemizedlist>
1191 <listitem>
1192 <para>
1193 Anthony Williams, <ulink
1194 url="http://www.boost.org/doc/libs/1_37_0/doc/html/thread.html">Boost.Thread</ulink>
1195 Library Documentation, Boost website, 2007.
1196 </para>
1197 </listitem>
1198 <listitem>
1199 <para>
1200 Anthony Williams, <ulink
1201 url="http://www.ddj.com/cpp/211600441">What's New in Boost
1202 Threads?</ulink>, Recent changes to the Boost Thread library,
1203 Dr. Dobbs Magazine, October 2008.
1204 </para>
1205 </listitem>
1206</itemizedlist>
1207</para>
1208
1209</sect2>
1210
1211
bart68bac812008-06-27 14:56:06 +00001212<sect2 id="drd-manual.openmp" xreflabel="OpenMP">
bart76ca62c2008-12-17 19:10:06 +00001213<title>Debugging OpenMP Programs</title>
bart5a5fe0c2008-06-15 12:22:37 +00001214
1215<para>
bartdd742f22009-07-19 19:50:54 +00001216OpenMP stands for <emphasis>Open Multi-Processing</emphasis>. The OpenMP
1217standard consists of a set of compiler directives for C, C++ and Fortran
1218programs that allows a compiler to transform a sequential program into a
1219parallel program. OpenMP is well suited for HPC applications and allows to
1220work at a higher level compared to direct use of the POSIX threads API. While
1221OpenMP ensures that the POSIX API is used correctly, OpenMP programs can still
1222contain data races. So it definitely makes sense to verify OpenMP programs
1223with a thread checking tool.
bart1e7f2782008-07-01 13:43:44 +00001224</para>
1225
1226<para>
njn7316df22009-08-04 01:16:01 +00001227DRD supports OpenMP shared-memory programs generated by GCC. GCC
1228supports OpenMP since version 4.2.0. GCC's runtime support
bart1e7f2782008-07-01 13:43:44 +00001229for OpenMP programs is provided by a library called
bartdd742f22009-07-19 19:50:54 +00001230<literal>libgomp</literal>. The synchronization primitives implemented
bart1e7f2782008-07-01 13:43:44 +00001231in this library use Linux' futex system call directly, unless the
1232library has been configured with the
njna3311642009-08-10 01:29:14 +00001233<literal>--disable-linux-futex</literal> option. DRD only supports
1234libgomp libraries that have been configured with this option and in
bart1e7f2782008-07-01 13:43:44 +00001235which symbol information is present. For most Linux distributions this
njn7316df22009-08-04 01:16:01 +00001236means that you will have to recompile GCC. See also the script
bartef1b9722008-07-04 15:34:23 +00001237<literal>drd/scripts/download-and-build-gcc</literal> in the
njn7316df22009-08-04 01:16:01 +00001238Valgrind source tree for an example of how to compile GCC. You will
bart1e7f2782008-07-01 13:43:44 +00001239also have to make sure that the newly compiled
1240<literal>libgomp.so</literal> library is loaded when OpenMP programs
1241are started. This is possible by adding a line similar to the
1242following to your shell startup script:
1243</para>
1244<programlisting><![CDATA[
bartdd742f22009-07-19 19:50:54 +00001245export LD_LIBRARY_PATH=~/gcc-4.4.0/lib64:~/gcc-4.4.0/lib:
bart1e7f2782008-07-01 13:43:44 +00001246]]></programlisting>
1247
1248<para>
1249As an example, the test OpenMP test program
bart66ba8c02008-10-11 18:28:12 +00001250<literal>drd/tests/omp_matinv</literal> triggers a data race
bart1e7f2782008-07-01 13:43:44 +00001251when the option -r has been specified on the command line. The data
1252race is triggered by the following code:
1253</para>
1254<programlisting><![CDATA[
1255#pragma omp parallel for private(j)
1256for (j = 0; j < rows; j++)
1257{
1258 if (i != j)
1259 {
1260 const elem_t factor = a[j * cols + i];
1261 for (k = 0; k < cols; k++)
1262 {
1263 a[j * cols + k] -= a[i * cols + k] * factor;
1264 }
1265 }
1266}
1267]]></programlisting>
1268
1269<para>
1270The above code is racy because the variable <literal>k</literal> has
1271not been declared private. DRD will print the following error message
1272for the above code:
1273</para>
1274<programlisting><![CDATA[
barte2b98232009-07-22 18:13:21 +00001275$ valgrind --tool=drd --check-stack-var=yes --read-var-info=yes drd/tests/omp_matinv 3 -t 2 -r
bart1e7f2782008-07-01 13:43:44 +00001276...
1277Conflicting store by thread 1/1 at 0x7fefffbc4 size 4
1278 at 0x4014A0: gj.omp_fn.0 (omp_matinv.c:203)
1279 by 0x401211: gj (omp_matinv.c:159)
1280 by 0x40166A: invert_matrix (omp_matinv.c:238)
1281 by 0x4019B4: main (omp_matinv.c:316)
bartdd742f22009-07-19 19:50:54 +00001282Location 0x7fefffbc4 is 0 bytes inside local var "k"
1283declared at omp_matinv.c:160, in frame #0 of thread 1
bart1e7f2782008-07-01 13:43:44 +00001284...
1285]]></programlisting>
1286<para>
1287In the above output the function name <function>gj.omp_fn.0</function>
njn7316df22009-08-04 01:16:01 +00001288has been generated by GCC from the function name
bartdd742f22009-07-19 19:50:54 +00001289<function>gj</function>. The allocation context information shows that the
1290data race has been caused by modifying the variable <literal>k</literal>.
bart1e7f2782008-07-01 13:43:44 +00001291</para>
1292
1293<para>
njn7316df22009-08-04 01:16:01 +00001294Note: for GCC versions before 4.4.0, no allocation context information is
1295shown. With these GCC versions the most usable information in the above output
bartdd742f22009-07-19 19:50:54 +00001296is the source file name and the line number where the data race has been
1297detected (<literal>omp_matinv.c:203</literal>).
bart243ad392008-07-02 11:50:37 +00001298</para>
1299
1300<para>
bart68bac812008-06-27 14:56:06 +00001301For more information about OpenMP, see also
1302<ulink url="http://openmp.org/">openmp.org</ulink>.
1303</para>
1304
1305</sect2>
1306
1307
bart88f11412008-07-03 07:08:04 +00001308<sect2 id="drd-manual.cust-mem-alloc" xreflabel="Custom Memory Allocators">
1309<title>DRD and Custom Memory Allocators</title>
1310
1311<para>
bartdd742f22009-07-19 19:50:54 +00001312DRD tracks all memory allocation events that happen via the
bart88f11412008-07-03 07:08:04 +00001313standard memory allocation and deallocation functions
1314(<function>malloc</function>, <function>free</function>,
bartdd742f22009-07-19 19:50:54 +00001315<function>new</function> and <function>delete</function>), via entry
1316and exit of stack frames or that have been annotated with Valgrind's
1317memory pool client requests. DRD uses memory allocation and deallocation
bart88f11412008-07-03 07:08:04 +00001318information for two purposes:
1319<itemizedlist>
1320 <listitem>
1321 <para>
1322 To know where the scope ends of POSIX objects that have not been
1323 destroyed explicitly. It is e.g. not required by the POSIX
1324 threads standard to call
njn2d6d5032009-08-07 05:23:31 +00001325 <function>pthread_mutex_destroy</function> before freeing the
bart88f11412008-07-03 07:08:04 +00001326 memory in which a mutex object resides.
1327 </para>
1328 </listitem>
1329 <listitem>
1330 <para>
1331 To know where the scope of variables ends. If e.g. heap memory
1332 has been used by one thread, that thread frees that memory, and
1333 another thread allocates and starts using that memory, no data
1334 races must be reported for that memory.
1335 </para>
1336 </listitem>
1337</itemizedlist>
1338</para>
1339
1340<para>
1341It is essential for correct operation of DRD that the tool knows about
bartdd742f22009-07-19 19:50:54 +00001342memory allocation and deallocation events. When analyzing a client program
1343with DRD that uses a custom memory allocator, either instrument the custom
njn2d6d5032009-08-07 05:23:31 +00001344memory allocator with the <literal>VALGRIND_MALLOCLIKE_BLOCK</literal>
1345and <literal>VALGRIND_FREELIKE_BLOCK</literal> macros or disable the
bartdd742f22009-07-19 19:50:54 +00001346custom memory allocator.
1347</para>
1348
1349<para>
1350As an example, the GNU libstdc++ library can be configured
bart88f11412008-07-03 07:08:04 +00001351to use standard memory allocation functions instead of memory pools by
1352setting the environment variable
1353<literal>GLIBCXX_FORCE_NEW</literal>. For more information, see also
1354the <ulink
1355url="http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html">libstdc++
1356manual</ulink>.
1357</para>
1358
1359</sect2>
1360
1361
1362<sect2 id="drd-manual.drd-versus-memcheck" xreflabel="DRD Versus Memcheck">
1363<title>DRD Versus Memcheck</title>
1364
1365<para>
1366It is essential for correct operation of DRD that there are no memory
bart4ac853b2009-01-02 13:29:32 +00001367errors such as dangling pointers in the client program. Which means that
njn2d6d5032009-08-07 05:23:31 +00001368it is a good idea to make sure that your program is Memcheck-clean
bart88f11412008-07-03 07:08:04 +00001369before you analyze it with DRD. It is possible however that some of
njn2d6d5032009-08-07 05:23:31 +00001370the Memcheck reports are caused by data races. In this case it makes
1371sense to run DRD before Memcheck.
bart88f11412008-07-03 07:08:04 +00001372</para>
1373
1374<para>
njn2d6d5032009-08-07 05:23:31 +00001375So which tool should be run first? In case both DRD and Memcheck
bart88f11412008-07-03 07:08:04 +00001376complain about a program, a possible approach is to run both tools
1377alternatingly and to fix as many errors as possible after each run of
1378each tool until none of the two tools prints any more error messages.
1379</para>
1380
1381</sect2>
1382
1383
bart3d986d62008-07-05 14:25:09 +00001384<sect2 id="drd-manual.resource-requirements" xreflabel="Resource Requirements">
bart88f11412008-07-03 07:08:04 +00001385<title>Resource Requirements</title>
1386
1387<para>
1388The requirements of DRD with regard to heap and stack memory and the
1389effect on the execution time of client programs are as follows:
1390<itemizedlist>
1391 <listitem>
1392 <para>
1393 When running a program under DRD with default DRD options,
1394 between 1.1 and 3.6 times more memory will be needed compared to
1395 a native run of the client program. More memory will be needed
1396 if loading debug information has been enabled
barte2b98232009-07-22 18:13:21 +00001397 (<literal>--read-var-info=yes</literal>).
bart88f11412008-07-03 07:08:04 +00001398 </para>
1399 </listitem>
1400 <listitem>
1401 <para>
1402 DRD allocates some of its temporary data structures on the stack
1403 of the client program threads. This amount of data is limited to
1404 1 - 2 KB. Make sure that thread stacks are sufficiently large.
1405 </para>
1406 </listitem>
1407 <listitem>
1408 <para>
1409 Most applications will run between 20 and 50 times slower under
bartdd742f22009-07-19 19:50:54 +00001410 DRD than a native single-threaded run. The slowdown will be most
njn2d6d5032009-08-07 05:23:31 +00001411 noticeable for applications which perform frequent mutex lock /
bartdd742f22009-07-19 19:50:54 +00001412 unlock operations.
bart88f11412008-07-03 07:08:04 +00001413 </para>
1414 </listitem>
1415</itemizedlist>
1416</para>
1417
1418</sect2>
1419
1420
bart3d986d62008-07-05 14:25:09 +00001421<sect2 id="drd-manual.effective-use" xreflabel="Effective Use">
1422<title>Hints and Tips for Effective Use of DRD</title>
1423
1424<para>
1425The following information may be helpful when using DRD:
1426<itemizedlist>
1427 <listitem>
1428 <para>
1429 Make sure that debug information is present in the executable
bartdd742f22009-07-19 19:50:54 +00001430 being analyzed, such that DRD can print function name and line
bart3d986d62008-07-05 14:25:09 +00001431 number information in stack traces. Most compilers can be told
1432 to include debug information via compiler option
1433 <option>-g</option>.
1434 </para>
1435 </listitem>
1436 <listitem>
1437 <para>
njna3311642009-08-10 01:29:14 +00001438 Compile with option <option>-O1</option> instead of
bart3d986d62008-07-05 14:25:09 +00001439 <option>-O0</option>. This will reduce the amount of generated
1440 code, may reduce the amount of debug info and will speed up
1441 DRD's processing of the client program. For more information,
1442 see also <xref linkend="manual-core.started"/>.
1443 </para>
1444 </listitem>
1445 <listitem>
1446 <para>
1447 If DRD reports any errors on libraries that are part of your
1448 Linux distribution like e.g. <literal>libc.so</literal> or
1449 <literal>libstdc++.so</literal>, installing the debug packages
1450 for these libraries will make the output of DRD a lot more
1451 detailed.
1452 </para>
1453 </listitem>
1454 <listitem>
1455 <para>
1456 When using C++, do not send output from more than one thread to
1457 <literal>std::cout</literal>. Doing so would not only
1458 generate multiple data race reports, it could also result in
1459 output from several threads getting mixed up. Either use
njn2d6d5032009-08-07 05:23:31 +00001460 <function>printf</function> or do the following:
bart3d986d62008-07-05 14:25:09 +00001461 <orderedlist>
1462 <listitem>
1463 <para>Derive a class from <literal>std::ostreambuf</literal>
1464 and let that class send output line by line to
1465 <literal>stdout</literal>. This will avoid that individual
1466 lines of text produced by different threads get mixed
1467 up.</para>
1468 </listitem>
1469 <listitem>
1470 <para>Create one instance of <literal>std::ostream</literal>
1471 for each thread. This makes stream formatting settings
1472 thread-local. Pass a per-thread instance of the class
1473 derived from <literal>std::ostreambuf</literal> to the
1474 constructor of each instance. </para>
1475 </listitem>
1476 <listitem>
1477 <para>Let each thread send its output to its own instance of
1478 <literal>std::ostream</literal> instead of
1479 <literal>std::cout</literal>.</para>
1480 </listitem>
1481 </orderedlist>
1482 </para>
1483 </listitem>
1484</itemizedlist>
1485</para>
1486
1487</sect2>
1488
1489
bart5a5fe0c2008-06-15 12:22:37 +00001490</sect1>
1491
1492
bart66ba8c02008-10-11 18:28:12 +00001493<sect1 id="drd-manual.Pthreads" xreflabel="Pthreads">
1494<title>Using the POSIX Threads API Effectively</title>
1495
1496<sect2 id="drd-manual.mutex-types" xreflabel="mutex-types">
1497<title>Mutex types</title>
1498
1499<para>
1500The Single UNIX Specification version two defines the following four
1501mutex types (see also the documentation of <ulink
njn2d6d5032009-08-07 05:23:31 +00001502url="http://www.opengroup.org/onlinepubs/007908799/xsh/pthread_mutexattr_settype.html"><function>pthread_mutexattr_settype</function></ulink>):
bart66ba8c02008-10-11 18:28:12 +00001503<itemizedlist>
1504 <listitem>
1505 <para>
1506 <emphasis>normal</emphasis>, which means that no error checking
1507 is performed, and that the mutex is non-recursive.
1508 </para>
1509 </listitem>
1510 <listitem>
1511 <para>
1512 <emphasis>error checking</emphasis>, which means that the mutex
1513 is non-recursive and that error checking is performed.
1514 </para>
1515 </listitem>
1516 <listitem>
1517 <para>
1518 <emphasis>recursive</emphasis>, which means that a mutex may be
1519 locked recursively.
1520 </para>
1521 </listitem>
1522 <listitem>
1523 <para>
1524 <emphasis>default</emphasis>, which means that error checking
1525 behavior is undefined, and that the behavior for recursive
1526 locking is also undefined. Or: portable code must neither
1527 trigger error conditions through the Pthreads API nor attempt to
1528 lock a mutex of default type recursively.
1529 </para>
1530 </listitem>
1531</itemizedlist>
1532</para>
1533
1534<para>
1535In complex applications it is not always clear from beforehand which
1536mutex will be locked recursively and which mutex will not be locked
1537recursively. Attempts lock a non-recursive mutex recursively will
1538result in race conditions that are very hard to find without a thread
1539checking tool. So either use the error checking mutex type and
1540consistently check the return value of Pthread API mutex calls, or use
1541the recursive mutex type.
1542</para>
1543
1544</sect2>
1545
1546<sect2 id="drd-manual.condvar" xreflabel="condition-variables">
1547<title>Condition variables</title>
1548
1549<para>
1550A condition variable allows one thread to wake up one or more other
bart4ac853b2009-01-02 13:29:32 +00001551threads. Condition variables are often used to notify one or more
bart66ba8c02008-10-11 18:28:12 +00001552threads about state changes of shared data. Unfortunately it is very
1553easy to introduce race conditions by using condition variables as the
1554only means of state information propagation. A better approach is to
1555let threads poll for changes of a state variable that is protected by
1556a mutex, and to use condition variables only as a thread wakeup
1557mechanism. See also the source file
1558<computeroutput>drd/tests/monitor_example.cpp</computeroutput> for an
1559example of how to implement this concept in C++. The monitor concept
bart4ac853b2009-01-02 13:29:32 +00001560used in this example is a well known and very useful concept -- see
1561also Wikipedia for more information about the <ulink
bart66ba8c02008-10-11 18:28:12 +00001562url="http://en.wikipedia.org/wiki/Monitor_(synchronization)">monitor</ulink>
1563concept.
1564</para>
1565
1566</sect2>
1567
1568<sect2 id="drd-manual.pctw" xreflabel="pthread_cond_timedwait">
njn2d6d5032009-08-07 05:23:31 +00001569<title><function>pthread_cond_timedwait</function> and timeouts</title>
bart66ba8c02008-10-11 18:28:12 +00001570
1571<para>
1572Historically the function
njn2d6d5032009-08-07 05:23:31 +00001573<function>pthread_cond_timedwait</function> only allowed the
bart66ba8c02008-10-11 18:28:12 +00001574specification of an absolute timeout, that is a timeout independent of
1575the time when this function was called. However, almost every call to
1576this function expresses a relative timeout. This typically happens by
1577passing the sum of
1578<computeroutput>clock_gettime(CLOCK_REALTIME)</computeroutput> and a
1579relative timeout as the third argument. This approach is incorrect
1580since forward or backward clock adjustments by e.g. ntpd will affect
1581the timeout. A more reliable approach is as follows:
1582<itemizedlist>
1583 <listitem>
1584 <para>
1585 When initializing a condition variable through
njn2d6d5032009-08-07 05:23:31 +00001586 <function>pthread_cond_init</function>, specify that the timeout of
1587 <function>pthread_cond_timedwait</function> will use the clock
bart66ba8c02008-10-11 18:28:12 +00001588 <literal>CLOCK_MONOTONIC</literal> instead of
1589 <literal>CLOCK_REALTIME</literal>. You can do this via
1590 <computeroutput>pthread_condattr_setclock(...,
bart4ac853b2009-01-02 13:29:32 +00001591 CLOCK_MONOTONIC)</computeroutput>.
bart66ba8c02008-10-11 18:28:12 +00001592 </para>
1593 </listitem>
1594 <listitem>
1595 <para>
njn2d6d5032009-08-07 05:23:31 +00001596 When calling <function>pthread_cond_timedwait</function>, pass
bart66ba8c02008-10-11 18:28:12 +00001597 the sum of
1598 <computeroutput>clock_gettime(CLOCK_MONOTONIC)</computeroutput>
1599 and a relative timeout as the third argument.
1600 </para>
1601 </listitem>
1602</itemizedlist>
bart4ac853b2009-01-02 13:29:32 +00001603See also
1604<computeroutput>drd/tests/monitor_example.cpp</computeroutput> for an
1605example.
bart66ba8c02008-10-11 18:28:12 +00001606</para>
1607
1608</sect2>
1609
1610<sect2 id="drd-manual.naming-threads" xreflabel="naming threads">
1611<title>Assigning names to threads</title>
1612
1613<para>
1614Many applications log information about changes in internal or
1615external state to a file. When analyzing log files of a multithreaded
1616application it can be very convenient to know which thread logged
1617which information. One possible approach is to identify threads in
1618logging output by including the result of
njn2d6d5032009-08-07 05:23:31 +00001619<function>pthread_self</function> in every log line. However, this approach
bart66ba8c02008-10-11 18:28:12 +00001620has two disadvantages: there is no direct relationship between these
1621values and the source code and these values can be different in each
1622run. A better approach is to assign a brief name to each thread and to
1623include the assigned thread name in each log line. One possible
1624approach for managing thread names is as follows:
1625<itemizedlist>
1626 <listitem>
1627 <para>
1628 Allocate a key for the pointer to the thread name through
njn2d6d5032009-08-07 05:23:31 +00001629 <function>pthread_key_create</function>.
bart66ba8c02008-10-11 18:28:12 +00001630 </para>
1631 </listitem>
1632 <listitem>
1633 <para>
1634 Just after thread creation, set the thread name through
njn2d6d5032009-08-07 05:23:31 +00001635 <function>pthread_setspecific</function>.
bart66ba8c02008-10-11 18:28:12 +00001636 </para>
1637 </listitem>
1638 <listitem>
1639 <para>
1640 In the code that generates the logging information, query the thread
njn2d6d5032009-08-07 05:23:31 +00001641 name by calling <function>pthread_getspecific</function>.
bart66ba8c02008-10-11 18:28:12 +00001642 </para>
1643 </listitem>
1644</itemizedlist>
1645
1646</para>
1647
1648</sect2>
1649
1650</sect1>
1651
1652
barte3e54df2008-06-12 15:20:42 +00001653<sect1 id="drd-manual.limitations" xreflabel="Limitations">
bart5a5fe0c2008-06-15 12:22:37 +00001654<title>Limitations</title>
barte3e54df2008-06-12 15:20:42 +00001655
1656<para>DRD currently has the following limitations:</para>
1657
1658<itemizedlist>
bart68bac812008-06-27 14:56:06 +00001659 <listitem>
1660 <para>
njn2d6d5032009-08-07 05:23:31 +00001661 DRD has only been tested on Linux and Mac OS X.
bart68bac812008-06-27 14:56:06 +00001662 </para>
barte3e54df2008-06-12 15:20:42 +00001663 </listitem>
bart68bac812008-06-27 14:56:06 +00001664 <listitem>
1665 <para>
1666 Of the two POSIX threads implementations for Linux, only the
1667 NPTL (Native POSIX Thread Library) is supported. The older
1668 LinuxThreads library is not supported.
1669 </para>
barte3e54df2008-06-12 15:20:42 +00001670 </listitem>
bart68bac812008-06-27 14:56:06 +00001671 <listitem>
1672 <para>
njn2d6d5032009-08-07 05:23:31 +00001673 DRD, just like Memcheck, will refuse to start on Linux
bart68bac812008-06-27 14:56:06 +00001674 distributions where all symbol information has been removed from
njn2d6d5032009-08-07 05:23:31 +00001675 <filename>ld.so</filename>. This is e.g. the case for the PPC editions
1676 of openSUSE and Gentoo. You will have to install the glibc debuginfo
1677 package on these platforms before you can use DRD. See also openSUSE
1678 bug <ulink url="http://bugzilla.novell.com/show_bug.cgi?id=396197">
bart68bac812008-06-27 14:56:06 +00001679 396197</ulink> and Gentoo bug <ulink
1680 url="http://bugs.gentoo.org/214065">214065</ulink>.
1681 </para>
1682 </listitem>
1683 <listitem>
1684 <para>
bart243ad392008-07-02 11:50:37 +00001685 When address tracing is enabled, no information on atomic stores
1686 will be displayed. This functionality is easy to add
1687 however. Please contact the Valgrind authors if you would like
1688 to see this functionality enabled.
1689 </para>
1690 </listitem>
1691 <listitem>
1692 <para>
njn7316df22009-08-04 01:16:01 +00001693 If you compile the DRD source code yourself, you need GCC 3.0 or
1694 later. GCC 2.95 is not supported.
bart68bac812008-06-27 14:56:06 +00001695 </para>
barte3e54df2008-06-12 15:20:42 +00001696 </listitem>
barte3e54df2008-06-12 15:20:42 +00001697</itemizedlist>
1698
1699</sect1>
1700
bart68bac812008-06-27 14:56:06 +00001701
1702<sect1 id="drd-manual.feedback" xreflabel="Feedback">
1703<title>Feedback</title>
1704
1705<para>
1706If you have any comments, suggestions, feedback or bug reports about
1707DRD, feel free to either post a message on the Valgrind users mailing
1708list or to file a bug report. See also <ulink
bart4ac853b2009-01-02 13:29:32 +00001709url="&vg-url;">&vg-url;</ulink> for more information.
bart68bac812008-06-27 14:56:06 +00001710</para>
1711
1712</sect1>
1713
1714
barte3e54df2008-06-12 15:20:42 +00001715</chapter>