2{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE RankNTypes #-}
4-----------------------------------------------------------------------------
6-- Module : Control.Distributed.Process.Debug
7-- Copyright : (c) Well-Typed / Tim Watson
8-- License : BSD3 (see the file LICENSE)
10-- Maintainer : Tim Watson <watson.timothy@gmail.com>
11-- Stability : experimental
12-- Portability : non-portable (requires concurrency)
14-- [Tracing/Debugging Facilities]
16-- Cloud Haskell provides a general purpose tracing mechanism, allowing a
17-- user supplied /tracer process/ to receive messages when certain classes of
18-- system events occur. It's possible to use this facility to aid in debugging
19-- and/or perform other diagnostic tasks to a program at runtime.
23-- Throughout the lifecycle of a local node, the distributed-process runtime
24-- generates /trace events/, describing internal runtime activities such as
25-- the spawning and death of processes, message sending, delivery and so on.
26-- See the 'MxEvent' type's documentation for a list of all the published
27-- event types, which correspond directly to the types of /management/ events.
28-- Users can additionally publish custom trace events in the form of
29-- 'MxLog' log messages or pass custom (i.e., completely user defined)
30-- event data using the 'traceMessage' function.
32-- All published traces are forwarded to a /tracer process/, which can be
33-- specified (and changed) at runtime using 'traceEnable'. Some pre-defined
34-- tracer processes are provided for conveniently printing to stderr, a log file
35-- or the GHC eventlog.
37-- If a tracer process crashes, no attempt is made to restart it.
39-- [Working with multiple tracer processes]
41-- The tracing facility only ever writes to a single tracer process. This
42-- invariant insulates the tracer controller and ensures a fast path for
43-- handling all trace events. /This/ module provides facilities for layering
44-- trace handlers using Cloud Haskell's built-in delegation primitives.
46-- The 'startTracer' function wraps the registered @tracer@ process with the
47-- supplied handler and also forwards trace events to the original tracer.
48-- The corresponding 'stopTracer' function terminates tracer processes in
49-- reverse of the order in which they were started, and re-registers the
50-- previous tracer process.
54-- The built in tracers provide a simple /logging/ facility that writes trace
55-- events out to either a log file, @stderr@ or the GHC eventlog. These tracers
56-- can be configured using environment variables, or specified manually using
57-- the 'traceEnable' function.
59-- When a new local node is started, the contents of several environment
60-- variables are checked to determine which default tracer process is selected.
61-- If none of these variables is set, a no-op tracer process is installed,
62-- which effectively ignores all trace messages. Note that in this case,
63-- trace events are still generated and passed through the system.
64-- Only one default tracer will be chosen - the first that contains a (valid)
65-- value. These environment variables, in the order they're examined, are:
67-- 1. @DISTRIBUTED_PROCESS_TRACE_FILE@
68-- This is checked for a valid file path. If it exists and the file can be
69-- opened for writing, all trace output will be directed thence. If the supplied
70-- path is invalid, or the file is unavailable for writing, this tracer will not
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-- By default, the built in tracers will ignore all trace events! In order to
82-- enable tracing the incoming 'MxEvent' stream, the @DISTRIBUTED_PROCESS_TRACE_FLAGS@
83-- environment variable accepts the following flags, which enable tracing specific
86-- * @p@ = trace the spawning of new processes
87-- * @d@ = trace the death of processes
88-- * @n@ = trace registration of names (i.e., named processes)
89-- * @u@ = trace un-registration of names (i.e., named processes)
90-- * @s@ = trace the sending of messages to other processes
91-- * @r@ = trace the receipt of messages from other processes
92-- * @l@ = trace node up/down events
94-- Users of the /simplelocalnet/ Cloud Haskell backend should also note that
95-- because the trace file option only supports trace output from a single node
96-- (so as to avoid interleaving), a file trace configured for the master node
97-- will prevent slaves from tracing to the file. They will need to fall back to
98-- the console or eventlog tracers instead, which can be accomplished by setting
99-- one of these environment variables /as well/, since the latter will only be
100-- selected on slaves (when the file tracer selection fails).
102-- Support for writing to the eventlog requires specific intervention to work,
103-- without which, written traces are silently dropped/ignored and no output will
104-- be generated. The GHC eventlog documentation provides information about
105-- enabling, viewing and working with event traces at
106-- <http://hackage.haskell.org/trac/ghc/wiki/EventLog>.
108module Control.Distributed.Process.Debug
109 ( -- * Exported Data Types
113 -- * Configuring Tracing
129 -- * Sending Custom Trace Data
133 -- * Working with remote nodes
135 , Remote.startTraceRelay
136 , Remote.setTraceFlagsRemote
137 -- * Built in tracers
144import Control.Applicative
145import Control.Distributed.Process.Internal.Primitives
154import Control.Distributed.Process.Internal.Types
158 , ProcessMonitorNotification(..)
160import Control.Distributed.Process.Management.Internal.Types
163import Control.Distributed.Process.Management.Internal.Trace.Types
169import Control.Distributed.Process.Management.Internal.Trace.Tracer
174import Control.Distributed.Process.Management.Internal.Trace.Primitives
175 ( withRegisteredTracer
189import qualified Control.Distributed.Process.Management.Internal.Trace.Remote as Remote
190import Control.Distributed.Process.Node
192import Control.Exception (SomeException)
194import Control.Monad.IO.Class (liftIO)
195import Control.Monad.Reader (ask)
196import Control.Monad.Catch (finally, try)
202--------------------------------------------------------------------------------
203-- Debugging/Tracing API --
204--------------------------------------------------------------------------------
206-- | Starts a new tracer, using the supplied trace function.
207-- Only one tracer can be registered at a time, however /this/ function overlays
208-- the registered tracer with the supplied handler, allowing the user to layer
209-- multiple tracers on top of one another, with trace events forwarded down
210-- through all the layers in turn. Once the top layer is stopped, the user
211-- is responsible for re-registering the original (prior) tracer pid before
212-- terminating. See 'withTracer' for a mechanism that handles that.
213startTracer :: (MxEvent -> Process ()) -> Process ProcessId
214startTracer handler = do
215 withRegisteredTracer $ \pid -> do
216 node <- processNode <$> ask
217 newPid <- liftIO $ forkProcess node $ traceProxy pid handler
218 enableTrace newPid -- invokes sync + registration
221-- | Evaluate @proc@ with tracing enabled via @handler@, and immediately
222-- disable tracing thereafter, before giving the result (or exception
223-- in case of failure).
224withTracer :: forall a.
225 (MxEvent -> Process ())
227 -> Process (Either SomeException a)
228withTracer handler proc = do
229 previous <- whereis "tracer"
230 tracer <- startTracer handler
232 (stopTracing tracer previous)
234 stopTracing :: ProcessId -> Maybe ProcessId -> Process ()
235 stopTracing tracer previousTracer = do
236 case previousTracer of
239 ref <- monitor tracer
240 send tracer MxTraceDisable
242 matchIf (\(ProcessMonitorNotification ref' _ _) -> ref == ref')
246-- | Evaluate @proc@ with the supplied flags enabled. Any previously set
247-- trace flags are restored immediately afterwards.
248withFlags :: forall a.
251 -> Process (Either SomeException a)
252withFlags flags proc = do
253 oldFlags <- getTraceFlags
254 finally (setTraceFlags flags >> try proc)
255 (setTraceFlags oldFlags)
257traceProxy :: ProcessId -> (MxEvent -> Process ()) -> Process ()
258traceProxy pid act = do
259 proxy pid $ \(ev :: MxEvent) ->
261 (MxTraceTakeover _) -> return False
262 MxTraceDisable -> die "disabled"
263 _ -> act ev >> return True
265-- | Stops a user supplied tracer started with 'startTracer'.
266-- Note that only one tracer process can be active at any given time.
267-- This process will stop the last process started with 'startTracer'.
268-- If 'startTracer' is called multiple times, successive calls to this
269-- function will stop the tracers in the reverse order which they were
272-- This function will never stop the system tracer (i.e., the tracer
273-- initially started when the node is created), therefore once all user
274-- supplied tracers (i.e., processes started via 'startTracer') have exited,
275-- subsequent calls to this function will have no effect.
277-- If the last tracer to have been registered was not started
278-- with 'startTracer' then the behaviour of this function is /undefined/.
279stopTracer :: Process ()
281 withRegisteredTracer $ \pid -> do
282 -- we need to avoid killing the initial (base) tracer, as
283 -- nothing we rely on having exactly 1 registered tracer
284 -- process at all times.
285 basePid <- whereis "tracer.initial"
286 case basePid == (Just pid) of
288 False -> send pid MxTraceDisable
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