@@ -989,11 +989,42 @@ void fdvendor_callback(CFSocketRef s, CFSocketCallBackType callbackType, CFDataR
989
989
CFRelease (s);
990
990
}
991
991
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
+ }
993
998
994
- ServiceConnRef con;
999
+ void start_remote_debug_server (AMDeviceRef device) {
995
1000
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
+ }
997
1028
assert (con != NULL );
998
1029
gdbfd = AMDServiceConnectionGetSocket (con);
999
1030
/*
@@ -1112,10 +1143,7 @@ void setup_lldb(AMDeviceRef device, CFURLRef url) {
1112
1143
CFStringRef device_full_name = get_device_full_name (device),
1113
1144
device_interface_name = get_device_interface_name (device);
1114
1145
1115
- AMDeviceConnect (device);
1116
- assert (AMDeviceIsPaired (device));
1117
- check_error (AMDeviceValidatePairing (device));
1118
- check_error (AMDeviceStartSession (device));
1146
+ connect_and_start_session (device);
1119
1147
1120
1148
NSLogOut (@" ------ Debug phase ------" );
1121
1149
@@ -1414,10 +1442,7 @@ AFCConnectionRef start_afc_service(AMDeviceRef device) {
1414
1442
1415
1443
// Used to send files to app-specific sandbox (Documents dir)
1416
1444
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);
1421
1446
1422
1447
AFCConnectionRef conn = NULL ;
1423
1448
@@ -1575,10 +1600,7 @@ void get_battery_level(AMDeviceRef device)
1575
1600
1576
1601
void list_bundle_id (AMDeviceRef device)
1577
1602
{
1578
- AMDeviceConnect (device);
1579
- assert (AMDeviceIsPaired (device));
1580
- check_error (AMDeviceValidatePairing (device));
1581
- check_error (AMDeviceStartSession (device));
1603
+ connect_and_start_session (device);
1582
1604
1583
1605
NSArray *a = [NSArray arrayWithObjects:
1584
1606
@" CFBundleIdentifier" ,
@@ -1841,10 +1863,7 @@ void uninstall_app(AMDeviceRef device) {
1841
1863
if (cf_uninstall_bundle_id == NULL ) {
1842
1864
on_error (@" Error: Unable to get bundle id from user command or package %@ .\n Uninstall failed." , [NSString stringWithUTF8String: app_path]);
1843
1865
} else {
1844
- AMDeviceConnect (device);
1845
- assert (AMDeviceIsPaired (device));
1846
- check_error (AMDeviceValidatePairing (device));
1847
- check_error (AMDeviceStartSession (device));
1866
+ connect_and_start_session (device);
1848
1867
1849
1868
int code = AMDeviceSecureUninstallApplication (0 , device, cf_uninstall_bundle_id, 0 , NULL , 0 );
1850
1869
if (code == 0 ) {
@@ -1941,10 +1960,7 @@ void handle_device(AMDeviceRef device) {
1941
1960
if (cf_uninstall_bundle_id == NULL ) {
1942
1961
on_error (@" Error: Unable to get bundle id from user command or package %@ .\n Uninstall failed." , [NSString stringWithUTF8String: app_path]);
1943
1962
} else {
1944
- AMDeviceConnect (device);
1945
- assert (AMDeviceIsPaired (device));
1946
- check_error (AMDeviceValidatePairing (device));
1947
- check_error (AMDeviceStartSession (device));
1963
+ connect_and_start_session (device);
1948
1964
1949
1965
int code = AMDeviceSecureUninstallApplication (0 , device, cf_uninstall_bundle_id, 0 , NULL , 0 );
1950
1966
if (code == 0 ) {
@@ -1962,10 +1978,7 @@ void handle_device(AMDeviceRef device) {
1962
1978
NSLogOut (@" ------ Install phase ------" );
1963
1979
NSLogOut (@" [ 0%% ] Found %@ connected through %@ , beginning install" , device_full_name, device_interface_name);
1964
1980
1965
- AMDeviceConnect (device);
1966
- assert (AMDeviceIsPaired (device));
1967
- check_error (AMDeviceValidatePairing (device));
1968
- check_error (AMDeviceStartSession (device));
1981
+ connect_and_start_session (device);
1969
1982
1970
1983
CFDictionaryRef options;
1971
1984
if (app_deltas == NULL ) { // standard install
@@ -1981,10 +1994,7 @@ void handle_device(AMDeviceRef device) {
1981
1994
check_error (AMDeviceSecureTransferPath (0 , device, url, options, transfer_callback, 0 ));
1982
1995
close (*afcFd);
1983
1996
1984
- AMDeviceConnect (device);
1985
- assert (AMDeviceIsPaired (device));
1986
- check_error (AMDeviceValidatePairing (device));
1987
- check_error (AMDeviceStartSession (device));
1997
+ connect_and_start_session (device);
1988
1998
check_error (AMDeviceSecureInstallApplication (0 , device, url, options, install_callback, 0 ));
1989
1999
} else { // incremental install
1990
2000
check_error (AMDeviceStopSession (device));
@@ -2032,10 +2042,7 @@ void handle_device(AMDeviceRef device) {
2032
2042
CFIndex size = sizeof (keys)/sizeof (CFStringRef);
2033
2043
options = CFDictionaryCreate (NULL , (const void **)&keys, (const void **)&values, size, &kCFTypeDictionaryKeyCallBacks , &kCFTypeDictionaryValueCallBacks );
2034
2044
2035
- AMDeviceConnect (device);
2036
- assert (AMDeviceIsPaired (device));
2037
- check_error (AMDeviceValidatePairing (device));
2038
- check_error (AMDeviceStartSession (device));
2045
+ connect_and_start_session (device);
2039
2046
check_error (AMDeviceSecureInstallApplicationBundle (device, url, options, incremental_install_callback, 0 ));
2040
2047
CFRelease (extracted_bundle_id);
2041
2048
CFRelease (deltas_path);
0 commit comments