sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2008 Bart Van Assche |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 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 | |
sewardj | e0744f0 | 2007-12-01 02:09:50 +0000 | [diff] [blame] | 26 | #include "drd_error.h" |
| 27 | #include "drd_segment.h" |
| 28 | #include "drd_thread.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 29 | #include "pub_tool_basics.h" // Addr, SizeT |
| 30 | #include "pub_tool_errormgr.h" // VG_(unique_error)() |
| 31 | #include "pub_tool_libcassert.h" // tl_assert() |
| 32 | #include "pub_tool_libcbase.h" // VG_(strlen)() |
| 33 | #include "pub_tool_libcprint.h" // VG_(printf)() |
tom | da3fdba | 2008-01-04 23:57:15 +0000 | [diff] [blame] | 34 | #include "pub_tool_machine.h" // VG_(get_SP)() |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 35 | #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)() |
sewardj | e0744f0 | 2007-12-01 02:09:50 +0000 | [diff] [blame] | 36 | #include "pub_tool_threadstate.h" // VG_INVALID_THREADID |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 37 | |
| 38 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 39 | /* Local variables. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 40 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 41 | static ULong DRD_(s_segments_created_count); |
| 42 | static ULong DRD_(s_segments_alive_count); |
| 43 | static ULong DRD_(s_max_segments_alive_count); |
| 44 | static Bool DRD_(s_trace_segment) = False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 45 | |
| 46 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 47 | /* Function definitions. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 48 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 49 | /** |
| 50 | * Initialize the memory 'sg' points at. |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 51 | * @note The creator and created thread ID's may be equal. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 52 | */ |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame] | 53 | static |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 54 | void DRD_(sg_init)(Segment* const sg, |
| 55 | DrdThreadId const creator, |
| 56 | DrdThreadId const created) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 57 | { |
| 58 | Segment* creator_sg; |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 59 | ThreadId vg_created = DRD_(DrdThreadIdToVgThreadId)(created); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 60 | |
| 61 | tl_assert(sg); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 62 | tl_assert(creator == DRD_INVALID_THREADID |
| 63 | || DRD_(IsValidDrdThreadId)(creator)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 64 | |
| 65 | creator_sg = (creator != DRD_INVALID_THREADID |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 66 | ? DRD_(thread_get_segment)(creator) : 0); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 67 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 68 | sg->next = 0; |
| 69 | sg->prev = 0; |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 70 | sg->refcnt = 1; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 71 | |
tom | 9e86a12 | 2008-01-02 10:07:44 +0000 | [diff] [blame] | 72 | if (vg_created != VG_INVALID_THREADID && VG_(get_SP)(vg_created) != 0) |
sewardj | e0744f0 | 2007-12-01 02:09:50 +0000 | [diff] [blame] | 73 | sg->stacktrace = VG_(record_ExeContext)(vg_created, 0); |
| 74 | else |
| 75 | sg->stacktrace = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 76 | |
| 77 | if (creator_sg) |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 78 | DRD_(vc_copy)(&sg->vc, &creator_sg->vc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 79 | else |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 80 | DRD_(vc_init)(&sg->vc, 0, 0); |
| 81 | DRD_(vc_increment)(&sg->vc, created); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 82 | sg->bm = bm_new(); |
| 83 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 84 | if (DRD_(s_trace_segment)) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 85 | { |
| 86 | char msg[256]; |
| 87 | VG_(snprintf)(msg, sizeof(msg), |
bart | 8514502 | 2008-04-06 13:07:45 +0000 | [diff] [blame] | 88 | "New segment for thread %d/%d with vc ", |
| 89 | created != VG_INVALID_THREADID |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 90 | ? DRD_(DrdThreadIdToVgThreadId)(created) |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 91 | : DRD_INVALID_THREADID, |
bart | 8514502 | 2008-04-06 13:07:45 +0000 | [diff] [blame] | 92 | created); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 93 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 94 | &sg->vc); |
bart | 2c7c1d5 | 2008-02-29 19:17:28 +0000 | [diff] [blame] | 95 | VG_(message)(Vg_UserMsg, "%s", msg); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 99 | /** Deallocate the memory that was allocated by sg_init(). */ |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 100 | static void DRD_(sg_cleanup)(Segment* const sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 101 | { |
| 102 | tl_assert(sg); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 103 | tl_assert(sg->refcnt == 0); |
| 104 | |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 105 | DRD_(vc_cleanup)(&sg->vc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 106 | bm_delete(sg->bm); |
| 107 | sg->bm = 0; |
| 108 | } |
| 109 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 110 | /** Allocate and initialize a new segment. */ |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 111 | Segment* DRD_(sg_new)(ThreadId const creator, ThreadId const created) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 112 | { |
| 113 | Segment* sg; |
| 114 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 115 | DRD_(s_segments_created_count)++; |
| 116 | DRD_(s_segments_alive_count)++; |
| 117 | if (DRD_(s_max_segments_alive_count) < DRD_(s_segments_alive_count)) |
| 118 | DRD_(s_max_segments_alive_count) = DRD_(s_segments_alive_count); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 119 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 120 | sg = VG_(malloc)("drd.segment.sn.1", sizeof(*sg)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 121 | tl_assert(sg); |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 122 | DRD_(sg_init)(sg, creator, created); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 123 | return sg; |
| 124 | } |
| 125 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 126 | static void DRD_(sg_delete)(Segment* const sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 127 | { |
bart | 68edad5 | 2008-02-24 18:21:12 +0000 | [diff] [blame] | 128 | #if 1 |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 129 | if (DRD_(sg_get_trace)()) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 130 | { |
| 131 | char msg[256]; |
| 132 | VG_(snprintf)(msg, sizeof(msg), |
| 133 | "Discarding the segment with vector clock "); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 134 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 135 | &sg->vc); |
| 136 | VG_(message)(Vg_UserMsg, "%s", msg); |
| 137 | } |
bart | 68edad5 | 2008-02-24 18:21:12 +0000 | [diff] [blame] | 138 | #endif |
| 139 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 140 | DRD_(s_segments_alive_count)--; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 141 | |
| 142 | tl_assert(sg); |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 143 | DRD_(sg_cleanup)(sg); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 144 | VG_(free)(sg); |
| 145 | } |
| 146 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 147 | /** Query the reference count of the specified segment. */ |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 148 | int DRD_(sg_get_refcnt)(const Segment* const sg) |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 149 | { |
| 150 | tl_assert(sg); |
| 151 | |
| 152 | return sg->refcnt; |
| 153 | } |
| 154 | |
| 155 | /** Increment the reference count of the specified segment. */ |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 156 | Segment* DRD_(sg_get)(Segment* const sg) |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 157 | { |
| 158 | tl_assert(sg); |
| 159 | |
| 160 | sg->refcnt++; |
| 161 | return sg; |
| 162 | } |
| 163 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 164 | /** |
| 165 | * Decrement the reference count of the specified segment and deallocate the |
| 166 | * segment if the reference count became zero. |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 167 | */ |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 168 | void DRD_(sg_put)(Segment* const sg) |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 169 | { |
| 170 | if (sg == 0) |
| 171 | return; |
| 172 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 173 | if (DRD_(s_trace_segment)) |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 174 | { |
| 175 | char msg[256]; |
| 176 | VG_(snprintf)(msg, sizeof(msg), |
| 177 | "Decrementing segment reference count %d -> %d with vc ", |
| 178 | sg->refcnt, sg->refcnt - 1); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 179 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 180 | &sg->vc); |
| 181 | VG_(message)(Vg_UserMsg, "%s", msg); |
| 182 | } |
| 183 | |
| 184 | tl_assert(sg->refcnt >= 1); |
| 185 | |
| 186 | if (--sg->refcnt == 0) |
| 187 | { |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 188 | DRD_(sg_delete)(sg); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 192 | /** Merge sg1 and sg2 into sg1. */ |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 193 | void DRD_(sg_merge)(const Segment* const sg1, Segment* const sg2) |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 194 | { |
| 195 | tl_assert(sg1); |
bart | a71e579 | 2008-03-22 17:09:04 +0000 | [diff] [blame] | 196 | tl_assert(sg1->refcnt == 1); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 197 | tl_assert(sg2); |
bart | a71e579 | 2008-03-22 17:09:04 +0000 | [diff] [blame] | 198 | tl_assert(sg2->refcnt == 1); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 199 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 200 | if (DRD_(s_trace_segment)) |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 201 | { |
| 202 | char msg[256]; |
| 203 | |
| 204 | VG_(snprintf)(msg, sizeof(msg), "Merging segments with vector clocks "); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 205 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 206 | &sg1->vc); |
| 207 | VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 208 | " and "); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 209 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 210 | &sg2->vc); |
| 211 | VG_(message)(Vg_UserMsg, "%s", msg); |
| 212 | } |
| 213 | |
| 214 | // Keep sg1->stacktrace. |
| 215 | // Keep sg1->vc. |
| 216 | // Merge sg2->bm into sg1->bm. |
| 217 | bm_merge2(sg1->bm, sg2->bm); |
| 218 | } |
| 219 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 220 | /** Print the vector clock and the bitmap of the specified segment. */ |
| 221 | void DRD_(sg_print)(const Segment* const sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 222 | { |
| 223 | tl_assert(sg); |
| 224 | VG_(printf)("vc: "); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 225 | DRD_(vc_print)(&sg->vc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 226 | VG_(printf)("\n"); |
| 227 | bm_print(sg->bm); |
| 228 | } |
| 229 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 230 | /** Query whether segment tracing has been enabled. */ |
| 231 | Bool DRD_(sg_get_trace)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 232 | { |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 233 | return DRD_(s_trace_segment); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 234 | } |
| 235 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 236 | /** Enable or disable segment tracing. */ |
| 237 | void DRD_(sg_set_trace)(Bool const trace_segment) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 238 | { |
| 239 | tl_assert(trace_segment == False || trace_segment == True); |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 240 | DRD_(s_trace_segment) = trace_segment; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 241 | } |
| 242 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 243 | ULong DRD_(sg_get_segments_created_count)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 244 | { |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 245 | return DRD_(s_segments_created_count); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 246 | } |
| 247 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 248 | ULong DRD_(sg_get_segments_alive_count)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 249 | { |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 250 | return DRD_(s_segments_alive_count); |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame] | 251 | } |
| 252 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 253 | ULong DRD_(sg_get_max_segments_alive_count)(void) |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame] | 254 | { |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 255 | return DRD_(s_max_segments_alive_count); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 256 | } |