Greg Clayton | 269f91e | 2011-07-15 18:02:58 +0000 | [diff] [blame] | 1 | //===-- ProcessKDP.cpp ------------------------------------------*- C++ -*-===// |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // C Includes |
| 11 | #include <errno.h> |
| 12 | #include <stdlib.h> |
| 13 | |
| 14 | // C++ Includes |
| 15 | // Other libraries and framework includes |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 16 | #include "lldb/Core/ConnectionFileDescriptor.h" |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Debugger.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 18 | #include "lldb/Core/PluginManager.h" |
| 19 | #include "lldb/Core/State.h" |
| 20 | #include "lldb/Host/Host.h" |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 21 | #include "lldb/Target/Target.h" |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 22 | #include "lldb/Target/Thread.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 23 | |
| 24 | // Project includes |
| 25 | #include "ProcessKDP.h" |
| 26 | #include "ProcessKDPLog.h" |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 27 | #include "ThreadKDP.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 28 | #include "StopInfoMachException.h" |
| 29 | |
| 30 | using namespace lldb; |
| 31 | using namespace lldb_private; |
| 32 | |
| 33 | const char * |
| 34 | ProcessKDP::GetPluginNameStatic() |
| 35 | { |
| 36 | return "kdp-remote"; |
| 37 | } |
| 38 | |
| 39 | const char * |
| 40 | ProcessKDP::GetPluginDescriptionStatic() |
| 41 | { |
| 42 | return "KDP Remote protocol based debugging plug-in for darwin kernel debugging."; |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | ProcessKDP::Terminate() |
| 47 | { |
| 48 | PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance); |
| 49 | } |
| 50 | |
| 51 | |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 52 | lldb::ProcessSP |
| 53 | ProcessKDP::CreateInstance (Target &target, |
| 54 | Listener &listener, |
| 55 | const FileSpec *crash_file_path) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 56 | { |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 57 | lldb::ProcessSP process_sp; |
| 58 | if (crash_file_path == NULL) |
| 59 | process_sp.reset(new ProcessKDP (target, listener)); |
| 60 | return process_sp; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | bool |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 64 | ProcessKDP::CanDebug(Target &target, bool plugin_specified_by_name) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 65 | { |
Greg Clayton | 61ddf56 | 2011-10-21 21:41:45 +0000 | [diff] [blame] | 66 | if (plugin_specified_by_name) |
| 67 | return true; |
| 68 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 69 | // For now we are just making sure the file exists for a given module |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 70 | Module *exe_module = target.GetExecutableModulePointer(); |
| 71 | if (exe_module) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 72 | { |
| 73 | const llvm::Triple &triple_ref = target.GetArchitecture().GetTriple(); |
| 74 | if (triple_ref.getOS() == llvm::Triple::Darwin && |
| 75 | triple_ref.getVendor() == llvm::Triple::Apple) |
| 76 | { |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 77 | ObjectFile *exe_objfile = exe_module->GetObjectFile(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 78 | if (exe_objfile->GetType() == ObjectFile::eTypeExecutable && |
| 79 | exe_objfile->GetStrata() == ObjectFile::eStrataKernel) |
| 80 | return true; |
| 81 | } |
| 82 | } |
Greg Clayton | 61ddf56 | 2011-10-21 21:41:45 +0000 | [diff] [blame] | 83 | return false; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | //---------------------------------------------------------------------- |
| 87 | // ProcessKDP constructor |
| 88 | //---------------------------------------------------------------------- |
| 89 | ProcessKDP::ProcessKDP(Target& target, Listener &listener) : |
| 90 | Process (target, listener), |
| 91 | m_comm("lldb.process.kdp-remote.communication"), |
Jim Ingham | 5a15e69 | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 92 | m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"), |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 93 | m_async_thread (LLDB_INVALID_HOST_THREAD) |
| 94 | { |
| 95 | // m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit, "async thread should exit"); |
| 96 | // m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue, "async thread continue"); |
| 97 | } |
| 98 | |
| 99 | //---------------------------------------------------------------------- |
| 100 | // Destructor |
| 101 | //---------------------------------------------------------------------- |
| 102 | ProcessKDP::~ProcessKDP() |
| 103 | { |
| 104 | Clear(); |
Greg Clayton | ffa43a6 | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 105 | // We need to call finalize on the process before destroying ourselves |
| 106 | // to make sure all of the broadcaster cleanup goes as planned. If we |
| 107 | // destruct this class, then Process::~Process() might have problems |
| 108 | // trying to fully destroy the broadcaster. |
| 109 | Finalize(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | //---------------------------------------------------------------------- |
| 113 | // PluginInterface |
| 114 | //---------------------------------------------------------------------- |
| 115 | const char * |
| 116 | ProcessKDP::GetPluginName() |
| 117 | { |
| 118 | return "Process debugging plug-in that uses the Darwin KDP remote protocol"; |
| 119 | } |
| 120 | |
| 121 | const char * |
| 122 | ProcessKDP::GetShortPluginName() |
| 123 | { |
| 124 | return GetPluginNameStatic(); |
| 125 | } |
| 126 | |
| 127 | uint32_t |
| 128 | ProcessKDP::GetPluginVersion() |
| 129 | { |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | Error |
| 134 | ProcessKDP::WillLaunch (Module* module) |
| 135 | { |
| 136 | Error error; |
| 137 | error.SetErrorString ("launching not supported in kdp-remote plug-in"); |
| 138 | return error; |
| 139 | } |
| 140 | |
| 141 | Error |
| 142 | ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid) |
| 143 | { |
| 144 | Error error; |
| 145 | error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in"); |
| 146 | return error; |
| 147 | } |
| 148 | |
| 149 | Error |
| 150 | ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) |
| 151 | { |
| 152 | Error error; |
| 153 | error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in"); |
| 154 | return error; |
| 155 | } |
| 156 | |
| 157 | Error |
| 158 | ProcessKDP::DoConnectRemote (const char *remote_url) |
| 159 | { |
| 160 | // TODO: fill in the remote connection to the remote KDP here! |
| 161 | Error error; |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 162 | |
| 163 | if (remote_url == NULL || remote_url[0] == '\0') |
| 164 | remote_url = "udp://localhost:41139"; |
| 165 | |
| 166 | std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); |
| 167 | if (conn_ap.get()) |
| 168 | { |
| 169 | // Only try once for now. |
| 170 | // TODO: check if we should be retrying? |
| 171 | const uint32_t max_retry_count = 1; |
| 172 | for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count) |
| 173 | { |
| 174 | if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess) |
| 175 | break; |
| 176 | usleep (100000); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (conn_ap->IsConnected()) |
| 181 | { |
| 182 | const uint16_t reply_port = conn_ap->GetReadPort (); |
| 183 | |
| 184 | if (reply_port != 0) |
| 185 | { |
| 186 | m_comm.SetConnection(conn_ap.release()); |
| 187 | |
| 188 | if (m_comm.SendRequestReattach(reply_port)) |
| 189 | { |
| 190 | if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB...")) |
| 191 | { |
| 192 | m_comm.GetVersion(); |
| 193 | uint32_t cpu = m_comm.GetCPUType(); |
| 194 | uint32_t sub = m_comm.GetCPUSubtype(); |
| 195 | ArchSpec kernel_arch; |
| 196 | kernel_arch.SetArchitecture(eArchTypeMachO, cpu, sub); |
| 197 | m_target.SetArchitecture(kernel_arch); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 198 | SetID (1); |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 199 | GetThreadList (); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 200 | SetPrivateState (eStateStopped); |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 201 | StreamSP async_strm_sp(m_target.GetDebugger().GetAsyncOutputStream()); |
| 202 | if (async_strm_sp) |
| 203 | { |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 204 | const char *cstr; |
| 205 | if ((cstr = m_comm.GetKernelVersion ()) != NULL) |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 206 | { |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 207 | async_strm_sp->Printf ("Version: %s\n", cstr); |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 208 | async_strm_sp->Flush(); |
| 209 | } |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 210 | // if ((cstr = m_comm.GetImagePath ()) != NULL) |
| 211 | // { |
| 212 | // async_strm_sp->Printf ("Image Path: %s\n", cstr); |
| 213 | // async_strm_sp->Flush(); |
| 214 | // } |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 215 | } |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | error.SetErrorString("KDP reattach failed"); |
| 221 | } |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | error.SetErrorString("invalid reply port from UDP connection"); |
| 226 | } |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | if (error.Success()) |
| 231 | error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url); |
| 232 | } |
| 233 | if (error.Fail()) |
| 234 | m_comm.Disconnect(); |
| 235 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 236 | return error; |
| 237 | } |
| 238 | |
| 239 | //---------------------------------------------------------------------- |
| 240 | // Process Control |
| 241 | //---------------------------------------------------------------------- |
| 242 | Error |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 243 | ProcessKDP::DoLaunch (Module *exe_module, |
| 244 | const ProcessLaunchInfo &launch_info) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 245 | { |
| 246 | Error error; |
| 247 | error.SetErrorString ("launching not supported in kdp-remote plug-in"); |
| 248 | return error; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | Error |
| 253 | ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid) |
| 254 | { |
| 255 | Error error; |
| 256 | error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging"); |
| 257 | return error; |
| 258 | } |
| 259 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 260 | Error |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 261 | ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info) |
| 262 | { |
| 263 | Error error; |
| 264 | error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging"); |
| 265 | return error; |
| 266 | } |
| 267 | |
| 268 | Error |
| 269 | ProcessKDP::DoAttachToProcessWithName (const char *process_name, bool wait_for_launch, const ProcessAttachInfo &attach_info) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 270 | { |
| 271 | Error error; |
| 272 | error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging"); |
| 273 | return error; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | void |
| 278 | ProcessKDP::DidAttach () |
| 279 | { |
| 280 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); |
| 281 | if (log) |
Johnny Chen | 01df057 | 2011-10-11 21:17:10 +0000 | [diff] [blame] | 282 | log->Printf ("ProcessKDP::DidAttach()"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 283 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 284 | { |
| 285 | // TODO: figure out the register context that we will use |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | Error |
| 290 | ProcessKDP::WillResume () |
| 291 | { |
| 292 | return Error(); |
| 293 | } |
| 294 | |
| 295 | Error |
| 296 | ProcessKDP::DoResume () |
| 297 | { |
| 298 | Error error; |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 299 | if (!m_comm.SendRequestResume ()) |
| 300 | error.SetErrorString ("KDP resume failed"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 301 | return error; |
| 302 | } |
| 303 | |
Greg Clayton | ae93235 | 2012-04-10 00:18:59 +0000 | [diff] [blame^] | 304 | bool |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 305 | ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 306 | { |
| 307 | // locker will keep a mutex locked until it goes out of scope |
| 308 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD)); |
| 309 | if (log && log->GetMask().Test(KDP_LOG_VERBOSE)) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 310 | log->Printf ("ProcessKDP::%s (pid = %llu)", __FUNCTION__, GetID()); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 311 | |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 312 | // We currently are making only one thread per core and we |
| 313 | // actually don't know about actual threads. Eventually we |
| 314 | // want to get the thread list from memory and note which |
| 315 | // threads are on CPU as those are the only ones that we |
| 316 | // will be able to resume. |
| 317 | const uint32_t cpu_mask = m_comm.GetCPUMask(); |
| 318 | for (uint32_t cpu_mask_bit = 1; cpu_mask_bit & cpu_mask; cpu_mask_bit <<= 1) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 319 | { |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 320 | lldb::tid_t tid = cpu_mask_bit; |
| 321 | ThreadSP thread_sp (old_thread_list.FindThreadByID (tid, false)); |
| 322 | if (!thread_sp) |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 323 | thread_sp.reset(new ThreadKDP (shared_from_this(), tid)); |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 324 | new_thread_list.AddThread(thread_sp); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 325 | } |
Greg Clayton | ae93235 | 2012-04-10 00:18:59 +0000 | [diff] [blame^] | 326 | return new_thread_list.GetSize(false) > 0; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | |
| 330 | StateType |
| 331 | ProcessKDP::SetThreadStopInfo (StringExtractor& stop_packet) |
| 332 | { |
| 333 | // TODO: figure out why we stopped given the packet that tells us we stopped... |
| 334 | return eStateStopped; |
| 335 | } |
| 336 | |
| 337 | void |
| 338 | ProcessKDP::RefreshStateAfterStop () |
| 339 | { |
| 340 | // Let all threads recover from stopping and do any clean up based |
| 341 | // on the previous thread state (if any). |
| 342 | m_thread_list.RefreshStateAfterStop(); |
| 343 | //SetThreadStopInfo (m_last_stop_packet); |
| 344 | } |
| 345 | |
| 346 | Error |
| 347 | ProcessKDP::DoHalt (bool &caused_stop) |
| 348 | { |
| 349 | Error error; |
| 350 | |
| 351 | // bool timed_out = false; |
| 352 | Mutex::Locker locker; |
| 353 | |
| 354 | if (m_public_state.GetValue() == eStateAttaching) |
| 355 | { |
| 356 | // We are being asked to halt during an attach. We need to just close |
| 357 | // our file handle and debugserver will go away, and we can be done... |
| 358 | m_comm.Disconnect(); |
| 359 | } |
| 360 | else |
| 361 | { |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 362 | if (!m_comm.SendRequestSuspend ()) |
| 363 | error.SetErrorString ("KDP halt failed"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 364 | } |
| 365 | return error; |
| 366 | } |
| 367 | |
| 368 | Error |
| 369 | ProcessKDP::InterruptIfRunning (bool discard_thread_plans, |
| 370 | bool catch_stop_event, |
| 371 | EventSP &stop_event_sp) |
| 372 | { |
| 373 | Error error; |
| 374 | |
| 375 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); |
| 376 | |
| 377 | bool paused_private_state_thread = false; |
| 378 | const bool is_running = m_comm.IsRunning(); |
| 379 | if (log) |
| 380 | log->Printf ("ProcessKDP::InterruptIfRunning(discard_thread_plans=%i, catch_stop_event=%i) is_running=%i", |
| 381 | discard_thread_plans, |
| 382 | catch_stop_event, |
| 383 | is_running); |
| 384 | |
| 385 | if (discard_thread_plans) |
| 386 | { |
| 387 | if (log) |
| 388 | log->Printf ("ProcessKDP::InterruptIfRunning() discarding all thread plans"); |
| 389 | m_thread_list.DiscardThreadPlans(); |
| 390 | } |
| 391 | if (is_running) |
| 392 | { |
| 393 | if (catch_stop_event) |
| 394 | { |
| 395 | if (log) |
| 396 | log->Printf ("ProcessKDP::InterruptIfRunning() pausing private state thread"); |
| 397 | PausePrivateStateThread(); |
| 398 | paused_private_state_thread = true; |
| 399 | } |
| 400 | |
| 401 | bool timed_out = false; |
| 402 | // bool sent_interrupt = false; |
| 403 | Mutex::Locker locker; |
| 404 | |
| 405 | // TODO: implement halt in CommunicationKDP |
| 406 | // if (!m_comm.SendInterrupt (locker, 1, sent_interrupt, timed_out)) |
| 407 | // { |
| 408 | // if (timed_out) |
| 409 | // error.SetErrorString("timed out sending interrupt packet"); |
| 410 | // else |
| 411 | // error.SetErrorString("unknown error sending interrupt packet"); |
| 412 | // if (paused_private_state_thread) |
| 413 | // ResumePrivateStateThread(); |
| 414 | // return error; |
| 415 | // } |
| 416 | |
| 417 | if (catch_stop_event) |
| 418 | { |
| 419 | // LISTEN HERE |
| 420 | TimeValue timeout_time; |
| 421 | timeout_time = TimeValue::Now(); |
| 422 | timeout_time.OffsetWithSeconds(5); |
| 423 | StateType state = WaitForStateChangedEventsPrivate (&timeout_time, stop_event_sp); |
| 424 | |
| 425 | timed_out = state == eStateInvalid; |
| 426 | if (log) |
| 427 | log->Printf ("ProcessKDP::InterruptIfRunning() catch stop event: state = %s, timed-out=%i", StateAsCString(state), timed_out); |
| 428 | |
| 429 | if (timed_out) |
| 430 | error.SetErrorString("unable to verify target stopped"); |
| 431 | } |
| 432 | |
| 433 | if (paused_private_state_thread) |
| 434 | { |
| 435 | if (log) |
| 436 | log->Printf ("ProcessKDP::InterruptIfRunning() resuming private state thread"); |
| 437 | ResumePrivateStateThread(); |
| 438 | } |
| 439 | } |
| 440 | return error; |
| 441 | } |
| 442 | |
| 443 | Error |
| 444 | ProcessKDP::WillDetach () |
| 445 | { |
| 446 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); |
| 447 | if (log) |
| 448 | log->Printf ("ProcessKDP::WillDetach()"); |
| 449 | |
| 450 | bool discard_thread_plans = true; |
| 451 | bool catch_stop_event = true; |
| 452 | EventSP event_sp; |
| 453 | return InterruptIfRunning (discard_thread_plans, catch_stop_event, event_sp); |
| 454 | } |
| 455 | |
| 456 | Error |
| 457 | ProcessKDP::DoDetach() |
| 458 | { |
| 459 | Error error; |
| 460 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); |
| 461 | if (log) |
| 462 | log->Printf ("ProcessKDP::DoDetach()"); |
| 463 | |
| 464 | DisableAllBreakpointSites (); |
| 465 | |
| 466 | m_thread_list.DiscardThreadPlans(); |
| 467 | |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 468 | if (m_comm.IsConnected()) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 469 | { |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 470 | |
| 471 | m_comm.SendRequestDisconnect(); |
| 472 | |
| 473 | size_t response_size = m_comm.Disconnect (); |
| 474 | if (log) |
| 475 | { |
| 476 | if (response_size) |
| 477 | log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully"); |
| 478 | else |
| 479 | log->PutCString ("ProcessKDP::DoDetach() detach packet send failed"); |
| 480 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 481 | } |
| 482 | // Sleep for one second to let the process get all detached... |
| 483 | StopAsyncThread (); |
| 484 | |
Greg Clayton | db9d6f4 | 2012-01-31 04:56:17 +0000 | [diff] [blame] | 485 | m_comm.Clear(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 486 | |
| 487 | SetPrivateState (eStateDetached); |
| 488 | ResumePrivateStateThread(); |
| 489 | |
| 490 | //KillDebugserverProcess (); |
| 491 | return error; |
| 492 | } |
| 493 | |
| 494 | Error |
| 495 | ProcessKDP::DoDestroy () |
| 496 | { |
| 497 | Error error; |
| 498 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); |
| 499 | if (log) |
| 500 | log->Printf ("ProcessKDP::DoDestroy()"); |
| 501 | |
| 502 | // Interrupt if our inferior is running... |
| 503 | if (m_comm.IsConnected()) |
| 504 | { |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 505 | if (m_public_state.GetValue() == eStateAttaching) |
| 506 | { |
| 507 | // We are being asked to halt during an attach. We need to just close |
| 508 | // our file handle and debugserver will go away, and we can be done... |
| 509 | m_comm.Disconnect(); |
| 510 | } |
| 511 | else |
| 512 | { |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 513 | DisableAllBreakpointSites (); |
| 514 | |
| 515 | m_comm.SendRequestDisconnect(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 516 | |
| 517 | StringExtractor response; |
| 518 | // TODO: Send kill packet? |
| 519 | SetExitStatus(SIGABRT, NULL); |
| 520 | } |
| 521 | } |
| 522 | StopAsyncThread (); |
Greg Clayton | db9d6f4 | 2012-01-31 04:56:17 +0000 | [diff] [blame] | 523 | m_comm.Clear(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 524 | return error; |
| 525 | } |
| 526 | |
| 527 | //------------------------------------------------------------------ |
| 528 | // Process Queries |
| 529 | //------------------------------------------------------------------ |
| 530 | |
| 531 | bool |
| 532 | ProcessKDP::IsAlive () |
| 533 | { |
| 534 | return m_comm.IsConnected() && m_private_state.GetValue() != eStateExited; |
| 535 | } |
| 536 | |
| 537 | //------------------------------------------------------------------ |
| 538 | // Process Memory |
| 539 | //------------------------------------------------------------------ |
| 540 | size_t |
| 541 | ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 542 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 543 | if (m_comm.IsConnected()) |
| 544 | return m_comm.SendRequestReadMemory (addr, buf, size, error); |
| 545 | error.SetErrorString ("not connected"); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | size_t |
| 550 | ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 551 | { |
| 552 | error.SetErrorString ("ProcessKDP::DoReadMemory not implemented"); |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | lldb::addr_t |
| 557 | ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error) |
| 558 | { |
| 559 | error.SetErrorString ("memory allocation not suppported in kdp remote debugging"); |
| 560 | return LLDB_INVALID_ADDRESS; |
| 561 | } |
| 562 | |
| 563 | Error |
| 564 | ProcessKDP::DoDeallocateMemory (lldb::addr_t addr) |
| 565 | { |
| 566 | Error error; |
| 567 | error.SetErrorString ("memory deallocation not suppported in kdp remote debugging"); |
| 568 | return error; |
| 569 | } |
| 570 | |
| 571 | Error |
| 572 | ProcessKDP::EnableBreakpoint (BreakpointSite *bp_site) |
| 573 | { |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 574 | if (m_comm.LocalBreakpointsAreSupported ()) |
| 575 | { |
| 576 | Error error; |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 577 | if (!bp_site->IsEnabled()) |
| 578 | { |
| 579 | if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress())) |
| 580 | { |
| 581 | bp_site->SetEnabled(true); |
| 582 | bp_site->SetType (BreakpointSite::eExternal); |
| 583 | } |
| 584 | else |
| 585 | { |
| 586 | error.SetErrorString ("KDP set breakpoint failed"); |
| 587 | } |
| 588 | } |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 589 | return error; |
| 590 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 591 | return EnableSoftwareBreakpoint (bp_site); |
| 592 | } |
| 593 | |
| 594 | Error |
| 595 | ProcessKDP::DisableBreakpoint (BreakpointSite *bp_site) |
| 596 | { |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 597 | if (m_comm.LocalBreakpointsAreSupported ()) |
| 598 | { |
| 599 | Error error; |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 600 | if (bp_site->IsEnabled()) |
| 601 | { |
| 602 | BreakpointSite::Type bp_type = bp_site->GetType(); |
| 603 | if (bp_type == BreakpointSite::eExternal) |
| 604 | { |
| 605 | if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress())) |
| 606 | bp_site->SetEnabled(false); |
| 607 | else |
| 608 | error.SetErrorString ("KDP remove breakpoint failed"); |
| 609 | } |
| 610 | else |
| 611 | { |
| 612 | error = DisableSoftwareBreakpoint (bp_site); |
| 613 | } |
| 614 | } |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 615 | return error; |
| 616 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 617 | return DisableSoftwareBreakpoint (bp_site); |
| 618 | } |
| 619 | |
| 620 | Error |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 621 | ProcessKDP::EnableWatchpoint (Watchpoint *wp) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 622 | { |
| 623 | Error error; |
| 624 | error.SetErrorString ("watchpoints are not suppported in kdp remote debugging"); |
| 625 | return error; |
| 626 | } |
| 627 | |
| 628 | Error |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 629 | ProcessKDP::DisableWatchpoint (Watchpoint *wp) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 630 | { |
| 631 | Error error; |
| 632 | error.SetErrorString ("watchpoints are not suppported in kdp remote debugging"); |
| 633 | return error; |
| 634 | } |
| 635 | |
| 636 | void |
| 637 | ProcessKDP::Clear() |
| 638 | { |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 639 | m_thread_list.Clear(); |
| 640 | } |
| 641 | |
| 642 | Error |
| 643 | ProcessKDP::DoSignal (int signo) |
| 644 | { |
| 645 | Error error; |
| 646 | error.SetErrorString ("sending signals is not suppported in kdp remote debugging"); |
| 647 | return error; |
| 648 | } |
| 649 | |
| 650 | void |
| 651 | ProcessKDP::Initialize() |
| 652 | { |
| 653 | static bool g_initialized = false; |
| 654 | |
| 655 | if (g_initialized == false) |
| 656 | { |
| 657 | g_initialized = true; |
| 658 | PluginManager::RegisterPlugin (GetPluginNameStatic(), |
| 659 | GetPluginDescriptionStatic(), |
| 660 | CreateInstance); |
| 661 | |
| 662 | Log::Callbacks log_callbacks = { |
| 663 | ProcessKDPLog::DisableLog, |
| 664 | ProcessKDPLog::EnableLog, |
| 665 | ProcessKDPLog::ListLogCategories |
| 666 | }; |
| 667 | |
| 668 | Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | bool |
| 673 | ProcessKDP::StartAsyncThread () |
| 674 | { |
| 675 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); |
| 676 | |
| 677 | if (log) |
| 678 | log->Printf ("ProcessKDP::%s ()", __FUNCTION__); |
| 679 | |
| 680 | // Create a thread that watches our internal state and controls which |
| 681 | // events make it to clients (into the DCProcess event queue). |
| 682 | m_async_thread = Host::ThreadCreate ("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL); |
| 683 | return IS_VALID_LLDB_HOST_THREAD(m_async_thread); |
| 684 | } |
| 685 | |
| 686 | void |
| 687 | ProcessKDP::StopAsyncThread () |
| 688 | { |
| 689 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); |
| 690 | |
| 691 | if (log) |
| 692 | log->Printf ("ProcessKDP::%s ()", __FUNCTION__); |
| 693 | |
| 694 | m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit); |
| 695 | |
| 696 | // Stop the stdio thread |
| 697 | if (IS_VALID_LLDB_HOST_THREAD(m_async_thread)) |
| 698 | { |
| 699 | Host::ThreadJoin (m_async_thread, NULL, NULL); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | |
| 704 | void * |
| 705 | ProcessKDP::AsyncThread (void *arg) |
| 706 | { |
| 707 | ProcessKDP *process = (ProcessKDP*) arg; |
| 708 | |
| 709 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); |
| 710 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 711 | log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, arg, process->GetID()); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 712 | |
| 713 | Listener listener ("ProcessKDP::AsyncThread"); |
| 714 | EventSP event_sp; |
| 715 | const uint32_t desired_event_mask = eBroadcastBitAsyncContinue | |
| 716 | eBroadcastBitAsyncThreadShouldExit; |
| 717 | |
| 718 | if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask) |
| 719 | { |
| 720 | listener.StartListeningForEvents (&process->m_comm, Communication::eBroadcastBitReadThreadDidExit); |
| 721 | |
| 722 | bool done = false; |
| 723 | while (!done) |
| 724 | { |
| 725 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 726 | log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp)...", __FUNCTION__, arg, process->GetID()); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 727 | if (listener.WaitForEvent (NULL, event_sp)) |
| 728 | { |
| 729 | const uint32_t event_type = event_sp->GetType(); |
| 730 | if (event_sp->BroadcasterIs (&process->m_async_broadcaster)) |
| 731 | { |
| 732 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 733 | log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) Got an event of type: %d...", __FUNCTION__, arg, process->GetID(), event_type); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 734 | |
| 735 | switch (event_type) |
| 736 | { |
| 737 | case eBroadcastBitAsyncContinue: |
| 738 | { |
| 739 | const EventDataBytes *continue_packet = EventDataBytes::GetEventDataFromEvent(event_sp.get()); |
| 740 | |
| 741 | if (continue_packet) |
| 742 | { |
| 743 | // TODO: do continue support here |
| 744 | |
| 745 | // const char *continue_cstr = (const char *)continue_packet->GetBytes (); |
| 746 | // const size_t continue_cstr_len = continue_packet->GetByteSize (); |
| 747 | // if (log) |
| 748 | // log->Printf ("ProcessKDP::%s (arg = %p, pid = %i) got eBroadcastBitAsyncContinue: %s", __FUNCTION__, arg, process->GetID(), continue_cstr); |
| 749 | // |
| 750 | // if (::strstr (continue_cstr, "vAttach") == NULL) |
| 751 | // process->SetPrivateState(eStateRunning); |
| 752 | // StringExtractor response; |
| 753 | // StateType stop_state = process->GetCommunication().SendContinuePacketAndWaitForResponse (process, continue_cstr, continue_cstr_len, response); |
| 754 | // |
| 755 | // switch (stop_state) |
| 756 | // { |
| 757 | // case eStateStopped: |
| 758 | // case eStateCrashed: |
| 759 | // case eStateSuspended: |
| 760 | // process->m_last_stop_packet = response; |
| 761 | // process->SetPrivateState (stop_state); |
| 762 | // break; |
| 763 | // |
| 764 | // case eStateExited: |
| 765 | // process->m_last_stop_packet = response; |
| 766 | // response.SetFilePos(1); |
| 767 | // process->SetExitStatus(response.GetHexU8(), NULL); |
| 768 | // done = true; |
| 769 | // break; |
| 770 | // |
| 771 | // case eStateInvalid: |
| 772 | // process->SetExitStatus(-1, "lost connection"); |
| 773 | // break; |
| 774 | // |
| 775 | // default: |
| 776 | // process->SetPrivateState (stop_state); |
| 777 | // break; |
| 778 | // } |
| 779 | } |
| 780 | } |
| 781 | break; |
| 782 | |
| 783 | case eBroadcastBitAsyncThreadShouldExit: |
| 784 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 785 | log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) got eBroadcastBitAsyncThreadShouldExit...", __FUNCTION__, arg, process->GetID()); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 786 | done = true; |
| 787 | break; |
| 788 | |
| 789 | default: |
| 790 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 791 | log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) got unknown event 0x%8.8x", __FUNCTION__, arg, process->GetID(), event_type); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 792 | done = true; |
| 793 | break; |
| 794 | } |
| 795 | } |
| 796 | else if (event_sp->BroadcasterIs (&process->m_comm)) |
| 797 | { |
| 798 | if (event_type & Communication::eBroadcastBitReadThreadDidExit) |
| 799 | { |
| 800 | process->SetExitStatus (-1, "lost connection"); |
| 801 | done = true; |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | else |
| 806 | { |
| 807 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 808 | log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) listener.WaitForEvent (NULL, event_sp) => false", __FUNCTION__, arg, process->GetID()); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 809 | done = true; |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 815 | log->Printf ("ProcessKDP::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, arg, process->GetID()); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 816 | |
| 817 | process->m_async_thread = LLDB_INVALID_HOST_THREAD; |
| 818 | return NULL; |
| 819 | } |
| 820 | |
| 821 | |