blob: 82e45a5d5fc1791291e6c7c70354af175f408095 [file] [log] [blame]
Kate Stoneb9c1b512016-09-06 20:57:50 +00001//===-- ProcessFreeBSD.cpp ----------------------------------------*- C++
2//-*-===//
Johnny Chen9ed5b492012-01-05 21:48:15 +00003//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11// C Includes
12#include <errno.h>
13
14// C++ Includes
Benjamin Kramer3f69fa62015-04-03 10:55:00 +000015#include <mutex>
16
Johnny Chen9ed5b492012-01-05 21:48:15 +000017// Other libraries and framework includes
18#include "lldb/Core/PluginManager.h"
19#include "lldb/Core/State.h"
20#include "lldb/Host/Host.h"
21#include "lldb/Symbol/ObjectFile.h"
22#include "lldb/Target/DynamicLoader.h"
23#include "lldb/Target/Target.h"
24
Ed Maste7fd845c2013-12-09 15:51:17 +000025#include "FreeBSDThread.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000026#include "Plugins/Process/Utility/FreeBSDSignals.h"
27#include "Plugins/Process/Utility/InferiorCallPOSIX.h"
28#include "ProcessFreeBSD.h"
29#include "ProcessMonitor.h"
30#include "ProcessPOSIXLog.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000031
Ed Mastefe5a6422015-07-28 15:45:57 +000032// Other libraries and framework includes
33#include "lldb/Breakpoint/BreakpointLocation.h"
34#include "lldb/Breakpoint/Watchpoint.h"
35#include "lldb/Core/Module.h"
36#include "lldb/Core/ModuleSpec.h"
37#include "lldb/Core/PluginManager.h"
38#include "lldb/Core/State.h"
39#include "lldb/Host/FileSpec.h"
40#include "lldb/Host/Host.h"
41#include "lldb/Symbol/ObjectFile.h"
42#include "lldb/Target/DynamicLoader.h"
43#include "lldb/Target/Platform.h"
44#include "lldb/Target/Target.h"
45
46#include "lldb/Host/posix/Fcntl.h"
47
Johnny Chen9ed5b492012-01-05 21:48:15 +000048using namespace lldb;
49using namespace lldb_private;
50
Kate Stoneb9c1b512016-09-06 20:57:50 +000051namespace {
52UnixSignalsSP &GetFreeBSDSignals() {
53 static UnixSignalsSP s_freebsd_signals_sp(new FreeBSDSignals());
54 return s_freebsd_signals_sp;
55}
Todd Fiala4ceced32014-08-29 17:35:57 +000056}
57
Johnny Chen9ed5b492012-01-05 21:48:15 +000058//------------------------------------------------------------------------------
59// Static functions.
60
Greg Clayton29d19302012-02-27 18:40:48 +000061lldb::ProcessSP
Zachary Turner12004eb2015-09-02 16:47:47 +000062ProcessFreeBSD::CreateInstance(lldb::TargetSP target_sp,
Jim Ingham583bbb12016-03-07 21:50:25 +000063 lldb::ListenerSP listener_sp,
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 const FileSpec *crash_file_path) {
65 lldb::ProcessSP process_sp;
66 if (crash_file_path == NULL)
67 process_sp.reset(
68 new ProcessFreeBSD(target_sp, listener_sp, GetFreeBSDSignals()));
69 return process_sp;
Johnny Chen9ed5b492012-01-05 21:48:15 +000070}
71
Kate Stoneb9c1b512016-09-06 20:57:50 +000072void ProcessFreeBSD::Initialize() {
73 static std::once_flag g_once_flag;
Johnny Chen9ed5b492012-01-05 21:48:15 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 std::call_once(g_once_flag, []() {
76 PluginManager::RegisterPlugin(GetPluginNameStatic(),
77 GetPluginDescriptionStatic(), CreateInstance);
78 ProcessPOSIXLog::Initialize(GetPluginNameStatic());
79 });
Johnny Chen9ed5b492012-01-05 21:48:15 +000080}
81
Kate Stoneb9c1b512016-09-06 20:57:50 +000082lldb_private::ConstString ProcessFreeBSD::GetPluginNameStatic() {
83 static ConstString g_name("freebsd");
84 return g_name;
Johnny Chen9ed5b492012-01-05 21:48:15 +000085}
86
Kate Stoneb9c1b512016-09-06 20:57:50 +000087const char *ProcessFreeBSD::GetPluginDescriptionStatic() {
88 return "Process plugin for FreeBSD";
Johnny Chen9ed5b492012-01-05 21:48:15 +000089}
90
91//------------------------------------------------------------------------------
92// ProcessInterface protocol.
93
Kate Stoneb9c1b512016-09-06 20:57:50 +000094lldb_private::ConstString ProcessFreeBSD::GetPluginName() {
95 return GetPluginNameStatic();
Johnny Chen9ed5b492012-01-05 21:48:15 +000096}
97
Kate Stoneb9c1b512016-09-06 20:57:50 +000098uint32_t ProcessFreeBSD::GetPluginVersion() { return 1; }
Johnny Chen9ed5b492012-01-05 21:48:15 +000099
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100void ProcessFreeBSD::Terminate() {}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102Error ProcessFreeBSD::DoDetach(bool keep_stopped) {
103 Error error;
104 if (keep_stopped) {
105 error.SetErrorString("Detaching with keep_stopped true is not currently "
106 "supported on FreeBSD.");
Ed Maste7dcb77d2013-08-30 13:11:30 +0000107 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 }
109
110 error = m_monitor->Detach(GetID());
111
112 if (error.Success())
113 SetPrivateState(eStateDetached);
114
115 return error;
Ed Maste7dcb77d2013-08-30 13:11:30 +0000116}
117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118Error ProcessFreeBSD::DoResume() {
119 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
Ed Maste7fd845c2013-12-09 15:51:17 +0000120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 SetPrivateState(eStateRunning);
Ed Maste7fd845c2013-12-09 15:51:17 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex());
124 bool do_step = false;
Ed Maste7fd845c2013-12-09 15:51:17 +0000125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 for (tid_collection::const_iterator t_pos = m_run_tids.begin(),
127 t_end = m_run_tids.end();
128 t_pos != t_end; ++t_pos) {
129 m_monitor->ThreadSuspend(*t_pos, false);
130 }
131 for (tid_collection::const_iterator t_pos = m_step_tids.begin(),
132 t_end = m_step_tids.end();
133 t_pos != t_end; ++t_pos) {
134 m_monitor->ThreadSuspend(*t_pos, false);
135 do_step = true;
136 }
137 for (tid_collection::const_iterator t_pos = m_suspend_tids.begin(),
138 t_end = m_suspend_tids.end();
139 t_pos != t_end; ++t_pos) {
140 m_monitor->ThreadSuspend(*t_pos, true);
141 // XXX Cannot PT_CONTINUE properly with suspended threads.
142 do_step = true;
143 }
Ed Maste7fd845c2013-12-09 15:51:17 +0000144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 if (log)
146 log->Printf("process %" PRIu64 " resuming (%s)", GetID(),
147 do_step ? "step" : "continue");
148 if (do_step)
149 m_monitor->SingleStep(GetID(), m_resume_signo);
150 else
151 m_monitor->Resume(GetID(), m_resume_signo);
Ed Maste7fd845c2013-12-09 15:51:17 +0000152
Mehdi Amini665be502016-11-11 05:07:57 +0000153 return Error();
Ed Maste7fd845c2013-12-09 15:51:17 +0000154}
155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156bool ProcessFreeBSD::UpdateThreadList(ThreadList &old_thread_list,
157 ThreadList &new_thread_list) {
158 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
159 if (log)
160 log->Printf("ProcessFreeBSD::%s (pid = %" PRIu64 ")", __FUNCTION__,
161 GetID());
Daniel Maleae0f8f572013-08-26 23:57:52 +0000162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 std::vector<lldb::pid_t> tds;
164 if (!GetMonitor().GetCurrentThreadIDs(tds)) {
165 return false;
166 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 ThreadList old_thread_list_copy(old_thread_list);
169 for (size_t i = 0; i < tds.size(); ++i) {
170 tid_t tid = tds[i];
171 ThreadSP thread_sp(old_thread_list_copy.RemoveThreadByID(tid, false));
172 if (!thread_sp) {
173 thread_sp.reset(new FreeBSDThread(*this, tid));
174 if (log)
175 log->Printf("ProcessFreeBSD::%s new tid = %" PRIu64, __FUNCTION__, tid);
176 } else {
177 if (log)
178 log->Printf("ProcessFreeBSD::%s existing tid = %" PRIu64, __FUNCTION__,
179 tid);
Ed Maste7fd845c2013-12-09 15:51:17 +0000180 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181 new_thread_list.AddThread(thread_sp);
182 }
183 for (size_t i = 0; i < old_thread_list_copy.GetSize(false); ++i) {
184 ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex(i, false));
185 if (old_thread_sp) {
186 if (log)
187 log->Printf("ProcessFreeBSD::%s remove tid", __FUNCTION__);
Ed Maste7fd845c2013-12-09 15:51:17 +0000188 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 return true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000192}
Ed Maste7fd845c2013-12-09 15:51:17 +0000193
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194Error ProcessFreeBSD::WillResume() {
195 m_resume_signo = 0;
196 m_suspend_tids.clear();
197 m_run_tids.clear();
198 m_step_tids.clear();
199 return Process::WillResume();
Ed Maste7fd845c2013-12-09 15:51:17 +0000200}
201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202void ProcessFreeBSD::SendMessage(const ProcessMessage &message) {
203 std::lock_guard<std::recursive_mutex> guard(m_message_mutex);
Ed Maste7fd845c2013-12-09 15:51:17 +0000204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 switch (message.GetKind()) {
206 case ProcessMessage::eInvalidMessage:
207 return;
Ed Maste7fd845c2013-12-09 15:51:17 +0000208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 case ProcessMessage::eAttachMessage:
210 SetPrivateState(eStateStopped);
211 return;
Ed Maste7fd845c2013-12-09 15:51:17 +0000212
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 case ProcessMessage::eLimboMessage:
214 case ProcessMessage::eExitMessage:
215 SetExitStatus(message.GetExitStatus(), NULL);
216 break;
Ed Maste7fd845c2013-12-09 15:51:17 +0000217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 case ProcessMessage::eSignalMessage:
219 case ProcessMessage::eSignalDeliveredMessage:
220 case ProcessMessage::eBreakpointMessage:
221 case ProcessMessage::eTraceMessage:
222 case ProcessMessage::eWatchpointMessage:
223 case ProcessMessage::eCrashMessage:
224 SetPrivateState(eStateStopped);
225 break;
Ed Maste7fd845c2013-12-09 15:51:17 +0000226
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 case ProcessMessage::eNewThreadMessage:
228 llvm_unreachable("eNewThreadMessage unexpected on FreeBSD");
229 break;
Ed Maste7fd845c2013-12-09 15:51:17 +0000230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 case ProcessMessage::eExecMessage:
232 SetPrivateState(eStateStopped);
233 break;
234 }
Ed Maste7fd845c2013-12-09 15:51:17 +0000235
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 m_message_queue.push(message);
Ed Maste7fd845c2013-12-09 15:51:17 +0000237}
Ed Mastefe5a6422015-07-28 15:45:57 +0000238
239//------------------------------------------------------------------------------
240// Constructors and destructors.
241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242ProcessFreeBSD::ProcessFreeBSD(lldb::TargetSP target_sp,
243 lldb::ListenerSP listener_sp,
244 UnixSignalsSP &unix_signals_sp)
Jim Ingham583bbb12016-03-07 21:50:25 +0000245 : Process(target_sp, listener_sp, unix_signals_sp),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 m_byte_order(endian::InlHostByteOrder()), m_monitor(NULL), m_module(NULL),
247 m_message_mutex(), m_exit_now(false), m_seen_initial_stop(),
248 m_resume_signo(0) {
249 // FIXME: Putting this code in the ctor and saving the byte order in a
250 // member variable is a hack to avoid const qual issues in GetByteOrder.
251 lldb::ModuleSP module = GetTarget().GetExecutableModule();
252 if (module && module->GetObjectFile())
253 m_byte_order = module->GetObjectFile()->GetByteOrder();
Ed Mastefe5a6422015-07-28 15:45:57 +0000254}
255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256ProcessFreeBSD::~ProcessFreeBSD() { delete m_monitor; }
Ed Mastefe5a6422015-07-28 15:45:57 +0000257
258//------------------------------------------------------------------------------
259// Process protocol.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260void ProcessFreeBSD::Finalize() {
Ed Mastefe5a6422015-07-28 15:45:57 +0000261 Process::Finalize();
262
263 if (m_monitor)
264 m_monitor->StopMonitor();
265}
266
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267bool ProcessFreeBSD::CanDebug(lldb::TargetSP target_sp,
268 bool plugin_specified_by_name) {
269 // For now we are just making sure the file exists for a given module
270 ModuleSP exe_module_sp(target_sp->GetExecutableModule());
271 if (exe_module_sp.get())
272 return exe_module_sp->GetFileSpec().Exists();
273 // If there is no executable module, we return true since we might be
274 // preparing to attach.
275 return true;
Ed Mastefe5a6422015-07-28 15:45:57 +0000276}
277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278Error ProcessFreeBSD::DoAttachToProcessWithID(
279 lldb::pid_t pid, const ProcessAttachInfo &attach_info) {
280 Error error;
281 assert(m_monitor == NULL);
Ed Mastefe5a6422015-07-28 15:45:57 +0000282
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
284 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
285 log->Printf("ProcessFreeBSD::%s(pid = %" PRIi64 ")", __FUNCTION__, GetID());
Ed Mastefe5a6422015-07-28 15:45:57 +0000286
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287 m_monitor = new ProcessMonitor(this, pid, error);
Ed Mastefe5a6422015-07-28 15:45:57 +0000288
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289 if (!error.Success())
Ed Mastefe5a6422015-07-28 15:45:57 +0000290 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291
292 PlatformSP platform_sp(GetTarget().GetPlatform());
293 assert(platform_sp.get());
294 if (!platform_sp)
295 return error; // FIXME: Detatch?
296
297 // Find out what we can about this process
298 ProcessInstanceInfo process_info;
299 platform_sp->GetProcessInfo(pid, process_info);
300
301 // Resolve the executable module
302 ModuleSP exe_module_sp;
303 FileSpecList executable_search_paths(
304 Target::GetDefaultExecutableSearchPaths());
305 ModuleSpec exe_module_spec(process_info.GetExecutableFile(),
306 GetTarget().GetArchitecture());
307 error = platform_sp->ResolveExecutable(
308 exe_module_spec, exe_module_sp,
309 executable_search_paths.GetSize() ? &executable_search_paths : NULL);
310 if (!error.Success())
311 return error;
312
313 // Fix the target architecture if necessary
314 const ArchSpec &module_arch = exe_module_sp->GetArchitecture();
315 if (module_arch.IsValid() &&
316 !GetTarget().GetArchitecture().IsExactMatch(module_arch))
317 GetTarget().SetArchitecture(module_arch);
318
319 // Initialize the target module list
320 GetTarget().SetExecutableModule(exe_module_sp, true);
321
322 SetSTDIOFileDescriptor(m_monitor->GetTerminalFD());
323
324 SetID(pid);
325
326 return error;
Ed Mastefe5a6422015-07-28 15:45:57 +0000327}
328
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329Error ProcessFreeBSD::WillLaunch(Module *module) {
330 Error error;
331 return error;
Ed Mastefe5a6422015-07-28 15:45:57 +0000332}
333
334FileSpec
335ProcessFreeBSD::GetFileSpec(const lldb_private::FileAction *file_action,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336 const FileSpec &default_file_spec,
337 const FileSpec &dbg_pts_file_spec) {
338 FileSpec file_spec{};
Ed Mastefe5a6422015-07-28 15:45:57 +0000339
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340 if (file_action && file_action->GetAction() == FileAction::eFileActionOpen) {
341 file_spec = file_action->GetFileSpec();
342 // By default the stdio paths passed in will be pseudo-terminal
343 // (/dev/pts). If so, convert to using a different default path
344 // instead to redirect I/O to the debugger console. This should
345 // also handle user overrides to /dev/null or a different file.
346 if (!file_spec || file_spec == dbg_pts_file_spec)
347 file_spec = default_file_spec;
348 }
349 return file_spec;
Ed Mastefe5a6422015-07-28 15:45:57 +0000350}
351
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352Error ProcessFreeBSD::DoLaunch(Module *module, ProcessLaunchInfo &launch_info) {
353 Error error;
354 assert(m_monitor == NULL);
Ed Mastefe5a6422015-07-28 15:45:57 +0000355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 FileSpec working_dir = launch_info.GetWorkingDirectory();
357 if (working_dir &&
358 (!working_dir.ResolvePath() ||
359 working_dir.GetFileType() != FileSpec::eFileTypeDirectory)) {
360 error.SetErrorStringWithFormat("No such file or directory: %s",
361 working_dir.GetCString());
362 return error;
363 }
Ed Mastefe5a6422015-07-28 15:45:57 +0000364
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 SetPrivateState(eStateLaunching);
Ed Mastefe5a6422015-07-28 15:45:57 +0000366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 const lldb_private::FileAction *file_action;
Ed Mastefe5a6422015-07-28 15:45:57 +0000368
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 // Default of empty will mean to use existing open file descriptors
370 FileSpec stdin_file_spec{};
371 FileSpec stdout_file_spec{};
372 FileSpec stderr_file_spec{};
Ed Mastefe5a6422015-07-28 15:45:57 +0000373
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374 const FileSpec dbg_pts_file_spec{launch_info.GetPTY().GetSlaveName(NULL, 0),
375 false};
Ed Mastefe5a6422015-07-28 15:45:57 +0000376
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
378 stdin_file_spec =
379 GetFileSpec(file_action, stdin_file_spec, dbg_pts_file_spec);
Ed Mastefe5a6422015-07-28 15:45:57 +0000380
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381 file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
382 stdout_file_spec =
383 GetFileSpec(file_action, stdout_file_spec, dbg_pts_file_spec);
Ed Mastefe5a6422015-07-28 15:45:57 +0000384
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385 file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
386 stderr_file_spec =
387 GetFileSpec(file_action, stderr_file_spec, dbg_pts_file_spec);
Ed Mastefe5a6422015-07-28 15:45:57 +0000388
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 m_monitor = new ProcessMonitor(
390 this, module, launch_info.GetArguments().GetConstArgumentVector(),
391 launch_info.GetEnvironmentEntries().GetConstArgumentVector(),
392 stdin_file_spec, stdout_file_spec, stderr_file_spec, working_dir,
393 launch_info, error);
Ed Mastefe5a6422015-07-28 15:45:57 +0000394
Kate Stoneb9c1b512016-09-06 20:57:50 +0000395 m_module = module;
Ed Mastefe5a6422015-07-28 15:45:57 +0000396
Kate Stoneb9c1b512016-09-06 20:57:50 +0000397 if (!error.Success())
398 return error;
Ed Mastefe5a6422015-07-28 15:45:57 +0000399
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 int terminal = m_monitor->GetTerminalFD();
401 if (terminal >= 0) {
402// The reader thread will close the file descriptor when done, so we pass it a
403// copy.
Sylvestre Ledru79cb0092015-08-28 12:24:07 +0000404#ifdef F_DUPFD_CLOEXEC
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 int stdio = fcntl(terminal, F_DUPFD_CLOEXEC, 0);
406 if (stdio == -1) {
407 error.SetErrorToErrno();
408 return error;
409 }
Sylvestre Ledru79cb0092015-08-28 12:24:07 +0000410#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411 // Special case when F_DUPFD_CLOEXEC does not exist (Debian kFreeBSD)
412 int stdio = fcntl(terminal, F_DUPFD, 0);
413 if (stdio == -1) {
414 error.SetErrorToErrno();
415 return error;
416 }
417 stdio = fcntl(terminal, F_SETFD, FD_CLOEXEC);
418 if (stdio == -1) {
419 error.SetErrorToErrno();
420 return error;
421 }
Sylvestre Ledru79cb0092015-08-28 12:24:07 +0000422#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 SetSTDIOFileDescriptor(stdio);
424 }
425
426 SetID(m_monitor->GetPID());
427 return error;
428}
429
430void ProcessFreeBSD::DidLaunch() {}
431
432addr_t ProcessFreeBSD::GetImageInfoAddress() {
433 Target *target = &GetTarget();
434 ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
435 Address addr = obj_file->GetImageInfoAddress(target);
436
437 if (addr.IsValid())
438 return addr.GetLoadAddress(target);
439 return LLDB_INVALID_ADDRESS;
440}
441
442Error ProcessFreeBSD::DoHalt(bool &caused_stop) {
443 Error error;
444
445 if (IsStopped()) {
446 caused_stop = false;
447 } else if (kill(GetID(), SIGSTOP)) {
448 caused_stop = false;
449 error.SetErrorToErrno();
450 } else {
451 caused_stop = true;
452 }
453 return error;
454}
455
456Error ProcessFreeBSD::DoSignal(int signal) {
457 Error error;
458
459 if (kill(GetID(), signal))
460 error.SetErrorToErrno();
461
462 return error;
463}
464
465Error ProcessFreeBSD::DoDestroy() {
466 Error error;
467
468 if (!HasExited()) {
469 assert(m_monitor);
470 m_exit_now = true;
471 if (GetID() == LLDB_INVALID_PROCESS_ID) {
472 error.SetErrorString("invalid process id");
473 return error;
474 }
475 if (!m_monitor->Kill()) {
476 error.SetErrorToErrno();
477 return error;
Ed Mastefe5a6422015-07-28 15:45:57 +0000478 }
479
Kate Stoneb9c1b512016-09-06 20:57:50 +0000480 SetPrivateState(eStateExited);
481 }
482
483 return error;
Ed Mastefe5a6422015-07-28 15:45:57 +0000484}
485
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486void ProcessFreeBSD::DoDidExec() {
487 Target *target = &GetTarget();
488 if (target) {
489 PlatformSP platform_sp(target->GetPlatform());
490 assert(platform_sp.get());
491 if (platform_sp) {
492 ProcessInstanceInfo process_info;
493 platform_sp->GetProcessInfo(GetID(), process_info);
494 ModuleSP exe_module_sp;
495 ModuleSpec exe_module_spec(process_info.GetExecutableFile(),
496 target->GetArchitecture());
497 FileSpecList executable_search_paths(
498 Target::GetDefaultExecutableSearchPaths());
499 Error error = platform_sp->ResolveExecutable(
500 exe_module_spec, exe_module_sp,
501 executable_search_paths.GetSize() ? &executable_search_paths : NULL);
502 if (!error.Success())
503 return;
504 target->SetExecutableModule(exe_module_sp, true);
Ed Mastefe5a6422015-07-28 15:45:57 +0000505 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506 }
Ed Mastefe5a6422015-07-28 15:45:57 +0000507}
508
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509bool ProcessFreeBSD::AddThreadForInitialStopIfNeeded(lldb::tid_t stop_tid) {
510 bool added_to_set = false;
511 ThreadStopSet::iterator it = m_seen_initial_stop.find(stop_tid);
512 if (it == m_seen_initial_stop.end()) {
513 m_seen_initial_stop.insert(stop_tid);
514 added_to_set = true;
515 }
516 return added_to_set;
Ed Mastefe5a6422015-07-28 15:45:57 +0000517}
518
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519bool ProcessFreeBSD::WaitingForInitialStop(lldb::tid_t stop_tid) {
520 return (m_seen_initial_stop.find(stop_tid) == m_seen_initial_stop.end());
Ed Mastefe5a6422015-07-28 15:45:57 +0000521}
522
523FreeBSDThread *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524ProcessFreeBSD::CreateNewFreeBSDThread(lldb_private::Process &process,
525 lldb::tid_t tid) {
526 return new FreeBSDThread(process, tid);
Ed Mastefe5a6422015-07-28 15:45:57 +0000527}
528
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529void ProcessFreeBSD::RefreshStateAfterStop() {
530 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS));
531 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
532 log->Printf("ProcessFreeBSD::%s(), message_queue size = %d", __FUNCTION__,
533 (int)m_message_queue.size());
Ed Mastefe5a6422015-07-28 15:45:57 +0000534
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535 std::lock_guard<std::recursive_mutex> guard(m_message_mutex);
Ed Mastefe5a6422015-07-28 15:45:57 +0000536
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537 // This method used to only handle one message. Changing it to loop allows
538 // it to handle the case where we hit a breakpoint while handling a different
539 // breakpoint.
540 while (!m_message_queue.empty()) {
541 ProcessMessage &message = m_message_queue.front();
Ed Mastefe5a6422015-07-28 15:45:57 +0000542
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543 // Resolve the thread this message corresponds to and pass it along.
544 lldb::tid_t tid = message.GetTID();
545 if (log)
546 log->Printf(
547 "ProcessFreeBSD::%s(), message_queue size = %d, pid = %" PRIi64,
548 __FUNCTION__, (int)m_message_queue.size(), tid);
Ed Mastefe5a6422015-07-28 15:45:57 +0000549
Kate Stoneb9c1b512016-09-06 20:57:50 +0000550 m_thread_list.RefreshStateAfterStop();
Ed Mastefe5a6422015-07-28 15:45:57 +0000551
Kate Stoneb9c1b512016-09-06 20:57:50 +0000552 FreeBSDThread *thread = static_cast<FreeBSDThread *>(
553 GetThreadList().FindThreadByID(tid, false).get());
Ed Mastefe5a6422015-07-28 15:45:57 +0000554 if (thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555 thread->Notify(message);
556
557 if (message.GetKind() == ProcessMessage::eExitMessage) {
558 // FIXME: We should tell the user about this, but the limbo message is
559 // probably better for that.
560 if (log)
561 log->Printf("ProcessFreeBSD::%s() removing thread, tid = %" PRIi64,
562 __FUNCTION__, tid);
563
564 std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex());
565
566 ThreadSP thread_sp = m_thread_list.RemoveThreadByID(tid, false);
567 thread_sp.reset();
568 m_seen_initial_stop.erase(tid);
569 }
570
571 m_message_queue.pop();
572 }
Ed Mastefe5a6422015-07-28 15:45:57 +0000573}
574
Kate Stoneb9c1b512016-09-06 20:57:50 +0000575bool ProcessFreeBSD::IsAlive() {
576 StateType state = GetPrivateState();
577 return state != eStateDetached && state != eStateExited &&
578 state != eStateInvalid && state != eStateUnloaded;
Ed Mastefe5a6422015-07-28 15:45:57 +0000579}
580
Kate Stoneb9c1b512016-09-06 20:57:50 +0000581size_t ProcessFreeBSD::DoReadMemory(addr_t vm_addr, void *buf, size_t size,
582 Error &error) {
583 assert(m_monitor);
584 return m_monitor->ReadMemory(vm_addr, buf, size, error);
585}
586
587size_t ProcessFreeBSD::DoWriteMemory(addr_t vm_addr, const void *buf,
588 size_t size, Error &error) {
589 assert(m_monitor);
590 return m_monitor->WriteMemory(vm_addr, buf, size, error);
591}
592
593addr_t ProcessFreeBSD::DoAllocateMemory(size_t size, uint32_t permissions,
594 Error &error) {
595 addr_t allocated_addr = LLDB_INVALID_ADDRESS;
596
597 unsigned prot = 0;
598 if (permissions & lldb::ePermissionsReadable)
599 prot |= eMmapProtRead;
600 if (permissions & lldb::ePermissionsWritable)
601 prot |= eMmapProtWrite;
602 if (permissions & lldb::ePermissionsExecutable)
603 prot |= eMmapProtExec;
604
605 if (InferiorCallMmap(this, allocated_addr, 0, size, prot,
606 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) {
607 m_addr_to_mmap_size[allocated_addr] = size;
608 error.Clear();
609 } else {
610 allocated_addr = LLDB_INVALID_ADDRESS;
611 error.SetErrorStringWithFormat(
612 "unable to allocate %zu bytes of memory with permissions %s", size,
613 GetPermissionsAsCString(permissions));
614 }
615
616 return allocated_addr;
617}
618
619Error ProcessFreeBSD::DoDeallocateMemory(lldb::addr_t addr) {
620 Error error;
621 MMapMap::iterator pos = m_addr_to_mmap_size.find(addr);
622 if (pos != m_addr_to_mmap_size.end() &&
623 InferiorCallMunmap(this, addr, pos->second))
624 m_addr_to_mmap_size.erase(pos);
625 else
626 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64,
627 addr);
628
629 return error;
630}
631
632size_t
633ProcessFreeBSD::GetSoftwareBreakpointTrapOpcode(BreakpointSite *bp_site) {
634 static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xD4};
635 static const uint8_t g_i386_opcode[] = {0xCC};
636
637 ArchSpec arch = GetTarget().GetArchitecture();
638 const uint8_t *opcode = NULL;
639 size_t opcode_size = 0;
640
641 switch (arch.GetMachine()) {
642 default:
643 assert(false && "CPU type not supported!");
644 break;
645
646 case llvm::Triple::arm: {
647 // The ARM reference recommends the use of 0xe7fddefe and 0xdefe
648 // but the linux kernel does otherwise.
649 static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
650 static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
651
652 lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
653 AddressClass addr_class = eAddressClassUnknown;
654
655 if (bp_loc_sp)
656 addr_class = bp_loc_sp->GetAddress().GetAddressClass();
657
658 if (addr_class == eAddressClassCodeAlternateISA ||
659 (addr_class == eAddressClassUnknown &&
660 bp_loc_sp->GetAddress().GetOffset() & 1)) {
661 opcode = g_thumb_breakpoint_opcode;
662 opcode_size = sizeof(g_thumb_breakpoint_opcode);
663 } else {
664 opcode = g_arm_breakpoint_opcode;
665 opcode_size = sizeof(g_arm_breakpoint_opcode);
666 }
667 } break;
668 case llvm::Triple::aarch64:
669 opcode = g_aarch64_opcode;
670 opcode_size = sizeof(g_aarch64_opcode);
671 break;
672
673 case llvm::Triple::x86:
674 case llvm::Triple::x86_64:
675 opcode = g_i386_opcode;
676 opcode_size = sizeof(g_i386_opcode);
677 break;
678 }
679
680 bp_site->SetTrapOpcode(opcode, opcode_size);
681 return opcode_size;
682}
683
684Error ProcessFreeBSD::EnableBreakpointSite(BreakpointSite *bp_site) {
685 return EnableSoftwareBreakpoint(bp_site);
686}
687
688Error ProcessFreeBSD::DisableBreakpointSite(BreakpointSite *bp_site) {
689 return DisableSoftwareBreakpoint(bp_site);
690}
691
692Error ProcessFreeBSD::EnableWatchpoint(Watchpoint *wp, bool notify) {
693 Error error;
694 if (wp) {
695 user_id_t watchID = wp->GetID();
696 addr_t addr = wp->GetLoadAddress();
697 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
698 if (log)
699 log->Printf("ProcessFreeBSD::EnableWatchpoint(watchID = %" PRIu64 ")",
700 watchID);
701 if (wp->IsEnabled()) {
702 if (log)
703 log->Printf("ProcessFreeBSD::EnableWatchpoint(watchID = %" PRIu64
704 ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.",
705 watchID, (uint64_t)addr);
706 return error;
707 }
708
709 // Try to find a vacant watchpoint slot in the inferiors' main thread
710 uint32_t wp_hw_index = LLDB_INVALID_INDEX32;
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000711 std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712 FreeBSDThread *thread = static_cast<FreeBSDThread *>(
713 m_thread_list.GetThreadAtIndex(0, false).get());
714
715 if (thread)
716 wp_hw_index = thread->FindVacantWatchpointIndex();
717
718 if (wp_hw_index == LLDB_INVALID_INDEX32) {
719 error.SetErrorString("Setting hardware watchpoint failed.");
720 } else {
721 wp->SetHardwareIndex(wp_hw_index);
722 bool wp_enabled = true;
723 uint32_t thread_count = m_thread_list.GetSize(false);
724 for (uint32_t i = 0; i < thread_count; ++i) {
725 thread = static_cast<FreeBSDThread *>(
726 m_thread_list.GetThreadAtIndex(i, false).get());
727 if (thread)
728 wp_enabled &= thread->EnableHardwareWatchpoint(wp);
729 else
730 wp_enabled = false;
731 }
732 if (wp_enabled) {
733 wp->SetEnabled(true, notify);
734 return error;
735 } else {
736 // Watchpoint enabling failed on at least one
737 // of the threads so roll back all of them
738 DisableWatchpoint(wp, false);
739 error.SetErrorString("Setting hardware watchpoint failed");
740 }
741 }
742 } else
743 error.SetErrorString("Watchpoint argument was NULL.");
744 return error;
745}
746
747Error ProcessFreeBSD::DisableWatchpoint(Watchpoint *wp, bool notify) {
748 Error error;
749 if (wp) {
750 user_id_t watchID = wp->GetID();
751 addr_t addr = wp->GetLoadAddress();
752 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
753 if (log)
754 log->Printf("ProcessFreeBSD::DisableWatchpoint(watchID = %" PRIu64 ")",
755 watchID);
756 if (!wp->IsEnabled()) {
757 if (log)
758 log->Printf("ProcessFreeBSD::DisableWatchpoint(watchID = %" PRIu64
759 ") addr = 0x%8.8" PRIx64 ": watchpoint already disabled.",
760 watchID, (uint64_t)addr);
761 // This is needed (for now) to keep watchpoints disabled correctly
762 wp->SetEnabled(false, notify);
763 return error;
764 }
765
766 if (wp->IsHardware()) {
767 bool wp_disabled = true;
768 std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex());
769 uint32_t thread_count = m_thread_list.GetSize(false);
770 for (uint32_t i = 0; i < thread_count; ++i) {
771 FreeBSDThread *thread = static_cast<FreeBSDThread *>(
772 m_thread_list.GetThreadAtIndex(i, false).get());
773 if (thread)
774 wp_disabled &= thread->DisableHardwareWatchpoint(wp);
775 else
776 wp_disabled = false;
777 }
778 if (wp_disabled) {
779 wp->SetHardwareIndex(LLDB_INVALID_INDEX32);
780 wp->SetEnabled(false, notify);
781 return error;
782 } else
783 error.SetErrorString("Disabling hardware watchpoint failed");
784 }
785 } else
786 error.SetErrorString("Watchpoint argument was NULL.");
787 return error;
788}
789
790Error ProcessFreeBSD::GetWatchpointSupportInfo(uint32_t &num) {
791 Error error;
792 std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex());
793 FreeBSDThread *thread = static_cast<FreeBSDThread *>(
794 m_thread_list.GetThreadAtIndex(0, false).get());
795 if (thread)
796 num = thread->NumSupportedHardwareWatchpoints();
797 else
798 error.SetErrorString("Process does not exist.");
799 return error;
800}
801
802Error ProcessFreeBSD::GetWatchpointSupportInfo(uint32_t &num, bool &after) {
803 Error error = GetWatchpointSupportInfo(num);
804 // Watchpoints trigger and halt the inferior after
805 // the corresponding instruction has been executed.
806 after = true;
807 return error;
808}
809
810uint32_t ProcessFreeBSD::UpdateThreadListIfNeeded() {
811 std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex());
812 // Do not allow recursive updates.
813 return m_thread_list.GetSize(false);
Ed Mastefe5a6422015-07-28 15:45:57 +0000814}
815
816#if 0
817bool
818ProcessFreeBSD::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list)
819{
820 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
821 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
822 log->Printf ("ProcessFreeBSD::%s() (pid = %" PRIi64 ")", __FUNCTION__, GetID());
823
824 bool has_updated = false;
825 // Update the process thread list with this new thread.
826 // FIXME: We should be using tid, not pid.
827 assert(m_monitor);
828 ThreadSP thread_sp (old_thread_list.FindThreadByID (GetID(), false));
829 if (!thread_sp) {
830 thread_sp.reset(CreateNewFreeBSDThread(*this, GetID()));
831 has_updated = true;
832 }
833
834 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
835 log->Printf ("ProcessFreeBSD::%s() updated pid = %" PRIi64, __FUNCTION__, GetID());
836 new_thread_list.AddThread(thread_sp);
837
838 return has_updated; // the list has been updated
839}
840#endif
841
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842ByteOrder ProcessFreeBSD::GetByteOrder() const {
843 // FIXME: We should be able to extract this value directly. See comment in
844 // ProcessFreeBSD().
845 return m_byte_order;
Ed Mastefe5a6422015-07-28 15:45:57 +0000846}
847
Kate Stoneb9c1b512016-09-06 20:57:50 +0000848size_t ProcessFreeBSD::PutSTDIN(const char *buf, size_t len, Error &error) {
849 ssize_t status;
850 if ((status = write(m_monitor->GetTerminalFD(), buf, len)) < 0) {
851 error.SetErrorToErrno();
852 return 0;
853 }
854 return status;
Ed Mastefe5a6422015-07-28 15:45:57 +0000855}
856
857//------------------------------------------------------------------------------
858// Utility functions.
859
Kate Stoneb9c1b512016-09-06 20:57:50 +0000860bool ProcessFreeBSD::HasExited() {
861 switch (GetPrivateState()) {
862 default:
863 break;
Ed Mastefe5a6422015-07-28 15:45:57 +0000864
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865 case eStateDetached:
866 case eStateExited:
867 return true;
868 }
Ed Mastefe5a6422015-07-28 15:45:57 +0000869
Kate Stoneb9c1b512016-09-06 20:57:50 +0000870 return false;
Ed Mastefe5a6422015-07-28 15:45:57 +0000871}
872
Kate Stoneb9c1b512016-09-06 20:57:50 +0000873bool ProcessFreeBSD::IsStopped() {
874 switch (GetPrivateState()) {
875 default:
876 break;
Ed Mastefe5a6422015-07-28 15:45:57 +0000877
Kate Stoneb9c1b512016-09-06 20:57:50 +0000878 case eStateStopped:
879 case eStateCrashed:
880 case eStateSuspended:
881 return true;
882 }
Ed Mastefe5a6422015-07-28 15:45:57 +0000883
Kate Stoneb9c1b512016-09-06 20:57:50 +0000884 return false;
Ed Mastefe5a6422015-07-28 15:45:57 +0000885}
886
Kate Stoneb9c1b512016-09-06 20:57:50 +0000887bool ProcessFreeBSD::IsAThreadRunning() {
888 bool is_running = false;
889 std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex());
890 uint32_t thread_count = m_thread_list.GetSize(false);
891 for (uint32_t i = 0; i < thread_count; ++i) {
892 FreeBSDThread *thread = static_cast<FreeBSDThread *>(
893 m_thread_list.GetThreadAtIndex(i, false).get());
894 StateType thread_state = thread->GetState();
895 if (thread_state == eStateRunning || thread_state == eStateStepping) {
896 is_running = true;
897 break;
Ed Mastefe5a6422015-07-28 15:45:57 +0000898 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000899 }
900 return is_running;
Ed Mastefe5a6422015-07-28 15:45:57 +0000901}
902
Kate Stoneb9c1b512016-09-06 20:57:50 +0000903const DataBufferSP ProcessFreeBSD::GetAuxvData() {
904 // If we're the local platform, we can ask the host for auxv data.
905 PlatformSP platform_sp = GetTarget().GetPlatform();
906 if (platform_sp && platform_sp->IsHost())
907 return lldb_private::Host::GetAuxvData(this);
Ed Mastefe5a6422015-07-28 15:45:57 +0000908
Kate Stoneb9c1b512016-09-06 20:57:50 +0000909 // Somewhat unexpected - the process is not running locally or we don't have a
910 // platform.
911 assert(
912 false &&
913 "no platform or not the host - how did we get here with ProcessFreeBSD?");
914 return DataBufferSP();
Ed Mastefe5a6422015-07-28 15:45:57 +0000915}