summaryrefslogtreecommitdiffstats
path: root/src/log.c
blob: 0829dd3d3e902de8de6b303ca030a3f71877f886 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
 * This file is part of libplacebo.
 *
 * libplacebo 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.
 *
 * libplacebo 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 libplacebo. If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <locale.h>

#include "common.h"
#include "log.h"
#include "pl_thread.h"

struct priv {
    pl_mutex lock;
    enum pl_log_level log_level_cap;
    pl_str logbuffer;
};

pl_log pl_log_create(int api_ver, const struct pl_log_params *params)
{
    (void) api_ver;
    struct pl_log_t *log = pl_zalloc_obj(NULL, log, struct priv);
    struct priv *p = PL_PRIV(log);
    log->params = *PL_DEF(params, &pl_log_default_params);
    pl_mutex_init(&p->lock);
    pl_info(log, "Initialized libplacebo %s (API v%d)", PL_VERSION, PL_API_VER);
    return log;
}

const struct pl_log_params pl_log_default_params = {0};

void pl_log_destroy(pl_log *plog)
{
    pl_log log = *plog;
    if (!log)
        return;

    struct priv *p = PL_PRIV(log);
    pl_mutex_destroy(&p->lock);
    pl_free((void *) log);
    *plog = NULL;
}

struct pl_log_params pl_log_update(pl_log ptr, const struct pl_log_params *params)
{
    struct pl_log_t *log = (struct pl_log_t *) ptr;
    if (!log)
        return pl_log_default_params;

    struct priv *p = PL_PRIV(log);
    pl_mutex_lock(&p->lock);
    struct pl_log_params prev_params = log->params;
    log->params = *PL_DEF(params, &pl_log_default_params);
    pl_mutex_unlock(&p->lock);

    return prev_params;
}

enum pl_log_level pl_log_level_update(pl_log ptr, enum pl_log_level level)
{
    struct pl_log_t *log = (struct pl_log_t *) ptr;
    if (!log)
        return PL_LOG_NONE;

    struct priv *p = PL_PRIV(log);
    pl_mutex_lock(&p->lock);
    enum pl_log_level prev_level = log->params.log_level;
    log->params.log_level = level;
    pl_mutex_unlock(&p->lock);

    return prev_level;
}

void pl_log_level_cap(pl_log log, enum pl_log_level cap)
{
    if (!log)
        return;

    struct priv *p = PL_PRIV(log);
    pl_mutex_lock(&p->lock);
    p->log_level_cap = cap;
    pl_mutex_unlock(&p->lock);
}

static FILE *default_stream(void *stream, enum pl_log_level level)
{
    return PL_DEF(stream, level <= PL_LOG_WARN ? stderr : stdout);
}

void pl_log_simple(void *stream, enum pl_log_level level, const char *msg)
{
    static const char *prefix[] = {
        [PL_LOG_FATAL] = "fatal",
        [PL_LOG_ERR]   = "error",
        [PL_LOG_WARN]  = "warn",
        [PL_LOG_INFO]  = "info",
        [PL_LOG_DEBUG] = "debug",
        [PL_LOG_TRACE] = "trace",
    };

    FILE *h = default_stream(stream, level);
    fprintf(h, "%5s: %s\n", prefix[level], msg);
    if (level <= PL_LOG_WARN)
        fflush(h);
}

void pl_log_color(void *stream, enum pl_log_level level, const char *msg)
{
    static const char *color[] = {
        [PL_LOG_FATAL] = "31;1", // bright red
        [PL_LOG_ERR]   = "31",   // red
        [PL_LOG_WARN]  = "33",   // yellow/orange
        [PL_LOG_INFO]  = "32",   // green
        [PL_LOG_DEBUG] = "34",   // blue
        [PL_LOG_TRACE] = "30;1", // bright black
    };

    FILE *h = default_stream(stream, level);
    fprintf(h, "\033[%sm%s\033[0m\n", color[level], msg);
    if (level <= PL_LOG_WARN)
        fflush(h);
}

static void pl_msg_va(pl_log log, enum pl_log_level lev,
                      const char *fmt, va_list va)
{
    // Test log message without taking the lock, to avoid thrashing the
    // lock for thousands of trace messages unless those are actually
    // enabled. This may be a false negative, in which case log messages may
    // be lost as a result. But this shouldn't be a big deal, since any
    // situation leading to lost log messages would itself be a race condition.
    if (!pl_msg_test(log, lev))
        return;

    // Re-test the log message level with held lock to avoid false positives,
    // which would be a considerably bigger deal than false negatives
    struct priv *p = PL_PRIV(log);
    pl_mutex_lock(&p->lock);

    // Apply this cap before re-testing the log level, to avoid giving users
    // messages that should have been dropped by the log level.
    lev = PL_MAX(lev, p->log_level_cap);
    if (!pl_msg_test(log, lev))
        goto done;

    p->logbuffer.len = 0;
    pl_str_append_vasprintf((void *) log, &p->logbuffer, fmt, va);
    log->params.log_cb(log->params.log_priv, lev, (char *) p->logbuffer.buf);

done:
    pl_mutex_unlock(&p->lock);
}

void pl_msg(pl_log log, enum pl_log_level lev, const char *fmt, ...)
{
    va_list va;
    va_start(va, fmt);
    pl_msg_va(log, lev, fmt, va);
    va_end(va);
}

void pl_msg_source(pl_log log, enum pl_log_level lev, const char *src)
{
    if (!pl_msg_test(log, lev) || !src)
        return;

    int line = 1;
    while (*src) {
        const char *end = strchr(src, '\n');
        if (!end) {
            pl_msg(log, lev, "[%3d] %s", line, src);
            break;
        }

        pl_msg(log, lev, "[%3d] %.*s", line, (int)(end - src), src);
        src = end + 1;
        line++;
    }
}

#ifdef PL_HAVE_DBGHELP

#include <windows.h>
#include <dbghelp.h>
#include <shlwapi.h>

// https://github.com/llvm/llvm-project/blob/f03cd763384bbb67ddfa12957859ed58841d4b34/compiler-rt/lib/sanitizer_common/sanitizer_stacktrace.h#L85-L106
static inline uintptr_t get_prev_inst_pc(uintptr_t pc) {
#if defined(__arm__)
  // T32 (Thumb) branch instructions might be 16 or 32 bit long,
  // so we return (pc-2) in that case in order to be safe.
  // For A32 mode we return (pc-4) because all instructions are 32 bit long.
  return (pc - 3) & (~1);
#elif defined(__x86_64__) || defined(__i386__)
  return pc - 1;
#else
  return pc - 4;
#endif
}

static DWORD64 get_preferred_base(const char *module)
{
    DWORD64 image_base = 0;
    HANDLE file_mapping = NULL;
    HANDLE file_view = NULL;

    HANDLE file = CreateFile(module, GENERIC_READ, FILE_SHARE_READ, NULL,
                             OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    if (file == INVALID_HANDLE_VALUE)
        goto done;

    file_mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL);
    if (file_mapping == NULL)
        goto done;

    file_view = MapViewOfFile(file_mapping, FILE_MAP_READ, 0, 0, 0);
    if (file_view == NULL)
        goto done;

    PIMAGE_DOS_HEADER dos_header = (PIMAGE_DOS_HEADER) file_view;
    if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
        goto done;

    PIMAGE_NT_HEADERS pe_header = (PIMAGE_NT_HEADERS) ((char *) file_view +
                                                                dos_header->e_lfanew);
    if (pe_header->Signature != IMAGE_NT_SIGNATURE)
        goto done;

    if (pe_header->FileHeader.SizeOfOptionalHeader != sizeof(pe_header->OptionalHeader))
        goto done;

    image_base = pe_header->OptionalHeader.ImageBase;

done:
    if (file_view)
        UnmapViewOfFile(file_view);
    if (file_mapping)
        CloseHandle(file_mapping);
    if (file != INVALID_HANDLE_VALUE)
        CloseHandle(file);

    return image_base;
}

void pl_log_stack_trace(pl_log log, enum pl_log_level lev)
{
    if (!pl_msg_test(log, lev))
        return;

    void *tmp = pl_tmp(NULL);
    PL_ARRAY(void *) frames = {0};

    size_t capacity = 16;
    do {
        capacity *= 2;
        PL_ARRAY_RESIZE(tmp, frames, capacity);
        // Skip first frame, we don't care about this function
        frames.num = CaptureStackBackTrace(1, capacity, frames.elem, NULL);
    } while (capacity == frames.num);

    if (!frames.num) {
        pl_free(tmp);
        return;
    }

    // Load dbghelp on demand. While it is available on all Windows versions,
    // no need to keep it loaded all the time as stack trace printing function,
    // in theory should be used repetitively rarely.
    HANDLE process = GetCurrentProcess();
    HMODULE dbghelp = LoadLibrary("dbghelp.dll");
    DWORD options;
    SYMBOL_INFO *symbol = NULL;
    BOOL use_dbghelp = !!dbghelp;

#define DBGHELP_SYM(sym)                                                        \
    __typeof__(&sym) p##sym = (__typeof__(&sym))(void *) GetProcAddress(dbghelp, #sym); \
    use_dbghelp &= !!p##sym

    DBGHELP_SYM(SymCleanup);
    DBGHELP_SYM(SymFromAddr);
    DBGHELP_SYM(SymGetLineFromAddr64);
    DBGHELP_SYM(SymGetModuleInfo64);
    DBGHELP_SYM(SymGetOptions);
    DBGHELP_SYM(SymGetSearchPathW);
    DBGHELP_SYM(SymInitialize);
    DBGHELP_SYM(SymSetOptions);
    DBGHELP_SYM(SymSetSearchPathW);

#undef DBGHELP_SYM

    struct priv *p = PL_PRIV(log);
    PL_ARRAY(wchar_t) base_search = { .num = 1024 };

    if (use_dbghelp) {
        // DbgHelp is not thread-safe. Note that on Windows mutex is recursive,
        // so no need to unlock before calling pl_msg.
        pl_mutex_lock(&p->lock);

        options = pSymGetOptions();
        pSymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS |
                       SYMOPT_LOAD_LINES | SYMOPT_FAVOR_COMPRESSED);
        use_dbghelp &= pSymInitialize(process, NULL, TRUE);

        if (use_dbghelp) {
            symbol = pl_alloc(tmp, sizeof(SYMBOL_INFO) + 512);
            symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
            symbol->MaxNameLen = 512;

            PL_ARRAY_RESIZE(tmp, base_search, base_search.num);
            BOOL ret = pSymGetSearchPathW(process, base_search.elem,
                                          base_search.num);
            base_search.num = ret ? wcslen(base_search.elem) : 0;
            PL_ARRAY_APPEND(tmp, base_search, L'\0');
        } else {
            pSymSetOptions(options);
            pl_mutex_unlock(&p->lock);
        }
    }

    pl_msg(log, lev, "  Backtrace:");
    for (int n = 0; n < frames.num; n++) {
        uintptr_t pc = get_prev_inst_pc((uintptr_t) frames.elem[n]);
        pl_str out = {0};
        pl_str_append_asprintf(tmp, &out, "    #%-2d 0x%"PRIxPTR, n, pc);

        MEMORY_BASIC_INFORMATION meminfo = {0};
        char module_path[MAX_PATH] = {0};
        if (VirtualQuery((LPCVOID) pc, &meminfo, sizeof(meminfo))) {
            DWORD sz = GetModuleFileNameA(meminfo.AllocationBase, module_path,
                                          sizeof(module_path));
            if (sz == sizeof(module_path))
                pl_msg(log, PL_LOG_ERR, "module path truncated");

            if (use_dbghelp) {
                // According to documentation it should search in "The directory
                // that contains the corresponding module.", but it doesn't appear
                // to work, so manually set the path to module path.
                // https://learn.microsoft.com/windows/win32/debug/symbol-paths
                PL_ARRAY(wchar_t) mod_search = { .num = MAX_PATH };
                PL_ARRAY_RESIZE(tmp, mod_search, mod_search.num);

                sz = GetModuleFileNameW(meminfo.AllocationBase,
                                        mod_search.elem, mod_search.num);

                if (sz > 0 && sz != MAX_PATH &&
                    // TODO: Replace with PathCchRemoveFileSpec once mingw-w64
                    // >= 8.0.1 is commonly available, at the time of writing
                    // there are a few high profile Linux distributions that ship
                    // 8.0.0.
                    PathRemoveFileSpecW(mod_search.elem))
                {
                    mod_search.num = wcslen(mod_search.elem);
                    PL_ARRAY_APPEND(tmp, mod_search, L';');
                    PL_ARRAY_CONCAT(tmp, mod_search, base_search);
                    pSymSetSearchPathW(process, mod_search.elem);
                }
            }
        }

        DWORD64 sym_displacement;
        if (use_dbghelp && pSymFromAddr(process, pc, &sym_displacement, symbol))
            pl_str_append_asprintf(tmp, &out, " in %s+0x%llx",
                                   symbol->Name, sym_displacement);

        DWORD line_displacement;
        IMAGEHLP_LINE64 line = {sizeof(line)};
        if (use_dbghelp &&
            pSymGetLineFromAddr64(process, pc, &line_displacement, &line))
        {
            pl_str_append_asprintf(tmp, &out, " %s:%lu+0x%lx", line.FileName,
                                   line.LineNumber, line_displacement);
            goto done;
        }

        // LLVM tools by convention use absolute addresses with "prefered" base
        // image offset. We need to read this offset from binary, because due to
        // ASLR we are not loaded at this base. While Windows tools like WinDbg
        // expect relative offset to image base. So to be able to easily use it
        // with both worlds, print both values.
        DWORD64 module_base = get_preferred_base(module_path);
        pl_str_append_asprintf(tmp, &out, " (%s+0x%"PRIxPTR") (0x%llx)", module_path,
                               pc - (uintptr_t) meminfo.AllocationBase,
                               module_base + (pc - (uintptr_t) meminfo.AllocationBase));

done:
        pl_msg(log, lev, "%s", out.buf);
    }

    if (use_dbghelp) {
        pSymSetOptions(options);
        pSymCleanup(process);
        pl_mutex_unlock(&p->lock);
    }
    // Unload dbghelp. Maybe it is better to keep it loaded?
    if (dbghelp)
        FreeLibrary(dbghelp);
    pl_free(tmp);
}

#elif defined(PL_HAVE_UNWIND)
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#include <dlfcn.h>

void pl_log_stack_trace(pl_log log, enum pl_log_level lev)
{
    if (!pl_msg_test(log, lev))
        return;

    unw_cursor_t cursor;
    unw_context_t uc;
    unw_word_t ip, off;
    unw_getcontext(&uc);
    unw_init_local(&cursor, &uc);

    int depth = 0;
    pl_msg(log, lev, "  Backtrace:");
    while (unw_step(&cursor) > 0) {
        char symbol[256] = "<unknown>";
        Dl_info info = {
            .dli_fname = "<unknown>",
        };

        unw_get_reg(&cursor, UNW_REG_IP, &ip);
        unw_get_proc_name(&cursor, symbol, sizeof(symbol), &off);
        dladdr((void *) (uintptr_t) ip, &info);
        pl_msg(log, lev, "    #%-2d 0x%016" PRIxPTR " in %s+0x%" PRIxPTR" at %s+0x%" PRIxPTR,
               depth++, ip, symbol, off, info.dli_fname, ip - (uintptr_t) info.dli_fbase);
    }
}

#elif defined(PL_HAVE_EXECINFO)
#include <execinfo.h>

void pl_log_stack_trace(pl_log log, enum pl_log_level lev)
{
    if (!pl_msg_test(log, lev))
        return;

    PL_ARRAY(void *) buf = {0};
    size_t buf_avail = 16;
    do {
        buf_avail *= 2;
        PL_ARRAY_RESIZE(NULL, buf, buf_avail);
        buf.num = backtrace(buf.elem, buf_avail);
    } while (buf.num == buf_avail);

    pl_msg(log, lev, "  Backtrace:");
    char **strings = backtrace_symbols(buf.elem, buf.num);
    for (int i = 1; i < buf.num; i++)
        pl_msg(log, lev, "    #%-2d %s", i - 1, strings[i]);

    free(strings);
    pl_free(buf.elem);
}

#else
void pl_log_stack_trace(pl_log log, enum pl_log_level lev) { }
#endif