util/log: Rename qemu_log_lock to qemu_log_trylock

This function can fail, which makes it more like ftrylockfile
or pthread_mutex_trylock than flockfile or pthread_mutex_lock,
so rename it.

To closer match the other trylock functions, release rcu_read_lock
along the failure path, so that qemu_log_unlock need not be called
on failure.

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20220417183019.755276-8-richard.henderson@linaro.org>
This commit is contained in:
Richard Henderson 2022-04-17 11:29:47 -07:00
parent c59fe6e536
commit c60f599bcb
12 changed files with 18 additions and 16 deletions

View File

@ -214,7 +214,7 @@ static inline void log_cpu_exec(target_ulong pc, CPUState *cpu,
#if defined(DEBUG_DISAS) #if defined(DEBUG_DISAS)
if (qemu_loglevel_mask(CPU_LOG_TB_CPU)) { if (qemu_loglevel_mask(CPU_LOG_TB_CPU)) {
FILE *logfile = qemu_log_lock(); FILE *logfile = qemu_log_trylock();
int flags = 0; int flags = 0;
if (qemu_loglevel_mask(CPU_LOG_TB_FPU)) { if (qemu_loglevel_mask(CPU_LOG_TB_FPU)) {

View File

@ -1527,7 +1527,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
#ifdef DEBUG_DISAS #ifdef DEBUG_DISAS
if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM) && if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM) &&
qemu_log_in_addr_range(tb->pc)) { qemu_log_in_addr_range(tb->pc)) {
FILE *logfile = qemu_log_lock(); FILE *logfile = qemu_log_trylock();
int code_size, data_size; int code_size, data_size;
const tcg_target_ulong *rx_data_gen_ptr; const tcg_target_ulong *rx_data_gen_ptr;
size_t chunk_start; size_t chunk_start;

View File

@ -139,7 +139,7 @@ void translator_loop(const TranslatorOps *ops, DisasContextBase *db,
#ifdef DEBUG_DISAS #ifdef DEBUG_DISAS
if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
&& qemu_log_in_addr_range(db->pc_first)) { && qemu_log_in_addr_range(db->pc_first)) {
FILE *logfile = qemu_log_lock(); FILE *logfile = qemu_log_trylock();
qemu_log("----------------\n"); qemu_log("----------------\n");
ops->disas_log(db, cpu); ops->disas_log(db, cpu);
qemu_log("\n"); qemu_log("\n");

2
cpu.c
View File

@ -399,7 +399,7 @@ void cpu_abort(CPUState *cpu, const char *fmt, ...)
fprintf(stderr, "\n"); fprintf(stderr, "\n");
cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP); cpu_dump_state(cpu, stderr, CPU_DUMP_FPU | CPU_DUMP_CCOP);
if (qemu_log_separate()) { if (qemu_log_separate()) {
FILE *logfile = qemu_log_lock(); FILE *logfile = qemu_log_trylock();
qemu_log("qemu: fatal: "); qemu_log("qemu: fatal: ");
qemu_log_vprintf(fmt, ap2); qemu_log_vprintf(fmt, ap2);
qemu_log("\n"); qemu_log("\n");

View File

@ -247,7 +247,7 @@ int can_sja_accept_filter(CanSJA1000State *s,
static void can_display_msg(const char *prefix, const qemu_can_frame *msg) static void can_display_msg(const char *prefix, const qemu_can_frame *msg)
{ {
int i; int i;
FILE *logfile = qemu_log_lock(); FILE *logfile = qemu_log_trylock();
qemu_log("%s%03X [%01d] %s %s", qemu_log("%s%03X [%01d] %s %s",
prefix, prefix,

View File

@ -71,7 +71,7 @@ static inline void log_disas(const void *code, unsigned long size)
/* page_dump() output to the log file: */ /* page_dump() output to the log file: */
static inline void log_page_dump(const char *operation) static inline void log_page_dump(const char *operation)
{ {
FILE *logfile = qemu_log_lock(); FILE *logfile = qemu_log_trylock();
if (logfile) { if (logfile) {
qemu_log("page layout changed following %s\n", operation); qemu_log("page layout changed following %s\n", operation);
page_dump(logfile); page_dump(logfile);

View File

@ -67,7 +67,7 @@ static inline bool qemu_log_separate(void)
/* Lock/unlock output. */ /* Lock/unlock output. */
FILE *qemu_log_lock(void); FILE *qemu_log_trylock(void);
void qemu_log_unlock(FILE *fd); void qemu_log_unlock(FILE *fd);
/* Logging functions: */ /* Logging functions: */

View File

@ -76,7 +76,7 @@ QEMU_BUILD_BUG_ON(offsetof(qemu_can_frame, data)
static void can_host_socketcan_display_msg(struct qemu_can_frame *msg) static void can_host_socketcan_display_msg(struct qemu_can_frame *msg)
{ {
int i; int i;
FILE *logfile = qemu_log_lock(); FILE *logfile = qemu_log_trylock();
qemu_log("[cansocketcan]: %03X [%01d] %s %s", qemu_log("[cansocketcan]: %03X [%01d] %s %s",
msg->can_id & QEMU_CAN_EFF_MASK, msg->can_id & QEMU_CAN_EFF_MASK,
msg->can_dlc, msg->can_dlc,

View File

@ -2580,7 +2580,7 @@ static void gen_unknown_opcode(CPUX86State *env, DisasContext *s)
gen_illegal_opcode(s); gen_illegal_opcode(s);
if (qemu_loglevel_mask(LOG_UNIMP)) { if (qemu_loglevel_mask(LOG_UNIMP)) {
FILE *logfile = qemu_log_lock(); FILE *logfile = qemu_log_trylock();
target_ulong pc = s->pc_start, end = s->pc; target_ulong pc = s->pc_start, end = s->pc;
qemu_log("ILLOPC: " TARGET_FMT_lx ":", pc); qemu_log("ILLOPC: " TARGET_FMT_lx ":", pc);

View File

@ -756,7 +756,7 @@ void tcg_prologue_init(TCGContext *s)
#ifdef DEBUG_DISAS #ifdef DEBUG_DISAS
if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) { if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
FILE *logfile = qemu_log_lock(); FILE *logfile = qemu_log_trylock();
qemu_log("PROLOGUE: [size=%zu]\n", prologue_size); qemu_log("PROLOGUE: [size=%zu]\n", prologue_size);
if (s->data_gen_ptr) { if (s->data_gen_ptr) {
size_t code_size = s->data_gen_ptr - s->code_gen_ptr; size_t code_size = s->data_gen_ptr - s->code_gen_ptr;
@ -4200,7 +4200,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
#ifdef DEBUG_DISAS #ifdef DEBUG_DISAS
if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP) if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)
&& qemu_log_in_addr_range(tb->pc))) { && qemu_log_in_addr_range(tb->pc))) {
FILE *logfile = qemu_log_lock(); FILE *logfile = qemu_log_trylock();
qemu_log("OP:\n"); qemu_log("OP:\n");
tcg_dump_ops(s, false); tcg_dump_ops(s, false);
qemu_log("\n"); qemu_log("\n");
@ -4245,7 +4245,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
#ifdef DEBUG_DISAS #ifdef DEBUG_DISAS
if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND) if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND)
&& qemu_log_in_addr_range(tb->pc))) { && qemu_log_in_addr_range(tb->pc))) {
FILE *logfile = qemu_log_lock(); FILE *logfile = qemu_log_trylock();
qemu_log("OP before indirect lowering:\n"); qemu_log("OP before indirect lowering:\n");
tcg_dump_ops(s, false); tcg_dump_ops(s, false);
qemu_log("\n"); qemu_log("\n");
@ -4266,7 +4266,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
#ifdef DEBUG_DISAS #ifdef DEBUG_DISAS
if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT) if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT)
&& qemu_log_in_addr_range(tb->pc))) { && qemu_log_in_addr_range(tb->pc))) {
FILE *logfile = qemu_log_lock(); FILE *logfile = qemu_log_trylock();
qemu_log("OP after optimization and liveness analysis:\n"); qemu_log("OP after optimization and liveness analysis:\n");
tcg_dump_ops(s, true); tcg_dump_ops(s, true);
qemu_log("\n"); qemu_log("\n");

View File

@ -162,7 +162,7 @@ static void test_logfile_lock(gconstpointer data)
* our handle remains valid for use due to RCU. * our handle remains valid for use due to RCU.
*/ */
qemu_set_log_filename(file_path, &error_abort); qemu_set_log_filename(file_path, &error_abort);
logfile = qemu_log_lock(); logfile = qemu_log_trylock();
g_assert(logfile); g_assert(logfile);
fprintf(logfile, "%s 1st write to file\n", __func__); fprintf(logfile, "%s 1st write to file\n", __func__);
fflush(logfile); fflush(logfile);

View File

@ -36,15 +36,17 @@ static GArray *debug_regions;
/* Lock/unlock output. */ /* Lock/unlock output. */
FILE *qemu_log_lock(void) FILE *qemu_log_trylock(void)
{ {
QemuLogFile *logfile; QemuLogFile *logfile;
rcu_read_lock(); rcu_read_lock();
logfile = qatomic_rcu_read(&qemu_logfile); logfile = qatomic_rcu_read(&qemu_logfile);
if (logfile) { if (logfile) {
qemu_flockfile(logfile->fd); qemu_flockfile(logfile->fd);
return logfile->fd; return logfile->fd;
} else { } else {
rcu_read_unlock();
return NULL; return NULL;
} }
} }
@ -53,8 +55,8 @@ void qemu_log_unlock(FILE *fd)
{ {
if (fd) { if (fd) {
qemu_funlockfile(fd); qemu_funlockfile(fd);
rcu_read_unlock();
} }
rcu_read_unlock();
} }
/* Return the number of characters emitted. */ /* Return the number of characters emitted. */