Skip to content

Commit

Permalink
replay: character devices
Browse files Browse the repository at this point in the history
This patch implements record and replay of character devices.
It records chardevs communication in replay mode. Recorded information
include data read from backend and counter of bytes written
from frontend to backend to preserve frontend internal state.
If character device was configured through the command line in record mode,
then in replay mode it should be also added to command line. Backend of
the character device could be changed in replay mode.
Replaying of devices that perform ioctl and get_msgfd operations is not
supported.
gdbstub which also acts as a backend is not recorded to allow controlling
the replaying through gdb. Monitor backends are also not recorded.

Signed-off-by: Pavel Dovgalyuk <[email protected]>
Message-Id: <20160314074436.4980.83856.stgit@PASHA-ISP>
[Add stubs. - Paolo]
Signed-off-by: Paolo Bonzini <[email protected]>
  • Loading branch information
Dovgalyuk authored and bonzini committed Mar 15, 2016
1 parent 39c350e commit 33577b4
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 30 deletions.
2 changes: 1 addition & 1 deletion gdbstub.c
Original file line number Diff line number Diff line change
Expand Up @@ -1752,7 +1752,7 @@ int gdbserver_start(const char *device)
sigaction(SIGINT, &act, NULL);
}
#endif
chr = qemu_chr_new("gdb", device, NULL);
chr = qemu_chr_new_noreplay("gdb", device, NULL);
if (!chr)
return -1;

Expand Down
26 changes: 26 additions & 0 deletions include/sysemu/char.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ struct CharDriverState {
int is_mux;
guint fd_in_tag;
QemuOpts *opts;
bool replay;
QTAILQ_ENTRY(CharDriverState) next;
};

Expand Down Expand Up @@ -138,6 +139,22 @@ void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend);
CharDriverState *qemu_chr_new(const char *label, const char *filename,
void (*init)(struct CharDriverState *s));

/**
* @qemu_chr_new_noreplay:
*
* Create a new character backend from a URI.
* Character device communications are not written
* into the replay log.
*
* @label the name of the backend
* @filename the URI
* @init not sure..
*
* Returns: a new character backend
*/
CharDriverState *qemu_chr_new_noreplay(const char *label, const char *filename,
void (*init)(struct CharDriverState *s));

/**
* @qemu_chr_delete:
*
Expand Down Expand Up @@ -341,6 +358,15 @@ int qemu_chr_be_can_write(CharDriverState *s);
*/
void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len);

/**
* @qemu_chr_be_write_impl:
*
* Implementation of back end writing. Used by replay module.
*
* @buf a buffer to receive data from the front end
* @len the number of bytes to receive from the front end
*/
void qemu_chr_be_write_impl(CharDriverState *s, uint8_t *buf, int len);

/**
* @qemu_chr_be_event:
Expand Down
17 changes: 17 additions & 0 deletions include/sysemu/replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,21 @@ void replay_input_event(QemuConsole *src, InputEvent *evt);
/*! Adds input sync event to the queue */
void replay_input_sync_event(void);

/* Character device */

/*! Registers char driver to save it's events */
void replay_register_char_driver(struct CharDriverState *chr);
/*! Saves write to char device event to the log */
void replay_chr_be_write(struct CharDriverState *s, uint8_t *buf, int len);
/*! Writes char write return value to the replay log. */
void replay_char_write_event_save(int res, int offset);
/*! Reads char write return value from the replay log. */
void replay_char_write_event_load(int *res, int *offset);
/*! Reads information about read_all character event. */
int replay_char_read_all_load(uint8_t *buf);
/*! Writes character read_all error code into the replay log. */
void replay_char_read_all_save_error(int res);
/*! Writes character read_all execution result into the replay log. */
void replay_char_read_all_save_buf(uint8_t *buf, int offset);

#endif
138 changes: 113 additions & 25 deletions qemu-char.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "io/channel-socket.h"
#include "io/channel-file.h"
#include "io/channel-tls.h"
#include "sysemu/replay.h"

#include <zlib.h>

Expand Down Expand Up @@ -234,10 +235,46 @@ static void qemu_chr_fe_write_log(CharDriverState *s,
}
}

static int qemu_chr_fe_write_buffer(CharDriverState *s, const uint8_t *buf, int len, int *offset)
{
int res = 0;
*offset = 0;

qemu_mutex_lock(&s->chr_write_lock);
while (*offset < len) {
do {
res = s->chr_write(s, buf + *offset, len - *offset);
if (res == -1 && errno == EAGAIN) {
g_usleep(100);
}
} while (res == -1 && errno == EAGAIN);

if (res <= 0) {
break;
}

*offset += res;
}
if (*offset > 0) {
qemu_chr_fe_write_log(s, buf, *offset);
}
qemu_mutex_unlock(&s->chr_write_lock);

return res;
}

int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
{
int ret;

if (s->replay && replay_mode == REPLAY_MODE_PLAY) {
int offset;
replay_char_write_event_load(&ret, &offset);
assert(offset <= len);
qemu_chr_fe_write_buffer(s, buf, offset, &offset);
return ret;
}

qemu_mutex_lock(&s->chr_write_lock);
ret = s->chr_write(s, buf, len);

Expand All @@ -246,34 +283,31 @@ int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len)
}

qemu_mutex_unlock(&s->chr_write_lock);

if (s->replay && replay_mode == REPLAY_MODE_RECORD) {
replay_char_write_event_save(ret, ret < 0 ? 0 : ret);
}

return ret;
}

int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len)
{
int offset = 0;
int res = 0;
int offset;
int res;

qemu_mutex_lock(&s->chr_write_lock);
while (offset < len) {
do {
res = s->chr_write(s, buf + offset, len - offset);
if (res == -1 && errno == EAGAIN) {
g_usleep(100);
}
} while (res == -1 && errno == EAGAIN);
if (s->replay && replay_mode == REPLAY_MODE_PLAY) {
replay_char_write_event_load(&res, &offset);
assert(offset <= len);
qemu_chr_fe_write_buffer(s, buf, offset, &offset);
return res;
}

if (res <= 0) {
break;
}
res = qemu_chr_fe_write_buffer(s, buf, len, &offset);

offset += res;
if (s->replay && replay_mode == REPLAY_MODE_RECORD) {
replay_char_write_event_save(res, offset);
}
if (offset > 0) {
qemu_chr_fe_write_log(s, buf, offset);
}

qemu_mutex_unlock(&s->chr_write_lock);

if (res < 0) {
return res;
Expand All @@ -289,6 +323,10 @@ int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
if (!s->chr_sync_read) {
return 0;
}

if (s->replay && replay_mode == REPLAY_MODE_PLAY) {
return replay_char_read_all_load(buf);
}

while (offset < len) {
do {
Expand All @@ -303,6 +341,9 @@ int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
}

if (res < 0) {
if (s->replay && replay_mode == REPLAY_MODE_RECORD) {
replay_char_read_all_save_error(res);
}
return res;
}

Expand All @@ -313,14 +354,22 @@ int qemu_chr_fe_read_all(CharDriverState *s, uint8_t *buf, int len)
}
}

if (s->replay && replay_mode == REPLAY_MODE_RECORD) {
replay_char_read_all_save_buf(buf, offset);
}
return offset;
}

int qemu_chr_fe_ioctl(CharDriverState *s, int cmd, void *arg)
{
if (!s->chr_ioctl)
return -ENOTSUP;
return s->chr_ioctl(s, cmd, arg);
int res;
if (!s->chr_ioctl || s->replay) {
res = -ENOTSUP;
} else {
res = s->chr_ioctl(s, cmd, arg);
}

return res;
}

int qemu_chr_be_can_write(CharDriverState *s)
Expand All @@ -330,17 +379,35 @@ int qemu_chr_be_can_write(CharDriverState *s)
return s->chr_can_read(s->handler_opaque);
}

void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
void qemu_chr_be_write_impl(CharDriverState *s, uint8_t *buf, int len)
{
if (s->chr_read) {
s->chr_read(s->handler_opaque, buf, len);
}
}

void qemu_chr_be_write(CharDriverState *s, uint8_t *buf, int len)
{
if (s->replay) {
if (replay_mode == REPLAY_MODE_PLAY) {
return;
}
replay_chr_be_write(s, buf, len);
} else {
qemu_chr_be_write_impl(s, buf, len);
}
}

int qemu_chr_fe_get_msgfd(CharDriverState *s)
{
int fd;
return (qemu_chr_fe_get_msgfds(s, &fd, 1) == 1) ? fd : -1;
int res = (qemu_chr_fe_get_msgfds(s, &fd, 1) == 1) ? fd : -1;
if (s->replay) {
fprintf(stderr,
"Replay: get msgfd is not supported for serial devices yet\n");
exit(1);
}
return res;
}

int qemu_chr_fe_get_msgfds(CharDriverState *s, int *fds, int len)
Expand Down Expand Up @@ -3821,7 +3888,8 @@ CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
return NULL;
}

CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
CharDriverState *qemu_chr_new_noreplay(const char *label, const char *filename,
void (*init)(struct CharDriverState *s))
{
const char *p;
CharDriverState *chr;
Expand All @@ -3847,6 +3915,21 @@ CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*in
return chr;
}

CharDriverState *qemu_chr_new(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
{
CharDriverState *chr;
chr = qemu_chr_new_noreplay(label, filename, init);
if (chr) {
chr->replay = replay_mode != REPLAY_MODE_NONE;
if (chr->replay && chr->chr_ioctl) {
fprintf(stderr,
"Replay: ioctl is not supported for serial devices yet\n");
}
replay_register_char_driver(chr);
}
return chr;
}

void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
{
if (chr->chr_set_echo) {
Expand Down Expand Up @@ -4455,6 +4538,11 @@ void qmp_chardev_remove(const char *id, Error **errp)
error_setg(errp, "Chardev '%s' is busy", id);
return;
}
if (chr->replay) {
error_setg(errp,
"Chardev '%s' cannot be unplugged in record/replay mode", id);
return;
}
qemu_chr_delete(chr);
}

Expand Down
1 change: 1 addition & 0 deletions replay/Makefile.objs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ common-obj-y += replay-internal.o
common-obj-y += replay-events.o
common-obj-y += replay-time.o
common-obj-y += replay-input.o
common-obj-y += replay-char.o
Loading

0 comments on commit 33577b4

Please sign in to comment.