blob: d77b8b2e97466eac17fff041406773ef6112d22d [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
49lldb_private::Error
Kate Stoneb9c1b512016-09-06 20:57:50 +000050NativeProcessProtocol::GetMemoryRegionInfo(lldb::addr_t load_addr,
51 MemoryRegionInfo &range_info) {
52 // Default: not implemented.
53 return Error("not implemented");
Todd Fialaaf245d12014-06-30 21:05:18 +000054}
55
Kate Stoneb9c1b512016-09-06 20:57:50 +000056bool NativeProcessProtocol::GetExitStatus(ExitType *exit_type, int *status,
57 std::string &exit_description) {
58 if (m_state == lldb::eStateExited) {
59 *exit_type = m_exit_type;
60 *status = m_exit_status;
61 exit_description = m_exit_description;
62 return true;
63 }
Todd Fialaaf245d12014-06-30 21:05:18 +000064
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 *status = 0;
66 return false;
Todd Fialaaf245d12014-06-30 21:05:18 +000067}
68
Kate Stoneb9c1b512016-09-06 20:57:50 +000069bool NativeProcessProtocol::SetExitStatus(ExitType exit_type, int status,
70 const char *exit_description,
71 bool bNotifyStateChange) {
72 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
73 if (log)
74 log->Printf("NativeProcessProtocol::%s(%d, %d, %s, %s) called",
75 __FUNCTION__, exit_type, status,
Todd Fialaaf245d12014-06-30 21:05:18 +000076 exit_description ? exit_description : "nullptr",
77 bNotifyStateChange ? "true" : "false");
78
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 // Exit status already set
80 if (m_state == lldb::eStateExited) {
81 if (log)
82 log->Printf("NativeProcessProtocol::%s exit status already set to %d, "
83 "ignoring new set to %d",
84 __FUNCTION__, m_exit_status, status);
85 return false;
86 }
Todd Fialaaf245d12014-06-30 21:05:18 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 m_state = lldb::eStateExited;
Todd Fialaaf245d12014-06-30 21:05:18 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 m_exit_type = exit_type;
91 m_exit_status = status;
92 if (exit_description && exit_description[0])
93 m_exit_description = exit_description;
94 else
95 m_exit_description.clear();
Todd Fialaaf245d12014-06-30 21:05:18 +000096
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 if (bNotifyStateChange)
98 SynchronouslyNotifyProcessStateChanged(lldb::eStateExited);
Todd Fialaaf245d12014-06-30 21:05:18 +000099
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 return true;
101}
102
103NativeThreadProtocolSP NativeProcessProtocol::GetThreadAtIndex(uint32_t idx) {
104 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
105 if (idx < m_threads.size())
106 return m_threads[idx];
107 return NativeThreadProtocolSP();
Todd Fialaaf245d12014-06-30 21:05:18 +0000108}
109
110NativeThreadProtocolSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111NativeProcessProtocol::GetThreadByIDUnlocked(lldb::tid_t tid) {
112 for (auto thread_sp : m_threads) {
113 if (thread_sp->GetID() == tid)
114 return thread_sp;
115 }
116 return NativeThreadProtocolSP();
Todd Fialaaf245d12014-06-30 21:05:18 +0000117}
118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119NativeThreadProtocolSP NativeProcessProtocol::GetThreadByID(lldb::tid_t tid) {
120 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
121 return GetThreadByIDUnlocked(tid);
Todd Fialaaf245d12014-06-30 21:05:18 +0000122}
123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124bool NativeProcessProtocol::IsAlive() const {
125 return m_state != eStateDetached && m_state != eStateExited &&
126 m_state != eStateInvalid && m_state != eStateUnloaded;
Todd Fiala511e5cd2014-09-11 23:29:14 +0000127}
128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129bool NativeProcessProtocol::GetByteOrder(lldb::ByteOrder &byte_order) const {
130 ArchSpec process_arch;
131 if (!GetArchitecture(process_arch))
132 return false;
133 byte_order = process_arch.GetByteOrder();
134 return true;
Todd Fialaaf245d12014-06-30 21:05:18 +0000135}
136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137const NativeWatchpointList::WatchpointMap &
138NativeProcessProtocol::GetWatchpointMap() const {
139 return m_watchpoint_list.GetWatchpointMap();
Todd Fialaaf245d12014-06-30 21:05:18 +0000140}
141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142uint32_t NativeProcessProtocol::GetMaxWatchpoints() const {
143 // This default implementation will return the number of
144 // *hardware* breakpoints available. MacOSX and other OS
145 // implementations that support software breakpoints will want to
146 // override this correctly for their implementation.
147 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
148
149 // get any thread
150 NativeThreadProtocolSP thread_sp(
151 const_cast<NativeProcessProtocol *>(this)->GetThreadAtIndex(0));
152 if (!thread_sp) {
153 if (log)
154 log->Warning("NativeProcessProtocol::%s (): failed to find a thread to "
155 "grab a NativeRegisterContext!",
156 __FUNCTION__);
157 return 0;
158 }
159
160 NativeRegisterContextSP reg_ctx_sp(thread_sp->GetRegisterContext());
161 if (!reg_ctx_sp) {
162 if (log)
163 log->Warning("NativeProcessProtocol::%s (): failed to get a "
164 "RegisterContextNativeProcess from the first thread!",
165 __FUNCTION__);
166 return 0;
167 }
168
169 return reg_ctx_sp->NumSupportedHardwareWatchpoints();
Chaoren Lin18fe6402015-02-03 01:51:47 +0000170}
171
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172Error NativeProcessProtocol::SetWatchpoint(lldb::addr_t addr, size_t size,
173 uint32_t watch_flags,
174 bool hardware) {
175 // This default implementation assumes setting the watchpoint for
176 // the process will require setting the watchpoint for each of the
177 // threads. Furthermore, it will track watchpoints set for the
178 // process and will add them to each thread that is attached to
179 // via the (FIXME implement) OnThreadAttached () method.
Todd Fialaaf245d12014-06-30 21:05:18 +0000180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
182
183 // Update the thread list
184 UpdateThreads();
185
186 // Keep track of the threads we successfully set the watchpoint
187 // for. If one of the thread watchpoint setting operations fails,
188 // back off and remove the watchpoint for all the threads that
189 // were successfully set so we get back to a consistent state.
190 std::vector<NativeThreadProtocolSP> watchpoint_established_threads;
191
192 // Tell each thread to set a watchpoint. In the event that
193 // hardware watchpoints are requested but the SetWatchpoint fails,
194 // try to set a software watchpoint as a fallback. It's
195 // conceivable that if there are more threads than hardware
196 // watchpoints available, some of the threads will fail to set
197 // hardware watchpoints while software ones may be available.
198 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
199 for (auto thread_sp : m_threads) {
200 assert(thread_sp && "thread list should not have a NULL thread!");
Todd Fialaaf245d12014-06-30 21:05:18 +0000201 if (!thread_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 continue;
203
204 Error thread_error =
205 thread_sp->SetWatchpoint(addr, size, watch_flags, hardware);
206 if (thread_error.Fail() && hardware) {
207 // Try software watchpoints since we failed on hardware watchpoint setting
208 // and we may have just run out of hardware watchpoints.
209 thread_error = thread_sp->SetWatchpoint(addr, size, watch_flags, false);
210 if (thread_error.Success()) {
Todd Fialaaf245d12014-06-30 21:05:18 +0000211 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 log->Warning(
213 "hardware watchpoint requested but software watchpoint set");
214 }
Todd Fialaaf245d12014-06-30 21:05:18 +0000215 }
216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 if (thread_error.Success()) {
218 // Remember that we set this watchpoint successfully in
219 // case we need to clear it later.
220 watchpoint_established_threads.push_back(thread_sp);
221 } else {
222 // Unset the watchpoint for each thread we successfully
223 // set so that we get back to a consistent state of "not
224 // set" for the watchpoint.
225 for (auto unwatch_thread_sp : watchpoint_established_threads) {
226 Error remove_error = unwatch_thread_sp->RemoveWatchpoint(addr);
227 if (remove_error.Fail() && log) {
228 log->Warning("NativeProcessProtocol::%s (): RemoveWatchpoint failed "
229 "for pid=%" PRIu64 ", tid=%" PRIu64 ": %s",
230 __FUNCTION__, GetID(), unwatch_thread_sp->GetID(),
231 remove_error.AsCString());
232 }
233 }
Todd Fialaaf245d12014-06-30 21:05:18 +0000234
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235 return thread_error;
236 }
237 }
238 return m_watchpoint_list.Add(addr, size, watch_flags, hardware);
Todd Fialaaf245d12014-06-30 21:05:18 +0000239}
240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241Error NativeProcessProtocol::RemoveWatchpoint(lldb::addr_t addr) {
242 // Update the thread list
243 UpdateThreads();
Todd Fialaaf245d12014-06-30 21:05:18 +0000244
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245 Error overall_error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 std::lock_guard<std::recursive_mutex> guard(m_threads_mutex);
248 for (auto thread_sp : m_threads) {
249 assert(thread_sp && "thread list should not have a NULL thread!");
250 if (!thread_sp)
251 continue;
Todd Fialaaf245d12014-06-30 21:05:18 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 const Error thread_error = thread_sp->RemoveWatchpoint(addr);
254 if (thread_error.Fail()) {
255 // Keep track of the first thread error if any threads
256 // fail. We want to try to remove the watchpoint from
257 // every thread, though, even if one or more have errors.
258 if (!overall_error.Fail())
259 overall_error = thread_error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000260 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 }
262 const Error error = m_watchpoint_list.Remove(addr);
263 return overall_error.Fail() ? overall_error : error;
Todd Fialaaf245d12014-06-30 21:05:18 +0000264}
265
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266bool NativeProcessProtocol::RegisterNativeDelegate(
267 NativeDelegate &native_delegate) {
268 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
269 if (std::find(m_delegates.begin(), m_delegates.end(), &native_delegate) !=
270 m_delegates.end())
271 return false;
Todd Fialaaf245d12014-06-30 21:05:18 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 m_delegates.push_back(&native_delegate);
274 native_delegate.InitializeDelegate(this);
275 return true;
Todd Fialaaf245d12014-06-30 21:05:18 +0000276}
277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278bool NativeProcessProtocol::UnregisterNativeDelegate(
279 NativeDelegate &native_delegate) {
280 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
281
282 const auto initial_size = m_delegates.size();
283 m_delegates.erase(
284 remove(m_delegates.begin(), m_delegates.end(), &native_delegate),
285 m_delegates.end());
286
287 // We removed the delegate if the count of delegates shrank after
288 // removing all copies of the given native_delegate from the vector.
289 return m_delegates.size() < initial_size;
290}
291
292void NativeProcessProtocol::SynchronouslyNotifyProcessStateChanged(
293 lldb::StateType state) {
294 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
295
296 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
297 for (auto native_delegate : m_delegates)
298 native_delegate->ProcessStateChanged(this, state);
299
300 if (log) {
301 if (!m_delegates.empty()) {
302 log->Printf("NativeProcessProtocol::%s: sent state notification [%s] "
303 "from process %" PRIu64,
304 __FUNCTION__, lldb_private::StateAsCString(state), GetID());
305 } else {
306 log->Printf("NativeProcessProtocol::%s: would send state notification "
307 "[%s] from process %" PRIu64 ", but no delegates",
308 __FUNCTION__, lldb_private::StateAsCString(state), GetID());
309 }
310 }
311}
312
313void NativeProcessProtocol::NotifyDidExec() {
314 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
315 if (log)
316 log->Printf("NativeProcessProtocol::%s - preparing to call delegates",
317 __FUNCTION__);
318
319 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000320 std::lock_guard<std::recursive_mutex> guard(m_delegates_mutex);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321 for (auto native_delegate : m_delegates)
322 native_delegate->DidExec(this);
323 }
Todd Fialaaf245d12014-06-30 21:05:18 +0000324}
325
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326Error NativeProcessProtocol::SetSoftwareBreakpoint(lldb::addr_t addr,
327 uint32_t size_hint) {
328 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
329 if (log)
330 log->Printf("NativeProcessProtocol::%s addr = 0x%" PRIx64, __FUNCTION__,
331 addr);
Todd Fialaaf245d12014-06-30 21:05:18 +0000332
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 return m_breakpoint_list.AddRef(
334 addr, size_hint, false,
335 [this](lldb::addr_t addr, size_t size_hint, bool /* hardware */,
336 NativeBreakpointSP &breakpoint_sp) -> Error {
337 return SoftwareBreakpoint::CreateSoftwareBreakpoint(
338 *this, addr, size_hint, breakpoint_sp);
339 });
Todd Fialaaf245d12014-06-30 21:05:18 +0000340}
341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342Error NativeProcessProtocol::RemoveBreakpoint(lldb::addr_t addr) {
343 return m_breakpoint_list.DecRef(addr);
Todd Fialaaf245d12014-06-30 21:05:18 +0000344}
345
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346Error NativeProcessProtocol::EnableBreakpoint(lldb::addr_t addr) {
347 return m_breakpoint_list.EnableBreakpoint(addr);
Todd Fialaa9882ce2014-08-28 15:46:54 +0000348}
349
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350Error NativeProcessProtocol::DisableBreakpoint(lldb::addr_t addr) {
351 return m_breakpoint_list.DisableBreakpoint(addr);
Todd Fialaaf245d12014-06-30 21:05:18 +0000352}
353
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354lldb::StateType NativeProcessProtocol::GetState() const {
355 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
356 return m_state;
Todd Fialaaf245d12014-06-30 21:05:18 +0000357}
358
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359void NativeProcessProtocol::SetState(lldb::StateType state,
360 bool notify_delegates) {
361 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
362
363 if (state == m_state)
364 return;
365
366 m_state = state;
367
368 if (StateIsStoppedState(state, false)) {
369 ++m_stop_id;
370
371 // Give process a chance to do any stop id bump processing, such as
372 // clearing cached data that is invalidated each time the process runs.
373 // Note if/when we support some threads running, we'll end up needing
374 // to manage this per thread and per process.
375 DoStopIDBumped(m_stop_id);
376 }
377
378 // Optionally notify delegates of the state change.
379 if (notify_delegates)
380 SynchronouslyNotifyProcessStateChanged(state);
Todd Fialaaf245d12014-06-30 21:05:18 +0000381}
382
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383uint32_t NativeProcessProtocol::GetStopID() const {
384 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
385 return m_stop_id;
Todd Fialaaf245d12014-06-30 21:05:18 +0000386}
387
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388void NativeProcessProtocol::DoStopIDBumped(uint32_t /* newBumpId */) {
389 // Default implementation does nothing.
Todd Fialaaf245d12014-06-30 21:05:18 +0000390}
391
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392Error NativeProcessProtocol::ResolveProcessArchitecture(lldb::pid_t pid,
393 ArchSpec &arch) {
394 // Grab process info for the running process.
395 ProcessInstanceInfo process_info;
396 if (!Host::GetProcessInfo(pid, process_info))
397 return Error("failed to get process info");
Tamas Berghammer5830aa72015-02-06 10:42:33 +0000398
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399 // Resolve the executable module.
400 ModuleSpecList module_specs;
401 if (!ObjectFile::GetModuleSpecifications(process_info.GetExecutableFile(), 0,
402 0, module_specs))
403 return Error("failed to get module specifications");
404 lldbassert(module_specs.GetSize() == 1);
Tamas Berghammer5830aa72015-02-06 10:42:33 +0000405
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406 arch = module_specs.GetModuleSpecRefAtIndex(0).GetArchitecture();
407 if (arch.IsValid())
408 return Error();
409 else
410 return Error("failed to retrieve a valid architecture from the exe module");
Todd Fialae77fce02016-09-04 00:18:56 +0000411}
412
Pavel Labathd5b310f2015-07-09 11:51:11 +0000413#ifndef __linux__
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414// These need to be implemented to support lldb-gdb-server on a given platform.
415// Stubs are
Pavel Labathd5b310f2015-07-09 11:51:11 +0000416// provided to make the rest of the code link on non-supported platforms.
417
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418Error NativeProcessProtocol::Launch(ProcessLaunchInfo &launch_info,
419 NativeDelegate &native_delegate,
420 MainLoop &mainloop,
421 NativeProcessProtocolSP &process_sp) {
422 llvm_unreachable("Platform has no NativeProcessProtocol support");
Pavel Labathd5b310f2015-07-09 11:51:11 +0000423}
424
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425Error NativeProcessProtocol::Attach(lldb::pid_t pid,
426 NativeDelegate &native_delegate,
427 MainLoop &mainloop,
428 NativeProcessProtocolSP &process_sp) {
429 llvm_unreachable("Platform has no NativeProcessProtocol support");
Pavel Labathd5b310f2015-07-09 11:51:11 +0000430}
431
432#endif