Skip to content

Commit

Permalink
Register port in port attribute provider (#140)
Browse files Browse the repository at this point in the history
* Add functions to register ports

* Addd debugport handler

* check if ort already exist

* fix lint

* fix error
  • Loading branch information
paulacamargo25 authored Jan 25, 2024
1 parent 4bf3b6a commit cd4dbdf
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/extension/debugger/debugPort/portAttributesProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,34 @@
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import { CancellationToken, PortAttributes, PortAttributesProvider, ProviderResult } from 'vscode';
import {
CancellationToken,
PortAttributes,
PortAttributesProvider,
PortAutoForwardAction,
ProviderResult,
} from 'vscode';

export class DebugPortAttributesProvider implements PortAttributesProvider {
private knownPorts: number[] = [];

public setPortAttribute(port: number): void {
if (!this.knownPorts.includes(port)) {
this.knownPorts.push(port);
}
}

public resetPortAttribute(): void {
this.knownPorts.pop();
}

public providePortAttributes(
_attributes: { port: number; pid?: number; commandLine?: string },
attributes: { port: number; pid?: number; commandLine?: string },
_token: CancellationToken,
): ProviderResult<PortAttributes> {
if (this.knownPorts.includes(attributes.port)) {
return new PortAttributes(PortAutoForwardAction.Ignore);
}
return undefined;
}
}
1 change: 1 addition & 0 deletions src/extension/debugger/hooks/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export enum DebuggerEvents {
// Event sent by PTVSD when a child process is launched and ready to be attached to for multi-proc debugging.
PtvsdAttachToSubprocess = 'ptvsd_attach',
DebugpyAttachToSubprocess = 'debugpyAttach',
DebugpySockets = 'debugpySockets',
}
43 changes: 43 additions & 0 deletions src/extension/debugger/hooks/debugpySocketsHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import { inject, injectable } from 'inversify';
import { DebugSessionCustomEvent } from 'vscode';
import { swallowExceptions } from '../../common/utils/decorators';
import { DebuggerEvents } from './constants';
import { DebuggerTypeName } from '../../constants';
import { DebugPortAttributesProvider } from '../debugPort/portAttributesProvider';
import { IDebugSessionEventHandlers } from './types';

/**
* This class is responsible for register ports using by debugpy in the portProvider.
* @export
* @class ChildProcessAttachEventHandler
* @implements {IDebugSessionEventHandlers}
*/
@injectable()
export class DebugpySocketsHandler implements IDebugSessionEventHandlers {
constructor(
@inject(DebugPortAttributesProvider) private readonly debugPortAttributesProvider: DebugPortAttributesProvider,
) {}

@swallowExceptions('Handle child process launch')
public async handleCustomEvent(event: DebugSessionCustomEvent): Promise<void> {
if (!event || event.session.configuration.type !== DebuggerTypeName) {
return;
}

if (event.event == DebuggerEvents.DebugpySockets) {

Check warning on line 32 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected '===' and instead saw '=='

Check warning on line 32 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 3.7)

Expected '===' and instead saw '=='

Check warning on line 32 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 3.8)

Expected '===' and instead saw '=='

Check warning on line 32 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 3.9)

Expected '===' and instead saw '=='

Check warning on line 32 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 3.10)

Expected '===' and instead saw '=='

Check warning on line 32 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 3.11)

Expected '===' and instead saw '=='

Check warning on line 32 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 3.9)

Expected '===' and instead saw '=='

Check warning on line 32 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 3.7)

Expected '===' and instead saw '=='

Check warning on line 32 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 3.8)

Expected '===' and instead saw '=='

Check warning on line 32 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 3.11)

Expected '===' and instead saw '=='

Check warning on line 32 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 3.10)

Expected '===' and instead saw '=='
let portSocket = event.body.sockets.find((socket: { [x: string]: any }) => {
return socket['internal'] == false;

Check warning on line 34 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected '===' and instead saw '=='

Check warning on line 34 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 3.7)

Expected '===' and instead saw '=='

Check warning on line 34 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 3.8)

Expected '===' and instead saw '=='

Check warning on line 34 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 3.9)

Expected '===' and instead saw '=='

Check warning on line 34 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 3.10)

Expected '===' and instead saw '=='

Check warning on line 34 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 3.11)

Expected '===' and instead saw '=='

Check warning on line 34 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 3.9)

Expected '===' and instead saw '=='

Check warning on line 34 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 3.7)

Expected '===' and instead saw '=='

Check warning on line 34 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 3.8)

Expected '===' and instead saw '=='

Check warning on line 34 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 3.11)

Expected '===' and instead saw '=='

Check warning on line 34 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 3.10)

Expected '===' and instead saw '=='
});
if (portSocket != undefined) {

Check warning on line 36 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Lint

Expected '!==' and instead saw '!='

Check warning on line 36 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 3.7)

Expected '!==' and instead saw '!='

Check warning on line 36 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 3.8)

Expected '!==' and instead saw '!='

Check warning on line 36 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 3.9)

Expected '!==' and instead saw '!='

Check warning on line 36 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 3.10)

Expected '!==' and instead saw '!='

Check warning on line 36 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (ubuntu-latest, 3.11)

Expected '!==' and instead saw '!='

Check warning on line 36 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 3.9)

Expected '!==' and instead saw '!='

Check warning on line 36 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 3.7)

Expected '!==' and instead saw '!='

Check warning on line 36 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 3.8)

Expected '!==' and instead saw '!='

Check warning on line 36 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 3.11)

Expected '!==' and instead saw '!='

Check warning on line 36 in src/extension/debugger/hooks/debugpySocketsHandler.ts

View workflow job for this annotation

GitHub Actions / Tests (windows-latest, 3.10)

Expected '!==' and instead saw '!='
this.debugPortAttributesProvider.setPortAttribute(portSocket.port);
}
} else {
return;
}
}
}
14 changes: 14 additions & 0 deletions src/extension/extensionInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { ignoreErrors } from './common/promiseUtils';
import { pickArgsInput } from './common/utils/localize';
import { DebugPortAttributesProvider } from './debugger/debugPort/portAttributesProvider';
import { getConfigurationsByUri } from './debugger/configuration/launch.json/launchJsonReader';
import { DebugpySocketsHandler } from './debugger/hooks/debugpySocketsHandler';

export async function registerDebugger(context: IExtensionContext): Promise<void> {
const childProcessAttachService = new ChildProcessAttachService();
Expand Down Expand Up @@ -155,4 +156,17 @@ export async function registerDebugger(context: IExtensionContext): Promise<void
debugPortAttributesProvider,
),
);

const debugpySocketsHandler = new DebugpySocketsHandler(debugPortAttributesProvider);
context.subscriptions.push(
debug.onDidReceiveDebugSessionCustomEvent((e) => {
ignoreErrors(debugpySocketsHandler.handleCustomEvent(e));
}),
);

context.subscriptions.push(
debug.onDidTerminateDebugSession(() => {
debugPortAttributesProvider.resetPortAttribute();
}),
);
}

0 comments on commit cd4dbdf

Please sign in to comment.