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