blob: a7947f7c49738bacdfa44430e0edb8cf6955ab67 [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
sewardjaf44c822007-11-25 14:01:38 +000030#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
bart1a3b0b32009-05-03 17:07:34 +000040static ULong s_segment_merge_count;
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;
bart8f822af2009-06-08 18:20:42 +000074 sg->tid = created;
bartbedfd232009-03-26 19:07:15 +000075 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);
bart8f822af2009-06-08 18:20:42 +000087 DRD_(bm_init)(&sg->bm);
sewardjaf44c822007-11-25 14:01:38 +000088
bartbedfd232009-03-26 19:07:15 +000089 if (s_trace_segment)
90 {
bart8f822af2009-06-08 18:20:42 +000091 char* vc;
92
93 vc = DRD_(vc_aprint)(&sg->vc);
bart4565e272010-03-07 20:05:58 +000094 VG_(message)(Vg_DebugMsg, "New segment for thread %d with vc %s\n",
bart8f822af2009-06-08 18:20:42 +000095 created, vc);
96 VG_(free)(vc);
bartbedfd232009-03-26 19:07:15 +000097 }
sewardjaf44c822007-11-25 14:01:38 +000098}
99
barta2b6e1b2008-03-17 18:32:39 +0000100/** Deallocate the memory that was allocated by sg_init(). */
bart62ada3f2009-02-14 17:19:58 +0000101static void DRD_(sg_cleanup)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000102{
bartbedfd232009-03-26 19:07:15 +0000103 tl_assert(sg);
104 tl_assert(sg->refcnt == 0);
barta2b6e1b2008-03-17 18:32:39 +0000105
bartbedfd232009-03-26 19:07:15 +0000106 DRD_(vc_cleanup)(&sg->vc);
bart8f822af2009-06-08 18:20:42 +0000107 DRD_(bm_cleanup)(&sg->bm);
sewardjaf44c822007-11-25 14:01:38 +0000108}
109
barta2b6e1b2008-03-17 18:32:39 +0000110/** Allocate and initialize a new segment. */
bart2adfc2a2009-03-12 18:38:00 +0000111Segment* DRD_(sg_new)(const DrdThreadId creator, const DrdThreadId created)
sewardjaf44c822007-11-25 14:01:38 +0000112{
bartbedfd232009-03-26 19:07:15 +0000113 Segment* sg;
sewardjaf44c822007-11-25 14:01:38 +0000114
bartbedfd232009-03-26 19:07:15 +0000115 s_segments_created_count++;
116 s_segments_alive_count++;
117 if (s_max_segments_alive_count < s_segments_alive_count)
118 s_max_segments_alive_count = s_segments_alive_count;
sewardjaf44c822007-11-25 14:01:38 +0000119
bartbedfd232009-03-26 19:07:15 +0000120 sg = VG_(malloc)("drd.segment.sn.1", sizeof(*sg));
121 tl_assert(sg);
122 sg_init(sg, creator, created);
123 return sg;
sewardjaf44c822007-11-25 14:01:38 +0000124}
125
bart62ada3f2009-02-14 17:19:58 +0000126static void DRD_(sg_delete)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000127{
bartbedfd232009-03-26 19:07:15 +0000128 if (DRD_(sg_get_trace)())
129 {
bart8f822af2009-06-08 18:20:42 +0000130 char* vc;
131
132 vc = DRD_(vc_aprint)(&sg->vc);
bart4565e272010-03-07 20:05:58 +0000133 VG_(message)(Vg_DebugMsg, "Discarding the segment with vector clock %s\n",
bart8f822af2009-06-08 18:20:42 +0000134 vc);
135 VG_(free)(vc);
bartbedfd232009-03-26 19:07:15 +0000136 }
bart68edad52008-02-24 18:21:12 +0000137
bartbedfd232009-03-26 19:07:15 +0000138 s_segments_alive_count--;
sewardjaf44c822007-11-25 14:01:38 +0000139
bartbedfd232009-03-26 19:07:15 +0000140 tl_assert(sg);
141 DRD_(sg_cleanup)(sg);
142 VG_(free)(sg);
sewardjaf44c822007-11-25 14:01:38 +0000143}
144
barta2b6e1b2008-03-17 18:32:39 +0000145/** Increment the reference count of the specified segment. */
bart62ada3f2009-02-14 17:19:58 +0000146Segment* DRD_(sg_get)(Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000147{
bartbedfd232009-03-26 19:07:15 +0000148 tl_assert(sg);
barta2b6e1b2008-03-17 18:32:39 +0000149
bartbedfd232009-03-26 19:07:15 +0000150 sg->refcnt++;
151 return sg;
barta2b6e1b2008-03-17 18:32:39 +0000152}
153
bart62ada3f2009-02-14 17:19:58 +0000154/**
155 * Decrement the reference count of the specified segment and deallocate the
156 * segment if the reference count became zero.
barta2b6e1b2008-03-17 18:32:39 +0000157 */
bart62ada3f2009-02-14 17:19:58 +0000158void DRD_(sg_put)(Segment* const sg)
barta2b6e1b2008-03-17 18:32:39 +0000159{
bartbedfd232009-03-26 19:07:15 +0000160 if (sg == 0)
161 return;
barta2b6e1b2008-03-17 18:32:39 +0000162
bartbedfd232009-03-26 19:07:15 +0000163 if (s_trace_segment)
164 {
bart8f822af2009-06-08 18:20:42 +0000165 char* vc;
166
167 vc = DRD_(vc_aprint)(&sg->vc);
168 VG_(message)(Vg_DebugMsg,
bart4565e272010-03-07 20:05:58 +0000169 "Decrementing segment reference count %d -> %d with vc %s\n",
bart8f822af2009-06-08 18:20:42 +0000170 sg->refcnt, sg->refcnt - 1, vc);
171 VG_(free)(vc);
bartbedfd232009-03-26 19:07:15 +0000172 }
barta2b6e1b2008-03-17 18:32:39 +0000173
bartbedfd232009-03-26 19:07:15 +0000174 tl_assert(sg->refcnt >= 1);
barta2b6e1b2008-03-17 18:32:39 +0000175
bartbedfd232009-03-26 19:07:15 +0000176 if (--sg->refcnt == 0)
177 {
178 DRD_(sg_delete)(sg);
179 }
barta2b6e1b2008-03-17 18:32:39 +0000180}
181
barta9c37392008-03-22 09:38:48 +0000182/** Merge sg1 and sg2 into sg1. */
bart8f822af2009-06-08 18:20:42 +0000183void DRD_(sg_merge)(Segment* const sg1, Segment* const sg2)
barta9c37392008-03-22 09:38:48 +0000184{
bartbedfd232009-03-26 19:07:15 +0000185 tl_assert(sg1);
186 tl_assert(sg1->refcnt == 1);
187 tl_assert(sg2);
188 tl_assert(sg2->refcnt == 1);
barta9c37392008-03-22 09:38:48 +0000189
bartbedfd232009-03-26 19:07:15 +0000190 if (s_trace_segment)
191 {
bart8f822af2009-06-08 18:20:42 +0000192 char *vc1, *vc2;
barta9c37392008-03-22 09:38:48 +0000193
bart8f822af2009-06-08 18:20:42 +0000194 vc1 = DRD_(vc_aprint)(&sg1->vc);
195 vc2 = DRD_(vc_aprint)(&sg2->vc);
196
bart4565e272010-03-07 20:05:58 +0000197 VG_(message)(Vg_DebugMsg,
198 "Merging segments with vector clocks %s and %s\n", vc1, vc2);
bart8f822af2009-06-08 18:20:42 +0000199 VG_(free)(vc1);
200 VG_(free)(vc2);
bartbedfd232009-03-26 19:07:15 +0000201 }
barta9c37392008-03-22 09:38:48 +0000202
bart1a3b0b32009-05-03 17:07:34 +0000203 s_segment_merge_count++;
204
bartbedfd232009-03-26 19:07:15 +0000205 // Keep sg1->stacktrace.
206 // Keep sg1->vc.
207 // Merge sg2->bm into sg1->bm.
bart8f822af2009-06-08 18:20:42 +0000208 DRD_(bm_merge2)(&sg1->bm, &sg2->bm);
barta9c37392008-03-22 09:38:48 +0000209}
210
bart62ada3f2009-02-14 17:19:58 +0000211/** Print the vector clock and the bitmap of the specified segment. */
bart8f822af2009-06-08 18:20:42 +0000212void DRD_(sg_print)(Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000213{
bartbedfd232009-03-26 19:07:15 +0000214 tl_assert(sg);
215 VG_(printf)("vc: ");
216 DRD_(vc_print)(&sg->vc);
217 VG_(printf)("\n");
bart8f822af2009-06-08 18:20:42 +0000218 DRD_(bm_print)(&sg->bm);
sewardjaf44c822007-11-25 14:01:38 +0000219}
220
bart62ada3f2009-02-14 17:19:58 +0000221/** Query whether segment tracing has been enabled. */
222Bool DRD_(sg_get_trace)(void)
sewardjaf44c822007-11-25 14:01:38 +0000223{
bartbedfd232009-03-26 19:07:15 +0000224 return s_trace_segment;
sewardjaf44c822007-11-25 14:01:38 +0000225}
226
bart62ada3f2009-02-14 17:19:58 +0000227/** Enable or disable segment tracing. */
228void DRD_(sg_set_trace)(Bool const trace_segment)
sewardjaf44c822007-11-25 14:01:38 +0000229{
bartbedfd232009-03-26 19:07:15 +0000230 tl_assert(trace_segment == False || trace_segment == True);
231 s_trace_segment = trace_segment;
sewardjaf44c822007-11-25 14:01:38 +0000232}
233
bart62ada3f2009-02-14 17:19:58 +0000234ULong DRD_(sg_get_segments_created_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000235{
bartbedfd232009-03-26 19:07:15 +0000236 return s_segments_created_count;
sewardjaf44c822007-11-25 14:01:38 +0000237}
238
bart62ada3f2009-02-14 17:19:58 +0000239ULong DRD_(sg_get_segments_alive_count)(void)
sewardjaf44c822007-11-25 14:01:38 +0000240{
bartbedfd232009-03-26 19:07:15 +0000241 return s_segments_alive_count;
bart7102f102008-03-17 17:37:53 +0000242}
243
bart62ada3f2009-02-14 17:19:58 +0000244ULong DRD_(sg_get_max_segments_alive_count)(void)
bart7102f102008-03-17 17:37:53 +0000245{
bartbedfd232009-03-26 19:07:15 +0000246 return s_max_segments_alive_count;
sewardjaf44c822007-11-25 14:01:38 +0000247}
bart1a3b0b32009-05-03 17:07:34 +0000248
249ULong DRD_(sg_get_segment_merge_count)(void)
250{
251 return s_segment_merge_count;
252}