blob: 3da19ced45b73195228ba3a9c695ae9348bda971 [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
Todd Fialaaf245d12014-06-30 21:05:18 +000012#include "lldb/Core/ArchSpec.h"
13#include "lldb/Core/Log.h"
Todd Fialae77fce02016-09-04 00:18:56 +000014#include "lldb/Core/ModuleSpec.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000015#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"
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000018#include "lldb/Host/common/NativeThreadProtocol.h"
19#include "lldb/Host/common/SoftwareBreakpoint.h"
Todd Fialae77fce02016-09-04 00:18:56 +000020#include "lldb/Symbol/ObjectFile.h"
21#include "lldb/Target/Process.h"
22#include "lldb/Utility/LLDBAssert.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "lldb/lldb-enumerations.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000024
25using namespace lldb;
26using namespace lldb_private;
27
28// -----------------------------------------------------------------------------
29// NativeProcessProtocol Members
30// -----------------------------------------------------------------------------
31
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000032NativeProcessProtocol::NativeProcessProtocol(lldb::pid_t pid)
Kate Stoneb9c1b512016-09-06 20:57:50 +000033 : 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) {}
Todd Fialaaf245d12014-06-30 21:05:18 +000038
Kate Stoneb9c1b512016-09-06 20:57:50 +000039lldb_private::Error NativeProcessProtocol::Interrupt() {
40 Error error;
41#if !defined(SIGSTOP)
42 error.SetErrorString("local host does not support signaling");
43 return error;
Todd Fiala511e5cd2014-09-11 23:29:14 +000044#else
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 return Signal(SIGSTOP);
Todd Fiala511e5cd2014-09-11 23:29:14 +000046#endif
47}
48
Pavel Labath4a705e72017-02-24 09:29:14 +000049Error 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
Todd Fiala511e5cd2014-09-11 23:29:14 +000055lldb_private::Error
Kate Stoneb9c1b512016-09-06 20:57:50 +000056NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr,
57 MemoryRegionInfo &range_info) {
58 // Default: not implemented.
59 return Error("not implemented");
Todd Fialaaf245d12014-06-30 21:05:18 +000060}
61
Kate Stoneb9c1b512016-09-06 20:57:50 +000062bool 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 }
Todd Fialaaf245d12014-06-30 21:05:18 +000070
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 *status = 0;
72 return false;
Todd Fialaaf245d12014-06-30 21:05:18 +000073}
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075bool 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,
Todd Fialaaf245d12014-06-30 21:05:18 +000082 exit_description ? exit_description : "nullptr",
83 bNotifyStateChange ? "true" : "false");
84
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 // 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 }
Todd Fialaaf245d12014-06-30 21:05:18 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 m_state = lldb::eStateExited;
Todd Fialaaf245d12014-06-30 21:05:18 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 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();
Todd Fialaaf245d12014-06-30 21:05:18 +0000102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 if (bNotifyStateChange)
104 SynchronouslyNotifyProcessStateChanged(lldb::eStateExited);
Todd Fialaaf245d12014-06-30 21:05:18 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 return true;
107}
108
109NativeThreadProtocolSP 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();
Todd Fialaaf245d12014-06-30 21:05:18 +0000114}
115
116NativeThreadProtocolSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117NativeProcessProtocol::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();
Todd Fialaaf245d12014-06-30 21:05:18 +0000123}
124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125NativeThreadProtocolSP NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) {
126 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
127 return GetThreadByIDUnlocked(tid);
Todd Fialaaf245d12014-06-30 21:05:18 +0000128}
129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130bool NativeProcessProtocol::IsAlive() const {
131 return m_state != eStateDetached && m_state != eStateExited &&
132 m_state != eStateInvalid && m_state != eStateUnloaded;
Todd Fiala511e5cd2014-09-11 23:29:14 +0000133}
134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135bool 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;
Todd Fialaaf245d12014-06-30 21:05:18 +0000141}
142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143const NativeWatchpointList::WatchpointMap &
144NativeProcessProtocol::GetWatchpointMap() const {
145 return m_watchpoint_list.GetWatchpointMap();
Todd Fialaaf245d12014-06-30 21:05:18 +0000146}
147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148uint32_t NativeProcessProtocol::GetMaxWatchpoints() const {
149 // This default implementation will return the number of
150 // *hardware* breakpoints available. MacOSX and other OS
151 // implementations that support software breakpoints will want to
152 // override this correctly for their implementation.
153 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
154
155 // get any thread
156 NativeThreadProtocolSP thread_sp(
157 const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0));
158 if (!thread_sp) {
159 if (log)
160 log->Warning("NativeProcessProtocol::%s (): failed to find a thread to "
161 "grab a NativeRegisterContext!",
162 __FUNCTION__);
163 return 0;
164 }
165
166 NativeRegisterContextSP reg_ctx_sp(thread_sp->GetRegisterContext());
167 if (!reg_ctx_sp) {
168 if (log)
169 log->Warning("NativeProcessProtocol::%s (): failed to get a "
170 "RegisterContextNativeProcess from the first thread!",
171 __FUNCTION__);
172 return 0;
173 }
174
175 return reg_ctx_sp->NumSupportedHardwareWatchpoints();
Chaoren Lin18fe6402015-02-03 01:51:47 +0000176}
177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178Error NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size,
179 uint32_t watch_flags,
180 bool hardware) {
181 // This default implementation assumes setting the watchpoint for
182 // the process will require setting the watchpoint for each of the
183 // threads. Furthermore, it will track watchpoints set for the
184 // process and will add them to each thread that is attached to
185 // via the (FIXME implement) OnThreadAttached () method.
Todd Fialaaf245d12014-06-30 21:05:18 +0000186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
188
189 // Update the thread list
190 UpdateThreads();
191
192 // Keep track of the threads we successfully set the watchpoint
193 // for. If one of the thread watchpoint setting operations fails,
194 // back off and remove the watchpoint for all the threads that
195 // were successfully set so we get back to a consistent state.
196 std::vector<NativeThreadProtocolSP> watchpoint_established_threads;
197
198 // Tell each thread to set a watchpoint. In the event that
199 // hardware watchpoints are requested but the SetWatchpoint fails,
200 // try to set a software watchpoint as a fallback. It's
201 // conceivable that if there are more threads than hardware
202 // watchpoints available, some of the threads will fail to set
203 // hardware watchpoints while software ones may be available.
204 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
205 for (auto thread_sp : m_threads) {
206 assert(thread_sp && "thread list should not have a NULL thread!");
Todd Fialaaf245d12014-06-30 21:05:18 +0000207 if (!thread_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208 continue;
209
210 Error thread_error =
211 thread_sp->SetWatchpoint(addr, size, watch_flags, hardware);
212 if (thread_error.Fail() && hardware) {
213 // Try software watchpoints since we failed on hardware watchpoint setting
214 // and we may have just run out of hardware watchpoints.
215 thread_error = thread_sp->SetWatchpoint(addr, size, watch_flags, false);
216 if (thread_error.Success()) {
Todd Fialaaf245d12014-06-30 21:05:18 +0000217 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 log->Warning(
219 "hardware watchpoint requested but software watchpoint set");
220 }
Todd Fialaaf245d12014-06-30 21:05:18 +0000221 }
222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 if (thread_error.Success()) {
224 // Remember that we set this watchpoint successfully in
225 // case we need to clear it later.
226 watchpoint_established_threads.push_back(thread_sp);
227 } else {
228 // Unset the watchpoint for each thread we successfully
229 // set so that we get back to a consistent state of "not
230 // set" for the watchpoint.
231 for (auto unwatch_thread_sp : watchpoint_established_threads) {
232 Error remove_error = unwatch_thread_sp->RemoveWatchpoint(addr);
233 if (remove_error.Fail() && log) {
234 log->Warning("NativeProcessProtocol::%s (): RemoveWatchpoint failed "
235 "for pid=%" PRIu64 ", tid=%" PRIu64 ": %s",
236 __FUNCTION__, GetID(), unwatch_thread_sp->GetID(),
237 remove_error.AsCString());
238 }
239 }
Todd Fialaaf245d12014-06-30 21:05:18 +0000240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 return thread_error;
242 }
243 }
244 return m_watchpoint_list.Add(addr, size, watch_flags, hardware);
Todd Fialaaf245d12014-06-30 21:05:18 +0000245}
246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247Error NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) {
248 // Update the thread list
249 UpdateThreads();
Todd Fialaaf245d12014-06-30 21:05:18 +0000250
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 Error overall_error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
254 for (auto thread_sp : m_threads) {
255 assert(thread_sp && "thread list should not have a NULL thread!");
256 if (!thread_sp)
257 continue;
Todd Fialaaf245d12014-06-30 21:05:18 +0000258
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 const Error thread_error = thread_sp->RemoveWatchpoint(addr);
260 if (thread_error.Fail()) {
261 // Keep track of the first thread error if any threads
262 // fail. We want to try to remove the watchpoint from
263 // every thread, though, even if one or more have errors.
264 if (!overall_error.Fail())
265 overall_error = thread_error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000266 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 }
268 const Error error = m_watchpoint_list.Remove(addr);
269 return overall_error.Fail() ? overall_error : error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000270}
271
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272bool NativeProcessProtocol::RegisterNativeDelegate(
273 NativeDelegate &native_delegate) {
274 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
275 if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) !=
276 m_delegates.end())
277 return false;
Todd Fialaaf245d12014-06-30 21:05:18 +0000278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 m_delegates.push_back(&native_delegate);
280 native_delegate.InitializeDelegate(this);
281 return true;
Todd Fialaaf245d12014-06-30 21:05:18 +0000282}
283
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284bool NativeProcessProtocol::UnregisterNativeDelegate(
285 NativeDelegate &native_delegate) {
286 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
287
288 const auto initial_size = m_delegates.size();
289 m_delegates.erase(
290 remove(m_delegates.begin(), m_delegates.end(), &native_delegate),
291 m_delegates.end());
292
293 // We removed the delegate if the count of delegates shrank after
294 // removing all copies of the given native_delegate from the vector.
295 return m_delegates.size() < initial_size;
296}
297
298void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged(
299 lldb::StateType state) {
300 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
301
302 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
303 for (auto native_delegate : m_delegates)
304 native_delegate->ProcessStateChanged(this, state);
305
306 if (log) {
307 if (!m_delegates.empty()) {
308 log->Printf("NativeProcessProtocol::%s: sent state notification [%s] "
309 "from process %" PRIu64,
310 __FUNCTION__, lldb_private::StateAsCString(state), GetID());
311 } else {
312 log->Printf("NativeProcessProtocol::%s: would send state notification "
313 "[%s] from process %" PRIu64 ", but no delegates",
314 __FUNCTION__, lldb_private::StateAsCString(state), GetID());
315 }
316 }
317}
318
319void NativeProcessProtocol::NotifyDidExec() {
320 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
321 if (log)
322 log->Printf("NativeProcessProtocol::%s - preparing to call delegates",
323 __FUNCTION__);
324
325 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000326 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327 for (auto native_delegate : m_delegates)
328 native_delegate->DidExec(this);
329 }
Todd Fialaaf245d12014-06-30 21:05:18 +0000330}
331
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332Error NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr,
333 uint32_t size_hint) {
334 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
335 if (log)
336 log->Printf("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__,
337 addr);
Todd Fialaaf245d12014-06-30 21:05:18 +0000338
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 return m_breakpoint_list.AddRef(
340 addr, size_hint, false,
341 [this](lldb::addr_t addr, size_t size_hint, bool /* hardware */,
342 NativeBreakpointSP &breakpoint_sp) -> Error {
343 return SoftwareBreakpoint::CreateSoftwareBreakpoint(
344 *this, addr, size_hint, breakpoint_sp);
345 });
Todd Fialaaf245d12014-06-30 21:05:18 +0000346}
347
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348Error NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr) {
349 return m_breakpoint_list.DecRef(addr);
Todd Fialaaf245d12014-06-30 21:05:18 +0000350}
351
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352Error NativeProcessProtocol::EnableBreakpoint(lldb::addr_t addr) {
353 return m_breakpoint_list.EnableBreakpoint(addr);
Todd Fialaa9882ce2014-08-28 15:46:54 +0000354}
355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356Error NativeProcessProtocol::DisableBreakpoint(lldb::addr_t addr) {
357 return m_breakpoint_list.DisableBreakpoint(addr);
Todd Fialaaf245d12014-06-30 21:05:18 +0000358}
359
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360lldb::StateType NativeProcessProtocol::GetState() const {
361 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
362 return m_state;
Todd Fialaaf245d12014-06-30 21:05:18 +0000363}
364
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365void NativeProcessProtocol::SetState(lldb::StateType state,
366 bool notify_delegates) {
367 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
368
369 if (state == m_state)
370 return;
371
372 m_state = state;
373
374 if (StateIsStoppedState(state, false)) {
375 ++m_stop_id;
376
377 // Give process a chance to do any stop id bump processing, such as
378 // clearing cached data that is invalidated each time the process runs.
379 // Note if/when we support some threads running, we'll end up needing
380 // to manage this per thread and per process.
381 DoStopIDBumped(m_stop_id);
382 }
383
384 // Optionally notify delegates of the state change.
385 if (notify_delegates)
386 SynchronouslyNotifyProcessStateChanged(state);
Todd Fialaaf245d12014-06-30 21:05:18 +0000387}
388
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389uint32_t NativeProcessProtocol::GetStopID() const {
390 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
391 return m_stop_id;
Todd Fialaaf245d12014-06-30 21:05:18 +0000392}
393
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) {
395 // Default implementation does nothing.
Todd Fialaaf245d12014-06-30 21:05:18 +0000396}
397
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398Error NativeProcessProtocol::ResolveProcessArchitecture(lldb::pid_t pid,
399 ArchSpec &arch) {
400 // Grab process info for the running process.
401 ProcessInstanceInfo process_info;
402 if (!Host::GetProcessInfo(pid, process_info))
403 return Error("failed to get process info");
Tamas Berghammer5830aa72015-02-06 10:42:33 +0000404
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 // Resolve the executable module.
406 ModuleSpecList module_specs;
407 if (!ObjectFile::GetModuleSpecifications(process_info.GetExecutableFile(), 0,
408 0, module_specs))
409 return Error("failed to get module specifications");
410 lldbassert(module_specs.GetSize() == 1);
Tamas Berghammer5830aa72015-02-06 10:42:33 +0000411
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 arch = module_specs.GetModuleSpecRefAtIndex(0).GetArchitecture();
413 if (arch.IsValid())
Mehdi Aminic1edf562016-11-11 04:29:25 +0000414 return Error();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415 else
416 return Error("failed to retrieve a valid architecture from the exe module");
Todd Fialae77fce02016-09-04 00:18:56 +0000417}
418
Pavel Labathd5b310f2015-07-09 11:51:11 +0000419#ifndef __linux__
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420// These need to be implemented to support lldb-gdb-server on a given platform.
421// Stubs are
Pavel Labathd5b310f2015-07-09 11:51:11 +0000422// provided to make the rest of the code link on non-supported platforms.
423
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424Error NativeProcessProtocol::Launch(ProcessLaunchInfo &launch_info,
425 NativeDelegate &native_delegate,
426 MainLoop &mainloop,
427 NativeProcessProtocolSP &process_sp) {
428 llvm_unreachable("Platform has no NativeProcessProtocol support");
Pavel Labathd5b310f2015-07-09 11:51:11 +0000429}
430
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431Error NativeProcessProtocol::Attach(lldb::pid_t pid,
432 NativeDelegate &native_delegate,
433 MainLoop &mainloop,
434 NativeProcessProtocolSP &process_sp) {
435 llvm_unreachable("Platform has no NativeProcessProtocol support");
Pavel Labathd5b310f2015-07-09 11:51:11 +0000436}
437
438#endif