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 | |
Ed Maste | dc97e23 | 2014-02-19 22:12:57 +0000 | [diff] [blame] | 136 | DisableAllBreakpointSites(); |
| 137 | |
Ed Maste | 7dcb77d | 2013-08-30 13:11:30 +0000 | [diff] [blame] | 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) |
| 173 | log->Printf("process %lu resuming (%s)", GetID(), do_step ? "step" : "continue"); |
| 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: |
| 252 | m_exit_status = message.GetExitStatus(); |
| 253 | SetExitStatus(m_exit_status, NULL); |
| 254 | break; |
| 255 | |
| 256 | case ProcessMessage::eSignalMessage: |
| 257 | case ProcessMessage::eSignalDeliveredMessage: |
| 258 | case ProcessMessage::eBreakpointMessage: |
| 259 | case ProcessMessage::eTraceMessage: |
| 260 | case ProcessMessage::eWatchpointMessage: |
| 261 | case ProcessMessage::eCrashMessage: |
| 262 | SetPrivateState(eStateStopped); |
| 263 | break; |
| 264 | |
| 265 | case ProcessMessage::eNewThreadMessage: |
| 266 | assert(0 && "eNewThreadMessage unexpected on FreeBSD"); |
| 267 | break; |
| 268 | |
| 269 | case ProcessMessage::eExecMessage: |
| 270 | SetPrivateState(eStateStopped); |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | m_message_queue.push(message); |
| 275 | } |
| 276 | |