1 /* -*- mode: C; c-basic-offset: 3; indent-tabs-mode: nil; -*- */
3 This file is part of drd, a thread error detector.
5 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
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.
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.
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
22 The GNU General Public License is contained in the file COPYING.
26 #include "drd_clientobj.h"
28 #include "drd_error.h"
29 #include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */
30 #include "pub_tool_libcassert.h" /* tl_assert() */
31 #include "pub_tool_libcprint.h" /* VG_(printf)() */
32 #include "pub_tool_machine.h" /* VG_(get_IP)() */
33 #include "pub_tool_mallocfree.h" /* VG_(malloc)(), VG_(free)()*/
34 #include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */
37 /* Type definitions. */
39 /** Per-thread hb information. */
42 UWord tid; // A DrdThreadId declared as UWord because
43 // this member variable is the key of an OSet.
44 Segment* sg; // Segment created before most recent
45 // ANNOTATE_HAPPENS_BEFORE().
49 /* Local functions. */
51 static void DRD_(hb_cleanup)(struct hb_info* p);
54 /* Local variables. */
56 static Bool DRD_(s_trace_hb);
59 /* Function definitions. */
61 void DRD_(hb_set_trace)(const Bool trace_hb)
63 DRD_(s_trace_hb) = trace_hb;
67 * Initialize the structure *p with the specified thread ID.
70 void DRD_(hb_thread_initialize)(struct hb_thread_info* const p,
71 const DrdThreadId tid)
78 * Deallocate the memory that is owned by members of struct hb_thread_info.
80 static void DRD_(hb_thread_destroy)(struct hb_thread_info* const p)
87 void DRD_(hb_initialize)(struct hb_info* const p, const Addr hb)
90 tl_assert(p->a1 == hb);
91 tl_assert(p->type == ClientHbvar);
93 p->cleanup = (void(*)(DrdClientobj*))(DRD_(hb_cleanup));
95 p->oset = VG_(OSetGen_Create)(0, 0, VG_(malloc), "drd.hb",
100 * Free the memory that was allocated by hb_initialize(). Called by
101 * DRD_(clientobj_remove)().
103 static void DRD_(hb_cleanup)(struct hb_info* p)
105 struct hb_thread_info* r;
108 VG_(OSetGen_ResetIter)(p->oset);
109 for ( ; (r = VG_(OSetGen_Next)(p->oset)) != 0; )
110 DRD_(hb_thread_destroy)(r);
111 VG_(OSetGen_Destroy)(p->oset);
115 * Report that the synchronization object at address 'addr' is of the
118 static void wrong_type(const Addr addr)
120 GenericErrInfo gei = {
121 .tid = DRD_(thread_get_running_tid)(),
124 VG_(maybe_record_error)(VG_(get_running_tid)(),
126 VG_(get_IP)(VG_(get_running_tid)()),
127 "wrong type of synchronization object",
131 struct hb_info* DRD_(hb_get_or_allocate)(const Addr hb)
135 tl_assert(offsetof(DrdClientobj, hb) == 0);
136 p = &(DRD_(clientobj_get)(hb, ClientHbvar)->hb);
140 if (DRD_(clientobj_present)(hb, hb + 1))
146 p = &(DRD_(clientobj_add)(hb, ClientHbvar)->hb);
147 DRD_(hb_initialize)(p, hb);
151 struct hb_info* DRD_(hb_get)(const Addr hb)
153 tl_assert(offsetof(DrdClientobj, hb) == 0);
154 return &(DRD_(clientobj_get)(hb, ClientHbvar)->hb);
157 /** Called because of a happens-before annotation. */
158 void DRD_(hb_happens_before)(const DrdThreadId tid, Addr const hb)
160 const ThreadId vg_tid = VG_(get_running_tid)();
161 const DrdThreadId drd_tid = DRD_(VgThreadIdToDrdThreadId)(vg_tid);
162 const UWord word_tid = tid;
164 struct hb_thread_info* q;
166 p = DRD_(hb_get_or_allocate)(hb);
167 if (DRD_(s_trace_hb))
169 VG_(message)(Vg_UserMsg,
170 "[%d] happens_before 0x%lx\n",
171 DRD_(thread_get_running_tid)(),
178 /* Allocate the per-thread data structure if necessary. */
179 q = VG_(OSetGen_Lookup)(p->oset, &word_tid);
182 q = VG_(OSetGen_AllocNode)(p->oset, sizeof(*q));
183 DRD_(hb_thread_initialize)(q, tid);
184 VG_(OSetGen_Insert)(p->oset, q);
185 tl_assert(VG_(OSetGen_Lookup)(p->oset, &word_tid) == q);
189 * Store a pointer to the latest segment of the current thread in the
190 * per-thread data structure.
192 DRD_(thread_get_latest_segment)(&q->sg, tid);
193 DRD_(thread_new_segment)(drd_tid);
196 /** Called because of a happens-after annotation. */
197 void DRD_(hb_happens_after)(const DrdThreadId tid, const Addr hb)
200 struct hb_thread_info* q;
203 p = DRD_(hb_get_or_allocate)(hb);
205 if (DRD_(s_trace_hb))
207 VG_(message)(Vg_UserMsg, "[%d] happens_after 0x%lx\n",
208 DRD_(thread_get_running_tid)(), hb);
214 DRD_(thread_new_segment)(tid);
217 * Combine all vector clocks that were stored because of happens-before
218 * annotations with the vector clock of the current thread.
220 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[tid].last->vc);
221 VG_(OSetGen_ResetIter)(p->oset);
222 for ( ; (q = VG_(OSetGen_Next)(p->oset)) != 0; )
227 DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, &q->sg->vc);
230 DRD_(thread_update_conflict_set)(tid, &old_vc);
231 DRD_(vc_cleanup)(&old_vc);
234 /** Called because of a happens-done annotation. */
235 void DRD_(hb_happens_done)(const DrdThreadId tid, const Addr hb)
239 if (DRD_(s_trace_hb))
241 VG_(message)(Vg_UserMsg, "[%d] happens_done 0x%lx\n",
242 DRD_(thread_get_running_tid)(), hb);
245 p = DRD_(hb_get)(hb);
248 GenericErrInfo gei = {
249 .tid = DRD_(thread_get_running_tid)(),
252 VG_(maybe_record_error)(VG_(get_running_tid)(),
254 VG_(get_IP)(VG_(get_running_tid)()),
255 "missing happens-before annotation",
260 DRD_(clientobj_remove)(p->a1, ClientHbvar);