#include #include #include #include #include #include #include #include "console.h" #include "debugger.h" #include "helpers.h" #include "list.h" static PANEL *left, *right; static pid_t parse_pid(const char *nptr) { char *endptr; pid_t pid = strtoul(nptr, &endptr, 0); return (*endptr ? 0 : pid); } static pid_t dofork(char **argv, struct console *cons) { pid_t pid = fork(); if (pid < 0) { return -1; } if (pid == 0) { usleep(100000); console_configslave(cons); close_range(STDERR_FILENO+1, ~0U, CLOSE_RANGE_UNSHARE); //raise(SIGSTOP); // ptrace(PTRACE_TRACEME, 0, NULL, NULL); execvp(argv[0], argv); exit(EXIT_FAILURE); } return pid; } static void list_breakpoints(struct thread *dbg, PANEL *pan) { struct list *breaks = &dbg->proc->breakpoints; if (breaks->head != breaks->end) { pprintw(pan, "---\n"); for (struct breakpoint *bp=breaks->head; bp!=breaks->end; bp=bp->next) { pprintw(pan, "0x%lx (%c) (%i) ", bp->address, (bp->installed ? '*' : ' '), bp->hits); } pprintw(pan, "\n"); } } static void describe_states(struct thread *dbg, PANEL *pan) { struct list *states = &dbg->states; for (struct state *s = states->head; s != states->end; s = s->next) { pprintw(pan, "%c", (s == dbg->state ? '#' : '-')); } pprintw(pan, "\n"); } static void dump_registers(struct thread *dbg, PANEL *pan) { struct user_regs_struct *regs = &dbg->state->regs; pprintw(pan, "rax = 0x%016llx\n", regs->rax); pprintw(pan, "rbx = 0x%016llx\n", regs->rbx); pprintw(pan, "rcx = 0x%016llx\n", regs->rcx); pprintw(pan, "rdx = 0x%016llx\n", regs->rdx); pprintw(pan, "rdi = 0x%016llx\n", regs->rdi); pprintw(pan, "rsi = 0x%016llx\n", regs->rsi); pprintw(pan, "rsp = 0x%016llx\n", regs->rsp); pprintw(pan, "rbp = 0x%016llx\n", regs->rbp); pprintw(pan, "rip = 0x%016llx\n", regs->rip); } static void dump_stack(struct thread *dbg, PANEL *pan) { unsigned long sp = dbg->state->regs.rsp; for (size_t i = 0; i < 16; i++, sp += 8) { unsigned long word = *(unsigned long *)deref(dbg, sp,sizeof(unsigned long)); pprintw(pan, "0x%lx:\t0x%lx\n", sp, word); } } static int rip_visited(struct thread *th, unsigned long rip) { struct list *states = &th->states; for (struct state *s = states->head; s != states->end; s = s->next) { if (rip == s->regs.rip) { return 1; } } return 0; } static void disasm(struct thread *dbg, PANEL *pan) { csh handle; cs_insn *insn; if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) { //perror("capstone open"); } else { uint64_t address = dbg->state->regs.rip; size_t codez = 128; const uint8_t *code = deref(dbg, address, codez); insn = cs_malloc(handle); for (size_t i = 0; i < 32; i++) { if (!cs_disasm_iter(handle, &code, &codez, &address, insn)) { break; } if (dbg->stopped) { if (insn->address == dbg->state->regs.rip) { pattron(pan, COLOR_PAIR(1)); } else if (get_breakpoint(dbg->proc, insn->address)) { pattron(pan, COLOR_PAIR(3)); } else if (rip_visited(dbg, insn->address)) { pattron(pan, COLOR_PAIR(2)); } } pprintw(pan, "0x%"PRIx64":\t%s %s\n", insn->address, insn->mnemonic, insn->op_str); if (dbg->stopped) { if (insn->address == dbg->state->regs.rip) { pattroff(pan, COLOR_PAIR(1)); } else if (get_breakpoint(dbg->proc, insn->address)) { pattroff(pan, COLOR_PAIR(3)); } else if (rip_visited(dbg, insn->address)) { pattroff(pan, COLOR_PAIR(2)); } } } cs_free(insn, 1); cs_close(&handle); } } static void info_update(struct thread *th, PANEL *pan) { pclear(pan); pprintw(pan, "TID: %li\n", (long)th->id); pprintw(pan, "%s (%i: %s)\n", th->status, th->signal, strsignal(th->signal)); list_breakpoints(th, pan); describe_states(th, pan); dump_registers(th, pan); pprintw(pan, "---\n"); dump_stack(th, pan); pprintw(pan, "---\n"); disasm(th, pan); } static void wait_all_threads(struct list *processes) { for (struct process *proc = processes->head; proc != processes->end; proc = proc->next) { dbg_sync(proc); } } static void layout(struct list *processes, struct thread *th) { int w = COLS/2; reset_panel(left, LINES-2, w, 1, 0); reset_panel(right, LINES-2, COLS-w, 1, w); clear(); attron(COLOR_PAIR(1)); for (struct process *proc = processes->head; proc != processes->end; proc = proc->next) { struct list *threads = &proc->threads; for (struct thread *t = threads->head; t != threads->end; t = t->next) { if (t == th) { printw("**"); } printw("%li (%s)", (long)t->id, t->status); if (t == th) { printw("**"); } printw(" "); } } attroff(COLOR_PAIR(1)); } int main(int argc, char **argv) { if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return EXIT_FAILURE; } getchar(); struct console cons = {0}; console_init(&cons); struct list processes = {0}; struct thread *th = NULL; list_init(&processes); int child = 0; pid_t pid = parse_pid(argv[1]); argv[argc] = NULL; if (pid == 0) { pid = dofork(argv+1, &cons); child = 1; } struct process *proc = dbg_attach(pid, child); if (!proc) { fprintf(stderr, "Failed to attach to process %li\n", (long)pid); return EXIT_FAILURE; } list_insert(processes.end, proc); th = proc->threads.head; if (child) { dbg_cont(th); } cursinit(); left = newpan(0, 0, 0, 0); right = newpan(0, 0, 0, 0); layout(&processes, th); int quit = 0; int mode = 0; while (!quit) { wait_all_threads(&processes); layout(&processes, th); console_update(&cons, right); info_update(th, left); cursupdate(); int ch = getch(); if (mode == 0) { switch (ch) { case KEY_RESIZE: layout(&processes, th); break; case 'q': quit = 1; break; case 'i': mode = 1; console_enter(&cons, right); break; case 'j': dbg_stepover(th); break; case 'k': dbg_stepback(th); break; case 'l': dbg_stepin(th); break; case 'h': /* todo: step out */ break; case 'g': if (th->stopped) { th->state = th->states.head; } break; case 'G': if (th->stopped) { th->state = th->states.tail; } break; case 's': dbg_syscall(th); break; case 'c': dbg_cont(th); break; case 'p': dbg_intr(th); break; case 't': proc = th->proc; do { th = th->next; } while (th == proc->threads.end); layout(&processes, th); break; case 'T': proc = th->proc; do { th = th->prev; } while (th == proc->threads.end); layout(&processes, th); break; /* todo: next/prev process bindings */ case ':': mvprintw(LINES-1, 0, ":"); curs_set(TRUE); echo(); timeout(-1); char cmd[128] = {0}; getstr(cmd); curs_set(FALSE); noecho(); timeout(25); clear(); layout(&processes, th); char *t = cmd; int en = 1; if (t[0] == '!') { en = -1; t++; } else if (t[0] == '#') { en = 0; t++; } unsigned long address = strtoul(t, NULL, 0); struct breakpoint *b = add_breakpoint(th->proc, address); b->enabled = en; break; } } else { switch (ch) { case KEY_RESIZE: layout(&processes, th); break; case KEY_ESCAPE: mode = 0; console_leave(&cons, right); break; case ERR: break; default: console_input(&cons, ch); break; } } } dbg_detach(th->proc); /* todo: detach all procs */ endwin(); return EXIT_SUCCESS; } //#include //#include //#include //#include //#include //#include // //case 'd': // // if (dbg->stopped) { // // while (dbg->breaks.head != dbg->breaks.end) { // // struct breakpoint *b = dbg->breaks.head; // // list_remove(b); // // free(b); // // } // // } // // break; // //case 'D': // // if (dbg->stopped) { // // if (ptrace(PTRACE_DETACH, dbg->id, NULL, NULL) < 0) { // // pprintw(right, "PTRACE_DETACH: %s\n", strerror(errno)); // // } // // struct tracee *rm = dbg; // // do { // // dbg = dbg->next; // // } while (dbg == tracees.end); // // list_remove(rm); // // dbg_free(rm); // // free(rm); // // } // // break;