Home | History | Annotate | Download | only in qemu
      1 #ifndef QEMU_LOG_H
      2 #define QEMU_LOG_H
      3 
      4 #include <stdarg.h>
      5 #include <stdbool.h>
      6 #include <stdio.h>
      7 #include "qemu/compiler.h"
      8 #ifdef NEED_CPU_H
      9 // TODO(digit): #include "qom/cpu.h"
     10 #include "disas/disas.h"
     11 #endif
     12 
     13 /* Private global variables, don't use */
     14 extern FILE *qemu_logfile;
     15 extern int qemu_loglevel;
     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  */
     26 static inline bool qemu_log_enabled(void)
     27 {
     28     return qemu_logfile != NULL;
     29 }
     30 
     31 #define CPU_LOG_TB_OUT_ASM (1 << 0)
     32 #define CPU_LOG_TB_IN_ASM  (1 << 1)
     33 #define CPU_LOG_TB_OP      (1 << 2)
     34 #define CPU_LOG_TB_OP_OPT  (1 << 3)
     35 #define CPU_LOG_INT        (1 << 4)
     36 #define CPU_LOG_EXEC       (1 << 5)
     37 #define CPU_LOG_PCALL      (1 << 6)
     38 #define CPU_LOG_IOPORT     (1 << 7)
     39 #define CPU_LOG_TB_CPU     (1 << 8)
     40 #define CPU_LOG_RESET      (1 << 9)
     41 #define LOG_UNIMP          (1 << 10)
     42 #define LOG_GUEST_ERROR    (1 << 11)
     43 
     44 /* Returns true if a bit is set in the current loglevel mask
     45  */
     46 static inline bool qemu_loglevel_mask(int mask)
     47 {
     48     return (qemu_loglevel & mask) != 0;
     49 }
     50 
     51 /* Logging functions: */
     52 
     53 /* main logging function
     54  */
     55 void GCC_FMT_ATTR(1, 2) qemu_log(const char *fmt, ...);
     56 
     57 /* vfprintf-like logging function
     58  */
     59 static inline void GCC_FMT_ATTR(1, 0)
     60 qemu_log_vprintf(const char *fmt, va_list va)
     61 {
     62     if (qemu_logfile) {
     63         vfprintf(qemu_logfile, fmt, va);
     64     }
     65 }
     66 
     67 /* log only if a bit is set on the current loglevel mask
     68  */
     69 void GCC_FMT_ATTR(2, 3) qemu_log_mask(int mask, const char *fmt, ...);
     70 
     71 
     72 /* Special cases: */
     73 
     74 #ifdef NEED_CPU_H
     75 
     76 /* cpu_dump_state() logging functions: */
     77 /**
     78  * log_cpu_state:
     79  * @cpu: The CPU whose state is to be logged.
     80  * @flags: Flags what to log.
     81  *
     82  * Logs the output of cpu_dump_state().
     83  */
     84 static inline void log_cpu_state(CPUState *cpu, int flags)
     85 {
     86     if (qemu_log_enabled()) {
     87         cpu_dump_state(cpu, qemu_logfile, fprintf, flags);
     88     }
     89 }
     90 /**
     91  * log_cpu_state_mask:
     92  * @mask: Mask when to log.
     93  * @cpu: The CPU whose state is to be logged.
     94  * @flags: Flags what to log.
     95  *
     96  * Logs the output of cpu_dump_state() if loglevel includes @mask.
     97  */
     98 static inline void log_cpu_state_mask(int mask, CPUState *cpu, int flags)
     99 {
    100     if (qemu_loglevel & mask) {
    101         log_cpu_state(cpu, flags);
    102     }
    103 }
    104 
    105 /* disas() and target_disas() to qemu_logfile: */
    106 void target_disas(FILE*, CPUArchState*, target_ulong, target_ulong, int);
    107 
    108 static inline void log_target_disas(CPUArchState *env, target_ulong start,
    109                                     target_ulong len, int flags)
    110 {
    111     target_disas(qemu_logfile, env, start, len, flags);
    112 }
    113 
    114 void disas(FILE*, void*, unsigned long);
    115 static inline void log_disas(void *code, unsigned long size)
    116 {
    117     disas(qemu_logfile, code, size);
    118 }
    119 
    120 #if defined(CONFIG_USER_ONLY)
    121 /* page_dump() output to the log file: */
    122 static inline void log_page_dump(void)
    123 {
    124     page_dump(qemu_logfile);
    125 }
    126 #endif
    127 #endif  // NEED_CPU_H
    128 
    129 
    130 /* Maintenance: */
    131 
    132 /* fflush() the log file */
    133 static inline void qemu_log_flush(void)
    134 {
    135     fflush(qemu_logfile);
    136 }
    137 
    138 /* Close the log file */
    139 static inline void qemu_log_close(void)
    140 {
    141     if (qemu_logfile) {
    142         if (qemu_logfile != stderr) {
    143             fclose(qemu_logfile);
    144         }
    145         qemu_logfile = NULL;
    146     }
    147 }
    148 
    149 /* Set up a new log file */
    150 static inline void qemu_log_set_file(FILE *f)
    151 {
    152     qemu_logfile = f;
    153 }
    154 
    155 /* define log items */
    156 typedef struct QEMULogItem {
    157     int mask;
    158     const char *name;
    159     const char *help;
    160 } QEMULogItem;
    161 
    162 extern const QEMULogItem qemu_log_items[];
    163 
    164 /* This is the function that actually does the work of
    165  * changing the log level; it should only be accessed via
    166  * the qemu_set_log() wrapper.
    167  */
    168 void do_qemu_set_log(int log_flags, bool use_own_buffers);
    169 
    170 static inline void qemu_set_log(int log_flags)
    171 {
    172 #ifdef CONFIG_USER_ONLY
    173     do_qemu_set_log(log_flags, true);
    174 #else
    175     do_qemu_set_log(log_flags, false);
    176 #endif
    177 }
    178 
    179 void qemu_set_log_filename(const char *filename);
    180 int qemu_str_to_log_mask(const char *str);
    181 
    182 /* Print a usage message listing all the valid logging categories
    183  * to the specified FILE*.
    184  */
    185 void qemu_print_log_usage(FILE *f);
    186 
    187 #endif
    188