blob: 818d69bdabdc98750381d78a0c83ebb024a6dc43 [file] [log] [blame]
Todd Fialaaf245d12014-06-30 21:05:18 +00001//===-- 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
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000010#include "lldb/Host/common/NativeProcessProtocol.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000011
12#include "lldb/lldb-enumerations.h"
13#include "lldb/Core/ArchSpec.h"
14#include "lldb/Core/Log.h"
15#include "lldb/Core/State.h"
Todd Fiala511e5cd2014-09-11 23:29:14 +000016#include "lldb/Host/Host.h"
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000017#include "lldb/Host/common/NativeRegisterContext.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000018
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000019#include "lldb/Host/common/NativeThreadProtocol.h"
20#include "lldb/Host/common/SoftwareBreakpoint.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000021
22using namespace lldb;
23using namespace lldb_private;
24
25// -----------------------------------------------------------------------------
26// NativeProcessProtocol Members
27// -----------------------------------------------------------------------------
28
29NativeProcessProtocol::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 (),
Chaoren Lin18fe6402015-02-03 01:51:47 +000042 m_watchpoint_list (),
Todd Fialaaf245d12014-06-30 21:05:18 +000043 m_terminal_fd (-1),
44 m_stop_id (0)
45{
46}
47
48lldb_private::Error
Todd Fiala511e5cd2014-09-11 23:29:14 +000049NativeProcessProtocol::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
60lldb_private::Error
Todd Fialaaf245d12014-06-30 21:05:18 +000061NativeProcessProtocol::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info)
62{
63 // Default: not implemented.
64 return Error ("not implemented");
65}
66
67bool
68NativeProcessProtocol::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
82bool
83NativeProcessProtocol::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
117NativeThreadProtocolSP
118NativeProcessProtocol::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
126NativeThreadProtocolSP
Todd Fiala511e5cd2014-09-11 23:29:14 +0000127NativeProcessProtocol::GetThreadByIDUnlocked (lldb::tid_t tid)
Todd Fialaaf245d12014-06-30 21:05:18 +0000128{
Todd Fialaaf245d12014-06-30 21:05:18 +0000129 for (auto thread_sp : m_threads)
130 {
131 if (thread_sp->GetID() == tid)
132 return thread_sp;
133 }
134 return NativeThreadProtocolSP ();
135}
136
Todd Fiala511e5cd2014-09-11 23:29:14 +0000137NativeThreadProtocolSP
138NativeProcessProtocol::GetThreadByID (lldb::tid_t tid)
139{
140 Mutex::Locker locker (m_threads_mutex);
141 return GetThreadByIDUnlocked (tid);
142}
143
Todd Fialaaf245d12014-06-30 21:05:18 +0000144bool
145NativeProcessProtocol::IsAlive () const
146{
147 return m_state != eStateDetached
148 && m_state != eStateExited
149 && m_state != eStateInvalid
150 && m_state != eStateUnloaded;
151}
152
153bool
154NativeProcessProtocol::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
Chaoren Lin18fe6402015-02-03 01:51:47 +0000163const NativeWatchpointList::WatchpointMap&
164NativeProcessProtocol::GetWatchpointMap () const
165{
166 return m_watchpoint_list.GetWatchpointMap();
167}
168
Todd Fialaaf245d12014-06-30 21:05:18 +0000169uint32_t
170NativeProcessProtocol::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
198Error
199NativeProcessProtocol::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
Todd Fialaaf245d12014-06-30 21:05:18 +0000209 // 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 }
Chaoren Lin18fe6402015-02-03 01:51:47 +0000268 return m_watchpoint_list.Add (addr, size, watch_flags, hardware);
Todd Fialaaf245d12014-06-30 21:05:18 +0000269}
270
271Error
272NativeProcessProtocol::RemoveWatchpoint (lldb::addr_t addr)
273{
Todd Fialaaf245d12014-06-30 21:05:18 +0000274 // 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 }
Chaoren Lin18fe6402015-02-03 01:51:47 +0000296 const Error error = m_watchpoint_list.Remove(addr);
297 return overall_error.Fail() ? overall_error : error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000298}
299
300bool
301NativeProcessProtocol::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
312bool
313NativeProcessProtocol::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
325void
326NativeProcessProtocol::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
Todd Fialaa9882ce2014-08-28 15:46:54 +0000349void
350NativeProcessProtocol::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
Todd Fialaaf245d12014-06-30 21:05:18 +0000364Error
365NativeProcessProtocol::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
376Error
377NativeProcessProtocol::RemoveBreakpoint (lldb::addr_t addr)
378{
379 return m_breakpoint_list.DecRef (addr);
380}
381
382Error
383NativeProcessProtocol::EnableBreakpoint (lldb::addr_t addr)
384{
385 return m_breakpoint_list.EnableBreakpoint (addr);
386}
387
388Error
389NativeProcessProtocol::DisableBreakpoint (lldb::addr_t addr)
390{
391 return m_breakpoint_list.DisableBreakpoint (addr);
392}
393
394lldb::StateType
395NativeProcessProtocol::GetState () const
396{
397 Mutex::Locker locker (m_state_mutex);
398 return m_state;
399}
400
401void
402NativeProcessProtocol::SetState (lldb::StateType state, bool notify_delegates)
403{
404 Mutex::Locker locker (m_state_mutex);
Tamas Berghammer5830aa72015-02-06 10:42:33 +0000405
406 if (state == m_state)
407 return;
408
Todd Fialaaf245d12014-06-30 21:05:18 +0000409 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
427uint32_t NativeProcessProtocol::GetStopID () const
428{
429 Mutex::Locker locker (m_state_mutex);
430 return m_stop_id;
431}
432
433void
434NativeProcessProtocol::DoStopIDBumped (uint32_t /* newBumpId */)
435{
436 // Default implementation does nothing.
437}
Oleksiy Vyalov8bc34f42015-02-19 17:58:04 +0000438
439void
440NativeProcessProtocol::Terminate ()
441{
442 // Default implementation does nothing.
443}
Pavel Labathd5b310f2015-07-09 11:51:11 +0000444
445#ifndef __linux__
446// These need to be implemented to support lldb-gdb-server on a given platform. Stubs are
447// provided to make the rest of the code link on non-supported platforms.
448
449Error
450NativeProcessProtocol::Launch (ProcessLaunchInfo &launch_info,
451 NativeDelegate &native_delegate,
452 NativeProcessProtocolSP &process_sp)
453{
454 llvm_unreachable("Platform has no NativeProcessProtocol support");
455}
456
457Error
458NativeProcessProtocol::Attach (lldb::pid_t pid,
459 NativeDelegate &native_delegate,
460 NativeProcessProtocolSP &process_sp)
461{
462 llvm_unreachable("Platform has no NativeProcessProtocol support");
463}
464
465#endif