Debian Codegraph @

cstool/cstool_bpf.c

raw symbols SCIP
1 #include <stdio.h>
2
3 #include <capstone/capstone.h>
4 #include <capstone/platform.h>
5 #include "cstool.h"
6
7 static const char * ext_name[] = {
8 [BPF_EXT_LEN] = "#len",
9 };
10
11 void print_insn_detail_bpf(csh handle, cs_insn *ins)
12 {
13 unsigned i;
14 cs_bpf *bpf;
15 cs_regs regs_read, regs_write;
16 uint8_t regs_read_count, regs_write_count;
17
18 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
19 if (ins->detail == NULL)
20 return;
21
22 bpf = &(ins->detail->bpf);
23
24 printf("\tOperand count: %u\n", bpf->op_count);
25
26 for (i = 0; i < bpf->op_count; i++) {
27 cs_bpf_op *op = &(bpf->operands[i]);
28 printf("\t\toperands[%u].type: ", i);
29 switch (op->type) {
30 case BPF_OP_INVALID:
31 printf("INVALID\n");
32 break;
33 case BPF_OP_REG:
34 printf("REG = %s\n", cs_reg_name(handle, op->reg));
35 break;
36 case BPF_OP_IMM:
37 printf("IMM = 0x%" PRIx64 "\n", op->imm);
38 break;
39 case BPF_OP_OFF:
40 printf("OFF = +0x%x\n", op->off);
41 break;
42 case BPF_OP_MEM:
43 printf("MEM\n");
44 if (op->mem.base != BPF_REG_INVALID)
45 printf("\t\t\toperands[%u].mem.base: REG = %s\n",
46 i, cs_reg_name(handle, op->mem.base));
47 printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
48 break;
49 case BPF_OP_MMEM:
50 printf("MMEM = M[0x%x]\n", op->mmem);
51 break;
52 case BPF_OP_MSH:
53 printf("MSH = 4*([0x%x]&0xf)\n", op->msh);
54 break;
55 case BPF_OP_EXT:
56 printf("EXT = %s\n", ext_name[op->ext]);
57 break;
58 }
59 }
60
61 /* print all registers that are involved in this instruction */
62 if (!cs_regs_access(handle, ins,
63 regs_read, &regs_read_count,
64 regs_write, &regs_write_count)) {
65 if (regs_read_count) {
66 printf("\tRegisters read:");
67 for(i = 0; i < regs_read_count; i++)
68 printf(" %s", cs_reg_name(handle, regs_read[i]));
69 printf("\n");
70 }
71
72 if (regs_write_count) {
73 printf("\tRegisters modified:");
74 for(i = 0; i < regs_write_count; i++)
75 printf(" %s", cs_reg_name(handle, regs_write[i]));
76 printf("\n");
77 }
78 }
79 }