to_json.rs 2.8 KB

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