summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjim@bo <jim@bo>2018-06-21 17:02:47 -0400
committerjim@bo <jim@bo>2018-06-21 17:02:47 -0400
commit217823867e3874cb4d3d8d619bc192aaf6c78028 (patch)
treea4225fa927137961ccae4a0e824f5aaf5a361814 /src
parentf0f2bd11e0fc53ee0442dd110ff0a297716f1eda (diff)
DPut Trace Variations
tput - like dput, but works in any Applicative dtrace - like trace, but takes DebugTag
Diffstat (limited to 'src')
-rw-r--r--src/DPut.hs8
-rw-r--r--src/Data/PacketQueue.hs38
-rw-r--r--src/Network/Tox/Crypto/Handlers.hs6
3 files changed, 33 insertions, 19 deletions
diff --git a/src/DPut.hs b/src/DPut.hs
index 52714086..1b87eb93 100644
--- a/src/DPut.hs
+++ b/src/DPut.hs
@@ -27,6 +27,14 @@ dput tag msg = liftIO $ debugM (appName <.> show tag) msg
27dputB :: MonadIO m => DebugTag -> B.ByteString -> m () 27dputB :: MonadIO m => DebugTag -> B.ByteString -> m ()
28dputB tag msg = liftIO $ debugM (appName <.> show tag) (T.unpack . T.decodeUtf8 $ msg) 28dputB tag msg = liftIO $ debugM (appName <.> show tag) (T.unpack . T.decodeUtf8 $ msg)
29 29
30-- | Trace version of 'dput' works in arbitrary monad, using unsafePerformIO.
31tput :: Applicative m => DebugTag -> String -> m ()
32tput tag msg = (unsafePerformIO $ dput tag msg) `seq` pure ()
33
34-- | like 'trace' but parameterized with 'DebugTag'
35dtrace :: DebugTag -> String -> a -> a
36dtrace tag msg result = (unsafePerformIO $ dput tag msg) `seq` result
37
30setTagLevel :: Priority -> DebugTag -> IO () 38setTagLevel :: Priority -> DebugTag -> IO ()
31setTagLevel level tag = updateGlobalLogger (appName <.> show tag) (setLevel level) 39setTagLevel level tag = updateGlobalLogger (appName <.> show tag) (setLevel level)
32 40
diff --git a/src/Data/PacketQueue.hs b/src/Data/PacketQueue.hs
index e0221f5a..8182706e 100644
--- a/src/Data/PacketQueue.hs
+++ b/src/Data/PacketQueue.hs
@@ -35,7 +35,7 @@ import Control.Applicative
35import Data.Word 35import Data.Word
36import Data.Array.MArray 36import Data.Array.MArray
37import Data.Maybe 37import Data.Maybe
38import Debug.Trace 38import DPut
39 39
40data PacketQueue a = PacketQueue 40data PacketQueue a = PacketQueue
41 { pktq :: TArray Word32 (Maybe a) 41 { pktq :: TArray Word32 (Maybe a)
@@ -234,15 +234,18 @@ peekPacket getExtra q@(PacketOutQueue { pktoInPQ, pktoOutPQ, pktoPacketNo, pktoT
234 let arrayEmpty :: MArray a e m => a Word32 e -> m Bool 234 let arrayEmpty :: MArray a e m => a Word32 e -> m Bool
235 arrayEmpty ar = do (lowB,highB) <- getBounds ar 235 arrayEmpty ar = do (lowB,highB) <- getBounds ar
236 let result= lowB > highB 236 let result= lowB > highB
237 return $ trace ("arrayEmpty result=" ++ show result 237 tput XNetCrypto
238 ("arrayEmpty result=" ++ show result
238 ++ " lowB=" ++ show lowB 239 ++ " lowB=" ++ show lowB
239 ++ " highB = " ++ show highB 240 ++ " highB = " ++ show highB
240 ++ " i = " ++ show i) result 241 ++ " i = " ++ show i)
242 return result
241 mbPkt <- do emp <- arrayEmpty (pktq pktoOutPQ) 243 mbPkt <- do emp <- arrayEmpty (pktq pktoOutPQ)
242 if emp then trace "(peekPacket empty)" $ return Nothing 244 if emp then tput XNetCrypto "(peekPacket empty)" >> return Nothing
243 else trace "(peekPacket nonempty)" $ do 245 else do tput XNetCrypto "(peekPacket nonempty)"
244 result <- readArray (pktq pktoOutPQ) i 246 result <- readArray (pktq pktoOutPQ) i
245 return $ trace ("readArray (isJust result)==" ++ show (isJust result)) result 247 tput XNetCrypto ("readArray (isJust result)==" ++ show (isJust result))
248 return result
246 pktno <- readTVar pktoPacketNo 249 pktno <- readTVar pktoPacketNo
247 nextno <- readTVar (seqno pktoInPQ) 250 nextno <- readTVar (seqno pktoInPQ)
248 pktoToWire getExtra nextno be pktno msg 251 pktoToWire getExtra nextno be pktno msg
@@ -252,37 +255,40 @@ peekPacket getExtra q@(PacketOutQueue { pktoInPQ, pktoOutPQ, pktoPacketNo, pktoT
252-- index in this implementation.) 255-- index in this implementation.)
253tryAppendQueueOutgoing :: STM extra -> PacketOutQueue extra msg wire fromwire -> msg -> STM OutGoingResult 256tryAppendQueueOutgoing :: STM extra -> PacketOutQueue extra msg wire fromwire -> msg -> STM OutGoingResult
254tryAppendQueueOutgoing getExtra q@(PacketOutQueue { pktoInPQ, pktoOutPQ, pktoPacketNo, pktoToWireIO, pktoToWire }) msg 257tryAppendQueueOutgoing getExtra q@(PacketOutQueue { pktoInPQ, pktoOutPQ, pktoPacketNo, pktoToWireIO, pktoToWire }) msg
255 = trace "(tryAppendQueueOutgoing)" $ do 258 = dtrace XNetCrypto "(tryAppendQueueOutgoing)" $ do
256 be <- readTVar (buffend pktoOutPQ) 259 be <- readTVar (buffend pktoOutPQ)
257 let i = be `mod` (qsize pktoOutPQ) 260 let i = be `mod` (qsize pktoOutPQ)
258 let arrayEmpty :: MArray a e m => a Word32 e -> m Bool 261 let arrayEmpty :: MArray a e m => a Word32 e -> m Bool
259 arrayEmpty ar = do (lowB,highB) <- getBounds ar 262 arrayEmpty ar = do (lowB,highB) <- getBounds ar
260 let result= lowB > highB 263 let result= lowB > highB
261 return $ trace ("arrayEmpty result=" ++ show result 264 tput XNetCrypto
265 ("arrayEmpty result=" ++ show result
262 ++ " lowB=" ++ show lowB 266 ++ " lowB=" ++ show lowB
263 ++ " highB = " ++ show highB 267 ++ " highB = " ++ show highB
264 ++ " i = " ++ show i) result 268 ++ " i = " ++ show i)
269 return result
265 mbPkt <- do emp <- arrayEmpty (pktq pktoOutPQ) 270 mbPkt <- do emp <- arrayEmpty (pktq pktoOutPQ)
266 if emp then trace "(tryAppendQueueOutgoing empty)" $ return Nothing 271 if emp then tput XNetCrypto "(tryAppendQueueOutgoing empty)" >> return Nothing
267 else trace "(tryAppendQueueOutgoing nonempty)" $ do 272 else do tput XNetCrypto "(tryAppendQueueOutgoing nonempty)"
268 result <- readArray (pktq pktoOutPQ) i 273 result <- readArray (pktq pktoOutPQ) i
269 return $ trace ("readArray (isJust result)==" ++ show (isJust result)) result 274 tput XNetCrypto ("readArray (isJust result)==" ++ show (isJust result))
275 return result
270 pktno <- readTVar pktoPacketNo 276 pktno <- readTVar pktoPacketNo
271 nextno <- readTVar (seqno pktoInPQ) 277 nextno <- readTVar (seqno pktoInPQ)
272 mbWire <- pktoToWire getExtra nextno be pktno msg 278 mbWire <- pktoToWire getExtra nextno be pktno msg
273 -- TODO all the above lines ^^ can be replaced with call to peekPacket 279 -- TODO all the above lines ^^ can be replaced with call to peekPacket
274 case trace "(tryAppendQueueOutgoing mbWire)" mbWire of 280 case dtrace XNetCrypto "(tryAppendQueueOutgoing mbWire)" mbWire of
275 Just (pkt,pktno') 281 Just (pkt,pktno')
276 -> trace "(tryAppendQueueOutgoing A)" 282 -> dtrace XNetCrypto "(tryAppendQueueOutgoing A)"
277 $ case mbPkt of 283 $ case mbPkt of
278 -- slot is free, insert element 284 -- slot is free, insert element
279 Nothing -> trace "(tryAppendQueueOutgoing Nothing case)" $ do 285 Nothing -> dtrace XNetCrypto "(tryAppendQueueOutgoing Nothing case)" $ do
280 modifyTVar' (buffend pktoOutPQ) (+1) 286 modifyTVar' (buffend pktoOutPQ) (+1)
281 writeTVar pktoPacketNo $! pktno' 287 writeTVar pktoPacketNo $! pktno'
282 writeArray (pktq pktoOutPQ) i (Just (pktno,pkt)) 288 writeArray (pktq pktoOutPQ) i (Just (pktno,pkt))
283 return OGSuccess 289 return OGSuccess
284 -- queue is full 290 -- queue is full
285 Just (n,_) -> trace "tryAppendQueueOutgoing Just case)" $ do 291 Just (n,_) -> dtrace XNetCrypto "tryAppendQueueOutgoing Just case)" $ do
286 nn <- getHighestHandledPacketPlus1 q 292 nn <- getHighestHandledPacketPlus1 q
287 if (n < nn) 293 if (n < nn)
288 -- but we can overwrite an old packet 294 -- but we can overwrite an old packet
diff --git a/src/Network/Tox/Crypto/Handlers.hs b/src/Network/Tox/Crypto/Handlers.hs
index 457171a9..44b2f26f 100644
--- a/src/Network/Tox/Crypto/Handlers.hs
+++ b/src/Network/Tox/Crypto/Handlers.hs
@@ -681,7 +681,7 @@ createNetCryptoOutQueue sessions newsession theirSessionKey pktq ncMyPacketNonce
681addSessionToMap :: NetCryptoSessions -> SockAddr -> NetCryptoSession -> STM () 681addSessionToMap :: NetCryptoSessions -> SockAddr -> NetCryptoSession -> STM ()
682addSessionToMap sessions addrRaw netCryptoSession = do 682addSessionToMap sessions addrRaw netCryptoSession = do
683 let addr = either id id $ either4or6 addrRaw 683 let addr = either id id $ either4or6 addrRaw
684 let dmsg msg = trace msg (return ()) 684 let dmsg msg = tput XNetCrypto msg
685 dmsg $ "addSessionToMapIfNotThere sockaddr = " ++ show addr ++ ", sessionid = " ++ show (ncSessionId netCryptoSession) 685 dmsg $ "addSessionToMapIfNotThere sockaddr = " ++ show addr ++ ", sessionid = " ++ show (ncSessionId netCryptoSession)
686 let remotePublicKey = ncTheirPublicKey netCryptoSession 686 let remotePublicKey = ncTheirPublicKey netCryptoSession
687 allsessions = netCryptoSessions sessions 687 allsessions = netCryptoSessions sessions
@@ -695,7 +695,7 @@ addSessionToMap sessions addrRaw netCryptoSession = do
695addSessionToMapIfNotThere :: NetCryptoSessions -> SockAddr -> NetCryptoSession -> STM () 695addSessionToMapIfNotThere :: NetCryptoSessions -> SockAddr -> NetCryptoSession -> STM ()
696addSessionToMapIfNotThere sessions addrRaw netCryptoSession = do 696addSessionToMapIfNotThere sessions addrRaw netCryptoSession = do
697 let addr = either id id $ either4or6 addrRaw 697 let addr = either id id $ either4or6 addrRaw
698 let dmsg msg = trace msg (return ()) 698 let dmsg msg = tput XNetCrypto msg
699 dmsg $ "addSessionToMapIfNotThere sockaddr = " ++ show addr ++ ", sessionid = " ++ show (ncSessionId netCryptoSession) 699 dmsg $ "addSessionToMapIfNotThere sockaddr = " ++ show addr ++ ", sessionid = " ++ show (ncSessionId netCryptoSession)
700 let remotePublicKey = ncTheirPublicKey netCryptoSession 700 let remotePublicKey = ncTheirPublicKey netCryptoSession
701 allsessions = netCryptoSessions sessions 701 allsessions = netCryptoSessions sessions
@@ -857,7 +857,7 @@ destroySession session = do
857updateCryptoSession :: NetCryptoSessions -> SockAddr -> SecretKey -> POSIXTime -> HandshakeParams 857updateCryptoSession :: NetCryptoSessions -> SockAddr -> SecretKey -> POSIXTime -> HandshakeParams
858 -> NetCryptoSession -> Handshake Encrypted -> STM (Maybe (Handshake Encrypted), IO ()) 858 -> NetCryptoSession -> Handshake Encrypted -> STM (Maybe (Handshake Encrypted), IO ())
859updateCryptoSession sessions addr newsession timestamp hp session handshake = do 859updateCryptoSession sessions addr newsession timestamp hp session handshake = do
860 let dmsg msg = trace msg (return ()) 860 let dmsg msg = tput XNetCrypto msg
861 ncState0 <- readTVar (ncState session) 861 ncState0 <- readTVar (ncState session)
862 ncTheirBaseNonce0 <- readTVar (ncTheirBaseNonce session) 862 ncTheirBaseNonce0 <- readTVar (ncTheirBaseNonce session)
863 if (ncState0 >= {-Accepted-}InProgress AwaitingSessionPacket) 863 if (ncState0 >= {-Accepted-}InProgress AwaitingSessionPacket)