From facd251edb9e7ce7ed19b4c833a4196e3968a359 Mon Sep 17 00:00:00 2001
From: Douglas Raillard <douglas.raillard@arm.com>
Date: Thu, 30 Jan 2025 11:54:51 +0000
Subject: [PATCH] collector/dmesg: Fix dmesg variant detection

Check for all the CLI options we are going to use when deciding whether
to use the system's dmesg or the one we ship via busybox.
---
 devlib/collector/dmesg.py | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/devlib/collector/dmesg.py b/devlib/collector/dmesg.py
index 3ee7484..06676aa 100644
--- a/devlib/collector/dmesg.py
+++ b/devlib/collector/dmesg.py
@@ -203,12 +203,16 @@ class DmesgCollector(CollectorBase):
             ))
         self.level = level
 
-        # Check if dmesg is the BusyBox one, or the one from util-linux in a
-        # recent version.
-        # Note: BusyBox dmesg does not support -h, but will still print the
-        # help with an exit code of 1
-        self.basic_dmesg = '--force-prefix' not in \
-                self.target.execute('dmesg -h', check_exit_code=False)
+        # Check if we have a dmesg from a recent util-linux build, rather than
+        # e.g. busybox's dmesg or the one shipped on some Android versions
+        # (toybox).  Note: BusyBox dmesg does not support -h, but will still
+        # print the help with an exit code of 1
+        help_ = self.target.execute('dmesg -h', check_exit_code=False)
+        self.basic_dmesg = not all(
+            opt in help_
+            for opt in ('--facility', '--force-prefix', '--decode', '--level')
+        )
+
         self.facility = facility
         try:
             needs_root = target.read_sysctl('kernel.dmesg_restrict')