【问题标题】:My Visual Code Debug Extension, doesn't execute my debugger我的可视化代码调试扩展,不执行我的调试器
【发布时间】:2020-02-21 03:43:22
【问题描述】:

我创建了一个可视化代码调试器扩展,并且在我的扩展激活例程中,我可以告诉它在我调试示例 *.xyz 文件时被激活,因为它写出 XYZ 调试器激活 到控制台。问题是,我的名为 debugger.exe 的调试器可执行文件(这是一个用 C# 编写的控制台应用程序)没有被执行。

当我关闭 *.xyz 文件时,我在 deactivate 扩展函数中的断点被命中。这让我相信,在大多数情况下,该扩展程序在一定程度上起作用,但并非完全起作用。

我做错了什么?

这是我的 extension.ts 文件

'use strict';
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import { WorkspaceFolder, DebugConfiguration, ProviderResult, CancellationToken } from 'vscode';
import * as Net from 'net';


// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

    // Use the console to output diagnostic information (console.log) and errors (console.error)
    // This line of code will only be executed once when your extension is activated
    console.log('XYZ-Debugger Activated');   
}

// this method is called when your extension is deactivated
export function deactivate() {
    console.log('XYZ-Debugger Deactivated');
}

这是我的 package.json 文件:

{
    "name": "xyz-debugger",
    "displayName": "XYZ Debugger",
    "description": "Test Debugger.",
    "version": "0.0.1",
    "publisher": "Ed_Starkey",

    "engines": {
        "vscode": "^1.24.0",
        "node": "^6.3.0"
    },
    "categories": [
        "Debuggers"        
    ],
    "dependencies": {
        "vscode-debugprotocol": "^1.20.0",
        "vscode-nls": "^2.0.2"

    },
    "activationEvents": [
        "onDebug"

    ],
    "main": "./out/extension",

    "contributes": {      

        "breakpoints": [
            {
                "language": "xyz"
            }
        ],

        "debuggers": [{
        "type": "XYZDebug",
        "label": "XYZ Debug",

        "windows": {
            "program": "program": "./out/debugger.exe"
        },

        "languages": [
            {
                "id": "xyz",
                "extensions": [
                    ".xyz"
                ],
                "aliases": [
                    "XYZ"
                ]

            }],        

            "configurationAttributes": {
                "launch": {
                    "required": [
                        "program"
                    ],
                    "properties": {
                        "program": {
                            "type": "string",
                            "description": "The program to debug."
                        }
                    }
                }
            },

            "initialConfigurations": [
                {
                    "type": "xyz",
                    "request": "launch",
                    "name": "Launch Program",
                    "windows": {
                        "program": "./out/debugger.exe"
                    }                
                }
            ],        
        }]
    },
    "scripts": {
        "vscode:prepublish": "npm run compile",
        "compile": "tsc -p ./",
        "watch": "tsc -watch -p ./",
        "postinstall": "node ./node_modules/vscode/bin/install",
        "test": "npm run compile && node ./node_modules/vscode/bin/test"
    },
    "devDependencies": {
        "typescript": "^2.5.3",
        "vscode": "^1.1.5",
        "@types/node": "^7.0.43",       
        "vscode-debugadapter-testsupport":"^1.29.0"

    }    
}

这是我的 launch.json:

// A launch configuration that compiles the extension and then opens it inside a new window
{
    "version": "0.1.0",
    "configurations": [
        {
            "name": "Launch XYZ Debug",
            "type": "extensionHost",
            "request": "launch",
            "runtimeExecutable": "${execPath}",
            "args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
            "stopOnEntry": false,
            "sourceMaps": true,
            "outFiles": [ "${workspaceRoot}/out/**/*.js" ],
            "preLaunchTask": "npm: watch"
        },
        {
            "name": "Launch Tests",
            "type": "extensionHost",
            "request": "launch",
            "runtimeExecutable": "${execPath}",
            "args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ],
            "stopOnEntry": false,
            "sourceMaps": true,
            "outFiles": [ "${workspaceRoot}/out/test/**/*.js" ],
            "preLaunchTask": "npm: watch"
        }
    ]
}

【问题讨论】:

    标签: visual-studio-code vscode-extensions vscode-debugger


    【解决方案1】:

    您没有在activate 中注册您希望 VSCode 执行的操作。

    这样的事情会做你想做的事:

    let factory: XYZDebugAdapterDescriptorFactory
    
    export function activate(context: vscode.ExtensionContext) {
        const debugProvider = new XYZDebugConfigProvider();
        factory = new XYZDebugAdapterDescriptorFactory();
        context.subscriptions.push(
            vscode.debug.registerDebugConfigurationProvider(
                'xyz', debugProvider
            )
        );
        context.subscriptions.push(
            vscode.debug.onDidReceiveDebugSessionCustomEvent(
                handleCustomEvent
            )
        );
        context.subscriptions.push(vscode.debug.registerDebugAdapterDescriptorFactory('xyz', factory));
        context.subscriptions.push(factory);
    
    }
    

    然后您需要为配置提供一个适配器。这将连接到提供端口上的 DAP 服务器或启动 xyz.exe

    class XYZDebugConfigProvider implements vscode.DebugConfigurationProvider {
        resolveDebugConfiguration(folder: WorkspaceFolder | undefined, config: DebugConfiguration, token?: CancellationToken): ProviderResult<DebugConfiguration> {
            const editor = vscode.window.activeTextEditor;
            const defaultConfig = vscode.extensions
                .getExtension("YOUR EXTENSION NAME HERE")
                .packageJSON
                .contributes
                .debuggers[0]
                .initialConfigurations[0];
            if (!config.request && editor.document.languageId === 'xyz') {
                return defaultConfig;
            } else if (!config.request) {
                // Not trying to debug xyz?
                return undefined;
            }
            config = {
                ...defaultConfig,
                ...config
            }
            config.execArgs = (config.args || [])
                .concat(`-l${config.port}`);
    
            return config;
        }
    }
    
    class ElpsDebugAdapterDescriptorFactory implements vscode.DebugAdapterDescriptorFactory {
        private outputchannel: vscode.OutputChannel
    
        constructor() {
            this.outputchannel = vscode.window.createOutputChannel(
                'XYZ Debug Log'
            );
        }
    
        createDebugAdapterDescriptor(session: DebugSession, executable: DebugAdapterExecutable | undefined): ProviderResult<DebugAdapterDescriptor> {
            if (session.configuration.type !== "xyz") {
                return undefined
            }
            if (session.configuration.request == "launch") {
                const args = [...session.configuration.execArgs, session.configuration.program]
                this.outputchannel.appendLine(`Starting XYZ with "${session.configuration.executable}" and args "${args.join(" ")}"`)
                const debugee = spawn(session.configuration.executable, args)
                debugee.stderr.on('data', (data) => {
                    this.outputchannel.appendLine(data.toString())
                })
                debugee.stdout.on('data', (data) => {
                    this.outputchannel.appendLine(data.toString())
                })
                debugee.on("close", (data) => {
                    this.outputchannel.appendLine(`Process closed with status ${data}`)
                })
                debugee.on("error", (data) => {
                    this.outputchannel.appendLine(`Error from XYZ: ${data.message}:\n${data.stack}`)
                })
            }
            return new vscode.DebugAdapterServer(session.configuration.port)
        }
    
        dispose() {}
    }
    

    最后,您需要将您可能想要使用的其余配置添加到您的 package.json 和调试器中的 launch.json - 该部分在示例调试器 @987654321 的文档中得到了很好的介绍@

    以上内容改编自我正在编写的一个 ELISP 调试器,反过来又受到 https://marketplace.visualstudio.com/items?itemName=mortenhenriksen.perl-debug 出色的 Perl 6 支持的启发

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 2011-05-11
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      相关资源
      最近更新 更多