@@ -1637,12 +1637,14 @@ impl Emu {
16371637 self.force_break = true;
16381638 }
16391639
1640+ let bits = self.get_size(operand);
1641+
16401642 if self.cfg.trace_mem {
16411643 let memory_operation = MemoryOperation {
16421644 pos: self.pos,
16431645 rip: self.regs.rip,
16441646 op: "write".to_string(),
1645- bits: 32 ,
1647+ bits: bits as u32 ,
16461648 address: addr,
16471649 old_value: 0, // TODO
16481650 new_value: value as u64,
@@ -1651,8 +1653,7 @@ impl Emu {
16511653 self.memory_operations.push(memory_operation);
16521654 println!("\tmem_trace: pos = {} rip = {:x} op = write bits = {} address = 0x{:x} value = 0x{:x} name = '{}'", self.pos, self.regs.rip, 32, addr, value, name);
16531655 }
1654-
1655- let bits = self.get_size(operand);
1656+
16561657 let ret = match bits {
16571658 64 => self.maps.write_qword(addr, value),
16581659 32 => self.maps.write_dword(addr, (value & 0xffffffff) as u32),
@@ -4202,6 +4203,7 @@ impl Emu {
42024203 let instruction = self.instruction.unwrap();
42034204 let instruction_bytes = &self.instruction_bytes;
42044205
4206+ // dump all registers on first, only differences on next
42054207 let mut registers = String::new();
42064208 if index == 0 {
42074209 /*
@@ -4248,7 +4250,14 @@ impl Emu {
42484250 );
42494251 }
42504252
4251- let flags = format!("rflags: {:x}-> {:x}", self.pre_op_flags.dump(), self.post_op_flags.dump());
4253+ let mut flags = String::new();
4254+ if index == 0 {
4255+ flags = format!("rflags: {:x}-> {:x}", self.pre_op_flags.dump(), self.post_op_flags.dump());
4256+ } else {
4257+ if self.pre_op_flags.dump() != self.post_op_flags.dump() {
4258+ flags = format!("rflags: {:x}-> {:x}", self.pre_op_flags.dump(), self.post_op_flags.dump());
4259+ }
4260+ }
42524261
42534262 let mut memory = String::new();
42544263 for memory_op in self.memory_operations.iter() {
@@ -4262,7 +4271,7 @@ impl Emu {
42624271 let mut trace_file = self.cfg.trace_file.as_ref().unwrap();
42634272 writeln!(
42644273 trace_file,
4265- " {index:02X}, {address:016X}, {bytes:02x?}, {disassembly}, {registers}, {memory}, {comments}",
4274+ r#"" {index:02X}"," {address:016X}"," {bytes:02x?}"," {disassembly}"," {registers}"," {memory}"," {comments}""# ,
42664275 index = index,
42674276 address = self.pre_op_regs.rip,
42684277 bytes = instruction_bytes,
@@ -4272,7 +4281,7 @@ impl Emu {
42724281 comments = ""
42734282 ).expect("failed to write to trace file");
42744283
4275- if index > 10 {
4284+ if index > 32 {
42764285 panic!("OUT");
42774286 }
42784287 }
@@ -4423,19 +4432,22 @@ impl Emu {
44234432 let mut formatter = IntelFormatter::new();
44244433 formatter.options_mut().set_digit_separator("");
44254434 formatter.options_mut().set_first_operand_char_index(6);
4435+
44264436 // get first instruction from iterator
4427- let ins = decoder.iter().next().unwrap();
4428- // size
4437+ let ins = decoder.decode();
44294438 let sz = ins.len();
4439+ let addr = ins.ip();
4440+ let position = decoder.position();
4441+ let instruction_bytes = block[position-sz..position].to_vec();
44304442
44314443 // clear
44324444 self.out.clear();
4445+ self.memory_operations.clear();
44334446
44344447 // format
44354448 formatter.format(&ins, &mut self.out);
44364449 self.instruction = Some(ins);
4437- self.instruction_bytes = vec![]; // TODO
4438- self.memory_operations.clear();
4450+ self.instruction_bytes = instruction_bytes;
44394451
44404452 // emulate
44414453 let result_ok = self.emulate_instruction(&ins, sz, true);
@@ -4509,17 +4521,17 @@ impl Emu {
45094521 Decoder::with_ip(32, &block, self.regs.get_eip(), DecoderOptions::NONE);
45104522 }
45114523
4512- for ins in decoder.iter() {
4524+ while decoder.can_decode() {
4525+ let ins = decoder.decode();
45134526 let sz = ins.len();
45144527 let addr = ins.ip();
4515- let position = ins.ip() - self.regs.rip ;
4516- let instruction_bytes = block[position as usize ..position as usize + sz ].to_vec();
4528+ let position = decoder.position() ;
4529+ let instruction_bytes = block[position-sz ..position].to_vec();
45174530
45184531 if !end_addr.is_none() && Some(addr) == end_addr {
45194532 return Ok(self.regs.rip);
45204533 }
45214534
4522-
45234535 self.out.clear();
45244536 formatter.format(&ins, &mut self.out);
45254537 self.instruction = Some(ins);
0 commit comments