blob: d63528427ca469940e45451c56731967f37854db [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
14// Other libraries and framework includes
15#include "lldb/Core/PluginManager.h"
Peter Collingbourne70969ef2011-06-03 20:40:44 +000016#include "lldb/Core/State.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000017#include "lldb/Host/Host.h"
18#include "lldb/Symbol/ObjectFile.h"
Stephen Wilson2103e252011-01-16 19:45:39 +000019#include "lldb/Target/DynamicLoader.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000020#include "lldb/Target/Target.h"
21
22#include "ProcessLinux.h"
Todd Fiala348fb382014-10-10 00:09:16 +000023#include "Plugins/Platform/Linux/PlatformLinux.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000024#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
Peter Collingbourne70969ef2011-06-03 20:40:44 +000025#include "Plugins/Process/Utility/InferiorCallPOSIX.h"
Todd Fiala4ceced32014-08-29 17:35:57 +000026#include "Plugins/Process/Utility/LinuxSignals.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000027#include "ProcessMonitor.h"
Michael Sartain9f822cd2013-07-31 23:27:46 +000028#include "LinuxThread.h"
Stephen Wilsone6f9f662010-07-24 02:19:04 +000029
30using namespace lldb;
31using namespace lldb_private;
32
Todd Fiala4ceced32014-08-29 17:35:57 +000033namespace
34{
35 UnixSignalsSP&
36 GetStaticLinuxSignalsSP ()
37 {
38 static UnixSignalsSP s_unix_signals_sp (new process_linux::LinuxSignals ());
39 return s_unix_signals_sp;
40 }
41}
42
Stephen Wilsone6f9f662010-07-24 02:19:04 +000043//------------------------------------------------------------------------------
44// Static functions.
45
Greg Clayton0c90ef42012-02-21 18:40:07 +000046ProcessSP
Ashok Thirumurthi4f01ff82013-07-17 16:06:12 +000047ProcessLinux::CreateInstance(Target &target, Listener &listener, const FileSpec *core_file)
Stephen Wilsone6f9f662010-07-24 02:19:04 +000048{
Ashok Thirumurthi4f01ff82013-07-17 16:06:12 +000049 return ProcessSP(new ProcessLinux(target, listener, (FileSpec *)core_file));
Stephen Wilsone6f9f662010-07-24 02:19:04 +000050}
51
52void
53ProcessLinux::Initialize()
54{
55 static bool g_initialized = false;
56
57 if (!g_initialized)
58 {
Johnny Chen6dcbeae2011-10-11 21:21:57 +000059 g_initialized = true;
Stephen Wilsone6f9f662010-07-24 02:19:04 +000060 PluginManager::RegisterPlugin(GetPluginNameStatic(),
61 GetPluginDescriptionStatic(),
62 CreateInstance);
Robert Flack5f4b6c72015-03-11 21:14:22 +000063 ProcessPOSIXLog::Initialize(GetPluginNameStatic());
Stephen Wilsone6f9f662010-07-24 02:19:04 +000064 }
65}
66
Stephen Wilsone6f9f662010-07-24 02:19:04 +000067//------------------------------------------------------------------------------
68// Constructors and destructors.
69
Ashok Thirumurthi4f01ff82013-07-17 16:06:12 +000070ProcessLinux::ProcessLinux(Target& target, Listener &listener, FileSpec *core_file)
Todd Fiala4ceced32014-08-29 17:35:57 +000071 : ProcessPOSIX(target, listener, GetStaticLinuxSignalsSP ()), m_core_file(core_file), m_stopping_threads(false)
Stephen Wilsone6f9f662010-07-24 02:19:04 +000072{
Greg Clayton386ff182011-11-05 01:09:16 +000073#if 0
Stephen Wilsone6f9f662010-07-24 02:19:04 +000074 // FIXME: Putting this code in the ctor and saving the byte order in a
75 // member variable is a hack to avoid const qual issues in GetByteOrder.
76 ObjectFile *obj_file = GetTarget().GetExecutableModule()->GetObjectFile();
77 m_byte_order = obj_file->GetByteOrder();
Greg Clayton386ff182011-11-05 01:09:16 +000078#else
79 // XXX: Will work only for local processes.
80 m_byte_order = lldb::endian::InlHostByteOrder();
81#endif
Stephen Wilsone6f9f662010-07-24 02:19:04 +000082}
83
Stephen Wilson2103e252011-01-16 19:45:39 +000084void
Johnny Chen30213ff2012-01-05 19:17:38 +000085ProcessLinux::Terminate()
Stephen Wilson2103e252011-01-16 19:45:39 +000086{
Stephen Wilson2103e252011-01-16 19:45:39 +000087}
Michael Sartain9f822cd2013-07-31 23:27:46 +000088
Greg Clayton57abc5d2013-05-10 21:47:16 +000089lldb_private::ConstString
Johnny Chen30213ff2012-01-05 19:17:38 +000090ProcessLinux::GetPluginNameStatic()
Stephen Wilsone6f9f662010-07-24 02:19:04 +000091{
Greg Clayton57abc5d2013-05-10 21:47:16 +000092 static ConstString g_name("linux");
93 return g_name;
Stephen Wilsone6f9f662010-07-24 02:19:04 +000094}
95
Johnny Chen30213ff2012-01-05 19:17:38 +000096const char *
97ProcessLinux::GetPluginDescriptionStatic()
Stephen Wilsonc4391cb2011-01-15 00:10:37 +000098{
Johnny Chen30213ff2012-01-05 19:17:38 +000099 return "Process plugin for Linux";
Stephen Wilsonc4391cb2011-01-15 00:10:37 +0000100}
101
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000102
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000103bool
Johnny Chen48d042b2011-10-10 23:11:50 +0000104ProcessLinux::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list)
105{
Matt Kopec650648f2013-01-08 16:30:18 +0000106 new_thread_list = old_thread_list;
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000107 return new_thread_list.GetSize(false) > 0;
Johnny Chen48d042b2011-10-10 23:11:50 +0000108}
109
Stephen Wilson26977162011-03-23 02:14:42 +0000110
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000111//------------------------------------------------------------------------------
112// ProcessInterface protocol.
113
Greg Clayton57abc5d2013-05-10 21:47:16 +0000114lldb_private::ConstString
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000115ProcessLinux::GetPluginName()
116{
Greg Clayton57abc5d2013-05-10 21:47:16 +0000117 return GetPluginNameStatic();
Stephen Wilsone6f9f662010-07-24 02:19:04 +0000118}
119
120uint32_t
121ProcessLinux::GetPluginVersion()
122{
123 return 1;
124}
125
126void
127ProcessLinux::GetPluginCommandHelp(const char *command, Stream *strm)
128{
129}
130
131Error
132ProcessLinux::ExecutePluginCommand(Args &command, Stream *strm)
133{
134 return Error(1, eErrorTypeGeneric);
135}
136
137Log *
138ProcessLinux::EnablePluginLogging(Stream *strm, Args &command)
139{
140 return NULL;
141}
Andrew Kaylor93132f52013-05-28 23:04:25 +0000142
Ed Maste7dcb77d2013-08-30 13:11:30 +0000143Error
144ProcessLinux::DoDetach(bool keep_stopped)
145{
146 Error error;
147 if (keep_stopped)
148 {
149 // FIXME: If you want to implement keep_stopped,
150 // this would be the place to do it.
151 error.SetErrorString("Detaching with keep_stopped true is not currently supported on Linux.");
152 return error;
153 }
154
155 Mutex::Locker lock(m_thread_list.GetMutex());
156
157 uint32_t thread_count = m_thread_list.GetSize(false);
158 for (uint32_t i = 0; i < thread_count; ++i)
159 {
160 POSIXThread *thread = static_cast<POSIXThread*>(
161 m_thread_list.GetThreadAtIndex(i, false).get());
162 error = m_monitor->Detach(thread->GetID());
163 }
164
165 if (error.Success())
166 SetPrivateState(eStateDetached);
167
168 return error;
169}
170
171
Andrew Kaylor93132f52013-05-28 23:04:25 +0000172// ProcessPOSIX override
173void
174ProcessLinux::StopAllThreads(lldb::tid_t stop_tid)
175{
176 // If a breakpoint occurs while we're stopping threads, we'll get back
177 // here, but we don't want to do it again. Only the MonitorChildProcess
178 // thread calls this function, so we don't need to protect this flag.
179 if (m_stopping_threads)
180 return;
181 m_stopping_threads = true;
182
183 Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS));
184 if (log)
185 log->Printf ("ProcessLinux::%s() stopping all threads", __FUNCTION__);
186
187 // Walk the thread list and stop the other threads. The thread that caused
188 // the stop should already be marked as stopped before we get here.
189 Mutex::Locker thread_list_lock(m_thread_list.GetMutex());
190
191 uint32_t thread_count = m_thread_list.GetSize(false);
192 for (uint32_t i = 0; i < thread_count; ++i)
193 {
194 POSIXThread *thread = static_cast<POSIXThread*>(
195 m_thread_list.GetThreadAtIndex(i, false).get());
196 assert(thread);
197 lldb::tid_t tid = thread->GetID();
198 if (!StateIsStoppedState(thread->GetState(), false))
199 m_monitor->StopThread(tid);
200 }
201
202 m_stopping_threads = false;
203
204 if (log)
205 log->Printf ("ProcessLinux::%s() finished", __FUNCTION__);
206}
Ashok Thirumurthi4f01ff82013-07-17 16:06:12 +0000207
Michael Sartain9f822cd2013-07-31 23:27:46 +0000208// ProcessPOSIX override
209POSIXThread *
210ProcessLinux::CreateNewPOSIXThread(lldb_private::Process &process, lldb::tid_t tid)
211{
212 return new LinuxThread(process, tid);
213}
214
Ashok Thirumurthi4f01ff82013-07-17 16:06:12 +0000215bool
216ProcessLinux::CanDebug(Target &target, bool plugin_specified_by_name)
217{
218 if (plugin_specified_by_name)
219 return true;
220
221 /* If core file is specified then let elf-core plugin handle it */
222 if (m_core_file)
223 return false;
224
Todd Fiala348fb382014-10-10 00:09:16 +0000225 // If we're using llgs for local debugging, we must not say that this process
226 // is used for debugging.
227 if (PlatformLinux::UseLlgsForLocalDebugging ())
228 return false;
229
Ashok Thirumurthi4f01ff82013-07-17 16:06:12 +0000230 return ProcessPOSIX::CanDebug(target, plugin_specified_by_name);
231}
232