]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/valgrind/src/valgrind-3.6.0-svn/drd/drd_main.c
Inital import
[l4.git] / l4 / pkg / valgrind / src / valgrind-3.6.0-svn / drd / drd_main.c
1 /* -*- mode: C; c-basic-offset: 3; -*- */
2 /*
3   This file is part of drd, a thread error detector.
4
5   Copyright (C) 2006-2010 Bart Van Assche <bart.vanassche@gmail.com>.
6
7   This program is free software; you can redistribute it and/or
8   modify it under the terms of the GNU General Public License as
9   published by the Free Software Foundation; either version 2 of the
10   License, or (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20   02111-1307, USA.
21
22   The GNU General Public License is contained in the file COPYING.
23 */
24
25
26 #include "drd_barrier.h"
27 #include "drd_clientobj.h"
28 #include "drd_clientreq.h"
29 #include "drd_cond.h"
30 #include "drd_error.h"
31 #include "drd_load_store.h"
32 #include "drd_malloc_wrappers.h"
33 #include "drd_mutex.h"
34 #include "drd_rwlock.h"
35 #include "drd_segment.h"
36 #include "drd_semaphore.h"
37 #include "drd_suppression.h"
38 #include "drd_thread.h"
39 #include "libvex_guest_offsets.h"
40 #include "pub_drd_bitmap.h"
41 #include "pub_tool_vki.h"         // Must be included before pub_tool_libcproc
42 #include "pub_tool_basics.h"
43 #include "pub_tool_debuginfo.h"   // VG_(describe_IP)()
44 #include "pub_tool_libcassert.h"  // tl_assert()
45 #include "pub_tool_libcbase.h"    // VG_(strcmp)
46 #include "pub_tool_libcprint.h"   // VG_(printf)
47 #include "pub_tool_libcproc.h"
48 #include "pub_tool_machine.h"
49 #include "pub_tool_mallocfree.h"  // VG_(malloc)(), VG_(free)()
50 #include "pub_tool_options.h"     // command line options
51 #include "pub_tool_replacemalloc.h"
52 #include "pub_tool_threadstate.h" // VG_(get_running_tid)()
53 #include "pub_tool_tooliface.h"
54
55
56 /* Local variables. */
57
58 static Bool DRD_(s_print_stats)      = False;
59 static Bool DRD_(s_var_info)         = False;
60 static Bool DRD_(s_show_stack_usage) = False;
61
62
63 /**
64  * Implement the needs_command_line_options for drd.
65  */
66 static Bool DRD_(process_cmd_line_option)(Char* arg)
67 {
68    int check_stack_accesses   = -1;
69    int exclusive_threshold_ms = -1;
70    int first_race_only        = -1;
71    int report_signal_unlocked = -1;
72    int segment_merging        = -1;
73    int segment_merge_interval = -1;
74    int shared_threshold_ms    = -1;
75    int show_confl_seg         = -1;
76    int trace_barrier          = -1;
77    int trace_clientobj        = -1;
78    int trace_cond             = -1;
79    int trace_csw              = -1;
80    int trace_fork_join        = -1;
81    int trace_conflict_set     = -1;
82    int trace_conflict_set_bm  = -1;
83    int trace_mutex            = -1;
84    int trace_rwlock           = -1;
85    int trace_segment          = -1;
86    int trace_semaphore        = -1;
87    int trace_suppression      = -1;
88    Char* trace_address        = 0;
89
90    if      VG_BOOL_CLO(arg, "--check-stack-var",     check_stack_accesses) {}
91    else if VG_BOOL_CLO(arg, "--drd-stats",           DRD_(s_print_stats)) {}
92    else if VG_BOOL_CLO(arg, "--first-race-only",     first_race_only) {}
93    else if VG_BOOL_CLO(arg,"--report-signal-unlocked",report_signal_unlocked)
94    {}
95    else if VG_BOOL_CLO(arg, "--segment-merging",     segment_merging) {}
96    else if VG_INT_CLO (arg, "--segment-merging-interval", segment_merge_interval)
97    {}
98    else if VG_BOOL_CLO(arg, "--show-confl-seg",      show_confl_seg) {}
99    else if VG_BOOL_CLO(arg, "--show-stack-usage",
100                        DRD_(s_show_stack_usage)) {}
101    else if VG_BOOL_CLO(arg, "--trace-barrier",       trace_barrier) {}
102    else if VG_BOOL_CLO(arg, "--trace-clientobj",     trace_clientobj) {}
103    else if VG_BOOL_CLO(arg, "--trace-cond",          trace_cond) {}
104    else if VG_BOOL_CLO(arg, "--trace-conflict-set",  trace_conflict_set) {}
105    else if VG_BOOL_CLO(arg, "--trace-conflict-set-bm", trace_conflict_set_bm){}
106    else if VG_BOOL_CLO(arg, "--trace-csw",           trace_csw) {}
107    else if VG_BOOL_CLO(arg, "--trace-fork-join",     trace_fork_join) {}
108    else if VG_BOOL_CLO(arg, "--trace-mutex",         trace_mutex) {}
109    else if VG_BOOL_CLO(arg, "--trace-rwlock",        trace_rwlock) {}
110    else if VG_BOOL_CLO(arg, "--trace-segment",       trace_segment) {}
111    else if VG_BOOL_CLO(arg, "--trace-semaphore",     trace_semaphore) {}
112    else if VG_BOOL_CLO(arg, "--trace-suppr",         trace_suppression) {}
113    else if VG_BOOL_CLO(arg, "--var-info",            DRD_(s_var_info)) {}
114    else if VG_INT_CLO (arg, "--exclusive-threshold", exclusive_threshold_ms) {}
115    else if VG_INT_CLO (arg, "--shared-threshold",    shared_threshold_ms)    {}
116    else if VG_STR_CLO (arg, "--trace-addr",          trace_address) {}
117    else
118       return VG_(replacement_malloc_process_cmd_line_option)(arg);
119
120    if (check_stack_accesses != -1)
121       DRD_(set_check_stack_accesses)(check_stack_accesses);
122    if (exclusive_threshold_ms != -1)
123    {
124       DRD_(mutex_set_lock_threshold)(exclusive_threshold_ms);
125       DRD_(rwlock_set_exclusive_threshold)(exclusive_threshold_ms);
126    }
127    if (first_race_only != -1)
128    {
129       DRD_(set_first_race_only)(first_race_only);
130    }
131    if (report_signal_unlocked != -1)
132    {
133       DRD_(cond_set_report_signal_unlocked)(report_signal_unlocked);
134    }
135    if (shared_threshold_ms != -1)
136    {
137       DRD_(rwlock_set_shared_threshold)(shared_threshold_ms);
138    }
139    if (segment_merging != -1)
140       DRD_(thread_set_segment_merging)(segment_merging);
141    if (segment_merge_interval != -1)
142       DRD_(thread_set_segment_merge_interval)(segment_merge_interval);
143    if (show_confl_seg != -1)
144       DRD_(set_show_conflicting_segments)(show_confl_seg);
145    if (trace_address)
146    {
147       const Addr addr = VG_(strtoll16)(trace_address, 0);
148       DRD_(start_tracing_address_range)(addr, addr + 1);
149    }
150    if (trace_barrier != -1)
151       DRD_(barrier_set_trace)(trace_barrier);
152    if (trace_clientobj != -1)
153       DRD_(clientobj_set_trace)(trace_clientobj);
154    if (trace_cond != -1)
155       DRD_(cond_set_trace)(trace_cond);
156    if (trace_csw != -1)
157       DRD_(thread_trace_context_switches)(trace_csw);
158    if (trace_fork_join != -1)
159       DRD_(thread_set_trace_fork_join)(trace_fork_join);
160    if (trace_conflict_set != -1)
161       DRD_(thread_trace_conflict_set)(trace_conflict_set);
162    if (trace_conflict_set_bm != -1)
163       DRD_(thread_trace_conflict_set_bm)(trace_conflict_set_bm);
164    if (trace_mutex != -1)
165       DRD_(mutex_set_trace)(trace_mutex);
166    if (trace_rwlock != -1)
167       DRD_(rwlock_set_trace)(trace_rwlock);
168    if (trace_segment != -1)
169       DRD_(sg_set_trace)(trace_segment);
170    if (trace_semaphore != -1)
171       DRD_(semaphore_set_trace)(trace_semaphore);
172    if (trace_suppression != -1)
173       DRD_(suppression_set_trace)(trace_suppression);
174
175    return True;
176 }
177
178 static void DRD_(print_usage)(void)
179 {
180    VG_(printf)(
181 "    --check-stack-var=yes|no  Whether or not to report data races on\n"
182 "                              stack variables [no].\n"
183 "    --exclusive-threshold=<n> Print an error message if any mutex or\n"
184 "        writer lock is held longer than the specified time (in milliseconds).\n"
185 "    --first-race-only=yes|no  Only report the first data race that occurs on\n"
186 "                              a memory location instead of all races [no].\n"
187 "    --report-signal-unlocked=yes|no Whether to report calls to\n"
188 "                              pthread_cond_signal() where the mutex associated\n"
189 "                              with the signal via pthread_cond_wait() is not\n"
190 "                              locked at the time the signal is sent [yes].\n"
191 "    --segment-merging=yes|no  Controls segment merging [yes].\n"
192 "        Segment merging is an algorithm to limit memory usage of the\n"
193 "        data race detection algorithm. Disabling segment merging may\n"
194 "        improve the accuracy of the so-called 'other segments' displayed\n"
195 "        in race reports but can also trigger an out of memory error.\n"
196 "    --segment-merging-interval=<n> Perform segment merging every time n new\n"
197 "        segments have been created. Default: %d.\n"
198 "    --shared-threshold=<n>    Print an error message if a reader lock\n"
199 "        is held longer than the specified time (in milliseconds).\n"
200 "    --show-confl-seg=yes|no   Show conflicting segments in race reports [yes].\n"
201 "    --show-stack-usage=yes|no Print stack usage at thread exit time [no].\n"
202 "\n"
203 "  drd options for monitoring process behavior:\n"
204 "    --trace-addr=<address>    Trace all load and store activity for the.\n"
205 "                              specified address [off].\n"
206 "    --trace-barrier=yes|no    Trace all barrier activity [no].\n"
207 "    --trace-cond=yes|no       Trace all condition variable activity [no].\n"
208 "    --trace-fork-join=yes|no  Trace all thread fork/join activity [no].\n"
209 "    --trace-mutex=yes|no      Trace all mutex activity [no].\n"
210 "    --trace-rwlock=yes|no     Trace all reader-writer lock activity[no].\n"
211 "    --trace-semaphore=yes|no  Trace all semaphore activity [no].\n",
212 DRD_(thread_get_segment_merge_interval)()
213 );
214 }
215
216 static void DRD_(print_debug_usage)(void)
217 {
218    VG_(printf)(
219 "    --drd-stats=yes|no        Print statistics about DRD activity [no].\n"
220 "    --trace-clientobj=yes|no  Trace all client object activity [no].\n"
221 "    --trace-csw=yes|no        Trace all scheduler context switches [no].\n"
222 "    --trace-conflict-set=yes|no Trace all conflict set updates [no].\n"
223 "    --trace-conflict-set-bm=yes|no Trace all conflict set bitmap\n"
224 "                              updates [no]. Note: enabling this option\n"
225 "                              will generate a lot of output !\n"
226 "    --trace-segment=yes|no    Trace segment actions [no].\n"
227 "    --trace-suppr=yes|no      Trace all address suppression actions [no].\n"
228 );
229 }
230
231
232 //
233 // Implements the thread-related core callbacks.
234 //
235
236 static void drd_pre_mem_read(const CorePart part,
237                              const ThreadId tid,
238                              Char* const s,
239                              const Addr a,
240                              const SizeT size)
241 {
242    if (size > 0)
243    {
244       DRD_(trace_load)(a, size);
245    }
246 }
247
248 static void drd_pre_mem_read_asciiz(const CorePart part,
249                                     const ThreadId tid,
250                                     Char* const s,
251                                     const Addr a)
252 {
253    const char* p = (void*)a;
254    SizeT size = 0;
255
256    /* Note: the expression '*p' reads client memory and may crash if the */
257    /* client provided an invalid pointer !                               */
258    while (*p)
259    {
260       p++;
261       size++;
262    }
263    if (size > 0)
264    {
265       DRD_(trace_load)(a, size);
266    }
267 }
268
269 static void drd_post_mem_write(const CorePart part,
270                                const ThreadId tid,
271                                const Addr a,
272                                const SizeT size)
273 {
274    DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
275    if (size > 0)
276    {
277       DRD_(trace_store)(a, size);
278    }
279 }
280
281 static __inline__
282 void drd_start_using_mem(const Addr a1, const SizeT len)
283 {
284    tl_assert(a1 < a1 + len);
285
286    if (UNLIKELY(DRD_(any_address_is_traced)()))
287    {
288       DRD_(trace_mem_access)(a1, len, eStart);
289    }
290
291    if (UNLIKELY(DRD_(running_thread_inside_pthread_create)()))
292    {
293       DRD_(start_suppression)(a1, a1 + len, "pthread_create()");
294    }
295 }
296
297 static void drd_start_using_mem_w_ecu(const Addr a1,
298                                       const SizeT len,
299                                       UInt ec_uniq)
300 {
301    drd_start_using_mem(a1, len);
302 }
303
304 static void drd_start_using_mem_w_tid(const Addr a1,
305                                       const SizeT len,
306                                       ThreadId tid)
307 {
308    drd_start_using_mem(a1, len);
309 }
310
311 static __inline__
312 void drd_stop_using_mem(const Addr a1, const SizeT len,
313                         const Bool is_stack_mem)
314 {
315    const Addr a2 = a1 + len;
316
317    tl_assert(a1 < a2);
318
319    if (UNLIKELY(DRD_(any_address_is_traced)()))
320    {
321       DRD_(trace_mem_access)(a1, len, eEnd);
322    }
323    if (! is_stack_mem || DRD_(get_check_stack_accesses)())
324    {
325       DRD_(thread_stop_using_mem)(a1, a2);
326       DRD_(clientobj_stop_using_mem)(a1, a2);
327       DRD_(suppression_stop_using_mem)(a1, a2);
328    }
329 }
330
331 static __inline__
332 void drd_stop_using_nonstack_mem(const Addr a1, const SizeT len)
333 {
334    drd_stop_using_mem(a1, len, False);
335 }
336
337 /**
338  * Discard all information DRD has about memory accesses and client objects
339  * in the specified address range.
340  */
341 void DRD_(clean_memory)(const Addr a1, const SizeT len)
342 {
343    const Bool is_stack_memory = DRD_(thread_address_on_any_stack)(a1);
344    drd_stop_using_mem(a1, len, is_stack_memory);
345    drd_start_using_mem(a1, len);
346 }
347
348 /**
349  * Suppress data race reports on all addresses contained in .plt and
350  * .got.plt sections inside the address range [ a, a + len [. The data in
351  * these sections is modified by _dl_relocate_object() every time a function
352  * in a shared library is called for the first time. Since the first call
353  * to a function in a shared library can happen from a multithreaded context,
354  * such calls can cause conflicting accesses. See also Ulrich Drepper's
355  * paper "How to Write Shared Libraries" for more information about relocation
356  * (http://people.redhat.com/drepper/dsohowto.pdf).
357  */
358 static void DRD_(suppress_relocation_conflicts)(const Addr a, const SizeT len)
359 {
360    const DebugInfo* di;
361
362 #if 0
363    VG_(printf)("Evaluating range @ 0x%lx size %ld\n", a, len);
364 #endif
365
366    for (di = VG_(next_DebugInfo)(0); di; di = VG_(next_DebugInfo)(di))
367    {
368       Addr  avma;
369       SizeT size;
370
371       avma = VG_(DebugInfo_get_plt_avma)(di);
372       size = VG_(DebugInfo_get_plt_size)(di);
373       tl_assert((avma && size) || (avma == 0 && size == 0));
374       if (size > 0)
375       {
376 #if 0
377          VG_(printf)("Suppressing .plt @ 0x%lx size %ld\n", avma, size);
378 #endif
379          tl_assert(VG_(DebugInfo_sect_kind)(NULL, 0, avma) == Vg_SectPLT);
380          DRD_(start_suppression)(avma, avma + size, ".plt");
381       }
382
383       avma = VG_(DebugInfo_get_gotplt_avma)(di);
384       size = VG_(DebugInfo_get_gotplt_size)(di);
385       tl_assert((avma && size) || (avma == 0 && size == 0));
386       if (size > 0)
387       {
388 #if 0
389          VG_(printf)("Suppressing .got.plt @ 0x%lx size %ld\n", avma, size);
390 #endif
391          tl_assert(VG_(DebugInfo_sect_kind)(NULL, 0, avma) == Vg_SectGOTPLT);
392          DRD_(start_suppression)(avma, avma + size, ".gotplt");
393       }
394    }
395 }
396
397 static
398 void drd_start_using_mem_w_perms(const Addr a, const SizeT len,
399                                  const Bool rr, const Bool ww, const Bool xx,
400                                  ULong di_handle)
401 {
402    DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
403
404    drd_start_using_mem(a, len);
405
406    DRD_(suppress_relocation_conflicts)(a, len);
407 }
408
409 /* Called by the core when the stack of a thread grows, to indicate that */
410 /* the addresses in range [ a, a + len [ may now be used by the client.  */
411 /* Assumption: stacks grow downward.                                     */
412 static __inline__
413 void drd_start_using_mem_stack(const Addr a, const SizeT len)
414 {
415    DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(),
416                               a - VG_STACK_REDZONE_SZB);
417    drd_start_using_mem(a - VG_STACK_REDZONE_SZB,
418                        len + VG_STACK_REDZONE_SZB);
419 }
420
421 /* Called by the core when the stack of a thread shrinks, to indicate that */
422 /* the addresses [ a, a + len [ are no longer accessible for the client.   */
423 /* Assumption: stacks grow downward.                                       */
424 static __inline__
425 void drd_stop_using_mem_stack(const Addr a, const SizeT len)
426 {
427    DRD_(thread_set_stack_min)(DRD_(thread_get_running_tid)(),
428                               a + len - VG_STACK_REDZONE_SZB);
429    drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB,
430                       True);
431 }
432
433 /**
434  * Callback function called by the Valgrind core before a stack area is
435  * being used by a signal handler.
436  *
437  * @param[in] a   Start of address range.
438  * @param[in] len Address range length.
439  * @param[in] tid Valgrind thread ID for whom the signal frame is being
440  *                constructed.
441  */
442 static void drd_start_using_mem_stack_signal(const Addr a, const SizeT len,
443                                              ThreadId tid)
444 {
445    DRD_(thread_set_vg_running_tid)(VG_(get_running_tid)());
446    drd_start_using_mem(a, len);
447 }
448
449 static void drd_stop_using_mem_stack_signal(Addr a, SizeT len)
450 {
451    drd_stop_using_mem(a, len, True);
452 }
453
454 static
455 void drd_pre_thread_create(const ThreadId creator, const ThreadId created)
456 {
457    const DrdThreadId drd_creator = DRD_(VgThreadIdToDrdThreadId)(creator);
458    tl_assert(created != VG_INVALID_THREADID);
459    DRD_(thread_pre_create)(drd_creator, created);
460    if (DRD_(IsValidDrdThreadId)(drd_creator))
461    {
462       DRD_(thread_new_segment)(drd_creator);
463    }
464    if (DRD_(thread_get_trace_fork_join)())
465    {
466       VG_(message)(Vg_DebugMsg,
467                    "drd_pre_thread_create creator = %d, created = %d\n",
468                    drd_creator, created);
469    }
470 }
471
472 /* Called by Valgrind's core before any loads or stores are performed on */
473 /* the context of thread "created". At startup, this function is called  */
474 /* with arguments (0,1).                                                 */
475 static
476 void drd_post_thread_create(const ThreadId vg_created)
477 {
478    DrdThreadId drd_created;
479
480    tl_assert(vg_created != VG_INVALID_THREADID);
481
482    drd_created = DRD_(thread_post_create)(vg_created);
483    if (DRD_(thread_get_trace_fork_join)())
484    {
485       VG_(message)(Vg_DebugMsg,
486                    "drd_post_thread_create created = %d\n",
487                    drd_created);
488    }
489    if (! DRD_(get_check_stack_accesses)())
490    {
491       DRD_(start_suppression)(DRD_(thread_get_stack_max)(drd_created)
492                               - DRD_(thread_get_stack_size)(drd_created),
493                               DRD_(thread_get_stack_max)(drd_created),
494                               "stack");
495    }
496 }
497
498 /* Called after a thread has performed its last memory access. */
499 static void drd_thread_finished(ThreadId vg_tid)
500 {
501    DrdThreadId drd_tid;
502
503    tl_assert(VG_(get_running_tid)() == vg_tid);
504
505    drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
506    if (DRD_(thread_get_trace_fork_join)())
507    {
508       VG_(message)(Vg_DebugMsg,
509                    "drd_thread_finished tid = %d%s\n",
510                    drd_tid,
511                    DRD_(thread_get_joinable)(drd_tid)
512                    ? ""
513                    : " (which is a detached thread)");
514    }
515    if (DRD_(s_show_stack_usage))
516    {
517       const SizeT stack_size = DRD_(thread_get_stack_size)(drd_tid);
518       const SizeT used_stack
519          = (DRD_(thread_get_stack_max)(drd_tid)
520             - DRD_(thread_get_stack_min_min)(drd_tid));
521       VG_(message)(Vg_UserMsg,
522                    "thread %d%s finished and used %ld bytes out of %ld"
523                    " on its stack. Margin: %ld bytes.\n",
524                    drd_tid,
525                    DRD_(thread_get_joinable)(drd_tid)
526                    ? ""
527                    : " (which is a detached thread)",
528                    used_stack,
529                    stack_size,
530                    stack_size - used_stack);
531
532    }
533    drd_stop_using_mem(DRD_(thread_get_stack_min)(drd_tid),
534                       DRD_(thread_get_stack_max)(drd_tid)
535                       - DRD_(thread_get_stack_min)(drd_tid),
536                       True);
537    DRD_(thread_set_record_loads)(drd_tid, False);
538    DRD_(thread_set_record_stores)(drd_tid, False);
539    DRD_(thread_finished)(drd_tid);
540 }
541
542 //
543 // Implementation of the tool interface.
544 //
545
546 static void DRD_(post_clo_init)(void)
547 {
548 #if defined(VGO_linux) || defined(VGO_darwin)
549    /* fine */
550 #else
551    VG_(printf)("\nWARNING: DRD has not yet been tested on this operating system.\n\n");
552 #  endif
553
554    if (DRD_(s_var_info))
555    {
556       VG_(needs_var_info)();
557    }
558 }
559
560 static void drd_start_client_code(const ThreadId tid, const ULong bbs_done)
561 {
562    tl_assert(tid == VG_(get_running_tid)());
563    DRD_(thread_set_vg_running_tid)(tid);
564 }
565
566 static void DRD_(fini)(Int exitcode)
567 {
568    // thread_print_all();
569    if (VG_(clo_verbosity) == 1 && !VG_(clo_xml)) {
570       VG_(message)(Vg_UserMsg,
571                    "For counts of detected and suppressed errors, "
572                    "rerun with: -v\n");
573    }
574
575    if (VG_(clo_stats) || DRD_(s_print_stats))
576    {
577       ULong pu = DRD_(thread_get_update_conflict_set_count)();
578       ULong pu_seg_cr = DRD_(thread_get_update_conflict_set_new_sg_count)();
579       ULong pu_mtx_cv = DRD_(thread_get_update_conflict_set_sync_count)();
580       ULong pu_join   = DRD_(thread_get_update_conflict_set_join_count)();
581
582       VG_(message)(Vg_UserMsg,
583                    "   thread: %lld context switches.\n",
584                    DRD_(thread_get_context_switch_count)());
585       VG_(message)(Vg_UserMsg,
586                    "confl set: %lld full updates and %lld partial updates;\n",
587                    DRD_(thread_get_compute_conflict_set_count)(),
588                    pu);
589       VG_(message)(Vg_UserMsg,
590                    "           %lld partial updates during segment creation,\n",
591                    pu_seg_cr);
592       VG_(message)(Vg_UserMsg,
593                    "           %lld because of mutex/sema/cond.var. operations,\n",
594                    pu_mtx_cv);
595       VG_(message)(Vg_UserMsg,
596                    "           %lld because of barrier/rwlock operations and\n",
597                    pu - pu_seg_cr - pu_mtx_cv - pu_join);
598       VG_(message)(Vg_UserMsg,
599                    "           %lld partial updates because of thread join"
600                    " operations.\n",
601                    pu_join);
602       VG_(message)(Vg_UserMsg,
603                    " segments: created %lld segments, max %lld alive,\n",
604                    DRD_(sg_get_segments_created_count)(),
605                    DRD_(sg_get_max_segments_alive_count)());
606       VG_(message)(Vg_UserMsg,
607                    "           %lld discard points and %lld merges.\n",
608                    DRD_(thread_get_discard_ordered_segments_count)(),
609                    DRD_(sg_get_segment_merge_count)());
610       VG_(message)(Vg_UserMsg,
611                    "segmnt cr: %lld mutex, %lld rwlock, %lld semaphore and"
612                    " %lld barrier.\n",
613                    DRD_(get_mutex_segment_creation_count)(),
614                    DRD_(get_rwlock_segment_creation_count)(),
615                    DRD_(get_semaphore_segment_creation_count)(),
616                    DRD_(get_barrier_segment_creation_count)());
617       VG_(message)(Vg_UserMsg,
618                    "  bitmaps: %lld level one"
619                    " and %lld level two bitmaps were allocated.\n",
620                    DRD_(bm_get_bitmap_creation_count)(),
621                    DRD_(bm_get_bitmap2_creation_count)());
622       VG_(message)(Vg_UserMsg,
623                    "    mutex: %lld non-recursive lock/unlock events.\n",
624                    DRD_(get_mutex_lock_count)());
625       DRD_(print_malloc_stats)();
626    }
627 }
628
629 static
630 void drd_pre_clo_init(void)
631 {
632    // Basic tool stuff.
633    VG_(details_name)            ("drd");
634    VG_(details_version)         (NULL);
635    VG_(details_description)     ("a thread error detector");
636    VG_(details_copyright_author)("Copyright (C) 2006-2010, and GNU GPL'd,"
637                                  " by Bart Van Assche.");
638    VG_(details_bug_reports_to)  (VG_BUGS_TO);
639
640    VG_(basic_tool_funcs)        (DRD_(post_clo_init),
641                                  DRD_(instrument),
642                                  DRD_(fini));
643
644    // Command line stuff.
645    VG_(needs_command_line_options)(DRD_(process_cmd_line_option),
646                                    DRD_(print_usage),
647                                    DRD_(print_debug_usage));
648
649    // Error handling.
650    DRD_(register_error_handlers)();
651
652    // Core event tracking.
653    VG_(track_pre_mem_read)         (drd_pre_mem_read);
654    VG_(track_pre_mem_read_asciiz)  (drd_pre_mem_read_asciiz);
655    VG_(track_post_mem_write)       (drd_post_mem_write);
656    VG_(track_new_mem_brk)          (drd_start_using_mem_w_tid);
657    VG_(track_new_mem_mmap)         (drd_start_using_mem_w_perms);
658    VG_(track_new_mem_stack)        (drd_start_using_mem_stack);
659    VG_(track_new_mem_stack_signal) (drd_start_using_mem_stack_signal);
660    VG_(track_new_mem_startup)      (drd_start_using_mem_w_perms);
661    VG_(track_die_mem_brk)          (drd_stop_using_nonstack_mem);
662    VG_(track_die_mem_munmap)       (drd_stop_using_nonstack_mem);
663    VG_(track_die_mem_stack)        (drd_stop_using_mem_stack);
664    VG_(track_die_mem_stack_signal) (drd_stop_using_mem_stack_signal);
665    VG_(track_start_client_code)    (drd_start_client_code);
666    VG_(track_pre_thread_ll_create) (drd_pre_thread_create);
667    VG_(track_pre_thread_first_insn)(drd_post_thread_create);
668    VG_(track_pre_thread_ll_exit)   (drd_thread_finished);
669
670    // Other stuff.
671    DRD_(register_malloc_wrappers)(drd_start_using_mem_w_ecu,
672                                   drd_stop_using_nonstack_mem);
673
674    DRD_(clientreq_init)();
675
676    DRD_(suppression_init)();
677
678    DRD_(clientobj_init)();
679
680    {
681       Char* const smi = VG_(getenv)("DRD_SEGMENT_MERGING_INTERVAL");
682       if (smi)
683          DRD_(thread_set_segment_merge_interval)(VG_(strtoll10)(smi, NULL));
684    }
685 }
686
687
688 VG_DETERMINE_INTERFACE_VERSION(drd_pre_clo_init)