opdb/server.py

117 lines
2.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import subprocess
import time
import traceback
from threading import Thread
from flask_cors import CORS
import io_wrapper
import log
import pdb_wrapper
from flask import Flask, json
from flask import jsonify
from flask import request
app = Flask(__name__)
CORS(app)
console = None
pdb = None
def frame_data():
global console
frame_data = console.get_frame_data()
frame_data.pop("dirname", None)
frame_data.pop("file_listing", None)
frame_data.pop("globals", None)
exception = frame_data.pop("exception", None)
if exception:
log.error(exception)
return frame_data
def run_debugger():
global pdb
pdb.debug()
import b
try:
b.main()
except Exception:
pass
@app.route("/start")
def start():
# # 输入输出重定向处理启动pdb调试器以后台线程运行
# try:
# # Try to import and see if it throws any syntax error
# from user_code.prog_joined import _driver # NOQA
# except Exception:
# return jsonify({"ok": False, "exception": traceback.format_exc()})
global console
global pdb
io_wrapper.override_input()
std_out_wrapper = io_wrapper.get_stdout_wrapper()
pdb = pdb_wrapper.get_pdb_instance(
stdout=std_out_wrapper, stderr=std_out_wrapper
)
console = pdb.get_console()
thread = Thread(target=run_debugger)
thread.daemon = True
thread.start()
return jsonify({"ok": True})
@app.route("/stop")
def stop():
try:
pdb.do_quit(1)
except Exception:
return jsonify({"ok": False, "exception": traceback.format_exc()})
with open("commands.txt", 'r+', encoding='utf-8') as f:
res = f.readlines()
print(res)
f.seek(0)
f.truncate()
return jsonify({"ok": True})
@app.route("/run_command")
# 执行命令取栈帧json格式化输出
def run_command():
command = request.args.get("command")
console.send_pdb_command(command)
return jsonify(frame_data())
@app.route("/add_expression")
def add_expression():
expression = request.args.get("expression")
pdb.add_expression(expression)
return jsonify(frame_data())
@app.route("/remove_expression")
def remove_expression():
expression = request.args.get("expression")
pdb.remove_expression(expression)
return jsonify(frame_data())
def main():
app.run(host="0.0.0.0", port=8079)
if __name__ == "__main__":
main()
## 测试