blob: 32c5e2839833e41dd3e18679bfaad51af14f4ec0 [file] [log] [blame]
bartbedfd232009-03-26 19:07:15 +00001/* -*- mode: C; c-basic-offset: 3; -*- */
sewardjaf44c822007-11-25 14:01:38 +00002/*
bart86562bd2009-02-16 19:43:56 +00003 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00004
bart86562bd2009-02-16 19:43:56 +00005 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
sewardjaf44c822007-11-25 14:01:38 +00006
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
bart1a3b0b32009-05-03 17:07:34 +000041static ULong s_segment_merge_count;
bart2adfc2a2009-03-12 18:38:00 +000042static ULong s_segments_created_count;
43static ULong s_segments_alive_count;
44static ULong s_max_segments_alive_count;
45static Bool s_trace_segment;
sewardjaf44c822007-11-25 14:01:38 +000046
47
bart62ada3f2009-02-14 17:19:58 +000048/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000049
bart62ada3f2009-02-14 17:19:58 +000050/**
51 * Initialize the memory 'sg' points at.
bart2adfc2a2009-03-12 18:38:00 +000052 *
53 * @note The creator and created thread ID's may be equal.
54 * @note This function copies the vector clock of thread 'creator', a technique
55 * also known as clock snooping. This will only work reliably if the thread
56 * that called pthread_create() waits until the created thread has copied
57 * the vector clock.
sewardjaf44c822007-11-25 14:01:38 +000058 */
bart2adfc2a2009-03-12 18:38:00 +000059static void sg_init(Segment* const sg,
60 const DrdThreadId creator,
61 const DrdThreadId created)
sewardjaf44c822007-11-25 14:01:38 +000062{
bartbedfd232009-03-26 19:07:15 +000063 Segment* creator_sg;
64 ThreadId vg_created = DRD_(DrdThreadIdToVgThreadId)(created);
sewardjaf44c822007-11-25 14:01:38 +000065
bartbedfd232009-03-26 19:07:15 +000066 tl_assert(sg);
67 tl_assert(creator == DRD_INVALID_THREADID
68 || DRD_(IsValidDrdThreadId)(creator));
sewardjaf44c822007-11-25 14:01:38 +000069
bartbedfd232009-03-26 19:07:15 +000070 creator_sg = (creator != DRD_INVALID_THREADID
71 ? DRD_(thread_get_segment)(creator) : 0);
barta2b6e1b2008-03-17 18:32:39 +000072
bartbedfd232009-03-26 19:07:15 +000073 sg->next = 0;
74 sg->prev = 0;
75 sg->refcnt = 1;
sewardjaf44c822007-11-25 14:01:38 +000076
bartbedfd232009-03-26 19:07:15 +000077 if (vg_created != VG_INVALID_THREADID && VG_(get_SP)(vg_created) != 0)
78 sg->stacktrace = VG_(record_ExeContext)(vg_created, 0);
79 else
80 sg->stacktrace = 0;
sewardjaf44c822007-11-25 14:01:38 +000081
bartbedfd232009-03-26 19:07:15 +000082 if (creator_sg)
83 DRD_(vc_copy)(&sg->vc, &creator_sg->vc);
84 else
85 DRD_(vc_init)(&sg->vc, 0, 0);
86 DRD_(vc_increment)(&sg->vc, created);
87 sg->bm = DRD_(bm_new)();
sewardjaf44c822007-11-25 14:01:38 +000088
bartbedfd232009-03-26 19:07:15 +000089 if (s_trace_segment)
90 {
91 char msg[256];
92 VG_(snprintf)(msg, sizeof(msg),
93 "New segment for thread %d/%d with vc ",
94 created != VG_INVALID_THREADID
95 ? DRD_(DrdThreadIdToVgThreadId)(created)
96 : DRD_INVALID_THREADID,
97 created);
98 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
99 &sg->vc);
100 VG_(message)(Vg_UserMsg, "%s", msg);
101 }
sewardjaf44c822007-11-25 14:01:38 +0000102}
103
barta2b6e1b2008-03-17 18:32:39 +0000104/** Deallocate the memory that was allocated by sg_init(). */
bart62ada3f2009-02-14 17:19:58 +0000105static void DRD_(sg_cleanup)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000106{
bartbedfd232009-03-26 19:07:15 +0000107 tl_assert(sg);
108 tl_assert(sg->refcnt == 0);
barta2b6e1b2008-03-17 18:32:39 +0000109
bartbedfd232009-03-26 19:07:15 +0000110 DRD_(vc_cleanup)(&sg->vc);
111 DRD_(bm_delete)(sg->bm);
112 sg->bm = 0;
sewardjaf44c822007-11-25 14:01:38 +0000113}
114
barta2b6e1b2008-03-17 18:32:39 +0000115/** Allocate and initialize a new segment. */
bart2adfc2a2009-03-12 18:38:00 +0000116Segment* DRD_(sg_new)(const DrdThreadId creator, const DrdThreadId created)
sewardjaf44c822007-11-25 14:01:38 +0000117{
bartbedfd232009-03-26 19:07:15 +0000118 Segment* sg;
sewardjaf44c822007-11-25 14:01:38 +0000119
bartbedfd232009-03-26 19:07:15 +0000120 s_segments_created_count++;
121 s_segments_alive_count++;
122 if (s_max_segments_alive_count < s_segments_alive_count)
123 s_max_segments_alive_count = s_segments_alive_count;
sewardjaf44c822007-11-25 14:01:38 +0000124
bartbedfd232009-03-26 19:07:15 +0000125 sg = VG_(malloc)("drd.segment.sn.1", sizeof(*sg));
126 tl_assert(sg);
127 sg_init(sg, creator, created);
128 return sg;
sewardjaf44c822007-11-25 14:01:38 +0000129}
130
bart62ada3f2009-02-14 17:19:58 +0000131static void DRD_(sg_delete)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000132{
bart68edad52008-02-24 18:21:12 +0000133#if 1
bartbedfd232009-03-26 19:07:15 +0000134 if (DRD_(sg_get_trace)())
135 {
136 char msg[256];
137 VG_(snprintf)(msg, sizeof(msg),
138 "Discarding the segment with vector clock ");
139 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
140 &sg->vc);
141 VG_(message)(Vg_UserMsg, "%s", msg);
142 }
bart68edad52008-02-24 18:21:12 +0000143#endif
144
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);
148 DRD_(sg_cleanup)(sg);
149 VG_(free)(sg);
sewardjaf44c822007-11-25 14:01:38 +0000150}
151
barta2b6e1b2008-03-17 18:32:39 +0000152/** Query the reference count of the specified segment. */
bart62ada3f2009-02-14 17:19:58 +0000153int DRD_(sg_get_refcnt)(const Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000154{
bartbedfd232009-03-26 19:07:15 +0000155 tl_assert(sg);
barta2b6e1b2008-03-17 18:32:39 +0000156
bartbedfd232009-03-26 19:07:15 +0000157 return sg->refcnt;
barta2b6e1b2008-03-17 18:32:39 +0000158}
159
160/** Increment the reference count of the specified segment. */
bart62ada3f2009-02-14 17:19:58 +0000161Segment* DRD_(sg_get)(Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000162{
bartbedfd232009-03-26 19:07:15 +0000163 tl_assert(sg);
barta2b6e1b2008-03-17 18:32:39 +0000164
bartbedfd232009-03-26 19:07:15 +0000165 sg->refcnt++;
166 return sg;
barta2b6e1b2008-03-17 18:32:39 +0000167}
168
bart62ada3f2009-02-14 17:19:58 +0000169/**
170 * Decrement the reference count of the specified segment and deallocate the
171 * segment if the reference count became zero.
barta2b6e1b2008-03-17 18:32:39 +0000172 */
bart62ada3f2009-02-14 17:19:58 +0000173void DRD_(sg_put)(Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000174{
bartbedfd232009-03-26 19:07:15 +0000175 if (sg == 0)
176 return;
barta2b6e1b2008-03-17 18:32:39 +0000177
bartbedfd232009-03-26 19:07:15 +0000178 if (s_trace_segment)
179 {
180 char msg[256];
181 VG_(snprintf)(msg, sizeof(msg),
182 "Decrementing segment reference count %d -> %d with vc ",
183 sg->refcnt, sg->refcnt - 1);
184 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
185 &sg->vc);
186 VG_(message)(Vg_UserMsg, "%s", msg);
187 }
barta2b6e1b2008-03-17 18:32:39 +0000188
bartbedfd232009-03-26 19:07:15 +0000189 tl_assert(sg->refcnt >= 1);
barta2b6e1b2008-03-17 18:32:39 +0000190
bartbedfd232009-03-26 19:07:15 +0000191 if (--sg->refcnt == 0)
192 {
193 DRD_(sg_delete)(sg);
194 }
barta2b6e1b2008-03-17 18:32:39 +0000195}
196
barta9c37392008-03-22 09:38:48 +0000197/** Merge sg1 and sg2 into sg1. */
bart62ada3f2009-02-14 17:19:58 +0000198void DRD_(sg_merge)(const Segment* const sg1, Segment* const sg2)
barta9c37392008-03-22 09:38:48 +0000199{
bartbedfd232009-03-26 19:07:15 +0000200 tl_assert(sg1);
201 tl_assert(sg1->refcnt == 1);
202 tl_assert(sg2);
203 tl_assert(sg2->refcnt == 1);
barta9c37392008-03-22 09:38:48 +0000204
bartbedfd232009-03-26 19:07:15 +0000205 if (s_trace_segment)
206 {
barta9c37392008-03-22 09:38:48 +0000207 char msg[256];
208
209 VG_(snprintf)(msg, sizeof(msg), "Merging segments with vector clocks ");
bart41b226c2009-02-14 16:55:19 +0000210 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
bartbedfd232009-03-26 19:07:15 +0000211 &sg1->vc);
barta9c37392008-03-22 09:38:48 +0000212 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
213 " and ");
bart41b226c2009-02-14 16:55:19 +0000214 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
bartbedfd232009-03-26 19:07:15 +0000215 &sg2->vc);
barta9c37392008-03-22 09:38:48 +0000216 VG_(message)(Vg_UserMsg, "%s", msg);
bartbedfd232009-03-26 19:07:15 +0000217 }
barta9c37392008-03-22 09:38:48 +0000218
bart1a3b0b32009-05-03 17:07:34 +0000219 s_segment_merge_count++;
220
bartbedfd232009-03-26 19:07:15 +0000221 // Keep sg1->stacktrace.
222 // Keep sg1->vc.
223 // Merge sg2->bm into sg1->bm.
224 DRD_(bm_merge2)(sg1->bm, sg2->bm);
barta9c37392008-03-22 09:38:48 +0000225}
226
bart62ada3f2009-02-14 17:19:58 +0000227/** Print the vector clock and the bitmap of the specified segment. */
228void DRD_(sg_print)(const Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000229{
bartbedfd232009-03-26 19:07:15 +0000230 tl_assert(sg);
231 VG_(printf)("vc: ");
232 DRD_(vc_print)(&sg->vc);
233 VG_(printf)("\n");
234 DRD_(bm_print)(sg->bm);
sewardjaf44c822007-11-25 14:01:38 +0000235}
236
bart62ada3f2009-02-14 17:19:58 +0000237/** Query whether segment tracing has been enabled. */
238Bool DRD_(sg_get_trace)(void)
sewardjaf44c822007-11-25 14:01:38 +0000239{
bartbedfd232009-03-26 19:07:15 +0000240 return s_trace_segment;
sewardjaf44c822007-11-25 14:01:38 +0000241}
242
bart62ada3f2009-02-14 17:19:58 +0000243/** Enable or disable segment tracing. */
244void DRD_(sg_set_trace)(Bool const trace_segment)
sewardjaf44c822007-11-25 14:01:38 +0000245{
bartbedfd232009-03-26 19:07:15 +0000246 tl_assert(trace_segment == False || trace_segment == True);
247 s_trace_segment = trace_segment;
sewardjaf44c822007-11-25 14:01:38 +0000248}
249
bart62ada3f2009-02-14 17:19:58 +0000250ULong DRD_(sg_get_segments_created_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000251{
bartbedfd232009-03-26 19:07:15 +0000252 return s_segments_created_count;
sewardjaf44c822007-11-25 14:01:38 +0000253}
254
bart62ada3f2009-02-14 17:19:58 +0000255ULong DRD_(sg_get_segments_alive_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000256{
bartbedfd232009-03-26 19:07:15 +0000257 return s_segments_alive_count;
bart7102f102008-03-17 17:37:53 +0000258}
259
bart62ada3f2009-02-14 17:19:58 +0000260ULong DRD_(sg_get_max_segments_alive_count)(void)
bart7102f102008-03-17 17:37:53 +0000261{
bartbedfd232009-03-26 19:07:15 +0000262 return s_max_segments_alive_count;
sewardjaf44c822007-11-25 14:01:38 +0000263}
bart1a3b0b32009-05-03 17:07:34 +0000264
265ULong DRD_(sg_get_segment_merge_count)(void)
266{
267 return s_segment_merge_count;
268}