blob: 40db038925a1143ef69aff31445364dac82d13f1 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
bart86562bd2009-02-16 19:43:56 +00002 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00003
bart922304f2011-03-13 12:02:44 +00004 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
sewardjaf44c822007-11-25 14:01:38 +00005
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.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307, USA.
20
21 The GNU General Public License is contained in the file COPYING.
22*/
23
24
sewardje0744f02007-12-01 02:09:50 +000025#include "drd_error.h"
26#include "drd_segment.h"
27#include "drd_thread.h"
sewardjaf44c822007-11-25 14:01:38 +000028#include "pub_tool_basics.h" // Addr, SizeT
sewardjaf44c822007-11-25 14:01:38 +000029#include "pub_tool_libcassert.h" // tl_assert()
30#include "pub_tool_libcbase.h" // VG_(strlen)()
31#include "pub_tool_libcprint.h" // VG_(printf)()
tomda3fdba2008-01-04 23:57:15 +000032#include "pub_tool_machine.h" // VG_(get_SP)()
sewardjaf44c822007-11-25 14:01:38 +000033#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardje0744f02007-12-01 02:09:50 +000034#include "pub_tool_threadstate.h" // VG_INVALID_THREADID
sewardjaf44c822007-11-25 14:01:38 +000035
36
barte278ab52012-01-24 18:28:55 +000037/* Global variables. */
38
39struct list_head DRD_(g_sg_list) = LIST_HEAD_INIT(DRD_(g_sg_list));
40
41
bart62ada3f2009-02-14 17:19:58 +000042/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000043
bart1a3b0b32009-05-03 17:07:34 +000044static ULong s_segment_merge_count;
bart2adfc2a2009-03-12 18:38:00 +000045static ULong s_segments_created_count;
46static ULong s_segments_alive_count;
47static ULong s_max_segments_alive_count;
48static Bool s_trace_segment;
sewardjaf44c822007-11-25 14:01:38 +000049
50
bart62ada3f2009-02-14 17:19:58 +000051/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000052
bart62ada3f2009-02-14 17:19:58 +000053/**
54 * Initialize the memory 'sg' points at.
bart2adfc2a2009-03-12 18:38:00 +000055 *
56 * @note The creator and created thread ID's may be equal.
57 * @note This function copies the vector clock of thread 'creator', a technique
58 * also known as clock snooping. This will only work reliably if the thread
59 * that called pthread_create() waits until the created thread has copied
60 * the vector clock.
sewardjaf44c822007-11-25 14:01:38 +000061 */
bart2adfc2a2009-03-12 18:38:00 +000062static void sg_init(Segment* const sg,
63 const DrdThreadId creator,
64 const DrdThreadId created)
sewardjaf44c822007-11-25 14:01:38 +000065{
bartbedfd232009-03-26 19:07:15 +000066 Segment* creator_sg;
67 ThreadId vg_created = DRD_(DrdThreadIdToVgThreadId)(created);
sewardjaf44c822007-11-25 14:01:38 +000068
bartbedfd232009-03-26 19:07:15 +000069 tl_assert(sg);
70 tl_assert(creator == DRD_INVALID_THREADID
71 || DRD_(IsValidDrdThreadId)(creator));
sewardjaf44c822007-11-25 14:01:38 +000072
bartbedfd232009-03-26 19:07:15 +000073 creator_sg = (creator != DRD_INVALID_THREADID
74 ? DRD_(thread_get_segment)(creator) : 0);
barta2b6e1b2008-03-17 18:32:39 +000075
barte278ab52012-01-24 18:28:55 +000076 sg->g_list.next = NULL;
77 sg->g_list.prev = NULL;
78 sg->thr_list.next = NULL;
79 sg->thr_list.prev = NULL;
bart8f822af2009-06-08 18:20:42 +000080 sg->tid = created;
bartbedfd232009-03-26 19:07:15 +000081 sg->refcnt = 1;
sewardjaf44c822007-11-25 14:01:38 +000082
bartbedfd232009-03-26 19:07:15 +000083 if (vg_created != VG_INVALID_THREADID && VG_(get_SP)(vg_created) != 0)
84 sg->stacktrace = VG_(record_ExeContext)(vg_created, 0);
85 else
86 sg->stacktrace = 0;
sewardjaf44c822007-11-25 14:01:38 +000087
bartbedfd232009-03-26 19:07:15 +000088 if (creator_sg)
89 DRD_(vc_copy)(&sg->vc, &creator_sg->vc);
90 else
91 DRD_(vc_init)(&sg->vc, 0, 0);
92 DRD_(vc_increment)(&sg->vc, created);
bart8f822af2009-06-08 18:20:42 +000093 DRD_(bm_init)(&sg->bm);
sewardjaf44c822007-11-25 14:01:38 +000094
bartbedfd232009-03-26 19:07:15 +000095 if (s_trace_segment)
96 {
bart8f822af2009-06-08 18:20:42 +000097 char* vc;
98
99 vc = DRD_(vc_aprint)(&sg->vc);
bart4565e272010-03-07 20:05:58 +0000100 VG_(message)(Vg_DebugMsg, "New segment for thread %d with vc %s\n",
bart8f822af2009-06-08 18:20:42 +0000101 created, vc);
102 VG_(free)(vc);
bartbedfd232009-03-26 19:07:15 +0000103 }
sewardjaf44c822007-11-25 14:01:38 +0000104}
105
barta2b6e1b2008-03-17 18:32:39 +0000106/** Deallocate the memory that was allocated by sg_init(). */
bart62ada3f2009-02-14 17:19:58 +0000107static void DRD_(sg_cleanup)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000108{
bartbedfd232009-03-26 19:07:15 +0000109 tl_assert(sg);
110 tl_assert(sg->refcnt == 0);
barta2b6e1b2008-03-17 18:32:39 +0000111
bartbedfd232009-03-26 19:07:15 +0000112 DRD_(vc_cleanup)(&sg->vc);
bart8f822af2009-06-08 18:20:42 +0000113 DRD_(bm_cleanup)(&sg->bm);
sewardjaf44c822007-11-25 14:01:38 +0000114}
115
barta2b6e1b2008-03-17 18:32:39 +0000116/** Allocate and initialize a new segment. */
bart2adfc2a2009-03-12 18:38:00 +0000117Segment* DRD_(sg_new)(const DrdThreadId creator, const DrdThreadId created)
sewardjaf44c822007-11-25 14:01:38 +0000118{
bartbedfd232009-03-26 19:07:15 +0000119 Segment* sg;
sewardjaf44c822007-11-25 14:01:38 +0000120
bartbedfd232009-03-26 19:07:15 +0000121 s_segments_created_count++;
122 s_segments_alive_count++;
123 if (s_max_segments_alive_count < s_segments_alive_count)
124 s_max_segments_alive_count = s_segments_alive_count;
sewardjaf44c822007-11-25 14:01:38 +0000125
bartbedfd232009-03-26 19:07:15 +0000126 sg = VG_(malloc)("drd.segment.sn.1", sizeof(*sg));
127 tl_assert(sg);
128 sg_init(sg, creator, created);
barte278ab52012-01-24 18:28:55 +0000129 list_add(&sg->g_list, &DRD_(g_sg_list));
bartbedfd232009-03-26 19:07:15 +0000130 return sg;
sewardjaf44c822007-11-25 14:01:38 +0000131}
132
bart62ada3f2009-02-14 17:19:58 +0000133static void DRD_(sg_delete)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000134{
bartbedfd232009-03-26 19:07:15 +0000135 if (DRD_(sg_get_trace)())
136 {
bart8f822af2009-06-08 18:20:42 +0000137 char* vc;
138
139 vc = DRD_(vc_aprint)(&sg->vc);
bart4565e272010-03-07 20:05:58 +0000140 VG_(message)(Vg_DebugMsg, "Discarding the segment with vector clock %s\n",
bart8f822af2009-06-08 18:20:42 +0000141 vc);
142 VG_(free)(vc);
bartbedfd232009-03-26 19:07:15 +0000143 }
bart68edad52008-02-24 18:21:12 +0000144
bartbedfd232009-03-26 19:07:15 +0000145 s_segments_alive_count--;
sewardjaf44c822007-11-25 14:01:38 +0000146
bartbedfd232009-03-26 19:07:15 +0000147 tl_assert(sg);
barte278ab52012-01-24 18:28:55 +0000148 list_del(&sg->g_list);
bartbedfd232009-03-26 19:07:15 +0000149 DRD_(sg_cleanup)(sg);
150 VG_(free)(sg);
sewardjaf44c822007-11-25 14:01:38 +0000151}
152
barta2b6e1b2008-03-17 18:32:39 +0000153/** Increment the reference count of the specified segment. */
bart62ada3f2009-02-14 17:19:58 +0000154Segment* DRD_(sg_get)(Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000155{
bartbedfd232009-03-26 19:07:15 +0000156 tl_assert(sg);
barta2b6e1b2008-03-17 18:32:39 +0000157
bartbedfd232009-03-26 19:07:15 +0000158 sg->refcnt++;
159 return sg;
barta2b6e1b2008-03-17 18:32:39 +0000160}
161
bart62ada3f2009-02-14 17:19:58 +0000162/**
163 * Decrement the reference count of the specified segment and deallocate the
164 * segment if the reference count became zero.
barta2b6e1b2008-03-17 18:32:39 +0000165 */
bart62ada3f2009-02-14 17:19:58 +0000166void DRD_(sg_put)(Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000167{
bartbedfd232009-03-26 19:07:15 +0000168 if (sg == 0)
169 return;
barta2b6e1b2008-03-17 18:32:39 +0000170
bartbedfd232009-03-26 19:07:15 +0000171 if (s_trace_segment)
172 {
bart8f822af2009-06-08 18:20:42 +0000173 char* vc;
174
175 vc = DRD_(vc_aprint)(&sg->vc);
176 VG_(message)(Vg_DebugMsg,
bart4565e272010-03-07 20:05:58 +0000177 "Decrementing segment reference count %d -> %d with vc %s\n",
bart8f822af2009-06-08 18:20:42 +0000178 sg->refcnt, sg->refcnt - 1, vc);
179 VG_(free)(vc);
bartbedfd232009-03-26 19:07:15 +0000180 }
barta2b6e1b2008-03-17 18:32:39 +0000181
bartbedfd232009-03-26 19:07:15 +0000182 tl_assert(sg->refcnt >= 1);
barta2b6e1b2008-03-17 18:32:39 +0000183
bartbedfd232009-03-26 19:07:15 +0000184 if (--sg->refcnt == 0)
185 {
186 DRD_(sg_delete)(sg);
187 }
barta2b6e1b2008-03-17 18:32:39 +0000188}
189
barta9c37392008-03-22 09:38:48 +0000190/** Merge sg1 and sg2 into sg1. */
bart8f822af2009-06-08 18:20:42 +0000191void DRD_(sg_merge)(Segment* const sg1, Segment* const sg2)
barta9c37392008-03-22 09:38:48 +0000192{
bartbedfd232009-03-26 19:07:15 +0000193 tl_assert(sg1);
194 tl_assert(sg1->refcnt == 1);
195 tl_assert(sg2);
196 tl_assert(sg2->refcnt == 1);
barta9c37392008-03-22 09:38:48 +0000197
bartbedfd232009-03-26 19:07:15 +0000198 if (s_trace_segment)
199 {
bart8f822af2009-06-08 18:20:42 +0000200 char *vc1, *vc2;
barta9c37392008-03-22 09:38:48 +0000201
bart8f822af2009-06-08 18:20:42 +0000202 vc1 = DRD_(vc_aprint)(&sg1->vc);
203 vc2 = DRD_(vc_aprint)(&sg2->vc);
204
bart4565e272010-03-07 20:05:58 +0000205 VG_(message)(Vg_DebugMsg,
206 "Merging segments with vector clocks %s and %s\n", vc1, vc2);
bart8f822af2009-06-08 18:20:42 +0000207 VG_(free)(vc1);
208 VG_(free)(vc2);
bartbedfd232009-03-26 19:07:15 +0000209 }
barta9c37392008-03-22 09:38:48 +0000210
bart1a3b0b32009-05-03 17:07:34 +0000211 s_segment_merge_count++;
212
bartbedfd232009-03-26 19:07:15 +0000213 // Keep sg1->stacktrace.
214 // Keep sg1->vc.
215 // Merge sg2->bm into sg1->bm.
bart8f822af2009-06-08 18:20:42 +0000216 DRD_(bm_merge2)(&sg1->bm, &sg2->bm);
barta9c37392008-03-22 09:38:48 +0000217}
218
bart62ada3f2009-02-14 17:19:58 +0000219/** Print the vector clock and the bitmap of the specified segment. */
bart8f822af2009-06-08 18:20:42 +0000220void DRD_(sg_print)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000221{
bartbedfd232009-03-26 19:07:15 +0000222 tl_assert(sg);
223 VG_(printf)("vc: ");
224 DRD_(vc_print)(&sg->vc);
225 VG_(printf)("\n");
bart8f822af2009-06-08 18:20:42 +0000226 DRD_(bm_print)(&sg->bm);
sewardjaf44c822007-11-25 14:01:38 +0000227}
228
bart62ada3f2009-02-14 17:19:58 +0000229/** Query whether segment tracing has been enabled. */
230Bool DRD_(sg_get_trace)(void)
sewardjaf44c822007-11-25 14:01:38 +0000231{
bartbedfd232009-03-26 19:07:15 +0000232 return s_trace_segment;
sewardjaf44c822007-11-25 14:01:38 +0000233}
234
bart62ada3f2009-02-14 17:19:58 +0000235/** Enable or disable segment tracing. */
236void DRD_(sg_set_trace)(Bool const trace_segment)
sewardjaf44c822007-11-25 14:01:38 +0000237{
bartbedfd232009-03-26 19:07:15 +0000238 tl_assert(trace_segment == False || trace_segment == True);
239 s_trace_segment = trace_segment;
sewardjaf44c822007-11-25 14:01:38 +0000240}
241
bart62ada3f2009-02-14 17:19:58 +0000242ULong DRD_(sg_get_segments_created_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000243{
bartbedfd232009-03-26 19:07:15 +0000244 return s_segments_created_count;
sewardjaf44c822007-11-25 14:01:38 +0000245}
246
bart62ada3f2009-02-14 17:19:58 +0000247ULong DRD_(sg_get_segments_alive_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000248{
bartbedfd232009-03-26 19:07:15 +0000249 return s_segments_alive_count;
bart7102f102008-03-17 17:37:53 +0000250}
251
bart62ada3f2009-02-14 17:19:58 +0000252ULong DRD_(sg_get_max_segments_alive_count)(void)
bart7102f102008-03-17 17:37:53 +0000253{
bartbedfd232009-03-26 19:07:15 +0000254 return s_max_segments_alive_count;
sewardjaf44c822007-11-25 14:01:38 +0000255}
bart1a3b0b32009-05-03 17:07:34 +0000256
257ULong DRD_(sg_get_segment_merge_count)(void)
258{
259 return s_segment_merge_count;
260}