python3 module, exports, upt-dump
Stefan Schuermans

Stefan Schuermans commited on 2020-05-22 14:11:56
Showing 12 changed files, with 129 additions and 63 deletions.

... ...
@@ -5,8 +5,25 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror -fPIC")
5 5
 
6 6
 enable_testing()
7 7
 
8
-add_subdirectory(dump)
8
+add_custom_command(
9
+  OUTPUT
10
+  ${CMAKE_CURRENT_BINARY_DIR}/exports
11
+  DEPENDS
12
+  ${CMAKE_CURRENT_SOURCE_DIR}/exports
13
+  COMMAND
14
+  cp -a ${CMAKE_CURRENT_SOURCE_DIR}/exports ${CMAKE_CURRENT_BINARY_DIR}/exports
15
+)
16
+
17
+add_custom_target(
18
+  copy_exports
19
+  ALL
20
+  DEPENDS
21
+  exports
22
+)
23
+
9 24
 add_subdirectory(libuptev)
10 25
 add_subdirectory(libuptpl)
26
+add_subdirectory(python3)
27
+add_subdirectory(upt-dump)
11 28
 add_subdirectory(upt-trace)
12 29
 add_subdirectory(tests)
... ...
@@ -50,6 +50,12 @@ Run tests:
50 50
 ctest
51 51
 ```
52 52
 
53
+Set up for direct usage from build directory (to be done in each shell):
54
+
55
+```
56
+source exports
57
+```
58
+
53 59
 ## Tracing Applications
54 60
 
55 61
 To trace an application, prefix the command with `upt-trace` and the
... ...
@@ -59,12 +65,12 @@ file name for the trace.  For example, to trace the command
59 65
 ```
60 66
 run the following command:
61 67
 ```
62
-<build dir>/bin/upt-trace mytrace.proto /usr/bin/printf "trace me"
68
+upt-trace mytrace.proto /usr/bin/printf "trace me"
63 69
 ```
64 70
 
65 71
 To show the recorded events, run:
66 72
 ```
67
-<build dir>/dump/dump.py mytrace.proto
73
+upt-dump mytrace.proto
68 74
 ```
69 75
 
70 76
 ## Example: Trace Build Process
... ...
@@ -77,7 +83,7 @@ Change to the build directory.
77 83
 
78 84
 Start a new shell to be traced:
79 85
 ```
80
-bin/upt-trace mytrace.proto bash
86
+upt-trace mytrace.proto bash
81 87
 ```
82 88
 
83 89
 Configure another build directory for this tracing example and run the build:
... ...
@@ -97,6 +103,6 @@ exit
97 103
 
98 104
 Show traced information:
99 105
 ```
100
-dump/dump.py mytrace.proto
106
+upt-dump mytrace.proto
101 107
 ```
102 108
 
... ...
@@ -1,27 +0,0 @@
1
-add_custom_command(
2
-  OUTPUT
3
-  ${CMAKE_CURRENT_BINARY_DIR}/uproctrace_pb2.py
4
-  DEPENDS
5
-  ${CMAKE_SOURCE_DIR}/uproctrace.proto
6
-  COMMAND
7
-  protoc --proto_path ${CMAKE_SOURCE_DIR}
8
-         --python_out ${CMAKE_CURRENT_BINARY_DIR}
9
-         uproctrace.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}/uproctrace_pb2.py
26
-  ${CMAKE_CURRENT_BINARY_DIR}/dump.py
27
-)
... ...
@@ -0,0 +1,8 @@
1
+if [[ -z ${BASH_SOURCE[0]:-} ]]
2
+then
3
+  echo "BASH_SOURCE is not net, unsupported shell" >&2
4
+  return 2
5
+fi
6
+export UPT_HOME="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")"
7
+export PATH="$UPT_HOME/bin${PATH:+:${PATH}}"
8
+export PYTHONPATH="$UPT_HOME/python3${PYTHONPATH:+:${PYTHONPATH}}"
... ...
@@ -0,0 +1 @@
1
+add_subdirectory(uproctrace)
... ...
@@ -0,0 +1,35 @@
1
+add_custom_command(
2
+  OUTPUT
3
+  ${CMAKE_CURRENT_BINARY_DIR}/uproctrace_pb2.py
4
+  DEPENDS
5
+  ${CMAKE_SOURCE_DIR}/uproctrace.proto
6
+  COMMAND
7
+  protoc --proto_path ${CMAKE_SOURCE_DIR}
8
+         --python_out ${CMAKE_CURRENT_BINARY_DIR}
9
+         uproctrace.proto
10
+)
11
+
12
+function(pyfile NAME)
13
+  add_custom_command(
14
+    OUTPUT
15
+    ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.py
16
+    DEPENDS
17
+    ${CMAKE_CURRENT_SOURCE_DIR}/${NAME}.py
18
+    COMMAND
19
+    cp -a ${CMAKE_CURRENT_SOURCE_DIR}/${NAME}.py
20
+          ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.py
21
+  )
22
+  list(APPEND PYFILES ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.py)
23
+  set(PYFILES ${PYFILES} PARENT_SCOPE)
24
+endfunction(pyfile)
25
+
26
+pyfile(__init__)
27
+pyfile(dump)
28
+
29
+add_custom_target(
30
+  python3_uproctrace
31
+  ALL
32
+  DEPENDS
33
+  ${CMAKE_CURRENT_BINARY_DIR}/uproctrace_pb2.py
34
+  ${PYFILES}
35
+)
... ...
@@ -1,18 +1,12 @@
1
-#! /usr/bin/env python3
2
-
3
-import argparse
4
-import uproctrace_pb2
1
+import uproctrace.uproctrace_pb2 as pb2
5 2
 import struct
6 3
 
7 4
 
8
-def parse_args():
9
-    parser = argparse.ArgumentParser(description='dump uproctrace trace')
10
-    parser.add_argument('trace', help='trace file')
11
-    args = parser.parse_args()
12
-    return args
13
-
14
-
15
-def dump_event(f):
5
+def dump_event(f, out):
6
+    """
7
+    Read the first event from f and dump it to out.
8
+    Return True if an event could be found and dumped, False otherwise.
9
+    """
16 10
     # skip till after magic
17 11
     magic = f.read(4)
18 12
     while magic != b'upt0':
... ...
@@ -29,22 +23,11 @@ def dump_event(f):
29 23
     if len(data) < size:
30 24
         return False  # EOF
31 25
     # unpack event
32
-    event = uproctrace_pb2.event.FromString(data)
26
+    event = pb2.event.FromString(data)
33 27
     # dump event
34
-    print('event {')
28
+    print('event {', file=out)
35 29
     for line in repr(event).split('\n'):
36 30
         if line != '':
37
-            print('  ' + line)
38
-    print('}')
31
+            print('  ' + line, file=out)
32
+    print('}', file=out)
39 33
     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()
... ...
@@ -11,12 +11,14 @@ UPT_HOME="$1"
11 11
 
12 12
 SCRIPT_DIR="$(dirname "$0")"
13 13
 
14
+source "$UPT_HOME/exports"
15
+
14 16
 rm -rf out.proto
15 17
 
16
-"$UPT_HOME/bin/upt-trace" out.proto "$SCRIPT_DIR/traceme.bash"
18
+upt-trace out.proto "$SCRIPT_DIR/traceme.bash"
17 19
 
18 20
 ls -l out.proto
19 21
 
20
-"$UPT_HOME/dump/dump.py" out.proto | tee out.dump
22
+upt-dump out.proto | tee out.dump
21 23
 grep '^ *event *{ *$' out.dump | wc -l | tee out.event_cnt
22 24
 grep '^6$' out.event_cnt
... ...
@@ -12,13 +12,15 @@ UPT_HOME="$(readlink -f "$2")"
12 12
 
13 13
 SCRIPT_DIR="$(dirname "$0")"
14 14
 
15
+source "$UPT_HOME/exports"
16
+
15 17
 rm -rf out.proto build
16 18
 
17
-"$UPT_HOME/bin/upt-trace" out.proto "$SCRIPT_DIR/run_build.bash" "$SOURCE_DIR"
19
+upt-trace out.proto "$SCRIPT_DIR/run_build.bash" "$SOURCE_DIR"
18 20
 
19 21
 ls -l out.proto
20 22
 
21
-"$UPT_HOME/dump/dump.py" out.proto | tee out.dump
23
+upt-dump out.proto | tee out.dump
22 24
 grep -A 1 '^ *cmdline {$' out.dump | grep '^ *s: "mkdir"$'
23 25
 grep '^ *s: "proc_begin.c"$' out.dump
24 26
 grep '^ *s: "libuptpl.so"$' out.dump
... ...
@@ -0,0 +1,16 @@
1
+add_custom_command(
2
+  OUTPUT
3
+  ${CMAKE_BINARY_DIR}/bin/upt-dump
4
+  DEPENDS
5
+  ${CMAKE_CURRENT_SOURCE_DIR}/upt-dump.py
6
+  COMMAND
7
+  cp -a ${CMAKE_CURRENT_SOURCE_DIR}/upt-dump.py
8
+        ${CMAKE_BINARY_DIR}/bin/upt-dump
9
+)
10
+
11
+add_custom_target(
12
+  upt-dump
13
+  ALL
14
+  DEPENDS
15
+  ${CMAKE_BINARY_DIR}/bin/upt-dump
16
+)
... ...
@@ -0,0 +1,23 @@
1
+#! /usr/bin/env python3
2
+
3
+import argparse
4
+import sys
5
+import uproctrace.dump
6
+
7
+
8
+def parse_args():
9
+    parser = argparse.ArgumentParser(description='dump uproctrace trace')
10
+    parser.add_argument('trace', help='trace file')
11
+    args = parser.parse_args()
12
+    return args
13
+
14
+
15
+def main():
16
+    args = parse_args()
17
+    with open(args.trace, 'rb') as f:
18
+        while uproctrace.dump.dump_event(f, sys.stdout):
19
+            pass
20
+
21
+
22
+if __name__ == '__main__':
23
+    main()
0 24