Skip to content

Commit d623003

Browse files
Implement --kill, given pid or bundle id (#577)
* Implement --kill, given pid or bundle id * Change variable name from pid to command_pid
1 parent 136872d commit d623003

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

src/ios-deploy/ios-deploy.m

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
char *symbols_download_directory = NULL;
130130
char *profile_uuid = NULL;
131131
char *profile_path = NULL;
132+
int command_pid = -1;
132133
int _timeout = 0;
133134
int _detectDeadlockTimeout = 0;
134135
bool _json_output = false;
@@ -3084,6 +3085,31 @@ void get_pid(AMDeviceRef device) {
30843085
@"pid": pid});
30853086
}
30863087

3088+
void kill_app(AMDeviceRef device) {
3089+
if (bundle_id == NULL && command_pid <= 0) {
3090+
on_error(@"Error: must specify either --pid or --bundle_id");
3091+
}
3092+
3093+
instruments_connect_service(device);
3094+
instruments_perform_handshake();
3095+
3096+
NSNumber* ns_pid = [NSNumber numberWithInt:command_pid];
3097+
if (![ns_pid isGreaterThan:@0]) {
3098+
CFStringRef cf_bundle_id = CFAutorelease(CFStringCreateWithCString(NULL, bundle_id, kCFStringEncodingUTF8));
3099+
ns_pid = pid_for_bundle_id((NSString*)cf_bundle_id);
3100+
3101+
if (![ns_pid isGreaterThan:@0]) {
3102+
NSLogOut(@"Could not find pid for bundle '%@'. Nothing to kill.", cf_bundle_id);
3103+
return;
3104+
}
3105+
}
3106+
3107+
int32_t channel = instruments_make_channel(@"com.apple.instruments.server.services.processcontrol");
3108+
3109+
instruments_send_message(channel, @"killPid:", @[instruments_object_argument(ns_pid)], false /* expectes_reply */);
3110+
[ns_pid release];
3111+
}
3112+
30873113
void list_processes(AMDeviceRef device) {
30883114
instruments_connect_service(device);
30893115
instruments_perform_handshake();
@@ -3102,7 +3128,7 @@ void list_processes(AMDeviceRef device) {
31023128

31033129
NSMutableArray* filteredProcesses = NSMutableArray.array;
31043130

3105-
if (pid > 0) {
3131+
if ([pid isGreaterThan:@0]) {
31063132
for (NSDictionary* proc in processes) {
31073133
NSNumber* procPid = proc[@"pid"];
31083134
if (procPid == pid) {
@@ -3226,6 +3252,8 @@ void handle_device(AMDeviceRef device) {
32263252
} else if (strcmp("check_developer_mode", command) == 0) {
32273253
check_developer_mode(device);
32283254
#endif
3255+
} else if (strcmp("kill_app", command) == 0) {
3256+
kill_app(device);
32293257
}
32303258
exit(0);
32313259
}
@@ -3507,6 +3535,8 @@ void usage(const char* app) {
35073535
@" -B, --list_bundle_id list bundle_id \n"
35083536
@" --list_processes list running processes \n"
35093537
@" --get_pid get process id for the bundle. must specify --bundle_id\n"
3538+
@" --pid <pid> specify pid, to be used with --kill\n"
3539+
@" --kill kill a process. must specify either --pid or --bundle_id\n"
35103540
@" -W, --no-wifi ignore wifi devices\n"
35113541
@" -C, --get_battery_level get battery current capacity \n"
35123542
@" -O, --output <file> write stdout to this file\n"
@@ -3526,10 +3556,9 @@ void usage(const char* app) {
35263556
@" --profile-install <file> install a provisioning profile\n"
35273557
@" --profile-uninstall uninstall a provisioning profile (requires --profile-uuid <UUID>)\n"
35283558
#if defined(IOS_DEPLOY_FEATURE_DEVELOPER_MODE)
3529-
@" --check-developer-mode checks whether the given device has developer mode enabled (requires Xcode 14 or newer)\n",
3530-
#else
3531-
,
3559+
@" --check-developer-mode checks whether the given device has developer mode enabled (requires Xcode 14 or newer)\n"
35323560
#endif
3561+
,
35333562
[NSString stringWithUTF8String:app]);
35343563
}
35353564

@@ -3600,6 +3629,8 @@ int main(int argc, char *argv[]) {
36003629
#endif
36013630
{ "list_processes", no_argument, NULL, 1009},
36023631
{ "get_pid", no_argument, NULL, 1010},
3632+
{ "pid", required_argument, NULL, 1011},
3633+
{ "kill", no_argument, NULL, 1012},
36033634
{ NULL, 0, NULL, 0 },
36043635
};
36053636
int ch;
@@ -3797,6 +3828,13 @@ int main(int argc, char *argv[]) {
37973828
if (!keys) keys = [[NSMutableArray alloc] init];
37983829
[keys addObject: [NSString stringWithUTF8String:optarg]];
37993830
break;
3831+
case 1011:
3832+
command_pid = atoi(optarg);
3833+
break;
3834+
case 1012:
3835+
command_only = true;
3836+
command = "kill_app";
3837+
break;
38003838
default:
38013839
usage(argv[0]);
38023840
return exitcode_error;

0 commit comments

Comments
 (0)