int fn_disassemble_xed(xed_syntax_enum_t syntax, char* buf, int buflen, xed_decoded_inst_t* xedd, xed_uint64_t runtime_instruction_address, void* caller_data) { /* the caller_data is for symbolic disam */ char buffer[XED_TMP_BUF_LEN]; int blen = buflen; int ok = xed_format_context(syntax, xedd, buffer, XED_TMP_BUF_LEN, runtime_instruction_address, caller_data); if(ok) blen = xed_strncpy(buf, buffer, buflen); else { blen = buflen; blen = xed_strncpy(buf, "Error disassembling ", blen); blen = xed_strncat(buf, xed_syntax_enum_t2str(syntax), blen); blen = xed_strncat(buf, " syntax.", blen); } return blen; }
void xed_dot_graph_add_instruction( xed_dot_graph_supp_t* gg, xed_decoded_inst_t* xedd, xed_uint64_t runtime_instr_addr, void* caller_data) { /* make a new node for each operand: if read: make edge from src node for that reg to the new node for each operand: if write: install this node as the writer what about partial writes? what about register nesting? */ char disasm_str[XED_DOT_TMP_BUF_LEN]; char* p = 0; size_t alen = 0; int ok; xed_dot_node_t* n = 0; xed_uint32_t remaining_buffer_bytes = XED_DOT_TMP_BUF_LEN; // put addr on separate line in node label #if defined(XED_WINDOWS) ok = sprintf_s(disasm_str, XED_DOT_TMP_BUF_LEN, XED_FMT_LX "\\n", runtime_instr_addr); #else ok = sprintf(disasm_str, XED_FMT_LX "\\n", runtime_instr_addr); #endif assert(ok > 0); alen = strlen(disasm_str); p = disasm_str + alen; remaining_buffer_bytes -= XED_CAST(xed_uint32_t, alen); ok = xed_format_context(gg->syntax, xedd, p, remaining_buffer_bytes, runtime_instr_addr, caller_data); if (!ok) { (void)xed_strncpy(disasm_str,"???", XED_DOT_TMP_BUF_LEN); } n = xed_dot_node(gg->g, disasm_str); add_read_operands(gg,xedd,n); add_write_operands(gg,xedd,n); }
static char* append3(const char* s1, const char* s2, const char* s3) { xed_uint_t n = 1; char* p = 0; if (s1) n += xed_strlen(s1); if (s2) n += xed_strlen(s2); if (s3) n += xed_strlen(s3); p = (char*) malloc(sizeof(char)*n); if (s1) n=xed_strncpy(p,s1,n); if (s2) n=xed_strncat(p,s2,n); if (s3) n=xed_strncat(p,s3,n); return p; }
bool dbg_help_client_t::get_symbol(DWORD64 address, char* symbol_name, int sym_name_buflen, DWORD64* offset) { DWORD64 displacement; ULONG64 n = (sizeof(SYMBOL_INFO) + sym_name_buflen*sizeof(TCHAR) + sizeof(ULONG64) - 1) / sizeof(ULONG64); ULONG64* buffer = new ULONG64[n]; PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer; pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO); pSymbol->MaxNameLen = sym_name_buflen; if (SymFromAddr(hProcess, address, &displacement, pSymbol)) { if (offset) *offset = displacement; if (offset || displacement == 0) { //printf("Name is [%s]\n", pSymbol->Name); xed_strncpy(symbol_name, pSymbol->Name, sym_name_buflen); // force a null. WINDOWS doesn't have strlcpy() symbol_name[sym_name_buflen-1] = 0; delete [] buffer; return 0; } else { /* not at the beginning of a symbol and no offset was supplied */ /* printf("Ignoring. Not at the beginning of a symbol [%s]\n", * pSymbol->Name); */ delete [] buffer; return 1; } } else { error = GetLastError(); printf("SymFromAddr returned error : %d 0x%x for address %llx\n", error, error, address); delete [] buffer; return 1; } }
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; }