Skip to content

Commit

Permalink
Introduce debug adapter protocol
Browse files Browse the repository at this point in the history
Signed-off-by: Anatoliy Bazko <abazko@redhat.com>
  • Loading branch information
tolusha committed Aug 21, 2018
1 parent 7c580ac commit 6a03487
Show file tree
Hide file tree
Showing 41 changed files with 4,890 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ cache:
- packages/bunyan/node_modules
- packages/callhierarchy/node_modules
- packages/core/node_modules
- packages/debug/node_modules
- packages/debug-nodejs/node_modules
- packages/cpp/node_modules
- packages/editorconfig/node_modules
- packages/editor/node_modules
Expand Down
2 changes: 2 additions & 0 deletions examples/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"@theia/callhierarchy": "^0.3.13",
"@theia/core": "^0.3.13",
"@theia/cpp": "^0.3.13",
"@theia/debug": "^0.3.13",
"@theia/debug-nodejs": "^0.3.13",
"@theia/editor": "^0.3.13",
"@theia/editorconfig": "^0.3.13",
"@theia/extension-manager": "^0.3.13",
Expand Down
4 changes: 3 additions & 1 deletion examples/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"@theia/callhierarchy": "^0.3.13",
"@theia/core": "^0.3.13",
"@theia/cpp": "^0.3.13",
"@theia/debug": "^0.3.13",
"@theia/debug-nodejs": "^0.3.13",
"@theia/editor": "^0.3.13",
"@theia/editorconfig": "^0.3.13",
"@theia/extension-manager": "^0.3.13",
Expand Down Expand Up @@ -55,4 +57,4 @@
"devDependencies": {
"@theia/cli": "^0.3.13"
}
}
}
2 changes: 1 addition & 1 deletion packages/core/src/browser/shell/tab-bars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class TabBarRenderer extends TabBar.Renderer {
const iconSize = data.iconSize;
let height: string | undefined;
if (labelSize || iconSize) {
const labelHeight = labelSize ? labelSize.width : 0;
const labelHeight = labelSize ? (this.tabBar && this.tabBar.orientation === 'horizontal' ? labelSize.height : labelSize.width) : 0;
const iconHeight = iconSize ? iconSize.height : 0;
let paddingTop = data.paddingTop || 0;
if (labelHeight > 0 && iconHeight > 0) {
Expand Down
7 changes: 7 additions & 0 deletions packages/debug-nodejs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Theia - NodeJS Debug Extension

See [here](https://github.com/theia-ide/theia) for a detailed documentation.

## License
- [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
- [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
10 changes: 10 additions & 0 deletions packages/debug-nodejs/compile.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../configs/base.tsconfig",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
},
"include": [
"src"
]
}
52 changes: 52 additions & 0 deletions packages/debug-nodejs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@theia/debug-nodejs",
"version": "0.3.13",
"description": "Theia - NodeJS Debug Extension",
"dependencies": {
"@theia/debug": "^0.3.13",
"vscode-debugprotocol": "^1.26.0"
},
"publishConfig": {
"access": "public"
},
"theiaExtensions": [
{
"backend": "lib/node/debug-nodejs-backend-module"
}
],
"keywords": [
"theia-extension, debug, nodejs"
],
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/theia-ide/theia.git"
},
"bugs": {
"url": "https://github.com/theia-ide/theia/issues"
},
"homepage": "https://github.com/theia-ide/theia",
"files": [
"lib",
"src",
"scripts"
],
"scripts": {
"prepare": "yarn run clean && yarn run build",
"clean": "theiaext clean",
"build": "concurrently -n download,build -c red,blue \"node ./scripts/download-vscode-node-debug.js\" \"theiaext build\"",
"watch": "theiaext watch",
"test": "theiaext test",
"docs": "theiaext docs"
},
"devDependencies": {
"@theia/ext-scripts": "^0.3.13"
},
"nyc": {
"extends": "../../configs/nyc.json"
},
"debugAdapter": {
"downloadUrl": "https://github.com/tolusha/node-debug/releases/download/v1.23.5/vscode-node-debug.tar.gz",
"dir": "lib/adapter"
}
}
92 changes: 92 additions & 0 deletions packages/debug-nodejs/scripts/download-vscode-node-debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/********************************************************************************
* Copyright (C) 2018 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

const fs = require('fs');
const https = require('https');
const http = require('http');
const path = require('path');
const zlib = require('zlib');
const tar = require('tar');
const mkdirp = require('mkdirp');
const packageJson = require('../package.json');
const downloadUrl = packageJson['debugAdapter']['downloadUrl'];
const downloadPath = path.join(__dirname, '../node_modules/download');
const archivePath = path.join(downloadPath, path.basename(downloadUrl));
const targetPath = packageJson['debugAdapter']['dir'];

function downloadDap() {
return new Promise((resolve, reject) => {
if (fs.existsSync(archivePath)) {
resolve();
return;
}

if (!fs.existsSync(downloadPath)) {
fs.mkdirSync(downloadPath);
}

const file = fs.createWriteStream(archivePath);
const downloadWithRedirect = url => {
const h = url.toString().startsWith('https') ? https : http;
h.get(url, response => {
const statusCode = response.statusCode;
const redirectLocation = response.headers.location;
if (statusCode >= 300 && statusCode < 400 && redirectLocation) {
console.log('Redirect location: ' + redirectLocation);
downloadWithRedirect(redirectLocation);
} else if (statusCode === 200) {
response.on('end', () => resolve());
response.on('error', e => {
file.destroy();
reject(e);
});
response.pipe(file);
} else {
file.destroy();
reject(new Error(`Failed to download 'VSCode Node Debug' with error code: ${statusCode}`));
}
})
};

downloadWithRedirect(downloadUrl);
});
}

decompressArchive = function () {
return new Promise((resolve, reject) => {
if (!fs.existsSync(archivePath)) {
reject(new Error(`The archive was not found at ${archivePath}.`));
return;
}

if (!fs.existsSync(targetPath)) {
mkdirp.sync(targetPath);
}

const gunzip = zlib.createGunzip({ finishFlush: zlib.Z_SYNC_FLUSH, flush: zlib.Z_SYNC_FLUSH });
const untar = tar.x({ cwd: targetPath });
fs.createReadStream(archivePath).pipe(gunzip).pipe(untar)
.on('error', e => reject(e))
.on('end', () => resolve());
});
}

downloadDap().then(() => {
decompressArchive();
}).catch(error => {
console.error(error);
process.exit(1);
});
23 changes: 23 additions & 0 deletions packages/debug-nodejs/src/node/debug-nodejs-backend-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/********************************************************************************
* Copyright (C) 2018 Red Hat, Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { ContainerModule } from 'inversify';
import { NodeJsDebugAdapterContribution } from './debug-nodejs';
import { DebugAdapterContribution } from '@theia/debug/lib/node/debug-model';

export default new ContainerModule(bind => {
bind(DebugAdapterContribution).to(NodeJsDebugAdapterContribution).inSingletonScope();
});
15 changes: 15 additions & 0 deletions packages/debug-nodejs/src/node/debug-nodejs.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/********************************************************************************
* Copyright (C) 2018 Red Hat, Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
64 changes: 64 additions & 0 deletions packages/debug-nodejs/src/node/debug-nodejs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/********************************************************************************
* Copyright (C) 2018 Red Hat, Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

const path = require('path');
const packageJson = require('../../package.json');
const debugAdapterDir = packageJson['debugAdapter']['dir'];

import { injectable } from 'inversify';
import { DebugConfiguration } from '@theia/debug/lib/common/debug-common';
import { DebugAdapterContribution, DebugAdapterExecutable } from '@theia/debug/lib/node/debug-model';

@injectable()
export class NodeJsDebugAdapterContribution implements DebugAdapterContribution {
readonly debugType = 'node';

provideDebugConfigurations = [{
type: this.debugType,
breakpoints: { filePatterns: ['[.]js$', '[.]ts$'] },
request: 'attach',
name: 'Attach by PID',
processId: ''
}];

resolveDebugConfiguration(config: DebugConfiguration): DebugConfiguration {
config.breakpoints = { filePatterns: ['[.]js$', '[.]ts$'] };

if (!config.request) {
throw new Error('Debug request type is not provided.');
}

switch (config.request) {
case 'attach': this.validateAttachConfig(config);
}

return config;
}

provideDebugAdapterExecutable(config: DebugConfiguration): DebugAdapterExecutable {
const program = path.join(__dirname, `../../${debugAdapterDir}/out/src/nodeDebug.js`);
return {
program,
runtime: 'node'
};
}

private validateAttachConfig(config: DebugConfiguration) {
if (!config.processId) {
throw new Error('PID is not provided.');
}
}
}
37 changes: 37 additions & 0 deletions packages/debug/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Architecture
`DebugService` is used to initialize a new `DebugSession`. This service provides functionality to configure and to start a new debug session. The workflow is the following. If user wants to debug an application and there is no debug configuration associated with the application then the list of available debuggers is requested to create a suitable debug configuration. When configuration is chosen it is possible to alter the configuration by filling in missing values or by adding/changing/removing attributes.

In most cases the default behavior of the `DebugSession` is enough. But it is possible to provide its own implementation. The `DebugSessionFactory` is used for this purpose via `DebugSessionContribution`. Documented model objects are located [here](https://github.com/theia-ide/theia/tree/master/packages/debug/src/browser/debug-model.ts)

### Debug Session life-cycle API
`DebugSession` life-cycle is controlled and can be tracked as follows:
* An `onDidPreCreateDebugSession` event indicates that a debug session is going to be created.
* An `onDidCreateDebugSession` event indicates that a debug session has been created.
* An `onDidDestroyDebugSession` event indicates that a debug session has terminated.
* An `onDidChangeActiveDebugSession` event indicates that an active debug session has been changed

### Breakpoints API
`ExtDebugProtocol.AggregatedBreakpoint` is used to handle breakpoints on the client side. It covers all three breakpoint types: `DebugProtocol.SourceBreakpoint`, `DebugProtocol.FunctionBreakpoint` and `ExtDebugProtocol.ExceptionBreakpoint`. It is possible to identify a breakpoint type with help of `DebugUtils`. Notification about added, removed, or changed breakpoints is received via `onDidChangeBreakpoints`.

### Server side
At the back-end we start a debug adapter using `DebugAdapterFactory` and then a `DebugAdapterSession` is instantiated which works as a proxy between client and debug adapter. If a default implementation of the debug adapter session does not fit needs, it is possible to provide its own implementation using `DebugAdapterSessionFactory`. If so, it is recommended to extend the default implementation of the `DebugAdapterSession`. Documented model objects are located [here](https://github.com/theia-ide/theia/tree/master/packages/debug/src/node/debug-model.ts)

`DebugSessionState` accumulates debug adapter events and is used to restore debug session on the client side when page is refreshed.

## How to contribute a new debugger
`DebugAdapterContribution` is a contribution point for all debug adapters to provide and resolve debug configuration.

Here is an example of [debug adapter contribution for node](https://github.com/theia-ide/theia/tree/master/packages/debug-nodejs/src/node/debug-nodejs.ts)

## References
* [Debug Adapter Protocol](https://github.com/Microsoft/vscode-debugadapter-node/blob/master/protocol/src/debugProtocol.ts)
* [VS Code debug API](https://code.visualstudio.com/docs/extensionAPI/api-debugging)
* [Debug adapter example for VS Code](https://code.visualstudio.com/docs/extensions/example-debuggers)

## Debug adapter implementations for VS Code
* [Node debugger](https://github.com/Microsoft/vscode-node-debug)
* [Java debugger](https://github.com/Microsoft/vscode-java-debug)

## License
- [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
- [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
10 changes: 10 additions & 0 deletions packages/debug/compile.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../configs/base.tsconfig",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
},
"include": [
"src"
]
}
Loading

0 comments on commit 6a03487

Please sign in to comment.