drd_segment.c revision 2adfc2a8ce3cd0d1c5d1bd211e52d745a75282da
1/*
2  This file is part of drd, a thread error detector.
3
4  Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
5
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
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_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)()
33#include "pub_tool_machine.h"     // VG_(get_SP)()
34#include "pub_tool_mallocfree.h"  // VG_(malloc)(), VG_(free)()
35#include "pub_tool_threadstate.h" // VG_INVALID_THREADID
36
37
38/* Local variables. */
39
40static ULong s_segments_created_count;
41static ULong s_segments_alive_count;
42static ULong s_max_segments_alive_count;
43static Bool s_trace_segment;
44
45
46/* Function definitions. */
47
48/**
49 * Initialize the memory 'sg' points at.
50 *
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.
56 */
57static void sg_init(Segment* const sg,
58                    const DrdThreadId creator,
59                    const DrdThreadId created)
60{
61  Segment* creator_sg;
62  ThreadId vg_created = DRD_(DrdThreadIdToVgThreadId)(created);
63
64  tl_assert(sg);
65  tl_assert(creator == DRD_INVALID_THREADID
66            || DRD_(IsValidDrdThreadId)(creator));
67
68  creator_sg = (creator != DRD_INVALID_THREADID
69                ? DRD_(thread_get_segment)(creator) : 0);
70
71  sg->next = 0;
72  sg->prev = 0;
73  sg->refcnt = 1;
74
75  if (vg_created != VG_INVALID_THREADID && VG_(get_SP)(vg_created) != 0)
76    sg->stacktrace = VG_(record_ExeContext)(vg_created, 0);
77  else
78    sg->stacktrace = 0;
79
80  if (creator_sg)
81    DRD_(vc_copy)(&sg->vc, &creator_sg->vc);
82  else
83    DRD_(vc_init)(&sg->vc, 0, 0);
84  DRD_(vc_increment)(&sg->vc, created);
85  sg->bm = DRD_(bm_new)();
86
87  if (s_trace_segment)
88  {
89    char msg[256];
90    VG_(snprintf)(msg, sizeof(msg),
91                  "New segment for thread %d/%d with vc ",
92                  created != VG_INVALID_THREADID
93                  ? DRD_(DrdThreadIdToVgThreadId)(created)
94                  : DRD_INVALID_THREADID,
95                  created);
96    DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
97               &sg->vc);
98    VG_(message)(Vg_UserMsg, "%s", msg);
99  }
100}
101
102/** Deallocate the memory that was allocated by sg_init(). */
103static void DRD_(sg_cleanup)(Segment* const sg)
104{
105  tl_assert(sg);
106  tl_assert(sg->refcnt == 0);
107
108  DRD_(vc_cleanup)(&sg->vc);
109  DRD_(bm_delete)(sg->bm);
110  sg->bm = 0;
111}
112
113/** Allocate and initialize a new segment. */
114Segment* DRD_(sg_new)(const DrdThreadId creator, const DrdThreadId created)
115{
116  Segment* sg;
117
118  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;
122
123  sg = VG_(malloc)("drd.segment.sn.1", sizeof(*sg));
124  tl_assert(sg);
125  sg_init(sg, creator, created);
126  return sg;
127}
128
129static void DRD_(sg_delete)(Segment* const sg)
130{
131#if 1
132  if (DRD_(sg_get_trace)())
133  {
134    char msg[256];
135    VG_(snprintf)(msg, sizeof(msg),
136                  "Discarding the segment with vector clock ");
137    DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
138               &sg->vc);
139    VG_(message)(Vg_UserMsg, "%s", msg);
140  }
141#endif
142
143  s_segments_alive_count--;
144
145  tl_assert(sg);
146  DRD_(sg_cleanup)(sg);
147  VG_(free)(sg);
148}
149
150/** Query the reference count of the specified segment. */
151int DRD_(sg_get_refcnt)(const Segment* const sg)
152{
153  tl_assert(sg);
154
155  return sg->refcnt;
156}
157
158/** Increment the reference count of the specified segment. */
159Segment* DRD_(sg_get)(Segment* const sg)
160{
161  tl_assert(sg);
162
163  sg->refcnt++;
164  return sg;
165}
166
167/**
168 * Decrement the reference count of the specified segment and deallocate the
169 * segment if the reference count became zero.
170 */
171void DRD_(sg_put)(Segment* const sg)
172{
173  if (sg == 0)
174    return;
175
176  if (s_trace_segment)
177  {
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);
182    DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
183               &sg->vc);
184    VG_(message)(Vg_UserMsg, "%s", msg);
185  }
186
187  tl_assert(sg->refcnt >= 1);
188
189  if (--sg->refcnt == 0)
190  {
191    DRD_(sg_delete)(sg);
192  }
193}
194
195/** Merge sg1 and sg2 into sg1. */
196void DRD_(sg_merge)(const Segment* const sg1, Segment* const sg2)
197{
198  tl_assert(sg1);
199  tl_assert(sg1->refcnt == 1);
200  tl_assert(sg2);
201  tl_assert(sg2->refcnt == 1);
202
203  if (s_trace_segment)
204  {
205      char msg[256];
206
207      VG_(snprintf)(msg, sizeof(msg), "Merging segments with vector clocks ");
208      DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
209                 &sg1->vc);
210      VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
211                    " and ");
212      DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
213                 &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.
220  DRD_(bm_merge2)(sg1->bm, sg2->bm);
221}
222
223/** Print the vector clock and the bitmap of the specified segment. */
224void DRD_(sg_print)(const Segment* const sg)
225{
226  tl_assert(sg);
227  VG_(printf)("vc: ");
228  DRD_(vc_print)(&sg->vc);
229  VG_(printf)("\n");
230  DRD_(bm_print)(sg->bm);
231}
232
233/** Query whether segment tracing has been enabled. */
234Bool DRD_(sg_get_trace)(void)
235{
236  return s_trace_segment;
237}
238
239/** Enable or disable segment tracing. */
240void DRD_(sg_set_trace)(Bool const trace_segment)
241{
242  tl_assert(trace_segment == False || trace_segment == True);
243  s_trace_segment = trace_segment;
244}
245
246ULong DRD_(sg_get_segments_created_count)(void)
247{
248  return s_segments_created_count;
249}
250
251ULong DRD_(sg_get_segments_alive_count)(void)
252{
253  return s_segments_alive_count;
254}
255
256ULong DRD_(sg_get_max_segments_alive_count)(void)
257{
258  return s_max_segments_alive_count;
259}
260