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