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