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 | |
sewardj | 03f8d3f | 2012-08-05 15:46:46 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2012 Bart Van Assche <bvanassche@acm.org>. |
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" |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 26 | #include "drd_error.h" |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 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 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 39 | /* Local variables. */ |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 40 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 41 | static OSet* s_clientobj_set; |
| 42 | static Bool s_trace_clientobj; |
| 43 | |
| 44 | |
| 45 | /* Local functions. */ |
| 46 | |
| 47 | static Bool clientobj_remove_obj(DrdClientobj* const p); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 48 | |
| 49 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 50 | /* Function definitions. */ |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 51 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 52 | void DRD_(clientobj_set_trace)(const Bool trace) |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 53 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 54 | s_trace_clientobj = trace; |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 55 | } |
| 56 | |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 57 | /** Initialize the client object set. */ |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 58 | void DRD_(clientobj_init)(void) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 59 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 60 | 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); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 64 | } |
| 65 | |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 66 | /** |
| 67 | * Free the memory allocated for the client object set. |
| 68 | * |
| 69 | * @pre Client object set is empty. |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 70 | */ |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 71 | void DRD_(clientobj_cleanup)(void) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 72 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 73 | 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; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 77 | } |
| 78 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 79 | /** |
| 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. |
bart | 391d9dc | 2008-07-03 10:57:30 +0000 | [diff] [blame] | 83 | */ |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 84 | DrdClientobj* DRD_(clientobj_get_any)(const Addr addr) |
bart | 391d9dc | 2008-07-03 10:57:30 +0000 | [diff] [blame] | 85 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 86 | return VG_(OSetGen_Lookup)(s_clientobj_set, &addr); |
bart | 391d9dc | 2008-07-03 10:57:30 +0000 | [diff] [blame] | 87 | } |
| 88 | |
bart | 1344d6c | 2009-02-21 16:17:50 +0000 | [diff] [blame] | 89 | /** |
| 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. |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 93 | */ |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 94 | DrdClientobj* DRD_(clientobj_get)(const Addr addr, const ObjType t) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 95 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 96 | DrdClientobj* p; |
| 97 | p = VG_(OSetGen_Lookup)(s_clientobj_set, &addr); |
| 98 | if (p && p->any.type == t) |
| 99 | return p; |
| 100 | return 0; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** Return true if and only if the address range of any client object overlaps |
| 104 | * with the specified address range. |
| 105 | */ |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 106 | Bool DRD_(clientobj_present)(const Addr a1, const Addr a2) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 107 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 108 | DrdClientobj *p; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 109 | |
bart | a300398 | 2010-09-08 16:29:17 +0000 | [diff] [blame] | 110 | tl_assert(a1 <= a2); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 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 | { |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 116 | return True; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | return False; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 120 | } |
| 121 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 122 | /** |
| 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[. |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 127 | */ |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 128 | DrdClientobj* DRD_(clientobj_add)(const Addr a1, const ObjType t) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 129 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 130 | DrdClientobj* p; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 131 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 132 | tl_assert(! DRD_(clientobj_present)(a1, a1 + 1)); |
| 133 | tl_assert(VG_(OSetGen_Lookup)(s_clientobj_set, &a1) == 0); |
bart | bcbd748 | 2008-02-29 19:19:39 +0000 | [diff] [blame] | 134 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 135 | if (s_trace_clientobj) |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 136 | DRD_(trace_msg)("Adding client object 0x%lx of type %d", a1, t); |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 137 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 138 | p = VG_(OSetGen_AllocNode)(s_clientobj_set, sizeof(*p)); |
| 139 | VG_(memset)(p, 0, sizeof(*p)); |
| 140 | p->any.a1 = a1; |
| 141 | p->any.type = t; |
| 142 | p->any.first_observed_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0); |
| 143 | VG_(OSetGen_Insert)(s_clientobj_set, p); |
| 144 | tl_assert(VG_(OSetGen_Lookup)(s_clientobj_set, &a1) == p); |
bart | b878a73 | 2010-03-07 20:07:15 +0000 | [diff] [blame] | 145 | if (t == ClientHbvar) |
| 146 | DRD_(mark_hbvar)(a1); |
| 147 | else |
| 148 | DRD_(start_suppression)(a1, a1 + 1, "clientobj"); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 149 | return p; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 150 | } |
| 151 | |
bart | 1344d6c | 2009-02-21 16:17:50 +0000 | [diff] [blame] | 152 | /** |
| 153 | * Remove the information that was stored about the client object. |
| 154 | * |
| 155 | * @param[in] addr Address of the client object in the client address space. |
| 156 | * @param[in] t Type of the client object. |
| 157 | */ |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 158 | Bool DRD_(clientobj_remove)(const Addr addr, const ObjType t) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 159 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 160 | DrdClientobj* p; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 161 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 162 | p = VG_(OSetGen_Lookup)(s_clientobj_set, &addr); |
| 163 | tl_assert(p); |
| 164 | tl_assert(p->any.type == t); |
| 165 | return clientobj_remove_obj(p); |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 166 | } |
| 167 | |
bart | 1344d6c | 2009-02-21 16:17:50 +0000 | [diff] [blame] | 168 | /** |
| 169 | * Remove the information that was stored about the client object p. |
| 170 | * |
| 171 | * @note The order of operations below is important. The client object is |
| 172 | * removed from the client object set after the cleanup function has been |
| 173 | * called such that if the cleanup function can still use the function |
| 174 | * DRD_(clientobj_get_any)(). This happens e.g. in the function |
| 175 | * first_observed() in drd_error.c. |
| 176 | */ |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 177 | static Bool clientobj_remove_obj(DrdClientobj* const p) |
| 178 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 179 | tl_assert(p); |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 180 | |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 181 | if (s_trace_clientobj) { |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 182 | DRD_(trace_msg)("Removing client object 0x%lx of type %d", p->any.a1, |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 183 | p->any.type); |
bart | 9374ee3 | 2009-07-23 16:24:02 +0000 | [diff] [blame] | 184 | #if 0 |
| 185 | VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(), |
| 186 | VG_(clo_backtrace_size)); |
| 187 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 188 | } |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 189 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 190 | 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; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 195 | } |
| 196 | |
bart | 28be7aa | 2009-02-23 19:15:32 +0000 | [diff] [blame] | 197 | /** |
| 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 | */ |
bart | 195e41f | 2009-02-15 11:34:57 +0000 | [diff] [blame] | 204 | void DRD_(clientobj_stop_using_mem)(const Addr a1, const Addr a2) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 205 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 206 | Addr removed_at; |
| 207 | DrdClientobj* p; |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 208 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 209 | tl_assert(s_clientobj_set); |
bart | fea6444 | 2008-03-30 16:55:40 +0000 | [diff] [blame] | 210 | |
bart | b878a73 | 2010-03-07 20:07:15 +0000 | [diff] [blame] | 211 | if (! DRD_(range_contains_suppression_or_hbvar)(a1, a2)) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 212 | return; |
bart | fea6444 | 2008-03-30 16:55:40 +0000 | [diff] [blame] | 213 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 214 | 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 | } |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 226 | } |
| 227 | |
bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 228 | /** |
| 229 | * Delete the per-thread information stored in client objects for the |
| 230 | * specified thread. |
| 231 | */ |
| 232 | void DRD_(clientobj_delete_thread)(const DrdThreadId tid) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 233 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 234 | DrdClientobj *p; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 235 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 236 | 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 | } |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 244 | } |
| 245 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 246 | const HChar* DRD_(clientobj_type_name)(const ObjType t) |
bart | 391d9dc | 2008-07-03 10:57:30 +0000 | [diff] [blame] | 247 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 248 | switch (t) |
| 249 | { |
| 250 | case ClientMutex: return "mutex"; |
| 251 | case ClientCondvar: return "cond"; |
bart | 62cc232 | 2010-03-07 10:54:21 +0000 | [diff] [blame] | 252 | case ClientHbvar: return "order annotation"; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 253 | case ClientSemaphore: return "semaphore"; |
| 254 | case ClientBarrier: return "barrier"; |
| 255 | case ClientRwlock: return "rwlock"; |
| 256 | } |
| 257 | return "(unknown)"; |
bart | 391d9dc | 2008-07-03 10:57:30 +0000 | [diff] [blame] | 258 | } |