@samitouri / QOSamiQemu / commits / d808d32f96

tests/qtest: Make qtest_get_arch() cleverer

The qtest_get_arch() function tries to determine the architecture under test by extracting it from the binary name as provided in QTEST_QEMU_BINARY. The current logic finds the last '-' in the string and assumes everything beyond it is the architecture name. Although we also look for the substring "-system-", the only effect this check has is that we will exit with an error if it is not present. Because the logic at the moment is very simplistic, although it is possible to provide more complex commands than a bare QEMU binary path, such as: QTEST_QEMU_BINARY='rr record ./qemu-system-x86_64' it is not possible to provide extra arguments to QEMU, such as: QTEST_QEMU_BINARY='./qemu-system-x86_64 -d trace:foo' Because the "-system-" check and the "find the architecture" check are not the same, the latter example will pass the "we found -system-" check and not notice that the "architecture name" it has found starts further on in the string; so rather than printing an error it will return "d trace:foo" to the test. Improve the "find the architecture name" logic to look for the rightmost occurrence of the substring "-system-" in QTEST_QEMU_BINARY, and take the architecture name as starting there and continuing until the first whitespace character or the end of the string. Because we now need to potentially modify the environment variable string to terminate the architecture name if it is not the last part of the string, we make a copy of it which we cache in a static variable. This lets us avoid having to modify all the callers to get them to take ownership of the returned string. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Link: https://lore.kernel.org/qemu-devel/20260427150007.1185559-1-peter.maydell@linaro.org Signed-off-by: Fabiano Rosas <farosas@suse.de>

Peter Maydell committed Apr 27, 2026 at 16:00 UTC d808d32f963bdd038847ba18b7c022065e695517
1 file changed +29 -12
tests/qtest/libqtest.c
+29 -12
@@ -1015,22 +1015,39 @@ char *qtest_hmp(QTestState *s, const char *fmt, ...)
1015
1016 const char *qtest_get_arch(void)
1017 {
1018 - const char *qemu = qtest_qemu_binary(NULL);
1019 - const char *end = strrchr(qemu, '-');
1018 + /*
1019 + * We find and cache the architecture name once, because we need to
1020 + * allocate memory to hold it. This memory will stay around for
1021 + * the lifetime of this test process.
1022 + */
1023 + static const char *arch;
1024
1021 - if (!end) {
1022 - fprintf(stderr, "Can't determine architecture from binary name.\n");
1023 - exit(1);
1024 - }
1025 + if (!arch) {
1026 + /*
1027 + * Find the rightmost occurrence of "-system-"; the architecture
1028 + * name runs from there to the next whitespace.
1029 + */
1030 + const char *qemu = qtest_qemu_binary(NULL);
1031 + const char *sysstr = g_strrstr(qemu, "-system-");
1032 +
1033 + if (sysstr) {
1034 + g_auto(GStrv) tokens = g_strsplit_set(sysstr + strlen("-system-"),
1035 + " \t", 2);
1036 + if (tokens && tokens[0]) {
1037 + arch = g_steal_pointer(&tokens[0]);
1038 + }
1039 + }
1040
1026 - if (!strstr(qemu, "-system-")) {
1027 - fprintf(stderr, "QTEST_QEMU_BINARY must end with *-system-<arch> "
1028 - "where 'arch' is the target\narchitecture (x86_64, aarch64, "
1029 - "etc).\n");
1030 - exit(1);
1041 + if (!arch) {
1042 + fprintf(stderr, "Can't determine architecture from binary name.\n"
1043 + "QTEST_QEMU_BINARY must include *-system-<arch> where "
1044 + "'arch' is the target architecture "
1045 + "(x86_64, aarch64, etc).\n");
1046 + exit(1);
1047 + }
1048 }
1049
1033 - return end + 1;
1050 + return arch;
1051 }
1052
1053 static bool qtest_qom_has_concrete_type(const char *parent_typename,