LCOV - code coverage report
Current view: top level - src - cdebuglog.c (source / functions) Coverage Total Hit
Test: c_collections 2.0.0 Lines: 97.6 % 42 41
Test Date: 2026-11-15 18:57:05 Functions: 100.0 % 4 4
Legend: Lines: hit not hit

            Line data    Source code
       1              : /*
       2              : MIT License
       3              : 
       4              : Copyright (c) 2026 - A bunch of nerds
       5              : 
       6              : Permission is hereby granted, free of charge, to any person obtaining a copy
       7              : of this software and associated documentation files (the "Software"), to deal
       8              : in the Software without restriction, including without limitation the rights
       9              : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      10              : copies of the Software, and to permit persons to whom the Software is
      11              : furnished to do so, subject to the following conditions:
      12              : 
      13              : The above copyright notice and this permission notice shall be included in all
      14              : copies or substantial portions of the Software.
      15              : 
      16              : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      17              : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      18              : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      19              : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      20              : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      21              : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      22              : SOFTWARE.
      23              : */
      24              : 
      25              : // See cdebuglog.h for why this file needs both RUNNING_UNIT_TESTS and
      26              : // CDEBUGLOG_ENABLED. Without both it's an empty translation unit.
      27              : #if defined(RUNNING_UNIT_TESTS) && defined(CDEBUGLOG_ENABLED)
      28              : 
      29              : #include <internal/cdebuglog.h>
      30              : #include <limits.h>
      31              : #include <stdarg.h>
      32              : #include <stdatomic.h>
      33              : #include <stdbool.h>
      34              : #include <stdio.h>
      35              : #include <stdlib.h>
      36              : #include <string.h>
      37              : #include <unistd.h>
      38              : 
      39              : #include "common.h"
      40              : 
      41              : // One buffer for the whole process, so the lines of all callers come out in
      42              : // their real order. It's large because a full run collects several MB.
      43              : #define CDEBUGLOG_BUF_CAP (8U * 1024U * 1024U)
      44              : // cdebuglog_write() flushes once the buffer passes this, so a long run comes
      45              : // out in several bursts near the tests that wrote them, instead of one burst at
      46              : // the first fork() or at exit.
      47              : #define CDEBUGLOG_FLUSH_THRESHOLD (CDEBUGLOG_BUF_CAP / 8U)
      48              : // The shared buffer, its fill level and whether the atexit flush is registered.
      49              : static char cdebuglog_buf[CDEBUGLOG_BUF_CAP];
      50              : static size_t cdebuglog_pos = 0;
      51              : static _Atomic bool cdebuglog_atexit_registered = false;
      52              : // Makes copying a line and flushing exclusive, so a flush never writes a
      53              : // half-copied line. It's initialized once through cdebuglog_lock_once.
      54              : static ccol__mutex cdebuglog_lock;
      55              : static ccol__once_flag cdebuglog_lock_once = CCOL__ONCE_INIT;
      56              : 
      57              : // Initializes cdebuglog_lock, once through cdebuglog_lock_once, because the
      58              : // library initializes no lock statically.
      59            1 : static void cdebuglog_lock_init(void) {
      60            1 :   (void)ccol__mutex_init(cdebuglog_lock);
      61            1 : }
      62              : 
      63              : // Writes cdebuglog_buf to stderr and empties it, for the flush entry points and
      64              : // the threshold flush of cdebuglog_write. A failed write is ignored, since this
      65              : // is a best-effort diagnostic. The caller holds cdebuglog_lock.
      66            1 : static void cdebuglog_flush_locked(void) {
      67            1 :   if (cdebuglog_pos > 0) {
      68            1 :     ssize_t wn = write(STDERR_FILENO, cdebuglog_buf, cdebuglog_pos);
      69            1 :     (void)wn;
      70              :   }
      71            1 :   cdebuglog_pos = 0;
      72            0 : }
      73              : 
      74              : // Writes the buffer to stderr and empties it, for a caller that is about to end
      75              : // the process without atexit() or wants its lines out now. It takes
      76              : // cdebuglog_lock, so no half-copied line goes out.
      77            1 : void cdebuglog_flush(void) {
      78            1 :   ccol__call_once(cdebuglog_lock_once, cdebuglog_lock_init);
      79            1 :   ccol__mutex_lock(cdebuglog_lock);
      80            1 :   cdebuglog_flush_locked();
      81            1 :   ccol__mutex_unlock(cdebuglog_lock);
      82            1 : }
      83              : 
      84              : // Flushes at exit, so the lines of an ordinary run aren't lost. cdebuglog_write
      85              : // registers it.
      86            1 : static void cdebuglog_atexit(void) { cdebuglog_flush(); }
      87              : 
      88              : // Formats one line and appends it to cdebuglog_buf under cdebuglog_lock. It's
      89              : // the logging call of a suite that chases a timing-sensitive failure, where a
      90              : // write per line would disturb the timing. It flushes once the buffer passes
      91              : // CDEBUGLOG_FLUSH_THRESHOLD, and drops a line that doesn't fit. The first call
      92              : // of a process sets cdebuglog_atexit_registered, registers the atexit flush and
      93              : // logs the umask and the working directory once, which helps explain a later
      94              : // permission mismatch. The umask comes from /proc/self/status, because umask(2)
      95              : // can only be read by setting it, and that would race with files other threads
      96              : // create.
      97            7 : void cdebuglog_write(const char *fmt, ...) {
      98            7 :   if (!atomic_exchange(&cdebuglog_atexit_registered, true)) {
      99            1 :     atexit(cdebuglog_atexit);
     100            1 :     char um[16] = "unknown";
     101            1 :     FILE *st = fopen("/proc/self/status", "r");
     102            1 :     if (st) {
     103              :       char l[256];
     104            2 :       while (fgets(l, sizeof(l), st))
     105            2 :         if (sscanf(l, "Umask: %15s", um) == 1) break;
     106            1 :       fclose(st);
     107              :     }
     108            1 :     char cwd[PATH_MAX];
     109            1 :     if (!getcwd(cwd, sizeof(cwd))) strcpy(cwd, "(unknown)");
     110            1 :     cdebuglog_write("[DEBUG_PROCSTART] pid=%d umask=%s cwd=%s\n", (int)getpid(),
     111              :                     um, cwd);
     112              :   }
     113              : 
     114            7 :   char line[512];
     115            7 :   va_list ap;
     116            7 :   va_start(ap, fmt);
     117            7 :   int len = vsnprintf(line, sizeof(line), fmt, ap);
     118            7 :   va_end(ap);
     119            7 :   if (len <= 0) return;
     120            7 :   size_t ulen = (size_t)len < sizeof(line) ? (size_t)len : sizeof(line) - 1;
     121              : 
     122            7 :   ccol__call_once(cdebuglog_lock_once, cdebuglog_lock_init);
     123            7 :   ccol__mutex_lock(cdebuglog_lock);
     124            7 :   if (cdebuglog_pos + ulen <= CDEBUGLOG_BUF_CAP) {
     125            7 :     memcpy(cdebuglog_buf + cdebuglog_pos, line, ulen);
     126            7 :     cdebuglog_pos += ulen;
     127              :   }
     128            7 :   if (cdebuglog_pos >= CDEBUGLOG_FLUSH_THRESHOLD) cdebuglog_flush_locked();
     129            7 :   ccol__mutex_unlock(cdebuglog_lock);
     130              : }
     131              : 
     132              : #endif
        

Generated by: LCOV version 2.0-1