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#include "qemu/osdep.h"
28#include <glib/gstdio.h>
29
30#include "qapi/error.h"
31#include "qemu/log.h"
32#include "qemu/rcu.h"
33
34static void test_parse_range(void)
35{
36 Error *err = NULL;
37
38 qemu_set_dfilter_ranges("0x1000+0x100", &error_abort);
39
40 g_assert_false(qemu_log_in_addr_range(0xfff));
41 g_assert(qemu_log_in_addr_range(0x1000));
42 g_assert(qemu_log_in_addr_range(0x1001));
43 g_assert(qemu_log_in_addr_range(0x10ff));
44 g_assert_false(qemu_log_in_addr_range(0x1100));
45
46 qemu_set_dfilter_ranges("0x1000-0x100", &error_abort);
47
48 g_assert_false(qemu_log_in_addr_range(0x1001));
49 g_assert(qemu_log_in_addr_range(0x1000));
50 g_assert(qemu_log_in_addr_range(0x0f01));
51 g_assert_false(qemu_log_in_addr_range(0x0f00));
52
53 qemu_set_dfilter_ranges("0x1000..0x1100", &error_abort);
54
55 g_assert_false(qemu_log_in_addr_range(0xfff));
56 g_assert(qemu_log_in_addr_range(0x1000));
57 g_assert(qemu_log_in_addr_range(0x1100));
58 g_assert_false(qemu_log_in_addr_range(0x1101));
59
60 qemu_set_dfilter_ranges("0x1000..0x1000", &error_abort);
61
62 g_assert_false(qemu_log_in_addr_range(0xfff));
63 g_assert(qemu_log_in_addr_range(0x1000));
64 g_assert_false(qemu_log_in_addr_range(0x1001));
65
66 qemu_set_dfilter_ranges("0x1000+0x100,0x2100-0x100,0x3000..0x3100",
67 &error_abort);
68 g_assert(qemu_log_in_addr_range(0x1050));
69 g_assert(qemu_log_in_addr_range(0x2050));
70 g_assert(qemu_log_in_addr_range(0x3050));
71
72 qemu_set_dfilter_ranges("0xffffffffffffffff-1", &error_abort);
73 g_assert(qemu_log_in_addr_range(UINT64_MAX));
74 g_assert_false(qemu_log_in_addr_range(UINT64_MAX - 1));
75
76 qemu_set_dfilter_ranges("0..0xffffffffffffffff", &error_abort);
77 g_assert(qemu_log_in_addr_range(0));
78 g_assert(qemu_log_in_addr_range(UINT64_MAX));
79
80 qemu_set_dfilter_ranges("2..1", &err);
81 error_free_or_abort(&err);
82
83 qemu_set_dfilter_ranges("0x1000+onehundred", &err);
84 error_free_or_abort(&err);
85
86 qemu_set_dfilter_ranges("0x1000+0", &err);
87 error_free_or_abort(&err);
88}
89
90static void set_log_path_tmp(char const *dir, char const *tpl, Error **errp)
91{
92 gchar *file_path = g_build_filename(dir, tpl, NULL);
93
94 qemu_set_log_filename(file_path, errp);
95 g_free(file_path);
96}
97
98static void test_parse_path(gconstpointer data)
99{
100 gchar const *tmp_path = data;
101 Error *err = NULL;
102
103 set_log_path_tmp(tmp_path, "qemu.log", &error_abort);
104 set_log_path_tmp(tmp_path, "qemu-%d.log", &error_abort);
105 set_log_path_tmp(tmp_path, "qemu.log.%d", &error_abort);
106
107 set_log_path_tmp(tmp_path, "qemu-%d%d.log", &err);
108 error_free_or_abort(&err);
109}
110
111static void test_logfile_write(gconstpointer data)
112{
113 FILE *logfile0, *logfile1;
114 gchar const *dir = data;
115 g_autofree gchar *file_path0 = NULL;
116 g_autofree gchar *file_path1 = NULL;
117
118
119
120
121
122
123
124 qemu_set_log(CPU_LOG_TB_OUT_ASM, &error_abort);
125 file_path0 = g_build_filename(dir, "qemu_test_log_write0.log", NULL);
126 file_path1 = g_build_filename(dir, "qemu_test_log_write1.log", NULL);
127
128
129
130
131
132 qemu_set_log_filename(file_path0, &error_abort);
133 logfile0 = qemu_log_trylock();
134 g_assert(logfile0);
135 fprintf(logfile0, "%s 1st write to file\n", __func__);
136 fflush(logfile0);
137
138
139 qemu_set_log_filename(file_path1, &error_abort);
140 logfile1 = qemu_log_trylock();
141 g_assert(logfile1);
142 g_assert(logfile0 != logfile1);
143 fprintf(logfile0, "%s 2nd write to file\n", __func__);
144 fflush(logfile0);
145 qemu_log_unlock(logfile0);
146 qemu_log_unlock(logfile1);
147}
148
149static void test_logfile_lock(gconstpointer data)
150{
151 FILE *logfile;
152 gchar const *dir = data;
153 g_autofree gchar *file_path = NULL;
154
155 file_path = g_build_filename(dir, "qemu_test_logfile_lock0.log", NULL);
156
157
158
159
160
161
162 qemu_set_log_filename(file_path, &error_abort);
163 logfile = qemu_log_trylock();
164 g_assert(logfile);
165 fprintf(logfile, "%s 1st write to file\n", __func__);
166 fflush(logfile);
167
168
169
170
171
172 qemu_set_log_filename_flags(NULL, 0, &error_abort);
173 fprintf(logfile, "%s 2nd write to file\n", __func__);
174 fflush(logfile);
175 qemu_log_unlock(logfile);
176}
177
178
179static void rmdir_full(gchar const *root)
180{
181 GDir *root_gdir = g_dir_open(root, 0, NULL);
182 gchar const *entry_name;
183
184 g_assert_nonnull(root_gdir);
185 while ((entry_name = g_dir_read_name(root_gdir)) != NULL) {
186 gchar *entry_path = g_build_filename(root, entry_name, NULL);
187 g_assert(g_remove(entry_path) == 0);
188 g_free(entry_path);
189 }
190 g_dir_close(root_gdir);
191 g_assert(g_rmdir(root) == 0);
192}
193
194int main(int argc, char **argv)
195{
196 g_autofree gchar *tmp_path = g_dir_make_tmp("qemu-test-logging.XXXXXX", NULL);
197 int rc;
198
199 g_test_init(&argc, &argv, NULL);
200 g_assert_nonnull(tmp_path);
201
202 g_test_add_func("/logging/parse_range", test_parse_range);
203 g_test_add_data_func("/logging/parse_path", tmp_path, test_parse_path);
204 g_test_add_data_func("/logging/logfile_write_path",
205 tmp_path, test_logfile_write);
206 g_test_add_data_func("/logging/logfile_lock_path",
207 tmp_path, test_logfile_lock);
208
209 rc = g_test_run();
210 qemu_set_log_filename_flags(NULL, 0, &error_abort);
211 drain_call_rcu();
212
213 rmdir_full(tmp_path);
214 return rc;
215}
216