Index: lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h =================================================================== --- lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h +++ lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.h @@ -231,6 +231,12 @@ virtual std::vector HandleFeatures(const llvm::ArrayRef client_features) override; + // Verify and get the process instance selected by the client. + // If run is true, the process selected by "Hc" packet will be returned, + // otherwise the one selected by "Hg" will be used. Returns nullptr if there + // is no process debugged. + NativeProcessProtocol *GetCurrentProcess(bool run) const; + private: llvm::Expected> BuildTargetXml(); @@ -256,13 +262,18 @@ void StopSTDIOForwarding(); - // Read thread-id from packet. If the thread-id is correct, returns it. - // Otherwise, returns the error. + // Read thread-id from packet. If run is true, a TID without PID will be + // associated with the process selected via "Hc" packet. Otherwise, the PID + // selected by "Hg" will be used. // // If allow_all is true, then the pid/tid value of -1 ('all') will be allowed. // In any case, the function assumes that exactly one inferior is being // debugged and rejects pid values that do no match that inferior. + // + // If the thread-id is correct, it is returned. Otherwise, the error is + // returned. llvm::Expected ReadTid(StringExtractorGDBRemote &packet, + bool run, bool allow_all = false); // Call SetEnabledExtensions() with appropriate flags on the process. Index: lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp =================================================================== --- lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp +++ lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp @@ -1198,21 +1198,18 @@ GDBRemoteCommunication::PacketResult GDBRemoteCommunicationServerLLGS::Handle_jLLDBTraceSupported( StringExtractorGDBRemote &packet) { - - // Fail if we don't have a current process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) + auto *process = GetCurrentProcess(false); + if (!process) return SendErrorResponse(Status("Process not running.")); - return SendJSONResponse(m_debugged_process_up->TraceSupported()); + return SendJSONResponse(process->TraceSupported()); } GDBRemoteCommunication::PacketResult GDBRemoteCommunicationServerLLGS::Handle_jLLDBTraceStop( StringExtractorGDBRemote &packet) { - // Fail if we don't have a current process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) + auto *process = GetCurrentProcess(false); + if (!process) return SendErrorResponse(Status("Process not running.")); packet.ConsumeFront("jLLDBTraceStop:"); @@ -1221,7 +1218,7 @@ if (!stop_request) return SendErrorResponse(stop_request.takeError()); - if (Error err = m_debugged_process_up->TraceStop(*stop_request)) + if (Error err = process->TraceStop(*stop_request)) return SendErrorResponse(std::move(err)); return SendOKResponse(); @@ -1230,10 +1227,8 @@ GDBRemoteCommunication::PacketResult GDBRemoteCommunicationServerLLGS::Handle_jLLDBTraceStart( StringExtractorGDBRemote &packet) { - - // Fail if we don't have a current process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) + auto *process = GetCurrentProcess(false); + if (!process) return SendErrorResponse(Status("Process not running.")); packet.ConsumeFront("jLLDBTraceStart:"); @@ -1242,8 +1237,7 @@ if (!request) return SendErrorResponse(request.takeError()); - if (Error err = - m_debugged_process_up->TraceStart(packet.Peek(), request->type)) + if (Error err = process->TraceStart(packet.Peek(), request->type)) return SendErrorResponse(std::move(err)); return SendOKResponse(); @@ -1252,10 +1246,8 @@ GDBRemoteCommunication::PacketResult GDBRemoteCommunicationServerLLGS::Handle_jLLDBTraceGetState( StringExtractorGDBRemote &packet) { - - // Fail if we don't have a current process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) + auto *process = GetCurrentProcess(false); + if (!process) return SendErrorResponse(Status("Process not running.")); packet.ConsumeFront("jLLDBTraceGetState:"); @@ -1264,16 +1256,14 @@ if (!request) return SendErrorResponse(request.takeError()); - return SendJSONResponse(m_debugged_process_up->TraceGetState(request->type)); + return SendJSONResponse(process->TraceGetState(request->type)); } GDBRemoteCommunication::PacketResult GDBRemoteCommunicationServerLLGS::Handle_jLLDBTraceGetBinaryData( StringExtractorGDBRemote &packet) { - - // Fail if we don't have a current process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) + auto *process = GetCurrentProcess(false); + if (!process) return SendErrorResponse(Status("Process not running.")); packet.ConsumeFront("jLLDBTraceGetBinaryData:"); @@ -1284,7 +1274,7 @@ return SendErrorResponse(Status(request.takeError())); if (Expected> bytes = - m_debugged_process_up->TraceGetBinaryData(*request)) { + process->TraceGetBinaryData(*request)) { StreamGDBRemote response; response.PutEscapedBytes(bytes->data(), bytes->size()); return SendPacketNoLock(response.GetString()); @@ -1295,12 +1285,11 @@ GDBRemoteCommunication::PacketResult GDBRemoteCommunicationServerLLGS::Handle_qProcessInfo( StringExtractorGDBRemote &packet) { - // Fail if we don't have a current process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) + auto *process = GetCurrentProcess(false); + if (!process) return SendErrorResponse(68); - lldb::pid_t pid = m_debugged_process_up->GetID(); + lldb::pid_t pid = process->GetID(); if (pid == LLDB_INVALID_PROCESS_ID) return SendErrorResponse(1); @@ -1316,17 +1305,16 @@ GDBRemoteCommunication::PacketResult GDBRemoteCommunicationServerLLGS::Handle_qC(StringExtractorGDBRemote &packet) { - // Fail if we don't have a current process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) + auto *process = GetCurrentProcess(false); + if (!process) return SendErrorResponse(68); // Make sure we set the current thread so g and p packets return the data the // gdb will expect. - lldb::tid_t tid = m_debugged_process_up->GetCurrentThreadID(); + lldb::tid_t tid = process->GetCurrentThreadID(); SetCurrentThreadID(tid); - NativeThreadProtocol *thread = m_debugged_process_up->GetCurrentThread(); + NativeThreadProtocol *thread = process->GetCurrentThread(); if (!thread) return SendErrorResponse(69); @@ -1342,15 +1330,16 @@ StopSTDIOForwarding(); - if (!m_debugged_process_up) { + auto *process = GetCurrentProcess(false); + if (!process) { LLDB_LOG(log, "No debugged process found."); return PacketResult::Success; } - Status error = m_debugged_process_up->Kill(); + Status error = process->Kill(); if (error.Fail()) - LLDB_LOG(log, "Failed to kill debugged process {0}: {1}", - m_debugged_process_up->GetID(), error); + LLDB_LOG(log, "Failed to kill debugged process {0}: {1}", process->GetID(), + error); // No OK response for kill packet. // return SendOKResponse (); @@ -1396,8 +1385,8 @@ Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); LLDB_LOGF(log, "GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__); - // Ensure we have a native process. - if (!m_debugged_process_up) { + auto *process = GetCurrentProcess(true); + if (!process) { LLDB_LOGF(log, "GDBRemoteCommunicationServerLLGS::%s no debugged process " "shared pointer", @@ -1450,20 +1439,20 @@ } else { // Send the signal to the process since we weren't targeting a specific // continue thread with the signal. - error = m_debugged_process_up->Signal(signo); + error = process->Signal(signo); if (error.Fail()) { LLDB_LOG(log, "failed to send signal for process {0}: {1}", - m_debugged_process_up->GetID(), error); + process->GetID(), error); return SendErrorResponse(0x52); } } // Resume the threads. - error = m_debugged_process_up->Resume(resume_actions); + error = process->Resume(resume_actions); if (error.Fail()) { LLDB_LOG(log, "failed to resume threads for process {0}: {1}", - m_debugged_process_up->GetID(), error); + process->GetID(), error); return SendErrorResponse(0x38); } @@ -1488,7 +1477,8 @@ } // Ensure we have a native process. - if (!m_debugged_process_up) { + auto *process = GetCurrentProcess(true); + if (!process) { LLDB_LOGF(log, "GDBRemoteCommunicationServerLLGS::%s no debugged process " "shared pointer", @@ -1500,14 +1490,13 @@ ResumeActionList actions(StateType::eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); - Status error = m_debugged_process_up->Resume(actions); + Status error = process->Resume(actions); if (error.Fail()) { - LLDB_LOG(log, "c failed for process {0}: {1}", - m_debugged_process_up->GetID(), error); + LLDB_LOG(log, "c failed for process {0}: {1}", process->GetID(), error); return SendErrorResponse(GDBRemoteServerError::eErrorResume); } - LLDB_LOG(log, "continued process {0}", m_debugged_process_up->GetID()); + LLDB_LOG(log, "continued process {0}", process->GetID()); // No response required from continue. return PacketResult::Success; } @@ -1550,7 +1539,8 @@ } // Ensure we have a native process. - if (!m_debugged_process_up) { + auto *process = GetCurrentProcess(true); + if (!process) { LLDB_LOG(log, "no debugged process"); return SendErrorResponse(0x36); } @@ -1603,7 +1593,8 @@ // Consume the separator. packet.GetChar(); - llvm::Expected tid_ret = ReadTid(packet, /*allow_all=*/true); + llvm::Expected tid_ret = ReadTid(packet, /*run=*/true, + /*allow_all=*/true); if (!tid_ret) return SendErrorResponse(tid_ret.takeError()); @@ -1615,14 +1606,13 @@ thread_actions.Append(thread_action); } - Status error = m_debugged_process_up->Resume(thread_actions); + Status error = process->Resume(thread_actions); if (error.Fail()) { - LLDB_LOG(log, "vCont failed for process {0}: {1}", - m_debugged_process_up->GetID(), error); + LLDB_LOG(log, "vCont failed for process {0}: {1}", process->GetID(), error); return SendErrorResponse(GDBRemoteServerError::eErrorResume); } - LLDB_LOG(log, "continued process {0}", m_debugged_process_up->GetID()); + LLDB_LOG(log, "continued process {0}", process->GetID()); // No response required from vCont. return PacketResult::Success; } @@ -1649,10 +1639,11 @@ // Handle the $? gdbremote command. // If no process, indicate error - if (!m_debugged_process_up) + auto *process = GetCurrentProcess(true); + if (!process) return SendErrorResponse(02); - return SendStopReasonForState(m_debugged_process_up->GetState()); + return SendStopReasonForState(process->GetState()); } GDBRemoteCommunication::PacketResult @@ -1698,13 +1689,12 @@ GDBRemoteCommunication::PacketResult GDBRemoteCommunicationServerLLGS::Handle_qRegisterInfo( StringExtractorGDBRemote &packet) { - // Fail if we don't have a current process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) + auto *process = GetCurrentProcess(false); + if (!process) return SendErrorResponse(68); // Ensure we have a thread. - NativeThreadProtocol *thread = m_debugged_process_up->GetThreadAtIndex(0); + NativeThreadProtocol *thread = process->GetThreadAtIndex(0); if (!thread) return SendErrorResponse(69); @@ -1798,12 +1788,9 @@ StringExtractorGDBRemote &packet) { Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); - // Fail if we don't have a current process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { - LLDB_LOG(log, "no process ({0}), returning OK", - m_debugged_process_up ? "invalid process id" - : "null m_debugged_process_up"); + auto *process = GetCurrentProcess(false); + if (!process) { + LLDB_LOG(log, "no process, returning OK"); return SendOKResponse(); } @@ -1814,9 +1801,9 @@ NativeThreadProtocol *thread; uint32_t thread_index; for (thread_index = 0, - thread = m_debugged_process_up->GetThreadAtIndex(thread_index); + thread = process->GetThreadAtIndex(thread_index); thread; ++thread_index, - thread = m_debugged_process_up->GetThreadAtIndex(thread_index)) { + thread = process->GetThreadAtIndex(thread_index)) { LLDB_LOG(log, "iterated thread {0}(tid={2})", thread_index, thread->GetID()); if (thread_index > 0) @@ -2094,7 +2081,8 @@ } // Parse out the thread number. - llvm::Expected tid_ret = ReadTid(packet, /*allow_all=*/true); + llvm::Expected tid_ret = + ReadTid(packet, /*run=*/h_variant == 'c', /*allow_all=*/true); if (!tid_ret) return SendErrorResponse(tid_ret.takeError()); @@ -2136,8 +2124,7 @@ Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); // Fail if we don't have a current process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { + if (!GetCurrentProcess(false)) { LLDB_LOGF( log, "GDBRemoteCommunicationServerLLGS::%s failed, no process available", @@ -2172,21 +2159,20 @@ Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); // Fail if we don't have a current process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { + auto *process = GetCurrentProcess(true); + if (!process) { LLDB_LOG(log, "failed, no process available"); return SendErrorResponse(0x15); } // Interrupt the process. - Status error = m_debugged_process_up->Interrupt(); + Status error = process->Interrupt(); if (error.Fail()) { - LLDB_LOG(log, "failed for process {0}: {1}", m_debugged_process_up->GetID(), - error); + LLDB_LOG(log, "failed for process {0}: {1}", process->GetID(), error); return SendErrorResponse(GDBRemoteServerError::eErrorResume); } - LLDB_LOG(log, "stopped process {0}", m_debugged_process_up->GetID()); + LLDB_LOG(log, "stopped process {0}", process->GetID()); // No response required from stop all. return PacketResult::Success; @@ -2197,8 +2183,8 @@ StringExtractorGDBRemote &packet) { Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { + auto *process = GetCurrentProcess(false); + if (!process) { LLDB_LOGF( log, "GDBRemoteCommunicationServerLLGS::%s failed, no process available", @@ -2240,14 +2226,13 @@ // Retrieve the process memory. size_t bytes_read = 0; - Status error = m_debugged_process_up->ReadMemoryWithoutTrap( + Status error = process->ReadMemoryWithoutTrap( read_addr, &buf[0], byte_count, bytes_read); if (error.Fail()) { LLDB_LOGF(log, "GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " mem 0x%" PRIx64 ": failed to read. Error: %s", - __FUNCTION__, m_debugged_process_up->GetID(), read_addr, - error.AsCString()); + __FUNCTION__, process->GetID(), read_addr, error.AsCString()); return SendErrorResponse(0x08); } @@ -2255,8 +2240,7 @@ LLDB_LOGF(log, "GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " mem 0x%" PRIx64 ": read 0 of %" PRIu64 " requested bytes", - __FUNCTION__, m_debugged_process_up->GetID(), read_addr, - byte_count); + __FUNCTION__, process->GetID(), read_addr, byte_count); return SendErrorResponse(0x08); } @@ -2278,8 +2262,8 @@ GDBRemoteCommunicationServerLLGS::Handle__M(StringExtractorGDBRemote &packet) { Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { + auto *process = GetCurrentProcess(false); + if (!process) { LLDB_LOGF( log, "GDBRemoteCommunicationServerLLGS::%s failed, no process available", @@ -2314,8 +2298,7 @@ } } - llvm::Expected addr = - m_debugged_process_up->AllocateMemory(size, perms); + llvm::Expected addr = process->AllocateMemory(size, perms); if (!addr) return SendErrorResponse(addr.takeError()); @@ -2328,8 +2311,8 @@ GDBRemoteCommunicationServerLLGS::Handle__m(StringExtractorGDBRemote &packet) { Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { + auto *process = GetCurrentProcess(false); + if (!process) { LLDB_LOGF( log, "GDBRemoteCommunicationServerLLGS::%s failed, no process available", @@ -2346,7 +2329,7 @@ if (addr == LLDB_INVALID_ADDRESS) return SendIllFormedResponse(packet, "Address not valid"); - if (llvm::Error Err = m_debugged_process_up->DeallocateMemory(addr)) + if (llvm::Error Err = process->DeallocateMemory(addr)) return SendErrorResponse(std::move(Err)); return SendOKResponse(); @@ -2356,8 +2339,8 @@ GDBRemoteCommunicationServerLLGS::Handle_M(StringExtractorGDBRemote &packet) { Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { + auto *process = GetCurrentProcess(false); + if (!process) { LLDB_LOGF( log, "GDBRemoteCommunicationServerLLGS::%s failed, no process available", @@ -2406,8 +2389,7 @@ LLDB_LOG(log, "pid {0} mem {1:x}: asked to write {2} bytes, but only found {3} " "to convert.", - m_debugged_process_up->GetID(), write_addr, byte_count, - convert_count); + process->GetID(), write_addr, byte_count, convert_count); return SendIllFormedResponse(packet, "M content byte length specified did " "not match hex-encoded content " "length"); @@ -2415,17 +2397,17 @@ // Write the process memory. size_t bytes_written = 0; - Status error = m_debugged_process_up->WriteMemory(write_addr, &buf[0], - byte_count, bytes_written); + Status error = + process->WriteMemory(write_addr, &buf[0], byte_count, bytes_written); if (error.Fail()) { LLDB_LOG(log, "pid {0} mem {1:x}: failed to write. Error: {2}", - m_debugged_process_up->GetID(), write_addr, error); + process->GetID(), write_addr, error); return SendErrorResponse(0x09); } if (bytes_written == 0) { LLDB_LOG(log, "pid {0} mem {1:x}: wrote 0 of {2} requested bytes", - m_debugged_process_up->GetID(), write_addr, byte_count); + process->GetID(), write_addr, byte_count); return SendErrorResponse(0x09); } @@ -2444,8 +2426,8 @@ // Ensure we have a process running; otherwise, we can't figure this out // since we won't have a NativeProcessProtocol. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { + auto *process = GetCurrentProcess(false); + if (!process) { LLDB_LOGF( log, "GDBRemoteCommunicationServerLLGS::%s failed, no process available", @@ -2455,8 +2437,7 @@ // Test if we can get any region back when asking for the region around NULL. MemoryRegionInfo region_info; - const Status error = - m_debugged_process_up->GetMemoryRegionInfo(0, region_info); + const Status error = process->GetMemoryRegionInfo(0, region_info); if (error.Fail()) { // We don't support memory region info collection for this // NativeProcessProtocol. @@ -2472,8 +2453,8 @@ Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); // Ensure we have a process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { + auto *process = GetCurrentProcess(false); + if (!process) { LLDB_LOGF( log, "GDBRemoteCommunicationServerLLGS::%s failed, no process available", @@ -2493,8 +2474,7 @@ // Get the memory region info for the target address. MemoryRegionInfo region_info; - const Status error = - m_debugged_process_up->GetMemoryRegionInfo(read_addr, region_info); + const Status error = process->GetMemoryRegionInfo(read_addr, region_info); if (error.Fail()) { // Return the error message. @@ -2549,8 +2529,8 @@ GDBRemoteCommunication::PacketResult GDBRemoteCommunicationServerLLGS::Handle_Z(StringExtractorGDBRemote &packet) { // Ensure we have a process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { + auto *process = GetCurrentProcess(false); + if (!process) { Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); LLDB_LOG(log, "failed, no process available"); return SendErrorResponse(0x15); @@ -2619,23 +2599,22 @@ if (want_breakpoint) { // Try to set the breakpoint. - const Status error = - m_debugged_process_up->SetBreakpoint(addr, size, want_hardware); + const Status error = process->SetBreakpoint(addr, size, want_hardware); if (error.Success()) return SendOKResponse(); Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); - LLDB_LOG(log, "pid {0} failed to set breakpoint: {1}", - m_debugged_process_up->GetID(), error); + LLDB_LOG(log, "pid {0} failed to set breakpoint: {1}", process->GetID(), + error); return SendErrorResponse(0x09); } else { // Try to set the watchpoint. - const Status error = m_debugged_process_up->SetWatchpoint( + const Status error = process->SetWatchpoint( addr, size, watch_flags, want_hardware); if (error.Success()) return SendOKResponse(); Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); - LLDB_LOG(log, "pid {0} failed to set watchpoint: {1}", - m_debugged_process_up->GetID(), error); + LLDB_LOG(log, "pid {0} failed to set watchpoint: {1}", process->GetID(), + error); return SendErrorResponse(0x09); } } @@ -2643,8 +2622,8 @@ GDBRemoteCommunication::PacketResult GDBRemoteCommunicationServerLLGS::Handle_z(StringExtractorGDBRemote &packet) { // Ensure we have a process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { + auto *process = GetCurrentProcess(false); + if (!process) { Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); LLDB_LOG(log, "failed, no process available"); return SendErrorResponse(0x15); @@ -2707,22 +2686,21 @@ if (want_breakpoint) { // Try to clear the breakpoint. - const Status error = - m_debugged_process_up->RemoveBreakpoint(addr, want_hardware); + const Status error = process->RemoveBreakpoint(addr, want_hardware); if (error.Success()) return SendOKResponse(); Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); - LLDB_LOG(log, "pid {0} failed to remove breakpoint: {1}", - m_debugged_process_up->GetID(), error); + LLDB_LOG(log, "pid {0} failed to remove breakpoint: {1}", process->GetID(), + error); return SendErrorResponse(0x09); } else { // Try to clear the watchpoint. - const Status error = m_debugged_process_up->RemoveWatchpoint(addr); + const Status error = process->RemoveWatchpoint(addr); if (error.Success()) return SendOKResponse(); Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); - LLDB_LOG(log, "pid {0} failed to remove watchpoint: {1}", - m_debugged_process_up->GetID(), error); + LLDB_LOG(log, "pid {0} failed to remove watchpoint: {1}", process->GetID(), + error); return SendErrorResponse(0x09); } } @@ -2732,8 +2710,8 @@ Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); // Ensure we have a process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { + auto *process = GetCurrentProcess(true); + if (!process) { LLDB_LOGF( log, "GDBRemoteCommunicationServerLLGS::%s failed, no process available", @@ -2751,7 +2729,7 @@ // Double check that we have such a thread. // TODO investigate: on MacOSX we might need to do an UpdateThreads () here. - NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid); + NativeThreadProtocol *thread = process->GetThreadByID(tid); if (!thread) return SendErrorResponse(0x33); @@ -2764,13 +2742,12 @@ // All other threads stop while we're single stepping a thread. actions.SetDefaultThreadActionIfNeeded(eStateStopped, 0); - Status error = m_debugged_process_up->Resume(actions); + Status error = process->Resume(actions); if (error.Fail()) { LLDB_LOGF(log, "GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " tid %" PRIu64 " Resume() failed with error: %s", - __FUNCTION__, m_debugged_process_up->GetID(), tid, - error.AsCString()); + __FUNCTION__, process->GetID(), tid, error.AsCString()); return SendErrorResponse(0x49); } @@ -2885,22 +2862,22 @@ GDBRemoteCommunicationServerLLGS::ReadXferObject(llvm::StringRef object, llvm::StringRef annex) { // Make sure we have a valid process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { + auto *process = GetCurrentProcess(false); + if (!process) { return llvm::createStringError(llvm::inconvertibleErrorCode(), "No process available"); } if (object == "auxv") { // Grab the auxv data. - auto buffer_or_error = m_debugged_process_up->GetAuxvData(); + auto buffer_or_error = process->GetAuxvData(); if (!buffer_or_error) return llvm::errorCodeToError(buffer_or_error.getError()); return std::move(*buffer_or_error); } if (object == "libraries-svr4") { - auto library_list = m_debugged_process_up->GetLoadedSVR4Libraries(); + auto library_list = process->GetLoadedSVR4Libraries(); if (!library_list) return library_list.takeError(); @@ -3300,19 +3277,19 @@ Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); // Ensure we have a debugged process. - if (!m_debugged_process_up || - (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) + auto *process = GetCurrentProcess(false); + if (!process) return SendErrorResponse(50); - LLDB_LOG(log, "preparing packet for pid {0}", m_debugged_process_up->GetID()); + LLDB_LOG(log, "preparing packet for pid {0}", process->GetID()); StreamString response; const bool threads_with_valid_stop_info_only = false; llvm::Expected threads_info = GetJSONThreadsInfo( - *m_debugged_process_up, threads_with_valid_stop_info_only); + *process, threads_with_valid_stop_info_only); if (!threads_info) { LLDB_LOG_ERROR(log, threads_info.takeError(), "failed to prepare a packet for pid {1}: {0}", - m_debugged_process_up->GetID()); + process->GetID()); return SendErrorResponse(52); } @@ -3326,8 +3303,8 @@ GDBRemoteCommunicationServerLLGS::Handle_qWatchpointSupportInfo( StringExtractorGDBRemote &packet) { // Fail if we don't have a current process. - if (!m_debugged_process_up || - m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID) + auto *process = GetCurrentProcess(false); + if (!process) return SendErrorResponse(68); packet.SetFilePos(strlen("qWatchpointSupportInfo")); @@ -3336,7 +3313,7 @@ if (packet.GetChar() != ':') return SendErrorResponse(67); - auto hw_debug_cap = m_debugged_process_up->GetHardwareDebugSupportInfo(); + auto hw_debug_cap = process->GetHardwareDebugSupportInfo(); StreamGDBRemote response; if (hw_debug_cap == llvm::None) @@ -3351,8 +3328,8 @@ GDBRemoteCommunicationServerLLGS::Handle_qFileLoadAddress( StringExtractorGDBRemote &packet) { // Fail if we don't have a current process. - if (!m_debugged_process_up || - m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID) + auto *process = GetCurrentProcess(false); + if (!process) return SendErrorResponse(67); packet.SetFilePos(strlen("qFileLoadAddress:")); @@ -3363,8 +3340,7 @@ packet.GetHexByteString(file_name); lldb::addr_t file_load_address = LLDB_INVALID_ADDRESS; - Status error = - m_debugged_process_up->GetFileLoadAddress(file_name, file_load_address); + Status error = process->GetFileLoadAddress(file_name, file_load_address); if (error.Fail()) return SendErrorResponse(69); @@ -3400,10 +3376,11 @@ } // Fail if we don't have a current process. - if (!m_debugged_process_up) + auto *process = GetCurrentProcess(false); + if (!process) return SendErrorResponse(68); - Status error = m_debugged_process_up->IgnoreSignals(signals); + Status error = process->IgnoreSignals(signals); if (error.Fail()) return SendErrorResponse(69); @@ -3438,8 +3415,8 @@ NativeThreadProtocol *GDBRemoteCommunicationServerLLGS::GetThreadFromSuffix( StringExtractorGDBRemote &packet) { // We have no thread if we don't have a process. - if (!m_debugged_process_up || - m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID) + auto *process = GetCurrentProcess(false); + if (!process) return nullptr; // If the client hasn't asked for thread suffix support, there will not be a @@ -3450,9 +3427,9 @@ return nullptr; else if (current_tid == 0) { // Pick a thread. - return m_debugged_process_up->GetThreadAtIndex(0); + return process->GetThreadAtIndex(0); } else - return m_debugged_process_up->GetThreadByID(current_tid); + return process->GetThreadByID(current_tid); } Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); @@ -3482,7 +3459,7 @@ packet.SetFilePos(packet.GetFilePos() + strlen("thread:")); const lldb::tid_t tid = packet.GetHexMaxU64(false, 0); if (tid != 0) - return m_debugged_process_up->GetThreadByID(tid); + return process->GetThreadByID(tid); return nullptr; } @@ -3492,9 +3469,10 @@ // Use whatever the debug process says is the current thread id since the // protocol either didn't specify or specified we want any/all threads // marked as the current thread. - if (!m_debugged_process_up) + auto *process = GetCurrentProcess(false); + if (!process) return LLDB_INVALID_THREAD_ID; - return m_debugged_process_up->GetCurrentThreadID(); + return process->GetCurrentThreadID(); } // Use the specific current thread id set by the gdb remote protocol. return m_current_tid; @@ -3515,10 +3493,10 @@ FileSpec GDBRemoteCommunicationServerLLGS::FindModuleFile(const std::string &module_path, const ArchSpec &arch) { - if (m_debugged_process_up) { + auto *process = GetCurrentProcess(false); + if (process) { FileSpec file_spec; - if (m_debugged_process_up - ->GetLoadedModuleFileSpec(module_path.c_str(), file_spec) + if (process->GetLoadedModuleFileSpec(module_path.c_str(), file_spec) .Success()) { if (FileSystem::Instance().Exists(file_spec)) return file_spec; @@ -3555,11 +3533,12 @@ llvm::Expected GDBRemoteCommunicationServerLLGS::ReadTid(StringExtractorGDBRemote &packet, - bool allow_all) { - assert(m_debugged_process_up); - assert(m_debugged_process_up->GetID() != LLDB_INVALID_PROCESS_ID); + bool run, bool allow_all) { + auto *process = GetCurrentProcess(run); + assert(process); + assert(process->GetID() != LLDB_INVALID_PROCESS_ID); - auto pid_tid = packet.GetPidTid(m_debugged_process_up->GetID()); + auto pid_tid = packet.GetPidTid(process->GetID()); if (!pid_tid) return llvm::make_error(inconvertibleErrorCode(), "Malformed thread-id"); @@ -3578,7 +3557,7 @@ llvm::formatv("TID value {0} not allowed", tid == 0 ? 0 : -1)); if (pid != StringExtractorGDBRemote::AllProcesses) { - if (pid != m_debugged_process_up->GetID()) + if (pid != process->GetID()) return llvm::make_error( inconvertibleErrorCode(), llvm::formatv("PID {0} not debugged", pid)); } @@ -3642,3 +3621,13 @@ LLDB_LOG_ERROR(log, std::move(error), "Enabling protocol extensions failed: {0}"); } + +NativeProcessProtocol * +GDBRemoteCommunicationServerLLGS::GetCurrentProcess(bool run) const { + // Fail if we don't have a current process. + if (!m_debugged_process_up || + (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) + return nullptr; + + return m_debugged_process_up.get(); +}