blob: 6d884349e063ba0a870b3c4e798f2a2cedaf3d5c [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
bart86562bd2009-02-16 19:43:56 +00002 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00003
bartd4bab992013-10-04 05:55:30 +00004 Copyright (C) 2006-2013 Bart Van Assche <bvanassche@acm.org>.
sewardjaf44c822007-11-25 14:01:38 +00005
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307, USA.
20
21 The GNU General Public License is contained in the file COPYING.
22*/
23
24
25#include "drd_suppression.h"
sewardjaf44c822007-11-25 14:01:38 +000026#include "pub_drd_bitmap.h"
sewardj85642922008-01-14 11:54:56 +000027#include "pub_tool_libcassert.h" // tl_assert()
sewardjaf44c822007-11-25 14:01:38 +000028#include "pub_tool_stacktrace.h" // VG_(get_and_pp_StackTrace)()
29#include "pub_tool_threadstate.h" // VG_(get_running_tid)()
sewardj85642922008-01-14 11:54:56 +000030#include "pub_tool_libcprint.h" // Vg_DebugMsg
sewardjaf44c822007-11-25 14:01:38 +000031
32
bart1335ecc2009-02-14 16:10:53 +000033/* Global variables. */
sewardjaf44c822007-11-25 14:01:38 +000034
bart1335ecc2009-02-14 16:10:53 +000035Bool DRD_(g_any_address_traced) = False;
sewardjaf44c822007-11-25 14:01:38 +000036
37
bart1335ecc2009-02-14 16:10:53 +000038/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000039
bart2e2f2132012-01-16 13:08:05 +000040static struct bitmap* s_suppressed;
41static struct bitmap* s_traced;
42static Bool s_trace_suppression;
bart1335ecc2009-02-14 16:10:53 +000043
44
45/* Function definitions. */
46
47void DRD_(suppression_set_trace)(const Bool trace_suppression)
sewardjaf44c822007-11-25 14:01:38 +000048{
bart2e2f2132012-01-16 13:08:05 +000049 s_trace_suppression = trace_suppression;
sewardjaf44c822007-11-25 14:01:38 +000050}
51
bart1335ecc2009-02-14 16:10:53 +000052void DRD_(suppression_init)(void)
sewardjaf44c822007-11-25 14:01:38 +000053{
bart2e2f2132012-01-16 13:08:05 +000054 tl_assert(s_suppressed == 0);
55 tl_assert(s_traced == 0);
56 s_suppressed = DRD_(bm_new)();
57 s_traced = DRD_(bm_new)();
58 tl_assert(s_suppressed);
59 tl_assert(s_traced);
sewardjaf44c822007-11-25 14:01:38 +000060}
61
bart1335ecc2009-02-14 16:10:53 +000062void DRD_(start_suppression)(const Addr a1, const Addr a2,
florian19f91bb2012-11-10 22:29:54 +000063 const HChar* const reason)
sewardjaf44c822007-11-25 14:01:38 +000064{
bart2e2f2132012-01-16 13:08:05 +000065 if (s_trace_suppression)
sewardj1e29ebc2009-07-15 14:49:17 +000066 VG_(message)(Vg_DebugMsg, "start suppression of 0x%lx sz %ld (%s)\n",
bartbedfd232009-03-26 19:07:15 +000067 a1, a2 - a1, reason);
sewardjaf44c822007-11-25 14:01:38 +000068
barta3003982010-09-08 16:29:17 +000069 tl_assert(a1 <= a2);
bart2e2f2132012-01-16 13:08:05 +000070 DRD_(bm_access_range_store)(s_suppressed, a1, a2);
sewardjaf44c822007-11-25 14:01:38 +000071}
72
bart1335ecc2009-02-14 16:10:53 +000073void DRD_(finish_suppression)(const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +000074{
bart2e2f2132012-01-16 13:08:05 +000075 if (s_trace_suppression) {
sewardj1e29ebc2009-07-15 14:49:17 +000076 VG_(message)(Vg_DebugMsg, "finish suppression of 0x%lx sz %ld\n",
bartbedfd232009-03-26 19:07:15 +000077 a1, a2 - a1);
bart31b983d2010-02-21 14:52:59 +000078 VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(), 12);
bartbedfd232009-03-26 19:07:15 +000079 }
sewardjaf44c822007-11-25 14:01:38 +000080
barta3003982010-09-08 16:29:17 +000081 tl_assert(a1 <= a2);
bart2e2f2132012-01-16 13:08:05 +000082 DRD_(bm_clear_store)(s_suppressed, a1, a2);
sewardjaf44c822007-11-25 14:01:38 +000083}
84
85/**
86 * Return true if data race detection suppression has been requested for all
87 * bytes in the range a1 .. a2 - 1 inclusive. Return false in case the range
88 * is only partially suppressed or not suppressed at all.
89 */
bart1335ecc2009-02-14 16:10:53 +000090Bool DRD_(is_suppressed)(const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +000091{
bart2e2f2132012-01-16 13:08:05 +000092 return DRD_(bm_has)(s_suppressed, a1, a2, eStore);
sewardjaf44c822007-11-25 14:01:38 +000093}
94
95/**
96 * Return true if data race detection suppression has been requested for any
97 * of the bytes in the range a1 .. a2 - 1 inclusive. Return false in case none
98 * of the bytes in the specified range is suppressed.
99 */
bart1335ecc2009-02-14 16:10:53 +0000100Bool DRD_(is_any_suppressed)(const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +0000101{
bart2e2f2132012-01-16 13:08:05 +0000102 return DRD_(bm_has_any_store)(s_suppressed, a1, a2);
sewardjaf44c822007-11-25 14:01:38 +0000103}
104
bartb00ac4c2010-03-07 20:05:23 +0000105void DRD_(mark_hbvar)(const Addr a1)
106{
bart2e2f2132012-01-16 13:08:05 +0000107 DRD_(bm_access_range_load)(s_suppressed, a1, a1 + 1);
bartb00ac4c2010-03-07 20:05:23 +0000108}
109
110Bool DRD_(range_contains_suppression_or_hbvar)(const Addr a1, const Addr a2)
111{
bart2e2f2132012-01-16 13:08:05 +0000112 return DRD_(bm_has_any_access)(s_suppressed, a1, a2);
bartb00ac4c2010-03-07 20:05:23 +0000113}
114
bart41a378a2012-01-24 18:39:29 +0000115/**
116 * Start tracing memory accesses in the range [a1,a2). If persistent == True,
117 * keep tracing even after memory deallocation and reallocation.
118 */
119void DRD_(start_tracing_address_range)(const Addr a1, const Addr a2,
120 const Bool persistent)
bart005dc972008-03-29 14:42:59 +0000121{
barta3003982010-09-08 16:29:17 +0000122 tl_assert(a1 <= a2);
bart005dc972008-03-29 14:42:59 +0000123
bart41a378a2012-01-24 18:39:29 +0000124 if (s_trace_suppression)
125 VG_(message)(Vg_DebugMsg, "start_tracing(0x%lx, %ld) %s\n",
126 a1, a2 - a1, persistent ? "persistent" : "non-persistent");
127
bart2e2f2132012-01-16 13:08:05 +0000128 DRD_(bm_access_range_load)(s_traced, a1, a2);
bart41a378a2012-01-24 18:39:29 +0000129 if (persistent)
130 DRD_(bm_access_range_store)(s_traced, a1, a2);
131 if (!DRD_(g_any_address_traced) && a1 < a2)
bartbedfd232009-03-26 19:07:15 +0000132 DRD_(g_any_address_traced) = True;
bart005dc972008-03-29 14:42:59 +0000133}
134
bart41a378a2012-01-24 18:39:29 +0000135/**
136 * Stop tracing memory accesses in the range [a1,a2).
137 */
bart1335ecc2009-02-14 16:10:53 +0000138void DRD_(stop_tracing_address_range)(const Addr a1, const Addr a2)
bart005dc972008-03-29 14:42:59 +0000139{
barta3003982010-09-08 16:29:17 +0000140 tl_assert(a1 <= a2);
bart005dc972008-03-29 14:42:59 +0000141
bart41a378a2012-01-24 18:39:29 +0000142 if (s_trace_suppression)
143 VG_(message)(Vg_DebugMsg, "stop_tracing(0x%lx, %ld)\n",
144 a1, a2 - a1);
145
146 if (DRD_(g_any_address_traced)) {
147 DRD_(bm_clear)(s_traced, a1, a2);
148 DRD_(g_any_address_traced) = DRD_(bm_has_any_load_g)(s_traced);
149 }
bart005dc972008-03-29 14:42:59 +0000150}
151
bart1335ecc2009-02-14 16:10:53 +0000152Bool DRD_(is_any_traced)(const Addr a1, const Addr a2)
bart005dc972008-03-29 14:42:59 +0000153{
bart41a378a2012-01-24 18:39:29 +0000154 return DRD_(bm_has_any_access)(s_traced, a1, a2);
bart005dc972008-03-29 14:42:59 +0000155}
156
bart41a378a2012-01-24 18:39:29 +0000157/**
158 * Stop using the memory range [a1,a2). Stop tracing memory accesses to
159 * non-persistent address ranges.
160 */
bart1335ecc2009-02-14 16:10:53 +0000161void DRD_(suppression_stop_using_mem)(const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +0000162{
bart2e2f2132012-01-16 13:08:05 +0000163 if (s_trace_suppression) {
bartbedfd232009-03-26 19:07:15 +0000164 Addr b;
bart2e2f2132012-01-16 13:08:05 +0000165 for (b = a1; b < a2; b++) {
166 if (DRD_(bm_has_1)(s_suppressed, b, eStore)) {
bartbedfd232009-03-26 19:07:15 +0000167 VG_(message)(Vg_DebugMsg,
168 "stop_using_mem(0x%lx, %ld) finish suppression of"
sewardj1e29ebc2009-07-15 14:49:17 +0000169 " 0x%lx\n", a1, a2 - a1, b);
bartbedfd232009-03-26 19:07:15 +0000170 }
sewardjaf44c822007-11-25 14:01:38 +0000171 }
bartbedfd232009-03-26 19:07:15 +0000172 }
173 tl_assert(a1);
barta3003982010-09-08 16:29:17 +0000174 tl_assert(a1 <= a2);
bart2e2f2132012-01-16 13:08:05 +0000175 DRD_(bm_clear)(s_suppressed, a1, a2);
bart41a378a2012-01-24 18:39:29 +0000176 DRD_(bm_clear_load)(s_traced, a1, a2);
sewardjaf44c822007-11-25 14:01:38 +0000177}