Skip to content

Commit

Permalink
Revert debugger changes to fix #279 (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-wiemer authored Jan 30, 2023
1 parent 000720c commit 2274c5c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 96 deletions.
21 changes: 19 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@
"dependencies": {
"@vscode/debugadapter": "^1.57.0",
"date-format": "^4.0.14",
"get-port": "^5.1.1",
"portfinder": "^1.0.32",
"xml2js": "^0.4.23"
},
Expand Down
111 changes: 47 additions & 64 deletions src/debugger/debugDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,76 +10,46 @@ import { StackHandler } from './handler/StackHandler';
import { VariableHandler } from './handler/variableHandler';
import { DbgpResponse } from './struct/dbgpResponse';
import { VarScope } from './struct/scope';
import * as portfinder from 'portfinder';

import getPort = require('get-port');
import { spawn } from 'child_process';
import { resolve } from 'path';
import { existsSync } from 'fs';
import { Out } from '../common/out';
import { Global, ConfigKey } from '../common/global';

/**
* An AHK runtime debugger.
* reference: https://xdebug.org/docs/dbgp
*/
/** An AHK runtime debugger, ref https://xdebug.org/docs/dbgp */
export class DebugDispatcher extends EventEmitter {
private port: number;
private debugServer: DebugServer;
private breakPointHandler: BreakPointHandler;
private commandHandler: CommandHandler;
private stackHandler: StackHandler;
private variableHandler: VariableHandler;
private startArgs: LaunchRequestArguments;

/**
* Start executing the given program.
*/
public async start(args: LaunchRequestArguments) {
await this.initDebugger(args, args.dbgpSettings);

const runtime = args.runtime ?? Global.getConfig(ConfigKey.executePath);
if (!args.program) {
args.program = await RunnerService.getPathByActive();
}
if (!existsSync(runtime)) {
Out.log(`AutoHotkey execute bin not found: ${runtime}`);
this.end();
return;
}
const ahkArgs = [
'/ErrorStdOut',
`/debug=localhost:${this.port}`,
args.program,
];
const ahkProcess = spawn(runtime, ahkArgs, {
cwd: `${resolve(args.program, '..')}`,
});
this.emit('output', `${runtime} ${ahkArgs.join(' ')}`);
ahkProcess.stderr.on('data', (err) => {
this.emit('output', err.toString('utf8'));
this.end();
});
public constructor() {
super();
}

private async initDebugger(
args: LaunchRequestArguments,
/** Start executing the given program. */
public async start(args: LaunchRequestArguments) {
let { runtime, dbgpSettings = {} } = args;
// names may used by AHK, let's not change them for now
// eslint-disable-next-line @typescript-eslint/naming-convention
dbgpSettings: { max_children?: number; max_data?: number } = {},
) {
if (this.port) {
return;
const { maxChildren, maxData }: LaunchRequestArguments['dbgpSettings'] =
{
maxChildren: 300,
maxData: 131072,
...dbgpSettings,
};
if (!runtime) {
runtime = Global.getConfig(ConfigKey.executePath);
}
// eslint-disable-next-line @typescript-eslint/naming-convention
const { max_children = 300, max_data = 131072 } = dbgpSettings;

this.breakPointHandler = new BreakPointHandler();
this.stackHandler = new StackHandler();
this.variableHandler = new VariableHandler();
this.startArgs = args;
const port = await portfinder.getPortPromise({
port: 9000,
stopPort: 9100,
});
Out.debug(`Creating debug server, port is ${port}`);
const port = await getPort({ port: getPort.makeRange(9000, 9100) });
this.debugServer = new DebugServer(port);
this.commandHandler = new CommandHandler(this.debugServer);
this.debugServer
Expand All @@ -89,9 +59,9 @@ export class DebugDispatcher extends EventEmitter {
this.setBreakPonit(bp);
});
this.sendCommand(
`feature_set -n max_children -v ${max_children}`,
`feature_set -n max_children -v ${maxChildren}`,
);
this.sendCommand(`feature_set -n max_data -v ${max_data}`);
this.sendCommand(`feature_set -n max_data -v ${maxData}`);
this.sendCommand(`feature_set -n max_depth -v 2`); // Get properties recursively. Therefore fixed at 2
this.sendCommand('stdout -c 1');
this.sendCommand('stderr -c 1');
Expand Down Expand Up @@ -122,8 +92,31 @@ export class DebugDispatcher extends EventEmitter {
}
}
});
this.port = port;
return port;
if (!args.program) {
args.program = await RunnerService.getPathByActive();
}

if (!existsSync(runtime)) {
Out.log(`AutoHotkey execute bin not found: ${runtime}`);
this.end();
return;
}

const ahkProcess = spawn(
runtime,
['/ErrorStdOut', `/debug=localhost:${port}`, args.program],
{ cwd: `${resolve(args.program, '..')}` },
);
ahkProcess.stderr.on('data', (err) => {
this.emit('output', err.toString('utf8'));
this.end();
});
}

public async restart() {
this.sendCommand('stop');
this.end();
RunnerService.startDebugger(this.startArgs.program);
}

/**
Expand All @@ -137,28 +130,19 @@ export class DebugDispatcher extends EventEmitter {
return null;
}

public async restart() {
this.sendCommand('stop');
this.debugServer.prepareNewConnection();
this.start(this.startArgs);
}

/**
* receive stop request from vscode, send command to notice the script stop.
*/
public stop() {
this.sendCommand('stop');
this.debugServer.shutdown();
this.emit('output', `The debugger has stopped.`);
}

/**
* receive end message from script, send event to stop the debug session.
*/
public end() {
if (!this.debugServer.hasNew) {
this.emit('end');
}
this.emit('end');
this.debugServer.shutdown();
}

Expand Down Expand Up @@ -273,8 +257,7 @@ export class DebugDispatcher extends EventEmitter {
`property_set -d ${frameId} -c ${scope} -n ${fullname} -t ${type}`,
value,
);
const success = !!parseInt(response.attr.success);
if (!success) {
if (!parseInt(response.attr.success)) {
throw new Error(
`"${fullname}" cannot be written. Probably read-only.`,
);
Expand Down
18 changes: 1 addition & 17 deletions src/debugger/debugServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Out } from '../common/out';
export class DebugServer extends EventEmitter {
private proxyServer: Net.Server;
private ahkConnection: Net.Socket;
public hasNew: boolean;
public constructor(private port: number) {
super();
}
Expand Down Expand Up @@ -39,27 +38,12 @@ export class DebugServer extends EventEmitter {
return this;
}

public prepareNewConnection() {
this.closeAhkConnection();
this.hasNew = true;
}

public closeAhkConnection() {
public shutdown() {
if (this.ahkConnection) {
this.ahkConnection.end();
this.ahkConnection = null;
}
}

public shutdown() {
this.closeAhkConnection();
if (this.hasNew) {
this.hasNew = false;
return;
}
if (this.proxyServer) {
this.proxyServer.close();
this.proxyServer = null;
}
}

Expand Down
15 changes: 2 additions & 13 deletions src/debugger/debugSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ export interface LaunchRequestArguments
/** An absolute path to the AutoHotkey.exe. */
runtime: string;
dbgpSettings: {
// eslint-disable-next-line @typescript-eslint/naming-convention
max_children?: number;
// eslint-disable-next-line @typescript-eslint/naming-convention
max_data?: number;
maxChildren: number;
maxData: number;
};
}

Expand Down Expand Up @@ -82,7 +80,6 @@ export class DebugSession extends LoggingDebugSession {
supportsDataBreakpoints: false,
supportsCompletionsRequest: true,
supportsCancelRequest: true,
supportsTerminateRequest: true,
supportsRestartRequest: true,
supportsBreakpointLocationsRequest: false,
supportsSetVariable: true,
Expand Down Expand Up @@ -113,14 +110,6 @@ export class DebugSession extends LoggingDebugSession {
response: DebugProtocol.DisconnectResponse,
args: DebugProtocol.DisconnectArguments,
request?: DebugProtocol.Request,
): void {
this.sendResponse(response);
}

protected terminateRequest(
response: DebugProtocol.TerminateResponse,
args: DebugProtocol.TerminateArguments,
request?: DebugProtocol.Request,
): void {
this.dispatcher.stop();
this.sendResponse(response);
Expand Down

0 comments on commit 2274c5c

Please sign in to comment.