blob: 6c7620d8a20cba7a404c494305e7e69b9641391f [file] [log] [blame]
barte7d58722008-02-28 19:08:04 +00001/*
bart86562bd2009-02-16 19:43:56 +00002 This file is part of drd, a thread error detector.
barte7d58722008-02-28 19:08:04 +00003
bart86562bd2009-02-16 19:43:56 +00004 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
barte7d58722008-02-28 19:08:04 +00005
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_clientobj.h"
26#include "drd_suppression.h"
27#include "pub_tool_basics.h"
28#include "pub_tool_libcassert.h"
29#include "pub_tool_libcbase.h"
bart72b751c2008-03-01 13:44:24 +000030#include "pub_tool_libcprint.h" // VG_(message)()
barte7d58722008-02-28 19:08:04 +000031#include "pub_tool_mallocfree.h"
bart72b751c2008-03-01 13:44:24 +000032#include "pub_tool_options.h" // VG_(clo_backtrace_size)
barte7d58722008-02-28 19:08:04 +000033#include "pub_tool_oset.h"
bart72b751c2008-03-01 13:44:24 +000034#include "pub_tool_stacktrace.h"
35#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
barte7d58722008-02-28 19:08:04 +000036
37
bart195e41f2009-02-15 11:34:57 +000038/* Local variables. */
barte7d58722008-02-28 19:08:04 +000039
bartd2c5eae2009-02-21 15:27:04 +000040static OSet* s_clientobj_set;
41static Bool s_trace_clientobj;
42
43
44/* Local functions. */
45
46static Bool clientobj_remove_obj(DrdClientobj* const p);
barte7d58722008-02-28 19:08:04 +000047
48
bart195e41f2009-02-15 11:34:57 +000049/* Function definitions. */
barte7d58722008-02-28 19:08:04 +000050
bart195e41f2009-02-15 11:34:57 +000051void DRD_(clientobj_set_trace)(const Bool trace)
bart72b751c2008-03-01 13:44:24 +000052{
bartd2c5eae2009-02-21 15:27:04 +000053 s_trace_clientobj = trace;
bart72b751c2008-03-01 13:44:24 +000054}
55
barte7d58722008-02-28 19:08:04 +000056/** Initialize the client object set. */
bart195e41f2009-02-15 11:34:57 +000057void DRD_(clientobj_init)(void)
barte7d58722008-02-28 19:08:04 +000058{
bartd2c5eae2009-02-21 15:27:04 +000059 tl_assert(s_clientobj_set == 0);
60 s_clientobj_set = VG_(OSetGen_Create)(0, 0, VG_(malloc),
61 "drd.clientobj.ci.1", VG_(free));
62 tl_assert(s_clientobj_set);
barte7d58722008-02-28 19:08:04 +000063}
64
bart195e41f2009-02-15 11:34:57 +000065/**
66 * Free the memory allocated for the client object set.
67 *
68 * @pre Client object set is empty.
barte7d58722008-02-28 19:08:04 +000069 */
bart195e41f2009-02-15 11:34:57 +000070void DRD_(clientobj_cleanup)(void)
barte7d58722008-02-28 19:08:04 +000071{
bartd2c5eae2009-02-21 15:27:04 +000072 tl_assert(s_clientobj_set);
73 tl_assert(VG_(OSetGen_Size)(s_clientobj_set) == 0);
74 VG_(OSetGen_Destroy)(s_clientobj_set);
75 s_clientobj_set = 0;
barte7d58722008-02-28 19:08:04 +000076}
77
bartd2c5eae2009-02-21 15:27:04 +000078/**
79 * Return the data associated with the client object at client address addr.
80 * Return 0 if there is no client object in the set with the specified start
81 * address.
bart391d9dc2008-07-03 10:57:30 +000082 */
bart195e41f2009-02-15 11:34:57 +000083DrdClientobj* DRD_(clientobj_get_any)(const Addr addr)
bart391d9dc2008-07-03 10:57:30 +000084{
bartd2c5eae2009-02-21 15:27:04 +000085 return VG_(OSetGen_Lookup)(s_clientobj_set, &addr);
bart391d9dc2008-07-03 10:57:30 +000086}
87
bart1344d6c2009-02-21 16:17:50 +000088/**
89 * Return the data associated with the client object at client address addr
90 * and that has object type t. Return 0 if there is no client object in the
91 * set with the specified start address.
barte7d58722008-02-28 19:08:04 +000092 */
bart195e41f2009-02-15 11:34:57 +000093DrdClientobj* DRD_(clientobj_get)(const Addr addr, const ObjType t)
barte7d58722008-02-28 19:08:04 +000094{
95 DrdClientobj* p;
bartd2c5eae2009-02-21 15:27:04 +000096 p = VG_(OSetGen_Lookup)(s_clientobj_set, &addr);
barte7d58722008-02-28 19:08:04 +000097 if (p && p->any.type == t)
98 return p;
99 return 0;
100}
101
102/** Return true if and only if the address range of any client object overlaps
103 * with the specified address range.
104 */
bart195e41f2009-02-15 11:34:57 +0000105Bool DRD_(clientobj_present)(const Addr a1, const Addr a2)
barte7d58722008-02-28 19:08:04 +0000106{
107 DrdClientobj *p;
108
109 tl_assert(a1 < a2);
bartd2c5eae2009-02-21 15:27:04 +0000110 VG_(OSetGen_ResetIter)(s_clientobj_set);
111 for ( ; (p = VG_(OSetGen_Next)(s_clientobj_set)) != 0; )
barte7d58722008-02-28 19:08:04 +0000112 {
bart0268dfa2008-03-11 20:10:21 +0000113 if (a1 <= p->any.a1 && p->any.a1 < a2)
barte7d58722008-02-28 19:08:04 +0000114 {
115 return True;
116 }
117 }
118 return False;
119}
120
121/** Add state information for the client object at client address addr and
122 * of type t. Suppress data race reports on the address range [addr,addr+size[.
123 * @pre No other client object is present in the address range [addr,addr+size[.
124 */
bart195e41f2009-02-15 11:34:57 +0000125DrdClientobj* DRD_(clientobj_add)(const Addr a1, const ObjType t)
barte7d58722008-02-28 19:08:04 +0000126{
127 DrdClientobj* p;
128
bart195e41f2009-02-15 11:34:57 +0000129 tl_assert(! DRD_(clientobj_present)(a1, a1 + 1));
bartd2c5eae2009-02-21 15:27:04 +0000130 tl_assert(VG_(OSetGen_Lookup)(s_clientobj_set, &a1) == 0);
bartbcbd7482008-02-29 19:19:39 +0000131
bartd2c5eae2009-02-21 15:27:04 +0000132 if (s_trace_clientobj)
bart72b751c2008-03-01 13:44:24 +0000133 {
134 VG_(message)(Vg_UserMsg, "Adding client object 0x%lx of type %d", a1, t);
135 }
136
bartd2c5eae2009-02-21 15:27:04 +0000137 p = VG_(OSetGen_AllocNode)(s_clientobj_set, sizeof(*p));
barte7d58722008-02-28 19:08:04 +0000138 VG_(memset)(p, 0, sizeof(*p));
139 p->any.a1 = a1;
barte7d58722008-02-28 19:08:04 +0000140 p->any.type = t;
bart391d9dc2008-07-03 10:57:30 +0000141 p->any.first_observed_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
bartd2c5eae2009-02-21 15:27:04 +0000142 VG_(OSetGen_Insert)(s_clientobj_set, p);
143 tl_assert(VG_(OSetGen_Lookup)(s_clientobj_set, &a1) == p);
bart1335ecc2009-02-14 16:10:53 +0000144 DRD_(start_suppression)(a1, a1 + 1, "clientobj");
barte7d58722008-02-28 19:08:04 +0000145 return p;
146}
147
bart1344d6c2009-02-21 16:17:50 +0000148/**
149 * Remove the information that was stored about the client object.
150 *
151 * @param[in] addr Address of the client object in the client address space.
152 * @param[in] t Type of the client object.
153 */
bart195e41f2009-02-15 11:34:57 +0000154Bool DRD_(clientobj_remove)(const Addr addr, const ObjType t)
barte7d58722008-02-28 19:08:04 +0000155{
156 DrdClientobj* p;
157
bartd2c5eae2009-02-21 15:27:04 +0000158 p = VG_(OSetGen_Lookup)(s_clientobj_set, &addr);
159 tl_assert(p);
160 tl_assert(p->any.type == t);
161 return clientobj_remove_obj(p);
162}
163
bart1344d6c2009-02-21 16:17:50 +0000164/**
165 * Remove the information that was stored about the client object p.
166 *
167 * @note The order of operations below is important. The client object is
168 * removed from the client object set after the cleanup function has been
169 * called such that if the cleanup function can still use the function
170 * DRD_(clientobj_get_any)(). This happens e.g. in the function
171 * first_observed() in drd_error.c.
172 */
bartd2c5eae2009-02-21 15:27:04 +0000173static Bool clientobj_remove_obj(DrdClientobj* const p)
174{
bart1344d6c2009-02-21 16:17:50 +0000175 tl_assert(p);
bartd2c5eae2009-02-21 15:27:04 +0000176
177 if (s_trace_clientobj)
bart72b751c2008-03-01 13:44:24 +0000178 {
179 VG_(message)(Vg_UserMsg, "Removing client object 0x%lx of type %d",
bartd2c5eae2009-02-21 15:27:04 +0000180 p->any.a1, p->any.type);
bart72b751c2008-03-01 13:44:24 +0000181 }
182
bartd2c5eae2009-02-21 15:27:04 +0000183 tl_assert(p->any.cleanup);
184 (*p->any.cleanup)(p);
bart1344d6c2009-02-21 16:17:50 +0000185 VG_(OSetGen_Remove)(s_clientobj_set, &p->any.a1);
bartd2c5eae2009-02-21 15:27:04 +0000186 VG_(OSetGen_FreeNode)(s_clientobj_set, p);
187 return True;
barte7d58722008-02-28 19:08:04 +0000188}
189
bart195e41f2009-02-15 11:34:57 +0000190void DRD_(clientobj_stop_using_mem)(const Addr a1, const Addr a2)
barte7d58722008-02-28 19:08:04 +0000191{
bartb78312c2008-02-29 11:00:17 +0000192 Addr removed_at;
barte7d58722008-02-28 19:08:04 +0000193 DrdClientobj* p;
bartb78312c2008-02-29 11:00:17 +0000194
bartd2c5eae2009-02-21 15:27:04 +0000195 tl_assert(s_clientobj_set);
bartfea64442008-03-30 16:55:40 +0000196
bart1335ecc2009-02-14 16:10:53 +0000197 if (! DRD_(is_any_suppressed)(a1, a2))
bartfea64442008-03-30 16:55:40 +0000198 return;
199
bartd2c5eae2009-02-21 15:27:04 +0000200 VG_(OSetGen_ResetIter)(s_clientobj_set);
201 p = VG_(OSetGen_Next)(s_clientobj_set);
bartb78312c2008-02-29 11:00:17 +0000202 for ( ; p != 0; )
barte7d58722008-02-28 19:08:04 +0000203 {
bart0268dfa2008-03-11 20:10:21 +0000204 if (a1 <= p->any.a1 && p->any.a1 < a2)
barte7d58722008-02-28 19:08:04 +0000205 {
bartb78312c2008-02-29 11:00:17 +0000206 removed_at = p->any.a1;
bart1344d6c2009-02-21 16:17:50 +0000207 clientobj_remove_obj(p);
bartbcbd7482008-02-29 19:19:39 +0000208 /* The above call removes an element from the oset and hence */
209 /* invalidates the iterator. Set the iterator back. */
bart1344d6c2009-02-21 16:17:50 +0000210 VG_(OSetGen_ResetIterAt)(s_clientobj_set, &removed_at);
bartb78312c2008-02-29 11:00:17 +0000211 }
212 else
213 {
bartd2c5eae2009-02-21 15:27:04 +0000214 p = VG_(OSetGen_Next)(s_clientobj_set);
barte7d58722008-02-28 19:08:04 +0000215 }
216 }
217}
218
bartd2c5eae2009-02-21 15:27:04 +0000219/**
220 * Delete the per-thread information stored in client objects for the
221 * specified thread.
222 */
223void DRD_(clientobj_delete_thread)(const DrdThreadId tid)
barte7d58722008-02-28 19:08:04 +0000224{
bartd2c5eae2009-02-21 15:27:04 +0000225 DrdClientobj *p;
barte7d58722008-02-28 19:08:04 +0000226
bartd2c5eae2009-02-21 15:27:04 +0000227 VG_(OSetGen_ResetIter)(s_clientobj_set);
228 for ( ; (p = VG_(OSetGen_Next)(s_clientobj_set)) != 0; )
229 {
230 if (p->any.delete_thread)
231 {
232 (*p->any.delete_thread)(p, tid);
233 }
234 }
barte7d58722008-02-28 19:08:04 +0000235}
236
bart195e41f2009-02-15 11:34:57 +0000237const char* DRD_(clientobj_type_name)(const ObjType t)
bart391d9dc2008-07-03 10:57:30 +0000238{
239 switch (t)
240 {
241 case ClientMutex: return "mutex";
242 case ClientCondvar: return "cond";
243 case ClientSemaphore: return "semaphore";
244 case ClientBarrier: return "barrier";
245 case ClientRwlock: return "rwlock";
246 }
247 return "(unknown)";
248}