1 //===-- NativeProcessProtocol.cpp -------------------------------*- C++ -*-===//
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 #include "lldb/Host/common/NativeProcessProtocol.h"
11 
12 #include "lldb/Core/ArchSpec.h"
13 #include "lldb/Core/ModuleSpec.h"
14 #include "lldb/Core/State.h"
15 #include "lldb/Host/Host.h"
16 #include "lldb/Host/common/NativeRegisterContext.h"
17 #include "lldb/Host/common/NativeThreadProtocol.h"
18 #include "lldb/Host/common/SoftwareBreakpoint.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Utility/LLDBAssert.h"
22 #include "lldb/Utility/Log.h"
23 #include "lldb/lldb-enumerations.h"
24 
25 using namespace lldb;
26 using namespace lldb_private;
27 
28 // -----------------------------------------------------------------------------
29 // NativeProcessProtocol Members
30 // -----------------------------------------------------------------------------
31 
32 NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid)
33     : m_pid(pid), m_threads(), m_current_thread_id(LLDB_INVALID_THREAD_ID),
34       m_threads_mutex(), m_state(lldb::eStateInvalid), m_state_mutex(),
35       m_exit_type(eExitTypeInvalid), m_exit_status(0), m_exit_description(),
36       m_delegates_mutex(), m_delegates(), m_breakpoint_list(),
37       m_watchpoint_list(), m_terminal_fd(-1), m_stop_id(0) {}
38 
39 lldb_private::Error NativeProcessProtocol::Interrupt() {
40   Error error;
41 #if !defined(SIGSTOP)
42   error.SetErrorString("local host does not support signaling");
43   return error;
44 #else
45   return Signal(SIGSTOP);
46 #endif
47 }
48 
49 Error NativeProcessProtocol::IgnoreSignals(llvm::ArrayRef<int> signals) {
50   m_signals_to_ignore.clear();
51   m_signals_to_ignore.insert(signals.begin(), signals.end());
52   return Error();
53 }
54 
55 lldb_private::Error
56 NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr,
57                                            MemoryRegionInfo &range_info) {
58   // Default: not implemented.
59   return Error("not implemented");
60 }
61 
62 bool NativeProcessProtocol::GetExitStatus(ExitType *exit_type, int *status,
63                                           std::string &exit_description) {
64   if (m_state == lldb::eStateExited) {
65     *exit_type = m_exit_type;
66     *status = m_exit_status;
67     exit_description = m_exit_description;
68     return true;
69   }
70 
71   *status = 0;
72   return false;
73 }
74 
75 bool NativeProcessProtocol::SetExitStatus(ExitType exit_type, int status,
76                                           const char *exit_description,
77                                           bool bNotifyStateChange) {
78   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
79   if (log)
80     log->Printf("NativeProcessProtocol::%s(%d, %d, %s, %s) called",
81                 __FUNCTION__, exit_type, status,
82                 exit_description ? exit_description : "nullptr",
83                 bNotifyStateChange ? "true" : "false");
84 
85   // Exit status already set
86   if (m_state == lldb::eStateExited) {
87     if (log)
88       log->Printf("NativeProcessProtocol::%s exit status already set to %d, "
89                   "ignoring new set to %d",
90                   __FUNCTION__, m_exit_status, status);
91     return false;
92   }
93 
94   m_state = lldb::eStateExited;
95 
96   m_exit_type = exit_type;
97   m_exit_status = status;
98   if (exit_description && exit_description[0])
99     m_exit_description = exit_description;
100   else
101     m_exit_description.clear();
102 
103   if (bNotifyStateChange)
104     SynchronouslyNotifyProcessStateChanged(lldb::eStateExited);
105 
106   return true;
107 }
108 
109 NativeThreadProtocolSP NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) {
110   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
111   if (idx < m_threads.size())
112     return m_threads[idx];
113   return NativeThreadProtocolSP();
114 }
115 
116 NativeThreadProtocolSP
117 NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) {
118   for (auto thread_sp : m_threads) {
119     if (thread_sp->GetID() == tid)
120       return thread_sp;
121   }
122   return NativeThreadProtocolSP();
123 }
124 
125 NativeThreadProtocolSP NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) {
126   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
127   return GetThreadByIDUnlocked(tid);
128 }
129 
130 bool NativeProcessProtocol::IsAlive() const {
131   return m_state != eStateDetached && m_state != eStateExited &&
132          m_state != eStateInvalid && m_state != eStateUnloaded;
133 }
134 
135 bool NativeProcessProtocol::GetByteOrder(lldb::ByteOrder &byte_order) const {
136   ArchSpec process_arch;
137   if (!GetArchitecture(process_arch))
138     return false;
139   byte_order = process_arch.GetByteOrder();
140   return true;
141 }
142 
143 const NativeWatchpointList::WatchpointMap &
144 NativeProcessProtocol::GetWatchpointMap() const {
145   return m_watchpoint_list.GetWatchpointMap();
146 }
147 
148 llvm::Optional<std::pair<uint32_t, uint32_t>>
149 NativeProcessProtocol::GetHardwareDebugSupportInfo() const {
150   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
151 
152   // get any thread
153   NativeThreadProtocolSP thread_sp(
154       const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0));
155   if (!thread_sp) {
156     if (log)
157       log->Warning("NativeProcessProtocol::%s (): failed to find a thread to "
158                    "grab a NativeRegisterContext!",
159                    __FUNCTION__);
160     return llvm::None;
161   }
162 
163   NativeRegisterContextSP reg_ctx_sp(thread_sp->GetRegisterContext());
164   if (!reg_ctx_sp) {
165     if (log)
166       log->Warning("NativeProcessProtocol::%s (): failed to get a "
167                    "RegisterContextNativeProcess from the first thread!",
168                    __FUNCTION__);
169     return llvm::None;
170   }
171 
172   return std::make_pair(reg_ctx_sp->NumSupportedHardwareBreakpoints(),
173                         reg_ctx_sp->NumSupportedHardwareWatchpoints());
174 }
175 
176 Error NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size,
177                                            uint32_t watch_flags,
178                                            bool hardware) {
179   // This default implementation assumes setting the watchpoint for
180   // the process will require setting the watchpoint for each of the
181   // threads.  Furthermore, it will track watchpoints set for the
182   // process and will add them to each thread that is attached to
183   // via the (FIXME implement) OnThreadAttached () method.
184 
185   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
186 
187   // Update the thread list
188   UpdateThreads();
189 
190   // Keep track of the threads we successfully set the watchpoint
191   // for.  If one of the thread watchpoint setting operations fails,
192   // back off and remove the watchpoint for all the threads that
193   // were successfully set so we get back to a consistent state.
194   std::vector<NativeThreadProtocolSP> watchpoint_established_threads;
195 
196   // Tell each thread to set a watchpoint.  In the event that
197   // hardware watchpoints are requested but the SetWatchpoint fails,
198   // try to set a software watchpoint as a fallback.  It's
199   // conceivable that if there are more threads than hardware
200   // watchpoints available, some of the threads will fail to set
201   // hardware watchpoints while software ones may be available.
202   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
203   for (auto thread_sp : m_threads) {
204     assert(thread_sp && "thread list should not have a NULL thread!");
205     if (!thread_sp)
206       continue;
207 
208     Error thread_error =
209         thread_sp->SetWatchpoint(addr, size, watch_flags, hardware);
210     if (thread_error.Fail() && hardware) {
211       // Try software watchpoints since we failed on hardware watchpoint setting
212       // and we may have just run out of hardware watchpoints.
213       thread_error = thread_sp->SetWatchpoint(addr, size, watch_flags, false);
214       if (thread_error.Success()) {
215         if (log)
216           log->Warning(
217               "hardware watchpoint requested but software watchpoint set");
218       }
219     }
220 
221     if (thread_error.Success()) {
222       // Remember that we set this watchpoint successfully in
223       // case we need to clear it later.
224       watchpoint_established_threads.push_back(thread_sp);
225     } else {
226       // Unset the watchpoint for each thread we successfully
227       // set so that we get back to a consistent state of "not
228       // set" for the watchpoint.
229       for (auto unwatch_thread_sp : watchpoint_established_threads) {
230         Error remove_error = unwatch_thread_sp->RemoveWatchpoint(addr);
231         if (remove_error.Fail() && log) {
232           log->Warning("NativeProcessProtocol::%s (): RemoveWatchpoint failed "
233                        "for pid=%" PRIu64 ", tid=%" PRIu64 ": %s",
234                        __FUNCTION__, GetID(), unwatch_thread_sp->GetID(),
235                        remove_error.AsCString());
236         }
237       }
238 
239       return thread_error;
240     }
241   }
242   return m_watchpoint_list.Add(addr, size, watch_flags, hardware);
243 }
244 
245 Error NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) {
246   // Update the thread list
247   UpdateThreads();
248 
249   Error overall_error;
250 
251   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
252   for (auto thread_sp : m_threads) {
253     assert(thread_sp && "thread list should not have a NULL thread!");
254     if (!thread_sp)
255       continue;
256 
257     const Error thread_error = thread_sp->RemoveWatchpoint(addr);
258     if (thread_error.Fail()) {
259       // Keep track of the first thread error if any threads
260       // fail. We want to try to remove the watchpoint from
261       // every thread, though, even if one or more have errors.
262       if (!overall_error.Fail())
263         overall_error = thread_error;
264     }
265   }
266   const Error error = m_watchpoint_list.Remove(addr);
267   return overall_error.Fail() ? overall_error : error;
268 }
269 
270 const HardwareBreakpointMap &
271 NativeProcessProtocol::GetHardwareBreakpointMap() const {
272   return m_hw_breakpoints_map;
273 }
274 
275 Error NativeProcessProtocol::SetHardwareBreakpoint(lldb::addr_t addr,
276                                                    size_t size) {
277   // This default implementation assumes setting a hardware breakpoint for
278   // this process will require setting same hardware breakpoint for each
279   // of its existing threads. New thread will do the same once created.
280   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
281 
282   // Update the thread list
283   UpdateThreads();
284 
285   // Exit here if target does not have required hardware breakpoint capability.
286   auto hw_debug_cap = GetHardwareDebugSupportInfo();
287 
288   if (hw_debug_cap == llvm::None || hw_debug_cap->first == 0 ||
289       hw_debug_cap->first <= m_hw_breakpoints_map.size())
290     return Error("Target does not have required no of hardware breakpoints");
291 
292   // Vector below stores all thread pointer for which we have we successfully
293   // set this hardware breakpoint. If any of the current process threads fails
294   // to set this hardware breakpoint then roll back and remove this breakpoint
295   // for all the threads that had already set it successfully.
296   std::vector<NativeThreadProtocolSP> breakpoint_established_threads;
297 
298   // Request to set a hardware breakpoint for each of current process threads.
299   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
300   for (auto thread_sp : m_threads) {
301     assert(thread_sp && "thread list should not have a NULL thread!");
302     if (!thread_sp)
303       continue;
304 
305     Error thread_error = thread_sp->SetHardwareBreakpoint(addr, size);
306     if (thread_error.Success()) {
307       // Remember that we set this breakpoint successfully in
308       // case we need to clear it later.
309       breakpoint_established_threads.push_back(thread_sp);
310     } else {
311       // Unset the breakpoint for each thread we successfully
312       // set so that we get back to a consistent state of "not
313       // set" for this hardware breakpoint.
314       for (auto rollback_thread_sp : breakpoint_established_threads) {
315         Error remove_error = rollback_thread_sp->RemoveHardwareBreakpoint(addr);
316         if (remove_error.Fail() && log) {
317           log->Warning("NativeProcessProtocol::%s (): RemoveHardwareBreakpoint"
318                        " failed for pid=%" PRIu64 ", tid=%" PRIu64 ": %s",
319                        __FUNCTION__, GetID(), rollback_thread_sp->GetID(),
320                        remove_error.AsCString());
321         }
322       }
323 
324       return thread_error;
325     }
326   }
327 
328   // Register new hardware breakpoint into hardware breakpoints map of current
329   // process.
330   m_hw_breakpoints_map[addr] = {addr, size};
331 
332   return Error();
333 }
334 
335 Error NativeProcessProtocol::RemoveHardwareBreakpoint(lldb::addr_t addr) {
336   // Update the thread list
337   UpdateThreads();
338 
339   Error error;
340 
341   std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
342   for (auto thread_sp : m_threads) {
343     assert(thread_sp && "thread list should not have a NULL thread!");
344     if (!thread_sp)
345       continue;
346 
347     error = thread_sp->RemoveHardwareBreakpoint(addr);
348   }
349 
350   // Also remove from hardware breakpoint map of current process.
351   m_hw_breakpoints_map.erase(addr);
352 
353   return error;
354 }
355 
356 bool NativeProcessProtocol::RegisterNativeDelegate(
357     NativeDelegate &native_delegate) {
358   std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
359   if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) !=
360       m_delegates.end())
361     return false;
362 
363   m_delegates.push_back(&native_delegate);
364   native_delegate.InitializeDelegate(this);
365   return true;
366 }
367 
368 bool NativeProcessProtocol::UnregisterNativeDelegate(
369     NativeDelegate &native_delegate) {
370   std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
371 
372   const auto initial_size = m_delegates.size();
373   m_delegates.erase(
374       remove(m_delegates.begin(), m_delegates.end(), &native_delegate),
375       m_delegates.end());
376 
377   // We removed the delegate if the count of delegates shrank after
378   // removing all copies of the given native_delegate from the vector.
379   return m_delegates.size() < initial_size;
380 }
381 
382 void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged(
383     lldb::StateType state) {
384   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
385 
386   std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
387   for (auto native_delegate : m_delegates)
388     native_delegate->ProcessStateChanged(this, state);
389 
390   if (log) {
391     if (!m_delegates.empty()) {
392       log->Printf("NativeProcessProtocol::%s: sent state notification [%s] "
393                   "from process %" PRIu64,
394                   __FUNCTION__, lldb_private::StateAsCString(state), GetID());
395     } else {
396       log->Printf("NativeProcessProtocol::%s: would send state notification "
397                   "[%s] from process %" PRIu64 ", but no delegates",
398                   __FUNCTION__, lldb_private::StateAsCString(state), GetID());
399     }
400   }
401 }
402 
403 void NativeProcessProtocol::NotifyDidExec() {
404   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
405   if (log)
406     log->Printf("NativeProcessProtocol::%s - preparing to call delegates",
407                 __FUNCTION__);
408 
409   {
410     std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
411     for (auto native_delegate : m_delegates)
412       native_delegate->DidExec(this);
413   }
414 }
415 
416 Error NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr,
417                                                    uint32_t size_hint) {
418   Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
419   if (log)
420     log->Printf("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__,
421                 addr);
422 
423   return m_breakpoint_list.AddRef(
424       addr, size_hint, false,
425       [this](lldb::addr_t addr, size_t size_hint, bool /* hardware */,
426              NativeBreakpointSP &breakpoint_sp) -> Error {
427         return SoftwareBreakpoint::CreateSoftwareBreakpoint(
428             *this, addr, size_hint, breakpoint_sp);
429       });
430 }
431 
432 Error NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr,
433                                               bool hardware) {
434   if (hardware)
435     return RemoveHardwareBreakpoint(addr);
436   else
437     return m_breakpoint_list.DecRef(addr);
438 }
439 
440 Error NativeProcessProtocol::EnableBreakpoint(lldb::addr_t addr) {
441   return m_breakpoint_list.EnableBreakpoint(addr);
442 }
443 
444 Error NativeProcessProtocol::DisableBreakpoint(lldb::addr_t addr) {
445   return m_breakpoint_list.DisableBreakpoint(addr);
446 }
447 
448 lldb::StateType NativeProcessProtocol::GetState() const {
449   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
450   return m_state;
451 }
452 
453 void NativeProcessProtocol::SetState(lldb::StateType state,
454                                      bool notify_delegates) {
455   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
456 
457   if (state == m_state)
458     return;
459 
460   m_state = state;
461 
462   if (StateIsStoppedState(state, false)) {
463     ++m_stop_id;
464 
465     // Give process a chance to do any stop id bump processing, such as
466     // clearing cached data that is invalidated each time the process runs.
467     // Note if/when we support some threads running, we'll end up needing
468     // to manage this per thread and per process.
469     DoStopIDBumped(m_stop_id);
470   }
471 
472   // Optionally notify delegates of the state change.
473   if (notify_delegates)
474     SynchronouslyNotifyProcessStateChanged(state);
475 }
476 
477 uint32_t NativeProcessProtocol::GetStopID() const {
478   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
479   return m_stop_id;
480 }
481 
482 void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) {
483   // Default implementation does nothing.
484 }
485 
486 Error NativeProcessProtocol::ResolveProcessArchitecture(lldb::pid_t pid,
487                                                         ArchSpec &arch) {
488   // Grab process info for the running process.
489   ProcessInstanceInfo process_info;
490   if (!Host::GetProcessInfo(pid, process_info))
491     return Error("failed to get process info");
492 
493   // Resolve the executable module.
494   ModuleSpecList module_specs;
495   if (!ObjectFile::GetModuleSpecifications(process_info.GetExecutableFile(), 0,
496                                            0, module_specs))
497     return Error("failed to get module specifications");
498   lldbassert(module_specs.GetSize() == 1);
499 
500   arch = module_specs.GetModuleSpecRefAtIndex(0).GetArchitecture();
501   if (arch.IsValid())
502     return Error();
503   else
504     return Error("failed to retrieve a valid architecture from the exe module");
505 }
506 
507 #if !defined(__linux__) && !defined(__NetBSD__)
508 // These need to be implemented to support lldb-gdb-server on a given platform.
509 // Stubs are
510 // provided to make the rest of the code link on non-supported platforms.
511 
512 Error NativeProcessProtocol::Launch(ProcessLaunchInfo &launch_info,
513                                     NativeDelegate &native_delegate,
514                                     MainLoop &mainloop,
515                                     NativeProcessProtocolSP &process_sp) {
516   llvm_unreachable("Platform has no NativeProcessProtocol support");
517 }
518 
519 Error NativeProcessProtocol::Attach(lldb::pid_t pid,
520                                     NativeDelegate &native_delegate,
521                                     MainLoop &mainloop,
522                                     NativeProcessProtocolSP &process_sp) {
523   llvm_unreachable("Platform has no NativeProcessProtocol support");
524 }
525 
526 #endif
527