void xed_decode_error( xed_uint64_t runtime_instruction_address, xed_uint64_t offset, const xed_uint8_t* ptr, xed_error_enum_t xed_error) { char buf[XED_HEX_BUFLEN]; printf("ERROR: %s Could not decode at offset: 0x" XED_FMT_LX " PC: 0x" XED_FMT_LX ": [", xed_error_enum_t2str(xed_error), offset, runtime_instruction_address); xed_print_hex_line(buf, ptr, 15,XED_HEX_BUFLEN); printf("%s]\n",buf); }
int main(int argc, char** argv) { xed_machine_mode_enum_t mmode; xed_address_width_enum_t stack_addr_width; xed_bool_t long_mode = 0; // create the decoded instruction, and fill in the machine mode (dstate) // make up a simple 2Byte instruction to decode unsigned int bytes = 0; unsigned char itext[15] = { 0xf, 0x85, 0x99, 0x00, 0x00, 0x00 }; // initialize the XED tables -- one time. xed_tables_init(); // The state of the machine -- required for decoding if (long_mode) { mmode=XED_MACHINE_MODE_LONG_64; stack_addr_width = XED_ADDRESS_WIDTH_64b; } else { mmode=XED_MACHINE_MODE_LEGACY_32; stack_addr_width = XED_ADDRESS_WIDTH_32b; } // This is a test of error handling. I vary the instuction length from // 0 bytes to 15 bytes. Normally, you should send in 15 bytes of itext // unless you are near the end of a page and don't want to take a page // fault or tlb miss. Note, you have to reinitialize the xedd each time // you try to decode in to it. // Try different instruction lengths to see when XED recognizes an // instruction as valid. for(bytes = 0;bytes<=15;bytes++) { xed_error_enum_t xed_error; xed_decoded_inst_t xedd; xed_decoded_inst_zero(&xedd); xed_decoded_inst_set_mode(&xedd, mmode, stack_addr_width); xed_error = xed_decode(&xedd, XED_STATIC_CAST(const xed_uint8_t*,itext), bytes); printf("%d %s\n",(int)bytes, xed_error_enum_t2str(xed_error)); } return 0; (void) argc; (void) argv; //pacify compiler }
xed_uint_t disas_decode_encode_binary(const xed_state_t* dstate, const xed_uint8_t* decode_text_binary, const unsigned int bytes, xed_decoded_inst_t* xedd, xed_uint64_t runtime_address) { // decode then encode unsigned int retval_olen = 0; // decode it... xed_bool_t decode_okay = disas_decode_binary(dstate, decode_text_binary, bytes, xedd, runtime_address); if(decode_okay) { xed_error_enum_t encode_okay; xed_uint64_t t1, t2; unsigned int enc_olen, ilen = XED_MAX_INSTRUCTION_BYTES; xed_uint8_t array[XED_MAX_INSTRUCTION_BYTES]; // they are basically the same now xed_encoder_request_t* enc_req = xedd; // convert decode structure to proper encode structure xed_encoder_request_init_from_decode(xedd); // encode it again... t1 = xed_get_time(); encode_okay = xed_encode(enc_req, array, ilen, &enc_olen); t2 = xed_get_time(); if(encode_okay != XED_ERROR_NONE) { if(CLIENT_VERBOSE) { char buf[XED_TMP_BUF_LEN]; char buf2[XED_TMP_BUF_LEN]; int blen = XED_TMP_BUF_LEN; xed_encode_request_print(enc_req, buf, XED_TMP_BUF_LEN); blen = xed_strncpy(buf2, "Could not re-encode: ", blen); blen = xed_strncat(buf2, buf, blen); blen = xed_strncat(buf2, "\nError code was: ", blen); blen = xed_strncat(buf2, xed_error_enum_t2str(encode_okay), blen); blen = xed_strncat(buf2, "\n", blen); xedex_dwarn(buf2); } } else { retval_olen = enc_olen; // See if it matched the original... if(CLIENT_VERBOSE) { char buf[XED_HEX_BUFLEN]; xed_uint_t dec_length; xed_print_hex_line(buf, array, enc_olen, XED_HEX_BUFLEN); printf("Encodable! %s\n", buf); xed_decode_stats_reset(&xed_enc_stats, t1, t2); dec_length = xed_decoded_inst_get_length(xedd); if((enc_olen != dec_length || memcmp(decode_text_binary, array, enc_olen))) { char buf2[XED_TMP_BUF_LEN]; char buf3[XED_TMP_BUF_LEN]; printf("Discrepenacy after re-encoding. dec_len= " XED_FMT_U " ", dec_length); xed_print_hex_line(buf, decode_text_binary, dec_length, XED_HEX_BUFLEN); printf("[%s] ", buf); printf("enc_olen= " XED_FMT_U "", enc_olen); xed_print_hex_line(buf, array, enc_olen, XED_HEX_BUFLEN); printf(" [%s] ", buf); printf("for instruction: "); xed_decoded_inst_dump(xedd, buf3, XED_TMP_BUF_LEN); printf("%s\n", buf3); printf("vs Encode request: "); xed_encode_request_print(enc_req, buf2, XED_TMP_BUF_LEN); printf("%s\n", buf2); } else printf("Identical re-encoding\n"); } } } return retval_olen; }
int main(int argc, char** argv) { xed_error_enum_t xed_error; xed_bool_t long_mode = 0; unsigned int bytes = 0; unsigned char itext[XED_MAX_INSTRUCTION_BYTES]; int i; unsigned int u; xed_decoded_inst_t xedd; #define BUFLEN 1000 char buffer[BUFLEN]; xed_bool_t ok; unsigned int isyntax; xed_syntax_enum_t syntax; xed_machine_mode_enum_t mmode; xed_address_width_enum_t stack_addr_width; xed_format_options_t format_options; // one time initialization xed_tables_init(); xed_set_verbosity( 99 ); memset(&format_options,0, sizeof(format_options)); format_options.hex_address_before_symbolic_name=0; format_options.xml_a=0; format_options.omit_unit_scale=0; for(i=1;i<argc;i++) { if (strcmp(argv[i], "-xml") == 0) format_options.xml_a=1; else if (strcmp(argv[i], "-no-unit-scale") == 0) format_options.omit_unit_scale=1; else if (strcmp(argv[i], "-64") == 0) long_mode = 1; else break; } xed_format_set_options( format_options ); /// begin processing of instructions... if (long_mode) { mmode=XED_MACHINE_MODE_LONG_64; stack_addr_width =XED_ADDRESS_WIDTH_64b; } else { mmode=XED_MACHINE_MODE_LEGACY_32; stack_addr_width =XED_ADDRESS_WIDTH_32b; } xed_decoded_inst_zero(&xedd); xed_decoded_inst_set_mode(&xedd, mmode, stack_addr_width); for( ;i < argc; i++) { xed_uint8_t x = (xed_uint8_t)(xed_atoi_hex(argv[i])); assert(bytes < XED_MAX_INSTRUCTION_BYTES); itext[bytes++] = x; } if (bytes == 0) { fprintf(stderr, "Must supply some hex bytes\n"); exit(1); } printf("PARSING BYTES: "); for( u=0;u<bytes; u++) printf("%02x ", STATIC_CAST(unsigned int,itext[u])); printf("\n"); xed_error = xed_decode(&xedd, REINTERPRET_CAST(const xed_uint8_t*,itext), bytes); switch(xed_error) { case XED_ERROR_NONE: break; case XED_ERROR_BUFFER_TOO_SHORT: fprintf(stderr,"Not enough bytes provided\n"); exit(1); case XED_ERROR_GENERAL_ERROR: fprintf(stderr,"Could not decode given input.\n"); exit(1); default: fprintf(stderr,"Unhandled error code %s\n", xed_error_enum_t2str(xed_error)); exit(1); } xed_decoded_inst_dump(&xedd,buffer, BUFLEN); printf("%s\n",buffer); for(isyntax= XED_SYNTAX_XED; isyntax < XED_SYNTAX_LAST; isyntax++) { syntax = STATIC_CAST(xed_syntax_enum_t, isyntax); ok = xed_format(syntax, &xedd, buffer, BUFLEN, 0); if (ok) printf("%s syntax: %s\n", xed_syntax_enum_t2str(syntax), buffer); else printf("Error disassembling %s syntax\n", xed_syntax_enum_t2str(syntax)); } return 0; }
int main(int argc, char** argv) { xed_error_enum_t xed_error; xed_bool_t long_mode = 0; xed_bool_t prot16 = 0; unsigned int first_argv; unsigned int bytes = 0; unsigned char itext[XED_MAX_INSTRUCTION_BYTES]; int i; unsigned int u; xed_decoded_inst_t xedd; xed_machine_mode_enum_t mmode; xed_address_width_enum_t stack_addr_width; xed_tables_init(); xed_set_verbosity( 99 ); if (argc > 2 && strcmp(argv[1], "-64") == 0) long_mode = 1; else if (argc > 2 && strcmp(argv[1], "-16") == 0) prot16 = 1; if (long_mode) { first_argv = 2; mmode=XED_MACHINE_MODE_LONG_64; stack_addr_width =XED_ADDRESS_WIDTH_64b; } else if (prot16) { first_argv = 2; mmode=XED_MACHINE_MODE_LEGACY_16; stack_addr_width =XED_ADDRESS_WIDTH_16b; } else { first_argv=1; mmode=XED_MACHINE_MODE_LEGACY_32; stack_addr_width =XED_ADDRESS_WIDTH_32b; } xed_decoded_inst_zero(&xedd); xed_decoded_inst_set_mode(&xedd, mmode, stack_addr_width); for( i=first_argv ;i < argc; i++) { xed_uint8_t x = (xed_uint8_t)(xed_atoi_hex(argv[i])); assert(bytes < XED_MAX_INSTRUCTION_BYTES); itext[bytes++] = x; } if (bytes == 0) { fprintf(stderr, "Must supply some hex bytes\n"); exit(1); } printf("PARSING BYTES: "); for( u=0;u<bytes; u++) printf("%02x ", XED_STATIC_CAST(unsigned int,itext[u])); printf("\n"); xed_error = xed_decode(&xedd, XED_REINTERPRET_CAST(const xed_uint8_t*,itext), bytes); switch(xed_error) { case XED_ERROR_NONE: break; case XED_ERROR_BUFFER_TOO_SHORT: fprintf(stderr,"Not enough bytes provided\n"); exit(1); case XED_ERROR_GENERAL_ERROR: fprintf(stderr,"Could not decode given input.\n"); exit(1); default: fprintf(stderr,"Unhandled error code %s\n", xed_error_enum_t2str(xed_error)); exit(1); } print_operands(&xedd); return 0; }
int main(int argc, char** argv) { xed_error_enum_t xed_error; xed_bool_t long_mode = 0; xed_bool_t prot16 = 0; unsigned int first_argv; unsigned int bytes = 0; unsigned char itext[XED_MAX_INSTRUCTION_BYTES]; int i; unsigned int u; xed_decoded_inst_t xedd; #define BUFLEN 1000 char buffer[BUFLEN]; xed_bool_t ok; unsigned int isyntax; xed_syntax_enum_t syntax; xed_machine_mode_enum_t mmode; xed_address_width_enum_t stack_addr_width; xed_encoder_request_t* enc_req; xed_uint8_t array[XED_MAX_INSTRUCTION_BYTES]; unsigned int enc_olen, ilen = XED_MAX_INSTRUCTION_BYTES; xed_error_enum_t encode_okay; xed_bool_t change_to_long_mode = 0; xed_tables_init(); xed_set_verbosity( 99 ); if (argc > 2 && strcmp(argv[1], "-64") == 0) long_mode = 1; else if (argc > 2 && strcmp(argv[1], "-16") == 0) prot16 = 1; if (long_mode) { first_argv = 2; mmode=XED_MACHINE_MODE_LONG_64; stack_addr_width =XED_ADDRESS_WIDTH_64b; } else if (prot16) { first_argv = 2; mmode=XED_MACHINE_MODE_LEGACY_16; stack_addr_width =XED_ADDRESS_WIDTH_16b; } else { first_argv=1; mmode=XED_MACHINE_MODE_LEGACY_32; stack_addr_width =XED_ADDRESS_WIDTH_32b; } xed_decoded_inst_zero(&xedd); xed_decoded_inst_set_mode(&xedd, mmode, stack_addr_width); for( i=first_argv ;i < argc; i++) { xed_uint8_t x = (xed_uint8_t)(xed_atoi_hex(argv[i])); assert(bytes < XED_MAX_INSTRUCTION_BYTES); itext[bytes++] = x; } if (bytes == 0) { fprintf(stderr, "Must supply some hex bytes\n"); exit(1); } printf("PARSING BYTES: "); for( u=0;u<bytes; u++) printf("%02x ", XED_STATIC_CAST(unsigned int,itext[u])); printf("\n"); xed_error = xed_decode(&xedd, XED_REINTERPRET_CAST(const xed_uint8_t*,itext), bytes); switch(xed_error) { case XED_ERROR_NONE: break; case XED_ERROR_BUFFER_TOO_SHORT: fprintf(stderr,"Not enough bytes provided\n"); exit(1); case XED_ERROR_GENERAL_ERROR: fprintf(stderr,"Could not decode given input.\n"); exit(1); default: fprintf(stderr,"Unhandled error code %s\n", xed_error_enum_t2str(xed_error)); exit(1); } //memset(buffer,0,BUFLEN); xed_decoded_inst_dump(&xedd,buffer, BUFLEN); printf("%s\n",buffer); for(isyntax= XED_SYNTAX_XED; isyntax < XED_SYNTAX_LAST; isyntax++) { syntax = XED_STATIC_CAST(xed_syntax_enum_t, isyntax); ok = xed_format_context(syntax, &xedd, buffer, BUFLEN, 0, 0, 0); if (ok) printf("%s syntax: %s\n", xed_syntax_enum_t2str(syntax), buffer); else printf("Error disassembling %s syntax\n", xed_syntax_enum_t2str(syntax)); } printf("\n\nPreparing to encode ...\n"); enc_req = &xedd; // they are basically the same now // convert decode structure to proper encode structure xed_encoder_request_init_from_decode(&xedd); change_to_long_mode = 0; if (change_to_long_mode) { // change to 64b mode xed_state_t state; xed_operand_values_t* ov; xed_state_init2(&state,XED_MACHINE_MODE_LONG_64,XED_ADDRESS_WIDTH_64b); ov = xed_decoded_inst_operands(&xedd); xed_operand_values_set_mode(ov, &state); xed_encoder_request_set_effective_address_size(enc_req, 64); // need to fix base regs... //xed_operand_values_set_operand_reg(ov, XED_OPERAND_BASE0, XED_REG_RSI); xed_encoder_request_set_effective_operand_width(enc_req, 32); } printf("Encoding...\n"); encode_okay = xed_encode(enc_req, array, ilen, &enc_olen); if (encode_okay != XED_ERROR_NONE) { printf("Error code = %s\n", xed_error_enum_t2str(encode_okay)); } else { unsigned int j; printf("Encodable: "); for(j=0;j<enc_olen;j++) { printf("%02x", array[j]); } printf("\n"); } return 0; }
int main(int argc, char** argv) { xed_error_enum_t xed_error; xed_bool_t long_mode = 0; xed_bool_t real_mode = 0; xed_bool_t protected_16 = 0; xed_state_t dstate; unsigned int first_argv; unsigned int bytes = 0; unsigned char itext[XED_MAX_INSTRUCTION_BYTES]; int i; unsigned int u; xed_decoded_inst_t xedd; #define BUFLEN 1000 char buffer[BUFLEN]; xed_bool_t ok; unsigned int isyntax; xed_syntax_enum_t syntax; unsigned int memop_index = 0; unsigned int memops = 0; xed_uint64_t out_addr = 0; xed_tables_init(); xed_agen_register_callback( register_callback, segment_callback); xed_state_zero(&dstate); xed_set_verbosity( 99 ); if (argc > 2 && strcmp(argv[1], "-64") == 0) long_mode = 1; if (argc > 2 && strcmp(argv[1], "-r") == 0) real_mode = 1; if (argc > 2 && strcmp(argv[1], "-16") == 0) protected_16 = 1; if (long_mode) { first_argv = 2; dstate.mmode=XED_MACHINE_MODE_LONG_64; } else if (protected_16) { first_argv = 2; xed_state_init(&dstate, XED_MACHINE_MODE_LEGACY_16, XED_ADDRESS_WIDTH_16b, XED_ADDRESS_WIDTH_16b); } else if (real_mode) { first_argv = 2; /* we say that real mode uses 16b addressing even though the addresses returned are 20b long. */ xed_state_init(&dstate, XED_MACHINE_MODE_REAL_16, XED_ADDRESS_WIDTH_16b, XED_ADDRESS_WIDTH_16b); } else { first_argv=1; xed_state_init(&dstate, XED_MACHINE_MODE_LEGACY_32, XED_ADDRESS_WIDTH_32b, XED_ADDRESS_WIDTH_32b); } xed_decoded_inst_zero_set_mode(&xedd, &dstate); for( i=first_argv ; i < argc; i++) { xed_uint8_t x = (xed_uint8_t)(xed_atoi_hex(argv[i])); assert(bytes < XED_MAX_INSTRUCTION_BYTES); itext[bytes++] = x; } if (bytes == 0) { fprintf(stderr, "Must supply some hex bytes\n"); exit(1); } printf("PARSING BYTES: "); for( u=0; u<bytes; u++) printf("%02x ", STATIC_CAST(unsigned int,itext[u])); printf("\n"); xed_error = xed_decode(&xedd, REINTERPRET_CAST(const xed_uint8_t*,itext), bytes); switch(xed_error) { case XED_ERROR_NONE: break; case XED_ERROR_BUFFER_TOO_SHORT: fprintf(stderr,"Not enough bytes provided\n"); exit(1); case XED_ERROR_GENERAL_ERROR: fprintf(stderr,"Could not decode given input.\n"); exit(1); default: fprintf(stderr,"Unhandled error code %s\n", xed_error_enum_t2str(xed_error)); exit(1); } xed_decoded_inst_dump(&xedd,buffer, BUFLEN); printf("%s\n",buffer); for(isyntax= XED_SYNTAX_XED; isyntax < XED_SYNTAX_LAST; isyntax++) { syntax = STATIC_CAST(xed_syntax_enum_t, isyntax); ok = xed_format(syntax, &xedd, buffer, BUFLEN, 0); if (ok) printf("%s syntax: %s\n", xed_syntax_enum_t2str(syntax), buffer); else printf("Error disassembling %s syntax\n", xed_syntax_enum_t2str(syntax)); } memops = xed_decoded_inst_number_of_memory_operands(&xedd); printf("\nNumber of memory operands: %d\n", (int)memops); for(memop_index=0; memop_index<memops; memop_index++) { xed_error = xed_agen(&xedd, memop_index, 0, &out_addr); if (xed_error != XED_ERROR_NONE) { fprintf(stderr,"Agen error code %s\n", xed_error_enum_t2str(xed_error)); exit(1); } printf("\tMemory agen%d: " XED_FMT_LX "\n", (int)memop_index, out_addr); } return 0; }
static void print_block(struct pt_block *block, struct pt_image_section_cache *iscache, const struct ptxed_options *options, const struct ptxed_stats *stats, uint64_t offset, uint64_t time) { xed_machine_mode_enum_t mode; xed_state_t xed; uint64_t last_ip; if (!block || !options) { printf("[internal error]\n"); return; } if (block->resynced) printf("[overflow]\n"); if (block->enabled) printf("[enabled]\n"); if (block->resumed) printf("[resumed]\n"); if (options->track_blocks) { printf("[block"); if (stats) printf(" %" PRIx64, stats->blocks); printf("]\n"); } mode = translate_mode(block->mode); xed_state_init2(&xed, mode, XED_ADDRESS_WIDTH_INVALID); last_ip = 0ull; for (; block->ninsn; --block->ninsn) { xed_decoded_inst_t inst; xed_error_enum_t xederrcode; uint8_t raw[pt_max_insn_size], *praw; int size, errcode; /* For truncated block, the last instruction is provided in the * block since it can't be read entirely from the image section * cache. */ if (block->truncated && (block->ninsn == 1)) { praw = block->raw; size = block->size; } else { praw = raw; size = pt_iscache_read(iscache, raw, sizeof(raw), block->isid, block->ip); if (size < 0) { printf(" [error reading insn: (%d) %s]\n", size, pt_errstr(pt_errcode(size))); break; } } xed_decoded_inst_zero_set_mode(&inst, &xed); if (block->speculative) printf("? "); if (options->print_offset) printf("%016" PRIx64 " ", offset); if (options->print_time) printf("%016" PRIx64 " ", time); printf("%016" PRIx64, block->ip); xederrcode = xed_decode(&inst, praw, size); if (xederrcode != XED_ERROR_NONE) { printf(" [xed decode error: (%u) %s]\n", xederrcode, xed_error_enum_t2str(xederrcode)); break; } if (!options->dont_print_insn) xed_print_insn(&inst, block->ip, options); printf("\n"); last_ip = block->ip; errcode = xed_next_ip(&block->ip, &inst, last_ip); if (errcode < 0) { diagnose_block_at("reconstruct error", offset, block, errcode); break; } } /* Decode should have brought us to @block->end_ip. */ if (last_ip != block->end_ip) diagnose_block_at("reconstruct error", offset, block, -pte_nosync); if (block->interrupted) printf("[interrupt]\n"); if (block->aborted) printf("[aborted]\n"); if (block->committed) printf("[committed]\n"); if (block->disabled) printf("[disabled]\n"); if (block->stopped) printf("[stopped]\n"); }
static void print_insn(const struct pt_insn *insn, xed_state_t *xed, const struct ptxed_options *options, uint64_t offset, uint64_t time) { if (!insn || !options) { printf("[internal error]\n"); return; } if (insn->resynced) printf("[overflow]\n"); if (insn->enabled) printf("[enabled]\n"); if (insn->resumed) printf("[resumed]\n"); if (insn->speculative) printf("? "); if (options->print_offset) printf("%016" PRIx64 " ", offset); if (options->print_time) printf("%016" PRIx64 " ", time); printf("%016" PRIx64, insn->ip); if (!options->dont_print_insn) { xed_machine_mode_enum_t mode; xed_decoded_inst_t inst; xed_error_enum_t errcode; mode = translate_mode(insn->mode); xed_state_set_machine_mode(xed, mode); xed_decoded_inst_zero_set_mode(&inst, xed); errcode = xed_decode(&inst, insn->raw, insn->size); switch (errcode) { case XED_ERROR_NONE: xed_print_insn(&inst, insn->ip, options); break; default: printf(" [xed decode error: (%u) %s]", errcode, xed_error_enum_t2str(errcode)); break; } } printf("\n"); if (insn->interrupted) printf("[interrupt]\n"); if (insn->aborted) printf("[aborted]\n"); if (insn->committed) printf("[committed]\n"); if (insn->disabled) printf("[disabled]\n"); if (insn->stopped) printf("[stopped]\n"); }
int main(int argc, char** argv) { xed_ild_t ild; xed_uint_t uargc = (xed_uint_t)argc; xed_uint_t length = 0; xed_uint_t dlen = 0; xed_uint_t i,j,input_nibbles=0; xed_uint8_t itext[XED_MAX_INSTRUCTION_BYTES]; char src[MAX_INPUT_NIBBLES+1]; xed_state_t dstate; xed_decoded_inst_t xedd; xed_uint_t first_argv; xed_uint_t bytes; xed_error_enum_t xed_error; xed_chip_enum_t chip = XED_CHIP_INVALID; int already_set_mode = 0; // initialize the XED tables -- one time. xed_tables_init(); xed_state_zero(&dstate); first_argv = 1; dstate.mmode=XED_MACHINE_MODE_LEGACY_32; dstate.stack_addr_width=XED_ADDRESS_WIDTH_32b; for(i=1;i< uargc;i++) { if (strcmp(argv[i], "-64") == 0) { assert(already_set_mode == 0); already_set_mode = 1; dstate.mmode=XED_MACHINE_MODE_LONG_64; first_argv++; } else if (strcmp(argv[i], "-16") == 0) { assert(already_set_mode == 0); already_set_mode = 1; dstate.mmode=XED_MACHINE_MODE_LEGACY_16; dstate.stack_addr_width=XED_ADDRESS_WIDTH_16b; first_argv++; } else if (strcmp(argv[i], "-s16") == 0) { already_set_mode = 1; dstate.stack_addr_width=XED_ADDRESS_WIDTH_16b; first_argv++; } else if (strcmp(argv[i], "-chip") == 0) { assert(i+1 < uargc); chip = str2xed_chip_enum_t(argv[i+1]); printf("Setting chip to %s\n", xed_chip_enum_t2str(chip)); assert(chip != XED_CHIP_INVALID); first_argv+=2; } } assert(first_argv < uargc); xed_decoded_inst_zero_set_mode(&xedd, &dstate); if (first_argv >= uargc) { printf("Need some hex instruction nibbles"); exit(1); } for(i=first_argv;i<uargc;i++) { for(j=0;argv[i][j];j++) { assert(input_nibbles < MAX_INPUT_NIBBLES); src[input_nibbles] = argv[i][j]; input_nibbles++; } } src[input_nibbles] = 0; if (input_nibbles & 1) { printf("Need an even number of nibbles"); exit(1); } bytes = xed_convert_ascii_to_hex(src, itext, XED_MAX_INSTRUCTION_BYTES); printf("Attempting to decode: "); for(i=0;i<bytes;i++) { printf("%02x", itext[i]); } printf("\n"); xed_ild_init(&ild, dstate.mmode, chip, itext, XED_MAX_INSTRUCTION_BYTES); length = xed_instruction_length_decode(&ild); print_ild(&ild); printf("ILD length = %d\n",length); xed_decoded_inst_set_input_chip(&xedd, chip); xed_error = xed_decode(&xedd, XED_REINTERPRET_CAST(const xed_uint8_t*,itext), bytes); switch(xed_error) { case XED_ERROR_NONE: break; case XED_ERROR_BUFFER_TOO_SHORT: printf("Not enough bytes provided\n"); exit(1); case XED_ERROR_INVALID_FOR_CHIP: printf("The instruction was not valid for the specified chip.\n"); exit(1); case XED_ERROR_GENERAL_ERROR: printf("Could not decode given input.\n"); exit(1); default: printf("Unhandled error code %s\n",xed_error_enum_t2str(xed_error)); exit(1); } dlen = xed_decoded_inst_get_length(&xedd); printf ("Traditional length = %d\n", dlen); if (dlen != length) { printf ("Length error\n"); exit(1); } printf ("Length matched\n"); return 0; }
int main(int argc, char** argv) { xed_error_enum_t xed_error; xed_bool_t long_mode = 0; xed_state_t dstate; unsigned int first_argv; unsigned int bytes = 0; unsigned char itext[XED_MAX_INSTRUCTION_BYTES]; int i; unsigned int u; xed_decoded_inst_t xedd; #define BUFLEN 1000 char buffer[BUFLEN]; xed_bool_t ok; unsigned int isyntax; xed_syntax_enum_t syntax; xed_tables_init(); xed_state_zero(&dstate); xed_set_verbosity( 99 ); if (argc > 2 && strcmp(argv[1], "-64") == 0) long_mode = 1; if (long_mode) { first_argv = 2; dstate.mmode=XED_MACHINE_MODE_LONG_64; } else { first_argv=1; xed_state_init(&dstate, XED_MACHINE_MODE_LEGACY_32, XED_ADDRESS_WIDTH_32b, XED_ADDRESS_WIDTH_32b); } xed_decoded_inst_zero_set_mode(&xedd, &dstate); for( i=first_argv ;i < argc; i++) { xed_uint8_t x = (xed_uint8_t)(xed_atoi_hex(argv[i])); assert(bytes < XED_MAX_INSTRUCTION_BYTES); itext[bytes++] = x; } if (bytes == 0) { fprintf(stderr, "Must supply some hex bytes\n"); exit(1); } printf("PARSING BYTES: "); for( u=0;u<bytes; u++) printf("%02x ", STATIC_CAST(unsigned int,itext[u])); printf("\n"); xed_error = xed_decode(&xedd, REINTERPRET_CAST(const xed_uint8_t*,itext), bytes); switch(xed_error) { case XED_ERROR_NONE: break; case XED_ERROR_BUFFER_TOO_SHORT: fprintf(stderr,"Not enough bytes provided\n"); exit(1); case XED_ERROR_GENERAL_ERROR: fprintf(stderr,"Could not decode given input.\n"); exit(1); default: fprintf(stderr,"Unhandled error code %s\n", xed_error_enum_t2str(xed_error)); exit(1); } //memset(buffer,0,BUFLEN); xed_decoded_inst_dump(&xedd,buffer, BUFLEN); printf("%s\n",buffer); for(isyntax= XED_SYNTAX_XED; isyntax < XED_SYNTAX_LAST; isyntax++) { syntax = STATIC_CAST(xed_syntax_enum_t, isyntax); ok = xed_format(syntax, &xedd, buffer, BUFLEN, 0); if (ok) printf("%s syntax: %s\n", xed_syntax_enum_t2str(syntax), buffer); else printf("Error disassembling %s syntax\n", xed_syntax_enum_t2str(syntax)); } return 0; }