blob: 768079aa2824f36459e1d3d5e9b6887554fb8e92 [file] [log] [blame]
barte7d58722008-02-28 19:08:04 +00001/*
2 This file is part of drd, a data race detector.
3
4 Copyright (C) 2006-2008 Bart Van Assche
5 bart.vanassche@gmail.com
6
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
39// Local variables.
40
41static OSet* s_clientobj;
bart72b751c2008-03-01 13:44:24 +000042static Bool s_trace_clientobj;
barte7d58722008-02-28 19:08:04 +000043
44
45// Function definitions.
46
bart72b751c2008-03-01 13:44:24 +000047void clientobj_set_trace(const Bool trace)
48{
49 s_trace_clientobj = trace;
50}
51
barte7d58722008-02-28 19:08:04 +000052/** Initialize the client object set. */
bart72b751c2008-03-01 13:44:24 +000053void clientobj_init(void)
barte7d58722008-02-28 19:08:04 +000054{
55 tl_assert(s_clientobj == 0);
sewardj9c606bd2008-09-18 18:12:50 +000056 s_clientobj = VG_(OSetGen_Create)(0, 0, VG_(malloc), "drd.clientobj.ci.1",
57 VG_(free));
barte7d58722008-02-28 19:08:04 +000058 tl_assert(s_clientobj);
59}
60
61/** Free the memory allocated for the client object set.
62 * @pre Client object set is empty.
63 */
bart72b751c2008-03-01 13:44:24 +000064void clientobj_cleanup(void)
barte7d58722008-02-28 19:08:04 +000065{
66 tl_assert(s_clientobj);
67 tl_assert(VG_(OSetGen_Size)(s_clientobj) == 0);
68 VG_(OSetGen_Destroy)(s_clientobj);
69 s_clientobj = 0;
70}
71
bart391d9dc2008-07-03 10:57:30 +000072/** Return the data associated with the client object at client address addr.
73 * Return 0 if there is no client object in the set with the specified start
74 * address.
75 */
76DrdClientobj* clientobj_get_any(const Addr addr)
77{
78 return VG_(OSetGen_Lookup)(s_clientobj, &addr);
79}
80
barte7d58722008-02-28 19:08:04 +000081/** Return the data associated with the client object at client address addr
82 * and that has object type t. Return 0 if there is no client object in the
83 * set with the specified start address.
84 */
bart72b751c2008-03-01 13:44:24 +000085DrdClientobj* clientobj_get(const Addr addr, const ObjType t)
barte7d58722008-02-28 19:08:04 +000086{
87 DrdClientobj* p;
88 p = VG_(OSetGen_Lookup)(s_clientobj, &addr);
89 if (p && p->any.type == t)
90 return p;
91 return 0;
92}
93
94/** Return true if and only if the address range of any client object overlaps
95 * with the specified address range.
96 */
bart72b751c2008-03-01 13:44:24 +000097Bool clientobj_present(const Addr a1, const Addr a2)
barte7d58722008-02-28 19:08:04 +000098{
99 DrdClientobj *p;
100
101 tl_assert(a1 < a2);
102 VG_(OSetGen_ResetIter)(s_clientobj);
103 for ( ; (p = VG_(OSetGen_Next)(s_clientobj)) != 0; )
104 {
bart0268dfa2008-03-11 20:10:21 +0000105 if (a1 <= p->any.a1 && p->any.a1 < a2)
barte7d58722008-02-28 19:08:04 +0000106 {
107 return True;
108 }
109 }
110 return False;
111}
112
113/** Add state information for the client object at client address addr and
114 * of type t. Suppress data race reports on the address range [addr,addr+size[.
115 * @pre No other client object is present in the address range [addr,addr+size[.
116 */
117DrdClientobj*
bart0268dfa2008-03-11 20:10:21 +0000118clientobj_add(const Addr a1, const ObjType t)
barte7d58722008-02-28 19:08:04 +0000119{
120 DrdClientobj* p;
121
bart0268dfa2008-03-11 20:10:21 +0000122 tl_assert(! clientobj_present(a1, a1 + 1));
barte7d58722008-02-28 19:08:04 +0000123 tl_assert(VG_(OSetGen_Lookup)(s_clientobj, &a1) == 0);
bartbcbd7482008-02-29 19:19:39 +0000124
bart72b751c2008-03-01 13:44:24 +0000125 if (s_trace_clientobj)
126 {
127 VG_(message)(Vg_UserMsg, "Adding client object 0x%lx of type %d", a1, t);
128 }
129
barte7d58722008-02-28 19:08:04 +0000130 p = VG_(OSetGen_AllocNode)(s_clientobj, sizeof(*p));
131 VG_(memset)(p, 0, sizeof(*p));
132 p->any.a1 = a1;
barte7d58722008-02-28 19:08:04 +0000133 p->any.type = t;
bart391d9dc2008-07-03 10:57:30 +0000134 p->any.first_observed_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
barte7d58722008-02-28 19:08:04 +0000135 VG_(OSetGen_Insert)(s_clientobj, p);
136 tl_assert(VG_(OSetGen_Lookup)(s_clientobj, &a1) == p);
bartfea64442008-03-30 16:55:40 +0000137 drd_start_suppression(a1, a1 + 1, "clientobj");
barte7d58722008-02-28 19:08:04 +0000138 return p;
139}
140
bart72b751c2008-03-01 13:44:24 +0000141Bool clientobj_remove(const Addr addr, const ObjType t)
barte7d58722008-02-28 19:08:04 +0000142{
143 DrdClientobj* p;
144
bart72b751c2008-03-01 13:44:24 +0000145 if (s_trace_clientobj)
146 {
147 VG_(message)(Vg_UserMsg, "Removing client object 0x%lx of type %d",
148 addr, t);
149#if 0
150 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(),
151 VG_(clo_backtrace_size));
152#endif
153 }
154
bart28230a32008-02-29 17:27:03 +0000155 p = VG_(OSetGen_Lookup)(s_clientobj, &addr);
156 tl_assert(p->any.type == t);
barte7d58722008-02-28 19:08:04 +0000157 p = VG_(OSetGen_Remove)(s_clientobj, &addr);
158 if (p)
159 {
barte7d58722008-02-28 19:08:04 +0000160 tl_assert(VG_(OSetGen_Lookup)(s_clientobj, &addr) == 0);
barte7d58722008-02-28 19:08:04 +0000161 tl_assert(p->any.cleanup);
162 (*p->any.cleanup)(p);
163 VG_(OSetGen_FreeNode)(s_clientobj, p);
164 return True;
165 }
166 return False;
167}
168
bart72b751c2008-03-01 13:44:24 +0000169void clientobj_stop_using_mem(const Addr a1, const Addr a2)
barte7d58722008-02-28 19:08:04 +0000170{
bartb78312c2008-02-29 11:00:17 +0000171 Addr removed_at;
barte7d58722008-02-28 19:08:04 +0000172 DrdClientobj* p;
bartb78312c2008-02-29 11:00:17 +0000173
barte7d58722008-02-28 19:08:04 +0000174 tl_assert(s_clientobj);
bartfea64442008-03-30 16:55:40 +0000175
176 if (! drd_is_any_suppressed(a1, a2))
177 return;
178
barte7d58722008-02-28 19:08:04 +0000179 VG_(OSetGen_ResetIter)(s_clientobj);
bartb78312c2008-02-29 11:00:17 +0000180 p = VG_(OSetGen_Next)(s_clientobj);
181 for ( ; p != 0; )
barte7d58722008-02-28 19:08:04 +0000182 {
bart0268dfa2008-03-11 20:10:21 +0000183 if (a1 <= p->any.a1 && p->any.a1 < a2)
barte7d58722008-02-28 19:08:04 +0000184 {
bartb78312c2008-02-29 11:00:17 +0000185 removed_at = p->any.a1;
bart72b751c2008-03-01 13:44:24 +0000186 clientobj_remove(p->any.a1, p->any.type);
bartbcbd7482008-02-29 19:19:39 +0000187 /* The above call removes an element from the oset and hence */
188 /* invalidates the iterator. Set the iterator back. */
bartb78312c2008-02-29 11:00:17 +0000189 VG_(OSetGen_ResetIter)(s_clientobj);
190 while ((p = VG_(OSetGen_Next)(s_clientobj)) != 0
191 && p->any.a1 <= removed_at)
192 { }
193 }
194 else
195 {
196 p = VG_(OSetGen_Next)(s_clientobj);
barte7d58722008-02-28 19:08:04 +0000197 }
198 }
199}
200
bart72b751c2008-03-01 13:44:24 +0000201void clientobj_resetiter(void)
barte7d58722008-02-28 19:08:04 +0000202{
203 VG_(OSetGen_ResetIter)(s_clientobj);
204}
205
bart72b751c2008-03-01 13:44:24 +0000206DrdClientobj* clientobj_next(const ObjType t)
barte7d58722008-02-28 19:08:04 +0000207{
208 DrdClientobj* p;
209 while ((p = VG_(OSetGen_Next)(s_clientobj)) != 0 && p->any.type != t)
210 ;
211 return p;
212}
213
bart391d9dc2008-07-03 10:57:30 +0000214const char* clientobj_type_name(const ObjType t)
215{
216 switch (t)
217 {
218 case ClientMutex: return "mutex";
219 case ClientCondvar: return "cond";
220 case ClientSemaphore: return "semaphore";
221 case ClientBarrier: return "barrier";
222 case ClientRwlock: return "rwlock";
223 }
224 return "(unknown)";
225}