Handle MAME SIGTERM during source debugging
This commit is contained in:
@@ -93,6 +93,10 @@ def main():
|
||||
parser.add_argument('--launch-path', default=None)
|
||||
parser.add_argument('--exit-while-stopped', action='store_true',
|
||||
help='аварийно завершить собственный MAME на main и ждать DAP terminated')
|
||||
parser.add_argument('--stop-while-stopped', action='store_true',
|
||||
help='послать DAP disconnect на main и проверить завершение MAME')
|
||||
parser.add_argument('--term-while-stopped', action='store_true',
|
||||
help='послать SIGTERM собственному MAME на main и проверить DAP terminated')
|
||||
options, _ = parser.parse_known_args()
|
||||
if not PYTHON.is_file():
|
||||
raise RuntimeError('Нет local pyenv shim: '+str(PYTHON))
|
||||
@@ -124,6 +128,50 @@ def main():
|
||||
frame = frame_response['body']['stackFrames'][0]
|
||||
if frame['name'] != 'main':
|
||||
raise RuntimeError('Не main: '+str(frame))
|
||||
if options.stop_while_stopped:
|
||||
mame_pid = launched['body']['mamePid']
|
||||
send(process, 5, 'disconnect')
|
||||
_, buffer, _ = wait_response(process, buffer, 'disconnect', 10)
|
||||
deadline = time.monotonic()+12
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
os.kill(mame_pid, 0)
|
||||
except ProcessLookupError:
|
||||
break
|
||||
time.sleep(.1)
|
||||
else:
|
||||
raise RuntimeError('После DAP disconnect MAME остался запущен')
|
||||
code = process.wait(timeout=3)
|
||||
if code != 0:
|
||||
raise RuntimeError(f'После DAP disconnect адаптер завершился с rc={code}')
|
||||
print(json.dumps({'event': 'vscode_stop_closes_mame', 'frame': frame},
|
||||
ensure_ascii=False), flush=True)
|
||||
return 0
|
||||
if options.term_while_stopped:
|
||||
mame_pid = launched['body']['mamePid']
|
||||
os.kill(mame_pid, signal.SIGTERM)
|
||||
deadline = time.monotonic()+12
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
message, buffer = receive(process, buffer, deadline-time.monotonic())
|
||||
except TimeoutError:
|
||||
break
|
||||
if message.get('event') == 'terminated':
|
||||
print(json.dumps({'event': 'sigterm_terminates_dap', 'frame': frame},
|
||||
ensure_ascii=False), flush=True)
|
||||
send(process, 5, 'disconnect')
|
||||
wait_response(process, buffer, 'disconnect', 10)
|
||||
return 0
|
||||
try:
|
||||
os.kill(mame_pid, 0)
|
||||
status = 'alive'
|
||||
except ProcessLookupError:
|
||||
status = 'exited'
|
||||
process_state = subprocess.run(
|
||||
['ps', '-p', str(mame_pid), '-o', 'stat=,ppid=,comm='],
|
||||
capture_output=True, text=True, check=False).stdout.strip()
|
||||
raise RuntimeError('SIGTERM: DAP terminated не получен за 12 с; MAME '+
|
||||
status+'; ps='+repr(process_state))
|
||||
if options.exit_while_stopped:
|
||||
os.kill(launched['body']['mamePid'], signal.SIGKILL)
|
||||
deadline = time.monotonic()+15
|
||||
|
||||
Reference in New Issue
Block a user