Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ProcessGDBRemote.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 | |
| 10 | // C Includes |
| 11 | #include <errno.h> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include <spawn.h> |
Stephen Wilson | 50daf77 | 2011-03-25 18:16:28 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
Greg Clayton | 989816b | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 14 | #include <sys/mman.h> // for mmap |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include <sys/stat.h> |
Greg Clayton | 989816b | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 16 | #include <sys/types.h> |
Stephen Wilson | 60f19d5 | 2011-03-30 00:12:40 +0000 | [diff] [blame] | 17 | #include <time.h> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | |
| 19 | // C++ Includes |
| 20 | #include <algorithm> |
| 21 | #include <map> |
| 22 | |
| 23 | // Other libraries and framework includes |
| 24 | |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 25 | #include "lldb/Breakpoint/Watchpoint.h" |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 26 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #include "lldb/Core/ArchSpec.h" |
| 28 | #include "lldb/Core/Debugger.h" |
| 29 | #include "lldb/Core/ConnectionFileDescriptor.h" |
Greg Clayton | 5f54ac3 | 2011-02-08 05:05:52 +0000 | [diff] [blame] | 30 | #include "lldb/Host/FileSpec.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | #include "lldb/Core/InputReader.h" |
| 32 | #include "lldb/Core/Module.h" |
| 33 | #include "lldb/Core/PluginManager.h" |
| 34 | #include "lldb/Core/State.h" |
Greg Clayton | 3355946 | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 35 | #include "lldb/Core/StreamFile.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | #include "lldb/Core/StreamString.h" |
| 37 | #include "lldb/Core/Timer.h" |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 38 | #include "lldb/Core/Value.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | #include "lldb/Host/TimeValue.h" |
| 40 | #include "lldb/Symbol/ObjectFile.h" |
| 41 | #include "lldb/Target/DynamicLoader.h" |
| 42 | #include "lldb/Target/Target.h" |
| 43 | #include "lldb/Target/TargetList.h" |
Greg Clayton | 989816b | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 44 | #include "lldb/Target/ThreadPlanCallFunction.h" |
Jason Molenda | dea5ea7 | 2010-06-09 21:28:42 +0000 | [diff] [blame] | 45 | #include "lldb/Utility/PseudoTerminal.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | |
| 47 | // Project includes |
| 48 | #include "lldb/Host/Host.h" |
Peter Collingbourne | 4d623e8 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 49 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 50 | #include "Utility/StringExtractorGDBRemote.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | #include "GDBRemoteRegisterContext.h" |
| 52 | #include "ProcessGDBRemote.h" |
| 53 | #include "ProcessGDBRemoteLog.h" |
| 54 | #include "ThreadGDBRemote.h" |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 55 | #include "StopInfoMachException.h" |
| 56 | |
Greg Clayton | 451fa82 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 57 | namespace lldb |
| 58 | { |
| 59 | // Provide a function that can easily dump the packet history if we know a |
| 60 | // ProcessGDBRemote * value (which we can get from logs or from debugging). |
| 61 | // We need the function in the lldb namespace so it makes it into the final |
| 62 | // executable since the LLDB shared library only exports stuff in the lldb |
| 63 | // namespace. This allows you to attach with a debugger and call this |
| 64 | // function and get the packet history dumped to a file. |
| 65 | void |
| 66 | DumpProcessGDBRemotePacketHistory (void *p, const char *path) |
| 67 | { |
Greg Clayton | 3355946 | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 68 | lldb_private::StreamFile strm; |
| 69 | lldb_private::Error error (strm.GetFile().Open(path, lldb_private::File::eOpenOptionWrite | lldb_private::File::eOpenOptionCanCreate)); |
| 70 | if (error.Success()) |
| 71 | ((ProcessGDBRemote *)p)->GetGDBRemote().DumpHistory (strm); |
Greg Clayton | 451fa82 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 72 | } |
| 73 | }; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 75 | |
| 76 | #define DEBUGSERVER_BASENAME "debugserver" |
| 77 | using namespace lldb; |
| 78 | using namespace lldb_private; |
| 79 | |
Jim Ingham | f960048 | 2011-03-29 21:45:47 +0000 | [diff] [blame] | 80 | static bool rand_initialized = false; |
| 81 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | static inline uint16_t |
| 83 | get_random_port () |
| 84 | { |
Jim Ingham | f960048 | 2011-03-29 21:45:47 +0000 | [diff] [blame] | 85 | if (!rand_initialized) |
| 86 | { |
Stephen Wilson | 60f19d5 | 2011-03-30 00:12:40 +0000 | [diff] [blame] | 87 | time_t seed = time(NULL); |
| 88 | |
Jim Ingham | f960048 | 2011-03-29 21:45:47 +0000 | [diff] [blame] | 89 | rand_initialized = true; |
Stephen Wilson | 60f19d5 | 2011-03-30 00:12:40 +0000 | [diff] [blame] | 90 | srand(seed); |
Jim Ingham | f960048 | 2011-03-29 21:45:47 +0000 | [diff] [blame] | 91 | } |
Stephen Wilson | 50daf77 | 2011-03-25 18:16:28 +0000 | [diff] [blame] | 92 | return (rand() % (UINT16_MAX - 1000u)) + 1000u; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
| 96 | const char * |
| 97 | ProcessGDBRemote::GetPluginNameStatic() |
| 98 | { |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 99 | return "gdb-remote"; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | const char * |
| 103 | ProcessGDBRemote::GetPluginDescriptionStatic() |
| 104 | { |
| 105 | return "GDB Remote protocol based debugging plug-in."; |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | ProcessGDBRemote::Terminate() |
| 110 | { |
| 111 | PluginManager::UnregisterPlugin (ProcessGDBRemote::CreateInstance); |
| 112 | } |
| 113 | |
| 114 | |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 115 | lldb::ProcessSP |
| 116 | ProcessGDBRemote::CreateInstance (Target &target, Listener &listener, const FileSpec *crash_file_path) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 117 | { |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 118 | lldb::ProcessSP process_sp; |
| 119 | if (crash_file_path == NULL) |
| 120 | process_sp.reset (new ProcessGDBRemote (target, listener)); |
| 121 | return process_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | bool |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 125 | ProcessGDBRemote::CanDebug (Target &target, bool plugin_specified_by_name) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 126 | { |
Greg Clayton | 61ddf56 | 2011-10-21 21:41:45 +0000 | [diff] [blame] | 127 | if (plugin_specified_by_name) |
| 128 | return true; |
| 129 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 130 | // For now we are just making sure the file exists for a given module |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 131 | Module *exe_module = target.GetExecutableModulePointer(); |
| 132 | if (exe_module) |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 133 | { |
| 134 | ObjectFile *exe_objfile = exe_module->GetObjectFile(); |
| 135 | // We can't debug core files... |
| 136 | switch (exe_objfile->GetType()) |
| 137 | { |
| 138 | case ObjectFile::eTypeInvalid: |
| 139 | case ObjectFile::eTypeCoreFile: |
| 140 | case ObjectFile::eTypeDebugInfo: |
| 141 | case ObjectFile::eTypeObjectFile: |
| 142 | case ObjectFile::eTypeSharedLibrary: |
| 143 | case ObjectFile::eTypeStubLibrary: |
| 144 | return false; |
| 145 | case ObjectFile::eTypeExecutable: |
| 146 | case ObjectFile::eTypeDynamicLinker: |
| 147 | case ObjectFile::eTypeUnknown: |
| 148 | break; |
| 149 | } |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 150 | return exe_module->GetFileSpec().Exists(); |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 151 | } |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 152 | // However, if there is no executable module, we return true since we might be preparing to attach. |
| 153 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | //---------------------------------------------------------------------- |
| 157 | // ProcessGDBRemote constructor |
| 158 | //---------------------------------------------------------------------- |
| 159 | ProcessGDBRemote::ProcessGDBRemote(Target& target, Listener &listener) : |
| 160 | Process (target, listener), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 161 | m_flags (0), |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 162 | m_gdb_comm(false), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 163 | m_debugserver_pid (LLDB_INVALID_PROCESS_ID), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 164 | m_last_stop_packet (), |
Greg Clayton | 0670900 | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 165 | m_last_stop_packet_mutex (Mutex::eMutexTypeNormal), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 166 | m_register_info (), |
Jim Ingham | 5a15e69 | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 167 | m_async_broadcaster (NULL, "lldb.process.gdb-remote.async-broadcaster"), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | m_async_thread (LLDB_INVALID_HOST_THREAD), |
Greg Clayton | 5a9f85c | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 169 | m_thread_ids (), |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 170 | m_continue_c_tids (), |
| 171 | m_continue_C_tids (), |
| 172 | m_continue_s_tids (), |
| 173 | m_continue_S_tids (), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 174 | m_dispatch_queue_offsets_addr (LLDB_INVALID_ADDRESS), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 175 | m_max_memory_size (512), |
Greg Clayton | bd5c23d | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 176 | m_addr_to_mmap_size (), |
| 177 | m_thread_create_bp_sp (), |
| 178 | m_waiting_for_attach (false) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | { |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 180 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit"); |
| 181 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue"); |
Jim Ingham | 7fa7b2f | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 182 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadDidExit, "async thread did exit"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | //---------------------------------------------------------------------- |
| 186 | // Destructor |
| 187 | //---------------------------------------------------------------------- |
| 188 | ProcessGDBRemote::~ProcessGDBRemote() |
| 189 | { |
| 190 | // m_mach_process.UnregisterNotificationCallbacks (this); |
| 191 | Clear(); |
Greg Clayton | 2f57db0 | 2011-10-01 00:45:15 +0000 | [diff] [blame] | 192 | // We need to call finalize on the process before destroying ourselves |
| 193 | // to make sure all of the broadcaster cleanup goes as planned. If we |
| 194 | // destruct this class, then Process::~Process() might have problems |
| 195 | // trying to fully destroy the broadcaster. |
| 196 | Finalize(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | //---------------------------------------------------------------------- |
| 200 | // PluginInterface |
| 201 | //---------------------------------------------------------------------- |
| 202 | const char * |
| 203 | ProcessGDBRemote::GetPluginName() |
| 204 | { |
| 205 | return "Process debugging plug-in that uses the GDB remote protocol"; |
| 206 | } |
| 207 | |
| 208 | const char * |
| 209 | ProcessGDBRemote::GetShortPluginName() |
| 210 | { |
| 211 | return GetPluginNameStatic(); |
| 212 | } |
| 213 | |
| 214 | uint32_t |
| 215 | ProcessGDBRemote::GetPluginVersion() |
| 216 | { |
| 217 | return 1; |
| 218 | } |
| 219 | |
| 220 | void |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 221 | ProcessGDBRemote::BuildDynamicRegisterInfo (bool force) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 222 | { |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 223 | if (!force && m_register_info.GetNumRegisters() > 0) |
| 224 | return; |
| 225 | |
| 226 | char packet[128]; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 227 | m_register_info.Clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | uint32_t reg_offset = 0; |
| 229 | uint32_t reg_num = 0; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 230 | StringExtractorGDBRemote::ResponseType response_type; |
| 231 | for (response_type = StringExtractorGDBRemote::eResponse; |
| 232 | response_type == StringExtractorGDBRemote::eResponse; |
| 233 | ++reg_num) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 234 | { |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 235 | const int packet_len = ::snprintf (packet, sizeof(packet), "qRegisterInfo%x", reg_num); |
| 236 | assert (packet_len < sizeof(packet)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 237 | StringExtractorGDBRemote response; |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 238 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, false)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 239 | { |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 240 | response_type = response.GetResponseType(); |
| 241 | if (response_type == StringExtractorGDBRemote::eResponse) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 242 | { |
| 243 | std::string name; |
| 244 | std::string value; |
| 245 | ConstString reg_name; |
| 246 | ConstString alt_name; |
| 247 | ConstString set_name; |
| 248 | RegisterInfo reg_info = { NULL, // Name |
| 249 | NULL, // Alt name |
| 250 | 0, // byte size |
| 251 | reg_offset, // offset |
| 252 | eEncodingUint, // encoding |
| 253 | eFormatHex, // formate |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 254 | { |
| 255 | LLDB_INVALID_REGNUM, // GCC reg num |
| 256 | LLDB_INVALID_REGNUM, // DWARF reg num |
| 257 | LLDB_INVALID_REGNUM, // generic reg num |
Jason Molenda | 3a4ea24 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 258 | reg_num, // GDB reg num |
| 259 | reg_num // native register number |
Greg Clayton | cd33042 | 2012-02-29 19:27:27 +0000 | [diff] [blame] | 260 | }, |
| 261 | NULL, |
| 262 | NULL |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | while (response.GetNameColonValue(name, value)) |
| 266 | { |
| 267 | if (name.compare("name") == 0) |
| 268 | { |
| 269 | reg_name.SetCString(value.c_str()); |
| 270 | } |
| 271 | else if (name.compare("alt-name") == 0) |
| 272 | { |
| 273 | alt_name.SetCString(value.c_str()); |
| 274 | } |
| 275 | else if (name.compare("bitsize") == 0) |
| 276 | { |
| 277 | reg_info.byte_size = Args::StringToUInt32(value.c_str(), 0, 0) / CHAR_BIT; |
| 278 | } |
| 279 | else if (name.compare("offset") == 0) |
| 280 | { |
| 281 | uint32_t offset = Args::StringToUInt32(value.c_str(), UINT32_MAX, 0); |
Jason Molenda | 53d9686 | 2010-06-11 23:44:18 +0000 | [diff] [blame] | 282 | if (reg_offset != offset) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 283 | { |
| 284 | reg_offset = offset; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | else if (name.compare("encoding") == 0) |
| 288 | { |
| 289 | if (value.compare("uint") == 0) |
| 290 | reg_info.encoding = eEncodingUint; |
| 291 | else if (value.compare("sint") == 0) |
| 292 | reg_info.encoding = eEncodingSint; |
| 293 | else if (value.compare("ieee754") == 0) |
| 294 | reg_info.encoding = eEncodingIEEE754; |
| 295 | else if (value.compare("vector") == 0) |
| 296 | reg_info.encoding = eEncodingVector; |
| 297 | } |
| 298 | else if (name.compare("format") == 0) |
| 299 | { |
| 300 | if (value.compare("binary") == 0) |
| 301 | reg_info.format = eFormatBinary; |
| 302 | else if (value.compare("decimal") == 0) |
| 303 | reg_info.format = eFormatDecimal; |
| 304 | else if (value.compare("hex") == 0) |
| 305 | reg_info.format = eFormatHex; |
| 306 | else if (value.compare("float") == 0) |
| 307 | reg_info.format = eFormatFloat; |
| 308 | else if (value.compare("vector-sint8") == 0) |
| 309 | reg_info.format = eFormatVectorOfSInt8; |
| 310 | else if (value.compare("vector-uint8") == 0) |
| 311 | reg_info.format = eFormatVectorOfUInt8; |
| 312 | else if (value.compare("vector-sint16") == 0) |
| 313 | reg_info.format = eFormatVectorOfSInt16; |
| 314 | else if (value.compare("vector-uint16") == 0) |
| 315 | reg_info.format = eFormatVectorOfUInt16; |
| 316 | else if (value.compare("vector-sint32") == 0) |
| 317 | reg_info.format = eFormatVectorOfSInt32; |
| 318 | else if (value.compare("vector-uint32") == 0) |
| 319 | reg_info.format = eFormatVectorOfUInt32; |
| 320 | else if (value.compare("vector-float32") == 0) |
| 321 | reg_info.format = eFormatVectorOfFloat32; |
| 322 | else if (value.compare("vector-uint128") == 0) |
| 323 | reg_info.format = eFormatVectorOfUInt128; |
| 324 | } |
| 325 | else if (name.compare("set") == 0) |
| 326 | { |
| 327 | set_name.SetCString(value.c_str()); |
| 328 | } |
| 329 | else if (name.compare("gcc") == 0) |
| 330 | { |
| 331 | reg_info.kinds[eRegisterKindGCC] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0); |
| 332 | } |
| 333 | else if (name.compare("dwarf") == 0) |
| 334 | { |
| 335 | reg_info.kinds[eRegisterKindDWARF] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0); |
| 336 | } |
| 337 | else if (name.compare("generic") == 0) |
| 338 | { |
| 339 | if (value.compare("pc") == 0) |
| 340 | reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_PC; |
| 341 | else if (value.compare("sp") == 0) |
| 342 | reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_SP; |
| 343 | else if (value.compare("fp") == 0) |
| 344 | reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FP; |
| 345 | else if (value.compare("ra") == 0) |
| 346 | reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_RA; |
| 347 | else if (value.compare("flags") == 0) |
| 348 | reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_FLAGS; |
Greg Clayton | 5a26910 | 2011-05-22 04:32:55 +0000 | [diff] [blame] | 349 | else if (value.find("arg") == 0) |
| 350 | { |
| 351 | if (value.size() == 4) |
| 352 | { |
| 353 | switch (value[3]) |
| 354 | { |
| 355 | case '1': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG1; break; |
| 356 | case '2': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG2; break; |
| 357 | case '3': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG3; break; |
| 358 | case '4': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG4; break; |
| 359 | case '5': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG5; break; |
| 360 | case '6': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG6; break; |
| 361 | case '7': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG7; break; |
| 362 | case '8': reg_info.kinds[eRegisterKindGeneric] = LLDB_REGNUM_GENERIC_ARG8; break; |
| 363 | } |
| 364 | } |
| 365 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Jason Molenda | 53d9686 | 2010-06-11 23:44:18 +0000 | [diff] [blame] | 369 | reg_info.byte_offset = reg_offset; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 370 | assert (reg_info.byte_size != 0); |
| 371 | reg_offset += reg_info.byte_size; |
| 372 | m_register_info.AddRegister(reg_info, reg_name, alt_name, set_name); |
| 373 | } |
| 374 | } |
| 375 | else |
| 376 | { |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 377 | response_type = StringExtractorGDBRemote::eError; |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 378 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
Johnny Chen | b7cdd6c | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 382 | // We didn't get anything if the accumulated reg_num is zero. See if we are |
| 383 | // debugging ARM and fill with a hard coded register set until we can get an |
| 384 | // updated debugserver down on the devices. |
| 385 | // On the other hand, if the accumulated reg_num is positive, see if we can |
| 386 | // add composite registers to the existing primordial ones. |
| 387 | bool from_scratch = (reg_num == 0); |
| 388 | |
| 389 | const ArchSpec &target_arch = GetTarget().GetArchitecture(); |
| 390 | const ArchSpec &remote_arch = m_gdb_comm.GetHostArchitecture(); |
| 391 | if (!target_arch.IsValid()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 392 | { |
Johnny Chen | b7cdd6c | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 393 | if (remote_arch.IsValid() |
| 394 | && remote_arch.GetMachine() == llvm::Triple::arm |
| 395 | && remote_arch.GetTriple().getVendor() == llvm::Triple::Apple) |
| 396 | m_register_info.HardcodeARMRegisters(from_scratch); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 397 | } |
Johnny Chen | b7cdd6c | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 398 | else if (target_arch.GetMachine() == llvm::Triple::arm) |
| 399 | { |
| 400 | m_register_info.HardcodeARMRegisters(from_scratch); |
| 401 | } |
| 402 | |
Johnny Chen | d2e3066 | 2012-05-22 00:57:05 +0000 | [diff] [blame] | 403 | // Add some convenience registers (eax, ebx, ecx, edx, esi, edi, ebp, esp) to x86_64. |
| 404 | if (target_arch.IsValid() && target_arch.GetMachine() == llvm::Triple::x86_64) |
| 405 | m_register_info.Addx86_64ConvenienceRegisters(); |
| 406 | |
Johnny Chen | b7cdd6c | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 407 | // At this point, we can finalize our register info. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 408 | m_register_info.Finalize (); |
| 409 | } |
| 410 | |
| 411 | Error |
| 412 | ProcessGDBRemote::WillLaunch (Module* module) |
| 413 | { |
| 414 | return WillLaunchOrAttach (); |
| 415 | } |
| 416 | |
| 417 | Error |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 418 | ProcessGDBRemote::WillAttachToProcessWithID (lldb::pid_t pid) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 419 | { |
| 420 | return WillLaunchOrAttach (); |
| 421 | } |
| 422 | |
| 423 | Error |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 424 | ProcessGDBRemote::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 425 | { |
| 426 | return WillLaunchOrAttach (); |
| 427 | } |
| 428 | |
| 429 | Error |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 430 | ProcessGDBRemote::DoConnectRemote (const char *remote_url) |
| 431 | { |
| 432 | Error error (WillLaunchOrAttach ()); |
| 433 | |
| 434 | if (error.Fail()) |
| 435 | return error; |
| 436 | |
Greg Clayton | 180546b | 2011-04-30 01:09:13 +0000 | [diff] [blame] | 437 | error = ConnectToDebugserver (remote_url); |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 438 | |
| 439 | if (error.Fail()) |
| 440 | return error; |
| 441 | StartAsyncThread (); |
| 442 | |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 443 | lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID (); |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 444 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 445 | { |
| 446 | // We don't have a valid process ID, so note that we are connected |
| 447 | // and could now request to launch or attach, or get remote process |
| 448 | // listings... |
| 449 | SetPrivateState (eStateConnected); |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | // We have a valid process |
| 454 | SetID (pid); |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 455 | GetThreadList(); |
Greg Clayton | 261a18b | 2011-06-02 22:22:38 +0000 | [diff] [blame] | 456 | if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false)) |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 457 | { |
Greg Clayton | 261a18b | 2011-06-02 22:22:38 +0000 | [diff] [blame] | 458 | const StateType state = SetThreadStopInfo (m_last_stop_packet); |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 459 | if (state == eStateStopped) |
| 460 | { |
| 461 | SetPrivateState (state); |
| 462 | } |
| 463 | else |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 464 | error.SetErrorStringWithFormat ("Process %llu was reported after connecting to '%s', but state was not stopped: %s", pid, remote_url, StateAsCString (state)); |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 465 | } |
| 466 | else |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 467 | error.SetErrorStringWithFormat ("Process %llu was reported after connecting to '%s', but no stop reply packet was received", pid, remote_url); |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 468 | } |
Jason Molenda | cb740b3 | 2012-05-03 22:37:30 +0000 | [diff] [blame] | 469 | |
| 470 | if (error.Success() |
| 471 | && !GetTarget().GetArchitecture().IsValid() |
| 472 | && m_gdb_comm.GetHostArchitecture().IsValid()) |
| 473 | { |
| 474 | GetTarget().SetArchitecture(m_gdb_comm.GetHostArchitecture()); |
| 475 | } |
| 476 | |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 477 | return error; |
| 478 | } |
| 479 | |
| 480 | Error |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 481 | ProcessGDBRemote::WillLaunchOrAttach () |
| 482 | { |
| 483 | Error error; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 484 | m_stdio_communication.Clear (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 485 | return error; |
| 486 | } |
| 487 | |
| 488 | //---------------------------------------------------------------------- |
| 489 | // Process Control |
| 490 | //---------------------------------------------------------------------- |
| 491 | Error |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 492 | ProcessGDBRemote::DoLaunch (Module *exe_module, const ProcessLaunchInfo &launch_info) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 493 | { |
Greg Clayton | 4b40711 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 494 | Error error; |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 495 | |
| 496 | uint32_t launch_flags = launch_info.GetFlags().Get(); |
| 497 | const char *stdin_path = NULL; |
| 498 | const char *stdout_path = NULL; |
| 499 | const char *stderr_path = NULL; |
| 500 | const char *working_dir = launch_info.GetWorkingDirectory(); |
| 501 | |
| 502 | const ProcessLaunchInfo::FileAction *file_action; |
| 503 | file_action = launch_info.GetFileActionForFD (STDIN_FILENO); |
| 504 | if (file_action) |
| 505 | { |
| 506 | if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen) |
| 507 | stdin_path = file_action->GetPath(); |
| 508 | } |
| 509 | file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); |
| 510 | if (file_action) |
| 511 | { |
| 512 | if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen) |
| 513 | stdout_path = file_action->GetPath(); |
| 514 | } |
| 515 | file_action = launch_info.GetFileActionForFD (STDERR_FILENO); |
| 516 | if (file_action) |
| 517 | { |
| 518 | if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen) |
| 519 | stderr_path = file_action->GetPath(); |
| 520 | } |
| 521 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 522 | // ::LogSetBitMask (GDBR_LOG_DEFAULT); |
| 523 | // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD); |
| 524 | // ::LogSetLogFile ("/dev/stdout"); |
Greg Clayton | 716cefb | 2011-08-09 05:20:29 +0000 | [diff] [blame] | 525 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 526 | |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 527 | ObjectFile * object_file = exe_module->GetObjectFile(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 528 | if (object_file) |
| 529 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 530 | char host_port[128]; |
| 531 | snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ()); |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 532 | char connect_url[128]; |
| 533 | snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 534 | |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 535 | // Make sure we aren't already connected? |
| 536 | if (!m_gdb_comm.IsConnected()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 537 | { |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 538 | error = StartDebugserverProcess (host_port, launch_info); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 539 | if (error.Fail()) |
Greg Clayton | 716cefb | 2011-08-09 05:20:29 +0000 | [diff] [blame] | 540 | { |
Johnny Chen | c143d62 | 2011-08-09 18:56:45 +0000 | [diff] [blame] | 541 | if (log) |
| 542 | log->Printf("failed to start debugserver process: %s", error.AsCString()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 543 | return error; |
Greg Clayton | 716cefb | 2011-08-09 05:20:29 +0000 | [diff] [blame] | 544 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 545 | |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 546 | error = ConnectToDebugserver (connect_url); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | if (error.Success()) |
| 550 | { |
| 551 | lldb_utility::PseudoTerminal pty; |
| 552 | const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0; |
Greg Clayton | afb8186 | 2011-03-02 21:34:46 +0000 | [diff] [blame] | 553 | |
| 554 | // If the debugserver is local and we aren't disabling STDIO, lets use |
| 555 | // a pseudo terminal to instead of relying on the 'O' packets for stdio |
| 556 | // since 'O' packets can really slow down debugging if the inferior |
| 557 | // does a lot of output. |
Greg Clayton | b474782 | 2011-06-24 22:32:10 +0000 | [diff] [blame] | 558 | PlatformSP platform_sp (m_target.GetPlatform()); |
| 559 | if (platform_sp && platform_sp->IsHost() && !disable_stdio) |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 560 | { |
| 561 | const char *slave_name = NULL; |
| 562 | if (stdin_path == NULL || stdout_path == NULL || stderr_path == NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 563 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 564 | if (pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, NULL, 0)) |
| 565 | slave_name = pty.GetSlaveName (NULL, 0); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 566 | } |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 567 | if (stdin_path == NULL) |
| 568 | stdin_path = slave_name; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 569 | |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 570 | if (stdout_path == NULL) |
| 571 | stdout_path = slave_name; |
| 572 | |
| 573 | if (stderr_path == NULL) |
| 574 | stderr_path = slave_name; |
| 575 | } |
| 576 | |
Greg Clayton | afb8186 | 2011-03-02 21:34:46 +0000 | [diff] [blame] | 577 | // Set STDIN to /dev/null if we want STDIO disabled or if either |
| 578 | // STDOUT or STDERR have been set to something and STDIN hasn't |
| 579 | if (disable_stdio || (stdin_path == NULL && (stdout_path || stderr_path))) |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 580 | stdin_path = "/dev/null"; |
| 581 | |
Greg Clayton | afb8186 | 2011-03-02 21:34:46 +0000 | [diff] [blame] | 582 | // Set STDOUT to /dev/null if we want STDIO disabled or if either |
| 583 | // STDIN or STDERR have been set to something and STDOUT hasn't |
| 584 | if (disable_stdio || (stdout_path == NULL && (stdin_path || stderr_path))) |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 585 | stdout_path = "/dev/null"; |
| 586 | |
Greg Clayton | afb8186 | 2011-03-02 21:34:46 +0000 | [diff] [blame] | 587 | // Set STDERR to /dev/null if we want STDIO disabled or if either |
| 588 | // STDIN or STDOUT have been set to something and STDERR hasn't |
| 589 | if (disable_stdio || (stderr_path == NULL && (stdin_path || stdout_path))) |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 590 | stderr_path = "/dev/null"; |
| 591 | |
| 592 | if (stdin_path) |
| 593 | m_gdb_comm.SetSTDIN (stdin_path); |
| 594 | if (stdout_path) |
| 595 | m_gdb_comm.SetSTDOUT (stdout_path); |
| 596 | if (stderr_path) |
| 597 | m_gdb_comm.SetSTDERR (stderr_path); |
| 598 | |
| 599 | m_gdb_comm.SetDisableASLR (launch_flags & eLaunchFlagDisableASLR); |
| 600 | |
Greg Clayton | a458240 | 2011-05-08 04:53:50 +0000 | [diff] [blame] | 601 | m_gdb_comm.SendLaunchArchPacket (m_target.GetArchitecture().GetArchitectureName()); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 602 | |
| 603 | if (working_dir && working_dir[0]) |
| 604 | { |
| 605 | m_gdb_comm.SetWorkingDir (working_dir); |
| 606 | } |
| 607 | |
| 608 | // Send the environment and the program + arguments after we connect |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 609 | const Args &environment = launch_info.GetEnvironmentEntries(); |
| 610 | if (environment.GetArgumentCount()) |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 611 | { |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 612 | size_t num_environment_entries = environment.GetArgumentCount(); |
| 613 | for (size_t i=0; i<num_environment_entries; ++i) |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 614 | { |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 615 | const char *env_entry = environment.GetArgumentAtIndex(i); |
| 616 | if (env_entry == NULL || m_gdb_comm.SendEnvironmentPacket(env_entry) != 0) |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 617 | break; |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 618 | } |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 619 | } |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 620 | |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 621 | const uint32_t old_packet_timeout = m_gdb_comm.SetPacketTimeout (10); |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 622 | int arg_packet_err = m_gdb_comm.SendArgumentsPacket (launch_info.GetArguments().GetConstArgumentVector()); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 623 | if (arg_packet_err == 0) |
| 624 | { |
| 625 | std::string error_str; |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 626 | if (m_gdb_comm.GetLaunchSuccess (error_str)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 627 | { |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 628 | SetID (m_gdb_comm.GetCurrentProcessID ()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 629 | } |
| 630 | else |
| 631 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 632 | error.SetErrorString (error_str.c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 633 | } |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 634 | } |
| 635 | else |
| 636 | { |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 637 | error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 638 | } |
Greg Clayton | 7c4fc6e | 2011-08-10 22:05:39 +0000 | [diff] [blame] | 639 | |
| 640 | m_gdb_comm.SetPacketTimeout (old_packet_timeout); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 641 | |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 642 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 643 | { |
Johnny Chen | c143d62 | 2011-08-09 18:56:45 +0000 | [diff] [blame] | 644 | if (log) |
| 645 | log->Printf("failed to connect to debugserver: %s", error.AsCString()); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 646 | KillDebugserverProcess (); |
| 647 | return error; |
| 648 | } |
| 649 | |
Greg Clayton | 261a18b | 2011-06-02 22:22:38 +0000 | [diff] [blame] | 650 | if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false)) |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 651 | { |
Greg Clayton | 261a18b | 2011-06-02 22:22:38 +0000 | [diff] [blame] | 652 | SetPrivateState (SetThreadStopInfo (m_last_stop_packet)); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 653 | |
| 654 | if (!disable_stdio) |
| 655 | { |
| 656 | if (pty.GetMasterFileDescriptor() != lldb_utility::PseudoTerminal::invalid_fd) |
Greg Clayton | 464c616 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 657 | SetSTDIOFileDescriptor (pty.ReleaseMasterFileDescriptor()); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 658 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
Greg Clayton | 716cefb | 2011-08-09 05:20:29 +0000 | [diff] [blame] | 661 | else |
| 662 | { |
Johnny Chen | c143d62 | 2011-08-09 18:56:45 +0000 | [diff] [blame] | 663 | if (log) |
| 664 | log->Printf("failed to connect to debugserver: %s", error.AsCString()); |
Greg Clayton | 716cefb | 2011-08-09 05:20:29 +0000 | [diff] [blame] | 665 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 666 | } |
| 667 | else |
| 668 | { |
| 669 | // Set our user ID to an invalid process ID. |
| 670 | SetID(LLDB_INVALID_PROCESS_ID); |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 671 | error.SetErrorStringWithFormat ("failed to get object file from '%s' for arch %s", |
| 672 | exe_module->GetFileSpec().GetFilename().AsCString(), |
| 673 | exe_module->GetArchitecture().GetArchitectureName()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 674 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 675 | return error; |
Greg Clayton | 4b40711 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 676 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | |
| 680 | Error |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 681 | ProcessGDBRemote::ConnectToDebugserver (const char *connect_url) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 682 | { |
| 683 | Error error; |
| 684 | // Sleep and wait a bit for debugserver to start to listen... |
| 685 | std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); |
| 686 | if (conn_ap.get()) |
| 687 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 688 | const uint32_t max_retry_count = 50; |
| 689 | uint32_t retry_count = 0; |
| 690 | while (!m_gdb_comm.IsConnected()) |
| 691 | { |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 692 | if (conn_ap->Connect(connect_url, &error) == eConnectionStatusSuccess) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 693 | { |
| 694 | m_gdb_comm.SetConnection (conn_ap.release()); |
| 695 | break; |
| 696 | } |
| 697 | retry_count++; |
| 698 | |
| 699 | if (retry_count >= max_retry_count) |
| 700 | break; |
| 701 | |
| 702 | usleep (100000); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | if (!m_gdb_comm.IsConnected()) |
| 707 | { |
| 708 | if (error.Success()) |
| 709 | error.SetErrorString("not connected to remote gdb server"); |
| 710 | return error; |
| 711 | } |
| 712 | |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 713 | // We always seem to be able to open a connection to a local port |
| 714 | // so we need to make sure we can then send data to it. If we can't |
| 715 | // then we aren't actually connected to anything, so try and do the |
| 716 | // handshake with the remote GDB server and make sure that goes |
| 717 | // alright. |
| 718 | if (!m_gdb_comm.HandshakeWithServer (NULL)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 719 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 720 | m_gdb_comm.Disconnect(); |
| 721 | if (error.Success()) |
| 722 | error.SetErrorString("not connected to remote gdb server"); |
| 723 | return error; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 724 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 725 | m_gdb_comm.ResetDiscoverableSettings(); |
| 726 | m_gdb_comm.QueryNoAckModeSupported (); |
| 727 | m_gdb_comm.GetThreadSuffixSupported (); |
Greg Clayton | a1f645e | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 728 | m_gdb_comm.GetListThreadsInStopReplySupported (); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 729 | m_gdb_comm.GetHostInfo (); |
| 730 | m_gdb_comm.GetVContSupported ('c'); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 731 | return error; |
| 732 | } |
| 733 | |
| 734 | void |
| 735 | ProcessGDBRemote::DidLaunchOrAttach () |
| 736 | { |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 737 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
| 738 | if (log) |
| 739 | log->Printf ("ProcessGDBRemote::DidLaunch()"); |
Greg Clayton | 75c703d | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 740 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 741 | { |
| 742 | m_dispatch_queue_offsets_addr = LLDB_INVALID_ADDRESS; |
| 743 | |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 744 | BuildDynamicRegisterInfo (false); |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 745 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 746 | // See if the GDB server supports the qHostInfo information |
Greg Clayton | fc7920f | 2011-02-09 03:09:55 +0000 | [diff] [blame] | 747 | |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 748 | const ArchSpec &gdb_remote_arch = m_gdb_comm.GetHostArchitecture(); |
| 749 | if (gdb_remote_arch.IsValid()) |
Greg Clayton | fc7920f | 2011-02-09 03:09:55 +0000 | [diff] [blame] | 750 | { |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 751 | ArchSpec &target_arch = GetTarget().GetArchitecture(); |
| 752 | |
| 753 | if (target_arch.IsValid()) |
| 754 | { |
| 755 | // If the remote host is ARM and we have apple as the vendor, then |
| 756 | // ARM executables and shared libraries can have mixed ARM architectures. |
| 757 | // You can have an armv6 executable, and if the host is armv7, then the |
| 758 | // system will load the best possible architecture for all shared libraries |
| 759 | // it has, so we really need to take the remote host architecture as our |
| 760 | // defacto architecture in this case. |
| 761 | |
| 762 | if (gdb_remote_arch.GetMachine() == llvm::Triple::arm && |
| 763 | gdb_remote_arch.GetTriple().getVendor() == llvm::Triple::Apple) |
| 764 | { |
| 765 | target_arch = gdb_remote_arch; |
| 766 | } |
| 767 | else |
| 768 | { |
| 769 | // Fill in what is missing in the triple |
| 770 | const llvm::Triple &remote_triple = gdb_remote_arch.GetTriple(); |
| 771 | llvm::Triple &target_triple = target_arch.GetTriple(); |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 772 | if (target_triple.getVendorName().size() == 0) |
| 773 | { |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 774 | target_triple.setVendor (remote_triple.getVendor()); |
| 775 | |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 776 | if (target_triple.getOSName().size() == 0) |
| 777 | { |
| 778 | target_triple.setOS (remote_triple.getOS()); |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 779 | |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 780 | if (target_triple.getEnvironmentName().size() == 0) |
| 781 | target_triple.setEnvironment (remote_triple.getEnvironment()); |
| 782 | } |
| 783 | } |
Greg Clayton | cb8977d | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | else |
| 787 | { |
| 788 | // The target doesn't have a valid architecture yet, set it from |
| 789 | // the architecture we got from the remote GDB server |
| 790 | target_arch = gdb_remote_arch; |
| 791 | } |
Greg Clayton | fc7920f | 2011-02-09 03:09:55 +0000 | [diff] [blame] | 792 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
| 796 | void |
| 797 | ProcessGDBRemote::DidLaunch () |
| 798 | { |
| 799 | DidLaunchOrAttach (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | Error |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 803 | ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 804 | { |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 805 | ProcessAttachInfo attach_info; |
| 806 | return DoAttachToProcessWithID(attach_pid, attach_info); |
| 807 | } |
| 808 | |
| 809 | Error |
| 810 | ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info) |
| 811 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 812 | Error error; |
| 813 | // Clear out and clean up from any current state |
| 814 | Clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 815 | if (attach_pid != LLDB_INVALID_PROCESS_ID) |
| 816 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 817 | // Make sure we aren't already connected? |
| 818 | if (!m_gdb_comm.IsConnected()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 819 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 820 | char host_port[128]; |
| 821 | snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ()); |
| 822 | char connect_url[128]; |
| 823 | snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 824 | |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 825 | error = StartDebugserverProcess (host_port, attach_info); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 826 | |
| 827 | if (error.Fail()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 828 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 829 | const char *error_string = error.AsCString(); |
| 830 | if (error_string == NULL) |
| 831 | error_string = "unable to launch " DEBUGSERVER_BASENAME; |
| 832 | |
| 833 | SetExitStatus (-1, error_string); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 834 | } |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 835 | else |
| 836 | { |
| 837 | error = ConnectToDebugserver (connect_url); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | if (error.Success()) |
| 842 | { |
| 843 | char packet[64]; |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 844 | const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%llx", attach_pid); |
Greg Clayton | 489575c | 2011-11-19 02:11:30 +0000 | [diff] [blame] | 845 | SetID (attach_pid); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 846 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet, packet_len)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 847 | } |
| 848 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 849 | return error; |
| 850 | } |
| 851 | |
| 852 | size_t |
| 853 | ProcessGDBRemote::AttachInputReaderCallback |
| 854 | ( |
| 855 | void *baton, |
| 856 | InputReader *reader, |
| 857 | lldb::InputReaderAction notification, |
| 858 | const char *bytes, |
| 859 | size_t bytes_len |
| 860 | ) |
| 861 | { |
| 862 | if (notification == eInputReaderGotToken) |
| 863 | { |
| 864 | ProcessGDBRemote *gdb_process = (ProcessGDBRemote *)baton; |
| 865 | if (gdb_process->m_waiting_for_attach) |
| 866 | gdb_process->m_waiting_for_attach = false; |
| 867 | reader->SetIsDone(true); |
| 868 | return 1; |
| 869 | } |
| 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | Error |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 874 | ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 875 | { |
| 876 | Error error; |
| 877 | // Clear out and clean up from any current state |
| 878 | Clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 879 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 880 | if (process_name && process_name[0]) |
| 881 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 882 | // Make sure we aren't already connected? |
| 883 | if (!m_gdb_comm.IsConnected()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 884 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 885 | char host_port[128]; |
| 886 | snprintf (host_port, sizeof(host_port), "localhost:%u", get_random_port ()); |
| 887 | char connect_url[128]; |
| 888 | snprintf (connect_url, sizeof(connect_url), "connect://%s", host_port); |
| 889 | |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 890 | error = StartDebugserverProcess (host_port, attach_info); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 891 | if (error.Fail()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 892 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 893 | const char *error_string = error.AsCString(); |
| 894 | if (error_string == NULL) |
| 895 | error_string = "unable to launch " DEBUGSERVER_BASENAME; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 896 | |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 897 | SetExitStatus (-1, error_string); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 898 | } |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 899 | else |
| 900 | { |
| 901 | error = ConnectToDebugserver (connect_url); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | if (error.Success()) |
| 906 | { |
| 907 | StreamString packet; |
| 908 | |
| 909 | if (wait_for_launch) |
| 910 | packet.PutCString("vAttachWait"); |
| 911 | else |
| 912 | packet.PutCString("vAttachName"); |
| 913 | packet.PutChar(';'); |
| 914 | packet.PutBytesAsRawHex8(process_name, strlen(process_name), lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder()); |
| 915 | |
| 916 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet.GetData(), packet.GetSize())); |
| 917 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 918 | } |
| 919 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 920 | return error; |
| 921 | } |
| 922 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 923 | |
| 924 | void |
| 925 | ProcessGDBRemote::DidAttach () |
| 926 | { |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 927 | DidLaunchOrAttach (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | Error |
| 931 | ProcessGDBRemote::WillResume () |
| 932 | { |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 933 | m_continue_c_tids.clear(); |
| 934 | m_continue_C_tids.clear(); |
| 935 | m_continue_s_tids.clear(); |
| 936 | m_continue_S_tids.clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 937 | return Error(); |
| 938 | } |
| 939 | |
| 940 | Error |
| 941 | ProcessGDBRemote::DoResume () |
| 942 | { |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 943 | Error error; |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 944 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
| 945 | if (log) |
| 946 | log->Printf ("ProcessGDBRemote::Resume()"); |
Greg Clayton | b749a26 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 947 | |
| 948 | Listener listener ("gdb-remote.resume-packet-sent"); |
| 949 | if (listener.StartListeningForEvents (&m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent)) |
| 950 | { |
Jim Ingham | 7fa7b2f | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 951 | listener.StartListeningForEvents (&m_async_broadcaster, ProcessGDBRemote::eBroadcastBitAsyncThreadDidExit); |
| 952 | |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 953 | StreamString continue_packet; |
| 954 | bool continue_packet_error = false; |
| 955 | if (m_gdb_comm.HasAnyVContSupport ()) |
| 956 | { |
| 957 | continue_packet.PutCString ("vCont"); |
| 958 | |
| 959 | if (!m_continue_c_tids.empty()) |
| 960 | { |
| 961 | if (m_gdb_comm.GetVContSupported ('c')) |
| 962 | { |
| 963 | for (tid_collection::const_iterator t_pos = m_continue_c_tids.begin(), t_end = m_continue_c_tids.end(); t_pos != t_end; ++t_pos) |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 964 | continue_packet.Printf(";c:%4.4llx", *t_pos); |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 965 | } |
| 966 | else |
| 967 | continue_packet_error = true; |
| 968 | } |
| 969 | |
| 970 | if (!continue_packet_error && !m_continue_C_tids.empty()) |
| 971 | { |
| 972 | if (m_gdb_comm.GetVContSupported ('C')) |
| 973 | { |
| 974 | for (tid_sig_collection::const_iterator s_pos = m_continue_C_tids.begin(), s_end = m_continue_C_tids.end(); s_pos != s_end; ++s_pos) |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 975 | continue_packet.Printf(";C%2.2x:%4.4llx", s_pos->second, s_pos->first); |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 976 | } |
| 977 | else |
| 978 | continue_packet_error = true; |
| 979 | } |
Greg Clayton | b749a26 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 980 | |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 981 | if (!continue_packet_error && !m_continue_s_tids.empty()) |
| 982 | { |
| 983 | if (m_gdb_comm.GetVContSupported ('s')) |
| 984 | { |
| 985 | for (tid_collection::const_iterator t_pos = m_continue_s_tids.begin(), t_end = m_continue_s_tids.end(); t_pos != t_end; ++t_pos) |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 986 | continue_packet.Printf(";s:%4.4llx", *t_pos); |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 987 | } |
| 988 | else |
| 989 | continue_packet_error = true; |
| 990 | } |
| 991 | |
| 992 | if (!continue_packet_error && !m_continue_S_tids.empty()) |
| 993 | { |
| 994 | if (m_gdb_comm.GetVContSupported ('S')) |
| 995 | { |
| 996 | for (tid_sig_collection::const_iterator s_pos = m_continue_S_tids.begin(), s_end = m_continue_S_tids.end(); s_pos != s_end; ++s_pos) |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 997 | continue_packet.Printf(";S%2.2x:%4.4llx", s_pos->second, s_pos->first); |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 998 | } |
| 999 | else |
| 1000 | continue_packet_error = true; |
| 1001 | } |
| 1002 | |
| 1003 | if (continue_packet_error) |
| 1004 | continue_packet.GetString().clear(); |
| 1005 | } |
| 1006 | else |
| 1007 | continue_packet_error = true; |
| 1008 | |
| 1009 | if (continue_packet_error) |
| 1010 | { |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1011 | // Either no vCont support, or we tried to use part of the vCont |
| 1012 | // packet that wasn't supported by the remote GDB server. |
| 1013 | // We need to try and make a simple packet that can do our continue |
| 1014 | const size_t num_threads = GetThreadList().GetSize(); |
| 1015 | const size_t num_continue_c_tids = m_continue_c_tids.size(); |
| 1016 | const size_t num_continue_C_tids = m_continue_C_tids.size(); |
| 1017 | const size_t num_continue_s_tids = m_continue_s_tids.size(); |
| 1018 | const size_t num_continue_S_tids = m_continue_S_tids.size(); |
| 1019 | if (num_continue_c_tids > 0) |
| 1020 | { |
| 1021 | if (num_continue_c_tids == num_threads) |
| 1022 | { |
| 1023 | // All threads are resuming... |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1024 | m_gdb_comm.SetCurrentThreadForRun (-1); |
Greg Clayton | de1dd81 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1025 | continue_packet.PutChar ('c'); |
| 1026 | continue_packet_error = false; |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1027 | } |
| 1028 | else if (num_continue_c_tids == 1 && |
| 1029 | num_continue_C_tids == 0 && |
| 1030 | num_continue_s_tids == 0 && |
| 1031 | num_continue_S_tids == 0 ) |
| 1032 | { |
| 1033 | // Only one thread is continuing |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1034 | m_gdb_comm.SetCurrentThreadForRun (m_continue_c_tids.front()); |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1035 | continue_packet.PutChar ('c'); |
Greg Clayton | de1dd81 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1036 | continue_packet_error = false; |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | |
Greg Clayton | de1dd81 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1040 | if (continue_packet_error && num_continue_C_tids > 0) |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1041 | { |
Greg Clayton | de1dd81 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1042 | if ((num_continue_C_tids + num_continue_c_tids) == num_threads && |
| 1043 | num_continue_C_tids > 0 && |
| 1044 | num_continue_s_tids == 0 && |
| 1045 | num_continue_S_tids == 0 ) |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1046 | { |
| 1047 | const int continue_signo = m_continue_C_tids.front().second; |
Greg Clayton | de1dd81 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1048 | // Only one thread is continuing |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1049 | if (num_continue_C_tids > 1) |
| 1050 | { |
Greg Clayton | de1dd81 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1051 | // More that one thread with a signal, yet we don't have |
| 1052 | // vCont support and we are being asked to resume each |
| 1053 | // thread with a signal, we need to make sure they are |
| 1054 | // all the same signal, or we can't issue the continue |
| 1055 | // accurately with the current support... |
| 1056 | if (num_continue_C_tids > 1) |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1057 | { |
Greg Clayton | de1dd81 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1058 | continue_packet_error = false; |
| 1059 | for (size_t i=1; i<m_continue_C_tids.size(); ++i) |
| 1060 | { |
| 1061 | if (m_continue_C_tids[i].second != continue_signo) |
| 1062 | continue_packet_error = true; |
| 1063 | } |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1064 | } |
Greg Clayton | de1dd81 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1065 | if (!continue_packet_error) |
| 1066 | m_gdb_comm.SetCurrentThreadForRun (-1); |
| 1067 | } |
| 1068 | else |
| 1069 | { |
| 1070 | // Set the continue thread ID |
| 1071 | continue_packet_error = false; |
| 1072 | m_gdb_comm.SetCurrentThreadForRun (m_continue_C_tids.front().first); |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1073 | } |
| 1074 | if (!continue_packet_error) |
| 1075 | { |
| 1076 | // Add threads continuing with the same signo... |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1077 | continue_packet.Printf("C%2.2x", continue_signo); |
| 1078 | } |
| 1079 | } |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Greg Clayton | de1dd81 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1082 | if (continue_packet_error && num_continue_s_tids > 0) |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1083 | { |
| 1084 | if (num_continue_s_tids == num_threads) |
| 1085 | { |
| 1086 | // All threads are resuming... |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1087 | m_gdb_comm.SetCurrentThreadForRun (-1); |
Greg Clayton | de1dd81 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1088 | continue_packet.PutChar ('s'); |
| 1089 | continue_packet_error = false; |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1090 | } |
| 1091 | else if (num_continue_c_tids == 0 && |
| 1092 | num_continue_C_tids == 0 && |
| 1093 | num_continue_s_tids == 1 && |
| 1094 | num_continue_S_tids == 0 ) |
| 1095 | { |
| 1096 | // Only one thread is stepping |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1097 | m_gdb_comm.SetCurrentThreadForRun (m_continue_s_tids.front()); |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1098 | continue_packet.PutChar ('s'); |
Greg Clayton | de1dd81 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1099 | continue_packet_error = false; |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | if (!continue_packet_error && num_continue_S_tids > 0) |
| 1104 | { |
| 1105 | if (num_continue_S_tids == num_threads) |
| 1106 | { |
| 1107 | const int step_signo = m_continue_S_tids.front().second; |
| 1108 | // Are all threads trying to step with the same signal? |
Greg Clayton | de1dd81 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1109 | continue_packet_error = false; |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1110 | if (num_continue_S_tids > 1) |
| 1111 | { |
| 1112 | for (size_t i=1; i<num_threads; ++i) |
| 1113 | { |
| 1114 | if (m_continue_S_tids[i].second != step_signo) |
| 1115 | continue_packet_error = true; |
| 1116 | } |
| 1117 | } |
| 1118 | if (!continue_packet_error) |
| 1119 | { |
| 1120 | // Add threads stepping with the same signo... |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1121 | m_gdb_comm.SetCurrentThreadForRun (-1); |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1122 | continue_packet.Printf("S%2.2x", step_signo); |
| 1123 | } |
| 1124 | } |
| 1125 | else if (num_continue_c_tids == 0 && |
| 1126 | num_continue_C_tids == 0 && |
| 1127 | num_continue_s_tids == 0 && |
| 1128 | num_continue_S_tids == 1 ) |
| 1129 | { |
| 1130 | // Only one thread is stepping with signal |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1131 | m_gdb_comm.SetCurrentThreadForRun (m_continue_S_tids.front().first); |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1132 | continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second); |
Greg Clayton | de1dd81 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1133 | continue_packet_error = false; |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | if (continue_packet_error) |
| 1139 | { |
| 1140 | error.SetErrorString ("can't make continue packet for this resume"); |
| 1141 | } |
| 1142 | else |
| 1143 | { |
| 1144 | EventSP event_sp; |
| 1145 | TimeValue timeout; |
| 1146 | timeout = TimeValue::Now(); |
| 1147 | timeout.OffsetWithSeconds (5); |
Jim Ingham | 7fa7b2f | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1148 | if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread)) |
| 1149 | { |
| 1150 | error.SetErrorString ("Trying to resume but the async thread is dead."); |
| 1151 | if (log) |
| 1152 | log->Printf ("ProcessGDBRemote::DoResume: Trying to resume but the async thread is dead."); |
| 1153 | return error; |
| 1154 | } |
| 1155 | |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1156 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (continue_packet.GetData(), continue_packet.GetSize())); |
| 1157 | |
| 1158 | if (listener.WaitForEvent (&timeout, event_sp) == false) |
Jim Ingham | 7fa7b2f | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1159 | { |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1160 | error.SetErrorString("Resume timed out."); |
Jim Ingham | 7fa7b2f | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1161 | if (log) |
| 1162 | log->Printf ("ProcessGDBRemote::DoResume: Resume timed out."); |
| 1163 | } |
| 1164 | else if (event_sp->BroadcasterIs (&m_async_broadcaster)) |
| 1165 | { |
| 1166 | error.SetErrorString ("Broadcast continue, but the async thread was killed before we got an ack back."); |
| 1167 | if (log) |
| 1168 | log->Printf ("ProcessGDBRemote::DoResume: Broadcast continue, but the async thread was killed before we got an ack back."); |
| 1169 | return error; |
| 1170 | } |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1171 | } |
Greg Clayton | b749a26 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1174 | return error; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Greg Clayton | 5a9f85c | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1177 | void |
| 1178 | ProcessGDBRemote::ClearThreadIDList () |
| 1179 | { |
Greg Clayton | ff3448e | 2012-04-13 02:11:32 +0000 | [diff] [blame] | 1180 | Mutex::Locker locker(m_thread_list.GetMutex()); |
Greg Clayton | 5a9f85c | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1181 | m_thread_ids.clear(); |
| 1182 | } |
| 1183 | |
| 1184 | bool |
| 1185 | ProcessGDBRemote::UpdateThreadIDList () |
| 1186 | { |
Greg Clayton | ff3448e | 2012-04-13 02:11:32 +0000 | [diff] [blame] | 1187 | Mutex::Locker locker(m_thread_list.GetMutex()); |
Greg Clayton | 5a9f85c | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1188 | bool sequence_mutex_unavailable = false; |
| 1189 | m_gdb_comm.GetCurrentThreadIDs (m_thread_ids, sequence_mutex_unavailable); |
| 1190 | if (sequence_mutex_unavailable) |
| 1191 | { |
| 1192 | #if defined (LLDB_CONFIGURATION_DEBUG) |
| 1193 | assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"); |
| 1194 | #endif |
| 1195 | return false; // We just didn't get the list |
| 1196 | } |
| 1197 | return true; |
| 1198 | } |
| 1199 | |
Greg Clayton | ae93235 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1200 | bool |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1201 | ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1202 | { |
| 1203 | // locker will keep a mutex locked until it goes out of scope |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1204 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD)); |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 1205 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 1206 | log->Printf ("ProcessGDBRemote::%s (pid = %llu)", __FUNCTION__, GetID()); |
Greg Clayton | 5a9f85c | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1207 | |
| 1208 | size_t num_thread_ids = m_thread_ids.size(); |
| 1209 | // The "m_thread_ids" thread ID list should always be updated after each stop |
| 1210 | // reply packet, but in case it isn't, update it here. |
| 1211 | if (num_thread_ids == 0) |
| 1212 | { |
| 1213 | if (!UpdateThreadIDList ()) |
| 1214 | return false; |
| 1215 | num_thread_ids = m_thread_ids.size(); |
| 1216 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1217 | |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1218 | if (num_thread_ids > 0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1219 | { |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1220 | for (size_t i=0; i<num_thread_ids; ++i) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1221 | { |
Greg Clayton | 5a9f85c | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1222 | tid_t tid = m_thread_ids[i]; |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1223 | ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false)); |
| 1224 | if (!thread_sp) |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 1225 | thread_sp.reset (new ThreadGDBRemote (shared_from_this(), tid)); |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1226 | new_thread_list.AddThread(thread_sp); |
Greg Clayton | 4a60f9e | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 1227 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1228 | } |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1229 | |
Greg Clayton | ae93235 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1230 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | |
| 1234 | StateType |
| 1235 | ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet) |
| 1236 | { |
Greg Clayton | 261a18b | 2011-06-02 22:22:38 +0000 | [diff] [blame] | 1237 | stop_packet.SetFilePos (0); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1238 | const char stop_type = stop_packet.GetChar(); |
| 1239 | switch (stop_type) |
| 1240 | { |
| 1241 | case 'T': |
| 1242 | case 'S': |
| 1243 | { |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1244 | if (GetStopID() == 0) |
| 1245 | { |
| 1246 | // Our first stop, make sure we have a process ID, and also make |
| 1247 | // sure we know about our registers |
| 1248 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 1249 | { |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 1250 | lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID (); |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1251 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 1252 | SetID (pid); |
| 1253 | } |
| 1254 | BuildDynamicRegisterInfo (true); |
| 1255 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1256 | // Stop with signal and thread info |
| 1257 | const uint8_t signo = stop_packet.GetHexU8(); |
| 1258 | std::string name; |
| 1259 | std::string value; |
| 1260 | std::string thread_name; |
Greg Clayton | 6561155 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1261 | std::string reason; |
| 1262 | std::string description; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1263 | uint32_t exc_type = 0; |
Greg Clayton | 7661a98 | 2010-07-23 16:45:51 +0000 | [diff] [blame] | 1264 | std::vector<addr_t> exc_data; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1265 | addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS; |
| 1266 | uint32_t exc_data_count = 0; |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1267 | ThreadSP thread_sp; |
| 1268 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1269 | while (stop_packet.GetNameColonValue(name, value)) |
| 1270 | { |
| 1271 | if (name.compare("metype") == 0) |
| 1272 | { |
| 1273 | // exception type in big endian hex |
| 1274 | exc_type = Args::StringToUInt32 (value.c_str(), 0, 16); |
| 1275 | } |
| 1276 | else if (name.compare("mecount") == 0) |
| 1277 | { |
| 1278 | // exception count in big endian hex |
| 1279 | exc_data_count = Args::StringToUInt32 (value.c_str(), 0, 16); |
| 1280 | } |
| 1281 | else if (name.compare("medata") == 0) |
| 1282 | { |
| 1283 | // exception data in big endian hex |
| 1284 | exc_data.push_back(Args::StringToUInt64 (value.c_str(), 0, 16)); |
| 1285 | } |
| 1286 | else if (name.compare("thread") == 0) |
| 1287 | { |
| 1288 | // thread in big endian hex |
Greg Clayton | 5a9f85c | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1289 | lldb::tid_t tid = Args::StringToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
Greg Clayton | ffa43a6 | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1290 | // m_thread_list does have its own mutex, but we need to |
| 1291 | // hold onto the mutex between the call to m_thread_list.FindThreadByID(...) |
| 1292 | // and the m_thread_list.AddThread(...) so it doesn't change on us |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1293 | Mutex::Locker locker (m_thread_list.GetMutex ()); |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1294 | thread_sp = m_thread_list.FindThreadByID(tid, false); |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1295 | if (!thread_sp) |
| 1296 | { |
| 1297 | // Create the thread if we need to |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 1298 | thread_sp.reset (new ThreadGDBRemote (shared_from_this(), tid)); |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1299 | m_thread_list.AddThread(thread_sp); |
| 1300 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1301 | } |
Greg Clayton | 5a9f85c | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1302 | else if (name.compare("threads") == 0) |
| 1303 | { |
Greg Clayton | ff3448e | 2012-04-13 02:11:32 +0000 | [diff] [blame] | 1304 | Mutex::Locker locker(m_thread_list.GetMutex()); |
Greg Clayton | 5a9f85c | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1305 | m_thread_ids.clear(); |
Greg Clayton | a1f645e | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 1306 | // A comma separated list of all threads in the current |
| 1307 | // process that includes the thread for this stop reply |
| 1308 | // packet |
Greg Clayton | 5a9f85c | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1309 | size_t comma_pos; |
| 1310 | lldb::tid_t tid; |
| 1311 | while ((comma_pos = value.find(',')) != std::string::npos) |
| 1312 | { |
| 1313 | value[comma_pos] = '\0'; |
| 1314 | // thread in big endian hex |
| 1315 | tid = Args::StringToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
| 1316 | if (tid != LLDB_INVALID_THREAD_ID) |
| 1317 | m_thread_ids.push_back (tid); |
| 1318 | value.erase(0, comma_pos + 1); |
| 1319 | |
| 1320 | } |
| 1321 | tid = Args::StringToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
| 1322 | if (tid != LLDB_INVALID_THREAD_ID) |
| 1323 | m_thread_ids.push_back (tid); |
| 1324 | } |
Greg Clayton | 4862fa2 | 2011-01-08 03:17:57 +0000 | [diff] [blame] | 1325 | else if (name.compare("hexname") == 0) |
| 1326 | { |
| 1327 | StringExtractor name_extractor; |
| 1328 | // Swap "value" over into "name_extractor" |
| 1329 | name_extractor.GetStringRef().swap(value); |
| 1330 | // Now convert the HEX bytes into a string value |
| 1331 | name_extractor.GetHexByteString (value); |
| 1332 | thread_name.swap (value); |
| 1333 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1334 | else if (name.compare("name") == 0) |
| 1335 | { |
| 1336 | thread_name.swap (value); |
| 1337 | } |
Greg Clayton | 0a7f75f | 2010-09-09 06:32:46 +0000 | [diff] [blame] | 1338 | else if (name.compare("qaddr") == 0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1339 | { |
| 1340 | thread_dispatch_qaddr = Args::StringToUInt64 (value.c_str(), 0, 16); |
| 1341 | } |
Greg Clayton | 6561155 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1342 | else if (name.compare("reason") == 0) |
| 1343 | { |
| 1344 | reason.swap(value); |
| 1345 | } |
| 1346 | else if (name.compare("description") == 0) |
| 1347 | { |
| 1348 | StringExtractor desc_extractor; |
| 1349 | // Swap "value" over into "name_extractor" |
| 1350 | desc_extractor.GetStringRef().swap(value); |
| 1351 | // Now convert the HEX bytes into a string value |
| 1352 | desc_extractor.GetHexByteString (thread_name); |
| 1353 | } |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1354 | else if (name.size() == 2 && ::isxdigit(name[0]) && ::isxdigit(name[1])) |
| 1355 | { |
| 1356 | // We have a register number that contains an expedited |
| 1357 | // register value. Lets supply this register to our thread |
| 1358 | // so it won't have to go and read it. |
| 1359 | if (thread_sp) |
| 1360 | { |
| 1361 | uint32_t reg = Args::StringToUInt32 (name.c_str(), UINT32_MAX, 16); |
| 1362 | |
| 1363 | if (reg != UINT32_MAX) |
| 1364 | { |
| 1365 | StringExtractor reg_value_extractor; |
| 1366 | // Swap "value" over into "reg_value_extractor" |
| 1367 | reg_value_extractor.GetStringRef().swap(value); |
Greg Clayton | c3c4661 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1368 | if (!static_cast<ThreadGDBRemote *> (thread_sp.get())->PrivateSetRegisterValue (reg, reg_value_extractor)) |
| 1369 | { |
| 1370 | Host::SetCrashDescriptionWithFormat("Setting thread register '%s' (decoded to %u (0x%x)) with value '%s' for stop packet: '%s'", |
| 1371 | name.c_str(), |
| 1372 | reg, |
| 1373 | reg, |
| 1374 | reg_value_extractor.GetStringRef().c_str(), |
| 1375 | stop_packet.GetStringRef().c_str()); |
| 1376 | } |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1380 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1381 | |
| 1382 | if (thread_sp) |
| 1383 | { |
| 1384 | ThreadGDBRemote *gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get()); |
| 1385 | |
| 1386 | gdb_thread->SetThreadDispatchQAddr (thread_dispatch_qaddr); |
Jim Ingham | 9082c8a | 2011-01-28 02:23:12 +0000 | [diff] [blame] | 1387 | gdb_thread->SetName (thread_name.empty() ? NULL : thread_name.c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1388 | if (exc_type != 0) |
| 1389 | { |
Greg Clayton | bdcb6ab | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 1390 | const size_t exc_data_size = exc_data.size(); |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1391 | |
| 1392 | gdb_thread->SetStopInfo (StopInfoMachException::CreateStopReasonWithMachException (*thread_sp, |
| 1393 | exc_type, |
Greg Clayton | bdcb6ab | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 1394 | exc_data_size, |
| 1395 | exc_data_size >= 1 ? exc_data[0] : 0, |
Johnny Chen | 36889ad | 2011-09-17 01:05:03 +0000 | [diff] [blame] | 1396 | exc_data_size >= 2 ? exc_data[1] : 0, |
| 1397 | exc_data_size >= 3 ? exc_data[2] : 0)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1398 | } |
Greg Clayton | 6561155 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1399 | else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1400 | { |
Greg Clayton | 6561155 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1401 | bool handled = false; |
| 1402 | if (!reason.empty()) |
| 1403 | { |
| 1404 | if (reason.compare("trace") == 0) |
| 1405 | { |
| 1406 | gdb_thread->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp)); |
| 1407 | handled = true; |
| 1408 | } |
| 1409 | else if (reason.compare("breakpoint") == 0) |
| 1410 | { |
| 1411 | addr_t pc = gdb_thread->GetRegisterContext()->GetPC(); |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 1412 | lldb::BreakpointSiteSP bp_site_sp = gdb_thread->GetProcess()->GetBreakpointSiteList().FindByAddress(pc); |
Greg Clayton | 6561155 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1413 | if (bp_site_sp) |
| 1414 | { |
| 1415 | // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread, |
| 1416 | // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that |
| 1417 | // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc. |
| 1418 | if (bp_site_sp->ValidForThisThread (gdb_thread)) |
| 1419 | { |
| 1420 | gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID())); |
| 1421 | handled = true; |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | if (!handled) |
| 1426 | { |
| 1427 | gdb_thread->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp)); |
| 1428 | } |
| 1429 | } |
| 1430 | else if (reason.compare("trap") == 0) |
| 1431 | { |
| 1432 | // Let the trap just use the standard signal stop reason below... |
| 1433 | } |
| 1434 | else if (reason.compare("watchpoint") == 0) |
| 1435 | { |
| 1436 | break_id_t watch_id = LLDB_INVALID_WATCH_ID; |
| 1437 | // TODO: locate the watchpoint somehow... |
| 1438 | gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID (*thread_sp, watch_id)); |
| 1439 | handled = true; |
| 1440 | } |
| 1441 | else if (reason.compare("exception") == 0) |
| 1442 | { |
| 1443 | gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithException(*thread_sp, description.c_str())); |
| 1444 | handled = true; |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | if (signo) |
| 1449 | { |
| 1450 | if (signo == SIGTRAP) |
| 1451 | { |
| 1452 | // Currently we are going to assume SIGTRAP means we are either |
| 1453 | // hitting a breakpoint or hardware single stepping. |
| 1454 | addr_t pc = gdb_thread->GetRegisterContext()->GetPC(); |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 1455 | lldb::BreakpointSiteSP bp_site_sp = gdb_thread->GetProcess()->GetBreakpointSiteList().FindByAddress(pc); |
Greg Clayton | 6561155 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1456 | if (bp_site_sp) |
| 1457 | { |
| 1458 | // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread, |
| 1459 | // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that |
| 1460 | // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc. |
| 1461 | if (bp_site_sp->ValidForThisThread (gdb_thread)) |
| 1462 | { |
| 1463 | gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID())); |
| 1464 | handled = true; |
| 1465 | } |
| 1466 | } |
| 1467 | if (!handled) |
| 1468 | { |
| 1469 | // TODO: check for breakpoint or trap opcode in case there is a hard |
| 1470 | // coded software trap |
| 1471 | gdb_thread->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp)); |
| 1472 | handled = true; |
| 1473 | } |
| 1474 | } |
| 1475 | if (!handled) |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1476 | gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithSignal (*thread_sp, signo)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1477 | } |
| 1478 | else |
| 1479 | { |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1480 | StopInfoSP invalid_stop_info_sp; |
| 1481 | gdb_thread->SetStopInfo (invalid_stop_info_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1482 | } |
Greg Clayton | 6561155 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1483 | |
| 1484 | if (!description.empty()) |
| 1485 | { |
| 1486 | lldb::StopInfoSP stop_info_sp (gdb_thread->GetStopInfo ()); |
| 1487 | if (stop_info_sp) |
| 1488 | { |
| 1489 | stop_info_sp->SetDescription (description.c_str()); |
Greg Clayton | 153ccd7 | 2011-08-10 02:10:13 +0000 | [diff] [blame] | 1490 | } |
Greg Clayton | 6561155 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1491 | else |
| 1492 | { |
| 1493 | gdb_thread->SetStopInfo (StopInfo::CreateStopReasonWithException (*thread_sp, description.c_str())); |
| 1494 | } |
| 1495 | } |
| 1496 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1497 | } |
| 1498 | return eStateStopped; |
| 1499 | } |
| 1500 | break; |
| 1501 | |
| 1502 | case 'W': |
| 1503 | // process exited |
| 1504 | return eStateExited; |
| 1505 | |
| 1506 | default: |
| 1507 | break; |
| 1508 | } |
| 1509 | return eStateInvalid; |
| 1510 | } |
| 1511 | |
| 1512 | void |
| 1513 | ProcessGDBRemote::RefreshStateAfterStop () |
| 1514 | { |
Greg Clayton | ff3448e | 2012-04-13 02:11:32 +0000 | [diff] [blame] | 1515 | Mutex::Locker locker(m_thread_list.GetMutex()); |
Greg Clayton | 5a9f85c | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1516 | m_thread_ids.clear(); |
| 1517 | // Set the thread stop info. It might have a "threads" key whose value is |
| 1518 | // a list of all thread IDs in the current process, so m_thread_ids might |
| 1519 | // get set. |
| 1520 | SetThreadStopInfo (m_last_stop_packet); |
| 1521 | // Check to see if SetThreadStopInfo() filled in m_thread_ids? |
| 1522 | if (m_thread_ids.empty()) |
| 1523 | { |
| 1524 | // No, we need to fetch the thread list manually |
| 1525 | UpdateThreadIDList(); |
| 1526 | } |
| 1527 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1528 | // Let all threads recover from stopping and do any clean up based |
| 1529 | // on the previous thread state (if any). |
| 1530 | m_thread_list.RefreshStateAfterStop(); |
Greg Clayton | 5a9f85c | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1531 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | Error |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1535 | ProcessGDBRemote::DoHalt (bool &caused_stop) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1536 | { |
| 1537 | Error error; |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1538 | |
Greg Clayton | a4881d0 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 1539 | bool timed_out = false; |
| 1540 | Mutex::Locker locker; |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1541 | |
| 1542 | if (m_public_state.GetValue() == eStateAttaching) |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1543 | { |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1544 | // We are being asked to halt during an attach. We need to just close |
| 1545 | // our file handle and debugserver will go away, and we can be done... |
| 1546 | m_gdb_comm.Disconnect(); |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1547 | } |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1548 | else |
| 1549 | { |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 1550 | if (!m_gdb_comm.SendInterrupt (locker, 2, timed_out)) |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1551 | { |
| 1552 | if (timed_out) |
| 1553 | error.SetErrorString("timed out sending interrupt packet"); |
| 1554 | else |
| 1555 | error.SetErrorString("unknown error sending interrupt packet"); |
| 1556 | } |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 1557 | |
| 1558 | caused_stop = m_gdb_comm.GetInterruptWasSent (); |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1559 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1560 | return error; |
| 1561 | } |
| 1562 | |
| 1563 | Error |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1564 | ProcessGDBRemote::InterruptIfRunning |
| 1565 | ( |
| 1566 | bool discard_thread_plans, |
| 1567 | bool catch_stop_event, |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1568 | EventSP &stop_event_sp |
| 1569 | ) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1570 | { |
| 1571 | Error error; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1572 | |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 1573 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 1574 | |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1575 | bool paused_private_state_thread = false; |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 1576 | const bool is_running = m_gdb_comm.IsRunning(); |
| 1577 | if (log) |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1578 | log->Printf ("ProcessGDBRemote::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i", |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 1579 | discard_thread_plans, |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1580 | catch_stop_event, |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 1581 | is_running); |
| 1582 | |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 1583 | if (discard_thread_plans) |
| 1584 | { |
| 1585 | if (log) |
| 1586 | log->Printf ("ProcessGDBRemote::InterruptIfRunning() discarding all thread plans"); |
| 1587 | m_thread_list.DiscardThreadPlans(); |
| 1588 | } |
| 1589 | if (is_running) |
Greg Clayton | 4fb400f | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1590 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1591 | if (catch_stop_event) |
| 1592 | { |
| 1593 | if (log) |
| 1594 | log->Printf ("ProcessGDBRemote::InterruptIfRunning() pausing private state thread"); |
| 1595 | PausePrivateStateThread(); |
| 1596 | paused_private_state_thread = true; |
| 1597 | } |
| 1598 | |
Greg Clayton | 4fb400f | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1599 | bool timed_out = false; |
| 1600 | Mutex::Locker locker; |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1601 | |
Greg Clayton | 05e4d97 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 1602 | if (!m_gdb_comm.SendInterrupt (locker, 1, timed_out)) |
Greg Clayton | 4fb400f | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1603 | { |
| 1604 | if (timed_out) |
| 1605 | error.SetErrorString("timed out sending interrupt packet"); |
| 1606 | else |
| 1607 | error.SetErrorString("unknown error sending interrupt packet"); |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1608 | if (paused_private_state_thread) |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1609 | ResumePrivateStateThread(); |
| 1610 | return error; |
Greg Clayton | 4fb400f | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1611 | } |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1612 | |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1613 | if (catch_stop_event) |
| 1614 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1615 | // LISTEN HERE |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1616 | TimeValue timeout_time; |
| 1617 | timeout_time = TimeValue::Now(); |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1618 | timeout_time.OffsetWithSeconds(5); |
| 1619 | StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp); |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 1620 | |
Greg Clayton | bdcb6ab | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 1621 | timed_out = state == eStateInvalid; |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 1622 | if (log) |
| 1623 | log->Printf ("ProcessGDBRemote::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out); |
Greg Clayton | 3b2c41c | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1624 | |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 1625 | if (timed_out) |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1626 | error.SetErrorString("unable to verify target stopped"); |
| 1627 | } |
| 1628 | |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1629 | if (paused_private_state_thread) |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 1630 | { |
| 1631 | if (log) |
| 1632 | log->Printf ("ProcessGDBRemote::InterruptIfRunning() resuming private state thread"); |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1633 | ResumePrivateStateThread(); |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 1634 | } |
Greg Clayton | 4fb400f | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1635 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1636 | return error; |
| 1637 | } |
| 1638 | |
Greg Clayton | 4fb400f | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1639 | Error |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1640 | ProcessGDBRemote::WillDetach () |
| 1641 | { |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 1642 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 1643 | if (log) |
| 1644 | log->Printf ("ProcessGDBRemote::WillDetach()"); |
| 1645 | |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1646 | bool discard_thread_plans = true; |
| 1647 | bool catch_stop_event = true; |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1648 | EventSP event_sp; |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1649 | return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp); |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | Error |
Greg Clayton | 4fb400f | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1653 | ProcessGDBRemote::DoDetach() |
| 1654 | { |
| 1655 | Error error; |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1656 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Greg Clayton | 4fb400f | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1657 | if (log) |
| 1658 | log->Printf ("ProcessGDBRemote::DoDetach()"); |
| 1659 | |
| 1660 | DisableAllBreakpointSites (); |
| 1661 | |
Greg Clayton | 3b2c41c | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1662 | m_thread_list.DiscardThreadPlans(); |
Greg Clayton | 4fb400f | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1663 | |
Greg Clayton | 516f084 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 1664 | bool success = m_gdb_comm.Detach (); |
Greg Clayton | 3b2c41c | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1665 | if (log) |
Greg Clayton | 4fb400f | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1666 | { |
Greg Clayton | 516f084 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 1667 | if (success) |
Greg Clayton | 3b2c41c | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1668 | log->PutCString ("ProcessGDBRemote::DoDetach() detach packet sent successfully"); |
| 1669 | else |
| 1670 | log->PutCString ("ProcessGDBRemote::DoDetach() detach packet send failed"); |
Greg Clayton | 4fb400f | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1671 | } |
Greg Clayton | 3b2c41c | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1672 | // Sleep for one second to let the process get all detached... |
Greg Clayton | 4fb400f | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1673 | StopAsyncThread (); |
Greg Clayton | 3b2c41c | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1674 | |
Greg Clayton | 3b2c41c | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1675 | SetPrivateState (eStateDetached); |
| 1676 | ResumePrivateStateThread(); |
| 1677 | |
| 1678 | //KillDebugserverProcess (); |
Greg Clayton | 4fb400f | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1679 | return error; |
| 1680 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1681 | |
| 1682 | Error |
| 1683 | ProcessGDBRemote::DoDestroy () |
| 1684 | { |
| 1685 | Error error; |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1686 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1687 | if (log) |
| 1688 | log->Printf ("ProcessGDBRemote::DoDestroy()"); |
| 1689 | |
| 1690 | // Interrupt if our inferior is running... |
Greg Clayton | a4881d0 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 1691 | if (m_gdb_comm.IsConnected()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1692 | { |
Jim Ingham | 8226e94 | 2011-10-28 01:11:35 +0000 | [diff] [blame] | 1693 | if (m_public_state.GetValue() != eStateAttaching) |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1694 | { |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1695 | |
| 1696 | StringExtractorGDBRemote response; |
| 1697 | bool send_async = true; |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 1698 | if (m_gdb_comm.SendPacketAndWaitForResponse("k", 1, response, send_async)) |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1699 | { |
| 1700 | char packet_cmd = response.GetChar(0); |
| 1701 | |
| 1702 | if (packet_cmd == 'W' || packet_cmd == 'X') |
| 1703 | { |
Greg Clayton | 0670900 | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 1704 | SetLastStopPacket (response); |
Greg Clayton | 5a9f85c | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1705 | ClearThreadIDList (); |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1706 | SetExitStatus(response.GetHexU8(), NULL); |
| 1707 | } |
| 1708 | } |
| 1709 | else |
| 1710 | { |
| 1711 | SetExitStatus(SIGABRT, NULL); |
| 1712 | //error.SetErrorString("kill packet failed"); |
| 1713 | } |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1714 | } |
| 1715 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1716 | StopAsyncThread (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1717 | KillDebugserverProcess (); |
| 1718 | return error; |
| 1719 | } |
| 1720 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1721 | //------------------------------------------------------------------ |
| 1722 | // Process Queries |
| 1723 | //------------------------------------------------------------------ |
| 1724 | |
| 1725 | bool |
| 1726 | ProcessGDBRemote::IsAlive () |
| 1727 | { |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 1728 | return m_gdb_comm.IsConnected() && m_private_state.GetValue() != eStateExited; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | addr_t |
| 1732 | ProcessGDBRemote::GetImageInfoAddress() |
| 1733 | { |
Greg Clayton | 516f084 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 1734 | return m_gdb_comm.GetShlibInfoAddr(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1737 | //------------------------------------------------------------------ |
| 1738 | // Process Memory |
| 1739 | //------------------------------------------------------------------ |
| 1740 | size_t |
| 1741 | ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 1742 | { |
| 1743 | if (size > m_max_memory_size) |
| 1744 | { |
| 1745 | // Keep memory read sizes down to a sane limit. This function will be |
| 1746 | // called multiple times in order to complete the task by |
| 1747 | // lldb_private::Process so it is ok to do this. |
| 1748 | size = m_max_memory_size; |
| 1749 | } |
| 1750 | |
| 1751 | char packet[64]; |
| 1752 | const int packet_len = ::snprintf (packet, sizeof(packet), "m%llx,%zx", (uint64_t)addr, size); |
| 1753 | assert (packet_len + 1 < sizeof(packet)); |
| 1754 | StringExtractorGDBRemote response; |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 1755 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, true)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1756 | { |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1757 | if (response.IsNormalResponse()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1758 | { |
| 1759 | error.Clear(); |
| 1760 | return response.GetHexBytes(buf, size, '\xdd'); |
| 1761 | } |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1762 | else if (response.IsErrorResponse()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1763 | error.SetErrorStringWithFormat("gdb remote returned an error: %s", response.GetStringRef().c_str()); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1764 | else if (response.IsUnsupportedResponse()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1765 | error.SetErrorStringWithFormat("'%s' packet unsupported", packet); |
| 1766 | else |
| 1767 | error.SetErrorStringWithFormat("unexpected response to '%s': '%s'", packet, response.GetStringRef().c_str()); |
| 1768 | } |
| 1769 | else |
| 1770 | { |
| 1771 | error.SetErrorStringWithFormat("failed to sent packet: '%s'", packet); |
| 1772 | } |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
| 1776 | size_t |
| 1777 | ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 1778 | { |
Greg Clayton | c8bc1c3 | 2011-05-16 02:35:02 +0000 | [diff] [blame] | 1779 | if (size > m_max_memory_size) |
| 1780 | { |
| 1781 | // Keep memory read sizes down to a sane limit. This function will be |
| 1782 | // called multiple times in order to complete the task by |
| 1783 | // lldb_private::Process so it is ok to do this. |
| 1784 | size = m_max_memory_size; |
| 1785 | } |
| 1786 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1787 | StreamString packet; |
| 1788 | packet.Printf("M%llx,%zx:", addr, size); |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 1789 | packet.PutBytesAsRawHex8(buf, size, lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1790 | StringExtractorGDBRemote response; |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 1791 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, true)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1792 | { |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1793 | if (response.IsOKResponse()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1794 | { |
| 1795 | error.Clear(); |
| 1796 | return size; |
| 1797 | } |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1798 | else if (response.IsErrorResponse()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1799 | error.SetErrorStringWithFormat("gdb remote returned an error: %s", response.GetStringRef().c_str()); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1800 | else if (response.IsUnsupportedResponse()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1801 | error.SetErrorStringWithFormat("'%s' packet unsupported", packet.GetString().c_str()); |
| 1802 | else |
| 1803 | error.SetErrorStringWithFormat("unexpected response to '%s': '%s'", packet.GetString().c_str(), response.GetStringRef().c_str()); |
| 1804 | } |
| 1805 | else |
| 1806 | { |
| 1807 | error.SetErrorStringWithFormat("failed to sent packet: '%s'", packet.GetString().c_str()); |
| 1808 | } |
| 1809 | return 0; |
| 1810 | } |
| 1811 | |
| 1812 | lldb::addr_t |
| 1813 | ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &error) |
| 1814 | { |
Greg Clayton | 989816b | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1815 | addr_t allocated_addr = LLDB_INVALID_ADDRESS; |
| 1816 | |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1817 | LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory(); |
Greg Clayton | 989816b | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1818 | switch (supported) |
| 1819 | { |
| 1820 | case eLazyBoolCalculate: |
| 1821 | case eLazyBoolYes: |
| 1822 | allocated_addr = m_gdb_comm.AllocateMemory (size, permissions); |
| 1823 | if (allocated_addr != LLDB_INVALID_ADDRESS || supported == eLazyBoolYes) |
| 1824 | return allocated_addr; |
| 1825 | |
| 1826 | case eLazyBoolNo: |
Peter Collingbourne | 4d623e8 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 1827 | // Call mmap() to create memory in the inferior.. |
| 1828 | unsigned prot = 0; |
| 1829 | if (permissions & lldb::ePermissionsReadable) |
| 1830 | prot |= eMmapProtRead; |
| 1831 | if (permissions & lldb::ePermissionsWritable) |
| 1832 | prot |= eMmapProtWrite; |
| 1833 | if (permissions & lldb::ePermissionsExecutable) |
| 1834 | prot |= eMmapProtExec; |
Greg Clayton | 989816b | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1835 | |
Peter Collingbourne | 4d623e8 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 1836 | if (InferiorCallMmap(this, allocated_addr, 0, size, prot, |
| 1837 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) |
| 1838 | m_addr_to_mmap_size[allocated_addr] = size; |
| 1839 | else |
| 1840 | allocated_addr = LLDB_INVALID_ADDRESS; |
Greg Clayton | 989816b | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 1841 | break; |
| 1842 | } |
| 1843 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1844 | if (allocated_addr == LLDB_INVALID_ADDRESS) |
Greg Clayton | 613b873 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 1845 | error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1846 | else |
| 1847 | error.Clear(); |
| 1848 | return allocated_addr; |
| 1849 | } |
| 1850 | |
| 1851 | Error |
Greg Clayton | a938553 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 1852 | ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr, |
| 1853 | MemoryRegionInfo ®ion_info) |
| 1854 | { |
| 1855 | |
| 1856 | Error error (m_gdb_comm.GetMemoryRegionInfo (load_addr, region_info)); |
| 1857 | return error; |
| 1858 | } |
| 1859 | |
| 1860 | Error |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1861 | ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr) |
| 1862 | { |
| 1863 | Error error; |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1864 | LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory(); |
| 1865 | |
| 1866 | switch (supported) |
| 1867 | { |
| 1868 | case eLazyBoolCalculate: |
| 1869 | // We should never be deallocating memory without allocating memory |
| 1870 | // first so we should never get eLazyBoolCalculate |
| 1871 | error.SetErrorString ("tried to deallocate memory without ever allocating memory"); |
| 1872 | break; |
| 1873 | |
| 1874 | case eLazyBoolYes: |
| 1875 | if (!m_gdb_comm.DeallocateMemory (addr)) |
| 1876 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%llx", addr); |
| 1877 | break; |
| 1878 | |
| 1879 | case eLazyBoolNo: |
Peter Collingbourne | 4d623e8 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 1880 | // Call munmap() to deallocate memory in the inferior.. |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1881 | { |
| 1882 | MMapMap::iterator pos = m_addr_to_mmap_size.find(addr); |
Peter Collingbourne | 4d623e8 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 1883 | if (pos != m_addr_to_mmap_size.end() && |
| 1884 | InferiorCallMunmap(this, addr, pos->second)) |
| 1885 | m_addr_to_mmap_size.erase (pos); |
| 1886 | else |
| 1887 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%llx", addr); |
Greg Clayton | 2f085c6 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1888 | } |
| 1889 | break; |
| 1890 | } |
| 1891 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1892 | return error; |
| 1893 | } |
| 1894 | |
| 1895 | |
| 1896 | //------------------------------------------------------------------ |
| 1897 | // Process STDIO |
| 1898 | //------------------------------------------------------------------ |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1899 | size_t |
| 1900 | ProcessGDBRemote::PutSTDIN (const char *src, size_t src_len, Error &error) |
| 1901 | { |
| 1902 | if (m_stdio_communication.IsConnected()) |
| 1903 | { |
| 1904 | ConnectionStatus status; |
| 1905 | m_stdio_communication.Write(src, src_len, status, NULL); |
| 1906 | } |
| 1907 | return 0; |
| 1908 | } |
| 1909 | |
| 1910 | Error |
| 1911 | ProcessGDBRemote::EnableBreakpoint (BreakpointSite *bp_site) |
| 1912 | { |
| 1913 | Error error; |
| 1914 | assert (bp_site != NULL); |
| 1915 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1916 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1917 | user_id_t site_id = bp_site->GetID(); |
| 1918 | const addr_t addr = bp_site->GetLoadAddress(); |
| 1919 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 1920 | log->Printf ("ProcessGDBRemote::EnableBreakpoint (size_id = %llu) address = 0x%llx", site_id, (uint64_t)addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1921 | |
| 1922 | if (bp_site->IsEnabled()) |
| 1923 | { |
| 1924 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 1925 | log->Printf ("ProcessGDBRemote::EnableBreakpoint (size_id = %llu) address = 0x%llx -- SUCCESS (already enabled)", site_id, (uint64_t)addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1926 | return error; |
| 1927 | } |
| 1928 | else |
| 1929 | { |
| 1930 | const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site); |
| 1931 | |
| 1932 | if (bp_site->HardwarePreferred()) |
| 1933 | { |
| 1934 | // Try and set hardware breakpoint, and if that fails, fall through |
| 1935 | // and set a software breakpoint? |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1936 | if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointHardware)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1937 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1938 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, true, addr, bp_op_size) == 0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1939 | { |
| 1940 | bp_site->SetEnabled(true); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1941 | bp_site->SetType (BreakpointSite::eHardware); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1942 | return error; |
| 1943 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1944 | } |
| 1945 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1946 | |
| 1947 | if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointSoftware)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1948 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1949 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, true, addr, bp_op_size) == 0) |
| 1950 | { |
| 1951 | bp_site->SetEnabled(true); |
| 1952 | bp_site->SetType (BreakpointSite::eExternal); |
| 1953 | return error; |
| 1954 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1955 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1956 | |
| 1957 | return EnableSoftwareBreakpoint (bp_site); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | if (log) |
| 1961 | { |
| 1962 | const char *err_string = error.AsCString(); |
| 1963 | log->Printf ("ProcessGDBRemote::EnableBreakpoint() error for breakpoint at 0x%8.8llx: %s", |
| 1964 | bp_site->GetLoadAddress(), |
| 1965 | err_string ? err_string : "NULL"); |
| 1966 | } |
| 1967 | // We shouldn't reach here on a successful breakpoint enable... |
| 1968 | if (error.Success()) |
| 1969 | error.SetErrorToGenericError(); |
| 1970 | return error; |
| 1971 | } |
| 1972 | |
| 1973 | Error |
| 1974 | ProcessGDBRemote::DisableBreakpoint (BreakpointSite *bp_site) |
| 1975 | { |
| 1976 | Error error; |
| 1977 | assert (bp_site != NULL); |
| 1978 | addr_t addr = bp_site->GetLoadAddress(); |
| 1979 | user_id_t site_id = bp_site->GetID(); |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1980 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1981 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 1982 | log->Printf ("ProcessGDBRemote::DisableBreakpoint (site_id = %llu) addr = 0x%8.8llx", site_id, (uint64_t)addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1983 | |
| 1984 | if (bp_site->IsEnabled()) |
| 1985 | { |
| 1986 | const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site); |
| 1987 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1988 | BreakpointSite::Type bp_type = bp_site->GetType(); |
| 1989 | switch (bp_type) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1990 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1991 | case BreakpointSite::eSoftware: |
| 1992 | error = DisableSoftwareBreakpoint (bp_site); |
| 1993 | break; |
| 1994 | |
| 1995 | case BreakpointSite::eHardware: |
| 1996 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size)) |
| 1997 | error.SetErrorToGenericError(); |
| 1998 | break; |
| 1999 | |
| 2000 | case BreakpointSite::eExternal: |
| 2001 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size)) |
| 2002 | error.SetErrorToGenericError(); |
| 2003 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2004 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2005 | if (error.Success()) |
| 2006 | bp_site->SetEnabled(false); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2007 | } |
| 2008 | else |
| 2009 | { |
| 2010 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 2011 | log->Printf ("ProcessGDBRemote::DisableBreakpoint (site_id = %llu) addr = 0x%8.8llx -- SUCCESS (already disabled)", site_id, (uint64_t)addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2012 | return error; |
| 2013 | } |
| 2014 | |
| 2015 | if (error.Success()) |
| 2016 | error.SetErrorToGenericError(); |
| 2017 | return error; |
| 2018 | } |
| 2019 | |
Johnny Chen | 21900fb | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2020 | // Pre-requisite: wp != NULL. |
| 2021 | static GDBStoppointType |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2022 | GetGDBStoppointType (Watchpoint *wp) |
Johnny Chen | 21900fb | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2023 | { |
| 2024 | assert(wp); |
| 2025 | bool watch_read = wp->WatchpointRead(); |
| 2026 | bool watch_write = wp->WatchpointWrite(); |
| 2027 | |
| 2028 | // watch_read and watch_write cannot both be false. |
| 2029 | assert(watch_read || watch_write); |
| 2030 | if (watch_read && watch_write) |
| 2031 | return eWatchpointReadWrite; |
Johnny Chen | 48a5e85 | 2011-09-09 20:35:15 +0000 | [diff] [blame] | 2032 | else if (watch_read) |
Johnny Chen | 21900fb | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2033 | return eWatchpointRead; |
Johnny Chen | 48a5e85 | 2011-09-09 20:35:15 +0000 | [diff] [blame] | 2034 | else // Must be watch_write, then. |
Johnny Chen | 21900fb | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2035 | return eWatchpointWrite; |
| 2036 | } |
| 2037 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2038 | Error |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2039 | ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2040 | { |
| 2041 | Error error; |
| 2042 | if (wp) |
| 2043 | { |
| 2044 | user_id_t watchID = wp->GetID(); |
| 2045 | addr_t addr = wp->GetLoadAddress(); |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 2046 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2047 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 2048 | log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %llu)", watchID); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2049 | if (wp->IsEnabled()) |
| 2050 | { |
| 2051 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 2052 | log->Printf("ProcessGDBRemote::EnableWatchpoint(watchID = %llu) addr = 0x%8.8llx: watchpoint already enabled.", watchID, (uint64_t)addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2053 | return error; |
| 2054 | } |
Johnny Chen | 21900fb | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2055 | |
| 2056 | GDBStoppointType type = GetGDBStoppointType(wp); |
| 2057 | // Pass down an appropriate z/Z packet... |
| 2058 | if (m_gdb_comm.SupportsGDBStoppointPacket (type)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2059 | { |
Johnny Chen | 21900fb | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2060 | if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, wp->GetByteSize()) == 0) |
| 2061 | { |
| 2062 | wp->SetEnabled(true); |
| 2063 | return error; |
| 2064 | } |
| 2065 | else |
| 2066 | error.SetErrorString("sending gdb watchpoint packet failed"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2067 | } |
Johnny Chen | 21900fb | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2068 | else |
| 2069 | error.SetErrorString("watchpoints not supported"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2070 | } |
| 2071 | else |
| 2072 | { |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2073 | error.SetErrorString("Watchpoint argument was NULL."); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2074 | } |
| 2075 | if (error.Success()) |
| 2076 | error.SetErrorToGenericError(); |
| 2077 | return error; |
| 2078 | } |
| 2079 | |
| 2080 | Error |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2081 | ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2082 | { |
| 2083 | Error error; |
| 2084 | if (wp) |
| 2085 | { |
| 2086 | user_id_t watchID = wp->GetID(); |
| 2087 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 2088 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2089 | |
| 2090 | addr_t addr = wp->GetLoadAddress(); |
| 2091 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 2092 | log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %llu) addr = 0x%8.8llx", watchID, (uint64_t)addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2093 | |
Johnny Chen | 21900fb | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2094 | if (!wp->IsEnabled()) |
| 2095 | { |
| 2096 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 2097 | log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %llu) addr = 0x%8.8llx -- SUCCESS (already disabled)", watchID, (uint64_t)addr); |
Johnny Chen | 21900fb | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2098 | return error; |
| 2099 | } |
| 2100 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2101 | if (wp->IsHardware()) |
| 2102 | { |
Johnny Chen | 21900fb | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2103 | GDBStoppointType type = GetGDBStoppointType(wp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2104 | // Pass down an appropriate z/Z packet... |
Johnny Chen | 21900fb | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2105 | if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, wp->GetByteSize()) == 0) |
| 2106 | { |
| 2107 | wp->SetEnabled(false); |
| 2108 | return error; |
| 2109 | } |
| 2110 | else |
| 2111 | error.SetErrorString("sending gdb watchpoint packet failed"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2112 | } |
| 2113 | // TODO: clear software watchpoints if we implement them |
| 2114 | } |
| 2115 | else |
| 2116 | { |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2117 | error.SetErrorString("Watchpoint argument was NULL."); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2118 | } |
| 2119 | if (error.Success()) |
| 2120 | error.SetErrorToGenericError(); |
| 2121 | return error; |
| 2122 | } |
| 2123 | |
| 2124 | void |
| 2125 | ProcessGDBRemote::Clear() |
| 2126 | { |
| 2127 | m_flags = 0; |
| 2128 | m_thread_list.Clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | Error |
| 2132 | ProcessGDBRemote::DoSignal (int signo) |
| 2133 | { |
| 2134 | Error error; |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 2135 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2136 | if (log) |
| 2137 | log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo); |
| 2138 | |
| 2139 | if (!m_gdb_comm.SendAsyncSignal (signo)) |
| 2140 | error.SetErrorStringWithFormat("failed to send signal %i", signo); |
| 2141 | return error; |
| 2142 | } |
| 2143 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2144 | Error |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 2145 | ProcessGDBRemote::StartDebugserverProcess (const char *debugserver_url) |
| 2146 | { |
| 2147 | ProcessLaunchInfo launch_info; |
| 2148 | return StartDebugserverProcess(debugserver_url, launch_info); |
| 2149 | } |
| 2150 | |
| 2151 | Error |
| 2152 | ProcessGDBRemote::StartDebugserverProcess (const char *debugserver_url, const ProcessInfo &process_info) // The connection string to use in the spawned debugserver ("localhost:1234" or "/dev/tty...") |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2153 | { |
| 2154 | Error error; |
| 2155 | if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID) |
| 2156 | { |
| 2157 | // If we locate debugserver, keep that located version around |
| 2158 | static FileSpec g_debugserver_file_spec; |
| 2159 | |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 2160 | ProcessLaunchInfo debugserver_launch_info; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2161 | char debugserver_path[PATH_MAX]; |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 2162 | FileSpec &debugserver_file_spec = debugserver_launch_info.GetExecutableFile(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2163 | |
| 2164 | // Always check to see if we have an environment override for the path |
| 2165 | // to the debugserver to use and use it if we do. |
| 2166 | const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH"); |
| 2167 | if (env_debugserver_path) |
Greg Clayton | 537a7a8 | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 2168 | debugserver_file_spec.SetFile (env_debugserver_path, false); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2169 | else |
| 2170 | debugserver_file_spec = g_debugserver_file_spec; |
| 2171 | bool debugserver_exists = debugserver_file_spec.Exists(); |
| 2172 | if (!debugserver_exists) |
| 2173 | { |
| 2174 | // The debugserver binary is in the LLDB.framework/Resources |
| 2175 | // directory. |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 2176 | if (Host::GetLLDBPath (ePathTypeSupportExecutableDir, debugserver_file_spec)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2177 | { |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 2178 | debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2179 | debugserver_exists = debugserver_file_spec.Exists(); |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 2180 | if (debugserver_exists) |
| 2181 | { |
| 2182 | g_debugserver_file_spec = debugserver_file_spec; |
| 2183 | } |
| 2184 | else |
| 2185 | { |
| 2186 | g_debugserver_file_spec.Clear(); |
| 2187 | debugserver_file_spec.Clear(); |
| 2188 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | if (debugserver_exists) |
| 2193 | { |
| 2194 | debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path)); |
| 2195 | |
| 2196 | m_stdio_communication.Clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2197 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 2198 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2199 | |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 2200 | Args &debugserver_args = debugserver_launch_info.GetArguments(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2201 | char arg_cstr[PATH_MAX]; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2202 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2203 | // Start args with "debugserver /file/path -r --" |
| 2204 | debugserver_args.AppendArgument(debugserver_path); |
| 2205 | debugserver_args.AppendArgument(debugserver_url); |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 2206 | // use native registers, not the GDB registers |
| 2207 | debugserver_args.AppendArgument("--native-regs"); |
| 2208 | // make debugserver run in its own session so signals generated by |
| 2209 | // special terminal key sequences (^C) don't affect debugserver |
| 2210 | debugserver_args.AppendArgument("--setsid"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2211 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2212 | const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE"); |
| 2213 | if (env_debugserver_log_file) |
| 2214 | { |
| 2215 | ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file); |
| 2216 | debugserver_args.AppendArgument(arg_cstr); |
| 2217 | } |
| 2218 | |
| 2219 | const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS"); |
| 2220 | if (env_debugserver_log_flags) |
| 2221 | { |
| 2222 | ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags); |
| 2223 | debugserver_args.AppendArgument(arg_cstr); |
| 2224 | } |
Greg Clayton | cc3e640 | 2011-01-25 06:55:13 +0000 | [diff] [blame] | 2225 | // debugserver_args.AppendArgument("--log-file=/tmp/debugserver.txt"); |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2226 | // debugserver_args.AppendArgument("--log-flags=0x802e0e"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2227 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2228 | // We currently send down all arguments, attach pids, or attach |
| 2229 | // process names in dedicated GDB server packets, so we don't need |
| 2230 | // to pass them as arguments. This is currently because of all the |
| 2231 | // things we need to setup prior to launching: the environment, |
| 2232 | // current working dir, file actions, etc. |
| 2233 | #if 0 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2234 | // Now append the program arguments |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2235 | if (inferior_argv) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2236 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2237 | // Terminate the debugserver args so we can now append the inferior args |
| 2238 | debugserver_args.AppendArgument("--"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2239 | |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2240 | for (int i = 0; inferior_argv[i] != NULL; ++i) |
| 2241 | debugserver_args.AppendArgument (inferior_argv[i]); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2242 | } |
| 2243 | else if (attach_pid != LLDB_INVALID_PROCESS_ID) |
| 2244 | { |
| 2245 | ::snprintf (arg_cstr, sizeof(arg_cstr), "--attach=%u", attach_pid); |
| 2246 | debugserver_args.AppendArgument (arg_cstr); |
| 2247 | } |
| 2248 | else if (attach_name && attach_name[0]) |
| 2249 | { |
| 2250 | if (wait_for_launch) |
| 2251 | debugserver_args.AppendArgument ("--waitfor"); |
| 2252 | else |
| 2253 | debugserver_args.AppendArgument ("--attach"); |
| 2254 | debugserver_args.AppendArgument (attach_name); |
| 2255 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2256 | #endif |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2257 | |
| 2258 | ProcessLaunchInfo::FileAction file_action; |
| 2259 | |
| 2260 | // Close STDIN, STDOUT and STDERR. We might need to redirect them |
| 2261 | // to "/dev/null" if we run into any problems. |
| 2262 | file_action.Close (STDIN_FILENO); |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 2263 | debugserver_launch_info.AppendFileAction (file_action); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2264 | file_action.Close (STDOUT_FILENO); |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 2265 | debugserver_launch_info.AppendFileAction (file_action); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2266 | file_action.Close (STDERR_FILENO); |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 2267 | debugserver_launch_info.AppendFileAction (file_action); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2268 | |
| 2269 | if (log) |
| 2270 | { |
| 2271 | StreamString strm; |
| 2272 | debugserver_args.Dump (&strm); |
| 2273 | log->Printf("%s arguments:\n%s", debugserver_args.GetArgumentAtIndex(0), strm.GetData()); |
| 2274 | } |
| 2275 | |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 2276 | debugserver_launch_info.SetMonitorProcessCallback (MonitorDebugserverProcess, this, false); |
| 2277 | debugserver_launch_info.SetUserID(process_info.GetUserID()); |
Greg Clayton | 1c4642c | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2278 | |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 2279 | error = Host::LaunchProcess(debugserver_launch_info); |
Greg Clayton | e9d0df4 | 2010-07-02 01:29:13 +0000 | [diff] [blame] | 2280 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2281 | if (error.Success ()) |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 2282 | m_debugserver_pid = debugserver_launch_info.GetProcessID(); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2283 | else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2284 | m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
| 2285 | |
| 2286 | if (error.Fail() || log) |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 2287 | error.PutToLog(log.get(), "Host::LaunchProcess (launch_info) => pid=%llu, path='%s'", m_debugserver_pid, debugserver_path); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2288 | } |
| 2289 | else |
| 2290 | { |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 2291 | error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) |
| 2295 | StartAsyncThread (); |
| 2296 | } |
| 2297 | return error; |
| 2298 | } |
| 2299 | |
| 2300 | bool |
| 2301 | ProcessGDBRemote::MonitorDebugserverProcess |
| 2302 | ( |
| 2303 | void *callback_baton, |
| 2304 | lldb::pid_t debugserver_pid, |
Greg Clayton | 1c4642c | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2305 | bool exited, // True if the process did exit |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2306 | int signo, // Zero for no signal |
| 2307 | int exit_status // Exit value of process if signal is zero |
| 2308 | ) |
| 2309 | { |
Greg Clayton | 1c4642c | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2310 | // The baton is a "ProcessGDBRemote *". Now this class might be gone |
| 2311 | // and might not exist anymore, so we need to carefully try to get the |
| 2312 | // target for this process first since we have a race condition when |
| 2313 | // we are done running between getting the notice that the inferior |
| 2314 | // process has died and the debugserver that was debugging this process. |
| 2315 | // In our test suite, we are also continually running process after |
| 2316 | // process, so we must be very careful to make sure: |
| 2317 | // 1 - process object hasn't been deleted already |
| 2318 | // 2 - that a new process object hasn't been recreated in its place |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2319 | |
| 2320 | // "debugserver_pid" argument passed in is the process ID for |
| 2321 | // debugserver that we are tracking... |
Greg Clayton | 1c4642c | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2322 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2323 | |
Greg Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 2324 | ProcessGDBRemote *process = (ProcessGDBRemote *)callback_baton; |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2325 | |
Greg Clayton | 1c4642c | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2326 | // Get a shared pointer to the target that has a matching process pointer. |
| 2327 | // This target could be gone, or the target could already have a new process |
| 2328 | // object inside of it |
| 2329 | TargetSP target_sp (Debugger::FindTargetWithProcess(process)); |
| 2330 | |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2331 | if (log) |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 2332 | log->Printf ("ProcessGDBRemote::MonitorDebugserverProcess (baton=%p, pid=%llu, signo=%i (0x%x), exit_status=%i)", callback_baton, debugserver_pid, signo, signo, exit_status); |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2333 | |
Greg Clayton | 1c4642c | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2334 | if (target_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2335 | { |
Greg Clayton | 1c4642c | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2336 | // We found a process in a target that matches, but another thread |
| 2337 | // might be in the process of launching a new process that will |
| 2338 | // soon replace it, so get a shared pointer to the process so we |
| 2339 | // can keep it alive. |
| 2340 | ProcessSP process_sp (target_sp->GetProcessSP()); |
| 2341 | // Now we have a shared pointer to the process that can't go away on us |
| 2342 | // so we now make sure it was the same as the one passed in, and also make |
| 2343 | // sure that our previous "process *" didn't get deleted and have a new |
| 2344 | // "process *" created in its place with the same pointer. To verify this |
| 2345 | // we make sure the process has our debugserver process ID. If we pass all |
| 2346 | // of these tests, then we are sure that this process is the one we were |
| 2347 | // looking for. |
| 2348 | if (process_sp && process == process_sp.get() && process->m_debugserver_pid == debugserver_pid) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2349 | { |
Greg Clayton | 1c4642c | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2350 | // Sleep for a half a second to make sure our inferior process has |
| 2351 | // time to set its exit status before we set it incorrectly when |
| 2352 | // both the debugserver and the inferior process shut down. |
| 2353 | usleep (500000); |
| 2354 | // If our process hasn't yet exited, debugserver might have died. |
| 2355 | // If the process did exit, the we are reaping it. |
| 2356 | const StateType state = process->GetState(); |
| 2357 | |
| 2358 | if (process->m_debugserver_pid != LLDB_INVALID_PROCESS_ID && |
| 2359 | state != eStateInvalid && |
| 2360 | state != eStateUnloaded && |
| 2361 | state != eStateExited && |
| 2362 | state != eStateDetached) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2363 | { |
Greg Clayton | 1c4642c | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2364 | char error_str[1024]; |
| 2365 | if (signo) |
| 2366 | { |
| 2367 | const char *signal_cstr = process->GetUnixSignals().GetSignalAsCString (signo); |
| 2368 | if (signal_cstr) |
| 2369 | ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %s", signal_cstr); |
| 2370 | else |
| 2371 | ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %i", signo); |
| 2372 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2373 | else |
Greg Clayton | 1c4642c | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2374 | { |
| 2375 | ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", exit_status); |
| 2376 | } |
Greg Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 2377 | |
Greg Clayton | 1c4642c | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2378 | process->SetExitStatus (-1, error_str); |
| 2379 | } |
| 2380 | // Debugserver has exited we need to let our ProcessGDBRemote |
| 2381 | // know that it no longer has a debugserver instance |
| 2382 | process->m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
Greg Clayton | 75ccf50 | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 2383 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2384 | } |
| 2385 | return true; |
| 2386 | } |
| 2387 | |
| 2388 | void |
| 2389 | ProcessGDBRemote::KillDebugserverProcess () |
| 2390 | { |
| 2391 | if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) |
| 2392 | { |
| 2393 | ::kill (m_debugserver_pid, SIGINT); |
| 2394 | m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | void |
| 2399 | ProcessGDBRemote::Initialize() |
| 2400 | { |
| 2401 | static bool g_initialized = false; |
| 2402 | |
| 2403 | if (g_initialized == false) |
| 2404 | { |
| 2405 | g_initialized = true; |
| 2406 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 2407 | GetPluginDescriptionStatic(), |
| 2408 | CreateInstance); |
| 2409 | |
| 2410 | Log::Callbacks log_callbacks = { |
| 2411 | ProcessGDBRemoteLog::DisableLog, |
| 2412 | ProcessGDBRemoteLog::EnableLog, |
| 2413 | ProcessGDBRemoteLog::ListLogCategories |
| 2414 | }; |
| 2415 | |
| 2416 | Log::RegisterLogChannel (ProcessGDBRemote::GetPluginNameStatic(), log_callbacks); |
| 2417 | } |
| 2418 | } |
| 2419 | |
| 2420 | bool |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2421 | ProcessGDBRemote::StartAsyncThread () |
| 2422 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 2423 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2424 | |
| 2425 | if (log) |
| 2426 | log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__); |
| 2427 | |
| 2428 | // Create a thread that watches our internal state and controls which |
| 2429 | // events make it to clients (into the DCProcess event queue). |
| 2430 | m_async_thread = Host::ThreadCreate ("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL); |
Greg Clayton | 09c81ef | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 2431 | return IS_VALID_LLDB_HOST_THREAD(m_async_thread); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | void |
| 2435 | ProcessGDBRemote::StopAsyncThread () |
| 2436 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 2437 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2438 | |
| 2439 | if (log) |
| 2440 | log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__); |
| 2441 | |
| 2442 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit); |
Jim Ingham | 8226e94 | 2011-10-28 01:11:35 +0000 | [diff] [blame] | 2443 | |
| 2444 | // This will shut down the async thread. |
| 2445 | m_gdb_comm.Disconnect(); // Disconnect from the debug server. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2446 | |
| 2447 | // Stop the stdio thread |
Greg Clayton | 09c81ef | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 2448 | if (IS_VALID_LLDB_HOST_THREAD(m_async_thread)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2449 | { |
| 2450 | Host::ThreadJoin (m_async_thread, NULL, NULL); |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | |
| 2455 | void * |
| 2456 | ProcessGDBRemote::AsyncThread (void *arg) |
| 2457 | { |
| 2458 | ProcessGDBRemote *process = (ProcessGDBRemote*) arg; |
| 2459 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 2460 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2461 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 2462 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, arg, process->GetID()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2463 | |
| 2464 | Listener listener ("ProcessGDBRemote::AsyncThread"); |
| 2465 | EventSP event_sp; |
| 2466 | const uint32_t desired_event_mask = eBroadcastBitAsyncContinue | |
| 2467 | eBroadcastBitAsyncThreadShouldExit; |
| 2468 | |
| 2469 | if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask) |
| 2470 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2471 | listener.StartListeningForEvents (&process->m_gdb_comm, Communication::eBroadcastBitReadThreadDidExit); |
| 2472 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2473 | bool done = false; |
| 2474 | while (!done) |
| 2475 | { |
| 2476 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 2477 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2478 | if (listener.WaitForEvent (NULL, event_sp)) |
| 2479 | { |
| 2480 | const uint32_t event_type = event_sp->GetType(); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2481 | if (event_sp->BroadcasterIs (&process->m_async_broadcaster)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2482 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2483 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 2484 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) Got an event of type: %d...", __FUNCTION__, arg, process->GetID(), event_type); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2485 | |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2486 | switch (event_type) |
| 2487 | { |
| 2488 | case eBroadcastBitAsyncContinue: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2489 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2490 | const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2491 | |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2492 | if (continue_packet) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2493 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2494 | const char *continue_cstr = (const char *)continue_packet->GetBytes (); |
| 2495 | const size_t continue_cstr_len = continue_packet->GetByteSize (); |
| 2496 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 2497 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2498 | |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2499 | if (::strstr (continue_cstr, "vAttach") == NULL) |
| 2500 | process->SetPrivateState(eStateRunning); |
| 2501 | StringExtractorGDBRemote response; |
| 2502 | StateType stop_state = process->GetGDBRemote().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2503 | |
Greg Clayton | 67b402c | 2012-05-16 02:48:06 +0000 | [diff] [blame] | 2504 | // We need to immediately clear the thread ID list so we are sure to get a valid list of threads. |
| 2505 | // The thread ID list might be contained within the "response", or the stop reply packet that |
| 2506 | // caused the stop. So clear it now before we give the stop reply packet to the process |
| 2507 | // using the process->SetLastStopPacket()... |
| 2508 | process->ClearThreadIDList (); |
| 2509 | |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2510 | switch (stop_state) |
| 2511 | { |
| 2512 | case eStateStopped: |
| 2513 | case eStateCrashed: |
| 2514 | case eStateSuspended: |
Greg Clayton | 0670900 | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 2515 | process->SetLastStopPacket (response); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2516 | process->SetPrivateState (stop_state); |
| 2517 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2518 | |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2519 | case eStateExited: |
Greg Clayton | 0670900 | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 2520 | process->SetLastStopPacket (response); |
Greg Clayton | 5a9f85c | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2521 | process->ClearThreadIDList(); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2522 | response.SetFilePos(1); |
| 2523 | process->SetExitStatus(response.GetHexU8(), NULL); |
| 2524 | done = true; |
| 2525 | break; |
| 2526 | |
| 2527 | case eStateInvalid: |
| 2528 | process->SetExitStatus(-1, "lost connection"); |
| 2529 | break; |
| 2530 | |
| 2531 | default: |
| 2532 | process->SetPrivateState (stop_state); |
| 2533 | break; |
| 2534 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2535 | } |
| 2536 | } |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2537 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2538 | |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2539 | case eBroadcastBitAsyncThreadShouldExit: |
| 2540 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 2541 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID()); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2542 | done = true; |
| 2543 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2544 | |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2545 | default: |
| 2546 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 2547 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) got unknown event 0x%8.8x", __FUNCTION__, arg, process->GetID(), event_type); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2548 | done = true; |
| 2549 | break; |
| 2550 | } |
| 2551 | } |
| 2552 | else if (event_sp->BroadcasterIs (&process->m_gdb_comm)) |
| 2553 | { |
| 2554 | if (event_type & Communication::eBroadcastBitReadThreadDidExit) |
| 2555 | { |
| 2556 | process->SetExitStatus (-1, "lost connection"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2557 | done = true; |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2558 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2559 | } |
| 2560 | } |
| 2561 | else |
| 2562 | { |
| 2563 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 2564 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp) => false", __FUNCTION__, arg, process->GetID()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2565 | done = true; |
| 2566 | } |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 2571 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, arg, process->GetID()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2572 | |
| 2573 | process->m_async_thread = LLDB_INVALID_HOST_THREAD; |
| 2574 | return NULL; |
| 2575 | } |
| 2576 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2577 | const char * |
| 2578 | ProcessGDBRemote::GetDispatchQueueNameForThread |
| 2579 | ( |
| 2580 | addr_t thread_dispatch_qaddr, |
| 2581 | std::string &dispatch_queue_name |
| 2582 | ) |
| 2583 | { |
| 2584 | dispatch_queue_name.clear(); |
| 2585 | if (thread_dispatch_qaddr != 0 && thread_dispatch_qaddr != LLDB_INVALID_ADDRESS) |
| 2586 | { |
| 2587 | // Cache the dispatch_queue_offsets_addr value so we don't always have |
| 2588 | // to look it up |
| 2589 | if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS) |
| 2590 | { |
Greg Clayton | af6e9e4 | 2010-10-12 17:33:06 +0000 | [diff] [blame] | 2591 | static ConstString g_dispatch_queue_offsets_symbol_name ("dispatch_queue_offsets"); |
| 2592 | const Symbol *dispatch_queue_offsets_symbol = NULL; |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 2593 | ModuleSpec libSystem_module_spec (FileSpec("libSystem.B.dylib", false)); |
| 2594 | ModuleSP module_sp(GetTarget().GetImages().FindFirstModule (libSystem_module_spec)); |
Greg Clayton | af6e9e4 | 2010-10-12 17:33:06 +0000 | [diff] [blame] | 2595 | if (module_sp) |
| 2596 | dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData); |
| 2597 | |
| 2598 | if (dispatch_queue_offsets_symbol == NULL) |
| 2599 | { |
Greg Clayton | 444fe99 | 2012-02-26 05:51:37 +0000 | [diff] [blame] | 2600 | ModuleSpec libdispatch_module_spec (FileSpec("libdispatch.dylib", false)); |
| 2601 | module_sp = GetTarget().GetImages().FindFirstModule (libdispatch_module_spec); |
Greg Clayton | af6e9e4 | 2010-10-12 17:33:06 +0000 | [diff] [blame] | 2602 | if (module_sp) |
| 2603 | dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType (g_dispatch_queue_offsets_symbol_name, eSymbolTypeData); |
| 2604 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2605 | if (dispatch_queue_offsets_symbol) |
Greg Clayton | 0c31d3d | 2012-03-07 21:03:09 +0000 | [diff] [blame] | 2606 | m_dispatch_queue_offsets_addr = dispatch_queue_offsets_symbol->GetAddress().GetLoadAddress(&m_target); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2607 | |
| 2608 | if (m_dispatch_queue_offsets_addr == LLDB_INVALID_ADDRESS) |
| 2609 | return NULL; |
| 2610 | } |
| 2611 | |
| 2612 | uint8_t memory_buffer[8]; |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 2613 | DataExtractor data (memory_buffer, |
| 2614 | sizeof(memory_buffer), |
| 2615 | m_target.GetArchitecture().GetByteOrder(), |
| 2616 | m_target.GetArchitecture().GetAddressByteSize()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2617 | |
| 2618 | // Excerpt from src/queue_private.h |
| 2619 | struct dispatch_queue_offsets_s |
| 2620 | { |
| 2621 | uint16_t dqo_version; |
| 2622 | uint16_t dqo_label; |
| 2623 | uint16_t dqo_label_size; |
| 2624 | } dispatch_queue_offsets; |
| 2625 | |
| 2626 | |
| 2627 | Error error; |
| 2628 | if (ReadMemory (m_dispatch_queue_offsets_addr, memory_buffer, sizeof(dispatch_queue_offsets), error) == sizeof(dispatch_queue_offsets)) |
| 2629 | { |
| 2630 | uint32_t data_offset = 0; |
| 2631 | if (data.GetU16(&data_offset, &dispatch_queue_offsets.dqo_version, sizeof(dispatch_queue_offsets)/sizeof(uint16_t))) |
| 2632 | { |
| 2633 | if (ReadMemory (thread_dispatch_qaddr, &memory_buffer, data.GetAddressByteSize(), error) == data.GetAddressByteSize()) |
| 2634 | { |
| 2635 | data_offset = 0; |
| 2636 | lldb::addr_t queue_addr = data.GetAddress(&data_offset); |
| 2637 | lldb::addr_t label_addr = queue_addr + dispatch_queue_offsets.dqo_label; |
| 2638 | dispatch_queue_name.resize(dispatch_queue_offsets.dqo_label_size, '\0'); |
| 2639 | size_t bytes_read = ReadMemory (label_addr, &dispatch_queue_name[0], dispatch_queue_offsets.dqo_label_size, error); |
| 2640 | if (bytes_read < dispatch_queue_offsets.dqo_label_size) |
| 2641 | dispatch_queue_name.erase (bytes_read); |
| 2642 | } |
| 2643 | } |
| 2644 | } |
| 2645 | } |
| 2646 | if (dispatch_queue_name.empty()) |
| 2647 | return NULL; |
| 2648 | return dispatch_queue_name.c_str(); |
| 2649 | } |
| 2650 | |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 2651 | //uint32_t |
| 2652 | //ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) |
| 2653 | //{ |
| 2654 | // // If we are planning to launch the debugserver remotely, then we need to fire up a debugserver |
| 2655 | // // process and ask it for the list of processes. But if we are local, we can let the Host do it. |
| 2656 | // if (m_local_debugserver) |
| 2657 | // { |
| 2658 | // return Host::ListProcessesMatchingName (name, matches, pids); |
| 2659 | // } |
| 2660 | // else |
| 2661 | // { |
| 2662 | // // FIXME: Implement talking to the remote debugserver. |
| 2663 | // return 0; |
| 2664 | // } |
| 2665 | // |
| 2666 | //} |
| 2667 | // |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2668 | bool |
| 2669 | ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton, |
| 2670 | lldb_private::StoppointCallbackContext *context, |
| 2671 | lldb::user_id_t break_id, |
| 2672 | lldb::user_id_t break_loc_id) |
| 2673 | { |
| 2674 | // I don't think I have to do anything here, just make sure I notice the new thread when it starts to |
| 2675 | // run so I can stop it if that's what I want to do. |
| 2676 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
| 2677 | if (log) |
| 2678 | log->Printf("Hit New Thread Notification breakpoint."); |
| 2679 | return false; |
| 2680 | } |
| 2681 | |
| 2682 | |
| 2683 | bool |
| 2684 | ProcessGDBRemote::StartNoticingNewThreads() |
| 2685 | { |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2686 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Greg Clayton | bd5c23d | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2687 | if (m_thread_create_bp_sp) |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2688 | { |
Greg Clayton | bd5c23d | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2689 | if (log && log->GetVerbose()) |
| 2690 | log->Printf("Enabled noticing new thread breakpoint."); |
| 2691 | m_thread_create_bp_sp->SetEnabled(true); |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2692 | } |
Greg Clayton | bd5c23d | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2693 | else |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2694 | { |
Greg Clayton | bd5c23d | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2695 | PlatformSP platform_sp (m_target.GetPlatform()); |
| 2696 | if (platform_sp) |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2697 | { |
Greg Clayton | bd5c23d | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2698 | m_thread_create_bp_sp = platform_sp->SetThreadCreationBreakpoint(m_target); |
| 2699 | if (m_thread_create_bp_sp) |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2700 | { |
Jim Ingham | 6bb7337 | 2011-10-15 00:21:37 +0000 | [diff] [blame] | 2701 | if (log && log->GetVerbose()) |
Greg Clayton | bd5c23d | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2702 | log->Printf("Successfully created new thread notification breakpoint %i", m_thread_create_bp_sp->GetID()); |
| 2703 | m_thread_create_bp_sp->SetCallback (ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true); |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2704 | } |
| 2705 | else |
| 2706 | { |
| 2707 | if (log) |
| 2708 | log->Printf("Failed to create new thread notification breakpoint."); |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2709 | } |
| 2710 | } |
| 2711 | } |
Greg Clayton | bd5c23d | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2712 | return m_thread_create_bp_sp.get() != NULL; |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | bool |
| 2716 | ProcessGDBRemote::StopNoticingNewThreads() |
| 2717 | { |
Jim Ingham | ff276fe | 2011-02-08 05:19:01 +0000 | [diff] [blame] | 2718 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 6bb7337 | 2011-10-15 00:21:37 +0000 | [diff] [blame] | 2719 | if (log && log->GetVerbose()) |
Jim Ingham | ff276fe | 2011-02-08 05:19:01 +0000 | [diff] [blame] | 2720 | log->Printf ("Disabling new thread notification breakpoint."); |
Greg Clayton | bd5c23d | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2721 | |
| 2722 | if (m_thread_create_bp_sp) |
| 2723 | m_thread_create_bp_sp->SetEnabled(false); |
| 2724 | |
Jim Ingham | 55e01d8 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2725 | return true; |
| 2726 | } |
| 2727 | |
| 2728 | |