tests/vg_regtest: Always evaluate prerequisite expressions with sh
[valgrind.git] / drd / drd_segment.c
bloba625f474321beef466c4e45e5a32d291d0ecd74a
1 /*
2 This file is part of drd, a thread error detector.
4 Copyright (C) 2006-2013 Bart Van Assche <bvanassche@acm.org>.
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.
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.
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.
21 The GNU General Public License is contained in the file COPYING.
25 #include "drd_error.h"
26 #include "drd_segment.h"
27 #include "drd_thread.h"
28 #include "pub_tool_basics.h" // Addr, SizeT
29 #include "pub_tool_libcassert.h" // tl_assert()
30 #include "pub_tool_libcbase.h" // VG_(strlen)()
31 #include "pub_tool_libcprint.h" // VG_(printf)()
32 #include "pub_tool_machine.h" // VG_(get_SP)()
33 #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
34 #include "pub_tool_threadstate.h" // VG_INVALID_THREADID
37 /* Global variables. */
39 Segment* DRD_(g_sg_list);
42 /* Local variables. */
44 static ULong s_segment_merge_count;
45 static ULong s_segments_created_count;
46 static ULong s_segments_alive_count;
47 static ULong s_max_segments_alive_count;
48 static Bool s_trace_segment;
51 /* Function definitions. */
53 /**
54 * Initialize the memory 'sg' points at.
56 * @note The creator and created thread ID's may be equal.
57 * @note This function copies the vector clock of thread 'creator', a technique
58 * also known as clock snooping. This will only work reliably if the thread
59 * that called pthread_create() waits until the created thread has copied
60 * the vector clock.
62 static void sg_init(Segment* const sg,
63 const DrdThreadId creator,
64 const DrdThreadId created)
66 Segment* creator_sg;
67 ThreadId vg_created = DRD_(DrdThreadIdToVgThreadId)(created);
69 tl_assert(sg);
70 tl_assert(creator == DRD_INVALID_THREADID
71 || DRD_(IsValidDrdThreadId)(creator));
73 creator_sg = (creator != DRD_INVALID_THREADID
74 ? DRD_(thread_get_segment)(creator) : 0);
76 sg->g_next = NULL;
77 sg->g_prev = NULL;
78 sg->thr_next = NULL;
79 sg->thr_prev = NULL;
80 sg->tid = created;
81 sg->refcnt = 1;
83 if (vg_created != VG_INVALID_THREADID && VG_(get_SP)(vg_created) != 0)
84 sg->stacktrace = VG_(record_ExeContext)(vg_created, 0);
85 else
86 sg->stacktrace = 0;
88 if (creator_sg)
89 DRD_(vc_copy)(&sg->vc, &creator_sg->vc);
90 else
91 DRD_(vc_init)(&sg->vc, 0, 0);
92 DRD_(vc_increment)(&sg->vc, created);
93 DRD_(bm_init)(&sg->bm);
95 if (s_trace_segment)
97 HChar* vc;
99 vc = DRD_(vc_aprint)(&sg->vc);
100 VG_(message)(Vg_DebugMsg, "New segment for thread %d with vc %s\n",
101 created, vc);
102 VG_(free)(vc);
106 /** Deallocate the memory that was allocated by sg_init(). */
107 static void DRD_(sg_cleanup)(Segment* const sg)
109 tl_assert(sg);
110 tl_assert(sg->refcnt == 0);
112 DRD_(vc_cleanup)(&sg->vc);
113 DRD_(bm_cleanup)(&sg->bm);
116 /** Allocate and initialize a new segment. */
117 Segment* DRD_(sg_new)(const DrdThreadId creator, const DrdThreadId created)
119 Segment* sg;
121 s_segments_created_count++;
122 s_segments_alive_count++;
123 if (s_max_segments_alive_count < s_segments_alive_count)
124 s_max_segments_alive_count = s_segments_alive_count;
126 sg = VG_(malloc)("drd.segment.sn.1", sizeof(*sg));
127 sg_init(sg, creator, created);
128 if (DRD_(g_sg_list)) {
129 DRD_(g_sg_list)->g_prev = sg;
130 sg->g_next = DRD_(g_sg_list);
132 DRD_(g_sg_list) = sg;
133 return sg;
136 static void DRD_(sg_delete)(Segment* const sg)
138 if (DRD_(sg_get_trace)())
140 HChar* vc;
142 vc = DRD_(vc_aprint)(&sg->vc);
143 VG_(message)(Vg_DebugMsg, "Discarding the segment with vector clock %s\n",
144 vc);
145 VG_(free)(vc);
148 s_segments_alive_count--;
150 tl_assert(sg);
151 if (sg->g_next)
152 sg->g_next->g_prev = sg->g_prev;
153 if (sg->g_prev)
154 sg->g_prev->g_next = sg->g_next;
155 else
156 DRD_(g_sg_list) = sg->g_next;
157 DRD_(sg_cleanup)(sg);
158 VG_(free)(sg);
161 /** Increment the reference count of the specified segment. */
162 Segment* DRD_(sg_get)(Segment* const sg)
164 tl_assert(sg);
166 sg->refcnt++;
167 return sg;
171 * Decrement the reference count of the specified segment and deallocate the
172 * segment if the reference count became zero.
174 void DRD_(sg_put)(Segment* const sg)
176 if (sg == 0)
177 return;
179 if (s_trace_segment)
181 HChar* vc;
183 vc = DRD_(vc_aprint)(&sg->vc);
184 VG_(message)(Vg_DebugMsg,
185 "Decrementing segment reference count %d -> %d with vc %s\n",
186 sg->refcnt, sg->refcnt - 1, vc);
187 VG_(free)(vc);
190 tl_assert(sg->refcnt >= 1);
192 if (--sg->refcnt == 0)
194 DRD_(sg_delete)(sg);
198 /** Merge sg1 and sg2 into sg1. */
199 void DRD_(sg_merge)(Segment* const sg1, Segment* const sg2)
201 tl_assert(sg1);
202 tl_assert(sg1->refcnt == 1);
203 tl_assert(sg2);
204 tl_assert(sg2->refcnt == 1);
206 if (s_trace_segment)
208 HChar *vc1, *vc2;
210 vc1 = DRD_(vc_aprint)(&sg1->vc);
211 vc2 = DRD_(vc_aprint)(&sg2->vc);
213 VG_(message)(Vg_DebugMsg,
214 "Merging segments with vector clocks %s and %s\n", vc1, vc2);
215 VG_(free)(vc1);
216 VG_(free)(vc2);
219 s_segment_merge_count++;
221 // Keep sg1->stacktrace.
222 // Keep sg1->vc.
223 // Merge sg2->bm into sg1->bm.
224 DRD_(bm_merge2)(&sg1->bm, &sg2->bm);
227 /** Print the vector clock and the bitmap of the specified segment. */
228 void DRD_(sg_print)(Segment* const sg)
230 tl_assert(sg);
231 VG_(printf)("vc: ");
232 DRD_(vc_print)(&sg->vc);
233 VG_(printf)("\n");
234 DRD_(bm_print)(&sg->bm);
237 /** Query whether segment tracing has been enabled. */
238 Bool DRD_(sg_get_trace)(void)
240 return s_trace_segment;
243 /** Enable or disable segment tracing. */
244 void DRD_(sg_set_trace)(Bool const trace_segment)
246 tl_assert(trace_segment == False || trace_segment == True);
247 s_trace_segment = trace_segment;
250 ULong DRD_(sg_get_segments_created_count)(void)
252 return s_segments_created_count;
255 ULong DRD_(sg_get_segments_alive_count)(void)
257 return s_segments_alive_count;
260 ULong DRD_(sg_get_max_segments_alive_count)(void)
262 return s_max_segments_alive_count;
265 ULong DRD_(sg_get_segment_merge_count)(void)
267 return s_segment_merge_count;