to_json.rs 3.0 KB

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