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