/** * Runs the instruction at the program counters position in memory. */ void vm_exec() { cycles++; Instruction* op = mem_load_instr(pcounter); print_instr(op); switch(op->op) { case OP_NAP: break; case OP_ADD: op_add(op); break; case OP_SUB: op_sub(op); break; case OP_EXP: op_exp(op); break; case OP_SIN: op_sin(op); break; case OP_COS: op_cos(op); break; case OP_TAN: op_tan(op); break; case OP_LW: op_lw(op); break; case OP_SW: op_sw(op); break; case OP_SB: op_sb(op); break; case OP_J: op_j(op); return; // Jump case OP_JR: op_jr(op); return; case OP_JAL: op_jal(op); return; case OP_JZ: op_jz(op); return; case OP_JNZ: op_jnz(op); return; case OP_JEQ: op_jeq(op); return; case OP_JNQ: op_jnq(op); return; case OP_PUSH: op_push(op); break; case OP_POP: op_pop(op); break; case OP_PRTI: op_prti(op); break; case OP_EXIT: vm_exit(op->k); return; /* Unsupported operation */ default: printf("Error: Invalid operation at %d\n", pcounter); print_instr(op); vm_exit(-1); return; } pcounter = pcounter + 8; }
void InstructionPrinter::print_line(Instruction* instr) { // print instruction data on one line if (instr->is_pinned()) tty->put('.'); fill_to(bci_pos ); tty->print("%d", instr->bci()); fill_to(use_pos ); tty->print("%d", instr->use_count()); fill_to(temp_pos ); print_temp(instr); fill_to(instr_pos); print_instr(instr); tty->cr(); // add a line for StateSplit instructions w/ non-empty stacks // (make it robust so we can print incomplete instructions) StateSplit* split = instr->as_StateSplit(); if (split != NULL && split->state() != NULL && !split->state()->stack_is_empty()) { fill_to(instr_pos); print_stack(split->state()); tty->cr(); } }
void jellyfish::pretty_dump() const { // get pc s32 pc=peekix(REG_PCO)%m_heap_size; s32 stop=peekix(REG_STP); printf("-- prog -- pc:%d\n",pc); for (s32 n=pc-7; n<pc+7; n++) { if (n>=0 || n<m_heap_size) { if (n==pc) { printf("%03d > ",n); } else { printf("%03d ",n); } print_instr(n); printf("\n"); } } printf("-- stk -- stk:%d\n",stop); for (u32 n=REG_STK+stop-2; n<REG_STK+stop+2; n++) { char txt[2048]; if (n==stop+REG_STK) { sprintf(txt,"%s","> %f %f %f : %d\n"); } else { sprintf(txt,"%s"," %f %f %f : %d\n"); } printf(txt, (float)m_heap[n].x, (float)m_heap[n].y, (float)m_heap[n].z, n); } }
void jellyfish::simple_dump() const { s32 pc=peekix(REG_PCO)%m_heap_size; for (u32 n=0; n<m_heap_size; n++) { if (n==pc) { printf("> "); } else { printf(" "); } print_instr(n); printf(" : %d\n",n); } }
void Shade::disassemble_code(void *code, void *target, size_t size) { instr_t instr; byte *pos = (byte *)code; byte *stop = (byte *)code + size; while(pos < stop) { instr_init(0, &instr); byte *next = decode_from_copy(0, pos, (byte *)target + (pos - (byte *)code), &instr); print_instr(pos - (byte *)code + (byte *)target, &instr); pos = next; instr_free(0, &instr); } }
void print_text_segment() { int i; instr_t *instr; printf("\n"); printf(".text\n"); printf(".global main\n"); for(i=0;i<MAX_NUM_OF_MODULES;i++) { instr = final_tree[i]; if (!instr) { return; } free_all_registers(); while (instr) { register_allocate(instr); print_instr(instr); register_free(instr); instr = instr->next; } printf("\n"); } }
void print_func_body(Output& out, const FuncInfo& finfo) { auto const func = finfo.func; auto lblIter = begin(finfo.labels); auto const lblStop = end(finfo.labels); auto ehIter = begin(finfo.ehStarts); auto const ehStop = end(finfo.ehStarts); auto bcIter = func->unit()->at(func->base()); auto const bcStop = func->unit()->at(func->past()); min_priority_queue<Offset> ehEnds; while (bcIter != bcStop) { auto const pop = reinterpret_cast<const Op*>(bcIter); auto const off = func->unit()->offsetOf(pop); // First, close any protected EH regions that are past-the-end at // this offset. while (!ehEnds.empty() && ehEnds.top() == off) { ehEnds.pop(); out.dec_indent(); out.fmtln("}}"); } // Next, open any new protected regions that start at this offset. for (; ehIter != ehStop && ehIter->first == off; ++ehIter) { auto const info = finfo.ehInfo.find(ehIter->second); always_assert(info != end(finfo.ehInfo)); match<void>( info->second, [&] (const EHCatch& catches) { out.indent(); out.fmt(".try_catch"); for (auto& kv : catches.blocks) { out.fmt(" ({} {})", kv.first, kv.second); } out.fmt(" {{"); out.nl(); }, [&] (const EHFault& fault) { out.fmtln(".try_fault {} {{", fault.label); } ); out.inc_indent(); ehEnds.push(ehIter->second->m_past); } // Then, print labels if we have any. This order keeps the labels // from dangling on weird sides of .try_fault or .try_catch // braces. while (lblIter != lblStop && lblIter->first < off) ++lblIter; if (lblIter != lblStop && lblIter->first == off) { out.dec_indent(); out.fmtln("{}:", lblIter->second); out.inc_indent(); } print_instr(out, finfo, bcIter); bcIter += instrLen(reinterpret_cast<const Op*>(bcIter)); } }
int main(int argc, char *argv[]) { FILE *fp; int i, pc_init; Program pm[50000]; float x = 0.0; fpin = stdin; fpout = stdout; for(i = 0; i < 32; i++) { reg[i].i = 0; freg[i].i = 0; } reg[29].i = 1048575; reg[30].i = 65536; for(i = 0; i < 1048576; i++) memory[i].i = 0; if((fp = fopen(argv[argc-1],"r")) == NULL) { perror("open error"); return 0; } load(fp, pm); fclose(fp); getoption(argc, argv); pc_init = pc; while(x < 2*M_PI) { freg[12].f = x; do{ if(printflag == 1) print_instr(pm[pc]); while((breakflag == 1 && pc == breakpoint) || (stepflag == 1 && stepcount == 0)) bpoint(pm[pc]); if(stepcount > 0) stepcount--; } while(exec(pm[pc]) == 0); fprintf(fpout, "%f %f\n", x, freg[0].f); x += 0.01; pc = pc_init; for(i = 0; i < 32; i++) freg[i].i = 0; } fprintf(stderr, "complete instructions\n"); print_status(); fclose(fpin); fclose(fpout); return 0; }
t_stat fprint_sym (FILE *ofile, t_addr simh_addr, t_value *val, UNIT *uptr, int32 sw) { //log_msg(INFO_MSG, "SYS:fprint_sym", "addr is %012llo; val-ptr is %p, uptr is %p, sw is %#o\n", simh_addr, val, uptr, sw); if (uptr == &cpu_unit) { // memory request -- print memory specified by SIMH addr_modes_t mode; unsigned segno; unsigned offset; if (addr_simh_to_emul(simh_addr, &mode, &segno, &offset) != 0) return SCPE_ARG; unsigned abs_addr; if (addr_any_to_abs(&abs_addr, mode, segno, offset) != 0) return SCPE_ARG; // note that parse_addr() was called by SIMH to determine the absolute addr. static int need_init = 1; if (need_init) { need_init = 0; prior_lineno = 0; prior_line = NULL; } /* First print matching source line if we're dumping instructions */ if (sw & SWMASK('M')) { // M -> instr -- print matching source line if we have one const char* line; int lineno; seginfo_find_line(segno, offset, &line, &lineno); if (line != NULL && prior_lineno != lineno && prior_line != line) { fprintf(ofile, "\r\n"); fprint_addr(ofile, NULL, simh_addr); // UNIT doesn't include a reference to a DEVICE fprintf(ofile, ":\t"); fprintf(ofile, "Line %d: %s\n", lineno, line); // fprintf(ofile, "%06o:\t", abs_addr); // BUG: print seg|offset too fprint_addr(ofile, NULL, simh_addr); // UNIT doesn't include a reference to a DEVICE fprintf(ofile, ":\t"); prior_lineno = lineno; prior_line = line; } } /* Next, we always output the numeric value */ t_addr alow = abs_addr; t_addr ahi = abs_addr; t_uint64 word; word = Mem[abs_addr]; if (word == ~ (t_uint64) 0) word = 0; fprintf(ofile, "%012llo", word); if (sw & SWMASK('S') || (sw & SWMASK('X'))) { // SDWs are two words ++ ahi; t_uint64 hiword = Mem[ahi]; if (hiword == ~ (t_uint64) 0) hiword = 0; fprintf(ofile, " %012llo", hiword); } /* User may request (A)scii in addition to another format */ if (sw & SWMASK('A')) { for (t_addr a = alow; a <= ahi; ++ a) { t_uint64 word = Mem[a]; if (word == ~ (t_uint64) 0) word = 0; fprintf(ofile, " "); for (int i = 0; i < 4; ++i) { uint c = word >> 27; word = (word << 9) & MASKBITS(36); if (c <= 0177 && isprint(c)) { fprintf(ofile, " '%c'", c); } else { fprintf(ofile, " \\%03o", c); } } } } /* See if any other format was requested (but don't bother honoring multiple formats */ if (sw & SWMASK('A')) { // already done } if (sw & SWMASK('L')) { // L -> LPW fprintf(ofile, " %s", print_lpw(abs_addr)); } else if (sw & SWMASK('M')) { // M -> instr char *instr = print_instr(word); fprintf(ofile, " %s", instr); } else if (sw & SWMASK('P') || (sw & SWMASK('Y'))) { // P/Y -> PTW char *s = print_ptw(word); fprintf(ofile, " %s", s); } else if (sw & SWMASK('S') || (sw & SWMASK('X'))) { // S/X -> SDW t_uint64 hiword = Mem[ahi]; if (hiword == ~ (t_uint64) 0) hiword = 0; char *s = print_sdw(word, hiword); fprintf(ofile, " %s", s); } else if (sw & SWMASK('W')) { // W -> DCW char *s = print_dcw(abs_addr); fprintf(ofile, " %s", s); } else if (sw) { return SCPE_ARG; } else { // we already printed the numeric value, so nothing else to do } fflush(ofile); return SCPE_OK; } else if (sw & SIM_SW_REG) {
int main(int argc, char *argv[]) { if (argc < 2) { usage(argv[0]); } if ((fin = fopen(argv[1], "rb")) == 0) { fprintf(stderr, "Error: could not open file\n"); exit(1); } BYTE *fbuf = (BYTE *) malloc(16 * sizeof(BYTE)); pe = (PESTRUCT *) malloc(sizeof(PESTRUCT)); parse_pe_header(); printf("EP RVA: %.8X\n", pe->rvaep); printf("Code section RVA: %.8X\n", pe->rvacode); printf("Data section RVA: %.8X\n", pe->rvadata); printf("Image base: %.8X\n", pe->imagebase); printf("Size of code section: %.8X\n", pe->codesize); /* Get size of headers to know where code section starts */ fseek(fin, pe->offset + 84, SEEK_SET); fgets(fbuf, 4, fin); pe->codeoffset = lendian(fbuf, 4); printf("Code section offset: %.8X\n", pe->codeoffset); /* Get OEP address */ pe->oep = pe->imagebase + pe->rvacode; printf("OEP address: %.8X\n", pe->oep); printf("\n"); // Formatting /* Get max offset from total file size */ fseek(fin, 0L, SEEK_END); pe->maxoffset = ftell(fin); fseek(fin, pe->codeoffset, SEEK_SET); // Go to start of code section DWORD len; DWORD cur_addr = pe->oep; // First instruction at OEP DWORD addr; int i; int quit = 0; /* Start parsing and outputting 50 instructions at a time, waiting for user input after each block */ while (!feof(fin) && !quit) { printf(":"); /* Wait for user input */ int ch = _getch(); printf("\b"); // Erase character switch (ch) { case KEY_ESC: // Quit case 'q': quit = 1; break; case 'n': // Next instruction print_instr(&cur_addr); break; case ' ': // Next 32 instructions print_ninstr(&cur_addr, 50); break; case 'o': // Go back to OEP printf("\r \n"); // Clear line printf("EP RVA: %.8X\n", pe->rvaep); printf("Code section RVA: %.8X\n", pe->rvacode); printf("Data section RVA: %.8X\n", pe->rvadata); printf("Image base: %.8X\n", pe->imagebase); printf("Size of code section: %.8X\n", pe->codesize); printf("Code section offset: %.8X\n", pe->codeoffset); printf("OEP address: %.8X\n", pe->oep); printf("\n"); fseek(fin, pe->codeoffset, SEEK_SET); cur_addr = pe->oep; break; case 'g': { // Go to specific address printf("\r \n"); // Clear line printf("Go to address: "); char getaddr[32]; fgets(getaddr, sizeof(getaddr), stdin); // Get input if (getaddr[0] == '\n' || getaddr[0] == '\r') { printf("\n"); break; } // Blank input (cancel instruction) addr = strtol(getaddr, NULL, 16); // Parse input address if (!print_instr(&addr)) break; // Print the first instruction cur_addr = addr; break; } case 'f': { // Follow instruction at specific address printf("\r \n"); // Clear line printf("Address of instruction to follow: "); char addrstr[32]; fgets(addrstr, sizeof(addrstr), stdin); // Get input if (addrstr[0] == '\n' || addrstr[0] == '\r') { printf("\n"); break; } // Blank input (cancel instruction) addr = strtol(addrstr, NULL, 16); char *instr = get_instr(addr); if (!instr) break; // Error printf("%.8X\t%s\n", addr, instr); // Print instruction to follow printf("\t\tv\n"); addr = parse_addr(instr); if (!print_instr(&addr)) break; cur_addr = addr; free(instr); break; } case 'd': { // Dump data at specific address printf("\r \n"); // Clear line printf("Address to dump: "); char addrstr[32]; fgets(addrstr, sizeof(addrstr), stdin); // Get input if (addrstr[0] == '\n' || addrstr[0] == '\r') { printf("\n"); break; } // Blank input (cancel instruction) addr = strtol(addrstr, NULL, 16); if (!valid_addr(addr)) { printf("Address out of bounds\n"); break; } printf("Number of bytes to dump (default 16): "); char bytesstr[3]; fgets(bytesstr, sizeof(bytesstr), stdin); int bytes = strtol(bytesstr, NULL, 10); if (bytes == 0) bytes = 16; if (bytes > DUMP_MAX) { printf("Too high\n"); break; } print_dump(addr, bytes); printf("\n"); // Formatting break; } case 's': { // Search for a string in .data, .rdata, and .rsrc sections (no unicode support) printf("\r \n"); // Clear line printf("Search for string: "); char searchstr[STRLEN_MAX]; fgets(searchstr, sizeof(searchstr), stdin); // Get input if (searchstr[0] == '\n' || searchstr[0] == '\r') { printf("\n"); break; } // Blank input (cancel instruction) addr = find_string_addr(searchstr); // Find address of string if (addr == 0) { printf("String not found\n"); } else { print_dump_str(addr); // Print dump of matched string } printf("\n"); // Formatting break; } case 'e': { // Edit binary printf("\r \n"); // Clear line printf("Starting address for editing: "); char addrstr[32]; fgets(addrstr, sizeof(addrstr), stdin); // Get input if (addrstr[0] == '\n' || addrstr[0] == '\r') { printf("\n"); break; } // Blank input (cancel instruction) addr = strtol(addrstr, NULL, 16); if (!valid_addr(addr)) { printf("Address out of bounds\n"); break; } printf("Input hex bytes: "); char bytestr[STRLEN_MAX]; fgets(bytestr, sizeof(bytestr), stdin); // Get input char *edit_file = (char *) calloc(strlen(argv[1]) + 6, sizeof(char)); strncpy(edit_file, argv[1], strlen(argv[1]) - 4); strcat(edit_file, "_edit.exe"); // Edited file name: inputfilename_edit.exe save_edits_to_file(fin, edit_file, addr, bytestr); printf("Edited file %s saved\n\n", edit_file); free(edit_file); break; } case 'h': // Show help case '?': print_help(); break; // Special keys case SPECIAL_KEY: ch = _getch(); switch (ch) { case KEY_DOWN: // Next instruction print_instr(&cur_addr); break; case KEY_PGDN: // Next 32 instructions print_ninstr(&cur_addr, 50); break; case KEY_HOME: // Go back to OEP printf("\r \n"); // Clear line printf("EP RVA: %.8X\n", pe->rvaep); printf("Code section RVA: %.8X\n", pe->rvacode); printf("Data section RVA: %.8X\n", pe->rvadata); printf("Image base: %.8X\n", pe->imagebase); printf("Size of code section: %.8X\n", pe->codesize); printf("Code section offset: %.8X\n", pe->codeoffset); printf("OEP address: %.8X\n", pe->oep); printf("\n"); fseek(fin, pe->codeoffset, SEEK_SET); cur_addr = pe->oep; break; default: //printf("0x%X\n", ch); // DEBUGGING break; } break; default: //printf("0x%X\n", ch); // DEBUGGING break; } fflush(stdin); } free(fbuf); free(pe); fclose(fin); return 0; }