From eacb7c3c0ba266923df0810dfe40892d7acbee1d Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Thu, 14 May 2020 09:32:20 -0400 Subject: [PATCH 1/3] Fail gracefully when trying to trace if LTTng is not installed Signed-off-by: Christophe Bedard --- .../tracetools_trace/tools/lttng.py | 32 ++++++++++++++++++- tracetools_trace/tracetools_trace/trace.py | 4 +++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/tracetools_trace/tracetools_trace/tools/lttng.py b/tracetools_trace/tracetools_trace/tools/lttng.py index ad402ef..4a75b07 100644 --- a/tracetools_trace/tracetools_trace/tools/lttng.py +++ b/tracetools_trace/tracetools_trace/tools/lttng.py @@ -14,8 +14,8 @@ """Interface for tracing with LTTng.""" +import subprocess import sys - from typing import List from typing import Optional @@ -83,3 +83,33 @@ def lttng_fini( """ _lttng.stop(session_name) _lttng.destroy(session_name) + + +def is_lttng_installed() -> bool: + """ + Check if LTTng is installed. + + Simply checks for the 'lttng' command. + + :return: True if it is installed, False otherwise + """ + try: + process = subprocess.Popen( + ['lttng', '--version'], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + _, stderr = process.communicate() + if 0 != process.returncode: + raise RuntimeError(stderr.decode()) + return True + except Exception as e: + print( + ( + f'LTTng not found: {e}\n' + 'Cannot trace. See documentation at: ' + 'https://gitlab.com/micro-ROS/ros_tracing/ros2_tracing' + ), + file=sys.stderr, + ) + return False diff --git a/tracetools_trace/tracetools_trace/trace.py b/tracetools_trace/tracetools_trace/trace.py index 1f3aad9..bfdd3d2 100644 --- a/tracetools_trace/tracetools_trace/trace.py +++ b/tracetools_trace/tracetools_trace/trace.py @@ -15,6 +15,7 @@ """Entrypoint/script to setup and start an LTTng tracing session.""" +import sys from typing import List from tracetools_trace.tools import args @@ -41,6 +42,9 @@ def init( :param context_names: list of context names to enable :param display_list: whether to display list(s) of enabled events and context names """ + if not lttng.is_lttng_installed(): + sys.exit(2) + ust_enabled = len(ros_events) > 0 kernel_enabled = len(kernel_events) > 0 if ust_enabled: From 8a11eed4dee32c36094625b5efcec5cbd5351fa4 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Thu, 14 May 2020 09:43:48 -0400 Subject: [PATCH 2/3] Add call to 'ros2 trace' in 'no_lttng' job to test failure Signed-off-by: Christophe Bedard --- .gitlab-ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d307b45..94a8530 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -50,7 +50,9 @@ no_lttng: image: $BASE_IMAGE_ID:$DISTRO-base script: - colcon build --symlink-install --event-handlers console_cohesion+ --packages-up-to $PACKAGES_LIST + - . install/setup.sh - (! ./build/tracetools/status) + - (! ros2 trace) - colcon test --event-handlers console_cohesion+ --packages-select $PACKAGES_LIST - colcon test-result --all <<: *global_artifacts From a915fa55f0bb10950e026364fc35bf7af890d152 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Thu, 14 May 2020 10:25:30 -0400 Subject: [PATCH 3/3] Make sure platform is Linux before checking if LTTng is installed Signed-off-by: Christophe Bedard --- .../tracetools_trace/tools/lttng.py | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tracetools_trace/tracetools_trace/tools/lttng.py b/tracetools_trace/tracetools_trace/tools/lttng.py index 4a75b07..31a7d93 100644 --- a/tracetools_trace/tracetools_trace/tools/lttng.py +++ b/tracetools_trace/tracetools_trace/tools/lttng.py @@ -14,6 +14,7 @@ """Interface for tracing with LTTng.""" +import platform import subprocess import sys from typing import List @@ -89,10 +90,19 @@ def is_lttng_installed() -> bool: """ Check if LTTng is installed. - Simply checks for the 'lttng' command. + It first checks if the OS can support LTTng. + If so, it then simply checks if LTTng is installed using the 'lttng' command. :return: True if it is installed, False otherwise """ + message_doc = ( + 'Cannot trace. See documentation at: ' + 'https://gitlab.com/micro-ROS/ros_tracing/ros2_tracing' + ) + system = platform.system() + if 'Linux' != system: + print(f"System '{system}' does not support LTTng.\n{message_doc}", file=sys.stderr) + return False try: process = subprocess.Popen( ['lttng', '--version'], @@ -104,12 +114,5 @@ def is_lttng_installed() -> bool: raise RuntimeError(stderr.decode()) return True except Exception as e: - print( - ( - f'LTTng not found: {e}\n' - 'Cannot trace. See documentation at: ' - 'https://gitlab.com/micro-ROS/ros_tracing/ros2_tracing' - ), - file=sys.stderr, - ) + print(f'LTTng not found: {e}\n{message_doc}', file=sys.stderr) return False