1 /* 2 * logging unit-tests 3 * 4 * Copyright (C) 2016 Linaro Ltd. 5 * 6 * Author: Alex Bennée <alex.bennee@linaro.org> 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a copy 9 * of this software and associated documentation files (the "Software"), to deal 10 * in the Software without restriction, including without limitation the rights 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 * copies of the Software, and to permit persons to whom the Software is 13 * furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included in 16 * all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 * THE SOFTWARE. 25 */ 26 27 #include "qemu/osdep.h" 28 #include <glib/gstdio.h> 29 30 #include "qapi/error.h" 31 #include "qemu/log.h" 32 33 static void test_parse_range(void) 34 { 35 Error *err = NULL; 36 37 qemu_set_dfilter_ranges("0x1000+0x100", &error_abort); 38 39 g_assert_false(qemu_log_in_addr_range(0xfff)); 40 g_assert(qemu_log_in_addr_range(0x1000)); 41 g_assert(qemu_log_in_addr_range(0x1001)); 42 g_assert(qemu_log_in_addr_range(0x10ff)); 43 g_assert_false(qemu_log_in_addr_range(0x1100)); 44 45 qemu_set_dfilter_ranges("0x1000-0x100", &error_abort); 46 47 g_assert_false(qemu_log_in_addr_range(0x1001)); 48 g_assert(qemu_log_in_addr_range(0x1000)); 49 g_assert(qemu_log_in_addr_range(0x0f01)); 50 g_assert_false(qemu_log_in_addr_range(0x0f00)); 51 52 qemu_set_dfilter_ranges("0x1000..0x1100", &error_abort); 53 54 g_assert_false(qemu_log_in_addr_range(0xfff)); 55 g_assert(qemu_log_in_addr_range(0x1000)); 56 g_assert(qemu_log_in_addr_range(0x1100)); 57 g_assert_false(qemu_log_in_addr_range(0x1101)); 58 59 qemu_set_dfilter_ranges("0x1000..0x1000", &error_abort); 60 61 g_assert_false(qemu_log_in_addr_range(0xfff)); 62 g_assert(qemu_log_in_addr_range(0x1000)); 63 g_assert_false(qemu_log_in_addr_range(0x1001)); 64 65 qemu_set_dfilter_ranges("0x1000+0x100,0x2100-0x100,0x3000..0x3100", 66 &error_abort); 67 g_assert(qemu_log_in_addr_range(0x1050)); 68 g_assert(qemu_log_in_addr_range(0x2050)); 69 g_assert(qemu_log_in_addr_range(0x3050)); 70 71 qemu_set_dfilter_ranges("0xffffffffffffffff-1", &error_abort); 72 g_assert(qemu_log_in_addr_range(UINT64_MAX)); 73 g_assert_false(qemu_log_in_addr_range(UINT64_MAX - 1)); 74 75 qemu_set_dfilter_ranges("0..0xffffffffffffffff", &error_abort); 76 g_assert(qemu_log_in_addr_range(0)); 77 g_assert(qemu_log_in_addr_range(UINT64_MAX)); 78 79 qemu_set_dfilter_ranges("2..1", &err); 80 error_free_or_abort(&err); 81 82 qemu_set_dfilter_ranges("0x1000+onehundred", &err); 83 error_free_or_abort(&err); 84 85 qemu_set_dfilter_ranges("0x1000+0", &err); 86 error_free_or_abort(&err); 87 } 88 89 static void set_log_path_tmp(char const *dir, char const *tpl, Error **errp) 90 { 91 gchar *file_path = g_build_filename(dir, tpl, NULL); 92 93 qemu_set_log_filename(file_path, errp); 94 g_free(file_path); 95 } 96 97 static void test_parse_path(gconstpointer data) 98 { 99 gchar const *tmp_path = data; 100 Error *err = NULL; 101 102 set_log_path_tmp(tmp_path, "qemu.log", &error_abort); 103 set_log_path_tmp(tmp_path, "qemu-%d.log", &error_abort); 104 set_log_path_tmp(tmp_path, "qemu.log.%d", &error_abort); 105 106 set_log_path_tmp(tmp_path, "qemu-%d%d.log", &err); 107 error_free_or_abort(&err); 108 } 109 110 static void test_logfile_write(gconstpointer data) 111 { 112 QemuLogFile *logfile; 113 QemuLogFile *logfile2; 114 gchar const *dir = data; 115 g_autofree gchar *file_path = NULL; 116 g_autofree gchar *file_path1 = NULL; 117 FILE *orig_fd; 118 119 /* 120 * Before starting test, set log flags, to ensure the file gets 121 * opened below with the call to qemu_set_log_filename(). 122 * In cases where a logging backend other than log is used, 123 * this is needed. 124 */ 125 qemu_set_log(CPU_LOG_TB_OUT_ASM, &error_abort); 126 file_path = g_build_filename(dir, "qemu_test_log_write0.log", NULL); 127 file_path1 = g_build_filename(dir, "qemu_test_log_write1.log", NULL); 128 129 /* 130 * Test that even if an open file handle is changed, 131 * our handle remains valid due to RCU. 132 */ 133 qemu_set_log_filename(file_path, &error_abort); 134 rcu_read_lock(); 135 logfile = qatomic_rcu_read(&qemu_logfile); 136 orig_fd = logfile->fd; 137 g_assert(logfile && logfile->fd); 138 fprintf(logfile->fd, "%s 1st write to file\n", __func__); 139 fflush(logfile->fd); 140 141 /* Change the logfile and ensure that the handle is still valid. */ 142 qemu_set_log_filename(file_path1, &error_abort); 143 logfile2 = qatomic_rcu_read(&qemu_logfile); 144 g_assert(logfile->fd == orig_fd); 145 g_assert(logfile2->fd != logfile->fd); 146 fprintf(logfile->fd, "%s 2nd write to file\n", __func__); 147 fflush(logfile->fd); 148 rcu_read_unlock(); 149 } 150 151 static void test_logfile_lock(gconstpointer data) 152 { 153 FILE *logfile; 154 gchar const *dir = data; 155 g_autofree gchar *file_path = NULL; 156 157 file_path = g_build_filename(dir, "qemu_test_logfile_lock0.log", NULL); 158 159 /* 160 * Test the use of the logfile lock, such 161 * that even if an open file handle is closed, 162 * our handle remains valid for use due to RCU. 163 */ 164 qemu_set_log_filename(file_path, &error_abort); 165 logfile = qemu_log_trylock(); 166 g_assert(logfile); 167 fprintf(logfile, "%s 1st write to file\n", __func__); 168 fflush(logfile); 169 170 /* 171 * Initiate a close file and make sure our handle remains 172 * valid since we still have the logfile lock. 173 */ 174 qemu_log_close(); 175 fprintf(logfile, "%s 2nd write to file\n", __func__); 176 fflush(logfile); 177 qemu_log_unlock(logfile); 178 } 179 180 /* Remove a directory and all its entries (non-recursive). */ 181 static void rmdir_full(gchar const *root) 182 { 183 GDir *root_gdir = g_dir_open(root, 0, NULL); 184 gchar const *entry_name; 185 186 g_assert_nonnull(root_gdir); 187 while ((entry_name = g_dir_read_name(root_gdir)) != NULL) { 188 gchar *entry_path = g_build_filename(root, entry_name, NULL); 189 g_assert(g_remove(entry_path) == 0); 190 g_free(entry_path); 191 } 192 g_dir_close(root_gdir); 193 g_assert(g_rmdir(root) == 0); 194 } 195 196 int main(int argc, char **argv) 197 { 198 g_autofree gchar *tmp_path = g_dir_make_tmp("qemu-test-logging.XXXXXX", NULL); 199 int rc; 200 201 g_test_init(&argc, &argv, NULL); 202 g_assert_nonnull(tmp_path); 203 204 g_test_add_func("/logging/parse_range", test_parse_range); 205 g_test_add_data_func("/logging/parse_path", tmp_path, test_parse_path); 206 g_test_add_data_func("/logging/logfile_write_path", 207 tmp_path, test_logfile_write); 208 g_test_add_data_func("/logging/logfile_lock_path", 209 tmp_path, test_logfile_lock); 210 211 rc = g_test_run(); 212 qemu_log_close(); 213 drain_call_rcu(); 214 215 rmdir_full(tmp_path); 216 return rc; 217 } 218