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" |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 23 | #include "ProcessPOSIXLog.h" |
Peter Collingbourne | 70969ef | 2011-06-03 20:40:44 +0000 | [diff] [blame] | 24 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 25 | #include "ProcessMonitor.h" |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 26 | #include "POSIXThread.h" |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace lldb; |
| 29 | using namespace lldb_private; |
| 30 | |
| 31 | //------------------------------------------------------------------------------ |
| 32 | // Static functions. |
| 33 | |
Greg Clayton | 0c90ef4 | 2012-02-21 18:40:07 +0000 | [diff] [blame] | 34 | ProcessSP |
Ashok Thirumurthi | c037383 | 2013-07-12 21:25:02 +0000 | [diff] [blame] | 35 | ProcessLinux::CreateInstance(Target &target, Listener &listener, const FileSpec *core_file) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 36 | { |
Ashok Thirumurthi | c037383 | 2013-07-12 21:25:02 +0000 | [diff] [blame] | 37 | return ProcessSP(new ProcessLinux(target, listener, (FileSpec *)core_file)); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void |
| 41 | ProcessLinux::Initialize() |
| 42 | { |
| 43 | static bool g_initialized = false; |
| 44 | |
| 45 | if (!g_initialized) |
| 46 | { |
Johnny Chen | 6dcbeae | 2011-10-11 21:21:57 +0000 | [diff] [blame] | 47 | g_initialized = true; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 48 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 49 | GetPluginDescriptionStatic(), |
| 50 | CreateInstance); |
Johnny Chen | 6dcbeae | 2011-10-11 21:21:57 +0000 | [diff] [blame] | 51 | |
| 52 | Log::Callbacks log_callbacks = { |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 53 | ProcessPOSIXLog::DisableLog, |
| 54 | ProcessPOSIXLog::EnableLog, |
| 55 | ProcessPOSIXLog::ListLogCategories |
Johnny Chen | 6dcbeae | 2011-10-11 21:21:57 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | Log::RegisterLogChannel (ProcessLinux::GetPluginNameStatic(), log_callbacks); |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 59 | ProcessPOSIXLog::RegisterPluginName(GetPluginNameStatic()); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 63 | //------------------------------------------------------------------------------ |
| 64 | // Constructors and destructors. |
| 65 | |
Ashok Thirumurthi | c037383 | 2013-07-12 21:25:02 +0000 | [diff] [blame] | 66 | ProcessLinux::ProcessLinux(Target& target, Listener &listener, FileSpec *core_file) |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 67 | : ProcessPOSIX(target, listener), m_stopping_threads(false) |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 68 | { |
Ashok Thirumurthi | c037383 | 2013-07-12 21:25:02 +0000 | [diff] [blame] | 69 | m_core_file = core_file; |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 70 | #if 0 |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 71 | // FIXME: Putting this code in the ctor and saving the byte order in a |
| 72 | // member variable is a hack to avoid const qual issues in GetByteOrder. |
| 73 | ObjectFile *obj_file = GetTarget().GetExecutableModule()->GetObjectFile(); |
| 74 | m_byte_order = obj_file->GetByteOrder(); |
Greg Clayton | 386ff18 | 2011-11-05 01:09:16 +0000 | [diff] [blame] | 75 | #else |
| 76 | // XXX: Will work only for local processes. |
| 77 | m_byte_order = lldb::endian::InlHostByteOrder(); |
| 78 | #endif |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Stephen Wilson | 2103e25 | 2011-01-16 19:45:39 +0000 | [diff] [blame] | 81 | void |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 82 | ProcessLinux::Terminate() |
Stephen Wilson | 2103e25 | 2011-01-16 19:45:39 +0000 | [diff] [blame] | 83 | { |
Stephen Wilson | 2103e25 | 2011-01-16 19:45:39 +0000 | [diff] [blame] | 84 | } |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 85 | lldb_private::ConstString |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 86 | ProcessLinux::GetPluginNameStatic() |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 87 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 88 | static ConstString g_name("linux"); |
| 89 | return g_name; |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 92 | const char * |
| 93 | ProcessLinux::GetPluginDescriptionStatic() |
Stephen Wilson | c4391cb | 2011-01-15 00:10:37 +0000 | [diff] [blame] | 94 | { |
Johnny Chen | 30213ff | 2012-01-05 19:17:38 +0000 | [diff] [blame] | 95 | return "Process plugin for Linux"; |
Stephen Wilson | c4391cb | 2011-01-15 00:10:37 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 98 | |
Greg Clayton | c3c0b0e | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 99 | bool |
Johnny Chen | 48d042b | 2011-10-10 23:11:50 +0000 | [diff] [blame] | 100 | ProcessLinux::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) |
| 101 | { |
Matt Kopec | 650648f | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 102 | new_thread_list = old_thread_list; |
Greg Clayton | c3c0b0e | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 103 | return new_thread_list.GetSize(false) > 0; |
Johnny Chen | 48d042b | 2011-10-10 23:11:50 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Stephen Wilson | 2697716 | 2011-03-23 02:14:42 +0000 | [diff] [blame] | 106 | |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 107 | //------------------------------------------------------------------------------ |
| 108 | // ProcessInterface protocol. |
| 109 | |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 110 | lldb_private::ConstString |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 111 | ProcessLinux::GetPluginName() |
| 112 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 113 | return GetPluginNameStatic(); |
Stephen Wilson | e6f9f66 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | uint32_t |
| 117 | ProcessLinux::GetPluginVersion() |
| 118 | { |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | ProcessLinux::GetPluginCommandHelp(const char *command, Stream *strm) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | Error |
| 128 | ProcessLinux::ExecutePluginCommand(Args &command, Stream *strm) |
| 129 | { |
| 130 | return Error(1, eErrorTypeGeneric); |
| 131 | } |
| 132 | |
| 133 | Log * |
| 134 | ProcessLinux::EnablePluginLogging(Stream *strm, Args &command) |
| 135 | { |
| 136 | return NULL; |
| 137 | } |
Andrew Kaylor | 93132f5 | 2013-05-28 23:04:25 +0000 | [diff] [blame] | 138 | |
| 139 | // ProcessPOSIX override |
| 140 | void |
| 141 | ProcessLinux::StopAllThreads(lldb::tid_t stop_tid) |
| 142 | { |
| 143 | // If a breakpoint occurs while we're stopping threads, we'll get back |
| 144 | // here, but we don't want to do it again. Only the MonitorChildProcess |
| 145 | // thread calls this function, so we don't need to protect this flag. |
| 146 | if (m_stopping_threads) |
| 147 | return; |
| 148 | m_stopping_threads = true; |
| 149 | |
| 150 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
| 151 | if (log) |
| 152 | log->Printf ("ProcessLinux::%s() stopping all threads", __FUNCTION__); |
| 153 | |
| 154 | // Walk the thread list and stop the other threads. The thread that caused |
| 155 | // the stop should already be marked as stopped before we get here. |
| 156 | Mutex::Locker thread_list_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 | assert(thread); |
| 164 | lldb::tid_t tid = thread->GetID(); |
| 165 | if (!StateIsStoppedState(thread->GetState(), false)) |
| 166 | m_monitor->StopThread(tid); |
| 167 | } |
| 168 | |
| 169 | m_stopping_threads = false; |
| 170 | |
| 171 | if (log) |
| 172 | log->Printf ("ProcessLinux::%s() finished", __FUNCTION__); |
| 173 | } |
Ashok Thirumurthi | c037383 | 2013-07-12 21:25:02 +0000 | [diff] [blame] | 174 | |
| 175 | bool |
| 176 | ProcessLinux::CanDebug(Target &target, bool plugin_specified_by_name) |
| 177 | { |
| 178 | if (plugin_specified_by_name) |
| 179 | return true; |
| 180 | |
| 181 | /* If core file is specified then let elf-core plugin handle it */ |
| 182 | if (m_core_file) |
| 183 | return false; |
| 184 | |
| 185 | return ProcessPOSIX::CanDebug(target, plugin_specified_by_name); |
| 186 | } |
| 187 | |