Skip to content

Commit d3e898f

Browse files
authored
handle errors during debugserver startup and add helper function to start session (#459)
1 parent 72cbd8d commit d3e898f

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

src/ios-deploy/ios-deploy.m

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -989,11 +989,42 @@ void fdvendor_callback(CFSocketRef s, CFSocketCallBackType callbackType, CFDataR
989989
CFRelease(s);
990990
}
991991

992-
void start_remote_debug_server(AMDeviceRef device) {
992+
void connect_and_start_session(AMDeviceRef device) {
993+
AMDeviceConnect(device);
994+
assert(AMDeviceIsPaired(device));
995+
check_error(AMDeviceValidatePairing(device));
996+
check_error(AMDeviceStartSession(device));
997+
}
993998

994-
ServiceConnRef con;
999+
void start_remote_debug_server(AMDeviceRef device) {
9951000

996-
check_error(AMDeviceSecureStartService(device, CFSTR("com.apple.debugserver"), NULL, &con));
1001+
ServiceConnRef con = NULL;
1002+
int start_err = AMDeviceSecureStartService(device, CFSTR("com.apple.debugserver"), NULL, &con);
1003+
if (start_err != 0)
1004+
{
1005+
// After we mount the image, iOS needs to scan the image to register new services.
1006+
// If we ask to start the service before it is found by ios, we will get 0xe8000022.
1007+
// In other cases, it's been observed, that device may loose connection here (0xe800002d).
1008+
// Luckly, we can just restart the connection and continue.
1009+
// In other cases we just error out.
1010+
NSLogOut(@"Failed to start debugserver: %x %s", start_err, get_error_message(start_err));
1011+
switch(start_err)
1012+
{
1013+
case 0xe8000022:
1014+
NSLogOut(@"Waiting for the device to scan mounted image");
1015+
sleep(1);
1016+
break;
1017+
case 0x800002d:
1018+
NSLogOut(@"Reconnecting to device");
1019+
// We dont call AMDeviceStopSession as we cannot send any messages anymore
1020+
check_error(AMDeviceDisconnect(device));
1021+
connect_and_start_session(device);
1022+
break;
1023+
default:
1024+
check_error(start_err);
1025+
}
1026+
check_error(AMDeviceSecureStartService(device, CFSTR("com.apple.debugserver"), NULL, &con));
1027+
}
9971028
assert(con != NULL);
9981029
gdbfd = AMDServiceConnectionGetSocket(con);
9991030
/*
@@ -1112,10 +1143,7 @@ void setup_lldb(AMDeviceRef device, CFURLRef url) {
11121143
CFStringRef device_full_name = get_device_full_name(device),
11131144
device_interface_name = get_device_interface_name(device);
11141145

1115-
AMDeviceConnect(device);
1116-
assert(AMDeviceIsPaired(device));
1117-
check_error(AMDeviceValidatePairing(device));
1118-
check_error(AMDeviceStartSession(device));
1146+
connect_and_start_session(device);
11191147

11201148
NSLogOut(@"------ Debug phase ------");
11211149

@@ -1414,10 +1442,7 @@ AFCConnectionRef start_afc_service(AMDeviceRef device) {
14141442

14151443
// Used to send files to app-specific sandbox (Documents dir)
14161444
AFCConnectionRef start_house_arrest_service(AMDeviceRef device) {
1417-
AMDeviceConnect(device);
1418-
assert(AMDeviceIsPaired(device));
1419-
check_error(AMDeviceValidatePairing(device));
1420-
check_error(AMDeviceStartSession(device));
1445+
connect_and_start_session(device);
14211446

14221447
AFCConnectionRef conn = NULL;
14231448

@@ -1575,10 +1600,7 @@ void get_battery_level(AMDeviceRef device)
15751600

15761601
void list_bundle_id(AMDeviceRef device)
15771602
{
1578-
AMDeviceConnect(device);
1579-
assert(AMDeviceIsPaired(device));
1580-
check_error(AMDeviceValidatePairing(device));
1581-
check_error(AMDeviceStartSession(device));
1603+
connect_and_start_session(device);
15821604

15831605
NSArray *a = [NSArray arrayWithObjects:
15841606
@"CFBundleIdentifier",
@@ -1841,10 +1863,7 @@ void uninstall_app(AMDeviceRef device) {
18411863
if (cf_uninstall_bundle_id == NULL) {
18421864
on_error(@"Error: Unable to get bundle id from user command or package %@.\nUninstall failed.", [NSString stringWithUTF8String:app_path]);
18431865
} else {
1844-
AMDeviceConnect(device);
1845-
assert(AMDeviceIsPaired(device));
1846-
check_error(AMDeviceValidatePairing(device));
1847-
check_error(AMDeviceStartSession(device));
1866+
connect_and_start_session(device);
18481867

18491868
int code = AMDeviceSecureUninstallApplication(0, device, cf_uninstall_bundle_id, 0, NULL, 0);
18501869
if (code == 0) {
@@ -1941,10 +1960,7 @@ void handle_device(AMDeviceRef device) {
19411960
if (cf_uninstall_bundle_id == NULL) {
19421961
on_error(@"Error: Unable to get bundle id from user command or package %@.\nUninstall failed.", [NSString stringWithUTF8String:app_path]);
19431962
} else {
1944-
AMDeviceConnect(device);
1945-
assert(AMDeviceIsPaired(device));
1946-
check_error(AMDeviceValidatePairing(device));
1947-
check_error(AMDeviceStartSession(device));
1963+
connect_and_start_session(device);
19481964

19491965
int code = AMDeviceSecureUninstallApplication(0, device, cf_uninstall_bundle_id, 0, NULL, 0);
19501966
if (code == 0) {
@@ -1962,10 +1978,7 @@ void handle_device(AMDeviceRef device) {
19621978
NSLogOut(@"------ Install phase ------");
19631979
NSLogOut(@"[ 0%%] Found %@ connected through %@, beginning install", device_full_name, device_interface_name);
19641980

1965-
AMDeviceConnect(device);
1966-
assert(AMDeviceIsPaired(device));
1967-
check_error(AMDeviceValidatePairing(device));
1968-
check_error(AMDeviceStartSession(device));
1981+
connect_and_start_session(device);
19691982

19701983
CFDictionaryRef options;
19711984
if (app_deltas == NULL) { // standard install
@@ -1981,10 +1994,7 @@ void handle_device(AMDeviceRef device) {
19811994
check_error(AMDeviceSecureTransferPath(0, device, url, options, transfer_callback, 0));
19821995
close(*afcFd);
19831996

1984-
AMDeviceConnect(device);
1985-
assert(AMDeviceIsPaired(device));
1986-
check_error(AMDeviceValidatePairing(device));
1987-
check_error(AMDeviceStartSession(device));
1997+
connect_and_start_session(device);
19881998
check_error(AMDeviceSecureInstallApplication(0, device, url, options, install_callback, 0));
19891999
} else { // incremental install
19902000
check_error(AMDeviceStopSession(device));
@@ -2032,10 +2042,7 @@ void handle_device(AMDeviceRef device) {
20322042
CFIndex size = sizeof(keys)/sizeof(CFStringRef);
20332043
options = CFDictionaryCreate(NULL, (const void **)&keys, (const void **)&values, size, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
20342044

2035-
AMDeviceConnect(device);
2036-
assert(AMDeviceIsPaired(device));
2037-
check_error(AMDeviceValidatePairing(device));
2038-
check_error(AMDeviceStartSession(device));
2045+
connect_and_start_session(device);
20392046
check_error(AMDeviceSecureInstallApplicationBundle(device, url, options, incremental_install_callback, 0));
20402047
CFRelease(extracted_bundle_id);
20412048
CFRelease(deltas_path);

0 commit comments

Comments
 (0)