microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/nodeDebugWrapper.ts
191lines · modeblame
65bb0c85Jimmy Thomson10 years ago | 1 | // 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 ago | 4 | import * as fs from "fs"; |
65bb0c85Jimmy Thomson10 years ago | 5 | import * as path from "path"; |
| 6 | import * as http from "http"; | |
| 7 | | |
d976d077Meena Kunnathur Balakrishnan10 years ago | 8 | import {Telemetry} from "../common/telemetry"; |
dd442738Jimmy Thomson10 years ago | 9 | import {TelemetryHelper} from "../common/telemetryHelper"; |
725cf712digeff10 years ago | 10 | import {RemoteExtension} from "../common/remoteExtension"; |
18d8ad2adigeff10 years ago | 11 | import {EntryPointHandler, ProcessType} from "../common/entryPointHandler"; |
| 12 | import {ErrorHelper} from "../common/error/errorHelper"; | |
| 13 | import {InternalErrorCode} from "../common/error/internalErrorCode"; | |
| 14 | import {ExtensionTelemetryReporter, NullTelemetryReporter, ReassignableTelemetryReporter} from "../common/telemetryReporters"; | |
d976d077Meena Kunnathur Balakrishnan10 years ago | 15 | |
65bb0c85Jimmy Thomson10 years ago | 16 | // 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. | |
| 19 | declare module VSCodeDebugAdapter { | |
| 20 | class DebugSession { | |
| 21 | public static run: Function; | |
5547a16fJimmy Thomson10 years ago | 22 | public sendEvent(event: VSCodeDebugAdapter.InitializedEvent): void; |
| 23 | public start(input: any, output: any): void; | |
| 24 | public launchRequest(response: any, args: any): void; | |
c2bf3c4fdigeff10 years ago | 25 | public disconnectRequest(response: any, args: any): void; |
65bb0c85Jimmy Thomson10 years ago | 26 | } |
| 27 | class InitializedEvent { | |
5547a16fJimmy Thomson10 years ago | 28 | constructor(); |
| 29 | } | |
| 30 | class OutputEvent { | |
| 31 | constructor(message: string, destination?: string); | |
| 32 | } | |
| 33 | class TerminatedEvent { | |
| 34 | constructor(); | |
65bb0c85Jimmy Thomson10 years ago | 35 | } |
| 36 | } | |
| 37 | | |
| 38 | declare class SourceMaps { | |
| 39 | public _sourceToGeneratedMaps: {}; | |
| 40 | public _generatedToSourceMaps: {}; | |
| 41 | public _allSourceMaps: {}; | |
| 42 | } | |
| 43 | | |
5547a16fJimmy Thomson10 years ago | 44 | declare class NodeDebugSession extends VSCodeDebugAdapter.DebugSession { |
65bb0c85Jimmy Thomson10 years ago | 45 | public _sourceMaps: SourceMaps; |
| 46 | } | |
| 47 | | |
5e54f6f2Jimmy Thomson10 years ago | 48 | interface ILaunchArgs { |
| 49 | platform: string; | |
| 50 | target?: string; | |
| 51 | internalDebuggerPort?: any; | |
| 52 | args: string[]; | |
710f8655digeff10 years ago | 53 | logCatArguments: any; |
4f4e082ddigeff10 years ago | 54 | program: string; |
5e54f6f2Jimmy Thomson10 years ago | 55 | } |
| 56 | | |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 57 | let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version; |
5547a16fJimmy Thomson10 years ago | 58 | |
642490c1Jimmy Thomson10 years ago | 59 | function bailOut(reason: string): void { |
| 60 | // Things have gone wrong in initialization: Report the error to telemetry and exit | |
| 61 | TelemetryHelper.sendSimpleEvent(reason); | |
6e4d7a62Joshua Skelton10 years ago | 62 | process.exit(1); |
642490c1Jimmy Thomson10 years ago | 63 | } |
| 64 | | |
c31e59fbdigeff10 years ago | 65 | function parseLogCatArguments(userProvidedLogCatArguments: any) { |
| 66 | return 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 | | |
| 71 | function isNullOrUndefined(value: any): boolean { | |
| 72 | return typeof value === "undefined" || value === null; | |
| 73 | } | |
| 74 | | |
4f4e082ddigeff10 years ago | 75 | let projectRootPath: string = null; |
| 76 | | |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 77 | // Enable telemetry |
18d8ad2adigeff10 years ago | 78 | const telemetryReporter = new ReassignableTelemetryReporter(new NullTelemetryReporter()); |
| 79 | const appName = "react-native-debug-adapter"; | |
| 80 | new EntryPointHandler(ProcessType.Debugger).runApp(appName, () => version, | |
| 81 | ErrorHelper.getInternalError(InternalErrorCode.DebuggingFailed), telemetryReporter, () => { | |
| 82 | let nodeDebugFolder: string; | |
| 83 | let 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 | |
| 87 | try { | |
| 88 | /* tslint:disable:no-var-requires */ | |
| 89 | nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath; | |
| 90 | vscodeDebugAdapterPackage = 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 | |
| 94 | bailOut("cannotFindDebugAdapter"); | |
| 95 | } | |
4881129dMeena Kunnathur Balakrishnan10 years ago | 96 | |
18d8ad2adigeff10 years ago | 97 | // Temporarily dummy out the DebugSession.run function so we do not start the debug adapter until we are ready |
| 98 | const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run; | |
| 99 | vscodeDebugAdapterPackage.DebugSession.run = function() { }; | |
4881129dMeena Kunnathur Balakrishnan10 years ago | 100 | |
18d8ad2adigeff10 years ago | 101 | let nodeDebug: { NodeDebugSession: typeof NodeDebugSession }; |
dd442738Jimmy Thomson10 years ago | 102 | |
18d8ad2adigeff10 years ago | 103 | try { |
| 104 | /* tslint:disable:no-var-requires */ | |
| 105 | nodeDebug = 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 | |
| 109 | const 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. | |
| 112 | debugSession.start(process.stdin, process.stdout); | |
| 113 | debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr")); | |
| 114 | debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent()); | |
| 115 | | |
| 116 | bailOut("cannotFindNodeDebugAdapter"); | |
| 117 | } | |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 118 | |
18d8ad2adigeff10 years ago | 119 | vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun; |
| 120 | | |
| 121 | // Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments | |
| 122 | const originalNodeDebugSessionLaunchRequest = nodeDebug.NodeDebugSession.prototype.launchRequest; | |
| 123 | nodeDebug.NodeDebugSession.prototype.launchRequest = function(request: any, args: ILaunchArgs) { | |
| 124 | projectRootPath = path.resolve(args.program, "../.."); | |
| 125 | telemetryReporter.reassignTo(new ExtensionTelemetryReporter( // We start to send telemetry | |
| 126 | appName, version, Telemetry.APPINSIGHTS_INSTRUMENTATIONKEY, projectRootPath)); | |
| 127 | | |
| 128 | // Create a server waiting for messages to re-initialize the debug session; | |
| 129 | const reinitializeServer = http.createServer((req, res) => { | |
| 130 | res.statusCode = 404; | |
| 131 | if (req.url === "/refreshBreakpoints") { | |
| 132 | res.statusCode = 200; | |
| 133 | if (this) { | |
| 134 | const sourceMaps = this._sourceMaps; | |
| 135 | if (sourceMaps) { | |
| 136 | // Flush any cached source maps | |
| 137 | sourceMaps._allSourceMaps = {}; | |
| 138 | sourceMaps._generatedToSourceMaps = {}; | |
| 139 | sourceMaps._sourceToGeneratedMaps = {}; | |
| 140 | } | |
| 141 | // Send an "initialized" event to trigger breakpoints to be re-sent | |
| 142 | this.sendEvent(new vscodeDebugAdapterPackage.InitializedEvent()); | |
48efc4d6Meena Kunnathur Balakrishnan10 years ago | 143 | } |
4881129dMeena Kunnathur Balakrishnan10 years ago | 144 | } |
18d8ad2adigeff10 years ago | 145 | res.end(); |
| 146 | }); | |
| 147 | const debugServerListeningPort = parseInt(args.internalDebuggerPort, 10) || 9090; | |
| 148 | | |
| 149 | | |
| 150 | reinitializeServer.listen(debugServerListeningPort); | |
| 151 | reinitializeServer.on("error", (err: Error) => { | |
| 152 | TelemetryHelper.sendSimpleEvent("reinitializeServerError"); | |
| 153 | this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Error in debug adapter server: " + err.toString(), "stderr")); | |
| 154 | this.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 | |
| 158 | args.args = [ | |
| 159 | args.platform, | |
| 160 | debugServerListeningPort.toString(), | |
| 161 | args.target || "simulator", | |
| 162 | ]; | |
| 163 | | |
| 164 | if (!isNullOrUndefined(args.logCatArguments)) { // We add the parameter if it's defined (adapter crashes otherwise) | |
| 165 | args.args = args.args.concat([parseLogCatArguments(args.logCatArguments)]); | |
4881129dMeena Kunnathur Balakrishnan10 years ago | 166 | } |
710f8655digeff10 years ago | 167 | |
18d8ad2adigeff10 years ago | 168 | originalNodeDebugSessionLaunchRequest.call(this, request, args); |
| 169 | }; | |
| 170 | | |
| 171 | // Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments | |
| 172 | const originalNodeDebugSessionDisconnectRequest = nodeDebug.NodeDebugSession.prototype.disconnectRequest; | |
| 173 | function customDisconnectRequest(response: any, args: any): void { | |
| 174 | try { | |
| 175 | // First we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session | |
c7f1165cdigeff10 years ago | 176 | const remoteExtension = RemoteExtension.atProjectRootPath(projectRootPath); |
0766856fdigeff10 years ago | 177 | remoteExtension.stopMonitoringLogcat() |
18d8ad2adigeff10 years ago | 178 | .finally(() => originalNodeDebugSessionDisconnectRequest.call(this, response, args)) |
| 179 | .done(() => { }, reason => // We just print a warning if something fails | |
| 180 | process.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 | |
| 184 | process.stderr.write(`WARNING: Couldn't stop monitoring logcat. Sync exception: ${exception.message || exception}\n`); | |
| 185 | originalNodeDebugSessionDisconnectRequest.call(this, response, args); | |
4881129dMeena Kunnathur Balakrishnan10 years ago | 186 | } |
c2bf3c4fdigeff10 years ago | 187 | } |
18d8ad2adigeff10 years ago | 188 | nodeDebug.NodeDebugSession.prototype.disconnectRequest = customDisconnectRequest; |
c2bf3c4fdigeff10 years ago | 189 | |
18d8ad2adigeff10 years ago | 190 | vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession); |
41c61f9aJoshua Skelton10 years ago | 191 | }); |