blob: 06972e4fd3cbf12f1458e81065365a6aa2dc1d80 [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
bart2adfc2a2009-03-12 18:38:00 +000040static ULong s_segments_created_count;
41static ULong s_segments_alive_count;
42static ULong s_max_segments_alive_count;
43static Bool s_trace_segment;
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.
bart2adfc2a2009-03-12 18:38:00 +000050 *
51 * @note The creator and created thread ID's may be equal.
52 * @note This function copies the vector clock of thread 'creator', a technique
53 * also known as clock snooping. This will only work reliably if the thread
54 * that called pthread_create() waits until the created thread has copied
55 * the vector clock.
sewardjaf44c822007-11-25 14:01:38 +000056 */
bart2adfc2a2009-03-12 18:38:00 +000057static void sg_init(Segment* const sg,
58 const DrdThreadId creator,
59 const DrdThreadId created)
sewardjaf44c822007-11-25 14:01:38 +000060{
61 Segment* creator_sg;
bart62a784c2009-02-15 13:11:14 +000062 ThreadId vg_created = DRD_(DrdThreadIdToVgThreadId)(created);
sewardjaf44c822007-11-25 14:01:38 +000063
64 tl_assert(sg);
bart62a784c2009-02-15 13:11:14 +000065 tl_assert(creator == DRD_INVALID_THREADID
66 || DRD_(IsValidDrdThreadId)(creator));
sewardjaf44c822007-11-25 14:01:38 +000067
68 creator_sg = (creator != DRD_INVALID_THREADID
bart62a784c2009-02-15 13:11:14 +000069 ? DRD_(thread_get_segment)(creator) : 0);
barta2b6e1b2008-03-17 18:32:39 +000070
sewardjaf44c822007-11-25 14:01:38 +000071 sg->next = 0;
72 sg->prev = 0;
barta2b6e1b2008-03-17 18:32:39 +000073 sg->refcnt = 1;
sewardjaf44c822007-11-25 14:01:38 +000074
tom9e86a122008-01-02 10:07:44 +000075 if (vg_created != VG_INVALID_THREADID && VG_(get_SP)(vg_created) != 0)
sewardje0744f02007-12-01 02:09:50 +000076 sg->stacktrace = VG_(record_ExeContext)(vg_created, 0);
77 else
78 sg->stacktrace = 0;
sewardjaf44c822007-11-25 14:01:38 +000079
80 if (creator_sg)
bart41b226c2009-02-14 16:55:19 +000081 DRD_(vc_copy)(&sg->vc, &creator_sg->vc);
sewardjaf44c822007-11-25 14:01:38 +000082 else
bart41b226c2009-02-14 16:55:19 +000083 DRD_(vc_init)(&sg->vc, 0, 0);
84 DRD_(vc_increment)(&sg->vc, created);
bart99edb292009-02-15 15:59:20 +000085 sg->bm = DRD_(bm_new)();
sewardjaf44c822007-11-25 14:01:38 +000086
bart2adfc2a2009-03-12 18:38:00 +000087 if (s_trace_segment)
sewardjaf44c822007-11-25 14:01:38 +000088 {
89 char msg[256];
90 VG_(snprintf)(msg, sizeof(msg),
bart85145022008-04-06 13:07:45 +000091 "New segment for thread %d/%d with vc ",
92 created != VG_INVALID_THREADID
bart62a784c2009-02-15 13:11:14 +000093 ? DRD_(DrdThreadIdToVgThreadId)(created)
barta2b6e1b2008-03-17 18:32:39 +000094 : DRD_INVALID_THREADID,
bart85145022008-04-06 13:07:45 +000095 created);
bart41b226c2009-02-14 16:55:19 +000096 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
sewardjaf44c822007-11-25 14:01:38 +000097 &sg->vc);
bart2c7c1d52008-02-29 19:17:28 +000098 VG_(message)(Vg_UserMsg, "%s", msg);
sewardjaf44c822007-11-25 14:01:38 +000099 }
100}
101
barta2b6e1b2008-03-17 18:32:39 +0000102/** Deallocate the memory that was allocated by sg_init(). */
bart62ada3f2009-02-14 17:19:58 +0000103static void DRD_(sg_cleanup)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000104{
105 tl_assert(sg);
barta2b6e1b2008-03-17 18:32:39 +0000106 tl_assert(sg->refcnt == 0);
107
bart41b226c2009-02-14 16:55:19 +0000108 DRD_(vc_cleanup)(&sg->vc);
bart99edb292009-02-15 15:59:20 +0000109 DRD_(bm_delete)(sg->bm);
sewardjaf44c822007-11-25 14:01:38 +0000110 sg->bm = 0;
111}
112
barta2b6e1b2008-03-17 18:32:39 +0000113/** Allocate and initialize a new segment. */
bart2adfc2a2009-03-12 18:38:00 +0000114Segment* DRD_(sg_new)(const DrdThreadId creator, const DrdThreadId created)
sewardjaf44c822007-11-25 14:01:38 +0000115{
116 Segment* sg;
117
bart2adfc2a2009-03-12 18:38:00 +0000118 s_segments_created_count++;
119 s_segments_alive_count++;
120 if (s_max_segments_alive_count < s_segments_alive_count)
121 s_max_segments_alive_count = s_segments_alive_count;
sewardjaf44c822007-11-25 14:01:38 +0000122
sewardj9c606bd2008-09-18 18:12:50 +0000123 sg = VG_(malloc)("drd.segment.sn.1", sizeof(*sg));
sewardjaf44c822007-11-25 14:01:38 +0000124 tl_assert(sg);
bart2adfc2a2009-03-12 18:38:00 +0000125 sg_init(sg, creator, created);
sewardjaf44c822007-11-25 14:01:38 +0000126 return sg;
127}
128
bart62ada3f2009-02-14 17:19:58 +0000129static void DRD_(sg_delete)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000130{
bart68edad52008-02-24 18:21:12 +0000131#if 1
bart62ada3f2009-02-14 17:19:58 +0000132 if (DRD_(sg_get_trace)())
bart3772a982008-03-15 08:11:03 +0000133 {
134 char msg[256];
135 VG_(snprintf)(msg, sizeof(msg),
136 "Discarding the segment with vector clock ");
bart41b226c2009-02-14 16:55:19 +0000137 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000138 &sg->vc);
139 VG_(message)(Vg_UserMsg, "%s", msg);
140 }
bart68edad52008-02-24 18:21:12 +0000141#endif
142
bart2adfc2a2009-03-12 18:38:00 +0000143 s_segments_alive_count--;
sewardjaf44c822007-11-25 14:01:38 +0000144
145 tl_assert(sg);
bart62ada3f2009-02-14 17:19:58 +0000146 DRD_(sg_cleanup)(sg);
sewardjaf44c822007-11-25 14:01:38 +0000147 VG_(free)(sg);
148}
149
barta2b6e1b2008-03-17 18:32:39 +0000150/** Query the reference count of the specified segment. */
bart62ada3f2009-02-14 17:19:58 +0000151int DRD_(sg_get_refcnt)(const Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000152{
153 tl_assert(sg);
154
155 return sg->refcnt;
156}
157
158/** Increment the reference count of the specified segment. */
bart62ada3f2009-02-14 17:19:58 +0000159Segment* DRD_(sg_get)(Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000160{
161 tl_assert(sg);
162
163 sg->refcnt++;
164 return sg;
165}
166
bart62ada3f2009-02-14 17:19:58 +0000167/**
168 * Decrement the reference count of the specified segment and deallocate the
169 * segment if the reference count became zero.
barta2b6e1b2008-03-17 18:32:39 +0000170 */
bart62ada3f2009-02-14 17:19:58 +0000171void DRD_(sg_put)(Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000172{
173 if (sg == 0)
174 return;
175
bart2adfc2a2009-03-12 18:38:00 +0000176 if (s_trace_segment)
barta2b6e1b2008-03-17 18:32:39 +0000177 {
178 char msg[256];
179 VG_(snprintf)(msg, sizeof(msg),
180 "Decrementing segment reference count %d -> %d with vc ",
181 sg->refcnt, sg->refcnt - 1);
bart41b226c2009-02-14 16:55:19 +0000182 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
barta2b6e1b2008-03-17 18:32:39 +0000183 &sg->vc);
184 VG_(message)(Vg_UserMsg, "%s", msg);
185 }
186
187 tl_assert(sg->refcnt >= 1);
188
189 if (--sg->refcnt == 0)
190 {
bart62ada3f2009-02-14 17:19:58 +0000191 DRD_(sg_delete)(sg);
barta2b6e1b2008-03-17 18:32:39 +0000192 }
193}
194
barta9c37392008-03-22 09:38:48 +0000195/** Merge sg1 and sg2 into sg1. */
bart62ada3f2009-02-14 17:19:58 +0000196void DRD_(sg_merge)(const Segment* const sg1, Segment* const sg2)
barta9c37392008-03-22 09:38:48 +0000197{
198 tl_assert(sg1);
barta71e5792008-03-22 17:09:04 +0000199 tl_assert(sg1->refcnt == 1);
barta9c37392008-03-22 09:38:48 +0000200 tl_assert(sg2);
barta71e5792008-03-22 17:09:04 +0000201 tl_assert(sg2->refcnt == 1);
barta9c37392008-03-22 09:38:48 +0000202
bart2adfc2a2009-03-12 18:38:00 +0000203 if (s_trace_segment)
barta9c37392008-03-22 09:38:48 +0000204 {
205 char msg[256];
206
207 VG_(snprintf)(msg, sizeof(msg), "Merging segments with vector clocks ");
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 &sg1->vc);
210 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
211 " and ");
bart41b226c2009-02-14 16:55:19 +0000212 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
barta9c37392008-03-22 09:38:48 +0000213 &sg2->vc);
214 VG_(message)(Vg_UserMsg, "%s", msg);
215 }
216
217 // Keep sg1->stacktrace.
218 // Keep sg1->vc.
219 // Merge sg2->bm into sg1->bm.
bart99edb292009-02-15 15:59:20 +0000220 DRD_(bm_merge2)(sg1->bm, sg2->bm);
barta9c37392008-03-22 09:38:48 +0000221}
222
bart62ada3f2009-02-14 17:19:58 +0000223/** Print the vector clock and the bitmap of the specified segment. */
224void DRD_(sg_print)(const Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000225{
226 tl_assert(sg);
227 VG_(printf)("vc: ");
bart41b226c2009-02-14 16:55:19 +0000228 DRD_(vc_print)(&sg->vc);
sewardjaf44c822007-11-25 14:01:38 +0000229 VG_(printf)("\n");
bart99edb292009-02-15 15:59:20 +0000230 DRD_(bm_print)(sg->bm);
sewardjaf44c822007-11-25 14:01:38 +0000231}
232
bart62ada3f2009-02-14 17:19:58 +0000233/** Query whether segment tracing has been enabled. */
234Bool DRD_(sg_get_trace)(void)
sewardjaf44c822007-11-25 14:01:38 +0000235{
bart2adfc2a2009-03-12 18:38:00 +0000236 return s_trace_segment;
sewardjaf44c822007-11-25 14:01:38 +0000237}
238
bart62ada3f2009-02-14 17:19:58 +0000239/** Enable or disable segment tracing. */
240void DRD_(sg_set_trace)(Bool const trace_segment)
sewardjaf44c822007-11-25 14:01:38 +0000241{
242 tl_assert(trace_segment == False || trace_segment == True);
bart2adfc2a2009-03-12 18:38:00 +0000243 s_trace_segment = trace_segment;
sewardjaf44c822007-11-25 14:01:38 +0000244}
245
bart62ada3f2009-02-14 17:19:58 +0000246ULong DRD_(sg_get_segments_created_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000247{
bart2adfc2a2009-03-12 18:38:00 +0000248 return s_segments_created_count;
sewardjaf44c822007-11-25 14:01:38 +0000249}
250
bart62ada3f2009-02-14 17:19:58 +0000251ULong DRD_(sg_get_segments_alive_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000252{
bart2adfc2a2009-03-12 18:38:00 +0000253 return s_segments_alive_count;
bart7102f102008-03-17 17:37:53 +0000254}
255
bart62ada3f2009-02-14 17:19:58 +0000256ULong DRD_(sg_get_max_segments_alive_count)(void)
bart7102f102008-03-17 17:37:53 +0000257{
bart2adfc2a2009-03-12 18:38:00 +0000258 return s_max_segments_alive_count;
sewardjaf44c822007-11-25 14:01:38 +0000259}