blob: 0e5ab5a8d8b1e1920b94b418d84c24796984101c [file] [log] [blame]
Johnny Chen9ed5b492012-01-05 21:48:15 +00001//===-- ProcessPOSIX.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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Johnny Chen9ed5b492012-01-05 21:48:15 +000012// C Includes
13#include <errno.h>
14
15// C++ Includes
16// Other libraries and framework includes
Matt Kopece9ea0da2013-05-07 19:29:28 +000017#include "lldb/Breakpoint/Watchpoint.h"
Greg Claytondcbfd192012-09-04 14:55:50 +000018#include "lldb/Core/Module.h"
Oleksiy Vyalov6edef202014-11-17 22:16:42 +000019#include "lldb/Core/ModuleSpec.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000020#include "lldb/Core/PluginManager.h"
21#include "lldb/Core/State.h"
Daniel Malea6217d2a2013-01-08 14:49:22 +000022#include "lldb/Host/FileSpec.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000023#include "lldb/Host/Host.h"
24#include "lldb/Symbol/ObjectFile.h"
25#include "lldb/Target/DynamicLoader.h"
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +000026#include "lldb/Target/Platform.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000027#include "lldb/Target/Target.h"
28
29#include "ProcessPOSIX.h"
30#include "ProcessPOSIXLog.h"
31#include "Plugins/Process/Utility/InferiorCallPOSIX.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000032#include "Plugins/Process/Linux/ProcessMonitor.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000033#include "POSIXThread.h"
34
35using namespace lldb;
36using namespace lldb_private;
37
38//------------------------------------------------------------------------------
39// Static functions.
40#if 0
41Process*
42ProcessPOSIX::CreateInstance(Target& target, Listener &listener)
43{
44 return new ProcessPOSIX(target, listener);
45}
46
47
48void
49ProcessPOSIX::Initialize()
50{
51 static bool g_initialized = false;
52
53 if (!g_initialized)
54 {
55 g_initialized = true;
56 PluginManager::RegisterPlugin(GetPluginNameStatic(),
57 GetPluginDescriptionStatic(),
58 CreateInstance);
59
60 Log::Callbacks log_callbacks = {
61 ProcessPOSIXLog::DisableLog,
62 ProcessPOSIXLog::EnableLog,
63 ProcessPOSIXLog::ListLogCategories
64 };
65
66 Log::RegisterLogChannel (ProcessPOSIX::GetPluginNameStatic(), log_callbacks);
67 }
68}
69#endif
70
71//------------------------------------------------------------------------------
72// Constructors and destructors.
73
Todd Fiala4ceced32014-08-29 17:35:57 +000074ProcessPOSIX::ProcessPOSIX(Target& target, Listener &listener, UnixSignalsSP &unix_signals_sp)
75 : Process(target, listener, unix_signals_sp),
Johnny Chen7b9f93a2012-04-14 00:54:42 +000076 m_byte_order(lldb::endian::InlHostByteOrder()),
Johnny Chen9ed5b492012-01-05 21:48:15 +000077 m_monitor(NULL),
78 m_module(NULL),
Andrew Kaylor93132f52013-05-28 23:04:25 +000079 m_message_mutex (Mutex::eMutexTypeRecursive),
Matt Kopecb2910442013-07-09 15:09:45 +000080 m_exit_now(false),
81 m_seen_initial_stop()
Johnny Chen9ed5b492012-01-05 21:48:15 +000082{
83 // FIXME: Putting this code in the ctor and saving the byte order in a
84 // member variable is a hack to avoid const qual issues in GetByteOrder.
Johnny Chen7b9f93a2012-04-14 00:54:42 +000085 lldb::ModuleSP module = GetTarget().GetExecutableModule();
Michael Sartaina7499c92013-07-01 19:45:50 +000086 if (module && module->GetObjectFile())
Johnny Chen7b9f93a2012-04-14 00:54:42 +000087 m_byte_order = module->GetObjectFile()->GetByteOrder();
Johnny Chen9ed5b492012-01-05 21:48:15 +000088}
89
90ProcessPOSIX::~ProcessPOSIX()
91{
92 delete m_monitor;
93}
94
95//------------------------------------------------------------------------------
96// Process protocol.
Andrew Kaylorbc68b432013-07-10 21:57:27 +000097void
98ProcessPOSIX::Finalize()
99{
100 Process::Finalize();
101
102 if (m_monitor)
103 m_monitor->StopMonitor();
104}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000105
106bool
107ProcessPOSIX::CanDebug(Target &target, bool plugin_specified_by_name)
108{
109 // For now we are just making sure the file exists for a given module
110 ModuleSP exe_module_sp(target.GetExecutableModule());
111 if (exe_module_sp.get())
112 return exe_module_sp->GetFileSpec().Exists();
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000113 // If there is no executable module, we return true since we might be preparing to attach.
114 return true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000115}
116
117Error
118ProcessPOSIX::DoAttachToProcessWithID(lldb::pid_t pid)
119{
120 Error error;
121 assert(m_monitor == NULL);
122
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000123 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000124 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000125 log->Printf ("ProcessPOSIX::%s(pid = %" PRIi64 ")", __FUNCTION__, GetID());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000126
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000127 m_monitor = new ProcessMonitor(this, pid, error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000128
129 if (!error.Success())
130 return error;
131
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000132 PlatformSP platform_sp (m_target.GetPlatform ());
133 assert (platform_sp.get());
134 if (!platform_sp)
135 return error; // FIXME: Detatch?
136
137 // Find out what we can about this process
138 ProcessInstanceInfo process_info;
139 platform_sp->GetProcessInfo (pid, process_info);
140
141 // Resolve the executable module
142 ModuleSP exe_module_sp;
143 FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths());
Oleksiy Vyalov6edef202014-11-17 22:16:42 +0000144 ModuleSpec exe_module_spec(process_info.GetExecutableFile(), m_target.GetArchitecture());
145 error = platform_sp->ResolveExecutable(exe_module_spec,
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000146 exe_module_sp,
147 executable_search_paths.GetSize() ? &executable_search_paths : NULL);
Ed Mastebe0b55d2013-07-04 18:25:34 +0000148 if (!error.Success())
149 return error;
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000150
151 // Fix the target architecture if necessary
152 const ArchSpec &module_arch = exe_module_sp->GetArchitecture();
153 if (module_arch.IsValid() && !m_target.GetArchitecture().IsExactMatch(module_arch))
154 m_target.SetArchitecture(module_arch);
155
156 // Initialize the target module list
157 m_target.SetExecutableModule (exe_module_sp, true);
158
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000159 SetSTDIOFileDescriptor(m_monitor->GetTerminalFD());
160
Johnny Chen9ed5b492012-01-05 21:48:15 +0000161 SetID(pid);
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000162
Johnny Chen9ed5b492012-01-05 21:48:15 +0000163 return error;
164}
165
166Error
Greg Claytone2186ed2012-09-07 17:51:47 +0000167ProcessPOSIX::DoAttachToProcessWithID (lldb::pid_t pid, const ProcessAttachInfo &attach_info)
168{
169 return DoAttachToProcessWithID(pid);
170}
171
172Error
Johnny Chen9ed5b492012-01-05 21:48:15 +0000173ProcessPOSIX::WillLaunch(Module* module)
174{
175 Error error;
176 return error;
177}
178
179const char *
Todd Fiala6a2f62c2014-09-08 15:57:14 +0000180ProcessPOSIX::GetFilePath(const lldb_private::FileAction *file_action, const char *default_path,
181 const char *dbg_pts_path)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000182{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000183 const char *path = NULL;
184
185 if (file_action)
186 {
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000187 if (file_action->GetAction() == FileAction::eFileActionOpen)
Saleem Abdulrasool03700de2014-03-08 20:47:03 +0000188 {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000189 path = file_action->GetPath();
190 // By default the stdio paths passed in will be pseudo-terminal
191 // (/dev/pts). If so, convert to using a different default path
192 // instead to redirect I/O to the debugger console. This should
193 // also handle user overrides to /dev/null or a different file.
Todd Fiala6a2f62c2014-09-08 15:57:14 +0000194 if (!path || (dbg_pts_path &&
195 ::strncmp(path, dbg_pts_path, ::strlen(dbg_pts_path)) == 0))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000196 path = default_path;
Saleem Abdulrasool03700de2014-03-08 20:47:03 +0000197 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000198 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000199 return path;
200}
201
202Error
203ProcessPOSIX::DoLaunch (Module *module,
Jean-Daniel Dupas7782de92013-12-09 22:52:50 +0000204 ProcessLaunchInfo &launch_info)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000205{
206 Error error;
207 assert(m_monitor == NULL);
208
Daniel Malea6217d2a2013-01-08 14:49:22 +0000209 const char* working_dir = launch_info.GetWorkingDirectory();
210 if (working_dir) {
211 FileSpec WorkingDir(working_dir, true);
212 if (!WorkingDir || WorkingDir.GetFileType() != FileSpec::eFileTypeDirectory)
213 {
214 error.SetErrorStringWithFormat("No such file or directory: %s", working_dir);
215 return error;
216 }
217 }
218
Johnny Chen9ed5b492012-01-05 21:48:15 +0000219 SetPrivateState(eStateLaunching);
220
Zachary Turner696b5282014-08-14 16:01:25 +0000221 const lldb_private::FileAction *file_action;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000222
223 // Default of NULL will mean to use existing open file descriptors
224 const char *stdin_path = NULL;
225 const char *stdout_path = NULL;
226 const char *stderr_path = NULL;
Daniel Malea6217d2a2013-01-08 14:49:22 +0000227
Todd Fiala6a2f62c2014-09-08 15:57:14 +0000228 const char * dbg_pts_path = launch_info.GetPTY().GetSlaveName(NULL,0);
229
Johnny Chen9ed5b492012-01-05 21:48:15 +0000230 file_action = launch_info.GetFileActionForFD (STDIN_FILENO);
Todd Fiala6a2f62c2014-09-08 15:57:14 +0000231 stdin_path = GetFilePath(file_action, stdin_path, dbg_pts_path);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000232
233 file_action = launch_info.GetFileActionForFD (STDOUT_FILENO);
Todd Fiala6a2f62c2014-09-08 15:57:14 +0000234 stdout_path = GetFilePath(file_action, stdout_path, dbg_pts_path);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000235
236 file_action = launch_info.GetFileActionForFD (STDERR_FILENO);
Todd Fiala6a2f62c2014-09-08 15:57:14 +0000237 stderr_path = GetFilePath(file_action, stderr_path, dbg_pts_path);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000238
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000239 m_monitor = new ProcessMonitor (this,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000240 module,
241 launch_info.GetArguments().GetConstArgumentVector(),
242 launch_info.GetEnvironmentEntries().GetConstArgumentVector(),
243 stdin_path,
244 stdout_path,
245 stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000246 working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000247 launch_info,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000248 error);
249
250 m_module = module;
251
252 if (!error.Success())
253 return error;
254
Matt Kopec9eb40a92013-03-14 21:35:26 +0000255 SetSTDIOFileDescriptor(m_monitor->GetTerminalFD());
256
Johnny Chen9ed5b492012-01-05 21:48:15 +0000257 SetID(m_monitor->GetPID());
258 return error;
259}
260
261void
262ProcessPOSIX::DidLaunch()
263{
264}
265
Ed Maste30df85e2013-12-11 20:43:27 +0000266Error
267ProcessPOSIX::DoResume()
268{
269 StateType state = GetPrivateState();
270
271 assert(state == eStateStopped);
272
273 SetPrivateState(eStateRunning);
274
275 bool did_resume = false;
276
277 Mutex::Locker lock(m_thread_list.GetMutex());
278
279 uint32_t thread_count = m_thread_list.GetSize(false);
280 for (uint32_t i = 0; i < thread_count; ++i)
281 {
282 POSIXThread *thread = static_cast<POSIXThread*>(
283 m_thread_list.GetThreadAtIndex(i, false).get());
284 did_resume = thread->Resume() || did_resume;
285 }
286 assert(did_resume && "Process resume failed!");
287
288 return Error();
289}
290
Johnny Chen9ed5b492012-01-05 21:48:15 +0000291addr_t
292ProcessPOSIX::GetImageInfoAddress()
293{
294 Target *target = &GetTarget();
295 ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
Ed Maste54803652013-10-11 17:39:07 +0000296 Address addr = obj_file->GetImageInfoAddress(target);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000297
Ed Maste04a8bab2013-10-11 01:16:08 +0000298 if (addr.IsValid())
Ed Maste54803652013-10-11 17:39:07 +0000299 return addr.GetLoadAddress(target);
Ed Maste04a8bab2013-10-11 01:16:08 +0000300 return LLDB_INVALID_ADDRESS;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000301}
302
303Error
304ProcessPOSIX::DoHalt(bool &caused_stop)
305{
306 Error error;
307
308 if (IsStopped())
309 {
310 caused_stop = false;
311 }
312 else if (kill(GetID(), SIGSTOP))
313 {
314 caused_stop = false;
315 error.SetErrorToErrno();
316 }
317 else
318 {
319 caused_stop = true;
320 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000321 return error;
322}
323
324Error
Johnny Chen9ed5b492012-01-05 21:48:15 +0000325ProcessPOSIX::DoSignal(int signal)
326{
327 Error error;
328
329 if (kill(GetID(), signal))
330 error.SetErrorToErrno();
331
332 return error;
333}
334
335Error
336ProcessPOSIX::DoDestroy()
337{
338 Error error;
339
340 if (!HasExited())
341 {
Ed Maste70882932014-04-01 14:30:56 +0000342 assert(m_monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000343 m_exit_now = true;
Oleksiy Vyalov5d064742014-11-19 18:27:45 +0000344 if (GetID() == LLDB_INVALID_PROCESS_ID)
345 {
346 error.SetErrorString("invalid process id");
347 return error;
348 }
Ed Maste70882932014-04-01 14:30:56 +0000349 if (!m_monitor->Kill())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000350 {
351 error.SetErrorToErrno();
352 return error;
353 }
354
355 SetPrivateState(eStateExited);
356 }
357
358 return error;
359}
360
361void
Matt Kopec718be872013-10-09 19:39:55 +0000362ProcessPOSIX::DoDidExec()
363{
364 Target *target = &GetTarget();
365 if (target)
366 {
367 PlatformSP platform_sp (target->GetPlatform());
368 assert (platform_sp.get());
369 if (platform_sp)
370 {
371 ProcessInstanceInfo process_info;
372 platform_sp->GetProcessInfo(GetID(), process_info);
373 ModuleSP exe_module_sp;
Oleksiy Vyalov6edef202014-11-17 22:16:42 +0000374 ModuleSpec exe_module_spec(process_info.GetExecutableFile(), target->GetArchitecture());
Matt Kopec718be872013-10-09 19:39:55 +0000375 FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths());
Oleksiy Vyalov6edef202014-11-17 22:16:42 +0000376 Error error = platform_sp->ResolveExecutable(exe_module_spec,
377 exe_module_sp,
378 executable_search_paths.GetSize() ? &executable_search_paths : NULL);
Matt Kopec718be872013-10-09 19:39:55 +0000379 if (!error.Success())
380 return;
381 target->SetExecutableModule(exe_module_sp, true);
382 }
383 }
384}
385
Ed Maste30df85e2013-12-11 20:43:27 +0000386void
387ProcessPOSIX::SendMessage(const ProcessMessage &message)
388{
Andrew MacPherson82aae0d2014-04-02 06:57:45 +0000389 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
390
Ed Maste30df85e2013-12-11 20:43:27 +0000391 Mutex::Locker lock(m_message_mutex);
392
393 Mutex::Locker thread_lock(m_thread_list.GetMutex());
394
395 POSIXThread *thread = static_cast<POSIXThread*>(
396 m_thread_list.FindThreadByID(message.GetTID(), false).get());
397
398 switch (message.GetKind())
399 {
400 case ProcessMessage::eInvalidMessage:
401 return;
402
403 case ProcessMessage::eAttachMessage:
404 SetPrivateState(eStateStopped);
405 return;
406
407 case ProcessMessage::eLimboMessage:
408 assert(thread);
409 thread->SetState(eStateStopped);
410 if (message.GetTID() == GetID())
411 {
412 m_exit_status = message.GetExitStatus();
413 if (m_exit_now)
414 {
415 SetPrivateState(eStateExited);
416 m_monitor->Detach(GetID());
417 }
418 else
419 {
420 StopAllThreads(message.GetTID());
421 SetPrivateState(eStateStopped);
422 }
423 }
424 else
425 {
426 StopAllThreads(message.GetTID());
427 SetPrivateState(eStateStopped);
428 }
429 break;
430
431 case ProcessMessage::eExitMessage:
Andrew MacPherson82aae0d2014-04-02 06:57:45 +0000432 if (thread != nullptr)
433 thread->SetState(eStateExited);
434 else
435 {
436 if (log)
437 log->Warning ("ProcessPOSIX::%s eExitMessage for TID %" PRIu64 " failed to find a thread to mark as eStateExited, ignoring", __FUNCTION__, message.GetTID ());
438 }
439
Ed Maste30df85e2013-12-11 20:43:27 +0000440 // FIXME: I'm not sure we need to do this.
441 if (message.GetTID() == GetID())
442 {
Todd Fiala7b0917a2014-09-15 20:07:33 +0000443 SetExitStatus(message.GetExitStatus(), NULL);
Ed Maste30df85e2013-12-11 20:43:27 +0000444 }
445 else if (!IsAThreadRunning())
446 SetPrivateState(eStateStopped);
447 break;
448
449 case ProcessMessage::eSignalMessage:
450 case ProcessMessage::eSignalDeliveredMessage:
451 if (message.GetSignal() == SIGSTOP &&
452 AddThreadForInitialStopIfNeeded(message.GetTID()))
453 return;
454 // Intentional fall-through
455
456 case ProcessMessage::eBreakpointMessage:
457 case ProcessMessage::eTraceMessage:
458 case ProcessMessage::eWatchpointMessage:
459 case ProcessMessage::eCrashMessage:
460 assert(thread);
461 thread->SetState(eStateStopped);
462 StopAllThreads(message.GetTID());
463 SetPrivateState(eStateStopped);
464 break;
465
466 case ProcessMessage::eNewThreadMessage:
467 {
468 lldb::tid_t new_tid = message.GetChildTID();
469 if (WaitingForInitialStop(new_tid))
470 {
471 m_monitor->WaitForInitialTIDStop(new_tid);
472 }
473 assert(thread);
474 thread->SetState(eStateStopped);
475 StopAllThreads(message.GetTID());
476 SetPrivateState(eStateStopped);
477 break;
478 }
479
480 case ProcessMessage::eExecMessage:
481 {
482 assert(thread);
483 thread->SetState(eStateStopped);
484 StopAllThreads(message.GetTID());
485 SetPrivateState(eStateStopped);
486 break;
487 }
488 }
489
490
491 m_message_queue.push(message);
492}
493
Andrew Kaylor93132f52013-05-28 23:04:25 +0000494void
495ProcessPOSIX::StopAllThreads(lldb::tid_t stop_tid)
496{
497 // FIXME: Will this work the same way on FreeBSD and Linux?
498}
499
Matt Kopecb2910442013-07-09 15:09:45 +0000500bool
501ProcessPOSIX::AddThreadForInitialStopIfNeeded(lldb::tid_t stop_tid)
502{
503 bool added_to_set = false;
504 ThreadStopSet::iterator it = m_seen_initial_stop.find(stop_tid);
505 if (it == m_seen_initial_stop.end())
506 {
507 m_seen_initial_stop.insert(stop_tid);
508 added_to_set = true;
509 }
510 return added_to_set;
511}
512
Andrew Kaylord4d54992013-09-17 00:30:24 +0000513bool
514ProcessPOSIX::WaitingForInitialStop(lldb::tid_t stop_tid)
515{
516 return (m_seen_initial_stop.find(stop_tid) == m_seen_initial_stop.end());
517}
518
Michael Sartain9f822cd2013-07-31 23:27:46 +0000519POSIXThread *
520ProcessPOSIX::CreateNewPOSIXThread(lldb_private::Process &process, lldb::tid_t tid)
521{
522 return new POSIXThread(process, tid);
523}
524
Johnny Chen9ed5b492012-01-05 21:48:15 +0000525void
526ProcessPOSIX::RefreshStateAfterStop()
527{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000528 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000529 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000530 log->Printf ("ProcessPOSIX::%s(), message_queue size = %d", __FUNCTION__, (int)m_message_queue.size());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000531
532 Mutex::Locker lock(m_message_mutex);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000533
Andrew Kaylor93132f52013-05-28 23:04:25 +0000534 // This method used to only handle one message. Changing it to loop allows
535 // it to handle the case where we hit a breakpoint while handling a different
536 // breakpoint.
537 while (!m_message_queue.empty())
538 {
539 ProcessMessage &message = m_message_queue.front();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000540
Andrew Kaylor93132f52013-05-28 23:04:25 +0000541 // Resolve the thread this message corresponds to and pass it along.
542 lldb::tid_t tid = message.GetTID();
543 if (log)
544 log->Printf ("ProcessPOSIX::%s(), message_queue size = %d, pid = %" PRIi64, __FUNCTION__, (int)m_message_queue.size(), tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000545
Andrew Kaylor93132f52013-05-28 23:04:25 +0000546 if (message.GetKind() == ProcessMessage::eNewThreadMessage)
547 {
548 if (log)
549 log->Printf ("ProcessPOSIX::%s() adding thread, tid = %" PRIi64, __FUNCTION__, message.GetChildTID());
Matt Kopecfb6ab542013-07-10 20:53:11 +0000550 lldb::tid_t child_tid = message.GetChildTID();
Andrew Kaylor93132f52013-05-28 23:04:25 +0000551 ThreadSP thread_sp;
Michael Sartain9f822cd2013-07-31 23:27:46 +0000552 thread_sp.reset(CreateNewPOSIXThread(*this, child_tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +0000553
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000554 Mutex::Locker lock(m_thread_list.GetMutex());
555
Andrew Kaylor93132f52013-05-28 23:04:25 +0000556 m_thread_list.AddThread(thread_sp);
557 }
558
559 m_thread_list.RefreshStateAfterStop();
560
Ed Maste685fea92013-08-29 20:40:11 +0000561 POSIXThread *thread = static_cast<POSIXThread*>(
562 GetThreadList().FindThreadByID(tid, false).get());
Andrew Kaylor93132f52013-05-28 23:04:25 +0000563 if (thread)
564 thread->Notify(message);
565
566 if (message.GetKind() == ProcessMessage::eExitMessage)
567 {
568 // FIXME: We should tell the user about this, but the limbo message is probably better for that.
569 if (log)
570 log->Printf ("ProcessPOSIX::%s() removing thread, tid = %" PRIi64, __FUNCTION__, tid);
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000571
572 Mutex::Locker lock(m_thread_list.GetMutex());
573
Andrew Kaylor93132f52013-05-28 23:04:25 +0000574 ThreadSP thread_sp = m_thread_list.RemoveThreadByID(tid, false);
575 thread_sp.reset();
Matt Kopecb2910442013-07-09 15:09:45 +0000576 m_seen_initial_stop.erase(tid);
Andrew Kaylor93132f52013-05-28 23:04:25 +0000577 }
578
579 m_message_queue.pop();
Matt Kopec650648f2013-01-08 16:30:18 +0000580 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000581}
582
583bool
584ProcessPOSIX::IsAlive()
585{
586 StateType state = GetPrivateState();
Daniel Malea335bf6f2013-04-01 19:48:37 +0000587 return state != eStateDetached
588 && state != eStateExited
589 && state != eStateInvalid
590 && state != eStateUnloaded;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000591}
592
593size_t
594ProcessPOSIX::DoReadMemory(addr_t vm_addr,
595 void *buf, size_t size, Error &error)
596{
597 assert(m_monitor);
598 return m_monitor->ReadMemory(vm_addr, buf, size, error);
599}
600
601size_t
602ProcessPOSIX::DoWriteMemory(addr_t vm_addr, const void *buf, size_t size,
603 Error &error)
604{
605 assert(m_monitor);
606 return m_monitor->WriteMemory(vm_addr, buf, size, error);
607}
608
609addr_t
610ProcessPOSIX::DoAllocateMemory(size_t size, uint32_t permissions,
611 Error &error)
612{
613 addr_t allocated_addr = LLDB_INVALID_ADDRESS;
614
615 unsigned prot = 0;
616 if (permissions & lldb::ePermissionsReadable)
617 prot |= eMmapProtRead;
618 if (permissions & lldb::ePermissionsWritable)
619 prot |= eMmapProtWrite;
620 if (permissions & lldb::ePermissionsExecutable)
621 prot |= eMmapProtExec;
622
623 if (InferiorCallMmap(this, allocated_addr, 0, size, prot,
624 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) {
625 m_addr_to_mmap_size[allocated_addr] = size;
626 error.Clear();
627 } else {
628 allocated_addr = LLDB_INVALID_ADDRESS;
629 error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions));
630 }
631
632 return allocated_addr;
633}
634
635Error
636ProcessPOSIX::DoDeallocateMemory(lldb::addr_t addr)
637{
638 Error error;
639 MMapMap::iterator pos = m_addr_to_mmap_size.find(addr);
640 if (pos != m_addr_to_mmap_size.end() &&
641 InferiorCallMunmap(this, addr, pos->second))
642 m_addr_to_mmap_size.erase (pos);
643 else
Daniel Malead01b2952012-11-29 21:49:15 +0000644 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000645
646 return error;
647}
648
649size_t
650ProcessPOSIX::GetSoftwareBreakpointTrapOpcode(BreakpointSite* bp_site)
651{
Todd Fiala1d6082f2014-08-27 16:32:02 +0000652 static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xD4 };
Johnny Chen9ed5b492012-01-05 21:48:15 +0000653 static const uint8_t g_i386_opcode[] = { 0xCC };
654
655 ArchSpec arch = GetTarget().GetArchitecture();
656 const uint8_t *opcode = NULL;
657 size_t opcode_size = 0;
658
Greg Clayton906e9ac2014-03-20 21:31:55 +0000659 switch (arch.GetMachine())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000660 {
661 default:
662 assert(false && "CPU type not supported!");
663 break;
664
Todd Fiala1d6082f2014-08-27 16:32:02 +0000665 case llvm::Triple::aarch64:
666 opcode = g_aarch64_opcode;
667 opcode_size = sizeof(g_aarch64_opcode);
668 break;
669
Greg Clayton906e9ac2014-03-20 21:31:55 +0000670 case llvm::Triple::x86:
671 case llvm::Triple::x86_64:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000672 opcode = g_i386_opcode;
673 opcode_size = sizeof(g_i386_opcode);
674 break;
675 }
676
677 bp_site->SetTrapOpcode(opcode, opcode_size);
678 return opcode_size;
679}
680
681Error
Daniel Maleab7eec012013-02-15 20:23:25 +0000682ProcessPOSIX::EnableBreakpointSite(BreakpointSite *bp_site)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000683{
684 return EnableSoftwareBreakpoint(bp_site);
685}
686
687Error
Daniel Maleab7eec012013-02-15 20:23:25 +0000688ProcessPOSIX::DisableBreakpointSite(BreakpointSite *bp_site)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000689{
690 return DisableSoftwareBreakpoint(bp_site);
691}
692
Matt Kopece9ea0da2013-05-07 19:29:28 +0000693Error
694ProcessPOSIX::EnableWatchpoint(Watchpoint *wp, bool notify)
695{
696 Error error;
697 if (wp)
698 {
699 user_id_t watchID = wp->GetID();
700 addr_t addr = wp->GetLoadAddress();
701 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
702 if (log)
703 log->Printf ("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64 ")",
704 watchID);
705 if (wp->IsEnabled())
706 {
707 if (log)
708 log->Printf("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64
709 ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.",
710 watchID, (uint64_t)addr);
711 return error;
712 }
713
Matt Kopec6f961232013-06-03 17:40:20 +0000714 // Try to find a vacant watchpoint slot in the inferiors' main thread
715 uint32_t wp_hw_index = LLDB_INVALID_INDEX32;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000716 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopec6f961232013-06-03 17:40:20 +0000717 POSIXThread *thread = static_cast<POSIXThread*>(
718 m_thread_list.GetThreadAtIndex(0, false).get());
719
720 if (thread)
721 wp_hw_index = thread->FindVacantWatchpointIndex();
722
723 if (wp_hw_index == LLDB_INVALID_INDEX32)
Matt Kopece9ea0da2013-05-07 19:29:28 +0000724 {
Matt Kopec6f961232013-06-03 17:40:20 +0000725 error.SetErrorString("Setting hardware watchpoint failed.");
Matt Kopece9ea0da2013-05-07 19:29:28 +0000726 }
727 else
728 {
Matt Kopec6f961232013-06-03 17:40:20 +0000729 wp->SetHardwareIndex(wp_hw_index);
730 bool wp_enabled = true;
731 uint32_t thread_count = m_thread_list.GetSize(false);
732 for (uint32_t i = 0; i < thread_count; ++i)
733 {
734 thread = static_cast<POSIXThread*>(
735 m_thread_list.GetThreadAtIndex(i, false).get());
736 if (thread)
737 wp_enabled &= thread->EnableHardwareWatchpoint(wp);
738 else
739 wp_enabled = false;
740 }
741 if (wp_enabled)
742 {
743 wp->SetEnabled(true, notify);
744 return error;
745 }
746 else
747 {
748 // Watchpoint enabling failed on at least one
749 // of the threads so roll back all of them
750 DisableWatchpoint(wp, false);
751 error.SetErrorString("Setting hardware watchpoint failed");
752 }
Matt Kopece9ea0da2013-05-07 19:29:28 +0000753 }
754 }
755 else
756 error.SetErrorString("Watchpoint argument was NULL.");
757 return error;
758}
759
760Error
761ProcessPOSIX::DisableWatchpoint(Watchpoint *wp, bool notify)
762{
763 Error error;
764 if (wp)
765 {
766 user_id_t watchID = wp->GetID();
767 addr_t addr = wp->GetLoadAddress();
768 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
769 if (log)
770 log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64 ")",
771 watchID);
772 if (!wp->IsEnabled())
773 {
774 if (log)
775 log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64
776 ") addr = 0x%8.8" PRIx64 ": watchpoint already disabled.",
777 watchID, (uint64_t)addr);
778 // This is needed (for now) to keep watchpoints disabled correctly
779 wp->SetEnabled(false, notify);
780 return error;
781 }
782
783 if (wp->IsHardware())
784 {
785 bool wp_disabled = true;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000786 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopece9ea0da2013-05-07 19:29:28 +0000787 uint32_t thread_count = m_thread_list.GetSize(false);
788 for (uint32_t i = 0; i < thread_count; ++i)
789 {
790 POSIXThread *thread = static_cast<POSIXThread*>(
791 m_thread_list.GetThreadAtIndex(i, false).get());
792 if (thread)
793 wp_disabled &= thread->DisableHardwareWatchpoint(wp);
794 else
795 wp_disabled = false;
796 }
797 if (wp_disabled)
798 {
Matt Kopec6f961232013-06-03 17:40:20 +0000799 wp->SetHardwareIndex(LLDB_INVALID_INDEX32);
Matt Kopece9ea0da2013-05-07 19:29:28 +0000800 wp->SetEnabled(false, notify);
801 return error;
802 }
803 else
804 error.SetErrorString("Disabling hardware watchpoint failed");
805 }
806 }
807 else
808 error.SetErrorString("Watchpoint argument was NULL.");
809 return error;
810}
811
812Error
813ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num)
814{
815 Error error;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000816 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopece9ea0da2013-05-07 19:29:28 +0000817 POSIXThread *thread = static_cast<POSIXThread*>(
818 m_thread_list.GetThreadAtIndex(0, false).get());
819 if (thread)
820 num = thread->NumSupportedHardwareWatchpoints();
821 else
822 error.SetErrorString("Process does not exist.");
823 return error;
824}
825
826Error
827ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num, bool &after)
828{
829 Error error = GetWatchpointSupportInfo(num);
830 // Watchpoints trigger and halt the inferior after
831 // the corresponding instruction has been executed.
832 after = true;
833 return error;
834}
835
Johnny Chen9ed5b492012-01-05 21:48:15 +0000836uint32_t
837ProcessPOSIX::UpdateThreadListIfNeeded()
838{
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000839 Mutex::Locker lock(m_thread_list.GetMutex());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000840 // Do not allow recursive updates.
841 return m_thread_list.GetSize(false);
842}
843
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000844bool
Johnny Chen9ed5b492012-01-05 21:48:15 +0000845ProcessPOSIX::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list)
846{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000847 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000848 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000849 log->Printf ("ProcessPOSIX::%s() (pid = %" PRIi64 ")", __FUNCTION__, GetID());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000850
Daniel Maleae0f8f572013-08-26 23:57:52 +0000851 bool has_updated = false;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000852 // Update the process thread list with this new thread.
853 // FIXME: We should be using tid, not pid.
854 assert(m_monitor);
855 ThreadSP thread_sp (old_thread_list.FindThreadByID (GetID(), false));
Greg Clayton0c90ef42012-02-21 18:40:07 +0000856 if (!thread_sp) {
Michael Sartain9f822cd2013-07-31 23:27:46 +0000857 thread_sp.reset(CreateNewPOSIXThread(*this, GetID()));
Daniel Maleae0f8f572013-08-26 23:57:52 +0000858 has_updated = true;
Greg Clayton0c90ef42012-02-21 18:40:07 +0000859 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000860
861 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000862 log->Printf ("ProcessPOSIX::%s() updated pid = %" PRIi64, __FUNCTION__, GetID());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000863 new_thread_list.AddThread(thread_sp);
864
Daniel Maleae0f8f572013-08-26 23:57:52 +0000865 return has_updated; // the list has been updated
Johnny Chen9ed5b492012-01-05 21:48:15 +0000866}
867
868ByteOrder
869ProcessPOSIX::GetByteOrder() const
870{
871 // FIXME: We should be able to extract this value directly. See comment in
872 // ProcessPOSIX().
873 return m_byte_order;
874}
875
876size_t
877ProcessPOSIX::PutSTDIN(const char *buf, size_t len, Error &error)
878{
879 ssize_t status;
880 if ((status = write(m_monitor->GetTerminalFD(), buf, len)) < 0)
881 {
882 error.SetErrorToErrno();
883 return 0;
884 }
885 return status;
886}
887
Johnny Chen9ed5b492012-01-05 21:48:15 +0000888//------------------------------------------------------------------------------
889// Utility functions.
890
891bool
892ProcessPOSIX::HasExited()
893{
894 switch (GetPrivateState())
895 {
896 default:
897 break;
898
899 case eStateDetached:
900 case eStateExited:
901 return true;
902 }
903
904 return false;
905}
906
907bool
908ProcessPOSIX::IsStopped()
909{
910 switch (GetPrivateState())
911 {
912 default:
913 break;
914
915 case eStateStopped:
916 case eStateCrashed:
917 case eStateSuspended:
918 return true;
919 }
920
921 return false;
922}
Matt Kopecb2910442013-07-09 15:09:45 +0000923
924bool
925ProcessPOSIX::IsAThreadRunning()
926{
927 bool is_running = false;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000928 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopecb2910442013-07-09 15:09:45 +0000929 uint32_t thread_count = m_thread_list.GetSize(false);
930 for (uint32_t i = 0; i < thread_count; ++i)
931 {
932 POSIXThread *thread = static_cast<POSIXThread*>(
933 m_thread_list.GetThreadAtIndex(i, false).get());
934 StateType thread_state = thread->GetState();
935 if (thread_state == eStateRunning || thread_state == eStateStepping)
936 {
937 is_running = true;
938 break;
939 }
940 }
941 return is_running;
942}
Todd Fialaaf245d12014-06-30 21:05:18 +0000943
944const DataBufferSP
945ProcessPOSIX::GetAuxvData ()
946{
947 // If we're the local platform, we can ask the host for auxv data.
948 PlatformSP platform_sp = m_target.GetPlatform ();
949 if (platform_sp && platform_sp->IsHost ())
950 return lldb_private::Host::GetAuxvData(this);
951
952 // Somewhat unexpected - the process is not running locally or we don't have a platform.
953 assert (false && "no platform or not the host - how did we get here with ProcessPOSIX?");
954 return DataBufferSP ();
955}