blob: 571818134d44249045b7f3d1a21900ddc885f3be [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)
Saleem Abdulrasool03700de2014-03-08 20:47:03 +0000189 {
Johnny Chen9ed5b492012-01-05 21:48:15 +0000190 path = file_action->GetPath();
191 // By default the stdio paths passed in will be pseudo-terminal
192 // (/dev/pts). If so, convert to using a different default path
193 // instead to redirect I/O to the debugger console. This should
194 // also handle user overrides to /dev/null or a different file.
Saleem Abdulrasoold41fca12014-03-08 20:47:07 +0000195 if (!path || ::strncmp(path, pts_name, ::strlen(pts_name)) == 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 }
199
200 return path;
201}
202
203Error
204ProcessPOSIX::DoLaunch (Module *module,
Jean-Daniel Dupas7782de92013-12-09 22:52:50 +0000205 ProcessLaunchInfo &launch_info)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000206{
207 Error error;
208 assert(m_monitor == NULL);
209
Daniel Malea6217d2a2013-01-08 14:49:22 +0000210 const char* working_dir = launch_info.GetWorkingDirectory();
211 if (working_dir) {
212 FileSpec WorkingDir(working_dir, true);
213 if (!WorkingDir || WorkingDir.GetFileType() != FileSpec::eFileTypeDirectory)
214 {
215 error.SetErrorStringWithFormat("No such file or directory: %s", working_dir);
216 return error;
217 }
218 }
219
Johnny Chen9ed5b492012-01-05 21:48:15 +0000220 SetPrivateState(eStateLaunching);
221
222 const lldb_private::ProcessLaunchInfo::FileAction *file_action;
223
224 // Default of NULL will mean to use existing open file descriptors
225 const char *stdin_path = NULL;
226 const char *stdout_path = NULL;
227 const char *stderr_path = NULL;
Daniel Malea6217d2a2013-01-08 14:49:22 +0000228
Johnny Chen9ed5b492012-01-05 21:48:15 +0000229 file_action = launch_info.GetFileActionForFD (STDIN_FILENO);
230 stdin_path = GetFilePath(file_action, stdin_path);
231
232 file_action = launch_info.GetFileActionForFD (STDOUT_FILENO);
233 stdout_path = GetFilePath(file_action, stdout_path);
234
235 file_action = launch_info.GetFileActionForFD (STDERR_FILENO);
236 stderr_path = GetFilePath(file_action, stderr_path);
237
Andrew Kaylor6578cb62013-07-09 22:36:48 +0000238 m_monitor = new ProcessMonitor (this,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000239 module,
240 launch_info.GetArguments().GetConstArgumentVector(),
241 launch_info.GetEnvironmentEntries().GetConstArgumentVector(),
242 stdin_path,
243 stdout_path,
244 stderr_path,
Daniel Malea6217d2a2013-01-08 14:49:22 +0000245 working_dir,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000246 error);
247
248 m_module = module;
249
250 if (!error.Success())
251 return error;
252
Matt Kopec9eb40a92013-03-14 21:35:26 +0000253 SetSTDIOFileDescriptor(m_monitor->GetTerminalFD());
254
Johnny Chen9ed5b492012-01-05 21:48:15 +0000255 SetID(m_monitor->GetPID());
256 return error;
257}
258
259void
260ProcessPOSIX::DidLaunch()
261{
262}
263
Ed Maste30df85e2013-12-11 20:43:27 +0000264Error
265ProcessPOSIX::DoResume()
266{
267 StateType state = GetPrivateState();
268
269 assert(state == eStateStopped);
270
271 SetPrivateState(eStateRunning);
272
273 bool did_resume = false;
274
275 Mutex::Locker lock(m_thread_list.GetMutex());
276
277 uint32_t thread_count = m_thread_list.GetSize(false);
278 for (uint32_t i = 0; i < thread_count; ++i)
279 {
280 POSIXThread *thread = static_cast<POSIXThread*>(
281 m_thread_list.GetThreadAtIndex(i, false).get());
282 did_resume = thread->Resume() || did_resume;
283 }
284 assert(did_resume && "Process resume failed!");
285
286 return Error();
287}
288
Johnny Chen9ed5b492012-01-05 21:48:15 +0000289addr_t
290ProcessPOSIX::GetImageInfoAddress()
291{
292 Target *target = &GetTarget();
293 ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
Ed Maste54803652013-10-11 17:39:07 +0000294 Address addr = obj_file->GetImageInfoAddress(target);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000295
Ed Maste04a8bab2013-10-11 01:16:08 +0000296 if (addr.IsValid())
Ed Maste54803652013-10-11 17:39:07 +0000297 return addr.GetLoadAddress(target);
Ed Maste04a8bab2013-10-11 01:16:08 +0000298 return LLDB_INVALID_ADDRESS;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000299}
300
301Error
302ProcessPOSIX::DoHalt(bool &caused_stop)
303{
304 Error error;
305
306 if (IsStopped())
307 {
308 caused_stop = false;
309 }
310 else if (kill(GetID(), SIGSTOP))
311 {
312 caused_stop = false;
313 error.SetErrorToErrno();
314 }
315 else
316 {
317 caused_stop = true;
318 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000319 return error;
320}
321
322Error
Johnny Chen9ed5b492012-01-05 21:48:15 +0000323ProcessPOSIX::DoSignal(int signal)
324{
325 Error error;
326
327 if (kill(GetID(), signal))
328 error.SetErrorToErrno();
329
330 return error;
331}
332
333Error
334ProcessPOSIX::DoDestroy()
335{
336 Error error;
337
338 if (!HasExited())
339 {
Ed Maste70882932014-04-01 14:30:56 +0000340 assert(m_monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000341 m_exit_now = true;
Ed Maste70882932014-04-01 14:30:56 +0000342#ifdef __linux__
Greg Claytonab950c32012-03-30 19:56:32 +0000343 if ((m_monitor == NULL || kill(m_monitor->GetPID(), SIGKILL)) && error.Success())
Ed Maste70882932014-04-01 14:30:56 +0000344#else
345 if (!m_monitor->Kill())
346#endif
Johnny Chen9ed5b492012-01-05 21:48:15 +0000347 {
348 error.SetErrorToErrno();
349 return error;
350 }
351
352 SetPrivateState(eStateExited);
353 }
354
355 return error;
356}
357
358void
Matt Kopec718be872013-10-09 19:39:55 +0000359ProcessPOSIX::DoDidExec()
360{
361 Target *target = &GetTarget();
362 if (target)
363 {
364 PlatformSP platform_sp (target->GetPlatform());
365 assert (platform_sp.get());
366 if (platform_sp)
367 {
368 ProcessInstanceInfo process_info;
369 platform_sp->GetProcessInfo(GetID(), process_info);
370 ModuleSP exe_module_sp;
371 FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths());
372 Error error = platform_sp->ResolveExecutable(process_info.GetExecutableFile(),
373 target->GetArchitecture(),
374 exe_module_sp,
375 executable_search_paths.GetSize() ? &executable_search_paths : NULL);
376 if (!error.Success())
377 return;
378 target->SetExecutableModule(exe_module_sp, true);
379 }
380 }
381}
382
Ed Maste30df85e2013-12-11 20:43:27 +0000383void
384ProcessPOSIX::SendMessage(const ProcessMessage &message)
385{
386 Mutex::Locker lock(m_message_mutex);
387
388 Mutex::Locker thread_lock(m_thread_list.GetMutex());
389
390 POSIXThread *thread = static_cast<POSIXThread*>(
391 m_thread_list.FindThreadByID(message.GetTID(), false).get());
392
393 switch (message.GetKind())
394 {
395 case ProcessMessage::eInvalidMessage:
396 return;
397
398 case ProcessMessage::eAttachMessage:
399 SetPrivateState(eStateStopped);
400 return;
401
402 case ProcessMessage::eLimboMessage:
403 assert(thread);
404 thread->SetState(eStateStopped);
405 if (message.GetTID() == GetID())
406 {
407 m_exit_status = message.GetExitStatus();
408 if (m_exit_now)
409 {
410 SetPrivateState(eStateExited);
411 m_monitor->Detach(GetID());
412 }
413 else
414 {
415 StopAllThreads(message.GetTID());
416 SetPrivateState(eStateStopped);
417 }
418 }
419 else
420 {
421 StopAllThreads(message.GetTID());
422 SetPrivateState(eStateStopped);
423 }
424 break;
425
426 case ProcessMessage::eExitMessage:
427 assert(thread);
428 thread->SetState(eStateExited);
429 // FIXME: I'm not sure we need to do this.
430 if (message.GetTID() == GetID())
431 {
432 m_exit_status = message.GetExitStatus();
433 SetExitStatus(m_exit_status, NULL);
434 }
435 else if (!IsAThreadRunning())
436 SetPrivateState(eStateStopped);
437 break;
438
439 case ProcessMessage::eSignalMessage:
440 case ProcessMessage::eSignalDeliveredMessage:
441 if (message.GetSignal() == SIGSTOP &&
442 AddThreadForInitialStopIfNeeded(message.GetTID()))
443 return;
444 // Intentional fall-through
445
446 case ProcessMessage::eBreakpointMessage:
447 case ProcessMessage::eTraceMessage:
448 case ProcessMessage::eWatchpointMessage:
449 case ProcessMessage::eCrashMessage:
450 assert(thread);
451 thread->SetState(eStateStopped);
452 StopAllThreads(message.GetTID());
453 SetPrivateState(eStateStopped);
454 break;
455
456 case ProcessMessage::eNewThreadMessage:
457 {
458 lldb::tid_t new_tid = message.GetChildTID();
459 if (WaitingForInitialStop(new_tid))
460 {
461 m_monitor->WaitForInitialTIDStop(new_tid);
462 }
463 assert(thread);
464 thread->SetState(eStateStopped);
465 StopAllThreads(message.GetTID());
466 SetPrivateState(eStateStopped);
467 break;
468 }
469
470 case ProcessMessage::eExecMessage:
471 {
472 assert(thread);
473 thread->SetState(eStateStopped);
474 StopAllThreads(message.GetTID());
475 SetPrivateState(eStateStopped);
476 break;
477 }
478 }
479
480
481 m_message_queue.push(message);
482}
483
Andrew Kaylor93132f52013-05-28 23:04:25 +0000484void
485ProcessPOSIX::StopAllThreads(lldb::tid_t stop_tid)
486{
487 // FIXME: Will this work the same way on FreeBSD and Linux?
488}
489
Matt Kopecb2910442013-07-09 15:09:45 +0000490bool
491ProcessPOSIX::AddThreadForInitialStopIfNeeded(lldb::tid_t stop_tid)
492{
493 bool added_to_set = false;
494 ThreadStopSet::iterator it = m_seen_initial_stop.find(stop_tid);
495 if (it == m_seen_initial_stop.end())
496 {
497 m_seen_initial_stop.insert(stop_tid);
498 added_to_set = true;
499 }
500 return added_to_set;
501}
502
Andrew Kaylord4d54992013-09-17 00:30:24 +0000503bool
504ProcessPOSIX::WaitingForInitialStop(lldb::tid_t stop_tid)
505{
506 return (m_seen_initial_stop.find(stop_tid) == m_seen_initial_stop.end());
507}
508
Michael Sartain9f822cd2013-07-31 23:27:46 +0000509POSIXThread *
510ProcessPOSIX::CreateNewPOSIXThread(lldb_private::Process &process, lldb::tid_t tid)
511{
512 return new POSIXThread(process, tid);
513}
514
Johnny Chen9ed5b492012-01-05 21:48:15 +0000515void
516ProcessPOSIX::RefreshStateAfterStop()
517{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000518 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000519 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000520 log->Printf ("ProcessPOSIX::%s(), message_queue size = %d", __FUNCTION__, (int)m_message_queue.size());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000521
522 Mutex::Locker lock(m_message_mutex);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000523
Andrew Kaylor93132f52013-05-28 23:04:25 +0000524 // This method used to only handle one message. Changing it to loop allows
525 // it to handle the case where we hit a breakpoint while handling a different
526 // breakpoint.
527 while (!m_message_queue.empty())
528 {
529 ProcessMessage &message = m_message_queue.front();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000530
Andrew Kaylor93132f52013-05-28 23:04:25 +0000531 // Resolve the thread this message corresponds to and pass it along.
532 lldb::tid_t tid = message.GetTID();
533 if (log)
534 log->Printf ("ProcessPOSIX::%s(), message_queue size = %d, pid = %" PRIi64, __FUNCTION__, (int)m_message_queue.size(), tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000535
Andrew Kaylor93132f52013-05-28 23:04:25 +0000536 if (message.GetKind() == ProcessMessage::eNewThreadMessage)
537 {
538 if (log)
539 log->Printf ("ProcessPOSIX::%s() adding thread, tid = %" PRIi64, __FUNCTION__, message.GetChildTID());
Matt Kopecfb6ab542013-07-10 20:53:11 +0000540 lldb::tid_t child_tid = message.GetChildTID();
Andrew Kaylor93132f52013-05-28 23:04:25 +0000541 ThreadSP thread_sp;
Michael Sartain9f822cd2013-07-31 23:27:46 +0000542 thread_sp.reset(CreateNewPOSIXThread(*this, child_tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +0000543
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000544 Mutex::Locker lock(m_thread_list.GetMutex());
545
Andrew Kaylor93132f52013-05-28 23:04:25 +0000546 m_thread_list.AddThread(thread_sp);
547 }
548
549 m_thread_list.RefreshStateAfterStop();
550
Ed Maste685fea92013-08-29 20:40:11 +0000551 POSIXThread *thread = static_cast<POSIXThread*>(
552 GetThreadList().FindThreadByID(tid, false).get());
Andrew Kaylor93132f52013-05-28 23:04:25 +0000553 if (thread)
554 thread->Notify(message);
555
556 if (message.GetKind() == ProcessMessage::eExitMessage)
557 {
558 // FIXME: We should tell the user about this, but the limbo message is probably better for that.
559 if (log)
560 log->Printf ("ProcessPOSIX::%s() removing thread, tid = %" PRIi64, __FUNCTION__, tid);
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000561
562 Mutex::Locker lock(m_thread_list.GetMutex());
563
Andrew Kaylor93132f52013-05-28 23:04:25 +0000564 ThreadSP thread_sp = m_thread_list.RemoveThreadByID(tid, false);
565 thread_sp.reset();
Matt Kopecb2910442013-07-09 15:09:45 +0000566 m_seen_initial_stop.erase(tid);
Andrew Kaylor93132f52013-05-28 23:04:25 +0000567 }
568
569 m_message_queue.pop();
Matt Kopec650648f2013-01-08 16:30:18 +0000570 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000571}
572
573bool
574ProcessPOSIX::IsAlive()
575{
576 StateType state = GetPrivateState();
Daniel Malea335bf6f2013-04-01 19:48:37 +0000577 return state != eStateDetached
578 && state != eStateExited
579 && state != eStateInvalid
580 && state != eStateUnloaded;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000581}
582
583size_t
584ProcessPOSIX::DoReadMemory(addr_t vm_addr,
585 void *buf, size_t size, Error &error)
586{
587 assert(m_monitor);
588 return m_monitor->ReadMemory(vm_addr, buf, size, error);
589}
590
591size_t
592ProcessPOSIX::DoWriteMemory(addr_t vm_addr, const void *buf, size_t size,
593 Error &error)
594{
595 assert(m_monitor);
596 return m_monitor->WriteMemory(vm_addr, buf, size, error);
597}
598
599addr_t
600ProcessPOSIX::DoAllocateMemory(size_t size, uint32_t permissions,
601 Error &error)
602{
603 addr_t allocated_addr = LLDB_INVALID_ADDRESS;
604
605 unsigned prot = 0;
606 if (permissions & lldb::ePermissionsReadable)
607 prot |= eMmapProtRead;
608 if (permissions & lldb::ePermissionsWritable)
609 prot |= eMmapProtWrite;
610 if (permissions & lldb::ePermissionsExecutable)
611 prot |= eMmapProtExec;
612
613 if (InferiorCallMmap(this, allocated_addr, 0, size, prot,
614 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) {
615 m_addr_to_mmap_size[allocated_addr] = size;
616 error.Clear();
617 } else {
618 allocated_addr = LLDB_INVALID_ADDRESS;
619 error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions));
620 }
621
622 return allocated_addr;
623}
624
625Error
626ProcessPOSIX::DoDeallocateMemory(lldb::addr_t addr)
627{
628 Error error;
629 MMapMap::iterator pos = m_addr_to_mmap_size.find(addr);
630 if (pos != m_addr_to_mmap_size.end() &&
631 InferiorCallMunmap(this, addr, pos->second))
632 m_addr_to_mmap_size.erase (pos);
633 else
Daniel Malead01b2952012-11-29 21:49:15 +0000634 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000635
636 return error;
637}
638
639size_t
640ProcessPOSIX::GetSoftwareBreakpointTrapOpcode(BreakpointSite* bp_site)
641{
642 static const uint8_t g_i386_opcode[] = { 0xCC };
643
644 ArchSpec arch = GetTarget().GetArchitecture();
645 const uint8_t *opcode = NULL;
646 size_t opcode_size = 0;
647
Greg Clayton906e9ac2014-03-20 21:31:55 +0000648 switch (arch.GetMachine())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000649 {
650 default:
651 assert(false && "CPU type not supported!");
652 break;
653
Greg Clayton906e9ac2014-03-20 21:31:55 +0000654 case llvm::Triple::x86:
655 case llvm::Triple::x86_64:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000656 opcode = g_i386_opcode;
657 opcode_size = sizeof(g_i386_opcode);
658 break;
659 }
660
661 bp_site->SetTrapOpcode(opcode, opcode_size);
662 return opcode_size;
663}
664
665Error
Daniel Maleab7eec012013-02-15 20:23:25 +0000666ProcessPOSIX::EnableBreakpointSite(BreakpointSite *bp_site)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000667{
668 return EnableSoftwareBreakpoint(bp_site);
669}
670
671Error
Daniel Maleab7eec012013-02-15 20:23:25 +0000672ProcessPOSIX::DisableBreakpointSite(BreakpointSite *bp_site)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000673{
674 return DisableSoftwareBreakpoint(bp_site);
675}
676
Matt Kopece9ea0da2013-05-07 19:29:28 +0000677Error
678ProcessPOSIX::EnableWatchpoint(Watchpoint *wp, bool notify)
679{
680 Error error;
681 if (wp)
682 {
683 user_id_t watchID = wp->GetID();
684 addr_t addr = wp->GetLoadAddress();
685 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
686 if (log)
687 log->Printf ("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64 ")",
688 watchID);
689 if (wp->IsEnabled())
690 {
691 if (log)
692 log->Printf("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64
693 ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.",
694 watchID, (uint64_t)addr);
695 return error;
696 }
697
Matt Kopec6f961232013-06-03 17:40:20 +0000698 // Try to find a vacant watchpoint slot in the inferiors' main thread
699 uint32_t wp_hw_index = LLDB_INVALID_INDEX32;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000700 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopec6f961232013-06-03 17:40:20 +0000701 POSIXThread *thread = static_cast<POSIXThread*>(
702 m_thread_list.GetThreadAtIndex(0, false).get());
703
704 if (thread)
705 wp_hw_index = thread->FindVacantWatchpointIndex();
706
707 if (wp_hw_index == LLDB_INVALID_INDEX32)
Matt Kopece9ea0da2013-05-07 19:29:28 +0000708 {
Matt Kopec6f961232013-06-03 17:40:20 +0000709 error.SetErrorString("Setting hardware watchpoint failed.");
Matt Kopece9ea0da2013-05-07 19:29:28 +0000710 }
711 else
712 {
Matt Kopec6f961232013-06-03 17:40:20 +0000713 wp->SetHardwareIndex(wp_hw_index);
714 bool wp_enabled = true;
715 uint32_t thread_count = m_thread_list.GetSize(false);
716 for (uint32_t i = 0; i < thread_count; ++i)
717 {
718 thread = static_cast<POSIXThread*>(
719 m_thread_list.GetThreadAtIndex(i, false).get());
720 if (thread)
721 wp_enabled &= thread->EnableHardwareWatchpoint(wp);
722 else
723 wp_enabled = false;
724 }
725 if (wp_enabled)
726 {
727 wp->SetEnabled(true, notify);
728 return error;
729 }
730 else
731 {
732 // Watchpoint enabling failed on at least one
733 // of the threads so roll back all of them
734 DisableWatchpoint(wp, false);
735 error.SetErrorString("Setting hardware watchpoint failed");
736 }
Matt Kopece9ea0da2013-05-07 19:29:28 +0000737 }
738 }
739 else
740 error.SetErrorString("Watchpoint argument was NULL.");
741 return error;
742}
743
744Error
745ProcessPOSIX::DisableWatchpoint(Watchpoint *wp, bool notify)
746{
747 Error error;
748 if (wp)
749 {
750 user_id_t watchID = wp->GetID();
751 addr_t addr = wp->GetLoadAddress();
752 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
753 if (log)
754 log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64 ")",
755 watchID);
756 if (!wp->IsEnabled())
757 {
758 if (log)
759 log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64
760 ") addr = 0x%8.8" PRIx64 ": watchpoint already disabled.",
761 watchID, (uint64_t)addr);
762 // This is needed (for now) to keep watchpoints disabled correctly
763 wp->SetEnabled(false, notify);
764 return error;
765 }
766
767 if (wp->IsHardware())
768 {
769 bool wp_disabled = true;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000770 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopece9ea0da2013-05-07 19:29:28 +0000771 uint32_t thread_count = m_thread_list.GetSize(false);
772 for (uint32_t i = 0; i < thread_count; ++i)
773 {
774 POSIXThread *thread = static_cast<POSIXThread*>(
775 m_thread_list.GetThreadAtIndex(i, false).get());
776 if (thread)
777 wp_disabled &= thread->DisableHardwareWatchpoint(wp);
778 else
779 wp_disabled = false;
780 }
781 if (wp_disabled)
782 {
Matt Kopec6f961232013-06-03 17:40:20 +0000783 wp->SetHardwareIndex(LLDB_INVALID_INDEX32);
Matt Kopece9ea0da2013-05-07 19:29:28 +0000784 wp->SetEnabled(false, notify);
785 return error;
786 }
787 else
788 error.SetErrorString("Disabling hardware watchpoint failed");
789 }
790 }
791 else
792 error.SetErrorString("Watchpoint argument was NULL.");
793 return error;
794}
795
796Error
797ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num)
798{
799 Error error;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000800 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopece9ea0da2013-05-07 19:29:28 +0000801 POSIXThread *thread = static_cast<POSIXThread*>(
802 m_thread_list.GetThreadAtIndex(0, false).get());
803 if (thread)
804 num = thread->NumSupportedHardwareWatchpoints();
805 else
806 error.SetErrorString("Process does not exist.");
807 return error;
808}
809
810Error
811ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num, bool &after)
812{
813 Error error = GetWatchpointSupportInfo(num);
814 // Watchpoints trigger and halt the inferior after
815 // the corresponding instruction has been executed.
816 after = true;
817 return error;
818}
819
Johnny Chen9ed5b492012-01-05 21:48:15 +0000820uint32_t
821ProcessPOSIX::UpdateThreadListIfNeeded()
822{
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000823 Mutex::Locker lock(m_thread_list.GetMutex());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000824 // Do not allow recursive updates.
825 return m_thread_list.GetSize(false);
826}
827
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000828bool
Johnny Chen9ed5b492012-01-05 21:48:15 +0000829ProcessPOSIX::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list)
830{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000831 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000832 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000833 log->Printf ("ProcessPOSIX::%s() (pid = %" PRIi64 ")", __FUNCTION__, GetID());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000834
Daniel Maleae0f8f572013-08-26 23:57:52 +0000835 bool has_updated = false;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000836 // Update the process thread list with this new thread.
837 // FIXME: We should be using tid, not pid.
838 assert(m_monitor);
839 ThreadSP thread_sp (old_thread_list.FindThreadByID (GetID(), false));
Greg Clayton0c90ef42012-02-21 18:40:07 +0000840 if (!thread_sp) {
Michael Sartain9f822cd2013-07-31 23:27:46 +0000841 thread_sp.reset(CreateNewPOSIXThread(*this, GetID()));
Daniel Maleae0f8f572013-08-26 23:57:52 +0000842 has_updated = true;
Greg Clayton0c90ef42012-02-21 18:40:07 +0000843 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000844
845 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000846 log->Printf ("ProcessPOSIX::%s() updated pid = %" PRIi64, __FUNCTION__, GetID());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000847 new_thread_list.AddThread(thread_sp);
848
Daniel Maleae0f8f572013-08-26 23:57:52 +0000849 return has_updated; // the list has been updated
Johnny Chen9ed5b492012-01-05 21:48:15 +0000850}
851
852ByteOrder
853ProcessPOSIX::GetByteOrder() const
854{
855 // FIXME: We should be able to extract this value directly. See comment in
856 // ProcessPOSIX().
857 return m_byte_order;
858}
859
860size_t
861ProcessPOSIX::PutSTDIN(const char *buf, size_t len, Error &error)
862{
863 ssize_t status;
864 if ((status = write(m_monitor->GetTerminalFD(), buf, len)) < 0)
865 {
866 error.SetErrorToErrno();
867 return 0;
868 }
869 return status;
870}
871
Johnny Chen9ed5b492012-01-05 21:48:15 +0000872UnixSignals &
873ProcessPOSIX::GetUnixSignals()
874{
875 return m_signals;
876}
877
878//------------------------------------------------------------------------------
879// Utility functions.
880
881bool
882ProcessPOSIX::HasExited()
883{
884 switch (GetPrivateState())
885 {
886 default:
887 break;
888
889 case eStateDetached:
890 case eStateExited:
891 return true;
892 }
893
894 return false;
895}
896
897bool
898ProcessPOSIX::IsStopped()
899{
900 switch (GetPrivateState())
901 {
902 default:
903 break;
904
905 case eStateStopped:
906 case eStateCrashed:
907 case eStateSuspended:
908 return true;
909 }
910
911 return false;
912}
Matt Kopecb2910442013-07-09 15:09:45 +0000913
914bool
915ProcessPOSIX::IsAThreadRunning()
916{
917 bool is_running = false;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000918 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopecb2910442013-07-09 15:09:45 +0000919 uint32_t thread_count = m_thread_list.GetSize(false);
920 for (uint32_t i = 0; i < thread_count; ++i)
921 {
922 POSIXThread *thread = static_cast<POSIXThread*>(
923 m_thread_list.GetThreadAtIndex(i, false).get());
924 StateType thread_state = thread->GetState();
925 if (thread_state == eStateRunning || thread_state == eStateStepping)
926 {
927 is_running = true;
928 break;
929 }
930 }
931 return is_running;
932}