2012-06-03 15:03:23 +00:00
|
|
|
/*
|
|
|
|
* Logging support
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2020-10-23 12:44:24 +00:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2012-06-03 15:03:23 +00:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-01-29 17:50:05 +00:00
|
|
|
#include "qemu/osdep.h"
|
2012-12-17 17:20:00 +00:00
|
|
|
#include "qemu/log.h"
|
2016-03-15 14:30:20 +00:00
|
|
|
#include "qemu/range.h"
|
|
|
|
#include "qemu/error-report.h"
|
2016-06-15 17:27:15 +00:00
|
|
|
#include "qapi/error.h"
|
2016-03-15 14:30:20 +00:00
|
|
|
#include "qemu/cutils.h"
|
2016-01-07 13:55:32 +00:00
|
|
|
#include "trace/control.h"
|
2019-11-18 21:15:25 +00:00
|
|
|
#include "qemu/thread.h"
|
2020-04-04 04:21:08 +00:00
|
|
|
#include "qemu/lockable.h"
|
2022-04-17 18:30:05 +00:00
|
|
|
#include "qemu/rcu.h"
|
2022-04-17 18:30:19 +00:00
|
|
|
#ifdef CONFIG_LINUX
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#endif
|
2022-04-17 18:30:05 +00:00
|
|
|
|
|
|
|
|
2022-04-17 18:30:17 +00:00
|
|
|
typedef struct RCUCloseFILE {
|
2022-04-17 18:30:05 +00:00
|
|
|
struct rcu_head rcu;
|
|
|
|
FILE *fd;
|
2022-04-17 18:30:17 +00:00
|
|
|
} RCUCloseFILE;
|
2012-06-03 15:03:23 +00:00
|
|
|
|
2022-04-17 18:30:14 +00:00
|
|
|
/* Mutex covering the other global_* variables. */
|
|
|
|
static QemuMutex global_mutex;
|
2022-04-17 18:30:12 +00:00
|
|
|
static char *global_filename;
|
2022-04-17 18:30:18 +00:00
|
|
|
static FILE *global_file;
|
2022-04-17 18:30:19 +00:00
|
|
|
static __thread FILE *thread_file;
|
2022-10-21 10:57:34 +00:00
|
|
|
static __thread Notifier qemu_log_thread_cleanup_notifier;
|
2022-04-17 18:30:14 +00:00
|
|
|
|
2012-06-03 16:35:32 +00:00
|
|
|
int qemu_loglevel;
|
2022-04-17 18:30:19 +00:00
|
|
|
static bool log_per_thread;
|
2016-03-15 14:30:20 +00:00
|
|
|
static GArray *debug_regions;
|
2012-06-03 15:03:23 +00:00
|
|
|
|
2022-04-17 18:30:05 +00:00
|
|
|
/* Returns true if qemu_log() will really write somewhere. */
|
|
|
|
bool qemu_log_enabled(void)
|
|
|
|
{
|
2022-04-17 18:30:19 +00:00
|
|
|
return log_per_thread || qatomic_read(&global_file) != NULL;
|
2022-04-17 18:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true if qemu_log() will write somewhere other than stderr. */
|
|
|
|
bool qemu_log_separate(void)
|
|
|
|
{
|
2022-04-17 18:30:19 +00:00
|
|
|
if (log_per_thread) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
FILE *logfile = qatomic_read(&global_file);
|
|
|
|
return logfile && logfile != stderr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int log_thread_id(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_GETTID
|
|
|
|
return gettid();
|
|
|
|
#elif defined(SYS_gettid)
|
|
|
|
return syscall(SYS_gettid);
|
|
|
|
#else
|
|
|
|
static int counter;
|
|
|
|
return qatomic_fetch_inc(&counter);
|
|
|
|
#endif
|
2022-04-17 18:30:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 10:57:34 +00:00
|
|
|
static void qemu_log_thread_cleanup(Notifier *n, void *unused)
|
|
|
|
{
|
2022-11-08 14:00:32 +00:00
|
|
|
if (thread_file != stderr) {
|
|
|
|
fclose(thread_file);
|
|
|
|
thread_file = NULL;
|
|
|
|
}
|
2022-10-21 10:57:34 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 18:29:46 +00:00
|
|
|
/* Lock/unlock output. */
|
|
|
|
|
2022-11-08 14:00:32 +00:00
|
|
|
static FILE *qemu_log_trylock_with_err(Error **errp)
|
2022-04-17 18:29:46 +00:00
|
|
|
{
|
2022-04-17 18:30:18 +00:00
|
|
|
FILE *logfile;
|
2022-04-17 18:29:47 +00:00
|
|
|
|
2022-04-17 18:30:19 +00:00
|
|
|
logfile = thread_file;
|
|
|
|
if (!logfile) {
|
|
|
|
if (log_per_thread) {
|
|
|
|
g_autofree char *filename
|
|
|
|
= g_strdup_printf(global_filename, log_thread_id());
|
|
|
|
logfile = fopen(filename, "w");
|
|
|
|
if (!logfile) {
|
2022-11-08 14:00:32 +00:00
|
|
|
error_setg_errno(errp, errno,
|
|
|
|
"Error opening logfile %s for thread %d",
|
|
|
|
filename, log_thread_id());
|
2022-04-17 18:30:19 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
thread_file = logfile;
|
2022-10-21 10:57:34 +00:00
|
|
|
qemu_log_thread_cleanup_notifier.notify = qemu_log_thread_cleanup;
|
|
|
|
qemu_thread_atexit_add(&qemu_log_thread_cleanup_notifier);
|
2022-04-17 18:30:19 +00:00
|
|
|
} else {
|
|
|
|
rcu_read_lock();
|
|
|
|
/*
|
|
|
|
* FIXME: typeof_strip_qual, as used by qatomic_rcu_read,
|
|
|
|
* does not work with pointers to undefined structures,
|
|
|
|
* such as we have with struct _IO_FILE and musl libc.
|
|
|
|
* Since all we want is a read of a pointer, cast to void**,
|
|
|
|
* which does work with typeof_strip_qual.
|
|
|
|
*/
|
|
|
|
logfile = qatomic_rcu_read((void **)&global_file);
|
|
|
|
if (!logfile) {
|
|
|
|
rcu_read_unlock();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2022-04-17 18:29:46 +00:00
|
|
|
}
|
2022-04-17 18:30:19 +00:00
|
|
|
|
|
|
|
qemu_flockfile(logfile);
|
2022-04-17 18:30:18 +00:00
|
|
|
return logfile;
|
2022-04-17 18:29:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 14:00:32 +00:00
|
|
|
FILE *qemu_log_trylock(void)
|
|
|
|
{
|
|
|
|
return qemu_log_trylock_with_err(NULL);
|
|
|
|
}
|
|
|
|
|
2022-04-17 18:30:18 +00:00
|
|
|
void qemu_log_unlock(FILE *logfile)
|
2022-04-17 18:29:46 +00:00
|
|
|
{
|
2022-04-17 18:30:18 +00:00
|
|
|
if (logfile) {
|
|
|
|
fflush(logfile);
|
|
|
|
qemu_funlockfile(logfile);
|
2022-04-17 18:30:19 +00:00
|
|
|
if (!log_per_thread) {
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
2022-04-17 18:29:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 18:29:57 +00:00
|
|
|
void qemu_log(const char *fmt, ...)
|
2012-06-03 16:35:32 +00:00
|
|
|
{
|
2022-04-17 18:29:56 +00:00
|
|
|
FILE *f = qemu_log_trylock();
|
|
|
|
if (f) {
|
2016-06-24 02:15:55 +00:00
|
|
|
va_list ap;
|
2022-04-17 18:29:56 +00:00
|
|
|
|
2016-06-24 02:15:55 +00:00
|
|
|
va_start(ap, fmt);
|
2022-04-17 18:29:57 +00:00
|
|
|
vfprintf(f, fmt, ap);
|
2016-06-24 02:15:55 +00:00
|
|
|
va_end(ap);
|
2022-04-17 18:29:56 +00:00
|
|
|
qemu_log_unlock(f);
|
2012-06-03 16:35:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-17 18:30:14 +00:00
|
|
|
static void __attribute__((__constructor__)) startup(void)
|
2019-11-18 21:15:25 +00:00
|
|
|
{
|
2022-04-17 18:30:14 +00:00
|
|
|
qemu_mutex_init(&global_mutex);
|
2019-11-18 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 18:30:17 +00:00
|
|
|
static void rcu_close_file(RCUCloseFILE *r)
|
2019-11-18 21:15:27 +00:00
|
|
|
{
|
2022-04-17 18:30:18 +00:00
|
|
|
fclose(r->fd);
|
2022-04-17 18:30:17 +00:00
|
|
|
g_free(r);
|
2019-11-18 21:15:27 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 18:30:19 +00:00
|
|
|
/**
|
|
|
|
* valid_filename_template:
|
|
|
|
*
|
|
|
|
* Validate the filename template. Require %d if per_thread, allow it
|
|
|
|
* otherwise; require no other % within the template.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
vft_error,
|
|
|
|
vft_stderr,
|
|
|
|
vft_strdup,
|
|
|
|
vft_pid_printf,
|
|
|
|
} ValidFilenameTemplateResult;
|
|
|
|
|
|
|
|
static ValidFilenameTemplateResult
|
|
|
|
valid_filename_template(const char *filename, bool per_thread, Error **errp)
|
|
|
|
{
|
|
|
|
if (filename) {
|
|
|
|
char *pidstr = strstr(filename, "%");
|
|
|
|
|
|
|
|
if (pidstr) {
|
|
|
|
/* We only accept one %d, no other format strings */
|
|
|
|
if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) {
|
|
|
|
error_setg(errp, "Bad logfile template: %s", filename);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return per_thread ? vft_strdup : vft_pid_printf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (per_thread) {
|
|
|
|
error_setg(errp, "Filename template with '%%d' required for 'tid'");
|
|
|
|
return vft_error;
|
|
|
|
}
|
|
|
|
return filename ? vft_strdup : vft_stderr;
|
|
|
|
}
|
|
|
|
|
2012-06-03 15:03:23 +00:00
|
|
|
/* enable or disable low levels log */
|
2022-04-17 18:30:07 +00:00
|
|
|
static bool qemu_set_log_internal(const char *filename, bool changed_name,
|
|
|
|
int log_flags, Error **errp)
|
2012-06-03 15:03:23 +00:00
|
|
|
{
|
2022-04-17 18:30:07 +00:00
|
|
|
bool need_to_open_file;
|
2022-04-17 18:30:15 +00:00
|
|
|
bool daemonized;
|
2022-04-17 18:30:19 +00:00
|
|
|
bool per_thread;
|
2022-04-17 18:30:18 +00:00
|
|
|
FILE *logfile;
|
2019-11-18 21:15:27 +00:00
|
|
|
|
2022-04-17 18:30:14 +00:00
|
|
|
QEMU_LOCK_GUARD(&global_mutex);
|
2022-04-17 18:30:13 +00:00
|
|
|
logfile = global_file;
|
2022-11-04 12:00:58 +00:00
|
|
|
|
|
|
|
/* The per-thread flag is immutable. */
|
|
|
|
if (log_per_thread) {
|
|
|
|
log_flags |= LOG_PER_THREAD;
|
2022-11-04 12:00:59 +00:00
|
|
|
} else {
|
|
|
|
if (global_filename) {
|
|
|
|
log_flags &= ~LOG_PER_THREAD;
|
|
|
|
}
|
2022-11-04 12:00:58 +00:00
|
|
|
}
|
2022-04-17 18:30:07 +00:00
|
|
|
|
2022-04-17 18:30:19 +00:00
|
|
|
per_thread = log_flags & LOG_PER_THREAD;
|
|
|
|
|
2022-04-17 18:30:07 +00:00
|
|
|
if (changed_name) {
|
|
|
|
char *newname = NULL;
|
|
|
|
|
|
|
|
/*
|
2022-04-17 18:30:19 +00:00
|
|
|
* Once threads start opening their own log files, we have no
|
|
|
|
* easy mechanism to tell them all to close and re-open.
|
|
|
|
* There seems little cause to do so either -- this option
|
|
|
|
* will most often be used at user-only startup.
|
2022-04-17 18:30:07 +00:00
|
|
|
*/
|
2022-04-17 18:30:19 +00:00
|
|
|
if (log_per_thread) {
|
|
|
|
error_setg(errp, "Cannot change log filename after setting 'tid'");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (valid_filename_template(filename, per_thread, errp)) {
|
|
|
|
case vft_error:
|
|
|
|
return false;
|
|
|
|
case vft_stderr:
|
|
|
|
break;
|
|
|
|
case vft_strdup:
|
|
|
|
newname = g_strdup(filename);
|
|
|
|
break;
|
|
|
|
case vft_pid_printf:
|
|
|
|
newname = g_strdup_printf(filename, getpid());
|
|
|
|
break;
|
2022-04-17 18:30:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 18:30:12 +00:00
|
|
|
g_free(global_filename);
|
|
|
|
global_filename = newname;
|
2022-04-17 18:30:07 +00:00
|
|
|
filename = newname;
|
|
|
|
} else {
|
2022-04-17 18:30:12 +00:00
|
|
|
filename = global_filename;
|
2022-04-17 18:30:19 +00:00
|
|
|
if (per_thread &&
|
|
|
|
valid_filename_template(filename, true, errp) == vft_error) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-04-17 18:30:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 18:30:19 +00:00
|
|
|
/* Once the per-thread flag is set, it cannot be unset. */
|
|
|
|
if (per_thread) {
|
|
|
|
log_per_thread = true;
|
|
|
|
}
|
|
|
|
/* The flag itself is not relevant for need_to_open_file. */
|
|
|
|
log_flags &= ~LOG_PER_THREAD;
|
2016-01-07 13:55:30 +00:00
|
|
|
#ifdef CONFIG_TRACE_LOG
|
2022-04-17 18:30:07 +00:00
|
|
|
log_flags |= LOG_TRACE;
|
2016-01-07 13:55:30 +00:00
|
|
|
#endif
|
2022-04-17 18:30:07 +00:00
|
|
|
qemu_loglevel = log_flags;
|
|
|
|
|
2022-04-17 18:30:15 +00:00
|
|
|
daemonized = is_daemonized();
|
2022-11-08 14:00:32 +00:00
|
|
|
need_to_open_file = false;
|
|
|
|
if (!daemonized) {
|
|
|
|
/*
|
|
|
|
* If not daemonized we only log if qemu_loglevel is set, either to
|
|
|
|
* stderr or to a file (if there is a filename).
|
|
|
|
* If per-thread, open the file for each thread in qemu_log_trylock().
|
|
|
|
*/
|
|
|
|
need_to_open_file = qemu_loglevel && !log_per_thread;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If we are daemonized, we will only log if there is a filename.
|
|
|
|
*/
|
|
|
|
need_to_open_file = filename != NULL;
|
|
|
|
}
|
2022-04-17 18:30:07 +00:00
|
|
|
|
2022-11-08 14:00:31 +00:00
|
|
|
if (logfile) {
|
|
|
|
fflush(logfile);
|
|
|
|
if (changed_name && logfile != stderr) {
|
2022-04-17 18:30:18 +00:00
|
|
|
RCUCloseFILE *r = g_new0(RCUCloseFILE, 1);
|
|
|
|
r->fd = logfile;
|
2022-11-08 14:00:31 +00:00
|
|
|
qatomic_rcu_set(&global_file, NULL);
|
2022-04-17 18:30:18 +00:00
|
|
|
call_rcu(r, rcu_close_file, rcu);
|
2022-11-08 14:00:31 +00:00
|
|
|
logfile = NULL;
|
2022-04-17 18:30:18 +00:00
|
|
|
}
|
2022-04-17 18:30:07 +00:00
|
|
|
}
|
2022-04-17 18:30:16 +00:00
|
|
|
|
2022-11-08 14:00:32 +00:00
|
|
|
if (log_per_thread && daemonized) {
|
|
|
|
logfile = thread_file;
|
|
|
|
}
|
|
|
|
|
2022-04-17 18:30:07 +00:00
|
|
|
if (!logfile && need_to_open_file) {
|
|
|
|
if (filename) {
|
2022-11-08 14:00:32 +00:00
|
|
|
if (log_per_thread) {
|
|
|
|
logfile = qemu_log_trylock_with_err(errp);
|
|
|
|
if (!logfile) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
qemu_log_unlock(logfile);
|
|
|
|
} else {
|
|
|
|
logfile = fopen(filename, "w");
|
|
|
|
if (!logfile) {
|
|
|
|
error_setg_errno(errp, errno, "Error opening logfile %s",
|
|
|
|
filename);
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-26 17:52:40 +00:00
|
|
|
}
|
2016-02-18 11:38:38 +00:00
|
|
|
/* In case we are a daemon redirect stderr to logfile */
|
2022-04-17 18:30:15 +00:00
|
|
|
if (daemonized) {
|
2022-04-17 18:30:18 +00:00
|
|
|
dup2(fileno(logfile), STDERR_FILENO);
|
|
|
|
fclose(logfile);
|
2022-11-08 14:00:32 +00:00
|
|
|
/*
|
|
|
|
* This will skip closing logfile in rcu_close_file()
|
|
|
|
* or qemu_log_thread_cleanup().
|
|
|
|
*/
|
2022-04-17 18:30:18 +00:00
|
|
|
logfile = stderr;
|
2016-02-18 11:38:38 +00:00
|
|
|
}
|
2013-02-26 17:52:40 +00:00
|
|
|
} else {
|
|
|
|
/* Default to stderr if no log file specified */
|
2022-04-17 18:30:15 +00:00
|
|
|
assert(!daemonized);
|
2022-04-17 18:30:18 +00:00
|
|
|
logfile = stderr;
|
2012-06-03 15:03:23 +00:00
|
|
|
}
|
2012-07-07 14:40:18 +00:00
|
|
|
|
2022-11-08 14:00:32 +00:00
|
|
|
if (log_per_thread && daemonized) {
|
|
|
|
thread_file = logfile;
|
|
|
|
} else {
|
|
|
|
qatomic_rcu_set(&global_file, logfile);
|
|
|
|
}
|
2012-06-03 15:03:23 +00:00
|
|
|
}
|
2022-04-17 18:29:44 +00:00
|
|
|
return true;
|
2012-06-03 15:03:23 +00:00
|
|
|
}
|
2015-12-04 12:12:57 +00:00
|
|
|
|
2022-04-17 18:30:07 +00:00
|
|
|
bool qemu_set_log(int log_flags, Error **errp)
|
2012-06-03 15:03:23 +00:00
|
|
|
{
|
2022-04-17 18:30:07 +00:00
|
|
|
return qemu_set_log_internal(NULL, false, log_flags, errp);
|
|
|
|
}
|
2016-03-15 14:30:23 +00:00
|
|
|
|
2022-04-17 18:30:07 +00:00
|
|
|
bool qemu_set_log_filename(const char *filename, Error **errp)
|
|
|
|
{
|
|
|
|
return qemu_set_log_internal(filename, true, qemu_loglevel, errp);
|
|
|
|
}
|
2020-01-23 19:36:26 +00:00
|
|
|
|
2022-04-17 18:30:07 +00:00
|
|
|
bool qemu_set_log_filename_flags(const char *name, int flags, Error **errp)
|
|
|
|
{
|
|
|
|
return qemu_set_log_internal(name, true, flags, errp);
|
2012-06-03 15:03:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-15 14:30:20 +00:00
|
|
|
/* Returns true if addr is in our debug filter or no filter defined
|
|
|
|
*/
|
|
|
|
bool qemu_log_in_addr_range(uint64_t addr)
|
|
|
|
{
|
|
|
|
if (debug_regions) {
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < debug_regions->len; i++) {
|
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 11:47:46 +00:00
|
|
|
Range *range = &g_array_index(debug_regions, Range, i);
|
2016-07-01 11:47:47 +00:00
|
|
|
if (range_contains(range, addr)) {
|
2016-03-15 14:30:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-15 17:27:15 +00:00
|
|
|
void qemu_set_dfilter_ranges(const char *filter_spec, Error **errp)
|
2016-03-15 14:30:20 +00:00
|
|
|
{
|
|
|
|
gchar **ranges = g_strsplit(filter_spec, ",", 0);
|
2016-06-15 17:27:15 +00:00
|
|
|
int i;
|
2016-06-15 17:27:14 +00:00
|
|
|
|
|
|
|
if (debug_regions) {
|
|
|
|
g_array_unref(debug_regions);
|
|
|
|
debug_regions = NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-15 17:27:15 +00:00
|
|
|
debug_regions = g_array_sized_new(FALSE, FALSE,
|
|
|
|
sizeof(Range), g_strv_length(ranges));
|
|
|
|
for (i = 0; ranges[i]; i++) {
|
|
|
|
const char *r = ranges[i];
|
|
|
|
const char *range_op, *r2, *e;
|
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 11:47:46 +00:00
|
|
|
uint64_t r1val, r2val, lob, upb;
|
2016-06-15 17:27:15 +00:00
|
|
|
struct Range range;
|
|
|
|
|
|
|
|
range_op = strstr(r, "-");
|
|
|
|
r2 = range_op ? range_op + 1 : NULL;
|
|
|
|
if (!range_op) {
|
|
|
|
range_op = strstr(r, "+");
|
|
|
|
r2 = range_op ? range_op + 1 : NULL;
|
|
|
|
}
|
|
|
|
if (!range_op) {
|
|
|
|
range_op = strstr(r, "..");
|
|
|
|
r2 = range_op ? range_op + 2 : NULL;
|
|
|
|
}
|
|
|
|
if (!range_op) {
|
|
|
|
error_setg(errp, "Bad range specifier");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-02-21 20:13:50 +00:00
|
|
|
if (qemu_strtou64(r, &e, 0, &r1val)
|
2016-06-15 17:27:15 +00:00
|
|
|
|| e != range_op) {
|
|
|
|
error_setg(errp, "Invalid number to the left of %.*s",
|
|
|
|
(int)(r2 - range_op), range_op);
|
|
|
|
goto out;
|
|
|
|
}
|
2017-02-21 20:13:50 +00:00
|
|
|
if (qemu_strtou64(r2, NULL, 0, &r2val)) {
|
2016-06-15 17:27:15 +00:00
|
|
|
error_setg(errp, "Invalid number to the right of %.*s",
|
|
|
|
(int)(r2 - range_op), range_op);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*range_op) {
|
|
|
|
case '+':
|
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 11:47:46 +00:00
|
|
|
lob = r1val;
|
|
|
|
upb = r1val + r2val - 1;
|
2016-06-15 17:27:15 +00:00
|
|
|
break;
|
|
|
|
case '-':
|
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 11:47:46 +00:00
|
|
|
upb = r1val;
|
|
|
|
lob = r1val - (r2val - 1);
|
2016-06-15 17:27:15 +00:00
|
|
|
break;
|
|
|
|
case '.':
|
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 11:47:46 +00:00
|
|
|
lob = r1val;
|
|
|
|
upb = r2val;
|
2016-06-15 17:27:15 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
2016-03-15 14:30:20 +00:00
|
|
|
}
|
2016-07-01 11:47:49 +00:00
|
|
|
if (lob > upb) {
|
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 11:47:46 +00:00
|
|
|
error_setg(errp, "Invalid range");
|
|
|
|
goto out;
|
|
|
|
}
|
2016-07-01 11:47:47 +00:00
|
|
|
range_set_bounds(&range, lob, upb);
|
2016-06-15 17:27:15 +00:00
|
|
|
g_array_append_val(debug_regions, range);
|
2016-03-15 14:30:20 +00:00
|
|
|
}
|
2016-06-15 17:27:15 +00:00
|
|
|
out:
|
|
|
|
g_strfreev(ranges);
|
2016-03-15 14:30:20 +00:00
|
|
|
}
|
|
|
|
|
2013-02-11 16:41:25 +00:00
|
|
|
const QEMULogItem qemu_log_items[] = {
|
2012-06-03 15:03:23 +00:00
|
|
|
{ CPU_LOG_TB_OUT_ASM, "out_asm",
|
|
|
|
"show generated host assembly code for each compiled TB" },
|
|
|
|
{ CPU_LOG_TB_IN_ASM, "in_asm",
|
|
|
|
"show target assembly code for each compiled TB" },
|
|
|
|
{ CPU_LOG_TB_OP, "op",
|
|
|
|
"show micro ops for each compiled TB" },
|
|
|
|
{ CPU_LOG_TB_OP_OPT, "op_opt",
|
2016-06-24 03:34:33 +00:00
|
|
|
"show micro ops after optimization" },
|
|
|
|
{ CPU_LOG_TB_OP_IND, "op_ind",
|
|
|
|
"show micro ops before indirect lowering" },
|
2012-06-03 15:03:23 +00:00
|
|
|
{ CPU_LOG_INT, "int",
|
|
|
|
"show interrupts/exceptions in short format" },
|
|
|
|
{ CPU_LOG_EXEC, "exec",
|
|
|
|
"show trace before each executed TB (lots of logs)" },
|
|
|
|
{ CPU_LOG_TB_CPU, "cpu",
|
2016-03-15 14:30:17 +00:00
|
|
|
"show CPU registers before entering a TB (lots of logs)" },
|
2018-05-15 13:58:44 +00:00
|
|
|
{ CPU_LOG_TB_FPU, "fpu",
|
|
|
|
"include FPU registers in the 'cpu' logging" },
|
2014-12-13 16:48:18 +00:00
|
|
|
{ CPU_LOG_MMU, "mmu",
|
|
|
|
"log MMU-related activities" },
|
2012-06-03 15:03:23 +00:00
|
|
|
{ CPU_LOG_PCALL, "pcall",
|
2012-07-07 14:40:18 +00:00
|
|
|
"x86 only: show protected mode far calls/returns/exceptions" },
|
2012-06-03 15:03:23 +00:00
|
|
|
{ CPU_LOG_RESET, "cpu_reset",
|
2015-01-27 12:11:26 +00:00
|
|
|
"show CPU state before CPU resets" },
|
2012-06-03 17:04:28 +00:00
|
|
|
{ LOG_UNIMP, "unimp",
|
|
|
|
"log unimplemented functionality" },
|
2012-10-18 13:11:35 +00:00
|
|
|
{ LOG_GUEST_ERROR, "guest_errors",
|
|
|
|
"log when the guest OS does something invalid (eg accessing a\n"
|
|
|
|
"non-existent register)" },
|
2015-11-13 11:32:19 +00:00
|
|
|
{ CPU_LOG_PAGE, "page",
|
|
|
|
"dump pages at beginning of user mode emulation" },
|
2015-09-16 22:33:53 +00:00
|
|
|
{ CPU_LOG_TB_NOCHAIN, "nochain",
|
|
|
|
"do not chain compiled TBs so that \"exec\" and \"cpu\" show\n"
|
|
|
|
"complete traces" },
|
2019-10-11 15:34:05 +00:00
|
|
|
#ifdef CONFIG_PLUGIN
|
2023-01-19 21:40:33 +00:00
|
|
|
{ CPU_LOG_PLUGIN, "plugin", "output from TCG plugins"},
|
2019-10-11 15:34:05 +00:00
|
|
|
#endif
|
2020-02-04 02:54:14 +00:00
|
|
|
{ LOG_STRACE, "strace",
|
|
|
|
"log every user-mode syscall, its input, and its result" },
|
2022-04-17 18:30:19 +00:00
|
|
|
{ LOG_PER_THREAD, "tid",
|
|
|
|
"open a separate log file per thread; filename must contain '%d'" },
|
2012-06-03 15:03:23 +00:00
|
|
|
{ 0, NULL, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
/* takes a comma separated list of log masks. Return 0 if error. */
|
2013-02-11 16:41:22 +00:00
|
|
|
int qemu_str_to_log_mask(const char *str)
|
2012-06-03 15:03:23 +00:00
|
|
|
{
|
2013-02-11 16:41:25 +00:00
|
|
|
const QEMULogItem *item;
|
2016-09-06 18:25:43 +00:00
|
|
|
int mask = 0;
|
|
|
|
char **parts = g_strsplit(str, ",", 0);
|
|
|
|
char **tmp;
|
2012-06-03 15:03:23 +00:00
|
|
|
|
2016-09-06 18:25:43 +00:00
|
|
|
for (tmp = parts; tmp && *tmp; tmp++) {
|
|
|
|
if (g_str_equal(*tmp, "all")) {
|
2013-02-11 16:41:25 +00:00
|
|
|
for (item = qemu_log_items; item->mask != 0; item++) {
|
2012-06-03 15:03:23 +00:00
|
|
|
mask |= item->mask;
|
|
|
|
}
|
2016-01-07 13:55:32 +00:00
|
|
|
#ifdef CONFIG_TRACE_LOG
|
2016-09-06 18:25:43 +00:00
|
|
|
} else if (g_str_has_prefix(*tmp, "trace:") && (*tmp)[6] != '\0') {
|
|
|
|
trace_enable_events((*tmp) + 6);
|
2016-01-07 13:55:32 +00:00
|
|
|
mask |= LOG_TRACE;
|
|
|
|
#endif
|
2012-06-03 15:03:23 +00:00
|
|
|
} else {
|
2013-02-11 16:41:25 +00:00
|
|
|
for (item = qemu_log_items; item->mask != 0; item++) {
|
2016-09-06 18:25:43 +00:00
|
|
|
if (g_str_equal(*tmp, item->name)) {
|
2012-06-03 15:03:23 +00:00
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
2016-09-06 18:25:43 +00:00
|
|
|
goto error;
|
2016-01-07 13:55:32 +00:00
|
|
|
found:
|
|
|
|
mask |= item->mask;
|
2012-06-03 15:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-06 18:25:43 +00:00
|
|
|
|
|
|
|
g_strfreev(parts);
|
2012-06-03 15:03:23 +00:00
|
|
|
return mask;
|
2016-09-06 18:25:43 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
g_strfreev(parts);
|
|
|
|
return 0;
|
2012-06-03 15:03:23 +00:00
|
|
|
}
|
2013-02-11 16:41:21 +00:00
|
|
|
|
|
|
|
void qemu_print_log_usage(FILE *f)
|
|
|
|
{
|
2013-02-11 16:41:25 +00:00
|
|
|
const QEMULogItem *item;
|
2013-02-11 16:41:21 +00:00
|
|
|
fprintf(f, "Log items (comma separated):\n");
|
2013-02-11 16:41:25 +00:00
|
|
|
for (item = qemu_log_items; item->mask != 0; item++) {
|
2016-01-07 13:55:32 +00:00
|
|
|
fprintf(f, "%-15s %s\n", item->name, item->help);
|
2013-02-11 16:41:21 +00:00
|
|
|
}
|
2016-01-07 13:55:32 +00:00
|
|
|
#ifdef CONFIG_TRACE_LOG
|
|
|
|
fprintf(f, "trace:PATTERN enable trace events\n");
|
|
|
|
fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n");
|
|
|
|
#endif
|
2013-02-11 16:41:21 +00:00
|
|
|
}
|