Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

launch.json env option to display numbers as hex while debugging #1801

Closed
wants to merge 19 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/debugAdapter/goDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import * as logger from 'vscode-debug-logger';
require('console-stamp')(console);

// This enum should stay in sync with https://golang.org/pkg/reflect/#Kind

enum GoReflectKind {
Invalid = 0,
Bool,
Expand Down Expand Up @@ -50,7 +49,6 @@ enum GoReflectKind {

// These types should stay in sync with:
// https://github.com/derekparker/delve/blob/master/service/api/types.go

interface CommandOut {
State: DebuggerState;
}
Expand Down Expand Up @@ -492,7 +490,7 @@ class GoDebugSession extends DebugSession {
private delve: Delve;
private localPathSeparator: string;
private remotePathSeparator: string;

private numAsHex: string;
private launchArgs: LaunchRequestArguments;

public constructor(debuggerLinesStartAt1: boolean, isServer: boolean = false) {
Expand All @@ -519,6 +517,19 @@ class GoDebugSession extends DebugSession {
if (/^(\w:[\\/]|\\\\)/.test(path)) return '\\';
return path.includes('/') ? '/' : '\\';
}
protected convertToHex(v) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sig should be private convertToHex(v: string): string. Also missing a new-line character between 519 and 520.

let s = v;
if (this.numAsHex === 'hex') {
s = '0x' + parseInt(v).toString(16);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will truncate floating-point numbers. Perhaps this functionality should be limited to integers?

}
else if (this.numAsHex === 'hexdec') {
s = '0x' + parseInt(v).toString(16) + ' (' + v + ')';
}
else if (this.numAsHex === 'hexascii') {
s = '0x' + parseInt(v).toString(16) + ' (' + String.fromCharCode(v) + ')';
}
return s;
}

protected launchRequest(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): void {
this.launchArgs = args;
Expand All @@ -533,6 +544,12 @@ class GoDebugSession extends DebugSession {
return;
}

// controls numbers display while debugging
// "hex" (shows hex only), "hexdec" ( shows hex (dec) ), "hexascii" ( shows hex (ascii) )
// default: shows dec only
// "DBG_SHOW_NUMBERS_HEX": "hex"
this.numAsHex = args.env['DBG_SHOW_NUMBERS_HEX'];

// Launch the Delve debugger on the program
let localPath = args.program;
let remotePath = args.remotePath || '';
Expand Down Expand Up @@ -844,9 +861,14 @@ class GoDebugSession extends DebugSession {
result: v.unreadable ? ('<' + v.unreadable + '>') : ('"' + val + '"'),
variablesReference: 0
};
} else {
} else if (v.kind === GoReflectKind.Bool) {
return {
result: v.value || ('<' + v.type + '>'),
variablesReference: this._variableHandles.create(v)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect and will cause boolean types to display like they have child elements. It should remain variablesReference: v.children.length > 0 ? this._variableHandles.create(v) : 0 like below which means the two clauses can be refactored.

};
} else {
return {
result: this.convertToHex(v.value) || ('<' + v.type + '>'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For types v.value will be an empty string, so convertToHex() needs to handle this case and return an empty string (with the current implementation you get 0xNaN in the variables window).

variablesReference: v.children.length > 0 ? this._variableHandles.create(v) : 0
};
}
Expand Down