Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1 | //===-- ProcessFreeBSD.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 |
| 11 | #include <errno.h> |
| 12 | |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | #include "lldb/Core/PluginManager.h" |
| 16 | #include "lldb/Core/State.h" |
| 17 | #include "lldb/Host/Host.h" |
| 18 | #include "lldb/Symbol/ObjectFile.h" |
| 19 | #include "lldb/Target/DynamicLoader.h" |
| 20 | #include "lldb/Target/Target.h" |
| 21 | |
| 22 | #include "ProcessFreeBSD.h" |
| 23 | #include "ProcessPOSIXLog.h" |
| 24 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 25 | #include "Plugins/Process/Utility/FreeBSDSignals.h" |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 26 | #include "ProcessMonitor.h" |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 27 | #include "FreeBSDThread.h" |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace lldb; |
| 30 | using namespace lldb_private; |
| 31 | |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 32 | namespace |
| 33 | { |
| 34 | UnixSignalsSP& |
| 35 | GetFreeBSDSignals () |
| 36 | { |
| 37 | static UnixSignalsSP s_freebsd_signals_sp (new FreeBSDSignals ()); |
| 38 | return s_freebsd_signals_sp; |
| 39 | } |
| 40 | } |
| 41 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 42 | //------------------------------------------------------------------------------ |
| 43 | // Static functions. |
| 44 | |
Greg Clayton | 29d1930 | 2012-02-27 18:40:48 +0000 | [diff] [blame] | 45 | lldb::ProcessSP |
| 46 | ProcessFreeBSD::CreateInstance(Target& target, |
| 47 | Listener &listener, |
| 48 | const FileSpec *crash_file_path) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 49 | { |
Greg Clayton | 29d1930 | 2012-02-27 18:40:48 +0000 | [diff] [blame] | 50 | lldb::ProcessSP process_sp; |
| 51 | if (crash_file_path == NULL) |
| 52 | process_sp.reset(new ProcessFreeBSD (target, listener)); |
| 53 | return process_sp; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void |
| 57 | ProcessFreeBSD::Initialize() |
| 58 | { |
Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 59 | static std::once_flag g_once_flag; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 60 | |
Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 61 | std::call_once(g_once_flag, []() { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 62 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 63 | GetPluginDescriptionStatic(), |
| 64 | CreateInstance); |
Robert Flack | 5f4b6c7 | 2015-03-11 21:14:22 +0000 | [diff] [blame] | 65 | ProcessPOSIXLog::Initialize(GetPluginNameStatic()); |
Davide Italiano | c8d6982 | 2015-04-03 04:24:32 +0000 | [diff] [blame] | 66 | }); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 69 | lldb_private::ConstString |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 70 | ProcessFreeBSD::GetPluginNameStatic() |
| 71 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 72 | static ConstString g_name("freebsd"); |
| 73 | return g_name; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | const char * |
| 77 | ProcessFreeBSD::GetPluginDescriptionStatic() |
| 78 | { |
| 79 | return "Process plugin for FreeBSD"; |
| 80 | } |
| 81 | |
| 82 | //------------------------------------------------------------------------------ |
| 83 | // ProcessInterface protocol. |
| 84 | |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 85 | lldb_private::ConstString |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 86 | ProcessFreeBSD::GetPluginName() |
| 87 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 88 | return GetPluginNameStatic(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | uint32_t |
| 92 | ProcessFreeBSD::GetPluginVersion() |
| 93 | { |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | ProcessFreeBSD::GetPluginCommandHelp(const char *command, Stream *strm) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | Error |
| 103 | ProcessFreeBSD::ExecutePluginCommand(Args &command, Stream *strm) |
| 104 | { |
| 105 | return Error(1, eErrorTypeGeneric); |
| 106 | } |
| 107 | |
| 108 | Log * |
| 109 | ProcessFreeBSD::EnablePluginLogging(Stream *strm, Args &command) |
| 110 | { |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | //------------------------------------------------------------------------------ |
| 115 | // Constructors and destructors. |
| 116 | |
| 117 | ProcessFreeBSD::ProcessFreeBSD(Target& target, Listener &listener) |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 118 | : ProcessPOSIX(target, listener, GetFreeBSDSignals ()), |
Ed Maste | c71f60f | 2014-02-28 17:13:39 +0000 | [diff] [blame] | 119 | m_resume_signo(0) |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 120 | { |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void |
| 124 | ProcessFreeBSD::Terminate() |
| 125 | { |
| 126 | } |
| 127 | |
Ed Maste | 7dcb77d | 2013-08-30 13:11:30 +0000 | [diff] [blame] | 128 | Error |
| 129 | ProcessFreeBSD::DoDetach(bool keep_stopped) |
| 130 | { |
| 131 | Error error; |
| 132 | if (keep_stopped) |
| 133 | { |
| 134 | error.SetErrorString("Detaching with keep_stopped true is not currently supported on FreeBSD."); |
| 135 | return error; |
| 136 | } |
| 137 | |
| 138 | error = m_monitor->Detach(GetID()); |
| 139 | |
| 140 | if (error.Success()) |
| 141 | SetPrivateState(eStateDetached); |
| 142 | |
| 143 | return error; |
| 144 | } |
| 145 | |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 146 | Error |
| 147 | ProcessFreeBSD::DoResume() |
| 148 | { |
| 149 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
| 150 | |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 151 | SetPrivateState(eStateRunning); |
| 152 | |
| 153 | Mutex::Locker lock(m_thread_list.GetMutex()); |
| 154 | bool do_step = false; |
| 155 | |
| 156 | for (tid_collection::const_iterator t_pos = m_run_tids.begin(), t_end = m_run_tids.end(); t_pos != t_end; ++t_pos) |
| 157 | { |
| 158 | m_monitor->ThreadSuspend(*t_pos, false); |
| 159 | } |
| 160 | for (tid_collection::const_iterator t_pos = m_step_tids.begin(), t_end = m_step_tids.end(); t_pos != t_end; ++t_pos) |
| 161 | { |
| 162 | m_monitor->ThreadSuspend(*t_pos, false); |
| 163 | do_step = true; |
| 164 | } |
| 165 | for (tid_collection::const_iterator t_pos = m_suspend_tids.begin(), t_end = m_suspend_tids.end(); t_pos != t_end; ++t_pos) |
| 166 | { |
| 167 | m_monitor->ThreadSuspend(*t_pos, true); |
| 168 | // XXX Cannot PT_CONTINUE properly with suspended threads. |
| 169 | do_step = true; |
| 170 | } |
| 171 | |
| 172 | if (log) |
Joerg Sonnenberger | 420708a | 2014-05-02 19:00:27 +0000 | [diff] [blame] | 173 | log->Printf("process %" PRIu64 " resuming (%s)", GetID(), do_step ? "step" : "continue"); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 174 | if (do_step) |
Ed Maste | c71f60f | 2014-02-28 17:13:39 +0000 | [diff] [blame] | 175 | m_monitor->SingleStep(GetID(), m_resume_signo); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 176 | else |
Ed Maste | c71f60f | 2014-02-28 17:13:39 +0000 | [diff] [blame] | 177 | m_monitor->Resume(GetID(), m_resume_signo); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 178 | |
| 179 | return Error(); |
| 180 | } |
| 181 | |
Greg Clayton | c3c0b0e | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 182 | bool |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 183 | ProcessFreeBSD::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) |
| 184 | { |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 185 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
| 186 | if (log) |
| 187 | log->Printf("ProcessFreeBSD::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID()); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 188 | |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 189 | std::vector<lldb::pid_t> tds; |
| 190 | if (!GetMonitor().GetCurrentThreadIDs(tds)) |
| 191 | { |
| 192 | return false; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 195 | ThreadList old_thread_list_copy(old_thread_list); |
| 196 | for (size_t i = 0; i < tds.size(); ++i) |
| 197 | { |
| 198 | tid_t tid = tds[i]; |
| 199 | ThreadSP thread_sp (old_thread_list_copy.RemoveThreadByID(tid, false)); |
| 200 | if (!thread_sp) |
| 201 | { |
| 202 | thread_sp.reset(new FreeBSDThread(*this, tid)); |
| 203 | if (log) |
| 204 | log->Printf("ProcessFreeBSD::%s new tid = %" PRIu64, __FUNCTION__, tid); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | if (log) |
| 209 | log->Printf("ProcessFreeBSD::%s existing tid = %" PRIu64, __FUNCTION__, tid); |
| 210 | } |
| 211 | new_thread_list.AddThread(thread_sp); |
| 212 | } |
| 213 | for (size_t i = 0; i < old_thread_list_copy.GetSize(false); ++i) |
| 214 | { |
| 215 | ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex(i, false)); |
| 216 | if (old_thread_sp) |
| 217 | { |
| 218 | if (log) |
| 219 | log->Printf("ProcessFreeBSD::%s remove tid", __FUNCTION__); |
| 220 | } |
| 221 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 222 | |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 223 | return true; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 224 | } |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 225 | |
| 226 | Error |
| 227 | ProcessFreeBSD::WillResume() |
| 228 | { |
Ed Maste | c71f60f | 2014-02-28 17:13:39 +0000 | [diff] [blame] | 229 | m_resume_signo = 0; |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 230 | m_suspend_tids.clear(); |
| 231 | m_run_tids.clear(); |
| 232 | m_step_tids.clear(); |
| 233 | return ProcessPOSIX::WillResume(); |
| 234 | } |
| 235 | |
| 236 | void |
| 237 | ProcessFreeBSD::SendMessage(const ProcessMessage &message) |
| 238 | { |
| 239 | Mutex::Locker lock(m_message_mutex); |
| 240 | |
| 241 | switch (message.GetKind()) |
| 242 | { |
| 243 | case ProcessMessage::eInvalidMessage: |
| 244 | return; |
| 245 | |
| 246 | case ProcessMessage::eAttachMessage: |
| 247 | SetPrivateState(eStateStopped); |
| 248 | return; |
| 249 | |
| 250 | case ProcessMessage::eLimboMessage: |
| 251 | case ProcessMessage::eExitMessage: |
Todd Fiala | 7b0917a | 2014-09-15 20:07:33 +0000 | [diff] [blame] | 252 | SetExitStatus(message.GetExitStatus(), NULL); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 253 | break; |
| 254 | |
| 255 | case ProcessMessage::eSignalMessage: |
| 256 | case ProcessMessage::eSignalDeliveredMessage: |
| 257 | case ProcessMessage::eBreakpointMessage: |
| 258 | case ProcessMessage::eTraceMessage: |
| 259 | case ProcessMessage::eWatchpointMessage: |
| 260 | case ProcessMessage::eCrashMessage: |
| 261 | SetPrivateState(eStateStopped); |
| 262 | break; |
| 263 | |
| 264 | case ProcessMessage::eNewThreadMessage: |
| 265 | assert(0 && "eNewThreadMessage unexpected on FreeBSD"); |
| 266 | break; |
| 267 | |
| 268 | case ProcessMessage::eExecMessage: |
| 269 | SetPrivateState(eStateStopped); |
| 270 | break; |
| 271 | } |
| 272 | |
| 273 | m_message_queue.push(message); |
| 274 | } |