Skip to content

Commit 8319c6b

Browse files
Replace println with log
1 parent 0a0b78c commit 8319c6b

File tree

8 files changed

+49
-40
lines changed

8 files changed

+49
-40
lines changed

risc_v_simulator/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ poseidon2 = { workspace = true, optional = true }
2424
rand = { workspace = true, features = ["std", "thread_rng", "std_rng"]}
2525
ruint = { version = "1.15", optional = true, default-features = false }
2626
serde = { workspace = true }
27+
log = { workspace = true }
2728

2829
[dev-dependencies]
2930
lib-rv32-asm = {git = "https://github.com/shamatar/lib-rv32.git" }

risc_v_simulator/bin/runner.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use risc_v_simulator::{runner::run_simple_simulator, sim::SimulatorConfig};
2+
use log::info;
23

34
pub fn main() {
45
// let args: Vec<String> = std::env::args().collect();
56
// dbg!(&args);
67
// assert_eq!(args.len(), 2);
78
// let path = &args[1];
8-
println!("ZK RISC-V simulator is starting");
9+
info!("ZK RISC-V simulator is starting");
910

1011
let path = "../zksync-os/zksync_os/app.bin";
1112
let path_sym = "../zksync-os/zksync_os/app.elf";

risc_v_simulator/src/abstractions/non_determinism.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use log::info;
2+
13
// there is no interpretation of methods here, it's just read/write and that's all
24
pub trait NonDeterminismCSRSource<M: MemorySource + ?Sized> {
35
const SHOULD_MOCK_READS_BEFORE_WRITES: bool = true;
@@ -105,7 +107,7 @@ impl QuasiUARTSourceState {
105107
}
106108
if remaining_words.unwrap() == 0 {
107109
let buffer = std::mem::replace(buffer, Vec::new());
108-
println!("UART: `{}`", String::from_utf8_lossy(&buffer));
110+
info!("UART: `{}`", String::from_utf8_lossy(&buffer));
109111
*self = QuasiUARTSourceState::Ready;
110112
}
111113
}

risc_v_simulator/src/cycle/state.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::HashMap;
22
use std::hint::unreachable_unchecked;
3+
use log::info;
34

45
use super::{status_registers::*, MachineConfig};
56
use crate::abstractions::csr_processor::CustomCSRProcessor;
@@ -142,7 +143,7 @@ pub fn output_opcode_stats() {
142143
keys.sort();
143144
for key in keys.into_iter() {
144145
let value = OPCODES_COUNTER.with_borrow(|el| el[key]);
145-
println!("Opcode {}: used {} times", key, value);
146+
info!("Opcode {}: used {} times", key, value);
146147
}
147148
}
148149
}
@@ -584,7 +585,7 @@ impl<Config: MachineConfig> RiscV32State<Config> {
584585

585586
let current_privilege_mode = self.extra_flags.get_current_mode();
586587
let mut pc = self.observable.pc;
587-
// println!("PC = 0x{:08x}", pc);
588+
// info!("PC = 0x{:08x}", pc);
588589
let mut ret_val: u32 = 0;
589590
let mut trap = TrapReason::NoTrap;
590591
let mut instr: u32 = 0;
@@ -746,7 +747,7 @@ impl<Config: MachineConfig> RiscV32State<Config> {
746747

747748
let virtual_address = rs1.wrapping_add(imm);
748749

749-
// println!("Load into x{:02} from 0x{:08x} at PC = 0x{:08x}", rd, virtual_address, pc);
750+
// info!("Load into x{:02} from 0x{:08x} at PC = 0x{:08x}", rd, virtual_address, pc);
750751

751752
// we formally access it once, but most likely full memory access
752753
// will be abstracted away into external interface hiding memory translation too
@@ -848,7 +849,7 @@ impl<Config: MachineConfig> RiscV32State<Config> {
848849
// it's S-type, that has no RD, so set it to x0
849850
rd = 0;
850851

851-
// println!("Store of x{:02} = 0x{:08x} into 0x{:08x} at PC = 0x{:08x}", STypeOpcode::rs2(instr), rs2, virtual_address, pc);
852+
// info!("Store of x{:02} = 0x{:08x} into 0x{:08x} at PC = 0x{:08x}", STypeOpcode::rs2(instr), rs2, virtual_address, pc);
852853

853854
// store operand rs2
854855

@@ -1194,7 +1195,7 @@ impl<Config: MachineConfig> RiscV32State<Config> {
11941195
}
11951196
csr => {
11961197
assert!(Config::ALLOWED_DELEGATION_CSRS.contains(&csr), "Machine {:?} is not configured to support CSR number {} at pc 0x{:08x}", Config::default(), csr, pc);
1197-
// println!("Custom CSR = 0x{:04x} READ at cycle {}", csr_number, proc_cycle);
1198+
// info!("Custom CSR = 0x{:04x} READ at cycle {}", csr_number, proc_cycle);
11981199
csr_processor.process_read(self, memory_source, non_determinism_source, tracer, mmu, csr_number, rs1, rs1_as_imm, &mut ret_val, &mut trap);
11991200
if trap.is_a_trap() {
12001201
break 'cycle_block;
@@ -1250,7 +1251,7 @@ impl<Config: MachineConfig> RiscV32State<Config> {
12501251
assert!(Config::ALLOWED_DELEGATION_CSRS.contains(&csr), "Machine {:?} is not configured to support CSR number {}", Config::default(), csr);
12511252
Self::add_delegation(csr);
12521253
// let t = CSR_COUNTER.fetch_add(1, std::sync::atomic::Ordering::AcqRel);
1253-
// println!("Custom CSR = 0x{:04x} WRITE at cycle {}, total: {}", csr_number, proc_cycle, t + 1);
1254+
// info!("Custom CSR = 0x{:04x} WRITE at cycle {}, total: {}", csr_number, proc_cycle, t + 1);
12541255
csr_processor.process_write(self, memory_source, non_determinism_source, tracer, mmu, csr_number, rs1, rs1_as_imm, &mut trap);
12551256
if trap.is_a_trap() {
12561257
break 'cycle_block;
@@ -1304,7 +1305,7 @@ impl<Config: MachineConfig> RiscV32State<Config> {
13041305
}
13051306
csr => {
13061307
assert!(Config::ALLOWED_DELEGATION_CSRS.contains(&csr), "Machine {:?} is not configured to support CSR number {}", Config::default(), csr);
1307-
// println!("Custom CSR = 0x{:04x} READ at cycle {}", csr_number, proc_cycle);
1308+
// info!("Custom CSR = 0x{:04x} READ at cycle {}", csr_number, proc_cycle);
13081309
csr_processor.process_read(self, memory_source, non_determinism_source, tracer, mmu, csr_number, rs1, rs1_as_imm, &mut ret_val, &mut trap);
13091310
if trap.is_a_trap() {
13101311
break 'cycle_block;
@@ -1391,7 +1392,7 @@ impl<Config: MachineConfig> RiscV32State<Config> {
13911392
// rd = 0;
13921393
// // mainly we support WFI, MRET, ECALL and EBREAK
13931394
// if csr_number == 0x105 {
1394-
// println!("WFI: proc_cycle: {:?}, pc = {}, opcode = 0x{:08x}", proc_cycle, pc, instr);
1395+
// info!("WFI: proc_cycle: {:?}, pc = {}, opcode = 0x{:08x}", proc_cycle, pc, instr);
13951396
// self.extra_flags.set_wait_for_interrupt_bit();
13961397
// self.pc = pc.wrapping_add(4u32);
13971398
// return;
@@ -1490,7 +1491,7 @@ impl<Config: MachineConfig> RiscV32State<Config> {
14901491

14911492
// If there was a trap, do NOT allow register writeback.
14921493
debug_assert_eq!(trap, TrapReason::NoTrap);
1493-
// println!("Set x{:02} = 0x{:08x}", rd, ret_val);
1494+
// info!("Set x{:02} = 0x{:08x}", rd, ret_val);
14941495
self.set_register(rd, ret_val, tracer);
14951496

14961497
// traps below will update PC themself, so it only happens if we have NO trap
@@ -1499,7 +1500,7 @@ impl<Config: MachineConfig> RiscV32State<Config> {
14991500

15001501
// Handle traps and interrupts.
15011502
if trap.is_a_trap() {
1502-
println!("trap: {:?}, pc: {:08x}, instr: {:08x}", trap, pc, instr);
1503+
info!("trap: {:?}, pc: {:08x}, instr: {:08x}", trap, pc, instr);
15031504

15041505
if Config::HANDLE_EXCEPTIONS == false {
15051506
panic!("Simulator encountered an exception");
@@ -1515,7 +1516,7 @@ impl<Config: MachineConfig> RiscV32State<Config> {
15151516
// TODO: here we have a freedom of what to put into tval. We place opcode value now, because PC will be placed into EPC below
15161517
self.machine_mode_trap_data.handling.tval = instr;
15171518
}
1518-
// println!("Trapping at pc = 0x{:08x} into PC = 0x{:08x}. MECP is set to 0x{:08x}", pc, self.machine_mode_trap_data.setup.tvec, pc);
1519+
// info!("Trapping at pc = 0x{:08x} into PC = 0x{:08x}. MECP is set to 0x{:08x}", pc, self.machine_mode_trap_data.setup.tvec, pc);
15191520
// self.pretty_dump();
15201521
// self.stack_dump(memory, mmu);
15211522

@@ -1546,11 +1547,11 @@ impl<Config: MachineConfig> RiscV32State<Config> {
15461547
tracer.at_cycle_end(&*self);
15471548

15481549
//let trap = trap.as_register_value();
1549-
//println!("end of cycle: PC = 0x{:08x}, trap = 0x{:08x}, interrupt = {:?}", self.pc, trap, trap & INTERRUPT_MASK != 0);
1550+
//info!("end of cycle: PC = 0x{:08x}, trap = 0x{:08x}, interrupt = {:?}", self.pc, trap, trap & INTERRUPT_MASK != 0);
15501551
}
15511552

15521553
pub fn pretty_dump(&self) {
1553-
println!(
1554+
info!(
15541555
"PC = 0x{:08x}, RA = 0x{:08x}, SP = 0x{:08x}, GP = 0x{:08x}",
15551556
self.observable.pc,
15561557
self.observable.registers[1],
@@ -1567,7 +1568,7 @@ impl<Config: MachineConfig> RiscV32State<Config> {
15671568
for (idx, reg) in chunk.iter() {
15681569
print!("x{:02} = 0x{:08x}, ", idx, reg);
15691570
}
1570-
println!("");
1571+
info!("");
15711572
}
15721573
}
15731574
}

risc_v_simulator/src/mmio/quasi_uart.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{collections::VecDeque, ffi::CString};
2+
use log::info;
23

34
use super::*;
45

@@ -37,7 +38,7 @@ impl MMIOSource for QuasiUART {
3738
let mut buffer = std::mem::replace(&mut self.buffer, vec![]);
3839
buffer.truncate(idx + 1);
3940
let c_string = CString::from_vec_with_nul(buffer).unwrap();
40-
println!("UART: `{}`", c_string.to_string_lossy());
41+
info!("UART: `{}`", c_string.to_string_lossy());
4142
break;
4243
}
4344
}

risc_v_simulator/src/runner/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::path::Path;
2+
use log::{info, debug};
23

34
use crate::abstractions::memory::MemorySource;
45
use crate::abstractions::memory::VectorMemoryImpl;
@@ -132,7 +133,7 @@ pub fn run_simulator_with_traces_for_config<C: MachineConfig>(
132133
let exec = sim.run(
133134
|_, _| {},
134135
|sim, cycle| {
135-
println!(
136+
info!(
136137
"mtvec: {:?}",
137138
sim.machine.state.machine_mode_trap_data.setup.tvec
138139
);
@@ -144,13 +145,13 @@ pub fn run_simulator_with_traces_for_config<C: MachineConfig>(
144145
}
145146

146147
fn read_bin<P: AsRef<Path>>(path: P) -> Vec<u8> {
147-
dbg!(path.as_ref());
148+
debug!("path.as_ref() = \"{}\"", path.as_ref().display());
148149
let mut file = std::fs::File::open(path).expect("must open provided file");
149150
let mut buffer = vec![];
150151
std::io::Read::read_to_end(&mut file, &mut buffer).expect("must read the file");
151152

152153
assert_eq!(buffer.len() % 4, 0);
153-
dbg!(buffer.len() / 4);
154+
debug!("buffer.len() / 4 = {}", buffer.len() / 4);
154155

155156
buffer
156157
}

risc_v_simulator/src/sim/diag.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::{
99
mem::size_of,
1010
path::{Path, PathBuf},
1111
};
12+
use log::info;
1213

1314
use addr2line::LookupContinuation;
1415
use addr2line::{
@@ -279,7 +280,7 @@ impl Profiler {
279280
}
280281

281282
pub(crate) fn print_stats(&self) {
282-
println!("{:#?}", self.stats);
283+
info!("{:#?}", self.stats);
283284
}
284285
}
285286

@@ -381,17 +382,17 @@ impl SymbolInfo {
381382
.expect("Frame info should've been created on frame loading.")
382383
.to(|x| {
383384
if x.is_tracked {
384-
println!("is_traceable:");
385-
println!("address: 0x{:08x}", address);
386-
println!("address: {}", address);
387-
println!("{:#?}", x);
385+
info!("is_traceable:");
386+
info!("address: 0x{:08x}", address);
387+
info!("address: {}", address);
388+
info!("{:#?}", x);
388389
tracked = true;
389390
}
390391
address >= x.prologue_end && address < x.epilogue_begin
391392
});
392393

393394
if tracked {
394-
println!("r {}", r);
395+
info!("r {}", r);
395396
}
396397

397398
r
@@ -420,12 +421,12 @@ impl SymbolInfo {
420421
gimli::DW_TAG_formal_parameter => "DW_TAG_formal_parameter".to_owned(),
421422
otherwise => format!("{:x?}", otherwise),
422423
};
423-
println!("tag {:?}", tag_n);
424+
info!("tag {:?}", tag_n);
424425

425426
let mut attrs = die.attrs();
426427

427428
while let Ok(Some(attr)) = attrs.next() {
428-
println!(" {:x?} -> {:x?}", attr.name(), attr.value());
429+
info!(" {:x?} -> {:x?}", attr.name(), attr.value());
429430

430431
match attr.name() {
431432
gimli::DW_AT_linkage_name | gimli::DW_AT_name => {
@@ -434,19 +435,19 @@ impl SymbolInfo {
434435
match n {
435436
gimli::AttributeValue::DebugStrRef(n) => {
436437
let s = dw.string(n).unwrap();
437-
println!(" value: {}", s.to_string_lossy());
438+
info!(" value: {}", s.to_string_lossy());
438439
}
439440
_ => {}
440441
}
441442
}
442443

443444
gimli::DW_AT_frame_base => match attr.value() {
444445
gimli::AttributeValue::Exprloc(ex) => {
445-
println!("expr decode");
446+
info!("expr decode");
446447
let mut ops = ex.operations(unit.encoding());
447448

448449
while let Ok(Some(op)) = ops.next() {
449-
println!("op: {:?}", op);
450+
info!("op: {:?}", op);
450451
}
451452
}
452453
_ => {}
@@ -460,7 +461,7 @@ impl SymbolInfo {
460461
let mut attrs = die2.attrs();
461462

462463
while let Ok(Some(attr)) = attrs.next() {
463-
println!(" {:x?} -> {:x?}", attr.name(), attr.value());
464+
info!(" {:x?} -> {:x?}", attr.name(), attr.value());
464465

465466
match attr.name() {
466467
gimli::DW_AT_linkage_name | gimli::DW_AT_name => {
@@ -469,7 +470,7 @@ impl SymbolInfo {
469470
match n {
470471
gimli::AttributeValue::DebugStrRef(n) => {
471472
let s = dw.string(n).unwrap();
472-
println!(" value: {}", s.to_string_lossy());
473+
info!(" value: {}", s.to_string_lossy());
473474
}
474475
_ => {}
475476
}
@@ -493,7 +494,7 @@ impl SymbolInfo {
493494

494495
for s in sequences {
495496
if address >= s.start && address < s.end {
496-
println!("found seq: {:x} -> {:x}", s.start, s.end);
497+
info!("found seq: {:x} -> {:x}", s.start, s.end);
497498

498499
let mut sm = line_program.resume_from(&s);
499500

@@ -502,7 +503,7 @@ impl SymbolInfo {
502503
Some(r) => r.get(),
503504
None => 0,
504505
};
505-
println!(
506+
info!(
506507
"row addr {:08x}, line {}, stmt {}, prol_end {}, epi_start {},",
507508
r.address(),
508509
line_num,
@@ -653,7 +654,7 @@ impl SymbolInfo {
653654
};
654655

655656
if tracked {
656-
println!("{:#?}", r);
657+
info!("{:#?}", r);
657658
}
658659

659660
return r;

risc_v_simulator/src/sim/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::marker::PhantomData;
22
use std::path::{Path, PathBuf};
3+
use log::{debug, info};
34

45
use diag::ProfilerStats;
56

@@ -99,7 +100,7 @@ where
99100
if self.machine.state().pc == previous_pc {
100101
end_of_execution_reached = true;
101102
cycles = cycle;
102-
println!("Took {} cycles to finish", cycle);
103+
info!("Took {} cycles to finish", cycle);
103104
break;
104105
}
105106
previous_pc = self.machine.state().pc;
@@ -116,7 +117,7 @@ where
116117
let exec_time = now.elapsed();
117118

118119
if let Some(profiler) = self.profiler.as_mut() {
119-
println!("Profiler begins execution");
120+
info!("Profiler begins execution");
120121
profiler.print_stats();
121122
profiler.write_stacktrace();
122123
}
@@ -189,13 +190,13 @@ pub enum BinarySource<'a> {
189190
impl<'a> BinarySource<'a> {
190191
pub fn to_iter(&self) -> Box<dyn Iterator<Item = u8> + 'a> {
191192
fn read_bin<P: AsRef<Path>>(path: P) -> Vec<u8> {
192-
dbg!(path.as_ref());
193+
debug!("path.as_ref() = \"{}\"", path.as_ref().display());
193194
let mut file = std::fs::File::open(path).expect("must open provided file");
194195
let mut buffer = vec![];
195196
std::io::Read::read_to_end(&mut file, &mut buffer).expect("must read the file");
196197

197198
assert_eq!(buffer.len() % 4, 0);
198-
dbg!(buffer.len() / 4);
199+
debug!("buffer.len() / 4 = {}", buffer.len() / 4);
199200

200201
buffer
201202
}

0 commit comments

Comments
 (0)