Skip to content

Commit

Permalink
system is now printing out the instrunction pointer and what the curr…
Browse files Browse the repository at this point in the history
…ent instrunction is
  • Loading branch information
matthewfl committed May 1, 2016
1 parent 15efa08 commit f62bb6b
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 104 deletions.
90 changes: 0 additions & 90 deletions .deps

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.o
jit-test
.deps
Binary file removed build/main.o
Binary file not shown.
26 changes: 26 additions & 0 deletions deps/udis86/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Makefile
config.h
config.log
config.status
docs/Makefile
docs/manual/Makefile
docs/x86/Makefile
libtool
libudis86/.deps/
libudis86/.libs/
libudis86/Makefile
libudis86/decode.lo
libudis86/itab.lo
libudis86/libudis86.la
libudis86/syn-att.lo
libudis86/syn-intel.lo
libudis86/syn.lo
libudis86/udis86.lo
scripts/Makefile
stamp-h1
tests/.deps/
tests/Makefile
tests/difftest.sh
udcli/.deps/
udcli/Makefile
udcli/udcli
15 changes: 13 additions & 2 deletions make
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ CXX_FLAGS = (
'-I ./deps/ '
'-ggdb '
'-O0 '
'-fopenmp '
'-I ./deps/udis86'
# '-I /home/matthew/Downloads/bochs-code/bochs/cpu/ '
# '-I /home/matthew/Downloads/bochs-code/bochs/ '
# '-I /home/matthew/Downloads/bochs-code/bochs/instrument/stubs '
Expand All @@ -28,7 +28,8 @@ CXX_FLAGS_UNIT = (
'-I ./src/ '
)
LIBS = (
'-pthread'
'-pthread '
'deps/udis86/libudis86/.libs/libudis86.a '
# '/home/matthew/Downloads/bochs-code/bochs/cpu/libcpu.a '
# '/home/matthew/Downloads/bochs-code/bochs/logio.o '
# '/home/matthew/Downloads/bochs-code/bochs/cpu/fpu/libfpu.a '
Expand All @@ -42,6 +43,7 @@ CXX='g++'
LD='g++'

def build():
deps()
compile()
link()

Expand All @@ -54,9 +56,11 @@ def mic():
def release():
global CXX_FLAGS
CXX_FLAGS = CXX_FLAGS.replace('-O0', '-O3')
CXX_FLAGS = CXX_FLAGS.replace('-ggdb', '')
build()

def clean():
Shell('cd deps/udis86 && make clean', shell=True)
autoclean()

def run():
Expand Down Expand Up @@ -103,11 +107,18 @@ def unit_link():
after()

def unit():
deps()
unit_compile()
compile()
unit_link()
Shell('./' + UNIT_TARGET)

def deps():
# udis86 version 1.7.2
# TODO: make this auto build if not there
#Shell('cd deps/udis86 && ./configure && make', shell=True)
after()


if __name__ == '__main__':
main(parallel_ok=True)#, jobs=4)
18 changes: 17 additions & 1 deletion src/jit_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,30 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/user.h>
#include <sys/reg.h>
//#include <sys/reg.h>

#include <sys/syscall.h>

#include <string.h>

#include <thread>
#include <atomic>

#include <cstddef>

#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})

typedef unsigned long long int register_type;

struct redmagic_handle_t {
struct redmagic_thread_trace_t *head = nullptr;
pid_t child_pid;

// hacky stuff to get things working....
register_type pc;
unsigned long read_offset;
};

struct redmagic_thread_trace_t {
Expand Down
110 changes: 106 additions & 4 deletions src/jit_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "redmagic.h"
#include "jit_internal.h"

#include "udis86.h"

#include <iostream>
using namespace std;
Expand All @@ -14,14 +15,115 @@ struct redmagic_handle_t *redmagic_global_default = nullptr;
handle = redmagic_global_default; \
}

static int udis_input_hook(ud_t *obj) {
// this method is suppose to only go forward one byte each time
// if the skip method is called then this just get call n times, so not too useful
struct redmagic_handle_t *user = (struct redmagic_handle_t*)ud_get_user_opaque_data(obj);

unsigned long at = user->read_offset;
long res = ptrace(PTRACE_PEEKDATA, user->child_pid, at, NULL);
// TODO: make this cache the internal result
user->read_offset++;
return res & 0xff;
}

extern "C" struct redmagic_handle_t *redmagic_init() {
auto r = new redmagic_handle_t;
if(redmagic_global_default == nullptr) {
// auto r = new redmagic_handle_t;
// if(redmagic_global_default == nullptr) {
// redmagic_global_default = r;
// }
// return r;

pid_t child = fork();
if(child == 0) {
// we are the child process
ptrace(PTRACE_TRACEME, 0, NULL, NULL);

asm("int3");


return NULL;
} else {
auto r = new redmagic_handle_t;
redmagic_global_default = r;
}
return r;
r->child_pid = child;

ud_t disassm;
ud_init(&disassm);
ud_set_user_opaque_data(&disassm, r);
ud_set_input_hook(&disassm, udis_input_hook);
ud_set_mode(&disassm, 64); // 64 bit
ud_set_vendor(&disassm, UD_VENDOR_INTEL);
ud_set_syntax(&disassm, UD_SYN_INTEL);

int res, stat;
// wait for a traceme call to arrive
res = waitpid(child, &stat, WUNTRACED);
cout << "attached to childed: " << res << " " << stat << endl << flush;
if((res != child) || !(WIFSTOPPED(stat))) {
cerr << "unexpected state\n";
exit(-1);
}
//struct user_regs_struct regs, oregs;
// ptrace(PTRACE_GETREGS, r->child_pid, &regs, NULL);
// cout << "got back regs\n" << flush;

// while(waitpid(child, NULL, WNOHANG) == 0) {
// ptrace(PTRACE_CONT, child, NULL, NULL);
// wait(NULL);
// long i = ptrace(PTRACE_GETREGS, child, &regs, NULL);
// //if(memcmp(&regs,&oregs, sizeof(regs)) != 0) {
// cout << "a:" << regs.rip << endl << flush;
// oregs = regs;
// //}
// //asm("int3");
// ptrace(PTRACE_CONT, child, NULL, NULL);
// }

// http://www.secretmango.com/jimb/Whitepapers/ptrace/ptrace.html

while(true) {
if((res = ptrace(PTRACE_SINGLESTEP, child, NULL, NULL)) < 0) {
cerr << "failed single step\n";
exit(-1);
}
res = wait(&stat);

int signo;
if((signo = WSTOPSIG(stat)) == SIGTRAP) {
signo = 0;
}
if(signo == SIGINT) {
cerr << "processes was killed\n";
exit(-1);
}

struct user_regs_struct regs;
if(ptrace(PTRACE_GETREGS, child, &regs, &regs) < 0) {
cerr << "failed to get regs\n";
exit(-1);
}

r->pc = regs.rip;
r->read_offset = regs.rip;
ud_set_pc(&disassm, regs.rip);

if (!ud_disassemble(&disassm)) {
cerr << "fail disassm\n";
exit(-1);
}


cout << "instrunction pointer: "<<regs.rip <<" " << ud_insn_asm(&disassm) << endl;



}


// this does not return in the parent processes
exit(0);
}
}

extern "C" void redmagic_destroy(struct redmagic_handle_t *handle) {
Expand Down
12 changes: 5 additions & 7 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ using namespace std;

int main(int argc, char* argv[]) {

// will perform a fork internally
redmagic_init();


std::thread tt([]() {
redmagic_start_trace(NULL);

cout << "asdf\n";

sleep(10);
sleep(1);

asm("int3");

cout << "test123";

});
sleep(1);

tt.join();
}

0 comments on commit f62bb6b

Please sign in to comment.