blob: d513efae35e80d0d35acf9731f2285ab0dadb2ce [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
bart2adfc2a2009-03-12 18:38:00 +000041static ULong s_segments_created_count;
42static ULong s_segments_alive_count;
43static ULong s_max_segments_alive_count;
44static Bool s_trace_segment;
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.
bart2adfc2a2009-03-12 18:38:00 +000051 *
52 * @note The creator and created thread ID's may be equal.
53 * @note This function copies the vector clock of thread 'creator', a technique
54 * also known as clock snooping. This will only work reliably if the thread
55 * that called pthread_create() waits until the created thread has copied
56 * the vector clock.
sewardjaf44c822007-11-25 14:01:38 +000057 */
bart2adfc2a2009-03-12 18:38:00 +000058static void sg_init(Segment* const sg,
59 const DrdThreadId creator,
60 const DrdThreadId created)
sewardjaf44c822007-11-25 14:01:38 +000061{
bartbedfd232009-03-26 19:07:15 +000062 Segment* creator_sg;
63 ThreadId vg_created = DRD_(DrdThreadIdToVgThreadId)(created);
sewardjaf44c822007-11-25 14:01:38 +000064
bartbedfd232009-03-26 19:07:15 +000065 tl_assert(sg);
66 tl_assert(creator == DRD_INVALID_THREADID
67 || DRD_(IsValidDrdThreadId)(creator));
sewardjaf44c822007-11-25 14:01:38 +000068
bartbedfd232009-03-26 19:07:15 +000069 creator_sg = (creator != DRD_INVALID_THREADID
70 ? DRD_(thread_get_segment)(creator) : 0);
barta2b6e1b2008-03-17 18:32:39 +000071
bartbedfd232009-03-26 19:07:15 +000072 sg->next = 0;
73 sg->prev = 0;
74 sg->refcnt = 1;
sewardjaf44c822007-11-25 14:01:38 +000075
bartbedfd232009-03-26 19:07:15 +000076 if (vg_created != VG_INVALID_THREADID && VG_(get_SP)(vg_created) != 0)
77 sg->stacktrace = VG_(record_ExeContext)(vg_created, 0);
78 else
79 sg->stacktrace = 0;
sewardjaf44c822007-11-25 14:01:38 +000080
bartbedfd232009-03-26 19:07:15 +000081 if (creator_sg)
82 DRD_(vc_copy)(&sg->vc, &creator_sg->vc);
83 else
84 DRD_(vc_init)(&sg->vc, 0, 0);
85 DRD_(vc_increment)(&sg->vc, created);
86 sg->bm = DRD_(bm_new)();
sewardjaf44c822007-11-25 14:01:38 +000087
bartbedfd232009-03-26 19:07:15 +000088 if (s_trace_segment)
89 {
90 char msg[256];
91 VG_(snprintf)(msg, sizeof(msg),
92 "New segment for thread %d/%d with vc ",
93 created != VG_INVALID_THREADID
94 ? DRD_(DrdThreadIdToVgThreadId)(created)
95 : DRD_INVALID_THREADID,
96 created);
97 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
98 &sg->vc);
99 VG_(message)(Vg_UserMsg, "%s", msg);
100 }
sewardjaf44c822007-11-25 14:01:38 +0000101}
102
barta2b6e1b2008-03-17 18:32:39 +0000103/** Deallocate the memory that was allocated by sg_init(). */
bart62ada3f2009-02-14 17:19:58 +0000104static void DRD_(sg_cleanup)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000105{
bartbedfd232009-03-26 19:07:15 +0000106 tl_assert(sg);
107 tl_assert(sg->refcnt == 0);
barta2b6e1b2008-03-17 18:32:39 +0000108
bartbedfd232009-03-26 19:07:15 +0000109 DRD_(vc_cleanup)(&sg->vc);
110 DRD_(bm_delete)(sg->bm);
111 sg->bm = 0;
sewardjaf44c822007-11-25 14:01:38 +0000112}
113
barta2b6e1b2008-03-17 18:32:39 +0000114/** Allocate and initialize a new segment. */
bart2adfc2a2009-03-12 18:38:00 +0000115Segment* DRD_(sg_new)(const DrdThreadId creator, const DrdThreadId created)
sewardjaf44c822007-11-25 14:01:38 +0000116{
bartbedfd232009-03-26 19:07:15 +0000117 Segment* sg;
sewardjaf44c822007-11-25 14:01:38 +0000118
bartbedfd232009-03-26 19:07:15 +0000119 s_segments_created_count++;
120 s_segments_alive_count++;
121 if (s_max_segments_alive_count < s_segments_alive_count)
122 s_max_segments_alive_count = s_segments_alive_count;
sewardjaf44c822007-11-25 14:01:38 +0000123
bartbedfd232009-03-26 19:07:15 +0000124 sg = VG_(malloc)("drd.segment.sn.1", sizeof(*sg));
125 tl_assert(sg);
126 sg_init(sg, creator, created);
127 return sg;
sewardjaf44c822007-11-25 14:01:38 +0000128}
129
bart62ada3f2009-02-14 17:19:58 +0000130static void DRD_(sg_delete)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000131{
bart68edad52008-02-24 18:21:12 +0000132#if 1
bartbedfd232009-03-26 19:07:15 +0000133 if (DRD_(sg_get_trace)())
134 {
135 char msg[256];
136 VG_(snprintf)(msg, sizeof(msg),
137 "Discarding the segment with vector clock ");
138 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
139 &sg->vc);
140 VG_(message)(Vg_UserMsg, "%s", msg);
141 }
bart68edad52008-02-24 18:21:12 +0000142#endif
143
bartbedfd232009-03-26 19:07:15 +0000144 s_segments_alive_count--;
sewardjaf44c822007-11-25 14:01:38 +0000145
bartbedfd232009-03-26 19:07:15 +0000146 tl_assert(sg);
147 DRD_(sg_cleanup)(sg);
148 VG_(free)(sg);
sewardjaf44c822007-11-25 14:01:38 +0000149}
150
barta2b6e1b2008-03-17 18:32:39 +0000151/** Query the reference count of the specified segment. */
bart62ada3f2009-02-14 17:19:58 +0000152int DRD_(sg_get_refcnt)(const Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000153{
bartbedfd232009-03-26 19:07:15 +0000154 tl_assert(sg);
barta2b6e1b2008-03-17 18:32:39 +0000155
bartbedfd232009-03-26 19:07:15 +0000156 return sg->refcnt;
barta2b6e1b2008-03-17 18:32:39 +0000157}
158
159/** Increment the reference count of the specified segment. */
bart62ada3f2009-02-14 17:19:58 +0000160Segment* DRD_(sg_get)(Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000161{
bartbedfd232009-03-26 19:07:15 +0000162 tl_assert(sg);
barta2b6e1b2008-03-17 18:32:39 +0000163
bartbedfd232009-03-26 19:07:15 +0000164 sg->refcnt++;
165 return sg;
barta2b6e1b2008-03-17 18:32:39 +0000166}
167
bart62ada3f2009-02-14 17:19:58 +0000168/**
169 * Decrement the reference count of the specified segment and deallocate the
170 * segment if the reference count became zero.
barta2b6e1b2008-03-17 18:32:39 +0000171 */
bart62ada3f2009-02-14 17:19:58 +0000172void DRD_(sg_put)(Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000173{
bartbedfd232009-03-26 19:07:15 +0000174 if (sg == 0)
175 return;
barta2b6e1b2008-03-17 18:32:39 +0000176
bartbedfd232009-03-26 19:07:15 +0000177 if (s_trace_segment)
178 {
179 char msg[256];
180 VG_(snprintf)(msg, sizeof(msg),
181 "Decrementing segment reference count %d -> %d with vc ",
182 sg->refcnt, sg->refcnt - 1);
183 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
184 &sg->vc);
185 VG_(message)(Vg_UserMsg, "%s", msg);
186 }
barta2b6e1b2008-03-17 18:32:39 +0000187
bartbedfd232009-03-26 19:07:15 +0000188 tl_assert(sg->refcnt >= 1);
barta2b6e1b2008-03-17 18:32:39 +0000189
bartbedfd232009-03-26 19:07:15 +0000190 if (--sg->refcnt == 0)
191 {
192 DRD_(sg_delete)(sg);
193 }
barta2b6e1b2008-03-17 18:32:39 +0000194}
195
barta9c37392008-03-22 09:38:48 +0000196/** Merge sg1 and sg2 into sg1. */
bart62ada3f2009-02-14 17:19:58 +0000197void DRD_(sg_merge)(const Segment* const sg1, Segment* const sg2)
barta9c37392008-03-22 09:38:48 +0000198{
bartbedfd232009-03-26 19:07:15 +0000199 tl_assert(sg1);
200 tl_assert(sg1->refcnt == 1);
201 tl_assert(sg2);
202 tl_assert(sg2->refcnt == 1);
barta9c37392008-03-22 09:38:48 +0000203
bartbedfd232009-03-26 19:07:15 +0000204 if (s_trace_segment)
205 {
barta9c37392008-03-22 09:38:48 +0000206 char msg[256];
207
208 VG_(snprintf)(msg, sizeof(msg), "Merging segments with vector clocks ");
bart41b226c2009-02-14 16:55:19 +0000209 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
bartbedfd232009-03-26 19:07:15 +0000210 &sg1->vc);
barta9c37392008-03-22 09:38:48 +0000211 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
212 " and ");
bart41b226c2009-02-14 16:55:19 +0000213 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
bartbedfd232009-03-26 19:07:15 +0000214 &sg2->vc);
barta9c37392008-03-22 09:38:48 +0000215 VG_(message)(Vg_UserMsg, "%s", msg);
bartbedfd232009-03-26 19:07:15 +0000216 }
barta9c37392008-03-22 09:38:48 +0000217
bartbedfd232009-03-26 19:07:15 +0000218 // Keep sg1->stacktrace.
219 // Keep sg1->vc.
220 // Merge sg2->bm into sg1->bm.
221 DRD_(bm_merge2)(sg1->bm, sg2->bm);
barta9c37392008-03-22 09:38:48 +0000222}
223
bart62ada3f2009-02-14 17:19:58 +0000224/** Print the vector clock and the bitmap of the specified segment. */
225void DRD_(sg_print)(const Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000226{
bartbedfd232009-03-26 19:07:15 +0000227 tl_assert(sg);
228 VG_(printf)("vc: ");
229 DRD_(vc_print)(&sg->vc);
230 VG_(printf)("\n");
231 DRD_(bm_print)(sg->bm);
sewardjaf44c822007-11-25 14:01:38 +0000232}
233
bart62ada3f2009-02-14 17:19:58 +0000234/** Query whether segment tracing has been enabled. */
235Bool DRD_(sg_get_trace)(void)
sewardjaf44c822007-11-25 14:01:38 +0000236{
bartbedfd232009-03-26 19:07:15 +0000237 return s_trace_segment;
sewardjaf44c822007-11-25 14:01:38 +0000238}
239
bart62ada3f2009-02-14 17:19:58 +0000240/** Enable or disable segment tracing. */
241void DRD_(sg_set_trace)(Bool const trace_segment)
sewardjaf44c822007-11-25 14:01:38 +0000242{
bartbedfd232009-03-26 19:07:15 +0000243 tl_assert(trace_segment == False || trace_segment == True);
244 s_trace_segment = trace_segment;
sewardjaf44c822007-11-25 14:01:38 +0000245}
246
bart62ada3f2009-02-14 17:19:58 +0000247ULong DRD_(sg_get_segments_created_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000248{
bartbedfd232009-03-26 19:07:15 +0000249 return s_segments_created_count;
sewardjaf44c822007-11-25 14:01:38 +0000250}
251
bart62ada3f2009-02-14 17:19:58 +0000252ULong DRD_(sg_get_segments_alive_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000253{
bartbedfd232009-03-26 19:07:15 +0000254 return s_segments_alive_count;
bart7102f102008-03-17 17:37:53 +0000255}
256
bart62ada3f2009-02-14 17:19:58 +0000257ULong DRD_(sg_get_max_segments_alive_count)(void)
bart7102f102008-03-17 17:37:53 +0000258{
bartbedfd232009-03-26 19:07:15 +0000259 return s_max_segments_alive_count;
sewardjaf44c822007-11-25 14:01:38 +0000260}