[LTP] [PATCH v2 3/3] lib: Prefer LTP_DEBUG over -D

Petr Vorel
2026-03-26T17:05:54Z

Environment variable has usually higher preference than getopt.
Also document it.

Fixes: e434de62b4 ("lib: Extend LTP_ENABLE_DEBUG to support verbosity levels")
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
doc/users/setup_tests.rst | 2 +-
lib/tst_test.c | 26 ++++++++++++++------------
2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/doc/users/setup_tests.rst b/doc/users/setup_tests.rst
index a42c397feb..7e7fb9f24c 100644
--- a/doc/users/setup_tests.rst
+++ b/doc/users/setup_tests.rst
@@ -92,7 +92,7 @@ users.

* - LTP_DEBUG
- Enable debug info (value ``1`` (``y``) or ``2`` for more verbose level).
- Equivalent of ``-D`` parameter.
+ Equivalent of ``-D`` parameter (but LTP_DEBUG has higher preference).

Test specific environment variables
-----------------------------------
diff --git a/lib/tst_test.c b/lib/tst_test.c
index 6b41c6e28f..21a299f98f 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -654,11 +654,11 @@ static struct option {
char *optstr;
char *help;
} options[] = {
- {"h", "-h Prints this help"},
- {"i:", "-i n Execute test n times"},
- {"I:", "-I x Execute test for n seconds"},
- {"D::", "-D[1,2] Prints debug information"},
- {"V", "-V Prints LTP version"},
+ {"h", "-h Prints this help"},
+ {"i:", "-i n Execute test n times"},
+ {"I:", "-I x Execute test for n seconds"},
+ {"D::", "-D[1,2] Prints debug information (can be overwriten by LTP_DEBUG"},
+ {"V", "-V Prints LTP version"},
};

static void print_help(void)
@@ -825,13 +825,15 @@ static void parse_opts(int argc, char *argv[])
tst_brk(TBROK, "Invalid option");
break;
case 'D':
- if (optarg)
- context->tdebug = SAFE_STRTOL(optarg, 1, 2);
- else
- context->tdebug = 1;
-
- if (context->tdebug)
- tst_res(TINFO, "Enabling debug info (level %d)", context->tdebug);
+ if (!getenv("LTP_DEBUG")) {
+ if (optarg)
+ context->tdebug = SAFE_STRTOL(optarg, 1, 2);
+ else
+ context->tdebug = 1;
+
+ if (context->tdebug)
+ tst_res(TINFO, "Enabling debug info (level %d)", context->tdebug);
+ }
break;
case 'h':
print_help();
--
2.53.0