diff --git a/lisa/trace.py b/lisa/trace.py index 2a21e565b329eab2c7e112e9a3f556fba4a22d03..a5056da5165753b6a05ebbb9886244989eceb4b6 100644 --- a/lisa/trace.py +++ b/lisa/trace.py @@ -3708,7 +3708,6 @@ class Trace(Loggable, TraceBase): :Variable keyword arguments: Forwarded to :class:`Trace`. """ - ftrace_coll = FtraceCollector(target, events=events, buffer_size=buffer_size) plat_info = target.plat_info class TraceProxy(TraceBase): @@ -3726,9 +3725,6 @@ class Trace(Loggable, TraceBase): proxy = TraceProxy() - with ftrace_coll: - yield proxy - if filepath: cm = nullcontext(filepath) else: @@ -3740,7 +3736,10 @@ class Trace(Loggable, TraceBase): cm = cm_func() with cm as path: - ftrace_coll.get_data(path) + ftrace_coll = FtraceCollector(target, events=events, buffer_size=buffer_size, output_path=path) + with ftrace_coll: + yield proxy + trace = cls( path, events=events, @@ -5670,7 +5669,8 @@ class FtraceCollector(CollectorBase, Configurable): needed_from_kmod = missing_events | missing_optional_events kmod = None kmod_cm = None - if needed_from_kmod and kmod_auto_load: + need_kmod = needed_from_kmod and kmod_auto_load + if need_kmod: self.logger.info(f'Building kernel module to try to provide the following events that are not currently available on the target: {", ".join(sorted(needed_from_kmod))}') try: kmod, kmod_cm = self._get_kmod( @@ -5689,7 +5689,9 @@ class FtraceCollector(CollectorBase, Configurable): ) from e else: self.logger.error(f'{msg}: {e}') + need_kmod = False + self._need_kmod = need_kmod self._kmod_cm = kmod_cm if kmod is not None: @@ -5790,8 +5792,15 @@ class FtraceCollector(CollectorBase, Configurable): x = self._cm.__exit__(*args, **kwargs) finally: self._cm = None + self._kmod_cm = None return x + def get_data(self, *args, **kwargs): + if self._need_kmod and not self._kmod_cm: + raise ValueError('FtraceCollector.get_data() cannot be called after the kernel module was unloaded.') + else: + return super().get_data(*args, **kwargs) + @staticmethod def _target_available_events(target, tracing_path): events = target.read_value(target.path.join(tracing_path, 'available_events'))