[PATCH 0/6] nss: Rework debugging

I've been debugging a problem with NSS plugin recently [1] and the fact that I had to recompile libvirt just to enable debugging printings for the NSS plugin turned out very inconvenient. Make the debug printings env var dependant and add a few more printings. 1: https://bugzilla.redhat.com/show_bug.cgi?id=2364285 Michal Prívozník (6): nss: Promote debug message to proper error when time() fails nss: Move logging into a separate file and turn it temporarily on nss: Make logging conditional on an envvar nss: Include filename in debug printings nss: Print module name nss: Debug print JSON files as they are parsed build-aux/syntax-check.mk | 2 +- docs/nss.rst | 13 ++++++ tools/nss/libvirt_nss.c | 8 +++- tools/nss/libvirt_nss.h | 30 +----------- tools/nss/libvirt_nss_leases.c | 12 +++-- tools/nss/libvirt_nss_log.c | 85 ++++++++++++++++++++++++++++++++++ tools/nss/libvirt_nss_log.h | 41 ++++++++++++++++ tools/nss/meson.build | 1 + 8 files changed, 158 insertions(+), 34 deletions(-) create mode 100644 tools/nss/libvirt_nss_log.c create mode 100644 tools/nss/libvirt_nss_log.h -- 2.49.0

From: Michal Privoznik <mprivozn@redhat.com> The difference between DEBUG() and ERROR() macros is that the latter also prints (stringified) errno. Inside of findLease() there's one line where time() is called which is documented as: On error, ((time_t) -1) is returned, and errno is set to indicate the error. Therefore, don't just print DEBUG() but ERROR() so that the cause for the error is printed too. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/nss/libvirt_nss.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/nss/libvirt_nss.c b/tools/nss/libvirt_nss.c index 6ee328a8df..52b769d760 100644 --- a/tools/nss/libvirt_nss.c +++ b/tools/nss/libvirt_nss.c @@ -174,7 +174,7 @@ findLease(const char *name, #endif if ((now = time(NULL)) == (time_t)-1) { - DEBUG("Failed to get time"); + ERROR("Failed to get time"); goto cleanup; } -- 2.49.0

From: Michal Privoznik <mprivozn@redhat.com> Currently, when somebody wants to debug the NSS plugin, they have to change a line in libvirt_nss.h (to enable debug printings) and recompile the module. This may work for us, developers, but we can not expect this from users. For now, this turns debug printings unconditionally on. Making it conditional on an envvar is handled in the next commit. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- build-aux/syntax-check.mk | 2 +- tools/nss/libvirt_nss.h | 30 +-------------- tools/nss/libvirt_nss_log.c | 76 +++++++++++++++++++++++++++++++++++++ tools/nss/libvirt_nss_log.h | 38 +++++++++++++++++++ tools/nss/meson.build | 1 + 5 files changed, 117 insertions(+), 30 deletions(-) create mode 100644 tools/nss/libvirt_nss_log.c create mode 100644 tools/nss/libvirt_nss_log.h diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk index 1303a0ce7e..541b2c5016 100644 --- a/build-aux/syntax-check.mk +++ b/build-aux/syntax-check.mk @@ -1421,7 +1421,7 @@ exclude_file_name_regexp--sc_prohibit_canonicalize_file_name = \ ^(build-aux/syntax-check\.mk|tests/virfilemock\.c)$$ exclude_file_name_regexp--sc_prohibit_raw_allocation = \ - ^(docs/advanced-tests\.rst|src/util/viralloc\.[ch]|examples/.*|tests/(securityselinuxhelper|(vircgroup|nss)mock|commandhelper)\.c|tools/wireshark/src/packet-libvirt\.c|tools/nss/libvirt_nss(_leases|_macs)?\.[ch])$$ + ^(docs/advanced-tests\.rst|src/util/viralloc\.[ch]|examples/.*|tests/(securityselinuxhelper|(vircgroup|nss)mock|commandhelper)\.c|tools/wireshark/src/packet-libvirt\.c|tools/nss/libvirt_nss(_leases|_log|_macs)?\.[ch])$$ exclude_file_name_regexp--sc_prohibit_readlink = \ ^src/(util/virutil|lxc/lxc_container)\.c$$ diff --git a/tools/nss/libvirt_nss.h b/tools/nss/libvirt_nss.h index 84db044409..e09fce9715 100644 --- a/tools/nss/libvirt_nss.h +++ b/tools/nss/libvirt_nss.h @@ -31,35 +31,7 @@ #include <netdb.h> #include <stdlib.h> - -#if 0 -# include <errno.h> -# include <stdio.h> -# include <string.h> -# define NULLSTR(s) ((s) ? (s) : "<null>") -# define ERROR(...) \ -do { \ - int saved_errno = errno; \ - const size_t ebuf_size = 512; \ - g_autofree char *ebuf = calloc(ebuf_size, sizeof(*ebuf)); \ - if (ebuf) \ - strerror_r(saved_errno, ebuf, ebuf_size); \ - fprintf(stderr, "ERROR %s:%d : ", __FUNCTION__, __LINE__); \ - fprintf(stderr, __VA_ARGS__); \ - fprintf(stderr, " : %s\n", NULLSTR(ebuf)); \ - fprintf(stderr, "\n"); \ -} while (0) - -# define DEBUG(...) \ -do { \ - fprintf(stderr, "DEBUG %s:%d : ", __FUNCTION__, __LINE__); \ - fprintf(stderr, __VA_ARGS__); \ - fprintf(stderr, "\n"); \ -} while (0) -#else -# define ERROR(...) do { } while (0) -# define DEBUG(...) do { } while (0) -#endif +#include "libvirt_nss_log.h" #if !defined(LIBVIRT_NSS_GUEST) # define NSS_NAME(s) _nss_libvirt_##s##_r diff --git a/tools/nss/libvirt_nss_log.c b/tools/nss/libvirt_nss_log.c new file mode 100644 index 0000000000..0863897c07 --- /dev/null +++ b/tools/nss/libvirt_nss_log.c @@ -0,0 +1,76 @@ +/* + * libvirt_nss_log: Logging for Name Service Switch plugin + * + * Copyright (C) 2025 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <stdarg.h> + +#include "libvirt_nss_log.h" +#include "libvirt_nss.h" + +#define NULLSTR(s) ((s) ? (s) : "<null>") + +static const char * __attribute__((returns_nonnull)) +nssLogPriorityToString(nssLogPriority prio) +{ + switch (prio) { + case NSS_DEBUG: + return "DEBUG"; + case NSS_ERROR: + return "ERROR"; + } + + return ""; +} + +void +nssLog(nssLogPriority prio, + const char *func, + int linenr, + const char *fmt, ...) +{ + int saved_errno = errno; + const size_t ebuf_size = 512; + g_autofree char *ebuf = NULL; + va_list ap; + + fprintf(stderr, "%s %s:%d : ", nssLogPriorityToString(prio), func, linenr); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + switch (prio) { + case NSS_DEBUG: + break; + + case NSS_ERROR: + ebuf = calloc(ebuf_size, sizeof(*ebuf)); + if (ebuf) + strerror_r(saved_errno, ebuf, ebuf_size); + fprintf(stderr, " : %s", NULLSTR(ebuf)); + break; + } + + fprintf(stderr, "\n"); +} diff --git a/tools/nss/libvirt_nss_log.h b/tools/nss/libvirt_nss_log.h new file mode 100644 index 0000000000..bac64d4bc7 --- /dev/null +++ b/tools/nss/libvirt_nss_log.h @@ -0,0 +1,38 @@ +/* + * libvirt_nss_log: Logging for Name Service Switch plugin + * + * Copyright (C) 2025 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */ + +#pragma once + +typedef enum { + NSS_DEBUG, + NSS_ERROR, +} nssLogPriority; + +#define DEBUG(...) \ + nssLog(NSS_DEBUG, __FUNCTION__, __LINE__, __VA_ARGS__) + +#define ERROR(...) \ + nssLog(NSS_ERROR, __FUNCTION__, __LINE__, __VA_ARGS__) + +void +nssLog(nssLogPriority prio, + const char *func, + int linenr, + const char *fmt, ...) __attribute__ ((format(printf, 4, 5))); diff --git a/tools/nss/meson.build b/tools/nss/meson.build index 38bba2d616..5893952adc 100644 --- a/tools/nss/meson.build +++ b/tools/nss/meson.build @@ -13,6 +13,7 @@ endif nss_sources = [ 'libvirt_nss.c', 'libvirt_nss_leases.c', + 'libvirt_nss_log.c', ] nss_guest_sources = [ -- 2.49.0

On a Friday in 2025, Michal Privoznik via Devel wrote:
From: Michal Privoznik <mprivozn@redhat.com>
Currently, when somebody wants to debug the NSS plugin, they have to change a line in libvirt_nss.h (to enable debug printings) and recompile the module. This may work for us, developers, but we can not expect this from users.
For now, this turns debug printings unconditionally on. Making it conditional on an envvar is handled in the next commit.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- build-aux/syntax-check.mk | 2 +- tools/nss/libvirt_nss.h | 30 +-------------- tools/nss/libvirt_nss_log.c | 76 +++++++++++++++++++++++++++++++++++++ tools/nss/libvirt_nss_log.h | 38 +++++++++++++++++++ tools/nss/meson.build | 1 + 5 files changed, 117 insertions(+), 30 deletions(-) create mode 100644 tools/nss/libvirt_nss_log.c create mode 100644 tools/nss/libvirt_nss_log.h
diff --git a/tools/nss/libvirt_nss_log.c b/tools/nss/libvirt_nss_log.c new file mode 100644 index 0000000000..0863897c07 --- /dev/null +++ b/tools/nss/libvirt_nss_log.c @@ -0,0 +1,76 @@ +/* + * libvirt_nss_log: Logging for Name Service Switch plugin + * + * Copyright (C) 2025 Red Hat, Inc. + *
+ * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>.
Can we replace this with just: SPDX-License-Identifier: LGPL-2.1-or-later
+ */ + +#include <config.h> + +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <stdarg.h> + +#include "libvirt_nss_log.h" +#include "libvirt_nss.h" + +#define NULLSTR(s) ((s) ? (s) : "<null>") + +static const char * __attribute__((returns_nonnull)) +nssLogPriorityToString(nssLogPriority prio) +{ + switch (prio) { + case NSS_DEBUG: + return "DEBUG"; + case NSS_ERROR: + return "ERROR"; + } + + return ""; +} + +void +nssLog(nssLogPriority prio, + const char *func, + int linenr, + const char *fmt, ...) +{ + int saved_errno = errno; + const size_t ebuf_size = 512; + g_autofree char *ebuf = NULL; + va_list ap; + + fprintf(stderr, "%s %s:%d : ", nssLogPriorityToString(prio), func, linenr); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + switch (prio) { + case NSS_DEBUG: + break; + + case NSS_ERROR: + ebuf = calloc(ebuf_size, sizeof(*ebuf)); + if (ebuf) + strerror_r(saved_errno, ebuf, ebuf_size);
clang complains: ../../work/libvirt/tools/nss/libvirt_nss_log.c:79:13: error: ignoring return value of function declared with 'warn_unused_result' attribute [-Werror,-Wunused-result] 79 | strerror_r(saved_errno, ebuf, ebuf_size); | ^~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ It seems we're building with GNU_SOURCE, so we should print the char* returned by the function; not our allocated buffer.
+ fprintf(stderr, " : %s", NULLSTR(ebuf)); + break; + } + + fprintf(stderr, "\n"); +} diff --git a/tools/nss/libvirt_nss_log.h b/tools/nss/libvirt_nss_log.h new file mode 100644 index 0000000000..bac64d4bc7 --- /dev/null +++ b/tools/nss/libvirt_nss_log.h @@ -0,0 +1,38 @@ +/* + * libvirt_nss_log: Logging for Name Service Switch plugin + * + * Copyright (C) 2025 Red Hat, Inc. + *
+ * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>. + */
Same comment about SPDX here. Jano
+ +#pragma once + +typedef enum { + NSS_DEBUG, + NSS_ERROR, +} nssLogPriority; + +#define DEBUG(...) \ + nssLog(NSS_DEBUG, __FUNCTION__, __LINE__, __VA_ARGS__) + +#define ERROR(...) \ + nssLog(NSS_ERROR, __FUNCTION__, __LINE__, __VA_ARGS__) + +void +nssLog(nssLogPriority prio, + const char *func, + int linenr, + const char *fmt, ...) __attribute__ ((format(printf, 4, 5))); diff --git a/tools/nss/meson.build b/tools/nss/meson.build index 38bba2d616..5893952adc 100644 --- a/tools/nss/meson.build +++ b/tools/nss/meson.build @@ -13,6 +13,7 @@ endif nss_sources = [ 'libvirt_nss.c', 'libvirt_nss_leases.c', + 'libvirt_nss_log.c', ]
nss_guest_sources = [ -- 2.49.0

On 7/16/25 13:27, Ján Tomko wrote:
On a Friday in 2025, Michal Privoznik via Devel wrote:
From: Michal Privoznik <mprivozn@redhat.com>
Currently, when somebody wants to debug the NSS plugin, they have to change a line in libvirt_nss.h (to enable debug printings) and recompile the module. This may work for us, developers, but we can not expect this from users.
For now, this turns debug printings unconditionally on. Making it conditional on an envvar is handled in the next commit.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- build-aux/syntax-check.mk | 2 +- tools/nss/libvirt_nss.h | 30 +-------------- tools/nss/libvirt_nss_log.c | 76 +++++++++++++++++++++++++++++++++++++ tools/nss/libvirt_nss_log.h | 38 +++++++++++++++++++ tools/nss/meson.build | 1 + 5 files changed, 117 insertions(+), 30 deletions(-) create mode 100644 tools/nss/libvirt_nss_log.c create mode 100644 tools/nss/libvirt_nss_log.h
diff --git a/tools/nss/libvirt_nss_log.c b/tools/nss/libvirt_nss_log.c new file mode 100644 index 0000000000..0863897c07 --- /dev/null +++ b/tools/nss/libvirt_nss_log.c @@ -0,0 +1,76 @@ +/* + * libvirt_nss_log: Logging for Name Service Switch plugin + * + * Copyright (C) 2025 Red Hat, Inc. + *
+ * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * <http://www.gnu.org/licenses/>.
Can we replace this with just: SPDX-License-Identifier: LGPL-2.1-or-later
+ */ + +#include <config.h> + +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <stdarg.h> + +#include "libvirt_nss_log.h" +#include "libvirt_nss.h" + +#define NULLSTR(s) ((s) ? (s) : "<null>") + +static const char * __attribute__((returns_nonnull)) +nssLogPriorityToString(nssLogPriority prio) +{ + switch (prio) { + case NSS_DEBUG: + return "DEBUG"; + case NSS_ERROR: + return "ERROR"; + } + + return ""; +} + +void +nssLog(nssLogPriority prio, + const char *func, + int linenr, + const char *fmt, ...) +{ + int saved_errno = errno; + const size_t ebuf_size = 512; + g_autofree char *ebuf = NULL; + va_list ap; + + fprintf(stderr, "%s %s:%d : ", nssLogPriorityToString(prio), func, linenr); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + switch (prio) { + case NSS_DEBUG: + break; + + case NSS_ERROR: + ebuf = calloc(ebuf_size, sizeof(*ebuf)); + if (ebuf) + strerror_r(saved_errno, ebuf, ebuf_size);
clang complains: ../../work/libvirt/tools/nss/libvirt_nss_log.c:79:13: error: ignoring return value of function declared with 'warn_unused_result' attribute [- Werror,-Wunused-result] 79 | strerror_r(saved_errno, ebuf, ebuf_size); | ^~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ah, gcc complains too. But only after I turn on optimizations (by default I compile as 'meson setup --optimization=0 ...' so that I'm able to gdb/valgrind the thing). I can't help but wonder why unused-result depends on optimizations.
It seems we're building with GNU_SOURCE, so we should print the char* returned by the function; not our allocated buffer.
which wouldn't fly on FreeBSD where are no GNU extensions. Le sigh. Fortunately, we do not need GNU_SOURCE for this. Michal

From: Michal Privoznik <mprivozn@redhat.com> As promised in previous commit, make NSS modules silent by default and enable debug printings if LIBVIRT_NSS_DEBUG envvar is set. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- docs/nss.rst | 13 +++++++++++++ tools/nss/libvirt_nss_log.c | 4 ++++ tools/nss/libvirt_nss_log.h | 2 ++ 3 files changed, 19 insertions(+) diff --git a/docs/nss.rst b/docs/nss.rst index be137d987b..3df85cee0a 100644 --- a/docs/nss.rst +++ b/docs/nss.rst @@ -111,6 +111,19 @@ their IP addresses in any other way (usermode networking, assigned network devices and so on) will not be able to have their hostnames resolved through it. +Debugging +--------- + +:since:`Since 11.5.0` both NSS modules check for ``LIBVIRT_NSS_DEBUG`` +environment variable¸ which if set to any value turns on printing of debug and +error messages onto standard error output. This can be useful when debugging +either of the module. + +:: + + $ LIBVIRT_NSS_DEBUG=1 getent hosts mydomain + + Alternatives ------------ diff --git a/tools/nss/libvirt_nss_log.c b/tools/nss/libvirt_nss_log.c index 0863897c07..9a28a91800 100644 --- a/tools/nss/libvirt_nss_log.c +++ b/tools/nss/libvirt_nss_log.c @@ -24,6 +24,7 @@ #include <stdio.h> #include <string.h> #include <stdarg.h> +#include <stdlib.h> #include "libvirt_nss_log.h" #include "libvirt_nss.h" @@ -54,6 +55,9 @@ nssLog(nssLogPriority prio, g_autofree char *ebuf = NULL; va_list ap; + if (!getenv(NSS_LOG_ENV_VAR)) + return; + fprintf(stderr, "%s %s:%d : ", nssLogPriorityToString(prio), func, linenr); va_start(ap, fmt); diff --git a/tools/nss/libvirt_nss_log.h b/tools/nss/libvirt_nss_log.h index bac64d4bc7..25926fb6de 100644 --- a/tools/nss/libvirt_nss_log.h +++ b/tools/nss/libvirt_nss_log.h @@ -31,6 +31,8 @@ typedef enum { #define ERROR(...) \ nssLog(NSS_ERROR, __FUNCTION__, __LINE__, __VA_ARGS__) +#define NSS_LOG_ENV_VAR "LIBVIRT_NSS_DEBUG" + void nssLog(nssLogPriority prio, const char *func, -- 2.49.0

On a Friday in 2025, Michal Privoznik via Devel wrote:
From: Michal Privoznik <mprivozn@redhat.com>
As promised in previous commit, make NSS modules silent by default and enable debug printings if LIBVIRT_NSS_DEBUG envvar is set.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- docs/nss.rst | 13 +++++++++++++ tools/nss/libvirt_nss_log.c | 4 ++++ tools/nss/libvirt_nss_log.h | 2 ++ 3 files changed, 19 insertions(+)
diff --git a/docs/nss.rst b/docs/nss.rst index be137d987b..3df85cee0a 100644 --- a/docs/nss.rst +++ b/docs/nss.rst @@ -111,6 +111,19 @@ their IP addresses in any other way (usermode networking, assigned network devices and so on) will not be able to have their hostnames resolved through it.
+Debugging +--------- + +:since:`Since 11.5.0` both NSS modules check for ``LIBVIRT_NSS_DEBUG``
11.6.0 Jano
+environment variable¸ which if set to any value turns on printing of debug and +error messages onto standard error output. This can be useful when debugging +either of the module. + +:: + + $ LIBVIRT_NSS_DEBUG=1 getent hosts mydomain + +

From: Michal Privoznik <mprivozn@redhat.com> Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/nss/libvirt_nss_log.c | 7 ++++++- tools/nss/libvirt_nss_log.h | 7 ++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/tools/nss/libvirt_nss_log.c b/tools/nss/libvirt_nss_log.c index 9a28a91800..04fc049376 100644 --- a/tools/nss/libvirt_nss_log.c +++ b/tools/nss/libvirt_nss_log.c @@ -46,6 +46,7 @@ nssLogPriorityToString(nssLogPriority prio) void nssLog(nssLogPriority prio, + const char *filename, const char *func, int linenr, const char *fmt, ...) @@ -58,7 +59,11 @@ nssLog(nssLogPriority prio, if (!getenv(NSS_LOG_ENV_VAR)) return; - fprintf(stderr, "%s %s:%d : ", nssLogPriorityToString(prio), func, linenr); + if ((filename = strrchr(filename, '/'))) + filename++; + + fprintf(stderr, "%s %s:%s():%d : ", + nssLogPriorityToString(prio), NULLSTR(filename), func, linenr); va_start(ap, fmt); vfprintf(stderr, fmt, ap); diff --git a/tools/nss/libvirt_nss_log.h b/tools/nss/libvirt_nss_log.h index 25926fb6de..60696c7b08 100644 --- a/tools/nss/libvirt_nss_log.h +++ b/tools/nss/libvirt_nss_log.h @@ -26,15 +26,16 @@ typedef enum { } nssLogPriority; #define DEBUG(...) \ - nssLog(NSS_DEBUG, __FUNCTION__, __LINE__, __VA_ARGS__) + nssLog(NSS_DEBUG, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) #define ERROR(...) \ - nssLog(NSS_ERROR, __FUNCTION__, __LINE__, __VA_ARGS__) + nssLog(NSS_ERROR, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) #define NSS_LOG_ENV_VAR "LIBVIRT_NSS_DEBUG" void nssLog(nssLogPriority prio, + const char *filename, const char *func, int linenr, - const char *fmt, ...) __attribute__ ((format(printf, 4, 5))); + const char *fmt, ...) __attribute__ ((format(printf, 5, 6))); -- 2.49.0

From: Michal Privoznik <mprivozn@redhat.com> Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/nss/libvirt_nss.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/nss/libvirt_nss.c b/tools/nss/libvirt_nss.c index 52b769d760..fe9a6bdb37 100644 --- a/tools/nss/libvirt_nss.c +++ b/tools/nss/libvirt_nss.c @@ -121,6 +121,12 @@ findLease(const char *name, *naddress = 0; *found = false; +#if defined(LIBVIRT_NSS_GUEST) + DEBUG("NSS module: libvirt_guest"); +#else + DEBUG("NSS module: libvirt"); +#endif + if (af != AF_UNSPEC && af != AF_INET && af != AF_INET6) { errno = EAFNOSUPPORT; goto cleanup; -- 2.49.0

From: Michal Privoznik <mprivozn@redhat.com> It may help in case the NSS plugin ever faces a corrupted JSON Signed-off-by: Michal Privoznik <mprivozn@redhat.com> --- tools/nss/libvirt_nss_leases.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tools/nss/libvirt_nss_leases.c b/tools/nss/libvirt_nss_leases.c index 6624df2928..c1b4cd7392 100644 --- a/tools/nss/libvirt_nss_leases.c +++ b/tools/nss/libvirt_nss_leases.c @@ -265,10 +265,9 @@ findLeases(const char *file, json_tokener *tok = NULL; enum json_tokener_error jerr = json_tokener_error_parse_eof; int jsonflags = JSON_TOKENER_STRICT | JSON_TOKENER_VALIDATE_UTF8; - char line[1024]; size_t nreadTotal = 0; - int rv; + DEBUG("Processing %s", file); if ((fd = open(file, O_RDONLY)) < 0) { ERROR("Cannot open %s", file); goto cleanup; @@ -282,7 +281,11 @@ findLeases(const char *file, json_tokener_set_flags(tok, jsonflags); do { - rv = read(fd, line, sizeof(line)); + char line[1024] = { 0 }; + ssize_t rv; + + rv = read(fd, line, sizeof(line) - 1); + DEBUG("read: rv=%zd line='%s'", rv, line); if (rv < 0) goto cleanup; if (rv == 0) @@ -293,6 +296,9 @@ findLeases(const char *file, jerr = json_tokener_get_error(tok); } while (jerr == json_tokener_continue); + DEBUG("Done reading, nreadTotal=%zu, jerr=%d '%s'", + nreadTotal, (int)jerr, json_tokener_error_desc(jerr)); + if (nreadTotal == 0) { ret = 0; goto cleanup; -- 2.49.0

On a Friday in 2025, Michal Privoznik via Devel wrote:
I've been debugging a problem with NSS plugin recently [1] and the fact that I had to recompile libvirt just to enable debugging printings for the NSS plugin turned out very inconvenient. Make the debug printings env var dependant and add a few more printings.
1: https://bugzilla.redhat.com/show_bug.cgi?id=2364285
Michal Prívozník (6): nss: Promote debug message to proper error when time() fails nss: Move logging into a separate file and turn it temporarily on nss: Make logging conditional on an envvar nss: Include filename in debug printings nss: Print module name nss: Debug print JSON files as they are parsed
build-aux/syntax-check.mk | 2 +- docs/nss.rst | 13 ++++++ tools/nss/libvirt_nss.c | 8 +++- tools/nss/libvirt_nss.h | 30 +----------- tools/nss/libvirt_nss_leases.c | 12 +++-- tools/nss/libvirt_nss_log.c | 85 ++++++++++++++++++++++++++++++++++ tools/nss/libvirt_nss_log.h | 41 ++++++++++++++++ tools/nss/meson.build | 1 + 8 files changed, 158 insertions(+), 34 deletions(-) create mode 100644 tools/nss/libvirt_nss_log.c create mode 100644 tools/nss/libvirt_nss_log.h
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano
participants (3)
-
Ján Tomko
-
Michal Privoznik
-
Michal Prívozník