Skip to content

Commit

Permalink
trace/simple: Fix unauthorized enable
Browse files Browse the repository at this point in the history
st_set_trace_file() accidentally enables tracing.  It's called
unconditionally during startup, which is why QEMU built with the
simple trace backend always writes a trace file "trace-$PID".

This has been broken for quite a while.  I didn't track down the exact
commit.

Fix st_set_trace_file() to restore the state.

Signed-off-by: Markus Armbruster <[email protected]>
Reviewed-by: Philippe Mathieu-Daudé <[email protected]>
Message-id: [email protected]
Signed-off-by: Stefan Hajnoczi <[email protected]>
  • Loading branch information
Markus Armbruster authored and stefanhaRH committed Jun 24, 2020
1 parent f892b49 commit db25d56
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions trace/simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,17 @@ static int st_write_event_mapping(void)
return 0;
}

void st_set_trace_file_enabled(bool enable)
/**
* Enable / disable tracing, return whether it was enabled.
*
* @enable: enable if %true, else disable.
*/
bool st_set_trace_file_enabled(bool enable)
{
bool was_enabled = trace_fp;

if (enable == !!trace_fp) {
return; /* no change */
return was_enabled; /* no change */
}

/* Halt trace writeout */
Expand All @@ -323,14 +330,14 @@ void st_set_trace_file_enabled(bool enable)

trace_fp = fopen(trace_file_name, "wb");
if (!trace_fp) {
return;
return was_enabled;
}

if (fwrite(&header, sizeof header, 1, trace_fp) != 1 ||
st_write_event_mapping() < 0) {
fclose(trace_fp);
trace_fp = NULL;
return;
return was_enabled;
}

/* Resume trace writeout */
Expand All @@ -340,6 +347,7 @@ void st_set_trace_file_enabled(bool enable)
fclose(trace_fp);
trace_fp = NULL;
}
return was_enabled;
}

/**
Expand All @@ -350,7 +358,7 @@ void st_set_trace_file_enabled(bool enable)
*/
void st_set_trace_file(const char *file)
{
st_set_trace_file_enabled(false);
bool saved_enable = st_set_trace_file_enabled(false);

g_free(trace_file_name);

Expand All @@ -361,7 +369,7 @@ void st_set_trace_file(const char *file)
trace_file_name = g_strdup_printf("%s", file);
}

st_set_trace_file_enabled(true);
st_set_trace_file_enabled(saved_enable);
}

void st_print_trace_file_status(void)
Expand Down
2 changes: 1 addition & 1 deletion trace/simple.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define TRACE_SIMPLE_H

void st_print_trace_file_status(void);
void st_set_trace_file_enabled(bool enable);
bool st_set_trace_file_enabled(bool enable);
void st_set_trace_file(const char *file);
bool st_init(void);
void st_flush_trace_buffer(void);
Expand Down

0 comments on commit db25d56

Please sign in to comment.