microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
gh-pages

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/direct/directDebugAdapter.ts

230lines · modeblame

549baae2RedMickey6 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
4import * as path from "path";
e3706a1cRedMickey6 years ago5import { ProjectVersionHelper } from "../../common/projectVersionHelper";
549baae2RedMickey6 years ago6import { ReactNativeProjectHelper } from "../../common/reactNativeProjectHelper";
7import { ErrorHelper } from "../../common/error/errorHelper";
8import { getExtensionVersion } from "../../common/extensionHelper";
9import { ILaunchArgs } from "../../extension/launchArgs";
10import { getProjectRoot } from "../nodeDebugWrapper";
11import { Telemetry } from "../../common/telemetry";
12import { OutputEvent, Logger } from "vscode-debugadapter";
13import { TelemetryHelper } from "../../common/telemetryHelper";
14import { RemoteTelemetryReporter } from "../../common/telemetryReporters";
6b9f7c4cRedMickey6 years ago15import { ChromeDebugAdapter, ChromeDebugSession, IChromeDebugSessionOpts, IAttachRequestArgs, logger, IOnPausedResult, Crdp } from "vscode-chrome-debug-core";
549baae2RedMickey6 years ago16import { InternalErrorCode } from "../../common/error/internalErrorCode";
17import { RemoteExtension } from "../../common/remoteExtension";
18import { DebugProtocol } from "vscode-debugprotocol";
19import { getLoggingDirectory } from "../../extension/log/LogHelper";
20import * as nls from "vscode-nls";
21import * as Q from "q";
22const localize = nls.loadMessageBundle();
23
24export interface IDirectAttachRequestArgs extends IAttachRequestArgs, ILaunchArgs {
25cwd: string; /* Automatically set by VS Code to the currently opened folder */
26}
27
28export interface IDirectLaunchRequestArgs extends DebugProtocol.LaunchRequestArguments, IDirectAttachRequestArgs { }
29
30export class DirectDebugAdapter extends ChromeDebugAdapter {
31
6b9f7c4cRedMickey6 years ago32/**
33* @description The Hermes native functions calls mark in call stack
34* @type {string}
35*/
36private 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*/
42private static HERMES_NATIVE_FUNCTION_SCRIPT_ID: string = "4294967295";
43
549baae2RedMickey6 years ago44private outputLogger: (message: string, error?: boolean | string) => void;
45private projectRootPath: string;
46private remoteExtension: RemoteExtension;
47private isSettingsInitialized: boolean; // used to prevent parameters reinitialization when attach is called from launch function
48private previousAttachArgs: IDirectAttachRequestArgs;
49
50public constructor(opts: IChromeDebugSessionOpts, debugSession: ChromeDebugSession) {
51super(opts, debugSession);
52this.outputLogger = (message: string, error?: boolean | string) => {
53let category = "console";
54if (error === true) {
55category = "stderr";
56}
57if (typeof error === "string") {
58category = error;
59}
60
61let newLine = "\n";
62if (category === "stdout" || category === "stderr") {
63newLine = "";
64}
65debugSession.sendEvent(new OutputEvent(message + newLine, category));
66};
67
68this.isSettingsInitialized = false;
69}
70
71public launch(launchArgs: IDirectLaunchRequestArgs): Promise<void> {
ba953e9fRedMickey6 years ago72let extProps = {
549baae2RedMickey6 years ago73platform: {
74value: launchArgs.platform,
75isPii: false,
76},
77isDirect: {
78value: true,
79isPii: false,
80},
81};
82
83return new Promise<void>((resolve, reject) => this.initializeSettings(launchArgs)
84.then(() => {
ba953e9fRedMickey6 years ago85this.outputLogger("Launching the application");
86logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null , 2)}`);
e3706a1cRedMickey6 years ago87return ProjectVersionHelper.getReactNativeVersions(launchArgs.cwd, launchArgs.platform === "windows")
7fa90b3bRedMickey6 years ago88.then(versions => {
89extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps);
90if (launchArgs.platform === "windows") {
91extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps);
92}
ba953e9fRedMickey6 years ago93return TelemetryHelper.generate("launch", extProps, (generator) => {
94return this.remoteExtension.launch({ "arguments": launchArgs })
95.then(() => {
96return this.remoteExtension.getPackagerPort(launchArgs.cwd);
97})
98.then((packagerPort: number) => {
99launchArgs.port = launchArgs.port || packagerPort;
100this.attach(launchArgs).then(() => {
101resolve();
102}).catch((e) => reject(e));
103}).catch((e) => reject(e));
549baae2RedMickey6 years ago104})
ba953e9fRedMickey6 years ago105.catch((err) => {
106this.outputLogger("An error occurred while launching the application. " + err.message || err, true);
107this.cleanUp();
108reject(err);
109});
110});
549baae2RedMickey6 years ago111}));
112}
113
114public attach(attachArgs: IDirectAttachRequestArgs): Promise<void> {
ba953e9fRedMickey6 years ago115let extProps = {
549baae2RedMickey6 years ago116platform: {
117value: attachArgs.platform,
118isPii: false,
119},
120isDirect: {
121value: true,
122isPii: false,
123},
124};
125
126this.previousAttachArgs = attachArgs;
127
128return new Promise<void>((resolve, reject) => this.initializeSettings(attachArgs)
129.then(() => {
ba953e9fRedMickey6 years ago130this.outputLogger("Attaching to the application");
131logger.verbose(`Attaching to the application: ${JSON.stringify(attachArgs, null , 2)}`);
e3706a1cRedMickey6 years ago132return ProjectVersionHelper.getReactNativeVersions(attachArgs.cwd, true)
7fa90b3bRedMickey6 years ago133.then(versions => {
134extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeVersion, "reactNativeVersion", extProps);
e3706a1cRedMickey6 years ago135if (!ProjectVersionHelper.isVersionError(versions.reactNativeWindowsVersion)) {
7fa90b3bRedMickey6 years ago136extProps = TelemetryHelper.addPropertyToTelemetryProperties(versions.reactNativeWindowsVersion, "reactNativeWindowsVersion", extProps);
137}
ba953e9fRedMickey6 years ago138return TelemetryHelper.generate("attach", extProps, (generator) => {
139return this.remoteExtension.getPackagerPort(attachArgs.cwd)
140.then((packagerPort: number) => {
141attachArgs.port = attachArgs.port || packagerPort;
142this.outputLogger(`Connecting to ${attachArgs.port} port`);
143const attachArguments = Object.assign({}, attachArgs, {
144address: "localhost",
145port: attachArgs.port,
146restart: true,
147request: "attach",
148remoteRoot: undefined,
149localRoot: undefined,
150});
151super.attach(attachArguments).then(() => {
152this.outputLogger("The debugger attached successfully");
153resolve();
154}).catch((e) => reject(e));
155}).catch((e) => reject(e));
156})
157.catch((err) => {
158this.outputLogger("An error occurred while attaching to the debugger. " + err.message || err, true);
159this.cleanUp();
160reject(err);
161});
162});
549baae2RedMickey6 years ago163}));
164}
165
166public disconnect(args: DebugProtocol.DisconnectArguments): void {
167this.cleanUp();
168super.disconnect(args);
169}
170
6b9f7c4cRedMickey6 years ago171protected 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
174notification.callFrames = notification.callFrames.filter(callFrame =>
175callFrame.functionName !== DirectDebugAdapter.HERMES_NATIVE_FUNCTION_NAME &&
176callFrame.location.scriptId !== DirectDebugAdapter.HERMES_NATIVE_FUNCTION_SCRIPT_ID
177);
178return super.onPaused(notification, expectingStopReason);
179}
180
549baae2RedMickey6 years ago181private initializeSettings(args: any): Q.Promise<any> {
182if (!this.isSettingsInitialized) {
183let chromeDebugCoreLogs = getLoggingDirectory();
184if (chromeDebugCoreLogs) {
185chromeDebugCoreLogs = path.join(chromeDebugCoreLogs, "ChromeDebugCoreLogs.txt");
186}
187let logLevel: string = args.trace;
188if (logLevel) {
189logLevel = logLevel.replace(logLevel[0], logLevel[0].toUpperCase());
190logger.setup(Logger.LogLevel[logLevel], chromeDebugCoreLogs || false);
191} else {
192logger.setup(Logger.LogLevel.Log, chromeDebugCoreLogs || false);
193}
194
195if (!args.sourceMaps) {
196args.sourceMaps = true;
197}
198
199const projectRootPath = getProjectRoot(args);
200return ReactNativeProjectHelper.isReactNativeProject(projectRootPath)
201.then((result) => {
202if (!result) {
203throw ErrorHelper.getInternalError(InternalErrorCode.NotInReactNativeFolderError);
204}
205this.projectRootPath = projectRootPath;
206this.remoteExtension = RemoteExtension.atProjectRootPath(this.projectRootPath);
207const 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
213this.isSettingsInitialized = true;
214
215return void 0;
216});
217} else {
218return Q.resolve<void>(void 0);
219}
220}
221
222private cleanUp() {
223if (this.previousAttachArgs.platform === "android") {
224this.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}