A dynamic tracer for Linux

ir.h 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef _PLY_IR_H
  2. #define _PLY_IR_H
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <linux/bpf.h>
  6. #define INSN(_code, _dst, _src, _off, _imm) \
  7. ((struct bpf_insn) { \
  8. .code = _code, \
  9. .dst_reg = _dst, \
  10. .src_reg = _src, \
  11. .off = _off, \
  12. .imm = _imm \
  13. })
  14. #define MOV(_dst, _src) INSN(BPF_ALU64 | BPF_MOV | BPF_X, _dst, _src, 0, 0)
  15. #define MOV_IMM(_dst, _imm) INSN(BPF_ALU64 | BPF_MOV | BPF_K, _dst, 0, 0, _imm)
  16. #define EXIT INSN(BPF_JMP | BPF_EXIT, 0, 0, 0, 0)
  17. #define CALL(_imm) INSN(BPF_JMP | BPF_CALL, 0, 0, 0, _imm)
  18. #define JMP(_op, _dst, _src, _off) INSN(BPF_JMP | BPF_OP((_op)) | BPF_X, _dst, _src, _off, 0)
  19. #define JMP_IMM(_op, _dst, _imm, _off) INSN(BPF_JMP | BPF_OP((_op)) | BPF_K, _dst, 0, _off, _imm)
  20. #define ALU(_op, _dst, _src) INSN(BPF_ALU64 | BPF_OP((_op)) | BPF_X, _dst, _src, 0, 0)
  21. #define ALU_IMM(_op, _dst, _imm) INSN(BPF_ALU64 | BPF_OP((_op)) | BPF_K, _dst, 0, 0, _imm)
  22. #define STW_IMM(_dst, _off, _imm) INSN(BPF_ST | BPF_SIZE(BPF_W) | BPF_MEM, _dst, 0, _off, _imm)
  23. #define STXB(_dst, _off, _src) INSN(BPF_STX | BPF_SIZE(BPF_B) | BPF_MEM, _dst, _src, _off, 0)
  24. #define STXH(_dst, _off, _src) INSN(BPF_STX | BPF_SIZE(BPF_H) | BPF_MEM, _dst, _src, _off, 0)
  25. #define STXW(_dst, _off, _src) INSN(BPF_STX | BPF_SIZE(BPF_W) | BPF_MEM, _dst, _src, _off, 0)
  26. #define STXDW(_dst, _off, _src) INSN(BPF_STX | BPF_SIZE(BPF_DW) | BPF_MEM, _dst, _src, _off, 0)
  27. #define LDXB(_dst, _off, _src) INSN(BPF_LDX | BPF_SIZE(BPF_B) | BPF_MEM, _dst, _src, _off, 0)
  28. #define LDXH(_dst, _off, _src) INSN(BPF_LDX | BPF_SIZE(BPF_H) | BPF_MEM, _dst, _src, _off, 0)
  29. #define LDXW(_dst, _off, _src) INSN(BPF_LDX | BPF_SIZE(BPF_W) | BPF_MEM, _dst, _src, _off, 0)
  30. #define LDXDW(_dst, _off, _src) INSN(BPF_LDX | BPF_SIZE(BPF_DW) | BPF_MEM, _dst, _src, _off, 0)
  31. typedef struct sym sym_t;
  32. typedef enum vitype {
  33. VI_INSN,
  34. VI_LDMAP,
  35. VI_LABEL,
  36. VI_REG_GET,
  37. VI_REG_PUT,
  38. } vitype_t;
  39. typedef struct vinsn {
  40. vitype_t vitype;
  41. union {
  42. struct {
  43. struct bpf_insn bpf;
  44. uint16_t dst;
  45. uint16_t src;
  46. } insn;
  47. int16_t label;
  48. uint16_t reg;
  49. struct {
  50. uint16_t reg;
  51. sym_t *sym;
  52. } map;
  53. };
  54. } vinsn_t;
  55. typedef struct ir {
  56. vinsn_t *vi;
  57. size_t len;
  58. int16_t next_label;
  59. uint16_t next_reg;
  60. } ir_t;
  61. void insn_dump(struct bpf_insn insn, FILE *fp);
  62. void vinsn_dump(vinsn_t *vi, FILE *fp);
  63. void ir_dump(ir_t *ir, FILE *fp);
  64. int16_t ir_alloc_label (ir_t *ir);
  65. uint16_t ir_alloc_register(ir_t *ir);
  66. void ir_emit_insn (ir_t *ir, struct bpf_insn bpf, uint16_t dst, uint16_t src);
  67. void ir_emit_ldmap (ir_t *ir, uint16_t dst, sym_t *map);
  68. void ir_emit_label (ir_t *ir, int16_t label);
  69. void ir_emit_reg_get(ir_t *ir, uint16_t reg);
  70. void ir_emit_reg_put(ir_t *ir, uint16_t reg);
  71. static inline void ir_emit_call(ir_t *ir, enum bpf_func_id func)
  72. {
  73. ir_emit_reg_get(ir, BPF_REG_0);
  74. ir_emit_reg_get(ir, BPF_REG_1);
  75. ir_emit_reg_get(ir, BPF_REG_2);
  76. ir_emit_reg_get(ir, BPF_REG_3);
  77. ir_emit_reg_get(ir, BPF_REG_4);
  78. ir_emit_reg_get(ir, BPF_REG_5);
  79. ir_emit_insn(ir, CALL(func), 0, 0);
  80. ir_emit_reg_put(ir, BPF_REG_5);
  81. ir_emit_reg_put(ir, BPF_REG_4);
  82. ir_emit_reg_put(ir, BPF_REG_3);
  83. ir_emit_reg_put(ir, BPF_REG_2);
  84. ir_emit_reg_put(ir, BPF_REG_1);
  85. ir_emit_reg_put(ir, BPF_REG_0);
  86. }
  87. ir_t *ir_new(void);
  88. typedef enum irloc {
  89. LOC_IMM = (1 << 0),
  90. LOC_REG = (1 << 1),
  91. LOC_STACK = (1 << 2),
  92. } irloc_t;
  93. typedef struct irstate {
  94. int loc;
  95. uint16_t reg;
  96. int16_t stack;
  97. } irstate_t;
  98. static inline void irs_alloc_reg(irstate_t *irs, ir_t *ir)
  99. {
  100. irs->loc = LOC_REG;
  101. irs->reg = ir_alloc_register(ir);
  102. ir_emit_reg_get(ir, irs->reg);
  103. }
  104. #endif /* _PLY_IR_H */