Skip to content

Commit

Permalink
ckb-debugger support decode instruction (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
joii2020 authored Jul 19, 2023
1 parent 7aabe00 commit b7ca5cc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
33 changes: 33 additions & 0 deletions ckb-debugger/src/decode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pub fn decode_instruction(data: &str) -> Result<(), Box<dyn std::error::Error>> {
let instruction = if data.starts_with("0x") {
u32::from_str_radix(&data[2..], 16)?
} else {
u32::from_str_radix(data, 10)?
};

use ckb_vm::instructions::*;
use ckb_vm::machine::VERSION2;

let (ins, isa) = if let Some(i) = i::factory::<u64>(instruction, VERSION2) {
let tagged_instruction: tagged::TaggedInstruction = tagged::TaggedInstruction::try_from(i).unwrap();
(tagged_instruction.to_string(), "Integer Instruction")
} else if let Some(i) = m::factory::<u64>(instruction, VERSION2) {
let tagged_instruction = tagged::TaggedInstruction::try_from(i).unwrap();
(tagged_instruction.to_string(), "M extension")
} else if let Some(i) = a::factory::<u64>(instruction, VERSION2) {
let tagged_instruction = tagged::TaggedInstruction::try_from(i).unwrap();
(tagged_instruction.to_string(), "A extension")
} else if let Some(i) = b::factory::<u64>(instruction, VERSION2) {
let tagged_instruction = tagged::TaggedInstruction::try_from(i).unwrap();
(tagged_instruction.to_string(), "B extension")
} else if let Some(i) = rvc::factory::<u64>(instruction, VERSION2) {
let tagged_instruction = tagged::TaggedInstruction::try_from(i).unwrap();
(tagged_instruction.to_string(), "RVC")
} else {
panic!("unknow instruction")
};

println!("instruction: {}\nIt is in RISC-V {}.", ins, isa);

Ok(())
}
16 changes: 15 additions & 1 deletion ckb-debugger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ use std::net::TcpListener;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::{collections::HashSet, io::Read};
mod decode;
mod misc;
use decode::decode_instruction;
use misc::{FileOperation, FileStream, HumanReadableCycles, Random, TimeNow};

#[cfg(feature = "probes")]
Expand Down Expand Up @@ -149,7 +151,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Arg::with_name("tx-file")
.long("tx-file")
.short("f")
.required_unless("bin")
.required_unless_one(&["bin", "decode"])
.help("Filename containing JSON formatted transaction dump")
.takes_value(true),
)
Expand All @@ -160,8 +162,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.takes_value(true),
)
.arg(Arg::with_name("args").multiple(true))
.arg(
Arg::with_name("decode")
.long("decode")
.help("Decode RISC-V instruction")
.takes_value(true)
.conflicts_with_all(&["bin", "tx-file"]),
)
.get_matches();

let matches_decode = matches.value_of_lossy("decode");
if matches_decode.is_some() {
return decode_instruction(&matches_decode.unwrap());
}

let matches_bin = matches.value_of("bin");
let matches_cell_index = matches.value_of("cell-index");
let matches_cell_type = matches.value_of("cell-type");
Expand Down

0 comments on commit b7ca5cc

Please sign in to comment.