Fix: include stdlib.h in compat/string.h
[lttng-tools.git] / tests / regression / ust / fork / test_fork.py
CommitLineData
37bd6c8e
JG
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
18import os
19import subprocess
20import re
21import shutil
22import sys
23
24test_path = os.path.dirname(os.path.abspath(__file__)) + "/"
25test_utils_path = test_path
26for i in range(4):
27 test_utils_path = os.path.dirname(test_utils_path)
28test_utils_path = test_utils_path + "/utils"
29sys.path.append(test_utils_path)
30from test_utils import *
31
32
33NR_TESTS = 6
34current_test = 1
35print("1..{0}".format(NR_TESTS))
36
37# Check if a sessiond is running... bail out if none found.
38if 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
41session_info = create_session()
42enable_ust_tracepoint_event(session_info, "ust_tests_fork*")
43start_session(session_info)
44
45fork_process = subprocess.Popen([test_path + "fork", test_path + "fork2"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
37bd6c8e
JG
46parent_pid = -1
47child_pid = -1
48for line in fork_process.stdout:
49 line = line.decode('utf-8').replace("\n", "")
50 match = re.search(r"child_pid (\d+)", line)
51 if match:
52 child_pid = match.group(1)
53 match = re.search(r"parent_pid (\d+)", line)
54 if match:
55 parent_pid = match.group(1)
56
3e198d90
JG
57fork_process.wait()
58
37bd6c8e
JG
59print_test_result(fork_process.returncode == 0, current_test, "Fork test application exited normally")
60current_test += 1
61
62stop_session(session_info)
63
64# Check both events (normal exit and suicide messages) are present in the resulting trace
65try:
66 babeltrace_process = subprocess.Popen(["babeltrace", session_info.trace_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
67except FileNotFoundError:
68 bail("Could not open babeltrace. Please make sure it is installed.", session_info)
69
70event_lines = []
71for event_line in babeltrace_process.stdout:
72 event_line = event_line.decode('utf-8').replace("\n", "")
73 if re.search(r"warning", event_line) is not None or re.search(r"error", event_line) is not None:
74 print( "# " + event_line )
75 else:
76 event_lines.append(event_line)
77
78babeltrace_process.wait()
79
80print_test_result(babeltrace_process.returncode == 0, current_test, "Resulting trace is readable")
81current_test += 1
82
83if babeltrace_process.returncode != 0:
84 bail("Unreadable trace; can't proceed with analysis.", session_info)
85
86event_before_fork = False
87event_after_fork_parent = False
88event_after_fork_child = False
89event_after_exec = False
90
91for event_line in event_lines:
92 match = re.search(r".*pid = (\d+)", event_line)
93 if match is not None:
94 event_pid = match.group(1)
95 else:
96 continue
97
98 if re.search(r"before_fork", event_line):
99 event_before_fork = (event_pid == parent_pid)
100 if re.search(r"after_fork_parent", event_line):
101 event_after_fork_parent = (event_pid == parent_pid)
102 if re.search(r"after_fork_child", event_line):
103 event_after_fork_child = (event_pid == child_pid)
104 if re.search(r"after_exec", event_line):
105 event_after_exec = (event_pid == child_pid)
106
107print_test_result(event_before_fork, current_test, "before_fork event logged by parent process found in trace")
108current_test += 1
109print_test_result(event_after_fork_parent, current_test, "after_fork_parent event logged by parent process found in trace")
110current_test += 1
111print_test_result(event_after_fork_child, current_test, "after_fork_child event logged by child process found in trace")
112current_test += 1
113print_test_result(event_after_exec, current_test, "after_exec event logged by child process found in trace")
114current_test += 1
115
116shutil.rmtree(session_info.tmp_directory)
This page took 0.039247 seconds and 4 git commands to generate.