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