1 /* -*- mode: C; c-basic-offset: 3; indent-tabs-mode: nil; -*- */
3 This file is part of drd, a thread error detector.
5 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
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.
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.
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
22 The GNU General Public License is contained in the file COPYING.
26 #include "drd_error.h"
27 #include "drd_segment.h"
28 #include "drd_thread.h"
29 #include "pub_tool_basics.h" // Addr, SizeT
30 #include "pub_tool_libcassert.h" // tl_assert()
31 #include "pub_tool_libcbase.h" // VG_(strlen)()
32 #include "pub_tool_libcprint.h" // VG_(printf)()
33 #include "pub_tool_machine.h" // VG_(get_SP)()
34 #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
35 #include "pub_tool_threadstate.h" // VG_INVALID_THREADID
38 /* Local variables. */
40 static ULong s_segment_merge_count;
41 static ULong s_segments_created_count;
42 static ULong s_segments_alive_count;
43 static ULong s_max_segments_alive_count;
44 static Bool s_trace_segment;
47 /* Function definitions. */
50 * Initialize the memory 'sg' points at.
52 * @note The creator and created thread ID's may be equal.
53 * @note This function copies the vector clock of thread 'creator', a technique
54 * also known as clock snooping. This will only work reliably if the thread
55 * that called pthread_create() waits until the created thread has copied
58 static void sg_init(Segment* const sg,
59 const DrdThreadId creator,
60 const DrdThreadId created)
63 ThreadId vg_created = DRD_(DrdThreadIdToVgThreadId)(created);
66 tl_assert(creator == DRD_INVALID_THREADID
67 || DRD_(IsValidDrdThreadId)(creator));
69 creator_sg = (creator != DRD_INVALID_THREADID
70 ? DRD_(thread_get_segment)(creator) : 0);
77 if (vg_created != VG_INVALID_THREADID && VG_(get_SP)(vg_created) != 0)
78 sg->stacktrace = VG_(record_ExeContext)(vg_created, 0);
83 DRD_(vc_copy)(&sg->vc, &creator_sg->vc);
85 DRD_(vc_init)(&sg->vc, 0, 0);
86 DRD_(vc_increment)(&sg->vc, created);
87 DRD_(bm_init)(&sg->bm);
93 vc = DRD_(vc_aprint)(&sg->vc);
94 VG_(message)(Vg_DebugMsg, "New segment for thread %d with vc %s\n",
100 /** Deallocate the memory that was allocated by sg_init(). */
101 static void DRD_(sg_cleanup)(Segment* const sg)
104 tl_assert(sg->refcnt == 0);
106 DRD_(vc_cleanup)(&sg->vc);
107 DRD_(bm_cleanup)(&sg->bm);
110 /** Allocate and initialize a new segment. */
111 Segment* DRD_(sg_new)(const DrdThreadId creator, const DrdThreadId created)
115 s_segments_created_count++;
116 s_segments_alive_count++;
117 if (s_max_segments_alive_count < s_segments_alive_count)
118 s_max_segments_alive_count = s_segments_alive_count;
120 sg = VG_(malloc)("drd.segment.sn.1", sizeof(*sg));
122 sg_init(sg, creator, created);
126 static void DRD_(sg_delete)(Segment* const sg)
128 if (DRD_(sg_get_trace)())
132 vc = DRD_(vc_aprint)(&sg->vc);
133 VG_(message)(Vg_DebugMsg, "Discarding the segment with vector clock %s\n",
138 s_segments_alive_count--;
141 DRD_(sg_cleanup)(sg);
145 /** Increment the reference count of the specified segment. */
146 Segment* DRD_(sg_get)(Segment* const sg)
155 * Decrement the reference count of the specified segment and deallocate the
156 * segment if the reference count became zero.
158 void DRD_(sg_put)(Segment* const sg)
167 vc = DRD_(vc_aprint)(&sg->vc);
168 VG_(message)(Vg_DebugMsg,
169 "Decrementing segment reference count %d -> %d with vc %s\n",
170 sg->refcnt, sg->refcnt - 1, vc);
174 tl_assert(sg->refcnt >= 1);
176 if (--sg->refcnt == 0)
182 /** Merge sg1 and sg2 into sg1. */
183 void DRD_(sg_merge)(Segment* const sg1, Segment* const sg2)
186 tl_assert(sg1->refcnt == 1);
188 tl_assert(sg2->refcnt == 1);
194 vc1 = DRD_(vc_aprint)(&sg1->vc);
195 vc2 = DRD_(vc_aprint)(&sg2->vc);
197 VG_(message)(Vg_DebugMsg,
198 "Merging segments with vector clocks %s and %s\n", vc1, vc2);
203 s_segment_merge_count++;
205 // Keep sg1->stacktrace.
207 // Merge sg2->bm into sg1->bm.
208 DRD_(bm_merge2)(&sg1->bm, &sg2->bm);
211 /** Print the vector clock and the bitmap of the specified segment. */
212 void DRD_(sg_print)(Segment* const sg)
216 DRD_(vc_print)(&sg->vc);
218 DRD_(bm_print)(&sg->bm);
221 /** Query whether segment tracing has been enabled. */
222 Bool DRD_(sg_get_trace)(void)
224 return s_trace_segment;
227 /** Enable or disable segment tracing. */
228 void DRD_(sg_set_trace)(Bool const trace_segment)
230 tl_assert(trace_segment == False || trace_segment == True);
231 s_trace_segment = trace_segment;
234 ULong DRD_(sg_get_segments_created_count)(void)
236 return s_segments_created_count;
239 ULong DRD_(sg_get_segments_alive_count)(void)
241 return s_segments_alive_count;
244 ULong DRD_(sg_get_max_segments_alive_count)(void)
246 return s_max_segments_alive_count;
249 ULong DRD_(sg_get_segment_merge_count)(void)
251 return s_segment_merge_count;