Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Process.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 | #include "lldb/Target/Process.h" |
| 11 | |
| 12 | #include "lldb/lldb-private-log.h" |
| 13 | |
| 14 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
| 15 | #include "lldb/Breakpoint/BreakpointLocation.h" |
| 16 | #include "lldb/Core/Event.h" |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 17 | #include "lldb/Core/ConnectionFileDescriptor.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Debugger.h" |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 19 | #include "lldb/Core/InputReader.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Log.h" |
| 21 | #include "lldb/Core/PluginManager.h" |
| 22 | #include "lldb/Core/State.h" |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 23 | #include "lldb/Interpreter/CommandInterpreter.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | #include "lldb/Host/Host.h" |
| 25 | #include "lldb/Target/ABI.h" |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 26 | #include "lldb/Target/DynamicLoader.h" |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 27 | #include "lldb/Target/LanguageRuntime.h" |
| 28 | #include "lldb/Target/CPPLanguageRuntime.h" |
| 29 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 31 | #include "lldb/Target/StopInfo.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | #include "lldb/Target/Target.h" |
| 33 | #include "lldb/Target/TargetList.h" |
| 34 | #include "lldb/Target/Thread.h" |
| 35 | #include "lldb/Target/ThreadPlan.h" |
| 36 | |
| 37 | using namespace lldb; |
| 38 | using namespace lldb_private; |
| 39 | |
| 40 | Process* |
| 41 | Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener) |
| 42 | { |
| 43 | ProcessCreateInstance create_callback = NULL; |
| 44 | if (plugin_name) |
| 45 | { |
| 46 | create_callback = PluginManager::GetProcessCreateCallbackForPluginName (plugin_name); |
| 47 | if (create_callback) |
| 48 | { |
| 49 | std::auto_ptr<Process> debugger_ap(create_callback(target, listener)); |
| 50 | if (debugger_ap->CanDebug(target)) |
| 51 | return debugger_ap.release(); |
| 52 | } |
| 53 | } |
| 54 | else |
| 55 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 56 | for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 58 | std::auto_ptr<Process> debugger_ap(create_callback(target, listener)); |
| 59 | if (debugger_ap->CanDebug(target)) |
| 60 | return debugger_ap.release(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | //---------------------------------------------------------------------- |
| 68 | // Process constructor |
| 69 | //---------------------------------------------------------------------- |
| 70 | Process::Process(Target &target, Listener &listener) : |
| 71 | UserID (LLDB_INVALID_PROCESS_ID), |
Greg Clayton | 49ce682 | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 72 | Broadcaster ("lldb.process"), |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 73 | ProcessInstanceSettings (*(Process::GetSettingsController().get())), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | m_target (target), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 75 | m_public_state (eStateUnloaded), |
| 76 | m_private_state (eStateUnloaded), |
| 77 | m_private_state_broadcaster ("lldb.process.internal_state_broadcaster"), |
| 78 | m_private_state_control_broadcaster ("lldb.process.internal_state_control_broadcaster"), |
| 79 | m_private_state_listener ("lldb.process.internal_state_listener"), |
| 80 | m_private_state_control_wait(), |
| 81 | m_private_state_thread (LLDB_INVALID_HOST_THREAD), |
| 82 | m_stop_id (0), |
| 83 | m_thread_index_id (0), |
| 84 | m_exit_status (-1), |
| 85 | m_exit_string (), |
| 86 | m_thread_list (this), |
| 87 | m_notifications (), |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 88 | m_image_tokens (), |
| 89 | m_listener (listener), |
| 90 | m_breakpoint_site_list (), |
| 91 | m_persistent_vars (), |
| 92 | m_dynamic_checkers_ap (), |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 93 | m_unix_signals (), |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 94 | m_target_triple (), |
| 95 | m_byte_order (eByteOrderHost), |
| 96 | m_addr_byte_size (0), |
| 97 | m_abi_sp (), |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 98 | m_process_input_reader (), |
| 99 | m_stdio_communication ("lldb.process.stdio"), |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 100 | m_stdio_communication_mutex (Mutex::eMutexTypeRecursive), |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 101 | m_stdout_data () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | { |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 103 | UpdateInstanceName(); |
| 104 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 105 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | if (log) |
| 107 | log->Printf ("%p Process::Process()", this); |
| 108 | |
Greg Clayton | 49ce682 | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 109 | SetEventName (eBroadcastBitStateChanged, "state-changed"); |
| 110 | SetEventName (eBroadcastBitInterrupt, "interrupt"); |
| 111 | SetEventName (eBroadcastBitSTDOUT, "stdout-available"); |
| 112 | SetEventName (eBroadcastBitSTDERR, "stderr-available"); |
| 113 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 114 | listener.StartListeningForEvents (this, |
| 115 | eBroadcastBitStateChanged | |
| 116 | eBroadcastBitInterrupt | |
| 117 | eBroadcastBitSTDOUT | |
| 118 | eBroadcastBitSTDERR); |
| 119 | |
| 120 | m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster, |
| 121 | eBroadcastBitStateChanged); |
| 122 | |
| 123 | m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster, |
| 124 | eBroadcastInternalStateControlStop | |
| 125 | eBroadcastInternalStateControlPause | |
| 126 | eBroadcastInternalStateControlResume); |
| 127 | } |
| 128 | |
| 129 | //---------------------------------------------------------------------- |
| 130 | // Destructor |
| 131 | //---------------------------------------------------------------------- |
| 132 | Process::~Process() |
| 133 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 134 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 135 | if (log) |
| 136 | log->Printf ("%p Process::~Process()", this); |
| 137 | StopPrivateStateThread(); |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | Process::Finalize() |
| 142 | { |
| 143 | // Do any cleanup needed prior to being destructed... Subclasses |
| 144 | // that override this method should call this superclass method as well. |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | Process::RegisterNotificationCallbacks (const Notifications& callbacks) |
| 149 | { |
| 150 | m_notifications.push_back(callbacks); |
| 151 | if (callbacks.initialize != NULL) |
| 152 | callbacks.initialize (callbacks.baton, this); |
| 153 | } |
| 154 | |
| 155 | bool |
| 156 | Process::UnregisterNotificationCallbacks(const Notifications& callbacks) |
| 157 | { |
| 158 | std::vector<Notifications>::iterator pos, end = m_notifications.end(); |
| 159 | for (pos = m_notifications.begin(); pos != end; ++pos) |
| 160 | { |
| 161 | if (pos->baton == callbacks.baton && |
| 162 | pos->initialize == callbacks.initialize && |
| 163 | pos->process_state_changed == callbacks.process_state_changed) |
| 164 | { |
| 165 | m_notifications.erase(pos); |
| 166 | return true; |
| 167 | } |
| 168 | } |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | Process::SynchronouslyNotifyStateChanged (StateType state) |
| 174 | { |
| 175 | std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end(); |
| 176 | for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos) |
| 177 | { |
| 178 | if (notification_pos->process_state_changed) |
| 179 | notification_pos->process_state_changed (notification_pos->baton, this, state); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // FIXME: We need to do some work on events before the general Listener sees them. |
| 184 | // For instance if we are continuing from a breakpoint, we need to ensure that we do |
| 185 | // the little "insert real insn, step & stop" trick. But we can't do that when the |
| 186 | // event is delivered by the broadcaster - since that is done on the thread that is |
| 187 | // waiting for new events, so if we needed more than one event for our handling, we would |
| 188 | // stall. So instead we do it when we fetch the event off of the queue. |
| 189 | // |
| 190 | |
| 191 | StateType |
| 192 | Process::GetNextEvent (EventSP &event_sp) |
| 193 | { |
| 194 | StateType state = eStateInvalid; |
| 195 | |
| 196 | if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp) |
| 197 | state = Process::ProcessEventData::GetStateFromEvent (event_sp.get()); |
| 198 | |
| 199 | return state; |
| 200 | } |
| 201 | |
| 202 | |
| 203 | StateType |
| 204 | Process::WaitForProcessToStop (const TimeValue *timeout) |
| 205 | { |
| 206 | StateType match_states[] = { eStateStopped, eStateCrashed, eStateDetached, eStateExited, eStateUnloaded }; |
| 207 | return WaitForState (timeout, match_states, sizeof(match_states) / sizeof(StateType)); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | StateType |
| 212 | Process::WaitForState |
| 213 | ( |
| 214 | const TimeValue *timeout, |
| 215 | const StateType *match_states, const uint32_t num_match_states |
| 216 | ) |
| 217 | { |
| 218 | EventSP event_sp; |
| 219 | uint32_t i; |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 220 | StateType state = GetState(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 221 | while (state != eStateInvalid) |
| 222 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 223 | // If we are exited or detached, we won't ever get back to any |
| 224 | // other valid state... |
| 225 | if (state == eStateDetached || state == eStateExited) |
| 226 | return state; |
| 227 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | state = WaitForStateChangedEvents (timeout, event_sp); |
| 229 | |
| 230 | for (i=0; i<num_match_states; ++i) |
| 231 | { |
| 232 | if (match_states[i] == state) |
| 233 | return state; |
| 234 | } |
| 235 | } |
| 236 | return state; |
| 237 | } |
| 238 | |
Jim Ingham | 63e24d7 | 2010-10-11 23:53:14 +0000 | [diff] [blame] | 239 | bool |
| 240 | Process::HijackProcessEvents (Listener *listener) |
| 241 | { |
| 242 | if (listener != NULL) |
| 243 | { |
| 244 | return HijackBroadcaster(listener, eBroadcastBitStateChanged); |
| 245 | } |
| 246 | else |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | void |
| 251 | Process::RestoreProcessEvents () |
| 252 | { |
| 253 | RestoreBroadcaster(); |
| 254 | } |
| 255 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 256 | StateType |
| 257 | Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp) |
| 258 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 259 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 260 | |
| 261 | if (log) |
| 262 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 263 | |
| 264 | StateType state = eStateInvalid; |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 265 | if (m_listener.WaitForEventForBroadcasterWithType (timeout, |
| 266 | this, |
| 267 | eBroadcastBitStateChanged, |
| 268 | event_sp)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 269 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 270 | |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 271 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 272 | if (log) |
| 273 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", |
| 274 | __FUNCTION__, |
| 275 | timeout, |
| 276 | StateAsCString(state)); |
| 277 | return state; |
| 278 | } |
| 279 | |
| 280 | Event * |
| 281 | Process::PeekAtStateChangedEvents () |
| 282 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 283 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 284 | |
| 285 | if (log) |
| 286 | log->Printf ("Process::%s...", __FUNCTION__); |
| 287 | |
| 288 | Event *event_ptr; |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 289 | event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this, |
| 290 | eBroadcastBitStateChanged); |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 291 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 292 | if (log) |
| 293 | { |
| 294 | if (event_ptr) |
| 295 | { |
| 296 | log->Printf ("Process::%s (event_ptr) => %s", |
| 297 | __FUNCTION__, |
| 298 | StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr))); |
| 299 | } |
| 300 | else |
| 301 | { |
| 302 | log->Printf ("Process::%s no events found", |
| 303 | __FUNCTION__); |
| 304 | } |
| 305 | } |
| 306 | return event_ptr; |
| 307 | } |
| 308 | |
| 309 | StateType |
| 310 | Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 311 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 312 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | |
| 314 | if (log) |
| 315 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 316 | |
| 317 | StateType state = eStateInvalid; |
| 318 | if (m_private_state_listener.WaitForEventForBroadcasterWithType(timeout, |
| 319 | &m_private_state_broadcaster, |
| 320 | eBroadcastBitStateChanged, |
| 321 | event_sp)) |
| 322 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 323 | |
| 324 | // This is a bit of a hack, but when we wait here we could very well return |
| 325 | // to the command-line, and that could disable the log, which would render the |
| 326 | // log we got above invalid. |
| 327 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
| 328 | if (log) |
| 329 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", __FUNCTION__, timeout, StateAsCString(state)); |
| 330 | return state; |
| 331 | } |
| 332 | |
| 333 | bool |
| 334 | Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only) |
| 335 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 336 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 337 | |
| 338 | if (log) |
| 339 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 340 | |
| 341 | if (control_only) |
| 342 | return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp); |
| 343 | else |
| 344 | return m_private_state_listener.WaitForEvent(timeout, event_sp); |
| 345 | } |
| 346 | |
| 347 | bool |
| 348 | Process::IsRunning () const |
| 349 | { |
| 350 | return StateIsRunningState (m_public_state.GetValue()); |
| 351 | } |
| 352 | |
| 353 | int |
| 354 | Process::GetExitStatus () |
| 355 | { |
| 356 | if (m_public_state.GetValue() == eStateExited) |
| 357 | return m_exit_status; |
| 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | const char * |
| 362 | Process::GetExitDescription () |
| 363 | { |
| 364 | if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty()) |
| 365 | return m_exit_string.c_str(); |
| 366 | return NULL; |
| 367 | } |
| 368 | |
| 369 | void |
| 370 | Process::SetExitStatus (int status, const char *cstr) |
| 371 | { |
| 372 | m_exit_status = status; |
| 373 | if (cstr) |
| 374 | m_exit_string = cstr; |
| 375 | else |
| 376 | m_exit_string.clear(); |
| 377 | |
| 378 | SetPrivateState (eStateExited); |
| 379 | } |
| 380 | |
| 381 | // This static callback can be used to watch for local child processes on |
| 382 | // the current host. The the child process exits, the process will be |
| 383 | // found in the global target list (we want to be completely sure that the |
| 384 | // lldb_private::Process doesn't go away before we can deliver the signal. |
| 385 | bool |
| 386 | Process::SetProcessExitStatus |
| 387 | ( |
| 388 | void *callback_baton, |
| 389 | lldb::pid_t pid, |
| 390 | int signo, // Zero for no signal |
| 391 | int exit_status // Exit value of process if signal is zero |
| 392 | ) |
| 393 | { |
| 394 | if (signo == 0 || exit_status) |
| 395 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 396 | TargetSP target_sp(Debugger::FindTargetWithProcessID (pid)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 397 | if (target_sp) |
| 398 | { |
| 399 | ProcessSP process_sp (target_sp->GetProcessSP()); |
| 400 | if (process_sp) |
| 401 | { |
| 402 | const char *signal_cstr = NULL; |
| 403 | if (signo) |
| 404 | signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo); |
| 405 | |
| 406 | process_sp->SetExitStatus (exit_status, signal_cstr); |
| 407 | } |
| 408 | } |
| 409 | return true; |
| 410 | } |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | |
| 415 | uint32_t |
| 416 | Process::GetNextThreadIndexID () |
| 417 | { |
| 418 | return ++m_thread_index_id; |
| 419 | } |
| 420 | |
| 421 | StateType |
| 422 | Process::GetState() |
| 423 | { |
| 424 | // If any other threads access this we will need a mutex for it |
| 425 | return m_public_state.GetValue (); |
| 426 | } |
| 427 | |
| 428 | void |
| 429 | Process::SetPublicState (StateType new_state) |
| 430 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 431 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 432 | if (log) |
| 433 | log->Printf("Process::SetPublicState (%s)", StateAsCString(new_state)); |
| 434 | m_public_state.SetValue (new_state); |
| 435 | } |
| 436 | |
| 437 | StateType |
| 438 | Process::GetPrivateState () |
| 439 | { |
| 440 | return m_private_state.GetValue(); |
| 441 | } |
| 442 | |
| 443 | void |
| 444 | Process::SetPrivateState (StateType new_state) |
| 445 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 446 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 447 | bool state_changed = false; |
| 448 | |
| 449 | if (log) |
| 450 | log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state)); |
| 451 | |
| 452 | Mutex::Locker locker(m_private_state.GetMutex()); |
| 453 | |
| 454 | const StateType old_state = m_private_state.GetValueNoLock (); |
| 455 | state_changed = old_state != new_state; |
| 456 | if (state_changed) |
| 457 | { |
| 458 | m_private_state.SetValueNoLock (new_state); |
| 459 | if (StateIsStoppedState(new_state)) |
| 460 | { |
| 461 | m_stop_id++; |
| 462 | if (log) |
| 463 | log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_stop_id); |
| 464 | } |
| 465 | // Use our target to get a shared pointer to ourselves... |
| 466 | m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (GetTarget().GetProcessSP(), new_state)); |
| 467 | } |
| 468 | else |
| 469 | { |
| 470 | if (log) |
| 471 | log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state), StateAsCString(old_state)); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | |
| 476 | uint32_t |
| 477 | Process::GetStopID() const |
| 478 | { |
| 479 | return m_stop_id; |
| 480 | } |
| 481 | |
| 482 | addr_t |
| 483 | Process::GetImageInfoAddress() |
| 484 | { |
| 485 | return LLDB_INVALID_ADDRESS; |
| 486 | } |
| 487 | |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 488 | //---------------------------------------------------------------------- |
| 489 | // LoadImage |
| 490 | // |
| 491 | // This function provides a default implementation that works for most |
| 492 | // unix variants. Any Process subclasses that need to do shared library |
| 493 | // loading differently should override LoadImage and UnloadImage and |
| 494 | // do what is needed. |
| 495 | //---------------------------------------------------------------------- |
| 496 | uint32_t |
| 497 | Process::LoadImage (const FileSpec &image_spec, Error &error) |
| 498 | { |
| 499 | DynamicLoader *loader = GetDynamicLoader(); |
| 500 | if (loader) |
| 501 | { |
| 502 | error = loader->CanLoadImage(); |
| 503 | if (error.Fail()) |
| 504 | return LLDB_INVALID_IMAGE_TOKEN; |
| 505 | } |
| 506 | |
| 507 | if (error.Success()) |
| 508 | { |
| 509 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
| 510 | if (thread_sp == NULL) |
| 511 | thread_sp = GetThreadList ().GetThreadAtIndex(0, true); |
| 512 | |
| 513 | if (thread_sp) |
| 514 | { |
| 515 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
| 516 | |
| 517 | if (frame_sp) |
| 518 | { |
| 519 | ExecutionContext exe_ctx; |
| 520 | frame_sp->CalculateExecutionContext (exe_ctx); |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 521 | bool unwind_on_error = true; |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 522 | StreamString expr; |
| 523 | char path[PATH_MAX]; |
| 524 | image_spec.GetPath(path, sizeof(path)); |
| 525 | expr.Printf("dlopen (\"%s\", 2)", path); |
| 526 | const char *prefix = "extern \"C\" void* dlopen (const char *path, int mode);\n"; |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 527 | lldb::ValueObjectSP result_valobj_sp (ClangUserExpression::Evaluate (exe_ctx, unwind_on_error, expr.GetData(), prefix)); |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 528 | if (result_valobj_sp->GetError().Success()) |
| 529 | { |
| 530 | Scalar scalar; |
| 531 | if (result_valobj_sp->ResolveValue (frame_sp.get(), scalar)) |
| 532 | { |
| 533 | addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 534 | if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS) |
| 535 | { |
| 536 | uint32_t image_token = m_image_tokens.size(); |
| 537 | m_image_tokens.push_back (image_ptr); |
| 538 | return image_token; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | return LLDB_INVALID_IMAGE_TOKEN; |
| 546 | } |
| 547 | |
| 548 | //---------------------------------------------------------------------- |
| 549 | // UnloadImage |
| 550 | // |
| 551 | // This function provides a default implementation that works for most |
| 552 | // unix variants. Any Process subclasses that need to do shared library |
| 553 | // loading differently should override LoadImage and UnloadImage and |
| 554 | // do what is needed. |
| 555 | //---------------------------------------------------------------------- |
| 556 | Error |
| 557 | Process::UnloadImage (uint32_t image_token) |
| 558 | { |
| 559 | Error error; |
| 560 | if (image_token < m_image_tokens.size()) |
| 561 | { |
| 562 | const addr_t image_addr = m_image_tokens[image_token]; |
| 563 | if (image_addr == LLDB_INVALID_ADDRESS) |
| 564 | { |
| 565 | error.SetErrorString("image already unloaded"); |
| 566 | } |
| 567 | else |
| 568 | { |
| 569 | DynamicLoader *loader = GetDynamicLoader(); |
| 570 | if (loader) |
| 571 | error = loader->CanLoadImage(); |
| 572 | |
| 573 | if (error.Success()) |
| 574 | { |
| 575 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
| 576 | if (thread_sp == NULL) |
| 577 | thread_sp = GetThreadList ().GetThreadAtIndex(0, true); |
| 578 | |
| 579 | if (thread_sp) |
| 580 | { |
| 581 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
| 582 | |
| 583 | if (frame_sp) |
| 584 | { |
| 585 | ExecutionContext exe_ctx; |
| 586 | frame_sp->CalculateExecutionContext (exe_ctx); |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 587 | bool unwind_on_error = true; |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 588 | StreamString expr; |
| 589 | expr.Printf("dlclose ((void *)0x%llx)", image_addr); |
| 590 | const char *prefix = "extern \"C\" int dlclose(void* handle);\n"; |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 591 | lldb::ValueObjectSP result_valobj_sp (ClangUserExpression::Evaluate (exe_ctx, unwind_on_error, expr.GetData(), prefix)); |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 592 | if (result_valobj_sp->GetError().Success()) |
| 593 | { |
| 594 | Scalar scalar; |
| 595 | if (result_valobj_sp->ResolveValue (frame_sp.get(), scalar)) |
| 596 | { |
| 597 | if (scalar.UInt(1)) |
| 598 | { |
| 599 | error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData()); |
| 600 | } |
| 601 | else |
| 602 | { |
| 603 | m_image_tokens[image_token] = LLDB_INVALID_ADDRESS; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | else |
| 608 | { |
| 609 | error = result_valobj_sp->GetError(); |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | else |
| 617 | { |
| 618 | error.SetErrorString("invalid image token"); |
| 619 | } |
| 620 | return error; |
| 621 | } |
| 622 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 623 | DynamicLoader * |
| 624 | Process::GetDynamicLoader() |
| 625 | { |
| 626 | return NULL; |
| 627 | } |
| 628 | |
| 629 | const ABI * |
| 630 | Process::GetABI() |
| 631 | { |
| 632 | ConstString& triple = m_target_triple; |
| 633 | |
| 634 | if (triple.IsEmpty()) |
| 635 | return NULL; |
| 636 | |
| 637 | if (m_abi_sp.get() == NULL) |
| 638 | { |
| 639 | m_abi_sp.reset(ABI::FindPlugin(triple)); |
| 640 | } |
| 641 | |
| 642 | return m_abi_sp.get(); |
| 643 | } |
| 644 | |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 645 | LanguageRuntime * |
| 646 | Process::GetLanguageRuntime(lldb::LanguageType language) |
| 647 | { |
| 648 | LanguageRuntimeCollection::iterator pos; |
| 649 | pos = m_language_runtimes.find (language); |
| 650 | if (pos == m_language_runtimes.end()) |
| 651 | { |
| 652 | lldb::LanguageRuntimeSP runtime(LanguageRuntime::FindPlugin(this, language)); |
| 653 | |
| 654 | m_language_runtimes[language] |
| 655 | = runtime; |
| 656 | return runtime.get(); |
| 657 | } |
| 658 | else |
| 659 | return (*pos).second.get(); |
| 660 | } |
| 661 | |
| 662 | CPPLanguageRuntime * |
| 663 | Process::GetCPPLanguageRuntime () |
| 664 | { |
| 665 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus); |
| 666 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus) |
| 667 | return static_cast<CPPLanguageRuntime *> (runtime); |
| 668 | return NULL; |
| 669 | } |
| 670 | |
| 671 | ObjCLanguageRuntime * |
| 672 | Process::GetObjCLanguageRuntime () |
| 673 | { |
| 674 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC); |
| 675 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC) |
| 676 | return static_cast<ObjCLanguageRuntime *> (runtime); |
| 677 | return NULL; |
| 678 | } |
| 679 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 680 | BreakpointSiteList & |
| 681 | Process::GetBreakpointSiteList() |
| 682 | { |
| 683 | return m_breakpoint_site_list; |
| 684 | } |
| 685 | |
| 686 | const BreakpointSiteList & |
| 687 | Process::GetBreakpointSiteList() const |
| 688 | { |
| 689 | return m_breakpoint_site_list; |
| 690 | } |
| 691 | |
| 692 | |
| 693 | void |
| 694 | Process::DisableAllBreakpointSites () |
| 695 | { |
| 696 | m_breakpoint_site_list.SetEnabledForAll (false); |
| 697 | } |
| 698 | |
| 699 | Error |
| 700 | Process::ClearBreakpointSiteByID (lldb::user_id_t break_id) |
| 701 | { |
| 702 | Error error (DisableBreakpointSiteByID (break_id)); |
| 703 | |
| 704 | if (error.Success()) |
| 705 | m_breakpoint_site_list.Remove(break_id); |
| 706 | |
| 707 | return error; |
| 708 | } |
| 709 | |
| 710 | Error |
| 711 | Process::DisableBreakpointSiteByID (lldb::user_id_t break_id) |
| 712 | { |
| 713 | Error error; |
| 714 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 715 | if (bp_site_sp) |
| 716 | { |
| 717 | if (bp_site_sp->IsEnabled()) |
| 718 | error = DisableBreakpoint (bp_site_sp.get()); |
| 719 | } |
| 720 | else |
| 721 | { |
| 722 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id); |
| 723 | } |
| 724 | |
| 725 | return error; |
| 726 | } |
| 727 | |
| 728 | Error |
| 729 | Process::EnableBreakpointSiteByID (lldb::user_id_t break_id) |
| 730 | { |
| 731 | Error error; |
| 732 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 733 | if (bp_site_sp) |
| 734 | { |
| 735 | if (!bp_site_sp->IsEnabled()) |
| 736 | error = EnableBreakpoint (bp_site_sp.get()); |
| 737 | } |
| 738 | else |
| 739 | { |
| 740 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id); |
| 741 | } |
| 742 | return error; |
| 743 | } |
| 744 | |
Stephen Wilson | 3fd1f36 | 2010-07-17 00:56:13 +0000 | [diff] [blame] | 745 | lldb::break_id_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 746 | Process::CreateBreakpointSite (BreakpointLocationSP &owner, bool use_hardware) |
| 747 | { |
Greg Clayton | eea2640 | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 748 | const addr_t load_addr = owner->GetAddress().GetLoadAddress (&m_target); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 749 | if (load_addr != LLDB_INVALID_ADDRESS) |
| 750 | { |
| 751 | BreakpointSiteSP bp_site_sp; |
| 752 | |
| 753 | // Look up this breakpoint site. If it exists, then add this new owner, otherwise |
| 754 | // create a new breakpoint site and add it. |
| 755 | |
| 756 | bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr); |
| 757 | |
| 758 | if (bp_site_sp) |
| 759 | { |
| 760 | bp_site_sp->AddOwner (owner); |
| 761 | owner->SetBreakpointSite (bp_site_sp); |
| 762 | return bp_site_sp->GetID(); |
| 763 | } |
| 764 | else |
| 765 | { |
| 766 | bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, LLDB_INVALID_THREAD_ID, use_hardware)); |
| 767 | if (bp_site_sp) |
| 768 | { |
| 769 | if (EnableBreakpoint (bp_site_sp.get()).Success()) |
| 770 | { |
| 771 | owner->SetBreakpointSite (bp_site_sp); |
| 772 | return m_breakpoint_site_list.Add (bp_site_sp); |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | // We failed to enable the breakpoint |
| 778 | return LLDB_INVALID_BREAK_ID; |
| 779 | |
| 780 | } |
| 781 | |
| 782 | void |
| 783 | Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp) |
| 784 | { |
| 785 | uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id); |
| 786 | if (num_owners == 0) |
| 787 | { |
| 788 | DisableBreakpoint(bp_site_sp.get()); |
| 789 | m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress()); |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | |
| 794 | size_t |
| 795 | Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const |
| 796 | { |
| 797 | size_t bytes_removed = 0; |
| 798 | addr_t intersect_addr; |
| 799 | size_t intersect_size; |
| 800 | size_t opcode_offset; |
| 801 | size_t idx; |
| 802 | BreakpointSiteSP bp; |
| 803 | |
| 804 | for (idx = 0; (bp = m_breakpoint_site_list.GetByIndex(idx)) != NULL; ++idx) |
| 805 | { |
| 806 | if (bp->GetType() == BreakpointSite::eSoftware) |
| 807 | { |
| 808 | if (bp->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset)) |
| 809 | { |
| 810 | assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size); |
| 811 | assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size); |
| 812 | assert(opcode_offset + intersect_size <= bp->GetByteSize()); |
| 813 | size_t buf_offset = intersect_addr - bp_addr; |
| 814 | ::memcpy(buf + buf_offset, bp->GetSavedOpcodeBytes() + opcode_offset, intersect_size); |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | return bytes_removed; |
| 819 | } |
| 820 | |
| 821 | |
| 822 | Error |
| 823 | Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 824 | { |
| 825 | Error error; |
| 826 | assert (bp_site != NULL); |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 827 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 828 | const addr_t bp_addr = bp_site->GetLoadAddress(); |
| 829 | if (log) |
| 830 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx", bp_site->GetID(), (uint64_t)bp_addr); |
| 831 | if (bp_site->IsEnabled()) |
| 832 | { |
| 833 | if (log) |
| 834 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already enabled", bp_site->GetID(), (uint64_t)bp_addr); |
| 835 | return error; |
| 836 | } |
| 837 | |
| 838 | if (bp_addr == LLDB_INVALID_ADDRESS) |
| 839 | { |
| 840 | error.SetErrorString("BreakpointSite contains an invalid load address."); |
| 841 | return error; |
| 842 | } |
| 843 | // Ask the lldb::Process subclass to fill in the correct software breakpoint |
| 844 | // trap for the breakpoint site |
| 845 | const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 846 | |
| 847 | if (bp_opcode_size == 0) |
| 848 | { |
| 849 | error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%llx.\n", bp_addr); |
| 850 | } |
| 851 | else |
| 852 | { |
| 853 | const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes(); |
| 854 | |
| 855 | if (bp_opcode_bytes == NULL) |
| 856 | { |
| 857 | error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode."); |
| 858 | return error; |
| 859 | } |
| 860 | |
| 861 | // Save the original opcode by reading it |
| 862 | if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size) |
| 863 | { |
| 864 | // Write a software breakpoint in place of the original opcode |
| 865 | if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 866 | { |
| 867 | uint8_t verify_bp_opcode_bytes[64]; |
| 868 | if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 869 | { |
| 870 | if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0) |
| 871 | { |
| 872 | bp_site->SetEnabled(true); |
| 873 | bp_site->SetType (BreakpointSite::eSoftware); |
| 874 | if (log) |
| 875 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", |
| 876 | bp_site->GetID(), |
| 877 | (uint64_t)bp_addr); |
| 878 | } |
| 879 | else |
| 880 | error.SetErrorString("Failed to verify the breakpoint trap in memory."); |
| 881 | } |
| 882 | else |
| 883 | error.SetErrorString("Unable to read memory to verify breakpoint trap."); |
| 884 | } |
| 885 | else |
| 886 | error.SetErrorString("Unable to write breakpoint trap to memory."); |
| 887 | } |
| 888 | else |
| 889 | error.SetErrorString("Unable to read memory at breakpoint address."); |
| 890 | } |
| 891 | if (log) |
| 892 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s", |
| 893 | bp_site->GetID(), |
| 894 | (uint64_t)bp_addr, |
| 895 | error.AsCString()); |
| 896 | return error; |
| 897 | } |
| 898 | |
| 899 | Error |
| 900 | Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 901 | { |
| 902 | Error error; |
| 903 | assert (bp_site != NULL); |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 904 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 905 | addr_t bp_addr = bp_site->GetLoadAddress(); |
| 906 | lldb::user_id_t breakID = bp_site->GetID(); |
| 907 | if (log) |
| 908 | log->Printf ("ProcessMacOSX::DisableBreakpoint (breakID = %d) addr = 0x%llx", breakID, (uint64_t)bp_addr); |
| 909 | |
| 910 | if (bp_site->IsHardware()) |
| 911 | { |
| 912 | error.SetErrorString("Breakpoint site is a hardware breakpoint."); |
| 913 | } |
| 914 | else if (bp_site->IsEnabled()) |
| 915 | { |
| 916 | const size_t break_op_size = bp_site->GetByteSize(); |
| 917 | const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes(); |
| 918 | if (break_op_size > 0) |
| 919 | { |
| 920 | // Clear a software breakoint instruction |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 921 | uint8_t curr_break_op[8]; |
Stephen Wilson | 141eeac | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 922 | assert (break_op_size <= sizeof(curr_break_op)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 923 | bool break_op_found = false; |
| 924 | |
| 925 | // Read the breakpoint opcode |
| 926 | if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size) |
| 927 | { |
| 928 | bool verify = false; |
| 929 | // Make sure we have the a breakpoint opcode exists at this address |
| 930 | if (::memcmp (curr_break_op, break_op, break_op_size) == 0) |
| 931 | { |
| 932 | break_op_found = true; |
| 933 | // We found a valid breakpoint opcode at this address, now restore |
| 934 | // the saved opcode. |
| 935 | if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size) |
| 936 | { |
| 937 | verify = true; |
| 938 | } |
| 939 | else |
| 940 | error.SetErrorString("Memory write failed when restoring original opcode."); |
| 941 | } |
| 942 | else |
| 943 | { |
| 944 | error.SetErrorString("Original breakpoint trap is no longer in memory."); |
| 945 | // Set verify to true and so we can check if the original opcode has already been restored |
| 946 | verify = true; |
| 947 | } |
| 948 | |
| 949 | if (verify) |
| 950 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 951 | uint8_t verify_opcode[8]; |
Stephen Wilson | 141eeac | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 952 | assert (break_op_size < sizeof(verify_opcode)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 953 | // Verify that our original opcode made it back to the inferior |
| 954 | if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size) |
| 955 | { |
| 956 | // compare the memory we just read with the original opcode |
| 957 | if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0) |
| 958 | { |
| 959 | // SUCCESS |
| 960 | bp_site->SetEnabled(false); |
| 961 | if (log) |
| 962 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", bp_site->GetID(), (uint64_t)bp_addr); |
| 963 | return error; |
| 964 | } |
| 965 | else |
| 966 | { |
| 967 | if (break_op_found) |
| 968 | error.SetErrorString("Failed to restore original opcode."); |
| 969 | } |
| 970 | } |
| 971 | else |
| 972 | error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored."); |
| 973 | } |
| 974 | } |
| 975 | else |
| 976 | error.SetErrorString("Unable to read memory that should contain the breakpoint trap."); |
| 977 | } |
| 978 | } |
| 979 | else |
| 980 | { |
| 981 | if (log) |
| 982 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already disabled", bp_site->GetID(), (uint64_t)bp_addr); |
| 983 | return error; |
| 984 | } |
| 985 | |
| 986 | if (log) |
| 987 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s", |
| 988 | bp_site->GetID(), |
| 989 | (uint64_t)bp_addr, |
| 990 | error.AsCString()); |
| 991 | return error; |
| 992 | |
| 993 | } |
| 994 | |
| 995 | |
| 996 | size_t |
| 997 | Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 998 | { |
| 999 | if (buf == NULL || size == 0) |
| 1000 | return 0; |
| 1001 | |
| 1002 | size_t bytes_read = 0; |
| 1003 | uint8_t *bytes = (uint8_t *)buf; |
| 1004 | |
| 1005 | while (bytes_read < size) |
| 1006 | { |
| 1007 | const size_t curr_size = size - bytes_read; |
| 1008 | const size_t curr_bytes_read = DoReadMemory (addr + bytes_read, |
| 1009 | bytes + bytes_read, |
| 1010 | curr_size, |
| 1011 | error); |
| 1012 | bytes_read += curr_bytes_read; |
| 1013 | if (curr_bytes_read == curr_size || curr_bytes_read == 0) |
| 1014 | break; |
| 1015 | } |
| 1016 | |
| 1017 | // Replace any software breakpoint opcodes that fall into this range back |
| 1018 | // into "buf" before we return |
| 1019 | if (bytes_read > 0) |
| 1020 | RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf); |
| 1021 | return bytes_read; |
| 1022 | } |
| 1023 | |
| 1024 | size_t |
| 1025 | Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error) |
| 1026 | { |
| 1027 | size_t bytes_written = 0; |
| 1028 | const uint8_t *bytes = (const uint8_t *)buf; |
| 1029 | |
| 1030 | while (bytes_written < size) |
| 1031 | { |
| 1032 | const size_t curr_size = size - bytes_written; |
| 1033 | const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written, |
| 1034 | bytes + bytes_written, |
| 1035 | curr_size, |
| 1036 | error); |
| 1037 | bytes_written += curr_bytes_written; |
| 1038 | if (curr_bytes_written == curr_size || curr_bytes_written == 0) |
| 1039 | break; |
| 1040 | } |
| 1041 | return bytes_written; |
| 1042 | } |
| 1043 | |
| 1044 | size_t |
| 1045 | Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 1046 | { |
| 1047 | if (buf == NULL || size == 0) |
| 1048 | return 0; |
| 1049 | // We need to write any data that would go where any current software traps |
| 1050 | // (enabled software breakpoints) any software traps (breakpoints) that we |
| 1051 | // may have placed in our tasks memory. |
| 1052 | |
| 1053 | BreakpointSiteList::collection::const_iterator iter = m_breakpoint_site_list.GetMap()->lower_bound (addr); |
| 1054 | BreakpointSiteList::collection::const_iterator end = m_breakpoint_site_list.GetMap()->end(); |
| 1055 | |
| 1056 | if (iter == end || iter->second->GetLoadAddress() > addr + size) |
| 1057 | return DoWriteMemory(addr, buf, size, error); |
| 1058 | |
| 1059 | BreakpointSiteList::collection::const_iterator pos; |
| 1060 | size_t bytes_written = 0; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1061 | addr_t intersect_addr = 0; |
| 1062 | size_t intersect_size = 0; |
| 1063 | size_t opcode_offset = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1064 | const uint8_t *ubuf = (const uint8_t *)buf; |
| 1065 | |
| 1066 | for (pos = iter; pos != end; ++pos) |
| 1067 | { |
| 1068 | BreakpointSiteSP bp; |
| 1069 | bp = pos->second; |
| 1070 | |
| 1071 | assert(bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset)); |
| 1072 | assert(addr <= intersect_addr && intersect_addr < addr + size); |
| 1073 | assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size); |
| 1074 | assert(opcode_offset + intersect_size <= bp->GetByteSize()); |
| 1075 | |
| 1076 | // Check for bytes before this breakpoint |
| 1077 | const addr_t curr_addr = addr + bytes_written; |
| 1078 | if (intersect_addr > curr_addr) |
| 1079 | { |
| 1080 | // There are some bytes before this breakpoint that we need to |
| 1081 | // just write to memory |
| 1082 | size_t curr_size = intersect_addr - curr_addr; |
| 1083 | size_t curr_bytes_written = WriteMemoryPrivate (curr_addr, |
| 1084 | ubuf + bytes_written, |
| 1085 | curr_size, |
| 1086 | error); |
| 1087 | bytes_written += curr_bytes_written; |
| 1088 | if (curr_bytes_written != curr_size) |
| 1089 | { |
| 1090 | // We weren't able to write all of the requested bytes, we |
| 1091 | // are done looping and will return the number of bytes that |
| 1092 | // we have written so far. |
| 1093 | break; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | // Now write any bytes that would cover up any software breakpoints |
| 1098 | // directly into the breakpoint opcode buffer |
| 1099 | ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size); |
| 1100 | bytes_written += intersect_size; |
| 1101 | } |
| 1102 | |
| 1103 | // Write any remaining bytes after the last breakpoint if we have any left |
| 1104 | if (bytes_written < size) |
| 1105 | bytes_written += WriteMemoryPrivate (addr + bytes_written, |
| 1106 | ubuf + bytes_written, |
| 1107 | size - bytes_written, |
| 1108 | error); |
| 1109 | |
| 1110 | return bytes_written; |
| 1111 | } |
| 1112 | |
| 1113 | addr_t |
| 1114 | Process::AllocateMemory(size_t size, uint32_t permissions, Error &error) |
| 1115 | { |
| 1116 | // Fixme: we should track the blocks we've allocated, and clean them up... |
| 1117 | // We could even do our own allocator here if that ends up being more efficient. |
| 1118 | return DoAllocateMemory (size, permissions, error); |
| 1119 | } |
| 1120 | |
| 1121 | Error |
| 1122 | Process::DeallocateMemory (addr_t ptr) |
| 1123 | { |
| 1124 | return DoDeallocateMemory (ptr); |
| 1125 | } |
| 1126 | |
| 1127 | |
| 1128 | Error |
| 1129 | Process::EnableWatchpoint (WatchpointLocation *watchpoint) |
| 1130 | { |
| 1131 | Error error; |
| 1132 | error.SetErrorString("watchpoints are not supported"); |
| 1133 | return error; |
| 1134 | } |
| 1135 | |
| 1136 | Error |
| 1137 | Process::DisableWatchpoint (WatchpointLocation *watchpoint) |
| 1138 | { |
| 1139 | Error error; |
| 1140 | error.SetErrorString("watchpoints are not supported"); |
| 1141 | return error; |
| 1142 | } |
| 1143 | |
| 1144 | StateType |
| 1145 | Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 1146 | { |
| 1147 | StateType state; |
| 1148 | // Now wait for the process to launch and return control to us, and then |
| 1149 | // call DidLaunch: |
| 1150 | while (1) |
| 1151 | { |
| 1152 | // FIXME: Might want to put a timeout in here: |
| 1153 | state = WaitForStateChangedEventsPrivate (NULL, event_sp); |
| 1154 | if (state == eStateStopped || state == eStateCrashed || state == eStateExited) |
| 1155 | break; |
| 1156 | else |
| 1157 | HandlePrivateEvent (event_sp); |
| 1158 | } |
| 1159 | return state; |
| 1160 | } |
| 1161 | |
| 1162 | Error |
| 1163 | Process::Launch |
| 1164 | ( |
| 1165 | char const *argv[], |
| 1166 | char const *envp[], |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 1167 | uint32_t launch_flags, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1168 | const char *stdin_path, |
| 1169 | const char *stdout_path, |
| 1170 | const char *stderr_path |
| 1171 | ) |
| 1172 | { |
| 1173 | Error error; |
| 1174 | m_target_triple.Clear(); |
| 1175 | m_abi_sp.reset(); |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 1176 | m_process_input_reader.reset(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1177 | |
| 1178 | Module *exe_module = m_target.GetExecutableModule().get(); |
| 1179 | if (exe_module) |
| 1180 | { |
| 1181 | char exec_file_path[PATH_MAX]; |
| 1182 | exe_module->GetFileSpec().GetPath(exec_file_path, sizeof(exec_file_path)); |
| 1183 | if (exe_module->GetFileSpec().Exists()) |
| 1184 | { |
| 1185 | error = WillLaunch (exe_module); |
| 1186 | if (error.Success()) |
| 1187 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1188 | SetPublicState (eStateLaunching); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1189 | // The args coming in should not contain the application name, the |
| 1190 | // lldb_private::Process class will add this in case the executable |
| 1191 | // gets resolved to a different file than was given on the command |
| 1192 | // line (like when an applicaiton bundle is specified and will |
| 1193 | // resolve to the contained exectuable file, or the file given was |
| 1194 | // a symlink or other file system link that resolves to a different |
| 1195 | // file). |
| 1196 | |
| 1197 | // Get the resolved exectuable path |
| 1198 | |
| 1199 | // Make a new argument vector |
| 1200 | std::vector<const char *> exec_path_plus_argv; |
| 1201 | // Append the resolved executable path |
| 1202 | exec_path_plus_argv.push_back (exec_file_path); |
| 1203 | |
| 1204 | // Push all args if there are any |
| 1205 | if (argv) |
| 1206 | { |
| 1207 | for (int i = 0; argv[i]; ++i) |
| 1208 | exec_path_plus_argv.push_back(argv[i]); |
| 1209 | } |
| 1210 | |
| 1211 | // Push a NULL to terminate the args. |
| 1212 | exec_path_plus_argv.push_back(NULL); |
| 1213 | |
| 1214 | // Now launch using these arguments. |
Greg Clayton | 53d68e7 | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 1215 | error = DoLaunch (exe_module, |
| 1216 | exec_path_plus_argv.empty() ? NULL : &exec_path_plus_argv.front(), |
| 1217 | envp, |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 1218 | launch_flags, |
Greg Clayton | 53d68e7 | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 1219 | stdin_path, |
| 1220 | stdout_path, |
| 1221 | stderr_path); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1222 | |
| 1223 | if (error.Fail()) |
| 1224 | { |
| 1225 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 1226 | { |
| 1227 | SetID (LLDB_INVALID_PROCESS_ID); |
| 1228 | const char *error_string = error.AsCString(); |
| 1229 | if (error_string == NULL) |
| 1230 | error_string = "launch failed"; |
| 1231 | SetExitStatus (-1, error_string); |
| 1232 | } |
| 1233 | } |
| 1234 | else |
| 1235 | { |
| 1236 | EventSP event_sp; |
| 1237 | StateType state = WaitForProcessStopPrivate(NULL, event_sp); |
| 1238 | |
| 1239 | if (state == eStateStopped || state == eStateCrashed) |
| 1240 | { |
| 1241 | DidLaunch (); |
| 1242 | |
| 1243 | // This delays passing the stopped event to listeners till DidLaunch gets |
| 1244 | // a chance to complete... |
| 1245 | HandlePrivateEvent (event_sp); |
| 1246 | StartPrivateStateThread (); |
| 1247 | } |
| 1248 | else if (state == eStateExited) |
| 1249 | { |
| 1250 | // We exited while trying to launch somehow. Don't call DidLaunch as that's |
| 1251 | // not likely to work, and return an invalid pid. |
| 1252 | HandlePrivateEvent (event_sp); |
| 1253 | } |
| 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | else |
| 1258 | { |
| 1259 | error.SetErrorStringWithFormat("File doesn't exist: '%s'.\n", exec_file_path); |
| 1260 | } |
| 1261 | } |
| 1262 | return error; |
| 1263 | } |
| 1264 | |
| 1265 | Error |
| 1266 | Process::CompleteAttach () |
| 1267 | { |
| 1268 | Error error; |
Greg Clayton | c1d3775 | 2010-10-18 01:45:30 +0000 | [diff] [blame] | 1269 | |
| 1270 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 1271 | { |
| 1272 | error.SetErrorString("no process"); |
| 1273 | } |
| 1274 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1275 | EventSP event_sp; |
| 1276 | StateType state = WaitForProcessStopPrivate(NULL, event_sp); |
| 1277 | if (state == eStateStopped || state == eStateCrashed) |
| 1278 | { |
| 1279 | DidAttach (); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1280 | // Figure out which one is the executable, and set that in our target: |
| 1281 | ModuleList &modules = GetTarget().GetImages(); |
| 1282 | |
| 1283 | size_t num_modules = modules.GetSize(); |
| 1284 | for (int i = 0; i < num_modules; i++) |
| 1285 | { |
| 1286 | ModuleSP module_sp = modules.GetModuleAtIndex(i); |
| 1287 | if (module_sp->IsExecutable()) |
| 1288 | { |
| 1289 | ModuleSP exec_module = GetTarget().GetExecutableModule(); |
| 1290 | if (!exec_module || exec_module != module_sp) |
| 1291 | { |
| 1292 | |
| 1293 | GetTarget().SetExecutableModule (module_sp, false); |
| 1294 | } |
| 1295 | break; |
| 1296 | } |
| 1297 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1298 | |
| 1299 | // This delays passing the stopped event to listeners till DidLaunch gets |
| 1300 | // a chance to complete... |
| 1301 | HandlePrivateEvent(event_sp); |
| 1302 | StartPrivateStateThread(); |
| 1303 | } |
| 1304 | else |
| 1305 | { |
| 1306 | // We exited while trying to launch somehow. Don't call DidLaunch as that's |
| 1307 | // not likely to work, and return an invalid pid. |
| 1308 | if (state == eStateExited) |
| 1309 | HandlePrivateEvent (event_sp); |
| 1310 | error.SetErrorStringWithFormat("invalid state after attach: %s", |
| 1311 | lldb_private::StateAsCString(state)); |
| 1312 | } |
| 1313 | return error; |
| 1314 | } |
| 1315 | |
| 1316 | Error |
| 1317 | Process::Attach (lldb::pid_t attach_pid) |
| 1318 | { |
| 1319 | |
| 1320 | m_target_triple.Clear(); |
| 1321 | m_abi_sp.reset(); |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 1322 | m_process_input_reader.reset(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1323 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1324 | // Find the process and its architecture. Make sure it matches the architecture |
| 1325 | // of the current Target, and if not adjust it. |
| 1326 | |
| 1327 | ArchSpec attach_spec = GetArchSpecForExistingProcess (attach_pid); |
| 1328 | if (attach_spec != GetTarget().GetArchitecture()) |
| 1329 | { |
| 1330 | // Set the architecture on the target. |
| 1331 | GetTarget().SetArchitecture(attach_spec); |
| 1332 | } |
| 1333 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1334 | Error error (WillAttachToProcessWithID(attach_pid)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1335 | if (error.Success()) |
| 1336 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1337 | SetPublicState (eStateAttaching); |
| 1338 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1339 | error = DoAttachToProcessWithID (attach_pid); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1340 | if (error.Success()) |
| 1341 | { |
| 1342 | error = CompleteAttach(); |
| 1343 | } |
| 1344 | else |
| 1345 | { |
| 1346 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 1347 | { |
| 1348 | SetID (LLDB_INVALID_PROCESS_ID); |
| 1349 | const char *error_string = error.AsCString(); |
| 1350 | if (error_string == NULL) |
| 1351 | error_string = "attach failed"; |
| 1352 | |
| 1353 | SetExitStatus(-1, error_string); |
| 1354 | } |
| 1355 | } |
| 1356 | } |
| 1357 | return error; |
| 1358 | } |
| 1359 | |
| 1360 | Error |
| 1361 | Process::Attach (const char *process_name, bool wait_for_launch) |
| 1362 | { |
| 1363 | m_target_triple.Clear(); |
| 1364 | m_abi_sp.reset(); |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 1365 | m_process_input_reader.reset(); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1366 | |
| 1367 | // Find the process and its architecture. Make sure it matches the architecture |
| 1368 | // of the current Target, and if not adjust it. |
| 1369 | |
Jim Ingham | ea29418 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 1370 | if (!wait_for_launch) |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1371 | { |
Jim Ingham | ea29418 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 1372 | ArchSpec attach_spec = GetArchSpecForExistingProcess (process_name); |
Greg Clayton | c1d3775 | 2010-10-18 01:45:30 +0000 | [diff] [blame] | 1373 | if (attach_spec.IsValid() && attach_spec != GetTarget().GetArchitecture()) |
Jim Ingham | ea29418 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 1374 | { |
| 1375 | // Set the architecture on the target. |
| 1376 | GetTarget().SetArchitecture(attach_spec); |
| 1377 | } |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1378 | } |
Jim Ingham | ea29418 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 1379 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1380 | Error error (WillAttachToProcessWithName(process_name, wait_for_launch)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1381 | if (error.Success()) |
| 1382 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1383 | SetPublicState (eStateAttaching); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1384 | error = DoAttachToProcessWithName (process_name, wait_for_launch); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1385 | if (error.Fail()) |
| 1386 | { |
| 1387 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 1388 | { |
| 1389 | SetID (LLDB_INVALID_PROCESS_ID); |
| 1390 | const char *error_string = error.AsCString(); |
| 1391 | if (error_string == NULL) |
| 1392 | error_string = "attach failed"; |
| 1393 | |
| 1394 | SetExitStatus(-1, error_string); |
| 1395 | } |
| 1396 | } |
| 1397 | else |
| 1398 | { |
| 1399 | error = CompleteAttach(); |
| 1400 | } |
| 1401 | } |
| 1402 | return error; |
| 1403 | } |
| 1404 | |
| 1405 | Error |
| 1406 | Process::Resume () |
| 1407 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1408 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1409 | if (log) |
| 1410 | log->Printf("Process::Resume() m_stop_id = %u", m_stop_id); |
| 1411 | |
| 1412 | Error error (WillResume()); |
| 1413 | // Tell the process it is about to resume before the thread list |
| 1414 | if (error.Success()) |
| 1415 | { |
| 1416 | // Now let the thread list know we are about to resume to it |
| 1417 | // can let all of our threads know that they are about to be |
| 1418 | // resumed. Threads will each be called with |
| 1419 | // Thread::WillResume(StateType) where StateType contains the state |
| 1420 | // that they are supposed to have when the process is resumed |
| 1421 | // (suspended/running/stepping). Threads should also check |
| 1422 | // their resume signal in lldb::Thread::GetResumeSignal() |
| 1423 | // to see if they are suppoed to start back up with a signal. |
| 1424 | if (m_thread_list.WillResume()) |
| 1425 | { |
| 1426 | error = DoResume(); |
| 1427 | if (error.Success()) |
| 1428 | { |
| 1429 | DidResume(); |
| 1430 | m_thread_list.DidResume(); |
| 1431 | } |
| 1432 | } |
| 1433 | else |
| 1434 | { |
| 1435 | error.SetErrorStringWithFormat("thread list returned flase after WillResume"); |
| 1436 | } |
| 1437 | } |
| 1438 | return error; |
| 1439 | } |
| 1440 | |
| 1441 | Error |
| 1442 | Process::Halt () |
| 1443 | { |
| 1444 | Error error (WillHalt()); |
| 1445 | |
| 1446 | if (error.Success()) |
| 1447 | { |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1448 | |
| 1449 | bool caused_stop = false; |
| 1450 | EventSP event_sp; |
| 1451 | |
| 1452 | // Pause our private state thread so we can ensure no one else eats |
| 1453 | // the stop event out from under us. |
| 1454 | PausePrivateStateThread(); |
| 1455 | |
| 1456 | // Ask the process subclass to actually halt our process |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1457 | error = DoHalt(caused_stop); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1458 | if (error.Success()) |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1459 | { |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1460 | // If "caused_stop" is true, then DoHalt stopped the process. If |
| 1461 | // "caused_stop" is false, the process was already stopped. |
| 1462 | // If the DoHalt caused the process to stop, then we want to catch |
| 1463 | // this event and set the interrupted bool to true before we pass |
| 1464 | // this along so clients know that the process was interrupted by |
| 1465 | // a halt command. |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1466 | if (caused_stop) |
| 1467 | { |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1468 | // Wait for 2 seconds for the process to stop. |
| 1469 | TimeValue timeout_time; |
| 1470 | timeout_time = TimeValue::Now(); |
| 1471 | timeout_time.OffsetWithSeconds(2); |
| 1472 | StateType state = WaitForStateChangedEventsPrivate (&timeout_time, event_sp); |
| 1473 | |
| 1474 | if (state == eStateInvalid) |
| 1475 | { |
| 1476 | // We timeout out and didn't get a stop event... |
| 1477 | error.SetErrorString ("Halt timed out."); |
| 1478 | } |
| 1479 | else |
| 1480 | { |
| 1481 | // Since we are eating the event, we need to update our state |
| 1482 | // otherwise the process state will not match reality... |
| 1483 | SetPublicState(state); |
| 1484 | |
| 1485 | if (StateIsStoppedState (state)) |
| 1486 | { |
| 1487 | // We caused the process to interrupt itself, so mark this |
| 1488 | // as such in the stop event so clients can tell an interrupted |
| 1489 | // process from a natural stop |
| 1490 | ProcessEventData::SetInterruptedInEvent (event_sp.get(), true); |
| 1491 | } |
| 1492 | else |
| 1493 | { |
| 1494 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1495 | if (log) |
| 1496 | log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state)); |
| 1497 | error.SetErrorString ("Did not get stopped event after halt."); |
| 1498 | } |
| 1499 | } |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1500 | } |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1501 | DidHalt(); |
| 1502 | |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1503 | } |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1504 | // Resume our private state thread before we post the event (if any) |
| 1505 | ResumePrivateStateThread(); |
| 1506 | |
| 1507 | // Post any event we might have consumed. If all goes well, we will have |
| 1508 | // stopped the process, intercepted the event and set the interrupted |
| 1509 | // bool in the event. |
| 1510 | if (event_sp) |
| 1511 | BroadcastEvent(event_sp); |
| 1512 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1513 | } |
| 1514 | return error; |
| 1515 | } |
| 1516 | |
| 1517 | Error |
| 1518 | Process::Detach () |
| 1519 | { |
| 1520 | Error error (WillDetach()); |
| 1521 | |
| 1522 | if (error.Success()) |
| 1523 | { |
| 1524 | DisableAllBreakpointSites(); |
| 1525 | error = DoDetach(); |
| 1526 | if (error.Success()) |
| 1527 | { |
| 1528 | DidDetach(); |
| 1529 | StopPrivateStateThread(); |
| 1530 | } |
| 1531 | } |
| 1532 | return error; |
| 1533 | } |
| 1534 | |
| 1535 | Error |
| 1536 | Process::Destroy () |
| 1537 | { |
| 1538 | Error error (WillDestroy()); |
| 1539 | if (error.Success()) |
| 1540 | { |
| 1541 | DisableAllBreakpointSites(); |
| 1542 | error = DoDestroy(); |
| 1543 | if (error.Success()) |
| 1544 | { |
| 1545 | DidDestroy(); |
| 1546 | StopPrivateStateThread(); |
| 1547 | } |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 1548 | m_stdio_communication.StopReadThread(); |
| 1549 | m_stdio_communication.Disconnect(); |
| 1550 | if (m_process_input_reader && m_process_input_reader->IsActive()) |
| 1551 | m_target.GetDebugger().PopInputReader (m_process_input_reader); |
| 1552 | if (m_process_input_reader) |
| 1553 | m_process_input_reader.reset(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1554 | } |
| 1555 | return error; |
| 1556 | } |
| 1557 | |
| 1558 | Error |
| 1559 | Process::Signal (int signal) |
| 1560 | { |
| 1561 | Error error (WillSignal()); |
| 1562 | if (error.Success()) |
| 1563 | { |
| 1564 | error = DoSignal(signal); |
| 1565 | if (error.Success()) |
| 1566 | DidSignal(); |
| 1567 | } |
| 1568 | return error; |
| 1569 | } |
| 1570 | |
| 1571 | UnixSignals & |
| 1572 | Process::GetUnixSignals () |
| 1573 | { |
| 1574 | return m_unix_signals; |
| 1575 | } |
| 1576 | |
| 1577 | Target & |
| 1578 | Process::GetTarget () |
| 1579 | { |
| 1580 | return m_target; |
| 1581 | } |
| 1582 | |
| 1583 | const Target & |
| 1584 | Process::GetTarget () const |
| 1585 | { |
| 1586 | return m_target; |
| 1587 | } |
| 1588 | |
| 1589 | uint32_t |
| 1590 | Process::GetAddressByteSize() |
| 1591 | { |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1592 | if (m_addr_byte_size == 0) |
| 1593 | return m_target.GetArchitecture().GetAddressByteSize(); |
| 1594 | return m_addr_byte_size; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | bool |
| 1598 | Process::ShouldBroadcastEvent (Event *event_ptr) |
| 1599 | { |
| 1600 | const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr); |
| 1601 | bool return_value = true; |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1602 | LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1603 | |
| 1604 | switch (state) |
| 1605 | { |
| 1606 | case eStateAttaching: |
| 1607 | case eStateLaunching: |
| 1608 | case eStateDetached: |
| 1609 | case eStateExited: |
| 1610 | case eStateUnloaded: |
| 1611 | // These events indicate changes in the state of the debugging session, always report them. |
| 1612 | return_value = true; |
| 1613 | break; |
| 1614 | case eStateInvalid: |
| 1615 | // We stopped for no apparent reason, don't report it. |
| 1616 | return_value = false; |
| 1617 | break; |
| 1618 | case eStateRunning: |
| 1619 | case eStateStepping: |
| 1620 | // If we've started the target running, we handle the cases where we |
| 1621 | // are already running and where there is a transition from stopped to |
| 1622 | // running differently. |
| 1623 | // running -> running: Automatically suppress extra running events |
| 1624 | // stopped -> running: Report except when there is one or more no votes |
| 1625 | // and no yes votes. |
| 1626 | SynchronouslyNotifyStateChanged (state); |
| 1627 | switch (m_public_state.GetValue()) |
| 1628 | { |
| 1629 | case eStateRunning: |
| 1630 | case eStateStepping: |
| 1631 | // We always suppress multiple runnings with no PUBLIC stop in between. |
| 1632 | return_value = false; |
| 1633 | break; |
| 1634 | default: |
| 1635 | // TODO: make this work correctly. For now always report |
| 1636 | // run if we aren't running so we don't miss any runnning |
| 1637 | // events. If I run the lldb/test/thread/a.out file and |
| 1638 | // break at main.cpp:58, run and hit the breakpoints on |
| 1639 | // multiple threads, then somehow during the stepping over |
| 1640 | // of all breakpoints no run gets reported. |
| 1641 | return_value = true; |
| 1642 | |
| 1643 | // This is a transition from stop to run. |
| 1644 | switch (m_thread_list.ShouldReportRun (event_ptr)) |
| 1645 | { |
| 1646 | case eVoteYes: |
| 1647 | case eVoteNoOpinion: |
| 1648 | return_value = true; |
| 1649 | break; |
| 1650 | case eVoteNo: |
| 1651 | return_value = false; |
| 1652 | break; |
| 1653 | } |
| 1654 | break; |
| 1655 | } |
| 1656 | break; |
| 1657 | case eStateStopped: |
| 1658 | case eStateCrashed: |
| 1659 | case eStateSuspended: |
| 1660 | { |
| 1661 | // We've stopped. First see if we're going to restart the target. |
| 1662 | // If we are going to stop, then we always broadcast the event. |
| 1663 | // If we aren't going to stop, let the thread plans decide if we're going to report this event. |
Jim Ingham | 5a47e8b | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 1664 | // If no thread has an opinion, we don't report it. |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1665 | if (ProcessEventData::GetInterruptedFromEvent (event_ptr)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1666 | { |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1667 | if (log) |
| 1668 | log->Printf ("Process::ShouldBroadcastEvent (%p) stopped due to an interrupt, state: %s", event_ptr, StateAsCString(state)); |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1669 | return true; |
| 1670 | } |
| 1671 | else |
| 1672 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1673 | RefreshStateAfterStop (); |
| 1674 | |
| 1675 | if (m_thread_list.ShouldStop (event_ptr) == false) |
| 1676 | { |
| 1677 | switch (m_thread_list.ShouldReportStop (event_ptr)) |
| 1678 | { |
| 1679 | case eVoteYes: |
| 1680 | Process::ProcessEventData::SetRestartedInEvent (event_ptr, true); |
Johnny Chen | 028784b | 2010-10-14 00:54:32 +0000 | [diff] [blame] | 1681 | // Intentional fall-through here. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1682 | case eVoteNoOpinion: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1683 | case eVoteNo: |
| 1684 | return_value = false; |
| 1685 | break; |
| 1686 | } |
| 1687 | |
| 1688 | if (log) |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1689 | log->Printf ("Process::ShouldBroadcastEvent (%p) Restarting process from state: %s", event_ptr, StateAsCString(state)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1690 | Resume (); |
| 1691 | } |
| 1692 | else |
| 1693 | { |
| 1694 | return_value = true; |
| 1695 | SynchronouslyNotifyStateChanged (state); |
| 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | if (log) |
| 1702 | log->Printf ("Process::ShouldBroadcastEvent (%p) => %s", event_ptr, StateAsCString(state), return_value ? "YES" : "NO"); |
| 1703 | return return_value; |
| 1704 | } |
| 1705 | |
| 1706 | //------------------------------------------------------------------ |
| 1707 | // Thread Queries |
| 1708 | //------------------------------------------------------------------ |
| 1709 | |
| 1710 | ThreadList & |
| 1711 | Process::GetThreadList () |
| 1712 | { |
| 1713 | return m_thread_list; |
| 1714 | } |
| 1715 | |
| 1716 | const ThreadList & |
| 1717 | Process::GetThreadList () const |
| 1718 | { |
| 1719 | return m_thread_list; |
| 1720 | } |
| 1721 | |
| 1722 | |
| 1723 | bool |
| 1724 | Process::StartPrivateStateThread () |
| 1725 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1726 | LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1727 | |
| 1728 | if (log) |
| 1729 | log->Printf ("Process::%s ( )", __FUNCTION__); |
| 1730 | |
| 1731 | // Create a thread that watches our internal state and controls which |
| 1732 | // events make it to clients (into the DCProcess event queue). |
| 1733 | m_private_state_thread = Host::ThreadCreate ("<lldb.process.internal-state>", Process::PrivateStateThread, this, NULL); |
| 1734 | return m_private_state_thread != LLDB_INVALID_HOST_THREAD; |
| 1735 | } |
| 1736 | |
| 1737 | void |
| 1738 | Process::PausePrivateStateThread () |
| 1739 | { |
| 1740 | ControlPrivateStateThread (eBroadcastInternalStateControlPause); |
| 1741 | } |
| 1742 | |
| 1743 | void |
| 1744 | Process::ResumePrivateStateThread () |
| 1745 | { |
| 1746 | ControlPrivateStateThread (eBroadcastInternalStateControlResume); |
| 1747 | } |
| 1748 | |
| 1749 | void |
| 1750 | Process::StopPrivateStateThread () |
| 1751 | { |
| 1752 | ControlPrivateStateThread (eBroadcastInternalStateControlStop); |
| 1753 | } |
| 1754 | |
| 1755 | void |
| 1756 | Process::ControlPrivateStateThread (uint32_t signal) |
| 1757 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1758 | LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1759 | |
| 1760 | assert (signal == eBroadcastInternalStateControlStop || |
| 1761 | signal == eBroadcastInternalStateControlPause || |
| 1762 | signal == eBroadcastInternalStateControlResume); |
| 1763 | |
| 1764 | if (log) |
| 1765 | log->Printf ("Process::%s ( ) - signal: %d", __FUNCTION__, signal); |
| 1766 | |
| 1767 | // Signal the private state thread |
| 1768 | if (m_private_state_thread != LLDB_INVALID_HOST_THREAD) |
| 1769 | { |
| 1770 | TimeValue timeout_time; |
| 1771 | bool timed_out; |
| 1772 | |
| 1773 | m_private_state_control_broadcaster.BroadcastEvent (signal, NULL); |
| 1774 | |
| 1775 | timeout_time = TimeValue::Now(); |
| 1776 | timeout_time.OffsetWithSeconds(2); |
| 1777 | m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out); |
| 1778 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
| 1779 | |
| 1780 | if (signal == eBroadcastInternalStateControlStop) |
| 1781 | { |
| 1782 | if (timed_out) |
| 1783 | Host::ThreadCancel (m_private_state_thread, NULL); |
| 1784 | |
| 1785 | thread_result_t result = NULL; |
| 1786 | Host::ThreadJoin (m_private_state_thread, &result, NULL); |
Greg Clayton | c607d86 | 2010-07-22 18:34:21 +0000 | [diff] [blame] | 1787 | m_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1788 | } |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | void |
| 1793 | Process::HandlePrivateEvent (EventSP &event_sp) |
| 1794 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1795 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1796 | const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1797 | // See if we should broadcast this state to external clients? |
| 1798 | const bool should_broadcast = ShouldBroadcastEvent (event_sp.get()); |
| 1799 | if (log) |
| 1800 | log->Printf ("Process::%s (arg = %p, pid = %i) got event '%s' broadcast = %s", __FUNCTION__, this, GetID(), StateAsCString(internal_state), should_broadcast ? "yes" : "no"); |
| 1801 | |
| 1802 | if (should_broadcast) |
| 1803 | { |
| 1804 | if (log) |
| 1805 | { |
| 1806 | log->Printf ("\tChanging public state from: %s to %s", StateAsCString(GetState ()), StateAsCString (internal_state)); |
| 1807 | } |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 1808 | if (StateIsRunningState (internal_state)) |
| 1809 | PushProcessInputReader (); |
| 1810 | else |
| 1811 | PopProcessInputReader (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1812 | Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get()); |
| 1813 | BroadcastEvent (event_sp); |
| 1814 | } |
| 1815 | else |
| 1816 | { |
| 1817 | if (log) |
| 1818 | { |
| 1819 | log->Printf ("\tNot changing public state with event: %s", StateAsCString (internal_state)); |
| 1820 | } |
| 1821 | } |
| 1822 | } |
| 1823 | |
| 1824 | void * |
| 1825 | Process::PrivateStateThread (void *arg) |
| 1826 | { |
| 1827 | Process *proc = static_cast<Process*> (arg); |
| 1828 | void *result = proc->RunPrivateStateThread (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1829 | return result; |
| 1830 | } |
| 1831 | |
| 1832 | void * |
| 1833 | Process::RunPrivateStateThread () |
| 1834 | { |
| 1835 | bool control_only = false; |
| 1836 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
| 1837 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1838 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1839 | if (log) |
| 1840 | log->Printf ("Process::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, this, GetID()); |
| 1841 | |
| 1842 | bool exit_now = false; |
| 1843 | while (!exit_now) |
| 1844 | { |
| 1845 | EventSP event_sp; |
| 1846 | WaitForEventsPrivate (NULL, event_sp, control_only); |
| 1847 | if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster)) |
| 1848 | { |
| 1849 | switch (event_sp->GetType()) |
| 1850 | { |
| 1851 | case eBroadcastInternalStateControlStop: |
| 1852 | exit_now = true; |
| 1853 | continue; // Go to next loop iteration so we exit without |
| 1854 | break; // doing any internal state managment below |
| 1855 | |
| 1856 | case eBroadcastInternalStateControlPause: |
| 1857 | control_only = true; |
| 1858 | break; |
| 1859 | |
| 1860 | case eBroadcastInternalStateControlResume: |
| 1861 | control_only = false; |
| 1862 | break; |
| 1863 | } |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1864 | |
| 1865 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
| 1866 | if (log) |
| 1867 | log->Printf ("Process::%s (arg = %p, pid = %i) got a control event: %d", __FUNCTION__, this, GetID(), event_sp->GetType()); |
| 1868 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1869 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1870 | continue; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | |
| 1874 | const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1875 | |
| 1876 | if (internal_state != eStateInvalid) |
| 1877 | { |
| 1878 | HandlePrivateEvent (event_sp); |
| 1879 | } |
| 1880 | |
Greg Clayton | 3b2c41c | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 1881 | if (internal_state == eStateInvalid || |
| 1882 | internal_state == eStateExited || |
| 1883 | internal_state == eStateDetached ) |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1884 | { |
| 1885 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
| 1886 | if (log) |
| 1887 | log->Printf ("Process::%s (arg = %p, pid = %i) about to exit with internal state %s...", __FUNCTION__, this, GetID(), StateAsCString(internal_state)); |
| 1888 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1889 | break; |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1890 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 1893 | // Verify log is still enabled before attempting to write to it... |
| 1894 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1895 | if (log) |
| 1896 | log->Printf ("Process::%s (arg = %p, pid = %i) thread exiting...", __FUNCTION__, this, GetID()); |
| 1897 | |
Greg Clayton | 8b4c16e | 2010-08-19 21:50:06 +0000 | [diff] [blame] | 1898 | m_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1899 | return NULL; |
| 1900 | } |
| 1901 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1902 | //------------------------------------------------------------------ |
| 1903 | // Process Event Data |
| 1904 | //------------------------------------------------------------------ |
| 1905 | |
| 1906 | Process::ProcessEventData::ProcessEventData () : |
| 1907 | EventData (), |
| 1908 | m_process_sp (), |
| 1909 | m_state (eStateInvalid), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1910 | m_restarted (false), |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1911 | m_update_state (false), |
| 1912 | m_interrupted (false) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1913 | { |
| 1914 | } |
| 1915 | |
| 1916 | Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) : |
| 1917 | EventData (), |
| 1918 | m_process_sp (process_sp), |
| 1919 | m_state (state), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1920 | m_restarted (false), |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1921 | m_update_state (false), |
| 1922 | m_interrupted (false) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1923 | { |
| 1924 | } |
| 1925 | |
| 1926 | Process::ProcessEventData::~ProcessEventData() |
| 1927 | { |
| 1928 | } |
| 1929 | |
| 1930 | const ConstString & |
| 1931 | Process::ProcessEventData::GetFlavorString () |
| 1932 | { |
| 1933 | static ConstString g_flavor ("Process::ProcessEventData"); |
| 1934 | return g_flavor; |
| 1935 | } |
| 1936 | |
| 1937 | const ConstString & |
| 1938 | Process::ProcessEventData::GetFlavor () const |
| 1939 | { |
| 1940 | return ProcessEventData::GetFlavorString (); |
| 1941 | } |
| 1942 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1943 | void |
| 1944 | Process::ProcessEventData::DoOnRemoval (Event *event_ptr) |
| 1945 | { |
| 1946 | // This function gets called twice for each event, once when the event gets pulled |
| 1947 | // off of the private process event queue, and once when it gets pulled off of |
| 1948 | // the public event queue. m_update_state is used to distinguish these |
| 1949 | // two cases; it is false when we're just pulling it off for private handling, |
| 1950 | // and we don't want to do the breakpoint command handling then. |
| 1951 | |
| 1952 | if (!m_update_state) |
| 1953 | return; |
| 1954 | |
| 1955 | m_process_sp->SetPublicState (m_state); |
| 1956 | |
| 1957 | // If we're stopped and haven't restarted, then do the breakpoint commands here: |
| 1958 | if (m_state == eStateStopped && ! m_restarted) |
| 1959 | { |
| 1960 | int num_threads = m_process_sp->GetThreadList().GetSize(); |
| 1961 | int idx; |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1962 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1963 | for (idx = 0; idx < num_threads; ++idx) |
| 1964 | { |
| 1965 | lldb::ThreadSP thread_sp = m_process_sp->GetThreadList().GetThreadAtIndex(idx); |
| 1966 | |
Jim Ingham | 6297a3a | 2010-10-20 00:39:53 +0000 | [diff] [blame] | 1967 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo (); |
| 1968 | if (stop_info_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1969 | { |
Jim Ingham | 6297a3a | 2010-10-20 00:39:53 +0000 | [diff] [blame] | 1970 | stop_info_sp->PerformAction(event_ptr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1971 | } |
| 1972 | } |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 1973 | |
Jim Ingham | 6fb8baa | 2010-08-10 00:59:59 +0000 | [diff] [blame] | 1974 | // The stop action might restart the target. If it does, then we want to mark that in the |
| 1975 | // event so that whoever is receiving it will know to wait for the running event and reflect |
| 1976 | // that state appropriately. |
| 1977 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1978 | if (m_process_sp->GetPrivateState() == eStateRunning) |
| 1979 | SetRestarted(true); |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | void |
| 1984 | Process::ProcessEventData::Dump (Stream *s) const |
| 1985 | { |
| 1986 | if (m_process_sp) |
| 1987 | s->Printf(" process = %p (pid = %u), ", m_process_sp.get(), m_process_sp->GetID()); |
| 1988 | |
| 1989 | s->Printf("state = %s", StateAsCString(GetState()));; |
| 1990 | } |
| 1991 | |
| 1992 | const Process::ProcessEventData * |
| 1993 | Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr) |
| 1994 | { |
| 1995 | if (event_ptr) |
| 1996 | { |
| 1997 | const EventData *event_data = event_ptr->GetData(); |
| 1998 | if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString()) |
| 1999 | return static_cast <const ProcessEventData *> (event_ptr->GetData()); |
| 2000 | } |
| 2001 | return NULL; |
| 2002 | } |
| 2003 | |
| 2004 | ProcessSP |
| 2005 | Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr) |
| 2006 | { |
| 2007 | ProcessSP process_sp; |
| 2008 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 2009 | if (data) |
| 2010 | process_sp = data->GetProcessSP(); |
| 2011 | return process_sp; |
| 2012 | } |
| 2013 | |
| 2014 | StateType |
| 2015 | Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr) |
| 2016 | { |
| 2017 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 2018 | if (data == NULL) |
| 2019 | return eStateInvalid; |
| 2020 | else |
| 2021 | return data->GetState(); |
| 2022 | } |
| 2023 | |
| 2024 | bool |
| 2025 | Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr) |
| 2026 | { |
| 2027 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 2028 | if (data == NULL) |
| 2029 | return false; |
| 2030 | else |
| 2031 | return data->GetRestarted(); |
| 2032 | } |
| 2033 | |
| 2034 | void |
| 2035 | Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value) |
| 2036 | { |
| 2037 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 2038 | if (data != NULL) |
| 2039 | data->SetRestarted(new_value); |
| 2040 | } |
| 2041 | |
| 2042 | bool |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2043 | Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr) |
| 2044 | { |
| 2045 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 2046 | if (data == NULL) |
| 2047 | return false; |
| 2048 | else |
| 2049 | return data->GetInterrupted (); |
| 2050 | } |
| 2051 | |
| 2052 | void |
| 2053 | Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value) |
| 2054 | { |
| 2055 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 2056 | if (data != NULL) |
| 2057 | data->SetInterrupted(new_value); |
| 2058 | } |
| 2059 | |
| 2060 | bool |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2061 | Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr) |
| 2062 | { |
| 2063 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 2064 | if (data) |
| 2065 | { |
| 2066 | data->SetUpdateStateOnRemoval(); |
| 2067 | return true; |
| 2068 | } |
| 2069 | return false; |
| 2070 | } |
| 2071 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2072 | Target * |
| 2073 | Process::CalculateTarget () |
| 2074 | { |
| 2075 | return &m_target; |
| 2076 | } |
| 2077 | |
| 2078 | Process * |
| 2079 | Process::CalculateProcess () |
| 2080 | { |
| 2081 | return this; |
| 2082 | } |
| 2083 | |
| 2084 | Thread * |
| 2085 | Process::CalculateThread () |
| 2086 | { |
| 2087 | return NULL; |
| 2088 | } |
| 2089 | |
| 2090 | StackFrame * |
| 2091 | Process::CalculateStackFrame () |
| 2092 | { |
| 2093 | return NULL; |
| 2094 | } |
| 2095 | |
| 2096 | void |
Greg Clayton | a830adb | 2010-10-04 01:05:56 +0000 | [diff] [blame] | 2097 | Process::CalculateExecutionContext (ExecutionContext &exe_ctx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2098 | { |
| 2099 | exe_ctx.target = &m_target; |
| 2100 | exe_ctx.process = this; |
| 2101 | exe_ctx.thread = NULL; |
| 2102 | exe_ctx.frame = NULL; |
| 2103 | } |
| 2104 | |
| 2105 | lldb::ProcessSP |
| 2106 | Process::GetSP () |
| 2107 | { |
| 2108 | return GetTarget().GetProcessSP(); |
| 2109 | } |
| 2110 | |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame] | 2111 | ClangPersistentVariables & |
| 2112 | Process::GetPersistentVariables() |
| 2113 | { |
| 2114 | return m_persistent_vars; |
| 2115 | } |
| 2116 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 2117 | uint32_t |
| 2118 | Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) |
| 2119 | { |
| 2120 | return 0; |
| 2121 | } |
| 2122 | |
| 2123 | ArchSpec |
| 2124 | Process::GetArchSpecForExistingProcess (lldb::pid_t pid) |
| 2125 | { |
| 2126 | return Host::GetArchSpecForExistingProcess (pid); |
| 2127 | } |
| 2128 | |
| 2129 | ArchSpec |
| 2130 | Process::GetArchSpecForExistingProcess (const char *process_name) |
| 2131 | { |
| 2132 | return Host::GetArchSpecForExistingProcess (process_name); |
| 2133 | } |
| 2134 | |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 2135 | void |
| 2136 | Process::AppendSTDOUT (const char * s, size_t len) |
| 2137 | { |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 2138 | Mutex::Locker locker (m_stdio_communication_mutex); |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 2139 | m_stdout_data.append (s, len); |
| 2140 | |
| 2141 | BroadcastEventIfUnique (eBroadcastBitSTDOUT); |
| 2142 | } |
| 2143 | |
| 2144 | void |
| 2145 | Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len) |
| 2146 | { |
| 2147 | Process *process = (Process *) baton; |
| 2148 | process->AppendSTDOUT (static_cast<const char *>(src), src_len); |
| 2149 | } |
| 2150 | |
| 2151 | size_t |
| 2152 | Process::ProcessInputReaderCallback (void *baton, |
| 2153 | InputReader &reader, |
| 2154 | lldb::InputReaderAction notification, |
| 2155 | const char *bytes, |
| 2156 | size_t bytes_len) |
| 2157 | { |
| 2158 | Process *process = (Process *) baton; |
| 2159 | |
| 2160 | switch (notification) |
| 2161 | { |
| 2162 | case eInputReaderActivate: |
| 2163 | break; |
| 2164 | |
| 2165 | case eInputReaderDeactivate: |
| 2166 | break; |
| 2167 | |
| 2168 | case eInputReaderReactivate: |
| 2169 | break; |
| 2170 | |
| 2171 | case eInputReaderGotToken: |
| 2172 | { |
| 2173 | Error error; |
| 2174 | process->PutSTDIN (bytes, bytes_len, error); |
| 2175 | } |
| 2176 | break; |
| 2177 | |
| 2178 | case eInputReaderDone: |
| 2179 | break; |
| 2180 | |
| 2181 | } |
| 2182 | |
| 2183 | return bytes_len; |
| 2184 | } |
| 2185 | |
| 2186 | void |
| 2187 | Process::ResetProcessInputReader () |
| 2188 | { |
| 2189 | m_process_input_reader.reset(); |
| 2190 | } |
| 2191 | |
| 2192 | void |
| 2193 | Process::SetUpProcessInputReader (int file_descriptor) |
| 2194 | { |
| 2195 | // First set up the Read Thread for reading/handling process I/O |
| 2196 | |
| 2197 | std::auto_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (file_descriptor, true)); |
| 2198 | |
| 2199 | if (conn_ap.get()) |
| 2200 | { |
| 2201 | m_stdio_communication.SetConnection (conn_ap.release()); |
| 2202 | if (m_stdio_communication.IsConnected()) |
| 2203 | { |
| 2204 | m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this); |
| 2205 | m_stdio_communication.StartReadThread(); |
| 2206 | |
| 2207 | // Now read thread is set up, set up input reader. |
| 2208 | |
| 2209 | if (!m_process_input_reader.get()) |
| 2210 | { |
| 2211 | m_process_input_reader.reset (new InputReader(m_target.GetDebugger())); |
| 2212 | Error err (m_process_input_reader->Initialize (Process::ProcessInputReaderCallback, |
| 2213 | this, |
| 2214 | eInputReaderGranularityByte, |
| 2215 | NULL, |
| 2216 | NULL, |
| 2217 | false)); |
| 2218 | |
| 2219 | if (err.Fail()) |
| 2220 | m_process_input_reader.reset(); |
| 2221 | } |
| 2222 | } |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | void |
| 2227 | Process::PushProcessInputReader () |
| 2228 | { |
| 2229 | if (m_process_input_reader && !m_process_input_reader->IsActive()) |
| 2230 | m_target.GetDebugger().PushInputReader (m_process_input_reader); |
| 2231 | } |
| 2232 | |
| 2233 | void |
| 2234 | Process::PopProcessInputReader () |
| 2235 | { |
| 2236 | if (m_process_input_reader && m_process_input_reader->IsActive()) |
| 2237 | m_target.GetDebugger().PopInputReader (m_process_input_reader); |
| 2238 | } |
| 2239 | |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame^] | 2240 | |
| 2241 | void |
| 2242 | Process::Initialize () |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2243 | { |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame^] | 2244 | UserSettingsControllerSP &usc = GetSettingsController(); |
| 2245 | usc.reset (new SettingsController); |
| 2246 | UserSettingsController::InitializeSettingsController (usc, |
| 2247 | SettingsController::global_settings_table, |
| 2248 | SettingsController::instance_settings_table); |
| 2249 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2250 | |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame^] | 2251 | void |
| 2252 | Process::Terminate () |
| 2253 | { |
| 2254 | UserSettingsControllerSP &usc = GetSettingsController(); |
| 2255 | UserSettingsController::FinalizeSettingsController (usc); |
| 2256 | usc.reset(); |
| 2257 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2258 | |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame^] | 2259 | UserSettingsControllerSP & |
| 2260 | Process::GetSettingsController () |
| 2261 | { |
| 2262 | static UserSettingsControllerSP g_settings_controller; |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2263 | return g_settings_controller; |
| 2264 | } |
| 2265 | |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 2266 | void |
| 2267 | Process::UpdateInstanceName () |
| 2268 | { |
| 2269 | ModuleSP module_sp = GetTarget().GetExecutableModule(); |
| 2270 | if (module_sp) |
| 2271 | { |
| 2272 | StreamString sstr; |
| 2273 | sstr.Printf ("%s", module_sp->GetFileSpec().GetFilename().AsCString()); |
| 2274 | |
| 2275 | Process::GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), |
| 2276 | sstr.GetData()); |
| 2277 | } |
| 2278 | } |
| 2279 | |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2280 | //-------------------------------------------------------------- |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 2281 | // class Process::SettingsController |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2282 | //-------------------------------------------------------------- |
| 2283 | |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 2284 | Process::SettingsController::SettingsController () : |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 2285 | UserSettingsController ("process", Target::GetSettingsController()) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2286 | { |
Caroline Tice | 004afcb | 2010-09-08 17:48:55 +0000 | [diff] [blame] | 2287 | m_default_settings.reset (new ProcessInstanceSettings (*this, false, |
| 2288 | InstanceSettings::GetDefaultName().AsCString())); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 2291 | Process::SettingsController::~SettingsController () |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2292 | { |
| 2293 | } |
| 2294 | |
| 2295 | lldb::InstanceSettingsSP |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 2296 | Process::SettingsController::CreateInstanceSettings (const char *instance_name) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2297 | { |
Caroline Tice | 004afcb | 2010-09-08 17:48:55 +0000 | [diff] [blame] | 2298 | ProcessInstanceSettings *new_settings = new ProcessInstanceSettings (*(Process::GetSettingsController().get()), |
| 2299 | false, instance_name); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2300 | lldb::InstanceSettingsSP new_settings_sp (new_settings); |
| 2301 | return new_settings_sp; |
| 2302 | } |
| 2303 | |
| 2304 | //-------------------------------------------------------------- |
| 2305 | // class ProcessInstanceSettings |
| 2306 | //-------------------------------------------------------------- |
| 2307 | |
Caroline Tice | 004afcb | 2010-09-08 17:48:55 +0000 | [diff] [blame] | 2308 | ProcessInstanceSettings::ProcessInstanceSettings (UserSettingsController &owner, bool live_instance, |
| 2309 | const char *name) : |
Caroline Tice | 75b11a3 | 2010-09-16 19:05:55 +0000 | [diff] [blame] | 2310 | InstanceSettings (owner, (name == NULL ? InstanceSettings::InvalidName().AsCString() : name), live_instance), |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2311 | m_run_args (), |
| 2312 | m_env_vars (), |
| 2313 | m_input_path (), |
| 2314 | m_output_path (), |
| 2315 | m_error_path (), |
| 2316 | m_plugin (), |
| 2317 | m_disable_aslr (true) |
| 2318 | { |
Caroline Tice | 396704b | 2010-09-09 18:26:37 +0000 | [diff] [blame] | 2319 | // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called |
| 2320 | // until the vtables for ProcessInstanceSettings are properly set up, i.e. AFTER all the initializers. |
| 2321 | // For this reason it has to be called here, rather than in the initializer or in the parent constructor. |
Caroline Tice | 75b11a3 | 2010-09-16 19:05:55 +0000 | [diff] [blame] | 2322 | // This is true for CreateInstanceName() too. |
| 2323 | |
| 2324 | if (GetInstanceName () == InstanceSettings::InvalidName()) |
| 2325 | { |
| 2326 | ChangeInstanceName (std::string (CreateInstanceName().AsCString())); |
| 2327 | m_owner.RegisterInstanceSettings (this); |
| 2328 | } |
Caroline Tice | 396704b | 2010-09-09 18:26:37 +0000 | [diff] [blame] | 2329 | |
| 2330 | if (live_instance) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2331 | { |
| 2332 | const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); |
| 2333 | CopyInstanceSettings (pending_settings,false); |
Caroline Tice | 396704b | 2010-09-09 18:26:37 +0000 | [diff] [blame] | 2334 | //m_owner.RemovePendingSettings (m_instance_name); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2335 | } |
| 2336 | } |
| 2337 | |
| 2338 | ProcessInstanceSettings::ProcessInstanceSettings (const ProcessInstanceSettings &rhs) : |
| 2339 | InstanceSettings (*(Process::GetSettingsController().get()), CreateInstanceName().AsCString()), |
| 2340 | m_run_args (rhs.m_run_args), |
| 2341 | m_env_vars (rhs.m_env_vars), |
| 2342 | m_input_path (rhs.m_input_path), |
| 2343 | m_output_path (rhs.m_output_path), |
| 2344 | m_error_path (rhs.m_error_path), |
| 2345 | m_plugin (rhs.m_plugin), |
| 2346 | m_disable_aslr (rhs.m_disable_aslr) |
| 2347 | { |
| 2348 | if (m_instance_name != InstanceSettings::GetDefaultName()) |
| 2349 | { |
| 2350 | const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); |
| 2351 | CopyInstanceSettings (pending_settings,false); |
| 2352 | m_owner.RemovePendingSettings (m_instance_name); |
| 2353 | } |
| 2354 | } |
| 2355 | |
| 2356 | ProcessInstanceSettings::~ProcessInstanceSettings () |
| 2357 | { |
| 2358 | } |
| 2359 | |
| 2360 | ProcessInstanceSettings& |
| 2361 | ProcessInstanceSettings::operator= (const ProcessInstanceSettings &rhs) |
| 2362 | { |
| 2363 | if (this != &rhs) |
| 2364 | { |
| 2365 | m_run_args = rhs.m_run_args; |
| 2366 | m_env_vars = rhs.m_env_vars; |
| 2367 | m_input_path = rhs.m_input_path; |
| 2368 | m_output_path = rhs.m_output_path; |
| 2369 | m_error_path = rhs.m_error_path; |
| 2370 | m_plugin = rhs.m_plugin; |
| 2371 | m_disable_aslr = rhs.m_disable_aslr; |
| 2372 | } |
| 2373 | |
| 2374 | return *this; |
| 2375 | } |
| 2376 | |
| 2377 | |
| 2378 | void |
| 2379 | ProcessInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name, |
| 2380 | const char *index_value, |
| 2381 | const char *value, |
| 2382 | const ConstString &instance_name, |
| 2383 | const SettingEntry &entry, |
| 2384 | lldb::VarSetOperationType op, |
| 2385 | Error &err, |
| 2386 | bool pending) |
| 2387 | { |
| 2388 | if (var_name == RunArgsVarName()) |
| 2389 | UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err); |
| 2390 | else if (var_name == EnvVarsVarName()) |
| 2391 | UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err); |
| 2392 | else if (var_name == InputPathVarName()) |
| 2393 | UserSettingsController::UpdateStringVariable (op, m_input_path, value, err); |
| 2394 | else if (var_name == OutputPathVarName()) |
| 2395 | UserSettingsController::UpdateStringVariable (op, m_output_path, value, err); |
| 2396 | else if (var_name == ErrorPathVarName()) |
| 2397 | UserSettingsController::UpdateStringVariable (op, m_error_path, value, err); |
| 2398 | else if (var_name == PluginVarName()) |
| 2399 | UserSettingsController::UpdateEnumVariable (entry.enum_values, (int *) &m_plugin, value, err); |
| 2400 | else if (var_name == DisableASLRVarName()) |
| 2401 | UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, err); |
| 2402 | } |
| 2403 | |
| 2404 | void |
| 2405 | ProcessInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, |
| 2406 | bool pending) |
| 2407 | { |
| 2408 | if (new_settings.get() == NULL) |
| 2409 | return; |
| 2410 | |
| 2411 | ProcessInstanceSettings *new_process_settings = (ProcessInstanceSettings *) new_settings.get(); |
| 2412 | |
| 2413 | m_run_args = new_process_settings->m_run_args; |
| 2414 | m_env_vars = new_process_settings->m_env_vars; |
| 2415 | m_input_path = new_process_settings->m_input_path; |
| 2416 | m_output_path = new_process_settings->m_output_path; |
| 2417 | m_error_path = new_process_settings->m_error_path; |
| 2418 | m_plugin = new_process_settings->m_plugin; |
| 2419 | m_disable_aslr = new_process_settings->m_disable_aslr; |
| 2420 | } |
| 2421 | |
Caroline Tice | bcb5b45 | 2010-09-20 21:37:42 +0000 | [diff] [blame] | 2422 | bool |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2423 | ProcessInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry, |
| 2424 | const ConstString &var_name, |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 2425 | StringList &value, |
Caroline Tice | bcb5b45 | 2010-09-20 21:37:42 +0000 | [diff] [blame] | 2426 | Error *err) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2427 | { |
| 2428 | if (var_name == RunArgsVarName()) |
| 2429 | { |
| 2430 | if (m_run_args.GetArgumentCount() > 0) |
Greg Clayton | c14069e | 2010-09-14 03:47:41 +0000 | [diff] [blame] | 2431 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2432 | for (int i = 0; i < m_run_args.GetArgumentCount(); ++i) |
| 2433 | value.AppendString (m_run_args.GetArgumentAtIndex (i)); |
Greg Clayton | c14069e | 2010-09-14 03:47:41 +0000 | [diff] [blame] | 2434 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2435 | } |
| 2436 | else if (var_name == EnvVarsVarName()) |
| 2437 | { |
| 2438 | if (m_env_vars.size() > 0) |
| 2439 | { |
| 2440 | std::map<std::string, std::string>::iterator pos; |
| 2441 | for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos) |
| 2442 | { |
| 2443 | StreamString value_str; |
| 2444 | value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str()); |
| 2445 | value.AppendString (value_str.GetData()); |
| 2446 | } |
| 2447 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2448 | } |
| 2449 | else if (var_name == InputPathVarName()) |
| 2450 | { |
| 2451 | value.AppendString (m_input_path.c_str()); |
| 2452 | } |
| 2453 | else if (var_name == OutputPathVarName()) |
| 2454 | { |
| 2455 | value.AppendString (m_output_path.c_str()); |
| 2456 | } |
| 2457 | else if (var_name == ErrorPathVarName()) |
| 2458 | { |
| 2459 | value.AppendString (m_error_path.c_str()); |
| 2460 | } |
| 2461 | else if (var_name == PluginVarName()) |
| 2462 | { |
| 2463 | value.AppendString (UserSettingsController::EnumToString (entry.enum_values, (int) m_plugin)); |
| 2464 | } |
| 2465 | else if (var_name == DisableASLRVarName()) |
| 2466 | { |
| 2467 | if (m_disable_aslr) |
| 2468 | value.AppendString ("true"); |
| 2469 | else |
| 2470 | value.AppendString ("false"); |
| 2471 | } |
| 2472 | else |
Caroline Tice | bcb5b45 | 2010-09-20 21:37:42 +0000 | [diff] [blame] | 2473 | { |
| 2474 | if (err) |
| 2475 | err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); |
| 2476 | return false; |
| 2477 | } |
| 2478 | return true; |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2481 | const ConstString |
| 2482 | ProcessInstanceSettings::CreateInstanceName () |
| 2483 | { |
| 2484 | static int instance_count = 1; |
| 2485 | StreamString sstr; |
| 2486 | |
| 2487 | sstr.Printf ("process_%d", instance_count); |
| 2488 | ++instance_count; |
| 2489 | |
| 2490 | const ConstString ret_val (sstr.GetData()); |
| 2491 | return ret_val; |
| 2492 | } |
| 2493 | |
| 2494 | const ConstString & |
| 2495 | ProcessInstanceSettings::RunArgsVarName () |
| 2496 | { |
| 2497 | static ConstString run_args_var_name ("run-args"); |
| 2498 | |
| 2499 | return run_args_var_name; |
| 2500 | } |
| 2501 | |
| 2502 | const ConstString & |
| 2503 | ProcessInstanceSettings::EnvVarsVarName () |
| 2504 | { |
| 2505 | static ConstString env_vars_var_name ("env-vars"); |
| 2506 | |
| 2507 | return env_vars_var_name; |
| 2508 | } |
| 2509 | |
| 2510 | const ConstString & |
| 2511 | ProcessInstanceSettings::InputPathVarName () |
| 2512 | { |
| 2513 | static ConstString input_path_var_name ("input-path"); |
| 2514 | |
| 2515 | return input_path_var_name; |
| 2516 | } |
| 2517 | |
| 2518 | const ConstString & |
| 2519 | ProcessInstanceSettings::OutputPathVarName () |
| 2520 | { |
Caroline Tice | 8709723 | 2010-09-07 18:35:40 +0000 | [diff] [blame] | 2521 | static ConstString output_path_var_name ("output-path"); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2522 | |
| 2523 | return output_path_var_name; |
| 2524 | } |
| 2525 | |
| 2526 | const ConstString & |
| 2527 | ProcessInstanceSettings::ErrorPathVarName () |
| 2528 | { |
Caroline Tice | 8709723 | 2010-09-07 18:35:40 +0000 | [diff] [blame] | 2529 | static ConstString error_path_var_name ("error-path"); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2530 | |
| 2531 | return error_path_var_name; |
| 2532 | } |
| 2533 | |
| 2534 | const ConstString & |
| 2535 | ProcessInstanceSettings::PluginVarName () |
| 2536 | { |
| 2537 | static ConstString plugin_var_name ("plugin"); |
| 2538 | |
| 2539 | return plugin_var_name; |
| 2540 | } |
| 2541 | |
| 2542 | |
| 2543 | const ConstString & |
| 2544 | ProcessInstanceSettings::DisableASLRVarName () |
| 2545 | { |
| 2546 | static ConstString disable_aslr_var_name ("disable-aslr"); |
| 2547 | |
| 2548 | return disable_aslr_var_name; |
| 2549 | } |
| 2550 | |
| 2551 | |
| 2552 | //-------------------------------------------------- |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 2553 | // SettingsController Variable Tables |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2554 | //-------------------------------------------------- |
| 2555 | |
| 2556 | SettingEntry |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 2557 | Process::SettingsController::global_settings_table[] = |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2558 | { |
| 2559 | //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"}, |
| 2560 | { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } |
| 2561 | }; |
| 2562 | |
| 2563 | |
| 2564 | lldb::OptionEnumValueElement |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 2565 | Process::SettingsController::g_plugins[] = |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2566 | { |
Caroline Tice | f2c330d | 2010-09-09 18:01:59 +0000 | [diff] [blame] | 2567 | { eMacosx, "process.macosx", "Use the native MacOSX debugger plugin" }, |
| 2568 | { eRemoteDebugger, "process.gdb-remote" , "Use the GDB Remote protocol based debugger plugin" }, |
| 2569 | { 0, NULL, NULL } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2570 | }; |
| 2571 | |
| 2572 | SettingEntry |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 2573 | Process::SettingsController::instance_settings_table[] = |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2574 | { |
| 2575 | //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"}, |
| 2576 | { "run-args", eSetVarTypeArray, NULL, NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." }, |
| 2577 | { "env-vars", eSetVarTypeDictionary, NULL, NULL, false, false, "A list of all the environment variables to be passed to the executable's environment, and their values." }, |
| 2578 | { "input-path", eSetVarTypeString, "/dev/stdin", NULL, false, false, "The file/path to be used by the executable program for reading its input." }, |
| 2579 | { "output-path", eSetVarTypeString, "/dev/stdout", NULL, false, false, "The file/path to be used by the executable program for writing its output." }, |
| 2580 | { "error-path", eSetVarTypeString, "/dev/stderr", NULL, false, false, "The file/path to be used by the executable program for writings its error messages." }, |
| 2581 | { "plugin", eSetVarTypeEnum, NULL , g_plugins, false, false, "The plugin to be used to run the process." }, |
Jim Ingham | 745ac7a | 2010-11-11 19:26:09 +0000 | [diff] [blame] | 2582 | { "disable-aslr", eSetVarTypeBoolean, "true", NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" }, |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2583 | { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } |
| 2584 | }; |
| 2585 | |
| 2586 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 2587 | |