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