microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
928ab0497c3b596af9cbd20ea4b33b2ad02e79df

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/nodeDebugWrapper.ts

191lines · 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";
725cf712digeff10 years ago10import {RemoteExtension} from "../common/remoteExtension";
18d8ad2adigeff10 years ago11import {EntryPointHandler, ProcessType} from "../common/entryPointHandler";
12import {ErrorHelper} from "../common/error/errorHelper";
13import {InternalErrorCode} from "../common/error/internalErrorCode";
14import {ExtensionTelemetryReporter, NullTelemetryReporter, ReassignableTelemetryReporter} from "../common/telemetryReporters";
d976d077Meena Kunnathur Balakrishnan10 years ago15
65bb0c85Jimmy Thomson10 years ago16// These typings do not reflect the typings as intended to be used
17// but rather as they exist in truth, so we can reach into the internals
18// and access what we need.
19declare module VSCodeDebugAdapter {
20class DebugSession {
21public static run: Function;
5547a16fJimmy Thomson10 years ago22public sendEvent(event: VSCodeDebugAdapter.InitializedEvent): void;
23public start(input: any, output: any): void;
24public launchRequest(response: any, args: any): void;
c2bf3c4fdigeff10 years ago25public disconnectRequest(response: any, args: any): void;
65bb0c85Jimmy Thomson10 years ago26}
27class InitializedEvent {
5547a16fJimmy Thomson10 years ago28constructor();
29}
30class OutputEvent {
31constructor(message: string, destination?: string);
32}
33class TerminatedEvent {
34constructor();
65bb0c85Jimmy Thomson10 years ago35}
36}
37
38declare class SourceMaps {
39public _sourceToGeneratedMaps: {};
40public _generatedToSourceMaps: {};
41public _allSourceMaps: {};
42}
43
5547a16fJimmy Thomson10 years ago44declare class NodeDebugSession extends VSCodeDebugAdapter.DebugSession {
65bb0c85Jimmy Thomson10 years ago45public _sourceMaps: SourceMaps;
46}
47
5e54f6f2Jimmy Thomson10 years ago48interface ILaunchArgs {
49platform: string;
50target?: string;
51internalDebuggerPort?: any;
52args: string[];
710f8655digeff10 years ago53logCatArguments: any;
4f4e082ddigeff10 years ago54program: string;
5e54f6f2Jimmy Thomson10 years ago55}
56
2d3a052eMeena Kunnathur Balakrishnan10 years ago57let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version;
5547a16fJimmy Thomson10 years ago58
642490c1Jimmy Thomson10 years ago59function bailOut(reason: string): void {
60// Things have gone wrong in initialization: Report the error to telemetry and exit
61TelemetryHelper.sendSimpleEvent(reason);
6e4d7a62Joshua Skelton10 years ago62process.exit(1);
642490c1Jimmy Thomson10 years ago63}
64
c31e59fbdigeff10 years ago65function parseLogCatArguments(userProvidedLogCatArguments: any) {
66return Array.isArray(userProvidedLogCatArguments)
67? userProvidedLogCatArguments.join(" ") // If it's an array, we join the arguments
68: userProvidedLogCatArguments; // If not, we leave it as-is
69}
70
71function isNullOrUndefined(value: any): boolean {
72return typeof value === "undefined" || value === null;
73}
74
4f4e082ddigeff10 years ago75let projectRootPath: string = null;
76
2d3a052eMeena Kunnathur Balakrishnan10 years ago77// Enable telemetry
18d8ad2adigeff10 years ago78const telemetryReporter = new ReassignableTelemetryReporter(new NullTelemetryReporter());
79const appName = "react-native-debug-adapter";
80new EntryPointHandler(ProcessType.Debugger).runApp(appName, () => version,
81ErrorHelper.getInternalError(InternalErrorCode.DebuggingFailed), telemetryReporter, () => {
82let nodeDebugFolder: string;
83let vscodeDebugAdapterPackage: typeof VSCodeDebugAdapter;
84
85// nodeDebugLocation.json is dynamically generated on extension activation.
86// If it fails, we must not have been in a react native project
87try {
88/* tslint:disable:no-var-requires */
89nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath;
90vscodeDebugAdapterPackage = require(path.join(nodeDebugFolder, "node_modules", "vscode-debugadapter"));
91/* tslint:enable:no-var-requires */
92} catch (e) {
93// Nothing we can do here: can't even communicate back because we don't know how to speak debug adapter
94bailOut("cannotFindDebugAdapter");
95}
4881129dMeena Kunnathur Balakrishnan10 years ago96
18d8ad2adigeff10 years ago97// Temporarily dummy out the DebugSession.run function so we do not start the debug adapter until we are ready
98const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run;
99vscodeDebugAdapterPackage.DebugSession.run = function() { };
4881129dMeena Kunnathur Balakrishnan10 years ago100
18d8ad2adigeff10 years ago101let nodeDebug: { NodeDebugSession: typeof NodeDebugSession };
dd442738Jimmy Thomson10 years ago102
18d8ad2adigeff10 years ago103try {
104/* tslint:disable:no-var-requires */
105nodeDebug = require(path.join(nodeDebugFolder, "out", "node", "nodeDebug"));
106/* tslint:enable:no-var-requires */
107} catch (e) {
108// Unable to find nodeDebug, but we can make our own communication channel now
109const debugSession = new vscodeDebugAdapterPackage.DebugSession();
110// Note: this will not work in the context of debugging the debug adapter and communicating over a socket,
111// but in that case we have much better ways to investigate errors.
112debugSession.start(process.stdin, process.stdout);
113debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr"));
114debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent());
115
116bailOut("cannotFindNodeDebugAdapter");
117}
2d3a052eMeena Kunnathur Balakrishnan10 years ago118
18d8ad2adigeff10 years ago119vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun;
120
121// Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments
122const originalNodeDebugSessionLaunchRequest = nodeDebug.NodeDebugSession.prototype.launchRequest;
123nodeDebug.NodeDebugSession.prototype.launchRequest = function(request: any, args: ILaunchArgs) {
124projectRootPath = path.resolve(args.program, "../..");
125telemetryReporter.reassignTo(new ExtensionTelemetryReporter( // We start to send telemetry
126appName, version, Telemetry.APPINSIGHTS_INSTRUMENTATIONKEY, projectRootPath));
127
128// Create a server waiting for messages to re-initialize the debug session;
129const reinitializeServer = http.createServer((req, res) => {
130res.statusCode = 404;
131if (req.url === "/refreshBreakpoints") {
132res.statusCode = 200;
133if (this) {
134const sourceMaps = this._sourceMaps;
135if (sourceMaps) {
136// Flush any cached source maps
137sourceMaps._allSourceMaps = {};
138sourceMaps._generatedToSourceMaps = {};
139sourceMaps._sourceToGeneratedMaps = {};
140}
141// Send an "initialized" event to trigger breakpoints to be re-sent
142this.sendEvent(new vscodeDebugAdapterPackage.InitializedEvent());
48efc4d6Meena Kunnathur Balakrishnan10 years ago143}
4881129dMeena Kunnathur Balakrishnan10 years ago144}
18d8ad2adigeff10 years ago145res.end();
146});
147const debugServerListeningPort = parseInt(args.internalDebuggerPort, 10) || 9090;
148
149
150reinitializeServer.listen(debugServerListeningPort);
151reinitializeServer.on("error", (err: Error) => {
152TelemetryHelper.sendSimpleEvent("reinitializeServerError");
153this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Error in debug adapter server: " + err.toString(), "stderr"));
154this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Breakpoints may not update. Consider restarting and specifying a different 'internalDebuggerPort' in launch.json"));
155});
156
157// We do not permit arbitrary args to be passed to our process
158args.args = [
159args.platform,
160debugServerListeningPort.toString(),
161args.target || "simulator",
162];
163
164if (!isNullOrUndefined(args.logCatArguments)) { // We add the parameter if it's defined (adapter crashes otherwise)
165args.args = args.args.concat([parseLogCatArguments(args.logCatArguments)]);
4881129dMeena Kunnathur Balakrishnan10 years ago166}
710f8655digeff10 years ago167
18d8ad2adigeff10 years ago168originalNodeDebugSessionLaunchRequest.call(this, request, args);
169};
170
171// Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments
172const originalNodeDebugSessionDisconnectRequest = nodeDebug.NodeDebugSession.prototype.disconnectRequest;
173function customDisconnectRequest(response: any, args: any): void {
174try {
175// First we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session
c7f1165cdigeff10 years ago176const remoteExtension = RemoteExtension.atProjectRootPath(projectRootPath);
0766856fdigeff10 years ago177remoteExtension.stopMonitoringLogcat()
18d8ad2adigeff10 years ago178.finally(() => originalNodeDebugSessionDisconnectRequest.call(this, response, args))
179.done(() => { }, reason => // We just print a warning if something fails
180process.stderr.write(`WARNING: Couldn't stop monitoring logcat: ${reason.message || reason}\n`));
181} catch (exception) {
182// This is a "nice to have" feature, so we just fire the message and forget. We don't event handle
183// errors in the response promise
184process.stderr.write(`WARNING: Couldn't stop monitoring logcat. Sync exception: ${exception.message || exception}\n`);
185originalNodeDebugSessionDisconnectRequest.call(this, response, args);
4881129dMeena Kunnathur Balakrishnan10 years ago186}
c2bf3c4fdigeff10 years ago187}
18d8ad2adigeff10 years ago188nodeDebug.NodeDebugSession.prototype.disconnectRequest = customDisconnectRequest;
c2bf3c4fdigeff10 years ago189
18d8ad2adigeff10 years ago190vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession);
41c61f9aJoshua Skelton10 years ago191});