Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1 | //===-- ProcessFreeBSD.cpp ----------------------------------------*- C++ |
| 2 | //-*-===// |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 3 | // |
| 4 | // The LLVM Compiler Infrastructure |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | // C Includes |
| 12 | #include <errno.h> |
| 13 | |
| 14 | // C++ Includes |
Benjamin Kramer | 3f69fa6 | 2015-04-03 10:55:00 +0000 | [diff] [blame] | 15 | #include <mutex> |
| 16 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 17 | // Other libraries and framework includes |
| 18 | #include "lldb/Core/PluginManager.h" |
| 19 | #include "lldb/Core/State.h" |
| 20 | #include "lldb/Host/Host.h" |
| 21 | #include "lldb/Symbol/ObjectFile.h" |
| 22 | #include "lldb/Target/DynamicLoader.h" |
| 23 | #include "lldb/Target/Target.h" |
| 24 | |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 25 | #include "FreeBSDThread.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 26 | #include "Plugins/Process/Utility/FreeBSDSignals.h" |
| 27 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
| 28 | #include "ProcessFreeBSD.h" |
| 29 | #include "ProcessMonitor.h" |
| 30 | #include "ProcessPOSIXLog.h" |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 31 | |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 32 | // Other libraries and framework includes |
| 33 | #include "lldb/Breakpoint/BreakpointLocation.h" |
| 34 | #include "lldb/Breakpoint/Watchpoint.h" |
| 35 | #include "lldb/Core/Module.h" |
| 36 | #include "lldb/Core/ModuleSpec.h" |
| 37 | #include "lldb/Core/PluginManager.h" |
| 38 | #include "lldb/Core/State.h" |
| 39 | #include "lldb/Host/FileSpec.h" |
| 40 | #include "lldb/Host/Host.h" |
| 41 | #include "lldb/Symbol/ObjectFile.h" |
| 42 | #include "lldb/Target/DynamicLoader.h" |
| 43 | #include "lldb/Target/Platform.h" |
| 44 | #include "lldb/Target/Target.h" |
| 45 | |
| 46 | #include "lldb/Host/posix/Fcntl.h" |
| 47 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 48 | using namespace lldb; |
| 49 | using namespace lldb_private; |
| 50 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | namespace { |
| 52 | UnixSignalsSP &GetFreeBSDSignals() { |
| 53 | static UnixSignalsSP s_freebsd_signals_sp(new FreeBSDSignals()); |
| 54 | return s_freebsd_signals_sp; |
| 55 | } |
Todd Fiala | 4ceced3 | 2014-08-29 17:35:57 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 58 | //------------------------------------------------------------------------------ |
| 59 | // Static functions. |
| 60 | |
Greg Clayton | 29d1930 | 2012-02-27 18:40:48 +0000 | [diff] [blame] | 61 | lldb::ProcessSP |
Zachary Turner | 12004eb | 2015-09-02 16:47:47 +0000 | [diff] [blame] | 62 | ProcessFreeBSD::CreateInstance(lldb::TargetSP target_sp, |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 63 | lldb::ListenerSP listener_sp, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | const FileSpec *crash_file_path) { |
| 65 | lldb::ProcessSP process_sp; |
| 66 | if (crash_file_path == NULL) |
| 67 | process_sp.reset( |
| 68 | new ProcessFreeBSD(target_sp, listener_sp, GetFreeBSDSignals())); |
| 69 | return process_sp; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | void ProcessFreeBSD::Initialize() { |
| 73 | static std::once_flag g_once_flag; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 74 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | std::call_once(g_once_flag, []() { |
| 76 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 77 | GetPluginDescriptionStatic(), CreateInstance); |
| 78 | ProcessPOSIXLog::Initialize(GetPluginNameStatic()); |
| 79 | }); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | lldb_private::ConstString ProcessFreeBSD::GetPluginNameStatic() { |
| 83 | static ConstString g_name("freebsd"); |
| 84 | return g_name; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | const char *ProcessFreeBSD::GetPluginDescriptionStatic() { |
| 88 | return "Process plugin for FreeBSD"; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | //------------------------------------------------------------------------------ |
| 92 | // ProcessInterface protocol. |
| 93 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 94 | lldb_private::ConstString ProcessFreeBSD::GetPluginName() { |
| 95 | return GetPluginNameStatic(); |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 98 | uint32_t ProcessFreeBSD::GetPluginVersion() { return 1; } |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 99 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 100 | void ProcessFreeBSD::Terminate() {} |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | Error ProcessFreeBSD::DoDetach(bool keep_stopped) { |
| 103 | Error error; |
| 104 | if (keep_stopped) { |
| 105 | error.SetErrorString("Detaching with keep_stopped true is not currently " |
| 106 | "supported on FreeBSD."); |
Ed Maste | 7dcb77d | 2013-08-30 13:11:30 +0000 | [diff] [blame] | 107 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | error = m_monitor->Detach(GetID()); |
| 111 | |
| 112 | if (error.Success()) |
| 113 | SetPrivateState(eStateDetached); |
| 114 | |
| 115 | return error; |
Ed Maste | 7dcb77d | 2013-08-30 13:11:30 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | Error ProcessFreeBSD::DoResume() { |
| 119 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 120 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | SetPrivateState(eStateRunning); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 122 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
| 124 | bool do_step = false; |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 125 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | for (tid_collection::const_iterator t_pos = m_run_tids.begin(), |
| 127 | t_end = m_run_tids.end(); |
| 128 | t_pos != t_end; ++t_pos) { |
| 129 | m_monitor->ThreadSuspend(*t_pos, false); |
| 130 | } |
| 131 | for (tid_collection::const_iterator t_pos = m_step_tids.begin(), |
| 132 | t_end = m_step_tids.end(); |
| 133 | t_pos != t_end; ++t_pos) { |
| 134 | m_monitor->ThreadSuspend(*t_pos, false); |
| 135 | do_step = true; |
| 136 | } |
| 137 | for (tid_collection::const_iterator t_pos = m_suspend_tids.begin(), |
| 138 | t_end = m_suspend_tids.end(); |
| 139 | t_pos != t_end; ++t_pos) { |
| 140 | m_monitor->ThreadSuspend(*t_pos, true); |
| 141 | // XXX Cannot PT_CONTINUE properly with suspended threads. |
| 142 | do_step = true; |
| 143 | } |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | if (log) |
| 146 | log->Printf("process %" PRIu64 " resuming (%s)", GetID(), |
| 147 | do_step ? "step" : "continue"); |
| 148 | if (do_step) |
| 149 | m_monitor->SingleStep(GetID(), m_resume_signo); |
| 150 | else |
| 151 | m_monitor->Resume(GetID(), m_resume_signo); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 152 | |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame^] | 153 | return Error(); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | bool ProcessFreeBSD::UpdateThreadList(ThreadList &old_thread_list, |
| 157 | ThreadList &new_thread_list) { |
| 158 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 159 | if (log) |
| 160 | log->Printf("ProcessFreeBSD::%s (pid = %" PRIu64 ")", __FUNCTION__, |
| 161 | GetID()); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 162 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 163 | std::vector<lldb::pid_t> tds; |
| 164 | if (!GetMonitor().GetCurrentThreadIDs(tds)) { |
| 165 | return false; |
| 166 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 167 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 168 | ThreadList old_thread_list_copy(old_thread_list); |
| 169 | for (size_t i = 0; i < tds.size(); ++i) { |
| 170 | tid_t tid = tds[i]; |
| 171 | ThreadSP thread_sp(old_thread_list_copy.RemoveThreadByID(tid, false)); |
| 172 | if (!thread_sp) { |
| 173 | thread_sp.reset(new FreeBSDThread(*this, tid)); |
| 174 | if (log) |
| 175 | log->Printf("ProcessFreeBSD::%s new tid = %" PRIu64, __FUNCTION__, tid); |
| 176 | } else { |
| 177 | if (log) |
| 178 | log->Printf("ProcessFreeBSD::%s existing tid = %" PRIu64, __FUNCTION__, |
| 179 | tid); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 180 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 181 | new_thread_list.AddThread(thread_sp); |
| 182 | } |
| 183 | for (size_t i = 0; i < old_thread_list_copy.GetSize(false); ++i) { |
| 184 | ThreadSP old_thread_sp(old_thread_list_copy.GetThreadAtIndex(i, false)); |
| 185 | if (old_thread_sp) { |
| 186 | if (log) |
| 187 | log->Printf("ProcessFreeBSD::%s remove tid", __FUNCTION__); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 188 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 189 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 190 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | return true; |
Johnny Chen | 9ed5b49 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 192 | } |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 193 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 194 | Error ProcessFreeBSD::WillResume() { |
| 195 | m_resume_signo = 0; |
| 196 | m_suspend_tids.clear(); |
| 197 | m_run_tids.clear(); |
| 198 | m_step_tids.clear(); |
| 199 | return Process::WillResume(); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | void ProcessFreeBSD::SendMessage(const ProcessMessage &message) { |
| 203 | std::lock_guard<std::recursive_mutex> guard(m_message_mutex); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 204 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 205 | switch (message.GetKind()) { |
| 206 | case ProcessMessage::eInvalidMessage: |
| 207 | return; |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 208 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | case ProcessMessage::eAttachMessage: |
| 210 | SetPrivateState(eStateStopped); |
| 211 | return; |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 212 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | case ProcessMessage::eLimboMessage: |
| 214 | case ProcessMessage::eExitMessage: |
| 215 | SetExitStatus(message.GetExitStatus(), NULL); |
| 216 | break; |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 217 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 218 | case ProcessMessage::eSignalMessage: |
| 219 | case ProcessMessage::eSignalDeliveredMessage: |
| 220 | case ProcessMessage::eBreakpointMessage: |
| 221 | case ProcessMessage::eTraceMessage: |
| 222 | case ProcessMessage::eWatchpointMessage: |
| 223 | case ProcessMessage::eCrashMessage: |
| 224 | SetPrivateState(eStateStopped); |
| 225 | break; |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 226 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 227 | case ProcessMessage::eNewThreadMessage: |
| 228 | llvm_unreachable("eNewThreadMessage unexpected on FreeBSD"); |
| 229 | break; |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 230 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 231 | case ProcessMessage::eExecMessage: |
| 232 | SetPrivateState(eStateStopped); |
| 233 | break; |
| 234 | } |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 235 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 236 | m_message_queue.push(message); |
Ed Maste | 7fd845c | 2013-12-09 15:51:17 +0000 | [diff] [blame] | 237 | } |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 238 | |
| 239 | //------------------------------------------------------------------------------ |
| 240 | // Constructors and destructors. |
| 241 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 242 | ProcessFreeBSD::ProcessFreeBSD(lldb::TargetSP target_sp, |
| 243 | lldb::ListenerSP listener_sp, |
| 244 | UnixSignalsSP &unix_signals_sp) |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 245 | : Process(target_sp, listener_sp, unix_signals_sp), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 246 | m_byte_order(endian::InlHostByteOrder()), m_monitor(NULL), m_module(NULL), |
| 247 | m_message_mutex(), m_exit_now(false), m_seen_initial_stop(), |
| 248 | m_resume_signo(0) { |
| 249 | // FIXME: Putting this code in the ctor and saving the byte order in a |
| 250 | // member variable is a hack to avoid const qual issues in GetByteOrder. |
| 251 | lldb::ModuleSP module = GetTarget().GetExecutableModule(); |
| 252 | if (module && module->GetObjectFile()) |
| 253 | m_byte_order = module->GetObjectFile()->GetByteOrder(); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 256 | ProcessFreeBSD::~ProcessFreeBSD() { delete m_monitor; } |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 257 | |
| 258 | //------------------------------------------------------------------------------ |
| 259 | // Process protocol. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 260 | void ProcessFreeBSD::Finalize() { |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 261 | Process::Finalize(); |
| 262 | |
| 263 | if (m_monitor) |
| 264 | m_monitor->StopMonitor(); |
| 265 | } |
| 266 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 267 | bool ProcessFreeBSD::CanDebug(lldb::TargetSP target_sp, |
| 268 | bool plugin_specified_by_name) { |
| 269 | // For now we are just making sure the file exists for a given module |
| 270 | ModuleSP exe_module_sp(target_sp->GetExecutableModule()); |
| 271 | if (exe_module_sp.get()) |
| 272 | return exe_module_sp->GetFileSpec().Exists(); |
| 273 | // If there is no executable module, we return true since we might be |
| 274 | // preparing to attach. |
| 275 | return true; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 278 | Error ProcessFreeBSD::DoAttachToProcessWithID( |
| 279 | lldb::pid_t pid, const ProcessAttachInfo &attach_info) { |
| 280 | Error error; |
| 281 | assert(m_monitor == NULL); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 282 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 283 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 284 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
| 285 | log->Printf("ProcessFreeBSD::%s(pid = %" PRIi64 ")", __FUNCTION__, GetID()); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 286 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 287 | m_monitor = new ProcessMonitor(this, pid, error); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 288 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 289 | if (!error.Success()) |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 290 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 291 | |
| 292 | PlatformSP platform_sp(GetTarget().GetPlatform()); |
| 293 | assert(platform_sp.get()); |
| 294 | if (!platform_sp) |
| 295 | return error; // FIXME: Detatch? |
| 296 | |
| 297 | // Find out what we can about this process |
| 298 | ProcessInstanceInfo process_info; |
| 299 | platform_sp->GetProcessInfo(pid, process_info); |
| 300 | |
| 301 | // Resolve the executable module |
| 302 | ModuleSP exe_module_sp; |
| 303 | FileSpecList executable_search_paths( |
| 304 | Target::GetDefaultExecutableSearchPaths()); |
| 305 | ModuleSpec exe_module_spec(process_info.GetExecutableFile(), |
| 306 | GetTarget().GetArchitecture()); |
| 307 | error = platform_sp->ResolveExecutable( |
| 308 | exe_module_spec, exe_module_sp, |
| 309 | executable_search_paths.GetSize() ? &executable_search_paths : NULL); |
| 310 | if (!error.Success()) |
| 311 | return error; |
| 312 | |
| 313 | // Fix the target architecture if necessary |
| 314 | const ArchSpec &module_arch = exe_module_sp->GetArchitecture(); |
| 315 | if (module_arch.IsValid() && |
| 316 | !GetTarget().GetArchitecture().IsExactMatch(module_arch)) |
| 317 | GetTarget().SetArchitecture(module_arch); |
| 318 | |
| 319 | // Initialize the target module list |
| 320 | GetTarget().SetExecutableModule(exe_module_sp, true); |
| 321 | |
| 322 | SetSTDIOFileDescriptor(m_monitor->GetTerminalFD()); |
| 323 | |
| 324 | SetID(pid); |
| 325 | |
| 326 | return error; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 329 | Error ProcessFreeBSD::WillLaunch(Module *module) { |
| 330 | Error error; |
| 331 | return error; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | FileSpec |
| 335 | ProcessFreeBSD::GetFileSpec(const lldb_private::FileAction *file_action, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 336 | const FileSpec &default_file_spec, |
| 337 | const FileSpec &dbg_pts_file_spec) { |
| 338 | FileSpec file_spec{}; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 339 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 340 | if (file_action && file_action->GetAction() == FileAction::eFileActionOpen) { |
| 341 | file_spec = file_action->GetFileSpec(); |
| 342 | // By default the stdio paths passed in will be pseudo-terminal |
| 343 | // (/dev/pts). If so, convert to using a different default path |
| 344 | // instead to redirect I/O to the debugger console. This should |
| 345 | // also handle user overrides to /dev/null or a different file. |
| 346 | if (!file_spec || file_spec == dbg_pts_file_spec) |
| 347 | file_spec = default_file_spec; |
| 348 | } |
| 349 | return file_spec; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 352 | Error ProcessFreeBSD::DoLaunch(Module *module, ProcessLaunchInfo &launch_info) { |
| 353 | Error error; |
| 354 | assert(m_monitor == NULL); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 355 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 356 | FileSpec working_dir = launch_info.GetWorkingDirectory(); |
| 357 | if (working_dir && |
| 358 | (!working_dir.ResolvePath() || |
| 359 | working_dir.GetFileType() != FileSpec::eFileTypeDirectory)) { |
| 360 | error.SetErrorStringWithFormat("No such file or directory: %s", |
| 361 | working_dir.GetCString()); |
| 362 | return error; |
| 363 | } |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 364 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 365 | SetPrivateState(eStateLaunching); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 366 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 367 | const lldb_private::FileAction *file_action; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 368 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 369 | // Default of empty will mean to use existing open file descriptors |
| 370 | FileSpec stdin_file_spec{}; |
| 371 | FileSpec stdout_file_spec{}; |
| 372 | FileSpec stderr_file_spec{}; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 373 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 374 | const FileSpec dbg_pts_file_spec{launch_info.GetPTY().GetSlaveName(NULL, 0), |
| 375 | false}; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 376 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 377 | file_action = launch_info.GetFileActionForFD(STDIN_FILENO); |
| 378 | stdin_file_spec = |
| 379 | GetFileSpec(file_action, stdin_file_spec, dbg_pts_file_spec); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 380 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 381 | file_action = launch_info.GetFileActionForFD(STDOUT_FILENO); |
| 382 | stdout_file_spec = |
| 383 | GetFileSpec(file_action, stdout_file_spec, dbg_pts_file_spec); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 384 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 385 | file_action = launch_info.GetFileActionForFD(STDERR_FILENO); |
| 386 | stderr_file_spec = |
| 387 | GetFileSpec(file_action, stderr_file_spec, dbg_pts_file_spec); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 388 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 389 | m_monitor = new ProcessMonitor( |
| 390 | this, module, launch_info.GetArguments().GetConstArgumentVector(), |
| 391 | launch_info.GetEnvironmentEntries().GetConstArgumentVector(), |
| 392 | stdin_file_spec, stdout_file_spec, stderr_file_spec, working_dir, |
| 393 | launch_info, error); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 394 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 395 | m_module = module; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 396 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 397 | if (!error.Success()) |
| 398 | return error; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 399 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 400 | int terminal = m_monitor->GetTerminalFD(); |
| 401 | if (terminal >= 0) { |
| 402 | // The reader thread will close the file descriptor when done, so we pass it a |
| 403 | // copy. |
Sylvestre Ledru | 79cb009 | 2015-08-28 12:24:07 +0000 | [diff] [blame] | 404 | #ifdef F_DUPFD_CLOEXEC |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 405 | int stdio = fcntl(terminal, F_DUPFD_CLOEXEC, 0); |
| 406 | if (stdio == -1) { |
| 407 | error.SetErrorToErrno(); |
| 408 | return error; |
| 409 | } |
Sylvestre Ledru | 79cb009 | 2015-08-28 12:24:07 +0000 | [diff] [blame] | 410 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 411 | // Special case when F_DUPFD_CLOEXEC does not exist (Debian kFreeBSD) |
| 412 | int stdio = fcntl(terminal, F_DUPFD, 0); |
| 413 | if (stdio == -1) { |
| 414 | error.SetErrorToErrno(); |
| 415 | return error; |
| 416 | } |
| 417 | stdio = fcntl(terminal, F_SETFD, FD_CLOEXEC); |
| 418 | if (stdio == -1) { |
| 419 | error.SetErrorToErrno(); |
| 420 | return error; |
| 421 | } |
Sylvestre Ledru | 79cb009 | 2015-08-28 12:24:07 +0000 | [diff] [blame] | 422 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 423 | SetSTDIOFileDescriptor(stdio); |
| 424 | } |
| 425 | |
| 426 | SetID(m_monitor->GetPID()); |
| 427 | return error; |
| 428 | } |
| 429 | |
| 430 | void ProcessFreeBSD::DidLaunch() {} |
| 431 | |
| 432 | addr_t ProcessFreeBSD::GetImageInfoAddress() { |
| 433 | Target *target = &GetTarget(); |
| 434 | ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); |
| 435 | Address addr = obj_file->GetImageInfoAddress(target); |
| 436 | |
| 437 | if (addr.IsValid()) |
| 438 | return addr.GetLoadAddress(target); |
| 439 | return LLDB_INVALID_ADDRESS; |
| 440 | } |
| 441 | |
| 442 | Error ProcessFreeBSD::DoHalt(bool &caused_stop) { |
| 443 | Error error; |
| 444 | |
| 445 | if (IsStopped()) { |
| 446 | caused_stop = false; |
| 447 | } else if (kill(GetID(), SIGSTOP)) { |
| 448 | caused_stop = false; |
| 449 | error.SetErrorToErrno(); |
| 450 | } else { |
| 451 | caused_stop = true; |
| 452 | } |
| 453 | return error; |
| 454 | } |
| 455 | |
| 456 | Error ProcessFreeBSD::DoSignal(int signal) { |
| 457 | Error error; |
| 458 | |
| 459 | if (kill(GetID(), signal)) |
| 460 | error.SetErrorToErrno(); |
| 461 | |
| 462 | return error; |
| 463 | } |
| 464 | |
| 465 | Error ProcessFreeBSD::DoDestroy() { |
| 466 | Error error; |
| 467 | |
| 468 | if (!HasExited()) { |
| 469 | assert(m_monitor); |
| 470 | m_exit_now = true; |
| 471 | if (GetID() == LLDB_INVALID_PROCESS_ID) { |
| 472 | error.SetErrorString("invalid process id"); |
| 473 | return error; |
| 474 | } |
| 475 | if (!m_monitor->Kill()) { |
| 476 | error.SetErrorToErrno(); |
| 477 | return error; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 480 | SetPrivateState(eStateExited); |
| 481 | } |
| 482 | |
| 483 | return error; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 486 | void ProcessFreeBSD::DoDidExec() { |
| 487 | Target *target = &GetTarget(); |
| 488 | if (target) { |
| 489 | PlatformSP platform_sp(target->GetPlatform()); |
| 490 | assert(platform_sp.get()); |
| 491 | if (platform_sp) { |
| 492 | ProcessInstanceInfo process_info; |
| 493 | platform_sp->GetProcessInfo(GetID(), process_info); |
| 494 | ModuleSP exe_module_sp; |
| 495 | ModuleSpec exe_module_spec(process_info.GetExecutableFile(), |
| 496 | target->GetArchitecture()); |
| 497 | FileSpecList executable_search_paths( |
| 498 | Target::GetDefaultExecutableSearchPaths()); |
| 499 | Error error = platform_sp->ResolveExecutable( |
| 500 | exe_module_spec, exe_module_sp, |
| 501 | executable_search_paths.GetSize() ? &executable_search_paths : NULL); |
| 502 | if (!error.Success()) |
| 503 | return; |
| 504 | target->SetExecutableModule(exe_module_sp, true); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 505 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 506 | } |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 509 | bool ProcessFreeBSD::AddThreadForInitialStopIfNeeded(lldb::tid_t stop_tid) { |
| 510 | bool added_to_set = false; |
| 511 | ThreadStopSet::iterator it = m_seen_initial_stop.find(stop_tid); |
| 512 | if (it == m_seen_initial_stop.end()) { |
| 513 | m_seen_initial_stop.insert(stop_tid); |
| 514 | added_to_set = true; |
| 515 | } |
| 516 | return added_to_set; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 519 | bool ProcessFreeBSD::WaitingForInitialStop(lldb::tid_t stop_tid) { |
| 520 | return (m_seen_initial_stop.find(stop_tid) == m_seen_initial_stop.end()); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | FreeBSDThread * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 524 | ProcessFreeBSD::CreateNewFreeBSDThread(lldb_private::Process &process, |
| 525 | lldb::tid_t tid) { |
| 526 | return new FreeBSDThread(process, tid); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 529 | void ProcessFreeBSD::RefreshStateAfterStop() { |
| 530 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 531 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
| 532 | log->Printf("ProcessFreeBSD::%s(), message_queue size = %d", __FUNCTION__, |
| 533 | (int)m_message_queue.size()); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 534 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 535 | std::lock_guard<std::recursive_mutex> guard(m_message_mutex); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 536 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 537 | // This method used to only handle one message. Changing it to loop allows |
| 538 | // it to handle the case where we hit a breakpoint while handling a different |
| 539 | // breakpoint. |
| 540 | while (!m_message_queue.empty()) { |
| 541 | ProcessMessage &message = m_message_queue.front(); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 542 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 543 | // Resolve the thread this message corresponds to and pass it along. |
| 544 | lldb::tid_t tid = message.GetTID(); |
| 545 | if (log) |
| 546 | log->Printf( |
| 547 | "ProcessFreeBSD::%s(), message_queue size = %d, pid = %" PRIi64, |
| 548 | __FUNCTION__, (int)m_message_queue.size(), tid); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 549 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 550 | m_thread_list.RefreshStateAfterStop(); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 551 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 552 | FreeBSDThread *thread = static_cast<FreeBSDThread *>( |
| 553 | GetThreadList().FindThreadByID(tid, false).get()); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 554 | if (thread) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 555 | thread->Notify(message); |
| 556 | |
| 557 | if (message.GetKind() == ProcessMessage::eExitMessage) { |
| 558 | // FIXME: We should tell the user about this, but the limbo message is |
| 559 | // probably better for that. |
| 560 | if (log) |
| 561 | log->Printf("ProcessFreeBSD::%s() removing thread, tid = %" PRIi64, |
| 562 | __FUNCTION__, tid); |
| 563 | |
| 564 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
| 565 | |
| 566 | ThreadSP thread_sp = m_thread_list.RemoveThreadByID(tid, false); |
| 567 | thread_sp.reset(); |
| 568 | m_seen_initial_stop.erase(tid); |
| 569 | } |
| 570 | |
| 571 | m_message_queue.pop(); |
| 572 | } |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 575 | bool ProcessFreeBSD::IsAlive() { |
| 576 | StateType state = GetPrivateState(); |
| 577 | return state != eStateDetached && state != eStateExited && |
| 578 | state != eStateInvalid && state != eStateUnloaded; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 581 | size_t ProcessFreeBSD::DoReadMemory(addr_t vm_addr, void *buf, size_t size, |
| 582 | Error &error) { |
| 583 | assert(m_monitor); |
| 584 | return m_monitor->ReadMemory(vm_addr, buf, size, error); |
| 585 | } |
| 586 | |
| 587 | size_t ProcessFreeBSD::DoWriteMemory(addr_t vm_addr, const void *buf, |
| 588 | size_t size, Error &error) { |
| 589 | assert(m_monitor); |
| 590 | return m_monitor->WriteMemory(vm_addr, buf, size, error); |
| 591 | } |
| 592 | |
| 593 | addr_t ProcessFreeBSD::DoAllocateMemory(size_t size, uint32_t permissions, |
| 594 | Error &error) { |
| 595 | addr_t allocated_addr = LLDB_INVALID_ADDRESS; |
| 596 | |
| 597 | unsigned prot = 0; |
| 598 | if (permissions & lldb::ePermissionsReadable) |
| 599 | prot |= eMmapProtRead; |
| 600 | if (permissions & lldb::ePermissionsWritable) |
| 601 | prot |= eMmapProtWrite; |
| 602 | if (permissions & lldb::ePermissionsExecutable) |
| 603 | prot |= eMmapProtExec; |
| 604 | |
| 605 | if (InferiorCallMmap(this, allocated_addr, 0, size, prot, |
| 606 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { |
| 607 | m_addr_to_mmap_size[allocated_addr] = size; |
| 608 | error.Clear(); |
| 609 | } else { |
| 610 | allocated_addr = LLDB_INVALID_ADDRESS; |
| 611 | error.SetErrorStringWithFormat( |
| 612 | "unable to allocate %zu bytes of memory with permissions %s", size, |
| 613 | GetPermissionsAsCString(permissions)); |
| 614 | } |
| 615 | |
| 616 | return allocated_addr; |
| 617 | } |
| 618 | |
| 619 | Error ProcessFreeBSD::DoDeallocateMemory(lldb::addr_t addr) { |
| 620 | Error error; |
| 621 | MMapMap::iterator pos = m_addr_to_mmap_size.find(addr); |
| 622 | if (pos != m_addr_to_mmap_size.end() && |
| 623 | InferiorCallMunmap(this, addr, pos->second)) |
| 624 | m_addr_to_mmap_size.erase(pos); |
| 625 | else |
| 626 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, |
| 627 | addr); |
| 628 | |
| 629 | return error; |
| 630 | } |
| 631 | |
| 632 | size_t |
| 633 | ProcessFreeBSD::GetSoftwareBreakpointTrapOpcode(BreakpointSite *bp_site) { |
| 634 | static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xD4}; |
| 635 | static const uint8_t g_i386_opcode[] = {0xCC}; |
| 636 | |
| 637 | ArchSpec arch = GetTarget().GetArchitecture(); |
| 638 | const uint8_t *opcode = NULL; |
| 639 | size_t opcode_size = 0; |
| 640 | |
| 641 | switch (arch.GetMachine()) { |
| 642 | default: |
| 643 | assert(false && "CPU type not supported!"); |
| 644 | break; |
| 645 | |
| 646 | case llvm::Triple::arm: { |
| 647 | // The ARM reference recommends the use of 0xe7fddefe and 0xdefe |
| 648 | // but the linux kernel does otherwise. |
| 649 | static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7}; |
| 650 | static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde}; |
| 651 | |
| 652 | lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0)); |
| 653 | AddressClass addr_class = eAddressClassUnknown; |
| 654 | |
| 655 | if (bp_loc_sp) |
| 656 | addr_class = bp_loc_sp->GetAddress().GetAddressClass(); |
| 657 | |
| 658 | if (addr_class == eAddressClassCodeAlternateISA || |
| 659 | (addr_class == eAddressClassUnknown && |
| 660 | bp_loc_sp->GetAddress().GetOffset() & 1)) { |
| 661 | opcode = g_thumb_breakpoint_opcode; |
| 662 | opcode_size = sizeof(g_thumb_breakpoint_opcode); |
| 663 | } else { |
| 664 | opcode = g_arm_breakpoint_opcode; |
| 665 | opcode_size = sizeof(g_arm_breakpoint_opcode); |
| 666 | } |
| 667 | } break; |
| 668 | case llvm::Triple::aarch64: |
| 669 | opcode = g_aarch64_opcode; |
| 670 | opcode_size = sizeof(g_aarch64_opcode); |
| 671 | break; |
| 672 | |
| 673 | case llvm::Triple::x86: |
| 674 | case llvm::Triple::x86_64: |
| 675 | opcode = g_i386_opcode; |
| 676 | opcode_size = sizeof(g_i386_opcode); |
| 677 | break; |
| 678 | } |
| 679 | |
| 680 | bp_site->SetTrapOpcode(opcode, opcode_size); |
| 681 | return opcode_size; |
| 682 | } |
| 683 | |
| 684 | Error ProcessFreeBSD::EnableBreakpointSite(BreakpointSite *bp_site) { |
| 685 | return EnableSoftwareBreakpoint(bp_site); |
| 686 | } |
| 687 | |
| 688 | Error ProcessFreeBSD::DisableBreakpointSite(BreakpointSite *bp_site) { |
| 689 | return DisableSoftwareBreakpoint(bp_site); |
| 690 | } |
| 691 | |
| 692 | Error ProcessFreeBSD::EnableWatchpoint(Watchpoint *wp, bool notify) { |
| 693 | Error error; |
| 694 | if (wp) { |
| 695 | user_id_t watchID = wp->GetID(); |
| 696 | addr_t addr = wp->GetLoadAddress(); |
| 697 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); |
| 698 | if (log) |
| 699 | log->Printf("ProcessFreeBSD::EnableWatchpoint(watchID = %" PRIu64 ")", |
| 700 | watchID); |
| 701 | if (wp->IsEnabled()) { |
| 702 | if (log) |
| 703 | log->Printf("ProcessFreeBSD::EnableWatchpoint(watchID = %" PRIu64 |
| 704 | ") addr = 0x%8.8" PRIx64 ": watchpoint already enabled.", |
| 705 | watchID, (uint64_t)addr); |
| 706 | return error; |
| 707 | } |
| 708 | |
| 709 | // Try to find a vacant watchpoint slot in the inferiors' main thread |
| 710 | uint32_t wp_hw_index = LLDB_INVALID_INDEX32; |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 711 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 712 | FreeBSDThread *thread = static_cast<FreeBSDThread *>( |
| 713 | m_thread_list.GetThreadAtIndex(0, false).get()); |
| 714 | |
| 715 | if (thread) |
| 716 | wp_hw_index = thread->FindVacantWatchpointIndex(); |
| 717 | |
| 718 | if (wp_hw_index == LLDB_INVALID_INDEX32) { |
| 719 | error.SetErrorString("Setting hardware watchpoint failed."); |
| 720 | } else { |
| 721 | wp->SetHardwareIndex(wp_hw_index); |
| 722 | bool wp_enabled = true; |
| 723 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 724 | for (uint32_t i = 0; i < thread_count; ++i) { |
| 725 | thread = static_cast<FreeBSDThread *>( |
| 726 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 727 | if (thread) |
| 728 | wp_enabled &= thread->EnableHardwareWatchpoint(wp); |
| 729 | else |
| 730 | wp_enabled = false; |
| 731 | } |
| 732 | if (wp_enabled) { |
| 733 | wp->SetEnabled(true, notify); |
| 734 | return error; |
| 735 | } else { |
| 736 | // Watchpoint enabling failed on at least one |
| 737 | // of the threads so roll back all of them |
| 738 | DisableWatchpoint(wp, false); |
| 739 | error.SetErrorString("Setting hardware watchpoint failed"); |
| 740 | } |
| 741 | } |
| 742 | } else |
| 743 | error.SetErrorString("Watchpoint argument was NULL."); |
| 744 | return error; |
| 745 | } |
| 746 | |
| 747 | Error ProcessFreeBSD::DisableWatchpoint(Watchpoint *wp, bool notify) { |
| 748 | Error error; |
| 749 | if (wp) { |
| 750 | user_id_t watchID = wp->GetID(); |
| 751 | addr_t addr = wp->GetLoadAddress(); |
| 752 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_WATCHPOINTS)); |
| 753 | if (log) |
| 754 | log->Printf("ProcessFreeBSD::DisableWatchpoint(watchID = %" PRIu64 ")", |
| 755 | watchID); |
| 756 | if (!wp->IsEnabled()) { |
| 757 | if (log) |
| 758 | log->Printf("ProcessFreeBSD::DisableWatchpoint(watchID = %" PRIu64 |
| 759 | ") addr = 0x%8.8" PRIx64 ": watchpoint already disabled.", |
| 760 | watchID, (uint64_t)addr); |
| 761 | // This is needed (for now) to keep watchpoints disabled correctly |
| 762 | wp->SetEnabled(false, notify); |
| 763 | return error; |
| 764 | } |
| 765 | |
| 766 | if (wp->IsHardware()) { |
| 767 | bool wp_disabled = true; |
| 768 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
| 769 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 770 | for (uint32_t i = 0; i < thread_count; ++i) { |
| 771 | FreeBSDThread *thread = static_cast<FreeBSDThread *>( |
| 772 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 773 | if (thread) |
| 774 | wp_disabled &= thread->DisableHardwareWatchpoint(wp); |
| 775 | else |
| 776 | wp_disabled = false; |
| 777 | } |
| 778 | if (wp_disabled) { |
| 779 | wp->SetHardwareIndex(LLDB_INVALID_INDEX32); |
| 780 | wp->SetEnabled(false, notify); |
| 781 | return error; |
| 782 | } else |
| 783 | error.SetErrorString("Disabling hardware watchpoint failed"); |
| 784 | } |
| 785 | } else |
| 786 | error.SetErrorString("Watchpoint argument was NULL."); |
| 787 | return error; |
| 788 | } |
| 789 | |
| 790 | Error ProcessFreeBSD::GetWatchpointSupportInfo(uint32_t &num) { |
| 791 | Error error; |
| 792 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
| 793 | FreeBSDThread *thread = static_cast<FreeBSDThread *>( |
| 794 | m_thread_list.GetThreadAtIndex(0, false).get()); |
| 795 | if (thread) |
| 796 | num = thread->NumSupportedHardwareWatchpoints(); |
| 797 | else |
| 798 | error.SetErrorString("Process does not exist."); |
| 799 | return error; |
| 800 | } |
| 801 | |
| 802 | Error ProcessFreeBSD::GetWatchpointSupportInfo(uint32_t &num, bool &after) { |
| 803 | Error error = GetWatchpointSupportInfo(num); |
| 804 | // Watchpoints trigger and halt the inferior after |
| 805 | // the corresponding instruction has been executed. |
| 806 | after = true; |
| 807 | return error; |
| 808 | } |
| 809 | |
| 810 | uint32_t ProcessFreeBSD::UpdateThreadListIfNeeded() { |
| 811 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
| 812 | // Do not allow recursive updates. |
| 813 | return m_thread_list.GetSize(false); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | #if 0 |
| 817 | bool |
| 818 | ProcessFreeBSD::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) |
| 819 | { |
| 820 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD)); |
| 821 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
| 822 | log->Printf ("ProcessFreeBSD::%s() (pid = %" PRIi64 ")", __FUNCTION__, GetID()); |
| 823 | |
| 824 | bool has_updated = false; |
| 825 | // Update the process thread list with this new thread. |
| 826 | // FIXME: We should be using tid, not pid. |
| 827 | assert(m_monitor); |
| 828 | ThreadSP thread_sp (old_thread_list.FindThreadByID (GetID(), false)); |
| 829 | if (!thread_sp) { |
| 830 | thread_sp.reset(CreateNewFreeBSDThread(*this, GetID())); |
| 831 | has_updated = true; |
| 832 | } |
| 833 | |
| 834 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
| 835 | log->Printf ("ProcessFreeBSD::%s() updated pid = %" PRIi64, __FUNCTION__, GetID()); |
| 836 | new_thread_list.AddThread(thread_sp); |
| 837 | |
| 838 | return has_updated; // the list has been updated |
| 839 | } |
| 840 | #endif |
| 841 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 842 | ByteOrder ProcessFreeBSD::GetByteOrder() const { |
| 843 | // FIXME: We should be able to extract this value directly. See comment in |
| 844 | // ProcessFreeBSD(). |
| 845 | return m_byte_order; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 848 | size_t ProcessFreeBSD::PutSTDIN(const char *buf, size_t len, Error &error) { |
| 849 | ssize_t status; |
| 850 | if ((status = write(m_monitor->GetTerminalFD(), buf, len)) < 0) { |
| 851 | error.SetErrorToErrno(); |
| 852 | return 0; |
| 853 | } |
| 854 | return status; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | //------------------------------------------------------------------------------ |
| 858 | // Utility functions. |
| 859 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 860 | bool ProcessFreeBSD::HasExited() { |
| 861 | switch (GetPrivateState()) { |
| 862 | default: |
| 863 | break; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 864 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 865 | case eStateDetached: |
| 866 | case eStateExited: |
| 867 | return true; |
| 868 | } |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 869 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 870 | return false; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 873 | bool ProcessFreeBSD::IsStopped() { |
| 874 | switch (GetPrivateState()) { |
| 875 | default: |
| 876 | break; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 877 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 878 | case eStateStopped: |
| 879 | case eStateCrashed: |
| 880 | case eStateSuspended: |
| 881 | return true; |
| 882 | } |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 883 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 884 | return false; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 887 | bool ProcessFreeBSD::IsAThreadRunning() { |
| 888 | bool is_running = false; |
| 889 | std::lock_guard<std::recursive_mutex> guard(m_thread_list.GetMutex()); |
| 890 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 891 | for (uint32_t i = 0; i < thread_count; ++i) { |
| 892 | FreeBSDThread *thread = static_cast<FreeBSDThread *>( |
| 893 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 894 | StateType thread_state = thread->GetState(); |
| 895 | if (thread_state == eStateRunning || thread_state == eStateStepping) { |
| 896 | is_running = true; |
| 897 | break; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 898 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 899 | } |
| 900 | return is_running; |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 903 | const DataBufferSP ProcessFreeBSD::GetAuxvData() { |
| 904 | // If we're the local platform, we can ask the host for auxv data. |
| 905 | PlatformSP platform_sp = GetTarget().GetPlatform(); |
| 906 | if (platform_sp && platform_sp->IsHost()) |
| 907 | return lldb_private::Host::GetAuxvData(this); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 908 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 909 | // Somewhat unexpected - the process is not running locally or we don't have a |
| 910 | // platform. |
| 911 | assert( |
| 912 | false && |
| 913 | "no platform or not the host - how did we get here with ProcessFreeBSD?"); |
| 914 | return DataBufferSP(); |
Ed Maste | fe5a642 | 2015-07-28 15:45:57 +0000 | [diff] [blame] | 915 | } |