blob: 9983e601519ca90dc207ca694488c87d3d432518 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 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
sewardje0744f02007-12-01 02:09:50 +000026#include "drd_error.h"
27#include "drd_segment.h"
28#include "drd_thread.h"
sewardjaf44c822007-11-25 14:01:38 +000029#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)()
tomda3fdba2008-01-04 23:57:15 +000034#include "pub_tool_machine.h" // VG_(get_SP)()
sewardjaf44c822007-11-25 14:01:38 +000035#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardje0744f02007-12-01 02:09:50 +000036#include "pub_tool_threadstate.h" // VG_INVALID_THREADID
sewardjaf44c822007-11-25 14:01:38 +000037
38
39// Local variables.
40
41static ULong s_segments_created_count;
42static ULong s_segments_alive_count;
43static ULong s_max_segments_alive_count;
44static Bool drd_trace_segment = False;
45
46
47// Function definitions.
48
49/**
50 * Note: creator and created may be equal.
51 */
52void sg_init(Segment* const sg,
53 DrdThreadId const creator,
54 DrdThreadId const created)
55{
56 Segment* creator_sg;
sewardje0744f02007-12-01 02:09:50 +000057 ThreadId vg_created = DrdThreadIdToVgThreadId(created);
sewardjaf44c822007-11-25 14:01:38 +000058
59 tl_assert(sg);
60 tl_assert(creator == DRD_INVALID_THREADID || IsValidDrdThreadId(creator));
61
62 creator_sg = (creator != DRD_INVALID_THREADID
63 ? thread_get_segment(creator) : 0);
64
65 sg->next = 0;
66 sg->prev = 0;
67
tom9e86a122008-01-02 10:07:44 +000068 if (vg_created != VG_INVALID_THREADID && VG_(get_SP)(vg_created) != 0)
sewardje0744f02007-12-01 02:09:50 +000069 sg->stacktrace = VG_(record_ExeContext)(vg_created, 0);
70 else
71 sg->stacktrace = 0;
sewardjaf44c822007-11-25 14:01:38 +000072
73 if (creator_sg)
74 vc_copy(&sg->vc, &creator_sg->vc);
75 else
76 vc_init(&sg->vc, 0, 0);
77 vc_increment(&sg->vc, created);
78 sg->bm = bm_new();
79
80 if (drd_trace_segment)
81 {
82 char msg[256];
83 VG_(snprintf)(msg, sizeof(msg),
84 "New segment for thread %d with vc ",
85 creator);
86 vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
87 &sg->vc);
88 VG_(message)(Vg_DebugMsg, "%s", msg);
89 }
90}
91
92void sg_cleanup(Segment* const sg)
93{
94 tl_assert(sg);
95 vc_cleanup(&sg->vc);
96 bm_delete(sg->bm);
97 sg->bm = 0;
98}
99
100Segment* sg_new(ThreadId const creator, ThreadId const created)
101{
102 Segment* sg;
103
104 s_segments_created_count++;
105 s_segments_alive_count++;
106 if (s_max_segments_alive_count < s_segments_alive_count)
107 s_max_segments_alive_count = s_segments_alive_count;
108
109 sg = VG_(malloc)(sizeof(*sg));
110 tl_assert(sg);
111 sg_init(sg, creator, created);
112 return sg;
113}
114
115void sg_delete(Segment* const sg)
116{
bart68edad52008-02-24 18:21:12 +0000117#if 1
118 if (sg_get_trace())
119 {
120 char msg[256];
121 VG_(snprintf)(msg, sizeof(msg),
122 "Discarding the segment with vector clock ");
123 vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
124 &sg->vc);
125 VG_(message)(Vg_DebugMsg, "%s", msg);
126 }
127#endif
128
sewardjaf44c822007-11-25 14:01:38 +0000129 s_segments_alive_count--;
130
131 tl_assert(sg);
132 sg_cleanup(sg);
133 VG_(free)(sg);
134}
135
136void sg_print(const Segment* const sg)
137{
138 tl_assert(sg);
139 VG_(printf)("vc: ");
140 vc_print(&sg->vc);
141 VG_(printf)("\n");
142 bm_print(sg->bm);
143}
144
145Bool sg_get_trace(void)
146{
147 return drd_trace_segment;
148}
149
150void sg_set_trace(Bool const trace_segment)
151{
152 tl_assert(trace_segment == False || trace_segment == True);
153 drd_trace_segment = trace_segment;
154}
155
156ULong sg_get_segments_created_count(void)
157{
158 return s_segments_created_count;
159}
160
161ULong sg_get_max_segments_alive_count(void)
162{
163 return s_max_segments_alive_count;
164}