From 9331df89200794e9df64af2b86bde8f98fd5011a Mon Sep 17 00:00:00 2001 From: Jeremiah Orians Date: Wed, 20 Jul 2016 21:27:28 -0400 Subject: [PATCH] Made Static development prototype into a wrapper around generic library so that web program can be more easily tested --- User_Interface.py | 155 +++++++++++++++++++++++++++ Web Interface prototype Generator.py | 153 +------------------------- 2 files changed, 157 insertions(+), 151 deletions(-) create mode 100644 User_Interface.py diff --git a/User_Interface.py b/User_Interface.py new file mode 100644 index 0000000..a2269c4 --- /dev/null +++ b/User_Interface.py @@ -0,0 +1,155 @@ +import subprocess +import ctypes +import re + +subprocess.call("./bin/dis foo | sponge z_disassembled", shell=True) +vm = ctypes.CDLL('./libvm.so') + +vm.initialize_lilith() +Current_IP = 0 +Watchpoints = {0} +vm.load_lilith(ctypes.create_string_buffer("foo".encode('ascii'))) + +def Step_lilith(): + global Current_IP + vm.step_lilith.restype = ctypes.c_uint + Current_IP = vm.step_lilith() + return + +def returnPage(): + return get_header() + (vm.get_memory(0)).decode('utf-8') + get_spacer1() + get_registers(0) + get_registers(8) + get_spacer2() + get_disassembled() + get_footer() + +vm.get_memory.argtype = ctypes.c_uint +vm.get_memory.restype = ctypes.c_char_p + +hexlookup = { 0 : '0', 1 : '1', 2 : '2', 3 : '3', 4 : '4', 5 : '5', 6 : '6', 7 : '7', 8 : '8', 9 : '9', 10 : 'A', 11 : 'B', 12 : 'C', 13 : 'D', 14 : 'E', 15 : 'F' } + +def formatByte(a): + first = a >> 4 + second = a % 16 + return str(hexlookup[first]+hexlookup[second]) + +def formatAddress(a): + first = a >> 24 + second = (a % 16777216) >> 16 + third = (a % 65536) >> 8 + fourth = a % 256 + myreturn = formatByte(first) + formatByte(second) + formatByte(third) + formatByte(fourth) + return myreturn[:-1] + "x" + +def formatRegister(a): + first = a >> 24 + second = (a % 16777216) >> 16 + third = (a % 65536) >> 8 + fourth = a % 256 + return formatByte(first) + formatByte(second) + formatByte(third) + formatByte(fourth) + +def get_header(): + return """ + + + + + Knight CPU Debugger + + + + + +
+ + + + + +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + """ + +def get_spacer1(): + return """ +
Index0123456788ABCDEF
+
+
+""" + +def get_registers(index): + vm.get_register.argtype = ctypes.c_uint + vm.get_register.restype = ctypes.c_uint + +# R0 = vm.get_register(3) +# print("Register R0 value:") +# print(R0) + + temp = """ + + + + + + + """ + + for i in range(0,8): + temp = temp + """\n" + + return temp + """ +
RegisterValue
R""" + str(index + i) + "" + formatRegister(vm.get_register(index + i)) + "
""" + +def get_spacer2(): + return """ +
+
+
""" + +def get_disassembled(): + f = open('z_disassembled', "r") + + temp = """""" + i = 0 + for line in f: + pieces = re.split(r'\t+', line) + + if (i < Current_IP): + temp = temp + "" + elif (i == Current_IP): + temp = temp + """\n" + elif i in Watchpoints: + temp = temp + """\n" + else: + temp = temp + "\n" + + i = i + 4 + + f.close() + return temp + "
""" + formatRegister(i) + "" + pieces[0] + "" + pieces[1] + "
""" + formatRegister(i) + "" + pieces[0] + "" + pieces[1] + "
" + formatRegister(i) + "" + pieces[0] + "" + pieces[1] + "
" + +def get_footer(): + return """ +
+ + +""" diff --git a/Web Interface prototype Generator.py b/Web Interface prototype Generator.py index 118c0e7..9525949 100644 --- a/Web Interface prototype Generator.py +++ b/Web Interface prototype Generator.py @@ -1,152 +1,3 @@ -import subprocess -import ctypes -import re +import User_Interface as UI -subprocess.call("./bin/dis foo | sponge z_disassembled", shell=True) - -vm = ctypes.CDLL('./libvm.so') - -vm.initialize_lilith() -vm.load_lilith(ctypes.create_string_buffer("foo".encode('ascii'))) - -# vm.get_byte.argtype = ctypes.c_uint -# vm.get_byte.restype = ctypes.c_ubyte - -# print("Memory 0-19 values:") -# for i in range(0, 20): -# print(vm.get_byte(i)) - -vm.get_memory.argtype = ctypes.c_uint -vm.get_memory.restype = ctypes.c_char_p - -hexlookup = { 0 : '0', 1 : '1', 2 : '2', 3 : '3', 4 : '4', 5 : '5', 6 : '6', 7 : '7', 8 : '8', 9 : '9', 10 : 'A', 11 : 'B', 12 : 'C', 13 : 'D', 14 : 'E', 15 : 'F' } - -def formatByte(a): - first = a >> 4 - second = a % 16 - return str(hexlookup[first]+hexlookup[second]) - -def formatAddress(a): - first = a >> 24 - second = (a % 16777216) >> 16 - third = (a % 65536) >> 8 - fourth = a % 256 - myreturn = formatByte(first) + formatByte(second) + formatByte(third) + formatByte(fourth) - return myreturn[:-1] + "x" - -def formatRegister(a): - first = a >> 24 - second = (a % 16777216) >> 16 - third = (a % 65536) >> 8 - fourth = a % 256 - return formatByte(first) + formatByte(second) + formatByte(third) + formatByte(fourth) - -def get_header(): - return """ - - - - - Knight CPU Debugger - - - - - -
- - - - - - -
-
-
- - - - - - - - - - - - - - - - - - - - - - - """ - -def get_spacer1(): - return """ -
Index0123456788ABCDEF
-
-
-""" - -def get_registers(index): - vm.get_register.argtype = ctypes.c_uint - vm.get_register.restype = ctypes.c_uint - -# R0 = vm.get_register(3) -# print("Register R0 value:") -# print(R0) - - temp = """ - - - - - - - """ - - for i in range(0,8): - temp = temp + """\n" - - return temp + """ -
RegisterValue
R""" + str(index + i) + "" + formatRegister(vm.get_register(index + i)) + "
""" - -def get_spacer2(): - return """ -
-
-
""" - -def get_disassembled(): - f = open('z_disassembled', "r") - - temp = """""" - i = 0 - for line in f: - pieces = re.split(r'\t+', line) - temp = temp + "\n" - i = i + 4 - - return temp + "
" + formatRegister(i) + "" + pieces[0] + "" + pieces[1] + "
" - -def get_footer(): - return """ -
- - -""" - -print(get_header()) -print( (vm.get_memory(0)).decode('utf-8')) -print(get_spacer1()) -print(get_registers(0)) -print(get_registers(8)) -print(get_spacer2()) -print(get_disassembled()) -print(get_footer()) +print(UI.returnPage())