blob: a83a4148f06d681245b9a578c6a848433f7028e8 [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
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)()
34#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardje0744f02007-12-01 02:09:50 +000035#include "pub_tool_threadstate.h" // VG_INVALID_THREADID
sewardjaf44c822007-11-25 14:01:38 +000036
37
38// Local variables.
39
40static ULong s_segments_created_count;
41static ULong s_segments_alive_count;
42static ULong s_max_segments_alive_count;
43static Bool drd_trace_segment = False;
44
45
46// Function definitions.
47
48/**
49 * Note: creator and created may be equal.
50 */
51void sg_init(Segment* const sg,
52 DrdThreadId const creator,
53 DrdThreadId const created)
54{
55 Segment* creator_sg;
sewardje0744f02007-12-01 02:09:50 +000056 ThreadId vg_created = DrdThreadIdToVgThreadId(created);
sewardjaf44c822007-11-25 14:01:38 +000057
58 tl_assert(sg);
59 tl_assert(creator == DRD_INVALID_THREADID || IsValidDrdThreadId(creator));
60
61 creator_sg = (creator != DRD_INVALID_THREADID
62 ? thread_get_segment(creator) : 0);
63
64 sg->next = 0;
65 sg->prev = 0;
66
sewardje0744f02007-12-01 02:09:50 +000067 if (vg_created != VG_INVALID_THREADID)
68 sg->stacktrace = VG_(record_ExeContext)(vg_created, 0);
69 else
70 sg->stacktrace = 0;
sewardjaf44c822007-11-25 14:01:38 +000071
72 if (creator_sg)
73 vc_copy(&sg->vc, &creator_sg->vc);
74 else
75 vc_init(&sg->vc, 0, 0);
76 vc_increment(&sg->vc, created);
77 sg->bm = bm_new();
78
79 if (drd_trace_segment)
80 {
81 char msg[256];
82 VG_(snprintf)(msg, sizeof(msg),
83 "New segment for thread %d with vc ",
84 creator);
85 vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
86 &sg->vc);
87 VG_(message)(Vg_DebugMsg, "%s", msg);
88 }
89}
90
91void sg_cleanup(Segment* const sg)
92{
93 tl_assert(sg);
94 vc_cleanup(&sg->vc);
95 bm_delete(sg->bm);
96 sg->bm = 0;
97}
98
99Segment* sg_new(ThreadId const creator, ThreadId const created)
100{
101 Segment* sg;
102
103 s_segments_created_count++;
104 s_segments_alive_count++;
105 if (s_max_segments_alive_count < s_segments_alive_count)
106 s_max_segments_alive_count = s_segments_alive_count;
107
108 sg = VG_(malloc)(sizeof(*sg));
109 tl_assert(sg);
110 sg_init(sg, creator, created);
111 return sg;
112}
113
114void sg_delete(Segment* const sg)
115{
116 s_segments_alive_count--;
117
118 tl_assert(sg);
119 sg_cleanup(sg);
120 VG_(free)(sg);
121}
122
123void sg_print(const Segment* const sg)
124{
125 tl_assert(sg);
126 VG_(printf)("vc: ");
127 vc_print(&sg->vc);
128 VG_(printf)("\n");
129 bm_print(sg->bm);
130}
131
132Bool sg_get_trace(void)
133{
134 return drd_trace_segment;
135}
136
137void sg_set_trace(Bool const trace_segment)
138{
139 tl_assert(trace_segment == False || trace_segment == True);
140 drd_trace_segment = trace_segment;
141}
142
143ULong sg_get_segments_created_count(void)
144{
145 return s_segments_created_count;
146}
147
148ULong sg_get_max_segments_alive_count(void)
149{
150 return s_max_segments_alive_count;
151}