debug_prompt.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import urllib
  2. import urllib2
  3. import threading
  4. import subprocess
  5. import os
  6. import sys
  7. import json
  8. def local_print(string):
  9. if os.name == "posix":
  10. # Nicer colour output when using posix (and thus supporting colour)
  11. string = "\033[92m%s\033[0m" % string
  12. print(string)
  13. def remote_print(string):
  14. if os.name == "posix":
  15. # Nicer colour output when using posix (and thus supporting colour)
  16. string = "\033[94m%s\033[0m" % string
  17. print(string)
  18. local_print("Welcome to the debugging shell!")
  19. try:
  20. address = sys.argv[1]
  21. except IndexError:
  22. address = "http://127.0.0.1:8001"
  23. try:
  24. taskname = sys.argv[2]
  25. except IndexError:
  26. import random
  27. taskname = str(random.random())
  28. local_print("Switching context to Modelverse: all data is piped.")
  29. 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'")
  30. local_print("To quit: execute command 'quit'")
  31. def polling_func():
  32. while 1:
  33. try:
  34. replies = json.loads(urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "poll_messages", "taskname": taskname})), timeout=60).read())
  35. except:
  36. continue
  37. for reply in replies:
  38. print "RES: %s" % reply['result']
  39. if 'state' in reply:
  40. print "STATE: %s" % reply['state']
  41. if 'stack' in reply:
  42. 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']))])
  43. if 'instruction' in reply:
  44. print "INST: %s" % reply['instruction']
  45. if 'phase' in reply:
  46. print "PHASE: %s" % reply['phase']
  47. if 'symbols' in reply:
  48. print "SYMB: %s" % reply['symbols']
  49. if 'triggered_bp' in reply:
  50. print "BP: %s" % reply['triggered_bp']
  51. thrd = threading.Thread(target=polling_func)
  52. thrd.daemon = True
  53. thrd.start()
  54. while 1:
  55. inp = raw_input().split(" ")
  56. action = inp[0]
  57. params = {}
  58. if action == "quit":
  59. local_print("Received quit: breaking connection to Modelverse immediately!")
  60. break
  61. 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"]):
  62. local_print("Invalid action %s" % action)
  63. continue
  64. if action == "add_breakpoint":
  65. if len(inp) < 2:
  66. print 'add_breakpoint requires at least 1 parameter!'
  67. continue
  68. inp += [None, None, True, True][(len(inp) - 2):]
  69. params['breakpoint_id'] = inp[1]
  70. params['file_name'] = inp[2] if inp[2] != "." else None
  71. params['line_number'] = int(inp[3]) if inp[3] else None
  72. params['enabled'] = bool(inp[4])
  73. params['disable_on_trigger'] = bool(inp[1])
  74. elif action == "del_breakpoint":
  75. if len(inp) < 2:
  76. print 'del_breakpoint requires 1 parameter!'
  77. continue
  78. params['breakpoint_id'] = inp[1]
  79. elif action == "toggle_breakpoint":
  80. if len(inp) < 3:
  81. print 'del_breakpoint requires 2 parameters!'
  82. continue
  83. params['breakpoint_id'] = inp[1]
  84. params['enabled'] = bool(inp[2])
  85. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": action, "taskname": taskname, "args": json.dumps(params)}))).read()