blob: 2ce61e94d85c2c2fba25615cb5db58d01cd32942 [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"
Johnny Chen9ed5b492012-01-05 21:48:15 +000019#include "lldb/Core/PluginManager.h"
20#include "lldb/Core/State.h"
Daniel Malea6217d2a2013-01-08 14:49:22 +000021#include "lldb/Host/FileSpec.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000022#include "lldb/Host/Host.h"
23#include "lldb/Symbol/ObjectFile.h"
24#include "lldb/Target/DynamicLoader.h"
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +000025#include "lldb/Target/Platform.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000026#include "lldb/Target/Target.h"
27
28#include "ProcessPOSIX.h"
29#include "ProcessPOSIXLog.h"
30#include "Plugins/Process/Utility/InferiorCallPOSIX.h"
31#include "ProcessMonitor.h"
32#include "POSIXThread.h"
33
34using namespace lldb;
35using namespace lldb_private;
36
37//------------------------------------------------------------------------------
38// Static functions.
39#if 0
40Process*
41ProcessPOSIX::CreateInstance(Target& target, Listener &listener)
42{
43 return new ProcessPOSIX(target, listener);
44}
45
46
47void
48ProcessPOSIX::Initialize()
49{
50 static bool g_initialized = false;
51
52 if (!g_initialized)
53 {
54 g_initialized = true;
55 PluginManager::RegisterPlugin(GetPluginNameStatic(),
56 GetPluginDescriptionStatic(),
57 CreateInstance);
58
59 Log::Callbacks log_callbacks = {
60 ProcessPOSIXLog::DisableLog,
61 ProcessPOSIXLog::EnableLog,
62 ProcessPOSIXLog::ListLogCategories
63 };
64
65 Log::RegisterLogChannel (ProcessPOSIX::GetPluginNameStatic(), log_callbacks);
66 }
67}
68#endif
69
70//------------------------------------------------------------------------------
71// Constructors and destructors.
72
73ProcessPOSIX::ProcessPOSIX(Target& target, Listener &listener)
74 : Process(target, listener),
Johnny Chen7b9f93a2012-04-14 00:54:42 +000075 m_byte_order(lldb::endian::InlHostByteOrder()),
Johnny Chen9ed5b492012-01-05 21:48:15 +000076 m_monitor(NULL),
77 m_module(NULL),
Andrew Kaylor93132f52013-05-28 23:04:25 +000078 m_message_mutex (Mutex::eMutexTypeRecursive),
Matt Kopecb2910442013-07-09 15:09:45 +000079 m_exit_now(false),
80 m_seen_initial_stop()
Johnny Chen9ed5b492012-01-05 21:48:15 +000081{
82 // FIXME: Putting this code in the ctor and saving the byte order in a
83 // member variable is a hack to avoid const qual issues in GetByteOrder.
Johnny Chen7b9f93a2012-04-14 00:54:42 +000084 lldb::ModuleSP module = GetTarget().GetExecutableModule();
Michael Sartaina7499c92013-07-01 19:45:50 +000085 if (module && module->GetObjectFile())
Johnny Chen7b9f93a2012-04-14 00:54:42 +000086 m_byte_order = module->GetObjectFile()->GetByteOrder();
Johnny Chen9ed5b492012-01-05 21:48:15 +000087}
88
89ProcessPOSIX::~ProcessPOSIX()
90{
91 delete m_monitor;
92}
93
94//------------------------------------------------------------------------------
95// Process protocol.
Andrew Kaylorbc68b432013-07-10 21:57:27 +000096void
97ProcessPOSIX::Finalize()
98{
99 Process::Finalize();
100
101 if (m_monitor)
102 m_monitor->StopMonitor();
103}
Johnny Chen9ed5b492012-01-05 21:48:15 +0000104
105bool
106ProcessPOSIX::CanDebug(Target &target, bool plugin_specified_by_name)
107{
108 // For now we are just making sure the file exists for a given module
109 ModuleSP exe_module_sp(target.GetExecutableModule());
110 if (exe_module_sp.get())
111 return exe_module_sp->GetFileSpec().Exists();
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000112 // If there is no executable module, we return true since we might be preparing to attach.
113 return true;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000114}
115
116Error
117ProcessPOSIX::DoAttachToProcessWithID(lldb::pid_t pid)
118{
119 Error error;
120 assert(m_monitor == NULL);
121
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000122 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000123 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000124 log->Printf ("ProcessPOSIX::%s(pid = %" PRIi64 ")", __FUNCTION__, GetID());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000125
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000126 m_monitor = new ProcessMonitor(this, pid, error);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000127
128 if (!error.Success())
129 return error;
130
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000131 PlatformSP platform_sp (m_target.GetPlatform ());
132 assert (platform_sp.get());
133 if (!platform_sp)
134 return error; // FIXME: Detatch?
135
136 // Find out what we can about this process
137 ProcessInstanceInfo process_info;
138 platform_sp->GetProcessInfo (pid, process_info);
139
140 // Resolve the executable module
141 ModuleSP exe_module_sp;
142 FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths());
143 error = platform_sp->ResolveExecutable(process_info.GetExecutableFile(),
144 m_target.GetArchitecture(),
145 exe_module_sp,
146 executable_search_paths.GetSize() ? &executable_search_paths : NULL);
Ed Mastebe0b55d2013-07-04 18:25:34 +0000147 if (!error.Success())
148 return error;
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000149
150 // Fix the target architecture if necessary
151 const ArchSpec &module_arch = exe_module_sp->GetArchitecture();
152 if (module_arch.IsValid() && !m_target.GetArchitecture().IsExactMatch(module_arch))
153 m_target.SetArchitecture(module_arch);
154
155 // Initialize the target module list
156 m_target.SetExecutableModule (exe_module_sp, true);
157
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000158 SetSTDIOFileDescriptor(m_monitor->GetTerminalFD());
159
Johnny Chen9ed5b492012-01-05 21:48:15 +0000160 SetID(pid);
Andrew Kaylorbf9b4c12013-05-07 22:46:38 +0000161
Johnny Chen9ed5b492012-01-05 21:48:15 +0000162 return error;
163}
164
165Error
Greg Claytone2186ed2012-09-07 17:51:47 +0000166ProcessPOSIX::DoAttachToProcessWithID (lldb::pid_t pid, const ProcessAttachInfo &attach_info)
167{
168 return DoAttachToProcessWithID(pid);
169}
170
171Error
Johnny Chen9ed5b492012-01-05 21:48:15 +0000172ProcessPOSIX::WillLaunch(Module* module)
173{
174 Error error;
175 return error;
176}
177
178const char *
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000179ProcessPOSIX::GetFilePath(const lldb_private::FileAction *file_action, const char *default_path)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000180{
181 const char *pts_name = "/dev/pts/";
182 const char *path = NULL;
183
184 if (file_action)
185 {
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000186 if (file_action->GetAction() == FileAction::eFileActionOpen)
Saleem Abdulrasool03700de2014-03-08 20:47:03 +0000187 {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000188 path = file_action->GetPath();
189 // By default the stdio paths passed in will be pseudo-terminal
190 // (/dev/pts). If so, convert to using a different default path
191 // instead to redirect I/O to the debugger console. This should
192 // also handle user overrides to /dev/null or a different file.
Saleem Abdulrasoold41fca12014-03-08 20:47:07 +0000193 if (!path || ::strncmp(path, pts_name, ::strlen(pts_name)) == 0)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000194 path = default_path;
Saleem Abdulrasool03700de2014-03-08 20:47:03 +0000195 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000196 }
197
198 return path;
199}
200
201Error
202ProcessPOSIX::DoLaunch (Module *module,
Jean-Daniel Dupas7782de92013-12-09 22:52:50 +0000203 ProcessLaunchInfo &launch_info)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000204{
205 Error error;
206 assert(m_monitor == NULL);
207
Daniel Malea6217d2a2013-01-08 14:49:22 +0000208 const char* working_dir = launch_info.GetWorkingDirectory();
209 if (working_dir) {
210 FileSpec WorkingDir(working_dir, true);
211 if (!WorkingDir || WorkingDir.GetFileType() != FileSpec::eFileTypeDirectory)
212 {
213 error.SetErrorStringWithFormat("No such file or directory: %s", working_dir);
214 return error;
215 }
216 }
217
Johnny Chen9ed5b492012-01-05 21:48:15 +0000218 SetPrivateState(eStateLaunching);
219
Zachary Turner696b5282014-08-14 16:01:25 +0000220 const lldb_private::FileAction *file_action;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000221
222 // Default of NULL will mean to use existing open file descriptors
223 const char *stdin_path = NULL;
224 const char *stdout_path = NULL;
225 const char *stderr_path = NULL;
Daniel Malea6217d2a2013-01-08 14:49:22 +0000226
Johnny Chen9ed5b492012-01-05 21:48:15 +0000227 file_action = launch_info.GetFileActionForFD (STDIN_FILENO);
228 stdin_path = GetFilePath(file_action, stdin_path);
229
230 file_action = launch_info.GetFileActionForFD (STDOUT_FILENO);
231 stdout_path = GetFilePath(file_action, stdout_path);
232
233 file_action = launch_info.GetFileActionForFD (STDERR_FILENO);
234 stderr_path = GetFilePath(file_action, stderr_path);
235
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000236 m_monitor = new ProcessMonitor (this,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000237 module,
238 launch_info.GetArguments().GetConstArgumentVector(),
239 launch_info.GetEnvironmentEntries().GetConstArgumentVector(),
240 stdin_path,
241 stdout_path,
242 stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000243 working_dir,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000244 error);
245
246 m_module = module;
247
248 if (!error.Success())
249 return error;
250
Matt Kopec9eb40a92013-03-14 21:35:26 +0000251 SetSTDIOFileDescriptor(m_monitor->GetTerminalFD());
252
Johnny Chen9ed5b492012-01-05 21:48:15 +0000253 SetID(m_monitor->GetPID());
254 return error;
255}
256
257void
258ProcessPOSIX::DidLaunch()
259{
260}
261
Ed Maste30df85e2013-12-11 20:43:27 +0000262Error
263ProcessPOSIX::DoResume()
264{
265 StateType state = GetPrivateState();
266
267 assert(state == eStateStopped);
268
269 SetPrivateState(eStateRunning);
270
271 bool did_resume = false;
272
273 Mutex::Locker lock(m_thread_list.GetMutex());
274
275 uint32_t thread_count = m_thread_list.GetSize(false);
276 for (uint32_t i = 0; i < thread_count; ++i)
277 {
278 POSIXThread *thread = static_cast<POSIXThread*>(
279 m_thread_list.GetThreadAtIndex(i, false).get());
280 did_resume = thread->Resume() || did_resume;
281 }
282 assert(did_resume && "Process resume failed!");
283
284 return Error();
285}
286
Johnny Chen9ed5b492012-01-05 21:48:15 +0000287addr_t
288ProcessPOSIX::GetImageInfoAddress()
289{
290 Target *target = &GetTarget();
291 ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
Ed Maste54803652013-10-11 17:39:07 +0000292 Address addr = obj_file->GetImageInfoAddress(target);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000293
Ed Maste04a8bab2013-10-11 01:16:08 +0000294 if (addr.IsValid())
Ed Maste54803652013-10-11 17:39:07 +0000295 return addr.GetLoadAddress(target);
Ed Maste04a8bab2013-10-11 01:16:08 +0000296 return LLDB_INVALID_ADDRESS;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000297}
298
299Error
300ProcessPOSIX::DoHalt(bool &caused_stop)
301{
302 Error error;
303
304 if (IsStopped())
305 {
306 caused_stop = false;
307 }
308 else if (kill(GetID(), SIGSTOP))
309 {
310 caused_stop = false;
311 error.SetErrorToErrno();
312 }
313 else
314 {
315 caused_stop = true;
316 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000317 return error;
318}
319
320Error
Johnny Chen9ed5b492012-01-05 21:48:15 +0000321ProcessPOSIX::DoSignal(int signal)
322{
323 Error error;
324
325 if (kill(GetID(), signal))
326 error.SetErrorToErrno();
327
328 return error;
329}
330
331Error
332ProcessPOSIX::DoDestroy()
333{
334 Error error;
335
336 if (!HasExited())
337 {
Ed Maste70882932014-04-01 14:30:56 +0000338 assert(m_monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000339 m_exit_now = true;
Ed Maste70882932014-04-01 14:30:56 +0000340 if (!m_monitor->Kill())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000341 {
342 error.SetErrorToErrno();
343 return error;
344 }
345
346 SetPrivateState(eStateExited);
347 }
348
349 return error;
350}
351
352void
Matt Kopec718be872013-10-09 19:39:55 +0000353ProcessPOSIX::DoDidExec()
354{
355 Target *target = &GetTarget();
356 if (target)
357 {
358 PlatformSP platform_sp (target->GetPlatform());
359 assert (platform_sp.get());
360 if (platform_sp)
361 {
362 ProcessInstanceInfo process_info;
363 platform_sp->GetProcessInfo(GetID(), process_info);
364 ModuleSP exe_module_sp;
365 FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths());
366 Error error = platform_sp->ResolveExecutable(process_info.GetExecutableFile(),
367 target->GetArchitecture(),
368 exe_module_sp,
369 executable_search_paths.GetSize() ? &executable_search_paths : NULL);
370 if (!error.Success())
371 return;
372 target->SetExecutableModule(exe_module_sp, true);
373 }
374 }
375}
376
Ed Maste30df85e2013-12-11 20:43:27 +0000377void
378ProcessPOSIX::SendMessage(const ProcessMessage &message)
379{
Andrew MacPherson82aae0d2014-04-02 06:57:45 +0000380 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
381
Ed Maste30df85e2013-12-11 20:43:27 +0000382 Mutex::Locker lock(m_message_mutex);
383
384 Mutex::Locker thread_lock(m_thread_list.GetMutex());
385
386 POSIXThread *thread = static_cast<POSIXThread*>(
387 m_thread_list.FindThreadByID(message.GetTID(), false).get());
388
389 switch (message.GetKind())
390 {
391 case ProcessMessage::eInvalidMessage:
392 return;
393
394 case ProcessMessage::eAttachMessage:
395 SetPrivateState(eStateStopped);
396 return;
397
398 case ProcessMessage::eLimboMessage:
399 assert(thread);
400 thread->SetState(eStateStopped);
401 if (message.GetTID() == GetID())
402 {
403 m_exit_status = message.GetExitStatus();
404 if (m_exit_now)
405 {
406 SetPrivateState(eStateExited);
407 m_monitor->Detach(GetID());
408 }
409 else
410 {
411 StopAllThreads(message.GetTID());
412 SetPrivateState(eStateStopped);
413 }
414 }
415 else
416 {
417 StopAllThreads(message.GetTID());
418 SetPrivateState(eStateStopped);
419 }
420 break;
421
422 case ProcessMessage::eExitMessage:
Andrew MacPherson82aae0d2014-04-02 06:57:45 +0000423 if (thread != nullptr)
424 thread->SetState(eStateExited);
425 else
426 {
427 if (log)
428 log->Warning ("ProcessPOSIX::%s eExitMessage for TID %" PRIu64 " failed to find a thread to mark as eStateExited, ignoring", __FUNCTION__, message.GetTID ());
429 }
430
Ed Maste30df85e2013-12-11 20:43:27 +0000431 // FIXME: I'm not sure we need to do this.
432 if (message.GetTID() == GetID())
433 {
434 m_exit_status = message.GetExitStatus();
435 SetExitStatus(m_exit_status, NULL);
436 }
437 else if (!IsAThreadRunning())
438 SetPrivateState(eStateStopped);
439 break;
440
441 case ProcessMessage::eSignalMessage:
442 case ProcessMessage::eSignalDeliveredMessage:
443 if (message.GetSignal() == SIGSTOP &&
444 AddThreadForInitialStopIfNeeded(message.GetTID()))
445 return;
446 // Intentional fall-through
447
448 case ProcessMessage::eBreakpointMessage:
449 case ProcessMessage::eTraceMessage:
450 case ProcessMessage::eWatchpointMessage:
451 case ProcessMessage::eCrashMessage:
452 assert(thread);
453 thread->SetState(eStateStopped);
454 StopAllThreads(message.GetTID());
455 SetPrivateState(eStateStopped);
456 break;
457
458 case ProcessMessage::eNewThreadMessage:
459 {
460 lldb::tid_t new_tid = message.GetChildTID();
461 if (WaitingForInitialStop(new_tid))
462 {
463 m_monitor->WaitForInitialTIDStop(new_tid);
464 }
465 assert(thread);
466 thread->SetState(eStateStopped);
467 StopAllThreads(message.GetTID());
468 SetPrivateState(eStateStopped);
469 break;
470 }
471
472 case ProcessMessage::eExecMessage:
473 {
474 assert(thread);
475 thread->SetState(eStateStopped);
476 StopAllThreads(message.GetTID());
477 SetPrivateState(eStateStopped);
478 break;
479 }
480 }
481
482
483 m_message_queue.push(message);
484}
485
Andrew Kaylor93132f52013-05-28 23:04:25 +0000486void
487ProcessPOSIX::StopAllThreads(lldb::tid_t stop_tid)
488{
489 // FIXME: Will this work the same way on FreeBSD and Linux?
490}
491
Matt Kopecb2910442013-07-09 15:09:45 +0000492bool
493ProcessPOSIX::AddThreadForInitialStopIfNeeded(lldb::tid_t stop_tid)
494{
495 bool added_to_set = false;
496 ThreadStopSet::iterator it = m_seen_initial_stop.find(stop_tid);
497 if (it == m_seen_initial_stop.end())
498 {
499 m_seen_initial_stop.insert(stop_tid);
500 added_to_set = true;
501 }
502 return added_to_set;
503}
504
Andrew Kaylord4d54992013-09-17 00:30:24 +0000505bool
506ProcessPOSIX::WaitingForInitialStop(lldb::tid_t stop_tid)
507{
508 return (m_seen_initial_stop.find(stop_tid) == m_seen_initial_stop.end());
509}
510
Michael Sartain9f822cd2013-07-31 23:27:46 +0000511POSIXThread *
512ProcessPOSIX::CreateNewPOSIXThread(lldb_private::Process &process, lldb::tid_t tid)
513{
514 return new POSIXThread(process, tid);
515}
516
Johnny Chen9ed5b492012-01-05 21:48:15 +0000517void
518ProcessPOSIX::RefreshStateAfterStop()
519{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000520 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000521 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000522 log->Printf ("ProcessPOSIX::%s(), message_queue size = %d", __FUNCTION__, (int)m_message_queue.size());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000523
524 Mutex::Locker lock(m_message_mutex);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000525
Andrew Kaylor93132f52013-05-28 23:04:25 +0000526 // This method used to only handle one message. Changing it to loop allows
527 // it to handle the case where we hit a breakpoint while handling a different
528 // breakpoint.
529 while (!m_message_queue.empty())
530 {
531 ProcessMessage &message = m_message_queue.front();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000532
Andrew Kaylor93132f52013-05-28 23:04:25 +0000533 // Resolve the thread this message corresponds to and pass it along.
534 lldb::tid_t tid = message.GetTID();
535 if (log)
536 log->Printf ("ProcessPOSIX::%s(), message_queue size = %d, pid = %" PRIi64, __FUNCTION__, (int)m_message_queue.size(), tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000537
Andrew Kaylor93132f52013-05-28 23:04:25 +0000538 if (message.GetKind() == ProcessMessage::eNewThreadMessage)
539 {
540 if (log)
541 log->Printf ("ProcessPOSIX::%s() adding thread, tid = %" PRIi64, __FUNCTION__, message.GetChildTID());
Matt Kopecfb6ab542013-07-10 20:53:11 +0000542 lldb::tid_t child_tid = message.GetChildTID();
Andrew Kaylor93132f52013-05-28 23:04:25 +0000543 ThreadSP thread_sp;
Michael Sartain9f822cd2013-07-31 23:27:46 +0000544 thread_sp.reset(CreateNewPOSIXThread(*this, child_tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +0000545
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000546 Mutex::Locker lock(m_thread_list.GetMutex());
547
Andrew Kaylor93132f52013-05-28 23:04:25 +0000548 m_thread_list.AddThread(thread_sp);
549 }
550
551 m_thread_list.RefreshStateAfterStop();
552
Ed Maste685fea92013-08-29 20:40:11 +0000553 POSIXThread *thread = static_cast<POSIXThread*>(
554 GetThreadList().FindThreadByID(tid, false).get());
Andrew Kaylor93132f52013-05-28 23:04:25 +0000555 if (thread)
556 thread->Notify(message);
557
558 if (message.GetKind() == ProcessMessage::eExitMessage)
559 {
560 // FIXME: We should tell the user about this, but the limbo message is probably better for that.
561 if (log)
562 log->Printf ("ProcessPOSIX::%s() removing thread, tid = %" PRIi64, __FUNCTION__, tid);
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000563
564 Mutex::Locker lock(m_thread_list.GetMutex());
565
Andrew Kaylor93132f52013-05-28 23:04:25 +0000566 ThreadSP thread_sp = m_thread_list.RemoveThreadByID(tid, false);
567 thread_sp.reset();
Matt Kopecb2910442013-07-09 15:09:45 +0000568 m_seen_initial_stop.erase(tid);
Andrew Kaylor93132f52013-05-28 23:04:25 +0000569 }
570
571 m_message_queue.pop();
Matt Kopec650648f2013-01-08 16:30:18 +0000572 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000573}
574
575bool
576ProcessPOSIX::IsAlive()
577{
578 StateType state = GetPrivateState();
Daniel Malea335bf6f2013-04-01 19:48:37 +0000579 return state != eStateDetached
580 && state != eStateExited
581 && state != eStateInvalid
582 && state != eStateUnloaded;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000583}
584
585size_t
586ProcessPOSIX::DoReadMemory(addr_t vm_addr,
587 void *buf, size_t size, Error &error)
588{
589 assert(m_monitor);
590 return m_monitor->ReadMemory(vm_addr, buf, size, error);
591}
592
593size_t
594ProcessPOSIX::DoWriteMemory(addr_t vm_addr, const void *buf, size_t size,
595 Error &error)
596{
597 assert(m_monitor);
598 return m_monitor->WriteMemory(vm_addr, buf, size, error);
599}
600
601addr_t
602ProcessPOSIX::DoAllocateMemory(size_t size, uint32_t permissions,
603 Error &error)
604{
605 addr_t allocated_addr = LLDB_INVALID_ADDRESS;
606
607 unsigned prot = 0;
608 if (permissions & lldb::ePermissionsReadable)
609 prot |= eMmapProtRead;
610 if (permissions & lldb::ePermissionsWritable)
611 prot |= eMmapProtWrite;
612 if (permissions & lldb::ePermissionsExecutable)
613 prot |= eMmapProtExec;
614
615 if (InferiorCallMmap(this, allocated_addr, 0, size, prot,
616 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) {
617 m_addr_to_mmap_size[allocated_addr] = size;
618 error.Clear();
619 } else {
620 allocated_addr = LLDB_INVALID_ADDRESS;
621 error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions));
622 }
623
624 return allocated_addr;
625}
626
627Error
628ProcessPOSIX::DoDeallocateMemory(lldb::addr_t addr)
629{
630 Error error;
631 MMapMap::iterator pos = m_addr_to_mmap_size.find(addr);
632 if (pos != m_addr_to_mmap_size.end() &&
633 InferiorCallMunmap(this, addr, pos->second))
634 m_addr_to_mmap_size.erase (pos);
635 else
Daniel Malead01b2952012-11-29 21:49:15 +0000636 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000637
638 return error;
639}
640
641size_t
642ProcessPOSIX::GetSoftwareBreakpointTrapOpcode(BreakpointSite* bp_site)
643{
644 static const uint8_t g_i386_opcode[] = { 0xCC };
645
646 ArchSpec arch = GetTarget().GetArchitecture();
647 const uint8_t *opcode = NULL;
648 size_t opcode_size = 0;
649
Greg Clayton906e9ac2014-03-20 21:31:55 +0000650 switch (arch.GetMachine())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000651 {
652 default:
653 assert(false && "CPU type not supported!");
654 break;
655
Greg Clayton906e9ac2014-03-20 21:31:55 +0000656 case llvm::Triple::x86:
657 case llvm::Triple::x86_64:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000658 opcode = g_i386_opcode;
659 opcode_size = sizeof(g_i386_opcode);
660 break;
661 }
662
663 bp_site->SetTrapOpcode(opcode, opcode_size);
664 return opcode_size;
665}
666
667Error
Daniel Maleab7eec012013-02-15 20:23:25 +0000668ProcessPOSIX::EnableBreakpointSite(BreakpointSite *bp_site)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000669{
670 return EnableSoftwareBreakpoint(bp_site);
671}
672
673Error
Daniel Maleab7eec012013-02-15 20:23:25 +0000674ProcessPOSIX::DisableBreakpointSite(BreakpointSite *bp_site)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000675{
676 return DisableSoftwareBreakpoint(bp_site);
677}
678
Matt Kopece9ea0da2013-05-07 19:29:28 +0000679Error
680ProcessPOSIX::EnableWatchpoint(Watchpoint *wp, bool notify)
681{
682 Error error;
683 if (wp)
684 {
685 user_id_t watchID = wp->GetID();
686 addr_t addr = wp->GetLoadAddress();
687 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
688 if (log)
689 log->Printf ("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64 ")",
690 watchID);
691 if (wp->IsEnabled())
692 {
693 if (log)
694 log->Printf("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64
695 ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.",
696 watchID, (uint64_t)addr);
697 return error;
698 }
699
Matt Kopec6f961232013-06-03 17:40:20 +0000700 // Try to find a vacant watchpoint slot in the inferiors' main thread
701 uint32_t wp_hw_index = LLDB_INVALID_INDEX32;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000702 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopec6f961232013-06-03 17:40:20 +0000703 POSIXThread *thread = static_cast<POSIXThread*>(
704 m_thread_list.GetThreadAtIndex(0, false).get());
705
706 if (thread)
707 wp_hw_index = thread->FindVacantWatchpointIndex();
708
709 if (wp_hw_index == LLDB_INVALID_INDEX32)
Matt Kopece9ea0da2013-05-07 19:29:28 +0000710 {
Matt Kopec6f961232013-06-03 17:40:20 +0000711 error.SetErrorString("Setting hardware watchpoint failed.");
Matt Kopece9ea0da2013-05-07 19:29:28 +0000712 }
713 else
714 {
Matt Kopec6f961232013-06-03 17:40:20 +0000715 wp->SetHardwareIndex(wp_hw_index);
716 bool wp_enabled = true;
717 uint32_t thread_count = m_thread_list.GetSize(false);
718 for (uint32_t i = 0; i < thread_count; ++i)
719 {
720 thread = static_cast<POSIXThread*>(
721 m_thread_list.GetThreadAtIndex(i, false).get());
722 if (thread)
723 wp_enabled &= thread->EnableHardwareWatchpoint(wp);
724 else
725 wp_enabled = false;
726 }
727 if (wp_enabled)
728 {
729 wp->SetEnabled(true, notify);
730 return error;
731 }
732 else
733 {
734 // Watchpoint enabling failed on at least one
735 // of the threads so roll back all of them
736 DisableWatchpoint(wp, false);
737 error.SetErrorString("Setting hardware watchpoint failed");
738 }
Matt Kopece9ea0da2013-05-07 19:29:28 +0000739 }
740 }
741 else
742 error.SetErrorString("Watchpoint argument was NULL.");
743 return error;
744}
745
746Error
747ProcessPOSIX::DisableWatchpoint(Watchpoint *wp, bool notify)
748{
749 Error error;
750 if (wp)
751 {
752 user_id_t watchID = wp->GetID();
753 addr_t addr = wp->GetLoadAddress();
754 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
755 if (log)
756 log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64 ")",
757 watchID);
758 if (!wp->IsEnabled())
759 {
760 if (log)
761 log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64
762 ") addr = 0x%8.8" PRIx64 ": watchpoint already disabled.",
763 watchID, (uint64_t)addr);
764 // This is needed (for now) to keep watchpoints disabled correctly
765 wp->SetEnabled(false, notify);
766 return error;
767 }
768
769 if (wp->IsHardware())
770 {
771 bool wp_disabled = true;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000772 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopece9ea0da2013-05-07 19:29:28 +0000773 uint32_t thread_count = m_thread_list.GetSize(false);
774 for (uint32_t i = 0; i < thread_count; ++i)
775 {
776 POSIXThread *thread = static_cast<POSIXThread*>(
777 m_thread_list.GetThreadAtIndex(i, false).get());
778 if (thread)
779 wp_disabled &= thread->DisableHardwareWatchpoint(wp);
780 else
781 wp_disabled = false;
782 }
783 if (wp_disabled)
784 {
Matt Kopec6f961232013-06-03 17:40:20 +0000785 wp->SetHardwareIndex(LLDB_INVALID_INDEX32);
Matt Kopece9ea0da2013-05-07 19:29:28 +0000786 wp->SetEnabled(false, notify);
787 return error;
788 }
789 else
790 error.SetErrorString("Disabling hardware watchpoint failed");
791 }
792 }
793 else
794 error.SetErrorString("Watchpoint argument was NULL.");
795 return error;
796}
797
798Error
799ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num)
800{
801 Error error;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000802 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopece9ea0da2013-05-07 19:29:28 +0000803 POSIXThread *thread = static_cast<POSIXThread*>(
804 m_thread_list.GetThreadAtIndex(0, false).get());
805 if (thread)
806 num = thread->NumSupportedHardwareWatchpoints();
807 else
808 error.SetErrorString("Process does not exist.");
809 return error;
810}
811
812Error
813ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num, bool &after)
814{
815 Error error = GetWatchpointSupportInfo(num);
816 // Watchpoints trigger and halt the inferior after
817 // the corresponding instruction has been executed.
818 after = true;
819 return error;
820}
821
Johnny Chen9ed5b492012-01-05 21:48:15 +0000822uint32_t
823ProcessPOSIX::UpdateThreadListIfNeeded()
824{
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000825 Mutex::Locker lock(m_thread_list.GetMutex());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000826 // Do not allow recursive updates.
827 return m_thread_list.GetSize(false);
828}
829
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000830bool
Johnny Chen9ed5b492012-01-05 21:48:15 +0000831ProcessPOSIX::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list)
832{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000833 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000834 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000835 log->Printf ("ProcessPOSIX::%s() (pid = %" PRIi64 ")", __FUNCTION__, GetID());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000836
Daniel Maleae0f8f572013-08-26 23:57:52 +0000837 bool has_updated = false;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000838 // Update the process thread list with this new thread.
839 // FIXME: We should be using tid, not pid.
840 assert(m_monitor);
841 ThreadSP thread_sp (old_thread_list.FindThreadByID (GetID(), false));
Greg Clayton0c90ef42012-02-21 18:40:07 +0000842 if (!thread_sp) {
Michael Sartain9f822cd2013-07-31 23:27:46 +0000843 thread_sp.reset(CreateNewPOSIXThread(*this, GetID()));
Daniel Maleae0f8f572013-08-26 23:57:52 +0000844 has_updated = true;
Greg Clayton0c90ef42012-02-21 18:40:07 +0000845 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000846
847 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000848 log->Printf ("ProcessPOSIX::%s() updated pid = %" PRIi64, __FUNCTION__, GetID());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000849 new_thread_list.AddThread(thread_sp);
850
Daniel Maleae0f8f572013-08-26 23:57:52 +0000851 return has_updated; // the list has been updated
Johnny Chen9ed5b492012-01-05 21:48:15 +0000852}
853
854ByteOrder
855ProcessPOSIX::GetByteOrder() const
856{
857 // FIXME: We should be able to extract this value directly. See comment in
858 // ProcessPOSIX().
859 return m_byte_order;
860}
861
862size_t
863ProcessPOSIX::PutSTDIN(const char *buf, size_t len, Error &error)
864{
865 ssize_t status;
866 if ((status = write(m_monitor->GetTerminalFD(), buf, len)) < 0)
867 {
868 error.SetErrorToErrno();
869 return 0;
870 }
871 return status;
872}
873
Johnny Chen9ed5b492012-01-05 21:48:15 +0000874UnixSignals &
875ProcessPOSIX::GetUnixSignals()
876{
877 return m_signals;
878}
879
880//------------------------------------------------------------------------------
881// Utility functions.
882
883bool
884ProcessPOSIX::HasExited()
885{
886 switch (GetPrivateState())
887 {
888 default:
889 break;
890
891 case eStateDetached:
892 case eStateExited:
893 return true;
894 }
895
896 return false;
897}
898
899bool
900ProcessPOSIX::IsStopped()
901{
902 switch (GetPrivateState())
903 {
904 default:
905 break;
906
907 case eStateStopped:
908 case eStateCrashed:
909 case eStateSuspended:
910 return true;
911 }
912
913 return false;
914}
Matt Kopecb2910442013-07-09 15:09:45 +0000915
916bool
917ProcessPOSIX::IsAThreadRunning()
918{
919 bool is_running = false;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000920 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopecb2910442013-07-09 15:09:45 +0000921 uint32_t thread_count = m_thread_list.GetSize(false);
922 for (uint32_t i = 0; i < thread_count; ++i)
923 {
924 POSIXThread *thread = static_cast<POSIXThread*>(
925 m_thread_list.GetThreadAtIndex(i, false).get());
926 StateType thread_state = thread->GetState();
927 if (thread_state == eStateRunning || thread_state == eStateStepping)
928 {
929 is_running = true;
930 break;
931 }
932 }
933 return is_running;
934}
Todd Fialaaf245d12014-06-30 21:05:18 +0000935
936const DataBufferSP
937ProcessPOSIX::GetAuxvData ()
938{
939 // If we're the local platform, we can ask the host for auxv data.
940 PlatformSP platform_sp = m_target.GetPlatform ();
941 if (platform_sp && platform_sp->IsHost ())
942 return lldb_private::Host::GetAuxvData(this);
943
944 // Somewhat unexpected - the process is not running locally or we don't have a platform.
945 assert (false && "no platform or not the host - how did we get here with ProcessPOSIX?");
946 return DataBufferSP ();
947}