blob: 07a3972ce92563befef6a16d8c402adfb222a148 [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"
Todd Fialacacde7d2014-09-27 16:54:22 +000031#include "Plugins/Process/Linux/ProcessMonitor.h"
Johnny Chen9ed5b492012-01-05 21:48:15 +000032#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
Todd Fiala4ceced32014-08-29 17:35:57 +000073ProcessPOSIX::ProcessPOSIX(Target& target, Listener &listener, UnixSignalsSP &unix_signals_sp)
74 : Process(target, listener, unix_signals_sp),
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 *
Todd Fiala6a2f62c2014-09-08 15:57:14 +0000179ProcessPOSIX::GetFilePath(const lldb_private::FileAction *file_action, const char *default_path,
180 const char *dbg_pts_path)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000181{
Johnny Chen9ed5b492012-01-05 21:48:15 +0000182 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.
Todd Fiala6a2f62c2014-09-08 15:57:14 +0000193 if (!path || (dbg_pts_path &&
194 ::strncmp(path, dbg_pts_path, ::strlen(dbg_pts_path)) == 0))
Johnny Chen9ed5b492012-01-05 21:48:15 +0000195 path = default_path;
Saleem Abdulrasool03700de2014-03-08 20:47:03 +0000196 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000197 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000198 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
Todd Fiala6a2f62c2014-09-08 15:57:14 +0000227 const char * dbg_pts_path = launch_info.GetPTY().GetSlaveName(NULL,0);
228
Johnny Chen9ed5b492012-01-05 21:48:15 +0000229 file_action = launch_info.GetFileActionForFD (STDIN_FILENO);
Todd Fiala6a2f62c2014-09-08 15:57:14 +0000230 stdin_path = GetFilePath(file_action, stdin_path, dbg_pts_path);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000231
232 file_action = launch_info.GetFileActionForFD (STDOUT_FILENO);
Todd Fiala6a2f62c2014-09-08 15:57:14 +0000233 stdout_path = GetFilePath(file_action, stdout_path, dbg_pts_path);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000234
235 file_action = launch_info.GetFileActionForFD (STDERR_FILENO);
Todd Fiala6a2f62c2014-09-08 15:57:14 +0000236 stderr_path = GetFilePath(file_action, stderr_path, dbg_pts_path);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000237
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,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000246 launch_info,
Johnny Chen9ed5b492012-01-05 21:48:15 +0000247 error);
248
249 m_module = module;
250
251 if (!error.Success())
252 return error;
253
Matt Kopec9eb40a92013-03-14 21:35:26 +0000254 SetSTDIOFileDescriptor(m_monitor->GetTerminalFD());
255
Johnny Chen9ed5b492012-01-05 21:48:15 +0000256 SetID(m_monitor->GetPID());
257 return error;
258}
259
260void
261ProcessPOSIX::DidLaunch()
262{
263}
264
Ed Maste30df85e2013-12-11 20:43:27 +0000265Error
266ProcessPOSIX::DoResume()
267{
268 StateType state = GetPrivateState();
269
270 assert(state == eStateStopped);
271
272 SetPrivateState(eStateRunning);
273
274 bool did_resume = false;
275
276 Mutex::Locker lock(m_thread_list.GetMutex());
277
278 uint32_t thread_count = m_thread_list.GetSize(false);
279 for (uint32_t i = 0; i < thread_count; ++i)
280 {
281 POSIXThread *thread = static_cast<POSIXThread*>(
282 m_thread_list.GetThreadAtIndex(i, false).get());
283 did_resume = thread->Resume() || did_resume;
284 }
285 assert(did_resume && "Process resume failed!");
286
287 return Error();
288}
289
Johnny Chen9ed5b492012-01-05 21:48:15 +0000290addr_t
291ProcessPOSIX::GetImageInfoAddress()
292{
293 Target *target = &GetTarget();
294 ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile();
Ed Maste54803652013-10-11 17:39:07 +0000295 Address addr = obj_file->GetImageInfoAddress(target);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000296
Ed Maste04a8bab2013-10-11 01:16:08 +0000297 if (addr.IsValid())
Ed Maste54803652013-10-11 17:39:07 +0000298 return addr.GetLoadAddress(target);
Ed Maste04a8bab2013-10-11 01:16:08 +0000299 return LLDB_INVALID_ADDRESS;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000300}
301
302Error
303ProcessPOSIX::DoHalt(bool &caused_stop)
304{
305 Error error;
306
307 if (IsStopped())
308 {
309 caused_stop = false;
310 }
311 else if (kill(GetID(), SIGSTOP))
312 {
313 caused_stop = false;
314 error.SetErrorToErrno();
315 }
316 else
317 {
318 caused_stop = true;
319 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000320 return error;
321}
322
323Error
Johnny Chen9ed5b492012-01-05 21:48:15 +0000324ProcessPOSIX::DoSignal(int signal)
325{
326 Error error;
327
328 if (kill(GetID(), signal))
329 error.SetErrorToErrno();
330
331 return error;
332}
333
334Error
335ProcessPOSIX::DoDestroy()
336{
337 Error error;
338
339 if (!HasExited())
340 {
Ed Maste70882932014-04-01 14:30:56 +0000341 assert(m_monitor);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000342 m_exit_now = true;
Ed Maste70882932014-04-01 14:30:56 +0000343 if (!m_monitor->Kill())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000344 {
345 error.SetErrorToErrno();
346 return error;
347 }
348
349 SetPrivateState(eStateExited);
350 }
351
352 return error;
353}
354
355void
Matt Kopec718be872013-10-09 19:39:55 +0000356ProcessPOSIX::DoDidExec()
357{
358 Target *target = &GetTarget();
359 if (target)
360 {
361 PlatformSP platform_sp (target->GetPlatform());
362 assert (platform_sp.get());
363 if (platform_sp)
364 {
365 ProcessInstanceInfo process_info;
366 platform_sp->GetProcessInfo(GetID(), process_info);
367 ModuleSP exe_module_sp;
368 FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths());
369 Error error = platform_sp->ResolveExecutable(process_info.GetExecutableFile(),
370 target->GetArchitecture(),
371 exe_module_sp,
372 executable_search_paths.GetSize() ? &executable_search_paths : NULL);
373 if (!error.Success())
374 return;
375 target->SetExecutableModule(exe_module_sp, true);
376 }
377 }
378}
379
Ed Maste30df85e2013-12-11 20:43:27 +0000380void
381ProcessPOSIX::SendMessage(const ProcessMessage &message)
382{
Andrew MacPherson82aae0d2014-04-02 06:57:45 +0000383 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
384
Ed Maste30df85e2013-12-11 20:43:27 +0000385 Mutex::Locker lock(m_message_mutex);
386
387 Mutex::Locker thread_lock(m_thread_list.GetMutex());
388
389 POSIXThread *thread = static_cast<POSIXThread*>(
390 m_thread_list.FindThreadByID(message.GetTID(), false).get());
391
392 switch (message.GetKind())
393 {
394 case ProcessMessage::eInvalidMessage:
395 return;
396
397 case ProcessMessage::eAttachMessage:
398 SetPrivateState(eStateStopped);
399 return;
400
401 case ProcessMessage::eLimboMessage:
402 assert(thread);
403 thread->SetState(eStateStopped);
404 if (message.GetTID() == GetID())
405 {
406 m_exit_status = message.GetExitStatus();
407 if (m_exit_now)
408 {
409 SetPrivateState(eStateExited);
410 m_monitor->Detach(GetID());
411 }
412 else
413 {
414 StopAllThreads(message.GetTID());
415 SetPrivateState(eStateStopped);
416 }
417 }
418 else
419 {
420 StopAllThreads(message.GetTID());
421 SetPrivateState(eStateStopped);
422 }
423 break;
424
425 case ProcessMessage::eExitMessage:
Andrew MacPherson82aae0d2014-04-02 06:57:45 +0000426 if (thread != nullptr)
427 thread->SetState(eStateExited);
428 else
429 {
430 if (log)
431 log->Warning ("ProcessPOSIX::%s eExitMessage for TID %" PRIu64 " failed to find a thread to mark as eStateExited, ignoring", __FUNCTION__, message.GetTID ());
432 }
433
Ed Maste30df85e2013-12-11 20:43:27 +0000434 // FIXME: I'm not sure we need to do this.
435 if (message.GetTID() == GetID())
436 {
Todd Fiala7b0917a2014-09-15 20:07:33 +0000437 SetExitStatus(message.GetExitStatus(), NULL);
Ed Maste30df85e2013-12-11 20:43:27 +0000438 }
439 else if (!IsAThreadRunning())
440 SetPrivateState(eStateStopped);
441 break;
442
443 case ProcessMessage::eSignalMessage:
444 case ProcessMessage::eSignalDeliveredMessage:
445 if (message.GetSignal() == SIGSTOP &&
446 AddThreadForInitialStopIfNeeded(message.GetTID()))
447 return;
448 // Intentional fall-through
449
450 case ProcessMessage::eBreakpointMessage:
451 case ProcessMessage::eTraceMessage:
452 case ProcessMessage::eWatchpointMessage:
453 case ProcessMessage::eCrashMessage:
454 assert(thread);
455 thread->SetState(eStateStopped);
456 StopAllThreads(message.GetTID());
457 SetPrivateState(eStateStopped);
458 break;
459
460 case ProcessMessage::eNewThreadMessage:
461 {
462 lldb::tid_t new_tid = message.GetChildTID();
463 if (WaitingForInitialStop(new_tid))
464 {
465 m_monitor->WaitForInitialTIDStop(new_tid);
466 }
467 assert(thread);
468 thread->SetState(eStateStopped);
469 StopAllThreads(message.GetTID());
470 SetPrivateState(eStateStopped);
471 break;
472 }
473
474 case ProcessMessage::eExecMessage:
475 {
476 assert(thread);
477 thread->SetState(eStateStopped);
478 StopAllThreads(message.GetTID());
479 SetPrivateState(eStateStopped);
480 break;
481 }
482 }
483
484
485 m_message_queue.push(message);
486}
487
Andrew Kaylor93132f52013-05-28 23:04:25 +0000488void
489ProcessPOSIX::StopAllThreads(lldb::tid_t stop_tid)
490{
491 // FIXME: Will this work the same way on FreeBSD and Linux?
492}
493
Matt Kopecb2910442013-07-09 15:09:45 +0000494bool
495ProcessPOSIX::AddThreadForInitialStopIfNeeded(lldb::tid_t stop_tid)
496{
497 bool added_to_set = false;
498 ThreadStopSet::iterator it = m_seen_initial_stop.find(stop_tid);
499 if (it == m_seen_initial_stop.end())
500 {
501 m_seen_initial_stop.insert(stop_tid);
502 added_to_set = true;
503 }
504 return added_to_set;
505}
506
Andrew Kaylord4d54992013-09-17 00:30:24 +0000507bool
508ProcessPOSIX::WaitingForInitialStop(lldb::tid_t stop_tid)
509{
510 return (m_seen_initial_stop.find(stop_tid) == m_seen_initial_stop.end());
511}
512
Michael Sartain9f822cd2013-07-31 23:27:46 +0000513POSIXThread *
514ProcessPOSIX::CreateNewPOSIXThread(lldb_private::Process &process, lldb::tid_t tid)
515{
516 return new POSIXThread(process, tid);
517}
518
Johnny Chen9ed5b492012-01-05 21:48:15 +0000519void
520ProcessPOSIX::RefreshStateAfterStop()
521{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000522 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000523 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Andrew Kaylor93132f52013-05-28 23:04:25 +0000524 log->Printf ("ProcessPOSIX::%s(), message_queue size = %d", __FUNCTION__, (int)m_message_queue.size());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000525
526 Mutex::Locker lock(m_message_mutex);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000527
Andrew Kaylor93132f52013-05-28 23:04:25 +0000528 // This method used to only handle one message. Changing it to loop allows
529 // it to handle the case where we hit a breakpoint while handling a different
530 // breakpoint.
531 while (!m_message_queue.empty())
532 {
533 ProcessMessage &message = m_message_queue.front();
Johnny Chen9ed5b492012-01-05 21:48:15 +0000534
Andrew Kaylor93132f52013-05-28 23:04:25 +0000535 // Resolve the thread this message corresponds to and pass it along.
536 lldb::tid_t tid = message.GetTID();
537 if (log)
538 log->Printf ("ProcessPOSIX::%s(), message_queue size = %d, pid = %" PRIi64, __FUNCTION__, (int)m_message_queue.size(), tid);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000539
Andrew Kaylor93132f52013-05-28 23:04:25 +0000540 if (message.GetKind() == ProcessMessage::eNewThreadMessage)
541 {
542 if (log)
543 log->Printf ("ProcessPOSIX::%s() adding thread, tid = %" PRIi64, __FUNCTION__, message.GetChildTID());
Matt Kopecfb6ab542013-07-10 20:53:11 +0000544 lldb::tid_t child_tid = message.GetChildTID();
Andrew Kaylor93132f52013-05-28 23:04:25 +0000545 ThreadSP thread_sp;
Michael Sartain9f822cd2013-07-31 23:27:46 +0000546 thread_sp.reset(CreateNewPOSIXThread(*this, child_tid));
Matt Kopecfb6ab542013-07-10 20:53:11 +0000547
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000548 Mutex::Locker lock(m_thread_list.GetMutex());
549
Andrew Kaylor93132f52013-05-28 23:04:25 +0000550 m_thread_list.AddThread(thread_sp);
551 }
552
553 m_thread_list.RefreshStateAfterStop();
554
Ed Maste685fea92013-08-29 20:40:11 +0000555 POSIXThread *thread = static_cast<POSIXThread*>(
556 GetThreadList().FindThreadByID(tid, false).get());
Andrew Kaylor93132f52013-05-28 23:04:25 +0000557 if (thread)
558 thread->Notify(message);
559
560 if (message.GetKind() == ProcessMessage::eExitMessage)
561 {
562 // FIXME: We should tell the user about this, but the limbo message is probably better for that.
563 if (log)
564 log->Printf ("ProcessPOSIX::%s() removing thread, tid = %" PRIi64, __FUNCTION__, tid);
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000565
566 Mutex::Locker lock(m_thread_list.GetMutex());
567
Andrew Kaylor93132f52013-05-28 23:04:25 +0000568 ThreadSP thread_sp = m_thread_list.RemoveThreadByID(tid, false);
569 thread_sp.reset();
Matt Kopecb2910442013-07-09 15:09:45 +0000570 m_seen_initial_stop.erase(tid);
Andrew Kaylor93132f52013-05-28 23:04:25 +0000571 }
572
573 m_message_queue.pop();
Matt Kopec650648f2013-01-08 16:30:18 +0000574 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000575}
576
577bool
578ProcessPOSIX::IsAlive()
579{
580 StateType state = GetPrivateState();
Daniel Malea335bf6f2013-04-01 19:48:37 +0000581 return state != eStateDetached
582 && state != eStateExited
583 && state != eStateInvalid
584 && state != eStateUnloaded;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000585}
586
587size_t
588ProcessPOSIX::DoReadMemory(addr_t vm_addr,
589 void *buf, size_t size, Error &error)
590{
591 assert(m_monitor);
592 return m_monitor->ReadMemory(vm_addr, buf, size, error);
593}
594
595size_t
596ProcessPOSIX::DoWriteMemory(addr_t vm_addr, const void *buf, size_t size,
597 Error &error)
598{
599 assert(m_monitor);
600 return m_monitor->WriteMemory(vm_addr, buf, size, error);
601}
602
603addr_t
604ProcessPOSIX::DoAllocateMemory(size_t size, uint32_t permissions,
605 Error &error)
606{
607 addr_t allocated_addr = LLDB_INVALID_ADDRESS;
608
609 unsigned prot = 0;
610 if (permissions & lldb::ePermissionsReadable)
611 prot |= eMmapProtRead;
612 if (permissions & lldb::ePermissionsWritable)
613 prot |= eMmapProtWrite;
614 if (permissions & lldb::ePermissionsExecutable)
615 prot |= eMmapProtExec;
616
617 if (InferiorCallMmap(this, allocated_addr, 0, size, prot,
618 eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) {
619 m_addr_to_mmap_size[allocated_addr] = size;
620 error.Clear();
621 } else {
622 allocated_addr = LLDB_INVALID_ADDRESS;
623 error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions));
624 }
625
626 return allocated_addr;
627}
628
629Error
630ProcessPOSIX::DoDeallocateMemory(lldb::addr_t addr)
631{
632 Error error;
633 MMapMap::iterator pos = m_addr_to_mmap_size.find(addr);
634 if (pos != m_addr_to_mmap_size.end() &&
635 InferiorCallMunmap(this, addr, pos->second))
636 m_addr_to_mmap_size.erase (pos);
637 else
Daniel Malead01b2952012-11-29 21:49:15 +0000638 error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr);
Johnny Chen9ed5b492012-01-05 21:48:15 +0000639
640 return error;
641}
642
643size_t
644ProcessPOSIX::GetSoftwareBreakpointTrapOpcode(BreakpointSite* bp_site)
645{
Todd Fiala1d6082f2014-08-27 16:32:02 +0000646 static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xD4 };
Johnny Chen9ed5b492012-01-05 21:48:15 +0000647 static const uint8_t g_i386_opcode[] = { 0xCC };
648
649 ArchSpec arch = GetTarget().GetArchitecture();
650 const uint8_t *opcode = NULL;
651 size_t opcode_size = 0;
652
Greg Clayton906e9ac2014-03-20 21:31:55 +0000653 switch (arch.GetMachine())
Johnny Chen9ed5b492012-01-05 21:48:15 +0000654 {
655 default:
656 assert(false && "CPU type not supported!");
657 break;
658
Todd Fiala1d6082f2014-08-27 16:32:02 +0000659 case llvm::Triple::aarch64:
660 opcode = g_aarch64_opcode;
661 opcode_size = sizeof(g_aarch64_opcode);
662 break;
663
Greg Clayton906e9ac2014-03-20 21:31:55 +0000664 case llvm::Triple::x86:
665 case llvm::Triple::x86_64:
Johnny Chen9ed5b492012-01-05 21:48:15 +0000666 opcode = g_i386_opcode;
667 opcode_size = sizeof(g_i386_opcode);
668 break;
669 }
670
671 bp_site->SetTrapOpcode(opcode, opcode_size);
672 return opcode_size;
673}
674
675Error
Daniel Maleab7eec012013-02-15 20:23:25 +0000676ProcessPOSIX::EnableBreakpointSite(BreakpointSite *bp_site)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000677{
678 return EnableSoftwareBreakpoint(bp_site);
679}
680
681Error
Daniel Maleab7eec012013-02-15 20:23:25 +0000682ProcessPOSIX::DisableBreakpointSite(BreakpointSite *bp_site)
Johnny Chen9ed5b492012-01-05 21:48:15 +0000683{
684 return DisableSoftwareBreakpoint(bp_site);
685}
686
Matt Kopece9ea0da2013-05-07 19:29:28 +0000687Error
688ProcessPOSIX::EnableWatchpoint(Watchpoint *wp, bool notify)
689{
690 Error error;
691 if (wp)
692 {
693 user_id_t watchID = wp->GetID();
694 addr_t addr = wp->GetLoadAddress();
695 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
696 if (log)
697 log->Printf ("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64 ")",
698 watchID);
699 if (wp->IsEnabled())
700 {
701 if (log)
702 log->Printf("ProcessPOSIX::EnableWatchpoint(watchID = %" PRIu64
703 ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.",
704 watchID, (uint64_t)addr);
705 return error;
706 }
707
Matt Kopec6f961232013-06-03 17:40:20 +0000708 // Try to find a vacant watchpoint slot in the inferiors' main thread
709 uint32_t wp_hw_index = LLDB_INVALID_INDEX32;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000710 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopec6f961232013-06-03 17:40:20 +0000711 POSIXThread *thread = static_cast<POSIXThread*>(
712 m_thread_list.GetThreadAtIndex(0, false).get());
713
714 if (thread)
715 wp_hw_index = thread->FindVacantWatchpointIndex();
716
717 if (wp_hw_index == LLDB_INVALID_INDEX32)
Matt Kopece9ea0da2013-05-07 19:29:28 +0000718 {
Matt Kopec6f961232013-06-03 17:40:20 +0000719 error.SetErrorString("Setting hardware watchpoint failed.");
Matt Kopece9ea0da2013-05-07 19:29:28 +0000720 }
721 else
722 {
Matt Kopec6f961232013-06-03 17:40:20 +0000723 wp->SetHardwareIndex(wp_hw_index);
724 bool wp_enabled = true;
725 uint32_t thread_count = m_thread_list.GetSize(false);
726 for (uint32_t i = 0; i < thread_count; ++i)
727 {
728 thread = static_cast<POSIXThread*>(
729 m_thread_list.GetThreadAtIndex(i, false).get());
730 if (thread)
731 wp_enabled &= thread->EnableHardwareWatchpoint(wp);
732 else
733 wp_enabled = false;
734 }
735 if (wp_enabled)
736 {
737 wp->SetEnabled(true, notify);
738 return error;
739 }
740 else
741 {
742 // Watchpoint enabling failed on at least one
743 // of the threads so roll back all of them
744 DisableWatchpoint(wp, false);
745 error.SetErrorString("Setting hardware watchpoint failed");
746 }
Matt Kopece9ea0da2013-05-07 19:29:28 +0000747 }
748 }
749 else
750 error.SetErrorString("Watchpoint argument was NULL.");
751 return error;
752}
753
754Error
755ProcessPOSIX::DisableWatchpoint(Watchpoint *wp, bool notify)
756{
757 Error error;
758 if (wp)
759 {
760 user_id_t watchID = wp->GetID();
761 addr_t addr = wp->GetLoadAddress();
762 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS));
763 if (log)
764 log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64 ")",
765 watchID);
766 if (!wp->IsEnabled())
767 {
768 if (log)
769 log->Printf("ProcessPOSIX::DisableWatchpoint(watchID = %" PRIu64
770 ") addr = 0x%8.8" PRIx64 ": watchpoint already disabled.",
771 watchID, (uint64_t)addr);
772 // This is needed (for now) to keep watchpoints disabled correctly
773 wp->SetEnabled(false, notify);
774 return error;
775 }
776
777 if (wp->IsHardware())
778 {
779 bool wp_disabled = true;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000780 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopece9ea0da2013-05-07 19:29:28 +0000781 uint32_t thread_count = m_thread_list.GetSize(false);
782 for (uint32_t i = 0; i < thread_count; ++i)
783 {
784 POSIXThread *thread = static_cast<POSIXThread*>(
785 m_thread_list.GetThreadAtIndex(i, false).get());
786 if (thread)
787 wp_disabled &= thread->DisableHardwareWatchpoint(wp);
788 else
789 wp_disabled = false;
790 }
791 if (wp_disabled)
792 {
Matt Kopec6f961232013-06-03 17:40:20 +0000793 wp->SetHardwareIndex(LLDB_INVALID_INDEX32);
Matt Kopece9ea0da2013-05-07 19:29:28 +0000794 wp->SetEnabled(false, notify);
795 return error;
796 }
797 else
798 error.SetErrorString("Disabling hardware watchpoint failed");
799 }
800 }
801 else
802 error.SetErrorString("Watchpoint argument was NULL.");
803 return error;
804}
805
806Error
807ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num)
808{
809 Error error;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000810 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopece9ea0da2013-05-07 19:29:28 +0000811 POSIXThread *thread = static_cast<POSIXThread*>(
812 m_thread_list.GetThreadAtIndex(0, false).get());
813 if (thread)
814 num = thread->NumSupportedHardwareWatchpoints();
815 else
816 error.SetErrorString("Process does not exist.");
817 return error;
818}
819
820Error
821ProcessPOSIX::GetWatchpointSupportInfo(uint32_t &num, bool &after)
822{
823 Error error = GetWatchpointSupportInfo(num);
824 // Watchpoints trigger and halt the inferior after
825 // the corresponding instruction has been executed.
826 after = true;
827 return error;
828}
829
Johnny Chen9ed5b492012-01-05 21:48:15 +0000830uint32_t
831ProcessPOSIX::UpdateThreadListIfNeeded()
832{
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000833 Mutex::Locker lock(m_thread_list.GetMutex());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000834 // Do not allow recursive updates.
835 return m_thread_list.GetSize(false);
836}
837
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000838bool
Johnny Chen9ed5b492012-01-05 21:48:15 +0000839ProcessPOSIX::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list)
840{
Ashok Thirumurthi01186352013-03-28 16:02:31 +0000841 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD));
Johnny Chen9ed5b492012-01-05 21:48:15 +0000842 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000843 log->Printf ("ProcessPOSIX::%s() (pid = %" PRIi64 ")", __FUNCTION__, GetID());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000844
Daniel Maleae0f8f572013-08-26 23:57:52 +0000845 bool has_updated = false;
Johnny Chen9ed5b492012-01-05 21:48:15 +0000846 // Update the process thread list with this new thread.
847 // FIXME: We should be using tid, not pid.
848 assert(m_monitor);
849 ThreadSP thread_sp (old_thread_list.FindThreadByID (GetID(), false));
Greg Clayton0c90ef42012-02-21 18:40:07 +0000850 if (!thread_sp) {
Michael Sartain9f822cd2013-07-31 23:27:46 +0000851 thread_sp.reset(CreateNewPOSIXThread(*this, GetID()));
Daniel Maleae0f8f572013-08-26 23:57:52 +0000852 has_updated = true;
Greg Clayton0c90ef42012-02-21 18:40:07 +0000853 }
Johnny Chen9ed5b492012-01-05 21:48:15 +0000854
855 if (log && log->GetMask().Test(POSIX_LOG_VERBOSE))
Daniel Malead01b2952012-11-29 21:49:15 +0000856 log->Printf ("ProcessPOSIX::%s() updated pid = %" PRIi64, __FUNCTION__, GetID());
Johnny Chen9ed5b492012-01-05 21:48:15 +0000857 new_thread_list.AddThread(thread_sp);
858
Daniel Maleae0f8f572013-08-26 23:57:52 +0000859 return has_updated; // the list has been updated
Johnny Chen9ed5b492012-01-05 21:48:15 +0000860}
861
862ByteOrder
863ProcessPOSIX::GetByteOrder() const
864{
865 // FIXME: We should be able to extract this value directly. See comment in
866 // ProcessPOSIX().
867 return m_byte_order;
868}
869
870size_t
871ProcessPOSIX::PutSTDIN(const char *buf, size_t len, Error &error)
872{
873 ssize_t status;
874 if ((status = write(m_monitor->GetTerminalFD(), buf, len)) < 0)
875 {
876 error.SetErrorToErrno();
877 return 0;
878 }
879 return status;
880}
881
Johnny Chen9ed5b492012-01-05 21:48:15 +0000882//------------------------------------------------------------------------------
883// Utility functions.
884
885bool
886ProcessPOSIX::HasExited()
887{
888 switch (GetPrivateState())
889 {
890 default:
891 break;
892
893 case eStateDetached:
894 case eStateExited:
895 return true;
896 }
897
898 return false;
899}
900
901bool
902ProcessPOSIX::IsStopped()
903{
904 switch (GetPrivateState())
905 {
906 default:
907 break;
908
909 case eStateStopped:
910 case eStateCrashed:
911 case eStateSuspended:
912 return true;
913 }
914
915 return false;
916}
Matt Kopecb2910442013-07-09 15:09:45 +0000917
918bool
919ProcessPOSIX::IsAThreadRunning()
920{
921 bool is_running = false;
Daniel Maleaa2cb9c42013-07-24 21:44:30 +0000922 Mutex::Locker lock(m_thread_list.GetMutex());
Matt Kopecb2910442013-07-09 15:09:45 +0000923 uint32_t thread_count = m_thread_list.GetSize(false);
924 for (uint32_t i = 0; i < thread_count; ++i)
925 {
926 POSIXThread *thread = static_cast<POSIXThread*>(
927 m_thread_list.GetThreadAtIndex(i, false).get());
928 StateType thread_state = thread->GetState();
929 if (thread_state == eStateRunning || thread_state == eStateStepping)
930 {
931 is_running = true;
932 break;
933 }
934 }
935 return is_running;
936}
Todd Fialaaf245d12014-06-30 21:05:18 +0000937
938const DataBufferSP
939ProcessPOSIX::GetAuxvData ()
940{
941 // If we're the local platform, we can ask the host for auxv data.
942 PlatformSP platform_sp = m_target.GetPlatform ();
943 if (platform_sp && platform_sp->IsHost ())
944 return lldb_private::Host::GetAuxvData(this);
945
946 // Somewhat unexpected - the process is not running locally or we don't have a platform.
947 assert (false && "no platform or not the host - how did we get here with ProcessPOSIX?");
948 return DataBufferSP ();
949}