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); |
| 56 | s_clientobj = VG_(OSetGen_Create)(0, 0, VG_(malloc), VG_(free)); |
| 57 | tl_assert(s_clientobj); |
| 58 | } |
| 59 | |
| 60 | /** Free the memory allocated for the client object set. |
| 61 | * @pre Client object set is empty. |
| 62 | */ |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 63 | void clientobj_cleanup(void) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 64 | { |
| 65 | tl_assert(s_clientobj); |
| 66 | tl_assert(VG_(OSetGen_Size)(s_clientobj) == 0); |
| 67 | VG_(OSetGen_Destroy)(s_clientobj); |
| 68 | s_clientobj = 0; |
| 69 | } |
| 70 | |
| 71 | /** Return the data associated with the client object at client address addr |
| 72 | * and that has object type t. Return 0 if there is no client object in the |
| 73 | * set with the specified start address. |
| 74 | */ |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 75 | DrdClientobj* clientobj_get(const Addr addr, const ObjType t) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 76 | { |
| 77 | DrdClientobj* p; |
| 78 | p = VG_(OSetGen_Lookup)(s_clientobj, &addr); |
| 79 | if (p && p->any.type == t) |
| 80 | return p; |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | /** Return true if and only if the address range of any client object overlaps |
| 85 | * with the specified address range. |
| 86 | */ |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 87 | Bool clientobj_present(const Addr a1, const Addr a2) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 88 | { |
| 89 | DrdClientobj *p; |
| 90 | |
| 91 | tl_assert(a1 < a2); |
| 92 | VG_(OSetGen_ResetIter)(s_clientobj); |
| 93 | for ( ; (p = VG_(OSetGen_Next)(s_clientobj)) != 0; ) |
| 94 | { |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 95 | if (a1 <= p->any.a1 && p->any.a1 < a2) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 96 | { |
| 97 | return True; |
| 98 | } |
| 99 | } |
| 100 | return False; |
| 101 | } |
| 102 | |
| 103 | /** Add state information for the client object at client address addr and |
| 104 | * of type t. Suppress data race reports on the address range [addr,addr+size[. |
| 105 | * @pre No other client object is present in the address range [addr,addr+size[. |
| 106 | */ |
| 107 | DrdClientobj* |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 108 | clientobj_add(const Addr a1, const ObjType t) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 109 | { |
| 110 | DrdClientobj* p; |
| 111 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 112 | tl_assert(! clientobj_present(a1, a1 + 1)); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 113 | tl_assert(VG_(OSetGen_Lookup)(s_clientobj, &a1) == 0); |
bart | bcbd748 | 2008-02-29 19:19:39 +0000 | [diff] [blame] | 114 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 115 | if (s_trace_clientobj) |
| 116 | { |
| 117 | VG_(message)(Vg_UserMsg, "Adding client object 0x%lx of type %d", a1, t); |
| 118 | } |
| 119 | |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 120 | p = VG_(OSetGen_AllocNode)(s_clientobj, sizeof(*p)); |
| 121 | VG_(memset)(p, 0, sizeof(*p)); |
| 122 | p->any.a1 = a1; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 123 | p->any.type = t; |
| 124 | VG_(OSetGen_Insert)(s_clientobj, p); |
| 125 | tl_assert(VG_(OSetGen_Lookup)(s_clientobj, &a1) == p); |
bart | fea6444 | 2008-03-30 16:55:40 +0000 | [diff] [blame^] | 126 | drd_start_suppression(a1, a1 + 1, "clientobj"); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 127 | return p; |
| 128 | } |
| 129 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 130 | Bool clientobj_remove(const Addr addr, const ObjType t) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 131 | { |
| 132 | DrdClientobj* p; |
| 133 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 134 | if (s_trace_clientobj) |
| 135 | { |
| 136 | VG_(message)(Vg_UserMsg, "Removing client object 0x%lx of type %d", |
| 137 | addr, t); |
| 138 | #if 0 |
| 139 | VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(), |
| 140 | VG_(clo_backtrace_size)); |
| 141 | #endif |
| 142 | } |
| 143 | |
bart | 28230a3 | 2008-02-29 17:27:03 +0000 | [diff] [blame] | 144 | p = VG_(OSetGen_Lookup)(s_clientobj, &addr); |
| 145 | tl_assert(p->any.type == t); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 146 | p = VG_(OSetGen_Remove)(s_clientobj, &addr); |
| 147 | if (p) |
| 148 | { |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 149 | tl_assert(VG_(OSetGen_Lookup)(s_clientobj, &addr) == 0); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 150 | tl_assert(p->any.cleanup); |
| 151 | (*p->any.cleanup)(p); |
| 152 | VG_(OSetGen_FreeNode)(s_clientobj, p); |
| 153 | return True; |
| 154 | } |
| 155 | return False; |
| 156 | } |
| 157 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 158 | void clientobj_stop_using_mem(const Addr a1, const Addr a2) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 159 | { |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 160 | Addr removed_at; |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 161 | DrdClientobj* p; |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 162 | |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 163 | tl_assert(s_clientobj); |
bart | fea6444 | 2008-03-30 16:55:40 +0000 | [diff] [blame^] | 164 | |
| 165 | if (! drd_is_any_suppressed(a1, a2)) |
| 166 | return; |
| 167 | |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 168 | VG_(OSetGen_ResetIter)(s_clientobj); |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 169 | p = VG_(OSetGen_Next)(s_clientobj); |
| 170 | for ( ; p != 0; ) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 171 | { |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 172 | if (a1 <= p->any.a1 && p->any.a1 < a2) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 173 | { |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 174 | removed_at = p->any.a1; |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 175 | clientobj_remove(p->any.a1, p->any.type); |
bart | bcbd748 | 2008-02-29 19:19:39 +0000 | [diff] [blame] | 176 | /* The above call removes an element from the oset and hence */ |
| 177 | /* invalidates the iterator. Set the iterator back. */ |
bart | b78312c | 2008-02-29 11:00:17 +0000 | [diff] [blame] | 178 | VG_(OSetGen_ResetIter)(s_clientobj); |
| 179 | while ((p = VG_(OSetGen_Next)(s_clientobj)) != 0 |
| 180 | && p->any.a1 <= removed_at) |
| 181 | { } |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | p = VG_(OSetGen_Next)(s_clientobj); |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 190 | void clientobj_resetiter(void) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 191 | { |
| 192 | VG_(OSetGen_ResetIter)(s_clientobj); |
| 193 | } |
| 194 | |
bart | 72b751c | 2008-03-01 13:44:24 +0000 | [diff] [blame] | 195 | DrdClientobj* clientobj_next(const ObjType t) |
bart | e7d5872 | 2008-02-28 19:08:04 +0000 | [diff] [blame] | 196 | { |
| 197 | DrdClientobj* p; |
| 198 | while ((p = VG_(OSetGen_Next)(s_clientobj)) != 0 && p->any.type != t) |
| 199 | ; |
| 200 | return p; |
| 201 | } |
| 202 | |