-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathios-debug-service.ts
266 lines (224 loc) · 11.6 KB
/
ios-debug-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import * as iOSDevice from "../common/mobile/ios/device/ios-device";
import * as net from "net";
import * as path from "path";
import * as log4js from "log4js";
import { ChildProcess } from "child_process";
import { DebugServiceBase } from "./debug-service-base";
import { IOS_LOG_PREDICATE } from "../common/constants";
import { CONNECTION_ERROR_EVENT_NAME, AWAIT_NOTIFICATION_TIMEOUT_SECONDS } from "../constants";
import { getPidFromiOSSimulatorLogs } from "../common/helpers";
const inspectorAppName = "NativeScript Inspector.app";
const inspectorNpmPackageName = "tns-ios-inspector";
const inspectorUiDir = "WebInspectorUI/";
export class IOSDebugService extends DebugServiceBase implements IPlatformDebugService {
private _lldbProcess: ChildProcess;
private _sockets: net.Socket[] = [];
private _socketProxy: any;
constructor(protected device: Mobile.IiOSDevice,
protected $devicesService: Mobile.IDevicesService,
private $platformService: IPlatformService,
private $iOSEmulatorServices: Mobile.IiOSSimulatorService,
private $childProcess: IChildProcess,
private $hostInfo: IHostInfo,
private $logger: ILogger,
private $errors: IErrors,
private $npmInstallationManager: INpmInstallationManager,
private $iOSDebuggerPortService: IIOSDebuggerPortService,
private $iOSNotification: IiOSNotification,
private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
private $processService: IProcessService,
private $socketProxyFactory: ISocketProxyFactory,
private $projectDataService: IProjectDataService,
private $deviceLogProvider: Mobile.IDeviceLogProvider) {
super(device, $devicesService);
this.$processService.attachToProcessExitSignals(this, this.debugStop);
this.$socketProxyFactory.on(CONNECTION_ERROR_EVENT_NAME, (e: Error) => this.emit(CONNECTION_ERROR_EVENT_NAME, e));
}
public get platform(): string {
return "ios";
}
public async debug(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
if (debugOptions.debugBrk && debugOptions.start) {
this.$errors.failWithoutHelp("Expected exactly one of the --debug-brk or --start options.");
}
if (this.$devicesService.isOnlyiOSSimultorRunning() || this.$devicesService.deviceCount === 0) {
debugOptions.emulator = true;
}
await this.startDeviceLogProcess(debugData, debugOptions);
await this.$iOSDebuggerPortService.attachToDebuggerPortFoundEvent(this.device, debugData, debugOptions);
if (debugOptions.emulator) {
if (debugOptions.start) {
return this.emulatorStart(debugData, debugOptions);
} else {
return this.emulatorDebugBrk(debugData, debugOptions);
}
} else {
if (debugOptions.start) {
return this.deviceStart(debugData, debugOptions);
} else {
return this.deviceDebugBrk(debugData, debugOptions);
}
}
}
public async debugStart(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {
await this.$devicesService.initialize({ platform: this.platform, deviceId: debugData.deviceIdentifier });
const action = async (device: Mobile.IiOSDevice) => device.isEmulator ? await this.emulatorDebugBrk(debugData, debugOptions) : await this.debugBrkCore(device, debugData, debugOptions);
await this.$devicesService.execute(action, this.getCanExecuteAction(debugData.deviceIdentifier));
}
public async debugStop(): Promise<void> {
if (this._socketProxy) {
this._socketProxy.close();
this._socketProxy = null;
}
_.forEach(this._sockets, socket => socket.destroy());
this._sockets = [];
if (this._lldbProcess) {
this._lldbProcess.stdin.write("process detach\n");
await this.killProcess(this._lldbProcess);
this._lldbProcess = undefined;
}
}
protected getChromeDebugUrl(debugOptions: IDebugOptions, port: number): string {
const debugOpts = _.cloneDeep(debugOptions);
debugOpts.useBundledDevTools = debugOpts.useBundledDevTools === undefined ? false : debugOpts.useBundledDevTools;
const chromeDebugUrl = super.getChromeDebugUrl(debugOpts, port);
return chromeDebugUrl;
}
private async startDeviceLogProcess(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {
if (debugOptions.justlaunch) {
// No logs should be printed on console when `--justlaunch` option is passed.
// On the other side we need to start log process in order to get debugger port from logs.
this.$deviceLogProvider.muteLogsForDevice(debugData.deviceIdentifier);
}
let projectName = debugData.projectName;
if (!projectName && debugData.projectDir) {
const projectData = this.$projectDataService.getProjectData(debugData.projectDir);
projectName = projectData.projectName;
}
if (projectName) {
this.$deviceLogProvider.setProjectNameForDevice(debugData.deviceIdentifier, projectName);
}
await this.device.openDeviceLogStream({ predicate: IOS_LOG_PREDICATE });
}
private async killProcess(childProcess: ChildProcess): Promise<void> {
if (childProcess) {
return new Promise<void>((resolve, reject) => {
childProcess.on("close", resolve);
childProcess.kill();
});
}
}
private async emulatorDebugBrk(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
const args = debugOptions.debugBrk ? "--nativescript-debug-brk" : "--nativescript-debug-start";
const launchResult = await this.$iOSEmulatorServices.runApplicationOnEmulator(debugData.pathToAppPackage, {
waitForDebugger: true,
captureStdin: true,
args: args,
appId: debugData.applicationIdentifier,
skipInstall: true,
device: debugData.deviceIdentifier,
justlaunch: debugOptions.justlaunch,
timeout: debugOptions.timeout,
sdk: debugOptions.sdk
});
const pid = getPidFromiOSSimulatorLogs(debugData.applicationIdentifier, launchResult);
this._lldbProcess = this.$childProcess.spawn("lldb", ["-p", pid]);
if (log4js.levels.TRACE.isGreaterThanOrEqualTo(this.$logger.getLevel())) {
this._lldbProcess.stdout.pipe(process.stdout);
}
this._lldbProcess.stderr.pipe(process.stderr);
this._lldbProcess.stdin.write("process continue\n");
return this.wireDebuggerClient(debugData, debugOptions);
}
private async emulatorStart(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
const result = await this.wireDebuggerClient(debugData, debugOptions);
const attachRequestMessage = this.$iOSNotification.getAttachRequest(debugData.applicationIdentifier, debugData.deviceIdentifier);
const iOSEmulatorService = <Mobile.IiOSSimulatorService>this.$iOSEmulatorServices;
await iOSEmulatorService.postDarwinNotification(attachRequestMessage, debugData.deviceIdentifier);
return result;
}
private async deviceDebugBrk(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
await this.$devicesService.initialize({ platform: this.platform, deviceId: debugData.deviceIdentifier });
const projectData = this.$projectDataService.getProjectData(debugData.projectDir);
const action = async (device: iOSDevice.IOSDevice): Promise<string> => {
if (device.isEmulator) {
return await this.emulatorDebugBrk(debugData, debugOptions);
}
const runOptions: IRunPlatformOptions = {
device: debugData.deviceIdentifier,
emulator: debugOptions.emulator,
justlaunch: debugOptions.justlaunch
};
const promisesResults = await Promise.all<any>([
this.$platformService.startApplication(this.platform, runOptions, { appId: debugData.applicationIdentifier, projectName: projectData.projectName }),
this.debugBrkCore(device, debugData, debugOptions)
]);
return _.last(promisesResults);
};
const deviceActionResult = await this.$devicesService.execute(action, this.getCanExecuteAction(debugData.deviceIdentifier));
return deviceActionResult[0].result;
}
private async debugBrkCore(device: Mobile.IiOSDevice, debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
await this.$iOSSocketRequestExecutor.executeLaunchRequest(device.deviceInfo.identifier, AWAIT_NOTIFICATION_TIMEOUT_SECONDS, AWAIT_NOTIFICATION_TIMEOUT_SECONDS, debugData.applicationIdentifier, debugOptions);
return this.wireDebuggerClient(debugData, debugOptions, device);
}
private async deviceStart(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
await this.$devicesService.initialize({ platform: this.platform, deviceId: debugData.deviceIdentifier });
const action = async (device: Mobile.IiOSDevice) => device.isEmulator ? await this.emulatorStart(debugData, debugOptions) : await this.deviceStartCore(device, debugData, debugOptions);
const deviceActionResult = await this.$devicesService.execute(action, this.getCanExecuteAction(debugData.deviceIdentifier));
return deviceActionResult[0].result;
}
private async deviceStartCore(device: Mobile.IiOSDevice, debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
await this.$iOSSocketRequestExecutor.executeAttachRequest(device, AWAIT_NOTIFICATION_TIMEOUT_SECONDS, debugData.applicationIdentifier);
return this.wireDebuggerClient(debugData, debugOptions, device);
}
private async wireDebuggerClient(debugData: IDebugData, debugOptions: IDebugOptions, device?: Mobile.IiOSDevice): Promise<string> {
// the VSCode Ext starts `tns debug ios --no-client` to start/attach to debug sessions
// check if --no-client is passed - default to opening a tcp socket (versus Chrome DevTools (websocket))
if ((debugOptions.inspector || !debugOptions.client) && this.$hostInfo.isDarwin) {
this._socketProxy = await this.$socketProxyFactory.createTCPSocketProxy(this.getSocketFactory(device, debugData, debugOptions));
await this.openAppInspector(this._socketProxy.address(), debugData, debugOptions);
return null;
} else {
if (debugOptions.chrome) {
this.$logger.info("'--chrome' is the default behavior. Use --inspector to debug iOS applications using the Safari Web Inspector.");
}
const deviceIdentifier = device ? device.deviceInfo.identifier : debugData.deviceIdentifier;
this._socketProxy = await this.$socketProxyFactory.createWebSocketProxy(this.getSocketFactory(device, debugData, debugOptions), deviceIdentifier);
return this.getChromeDebugUrl(debugOptions, this._socketProxy.options.port);
}
}
private async openAppInspector(fileDescriptor: string, debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {
if (debugOptions.client) {
const inspectorPath = await this.$npmInstallationManager.getInspectorFromCache(inspectorNpmPackageName, debugData.projectDir);
const inspectorSourceLocation = path.join(inspectorPath, inspectorUiDir, "Main.html");
const inspectorApplicationPath = path.join(inspectorPath, inspectorAppName);
const cmd = `open -a '${inspectorApplicationPath}' --args '${inspectorSourceLocation}' '${debugData.projectName}' '${fileDescriptor}'`;
await this.$childProcess.exec(cmd);
} else {
this.$logger.info("Suppressing debugging client.");
}
}
private getSocketFactory(device: Mobile.IiOSDevice, debugData: IDebugData, debugOptions: IDebugOptions): () => Promise<net.Socket> {
let pendingExecution: Promise<net.Socket> = null;
const factory = async () => {
if (!pendingExecution) {
const func = async () => {
const port = await this.$iOSDebuggerPortService.getPort({ projectDir: debugData.projectDir, deviceId: debugData.deviceIdentifier, appId: debugData.applicationIdentifier }, debugOptions);
if (!port) {
this.$errors.fail("NativeScript debugger was not able to get inspector socket port.");
}
const socket = device ? await device.connectToPort(port) : net.connect(port);
this._sockets.push(socket);
pendingExecution = null;
return socket;
};
pendingExecution = func();
}
return pendingExecution;
};
factory.bind(this);
return factory;
}
}
$injector.register("iOSDebugService", IOSDebugService, false);