bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 1 | /* |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 2 | This file is part of drd, a thread error detector. |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 3 | |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>. |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 5 | |
| 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" |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 30 | #include "pub_tool_libcprint.h" // VG_(message)() |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 31 | #include "pub_tool_mallocfree.h" |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 32 | #include "pub_tool_options.h" // VG_(clo_backtrace_size) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 33 | #include "pub_tool_oset.h" |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 34 | #include "pub_tool_stacktrace.h" |
| 35 | #include "pub_tool_threadstate.h" // VG_(get_running_tid)() |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 36 | |
| 37 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 38 | /* Local variables. */ |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 39 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 40 | static OSet* s_clientobj_set; |
| 41 | static Bool s_trace_clientobj; |
| 42 | |
| 43 | |
| 44 | /* Local functions. */ |
| 45 | |
| 46 | static Bool clientobj_remove_obj(DrdClientobj* const p); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 47 | |
| 48 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 49 | /* Function definitions. */ |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 50 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 51 | void DRD_(clientobj_set_trace)(const Bool trace) |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 52 | { |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 53 | s_trace_clientobj = trace; |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 54 | } |
| 55 | |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 56 | /** Initialize the client object set. */ |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 57 | void DRD_(clientobj_init)(void) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 58 | { |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 59 | 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); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 63 | } |
| 64 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 65 | /** |
| 66 | * Free the memory allocated for the client object set. |
| 67 | * |
| 68 | * @pre Client object set is empty. |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 69 | */ |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 70 | void DRD_(clientobj_cleanup)(void) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 71 | { |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 72 | 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; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 76 | } |
| 77 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 78 | /** |
| 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. |
bart | 391d9dc | 2008-07-03 10:57:30 +0000 | [diff] [blame] | 82 | */ |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 83 | DrdClientobj* DRD_(clientobj_get_any)(const Addr addr) |
bart | 391d9dc | 2008-07-03 10:57:30 +0000 | [diff] [blame] | 84 | { |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 85 | return VG_(OSetGen_Lookup)(s_clientobj_set, &addr); |
bart | 391d9dc | 2008-07-03 10:57:30 +0000 | [diff] [blame] | 86 | } |
| 87 | |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 88 | /** Return the data associated with the client object at client address addr |
| 89 | * and that has object type t. Return 0 if there is no client object in the |
| 90 | * set with the specified start address. |
| 91 | */ |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 92 | DrdClientobj* DRD_(clientobj_get)(const Addr addr, const ObjType t) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 93 | { |
| 94 | DrdClientobj* p; |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 95 | p = VG_(OSetGen_Lookup)(s_clientobj_set, &addr); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 96 | if (p && p->any.type == t) |
| 97 | return p; |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | /** Return true if and only if the address range of any client object overlaps |
| 102 | * with the specified address range. |
| 103 | */ |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 104 | Bool DRD_(clientobj_present)(const Addr a1, const Addr a2) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 105 | { |
| 106 | DrdClientobj *p; |
| 107 | |
| 108 | tl_assert(a1 < a2); |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 109 | VG_(OSetGen_ResetIter)(s_clientobj_set); |
| 110 | for ( ; (p = VG_(OSetGen_Next)(s_clientobj_set)) != 0; ) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 111 | { |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 112 | if (a1 <= p->any.a1 && p->any.a1 < a2) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 113 | { |
| 114 | return True; |
| 115 | } |
| 116 | } |
| 117 | return False; |
| 118 | } |
| 119 | |
| 120 | /** Add state information for the client object at client address addr and |
| 121 | * of type t. Suppress data race reports on the address range [addr,addr+size[. |
| 122 | * @pre No other client object is present in the address range [addr,addr+size[. |
| 123 | */ |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 124 | DrdClientobj* DRD_(clientobj_add)(const Addr a1, const ObjType t) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 125 | { |
| 126 | DrdClientobj* p; |
| 127 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 128 | tl_assert(! DRD_(clientobj_present)(a1, a1 + 1)); |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 129 | tl_assert(VG_(OSetGen_Lookup)(s_clientobj_set, &a1) == 0); |
bart | bcbd748 | 2008-02-29 19:19:39 +0000 | [diff] [blame] | 130 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 131 | if (s_trace_clientobj) |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 132 | { |
| 133 | VG_(message)(Vg_UserMsg, "Adding client object 0x%lx of type %d", a1, t); |
| 134 | } |
| 135 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 136 | p = VG_(OSetGen_AllocNode)(s_clientobj_set, sizeof(*p)); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 137 | VG_(memset)(p, 0, sizeof(*p)); |
| 138 | p->any.a1 = a1; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 139 | p->any.type = t; |
bart | 391d9dc | 2008-07-03 10:57:30 +0000 | [diff] [blame] | 140 | p->any.first_observed_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0); |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 141 | VG_(OSetGen_Insert)(s_clientobj_set, p); |
| 142 | tl_assert(VG_(OSetGen_Lookup)(s_clientobj_set, &a1) == p); |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 143 | DRD_(start_suppression)(a1, a1 + 1, "clientobj"); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 144 | return p; |
| 145 | } |
| 146 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 147 | Bool DRD_(clientobj_remove)(const Addr addr, const ObjType t) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 148 | { |
| 149 | DrdClientobj* p; |
| 150 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 151 | p = VG_(OSetGen_Lookup)(s_clientobj_set, &addr); |
| 152 | tl_assert(p); |
| 153 | tl_assert(p->any.type == t); |
| 154 | return clientobj_remove_obj(p); |
| 155 | } |
| 156 | |
| 157 | static Bool clientobj_remove_obj(DrdClientobj* const p) |
| 158 | { |
| 159 | DrdClientobj* q; |
| 160 | |
| 161 | if (s_trace_clientobj) |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 162 | { |
| 163 | VG_(message)(Vg_UserMsg, "Removing client object 0x%lx of type %d", |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 164 | p->any.a1, p->any.type); |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 165 | #if 0 |
| 166 | VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(), |
| 167 | VG_(clo_backtrace_size)); |
| 168 | #endif |
| 169 | } |
| 170 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 171 | tl_assert(p); |
| 172 | q = VG_(OSetGen_Remove)(s_clientobj_set, &p->any.a1); |
| 173 | tl_assert(p == q); |
| 174 | |
| 175 | tl_assert(VG_(OSetGen_Lookup)(s_clientobj_set, &p->any.a1) == 0); |
| 176 | tl_assert(p->any.cleanup); |
| 177 | (*p->any.cleanup)(p); |
| 178 | VG_(OSetGen_FreeNode)(s_clientobj_set, p); |
| 179 | return True; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 180 | } |
| 181 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 182 | void DRD_(clientobj_stop_using_mem)(const Addr a1, const Addr a2) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 183 | { |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 184 | Addr removed_at; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 185 | DrdClientobj* p; |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 186 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 187 | tl_assert(s_clientobj_set); |
bart | fea6444 | 2008-03-30 16:55:40 +0000 | [diff] [blame] | 188 | |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 189 | if (! DRD_(is_any_suppressed)(a1, a2)) |
bart | fea6444 | 2008-03-30 16:55:40 +0000 | [diff] [blame] | 190 | return; |
| 191 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 192 | VG_(OSetGen_ResetIter)(s_clientobj_set); |
| 193 | p = VG_(OSetGen_Next)(s_clientobj_set); |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 194 | for ( ; p != 0; ) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 195 | { |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 196 | if (a1 <= p->any.a1 && p->any.a1 < a2) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 197 | { |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 198 | removed_at = p->any.a1; |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 199 | DRD_(clientobj_remove)(p->any.a1, p->any.type); |
bart | bcbd748 | 2008-02-29 19:19:39 +0000 | [diff] [blame] | 200 | /* The above call removes an element from the oset and hence */ |
| 201 | /* invalidates the iterator. Set the iterator back. */ |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 202 | VG_(OSetGen_ResetIter)(s_clientobj_set); |
| 203 | while ((p = VG_(OSetGen_Next)(s_clientobj_set)) != 0 |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 204 | && p->any.a1 <= removed_at) |
| 205 | { } |
| 206 | } |
| 207 | else |
| 208 | { |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 209 | p = VG_(OSetGen_Next)(s_clientobj_set); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 214 | /** |
| 215 | * Delete the per-thread information stored in client objects for the |
| 216 | * specified thread. |
| 217 | */ |
| 218 | void DRD_(clientobj_delete_thread)(const DrdThreadId tid) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 219 | { |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 220 | DrdClientobj *p; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 221 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame^] | 222 | VG_(OSetGen_ResetIter)(s_clientobj_set); |
| 223 | for ( ; (p = VG_(OSetGen_Next)(s_clientobj_set)) != 0; ) |
| 224 | { |
| 225 | if (p->any.delete_thread) |
| 226 | { |
| 227 | (*p->any.delete_thread)(p, tid); |
| 228 | } |
| 229 | } |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 230 | } |
| 231 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 232 | const char* DRD_(clientobj_type_name)(const ObjType t) |
bart | 391d9dc | 2008-07-03 10:57:30 +0000 | [diff] [blame] | 233 | { |
| 234 | switch (t) |
| 235 | { |
| 236 | case ClientMutex: return "mutex"; |
| 237 | case ClientCondvar: return "cond"; |
| 238 | case ClientSemaphore: return "semaphore"; |
| 239 | case ClientBarrier: return "barrier"; |
| 240 | case ClientRwlock: return "rwlock"; |
| 241 | } |
| 242 | return "(unknown)"; |
| 243 | } |