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; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 32 | using namespace lldb_private::process_linux; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 33 | |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 34 | namespace |
| 35 | { |
| 36 | UnixSignalsSP& |
| 37 | GetStaticLinuxSignalsSP () |
| 38 | { |
| 39 | static UnixSignalsSP s_unix_signals_sp (new process_linux::LinuxSignals ()); |
| 40 | return s_unix_signals_sp; |
| 41 | } |
| 42 | } |
| 43 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 44 | //------------------------------------------------------------------------------ |
| 45 | // Static functions. |
| 46 | |
Greg Clayton | 0c90ef4 | 2012-02-21 18:40:07 +0000 | [diff] [blame] | 47 | ProcessSP |
Ashok Thirumurthi | 4f01ff8 | 2013-07-17 16:06:12 +0000 | [diff] [blame] | 48 | ProcessLinux::CreateInstance(Target &target, Listener &listener, const FileSpec *core_file) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 49 | { |
Ashok Thirumurthi | 4f01ff8 | 2013-07-17 16:06:12 +0000 | [diff] [blame] | 50 | return ProcessSP(new ProcessLinux(target, listener, (FileSpec *)core_file)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void |
| 54 | ProcessLinux::Initialize() |
| 55 | { |
Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 56 | static std::once_flag g_once_flag; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 57 | |
Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 58 | std::call_once(g_once_flag, []() { |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 59 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 60 | GetPluginDescriptionStatic(), |
| 61 | CreateInstance); |
Robert Flack | 5f4b6c7 | 2015-03-11 21:14:22 +0000 | [diff] [blame] | 62 | ProcessPOSIXLog::Initialize(GetPluginNameStatic()); |
Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 63 | }); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 66 | //------------------------------------------------------------------------------ |
| 67 | // Constructors and destructors. |
| 68 | |
Ashok Thirumurthi | 4f01ff8 | 2013-07-17 16:06:12 +0000 | [diff] [blame] | 69 | ProcessLinux::ProcessLinux(Target& target, Listener &listener, FileSpec *core_file) |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 70 | : 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] | 71 | { |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 72 | #if 0 |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 73 | // FIXME: Putting this code in the ctor and saving the byte order in a |
| 74 | // member variable is a hack to avoid const qual issues in GetByteOrder. |
| 75 | ObjectFile *obj_file = GetTarget().GetExecutableModule()->GetObjectFile(); |
| 76 | m_byte_order = obj_file->GetByteOrder(); |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 77 | #else |
| 78 | // XXX: Will work only for local processes. |
| 79 | m_byte_order = lldb::endian::InlHostByteOrder(); |
| 80 | #endif |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Stephen Wilson | 2103e25 | 2011-01-16 19:45:39 +0000 | [diff] [blame] | 83 | void |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 84 | ProcessLinux::Terminate() |
Stephen Wilson | 2103e25 | 2011-01-16 19:45:39 +0000 | [diff] [blame] | 85 | { |
Stephen Wilson | 2103e25 | 2011-01-16 19:45:39 +0000 | [diff] [blame] | 86 | } |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 87 | |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 88 | lldb_private::ConstString |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 89 | ProcessLinux::GetPluginNameStatic() |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 90 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 91 | static ConstString g_name("linux"); |
| 92 | return g_name; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 95 | const char * |
| 96 | ProcessLinux::GetPluginDescriptionStatic() |
Stephen Wilson | c4391cb | 2011-01-15 00:10:37 +0000 | [diff] [blame] | 97 | { |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 98 | return "Process plugin for Linux"; |
Stephen Wilson | c4391cb | 2011-01-15 00:10:37 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 101 | |
Greg Clayton | c3c0b0e | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 102 | bool |
Johnny Chen | 48d042b | 2011-10-10 23:11:50 +0000 | [diff] [blame] | 103 | ProcessLinux::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) |
| 104 | { |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 105 | new_thread_list = old_thread_list; |
Greg Clayton | c3c0b0e | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 106 | return new_thread_list.GetSize(false) > 0; |
Johnny Chen | 48d042b | 2011-10-10 23:11:50 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Stephen Wilson | 2697716 | 2011-03-23 02:14:42 +0000 | [diff] [blame] | 109 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 110 | //------------------------------------------------------------------------------ |
| 111 | // ProcessInterface protocol. |
| 112 | |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 113 | lldb_private::ConstString |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 114 | ProcessLinux::GetPluginName() |
| 115 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 116 | return GetPluginNameStatic(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | uint32_t |
| 120 | ProcessLinux::GetPluginVersion() |
| 121 | { |
| 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | ProcessLinux::GetPluginCommandHelp(const char *command, Stream *strm) |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | Error |
| 131 | ProcessLinux::ExecutePluginCommand(Args &command, Stream *strm) |
| 132 | { |
| 133 | return Error(1, eErrorTypeGeneric); |
| 134 | } |
| 135 | |
| 136 | Log * |
| 137 | ProcessLinux::EnablePluginLogging(Stream *strm, Args &command) |
| 138 | { |
| 139 | return NULL; |
| 140 | } |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 141 | |
Ed Maste | 7dcb77d | 2013-08-30 13:11:30 +0000 | [diff] [blame] | 142 | Error |
| 143 | ProcessLinux::DoDetach(bool keep_stopped) |
| 144 | { |
| 145 | Error error; |
| 146 | if (keep_stopped) |
| 147 | { |
| 148 | // FIXME: If you want to implement keep_stopped, |
| 149 | // this would be the place to do it. |
| 150 | error.SetErrorString("Detaching with keep_stopped true is not currently supported on Linux."); |
| 151 | return error; |
| 152 | } |
| 153 | |
| 154 | Mutex::Locker lock(m_thread_list.GetMutex()); |
| 155 | |
| 156 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 157 | for (uint32_t i = 0; i < thread_count; ++i) |
| 158 | { |
| 159 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 160 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 161 | error = m_monitor->Detach(thread->GetID()); |
| 162 | } |
| 163 | |
| 164 | if (error.Success()) |
| 165 | SetPrivateState(eStateDetached); |
| 166 | |
| 167 | return error; |
| 168 | } |
| 169 | |
| 170 | |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 171 | // ProcessPOSIX override |
| 172 | void |
| 173 | ProcessLinux::StopAllThreads(lldb::tid_t stop_tid) |
| 174 | { |
| 175 | // If a breakpoint occurs while we're stopping threads, we'll get back |
| 176 | // here, but we don't want to do it again. Only the MonitorChildProcess |
| 177 | // thread calls this function, so we don't need to protect this flag. |
| 178 | if (m_stopping_threads) |
| 179 | return; |
| 180 | m_stopping_threads = true; |
| 181 | |
| 182 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
| 183 | if (log) |
| 184 | log->Printf ("ProcessLinux::%s() stopping all threads", __FUNCTION__); |
| 185 | |
| 186 | // Walk the thread list and stop the other threads. The thread that caused |
| 187 | // the stop should already be marked as stopped before we get here. |
| 188 | Mutex::Locker thread_list_lock(m_thread_list.GetMutex()); |
| 189 | |
| 190 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 191 | for (uint32_t i = 0; i < thread_count; ++i) |
| 192 | { |
| 193 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 194 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 195 | assert(thread); |
| 196 | lldb::tid_t tid = thread->GetID(); |
| 197 | if (!StateIsStoppedState(thread->GetState(), false)) |
| 198 | m_monitor->StopThread(tid); |
| 199 | } |
| 200 | |
| 201 | m_stopping_threads = false; |
| 202 | |
| 203 | if (log) |
| 204 | log->Printf ("ProcessLinux::%s() finished", __FUNCTION__); |
| 205 | } |
Ashok Thirumurthi | 4f01ff8 | 2013-07-17 16:06:12 +0000 | [diff] [blame] | 206 | |
Michael Sartain | 9f822cd | 2013-07-31 23:27:46 +0000 | [diff] [blame] | 207 | // ProcessPOSIX override |
| 208 | POSIXThread * |
| 209 | ProcessLinux::CreateNewPOSIXThread(lldb_private::Process &process, lldb::tid_t tid) |
| 210 | { |
| 211 | return new LinuxThread(process, tid); |
| 212 | } |
| 213 | |
Ashok Thirumurthi | 4f01ff8 | 2013-07-17 16:06:12 +0000 | [diff] [blame] | 214 | bool |
| 215 | ProcessLinux::CanDebug(Target &target, bool plugin_specified_by_name) |
| 216 | { |
| 217 | if (plugin_specified_by_name) |
| 218 | return true; |
| 219 | |
| 220 | /* If core file is specified then let elf-core plugin handle it */ |
| 221 | if (m_core_file) |
| 222 | return false; |
| 223 | |
Todd Fiala | 348fb38 | 2014-10-10 00:09:16 +0000 | [diff] [blame] | 224 | // If we're using llgs for local debugging, we must not say that this process |
| 225 | // is used for debugging. |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 226 | if (platform_linux::PlatformLinux::UseLlgsForLocalDebugging ()) |
Todd Fiala | 348fb38 | 2014-10-10 00:09:16 +0000 | [diff] [blame] | 227 | return false; |
| 228 | |
Ashok Thirumurthi | 4f01ff8 | 2013-07-17 16:06:12 +0000 | [diff] [blame] | 229 | return ProcessPOSIX::CanDebug(target, plugin_specified_by_name); |
| 230 | } |
| 231 | |