blob: 7e3a8c67a1674eb3060b99504a0cd36b6ea901ad [file] [log] [blame]
bart922304f2011-03-13 12:02:44 +00001/* -*- mode: C; c-basic-offset: 3; indent-tabs-mode: nil; -*- */
barte7d58722008-02-28 19:08:04 +00002/*
bart86562bd2009-02-16 19:43:56 +00003 This file is part of drd, a thread error detector.
barte7d58722008-02-28 19:08:04 +00004
bart922304f2011-03-13 12:02:44 +00005 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
barte7d58722008-02-28 19:08:04 +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
26#include "drd_clientobj.h"
bartb92ff0f2011-10-08 08:29:29 +000027#include "drd_error.h"
barte7d58722008-02-28 19:08:04 +000028#include "drd_suppression.h"
29#include "pub_tool_basics.h"
30#include "pub_tool_libcassert.h"
31#include "pub_tool_libcbase.h"
bart72b751c2008-03-01 13:44:24 +000032#include "pub_tool_libcprint.h" // VG_(message)()
barte7d58722008-02-28 19:08:04 +000033#include "pub_tool_mallocfree.h"
bart72b751c2008-03-01 13:44:24 +000034#include "pub_tool_options.h" // VG_(clo_backtrace_size)
barte7d58722008-02-28 19:08:04 +000035#include "pub_tool_oset.h"
bart72b751c2008-03-01 13:44:24 +000036#include "pub_tool_stacktrace.h"
37#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
barte7d58722008-02-28 19:08:04 +000038
39
bart195e41f2009-02-15 11:34:57 +000040/* Local variables. */
barte7d58722008-02-28 19:08:04 +000041
bartd2c5eae2009-02-21 15:27:04 +000042static OSet* s_clientobj_set;
43static Bool s_trace_clientobj;
44
45
46/* Local functions. */
47
48static Bool clientobj_remove_obj(DrdClientobj* const p);
barte7d58722008-02-28 19:08:04 +000049
50
bart195e41f2009-02-15 11:34:57 +000051/* Function definitions. */
barte7d58722008-02-28 19:08:04 +000052
bart195e41f2009-02-15 11:34:57 +000053void DRD_(clientobj_set_trace)(const Bool trace)
bart72b751c2008-03-01 13:44:24 +000054{
bartbedfd232009-03-26 19:07:15 +000055 s_trace_clientobj = trace;
bart72b751c2008-03-01 13:44:24 +000056}
57
barte7d58722008-02-28 19:08:04 +000058/** Initialize the client object set. */
bart195e41f2009-02-15 11:34:57 +000059void DRD_(clientobj_init)(void)
barte7d58722008-02-28 19:08:04 +000060{
bartbedfd232009-03-26 19:07:15 +000061 tl_assert(s_clientobj_set == 0);
62 s_clientobj_set = VG_(OSetGen_Create)(0, 0, VG_(malloc),
63 "drd.clientobj.ci.1", VG_(free));
64 tl_assert(s_clientobj_set);
barte7d58722008-02-28 19:08:04 +000065}
66
bart195e41f2009-02-15 11:34:57 +000067/**
68 * Free the memory allocated for the client object set.
69 *
70 * @pre Client object set is empty.
barte7d58722008-02-28 19:08:04 +000071 */
bart195e41f2009-02-15 11:34:57 +000072void DRD_(clientobj_cleanup)(void)
barte7d58722008-02-28 19:08:04 +000073{
bartbedfd232009-03-26 19:07:15 +000074 tl_assert(s_clientobj_set);
75 tl_assert(VG_(OSetGen_Size)(s_clientobj_set) == 0);
76 VG_(OSetGen_Destroy)(s_clientobj_set);
77 s_clientobj_set = 0;
barte7d58722008-02-28 19:08:04 +000078}
79
bartd2c5eae2009-02-21 15:27:04 +000080/**
81 * Return the data associated with the client object at client address addr.
82 * Return 0 if there is no client object in the set with the specified start
83 * address.
bart391d9dc2008-07-03 10:57:30 +000084 */
bart195e41f2009-02-15 11:34:57 +000085DrdClientobj* DRD_(clientobj_get_any)(const Addr addr)
bart391d9dc2008-07-03 10:57:30 +000086{
bartbedfd232009-03-26 19:07:15 +000087 return VG_(OSetGen_Lookup)(s_clientobj_set, &addr);
bart391d9dc2008-07-03 10:57:30 +000088}
89
bart1344d6c2009-02-21 16:17:50 +000090/**
91 * Return the data associated with the client object at client address addr
92 * and that has object type t. Return 0 if there is no client object in the
93 * set with the specified start address.
barte7d58722008-02-28 19:08:04 +000094 */
bart195e41f2009-02-15 11:34:57 +000095DrdClientobj* DRD_(clientobj_get)(const Addr addr, const ObjType t)
barte7d58722008-02-28 19:08:04 +000096{
bartbedfd232009-03-26 19:07:15 +000097 DrdClientobj* p;
98 p = VG_(OSetGen_Lookup)(s_clientobj_set, &addr);
99 if (p && p->any.type == t)
100 return p;
101 return 0;
barte7d58722008-02-28 19:08:04 +0000102}
103
104/** Return true if and only if the address range of any client object overlaps
105 * with the specified address range.
106 */
bart195e41f2009-02-15 11:34:57 +0000107Bool DRD_(clientobj_present)(const Addr a1, const Addr a2)
barte7d58722008-02-28 19:08:04 +0000108{
bartbedfd232009-03-26 19:07:15 +0000109 DrdClientobj *p;
barte7d58722008-02-28 19:08:04 +0000110
barta3003982010-09-08 16:29:17 +0000111 tl_assert(a1 <= a2);
bartbedfd232009-03-26 19:07:15 +0000112 VG_(OSetGen_ResetIter)(s_clientobj_set);
113 for ( ; (p = VG_(OSetGen_Next)(s_clientobj_set)) != 0; )
114 {
115 if (a1 <= p->any.a1 && p->any.a1 < a2)
116 {
bart31b983d2010-02-21 14:52:59 +0000117 return True;
bartbedfd232009-03-26 19:07:15 +0000118 }
119 }
120 return False;
barte7d58722008-02-28 19:08:04 +0000121}
122
bartbedfd232009-03-26 19:07:15 +0000123/**
124 * Add state information for the client object at client address addr and
125 * of type t. Suppress data race reports on the address range [addr,addr+size[.
126 *
127 * @pre No other client object is present in the address range [addr,addr+size[.
barte7d58722008-02-28 19:08:04 +0000128 */
bart195e41f2009-02-15 11:34:57 +0000129DrdClientobj* DRD_(clientobj_add)(const Addr a1, const ObjType t)
barte7d58722008-02-28 19:08:04 +0000130{
bartbedfd232009-03-26 19:07:15 +0000131 DrdClientobj* p;
barte7d58722008-02-28 19:08:04 +0000132
bartbedfd232009-03-26 19:07:15 +0000133 tl_assert(! DRD_(clientobj_present)(a1, a1 + 1));
134 tl_assert(VG_(OSetGen_Lookup)(s_clientobj_set, &a1) == 0);
bartbcbd7482008-02-29 19:19:39 +0000135
bartbedfd232009-03-26 19:07:15 +0000136 if (s_trace_clientobj)
bartb92ff0f2011-10-08 08:29:29 +0000137 DRD_(trace_msg)("Adding client object 0x%lx of type %d\n", a1, t);
bart72b751c2008-03-01 13:44:24 +0000138
bartbedfd232009-03-26 19:07:15 +0000139 p = VG_(OSetGen_AllocNode)(s_clientobj_set, sizeof(*p));
140 VG_(memset)(p, 0, sizeof(*p));
141 p->any.a1 = a1;
142 p->any.type = t;
143 p->any.first_observed_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
144 VG_(OSetGen_Insert)(s_clientobj_set, p);
145 tl_assert(VG_(OSetGen_Lookup)(s_clientobj_set, &a1) == p);
bartb878a732010-03-07 20:07:15 +0000146 if (t == ClientHbvar)
147 DRD_(mark_hbvar)(a1);
148 else
149 DRD_(start_suppression)(a1, a1 + 1, "clientobj");
bartbedfd232009-03-26 19:07:15 +0000150 return p;
barte7d58722008-02-28 19:08:04 +0000151}
152
bart1344d6c2009-02-21 16:17:50 +0000153/**
154 * Remove the information that was stored about the client object.
155 *
156 * @param[in] addr Address of the client object in the client address space.
157 * @param[in] t Type of the client object.
158 */
bart195e41f2009-02-15 11:34:57 +0000159Bool DRD_(clientobj_remove)(const Addr addr, const ObjType t)
barte7d58722008-02-28 19:08:04 +0000160{
bartbedfd232009-03-26 19:07:15 +0000161 DrdClientobj* p;
barte7d58722008-02-28 19:08:04 +0000162
bartbedfd232009-03-26 19:07:15 +0000163 p = VG_(OSetGen_Lookup)(s_clientobj_set, &addr);
164 tl_assert(p);
165 tl_assert(p->any.type == t);
166 return clientobj_remove_obj(p);
bartd2c5eae2009-02-21 15:27:04 +0000167}
168
bart1344d6c2009-02-21 16:17:50 +0000169/**
170 * Remove the information that was stored about the client object p.
171 *
172 * @note The order of operations below is important. The client object is
173 * removed from the client object set after the cleanup function has been
174 * called such that if the cleanup function can still use the function
175 * DRD_(clientobj_get_any)(). This happens e.g. in the function
176 * first_observed() in drd_error.c.
177 */
bartd2c5eae2009-02-21 15:27:04 +0000178static Bool clientobj_remove_obj(DrdClientobj* const p)
179{
bartbedfd232009-03-26 19:07:15 +0000180 tl_assert(p);
bartd2c5eae2009-02-21 15:27:04 +0000181
bartb92ff0f2011-10-08 08:29:29 +0000182 if (s_trace_clientobj) {
183 DRD_(trace_msg)("Removing client object 0x%lx of type %d\n", p->any.a1,
184 p->any.type);
bart9374ee32009-07-23 16:24:02 +0000185#if 0
186 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(),
187 VG_(clo_backtrace_size));
188#endif
bartbedfd232009-03-26 19:07:15 +0000189 }
bart72b751c2008-03-01 13:44:24 +0000190
bartbedfd232009-03-26 19:07:15 +0000191 tl_assert(p->any.cleanup);
192 (*p->any.cleanup)(p);
193 VG_(OSetGen_Remove)(s_clientobj_set, &p->any.a1);
194 VG_(OSetGen_FreeNode)(s_clientobj_set, p);
195 return True;
barte7d58722008-02-28 19:08:04 +0000196}
197
bart28be7aa2009-02-23 19:15:32 +0000198/**
199 * Clean up all client objects p for which their start address p->any.a1 fits
200 * inside the address range [ a1, a2 [.
201 *
202 * @note The implementation of this function relies on the fact that the
203 * data in s_clientobj_set is sorted on the start address of client objects.
204 */
bart195e41f2009-02-15 11:34:57 +0000205void DRD_(clientobj_stop_using_mem)(const Addr a1, const Addr a2)
barte7d58722008-02-28 19:08:04 +0000206{
bartbedfd232009-03-26 19:07:15 +0000207 Addr removed_at;
208 DrdClientobj* p;
bartb78312c2008-02-29 11:00:17 +0000209
bartbedfd232009-03-26 19:07:15 +0000210 tl_assert(s_clientobj_set);
bartfea64442008-03-30 16:55:40 +0000211
bartb878a732010-03-07 20:07:15 +0000212 if (! DRD_(range_contains_suppression_or_hbvar)(a1, a2))
bartbedfd232009-03-26 19:07:15 +0000213 return;
bartfea64442008-03-30 16:55:40 +0000214
bartbedfd232009-03-26 19:07:15 +0000215 VG_(OSetGen_ResetIterAt)(s_clientobj_set, &a1);
216 for ( ; (p = VG_(OSetGen_Next)(s_clientobj_set)) != 0 && p->any.a1 < a2; )
217 {
218 tl_assert(a1 <= p->any.a1);
219 removed_at = p->any.a1;
220 clientobj_remove_obj(p);
221 /*
222 * The above call removes an element from the oset and hence
223 * invalidates the iterator. Restore the iterator.
224 */
225 VG_(OSetGen_ResetIterAt)(s_clientobj_set, &removed_at);
226 }
barte7d58722008-02-28 19:08:04 +0000227}
228
bartd2c5eae2009-02-21 15:27:04 +0000229/**
230 * Delete the per-thread information stored in client objects for the
231 * specified thread.
232 */
233void DRD_(clientobj_delete_thread)(const DrdThreadId tid)
barte7d58722008-02-28 19:08:04 +0000234{
bartbedfd232009-03-26 19:07:15 +0000235 DrdClientobj *p;
barte7d58722008-02-28 19:08:04 +0000236
bartbedfd232009-03-26 19:07:15 +0000237 VG_(OSetGen_ResetIter)(s_clientobj_set);
238 for ( ; (p = VG_(OSetGen_Next)(s_clientobj_set)) != 0; )
239 {
240 if (p->any.delete_thread)
241 {
242 (*p->any.delete_thread)(p, tid);
243 }
244 }
barte7d58722008-02-28 19:08:04 +0000245}
246
bart195e41f2009-02-15 11:34:57 +0000247const char* DRD_(clientobj_type_name)(const ObjType t)
bart391d9dc2008-07-03 10:57:30 +0000248{
bartbedfd232009-03-26 19:07:15 +0000249 switch (t)
250 {
251 case ClientMutex: return "mutex";
252 case ClientCondvar: return "cond";
bart62cc2322010-03-07 10:54:21 +0000253 case ClientHbvar: return "order annotation";
bartbedfd232009-03-26 19:07:15 +0000254 case ClientSemaphore: return "semaphore";
255 case ClientBarrier: return "barrier";
256 case ClientRwlock: return "rwlock";
257 }
258 return "(unknown)";
bart391d9dc2008-07-03 10:57:30 +0000259}