to_json.rs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2017 Quentin Monnet <quentin.monnet@6wind.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0> or
  4. // the MIT license <http://opensource.org/licenses/MIT>, at your option. This file may not be
  5. // copied, modified, or distributed except according to those terms.
  6. #[macro_use]
  7. extern crate json;
  8. extern crate byteorder;
  9. extern crate elf;
  10. use std::path::PathBuf;
  11. extern crate rbpf;
  12. use rbpf::disassembler;
  13. // Turn a program into a JSON string.
  14. //
  15. // Relies on `json` crate.
  16. //
  17. // You may copy this function and adapt it according to your needs. For instance, you may want to:
  18. //
  19. // * Remove the "desc" (description) attributes from the output.
  20. // * Print integers as integers, and not as strings containing their hexadecimal representation
  21. // (just replace the relevant `format!()` calls by the commented values.
  22. fn to_json(prog: &std::vec::Vec<u8>) -> String {
  23. // This call returns a high-level representation of the instructions, with the two parts of
  24. // `LD_DW_IMM` instructions merged, and name and descriptions of the instructions.
  25. // If you prefer to use a lower-level representation, use `ebpf::to_insn_vec()` function
  26. // instead.
  27. let insns = disassembler::to_insn_vec(&prog);
  28. let mut json_insns = vec![];
  29. for insn in insns {
  30. json_insns.push(object!(
  31. "opc" => format!("{:#x}", insn.opc), // => insn.opc,
  32. "dst" => format!("{:#x}", insn.dst), // => insn.dst,
  33. "src" => format!("{:#x}", insn.src), // => insn.src,
  34. "off" => format!("{:#x}", insn.off), // => insn.off,
  35. // Warning: for imm we use a i64 instead of a i32 (to have correct values for
  36. // `lddw` operation. If we print a number in the JSON this is not a problem, the
  37. // internal i64 has the same value with extended sign on 32 most significant bytes.
  38. // If we print the hexadecimal value as a string however, we want to cast as a i32
  39. // to prevent all other instructions to print spurious `ffffffff` prefix if the
  40. // number is negative. When values takes more than 32 bits with `lddw`, the cast
  41. // has no effect and the complete value is printed anyway.
  42. "imm" => format!("{:#x}", insn.imm as i32), // => insn.imm,
  43. "desc" => format!("{}", insn.desc)
  44. )
  45. );
  46. }
  47. json::stringify_pretty(object!(
  48. "size" => json_insns.len(),
  49. "insns" => json_insns
  50. ), 4)
  51. }
  52. // Load a program from an object file, and prints it to standard output as a JSON string.
  53. fn main() {
  54. // Let's reuse this file from `load_elf` example.
  55. let filename = "examples/load_elf__block_a_port.o";
  56. let path = PathBuf::from(filename);
  57. let file = match elf::File::open_path(&path) {
  58. Ok(f) => f,
  59. Err(e) => panic!("Error: {:?}", e),
  60. };
  61. let text_scn = match file.get_section(".classifier") {
  62. Some(s) => s,
  63. None => panic!("Failed to look up .classifier section"),
  64. };
  65. let ref prog = &text_scn.data;
  66. println!("{}", to_json(&prog));
  67. }