Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1 | //===-- 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 Wilson | 2697716 | 2011-03-23 02:14:42 +0000 | [diff] [blame] | 11 | #include <errno.h> |
| 12 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | #include "lldb/Core/PluginManager.h" |
Peter Collingbourne | 70969ef | 2011-06-03 20:40:44 +0000 | [diff] [blame] | 16 | #include "lldb/Core/State.h" |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 17 | #include "lldb/Host/Host.h" |
| 18 | #include "lldb/Symbol/ObjectFile.h" |
Stephen Wilson | 2103e25 | 2011-01-16 19:45:39 +0000 | [diff] [blame] | 19 | #include "lldb/Target/DynamicLoader.h" |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 20 | #include "lldb/Target/Target.h" |
| 21 | |
| 22 | #include "ProcessLinux.h" |
Todd Fiala | 348fb38 | 2014-10-10 00:09:16 +0000 | [diff] [blame] | 23 | #include "Plugins/Platform/Linux/PlatformLinux.h" |
Todd Fiala | cacde7d | 2014-09-27 16:54:22 +0000 | [diff] [blame] | 24 | #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" |
Peter Collingbourne | 70969ef | 2011-06-03 20:40:44 +0000 | [diff] [blame] | 25 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 26 | #include "Plugins/Process/Utility/LinuxSignals.h" |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 27 | #include "ProcessMonitor.h" |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 28 | #include "LinuxThread.h" |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace lldb; |
| 31 | using namespace lldb_private; |
| 32 | |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 33 | namespace |
| 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 Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 43 | //------------------------------------------------------------------------------ |
| 44 | // Static functions. |
| 45 | |
Greg Clayton | 0c90ef4 | 2012-02-21 18:40:07 +0000 | [diff] [blame] | 46 | ProcessSP |
Ashok Thirumurthi | 4f01ff8 | 2013-07-17 16:06:12 +0000 | [diff] [blame] | 47 | ProcessLinux::CreateInstance(Target &target, Listener &listener, const FileSpec *core_file) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 48 | { |
Ashok Thirumurthi | 4f01ff8 | 2013-07-17 16:06:12 +0000 | [diff] [blame] | 49 | return ProcessSP(new ProcessLinux(target, listener, (FileSpec *)core_file)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void |
| 53 | ProcessLinux::Initialize() |
| 54 | { |
| 55 | static bool g_initialized = false; |
| 56 | |
| 57 | if (!g_initialized) |
| 58 | { |
Johnny Chen | 6dcbeae | 2011-10-11 21:21:57 +0000 | [diff] [blame] | 59 | g_initialized = true; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 60 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 61 | GetPluginDescriptionStatic(), |
| 62 | CreateInstance); |
Robert Flack | 5f4b6c7 | 2015-03-11 21:14:22 +0000 | [diff] [blame^] | 63 | ProcessPOSIXLog::Initialize(GetPluginNameStatic()); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 67 | //------------------------------------------------------------------------------ |
| 68 | // Constructors and destructors. |
| 69 | |
Ashok Thirumurthi | 4f01ff8 | 2013-07-17 16:06:12 +0000 | [diff] [blame] | 70 | ProcessLinux::ProcessLinux(Target& target, Listener &listener, FileSpec *core_file) |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 71 | : ProcessPOSIX(target, listener, GetStaticLinuxSignalsSP ()), m_core_file(core_file), m_stopping_threads(false) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 72 | { |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 73 | #if 0 |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 74 | // 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 Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 78 | #else |
| 79 | // XXX: Will work only for local processes. |
| 80 | m_byte_order = lldb::endian::InlHostByteOrder(); |
| 81 | #endif |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Stephen Wilson | 2103e25 | 2011-01-16 19:45:39 +0000 | [diff] [blame] | 84 | void |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 85 | ProcessLinux::Terminate() |
Stephen Wilson | 2103e25 | 2011-01-16 19:45:39 +0000 | [diff] [blame] | 86 | { |
Stephen Wilson | 2103e25 | 2011-01-16 19:45:39 +0000 | [diff] [blame] | 87 | } |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 88 | |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 89 | lldb_private::ConstString |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 90 | ProcessLinux::GetPluginNameStatic() |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 91 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 92 | static ConstString g_name("linux"); |
| 93 | return g_name; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 96 | const char * |
| 97 | ProcessLinux::GetPluginDescriptionStatic() |
Stephen Wilson | c4391cb | 2011-01-15 00:10:37 +0000 | [diff] [blame] | 98 | { |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 99 | return "Process plugin for Linux"; |
Stephen Wilson | c4391cb | 2011-01-15 00:10:37 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 102 | |
Greg Clayton | c3c0b0e | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 103 | bool |
Johnny Chen | 48d042b | 2011-10-10 23:11:50 +0000 | [diff] [blame] | 104 | ProcessLinux::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) |
| 105 | { |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 106 | new_thread_list = old_thread_list; |
Greg Clayton | c3c0b0e | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 107 | return new_thread_list.GetSize(false) > 0; |
Johnny Chen | 48d042b | 2011-10-10 23:11:50 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Stephen Wilson | 2697716 | 2011-03-23 02:14:42 +0000 | [diff] [blame] | 110 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 111 | //------------------------------------------------------------------------------ |
| 112 | // ProcessInterface protocol. |
| 113 | |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 114 | lldb_private::ConstString |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 115 | ProcessLinux::GetPluginName() |
| 116 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 117 | return GetPluginNameStatic(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | uint32_t |
| 121 | ProcessLinux::GetPluginVersion() |
| 122 | { |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | ProcessLinux::GetPluginCommandHelp(const char *command, Stream *strm) |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | Error |
| 132 | ProcessLinux::ExecutePluginCommand(Args &command, Stream *strm) |
| 133 | { |
| 134 | return Error(1, eErrorTypeGeneric); |
| 135 | } |
| 136 | |
| 137 | Log * |
| 138 | ProcessLinux::EnablePluginLogging(Stream *strm, Args &command) |
| 139 | { |
| 140 | return NULL; |
| 141 | } |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 142 | |
Ed Maste | 7dcb77d | 2013-08-30 13:11:30 +0000 | [diff] [blame] | 143 | Error |
| 144 | ProcessLinux::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 Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 172 | // ProcessPOSIX override |
| 173 | void |
| 174 | ProcessLinux::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 Thirumurthi | 4f01ff8 | 2013-07-17 16:06:12 +0000 | [diff] [blame] | 207 | |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 208 | // ProcessPOSIX override |
| 209 | POSIXThread * |
| 210 | ProcessLinux::CreateNewPOSIXThread(lldb_private::Process &process, lldb::tid_t tid) |
| 211 | { |
| 212 | return new LinuxThread(process, tid); |
| 213 | } |
| 214 | |
Ashok Thirumurthi | 4f01ff8 | 2013-07-17 16:06:12 +0000 | [diff] [blame] | 215 | bool |
| 216 | ProcessLinux::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 Fiala | 348fb38 | 2014-10-10 00:09:16 +0000 | [diff] [blame] | 225 | // 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 Thirumurthi | 4f01ff8 | 2013-07-17 16:06:12 +0000 | [diff] [blame] | 230 | return ProcessPOSIX::CanDebug(target, plugin_specified_by_name); |
| 231 | } |
| 232 | |