2016-03-15 22:30:20 +08:00
|
|
|
/*
|
|
|
|
* logging unit-tests
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Linaro Ltd.
|
|
|
|
*
|
|
|
|
* Author: Alex Bennée <alex.bennee@linaro.org>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2016-08-19 02:46:03 +08:00
|
|
|
#include <glib/gstdio.h>
|
2016-03-15 22:30:20 +08:00
|
|
|
|
|
|
|
#include "qemu-common.h"
|
2016-06-16 01:27:15 +08:00
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "qemu/log.h"
|
2016-03-15 22:30:20 +08:00
|
|
|
|
|
|
|
static void test_parse_range(void)
|
|
|
|
{
|
2016-06-16 01:27:15 +08:00
|
|
|
Error *err = NULL;
|
|
|
|
|
|
|
|
qemu_set_dfilter_ranges("0x1000+0x100", &error_abort);
|
2016-03-15 22:30:20 +08:00
|
|
|
|
|
|
|
g_assert_false(qemu_log_in_addr_range(0xfff));
|
|
|
|
g_assert(qemu_log_in_addr_range(0x1000));
|
|
|
|
g_assert(qemu_log_in_addr_range(0x1001));
|
|
|
|
g_assert(qemu_log_in_addr_range(0x10ff));
|
|
|
|
g_assert_false(qemu_log_in_addr_range(0x1100));
|
|
|
|
|
2016-06-16 01:27:15 +08:00
|
|
|
qemu_set_dfilter_ranges("0x1000-0x100", &error_abort);
|
2016-03-15 22:30:20 +08:00
|
|
|
|
|
|
|
g_assert_false(qemu_log_in_addr_range(0x1001));
|
|
|
|
g_assert(qemu_log_in_addr_range(0x1000));
|
|
|
|
g_assert(qemu_log_in_addr_range(0x0f01));
|
|
|
|
g_assert_false(qemu_log_in_addr_range(0x0f00));
|
|
|
|
|
2016-06-16 01:27:15 +08:00
|
|
|
qemu_set_dfilter_ranges("0x1000..0x1100", &error_abort);
|
2016-03-15 22:30:20 +08:00
|
|
|
|
|
|
|
g_assert_false(qemu_log_in_addr_range(0xfff));
|
|
|
|
g_assert(qemu_log_in_addr_range(0x1000));
|
|
|
|
g_assert(qemu_log_in_addr_range(0x1100));
|
|
|
|
g_assert_false(qemu_log_in_addr_range(0x1101));
|
|
|
|
|
2016-06-16 01:27:15 +08:00
|
|
|
qemu_set_dfilter_ranges("0x1000..0x1000", &error_abort);
|
2016-03-15 22:30:20 +08:00
|
|
|
|
|
|
|
g_assert_false(qemu_log_in_addr_range(0xfff));
|
|
|
|
g_assert(qemu_log_in_addr_range(0x1000));
|
|
|
|
g_assert_false(qemu_log_in_addr_range(0x1001));
|
|
|
|
|
2016-06-16 01:27:15 +08:00
|
|
|
qemu_set_dfilter_ranges("0x1000+0x100,0x2100-0x100,0x3000..0x3100",
|
|
|
|
&error_abort);
|
2016-03-15 22:30:20 +08:00
|
|
|
g_assert(qemu_log_in_addr_range(0x1050));
|
|
|
|
g_assert(qemu_log_in_addr_range(0x2050));
|
|
|
|
g_assert(qemu_log_in_addr_range(0x3050));
|
|
|
|
|
log: Clean up misuse of Range for -dfilter
Range encodes an integer interval [a,b] as { begin = a, end = b + 1 },
where a \in [0,2^64-1] and b \in [1,2^64]. Thus, zero end is to be
interpreted as 2^64.
The implementation of -dfilter (commit 3514552) uses Range
differently: it encodes [a,b] as { begin = a, end = b }. The code
works, but it contradicts the specification of Range in range.h.
Switch to the specified representation. Since it can't represent
[0,UINT64_MAX], we have to reject that now. Add a test for it.
While we're rejecting anyway: observe that we reject -dfilter LOB..UPB
where LOB > UPB when UPB is zero, but happily create an empty Range
when it isn't. Reject it then, too, and add a test for it.
While there, add a positive test for the problematic upper bound
UINT64_MAX.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
2016-07-01 19:47:46 +08:00
|
|
|
qemu_set_dfilter_ranges("0xffffffffffffffff-1", &error_abort);
|
|
|
|
g_assert(qemu_log_in_addr_range(UINT64_MAX));
|
|
|
|
g_assert_false(qemu_log_in_addr_range(UINT64_MAX - 1));
|
|
|
|
|
2020-04-22 21:07:17 +08:00
|
|
|
qemu_set_dfilter_ranges("0..0xffffffffffffffff", &error_abort);
|
2016-07-01 19:47:49 +08:00
|
|
|
g_assert(qemu_log_in_addr_range(0));
|
|
|
|
g_assert(qemu_log_in_addr_range(UINT64_MAX));
|
2020-04-22 21:07:17 +08:00
|
|
|
|
log: Clean up misuse of Range for -dfilter
Range encodes an integer interval [a,b] as { begin = a, end = b + 1 },
where a \in [0,2^64-1] and b \in [1,2^64]. Thus, zero end is to be
interpreted as 2^64.
The implementation of -dfilter (commit 3514552) uses Range
differently: it encodes [a,b] as { begin = a, end = b }. The code
works, but it contradicts the specification of Range in range.h.
Switch to the specified representation. Since it can't represent
[0,UINT64_MAX], we have to reject that now. Add a test for it.
While we're rejecting anyway: observe that we reject -dfilter LOB..UPB
where LOB > UPB when UPB is zero, but happily create an empty Range
when it isn't. Reject it then, too, and add a test for it.
While there, add a positive test for the problematic upper bound
UINT64_MAX.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
2016-07-01 19:47:46 +08:00
|
|
|
qemu_set_dfilter_ranges("2..1", &err);
|
|
|
|
error_free_or_abort(&err);
|
|
|
|
|
2016-06-16 01:27:15 +08:00
|
|
|
qemu_set_dfilter_ranges("0x1000+onehundred", &err);
|
|
|
|
error_free_or_abort(&err);
|
|
|
|
|
|
|
|
qemu_set_dfilter_ranges("0x1000+0", &err);
|
|
|
|
error_free_or_abort(&err);
|
2016-03-15 22:30:20 +08:00
|
|
|
}
|
2016-03-15 22:30:23 +08:00
|
|
|
|
2016-08-19 02:46:03 +08:00
|
|
|
static void set_log_path_tmp(char const *dir, char const *tpl, Error **errp)
|
2016-03-15 22:30:23 +08:00
|
|
|
{
|
2016-08-19 02:46:03 +08:00
|
|
|
gchar *file_path = g_build_filename(dir, tpl, NULL);
|
|
|
|
|
|
|
|
qemu_set_log_filename(file_path, errp);
|
|
|
|
g_free(file_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_parse_path(gconstpointer data)
|
|
|
|
{
|
|
|
|
gchar const *tmp_path = data;
|
2016-06-16 01:27:16 +08:00
|
|
|
Error *err = NULL;
|
|
|
|
|
2016-08-19 02:46:03 +08:00
|
|
|
set_log_path_tmp(tmp_path, "qemu.log", &error_abort);
|
|
|
|
set_log_path_tmp(tmp_path, "qemu-%d.log", &error_abort);
|
|
|
|
set_log_path_tmp(tmp_path, "qemu.log.%d", &error_abort);
|
2016-06-16 01:27:16 +08:00
|
|
|
|
2016-08-19 02:46:03 +08:00
|
|
|
set_log_path_tmp(tmp_path, "qemu-%d%d.log", &err);
|
2016-06-16 01:27:16 +08:00
|
|
|
error_free_or_abort(&err);
|
2016-03-15 22:30:23 +08:00
|
|
|
}
|
2016-03-15 22:30:20 +08:00
|
|
|
|
2019-11-19 05:15:28 +08:00
|
|
|
static void test_logfile_write(gconstpointer data)
|
|
|
|
{
|
|
|
|
QemuLogFile *logfile;
|
|
|
|
QemuLogFile *logfile2;
|
|
|
|
gchar const *dir = data;
|
2020-01-21 17:28:13 +08:00
|
|
|
g_autofree gchar *file_path = NULL;
|
|
|
|
g_autofree gchar *file_path1 = NULL;
|
2019-11-19 05:15:28 +08:00
|
|
|
FILE *orig_fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Before starting test, set log flags, to ensure the file gets
|
|
|
|
* opened below with the call to qemu_set_log_filename().
|
|
|
|
* In cases where a logging backend other than log is used,
|
|
|
|
* this is needed.
|
|
|
|
*/
|
|
|
|
qemu_set_log(CPU_LOG_TB_OUT_ASM);
|
|
|
|
file_path = g_build_filename(dir, "qemu_test_log_write0.log", NULL);
|
|
|
|
file_path1 = g_build_filename(dir, "qemu_test_log_write1.log", NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test that even if an open file handle is changed,
|
|
|
|
* our handle remains valid due to RCU.
|
|
|
|
*/
|
2020-06-30 17:03:29 +08:00
|
|
|
qemu_set_log_filename(file_path, &error_abort);
|
2019-11-19 05:15:28 +08:00
|
|
|
rcu_read_lock();
|
2020-09-23 18:56:46 +08:00
|
|
|
logfile = qatomic_rcu_read(&qemu_logfile);
|
2019-11-19 05:15:28 +08:00
|
|
|
orig_fd = logfile->fd;
|
|
|
|
g_assert(logfile && logfile->fd);
|
|
|
|
fprintf(logfile->fd, "%s 1st write to file\n", __func__);
|
|
|
|
fflush(logfile->fd);
|
|
|
|
|
|
|
|
/* Change the logfile and ensure that the handle is still valid. */
|
2020-06-30 17:03:29 +08:00
|
|
|
qemu_set_log_filename(file_path1, &error_abort);
|
2020-09-23 18:56:46 +08:00
|
|
|
logfile2 = qatomic_rcu_read(&qemu_logfile);
|
2019-11-19 05:15:28 +08:00
|
|
|
g_assert(logfile->fd == orig_fd);
|
|
|
|
g_assert(logfile2->fd != logfile->fd);
|
|
|
|
fprintf(logfile->fd, "%s 2nd write to file\n", __func__);
|
|
|
|
fflush(logfile->fd);
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_logfile_lock(gconstpointer data)
|
|
|
|
{
|
|
|
|
FILE *logfile;
|
|
|
|
gchar const *dir = data;
|
2020-01-21 17:28:13 +08:00
|
|
|
g_autofree gchar *file_path = NULL;
|
2019-11-19 05:15:28 +08:00
|
|
|
|
|
|
|
file_path = g_build_filename(dir, "qemu_test_logfile_lock0.log", NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test the use of the logfile lock, such
|
|
|
|
* that even if an open file handle is closed,
|
|
|
|
* our handle remains valid for use due to RCU.
|
|
|
|
*/
|
2020-06-30 17:03:29 +08:00
|
|
|
qemu_set_log_filename(file_path, &error_abort);
|
2019-11-19 05:15:28 +08:00
|
|
|
logfile = qemu_log_lock();
|
|
|
|
g_assert(logfile);
|
|
|
|
fprintf(logfile, "%s 1st write to file\n", __func__);
|
|
|
|
fflush(logfile);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initiate a close file and make sure our handle remains
|
|
|
|
* valid since we still have the logfile lock.
|
|
|
|
*/
|
|
|
|
qemu_log_close();
|
|
|
|
fprintf(logfile, "%s 2nd write to file\n", __func__);
|
|
|
|
fflush(logfile);
|
|
|
|
qemu_log_unlock(logfile);
|
|
|
|
}
|
|
|
|
|
2016-08-19 02:46:03 +08:00
|
|
|
/* Remove a directory and all its entries (non-recursive). */
|
|
|
|
static void rmdir_full(gchar const *root)
|
|
|
|
{
|
|
|
|
GDir *root_gdir = g_dir_open(root, 0, NULL);
|
|
|
|
gchar const *entry_name;
|
|
|
|
|
|
|
|
g_assert_nonnull(root_gdir);
|
|
|
|
while ((entry_name = g_dir_read_name(root_gdir)) != NULL) {
|
|
|
|
gchar *entry_path = g_build_filename(root, entry_name, NULL);
|
|
|
|
g_assert(g_remove(entry_path) == 0);
|
|
|
|
g_free(entry_path);
|
|
|
|
}
|
|
|
|
g_dir_close(root_gdir);
|
|
|
|
g_assert(g_rmdir(root) == 0);
|
|
|
|
}
|
|
|
|
|
2016-03-15 22:30:20 +08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2020-09-15 20:13:08 +08:00
|
|
|
g_autofree gchar *tmp_path = g_dir_make_tmp("qemu-test-logging.XXXXXX", NULL);
|
2016-08-19 02:46:03 +08:00
|
|
|
int rc;
|
|
|
|
|
2016-03-15 22:30:20 +08:00
|
|
|
g_test_init(&argc, &argv, NULL);
|
2016-08-19 02:46:03 +08:00
|
|
|
g_assert_nonnull(tmp_path);
|
2016-03-15 22:30:20 +08:00
|
|
|
|
|
|
|
g_test_add_func("/logging/parse_range", test_parse_range);
|
2016-08-19 02:46:03 +08:00
|
|
|
g_test_add_data_func("/logging/parse_path", tmp_path, test_parse_path);
|
2019-11-19 05:15:28 +08:00
|
|
|
g_test_add_data_func("/logging/logfile_write_path",
|
|
|
|
tmp_path, test_logfile_write);
|
|
|
|
g_test_add_data_func("/logging/logfile_lock_path",
|
|
|
|
tmp_path, test_logfile_lock);
|
2016-08-19 02:46:03 +08:00
|
|
|
|
|
|
|
rc = g_test_run();
|
2020-09-15 20:13:14 +08:00
|
|
|
qemu_log_close();
|
|
|
|
drain_call_rcu();
|
2016-03-15 22:30:20 +08:00
|
|
|
|
2016-08-19 02:46:03 +08:00
|
|
|
rmdir_full(tmp_path);
|
|
|
|
return rc;
|
2016-03-15 22:30:20 +08:00
|
|
|
}
|