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