p2  0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vm-dis.c
Go to the documentation of this file.
1 
14 #if defined(JIT_DEBUG)
15  printf("-- jit --\n");
16  printf("; function definition: %p ; %u bytes\n", asmb->ptr, asmb->len);
17 # if defined(HAVE_LIBUDIS86) && (POTION_JIT_TARGET == POTION_X86)
18  {
19  ud_t ud_obj;
20 
21  ud_init(&ud_obj);
22  ud_set_input_buffer(&ud_obj, asmb->ptr, asmb->len);
23  ud_set_mode(&ud_obj, __WORDSIZE == 64 ? 64 : 32);
24  ud_set_syntax(&ud_obj, UD_SYN_ATT);
25 
26  while (ud_disassemble(&ud_obj)) {
27  printf("0x%012lx 0x%lx %-24s \t%s\n", (unsigned long)(ud_insn_off(&ud_obj)+(unsigned long)asmb->ptr),
28  (long)ud_insn_off(&ud_obj), ud_insn_hex(&ud_obj), ud_insn_asm(&ud_obj));
29  }
30  }
31 # else
32 # if defined(HAVE_LIBDISTORM64) && (POTION_JIT_TARGET == POTION_X86)
33  {
34  #define MAX_INSTRUCTIONS 2048
35  #define MAX_TEXT_SIZE (60)
36  typedef enum {Decode16Bits = 0, Decode32Bits = 1, Decode64Bits = 2} _DecodeType;
37  typedef enum {DECRES_NONE, DECRES_SUCCESS, DECRES_MEMORYERR, DECRES_INPUTERR} _DecodeResult;
38  typedef long _OffsetType;
39  typedef struct {
40  unsigned int pos;
41  int8_t p[MAX_TEXT_SIZE];
42  } _WString;
43  typedef struct {
44  _WString mnemonic;
45  _WString operands;
46  _WString instructionHex;
47  unsigned int size;
48  _OffsetType offset;
49  } _DecodedInst;
50  _DecodeResult distorm_decode64(_OffsetType,
51  const unsigned char*,
52  long,
53  int,
54  _DecodedInst*,
55  int,
56  unsigned int*);
57 
58  _DecodedInst disassembled[MAX_INSTRUCTIONS];
59  unsigned int decodedInstructionsCount = 0;
60  _OffsetType offset = 0;
61  int i;
62 
63  distorm_decode64(offset,
64  (const unsigned char*)asmb->ptr,
65  asmb->len,
66  PN_SIZE_T == 8 ? Decode64Bits : Decode32Bits,
67  disassembled,
68  MAX_INSTRUCTIONS,
69  &decodedInstructionsCount);
70  for (i = 0; i < decodedInstructionsCount; i++) {
71  printf("0x%012lx 0x%04x (%02d) %-24s %s%s%s\r\n",
72  disassembled[i].offset + (unsigned long)asmb->ptr,
73  (unsigned int)disassembled[i].offset,
74  disassembled[i].size,
75  (char*)disassembled[i].instructionHex.p,
76  (char*)disassembled[i].mnemonic.p,
77  disassembled[i].operands.pos != 0 ? " " : "",
78  (char*)disassembled[i].operands.p);
79  }
80  }
81 # else
82 # if defined(HAVE_LIBDISASM) && (POTION_JIT_TARGET == POTION_X86)
83 # define LINE_SIZE 255
84  {
85  char line[LINE_SIZE];
86  int pos = 0;
87  int size = asmb->len;
88  int insnsize; /* size of instruction */
89  x86_insn_t insn; /* one instruction */
90 
91  // only stable for 32bit
92  x86_init(opt_none, NULL, NULL);
93  while ( pos < size ) {
94  insnsize = x86_disasm(asmb->ptr, size, 0, pos, &insn);
95  if ( insnsize ) {
96  int i;
97  x86_format_insn(&insn, line, LINE_SIZE, att_syntax);
98  printf("0x%x\t", pos);
99  for ( i = 0; i < 10; i++ ) {
100  if ( i < insn.size ) printf("%02x", insn.bytes[i]);
101  else printf(" ");
102  }
103  printf("%s\n", line);
104  pos += insnsize;
105  } else {
106  printf("Invalid instruction at 0x%x. size=0x%x\n", pos, size);
107  pos++;
108  }
109  }
110  x86_cleanup();
111  }
112 #else
113  long ai = 0;
114  for (ai = 0; ai < asmb->len; ai++) {
115  printf("%x ", asmb->ptr[ai]);
116  }
117  printf("\n");
118 # endif
119 # endif
120 # endif
121 #endif
#define __WORDSIZE
Definition: potion.h:101
#define PN_SIZE_T
Definition: config.h:26