-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added examining memory locations (fix #64)
also added a way to run mi commands over unix domain sockets so other applications can communicate with code-debug
- Loading branch information
WebFreak001
committed
Jul 23, 2016
1 parent
e787333
commit 87ff8b8
Showing
5 changed files
with
184 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import * as vscode from "vscode"; | ||
import * as net from "net"; | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
import * as os from "os"; | ||
|
||
export function activate(context: vscode.ExtensionContext) { | ||
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider("debugmemory", new MemoryContentProvider())); | ||
context.subscriptions.push(vscode.commands.registerCommand("code-debug.examineMemoryLocation", examineMemory)); | ||
} | ||
|
||
var memoryLocationRegex = /^0x[0-9a-f]+$/; | ||
|
||
function getMemoryRange(range: string) { | ||
if (!range) | ||
return undefined; | ||
range = range.replace(/\s+/g, "").toLowerCase(); | ||
var index; | ||
if ((index = range.indexOf("+")) != -1) { | ||
var from = range.substr(0, index); | ||
var length = range.substr(index + 1); | ||
if (!memoryLocationRegex.exec(from)) | ||
return undefined; | ||
if (memoryLocationRegex.exec(length)) | ||
length = parseInt(length.substr(2), 16).toString(); | ||
return "from=" + encodeURIComponent(from) + "&length=" + encodeURIComponent(length); | ||
} | ||
else if ((index = range.indexOf("-")) != -1) { | ||
var from = range.substr(0, index); | ||
var to = range.substr(index + 1); | ||
if (!memoryLocationRegex.exec(from)) | ||
return undefined; | ||
if (!memoryLocationRegex.exec(to)) | ||
return undefined; | ||
return "from=" + encodeURIComponent(from) + "&to=" + encodeURIComponent(to); | ||
} | ||
else if (memoryLocationRegex.exec(range)) | ||
return "at=" + encodeURIComponent(range); | ||
else return undefined; | ||
} | ||
|
||
function examineMemory() { | ||
let socketlists = path.join(os.tmpdir(), "code-debug-sockets"); | ||
if (!fs.existsSync(socketlists)) | ||
return vscode.window.showErrorMessage("No debugging sessions available"); | ||
fs.readdir(socketlists, (err, files) => { | ||
if (err) | ||
return vscode.window.showErrorMessage("No debugging sessions available"); | ||
var pickedFile = (file) => { | ||
vscode.window.showInputBox({ placeHolder: "Memory Location or Range", validateInput: range => getMemoryRange(range) === undefined ? "Range must either be in format 0xF00-0xF01, 0xF100+32 or 0xABC154" : "" }).then(range => { | ||
vscode.commands.executeCommand("vscode.previewHtml", vscode.Uri.parse("debugmemory://" + file + "#" + getMemoryRange(range))); | ||
}); | ||
}; | ||
if (files.length == 1) | ||
pickedFile(files[0]); | ||
else if (files.length > 0) | ||
vscode.window.showQuickPick(files, { placeHolder: "Running debugging instance" }).then(file => pickedFile(file)); | ||
else | ||
vscode.window.showErrorMessage("No debugging sessions available"); | ||
}); | ||
} | ||
|
||
class MemoryContentProvider implements vscode.TextDocumentContentProvider { | ||
provideTextDocumentContent(uri: vscode.Uri, token: vscode.CancellationToken): Thenable<string> { | ||
return new Promise((resolve, reject) => { | ||
var conn = net.connect(path.join(os.tmpdir(), "code-debug-sockets", uri.authority)); | ||
var from, to; | ||
var highlightAt = -1; | ||
var splits = uri.fragment.split("&"); | ||
if (splits[0].split("=")[0] == "at") { | ||
var loc = parseInt(splits[0].split("=")[1].substr(2), 16); | ||
highlightAt = 64; | ||
from = Math.max(loc - 64, 0); | ||
to = Math.max(loc + 768, 0); | ||
} | ||
else if (splits[0].split("=")[0] == "from") { | ||
from = parseInt(splits[0].split("=")[1].substr(2), 16); | ||
if (splits[1].split("=")[0] == "to") { | ||
to = parseInt(splits[1].split("=")[1].substr(2), 16); | ||
} | ||
else if (splits[1].split("=")[0] == "length") { | ||
to = from + parseInt(splits[1].split("=")[1]); | ||
} | ||
else return reject("Invalid Range"); | ||
} | ||
else return reject("Invalid Range"); | ||
if (to < from) | ||
return reject("Negative Range"); | ||
conn.write("examineMemory " + JSON.stringify([from, to - from + 1])); | ||
conn.once("data", data => { | ||
var formattedCode = ""; | ||
var hexString = data.toString(); | ||
var x = 0; | ||
var asciiLine = ""; | ||
var byteNo = 0; | ||
for (var i = 0; i < hexString.length; i += 2) { | ||
var digit = hexString.substr(i, 2); | ||
var digitNum = parseInt(digit, 16); | ||
if (digitNum >= 32 && digitNum <= 126) | ||
asciiLine += String.fromCharCode(digitNum); | ||
else | ||
asciiLine += "."; | ||
if (highlightAt == byteNo) { | ||
formattedCode += "<b>" + digit + "</b> "; | ||
} else | ||
formattedCode += digit + " "; | ||
if (++x > 16) { | ||
formattedCode += asciiLine + "\n"; | ||
x = 0; | ||
asciiLine = ""; | ||
} | ||
byteNo++; | ||
} | ||
if (x > 0) { | ||
for (var i = 0; i <= 16 - x; i++) { | ||
formattedCode += " "; | ||
} | ||
formattedCode += asciiLine; | ||
} | ||
resolve("<h2>Memory Range from 0x" + from.toString(16) + " to 0x" + to.toString(16) + "</h2><code><pre>" + formattedCode + "</pre></code>"); | ||
conn.destroy(); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters