blob: cb4b979ce9b6ceb0fa19741b8fe9af709080f0a6 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
4 Copyright (C) 2006-2007 Bart Van Assche
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
26#include "pub_tool_basics.h" // Addr, SizeT
27#include "pub_tool_errormgr.h" // VG_(unique_error)()
28#include "pub_tool_libcassert.h" // tl_assert()
29#include "pub_tool_libcbase.h" // VG_(strlen)()
30#include "pub_tool_libcprint.h" // VG_(printf)()
31#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
32#include "drd_error.h"
33#include "drd_segment.h"
34#include "drd_thread.h"
35
36
37// Local variables.
38
39static ULong s_segments_created_count;
40static ULong s_segments_alive_count;
41static ULong s_max_segments_alive_count;
42static Bool drd_trace_segment = False;
43
44
45// Function definitions.
46
47/**
48 * Note: creator and created may be equal.
49 */
50void sg_init(Segment* const sg,
51 DrdThreadId const creator,
52 DrdThreadId const created)
53{
54 Segment* creator_sg;
55
56 tl_assert(sg);
57 tl_assert(creator == DRD_INVALID_THREADID || IsValidDrdThreadId(creator));
58
59 creator_sg = (creator != DRD_INVALID_THREADID
60 ? thread_get_segment(creator) : 0);
61
62 sg->next = 0;
63 sg->prev = 0;
64
65 sg->stacktrace = VG_(record_ExeContext)(created, 0);
66
67 if (creator_sg)
68 vc_copy(&sg->vc, &creator_sg->vc);
69 else
70 vc_init(&sg->vc, 0, 0);
71 vc_increment(&sg->vc, created);
72 sg->bm = bm_new();
73
74 if (drd_trace_segment)
75 {
76 char msg[256];
77 VG_(snprintf)(msg, sizeof(msg),
78 "New segment for thread %d with vc ",
79 creator);
80 vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
81 &sg->vc);
82 VG_(message)(Vg_DebugMsg, "%s", msg);
83 }
84}
85
86void sg_cleanup(Segment* const sg)
87{
88 tl_assert(sg);
89 vc_cleanup(&sg->vc);
90 bm_delete(sg->bm);
91 sg->bm = 0;
92}
93
94Segment* sg_new(ThreadId const creator, ThreadId const created)
95{
96 Segment* sg;
97
98 s_segments_created_count++;
99 s_segments_alive_count++;
100 if (s_max_segments_alive_count < s_segments_alive_count)
101 s_max_segments_alive_count = s_segments_alive_count;
102
103 sg = VG_(malloc)(sizeof(*sg));
104 tl_assert(sg);
105 sg_init(sg, creator, created);
106 return sg;
107}
108
109void sg_delete(Segment* const sg)
110{
111 s_segments_alive_count--;
112
113 tl_assert(sg);
114 sg_cleanup(sg);
115 VG_(free)(sg);
116}
117
118void sg_print(const Segment* const sg)
119{
120 tl_assert(sg);
121 VG_(printf)("vc: ");
122 vc_print(&sg->vc);
123 VG_(printf)("\n");
124 bm_print(sg->bm);
125}
126
127Bool sg_get_trace(void)
128{
129 return drd_trace_segment;
130}
131
132void sg_set_trace(Bool const trace_segment)
133{
134 tl_assert(trace_segment == False || trace_segment == True);
135 drd_trace_segment = trace_segment;
136}
137
138ULong sg_get_segments_created_count(void)
139{
140 return s_segments_created_count;
141}
142
143ULong sg_get_max_segments_alive_count(void)
144{
145 return s_max_segments_alive_count;
146}