bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 2 | /* |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 3 | This file is part of drd, a thread error detector. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 4 | |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 5 | Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 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 | 1a3b0b3 | 2009-05-03 17:07:34 +0000 | [diff] [blame] | 41 | static ULong s_segment_merge_count; |
bart | 2adfc2a | 2009-03-12 18:38:00 +0000 | [diff] [blame] | 42 | static ULong s_segments_created_count; |
| 43 | static ULong s_segments_alive_count; |
| 44 | static ULong s_max_segments_alive_count; |
| 45 | static Bool s_trace_segment; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 46 | |
| 47 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 48 | /* Function definitions. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 49 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 50 | /** |
| 51 | * Initialize the memory 'sg' points at. |
bart | 2adfc2a | 2009-03-12 18:38:00 +0000 | [diff] [blame] | 52 | * |
| 53 | * @note The creator and created thread ID's may be equal. |
| 54 | * @note This function copies the vector clock of thread 'creator', a technique |
| 55 | * also known as clock snooping. This will only work reliably if the thread |
| 56 | * that called pthread_create() waits until the created thread has copied |
| 57 | * the vector clock. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 58 | */ |
bart | 2adfc2a | 2009-03-12 18:38:00 +0000 | [diff] [blame] | 59 | static void sg_init(Segment* const sg, |
| 60 | const DrdThreadId creator, |
| 61 | const DrdThreadId created) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 62 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 63 | Segment* creator_sg; |
| 64 | ThreadId vg_created = DRD_(DrdThreadIdToVgThreadId)(created); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 65 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 66 | tl_assert(sg); |
| 67 | tl_assert(creator == DRD_INVALID_THREADID |
| 68 | || DRD_(IsValidDrdThreadId)(creator)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 69 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 70 | creator_sg = (creator != DRD_INVALID_THREADID |
| 71 | ? DRD_(thread_get_segment)(creator) : 0); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 72 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 73 | sg->next = 0; |
| 74 | sg->prev = 0; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame^] | 75 | sg->tid = created; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 76 | sg->refcnt = 1; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 77 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 78 | if (vg_created != VG_INVALID_THREADID && VG_(get_SP)(vg_created) != 0) |
| 79 | sg->stacktrace = VG_(record_ExeContext)(vg_created, 0); |
| 80 | else |
| 81 | sg->stacktrace = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 82 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 83 | if (creator_sg) |
| 84 | DRD_(vc_copy)(&sg->vc, &creator_sg->vc); |
| 85 | else |
| 86 | DRD_(vc_init)(&sg->vc, 0, 0); |
| 87 | DRD_(vc_increment)(&sg->vc, created); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame^] | 88 | DRD_(bm_init)(&sg->bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 89 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 90 | if (s_trace_segment) |
| 91 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame^] | 92 | char* vc; |
| 93 | |
| 94 | vc = DRD_(vc_aprint)(&sg->vc); |
| 95 | VG_(message)(Vg_DebugMsg, "New segment for thread %d/%d with vc %s", |
| 96 | created != VG_INVALID_THREADID |
| 97 | ? DRD_(DrdThreadIdToVgThreadId)(created) |
| 98 | : DRD_INVALID_THREADID, |
| 99 | created, vc); |
| 100 | VG_(free)(vc); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 101 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 102 | } |
| 103 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 104 | /** Deallocate the memory that was allocated by sg_init(). */ |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 105 | static void DRD_(sg_cleanup)(Segment* const sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 106 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 107 | tl_assert(sg); |
| 108 | tl_assert(sg->refcnt == 0); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 109 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 110 | DRD_(vc_cleanup)(&sg->vc); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame^] | 111 | DRD_(bm_cleanup)(&sg->bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 112 | } |
| 113 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 114 | /** Allocate and initialize a new segment. */ |
bart | 2adfc2a | 2009-03-12 18:38:00 +0000 | [diff] [blame] | 115 | Segment* DRD_(sg_new)(const DrdThreadId creator, const DrdThreadId created) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 116 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 117 | Segment* sg; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 118 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 119 | s_segments_created_count++; |
| 120 | s_segments_alive_count++; |
| 121 | if (s_max_segments_alive_count < s_segments_alive_count) |
| 122 | s_max_segments_alive_count = s_segments_alive_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 123 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 124 | sg = VG_(malloc)("drd.segment.sn.1", sizeof(*sg)); |
| 125 | tl_assert(sg); |
| 126 | sg_init(sg, creator, created); |
| 127 | return sg; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 128 | } |
| 129 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 130 | static void DRD_(sg_delete)(Segment* const sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 131 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 132 | if (DRD_(sg_get_trace)()) |
| 133 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame^] | 134 | char* vc; |
| 135 | |
| 136 | vc = DRD_(vc_aprint)(&sg->vc); |
| 137 | VG_(message)(Vg_DebugMsg, "Discarding the segment with vector clock %s", |
| 138 | vc); |
| 139 | VG_(free)(vc); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 140 | } |
bart | 68edad5 | 2008-02-24 18:21:12 +0000 | [diff] [blame] | 141 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 142 | s_segments_alive_count--; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 143 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 144 | tl_assert(sg); |
| 145 | DRD_(sg_cleanup)(sg); |
| 146 | VG_(free)(sg); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 147 | } |
| 148 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 149 | /** Increment the reference count of the specified segment. */ |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 150 | Segment* DRD_(sg_get)(Segment* const sg) |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 151 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 152 | tl_assert(sg); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 153 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 154 | sg->refcnt++; |
| 155 | return sg; |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 156 | } |
| 157 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 158 | /** |
| 159 | * Decrement the reference count of the specified segment and deallocate the |
| 160 | * segment if the reference count became zero. |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 161 | */ |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 162 | void DRD_(sg_put)(Segment* const sg) |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 163 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 164 | if (sg == 0) |
| 165 | return; |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 166 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 167 | if (s_trace_segment) |
| 168 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame^] | 169 | char* vc; |
| 170 | |
| 171 | vc = DRD_(vc_aprint)(&sg->vc); |
| 172 | VG_(message)(Vg_DebugMsg, |
| 173 | "Decrementing segment reference count %d -> %d with vc %s", |
| 174 | sg->refcnt, sg->refcnt - 1, vc); |
| 175 | VG_(free)(vc); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 176 | } |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 177 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 178 | tl_assert(sg->refcnt >= 1); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 179 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 180 | if (--sg->refcnt == 0) |
| 181 | { |
| 182 | DRD_(sg_delete)(sg); |
| 183 | } |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 184 | } |
| 185 | |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 186 | /** Merge sg1 and sg2 into sg1. */ |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame^] | 187 | void DRD_(sg_merge)(Segment* const sg1, Segment* const sg2) |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 188 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 189 | tl_assert(sg1); |
| 190 | tl_assert(sg1->refcnt == 1); |
| 191 | tl_assert(sg2); |
| 192 | tl_assert(sg2->refcnt == 1); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 193 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 194 | if (s_trace_segment) |
| 195 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame^] | 196 | char *vc1, *vc2; |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 197 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame^] | 198 | vc1 = DRD_(vc_aprint)(&sg1->vc); |
| 199 | vc2 = DRD_(vc_aprint)(&sg2->vc); |
| 200 | |
| 201 | VG_(message)(Vg_DebugMsg, "Merging segments with vector clocks %s and %s", |
| 202 | vc1, vc2); |
| 203 | VG_(free)(vc1); |
| 204 | VG_(free)(vc2); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 205 | } |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 206 | |
bart | 1a3b0b3 | 2009-05-03 17:07:34 +0000 | [diff] [blame] | 207 | s_segment_merge_count++; |
| 208 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 209 | // Keep sg1->stacktrace. |
| 210 | // Keep sg1->vc. |
| 211 | // Merge sg2->bm into sg1->bm. |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame^] | 212 | DRD_(bm_merge2)(&sg1->bm, &sg2->bm); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 213 | } |
| 214 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 215 | /** Print the vector clock and the bitmap of the specified segment. */ |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame^] | 216 | void DRD_(sg_print)(Segment* const sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 217 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 218 | tl_assert(sg); |
| 219 | VG_(printf)("vc: "); |
| 220 | DRD_(vc_print)(&sg->vc); |
| 221 | VG_(printf)("\n"); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame^] | 222 | DRD_(bm_print)(&sg->bm); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 223 | } |
| 224 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 225 | /** Query whether segment tracing has been enabled. */ |
| 226 | Bool DRD_(sg_get_trace)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 227 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 228 | return s_trace_segment; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 229 | } |
| 230 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 231 | /** Enable or disable segment tracing. */ |
| 232 | void DRD_(sg_set_trace)(Bool const trace_segment) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 233 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 234 | tl_assert(trace_segment == False || trace_segment == True); |
| 235 | s_trace_segment = trace_segment; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 236 | } |
| 237 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 238 | ULong DRD_(sg_get_segments_created_count)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 239 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 240 | return s_segments_created_count; |
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_alive_count)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 244 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 245 | return s_segments_alive_count; |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame] | 246 | } |
| 247 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 248 | ULong DRD_(sg_get_max_segments_alive_count)(void) |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame] | 249 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 250 | return s_max_segments_alive_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 251 | } |
bart | 1a3b0b3 | 2009-05-03 17:07:34 +0000 | [diff] [blame] | 252 | |
| 253 | ULong DRD_(sg_get_segment_merge_count)(void) |
| 254 | { |
| 255 | return s_segment_merge_count; |
| 256 | } |