You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
169 lines
4.2 KiB
169 lines
4.2 KiB
3 years ago
|
static struct mi_sourcepoint *
|
||
|
pc_to_sourcepoint(struct mi_process proc, u64 pc)
|
||
|
{
|
||
|
// NOTE: find first point BIGGER that pc, return the sourcepoint just before that
|
||
|
// TODO: binary search
|
||
|
for (int i = 0; i < proc.sp_count; ++i) {
|
||
|
struct mi_sourcepoint *point = proc.sp_table + i;
|
||
|
if (point->pc > pc) {
|
||
|
return(proc.sp_table + i - 1);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return(0);
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
print_word_at_address(int child, u64 address)
|
||
|
{
|
||
|
long word = ptrace(PTRACE_PEEKDATA, child, address, NULL);
|
||
|
u8 nb[4];
|
||
|
memcpy(nb, &word, 4);
|
||
|
printf("word at %#018lx: {%x %x %x %x}\n", address, nb[0], nb[1], nb[2], nb[3]);
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
print_current_line(struct mi_process proc, u64 line)
|
||
|
{
|
||
|
char *source = proc.source;
|
||
|
u64 size = proc.source_size;
|
||
|
u64 current_line = 1;
|
||
|
|
||
|
for (u32 i = 0; i < size; ++i) {
|
||
|
if (current_line == line) {
|
||
|
int len = 0;
|
||
|
int start = 0;
|
||
|
int leading = 1;
|
||
|
|
||
|
for (u32 j = i; j < size; ++j) {
|
||
|
if (leading && (source[j] == '\t' || source[j] == ' ')) {
|
||
|
++start;
|
||
|
} else {
|
||
|
leading = 0;
|
||
|
}
|
||
|
|
||
|
if (source[j] == '\n') {
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
++len;
|
||
|
}
|
||
|
|
||
|
|
||
|
printf("%.*s\n", len - start, source + i + start);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (source[i] == '\n') {
|
||
|
current_line++;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static struct mi_process
|
||
|
process_create(char *path)
|
||
|
{
|
||
|
struct mi_process result = { 0 };
|
||
|
|
||
|
/* disable ASLR */
|
||
|
syscall(SYS_personality, ADDR_NO_RANDOMIZE);
|
||
|
|
||
|
pid_t pid = fork();
|
||
|
|
||
|
if (pid == -1) {
|
||
|
DIE("fork error\n");
|
||
|
}
|
||
|
|
||
|
if (pid == 0) {
|
||
|
int rt = ptrace(PTRACE_TRACEME, 0, 0, 0);
|
||
|
assert(rt == 0);
|
||
|
char *args[] = { path, NULL };
|
||
|
rt = execvp(path, args);
|
||
|
if (rt == -1) {
|
||
|
DIE("exevp error\n");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
u64 source_size;
|
||
|
u64 elf_size;
|
||
|
u8 *elf;
|
||
|
char *source;
|
||
|
struct stat s;
|
||
|
int fd;
|
||
|
|
||
|
{
|
||
|
fd = open(path, O_RDONLY);
|
||
|
assert(fd != -1);
|
||
|
fstat(fd, &s);
|
||
|
elf_size = s.st_size;
|
||
|
elf = mmap(0, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||
|
close(fd);
|
||
|
}
|
||
|
|
||
|
{
|
||
|
fd = open("/code/trace-test/traceme.c", O_RDONLY); // TODO
|
||
|
assert(fd != -1);
|
||
|
fstat(fd, &s);
|
||
|
source_size = s.st_size;
|
||
|
source = mmap(0, source_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||
|
close(fd);
|
||
|
}
|
||
|
|
||
|
result.elf = elf;
|
||
|
result.elf_size = elf_size;
|
||
|
result.source = source;
|
||
|
result.source_size = source_size;
|
||
|
result.pid = pid;
|
||
|
|
||
|
return(result);
|
||
|
}
|
||
|
|
||
|
static struct mi_registers
|
||
|
get_process_registers(struct mi_process proc)
|
||
|
{
|
||
|
struct user_regs_struct regs = { 0 };
|
||
|
struct iovec iov = { ®s, sizeof(regs) };
|
||
|
struct mi_registers result = { 0 };
|
||
|
|
||
|
ptrace(PTRACE_GETREGSET, proc.pid, NT_PRSTATUS, &iov);
|
||
|
|
||
|
result.rax = regs.rax;
|
||
|
result.rip = regs.rip;
|
||
|
result.rbp = regs.rbp;
|
||
|
result.rbx = regs.rbx;
|
||
|
result.rcx = regs.rcx;
|
||
|
result.rdx = regs.rdx;
|
||
|
result.rsi = regs.rsi;
|
||
|
result.rdi = regs.rdi;
|
||
|
result.rsp = regs.rsp;
|
||
|
|
||
|
result._sys = regs;
|
||
|
|
||
|
return(result);
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
set_process_registers(struct mi_process proc, struct mi_registers regs)
|
||
|
{
|
||
|
struct iovec iov = { ®s._sys, sizeof(regs._sys) };
|
||
|
ptrace(PTRACE_GETREGSET, proc.pid, NT_PRSTATUS, &iov);
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
print_current_instruction(struct mi_process proc)
|
||
|
{
|
||
|
struct mi_registers regs = get_process_registers(proc);
|
||
|
|
||
|
long wordb = ptrace(PTRACE_PEEKDATA, proc.pid, regs.rip - 4, NULL);
|
||
|
long word = ptrace(PTRACE_PEEKDATA, proc.pid, regs.rip, NULL);
|
||
|
long worda = ptrace(PTRACE_PEEKDATA, proc.pid, regs.rip + 4, NULL);
|
||
|
|
||
|
u8 nb[12];
|
||
|
memcpy(nb, &wordb, 4);
|
||
|
memcpy(nb + 4, &word, 4);
|
||
|
memcpy(nb + 8, &worda, 4);
|
||
|
|
||
|
printf("PC = %#018lx: %x %x %x %x [%x] %x %x %x %x %x %x %x\n", regs.rip,
|
||
|
nb[0], nb[1], nb[2], nb[3], nb[4], nb[5], nb[6], nb[7],
|
||
|
nb[8], nb[9], nb[10], nb[11]);
|
||
|
}
|