| 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) - 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | # |
| 5 | # This program is free software; you can redistribute it and/or modify it |
| 6 | # under the terms of the GNU General Public License, version 2 only, as |
| 7 | # published by the Free Software Foundation. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | # more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License along with |
| 15 | # this program; if not, write to the Free Software Foundation, Inc., 51 |
| 16 | # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | |
| 18 | import os |
| 19 | import subprocess |
| 20 | import re |
| 21 | import shutil |
| 22 | import sys |
| 23 | |
| 24 | test_path = os.path.dirname(os.path.abspath(__file__)) + "/" |
| 25 | test_utils_path = test_path |
| 26 | for i in range(4): |
| 27 | test_utils_path = os.path.dirname(test_utils_path) |
| 28 | test_utils_path = test_utils_path + "/utils" |
| 29 | sys.path.append(test_utils_path) |
| 30 | from test_utils import * |
| 31 | |
| 32 | |
| 33 | NR_TESTS = 6 |
| 34 | current_test = 1 |
| 35 | print("1..{0}".format(NR_TESTS)) |
| 36 | |
| 37 | # Check if a sessiond is running... bail out if none found. |
| 38 | if session_daemon_alive() == 0: |
| 39 | bail("No sessiond running. Please make sure you are running this test with the \"run\" shell script and verify that the lttng tools are properly installed.") |
| 40 | |
| 41 | session_info = create_session() |
| 42 | enable_ust_tracepoint_event(session_info, "ust_tests_fork*") |
| 43 | start_session(session_info) |
| 44 | |
| 45 | fork_process = subprocess.Popen([test_path + "fork", test_path + "fork2"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 46 | |
| 47 | if sys.version_info >= (3, 3): |
| 48 | try: |
| 49 | fork_process.wait(5) |
| 50 | except TimeoutExpired: |
| 51 | fork_process.kill() |
| 52 | bail("Failed to run fork test application (time out)", session_info) |
| 53 | else: |
| 54 | fork_process.wait() |
| 55 | |
| 56 | parent_pid = -1 |
| 57 | child_pid = -1 |
| 58 | for line in fork_process.stdout: |
| 59 | line = line.decode('utf-8').replace("\n", "") |
| 60 | match = re.search(r"child_pid (\d+)", line) |
| 61 | if match: |
| 62 | child_pid = match.group(1) |
| 63 | match = re.search(r"parent_pid (\d+)", line) |
| 64 | if match: |
| 65 | parent_pid = match.group(1) |
| 66 | |
| 67 | print_test_result(fork_process.returncode == 0, current_test, "Fork test application exited normally") |
| 68 | current_test += 1 |
| 69 | |
| 70 | stop_session(session_info) |
| 71 | |
| 72 | # Check both events (normal exit and suicide messages) are present in the resulting trace |
| 73 | try: |
| 74 | babeltrace_process = subprocess.Popen(["babeltrace", session_info.trace_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 75 | except FileNotFoundError: |
| 76 | bail("Could not open babeltrace. Please make sure it is installed.", session_info) |
| 77 | |
| 78 | event_lines = [] |
| 79 | for event_line in babeltrace_process.stdout: |
| 80 | event_line = event_line.decode('utf-8').replace("\n", "") |
| 81 | if re.search(r"warning", event_line) is not None or re.search(r"error", event_line) is not None: |
| 82 | print( "# " + event_line ) |
| 83 | else: |
| 84 | event_lines.append(event_line) |
| 85 | |
| 86 | babeltrace_process.wait() |
| 87 | |
| 88 | print_test_result(babeltrace_process.returncode == 0, current_test, "Resulting trace is readable") |
| 89 | current_test += 1 |
| 90 | |
| 91 | if babeltrace_process.returncode != 0: |
| 92 | bail("Unreadable trace; can't proceed with analysis.", session_info) |
| 93 | |
| 94 | event_before_fork = False |
| 95 | event_after_fork_parent = False |
| 96 | event_after_fork_child = False |
| 97 | event_after_exec = False |
| 98 | |
| 99 | for event_line in event_lines: |
| 100 | match = re.search(r".*pid = (\d+)", event_line) |
| 101 | if match is not None: |
| 102 | event_pid = match.group(1) |
| 103 | else: |
| 104 | continue |
| 105 | |
| 106 | if re.search(r"before_fork", event_line): |
| 107 | event_before_fork = (event_pid == parent_pid) |
| 108 | if re.search(r"after_fork_parent", event_line): |
| 109 | event_after_fork_parent = (event_pid == parent_pid) |
| 110 | if re.search(r"after_fork_child", event_line): |
| 111 | event_after_fork_child = (event_pid == child_pid) |
| 112 | if re.search(r"after_exec", event_line): |
| 113 | event_after_exec = (event_pid == child_pid) |
| 114 | |
| 115 | print_test_result(event_before_fork, current_test, "before_fork event logged by parent process found in trace") |
| 116 | current_test += 1 |
| 117 | print_test_result(event_after_fork_parent, current_test, "after_fork_parent event logged by parent process found in trace") |
| 118 | current_test += 1 |
| 119 | print_test_result(event_after_fork_child, current_test, "after_fork_child event logged by child process found in trace") |
| 120 | current_test += 1 |
| 121 | print_test_result(event_after_exec, current_test, "after_exec event logged by child process found in trace") |
| 122 | current_test += 1 |
| 123 | |
| 124 | shutil.rmtree(session_info.tmp_directory) |