blob: 303f1e2b442a32dfdf0b6acc523d10ab22d06200 [file] [log] [blame]
Stephen Wilsone6f9f662010-07-24 02:19:04 +00001//===-- ProcessLinux.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
10// C Includes
Stephen Wilson26977162011-03-23 02:14:42 +000011#include <errno.h>
12
Stephen Wilsone6f9f662010-07-24 02:19:04 +000013// C++ Includes
Benjamin Kramer3f69fa62015-04-03 10:55:00 +000014#include <mutex>
15
Stephen Wilsone6f9f662010-07-24 02:19:04 +000016// Other libraries and framework includes
17#include "lldb/Core/PluginManager.h"
Peter Collingbourne70969ef2011-06-03 20:40:44 +000018#include "lldb/Core/State.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000019#include "lldb/Host/Host.h"
20#include "lldb/Symbol/ObjectFile.h"
Stephen Wilson2103e252011-01-16 19:45:39 +000021#include "lldb/Target/DynamicLoader.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000022#include "lldb/Target/Target.h"
23
24#include "ProcessLinux.h"
Todd Fiala348fb382014-10-10 00:09:16 +000025#include "Plugins/Platform/Linux/PlatformLinux.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000026#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
Peter Collingbourne70969ef2011-06-03 20:40:44 +000027#include "Plugins/Process/Utility/InferiorCallPOSIX.h"
Todd Fiala4ceced32014-08-29 17:35:57 +000028#include "Plugins/Process/Utility/LinuxSignals.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000029#include "ProcessMonitor.h"
Michael Sartain9f822cd2013-07-31 23:27:46 +000030#include "LinuxThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000031
32using namespace lldb;
33using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000034using namespace lldb_private::process_linux;
Stephen Wilsone6f9f662010-07-24 02:19:04 +000035
Todd Fiala4ceced32014-08-29 17:35:57 +000036namespace
37{
38 UnixSignalsSP&
39 GetStaticLinuxSignalsSP ()
40 {
41 static UnixSignalsSP s_unix_signals_sp (new process_linux::LinuxSignals ());
42 return s_unix_signals_sp;
43 }
44}
45
Stephen Wilsone6f9f662010-07-24 02:19:04 +000046//------------------------------------------------------------------------------
47// Static functions.
48
Greg Clayton0c90ef42012-02-21 18:40:07 +000049ProcessSP
Ashok Thirumurthi4f01ff82013-07-17 16:06:12 +000050ProcessLinux::CreateInstance(Target &target, Listener &listener, const FileSpec *core_file)
Stephen Wilsone6f9f662010-07-24 02:19:04 +000051{
Vince Harrond7e6a4f2015-05-13 00:25:54 +000052 return ProcessSP(new ProcessLinux(target, listener, core_file));
Stephen Wilsone6f9f662010-07-24 02:19:04 +000053}
54
55void
56ProcessLinux::Initialize()
57{
Davide Italianoc8d69822015-04-03 04:24:32 +000058 static std::once_flag g_once_flag;
Stephen Wilsone6f9f662010-07-24 02:19:04 +000059
Davide Italianoc8d69822015-04-03 04:24:32 +000060 std::call_once(g_once_flag, []() {
Stephen Wilsone6f9f662010-07-24 02:19:04 +000061 PluginManager::RegisterPlugin(GetPluginNameStatic(),
62 GetPluginDescriptionStatic(),
63 CreateInstance);
Robert Flack5f4b6c72015-03-11 21:14:22 +000064 ProcessPOSIXLog::Initialize(GetPluginNameStatic());
Davide Italianoc8d69822015-04-03 04:24:32 +000065 });
Stephen Wilsone6f9f662010-07-24 02:19:04 +000066}
67
Stephen Wilsone6f9f662010-07-24 02:19:04 +000068//------------------------------------------------------------------------------
69// Constructors and destructors.
70
Vince Harrond7e6a4f2015-05-13 00:25:54 +000071ProcessLinux::ProcessLinux(Target& target, Listener &listener, const FileSpec *core_file)
Todd Fiala4ceced32014-08-29 17:35:57 +000072 : ProcessPOSIX(target, listener, GetStaticLinuxSignalsSP ()), m_core_file(core_file), m_stopping_threads(false)
Stephen Wilsone6f9f662010-07-24 02:19:04 +000073{
Greg Clayton386ff182011-11-05 01:09:16 +000074#if 0
Stephen Wilsone6f9f662010-07-24 02:19:04 +000075 // FIXME: Putting this code in the ctor and saving the byte order in a
76 // member variable is a hack to avoid const qual issues in GetByteOrder.
77 ObjectFile *obj_file = GetTarget().GetExecutableModule()->GetObjectFile();
78 m_byte_order = obj_file->GetByteOrder();
Greg Clayton386ff182011-11-05 01:09:16 +000079#else
80 // XXX: Will work only for local processes.
81 m_byte_order = lldb::endian::InlHostByteOrder();
82#endif
Stephen Wilsone6f9f662010-07-24 02:19:04 +000083}
84
Stephen Wilson2103e252011-01-16 19:45:39 +000085void
Johnny Chen30213ff2012-01-05 19:17:38 +000086ProcessLinux::Terminate()
Stephen Wilson2103e252011-01-16 19:45:39 +000087{
Stephen Wilson2103e252011-01-16 19:45:39 +000088}
Michael Sartain9f822cd2013-07-31 23:27:46 +000089
Greg Clayton57abc5d2013-05-10 21:47:16 +000090lldb_private::ConstString
Johnny Chen30213ff2012-01-05 19:17:38 +000091ProcessLinux::GetPluginNameStatic()
Stephen Wilsone6f9f662010-07-24 02:19:04 +000092{
Greg Clayton57abc5d2013-05-10 21:47:16 +000093 static ConstString g_name("linux");
94 return g_name;
Stephen Wilsone6f9f662010-07-24 02:19:04 +000095}
96
Johnny Chen30213ff2012-01-05 19:17:38 +000097const char *
98ProcessLinux::GetPluginDescriptionStatic()
Stephen Wilsonc4391cb2011-01-15 00:10:37 +000099{
Johnny Chen30213ff2012-01-05 19:17:38 +0000100 return "Process plugin for Linux";
Stephen Wilsonc4391cb2011-01-15 00:10:37 +0000101}
102
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000103
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000104bool
Johnny Chen48d042b2011-10-10 23:11:50 +0000105ProcessLinux::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list)
106{
Matt Kopec650648f2013-01-08 16:30:18 +0000107 new_thread_list = old_thread_list;
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000108 return new_thread_list.GetSize(false) > 0;
Johnny Chen48d042b2011-10-10 23:11:50 +0000109}
110
Stephen Wilson26977162011-03-23 02:14:42 +0000111
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000112//------------------------------------------------------------------------------
113// ProcessInterface protocol.
114
Greg Clayton57abc5d2013-05-10 21:47:16 +0000115lldb_private::ConstString
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000116ProcessLinux::GetPluginName()
117{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000118 return GetPluginNameStatic();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000119}
120
121uint32_t
122ProcessLinux::GetPluginVersion()
123{
124 return 1;
125}
126
127void
128ProcessLinux::GetPluginCommandHelp(const char *command, Stream *strm)
129{
130}
131
132Error
133ProcessLinux::ExecutePluginCommand(Args &command, Stream *strm)
134{
135 return Error(1, eErrorTypeGeneric);
136}
137
138Log *
139ProcessLinux::EnablePluginLogging(Stream *strm, Args &command)
140{
141 return NULL;
142}
Andrew Kaylor93132f52013-05-28 23:04:25 +0000143
Ed Maste7dcb77d2013-08-30 13:11:30 +0000144Error
145ProcessLinux::DoDetach(bool keep_stopped)
146{
147 Error error;
148 if (keep_stopped)
149 {
150 // FIXME: If you want to implement keep_stopped,
151 // this would be the place to do it.
152 error.SetErrorString("Detaching with keep_stopped true is not currently supported on Linux.");
153 return error;
154 }
155
156 Mutex::Locker lock(m_thread_list.GetMutex());
157
158 uint32_t thread_count = m_thread_list.GetSize(false);
159 for (uint32_t i = 0; i < thread_count; ++i)
160 {
161 POSIXThread *thread = static_cast<POSIXThread*>(
162 m_thread_list.GetThreadAtIndex(i, false).get());
163 error = m_monitor->Detach(thread->GetID());
164 }
165
166 if (error.Success())
167 SetPrivateState(eStateDetached);
168
169 return error;
170}
171
172
Andrew Kaylor93132f52013-05-28 23:04:25 +0000173// ProcessPOSIX override
174void
175ProcessLinux::StopAllThreads(lldb::tid_t stop_tid)
176{
177 // If a breakpoint occurs while we're stopping threads, we'll get back
178 // here, but we don't want to do it again. Only the MonitorChildProcess
179 // thread calls this function, so we don't need to protect this flag.
180 if (m_stopping_threads)
181 return;
182 m_stopping_threads = true;
183
184 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
185 if (log)
186 log->Printf ("ProcessLinux::%s() stopping all threads", __FUNCTION__);
187
188 // Walk the thread list and stop the other threads. The thread that caused
189 // the stop should already be marked as stopped before we get here.
190 Mutex::Locker thread_list_lock(m_thread_list.GetMutex());
191
192 uint32_t thread_count = m_thread_list.GetSize(false);
193 for (uint32_t i = 0; i < thread_count; ++i)
194 {
195 POSIXThread *thread = static_cast<POSIXThread*>(
196 m_thread_list.GetThreadAtIndex(i, false).get());
197 assert(thread);
198 lldb::tid_t tid = thread->GetID();
199 if (!StateIsStoppedState(thread->GetState(), false))
200 m_monitor->StopThread(tid);
201 }
202
203 m_stopping_threads = false;
204
205 if (log)
206 log->Printf ("ProcessLinux::%s() finished", __FUNCTION__);
207}
Ashok Thirumurthi4f01ff82013-07-17 16:06:12 +0000208
Michael Sartain9f822cd2013-07-31 23:27:46 +0000209// ProcessPOSIX override
210POSIXThread *
211ProcessLinux::CreateNewPOSIXThread(lldb_private::Process &process, lldb::tid_t tid)
212{
213 return new LinuxThread(process, tid);
214}
215
Ashok Thirumurthi4f01ff82013-07-17 16:06:12 +0000216bool
217ProcessLinux::CanDebug(Target &target, bool plugin_specified_by_name)
218{
219 if (plugin_specified_by_name)
220 return true;
221
222 /* If core file is specified then let elf-core plugin handle it */
223 if (m_core_file)
224 return false;
225
Todd Fiala348fb382014-10-10 00:09:16 +0000226 // If we're using llgs for local debugging, we must not say that this process
227 // is used for debugging.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000228 if (platform_linux::PlatformLinux::UseLlgsForLocalDebugging ())
Todd Fiala348fb382014-10-10 00:09:16 +0000229 return false;
230
Ashok Thirumurthi4f01ff82013-07-17 16:06:12 +0000231 return ProcessPOSIX::CanDebug(target, plugin_specified_by_name);
232}
233