1 //===-- ProcessKDP.cpp ------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <errno.h>
10 #include <stdlib.h>
11 
12 #include <mutex>
13 
14 #include "lldb/Core/Debugger.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/ModuleSpec.h"
17 #include "lldb/Core/PluginManager.h"
18 #include "lldb/Host/ConnectionFileDescriptor.h"
19 #include "lldb/Host/Host.h"
20 #include "lldb/Host/Symbols.h"
21 #include "lldb/Host/ThreadLauncher.h"
22 #include "lldb/Host/common/TCPSocket.h"
23 #include "lldb/Interpreter/CommandInterpreter.h"
24 #include "lldb/Interpreter/CommandObject.h"
25 #include "lldb/Interpreter/CommandObjectMultiword.h"
26 #include "lldb/Interpreter/CommandReturnObject.h"
27 #include "lldb/Interpreter/OptionGroupString.h"
28 #include "lldb/Interpreter/OptionGroupUInt64.h"
29 #include "lldb/Interpreter/OptionValueProperties.h"
30 #include "lldb/Symbol/ObjectFile.h"
31 #include "lldb/Target/RegisterContext.h"
32 #include "lldb/Target/Target.h"
33 #include "lldb/Target/Thread.h"
34 #include "lldb/Utility/State.h"
35 #include "lldb/Utility/StringExtractor.h"
36 #include "lldb/Utility/UUID.h"
37 
38 #include "llvm/Support/Threading.h"
39 
40 #define USEC_PER_SEC 1000000
41 
42 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
43 #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
44 #include "ProcessKDP.h"
45 #include "ProcessKDPLog.h"
46 #include "ThreadKDP.h"
47 
48 using namespace lldb;
49 using namespace lldb_private;
50 
51 namespace {
52 
53 static constexpr PropertyDefinition g_properties[] = {
54     {"packet-timeout", OptionValue::eTypeUInt64, true, 5, NULL, {},
55      "Specify the default packet timeout in seconds."}};
56 
57 enum { ePropertyPacketTimeout };
58 
59 class PluginProperties : public Properties {
60 public:
61   static ConstString GetSettingName() {
62     return ProcessKDP::GetPluginNameStatic();
63   }
64 
65   PluginProperties() : Properties() {
66     m_collection_sp.reset(new OptionValueProperties(GetSettingName()));
67     m_collection_sp->Initialize(g_properties);
68   }
69 
70   virtual ~PluginProperties() {}
71 
72   uint64_t GetPacketTimeout() {
73     const uint32_t idx = ePropertyPacketTimeout;
74     return m_collection_sp->GetPropertyAtIndexAsUInt64(
75         NULL, idx, g_properties[idx].default_uint_value);
76   }
77 };
78 
79 typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP;
80 
81 static const ProcessKDPPropertiesSP &GetGlobalPluginProperties() {
82   static ProcessKDPPropertiesSP g_settings_sp;
83   if (!g_settings_sp)
84     g_settings_sp.reset(new PluginProperties());
85   return g_settings_sp;
86 }
87 
88 } // anonymous namespace end
89 
90 static const lldb::tid_t g_kernel_tid = 1;
91 
92 ConstString ProcessKDP::GetPluginNameStatic() {
93   static ConstString g_name("kdp-remote");
94   return g_name;
95 }
96 
97 const char *ProcessKDP::GetPluginDescriptionStatic() {
98   return "KDP Remote protocol based debugging plug-in for darwin kernel "
99          "debugging.";
100 }
101 
102 void ProcessKDP::Terminate() {
103   PluginManager::UnregisterPlugin(ProcessKDP::CreateInstance);
104 }
105 
106 lldb::ProcessSP ProcessKDP::CreateInstance(TargetSP target_sp,
107                                            ListenerSP listener_sp,
108                                            const FileSpec *crash_file_path) {
109   lldb::ProcessSP process_sp;
110   if (crash_file_path == NULL)
111     process_sp.reset(new ProcessKDP(target_sp, listener_sp));
112   return process_sp;
113 }
114 
115 bool ProcessKDP::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) {
116   if (plugin_specified_by_name)
117     return true;
118 
119   // For now we are just making sure the file exists for a given module
120   Module *exe_module = target_sp->GetExecutableModulePointer();
121   if (exe_module) {
122     const llvm::Triple &triple_ref = target_sp->GetArchitecture().GetTriple();
123     switch (triple_ref.getOS()) {
124     case llvm::Triple::Darwin: // Should use "macosx" for desktop and "ios" for
125                                // iOS, but accept darwin just in case
126     case llvm::Triple::MacOSX: // For desktop targets
127     case llvm::Triple::IOS:    // For arm targets
128     case llvm::Triple::TvOS:
129     case llvm::Triple::WatchOS:
130       if (triple_ref.getVendor() == llvm::Triple::Apple) {
131         ObjectFile *exe_objfile = exe_module->GetObjectFile();
132         if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&
133             exe_objfile->GetStrata() == ObjectFile::eStrataKernel)
134           return true;
135       }
136       break;
137 
138     default:
139       break;
140     }
141   }
142   return false;
143 }
144 
145 //----------------------------------------------------------------------
146 // ProcessKDP constructor
147 //----------------------------------------------------------------------
148 ProcessKDP::ProcessKDP(TargetSP target_sp, ListenerSP listener_sp)
149     : Process(target_sp, listener_sp),
150       m_comm("lldb.process.kdp-remote.communication"),
151       m_async_broadcaster(NULL, "lldb.process.kdp-remote.async-broadcaster"),
152       m_dyld_plugin_name(), m_kernel_load_addr(LLDB_INVALID_ADDRESS),
153       m_command_sp(), m_kernel_thread_wp() {
154   m_async_broadcaster.SetEventName(eBroadcastBitAsyncThreadShouldExit,
155                                    "async thread should exit");
156   m_async_broadcaster.SetEventName(eBroadcastBitAsyncContinue,
157                                    "async thread continue");
158   const uint64_t timeout_seconds =
159       GetGlobalPluginProperties()->GetPacketTimeout();
160   if (timeout_seconds > 0)
161     m_comm.SetPacketTimeout(std::chrono::seconds(timeout_seconds));
162 }
163 
164 //----------------------------------------------------------------------
165 // Destructor
166 //----------------------------------------------------------------------
167 ProcessKDP::~ProcessKDP() {
168   Clear();
169   // We need to call finalize on the process before destroying ourselves to
170   // make sure all of the broadcaster cleanup goes as planned. If we destruct
171   // this class, then Process::~Process() might have problems trying to fully
172   // destroy the broadcaster.
173   Finalize();
174 }
175 
176 //----------------------------------------------------------------------
177 // PluginInterface
178 //----------------------------------------------------------------------
179 lldb_private::ConstString ProcessKDP::GetPluginName() {
180   return GetPluginNameStatic();
181 }
182 
183 uint32_t ProcessKDP::GetPluginVersion() { return 1; }
184 
185 Status ProcessKDP::WillLaunch(Module *module) {
186   Status error;
187   error.SetErrorString("launching not supported in kdp-remote plug-in");
188   return error;
189 }
190 
191 Status ProcessKDP::WillAttachToProcessWithID(lldb::pid_t pid) {
192   Status error;
193   error.SetErrorString(
194       "attaching to a by process ID not supported in kdp-remote plug-in");
195   return error;
196 }
197 
198 Status ProcessKDP::WillAttachToProcessWithName(const char *process_name,
199                                                bool wait_for_launch) {
200   Status error;
201   error.SetErrorString(
202       "attaching to a by process name not supported in kdp-remote plug-in");
203   return error;
204 }
205 
206 bool ProcessKDP::GetHostArchitecture(ArchSpec &arch) {
207   uint32_t cpu = m_comm.GetCPUType();
208   if (cpu) {
209     uint32_t sub = m_comm.GetCPUSubtype();
210     arch.SetArchitecture(eArchTypeMachO, cpu, sub);
211     // Leave architecture vendor as unspecified unknown
212     arch.GetTriple().setVendor(llvm::Triple::UnknownVendor);
213     arch.GetTriple().setVendorName(llvm::StringRef());
214     return true;
215   }
216   arch.Clear();
217   return false;
218 }
219 
220 Status ProcessKDP::DoConnectRemote(Stream *strm, llvm::StringRef remote_url) {
221   Status error;
222 
223   // Don't let any JIT happen when doing KDP as we can't allocate memory and we
224   // don't want to be mucking with threads that might already be handling
225   // exceptions
226   SetCanJIT(false);
227 
228   if (remote_url.empty()) {
229     error.SetErrorStringWithFormat("empty connection URL");
230     return error;
231   }
232 
233   std::unique_ptr<ConnectionFileDescriptor> conn_ap(
234       new ConnectionFileDescriptor());
235   if (conn_ap.get()) {
236     // Only try once for now.
237     // TODO: check if we should be retrying?
238     const uint32_t max_retry_count = 1;
239     for (uint32_t retry_count = 0; retry_count < max_retry_count;
240          ++retry_count) {
241       if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess)
242         break;
243       usleep(100000);
244     }
245   }
246 
247   if (conn_ap->IsConnected()) {
248     const TCPSocket &socket =
249         static_cast<const TCPSocket &>(*conn_ap->GetReadObject());
250     const uint16_t reply_port = socket.GetLocalPortNumber();
251 
252     if (reply_port != 0) {
253       m_comm.SetConnection(conn_ap.release());
254 
255       if (m_comm.SendRequestReattach(reply_port)) {
256         if (m_comm.SendRequestConnect(reply_port, reply_port,
257                                       "Greetings from LLDB...")) {
258           m_comm.GetVersion();
259 
260           Target &target = GetTarget();
261           ArchSpec kernel_arch;
262           // The host architecture
263           GetHostArchitecture(kernel_arch);
264           ArchSpec target_arch = target.GetArchitecture();
265           // Merge in any unspecified stuff into the target architecture in
266           // case the target arch isn't set at all or incompletely.
267           target_arch.MergeFrom(kernel_arch);
268           target.SetArchitecture(target_arch);
269 
270           /* Get the kernel's UUID and load address via KDP_KERNELVERSION
271            * packet.  */
272           /* An EFI kdp session has neither UUID nor load address. */
273 
274           UUID kernel_uuid = m_comm.GetUUID();
275           addr_t kernel_load_addr = m_comm.GetLoadAddress();
276 
277           if (m_comm.RemoteIsEFI()) {
278             // Select an invalid plugin name for the dynamic loader so one
279             // doesn't get used since EFI does its own manual loading via
280             // python scripting
281             static ConstString g_none_dynamic_loader("none");
282             m_dyld_plugin_name = g_none_dynamic_loader;
283 
284             if (kernel_uuid.IsValid()) {
285               // If EFI passed in a UUID= try to lookup UUID The slide will not
286               // be provided. But the UUID lookup will be used to launch EFI
287               // debug scripts from the dSYM, that can load all of the symbols.
288               ModuleSpec module_spec;
289               module_spec.GetUUID() = kernel_uuid;
290               module_spec.GetArchitecture() = target.GetArchitecture();
291 
292               // Lookup UUID locally, before attempting dsymForUUID like action
293               module_spec.GetSymbolFileSpec() =
294                   Symbols::LocateExecutableSymbolFile(module_spec);
295               if (module_spec.GetSymbolFileSpec()) {
296                 ModuleSpec executable_module_spec =
297                     Symbols::LocateExecutableObjectFile(module_spec);
298                 if (FileSystem::Instance().Exists(
299                         executable_module_spec.GetFileSpec())) {
300                   module_spec.GetFileSpec() =
301                       executable_module_spec.GetFileSpec();
302                 }
303               }
304               if (!module_spec.GetSymbolFileSpec() ||
305                   !module_spec.GetSymbolFileSpec())
306                 Symbols::DownloadObjectAndSymbolFile(module_spec, true);
307 
308               if (FileSystem::Instance().Exists(module_spec.GetFileSpec())) {
309                 ModuleSP module_sp(new Module(module_spec));
310                 if (module_sp.get() && module_sp->GetObjectFile()) {
311                   // Get the current target executable
312                   ModuleSP exe_module_sp(target.GetExecutableModule());
313 
314                   // Make sure you don't already have the right module loaded
315                   // and they will be uniqued
316                   if (exe_module_sp.get() != module_sp.get())
317                     target.SetExecutableModule(module_sp, eLoadDependentsNo);
318                 }
319               }
320             }
321           } else if (m_comm.RemoteIsDarwinKernel()) {
322             m_dyld_plugin_name =
323                 DynamicLoaderDarwinKernel::GetPluginNameStatic();
324             if (kernel_load_addr != LLDB_INVALID_ADDRESS) {
325               m_kernel_load_addr = kernel_load_addr;
326             }
327           }
328 
329           // Set the thread ID
330           UpdateThreadListIfNeeded();
331           SetID(1);
332           GetThreadList();
333           SetPrivateState(eStateStopped);
334           StreamSP async_strm_sp(target.GetDebugger().GetAsyncOutputStream());
335           if (async_strm_sp) {
336             const char *cstr;
337             if ((cstr = m_comm.GetKernelVersion()) != NULL) {
338               async_strm_sp->Printf("Version: %s\n", cstr);
339               async_strm_sp->Flush();
340             }
341             //                      if ((cstr = m_comm.GetImagePath ()) != NULL)
342             //                      {
343             //                          async_strm_sp->Printf ("Image Path:
344             //                          %s\n", cstr);
345             //                          async_strm_sp->Flush();
346             //                      }
347           }
348         } else {
349           error.SetErrorString("KDP_REATTACH failed");
350         }
351       } else {
352         error.SetErrorString("KDP_REATTACH failed");
353       }
354     } else {
355       error.SetErrorString("invalid reply port from UDP connection");
356     }
357   } else {
358     if (error.Success())
359       error.SetErrorStringWithFormat("failed to connect to '%s'",
360                                      remote_url.str().c_str());
361   }
362   if (error.Fail())
363     m_comm.Disconnect();
364 
365   return error;
366 }
367 
368 //----------------------------------------------------------------------
369 // Process Control
370 //----------------------------------------------------------------------
371 Status ProcessKDP::DoLaunch(Module *exe_module,
372                             ProcessLaunchInfo &launch_info) {
373   Status error;
374   error.SetErrorString("launching not supported in kdp-remote plug-in");
375   return error;
376 }
377 
378 Status
379 ProcessKDP::DoAttachToProcessWithID(lldb::pid_t attach_pid,
380                                     const ProcessAttachInfo &attach_info) {
381   Status error;
382   error.SetErrorString(
383       "attach to process by ID is not supported in kdp remote debugging");
384   return error;
385 }
386 
387 Status
388 ProcessKDP::DoAttachToProcessWithName(const char *process_name,
389                                       const ProcessAttachInfo &attach_info) {
390   Status error;
391   error.SetErrorString(
392       "attach to process by name is not supported in kdp remote debugging");
393   return error;
394 }
395 
396 void ProcessKDP::DidAttach(ArchSpec &process_arch) {
397   Process::DidAttach(process_arch);
398 
399   Log *log(ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
400   if (log)
401     log->Printf("ProcessKDP::DidAttach()");
402   if (GetID() != LLDB_INVALID_PROCESS_ID) {
403     GetHostArchitecture(process_arch);
404   }
405 }
406 
407 addr_t ProcessKDP::GetImageInfoAddress() { return m_kernel_load_addr; }
408 
409 lldb_private::DynamicLoader *ProcessKDP::GetDynamicLoader() {
410   if (m_dyld_ap.get() == NULL)
411     m_dyld_ap.reset(DynamicLoader::FindPlugin(
412         this,
413         m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString()));
414   return m_dyld_ap.get();
415 }
416 
417 Status ProcessKDP::WillResume() { return Status(); }
418 
419 Status ProcessKDP::DoResume() {
420   Status error;
421   Log *log(ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
422   // Only start the async thread if we try to do any process control
423   if (!m_async_thread.IsJoinable())
424     StartAsyncThread();
425 
426   bool resume = false;
427 
428   // With KDP there is only one thread we can tell what to do
429   ThreadSP kernel_thread_sp(m_thread_list.FindThreadByProtocolID(g_kernel_tid));
430 
431   if (kernel_thread_sp) {
432     const StateType thread_resume_state =
433         kernel_thread_sp->GetTemporaryResumeState();
434 
435     if (log)
436       log->Printf("ProcessKDP::DoResume() thread_resume_state = %s",
437                   StateAsCString(thread_resume_state));
438     switch (thread_resume_state) {
439     case eStateSuspended:
440       // Nothing to do here when a thread will stay suspended we just leave the
441       // CPU mask bit set to zero for the thread
442       if (log)
443         log->Printf("ProcessKDP::DoResume() = suspended???");
444       break;
445 
446     case eStateStepping: {
447       lldb::RegisterContextSP reg_ctx_sp(
448           kernel_thread_sp->GetRegisterContext());
449 
450       if (reg_ctx_sp) {
451         if (log)
452           log->Printf(
453               "ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (true);");
454         reg_ctx_sp->HardwareSingleStep(true);
455         resume = true;
456       } else {
457         error.SetErrorStringWithFormat(
458             "KDP thread 0x%llx has no register context",
459             kernel_thread_sp->GetID());
460       }
461     } break;
462 
463     case eStateRunning: {
464       lldb::RegisterContextSP reg_ctx_sp(
465           kernel_thread_sp->GetRegisterContext());
466 
467       if (reg_ctx_sp) {
468         if (log)
469           log->Printf("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep "
470                       "(false);");
471         reg_ctx_sp->HardwareSingleStep(false);
472         resume = true;
473       } else {
474         error.SetErrorStringWithFormat(
475             "KDP thread 0x%llx has no register context",
476             kernel_thread_sp->GetID());
477       }
478     } break;
479 
480     default:
481       // The only valid thread resume states are listed above
482       llvm_unreachable("invalid thread resume state");
483     }
484   }
485 
486   if (resume) {
487     if (log)
488       log->Printf("ProcessKDP::DoResume () sending resume");
489 
490     if (m_comm.SendRequestResume()) {
491       m_async_broadcaster.BroadcastEvent(eBroadcastBitAsyncContinue);
492       SetPrivateState(eStateRunning);
493     } else
494       error.SetErrorString("KDP resume failed");
495   } else {
496     error.SetErrorString("kernel thread is suspended");
497   }
498 
499   return error;
500 }
501 
502 lldb::ThreadSP ProcessKDP::GetKernelThread() {
503   // KDP only tells us about one thread/core. Any other threads will usually
504   // be the ones that are read from memory by the OS plug-ins.
505 
506   ThreadSP thread_sp(m_kernel_thread_wp.lock());
507   if (!thread_sp) {
508     thread_sp.reset(new ThreadKDP(*this, g_kernel_tid));
509     m_kernel_thread_wp = thread_sp;
510   }
511   return thread_sp;
512 }
513 
514 bool ProcessKDP::UpdateThreadList(ThreadList &old_thread_list,
515                                   ThreadList &new_thread_list) {
516   // locker will keep a mutex locked until it goes out of scope
517   Log *log(ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_THREAD));
518   LLDB_LOGV(log, "pid = {0}", GetID());
519 
520   // Even though there is a CPU mask, it doesn't mean we can see each CPU
521   // individually, there is really only one. Lets call this thread 1.
522   ThreadSP thread_sp(
523       old_thread_list.FindThreadByProtocolID(g_kernel_tid, false));
524   if (!thread_sp)
525     thread_sp = GetKernelThread();
526   new_thread_list.AddThread(thread_sp);
527 
528   return new_thread_list.GetSize(false) > 0;
529 }
530 
531 void ProcessKDP::RefreshStateAfterStop() {
532   // Let all threads recover from stopping and do any clean up based on the
533   // previous thread state (if any).
534   m_thread_list.RefreshStateAfterStop();
535 }
536 
537 Status ProcessKDP::DoHalt(bool &caused_stop) {
538   Status error;
539 
540   if (m_comm.IsRunning()) {
541     if (m_destroy_in_process) {
542       // If we are attempting to destroy, we need to not return an error to Halt
543       // or DoDestroy won't get called. We are also currently running, so send
544       // a process stopped event
545       SetPrivateState(eStateStopped);
546     } else {
547       error.SetErrorString("KDP cannot interrupt a running kernel");
548     }
549   }
550   return error;
551 }
552 
553 Status ProcessKDP::DoDetach(bool keep_stopped) {
554   Status error;
555   Log *log(ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
556   if (log)
557     log->Printf("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped);
558 
559   if (m_comm.IsRunning()) {
560     // We are running and we can't interrupt a running kernel, so we need to
561     // just close the connection to the kernel and hope for the best
562   } else {
563     // If we are going to keep the target stopped, then don't send the
564     // disconnect message.
565     if (!keep_stopped && m_comm.IsConnected()) {
566       const bool success = m_comm.SendRequestDisconnect();
567       if (log) {
568         if (success)
569           log->PutCString(
570               "ProcessKDP::DoDetach() detach packet sent successfully");
571         else
572           log->PutCString(
573               "ProcessKDP::DoDetach() connection channel shutdown failed");
574       }
575       m_comm.Disconnect();
576     }
577   }
578   StopAsyncThread();
579   m_comm.Clear();
580 
581   SetPrivateState(eStateDetached);
582   ResumePrivateStateThread();
583 
584   // KillDebugserverProcess ();
585   return error;
586 }
587 
588 Status ProcessKDP::DoDestroy() {
589   // For KDP there really is no difference between destroy and detach
590   bool keep_stopped = false;
591   return DoDetach(keep_stopped);
592 }
593 
594 //------------------------------------------------------------------
595 // Process Queries
596 //------------------------------------------------------------------
597 
598 bool ProcessKDP::IsAlive() {
599   return m_comm.IsConnected() && Process::IsAlive();
600 }
601 
602 //------------------------------------------------------------------
603 // Process Memory
604 //------------------------------------------------------------------
605 size_t ProcessKDP::DoReadMemory(addr_t addr, void *buf, size_t size,
606                                 Status &error) {
607   uint8_t *data_buffer = (uint8_t *)buf;
608   if (m_comm.IsConnected()) {
609     const size_t max_read_size = 512;
610     size_t total_bytes_read = 0;
611 
612     // Read the requested amount of memory in 512 byte chunks
613     while (total_bytes_read < size) {
614       size_t bytes_to_read_this_request = size - total_bytes_read;
615       if (bytes_to_read_this_request > max_read_size) {
616         bytes_to_read_this_request = max_read_size;
617       }
618       size_t bytes_read = m_comm.SendRequestReadMemory(
619           addr + total_bytes_read, data_buffer + total_bytes_read,
620           bytes_to_read_this_request, error);
621       total_bytes_read += bytes_read;
622       if (error.Fail() || bytes_read == 0) {
623         return total_bytes_read;
624       }
625     }
626 
627     return total_bytes_read;
628   }
629   error.SetErrorString("not connected");
630   return 0;
631 }
632 
633 size_t ProcessKDP::DoWriteMemory(addr_t addr, const void *buf, size_t size,
634                                  Status &error) {
635   if (m_comm.IsConnected())
636     return m_comm.SendRequestWriteMemory(addr, buf, size, error);
637   error.SetErrorString("not connected");
638   return 0;
639 }
640 
641 lldb::addr_t ProcessKDP::DoAllocateMemory(size_t size, uint32_t permissions,
642                                           Status &error) {
643   error.SetErrorString(
644       "memory allocation not supported in kdp remote debugging");
645   return LLDB_INVALID_ADDRESS;
646 }
647 
648 Status ProcessKDP::DoDeallocateMemory(lldb::addr_t addr) {
649   Status error;
650   error.SetErrorString(
651       "memory deallocation not supported in kdp remote debugging");
652   return error;
653 }
654 
655 Status ProcessKDP::EnableBreakpointSite(BreakpointSite *bp_site) {
656   if (m_comm.LocalBreakpointsAreSupported()) {
657     Status error;
658     if (!bp_site->IsEnabled()) {
659       if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress())) {
660         bp_site->SetEnabled(true);
661         bp_site->SetType(BreakpointSite::eExternal);
662       } else {
663         error.SetErrorString("KDP set breakpoint failed");
664       }
665     }
666     return error;
667   }
668   return EnableSoftwareBreakpoint(bp_site);
669 }
670 
671 Status ProcessKDP::DisableBreakpointSite(BreakpointSite *bp_site) {
672   if (m_comm.LocalBreakpointsAreSupported()) {
673     Status error;
674     if (bp_site->IsEnabled()) {
675       BreakpointSite::Type bp_type = bp_site->GetType();
676       if (bp_type == BreakpointSite::eExternal) {
677         if (m_destroy_in_process && m_comm.IsRunning()) {
678           // We are trying to destroy our connection and we are running
679           bp_site->SetEnabled(false);
680         } else {
681           if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress()))
682             bp_site->SetEnabled(false);
683           else
684             error.SetErrorString("KDP remove breakpoint failed");
685         }
686       } else {
687         error = DisableSoftwareBreakpoint(bp_site);
688       }
689     }
690     return error;
691   }
692   return DisableSoftwareBreakpoint(bp_site);
693 }
694 
695 Status ProcessKDP::EnableWatchpoint(Watchpoint *wp, bool notify) {
696   Status error;
697   error.SetErrorString(
698       "watchpoints are not supported in kdp remote debugging");
699   return error;
700 }
701 
702 Status ProcessKDP::DisableWatchpoint(Watchpoint *wp, bool notify) {
703   Status error;
704   error.SetErrorString(
705       "watchpoints are not supported in kdp remote debugging");
706   return error;
707 }
708 
709 void ProcessKDP::Clear() { m_thread_list.Clear(); }
710 
711 Status ProcessKDP::DoSignal(int signo) {
712   Status error;
713   error.SetErrorString(
714       "sending signals is not supported in kdp remote debugging");
715   return error;
716 }
717 
718 void ProcessKDP::Initialize() {
719   static llvm::once_flag g_once_flag;
720 
721   llvm::call_once(g_once_flag, []() {
722     PluginManager::RegisterPlugin(GetPluginNameStatic(),
723                                   GetPluginDescriptionStatic(), CreateInstance,
724                                   DebuggerInitialize);
725 
726     ProcessKDPLog::Initialize();
727   });
728 }
729 
730 void ProcessKDP::DebuggerInitialize(lldb_private::Debugger &debugger) {
731   if (!PluginManager::GetSettingForProcessPlugin(
732           debugger, PluginProperties::GetSettingName())) {
733     const bool is_global_setting = true;
734     PluginManager::CreateSettingForProcessPlugin(
735         debugger, GetGlobalPluginProperties()->GetValueProperties(),
736         ConstString("Properties for the kdp-remote process plug-in."),
737         is_global_setting);
738   }
739 }
740 
741 bool ProcessKDP::StartAsyncThread() {
742   Log *log(ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
743 
744   if (log)
745     log->Printf("ProcessKDP::StartAsyncThread ()");
746 
747   if (m_async_thread.IsJoinable())
748     return true;
749 
750   m_async_thread = ThreadLauncher::LaunchThread(
751       "<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL);
752   return m_async_thread.IsJoinable();
753 }
754 
755 void ProcessKDP::StopAsyncThread() {
756   Log *log(ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
757 
758   if (log)
759     log->Printf("ProcessKDP::StopAsyncThread ()");
760 
761   m_async_broadcaster.BroadcastEvent(eBroadcastBitAsyncThreadShouldExit);
762 
763   // Stop the stdio thread
764   if (m_async_thread.IsJoinable())
765     m_async_thread.Join(nullptr);
766 }
767 
768 void *ProcessKDP::AsyncThread(void *arg) {
769   ProcessKDP *process = (ProcessKDP *)arg;
770 
771   const lldb::pid_t pid = process->GetID();
772 
773   Log *log(ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS));
774   if (log)
775     log->Printf("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64
776                 ") thread starting...",
777                 arg, pid);
778 
779   ListenerSP listener_sp(Listener::MakeListener("ProcessKDP::AsyncThread"));
780   EventSP event_sp;
781   const uint32_t desired_event_mask =
782       eBroadcastBitAsyncContinue | eBroadcastBitAsyncThreadShouldExit;
783 
784   if (listener_sp->StartListeningForEvents(&process->m_async_broadcaster,
785                                            desired_event_mask) ==
786       desired_event_mask) {
787     bool done = false;
788     while (!done) {
789       if (log)
790         log->Printf("ProcessKDP::AsyncThread (pid = %" PRIu64
791                     ") listener.WaitForEvent (NULL, event_sp)...",
792                     pid);
793       if (listener_sp->GetEvent(event_sp, llvm::None)) {
794         uint32_t event_type = event_sp->GetType();
795         if (log)
796           log->Printf("ProcessKDP::AsyncThread (pid = %" PRIu64
797                       ") Got an event of type: %d...",
798                       pid, event_type);
799 
800         // When we are running, poll for 1 second to try and get an exception
801         // to indicate the process has stopped. If we don't get one, check to
802         // make sure no one asked us to exit
803         bool is_running = false;
804         DataExtractor exc_reply_packet;
805         do {
806           switch (event_type) {
807           case eBroadcastBitAsyncContinue: {
808             is_running = true;
809             if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds(
810                     exc_reply_packet, 1 * USEC_PER_SEC)) {
811               ThreadSP thread_sp(process->GetKernelThread());
812               if (thread_sp) {
813                 lldb::RegisterContextSP reg_ctx_sp(
814                     thread_sp->GetRegisterContext());
815                 if (reg_ctx_sp)
816                   reg_ctx_sp->InvalidateAllRegisters();
817                 static_cast<ThreadKDP *>(thread_sp.get())
818                     ->SetStopInfoFrom_KDP_EXCEPTION(exc_reply_packet);
819               }
820 
821               // TODO: parse the stop reply packet
822               is_running = false;
823               process->SetPrivateState(eStateStopped);
824             } else {
825               // Check to see if we are supposed to exit. There is no way to
826               // interrupt a running kernel, so all we can do is wait for an
827               // exception or detach...
828               if (listener_sp->GetEvent(event_sp,
829                                         std::chrono::microseconds(0))) {
830                 // We got an event, go through the loop again
831                 event_type = event_sp->GetType();
832               }
833             }
834           } break;
835 
836           case eBroadcastBitAsyncThreadShouldExit:
837             if (log)
838               log->Printf("ProcessKDP::AsyncThread (pid = %" PRIu64
839                           ") got eBroadcastBitAsyncThreadShouldExit...",
840                           pid);
841             done = true;
842             is_running = false;
843             break;
844 
845           default:
846             if (log)
847               log->Printf("ProcessKDP::AsyncThread (pid = %" PRIu64
848                           ") got unknown event 0x%8.8x",
849                           pid, event_type);
850             done = true;
851             is_running = false;
852             break;
853           }
854         } while (is_running);
855       } else {
856         if (log)
857           log->Printf("ProcessKDP::AsyncThread (pid = %" PRIu64
858                       ") listener.WaitForEvent (NULL, event_sp) => false",
859                       pid);
860         done = true;
861       }
862     }
863   }
864 
865   if (log)
866     log->Printf("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64
867                 ") thread exiting...",
868                 arg, pid);
869 
870   process->m_async_thread.Reset();
871   return NULL;
872 }
873 
874 class CommandObjectProcessKDPPacketSend : public CommandObjectParsed {
875 private:
876   OptionGroupOptions m_option_group;
877   OptionGroupUInt64 m_command_byte;
878   OptionGroupString m_packet_data;
879 
880   virtual Options *GetOptions() { return &m_option_group; }
881 
882 public:
883   CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter)
884       : CommandObjectParsed(interpreter, "process plugin packet send",
885                             "Send a custom packet through the KDP protocol by "
886                             "specifying the command byte and the packet "
887                             "payload data. A packet will be sent with a "
888                             "correct header and payload, and the raw result "
889                             "bytes will be displayed as a string value. ",
890                             NULL),
891         m_option_group(),
892         m_command_byte(LLDB_OPT_SET_1, true, "command", 'c', 0, eArgTypeNone,
893                        "Specify the command byte to use when sending the KDP "
894                        "request packet.",
895                        0),
896         m_packet_data(LLDB_OPT_SET_1, false, "payload", 'p', 0, eArgTypeNone,
897                       "Specify packet payload bytes as a hex ASCII string with "
898                       "no spaces or hex prefixes.",
899                       NULL) {
900     m_option_group.Append(&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
901     m_option_group.Append(&m_packet_data, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
902     m_option_group.Finalize();
903   }
904 
905   ~CommandObjectProcessKDPPacketSend() {}
906 
907   bool DoExecute(Args &command, CommandReturnObject &result) {
908     const size_t argc = command.GetArgumentCount();
909     if (argc == 0) {
910       if (!m_command_byte.GetOptionValue().OptionWasSet()) {
911         result.AppendError(
912             "the --command option must be set to a valid command byte");
913         result.SetStatus(eReturnStatusFailed);
914       } else {
915         const uint64_t command_byte =
916             m_command_byte.GetOptionValue().GetUInt64Value(0);
917         if (command_byte > 0 && command_byte <= UINT8_MAX) {
918           ProcessKDP *process =
919               (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr();
920           if (process) {
921             const StateType state = process->GetState();
922 
923             if (StateIsStoppedState(state, true)) {
924               std::vector<uint8_t> payload_bytes;
925               const char *ascii_hex_bytes_cstr =
926                   m_packet_data.GetOptionValue().GetCurrentValue();
927               if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0]) {
928                 StringExtractor extractor(ascii_hex_bytes_cstr);
929                 const size_t ascii_hex_bytes_cstr_len =
930                     extractor.GetStringRef().size();
931                 if (ascii_hex_bytes_cstr_len & 1) {
932                   result.AppendErrorWithFormat("payload data must contain an "
933                                                "even number of ASCII hex "
934                                                "characters: '%s'",
935                                                ascii_hex_bytes_cstr);
936                   result.SetStatus(eReturnStatusFailed);
937                   return false;
938                 }
939                 payload_bytes.resize(ascii_hex_bytes_cstr_len / 2);
940                 if (extractor.GetHexBytes(payload_bytes, '\xdd') !=
941                     payload_bytes.size()) {
942                   result.AppendErrorWithFormat("payload data must only contain "
943                                                "ASCII hex characters (no "
944                                                "spaces or hex prefixes): '%s'",
945                                                ascii_hex_bytes_cstr);
946                   result.SetStatus(eReturnStatusFailed);
947                   return false;
948                 }
949               }
950               Status error;
951               DataExtractor reply;
952               process->GetCommunication().SendRawRequest(
953                   command_byte,
954                   payload_bytes.empty() ? NULL : payload_bytes.data(),
955                   payload_bytes.size(), reply, error);
956 
957               if (error.Success()) {
958                 // Copy the binary bytes into a hex ASCII string for the result
959                 StreamString packet;
960                 packet.PutBytesAsRawHex8(
961                     reply.GetDataStart(), reply.GetByteSize(),
962                     endian::InlHostByteOrder(), endian::InlHostByteOrder());
963                 result.AppendMessage(packet.GetString());
964                 result.SetStatus(eReturnStatusSuccessFinishResult);
965                 return true;
966               } else {
967                 const char *error_cstr = error.AsCString();
968                 if (error_cstr && error_cstr[0])
969                   result.AppendError(error_cstr);
970                 else
971                   result.AppendErrorWithFormat("unknown error 0x%8.8x",
972                                                error.GetError());
973                 result.SetStatus(eReturnStatusFailed);
974                 return false;
975               }
976             } else {
977               result.AppendErrorWithFormat("process must be stopped in order "
978                                            "to send KDP packets, state is %s",
979                                            StateAsCString(state));
980               result.SetStatus(eReturnStatusFailed);
981             }
982           } else {
983             result.AppendError("invalid process");
984             result.SetStatus(eReturnStatusFailed);
985           }
986         } else {
987           result.AppendErrorWithFormat("invalid command byte 0x%" PRIx64
988                                        ", valid values are 1 - 255",
989                                        command_byte);
990           result.SetStatus(eReturnStatusFailed);
991         }
992       }
993     } else {
994       result.AppendErrorWithFormat("'%s' takes no arguments, only options.",
995                                    m_cmd_name.c_str());
996       result.SetStatus(eReturnStatusFailed);
997     }
998     return false;
999   }
1000 };
1001 
1002 class CommandObjectProcessKDPPacket : public CommandObjectMultiword {
1003 private:
1004 public:
1005   CommandObjectProcessKDPPacket(CommandInterpreter &interpreter)
1006       : CommandObjectMultiword(interpreter, "process plugin packet",
1007                                "Commands that deal with KDP remote packets.",
1008                                NULL) {
1009     LoadSubCommand(
1010         "send",
1011         CommandObjectSP(new CommandObjectProcessKDPPacketSend(interpreter)));
1012   }
1013 
1014   ~CommandObjectProcessKDPPacket() {}
1015 };
1016 
1017 class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword {
1018 public:
1019   CommandObjectMultiwordProcessKDP(CommandInterpreter &interpreter)
1020       : CommandObjectMultiword(
1021             interpreter, "process plugin",
1022             "Commands for operating on a ProcessKDP process.",
1023             "process plugin <subcommand> [<subcommand-options>]") {
1024     LoadSubCommand("packet", CommandObjectSP(new CommandObjectProcessKDPPacket(
1025                                  interpreter)));
1026   }
1027 
1028   ~CommandObjectMultiwordProcessKDP() {}
1029 };
1030 
1031 CommandObject *ProcessKDP::GetPluginCommandObject() {
1032   if (!m_command_sp)
1033     m_command_sp.reset(new CommandObjectMultiwordProcessKDP(
1034         GetTarget().GetDebugger().GetCommandInterpreter()));
1035   return m_command_sp.get();
1036 }
1037