2 This file is part of drd, a thread error detector.
4 Copyright (C) 2006-2020 Bart Van Assche <bvanassche@acm.org>.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
19 The GNU General Public License is contained in the file COPYING.
23 #include "drd_error.h"
24 #include "drd_segment.h"
25 #include "drd_thread.h"
26 #include "pub_tool_basics.h" // Addr, SizeT
27 #include "pub_tool_libcassert.h" // tl_assert()
28 #include "pub_tool_libcbase.h" // VG_(strlen)()
29 #include "pub_tool_libcprint.h" // VG_(printf)()
30 #include "pub_tool_machine.h" // VG_(get_SP)()
31 #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
32 #include "pub_tool_threadstate.h" // VG_INVALID_THREADID
35 /* Global variables. */
37 Segment
* DRD_(g_sg_list
);
40 /* Local variables. */
42 static ULong s_segment_merge_count
;
43 static ULong s_segments_created_count
;
44 static ULong s_segments_alive_count
;
45 static ULong s_max_segments_alive_count
;
46 static Bool s_trace_segment
;
49 /* Function definitions. */
52 * Initialize the memory 'sg' points at.
54 * @note The creator and created thread ID's may be equal.
55 * @note This function copies the vector clock of thread 'creator', a technique
56 * also known as clock snooping. This will only work reliably if the thread
57 * that called pthread_create() waits until the created thread has copied
60 static void sg_init(Segment
* const sg
,
61 const DrdThreadId creator
,
62 const DrdThreadId created
)
65 ThreadId vg_created
= DRD_(DrdThreadIdToVgThreadId
)(created
);
68 tl_assert(creator
== DRD_INVALID_THREADID
69 || DRD_(IsValidDrdThreadId
)(creator
));
71 creator_sg
= (creator
!= DRD_INVALID_THREADID
72 ? DRD_(thread_get_segment
)(creator
) : 0);
81 if (vg_created
!= VG_INVALID_THREADID
&& VG_(get_SP
)(vg_created
) != 0)
82 sg
->stacktrace
= VG_(record_ExeContext
)(vg_created
, 0);
87 DRD_(vc_copy
)(&sg
->vc
, &creator_sg
->vc
);
89 DRD_(vc_init
)(&sg
->vc
, 0, 0);
90 DRD_(vc_increment
)(&sg
->vc
, created
);
91 DRD_(bm_init
)(&sg
->bm
);
97 vc
= DRD_(vc_aprint
)(&sg
->vc
);
98 VG_(message
)(Vg_DebugMsg
, "New segment for thread %u with vc %s\n",
104 /** Deallocate the memory that was allocated by sg_init(). */
105 static void DRD_(sg_cleanup
)(Segment
* const sg
)
108 tl_assert(sg
->refcnt
== 0);
110 DRD_(vc_cleanup
)(&sg
->vc
);
111 DRD_(bm_cleanup
)(&sg
->bm
);
114 /** Allocate and initialize a new segment. */
115 Segment
* DRD_(sg_new
)(const DrdThreadId creator
, const DrdThreadId created
)
119 s_segments_created_count
++;
120 s_segments_alive_count
++;
121 if (s_max_segments_alive_count
< s_segments_alive_count
)
122 s_max_segments_alive_count
= s_segments_alive_count
;
124 sg
= VG_(malloc
)("drd.segment.sn.1", sizeof(*sg
));
125 sg_init(sg
, creator
, created
);
126 if (DRD_(g_sg_list
)) {
127 DRD_(g_sg_list
)->g_prev
= sg
;
128 sg
->g_next
= DRD_(g_sg_list
);
130 DRD_(g_sg_list
) = sg
;
134 static void DRD_(sg_delete
)(Segment
* const sg
)
136 if (DRD_(sg_get_trace
)())
140 vc
= DRD_(vc_aprint
)(&sg
->vc
);
141 VG_(message
)(Vg_DebugMsg
, "Discarding the segment with vector clock %s\n",
146 s_segments_alive_count
--;
150 sg
->g_next
->g_prev
= sg
->g_prev
;
152 sg
->g_prev
->g_next
= sg
->g_next
;
154 DRD_(g_sg_list
) = sg
->g_next
;
155 DRD_(sg_cleanup
)(sg
);
159 /** Increment the reference count of the specified segment. */
160 Segment
* DRD_(sg_get
)(Segment
* const sg
)
169 * Decrement the reference count of the specified segment and deallocate the
170 * segment if the reference count became zero.
172 void DRD_(sg_put
)(Segment
* const sg
)
181 vc
= DRD_(vc_aprint
)(&sg
->vc
);
182 VG_(message
)(Vg_DebugMsg
,
183 "Decrementing segment reference count %d -> %d with vc %s\n",
184 sg
->refcnt
, sg
->refcnt
- 1, vc
);
188 tl_assert(sg
->refcnt
>= 1);
190 if (--sg
->refcnt
== 0)
196 /** Merge sg1 and sg2 into sg1. */
197 void DRD_(sg_merge
)(Segment
* const sg1
, Segment
* const sg2
)
200 tl_assert(sg1
->refcnt
== 1);
202 tl_assert(sg2
->refcnt
== 1);
208 vc1
= DRD_(vc_aprint
)(&sg1
->vc
);
209 vc2
= DRD_(vc_aprint
)(&sg2
->vc
);
211 VG_(message
)(Vg_DebugMsg
,
212 "Merging segments with vector clocks %s and %s\n", vc1
, vc2
);
217 s_segment_merge_count
++;
219 // Keep sg1->stacktrace.
221 // Merge sg2->bm into sg1->bm.
222 DRD_(bm_merge2
)(&sg1
->bm
, &sg2
->bm
);
225 /** Print the vector clock and the bitmap of the specified segment. */
226 void DRD_(sg_print
)(Segment
* const sg
)
230 DRD_(vc_print
)(&sg
->vc
);
232 DRD_(bm_print
)(&sg
->bm
);
235 /** Query whether segment tracing has been enabled. */
236 Bool
DRD_(sg_get_trace
)(void)
238 return s_trace_segment
;
241 /** Enable or disable segment tracing. */
242 void DRD_(sg_set_trace
)(Bool
const trace_segment
)
244 tl_assert(trace_segment
== False
|| trace_segment
== True
);
245 s_trace_segment
= trace_segment
;
248 ULong
DRD_(sg_get_segments_created_count
)(void)
250 return s_segments_created_count
;
253 ULong
DRD_(sg_get_segments_alive_count
)(void)
255 return s_segments_alive_count
;
258 ULong
DRD_(sg_get_max_segments_alive_count
)(void)
260 return s_max_segments_alive_count
;
263 ULong
DRD_(sg_get_segment_merge_count
)(void)
265 return s_segment_merge_count
;