void dump_bfin_trace_buffer(void) { #ifdef CONFIG_DEBUG_VERBOSE #ifdef CONFIG_DEBUG_BFIN_HWTRACE_ON int tflags, i = 0; char buf[150]; unsigned short *addr; #ifdef CONFIG_DEBUG_BFIN_HWTRACE_EXPAND int j, index; #endif trace_buffer_save(tflags); printk(KERN_NOTICE "Hardware Trace:\n"); #ifdef CONFIG_DEBUG_BFIN_HWTRACE_EXPAND printk(KERN_NOTICE "WARNING: Expanded trace turned on - can not trace exceptions\n"); #endif if (likely(bfin_read_TBUFSTAT() & TBUFCNT)) { for (; bfin_read_TBUFSTAT() & TBUFCNT; i++) { decode_address(buf, (unsigned long)bfin_read_TBUF()); printk(KERN_NOTICE "%4i Target : %s\n", i, buf); addr = (unsigned short *)bfin_read_TBUF(); decode_address(buf, (unsigned long)addr); printk(KERN_NOTICE " Source : %s ", buf); decode_instruction(addr); printk("\n"); } } #ifdef CONFIG_DEBUG_BFIN_HWTRACE_EXPAND if (trace_buff_offset) index = trace_buff_offset / 4; else index = EXPAND_LEN; j = (1 << CONFIG_DEBUG_BFIN_HWTRACE_EXPAND_LEN) * 128; while (j) { decode_address(buf, software_trace_buff[index]); printk(KERN_NOTICE "%4i Target : %s\n", i, buf); index -= 1; if (index < 0 ) index = EXPAND_LEN; decode_address(buf, software_trace_buff[index]); printk(KERN_NOTICE " Source : %s ", buf); decode_instruction((unsigned short *)software_trace_buff[index]); printk("\n"); index -= 1; if (index < 0) index = EXPAND_LEN; j--; i++; } #endif trace_buffer_restore(tflags); #endif #endif }
static void handle_sigill ( struct mon_t *mon, int signo ) { struct instruction_t i; bit32u_t saved_eip; bool_t b; ASSERT ( mon != NULL ); if ( ! is_undefined_instruction ( mon->regs->user.eip ) ) { Monitor_print_detail ( stderr, mon ); } ASSERT ( is_undefined_instruction ( mon->regs->user.eip ) ); saved_eip = mon->regs->user.eip; skip_undefined_instruction ( mon ); i = decode_instruction ( mon->regs->user.eip ); ASSERT ( ( i.is_sensitive ) || ( i.is_locked ) ); b = check_instr_fetch ( mon, &i, saved_eip ); if ( b ) { return; } if ( i.is_locked ) { /* [TODO] xchg instruction 実行時の処理 */ sync_shared_memory ( mon, &i ); skip_lock_prefix ( mon ); } else { emulate_instruction ( mon, &i ); } }
void CompiledMethod::print_code_on(Stream* st) { int end_offset = RelocationReader::code_length(this); for (int offset = 0; offset < end_offset; offset += 2) { short* instruction_start = (short*) (entry() + offset); print_comment_for(offset, st); bool is_oop = false; for (RelocationReader stream(this); !stream.at_end(); stream.advance()) { if (stream.code_offset() == offset) { if (stream.is_oop() || stream.is_rom_oop()) { is_oop = true; break; } } } st->print(" %4d: ", offset); if (VerbosePointers) { st->print("0x%08x: ", instruction_start); } if (!is_oop) { decode_instruction(st, instruction_start, offset); } else { GUARANTEE(((int)instruction_start & 0x3) == 0, "Disassembler: Invalid embedded Oop"); Oop o = (OopDesc*)*(int*)instruction_start; if (VerbosePointers) { st->print("0x%08x:", *(int*)instruction_start); } o.print_value_on(st); offset += 2; } st->cr(); } }
void update_view(CPU& cpu, WINDOW *mem_win, WINDOW *a_win, WINDOW *x_win, WINDOW *y_win, WINDOW *p_win, WINDOW *sp_win) { for (uint16_t i=0; i<=20; ++i) { uint16_t pc = cpu.PC + i - 10; uint8_t insVal = cpu.read_mem_val(pc); Instruction ins = decode_instruction(insVal); if (pc == cpu.PC) { wattron(mem_win, COLOR_PAIR(1)); } mvwprintw(mem_win, i+1, 1, "%04x: %02x %s %-17s", pc, insVal, optype_str[ins.type], addrmode_str[ins.mode]); if (pc == cpu.PC) { wattroff(mem_win, COLOR_PAIR(1)); } } wrefresh(mem_win); mvwprintw(a_win, 1, 1, "%02x", cpu.A); wrefresh(a_win); mvwprintw(x_win, 1, 1, "%02x", cpu.X); wrefresh(x_win); mvwprintw(y_win, 1, 1, "%02x", cpu.Y); wrefresh(y_win); mvwprintw(p_win, 1, 1, std::bitset<8>(cpu.P).to_string().c_str(), cpu.P); mvwprintw(p_win, 2, 1, "NV-BDIZC"); wrefresh(p_win); mvwprintw(sp_win, 1, 1, "%02x", cpu.SP); wrefresh(sp_win); }
int main() { int retval; Instr instr; retval= decode_instruction(0x045f4ab9, &instr); if (retval != 1) { printf("decode_instruction() returned %d!\n", retval); printf("Test failed\n"); return -1; } if (instr.operation != 1 || instr.addr_or_const != 0x5f4 || instr.reg1 != 0xa || instr.reg2 != 0xb || instr.reg3 != 0x9) { printf("Incorrect decoding of instruction.\n"); printf(" Operation: %d\n", instr.operation); printf(" Addr/Const: %x\n", instr.addr_or_const); printf(" Register 1: %d\n", instr.reg1); printf(" Register 2: %d\n", instr.reg2); printf(" Register 3: %d\n", instr.reg3); printf("Test failed\n"); return -1; } printf("Test finished!\n"); return 0; }
void execute(Processor *processor,int prompt,int print) { Instruction instruction; /* fetch an instruction */ instruction.bits = load(memory,processor->PC,LENGTH_WORD); /* interactive-mode prompt */ if(prompt) { if(prompt==1) { printf("simulator paused,enter to continue..."); while(getchar()!='\n'); } printf("%08x: ",processor->PC); decode_instruction(instruction); } execute_instruction(instruction,processor,memory); // enforce $0 being hard-wired to 0 processor->R[0] = 0; // print trace if(print) { int i,j; for(i=0;i<8;i++) { for(j=0;j<4;j++) { printf("r%2d=%08x ",i*4+j,processor->R[i*4+j]); } puts(""); } } }
int main() { // Read newline-separated hex strings from STDIN // Prints disassembled mips code to STDOUT Instruction ins; unsigned int i; while (scanf("%x", &i) != EOF) { ins.bits = i; decode_instruction(ins); } return 0; }
// load program "fn" into memory void begin(uint8_t* mem,size_t memsize,const char* fn,int disasm) { int fd,i,j; size_t size; struct stat s; char* buf; const Elf32_Ehdr* eh; const Elf32_Shdr* sh; const Elf32_Phdr* ph; fd = open(fn,O_RDONLY); demand(fd != -1,"Couldn't open executable file %s!",fn); demand(fstat(fd,&s) != -1,"Couldn't access executable file %s!",fn); size = s.st_size; buf = (char*)mmap(NULL,size,PROT_READ,MAP_PRIVATE,fd,0); eh = (const Elf32_Ehdr*)buf; demand(buf != MAP_FAILED,"Couldn't read executable file %s!",fn); close(fd); demand(size >= sizeof(Elf32_Ehdr) && strncmp((const char*)eh->e_ident,ELFMAG,strlen(ELFMAG)) == 0 && eh->e_ident[EI_CLASS] == ELFCLASS32 && size >= eh->e_phoff + eh->e_phnum*sizeof(Elf32_Ehdr), "Invalid executable file %s!",fn); sh = (const Elf32_Shdr*)(buf+eh->e_shoff); for(i = 0; i < eh->e_shnum; i++,sh++) { if(disasm) { if(sh->sh_type == SHT_PROGBITS && (sh->sh_flags & SHF_EXECINSTR)) { for(j = 0; j < sh->sh_size/4; j++) { printf("%08x: ",sh->sh_offset + 4*j); decode_instruction(*(Instruction*)((uint8_t*)buf + sh->sh_offset + 4*j)); } } } } ph = (const Elf32_Phdr*)(buf+eh->e_phoff); for(i = 0; i < eh->e_phnum; i++,ph++) { if(ph->p_type == SHT_PROGBITS && ph->p_memsz) { demand(size >= ph->p_offset + ph->p_filesz && ph->p_vaddr + ph->p_memsz <= memsize, "Invalid executable file %s!",fn); memcpy(mem + ph->p_vaddr,(uint8_t*)buf + ph->p_offset,ph->p_filesz); memset(mem + ph->p_vaddr+ph->p_filesz,0,ph->p_memsz - ph->p_filesz); } } }
void Disassembler::decode(nmethod* nm, outputStream* st) { #ifndef CORE st = st ? st : tty; st->print_cr("Decoding compiled method " INTPTR_FORMAT ":", nm); st->print("Code:"); st->cr(); if (!load_library()) { st->print_cr("Could not load disassembler"); return; } sparc_env env(nm, st); unsigned char* p = nm->instructions_begin(); unsigned char* end = nm->instructions_end(); assert ((((intptr_t)p | (intptr_t)end) % sizeof(int) == 0), "misaligned insn addr"); unsigned char *p1 = p; int total_bucket_count = 0; while (p1 < end && p1) { unsigned char *p0 = p1; ++p1; address bucket_pc = FlatProfiler::bucket_start_for(p1); if (bucket_pc != NULL && bucket_pc > p0 && bucket_pc <= p1) total_bucket_count += FlatProfiler::bucket_count_for(p0); } while (p < end && p) { if (p == nm->entry_point()) st->print_cr("[Entry Point]"); if (p == nm->verified_entry_point()) st->print_cr("[Verified Entry Point]"); if (p == nm->exception_begin()) st->print_cr("[Exception Handler]"); if (p == nm->stub_begin()) st->print_cr("[Stub Code]"); if (p == nm->interpreter_entry_point_or_null()) st->print_cr("[Interpreter Entry Point]"); unsigned char* p0 = p; st->print(" " INTPTR_FORMAT ": ", p); p = decode_instruction(p, &env); nm->print_code_comment_on(st, 40, p0, p); st->cr(); // Output pc bucket ticks if we have any address bucket_pc = FlatProfiler::bucket_start_for(p); if (bucket_pc != NULL && bucket_pc > p0 && bucket_pc <= p) { int bucket_count = FlatProfiler::bucket_count_for(p0); tty->print_cr("%3.1f%% [%d]", bucket_count*100.0/total_bucket_count, bucket_count); tty->cr(); } } #endif }
// The main method that takes in the context and path. void run_raptor_vm(struct raptor_context *context, char *file_path) { // Put the file in context->ram. read_file_to_ram(context, file_path); // Set the IP to 0. context->registers[15] = 0; struct raptor_instruction *instruction = (struct raptor_instruction*)(&context->ram[context->registers[15]]); // While the program still exists. while (instruction->opcode != 0) { // Increase the value of the IP by 4 (length of an instruction). context->registers[15] += 4; // Actually execute the instruction. decode_instruction(context, instruction); // Read from the virtual RAM into an instruction struct. instruction = (struct raptor_instruction*)(&context->ram[context->registers[15]]); } }
static void __handle_other_signals ( struct mon_t *mon, int signo ) { struct instruction_t i; struct segv_info_t si; i = decode_instruction ( mon->regs->user.eip ); assert ( i.opcode != INVALID_OPCODE ); si = try_handle_signal_by_mem_access ( mon, &i ); switch ( si.emu_kind ) { case NO_EMULATION: if ( i.is_sensitive ) { /* A sensitive instruction inserted by a user program is executed. */ emulate_instruction ( mon, &i ); } else { /* An exception other than page-fault exceptions happend. */ #if 1 Print ( stderr, "signo = %s\n", Signo_to_string ( signo ) ); /* [DEBUG] */ Instruction_print ( stderr, &i ); exit ( 1 ); #endif run_emulation_code_of_vm2 ( mon, signo, RAISE_INT ); } break; case PAGEFAULT_EMULATION: raise_pagefault ( mon, &si ); break; case SHMEM_EMULATION: /* necessary emulation procedure has been done. */ break; case HDMEM_EMULATION: case APIC_EMULATION: emulate_instruction ( mon, &i ); break; default: Match_failure ( "__handle_other_signals\n" ); } }
/** * TODO: Turn this into lib functionality to be used in both a seperate disass * binary, and with a flag in the emulator itself. */ int main(int argc, char *argv[]) { // TODO: Use the GNU C args parsing lib if(argc == 1) { printf("Usage todo, RTFM\n"); return 1; } else if(argc > 2) { printf("Only the first argument is used!\n"); // Bah } uint32_t* in_word = malloc(sizeof(uint32_t)); FILE* fp = fopen(argv[1], "rb"); fseek(fp, 0, SEEK_END); int num_bytes = ftell(fp); fseek(fp, 0, SEEK_SET); printf("Disassembling %s (%i bytes):\n\n", argv[1], num_bytes); unsigned char buff[1024]; unsigned int address = 0; while(fread(in_word, sizeof(uint32_t), 1, fp) != 0) { struct Instruction* instruction = malloc(sizeof(struct Instruction)); decode_instruction(in_word, instruction); disassemble_instruction(instruction, (char *)buff); printf("0x%x: %s\n", address, buff); address++; } int res = fclose(fp); if(res != 0) { printf("Error closing %s", argv[1]); } free(in_word); return 0; }
void Disassembler::decode(u_char* begin, u_char* end, outputStream* st) { assert ((((intptr_t)begin | (intptr_t)end) % sizeof(int) == 0), "misaligned insn addr"); st = st ? st : tty; if (!load_library()) { st->print_cr("Could not load disassembler"); return; } sparc_env env(NULL, st); unsigned char* p = (unsigned char*) begin; while (p < (unsigned char*) end && p) { unsigned char* p0 = p; st->print(INTPTR_FORMAT ": ", p); p = decode_instruction(p, &env); if (show_bytes && p) { st->print("\t\t\t"); while (p0 < p) { st->print("%08lx ", *(int*)p0); p0 += sizeof(int); } } st->cr(); } }
static int execute_cycle(UM_machine machine) { Seq_T mapped_segments = machine->address_space->mapped_segments; UM_segment program_segment = Seq_get(mapped_segments, 0); unsigned num_instructions = machine->num_instructions; /* stop if program counter has run out of bounds */ if(machine->program_counter >= num_instructions) return 0; /* retreive the next instruction */ Um_instruction new_instruction = *((Um_instruction *)UArray_at(program_segment->words, machine->program_counter)); /* increment the program counter */ machine->program_counter++; /* decode and execute the instruction retreived */ return decode_instruction(machine, &new_instruction); }
void Disassembler::decode(nmethod* nm, outputStream* st) { #ifndef CORE st = st ? st : tty; st->print_cr("Decoding compiled method " INTPTR_FORMAT ":", nm); st->print("Code:"); st->cr(); if (!load_library()) { st->print_cr("Could not load disassembler"); return; } i486_env env(nm, st); #ifdef COMPILER1 unsigned char* p = nm->code_begin(); #else unsigned char* p = nm->instructions_begin(); #endif unsigned char* end = nm->instructions_end(); while (p < end) { if (p == nm->entry_point()) st->print_cr("[Entry Point]"); if (p == nm->verified_entry_point()) st->print_cr("[Verified Entry Point]"); if (p == nm->exception_begin()) st->print_cr("[Exception Handler]"); if (p == nm->stub_begin()) st->print_cr("[Stub Code]"); unsigned char* p0 = p; st->print(" " INTPTR_FORMAT ": ", p); p = decode_instruction(p, &env); nm->print_code_comment_on(st, 40, p0, p); st->cr(); // Output pc bucket ticks if we have any address bucket_pc = FlatProfiler::bucket_start_for(p); if (bucket_pc != NULL && bucket_pc > p0 && bucket_pc <= p) { int bucket_count = FlatProfiler::bucket_count_for(bucket_pc); tty->print_cr("[%d]", bucket_count); } } #endif }
void Disassembler::decode(u_char* begin, u_char* end, outputStream* st) { st = st ? st : tty; const int show_bytes = false; // for disassembler debugging if (!load_library()) { st->print_cr("Could not load disassembler"); return; } i486_env env(NULL, st); unsigned char* p = (unsigned char*) begin; while (p < (unsigned char*) end) { unsigned char* p0 = p; st->print(INTPTR_FORMAT ": ", p); p = decode_instruction(p, &env); if (show_bytes) { st->print("\t\t\t"); while (p0 < p) st->print("%x ", *p0++); } st->cr(); } }
int main(int argc, char **argv) { int ch; int fd; char * filename = NULL; int interval = 1; int i = 0; signal(SIGUSR1, handle_usr1); signal(SIGUSR2, handle_usr2); (void)memset(&cpu, 0, sizeof(cpu)); while ((ch = getopt(argc, argv, "qvi:f:")) != -1) { switch (ch) { case 'f': filename = optarg; break; case 'i': interval = atoi(optarg); break; case 'v': verbose++; break; case 'q': verbose--; break; default: usage(argv[0]); } } if (filename == NULL) usage(argv[0]); fd = open(filename, O_RDONLY); if (fd < 0) { perror("error opening file"); exit(EXIT_FAILURE); } /* big endian data files */ for (i = 0; i < 0x10000; i++) { uint8_t byte; if (read(fd, &byte, sizeof(byte)) < 0) { break; } cpu.memory[i] = byte << 8; if (read(fd, &byte, sizeof(byte)) < 0) { break; } cpu.memory[i] += byte; } while(1) { word value = cpu.memory[cpu.PC++]; instruction i = decode_instruction(value); handle_instruction(i); if (verbose) dump_registers(); sleep(interval); while(halt) {}; } exit(EXIT_SUCCESS); }
/* this is a debug function used for check bytecode chunk file */ static void dump_function(int level, ktap_proto *f) { int i; printf("\n----------------------------------------------------\n"); printf("function %d [level %d]:\n", function_nr++, level); printf("linedefined: %d\n", f->linedefined); printf("lastlinedefined: %d\n", f->lastlinedefined); printf("numparams: %d\n", f->numparams); printf("is_vararg: %d\n", f->is_vararg); printf("maxstacksize: %d\n", f->maxstacksize); printf("source: %s\n", getstr(f->source)); printf("sizelineinfo: %d \t", f->sizelineinfo); for (i = 0; i < f->sizelineinfo; i++) printf("%d ", f->lineinfo[i]); printf("\n"); printf("sizek: %d\n", f->sizek); for (i = 0; i < f->sizek; i++) { switch(f->k[i].type) { case KTAP_TNIL: printf("\tNIL\n"); break; case KTAP_TBOOLEAN: printf("\tBOOLEAN: "); printf("%d\n", f->k[i].val.b); break; case KTAP_TNUMBER: printf("\tTNUMBER: "); printf("%ld\n", f->k[i].val.n); break; case KTAP_TSHRSTR: case KTAP_TLNGSTR: printf("\tTSTRING: "); printf("%s\n", svalue(&(f->k[i]))); break; default: printf("\tUnknow constant type %d: ", f->k[i].type); kp_showobj(NULL, &(f->k[i])); printf("\n"); } } printf("sizelocvars: %d\n", f->sizelocvars); for (i = 0; i < f->sizelocvars; i++) { printf("\tlocvars: %s startpc: %d endpc: %d\n", getstr(f->locvars[i].varname), f->locvars[i].startpc, f->locvars[i].endpc); } printf("sizeupvalues: %d\n", f->sizeupvalues); for (i = 0; i < f->sizeupvalues; i++) { printf("\tname: %s instack: %d idx: %d\n", getstr(f->upvalues[i].name), f->upvalues[i].instack, f->upvalues[i].idx); } printf("\n"); printf("sizecode: %d\n", f->sizecode); for (i = 0; i < f->sizecode; i++) decode_instruction(f, f->code[i]); printf("sizep: %d\n", f->sizep); for (i = 0; i < f->sizep; i++) dump_function(level + 1, f->p[i]); }
void dump_bfin_trace_buffer(void) { #ifdef CONFIG_DEBUG_BFIN_HWTRACE_ON int tflags, i = 0, fault = 0; char buf[150]; unsigned short *addr; unsigned int cpu = raw_smp_processor_id(); #ifdef CONFIG_DEBUG_BFIN_HWTRACE_EXPAND int j, index; #endif trace_buffer_save(tflags); pr_notice("Hardware Trace:\n"); #ifdef CONFIG_DEBUG_BFIN_HWTRACE_EXPAND pr_notice("WARNING: Expanded trace turned on - can not trace exceptions\n"); #endif if (likely(bfin_read_TBUFSTAT() & TBUFCNT)) { for (; bfin_read_TBUFSTAT() & TBUFCNT; i++) { addr = (unsigned short *)bfin_read_TBUF(); decode_address(buf, (unsigned long)addr); pr_notice("%4i Target : %s\n", i, buf); if (!fault && addr == ((unsigned short *)evt_ivhw)) { addr = (unsigned short *)bfin_read_TBUF(); decode_address(buf, (unsigned long)addr); pr_notice(" FAULT : %s ", buf); decode_instruction(addr); pr_cont("\n"); fault = 1; continue; } if (!fault && addr == (unsigned short *)trap && (cpu_pda[cpu].seqstat & SEQSTAT_EXCAUSE) > VEC_EXCPT15) { decode_address(buf, cpu_pda[cpu].icplb_fault_addr); pr_notice(" FAULT : %s ", buf); decode_instruction((unsigned short *)cpu_pda[cpu].icplb_fault_addr); pr_cont("\n"); fault = 1; } addr = (unsigned short *)bfin_read_TBUF(); decode_address(buf, (unsigned long)addr); pr_notice(" Source : %s ", buf); decode_instruction(addr); pr_cont("\n"); } } #ifdef CONFIG_DEBUG_BFIN_HWTRACE_EXPAND if (trace_buff_offset) index = trace_buff_offset / 4; else index = EXPAND_LEN; j = (1 << CONFIG_DEBUG_BFIN_HWTRACE_EXPAND_LEN) * 128; while (j) { decode_address(buf, software_trace_buff[index]); pr_notice("%4i Target : %s\n", i, buf); index -= 1; if (index < 0) index = EXPAND_LEN; decode_address(buf, software_trace_buff[index]); pr_notice(" Source : %s ", buf); decode_instruction((unsigned short *)software_trace_buff[index]); pr_cont("\n"); index -= 1; if (index < 0) index = EXPAND_LEN; j--; i++; } #endif trace_buffer_restore(tflags); #endif }