@@ -89,6 +89,7 @@ GDBRemoteCommunicationClient::GDBRemoteCommunicationClient()
8989 m_supports_jGetSharedCacheInfo(eLazyBoolCalculate),
9090 m_supports_QPassSignals(eLazyBoolCalculate),
9191 m_supports_error_string_reply(eLazyBoolCalculate),
92+ m_supports_multiprocess(eLazyBoolCalculate),
9293 m_supports_qProcessInfoPID(true ), m_supports_qfProcessInfo(true ),
9394 m_supports_qUserName(true ), m_supports_qGroupName(true ),
9495 m_supports_qThreadStopInfo(true ), m_supports_z0(true ),
@@ -292,6 +293,7 @@ void GDBRemoteCommunicationClient::ResetDiscoverableSettings(bool did_exec) {
292293 m_prepare_for_reg_writing_reply = eLazyBoolCalculate;
293294 m_attach_or_wait_reply = eLazyBoolCalculate;
294295 m_avoid_g_packets = eLazyBoolCalculate;
296+ m_supports_multiprocess = eLazyBoolCalculate;
295297 m_supports_qXfer_auxv_read = eLazyBoolCalculate;
296298 m_supports_qXfer_libraries_read = eLazyBoolCalculate;
297299 m_supports_qXfer_libraries_svr4_read = eLazyBoolCalculate;
@@ -342,11 +344,13 @@ void GDBRemoteCommunicationClient::GetRemoteQSupported() {
342344 m_supports_augmented_libraries_svr4_read = eLazyBoolNo;
343345 m_supports_qXfer_features_read = eLazyBoolNo;
344346 m_supports_qXfer_memory_map_read = eLazyBoolNo;
347+ m_supports_multiprocess = eLazyBoolNo;
345348 m_max_packet_size = UINT64_MAX ; // It's supposed to always be there, but if
346349 // not, we assume no limit
347350
348351 // build the qSupported packet
349- std::vector<std::string> features = {" xmlRegisters=i386,arm,mips,arc" };
352+ std::vector<std::string> features = {" xmlRegisters=i386,arm,mips,arc" ,
353+ " multiprocess+" };
350354 StreamString packet;
351355 packet.PutCString (" qSupported" );
352356 for (uint32_t i = 0 ; i < features.size (); ++i) {
@@ -433,6 +437,11 @@ void GDBRemoteCommunicationClient::GetRemoteQSupported() {
433437 else
434438 m_supports_QPassSignals = eLazyBoolNo;
435439
440+ if (::strstr (response_cstr, " multiprocess+" ))
441+ m_supports_multiprocess = eLazyBoolYes;
442+ else
443+ m_supports_multiprocess = eLazyBoolNo;
444+
436445 const char *packet_size_str = ::strstr (response_cstr, " PacketSize=" );
437446 if (packet_size_str) {
438447 StringExtractorGDBRemote packet_response (packet_size_str +
@@ -741,12 +750,14 @@ lldb::pid_t GDBRemoteCommunicationClient::GetCurrentProcessID(bool allow_lazy) {
741750 // If we don't get a response for $qC, check if $qfThreadID gives us a
742751 // result.
743752 if (m_curr_pid == LLDB_INVALID_PROCESS_ID ) {
744- std::vector<lldb::tid_t > thread_ids;
745753 bool sequence_mutex_unavailable;
746- size_t size;
747- size = GetCurrentThreadIDs (thread_ids, sequence_mutex_unavailable);
748- if (size && !sequence_mutex_unavailable) {
749- m_curr_pid = thread_ids.front ();
754+ auto ids = GetCurrentProcessAndThreadIDs (sequence_mutex_unavailable);
755+ if (!ids.empty () && !sequence_mutex_unavailable) {
756+ // If server returned an explicit PID, use that.
757+ m_curr_pid = ids.front ().first ;
758+ // Otherwise, use the TID of the first thread (Linux hack).
759+ if (m_curr_pid == LLDB_INVALID_PROCESS_ID )
760+ m_curr_pid = ids.front ().second ;
750761 m_curr_pid_is_valid = eLazyBoolYes;
751762 return m_curr_pid;
752763 }
@@ -1125,8 +1136,23 @@ bool GDBRemoteCommunicationClient::GetDefaultThreadId(lldb::tid_t &tid) {
11251136 if (!response.IsNormalResponse ())
11261137 return false ;
11271138
1128- if (response.GetChar () == ' Q' && response.GetChar () == ' C' )
1129- tid = response.GetHexMaxU64 (true , -1 );
1139+ if (response.GetChar () == ' Q' && response.GetChar () == ' C' ) {
1140+ auto pid_tid = response.GetPidTid (0 );
1141+ if (!pid_tid)
1142+ return false ;
1143+
1144+ lldb::pid_t pid = pid_tid->first ;
1145+ // invalid
1146+ if (pid == StringExtractorGDBRemote::AllProcesses)
1147+ return false ;
1148+
1149+ // if we get pid as well, update m_curr_pid
1150+ if (pid != 0 ) {
1151+ m_curr_pid = pid;
1152+ m_curr_pid_is_valid = eLazyBoolYes;
1153+ }
1154+ tid = pid_tid->second ;
1155+ }
11301156
11311157 return true ;
11321158}
@@ -2766,9 +2792,10 @@ uint8_t GDBRemoteCommunicationClient::SendGDBStoppointTypePacket(
27662792 return UINT8_MAX ;
27672793}
27682794
2769- size_t GDBRemoteCommunicationClient::GetCurrentThreadIDs (
2770- std::vector<lldb::tid_t > &thread_ids, bool &sequence_mutex_unavailable) {
2771- thread_ids.clear ();
2795+ std::vector<std::pair<lldb::pid_t , lldb::tid_t >>
2796+ GDBRemoteCommunicationClient::GetCurrentProcessAndThreadIDs (
2797+ bool &sequence_mutex_unavailable) {
2798+ std::vector<std::pair<lldb::pid_t , lldb::tid_t >> ids;
27722799
27732800 Lock lock (*this , false );
27742801 if (lock) {
@@ -2786,11 +2813,11 @@ size_t GDBRemoteCommunicationClient::GetCurrentThreadIDs(
27862813 break ;
27872814 if (ch == ' m' ) {
27882815 do {
2789- tid_t tid = response.GetHexMaxU64 (false , LLDB_INVALID_THREAD_ID );
2816+ auto pid_tid = response.GetPidTid (LLDB_INVALID_PROCESS_ID );
2817+ if (!pid_tid)
2818+ return {};
27902819
2791- if (tid != LLDB_INVALID_THREAD_ID ) {
2792- thread_ids.push_back (tid);
2793- }
2820+ ids.push_back (pid_tid.getValue ());
27942821 ch = response.GetChar (); // Skip the command separator
27952822 } while (ch == ' ,' ); // Make sure we got a comma separator
27962823 }
@@ -2803,10 +2830,10 @@ size_t GDBRemoteCommunicationClient::GetCurrentThreadIDs(
28032830 * be as simple as 'S05'. There is no packet which can give us pid and/or
28042831 * tid.
28052832 * Assume pid=tid=1 in such cases.
2806- */
2833+ */
28072834 if ((response.IsUnsupportedResponse () || response.IsNormalResponse ()) &&
2808- thread_ids .size () == 0 && IsConnected ()) {
2809- thread_ids. push_back ( 1 );
2835+ ids .size () == 0 && IsConnected ()) {
2836+ ids. emplace_back ( 1 , 1 );
28102837 }
28112838 } else {
28122839 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS |
@@ -2815,6 +2842,28 @@ size_t GDBRemoteCommunicationClient::GetCurrentThreadIDs(
28152842 " packet 'qfThreadInfo'" );
28162843 sequence_mutex_unavailable = true ;
28172844 }
2845+
2846+ return ids;
2847+ }
2848+
2849+ size_t GDBRemoteCommunicationClient::GetCurrentThreadIDs (
2850+ std::vector<lldb::tid_t > &thread_ids, bool &sequence_mutex_unavailable) {
2851+ lldb::pid_t pid = GetCurrentProcessID ();
2852+ thread_ids.clear ();
2853+
2854+ auto ids = GetCurrentProcessAndThreadIDs (sequence_mutex_unavailable);
2855+ if (ids.empty () || sequence_mutex_unavailable)
2856+ return 0 ;
2857+
2858+ for (auto id : ids) {
2859+ // skip threads that do not belong to the current process
2860+ if (id.first != LLDB_INVALID_PROCESS_ID && id.first != pid)
2861+ continue ;
2862+ if (id.second != LLDB_INVALID_THREAD_ID &&
2863+ id.second != StringExtractorGDBRemote::AllThreads)
2864+ thread_ids.push_back (id.second );
2865+ }
2866+
28182867 return thread_ids.size ();
28192868}
28202869
0 commit comments