microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.4

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/debugger/nodeDebugWrapper.ts

175lines · modeblame

65bb0c85Jimmy Thomson10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
d976d077Meena Kunnathur Balakrishnan10 years ago4import * as fs from "fs";
65bb0c85Jimmy Thomson10 years ago5import * as path from "path";
6import * as http from "http";
7
d976d077Meena Kunnathur Balakrishnan10 years ago8import {Telemetry} from "../common/telemetry";
dd442738Jimmy Thomson10 years ago9import {TelemetryHelper} from "../common/telemetryHelper";
c2bf3c4fdigeff10 years ago10import {ExtensionMessageSender, ExtensionMessage} from "../common/extensionMessaging";
d976d077Meena Kunnathur Balakrishnan10 years ago11
65bb0c85Jimmy Thomson10 years ago12// These typings do not reflect the typings as intended to be used
13// but rather as they exist in truth, so we can reach into the internals
14// and access what we need.
15declare module VSCodeDebugAdapter {
16class DebugSession {
17public static run: Function;
5547a16fJimmy Thomson10 years ago18public sendEvent(event: VSCodeDebugAdapter.InitializedEvent): void;
19public start(input: any, output: any): void;
20public launchRequest(response: any, args: any): void;
c2bf3c4fdigeff10 years ago21public disconnectRequest(response: any, args: any): void;
65bb0c85Jimmy Thomson10 years ago22}
23class InitializedEvent {
5547a16fJimmy Thomson10 years ago24constructor();
25}
26class OutputEvent {
27constructor(message: string, destination?: string);
28}
29class TerminatedEvent {
30constructor();
65bb0c85Jimmy Thomson10 years ago31}
32}
33
34declare class SourceMaps {
35public _sourceToGeneratedMaps: {};
36public _generatedToSourceMaps: {};
37public _allSourceMaps: {};
38}
39
5547a16fJimmy Thomson10 years ago40declare class NodeDebugSession extends VSCodeDebugAdapter.DebugSession {
65bb0c85Jimmy Thomson10 years ago41public _sourceMaps: SourceMaps;
42}
43
5e54f6f2Jimmy Thomson10 years ago44interface ILaunchArgs {
45platform: string;
46target?: string;
47internalDebuggerPort?: any;
48args: string[];
710f8655digeff10 years ago49logCatArguments: any;
5e54f6f2Jimmy Thomson10 years ago50}
51
2d3a052eMeena Kunnathur Balakrishnan10 years ago52let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version;
5547a16fJimmy Thomson10 years ago53
642490c1Jimmy Thomson10 years ago54function bailOut(reason: string): void {
55// Things have gone wrong in initialization: Report the error to telemetry and exit
56TelemetryHelper.sendSimpleEvent(reason);
6e4d7a62Joshua Skelton10 years ago57process.exit(1);
642490c1Jimmy Thomson10 years ago58}
59
c31e59fbdigeff10 years ago60function parseLogCatArguments(userProvidedLogCatArguments: any) {
61return Array.isArray(userProvidedLogCatArguments)
62? userProvidedLogCatArguments.join(" ") // If it's an array, we join the arguments
63: userProvidedLogCatArguments; // If not, we leave it as-is
64}
65
66function isNullOrUndefined(value: any): boolean {
67return typeof value === "undefined" || value === null;
68}
69
2d3a052eMeena Kunnathur Balakrishnan10 years ago70// Enable telemetry
41c61f9aJoshua Skelton10 years ago71Telemetry.init("react-native-debug-adapter", version, {isExtensionProcess: false});
72let nodeDebugFolder: string;
73let vscodeDebugAdapterPackage: typeof VSCodeDebugAdapter;
b8ef4af9Jimmy Thomson10 years ago74
41c61f9aJoshua Skelton10 years ago75// nodeDebugLocation.json is dynamically generated on extension activation.
76// If it fails, we must not have been in a react native project
77try {
2d3a052eMeena Kunnathur Balakrishnan10 years ago78/* tslint:disable:no-var-requires */
41c61f9aJoshua Skelton10 years ago79nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath;
80vscodeDebugAdapterPackage = require(path.join(nodeDebugFolder, "node_modules", "vscode-debugadapter"));
81/* tslint:enable:no-var-requires */
82} catch (e) {
83// Nothing we can do here: can't even communicate back because we don't know how to speak debug adapter
84bailOut("cannotFindDebugAdapter");
85}
65bb0c85Jimmy Thomson10 years ago86
41c61f9aJoshua Skelton10 years ago87// Temporarily dummy out the DebugSession.run function so we do not start the debug adapter until we are ready
88const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run;
89vscodeDebugAdapterPackage.DebugSession.run = function() { };
4881129dMeena Kunnathur Balakrishnan10 years ago90
41c61f9aJoshua Skelton10 years ago91let nodeDebug: { NodeDebugSession: typeof NodeDebugSession };
2d3a052eMeena Kunnathur Balakrishnan10 years ago92
41c61f9aJoshua Skelton10 years ago93try {
94/* tslint:disable:no-var-requires */
95nodeDebug = require(path.join(nodeDebugFolder, "out", "node", "nodeDebug"));
4881129dMeena Kunnathur Balakrishnan10 years ago96/* tslint:enable:no-var-requires */
41c61f9aJoshua Skelton10 years ago97} catch (e) {
98// Unable to find nodeDebug, but we can make our own communication channel now
99const debugSession = new vscodeDebugAdapterPackage.DebugSession();
100// Note: this will not work in the context of debugging the debug adapter and communicating over a socket,
101// but in that case we have much better ways to investigate errors.
102debugSession.start(process.stdin, process.stdout);
103debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr"));
104debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent());
105
106bailOut("cannotFindNodeDebugAdapter");
107}
4881129dMeena Kunnathur Balakrishnan10 years ago108
41c61f9aJoshua Skelton10 years ago109vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun;
110
111// Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments
112const originalNodeDebugSessionLaunchRequest = nodeDebug.NodeDebugSession.prototype.launchRequest;
113nodeDebug.NodeDebugSession.prototype.launchRequest = function(request: any, args: ILaunchArgs) {
114// Create a server waiting for messages to re-initialize the debug session;
115const reinitializeServer = http.createServer((req, res) => {
116res.statusCode = 404;
117if (req.url === "/refreshBreakpoints") {
118res.statusCode = 200;
119if (this) {
120const sourceMaps = this._sourceMaps;
121if (sourceMaps) {
122// Flush any cached source maps
123sourceMaps._allSourceMaps = {};
124sourceMaps._generatedToSourceMaps = {};
125sourceMaps._sourceToGeneratedMaps = {};
4881129dMeena Kunnathur Balakrishnan10 years ago126}
41c61f9aJoshua Skelton10 years ago127// Send an "initialized" event to trigger breakpoints to be re-sent
128this.sendEvent(new vscodeDebugAdapterPackage.InitializedEvent());
4881129dMeena Kunnathur Balakrishnan10 years ago129}
710f8655digeff10 years ago130}
41c61f9aJoshua Skelton10 years ago131res.end();
132});
133const debugServerListeningPort = parseInt(args.internalDebuggerPort, 10) || 9090;
134
135reinitializeServer.listen(debugServerListeningPort);
136reinitializeServer.on("error", (err: Error) => {
137TelemetryHelper.sendSimpleEvent("reinitializeServerError");
138this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Error in debug adapter server: " + err.toString(), "stderr"));
139this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Breakpoints may not update. Consider restarting and specifying a different 'internalDebuggerPort' in launch.json"));
140});
141
142// We do not permit arbitrary args to be passed to our process
143args.args = [
144args.platform,
145debugServerListeningPort.toString(),
146args.target || "simulator",
147];
148
149if (!isNullOrUndefined(args.logCatArguments)) { // We add the parameter if it's defined (adapter crashes otherwise)
150args.args = args.args.concat([parseLogCatArguments(args.logCatArguments)]);
151}
710f8655digeff10 years ago152
41c61f9aJoshua Skelton10 years ago153originalNodeDebugSessionLaunchRequest.call(this, request, args);
154};
155
156// Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments
157const originalNodeDebugSessionDisconnectRequest = nodeDebug.NodeDebugSession.prototype.disconnectRequest;
158function customDisconnectRequest(response: any, args: any): void {
159try {
160// First we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session
161const extensionMessageSender = new ExtensionMessageSender();
162extensionMessageSender.sendMessage(ExtensionMessage.STOP_MONITORING_LOGCAT)
163.finally(() => originalNodeDebugSessionDisconnectRequest.call(this, response, args))
164.done(() => {}, reason => // We just print a warning if something fails
165process.stderr.write(`WARNING: Couldn't stop monitoring logcat: ${reason.message || reason}\n`));
166} catch (exception) {
167// This is a "nice to have" feature, so we just fire the message and forget. We don't event handle
168// errors in the response promise
169process.stderr.write(`WARNING: Couldn't stop monitoring logcat. Sync exception: ${exception.message || exception}\n`);
170originalNodeDebugSessionDisconnectRequest.call(this, response, args);
c2bf3c4fdigeff10 years ago171}
41c61f9aJoshua Skelton10 years ago172}
173nodeDebug.NodeDebugSession.prototype.disconnectRequest = customDisconnectRequest;
c2bf3c4fdigeff10 years ago174
41c61f9aJoshua Skelton10 years ago175vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession);