Skip to content

Commit aa450f8

Browse files
authored
fix: make devicectl launches work with debug-brk and report the pid (#89)
On iOS 17+ devices, waitForDebugger now launches the app running with --nativescript-debug-brk instead of devicectl --start-stopped, so the runtime does the waiting. App arguments go after devicectl's own options, and the start response includes the pid so the CLI can attach a native debugger.
1 parent 7283f21 commit aa450f8

5 files changed

Lines changed: 20 additions & 8 deletions

File tree

‎IOSDeviceLib/Constants.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ static const char *kCurrentApplicationsKey = "CurrentList";
7676
static const char *kCommandKey = "Command";
7777
static const char *kStatusKey = "Status";
7878
static const char *kResponse = "response";
79+
static const char *kPid = "pid";
7980
static const char *kSocket = "socket";
8081
static const char *kMessage = "message";
8182
static const char *kResponseCommandType = "responseCommandType";

‎IOSDeviceLib/DevicectlHelper.cpp‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
using json = nlohmann::json;
99

1010

11-
json run_devicectl(std::string command){
11+
json run_devicectl(std::string command, std::string app_arguments = ""){
1212
char templatea[] = "/tmp/mytempfile-XXXXXX";
1313
char* tempFileDir = mkdtemp(templatea);
1414
std::string outputFile = std::string(tempFileDir) + "tempFileName";
15-
std::string commandWithJson = command + " --quiet --json-output "+outputFile;
15+
// devicectl hands everything after "--" to the launched app.
16+
std::string commandWithJson = command + " --quiet --json-output " + outputFile + (app_arguments.empty() ? "" : " -- " + app_arguments);
1617
std::string cmdResult = exec(commandWithJson.c_str());
1718
FileInfo result = get_file_info(outputFile, true);
1819
std::remove(outputFile.c_str());
@@ -66,12 +67,13 @@ bool devicectl_stop_application(std::string &executable, std::string &device_ide
6667
}
6768

6869

69-
bool devicectl_start_application(std::string &bundle_id, std::string &device_identifier, bool wait_for_debugger) {
70+
bool devicectl_start_application(std::string &bundle_id, std::string &device_identifier, bool wait_for_debugger, int &pid) {
7071

71-
std::string command = "xcrun devicectl device process launch --device " + device_identifier + " --terminate-existing " + bundle_id + (wait_for_debugger?" --start-stopped":"");
72+
std::string command = "xcrun devicectl device process launch --device " + device_identifier + " --terminate-existing " + bundle_id;
7273

73-
json result = run_devicectl(command);
74+
json result = run_devicectl(command, wait_for_debugger ? "--nativescript-debug-brk" : "");
7475
if(json_ok(result)){
76+
pid = result.value("result", json()).value("process", json()).value("processIdentifier", -1);
7577
return true;
7678
}
7779
return false;

‎IOSDeviceLib/DevicectlHelper.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
#include "Declarations.h"
33

44
bool devicectl_stop_application(std::string &executable, std::string &device_identifier);
5-
bool devicectl_start_application(std::string &bundle_id, std::string &device_identifier, bool wait_for_debugger);
5+
bool devicectl_start_application(std::string &bundle_id, std::string &device_identifier, bool wait_for_debugger, int &pid);

‎IOSDeviceLib/IOSDeviceLib.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1773,8 +1773,10 @@ void start_app(std::string device_identifier,
17731773
kApplicationsCustomError);
17741774
} else {
17751775
if(check_xcode_major_version(15, device_identifier, method_id)){
1776-
if(devicectl_start_application(application_identifier, device_identifier, wait_for_debugger))
1776+
int pid = -1;
1777+
if(devicectl_start_application(application_identifier, device_identifier, wait_for_debugger, pid))
17771778
print(json({{kResponse, "Successfully started application"},
1779+
{kPid, pid},
17781780
{kId, method_id},
17791781
{kDeviceId, device_identifier}}));
17801782
else

‎typings/interfaces.d.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ declare global {
7575
response: string;
7676
}
7777

78+
interface IStartApplicationResponse extends IDeviceResponse {
79+
/**
80+
* Process id of the started application, on devices running iOS 17 or later.
81+
*/
82+
pid?: number;
83+
}
84+
7885
interface IDeviceMultipleResponse extends IDeviceId {
7986
response: string[];
8087
}
@@ -185,7 +192,7 @@ declare global {
185192
postNotification(postNotificationArray: IPostNotificationData[]): Promise<IDeviceResponse>[];
186193
awaitNotificationResponse(awaitNotificationResponseArray: IAwaitNotificatioNResponseData[]): Promise<IDeviceResponse>[];
187194
apps(deviceIdentifiers: string[]): Promise<IDeviceAppInfo>[];
188-
start(startArray: IIOSApplicationData[]): Promise<IDeviceResponse>[];
195+
start(startArray: IIOSApplicationData[]): Promise<IStartApplicationResponse>[];
189196
stop(stopArray: IIOSApplicationData[]): Promise<IDeviceResponse>[];
190197
startDeviceLog(deviceIdentifiers: string[]): void;
191198
startDeviceLogStream(logStreamArray: IDeviceLogStreamData[]): void;

0 commit comments

Comments
 (0)