hanchenye-llvm-project/lldb/source/Utility
Greg Clayton 830c81d511 Fixed an issue that could cause debugserver to return two stop reply packets ($T packets) for one \x03 interrupt. The problem was that when a \x03 byte is sent to debugserver while the process is running, and up calling:
rnb_err_t
RNBRemote::HandlePacket_stop_process (const char *p)
{
    if (!DNBProcessInterrupt(m_ctx.ProcessID()))
        HandlePacket_last_signal (NULL);
    return rnb_success;
}

In the call to DNBProcessInterrupt we did:

nub_bool_t
DNBProcessInterrupt(nub_process_t pid)
{
    MachProcessSP procSP;
    if (GetProcessSP (pid, procSP))
        return procSP->Interrupt();
    return false;
}

This would always return false. It would cause HandlePacket_stop_process to always call "HandlePacket_last_signal (NULL);" which would send an extra stop reply packet _if_ the process is stopped. On a machine with enough cores, it would call DNBProcessInterrupt(...) and then HandlePacket_last_signal(NULL) so quickly that it will never send out an extra stop reply packet. But if the machine is slow enough or doesn't have enough cores, it could cause the call to HandlePacket_last_signal() to actually succeed and send an extra stop reply packet. This would cause problems up in GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse() where it would get the first stop reply packet and then possibly return or execute an async packet. If it returned, then the next packet that was sent will get the second stop reply as its response. If it executes an async packet, the async packet will get the wrong response.

To fix this I did the following:
1 - in debugserver, I fixed "bool MachProcess::Interrupt()" to return true if it sends the signal so we avoid sending the stop reply twice on slower machines
2 - Added a log line to RNBRemote::HandlePacket_stop_process() to say if we ever send an extra stop reply so we will see this in the darwin console output if this does happen
3 - Added response validators to StringExtractorGDBRemote so that we can verify some responses to some packets. 
4 - Added validators to packets that often follow stop reply packets like the "m" packet for memory reads, JSON packets since "jThreadsInfo" is often sent immediately following a stop reply.
5 - Modified GDBRemoteCommunicationClient::SendPacketAndWaitForResponseNoLock() to validate responses. Any "StringExtractorGDBRemote &response" that contains a valid response verifier will verify the response and keep looking for correct responses up to 3 times. This will help us get back on track if we do get extra stop replies. If a StringExtractorGDBRemote does not have a response validator, it will accept any packet in response.
6 - In GDBRemoteCommunicationClient::SendPacketAndWaitForResponse we copy the response validator from the "response" argument over into m_async_response so that if we send the packet by interrupting the running process, we can validate the response we actually get in GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse()
7 - Modified GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse() to always check for an extra stop reply packet for 100ms when the process is interrupted. We were already doing this because we might interrupt a process with a \x03 packet, yet the process was in the process of stopping due to another reason. This race condition could cause an extra stop reply packet because the GDB remote protocol says if a \x03 packet is sent while the process is stopped, we should send a stop reply packet back. Now we always check for an extra stop reply packet when we manually interrupt a process.

The issue was showing up when our IDE would attempt to set a breakpoint while the process is running and this would happen:

--> \x03
<-- $T<stop reply 1>
--> z0,AAAAA,BB (set breakpoint)
<-- $T<stop reply 1> (incorrect extra stop reply packet)
--> c
<-- OK (response from z0 packet)

Now all packet traffic was off by one response. Since we now have a validator on the response for "z" packets, we do this:

--> \x03
<-- $T<stop reply 1>
--> z0,AAAAA,BB (set breakpoint)
<-- $T<stop reply 1> (Ignore this because this can't be the response to z0 packets)
<-- OK -- (we are back on track as this is a valid response to z0)
...

As time goes on we should add more packet validators.

<rdar://problem/22859505>

llvm-svn: 265086
2016-04-01 00:41:29 +00:00
..
ARM64_DWARF_Registers.cpp
ARM64_DWARF_Registers.h
ARM64_ehframe_Registers.h Clean up register naming conventions inside lldb. 2015-09-15 23:20:34 +00:00
ARM_DWARF_Registers.cpp
ARM_DWARF_Registers.h
ARM_ehframe_Registers.h Clean up register naming conventions inside lldb. 2015-09-15 23:20:34 +00:00
CMakeLists.txt Add a new task pool class to LLDB 2015-10-20 12:42:05 +00:00
ConvertEnum.cpp Add support for handling absolute symbols in ELF 2016-02-25 12:23:43 +00:00
JSON.cpp Fix all of the unannotated switch cases to annotate the fall through or do the right thing and break. 2016-02-26 01:20:20 +00:00
KQueue.cpp
KQueue.h
LLDBAssert.cpp
ModuleCache.cpp Add modules downloaded by ModuleCache to the global ModuleList 2015-12-10 17:08:23 +00:00
ModuleCache.h Download symbol file for .oat files on android 2015-08-12 11:10:25 +00:00
NameMatches.cpp
PseudoTerminal.cpp
Range.cpp
RegisterNumber.cpp
SharingPtr.cpp
StringExtractor.cpp Add comment in StringExtractor::GetHexU8 2015-09-25 18:23:50 +00:00
StringExtractorGDBRemote.cpp Fixed an issue that could cause debugserver to return two stop reply packets ($T packets) for one \x03 interrupt. The problem was that when a \x03 byte is sent to debugserver while the process is running, and up calling: 2016-04-01 00:41:29 +00:00
StringExtractorGDBRemote.h Fixed an issue that could cause debugserver to return two stop reply packets ($T packets) for one \x03 interrupt. The problem was that when a \x03 byte is sent to debugserver while the process is running, and up calling: 2016-04-01 00:41:29 +00:00
StringLexer.cpp
TaskPool.cpp Add a new task pool class to LLDB 2015-10-20 12:42:05 +00:00
TimeSpecTimeout.cpp
TimeSpecTimeout.h
UriParser.cpp Simplify find_first_of & find_last_of on single char. 2015-09-01 23:57:17 +00:00
UriParser.h Make UriParser to support [$HOSTNAME] notation. 2015-08-20 23:09:34 +00:00
UuidCompatibility.h