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