@@ -97,9 +97,19 @@ static GPtrArray *registers_init(void) {
9797 return registers -> len ? g_steal_pointer (& registers ) : NULL ;
9898}
9999
100+ static void write_toc_entry (FrameBuffer * fbuf ) {
101+ g_rw_lock_writer_lock (& state .file_lock );
102+ g_rw_lock_writer_lock (& state .toc_entries_offsets_lock );
103+ frame_buffer_flush_to_file (fbuf , state .file );
104+ uint64_t next_toc_entry = ftell (state .file );
105+ g_array_append_val (state .toc_entries_offsets , next_toc_entry );
106+ g_rw_lock_writer_unlock (& state .toc_entries_offsets_lock );
107+ g_rw_lock_writer_unlock (& state .file_lock );
108+ }
109+
100110static void log_insn_reg_access (unsigned int vcpu_index , void * udata ) {
101111 g_rw_lock_reader_lock (& state .vcpus_array_lock );
102- g_rw_lock_reader_lock (& state .frame_buffer_lock );
112+ g_rw_lock_writer_lock (& state .frame_buffer_lock );
103113
104114 FrameBuffer * fbuf = g_ptr_array_index (state .frame_buffer , vcpu_index );
105115 VCPU * vcpu = g_ptr_array_index (state .vcpus , vcpu_index );
@@ -113,17 +123,15 @@ static void log_insn_reg_access(unsigned int vcpu_index, void *udata) {
113123 }
114124
115125 if (frame_buffer_is_full (fbuf )) {
116- g_rw_lock_writer_lock (& state .file_lock );
117- frame_buffer_flush_to_file (fbuf , state .file );
118- g_rw_lock_writer_unlock (& state .file_lock );
126+ write_toc_entry (fbuf );
119127 }
120128
121129 // Open new one.
122130 Instruction * insn = udata ;
123131 add_new_insn_frame (vcpu , vcpu_index , fbuf , insn );
124132 add_pre_reg_state (vcpu , vcpu_index , current_regs , fbuf );
125133
126- g_rw_lock_reader_unlock (& state .frame_buffer_lock );
134+ g_rw_lock_writer_unlock (& state .frame_buffer_lock );
127135 g_rw_lock_reader_unlock (& state .vcpus_array_lock );
128136}
129137
@@ -228,9 +236,11 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
228236
229237 const char * target_path = "/tmp/test.trace" ;
230238 state .frame_buffer = g_ptr_array_new ();
239+ state .toc_entries_offsets = g_array_new (false, true, sizeof (uint64_t ));
231240 state .vcpus = g_ptr_array_new ();
232241 state .file = fopen (target_path , "wb" );
233- if (!(state .frame_buffer || state .vcpus || state .file )) {
242+ if (!(state .frame_buffer || state .vcpus || state .file ||
243+ !state .toc_entries_offsets )) {
234244 return 1 ;
235245 }
236246 for (size_t i = 0 ; i < argc ; ++ i ) {
@@ -242,6 +252,8 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
242252 }
243253 // write_meta(argv, envp, target_argv, target_envp);
244254
255+ g_array_append_val (state .toc_entries_offsets , offset_toc_start );
256+
245257 qemu_plugin_register_vcpu_init_cb (id , vcpu_init );
246258 qemu_plugin_register_vcpu_tb_trans_cb (id , cb_trans );
247259 qemu_plugin_register_atexit_cb (id , plugin_exit , NULL );
0 commit comments