import urllib import urllib2 import threading import subprocess import os import sys import json def local_print(string): if os.name == "posix": # Nicer colour output when using posix (and thus supporting colour) string = "\033[92m%s\033[0m" % string print(string) def remote_print(string): if os.name == "posix": # Nicer colour output when using posix (and thus supporting colour) string = "\033[94m%s\033[0m" % string print(string) local_print("Welcome to the debugging shell!") try: address = sys.argv[1] except IndexError: address = "http://127.0.0.1:8001" try: taskname = sys.argv[2] except IndexError: import random taskname = str(random.random()) local_print("Switching context to Modelverse: all data is piped.") local_print("Available commands: 'attach_debugger', 'detach_debugger', 'pause', 'resume', 'line_step', 'big_step', 'small_step', 'step_into', 'read_symbols', 'add_breakpoint', 'del_breakpoint', 'toggle_breakpoint'") local_print("To quit: execute command 'quit'") def polling_func(): while 1: try: replies = json.loads(urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "poll_messages", "taskname": taskname})), timeout=60).read()) except: continue for reply in replies: print "RES: %s" % reply['result'] if 'state' in reply: print "STATE: %s" % reply['state'] if 'stack' in reply: print "STACK:\n\t%s" % '\n\t'.join(['{} {: >50.50} .{}'.format(l[::-1], lnr[::-1], str(k)[::-1])[::-1] for (k, (lnr, l)) in list(enumerate(reply['stack']))]) if 'instruction' in reply: print "INST: %s" % reply['instruction'] if 'phase' in reply: print "PHASE: %s" % reply['phase'] if 'symbols' in reply: print "SYMB: %s" % reply['symbols'] if 'triggered_bp' in reply: print "BP: %s" % reply['triggered_bp'] thrd = threading.Thread(target=polling_func) thrd.daemon = True thrd.start() while 1: inp = raw_input().split(" ") action = inp[0] params = {} if action == "quit": local_print("Received quit: breaking connection to Modelverse immediately!") break elif action not in set(["attach_debugger", "detach_debugger", "pause", "resume", "line_step", "big_step", "small_step", "step_into", "read_symbols", "add_breakpoint", "del_breakpoint", "toggle_breakpoint"]): local_print("Invalid action %s" % action) continue if action == "add_breakpoint": if len(inp) < 2: print 'add_breakpoint requires at least 1 parameter!' continue inp += [None, None, True, True][(len(inp) - 2):] params['breakpoint_id'] = inp[1] params['file_name'] = inp[2] if inp[2] != "." else None params['line_number'] = int(inp[3]) if inp[3] else None params['enabled'] = bool(inp[4]) params['disable_on_trigger'] = bool(inp[1]) elif action == "del_breakpoint": if len(inp) < 2: print 'del_breakpoint requires 1 parameter!' continue params['breakpoint_id'] = inp[1] elif action == "toggle_breakpoint": if len(inp) < 3: print 'del_breakpoint requires 2 parameters!' continue params['breakpoint_id'] = inp[1] params['enabled'] = bool(inp[2]) urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": action, "taskname": taskname, "args": json.dumps(params)}))).read()