microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/direct/directDebugAdapter.ts
230lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as path from "path"; |
| 5 | import { ProjectVersionHelper } from "../../common/projectVersionHelper"; |
| 6 | import { ReactNativeProjectHelper } from "../../common/reactNativeProjectHelper"; |
| 7 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 8 | import { getExtensionVersion } from "../../common/extensionHelper"; |
| 9 | import { ILaunchArgs } from "../../extension/launchArgs"; |
| 10 | import { getProjectRoot } from "../nodeDebugWrapper"; |
| 11 | import { Telemetry } from "../../common/telemetry"; |
| 12 | import { OutputEvent, Logger } from "vscode-debugadapter"; |
| 13 | import { TelemetryHelper } from "../../common/telemetryHelper"; |
| 14 | import { RemoteTelemetryReporter } from "../../common/telemetryReporters"; |
| 15 | import { ChromeDebugAdapter, ChromeDebugSession, IChromeDebugSessionOpts, IAttachRequestArgs, logger, IOnPausedResult, Crdp } from "vscode-chrome-debug-core"; |
| 16 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
| 17 | import { RemoteExtension } from "../../common/remoteExtension"; |
| 18 | import { DebugProtocol } from "vscode-debugprotocol"; |
| 19 | import { getLoggingDirectory } from "../../extension/log/LogHelper"; |
| 20 | import * as nls from "vscode-nls"; |
| 21 | import * as Q from "q"; |
| 22 | const localize = nls.loadMessageBundle(); |
| 23 | |
| 24 | export interface IDirectAttachRequestArgs extends IAttachRequestArgs, ILaunchArgs { |
| 25 | cwd: string; /* Automatically set by VS Code to the currently opened folder */ |
| 26 | } |
| 27 | |
| 28 | export interface IDirectLaunchRequestArgs extends DebugProtocol.LaunchRequestArguments, IDirectAttachRequestArgs { } |
| 29 | |
| 30 | export class DirectDebugAdapter extends ChromeDebugAdapter { |
| 31 | |
| 32 | /** |
| 33 | * @description The Hermes native functions calls mark in call stack |
| 34 | * @type {string} |
| 35 | */ |
| 36 | private static HERMES_NATIVE_FUNCTION_NAME: string = "(native)"; |
| 37 | |
| 38 | /** |
| 39 | * @description Equals to 0xfffffff - the scriptId returned by Hermes debugger, that means "invalid script ID" |
| 40 | * @type {string} |
| 41 | */ |
| 42 | private static HERMES_NATIVE_FUNCTION_SCRIPT_ID: string = "4294967295"; |
| 43 | |
| 44 | private outputLogger: (message: string, error?: boolean | string) => void; |
| 45 | private projectRootPath: string; |
| 46 | private remoteExtension: RemoteExtension; |
| 47 | private isSettingsInitialized: boolean; // used to prevent parameters reinitialization when attach is called from launch function |
| 48 | private previousAttachArgs: IDirectAttachRequestArgs; |
| 49 | |
| 50 | public constructor(opts: IChromeDebugSessionOpts, debugSession: ChromeDebugSession) { |
| 51 | super(opts, debugSession); |
| 52 | this.outputLogger = (message: string, error?: boolean | string) => { |
| 53 | let category = "console"; |
| 54 | if (error === true) { |
| 55 | category = "stderr"; |
| 56 | } |
| 57 | if (typeof error === "string") { |
| 58 | category = error; |
| 59 | } |
| 60 | |
| 61 | let newLine = "\n"; |
| 62 | if (category === "stdout" || category === "stderr") { |
| 63 | newLine = ""; |
| 64 | } |
| 65 | debugSession.sendEvent(new OutputEvent(message + newLine, category)); |
| 66 | }; |
| 67 | |
| 68 | this.isSettingsInitialized = false; |
| 69 | } |
| 70 | |
| 71 | public launch(launchArgs: IDirectLaunchRequestArgs): Promise<void> { |
| 72 | let extProps = { |
| 73 | platform: { |
| 74 | value: launchArgs.platform, |
| 75 | isPii: false, |
| 76 | }, |
| 77 | isDirect: { |
| 78 | value: true, |
| 79 | isPii: false, |
| 80 | }, |
| 81 | }; |
| 82 | |
| 83 | return new Promise<void>((resolve, reject) => this.initializeSettings(launchArgs) |
| 84 | .then(() => { |
| 85 | this.outputLogger("Launching the application"); |
| 86 | logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null , 2)}`); |
| 87 | return ProjectVersionHelper.getReactNativeVersions(launchArgs.cwd, launchArgs.platform === "windows") |
| 88 | .then(versions => { |
| 89 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps); |
| 90 | if (launchArgs.platform === "windows") { |
| 91 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps); |
| 92 | } |
| 93 | return TelemetryHelper.generate("launch", extProps, (generator) => { |
| 94 | return this.remoteExtension.launch({ "arguments": launchArgs }) |
| 95 | .then(() => { |
| 96 | return this.remoteExtension.getPackagerPort(launchArgs.cwd); |
| 97 | }) |
| 98 | .then((packagerPort: number) => { |
| 99 | launchArgs.port = launchArgs.port || packagerPort; |
| 100 | this.attach(launchArgs).then(() => { |
| 101 | resolve(); |
| 102 | }).catch((e) => reject(e)); |
| 103 | }).catch((e) => reject(e)); |
| 104 | }) |
| 105 | .catch((err) => { |
| 106 | this.outputLogger("An error occurred while launching the application. " + err.message || err, true); |
| 107 | this.cleanUp(); |
| 108 | reject(err); |
| 109 | }); |
| 110 | }); |
| 111 | })); |
| 112 | } |
| 113 | |
| 114 | public attach(attachArgs: IDirectAttachRequestArgs): Promise<void> { |
| 115 | let extProps = { |
| 116 | platform: { |
| 117 | value: attachArgs.platform, |
| 118 | isPii: false, |
| 119 | }, |
| 120 | isDirect: { |
| 121 | value: true, |
| 122 | isPii: false, |
| 123 | }, |
| 124 | }; |
| 125 | |
| 126 | this.previousAttachArgs = attachArgs; |
| 127 | |
| 128 | return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs) |
| 129 | .then(() => { |
| 130 | this.outputLogger("Attaching to the application"); |
| 131 | logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null , 2)}`); |
| 132 | return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, true) |
| 133 | .then(versions => { |
| 134 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps); |
| 135 | if (!ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) { |
| 136 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps); |
| 137 | } |
| 138 | return TelemetryHelper.generate("attach", extProps, (generator) => { |
| 139 | return this.remoteExtension.getPackagerPort(attachArgs.cwd) |
| 140 | .then((packagerPort: number) => { |
| 141 | attachArgs.port = attachArgs.port || packagerPort; |
| 142 | this.outputLogger(`Connecting to ${attachArgs.port} port`); |
| 143 | const attachArguments = Object.assign({}, attachArgs, { |
| 144 | address: "localhost", |
| 145 | port: attachArgs.port, |
| 146 | restart: true, |
| 147 | request: "attach", |
| 148 | remoteRoot: undefined, |
| 149 | localRoot: undefined, |
| 150 | }); |
| 151 | super.attach(attachArguments).then(() => { |
| 152 | this.outputLogger("The debugger attached successfully"); |
| 153 | resolve(); |
| 154 | }).catch((e) => reject(e)); |
| 155 | }).catch((e) => reject(e)); |
| 156 | }) |
| 157 | .catch((err) => { |
| 158 | this.outputLogger("An error occurred while attaching to the debugger. " + err.message || err, true); |
| 159 | this.cleanUp(); |
| 160 | reject(err); |
| 161 | }); |
| 162 | }); |
| 163 | })); |
| 164 | } |
| 165 | |
| 166 | public disconnect(args: DebugProtocol.DisconnectArguments): void { |
| 167 | this.cleanUp(); |
| 168 | super.disconnect(args); |
| 169 | } |
| 170 | |
| 171 | protected async onPaused(notification: Crdp.Debugger.PausedEvent, expectingStopReason = this._expectingStopReason): Promise<IOnPausedResult> { |
| 172 | // Excluding Hermes native function calls from call stack, since VS Code can't process them properly |
| 173 | // More info: https://github.com/facebook/hermes/issues/168 |
| 174 | notification.callFrames = notification.callFrames.filter(callFrame => |
| 175 | callFrame.functionName !== DirectDebugAdapter.HERMES_NATIVE_FUNCTION_NAME && |
| 176 | callFrame.location.scriptId !== DirectDebugAdapter.HERMES_NATIVE_FUNCTION_SCRIPT_ID |
| 177 | ); |
| 178 | return super.onPaused(notification, expectingStopReason); |
| 179 | } |
| 180 | |
| 181 | private initializeSettings(args: any): Q.Promise<any> { |
| 182 | if (!this.isSettingsInitialized) { |
| 183 | let chromeDebugCoreLogs = getLoggingDirectory(); |
| 184 | if (chromeDebugCoreLogs) { |
| 185 | chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "ChromeDebugCoreLogs.txt"); |
| 186 | } |
| 187 | let logLevel: string = args.trace; |
| 188 | if (logLevel) { |
| 189 | logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase()); |
| 190 | logger.setup(Logger.LogLevel[logLevel], chromeDebugCoreLogs || false); |
| 191 | } else { |
| 192 | logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false); |
| 193 | } |
| 194 | |
| 195 | if (!args.sourceMaps) { |
| 196 | args.sourceMaps = true; |
| 197 | } |
| 198 | |
| 199 | const projectRootPath = getProjectRoot(args); |
| 200 | return ReactNativeProjectHelper.isReactNativeProject(projectRootPath) |
| 201 | .then((result) => { |
| 202 | if (!result) { |
| 203 | throw ErrorHelper.getInternalError(InternalErrorCode.NotInReactNativeFolderError); |
| 204 | } |
| 205 | this.projectRootPath = projectRootPath; |
| 206 | this.remoteExtension = RemoteExtension.atProjectRootPath(this.projectRootPath); |
| 207 | const version = getExtensionVersion(); |
| 208 | |
| 209 | // Start to send telemetry |
| 210 | (this._session as any).getTelemetryReporter().reassignTo(new RemoteTelemetryReporter( |
| 211 | "react-native-tools", version, Telemetry.APPINSIGHTS_INSTRUMENTATIONKEY, this.projectRootPath)); |
| 212 | |
| 213 | this.isSettingsInitialized = true; |
| 214 | |
| 215 | return void 0; |
| 216 | }); |
| 217 | } else { |
| 218 | return Q.resolve<void>(void 0); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | private cleanUp() { |
| 223 | if (this.previousAttachArgs.platform === "android") { |
| 224 | this.remoteExtension.stopMonitoringLogcat() |
| 225 | .catch(reason => logger.warn(localize("CouldNotStopMonitoringLogcat", "Couldn't stop monitoring logcat: {0}", reason.message || reason))) |
| 226 | .finally(() => super.disconnect({terminateDebuggee: true})); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | } |
| 231 | |