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