-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathios-debug-service.ts
472 lines (392 loc) · 18.8 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
///<reference path="../.d.ts"/>
"use strict";
import * as iOSProxyServices from "../common/mobile/ios/ios-proxy-services";
import * as iOSDevice from "../common/mobile/ios/ios-device";
import * as net from "net";
import ws = require("ws");
import * as stream from "stream";
import * as path from "path";
import Future = require("fibers/future");
import semver = require("semver");
import temp = require("temp");
import byline = require("byline");
module notification {
function formatNotification(bundleId: string, notification: string) {
return `${bundleId}:NativeScript.Debug.${notification}`;
}
export function waitForDebug(bundleId: string): string {
return formatNotification(bundleId, "WaitForDebugger");
}
export function attachRequest(bundleId: string): string {
return formatNotification(bundleId, "AttachRequest");
}
export function appLaunching(bundleId: string): string {
return formatNotification(bundleId, "AppLaunching");
}
export function readyForAttach(bundleId: string): string {
return formatNotification(bundleId, "ReadyForAttach");
}
export function attachAvailabilityQuery(bundleId: string) {
return formatNotification(bundleId, "AttachAvailabilityQuery");
}
export function alreadyConnected(bundleId: string) {
return formatNotification(bundleId, "AlreadyConnected");
}
export function attachAvailable(bundleId: string) {
return formatNotification(bundleId, "AttachAvailable");
}
}
let InspectorBackendPort = 18181;
function connectEventually(factory: () => net.Socket, handler: (_socket: net.Socket) => void) {
function tryConnect() {
let tryConnectAfterTimeout = setTimeout.bind(undefined, tryConnect, 1000);
let socket = factory();
socket.on("connect", () => {
socket.removeListener("error", tryConnectAfterTimeout);
handler(socket);
});
socket.on("error", tryConnectAfterTimeout);
}
tryConnect();
}
class IOSDebugService implements IDebugService {
private static TIMEOUT_SECONDS = 90;
constructor(
private $platformService: IPlatformService,
private $iOSEmulatorServices: Mobile.IEmulatorPlatformServices,
private $devicesServices: Mobile.IDevicesServices,
private $platformsData: IPlatformsData,
private $projectData: IProjectData,
private $childProcess: IChildProcess,
private $logger: ILogger,
private $fs: IFileSystem,
private $errors: IErrors,
private $injector: IInjector,
private $npmInstallationManager: INpmInstallationManager,
private $options: IOptions,
private $projectDataService: IProjectDataService,
private $utils: IUtils) { }
get platform(): string {
return "ios";
}
public debug(): IFuture<void> {
if (this.$options.debugBrk && this.$options.start) {
this.$errors.failWithoutHelp("Expected exactly one of the --debug-brk or --start options.");
}
if(!this.$options.debugBrk && !this.$options.start) {
this.$logger.warn("Neither --debug-brk nor --start option was specified. Defaulting to --debug-brk.");
this.$options.debugBrk = true;
}
if (this.$options.emulator) {
if (this.$options.debugBrk) {
return this.emulatorDebugBrk();
} else if (this.$options.start) {
return this.emulatorStart();
}
} else {
if (this.$options.debugBrk) {
return this.deviceDebugBrk();
} else if (this.$options.start) {
return this.deviceStart();
}
}
this.$errors.failWithoutHelp("Failed to select device or emulator to debug on.");
}
private emulatorDebugBrk(): IFuture<void> {
return (() => {
let platformData = this.$platformsData.getPlatformData(this.platform);
this.$platformService.buildPlatform(this.platform).wait();
let emulatorPackage = this.$platformService.getLatestApplicationPackageForEmulator(platformData).wait();
let child_process = this.$iOSEmulatorServices.startEmulator(emulatorPackage.packageName, { waitForDebugger: true, captureStdin: true, args: "--nativescript-debug-brk" }).wait();
let lineStream = byline(child_process.stdout);
lineStream.on('data', (line: NodeBuffer) => {
let lineText = line.toString();
if(lineText && _.startsWith(lineText, emulatorPackage.packageName)) {
let pid = _.trimLeft(lineText, emulatorPackage.packageName + ": ");
this.$childProcess.exec(`lldb -p ${pid} -o "process continue"`);
} else {
process.stdout.write(line + "\n");
}
});
this.wireDebuggerClient(() => net.connect(InspectorBackendPort)).wait();
}).future<void>()();
}
private emulatorStart(): IFuture<void> {
return (() => {
this.wireDebuggerClient(() => net.connect(InspectorBackendPort)).wait();
let projectId = this.$projectData.projectId;
let attachRequestMessage = notification.attachRequest(projectId);
let iOSEmulator = <Mobile.IiOSSimulatorService>this.$iOSEmulatorServices;
iOSEmulator.postDarwinNotification(attachRequestMessage).wait();
}).future<void>()();
}
private deviceDebugBrk(): IFuture<void> {
return (() => {
this.$devicesServices.initialize({ platform: this.platform, deviceId: this.$options.device }).wait();
this.$devicesServices.execute(device => (() => {
// we intentionally do not wait on this here, because if we did, we'd miss the AppLaunching notification
let deploy = this.$platformService.deployOnDevice(this.platform);
let iosDevice = <iOSDevice.IOSDevice>device;
let projectId = this.$projectData.projectId;
let npc = new iOSProxyServices.NotificationProxyClient(iosDevice, this.$injector);
try {
let timeout = this.$utils.getMilliSecondsTimeout(IOSDebugService.TIMEOUT_SECONDS);
awaitNotification(npc, notification.appLaunching(projectId), timeout).wait();
process.nextTick(() => {
npc.postNotificationAndAttachForData(notification.waitForDebug(projectId));
npc.postNotificationAndAttachForData(notification.attachRequest(projectId));
});
awaitNotification(npc, notification.readyForAttach(projectId), this.getReadyForAttachTimeout(timeout)).wait();
} catch(e) {
this.$logger.trace(`Timeout error: ${e}`);
this.$errors.failWithoutHelp("Timeout waiting for NativeScript debugger.");
}
this.wireDebuggerClient(() => iosDevice.connectToPort(InspectorBackendPort)).wait();
deploy.wait();
}).future<void>()()).wait();
}).future<void>()();
}
private deviceStart(): IFuture<void> {
return (() => {
this.$devicesServices.initialize({ platform: this.platform, deviceId: this.$options.device }).wait();
this.$devicesServices.execute(device => (() => {
let iosDevice = <iOSDevice.IOSDevice>device;
let projectId = this.$projectData.projectId;
let npc = new iOSProxyServices.NotificationProxyClient(iosDevice, this.$injector);
let timeout = this.getReadyForAttachTimeout();
let [alreadyConnected, readyForAttach, attachAvailable] = [
notification.alreadyConnected(projectId),
notification.readyForAttach(projectId),
notification.attachAvailable(projectId)
].map((notification) => awaitNotification(npc, notification, timeout));
npc.postNotificationAndAttachForData(notification.attachAvailabilityQuery(projectId));
let receivedNotification: IFuture<string>;
try {
receivedNotification = whenAny(alreadyConnected, readyForAttach, attachAvailable).wait();
} catch (e) {
this.$errors.failWithoutHelp(`The application ${projectId} does not appear to be running on ${device.deviceInfo.displayName} or is not built with debugging enabled.`);
}
switch (receivedNotification) {
case alreadyConnected:
this.$errors.failWithoutHelp("A debugger is already connected.");
break;
case attachAvailable:
process.nextTick(() => npc.postNotificationAndAttachForData(notification.attachRequest(projectId)));
try {
awaitNotification(npc, notification.readyForAttach(projectId), timeout).wait();
} catch (e) {
this.$errors.failWithoutHelp(`The application ${projectId} timed out when performing the NativeScript debugger handshake.`);
}
this.wireDebuggerClient(() => iosDevice.connectToPort(InspectorBackendPort)).wait();
break;
case readyForAttach:
this.wireDebuggerClient(() => iosDevice.connectToPort(InspectorBackendPort)).wait();
break;
}
}).future<void>()()).wait();
}).future<void>()();
}
private wireDebuggerClient(factory: () => net.Socket): IFuture<void> {
return (() => {
let frameworkVersion = this.getProjectFrameworkVersion().wait();
let socketFileLocation = "";
if (semver.gte(frameworkVersion, "1.4.0")) {
socketFileLocation = createTcpSocketProxy(this.$logger, (callback) => connectEventually(factory, callback));
} else {
createWebSocketProxy(this.$logger, (callback) => connectEventually(factory, callback));
}
this.executeOpenDebuggerClient(socketFileLocation).wait();
}).future<void>()();
}
public executeOpenDebuggerClient(fileDescriptor: string): IFuture<void> {
if (this.$options.client) {
return this.openDebuggingClient(fileDescriptor);
} else {
return (() => {
this.$logger.info("Supressing debugging client.");
}).future<void>()();
}
}
private openDebuggingClient(fileDescriptor: string): IFuture<void> {
return (() => {
let frameworkVersion = this.getProjectFrameworkVersion().wait();
let inspectorPath = this.getInspectorPath(frameworkVersion).wait();
let inspectorSourceLocation = path.join(inspectorPath, "Safari/Main.html");
let cmd: string = null;
if(semver.lt(frameworkVersion, "1.2.0")) {
cmd = `open -a Safari "${inspectorSourceLocation}"`;
} else {
let inspectorApplicationPath = path.join(inspectorPath, "NativeScript Inspector.app");
if(!this.$fs.exists(inspectorApplicationPath).wait()) {
this.$fs.unzip(path.join(inspectorPath, "NativeScript Inspector.zip"), inspectorPath).wait();
}
cmd = `open -a '${inspectorApplicationPath}' --args '${inspectorSourceLocation}' '${this.$projectData.projectName}' '${fileDescriptor}'`;
}
this.$childProcess.exec(cmd).wait();
}).future<void>()();
}
private getProjectFrameworkVersion(): IFuture<string> {
return (() => {
this.$projectDataService.initialize(this.$projectData.projectDir);
let platformData = this.$platformsData.getPlatformData(this.platform);
return this.$projectDataService.getValue(platformData.frameworkPackageName).wait().version;
}).future<string>()();
}
private getInspectorPath(frameworkVersion: string): IFuture<string> {
return (() => {
let tnsIosPackage = "";
if (this.$options.frameworkPath) {
if (this.$fs.getFsStats(this.$options.frameworkPath).wait().isFile()) {
this.$errors.failWithoutHelp("frameworkPath option must be path to directory which contains tns-ios framework");
}
tnsIosPackage = path.resolve(this.$options.frameworkPath);
} else {
let platformData = this.$platformsData.getPlatformData(this.platform);
tnsIosPackage = this.$npmInstallationManager.install(platformData.frameworkPackageName, { version: frameworkVersion }).wait();
}
let inspectorPath = path.join(tnsIosPackage, "WebInspectorUI/");
return inspectorPath;
}).future<string>()();
}
private getReadyForAttachTimeout(timeoutInMilliseconds?: number): number {
let timeout = timeoutInMilliseconds || this.$utils.getMilliSecondsTimeout(IOSDebugService.TIMEOUT_SECONDS);
let readyForAttachTimeout = timeout / 10 ;
let defaultReadyForAttachTimeout = 5000;
return readyForAttachTimeout > defaultReadyForAttachTimeout ? readyForAttachTimeout : defaultReadyForAttachTimeout;
}
}
$injector.register("iOSDebugService", IOSDebugService);
function createTcpSocketProxy($logger: ILogger, socketFactory: (handler: (socket: net.Socket) => void) => void): string {
$logger.info("\nSetting up debugger proxy...\nPress Ctrl + C to terminate, or disconnect.\n");
let server = net.createServer({
allowHalfOpen: true
});
server.on("connection", (frontendSocket: net.Socket) => {
$logger.info("Frontend client connected.");
frontendSocket.on("end", function() {
$logger.info('Frontend socket closed!');
process.exit(0);
});
socketFactory((backendSocket) => {
$logger.info("Backend socket created.");
backendSocket.on("end", () => {
$logger.info("Backend socket closed!");
process.exit(0);
});
backendSocket.pipe(frontendSocket);
frontendSocket.pipe(backendSocket);
frontendSocket.resume();
});
});
let socketFileLocation = temp.path({ suffix: ".sock" });
server.listen(socketFileLocation);
return socketFileLocation;
}
function createWebSocketProxy($logger: ILogger, socketFactory: (handler: (socket: net.Socket) => void) => void): ws.Server {
// NOTE: We will try to provide command line options to select ports, at least on the localhost.
let localPort = 8080;
$logger.info("\nSetting up debugger proxy...\nPress Ctrl + C to terminate, or disconnect.\n");
// NB: When the inspector frontend connects we might not have connected to the inspector backend yet.
// That's why we use the verifyClient callback of the websocket server to stall the upgrade request until we connect.
// We store the socket that connects us to the device in the upgrade request object itself and later on retrieve it
// in the connection callback.
let server = ws.createServer(<any>{
port: localPort,
verifyClient: (info: any, callback: any) => {
$logger.info("Frontend client connected.");
socketFactory((_socket: any) => {
$logger.info("Backend socket created.");
info.req["__deviceSocket"] = _socket;
callback(true);
});
}
});
server.on("connection", (webSocket) => {
let deviceSocket: net.Socket = (<any>webSocket.upgradeReq)["__deviceSocket"];
let packets = new PacketStream();
deviceSocket.pipe(packets);
packets.on("data", (buffer: Buffer) => {
webSocket.send(buffer.toString("utf16le"));
});
webSocket.on("message", (message, flags) => {
let length = Buffer.byteLength(message, "utf16le");
let payload = new Buffer(length + 4);
payload.writeInt32BE(length, 0);
payload.write(message, 4, length, "utf16le");
deviceSocket.write(payload);
});
deviceSocket.on("end", () => {
$logger.info("Backend socket closed!");
process.exit(0);
});
webSocket.on("close", () => {
$logger.info('Frontend socket closed!');
process.exit(0);
});
});
$logger.info("Opened localhost " + localPort);
return server;
}
function awaitNotification(npc: iOSProxyServices.NotificationProxyClient, notification: string, timeout: number): IFuture<string> {
let future = new Future<string>();
let timeoutObject = setTimeout(() => {
detachObserver();
future.throw(new Error(`Timeout receiving ${notification} notification.`));
}, timeout);
function notificationObserver(_notification: string) {
clearTimeout(timeoutObject);
detachObserver();
future.return(_notification);
}
function detachObserver() {
process.nextTick(() => npc.removeObserver(notification, notificationObserver));
}
npc.addObserver(notification, notificationObserver);
return future;
}
function whenAny<T>(...futures: IFuture<T>[]): IFuture<IFuture<T>> {
let resultFuture = new Future<IFuture<T>>();
let futuresLeft = futures.length;
let futureLocal: IFuture<T>;
for (let future of futures) {
futureLocal = future;
future.resolve((error, result?) => {
futuresLeft--;
if (!resultFuture.isResolved()) {
if (typeof error === "undefined") {
resultFuture.return(futureLocal);
} else if (futuresLeft === 0) {
resultFuture.throw(new Error("None of the futures succeeded."));
}
}
});
}
return resultFuture;
}
class PacketStream extends stream.Transform {
private buffer: Buffer;
private offset: number;
constructor(opts?: stream.TransformOptions) {
super(opts);
}
public _transform(packet: any, encoding: string, done: Function): void {
while (packet.length > 0) {
if (!this.buffer) {
// read length
let length = packet.readInt32BE(0);
this.buffer = new Buffer(length);
this.offset = 0;
packet = packet.slice(4);
}
packet.copy(this.buffer, this.offset);
let copied = Math.min(this.buffer.length - this.offset, packet.length);
this.offset += copied;
packet = packet.slice(copied);
if (this.offset === this.buffer.length) {
this.push(this.buffer);
this.buffer = undefined;
}
}
done();
}
}