blob: 4f305cb1ffa1ddeb1fe0764f4f0e049ae0004330 [file] [log] [blame]
bartbedfd232009-03-26 19:07:15 +00001/* -*- mode: C; c-basic-offset: 3; -*- */
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
bart86562bd2009-02-16 19:43:56 +00005 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
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"
27#include "drd_suppression.h"
28#include "pub_tool_basics.h"
29#include "pub_tool_libcassert.h"
30#include "pub_tool_libcbase.h"
bart72b751c2008-03-01 13:44:24 +000031#include "pub_tool_libcprint.h" // VG_(message)()
barte7d58722008-02-28 19:08:04 +000032#include "pub_tool_mallocfree.h"
bart72b751c2008-03-01 13:44:24 +000033#include "pub_tool_options.h" // VG_(clo_backtrace_size)
barte7d58722008-02-28 19:08:04 +000034#include "pub_tool_oset.h"
bart72b751c2008-03-01 13:44:24 +000035#include "pub_tool_stacktrace.h"
36#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
barte7d58722008-02-28 19:08:04 +000037
38
bart195e41f2009-02-15 11:34:57 +000039/* Local variables. */
barte7d58722008-02-28 19:08:04 +000040
bartd2c5eae2009-02-21 15:27:04 +000041static OSet* s_clientobj_set;
42static Bool s_trace_clientobj;
43
44
45/* Local functions. */
46
47static Bool clientobj_remove_obj(DrdClientobj* const p);
barte7d58722008-02-28 19:08:04 +000048
49
bart195e41f2009-02-15 11:34:57 +000050/* Function definitions. */
barte7d58722008-02-28 19:08:04 +000051
bart195e41f2009-02-15 11:34:57 +000052void DRD_(clientobj_set_trace)(const Bool trace)
bart72b751c2008-03-01 13:44:24 +000053{
bartbedfd232009-03-26 19:07:15 +000054 s_trace_clientobj = trace;
bart72b751c2008-03-01 13:44:24 +000055}
56
barte7d58722008-02-28 19:08:04 +000057/** Initialize the client object set. */
bart195e41f2009-02-15 11:34:57 +000058void DRD_(clientobj_init)(void)
barte7d58722008-02-28 19:08:04 +000059{
bartbedfd232009-03-26 19:07:15 +000060 tl_assert(s_clientobj_set == 0);
61 s_clientobj_set = VG_(OSetGen_Create)(0, 0, VG_(malloc),
62 "drd.clientobj.ci.1", VG_(free));
63 tl_assert(s_clientobj_set);
barte7d58722008-02-28 19:08:04 +000064}
65
bart195e41f2009-02-15 11:34:57 +000066/**
67 * Free the memory allocated for the client object set.
68 *
69 * @pre Client object set is empty.
barte7d58722008-02-28 19:08:04 +000070 */
bart195e41f2009-02-15 11:34:57 +000071void DRD_(clientobj_cleanup)(void)
barte7d58722008-02-28 19:08:04 +000072{
bartbedfd232009-03-26 19:07:15 +000073 tl_assert(s_clientobj_set);
74 tl_assert(VG_(OSetGen_Size)(s_clientobj_set) == 0);
75 VG_(OSetGen_Destroy)(s_clientobj_set);
76 s_clientobj_set = 0;
barte7d58722008-02-28 19:08:04 +000077}
78
bartd2c5eae2009-02-21 15:27:04 +000079/**
80 * Return the data associated with the client object at client address addr.
81 * Return 0 if there is no client object in the set with the specified start
82 * address.
bart391d9dc2008-07-03 10:57:30 +000083 */
bart195e41f2009-02-15 11:34:57 +000084DrdClientobj* DRD_(clientobj_get_any)(const Addr addr)
bart391d9dc2008-07-03 10:57:30 +000085{
bartbedfd232009-03-26 19:07:15 +000086 return VG_(OSetGen_Lookup)(s_clientobj_set, &addr);
bart391d9dc2008-07-03 10:57:30 +000087}
88
bart1344d6c2009-02-21 16:17:50 +000089/**
90 * Return the data associated with the client object at client address addr
91 * and that has object type t. Return 0 if there is no client object in the
92 * set with the specified start address.
barte7d58722008-02-28 19:08:04 +000093 */
bart195e41f2009-02-15 11:34:57 +000094DrdClientobj* DRD_(clientobj_get)(const Addr addr, const ObjType t)
barte7d58722008-02-28 19:08:04 +000095{
bartbedfd232009-03-26 19:07:15 +000096 DrdClientobj* p;
97 p = VG_(OSetGen_Lookup)(s_clientobj_set, &addr);
98 if (p && p->any.type == t)
99 return p;
100 return 0;
barte7d58722008-02-28 19:08:04 +0000101}
102
103/** Return true if and only if the address range of any client object overlaps
104 * with the specified address range.
105 */
bart195e41f2009-02-15 11:34:57 +0000106Bool DRD_(clientobj_present)(const Addr a1, const Addr a2)
barte7d58722008-02-28 19:08:04 +0000107{
bartbedfd232009-03-26 19:07:15 +0000108 DrdClientobj *p;
barte7d58722008-02-28 19:08:04 +0000109
bartbedfd232009-03-26 19:07:15 +0000110 tl_assert(a1 < a2);
111 VG_(OSetGen_ResetIter)(s_clientobj_set);
112 for ( ; (p = VG_(OSetGen_Next)(s_clientobj_set)) != 0; )
113 {
114 if (a1 <= p->any.a1 && p->any.a1 < a2)
115 {
bart31b983d2010-02-21 14:52:59 +0000116 return True;
bartbedfd232009-03-26 19:07:15 +0000117 }
118 }
119 return False;
barte7d58722008-02-28 19:08:04 +0000120}
121
bartbedfd232009-03-26 19:07:15 +0000122/**
123 * Add state information for the client object at client address addr and
124 * of type t. Suppress data race reports on the address range [addr,addr+size[.
125 *
126 * @pre No other client object is present in the address range [addr,addr+size[.
barte7d58722008-02-28 19:08:04 +0000127 */
bart195e41f2009-02-15 11:34:57 +0000128DrdClientobj* DRD_(clientobj_add)(const Addr a1, const ObjType t)
barte7d58722008-02-28 19:08:04 +0000129{
bartbedfd232009-03-26 19:07:15 +0000130 DrdClientobj* p;
barte7d58722008-02-28 19:08:04 +0000131
bartbedfd232009-03-26 19:07:15 +0000132 tl_assert(! DRD_(clientobj_present)(a1, a1 + 1));
133 tl_assert(VG_(OSetGen_Lookup)(s_clientobj_set, &a1) == 0);
bartbcbd7482008-02-29 19:19:39 +0000134
bartbedfd232009-03-26 19:07:15 +0000135 if (s_trace_clientobj)
136 {
sewardj1e29ebc2009-07-15 14:49:17 +0000137 VG_(message)(Vg_UserMsg, "Adding client object 0x%lx of type %d\n", a1, t);
bartbedfd232009-03-26 19:07:15 +0000138 }
bart72b751c2008-03-01 13:44:24 +0000139
bartbedfd232009-03-26 19:07:15 +0000140 p = VG_(OSetGen_AllocNode)(s_clientobj_set, sizeof(*p));
141 VG_(memset)(p, 0, sizeof(*p));
142 p->any.a1 = a1;
143 p->any.type = t;
144 p->any.first_observed_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
145 VG_(OSetGen_Insert)(s_clientobj_set, p);
146 tl_assert(VG_(OSetGen_Lookup)(s_clientobj_set, &a1) == p);
147 DRD_(start_suppression)(a1, a1 + 1, "clientobj");
148 return p;
barte7d58722008-02-28 19:08:04 +0000149}
150
bart1344d6c2009-02-21 16:17:50 +0000151/**
152 * Remove the information that was stored about the client object.
153 *
154 * @param[in] addr Address of the client object in the client address space.
155 * @param[in] t Type of the client object.
156 */
bart195e41f2009-02-15 11:34:57 +0000157Bool DRD_(clientobj_remove)(const Addr addr, const ObjType t)
barte7d58722008-02-28 19:08:04 +0000158{
bartbedfd232009-03-26 19:07:15 +0000159 DrdClientobj* p;
barte7d58722008-02-28 19:08:04 +0000160
bartbedfd232009-03-26 19:07:15 +0000161 p = VG_(OSetGen_Lookup)(s_clientobj_set, &addr);
162 tl_assert(p);
163 tl_assert(p->any.type == t);
164 return clientobj_remove_obj(p);
bartd2c5eae2009-02-21 15:27:04 +0000165}
166
bart1344d6c2009-02-21 16:17:50 +0000167/**
168 * Remove the information that was stored about the client object p.
169 *
170 * @note The order of operations below is important. The client object is
171 * removed from the client object set after the cleanup function has been
172 * called such that if the cleanup function can still use the function
173 * DRD_(clientobj_get_any)(). This happens e.g. in the function
174 * first_observed() in drd_error.c.
175 */
bartd2c5eae2009-02-21 15:27:04 +0000176static Bool clientobj_remove_obj(DrdClientobj* const p)
177{
bartbedfd232009-03-26 19:07:15 +0000178 tl_assert(p);
bartd2c5eae2009-02-21 15:27:04 +0000179
bartbedfd232009-03-26 19:07:15 +0000180 if (s_trace_clientobj)
181 {
sewardj1e29ebc2009-07-15 14:49:17 +0000182 VG_(message)(Vg_UserMsg, "Removing client object 0x%lx of type %d\n",
bartbedfd232009-03-26 19:07:15 +0000183 p->any.a1, p->any.type);
bart9374ee32009-07-23 16:24:02 +0000184#if 0
185 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(),
186 VG_(clo_backtrace_size));
187#endif
bartbedfd232009-03-26 19:07:15 +0000188 }
bart72b751c2008-03-01 13:44:24 +0000189
bartbedfd232009-03-26 19:07:15 +0000190 tl_assert(p->any.cleanup);
191 (*p->any.cleanup)(p);
192 VG_(OSetGen_Remove)(s_clientobj_set, &p->any.a1);
193 VG_(OSetGen_FreeNode)(s_clientobj_set, p);
194 return True;
barte7d58722008-02-28 19:08:04 +0000195}
196
bart28be7aa2009-02-23 19:15:32 +0000197/**
198 * Clean up all client objects p for which their start address p->any.a1 fits
199 * inside the address range [ a1, a2 [.
200 *
201 * @note The implementation of this function relies on the fact that the
202 * data in s_clientobj_set is sorted on the start address of client objects.
203 */
bart195e41f2009-02-15 11:34:57 +0000204void DRD_(clientobj_stop_using_mem)(const Addr a1, const Addr a2)
barte7d58722008-02-28 19:08:04 +0000205{
bartbedfd232009-03-26 19:07:15 +0000206 Addr removed_at;
207 DrdClientobj* p;
bartb78312c2008-02-29 11:00:17 +0000208
bartbedfd232009-03-26 19:07:15 +0000209 tl_assert(s_clientobj_set);
bartfea64442008-03-30 16:55:40 +0000210
bartbedfd232009-03-26 19:07:15 +0000211 if (! DRD_(is_any_suppressed)(a1, a2))
212 return;
bartfea64442008-03-30 16:55:40 +0000213
bartbedfd232009-03-26 19:07:15 +0000214 VG_(OSetGen_ResetIterAt)(s_clientobj_set, &a1);
215 for ( ; (p = VG_(OSetGen_Next)(s_clientobj_set)) != 0 && p->any.a1 < a2; )
216 {
217 tl_assert(a1 <= p->any.a1);
218 removed_at = p->any.a1;
219 clientobj_remove_obj(p);
220 /*
221 * The above call removes an element from the oset and hence
222 * invalidates the iterator. Restore the iterator.
223 */
224 VG_(OSetGen_ResetIterAt)(s_clientobj_set, &removed_at);
225 }
barte7d58722008-02-28 19:08:04 +0000226}
227
bartd2c5eae2009-02-21 15:27:04 +0000228/**
229 * Delete the per-thread information stored in client objects for the
230 * specified thread.
231 */
232void DRD_(clientobj_delete_thread)(const DrdThreadId tid)
barte7d58722008-02-28 19:08:04 +0000233{
bartbedfd232009-03-26 19:07:15 +0000234 DrdClientobj *p;
barte7d58722008-02-28 19:08:04 +0000235
bartbedfd232009-03-26 19:07:15 +0000236 VG_(OSetGen_ResetIter)(s_clientobj_set);
237 for ( ; (p = VG_(OSetGen_Next)(s_clientobj_set)) != 0; )
238 {
239 if (p->any.delete_thread)
240 {
241 (*p->any.delete_thread)(p, tid);
242 }
243 }
barte7d58722008-02-28 19:08:04 +0000244}
245
bart195e41f2009-02-15 11:34:57 +0000246const char* DRD_(clientobj_type_name)(const ObjType t)
bart391d9dc2008-07-03 10:57:30 +0000247{
bartbedfd232009-03-26 19:07:15 +0000248 switch (t)
249 {
250 case ClientMutex: return "mutex";
251 case ClientCondvar: return "cond";
252 case ClientSemaphore: return "semaphore";
253 case ClientBarrier: return "barrier";
254 case ClientRwlock: return "rwlock";
255 }
256 return "(unknown)";
bart391d9dc2008-07-03 10:57:30 +0000257}