Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1 | //===-- GDBRemoteCommunicationServer.cpp ------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Sylvestre Ledru | d28b993 | 2013-09-28 15:23:41 +0000 | [diff] [blame] | 10 | #include <errno.h> |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 11 | |
| 12 | #include "GDBRemoteCommunicationServer.h" |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 13 | #include "lldb/Core/StreamGDBRemote.h" |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 14 | |
| 15 | // C Includes |
| 16 | // C++ Includes |
| 17 | // Other libraries and framework includes |
| 18 | #include "llvm/ADT/Triple.h" |
| 19 | #include "lldb/Interpreter/Args.h" |
| 20 | #include "lldb/Core/ConnectionFileDescriptor.h" |
| 21 | #include "lldb/Core/Log.h" |
| 22 | #include "lldb/Core/State.h" |
| 23 | #include "lldb/Core/StreamString.h" |
Enrico Granata | f2bbf71 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 24 | #include "lldb/Host/Endian.h" |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 25 | #include "lldb/Host/File.h" |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 26 | #include "lldb/Host/Host.h" |
| 27 | #include "lldb/Host/TimeValue.h" |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 28 | #include "lldb/Target/Process.h" |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 29 | |
| 30 | // Project includes |
| 31 | #include "Utility/StringExtractorGDBRemote.h" |
| 32 | #include "ProcessGDBRemote.h" |
| 33 | #include "ProcessGDBRemoteLog.h" |
| 34 | |
| 35 | using namespace lldb; |
| 36 | using namespace lldb_private; |
| 37 | |
| 38 | //---------------------------------------------------------------------- |
| 39 | // GDBRemoteCommunicationServer constructor |
| 40 | //---------------------------------------------------------------------- |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 41 | GDBRemoteCommunicationServer::GDBRemoteCommunicationServer(bool is_platform) : |
| 42 | GDBRemoteCommunication ("gdb-remote.server", "gdb-remote.server.rx_packet", is_platform), |
| 43 | m_async_thread (LLDB_INVALID_HOST_THREAD), |
| 44 | m_process_launch_info (), |
| 45 | m_process_launch_error (), |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 46 | m_spawned_pids (), |
| 47 | m_spawned_pids_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 48 | m_proc_infos (), |
| 49 | m_proc_infos_index (0), |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 50 | m_port_map (), |
| 51 | m_port_offset(0) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | |
| 55 | //---------------------------------------------------------------------- |
| 56 | // Destructor |
| 57 | //---------------------------------------------------------------------- |
| 58 | GDBRemoteCommunicationServer::~GDBRemoteCommunicationServer() |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | |
| 63 | //void * |
| 64 | //GDBRemoteCommunicationServer::AsyncThread (void *arg) |
| 65 | //{ |
| 66 | // GDBRemoteCommunicationServer *server = (GDBRemoteCommunicationServer*) arg; |
| 67 | // |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 68 | // Log *log;// (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 69 | // if (log) |
| 70 | // log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, arg, process->GetID()); |
| 71 | // |
| 72 | // StringExtractorGDBRemote packet; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 73 | // |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 74 | // while () |
| 75 | // { |
| 76 | // if (packet. |
| 77 | // } |
| 78 | // |
| 79 | // if (log) |
| 80 | // log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %i) thread exiting...", __FUNCTION__, arg, process->GetID()); |
| 81 | // |
| 82 | // process->m_async_thread = LLDB_INVALID_HOST_THREAD; |
| 83 | // return NULL; |
| 84 | //} |
| 85 | // |
| 86 | bool |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 87 | GDBRemoteCommunicationServer::GetPacketAndSendResponse (uint32_t timeout_usec, |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 88 | Error &error, |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 89 | bool &interrupt, |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 90 | bool &quit) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 91 | { |
| 92 | StringExtractorGDBRemote packet; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 93 | PacketResult packet_result = WaitForPacketWithTimeoutMicroSecondsNoLock (packet, timeout_usec); |
| 94 | if (packet_result == PacketResult::Success) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 95 | { |
| 96 | const StringExtractorGDBRemote::ServerPacketType packet_type = packet.GetServerPacketType (); |
| 97 | switch (packet_type) |
| 98 | { |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 99 | case StringExtractorGDBRemote::eServerPacketType_nack: |
| 100 | case StringExtractorGDBRemote::eServerPacketType_ack: |
| 101 | break; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 102 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 103 | case StringExtractorGDBRemote::eServerPacketType_invalid: |
| 104 | error.SetErrorString("invalid packet"); |
| 105 | quit = true; |
| 106 | break; |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 107 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 108 | case StringExtractorGDBRemote::eServerPacketType_interrupt: |
| 109 | error.SetErrorString("interrupt received"); |
| 110 | interrupt = true; |
| 111 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 112 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 113 | default: |
| 114 | case StringExtractorGDBRemote::eServerPacketType_unimplemented: |
| 115 | packet_result = SendUnimplementedResponse (packet.GetStringRef().c_str()); |
| 116 | break; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 117 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 118 | case StringExtractorGDBRemote::eServerPacketType_A: |
| 119 | packet_result = Handle_A (packet); |
| 120 | break; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 121 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 122 | case StringExtractorGDBRemote::eServerPacketType_qfProcessInfo: |
| 123 | packet_result = Handle_qfProcessInfo (packet); |
| 124 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 125 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 126 | case StringExtractorGDBRemote::eServerPacketType_qsProcessInfo: |
| 127 | packet_result = Handle_qsProcessInfo (packet); |
| 128 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 129 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 130 | case StringExtractorGDBRemote::eServerPacketType_qC: |
| 131 | packet_result = Handle_qC (packet); |
| 132 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 133 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 134 | case StringExtractorGDBRemote::eServerPacketType_qHostInfo: |
| 135 | packet_result = Handle_qHostInfo (packet); |
| 136 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 137 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 138 | case StringExtractorGDBRemote::eServerPacketType_qLaunchGDBServer: |
| 139 | packet_result = Handle_qLaunchGDBServer (packet); |
| 140 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 141 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 142 | case StringExtractorGDBRemote::eServerPacketType_qKillSpawnedProcess: |
| 143 | packet_result = Handle_qKillSpawnedProcess (packet); |
| 144 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 145 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 146 | case StringExtractorGDBRemote::eServerPacketType_qLaunchSuccess: |
| 147 | packet_result = Handle_qLaunchSuccess (packet); |
| 148 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 149 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 150 | case StringExtractorGDBRemote::eServerPacketType_qGroupName: |
| 151 | packet_result = Handle_qGroupName (packet); |
| 152 | break; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 153 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 154 | case StringExtractorGDBRemote::eServerPacketType_qProcessInfoPID: |
| 155 | packet_result = Handle_qProcessInfoPID (packet); |
| 156 | break; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 157 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 158 | case StringExtractorGDBRemote::eServerPacketType_qSpeedTest: |
| 159 | packet_result = Handle_qSpeedTest (packet); |
| 160 | break; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 161 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 162 | case StringExtractorGDBRemote::eServerPacketType_qUserName: |
| 163 | packet_result = Handle_qUserName (packet); |
| 164 | break; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 165 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 166 | case StringExtractorGDBRemote::eServerPacketType_qGetWorkingDir: |
| 167 | packet_result = Handle_qGetWorkingDir(packet); |
| 168 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 169 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 170 | case StringExtractorGDBRemote::eServerPacketType_QEnvironment: |
| 171 | packet_result = Handle_QEnvironment (packet); |
| 172 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 173 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 174 | case StringExtractorGDBRemote::eServerPacketType_QLaunchArch: |
| 175 | packet_result = Handle_QLaunchArch (packet); |
| 176 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 177 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 178 | case StringExtractorGDBRemote::eServerPacketType_QSetDisableASLR: |
| 179 | packet_result = Handle_QSetDisableASLR (packet); |
| 180 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 181 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 182 | case StringExtractorGDBRemote::eServerPacketType_QSetSTDIN: |
| 183 | packet_result = Handle_QSetSTDIN (packet); |
| 184 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 185 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 186 | case StringExtractorGDBRemote::eServerPacketType_QSetSTDOUT: |
| 187 | packet_result = Handle_QSetSTDOUT (packet); |
| 188 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 189 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 190 | case StringExtractorGDBRemote::eServerPacketType_QSetSTDERR: |
| 191 | packet_result = Handle_QSetSTDERR (packet); |
| 192 | break; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 193 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 194 | case StringExtractorGDBRemote::eServerPacketType_QSetWorkingDir: |
| 195 | packet_result = Handle_QSetWorkingDir (packet); |
| 196 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 197 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 198 | case StringExtractorGDBRemote::eServerPacketType_QStartNoAckMode: |
| 199 | packet_result = Handle_QStartNoAckMode (packet); |
| 200 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 201 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 202 | case StringExtractorGDBRemote::eServerPacketType_qPlatform_mkdir: |
| 203 | packet_result = Handle_qPlatform_mkdir (packet); |
| 204 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 205 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 206 | case StringExtractorGDBRemote::eServerPacketType_qPlatform_chmod: |
| 207 | packet_result = Handle_qPlatform_chmod (packet); |
| 208 | break; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 209 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 210 | case StringExtractorGDBRemote::eServerPacketType_qPlatform_shell: |
| 211 | packet_result = Handle_qPlatform_shell (packet); |
| 212 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 213 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 214 | case StringExtractorGDBRemote::eServerPacketType_vFile_open: |
| 215 | packet_result = Handle_vFile_Open (packet); |
| 216 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 217 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 218 | case StringExtractorGDBRemote::eServerPacketType_vFile_close: |
| 219 | packet_result = Handle_vFile_Close (packet); |
| 220 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 221 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 222 | case StringExtractorGDBRemote::eServerPacketType_vFile_pread: |
| 223 | packet_result = Handle_vFile_pRead (packet); |
| 224 | break; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 225 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 226 | case StringExtractorGDBRemote::eServerPacketType_vFile_pwrite: |
| 227 | packet_result = Handle_vFile_pWrite (packet); |
| 228 | break; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 229 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 230 | case StringExtractorGDBRemote::eServerPacketType_vFile_size: |
| 231 | packet_result = Handle_vFile_Size (packet); |
| 232 | break; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 233 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 234 | case StringExtractorGDBRemote::eServerPacketType_vFile_mode: |
| 235 | packet_result = Handle_vFile_Mode (packet); |
| 236 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 237 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 238 | case StringExtractorGDBRemote::eServerPacketType_vFile_exists: |
| 239 | packet_result = Handle_vFile_Exists (packet); |
| 240 | break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 241 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 242 | case StringExtractorGDBRemote::eServerPacketType_vFile_stat: |
| 243 | packet_result = Handle_vFile_Stat (packet); |
| 244 | break; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 245 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 246 | case StringExtractorGDBRemote::eServerPacketType_vFile_md5: |
| 247 | packet_result = Handle_vFile_MD5 (packet); |
| 248 | break; |
| 249 | |
| 250 | case StringExtractorGDBRemote::eServerPacketType_vFile_symlink: |
| 251 | packet_result = Handle_vFile_symlink (packet); |
| 252 | break; |
| 253 | |
| 254 | case StringExtractorGDBRemote::eServerPacketType_vFile_unlink: |
| 255 | packet_result = Handle_vFile_unlink (packet); |
| 256 | break; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 257 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 258 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 259 | else |
| 260 | { |
| 261 | if (!IsConnected()) |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 262 | { |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 263 | error.SetErrorString("lost connection"); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 264 | quit = true; |
| 265 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 266 | else |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 267 | { |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 268 | error.SetErrorString("timeout"); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 269 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 270 | } |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 271 | return packet_result == PacketResult::Success; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 274 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 275 | GDBRemoteCommunicationServer::SendUnimplementedResponse (const char *) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 276 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 277 | // TODO: Log the packet we aren't handling... |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 278 | return SendPacketNoLock ("", 0); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 281 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 282 | GDBRemoteCommunicationServer::SendErrorResponse (uint8_t err) |
| 283 | { |
| 284 | char packet[16]; |
| 285 | int packet_len = ::snprintf (packet, sizeof(packet), "E%2.2x", err); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 286 | assert (packet_len < (int)sizeof(packet)); |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 287 | return SendPacketNoLock (packet, packet_len); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 291 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 292 | GDBRemoteCommunicationServer::SendOKResponse () |
| 293 | { |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 294 | return SendPacketNoLock ("OK", 2); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | bool |
| 298 | GDBRemoteCommunicationServer::HandshakeWithClient(Error *error_ptr) |
| 299 | { |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 300 | return GetAck() == PacketResult::Success; |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 301 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 302 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 303 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 304 | GDBRemoteCommunicationServer::Handle_qHostInfo (StringExtractorGDBRemote &packet) |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 305 | { |
| 306 | StreamString response; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 307 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 308 | // $cputype:16777223;cpusubtype:3;ostype:Darwin;vendor:apple;endian:little;ptrsize:8;#00 |
| 309 | |
| 310 | ArchSpec host_arch (Host::GetArchitecture ()); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 311 | const llvm::Triple &host_triple = host_arch.GetTriple(); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 312 | response.PutCString("triple:"); |
| 313 | response.PutCStringAsRawHex8(host_triple.getTriple().c_str()); |
| 314 | response.Printf (";ptrsize:%u;",host_arch.GetAddressByteSize()); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 315 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 316 | uint32_t cpu = host_arch.GetMachOCPUType(); |
| 317 | uint32_t sub = host_arch.GetMachOCPUSubType(); |
| 318 | if (cpu != LLDB_INVALID_CPUTYPE) |
| 319 | response.Printf ("cputype:%u;", cpu); |
| 320 | if (sub != LLDB_INVALID_CPUTYPE) |
| 321 | response.Printf ("cpusubtype:%u;", sub); |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 322 | |
Enrico Granata | 1c5431a | 2012-07-13 23:55:22 +0000 | [diff] [blame] | 323 | if (cpu == ArchSpec::kCore_arm_any) |
Enrico Granata | f04a219 | 2012-07-13 23:18:48 +0000 | [diff] [blame] | 324 | response.Printf("watchpoint_exceptions_received:before;"); // On armv7 we use "synchronous" watchpoints which means the exception is delivered before the instruction executes. |
| 325 | else |
| 326 | response.Printf("watchpoint_exceptions_received:after;"); |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 327 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 328 | switch (lldb::endian::InlHostByteOrder()) |
| 329 | { |
| 330 | case eByteOrderBig: response.PutCString ("endian:big;"); break; |
| 331 | case eByteOrderLittle: response.PutCString ("endian:little;"); break; |
| 332 | case eByteOrderPDP: response.PutCString ("endian:pdp;"); break; |
| 333 | default: response.PutCString ("endian:unknown;"); break; |
| 334 | } |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 335 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 336 | uint32_t major = UINT32_MAX; |
| 337 | uint32_t minor = UINT32_MAX; |
| 338 | uint32_t update = UINT32_MAX; |
| 339 | if (Host::GetOSVersion (major, minor, update)) |
| 340 | { |
| 341 | if (major != UINT32_MAX) |
| 342 | { |
| 343 | response.Printf("os_version:%u", major); |
| 344 | if (minor != UINT32_MAX) |
| 345 | { |
| 346 | response.Printf(".%u", minor); |
| 347 | if (update != UINT32_MAX) |
| 348 | response.Printf(".%u", update); |
| 349 | } |
| 350 | response.PutChar(';'); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | std::string s; |
| 355 | if (Host::GetOSBuildString (s)) |
| 356 | { |
| 357 | response.PutCString ("os_build:"); |
| 358 | response.PutCStringAsRawHex8(s.c_str()); |
| 359 | response.PutChar(';'); |
| 360 | } |
| 361 | if (Host::GetOSKernelDescription (s)) |
| 362 | { |
| 363 | response.PutCString ("os_kernel:"); |
| 364 | response.PutCStringAsRawHex8(s.c_str()); |
| 365 | response.PutChar(';'); |
| 366 | } |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 367 | #if defined(__APPLE__) |
| 368 | |
| 369 | #if defined(__arm__) |
| 370 | // For iOS devices, we are connected through a USB Mux so we never pretend |
| 371 | // to actually have a hostname as far as the remote lldb that is connecting |
| 372 | // to this lldb-platform is concerned |
| 373 | response.PutCString ("hostname:"); |
| 374 | response.PutCStringAsRawHex8("localhost"); |
| 375 | response.PutChar(';'); |
| 376 | #else // #if defined(__arm__) |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 377 | if (Host::GetHostname (s)) |
| 378 | { |
| 379 | response.PutCString ("hostname:"); |
| 380 | response.PutCStringAsRawHex8(s.c_str()); |
| 381 | response.PutChar(';'); |
| 382 | } |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 383 | |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 384 | #endif // #if defined(__arm__) |
| 385 | |
| 386 | #else // #if defined(__APPLE__) |
| 387 | if (Host::GetHostname (s)) |
| 388 | { |
| 389 | response.PutCString ("hostname:"); |
| 390 | response.PutCStringAsRawHex8(s.c_str()); |
| 391 | response.PutChar(';'); |
| 392 | } |
| 393 | #endif // #if defined(__APPLE__) |
| 394 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 395 | return SendPacketNoLock (response.GetData(), response.GetSize()); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 396 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 397 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 398 | static void |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 399 | CreateProcessInfoResponse (const ProcessInstanceInfo &proc_info, StreamString &response) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 400 | { |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 401 | response.Printf ("pid:%" PRIu64 ";ppid:%" PRIu64 ";uid:%i;gid:%i;euid:%i;egid:%i;", |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 402 | proc_info.GetProcessID(), |
| 403 | proc_info.GetParentProcessID(), |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 404 | proc_info.GetUserID(), |
| 405 | proc_info.GetGroupID(), |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 406 | proc_info.GetEffectiveUserID(), |
| 407 | proc_info.GetEffectiveGroupID()); |
| 408 | response.PutCString ("name:"); |
| 409 | response.PutCStringAsRawHex8(proc_info.GetName()); |
| 410 | response.PutChar(';'); |
| 411 | const ArchSpec &proc_arch = proc_info.GetArchitecture(); |
| 412 | if (proc_arch.IsValid()) |
| 413 | { |
| 414 | const llvm::Triple &proc_triple = proc_arch.GetTriple(); |
| 415 | response.PutCString("triple:"); |
| 416 | response.PutCStringAsRawHex8(proc_triple.getTriple().c_str()); |
| 417 | response.PutChar(';'); |
| 418 | } |
| 419 | } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 420 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 421 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 422 | GDBRemoteCommunicationServer::Handle_qProcessInfoPID (StringExtractorGDBRemote &packet) |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 423 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 424 | // Packet format: "qProcessInfoPID:%i" where %i is the pid |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 425 | packet.SetFilePos(::strlen ("qProcessInfoPID:")); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 426 | lldb::pid_t pid = packet.GetU32 (LLDB_INVALID_PROCESS_ID); |
| 427 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 428 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 429 | ProcessInstanceInfo proc_info; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 430 | if (Host::GetProcessInfo(pid, proc_info)) |
| 431 | { |
| 432 | StreamString response; |
| 433 | CreateProcessInfoResponse (proc_info, response); |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 434 | return SendPacketNoLock (response.GetData(), response.GetSize()); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | return SendErrorResponse (1); |
| 438 | } |
| 439 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 440 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 441 | GDBRemoteCommunicationServer::Handle_qfProcessInfo (StringExtractorGDBRemote &packet) |
| 442 | { |
| 443 | m_proc_infos_index = 0; |
| 444 | m_proc_infos.Clear(); |
| 445 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 446 | ProcessInstanceInfoMatch match_info; |
| 447 | packet.SetFilePos(::strlen ("qfProcessInfo")); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 448 | if (packet.GetChar() == ':') |
| 449 | { |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 450 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 451 | std::string key; |
| 452 | std::string value; |
| 453 | while (packet.GetNameColonValue(key, value)) |
| 454 | { |
| 455 | bool success = true; |
| 456 | if (key.compare("name") == 0) |
| 457 | { |
| 458 | StringExtractor extractor; |
| 459 | extractor.GetStringRef().swap(value); |
| 460 | extractor.GetHexByteString (value); |
Greg Clayton | 144f3a9 | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 461 | match_info.GetProcessInfo().GetExecutableFile().SetFile(value.c_str(), false); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 462 | } |
| 463 | else if (key.compare("name_match") == 0) |
| 464 | { |
| 465 | if (value.compare("equals") == 0) |
| 466 | { |
| 467 | match_info.SetNameMatchType (eNameMatchEquals); |
| 468 | } |
| 469 | else if (value.compare("starts_with") == 0) |
| 470 | { |
| 471 | match_info.SetNameMatchType (eNameMatchStartsWith); |
| 472 | } |
| 473 | else if (value.compare("ends_with") == 0) |
| 474 | { |
| 475 | match_info.SetNameMatchType (eNameMatchEndsWith); |
| 476 | } |
| 477 | else if (value.compare("contains") == 0) |
| 478 | { |
| 479 | match_info.SetNameMatchType (eNameMatchContains); |
| 480 | } |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 481 | else if (value.compare("regex") == 0) |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 482 | { |
| 483 | match_info.SetNameMatchType (eNameMatchRegularExpression); |
| 484 | } |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 485 | else |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 486 | { |
| 487 | success = false; |
| 488 | } |
| 489 | } |
| 490 | else if (key.compare("pid") == 0) |
| 491 | { |
| 492 | match_info.GetProcessInfo().SetProcessID (Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0, &success)); |
| 493 | } |
| 494 | else if (key.compare("parent_pid") == 0) |
| 495 | { |
| 496 | match_info.GetProcessInfo().SetParentProcessID (Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0, &success)); |
| 497 | } |
| 498 | else if (key.compare("uid") == 0) |
| 499 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 500 | match_info.GetProcessInfo().SetUserID (Args::StringToUInt32(value.c_str(), UINT32_MAX, 0, &success)); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 501 | } |
| 502 | else if (key.compare("gid") == 0) |
| 503 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 504 | match_info.GetProcessInfo().SetGroupID (Args::StringToUInt32(value.c_str(), UINT32_MAX, 0, &success)); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 505 | } |
| 506 | else if (key.compare("euid") == 0) |
| 507 | { |
| 508 | match_info.GetProcessInfo().SetEffectiveUserID (Args::StringToUInt32(value.c_str(), UINT32_MAX, 0, &success)); |
| 509 | } |
| 510 | else if (key.compare("egid") == 0) |
| 511 | { |
| 512 | match_info.GetProcessInfo().SetEffectiveGroupID (Args::StringToUInt32(value.c_str(), UINT32_MAX, 0, &success)); |
| 513 | } |
| 514 | else if (key.compare("all_users") == 0) |
| 515 | { |
| 516 | match_info.SetMatchAllUsers(Args::StringToBoolean(value.c_str(), false, &success)); |
| 517 | } |
| 518 | else if (key.compare("triple") == 0) |
| 519 | { |
Greg Clayton | eb0103f | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 520 | match_info.GetProcessInfo().GetArchitecture().SetTriple (value.c_str(), NULL); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 521 | } |
| 522 | else |
| 523 | { |
| 524 | success = false; |
| 525 | } |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 526 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 527 | if (!success) |
| 528 | return SendErrorResponse (2); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | if (Host::FindProcesses (match_info, m_proc_infos)) |
| 533 | { |
| 534 | // We found something, return the first item by calling the get |
| 535 | // subsequent process info packet handler... |
| 536 | return Handle_qsProcessInfo (packet); |
| 537 | } |
| 538 | return SendErrorResponse (3); |
| 539 | } |
| 540 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 541 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 542 | GDBRemoteCommunicationServer::Handle_qsProcessInfo (StringExtractorGDBRemote &packet) |
| 543 | { |
| 544 | if (m_proc_infos_index < m_proc_infos.GetSize()) |
| 545 | { |
| 546 | StreamString response; |
| 547 | CreateProcessInfoResponse (m_proc_infos.GetProcessInfoAtIndex(m_proc_infos_index), response); |
| 548 | ++m_proc_infos_index; |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 549 | return SendPacketNoLock (response.GetData(), response.GetSize()); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 550 | } |
| 551 | return SendErrorResponse (4); |
| 552 | } |
| 553 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 554 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 555 | GDBRemoteCommunicationServer::Handle_qUserName (StringExtractorGDBRemote &packet) |
| 556 | { |
| 557 | // Packet format: "qUserName:%i" where %i is the uid |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 558 | packet.SetFilePos(::strlen ("qUserName:")); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 559 | uint32_t uid = packet.GetU32 (UINT32_MAX); |
| 560 | if (uid != UINT32_MAX) |
| 561 | { |
| 562 | std::string name; |
| 563 | if (Host::GetUserName (uid, name)) |
| 564 | { |
| 565 | StreamString response; |
| 566 | response.PutCStringAsRawHex8 (name.c_str()); |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 567 | return SendPacketNoLock (response.GetData(), response.GetSize()); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | return SendErrorResponse (5); |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 571 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 574 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 575 | GDBRemoteCommunicationServer::Handle_qGroupName (StringExtractorGDBRemote &packet) |
| 576 | { |
| 577 | // Packet format: "qGroupName:%i" where %i is the gid |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 578 | packet.SetFilePos(::strlen ("qGroupName:")); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 579 | uint32_t gid = packet.GetU32 (UINT32_MAX); |
| 580 | if (gid != UINT32_MAX) |
| 581 | { |
| 582 | std::string name; |
| 583 | if (Host::GetGroupName (gid, name)) |
| 584 | { |
| 585 | StreamString response; |
| 586 | response.PutCStringAsRawHex8 (name.c_str()); |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 587 | return SendPacketNoLock (response.GetData(), response.GetSize()); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | return SendErrorResponse (6); |
| 591 | } |
| 592 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 593 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 594 | GDBRemoteCommunicationServer::Handle_qSpeedTest (StringExtractorGDBRemote &packet) |
| 595 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 596 | packet.SetFilePos(::strlen ("qSpeedTest:")); |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 597 | |
| 598 | std::string key; |
| 599 | std::string value; |
| 600 | bool success = packet.GetNameColonValue(key, value); |
| 601 | if (success && key.compare("response_size") == 0) |
| 602 | { |
| 603 | uint32_t response_size = Args::StringToUInt32(value.c_str(), 0, 0, &success); |
| 604 | if (success) |
| 605 | { |
| 606 | if (response_size == 0) |
| 607 | return SendOKResponse(); |
| 608 | StreamString response; |
| 609 | uint32_t bytes_left = response_size; |
| 610 | response.PutCString("data:"); |
| 611 | while (bytes_left > 0) |
| 612 | { |
| 613 | if (bytes_left >= 26) |
| 614 | { |
| 615 | response.PutCString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
| 616 | bytes_left -= 26; |
| 617 | } |
| 618 | else |
| 619 | { |
| 620 | response.Printf ("%*.*s;", bytes_left, bytes_left, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
| 621 | bytes_left = 0; |
| 622 | } |
| 623 | } |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 624 | return SendPacketNoLock (response.GetData(), response.GetSize()); |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | return SendErrorResponse (7); |
| 628 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 629 | |
| 630 | |
| 631 | static void * |
| 632 | AcceptPortFromInferior (void *arg) |
| 633 | { |
| 634 | const char *connect_url = (const char *)arg; |
| 635 | ConnectionFileDescriptor file_conn; |
| 636 | Error error; |
| 637 | if (file_conn.Connect (connect_url, &error) == eConnectionStatusSuccess) |
| 638 | { |
| 639 | char pid_str[256]; |
| 640 | ::memset (pid_str, 0, sizeof(pid_str)); |
| 641 | ConnectionStatus status; |
Bill Wendling | ed24dcc | 2012-04-03 07:50:11 +0000 | [diff] [blame] | 642 | const size_t pid_str_len = file_conn.Read (pid_str, sizeof(pid_str), 0, status, NULL); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 643 | if (pid_str_len > 0) |
| 644 | { |
| 645 | int pid = atoi (pid_str); |
| 646 | return (void *)(intptr_t)pid; |
| 647 | } |
| 648 | } |
| 649 | return NULL; |
| 650 | } |
| 651 | // |
| 652 | //static bool |
| 653 | //WaitForProcessToSIGSTOP (const lldb::pid_t pid, const int timeout_in_seconds) |
| 654 | //{ |
| 655 | // const int time_delta_usecs = 100000; |
| 656 | // const int num_retries = timeout_in_seconds/time_delta_usecs; |
| 657 | // for (int i=0; i<num_retries; i++) |
| 658 | // { |
| 659 | // struct proc_bsdinfo bsd_info; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 660 | // int error = ::proc_pidinfo (pid, PROC_PIDTBSDINFO, |
| 661 | // (uint64_t) 0, |
| 662 | // &bsd_info, |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 663 | // PROC_PIDTBSDINFO_SIZE); |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 664 | // |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 665 | // switch (error) |
| 666 | // { |
| 667 | // case EINVAL: |
| 668 | // case ENOTSUP: |
| 669 | // case ESRCH: |
| 670 | // case EPERM: |
| 671 | // return false; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 672 | // |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 673 | // default: |
| 674 | // break; |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 675 | // |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 676 | // case 0: |
| 677 | // if (bsd_info.pbi_status == SSTOP) |
| 678 | // return true; |
| 679 | // } |
| 680 | // ::usleep (time_delta_usecs); |
| 681 | // } |
| 682 | // return false; |
| 683 | //} |
| 684 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 685 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 686 | GDBRemoteCommunicationServer::Handle_A (StringExtractorGDBRemote &packet) |
| 687 | { |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 688 | // The 'A' packet is the most over designed packet ever here with |
| 689 | // redundant argument indexes, redundant argument lengths and needed hex |
| 690 | // encoded argument string values. Really all that is needed is a comma |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 691 | // separated hex encoded argument value list, but we will stay true to the |
| 692 | // documented version of the 'A' packet here... |
| 693 | |
| 694 | packet.SetFilePos(1); // Skip the 'A' |
| 695 | bool success = true; |
| 696 | while (success && packet.GetBytesLeft() > 0) |
| 697 | { |
| 698 | // Decode the decimal argument string length. This length is the |
| 699 | // number of hex nibbles in the argument string value. |
| 700 | const uint32_t arg_len = packet.GetU32(UINT32_MAX); |
| 701 | if (arg_len == UINT32_MAX) |
| 702 | success = false; |
| 703 | else |
| 704 | { |
| 705 | // Make sure the argument hex string length is followed by a comma |
| 706 | if (packet.GetChar() != ',') |
| 707 | success = false; |
| 708 | else |
| 709 | { |
| 710 | // Decode the argument index. We ignore this really becuase |
| 711 | // who would really send down the arguments in a random order??? |
| 712 | const uint32_t arg_idx = packet.GetU32(UINT32_MAX); |
| 713 | if (arg_idx == UINT32_MAX) |
| 714 | success = false; |
| 715 | else |
| 716 | { |
| 717 | // Make sure the argument index is followed by a comma |
| 718 | if (packet.GetChar() != ',') |
| 719 | success = false; |
| 720 | else |
| 721 | { |
| 722 | // Decode the argument string value from hex bytes |
| 723 | // back into a UTF8 string and make sure the length |
| 724 | // matches the one supplied in the packet |
| 725 | std::string arg; |
| 726 | if (packet.GetHexByteString(arg) != (arg_len / 2)) |
| 727 | success = false; |
| 728 | else |
| 729 | { |
| 730 | // If there are any bytes lft |
| 731 | if (packet.GetBytesLeft()) |
| 732 | { |
| 733 | if (packet.GetChar() != ',') |
| 734 | success = false; |
| 735 | } |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 736 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 737 | if (success) |
| 738 | { |
| 739 | if (arg_idx == 0) |
| 740 | m_process_launch_info.GetExecutableFile().SetFile(arg.c_str(), false); |
| 741 | m_process_launch_info.GetArguments().AppendArgument(arg.c_str()); |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | if (success) |
| 751 | { |
| 752 | m_process_launch_info.GetFlags().Set (eLaunchFlagDebug); |
| 753 | m_process_launch_error = Host::LaunchProcess (m_process_launch_info); |
| 754 | if (m_process_launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) |
| 755 | { |
| 756 | return SendOKResponse (); |
| 757 | } |
| 758 | } |
| 759 | return SendErrorResponse (8); |
| 760 | } |
| 761 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 762 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 763 | GDBRemoteCommunicationServer::Handle_qC (StringExtractorGDBRemote &packet) |
| 764 | { |
| 765 | lldb::pid_t pid = m_process_launch_info.GetProcessID(); |
| 766 | StreamString response; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 767 | response.Printf("QC%" PRIx64, pid); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 768 | if (m_is_platform) |
| 769 | { |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 770 | // If we launch a process and this GDB server is acting as a platform, |
| 771 | // then we need to clear the process launch state so we can start |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 772 | // launching another process. In order to launch a process a bunch or |
| 773 | // packets need to be sent: environment packets, working directory, |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 774 | // disable ASLR, and many more settings. When we launch a process we |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 775 | // then need to know when to clear this information. Currently we are |
| 776 | // selecting the 'qC' packet as that packet which seems to make the most |
| 777 | // sense. |
| 778 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 779 | { |
| 780 | m_process_launch_info.Clear(); |
| 781 | } |
| 782 | } |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 783 | return SendPacketNoLock (response.GetData(), response.GetSize()); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | bool |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 787 | GDBRemoteCommunicationServer::DebugserverProcessReaped (lldb::pid_t pid) |
| 788 | { |
| 789 | Mutex::Locker locker (m_spawned_pids_mutex); |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 790 | FreePortForProcess(pid); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 791 | return m_spawned_pids.erase(pid) > 0; |
| 792 | } |
| 793 | bool |
| 794 | GDBRemoteCommunicationServer::ReapDebugserverProcess (void *callback_baton, |
| 795 | lldb::pid_t pid, |
| 796 | bool exited, |
| 797 | int signal, // Zero for no signal |
| 798 | int status) // Exit value of process if signal is zero |
| 799 | { |
| 800 | GDBRemoteCommunicationServer *server = (GDBRemoteCommunicationServer *)callback_baton; |
| 801 | server->DebugserverProcessReaped (pid); |
| 802 | return true; |
| 803 | } |
| 804 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 805 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 806 | GDBRemoteCommunicationServer::Handle_qLaunchGDBServer (StringExtractorGDBRemote &packet) |
| 807 | { |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 808 | #ifdef _WIN32 |
Deepak Panickal | 263fde0 | 2014-01-14 11:34:44 +0000 | [diff] [blame] | 809 | return SendErrorResponse(9); |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 810 | #else |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 811 | // Spawn a local debugserver as a platform so we can then attach or launch |
| 812 | // a process... |
| 813 | |
| 814 | if (m_is_platform) |
| 815 | { |
| 816 | // Sleep and wait a bit for debugserver to start to listen... |
| 817 | ConnectionFileDescriptor file_conn; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 818 | Error error; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 819 | std::string hostname; |
Sylvestre Ledru | faa63ce | 2013-09-28 15:57:37 +0000 | [diff] [blame] | 820 | // TODO: /tmp/ should not be hardcoded. User might want to override /tmp |
| 821 | // with the TMPDIR environnement variable |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 822 | packet.SetFilePos(::strlen ("qLaunchGDBServer;")); |
| 823 | std::string name; |
| 824 | std::string value; |
| 825 | uint16_t port = UINT16_MAX; |
| 826 | while (packet.GetNameColonValue(name, value)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 827 | { |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 828 | if (name.compare ("host") == 0) |
| 829 | hostname.swap(value); |
| 830 | else if (name.compare ("port") == 0) |
| 831 | port = Args::StringToUInt32(value.c_str(), 0, 0); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 832 | } |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 833 | if (port == UINT16_MAX) |
| 834 | port = GetNextAvailablePort(); |
| 835 | |
| 836 | // Spawn a new thread to accept the port that gets bound after |
| 837 | // binding to port 0 (zero). |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 838 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 839 | if (error.Success()) |
| 840 | { |
| 841 | // Spawn a debugserver and try to get the port it listens to. |
| 842 | ProcessLaunchInfo debugserver_launch_info; |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 843 | if (hostname.empty()) |
| 844 | hostname = "localhost"; |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 845 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM)); |
| 846 | if (log) |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 847 | log->Printf("Launching debugserver with: %s:%u...\n", hostname.c_str(), port); |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 848 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 849 | debugserver_launch_info.SetMonitorProcessCallback(ReapDebugserverProcess, this, false); |
| 850 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 851 | error = StartDebugserverProcess (hostname.empty() ? NULL : hostname.c_str(), |
| 852 | port, |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 853 | debugserver_launch_info, |
| 854 | port); |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 855 | |
| 856 | lldb::pid_t debugserver_pid = debugserver_launch_info.GetProcessID(); |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 857 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 858 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 859 | if (debugserver_pid != LLDB_INVALID_PROCESS_ID) |
| 860 | { |
| 861 | Mutex::Locker locker (m_spawned_pids_mutex); |
| 862 | m_spawned_pids.insert(debugserver_pid); |
| 863 | if (port > 0) |
| 864 | AssociatePortWithProcess(port, debugserver_pid); |
| 865 | } |
| 866 | else |
| 867 | { |
| 868 | if (port > 0) |
| 869 | FreePort (port); |
| 870 | } |
| 871 | |
| 872 | if (error.Success()) |
| 873 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 874 | char response[256]; |
| 875 | const int response_len = ::snprintf (response, sizeof(response), "pid:%" PRIu64 ";port:%u;", debugserver_pid, port + m_port_offset); |
| 876 | assert (response_len < sizeof(response)); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 877 | PacketResult packet_result = SendPacketNoLock (response, response_len); |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 878 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 879 | if (packet_result != PacketResult::Success) |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 880 | { |
| 881 | if (debugserver_pid != LLDB_INVALID_PROCESS_ID) |
| 882 | ::kill (debugserver_pid, SIGINT); |
| 883 | } |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 884 | return packet_result; |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 885 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 886 | } |
| 887 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 888 | return SendErrorResponse (9); |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 889 | #endif |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 892 | GDBRemoteCommunication::PacketResult |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 893 | GDBRemoteCommunicationServer::Handle_qKillSpawnedProcess (StringExtractorGDBRemote &packet) |
| 894 | { |
| 895 | // Spawn a local debugserver as a platform so we can then attach or launch |
| 896 | // a process... |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 897 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 898 | if (m_is_platform) |
| 899 | { |
| 900 | packet.SetFilePos(::strlen ("qKillSpawnedProcess:")); |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 901 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 902 | lldb::pid_t pid = packet.GetU64(LLDB_INVALID_PROCESS_ID); |
| 903 | |
| 904 | // Scope for locker |
| 905 | { |
| 906 | Mutex::Locker locker (m_spawned_pids_mutex); |
| 907 | if (m_spawned_pids.find(pid) == m_spawned_pids.end()) |
| 908 | return SendErrorResponse (10); |
| 909 | } |
Virgile Bello | ae12a36 | 2013-08-27 16:21:49 +0000 | [diff] [blame] | 910 | Host::Kill (pid, SIGTERM); |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 911 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 912 | for (size_t i=0; i<10; ++i) |
| 913 | { |
| 914 | // Scope for locker |
| 915 | { |
| 916 | Mutex::Locker locker (m_spawned_pids_mutex); |
| 917 | if (m_spawned_pids.find(pid) == m_spawned_pids.end()) |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 918 | return SendOKResponse(); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 919 | } |
| 920 | usleep (10000); |
| 921 | } |
| 922 | |
| 923 | // Scope for locker |
| 924 | { |
| 925 | Mutex::Locker locker (m_spawned_pids_mutex); |
| 926 | if (m_spawned_pids.find(pid) == m_spawned_pids.end()) |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 927 | return SendOKResponse(); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 928 | } |
Virgile Bello | ae12a36 | 2013-08-27 16:21:49 +0000 | [diff] [blame] | 929 | Host::Kill (pid, SIGKILL); |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 930 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 931 | for (size_t i=0; i<10; ++i) |
| 932 | { |
| 933 | // Scope for locker |
| 934 | { |
| 935 | Mutex::Locker locker (m_spawned_pids_mutex); |
| 936 | if (m_spawned_pids.find(pid) == m_spawned_pids.end()) |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 937 | return SendOKResponse(); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 938 | } |
| 939 | usleep (10000); |
| 940 | } |
| 941 | } |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 942 | return SendErrorResponse (11); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 945 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 946 | GDBRemoteCommunicationServer::Handle_qLaunchSuccess (StringExtractorGDBRemote &packet) |
| 947 | { |
| 948 | if (m_process_launch_error.Success()) |
| 949 | return SendOKResponse(); |
Sylvestre Ledru | b027bd2 | 2013-09-28 14:35:00 +0000 | [diff] [blame] | 950 | StreamString response; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 951 | response.PutChar('E'); |
| 952 | response.PutCString(m_process_launch_error.AsCString("<unknown error>")); |
Greg Clayton | 37a0a24 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 953 | return SendPacketNoLock (response.GetData(), response.GetSize()); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 956 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 957 | GDBRemoteCommunicationServer::Handle_QEnvironment (StringExtractorGDBRemote &packet) |
| 958 | { |
| 959 | packet.SetFilePos(::strlen ("QEnvironment:")); |
| 960 | const uint32_t bytes_left = packet.GetBytesLeft(); |
| 961 | if (bytes_left > 0) |
| 962 | { |
| 963 | m_process_launch_info.GetEnvironmentEntries ().AppendArgument (packet.Peek()); |
| 964 | return SendOKResponse (); |
| 965 | } |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 966 | return SendErrorResponse (12); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 969 | GDBRemoteCommunication::PacketResult |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 970 | GDBRemoteCommunicationServer::Handle_QLaunchArch (StringExtractorGDBRemote &packet) |
| 971 | { |
| 972 | packet.SetFilePos(::strlen ("QLaunchArch:")); |
| 973 | const uint32_t bytes_left = packet.GetBytesLeft(); |
| 974 | if (bytes_left > 0) |
| 975 | { |
| 976 | const char* arch_triple = packet.Peek(); |
| 977 | ArchSpec arch_spec(arch_triple,NULL); |
| 978 | m_process_launch_info.SetArchitecture(arch_spec); |
| 979 | return SendOKResponse(); |
| 980 | } |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 981 | return SendErrorResponse(13); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 984 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 985 | GDBRemoteCommunicationServer::Handle_QSetDisableASLR (StringExtractorGDBRemote &packet) |
| 986 | { |
| 987 | packet.SetFilePos(::strlen ("QSetDisableASLR:")); |
| 988 | if (packet.GetU32(0)) |
| 989 | m_process_launch_info.GetFlags().Set (eLaunchFlagDisableASLR); |
| 990 | else |
| 991 | m_process_launch_info.GetFlags().Clear (eLaunchFlagDisableASLR); |
| 992 | return SendOKResponse (); |
| 993 | } |
| 994 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 995 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 996 | GDBRemoteCommunicationServer::Handle_QSetWorkingDir (StringExtractorGDBRemote &packet) |
| 997 | { |
| 998 | packet.SetFilePos(::strlen ("QSetWorkingDir:")); |
| 999 | std::string path; |
| 1000 | packet.GetHexByteString(path); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1001 | if (m_is_platform) |
| 1002 | { |
Colin Riley | 909bb7a | 2013-11-26 15:10:46 +0000 | [diff] [blame] | 1003 | #ifdef _WIN32 |
| 1004 | // Not implemented on Windows |
| 1005 | return SendUnimplementedResponse("GDBRemoteCommunicationServer::Handle_QSetWorkingDir unimplemented"); |
| 1006 | #else |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1007 | // If this packet is sent to a platform, then change the current working directory |
| 1008 | if (::chdir(path.c_str()) != 0) |
| 1009 | return SendErrorResponse(errno); |
Colin Riley | 909bb7a | 2013-11-26 15:10:46 +0000 | [diff] [blame] | 1010 | #endif |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1011 | } |
| 1012 | else |
| 1013 | { |
| 1014 | m_process_launch_info.SwapWorkingDirectory (path); |
| 1015 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1016 | return SendOKResponse (); |
| 1017 | } |
| 1018 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1019 | GDBRemoteCommunication::PacketResult |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1020 | GDBRemoteCommunicationServer::Handle_qGetWorkingDir (StringExtractorGDBRemote &packet) |
| 1021 | { |
| 1022 | StreamString response; |
| 1023 | |
| 1024 | if (m_is_platform) |
| 1025 | { |
| 1026 | // If this packet is sent to a platform, then change the current working directory |
| 1027 | char cwd[PATH_MAX]; |
| 1028 | if (getcwd(cwd, sizeof(cwd)) == NULL) |
| 1029 | { |
| 1030 | return SendErrorResponse(errno); |
| 1031 | } |
| 1032 | else |
| 1033 | { |
| 1034 | response.PutBytesAsRawHex8(cwd, strlen(cwd)); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1035 | return SendPacketNoLock(response.GetData(), response.GetSize()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | else |
| 1039 | { |
| 1040 | const char *working_dir = m_process_launch_info.GetWorkingDirectory(); |
| 1041 | if (working_dir && working_dir[0]) |
| 1042 | { |
| 1043 | response.PutBytesAsRawHex8(working_dir, strlen(working_dir)); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1044 | return SendPacketNoLock(response.GetData(), response.GetSize()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1045 | } |
| 1046 | else |
| 1047 | { |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1048 | return SendErrorResponse(14); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1053 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1054 | GDBRemoteCommunicationServer::Handle_QSetSTDIN (StringExtractorGDBRemote &packet) |
| 1055 | { |
| 1056 | packet.SetFilePos(::strlen ("QSetSTDIN:")); |
| 1057 | ProcessLaunchInfo::FileAction file_action; |
| 1058 | std::string path; |
| 1059 | packet.GetHexByteString(path); |
| 1060 | const bool read = false; |
| 1061 | const bool write = true; |
| 1062 | if (file_action.Open(STDIN_FILENO, path.c_str(), read, write)) |
| 1063 | { |
| 1064 | m_process_launch_info.AppendFileAction(file_action); |
| 1065 | return SendOKResponse (); |
| 1066 | } |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1067 | return SendErrorResponse (15); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1070 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1071 | GDBRemoteCommunicationServer::Handle_QSetSTDOUT (StringExtractorGDBRemote &packet) |
| 1072 | { |
| 1073 | packet.SetFilePos(::strlen ("QSetSTDOUT:")); |
| 1074 | ProcessLaunchInfo::FileAction file_action; |
| 1075 | std::string path; |
| 1076 | packet.GetHexByteString(path); |
| 1077 | const bool read = true; |
| 1078 | const bool write = false; |
| 1079 | if (file_action.Open(STDOUT_FILENO, path.c_str(), read, write)) |
| 1080 | { |
| 1081 | m_process_launch_info.AppendFileAction(file_action); |
| 1082 | return SendOKResponse (); |
| 1083 | } |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1084 | return SendErrorResponse (16); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1087 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1088 | GDBRemoteCommunicationServer::Handle_QSetSTDERR (StringExtractorGDBRemote &packet) |
| 1089 | { |
| 1090 | packet.SetFilePos(::strlen ("QSetSTDERR:")); |
| 1091 | ProcessLaunchInfo::FileAction file_action; |
| 1092 | std::string path; |
| 1093 | packet.GetHexByteString(path); |
| 1094 | const bool read = true; |
Greg Clayton | 9845a8d | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 1095 | const bool write = false; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1096 | if (file_action.Open(STDERR_FILENO, path.c_str(), read, write)) |
| 1097 | { |
| 1098 | m_process_launch_info.AppendFileAction(file_action); |
| 1099 | return SendOKResponse (); |
| 1100 | } |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1101 | return SendErrorResponse (17); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1104 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1105 | GDBRemoteCommunicationServer::Handle_QStartNoAckMode (StringExtractorGDBRemote &packet) |
| 1106 | { |
| 1107 | // Send response first before changing m_send_acks to we ack this packet |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1108 | PacketResult packet_result = SendOKResponse (); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1109 | m_send_acks = false; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1110 | return packet_result; |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 1111 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1112 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1113 | GDBRemoteCommunication::PacketResult |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1114 | GDBRemoteCommunicationServer::Handle_qPlatform_mkdir (StringExtractorGDBRemote &packet) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1115 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1116 | packet.SetFilePos(::strlen("qPlatform_mkdir:")); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1117 | mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX); |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1118 | if (packet.GetChar() == ',') |
| 1119 | { |
| 1120 | std::string path; |
| 1121 | packet.GetHexByteString(path); |
| 1122 | Error error = Host::MakeDirectory(path.c_str(),mode); |
| 1123 | if (error.Success()) |
| 1124 | return SendPacketNoLock ("OK", 2); |
| 1125 | else |
| 1126 | return SendErrorResponse(error.GetError()); |
| 1127 | } |
| 1128 | return SendErrorResponse(20); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1131 | GDBRemoteCommunication::PacketResult |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1132 | GDBRemoteCommunicationServer::Handle_qPlatform_chmod (StringExtractorGDBRemote &packet) |
| 1133 | { |
| 1134 | packet.SetFilePos(::strlen("qPlatform_chmod:")); |
| 1135 | |
| 1136 | mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX); |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1137 | if (packet.GetChar() == ',') |
| 1138 | { |
| 1139 | std::string path; |
| 1140 | packet.GetHexByteString(path); |
| 1141 | Error error = Host::SetFilePermissions (path.c_str(), mode); |
| 1142 | if (error.Success()) |
| 1143 | return SendPacketNoLock ("OK", 2); |
| 1144 | else |
| 1145 | return SendErrorResponse(error.GetError()); |
| 1146 | } |
| 1147 | return SendErrorResponse(19); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1150 | GDBRemoteCommunication::PacketResult |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1151 | GDBRemoteCommunicationServer::Handle_vFile_Open (StringExtractorGDBRemote &packet) |
| 1152 | { |
| 1153 | packet.SetFilePos(::strlen("vFile:open:")); |
| 1154 | std::string path; |
| 1155 | packet.GetHexByteStringTerminatedBy(path,','); |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1156 | if (!path.empty()) |
| 1157 | { |
| 1158 | if (packet.GetChar() == ',') |
| 1159 | { |
| 1160 | uint32_t flags = packet.GetHexMaxU32(false, 0); |
| 1161 | if (packet.GetChar() == ',') |
| 1162 | { |
| 1163 | mode_t mode = packet.GetHexMaxU32(false, 0600); |
| 1164 | Error error; |
| 1165 | int fd = ::open (path.c_str(), flags, mode); |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1166 | const int save_errno = fd == -1 ? errno : 0; |
| 1167 | StreamString response; |
| 1168 | response.PutChar('F'); |
| 1169 | response.Printf("%i", fd); |
| 1170 | if (save_errno) |
| 1171 | response.Printf(",%i", save_errno); |
| 1172 | return SendPacketNoLock(response.GetData(), response.GetSize()); |
| 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | return SendErrorResponse(18); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1179 | GDBRemoteCommunication::PacketResult |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1180 | GDBRemoteCommunicationServer::Handle_vFile_Close (StringExtractorGDBRemote &packet) |
| 1181 | { |
| 1182 | packet.SetFilePos(::strlen("vFile:close:")); |
| 1183 | int fd = packet.GetS32(-1); |
| 1184 | Error error; |
| 1185 | int err = -1; |
| 1186 | int save_errno = 0; |
| 1187 | if (fd >= 0) |
| 1188 | { |
| 1189 | err = close(fd); |
| 1190 | save_errno = err == -1 ? errno : 0; |
| 1191 | } |
| 1192 | else |
| 1193 | { |
| 1194 | save_errno = EINVAL; |
| 1195 | } |
| 1196 | StreamString response; |
| 1197 | response.PutChar('F'); |
| 1198 | response.Printf("%i", err); |
| 1199 | if (save_errno) |
| 1200 | response.Printf(",%i", save_errno); |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1201 | return SendPacketNoLock(response.GetData(), response.GetSize()); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1204 | GDBRemoteCommunication::PacketResult |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1205 | GDBRemoteCommunicationServer::Handle_vFile_pRead (StringExtractorGDBRemote &packet) |
| 1206 | { |
Virgile Bello | ae12a36 | 2013-08-27 16:21:49 +0000 | [diff] [blame] | 1207 | #ifdef _WIN32 |
| 1208 | // Not implemented on Windows |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1209 | return SendUnimplementedResponse("GDBRemoteCommunicationServer::Handle_vFile_pRead() unimplemented"); |
Virgile Bello | ae12a36 | 2013-08-27 16:21:49 +0000 | [diff] [blame] | 1210 | #else |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1211 | StreamGDBRemote response; |
| 1212 | packet.SetFilePos(::strlen("vFile:pread:")); |
| 1213 | int fd = packet.GetS32(-1); |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1214 | if (packet.GetChar() == ',') |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1215 | { |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1216 | uint64_t count = packet.GetU64(UINT64_MAX); |
| 1217 | if (packet.GetChar() == ',') |
| 1218 | { |
| 1219 | uint64_t offset = packet.GetU64(UINT32_MAX); |
| 1220 | if (count == UINT64_MAX) |
| 1221 | { |
| 1222 | response.Printf("F-1:%i", EINVAL); |
| 1223 | return SendPacketNoLock(response.GetData(), response.GetSize()); |
| 1224 | } |
| 1225 | |
| 1226 | std::string buffer(count, 0); |
| 1227 | const ssize_t bytes_read = ::pread (fd, &buffer[0], buffer.size(), offset); |
| 1228 | const int save_errno = bytes_read == -1 ? errno : 0; |
| 1229 | response.PutChar('F'); |
| 1230 | response.Printf("%zi", bytes_read); |
| 1231 | if (save_errno) |
| 1232 | response.Printf(",%i", save_errno); |
| 1233 | else |
| 1234 | { |
| 1235 | response.PutChar(';'); |
| 1236 | response.PutEscapedBytes(&buffer[0], bytes_read); |
| 1237 | } |
| 1238 | return SendPacketNoLock(response.GetData(), response.GetSize()); |
| 1239 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1240 | } |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1241 | return SendErrorResponse(21); |
| 1242 | |
Virgile Bello | ae12a36 | 2013-08-27 16:21:49 +0000 | [diff] [blame] | 1243 | #endif |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1246 | GDBRemoteCommunication::PacketResult |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1247 | GDBRemoteCommunicationServer::Handle_vFile_pWrite (StringExtractorGDBRemote &packet) |
| 1248 | { |
Virgile Bello | ae12a36 | 2013-08-27 16:21:49 +0000 | [diff] [blame] | 1249 | #ifdef _WIN32 |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1250 | return SendUnimplementedResponse("GDBRemoteCommunicationServer::Handle_vFile_pWrite() unimplemented"); |
Virgile Bello | ae12a36 | 2013-08-27 16:21:49 +0000 | [diff] [blame] | 1251 | #else |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1252 | packet.SetFilePos(::strlen("vFile:pwrite:")); |
| 1253 | |
| 1254 | StreamGDBRemote response; |
| 1255 | response.PutChar('F'); |
| 1256 | |
| 1257 | int fd = packet.GetU32(UINT32_MAX); |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1258 | if (packet.GetChar() == ',') |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1259 | { |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1260 | off_t offset = packet.GetU64(UINT32_MAX); |
| 1261 | if (packet.GetChar() == ',') |
| 1262 | { |
| 1263 | std::string buffer; |
| 1264 | if (packet.GetEscapedBinaryData(buffer)) |
| 1265 | { |
| 1266 | const ssize_t bytes_written = ::pwrite (fd, buffer.data(), buffer.size(), offset); |
| 1267 | const int save_errno = bytes_written == -1 ? errno : 0; |
| 1268 | response.Printf("%zi", bytes_written); |
| 1269 | if (save_errno) |
| 1270 | response.Printf(",%i", save_errno); |
| 1271 | } |
| 1272 | else |
| 1273 | { |
| 1274 | response.Printf ("-1,%i", EINVAL); |
| 1275 | } |
| 1276 | return SendPacketNoLock(response.GetData(), response.GetSize()); |
| 1277 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1278 | } |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1279 | return SendErrorResponse(27); |
Virgile Bello | ae12a36 | 2013-08-27 16:21:49 +0000 | [diff] [blame] | 1280 | #endif |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1283 | GDBRemoteCommunication::PacketResult |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1284 | GDBRemoteCommunicationServer::Handle_vFile_Size (StringExtractorGDBRemote &packet) |
| 1285 | { |
| 1286 | packet.SetFilePos(::strlen("vFile:size:")); |
| 1287 | std::string path; |
| 1288 | packet.GetHexByteString(path); |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1289 | if (!path.empty()) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1290 | { |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1291 | lldb::user_id_t retcode = Host::GetFileSize(FileSpec(path.c_str(), false)); |
| 1292 | StreamString response; |
| 1293 | response.PutChar('F'); |
| 1294 | response.PutHex64(retcode); |
| 1295 | if (retcode == UINT64_MAX) |
| 1296 | { |
| 1297 | response.PutChar(','); |
| 1298 | response.PutHex64(retcode); // TODO: replace with Host::GetSyswideErrorCode() |
| 1299 | } |
| 1300 | return SendPacketNoLock(response.GetData(), response.GetSize()); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1301 | } |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1302 | return SendErrorResponse(22); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1305 | GDBRemoteCommunication::PacketResult |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1306 | GDBRemoteCommunicationServer::Handle_vFile_Mode (StringExtractorGDBRemote &packet) |
| 1307 | { |
| 1308 | packet.SetFilePos(::strlen("vFile:mode:")); |
| 1309 | std::string path; |
| 1310 | packet.GetHexByteString(path); |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1311 | if (!path.empty()) |
| 1312 | { |
| 1313 | Error error; |
| 1314 | const uint32_t mode = File::GetPermissions(path.c_str(), error); |
| 1315 | StreamString response; |
| 1316 | response.Printf("F%u", mode); |
| 1317 | if (mode == 0 || error.Fail()) |
| 1318 | response.Printf(",%i", (int)error.GetError()); |
| 1319 | return SendPacketNoLock(response.GetData(), response.GetSize()); |
| 1320 | } |
| 1321 | return SendErrorResponse(23); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1324 | GDBRemoteCommunication::PacketResult |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1325 | GDBRemoteCommunicationServer::Handle_vFile_Exists (StringExtractorGDBRemote &packet) |
| 1326 | { |
| 1327 | packet.SetFilePos(::strlen("vFile:exists:")); |
| 1328 | std::string path; |
| 1329 | packet.GetHexByteString(path); |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1330 | if (!path.empty()) |
| 1331 | { |
| 1332 | bool retcode = Host::GetFileExists(FileSpec(path.c_str(), false)); |
| 1333 | StreamString response; |
| 1334 | response.PutChar('F'); |
| 1335 | response.PutChar(','); |
| 1336 | if (retcode) |
| 1337 | response.PutChar('1'); |
| 1338 | else |
| 1339 | response.PutChar('0'); |
| 1340 | return SendPacketNoLock(response.GetData(), response.GetSize()); |
| 1341 | } |
| 1342 | return SendErrorResponse(24); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1345 | GDBRemoteCommunication::PacketResult |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1346 | GDBRemoteCommunicationServer::Handle_vFile_symlink (StringExtractorGDBRemote &packet) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1347 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1348 | packet.SetFilePos(::strlen("vFile:symlink:")); |
| 1349 | std::string dst, src; |
| 1350 | packet.GetHexByteStringTerminatedBy(dst, ','); |
| 1351 | packet.GetChar(); // Skip ',' char |
| 1352 | packet.GetHexByteString(src); |
| 1353 | Error error = Host::Symlink(src.c_str(), dst.c_str()); |
| 1354 | StreamString response; |
| 1355 | response.Printf("F%u,%u", error.GetError(), error.GetError()); |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1356 | return SendPacketNoLock(response.GetData(), response.GetSize()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1359 | GDBRemoteCommunication::PacketResult |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1360 | GDBRemoteCommunicationServer::Handle_vFile_unlink (StringExtractorGDBRemote &packet) |
| 1361 | { |
| 1362 | packet.SetFilePos(::strlen("vFile:unlink:")); |
| 1363 | std::string path; |
| 1364 | packet.GetHexByteString(path); |
| 1365 | Error error = Host::Unlink(path.c_str()); |
| 1366 | StreamString response; |
| 1367 | response.Printf("F%u,%u", error.GetError(), error.GetError()); |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1368 | return SendPacketNoLock(response.GetData(), response.GetSize()); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1371 | GDBRemoteCommunication::PacketResult |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1372 | GDBRemoteCommunicationServer::Handle_qPlatform_shell (StringExtractorGDBRemote &packet) |
| 1373 | { |
| 1374 | packet.SetFilePos(::strlen("qPlatform_shell:")); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1375 | std::string path; |
| 1376 | std::string working_dir; |
| 1377 | packet.GetHexByteStringTerminatedBy(path,','); |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1378 | if (!path.empty()) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1379 | { |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1380 | if (packet.GetChar() == ',') |
| 1381 | { |
| 1382 | // FIXME: add timeout to qPlatform_shell packet |
| 1383 | // uint32_t timeout = packet.GetHexMaxU32(false, 32); |
| 1384 | uint32_t timeout = 10; |
| 1385 | if (packet.GetChar() == ',') |
| 1386 | packet.GetHexByteString(working_dir); |
| 1387 | int status, signo; |
| 1388 | std::string output; |
| 1389 | Error err = Host::RunShellCommand(path.c_str(), |
| 1390 | working_dir.empty() ? NULL : working_dir.c_str(), |
| 1391 | &status, &signo, &output, timeout); |
| 1392 | StreamGDBRemote response; |
| 1393 | if (err.Fail()) |
| 1394 | { |
| 1395 | response.PutCString("F,"); |
| 1396 | response.PutHex32(UINT32_MAX); |
| 1397 | } |
| 1398 | else |
| 1399 | { |
| 1400 | response.PutCString("F,"); |
| 1401 | response.PutHex32(status); |
| 1402 | response.PutChar(','); |
| 1403 | response.PutHex32(signo); |
| 1404 | response.PutChar(','); |
| 1405 | response.PutEscapedBytes(output.c_str(), output.size()); |
| 1406 | } |
| 1407 | return SendPacketNoLock(response.GetData(), response.GetSize()); |
| 1408 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1409 | } |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1410 | return SendErrorResponse(24); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1413 | GDBRemoteCommunication::PacketResult |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1414 | GDBRemoteCommunicationServer::Handle_vFile_Stat (StringExtractorGDBRemote &packet) |
| 1415 | { |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1416 | return SendUnimplementedResponse("GDBRemoteCommunicationServer::Handle_vFile_Stat() unimplemented"); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1419 | GDBRemoteCommunication::PacketResult |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1420 | GDBRemoteCommunicationServer::Handle_vFile_MD5 (StringExtractorGDBRemote &packet) |
| 1421 | { |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1422 | packet.SetFilePos(::strlen("vFile:MD5:")); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1423 | std::string path; |
| 1424 | packet.GetHexByteString(path); |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1425 | if (!path.empty()) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1426 | { |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1427 | uint64_t a,b; |
| 1428 | StreamGDBRemote response; |
| 1429 | if (Host::CalculateMD5(FileSpec(path.c_str(),false),a,b) == false) |
| 1430 | { |
| 1431 | response.PutCString("F,"); |
| 1432 | response.PutCString("x"); |
| 1433 | } |
| 1434 | else |
| 1435 | { |
| 1436 | response.PutCString("F,"); |
| 1437 | response.PutHex64(a); |
| 1438 | response.PutHex64(b); |
| 1439 | } |
| 1440 | return SendPacketNoLock(response.GetData(), response.GetSize()); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1441 | } |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1442 | return SendErrorResponse(25); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1443 | } |
Greg Clayton | 2b98c56 | 2013-11-22 18:53:12 +0000 | [diff] [blame] | 1444 | |