Export VG_(discard_tranalsations_safely) to the tools. It is
only allowed to be called in certain contexts which is
enforced at runtime.
Change callgrind accordingly.
New header file pub_tool_transtab.h added.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14867 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/callgrind/main.c b/callgrind/main.c
index ad61d45..26e5c5d 100644
--- a/callgrind/main.c
+++ b/callgrind/main.c
@@ -37,6 +37,7 @@
 
 #include "pub_tool_threadstate.h"
 #include "pub_tool_gdbserver.h"
+#include "pub_tool_transtab.h"       // VG_(discard_translations_safely)
 
 #include "cg_branchpred.c"
 
@@ -1448,10 +1449,6 @@
     CLG_(zero_cost)( CLG_(sets).full, CLG_(current_state).cost );
 }
 
-/* Ups, this can go very wrong...
-   FIXME: We should export this function or provide other means to get a handle */
-extern void VG_(discard_translations) ( Addr start, ULong range, const HChar* who );
-
 void CLG_(set_instrument_state)(const HChar* reason, Bool state)
 {
   if (CLG_(instrument_state) == state) {
@@ -1463,7 +1460,7 @@
   CLG_DEBUG(2, "%s: Switching instrumentation %s ...\n",
 	   reason, state ? "ON" : "OFF");
 
-  VG_(discard_translations)( (Addr)0x1000, (ULong) ~0xfffl, "callgrind");
+  VG_(discard_translations_safely)( (Addr)0x1000, ~(SizeT)0xfff, "callgrind");
 
   /* reset internal state: call stacks, simulator */
   CLG_(forall_threads)(unwind_thread);
diff --git a/coregrind/m_tooliface.c b/coregrind/m_tooliface.c
index 00006f2..40d8325 100644
--- a/coregrind/m_tooliface.c
+++ b/coregrind/m_tooliface.c
@@ -31,6 +31,7 @@
 
 #include "pub_core_basics.h"
 #include "pub_core_tooliface.h"
+#include "pub_core_transtab.h"     /* VG_(ok_to_discard_translations) */
 
 // The core/tool dictionary of functions (initially zeroed, as we want it)
 VgToolInterface VG_(tdict);
@@ -268,12 +269,27 @@
    VG_(tdict).tool_print_debug_usage       = debug_usage;
 }
 
+/* The tool's function for handling client requests. */
+static Bool (*tool_handle_client_request_func)(ThreadId, UWord *, UWord *);
+
+static Bool wrap_tool_handle_client_request(ThreadId tid, UWord *arg1,
+                                            UWord *arg2)
+{
+   Bool ret;
+   VG_(ok_to_discard_translations) = True;
+   ret = tool_handle_client_request_func(tid, arg1, arg2);
+   VG_(ok_to_discard_translations) = False;
+   return ret;
+}
+
 void VG_(needs_client_requests)(
    Bool (*handle)(ThreadId, UWord*, UWord*)
 )
 {
    VG_(needs).client_requests = True;
-   VG_(tdict).tool_handle_client_request = handle;
+   tool_handle_client_request_func = handle;   /* Stash away */
+   /* Register the wrapper function */
+   VG_(tdict).tool_handle_client_request = wrap_tool_handle_client_request;
 }
 
 void VG_(needs_syscall_wrapper)(
diff --git a/coregrind/m_transtab.c b/coregrind/m_transtab.c
index 4fcd13d..ec36c43 100644
--- a/coregrind/m_transtab.c
+++ b/coregrind/m_transtab.c
@@ -2022,6 +2022,17 @@
    }
 }
 
+/* Whether or not tools may discard translations. */
+Bool  VG_(ok_to_discard_translations) = False;
+
+/* This function is exported to tools which can use it to discard
+   translations, provided it is safe to do so. */
+void VG_(discard_translations_safely) ( Addr  start, SizeT len,
+                                        const HChar* who )
+{
+   vg_assert(VG_(ok_to_discard_translations));
+   VG_(discard_translations)(start, len, who);
+}
 
 /*------------------------------------------------------------*/
 /*--- AUXILIARY: the unredirected TT/TC                    ---*/
diff --git a/coregrind/pub_core_transtab.h b/coregrind/pub_core_transtab.h
index 892a0b1..67fd9d8 100644
--- a/coregrind/pub_core_transtab.h
+++ b/coregrind/pub_core_transtab.h
@@ -38,6 +38,7 @@
 //--------------------------------------------------------------------
 
 #include "pub_core_transtab_asm.h"
+#include "pub_tool_transtab.h"
 #include "libvex.h"                   // VexGuestExtents
 
 /* The fast-cache for tt-lookup.  Unused entries are denoted by .guest
@@ -129,6 +130,9 @@
 
 extern ULong VG_(get_SB_profile) ( SBProfEntry tops[], UInt n_tops );
 
+//  Exported variables
+extern Bool  VG_(ok_to_discard_translations);
+
 #endif   // __PUB_CORE_TRANSTAB_H
 
 /*--------------------------------------------------------------------*/
diff --git a/include/Makefile.am b/include/Makefile.am
index 520b718..9e5d2b4 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -37,6 +37,7 @@
 	pub_tool_stacktrace.h 		\
 	pub_tool_threadstate.h 		\
 	pub_tool_tooliface.h 		\
+	pub_tool_transtab.h 		\
 	pub_tool_vki.h			\
 	pub_tool_vkiscnums.h		\
 	pub_tool_vkiscnums_asm.h	\
diff --git a/include/pub_tool_transtab.h b/include/pub_tool_transtab.h
new file mode 100644
index 0000000..16908b3
--- /dev/null
+++ b/include/pub_tool_transtab.h
@@ -0,0 +1,43 @@
+
+/*--------------------------------------------------------------------*/
+/*--- The translation table and cache.                             ---*/
+/*---                                          pub_tool_transtab.h ---*/
+/*--------------------------------------------------------------------*/
+
+/*
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2014-2014 Florian Krohm   (florian@eich-krohm.de)
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307, USA.
+
+   The GNU General Public License is contained in the file COPYING.
+*/
+
+#ifndef __PUB_TOOL_TRANSTAB_H
+#define __PUB_TOOL_TRANSTAB_H
+
+#include "pub_tool_basics.h"   // VG_ macro and primitive types
+
+void VG_(discard_translations_safely) ( Addr  start, SizeT len,
+                                        const HChar* who );
+
+#endif   // __PUB_TOOL_TRANSTAB_H
+
+/*--------------------------------------------------------------------*/
+/*--- end                                                          ---*/
+/*--------------------------------------------------------------------*/