-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
66 lines (57 loc) · 1.86 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
import Control.Monad
import Control.Monad.Trans.Resource
import Data.Attoparsec.ByteString.Streaming qualified as AS
import Data.ByteString.Char8 (ByteString)
import Data.Function
import Data.List
import Data.Map (Map)
import Data.Map qualified as Map
import Strace.Finish
import Strace.Raw
import Strace.SystemCalls
import Strace.Types
import Streaming
import Streaming.ByteString.Char8 qualified as Q
import Streaming.Prelude qualified as S
import System.Clock
import System.Environment
import System.IO
import Text.Pretty.Simple (pPrint)
import Text.Printf
-- We assume the following strace invocation:
--
-- strace -f -ttt -v -x -y -s 1048576
--
-- This follows forks and shows PIDs (-f), prints timestamps in the right format
-- (-ttt), expands all structures incl. env vars (-v), prints non-ascii strings
-- in hex format (-x), resolves file descriptors (-y) and prints strings up to 1
-- MB (-s 1048576).
--
-- TODO: check whether -x is necessary
main :: IO ()
main = do
[file] <- getArgs
start <- getTime Monotonic
!r <- runResourceT
$ Q.readFile file
& AS.parsed line
& finishSystemCalls
-- & S.map (mapEvent $ mapSystemCall parseSystemCall)
& S.length_
-- & (S.fold_ countUnknownSysCalls mempty id)
end <- getTime Monotonic
print r
-- pPrint $ sortBy (compare `on` snd) $ Map.toList r
let time :: Double = (fromIntegral $ toNanoSecs (diffTimeSpec start end)) / 1e9
printf "(%f s)\n" time
countUnknownSysCalls :: Map SystemCallName Int -> Line -> Map SystemCallName Int
countUnknownSysCalls m (Line _ _ (SystemCall (Unknown name _ _))) =
Map.insertWith (+) name 1 m
countUnknownSysCalls m _ = m