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 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 10 | #include "lldb/Host/Config.h" |
Daniel Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 11 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | // C Includes |
| 13 | #include <errno.h> |
Stephen Wilson | a78867b | 2011-03-25 18:16:28 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 15 | #ifndef LLDB_DISABLE_POSIX |
Sean Callanan | 224f6f5 | 2012-07-19 18:07:36 +0000 | [diff] [blame] | 16 | #include <netinet/in.h> |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 17 | #include <sys/mman.h> // for mmap |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 18 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include <sys/stat.h> |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 20 | #include <sys/types.h> |
Stephen Wilson | dc91686 | 2011-03-30 00:12:40 +0000 | [diff] [blame] | 21 | #include <time.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
| 23 | // C++ Includes |
| 24 | #include <algorithm> |
| 25 | #include <map> |
Benjamin Kramer | 3f69fa6 | 2015-04-03 10:55:00 +0000 | [diff] [blame] | 26 | #include <mutex> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 28 | #include "lldb/Breakpoint/Watchpoint.h" |
Jim Ingham | 40af72e | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 29 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | #include "lldb/Core/ArchSpec.h" |
| 31 | #include "lldb/Core/Debugger.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 32 | #include "lldb/Host/ConnectionFileDescriptor.h" |
Greg Clayton | 53239f0 | 2011-02-08 05:05:52 +0000 | [diff] [blame] | 33 | #include "lldb/Host/FileSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | #include "lldb/Core/Module.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 35 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | #include "lldb/Core/PluginManager.h" |
| 37 | #include "lldb/Core/State.h" |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 38 | #include "lldb/Core/StreamFile.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | #include "lldb/Core/StreamString.h" |
| 40 | #include "lldb/Core/Timer.h" |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 41 | #include "lldb/Core/Value.h" |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 42 | #include "lldb/DataFormatters/FormatManager.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 43 | #include "lldb/Host/HostThread.h" |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 44 | #include "lldb/Host/StringConvert.h" |
Jason Molenda | d1fae14 | 2012-09-29 08:03:33 +0000 | [diff] [blame] | 45 | #include "lldb/Host/Symbols.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 46 | #include "lldb/Host/ThreadLauncher.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | #include "lldb/Host/TimeValue.h" |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 48 | #include "lldb/Host/XML.h" |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 49 | #include "lldb/Interpreter/CommandInterpreter.h" |
Greg Clayton | 1d19a2f | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 50 | #include "lldb/Interpreter/CommandObject.h" |
| 51 | #include "lldb/Interpreter/CommandObjectMultiword.h" |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 52 | #include "lldb/Interpreter/CommandReturnObject.h" |
Zachary Turner | 633a29c | 2015-03-04 01:58:01 +0000 | [diff] [blame] | 53 | #include "lldb/Interpreter/OptionValueProperties.h" |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 54 | #include "lldb/Interpreter/Options.h" |
| 55 | #include "lldb/Interpreter/OptionGroupBoolean.h" |
| 56 | #include "lldb/Interpreter/OptionGroupUInt64.h" |
Zachary Turner | 633a29c | 2015-03-04 01:58:01 +0000 | [diff] [blame] | 57 | #include "lldb/Interpreter/Property.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | #include "lldb/Symbol/ObjectFile.h" |
| 59 | #include "lldb/Target/DynamicLoader.h" |
| 60 | #include "lldb/Target/Target.h" |
| 61 | #include "lldb/Target/TargetList.h" |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 62 | #include "lldb/Target/ThreadPlanCallFunction.h" |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 63 | #include "lldb/Target/SystemRuntime.h" |
Jason Molenda | a34a0c6 | 2010-06-09 21:28:42 +0000 | [diff] [blame] | 64 | #include "lldb/Utility/PseudoTerminal.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 65 | |
| 66 | // Project includes |
| 67 | #include "lldb/Host/Host.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 68 | #include "Plugins/Process/Utility/FreeBSDSignals.h" |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 69 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 70 | #include "Plugins/Process/Utility/LinuxSignals.h" |
Mohit K. Bhakkad | 2c2acf9 | 2015-04-09 07:12:15 +0000 | [diff] [blame] | 71 | #include "Plugins/Process/Utility/MipsLinuxSignals.h" |
Jason Molenda | c42d243 | 2012-07-25 03:40:06 +0000 | [diff] [blame] | 72 | #include "Plugins/Process/Utility/StopInfoMachException.h" |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 73 | #include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h" |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 74 | #include "Utility/StringExtractorGDBRemote.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 75 | #include "GDBRemoteRegisterContext.h" |
| 76 | #include "ProcessGDBRemote.h" |
| 77 | #include "ProcessGDBRemoteLog.h" |
| 78 | #include "ThreadGDBRemote.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 79 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 80 | #define DEBUGSERVER_BASENAME "debugserver" |
| 81 | using namespace lldb; |
| 82 | using namespace lldb_private; |
| 83 | using namespace lldb_private::process_gdb_remote; |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 84 | |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 85 | namespace lldb |
| 86 | { |
| 87 | // Provide a function that can easily dump the packet history if we know a |
| 88 | // ProcessGDBRemote * value (which we can get from logs or from debugging). |
| 89 | // We need the function in the lldb namespace so it makes it into the final |
| 90 | // executable since the LLDB shared library only exports stuff in the lldb |
| 91 | // namespace. This allows you to attach with a debugger and call this |
| 92 | // function and get the packet history dumped to a file. |
| 93 | void |
| 94 | DumpProcessGDBRemotePacketHistory (void *p, const char *path) |
| 95 | { |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 96 | StreamFile strm; |
| 97 | Error error (strm.GetFile().Open(path, File::eOpenOptionWrite | File::eOpenOptionCanCreate)); |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 98 | if (error.Success()) |
| 99 | ((ProcessGDBRemote *)p)->GetGDBRemote().DumpHistory (strm); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 100 | } |
Filipe Cabecinhas | c34f776 | 2012-05-23 16:27:09 +0000 | [diff] [blame] | 101 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 103 | namespace { |
Steve Pucci | 5ec012d | 2014-01-16 22:18:14 +0000 | [diff] [blame] | 104 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 105 | static PropertyDefinition |
| 106 | g_properties[] = |
| 107 | { |
| 108 | { "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] | 109 | { "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] | 110 | { NULL , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL } |
| 111 | }; |
| 112 | |
| 113 | enum |
| 114 | { |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 115 | ePropertyPacketTimeout, |
| 116 | ePropertyTargetDefinitionFile |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | class PluginProperties : public Properties |
| 120 | { |
| 121 | public: |
| 122 | |
| 123 | static ConstString |
| 124 | GetSettingName () |
| 125 | { |
| 126 | return ProcessGDBRemote::GetPluginNameStatic(); |
| 127 | } |
| 128 | |
| 129 | PluginProperties() : |
| 130 | Properties () |
| 131 | { |
| 132 | m_collection_sp.reset (new OptionValueProperties(GetSettingName())); |
| 133 | m_collection_sp->Initialize(g_properties); |
| 134 | } |
| 135 | |
| 136 | virtual |
| 137 | ~PluginProperties() |
| 138 | { |
| 139 | } |
| 140 | |
| 141 | uint64_t |
| 142 | GetPacketTimeout() |
| 143 | { |
| 144 | const uint32_t idx = ePropertyPacketTimeout; |
| 145 | return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value); |
| 146 | } |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 147 | |
| 148 | bool |
| 149 | SetPacketTimeout(uint64_t timeout) |
| 150 | { |
| 151 | const uint32_t idx = ePropertyPacketTimeout; |
| 152 | return m_collection_sp->SetPropertyAtIndexAsUInt64(NULL, idx, timeout); |
| 153 | } |
| 154 | |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 155 | FileSpec |
| 156 | GetTargetDefinitionFile () const |
| 157 | { |
| 158 | const uint32_t idx = ePropertyTargetDefinitionFile; |
| 159 | return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx); |
| 160 | } |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP; |
| 164 | |
| 165 | static const ProcessKDPPropertiesSP & |
| 166 | GetGlobalPluginProperties() |
| 167 | { |
| 168 | static ProcessKDPPropertiesSP g_settings_sp; |
| 169 | if (!g_settings_sp) |
| 170 | g_settings_sp.reset (new PluginProperties ()); |
| 171 | return g_settings_sp; |
| 172 | } |
| 173 | |
| 174 | } // anonymous namespace end |
| 175 | |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 176 | class ProcessGDBRemote::GDBLoadedModuleInfoList |
| 177 | { |
| 178 | public: |
| 179 | |
| 180 | class LoadedModuleInfo |
| 181 | { |
| 182 | public: |
| 183 | |
| 184 | enum e_data_point |
| 185 | { |
| 186 | e_has_name = 0, |
| 187 | e_has_base , |
| 188 | e_has_dynamic , |
| 189 | e_has_link_map , |
| 190 | e_num |
| 191 | }; |
| 192 | |
| 193 | LoadedModuleInfo () |
| 194 | { |
| 195 | for (uint32_t i = 0; i < e_num; ++i) |
| 196 | m_has[i] = false; |
| 197 | }; |
| 198 | |
| 199 | void set_name (const std::string & name) |
| 200 | { |
| 201 | m_name = name; |
| 202 | m_has[e_has_name] = true; |
| 203 | } |
| 204 | bool get_name (std::string & out) const |
| 205 | { |
| 206 | out = m_name; |
| 207 | return m_has[e_has_name]; |
| 208 | } |
| 209 | |
| 210 | void set_base (const lldb::addr_t base) |
| 211 | { |
| 212 | m_base = base; |
| 213 | m_has[e_has_base] = true; |
| 214 | } |
| 215 | bool get_base (lldb::addr_t & out) const |
| 216 | { |
| 217 | out = m_base; |
| 218 | return m_has[e_has_base]; |
| 219 | } |
| 220 | |
| 221 | void set_link_map (const lldb::addr_t addr) |
| 222 | { |
| 223 | m_link_map = addr; |
| 224 | m_has[e_has_link_map] = true; |
| 225 | } |
| 226 | bool get_link_map (lldb::addr_t & out) const |
| 227 | { |
| 228 | out = m_link_map; |
| 229 | return m_has[e_has_link_map]; |
| 230 | } |
| 231 | |
| 232 | void set_dynamic (const lldb::addr_t addr) |
| 233 | { |
| 234 | m_dynamic = addr; |
| 235 | m_has[e_has_dynamic] = true; |
| 236 | } |
| 237 | bool get_dynamic (lldb::addr_t & out) const |
| 238 | { |
| 239 | out = m_dynamic; |
| 240 | return m_has[e_has_dynamic]; |
| 241 | } |
| 242 | |
| 243 | bool has_info (e_data_point datum) |
| 244 | { |
| 245 | assert (datum < e_num); |
| 246 | return m_has[datum]; |
| 247 | } |
| 248 | |
| 249 | protected: |
| 250 | |
| 251 | bool m_has[e_num]; |
| 252 | std::string m_name; |
| 253 | lldb::addr_t m_link_map; |
| 254 | lldb::addr_t m_base; |
| 255 | lldb::addr_t m_dynamic; |
| 256 | }; |
| 257 | |
| 258 | GDBLoadedModuleInfoList () |
| 259 | : m_list () |
| 260 | , m_link_map (LLDB_INVALID_ADDRESS) |
| 261 | {} |
| 262 | |
| 263 | void add (const LoadedModuleInfo & mod) |
| 264 | { |
| 265 | m_list.push_back (mod); |
| 266 | } |
| 267 | |
| 268 | void clear () |
| 269 | { |
| 270 | m_list.clear (); |
| 271 | } |
| 272 | |
| 273 | std::vector<LoadedModuleInfo> m_list; |
| 274 | lldb::addr_t m_link_map; |
| 275 | }; |
| 276 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 277 | // TODO Randomly assigning a port is unsafe. We should get an unused |
| 278 | // ephemeral port from the kernel and make sure we reserve it before passing |
| 279 | // it to debugserver. |
| 280 | |
| 281 | #if defined (__APPLE__) |
| 282 | #define LOW_PORT (IPPORT_RESERVED) |
| 283 | #define HIGH_PORT (IPPORT_HIFIRSTAUTO) |
| 284 | #else |
| 285 | #define LOW_PORT (1024u) |
| 286 | #define HIGH_PORT (49151u) |
| 287 | #endif |
| 288 | |
Todd Fiala | 013434e | 2014-07-09 01:29:05 +0000 | [diff] [blame] | 289 | #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__) || defined(__aarch64__)) |
Saleem Abdulrasool | a68f7b6 | 2014-03-20 06:08:36 +0000 | [diff] [blame] | 290 | static bool rand_initialized = false; |
| 291 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 292 | static inline uint16_t |
| 293 | get_random_port () |
| 294 | { |
| 295 | if (!rand_initialized) |
| 296 | { |
| 297 | time_t seed = time(NULL); |
Saleem Abdulrasool | a68f7b6 | 2014-03-20 06:08:36 +0000 | [diff] [blame] | 298 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 299 | rand_initialized = true; |
| 300 | srand(seed); |
| 301 | } |
| 302 | return (rand() % (HIGH_PORT - LOW_PORT)) + LOW_PORT; |
| 303 | } |
Saleem Abdulrasool | a68f7b6 | 2014-03-20 06:08:36 +0000 | [diff] [blame] | 304 | #endif |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 305 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 306 | ConstString |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 307 | ProcessGDBRemote::GetPluginNameStatic() |
| 308 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 309 | static ConstString g_name("gdb-remote"); |
| 310 | return g_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | const char * |
| 314 | ProcessGDBRemote::GetPluginDescriptionStatic() |
| 315 | { |
| 316 | return "GDB Remote protocol based debugging plug-in."; |
| 317 | } |
| 318 | |
| 319 | void |
| 320 | ProcessGDBRemote::Terminate() |
| 321 | { |
| 322 | PluginManager::UnregisterPlugin (ProcessGDBRemote::CreateInstance); |
| 323 | } |
| 324 | |
| 325 | |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 326 | lldb::ProcessSP |
| 327 | ProcessGDBRemote::CreateInstance (Target &target, Listener &listener, const FileSpec *crash_file_path) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 328 | { |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 329 | lldb::ProcessSP process_sp; |
| 330 | if (crash_file_path == NULL) |
| 331 | process_sp.reset (new ProcessGDBRemote (target, listener)); |
| 332 | return process_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | bool |
Greg Clayton | 3a29bdb | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 336 | ProcessGDBRemote::CanDebug (Target &target, bool plugin_specified_by_name) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 337 | { |
Greg Clayton | 596ed24 | 2011-10-21 21:41:45 +0000 | [diff] [blame] | 338 | if (plugin_specified_by_name) |
| 339 | return true; |
| 340 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 341 | // 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] | 342 | Module *exe_module = target.GetExecutableModulePointer(); |
| 343 | if (exe_module) |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 344 | { |
| 345 | ObjectFile *exe_objfile = exe_module->GetObjectFile(); |
| 346 | // We can't debug core files... |
| 347 | switch (exe_objfile->GetType()) |
| 348 | { |
| 349 | case ObjectFile::eTypeInvalid: |
| 350 | case ObjectFile::eTypeCoreFile: |
| 351 | case ObjectFile::eTypeDebugInfo: |
| 352 | case ObjectFile::eTypeObjectFile: |
| 353 | case ObjectFile::eTypeSharedLibrary: |
| 354 | case ObjectFile::eTypeStubLibrary: |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 355 | case ObjectFile::eTypeJIT: |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 356 | return false; |
| 357 | case ObjectFile::eTypeExecutable: |
| 358 | case ObjectFile::eTypeDynamicLinker: |
| 359 | case ObjectFile::eTypeUnknown: |
| 360 | break; |
| 361 | } |
Greg Clayton | aa149cb | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 362 | return exe_module->GetFileSpec().Exists(); |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 363 | } |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 364 | // However, if there is no executable module, we return true since we might be preparing to attach. |
| 365 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | //---------------------------------------------------------------------- |
| 369 | // ProcessGDBRemote constructor |
| 370 | //---------------------------------------------------------------------- |
| 371 | ProcessGDBRemote::ProcessGDBRemote(Target& target, Listener &listener) : |
| 372 | Process (target, listener), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 373 | m_flags (0), |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 374 | m_gdb_comm (), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 375 | m_debugserver_pid (LLDB_INVALID_PROCESS_ID), |
Greg Clayton | 09c3e3d | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 376 | m_last_stop_packet_mutex (Mutex::eMutexTypeNormal), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 377 | m_register_info (), |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 378 | m_async_broadcaster (NULL, "lldb.process.gdb-remote.async-broadcaster"), |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 379 | m_async_thread_state_mutex(Mutex::eMutexTypeRecursive), |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 380 | m_thread_ids (), |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 381 | m_continue_c_tids (), |
| 382 | m_continue_C_tids (), |
| 383 | m_continue_s_tids (), |
| 384 | m_continue_S_tids (), |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 385 | m_max_memory_size (0), |
| 386 | m_remote_stub_max_memory_size (0), |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 387 | m_addr_to_mmap_size (), |
| 388 | m_thread_create_bp_sp (), |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 389 | m_waiting_for_attach (false), |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 390 | m_destroy_tried_resuming (false), |
Hafiz Abid Qadeer | 85a4daf | 2013-10-18 10:04:33 +0000 | [diff] [blame] | 391 | m_command_sp (), |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 392 | m_breakpoint_pc_offset (0), |
| 393 | m_initial_tid (LLDB_INVALID_THREAD_ID) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 394 | { |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 395 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit"); |
| 396 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue"); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 397 | m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadDidExit, "async thread did exit"); |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 398 | const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout(); |
| 399 | if (timeout_seconds > 0) |
| 400 | m_gdb_comm.SetPacketTimeout(timeout_seconds); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | //---------------------------------------------------------------------- |
| 404 | // Destructor |
| 405 | //---------------------------------------------------------------------- |
| 406 | ProcessGDBRemote::~ProcessGDBRemote() |
| 407 | { |
| 408 | // m_mach_process.UnregisterNotificationCallbacks (this); |
| 409 | Clear(); |
Greg Clayton | 1ed54f5 | 2011-10-01 00:45:15 +0000 | [diff] [blame] | 410 | // We need to call finalize on the process before destroying ourselves |
| 411 | // to make sure all of the broadcaster cleanup goes as planned. If we |
| 412 | // destruct this class, then Process::~Process() might have problems |
| 413 | // trying to fully destroy the broadcaster. |
| 414 | Finalize(); |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 415 | |
| 416 | // The general Finalize is going to try to destroy the process and that SHOULD |
| 417 | // shut down the async thread. However, if we don't kill it it will get stranded and |
| 418 | // its connection will go away so when it wakes up it will crash. So kill it for sure here. |
| 419 | StopAsyncThread(); |
| 420 | KillDebugserverProcess(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | //---------------------------------------------------------------------- |
| 424 | // PluginInterface |
| 425 | //---------------------------------------------------------------------- |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 426 | ConstString |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 427 | ProcessGDBRemote::GetPluginName() |
| 428 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 429 | return GetPluginNameStatic(); |
| 430 | } |
| 431 | |
| 432 | uint32_t |
| 433 | ProcessGDBRemote::GetPluginVersion() |
| 434 | { |
| 435 | return 1; |
| 436 | } |
| 437 | |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 438 | bool |
| 439 | ProcessGDBRemote::ParsePythonTargetDefinition(const FileSpec &target_definition_fspec) |
| 440 | { |
| 441 | ScriptInterpreter *interpreter = GetTarget().GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); |
| 442 | Error error; |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 443 | StructuredData::ObjectSP module_object_sp(interpreter->LoadPluginModule(target_definition_fspec, error)); |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 444 | if (module_object_sp) |
| 445 | { |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 446 | StructuredData::DictionarySP target_definition_sp( |
| 447 | interpreter->GetDynamicSettings(module_object_sp, &GetTarget(), "gdb-server-target-definition", error)); |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 448 | |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 449 | if (target_definition_sp) |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 450 | { |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 451 | StructuredData::ObjectSP target_object(target_definition_sp->GetValueForKey("host-info")); |
| 452 | if (target_object) |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 453 | { |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 454 | if (auto host_info_dict = target_object->GetAsDictionary()) |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 455 | { |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 456 | StructuredData::ObjectSP triple_value = host_info_dict->GetValueForKey("triple"); |
| 457 | if (auto triple_string_value = triple_value->GetAsString()) |
| 458 | { |
| 459 | std::string triple_string = triple_string_value->GetValue(); |
| 460 | ArchSpec host_arch(triple_string.c_str()); |
| 461 | if (!host_arch.IsCompatibleMatch(GetTarget().GetArchitecture())) |
| 462 | { |
| 463 | GetTarget().SetArchitecture(host_arch); |
| 464 | } |
| 465 | } |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 466 | } |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 467 | } |
Zachary Turner | 0641ca1 | 2015-03-17 20:04:04 +0000 | [diff] [blame] | 468 | m_breakpoint_pc_offset = 0; |
| 469 | StructuredData::ObjectSP breakpoint_pc_offset_value = target_definition_sp->GetValueForKey("breakpoint-pc-offset"); |
| 470 | if (breakpoint_pc_offset_value) |
| 471 | { |
| 472 | if (auto breakpoint_pc_int_value = breakpoint_pc_offset_value->GetAsInteger()) |
| 473 | m_breakpoint_pc_offset = breakpoint_pc_int_value->GetValue(); |
| 474 | } |
Hafiz Abid Qadeer | 85a4daf | 2013-10-18 10:04:33 +0000 | [diff] [blame] | 475 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 476 | if (m_register_info.SetRegisterInfo(*target_definition_sp, GetTarget().GetArchitecture()) > 0) |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 477 | { |
| 478 | return true; |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | return false; |
| 483 | } |
| 484 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 485 | static size_t |
| 486 | SplitCommaSeparatedRegisterNumberString(const llvm::StringRef &comma_separated_regiter_numbers, std::vector<uint32_t> ®nums, int base) |
| 487 | { |
| 488 | regnums.clear(); |
| 489 | std::pair<llvm::StringRef, llvm::StringRef> value_pair; |
| 490 | value_pair.second = comma_separated_regiter_numbers; |
| 491 | do |
| 492 | { |
| 493 | value_pair = value_pair.second.split(','); |
| 494 | if (!value_pair.first.empty()) |
| 495 | { |
| 496 | uint32_t reg = StringConvert::ToUInt32 (value_pair.first.str().c_str(), LLDB_INVALID_REGNUM, base); |
| 497 | if (reg != LLDB_INVALID_REGNUM) |
| 498 | regnums.push_back (reg); |
| 499 | } |
| 500 | } while (!value_pair.second.empty()); |
| 501 | return regnums.size(); |
| 502 | } |
| 503 | |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 504 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 505 | void |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 506 | ProcessGDBRemote::BuildDynamicRegisterInfo (bool force) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 507 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 508 | if (!force && m_register_info.GetNumRegisters() > 0) |
| 509 | return; |
| 510 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 511 | m_register_info.Clear(); |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 512 | |
| 513 | // Check if qHostInfo specified a specific packet timeout for this connection. |
| 514 | // If so then lets update our setting so the user knows what the timeout is |
| 515 | // and can see it. |
| 516 | const uint32_t host_packet_timeout = m_gdb_comm.GetHostDefaultPacketTimeout(); |
| 517 | if (host_packet_timeout) |
| 518 | { |
| 519 | GetGlobalPluginProperties()->SetPacketTimeout(host_packet_timeout); |
| 520 | } |
| 521 | |
| 522 | if (GetGDBServerRegisterInfo ()) |
| 523 | return; |
| 524 | |
| 525 | char packet[128]; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 526 | uint32_t reg_offset = 0; |
| 527 | uint32_t reg_num = 0; |
Greg Clayton | 23f5950 | 2012-07-17 03:23:13 +0000 | [diff] [blame] | 528 | for (StringExtractorGDBRemote::ResponseType response_type = StringExtractorGDBRemote::eResponse; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 529 | response_type == StringExtractorGDBRemote::eResponse; |
| 530 | ++reg_num) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 531 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 532 | const int packet_len = ::snprintf (packet, sizeof(packet), "qRegisterInfo%x", reg_num); |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 533 | assert (packet_len < (int)sizeof(packet)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 534 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 535 | 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] | 536 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 537 | response_type = response.GetResponseType(); |
| 538 | if (response_type == StringExtractorGDBRemote::eResponse) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 539 | { |
| 540 | std::string name; |
| 541 | std::string value; |
| 542 | ConstString reg_name; |
| 543 | ConstString alt_name; |
| 544 | ConstString set_name; |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 545 | std::vector<uint32_t> value_regs; |
| 546 | std::vector<uint32_t> invalidate_regs; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 547 | RegisterInfo reg_info = { NULL, // Name |
| 548 | NULL, // Alt name |
| 549 | 0, // byte size |
| 550 | reg_offset, // offset |
| 551 | eEncodingUint, // encoding |
| 552 | eFormatHex, // formate |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 553 | { |
| 554 | LLDB_INVALID_REGNUM, // GCC reg num |
| 555 | LLDB_INVALID_REGNUM, // DWARF reg num |
| 556 | LLDB_INVALID_REGNUM, // generic reg num |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 557 | reg_num, // GDB reg num |
| 558 | reg_num // native register number |
Greg Clayton | 435d85a | 2012-02-29 19:27:27 +0000 | [diff] [blame] | 559 | }, |
| 560 | NULL, |
| 561 | NULL |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 562 | }; |
| 563 | |
| 564 | while (response.GetNameColonValue(name, value)) |
| 565 | { |
| 566 | if (name.compare("name") == 0) |
| 567 | { |
| 568 | reg_name.SetCString(value.c_str()); |
| 569 | } |
| 570 | else if (name.compare("alt-name") == 0) |
| 571 | { |
| 572 | alt_name.SetCString(value.c_str()); |
| 573 | } |
| 574 | else if (name.compare("bitsize") == 0) |
| 575 | { |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 576 | reg_info.byte_size = StringConvert::ToUInt32(value.c_str(), 0, 0) / CHAR_BIT; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 577 | } |
| 578 | else if (name.compare("offset") == 0) |
| 579 | { |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 580 | uint32_t offset = StringConvert::ToUInt32(value.c_str(), UINT32_MAX, 0); |
Jason Molenda | 743e86a | 2010-06-11 23:44:18 +0000 | [diff] [blame] | 581 | if (reg_offset != offset) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 582 | { |
| 583 | reg_offset = offset; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | else if (name.compare("encoding") == 0) |
| 587 | { |
Greg Clayton | 2443cbd | 2012-08-24 01:42:50 +0000 | [diff] [blame] | 588 | const Encoding encoding = Args::StringToEncoding (value.c_str()); |
| 589 | if (encoding != eEncodingInvalid) |
| 590 | reg_info.encoding = encoding; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 591 | } |
| 592 | else if (name.compare("format") == 0) |
| 593 | { |
Greg Clayton | 2443cbd | 2012-08-24 01:42:50 +0000 | [diff] [blame] | 594 | Format format = eFormatInvalid; |
| 595 | if (Args::StringToFormat (value.c_str(), format, NULL).Success()) |
| 596 | reg_info.format = format; |
| 597 | else if (value.compare("binary") == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 598 | reg_info.format = eFormatBinary; |
| 599 | else if (value.compare("decimal") == 0) |
| 600 | reg_info.format = eFormatDecimal; |
| 601 | else if (value.compare("hex") == 0) |
| 602 | reg_info.format = eFormatHex; |
| 603 | else if (value.compare("float") == 0) |
| 604 | reg_info.format = eFormatFloat; |
| 605 | else if (value.compare("vector-sint8") == 0) |
| 606 | reg_info.format = eFormatVectorOfSInt8; |
| 607 | else if (value.compare("vector-uint8") == 0) |
| 608 | reg_info.format = eFormatVectorOfUInt8; |
| 609 | else if (value.compare("vector-sint16") == 0) |
| 610 | reg_info.format = eFormatVectorOfSInt16; |
| 611 | else if (value.compare("vector-uint16") == 0) |
| 612 | reg_info.format = eFormatVectorOfUInt16; |
| 613 | else if (value.compare("vector-sint32") == 0) |
| 614 | reg_info.format = eFormatVectorOfSInt32; |
| 615 | else if (value.compare("vector-uint32") == 0) |
| 616 | reg_info.format = eFormatVectorOfUInt32; |
| 617 | else if (value.compare("vector-float32") == 0) |
| 618 | reg_info.format = eFormatVectorOfFloat32; |
| 619 | else if (value.compare("vector-uint128") == 0) |
| 620 | reg_info.format = eFormatVectorOfUInt128; |
| 621 | } |
| 622 | else if (name.compare("set") == 0) |
| 623 | { |
| 624 | set_name.SetCString(value.c_str()); |
| 625 | } |
| 626 | else if (name.compare("gcc") == 0) |
| 627 | { |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 628 | reg_info.kinds[eRegisterKindGCC] = StringConvert::ToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 629 | } |
| 630 | else if (name.compare("dwarf") == 0) |
| 631 | { |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 632 | reg_info.kinds[eRegisterKindDWARF] = StringConvert::ToUInt32(value.c_str(), LLDB_INVALID_REGNUM, 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 633 | } |
| 634 | else if (name.compare("generic") == 0) |
| 635 | { |
Greg Clayton | 2443cbd | 2012-08-24 01:42:50 +0000 | [diff] [blame] | 636 | reg_info.kinds[eRegisterKindGeneric] = Args::StringToGenericRegister (value.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 637 | } |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 638 | else if (name.compare("container-regs") == 0) |
| 639 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 640 | SplitCommaSeparatedRegisterNumberString(value, value_regs, 16); |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 641 | } |
| 642 | else if (name.compare("invalidate-regs") == 0) |
| 643 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 644 | SplitCommaSeparatedRegisterNumberString(value, invalidate_regs, 16); |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 645 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Jason Molenda | 743e86a | 2010-06-11 23:44:18 +0000 | [diff] [blame] | 648 | reg_info.byte_offset = reg_offset; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 649 | assert (reg_info.byte_size != 0); |
| 650 | reg_offset += reg_info.byte_size; |
Greg Clayton | ce1ffcf | 2013-01-21 22:17:50 +0000 | [diff] [blame] | 651 | if (!value_regs.empty()) |
| 652 | { |
| 653 | value_regs.push_back(LLDB_INVALID_REGNUM); |
| 654 | reg_info.value_regs = value_regs.data(); |
| 655 | } |
| 656 | if (!invalidate_regs.empty()) |
| 657 | { |
| 658 | invalidate_regs.push_back(LLDB_INVALID_REGNUM); |
| 659 | reg_info.invalidate_regs = invalidate_regs.data(); |
| 660 | } |
| 661 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 662 | m_register_info.AddRegister(reg_info, reg_name, alt_name, set_name); |
| 663 | } |
Todd Fiala | 1a63440 | 2014-01-08 07:52:40 +0000 | [diff] [blame] | 664 | else |
| 665 | { |
| 666 | break; // ensure exit before reg_num is incremented |
| 667 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 668 | } |
| 669 | else |
| 670 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 671 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 675 | if (m_register_info.GetNumRegisters() > 0) |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 676 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 677 | m_register_info.Finalize(GetTarget().GetArchitecture()); |
| 678 | return; |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 679 | } |
Greg Clayton | 9ac6d2d | 2013-10-25 18:13:17 +0000 | [diff] [blame] | 680 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 681 | FileSpec target_definition_fspec = GetGlobalPluginProperties()->GetTargetDefinitionFile (); |
Greg Clayton | 312bcbe | 2013-10-17 01:10:23 +0000 | [diff] [blame] | 682 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 683 | if (target_definition_fspec) |
| 684 | { |
| 685 | // See if we can get register definitions from a python file |
| 686 | if (ParsePythonTargetDefinition (target_definition_fspec)) |
| 687 | return; |
Greg Clayton | ef8180a | 2013-10-15 00:14:28 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 690 | // We didn't get anything if the accumulated reg_num is zero. See if we are |
| 691 | // debugging ARM and fill with a hard coded register set until we can get an |
| 692 | // updated debugserver down on the devices. |
| 693 | // On the other hand, if the accumulated reg_num is positive, see if we can |
| 694 | // add composite registers to the existing primordial ones. |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 695 | bool from_scratch = (m_register_info.GetNumRegisters() == 0); |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 696 | |
| 697 | const ArchSpec &target_arch = GetTarget().GetArchitecture(); |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 698 | const ArchSpec &remote_host_arch = m_gdb_comm.GetHostArchitecture(); |
| 699 | const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture(); |
| 700 | |
| 701 | // Use the process' architecture instead of the host arch, if available |
| 702 | ArchSpec remote_arch; |
| 703 | if (remote_process_arch.IsValid ()) |
| 704 | remote_arch = remote_process_arch; |
| 705 | else |
| 706 | remote_arch = remote_host_arch; |
| 707 | |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 708 | if (!target_arch.IsValid()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 709 | { |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 710 | if (remote_arch.IsValid() |
| 711 | && remote_arch.GetMachine() == llvm::Triple::arm |
| 712 | && remote_arch.GetTriple().getVendor() == llvm::Triple::Apple) |
| 713 | m_register_info.HardcodeARMRegisters(from_scratch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 714 | } |
Johnny Chen | 2fa9de1 | 2012-05-14 18:44:23 +0000 | [diff] [blame] | 715 | else if (target_arch.GetMachine() == llvm::Triple::arm) |
| 716 | { |
| 717 | m_register_info.HardcodeARMRegisters(from_scratch); |
| 718 | } |
| 719 | |
| 720 | // At this point, we can finalize our register info. |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 721 | m_register_info.Finalize (GetTarget().GetArchitecture()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | Error |
| 725 | ProcessGDBRemote::WillLaunch (Module* module) |
| 726 | { |
| 727 | return WillLaunchOrAttach (); |
| 728 | } |
| 729 | |
| 730 | Error |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 731 | ProcessGDBRemote::WillAttachToProcessWithID (lldb::pid_t pid) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 732 | { |
| 733 | return WillLaunchOrAttach (); |
| 734 | } |
| 735 | |
| 736 | Error |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 737 | ProcessGDBRemote::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 738 | { |
| 739 | return WillLaunchOrAttach (); |
| 740 | } |
| 741 | |
| 742 | Error |
Jason Molenda | 4bd4e7e | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 743 | ProcessGDBRemote::DoConnectRemote (Stream *strm, const char *remote_url) |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 744 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 745 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 746 | Error error (WillLaunchOrAttach ()); |
| 747 | |
| 748 | if (error.Fail()) |
| 749 | return error; |
| 750 | |
Greg Clayton | 2289fa4 | 2011-04-30 01:09:13 +0000 | [diff] [blame] | 751 | error = ConnectToDebugserver (remote_url); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 752 | |
| 753 | if (error.Fail()) |
| 754 | return error; |
| 755 | StartAsyncThread (); |
| 756 | |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 757 | lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID (); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 758 | if (pid == LLDB_INVALID_PROCESS_ID) |
| 759 | { |
| 760 | // We don't have a valid process ID, so note that we are connected |
| 761 | // and could now request to launch or attach, or get remote process |
| 762 | // listings... |
| 763 | SetPrivateState (eStateConnected); |
| 764 | } |
| 765 | else |
| 766 | { |
| 767 | // We have a valid process |
| 768 | SetID (pid); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 769 | GetThreadList(); |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 770 | StringExtractorGDBRemote response; |
| 771 | if (m_gdb_comm.GetStopReply(response)) |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 772 | { |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 773 | SetLastStopPacket(response); |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 774 | |
| 775 | // '?' Packets must be handled differently in non-stop mode |
| 776 | if (GetTarget().GetNonStopModeEnabled()) |
| 777 | HandleStopReplySequence(); |
| 778 | |
Jason Molenda | c62bd7b | 2013-12-21 05:20:36 +0000 | [diff] [blame] | 779 | if (!m_target.GetArchitecture().IsValid()) |
| 780 | { |
| 781 | if (m_gdb_comm.GetProcessArchitecture().IsValid()) |
| 782 | { |
| 783 | m_target.SetArchitecture(m_gdb_comm.GetProcessArchitecture()); |
| 784 | } |
| 785 | else |
| 786 | { |
| 787 | m_target.SetArchitecture(m_gdb_comm.GetHostArchitecture()); |
| 788 | } |
Carlo Kok | 7438912 | 2013-10-14 07:09:13 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 791 | const StateType state = SetThreadStopInfo (response); |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 792 | if (state == eStateStopped) |
| 793 | { |
| 794 | SetPrivateState (state); |
| 795 | } |
| 796 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 797 | 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] | 798 | } |
| 799 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 800 | 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] | 801 | } |
Jason Molenda | 16d127c | 2012-05-03 22:37:30 +0000 | [diff] [blame] | 802 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 803 | if (log) |
| 804 | log->Printf ("ProcessGDBRemote::%s pid %" PRIu64 ": normalizing target architecture initial triple: %s (GetTarget().GetArchitecture().IsValid() %s, m_gdb_comm.GetHostArchitecture().IsValid(): %s)", __FUNCTION__, GetID (), GetTarget ().GetArchitecture ().GetTriple ().getTriple ().c_str (), GetTarget ().GetArchitecture ().IsValid () ? "true" : "false", m_gdb_comm.GetHostArchitecture ().IsValid () ? "true" : "false"); |
| 805 | |
| 806 | |
| 807 | if (error.Success() |
Jason Molenda | 16d127c | 2012-05-03 22:37:30 +0000 | [diff] [blame] | 808 | && !GetTarget().GetArchitecture().IsValid() |
| 809 | && m_gdb_comm.GetHostArchitecture().IsValid()) |
| 810 | { |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 811 | // Prefer the *process'* architecture over that of the *host*, if available. |
| 812 | if (m_gdb_comm.GetProcessArchitecture().IsValid()) |
| 813 | GetTarget().SetArchitecture(m_gdb_comm.GetProcessArchitecture()); |
| 814 | else |
| 815 | GetTarget().SetArchitecture(m_gdb_comm.GetHostArchitecture()); |
Jason Molenda | 16d127c | 2012-05-03 22:37:30 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 818 | if (log) |
| 819 | log->Printf ("ProcessGDBRemote::%s pid %" PRIu64 ": normalized target architecture triple: %s", __FUNCTION__, GetID (), GetTarget ().GetArchitecture ().GetTriple ().getTriple ().c_str ()); |
| 820 | |
| 821 | // Set the Unix signals properly for the target. |
| 822 | // FIXME Add a gdb-remote packet to discover dynamically. |
| 823 | if (error.Success ()) |
| 824 | { |
Tamas Berghammer | ac83982 | 2015-03-04 11:34:10 +0000 | [diff] [blame] | 825 | const ArchSpec arch_spec = m_gdb_comm.GetHostArchitecture(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 826 | if (arch_spec.IsValid ()) |
| 827 | { |
| 828 | if (log) |
| 829 | log->Printf ("ProcessGDBRemote::%s pid %" PRIu64 ": determining unix signals type based on architecture %s, triple %s", __FUNCTION__, GetID (), arch_spec.GetArchitectureName () ? arch_spec.GetArchitectureName () : "<null>", arch_spec.GetTriple ().getTriple ().c_str ()); |
| 830 | |
| 831 | switch (arch_spec.GetTriple ().getOS ()) |
| 832 | { |
| 833 | case llvm::Triple::Linux: |
Mohit K. Bhakkad | 2c2acf9 | 2015-04-09 07:12:15 +0000 | [diff] [blame] | 834 | if (arch_spec.GetTriple ().getArch () == llvm::Triple::mips64 || arch_spec.GetTriple ().getArch () == llvm::Triple::mips64el) |
| 835 | SetUnixSignals (UnixSignalsSP (new process_linux::MipsLinuxSignals ())); |
| 836 | else |
| 837 | SetUnixSignals (UnixSignalsSP (new process_linux::LinuxSignals ())); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 838 | if (log) |
| 839 | log->Printf ("ProcessGDBRemote::%s using Linux unix signals type for pid %" PRIu64, __FUNCTION__, GetID ()); |
| 840 | break; |
| 841 | case llvm::Triple::OpenBSD: |
| 842 | case llvm::Triple::FreeBSD: |
| 843 | case llvm::Triple::NetBSD: |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 844 | SetUnixSignals (UnixSignalsSP (new FreeBSDSignals ())); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 845 | if (log) |
| 846 | log->Printf ("ProcessGDBRemote::%s using *BSD unix signals type for pid %" PRIu64, __FUNCTION__, GetID ()); |
| 847 | break; |
| 848 | default: |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 849 | SetUnixSignals (UnixSignalsSP (new UnixSignals ())); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 850 | if (log) |
| 851 | log->Printf ("ProcessGDBRemote::%s using generic unix signals type for pid %" PRIu64, __FUNCTION__, GetID ()); |
| 852 | break; |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 857 | return error; |
| 858 | } |
| 859 | |
| 860 | Error |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 861 | ProcessGDBRemote::WillLaunchOrAttach () |
| 862 | { |
| 863 | Error error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 864 | m_stdio_communication.Clear (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 865 | return error; |
| 866 | } |
| 867 | |
| 868 | //---------------------------------------------------------------------- |
| 869 | // Process Control |
| 870 | //---------------------------------------------------------------------- |
| 871 | Error |
Jean-Daniel Dupas | 7782de9 | 2013-12-09 22:52:50 +0000 | [diff] [blame] | 872 | ProcessGDBRemote::DoLaunch (Module *exe_module, ProcessLaunchInfo &launch_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 873 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 874 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | 4957bf6 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 875 | Error error; |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 876 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 877 | if (log) |
| 878 | log->Printf ("ProcessGDBRemote::%s() entered", __FUNCTION__); |
| 879 | |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 880 | uint32_t launch_flags = launch_info.GetFlags().Get(); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 881 | FileSpec stdin_file_spec{}; |
| 882 | FileSpec stdout_file_spec{}; |
| 883 | FileSpec stderr_file_spec{}; |
| 884 | FileSpec working_dir = launch_info.GetWorkingDirectory(); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 885 | |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 886 | const FileAction *file_action; |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 887 | file_action = launch_info.GetFileActionForFD (STDIN_FILENO); |
| 888 | if (file_action) |
| 889 | { |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 890 | if (file_action->GetAction() == FileAction::eFileActionOpen) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 891 | stdin_file_spec = file_action->GetFileSpec(); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 892 | } |
| 893 | file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); |
| 894 | if (file_action) |
| 895 | { |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 896 | if (file_action->GetAction() == FileAction::eFileActionOpen) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 897 | stdout_file_spec = file_action->GetFileSpec(); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 898 | } |
| 899 | file_action = launch_info.GetFileActionForFD (STDERR_FILENO); |
| 900 | if (file_action) |
| 901 | { |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 902 | if (file_action->GetAction() == FileAction::eFileActionOpen) |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 903 | stderr_file_spec = file_action->GetFileSpec(); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 906 | if (log) |
| 907 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 908 | if (stdin_file_spec || stdout_file_spec || stderr_file_spec) |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 909 | log->Printf ("ProcessGDBRemote::%s provided with STDIO paths via launch_info: stdin=%s, stdout=%s, stderr=%s", |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 910 | __FUNCTION__, |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 911 | stdin_file_spec ? stdin_file_spec.GetCString() : "<null>", |
| 912 | stdout_file_spec ? stdout_file_spec.GetCString() : "<null>", |
| 913 | stderr_file_spec ? stderr_file_spec.GetCString() : "<null>"); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 914 | else |
| 915 | log->Printf ("ProcessGDBRemote::%s no STDIO paths given via launch_info", __FUNCTION__); |
| 916 | } |
| 917 | |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 918 | const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 919 | if (stdin_file_spec || disable_stdio) |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 920 | { |
| 921 | // the inferior will be reading stdin from the specified file |
| 922 | // or stdio is completely disabled |
| 923 | m_stdin_forward = false; |
| 924 | } |
| 925 | else |
| 926 | { |
| 927 | m_stdin_forward = true; |
| 928 | } |
| 929 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 930 | // ::LogSetBitMask (GDBR_LOG_DEFAULT); |
| 931 | // ::LogSetOptions (LLDB_LOG_OPTION_THREADSAFE | LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD); |
| 932 | // ::LogSetLogFile ("/dev/stdout"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 933 | |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 934 | ObjectFile * object_file = exe_module->GetObjectFile(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 935 | if (object_file) |
| 936 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 937 | // Make sure we aren't already connected? |
| 938 | if (!m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 939 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 940 | error = LaunchAndConnectToDebugserver (launch_info); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | if (error.Success()) |
| 944 | { |
| 945 | lldb_utility::PseudoTerminal pty; |
| 946 | const bool disable_stdio = (launch_flags & eLaunchFlagDisableSTDIO) != 0; |
Greg Clayton | 5f2a4f9 | 2011-03-02 21:34:46 +0000 | [diff] [blame] | 947 | |
Greg Clayton | f58c269 | 2011-06-24 22:32:10 +0000 | [diff] [blame] | 948 | PlatformSP platform_sp (m_target.GetPlatform()); |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 949 | if (disable_stdio) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 950 | { |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 951 | // set to /dev/null unless redirected to a file above |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 952 | if (!stdin_file_spec) |
| 953 | stdin_file_spec.SetFile("/dev/null", false); |
| 954 | if (!stdout_file_spec) |
| 955 | stdout_file_spec.SetFile("/dev/null", false); |
| 956 | if (!stderr_file_spec) |
| 957 | stderr_file_spec.SetFile("/dev/null", false); |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 958 | } |
| 959 | else if (platform_sp && platform_sp->IsHost()) |
| 960 | { |
| 961 | // If the debugserver is local and we aren't disabling STDIO, lets use |
| 962 | // a pseudo terminal to instead of relying on the 'O' packets for stdio |
| 963 | // since 'O' packets can really slow down debugging if the inferior |
| 964 | // does a lot of output. |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 965 | if ((!stdin_file_spec || !stdout_file_spec || !stderr_file_spec) && |
| 966 | pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, NULL, 0)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 967 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 968 | FileSpec slave_name{pty.GetSlaveName(NULL, 0), false}; |
| 969 | |
| 970 | if (!stdin_file_spec) |
| 971 | stdin_file_spec = slave_name; |
| 972 | |
| 973 | if (!stdout_file_spec) |
| 974 | stdout_file_spec = slave_name; |
| 975 | |
| 976 | if (!stderr_file_spec) |
| 977 | stderr_file_spec = slave_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 978 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 979 | if (log) |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 980 | log->Printf ("ProcessGDBRemote::%s adjusted STDIO paths for local platform (IsHost() is true) using slave: stdin=%s, stdout=%s, stderr=%s", |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 981 | __FUNCTION__, |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 982 | stdin_file_spec ? stdin_file_spec.GetCString() : "<null>", |
| 983 | stdout_file_spec ? stdout_file_spec.GetCString() : "<null>", |
| 984 | stderr_file_spec ? stderr_file_spec.GetCString() : "<null>"); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 987 | if (log) |
Vince Harron | 4a8abd3 | 2015-02-13 19:15:24 +0000 | [diff] [blame] | 988 | log->Printf ("ProcessGDBRemote::%s final STDIO paths after all adjustments: stdin=%s, stdout=%s, stderr=%s", |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 989 | __FUNCTION__, |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 990 | stdin_file_spec ? stdin_file_spec.GetCString() : "<null>", |
| 991 | stdout_file_spec ? stdout_file_spec.GetCString() : "<null>", |
| 992 | stderr_file_spec ? stderr_file_spec.GetCString() : "<null>"); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 993 | |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 994 | if (stdin_file_spec) |
| 995 | m_gdb_comm.SetSTDIN(stdin_file_spec); |
| 996 | if (stdout_file_spec) |
| 997 | m_gdb_comm.SetSTDOUT(stdout_file_spec); |
| 998 | if (stderr_file_spec) |
| 999 | m_gdb_comm.SetSTDERR(stderr_file_spec); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1000 | |
| 1001 | m_gdb_comm.SetDisableASLR (launch_flags & eLaunchFlagDisableASLR); |
Jim Ingham | 106d028 | 2014-06-25 02:32:56 +0000 | [diff] [blame] | 1002 | m_gdb_comm.SetDetachOnError (launch_flags & eLaunchFlagDetachOnError); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1003 | |
Greg Clayton | c4103b3 | 2011-05-08 04:53:50 +0000 | [diff] [blame] | 1004 | m_gdb_comm.SendLaunchArchPacket (m_target.GetArchitecture().GetArchitectureName()); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1005 | |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 1006 | const char * launch_event_data = launch_info.GetLaunchEventData(); |
| 1007 | if (launch_event_data != NULL && *launch_event_data != '\0') |
| 1008 | m_gdb_comm.SendLaunchEventDataPacket (launch_event_data); |
| 1009 | |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 1010 | if (working_dir) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1011 | { |
| 1012 | m_gdb_comm.SetWorkingDir (working_dir); |
| 1013 | } |
| 1014 | |
| 1015 | // Send the environment and the program + arguments after we connect |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 1016 | const Args &environment = launch_info.GetEnvironmentEntries(); |
| 1017 | if (environment.GetArgumentCount()) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1018 | { |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 1019 | size_t num_environment_entries = environment.GetArgumentCount(); |
| 1020 | for (size_t i=0; i<num_environment_entries; ++i) |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 1021 | { |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 1022 | const char *env_entry = environment.GetArgumentAtIndex(i); |
| 1023 | if (env_entry == NULL || m_gdb_comm.SendEnvironmentPacket(env_entry) != 0) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1024 | break; |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 1025 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1026 | } |
Greg Clayton | b0b9fe6 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 1027 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1028 | { |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 1029 | // Scope for the scoped timeout object |
| 1030 | GDBRemoteCommunication::ScopedTimeout timeout (m_gdb_comm, 10); |
| 1031 | |
| 1032 | int arg_packet_err = m_gdb_comm.SendArgumentsPacket (launch_info); |
| 1033 | if (arg_packet_err == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1034 | { |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 1035 | std::string error_str; |
| 1036 | if (m_gdb_comm.GetLaunchSuccess (error_str)) |
| 1037 | { |
| 1038 | SetID (m_gdb_comm.GetCurrentProcessID ()); |
| 1039 | } |
| 1040 | else |
| 1041 | { |
| 1042 | error.SetErrorString (error_str.c_str()); |
| 1043 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1044 | } |
| 1045 | else |
| 1046 | { |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 1047 | error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1048 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1049 | } |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 1050 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1051 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 1052 | { |
Johnny Chen | 4c1e920 | 2011-08-09 18:56:45 +0000 | [diff] [blame] | 1053 | if (log) |
| 1054 | log->Printf("failed to connect to debugserver: %s", error.AsCString()); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1055 | KillDebugserverProcess (); |
| 1056 | return error; |
| 1057 | } |
| 1058 | |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 1059 | StringExtractorGDBRemote response; |
| 1060 | if (m_gdb_comm.GetStopReply(response)) |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1061 | { |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 1062 | SetLastStopPacket(response); |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 1063 | // '?' Packets must be handled differently in non-stop mode |
| 1064 | if (GetTarget().GetNonStopModeEnabled()) |
| 1065 | HandleStopReplySequence(); |
| 1066 | |
Tamas Berghammer | e9f4dfe | 2015-03-13 10:32:42 +0000 | [diff] [blame] | 1067 | const ArchSpec &process_arch = m_gdb_comm.GetProcessArchitecture(); |
| 1068 | |
| 1069 | if (process_arch.IsValid()) |
Jason Molenda | c62bd7b | 2013-12-21 05:20:36 +0000 | [diff] [blame] | 1070 | { |
Tamas Berghammer | e9f4dfe | 2015-03-13 10:32:42 +0000 | [diff] [blame] | 1071 | m_target.MergeArchitecture(process_arch); |
| 1072 | } |
| 1073 | else |
| 1074 | { |
| 1075 | const ArchSpec &host_arch = m_gdb_comm.GetHostArchitecture(); |
| 1076 | if (host_arch.IsValid()) |
| 1077 | m_target.MergeArchitecture(host_arch); |
Carlo Kok | 7438912 | 2013-10-14 07:09:13 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 1080 | SetPrivateState (SetThreadStopInfo (response)); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1081 | |
| 1082 | if (!disable_stdio) |
| 1083 | { |
| 1084 | if (pty.GetMasterFileDescriptor() != lldb_utility::PseudoTerminal::invalid_fd) |
Greg Clayton | ee95ed5 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 1085 | SetSTDIOFileDescriptor (pty.ReleaseMasterFileDescriptor()); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1086 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1087 | } |
| 1088 | } |
Greg Clayton | c235ac7 | 2011-08-09 05:20:29 +0000 | [diff] [blame] | 1089 | else |
| 1090 | { |
Johnny Chen | 4c1e920 | 2011-08-09 18:56:45 +0000 | [diff] [blame] | 1091 | if (log) |
| 1092 | log->Printf("failed to connect to debugserver: %s", error.AsCString()); |
Greg Clayton | c235ac7 | 2011-08-09 05:20:29 +0000 | [diff] [blame] | 1093 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1094 | } |
| 1095 | else |
| 1096 | { |
| 1097 | // Set our user ID to an invalid process ID. |
| 1098 | SetID(LLDB_INVALID_PROCESS_ID); |
Greg Clayton | 982c976 | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 1099 | error.SetErrorStringWithFormat ("failed to get object file from '%s' for arch %s", |
| 1100 | exe_module->GetFileSpec().GetFilename().AsCString(), |
| 1101 | exe_module->GetArchitecture().GetArchitectureName()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1102 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1103 | return error; |
Greg Clayton | 4957bf6 | 2010-09-30 21:49:03 +0000 | [diff] [blame] | 1104 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | |
| 1108 | Error |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 1109 | ProcessGDBRemote::ConnectToDebugserver (const char *connect_url) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1110 | { |
| 1111 | Error error; |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1112 | // Only connect if we have a valid connect URL |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1113 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1114 | |
| 1115 | if (connect_url && connect_url[0]) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1116 | { |
Vince Harron | 1b5a74e | 2015-01-21 22:42:49 +0000 | [diff] [blame] | 1117 | if (log) |
| 1118 | log->Printf("ProcessGDBRemote::%s Connecting to %s", __FUNCTION__, connect_url); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1119 | std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); |
| 1120 | if (conn_ap.get()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1121 | { |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1122 | const uint32_t max_retry_count = 50; |
| 1123 | uint32_t retry_count = 0; |
| 1124 | while (!m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1125 | { |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1126 | if (conn_ap->Connect(connect_url, &error) == eConnectionStatusSuccess) |
| 1127 | { |
| 1128 | m_gdb_comm.SetConnection (conn_ap.release()); |
| 1129 | break; |
| 1130 | } |
| 1131 | else if (error.WasInterrupted()) |
| 1132 | { |
| 1133 | // If we were interrupted, don't keep retrying. |
| 1134 | break; |
| 1135 | } |
| 1136 | |
| 1137 | retry_count++; |
| 1138 | |
| 1139 | if (retry_count >= max_retry_count) |
| 1140 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1141 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1142 | usleep (100000); |
| 1143 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | if (!m_gdb_comm.IsConnected()) |
| 1148 | { |
| 1149 | if (error.Success()) |
| 1150 | error.SetErrorString("not connected to remote gdb server"); |
| 1151 | return error; |
| 1152 | } |
| 1153 | |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 1154 | |
| 1155 | // Start the communications read thread so all incoming data can be |
| 1156 | // parsed into packets and queued as they arrive. |
| 1157 | if (GetTarget().GetNonStopModeEnabled()) |
| 1158 | m_gdb_comm.StartReadThread(); |
| 1159 | |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1160 | // We always seem to be able to open a connection to a local port |
| 1161 | // so we need to make sure we can then send data to it. If we can't |
| 1162 | // then we aren't actually connected to anything, so try and do the |
| 1163 | // handshake with the remote GDB server and make sure that goes |
| 1164 | // alright. |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 1165 | if (!m_gdb_comm.HandshakeWithServer (&error)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1166 | { |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1167 | m_gdb_comm.Disconnect(); |
| 1168 | if (error.Success()) |
| 1169 | error.SetErrorString("not connected to remote gdb server"); |
| 1170 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1171 | } |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 1172 | |
| 1173 | // Send $QNonStop:1 packet on startup if required |
| 1174 | if (GetTarget().GetNonStopModeEnabled()) |
| 1175 | m_gdb_comm.SetNonStopMode(true); |
| 1176 | |
Greg Clayton | b30c50c | 2015-05-29 00:01:55 +0000 | [diff] [blame] | 1177 | m_gdb_comm.GetEchoSupported (); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1178 | m_gdb_comm.GetThreadSuffixSupported (); |
Greg Clayton | 4463399 | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 1179 | m_gdb_comm.GetListThreadsInStopReplySupported (); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 1180 | m_gdb_comm.GetHostInfo (); |
| 1181 | m_gdb_comm.GetVContSupported ('c'); |
Jim Ingham | cd16df9 | 2012-07-20 21:37:13 +0000 | [diff] [blame] | 1182 | m_gdb_comm.GetVAttachOrWaitSupported(); |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 1183 | |
| 1184 | // Ask the remote server for the default thread id |
| 1185 | if (GetTarget().GetNonStopModeEnabled()) |
| 1186 | m_gdb_comm.GetDefaultThreadId(m_initial_tid); |
| 1187 | |
| 1188 | |
Jim Ingham | 03afad8 | 2012-07-02 05:40:07 +0000 | [diff] [blame] | 1189 | size_t num_cmds = GetExtraStartupCommands().GetArgumentCount(); |
| 1190 | for (size_t idx = 0; idx < num_cmds; idx++) |
| 1191 | { |
| 1192 | StringExtractorGDBRemote response; |
Jim Ingham | 03afad8 | 2012-07-02 05:40:07 +0000 | [diff] [blame] | 1193 | m_gdb_comm.SendPacketAndWaitForResponse (GetExtraStartupCommands().GetArgumentAtIndex(idx), response, false); |
| 1194 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1195 | return error; |
| 1196 | } |
| 1197 | |
| 1198 | void |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1199 | ProcessGDBRemote::DidLaunchOrAttach (ArchSpec& process_arch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1200 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1201 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | 6d09345 | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 1202 | if (log) |
| 1203 | log->Printf ("ProcessGDBRemote::DidLaunch()"); |
Greg Clayton | 93d3c833 | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 1204 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1205 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1206 | BuildDynamicRegisterInfo (false); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1207 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1208 | // See if the GDB server supports the qHostInfo information |
Greg Clayton | 0d0c12a | 2011-02-09 03:09:55 +0000 | [diff] [blame] | 1209 | |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 1210 | |
| 1211 | // See if the GDB server supports the qProcessInfo packet, if so |
| 1212 | // prefer that over the Host information as it will be more specific |
| 1213 | // to our process. |
| 1214 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1215 | const ArchSpec &remote_process_arch = m_gdb_comm.GetProcessArchitecture(); |
| 1216 | if (remote_process_arch.IsValid()) |
| 1217 | { |
| 1218 | process_arch = remote_process_arch; |
| 1219 | if (log) |
| 1220 | log->Printf ("ProcessGDBRemote::%s gdb-remote had process architecture, using %s %s", |
| 1221 | __FUNCTION__, |
| 1222 | process_arch.GetArchitectureName () ? process_arch.GetArchitectureName () : "<null>", |
| 1223 | process_arch.GetTriple().getTriple ().c_str() ? process_arch.GetTriple().getTriple ().c_str() : "<null>"); |
| 1224 | } |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1225 | else |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1226 | { |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1227 | process_arch = m_gdb_comm.GetHostArchitecture(); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1228 | if (log) |
| 1229 | log->Printf ("ProcessGDBRemote::%s gdb-remote did not have process architecture, using gdb-remote host architecture %s %s", |
| 1230 | __FUNCTION__, |
| 1231 | process_arch.GetArchitectureName () ? process_arch.GetArchitectureName () : "<null>", |
| 1232 | process_arch.GetTriple().getTriple ().c_str() ? process_arch.GetTriple().getTriple ().c_str() : "<null>"); |
| 1233 | } |
Jason Molenda | f17b5ac | 2012-12-19 02:54:03 +0000 | [diff] [blame] | 1234 | |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1235 | if (process_arch.IsValid()) |
Greg Clayton | 0d0c12a | 2011-02-09 03:09:55 +0000 | [diff] [blame] | 1236 | { |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1237 | const ArchSpec &target_arch = GetTarget().GetArchitecture(); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1238 | if (target_arch.IsValid()) |
| 1239 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1240 | if (log) |
| 1241 | log->Printf ("ProcessGDBRemote::%s analyzing target arch, currently %s %s", |
| 1242 | __FUNCTION__, |
| 1243 | target_arch.GetArchitectureName () ? target_arch.GetArchitectureName () : "<null>", |
| 1244 | target_arch.GetTriple().getTriple ().c_str() ? target_arch.GetTriple().getTriple ().c_str() : "<null>"); |
| 1245 | |
| 1246 | // If the remote host is ARM and we have apple as the vendor, then |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1247 | // ARM executables and shared libraries can have mixed ARM architectures. |
| 1248 | // You can have an armv6 executable, and if the host is armv7, then the |
| 1249 | // system will load the best possible architecture for all shared libraries |
| 1250 | // it has, so we really need to take the remote host architecture as our |
| 1251 | // defacto architecture in this case. |
| 1252 | |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1253 | if (process_arch.GetMachine() == llvm::Triple::arm && |
| 1254 | process_arch.GetTriple().getVendor() == llvm::Triple::Apple) |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1255 | { |
Jason Molenda | 921c01b | 2014-08-03 21:42:52 +0000 | [diff] [blame] | 1256 | GetTarget().SetArchitecture (process_arch); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1257 | if (log) |
| 1258 | log->Printf ("ProcessGDBRemote::%s remote process is ARM/Apple, setting target arch to %s %s", |
| 1259 | __FUNCTION__, |
| 1260 | process_arch.GetArchitectureName () ? process_arch.GetArchitectureName () : "<null>", |
| 1261 | process_arch.GetTriple().getTriple ().c_str() ? process_arch.GetTriple().getTriple ().c_str() : "<null>"); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1262 | } |
| 1263 | else |
| 1264 | { |
| 1265 | // Fill in what is missing in the triple |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1266 | const llvm::Triple &remote_triple = process_arch.GetTriple(); |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1267 | llvm::Triple new_target_triple = target_arch.GetTriple(); |
| 1268 | if (new_target_triple.getVendorName().size() == 0) |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1269 | { |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1270 | new_target_triple.setVendor (remote_triple.getVendor()); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1271 | |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1272 | if (new_target_triple.getOSName().size() == 0) |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1273 | { |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1274 | new_target_triple.setOS (remote_triple.getOS()); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1275 | |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1276 | if (new_target_triple.getEnvironmentName().size() == 0) |
| 1277 | new_target_triple.setEnvironment (remote_triple.getEnvironment()); |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 1278 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1279 | |
Tamas Berghammer | e724af1 | 2015-03-13 10:32:37 +0000 | [diff] [blame] | 1280 | ArchSpec new_target_arch = target_arch; |
| 1281 | new_target_arch.SetTriple(new_target_triple); |
| 1282 | GetTarget().SetArchitecture(new_target_arch); |
| 1283 | } |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1284 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1285 | |
| 1286 | if (log) |
| 1287 | log->Printf ("ProcessGDBRemote::%s final target arch after adjustments for remote architecture: %s %s", |
| 1288 | __FUNCTION__, |
| 1289 | target_arch.GetArchitectureName () ? target_arch.GetArchitectureName () : "<null>", |
| 1290 | target_arch.GetTriple().getTriple ().c_str() ? target_arch.GetTriple().getTriple ().c_str() : "<null>"); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1291 | } |
| 1292 | else |
| 1293 | { |
| 1294 | // The target doesn't have a valid architecture yet, set it from |
| 1295 | // the architecture we got from the remote GDB server |
Jason Molenda | 921c01b | 2014-08-03 21:42:52 +0000 | [diff] [blame] | 1296 | GetTarget().SetArchitecture (process_arch); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 1297 | } |
Greg Clayton | 0d0c12a | 2011-02-09 03:09:55 +0000 | [diff] [blame] | 1298 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | void |
| 1303 | ProcessGDBRemote::DidLaunch () |
| 1304 | { |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1305 | ArchSpec process_arch; |
| 1306 | DidLaunchOrAttach (process_arch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | Error |
Han Ming Ong | 8464704 | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 1310 | ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info) |
| 1311 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1312 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1313 | Error error; |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1314 | |
| 1315 | if (log) |
| 1316 | log->Printf ("ProcessGDBRemote::%s()", __FUNCTION__); |
| 1317 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1318 | // Clear out and clean up from any current state |
| 1319 | Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1320 | if (attach_pid != LLDB_INVALID_PROCESS_ID) |
| 1321 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1322 | // Make sure we aren't already connected? |
| 1323 | if (!m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1324 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 1325 | error = LaunchAndConnectToDebugserver (attach_info); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1326 | |
| 1327 | if (error.Fail()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1328 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1329 | const char *error_string = error.AsCString(); |
| 1330 | if (error_string == NULL) |
| 1331 | error_string = "unable to launch " DEBUGSERVER_BASENAME; |
| 1332 | |
| 1333 | SetExitStatus (-1, error_string); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1334 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | if (error.Success()) |
| 1338 | { |
Jim Ingham | 106d028 | 2014-06-25 02:32:56 +0000 | [diff] [blame] | 1339 | m_gdb_comm.SetDetachOnError(attach_info.GetDetachOnError()); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1340 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1341 | char packet[64]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1342 | const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, attach_pid); |
Greg Clayton | 3b60842 | 2011-11-19 02:11:30 +0000 | [diff] [blame] | 1343 | SetID (attach_pid); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1344 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet, packet_len)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1345 | } |
| 1346 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1347 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1348 | return error; |
| 1349 | } |
| 1350 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1351 | Error |
Jean-Daniel Dupas | 9c517c0 | 2013-12-23 22:32:54 +0000 | [diff] [blame] | 1352 | ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, const ProcessAttachInfo &attach_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1353 | { |
| 1354 | Error error; |
| 1355 | // Clear out and clean up from any current state |
| 1356 | Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1357 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1358 | if (process_name && process_name[0]) |
| 1359 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1360 | // Make sure we aren't already connected? |
| 1361 | if (!m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1362 | { |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 1363 | error = LaunchAndConnectToDebugserver (attach_info); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1364 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1365 | if (error.Fail()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1366 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1367 | const char *error_string = error.AsCString(); |
| 1368 | if (error_string == NULL) |
| 1369 | error_string = "unable to launch " DEBUGSERVER_BASENAME; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1370 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1371 | SetExitStatus (-1, error_string); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1372 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | if (error.Success()) |
| 1376 | { |
| 1377 | StreamString packet; |
| 1378 | |
Jim Ingham | 106d028 | 2014-06-25 02:32:56 +0000 | [diff] [blame] | 1379 | m_gdb_comm.SetDetachOnError(attach_info.GetDetachOnError()); |
| 1380 | |
Jean-Daniel Dupas | 9c517c0 | 2013-12-23 22:32:54 +0000 | [diff] [blame] | 1381 | if (attach_info.GetWaitForLaunch()) |
Jim Ingham | cd16df9 | 2012-07-20 21:37:13 +0000 | [diff] [blame] | 1382 | { |
| 1383 | if (!m_gdb_comm.GetVAttachOrWaitSupported()) |
| 1384 | { |
| 1385 | packet.PutCString ("vAttachWait"); |
| 1386 | } |
| 1387 | else |
| 1388 | { |
| 1389 | if (attach_info.GetIgnoreExisting()) |
| 1390 | packet.PutCString("vAttachWait"); |
| 1391 | else |
| 1392 | packet.PutCString ("vAttachOrWait"); |
| 1393 | } |
| 1394 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 1395 | else |
| 1396 | packet.PutCString("vAttachName"); |
| 1397 | packet.PutChar(';'); |
| 1398 | packet.PutBytesAsRawHex8(process_name, strlen(process_name), lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder()); |
| 1399 | |
| 1400 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet.GetData(), packet.GetSize())); |
| 1401 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1402 | } |
| 1403 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1404 | return error; |
| 1405 | } |
| 1406 | |
Greg Clayton | 5df78fa | 2015-05-23 03:54:53 +0000 | [diff] [blame] | 1407 | void |
| 1408 | ProcessGDBRemote::DidExit () |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1409 | { |
Greg Clayton | 5df78fa | 2015-05-23 03:54:53 +0000 | [diff] [blame] | 1410 | // When we exit, disconnect from the GDB server communications |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1411 | m_gdb_comm.Disconnect(); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1414 | void |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1415 | ProcessGDBRemote::DidAttach (ArchSpec &process_arch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1416 | { |
Jim Ingham | bb006ce | 2014-08-02 00:33:35 +0000 | [diff] [blame] | 1417 | // If you can figure out what the architecture is, fill it in here. |
| 1418 | process_arch.Clear(); |
| 1419 | DidLaunchOrAttach (process_arch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 1422 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1423 | Error |
| 1424 | ProcessGDBRemote::WillResume () |
| 1425 | { |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1426 | m_continue_c_tids.clear(); |
| 1427 | m_continue_C_tids.clear(); |
| 1428 | m_continue_s_tids.clear(); |
| 1429 | m_continue_S_tids.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1430 | return Error(); |
| 1431 | } |
| 1432 | |
| 1433 | Error |
| 1434 | ProcessGDBRemote::DoResume () |
| 1435 | { |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1436 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1437 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Greg Clayton | 6d09345 | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 1438 | if (log) |
| 1439 | log->Printf ("ProcessGDBRemote::Resume()"); |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 1440 | |
| 1441 | Listener listener ("gdb-remote.resume-packet-sent"); |
| 1442 | if (listener.StartListeningForEvents (&m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent)) |
| 1443 | { |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1444 | listener.StartListeningForEvents (&m_async_broadcaster, ProcessGDBRemote::eBroadcastBitAsyncThreadDidExit); |
| 1445 | |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1446 | const size_t num_threads = GetThreadList().GetSize(); |
| 1447 | |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1448 | StreamString continue_packet; |
| 1449 | bool continue_packet_error = false; |
| 1450 | if (m_gdb_comm.HasAnyVContSupport ()) |
| 1451 | { |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 1452 | if (!GetTarget().GetNonStopModeEnabled() && |
| 1453 | (m_continue_c_tids.size() == num_threads || |
Greg Clayton | e98008c | 2014-02-13 23:34:38 +0000 | [diff] [blame] | 1454 | (m_continue_c_tids.empty() && |
| 1455 | m_continue_C_tids.empty() && |
| 1456 | m_continue_s_tids.empty() && |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 1457 | m_continue_S_tids.empty()))) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1458 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1459 | // All threads are continuing, just send a "c" packet |
| 1460 | continue_packet.PutCString ("c"); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1461 | } |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1462 | else |
| 1463 | { |
| 1464 | continue_packet.PutCString ("vCont"); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1465 | |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1466 | if (!m_continue_c_tids.empty()) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1467 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1468 | if (m_gdb_comm.GetVContSupported ('c')) |
| 1469 | { |
| 1470 | 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) |
| 1471 | continue_packet.Printf(";c:%4.4" PRIx64, *t_pos); |
| 1472 | } |
| 1473 | else |
| 1474 | continue_packet_error = true; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1475 | } |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1476 | |
| 1477 | if (!continue_packet_error && !m_continue_C_tids.empty()) |
| 1478 | { |
| 1479 | if (m_gdb_comm.GetVContSupported ('C')) |
| 1480 | { |
| 1481 | 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) |
| 1482 | continue_packet.Printf(";C%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first); |
| 1483 | } |
| 1484 | else |
| 1485 | continue_packet_error = true; |
| 1486 | } |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 1487 | |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1488 | if (!continue_packet_error && !m_continue_s_tids.empty()) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1489 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1490 | if (m_gdb_comm.GetVContSupported ('s')) |
| 1491 | { |
| 1492 | 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) |
| 1493 | continue_packet.Printf(";s:%4.4" PRIx64, *t_pos); |
| 1494 | } |
| 1495 | else |
| 1496 | continue_packet_error = true; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1497 | } |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1498 | |
| 1499 | if (!continue_packet_error && !m_continue_S_tids.empty()) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1500 | { |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1501 | if (m_gdb_comm.GetVContSupported ('S')) |
| 1502 | { |
| 1503 | 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) |
| 1504 | continue_packet.Printf(";S%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first); |
| 1505 | } |
| 1506 | else |
| 1507 | continue_packet_error = true; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1508 | } |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1509 | |
| 1510 | if (continue_packet_error) |
| 1511 | continue_packet.GetString().clear(); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1512 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1513 | } |
| 1514 | else |
| 1515 | continue_packet_error = true; |
| 1516 | |
| 1517 | if (continue_packet_error) |
| 1518 | { |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1519 | // Either no vCont support, or we tried to use part of the vCont |
| 1520 | // packet that wasn't supported by the remote GDB server. |
| 1521 | // 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] | 1522 | const size_t num_continue_c_tids = m_continue_c_tids.size(); |
| 1523 | const size_t num_continue_C_tids = m_continue_C_tids.size(); |
| 1524 | const size_t num_continue_s_tids = m_continue_s_tids.size(); |
| 1525 | const size_t num_continue_S_tids = m_continue_S_tids.size(); |
| 1526 | if (num_continue_c_tids > 0) |
| 1527 | { |
| 1528 | if (num_continue_c_tids == num_threads) |
| 1529 | { |
| 1530 | // All threads are resuming... |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1531 | m_gdb_comm.SetCurrentThreadForRun (-1); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1532 | continue_packet.PutChar ('c'); |
| 1533 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1534 | } |
| 1535 | else if (num_continue_c_tids == 1 && |
| 1536 | num_continue_C_tids == 0 && |
| 1537 | num_continue_s_tids == 0 && |
| 1538 | num_continue_S_tids == 0 ) |
| 1539 | { |
| 1540 | // Only one thread is continuing |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1541 | m_gdb_comm.SetCurrentThreadForRun (m_continue_c_tids.front()); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1542 | continue_packet.PutChar ('c'); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1543 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1544 | } |
| 1545 | } |
| 1546 | |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1547 | if (continue_packet_error && num_continue_C_tids > 0) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1548 | { |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1549 | if ((num_continue_C_tids + num_continue_c_tids) == num_threads && |
| 1550 | num_continue_C_tids > 0 && |
| 1551 | num_continue_s_tids == 0 && |
| 1552 | num_continue_S_tids == 0 ) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1553 | { |
| 1554 | const int continue_signo = m_continue_C_tids.front().second; |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1555 | // Only one thread is continuing |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1556 | if (num_continue_C_tids > 1) |
| 1557 | { |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1558 | // More that one thread with a signal, yet we don't have |
| 1559 | // vCont support and we are being asked to resume each |
| 1560 | // thread with a signal, we need to make sure they are |
| 1561 | // all the same signal, or we can't issue the continue |
| 1562 | // accurately with the current support... |
| 1563 | if (num_continue_C_tids > 1) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1564 | { |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1565 | continue_packet_error = false; |
| 1566 | for (size_t i=1; i<m_continue_C_tids.size(); ++i) |
| 1567 | { |
| 1568 | if (m_continue_C_tids[i].second != continue_signo) |
| 1569 | continue_packet_error = true; |
| 1570 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1571 | } |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1572 | if (!continue_packet_error) |
| 1573 | m_gdb_comm.SetCurrentThreadForRun (-1); |
| 1574 | } |
| 1575 | else |
| 1576 | { |
| 1577 | // Set the continue thread ID |
| 1578 | continue_packet_error = false; |
| 1579 | m_gdb_comm.SetCurrentThreadForRun (m_continue_C_tids.front().first); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1580 | } |
| 1581 | if (!continue_packet_error) |
| 1582 | { |
| 1583 | // Add threads continuing with the same signo... |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1584 | continue_packet.Printf("C%2.2x", continue_signo); |
| 1585 | } |
| 1586 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1589 | if (continue_packet_error && num_continue_s_tids > 0) |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1590 | { |
| 1591 | if (num_continue_s_tids == num_threads) |
| 1592 | { |
| 1593 | // All threads are resuming... |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1594 | m_gdb_comm.SetCurrentThreadForRun (-1); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1595 | continue_packet.PutChar ('s'); |
| 1596 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1597 | } |
| 1598 | else if (num_continue_c_tids == 0 && |
| 1599 | num_continue_C_tids == 0 && |
| 1600 | num_continue_s_tids == 1 && |
| 1601 | num_continue_S_tids == 0 ) |
| 1602 | { |
| 1603 | // Only one thread is stepping |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1604 | m_gdb_comm.SetCurrentThreadForRun (m_continue_s_tids.front()); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1605 | continue_packet.PutChar ('s'); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1606 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | if (!continue_packet_error && num_continue_S_tids > 0) |
| 1611 | { |
| 1612 | if (num_continue_S_tids == num_threads) |
| 1613 | { |
| 1614 | const int step_signo = m_continue_S_tids.front().second; |
| 1615 | // Are all threads trying to step with the same signal? |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1616 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1617 | if (num_continue_S_tids > 1) |
| 1618 | { |
| 1619 | for (size_t i=1; i<num_threads; ++i) |
| 1620 | { |
| 1621 | if (m_continue_S_tids[i].second != step_signo) |
| 1622 | continue_packet_error = true; |
| 1623 | } |
| 1624 | } |
| 1625 | if (!continue_packet_error) |
| 1626 | { |
| 1627 | // Add threads stepping with the same signo... |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1628 | m_gdb_comm.SetCurrentThreadForRun (-1); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1629 | continue_packet.Printf("S%2.2x", step_signo); |
| 1630 | } |
| 1631 | } |
| 1632 | else if (num_continue_c_tids == 0 && |
| 1633 | num_continue_C_tids == 0 && |
| 1634 | num_continue_s_tids == 0 && |
| 1635 | num_continue_S_tids == 1 ) |
| 1636 | { |
| 1637 | // Only one thread is stepping with signal |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1638 | m_gdb_comm.SetCurrentThreadForRun (m_continue_S_tids.front().first); |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1639 | continue_packet.Printf("S%2.2x", m_continue_S_tids.front().second); |
Greg Clayton | 0c74e78 | 2011-06-24 03:21:43 +0000 | [diff] [blame] | 1640 | continue_packet_error = false; |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1641 | } |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | if (continue_packet_error) |
| 1646 | { |
| 1647 | error.SetErrorString ("can't make continue packet for this resume"); |
| 1648 | } |
| 1649 | else |
| 1650 | { |
| 1651 | EventSP event_sp; |
| 1652 | TimeValue timeout; |
| 1653 | timeout = TimeValue::Now(); |
| 1654 | timeout.OffsetWithSeconds (5); |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 1655 | if (!m_async_thread.IsJoinable()) |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1656 | { |
| 1657 | error.SetErrorString ("Trying to resume but the async thread is dead."); |
| 1658 | if (log) |
| 1659 | log->Printf ("ProcessGDBRemote::DoResume: Trying to resume but the async thread is dead."); |
| 1660 | return error; |
| 1661 | } |
| 1662 | |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1663 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (continue_packet.GetData(), continue_packet.GetSize())); |
| 1664 | |
| 1665 | if (listener.WaitForEvent (&timeout, event_sp) == false) |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1666 | { |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1667 | error.SetErrorString("Resume timed out."); |
Jim Ingham | b1e2e84 | 2012-04-12 18:49:31 +0000 | [diff] [blame] | 1668 | if (log) |
| 1669 | log->Printf ("ProcessGDBRemote::DoResume: Resume timed out."); |
| 1670 | } |
| 1671 | else if (event_sp->BroadcasterIs (&m_async_broadcaster)) |
| 1672 | { |
| 1673 | error.SetErrorString ("Broadcast continue, but the async thread was killed before we got an ack back."); |
| 1674 | if (log) |
| 1675 | log->Printf ("ProcessGDBRemote::DoResume: Broadcast continue, but the async thread was killed before we got an ack back."); |
| 1676 | return error; |
| 1677 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 1678 | } |
Greg Clayton | e521966 | 2010-12-03 06:02:24 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1681 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1684 | void |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 1685 | ProcessGDBRemote::HandleStopReplySequence () |
| 1686 | { |
| 1687 | while(true) |
| 1688 | { |
| 1689 | // Send vStopped |
| 1690 | StringExtractorGDBRemote response; |
| 1691 | m_gdb_comm.SendPacketAndWaitForResponse("vStopped", response, false); |
| 1692 | |
| 1693 | // OK represents end of signal list |
| 1694 | if (response.IsOKResponse()) |
| 1695 | break; |
| 1696 | |
| 1697 | // If not OK or a normal packet we have a problem |
| 1698 | if (!response.IsNormalResponse()) |
| 1699 | break; |
| 1700 | |
| 1701 | SetLastStopPacket(response); |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | void |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1706 | ProcessGDBRemote::ClearThreadIDList () |
| 1707 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1708 | Mutex::Locker locker(m_thread_list_real.GetMutex()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1709 | m_thread_ids.clear(); |
| 1710 | } |
| 1711 | |
| 1712 | bool |
| 1713 | ProcessGDBRemote::UpdateThreadIDList () |
| 1714 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1715 | Mutex::Locker locker(m_thread_list_real.GetMutex()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1716 | bool sequence_mutex_unavailable = false; |
| 1717 | m_gdb_comm.GetCurrentThreadIDs (m_thread_ids, sequence_mutex_unavailable); |
| 1718 | if (sequence_mutex_unavailable) |
| 1719 | { |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1720 | return false; // We just didn't get the list |
| 1721 | } |
| 1722 | return true; |
| 1723 | } |
| 1724 | |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1725 | bool |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1726 | ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1727 | { |
| 1728 | // locker will keep a mutex locked until it goes out of scope |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 1729 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD)); |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 1730 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1731 | log->Printf ("ProcessGDBRemote::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1732 | |
| 1733 | size_t num_thread_ids = m_thread_ids.size(); |
| 1734 | // The "m_thread_ids" thread ID list should always be updated after each stop |
| 1735 | // reply packet, but in case it isn't, update it here. |
| 1736 | if (num_thread_ids == 0) |
| 1737 | { |
| 1738 | if (!UpdateThreadIDList ()) |
| 1739 | return false; |
| 1740 | num_thread_ids = m_thread_ids.size(); |
| 1741 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1742 | |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1743 | ThreadList old_thread_list_copy(old_thread_list); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1744 | if (num_thread_ids > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1745 | { |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1746 | for (size_t i=0; i<num_thread_ids; ++i) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1747 | { |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1748 | tid_t tid = m_thread_ids[i]; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1749 | ThreadSP thread_sp (old_thread_list_copy.RemoveThreadByProtocolID(tid, false)); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1750 | if (!thread_sp) |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1751 | { |
Jim Ingham | 4f465cf | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 1752 | thread_sp.reset (new ThreadGDBRemote (*this, tid)); |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1753 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
| 1754 | log->Printf( |
| 1755 | "ProcessGDBRemote::%s Making new thread: %p for thread ID: 0x%" PRIx64 ".\n", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1756 | __FUNCTION__, static_cast<void*>(thread_sp.get()), |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1757 | thread_sp->GetID()); |
| 1758 | } |
| 1759 | else |
| 1760 | { |
| 1761 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
| 1762 | log->Printf( |
| 1763 | "ProcessGDBRemote::%s Found old thread: %p for thread ID: 0x%" PRIx64 ".\n", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1764 | __FUNCTION__, static_cast<void*>(thread_sp.get()), |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1765 | thread_sp->GetID()); |
| 1766 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1767 | new_thread_list.AddThread(thread_sp); |
Greg Clayton | adc00cb | 2011-05-20 23:38:13 +0000 | [diff] [blame] | 1768 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1769 | } |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1770 | |
| 1771 | // Whatever that is left in old_thread_list_copy are not |
| 1772 | // present in new_thread_list. Remove non-existent threads from internal id table. |
| 1773 | size_t old_num_thread_ids = old_thread_list_copy.GetSize(false); |
| 1774 | for (size_t i=0; i<old_num_thread_ids; i++) |
| 1775 | { |
| 1776 | ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex (i, false)); |
| 1777 | if (old_thread_sp) |
| 1778 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1779 | lldb::tid_t old_thread_id = old_thread_sp->GetProtocolID(); |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 1780 | m_thread_id_to_index_id_map.erase(old_thread_id); |
| 1781 | } |
| 1782 | } |
| 1783 | |
Greg Clayton | 9fc1355 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1784 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | |
| 1788 | StateType |
| 1789 | ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet) |
| 1790 | { |
Greg Clayton | dd0e5a5 | 2011-06-02 22:22:38 +0000 | [diff] [blame] | 1791 | stop_packet.SetFilePos (0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1792 | const char stop_type = stop_packet.GetChar(); |
| 1793 | switch (stop_type) |
| 1794 | { |
| 1795 | case 'T': |
| 1796 | case 'S': |
| 1797 | { |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 1798 | // This is a bit of a hack, but is is required. If we did exec, we |
| 1799 | // need to clear our thread lists and also know to rebuild our dynamic |
| 1800 | // register info before we lookup and threads and populate the expedited |
| 1801 | // register values so we need to know this right away so we can cleanup |
| 1802 | // and update our registers. |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 1803 | const uint32_t stop_id = GetStopID(); |
| 1804 | if (stop_id == 0) |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1805 | { |
| 1806 | // Our first stop, make sure we have a process ID, and also make |
| 1807 | // sure we know about our registers |
| 1808 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 1809 | { |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 1810 | lldb::pid_t pid = m_gdb_comm.GetCurrentProcessID (); |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1811 | if (pid != LLDB_INVALID_PROCESS_ID) |
| 1812 | SetID (pid); |
| 1813 | } |
| 1814 | BuildDynamicRegisterInfo (true); |
| 1815 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1816 | // Stop with signal and thread info |
| 1817 | const uint8_t signo = stop_packet.GetHexU8(); |
| 1818 | std::string name; |
| 1819 | std::string value; |
| 1820 | std::string thread_name; |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1821 | std::string reason; |
| 1822 | std::string description; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1823 | uint32_t exc_type = 0; |
Greg Clayton | 896dff6 | 2010-07-23 16:45:51 +0000 | [diff] [blame] | 1824 | std::vector<addr_t> exc_data; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1825 | addr_t thread_dispatch_qaddr = LLDB_INVALID_ADDRESS; |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1826 | ThreadSP thread_sp; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1827 | ThreadGDBRemote *gdb_thread = NULL; |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1828 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1829 | while (stop_packet.GetNameColonValue(name, value)) |
| 1830 | { |
| 1831 | if (name.compare("metype") == 0) |
| 1832 | { |
| 1833 | // exception type in big endian hex |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 1834 | exc_type = StringConvert::ToUInt32 (value.c_str(), 0, 16); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1835 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1836 | else if (name.compare("medata") == 0) |
| 1837 | { |
| 1838 | // exception data in big endian hex |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 1839 | exc_data.push_back(StringConvert::ToUInt64 (value.c_str(), 0, 16)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1840 | } |
| 1841 | else if (name.compare("thread") == 0) |
| 1842 | { |
| 1843 | // thread in big endian hex |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 1844 | lldb::tid_t tid = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1845 | // m_thread_list_real does have its own mutex, but we need to |
| 1846 | // hold onto the mutex between the call to m_thread_list_real.FindThreadByID(...) |
| 1847 | // and the m_thread_list_real.AddThread(...) so it doesn't change on us |
| 1848 | Mutex::Locker locker (m_thread_list_real.GetMutex ()); |
| 1849 | thread_sp = m_thread_list_real.FindThreadByProtocolID(tid, false); |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1850 | |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1851 | if (!thread_sp) |
Greg Clayton | d1d06e4 | 2013-04-20 00:27:58 +0000 | [diff] [blame] | 1852 | { |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1853 | // Create the thread if we need to |
Jim Ingham | 4f465cf | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 1854 | thread_sp.reset (new ThreadGDBRemote (*this, tid)); |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1855 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD)); |
| 1856 | if (log && log->GetMask().Test(GDBR_LOG_VERBOSE)) |
| 1857 | log->Printf ("ProcessGDBRemote::%s Adding new thread: %p for thread ID: 0x%" PRIx64 ".\n", |
| 1858 | __FUNCTION__, |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1859 | static_cast<void*>(thread_sp.get()), |
Jim Ingham | dee1bc9 | 2013-06-22 00:27:45 +0000 | [diff] [blame] | 1860 | thread_sp->GetID()); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 1861 | |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1862 | m_thread_list_real.AddThread(thread_sp); |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1863 | } |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1864 | gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get()); |
| 1865 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1866 | } |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1867 | else if (name.compare("threads") == 0) |
| 1868 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1869 | Mutex::Locker locker(m_thread_list_real.GetMutex()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1870 | m_thread_ids.clear(); |
Greg Clayton | 4463399 | 2012-04-10 03:22:03 +0000 | [diff] [blame] | 1871 | // A comma separated list of all threads in the current |
| 1872 | // process that includes the thread for this stop reply |
| 1873 | // packet |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1874 | size_t comma_pos; |
| 1875 | lldb::tid_t tid; |
| 1876 | while ((comma_pos = value.find(',')) != std::string::npos) |
| 1877 | { |
| 1878 | value[comma_pos] = '\0'; |
| 1879 | // thread in big endian hex |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 1880 | tid = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1881 | if (tid != LLDB_INVALID_THREAD_ID) |
| 1882 | m_thread_ids.push_back (tid); |
| 1883 | value.erase(0, comma_pos + 1); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1884 | } |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 1885 | tid = StringConvert::ToUInt64 (value.c_str(), LLDB_INVALID_THREAD_ID, 16); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 1886 | if (tid != LLDB_INVALID_THREAD_ID) |
| 1887 | m_thread_ids.push_back (tid); |
| 1888 | } |
Greg Clayton | de9d049 | 2011-01-08 03:17:57 +0000 | [diff] [blame] | 1889 | else if (name.compare("hexname") == 0) |
| 1890 | { |
| 1891 | StringExtractor name_extractor; |
| 1892 | // Swap "value" over into "name_extractor" |
| 1893 | name_extractor.GetStringRef().swap(value); |
| 1894 | // Now convert the HEX bytes into a string value |
| 1895 | name_extractor.GetHexByteString (value); |
| 1896 | thread_name.swap (value); |
| 1897 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1898 | else if (name.compare("name") == 0) |
| 1899 | { |
| 1900 | thread_name.swap (value); |
| 1901 | } |
Greg Clayton | 6f35f5c | 2010-09-09 06:32:46 +0000 | [diff] [blame] | 1902 | else if (name.compare("qaddr") == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1903 | { |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 1904 | thread_dispatch_qaddr = StringConvert::ToUInt64 (value.c_str(), 0, 16); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1905 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1906 | else if (name.compare("reason") == 0) |
| 1907 | { |
| 1908 | reason.swap(value); |
| 1909 | } |
| 1910 | else if (name.compare("description") == 0) |
| 1911 | { |
| 1912 | StringExtractor desc_extractor; |
| 1913 | // Swap "value" over into "name_extractor" |
| 1914 | desc_extractor.GetStringRef().swap(value); |
| 1915 | // Now convert the HEX bytes into a string value |
Chaoren Lin | 28e5742 | 2015-02-03 01:51:25 +0000 | [diff] [blame] | 1916 | desc_extractor.GetHexByteString (value); |
| 1917 | description.swap(value); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1918 | } |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1919 | else if (name.size() == 2 && ::isxdigit(name[0]) && ::isxdigit(name[1])) |
| 1920 | { |
| 1921 | // We have a register number that contains an expedited |
| 1922 | // register value. Lets supply this register to our thread |
| 1923 | // so it won't have to go and read it. |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1924 | if (gdb_thread) |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1925 | { |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 1926 | uint32_t reg = StringConvert::ToUInt32 (name.c_str(), UINT32_MAX, 16); |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1927 | |
| 1928 | if (reg != UINT32_MAX) |
| 1929 | { |
| 1930 | StringExtractor reg_value_extractor; |
| 1931 | // Swap "value" over into "reg_value_extractor" |
| 1932 | reg_value_extractor.GetStringRef().swap(value); |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1933 | if (!gdb_thread->PrivateSetRegisterValue (reg, reg_value_extractor)) |
Greg Clayton | e576ab2 | 2011-02-15 00:19:15 +0000 | [diff] [blame] | 1934 | { |
| 1935 | Host::SetCrashDescriptionWithFormat("Setting thread register '%s' (decoded to %u (0x%x)) with value '%s' for stop packet: '%s'", |
| 1936 | name.c_str(), |
| 1937 | reg, |
| 1938 | reg, |
| 1939 | reg_value_extractor.GetStringRef().c_str(), |
| 1940 | stop_packet.GetStringRef().c_str()); |
| 1941 | } |
Greg Clayton | 3e06bd9 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 1942 | } |
| 1943 | } |
| 1944 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1945 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1946 | |
Hafiz Abid Qadeer | 673b4a3 | 2014-02-20 10:23:20 +0000 | [diff] [blame] | 1947 | // If the response is old style 'S' packet which does not provide us with thread information |
| 1948 | // then update the thread list and choose the first one. |
| 1949 | if (!thread_sp) |
| 1950 | { |
| 1951 | UpdateThreadIDList (); |
| 1952 | |
| 1953 | if (!m_thread_ids.empty ()) |
| 1954 | { |
| 1955 | Mutex::Locker locker (m_thread_list_real.GetMutex ()); |
| 1956 | thread_sp = m_thread_list_real.FindThreadByProtocolID (m_thread_ids.front (), false); |
| 1957 | if (thread_sp) |
| 1958 | gdb_thread = static_cast<ThreadGDBRemote *> (thread_sp.get ()); |
| 1959 | } |
| 1960 | } |
| 1961 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1962 | if (thread_sp) |
| 1963 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 1964 | // Clear the stop info just in case we don't set it to anything |
| 1965 | thread_sp->SetStopInfo (StopInfoSP()); |
| 1966 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1967 | gdb_thread->SetThreadDispatchQAddr (thread_dispatch_qaddr); |
Jim Ingham | dd2fe7a | 2011-01-28 02:23:12 +0000 | [diff] [blame] | 1968 | gdb_thread->SetName (thread_name.empty() ? NULL : thread_name.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1969 | if (exc_type != 0) |
| 1970 | { |
Greg Clayton | 1a65ae1 | 2011-01-25 23:55:37 +0000 | [diff] [blame] | 1971 | const size_t exc_data_size = exc_data.size(); |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1972 | |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1973 | thread_sp->SetStopInfo (StopInfoMachException::CreateStopReasonWithMachException (*thread_sp, |
| 1974 | exc_type, |
| 1975 | exc_data_size, |
| 1976 | exc_data_size >= 1 ? exc_data[0] : 0, |
| 1977 | exc_data_size >= 2 ? exc_data[1] : 0, |
| 1978 | exc_data_size >= 3 ? exc_data[2] : 0)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1979 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1980 | else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1981 | { |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1982 | bool handled = false; |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 1983 | bool did_exec = false; |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1984 | if (!reason.empty()) |
| 1985 | { |
| 1986 | if (reason.compare("trace") == 0) |
| 1987 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1988 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp)); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1989 | handled = true; |
| 1990 | } |
| 1991 | else if (reason.compare("breakpoint") == 0) |
| 1992 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 1993 | addr_t pc = thread_sp->GetRegisterContext()->GetPC(); |
| 1994 | lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 1995 | if (bp_site_sp) |
| 1996 | { |
| 1997 | // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread, |
| 1998 | // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that |
| 1999 | // 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] | 2000 | handled = true; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2001 | if (bp_site_sp->ValidForThisThread (thread_sp.get())) |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2002 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2003 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID())); |
Jim Ingham | 54cc6e4 | 2012-07-11 21:41:19 +0000 | [diff] [blame] | 2004 | } |
| 2005 | else |
| 2006 | { |
| 2007 | StopInfoSP invalid_stop_info_sp; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2008 | thread_sp->SetStopInfo (invalid_stop_info_sp); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2009 | } |
| 2010 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2011 | } |
| 2012 | else if (reason.compare("trap") == 0) |
| 2013 | { |
| 2014 | // Let the trap just use the standard signal stop reason below... |
| 2015 | } |
| 2016 | else if (reason.compare("watchpoint") == 0) |
| 2017 | { |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 2018 | StringExtractor desc_extractor(description.c_str()); |
| 2019 | addr_t wp_addr = desc_extractor.GetU64(LLDB_INVALID_ADDRESS); |
| 2020 | uint32_t wp_index = desc_extractor.GetU32(LLDB_INVALID_INDEX32); |
| 2021 | watch_id_t watch_id = LLDB_INVALID_WATCH_ID; |
| 2022 | if (wp_addr != LLDB_INVALID_ADDRESS) |
| 2023 | { |
| 2024 | WatchpointSP wp_sp = GetTarget().GetWatchpointList().FindByAddress(wp_addr); |
| 2025 | if (wp_sp) |
| 2026 | { |
| 2027 | wp_sp->SetHardwareIndex(wp_index); |
| 2028 | watch_id = wp_sp->GetID(); |
| 2029 | } |
| 2030 | } |
| 2031 | if (watch_id == LLDB_INVALID_WATCH_ID) |
| 2032 | { |
| 2033 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_WATCHPOINTS)); |
| 2034 | if (log) log->Printf ("failed to find watchpoint"); |
| 2035 | } |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2036 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithWatchpointID (*thread_sp, watch_id)); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2037 | handled = true; |
| 2038 | } |
| 2039 | else if (reason.compare("exception") == 0) |
| 2040 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2041 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException(*thread_sp, description.c_str())); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2042 | handled = true; |
| 2043 | } |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 2044 | else if (reason.compare("exec") == 0) |
| 2045 | { |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2046 | did_exec = true; |
Greg Clayton | 15fc2be | 2013-05-21 01:00:52 +0000 | [diff] [blame] | 2047 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithExec(*thread_sp)); |
| 2048 | handled = true; |
| 2049 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2050 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 2051 | |
Jim Ingham | 40083a4 | 2014-02-24 19:49:46 +0000 | [diff] [blame] | 2052 | if (!handled && signo && did_exec == false) |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2053 | { |
| 2054 | if (signo == SIGTRAP) |
| 2055 | { |
| 2056 | // Currently we are going to assume SIGTRAP means we are either |
| 2057 | // hitting a breakpoint or hardware single stepping. |
Jim Ingham | 54cc6e4 | 2012-07-11 21:41:19 +0000 | [diff] [blame] | 2058 | handled = true; |
Hafiz Abid Qadeer | 85a4daf | 2013-10-18 10:04:33 +0000 | [diff] [blame] | 2059 | addr_t pc = thread_sp->GetRegisterContext()->GetPC() + m_breakpoint_pc_offset; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2060 | lldb::BreakpointSiteSP bp_site_sp = thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 2061 | |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2062 | if (bp_site_sp) |
| 2063 | { |
| 2064 | // If the breakpoint is for this thread, then we'll report the hit, but if it is for another thread, |
| 2065 | // we can just report no reason. We don't need to worry about stepping over the breakpoint here, that |
| 2066 | // 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] | 2067 | if (bp_site_sp->ValidForThisThread (thread_sp.get())) |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2068 | { |
Hafiz Abid Qadeer | 85a4daf | 2013-10-18 10:04:33 +0000 | [diff] [blame] | 2069 | if(m_breakpoint_pc_offset != 0) |
| 2070 | thread_sp->GetRegisterContext()->SetPC(pc); |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2071 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithBreakpointSiteID (*thread_sp, bp_site_sp->GetID())); |
Jim Ingham | 54cc6e4 | 2012-07-11 21:41:19 +0000 | [diff] [blame] | 2072 | } |
| 2073 | else |
| 2074 | { |
| 2075 | StopInfoSP invalid_stop_info_sp; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2076 | thread_sp->SetStopInfo (invalid_stop_info_sp); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2077 | } |
| 2078 | } |
Jim Ingham | 54cc6e4 | 2012-07-11 21:41:19 +0000 | [diff] [blame] | 2079 | else |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2080 | { |
Jim Ingham | 4dc613b | 2012-10-27 02:52:04 +0000 | [diff] [blame] | 2081 | // If we were stepping then assume the stop was the result of the trace. If we were |
| 2082 | // not stepping then report the SIGTRAP. |
| 2083 | // 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] | 2084 | if (thread_sp->GetTemporaryResumeState() == eStateStepping) |
| 2085 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonToTrace (*thread_sp)); |
Jim Ingham | 4dc613b | 2012-10-27 02:52:04 +0000 | [diff] [blame] | 2086 | else |
Pavel Labath | c4e25c9 | 2015-05-29 10:13:03 +0000 | [diff] [blame] | 2087 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal(*thread_sp, signo, description.c_str())); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2088 | } |
| 2089 | } |
| 2090 | if (!handled) |
Pavel Labath | c4e25c9 | 2015-05-29 10:13:03 +0000 | [diff] [blame] | 2091 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithSignal (*thread_sp, signo, description.c_str())); |
Jason Molenda | 8214b01 | 2013-04-25 01:33:46 +0000 | [diff] [blame] | 2092 | } |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 2093 | |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2094 | if (!description.empty()) |
| 2095 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2096 | lldb::StopInfoSP stop_info_sp (thread_sp->GetStopInfo ()); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2097 | if (stop_info_sp) |
| 2098 | { |
Pavel Labath | 48fca3b | 2015-05-15 10:14:51 +0000 | [diff] [blame] | 2099 | const char *stop_info_desc = stop_info_sp->GetDescription(); |
| 2100 | if (!stop_info_desc || !stop_info_desc[0]) |
| 2101 | stop_info_sp->SetDescription (description.c_str()); |
Greg Clayton | 3418c85 | 2011-08-10 02:10:13 +0000 | [diff] [blame] | 2102 | } |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2103 | else |
| 2104 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2105 | thread_sp->SetStopInfo (StopInfo::CreateStopReasonWithException (*thread_sp, description.c_str())); |
Greg Clayton | a658fd2 | 2011-06-04 01:26:29 +0000 | [diff] [blame] | 2106 | } |
| 2107 | } |
| 2108 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2109 | } |
| 2110 | return eStateStopped; |
| 2111 | } |
| 2112 | break; |
| 2113 | |
| 2114 | case 'W': |
Todd Fiala | ff6131a | 2014-05-19 04:57:23 +0000 | [diff] [blame] | 2115 | case 'X': |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2116 | // process exited |
| 2117 | return eStateExited; |
| 2118 | |
| 2119 | default: |
| 2120 | break; |
| 2121 | } |
| 2122 | return eStateInvalid; |
| 2123 | } |
| 2124 | |
| 2125 | void |
| 2126 | ProcessGDBRemote::RefreshStateAfterStop () |
| 2127 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 2128 | Mutex::Locker locker(m_thread_list_real.GetMutex()); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2129 | m_thread_ids.clear(); |
| 2130 | // Set the thread stop info. It might have a "threads" key whose value is |
| 2131 | // a list of all thread IDs in the current process, so m_thread_ids might |
| 2132 | // get set. |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 2133 | |
| 2134 | // Scope for the lock |
| 2135 | { |
| 2136 | // Lock the thread stack while we access it |
| 2137 | Mutex::Locker stop_stack_lock(m_last_stop_packet_mutex); |
| 2138 | // Get the number of stop packets on the stack |
| 2139 | int nItems = m_stop_packet_stack.size(); |
| 2140 | // Iterate over them |
| 2141 | for (int i = 0; i < nItems; i++) |
| 2142 | { |
| 2143 | // Get the thread stop info |
| 2144 | StringExtractorGDBRemote stop_info = m_stop_packet_stack[i]; |
| 2145 | // Process thread stop info |
| 2146 | SetThreadStopInfo(stop_info); |
| 2147 | } |
| 2148 | // Clear the thread stop stack |
| 2149 | m_stop_packet_stack.clear(); |
| 2150 | } |
| 2151 | |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2152 | // Check to see if SetThreadStopInfo() filled in m_thread_ids? |
| 2153 | if (m_thread_ids.empty()) |
| 2154 | { |
| 2155 | // No, we need to fetch the thread list manually |
| 2156 | UpdateThreadIDList(); |
| 2157 | } |
| 2158 | |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 2159 | // If we have queried for a default thread id |
| 2160 | if (m_initial_tid != LLDB_INVALID_THREAD_ID) |
| 2161 | { |
| 2162 | m_thread_list.SetSelectedThreadByID(m_initial_tid); |
| 2163 | m_initial_tid = LLDB_INVALID_THREAD_ID; |
| 2164 | } |
| 2165 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2166 | // Let all threads recover from stopping and do any clean up based |
| 2167 | // on the previous thread state (if any). |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 2168 | m_thread_list_real.RefreshStateAfterStop(); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2169 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | Error |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2173 | ProcessGDBRemote::DoHalt (bool &caused_stop) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2174 | { |
| 2175 | Error error; |
Jim Ingham | 0d8bcc7 | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2176 | |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 2177 | bool timed_out = false; |
| 2178 | Mutex::Locker locker; |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2179 | |
| 2180 | if (m_public_state.GetValue() == eStateAttaching) |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 2181 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2182 | // We are being asked to halt during an attach. We need to just close |
| 2183 | // our file handle and debugserver will go away, and we can be done... |
| 2184 | m_gdb_comm.Disconnect(); |
Greg Clayton | 3af9ea5 | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 2185 | } |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2186 | else |
| 2187 | { |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 2188 | if (!m_gdb_comm.SendInterrupt (locker, 2, timed_out)) |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2189 | { |
| 2190 | if (timed_out) |
| 2191 | error.SetErrorString("timed out sending interrupt packet"); |
| 2192 | else |
| 2193 | error.SetErrorString("unknown error sending interrupt packet"); |
| 2194 | } |
Greg Clayton | 2687cd1 | 2012-03-29 01:55:41 +0000 | [diff] [blame] | 2195 | |
| 2196 | caused_stop = m_gdb_comm.GetInterruptWasSent (); |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2197 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2198 | return error; |
| 2199 | } |
| 2200 | |
| 2201 | Error |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2202 | ProcessGDBRemote::DoDetach(bool keep_stopped) |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2203 | { |
| 2204 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2205 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2206 | if (log) |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2207 | log->Printf ("ProcessGDBRemote::DoDetach(keep_stopped: %i)", keep_stopped); |
| 2208 | |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2209 | error = m_gdb_comm.Detach (keep_stopped); |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2210 | if (log) |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2211 | { |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2212 | if (error.Success()) |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2213 | log->PutCString ("ProcessGDBRemote::DoDetach() detach packet sent successfully"); |
| 2214 | else |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2215 | 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] | 2216 | } |
Jim Ingham | acff895 | 2013-05-02 00:27:30 +0000 | [diff] [blame] | 2217 | |
| 2218 | if (!error.Success()) |
| 2219 | return error; |
| 2220 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2221 | // Sleep for one second to let the process get all detached... |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2222 | StopAsyncThread (); |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2223 | |
Greg Clayton | 58d1c9a | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2224 | SetPrivateState (eStateDetached); |
| 2225 | ResumePrivateStateThread(); |
| 2226 | |
| 2227 | //KillDebugserverProcess (); |
Greg Clayton | 594e5ed | 2010-09-27 21:07:38 +0000 | [diff] [blame] | 2228 | return error; |
| 2229 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2230 | |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2231 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2232 | Error |
| 2233 | ProcessGDBRemote::DoDestroy () |
| 2234 | { |
| 2235 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2236 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2237 | if (log) |
| 2238 | log->Printf ("ProcessGDBRemote::DoDestroy()"); |
| 2239 | |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2240 | // There is a bug in older iOS debugservers where they don't shut down the process |
| 2241 | // they are debugging properly. If the process is sitting at a breakpoint or an exception, |
| 2242 | // this can cause problems with restarting. So we check to see if any of our threads are stopped |
| 2243 | // at a breakpoint, and if so we remove all the breakpoints, resume the process, and THEN |
| 2244 | // destroy it again. |
| 2245 | // |
| 2246 | // Note, we don't have a good way to test the version of debugserver, but I happen to know that |
| 2247 | // the set of all the iOS debugservers which don't support GetThreadSuffixSupported() and that of |
| 2248 | // the debugservers with this bug are equal. There really should be a better way to test this! |
| 2249 | // |
| 2250 | // We also use m_destroy_tried_resuming to make sure we only do this once, if we resume and then halt and |
| 2251 | // get called here to destroy again and we're still at a breakpoint or exception, then we should |
| 2252 | // just do the straight-forward kill. |
| 2253 | // |
| 2254 | // And of course, if we weren't able to stop the process by the time we get here, it isn't |
| 2255 | // necessary (or helpful) to do any of this. |
| 2256 | |
| 2257 | if (!m_gdb_comm.GetThreadSuffixSupported() && m_public_state.GetValue() != eStateRunning) |
| 2258 | { |
| 2259 | PlatformSP platform_sp = GetTarget().GetPlatform(); |
| 2260 | |
| 2261 | // FIXME: These should be ConstStrings so we aren't doing strcmp'ing. |
| 2262 | if (platform_sp |
| 2263 | && platform_sp->GetName() |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 2264 | && platform_sp->GetName() == PlatformRemoteiOS::GetPluginNameStatic()) |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2265 | { |
| 2266 | if (m_destroy_tried_resuming) |
| 2267 | { |
| 2268 | if (log) |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 2269 | log->PutCString ("ProcessGDBRemote::DoDestroy() - Tried resuming to destroy once already, not doing it again."); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2270 | } |
| 2271 | else |
| 2272 | { |
| 2273 | // At present, the plans are discarded and the breakpoints disabled Process::Destroy, |
| 2274 | // but we really need it to happen here and it doesn't matter if we do it twice. |
| 2275 | m_thread_list.DiscardThreadPlans(); |
| 2276 | DisableAllBreakpointSites(); |
| 2277 | |
| 2278 | bool stop_looks_like_crash = false; |
| 2279 | ThreadList &threads = GetThreadList(); |
| 2280 | |
| 2281 | { |
Jim Ingham | 4535037 | 2012-09-11 00:08:52 +0000 | [diff] [blame] | 2282 | Mutex::Locker locker(threads.GetMutex()); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2283 | |
| 2284 | size_t num_threads = threads.GetSize(); |
| 2285 | for (size_t i = 0; i < num_threads; i++) |
| 2286 | { |
| 2287 | ThreadSP thread_sp = threads.GetThreadAtIndex(i); |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 2288 | StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo(); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2289 | StopReason reason = eStopReasonInvalid; |
| 2290 | if (stop_info_sp) |
| 2291 | reason = stop_info_sp->GetStopReason(); |
| 2292 | if (reason == eStopReasonBreakpoint |
| 2293 | || reason == eStopReasonException) |
| 2294 | { |
| 2295 | if (log) |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2296 | log->Printf ("ProcessGDBRemote::DoDestroy() - thread: 0x%4.4" PRIx64 " stopped with reason: %s.", |
| 2297 | thread_sp->GetProtocolID(), |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2298 | stop_info_sp->GetDescription()); |
| 2299 | stop_looks_like_crash = true; |
| 2300 | break; |
| 2301 | } |
| 2302 | } |
| 2303 | } |
| 2304 | |
| 2305 | if (stop_looks_like_crash) |
| 2306 | { |
| 2307 | if (log) |
| 2308 | log->PutCString ("ProcessGDBRemote::DoDestroy() - Stopped at a breakpoint, continue and then kill."); |
| 2309 | m_destroy_tried_resuming = true; |
| 2310 | |
| 2311 | // If we are going to run again before killing, it would be good to suspend all the threads |
| 2312 | // before resuming so they won't get into more trouble. Sadly, for the threads stopped with |
| 2313 | // the breakpoint or exception, the exception doesn't get cleared if it is suspended, so we do |
| 2314 | // have to run the risk of letting those threads proceed a bit. |
| 2315 | |
| 2316 | { |
Jim Ingham | 4535037 | 2012-09-11 00:08:52 +0000 | [diff] [blame] | 2317 | Mutex::Locker locker(threads.GetMutex()); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2318 | |
| 2319 | size_t num_threads = threads.GetSize(); |
| 2320 | for (size_t i = 0; i < num_threads; i++) |
| 2321 | { |
| 2322 | ThreadSP thread_sp = threads.GetThreadAtIndex(i); |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 2323 | StopInfoSP stop_info_sp = thread_sp->GetPrivateStopInfo(); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2324 | StopReason reason = eStopReasonInvalid; |
| 2325 | if (stop_info_sp) |
| 2326 | reason = stop_info_sp->GetStopReason(); |
| 2327 | if (reason != eStopReasonBreakpoint |
| 2328 | && reason != eStopReasonException) |
| 2329 | { |
| 2330 | if (log) |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 2331 | log->Printf ("ProcessGDBRemote::DoDestroy() - Suspending thread: 0x%4.4" PRIx64 " before running.", |
| 2332 | thread_sp->GetProtocolID()); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2333 | thread_sp->SetResumeState(eStateSuspended); |
| 2334 | } |
| 2335 | } |
| 2336 | } |
| 2337 | Resume (); |
Jason Molenda | ede3193 | 2015-04-17 05:01:58 +0000 | [diff] [blame] | 2338 | return Destroy(false); |
Jim Ingham | 43c555d | 2012-07-04 00:35:43 +0000 | [diff] [blame] | 2339 | } |
| 2340 | } |
| 2341 | } |
| 2342 | } |
| 2343 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2344 | // Interrupt if our inferior is running... |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2345 | int exit_status = SIGABRT; |
| 2346 | std::string exit_string; |
| 2347 | |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 2348 | if (m_gdb_comm.IsConnected()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2349 | { |
Jim Ingham | aab7837 | 2011-10-28 01:11:35 +0000 | [diff] [blame] | 2350 | if (m_public_state.GetValue() != eStateAttaching) |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2351 | { |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2352 | StringExtractorGDBRemote response; |
| 2353 | bool send_async = true; |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 2354 | GDBRemoteCommunication::ScopedTimeout (m_gdb_comm, 3); |
Filipe Cabecinhas | 9e10605 | 2012-08-22 13:25:58 +0000 | [diff] [blame] | 2355 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2356 | 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] | 2357 | { |
| 2358 | char packet_cmd = response.GetChar(0); |
| 2359 | |
| 2360 | if (packet_cmd == 'W' || packet_cmd == 'X') |
| 2361 | { |
Jim Ingham | eac0aa4 | 2014-02-21 22:36:11 +0000 | [diff] [blame] | 2362 | #if defined(__APPLE__) |
Jim Ingham | 9d67cc5 | 2014-02-21 22:35:29 +0000 | [diff] [blame] | 2363 | // For Native processes on Mac OS X, we launch through the Host Platform, then hand the process off |
| 2364 | // to debugserver, which becomes the parent process through "PT_ATTACH". Then when we go to kill |
| 2365 | // the process on Mac OS X we call ptrace(PT_KILL) to kill it, then we call waitpid which returns |
| 2366 | // with no error and the correct status. But amusingly enough that doesn't seem to actually reap |
| 2367 | // the process, but instead it is left around as a Zombie. Probably the kernel is in the process of |
| 2368 | // switching ownership back to lldb which was the original parent, and gets confused in the handoff. |
| 2369 | // Anyway, so call waitpid here to finally reap it. |
| 2370 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 2371 | if (platform_sp && platform_sp->IsHost()) |
| 2372 | { |
| 2373 | int status; |
| 2374 | ::pid_t reap_pid; |
| 2375 | reap_pid = waitpid (GetID(), &status, WNOHANG); |
| 2376 | if (log) |
| 2377 | log->Printf ("Reaped pid: %d, status: %d.\n", reap_pid, status); |
| 2378 | } |
| 2379 | #endif |
Greg Clayton | 09c3e3d | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 2380 | SetLastStopPacket (response); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 2381 | ClearThreadIDList (); |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2382 | exit_status = response.GetHexU8(); |
| 2383 | } |
| 2384 | else |
| 2385 | { |
| 2386 | if (log) |
| 2387 | log->Printf ("ProcessGDBRemote::DoDestroy - got unexpected response to k packet: %s", response.GetStringRef().c_str()); |
| 2388 | exit_string.assign("got unexpected response to k packet: "); |
| 2389 | exit_string.append(response.GetStringRef()); |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2390 | } |
| 2391 | } |
| 2392 | else |
| 2393 | { |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2394 | if (log) |
| 2395 | log->Printf ("ProcessGDBRemote::DoDestroy - failed to send k packet"); |
| 2396 | exit_string.assign("failed to send the k packet"); |
Greg Clayton | 513c26c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2397 | } |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2398 | } |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2399 | else |
| 2400 | { |
| 2401 | if (log) |
Ed Maste | b3a5333 | 2014-03-17 17:05:22 +0000 | [diff] [blame] | 2402 | log->Printf ("ProcessGDBRemote::DoDestroy - killed or interrupted while attaching"); |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 2403 | exit_string.assign ("killed or interrupted while attaching."); |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2404 | } |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2405 | } |
Jim Ingham | babfc38 | 2012-06-06 00:32:39 +0000 | [diff] [blame] | 2406 | else |
| 2407 | { |
| 2408 | // If we missed setting the exit status on the way out, do it here. |
| 2409 | // NB set exit status can be called multiple times, the first one sets the status. |
| 2410 | exit_string.assign("destroying when not connected to debugserver"); |
| 2411 | } |
| 2412 | |
| 2413 | SetExitStatus(exit_status, exit_string.c_str()); |
| 2414 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2415 | StopAsyncThread (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2416 | KillDebugserverProcess (); |
| 2417 | return error; |
| 2418 | } |
| 2419 | |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2420 | void |
| 2421 | ProcessGDBRemote::SetLastStopPacket (const StringExtractorGDBRemote &response) |
| 2422 | { |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2423 | const bool did_exec = response.GetStringRef().find(";reason:exec;") != std::string::npos; |
| 2424 | if (did_exec) |
| 2425 | { |
| 2426 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
| 2427 | if (log) |
| 2428 | log->Printf ("ProcessGDBRemote::SetLastStopPacket () - detected exec"); |
| 2429 | |
| 2430 | m_thread_list_real.Clear(); |
| 2431 | m_thread_list.Clear(); |
| 2432 | BuildDynamicRegisterInfo (true); |
| 2433 | m_gdb_comm.ResetDiscoverableSettings(); |
| 2434 | } |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 2435 | |
| 2436 | // Scope the lock |
| 2437 | { |
| 2438 | // Lock the thread stack while we access it |
| 2439 | Mutex::Locker stop_stack_lock(m_last_stop_packet_mutex); |
| 2440 | // Add this stop packet to the stop packet stack |
| 2441 | // This stack will get popped and examined when we switch to the |
| 2442 | // Stopped state |
| 2443 | m_stop_packet_stack.push_back(response); |
| 2444 | } |
Greg Clayton | 8cda7f0 | 2013-05-21 21:55:59 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
| 2447 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2448 | //------------------------------------------------------------------ |
| 2449 | // Process Queries |
| 2450 | //------------------------------------------------------------------ |
| 2451 | |
| 2452 | bool |
| 2453 | ProcessGDBRemote::IsAlive () |
| 2454 | { |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 2455 | return m_gdb_comm.IsConnected() && m_private_state.GetValue() != eStateExited; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | addr_t |
| 2459 | ProcessGDBRemote::GetImageInfoAddress() |
| 2460 | { |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 2461 | // request the link map address via the $qShlibInfoAddr packet |
| 2462 | lldb::addr_t addr = m_gdb_comm.GetShlibInfoAddr(); |
| 2463 | |
| 2464 | // the loaded module list can also provides a link map address |
| 2465 | if (addr == LLDB_INVALID_ADDRESS) |
| 2466 | { |
| 2467 | GDBLoadedModuleInfoList list; |
| 2468 | if (GetLoadedModuleList (list).Success()) |
| 2469 | addr = list.m_link_map; |
| 2470 | } |
| 2471 | |
| 2472 | return addr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2475 | //------------------------------------------------------------------ |
| 2476 | // Process Memory |
| 2477 | //------------------------------------------------------------------ |
| 2478 | size_t |
| 2479 | ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 2480 | { |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 2481 | GetMaxMemorySize (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2482 | if (size > m_max_memory_size) |
| 2483 | { |
| 2484 | // Keep memory read sizes down to a sane limit. This function will be |
| 2485 | // called multiple times in order to complete the task by |
| 2486 | // lldb_private::Process so it is ok to do this. |
| 2487 | size = m_max_memory_size; |
| 2488 | } |
| 2489 | |
| 2490 | char packet[64]; |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 2491 | int packet_len; |
| 2492 | bool binary_memory_read = m_gdb_comm.GetxPacketSupported(); |
| 2493 | if (binary_memory_read) |
| 2494 | { |
| 2495 | packet_len = ::snprintf (packet, sizeof(packet), "x0x%" PRIx64 ",0x%" PRIx64, (uint64_t)addr, (uint64_t)size); |
| 2496 | } |
| 2497 | else |
| 2498 | { |
| 2499 | packet_len = ::snprintf (packet, sizeof(packet), "m%" PRIx64 ",%" PRIx64, (uint64_t)addr, (uint64_t)size); |
| 2500 | } |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 2501 | assert (packet_len + 1 < (int)sizeof(packet)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2502 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2503 | 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] | 2504 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2505 | if (response.IsNormalResponse()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2506 | { |
| 2507 | error.Clear(); |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 2508 | if (binary_memory_read) |
| 2509 | { |
| 2510 | // The lower level GDBRemoteCommunication packet receive layer has already de-quoted any |
| 2511 | // 0x7d character escaping that was present in the packet |
| 2512 | |
| 2513 | size_t data_received_size = response.GetBytesLeft(); |
| 2514 | if (data_received_size > size) |
| 2515 | { |
| 2516 | // Don't write past the end of BUF if the remote debug server gave us too |
| 2517 | // much data for some reason. |
| 2518 | data_received_size = size; |
| 2519 | } |
| 2520 | memcpy (buf, response.GetStringRef().data(), data_received_size); |
| 2521 | return data_received_size; |
| 2522 | } |
| 2523 | else |
| 2524 | { |
| 2525 | return response.GetHexBytes(buf, size, '\xdd'); |
| 2526 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2527 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2528 | else if (response.IsErrorResponse()) |
Greg Clayton | b9d5df5 | 2012-12-06 22:49:16 +0000 | [diff] [blame] | 2529 | error.SetErrorStringWithFormat("memory read failed for 0x%" PRIx64, addr); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2530 | else if (response.IsUnsupportedResponse()) |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 2531 | error.SetErrorStringWithFormat("GDB server does not support reading memory"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2532 | else |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 2533 | 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] | 2534 | } |
| 2535 | else |
| 2536 | { |
Jim Ingham | 35579dd | 2013-06-03 19:34:01 +0000 | [diff] [blame] | 2537 | error.SetErrorStringWithFormat("failed to send packet: '%s'", packet); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2538 | } |
| 2539 | return 0; |
| 2540 | } |
| 2541 | |
| 2542 | size_t |
| 2543 | ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 2544 | { |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 2545 | GetMaxMemorySize (); |
Greg Clayton | b4aaf2e | 2011-05-16 02:35:02 +0000 | [diff] [blame] | 2546 | if (size > m_max_memory_size) |
| 2547 | { |
| 2548 | // Keep memory read sizes down to a sane limit. This function will be |
| 2549 | // called multiple times in order to complete the task by |
| 2550 | // lldb_private::Process so it is ok to do this. |
| 2551 | size = m_max_memory_size; |
| 2552 | } |
| 2553 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2554 | StreamString packet; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2555 | packet.Printf("M%" PRIx64 ",%" PRIx64 ":", addr, (uint64_t)size); |
Greg Clayton | 7fb56d0 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 2556 | packet.PutBytesAsRawHex8(buf, size, lldb::endian::InlHostByteOrder(), lldb::endian::InlHostByteOrder()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2557 | StringExtractorGDBRemote response; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 2558 | 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] | 2559 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2560 | if (response.IsOKResponse()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2561 | { |
| 2562 | error.Clear(); |
| 2563 | return size; |
| 2564 | } |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2565 | else if (response.IsErrorResponse()) |
Greg Clayton | b9d5df5 | 2012-12-06 22:49:16 +0000 | [diff] [blame] | 2566 | error.SetErrorStringWithFormat("memory write failed for 0x%" PRIx64, addr); |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 2567 | else if (response.IsUnsupportedResponse()) |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 2568 | error.SetErrorStringWithFormat("GDB server does not support writing memory"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2569 | else |
Greg Clayton | 9944cd7 | 2012-09-19 01:46:31 +0000 | [diff] [blame] | 2570 | 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] | 2571 | } |
| 2572 | else |
| 2573 | { |
Jim Ingham | 35579dd | 2013-06-03 19:34:01 +0000 | [diff] [blame] | 2574 | error.SetErrorStringWithFormat("failed to send packet: '%s'", packet.GetString().c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2575 | } |
| 2576 | return 0; |
| 2577 | } |
| 2578 | |
| 2579 | lldb::addr_t |
| 2580 | ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &error) |
| 2581 | { |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 2582 | Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_EXPRESSIONS)); |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 2583 | addr_t allocated_addr = LLDB_INVALID_ADDRESS; |
| 2584 | |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 2585 | LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory(); |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 2586 | switch (supported) |
| 2587 | { |
| 2588 | case eLazyBoolCalculate: |
| 2589 | case eLazyBoolYes: |
| 2590 | allocated_addr = m_gdb_comm.AllocateMemory (size, permissions); |
| 2591 | if (allocated_addr != LLDB_INVALID_ADDRESS || supported == eLazyBoolYes) |
| 2592 | return allocated_addr; |
| 2593 | |
| 2594 | case eLazyBoolNo: |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 2595 | // Call mmap() to create memory in the inferior.. |
| 2596 | unsigned prot = 0; |
| 2597 | if (permissions & lldb::ePermissionsReadable) |
| 2598 | prot |= eMmapProtRead; |
| 2599 | if (permissions & lldb::ePermissionsWritable) |
| 2600 | prot |= eMmapProtWrite; |
| 2601 | if (permissions & lldb::ePermissionsExecutable) |
| 2602 | prot |= eMmapProtExec; |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 2603 | |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 2604 | if (InferiorCallMmap(this, allocated_addr, 0, size, prot, |
| 2605 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) |
| 2606 | m_addr_to_mmap_size[allocated_addr] = size; |
| 2607 | else |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2608 | { |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 2609 | allocated_addr = LLDB_INVALID_ADDRESS; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2610 | if (log) |
| 2611 | log->Printf ("ProcessGDBRemote::%s no direct stub support for memory allocation, and InferiorCallMmap also failed - is stub missing register context save/restore capability?", __FUNCTION__); |
| 2612 | } |
Greg Clayton | 2a48f52 | 2011-05-14 01:50:35 +0000 | [diff] [blame] | 2613 | break; |
| 2614 | } |
| 2615 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2616 | if (allocated_addr == LLDB_INVALID_ADDRESS) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2617 | 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] | 2618 | else |
| 2619 | error.Clear(); |
| 2620 | return allocated_addr; |
| 2621 | } |
| 2622 | |
| 2623 | Error |
Greg Clayton | 46fb558 | 2011-11-18 07:03:08 +0000 | [diff] [blame] | 2624 | ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr, |
| 2625 | MemoryRegionInfo ®ion_info) |
| 2626 | { |
| 2627 | |
| 2628 | Error error (m_gdb_comm.GetMemoryRegionInfo (load_addr, region_info)); |
| 2629 | return error; |
| 2630 | } |
| 2631 | |
| 2632 | Error |
Johnny Chen | 6463720 | 2012-05-23 21:09:52 +0000 | [diff] [blame] | 2633 | ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num) |
| 2634 | { |
| 2635 | |
| 2636 | Error error (m_gdb_comm.GetWatchpointSupportInfo (num)); |
| 2637 | return error; |
| 2638 | } |
| 2639 | |
| 2640 | Error |
Enrico Granata | f04a219 | 2012-07-13 23:18:48 +0000 | [diff] [blame] | 2641 | ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num, bool& after) |
| 2642 | { |
| 2643 | Error error (m_gdb_comm.GetWatchpointSupportInfo (num, after)); |
| 2644 | return error; |
| 2645 | } |
| 2646 | |
| 2647 | Error |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2648 | ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr) |
| 2649 | { |
| 2650 | Error error; |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 2651 | LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory(); |
| 2652 | |
| 2653 | switch (supported) |
| 2654 | { |
| 2655 | case eLazyBoolCalculate: |
| 2656 | // We should never be deallocating memory without allocating memory |
| 2657 | // first so we should never get eLazyBoolCalculate |
| 2658 | error.SetErrorString ("tried to deallocate memory without ever allocating memory"); |
| 2659 | break; |
| 2660 | |
| 2661 | case eLazyBoolYes: |
| 2662 | if (!m_gdb_comm.DeallocateMemory (addr)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2663 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr); |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 2664 | break; |
| 2665 | |
| 2666 | case eLazyBoolNo: |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 2667 | // Call munmap() to deallocate memory in the inferior.. |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 2668 | { |
| 2669 | MMapMap::iterator pos = m_addr_to_mmap_size.find(addr); |
Peter Collingbourne | 99f9aa0 | 2011-06-03 20:40:38 +0000 | [diff] [blame] | 2670 | if (pos != m_addr_to_mmap_size.end() && |
| 2671 | InferiorCallMunmap(this, addr, pos->second)) |
| 2672 | m_addr_to_mmap_size.erase (pos); |
| 2673 | else |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2674 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr); |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 2675 | } |
| 2676 | break; |
| 2677 | } |
| 2678 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2679 | return error; |
| 2680 | } |
| 2681 | |
| 2682 | |
| 2683 | //------------------------------------------------------------------ |
| 2684 | // Process STDIO |
| 2685 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2686 | size_t |
| 2687 | ProcessGDBRemote::PutSTDIN (const char *src, size_t src_len, Error &error) |
| 2688 | { |
| 2689 | if (m_stdio_communication.IsConnected()) |
| 2690 | { |
| 2691 | ConnectionStatus status; |
| 2692 | m_stdio_communication.Write(src, src_len, status, NULL); |
| 2693 | } |
Vince Harron | df3f00f | 2015-02-10 21:09:04 +0000 | [diff] [blame] | 2694 | else if (m_stdin_forward) |
Vince Harron | e0be425 | 2015-02-06 18:32:57 +0000 | [diff] [blame] | 2695 | { |
| 2696 | m_gdb_comm.SendStdinNotification(src, src_len); |
| 2697 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2698 | return 0; |
| 2699 | } |
| 2700 | |
| 2701 | Error |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2702 | ProcessGDBRemote::EnableBreakpointSite (BreakpointSite *bp_site) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2703 | { |
| 2704 | Error error; |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2705 | assert(bp_site != NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2706 | |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2707 | // Get logging info |
| 2708 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2709 | user_id_t site_id = bp_site->GetID(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2710 | |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2711 | // Get the breakpoint address |
| 2712 | const addr_t addr = bp_site->GetLoadAddress(); |
| 2713 | |
| 2714 | // Log that a breakpoint was requested |
| 2715 | if (log) |
| 2716 | log->Printf("ProcessGDBRemote::EnableBreakpointSite (size_id = %" PRIu64 ") address = 0x%" PRIx64, site_id, (uint64_t)addr); |
| 2717 | |
| 2718 | // Breakpoint already exists and is enabled |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2719 | if (bp_site->IsEnabled()) |
| 2720 | { |
| 2721 | if (log) |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2722 | 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] | 2723 | return error; |
| 2724 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2725 | |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2726 | // Get the software breakpoint trap opcode size |
| 2727 | const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 2728 | |
| 2729 | // SupportsGDBStoppointPacket() simply checks a boolean, indicating if this breakpoint type |
| 2730 | // is supported by the remote stub. These are set to true by default, and later set to false |
| 2731 | // only after we receive an unimplemented response when sending a breakpoint packet. This means |
| 2732 | // initially that unless we were specifically instructed to use a hardware breakpoint, LLDB will |
| 2733 | // attempt to set a software breakpoint. HardwareRequired() also queries a boolean variable which |
| 2734 | // indicates if the user specifically asked for hardware breakpoints. If true then we will |
| 2735 | // skip over software breakpoints. |
| 2736 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware) && (!bp_site->HardwareRequired())) |
| 2737 | { |
| 2738 | // Try to send off a software breakpoint packet ($Z0) |
| 2739 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointSoftware, true, addr, bp_op_size) == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2740 | { |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2741 | // The breakpoint was placed successfully |
| 2742 | bp_site->SetEnabled(true); |
| 2743 | bp_site->SetType(BreakpointSite::eExternal); |
Greg Clayton | eb023e7 | 2013-10-11 19:48:25 +0000 | [diff] [blame] | 2744 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2745 | } |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2746 | |
| 2747 | // SendGDBStoppointTypePacket() will return an error if it was unable to set this |
| 2748 | // breakpoint. We need to differentiate between a error specific to placing this breakpoint |
| 2749 | // or if we have learned that this breakpoint type is unsupported. To do this, we |
| 2750 | // must test the support boolean for this breakpoint type to see if it now indicates that |
| 2751 | // this breakpoint type is unsupported. If they are still supported then we should return |
| 2752 | // with the error code. If they are now unsupported, then we would like to fall through |
| 2753 | // and try another form of breakpoint. |
| 2754 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointSoftware)) |
| 2755 | return error; |
| 2756 | |
| 2757 | // We reach here when software breakpoints have been found to be unsupported. For future |
| 2758 | // calls to set a breakpoint, we will not attempt to set a breakpoint with a type that is |
| 2759 | // known not to be supported. |
| 2760 | if (log) |
| 2761 | log->Printf("Software breakpoints are unsupported"); |
| 2762 | |
| 2763 | // So we will fall through and try a hardware breakpoint |
| 2764 | } |
| 2765 | |
| 2766 | // The process of setting a hardware breakpoint is much the same as above. We check the |
| 2767 | // supported boolean for this breakpoint type, and if it is thought to be supported then we |
| 2768 | // will try to set this breakpoint with a hardware breakpoint. |
| 2769 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointHardware)) |
| 2770 | { |
| 2771 | // Try to send off a hardware breakpoint packet ($Z1) |
| 2772 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, true, addr, bp_op_size) == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2773 | { |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2774 | // The breakpoint was placed successfully |
| 2775 | bp_site->SetEnabled(true); |
| 2776 | bp_site->SetType(BreakpointSite::eHardware); |
| 2777 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2778 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2779 | |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2780 | // Check if the error was something other then an unsupported breakpoint type |
| 2781 | if (m_gdb_comm.SupportsGDBStoppointPacket(eBreakpointHardware)) |
| 2782 | { |
| 2783 | // Unable to set this hardware breakpoint |
| 2784 | error.SetErrorString("failed to set hardware breakpoint (hardware breakpoint resources might be exhausted or unavailable)"); |
| 2785 | return error; |
| 2786 | } |
| 2787 | |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 2788 | // We will reach here when the stub gives an unsupported response to a hardware breakpoint |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2789 | if (log) |
| 2790 | log->Printf("Hardware breakpoints are unsupported"); |
| 2791 | |
| 2792 | // Finally we will falling through to a #trap style breakpoint |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2795 | // Don't fall through when hardware breakpoints were specifically requested |
| 2796 | if (bp_site->HardwareRequired()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2797 | { |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2798 | error.SetErrorString("hardware breakpoints are not supported"); |
| 2799 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2800 | } |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2801 | |
| 2802 | // As a last resort we want to place a manual breakpoint. An instruction |
| 2803 | // is placed into the process memory using memory write packets. |
| 2804 | return EnableSoftwareBreakpoint(bp_site); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | Error |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2808 | ProcessGDBRemote::DisableBreakpointSite (BreakpointSite *bp_site) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2809 | { |
| 2810 | Error error; |
| 2811 | assert (bp_site != NULL); |
| 2812 | addr_t addr = bp_site->GetLoadAddress(); |
| 2813 | user_id_t site_id = bp_site->GetID(); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2814 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_BREAKPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2815 | if (log) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2816 | 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] | 2817 | |
| 2818 | if (bp_site->IsEnabled()) |
| 2819 | { |
| 2820 | const size_t bp_op_size = GetSoftwareBreakpointTrapOpcode (bp_site); |
| 2821 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2822 | BreakpointSite::Type bp_type = bp_site->GetType(); |
| 2823 | switch (bp_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2824 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2825 | case BreakpointSite::eSoftware: |
| 2826 | error = DisableSoftwareBreakpoint (bp_site); |
| 2827 | break; |
| 2828 | |
| 2829 | case BreakpointSite::eHardware: |
Deepak Panickal | b98a2bb | 2014-02-24 11:50:46 +0000 | [diff] [blame] | 2830 | if (m_gdb_comm.SendGDBStoppointTypePacket(eBreakpointHardware, false, addr, bp_op_size)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2831 | error.SetErrorToGenericError(); |
| 2832 | break; |
| 2833 | |
| 2834 | case BreakpointSite::eExternal: |
Jim Ingham | a04ef75 | 2014-03-07 11:18:02 +0000 | [diff] [blame] | 2835 | { |
| 2836 | GDBStoppointType stoppoint_type; |
| 2837 | if (bp_site->IsHardware()) |
| 2838 | stoppoint_type = eBreakpointHardware; |
| 2839 | else |
| 2840 | stoppoint_type = eBreakpointSoftware; |
| 2841 | |
| 2842 | if (m_gdb_comm.SendGDBStoppointTypePacket(stoppoint_type, false, addr, bp_op_size)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2843 | error.SetErrorToGenericError(); |
Jim Ingham | a04ef75 | 2014-03-07 11:18:02 +0000 | [diff] [blame] | 2844 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2845 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2846 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2847 | if (error.Success()) |
| 2848 | bp_site->SetEnabled(false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2849 | } |
| 2850 | else |
| 2851 | { |
| 2852 | if (log) |
Jim Ingham | 299c0c1 | 2013-02-15 02:06:30 +0000 | [diff] [blame] | 2853 | 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] | 2854 | return error; |
| 2855 | } |
| 2856 | |
| 2857 | if (error.Success()) |
| 2858 | error.SetErrorToGenericError(); |
| 2859 | return error; |
| 2860 | } |
| 2861 | |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2862 | // Pre-requisite: wp != NULL. |
| 2863 | static GDBStoppointType |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2864 | GetGDBStoppointType (Watchpoint *wp) |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2865 | { |
| 2866 | assert(wp); |
| 2867 | bool watch_read = wp->WatchpointRead(); |
| 2868 | bool watch_write = wp->WatchpointWrite(); |
| 2869 | |
| 2870 | // watch_read and watch_write cannot both be false. |
| 2871 | assert(watch_read || watch_write); |
| 2872 | if (watch_read && watch_write) |
| 2873 | return eWatchpointReadWrite; |
Johnny Chen | 6d487a9 | 2011-09-09 20:35:15 +0000 | [diff] [blame] | 2874 | else if (watch_read) |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2875 | return eWatchpointRead; |
Johnny Chen | 6d487a9 | 2011-09-09 20:35:15 +0000 | [diff] [blame] | 2876 | else // Must be watch_write, then. |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2877 | return eWatchpointWrite; |
| 2878 | } |
| 2879 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2880 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2881 | ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2882 | { |
| 2883 | Error error; |
| 2884 | if (wp) |
| 2885 | { |
| 2886 | user_id_t watchID = wp->GetID(); |
| 2887 | addr_t addr = wp->GetLoadAddress(); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2888 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2889 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2890 | log->Printf ("ProcessGDBRemote::EnableWatchpoint(watchID = %" PRIu64 ")", watchID); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2891 | if (wp->IsEnabled()) |
| 2892 | { |
| 2893 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2894 | 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] | 2895 | return error; |
| 2896 | } |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2897 | |
| 2898 | GDBStoppointType type = GetGDBStoppointType(wp); |
| 2899 | // Pass down an appropriate z/Z packet... |
| 2900 | if (m_gdb_comm.SupportsGDBStoppointPacket (type)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2901 | { |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2902 | if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, wp->GetByteSize()) == 0) |
| 2903 | { |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2904 | wp->SetEnabled(true, notify); |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2905 | return error; |
| 2906 | } |
| 2907 | else |
| 2908 | error.SetErrorString("sending gdb watchpoint packet failed"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2909 | } |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2910 | else |
| 2911 | error.SetErrorString("watchpoints not supported"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2912 | } |
| 2913 | else |
| 2914 | { |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2915 | error.SetErrorString("Watchpoint argument was NULL."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2916 | } |
| 2917 | if (error.Success()) |
| 2918 | error.SetErrorToGenericError(); |
| 2919 | return error; |
| 2920 | } |
| 2921 | |
| 2922 | Error |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2923 | ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2924 | { |
| 2925 | Error error; |
| 2926 | if (wp) |
| 2927 | { |
| 2928 | user_id_t watchID = wp->GetID(); |
| 2929 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2930 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2931 | |
| 2932 | addr_t addr = wp->GetLoadAddress(); |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2933 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2934 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2935 | 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] | 2936 | |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2937 | if (!wp->IsEnabled()) |
| 2938 | { |
| 2939 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2940 | 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] | 2941 | // See also 'class WatchpointSentry' within StopInfo.cpp. |
| 2942 | // This disabling attempt might come from the user-supplied actions, we'll route it in order for |
| 2943 | // the watchpoint object to intelligently process this action. |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2944 | wp->SetEnabled(false, notify); |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2945 | return error; |
| 2946 | } |
| 2947 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2948 | if (wp->IsHardware()) |
| 2949 | { |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2950 | GDBStoppointType type = GetGDBStoppointType(wp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2951 | // Pass down an appropriate z/Z packet... |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2952 | if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, wp->GetByteSize()) == 0) |
| 2953 | { |
Jim Ingham | 1b5792e | 2012-12-18 02:03:49 +0000 | [diff] [blame] | 2954 | wp->SetEnabled(false, notify); |
Johnny Chen | 11309a3 | 2011-09-06 22:38:36 +0000 | [diff] [blame] | 2955 | return error; |
| 2956 | } |
| 2957 | else |
| 2958 | error.SetErrorString("sending gdb watchpoint packet failed"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2959 | } |
| 2960 | // TODO: clear software watchpoints if we implement them |
| 2961 | } |
| 2962 | else |
| 2963 | { |
Johnny Chen | 01a6786 | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2964 | error.SetErrorString("Watchpoint argument was NULL."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2965 | } |
| 2966 | if (error.Success()) |
| 2967 | error.SetErrorToGenericError(); |
| 2968 | return error; |
| 2969 | } |
| 2970 | |
| 2971 | void |
| 2972 | ProcessGDBRemote::Clear() |
| 2973 | { |
| 2974 | m_flags = 0; |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 2975 | m_thread_list_real.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2976 | m_thread_list.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | Error |
| 2980 | ProcessGDBRemote::DoSignal (int signo) |
| 2981 | { |
| 2982 | Error error; |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 2983 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2984 | if (log) |
| 2985 | log->Printf ("ProcessGDBRemote::DoSignal (signal = %d)", signo); |
| 2986 | |
| 2987 | if (!m_gdb_comm.SendAsyncSignal (signo)) |
| 2988 | error.SetErrorStringWithFormat("failed to send signal %i", signo); |
| 2989 | return error; |
| 2990 | } |
| 2991 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2992 | Error |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 2993 | ProcessGDBRemote::LaunchAndConnectToDebugserver (const ProcessInfo &process_info) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2994 | { |
| 2995 | Error error; |
| 2996 | if (m_debugserver_pid == LLDB_INVALID_PROCESS_ID) |
| 2997 | { |
| 2998 | // If we locate debugserver, keep that located version around |
| 2999 | static FileSpec g_debugserver_file_spec; |
| 3000 | |
Han Ming Ong | 8464704 | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 3001 | ProcessLaunchInfo debugserver_launch_info; |
Oleksiy Vyalov | f8ce61c | 2015-01-28 17:36:59 +0000 | [diff] [blame] | 3002 | // Make debugserver run in its own session so signals generated by |
| 3003 | // special terminal key sequences (^C) don't affect debugserver. |
| 3004 | debugserver_launch_info.SetLaunchInSeparateProcessGroup(true); |
| 3005 | |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 3006 | debugserver_launch_info.SetMonitorProcessCallback (MonitorDebugserverProcess, this, false); |
| 3007 | debugserver_launch_info.SetUserID(process_info.GetUserID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3008 | |
Todd Fiala | 013434e | 2014-07-09 01:29:05 +0000 | [diff] [blame] | 3009 | #if defined (__APPLE__) && (defined (__arm__) || defined (__arm64__) || defined (__aarch64__)) |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 3010 | // On iOS, still do a local connection using a random port |
Greg Clayton | 1681092 | 2014-02-27 19:38:18 +0000 | [diff] [blame] | 3011 | const char *hostname = "127.0.0.1"; |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 3012 | uint16_t port = get_random_port (); |
| 3013 | #else |
| 3014 | // Set hostname being NULL to do the reverse connect where debugserver |
| 3015 | // will bind to port zero and it will communicate back to us the port |
| 3016 | // that we will connect to |
| 3017 | const char *hostname = NULL; |
| 3018 | uint16_t port = 0; |
| 3019 | #endif |
| 3020 | |
| 3021 | error = m_gdb_comm.StartDebugserverProcess (hostname, |
| 3022 | port, |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 3023 | debugserver_launch_info, |
| 3024 | port); |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 3025 | |
| 3026 | if (error.Success ()) |
| 3027 | m_debugserver_pid = debugserver_launch_info.GetProcessID(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3028 | else |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 3029 | m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3030 | |
| 3031 | if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) |
| 3032 | StartAsyncThread (); |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 3033 | |
| 3034 | if (error.Fail()) |
| 3035 | { |
| 3036 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
| 3037 | |
| 3038 | if (log) |
| 3039 | log->Printf("failed to start debugserver process: %s", error.AsCString()); |
| 3040 | return error; |
| 3041 | } |
| 3042 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 3043 | if (m_gdb_comm.IsConnected()) |
| 3044 | { |
| 3045 | // Finish the connection process by doing the handshake without connecting (send NULL URL) |
| 3046 | ConnectToDebugserver (NULL); |
| 3047 | } |
| 3048 | else |
| 3049 | { |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 3050 | StreamString connect_url; |
| 3051 | connect_url.Printf("connect://%s:%u", hostname, port); |
| 3052 | error = ConnectToDebugserver (connect_url.GetString().c_str()); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 3053 | } |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 3054 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3055 | } |
| 3056 | return error; |
| 3057 | } |
| 3058 | |
| 3059 | bool |
| 3060 | ProcessGDBRemote::MonitorDebugserverProcess |
| 3061 | ( |
| 3062 | void *callback_baton, |
| 3063 | lldb::pid_t debugserver_pid, |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 3064 | bool exited, // True if the process did exit |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3065 | int signo, // Zero for no signal |
| 3066 | int exit_status // Exit value of process if signal is zero |
| 3067 | ) |
| 3068 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 3069 | // The baton is a "ProcessGDBRemote *". Now this class might be gone |
| 3070 | // and might not exist anymore, so we need to carefully try to get the |
| 3071 | // target for this process first since we have a race condition when |
| 3072 | // we are done running between getting the notice that the inferior |
| 3073 | // process has died and the debugserver that was debugging this process. |
| 3074 | // In our test suite, we are also continually running process after |
| 3075 | // process, so we must be very careful to make sure: |
| 3076 | // 1 - process object hasn't been deleted already |
| 3077 | // 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] | 3078 | |
| 3079 | // "debugserver_pid" argument passed in is the process ID for |
| 3080 | // debugserver that we are tracking... |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3081 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3082 | |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 3083 | ProcessGDBRemote *process = (ProcessGDBRemote *)callback_baton; |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 3084 | |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 3085 | // Get a shared pointer to the target that has a matching process pointer. |
| 3086 | // This target could be gone, or the target could already have a new process |
| 3087 | // object inside of it |
| 3088 | TargetSP target_sp (Debugger::FindTargetWithProcess(process)); |
| 3089 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 3090 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3091 | 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] | 3092 | |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 3093 | if (target_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3094 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 3095 | // We found a process in a target that matches, but another thread |
| 3096 | // might be in the process of launching a new process that will |
| 3097 | // soon replace it, so get a shared pointer to the process so we |
| 3098 | // can keep it alive. |
| 3099 | ProcessSP process_sp (target_sp->GetProcessSP()); |
| 3100 | // Now we have a shared pointer to the process that can't go away on us |
| 3101 | // so we now make sure it was the same as the one passed in, and also make |
| 3102 | // sure that our previous "process *" didn't get deleted and have a new |
| 3103 | // "process *" created in its place with the same pointer. To verify this |
| 3104 | // we make sure the process has our debugserver process ID. If we pass all |
| 3105 | // of these tests, then we are sure that this process is the one we were |
| 3106 | // looking for. |
| 3107 | 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] | 3108 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 3109 | // Sleep for a half a second to make sure our inferior process has |
| 3110 | // time to set its exit status before we set it incorrectly when |
| 3111 | // both the debugserver and the inferior process shut down. |
| 3112 | usleep (500000); |
| 3113 | // If our process hasn't yet exited, debugserver might have died. |
| 3114 | // If the process did exit, the we are reaping it. |
| 3115 | const StateType state = process->GetState(); |
| 3116 | |
| 3117 | if (process->m_debugserver_pid != LLDB_INVALID_PROCESS_ID && |
| 3118 | state != eStateInvalid && |
| 3119 | state != eStateUnloaded && |
| 3120 | state != eStateExited && |
| 3121 | state != eStateDetached) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3122 | { |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 3123 | char error_str[1024]; |
| 3124 | if (signo) |
| 3125 | { |
| 3126 | const char *signal_cstr = process->GetUnixSignals().GetSignalAsCString (signo); |
| 3127 | if (signal_cstr) |
| 3128 | ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %s", signal_cstr); |
| 3129 | else |
| 3130 | ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with signal %i", signo); |
| 3131 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3132 | else |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 3133 | { |
| 3134 | ::snprintf (error_str, sizeof (error_str), DEBUGSERVER_BASENAME " died with an exit status of 0x%8.8x", exit_status); |
| 3135 | } |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 3136 | |
Greg Clayton | e4e4592 | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 3137 | process->SetExitStatus (-1, error_str); |
| 3138 | } |
| 3139 | // Debugserver has exited we need to let our ProcessGDBRemote |
| 3140 | // know that it no longer has a debugserver instance |
| 3141 | process->m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 3142 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3143 | } |
| 3144 | return true; |
| 3145 | } |
| 3146 | |
| 3147 | void |
| 3148 | ProcessGDBRemote::KillDebugserverProcess () |
| 3149 | { |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 3150 | m_gdb_comm.Disconnect(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3151 | if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID) |
| 3152 | { |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 3153 | Host::Kill (m_debugserver_pid, SIGINT); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3154 | m_debugserver_pid = LLDB_INVALID_PROCESS_ID; |
| 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | void |
| 3159 | ProcessGDBRemote::Initialize() |
| 3160 | { |
Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 3161 | static std::once_flag g_once_flag; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3162 | |
Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 3163 | std::call_once(g_once_flag, []() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3164 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3165 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 3166 | GetPluginDescriptionStatic(), |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 3167 | CreateInstance, |
| 3168 | DebuggerInitialize); |
Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 3169 | }); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 3172 | void |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 3173 | ProcessGDBRemote::DebuggerInitialize (Debugger &debugger) |
Greg Clayton | 7f98240 | 2013-07-15 22:54:20 +0000 | [diff] [blame] | 3174 | { |
| 3175 | if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName())) |
| 3176 | { |
| 3177 | const bool is_global_setting = true; |
| 3178 | PluginManager::CreateSettingForProcessPlugin (debugger, |
| 3179 | GetGlobalPluginProperties()->GetValueProperties(), |
| 3180 | ConstString ("Properties for the gdb-remote process plug-in."), |
| 3181 | is_global_setting); |
| 3182 | } |
| 3183 | } |
| 3184 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3185 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3186 | ProcessGDBRemote::StartAsyncThread () |
| 3187 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3188 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3189 | |
| 3190 | if (log) |
| 3191 | log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__); |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 3192 | |
| 3193 | Mutex::Locker start_locker(m_async_thread_state_mutex); |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3194 | if (!m_async_thread.IsJoinable()) |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 3195 | { |
| 3196 | // Create a thread that watches our internal state and controls which |
| 3197 | // events make it to clients (into the DCProcess event queue). |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 3198 | |
| 3199 | m_async_thread = ThreadLauncher::LaunchThread("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL); |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 3200 | } |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3201 | else if (log) |
| 3202 | log->Printf("ProcessGDBRemote::%s () - Called when Async thread was already running.", __FUNCTION__); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 3203 | |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3204 | return m_async_thread.IsJoinable(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3205 | } |
| 3206 | |
| 3207 | void |
| 3208 | ProcessGDBRemote::StopAsyncThread () |
| 3209 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3210 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3211 | |
| 3212 | if (log) |
| 3213 | log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__); |
| 3214 | |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 3215 | Mutex::Locker start_locker(m_async_thread_state_mutex); |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3216 | if (m_async_thread.IsJoinable()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3217 | { |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 3218 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit); |
| 3219 | |
| 3220 | // This will shut down the async thread. |
| 3221 | m_gdb_comm.Disconnect(); // Disconnect from the debug server. |
| 3222 | |
| 3223 | // Stop the stdio thread |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 3224 | m_async_thread.Join(nullptr); |
Pavel Labath | a55a953 | 2015-03-11 09:53:42 +0000 | [diff] [blame] | 3225 | m_async_thread.Reset(); |
Jim Ingham | 455fa5c | 2012-11-01 01:15:33 +0000 | [diff] [blame] | 3226 | } |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3227 | else if (log) |
| 3228 | log->Printf("ProcessGDBRemote::%s () - Called when Async thread was not running.", __FUNCTION__); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
| 3231 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 3232 | thread_result_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3233 | ProcessGDBRemote::AsyncThread (void *arg) |
| 3234 | { |
| 3235 | ProcessGDBRemote *process = (ProcessGDBRemote*) arg; |
| 3236 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 3237 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3238 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3239 | 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] | 3240 | |
| 3241 | Listener listener ("ProcessGDBRemote::AsyncThread"); |
| 3242 | EventSP event_sp; |
| 3243 | const uint32_t desired_event_mask = eBroadcastBitAsyncContinue | |
| 3244 | eBroadcastBitAsyncThreadShouldExit; |
| 3245 | |
| 3246 | if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask) |
| 3247 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3248 | listener.StartListeningForEvents (&process->m_gdb_comm, Communication::eBroadcastBitReadThreadDidExit); |
| 3249 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3250 | bool done = false; |
| 3251 | while (!done) |
| 3252 | { |
| 3253 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3254 | 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] | 3255 | if (listener.WaitForEvent (NULL, event_sp)) |
| 3256 | { |
| 3257 | const uint32_t event_type = event_sp->GetType(); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3258 | if (event_sp->BroadcasterIs (&process->m_async_broadcaster)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3259 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3260 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3261 | 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] | 3262 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3263 | switch (event_type) |
| 3264 | { |
| 3265 | case eBroadcastBitAsyncContinue: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3266 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3267 | const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3268 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3269 | if (continue_packet) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3270 | { |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3271 | const char *continue_cstr = (const char *)continue_packet->GetBytes (); |
| 3272 | const size_t continue_cstr_len = continue_packet->GetByteSize (); |
| 3273 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3274 | 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] | 3275 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3276 | if (::strstr (continue_cstr, "vAttach") == NULL) |
| 3277 | process->SetPrivateState(eStateRunning); |
| 3278 | StringExtractorGDBRemote response; |
| 3279 | 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] | 3280 | |
Greg Clayton | 0772ded | 2012-05-16 02:48:06 +0000 | [diff] [blame] | 3281 | // We need to immediately clear the thread ID list so we are sure to get a valid list of threads. |
| 3282 | // The thread ID list might be contained within the "response", or the stop reply packet that |
| 3283 | // caused the stop. So clear it now before we give the stop reply packet to the process |
| 3284 | // using the process->SetLastStopPacket()... |
| 3285 | process->ClearThreadIDList (); |
| 3286 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3287 | switch (stop_state) |
| 3288 | { |
| 3289 | case eStateStopped: |
| 3290 | case eStateCrashed: |
| 3291 | case eStateSuspended: |
Greg Clayton | 09c3e3d | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 3292 | process->SetLastStopPacket (response); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3293 | process->SetPrivateState (stop_state); |
| 3294 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3295 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3296 | case eStateExited: |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 3297 | { |
Greg Clayton | 09c3e3d | 2011-12-06 04:51:14 +0000 | [diff] [blame] | 3298 | process->SetLastStopPacket (response); |
Greg Clayton | 9e92090 | 2012-04-10 02:25:43 +0000 | [diff] [blame] | 3299 | process->ClearThreadIDList(); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3300 | response.SetFilePos(1); |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 3301 | |
| 3302 | int exit_status = response.GetHexU8(); |
| 3303 | const char *desc_cstr = NULL; |
| 3304 | StringExtractor extractor; |
| 3305 | std::string desc_string; |
| 3306 | if (response.GetBytesLeft() > 0 && response.GetChar('-') == ';') |
| 3307 | { |
| 3308 | std::string desc_token; |
| 3309 | while (response.GetNameColonValue (desc_token, desc_string)) |
| 3310 | { |
| 3311 | if (desc_token == "description") |
| 3312 | { |
| 3313 | extractor.GetStringRef().swap(desc_string); |
| 3314 | extractor.SetFilePos(0); |
| 3315 | extractor.GetHexByteString (desc_string); |
| 3316 | desc_cstr = desc_string.c_str(); |
| 3317 | } |
| 3318 | } |
| 3319 | } |
| 3320 | process->SetExitStatus(exit_status, desc_cstr); |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3321 | done = true; |
| 3322 | break; |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 3323 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3324 | case eStateInvalid: |
| 3325 | process->SetExitStatus(-1, "lost connection"); |
| 3326 | break; |
| 3327 | |
| 3328 | default: |
| 3329 | process->SetPrivateState (stop_state); |
| 3330 | break; |
| 3331 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3332 | } |
| 3333 | } |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3334 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3335 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3336 | case eBroadcastBitAsyncThreadShouldExit: |
| 3337 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3338 | 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] | 3339 | done = true; |
| 3340 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3341 | |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3342 | default: |
| 3343 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3344 | 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] | 3345 | done = true; |
| 3346 | break; |
| 3347 | } |
| 3348 | } |
| 3349 | else if (event_sp->BroadcasterIs (&process->m_gdb_comm)) |
| 3350 | { |
| 3351 | if (event_type & Communication::eBroadcastBitReadThreadDidExit) |
| 3352 | { |
| 3353 | process->SetExitStatus (-1, "lost connection"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3354 | done = true; |
Greg Clayton | 7133762 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 3355 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3356 | } |
| 3357 | } |
| 3358 | else |
| 3359 | { |
| 3360 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3361 | 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] | 3362 | done = true; |
| 3363 | } |
| 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 3368 | 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] | 3369 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3370 | return NULL; |
| 3371 | } |
| 3372 | |
Greg Clayton | e996fd3 | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3373 | //uint32_t |
| 3374 | //ProcessGDBRemote::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) |
| 3375 | //{ |
| 3376 | // // If we are planning to launch the debugserver remotely, then we need to fire up a debugserver |
| 3377 | // // process and ask it for the list of processes. But if we are local, we can let the Host do it. |
| 3378 | // if (m_local_debugserver) |
| 3379 | // { |
| 3380 | // return Host::ListProcessesMatchingName (name, matches, pids); |
| 3381 | // } |
| 3382 | // else |
| 3383 | // { |
| 3384 | // // FIXME: Implement talking to the remote debugserver. |
| 3385 | // return 0; |
| 3386 | // } |
| 3387 | // |
| 3388 | //} |
| 3389 | // |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3390 | bool |
| 3391 | ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 3392 | StoppointCallbackContext *context, |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3393 | lldb::user_id_t break_id, |
| 3394 | lldb::user_id_t break_loc_id) |
| 3395 | { |
| 3396 | // I don't think I have to do anything here, just make sure I notice the new thread when it starts to |
| 3397 | // run so I can stop it if that's what I want to do. |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 3398 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3399 | if (log) |
| 3400 | log->Printf("Hit New Thread Notification breakpoint."); |
| 3401 | return false; |
| 3402 | } |
| 3403 | |
| 3404 | |
| 3405 | bool |
| 3406 | ProcessGDBRemote::StartNoticingNewThreads() |
| 3407 | { |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 3408 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3409 | if (m_thread_create_bp_sp) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3410 | { |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3411 | if (log && log->GetVerbose()) |
| 3412 | log->Printf("Enabled noticing new thread breakpoint."); |
| 3413 | m_thread_create_bp_sp->SetEnabled(true); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3414 | } |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3415 | else |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3416 | { |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3417 | PlatformSP platform_sp (m_target.GetPlatform()); |
| 3418 | if (platform_sp) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3419 | { |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3420 | m_thread_create_bp_sp = platform_sp->SetThreadCreationBreakpoint(m_target); |
| 3421 | if (m_thread_create_bp_sp) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3422 | { |
Jim Ingham | 37cfeab | 2011-10-15 00:21:37 +0000 | [diff] [blame] | 3423 | if (log && log->GetVerbose()) |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3424 | log->Printf("Successfully created new thread notification breakpoint %i", m_thread_create_bp_sp->GetID()); |
| 3425 | m_thread_create_bp_sp->SetCallback (ProcessGDBRemote::NewThreadNotifyBreakpointHit, this, true); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3426 | } |
| 3427 | else |
| 3428 | { |
| 3429 | if (log) |
| 3430 | log->Printf("Failed to create new thread notification breakpoint."); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3431 | } |
| 3432 | } |
| 3433 | } |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3434 | return m_thread_create_bp_sp.get() != NULL; |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3435 | } |
| 3436 | |
| 3437 | bool |
| 3438 | ProcessGDBRemote::StopNoticingNewThreads() |
| 3439 | { |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 3440 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 37cfeab | 2011-10-15 00:21:37 +0000 | [diff] [blame] | 3441 | if (log && log->GetVerbose()) |
Jim Ingham | 0e97cbc | 2011-02-08 05:19:01 +0000 | [diff] [blame] | 3442 | log->Printf ("Disabling new thread notification breakpoint."); |
Greg Clayton | 4116e93 | 2012-05-15 02:33:01 +0000 | [diff] [blame] | 3443 | |
| 3444 | if (m_thread_create_bp_sp) |
| 3445 | m_thread_create_bp_sp->SetEnabled(false); |
| 3446 | |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3447 | return true; |
| 3448 | } |
| 3449 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 3450 | DynamicLoader * |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 3451 | ProcessGDBRemote::GetDynamicLoader () |
| 3452 | { |
| 3453 | if (m_dyld_ap.get() == NULL) |
Jason Molenda | 2e56a25 | 2013-05-11 03:09:05 +0000 | [diff] [blame] | 3454 | m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL)); |
Jason Molenda | 5e8534e | 2012-10-03 01:29:34 +0000 | [diff] [blame] | 3455 | return m_dyld_ap.get(); |
| 3456 | } |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 3457 | |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 3458 | Error |
| 3459 | ProcessGDBRemote::SendEventData(const char *data) |
| 3460 | { |
| 3461 | int return_value; |
| 3462 | bool was_supported; |
| 3463 | |
| 3464 | Error error; |
| 3465 | |
| 3466 | return_value = m_gdb_comm.SendLaunchEventDataPacket (data, &was_supported); |
| 3467 | if (return_value != 0) |
| 3468 | { |
| 3469 | if (!was_supported) |
| 3470 | error.SetErrorString("Sending events is not supported for this process."); |
| 3471 | else |
| 3472 | error.SetErrorStringWithFormat("Error sending event data: %d.", return_value); |
| 3473 | } |
| 3474 | return error; |
| 3475 | } |
| 3476 | |
Steve Pucci | 03904ac | 2014-03-04 23:18:46 +0000 | [diff] [blame] | 3477 | const DataBufferSP |
| 3478 | ProcessGDBRemote::GetAuxvData() |
| 3479 | { |
| 3480 | DataBufferSP buf; |
| 3481 | if (m_gdb_comm.GetQXferAuxvReadSupported()) |
| 3482 | { |
| 3483 | std::string response_string; |
| 3484 | if (m_gdb_comm.SendPacketsAndConcatenateResponses("qXfer:auxv:read::", response_string) == GDBRemoteCommunication::PacketResult::Success) |
| 3485 | buf.reset(new DataBufferHeap(response_string.c_str(), response_string.length())); |
| 3486 | } |
| 3487 | return buf; |
| 3488 | } |
| 3489 | |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 3490 | StructuredData::ObjectSP |
| 3491 | ProcessGDBRemote::GetExtendedInfoForThread (lldb::tid_t tid) |
| 3492 | { |
| 3493 | StructuredData::ObjectSP object_sp; |
| 3494 | |
| 3495 | if (m_gdb_comm.GetThreadExtendedInfoSupported()) |
| 3496 | { |
| 3497 | StructuredData::ObjectSP args_dict(new StructuredData::Dictionary()); |
| 3498 | SystemRuntime *runtime = GetSystemRuntime(); |
| 3499 | if (runtime) |
| 3500 | { |
| 3501 | runtime->AddThreadExtendedInfoPacketHints (args_dict); |
| 3502 | } |
| 3503 | args_dict->GetAsDictionary()->AddIntegerItem ("thread", tid); |
| 3504 | |
| 3505 | StreamString packet; |
| 3506 | packet << "jThreadExtendedInfo:"; |
| 3507 | args_dict->Dump (packet); |
| 3508 | |
| 3509 | // FIXME the final character of a JSON dictionary, '}', is the escape |
| 3510 | // character in gdb-remote binary mode. lldb currently doesn't escape |
| 3511 | // these characters in its packet output -- so we add the quoted version |
| 3512 | // of the } character here manually in case we talk to a debugserver which |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 3513 | // un-escapes the characters at packet read time. |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 3514 | packet << (char) (0x7d ^ 0x20); |
| 3515 | |
| 3516 | StringExtractorGDBRemote response; |
| 3517 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetData(), packet.GetSize(), response, false) == GDBRemoteCommunication::PacketResult::Success) |
| 3518 | { |
| 3519 | StringExtractorGDBRemote::ResponseType response_type = response.GetResponseType(); |
| 3520 | if (response_type == StringExtractorGDBRemote::eResponse) |
| 3521 | { |
| 3522 | if (!response.Empty()) |
| 3523 | { |
| 3524 | // The packet has already had the 0x7d xor quoting stripped out at the |
| 3525 | // GDBRemoteCommunication packet receive level. |
| 3526 | object_sp = StructuredData::ParseJSON (response.GetStringRef()); |
| 3527 | } |
| 3528 | } |
| 3529 | } |
| 3530 | } |
| 3531 | return object_sp; |
| 3532 | } |
| 3533 | |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3534 | // Establish the largest memory read/write payloads we should use. |
| 3535 | // If the remote stub has a max packet size, stay under that size. |
| 3536 | // |
| 3537 | // If the remote stub's max packet size is crazy large, use a |
| 3538 | // reasonable largeish default. |
| 3539 | // |
| 3540 | // If the remote stub doesn't advertise a max packet size, use a |
| 3541 | // conservative default. |
| 3542 | |
| 3543 | void |
| 3544 | ProcessGDBRemote::GetMaxMemorySize() |
| 3545 | { |
| 3546 | const uint64_t reasonable_largeish_default = 128 * 1024; |
| 3547 | const uint64_t conservative_default = 512; |
| 3548 | |
| 3549 | if (m_max_memory_size == 0) |
| 3550 | { |
| 3551 | uint64_t stub_max_size = m_gdb_comm.GetRemoteMaxPacketSize(); |
| 3552 | if (stub_max_size != UINT64_MAX && stub_max_size != 0) |
| 3553 | { |
| 3554 | // Save the stub's claimed maximum packet size |
| 3555 | m_remote_stub_max_memory_size = stub_max_size; |
| 3556 | |
| 3557 | // Even if the stub says it can support ginormous packets, |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 3558 | // don't exceed our reasonable largeish default packet size. |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 3559 | if (stub_max_size > reasonable_largeish_default) |
| 3560 | { |
| 3561 | stub_max_size = reasonable_largeish_default; |
| 3562 | } |
| 3563 | |
| 3564 | m_max_memory_size = stub_max_size; |
| 3565 | } |
| 3566 | else |
| 3567 | { |
| 3568 | m_max_memory_size = conservative_default; |
| 3569 | } |
| 3570 | } |
| 3571 | } |
| 3572 | |
| 3573 | void |
| 3574 | ProcessGDBRemote::SetUserSpecifiedMaxMemoryTransferSize (uint64_t user_specified_max) |
| 3575 | { |
| 3576 | if (user_specified_max != 0) |
| 3577 | { |
| 3578 | GetMaxMemorySize (); |
| 3579 | |
| 3580 | if (m_remote_stub_max_memory_size != 0) |
| 3581 | { |
| 3582 | if (m_remote_stub_max_memory_size < user_specified_max) |
| 3583 | { |
| 3584 | m_max_memory_size = m_remote_stub_max_memory_size; // user specified a packet size too big, go as big |
| 3585 | // as the remote stub says we can go. |
| 3586 | } |
| 3587 | else |
| 3588 | { |
| 3589 | m_max_memory_size = user_specified_max; // user's packet size is good |
| 3590 | } |
| 3591 | } |
| 3592 | else |
| 3593 | { |
| 3594 | m_max_memory_size = user_specified_max; // user's packet size is probably fine |
| 3595 | } |
| 3596 | } |
| 3597 | } |
| 3598 | |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 3599 | bool |
| 3600 | ProcessGDBRemote::GetModuleSpec(const FileSpec& module_file_spec, |
| 3601 | const ArchSpec& arch, |
| 3602 | ModuleSpec &module_spec) |
| 3603 | { |
| 3604 | Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PLATFORM); |
| 3605 | |
| 3606 | if (!m_gdb_comm.GetModuleInfo (module_file_spec, arch, module_spec)) |
| 3607 | { |
| 3608 | if (log) |
| 3609 | log->Printf ("ProcessGDBRemote::%s - failed to get module info for %s:%s", |
| 3610 | __FUNCTION__, module_file_spec.GetPath ().c_str (), |
| 3611 | arch.GetTriple ().getTriple ().c_str ()); |
| 3612 | return false; |
| 3613 | } |
| 3614 | |
| 3615 | if (log) |
| 3616 | { |
| 3617 | StreamString stream; |
| 3618 | module_spec.Dump (stream); |
| 3619 | log->Printf ("ProcessGDBRemote::%s - got module info for (%s:%s) : %s", |
| 3620 | __FUNCTION__, module_file_spec.GetPath ().c_str (), |
| 3621 | arch.GetTriple ().getTriple ().c_str (), stream.GetString ().c_str ()); |
| 3622 | } |
| 3623 | |
| 3624 | return true; |
| 3625 | } |
| 3626 | |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3627 | namespace { |
| 3628 | |
| 3629 | typedef std::vector<std::string> stringVec; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3630 | |
| 3631 | typedef std::vector<struct GdbServerRegisterInfo> GDBServerRegisterVec; |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3632 | struct RegisterSetInfo |
| 3633 | { |
| 3634 | ConstString name; |
| 3635 | }; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3636 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3637 | typedef std::map<uint32_t, RegisterSetInfo> RegisterSetMap; |
| 3638 | |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 3639 | struct GdbServerTargetInfo |
| 3640 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3641 | std::string arch; |
| 3642 | std::string osabi; |
| 3643 | stringVec includes; |
| 3644 | RegisterSetMap reg_set_map; |
| 3645 | XMLNode feature_node; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3646 | }; |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3647 | |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3648 | bool |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3649 | ParseRegisters (XMLNode feature_node, GdbServerTargetInfo &target_info, GDBRemoteDynamicRegisterInfo &dyn_reg_info) |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 3650 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3651 | if (!feature_node) |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 3652 | return false; |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3653 | |
| 3654 | uint32_t prev_reg_num = 0; |
| 3655 | uint32_t reg_offset = 0; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3656 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3657 | feature_node.ForEachChildElementWithName("reg", [&target_info, &dyn_reg_info, &prev_reg_num, ®_offset](const XMLNode ®_node) -> bool { |
| 3658 | std::string name; |
| 3659 | std::string gdb_group; |
| 3660 | std::string gdb_type; |
| 3661 | ConstString reg_name; |
| 3662 | ConstString alt_name; |
| 3663 | ConstString set_name; |
| 3664 | std::vector<uint32_t> value_regs; |
| 3665 | std::vector<uint32_t> invalidate_regs; |
| 3666 | bool encoding_set = false; |
| 3667 | bool format_set = false; |
| 3668 | RegisterInfo reg_info = { NULL, // Name |
| 3669 | NULL, // Alt name |
| 3670 | 0, // byte size |
| 3671 | reg_offset, // offset |
| 3672 | eEncodingUint, // encoding |
| 3673 | eFormatHex, // formate |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 3674 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3675 | LLDB_INVALID_REGNUM, // GCC reg num |
| 3676 | LLDB_INVALID_REGNUM, // DWARF reg num |
| 3677 | LLDB_INVALID_REGNUM, // generic reg num |
| 3678 | prev_reg_num, // GDB reg num |
| 3679 | prev_reg_num // native register number |
| 3680 | }, |
| 3681 | NULL, |
| 3682 | NULL |
| 3683 | }; |
| 3684 | |
| 3685 | reg_node.ForEachAttribute([&target_info, &name, &gdb_group, &gdb_type, ®_name, &alt_name, &set_name, &value_regs, &invalidate_regs, &encoding_set, &format_set, ®_info, &prev_reg_num, ®_offset](const llvm::StringRef &name, const llvm::StringRef &value) -> bool { |
| 3686 | if (name == "name") |
| 3687 | { |
| 3688 | reg_name.SetString(value); |
| 3689 | } |
| 3690 | else if (name == "bitsize") |
| 3691 | { |
| 3692 | reg_info.byte_size = StringConvert::ToUInt32(value.data(), 0, 0) / CHAR_BIT; |
| 3693 | } |
| 3694 | else if (name == "type") |
| 3695 | { |
| 3696 | gdb_type = value.str(); |
| 3697 | } |
| 3698 | else if (name == "group") |
| 3699 | { |
| 3700 | gdb_group = value.str(); |
| 3701 | } |
| 3702 | else if (name == "regnum") |
| 3703 | { |
| 3704 | const uint32_t regnum = StringConvert::ToUInt32(value.data(), LLDB_INVALID_REGNUM, 0); |
| 3705 | if (regnum != LLDB_INVALID_REGNUM) |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 3706 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3707 | reg_info.kinds[eRegisterKindGDB] = regnum; |
| 3708 | reg_info.kinds[eRegisterKindLLDB] = regnum; |
| 3709 | prev_reg_num = regnum; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3710 | } |
| 3711 | } |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3712 | else if (name == "offset") |
| 3713 | { |
| 3714 | reg_offset = StringConvert::ToUInt32(value.data(), UINT32_MAX, 0); |
| 3715 | } |
| 3716 | else if (name == "altname") |
| 3717 | { |
| 3718 | alt_name.SetString(value); |
| 3719 | } |
| 3720 | else if (name == "encoding") |
| 3721 | { |
| 3722 | encoding_set = true; |
| 3723 | reg_info.encoding = Args::StringToEncoding (value.data(), eEncodingUint); |
| 3724 | } |
| 3725 | else if (name == "format") |
| 3726 | { |
| 3727 | format_set = true; |
| 3728 | Format format = eFormatInvalid; |
| 3729 | if (Args::StringToFormat (value.data(), format, NULL).Success()) |
| 3730 | reg_info.format = format; |
| 3731 | else if (value == "vector-sint8") |
| 3732 | reg_info.format = eFormatVectorOfSInt8; |
| 3733 | else if (value == "vector-uint8") |
| 3734 | reg_info.format = eFormatVectorOfUInt8; |
| 3735 | else if (value == "vector-sint16") |
| 3736 | reg_info.format = eFormatVectorOfSInt16; |
| 3737 | else if (value == "vector-uint16") |
| 3738 | reg_info.format = eFormatVectorOfUInt16; |
| 3739 | else if (value == "vector-sint32") |
| 3740 | reg_info.format = eFormatVectorOfSInt32; |
| 3741 | else if (value == "vector-uint32") |
| 3742 | reg_info.format = eFormatVectorOfUInt32; |
| 3743 | else if (value == "vector-float32") |
| 3744 | reg_info.format = eFormatVectorOfFloat32; |
| 3745 | else if (value == "vector-uint128") |
| 3746 | reg_info.format = eFormatVectorOfUInt128; |
| 3747 | } |
| 3748 | else if (name == "group_id") |
| 3749 | { |
| 3750 | const uint32_t set_id = StringConvert::ToUInt32(value.data(), UINT32_MAX, 0); |
| 3751 | RegisterSetMap::const_iterator pos = target_info.reg_set_map.find(set_id); |
| 3752 | if (pos != target_info.reg_set_map.end()) |
| 3753 | set_name = pos->second.name; |
| 3754 | } |
| 3755 | else if (name == "gcc_regnum") |
| 3756 | { |
| 3757 | reg_info.kinds[eRegisterKindGCC] = StringConvert::ToUInt32(value.data(), LLDB_INVALID_REGNUM, 0); |
| 3758 | } |
| 3759 | else if (name == "dwarf_regnum") |
| 3760 | { |
| 3761 | reg_info.kinds[eRegisterKindDWARF] = StringConvert::ToUInt32(value.data(), LLDB_INVALID_REGNUM, 0); |
| 3762 | } |
| 3763 | else if (name == "generic") |
| 3764 | { |
| 3765 | reg_info.kinds[eRegisterKindGeneric] = Args::StringToGenericRegister(value.data()); |
| 3766 | } |
| 3767 | else if (name == "value_regnums") |
| 3768 | { |
| 3769 | SplitCommaSeparatedRegisterNumberString(value, value_regs, 0); |
| 3770 | } |
| 3771 | else if (name == "invalidate_regnums") |
| 3772 | { |
| 3773 | SplitCommaSeparatedRegisterNumberString(value, invalidate_regs, 0); |
| 3774 | } |
| 3775 | else |
| 3776 | { |
| 3777 | printf("unhandled attribute %s = %s\n", name.data(), value.data()); |
| 3778 | } |
| 3779 | return true; // Keep iterating through all attributes |
| 3780 | }); |
| 3781 | |
| 3782 | if (!gdb_type.empty() && !(encoding_set || format_set)) |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 3783 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3784 | if (gdb_type.find("int") == 0) |
| 3785 | { |
| 3786 | reg_info.format = eFormatHex; |
| 3787 | reg_info.encoding = eEncodingUint; |
| 3788 | } |
| 3789 | else if (gdb_type == "data_ptr" || gdb_type == "code_ptr") |
| 3790 | { |
| 3791 | reg_info.format = eFormatAddressInfo; |
| 3792 | reg_info.encoding = eEncodingUint; |
| 3793 | } |
| 3794 | else if (gdb_type == "i387_ext" || gdb_type == "float") |
| 3795 | { |
| 3796 | reg_info.format = eFormatFloat; |
| 3797 | reg_info.encoding = eEncodingIEEE754; |
| 3798 | } |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3799 | } |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3800 | |
| 3801 | // Only update the register set name if we didn't get a "reg_set" attribute. |
| 3802 | // "set_name" will be empty if we didn't have a "reg_set" attribute. |
| 3803 | if (!set_name && !gdb_group.empty()) |
| 3804 | set_name.SetCString(gdb_group.c_str()); |
| 3805 | |
| 3806 | reg_info.byte_offset = reg_offset; |
| 3807 | assert (reg_info.byte_size != 0); |
| 3808 | reg_offset += reg_info.byte_size; |
| 3809 | if (!value_regs.empty()) |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 3810 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3811 | value_regs.push_back(LLDB_INVALID_REGNUM); |
| 3812 | reg_info.value_regs = value_regs.data(); |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3813 | } |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3814 | if (!invalidate_regs.empty()) |
| 3815 | { |
| 3816 | invalidate_regs.push_back(LLDB_INVALID_REGNUM); |
| 3817 | reg_info.invalidate_regs = invalidate_regs.data(); |
| 3818 | } |
| 3819 | |
| 3820 | dyn_reg_info.AddRegister(reg_info, reg_name, alt_name, set_name); |
| 3821 | |
| 3822 | return true; // Keep iterating through all "reg" elements |
| 3823 | }); |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3824 | return true; |
| 3825 | } |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3826 | |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3827 | } // namespace {} |
| 3828 | |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3829 | |
| 3830 | // query the target of gdb-remote for extended target information |
| 3831 | // return: 'true' on success |
| 3832 | // 'false' on failure |
| 3833 | bool |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 3834 | ProcessGDBRemote::GetGDBServerRegisterInfo () |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3835 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3836 | // Make sure LLDB has an XML parser it can use first |
| 3837 | if (!XMLDocument::XMLEnabled()) |
| 3838 | return false; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3839 | |
| 3840 | // redirect libxml2's error handler since the default prints to stdout |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3841 | |
| 3842 | GDBRemoteCommunicationClient & comm = m_gdb_comm; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3843 | |
| 3844 | // check that we have extended feature read support |
| 3845 | if ( !comm.GetQXferFeaturesReadSupported( ) ) |
| 3846 | return false; |
| 3847 | |
| 3848 | // request the target xml file |
| 3849 | std::string raw; |
| 3850 | lldb_private::Error lldberr; |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 3851 | if (!comm.ReadExtFeature(ConstString("features"), |
| 3852 | ConstString("target.xml"), |
| 3853 | raw, |
| 3854 | lldberr)) |
| 3855 | { |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3856 | return false; |
| 3857 | } |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3858 | |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3859 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3860 | XMLDocument xml_document; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3861 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3862 | if (xml_document.ParseMemory(raw.c_str(), raw.size(), "target.xml")) |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 3863 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3864 | GdbServerTargetInfo target_info; |
| 3865 | |
| 3866 | XMLNode target_node = xml_document.GetRootElement("target"); |
| 3867 | if (target_node) |
Jason Molenda | 6ae1aab | 2015-04-17 19:15:02 +0000 | [diff] [blame] | 3868 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3869 | XMLNode feature_node; |
| 3870 | target_node.ForEachChildElement([&target_info, this, &feature_node](const XMLNode &node) -> bool |
| 3871 | { |
| 3872 | llvm::StringRef name = node.GetName(); |
| 3873 | if (name == "architecture") |
| 3874 | { |
| 3875 | node.GetElementText(target_info.arch); |
| 3876 | } |
| 3877 | else if (name == "osabi") |
| 3878 | { |
| 3879 | node.GetElementText(target_info.osabi); |
| 3880 | } |
| 3881 | else if (name == "xi:include") |
| 3882 | { |
| 3883 | llvm::StringRef href = node.GetAttributeValue("href"); |
| 3884 | if (!href.empty()) |
| 3885 | target_info.includes.push_back(href.str()); |
| 3886 | } |
| 3887 | else if (name == "feature") |
| 3888 | { |
| 3889 | feature_node = node; |
| 3890 | } |
| 3891 | else if (name == "groups") |
| 3892 | { |
| 3893 | node.ForEachChildElementWithName("group", [&target_info](const XMLNode &node) -> bool { |
| 3894 | uint32_t set_id = UINT32_MAX; |
| 3895 | RegisterSetInfo set_info; |
| 3896 | |
| 3897 | node.ForEachAttribute([&set_id, &set_info](const llvm::StringRef &name, const llvm::StringRef &value) -> bool { |
| 3898 | if (name == "id") |
| 3899 | set_id = StringConvert::ToUInt32(value.data(), UINT32_MAX, 0); |
| 3900 | if (name == "name") |
| 3901 | set_info.name = ConstString(value); |
| 3902 | return true; // Keep iterating through all attributes |
| 3903 | }); |
| 3904 | |
| 3905 | if (set_id != UINT32_MAX) |
| 3906 | target_info.reg_set_map[set_id] = set_info; |
| 3907 | return true; // Keep iterating through all "group" elements |
| 3908 | }); |
| 3909 | } |
| 3910 | return true; // Keep iterating through all children of the target_node |
| 3911 | }); |
| 3912 | |
| 3913 | if (feature_node) |
| 3914 | { |
| 3915 | ParseRegisters(feature_node, target_info, this->m_register_info); |
| 3916 | } |
| 3917 | |
| 3918 | for (const auto &include : target_info.includes) |
| 3919 | { |
| 3920 | // request register file |
| 3921 | std::string xml_data; |
| 3922 | if (!comm.ReadExtFeature(ConstString("features"), |
| 3923 | ConstString(include), |
| 3924 | xml_data, |
| 3925 | lldberr)) |
| 3926 | continue; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3927 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3928 | XMLDocument include_xml_document; |
| 3929 | include_xml_document.ParseMemory(xml_data.data(), xml_data.size(), include.c_str()); |
| 3930 | XMLNode include_feature_node = include_xml_document.GetRootElement("feature"); |
| 3931 | if (include_feature_node) |
| 3932 | { |
| 3933 | ParseRegisters(include_feature_node, target_info, this->m_register_info); |
| 3934 | } |
| 3935 | } |
| 3936 | this->m_register_info.Finalize(GetTarget().GetArchitecture()); |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3937 | } |
| 3938 | } |
| 3939 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3940 | return m_register_info.GetNumRegisters() > 0; |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 3943 | Error |
| 3944 | ProcessGDBRemote::GetLoadedModuleList (GDBLoadedModuleInfoList & list) |
| 3945 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3946 | // Make sure LLDB has an XML parser it can use first |
| 3947 | if (!XMLDocument::XMLEnabled()) |
| 3948 | return Error (0, ErrorType::eErrorTypeGeneric); |
| 3949 | |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 3950 | Log *log = GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS); |
| 3951 | if (log) |
| 3952 | log->Printf ("ProcessGDBRemote::%s", __FUNCTION__); |
| 3953 | |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 3954 | GDBRemoteCommunicationClient & comm = m_gdb_comm; |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 3955 | |
| 3956 | // check that we have extended feature read support |
| 3957 | if (!comm.GetQXferLibrariesSVR4ReadSupported ()) |
| 3958 | return Error (0, ErrorType::eErrorTypeGeneric); |
| 3959 | |
| 3960 | list.clear (); |
| 3961 | |
| 3962 | // request the loaded library list |
| 3963 | std::string raw; |
| 3964 | lldb_private::Error lldberr; |
| 3965 | if (!comm.ReadExtFeature (ConstString ("libraries-svr4"), ConstString (""), raw, lldberr)) |
| 3966 | return Error (0, ErrorType::eErrorTypeGeneric); |
| 3967 | |
| 3968 | // parse the xml file in memory |
| 3969 | if (log) |
| 3970 | log->Printf ("parsing: %s", raw.c_str()); |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3971 | XMLDocument doc; |
| 3972 | |
| 3973 | if (!doc.ParseMemory(raw.c_str(), raw.size(), "noname.xml")) |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 3974 | return Error (0, ErrorType::eErrorTypeGeneric); |
| 3975 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3976 | XMLNode root_element = doc.GetRootElement("library-list-svr4"); |
| 3977 | if (!root_element) |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 3978 | return Error(); |
| 3979 | |
| 3980 | // main link map structure |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3981 | llvm::StringRef main_lm = root_element.GetAttributeValue("main-lm"); |
| 3982 | if (!main_lm.empty()) |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 3983 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3984 | list.m_link_map = StringConvert::ToUInt64(main_lm.data(), LLDB_INVALID_ADDRESS, 0); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 3985 | } |
| 3986 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3987 | root_element.ForEachChildElementWithName("library", [log, &list](const XMLNode &library) -> bool { |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 3988 | |
| 3989 | GDBLoadedModuleInfoList::LoadedModuleInfo module; |
| 3990 | |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3991 | library.ForEachAttribute([log, &module](const llvm::StringRef &name, const llvm::StringRef &value) -> bool { |
| 3992 | |
| 3993 | if (name == "name") |
| 3994 | module.set_name (value.str()); |
| 3995 | else if (name == "lm") |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 3996 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 3997 | // the address of the link_map struct. |
| 3998 | module.set_link_map(StringConvert::ToUInt64(value.data(), LLDB_INVALID_ADDRESS, 0)); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 3999 | } |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4000 | else if (name == "l_addr") |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4001 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4002 | // the displacement as read from the field 'l_addr' of the link_map struct. |
| 4003 | module.set_base(StringConvert::ToUInt64(value.data(), LLDB_INVALID_ADDRESS, 0)); |
| 4004 | |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4005 | } |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4006 | else if (name == "l_ld") |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4007 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4008 | // the memory address of the libraries PT_DYAMIC section. |
| 4009 | module.set_dynamic(StringConvert::ToUInt64(value.data(), LLDB_INVALID_ADDRESS, 0)); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4010 | } |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4011 | |
| 4012 | return true; // Keep iterating over all properties of "library" |
| 4013 | }); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4014 | |
| 4015 | if (log) |
| 4016 | { |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4017 | std::string name; |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4018 | lldb::addr_t lm=0, base=0, ld=0; |
| 4019 | |
| 4020 | module.get_name (name); |
| 4021 | module.get_link_map (lm); |
| 4022 | module.get_base (base); |
| 4023 | module.get_dynamic (ld); |
| 4024 | |
| 4025 | log->Printf ("found (link_map:0x08%" PRIx64 ", base:0x08%" PRIx64 ", ld:0x08%" PRIx64 ", name:'%s')", lm, base, ld, name.c_str()); |
| 4026 | } |
| 4027 | |
| 4028 | list.add (module); |
Greg Clayton | d04f0ed | 2015-05-26 18:00:51 +0000 | [diff] [blame] | 4029 | return true; // Keep iterating over all "library" elements in the root node |
| 4030 | }); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4031 | |
| 4032 | if (log) |
| 4033 | log->Printf ("found %" PRId32 " modules in total", (int) list.m_list.size()); |
| 4034 | |
| 4035 | return Error(); |
| 4036 | } |
| 4037 | |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4038 | lldb::ModuleSP |
| 4039 | ProcessGDBRemote::LoadModuleAtAddress (const FileSpec &file, lldb::addr_t base_addr) |
| 4040 | { |
| 4041 | Target &target = m_process->GetTarget(); |
| 4042 | ModuleList &modules = target.GetImages(); |
| 4043 | ModuleSP module_sp; |
| 4044 | |
| 4045 | bool changed = false; |
| 4046 | |
| 4047 | ModuleSpec module_spec (file, target.GetArchitecture()); |
| 4048 | if ((module_sp = modules.FindFirstModule (module_spec))) |
| 4049 | { |
| 4050 | module_sp->SetLoadAddress (target, base_addr, true, changed); |
| 4051 | } |
| 4052 | else if ((module_sp = target.GetSharedModule (module_spec))) |
| 4053 | { |
| 4054 | module_sp->SetLoadAddress (target, base_addr, true, changed); |
| 4055 | } |
| 4056 | |
| 4057 | return module_sp; |
| 4058 | } |
| 4059 | |
| 4060 | size_t |
| 4061 | ProcessGDBRemote::LoadModules () |
| 4062 | { |
| 4063 | using lldb_private::process_gdb_remote::ProcessGDBRemote; |
| 4064 | |
| 4065 | // request a list of loaded libraries from GDBServer |
| 4066 | GDBLoadedModuleInfoList module_list; |
| 4067 | if (GetLoadedModuleList (module_list).Fail()) |
| 4068 | return 0; |
| 4069 | |
| 4070 | // get a list of all the modules |
| 4071 | ModuleList new_modules; |
| 4072 | |
| 4073 | for (GDBLoadedModuleInfoList::LoadedModuleInfo & modInfo : module_list.m_list) |
| 4074 | { |
| 4075 | std::string mod_name; |
| 4076 | lldb::addr_t mod_base; |
| 4077 | |
| 4078 | bool valid = true; |
| 4079 | valid &= modInfo.get_name (mod_name); |
| 4080 | valid &= modInfo.get_base (mod_base); |
| 4081 | if (!valid) |
| 4082 | continue; |
| 4083 | |
| 4084 | // hack (cleaner way to get file name only?) (win/unix compat?) |
Vince Harron | 4cc8d20 | 2015-05-10 08:33:58 +0000 | [diff] [blame] | 4085 | size_t marker = mod_name.rfind ('/'); |
Aidan Dodds | c0c8385 | 2015-05-08 09:36:31 +0000 | [diff] [blame] | 4086 | if (marker == std::string::npos) |
| 4087 | marker = 0; |
| 4088 | else |
| 4089 | marker += 1; |
| 4090 | |
| 4091 | FileSpec file (mod_name.c_str()+marker, true); |
| 4092 | lldb::ModuleSP module_sp = LoadModuleAtAddress (file, mod_base); |
| 4093 | |
| 4094 | if (module_sp.get()) |
| 4095 | new_modules.Append (module_sp); |
| 4096 | } |
| 4097 | |
| 4098 | if (new_modules.GetSize() > 0) |
| 4099 | { |
| 4100 | Target & target = m_target; |
| 4101 | |
| 4102 | new_modules.ForEach ([&target](const lldb::ModuleSP module_sp) -> bool |
| 4103 | { |
| 4104 | lldb_private::ObjectFile * obj = module_sp->GetObjectFile (); |
| 4105 | if (!obj) |
| 4106 | return true; |
| 4107 | |
| 4108 | if (obj->GetType () != ObjectFile::Type::eTypeExecutable) |
| 4109 | return true; |
| 4110 | |
| 4111 | lldb::ModuleSP module_copy_sp = module_sp; |
| 4112 | target.SetExecutableModule (module_copy_sp, false); |
| 4113 | return false; |
| 4114 | }); |
| 4115 | |
| 4116 | ModuleList &loaded_modules = m_process->GetTarget().GetImages(); |
| 4117 | loaded_modules.AppendIfNeeded (new_modules); |
| 4118 | m_process->GetTarget().ModulesDidLoad (new_modules); |
| 4119 | } |
| 4120 | |
| 4121 | return new_modules.GetSize(); |
| 4122 | } |
Colin Riley | c3c95b2 | 2015-04-16 15:51:33 +0000 | [diff] [blame] | 4123 | |
Tamas Berghammer | 783bfc8 | 2015-06-18 20:43:56 +0000 | [diff] [blame^] | 4124 | Error |
| 4125 | ProcessGDBRemote::GetFileLoadAddress(const FileSpec& file, bool& is_loaded, lldb::addr_t& load_addr) |
| 4126 | { |
| 4127 | is_loaded = false; |
| 4128 | load_addr = LLDB_INVALID_ADDRESS; |
| 4129 | |
| 4130 | std::string file_path = file.GetPath(false); |
| 4131 | if (file_path.empty ()) |
| 4132 | return Error("Empty file name specified"); |
| 4133 | |
| 4134 | StreamString packet; |
| 4135 | packet.PutCString("qFileLoadAddress:"); |
| 4136 | packet.PutCStringAsRawHex8(file_path.c_str()); |
| 4137 | |
| 4138 | StringExtractorGDBRemote response; |
| 4139 | if (m_gdb_comm.SendPacketAndWaitForResponse(packet.GetString().c_str(), response, false) != GDBRemoteCommunication::PacketResult::Success) |
| 4140 | return Error("Sending qFileLoadAddress packet failed"); |
| 4141 | |
| 4142 | if (response.IsErrorResponse()) |
| 4143 | { |
| 4144 | if (response.GetError() == 1) |
| 4145 | { |
| 4146 | // The file is not loaded into the inferior |
| 4147 | is_loaded = false; |
| 4148 | load_addr = LLDB_INVALID_ADDRESS; |
| 4149 | return Error(); |
| 4150 | } |
| 4151 | |
| 4152 | return Error("Fetching file load address from remote server returned an error"); |
| 4153 | } |
| 4154 | |
| 4155 | if (response.IsNormalResponse()) |
| 4156 | { |
| 4157 | is_loaded = true; |
| 4158 | load_addr = response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); |
| 4159 | return Error(); |
| 4160 | } |
| 4161 | |
| 4162 | return Error("Unknown error happened during sending the load address packet"); |
| 4163 | } |
| 4164 | |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 4165 | class CommandObjectProcessGDBRemoteSpeedTest: public CommandObjectParsed |
| 4166 | { |
| 4167 | public: |
| 4168 | CommandObjectProcessGDBRemoteSpeedTest(CommandInterpreter &interpreter) : |
| 4169 | CommandObjectParsed (interpreter, |
| 4170 | "process plugin packet speed-test", |
| 4171 | "Tests packet speeds of various sizes to determine the performance characteristics of the GDB remote connection. ", |
| 4172 | NULL), |
| 4173 | m_option_group (interpreter), |
| 4174 | m_num_packets (LLDB_OPT_SET_1, false, "count", 'c', 0, eArgTypeCount, "The number of packets to send of each varying size (default is 1000).", 1000), |
| 4175 | m_max_send (LLDB_OPT_SET_1, false, "max-send", 's', 0, eArgTypeCount, "The maximum number of bytes to send in a packet. Sizes increase in powers of 2 while the size is less than or equal to this option value. (default 1024).", 1024), |
| 4176 | m_max_recv (LLDB_OPT_SET_1, false, "max-receive", 'r', 0, eArgTypeCount, "The maximum number of bytes to receive in a packet. Sizes increase in powers of 2 while the size is less than or equal to this option value. (default 1024).", 1024), |
| 4177 | m_json (LLDB_OPT_SET_1, false, "json", 'j', "Print the output as JSON data for easy parsing.", false, true) |
| 4178 | { |
| 4179 | m_option_group.Append (&m_num_packets, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 4180 | m_option_group.Append (&m_max_send, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 4181 | m_option_group.Append (&m_max_recv, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 4182 | m_option_group.Append (&m_json, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); |
| 4183 | m_option_group.Finalize(); |
| 4184 | } |
| 4185 | |
| 4186 | ~CommandObjectProcessGDBRemoteSpeedTest () |
| 4187 | { |
| 4188 | } |
| 4189 | |
| 4190 | |
| 4191 | Options * |
| 4192 | GetOptions () override |
| 4193 | { |
| 4194 | return &m_option_group; |
| 4195 | } |
| 4196 | |
| 4197 | bool |
| 4198 | DoExecute (Args& command, CommandReturnObject &result) override |
| 4199 | { |
| 4200 | const size_t argc = command.GetArgumentCount(); |
| 4201 | if (argc == 0) |
| 4202 | { |
| 4203 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 4204 | if (process) |
| 4205 | { |
| 4206 | StreamSP output_stream_sp (m_interpreter.GetDebugger().GetAsyncOutputStream()); |
| 4207 | result.SetImmediateOutputStream (output_stream_sp); |
| 4208 | |
| 4209 | const uint32_t num_packets = (uint32_t)m_num_packets.GetOptionValue().GetCurrentValue(); |
| 4210 | const uint64_t max_send = m_max_send.GetOptionValue().GetCurrentValue(); |
| 4211 | const uint64_t max_recv = m_max_recv.GetOptionValue().GetCurrentValue(); |
| 4212 | const bool json = m_json.GetOptionValue().GetCurrentValue(); |
| 4213 | if (output_stream_sp) |
| 4214 | process->GetGDBRemote().TestPacketSpeed (num_packets, max_send, max_recv, json, *output_stream_sp); |
| 4215 | else |
| 4216 | { |
| 4217 | process->GetGDBRemote().TestPacketSpeed (num_packets, max_send, max_recv, json, result.GetOutputStream()); |
| 4218 | } |
| 4219 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 4220 | return true; |
| 4221 | } |
| 4222 | } |
| 4223 | else |
| 4224 | { |
| 4225 | result.AppendErrorWithFormat ("'%s' takes no arguments", m_cmd_name.c_str()); |
| 4226 | } |
| 4227 | result.SetStatus (eReturnStatusFailed); |
| 4228 | return false; |
| 4229 | } |
| 4230 | protected: |
| 4231 | OptionGroupOptions m_option_group; |
| 4232 | OptionGroupUInt64 m_num_packets; |
| 4233 | OptionGroupUInt64 m_max_send; |
| 4234 | OptionGroupUInt64 m_max_recv; |
| 4235 | OptionGroupBoolean m_json; |
| 4236 | |
| 4237 | }; |
| 4238 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 4239 | class CommandObjectProcessGDBRemotePacketHistory : public CommandObjectParsed |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 4240 | { |
| 4241 | private: |
| 4242 | |
| 4243 | public: |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 4244 | CommandObjectProcessGDBRemotePacketHistory(CommandInterpreter &interpreter) : |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 4245 | CommandObjectParsed (interpreter, |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 4246 | "process plugin packet history", |
| 4247 | "Dumps the packet history buffer. ", |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 4248 | NULL) |
| 4249 | { |
| 4250 | } |
| 4251 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 4252 | ~CommandObjectProcessGDBRemotePacketHistory () |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 4253 | { |
| 4254 | } |
| 4255 | |
| 4256 | bool |
Tamas Berghammer | 30b8cd3 | 2015-03-23 15:50:03 +0000 | [diff] [blame] | 4257 | DoExecute (Args& command, CommandReturnObject &result) override |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 4258 | { |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 4259 | const size_t argc = command.GetArgumentCount(); |
| 4260 | if (argc == 0) |
| 4261 | { |
| 4262 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 4263 | if (process) |
| 4264 | { |
| 4265 | process->GetGDBRemote().DumpHistory(result.GetOutputStream()); |
| 4266 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 4267 | return true; |
| 4268 | } |
| 4269 | } |
| 4270 | else |
| 4271 | { |
| 4272 | result.AppendErrorWithFormat ("'%s' takes no arguments", m_cmd_name.c_str()); |
| 4273 | } |
| 4274 | result.SetStatus (eReturnStatusFailed); |
| 4275 | return false; |
| 4276 | } |
| 4277 | }; |
| 4278 | |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 4279 | class CommandObjectProcessGDBRemotePacketXferSize : public CommandObjectParsed |
| 4280 | { |
| 4281 | private: |
| 4282 | |
| 4283 | public: |
| 4284 | CommandObjectProcessGDBRemotePacketXferSize(CommandInterpreter &interpreter) : |
| 4285 | CommandObjectParsed (interpreter, |
| 4286 | "process plugin packet xfer-size", |
| 4287 | "Maximum size that lldb will try to read/write one one chunk.", |
| 4288 | NULL) |
| 4289 | { |
| 4290 | } |
| 4291 | |
| 4292 | ~CommandObjectProcessGDBRemotePacketXferSize () |
| 4293 | { |
| 4294 | } |
| 4295 | |
| 4296 | bool |
Tamas Berghammer | 30b8cd3 | 2015-03-23 15:50:03 +0000 | [diff] [blame] | 4297 | DoExecute (Args& command, CommandReturnObject &result) override |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 4298 | { |
| 4299 | const size_t argc = command.GetArgumentCount(); |
| 4300 | if (argc == 0) |
| 4301 | { |
| 4302 | result.AppendErrorWithFormat ("'%s' takes an argument to specify the max amount to be transferred when reading/writing", m_cmd_name.c_str()); |
| 4303 | result.SetStatus (eReturnStatusFailed); |
| 4304 | return false; |
| 4305 | } |
| 4306 | |
| 4307 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 4308 | if (process) |
| 4309 | { |
| 4310 | const char *packet_size = command.GetArgumentAtIndex(0); |
| 4311 | errno = 0; |
| 4312 | uint64_t user_specified_max = strtoul (packet_size, NULL, 10); |
| 4313 | if (errno == 0 && user_specified_max != 0) |
| 4314 | { |
| 4315 | process->SetUserSpecifiedMaxMemoryTransferSize (user_specified_max); |
| 4316 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 4317 | return true; |
| 4318 | } |
| 4319 | } |
| 4320 | result.SetStatus (eReturnStatusFailed); |
| 4321 | return false; |
| 4322 | } |
| 4323 | }; |
| 4324 | |
| 4325 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 4326 | class CommandObjectProcessGDBRemotePacketSend : public CommandObjectParsed |
| 4327 | { |
| 4328 | private: |
| 4329 | |
| 4330 | public: |
| 4331 | CommandObjectProcessGDBRemotePacketSend(CommandInterpreter &interpreter) : |
| 4332 | CommandObjectParsed (interpreter, |
| 4333 | "process plugin packet send", |
| 4334 | "Send a custom packet through the GDB remote protocol and print the answer. " |
| 4335 | "The packet header and footer will automatically be added to the packet prior to sending and stripped from the result.", |
| 4336 | NULL) |
| 4337 | { |
| 4338 | } |
| 4339 | |
| 4340 | ~CommandObjectProcessGDBRemotePacketSend () |
| 4341 | { |
| 4342 | } |
| 4343 | |
| 4344 | bool |
Tamas Berghammer | 30b8cd3 | 2015-03-23 15:50:03 +0000 | [diff] [blame] | 4345 | DoExecute (Args& command, CommandReturnObject &result) override |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 4346 | { |
| 4347 | const size_t argc = command.GetArgumentCount(); |
| 4348 | if (argc == 0) |
| 4349 | { |
| 4350 | result.AppendErrorWithFormat ("'%s' takes a one or more packet content arguments", m_cmd_name.c_str()); |
| 4351 | result.SetStatus (eReturnStatusFailed); |
| 4352 | return false; |
| 4353 | } |
| 4354 | |
| 4355 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 4356 | if (process) |
| 4357 | { |
Han Ming Ong | 8414585 | 2012-11-26 20:42:03 +0000 | [diff] [blame] | 4358 | for (size_t i=0; i<argc; ++ i) |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 4359 | { |
Han Ming Ong | 8414585 | 2012-11-26 20:42:03 +0000 | [diff] [blame] | 4360 | const char *packet_cstr = command.GetArgumentAtIndex(0); |
| 4361 | bool send_async = true; |
| 4362 | StringExtractorGDBRemote response; |
| 4363 | process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async); |
| 4364 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 4365 | Stream &output_strm = result.GetOutputStream(); |
| 4366 | output_strm.Printf (" packet: %s\n", packet_cstr); |
Han Ming Ong | 4b6459f | 2013-01-18 23:11:53 +0000 | [diff] [blame] | 4367 | std::string &response_str = response.GetStringRef(); |
| 4368 | |
Han Ming Ong | 399289e | 2013-06-21 19:56:59 +0000 | [diff] [blame] | 4369 | if (strstr(packet_cstr, "qGetProfileData") != NULL) |
Han Ming Ong | 4b6459f | 2013-01-18 23:11:53 +0000 | [diff] [blame] | 4370 | { |
| 4371 | response_str = process->GetGDBRemote().HarmonizeThreadIdsForProfileData(process, response); |
| 4372 | } |
| 4373 | |
Han Ming Ong | 8414585 | 2012-11-26 20:42:03 +0000 | [diff] [blame] | 4374 | if (response_str.empty()) |
| 4375 | output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n"); |
| 4376 | else |
| 4377 | output_strm.Printf ("response: %s\n", response.GetStringRef().c_str()); |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 4378 | } |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 4379 | } |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 4380 | return true; |
| 4381 | } |
| 4382 | }; |
| 4383 | |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 4384 | class CommandObjectProcessGDBRemotePacketMonitor : public CommandObjectRaw |
| 4385 | { |
| 4386 | private: |
| 4387 | |
| 4388 | public: |
| 4389 | CommandObjectProcessGDBRemotePacketMonitor(CommandInterpreter &interpreter) : |
| 4390 | CommandObjectRaw (interpreter, |
| 4391 | "process plugin packet monitor", |
Greg Clayton | eee5e98 | 2013-02-14 18:39:30 +0000 | [diff] [blame] | 4392 | "Send a qRcmd packet through the GDB remote protocol and print the response." |
| 4393 | "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] | 4394 | NULL) |
| 4395 | { |
| 4396 | } |
| 4397 | |
| 4398 | ~CommandObjectProcessGDBRemotePacketMonitor () |
| 4399 | { |
| 4400 | } |
| 4401 | |
| 4402 | bool |
Tamas Berghammer | 30b8cd3 | 2015-03-23 15:50:03 +0000 | [diff] [blame] | 4403 | DoExecute (const char *command, CommandReturnObject &result) override |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 4404 | { |
| 4405 | if (command == NULL || command[0] == '\0') |
| 4406 | { |
| 4407 | result.AppendErrorWithFormat ("'%s' takes a command string argument", m_cmd_name.c_str()); |
| 4408 | result.SetStatus (eReturnStatusFailed); |
| 4409 | return false; |
| 4410 | } |
| 4411 | |
| 4412 | ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr(); |
| 4413 | if (process) |
| 4414 | { |
| 4415 | StreamString packet; |
Greg Clayton | eee5e98 | 2013-02-14 18:39:30 +0000 | [diff] [blame] | 4416 | packet.PutCString("qRcmd,"); |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 4417 | packet.PutBytesAsRawHex8(command, strlen(command)); |
| 4418 | const char *packet_cstr = packet.GetString().c_str(); |
| 4419 | |
| 4420 | bool send_async = true; |
| 4421 | StringExtractorGDBRemote response; |
| 4422 | process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async); |
| 4423 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 4424 | Stream &output_strm = result.GetOutputStream(); |
| 4425 | output_strm.Printf (" packet: %s\n", packet_cstr); |
| 4426 | const std::string &response_str = response.GetStringRef(); |
| 4427 | |
| 4428 | if (response_str.empty()) |
| 4429 | output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n"); |
| 4430 | else |
| 4431 | output_strm.Printf ("response: %s\n", response.GetStringRef().c_str()); |
| 4432 | } |
| 4433 | return true; |
| 4434 | } |
| 4435 | }; |
| 4436 | |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 4437 | class CommandObjectProcessGDBRemotePacket : public CommandObjectMultiword |
| 4438 | { |
| 4439 | private: |
| 4440 | |
| 4441 | public: |
| 4442 | CommandObjectProcessGDBRemotePacket(CommandInterpreter &interpreter) : |
| 4443 | CommandObjectMultiword (interpreter, |
| 4444 | "process plugin packet", |
| 4445 | "Commands that deal with GDB remote packets.", |
| 4446 | NULL) |
| 4447 | { |
| 4448 | LoadSubCommand ("history", CommandObjectSP (new CommandObjectProcessGDBRemotePacketHistory (interpreter))); |
| 4449 | LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessGDBRemotePacketSend (interpreter))); |
Greg Clayton | ba4a0a5 | 2013-02-01 23:03:47 +0000 | [diff] [blame] | 4450 | LoadSubCommand ("monitor", CommandObjectSP (new CommandObjectProcessGDBRemotePacketMonitor (interpreter))); |
Jason Molenda | 6076bf4 | 2014-05-06 04:34:52 +0000 | [diff] [blame] | 4451 | LoadSubCommand ("xfer-size", CommandObjectSP (new CommandObjectProcessGDBRemotePacketXferSize (interpreter))); |
Greg Clayton | e034a04 | 2015-05-21 20:52:06 +0000 | [diff] [blame] | 4452 | LoadSubCommand ("speed-test", CommandObjectSP (new CommandObjectProcessGDBRemoteSpeedTest (interpreter))); |
Greg Clayton | 02686b8 | 2012-10-15 22:42:16 +0000 | [diff] [blame] | 4453 | } |
| 4454 | |
| 4455 | ~CommandObjectProcessGDBRemotePacket () |
| 4456 | { |
| 4457 | } |
| 4458 | }; |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 4459 | |
| 4460 | class CommandObjectMultiwordProcessGDBRemote : public CommandObjectMultiword |
| 4461 | { |
| 4462 | public: |
| 4463 | CommandObjectMultiwordProcessGDBRemote (CommandInterpreter &interpreter) : |
| 4464 | CommandObjectMultiword (interpreter, |
| 4465 | "process plugin", |
| 4466 | "A set of commands for operating on a ProcessGDBRemote process.", |
| 4467 | "process plugin <subcommand> [<subcommand-options>]") |
| 4468 | { |
| 4469 | LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessGDBRemotePacket (interpreter))); |
| 4470 | } |
| 4471 | |
| 4472 | ~CommandObjectMultiwordProcessGDBRemote () |
| 4473 | { |
| 4474 | } |
| 4475 | }; |
| 4476 | |
Greg Clayton | 998255b | 2012-10-13 02:07:45 +0000 | [diff] [blame] | 4477 | CommandObject * |
| 4478 | ProcessGDBRemote::GetPluginCommandObject() |
| 4479 | { |
| 4480 | if (!m_command_sp) |
| 4481 | m_command_sp.reset (new CommandObjectMultiwordProcessGDBRemote (GetTarget().GetDebugger().GetCommandInterpreter())); |
| 4482 | return m_command_sp.get(); |
| 4483 | } |