microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/nodeDebugWrapper.ts
178lines · 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"; |
c2bf3c4fdigeff10 years ago | 10 | import {ExtensionMessageSender, ExtensionMessage} from "../common/extensionMessaging"; |
d976d077Meena Kunnathur Balakrishnan10 years ago | 11 | |
65bb0c85Jimmy Thomson10 years ago | 12 | // 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. | |
| 15 | declare module VSCodeDebugAdapter { | |
| 16 | class DebugSession { | |
| 17 | public static run: Function; | |
5547a16fJimmy Thomson10 years ago | 18 | public sendEvent(event: VSCodeDebugAdapter.InitializedEvent): void; |
| 19 | public start(input: any, output: any): void; | |
| 20 | public launchRequest(response: any, args: any): void; | |
c2bf3c4fdigeff10 years ago | 21 | public disconnectRequest(response: any, args: any): void; |
65bb0c85Jimmy Thomson10 years ago | 22 | } |
| 23 | class InitializedEvent { | |
5547a16fJimmy Thomson10 years ago | 24 | constructor(); |
| 25 | } | |
| 26 | class OutputEvent { | |
| 27 | constructor(message: string, destination?: string); | |
| 28 | } | |
| 29 | class TerminatedEvent { | |
| 30 | constructor(); | |
65bb0c85Jimmy Thomson10 years ago | 31 | } |
| 32 | } | |
| 33 | | |
| 34 | declare class SourceMaps { | |
| 35 | public _sourceToGeneratedMaps: {}; | |
| 36 | public _generatedToSourceMaps: {}; | |
| 37 | public _allSourceMaps: {}; | |
| 38 | } | |
| 39 | | |
5547a16fJimmy Thomson10 years ago | 40 | declare class NodeDebugSession extends VSCodeDebugAdapter.DebugSession { |
65bb0c85Jimmy Thomson10 years ago | 41 | public _sourceMaps: SourceMaps; |
| 42 | } | |
| 43 | | |
5e54f6f2Jimmy Thomson10 years ago | 44 | interface ILaunchArgs { |
| 45 | platform: string; | |
| 46 | target?: string; | |
| 47 | internalDebuggerPort?: any; | |
| 48 | args: string[]; | |
710f8655digeff10 years ago | 49 | logCatArguments: any; |
5e54f6f2Jimmy Thomson10 years ago | 50 | } |
| 51 | | |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 52 | let version = JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf-8")).version; |
5547a16fJimmy Thomson10 years ago | 53 | |
642490c1Jimmy Thomson10 years ago | 54 | function bailOut(reason: string): void { |
| 55 | // Things have gone wrong in initialization: Report the error to telemetry and exit | |
| 56 | TelemetryHelper.sendSimpleEvent(reason); | |
| 57 | Telemetry.sendPendingData().finally(() => { | |
| 58 | process.exit(1); | |
| 59 | }); | |
| 60 | } | |
| 61 | | |
c31e59fbdigeff10 years ago | 62 | function parseLogCatArguments(userProvidedLogCatArguments: any) { |
| 63 | return Array.isArray(userProvidedLogCatArguments) | |
| 64 | ? userProvidedLogCatArguments.join(" ") // If it's an array, we join the arguments | |
| 65 | : userProvidedLogCatArguments; // If not, we leave it as-is | |
| 66 | } | |
| 67 | | |
| 68 | function isNullOrUndefined(value: any): boolean { | |
| 69 | return typeof value === "undefined" || value === null; | |
| 70 | } | |
| 71 | | |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 72 | // Enable telemetry |
48efc4d6Meena Kunnathur Balakrishnan10 years ago | 73 | Telemetry.init("react-native-debug-adapter", version, true).then(() => { |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 74 | let nodeDebugFolder: string; |
| 75 | let vscodeDebugAdapterPackage: typeof VSCodeDebugAdapter; | |
b8ef4af9Jimmy Thomson10 years ago | 76 | |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 77 | /* tslint:disable:no-var-requires */ |
65bb0c85Jimmy Thomson10 years ago | 78 | |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 79 | // nodeDebugLocation.json is dynamically generated on extension activation. |
| 80 | // If it fails, we must not have been in a react native project | |
4881129dMeena Kunnathur Balakrishnan10 years ago | 81 | try { |
| 82 | nodeDebugFolder = require("./nodeDebugLocation.json").nodeDebugPath; | |
| 83 | vscodeDebugAdapterPackage = require(path.join(nodeDebugFolder, "node_modules", "vscode-debugadapter")); | |
| 84 | } catch (e) { | |
642490c1Jimmy Thomson10 years ago | 85 | // Nothing we can do here: can't even communicate back because we don't know how to speak debug adapter |
| 86 | return bailOut("cannotFindDebugAdapter"); | |
4881129dMeena Kunnathur Balakrishnan10 years ago | 87 | } |
65bb0c85Jimmy Thomson10 years ago | 88 | |
4881129dMeena Kunnathur Balakrishnan10 years ago | 89 | // Temporarily dummy out the DebugSession.run function so we do not start the debug adapter until we are ready |
| 90 | const originalDebugSessionRun = vscodeDebugAdapterPackage.DebugSession.run; | |
| 91 | vscodeDebugAdapterPackage.DebugSession.run = function() { }; | |
| 92 | | |
| 93 | let nodeDebug: { NodeDebugSession: typeof NodeDebugSession }; | |
| 94 | | |
| 95 | try { | |
| 96 | nodeDebug = require(path.join(nodeDebugFolder, "out", "node", "nodeDebug")); | |
| 97 | } catch (e) { | |
| 98 | // Unable to find nodeDebug, but we can make our own communication channel now | |
| 99 | const 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. | |
| 102 | debugSession.start(process.stdin, process.stdout); | |
| 103 | debugSession.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Unable to start debug adapter: " + e.toString(), "stderr")); | |
| 104 | debugSession.sendEvent(new vscodeDebugAdapterPackage.TerminatedEvent()); | |
dd442738Jimmy Thomson10 years ago | 105 | |
642490c1Jimmy Thomson10 years ago | 106 | return bailOut("cannotFindNodeDebugAdapter"); |
4881129dMeena Kunnathur Balakrishnan10 years ago | 107 | } |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 108 | |
4881129dMeena Kunnathur Balakrishnan10 years ago | 109 | /* tslint:enable:no-var-requires */ |
| 110 | | |
| 111 | vscodeDebugAdapterPackage.DebugSession.run = originalDebugSessionRun; | |
| 112 | | |
| 113 | // Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments | |
| 114 | const originalNodeDebugSessionLaunchRequest = nodeDebug.NodeDebugSession.prototype.launchRequest; | |
| 115 | nodeDebug.NodeDebugSession.prototype.launchRequest = function(request: any, args: ILaunchArgs) { | |
| 116 | // Create a server waiting for messages to re-initialize the debug session; | |
| 117 | const reinitializeServer = http.createServer((req, res) => { | |
| 118 | res.statusCode = 404; | |
| 119 | if (req.url === "/refreshBreakpoints") { | |
| 120 | res.statusCode = 200; | |
| 121 | if (this) { | |
| 122 | const sourceMaps = this._sourceMaps; | |
| 123 | if (sourceMaps) { | |
| 124 | // Flush any cached source maps | |
| 125 | sourceMaps._allSourceMaps = {}; | |
| 126 | sourceMaps._generatedToSourceMaps = {}; | |
| 127 | sourceMaps._sourceToGeneratedMaps = {}; | |
48efc4d6Meena Kunnathur Balakrishnan10 years ago | 128 | } |
4881129dMeena Kunnathur Balakrishnan10 years ago | 129 | // Send an "initialized" event to trigger breakpoints to be re-sent |
| 130 | this.sendEvent(new vscodeDebugAdapterPackage.InitializedEvent()); | |
| 131 | } | |
| 132 | } | |
| 133 | res.end(); | |
2d3a052eMeena Kunnathur Balakrishnan10 years ago | 134 | }); |
4881129dMeena Kunnathur Balakrishnan10 years ago | 135 | const debugServerListeningPort = parseInt(args.internalDebuggerPort, 10) || 9090; |
| 136 | | |
| 137 | reinitializeServer.listen(debugServerListeningPort); | |
| 138 | reinitializeServer.on("error", (err: Error) => { | |
642490c1Jimmy Thomson10 years ago | 139 | TelemetryHelper.sendSimpleEvent("reinitializeServerError"); |
4881129dMeena Kunnathur Balakrishnan10 years ago | 140 | this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Error in debug adapter server: " + err.toString(), "stderr")); |
| 141 | this.sendEvent(new vscodeDebugAdapterPackage.OutputEvent("Breakpoints may not update. Consider restarting and specifying a different 'internalDebuggerPort' in launch.json")); | |
| 142 | }); | |
| 143 | | |
| 144 | // We do not permit arbitrary args to be passed to our process | |
| 145 | args.args = [ | |
| 146 | args.platform, | |
| 147 | debugServerListeningPort.toString(), | |
| 148 | args.target || "simulator" | |
| 149 | ]; | |
| 150 | | |
710f8655digeff10 years ago | 151 | if (!isNullOrUndefined(args.logCatArguments)) { // We add the parameter if it's defined (adapter crashes otherwise) |
| 152 | args.args = args.args.concat([parseLogCatArguments(args.logCatArguments)]); | |
| 153 | } | |
| 154 | | |
4881129dMeena Kunnathur Balakrishnan10 years ago | 155 | originalNodeDebugSessionLaunchRequest.call(this, request, args); |
| 156 | }; | |
| 157 | | |
c2bf3c4fdigeff10 years ago | 158 | // Intecept the "launchRequest" instance method of NodeDebugSession to interpret arguments |
| 159 | const originalNodeDebugSessionDisconnectRequest = nodeDebug.NodeDebugSession.prototype.disconnectRequest; | |
| 160 | function customDisconnectRequest(response: any, args: any): void { | |
| 161 | try { | |
| 162 | // First we tell the extension to stop monitoring the logcat, and then we disconnect the debugging session | |
| 163 | const extensionMessageSender = new ExtensionMessageSender(); | |
| 164 | extensionMessageSender.sendMessage(ExtensionMessage.STOP_MONITORING_LOGCAT) | |
| 165 | .finally(() => originalNodeDebugSessionDisconnectRequest.call(this, response, args)) | |
| 166 | .done(() => {}, reason => // We just print a warning if something fails | |
| 167 | process.stderr.write(`WARNING: Couldn't stop monitoring logcat: ${reason.message || reason}\n`)); | |
| 168 | } catch (exception) { | |
| 169 | // This is a "nice to have" feature, so we just fire the message and forget. We don't event handle | |
| 170 | // errors in the response promise | |
| 171 | process.stderr.write(`WARNING: Couldn't stop monitoring logcat. Sync exception: ${exception.message || exception}\n`); | |
| 172 | originalNodeDebugSessionDisconnectRequest.call(this, response, args); | |
| 173 | } | |
| 174 | } | |
| 175 | nodeDebug.NodeDebugSession.prototype.disconnectRequest = customDisconnectRequest; | |
| 176 | | |
4881129dMeena Kunnathur Balakrishnan10 years ago | 177 | vscodeDebugAdapterPackage.DebugSession.run(nodeDebug.NodeDebugSession); |
710f8655digeff10 years ago | 178 | }); |