blob: 1cba5f17cb2ed86273e64e7dc3c52740082243a6 [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 *
179ProcessPOSIX::GetFilePath(
180 const lldb_private::ProcessLaunchInfo::FileAction *file_action,
181 const char *default_path)
182{
183 const char *pts_name = "/dev/pts/";
184 const char *path = NULL;
185
186 if (file_action)
187 {
188 if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen)
189 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.
194 if (::strncmp(path, pts_name, ::strlen(pts_name)) == 0)
195 path = default_path;
196 }
197
198 return path;
199}
200
201Error
202ProcessPOSIX::DoLaunch (Module *module,
203 const ProcessLaunchInfo &launch_info)
204{
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
220 const lldb_private::ProcessLaunchInfo::FileAction *file_action;
221
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
262Error
263ProcessPOSIX::DoResume()
264{
265 StateType state = GetPrivateState();
266
Matt Kopeceb7f2312013-06-26 18:46:08 +0000267 assert(state == eStateStopped);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000268
Matt Kopeceb7f2312013-06-26 18:46:08 +0000269 SetPrivateState(eStateRunning);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000270
271 bool did_resume = false;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000272
273 Mutex::Locker lock(m_thread_list.GetMutex());
274
Johnny Chen9ed5b492012-01-05 21:48:15 +0000275 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
287addr_t
288ProcessPOSIX::GetImageInfoAddress()
289{
290 Target *target = &GetTarget();
291 ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
292 Address addr = obj_file->GetImageInfoAddress();
293
294 if (addr.IsValid())
295 return addr.GetLoadAddress(target);
296 else
297 return LLDB_INVALID_ADDRESS;
298}
299
300Error
301ProcessPOSIX::DoHalt(bool &caused_stop)
302{
303 Error error;
304
305 if (IsStopped())
306 {
307 caused_stop = false;
308 }
309 else if (kill(GetID(), SIGSTOP))
310 {
311 caused_stop = false;
312 error.SetErrorToErrno();
313 }
314 else
315 {
316 caused_stop = true;
317 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000318 return error;
319}
320
321Error
Jim Inghamacff8952013-05-02 00:27:30 +0000322ProcessPOSIX::DoDetach(bool keep_stopped)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000323{
324 Error error;
Jim Inghamacff8952013-05-02 00:27:30 +0000325 if (keep_stopped)
326 {
Ed Maste18757762013-07-03 16:26:34 +0000327 // FIXME: If you want to implement keep_stopped,
Jim Inghamacff8952013-05-02 00:27:30 +0000328 // this would be the place to do it.
Ed Maste18757762013-07-03 16:26:34 +0000329 error.SetErrorString("Detaching with keep_stopped true is not currently supported on this platform.");
Jim Inghamacff8952013-05-02 00:27:30 +0000330 return error;
331 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000332
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000333 Mutex::Locker lock(m_thread_list.GetMutex());
334
Matt Kopec085d6ce2013-05-31 22:00:07 +0000335 uint32_t thread_count = m_thread_list.GetSize(false);
336 for (uint32_t i = 0; i < thread_count; ++i)
337 {
338 POSIXThread *thread = static_cast<POSIXThread*>(
339 m_thread_list.GetThreadAtIndex(i, false).get());
340 error = m_monitor->Detach(thread->GetID());
341 }
342
Johnny Chen9ed5b492012-01-05 21:48:15 +0000343 if (error.Success())
344 SetPrivateState(eStateDetached);
345
346 return error;
347}
348
349Error
350ProcessPOSIX::DoSignal(int signal)
351{
352 Error error;
353
354 if (kill(GetID(), signal))
355 error.SetErrorToErrno();
356
357 return error;
358}
359
360Error
361ProcessPOSIX::DoDestroy()
362{
363 Error error;
364
365 if (!HasExited())
366 {
367 // Drive the exit event to completion (do not keep the inferior in
368 // limbo).
369 m_exit_now = true;
370
Greg Claytonab950c32012-03-30 19:56:32 +0000371 if ((m_monitor == NULL || kill(m_monitor->GetPID(), SIGKILL)) && error.Success())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000372 {
373 error.SetErrorToErrno();
374 return error;
375 }
376
377 SetPrivateState(eStateExited);
378 }
379
380 return error;
381}
382
383void
384ProcessPOSIX::SendMessage(const ProcessMessage &message)
385{
386 Mutex::Locker lock(m_message_mutex);
387
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000388 Mutex::Locker thread_lock(m_thread_list.GetMutex());
389
Andrew Kaylor93132f52013-05-28 23:04:25 +0000390 POSIXThread *thread = static_cast<POSIXThread*>(
391 m_thread_list.FindThreadByID(message.GetTID(), false).get());
392
Johnny Chen9ed5b492012-01-05 21:48:15 +0000393 switch (message.GetKind())
394 {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000395 case ProcessMessage::eInvalidMessage:
396 return;
397
398 case ProcessMessage::eLimboMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +0000399 assert(thread);
400 thread->SetState(eStateStopped);
401 if (message.GetTID() == GetID())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000402 {
Andrew Kaylor93132f52013-05-28 23:04:25 +0000403 m_exit_status = message.GetExitStatus();
404 if (m_exit_now)
405 {
406 SetPrivateState(eStateExited);
Matt Kopec085d6ce2013-05-31 22:00:07 +0000407 m_monitor->Detach(GetID());
Andrew Kaylor93132f52013-05-28 23:04:25 +0000408 }
409 else
410 {
411 StopAllThreads(message.GetTID());
412 SetPrivateState(eStateStopped);
413 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000414 }
415 else
Andrew Kaylor93132f52013-05-28 23:04:25 +0000416 {
417 StopAllThreads(message.GetTID());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000418 SetPrivateState(eStateStopped);
Andrew Kaylor93132f52013-05-28 23:04:25 +0000419 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000420 break;
421
422 case ProcessMessage::eExitMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +0000423 assert(thread);
424 thread->SetState(eStateExited);
425 // FIXME: I'm not sure we need to do this.
426 if (message.GetTID() == GetID())
427 {
428 m_exit_status = message.GetExitStatus();
429 SetExitStatus(m_exit_status, NULL);
430 }
Matt Kopecb2910442013-07-09 15:09:45 +0000431 else if (!IsAThreadRunning())
432 SetPrivateState(eStateStopped);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000433 break;
434
Matt Kopecb2910442013-07-09 15:09:45 +0000435 assert(thread);
436 thread->SetState(eStateStopped);
437 StopAllThreads(message.GetTID());
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
Johnny Chen9ed5b492012-01-05 21:48:15 +0000448 case ProcessMessage::eBreakpointMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +0000449 case ProcessMessage::eTraceMessage:
Matt Kopece9ea0da2013-05-07 19:29:28 +0000450 case ProcessMessage::eWatchpointMessage:
Andrew Kaylor93132f52013-05-28 23:04:25 +0000451 case ProcessMessage::eNewThreadMessage:
452 case ProcessMessage::eCrashMessage:
453 assert(thread);
454 thread->SetState(eStateStopped);
455 StopAllThreads(message.GetTID());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000456 SetPrivateState(eStateStopped);
457 break;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000458 }
459
460 m_message_queue.push(message);
461}
462
Andrew Kaylor93132f52013-05-28 23:04:25 +0000463void
464ProcessPOSIX::StopAllThreads(lldb::tid_t stop_tid)
465{
466 // FIXME: Will this work the same way on FreeBSD and Linux?
467}
468
Matt Kopecb2910442013-07-09 15:09:45 +0000469bool
470ProcessPOSIX::AddThreadForInitialStopIfNeeded(lldb::tid_t stop_tid)
471{
472 bool added_to_set = false;
473 ThreadStopSet::iterator it = m_seen_initial_stop.find(stop_tid);
474 if (it == m_seen_initial_stop.end())
475 {
476 m_seen_initial_stop.insert(stop_tid);
477 added_to_set = true;
478 }
479 return added_to_set;
480}
481
Johnny Chen9ed5b492012-01-05 21:48:15 +0000482void
483ProcessPOSIX::RefreshStateAfterStop()
484{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000485 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000486 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000487 log->Printf ("ProcessPOSIX::%s(), message_queue size = %d", __FUNCTION__, (int)m_message_queue.size());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000488
489 Mutex::Locker lock(m_message_mutex);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000490
Andrew Kaylor93132f52013-05-28 23:04:25 +0000491 // This method used to only handle one message. Changing it to loop allows
492 // it to handle the case where we hit a breakpoint while handling a different
493 // breakpoint.
494 while (!m_message_queue.empty())
495 {
496 ProcessMessage &message = m_message_queue.front();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000497
Andrew Kaylor93132f52013-05-28 23:04:25 +0000498 // Resolve the thread this message corresponds to and pass it along.
499 lldb::tid_t tid = message.GetTID();
500 if (log)
501 log->Printf ("ProcessPOSIX::%s(), message_queue size = %d, pid = %" PRIi64, __FUNCTION__, (int)m_message_queue.size(), tid);
502 POSIXThread *thread = static_cast<POSIXThread*>(
503 GetThreadList().FindThreadByID(tid, false).get());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000504
Andrew Kaylor93132f52013-05-28 23:04:25 +0000505 if (message.GetKind() == ProcessMessage::eNewThreadMessage)
506 {
507 if (log)
508 log->Printf ("ProcessPOSIX::%s() adding thread, tid = %" PRIi64, __FUNCTION__, message.GetChildTID());
Matt Kopecfb6ab542013-07-10 20:53:11 +0000509 lldb::tid_t child_tid = message.GetChildTID();
Andrew Kaylor93132f52013-05-28 23:04:25 +0000510 ThreadSP thread_sp;
Matt Kopecfb6ab542013-07-10 20:53:11 +0000511 thread_sp.reset(new POSIXThread(*this, child_tid));
512
513 POSIXThread *thread = static_cast<POSIXThread*>(thread_sp.get());
514 thread->SetName(Host::GetThreadName(GetID(), child_tid).c_str());
515
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000516 Mutex::Locker lock(m_thread_list.GetMutex());
517
Andrew Kaylor93132f52013-05-28 23:04:25 +0000518 m_thread_list.AddThread(thread_sp);
519 }
520
521 m_thread_list.RefreshStateAfterStop();
522
523 if (thread)
524 thread->Notify(message);
525
526 if (message.GetKind() == ProcessMessage::eExitMessage)
527 {
528 // FIXME: We should tell the user about this, but the limbo message is probably better for that.
529 if (log)
530 log->Printf ("ProcessPOSIX::%s() removing thread, tid = %" PRIi64, __FUNCTION__, tid);
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000531
532 Mutex::Locker lock(m_thread_list.GetMutex());
533
Andrew Kaylor93132f52013-05-28 23:04:25 +0000534 ThreadSP thread_sp = m_thread_list.RemoveThreadByID(tid, false);
535 thread_sp.reset();
Matt Kopecb2910442013-07-09 15:09:45 +0000536 m_seen_initial_stop.erase(tid);
Andrew Kaylor93132f52013-05-28 23:04:25 +0000537 }
538
539 m_message_queue.pop();
Matt Kopec650648f2013-01-08 16:30:18 +0000540 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000541}
542
543bool
544ProcessPOSIX::IsAlive()
545{
546 StateType state = GetPrivateState();
Daniel Malea335bf6f2013-04-01 19:48:37 +0000547 return state != eStateDetached
548 && state != eStateExited
549 && state != eStateInvalid
550 && state != eStateUnloaded;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000551}
552
553size_t
554ProcessPOSIX::DoReadMemory(addr_t vm_addr,
555 void *buf, size_t size, Error &error)
556{
557 assert(m_monitor);
558 return m_monitor->ReadMemory(vm_addr, buf, size, error);
559}
560
561size_t
562ProcessPOSIX::DoWriteMemory(addr_t vm_addr, const void *buf, size_t size,
563 Error &error)
564{
565 assert(m_monitor);
566 return m_monitor->WriteMemory(vm_addr, buf, size, error);
567}
568
569addr_t
570ProcessPOSIX::DoAllocateMemory(size_t size, uint32_t permissions,
571 Error &error)
572{
573 addr_t allocated_addr = LLDB_INVALID_ADDRESS;
574
575 unsigned prot = 0;
576 if (permissions & lldb::ePermissionsReadable)
577 prot |= eMmapProtRead;
578 if (permissions & lldb::ePermissionsWritable)
579 prot |= eMmapProtWrite;
580 if (permissions & lldb::ePermissionsExecutable)
581 prot |= eMmapProtExec;
582
583 if (InferiorCallMmap(this, allocated_addr, 0, size, prot,
584 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) {
585 m_addr_to_mmap_size[allocated_addr] = size;
586 error.Clear();
587 } else {
588 allocated_addr = LLDB_INVALID_ADDRESS;
589 error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions));
590 }
591
592 return allocated_addr;
593}
594
595Error
596ProcessPOSIX::DoDeallocateMemory(lldb::addr_t addr)
597{
598 Error error;
599 MMapMap::iterator pos = m_addr_to_mmap_size.find(addr);
600 if (pos != m_addr_to_mmap_size.end() &&
601 InferiorCallMunmap(this, addr, pos->second))
602 m_addr_to_mmap_size.erase (pos);
603 else
Daniel Malead01b2952012-11-29 21:49:15 +0000604 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000605
606 return error;
607}
608
Matt Kopec00049b82013-02-27 20:13:38 +0000609addr_t
610ProcessPOSIX::ResolveIndirectFunction(const Address *address, Error &error)
611{
612 addr_t function_addr = LLDB_INVALID_ADDRESS;
613 if (address == NULL) {
614 error.SetErrorStringWithFormat("unable to determine direct function call for NULL address");
615 } else if (!InferiorCall(this, address, function_addr)) {
616 function_addr = LLDB_INVALID_ADDRESS;
Matt Kopec66fd4b12013-03-01 17:44:31 +0000617 error.SetErrorStringWithFormat("unable to determine direct function call for indirect function %s",
618 address->CalculateSymbolContextSymbol()->GetName().AsCString());
Matt Kopec00049b82013-02-27 20:13:38 +0000619 }
620 return function_addr;
621}
622
Johnny Chen9ed5b492012-01-05 21:48:15 +0000623size_t
624ProcessPOSIX::GetSoftwareBreakpointTrapOpcode(BreakpointSite* bp_site)
625{
626 static const uint8_t g_i386_opcode[] = { 0xCC };
627
628 ArchSpec arch = GetTarget().GetArchitecture();
629 const uint8_t *opcode = NULL;
630 size_t opcode_size = 0;
631
632 switch (arch.GetCore())
633 {
634 default:
635 assert(false && "CPU type not supported!");
636 break;
637
638 case ArchSpec::eCore_x86_32_i386:
639 case ArchSpec::eCore_x86_64_x86_64:
640 opcode = g_i386_opcode;
641 opcode_size = sizeof(g_i386_opcode);
642 break;
643 }
644
645 bp_site->SetTrapOpcode(opcode, opcode_size);
646 return opcode_size;
647}
648
649Error
Daniel Maleab7eec012013-02-15 20:23:25 +0000650ProcessPOSIX::EnableBreakpointSite(BreakpointSite *bp_site)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000651{
652 return EnableSoftwareBreakpoint(bp_site);
653}
654
655Error
Daniel Maleab7eec012013-02-15 20:23:25 +0000656ProcessPOSIX::DisableBreakpointSite(BreakpointSite *bp_site)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000657{
658 return DisableSoftwareBreakpoint(bp_site);
659}
660
Matt Kopece9ea0da2013-05-07 19:29:28 +0000661Error
662ProcessPOSIX::EnableWatchpoint(Watchpoint *wp, bool notify)
663{
664 Error error;
665 if (wp)
666 {
667 user_id_t watchID = wp->GetID();
668 addr_t addr = wp->GetLoadAddress();
669 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
670 if (log)
671 log->Printf ("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64 ")",
672 watchID);
673 if (wp->IsEnabled())
674 {
675 if (log)
676 log->Printf("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64
677 ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.",
678 watchID, (uint64_t)addr);
679 return error;
680 }
681
Matt Kopec6f961232013-06-03 17:40:20 +0000682 // Try to find a vacant watchpoint slot in the inferiors' main thread
683 uint32_t wp_hw_index = LLDB_INVALID_INDEX32;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000684 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopec6f961232013-06-03 17:40:20 +0000685 POSIXThread *thread = static_cast<POSIXThread*>(
686 m_thread_list.GetThreadAtIndex(0, false).get());
687
688 if (thread)
689 wp_hw_index = thread->FindVacantWatchpointIndex();
690
691 if (wp_hw_index == LLDB_INVALID_INDEX32)
Matt Kopece9ea0da2013-05-07 19:29:28 +0000692 {
Matt Kopec6f961232013-06-03 17:40:20 +0000693 error.SetErrorString("Setting hardware watchpoint failed.");
Matt Kopece9ea0da2013-05-07 19:29:28 +0000694 }
695 else
696 {
Matt Kopec6f961232013-06-03 17:40:20 +0000697 wp->SetHardwareIndex(wp_hw_index);
698 bool wp_enabled = true;
699 uint32_t thread_count = m_thread_list.GetSize(false);
700 for (uint32_t i = 0; i < thread_count; ++i)
701 {
702 thread = static_cast<POSIXThread*>(
703 m_thread_list.GetThreadAtIndex(i, false).get());
704 if (thread)
705 wp_enabled &= thread->EnableHardwareWatchpoint(wp);
706 else
707 wp_enabled = false;
708 }
709 if (wp_enabled)
710 {
711 wp->SetEnabled(true, notify);
712 return error;
713 }
714 else
715 {
716 // Watchpoint enabling failed on at least one
717 // of the threads so roll back all of them
718 DisableWatchpoint(wp, false);
719 error.SetErrorString("Setting hardware watchpoint failed");
720 }
Matt Kopece9ea0da2013-05-07 19:29:28 +0000721 }
722 }
723 else
724 error.SetErrorString("Watchpoint argument was NULL.");
725 return error;
726}
727
728Error
729ProcessPOSIX::DisableWatchpoint(Watchpoint *wp, bool notify)
730{
731 Error error;
732 if (wp)
733 {
734 user_id_t watchID = wp->GetID();
735 addr_t addr = wp->GetLoadAddress();
736 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
737 if (log)
738 log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64 ")",
739 watchID);
740 if (!wp->IsEnabled())
741 {
742 if (log)
743 log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64
744 ") addr = 0x%8.8" PRIx64 ": watchpoint already disabled.",
745 watchID, (uint64_t)addr);
746 // This is needed (for now) to keep watchpoints disabled correctly
747 wp->SetEnabled(false, notify);
748 return error;
749 }
750
751 if (wp->IsHardware())
752 {
753 bool wp_disabled = true;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000754 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopece9ea0da2013-05-07 19:29:28 +0000755 uint32_t thread_count = m_thread_list.GetSize(false);
756 for (uint32_t i = 0; i < thread_count; ++i)
757 {
758 POSIXThread *thread = static_cast<POSIXThread*>(
759 m_thread_list.GetThreadAtIndex(i, false).get());
760 if (thread)
761 wp_disabled &= thread->DisableHardwareWatchpoint(wp);
762 else
763 wp_disabled = false;
764 }
765 if (wp_disabled)
766 {
Matt Kopec6f961232013-06-03 17:40:20 +0000767 wp->SetHardwareIndex(LLDB_INVALID_INDEX32);
Matt Kopece9ea0da2013-05-07 19:29:28 +0000768 wp->SetEnabled(false, notify);
769 return error;
770 }
771 else
772 error.SetErrorString("Disabling hardware watchpoint failed");
773 }
774 }
775 else
776 error.SetErrorString("Watchpoint argument was NULL.");
777 return error;
778}
779
780Error
781ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num)
782{
783 Error error;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000784 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopece9ea0da2013-05-07 19:29:28 +0000785 POSIXThread *thread = static_cast<POSIXThread*>(
786 m_thread_list.GetThreadAtIndex(0, false).get());
787 if (thread)
788 num = thread->NumSupportedHardwareWatchpoints();
789 else
790 error.SetErrorString("Process does not exist.");
791 return error;
792}
793
794Error
795ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num, bool &after)
796{
797 Error error = GetWatchpointSupportInfo(num);
798 // Watchpoints trigger and halt the inferior after
799 // the corresponding instruction has been executed.
800 after = true;
801 return error;
802}
803
Johnny Chen9ed5b492012-01-05 21:48:15 +0000804uint32_t
805ProcessPOSIX::UpdateThreadListIfNeeded()
806{
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000807 Mutex::Locker lock(m_thread_list.GetMutex());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000808 // Do not allow recursive updates.
809 return m_thread_list.GetSize(false);
810}
811
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000812bool
Johnny Chen9ed5b492012-01-05 21:48:15 +0000813ProcessPOSIX::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list)
814{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000815 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000816 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000817 log->Printf ("ProcessPOSIX::%s() (pid = %" PRIi64 ")", __FUNCTION__, GetID());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000818
819 // Update the process thread list with this new thread.
820 // FIXME: We should be using tid, not pid.
821 assert(m_monitor);
822 ThreadSP thread_sp (old_thread_list.FindThreadByID (GetID(), false));
Greg Clayton0c90ef42012-02-21 18:40:07 +0000823 if (!thread_sp) {
Greg Claytondf3df252012-10-12 16:23:23 +0000824 thread_sp.reset(new POSIXThread(*this, GetID()));
Greg Clayton0c90ef42012-02-21 18:40:07 +0000825 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000826
827 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000828 log->Printf ("ProcessPOSIX::%s() updated pid = %" PRIi64, __FUNCTION__, GetID());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000829 new_thread_list.AddThread(thread_sp);
830
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000831 return new_thread_list.GetSize(false) > 0;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000832}
833
834ByteOrder
835ProcessPOSIX::GetByteOrder() const
836{
837 // FIXME: We should be able to extract this value directly. See comment in
838 // ProcessPOSIX().
839 return m_byte_order;
840}
841
842size_t
843ProcessPOSIX::PutSTDIN(const char *buf, size_t len, Error &error)
844{
845 ssize_t status;
846 if ((status = write(m_monitor->GetTerminalFD(), buf, len)) < 0)
847 {
848 error.SetErrorToErrno();
849 return 0;
850 }
851 return status;
852}
853
Johnny Chen9ed5b492012-01-05 21:48:15 +0000854UnixSignals &
855ProcessPOSIX::GetUnixSignals()
856{
857 return m_signals;
858}
859
860//------------------------------------------------------------------------------
861// Utility functions.
862
863bool
864ProcessPOSIX::HasExited()
865{
866 switch (GetPrivateState())
867 {
868 default:
869 break;
870
871 case eStateDetached:
872 case eStateExited:
873 return true;
874 }
875
876 return false;
877}
878
879bool
880ProcessPOSIX::IsStopped()
881{
882 switch (GetPrivateState())
883 {
884 default:
885 break;
886
887 case eStateStopped:
888 case eStateCrashed:
889 case eStateSuspended:
890 return true;
891 }
892
893 return false;
894}
Matt Kopecb2910442013-07-09 15:09:45 +0000895
896bool
897ProcessPOSIX::IsAThreadRunning()
898{
899 bool is_running = false;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000900 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopecb2910442013-07-09 15:09:45 +0000901 uint32_t thread_count = m_thread_list.GetSize(false);
902 for (uint32_t i = 0; i < thread_count; ++i)
903 {
904 POSIXThread *thread = static_cast<POSIXThread*>(
905 m_thread_list.GetThreadAtIndex(i, false).get());
906 StateType thread_state = thread->GetState();
907 if (thread_state == eStateRunning || thread_state == eStateStepping)
908 {
909 is_running = true;
910 break;
911 }
912 }
913 return is_running;
914}