Fail gracefully when trying to trace if LTTng is not installed

Signed-off-by: Christophe Bedard <bedard.christophe@gmail.com>
This commit is contained in:
Christophe Bedard 2020-05-14 09:32:20 -04:00
parent c3a99712f9
commit eacb7c3c0b
2 changed files with 35 additions and 1 deletions

View file

@ -14,8 +14,8 @@
"""Interface for tracing with LTTng.""" """Interface for tracing with LTTng."""
import subprocess
import sys import sys
from typing import List from typing import List
from typing import Optional from typing import Optional
@ -83,3 +83,33 @@ def lttng_fini(
""" """
_lttng.stop(session_name) _lttng.stop(session_name)
_lttng.destroy(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

View file

@ -15,6 +15,7 @@
"""Entrypoint/script to setup and start an LTTng tracing session.""" """Entrypoint/script to setup and start an LTTng tracing session."""
import sys
from typing import List from typing import List
from tracetools_trace.tools import args from tracetools_trace.tools import args
@ -41,6 +42,9 @@ def init(
:param context_names: list of context names to enable :param context_names: list of context names to enable
:param display_list: whether to display list(s) of enabled events and context names :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 ust_enabled = len(ros_events) > 0
kernel_enabled = len(kernel_events) > 0 kernel_enabled = len(kernel_events) > 0
if ust_enabled: if ust_enabled: