| 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 = 4 |
| 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, "lttng_ust_libc*") |
| 43 | start_session(session_info) |
| 44 | |
| 45 | malloc_process = subprocess.Popen(test_path + "prog", stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 46 | if sys.version_info >= (3, 3): |
| 47 | try: |
| 48 | malloc_process.wait(5) |
| 49 | except subprocess.TimeoutExpired: |
| 50 | malloc_process.kill() |
| 51 | bail("Failed to run libustinstr-malloc test application.", session_info) |
| 52 | else: |
| 53 | malloc_process.wait() |
| 54 | |
| 55 | print_test_result(malloc_process.returncode == 0, current_test, "Test application exited normally") |
| 56 | current_test += 1 |
| 57 | |
| 58 | stop_session(session_info) |
| 59 | |
| 60 | # Check for malloc events in the resulting trace |
| 61 | try: |
| 62 | babeltrace_process = subprocess.Popen(["babeltrace", session_info.trace_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 63 | except FileNotFoundError: |
| 64 | bail("Could not open babeltrace. Please make sure it is installed.", session_info) |
| 65 | |
| 66 | malloc_event_found = False |
| 67 | free_event_found = False |
| 68 | |
| 69 | for event_line in babeltrace_process.stdout: |
| 70 | # Let babeltrace finish to get the return code |
| 71 | if malloc_event_found and free_event_found: |
| 72 | continue |
| 73 | |
| 74 | event_line = event_line.decode('utf-8').replace("\n", "") |
| 75 | if re.search(r".*lttng_ust_libc:malloc.*", event_line) is not None: |
| 76 | malloc_event_found = True |
| 77 | |
| 78 | if re.search(r".*lttng_ust_libc:free.*", event_line) is not None: |
| 79 | free_event_found = True |
| 80 | |
| 81 | babeltrace_process.wait() |
| 82 | |
| 83 | print_test_result(babeltrace_process.returncode == 0, current_test, "Resulting trace is readable") |
| 84 | current_test += 1 |
| 85 | |
| 86 | print_test_result(malloc_event_found, current_test, "lttng_ust_libc:malloc event found in resulting trace") |
| 87 | current_test += 1 |
| 88 | |
| 89 | print_test_result(free_event_found, current_test, "lttng_ust_libc:free event found in resulting trace") |
| 90 | current_test += 1 |
| 91 | |
| 92 | shutil.rmtree(session_info.tmp_directory) |