Skip to content

Commit 4963297

Browse files
committed
feat: support waitForDebugger (debug-brk) on application start
1 parent 5cd9f8e commit 4963297

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

IOSDeviceLib/Constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static const char *kId = "id";
4848
static const char *kNullMessageId = "null";
4949
static const char *kDeviceId = "deviceId";
5050
static const char *kDeveloperDiskImage = "ddi";
51+
static const char *kWaitForDebugger = "waitForDebugger";
5152
static const char *kAppId = "appId";
5253
static const char *kNotificationName = "notificationName";
5354
static const char *kDestination = "destination";

IOSDeviceLib/GDBHelper.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ bool await_response(std::string message, SOCKET socket)
3535
return !starts_with(answer, kGDBErrorPrefix);
3636
}
3737

38-
bool init(std::string& executable, SOCKET socket, std::string& application_identifier, std::map<std::string, ApplicationCache>& apps_cache)
38+
bool init(std::string& executable, SOCKET socket, std::string& application_identifier, std::map<std::string, ApplicationCache>& apps_cache, bool wait_for_debugger = false)
3939
{
4040
if (apps_cache[application_identifier].has_initialized_gdb)
4141
return true;
@@ -50,21 +50,25 @@ bool init(std::string& executable, SOCKET socket, std::string& application_ident
5050
// We need this because GDB isn't connected to any inferior process (e.g. the application we want to control)
5151
RETURN_IF_FALSE(await_response("QSetDisableASLR:1", socket));
5252
std::stringstream result;
53+
std::string debugBrk = "--nativescript-debug-brk";
5354
// Initializes the argv in the form of arglen,argnum,arg
5455
// In our case:
5556
// arglen - twice the size of the original argument because we HEX encode it and every symbol becomes two bytes
5657
// argnum - 0 we only have one arg
5758
// arg - the actual arg, which is HEX encoded because we set the environment that way
5859
result << "A" << executable.size() * 2 << ",0," << to_hex(executable);
60+
if (wait_for_debugger) {
61+
result << debugBrk.size() * 2 << ",1," << to_hex(debugBrk);
62+
}
5963
RETURN_IF_FALSE(await_response(result.str(), socket));
6064
// After all of this is done we can actually call a method with these arguments
6165
apps_cache[application_identifier].has_initialized_gdb = true;
6266
return true;
6367
}
6468

65-
bool run_application(std::string& executable, SOCKET socket, std::string& application_identifier, DeviceData* device_data)
69+
bool run_application(std::string& executable, SOCKET socket, std::string& application_identifier, DeviceData* device_data, bool wait_for_debugger)
6670
{
67-
RETURN_IF_FALSE(init(executable, socket, application_identifier, device_data->apps_cache));
71+
RETURN_IF_FALSE(init(executable, socket, application_identifier, device_data->apps_cache, wait_for_debugger));
6872
// Couldn't find official info on this but I'm guessing this is the method we need to call
6973
RETURN_IF_FALSE(await_response("qLaunchSuccess", socket));
7074
// vCont specifies a command to be run - c means continue
@@ -81,9 +85,9 @@ bool run_application(std::string& executable, SOCKET socket, std::string& applic
8185
// We have decided we do not need this at the moment, but it is vital that it be read from the pipe
8286
// Else it will remain there and may be read later on and mistaken for a response to same other package
8387
std::string answer = receive_message_raw(socket);
84-
88+
8589
detach_connection(socket, application_identifier, device_data);
86-
90+
8791
return true;
8892
}
8993

@@ -106,7 +110,7 @@ bool stop_application(std::string & executable, SOCKET socket, std::string& appl
106110
// If we get infromation about the thread then apparently it is now safe to send the kill command
107111
// Thread information contains data that I cannot decipher - looks like this:
108112
// $T11thread:42202;00:0540001000000000;01:0608000700000000;02:0000000000000000;03:000c000000000000;04:0321000000000000;05:ffffffff00000000;06:0000000000000000;07:0100000000000000;08:bffbffff00000000;09:0000000700000000;0a:0001000700000000;0b:c0bdf0ff00000000;0c:034e0d00004d0d00;0d:0000000000000000;0e:004e0d00004e0d00;0f:0000000000000000;10:e1ffffffffffffff;11:20a59e8201000000;12:0000000000000000;13:0000000000000000;14:ffffffff00000000;15:0321000000000000;16:000c000000000000;17:08a0d66f01000000;18:0608000700000000;19:0000000000000000;1a:0608000700000000;1b:000c000000000000;1c:0100000000000000;1d:009fd66f01000000;1e:dcffab8101000000;1f:b09ed66f01000000;20:6c01ac8101000000;21:00000060;metype:5;mecount:2;medata:10003;medata:11;memory:0x16fd69f00=609fd66f01000000ecdcab8201000000;memory:0x16fd69f60=70acd66f0100000008b9ab8201000000;#00
109-
// If the application is not running the gdb will return $OK#00 instead of the above message.
113+
// If the application is not running the gdb will return $OK#00 instead of the above message.
110114
if (contains(answer, "thread") || contains(answer, "$OK#"))
111115
{
112116
can_send_kill = true;
@@ -134,7 +138,7 @@ void detach_connection(SOCKET socket, std::string& application_identifier, Devic
134138
device_data->apps_cache[application_identifier].has_initialized_gdb = false;
135139
device_data->services.erase(kDebugServer);
136140
#ifdef _WIN32
137-
closesocket(socket);
141+
closesocket(socket);
138142
#else
139143
close(socket);
140144
#endif

IOSDeviceLib/GDBHelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
std::string get_gdb_message(std::string message);
88
int gdb_send_message(std::string message, SOCKET socket, long long length = -1);
9-
bool run_application(std::string& executable, SOCKET socket, std::string& application_identifier, DeviceData* device_data);
9+
bool run_application(std::string& executable, SOCKET socket, std::string& application_identifier, DeviceData* device_data, bool wait_for_debugger);
1010
bool stop_application(std::string& executable, SOCKET socket, std::string& application_identifier, std::map<std::string, ApplicationCache>& apps_cache);
1111
void detach_connection(SOCKET socket, std::string& application_identifier, DeviceData* device_data);
1212

IOSDeviceLib/IOSDeviceLib.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ void stop_app(std::string device_identifier, std::string application_identifier,
12361236
}
12371237
}
12381238

1239-
void start_app(std::string device_identifier, std::string application_identifier, std::string ddi, std::string method_id)
1239+
void start_app(std::string device_identifier, std::string application_identifier, std::string ddi, std::string method_id, bool wait_for_debugger)
12401240
{
12411241
if (!devices.count(device_identifier))
12421242
{
@@ -1260,7 +1260,7 @@ void start_app(std::string device_identifier, std::string application_identifier
12601260
}
12611261

12621262
std::string executable = map[application_identifier][kPathPascalCase] + "/" + map[application_identifier]["CFBundleExecutable"];
1263-
if (run_application(executable, (SOCKET)gdb, application_identifier, &devices[device_identifier]))
1263+
if (run_application(executable, (SOCKET)gdb, application_identifier, &devices[device_identifier], wait_for_debugger))
12641264
print(json({ { kResponse, "Successfully started application" },{ kId, method_id },{ kDeviceId, device_identifier } }));
12651265
else
12661266
print_error("Could not start application", device_identifier, method_id, kApplicationsCustomError);
@@ -1499,7 +1499,8 @@ int main()
14991499
std::string application_identifier = arg.value(kAppId, "");
15001500
std::string device_identifier = arg.value(kDeviceId, "");
15011501
std::string ddi = arg.value(kDeveloperDiskImage, "");
1502-
start_app(device_identifier, application_identifier, ddi, method_id);
1502+
std::string wait_for_debugger = arg.value(kWaitForDebugger, "");
1503+
start_app(device_identifier, application_identifier, ddi, method_id, wait_for_debugger == "true");
15031504
}
15041505
}
15051506
else if (method_name == "stop")

0 commit comments

Comments
 (0)