qemu/include/qemu/log.h
<<
>>
Prefs
   1#ifndef QEMU_LOG_H
   2#define QEMU_LOG_H
   3
   4/* A small part of this API is split into its own header */
   5#include "qemu/log-for-trace.h"
   6#include "qemu/rcu.h"
   7
   8typedef struct QemuLogFile {
   9    struct rcu_head rcu;
  10    FILE *fd;
  11} QemuLogFile;
  12
  13/* Private global variable, don't use */
  14extern QemuLogFile *qemu_logfile;
  15
  16
  17/* 
  18 * The new API:
  19 *
  20 */
  21
  22/* Log settings checking macros: */
  23
  24/* Returns true if qemu_log() will really write somewhere
  25 */
  26static inline bool qemu_log_enabled(void)
  27{
  28    return qemu_logfile != NULL;
  29}
  30
  31/* Returns true if qemu_log() will write somewhere else than stderr
  32 */
  33static inline bool qemu_log_separate(void)
  34{
  35    QemuLogFile *logfile;
  36    bool res = false;
  37
  38    rcu_read_lock();
  39    logfile = atomic_rcu_read(&qemu_logfile);
  40    if (logfile && logfile->fd != stderr) {
  41        res = true;
  42    }
  43    rcu_read_unlock();
  44    return res;
  45}
  46
  47#define CPU_LOG_TB_OUT_ASM (1 << 0)
  48#define CPU_LOG_TB_IN_ASM  (1 << 1)
  49#define CPU_LOG_TB_OP      (1 << 2)
  50#define CPU_LOG_TB_OP_OPT  (1 << 3)
  51#define CPU_LOG_INT        (1 << 4)
  52#define CPU_LOG_EXEC       (1 << 5)
  53#define CPU_LOG_PCALL      (1 << 6)
  54#define CPU_LOG_TB_CPU     (1 << 8)
  55#define CPU_LOG_RESET      (1 << 9)
  56#define LOG_UNIMP          (1 << 10)
  57#define LOG_GUEST_ERROR    (1 << 11)
  58#define CPU_LOG_MMU        (1 << 12)
  59#define CPU_LOG_TB_NOCHAIN (1 << 13)
  60#define CPU_LOG_PAGE       (1 << 14)
  61/* LOG_TRACE (1 << 15) is defined in log-for-trace.h */
  62#define CPU_LOG_TB_OP_IND  (1 << 16)
  63#define CPU_LOG_TB_FPU     (1 << 17)
  64#define CPU_LOG_PLUGIN     (1 << 18)
  65/* LOG_STRACE is used for user-mode strace logging. */
  66#define LOG_STRACE         (1 << 19)
  67
  68/* device entries */
  69#define DEV_LOG_NET_DEV    (1 << 24)
  70#define DEV_LOG_NAND       (1 << 25)
  71#define DEV_LOG_NANDC      (1 << 26)
  72#define DEV_LOG_SPI        (1 << 27)
  73#define DEV_LOG_SPI_DEV    (1 << 28)
  74#define LOG_FDT            (1 << 29)
  75#define LOG_PM             (1 << 30)
  76
  77/* Lock output for a series of related logs.  Since this is not needed
  78 * for a single qemu_log / qemu_log_mask / qemu_log_mask_and_addr, we
  79 * assume that qemu_loglevel_mask has already been tested, and that
  80 * qemu_loglevel is never set when qemu_logfile is unset.
  81 */
  82
  83static inline FILE *qemu_log_lock(void)
  84{
  85    QemuLogFile *logfile;
  86    rcu_read_lock();
  87    logfile = atomic_rcu_read(&qemu_logfile);
  88    if (logfile) {
  89        qemu_flockfile(logfile->fd);
  90        return logfile->fd;
  91    } else {
  92        return NULL;
  93    }
  94}
  95
  96static inline void qemu_log_unlock(FILE *fd)
  97{
  98    if (fd) {
  99        qemu_funlockfile(fd);
 100    }
 101    rcu_read_unlock();
 102}
 103
 104/* Logging functions: */
 105
 106/* vfprintf-like logging function
 107 */
 108static inline void GCC_FMT_ATTR(1, 0)
 109qemu_log_vprintf(const char *fmt, va_list va)
 110{
 111    QemuLogFile *logfile;
 112
 113    rcu_read_lock();
 114    logfile = atomic_rcu_read(&qemu_logfile);
 115    if (logfile) {
 116        vfprintf(logfile->fd, fmt, va);
 117    }
 118    rcu_read_unlock();
 119}
 120
 121/* log only if a bit is set on the current loglevel mask:
 122 * @mask: bit to check in the mask
 123 * @fmt: printf-style format string
 124 * @args: optional arguments for format string
 125 */
 126#define qemu_log_mask(MASK, FMT, ...)                   \
 127    do {                                                \
 128        if (unlikely(qemu_loglevel_mask(MASK))) {       \
 129            qemu_log(FMT, ## __VA_ARGS__);              \
 130        }                                               \
 131    } while (0)
 132
 133/* log only if a bit is set on the current loglevel mask
 134 * and we are in the address range we care about:
 135 * @mask: bit to check in the mask
 136 * @addr: address to check in dfilter
 137 * @fmt: printf-style format string
 138 * @args: optional arguments for format string
 139 */
 140#define qemu_log_mask_and_addr(MASK, ADDR, FMT, ...)    \
 141    do {                                                \
 142        if (unlikely(qemu_loglevel_mask(MASK)) &&       \
 143                     qemu_log_in_addr_range(ADDR)) {    \
 144            qemu_log(FMT, ## __VA_ARGS__);              \
 145        }                                               \
 146    } while (0)
 147
 148/* Maintenance: */
 149
 150/* define log items */
 151typedef struct QEMULogItem {
 152    int mask;
 153    const char *name;
 154    const char *help;
 155} QEMULogItem;
 156
 157extern const QEMULogItem qemu_log_items[];
 158
 159void qemu_set_log(int log_flags);
 160void qemu_log_needs_buffers(void);
 161void qemu_set_log_filename(const char *filename, Error **errp);
 162void qemu_set_dfilter_ranges(const char *ranges, Error **errp);
 163bool qemu_log_in_addr_range(uint64_t addr);
 164int qemu_str_to_log_mask(const char *str);
 165
 166/* Print a usage message listing all the valid logging categories
 167 * to the specified FILE*.
 168 */
 169void qemu_print_log_usage(FILE *f);
 170
 171/* fflush() the log file */
 172void qemu_log_flush(void);
 173/* Close the log file */
 174void qemu_log_close(void);
 175
 176#endif
 177