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 | |
| 39 | // Local variables. |
| 40 | |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame] | 41 | static ULong s_created_segments_count; |
| 42 | static ULong s_alive_segments_count; |
| 43 | static ULong s_max_alive_segments_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 44 | static Bool drd_trace_segment = False; |
| 45 | |
| 46 | |
| 47 | // Function definitions. |
| 48 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 49 | /** Initialize the memory pointed at by sg. |
| 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 |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 53 | void sg_init(Segment* const sg, |
| 54 | DrdThreadId const creator, |
| 55 | DrdThreadId const created) |
| 56 | { |
| 57 | Segment* creator_sg; |
sewardj | e0744f0 | 2007-12-01 02:09:50 +0000 | [diff] [blame] | 58 | ThreadId vg_created = DrdThreadIdToVgThreadId(created); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 59 | |
| 60 | tl_assert(sg); |
| 61 | tl_assert(creator == DRD_INVALID_THREADID || IsValidDrdThreadId(creator)); |
| 62 | |
| 63 | creator_sg = (creator != DRD_INVALID_THREADID |
| 64 | ? thread_get_segment(creator) : 0); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 65 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 66 | sg->next = 0; |
| 67 | sg->prev = 0; |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 68 | sg->refcnt = 1; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 69 | |
tom | 9e86a12 | 2008-01-02 10:07:44 +0000 | [diff] [blame] | 70 | if (vg_created != VG_INVALID_THREADID && VG_(get_SP)(vg_created) != 0) |
sewardj | e0744f0 | 2007-12-01 02:09:50 +0000 | [diff] [blame] | 71 | sg->stacktrace = VG_(record_ExeContext)(vg_created, 0); |
| 72 | else |
| 73 | sg->stacktrace = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 74 | |
| 75 | if (creator_sg) |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame^] | 76 | DRD_(vc_copy)(&sg->vc, &creator_sg->vc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 77 | else |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame^] | 78 | DRD_(vc_init)(&sg->vc, 0, 0); |
| 79 | DRD_(vc_increment)(&sg->vc, created); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 80 | sg->bm = bm_new(); |
| 81 | |
| 82 | if (drd_trace_segment) |
| 83 | { |
| 84 | char msg[256]; |
| 85 | VG_(snprintf)(msg, sizeof(msg), |
bart | 8514502 | 2008-04-06 13:07:45 +0000 | [diff] [blame] | 86 | "New segment for thread %d/%d with vc ", |
| 87 | created != VG_INVALID_THREADID |
| 88 | ? DrdThreadIdToVgThreadId(created) |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 89 | : DRD_INVALID_THREADID, |
bart | 8514502 | 2008-04-06 13:07:45 +0000 | [diff] [blame] | 90 | created); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame^] | 91 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 92 | &sg->vc); |
bart | 2c7c1d5 | 2008-02-29 19:17:28 +0000 | [diff] [blame] | 93 | VG_(message)(Vg_UserMsg, "%s", msg); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 97 | /** Deallocate the memory that was allocated by sg_init(). */ |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame] | 98 | static |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 99 | void sg_cleanup(Segment* const sg) |
| 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); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 105 | bm_delete(sg->bm); |
| 106 | sg->bm = 0; |
| 107 | } |
| 108 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 109 | /** Allocate and initialize a new segment. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 110 | Segment* sg_new(ThreadId const creator, ThreadId const created) |
| 111 | { |
| 112 | Segment* sg; |
| 113 | |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame] | 114 | s_created_segments_count++; |
| 115 | s_alive_segments_count++; |
| 116 | if (s_max_alive_segments_count < s_alive_segments_count) |
| 117 | s_max_alive_segments_count = s_alive_segments_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); |
| 121 | sg_init(sg, creator, created); |
| 122 | return sg; |
| 123 | } |
| 124 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 125 | static |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 126 | void sg_delete(Segment* const sg) |
| 127 | { |
bart | 68edad5 | 2008-02-24 18:21:12 +0000 | [diff] [blame] | 128 | #if 1 |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 129 | if (sg_get_trace()) |
| 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 | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame] | 140 | s_alive_segments_count--; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 141 | |
| 142 | tl_assert(sg); |
| 143 | sg_cleanup(sg); |
| 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. */ |
| 148 | int sg_get_refcnt(const Segment* const sg) |
| 149 | { |
| 150 | tl_assert(sg); |
| 151 | |
| 152 | return sg->refcnt; |
| 153 | } |
| 154 | |
| 155 | /** Increment the reference count of the specified segment. */ |
| 156 | Segment* sg_get(Segment* const sg) |
| 157 | { |
| 158 | tl_assert(sg); |
| 159 | |
| 160 | sg->refcnt++; |
| 161 | return sg; |
| 162 | } |
| 163 | |
| 164 | /** Decrement the reference count of the specified segment and deallocate the |
| 165 | * segment if the reference count became zero. |
| 166 | */ |
| 167 | void sg_put(Segment* const sg) |
| 168 | { |
| 169 | if (sg == 0) |
| 170 | return; |
| 171 | |
| 172 | if (drd_trace_segment) |
| 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 | { |
| 187 | sg_delete(sg); |
| 188 | } |
| 189 | } |
| 190 | |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 191 | /** Merge sg1 and sg2 into sg1. */ |
| 192 | void sg_merge(const Segment* const sg1, Segment* const sg2) |
| 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 | |
| 199 | if (drd_trace_segment) |
| 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. |
| 216 | bm_merge2(sg1->bm, sg2->bm); |
| 217 | } |
| 218 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 219 | void sg_print(const Segment* const sg) |
| 220 | { |
| 221 | tl_assert(sg); |
| 222 | VG_(printf)("vc: "); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame^] | 223 | DRD_(vc_print)(&sg->vc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 224 | VG_(printf)("\n"); |
| 225 | bm_print(sg->bm); |
| 226 | } |
| 227 | |
| 228 | Bool sg_get_trace(void) |
| 229 | { |
| 230 | return drd_trace_segment; |
| 231 | } |
| 232 | |
| 233 | void sg_set_trace(Bool const trace_segment) |
| 234 | { |
| 235 | tl_assert(trace_segment == False || trace_segment == True); |
| 236 | drd_trace_segment = trace_segment; |
| 237 | } |
| 238 | |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame] | 239 | ULong sg_get_created_segments_count(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 240 | { |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame] | 241 | return s_created_segments_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 242 | } |
| 243 | |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame] | 244 | ULong sg_get_alive_segments_count(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 245 | { |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame] | 246 | return s_alive_segments_count; |
| 247 | } |
| 248 | |
| 249 | ULong sg_get_max_alive_segments_count(void) |
| 250 | { |
| 251 | return s_max_alive_segments_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 252 | } |