Deterministic replay core functions
RFC PATCH 19/22 Deterministic replay core functions
Functions for writing and reading replay log. These functions are used to record the following events:
* Output of the time() function
* Sound in and sound out events in winaudio
* Hardware interrupts and exceptions
* Special replay debug assert
* Target time information used for reviewing the events log
* Start and finish of saving VM
* Synchronous data from some module
* Execution of the group of the instructions
* Hardware clock read (real ticks, host clock, virtual clock)
* Asynchronous events:
- Setting event notifier
- Network packet
- Worker thread invocation
- BH execution
- USB packets
- Serial port input
- Mouse and keyboard input
* Checkpoint in the code (used to synchronize the events)
* System shutdown
This module also sets timer for periodical snapshotting of the system state, when it is required by user.
---
replay/Makefile.objs new file +1 -0
@@ -0,0 +1 @@ +obj-y += replay.o replay-events.o replay-internal.o replay-usb.o replay-icount.o replay-audio.o
replay-char.o replay-debug.o replay-input.o replay-net.o
replay/replay-audio.c new file +238 -0
238 lines not on this page · raw message
replay/replay-char.c new file +119 -0
119 lines not on this page · raw message
replay/replay-debug.c new file +152 -0
152 lines not on this page · raw message
replay/replay-events.c new file +47 -0
47 lines not on this page · raw message
*)event->opaque2);
+ break;
+ case REPLAY_ASYNC_EVENT_BH:
+ aio_bh_call(event->opaque);
+ break;
+#ifdef CONFIG_USB_LIBUSB
+ case REPLAY_ASYNC_EVENT_USB_CTRL:
+ replay_event_usb_ctrl(event->opaque);
+ break;
+ case REPLAY_ASYNC_EVENT_USB_DATA:
+ replay_event_usb_data(event->opaque);
+ break;
+ case REPLAY_ASYNC_EVENT_USB_ISO:
+ replay_event_usb_iso(event->opaque);
+ break;
+#endif
+ case REPLAY_ASYNC_EVENT_CHAR:
+ replay_event_char_run(event->opaque);
+ break;
+ case REPLAY_ASYNC_EVENT_INPUT:
+ qemu_input_event_send_impl(NULL, (InputEvent*)event->opaque);
+ // Using local variables, when replaying. Do not free them.
+ if (replay_mode == REPLAY_SAVE)
+ {+ qapi_free_InputEvent((InputEvent*)event->opaque);
+ }
+ break;
+ case REPLAY_ASYNC_EVENT_INPUT_SYNC:
+ qemu_input_event_sync_impl();
+ break;
+ default:
+ fprintf(stderr, "Replay: invalid async event ID (%d) in the queue\n", event->event_id); + exit(1); + break;
+ }
+}
+
+void replay_enable_events(void)
+{
+ replay_events_enabled = true;
+}
+
+void replay_flush_events(void)
+{
+ qemu_mutex_lock(&lock);
+ while (!QTAILQ_EMPTY(&events_list))
+ {+ Event *event = QTAILQ_FIRST(&events_list);
+
+ // perform an action
+ replay_run_event(event);
+
+ // go to next event
+ QTAILQ_REMOVE(&events_list, event, events);
+
+ g_free(event);
+ }
+ qemu_mutex_unlock(&lock);
+}
+
+void replay_disable_events(void)
+{
+ replay_events_enabled = false; + // Flush events queue before waiting of completion + replay_flush_events();
+}
+
+void replay_clear_events(void)
+{
+ qemu_mutex_lock(&lock);
+ while (!QTAILQ_EMPTY(&events_list))
+ {
+ Event *event = QTAILQ_FIRST(&events_list);
+ // go to next event
+ QTAILQ_REMOVE(&events_list, event, events);+
+ g_free(event);
+ }
+ qemu_mutex_unlock(&lock);
+}
+
+static void replay_add_event_internal(unsigned int event_id, void *opaque, void *opaque2, uint64_t id)
+{
+ if (event_id >= REPLAY_ASYNC_COUNT)
+ {
+ fprintf(stderr, "Replay: invalid async event ID (%d)\n", event_id);
+ exit(1);
+ }
+ if (!replay_file || replay_mode == REPLAY_NONE
+ || !replay_events_enabled
+ || replay_get_play_submode() == REPLAY_PLAY_CHANGED)
+ {
+ Event e;
+ e.event_id = event_id;
+ e.opaque = opaque;
+ e.opaque2 = opaque2;
+ e.id = id;
+ replay_run_event(&e);
+ return;
+ }
+
+ Event *event = g_malloc0(sizeof(Event));
+ event->event_id = event_id;
+ event->opaque = opaque;
+ event->opaque2 = opaque2;
+ event->id = id;
+
+ qemu_mutex_lock(&lock);
+ QTAILQ_INSERT_TAIL(&events_list, event, events);
+ qemu_mutex_unlock(&lock);+}
+
+void replay_add_event(unsigned int event_id, void *opaque)
+{
+ replay_add_event_internal(event_id, opaque, NULL, 0);
+}
+
+void replay_add_usb_event(unsigned int event_id, uint64_t id, void *opaque) +{
+ replay_add_event_internal(event_id, opaque, NULL, id);
+}
+
+void replay_add_bh_event(void *bh, uint64_t id)
+{
+ replay_add_event_internal(REPLAY_ASYNC_EVENT_BH, bh, NULL, id);
+}
+
+void replay_add_event2(unsigned int event_id, void *opaque, void *opaque2) +{
+ replay_add_event_internal(event_id, opaque, opaque2, 0);
+}
+
+void replay_add_thread_event(void *opaque, void *opaque2, uint64_t id) +{
+ replay_add_event_internal(REPLAY_ASYNC_EVENT_THREAD, opaque, opaque2, id);
+}
+
+void replay_save_events(int opt)
+{
+ qemu_mutex_lock(&lock);
+ while (!QTAILQ_EMPTY(&events_list))
+ {
+ Event *event = QTAILQ_FIRST(&events_list);
+ if (replay_mode != REPLAY_PLAY)
+ {
+ // put the event into the file
+ if (opt == -1)
+ {
+ replay_put_event(EVENT_ASYNC);
+ }
+ else
+ {+ replay_put_event(EVENT_ASYNC_OPT);
+ replay_put_byte(opt);
+ }
+ replay_put_dword(event->event_id);
+
+ // save event-specific data
+ switch (event->event_id)
+ {
+ case REPLAY_ASYNC_EVENT_NETWORK:+ replay_net_save_packet(event->opaque);
+ break;
+#ifdef CONFIG_USB_LIBUSB
+ case REPLAY_ASYNC_EVENT_USB_CTRL:
+ case REPLAY_ASYNC_EVENT_USB_DATA:
+ replay_put_qword(event->id); + replay_event_save_usb_xfer(event->opaque); + break; + case REPLAY_ASYNC_EVENT_USB_ISO: + replay_put_qword(event->id); + replay_event_save_usb_iso_xfer(event->opaque); + break;
+#endif
+ case REPLAY_ASYNC_EVENT_THREAD:
+ case REPLAY_ASYNC_EVENT_BH:
+ replay_put_qword(event->id);
+ break;
+ case REPLAY_ASYNC_EVENT_CHAR:
+ replay_event_char_save(event->opaque);
+ break;
+ case REPLAY_ASYNC_EVENT_INPUT:
+ replay_save_input_event(event->opaque);
+ break;
+ }
+ }
+
+ // perform an action
+ replay_run_event(event);
+
+ // go to next event
+ QTAILQ_REMOVE(&events_list, event, events);
+
+ g_free(event);
+ }
+ qemu_mutex_unlock(&lock);
+}
+
+bool replay_has_events(void)
+{
+ return !QTAILQ_EMPTY(&events_list);
+}
+
+
+void replay_read_events(int opt)
+{
+ replay_fetch_data_kind();
+ while ((opt == -1 && replay_data_kind == EVENT_ASYNC)
+ || (opt != -1 && replay_data_kind == EVENT_ASYNC_OPT))
+ {
+ if (read_event_id == -1)
+ {
+ if (opt != -1)
+ {
+ read_opt = replay_get_byte();
+ } + read_event_id = replay_get_dword(); + read_bh_id = -1; + replay_check_error();
+ }
+
+ if (opt != read_opt)
+ break;
+ // Execute some events without searching them in the queue
+ Event e;
+ switch (read_event_id)
+ {
+ case REPLAY_ASYNC_EVENT_NOTIFIER:
+ // Nothing to read
+ break;
+ case REPLAY_ASYNC_EVENT_NETWORK:
+ e.opaque = replay_net_read_packet();
+ e.event_id = read_event_id;
+ replay_run_event(&e);
+
+ replay_has_unread_data = 0;
+ read_event_id = -1;
+ read_opt = -1;
+ replay_fetch_data_kind();
+ // continue with the next event
+ continue;+ case REPLAY_ASYNC_EVENT_THREAD:
+ case REPLAY_ASYNC_EVENT_BH:
+ if (read_bh_id == -1)
+ {
+ read_bh_id = replay_get_qword();
+ }
+ break;
+#ifdef CONFIG_USB_LIBUSB
+ case REPLAY_ASYNC_EVENT_USB_CTRL:
+ case REPLAY_ASYNC_EVENT_USB_DATA:
+ case REPLAY_ASYNC_EVENT_USB_ISO:
+ if (read_bh_id == -1)
+ {
+ read_bh_id = replay_get_qword();
+ }
+ // read after finding event in the list
+ break;+#endif
+ case REPLAY_ASYNC_EVENT_CHAR:
+ e.event_id = read_event_id;
+ e.opaque = replay_event_char_read();
+
+ replay_has_unread_data = 0; + read_event_id = -1; + read_opt = -1; + replay_fetch_data_kind(); + + replay_run_event(&e); + // continue with the next event + continue;
+ case REPLAY_ASYNC_EVENT_INPUT:
+ e.event_id = read_event_id;
+ e.opaque = replay_read_input_event();
+
+ replay_run_event(&e);
+
+ replay_has_unread_data = 0; + read_event_id = -1; + read_opt = -1; + replay_fetch_data_kind(); + // continue with the next event + continue; + case REPLAY_ASYNC_EVENT_INPUT_SYNC: + e.event_id = read_event_id; + e.opaque = 0; + replay_run_event(&e); + + replay_has_unread_data = 0; + read_event_id = -1; + read_opt = -1; + replay_fetch_data_kind(); + // continue with the next event + continue; + default: + fprintf(stderr, "Unknown ID %d of replay event\n", read_event_id); + exit(1); + break;
+ }
+
+ qemu_mutex_lock(&lock);
+
+ Event *event = NULL;
+ Event *curr = NULL;
+ QTAILQ_FOREACH(curr, &events_list, events)
+ {+ if (curr->event_id == read_event_id
+ && (read_bh_id == -1 || read_bh_id == curr->id))
+ {
+ event = curr;
+ break;
+ }
+ }
+
+ if (event)
+ {
+ // continue reading data
+#ifdef CONFIG_USB_LIBUSB
+ switch (read_event_id)
+ {
+ case REPLAY_ASYNC_EVENT_USB_CTRL:
+ case REPLAY_ASYNC_EVENT_USB_DATA:+ replay_event_read_usb_xfer(event->opaque);
+ break;
+ case REPLAY_ASYNC_EVENT_USB_ISO:
+ replay_event_read_usb_iso_xfer(event->opaque);
+ break;
+ }
+#endif
+
+ QTAILQ_REMOVE(&events_list, event, events);
+
+ qemu_mutex_unlock(&lock);
+
+ // reset unread data and other parameters to allow + // reading other data from the log while + // running the event + replay_has_unread_data = 0; + read_event_id = -1; + read_bh_id = -1; + read_opt = -1;
+
+ replay_run_event(event);
+ g_free(event);
+
+ replay_fetch_data_kind();
+ }
+ else
+ {
+ qemu_mutex_unlock(&lock);
+ // No such event found in the queue
+ break;+ }
+ }
+}
+
+
+void replay_init_events(void)
+{
+ read_event_id = -1;
+ qemu_mutex_init(&lock);
+}
+
+
+void replay_finish_events(void)
+{
+ replay_events_enabled = false; + replay_clear_events(); + qemu_mutex_destroy(&lock);
+}
+
+
+void replay_skip_async_event(struct Monitor *mon, uint64_t step, bool opt) +{
+ if (opt)
+ {
+ replay_get_byte();
+ }
+
+ int id = replay_get_dword();
+ switch (id)
+ {
+ case REPLAY_ASYNC_EVENT_NOTIFIER:+ break;
+ case REPLAY_ASYNC_EVENT_NETWORK:
+ replay_net_skip_packet(mon, step);
+ break;
+ case REPLAY_ASYNC_EVENT_THREAD:
+ case REPLAY_ASYNC_EVENT_BH:
+ fseek(replay_file, sizeof(uint64_t), SEEK_CUR);
+ break;
+#ifdef CONFIG_USB_LIBUSB
+ case REPLAY_ASYNC_EVENT_USB_CTRL:
+ case REPLAY_ASYNC_EVENT_USB_DATA:
+ replay_get_qword(); + replay_event_skip_usb_xfer(); + break; + case REPLAY_ASYNC_EVENT_USB_ISO: + replay_get_qword(); + replay_event_skip_usb_iso_xfer(); + break;
+#endif
+ case REPLAY_ASYNC_EVENT_CHAR:
+ replay_event_char_skip();
+ break;
+ case REPLAY_ASYNC_EVENT_INPUT:
+ replay_skip_input_event(mon, step);
+ break;
+ case REPLAY_ASYNC_EVENT_INPUT_SYNC:
+ break;
+ default:
+ fprintf(stderr, "Unknown ID %d of replay event found while skipping\n", read_event_id); + exit(1); + break;
+ }
+}
+
replay/replay-icount.c new file +112 -0
112 lines not on this page · raw message
replay/replay-input.c new file +117 -0
117 lines not on this page · raw message
"pressed" : "released");
+ }
+ break;
+ case KEY_VALUE_KIND_QCODE:
+ {
+ int qcode = replay_get_dword(); + bool down = replay_get_byte(); + monitor_printf(mon, "%" PRId64 ": Key %d %s\n", step, qcode, down ? "pressed" :
"released");
+ }
+ break;
+ case KEY_VALUE_KIND_MAX:
+ /* keep gcc happy */
+ break;
+ }
+ break;
+ case INPUT_EVENT_KIND_BTN:
+ {
+ int btn = replay_get_dword(); + bool down = replay_get_byte(); + monitor_printf(mon, "%" PRId64 ": Mouse buttons %d %s\n", step, btn, down ? "pressed" :
"released");
+ }
+ break;
+ case INPUT_EVENT_KIND_REL:
+ replay_get_dword(); + replay_get_qword(); + break; + case INPUT_EVENT_KIND_ABS: + replay_get_dword(); + replay_get_qword(); + break;
+ case INPUT_EVENT_KIND_MAX:
+ /* keep gcc happy */
+ break;
+ }
+}
+
replay/replay-internal.c new file +176 -0
176 lines not on this page · raw message
replay/replay-internal.h new file +192 -0
192 lines not on this page · raw message
replay/replay-net.c new file +75 -0
75 lines not on this page · raw message
net_packets_count)
+ {
+ fprintf(stderr, "Internal error in replay_net_read_packets_data\n");
+ exit(1);
+ }
+ }
+}
+
+void replay_net_write_packets_data(void)
+{
+ // write network packets info
+ replay_put_qword(net_packets_count);
+ if (net_packets && net_packets_count)
+ {+ // TODO ??? Save distinct fields in the loop
+ fwrite(net_packets, sizeof(NetPacketInfo), net_packets_count, replay_file);
+ }
+}
+
+void replay_add_network_client(NetClientState *nc) +{
+ if (vlan_states_count == 0)
+ {
+ vlan_states = (NetClientState **)g_malloc(sizeof(*vlan_states));
+ vlan_states_count = 0;
+ vlan_states_capacity = 1;
+ }
+ else if (vlan_states_count == vlan_states_capacity)
+ {+ vlan_states_capacity *= 2;
+ vlan_states = (NetClientState **)g_realloc(vlan_states, sizeof(*vlan_states) *vlan_states_capacity);
+ }
+
+ vlan_states[vlan_states_count++] = nc;
+}
+
+void replay_net_free(void)
+{
+ // free modified packets
+ ModifiedNetPacketInfo *packet, *nextPacket;
+ QLIST_FOREACH_SAFE(packet, &modified_net_packets, next, nextPacket)
+ {
+ QLIST_REMOVE(packet, next);
+ g_free(packet->buf);
+ g_free(packet);+ }
+
+ if (vlan_states)
+ {
+ g_free(vlan_states);
+ vlan_states = NULL;
+ }
+}
+
+
+//! Saves network packet into the log.
+void replay_save_net_packet(NetClientState *nc, const uint8_t *buf, size_t size) +{
+ if (replay_file)
+ {
+ // save packet info into the array
+ if (net_packets_capacity == net_packets_count)
+ {+ if (net_packets_capacity == 0)
+ net_packets_capacity = 1;
+ else
+ net_packets_capacity *= 2;
+ net_packets = (NetPacketInfo*)g_realloc(net_packets, net_packets_capacity *
sizeof(NetPacketInfo));
+ }
+
+ // add packet processing event to the queue
+ NetPacketQueue *p = (NetPacketQueue*)g_malloc0(sizeof(NetPacketQueue));
+ p->id = net_packets_count;
+ p->size = size;
+ if (net_hub_id_for_client(nc, &p->nc_id) < 0)
+ {
+ fprintf(stderr, "Replay: Cannot determine net client id\n");
+ exit(1);
+ }
+ memcpy(p->buf, buf, size);
+ replay_add_event(REPLAY_ASYNC_EVENT_NETWORK, p);+
+ ++net_packets_count;
+ }
+}
+
+
+static ModifiedNetPacketInfo *find_modified_packet(int64_t id) +{
+ ModifiedNetPacketInfo *packet = NULL;
+ QLIST_FOREACH(packet, &modified_net_packets, next)
+ {+ if (packet->id == id)
+ return packet;
+ }
+
+ return NULL;
+}
+
+static NetClientState *replay_net_find_vlan(NetPacketQueue *packet) +{
+ int i;
+ for (i = 0 ; i < vlan_states_count ; ++i)
+ {
+ int id = 0;
+ if (net_hub_id_for_client(vlan_states[i], &id) < 0)
+ {+ fprintf(stderr, "Replay: Cannot determine net client id\n");
+ exit(1);
+ }
+ if (id == packet->nc_id)
+ return vlan_states[i];
+ }
+
+ fprintf(stderr, "Replay: please specify -net replay command-line option\n");
+ exit(1);
+
+ return NULL;
+}
+
+void replay_net_send_packet(void *opaque)
+{
+ NetPacketQueue *packet = (NetPacketQueue *)opaque;
+ NetClientState *vlan_state = replay_net_find_vlan(packet);
+
+ if (replay_mode == REPLAY_SAVE)
+ {
+ net_packets[packet->id].file_offset = packet->offset;
+ net_packets[packet->id].step = replay_get_current_step();
+
+ qemu_send_packet(vlan_state, packet->buf, packet->size);
+ }
+ else if (replay_mode == REPLAY_PLAY)
+ {
+ // fuzzing or not
+ ModifiedNetPacketInfo *mod_packet = find_modified_packet(packet->id);
+ if (mod_packet)
+ {
+ replay_set_play_changed();
+ qemu_send_packet(vlan_state, mod_packet->buf, mod_packet->size);
+ }
+ else
+ {+ qemu_send_packet(vlan_state, packet->buf, packet->size);
+ }
+ }
+
+ g_free(packet);
+}
+
+int replay_get_packet(struct Monitor *mon, int64_t id) +{
+ if (replay_mode == REPLAY_PLAY && replay_file)
+ {
+ if (id < 0 || id >= net_packets_count)
+ return 0;
+
+ uint64_t offset = ftello64(replay_file);
+
+ size_t size;
+ // find the packet
+ fseeko64(replay_file, net_packets[id].file_offset, SEEK_SET);
+ // skip the id and nc_id
+ fseek(replay_file, sizeof(uint64_t), SEEK_CUR);
+ fseek(replay_file, sizeof(uint32_t), SEEK_CUR);
+
+ monitor_printf(mon, "Packet: ");
+ // read the packet
+ if (fread(&size, sizeof(size), 1, replay_file) != 1)
+ {
+ fprintf(stderr, "Internal error in replay_get_packet\n");
+ exit(1);
+ }
+ while (size--)
+ {
+ uint8_t byte;
+ if (fread(&byte, sizeof(byte), 1, replay_file) != 1)
+ {
+ fprintf(stderr, "Internal error in replay_get_packet\n");
+ exit(1);
+ }
+ //monitor_printf(mon, "%02x", byte);
+ monitor_print_bytes(mon, &byte, sizeof(byte));+ }
+ monitor_printf(mon, "#\n\n");
+
+ // restore original position
+ fseeko64(replay_file, offset, SEEK_SET);
+
+ //**************************** printing modified packet
+ ModifiedNetPacketInfo *packet = find_modified_packet(id);
+ if (packet != NULL)
+ {
+ monitor_printf(mon, "Modified packet: ");
+ size_t i = 0;
+ for ( ; i < packet->size ; ++i)+ monitor_print_bytes(mon, &packet->buf[i], sizeof(uint8_t));
+
+ monitor_printf(mon, "#\n\n");
+ }
+ //****************************
+
+ return 1;
+ }
+
+ return 0;
+}
+
+
+int replay_set_packet(struct Monitor *mon, int64_t id, const char *bytes) +{
+ if (replay_mode == REPLAY_PLAY && replay_file)
+ {
+ if (bytes == NULL)
+ {
+ // remove packet
+ ModifiedNetPacketInfo *packet = find_modified_packet(id);
+ if (packet == NULL)
+ return 0;
+
+ QLIST_REMOVE(packet, next);
+ g_free(packet->buf);
+ g_free(packet);
+
+ return 1;
+ }
+ else
+ {
+ ModifiedNetPacketInfo *packet = find_modified_packet(id);
+ if (!packet)
+ {
+ // add packet
+ packet = (ModifiedNetPacketInfo *)g_malloc(sizeof(*packet));
+ QLIST_INSERT_HEAD(&modified_net_packets, packet, next);
+ packet->id = id;
+ }
+ else
+ {
+ g_free(packet->buf);
+ }
+
+ packet->size = strlen(bytes) / 2;
+ packet->buf = g_malloc(packet->size);
+ size_t i = 0;
+ for ( ; i < packet->size ; ++i)
+ {+ packet->buf[i] = replay_xtoi(bytes[2 * i]) * 16 + replay_xtoi(bytes[2 * i + 1]);
+ }
+
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+void replay_net_save_packet(void *opaque)
+{
+ NetPacketQueue *p = (NetPacketQueue*)opaque; + p->offset = ftello64(replay_file); + replay_put_qword(p->id); + replay_put_dword(p->nc_id); + replay_put_array(p->buf, p->size); + replay_network_packets += p->size;
+}
+
+void *replay_net_read_packet(void)
+{
+ NetPacketQueue *p = g_malloc0(sizeof(NetPacketQueue));; + p->id = replay_get_qword(); + p->nc_id = replay_get_dword(); + replay_get_array(p->buf, &p->size); + replay_check_error();
+
+ return p;
+}
+
+void replay_net_skip_packet(struct Monitor *mon, uint64_t step) +{
+ uint64_t id;
+ int nc_id;
+ size_t size;
+ if (fread(&id, sizeof(id), 1, replay_file) != 1
+ || fread(&nc_id, sizeof(nc_id), 1, replay_file) != 1
+ || fread(&size, sizeof(size), 1, replay_file) != 1)
+ {
+ fprintf(stderr, "Internal error while skipping network packet\n");
+ exit(1);
+ }
+ fseek(replay_file, size, SEEK_CUR);
+ monitor_printf(mon, "%" PRId64 ": Net packet net=%d id=%" PRId64 "\n", step, nc_id, id);+}
+
replay/replay-usb.c new file +104 -0
104 lines not on this page · raw message
size\n");
+ exit(1);
+ }
+ }
+}
+
+void replay_event_read_usb_iso_xfer(void *opaque) +{
+ struct libusb_transfer *xfer = opaque;
+ USBHostIsoXfer *iso = xfer->user_data;
+ int i;
+
+ if (replay_mode == REPLAY_PLAY)
+ {
+ bool in = iso->ring->ep->pid == USB_TOKEN_IN;
+ // read data here
+ xfer->status = replay_get_dword();
+ xfer->num_iso_packets = replay_get_dword();
+ for (i = 0 ; i < xfer->num_iso_packets ; ++i)
+ {
+ // all other fields of the packet are not used
+ //unsigned int len = replay_get_dword();
+ if (in)
+ {
+ size_t sz;
+ replay_get_array(usb_host_get_iso_packet_buffer(iso, i), &sz);
+ xfer->iso_packet_desc[i].actual_length = (unsigned int)sz;+ }
+ }
+ }
+}
+
+void replay_event_skip_usb_xfer(void)
+{
+ if (replay_mode == REPLAY_PLAY)
+ {
+ size_t sz;
+ // data/ctrl
+ replay_get_dword();
+ replay_get_dword();
+ if (fread(&sz, sizeof(sz), 1, replay_file) != 1)
+ {+ fprintf(stderr, "Internal error while skipping usb xfer\n");
+ exit(1);
+ }
+ fseek(replay_file, sz, SEEK_CUR);
+ }
+}
+
+void replay_event_skip_usb_iso_xfer(void)
+{
+ if (replay_mode == REPLAY_PLAY)
+ {
+ // status
+ replay_get_dword();
+ // count
+ int num = replay_get_dword();
+ // packets
+ while (num--)
+ {
+ size_t sz;
+ if (fread(&sz, sizeof(sz), 1, replay_file) != 1)
+ {+ fprintf(stderr, "Internal error while skipping usb iso xfer\n");
+ exit(1);
+ }
+ fseek(replay_file, sz, SEEK_CUR);
+ }
+ }
+}
+
+
+void replay_req_complete_data(struct libusb_transfer *xfer) +{
+ if (replay_mode == REPLAY_SAVE)
+ {
+ replay_add_usb_event(REPLAY_ASYNC_EVENT_USB_DATA,
+ replay_get_xfer_id(xfer), xfer);
+ }
+}
+
+void replay_req_register_data(struct libusb_transfer *xfer) +{
+ if (replay_mode == REPLAY_PLAY)
+ {
+ replay_add_usb_event(REPLAY_ASYNC_EVENT_USB_DATA,
+ replay_get_xfer_id(xfer), xfer);
+ }
+}
+
+
+void replay_event_usb_data(void *opaque)
+{
+ struct libusb_transfer *xfer = opaque;
+
+ usb_host_req_complete_data(xfer);
+}
+
+
+void replay_req_complete_iso(struct libusb_transfer *xfer) +{
+ if (replay_mode == REPLAY_SAVE)
+ {
+ replay_add_usb_event(REPLAY_ASYNC_EVENT_USB_ISO,
+ replay_get_iso_xfer_id(xfer), xfer);
+ }
+}
+
+void replay_req_register_iso(struct libusb_transfer *xfer) +{
+ if (replay_mode == REPLAY_PLAY)
+ {
+ USBHostIsoXfer *r = xfer->user_data;
+ USBHostDevice *s = r->ring->host;+
+ replay_add_usb_event(REPLAY_ASYNC_EVENT_USB_ISO,
+ replay_get_iso_xfer_id(xfer), xfer);
+ }
+}
+
+
+void replay_event_usb_iso(void *opaque)
+{
+ struct libusb_transfer *xfer = opaque;
+
+ usb_host_req_complete_iso(xfer);
+}
+
+#endif
+
+bool replay_usb_has_xfers(void)
+{
+#ifdef CONFIG_USB_LIBUSB
+ return usb_host_has_xfers();
+#else
+ return false;
+#endif
+}
replay/replay.c new file +431 -0
431 lines not on this page · raw message
replay_change.\n");
+ return;
+ }
+ else
+ {+ fprintf(stderr, "Cannot start replay_change\n");
+ exit(1);
+ }
+}
+
+
+
+//! Closes replay file.
+void replay_finish(void)
+{
+ if (replay_mode == REPLAY_NONE)
+ return;
+ replay_save_instructions();
+
+ // finalize the file
+ if (replay_file)
+ {
+ if (replay_mode == REPLAY_SAVE)
+ {
+ uint64_t offset;
+ // write end event
+ replay_put_event(EVENT_END);
+
+ // write states table
+ offset = ftello64(replay_file);
+ replay_put_qword(saved_states_count);
+ if (saved_states && saved_states_count)
+ {+ // TODO ??? Save distinct fields in the loop
+ fwrite(saved_states, sizeof(SavedStateInfo), saved_states_count, replay_file);
+ }
+ replay_net_write_packets_data();
+
+ // write header + fseek(replay_file, 0, SEEK_SET); + replay_put_dword(REPLAY_VERSION); + replay_put_qword(offset); + } + + // close file + fclose(replay_file); + replay_file = NULL;
+ }
+
+ replay_net_free();
+
+ // deallocate everything
+ if (save_timer)
+ {
+ timer_del(save_timer);
+ timer_free(save_timer);
+ save_timer = NULL;
+ }
+ if (target_timer)
+ {
+ timer_del(target_timer);
+ timer_free(target_timer);
+ target_timer = NULL;
+ }
+ if (saved_states)
+ {+ g_free(saved_states);
+ saved_states = NULL;
+ }
+
+ if (replay_filename)
+ {
+ g_free(replay_filename);
+ replay_filename = NULL;
+ }
+
+ if (replay_image_suffix)
+ {
+ g_free(replay_image_suffix);
+ replay_image_suffix = NULL;
+ }
+
+ replay_finish_events();
+}
+
+
+
+//! Saves the value of the desired clock into the output file. +void replay_save_clock(unsigned int kind, int64_t clock)
+{
+ replay_save_instructions();
+
+ if (kind >= REPLAY_CLOCK_COUNT)
+ {
+ fprintf(stderr, "invalid clock ID %d for replay\n", kind);
+ exit(1);
+ }
+
+ if (replay_file)
+ {
+ replay_put_event(EVENT_CLOCK + kind);
+ replay_put_qword(clock);
+ }
+}
+
+
+//! Reads next clock value from the file.
+//! If clock kind read from the file is different from the parameter, +//! the value is not used.
+//! If the parameter is -1, the clock value is read to the cache anyway. +static void replay_read_next_clock(unsigned int kind)
+{
+ replay_fetch_data_kind();
+ if (replay_file)
+ {+ unsigned int read_kind = replay_data_kind - EVENT_CLOCK;
+
+ if (kind != -1 && read_kind != kind)
+ {
+ return;
+ }
+ if (read_kind >= REPLAY_CLOCK_COUNT)
+ {+ fprintf(stderr, "invalid clock ID %d was read from replay\n", read_kind);
+ exit(1);
+ }
+
+ int64_t clock = replay_get_qword();
+
+ replay_check_error();
+ replay_has_unread_data = 0;
+
+ replay_state.cached_clock[read_kind] = clock;
+ }
+}
+
+
+//! Checks SAVEVM event while reading event log.
+static void check_savevm(void)
+{
+ replay_fetch_data_kind();
+ if (replay_data_kind != EVENT_SAVE_VM_BEGIN && replay_data_kind != EVENT_SAVE_VM_END)
+ {+ fprintf(stderr, "read wrong data kind %d within savevm\n", replay_data_kind);
+ exit(1);
+ }
+ replay_has_unread_data = 0;
+}
+
+//! Skips clock events saved to file while saving the VM state. +static void replay_skip_savevm(void)
+{
+ char name[128];
+
+ replay_has_unread_data = 0;
+
+ if (runstate_is_running())
+ {
+ vm_stop(RUN_STATE_RESTORE_VM);
+ }
+ else
+ {
+ // Read saved timers from event log
+ cpu_disable_ticks();
+ // Read checkpoints
+ while (replay_checkpoint(8)) {+ /* Nothing */
+ }
+ }
+
+ ++current_saved_state; + sprintf(name, SAVED_STATE_NAME_FORMAT, current_saved_state); + load_vmstate(name);
+
+ // check the closing event
+ check_savevm();
+
+ tb_flush_all();
+
+ // Read saved timers from event log
+ cpu_enable_ticks();
+
+ vm_start();
+}
+
+//! Skips async events until some sync event will be found. +static int skip_async_events(int stop_event)
+{
+ /* nothing to skip - not all instructions used
+ or timer is not processed yet*/
+ if (first_cpu != NULL && first_cpu->instructions_count != 0 && replay_has_unread_data)
+ {
+ return 0;
+ }
+
+ int skipping = 1;
+ while (skipping)
+ {
+ replay_fetch_data_kind();
+ if (stop_event == replay_data_kind)
+ skipping = 0;
+ switch (replay_data_kind)
+ {
+ case EVENT_END:
+ fprintf(stderr, "Replay log file was successfully played\n");
+ exit(1);
+ break;+ case EVENT_SOUND_OUT:
+#ifdef _WIN32
+ replay_read_sound_out();
+#endif
+ break;
+ case EVENT_SOUND_IN:
+#ifdef _WIN32
+ replay_read_sound_in();
+#endif
+ break;
+ case EVENT_SAVE_VM_BEGIN:
+ // cannot correctly load VM while in CPU thread
+ if (qemu_in_vcpu_thread())
+ {
+ skipping = 0;
+ break;
+ }
+ replay_skip_savevm();
+ break;
+ case EVENT_SHUTDOWN:
+ replay_has_unread_data = 0;
+ qemu_system_shutdown_request_impl();
+ break;
+ case EVENT_TARGET_TIME:
+ replay_has_unread_data = 0;
+ // target time
+ replay_get_dword();
+ replay_get_dword();
+ replay_get_dword();
+ replay_get_dword();
+ replay_get_dword();
+ replay_get_dword();
+ // host time
+ replay_get_dword();
+ replay_get_dword();
+ replay_get_dword();
+ replay_get_dword();
+ replay_get_dword();
+ replay_get_dword();
+ break;
+ case EVENT_INSTRUCTION:
+ first_cpu->instructions_count = replay_get_dword();
+ skipping = 0;
+ break;
+ default:
+ // clock, time_t, checkpoint and other events
+ skipping = 0;
+ break;+ }
+ }
+
+ return 0;
+}
+
+
+
+//! Skips async events invocations from the input, +//! until required data kind is found. +static int skip_async_events_until(unsigned int kind) +{
+ while (true)
+ {
+ if (skip_async_events(kind))
+ {
+ return 1;
+ }
+
+ if (replay_data_kind != kind)
+ {
+ // TODO: fix this
+ if (replay_data_kind == EVENT_ASYNC && kind == EVENT_INSTRUCTION)
+ {+ return 0;
+ }
+
+ fprintf(stderr, "%" PRId64 ": read data kind %d instead of expected %d\n",
+ replay_get_current_step(), replay_data_kind, kind);
+ exit(1);
+ }
+ break;
+ }
+
+ return 0;
+}
+
+
+void replay_assert(uint64_t value, const char *message) +{
+ if (!replay_file || !debug_mode)
+ return;
+
+ if (replay_mode == REPLAY_SAVE)
+ {
+ replay_save_instructions();
+
+ replay_put_event(EVENT_ASSERT);
+ replay_put_qword(value);
+ }
+ else if (replay_mode == REPLAY_PLAY)
+ {+ if (play_submode == REPLAY_PLAY_CHANGED)
+ {
+ fprintf(stderr, "Cannot read log in replay run mode\n");
+ exit(1);
+ }
+
+ if (skip_async_events_until(EVENT_ASSERT))
+ {
+ // No assert was written here + // TODO: do not exit? + fprintf(stderr, "Cannot read assert\n"); + exit(1);
+ }
+
+ int64_t val = replay_get_qword();
+
+ replay_check_error();
+ replay_has_unread_data = 0;
+
+ if (value != val)
+ {
+ fprintf(stderr, "%" PRId64 ": invalid assert: read %" PRIx64 " instead of expected %"
PRIx64 " (%s)\n",
+ replay_get_current_step(), val, value, message ? message : "---");
+ exit(1);
+ }
+ }
+}
+
+
+
+//! Reads next clock event from the input.
+int64_t replay_read_clock(unsigned int kind)
+{
+ if (play_submode == REPLAY_PLAY_CHANGED)
+ {
+ fprintf(stderr, "Cannot read log in replay run mode\n");
+ exit(1);
+ }
+
+ if (kind >= REPLAY_CLOCK_COUNT)
+ {
+ fprintf(stderr, "invalid clock ID %d for replay\n", kind);
+ exit(1);
+ }
+
+ if (replay_file)
+ {
+ if (!skip_async_events(EVENT_CLOCK + kind))
+ {
+ if (replay_data_kind == EVENT_CLOCK + kind)
+ {
+ replay_read_next_clock(kind);
+ }
+ }
+ int64_t ret = replay_state.cached_clock[kind];
+
+ return ret;
+ }
+
+ fprintf(stderr, "REPLAY INTERNAL ERROR %d\n", __LINE__);
+ exit(1);
+}
+
+int replay_has_timer_request(void)
+{
+ if (replay_state.skipping_instruction)
+ {
+ return 0;
+ }
+
+ if (replay_mode == REPLAY_PLAY)
+ {
+ if (play_submode == REPLAY_PLAY_CHANGED)
+ return 1;
+
+ skip_async_events(EVENT_ASYNC);
+ if (replay_data_kind == EVENT_ASYNC)
+ return 1;
+
+ {
+ if (replay_get_current_step() == replay_break_step)
+ return 1;
+
+ return 0;
+ }
+ }
+ else if (replay_mode == REPLAY_SAVE)
+ {+ if (replay_has_events())
+ {
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+void replay_write_interrupt_request(void)
+{
+ replay_save_instructions();
+ replay_put_event(EVENT_INTERRUPT);
+}
+
+
+int replay_read_interrupt_request(void)
+{
+ // try to read event
+ if (replay_mode == REPLAY_PLAY)
+ {
+ skip_async_events(EVENT_INTERRUPT);
+ if (replay_data_kind == EVENT_INTERRUPT)
+ {
+ }+ }
+ int ret = replay_data_kind == EVENT_INTERRUPT;
+
+ return ret;
+}
+
+void replay_reset_interrupt_request(void)
+{
+ if (replay_data_kind == EVENT_INTERRUPT)
+ {
+ replay_has_unread_data = 0;
+ }
+}
+
+int replay_interrupt_request(void)
+{
+ if (replay_mode == REPLAY_SAVE)
+ {
+ replay_write_interrupt_request();
+ return 1;
+ }
+ else if (replay_mode == REPLAY_PLAY)
+ {+ if (play_submode == REPLAY_PLAY_CHANGED)
+ {
+ fprintf(stderr, "Cannot read log in replay run mode\n");
+ exit(1);
+ }
+
+ if (replay_read_interrupt_request())
+ {
+ replay_reset_interrupt_request();
+ return 1;
+ }
+ else
+ {+ // no action
+ // wait here in outer loop
+ }
+
+ return 0;
+ }
+
+ return 1;
+}
+
+
+bool replay_exception(void)
+{
+ if (replay_mode == REPLAY_SAVE)
+ {
+ replay_save_instructions();
+ replay_put_event(EVENT_EXCEPTION);
+ return true;
+ }
+ else if (replay_mode == REPLAY_PLAY)
+ {
+ if (play_submode == REPLAY_PLAY_CHANGED)
+ return true;
+
+ skip_async_events(EVENT_EXCEPTION);
+ if (replay_data_kind == EVENT_EXCEPTION)
+ {+ replay_has_unread_data = 0;
+ return true;
+ }
+ return false;
+ }
+
+ return true;
+}
+
+
+
+void replay_shutdown_request(void)
+{
+ if (replay_mode == REPLAY_SAVE)
+ {
+ replay_put_event(EVENT_SHUTDOWN);
+ }
+}
+
+bool replay_has_code(void)
+{
+ if (play_submode == REPLAY_PLAY_CHANGED)
+ return true;
+
+ if (replay_has_instruction())
+ return true;
+
+ if (replay_mode == REPLAY_PLAY)
+ {
+ skip_async_events(EVENT_INTERRUPT);
+ if (replay_data_kind == EVENT_INTERRUPT)
+ return true;
+ skip_async_events(EVENT_EXCEPTION);
+ if (replay_data_kind == EVENT_EXCEPTION)
+ return true;
+ return false;
+ }
+
+ return true;
+}
+
+int replay_has_instruction(void)
+{
+ int res = 1;
+ if (replay_state.skipping_instruction)
+ {+ return 1;
+ }
+
+ if (replay_mode == REPLAY_PLAY)
+ {
+ skip_async_events(EVENT_INSTRUCTION);
+ if (replay_data_kind != EVENT_INSTRUCTION
+ && replay_data_kind != EVENT_ASYNC)
+ {
+ res = 0;
+ }
+ }
+ return res;
+}
+
+
+void replay_instruction(int process_timer)
+{
+ if (replay_state.skipping_instruction) {
+ replay_state.skipping_instruction = 0;
+
+ return;
+ }
+
+ if (replay_file)
+ {
+ if (replay_mode == REPLAY_SAVE)
+ {
+ if (replay_has_sound_events())
+ {
+ replay_save_instructions();
+ }
+
+ if (process_timer && replay_has_events())
+ {
+ replay_save_instructions();
+ // events will be after the last instruction
+ replay_save_events(-1);
+ }
+ else
+ {
+ // instruction - increase the step counter
+ ++first_cpu->instructions_count;
+ }
+ }
+ else if (replay_mode == REPLAY_PLAY)
+ {
+ if (play_submode == REPLAY_PLAY_CHANGED)
+ {
+ replay_state.current_step += first_cpu->instructions_count;
+ first_cpu->instructions_count = 0;
+ return;+ }
+
+ if (skip_async_events_until(EVENT_INSTRUCTION))
+ {
+ // execution should be stopped in changed replay mode
+ return;
+ }
+ // No timer is expected right now
+ if (first_cpu->instructions_count >= 1)
+ {
+ // stop only at instruction
+ if (replay_get_current_step() == replay_break_step)
+ {
+ replay_break_step = -1;
+
+ // for stopping VM
+ if (play_submode == REPLAY_PLAY_NORMAL)
+ {
+ first_cpu->exception_index = EXCP_DEBUG;
+ monitor_printf(default_mon, "Execution has stopped.\n");
+ vm_stop(EXCP_DEBUG);
+ }
+ else if (play_submode == REPLAY_PLAY_GOTO)
+ {
+ play_submode = REPLAY_PLAY_NORMAL;
+ cpu_handle_debug_exception(first_cpu->env_ptr);
+ }
+ else if (play_submode == REPLAY_PLAY_REVERSE)
+ {
+ cpu_handle_debug_exception(first_cpu->env_ptr);
+ return;
+ }
+ // for breaking execution loop
+ cpu_exit(first_cpu);
+ return;
+ }
+
+ ++replay_state.current_step;
+ --first_cpu->instructions_count;
+ if (first_cpu->instructions_count == 0)
+ replay_has_unread_data = 0;
+ }
+ else
+ {+ replay_read_events(-1);
+ }
+ }
+ }
+}
+
+
+bool replay_has_checkpoint(unsigned int checkpoint) +{
+ if (replay_file && replay_mode == REPLAY_PLAY)
+ {
+ if (play_submode == REPLAY_PLAY_CHANGED)
+ return true;
+
+ if (skip_async_events(EVENT_CHECKPOINT + checkpoint))
+ {
+ return false;
+ }
+ if (replay_data_kind == EVENT_CHECKPOINT + checkpoint)
+ {
+ return true;
+ }
+ // not checking the checkpoint id
+ // assume that this function is called at the right place
+ if (replay_data_kind == EVENT_ASYNC_OPT)
+ {+ return true;
+ }
+ }
+
+ return false;
+}
+
+//! Should be called at optional check points in the execution. +//! These check points are skipped, if they were not meet. +//! Saves checkpoint in the SAVE mode and validates (skipping +//! the interrupts) in the PLAY mode.
+//! Returns 0 in PLAY mode if checkpoint was not found. +//! Returns 1 in all other cases.
+//! Used checkpoints: 0 1 2 3 5 6 7 8 9
+int replay_checkpoint(unsigned int checkpoint)
+{
+ replay_save_instructions();
+
+ if (replay_file)
+ {
+ if (replay_mode == REPLAY_PLAY)
+ {
+ if (play_submode == REPLAY_PLAY_CHANGED)
+ return 1;
+ if (skip_async_events(EVENT_CHECKPOINT + checkpoint))
+ {
+ return 0;
+ }
+ if (replay_data_kind != EVENT_CHECKPOINT + checkpoint)
+ {
+ if (replay_data_kind == EVENT_ASYNC_OPT)
+ {
+ replay_read_events(checkpoint);
+ replay_fetch_data_kind();
+ return replay_data_kind != EVENT_ASYNC_OPT;
+ }
+ return 0;
+ }
+ replay_has_unread_data = 0;
+ replay_read_events(checkpoint);
+ replay_fetch_data_kind();
+ return replay_data_kind != EVENT_ASYNC_OPT;
+ }
+ else if (replay_mode == REPLAY_SAVE)
+ {+ replay_put_event(EVENT_CHECKPOINT + checkpoint);
+ replay_save_events(checkpoint);
+ }
+ }
+
+ return 1;
+}
+
+
+void replay_save_time_t(time_t tm)
+{
+ replay_save_instructions();
+
+ if (replay_file)
+ {
+ replay_put_event(EVENT_TIME_T);
+ if (sizeof(tm) == 4)
+ {
+ replay_put_dword(tm);
+ }
+ else if (sizeof(tm) == 8)
+ {
+ replay_put_qword(tm);
+ }
+ else
+ {+ fprintf(stderr, "invalid time_t sizeof: %u\n", (unsigned)sizeof(tm));
+ exit(1);
+ }
+ }
+}
+
+time_t replay_read_time_t(void)
+{
+ if (play_submode == REPLAY_PLAY_CHANGED)
+ {
+ fprintf(stderr, "Cannot read log in replay run mode\n");
+ exit(1);
+ }
+
+ if (replay_file)
+ {
+ time_t tm;
+
+ if (skip_async_events_until(EVENT_TIME_T))
+ {
+ // TODO: return cached value + fprintf(stderr, "Cannot read time_t when stopped\n"); + exit(1);
+ }
+
+ if (sizeof(tm) == 4)
+ {
+ tm = replay_get_dword();
+ }
+ else if (sizeof(tm) == 8)
+ {
+ tm = replay_get_qword();
+ }
+ else
+ {+ fprintf(stderr, "invalid time_t sizeof: %u\n", (unsigned)sizeof(tm));
+ exit(1);
+ }
+
+ replay_check_error();
+
+ replay_has_unread_data = 0;
+
+ return tm;
+ }
+
+ fprintf(stderr, "REPLAY INTERNAL ERROR %d\n", __LINE__);
+ exit(1);
+}
+
+void replay_save_tm(struct tm *tm)
+{
+ replay_save_instructions();
+
+ if (replay_file)
+ {
+ replay_put_event(EVENT_TM);
+
+ replay_put_dword(tm->tm_sec); + replay_put_dword(tm->tm_min); + replay_put_dword(tm->tm_hour); + replay_put_dword(tm->tm_mday); + replay_put_dword(tm->tm_mon); + replay_put_dword(tm->tm_year); + replay_put_dword(tm->tm_wday); + replay_put_dword(tm->tm_yday); + replay_put_dword(tm->tm_isdst);
+ }
+}
+
+void replay_read_tm(struct tm *tm)
+{
+ if (play_submode == REPLAY_PLAY_CHANGED)
+ {
+ fprintf(stderr, "Cannot read log in replay run mode\n");
+ exit(1);
+ }
+
+ if (replay_file)
+ {
+ if (skip_async_events_until(EVENT_TM))
+ {
+ return;
+ }
+
+ tm->tm_sec = replay_get_dword(); + tm->tm_min = replay_get_dword(); + tm->tm_hour = replay_get_dword(); + tm->tm_mday = replay_get_dword(); + tm->tm_mon = replay_get_dword(); + tm->tm_year = replay_get_dword(); + tm->tm_wday = replay_get_dword(); + tm->tm_yday = replay_get_dword(); + tm->tm_isdst = replay_get_dword();
+
+ replay_check_error();
+ replay_has_unread_data = 0;
+
+ return;
+ }
+
+ fprintf(stderr, "REPLAY INTERNAL ERROR %d\n", __LINE__);
+ exit(1);
+}
+
+
+
+void replay_undo_last_instruction(void)
+{
+ if (replay_mode == REPLAY_SAVE
+ || play_submode == REPLAY_PLAY_CHANGED)
+ {
+ first_cpu->instructions_count--;
+ }
+ else
+ {+ replay_state.skipping_instruction = 1;
+ }
+}
+
+
+uint64_t replay_get_current_step(void)
+{
+ if (first_cpu == NULL)
+ return 0;
+ if (replay_file)
+ {
+ if (replay_mode == REPLAY_SAVE)
+ {
+ return replay_state.current_step + first_cpu->instructions_count;
+ }
+ }
+ return replay_state.current_step;
+}
+
+
+void replay_set_break(uint64_t step)
+{
+ replay_break_step = step;
+}
+
+
+SavedStateInfo *find_nearest_state(uint64_t step) +{
+ SavedStateInfo *first = saved_states;
+ SavedStateInfo *last = saved_states + saved_states_count;
+ while (first < last - 1)
+ {+ SavedStateInfo *next = first + (last - first) / 2;
+ if (next->step > step)
+ last = next;
+ else
+ first = next;
+ }
+
+ return first;
+}
+
+
+int replay_seek_step(uint64_t step)
+{
+ if (replay_mode != REPLAY_PLAY)
+ return 0;
+
+ // load VM state, if possible
+ if (saved_states_count > 0)
+ {+ // find VM state to load
+ SavedStateInfo *first = find_nearest_state(step);
+
+ if (first->step <= step
+ && (replay_get_current_step() > step
+ || replay_get_current_step() < first->step
+ // always load the state, if something was changed
+ || play_submode == REPLAY_PLAY_CHANGED))
+ {
+ char name[128];
+ bool running = runstate_is_running();
+ if (running && !qemu_in_vcpu_thread())
+ {
+ vm_stop(RUN_STATE_RESTORE_VM);
+ }
+ else
+ {+ cpu_disable_ticks();
+ }
+
+ replay_clear_events();
+ replay_sound_flush_queue();
+
+ // reset the mode before loading to allow correct timers operation
+ if (/*play_submode == REPLAY_PLAY_STOPPED
+ || */play_submode == REPLAY_PLAY_CHANGED)
+ {
+ play_submode = REPLAY_PLAY_NORMAL;
+ }
+
+ current_saved_state = first - saved_states;
+ sprintf(name, SAVED_STATE_NAME_FORMAT, current_saved_state);
+ if (load_vmstate(name) < 0)
+ {
+ fprintf(stderr, "Replay: cannot load VM state while seeking\n");
+ exit(1);
+ }
+ // check end event
+ check_savevm();
+
+ tb_flush_all();
+
+ cpu_enable_ticks();
+ if (running && !qemu_in_vcpu_thread())
+ {+ vm_start();
+ }
+
+ replay_fetch_data_kind();
+ while (replay_data_kind >= EVENT_CLOCK && replay_data_kind < EVENT_CLOCK +REPLAY_CLOCK_COUNT)
+ {
+ replay_read_next_clock(-1);
+ replay_fetch_data_kind();
+ }
+ }
+ }
+
+ // setup the breakpoint
+ if (step >= replay_get_current_step())
+ {+ replay_set_break(step);
+ return 1;
+ }
+
+ return 0;
+}
+
+
+
+void replay_goto_step(uint64_t step)
+{
+ if (replay_seek_step(step))
+ {
+ play_submode = REPLAY_PLAY_GOTO;
+ }
+}
+
+
+void replay_events(struct Monitor *mon)
+{
+ if (replay_mode == REPLAY_PLAY && replay_file)
+ {
+ int ok = 1;
+ // becomes 1 when grey key is pressed or released
+ uint64_t offset = ftello64(replay_file);
+ uint64_t step = 0;
+ fseek(replay_file, HEADER_SIZE, SEEK_SET);
+
+ // read whole file and print the events
+ while (ok && !feof(replay_file))
+ {
+ uint8_t data;
+ size_t size;
+ fread(&data, sizeof(data), 1, replay_file);
+ switch (data) {
+ case EVENT_END_STARTUP:+ break;
+ case EVENT_TIME_T:
+ fseek(replay_file, sizeof(time_t), SEEK_CUR);
+ break;
+ case EVENT_TM:
+ fseek(replay_file, sizeof(uint32_t) * 9, SEEK_CUR);
+ break;
+ case EVENT_SOUND_OUT:
+ fseek(replay_file, sizeof(uint32_t), SEEK_CUR);
+ break;
+ case EVENT_SOUND_IN:
+ fread(&size, sizeof(size), 1, replay_file); + fseek(replay_file, size, SEEK_CUR); + break;
+ case EVENT_SAVE_VM_BEGIN:
+ monitor_printf(mon, "%" PRId64 ": VM state\n", step);
+ break;
+ case EVENT_SAVE_VM_END:
+ break;
+ case EVENT_INTERRUPT:
+ case EVENT_EXCEPTION:
+ break;
+ case EVENT_SHUTDOWN:
+ monitor_printf(mon, "%" PRId64 ": Request for closing simulator window\n", step);
+ break;
+ case EVENT_ASSERT:
+ fseek(replay_file, sizeof(uint64_t), SEEK_CUR);
+ break;
+ case EVENT_TARGET_TIME:
+ {
+ int32_t sec, min, hour, day, month, year,
+ hsec, hmin, hhour, hday, hmonth, hyear;
+ sec = replay_get_dword(); + min = replay_get_dword(); + hour = replay_get_dword(); + day = replay_get_dword(); + month = replay_get_dword(); + year = replay_get_dword(); + hsec = replay_get_dword(); + hmin = replay_get_dword(); + hhour = replay_get_dword(); + hday = replay_get_dword(); + hmonth = replay_get_dword(); + hyear = replay_get_dword(); + monitor_printf(mon, "%" PRId64 ": Target time %d.%02d.%02d-%02d:%02d:%02d Host
time %d.%02d.%02d-%02d:%02d:%02d\n",
+ step, year, month, day, hour, min, sec, hyear, hmonth, hday, hhour, hmin,
hsec);
+ }
+ break;
+ case EVENT_INSTRUCTION:
+ {
+ uint32_t count; + fread(&count, sizeof(count), 1, replay_file); + step += count;
+ }
+ break;
+ case EVENT_DATA_INT:
+ fseek(replay_file, sizeof(uint32_t), SEEK_CUR);
+ break;
+ case EVENT_DATA_BUFFER:
+ fread(&size, sizeof(size), 1, replay_file); + fseek(replay_file, size, SEEK_CUR); + break;
+ case EVENT_END:
+ ok = 0;
+ break;
+ case EVENT_ASYNC:
+ case EVENT_ASYNC_OPT:
+ replay_skip_async_event(mon, step, data == EVENT_ASYNC_OPT);
+ break;
+ default:
+ if (data >= EVENT_CLOCK && data < EVENT_CHECKPOINT)
+ {
+ fseek(replay_file, sizeof(uint64_t), SEEK_CUR);
+ }
+ else if (data >= EVENT_CHECKPOINT && data < EVENT_END)
+ {
+ // skip
+ }
+ else
+ {+ ok = 0;
+ monitor_printf(mon, "%" PRId64 ": Found unknown event %d\n", step, data);
+ }
+ break;
+ }
+ }
+ fseeko64(replay_file, offset, SEEK_SET);
+ }
+}
+
+
+int replay_xtoi(char c)
+{
+ switch (c)
+ {
+ case 'a':
+ case 'A':+ return 10;
+ case 'b':
+ case 'B':
+ return 11;
+ case 'c':
+ case 'C':
+ return 12;
+ case 'd':
+ case 'D':
+ return 13;
+ case 'e':
+ case 'E':
+ return 14;
+ case 'f':
+ case 'F':
+ return 15;
+ default:
+ if (isdigit(c))
+ return c - '0';
+
+ return 0;
+ }
+
+ return 0;
+}
+
+int64_t replay_get_file_offset(void)
+{
+ return (int64_t) ftello64(replay_file);
+}
+
+void replay_register_get_time(replay_get_date_func f, void *opaque) +{
+ if (replay_mode != REPLAY_NONE)
+ {
+ replay_get_target_time = f;
+ replay_target_time_dev = opaque;
+ }
+}
+
+void replay_data_int(int *data)
+{
+ if (replay_file && replay_mode == REPLAY_PLAY)
+ {
+ if (play_submode == REPLAY_PLAY_CHANGED)
+ {
+ fprintf(stderr, "Cannot read log in replay run mode\n");
+ exit(1);
+ }
+ skip_async_events(EVENT_DATA_INT);
+ validate_data_kind(EVENT_DATA_INT);
+ *data = replay_get_dword();
+ replay_check_error();
+ replay_has_unread_data = 0;
+ }
+ else if (replay_file && replay_mode == REPLAY_SAVE)
+ {
+ replay_save_instructions();
+ replay_put_event(EVENT_DATA_INT);
+ replay_put_dword(*data);+ }
+}
+
+void replay_data_buffer(unsigned char *data, size_t size) +{
+ if (replay_file && replay_mode == REPLAY_PLAY)
+ {
+ if (play_submode == REPLAY_PLAY_CHANGED)
+ {
+ fprintf(stderr, "Cannot read log in replay run mode\n");
+ exit(1);
+ }
+ size_t read_size = 0;
+ skip_async_events(EVENT_DATA_BUFFER);
+ validate_data_kind(EVENT_DATA_BUFFER);
+ replay_get_array(data, &read_size);
+ replay_check_error();
+ if (read_size != size)
+ {+ fprintf(stderr, "Replay: read non-matching size of the buffer\n");
+ exit(1);
+ }
+ replay_has_unread_data = 0;
+ }
+ else if (replay_file && replay_mode == REPLAY_SAVE)
+ {
+ replay_save_instructions();
+ replay_put_event(EVENT_DATA_BUFFER);
+ replay_put_array(data, size);+ }
+}
+
replay/replay.h new file +235 -0
235 lines not on this page · raw message
REPLAY_SAVE ? (var = (value), replay_data_int(&var), var) : /* PLAY */ (replay_data_int(&var), var)))
+/*! Writes or reads buffer to/from replay log. */
+void replay_data_buffer(unsigned char *data, size_t size);
+/*! Macro for using replay_data_buffer function */
+#define REPLAY_DATA_VAR(var) replay_data_buffer((unsigned char*)&(var), sizeof(var)) +/*! Saves write to char device event to the log */
+void replay_chr_be_write(CharDriverState *s, uint8_t *buf, int len);
+
+/* Returns the virtual CPU time, based on the instruction counter. */ +int64_t replay_get_icount(void);
+void replay_clock_warp(void);
+
+/*! Starts normal execution from the current replay step. */
+void replay_change(struct Monitor *mon);
+#endif
Quoted 22 lines · Pavel Dovgaluk
Functions for writing and reading replay log. These functions are used to record the following events:
* Output of thetime()function
* Sound in and sound out events in winaudio
* Hardware interrupts and exceptions
* Special replay debug assert
* Target time information used for reviewing the events log
* Start and finish of saving VM
* Synchronous data from some module
* Execution of the group of the instructions
* Hardware clock read (real ticks, host clock, virtual clock)
* Asynchronous events:
- Setting event notifier
- Network packet
- Worker thread invocation
- BH execution
- USB packets
- Serial port input
- Mouse and keyboard input
* Checkpoint in the code (used to synchronize the events)
* System shutdown
Quoted 5 lines
This module also sets timer for periodical snapshotting of the system state, when it is required by user.
Signed-off-by Pavel Dovgalyuk <[email protected]>---
This change is way to large for one patch (more than 4000 lines of code). You need to split it into smaller ones before it can be reviewed.
Signature · 1 line
Kirill