Skip to content

Extension API - Determining the reason that debugging was terminated  #145159

@jimasp

Description

@jimasp

Context - I'm writing a vs code extension.

The callback for onDidTerminateDebugSession provides a DebugSession object, but that object does not indicate the reason that the debug session was terminated, or at least I can't find it in there.

I'd like to determine whether the debug session was either:
a) stopped by the user (clicking the stop button), or
b) the debug session reached conclusion (no more code to run) and the thread exited automatically.

At the moment I'm using this horrible hack as a workaround:

vscode.debug.registerDebugAdapterTrackerFactory('*', {
  createDebugAdapterTracker() {
    return {
      onDidSendMessage: m => {
        // 247 = magic number exit code that indicates user clicked stop
        if(m.event === "exited" && m.body?.exitCode === 247) {  
          debugStopClicked = true;
        }
      }
    };
  }
});

Unless an alternative method is available (please advise) then I'd like to request that onDidTerminateDebugSession either:
a. calls back with a DebugSession object that contains a property that indicates the reason for termination, or
b. calls back with a second parameter that contains the reason.


Edit: this version is slightly better for the case of MS debugpy as I discovered the exit code varies per platform:

    // onDidTerminateDebugSession doesn't provide reason for the stop,
    // so we need to check the reason from the debug adapter protocol
    context.subscriptions.push(vscode.debug.registerDebugAdapterTrackerFactory('*', {
      createDebugAdapterTracker() {
        let threadExit = false;

        return {
          onDidSendMessage: (m) => {

            // https://github.com/microsoft/vscode-debugadapter-node/blob/main/debugProtocol.json
            // console.log(m);

            if (m.body?.reason === "exited" && m.body?.threadId) {
              // thread exit
              threadExit = true;
              return;
            }

            if (m.event === "exited") {
              if (!threadExit) {
                // exit, but not a thread exit, so we need to set flag to 
                // stop the run, (most likely debug was stopped by user)
                debugStopClicked = true;
              }
            }

          },
        };
      }
    }));
    

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions