Skip to content

Commit

Permalink
fix(iOS): implement iOS 14 compatibility (#157)
Browse files Browse the repository at this point in the history
fixes #60
  • Loading branch information
ikeith committed Sep 28, 2020
1 parent d56415d commit 6f242fd
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
19 changes: 14 additions & 5 deletions src/ios/lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,20 @@ export class ClientManager {
}

async getDebugserverClient() {
return this.getServiceClient(
'com.apple.debugserver',
DebugserverClient,
true,
);
try {
// iOS 14 added support for a secure debug service so try to connect to that first
return await this.getServiceClient(
'com.apple.debugserver.DVTSecureSocketProxy',
DebugserverClient,
);
} catch {
// otherwise, fall back to the previous implementation
return this.getServiceClient(
'com.apple.debugserver',
DebugserverClient,
true,
);
}
}

private async getServiceClient<T extends ServiceClient<any>>(
Expand Down
35 changes: 35 additions & 0 deletions src/ios/lib/protocol/gdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,41 @@ export class GDBProtocolReader extends ProtocolReader {
super(1 /* "Header" is '+' or '-' */, callback);
}

onData(data?: Buffer) {
// the GDB protocol does not support body length in its header so we cannot rely on
// the parent implementation to determine when a payload is complete
try {
// if there's data, add it to the existing buffer
this.buffer = data ? Buffer.concat([this.buffer, data]) : this.buffer;

// do we have enough bytes to proceed
if (this.buffer.length < this.headerSize) {
return; // incomplete header, wait for more
}

// first, check the header
if (this.parseHeader(this.buffer) === -1) {
// we have a valid header so check the body. GDB packets will always be a leading '$', data bytes,
// a trailing '#', and a two digit checksum. minimum valid body is the empty response '$#00'
// https://developer.apple.com/library/archive/documentation/DeveloperTools/gdb/gdb/gdb_33.html
const packetData = this.buffer.toString().match('\\$.*#[0-9a-f]{2}');
if (packetData == null) {
return; // incomplete body, wait for more
}
// extract the body and update the buffer
const body = Buffer.from(packetData[0]);
this.buffer = this.buffer.slice(this.headerSize + body.length);
// parse the payload and recurse if there is more data to process
this.callback(this.parseBody(body));
if (this.buffer.length) {
this.onData();
}
}
} catch (err) {
this.callback(null, err);
}
}

parseHeader(data: Buffer) {
if (data[0] !== ACK_SUCCESS) {
throw new Error('Unsuccessful debugserver response');
Expand Down
8 changes: 4 additions & 4 deletions src/ios/lib/protocol/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export class ProtocolReaderFactory<T> {
}

export abstract class ProtocolReader {
private body!: Buffer; // TODO: ! -> ?
private bodyLength!: number; // TODO: ! -> ?
private buffer = Buffer.alloc(0);
protected body!: Buffer; // TODO: ! -> ?
protected bodyLength!: number; // TODO: ! -> ?
protected buffer = Buffer.alloc(0);
constructor(
private headerSize: number,
protected headerSize: number,
protected callback: ProtocolReaderCallback,
) {
this.onData = this.onData.bind(this);
Expand Down

0 comments on commit 6f242fd

Please sign in to comment.