Chris Lattner | 30fdc8d | 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 | |
Daniel Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 11 | #include "lldb/Host/Config.h" |
Daniel Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 12 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | // C Includes |
| 14 | #include <errno.h> |
Stephen Wilson | a78867b | 2011-03-25 18:16:28 +0000 | [diff] [blame] | 15 | #include <stdlib.h> |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 16 | #ifndef LLDB_DISABLE_POSIX |
| 17 | #include <spawn.h> |
Sean Callanan | 224f6f5 | 2012-07-19 18:07:36 +0000 | [diff] [blame] | 18 | #include <netinet/in.h> |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 19 | #include <sys/mman.h> // for mmap |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 20 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include <sys/stat.h> |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 22 | #include <sys/types.h> |
Stephen Wilson | dc91686 | 2011-03-30 00:12:40 +0000 | [diff] [blame] | 23 | #include <time.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | |
| 25 | // C++ Includes |
| 26 | #include <algorithm> |
| 27 | #include <map> |
| 28 | |
| 29 | // Other libraries and framework includes |
| 30 | |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 31 | #include "lldb/Breakpoint/Watchpoint.h" |
Jim Ingham | 40af72e | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 32 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | #include "lldb/Core/ArchSpec.h" |
| 34 | #include "lldb/Core/Debugger.h" |
| 35 | #include "lldb/Core/ConnectionFileDescriptor.h" |
Greg Clayton | 53239f0 | 2011-02-08 05:05:52 +0000 | [diff] [blame] | 36 | #include "lldb/Host/FileSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | #include "lldb/Core/InputReader.h" |
| 38 | #include "lldb/Core/Module.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 39 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | #include "lldb/Core/PluginManager.h" |
| 41 | #include "lldb/Core/State.h" |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 42 | #include "lldb/Core/StreamFile.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | #include "lldb/Core/StreamString.h" |
| 44 | #include "lldb/Core/Timer.h" |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 45 | #include "lldb/Core/Value.h" |
Jason Molenda | d1fae14 | 2012-09-29 08:03:33 +0000 | [diff] [blame] | 46 | #include "lldb/Host/Symbols.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | #include "lldb/Host/TimeValue.h" |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 48 | #include "lldb/Interpreter/CommandInterpreter.h" |
Greg Clayton | 1d19a2f | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 49 | #include "lldb/Interpreter/CommandObject.h" |
| 50 | #include "lldb/Interpreter/CommandObjectMultiword.h" |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 51 | #include "lldb/Interpreter/CommandReturnObject.h" |
Joerg Sonnenberger | e77b042 | 2013-10-20 17:36:05 +0000 | [diff] [blame] | 52 | #ifndef LLDB_DISABLE_PYTHON |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 53 | #include "lldb/Interpreter/PythonDataObjects.h" |
Joerg Sonnenberger | e77b042 | 2013-10-20 17:36:05 +0000 | [diff] [blame] | 54 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | #include "lldb/Symbol/ObjectFile.h" |
| 56 | #include "lldb/Target/DynamicLoader.h" |
| 57 | #include "lldb/Target/Target.h" |
| 58 | #include "lldb/Target/TargetList.h" |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 59 | #include "lldb/Target/ThreadPlanCallFunction.h" |
Jason Molenda | a34a0c6 | 2010-06-09 21:28:42 +0000 | [diff] [blame] | 60 | #include "lldb/Utility/PseudoTerminal.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | |
| 62 | // Project includes |
| 63 | #include "lldb/Host/Host.h" |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 64 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
Jason Molenda | c42d243 | 2012-07-25 03:40:06 +0000 | [diff] [blame] | 65 | #include "Plugins/Process/Utility/StopInfoMachException.h" |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 66 | #include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h" |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 67 | #include "Utility/StringExtractorGDBRemote.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 68 | #include "GDBRemoteRegisterContext.h" |
| 69 | #include "ProcessGDBRemote.h" |
| 70 | #include "ProcessGDBRemoteLog.h" |
| 71 | #include "ThreadGDBRemote.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 72 | |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 73 | |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 74 | namespace lldb |
| 75 | { |
| 76 | // Provide a function that can easily dump the packet history if we know a |
| 77 | // ProcessGDBRemote * value (which we can get from logs or from debugging). |
| 78 | // We need the function in the lldb namespace so it makes it into the final |
| 79 | // executable since the LLDB shared library only exports stuff in the lldb |
| 80 | // namespace. This allows you to attach with a debugger and call this |
| 81 | // function and get the packet history dumped to a file. |
| 82 | void |
| 83 | DumpProcessGDBRemotePacketHistory (void *p, const char *path) |
| 84 | { |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 85 | lldb_private::StreamFile strm; |
| 86 | lldb_private::Error error (strm.GetFile().Open(path, lldb_private::File::eOpenOptionWrite | lldb_private::File::eOpenOptionCanCreate)); |
| 87 | if (error.Success()) |
| 88 | ((ProcessGDBRemote *)p)->GetGDBRemote().DumpHistory (strm); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 89 | } |
Filipe Cabecinhas | c34f776 | 2012-05-23 16:27:09 +0000 | [diff] [blame] | 90 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | #define DEBUGSERVER_BASENAME "debugserver" |
| 93 | using namespace lldb; |
| 94 | using namespace lldb_private; |
| 95 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 96 | |
| 97 | namespace { |
| 98 | |
| 99 | static PropertyDefinition |
| 100 | g_properties[] = |
| 101 | { |
| 102 | { "packet-timeout" , OptionValue::eTypeUInt64 , true , 1, NULL, NULL, "Specify the default packet timeout in seconds." }, |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 103 | { "target-definition-file" , OptionValue::eTypeFileSpec , true, 0 , NULL, NULL, "The file that provides the description for remote target registers." }, |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 104 | { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL } |
| 105 | }; |
| 106 | |
| 107 | enum |
| 108 | { |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 109 | ePropertyPacketTimeout, |
| 110 | ePropertyTargetDefinitionFile |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | class PluginProperties : public Properties |
| 114 | { |
| 115 | public: |
| 116 | |
| 117 | static ConstString |
| 118 | GetSettingName () |
| 119 | { |
| 120 | return ProcessGDBRemote::GetPluginNameStatic(); |
| 121 | } |
| 122 | |
| 123 | PluginProperties() : |
| 124 | Properties () |
| 125 | { |
| 126 | m_collection_sp.reset (new OptionValueProperties(GetSettingName())); |
| 127 | m_collection_sp->Initialize(g_properties); |
| 128 | } |
| 129 | |
| 130 | virtual |
| 131 | ~PluginProperties() |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | uint64_t |
| 136 | GetPacketTimeout() |
| 137 | { |
| 138 | const uint32_t idx = ePropertyPacketTimeout; |
| 139 | return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value); |
| 140 | } |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 141 | |
| 142 | bool |
| 143 | SetPacketTimeout(uint64_t timeout) |
| 144 | { |
| 145 | const uint32_t idx = ePropertyPacketTimeout; |
| 146 | return m_collection_sp->SetPropertyAtIndexAsUInt64(NULL, idx, timeout); |
| 147 | } |
| 148 | |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 149 | FileSpec |
| 150 | GetTargetDefinitionFile () const |
| 151 | { |
| 152 | const uint32_t idx = ePropertyTargetDefinitionFile; |
| 153 | return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx); |
| 154 | } |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP; |
| 158 | |
| 159 | static const ProcessKDPPropertiesSP & |
| 160 | GetGlobalPluginProperties() |
| 161 | { |
| 162 | static ProcessKDPPropertiesSP g_settings_sp; |
| 163 | if (!g_settings_sp) |
| 164 | g_settings_sp.reset (new PluginProperties ()); |
| 165 | return g_settings_sp; |
| 166 | } |
| 167 | |
| 168 | } // anonymous namespace end |
| 169 | |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 170 | lldb_private::ConstString |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 171 | ProcessGDBRemote::GetPluginNameStatic() |
| 172 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 173 | static ConstString g_name("gdb-remote"); |
| 174 | return g_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | const char * |
| 178 | ProcessGDBRemote::GetPluginDescriptionStatic() |
| 179 | { |
| 180 | return "GDB Remote protocol based debugging plug-in."; |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | ProcessGDBRemote::Terminate() |
| 185 | { |
| 186 | PluginManager::UnregisterPlugin (ProcessGDBRemote::CreateInstance); |
| 187 | } |
| 188 | |
| 189 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 190 | lldb::ProcessSP |
| 191 | ProcessGDBRemote::CreateInstance (Target &target, Listener &listener, const FileSpec *crash_file_path) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 192 | { |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 193 | lldb::ProcessSP process_sp; |
| 194 | if (crash_file_path == NULL) |
| 195 | process_sp.reset (new ProcessGDBRemote (target, listener)); |
| 196 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | bool |
Greg Clayton | 3a29bdb | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 200 | ProcessGDBRemote::CanDebug (Target &target, bool plugin_specified_by_name) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 201 | { |
Greg Clayton | 596ed24 | 2011-10-21 21:41:45 +0000 | [diff] [blame] | 202 | if (plugin_specified_by_name) |
| 203 | return true; |
| 204 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 205 | // For now we are just making sure the file exists for a given module |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 206 | Module *exe_module = target.GetExecutableModulePointer(); |
| 207 | if (exe_module) |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 208 | { |
| 209 | ObjectFile *exe_objfile = exe_module->GetObjectFile(); |
| 210 | // We can't debug core files... |
| 211 | switch (exe_objfile->GetType()) |
| 212 | { |
| 213 | case ObjectFile::eTypeInvalid: |
| 214 | case ObjectFile::eTypeCoreFile: |
| 215 | case ObjectFile::eTypeDebugInfo: |
| 216 | case ObjectFile::eTypeObjectFile: |
| 217 | case ObjectFile::eTypeSharedLibrary: |
| 218 | case ObjectFile::eTypeStubLibrary: |
| 219 | return false; |
| 220 | case ObjectFile::eTypeExecutable: |
| 221 | case ObjectFile::eTypeDynamicLinker: |
| 222 | case ObjectFile::eTypeUnknown: |
| 223 | break; |
| 224 | } |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 225 | return exe_module->GetFileSpec().Exists(); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 226 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 227 | // However, if there is no executable module, we return true since we might be preparing to attach. |
| 228 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | //---------------------------------------------------------------------- |
| 232 | // ProcessGDBRemote constructor |
| 233 | //---------------------------------------------------------------------- |
| 234 | ProcessGDBRemote::ProcessGDBRemote(Target& target, Listener &listener) : |
| 235 | Process (target, listener), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 236 | m_flags (0), |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 237 | m_gdb_comm(false), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 238 | m_debugserver_pid (LLDB_INVALID_PROCESS_ID), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 239 | m_last_stop_packet (), |
Greg Clayton | 09c3e3d | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 240 | m_last_stop_packet_mutex (Mutex::eMutexTypeNormal), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 241 | m_register_info (), |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 242 | m_async_broadcaster (NULL, "lldb.process.gdb-remote.async-broadcaster"), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 243 | m_async_thread (LLDB_INVALID_HOST_THREAD), |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 244 | m_async_thread_state(eAsyncThreadNotStarted), |
| 245 | m_async_thread_state_mutex(Mutex::eMutexTypeRecursive), |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 246 | m_thread_ids (), |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 247 | m_continue_c_tids (), |
| 248 | m_continue_C_tids (), |
| 249 | m_continue_s_tids (), |
| 250 | m_continue_S_tids (), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 251 | m_max_memory_size (512), |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 252 | m_addr_to_mmap_size (), |
| 253 | m_thread_create_bp_sp (), |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 254 | m_waiting_for_attach (false), |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 255 | m_destroy_tried_resuming (false), |
Hafiz Abid Qadeer | 85a4daf | 2013-10-18 10:04:33 +0000 | [diff] [blame] | 256 | m_command_sp (), |
| 257 | m_breakpoint_pc_offset (0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 258 | { |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 259 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit"); |
| 260 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue"); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 261 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadDidExit, "async thread did exit"); |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 262 | const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout(); |
| 263 | if (timeout_seconds > 0) |
| 264 | m_gdb_comm.SetPacketTimeout(timeout_seconds); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | //---------------------------------------------------------------------- |
| 268 | // Destructor |
| 269 | //---------------------------------------------------------------------- |
| 270 | ProcessGDBRemote::~ProcessGDBRemote() |
| 271 | { |
| 272 | // m_mach_process.UnregisterNotificationCallbacks (this); |
| 273 | Clear(); |
Greg Clayton | 1ed54f5 | 2011-10-01 00:45:15 +0000 | [diff] [blame] | 274 | // We need to call finalize on the process before destroying ourselves |
| 275 | // to make sure all of the broadcaster cleanup goes as planned. If we |
| 276 | // destruct this class, then Process::~Process() might have problems |
| 277 | // trying to fully destroy the broadcaster. |
| 278 | Finalize(); |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 279 | |
| 280 | // The general Finalize is going to try to destroy the process and that SHOULD |
| 281 | // shut down the async thread. However, if we don't kill it it will get stranded and |
| 282 | // its connection will go away so when it wakes up it will crash. So kill it for sure here. |
| 283 | StopAsyncThread(); |
| 284 | KillDebugserverProcess(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | //---------------------------------------------------------------------- |
| 288 | // PluginInterface |
| 289 | //---------------------------------------------------------------------- |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 290 | ConstString |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 291 | ProcessGDBRemote::GetPluginName() |
| 292 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 293 | return GetPluginNameStatic(); |
| 294 | } |
| 295 | |
| 296 | uint32_t |
| 297 | ProcessGDBRemote::GetPluginVersion() |
| 298 | { |
| 299 | return 1; |
| 300 | } |
| 301 | |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 302 | bool |
| 303 | ProcessGDBRemote::ParsePythonTargetDefinition(const FileSpec &target_definition_fspec) |
| 304 | { |
Joerg Sonnenberger | e77b042 | 2013-10-20 17:36:05 +0000 | [diff] [blame] | 305 | #ifndef LLDB_DISABLE_PYTHON |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 306 | ScriptInterpreter *interpreter = GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); |
| 307 | Error error; |
| 308 | lldb::ScriptInterpreterObjectSP module_object_sp (interpreter->LoadPluginModule(target_definition_fspec, error)); |
| 309 | if (module_object_sp) |
| 310 | { |
| 311 | lldb::ScriptInterpreterObjectSP target_definition_sp (interpreter->GetDynamicSettings(module_object_sp, |
| 312 | &GetTarget(), |
| 313 | "gdb-server-target-definition", |
| 314 | error)); |
| 315 | |
| 316 | PythonDictionary target_dict(target_definition_sp); |
| 317 | |
| 318 | if (target_dict) |
| 319 | { |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 320 | PythonDictionary host_info_dict (target_dict.GetItemForKey("host-info")); |
| 321 | if (host_info_dict) |
| 322 | { |
| 323 | ArchSpec host_arch (host_info_dict.GetItemForKeyAsString(PythonString("triple"))); |
| 324 | |
| 325 | if (!host_arch.IsCompatibleMatch(GetTarget().GetArchitecture())) |
| 326 | { |
| 327 | GetTarget().SetArchitecture(host_arch); |
| 328 | } |
| 329 | |
| 330 | } |
Hafiz Abid Qadeer | 85a4daf | 2013-10-18 10:04:33 +0000 | [diff] [blame] | 331 | m_breakpoint_pc_offset = target_dict.GetItemForKeyAsInteger("breakpoint-pc-offset", 0); |
| 332 | |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 333 | if (m_register_info.SetRegisterInfo (target_dict, GetTarget().GetArchitecture().GetByteOrder()) > 0) |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 334 | { |
| 335 | return true; |
| 336 | } |
| 337 | } |
| 338 | } |
Joerg Sonnenberger | e77b042 | 2013-10-20 17:36:05 +0000 | [diff] [blame] | 339 | #endif |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 340 | return false; |
| 341 | } |
| 342 | |
| 343 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 344 | void |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 345 | ProcessGDBRemote::BuildDynamicRegisterInfo (bool force) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 346 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 347 | if (!force && m_register_info.GetNumRegisters() > 0) |
| 348 | return; |
| 349 | |
| 350 | char packet[128]; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 351 | m_register_info.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 352 | uint32_t reg_offset = 0; |
| 353 | uint32_t reg_num = 0; |
Greg Clayton | 23f5950 | 2012-07-17 03:23:13 +0000 | [diff] [blame] | 354 | for (StringExtractorGDBRemote::ResponseType response_type = StringExtractorGDBRemote::eResponse; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 355 | response_type == StringExtractorGDBRemote::eResponse; |
| 356 | ++reg_num) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 357 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 358 | const int packet_len = ::snprintf (packet, sizeof(packet), "qRegisterInfo%x", reg_num); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 359 | assert (packet_len < (int)sizeof(packet)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 360 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 361 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, false) == GDBRemoteCommunication::PacketResult::Success) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 362 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 363 | response_type = response.GetResponseType(); |
| 364 | if (response_type == StringExtractorGDBRemote::eResponse) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 365 | { |
| 366 | std::string name; |
| 367 | std::string value; |
| 368 | ConstString reg_name; |
| 369 | ConstString alt_name; |
| 370 | ConstString set_name; |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 371 | std::vector<uint32_t> value_regs; |
| 372 | std::vector<uint32_t> invalidate_regs; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 373 | RegisterInfo reg_info = { NULL, // Name |
| 374 | NULL, // Alt name |
| 375 | 0, // byte size |
| 376 | reg_offset, // offset |
| 377 | eEncodingUint, // encoding |
| 378 | eFormatHex, // formate |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 379 | { |
| 380 | LLDB_INVALID_REGNUM, // GCC reg num |
| 381 | LLDB_INVALID_REGNUM, // DWARF reg num |
| 382 | LLDB_INVALID_REGNUM, // generic reg num |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 383 | reg_num, // GDB reg num |
| 384 | reg_num // native register number |
Greg Clayton | 435d85a | 2012-02-29 19:27:27 +0000 | [diff] [blame] | 385 | }, |
| 386 | NULL, |
| 387 | NULL |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 388 | }; |
| 389 | |
| 390 | while (response.GetNameColonValue(name, value)) |
| 391 | { |
| 392 | if (name.compare("name") == 0) |
| 393 | { |
| 394 | reg_name.SetCString(value.c_str()); |
| 395 | } |
| 396 | else if (name.compare("alt-name") == 0) |
| 397 | { |
| 398 | alt_name.SetCString(value.c_str()); |
| 399 | } |
| 400 | else if (name.compare("bitsize") == 0) |
| 401 | { |
| 402 | reg_info.byte_size = Args::StringToUInt32(value.c_str(), 0, 0) / CHAR_BIT; |
| 403 | } |
| 404 | else if (name.compare("offset") == 0) |
| 405 | { |
| 406 | uint32_t offset = Args::StringToUInt32(value.c_str(), UINT32_MAX, 0); |
Jason Molenda | 743e86a | 2010-06-11 23:44:18 +0000 | [diff] [blame] | 407 | if (reg_offset != offset) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 408 | { |
| 409 | reg_offset = offset; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | else if (name.compare("encoding") == 0) |
| 413 | { |
Greg Clayton | 2443cbd | 2012-08-24 01:42:50 +0000 | [diff] [blame] | 414 | const Encoding encoding = Args::StringToEncoding (value.c_str()); |
| 415 | if (encoding != eEncodingInvalid) |
| 416 | reg_info.encoding = encoding; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 417 | } |
| 418 | else if (name.compare("format") == 0) |
| 419 | { |
Greg Clayton | 2443cbd | 2012-08-24 01:42:50 +0000 | [diff] [blame] | 420 | Format format = eFormatInvalid; |
| 421 | if (Args::StringToFormat (value.c_str(), format, NULL).Success()) |
| 422 | reg_info.format = format; |
| 423 | else if (value.compare("binary") == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 424 | reg_info.format = eFormatBinary; |
| 425 | else if (value.compare("decimal") == 0) |
| 426 | reg_info.format = eFormatDecimal; |
| 427 | else if (value.compare("hex") == 0) |
| 428 | reg_info.format = eFormatHex; |
| 429 | else if (value.compare("float") == 0) |
| 430 | reg_info.format = eFormatFloat; |
| 431 | else if (value.compare("vector-sint8") == 0) |
| 432 | reg_info.format = eFormatVectorOfSInt8; |
| 433 | else if (value.compare("vector-uint8") == 0) |
| 434 | reg_info.format = eFormatVectorOfUInt8; |
| 435 | else if (value.compare("vector-sint16") == 0) |
| 436 | reg_info.format = eFormatVectorOfSInt16; |
| 437 | else if (value.compare("vector-uint16") == 0) |
| 438 | reg_info.format = eFormatVectorOfUInt16; |
| 439 | else if (value.compare("vector-sint32") == 0) |
| 440 | reg_info.format = eFormatVectorOfSInt32; |
| 441 | else if (value.compare("vector-uint32") == 0) |
| 442 | reg_info.format = eFormatVectorOfUInt32; |
| 443 | else if (value.compare("vector-float32") == 0) |
| 444 | reg_info.format = eFormatVectorOfFloat32; |
| 445 | else if (value.compare("vector-uint128") == 0) |
| 446 | reg_info.format = eFormatVectorOfUInt128; |
| 447 | } |
| 448 | else if (name.compare("set") == 0) |
| 449 | { |
| 450 | set_name.SetCString(value.c_str()); |
| 451 | } |
| 452 | else if (name.compare("gcc") == 0) |
| 453 | { |
| 454 | reg_info.kinds[eRegisterKindGCC] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0); |
| 455 | } |
| 456 | else if (name.compare("dwarf") == 0) |
| 457 | { |
| 458 | reg_info.kinds[eRegisterKindDWARF] = Args::StringToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0); |
| 459 | } |
| 460 | else if (name.compare("generic") == 0) |
| 461 | { |
Greg Clayton | 2443cbd | 2012-08-24 01:42:50 +0000 | [diff] [blame] | 462 | reg_info.kinds[eRegisterKindGeneric] = Args::StringToGenericRegister (value.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 463 | } |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 464 | else if (name.compare("container-regs") == 0) |
| 465 | { |
| 466 | std::pair<llvm::StringRef, llvm::StringRef> value_pair; |
| 467 | value_pair.second = value; |
| 468 | do |
| 469 | { |
| 470 | value_pair = value_pair.second.split(','); |
| 471 | if (!value_pair.first.empty()) |
| 472 | { |
Greg Clayton | 0ba2024 | 2013-01-21 23:32:42 +0000 | [diff] [blame] | 473 | uint32_t reg = Args::StringToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, 16); |
| 474 | if (reg != LLDB_INVALID_REGNUM) |
| 475 | value_regs.push_back (reg); |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 476 | } |
| 477 | } while (!value_pair.second.empty()); |
| 478 | } |
| 479 | else if (name.compare("invalidate-regs") == 0) |
| 480 | { |
| 481 | std::pair<llvm::StringRef, llvm::StringRef> value_pair; |
| 482 | value_pair.second = value; |
| 483 | do |
| 484 | { |
| 485 | value_pair = value_pair.second.split(','); |
| 486 | if (!value_pair.first.empty()) |
| 487 | { |
Greg Clayton | 0ba2024 | 2013-01-21 23:32:42 +0000 | [diff] [blame] | 488 | uint32_t reg = Args::StringToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, 16); |
| 489 | if (reg != LLDB_INVALID_REGNUM) |
| 490 | invalidate_regs.push_back (reg); |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 491 | } |
| 492 | } while (!value_pair.second.empty()); |
| 493 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Jason Molenda | 743e86a | 2010-06-11 23:44:18 +0000 | [diff] [blame] | 496 | reg_info.byte_offset = reg_offset; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 497 | assert (reg_info.byte_size != 0); |
| 498 | reg_offset += reg_info.byte_size; |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 499 | if (!value_regs.empty()) |
| 500 | { |
| 501 | value_regs.push_back(LLDB_INVALID_REGNUM); |
| 502 | reg_info.value_regs = value_regs.data(); |
| 503 | } |
| 504 | if (!invalidate_regs.empty()) |
| 505 | { |
| 506 | invalidate_regs.push_back(LLDB_INVALID_REGNUM); |
| 507 | reg_info.invalidate_regs = invalidate_regs.data(); |
| 508 | } |
| 509 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 510 | m_register_info.AddRegister(reg_info, reg_name, alt_name, set_name); |
| 511 | } |
| 512 | } |
| 513 | else |
| 514 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 515 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 519 | // Check if qHostInfo specified a specific packet timeout for this connection. |
| 520 | // If so then lets update our setting so the user knows what the timeout is |
| 521 | // and can see it. |
| 522 | const uint32_t host_packet_timeout = m_gdb_comm.GetHostDefaultPacketTimeout(); |
| 523 | if (host_packet_timeout) |
| 524 | { |
| 525 | GetGlobalPluginProperties()->SetPacketTimeout(host_packet_timeout); |
| 526 | } |
| 527 | |
| 528 | |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 529 | if (reg_num == 0) |
| 530 | { |
| 531 | FileSpec target_definition_fspec = GetGlobalPluginProperties()->GetTargetDefinitionFile (); |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 532 | |
| 533 | if (target_definition_fspec) |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 534 | { |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 535 | // See if we can get register definitions from a python file |
| 536 | if (ParsePythonTargetDefinition (target_definition_fspec)) |
| 537 | return; |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 541 | // We didn't get anything if the accumulated reg_num is zero. See if we are |
| 542 | // debugging ARM and fill with a hard coded register set until we can get an |
| 543 | // updated debugserver down on the devices. |
| 544 | // On the other hand, if the accumulated reg_num is positive, see if we can |
| 545 | // add composite registers to the existing primordial ones. |
| 546 | bool from_scratch = (reg_num == 0); |
| 547 | |
| 548 | const ArchSpec &target_arch = GetTarget().GetArchitecture(); |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 549 | const ArchSpec &remote_host_arch = m_gdb_comm.GetHostArchitecture(); |
| 550 | const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture(); |
| 551 | |
| 552 | // Use the process' architecture instead of the host arch, if available |
| 553 | ArchSpec remote_arch; |
| 554 | if (remote_process_arch.IsValid ()) |
| 555 | remote_arch = remote_process_arch; |
| 556 | else |
| 557 | remote_arch = remote_host_arch; |
| 558 | |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 559 | if (!target_arch.IsValid()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 560 | { |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 561 | if (remote_arch.IsValid() |
| 562 | && remote_arch.GetMachine() == llvm::Triple::arm |
| 563 | && remote_arch.GetTriple().getVendor() == llvm::Triple::Apple) |
| 564 | m_register_info.HardcodeARMRegisters(from_scratch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 565 | } |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 566 | else if (target_arch.GetMachine() == llvm::Triple::arm) |
| 567 | { |
| 568 | m_register_info.HardcodeARMRegisters(from_scratch); |
| 569 | } |
| 570 | |
| 571 | // At this point, we can finalize our register info. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 572 | m_register_info.Finalize (); |
| 573 | } |
| 574 | |
| 575 | Error |
| 576 | ProcessGDBRemote::WillLaunch (Module* module) |
| 577 | { |
| 578 | return WillLaunchOrAttach (); |
| 579 | } |
| 580 | |
| 581 | Error |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 582 | ProcessGDBRemote::WillAttachToProcessWithID (lldb::pid_t pid) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 583 | { |
| 584 | return WillLaunchOrAttach (); |
| 585 | } |
| 586 | |
| 587 | Error |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 588 | ProcessGDBRemote::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 589 | { |
| 590 | return WillLaunchOrAttach (); |
| 591 | } |
| 592 | |
| 593 | Error |
Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 594 | ProcessGDBRemote::DoConnectRemote (Stream *strm, const char *remote_url) |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 595 | { |
| 596 | Error error (WillLaunchOrAttach ()); |
| 597 | |
| 598 | if (error.Fail()) |
| 599 | return error; |
| 600 | |
Greg Clayton | 2289fa4 | 2011-04-30 01:09:13 +0000 | [diff] [blame] | 601 | error = ConnectToDebugserver (remote_url); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 602 | |
| 603 | if (error.Fail()) |
| 604 | return error; |
| 605 | StartAsyncThread (); |
| 606 | |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 607 | lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID (); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 608 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 609 | { |
| 610 | // We don't have a valid process ID, so note that we are connected |
| 611 | // and could now request to launch or attach, or get remote process |
| 612 | // listings... |
| 613 | SetPrivateState (eStateConnected); |
| 614 | } |
| 615 | else |
| 616 | { |
| 617 | // We have a valid process |
| 618 | SetID (pid); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 619 | GetThreadList(); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 620 | if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false) == GDBRemoteCommunication::PacketResult::Success) |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 621 | { |
Jason Molenda | c62bd7b | 2013-12-21 05:20:36 +0000 | [diff] [blame^] | 622 | if (!m_target.GetArchitecture().IsValid()) |
| 623 | { |
| 624 | if (m_gdb_comm.GetProcessArchitecture().IsValid()) |
| 625 | { |
| 626 | m_target.SetArchitecture(m_gdb_comm.GetProcessArchitecture()); |
| 627 | } |
| 628 | else |
| 629 | { |
| 630 | m_target.SetArchitecture(m_gdb_comm.GetHostArchitecture()); |
| 631 | } |
Carlo Kok | 7438912 | 2013-10-14 07:09:13 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Greg Clayton | dd0e5a5 | 2011-06-02 22:22:38 +0000 | [diff] [blame] | 634 | const StateType state = SetThreadStopInfo (m_last_stop_packet); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 635 | if (state == eStateStopped) |
| 636 | { |
| 637 | SetPrivateState (state); |
| 638 | } |
| 639 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 640 | error.SetErrorStringWithFormat ("Process %" PRIu64 " was reported after connecting to '%s', but state was not stopped: %s", pid, remote_url, StateAsCString (state)); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 641 | } |
| 642 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 643 | error.SetErrorStringWithFormat ("Process %" PRIu64 " was reported after connecting to '%s', but no stop reply packet was received", pid, remote_url); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 644 | } |
Jason Molenda | 16d127c | 2012-05-03 22:37:30 +0000 | [diff] [blame] | 645 | |
| 646 | if (error.Success() |
| 647 | && !GetTarget().GetArchitecture().IsValid() |
| 648 | && m_gdb_comm.GetHostArchitecture().IsValid()) |
| 649 | { |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 650 | // Prefer the *process'* architecture over that of the *host*, if available. |
| 651 | if (m_gdb_comm.GetProcessArchitecture().IsValid()) |
| 652 | GetTarget().SetArchitecture(m_gdb_comm.GetProcessArchitecture()); |
| 653 | else |
| 654 | GetTarget().SetArchitecture(m_gdb_comm.GetHostArchitecture()); |
Jason Molenda | 16d127c | 2012-05-03 22:37:30 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 657 | return error; |
| 658 | } |
| 659 | |
| 660 | Error |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 661 | ProcessGDBRemote::WillLaunchOrAttach () |
| 662 | { |
| 663 | Error error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 664 | m_stdio_communication.Clear (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 665 | return error; |
| 666 | } |
| 667 | |
| 668 | //---------------------------------------------------------------------- |
| 669 | // Process Control |
| 670 | //---------------------------------------------------------------------- |
| 671 | Error |
Jean-Daniel Dupas | 7782de9 | 2013-12-09 22:52:50 +0000 | [diff] [blame] | 672 | ProcessGDBRemote::DoLaunch (Module *exe_module, ProcessLaunchInfo &launch_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 673 | { |
Greg Clayton | 4957bf6 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 674 | Error error; |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 675 | |
| 676 | uint32_t launch_flags = launch_info.GetFlags().Get(); |
| 677 | const char *stdin_path = NULL; |
| 678 | const char *stdout_path = NULL; |
| 679 | const char *stderr_path = NULL; |
| 680 | const char *working_dir = launch_info.GetWorkingDirectory(); |
| 681 | |
| 682 | const ProcessLaunchInfo::FileAction *file_action; |
| 683 | file_action = launch_info.GetFileActionForFD (STDIN_FILENO); |
| 684 | if (file_action) |
| 685 | { |
| 686 | if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen) |
| 687 | stdin_path = file_action->GetPath(); |
| 688 | } |
| 689 | file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); |
| 690 | if (file_action) |
| 691 | { |
| 692 | if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen) |
| 693 | stdout_path = file_action->GetPath(); |
| 694 | } |
| 695 | file_action = launch_info.GetFileActionForFD (STDERR_FILENO); |
| 696 | if (file_action) |
| 697 | { |
| 698 | if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen) |
| 699 | stderr_path = file_action->GetPath(); |
| 700 | } |
| 701 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 702 | // ::LogSetBitMask (GDBR_LOG_DEFAULT); |
| 703 | // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD); |
| 704 | // ::LogSetLogFile ("/dev/stdout"); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 705 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 706 | |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 707 | ObjectFile * object_file = exe_module->GetObjectFile(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 708 | if (object_file) |
| 709 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 710 | // Make sure we aren't already connected? |
| 711 | if (!m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 712 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 713 | error = LaunchAndConnectToDebugserver (launch_info); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | if (error.Success()) |
| 717 | { |
| 718 | lldb_utility::PseudoTerminal pty; |
| 719 | const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0; |
Greg Clayton | 5f2a4f9 | 2011-03-02 21:34:46 +0000 | [diff] [blame] | 720 | |
| 721 | // If the debugserver is local and we aren't disabling STDIO, lets use |
| 722 | // a pseudo terminal to instead of relying on the 'O' packets for stdio |
| 723 | // since 'O' packets can really slow down debugging if the inferior |
| 724 | // does a lot of output. |
Greg Clayton | f58c269 | 2011-06-24 22:32:10 +0000 | [diff] [blame] | 725 | PlatformSP platform_sp (m_target.GetPlatform()); |
| 726 | if (platform_sp && platform_sp->IsHost() && !disable_stdio) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 727 | { |
| 728 | const char *slave_name = NULL; |
| 729 | if (stdin_path == NULL || stdout_path == NULL || stderr_path == NULL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 730 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 731 | if (pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, NULL, 0)) |
| 732 | slave_name = pty.GetSlaveName (NULL, 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 733 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 734 | if (stdin_path == NULL) |
| 735 | stdin_path = slave_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 736 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 737 | if (stdout_path == NULL) |
| 738 | stdout_path = slave_name; |
| 739 | |
| 740 | if (stderr_path == NULL) |
| 741 | stderr_path = slave_name; |
| 742 | } |
| 743 | |
Greg Clayton | 5f2a4f9 | 2011-03-02 21:34:46 +0000 | [diff] [blame] | 744 | // Set STDIN to /dev/null if we want STDIO disabled or if either |
| 745 | // STDOUT or STDERR have been set to something and STDIN hasn't |
| 746 | if (disable_stdio || (stdin_path == NULL && (stdout_path || stderr_path))) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 747 | stdin_path = "/dev/null"; |
| 748 | |
Greg Clayton | 5f2a4f9 | 2011-03-02 21:34:46 +0000 | [diff] [blame] | 749 | // Set STDOUT to /dev/null if we want STDIO disabled or if either |
| 750 | // STDIN or STDERR have been set to something and STDOUT hasn't |
| 751 | if (disable_stdio || (stdout_path == NULL && (stdin_path || stderr_path))) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 752 | stdout_path = "/dev/null"; |
| 753 | |
Greg Clayton | 5f2a4f9 | 2011-03-02 21:34:46 +0000 | [diff] [blame] | 754 | // Set STDERR to /dev/null if we want STDIO disabled or if either |
| 755 | // STDIN or STDOUT have been set to something and STDERR hasn't |
| 756 | if (disable_stdio || (stderr_path == NULL && (stdin_path || stdout_path))) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 757 | stderr_path = "/dev/null"; |
| 758 | |
| 759 | if (stdin_path) |
| 760 | m_gdb_comm.SetSTDIN (stdin_path); |
| 761 | if (stdout_path) |
| 762 | m_gdb_comm.SetSTDOUT (stdout_path); |
| 763 | if (stderr_path) |
| 764 | m_gdb_comm.SetSTDERR (stderr_path); |
| 765 | |
| 766 | m_gdb_comm.SetDisableASLR (launch_flags & eLaunchFlagDisableASLR); |
| 767 | |
Greg Clayton | c4103b3 | 2011-05-08 04:53:50 +0000 | [diff] [blame] | 768 | m_gdb_comm.SendLaunchArchPacket (m_target.GetArchitecture().GetArchitectureName()); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 769 | |
| 770 | if (working_dir && working_dir[0]) |
| 771 | { |
| 772 | m_gdb_comm.SetWorkingDir (working_dir); |
| 773 | } |
| 774 | |
| 775 | // Send the environment and the program + arguments after we connect |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 776 | const Args &environment = launch_info.GetEnvironmentEntries(); |
| 777 | if (environment.GetArgumentCount()) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 778 | { |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 779 | size_t num_environment_entries = environment.GetArgumentCount(); |
| 780 | for (size_t i=0; i<num_environment_entries; ++i) |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 781 | { |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 782 | const char *env_entry = environment.GetArgumentAtIndex(i); |
| 783 | if (env_entry == NULL || m_gdb_comm.SendEnvironmentPacket(env_entry) != 0) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 784 | break; |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 785 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 786 | } |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 787 | |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 788 | const uint32_t old_packet_timeout = m_gdb_comm.SetPacketTimeout (10); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 789 | int arg_packet_err = m_gdb_comm.SendArgumentsPacket (launch_info); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 790 | if (arg_packet_err == 0) |
| 791 | { |
| 792 | std::string error_str; |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 793 | if (m_gdb_comm.GetLaunchSuccess (error_str)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 794 | { |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 795 | SetID (m_gdb_comm.GetCurrentProcessID ()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 796 | } |
| 797 | else |
| 798 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 799 | error.SetErrorString (error_str.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 800 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 801 | } |
| 802 | else |
| 803 | { |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 804 | error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 805 | } |
Greg Clayton | 8b45bee | 2011-08-10 22:05:39 +0000 | [diff] [blame] | 806 | |
| 807 | m_gdb_comm.SetPacketTimeout (old_packet_timeout); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 808 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 809 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 810 | { |
Johnny Chen | 4c1e920 | 2011-08-09 18:56:45 +0000 | [diff] [blame] | 811 | if (log) |
| 812 | log->Printf("failed to connect to debugserver: %s", error.AsCString()); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 813 | KillDebugserverProcess (); |
| 814 | return error; |
| 815 | } |
| 816 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 817 | if (m_gdb_comm.SendPacketAndWaitForResponse("?", 1, m_last_stop_packet, false) == GDBRemoteCommunication::PacketResult::Success) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 818 | { |
Jason Molenda | c62bd7b | 2013-12-21 05:20:36 +0000 | [diff] [blame^] | 819 | if (!m_target.GetArchitecture().IsValid()) |
| 820 | { |
| 821 | if (m_gdb_comm.GetProcessArchitecture().IsValid()) |
| 822 | { |
| 823 | m_target.SetArchitecture(m_gdb_comm.GetProcessArchitecture()); |
| 824 | } |
| 825 | else |
| 826 | { |
| 827 | m_target.SetArchitecture(m_gdb_comm.GetHostArchitecture()); |
| 828 | } |
Carlo Kok | 7438912 | 2013-10-14 07:09:13 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Greg Clayton | dd0e5a5 | 2011-06-02 22:22:38 +0000 | [diff] [blame] | 831 | SetPrivateState (SetThreadStopInfo (m_last_stop_packet)); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 832 | |
| 833 | if (!disable_stdio) |
| 834 | { |
| 835 | if (pty.GetMasterFileDescriptor() != lldb_utility::PseudoTerminal::invalid_fd) |
Greg Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 836 | SetSTDIOFileDescriptor (pty.ReleaseMasterFileDescriptor()); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 837 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 838 | } |
| 839 | } |
Greg Clayton | c235ac7 | 2011-08-09 05:20:29 +0000 | [diff] [blame] | 840 | else |
| 841 | { |
Johnny Chen | 4c1e920 | 2011-08-09 18:56:45 +0000 | [diff] [blame] | 842 | if (log) |
| 843 | log->Printf("failed to connect to debugserver: %s", error.AsCString()); |
Greg Clayton | c235ac7 | 2011-08-09 05:20:29 +0000 | [diff] [blame] | 844 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 845 | } |
| 846 | else |
| 847 | { |
| 848 | // Set our user ID to an invalid process ID. |
| 849 | SetID(LLDB_INVALID_PROCESS_ID); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 850 | error.SetErrorStringWithFormat ("failed to get object file from '%s' for arch %s", |
| 851 | exe_module->GetFileSpec().GetFilename().AsCString(), |
| 852 | exe_module->GetArchitecture().GetArchitectureName()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 853 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 854 | return error; |
Greg Clayton | 4957bf6 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 855 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | |
| 859 | Error |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 860 | ProcessGDBRemote::ConnectToDebugserver (const char *connect_url) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 861 | { |
| 862 | Error error; |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 863 | // Only connect if we have a valid connect URL |
| 864 | |
| 865 | if (connect_url && connect_url[0]) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 866 | { |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 867 | std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); |
| 868 | if (conn_ap.get()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 869 | { |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 870 | const uint32_t max_retry_count = 50; |
| 871 | uint32_t retry_count = 0; |
| 872 | while (!m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 873 | { |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 874 | if (conn_ap->Connect(connect_url, &error) == eConnectionStatusSuccess) |
| 875 | { |
| 876 | m_gdb_comm.SetConnection (conn_ap.release()); |
| 877 | break; |
| 878 | } |
| 879 | else if (error.WasInterrupted()) |
| 880 | { |
| 881 | // If we were interrupted, don't keep retrying. |
| 882 | break; |
| 883 | } |
| 884 | |
| 885 | retry_count++; |
| 886 | |
| 887 | if (retry_count >= max_retry_count) |
| 888 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 889 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 890 | usleep (100000); |
| 891 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | |
| 895 | if (!m_gdb_comm.IsConnected()) |
| 896 | { |
| 897 | if (error.Success()) |
| 898 | error.SetErrorString("not connected to remote gdb server"); |
| 899 | return error; |
| 900 | } |
| 901 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 902 | // We always seem to be able to open a connection to a local port |
| 903 | // so we need to make sure we can then send data to it. If we can't |
| 904 | // then we aren't actually connected to anything, so try and do the |
| 905 | // handshake with the remote GDB server and make sure that goes |
| 906 | // alright. |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 907 | if (!m_gdb_comm.HandshakeWithServer (&error)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 908 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 909 | m_gdb_comm.Disconnect(); |
| 910 | if (error.Success()) |
| 911 | error.SetErrorString("not connected to remote gdb server"); |
| 912 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 913 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 914 | m_gdb_comm.GetThreadSuffixSupported (); |
Greg Clayton | 4463399 | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 915 | m_gdb_comm.GetListThreadsInStopReplySupported (); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 916 | m_gdb_comm.GetHostInfo (); |
| 917 | m_gdb_comm.GetVContSupported ('c'); |
Jim Ingham | cd16df9 | 2012-07-20 21:37:13 +0000 | [diff] [blame] | 918 | m_gdb_comm.GetVAttachOrWaitSupported(); |
Jim Ingham | 03afad8 | 2012-07-02 05:40:07 +0000 | [diff] [blame] | 919 | |
| 920 | size_t num_cmds = GetExtraStartupCommands().GetArgumentCount(); |
| 921 | for (size_t idx = 0; idx < num_cmds; idx++) |
| 922 | { |
| 923 | StringExtractorGDBRemote response; |
Jim Ingham | 03afad8 | 2012-07-02 05:40:07 +0000 | [diff] [blame] | 924 | m_gdb_comm.SendPacketAndWaitForResponse (GetExtraStartupCommands().GetArgumentAtIndex(idx), response, false); |
| 925 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 926 | return error; |
| 927 | } |
| 928 | |
| 929 | void |
| 930 | ProcessGDBRemote::DidLaunchOrAttach () |
| 931 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 932 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | 6d09345 | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 933 | if (log) |
| 934 | log->Printf ("ProcessGDBRemote::DidLaunch()"); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 935 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 936 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 937 | BuildDynamicRegisterInfo (false); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 938 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 939 | // See if the GDB server supports the qHostInfo information |
Greg Clayton | 0d0c12a | 2011-02-09 03:09:55 +0000 | [diff] [blame] | 940 | |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 941 | ArchSpec gdb_remote_arch = m_gdb_comm.GetHostArchitecture(); |
| 942 | |
| 943 | // See if the GDB server supports the qProcessInfo packet, if so |
| 944 | // prefer that over the Host information as it will be more specific |
| 945 | // to our process. |
| 946 | |
| 947 | if (m_gdb_comm.GetProcessArchitecture().IsValid()) |
| 948 | gdb_remote_arch = m_gdb_comm.GetProcessArchitecture(); |
| 949 | |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 950 | if (gdb_remote_arch.IsValid()) |
Greg Clayton | 0d0c12a | 2011-02-09 03:09:55 +0000 | [diff] [blame] | 951 | { |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 952 | ArchSpec &target_arch = GetTarget().GetArchitecture(); |
| 953 | |
| 954 | if (target_arch.IsValid()) |
| 955 | { |
| 956 | // If the remote host is ARM and we have apple as the vendor, then |
| 957 | // ARM executables and shared libraries can have mixed ARM architectures. |
| 958 | // You can have an armv6 executable, and if the host is armv7, then the |
| 959 | // system will load the best possible architecture for all shared libraries |
| 960 | // it has, so we really need to take the remote host architecture as our |
| 961 | // defacto architecture in this case. |
| 962 | |
| 963 | if (gdb_remote_arch.GetMachine() == llvm::Triple::arm && |
| 964 | gdb_remote_arch.GetTriple().getVendor() == llvm::Triple::Apple) |
| 965 | { |
| 966 | target_arch = gdb_remote_arch; |
| 967 | } |
| 968 | else |
| 969 | { |
| 970 | // Fill in what is missing in the triple |
| 971 | const llvm::Triple &remote_triple = gdb_remote_arch.GetTriple(); |
| 972 | llvm::Triple &target_triple = target_arch.GetTriple(); |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 973 | if (target_triple.getVendorName().size() == 0) |
| 974 | { |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 975 | target_triple.setVendor (remote_triple.getVendor()); |
| 976 | |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 977 | if (target_triple.getOSName().size() == 0) |
| 978 | { |
| 979 | target_triple.setOS (remote_triple.getOS()); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 980 | |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 981 | if (target_triple.getEnvironmentName().size() == 0) |
| 982 | target_triple.setEnvironment (remote_triple.getEnvironment()); |
| 983 | } |
| 984 | } |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | else |
| 988 | { |
| 989 | // The target doesn't have a valid architecture yet, set it from |
| 990 | // the architecture we got from the remote GDB server |
| 991 | target_arch = gdb_remote_arch; |
| 992 | } |
Greg Clayton | 0d0c12a | 2011-02-09 03:09:55 +0000 | [diff] [blame] | 993 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | |
| 997 | void |
| 998 | ProcessGDBRemote::DidLaunch () |
| 999 | { |
| 1000 | DidLaunchOrAttach (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | Error |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1004 | ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1005 | { |
Han Ming Ong | 8464704 | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 1006 | ProcessAttachInfo attach_info; |
| 1007 | return DoAttachToProcessWithID(attach_pid, attach_info); |
| 1008 | } |
| 1009 | |
| 1010 | Error |
| 1011 | ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info) |
| 1012 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1013 | Error error; |
| 1014 | // Clear out and clean up from any current state |
| 1015 | Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1016 | if (attach_pid != LLDB_INVALID_PROCESS_ID) |
| 1017 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1018 | // Make sure we aren't already connected? |
| 1019 | if (!m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1020 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 1021 | error = LaunchAndConnectToDebugserver (attach_info); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1022 | |
| 1023 | if (error.Fail()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1024 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1025 | const char *error_string = error.AsCString(); |
| 1026 | if (error_string == NULL) |
| 1027 | error_string = "unable to launch " DEBUGSERVER_BASENAME; |
| 1028 | |
| 1029 | SetExitStatus (-1, error_string); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1030 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | if (error.Success()) |
| 1034 | { |
| 1035 | char packet[64]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1036 | const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, attach_pid); |
Greg Clayton | 3b60842 | 2011-11-19 02:11:30 +0000 | [diff] [blame] | 1037 | SetID (attach_pid); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1038 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet, packet_len)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1039 | } |
| 1040 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1041 | return error; |
| 1042 | } |
| 1043 | |
| 1044 | size_t |
| 1045 | ProcessGDBRemote::AttachInputReaderCallback |
| 1046 | ( |
| 1047 | void *baton, |
| 1048 | InputReader *reader, |
| 1049 | lldb::InputReaderAction notification, |
| 1050 | const char *bytes, |
| 1051 | size_t bytes_len |
| 1052 | ) |
| 1053 | { |
| 1054 | if (notification == eInputReaderGotToken) |
| 1055 | { |
| 1056 | ProcessGDBRemote *gdb_process = (ProcessGDBRemote *)baton; |
| 1057 | if (gdb_process->m_waiting_for_attach) |
| 1058 | gdb_process->m_waiting_for_attach = false; |
| 1059 | reader->SetIsDone(true); |
| 1060 | return 1; |
| 1061 | } |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | Error |
Han Ming Ong | 8464704 | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 1066 | ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1067 | { |
| 1068 | Error error; |
| 1069 | // Clear out and clean up from any current state |
| 1070 | Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1071 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1072 | if (process_name && process_name[0]) |
| 1073 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1074 | // Make sure we aren't already connected? |
| 1075 | if (!m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1076 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 1077 | error = LaunchAndConnectToDebugserver (attach_info); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1078 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1079 | if (error.Fail()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1080 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1081 | const char *error_string = error.AsCString(); |
| 1082 | if (error_string == NULL) |
| 1083 | error_string = "unable to launch " DEBUGSERVER_BASENAME; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1084 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1085 | SetExitStatus (-1, error_string); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1086 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | if (error.Success()) |
| 1090 | { |
| 1091 | StreamString packet; |
| 1092 | |
| 1093 | if (wait_for_launch) |
Jim Ingham | cd16df9 | 2012-07-20 21:37:13 +0000 | [diff] [blame] | 1094 | { |
| 1095 | if (!m_gdb_comm.GetVAttachOrWaitSupported()) |
| 1096 | { |
| 1097 | packet.PutCString ("vAttachWait"); |
| 1098 | } |
| 1099 | else |
| 1100 | { |
| 1101 | if (attach_info.GetIgnoreExisting()) |
| 1102 | packet.PutCString("vAttachWait"); |
| 1103 | else |
| 1104 | packet.PutCString ("vAttachOrWait"); |
| 1105 | } |
| 1106 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1107 | else |
| 1108 | packet.PutCString("vAttachName"); |
| 1109 | packet.PutChar(';'); |
| 1110 | packet.PutBytesAsRawHex8(process_name, strlen(process_name), lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder()); |
| 1111 | |
| 1112 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet.GetData(), packet.GetSize())); |
| 1113 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1114 | } |
| 1115 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1116 | return error; |
| 1117 | } |
| 1118 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1119 | |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1120 | bool |
| 1121 | ProcessGDBRemote::SetExitStatus (int exit_status, const char *cstr) |
| 1122 | { |
| 1123 | m_gdb_comm.Disconnect(); |
| 1124 | return Process::SetExitStatus (exit_status, cstr); |
| 1125 | } |
| 1126 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1127 | void |
| 1128 | ProcessGDBRemote::DidAttach () |
| 1129 | { |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 1130 | DidLaunchOrAttach (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 1133 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1134 | Error |
| 1135 | ProcessGDBRemote::WillResume () |
| 1136 | { |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1137 | m_continue_c_tids.clear(); |
| 1138 | m_continue_C_tids.clear(); |
| 1139 | m_continue_s_tids.clear(); |
| 1140 | m_continue_S_tids.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1141 | return Error(); |
| 1142 | } |
| 1143 | |
| 1144 | Error |
| 1145 | ProcessGDBRemote::DoResume () |
| 1146 | { |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1147 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1148 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | 6d09345 | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 1149 | if (log) |
| 1150 | log->Printf ("ProcessGDBRemote::Resume()"); |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 1151 | |
| 1152 | Listener listener ("gdb-remote.resume-packet-sent"); |
| 1153 | if (listener.StartListeningForEvents (&m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent)) |
| 1154 | { |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1155 | listener.StartListeningForEvents (&m_async_broadcaster, ProcessGDBRemote::eBroadcastBitAsyncThreadDidExit); |
| 1156 | |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1157 | const size_t num_threads = GetThreadList().GetSize(); |
| 1158 | |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1159 | StreamString continue_packet; |
| 1160 | bool continue_packet_error = false; |
| 1161 | if (m_gdb_comm.HasAnyVContSupport ()) |
| 1162 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1163 | if (m_continue_c_tids.size() == num_threads) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1164 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1165 | // All threads are continuing, just send a "c" packet |
| 1166 | continue_packet.PutCString ("c"); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1167 | } |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1168 | else |
| 1169 | { |
| 1170 | continue_packet.PutCString ("vCont"); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1171 | |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1172 | if (!m_continue_c_tids.empty()) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1173 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1174 | if (m_gdb_comm.GetVContSupported ('c')) |
| 1175 | { |
| 1176 | 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) |
| 1177 | continue_packet.Printf(";c:%4.4" PRIx64, *t_pos); |
| 1178 | } |
| 1179 | else |
| 1180 | continue_packet_error = true; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1181 | } |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1182 | |
| 1183 | if (!continue_packet_error && !m_continue_C_tids.empty()) |
| 1184 | { |
| 1185 | if (m_gdb_comm.GetVContSupported ('C')) |
| 1186 | { |
| 1187 | 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) |
| 1188 | continue_packet.Printf(";C%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first); |
| 1189 | } |
| 1190 | else |
| 1191 | continue_packet_error = true; |
| 1192 | } |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 1193 | |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1194 | if (!continue_packet_error && !m_continue_s_tids.empty()) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1195 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1196 | if (m_gdb_comm.GetVContSupported ('s')) |
| 1197 | { |
| 1198 | 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) |
| 1199 | continue_packet.Printf(";s:%4.4" PRIx64, *t_pos); |
| 1200 | } |
| 1201 | else |
| 1202 | continue_packet_error = true; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1203 | } |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1204 | |
| 1205 | if (!continue_packet_error && !m_continue_S_tids.empty()) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1206 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1207 | if (m_gdb_comm.GetVContSupported ('S')) |
| 1208 | { |
| 1209 | 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) |
| 1210 | continue_packet.Printf(";S%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first); |
| 1211 | } |
| 1212 | else |
| 1213 | continue_packet_error = true; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1214 | } |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1215 | |
| 1216 | if (continue_packet_error) |
| 1217 | continue_packet.GetString().clear(); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1218 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1219 | } |
| 1220 | else |
| 1221 | continue_packet_error = true; |
| 1222 | |
| 1223 | if (continue_packet_error) |
| 1224 | { |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1225 | // Either no vCont support, or we tried to use part of the vCont |
| 1226 | // packet that wasn't supported by the remote GDB server. |
| 1227 | // We need to try and make a simple packet that can do our continue |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1228 | const size_t num_continue_c_tids = m_continue_c_tids.size(); |
| 1229 | const size_t num_continue_C_tids = m_continue_C_tids.size(); |
| 1230 | const size_t num_continue_s_tids = m_continue_s_tids.size(); |
| 1231 | const size_t num_continue_S_tids = m_continue_S_tids.size(); |
| 1232 | if (num_continue_c_tids > 0) |
| 1233 | { |
| 1234 | if (num_continue_c_tids == num_threads) |
| 1235 | { |
| 1236 | // All threads are resuming... |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1237 | m_gdb_comm.SetCurrentThreadForRun (-1); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1238 | continue_packet.PutChar ('c'); |
| 1239 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1240 | } |
| 1241 | else if (num_continue_c_tids == 1 && |
| 1242 | num_continue_C_tids == 0 && |
| 1243 | num_continue_s_tids == 0 && |
| 1244 | num_continue_S_tids == 0 ) |
| 1245 | { |
| 1246 | // Only one thread is continuing |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1247 | m_gdb_comm.SetCurrentThreadForRun (m_continue_c_tids.front()); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1248 | continue_packet.PutChar ('c'); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1249 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1253 | if (continue_packet_error && num_continue_C_tids > 0) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1254 | { |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1255 | if ((num_continue_C_tids + num_continue_c_tids) == num_threads && |
| 1256 | num_continue_C_tids > 0 && |
| 1257 | num_continue_s_tids == 0 && |
| 1258 | num_continue_S_tids == 0 ) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1259 | { |
| 1260 | const int continue_signo = m_continue_C_tids.front().second; |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1261 | // Only one thread is continuing |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1262 | if (num_continue_C_tids > 1) |
| 1263 | { |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1264 | // More that one thread with a signal, yet we don't have |
| 1265 | // vCont support and we are being asked to resume each |
| 1266 | // thread with a signal, we need to make sure they are |
| 1267 | // all the same signal, or we can't issue the continue |
| 1268 | // accurately with the current support... |
| 1269 | if (num_continue_C_tids > 1) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1270 | { |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1271 | continue_packet_error = false; |
| 1272 | for (size_t i=1; i<m_continue_C_tids.size(); ++i) |
| 1273 | { |
| 1274 | if (m_continue_C_tids[i].second != continue_signo) |
| 1275 | continue_packet_error = true; |
| 1276 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1277 | } |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1278 | if (!continue_packet_error) |
| 1279 | m_gdb_comm.SetCurrentThreadForRun (-1); |
| 1280 | } |
| 1281 | else |
| 1282 | { |
| 1283 | // Set the continue thread ID |
| 1284 | continue_packet_error = false; |
| 1285 | m_gdb_comm.SetCurrentThreadForRun (m_continue_C_tids.front().first); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1286 | } |
| 1287 | if (!continue_packet_error) |
| 1288 | { |
| 1289 | // Add threads continuing with the same signo... |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1290 | continue_packet.Printf("C%2.2x", continue_signo); |
| 1291 | } |
| 1292 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1295 | if (continue_packet_error && num_continue_s_tids > 0) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1296 | { |
| 1297 | if (num_continue_s_tids == num_threads) |
| 1298 | { |
| 1299 | // All threads are resuming... |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1300 | m_gdb_comm.SetCurrentThreadForRun (-1); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1301 | continue_packet.PutChar ('s'); |
| 1302 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1303 | } |
| 1304 | else if (num_continue_c_tids == 0 && |
| 1305 | num_continue_C_tids == 0 && |
| 1306 | num_continue_s_tids == 1 && |
| 1307 | num_continue_S_tids == 0 ) |
| 1308 | { |
| 1309 | // Only one thread is stepping |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1310 | m_gdb_comm.SetCurrentThreadForRun (m_continue_s_tids.front()); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1311 | continue_packet.PutChar ('s'); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1312 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | if (!continue_packet_error && num_continue_S_tids > 0) |
| 1317 | { |
| 1318 | if (num_continue_S_tids == num_threads) |
| 1319 | { |
| 1320 | const int step_signo = m_continue_S_tids.front().second; |
| 1321 | // Are all threads trying to step with the same signal? |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1322 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1323 | if (num_continue_S_tids > 1) |
| 1324 | { |
| 1325 | for (size_t i=1; i<num_threads; ++i) |
| 1326 | { |
| 1327 | if (m_continue_S_tids[i].second != step_signo) |
| 1328 | continue_packet_error = true; |
| 1329 | } |
| 1330 | } |
| 1331 | if (!continue_packet_error) |
| 1332 | { |
| 1333 | // Add threads stepping with the same signo... |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1334 | m_gdb_comm.SetCurrentThreadForRun (-1); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1335 | continue_packet.Printf("S%2.2x", step_signo); |
| 1336 | } |
| 1337 | } |
| 1338 | else if (num_continue_c_tids == 0 && |
| 1339 | num_continue_C_tids == 0 && |
| 1340 | num_continue_s_tids == 0 && |
| 1341 | num_continue_S_tids == 1 ) |
| 1342 | { |
| 1343 | // Only one thread is stepping with signal |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1344 | m_gdb_comm.SetCurrentThreadForRun (m_continue_S_tids.front().first); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1345 | continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1346 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | if (continue_packet_error) |
| 1352 | { |
| 1353 | error.SetErrorString ("can't make continue packet for this resume"); |
| 1354 | } |
| 1355 | else |
| 1356 | { |
| 1357 | EventSP event_sp; |
| 1358 | TimeValue timeout; |
| 1359 | timeout = TimeValue::Now(); |
| 1360 | timeout.OffsetWithSeconds (5); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1361 | if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread)) |
| 1362 | { |
| 1363 | error.SetErrorString ("Trying to resume but the async thread is dead."); |
| 1364 | if (log) |
| 1365 | log->Printf ("ProcessGDBRemote::DoResume: Trying to resume but the async thread is dead."); |
| 1366 | return error; |
| 1367 | } |
| 1368 | |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1369 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (continue_packet.GetData(), continue_packet.GetSize())); |
| 1370 | |
| 1371 | if (listener.WaitForEvent (&timeout, event_sp) == false) |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1372 | { |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1373 | error.SetErrorString("Resume timed out."); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1374 | if (log) |
| 1375 | log->Printf ("ProcessGDBRemote::DoResume: Resume timed out."); |
| 1376 | } |
| 1377 | else if (event_sp->BroadcasterIs (&m_async_broadcaster)) |
| 1378 | { |
| 1379 | error.SetErrorString ("Broadcast continue, but the async thread was killed before we got an ack back."); |
| 1380 | if (log) |
| 1381 | log->Printf ("ProcessGDBRemote::DoResume: Broadcast continue, but the async thread was killed before we got an ack back."); |
| 1382 | return error; |
| 1383 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1384 | } |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1387 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1390 | void |
| 1391 | ProcessGDBRemote::ClearThreadIDList () |
| 1392 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1393 | Mutex::Locker locker(m_thread_list_real.GetMutex()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1394 | m_thread_ids.clear(); |
| 1395 | } |
| 1396 | |
| 1397 | bool |
| 1398 | ProcessGDBRemote::UpdateThreadIDList () |
| 1399 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1400 | Mutex::Locker locker(m_thread_list_real.GetMutex()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1401 | bool sequence_mutex_unavailable = false; |
| 1402 | m_gdb_comm.GetCurrentThreadIDs (m_thread_ids, sequence_mutex_unavailable); |
| 1403 | if (sequence_mutex_unavailable) |
| 1404 | { |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1405 | return false; // We just didn't get the list |
| 1406 | } |
| 1407 | return true; |
| 1408 | } |
| 1409 | |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1410 | bool |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1411 | ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1412 | { |
| 1413 | // locker will keep a mutex locked until it goes out of scope |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1414 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD)); |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 1415 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1416 | log->Printf ("ProcessGDBRemote::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1417 | |
| 1418 | size_t num_thread_ids = m_thread_ids.size(); |
| 1419 | // The "m_thread_ids" thread ID list should always be updated after each stop |
| 1420 | // reply packet, but in case it isn't, update it here. |
| 1421 | if (num_thread_ids == 0) |
| 1422 | { |
| 1423 | if (!UpdateThreadIDList ()) |
| 1424 | return false; |
| 1425 | num_thread_ids = m_thread_ids.size(); |
| 1426 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1427 | |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1428 | ThreadList old_thread_list_copy(old_thread_list); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1429 | if (num_thread_ids > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1430 | { |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1431 | for (size_t i=0; i<num_thread_ids; ++i) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1432 | { |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1433 | tid_t tid = m_thread_ids[i]; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1434 | ThreadSP thread_sp (old_thread_list_copy.RemoveThreadByProtocolID(tid, false)); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1435 | if (!thread_sp) |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1436 | { |
Jim Ingham | 4f465cf | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 1437 | thread_sp.reset (new ThreadGDBRemote (*this, tid)); |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1438 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
| 1439 | log->Printf( |
| 1440 | "ProcessGDBRemote::%s Making new thread: %p for thread ID: 0x%" PRIx64 ".\n", |
| 1441 | __FUNCTION__, |
| 1442 | thread_sp.get(), |
| 1443 | thread_sp->GetID()); |
| 1444 | } |
| 1445 | else |
| 1446 | { |
| 1447 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
| 1448 | log->Printf( |
| 1449 | "ProcessGDBRemote::%s Found old thread: %p for thread ID: 0x%" PRIx64 ".\n", |
| 1450 | __FUNCTION__, |
| 1451 | thread_sp.get(), |
| 1452 | thread_sp->GetID()); |
| 1453 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1454 | new_thread_list.AddThread(thread_sp); |
Greg Clayton | adc00cb | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 1455 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1456 | } |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1457 | |
| 1458 | // Whatever that is left in old_thread_list_copy are not |
| 1459 | // present in new_thread_list. Remove non-existent threads from internal id table. |
| 1460 | size_t old_num_thread_ids = old_thread_list_copy.GetSize(false); |
| 1461 | for (size_t i=0; i<old_num_thread_ids; i++) |
| 1462 | { |
| 1463 | ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex (i, false)); |
| 1464 | if (old_thread_sp) |
| 1465 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1466 | lldb::tid_t old_thread_id = old_thread_sp->GetProtocolID(); |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1467 | m_thread_id_to_index_id_map.erase(old_thread_id); |
| 1468 | } |
| 1469 | } |
| 1470 | |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1471 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | |
| 1475 | StateType |
| 1476 | ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet) |
| 1477 | { |
Greg Clayton | dd0e5a5 | 2011-06-02 22:22:38 +0000 | [diff] [blame] | 1478 | stop_packet.SetFilePos (0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1479 | const char stop_type = stop_packet.GetChar(); |
| 1480 | switch (stop_type) |
| 1481 | { |
| 1482 | case 'T': |
| 1483 | case 'S': |
| 1484 | { |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 1485 | // This is a bit of a hack, but is is required. If we did exec, we |
| 1486 | // need to clear our thread lists and also know to rebuild our dynamic |
| 1487 | // register info before we lookup and threads and populate the expedited |
| 1488 | // register values so we need to know this right away so we can cleanup |
| 1489 | // and update our registers. |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 1490 | const uint32_t stop_id = GetStopID(); |
| 1491 | if (stop_id == 0) |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1492 | { |
| 1493 | // Our first stop, make sure we have a process ID, and also make |
| 1494 | // sure we know about our registers |
| 1495 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 1496 | { |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 1497 | lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID (); |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1498 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 1499 | SetID (pid); |
| 1500 | } |
| 1501 | BuildDynamicRegisterInfo (true); |
| 1502 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1503 | // Stop with signal and thread info |
| 1504 | const uint8_t signo = stop_packet.GetHexU8(); |
| 1505 | std::string name; |
| 1506 | std::string value; |
| 1507 | std::string thread_name; |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1508 | std::string reason; |
| 1509 | std::string description; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1510 | uint32_t exc_type = 0; |
Greg Clayton | 896dff6 | 2010-07-23 16:45:51 +0000 | [diff] [blame] | 1511 | std::vector<addr_t> exc_data; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1512 | addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS; |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1513 | ThreadSP thread_sp; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1514 | ThreadGDBRemote *gdb_thread = NULL; |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1515 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1516 | while (stop_packet.GetNameColonValue(name, value)) |
| 1517 | { |
| 1518 | if (name.compare("metype") == 0) |
| 1519 | { |
| 1520 | // exception type in big endian hex |
| 1521 | exc_type = Args::StringToUInt32 (value.c_str(), 0, 16); |
| 1522 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1523 | else if (name.compare("medata") == 0) |
| 1524 | { |
| 1525 | // exception data in big endian hex |
| 1526 | exc_data.push_back(Args::StringToUInt64 (value.c_str(), 0, 16)); |
| 1527 | } |
| 1528 | else if (name.compare("thread") == 0) |
| 1529 | { |
| 1530 | // thread in big endian hex |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1531 | lldb::tid_t tid = Args::StringToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1532 | // m_thread_list_real does have its own mutex, but we need to |
| 1533 | // hold onto the mutex between the call to m_thread_list_real.FindThreadByID(...) |
| 1534 | // and the m_thread_list_real.AddThread(...) so it doesn't change on us |
| 1535 | Mutex::Locker locker (m_thread_list_real.GetMutex ()); |
| 1536 | thread_sp = m_thread_list_real.FindThreadByProtocolID(tid, false); |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1537 | |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1538 | if (!thread_sp) |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1539 | { |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1540 | // Create the thread if we need to |
Jim Ingham | 4f465cf | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 1541 | thread_sp.reset (new ThreadGDBRemote (*this, tid)); |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1542 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD)); |
| 1543 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
| 1544 | log->Printf ("ProcessGDBRemote::%s Adding new thread: %p for thread ID: 0x%" PRIx64 ".\n", |
| 1545 | __FUNCTION__, |
| 1546 | thread_sp.get(), |
| 1547 | thread_sp->GetID()); |
| 1548 | |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1549 | m_thread_list_real.AddThread(thread_sp); |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1550 | } |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1551 | gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get()); |
| 1552 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1553 | } |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1554 | else if (name.compare("threads") == 0) |
| 1555 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1556 | Mutex::Locker locker(m_thread_list_real.GetMutex()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1557 | m_thread_ids.clear(); |
Greg Clayton | 4463399 | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 1558 | // A comma separated list of all threads in the current |
| 1559 | // process that includes the thread for this stop reply |
| 1560 | // packet |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1561 | size_t comma_pos; |
| 1562 | lldb::tid_t tid; |
| 1563 | while ((comma_pos = value.find(',')) != std::string::npos) |
| 1564 | { |
| 1565 | value[comma_pos] = '\0'; |
| 1566 | // thread in big endian hex |
| 1567 | tid = Args::StringToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
| 1568 | if (tid != LLDB_INVALID_THREAD_ID) |
| 1569 | m_thread_ids.push_back (tid); |
| 1570 | value.erase(0, comma_pos + 1); |
| 1571 | |
| 1572 | } |
| 1573 | tid = Args::StringToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
| 1574 | if (tid != LLDB_INVALID_THREAD_ID) |
| 1575 | m_thread_ids.push_back (tid); |
| 1576 | } |
Greg Clayton | de9d049 | 2011-01-08 03:17:57 +0000 | [diff] [blame] | 1577 | else if (name.compare("hexname") == 0) |
| 1578 | { |
| 1579 | StringExtractor name_extractor; |
| 1580 | // Swap "value" over into "name_extractor" |
| 1581 | name_extractor.GetStringRef().swap(value); |
| 1582 | // Now convert the HEX bytes into a string value |
| 1583 | name_extractor.GetHexByteString (value); |
| 1584 | thread_name.swap (value); |
| 1585 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1586 | else if (name.compare("name") == 0) |
| 1587 | { |
| 1588 | thread_name.swap (value); |
| 1589 | } |
Greg Clayton | 6f35f5c | 2010-09-09 06:32:46 +0000 | [diff] [blame] | 1590 | else if (name.compare("qaddr") == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1591 | { |
| 1592 | thread_dispatch_qaddr = Args::StringToUInt64 (value.c_str(), 0, 16); |
| 1593 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1594 | else if (name.compare("reason") == 0) |
| 1595 | { |
| 1596 | reason.swap(value); |
| 1597 | } |
| 1598 | else if (name.compare("description") == 0) |
| 1599 | { |
| 1600 | StringExtractor desc_extractor; |
| 1601 | // Swap "value" over into "name_extractor" |
| 1602 | desc_extractor.GetStringRef().swap(value); |
| 1603 | // Now convert the HEX bytes into a string value |
| 1604 | desc_extractor.GetHexByteString (thread_name); |
| 1605 | } |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1606 | else if (name.size() == 2 && ::isxdigit(name[0]) && ::isxdigit(name[1])) |
| 1607 | { |
| 1608 | // We have a register number that contains an expedited |
| 1609 | // register value. Lets supply this register to our thread |
| 1610 | // so it won't have to go and read it. |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1611 | if (gdb_thread) |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1612 | { |
| 1613 | uint32_t reg = Args::StringToUInt32 (name.c_str(), UINT32_MAX, 16); |
| 1614 | |
| 1615 | if (reg != UINT32_MAX) |
| 1616 | { |
| 1617 | StringExtractor reg_value_extractor; |
| 1618 | // Swap "value" over into "reg_value_extractor" |
| 1619 | reg_value_extractor.GetStringRef().swap(value); |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1620 | if (!gdb_thread->PrivateSetRegisterValue (reg, reg_value_extractor)) |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1621 | { |
| 1622 | Host::SetCrashDescriptionWithFormat("Setting thread register '%s' (decoded to %u (0x%x)) with value '%s' for stop packet: '%s'", |
| 1623 | name.c_str(), |
| 1624 | reg, |
| 1625 | reg, |
| 1626 | reg_value_extractor.GetStringRef().c_str(), |
| 1627 | stop_packet.GetStringRef().c_str()); |
| 1628 | } |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1629 | } |
| 1630 | } |
| 1631 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1632 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1633 | |
| 1634 | if (thread_sp) |
| 1635 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1636 | // Clear the stop info just in case we don't set it to anything |
| 1637 | thread_sp->SetStopInfo (StopInfoSP()); |
| 1638 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1639 | gdb_thread->SetThreadDispatchQAddr (thread_dispatch_qaddr); |
Jim Ingham | dd2fe7a | 2011-01-28 02:23:12 +0000 | [diff] [blame] | 1640 | gdb_thread->SetName (thread_name.empty() ? NULL : thread_name.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1641 | if (exc_type != 0) |
| 1642 | { |
Greg Clayton | 1a65ae1 | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 1643 | const size_t exc_data_size = exc_data.size(); |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1644 | |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1645 | thread_sp->SetStopInfo (StopInfoMachException::CreateStopReasonWithMachException (*thread_sp, |
| 1646 | exc_type, |
| 1647 | exc_data_size, |
| 1648 | exc_data_size >= 1 ? exc_data[0] : 0, |
| 1649 | exc_data_size >= 2 ? exc_data[1] : 0, |
| 1650 | exc_data_size >= 3 ? exc_data[2] : 0)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1651 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1652 | else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1653 | { |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1654 | bool handled = false; |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 1655 | bool did_exec = false; |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1656 | if (!reason.empty()) |
| 1657 | { |
| 1658 | if (reason.compare("trace") == 0) |
| 1659 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1660 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp)); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1661 | handled = true; |
| 1662 | } |
| 1663 | else if (reason.compare("breakpoint") == 0) |
| 1664 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1665 | addr_t pc = thread_sp->GetRegisterContext()->GetPC(); |
| 1666 | lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1667 | if (bp_site_sp) |
| 1668 | { |
| 1669 | // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread, |
| 1670 | // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that |
| 1671 | // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc. |
Jim Ingham | 54cc6e4 | 2012-07-11 21:41:19 +0000 | [diff] [blame] | 1672 | handled = true; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1673 | if (bp_site_sp->ValidForThisThread (thread_sp.get())) |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1674 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1675 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID())); |
Jim Ingham | 54cc6e4 | 2012-07-11 21:41:19 +0000 | [diff] [blame] | 1676 | } |
| 1677 | else |
| 1678 | { |
| 1679 | StopInfoSP invalid_stop_info_sp; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1680 | thread_sp->SetStopInfo (invalid_stop_info_sp); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1684 | } |
| 1685 | else if (reason.compare("trap") == 0) |
| 1686 | { |
| 1687 | // Let the trap just use the standard signal stop reason below... |
| 1688 | } |
| 1689 | else if (reason.compare("watchpoint") == 0) |
| 1690 | { |
| 1691 | break_id_t watch_id = LLDB_INVALID_WATCH_ID; |
| 1692 | // TODO: locate the watchpoint somehow... |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1693 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID (*thread_sp, watch_id)); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1694 | handled = true; |
| 1695 | } |
| 1696 | else if (reason.compare("exception") == 0) |
| 1697 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1698 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException(*thread_sp, description.c_str())); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1699 | handled = true; |
| 1700 | } |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 1701 | else if (reason.compare("exec") == 0) |
| 1702 | { |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 1703 | did_exec = true; |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 1704 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithExec(*thread_sp)); |
| 1705 | handled = true; |
| 1706 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 1709 | if (signo && did_exec == false) |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1710 | { |
| 1711 | if (signo == SIGTRAP) |
| 1712 | { |
| 1713 | // Currently we are going to assume SIGTRAP means we are either |
| 1714 | // hitting a breakpoint or hardware single stepping. |
Jim Ingham | 54cc6e4 | 2012-07-11 21:41:19 +0000 | [diff] [blame] | 1715 | handled = true; |
Hafiz Abid Qadeer | 85a4daf | 2013-10-18 10:04:33 +0000 | [diff] [blame] | 1716 | addr_t pc = thread_sp->GetRegisterContext()->GetPC() + m_breakpoint_pc_offset; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1717 | lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc); |
Jim Ingham | 54cc6e4 | 2012-07-11 21:41:19 +0000 | [diff] [blame] | 1718 | |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1719 | if (bp_site_sp) |
| 1720 | { |
| 1721 | // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread, |
| 1722 | // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that |
| 1723 | // will be taken care of when the thread resumes and notices that there's a breakpoint under the pc. |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1724 | if (bp_site_sp->ValidForThisThread (thread_sp.get())) |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1725 | { |
Hafiz Abid Qadeer | 85a4daf | 2013-10-18 10:04:33 +0000 | [diff] [blame] | 1726 | if(m_breakpoint_pc_offset != 0) |
| 1727 | thread_sp->GetRegisterContext()->SetPC(pc); |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1728 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID())); |
Jim Ingham | 54cc6e4 | 2012-07-11 21:41:19 +0000 | [diff] [blame] | 1729 | } |
| 1730 | else |
| 1731 | { |
| 1732 | StopInfoSP invalid_stop_info_sp; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1733 | thread_sp->SetStopInfo (invalid_stop_info_sp); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1734 | } |
| 1735 | } |
Jim Ingham | 54cc6e4 | 2012-07-11 21:41:19 +0000 | [diff] [blame] | 1736 | else |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1737 | { |
Jim Ingham | 4dc613b | 2012-10-27 02:52:04 +0000 | [diff] [blame] | 1738 | // If we were stepping then assume the stop was the result of the trace. If we were |
| 1739 | // not stepping then report the SIGTRAP. |
| 1740 | // FIXME: We are still missing the case where we single step over a trap instruction. |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1741 | if (thread_sp->GetTemporaryResumeState() == eStateStepping) |
| 1742 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp)); |
Jim Ingham | 4dc613b | 2012-10-27 02:52:04 +0000 | [diff] [blame] | 1743 | else |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1744 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal(*thread_sp, signo)); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1745 | } |
| 1746 | } |
| 1747 | if (!handled) |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1748 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal (*thread_sp, signo)); |
Jason Molenda | 8214b01 | 2013-04-25 01:33:46 +0000 | [diff] [blame] | 1749 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1750 | |
| 1751 | if (!description.empty()) |
| 1752 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1753 | lldb::StopInfoSP stop_info_sp (thread_sp->GetStopInfo ()); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1754 | if (stop_info_sp) |
| 1755 | { |
| 1756 | stop_info_sp->SetDescription (description.c_str()); |
Greg Clayton | 3418c85 | 2011-08-10 02:10:13 +0000 | [diff] [blame] | 1757 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1758 | else |
| 1759 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1760 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException (*thread_sp, description.c_str())); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1761 | } |
| 1762 | } |
| 1763 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1764 | } |
| 1765 | return eStateStopped; |
| 1766 | } |
| 1767 | break; |
| 1768 | |
| 1769 | case 'W': |
| 1770 | // process exited |
| 1771 | return eStateExited; |
| 1772 | |
| 1773 | default: |
| 1774 | break; |
| 1775 | } |
| 1776 | return eStateInvalid; |
| 1777 | } |
| 1778 | |
| 1779 | void |
| 1780 | ProcessGDBRemote::RefreshStateAfterStop () |
| 1781 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1782 | Mutex::Locker locker(m_thread_list_real.GetMutex()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1783 | m_thread_ids.clear(); |
| 1784 | // Set the thread stop info. It might have a "threads" key whose value is |
| 1785 | // a list of all thread IDs in the current process, so m_thread_ids might |
| 1786 | // get set. |
| 1787 | SetThreadStopInfo (m_last_stop_packet); |
| 1788 | // Check to see if SetThreadStopInfo() filled in m_thread_ids? |
| 1789 | if (m_thread_ids.empty()) |
| 1790 | { |
| 1791 | // No, we need to fetch the thread list manually |
| 1792 | UpdateThreadIDList(); |
| 1793 | } |
| 1794 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1795 | // Let all threads recover from stopping and do any clean up based |
| 1796 | // on the previous thread state (if any). |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1797 | m_thread_list_real.RefreshStateAfterStop(); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1798 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | Error |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1802 | ProcessGDBRemote::DoHalt (bool &caused_stop) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1803 | { |
| 1804 | Error error; |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1805 | |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 1806 | bool timed_out = false; |
| 1807 | Mutex::Locker locker; |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1808 | |
| 1809 | if (m_public_state.GetValue() == eStateAttaching) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1810 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1811 | // We are being asked to halt during an attach. We need to just close |
| 1812 | // our file handle and debugserver will go away, and we can be done... |
| 1813 | m_gdb_comm.Disconnect(); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1814 | } |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1815 | else |
| 1816 | { |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 1817 | if (!m_gdb_comm.SendInterrupt (locker, 2, timed_out)) |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1818 | { |
| 1819 | if (timed_out) |
| 1820 | error.SetErrorString("timed out sending interrupt packet"); |
| 1821 | else |
| 1822 | error.SetErrorString("unknown error sending interrupt packet"); |
| 1823 | } |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 1824 | |
| 1825 | caused_stop = m_gdb_comm.GetInterruptWasSent (); |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1826 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1827 | return error; |
| 1828 | } |
| 1829 | |
| 1830 | Error |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 1831 | ProcessGDBRemote::DoDetach(bool keep_stopped) |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1832 | { |
| 1833 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1834 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1835 | if (log) |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 1836 | log->Printf ("ProcessGDBRemote::DoDetach(keep_stopped: %i)", keep_stopped); |
| 1837 | |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1838 | DisableAllBreakpointSites (); |
| 1839 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1840 | m_thread_list.DiscardThreadPlans(); |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1841 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 1842 | error = m_gdb_comm.Detach (keep_stopped); |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1843 | if (log) |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1844 | { |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 1845 | if (error.Success()) |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1846 | log->PutCString ("ProcessGDBRemote::DoDetach() detach packet sent successfully"); |
| 1847 | else |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 1848 | log->Printf ("ProcessGDBRemote::DoDetach() detach packet send failed: %s", error.AsCString() ? error.AsCString() : "<unknown error>"); |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1849 | } |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 1850 | |
| 1851 | if (!error.Success()) |
| 1852 | return error; |
| 1853 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1854 | // Sleep for one second to let the process get all detached... |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1855 | StopAsyncThread (); |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1856 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1857 | SetPrivateState (eStateDetached); |
| 1858 | ResumePrivateStateThread(); |
| 1859 | |
| 1860 | //KillDebugserverProcess (); |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 1861 | return error; |
| 1862 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1863 | |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 1864 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1865 | Error |
| 1866 | ProcessGDBRemote::DoDestroy () |
| 1867 | { |
| 1868 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1869 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1870 | if (log) |
| 1871 | log->Printf ("ProcessGDBRemote::DoDestroy()"); |
| 1872 | |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 1873 | // There is a bug in older iOS debugservers where they don't shut down the process |
| 1874 | // they are debugging properly. If the process is sitting at a breakpoint or an exception, |
| 1875 | // this can cause problems with restarting. So we check to see if any of our threads are stopped |
| 1876 | // at a breakpoint, and if so we remove all the breakpoints, resume the process, and THEN |
| 1877 | // destroy it again. |
| 1878 | // |
| 1879 | // Note, we don't have a good way to test the version of debugserver, but I happen to know that |
| 1880 | // the set of all the iOS debugservers which don't support GetThreadSuffixSupported() and that of |
| 1881 | // the debugservers with this bug are equal. There really should be a better way to test this! |
| 1882 | // |
| 1883 | // We also use m_destroy_tried_resuming to make sure we only do this once, if we resume and then halt and |
| 1884 | // get called here to destroy again and we're still at a breakpoint or exception, then we should |
| 1885 | // just do the straight-forward kill. |
| 1886 | // |
| 1887 | // And of course, if we weren't able to stop the process by the time we get here, it isn't |
| 1888 | // necessary (or helpful) to do any of this. |
| 1889 | |
| 1890 | if (!m_gdb_comm.GetThreadSuffixSupported() && m_public_state.GetValue() != eStateRunning) |
| 1891 | { |
| 1892 | PlatformSP platform_sp = GetTarget().GetPlatform(); |
| 1893 | |
| 1894 | // FIXME: These should be ConstStrings so we aren't doing strcmp'ing. |
| 1895 | if (platform_sp |
| 1896 | && platform_sp->GetName() |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 1897 | && platform_sp->GetName() == PlatformRemoteiOS::GetPluginNameStatic()) |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 1898 | { |
| 1899 | if (m_destroy_tried_resuming) |
| 1900 | { |
| 1901 | if (log) |
| 1902 | log->PutCString ("ProcessGDBRemote::DoDestroy()Tried resuming to destroy once already, not doing it again."); |
| 1903 | } |
| 1904 | else |
| 1905 | { |
| 1906 | // At present, the plans are discarded and the breakpoints disabled Process::Destroy, |
| 1907 | // but we really need it to happen here and it doesn't matter if we do it twice. |
| 1908 | m_thread_list.DiscardThreadPlans(); |
| 1909 | DisableAllBreakpointSites(); |
| 1910 | |
| 1911 | bool stop_looks_like_crash = false; |
| 1912 | ThreadList &threads = GetThreadList(); |
| 1913 | |
| 1914 | { |
Jim Ingham | 4535037 | 2012-09-11 00:08:52 +0000 | [diff] [blame] | 1915 | Mutex::Locker locker(threads.GetMutex()); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 1916 | |
| 1917 | size_t num_threads = threads.GetSize(); |
| 1918 | for (size_t i = 0; i < num_threads; i++) |
| 1919 | { |
| 1920 | ThreadSP thread_sp = threads.GetThreadAtIndex(i); |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 1921 | StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo(); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 1922 | StopReason reason = eStopReasonInvalid; |
| 1923 | if (stop_info_sp) |
| 1924 | reason = stop_info_sp->GetStopReason(); |
| 1925 | if (reason == eStopReasonBreakpoint |
| 1926 | || reason == eStopReasonException) |
| 1927 | { |
| 1928 | if (log) |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1929 | log->Printf ("ProcessGDBRemote::DoDestroy() - thread: 0x%4.4" PRIx64 " stopped with reason: %s.", |
| 1930 | thread_sp->GetProtocolID(), |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 1931 | stop_info_sp->GetDescription()); |
| 1932 | stop_looks_like_crash = true; |
| 1933 | break; |
| 1934 | } |
| 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | if (stop_looks_like_crash) |
| 1939 | { |
| 1940 | if (log) |
| 1941 | log->PutCString ("ProcessGDBRemote::DoDestroy() - Stopped at a breakpoint, continue and then kill."); |
| 1942 | m_destroy_tried_resuming = true; |
| 1943 | |
| 1944 | // If we are going to run again before killing, it would be good to suspend all the threads |
| 1945 | // before resuming so they won't get into more trouble. Sadly, for the threads stopped with |
| 1946 | // the breakpoint or exception, the exception doesn't get cleared if it is suspended, so we do |
| 1947 | // have to run the risk of letting those threads proceed a bit. |
| 1948 | |
| 1949 | { |
Jim Ingham | 4535037 | 2012-09-11 00:08:52 +0000 | [diff] [blame] | 1950 | Mutex::Locker locker(threads.GetMutex()); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 1951 | |
| 1952 | size_t num_threads = threads.GetSize(); |
| 1953 | for (size_t i = 0; i < num_threads; i++) |
| 1954 | { |
| 1955 | ThreadSP thread_sp = threads.GetThreadAtIndex(i); |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 1956 | StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo(); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 1957 | StopReason reason = eStopReasonInvalid; |
| 1958 | if (stop_info_sp) |
| 1959 | reason = stop_info_sp->GetStopReason(); |
| 1960 | if (reason != eStopReasonBreakpoint |
| 1961 | && reason != eStopReasonException) |
| 1962 | { |
| 1963 | if (log) |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1964 | log->Printf ("ProcessGDBRemote::DoDestroy() - Suspending thread: 0x%4.4" PRIx64 " before running.", |
| 1965 | thread_sp->GetProtocolID()); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 1966 | thread_sp->SetResumeState(eStateSuspended); |
| 1967 | } |
| 1968 | } |
| 1969 | } |
| 1970 | Resume (); |
| 1971 | return Destroy(); |
| 1972 | } |
| 1973 | } |
| 1974 | } |
| 1975 | } |
| 1976 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1977 | // Interrupt if our inferior is running... |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 1978 | int exit_status = SIGABRT; |
| 1979 | std::string exit_string; |
| 1980 | |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 1981 | if (m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1982 | { |
Jim Ingham | aab7837 | 2011-10-28 01:11:35 +0000 | [diff] [blame] | 1983 | if (m_public_state.GetValue() != eStateAttaching) |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1984 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1985 | |
| 1986 | StringExtractorGDBRemote response; |
| 1987 | bool send_async = true; |
Filipe Cabecinhas | 9e10605 | 2012-08-22 13:25:58 +0000 | [diff] [blame] | 1988 | const uint32_t old_packet_timeout = m_gdb_comm.SetPacketTimeout (3); |
| 1989 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 1990 | if (m_gdb_comm.SendPacketAndWaitForResponse("k", 1, response, send_async) == GDBRemoteCommunication::PacketResult::Success) |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1991 | { |
| 1992 | char packet_cmd = response.GetChar(0); |
| 1993 | |
| 1994 | if (packet_cmd == 'W' || packet_cmd == 'X') |
| 1995 | { |
Greg Clayton | 09c3e3d | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 1996 | SetLastStopPacket (response); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1997 | ClearThreadIDList (); |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 1998 | exit_status = response.GetHexU8(); |
| 1999 | } |
| 2000 | else |
| 2001 | { |
| 2002 | if (log) |
| 2003 | log->Printf ("ProcessGDBRemote::DoDestroy - got unexpected response to k packet: %s", response.GetStringRef().c_str()); |
| 2004 | exit_string.assign("got unexpected response to k packet: "); |
| 2005 | exit_string.append(response.GetStringRef()); |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2006 | } |
| 2007 | } |
| 2008 | else |
| 2009 | { |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2010 | if (log) |
| 2011 | log->Printf ("ProcessGDBRemote::DoDestroy - failed to send k packet"); |
| 2012 | exit_string.assign("failed to send the k packet"); |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2013 | } |
Filipe Cabecinhas | 9e10605 | 2012-08-22 13:25:58 +0000 | [diff] [blame] | 2014 | |
| 2015 | m_gdb_comm.SetPacketTimeout(old_packet_timeout); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2016 | } |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2017 | else |
| 2018 | { |
| 2019 | if (log) |
| 2020 | log->Printf ("ProcessGDBRemote::DoDestroy - failed to send k packet"); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 2021 | exit_string.assign ("killed or interrupted while attaching."); |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2022 | } |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2023 | } |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2024 | else |
| 2025 | { |
| 2026 | // If we missed setting the exit status on the way out, do it here. |
| 2027 | // NB set exit status can be called multiple times, the first one sets the status. |
| 2028 | exit_string.assign("destroying when not connected to debugserver"); |
| 2029 | } |
| 2030 | |
| 2031 | SetExitStatus(exit_status, exit_string.c_str()); |
| 2032 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2033 | StopAsyncThread (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2034 | KillDebugserverProcess (); |
| 2035 | return error; |
| 2036 | } |
| 2037 | |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2038 | void |
| 2039 | ProcessGDBRemote::SetLastStopPacket (const StringExtractorGDBRemote &response) |
| 2040 | { |
| 2041 | lldb_private::Mutex::Locker locker (m_last_stop_packet_mutex); |
| 2042 | const bool did_exec = response.GetStringRef().find(";reason:exec;") != std::string::npos; |
| 2043 | if (did_exec) |
| 2044 | { |
| 2045 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 2046 | if (log) |
| 2047 | log->Printf ("ProcessGDBRemote::SetLastStopPacket () - detected exec"); |
| 2048 | |
| 2049 | m_thread_list_real.Clear(); |
| 2050 | m_thread_list.Clear(); |
| 2051 | BuildDynamicRegisterInfo (true); |
| 2052 | m_gdb_comm.ResetDiscoverableSettings(); |
| 2053 | } |
| 2054 | m_last_stop_packet = response; |
| 2055 | } |
| 2056 | |
| 2057 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2058 | //------------------------------------------------------------------ |
| 2059 | // Process Queries |
| 2060 | //------------------------------------------------------------------ |
| 2061 | |
| 2062 | bool |
| 2063 | ProcessGDBRemote::IsAlive () |
| 2064 | { |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 2065 | return m_gdb_comm.IsConnected() && m_private_state.GetValue() != eStateExited; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | addr_t |
| 2069 | ProcessGDBRemote::GetImageInfoAddress() |
| 2070 | { |
Jason Molenda | 6ba6d3d | 2013-01-30 04:39:32 +0000 | [diff] [blame] | 2071 | return m_gdb_comm.GetShlibInfoAddr(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2074 | //------------------------------------------------------------------ |
| 2075 | // Process Memory |
| 2076 | //------------------------------------------------------------------ |
| 2077 | size_t |
| 2078 | ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 2079 | { |
| 2080 | if (size > m_max_memory_size) |
| 2081 | { |
| 2082 | // Keep memory read sizes down to a sane limit. This function will be |
| 2083 | // called multiple times in order to complete the task by |
| 2084 | // lldb_private::Process so it is ok to do this. |
| 2085 | size = m_max_memory_size; |
| 2086 | } |
| 2087 | |
| 2088 | char packet[64]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2089 | const int packet_len = ::snprintf (packet, sizeof(packet), "m%" PRIx64 ",%" PRIx64, (uint64_t)addr, (uint64_t)size); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 2090 | assert (packet_len + 1 < (int)sizeof(packet)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2091 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2092 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet, packet_len, response, true) == GDBRemoteCommunication::PacketResult::Success) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2093 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2094 | if (response.IsNormalResponse()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2095 | { |
| 2096 | error.Clear(); |
| 2097 | return response.GetHexBytes(buf, size, '\xdd'); |
| 2098 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2099 | else if (response.IsErrorResponse()) |
Greg Clayton | b9d5df5 | 2012-12-06 22:49:16 +0000 | [diff] [blame] | 2100 | error.SetErrorStringWithFormat("memory read failed for 0x%" PRIx64, addr); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2101 | else if (response.IsUnsupportedResponse()) |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 2102 | error.SetErrorStringWithFormat("GDB server does not support reading memory"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2103 | else |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 2104 | error.SetErrorStringWithFormat("unexpected response to GDB server memory read packet '%s': '%s'", packet, response.GetStringRef().c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2105 | } |
| 2106 | else |
| 2107 | { |
Jim Ingham | 35579dd | 2013-06-03 19:34:01 +0000 | [diff] [blame] | 2108 | error.SetErrorStringWithFormat("failed to send packet: '%s'", packet); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2109 | } |
| 2110 | return 0; |
| 2111 | } |
| 2112 | |
| 2113 | size_t |
| 2114 | ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 2115 | { |
Greg Clayton | b4aaf2e | 2011-05-16 02:35:02 +0000 | [diff] [blame] | 2116 | if (size > m_max_memory_size) |
| 2117 | { |
| 2118 | // Keep memory read sizes down to a sane limit. This function will be |
| 2119 | // called multiple times in order to complete the task by |
| 2120 | // lldb_private::Process so it is ok to do this. |
| 2121 | size = m_max_memory_size; |
| 2122 | } |
| 2123 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2124 | StreamString packet; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2125 | packet.Printf("M%" PRIx64 ",%" PRIx64 ":", addr, (uint64_t)size); |
Greg Clayton | 7fb56d0 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 2126 | packet.PutBytesAsRawHex8(buf, size, lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2127 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2128 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, true) == GDBRemoteCommunication::PacketResult::Success) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2129 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2130 | if (response.IsOKResponse()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2131 | { |
| 2132 | error.Clear(); |
| 2133 | return size; |
| 2134 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2135 | else if (response.IsErrorResponse()) |
Greg Clayton | b9d5df5 | 2012-12-06 22:49:16 +0000 | [diff] [blame] | 2136 | error.SetErrorStringWithFormat("memory write failed for 0x%" PRIx64, addr); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2137 | else if (response.IsUnsupportedResponse()) |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 2138 | error.SetErrorStringWithFormat("GDB server does not support writing memory"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2139 | else |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 2140 | error.SetErrorStringWithFormat("unexpected response to GDB server memory write packet '%s': '%s'", packet.GetString().c_str(), response.GetStringRef().c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2141 | } |
| 2142 | else |
| 2143 | { |
Jim Ingham | 35579dd | 2013-06-03 19:34:01 +0000 | [diff] [blame] | 2144 | error.SetErrorStringWithFormat("failed to send packet: '%s'", packet.GetString().c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2145 | } |
| 2146 | return 0; |
| 2147 | } |
| 2148 | |
| 2149 | lldb::addr_t |
| 2150 | ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &error) |
| 2151 | { |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 2152 | addr_t allocated_addr = LLDB_INVALID_ADDRESS; |
| 2153 | |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 2154 | LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory(); |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 2155 | switch (supported) |
| 2156 | { |
| 2157 | case eLazyBoolCalculate: |
| 2158 | case eLazyBoolYes: |
| 2159 | allocated_addr = m_gdb_comm.AllocateMemory (size, permissions); |
| 2160 | if (allocated_addr != LLDB_INVALID_ADDRESS || supported == eLazyBoolYes) |
| 2161 | return allocated_addr; |
| 2162 | |
| 2163 | case eLazyBoolNo: |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 2164 | // Call mmap() to create memory in the inferior.. |
| 2165 | unsigned prot = 0; |
| 2166 | if (permissions & lldb::ePermissionsReadable) |
| 2167 | prot |= eMmapProtRead; |
| 2168 | if (permissions & lldb::ePermissionsWritable) |
| 2169 | prot |= eMmapProtWrite; |
| 2170 | if (permissions & lldb::ePermissionsExecutable) |
| 2171 | prot |= eMmapProtExec; |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 2172 | |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 2173 | if (InferiorCallMmap(this, allocated_addr, 0, size, prot, |
| 2174 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) |
| 2175 | m_addr_to_mmap_size[allocated_addr] = size; |
| 2176 | else |
| 2177 | allocated_addr = LLDB_INVALID_ADDRESS; |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 2178 | break; |
| 2179 | } |
| 2180 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2181 | if (allocated_addr == LLDB_INVALID_ADDRESS) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2182 | error.SetErrorStringWithFormat("unable to allocate %" PRIu64 " bytes of memory with permissions %s", (uint64_t)size, GetPermissionsAsCString (permissions)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2183 | else |
| 2184 | error.Clear(); |
| 2185 | return allocated_addr; |
| 2186 | } |
| 2187 | |
| 2188 | Error |
Greg Clayton | 46fb558 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 2189 | ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr, |
| 2190 | MemoryRegionInfo ®ion_info) |
| 2191 | { |
| 2192 | |
| 2193 | Error error (m_gdb_comm.GetMemoryRegionInfo (load_addr, region_info)); |
| 2194 | return error; |
| 2195 | } |
| 2196 | |
| 2197 | Error |
Johnny Chen | 6463720 | 2012-05-23 21:09:52 +0000 | [diff] [blame] | 2198 | ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num) |
| 2199 | { |
| 2200 | |
| 2201 | Error error (m_gdb_comm.GetWatchpointSupportInfo (num)); |
| 2202 | return error; |
| 2203 | } |
| 2204 | |
| 2205 | Error |
Enrico Granata | f04a219 | 2012-07-13 23:18:48 +0000 | [diff] [blame] | 2206 | ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num, bool& after) |
| 2207 | { |
| 2208 | Error error (m_gdb_comm.GetWatchpointSupportInfo (num, after)); |
| 2209 | return error; |
| 2210 | } |
| 2211 | |
| 2212 | Error |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2213 | ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr) |
| 2214 | { |
| 2215 | Error error; |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 2216 | LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory(); |
| 2217 | |
| 2218 | switch (supported) |
| 2219 | { |
| 2220 | case eLazyBoolCalculate: |
| 2221 | // We should never be deallocating memory without allocating memory |
| 2222 | // first so we should never get eLazyBoolCalculate |
| 2223 | error.SetErrorString ("tried to deallocate memory without ever allocating memory"); |
| 2224 | break; |
| 2225 | |
| 2226 | case eLazyBoolYes: |
| 2227 | if (!m_gdb_comm.DeallocateMemory (addr)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2228 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr); |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 2229 | break; |
| 2230 | |
| 2231 | case eLazyBoolNo: |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 2232 | // Call munmap() to deallocate memory in the inferior.. |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 2233 | { |
| 2234 | MMapMap::iterator pos = m_addr_to_mmap_size.find(addr); |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 2235 | if (pos != m_addr_to_mmap_size.end() && |
| 2236 | InferiorCallMunmap(this, addr, pos->second)) |
| 2237 | m_addr_to_mmap_size.erase (pos); |
| 2238 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2239 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr); |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 2240 | } |
| 2241 | break; |
| 2242 | } |
| 2243 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2244 | return error; |
| 2245 | } |
| 2246 | |
| 2247 | |
| 2248 | //------------------------------------------------------------------ |
| 2249 | // Process STDIO |
| 2250 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2251 | size_t |
| 2252 | ProcessGDBRemote::PutSTDIN (const char *src, size_t src_len, Error &error) |
| 2253 | { |
| 2254 | if (m_stdio_communication.IsConnected()) |
| 2255 | { |
| 2256 | ConnectionStatus status; |
| 2257 | m_stdio_communication.Write(src, src_len, status, NULL); |
| 2258 | } |
| 2259 | return 0; |
| 2260 | } |
| 2261 | |
| 2262 | Error |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2263 | ProcessGDBRemote::EnableBreakpointSite (BreakpointSite *bp_site) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2264 | { |
| 2265 | Error error; |
| 2266 | assert (bp_site != NULL); |
| 2267 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2268 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2269 | user_id_t site_id = bp_site->GetID(); |
| 2270 | const addr_t addr = bp_site->GetLoadAddress(); |
| 2271 | if (log) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2272 | log->Printf ("ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 ") address = 0x%" PRIx64, site_id, (uint64_t)addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2273 | |
| 2274 | if (bp_site->IsEnabled()) |
| 2275 | { |
| 2276 | if (log) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2277 | log->Printf ("ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 ") address = 0x%" PRIx64 " -- SUCCESS (already enabled)", site_id, (uint64_t)addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2278 | return error; |
| 2279 | } |
| 2280 | else |
| 2281 | { |
| 2282 | const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site); |
| 2283 | |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2284 | if (bp_site->HardwareRequired()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2285 | { |
| 2286 | // Try and set hardware breakpoint, and if that fails, fall through |
| 2287 | // and set a software breakpoint? |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2288 | if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointHardware)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2289 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2290 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, true, addr, bp_op_size) == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2291 | { |
| 2292 | bp_site->SetEnabled(true); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2293 | bp_site->SetType (BreakpointSite::eHardware); |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2294 | } |
| 2295 | else |
| 2296 | { |
| 2297 | error.SetErrorString("failed to set hardware breakpoint (hardware breakpoint resources might be exhausted or unavailable)"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2298 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2299 | } |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2300 | else |
| 2301 | { |
| 2302 | error.SetErrorString("hardware breakpoints are not supported"); |
| 2303 | } |
| 2304 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2305 | } |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2306 | else if (m_gdb_comm.SupportsGDBStoppointPacket (eBreakpointSoftware)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2307 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2308 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, true, addr, bp_op_size) == 0) |
| 2309 | { |
| 2310 | bp_site->SetEnabled(true); |
| 2311 | bp_site->SetType (BreakpointSite::eExternal); |
| 2312 | return error; |
| 2313 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2314 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2315 | |
| 2316 | return EnableSoftwareBreakpoint (bp_site); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | if (log) |
| 2320 | { |
| 2321 | const char *err_string = error.AsCString(); |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2322 | log->Printf ("ProcessGDBRemote::EnableBreakpointSite () error for breakpoint at 0x%8.8" PRIx64 ": %s", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2323 | bp_site->GetLoadAddress(), |
| 2324 | err_string ? err_string : "NULL"); |
| 2325 | } |
| 2326 | // We shouldn't reach here on a successful breakpoint enable... |
| 2327 | if (error.Success()) |
| 2328 | error.SetErrorToGenericError(); |
| 2329 | return error; |
| 2330 | } |
| 2331 | |
| 2332 | Error |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2333 | ProcessGDBRemote::DisableBreakpointSite (BreakpointSite *bp_site) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2334 | { |
| 2335 | Error error; |
| 2336 | assert (bp_site != NULL); |
| 2337 | addr_t addr = bp_site->GetLoadAddress(); |
| 2338 | user_id_t site_id = bp_site->GetID(); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2339 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2340 | if (log) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2341 | log->Printf ("ProcessGDBRemote::DisableBreakpointSite (site_id = %" PRIu64 ") addr = 0x%8.8" PRIx64, site_id, (uint64_t)addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2342 | |
| 2343 | if (bp_site->IsEnabled()) |
| 2344 | { |
| 2345 | const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site); |
| 2346 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2347 | BreakpointSite::Type bp_type = bp_site->GetType(); |
| 2348 | switch (bp_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2349 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2350 | case BreakpointSite::eSoftware: |
| 2351 | error = DisableSoftwareBreakpoint (bp_site); |
| 2352 | break; |
| 2353 | |
| 2354 | case BreakpointSite::eHardware: |
| 2355 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size)) |
| 2356 | error.SetErrorToGenericError(); |
| 2357 | break; |
| 2358 | |
| 2359 | case BreakpointSite::eExternal: |
| 2360 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, false, addr, bp_op_size)) |
| 2361 | error.SetErrorToGenericError(); |
| 2362 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2363 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2364 | if (error.Success()) |
| 2365 | bp_site->SetEnabled(false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2366 | } |
| 2367 | else |
| 2368 | { |
| 2369 | if (log) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2370 | log->Printf ("ProcessGDBRemote::DisableBreakpointSite (site_id = %" PRIu64 ") addr = 0x%8.8" PRIx64 " -- SUCCESS (already disabled)", site_id, (uint64_t)addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2371 | return error; |
| 2372 | } |
| 2373 | |
| 2374 | if (error.Success()) |
| 2375 | error.SetErrorToGenericError(); |
| 2376 | return error; |
| 2377 | } |
| 2378 | |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2379 | // Pre-requisite: wp != NULL. |
| 2380 | static GDBStoppointType |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2381 | GetGDBStoppointType (Watchpoint *wp) |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2382 | { |
| 2383 | assert(wp); |
| 2384 | bool watch_read = wp->WatchpointRead(); |
| 2385 | bool watch_write = wp->WatchpointWrite(); |
| 2386 | |
| 2387 | // watch_read and watch_write cannot both be false. |
| 2388 | assert(watch_read || watch_write); |
| 2389 | if (watch_read && watch_write) |
| 2390 | return eWatchpointReadWrite; |
Johnny Chen | 6d487a9 | 2011-09-09 20:35:15 +0000 | [diff] [blame] | 2391 | else if (watch_read) |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2392 | return eWatchpointRead; |
Johnny Chen | 6d487a9 | 2011-09-09 20:35:15 +0000 | [diff] [blame] | 2393 | else // Must be watch_write, then. |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2394 | return eWatchpointWrite; |
| 2395 | } |
| 2396 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2397 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2398 | ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2399 | { |
| 2400 | Error error; |
| 2401 | if (wp) |
| 2402 | { |
| 2403 | user_id_t watchID = wp->GetID(); |
| 2404 | addr_t addr = wp->GetLoadAddress(); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2405 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2406 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2407 | log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 ")", watchID); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2408 | if (wp->IsEnabled()) |
| 2409 | { |
| 2410 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2411 | log->Printf("ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.", watchID, (uint64_t)addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2412 | return error; |
| 2413 | } |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2414 | |
| 2415 | GDBStoppointType type = GetGDBStoppointType(wp); |
| 2416 | // Pass down an appropriate z/Z packet... |
| 2417 | if (m_gdb_comm.SupportsGDBStoppointPacket (type)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2418 | { |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2419 | if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, wp->GetByteSize()) == 0) |
| 2420 | { |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2421 | wp->SetEnabled(true, notify); |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2422 | return error; |
| 2423 | } |
| 2424 | else |
| 2425 | error.SetErrorString("sending gdb watchpoint packet failed"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2426 | } |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2427 | else |
| 2428 | error.SetErrorString("watchpoints not supported"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2429 | } |
| 2430 | else |
| 2431 | { |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2432 | error.SetErrorString("Watchpoint argument was NULL."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2433 | } |
| 2434 | if (error.Success()) |
| 2435 | error.SetErrorToGenericError(); |
| 2436 | return error; |
| 2437 | } |
| 2438 | |
| 2439 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2440 | ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2441 | { |
| 2442 | Error error; |
| 2443 | if (wp) |
| 2444 | { |
| 2445 | user_id_t watchID = wp->GetID(); |
| 2446 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2447 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2448 | |
| 2449 | addr_t addr = wp->GetLoadAddress(); |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2450 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2451 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2452 | log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64, watchID, (uint64_t)addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2453 | |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2454 | if (!wp->IsEnabled()) |
| 2455 | { |
| 2456 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2457 | log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64 " -- SUCCESS (already disabled)", watchID, (uint64_t)addr); |
Johnny Chen | 892943f | 2012-08-23 22:28:26 +0000 | [diff] [blame] | 2458 | // See also 'class WatchpointSentry' within StopInfo.cpp. |
| 2459 | // This disabling attempt might come from the user-supplied actions, we'll route it in order for |
| 2460 | // the watchpoint object to intelligently process this action. |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2461 | wp->SetEnabled(false, notify); |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2462 | return error; |
| 2463 | } |
| 2464 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2465 | if (wp->IsHardware()) |
| 2466 | { |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2467 | GDBStoppointType type = GetGDBStoppointType(wp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2468 | // Pass down an appropriate z/Z packet... |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2469 | if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, wp->GetByteSize()) == 0) |
| 2470 | { |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2471 | wp->SetEnabled(false, notify); |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2472 | return error; |
| 2473 | } |
| 2474 | else |
| 2475 | error.SetErrorString("sending gdb watchpoint packet failed"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2476 | } |
| 2477 | // TODO: clear software watchpoints if we implement them |
| 2478 | } |
| 2479 | else |
| 2480 | { |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2481 | error.SetErrorString("Watchpoint argument was NULL."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2482 | } |
| 2483 | if (error.Success()) |
| 2484 | error.SetErrorToGenericError(); |
| 2485 | return error; |
| 2486 | } |
| 2487 | |
| 2488 | void |
| 2489 | ProcessGDBRemote::Clear() |
| 2490 | { |
| 2491 | m_flags = 0; |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 2492 | m_thread_list_real.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2493 | m_thread_list.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
| 2496 | Error |
| 2497 | ProcessGDBRemote::DoSignal (int signo) |
| 2498 | { |
| 2499 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2500 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2501 | if (log) |
| 2502 | log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo); |
| 2503 | |
| 2504 | if (!m_gdb_comm.SendAsyncSignal (signo)) |
| 2505 | error.SetErrorStringWithFormat("failed to send signal %i", signo); |
| 2506 | return error; |
| 2507 | } |
| 2508 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2509 | Error |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 2510 | ProcessGDBRemote::LaunchAndConnectToDebugserver (const ProcessInfo &process_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2511 | { |
| 2512 | Error error; |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 2513 | uint16_t port = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2514 | if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID) |
| 2515 | { |
| 2516 | // If we locate debugserver, keep that located version around |
| 2517 | static FileSpec g_debugserver_file_spec; |
| 2518 | |
Han Ming Ong | 8464704 | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 2519 | ProcessLaunchInfo debugserver_launch_info; |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 2520 | debugserver_launch_info.SetMonitorProcessCallback (MonitorDebugserverProcess, this, false); |
| 2521 | debugserver_launch_info.SetUserID(process_info.GetUserID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2522 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 2523 | error = m_gdb_comm.StartDebugserverProcess (NULL, |
| 2524 | debugserver_launch_info, |
| 2525 | port); |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 2526 | |
| 2527 | if (error.Success ()) |
| 2528 | m_debugserver_pid = debugserver_launch_info.GetProcessID(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2529 | else |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 2530 | m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2531 | |
| 2532 | if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) |
| 2533 | StartAsyncThread (); |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 2534 | |
| 2535 | if (error.Fail()) |
| 2536 | { |
| 2537 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
| 2538 | |
| 2539 | if (log) |
| 2540 | log->Printf("failed to start debugserver process: %s", error.AsCString()); |
| 2541 | return error; |
| 2542 | } |
| 2543 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 2544 | if (m_gdb_comm.IsConnected()) |
| 2545 | { |
| 2546 | // Finish the connection process by doing the handshake without connecting (send NULL URL) |
| 2547 | ConnectToDebugserver (NULL); |
| 2548 | } |
| 2549 | else |
| 2550 | { |
| 2551 | char connect_url[128]; |
| 2552 | snprintf (connect_url, sizeof(connect_url), "connect://localhost:%u", port); |
| 2553 | error = ConnectToDebugserver (connect_url); |
| 2554 | } |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 2555 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2556 | } |
| 2557 | return error; |
| 2558 | } |
| 2559 | |
| 2560 | bool |
| 2561 | ProcessGDBRemote::MonitorDebugserverProcess |
| 2562 | ( |
| 2563 | void *callback_baton, |
| 2564 | lldb::pid_t debugserver_pid, |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2565 | bool exited, // True if the process did exit |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2566 | int signo, // Zero for no signal |
| 2567 | int exit_status // Exit value of process if signal is zero |
| 2568 | ) |
| 2569 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2570 | // The baton is a "ProcessGDBRemote *". Now this class might be gone |
| 2571 | // and might not exist anymore, so we need to carefully try to get the |
| 2572 | // target for this process first since we have a race condition when |
| 2573 | // we are done running between getting the notice that the inferior |
| 2574 | // process has died and the debugserver that was debugging this process. |
| 2575 | // In our test suite, we are also continually running process after |
| 2576 | // process, so we must be very careful to make sure: |
| 2577 | // 1 - process object hasn't been deleted already |
| 2578 | // 2 - that a new process object hasn't been recreated in its place |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2579 | |
| 2580 | // "debugserver_pid" argument passed in is the process ID for |
| 2581 | // debugserver that we are tracking... |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2582 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2583 | |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 2584 | ProcessGDBRemote *process = (ProcessGDBRemote *)callback_baton; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2585 | |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2586 | // Get a shared pointer to the target that has a matching process pointer. |
| 2587 | // This target could be gone, or the target could already have a new process |
| 2588 | // object inside of it |
| 2589 | TargetSP target_sp (Debugger::FindTargetWithProcess(process)); |
| 2590 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2591 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2592 | log->Printf ("ProcessGDBRemote::MonitorDebugserverProcess (baton=%p, pid=%" PRIu64 ", signo=%i (0x%x), exit_status=%i)", callback_baton, debugserver_pid, signo, signo, exit_status); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2593 | |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2594 | if (target_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2595 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2596 | // We found a process in a target that matches, but another thread |
| 2597 | // might be in the process of launching a new process that will |
| 2598 | // soon replace it, so get a shared pointer to the process so we |
| 2599 | // can keep it alive. |
| 2600 | ProcessSP process_sp (target_sp->GetProcessSP()); |
| 2601 | // Now we have a shared pointer to the process that can't go away on us |
| 2602 | // so we now make sure it was the same as the one passed in, and also make |
| 2603 | // sure that our previous "process *" didn't get deleted and have a new |
| 2604 | // "process *" created in its place with the same pointer. To verify this |
| 2605 | // we make sure the process has our debugserver process ID. If we pass all |
| 2606 | // of these tests, then we are sure that this process is the one we were |
| 2607 | // looking for. |
| 2608 | if (process_sp && process == process_sp.get() && process->m_debugserver_pid == debugserver_pid) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2609 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2610 | // Sleep for a half a second to make sure our inferior process has |
| 2611 | // time to set its exit status before we set it incorrectly when |
| 2612 | // both the debugserver and the inferior process shut down. |
| 2613 | usleep (500000); |
| 2614 | // If our process hasn't yet exited, debugserver might have died. |
| 2615 | // If the process did exit, the we are reaping it. |
| 2616 | const StateType state = process->GetState(); |
| 2617 | |
| 2618 | if (process->m_debugserver_pid != LLDB_INVALID_PROCESS_ID && |
| 2619 | state != eStateInvalid && |
| 2620 | state != eStateUnloaded && |
| 2621 | state != eStateExited && |
| 2622 | state != eStateDetached) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2623 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2624 | char error_str[1024]; |
| 2625 | if (signo) |
| 2626 | { |
| 2627 | const char *signal_cstr = process->GetUnixSignals().GetSignalAsCString (signo); |
| 2628 | if (signal_cstr) |
| 2629 | ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %s", signal_cstr); |
| 2630 | else |
| 2631 | ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %i", signo); |
| 2632 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2633 | else |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2634 | { |
| 2635 | ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", exit_status); |
| 2636 | } |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 2637 | |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 2638 | process->SetExitStatus (-1, error_str); |
| 2639 | } |
| 2640 | // Debugserver has exited we need to let our ProcessGDBRemote |
| 2641 | // know that it no longer has a debugserver instance |
| 2642 | process->m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 2643 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2644 | } |
| 2645 | return true; |
| 2646 | } |
| 2647 | |
| 2648 | void |
| 2649 | ProcessGDBRemote::KillDebugserverProcess () |
| 2650 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 2651 | m_gdb_comm.Disconnect(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2652 | if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) |
| 2653 | { |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 2654 | Host::Kill (m_debugserver_pid, SIGINT); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2655 | m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | void |
| 2660 | ProcessGDBRemote::Initialize() |
| 2661 | { |
| 2662 | static bool g_initialized = false; |
| 2663 | |
| 2664 | if (g_initialized == false) |
| 2665 | { |
| 2666 | g_initialized = true; |
| 2667 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 2668 | GetPluginDescriptionStatic(), |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 2669 | CreateInstance, |
| 2670 | DebuggerInitialize); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2671 | |
| 2672 | Log::Callbacks log_callbacks = { |
| 2673 | ProcessGDBRemoteLog::DisableLog, |
| 2674 | ProcessGDBRemoteLog::EnableLog, |
| 2675 | ProcessGDBRemoteLog::ListLogCategories |
| 2676 | }; |
| 2677 | |
| 2678 | Log::RegisterLogChannel (ProcessGDBRemote::GetPluginNameStatic(), log_callbacks); |
| 2679 | } |
| 2680 | } |
| 2681 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 2682 | void |
| 2683 | ProcessGDBRemote::DebuggerInitialize (lldb_private::Debugger &debugger) |
| 2684 | { |
| 2685 | if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName())) |
| 2686 | { |
| 2687 | const bool is_global_setting = true; |
| 2688 | PluginManager::CreateSettingForProcessPlugin (debugger, |
| 2689 | GetGlobalPluginProperties()->GetValueProperties(), |
| 2690 | ConstString ("Properties for the gdb-remote process plug-in."), |
| 2691 | is_global_setting); |
| 2692 | } |
| 2693 | } |
| 2694 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2695 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2696 | ProcessGDBRemote::StartAsyncThread () |
| 2697 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2698 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2699 | |
| 2700 | if (log) |
| 2701 | log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__); |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 2702 | |
| 2703 | Mutex::Locker start_locker(m_async_thread_state_mutex); |
| 2704 | if (m_async_thread_state == eAsyncThreadNotStarted) |
| 2705 | { |
| 2706 | // Create a thread that watches our internal state and controls which |
| 2707 | // events make it to clients (into the DCProcess event queue). |
| 2708 | m_async_thread = Host::ThreadCreate ("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL); |
| 2709 | if (IS_VALID_LLDB_HOST_THREAD(m_async_thread)) |
| 2710 | { |
| 2711 | m_async_thread_state = eAsyncThreadRunning; |
| 2712 | return true; |
| 2713 | } |
| 2714 | else |
| 2715 | return false; |
| 2716 | } |
| 2717 | else |
| 2718 | { |
| 2719 | // Somebody tried to start the async thread while it was either being started or stopped. If the former, and |
| 2720 | // it started up successfully, then say all's well. Otherwise it is an error, since we aren't going to restart it. |
| 2721 | if (log) |
| 2722 | log->Printf ("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread_state); |
| 2723 | if (m_async_thread_state == eAsyncThreadRunning) |
| 2724 | return true; |
| 2725 | else |
| 2726 | return false; |
| 2727 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | void |
| 2731 | ProcessGDBRemote::StopAsyncThread () |
| 2732 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2733 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2734 | |
| 2735 | if (log) |
| 2736 | log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__); |
| 2737 | |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 2738 | Mutex::Locker start_locker(m_async_thread_state_mutex); |
| 2739 | if (m_async_thread_state == eAsyncThreadRunning) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2740 | { |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 2741 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit); |
| 2742 | |
| 2743 | // This will shut down the async thread. |
| 2744 | m_gdb_comm.Disconnect(); // Disconnect from the debug server. |
| 2745 | |
| 2746 | // Stop the stdio thread |
| 2747 | if (IS_VALID_LLDB_HOST_THREAD(m_async_thread)) |
| 2748 | { |
| 2749 | Host::ThreadJoin (m_async_thread, NULL, NULL); |
| 2750 | } |
| 2751 | m_async_thread_state = eAsyncThreadDone; |
| 2752 | } |
| 2753 | else |
| 2754 | { |
| 2755 | if (log) |
| 2756 | log->Printf ("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread_state); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 2761 | thread_result_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2762 | ProcessGDBRemote::AsyncThread (void *arg) |
| 2763 | { |
| 2764 | ProcessGDBRemote *process = (ProcessGDBRemote*) arg; |
| 2765 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2766 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2767 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2768 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread starting...", __FUNCTION__, arg, process->GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2769 | |
| 2770 | Listener listener ("ProcessGDBRemote::AsyncThread"); |
| 2771 | EventSP event_sp; |
| 2772 | const uint32_t desired_event_mask = eBroadcastBitAsyncContinue | |
| 2773 | eBroadcastBitAsyncThreadShouldExit; |
| 2774 | |
| 2775 | if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask) |
| 2776 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2777 | listener.StartListeningForEvents (&process->m_gdb_comm, Communication::eBroadcastBitReadThreadDidExit); |
| 2778 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2779 | bool done = false; |
| 2780 | while (!done) |
| 2781 | { |
| 2782 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2783 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2784 | if (listener.WaitForEvent (NULL, event_sp)) |
| 2785 | { |
| 2786 | const uint32_t event_type = event_sp->GetType(); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2787 | if (event_sp->BroadcasterIs (&process->m_async_broadcaster)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2788 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2789 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2790 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") Got an event of type: %d...", __FUNCTION__, arg, process->GetID(), event_type); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2791 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2792 | switch (event_type) |
| 2793 | { |
| 2794 | case eBroadcastBitAsyncContinue: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2795 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2796 | const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2797 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2798 | if (continue_packet) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2799 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2800 | const char *continue_cstr = (const char *)continue_packet->GetBytes (); |
| 2801 | const size_t continue_cstr_len = continue_packet->GetByteSize (); |
| 2802 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2803 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2804 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2805 | if (::strstr (continue_cstr, "vAttach") == NULL) |
| 2806 | process->SetPrivateState(eStateRunning); |
| 2807 | StringExtractorGDBRemote response; |
| 2808 | StateType stop_state = process->GetGDBRemote().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2809 | |
Greg Clayton | 0772ded | 2012-05-16 02:48:06 +0000 | [diff] [blame] | 2810 | // We need to immediately clear the thread ID list so we are sure to get a valid list of threads. |
| 2811 | // The thread ID list might be contained within the "response", or the stop reply packet that |
| 2812 | // caused the stop. So clear it now before we give the stop reply packet to the process |
| 2813 | // using the process->SetLastStopPacket()... |
| 2814 | process->ClearThreadIDList (); |
| 2815 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2816 | switch (stop_state) |
| 2817 | { |
| 2818 | case eStateStopped: |
| 2819 | case eStateCrashed: |
| 2820 | case eStateSuspended: |
Greg Clayton | 09c3e3d | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 2821 | process->SetLastStopPacket (response); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2822 | process->SetPrivateState (stop_state); |
| 2823 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2824 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2825 | case eStateExited: |
Greg Clayton | 09c3e3d | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 2826 | process->SetLastStopPacket (response); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2827 | process->ClearThreadIDList(); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2828 | response.SetFilePos(1); |
| 2829 | process->SetExitStatus(response.GetHexU8(), NULL); |
| 2830 | done = true; |
| 2831 | break; |
| 2832 | |
| 2833 | case eStateInvalid: |
| 2834 | process->SetExitStatus(-1, "lost connection"); |
| 2835 | break; |
| 2836 | |
| 2837 | default: |
| 2838 | process->SetPrivateState (stop_state); |
| 2839 | break; |
| 2840 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2841 | } |
| 2842 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2843 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2844 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2845 | case eBroadcastBitAsyncThreadShouldExit: |
| 2846 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2847 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID()); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2848 | done = true; |
| 2849 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2850 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2851 | default: |
| 2852 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2853 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") got unknown event 0x%8.8x", __FUNCTION__, arg, process->GetID(), event_type); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2854 | done = true; |
| 2855 | break; |
| 2856 | } |
| 2857 | } |
| 2858 | else if (event_sp->BroadcasterIs (&process->m_gdb_comm)) |
| 2859 | { |
| 2860 | if (event_type & Communication::eBroadcastBitReadThreadDidExit) |
| 2861 | { |
| 2862 | process->SetExitStatus (-1, "lost connection"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2863 | done = true; |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2864 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2865 | } |
| 2866 | } |
| 2867 | else |
| 2868 | { |
| 2869 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2870 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false", __FUNCTION__, arg, process->GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2871 | done = true; |
| 2872 | } |
| 2873 | } |
| 2874 | } |
| 2875 | |
| 2876 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2877 | log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", __FUNCTION__, arg, process->GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2878 | |
| 2879 | process->m_async_thread = LLDB_INVALID_HOST_THREAD; |
| 2880 | return NULL; |
| 2881 | } |
| 2882 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 2883 | //uint32_t |
| 2884 | //ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) |
| 2885 | //{ |
| 2886 | // // If we are planning to launch the debugserver remotely, then we need to fire up a debugserver |
| 2887 | // // process and ask it for the list of processes. But if we are local, we can let the Host do it. |
| 2888 | // if (m_local_debugserver) |
| 2889 | // { |
| 2890 | // return Host::ListProcessesMatchingName (name, matches, pids); |
| 2891 | // } |
| 2892 | // else |
| 2893 | // { |
| 2894 | // // FIXME: Implement talking to the remote debugserver. |
| 2895 | // return 0; |
| 2896 | // } |
| 2897 | // |
| 2898 | //} |
| 2899 | // |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2900 | bool |
| 2901 | ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton, |
| 2902 | lldb_private::StoppointCallbackContext *context, |
| 2903 | lldb::user_id_t break_id, |
| 2904 | lldb::user_id_t break_loc_id) |
| 2905 | { |
| 2906 | // I don't think I have to do anything here, just make sure I notice the new thread when it starts to |
| 2907 | // run so I can stop it if that's what I want to do. |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2908 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2909 | if (log) |
| 2910 | log->Printf("Hit New Thread Notification breakpoint."); |
| 2911 | return false; |
| 2912 | } |
| 2913 | |
| 2914 | |
| 2915 | bool |
| 2916 | ProcessGDBRemote::StartNoticingNewThreads() |
| 2917 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2918 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2919 | if (m_thread_create_bp_sp) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2920 | { |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2921 | if (log && log->GetVerbose()) |
| 2922 | log->Printf("Enabled noticing new thread breakpoint."); |
| 2923 | m_thread_create_bp_sp->SetEnabled(true); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2924 | } |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2925 | else |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2926 | { |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2927 | PlatformSP platform_sp (m_target.GetPlatform()); |
| 2928 | if (platform_sp) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2929 | { |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2930 | m_thread_create_bp_sp = platform_sp->SetThreadCreationBreakpoint(m_target); |
| 2931 | if (m_thread_create_bp_sp) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2932 | { |
Jim Ingham | 37cfeab | 2011-10-15 00:21:37 +0000 | [diff] [blame] | 2933 | if (log && log->GetVerbose()) |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2934 | log->Printf("Successfully created new thread notification breakpoint %i", m_thread_create_bp_sp->GetID()); |
| 2935 | m_thread_create_bp_sp->SetCallback (ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2936 | } |
| 2937 | else |
| 2938 | { |
| 2939 | if (log) |
| 2940 | log->Printf("Failed to create new thread notification breakpoint."); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2941 | } |
| 2942 | } |
| 2943 | } |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2944 | return m_thread_create_bp_sp.get() != NULL; |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
| 2947 | bool |
| 2948 | ProcessGDBRemote::StopNoticingNewThreads() |
| 2949 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2950 | Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 37cfeab | 2011-10-15 00:21:37 +0000 | [diff] [blame] | 2951 | if (log && log->GetVerbose()) |
Jim Ingham | 0e97cbc | 2011-02-08 05:19:01 +0000 | [diff] [blame] | 2952 | log->Printf ("Disabling new thread notification breakpoint."); |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 2953 | |
| 2954 | if (m_thread_create_bp_sp) |
| 2955 | m_thread_create_bp_sp->SetEnabled(false); |
| 2956 | |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2957 | return true; |
| 2958 | } |
| 2959 | |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 2960 | lldb_private::DynamicLoader * |
| 2961 | ProcessGDBRemote::GetDynamicLoader () |
| 2962 | { |
| 2963 | if (m_dyld_ap.get() == NULL) |
Jason Molenda | 2e56a25 | 2013-05-11 03:09:05 +0000 | [diff] [blame] | 2964 | m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL)); |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 2965 | return m_dyld_ap.get(); |
| 2966 | } |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 2967 | |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 2968 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 2969 | class CommandObjectProcessGDBRemotePacketHistory : public CommandObjectParsed |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 2970 | { |
| 2971 | private: |
| 2972 | |
| 2973 | public: |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 2974 | CommandObjectProcessGDBRemotePacketHistory(CommandInterpreter &interpreter) : |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 2975 | CommandObjectParsed (interpreter, |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 2976 | "process plugin packet history", |
| 2977 | "Dumps the packet history buffer. ", |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 2978 | NULL) |
| 2979 | { |
| 2980 | } |
| 2981 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 2982 | ~CommandObjectProcessGDBRemotePacketHistory () |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 2983 | { |
| 2984 | } |
| 2985 | |
| 2986 | bool |
| 2987 | DoExecute (Args& command, CommandReturnObject &result) |
| 2988 | { |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 2989 | const size_t argc = command.GetArgumentCount(); |
| 2990 | if (argc == 0) |
| 2991 | { |
| 2992 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 2993 | if (process) |
| 2994 | { |
| 2995 | process->GetGDBRemote().DumpHistory(result.GetOutputStream()); |
| 2996 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 2997 | return true; |
| 2998 | } |
| 2999 | } |
| 3000 | else |
| 3001 | { |
| 3002 | result.AppendErrorWithFormat ("'%s' takes no arguments", m_cmd_name.c_str()); |
| 3003 | } |
| 3004 | result.SetStatus (eReturnStatusFailed); |
| 3005 | return false; |
| 3006 | } |
| 3007 | }; |
| 3008 | |
| 3009 | class CommandObjectProcessGDBRemotePacketSend : public CommandObjectParsed |
| 3010 | { |
| 3011 | private: |
| 3012 | |
| 3013 | public: |
| 3014 | CommandObjectProcessGDBRemotePacketSend(CommandInterpreter &interpreter) : |
| 3015 | CommandObjectParsed (interpreter, |
| 3016 | "process plugin packet send", |
| 3017 | "Send a custom packet through the GDB remote protocol and print the answer. " |
| 3018 | "The packet header and footer will automatically be added to the packet prior to sending and stripped from the result.", |
| 3019 | NULL) |
| 3020 | { |
| 3021 | } |
| 3022 | |
| 3023 | ~CommandObjectProcessGDBRemotePacketSend () |
| 3024 | { |
| 3025 | } |
| 3026 | |
| 3027 | bool |
| 3028 | DoExecute (Args& command, CommandReturnObject &result) |
| 3029 | { |
| 3030 | const size_t argc = command.GetArgumentCount(); |
| 3031 | if (argc == 0) |
| 3032 | { |
| 3033 | result.AppendErrorWithFormat ("'%s' takes a one or more packet content arguments", m_cmd_name.c_str()); |
| 3034 | result.SetStatus (eReturnStatusFailed); |
| 3035 | return false; |
| 3036 | } |
| 3037 | |
| 3038 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 3039 | if (process) |
| 3040 | { |
Han Ming Ong | 8414585 | 2012-11-26 20:42:03 +0000 | [diff] [blame] | 3041 | for (size_t i=0; i<argc; ++ i) |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3042 | { |
Han Ming Ong | 8414585 | 2012-11-26 20:42:03 +0000 | [diff] [blame] | 3043 | const char *packet_cstr = command.GetArgumentAtIndex(0); |
| 3044 | bool send_async = true; |
| 3045 | StringExtractorGDBRemote response; |
| 3046 | process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async); |
| 3047 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 3048 | Stream &output_strm = result.GetOutputStream(); |
| 3049 | output_strm.Printf (" packet: %s\n", packet_cstr); |
Han Ming Ong | 4b6459f | 2013-01-18 23:11:53 +0000 | [diff] [blame] | 3050 | std::string &response_str = response.GetStringRef(); |
| 3051 | |
Han Ming Ong | 399289e | 2013-06-21 19:56:59 +0000 | [diff] [blame] | 3052 | if (strstr(packet_cstr, "qGetProfileData") != NULL) |
Han Ming Ong | 4b6459f | 2013-01-18 23:11:53 +0000 | [diff] [blame] | 3053 | { |
| 3054 | response_str = process->GetGDBRemote().HarmonizeThreadIdsForProfileData(process, response); |
| 3055 | } |
| 3056 | |
Han Ming Ong | 8414585 | 2012-11-26 20:42:03 +0000 | [diff] [blame] | 3057 | if (response_str.empty()) |
| 3058 | output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n"); |
| 3059 | else |
| 3060 | output_strm.Printf ("response: %s\n", response.GetStringRef().c_str()); |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3061 | } |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3062 | } |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 3063 | return true; |
| 3064 | } |
| 3065 | }; |
| 3066 | |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 3067 | class CommandObjectProcessGDBRemotePacketMonitor : public CommandObjectRaw |
| 3068 | { |
| 3069 | private: |
| 3070 | |
| 3071 | public: |
| 3072 | CommandObjectProcessGDBRemotePacketMonitor(CommandInterpreter &interpreter) : |
| 3073 | CommandObjectRaw (interpreter, |
| 3074 | "process plugin packet monitor", |
Greg Clayton | eee5e98 | 2013-02-14 18:39:30 +0000 | [diff] [blame] | 3075 | "Send a qRcmd packet through the GDB remote protocol and print the response." |
| 3076 | "The argument passed to this command will be hex encoded into a valid 'qRcmd' packet, sent and the response will be printed.", |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 3077 | NULL) |
| 3078 | { |
| 3079 | } |
| 3080 | |
| 3081 | ~CommandObjectProcessGDBRemotePacketMonitor () |
| 3082 | { |
| 3083 | } |
| 3084 | |
| 3085 | bool |
| 3086 | DoExecute (const char *command, CommandReturnObject &result) |
| 3087 | { |
| 3088 | if (command == NULL || command[0] == '\0') |
| 3089 | { |
| 3090 | result.AppendErrorWithFormat ("'%s' takes a command string argument", m_cmd_name.c_str()); |
| 3091 | result.SetStatus (eReturnStatusFailed); |
| 3092 | return false; |
| 3093 | } |
| 3094 | |
| 3095 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 3096 | if (process) |
| 3097 | { |
| 3098 | StreamString packet; |
Greg Clayton | eee5e98 | 2013-02-14 18:39:30 +0000 | [diff] [blame] | 3099 | packet.PutCString("qRcmd,"); |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 3100 | packet.PutBytesAsRawHex8(command, strlen(command)); |
| 3101 | const char *packet_cstr = packet.GetString().c_str(); |
| 3102 | |
| 3103 | bool send_async = true; |
| 3104 | StringExtractorGDBRemote response; |
| 3105 | process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async); |
| 3106 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 3107 | Stream &output_strm = result.GetOutputStream(); |
| 3108 | output_strm.Printf (" packet: %s\n", packet_cstr); |
| 3109 | const std::string &response_str = response.GetStringRef(); |
| 3110 | |
| 3111 | if (response_str.empty()) |
| 3112 | output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n"); |
| 3113 | else |
| 3114 | output_strm.Printf ("response: %s\n", response.GetStringRef().c_str()); |
| 3115 | } |
| 3116 | return true; |
| 3117 | } |
| 3118 | }; |
| 3119 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3120 | class CommandObjectProcessGDBRemotePacket : public CommandObjectMultiword |
| 3121 | { |
| 3122 | private: |
| 3123 | |
| 3124 | public: |
| 3125 | CommandObjectProcessGDBRemotePacket(CommandInterpreter &interpreter) : |
| 3126 | CommandObjectMultiword (interpreter, |
| 3127 | "process plugin packet", |
| 3128 | "Commands that deal with GDB remote packets.", |
| 3129 | NULL) |
| 3130 | { |
| 3131 | LoadSubCommand ("history", CommandObjectSP (new CommandObjectProcessGDBRemotePacketHistory (interpreter))); |
| 3132 | LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessGDBRemotePacketSend (interpreter))); |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 3133 | LoadSubCommand ("monitor", CommandObjectSP (new CommandObjectProcessGDBRemotePacketMonitor (interpreter))); |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
| 3136 | ~CommandObjectProcessGDBRemotePacket () |
| 3137 | { |
| 3138 | } |
| 3139 | }; |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 3140 | |
| 3141 | class CommandObjectMultiwordProcessGDBRemote : public CommandObjectMultiword |
| 3142 | { |
| 3143 | public: |
| 3144 | CommandObjectMultiwordProcessGDBRemote (CommandInterpreter &interpreter) : |
| 3145 | CommandObjectMultiword (interpreter, |
| 3146 | "process plugin", |
| 3147 | "A set of commands for operating on a ProcessGDBRemote process.", |
| 3148 | "process plugin <subcommand> [<subcommand-options>]") |
| 3149 | { |
| 3150 | LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessGDBRemotePacket (interpreter))); |
| 3151 | } |
| 3152 | |
| 3153 | ~CommandObjectMultiwordProcessGDBRemote () |
| 3154 | { |
| 3155 | } |
| 3156 | }; |
| 3157 | |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 3158 | CommandObject * |
| 3159 | ProcessGDBRemote::GetPluginCommandObject() |
| 3160 | { |
| 3161 | if (!m_command_sp) |
| 3162 | m_command_sp.reset (new CommandObjectMultiwordProcessGDBRemote (GetTarget().GetDebugger().GetCommandInterpreter())); |
| 3163 | return m_command_sp.get(); |
| 3164 | } |