trace dump utility
Stefan Schuermans

Stefan Schuermans commited on 2020-05-17 18:41:55
Showing 4 changed files, with 79 additions and 0 deletions.

... ...
@@ -5,6 +5,7 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror -fPIC")
5 5
 
6 6
 enable_testing()
7 7
 
8
+add_subdirectory(dump)
8 9
 add_subdirectory(liblwptev)
9 10
 add_subdirectory(liblwptpl)
10 11
 add_subdirectory(tests)
... ...
@@ -0,0 +1,27 @@
1
+add_custom_command(
2
+  OUTPUT
3
+  ${CMAKE_CURRENT_BINARY_DIR}/lwproctrace_pb2.py
4
+  DEPENDS
5
+  ${CMAKE_SOURCE_DIR}/lwproctrace.proto
6
+  COMMAND
7
+  protoc --proto_path ${CMAKE_SOURCE_DIR}
8
+         --python_out ${CMAKE_CURRENT_BINARY_DIR}
9
+         lwproctrace.proto
10
+)
11
+
12
+add_custom_command(
13
+  OUTPUT
14
+  ${CMAKE_CURRENT_BINARY_DIR}/dump.py
15
+  DEPENDS
16
+  ${CMAKE_CURRENT_SOURCE_DIR}/dump.py
17
+  COMMAND
18
+  cp -a ${CMAKE_CURRENT_SOURCE_DIR}/dump.py ${CMAKE_CURRENT_BINARY_DIR}/dump.py
19
+)
20
+
21
+add_custom_target(
22
+  dump
23
+  ALL
24
+  DEPENDS
25
+  ${CMAKE_CURRENT_BINARY_DIR}/lwproctrace_pb2.py
26
+  ${CMAKE_CURRENT_BINARY_DIR}/dump.py
27
+)
... ...
@@ -0,0 +1,50 @@
1
+#! /usr/bin/env python3
2
+
3
+import argparse
4
+import lwproctrace_pb2
5
+import struct
6
+
7
+
8
+def parse_args():
9
+    parser = argparse.ArgumentParser(description='dump lwproctrace trace')
10
+    parser.add_argument('trace', help='trace file')
11
+    args = parser.parse_args()
12
+    return args
13
+
14
+
15
+def dump_event(f):
16
+    # skip till after magic
17
+    magic = f.read(4)
18
+    while magic != b'lwpt':
19
+        if len(magic) < 4:
20
+            return False  # EOF
21
+        magic = magic[1:] + f.read(1)  # search for magic byte for byte
22
+    # read size of next event (32 bit network byte order)
23
+    size = f.read(4)
24
+    if len(size) < 4:
25
+        return False  # EOF
26
+    size = struct.unpack('!L', size)[0]
27
+    # read event data
28
+    data = f.read(size)
29
+    if len(data) < size:
30
+        return False  # EOF
31
+    # unpack event
32
+    event = lwproctrace_pb2.event.FromString(data)
33
+    # dump event
34
+    print('event {')
35
+    for line in repr(event).split('\n'):
36
+        if line != '':
37
+            print('  ' + line)
38
+    print('}')
39
+    return True
40
+
41
+
42
+def main():
43
+    args = parse_args()
44
+    with open(args.trace, 'rb') as f:
45
+        while dump_event(f):
46
+            pass
47
+
48
+
49
+if __name__ == '__main__':
50
+    main()
... ...
@@ -12,5 +12,6 @@ add_test(
12 12
       /bin/false e f 'g h'
13 13
     )
14 14
     ls -l out.proto
15
+    ${CMAKE_BINARY_DIR}/dump/dump.py out.proto
15 16
   "
16 17
 )
17 18