Sfoglia il codice sorgente

examples/: document examples `disassemble.rs` and `to_json.rs`

Quentin Monnet 8 anni fa
parent
commit
f219d8fb30
2 ha cambiato i file con 15 aggiunte e 0 eliminazioni
  1. 1 0
      examples/disassemble.rs
  2. 14 0
      examples/to_json.rs

+ 1 - 0
examples/disassemble.rs

@@ -8,6 +8,7 @@
 extern crate rbpf;
 use rbpf::disassembler;
 
+// Simply disassemble a program into human-readable instructions.
 fn main() {
     let prog = vec![
         0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

+ 14 - 0
examples/to_json.rs

@@ -11,8 +11,21 @@ extern crate json;
 extern crate rbpf;
 use rbpf::disassembler;
 
+// Turn a program into a JSON string.
+//
+// Relies on `json` crate.
+//
+// You may copy this function and adapt it according to your needs. For instance, you may want to:
+//
+// * Remove the "desc" (description) attributes from the output.
+// * Print integers as integers, and not as strings containing their hexadecimal representation
+//   (just replace the relevant `format!()` calls by the commented values.
 fn to_json(prog: &std::vec::Vec<u8>) -> String {
 
+    // This call returns a high-level representation of the instructions, with the two parts of
+    // `LD_DW_IMM` instructions merged, and name and descriptions of the instructions.
+    // If you prefer to use a lower-level representation, use `ebpf::to_insn_vec()` function
+    // instead.
     let insns = disassembler::to_insn_vec(&prog);
     let mut json_insns = vec![];
     for insn in insns {
@@ -39,6 +52,7 @@ fn to_json(prog: &std::vec::Vec<u8>) -> String {
         ), 4)
 }
 
+// Print a JSON string representing the program to standard output.
 fn main() {
     let prog = vec![
         0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,