summaryrefslogtreecommitdiff
path: root/lib/util/debug.c
AgeCommit message (Collapse)AuthorFilesLines
2026-01-28lib:util:debug Logging macros for JSON outputGary Lockyer1-0/+46
Add new debug macros for outputting JSON log lines BUG: https://bugzilla.samba.org/show_bug.cgi?id=15898 Signed-off-by: Gary Lockyer <gary@catalyst.net.nz> Reviewed-by: Volker Lendecke <vl@samba.org>
2025-06-18lib:util: Disable logging to syslog for startup messagesAndreas Schneider1-0/+15
D_ERR also is sent to syslog! BUG: https://bugzilla.samba.org/show_bug.cgi?id=15869 Signed-off-by: Andreas Schneider <asn@samba.org> Reviewed-by: Gary Lockyer <gary@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Autobuild-User(master): Andreas Schneider <asn@cryptomilk.org> Autobuild-Date(master): Wed Jun 18 12:10:03 UTC 2025 on atb-devel-224
2024-01-04lib: Confine the copy_no_nl memcpy to debug_gpfs_log()Volker Lendecke1-62/+29
gpfswrap_add_trace() seems not to have a format string that could understand the %.*s notation. While there this removes >4k of r/w memory from every smbd. Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Stefan Metzmacher <metze@samba.org> Autobuild-User(master): Volker Lendecke <vl@samba.org> Autobuild-Date(master): Thu Jan 4 17:06:19 UTC 2024 on atb-devel-224
2024-01-04lib: Avoid memcpy in debug_lttng_log()Volker Lendecke1-9/+12
tracef() understands the %.*s format. Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Stefan Metzmacher <metze@samba.org>
2024-01-04lib: Avoid memcpy in debug_systemd_log()Volker Lendecke1-12/+21
sd_journal_send() understands the %.*s format. Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Stefan Metzmacher <metze@samba.org>
2023-11-24lib/util: add debug_set_forced_log_priority()Stefan Metzmacher1-0/+10
By default the priority for syslog/systemd is derived from the log level of the debug message. But for things like startup messages we want to change the priority temporary, like this: debug_set_forced_log_priority(DBGLVL_NOTICE); D_ERR("Startup...\n"); debug_set_forced_log_priority(-1); BUG: https://bugzilla.samba.org/show_bug.cgi?id=15377 Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Björn Jacke <bjacke@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-01lib/util: Fix building with FORTIFY_SOURCE=2Joseph Sutton1-1/+1
Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-08-28util: Avoid logging to multiple backends for stdout/stderrMartin Schwenke1-23/+13
Commit 83fe7a0316d3e5867a56cfdc51ec17f36ea03889 converted the stdout/stderr logging types to DEBUG_FILE to get a header when using DEBUG_SYSLOG_FORMAT_ALWAYS. However, this causes all configured backends to be invoked. When syslog is one of those backends then this is almost certainly not what is intended. Instead, call debug_file_log() directly in that special case and revert the parts of the above commit that convert to file logging. Most of the changes to debughdrclass() still seem necessary, since they handle the change of debug_syslog_format from a bool to an enum. BUG: https://bugzilla.samba.org/show_bug.cgi?id=15460 Signed-off-by: Martin Schwenke <mschwenke@ddn.com> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Autobuild-User(master): Martin Schwenke <martins@samba.org> Autobuild-Date(master): Mon Aug 28 01:21:07 UTC 2023 on atb-devel-224
2023-04-14debug: Only initialize gpfs wrapper when gpfs logging is enabledChristof Schmitt1-1/+3
This avoids unnecessary attempts to load libgpfs.so when it is not needed. Signed-off-by: Christof Schmitt <cs@samba.org> Reviewed-by: Michael Tokarev <mjt@tls.msk.ru> Reviewed-by: Volker Lendecke <vl@samba.org> Autobuild-User(master): Volker Lendecke <vl@samba.org> Autobuild-Date(master): Fri Apr 14 12:28:23 UTC 2023 on atb-devel-224
2023-04-06lib/util: Add "debug syslog format = always", which logs to stdout in syslog ↵Andrew Bartlett1-8/+31
style Signed-off-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Stefan Metzmacher <metze@samba.org>
2023-01-26debug: Call depth: Indent the debug textPavel Filipenský1-0/+13
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15287 Signed-off-by: Pavel Filipenský <pfilipensky@samba.org> Reviewed-by: Andreas Schneider <asn@samba.org>
2023-01-26debug: Call depth: Print ", depth=..." in the debug headerPavel Filipenský1-0/+10
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15287 Signed-off-by: Pavel Filipenský <pfilipensky@samba.org> Reviewed-by: Andreas Schneider <asn@samba.org>
2023-01-26debug: Call depth: InterfacePavel Filipenský1-0/+11
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15287 Signed-off-by: Pavel Filipenský <pfilipensky@samba.org> Reviewed-by: Andreas Schneider <asn@samba.org>
2023-01-26debug: Fix whitespaces in debug.cPavel Filipenský1-4/+4
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15287 Signed-off-by: Pavel Filipenský <pfilipensky@samba.org> Reviewed-by: Andreas Schneider <asn@samba.org>
2022-12-14lib: Use talloc_asprintf_addbuf() in debug.cVolker Lendecke1-9/+6
Slightly simplify debug_list_class_names_and_levels() Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2022-06-17util/debug: share classname table with testsDouglas Bagnall1-40/+5
The executables generated from lib/util/tests/test_logging.c are used by the samba.tests.logfiles tests to test logging with various smb.confs that assign classes to various files at different levels etc. Previously test_logging.c had its own version of the table; now it shares one with debug.c We put the table in a sub-directory (lib/util/debug-classes/), because adding local_include=True to the wscript_build stanza causes the compiler confusion between <time.h> and lib/util/time.h. Note: there are still two other lists of the class names, in python/samba/tests/logfiles.py and docs-xml/smbdotconf/logging/loglevel.xml. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-06-17debug: add DBG_DEV()Douglas Bagnall1-0/+66
This can be a useful macro when you are trying to track the behaviour of one process out of the dozens that samba starts up, and when your interest is in following it over time, not necessarily in a single stack. In DEVELOPER mode, if you call 'debug_developer_enable()' in the process you're following, then any instances of DBG_DEV() will work like DBG_ERR(), also adding ":DEV:12345:" where "12345" is the pid of th current process. Within debug.c itself, the macro always writes to stderr, because the debug.c functions are not all reentrant. When not in DEVELOPER MODE, the macro evaluates to nothing. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-06-17debug: drop an '#if _SAMBA_BUILD_ == 3'Douglas Bagnall1-15/+7
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-06-16util: Add new debug setting debug_no_stderr_redirectMartin Schwenke1-1/+2
CTDB doesn't want this redirection of stderr to the log file. It expects to be able to capture stderr of subprocesses and log them with a header. This redirection stops that from happening. Unfortunately this has to be a negative option (i.e. "no" in the name) so that the default of 0/false maintains existing behaviour. Note that the default behaviour is sub-optimal because it causes raw data (i.e. debug data without a header) to appear in the log. BUG: https://bugzilla.samba.org/show_bug.cgi?id=15090 Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org>
2022-05-30debug: update comments about setup_logging()Douglas Bagnall1-9/+18
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Andreas Schneider <asn@samba.org> Autobuild-User(master): Andreas Schneider <asn@cryptomilk.org> Autobuild-Date(master): Mon May 30 11:03:47 UTC 2022 on sn-devel-184
2022-05-10debug: add debug_traceid_set/get() interfacePavel Filipenský1-2/+26
Signed-off-by: Pavel Filipenský <pfilipen@redhat.com> Reviewed-by: Andreas Schneider <asn@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2022-02-22util: CID 1499409: Memory - corruptions (OVERLAPPING_COPY)Martin Schwenke1-1/+2
This is quite bizarre: *** CID 1499409: Memory - corruptions (OVERLAPPING_COPY) /lib/util/debug.c: 1742 in dbghdrclass() 1736 sizeof(tvbuf.buf), 1737 "%ld seconds since the Epoch", (long)t); 1738 } 1739 } 1740 1741 ensure_hostname(); >>> CID 1499409: Memory - corruptions (OVERLAPPING_COPY) >>> In the call to function "snprintf", the object pointed to by argument "state.hostname" may overlap with the object pointed to by argument "state.header_str". 1742 state.hs_len = snprintf(state.header_str, 1743 sizeof(state.header_str), 1744 "%s %s %s[%u]: ", 1745 tvbuf.buf, 1746 state.hostname, 1747 state.prog_name, Coverity doesn't explicitly say so but the only way this can happen is if state.hostname is not NUL-terminated within its declared length. ensure_hostname() and debug_set_hostname() ensure NUL-termination, but the caching effect of ensure_hostname() probably stops Coverity from being certain about anything. Try making Coverity happy by using a precision to limit the number of characters from hostname that can be used. Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Andreas Schneider <asn@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Autobuild-User(master): Martin Schwenke <martins@samba.org> Autobuild-Date(master): Tue Feb 22 00:17:12 UTC 2022 on sn-devel-184
2021-11-11lib/debug: in debug_set_logfile() call reopen_logs_internal()Ralph Boehme1-0/+2
This simplifies the logging API for callers that typically would want to set logging by just setup_logging() once without bothering that typically configuration is loaded (via some lpcfg_load*() or lp_load*() varient) which will only then pick up the configured logfile from smb.conf without actually applying the new logifle to the logging subsytem. Therefor our daemons will additionally call reopen_logs() explicitly in their startup code after config is loaded, eg setup_logging(getprogname(), DEBUG_FILE); ... lpcfg_load(lp_ctx, config_file); ... reopen_logs(); By calling reopen_logs_internal() implicitly from debug_set_logfile() there's no need to call reopen_logs() explicitly anymore to apply the logfile. As reopen_logs() will also apply other logging configuration options, we have to keep the explicit calls in the daemon code. But at least this allows consistent logging setup wrt to the logfile in the new cmdline library. BUG: https://bugzilla.samba.org/show_bug.cgi?id=14897 Signed-off-by: Ralph Boehme <slow@samba.org> Reviewed-by: Andreas Schneider <asn@samba.org>
2021-11-11lib/debug: fix fd check before dup'ing to stderrRalph Boehme1-2/+1
Before I added per-class logfile and we had only one fd for the logfile the code looked like this: /* Take over stderr to catch output into logs */ if (state.fd > 0) { if (dup2(state.fd, 2) == -1) { /* Close stderr too, if dup2 can't point it - at the logfile. There really isn't much that can be done on such a fundamental failure... */ close_low_fd(2); } } In the current code the equivalent to state.fd is dbgc_config[DBGC_ALL].fd. BUG: https://bugzilla.samba.org/show_bug.cgi?id=14897 Signed-off-by: Ralph Boehme <slow@samba.org> Reviewed-by: Andreas Schneider <asn@samba.org>
2021-11-01debug: Add debug_syslog_format settingMartin Schwenke1-1/+74
Without debug_hires_timestamp this produces a syslog style header containing: "MON DD HH:MM:SS HOSTNAME PROGNAME[PID] " With debug_hires_timestamp this produces a syslog style header containing: "RFC5424-TIMESTAMP HOSTNAME PROGNAME[PID] " All other settings are ignored. This will be made visible via smb.conf in a subsequent commit. This commit adds some simple hostname handling. It avoids using get_myname() from util.c because using that potentially pulls in all manner of dependencies. No real error handling is done. In the worst case debug_set_hostname() sets the hostname to a truncated version of the given string. Similarly, in an even weirder world, ensure_hostname() sets the hostname to a truncation of "unknown". Both of these are unlikely in all reasonable cases. Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Ralph Boehme <slow@samba.org>
2021-10-14debug: Optimise construction of msg_no_nlMartin Schwenke1-5/+15
If it isn't used then it isn't copied. Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org> Autobuild-User(master): Volker Lendecke <vl@samba.org> Autobuild-Date(master): Thu Oct 14 11:10:40 UTC 2021 on sn-devel-184
2021-10-14debug: Move msg_no_nl to stateMartin Schwenke1-38/+14
This enables an optimisation. Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org>
2021-10-14debug: Optimise early return when header string buffer is fullMartin Schwenke1-6/+3
The existing check is for truncation, not whether the buffer is full. However, if the buffer is full (i.e. hs_len == sizeof(header_str) - 1) then there's no use trying subsequent snprintf() calls because there will be one byte available that already contains the NUL-terminator. A subsequent call will just do a no-op truncation. Check for full buffer instead. This might be confusing because it isn't the standard check that is done after snprintf() calls. Is it worth it for a rare corner case? Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org>
2021-10-14debug: Optimise to avoid walking the header stringMartin Schwenke1-5/+8
strlcat() needs to walk to the end of its first argument. However, but the length of state.header_str is already known, so optimise by manually appending the extra characters if they will fit. Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org>
2021-10-14debug: Optimise construction of header_str_no_nlMartin Schwenke1-4/+34
If it isn't used then it isn't copied. Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org>
2021-10-14debug: Rename variable for consistencyMartin Schwenke1-6/+6
Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org>
2021-10-14debug: Push message length argument down to backend log functionsMartin Schwenke1-18/+34
Optimise because length is now available. Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org>
2021-10-14debug: Add length argument to Debug1()Martin Schwenke1-4/+5
This the first step in avoiding potentially repeated length calculations in the backends. The length is known at call time for most usual callers, so pass it down. Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org>
2021-10-14debug: Avoid debug header being separated from debug textMartin Schwenke1-4/+42
Currently the file backend can produce something like: HEADER1 HEADER2 TEXT2 TEXT1 when different processes try to log at the same time. Avoid this by writing the header and text at the same time using writev(). This means that the header always has to be written by the backend, so update all backends to do this. The non-file backends should behave as before when they were invoked separately to render the header. It might be possible to optimise some of them (e.g. via sd_journal_sendv) but this requires more investigation (e.g. sd_journal_sendv()'s handling of newlines) and is beyond the scope of this change. state.header_str_no_nl takes the place of msg_no_nl for the header, since some of the backends need the no-newline version. It is handled the same was as msg_no_nl: produce the no_nl version exactly once, whether or not it is needed, since this is better than repeating it in several backends. Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org>
2021-10-14debug: Factor out function copy_no_nl()Martin Schwenke1-8/+22
Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org>
2021-10-14debug: Add a level of indirection to ring buffer loggingMartin Schwenke1-3/+9
Add an internal function to do the work and call it. It will be called again in a subsequent commit. Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org>
2021-10-14debug: Move header_str and hs_len to stateMartin Schwenke1-26/+54
They'll need to be accessible by the backends. Note that the snprintf() and strlcat() calls can result in state.hs_len >= sizeof(state.header_str), so state.hs_len needs to be sanitised before any potential use. Previously this wasn't necessary because this value was on the stack, so it couldn't be used after dbghdrclass() returned. Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org>
2021-09-18debug: Remove "override_logfile"Volker Lendecke1-10/+0
The only writer to this variable left with c377845d27d4dcd7. The closest match for override_logfile is is_default_dyn_LOGFILEBASE() with the opposite logic. Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org> Autobuild-User(master): Jeremy Allison <jra@samba.org> Autobuild-Date(master): Sat Sep 18 00:53:28 UTC 2021 on sn-devel-184
2021-07-01lib/util: improve debug message about unknown classesStefan Metzmacher1-2/+1
debug classes registered by vfs modules are not available immediately. Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Ralph Boehme <slow@samba.org>
2021-04-29lib:util: Add debug_get_log_type() functionAndreas Schneider1-0/+5
Signed-off-by: Andreas Schneider <asn@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2020-12-07debug: detect logrotation by checking inode numberRalph Boehme1-2/+29
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14248 Signed-off-by: Ralph Boehme <slow@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2020-12-07debug: pass struct debug_class *config to do_one_check_log_size()Ralph Boehme1-13/+9
Pass a pointer to the struct instead of all struct members individually. No change in behaviour. BUG: https://bugzilla.samba.org/show_bug.cgi?id=14248 Signed-off-by: Ralph Boehme <slow@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2020-12-07debug: pass struct debug_class *config to reopen_one_log()Ralph Boehme1-6/+6
Pass a pointer to the struct instead of all struct members individually. No change in behaviour. BUG: https://bugzilla.samba.org/show_bug.cgi?id=14248 Signed-off-by: Ralph Boehme <slow@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
2020-11-10debug: remove a cast, which makes the Solaris Studio compiler unhappyBjörn Jacke1-1/+1
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14526 Signed-off-by: Bjoern Jacke <bjacke@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2020-07-02lib/debug: set the correct default backend loglevel to MAX_DEBUG_LEVELRalph Boehme1-1/+1
This fixed a regression introduced by commit c83ce5f4f99aef94530411ec82cc03e9935b352d for bug #14345. The backend loglevel globally restricts logging of a particular backend. If this value is smaller then any explicitly configured logging class, logging for this class is skipped. Eg, given the following logging config in smb.conf: log level = 1 auth_json_audit:3@/var/log/samba/samba_auth_audit.log the default class loglevel of 1 (dbgc_config[DBGC_ALL].loglevel) will be assigned to the backend loglevel. So even though the logging class auth_json_audit is configured at level 3, this doesn't become effective as the file backend drops all log messages with a level below 1. BUG: https://bugzilla.samba.org/show_bug.cgi?id=14426 Signed-off-by: Ralph Boehme <slow@samba.org> Reviewed-by: Andreas Schneider <asn@samba.org> Autobuild-User(master): Andreas Schneider <asn@cryptomilk.org> Autobuild-Date(master): Thu Jul 2 13:25:29 UTC 2020 on sn-devel-184
2020-07-02lib/debug: assert file backendRalph Boehme1-6/+6
The debug file backend is a built-in default, if it's missing we're totally screwed. BUG: https://bugzilla.samba.org/show_bug.cgi?id=14426 Signed-off-by: Ralph Boehme <slow@samba.org> Reviewed-by: Andreas Schneider <asn@samba.org>
2020-05-07lib:util: Fix smbclient -l basename dirAmit Kumar1-1/+7
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14345 Pair-Programmed-With: Andreas Schneider <asn@samba.org> Signed-off-by: Andreas Schneider <asn@samba.org> Signed-off-by: Amit Kumar <amitkuma@redhat.com> Reviewed-by: Alexander Bokovoy <ab@samba.org> Autobuild-User(master): Andreas Schneider <asn@cryptomilk.org> Autobuild-Date(master): Thu May 7 10:38:29 UTC 2020 on sn-devel-184
2020-04-22util: Fix signed/unsigned integer comparisonMartin Schwenke1-1/+1
Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Volker Lendecke <vl@samba.org> Autobuild-User(master): Martin Schwenke <martins@samba.org> Autobuild-Date(master): Wed Apr 22 01:48:03 UTC 2020 on sn-devel-184
2020-02-19lib: Fix rotating nonstandard debug class log filesVolker Lendecke1-1/+1
Looking at the same pointer in the loop does not really make sense to me Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Martin Schwenke <martin@meltin.net>
2019-09-01Spelling fixes s/preceeding/preceding/Mathieu Parent1-1/+1
Signed-off-by: Mathieu Parent <math.parent@gmail.com> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>