1-----------------------------------------------------------------------------
3-- Module : Control.Distributed.Process.Debug
4-- Copyright : (c) Well-Typed / Tim Watson
5-- License : BSD3 (see the file LICENSE)
7-- Maintainer : Tim Watson <watson.timothy@gmail.com>
8-- Stability : experimental
9-- Portability : non-portable (requires concurrency)
11-- [Tracing/Debugging Facilities]
13-- Cloud Haskell provides a general purpose tracing mechanism, allowing a
14-- user supplied /tracer process/ to receive messages when certain classes of
15-- system events occur. It's possible to use this facility to aid in debugging
16-- and/or perform other diagnostic tasks to a program at runtime.
20-- Tracing is disabled by default, because it carries a relatively small, but
21-- tangible cost. To enable tracing, the environment variable
22-- @DISTRIBUTED_PROCESS_TRACE_ENABLED@ must be set to some value (the contents
23-- are ignored). When the environment variable /is/ set, the system will
24-- generate /trace events/ in various circumstances - see 'TraceEvent' for a
25-- list of all the published events. A user can additionally publish custom
26-- trace events in the form of 'TraceEvLog' log messages or pass custom
27-- (i.e., completely user defined) event data using the 'traceMessage' function.
28-- If the environment variable is not set, no trace events will ever be
31-- All published traces are forwarded to a /tracer process/, which can be
32-- specified (and changed) at runtime using 'traceEnable'. Some pre-defined
33-- tracer processes are provided for conveniently printing to stderr, a log file
34-- or the GHC eventlog.
36-- If a tracer process crashes, no attempt is made to restart it.
38-- [Working with multiple tracer processes]
40-- The system tracing facility only ever writes to a single tracer process. This
41-- invariant insulates the tracer controller and ensures a fast path for
42-- handling all trace events. /This/ module provides facilities for layering
43-- trace handlers using Cloud Haskell's built-in delegation primitives.
45-- The 'startTracer' function wraps the registered @tracer@ process with the
46-- supplied handler and also forwarding trace events to the original tracer.
47-- The corresponding 'stopTracer' function terminates tracer processes in
48-- reverse of the order in which they were started, and re-establishes the
49-- previous tracer process.
53-- The built in tracers provide a simple /logging/ facility that writes trace
54-- events out to either a log file, @stderr@ or the GHC eventlog. These tracers
55-- can be configured using environment variables, or specified manually using
56-- the 'traceEnable' function. The base tracer process cannot be
58-- When a new local node is started, the contents of several environment
59-- variables are checked to determine how the default tracer process
60-- will handle trace events. If none of these variables is set, then the
61-- trace events will be effectively ignored, although they will still be
62-- generated and passed through the system. Only one configuration will be
63-- chosen - the first that contains a (valid) value. These environment
64-- variables, in the order they're checked, are:
66-- 1. @DISTRIBUTED_PROCESS_TRACE_FILE@
67-- This is checked for a valid file path. If it exists and the file can be
68-- opened for writing, all trace output will be directed thence. If the supplied
69-- path is invalid, or the file is unavailable for writing - e.g., because
70-- another node has already started tracing to it - then this tracer will be
73-- 2. @DISTRIBUTED_PROCESS_TRACE_CONSOLE@
74-- This is checked for /any/ non-empty value. If set, then all trace output will
75-- be directed to the system logger process.
77-- 3. @DISTRIBUTED_PROCESS_TRACE_EVENTLOG@
78-- This is checked for /any/ non-empty value. If set, all internal traces are
79-- written to the GHC eventlog.
81-- Users of the /simplelocalnet/ Cloud Haskell backend should also note that
82-- because the trace file option only supports trace output from a single node
83-- (so as to avoid interleaving), a file trace configured for the master node
84-- will prevent slaves from tracing to the file and will need to fall back to
85-- the console or eventlog tracers instead.
87-- Support for writing to the eventlog requires specific intervention to work,
88-- without which, written traces are silently dropped/ignored and no output will
89-- be generated. The GHC eventlog documentation provides information about
90-- enabling, viewing and working with event traces at
91-- <http://hackage.haskell.org/trac/ghc/wiki/EventLog>.
93module Control.Distributed.Process.Debug
94 ( -- * Exported Data Types
99 -- * Configuring Tracing
116 -- * Sending Trace Data
120 -- * Working with remote nodes
122 , Remote.startTraceRelay
123 , Remote.setTraceFlagsRemote
124 -- * Built in tracers
131import Control.Applicative ((<$>))
132import Control.Distributed.Process.Internal.Primitives
147import Control.Distributed.Process.Internal.Spawn
150import Control.Distributed.Process.Internal.Types
157 , ProcessMonitorNotification(..)
160import Control.Distributed.Process.Internal.Trace.Types
168import Control.Distributed.Process.Internal.Trace.Tracer
173import Control.Distributed.Process.Internal.Trace.Primitives
174 ( withRegisteredTracer
189import qualified Control.Distributed.Process.Internal.Trace.Remote as Remote
190import Control.Distributed.Process.Node
191import Control.Distributed.Process.Serializable
193import Control.Exception (SomeException)
195import Control.Monad.IO.Class (liftIO)
196import Control.Monad.Reader (ask)
200#if ! MIN_VERSION_base(4,6,0)
201import Prelude hiding (catch)
204--------------------------------------------------------------------------------
205-- Debugging/Tracing API --
206--------------------------------------------------------------------------------
208-- | Starts a new tracer, using the supplied trace function.
209-- Only one tracer can be registered at a time, however /this/ function overlays
210-- the registered tracer with the supplied handler, allowing the user to layer
211-- multiple tracers on top of one another, with trace events forwarded down
212-- through all the layers in turn. Once the top layer is stopped, the user
213-- is responsible for re-registering the original (prior) tracer pid before
214-- terminating. See 'withTracer' for a mechanism that handles that.
215startTracer :: (TraceEvent -> Process ()) -> Process ProcessId
216startTracer handler = do
217 withRegisteredTracer $ \pid -> do
218 node <- processNode <$> ask
219 newPid <- liftIO $ forkProcess node $ traceProxy pid handler
220 enableTrace newPid -- invokes sync + registration
223-- | Evaluate @proc@ with tracing enabled via @handler@, and immediately
224-- disable tracing thereafter, before giving the result (or exception
225-- in case of failure).
226withTracer :: forall a.
227 (TraceEvent -> Process ())
229 -> Process (Either SomeException a)
230withTracer handler proc = do
231 previous <- whereis "tracer"
232 tracer <- startTracer handler
234 (stopTracing tracer previous)
236 stopTracing :: ProcessId -> Maybe ProcessId -> Process ()
237 stopTracing tracer previousTracer = do
238 case previousTracer of
241 ref <- monitor tracer
242 send tracer TraceEvDisable
244 matchIf (\(ProcessMonitorNotification ref' _ _) -> ref == ref')
248-- | Evaluate @proc@ with the supplied flags enabled. Any previously set
249-- trace flags are restored immediately afterwards.
250withFlags :: forall a.
253 -> Process (Either SomeException a)
254withFlags flags proc = do
255 oldFlags <- getTraceFlags
256 finally (setTraceFlags flags >> try proc)
257 (setTraceFlags oldFlags)
259traceProxy :: ProcessId -> (TraceEvent -> Process ()) -> Process ()
260traceProxy pid act = do
261 proxy pid $ \(ev :: TraceEvent) ->
263 (TraceEvTakeover p) -> return False
264 TraceEvDisable -> die "disabled"
265 _ -> act ev >> return True
267-- | Stops a user supplied tracer started with 'startTracer'.
268-- Note that only one tracer process can be active at any given time.
269-- This process will stop the last process started with 'startTracer'.
270-- If 'startTracer' is called multiple times, successive calls to this
271-- function will stop the tracers in the reverse order which they were
274-- This function will never stop the system tracer (i.e., the tracer
275-- initially started when the node is created), therefore once all user
276-- supplied tracers (i.e., processes started via 'startTracer') have exited,
277-- subsequent calls to this function will have no effect.
279-- If tracing support is disabled for the node, this function will also
280-- have no effect. If the last tracer to have been registered was not started
281-- with 'startTracer' then the behaviour of this function is /undefined/.
282stopTracer :: Process ()
284 withRegisteredTracer $ \pid -> do
285 -- we need to avoid killing the initial (base) tracer, as
286 -- nothing we rely on having exactly 1 registered tracer
287 -- process at all times.
288 basePid <- whereis "tracer.initial"
289 case basePid == (Just pid) of
291 False -> send pid TraceEvDisable
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list