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