bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 1 | /* |
| 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" |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 31 | #include "pub_tool_libcprint.h" // VG_(message)() |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 32 | #include "pub_tool_mallocfree.h" |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 33 | #include "pub_tool_options.h" // VG_(clo_backtrace_size) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 34 | #include "pub_tool_oset.h" |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 35 | #include "pub_tool_stacktrace.h" |
| 36 | #include "pub_tool_threadstate.h" // VG_(get_running_tid)() |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | // Local variables. |
| 40 | |
| 41 | static OSet* s_clientobj; |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 42 | static Bool s_trace_clientobj; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | // Function definitions. |
| 46 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 47 | void clientobj_set_trace(const Bool trace) |
| 48 | { |
| 49 | s_trace_clientobj = trace; |
| 50 | } |
| 51 | |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 52 | /** Initialize the client object set. */ |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 53 | void clientobj_init(void) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 54 | { |
| 55 | tl_assert(s_clientobj == 0); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame^] | 56 | s_clientobj = VG_(OSetGen_Create)(0, 0, VG_(malloc), "drd.clientobj.ci.1", |
| 57 | VG_(free)); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 58 | tl_assert(s_clientobj); |
| 59 | } |
| 60 | |
| 61 | /** Free the memory allocated for the client object set. |
| 62 | * @pre Client object set is empty. |
| 63 | */ |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 64 | void clientobj_cleanup(void) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 65 | { |
| 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 | |
bart | 391d9dc | 2008-07-03 10:57:30 +0000 | [diff] [blame] | 72 | /** 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 | */ |
| 76 | DrdClientobj* clientobj_get_any(const Addr addr) |
| 77 | { |
| 78 | return VG_(OSetGen_Lookup)(s_clientobj, &addr); |
| 79 | } |
| 80 | |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 81 | /** 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 | */ |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 85 | DrdClientobj* clientobj_get(const Addr addr, const ObjType t) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 86 | { |
| 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 | */ |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 97 | Bool clientobj_present(const Addr a1, const Addr a2) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 98 | { |
| 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 | { |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 105 | if (a1 <= p->any.a1 && p->any.a1 < a2) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 106 | { |
| 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 | */ |
| 117 | DrdClientobj* |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 118 | clientobj_add(const Addr a1, const ObjType t) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 119 | { |
| 120 | DrdClientobj* p; |
| 121 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 122 | tl_assert(! clientobj_present(a1, a1 + 1)); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 123 | tl_assert(VG_(OSetGen_Lookup)(s_clientobj, &a1) == 0); |
bart | bcbd748 | 2008-02-29 19:19:39 +0000 | [diff] [blame] | 124 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 125 | if (s_trace_clientobj) |
| 126 | { |
| 127 | VG_(message)(Vg_UserMsg, "Adding client object 0x%lx of type %d", a1, t); |
| 128 | } |
| 129 | |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 130 | p = VG_(OSetGen_AllocNode)(s_clientobj, sizeof(*p)); |
| 131 | VG_(memset)(p, 0, sizeof(*p)); |
| 132 | p->any.a1 = a1; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 133 | p->any.type = t; |
bart | 391d9dc | 2008-07-03 10:57:30 +0000 | [diff] [blame] | 134 | p->any.first_observed_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 135 | VG_(OSetGen_Insert)(s_clientobj, p); |
| 136 | tl_assert(VG_(OSetGen_Lookup)(s_clientobj, &a1) == p); |
bart | fea6444 | 2008-03-30 16:55:40 +0000 | [diff] [blame] | 137 | drd_start_suppression(a1, a1 + 1, "clientobj"); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 138 | return p; |
| 139 | } |
| 140 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 141 | Bool clientobj_remove(const Addr addr, const ObjType t) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 142 | { |
| 143 | DrdClientobj* p; |
| 144 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 145 | 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 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 155 | p = VG_(OSetGen_Lookup)(s_clientobj, &addr); |
| 156 | tl_assert(p->any.type == t); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 157 | p = VG_(OSetGen_Remove)(s_clientobj, &addr); |
| 158 | if (p) |
| 159 | { |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 160 | tl_assert(VG_(OSetGen_Lookup)(s_clientobj, &addr) == 0); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 161 | 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 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 169 | void clientobj_stop_using_mem(const Addr a1, const Addr a2) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 170 | { |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 171 | Addr removed_at; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 172 | DrdClientobj* p; |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 173 | |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 174 | tl_assert(s_clientobj); |
bart | fea6444 | 2008-03-30 16:55:40 +0000 | [diff] [blame] | 175 | |
| 176 | if (! drd_is_any_suppressed(a1, a2)) |
| 177 | return; |
| 178 | |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 179 | VG_(OSetGen_ResetIter)(s_clientobj); |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 180 | p = VG_(OSetGen_Next)(s_clientobj); |
| 181 | for ( ; p != 0; ) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 182 | { |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 183 | if (a1 <= p->any.a1 && p->any.a1 < a2) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 184 | { |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 185 | removed_at = p->any.a1; |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 186 | clientobj_remove(p->any.a1, p->any.type); |
bart | bcbd748 | 2008-02-29 19:19:39 +0000 | [diff] [blame] | 187 | /* The above call removes an element from the oset and hence */ |
| 188 | /* invalidates the iterator. Set the iterator back. */ |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 189 | 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); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 201 | void clientobj_resetiter(void) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 202 | { |
| 203 | VG_(OSetGen_ResetIter)(s_clientobj); |
| 204 | } |
| 205 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 206 | DrdClientobj* clientobj_next(const ObjType t) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 207 | { |
| 208 | DrdClientobj* p; |
| 209 | while ((p = VG_(OSetGen_Next)(s_clientobj)) != 0 && p->any.type != t) |
| 210 | ; |
| 211 | return p; |
| 212 | } |
| 213 | |
bart | 391d9dc | 2008-07-03 10:57:30 +0000 | [diff] [blame] | 214 | const 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 | } |