Skip to content

Commit

Permalink
Merged PR 58: ExtensionDLL: Update WinDbg extension to retrieve JsDbg…
Browse files Browse the repository at this point in the history
… extension from Azure

Currently, the JsDbg WinDbg extension simply points to a script on iefs that retrieves the necessary files (also stored on iefs) and runs them. To decouple from iefs, the scripts and launch files are being moved into Azure, and the WinDbg extension is being modified to download these locally, using wininet.

To make this work, the JsDbg scripts also need to be updated to use the local files. This has been done for the unstable script. The same will be done for the stable script once the unstable version has been validated.
  • Loading branch information
sanketj committed Jan 30, 2019
1 parent 954ec27 commit 042597f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 9 deletions.
89 changes: 83 additions & 6 deletions server/ExtensionDLL/ExtensionDLL.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,101 @@
#include <stdio.h>
#include <windows.h>
#include <dbgeng.h>
#include <wininet.h>

const char StableCommand[] = "$$><\\\\iefs\\users\\psalas\\jsdbg\\support\\scripts\\jsdbg.script";
const char UnstableCommand[] = "$$><\\\\iefs\\users\\psalas\\jsdbg\\support\\scripts\\jsdbg-UNSTABLE.script";
#pragma comment(lib, "wininet.lib")
#pragma comment(lib, "urlmon.lib")

static bool downloadsPending;

extern "C" HRESULT CALLBACK DebugExtensionInitialize(PULONG Version, PULONG Flags)
{
*Version = DEBUG_EXTENSION_VERSION(1, 0);
*Flags = 0;
downloadsPending = true;
return S_OK;
}

static HRESULT DownloadFileToTempDir(const char* downloadURL, const char* fileName) {
TCHAR tempPath[MAX_PATH];
DWORD dwRetVal = GetTempPath(MAX_PATH, tempPath);
if (dwRetVal > MAX_PATH || (dwRetVal == 0)) { return E_FAIL; }
strcat_s(tempPath, fileName);

return URLDownloadToFile(NULL, downloadURL, tempPath, 0, NULL);
}

extern "C" HRESULT CALLBACK jsdbg(PDEBUG_CLIENT4 client, PCSTR args)
{
const char* command = StableCommand;
const char* commandScriptName;
const char* commandScriptDownloadURL;
const char* versionFileDownloadURL;
if (args != nullptr && strstr(args, "-u") == args) {
command = UnstableCommand;
commandScriptName = "jsdbg-UNSTABLE.script";
commandScriptDownloadURL = "https://jsdbg.blob.core.windows.net/launch-files/jsdbg-UNSTABLE.script";
versionFileDownloadURL = "https://jsdbg.blob.core.windows.net/launch-files/jsdbg-UNSTABLE-version.txt";
} else {
commandScriptName = "jsdbg.script";
commandScriptDownloadURL = "https://jsdbg.blob.core.windows.net/launch-files/jsdbg.script";
versionFileDownloadURL = "https://jsdbg.blob.core.windows.net/launch-files/jsdbg-version.txt";
}

if (downloadsPending) {
HRESULT hr = DownloadFileToTempDir("https://jsdbg.blob.core.windows.net/launch-files/JsDbg.exe", "JsDbg.exe");
if (hr != S_OK) {
return hr;
}

hr = DownloadFileToTempDir("https://jsdbg.blob.core.windows.net/launch-files/JsDbg.Remoting.dll", "JsDbg.Remoting.dll");
if (hr != S_OK) {
return hr;
}

hr = DownloadFileToTempDir(commandScriptDownloadURL, commandScriptName);
if (hr != S_OK) {
return hr;
}

char zipName[2048];
{
HINTERNET hSession = InternetOpen(TEXT("Jsdbg WinDbg extension"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_DONT_CACHE);
if (!hSession) { return E_FAIL; }
HINTERNET hConnect = InternetOpenUrl(hSession, versionFileDownloadURL, 0, 0, 0, 0);
if (!hConnect) { return E_FAIL; }

DWORD totalBytesRead = 0;
DWORD bytesRead;
do {
if (!InternetReadFile(hConnect, zipName, sizeof(zipName), &bytesRead)) { return E_FAIL; }
if (!bytesRead) { break; }
totalBytesRead += bytesRead;
} while (true);
zipName[totalBytesRead] = '\0';

InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
}

char zipDownloadURL[2048];
strcpy_s(zipDownloadURL, "https://jsdbg.blob.core.windows.net/launch-files/");
strcat_s(zipDownloadURL, zipName);
hr = DownloadFileToTempDir(zipDownloadURL, zipName);
if (hr != S_OK) {
return hr;
}

downloadsPending = false;
}

TCHAR commandScriptTempPath[MAX_PATH];
DWORD dwRetVal = GetTempPath(MAX_PATH, commandScriptTempPath);
if (dwRetVal > MAX_PATH || (dwRetVal == 0)) { return E_FAIL; }
strcat_s(commandScriptTempPath, commandScriptName);

TCHAR command[MAX_PATH];
strcpy_s(command, "$$><");
strcat_s(command, commandScriptTempPath);

// Get an IDebugControl and execute the command.
IDebugControl* control = nullptr;
HRESULT hr = S_OK;
Expand All @@ -45,12 +122,12 @@ extern "C" HRESULT CALLBACK help(PDEBUG_CLIENT4 client, PCSTR args)

if (args == nullptr || strlen(args) == 0)
{
control->Output(DEBUG_OUTPUT_NORMAL, "!jsdbg [-unstable] - Launches JsDbg, debugger extensions in the browser (http://aka.ms/jsdbg)\n");
control->Output(DEBUG_OUTPUT_NORMAL, "!jsdbg [-unstable] - Launches JsDbg, debugger extensions in the browser\n");
hr = DEBUG_EXTENSION_CONTINUE_SEARCH;
}
else if (strcmp(args, "jsdbg") == 0)
{
control->Output(DEBUG_OUTPUT_NORMAL, "JsDbg is a platform for debugger extensions that run in a web browser. For more information, see http://aka.ms/jsdbg.\n"
control->Output(DEBUG_OUTPUT_NORMAL, "JsDbg is a platform for debugger extensions that run in a web browser.\n"
"!jsdbg [-unstable]\n"
" -[u]nstable - Launches the latest unstable version of JsDbg.\n");
hr = S_OK;
Expand Down
6 changes: 3 additions & 3 deletions server/LaunchJsDbg/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static void LaunchJsDbg(string packagePath, string remoteString, bool launchSile
}

static void CleanupInstallationDirectory(string installationDirectory, string packageName) {
foreach (var file in Directory.GetFiles(installationDirectory)) {
foreach (var file in Directory.GetFiles(installationDirectory)) {
if (Path.GetFileName(file) == packageName) {
continue;
}
Expand All @@ -98,8 +98,8 @@ static void CleanupInstallationDirectory(string installationDirectory, string pa
}
}

foreach (var directory in Directory.GetDirectories(installationDirectory)) {
// First try to delete the JsDbg.exe to ensure that it's not running.
foreach (var directory in Directory.GetDirectories(installationDirectory)) {
// First try to delete the JsDbg.exe to ensure that it's not running.
if (Path.GetFileName(directory) == Path.GetFileNameWithoutExtension(packageName)) {
continue;
}
Expand Down

0 comments on commit 042597f

Please sign in to comment.