blob: 0c7cf034e7fbdc46cb324f82608bd131cbc91b44 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 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
sewardje0744f02007-12-01 02:09:50 +000026#include "drd_error.h"
27#include "drd_segment.h"
28#include "drd_thread.h"
sewardjaf44c822007-11-25 14:01:38 +000029#include "pub_tool_basics.h" // Addr, SizeT
30#include "pub_tool_errormgr.h" // VG_(unique_error)()
31#include "pub_tool_libcassert.h" // tl_assert()
32#include "pub_tool_libcbase.h" // VG_(strlen)()
33#include "pub_tool_libcprint.h" // VG_(printf)()
tomda3fdba2008-01-04 23:57:15 +000034#include "pub_tool_machine.h" // VG_(get_SP)()
sewardjaf44c822007-11-25 14:01:38 +000035#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardje0744f02007-12-01 02:09:50 +000036#include "pub_tool_threadstate.h" // VG_INVALID_THREADID
sewardjaf44c822007-11-25 14:01:38 +000037
38
bart62ada3f2009-02-14 17:19:58 +000039/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000040
bart62ada3f2009-02-14 17:19:58 +000041static ULong DRD_(s_segments_created_count);
42static ULong DRD_(s_segments_alive_count);
43static ULong DRD_(s_max_segments_alive_count);
44static Bool DRD_(s_trace_segment) = False;
sewardjaf44c822007-11-25 14:01:38 +000045
46
bart62ada3f2009-02-14 17:19:58 +000047/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000048
bart62ada3f2009-02-14 17:19:58 +000049/**
50 * Initialize the memory 'sg' points at.
barta2b6e1b2008-03-17 18:32:39 +000051 * @note The creator and created thread ID's may be equal.
sewardjaf44c822007-11-25 14:01:38 +000052 */
bart7102f102008-03-17 17:37:53 +000053static
bart62ada3f2009-02-14 17:19:58 +000054void DRD_(sg_init)(Segment* const sg,
55 DrdThreadId const creator,
56 DrdThreadId const created)
sewardjaf44c822007-11-25 14:01:38 +000057{
58 Segment* creator_sg;
sewardje0744f02007-12-01 02:09:50 +000059 ThreadId vg_created = DrdThreadIdToVgThreadId(created);
sewardjaf44c822007-11-25 14:01:38 +000060
61 tl_assert(sg);
62 tl_assert(creator == DRD_INVALID_THREADID || IsValidDrdThreadId(creator));
63
64 creator_sg = (creator != DRD_INVALID_THREADID
65 ? thread_get_segment(creator) : 0);
barta2b6e1b2008-03-17 18:32:39 +000066
sewardjaf44c822007-11-25 14:01:38 +000067 sg->next = 0;
68 sg->prev = 0;
barta2b6e1b2008-03-17 18:32:39 +000069 sg->refcnt = 1;
sewardjaf44c822007-11-25 14:01:38 +000070
tom9e86a122008-01-02 10:07:44 +000071 if (vg_created != VG_INVALID_THREADID && VG_(get_SP)(vg_created) != 0)
sewardje0744f02007-12-01 02:09:50 +000072 sg->stacktrace = VG_(record_ExeContext)(vg_created, 0);
73 else
74 sg->stacktrace = 0;
sewardjaf44c822007-11-25 14:01:38 +000075
76 if (creator_sg)
bart41b226c2009-02-14 16:55:19 +000077 DRD_(vc_copy)(&sg->vc, &creator_sg->vc);
sewardjaf44c822007-11-25 14:01:38 +000078 else
bart41b226c2009-02-14 16:55:19 +000079 DRD_(vc_init)(&sg->vc, 0, 0);
80 DRD_(vc_increment)(&sg->vc, created);
sewardjaf44c822007-11-25 14:01:38 +000081 sg->bm = bm_new();
82
bart62ada3f2009-02-14 17:19:58 +000083 if (DRD_(s_trace_segment))
sewardjaf44c822007-11-25 14:01:38 +000084 {
85 char msg[256];
86 VG_(snprintf)(msg, sizeof(msg),
bart85145022008-04-06 13:07:45 +000087 "New segment for thread %d/%d with vc ",
88 created != VG_INVALID_THREADID
89 ? DrdThreadIdToVgThreadId(created)
barta2b6e1b2008-03-17 18:32:39 +000090 : DRD_INVALID_THREADID,
bart85145022008-04-06 13:07:45 +000091 created);
bart41b226c2009-02-14 16:55:19 +000092 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
sewardjaf44c822007-11-25 14:01:38 +000093 &sg->vc);
bart2c7c1d52008-02-29 19:17:28 +000094 VG_(message)(Vg_UserMsg, "%s", msg);
sewardjaf44c822007-11-25 14:01:38 +000095 }
96}
97
barta2b6e1b2008-03-17 18:32:39 +000098/** Deallocate the memory that was allocated by sg_init(). */
bart62ada3f2009-02-14 17:19:58 +000099static void DRD_(sg_cleanup)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000100{
101 tl_assert(sg);
barta2b6e1b2008-03-17 18:32:39 +0000102 tl_assert(sg->refcnt == 0);
103
bart41b226c2009-02-14 16:55:19 +0000104 DRD_(vc_cleanup)(&sg->vc);
sewardjaf44c822007-11-25 14:01:38 +0000105 bm_delete(sg->bm);
106 sg->bm = 0;
107}
108
barta2b6e1b2008-03-17 18:32:39 +0000109/** Allocate and initialize a new segment. */
bart62ada3f2009-02-14 17:19:58 +0000110Segment* DRD_(sg_new)(ThreadId const creator, ThreadId const created)
sewardjaf44c822007-11-25 14:01:38 +0000111{
112 Segment* sg;
113
bart62ada3f2009-02-14 17:19:58 +0000114 DRD_(s_segments_created_count)++;
115 DRD_(s_segments_alive_count)++;
116 if (DRD_(s_max_segments_alive_count) < DRD_(s_segments_alive_count))
117 DRD_(s_max_segments_alive_count) = DRD_(s_segments_alive_count);
sewardjaf44c822007-11-25 14:01:38 +0000118
sewardj9c606bd2008-09-18 18:12:50 +0000119 sg = VG_(malloc)("drd.segment.sn.1", sizeof(*sg));
sewardjaf44c822007-11-25 14:01:38 +0000120 tl_assert(sg);
bart62ada3f2009-02-14 17:19:58 +0000121 DRD_(sg_init)(sg, creator, created);
sewardjaf44c822007-11-25 14:01:38 +0000122 return sg;
123}
124
bart62ada3f2009-02-14 17:19:58 +0000125static void DRD_(sg_delete)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000126{
bart68edad52008-02-24 18:21:12 +0000127#if 1
bart62ada3f2009-02-14 17:19:58 +0000128 if (DRD_(sg_get_trace)())
bart3772a982008-03-15 08:11:03 +0000129 {
130 char msg[256];
131 VG_(snprintf)(msg, sizeof(msg),
132 "Discarding the segment with vector clock ");
bart41b226c2009-02-14 16:55:19 +0000133 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000134 &sg->vc);
135 VG_(message)(Vg_UserMsg, "%s", msg);
136 }
bart68edad52008-02-24 18:21:12 +0000137#endif
138
bart62ada3f2009-02-14 17:19:58 +0000139 DRD_(s_segments_alive_count)--;
sewardjaf44c822007-11-25 14:01:38 +0000140
141 tl_assert(sg);
bart62ada3f2009-02-14 17:19:58 +0000142 DRD_(sg_cleanup)(sg);
sewardjaf44c822007-11-25 14:01:38 +0000143 VG_(free)(sg);
144}
145
barta2b6e1b2008-03-17 18:32:39 +0000146/** Query the reference count of the specified segment. */
bart62ada3f2009-02-14 17:19:58 +0000147int DRD_(sg_get_refcnt)(const Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000148{
149 tl_assert(sg);
150
151 return sg->refcnt;
152}
153
154/** Increment the reference count of the specified segment. */
bart62ada3f2009-02-14 17:19:58 +0000155Segment* DRD_(sg_get)(Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000156{
157 tl_assert(sg);
158
159 sg->refcnt++;
160 return sg;
161}
162
bart62ada3f2009-02-14 17:19:58 +0000163/**
164 * Decrement the reference count of the specified segment and deallocate the
165 * segment if the reference count became zero.
barta2b6e1b2008-03-17 18:32:39 +0000166 */
bart62ada3f2009-02-14 17:19:58 +0000167void DRD_(sg_put)(Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000168{
169 if (sg == 0)
170 return;
171
bart62ada3f2009-02-14 17:19:58 +0000172 if (DRD_(s_trace_segment))
barta2b6e1b2008-03-17 18:32:39 +0000173 {
174 char msg[256];
175 VG_(snprintf)(msg, sizeof(msg),
176 "Decrementing segment reference count %d -> %d with vc ",
177 sg->refcnt, sg->refcnt - 1);
bart41b226c2009-02-14 16:55:19 +0000178 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
barta2b6e1b2008-03-17 18:32:39 +0000179 &sg->vc);
180 VG_(message)(Vg_UserMsg, "%s", msg);
181 }
182
183 tl_assert(sg->refcnt >= 1);
184
185 if (--sg->refcnt == 0)
186 {
bart62ada3f2009-02-14 17:19:58 +0000187 DRD_(sg_delete)(sg);
barta2b6e1b2008-03-17 18:32:39 +0000188 }
189}
190
barta9c37392008-03-22 09:38:48 +0000191/** Merge sg1 and sg2 into sg1. */
bart62ada3f2009-02-14 17:19:58 +0000192void DRD_(sg_merge)(const Segment* const sg1, Segment* const sg2)
barta9c37392008-03-22 09:38:48 +0000193{
194 tl_assert(sg1);
barta71e5792008-03-22 17:09:04 +0000195 tl_assert(sg1->refcnt == 1);
barta9c37392008-03-22 09:38:48 +0000196 tl_assert(sg2);
barta71e5792008-03-22 17:09:04 +0000197 tl_assert(sg2->refcnt == 1);
barta9c37392008-03-22 09:38:48 +0000198
bart62ada3f2009-02-14 17:19:58 +0000199 if (DRD_(s_trace_segment))
barta9c37392008-03-22 09:38:48 +0000200 {
201 char msg[256];
202
203 VG_(snprintf)(msg, sizeof(msg), "Merging segments with vector clocks ");
bart41b226c2009-02-14 16:55:19 +0000204 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
barta9c37392008-03-22 09:38:48 +0000205 &sg1->vc);
206 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
207 " and ");
bart41b226c2009-02-14 16:55:19 +0000208 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
barta9c37392008-03-22 09:38:48 +0000209 &sg2->vc);
210 VG_(message)(Vg_UserMsg, "%s", msg);
211 }
212
213 // Keep sg1->stacktrace.
214 // Keep sg1->vc.
215 // Merge sg2->bm into sg1->bm.
216 bm_merge2(sg1->bm, sg2->bm);
217}
218
bart62ada3f2009-02-14 17:19:58 +0000219/** Print the vector clock and the bitmap of the specified segment. */
220void DRD_(sg_print)(const Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000221{
222 tl_assert(sg);
223 VG_(printf)("vc: ");
bart41b226c2009-02-14 16:55:19 +0000224 DRD_(vc_print)(&sg->vc);
sewardjaf44c822007-11-25 14:01:38 +0000225 VG_(printf)("\n");
226 bm_print(sg->bm);
227}
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{
bart62ada3f2009-02-14 17:19:58 +0000232 return DRD_(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{
238 tl_assert(trace_segment == False || trace_segment == True);
bart62ada3f2009-02-14 17:19:58 +0000239 DRD_(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{
bart62ada3f2009-02-14 17:19:58 +0000244 return DRD_(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{
bart62ada3f2009-02-14 17:19:58 +0000249 return DRD_(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{
bart62ada3f2009-02-14 17:19:58 +0000254 return DRD_(s_max_segments_alive_count);
sewardjaf44c822007-11-25 14:01:38 +0000255}