blob: 6f166d1149300e0e7dce7e9e605d9602f395a88e [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
bart86562bd2009-02-16 19:43:56 +00004 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
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
29#include "pub_tool_errormgr.h" // VG_(unique_error)()
30#include "pub_tool_libcassert.h" // tl_assert()
31#include "pub_tool_libcbase.h" // VG_(strlen)()
32#include "pub_tool_libcprint.h" // VG_(printf)()
tomda3fdba2008-01-04 23:57:15 +000033#include "pub_tool_machine.h" // VG_(get_SP)()
sewardjaf44c822007-11-25 14:01:38 +000034#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardje0744f02007-12-01 02:09:50 +000035#include "pub_tool_threadstate.h" // VG_INVALID_THREADID
sewardjaf44c822007-11-25 14:01:38 +000036
37
bart62ada3f2009-02-14 17:19:58 +000038/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000039
bart62ada3f2009-02-14 17:19:58 +000040static ULong DRD_(s_segments_created_count);
41static ULong DRD_(s_segments_alive_count);
42static ULong DRD_(s_max_segments_alive_count);
43static Bool DRD_(s_trace_segment) = False;
sewardjaf44c822007-11-25 14:01:38 +000044
45
bart62ada3f2009-02-14 17:19:58 +000046/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000047
bart62ada3f2009-02-14 17:19:58 +000048/**
49 * Initialize the memory 'sg' points at.
barta2b6e1b2008-03-17 18:32:39 +000050 * @note The creator and created thread ID's may be equal.
sewardjaf44c822007-11-25 14:01:38 +000051 */
bart7102f102008-03-17 17:37:53 +000052static
bart62ada3f2009-02-14 17:19:58 +000053void DRD_(sg_init)(Segment* const sg,
54 DrdThreadId const creator,
55 DrdThreadId const created)
sewardjaf44c822007-11-25 14:01:38 +000056{
57 Segment* creator_sg;
bart62a784c2009-02-15 13:11:14 +000058 ThreadId vg_created = DRD_(DrdThreadIdToVgThreadId)(created);
sewardjaf44c822007-11-25 14:01:38 +000059
60 tl_assert(sg);
bart62a784c2009-02-15 13:11:14 +000061 tl_assert(creator == DRD_INVALID_THREADID
62 || DRD_(IsValidDrdThreadId)(creator));
sewardjaf44c822007-11-25 14:01:38 +000063
64 creator_sg = (creator != DRD_INVALID_THREADID
bart62a784c2009-02-15 13:11:14 +000065 ? DRD_(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);
bart99edb292009-02-15 15:59:20 +000081 sg->bm = DRD_(bm_new)();
sewardjaf44c822007-11-25 14:01:38 +000082
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
bart62a784c2009-02-15 13:11:14 +000089 ? DRD_(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);
bart99edb292009-02-15 15:59:20 +0000105 DRD_(bm_delete)(sg->bm);
sewardjaf44c822007-11-25 14:01:38 +0000106 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.
bart99edb292009-02-15 15:59:20 +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. */
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");
bart99edb292009-02-15 15:59:20 +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{
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}