blob: 266e819e0de95e27c5d65ae4c42886cd4e43f657 [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;
bart62a784c2009-02-15 13:11:14 +000059 ThreadId vg_created = DRD_(DrdThreadIdToVgThreadId)(created);
sewardjaf44c822007-11-25 14:01:38 +000060
61 tl_assert(sg);
bart62a784c2009-02-15 13:11:14 +000062 tl_assert(creator == DRD_INVALID_THREADID
63 || DRD_(IsValidDrdThreadId)(creator));
sewardjaf44c822007-11-25 14:01:38 +000064
65 creator_sg = (creator != DRD_INVALID_THREADID
bart62a784c2009-02-15 13:11:14 +000066 ? DRD_(thread_get_segment)(creator) : 0);
barta2b6e1b2008-03-17 18:32:39 +000067
sewardjaf44c822007-11-25 14:01:38 +000068 sg->next = 0;
69 sg->prev = 0;
barta2b6e1b2008-03-17 18:32:39 +000070 sg->refcnt = 1;
sewardjaf44c822007-11-25 14:01:38 +000071
tom9e86a122008-01-02 10:07:44 +000072 if (vg_created != VG_INVALID_THREADID && VG_(get_SP)(vg_created) != 0)
sewardje0744f02007-12-01 02:09:50 +000073 sg->stacktrace = VG_(record_ExeContext)(vg_created, 0);
74 else
75 sg->stacktrace = 0;
sewardjaf44c822007-11-25 14:01:38 +000076
77 if (creator_sg)
bart41b226c2009-02-14 16:55:19 +000078 DRD_(vc_copy)(&sg->vc, &creator_sg->vc);
sewardjaf44c822007-11-25 14:01:38 +000079 else
bart41b226c2009-02-14 16:55:19 +000080 DRD_(vc_init)(&sg->vc, 0, 0);
81 DRD_(vc_increment)(&sg->vc, created);
sewardjaf44c822007-11-25 14:01:38 +000082 sg->bm = bm_new();
83
bart62ada3f2009-02-14 17:19:58 +000084 if (DRD_(s_trace_segment))
sewardjaf44c822007-11-25 14:01:38 +000085 {
86 char msg[256];
87 VG_(snprintf)(msg, sizeof(msg),
bart85145022008-04-06 13:07:45 +000088 "New segment for thread %d/%d with vc ",
89 created != VG_INVALID_THREADID
bart62a784c2009-02-15 13:11:14 +000090 ? DRD_(DrdThreadIdToVgThreadId)(created)
barta2b6e1b2008-03-17 18:32:39 +000091 : DRD_INVALID_THREADID,
bart85145022008-04-06 13:07:45 +000092 created);
bart41b226c2009-02-14 16:55:19 +000093 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
sewardjaf44c822007-11-25 14:01:38 +000094 &sg->vc);
bart2c7c1d52008-02-29 19:17:28 +000095 VG_(message)(Vg_UserMsg, "%s", msg);
sewardjaf44c822007-11-25 14:01:38 +000096 }
97}
98
barta2b6e1b2008-03-17 18:32:39 +000099/** Deallocate the memory that was allocated by sg_init(). */
bart62ada3f2009-02-14 17:19:58 +0000100static void DRD_(sg_cleanup)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000101{
102 tl_assert(sg);
barta2b6e1b2008-03-17 18:32:39 +0000103 tl_assert(sg->refcnt == 0);
104
bart41b226c2009-02-14 16:55:19 +0000105 DRD_(vc_cleanup)(&sg->vc);
sewardjaf44c822007-11-25 14:01:38 +0000106 bm_delete(sg->bm);
107 sg->bm = 0;
108}
109
barta2b6e1b2008-03-17 18:32:39 +0000110/** Allocate and initialize a new segment. */
bart62ada3f2009-02-14 17:19:58 +0000111Segment* DRD_(sg_new)(ThreadId const creator, ThreadId const created)
sewardjaf44c822007-11-25 14:01:38 +0000112{
113 Segment* sg;
114
bart62ada3f2009-02-14 17:19:58 +0000115 DRD_(s_segments_created_count)++;
116 DRD_(s_segments_alive_count)++;
117 if (DRD_(s_max_segments_alive_count) < DRD_(s_segments_alive_count))
118 DRD_(s_max_segments_alive_count) = DRD_(s_segments_alive_count);
sewardjaf44c822007-11-25 14:01:38 +0000119
sewardj9c606bd2008-09-18 18:12:50 +0000120 sg = VG_(malloc)("drd.segment.sn.1", sizeof(*sg));
sewardjaf44c822007-11-25 14:01:38 +0000121 tl_assert(sg);
bart62ada3f2009-02-14 17:19:58 +0000122 DRD_(sg_init)(sg, creator, created);
sewardjaf44c822007-11-25 14:01:38 +0000123 return sg;
124}
125
bart62ada3f2009-02-14 17:19:58 +0000126static void DRD_(sg_delete)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000127{
bart68edad52008-02-24 18:21:12 +0000128#if 1
bart62ada3f2009-02-14 17:19:58 +0000129 if (DRD_(sg_get_trace)())
bart3772a982008-03-15 08:11:03 +0000130 {
131 char msg[256];
132 VG_(snprintf)(msg, sizeof(msg),
133 "Discarding the segment with vector clock ");
bart41b226c2009-02-14 16:55:19 +0000134 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000135 &sg->vc);
136 VG_(message)(Vg_UserMsg, "%s", msg);
137 }
bart68edad52008-02-24 18:21:12 +0000138#endif
139
bart62ada3f2009-02-14 17:19:58 +0000140 DRD_(s_segments_alive_count)--;
sewardjaf44c822007-11-25 14:01:38 +0000141
142 tl_assert(sg);
bart62ada3f2009-02-14 17:19:58 +0000143 DRD_(sg_cleanup)(sg);
sewardjaf44c822007-11-25 14:01:38 +0000144 VG_(free)(sg);
145}
146
barta2b6e1b2008-03-17 18:32:39 +0000147/** Query the reference count of the specified segment. */
bart62ada3f2009-02-14 17:19:58 +0000148int DRD_(sg_get_refcnt)(const Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000149{
150 tl_assert(sg);
151
152 return sg->refcnt;
153}
154
155/** Increment the reference count of the specified segment. */
bart62ada3f2009-02-14 17:19:58 +0000156Segment* DRD_(sg_get)(Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000157{
158 tl_assert(sg);
159
160 sg->refcnt++;
161 return sg;
162}
163
bart62ada3f2009-02-14 17:19:58 +0000164/**
165 * Decrement the reference count of the specified segment and deallocate the
166 * segment if the reference count became zero.
barta2b6e1b2008-03-17 18:32:39 +0000167 */
bart62ada3f2009-02-14 17:19:58 +0000168void DRD_(sg_put)(Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000169{
170 if (sg == 0)
171 return;
172
bart62ada3f2009-02-14 17:19:58 +0000173 if (DRD_(s_trace_segment))
barta2b6e1b2008-03-17 18:32:39 +0000174 {
175 char msg[256];
176 VG_(snprintf)(msg, sizeof(msg),
177 "Decrementing segment reference count %d -> %d with vc ",
178 sg->refcnt, sg->refcnt - 1);
bart41b226c2009-02-14 16:55:19 +0000179 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
barta2b6e1b2008-03-17 18:32:39 +0000180 &sg->vc);
181 VG_(message)(Vg_UserMsg, "%s", msg);
182 }
183
184 tl_assert(sg->refcnt >= 1);
185
186 if (--sg->refcnt == 0)
187 {
bart62ada3f2009-02-14 17:19:58 +0000188 DRD_(sg_delete)(sg);
barta2b6e1b2008-03-17 18:32:39 +0000189 }
190}
191
barta9c37392008-03-22 09:38:48 +0000192/** Merge sg1 and sg2 into sg1. */
bart62ada3f2009-02-14 17:19:58 +0000193void DRD_(sg_merge)(const Segment* const sg1, Segment* const sg2)
barta9c37392008-03-22 09:38:48 +0000194{
195 tl_assert(sg1);
barta71e5792008-03-22 17:09:04 +0000196 tl_assert(sg1->refcnt == 1);
barta9c37392008-03-22 09:38:48 +0000197 tl_assert(sg2);
barta71e5792008-03-22 17:09:04 +0000198 tl_assert(sg2->refcnt == 1);
barta9c37392008-03-22 09:38:48 +0000199
bart62ada3f2009-02-14 17:19:58 +0000200 if (DRD_(s_trace_segment))
barta9c37392008-03-22 09:38:48 +0000201 {
202 char msg[256];
203
204 VG_(snprintf)(msg, sizeof(msg), "Merging segments with vector clocks ");
bart41b226c2009-02-14 16:55:19 +0000205 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
barta9c37392008-03-22 09:38:48 +0000206 &sg1->vc);
207 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
208 " and ");
bart41b226c2009-02-14 16:55:19 +0000209 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
barta9c37392008-03-22 09:38:48 +0000210 &sg2->vc);
211 VG_(message)(Vg_UserMsg, "%s", msg);
212 }
213
214 // Keep sg1->stacktrace.
215 // Keep sg1->vc.
216 // Merge sg2->bm into sg1->bm.
217 bm_merge2(sg1->bm, sg2->bm);
218}
219
bart62ada3f2009-02-14 17:19:58 +0000220/** Print the vector clock and the bitmap of the specified segment. */
221void DRD_(sg_print)(const Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000222{
223 tl_assert(sg);
224 VG_(printf)("vc: ");
bart41b226c2009-02-14 16:55:19 +0000225 DRD_(vc_print)(&sg->vc);
sewardjaf44c822007-11-25 14:01:38 +0000226 VG_(printf)("\n");
227 bm_print(sg->bm);
228}
229
bart62ada3f2009-02-14 17:19:58 +0000230/** Query whether segment tracing has been enabled. */
231Bool DRD_(sg_get_trace)(void)
sewardjaf44c822007-11-25 14:01:38 +0000232{
bart62ada3f2009-02-14 17:19:58 +0000233 return DRD_(s_trace_segment);
sewardjaf44c822007-11-25 14:01:38 +0000234}
235
bart62ada3f2009-02-14 17:19:58 +0000236/** Enable or disable segment tracing. */
237void DRD_(sg_set_trace)(Bool const trace_segment)
sewardjaf44c822007-11-25 14:01:38 +0000238{
239 tl_assert(trace_segment == False || trace_segment == True);
bart62ada3f2009-02-14 17:19:58 +0000240 DRD_(s_trace_segment) = trace_segment;
sewardjaf44c822007-11-25 14:01:38 +0000241}
242
bart62ada3f2009-02-14 17:19:58 +0000243ULong DRD_(sg_get_segments_created_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000244{
bart62ada3f2009-02-14 17:19:58 +0000245 return DRD_(s_segments_created_count);
sewardjaf44c822007-11-25 14:01:38 +0000246}
247
bart62ada3f2009-02-14 17:19:58 +0000248ULong DRD_(sg_get_segments_alive_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000249{
bart62ada3f2009-02-14 17:19:58 +0000250 return DRD_(s_segments_alive_count);
bart7102f102008-03-17 17:37:53 +0000251}
252
bart62ada3f2009-02-14 17:19:58 +0000253ULong DRD_(sg_get_max_segments_alive_count)(void)
bart7102f102008-03-17 17:37:53 +0000254{
bart62ada3f2009-02-14 17:19:58 +0000255 return DRD_(s_max_segments_alive_count);
sewardjaf44c822007-11-25 14:01:38 +0000256}