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 | |
| 49 | /** |
| 50 | * Note: creator and created may be equal. |
| 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); |
| 65 | |
| 66 | sg->next = 0; |
| 67 | sg->prev = 0; |
| 68 | |
tom | 9e86a12 | 2008-01-02 10:07:44 +0000 | [diff] [blame] | 69 | if (vg_created != VG_INVALID_THREADID && VG_(get_SP)(vg_created) != 0) |
sewardj | e0744f0 | 2007-12-01 02:09:50 +0000 | [diff] [blame] | 70 | sg->stacktrace = VG_(record_ExeContext)(vg_created, 0); |
| 71 | else |
| 72 | sg->stacktrace = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 73 | |
| 74 | if (creator_sg) |
| 75 | vc_copy(&sg->vc, &creator_sg->vc); |
| 76 | else |
| 77 | vc_init(&sg->vc, 0, 0); |
| 78 | vc_increment(&sg->vc, created); |
| 79 | sg->bm = bm_new(); |
| 80 | |
| 81 | if (drd_trace_segment) |
| 82 | { |
| 83 | char msg[256]; |
| 84 | VG_(snprintf)(msg, sizeof(msg), |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 85 | "New segment for thread %d/%d with vc ", |
| 86 | DrdThreadIdToVgThreadId(creator), creator); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 87 | vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 88 | &sg->vc); |
bart | 2c7c1d5 | 2008-02-29 19:17:28 +0000 | [diff] [blame] | 89 | VG_(message)(Vg_UserMsg, "%s", msg); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame^] | 93 | static |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 94 | void sg_cleanup(Segment* const sg) |
| 95 | { |
| 96 | tl_assert(sg); |
| 97 | vc_cleanup(&sg->vc); |
| 98 | bm_delete(sg->bm); |
| 99 | sg->bm = 0; |
| 100 | } |
| 101 | |
| 102 | Segment* sg_new(ThreadId const creator, ThreadId const created) |
| 103 | { |
| 104 | Segment* sg; |
| 105 | |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame^] | 106 | s_created_segments_count++; |
| 107 | s_alive_segments_count++; |
| 108 | if (s_max_alive_segments_count < s_alive_segments_count) |
| 109 | s_max_alive_segments_count = s_alive_segments_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 110 | |
| 111 | sg = VG_(malloc)(sizeof(*sg)); |
| 112 | tl_assert(sg); |
| 113 | sg_init(sg, creator, created); |
| 114 | return sg; |
| 115 | } |
| 116 | |
| 117 | void sg_delete(Segment* const sg) |
| 118 | { |
bart | 68edad5 | 2008-02-24 18:21:12 +0000 | [diff] [blame] | 119 | #if 1 |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 120 | if (sg_get_trace()) |
| 121 | { |
| 122 | char msg[256]; |
| 123 | VG_(snprintf)(msg, sizeof(msg), |
| 124 | "Discarding the segment with vector clock "); |
| 125 | vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 126 | &sg->vc); |
| 127 | VG_(message)(Vg_UserMsg, "%s", msg); |
| 128 | } |
bart | 68edad5 | 2008-02-24 18:21:12 +0000 | [diff] [blame] | 129 | #endif |
| 130 | |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame^] | 131 | s_alive_segments_count--; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 132 | |
| 133 | tl_assert(sg); |
| 134 | sg_cleanup(sg); |
| 135 | VG_(free)(sg); |
| 136 | } |
| 137 | |
| 138 | void sg_print(const Segment* const sg) |
| 139 | { |
| 140 | tl_assert(sg); |
| 141 | VG_(printf)("vc: "); |
| 142 | vc_print(&sg->vc); |
| 143 | VG_(printf)("\n"); |
| 144 | bm_print(sg->bm); |
| 145 | } |
| 146 | |
| 147 | Bool sg_get_trace(void) |
| 148 | { |
| 149 | return drd_trace_segment; |
| 150 | } |
| 151 | |
| 152 | void sg_set_trace(Bool const trace_segment) |
| 153 | { |
| 154 | tl_assert(trace_segment == False || trace_segment == True); |
| 155 | drd_trace_segment = trace_segment; |
| 156 | } |
| 157 | |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame^] | 158 | ULong sg_get_created_segments_count(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 159 | { |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame^] | 160 | return s_created_segments_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 161 | } |
| 162 | |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame^] | 163 | ULong sg_get_alive_segments_count(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 164 | { |
bart | 7102f10 | 2008-03-17 17:37:53 +0000 | [diff] [blame^] | 165 | return s_alive_segments_count; |
| 166 | } |
| 167 | |
| 168 | ULong sg_get_max_alive_segments_count(void) |
| 169 | { |
| 170 | return s_max_alive_segments_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 171 | } |