1 //===-- GDBRemoteCommunication.cpp ------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "GDBRemoteCommunication.h"
18 // Other libraries and framework includes
19 #include "lldb/Core/Log.h"
20 #include "lldb/Core/StreamFile.h"
21 #include "lldb/Core/StreamString.h"
22 #include "lldb/Host/FileSpec.h"
23 #include "lldb/Host/Host.h"
24 #include "lldb/Host/TimeValue.h"
25 #include "lldb/Target/Process.h"
28 #include "ProcessGDBRemoteLog.h"
30 #define DEBUGSERVER_BASENAME "debugserver"
33 using namespace lldb_private;
35 GDBRemoteCommunication::History::History (uint32_t size) :
38 m_total_packet_count (0),
39 m_dumped_to_log (false)
41 m_packets.resize(size);
44 GDBRemoteCommunication::History::~History ()
49 GDBRemoteCommunication::History::AddPacket (char packet_char,
51 uint32_t bytes_transmitted)
53 const size_t size = m_packets.size();
56 const uint32_t idx = GetNextIndex();
57 m_packets[idx].packet.assign (1, packet_char);
58 m_packets[idx].type = type;
59 m_packets[idx].bytes_transmitted = bytes_transmitted;
60 m_packets[idx].packet_idx = m_total_packet_count;
61 m_packets[idx].tid = Host::GetCurrentThreadID();
66 GDBRemoteCommunication::History::AddPacket (const std::string &src,
69 uint32_t bytes_transmitted)
71 const size_t size = m_packets.size();
74 const uint32_t idx = GetNextIndex();
75 m_packets[idx].packet.assign (src, 0, src_len);
76 m_packets[idx].type = type;
77 m_packets[idx].bytes_transmitted = bytes_transmitted;
78 m_packets[idx].packet_idx = m_total_packet_count;
79 m_packets[idx].tid = Host::GetCurrentThreadID();
84 GDBRemoteCommunication::History::Dump (lldb_private::Stream &strm) const
86 const uint32_t size = GetNumPacketsInHistory ();
87 const uint32_t first_idx = GetFirstSavedPacketIndex ();
88 const uint32_t stop_idx = m_curr_idx + size;
89 for (uint32_t i = first_idx; i < stop_idx; ++i)
91 const uint32_t idx = NormalizeIndex (i);
92 const Entry &entry = m_packets[idx];
93 if (entry.type == ePacketTypeInvalid || entry.packet.empty())
95 strm.Printf ("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s\n",
98 entry.bytes_transmitted,
99 (entry.type == ePacketTypeSend) ? "send" : "read",
100 entry.packet.c_str());
105 GDBRemoteCommunication::History::Dump (lldb_private::Log *log) const
107 if (log && !m_dumped_to_log)
109 m_dumped_to_log = true;
110 const uint32_t size = GetNumPacketsInHistory ();
111 const uint32_t first_idx = GetFirstSavedPacketIndex ();
112 const uint32_t stop_idx = m_curr_idx + size;
113 for (uint32_t i = first_idx; i < stop_idx; ++i)
115 const uint32_t idx = NormalizeIndex (i);
116 const Entry &entry = m_packets[idx];
117 if (entry.type == ePacketTypeInvalid || entry.packet.empty())
119 log->Printf ("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s",
122 entry.bytes_transmitted,
123 (entry.type == ePacketTypeSend) ? "send" : "read",
124 entry.packet.c_str());
129 //----------------------------------------------------------------------
130 // GDBRemoteCommunication constructor
131 //----------------------------------------------------------------------
132 GDBRemoteCommunication::GDBRemoteCommunication(const char *comm_name,
133 const char *listener_name,
135 Communication(comm_name),
136 m_packet_timeout (1),
137 m_sequence_mutex (Mutex::eMutexTypeRecursive),
138 m_public_is_running (false),
139 m_private_is_running (false),
142 m_is_platform (is_platform)
146 //----------------------------------------------------------------------
148 //----------------------------------------------------------------------
149 GDBRemoteCommunication::~GDBRemoteCommunication()
158 GDBRemoteCommunication::CalculcateChecksum (const char *payload, size_t payload_length)
162 for (size_t i = 0; i < payload_length; ++i)
163 checksum += payload[i];
165 return checksum & 255;
169 GDBRemoteCommunication::SendAck ()
171 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
172 ConnectionStatus status = eConnectionStatusSuccess;
174 const size_t bytes_written = Write (&ch, 1, status, NULL);
176 log->Printf ("<%4zu> send packet: %c", bytes_written, ch);
177 m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written);
178 return bytes_written;
182 GDBRemoteCommunication::SendNack ()
184 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
185 ConnectionStatus status = eConnectionStatusSuccess;
187 const size_t bytes_written = Write (&ch, 1, status, NULL);
189 log->Printf ("<%4zu> send packet: %c", bytes_written, ch);
190 m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written);
191 return bytes_written;
195 GDBRemoteCommunication::SendPacket (const char *payload, size_t payload_length)
197 Mutex::Locker locker(m_sequence_mutex);
198 return SendPacketNoLock (payload, payload_length);
202 GDBRemoteCommunication::SendPacketNoLock (const char *payload, size_t payload_length)
206 StreamString packet(0, 4, eByteOrderBig);
209 packet.Write (payload, payload_length);
211 packet.PutHex8(CalculcateChecksum (payload, payload_length));
213 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
214 ConnectionStatus status = eConnectionStatusSuccess;
215 size_t bytes_written = Write (packet.GetData(), packet.GetSize(), status, NULL);
218 // If logging was just enabled and we have history, then dump out what
219 // we have to the log so we get the historical context. The Dump() call that
220 // logs all of the packet will set a boolean so that we don't dump this more
222 if (!m_history.DidDumpToLog ())
223 m_history.Dump (log);
225 log->Printf ("<%4zu> send packet: %.*s", bytes_written, (int)packet.GetSize(), packet.GetData());
228 m_history.AddPacket (packet.GetString(), packet.GetSize(), History::ePacketTypeSend, bytes_written);
231 if (bytes_written == packet.GetSize())
235 if (GetAck () != '+')
238 log->Printf("get ack failed...");
246 log->Printf ("error: failed to send packet: %.*s", (int)packet.GetSize(), packet.GetData());
248 return bytes_written;
254 GDBRemoteCommunication::GetAck ()
256 StringExtractorGDBRemote packet;
257 if (WaitForPacketWithTimeoutMicroSecondsNoLock (packet, GetPacketTimeoutInMicroSeconds ()) == 1)
258 return packet.GetChar();
263 GDBRemoteCommunication::GetSequenceMutex (Mutex::Locker& locker, const char *failure_message)
266 return locker.TryLock (m_sequence_mutex, failure_message);
268 locker.Lock (m_sequence_mutex);
274 GDBRemoteCommunication::WaitForNotRunningPrivate (const TimeValue *timeout_ptr)
276 return m_private_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL);
280 GDBRemoteCommunication::WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &packet, uint32_t timeout_usec)
282 uint8_t buffer[8192];
285 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS | GDBR_LOG_VERBOSE));
287 // Check for a packet from our cache first without trying any reading...
288 if (CheckForPacket (NULL, 0, packet))
289 return packet.GetStringRef().size();
291 bool timed_out = false;
292 while (IsConnected() && !timed_out)
294 lldb::ConnectionStatus status = eConnectionStatusNoConnection;
295 size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error);
298 log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %" PRIu64,
301 Communication::ConnectionStatusAsCString (status),
303 (uint64_t)bytes_read);
307 if (CheckForPacket (buffer, bytes_read, packet))
308 return packet.GetStringRef().size();
314 case eConnectionStatusTimedOut:
317 case eConnectionStatusSuccess:
318 //printf ("status = success but error = %s\n", error.AsCString("<invalid>"));
321 case eConnectionStatusEndOfFile:
322 case eConnectionStatusNoConnection:
323 case eConnectionStatusLostConnection:
324 case eConnectionStatusError:
335 GDBRemoteCommunication::CheckForPacket (const uint8_t *src, size_t src_len, StringExtractorGDBRemote &packet)
337 // Put the packet data into the buffer in a thread safe fashion
338 Mutex::Locker locker(m_bytes_mutex);
340 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
342 if (src && src_len > 0)
344 if (log && log->GetVerbose())
347 log->Printf ("GDBRemoteCommunication::%s adding %u bytes: %.*s",
353 m_bytes.append ((const char *)src, src_len);
356 // Parse up the packets into gdb remote packets
357 if (!m_bytes.empty())
359 // end_idx must be one past the last valid packet byte. Start
360 // it off with an invalid value that is the same as the current
362 size_t content_start = 0;
363 size_t content_length = 0;
364 size_t total_length = 0;
365 size_t checksum_idx = std::string::npos;
369 case '+': // Look for ack
370 case '-': // Look for cancel
371 case '\x03': // ^C to halt target
372 content_length = total_length = 1; // The command is one byte long...
376 // Look for a standard gdb packet?
378 size_t hash_pos = m_bytes.find('#');
379 if (hash_pos != std::string::npos)
381 if (hash_pos + 2 < m_bytes.size())
383 checksum_idx = hash_pos + 1;
384 // Skip the dollar sign
386 // Don't include the # in the content or the $ in the content length
387 content_length = hash_pos - 1;
389 total_length = hash_pos + 3; // Skip the # and the two hex checksum bytes
393 // Checksum bytes aren't all here yet
394 content_length = std::string::npos;
402 // We have an unexpected byte and we need to flush all bad
403 // data that is in m_bytes, so we need to find the first
404 // byte that is a '+' (ACK), '-' (NACK), \x03 (CTRL+C interrupt),
405 // or '$' character (start of packet header) or of course,
406 // the end of the data in m_bytes...
407 const size_t bytes_len = m_bytes.size();
410 for (idx = 1; !done && idx < bytes_len; ++idx)
412 switch (m_bytes[idx])
426 log->Printf ("GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'",
427 __FUNCTION__, idx, idx, m_bytes.c_str());
428 m_bytes.erase(0, idx);
433 if (content_length == std::string::npos)
438 else if (total_length > 0)
441 // We have a valid packet...
442 assert (content_length <= m_bytes.size());
443 assert (total_length <= m_bytes.size());
444 assert (content_length <= total_length);
447 std::string &packet_str = packet.GetStringRef();
452 // If logging was just enabled and we have history, then dump out what
453 // we have to the log so we get the historical context. The Dump() call that
454 // logs all of the packet will set a boolean so that we don't dump this more
456 if (!m_history.DidDumpToLog ())
457 m_history.Dump (log);
459 log->Printf ("<%4zu> read packet: %.*s", total_length, (int)(total_length), m_bytes.c_str());
462 m_history.AddPacket (m_bytes.c_str(), total_length, History::ePacketTypeRecv, total_length);
464 packet_str.assign (m_bytes, content_start, content_length);
466 if (m_bytes[0] == '$')
468 assert (checksum_idx < m_bytes.size());
469 if (::isxdigit (m_bytes[checksum_idx+0]) ||
470 ::isxdigit (m_bytes[checksum_idx+1]))
474 const char *packet_checksum_cstr = &m_bytes[checksum_idx];
475 char packet_checksum = strtol (packet_checksum_cstr, NULL, 16);
476 char actual_checksum = CalculcateChecksum (packet_str.c_str(), packet_str.size());
477 success = packet_checksum == actual_checksum;
481 log->Printf ("error: checksum mismatch: %.*s expected 0x%2.2x, got 0x%2.2x",
484 (uint8_t)packet_checksum,
485 (uint8_t)actual_checksum);
487 // Send the ack or nack if needed
498 log->Printf ("error: invalid checksum in packet: '%s'\n", m_bytes.c_str());
502 m_bytes.erase(0, total_length);
503 packet.SetFilePos(0);
512 GDBRemoteCommunication::StartDebugserverProcess (const char *debugserver_url,
513 const char *unix_socket_name, // For handshaking
514 lldb_private::ProcessLaunchInfo &launch_info)
517 // If we locate debugserver, keep that located version around
518 static FileSpec g_debugserver_file_spec;
520 // This function will fill in the launch information for the debugserver
521 // instance that gets launched.
524 char debugserver_path[PATH_MAX];
525 FileSpec &debugserver_file_spec = launch_info.GetExecutableFile();
527 // Always check to see if we have an environment override for the path
528 // to the debugserver to use and use it if we do.
529 const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH");
530 if (env_debugserver_path)
531 debugserver_file_spec.SetFile (env_debugserver_path, false);
533 debugserver_file_spec = g_debugserver_file_spec;
534 bool debugserver_exists = debugserver_file_spec.Exists();
535 if (!debugserver_exists)
537 // The debugserver binary is in the LLDB.framework/Resources
539 if (Host::GetLLDBPath (ePathTypeSupportExecutableDir, debugserver_file_spec))
541 debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME);
542 debugserver_exists = debugserver_file_spec.Exists();
543 if (debugserver_exists)
545 g_debugserver_file_spec = debugserver_file_spec;
549 g_debugserver_file_spec.Clear();
550 debugserver_file_spec.Clear();
555 if (debugserver_exists)
557 debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path));
559 Args &debugserver_args = launch_info.GetArguments();
560 debugserver_args.Clear();
561 char arg_cstr[PATH_MAX];
563 // Start args with "debugserver /file/path -r --"
564 debugserver_args.AppendArgument(debugserver_path);
565 debugserver_args.AppendArgument(debugserver_url);
566 // use native registers, not the GDB registers
567 debugserver_args.AppendArgument("--native-regs");
568 // make debugserver run in its own session so signals generated by
569 // special terminal key sequences (^C) don't affect debugserver
570 debugserver_args.AppendArgument("--setsid");
572 if (unix_socket_name && unix_socket_name[0])
574 debugserver_args.AppendArgument("--unix-socket");
575 debugserver_args.AppendArgument(unix_socket_name);
578 const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE");
579 if (env_debugserver_log_file)
581 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file);
582 debugserver_args.AppendArgument(arg_cstr);
585 const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS");
586 if (env_debugserver_log_flags)
588 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags);
589 debugserver_args.AppendArgument(arg_cstr);
591 // debugserver_args.AppendArgument("--log-file=/tmp/debugserver.txt");
592 // debugserver_args.AppendArgument("--log-flags=0x802e0e");
594 // We currently send down all arguments, attach pids, or attach
595 // process names in dedicated GDB server packets, so we don't need
596 // to pass them as arguments. This is currently because of all the
597 // things we need to setup prior to launching: the environment,
598 // current working dir, file actions, etc.
600 // Now append the program arguments
603 // Terminate the debugserver args so we can now append the inferior args
604 debugserver_args.AppendArgument("--");
606 for (int i = 0; inferior_argv[i] != NULL; ++i)
607 debugserver_args.AppendArgument (inferior_argv[i]);
609 else if (attach_pid != LLDB_INVALID_PROCESS_ID)
611 ::snprintf (arg_cstr, sizeof(arg_cstr), "--attach=%u", attach_pid);
612 debugserver_args.AppendArgument (arg_cstr);
614 else if (attach_name && attach_name[0])
617 debugserver_args.AppendArgument ("--waitfor");
619 debugserver_args.AppendArgument ("--attach");
620 debugserver_args.AppendArgument (attach_name);
624 // Close STDIN, STDOUT and STDERR. We might need to redirect them
625 // to "/dev/null" if we run into any problems.
626 // launch_info.AppendCloseFileAction (STDIN_FILENO);
627 // launch_info.AppendCloseFileAction (STDOUT_FILENO);
628 // launch_info.AppendCloseFileAction (STDERR_FILENO);
630 error = Host::LaunchProcess(launch_info);
634 error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME );
640 GDBRemoteCommunication::DumpHistory(Stream &strm)
642 m_history.Dump (strm);