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
157import Control.Distributed.Process.Internal.Types
161 , ProcessMonitorNotification(..)
163import Control.Distributed.Process.Management.Internal.Types
166import Control.Distributed.Process.Management.Internal.Trace.Types
172import Control.Distributed.Process.Management.Internal.Trace.Tracer
177import Control.Distributed.Process.Management.Internal.Trace.Primitives
178 ( withRegisteredTracer
192import qualified Control.Distributed.Process.Management.Internal.Trace.Remote as Remote
193import Control.Distributed.Process.Node
195import Control.Exception (SomeException)
197import Control.Monad.IO.Class (liftIO)
198import Control.Monad.Reader (ask)
202#if ! MIN_VERSION_base(4,6,0)
203import Prelude hiding (catch)
206--------------------------------------------------------------------------------
207-- Debugging/Tracing API --
208--------------------------------------------------------------------------------
210-- | Starts a new tracer, using the supplied trace function.
211-- Only one tracer can be registered at a time, however /this/ function overlays
212-- the registered tracer with the supplied handler, allowing the user to layer
213-- multiple tracers on top of one another, with trace events forwarded down
214-- through all the layers in turn. Once the top layer is stopped, the user
215-- is responsible for re-registering the original (prior) tracer pid before
216-- terminating. See 'withTracer' for a mechanism that handles that.
217startTracer :: (MxEvent -> Process ()) -> Process ProcessId
218startTracer handler = do
219 withRegisteredTracer $ \pid -> do
220 node <- processNode <$> ask
221 newPid <- liftIO $ forkProcess node $ traceProxy pid handler
222 enableTrace newPid -- invokes sync + registration
225-- | Evaluate @proc@ with tracing enabled via @handler@, and immediately
226-- disable tracing thereafter, before giving the result (or exception
227-- in case of failure).
228withTracer :: forall a.
229 (MxEvent -> Process ())
231 -> Process (Either SomeException a)
232withTracer handler proc = do
233 previous <- whereis "tracer"
234 tracer <- startTracer handler
236 (stopTracing tracer previous)
238 stopTracing :: ProcessId -> Maybe ProcessId -> Process ()
239 stopTracing tracer previousTracer = do
240 case previousTracer of
243 ref <- monitor tracer
244 send tracer MxTraceDisable
246 matchIf (\(ProcessMonitorNotification ref' _ _) -> ref == ref')
250-- | Evaluate @proc@ with the supplied flags enabled. Any previously set
251-- trace flags are restored immediately afterwards.
252withFlags :: forall a.
255 -> Process (Either SomeException a)
256withFlags flags proc = do
257 oldFlags <- getTraceFlags
258 finally (setTraceFlags flags >> try proc)
259 (setTraceFlags oldFlags)
261traceProxy :: ProcessId -> (MxEvent -> Process ()) -> Process ()
262traceProxy pid act = do
263 proxy pid $ \(ev :: MxEvent) ->
265 (MxTraceTakeover _) -> return False
266 MxTraceDisable -> die "disabled"
267 _ -> act ev >> return True
269-- | Stops a user supplied tracer started with 'startTracer'.
270-- Note that only one tracer process can be active at any given time.
271-- This process will stop the last process started with 'startTracer'.
272-- If 'startTracer' is called multiple times, successive calls to this
273-- function will stop the tracers in the reverse order which they were
276-- This function will never stop the system tracer (i.e., the tracer
277-- initially started when the node is created), therefore once all user
278-- supplied tracers (i.e., processes started via 'startTracer') have exited,
279-- subsequent calls to this function will have no effect.
281-- If the last tracer to have been registered was not started
282-- with 'startTracer' then the behaviour of this function is /undefined/.
283stopTracer :: Process ()
285 withRegisteredTracer $ \pid -> do
286 -- we need to avoid killing the initial (base) tracer, as
287 -- nothing we rely on having exactly 1 registered tracer
288 -- process at all times.
289 basePid <- whereis "tracer.initial"
290 case basePid == (Just pid) of
292 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