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 | |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 40 | |
| 41 | //---------------------------------------------------------------------- |
| 42 | // MemoryCache constructor |
| 43 | //---------------------------------------------------------------------- |
| 44 | Process::MemoryCache::MemoryCache() : |
| 45 | m_cache_line_byte_size (512), |
| 46 | m_cache_mutex (Mutex::eMutexTypeRecursive), |
| 47 | m_cache () |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | //---------------------------------------------------------------------- |
| 52 | // Destructor |
| 53 | //---------------------------------------------------------------------- |
| 54 | Process::MemoryCache::~MemoryCache() |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | Process::MemoryCache::Clear() |
| 60 | { |
| 61 | Mutex::Locker locker (m_cache_mutex); |
| 62 | m_cache.clear(); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | Process::MemoryCache::Flush (addr_t addr, size_t size) |
| 67 | { |
| 68 | if (size == 0) |
| 69 | return; |
| 70 | |
| 71 | const uint32_t cache_line_byte_size = m_cache_line_byte_size; |
| 72 | const addr_t end_addr = (addr + size - 1); |
| 73 | const addr_t flush_start_addr = addr - (addr % cache_line_byte_size); |
| 74 | const addr_t flush_end_addr = end_addr - (end_addr % cache_line_byte_size); |
| 75 | |
| 76 | Mutex::Locker locker (m_cache_mutex); |
| 77 | if (m_cache.empty()) |
| 78 | return; |
| 79 | |
| 80 | assert ((flush_start_addr % cache_line_byte_size) == 0); |
| 81 | |
| 82 | for (addr_t curr_addr = flush_start_addr; curr_addr <= flush_end_addr; curr_addr += cache_line_byte_size) |
| 83 | { |
| 84 | collection::iterator pos = m_cache.find (curr_addr); |
| 85 | if (pos != m_cache.end()) |
| 86 | m_cache.erase(pos); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | size_t |
| 91 | Process::MemoryCache::Read |
| 92 | ( |
| 93 | Process *process, |
| 94 | addr_t addr, |
| 95 | void *dst, |
| 96 | size_t dst_len, |
| 97 | Error &error |
| 98 | ) |
| 99 | { |
| 100 | size_t bytes_left = dst_len; |
| 101 | if (dst && bytes_left > 0) |
| 102 | { |
| 103 | const uint32_t cache_line_byte_size = m_cache_line_byte_size; |
| 104 | uint8_t *dst_buf = (uint8_t *)dst; |
| 105 | addr_t curr_addr = addr - (addr % cache_line_byte_size); |
| 106 | addr_t cache_offset = addr - curr_addr; |
| 107 | Mutex::Locker locker (m_cache_mutex); |
| 108 | |
| 109 | while (bytes_left > 0) |
| 110 | { |
| 111 | collection::const_iterator pos = m_cache.find (curr_addr); |
| 112 | collection::const_iterator end = m_cache.end (); |
| 113 | |
| 114 | if (pos != end) |
| 115 | { |
| 116 | size_t curr_read_size = cache_line_byte_size - cache_offset; |
| 117 | if (curr_read_size > bytes_left) |
| 118 | curr_read_size = bytes_left; |
| 119 | |
| 120 | memcpy (dst_buf + dst_len - bytes_left, pos->second->GetBytes() + cache_offset, curr_read_size); |
| 121 | |
| 122 | bytes_left -= curr_read_size; |
| 123 | curr_addr += curr_read_size + cache_offset; |
| 124 | cache_offset = 0; |
| 125 | |
| 126 | if (bytes_left > 0) |
| 127 | { |
| 128 | // Get sequential cache page hits |
| 129 | for (++pos; (pos != end) && (bytes_left > 0); ++pos) |
| 130 | { |
| 131 | assert ((curr_addr % cache_line_byte_size) == 0); |
| 132 | |
| 133 | if (pos->first != curr_addr) |
| 134 | break; |
| 135 | |
| 136 | curr_read_size = pos->second->GetByteSize(); |
| 137 | if (curr_read_size > bytes_left) |
| 138 | curr_read_size = bytes_left; |
| 139 | |
| 140 | memcpy (dst_buf + dst_len - bytes_left, pos->second->GetBytes(), curr_read_size); |
| 141 | |
| 142 | bytes_left -= curr_read_size; |
| 143 | curr_addr += curr_read_size; |
| 144 | |
| 145 | // We have a cache page that succeeded to read some bytes |
| 146 | // but not an entire page. If this happens, we must cap |
| 147 | // off how much data we are able to read... |
| 148 | if (pos->second->GetByteSize() != cache_line_byte_size) |
| 149 | return dst_len - bytes_left; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // We need to read from the process |
| 155 | |
| 156 | if (bytes_left > 0) |
| 157 | { |
| 158 | assert ((curr_addr % cache_line_byte_size) == 0); |
| 159 | std::auto_ptr<DataBufferHeap> data_buffer_heap_ap(new DataBufferHeap (cache_line_byte_size, 0)); |
| 160 | size_t process_bytes_read = process->ReadMemoryFromInferior (curr_addr, |
| 161 | data_buffer_heap_ap->GetBytes(), |
| 162 | data_buffer_heap_ap->GetByteSize(), |
| 163 | error); |
| 164 | if (process_bytes_read == 0) |
| 165 | return dst_len - bytes_left; |
| 166 | |
| 167 | if (process_bytes_read != cache_line_byte_size) |
| 168 | data_buffer_heap_ap->SetByteSize (process_bytes_read); |
| 169 | m_cache[curr_addr] = DataBufferSP (data_buffer_heap_ap.release()); |
| 170 | // We have read data and put it into the cache, continue through the |
| 171 | // loop again to get the data out of the cache... |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return dst_len - bytes_left; |
| 177 | } |
| 178 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | Process* |
| 180 | Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener) |
| 181 | { |
| 182 | ProcessCreateInstance create_callback = NULL; |
| 183 | if (plugin_name) |
| 184 | { |
| 185 | create_callback = PluginManager::GetProcessCreateCallbackForPluginName (plugin_name); |
| 186 | if (create_callback) |
| 187 | { |
| 188 | std::auto_ptr<Process> debugger_ap(create_callback(target, listener)); |
| 189 | if (debugger_ap->CanDebug(target)) |
| 190 | return debugger_ap.release(); |
| 191 | } |
| 192 | } |
| 193 | else |
| 194 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 195 | for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 196 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 197 | std::auto_ptr<Process> debugger_ap(create_callback(target, listener)); |
| 198 | if (debugger_ap->CanDebug(target)) |
| 199 | return debugger_ap.release(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | //---------------------------------------------------------------------- |
| 207 | // Process constructor |
| 208 | //---------------------------------------------------------------------- |
| 209 | Process::Process(Target &target, Listener &listener) : |
| 210 | UserID (LLDB_INVALID_PROCESS_ID), |
Greg Clayton | 49ce682 | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 211 | Broadcaster ("lldb.process"), |
Greg Clayton | c0c1b0c | 2010-11-19 03:46:01 +0000 | [diff] [blame] | 212 | ProcessInstanceSettings (*GetSettingsController()), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 213 | m_target (target), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 214 | m_public_state (eStateUnloaded), |
| 215 | m_private_state (eStateUnloaded), |
| 216 | m_private_state_broadcaster ("lldb.process.internal_state_broadcaster"), |
| 217 | m_private_state_control_broadcaster ("lldb.process.internal_state_control_broadcaster"), |
| 218 | m_private_state_listener ("lldb.process.internal_state_listener"), |
| 219 | m_private_state_control_wait(), |
| 220 | m_private_state_thread (LLDB_INVALID_HOST_THREAD), |
| 221 | m_stop_id (0), |
| 222 | m_thread_index_id (0), |
| 223 | m_exit_status (-1), |
| 224 | m_exit_string (), |
| 225 | m_thread_list (this), |
| 226 | m_notifications (), |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 227 | m_image_tokens (), |
| 228 | m_listener (listener), |
| 229 | m_breakpoint_site_list (), |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 230 | m_dynamic_checkers_ap (), |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 231 | m_unix_signals (), |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 232 | m_target_triple (), |
Greg Clayton | cd54803 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 233 | m_byte_order (lldb::endian::InlHostByteOrder()), |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 234 | m_addr_byte_size (0), |
| 235 | m_abi_sp (), |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 236 | m_process_input_reader (), |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 237 | m_stdio_communication ("process.stdio"), |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 238 | m_stdio_communication_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 239 | m_stdout_data (), |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 240 | m_memory_cache (), |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 241 | m_next_event_action_ap() |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 242 | { |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 243 | UpdateInstanceName(); |
| 244 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 245 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 246 | if (log) |
| 247 | log->Printf ("%p Process::Process()", this); |
| 248 | |
Greg Clayton | 49ce682 | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 249 | SetEventName (eBroadcastBitStateChanged, "state-changed"); |
| 250 | SetEventName (eBroadcastBitInterrupt, "interrupt"); |
| 251 | SetEventName (eBroadcastBitSTDOUT, "stdout-available"); |
| 252 | SetEventName (eBroadcastBitSTDERR, "stderr-available"); |
| 253 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 254 | listener.StartListeningForEvents (this, |
| 255 | eBroadcastBitStateChanged | |
| 256 | eBroadcastBitInterrupt | |
| 257 | eBroadcastBitSTDOUT | |
| 258 | eBroadcastBitSTDERR); |
| 259 | |
| 260 | m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster, |
| 261 | eBroadcastBitStateChanged); |
| 262 | |
| 263 | m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster, |
| 264 | eBroadcastInternalStateControlStop | |
| 265 | eBroadcastInternalStateControlPause | |
| 266 | eBroadcastInternalStateControlResume); |
| 267 | } |
| 268 | |
| 269 | //---------------------------------------------------------------------- |
| 270 | // Destructor |
| 271 | //---------------------------------------------------------------------- |
| 272 | Process::~Process() |
| 273 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 274 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | if (log) |
| 276 | log->Printf ("%p Process::~Process()", this); |
| 277 | StopPrivateStateThread(); |
| 278 | } |
| 279 | |
| 280 | void |
| 281 | Process::Finalize() |
| 282 | { |
| 283 | // Do any cleanup needed prior to being destructed... Subclasses |
| 284 | // that override this method should call this superclass method as well. |
| 285 | } |
| 286 | |
| 287 | void |
| 288 | Process::RegisterNotificationCallbacks (const Notifications& callbacks) |
| 289 | { |
| 290 | m_notifications.push_back(callbacks); |
| 291 | if (callbacks.initialize != NULL) |
| 292 | callbacks.initialize (callbacks.baton, this); |
| 293 | } |
| 294 | |
| 295 | bool |
| 296 | Process::UnregisterNotificationCallbacks(const Notifications& callbacks) |
| 297 | { |
| 298 | std::vector<Notifications>::iterator pos, end = m_notifications.end(); |
| 299 | for (pos = m_notifications.begin(); pos != end; ++pos) |
| 300 | { |
| 301 | if (pos->baton == callbacks.baton && |
| 302 | pos->initialize == callbacks.initialize && |
| 303 | pos->process_state_changed == callbacks.process_state_changed) |
| 304 | { |
| 305 | m_notifications.erase(pos); |
| 306 | return true; |
| 307 | } |
| 308 | } |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | void |
| 313 | Process::SynchronouslyNotifyStateChanged (StateType state) |
| 314 | { |
| 315 | std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end(); |
| 316 | for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos) |
| 317 | { |
| 318 | if (notification_pos->process_state_changed) |
| 319 | notification_pos->process_state_changed (notification_pos->baton, this, state); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // FIXME: We need to do some work on events before the general Listener sees them. |
| 324 | // For instance if we are continuing from a breakpoint, we need to ensure that we do |
| 325 | // the little "insert real insn, step & stop" trick. But we can't do that when the |
| 326 | // event is delivered by the broadcaster - since that is done on the thread that is |
| 327 | // waiting for new events, so if we needed more than one event for our handling, we would |
| 328 | // stall. So instead we do it when we fetch the event off of the queue. |
| 329 | // |
| 330 | |
| 331 | StateType |
| 332 | Process::GetNextEvent (EventSP &event_sp) |
| 333 | { |
| 334 | StateType state = eStateInvalid; |
| 335 | |
| 336 | if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp) |
| 337 | state = Process::ProcessEventData::GetStateFromEvent (event_sp.get()); |
| 338 | |
| 339 | return state; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | StateType |
| 344 | Process::WaitForProcessToStop (const TimeValue *timeout) |
| 345 | { |
| 346 | StateType match_states[] = { eStateStopped, eStateCrashed, eStateDetached, eStateExited, eStateUnloaded }; |
| 347 | return WaitForState (timeout, match_states, sizeof(match_states) / sizeof(StateType)); |
| 348 | } |
| 349 | |
| 350 | |
| 351 | StateType |
| 352 | Process::WaitForState |
| 353 | ( |
| 354 | const TimeValue *timeout, |
| 355 | const StateType *match_states, const uint32_t num_match_states |
| 356 | ) |
| 357 | { |
| 358 | EventSP event_sp; |
| 359 | uint32_t i; |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 360 | StateType state = GetState(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 361 | while (state != eStateInvalid) |
| 362 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 363 | // If we are exited or detached, we won't ever get back to any |
| 364 | // other valid state... |
| 365 | if (state == eStateDetached || state == eStateExited) |
| 366 | return state; |
| 367 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 368 | state = WaitForStateChangedEvents (timeout, event_sp); |
| 369 | |
| 370 | for (i=0; i<num_match_states; ++i) |
| 371 | { |
| 372 | if (match_states[i] == state) |
| 373 | return state; |
| 374 | } |
| 375 | } |
| 376 | return state; |
| 377 | } |
| 378 | |
Jim Ingham | 63e24d7 | 2010-10-11 23:53:14 +0000 | [diff] [blame] | 379 | bool |
| 380 | Process::HijackProcessEvents (Listener *listener) |
| 381 | { |
| 382 | if (listener != NULL) |
| 383 | { |
| 384 | return HijackBroadcaster(listener, eBroadcastBitStateChanged); |
| 385 | } |
| 386 | else |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | void |
| 391 | Process::RestoreProcessEvents () |
| 392 | { |
| 393 | RestoreBroadcaster(); |
| 394 | } |
| 395 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 396 | StateType |
| 397 | Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp) |
| 398 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 399 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 400 | |
| 401 | if (log) |
| 402 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 403 | |
| 404 | StateType state = eStateInvalid; |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 405 | if (m_listener.WaitForEventForBroadcasterWithType (timeout, |
| 406 | this, |
| 407 | eBroadcastBitStateChanged, |
| 408 | event_sp)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 409 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 410 | |
| 411 | if (log) |
| 412 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", |
| 413 | __FUNCTION__, |
| 414 | timeout, |
| 415 | StateAsCString(state)); |
| 416 | return state; |
| 417 | } |
| 418 | |
| 419 | Event * |
| 420 | Process::PeekAtStateChangedEvents () |
| 421 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 422 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 423 | |
| 424 | if (log) |
| 425 | log->Printf ("Process::%s...", __FUNCTION__); |
| 426 | |
| 427 | Event *event_ptr; |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 428 | event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this, |
| 429 | eBroadcastBitStateChanged); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 430 | if (log) |
| 431 | { |
| 432 | if (event_ptr) |
| 433 | { |
| 434 | log->Printf ("Process::%s (event_ptr) => %s", |
| 435 | __FUNCTION__, |
| 436 | StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr))); |
| 437 | } |
| 438 | else |
| 439 | { |
| 440 | log->Printf ("Process::%s no events found", |
| 441 | __FUNCTION__); |
| 442 | } |
| 443 | } |
| 444 | return event_ptr; |
| 445 | } |
| 446 | |
| 447 | StateType |
| 448 | Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 449 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 450 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 451 | |
| 452 | if (log) |
| 453 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 454 | |
| 455 | StateType state = eStateInvalid; |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 456 | if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout, |
| 457 | &m_private_state_broadcaster, |
| 458 | eBroadcastBitStateChanged, |
| 459 | event_sp)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 460 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 461 | |
| 462 | // This is a bit of a hack, but when we wait here we could very well return |
| 463 | // to the command-line, and that could disable the log, which would render the |
| 464 | // log we got above invalid. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 465 | if (log) |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 466 | { |
| 467 | if (state == eStateInvalid) |
| 468 | log->Printf ("Process::%s (timeout = %p, event_sp) => TIMEOUT", __FUNCTION__, timeout); |
| 469 | else |
| 470 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", __FUNCTION__, timeout, StateAsCString(state)); |
| 471 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 472 | return state; |
| 473 | } |
| 474 | |
| 475 | bool |
| 476 | Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only) |
| 477 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 478 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 479 | |
| 480 | if (log) |
| 481 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 482 | |
| 483 | if (control_only) |
| 484 | return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp); |
| 485 | else |
| 486 | return m_private_state_listener.WaitForEvent(timeout, event_sp); |
| 487 | } |
| 488 | |
| 489 | bool |
| 490 | Process::IsRunning () const |
| 491 | { |
| 492 | return StateIsRunningState (m_public_state.GetValue()); |
| 493 | } |
| 494 | |
| 495 | int |
| 496 | Process::GetExitStatus () |
| 497 | { |
| 498 | if (m_public_state.GetValue() == eStateExited) |
| 499 | return m_exit_status; |
| 500 | return -1; |
| 501 | } |
| 502 | |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 503 | |
| 504 | void |
| 505 | Process::ProcessInstanceSettings::GetHostEnvironmentIfNeeded () |
| 506 | { |
| 507 | if (m_inherit_host_env && !m_got_host_env) |
| 508 | { |
| 509 | m_got_host_env = true; |
| 510 | StringList host_env; |
| 511 | const size_t host_env_count = Host::GetEnvironment (host_env); |
| 512 | for (size_t idx=0; idx<host_env_count; idx++) |
| 513 | { |
| 514 | const char *env_entry = host_env.GetStringAtIndex (idx); |
| 515 | if (env_entry) |
| 516 | { |
Greg Clayton | 1f3dd64 | 2010-12-15 20:52:40 +0000 | [diff] [blame] | 517 | const char *equal_pos = ::strchr(env_entry, '='); |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 518 | if (equal_pos) |
| 519 | { |
| 520 | std::string key (env_entry, equal_pos - env_entry); |
| 521 | std::string value (equal_pos + 1); |
| 522 | if (m_env_vars.find (key) == m_env_vars.end()) |
| 523 | m_env_vars[key] = value; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | |
| 531 | size_t |
| 532 | Process::ProcessInstanceSettings::GetEnvironmentAsArgs (Args &env) |
| 533 | { |
| 534 | GetHostEnvironmentIfNeeded (); |
| 535 | |
| 536 | dictionary::const_iterator pos, end = m_env_vars.end(); |
| 537 | for (pos = m_env_vars.begin(); pos != end; ++pos) |
| 538 | { |
| 539 | std::string env_var_equal_value (pos->first); |
| 540 | env_var_equal_value.append(1, '='); |
| 541 | env_var_equal_value.append (pos->second); |
| 542 | env.AppendArgument (env_var_equal_value.c_str()); |
| 543 | } |
| 544 | return env.GetArgumentCount(); |
| 545 | } |
| 546 | |
| 547 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 548 | const char * |
| 549 | Process::GetExitDescription () |
| 550 | { |
| 551 | if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty()) |
| 552 | return m_exit_string.c_str(); |
| 553 | return NULL; |
| 554 | } |
| 555 | |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 556 | bool |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 557 | Process::SetExitStatus (int status, const char *cstr) |
| 558 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 559 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
| 560 | if (log) |
| 561 | log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)", |
| 562 | status, status, |
| 563 | cstr ? "\"" : "", |
| 564 | cstr ? cstr : "NULL", |
| 565 | cstr ? "\"" : ""); |
| 566 | |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 567 | // We were already in the exited state |
| 568 | if (m_private_state.GetValue() == eStateExited) |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 569 | { |
Greg Clayton | 644ddfb | 2011-01-26 23:47:29 +0000 | [diff] [blame] | 570 | if (log) |
| 571 | log->Printf("Process::SetExitStatus () ignoring exit status because state was already set to eStateExited"); |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 572 | return false; |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 573 | } |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 574 | |
| 575 | m_exit_status = status; |
| 576 | if (cstr) |
| 577 | m_exit_string = cstr; |
| 578 | else |
| 579 | m_exit_string.clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 580 | |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 581 | DidExit (); |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 582 | |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 583 | SetPrivateState (eStateExited); |
| 584 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | // This static callback can be used to watch for local child processes on |
| 588 | // the current host. The the child process exits, the process will be |
| 589 | // found in the global target list (we want to be completely sure that the |
| 590 | // lldb_private::Process doesn't go away before we can deliver the signal. |
| 591 | bool |
| 592 | Process::SetProcessExitStatus |
| 593 | ( |
| 594 | void *callback_baton, |
| 595 | lldb::pid_t pid, |
| 596 | int signo, // Zero for no signal |
| 597 | int exit_status // Exit value of process if signal is zero |
| 598 | ) |
| 599 | { |
| 600 | if (signo == 0 || exit_status) |
| 601 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 602 | TargetSP target_sp(Debugger::FindTargetWithProcessID (pid)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 603 | if (target_sp) |
| 604 | { |
| 605 | ProcessSP process_sp (target_sp->GetProcessSP()); |
| 606 | if (process_sp) |
| 607 | { |
| 608 | const char *signal_cstr = NULL; |
| 609 | if (signo) |
| 610 | signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo); |
| 611 | |
| 612 | process_sp->SetExitStatus (exit_status, signal_cstr); |
| 613 | } |
| 614 | } |
| 615 | return true; |
| 616 | } |
| 617 | return false; |
| 618 | } |
| 619 | |
| 620 | |
| 621 | uint32_t |
| 622 | Process::GetNextThreadIndexID () |
| 623 | { |
| 624 | return ++m_thread_index_id; |
| 625 | } |
| 626 | |
| 627 | StateType |
| 628 | Process::GetState() |
| 629 | { |
| 630 | // If any other threads access this we will need a mutex for it |
| 631 | return m_public_state.GetValue (); |
| 632 | } |
| 633 | |
| 634 | void |
| 635 | Process::SetPublicState (StateType new_state) |
| 636 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 637 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 638 | if (log) |
| 639 | log->Printf("Process::SetPublicState (%s)", StateAsCString(new_state)); |
| 640 | m_public_state.SetValue (new_state); |
| 641 | } |
| 642 | |
| 643 | StateType |
| 644 | Process::GetPrivateState () |
| 645 | { |
| 646 | return m_private_state.GetValue(); |
| 647 | } |
| 648 | |
| 649 | void |
| 650 | Process::SetPrivateState (StateType new_state) |
| 651 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 652 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 653 | bool state_changed = false; |
| 654 | |
| 655 | if (log) |
| 656 | log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state)); |
| 657 | |
| 658 | Mutex::Locker locker(m_private_state.GetMutex()); |
| 659 | |
| 660 | const StateType old_state = m_private_state.GetValueNoLock (); |
| 661 | state_changed = old_state != new_state; |
| 662 | if (state_changed) |
| 663 | { |
| 664 | m_private_state.SetValueNoLock (new_state); |
| 665 | if (StateIsStoppedState(new_state)) |
| 666 | { |
| 667 | m_stop_id++; |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 668 | m_memory_cache.Clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 669 | if (log) |
| 670 | log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_stop_id); |
| 671 | } |
| 672 | // Use our target to get a shared pointer to ourselves... |
| 673 | m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (GetTarget().GetProcessSP(), new_state)); |
| 674 | } |
| 675 | else |
| 676 | { |
| 677 | if (log) |
| 678 | log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state), StateAsCString(old_state)); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | |
| 683 | uint32_t |
| 684 | Process::GetStopID() const |
| 685 | { |
| 686 | return m_stop_id; |
| 687 | } |
| 688 | |
| 689 | addr_t |
| 690 | Process::GetImageInfoAddress() |
| 691 | { |
| 692 | return LLDB_INVALID_ADDRESS; |
| 693 | } |
| 694 | |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 695 | //---------------------------------------------------------------------- |
| 696 | // LoadImage |
| 697 | // |
| 698 | // This function provides a default implementation that works for most |
| 699 | // unix variants. Any Process subclasses that need to do shared library |
| 700 | // loading differently should override LoadImage and UnloadImage and |
| 701 | // do what is needed. |
| 702 | //---------------------------------------------------------------------- |
| 703 | uint32_t |
| 704 | Process::LoadImage (const FileSpec &image_spec, Error &error) |
| 705 | { |
| 706 | DynamicLoader *loader = GetDynamicLoader(); |
| 707 | if (loader) |
| 708 | { |
| 709 | error = loader->CanLoadImage(); |
| 710 | if (error.Fail()) |
| 711 | return LLDB_INVALID_IMAGE_TOKEN; |
| 712 | } |
| 713 | |
| 714 | if (error.Success()) |
| 715 | { |
| 716 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
| 717 | if (thread_sp == NULL) |
| 718 | thread_sp = GetThreadList ().GetThreadAtIndex(0, true); |
| 719 | |
| 720 | if (thread_sp) |
| 721 | { |
| 722 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
| 723 | |
| 724 | if (frame_sp) |
| 725 | { |
| 726 | ExecutionContext exe_ctx; |
| 727 | frame_sp->CalculateExecutionContext (exe_ctx); |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 728 | bool unwind_on_error = true; |
Sean Callanan | 6a92553 | 2011-01-13 08:53:35 +0000 | [diff] [blame] | 729 | bool keep_in_memory = false; |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 730 | StreamString expr; |
| 731 | char path[PATH_MAX]; |
| 732 | image_spec.GetPath(path, sizeof(path)); |
| 733 | expr.Printf("dlopen (\"%s\", 2)", path); |
| 734 | const char *prefix = "extern \"C\" void* dlopen (const char *path, int mode);\n"; |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 735 | lldb::ValueObjectSP result_valobj_sp; |
Sean Callanan | 6a92553 | 2011-01-13 08:53:35 +0000 | [diff] [blame] | 736 | ClangUserExpression::Evaluate (exe_ctx, keep_in_memory, unwind_on_error, expr.GetData(), prefix, result_valobj_sp); |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 737 | if (result_valobj_sp->GetError().Success()) |
| 738 | { |
| 739 | Scalar scalar; |
| 740 | if (result_valobj_sp->ResolveValue (frame_sp.get(), scalar)) |
| 741 | { |
| 742 | addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 743 | if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS) |
| 744 | { |
| 745 | uint32_t image_token = m_image_tokens.size(); |
| 746 | m_image_tokens.push_back (image_ptr); |
| 747 | return image_token; |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | return LLDB_INVALID_IMAGE_TOKEN; |
| 755 | } |
| 756 | |
| 757 | //---------------------------------------------------------------------- |
| 758 | // UnloadImage |
| 759 | // |
| 760 | // This function provides a default implementation that works for most |
| 761 | // unix variants. Any Process subclasses that need to do shared library |
| 762 | // loading differently should override LoadImage and UnloadImage and |
| 763 | // do what is needed. |
| 764 | //---------------------------------------------------------------------- |
| 765 | Error |
| 766 | Process::UnloadImage (uint32_t image_token) |
| 767 | { |
| 768 | Error error; |
| 769 | if (image_token < m_image_tokens.size()) |
| 770 | { |
| 771 | const addr_t image_addr = m_image_tokens[image_token]; |
| 772 | if (image_addr == LLDB_INVALID_ADDRESS) |
| 773 | { |
| 774 | error.SetErrorString("image already unloaded"); |
| 775 | } |
| 776 | else |
| 777 | { |
| 778 | DynamicLoader *loader = GetDynamicLoader(); |
| 779 | if (loader) |
| 780 | error = loader->CanLoadImage(); |
| 781 | |
| 782 | if (error.Success()) |
| 783 | { |
| 784 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
| 785 | if (thread_sp == NULL) |
| 786 | thread_sp = GetThreadList ().GetThreadAtIndex(0, true); |
| 787 | |
| 788 | if (thread_sp) |
| 789 | { |
| 790 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
| 791 | |
| 792 | if (frame_sp) |
| 793 | { |
| 794 | ExecutionContext exe_ctx; |
| 795 | frame_sp->CalculateExecutionContext (exe_ctx); |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 796 | bool unwind_on_error = true; |
Sean Callanan | 6a92553 | 2011-01-13 08:53:35 +0000 | [diff] [blame] | 797 | bool keep_in_memory = false; |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 798 | StreamString expr; |
| 799 | expr.Printf("dlclose ((void *)0x%llx)", image_addr); |
| 800 | const char *prefix = "extern \"C\" int dlclose(void* handle);\n"; |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 801 | lldb::ValueObjectSP result_valobj_sp; |
Sean Callanan | 6a92553 | 2011-01-13 08:53:35 +0000 | [diff] [blame] | 802 | ClangUserExpression::Evaluate (exe_ctx, unwind_on_error, keep_in_memory, expr.GetData(), prefix, result_valobj_sp); |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 803 | if (result_valobj_sp->GetError().Success()) |
| 804 | { |
| 805 | Scalar scalar; |
| 806 | if (result_valobj_sp->ResolveValue (frame_sp.get(), scalar)) |
| 807 | { |
| 808 | if (scalar.UInt(1)) |
| 809 | { |
| 810 | error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData()); |
| 811 | } |
| 812 | else |
| 813 | { |
| 814 | m_image_tokens[image_token] = LLDB_INVALID_ADDRESS; |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | else |
| 819 | { |
| 820 | error = result_valobj_sp->GetError(); |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | else |
| 828 | { |
| 829 | error.SetErrorString("invalid image token"); |
| 830 | } |
| 831 | return error; |
| 832 | } |
| 833 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 834 | DynamicLoader * |
| 835 | Process::GetDynamicLoader() |
| 836 | { |
| 837 | return NULL; |
| 838 | } |
| 839 | |
| 840 | const ABI * |
| 841 | Process::GetABI() |
| 842 | { |
| 843 | ConstString& triple = m_target_triple; |
| 844 | |
| 845 | if (triple.IsEmpty()) |
| 846 | return NULL; |
| 847 | |
| 848 | if (m_abi_sp.get() == NULL) |
| 849 | { |
| 850 | m_abi_sp.reset(ABI::FindPlugin(triple)); |
| 851 | } |
| 852 | |
| 853 | return m_abi_sp.get(); |
| 854 | } |
| 855 | |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 856 | LanguageRuntime * |
| 857 | Process::GetLanguageRuntime(lldb::LanguageType language) |
| 858 | { |
| 859 | LanguageRuntimeCollection::iterator pos; |
| 860 | pos = m_language_runtimes.find (language); |
| 861 | if (pos == m_language_runtimes.end()) |
| 862 | { |
| 863 | lldb::LanguageRuntimeSP runtime(LanguageRuntime::FindPlugin(this, language)); |
| 864 | |
| 865 | m_language_runtimes[language] |
| 866 | = runtime; |
| 867 | return runtime.get(); |
| 868 | } |
| 869 | else |
| 870 | return (*pos).second.get(); |
| 871 | } |
| 872 | |
| 873 | CPPLanguageRuntime * |
| 874 | Process::GetCPPLanguageRuntime () |
| 875 | { |
| 876 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus); |
| 877 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus) |
| 878 | return static_cast<CPPLanguageRuntime *> (runtime); |
| 879 | return NULL; |
| 880 | } |
| 881 | |
| 882 | ObjCLanguageRuntime * |
| 883 | Process::GetObjCLanguageRuntime () |
| 884 | { |
| 885 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC); |
| 886 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC) |
| 887 | return static_cast<ObjCLanguageRuntime *> (runtime); |
| 888 | return NULL; |
| 889 | } |
| 890 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 891 | BreakpointSiteList & |
| 892 | Process::GetBreakpointSiteList() |
| 893 | { |
| 894 | return m_breakpoint_site_list; |
| 895 | } |
| 896 | |
| 897 | const BreakpointSiteList & |
| 898 | Process::GetBreakpointSiteList() const |
| 899 | { |
| 900 | return m_breakpoint_site_list; |
| 901 | } |
| 902 | |
| 903 | |
| 904 | void |
| 905 | Process::DisableAllBreakpointSites () |
| 906 | { |
| 907 | m_breakpoint_site_list.SetEnabledForAll (false); |
| 908 | } |
| 909 | |
| 910 | Error |
| 911 | Process::ClearBreakpointSiteByID (lldb::user_id_t break_id) |
| 912 | { |
| 913 | Error error (DisableBreakpointSiteByID (break_id)); |
| 914 | |
| 915 | if (error.Success()) |
| 916 | m_breakpoint_site_list.Remove(break_id); |
| 917 | |
| 918 | return error; |
| 919 | } |
| 920 | |
| 921 | Error |
| 922 | Process::DisableBreakpointSiteByID (lldb::user_id_t break_id) |
| 923 | { |
| 924 | Error error; |
| 925 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 926 | if (bp_site_sp) |
| 927 | { |
| 928 | if (bp_site_sp->IsEnabled()) |
| 929 | error = DisableBreakpoint (bp_site_sp.get()); |
| 930 | } |
| 931 | else |
| 932 | { |
| 933 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id); |
| 934 | } |
| 935 | |
| 936 | return error; |
| 937 | } |
| 938 | |
| 939 | Error |
| 940 | Process::EnableBreakpointSiteByID (lldb::user_id_t break_id) |
| 941 | { |
| 942 | Error error; |
| 943 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 944 | if (bp_site_sp) |
| 945 | { |
| 946 | if (!bp_site_sp->IsEnabled()) |
| 947 | error = EnableBreakpoint (bp_site_sp.get()); |
| 948 | } |
| 949 | else |
| 950 | { |
| 951 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %i", break_id); |
| 952 | } |
| 953 | return error; |
| 954 | } |
| 955 | |
Stephen Wilson | 3fd1f36 | 2010-07-17 00:56:13 +0000 | [diff] [blame] | 956 | lldb::break_id_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 957 | Process::CreateBreakpointSite (BreakpointLocationSP &owner, bool use_hardware) |
| 958 | { |
Greg Clayton | eea2640 | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 959 | const addr_t load_addr = owner->GetAddress().GetLoadAddress (&m_target); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 960 | if (load_addr != LLDB_INVALID_ADDRESS) |
| 961 | { |
| 962 | BreakpointSiteSP bp_site_sp; |
| 963 | |
| 964 | // Look up this breakpoint site. If it exists, then add this new owner, otherwise |
| 965 | // create a new breakpoint site and add it. |
| 966 | |
| 967 | bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr); |
| 968 | |
| 969 | if (bp_site_sp) |
| 970 | { |
| 971 | bp_site_sp->AddOwner (owner); |
| 972 | owner->SetBreakpointSite (bp_site_sp); |
| 973 | return bp_site_sp->GetID(); |
| 974 | } |
| 975 | else |
| 976 | { |
| 977 | bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, LLDB_INVALID_THREAD_ID, use_hardware)); |
| 978 | if (bp_site_sp) |
| 979 | { |
| 980 | if (EnableBreakpoint (bp_site_sp.get()).Success()) |
| 981 | { |
| 982 | owner->SetBreakpointSite (bp_site_sp); |
| 983 | return m_breakpoint_site_list.Add (bp_site_sp); |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | // We failed to enable the breakpoint |
| 989 | return LLDB_INVALID_BREAK_ID; |
| 990 | |
| 991 | } |
| 992 | |
| 993 | void |
| 994 | Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp) |
| 995 | { |
| 996 | uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id); |
| 997 | if (num_owners == 0) |
| 998 | { |
| 999 | DisableBreakpoint(bp_site_sp.get()); |
| 1000 | m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress()); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | |
| 1005 | size_t |
| 1006 | Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const |
| 1007 | { |
| 1008 | size_t bytes_removed = 0; |
| 1009 | addr_t intersect_addr; |
| 1010 | size_t intersect_size; |
| 1011 | size_t opcode_offset; |
| 1012 | size_t idx; |
| 1013 | BreakpointSiteSP bp; |
| 1014 | |
| 1015 | for (idx = 0; (bp = m_breakpoint_site_list.GetByIndex(idx)) != NULL; ++idx) |
| 1016 | { |
| 1017 | if (bp->GetType() == BreakpointSite::eSoftware) |
| 1018 | { |
| 1019 | if (bp->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset)) |
| 1020 | { |
| 1021 | assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size); |
| 1022 | assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size); |
| 1023 | assert(opcode_offset + intersect_size <= bp->GetByteSize()); |
| 1024 | size_t buf_offset = intersect_addr - bp_addr; |
| 1025 | ::memcpy(buf + buf_offset, bp->GetSavedOpcodeBytes() + opcode_offset, intersect_size); |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | return bytes_removed; |
| 1030 | } |
| 1031 | |
| 1032 | |
| 1033 | Error |
| 1034 | Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 1035 | { |
| 1036 | Error error; |
| 1037 | assert (bp_site != NULL); |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1038 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1039 | const addr_t bp_addr = bp_site->GetLoadAddress(); |
| 1040 | if (log) |
| 1041 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx", bp_site->GetID(), (uint64_t)bp_addr); |
| 1042 | if (bp_site->IsEnabled()) |
| 1043 | { |
| 1044 | if (log) |
| 1045 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already enabled", bp_site->GetID(), (uint64_t)bp_addr); |
| 1046 | return error; |
| 1047 | } |
| 1048 | |
| 1049 | if (bp_addr == LLDB_INVALID_ADDRESS) |
| 1050 | { |
| 1051 | error.SetErrorString("BreakpointSite contains an invalid load address."); |
| 1052 | return error; |
| 1053 | } |
| 1054 | // Ask the lldb::Process subclass to fill in the correct software breakpoint |
| 1055 | // trap for the breakpoint site |
| 1056 | const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 1057 | |
| 1058 | if (bp_opcode_size == 0) |
| 1059 | { |
| 1060 | error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%llx.\n", bp_addr); |
| 1061 | } |
| 1062 | else |
| 1063 | { |
| 1064 | const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes(); |
| 1065 | |
| 1066 | if (bp_opcode_bytes == NULL) |
| 1067 | { |
| 1068 | error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode."); |
| 1069 | return error; |
| 1070 | } |
| 1071 | |
| 1072 | // Save the original opcode by reading it |
| 1073 | if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size) |
| 1074 | { |
| 1075 | // Write a software breakpoint in place of the original opcode |
| 1076 | if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 1077 | { |
| 1078 | uint8_t verify_bp_opcode_bytes[64]; |
| 1079 | if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 1080 | { |
| 1081 | if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0) |
| 1082 | { |
| 1083 | bp_site->SetEnabled(true); |
| 1084 | bp_site->SetType (BreakpointSite::eSoftware); |
| 1085 | if (log) |
| 1086 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", |
| 1087 | bp_site->GetID(), |
| 1088 | (uint64_t)bp_addr); |
| 1089 | } |
| 1090 | else |
| 1091 | error.SetErrorString("Failed to verify the breakpoint trap in memory."); |
| 1092 | } |
| 1093 | else |
| 1094 | error.SetErrorString("Unable to read memory to verify breakpoint trap."); |
| 1095 | } |
| 1096 | else |
| 1097 | error.SetErrorString("Unable to write breakpoint trap to memory."); |
| 1098 | } |
| 1099 | else |
| 1100 | error.SetErrorString("Unable to read memory at breakpoint address."); |
| 1101 | } |
Stephen Wilson | c2b9825 | 2011-01-12 04:20:03 +0000 | [diff] [blame] | 1102 | if (log && error.Fail()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1103 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s", |
| 1104 | bp_site->GetID(), |
| 1105 | (uint64_t)bp_addr, |
| 1106 | error.AsCString()); |
| 1107 | return error; |
| 1108 | } |
| 1109 | |
| 1110 | Error |
| 1111 | Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 1112 | { |
| 1113 | Error error; |
| 1114 | assert (bp_site != NULL); |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1115 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1116 | addr_t bp_addr = bp_site->GetLoadAddress(); |
| 1117 | lldb::user_id_t breakID = bp_site->GetID(); |
| 1118 | if (log) |
Stephen Wilson | 9ff73ed | 2011-01-14 21:07:07 +0000 | [diff] [blame] | 1119 | log->Printf ("Process::DisableBreakpoint (breakID = %d) addr = 0x%llx", breakID, (uint64_t)bp_addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1120 | |
| 1121 | if (bp_site->IsHardware()) |
| 1122 | { |
| 1123 | error.SetErrorString("Breakpoint site is a hardware breakpoint."); |
| 1124 | } |
| 1125 | else if (bp_site->IsEnabled()) |
| 1126 | { |
| 1127 | const size_t break_op_size = bp_site->GetByteSize(); |
| 1128 | const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes(); |
| 1129 | if (break_op_size > 0) |
| 1130 | { |
| 1131 | // Clear a software breakoint instruction |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1132 | uint8_t curr_break_op[8]; |
Stephen Wilson | 141eeac | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 1133 | assert (break_op_size <= sizeof(curr_break_op)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1134 | bool break_op_found = false; |
| 1135 | |
| 1136 | // Read the breakpoint opcode |
| 1137 | if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size) |
| 1138 | { |
| 1139 | bool verify = false; |
| 1140 | // Make sure we have the a breakpoint opcode exists at this address |
| 1141 | if (::memcmp (curr_break_op, break_op, break_op_size) == 0) |
| 1142 | { |
| 1143 | break_op_found = true; |
| 1144 | // We found a valid breakpoint opcode at this address, now restore |
| 1145 | // the saved opcode. |
| 1146 | if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size) |
| 1147 | { |
| 1148 | verify = true; |
| 1149 | } |
| 1150 | else |
| 1151 | error.SetErrorString("Memory write failed when restoring original opcode."); |
| 1152 | } |
| 1153 | else |
| 1154 | { |
| 1155 | error.SetErrorString("Original breakpoint trap is no longer in memory."); |
| 1156 | // Set verify to true and so we can check if the original opcode has already been restored |
| 1157 | verify = true; |
| 1158 | } |
| 1159 | |
| 1160 | if (verify) |
| 1161 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1162 | uint8_t verify_opcode[8]; |
Stephen Wilson | 141eeac | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 1163 | assert (break_op_size < sizeof(verify_opcode)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1164 | // Verify that our original opcode made it back to the inferior |
| 1165 | if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size) |
| 1166 | { |
| 1167 | // compare the memory we just read with the original opcode |
| 1168 | if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0) |
| 1169 | { |
| 1170 | // SUCCESS |
| 1171 | bp_site->SetEnabled(false); |
| 1172 | if (log) |
| 1173 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", bp_site->GetID(), (uint64_t)bp_addr); |
| 1174 | return error; |
| 1175 | } |
| 1176 | else |
| 1177 | { |
| 1178 | if (break_op_found) |
| 1179 | error.SetErrorString("Failed to restore original opcode."); |
| 1180 | } |
| 1181 | } |
| 1182 | else |
| 1183 | error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored."); |
| 1184 | } |
| 1185 | } |
| 1186 | else |
| 1187 | error.SetErrorString("Unable to read memory that should contain the breakpoint trap."); |
| 1188 | } |
| 1189 | } |
| 1190 | else |
| 1191 | { |
| 1192 | if (log) |
| 1193 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already disabled", bp_site->GetID(), (uint64_t)bp_addr); |
| 1194 | return error; |
| 1195 | } |
| 1196 | |
| 1197 | if (log) |
| 1198 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s", |
| 1199 | bp_site->GetID(), |
| 1200 | (uint64_t)bp_addr, |
| 1201 | error.AsCString()); |
| 1202 | return error; |
| 1203 | |
| 1204 | } |
| 1205 | |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1206 | // Comment out line below to disable memory caching |
| 1207 | #define ENABLE_MEMORY_CACHING |
| 1208 | // Uncomment to verify memory caching works after making changes to caching code |
| 1209 | //#define VERIFY_MEMORY_READS |
| 1210 | |
| 1211 | #if defined (ENABLE_MEMORY_CACHING) |
| 1212 | |
| 1213 | #if defined (VERIFY_MEMORY_READS) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1214 | |
| 1215 | size_t |
| 1216 | Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 1217 | { |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1218 | // Memory caching is enabled, with debug verification |
| 1219 | if (buf && size) |
| 1220 | { |
| 1221 | // Uncomment the line below to make sure memory caching is working. |
| 1222 | // I ran this through the test suite and got no assertions, so I am |
| 1223 | // pretty confident this is working well. If any changes are made to |
| 1224 | // memory caching, uncomment the line below and test your changes! |
| 1225 | |
| 1226 | // Verify all memory reads by using the cache first, then redundantly |
| 1227 | // reading the same memory from the inferior and comparing to make sure |
| 1228 | // everything is exactly the same. |
| 1229 | std::string verify_buf (size, '\0'); |
| 1230 | assert (verify_buf.size() == size); |
| 1231 | const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error); |
| 1232 | Error verify_error; |
| 1233 | const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error); |
| 1234 | assert (cache_bytes_read == verify_bytes_read); |
| 1235 | assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0); |
| 1236 | assert (verify_error.Success() == error.Success()); |
| 1237 | return cache_bytes_read; |
| 1238 | } |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | #else // #if defined (VERIFY_MEMORY_READS) |
| 1243 | |
| 1244 | size_t |
| 1245 | Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 1246 | { |
| 1247 | // Memory caching enabled, no verification |
| 1248 | return m_memory_cache.Read (this, addr, buf, size, error); |
| 1249 | } |
| 1250 | |
| 1251 | #endif // #else for #if defined (VERIFY_MEMORY_READS) |
| 1252 | |
| 1253 | #else // #if defined (ENABLE_MEMORY_CACHING) |
| 1254 | |
| 1255 | size_t |
| 1256 | Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 1257 | { |
| 1258 | // Memory caching is disabled |
| 1259 | return ReadMemoryFromInferior (addr, buf, size, error); |
| 1260 | } |
| 1261 | |
| 1262 | #endif // #else for #if defined (ENABLE_MEMORY_CACHING) |
| 1263 | |
| 1264 | |
| 1265 | size_t |
| 1266 | Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error) |
| 1267 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1268 | if (buf == NULL || size == 0) |
| 1269 | return 0; |
| 1270 | |
| 1271 | size_t bytes_read = 0; |
| 1272 | uint8_t *bytes = (uint8_t *)buf; |
| 1273 | |
| 1274 | while (bytes_read < size) |
| 1275 | { |
| 1276 | const size_t curr_size = size - bytes_read; |
| 1277 | const size_t curr_bytes_read = DoReadMemory (addr + bytes_read, |
| 1278 | bytes + bytes_read, |
| 1279 | curr_size, |
| 1280 | error); |
| 1281 | bytes_read += curr_bytes_read; |
| 1282 | if (curr_bytes_read == curr_size || curr_bytes_read == 0) |
| 1283 | break; |
| 1284 | } |
| 1285 | |
| 1286 | // Replace any software breakpoint opcodes that fall into this range back |
| 1287 | // into "buf" before we return |
| 1288 | if (bytes_read > 0) |
| 1289 | RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf); |
| 1290 | return bytes_read; |
| 1291 | } |
| 1292 | |
Greg Clayton | f72fdee | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 1293 | uint64_t |
| 1294 | Process::ReadUnsignedInteger (lldb::addr_t vm_addr, size_t integer_byte_size, Error &error) |
| 1295 | { |
| 1296 | if (integer_byte_size > sizeof(uint64_t)) |
| 1297 | { |
| 1298 | error.SetErrorString ("unsupported integer size"); |
| 1299 | } |
| 1300 | else |
| 1301 | { |
| 1302 | uint8_t tmp[sizeof(uint64_t)]; |
| 1303 | DataExtractor data (tmp, integer_byte_size, GetByteOrder(), GetAddressByteSize()); |
| 1304 | if (ReadMemory (vm_addr, tmp, integer_byte_size, error) == integer_byte_size) |
| 1305 | { |
| 1306 | uint32_t offset = 0; |
| 1307 | return data.GetMaxU64 (&offset, integer_byte_size); |
| 1308 | } |
| 1309 | } |
| 1310 | // Any plug-in that doesn't return success a memory read with the number |
| 1311 | // of bytes that were requested should be setting the error |
| 1312 | assert (error.Fail()); |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1316 | size_t |
| 1317 | Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error) |
| 1318 | { |
| 1319 | size_t bytes_written = 0; |
| 1320 | const uint8_t *bytes = (const uint8_t *)buf; |
| 1321 | |
| 1322 | while (bytes_written < size) |
| 1323 | { |
| 1324 | const size_t curr_size = size - bytes_written; |
| 1325 | const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written, |
| 1326 | bytes + bytes_written, |
| 1327 | curr_size, |
| 1328 | error); |
| 1329 | bytes_written += curr_bytes_written; |
| 1330 | if (curr_bytes_written == curr_size || curr_bytes_written == 0) |
| 1331 | break; |
| 1332 | } |
| 1333 | return bytes_written; |
| 1334 | } |
| 1335 | |
| 1336 | size_t |
| 1337 | Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 1338 | { |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1339 | #if defined (ENABLE_MEMORY_CACHING) |
| 1340 | m_memory_cache.Flush (addr, size); |
| 1341 | #endif |
| 1342 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1343 | if (buf == NULL || size == 0) |
| 1344 | return 0; |
| 1345 | // We need to write any data that would go where any current software traps |
| 1346 | // (enabled software breakpoints) any software traps (breakpoints) that we |
| 1347 | // may have placed in our tasks memory. |
| 1348 | |
| 1349 | BreakpointSiteList::collection::const_iterator iter = m_breakpoint_site_list.GetMap()->lower_bound (addr); |
| 1350 | BreakpointSiteList::collection::const_iterator end = m_breakpoint_site_list.GetMap()->end(); |
| 1351 | |
| 1352 | if (iter == end || iter->second->GetLoadAddress() > addr + size) |
| 1353 | return DoWriteMemory(addr, buf, size, error); |
| 1354 | |
| 1355 | BreakpointSiteList::collection::const_iterator pos; |
| 1356 | size_t bytes_written = 0; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1357 | addr_t intersect_addr = 0; |
| 1358 | size_t intersect_size = 0; |
| 1359 | size_t opcode_offset = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1360 | const uint8_t *ubuf = (const uint8_t *)buf; |
| 1361 | |
| 1362 | for (pos = iter; pos != end; ++pos) |
| 1363 | { |
| 1364 | BreakpointSiteSP bp; |
| 1365 | bp = pos->second; |
| 1366 | |
| 1367 | assert(bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset)); |
| 1368 | assert(addr <= intersect_addr && intersect_addr < addr + size); |
| 1369 | assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size); |
| 1370 | assert(opcode_offset + intersect_size <= bp->GetByteSize()); |
| 1371 | |
| 1372 | // Check for bytes before this breakpoint |
| 1373 | const addr_t curr_addr = addr + bytes_written; |
| 1374 | if (intersect_addr > curr_addr) |
| 1375 | { |
| 1376 | // There are some bytes before this breakpoint that we need to |
| 1377 | // just write to memory |
| 1378 | size_t curr_size = intersect_addr - curr_addr; |
| 1379 | size_t curr_bytes_written = WriteMemoryPrivate (curr_addr, |
| 1380 | ubuf + bytes_written, |
| 1381 | curr_size, |
| 1382 | error); |
| 1383 | bytes_written += curr_bytes_written; |
| 1384 | if (curr_bytes_written != curr_size) |
| 1385 | { |
| 1386 | // We weren't able to write all of the requested bytes, we |
| 1387 | // are done looping and will return the number of bytes that |
| 1388 | // we have written so far. |
| 1389 | break; |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | // Now write any bytes that would cover up any software breakpoints |
| 1394 | // directly into the breakpoint opcode buffer |
| 1395 | ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size); |
| 1396 | bytes_written += intersect_size; |
| 1397 | } |
| 1398 | |
| 1399 | // Write any remaining bytes after the last breakpoint if we have any left |
| 1400 | if (bytes_written < size) |
| 1401 | bytes_written += WriteMemoryPrivate (addr + bytes_written, |
| 1402 | ubuf + bytes_written, |
| 1403 | size - bytes_written, |
| 1404 | error); |
| 1405 | |
| 1406 | return bytes_written; |
| 1407 | } |
| 1408 | |
| 1409 | addr_t |
| 1410 | Process::AllocateMemory(size_t size, uint32_t permissions, Error &error) |
| 1411 | { |
| 1412 | // Fixme: we should track the blocks we've allocated, and clean them up... |
| 1413 | // We could even do our own allocator here if that ends up being more efficient. |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 1414 | addr_t allocated_addr = DoAllocateMemory (size, permissions, error); |
| 1415 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1416 | if (log) |
Greg Clayton | b349adc | 2011-01-24 06:30:45 +0000 | [diff] [blame] | 1417 | log->Printf("Process::AllocateMemory(size=%4zu, permissions=%c%c%c) => 0x%16.16llx (m_stop_id = %u)", |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 1418 | size, |
| 1419 | permissions & ePermissionsReadable ? 'r' : '-', |
| 1420 | permissions & ePermissionsWritable ? 'w' : '-', |
| 1421 | permissions & ePermissionsExecutable ? 'x' : '-', |
| 1422 | (uint64_t)allocated_addr, |
| 1423 | m_stop_id); |
| 1424 | return allocated_addr; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | Error |
| 1428 | Process::DeallocateMemory (addr_t ptr) |
| 1429 | { |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 1430 | Error error(DoDeallocateMemory (ptr)); |
| 1431 | |
| 1432 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1433 | if (log) |
| 1434 | log->Printf("Process::DeallocateMemory(addr=0x%16.16llx) => err = %s (m_stop_id = %u)", |
| 1435 | ptr, |
| 1436 | error.AsCString("SUCCESS"), |
| 1437 | m_stop_id); |
| 1438 | return error; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | |
| 1442 | Error |
| 1443 | Process::EnableWatchpoint (WatchpointLocation *watchpoint) |
| 1444 | { |
| 1445 | Error error; |
| 1446 | error.SetErrorString("watchpoints are not supported"); |
| 1447 | return error; |
| 1448 | } |
| 1449 | |
| 1450 | Error |
| 1451 | Process::DisableWatchpoint (WatchpointLocation *watchpoint) |
| 1452 | { |
| 1453 | Error error; |
| 1454 | error.SetErrorString("watchpoints are not supported"); |
| 1455 | return error; |
| 1456 | } |
| 1457 | |
| 1458 | StateType |
| 1459 | Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 1460 | { |
| 1461 | StateType state; |
| 1462 | // Now wait for the process to launch and return control to us, and then |
| 1463 | // call DidLaunch: |
| 1464 | while (1) |
| 1465 | { |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1466 | event_sp.reset(); |
| 1467 | state = WaitForStateChangedEventsPrivate (timeout, event_sp); |
| 1468 | |
| 1469 | if (StateIsStoppedState(state)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1470 | break; |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1471 | |
| 1472 | // If state is invalid, then we timed out |
| 1473 | if (state == eStateInvalid) |
| 1474 | break; |
| 1475 | |
| 1476 | if (event_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1477 | HandlePrivateEvent (event_sp); |
| 1478 | } |
| 1479 | return state; |
| 1480 | } |
| 1481 | |
| 1482 | Error |
| 1483 | Process::Launch |
| 1484 | ( |
| 1485 | char const *argv[], |
| 1486 | char const *envp[], |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 1487 | uint32_t launch_flags, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1488 | const char *stdin_path, |
| 1489 | const char *stdout_path, |
Greg Clayton | de915be | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 1490 | const char *stderr_path, |
| 1491 | const char *working_directory |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1492 | ) |
| 1493 | { |
| 1494 | Error error; |
| 1495 | m_target_triple.Clear(); |
| 1496 | m_abi_sp.reset(); |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 1497 | m_process_input_reader.reset(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1498 | |
| 1499 | Module *exe_module = m_target.GetExecutableModule().get(); |
| 1500 | if (exe_module) |
| 1501 | { |
| 1502 | char exec_file_path[PATH_MAX]; |
| 1503 | exe_module->GetFileSpec().GetPath(exec_file_path, sizeof(exec_file_path)); |
| 1504 | if (exe_module->GetFileSpec().Exists()) |
| 1505 | { |
| 1506 | error = WillLaunch (exe_module); |
| 1507 | if (error.Success()) |
| 1508 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1509 | SetPublicState (eStateLaunching); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1510 | // The args coming in should not contain the application name, the |
| 1511 | // lldb_private::Process class will add this in case the executable |
| 1512 | // gets resolved to a different file than was given on the command |
| 1513 | // line (like when an applicaiton bundle is specified and will |
| 1514 | // resolve to the contained exectuable file, or the file given was |
| 1515 | // a symlink or other file system link that resolves to a different |
| 1516 | // file). |
| 1517 | |
| 1518 | // Get the resolved exectuable path |
| 1519 | |
| 1520 | // Make a new argument vector |
| 1521 | std::vector<const char *> exec_path_plus_argv; |
| 1522 | // Append the resolved executable path |
| 1523 | exec_path_plus_argv.push_back (exec_file_path); |
| 1524 | |
| 1525 | // Push all args if there are any |
| 1526 | if (argv) |
| 1527 | { |
| 1528 | for (int i = 0; argv[i]; ++i) |
| 1529 | exec_path_plus_argv.push_back(argv[i]); |
| 1530 | } |
| 1531 | |
| 1532 | // Push a NULL to terminate the args. |
| 1533 | exec_path_plus_argv.push_back(NULL); |
| 1534 | |
| 1535 | // Now launch using these arguments. |
Greg Clayton | 53d68e7 | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 1536 | error = DoLaunch (exe_module, |
| 1537 | exec_path_plus_argv.empty() ? NULL : &exec_path_plus_argv.front(), |
| 1538 | envp, |
Greg Clayton | 452bf61 | 2010-08-31 18:35:14 +0000 | [diff] [blame] | 1539 | launch_flags, |
Greg Clayton | 53d68e7 | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 1540 | stdin_path, |
| 1541 | stdout_path, |
Greg Clayton | de915be | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 1542 | stderr_path, |
| 1543 | working_directory); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1544 | |
| 1545 | if (error.Fail()) |
| 1546 | { |
| 1547 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 1548 | { |
| 1549 | SetID (LLDB_INVALID_PROCESS_ID); |
| 1550 | const char *error_string = error.AsCString(); |
| 1551 | if (error_string == NULL) |
| 1552 | error_string = "launch failed"; |
| 1553 | SetExitStatus (-1, error_string); |
| 1554 | } |
| 1555 | } |
| 1556 | else |
| 1557 | { |
| 1558 | EventSP event_sp; |
| 1559 | StateType state = WaitForProcessStopPrivate(NULL, event_sp); |
| 1560 | |
| 1561 | if (state == eStateStopped || state == eStateCrashed) |
| 1562 | { |
| 1563 | DidLaunch (); |
| 1564 | |
| 1565 | // This delays passing the stopped event to listeners till DidLaunch gets |
| 1566 | // a chance to complete... |
| 1567 | HandlePrivateEvent (event_sp); |
| 1568 | StartPrivateStateThread (); |
| 1569 | } |
| 1570 | else if (state == eStateExited) |
| 1571 | { |
| 1572 | // We exited while trying to launch somehow. Don't call DidLaunch as that's |
| 1573 | // not likely to work, and return an invalid pid. |
| 1574 | HandlePrivateEvent (event_sp); |
| 1575 | } |
| 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | else |
| 1580 | { |
| 1581 | error.SetErrorStringWithFormat("File doesn't exist: '%s'.\n", exec_file_path); |
| 1582 | } |
| 1583 | } |
| 1584 | return error; |
| 1585 | } |
| 1586 | |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1587 | Process::NextEventAction::EventActionResult |
| 1588 | Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1589 | { |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1590 | StateType state = ProcessEventData::GetStateFromEvent (event_sp.get()); |
| 1591 | switch (state) |
Greg Clayton | c1d3775 | 2010-10-18 01:45:30 +0000 | [diff] [blame] | 1592 | { |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1593 | case eStateRunning: |
| 1594 | return eEventActionRetry; |
| 1595 | |
| 1596 | case eStateStopped: |
| 1597 | case eStateCrashed: |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1598 | { |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1599 | // During attach, prior to sending the eStateStopped event, |
| 1600 | // lldb_private::Process subclasses must set the process must set |
| 1601 | // the new process ID. |
| 1602 | assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID); |
| 1603 | m_process->DidAttach (); |
| 1604 | // Figure out which one is the executable, and set that in our target: |
| 1605 | ModuleList &modules = m_process->GetTarget().GetImages(); |
| 1606 | |
| 1607 | size_t num_modules = modules.GetSize(); |
| 1608 | for (int i = 0; i < num_modules; i++) |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1609 | { |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1610 | ModuleSP module_sp = modules.GetModuleAtIndex(i); |
| 1611 | if (module_sp->IsExecutable()) |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1612 | { |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1613 | ModuleSP exec_module = m_process->GetTarget().GetExecutableModule(); |
| 1614 | if (!exec_module || exec_module != module_sp) |
| 1615 | { |
| 1616 | |
| 1617 | m_process->GetTarget().SetExecutableModule (module_sp, false); |
| 1618 | } |
| 1619 | break; |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1620 | } |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1621 | } |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1622 | return eEventActionSuccess; |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1623 | } |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1624 | |
| 1625 | |
| 1626 | break; |
| 1627 | default: |
| 1628 | case eStateExited: |
| 1629 | case eStateInvalid: |
| 1630 | m_exit_string.assign ("No valid Process"); |
| 1631 | return eEventActionExit; |
| 1632 | break; |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1633 | } |
| 1634 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1635 | |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1636 | Process::NextEventAction::EventActionResult |
| 1637 | Process::AttachCompletionHandler::HandleBeingInterrupted() |
| 1638 | { |
| 1639 | return eEventActionSuccess; |
| 1640 | } |
| 1641 | |
| 1642 | const char * |
| 1643 | Process::AttachCompletionHandler::GetExitString () |
| 1644 | { |
| 1645 | return m_exit_string.c_str(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | Error |
| 1649 | Process::Attach (lldb::pid_t attach_pid) |
| 1650 | { |
| 1651 | |
| 1652 | m_target_triple.Clear(); |
| 1653 | m_abi_sp.reset(); |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 1654 | m_process_input_reader.reset(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1655 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1656 | // Find the process and its architecture. Make sure it matches the architecture |
| 1657 | // of the current Target, and if not adjust it. |
| 1658 | |
| 1659 | ArchSpec attach_spec = GetArchSpecForExistingProcess (attach_pid); |
| 1660 | if (attach_spec != GetTarget().GetArchitecture()) |
| 1661 | { |
| 1662 | // Set the architecture on the target. |
| 1663 | GetTarget().SetArchitecture(attach_spec); |
| 1664 | } |
| 1665 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1666 | Error error (WillAttachToProcessWithID(attach_pid)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1667 | if (error.Success()) |
| 1668 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1669 | SetPublicState (eStateAttaching); |
| 1670 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1671 | error = DoAttachToProcessWithID (attach_pid); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1672 | if (error.Success()) |
| 1673 | { |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1674 | SetNextEventAction(new Process::AttachCompletionHandler(this)); |
| 1675 | StartPrivateStateThread(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1676 | } |
| 1677 | else |
| 1678 | { |
| 1679 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 1680 | { |
| 1681 | SetID (LLDB_INVALID_PROCESS_ID); |
| 1682 | const char *error_string = error.AsCString(); |
| 1683 | if (error_string == NULL) |
| 1684 | error_string = "attach failed"; |
| 1685 | |
| 1686 | SetExitStatus(-1, error_string); |
| 1687 | } |
| 1688 | } |
| 1689 | } |
| 1690 | return error; |
| 1691 | } |
| 1692 | |
| 1693 | Error |
| 1694 | Process::Attach (const char *process_name, bool wait_for_launch) |
| 1695 | { |
| 1696 | m_target_triple.Clear(); |
| 1697 | m_abi_sp.reset(); |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 1698 | m_process_input_reader.reset(); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1699 | |
| 1700 | // Find the process and its architecture. Make sure it matches the architecture |
| 1701 | // of the current Target, and if not adjust it. |
| 1702 | |
Jim Ingham | ea29418 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 1703 | if (!wait_for_launch) |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1704 | { |
Jim Ingham | ea29418 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 1705 | ArchSpec attach_spec = GetArchSpecForExistingProcess (process_name); |
Greg Clayton | c1d3775 | 2010-10-18 01:45:30 +0000 | [diff] [blame] | 1706 | if (attach_spec.IsValid() && attach_spec != GetTarget().GetArchitecture()) |
Jim Ingham | ea29418 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 1707 | { |
| 1708 | // Set the architecture on the target. |
| 1709 | GetTarget().SetArchitecture(attach_spec); |
| 1710 | } |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 1711 | } |
Jim Ingham | ea29418 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 1712 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1713 | Error error (WillAttachToProcessWithName(process_name, wait_for_launch)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1714 | if (error.Success()) |
| 1715 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 1716 | SetPublicState (eStateAttaching); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1717 | error = DoAttachToProcessWithName (process_name, wait_for_launch); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1718 | if (error.Fail()) |
| 1719 | { |
| 1720 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 1721 | { |
| 1722 | SetID (LLDB_INVALID_PROCESS_ID); |
| 1723 | const char *error_string = error.AsCString(); |
| 1724 | if (error_string == NULL) |
| 1725 | error_string = "attach failed"; |
| 1726 | |
| 1727 | SetExitStatus(-1, error_string); |
| 1728 | } |
| 1729 | } |
| 1730 | else |
| 1731 | { |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1732 | SetNextEventAction(new Process::AttachCompletionHandler(this)); |
| 1733 | StartPrivateStateThread(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1734 | } |
| 1735 | } |
| 1736 | return error; |
| 1737 | } |
| 1738 | |
| 1739 | Error |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 1740 | Process::ConnectRemote (const char *remote_url) |
| 1741 | { |
| 1742 | m_target_triple.Clear(); |
| 1743 | m_abi_sp.reset(); |
| 1744 | m_process_input_reader.reset(); |
| 1745 | |
| 1746 | // Find the process and its architecture. Make sure it matches the architecture |
| 1747 | // of the current Target, and if not adjust it. |
| 1748 | |
| 1749 | Error error (DoConnectRemote (remote_url)); |
| 1750 | if (error.Success()) |
| 1751 | { |
| 1752 | SetNextEventAction(new Process::AttachCompletionHandler(this)); |
| 1753 | StartPrivateStateThread(); |
| 1754 | // TimeValue timeout; |
| 1755 | // timeout = TimeValue::Now(); |
| 1756 | // timeout.OffsetWithMicroSeconds(000); |
| 1757 | // EventSP event_sp; |
| 1758 | // StateType state = WaitForProcessStopPrivate(NULL, event_sp); |
| 1759 | // |
| 1760 | // if (state == eStateStopped || state == eStateCrashed) |
| 1761 | // { |
| 1762 | // DidLaunch (); |
| 1763 | // |
| 1764 | // // This delays passing the stopped event to listeners till DidLaunch gets |
| 1765 | // // a chance to complete... |
| 1766 | // HandlePrivateEvent (event_sp); |
| 1767 | // StartPrivateStateThread (); |
| 1768 | // } |
| 1769 | // else if (state == eStateExited) |
| 1770 | // { |
| 1771 | // // We exited while trying to launch somehow. Don't call DidLaunch as that's |
| 1772 | // // not likely to work, and return an invalid pid. |
| 1773 | // HandlePrivateEvent (event_sp); |
| 1774 | // } |
| 1775 | // |
| 1776 | // StartPrivateStateThread(); |
| 1777 | } |
| 1778 | return error; |
| 1779 | } |
| 1780 | |
| 1781 | |
| 1782 | Error |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1783 | Process::Resume () |
| 1784 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1785 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1786 | if (log) |
Jim Ingham | ac95966 | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 1787 | log->Printf("Process::Resume() m_stop_id = %u, public state: %s private state: %s", |
| 1788 | m_stop_id, |
| 1789 | StateAsCString(m_public_state.GetValue()), |
| 1790 | StateAsCString(m_private_state.GetValue())); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1791 | |
| 1792 | Error error (WillResume()); |
| 1793 | // Tell the process it is about to resume before the thread list |
| 1794 | if (error.Success()) |
| 1795 | { |
Johnny Chen | 9c11d47 | 2010-12-02 20:53:05 +0000 | [diff] [blame] | 1796 | // Now let the thread list know we are about to resume so it |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1797 | // can let all of our threads know that they are about to be |
| 1798 | // resumed. Threads will each be called with |
| 1799 | // Thread::WillResume(StateType) where StateType contains the state |
| 1800 | // that they are supposed to have when the process is resumed |
| 1801 | // (suspended/running/stepping). Threads should also check |
| 1802 | // their resume signal in lldb::Thread::GetResumeSignal() |
| 1803 | // to see if they are suppoed to start back up with a signal. |
| 1804 | if (m_thread_list.WillResume()) |
| 1805 | { |
| 1806 | error = DoResume(); |
| 1807 | if (error.Success()) |
| 1808 | { |
| 1809 | DidResume(); |
| 1810 | m_thread_list.DidResume(); |
Jim Ingham | ac95966 | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 1811 | if (log) |
| 1812 | log->Printf ("Process thinks the process has resumed."); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1813 | } |
| 1814 | } |
| 1815 | else |
| 1816 | { |
Jim Ingham | ac95966 | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 1817 | error.SetErrorStringWithFormat("Process::WillResume() thread list returned false after WillResume"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1818 | } |
| 1819 | } |
Jim Ingham | ac95966 | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 1820 | else if (log) |
| 1821 | log->Printf ("Process::WillResume() got an error \"%s\".", error.AsCString("<unknown error>")); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1822 | return error; |
| 1823 | } |
| 1824 | |
| 1825 | Error |
| 1826 | Process::Halt () |
| 1827 | { |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1828 | // Pause our private state thread so we can ensure no one else eats |
| 1829 | // the stop event out from under us. |
| 1830 | PausePrivateStateThread(); |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1831 | |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1832 | EventSP event_sp; |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1833 | Error error (WillHalt()); |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1834 | |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1835 | if (error.Success()) |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1836 | { |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1837 | |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1838 | bool caused_stop = false; |
| 1839 | |
| 1840 | // Ask the process subclass to actually halt our process |
| 1841 | error = DoHalt(caused_stop); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1842 | if (error.Success()) |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1843 | { |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 1844 | if (m_public_state.GetValue() == eStateAttaching) |
| 1845 | { |
| 1846 | SetExitStatus(SIGKILL, "Cancelled async attach."); |
| 1847 | Destroy (); |
| 1848 | } |
| 1849 | else |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1850 | { |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1851 | // If "caused_stop" is true, then DoHalt stopped the process. If |
| 1852 | // "caused_stop" is false, the process was already stopped. |
| 1853 | // If the DoHalt caused the process to stop, then we want to catch |
| 1854 | // this event and set the interrupted bool to true before we pass |
| 1855 | // this along so clients know that the process was interrupted by |
| 1856 | // a halt command. |
| 1857 | if (caused_stop) |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1858 | { |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1859 | // Wait for 2 seconds for the process to stop. |
| 1860 | TimeValue timeout_time; |
| 1861 | timeout_time = TimeValue::Now(); |
| 1862 | timeout_time.OffsetWithSeconds(1); |
| 1863 | StateType state = WaitForStateChangedEventsPrivate (&timeout_time, event_sp); |
| 1864 | |
| 1865 | if (state == eStateInvalid) |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1866 | { |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1867 | // We timeout out and didn't get a stop event... |
| 1868 | error.SetErrorString ("Halt timed out."); |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1869 | } |
| 1870 | else |
| 1871 | { |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1872 | if (StateIsStoppedState (state)) |
| 1873 | { |
| 1874 | // We caused the process to interrupt itself, so mark this |
| 1875 | // as such in the stop event so clients can tell an interrupted |
| 1876 | // process from a natural stop |
| 1877 | ProcessEventData::SetInterruptedInEvent (event_sp.get(), true); |
| 1878 | } |
| 1879 | else |
| 1880 | { |
| 1881 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1882 | if (log) |
| 1883 | log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state)); |
| 1884 | error.SetErrorString ("Did not get stopped event after halt."); |
| 1885 | } |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1886 | } |
| 1887 | } |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1888 | DidHalt(); |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 1889 | } |
| 1890 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1891 | } |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 1892 | // Resume our private state thread before we post the event (if any) |
| 1893 | ResumePrivateStateThread(); |
| 1894 | |
| 1895 | // Post any event we might have consumed. If all goes well, we will have |
| 1896 | // stopped the process, intercepted the event and set the interrupted |
| 1897 | // bool in the event. Post it to the private event queue and that will end up |
| 1898 | // correctly setting the state. |
| 1899 | if (event_sp) |
| 1900 | m_private_state_broadcaster.BroadcastEvent(event_sp); |
| 1901 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1902 | return error; |
| 1903 | } |
| 1904 | |
| 1905 | Error |
| 1906 | Process::Detach () |
| 1907 | { |
| 1908 | Error error (WillDetach()); |
| 1909 | |
| 1910 | if (error.Success()) |
| 1911 | { |
| 1912 | DisableAllBreakpointSites(); |
| 1913 | error = DoDetach(); |
| 1914 | if (error.Success()) |
| 1915 | { |
| 1916 | DidDetach(); |
| 1917 | StopPrivateStateThread(); |
| 1918 | } |
| 1919 | } |
| 1920 | return error; |
| 1921 | } |
| 1922 | |
| 1923 | Error |
| 1924 | Process::Destroy () |
| 1925 | { |
| 1926 | Error error (WillDestroy()); |
| 1927 | if (error.Success()) |
| 1928 | { |
| 1929 | DisableAllBreakpointSites(); |
| 1930 | error = DoDestroy(); |
| 1931 | if (error.Success()) |
| 1932 | { |
| 1933 | DidDestroy(); |
| 1934 | StopPrivateStateThread(); |
| 1935 | } |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 1936 | m_stdio_communication.StopReadThread(); |
| 1937 | m_stdio_communication.Disconnect(); |
| 1938 | if (m_process_input_reader && m_process_input_reader->IsActive()) |
| 1939 | m_target.GetDebugger().PopInputReader (m_process_input_reader); |
| 1940 | if (m_process_input_reader) |
| 1941 | m_process_input_reader.reset(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1942 | } |
| 1943 | return error; |
| 1944 | } |
| 1945 | |
| 1946 | Error |
| 1947 | Process::Signal (int signal) |
| 1948 | { |
| 1949 | Error error (WillSignal()); |
| 1950 | if (error.Success()) |
| 1951 | { |
| 1952 | error = DoSignal(signal); |
| 1953 | if (error.Success()) |
| 1954 | DidSignal(); |
| 1955 | } |
| 1956 | return error; |
| 1957 | } |
| 1958 | |
| 1959 | UnixSignals & |
| 1960 | Process::GetUnixSignals () |
| 1961 | { |
| 1962 | return m_unix_signals; |
| 1963 | } |
| 1964 | |
| 1965 | Target & |
| 1966 | Process::GetTarget () |
| 1967 | { |
| 1968 | return m_target; |
| 1969 | } |
| 1970 | |
| 1971 | const Target & |
| 1972 | Process::GetTarget () const |
| 1973 | { |
| 1974 | return m_target; |
| 1975 | } |
| 1976 | |
| 1977 | uint32_t |
| 1978 | Process::GetAddressByteSize() |
| 1979 | { |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 1980 | if (m_addr_byte_size == 0) |
| 1981 | return m_target.GetArchitecture().GetAddressByteSize(); |
| 1982 | return m_addr_byte_size; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | bool |
| 1986 | Process::ShouldBroadcastEvent (Event *event_ptr) |
| 1987 | { |
| 1988 | const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr); |
| 1989 | bool return_value = true; |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1990 | LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1991 | |
| 1992 | switch (state) |
| 1993 | { |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 1994 | case eStateConnected: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1995 | case eStateAttaching: |
| 1996 | case eStateLaunching: |
| 1997 | case eStateDetached: |
| 1998 | case eStateExited: |
| 1999 | case eStateUnloaded: |
| 2000 | // These events indicate changes in the state of the debugging session, always report them. |
| 2001 | return_value = true; |
| 2002 | break; |
| 2003 | case eStateInvalid: |
| 2004 | // We stopped for no apparent reason, don't report it. |
| 2005 | return_value = false; |
| 2006 | break; |
| 2007 | case eStateRunning: |
| 2008 | case eStateStepping: |
| 2009 | // If we've started the target running, we handle the cases where we |
| 2010 | // are already running and where there is a transition from stopped to |
| 2011 | // running differently. |
| 2012 | // running -> running: Automatically suppress extra running events |
| 2013 | // stopped -> running: Report except when there is one or more no votes |
| 2014 | // and no yes votes. |
| 2015 | SynchronouslyNotifyStateChanged (state); |
| 2016 | switch (m_public_state.GetValue()) |
| 2017 | { |
| 2018 | case eStateRunning: |
| 2019 | case eStateStepping: |
| 2020 | // We always suppress multiple runnings with no PUBLIC stop in between. |
| 2021 | return_value = false; |
| 2022 | break; |
| 2023 | default: |
| 2024 | // TODO: make this work correctly. For now always report |
| 2025 | // run if we aren't running so we don't miss any runnning |
| 2026 | // events. If I run the lldb/test/thread/a.out file and |
| 2027 | // break at main.cpp:58, run and hit the breakpoints on |
| 2028 | // multiple threads, then somehow during the stepping over |
| 2029 | // of all breakpoints no run gets reported. |
| 2030 | return_value = true; |
| 2031 | |
| 2032 | // This is a transition from stop to run. |
| 2033 | switch (m_thread_list.ShouldReportRun (event_ptr)) |
| 2034 | { |
| 2035 | case eVoteYes: |
| 2036 | case eVoteNoOpinion: |
| 2037 | return_value = true; |
| 2038 | break; |
| 2039 | case eVoteNo: |
| 2040 | return_value = false; |
| 2041 | break; |
| 2042 | } |
| 2043 | break; |
| 2044 | } |
| 2045 | break; |
| 2046 | case eStateStopped: |
| 2047 | case eStateCrashed: |
| 2048 | case eStateSuspended: |
| 2049 | { |
| 2050 | // We've stopped. First see if we're going to restart the target. |
| 2051 | // If we are going to stop, then we always broadcast the event. |
| 2052 | // 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] | 2053 | // If no thread has an opinion, we don't report it. |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2054 | if (ProcessEventData::GetInterruptedFromEvent (event_ptr)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2055 | { |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 2056 | if (log) |
| 2057 | 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] | 2058 | return true; |
| 2059 | } |
| 2060 | else |
| 2061 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2062 | RefreshStateAfterStop (); |
| 2063 | |
| 2064 | if (m_thread_list.ShouldStop (event_ptr) == false) |
| 2065 | { |
| 2066 | switch (m_thread_list.ShouldReportStop (event_ptr)) |
| 2067 | { |
| 2068 | case eVoteYes: |
| 2069 | Process::ProcessEventData::SetRestartedInEvent (event_ptr, true); |
Johnny Chen | 028784b | 2010-10-14 00:54:32 +0000 | [diff] [blame] | 2070 | // Intentional fall-through here. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2071 | case eVoteNoOpinion: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2072 | case eVoteNo: |
| 2073 | return_value = false; |
| 2074 | break; |
| 2075 | } |
| 2076 | |
| 2077 | if (log) |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2078 | 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] | 2079 | Resume (); |
| 2080 | } |
| 2081 | else |
| 2082 | { |
| 2083 | return_value = true; |
| 2084 | SynchronouslyNotifyStateChanged (state); |
| 2085 | } |
| 2086 | } |
| 2087 | } |
| 2088 | } |
| 2089 | |
| 2090 | if (log) |
| 2091 | log->Printf ("Process::ShouldBroadcastEvent (%p) => %s", event_ptr, StateAsCString(state), return_value ? "YES" : "NO"); |
| 2092 | return return_value; |
| 2093 | } |
| 2094 | |
| 2095 | //------------------------------------------------------------------ |
| 2096 | // Thread Queries |
| 2097 | //------------------------------------------------------------------ |
| 2098 | |
| 2099 | ThreadList & |
| 2100 | Process::GetThreadList () |
| 2101 | { |
| 2102 | return m_thread_list; |
| 2103 | } |
| 2104 | |
| 2105 | const ThreadList & |
| 2106 | Process::GetThreadList () const |
| 2107 | { |
| 2108 | return m_thread_list; |
| 2109 | } |
| 2110 | |
| 2111 | |
| 2112 | bool |
| 2113 | Process::StartPrivateStateThread () |
| 2114 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 2115 | LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2116 | |
| 2117 | if (log) |
| 2118 | log->Printf ("Process::%s ( )", __FUNCTION__); |
| 2119 | |
| 2120 | // Create a thread that watches our internal state and controls which |
| 2121 | // events make it to clients (into the DCProcess event queue). |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 2122 | char thread_name[1024]; |
| 2123 | snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%i)>", GetID()); |
| 2124 | m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL); |
Greg Clayton | 09c81ef | 2011-02-08 01:34:25 +0000 | [diff] [blame^] | 2125 | return IS_VALID_LLDB_HOST_THREAD(m_private_state_thread); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | void |
| 2129 | Process::PausePrivateStateThread () |
| 2130 | { |
| 2131 | ControlPrivateStateThread (eBroadcastInternalStateControlPause); |
| 2132 | } |
| 2133 | |
| 2134 | void |
| 2135 | Process::ResumePrivateStateThread () |
| 2136 | { |
| 2137 | ControlPrivateStateThread (eBroadcastInternalStateControlResume); |
| 2138 | } |
| 2139 | |
| 2140 | void |
| 2141 | Process::StopPrivateStateThread () |
| 2142 | { |
| 2143 | ControlPrivateStateThread (eBroadcastInternalStateControlStop); |
| 2144 | } |
| 2145 | |
| 2146 | void |
| 2147 | Process::ControlPrivateStateThread (uint32_t signal) |
| 2148 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 2149 | LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2150 | |
| 2151 | assert (signal == eBroadcastInternalStateControlStop || |
| 2152 | signal == eBroadcastInternalStateControlPause || |
| 2153 | signal == eBroadcastInternalStateControlResume); |
| 2154 | |
| 2155 | if (log) |
Greg Clayton | f4fbc0b | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 2156 | log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2157 | |
Greg Clayton | f4fbc0b | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 2158 | // Signal the private state thread. First we should copy this is case the |
| 2159 | // thread starts exiting since the private state thread will NULL this out |
| 2160 | // when it exits |
| 2161 | const lldb::thread_t private_state_thread = m_private_state_thread; |
Greg Clayton | 09c81ef | 2011-02-08 01:34:25 +0000 | [diff] [blame^] | 2162 | if (IS_VALID_LLDB_HOST_THREAD(private_state_thread)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2163 | { |
| 2164 | TimeValue timeout_time; |
| 2165 | bool timed_out; |
| 2166 | |
| 2167 | m_private_state_control_broadcaster.BroadcastEvent (signal, NULL); |
| 2168 | |
| 2169 | timeout_time = TimeValue::Now(); |
| 2170 | timeout_time.OffsetWithSeconds(2); |
| 2171 | m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out); |
| 2172 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
| 2173 | |
| 2174 | if (signal == eBroadcastInternalStateControlStop) |
| 2175 | { |
| 2176 | if (timed_out) |
Greg Clayton | f4fbc0b | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 2177 | Host::ThreadCancel (private_state_thread, NULL); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2178 | |
| 2179 | thread_result_t result = NULL; |
Greg Clayton | f4fbc0b | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 2180 | Host::ThreadJoin (private_state_thread, &result, NULL); |
Greg Clayton | c607d86 | 2010-07-22 18:34:21 +0000 | [diff] [blame] | 2181 | m_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2182 | } |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | void |
| 2187 | Process::HandlePrivateEvent (EventSP &event_sp) |
| 2188 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 2189 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2190 | |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2191 | const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2192 | |
| 2193 | // First check to see if anybody wants a shot at this event: |
Jim Ingham | 68bffc5 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 2194 | if (m_next_event_action_ap.get() != NULL) |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2195 | { |
Jim Ingham | 68bffc5 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 2196 | NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp); |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2197 | switch (action_result) |
| 2198 | { |
| 2199 | case NextEventAction::eEventActionSuccess: |
| 2200 | SetNextEventAction(NULL); |
| 2201 | break; |
| 2202 | case NextEventAction::eEventActionRetry: |
| 2203 | break; |
| 2204 | case NextEventAction::eEventActionExit: |
Jim Ingham | 84c8638 | 2011-01-29 01:57:31 +0000 | [diff] [blame] | 2205 | // Handle Exiting Here. If we already got an exited event, |
| 2206 | // we should just propagate it. Otherwise, swallow this event, |
| 2207 | // and set our state to exit so the next event will kill us. |
| 2208 | if (new_state != eStateExited) |
| 2209 | { |
| 2210 | // FIXME: should cons up an exited event, and discard this one. |
Jim Ingham | 68bffc5 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 2211 | SetExitStatus(0, m_next_event_action_ap->GetExitString()); |
Jim Ingham | 84c8638 | 2011-01-29 01:57:31 +0000 | [diff] [blame] | 2212 | SetNextEventAction(NULL); |
| 2213 | return; |
| 2214 | } |
| 2215 | SetNextEventAction(NULL); |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2216 | break; |
| 2217 | } |
| 2218 | } |
| 2219 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2220 | // See if we should broadcast this state to external clients? |
| 2221 | const bool should_broadcast = ShouldBroadcastEvent (event_sp.get()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2222 | |
| 2223 | if (should_broadcast) |
| 2224 | { |
| 2225 | if (log) |
| 2226 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2227 | log->Printf ("Process::%s (pid = %i) broadcasting new state %s (old state %s) to %s", |
| 2228 | __FUNCTION__, |
| 2229 | GetID(), |
| 2230 | StateAsCString(new_state), |
| 2231 | StateAsCString (GetState ()), |
| 2232 | IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2233 | } |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2234 | if (StateIsRunningState (new_state)) |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 2235 | PushProcessInputReader (); |
| 2236 | else |
| 2237 | PopProcessInputReader (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2238 | Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get()); |
| 2239 | BroadcastEvent (event_sp); |
| 2240 | } |
| 2241 | else |
| 2242 | { |
| 2243 | if (log) |
| 2244 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2245 | log->Printf ("Process::%s (pid = %i) suppressing state %s (old state %s): should_broadcast == false", |
| 2246 | __FUNCTION__, |
| 2247 | GetID(), |
| 2248 | StateAsCString(new_state), |
| 2249 | StateAsCString (GetState ()), |
| 2250 | IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2251 | } |
| 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | void * |
| 2256 | Process::PrivateStateThread (void *arg) |
| 2257 | { |
| 2258 | Process *proc = static_cast<Process*> (arg); |
| 2259 | void *result = proc->RunPrivateStateThread (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2260 | return result; |
| 2261 | } |
| 2262 | |
| 2263 | void * |
| 2264 | Process::RunPrivateStateThread () |
| 2265 | { |
| 2266 | bool control_only = false; |
| 2267 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
| 2268 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 2269 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2270 | if (log) |
| 2271 | log->Printf ("Process::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, this, GetID()); |
| 2272 | |
| 2273 | bool exit_now = false; |
| 2274 | while (!exit_now) |
| 2275 | { |
| 2276 | EventSP event_sp; |
| 2277 | WaitForEventsPrivate (NULL, event_sp, control_only); |
| 2278 | if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster)) |
| 2279 | { |
| 2280 | switch (event_sp->GetType()) |
| 2281 | { |
| 2282 | case eBroadcastInternalStateControlStop: |
| 2283 | exit_now = true; |
| 2284 | continue; // Go to next loop iteration so we exit without |
| 2285 | break; // doing any internal state managment below |
| 2286 | |
| 2287 | case eBroadcastInternalStateControlPause: |
| 2288 | control_only = true; |
| 2289 | break; |
| 2290 | |
| 2291 | case eBroadcastInternalStateControlResume: |
| 2292 | control_only = false; |
| 2293 | break; |
| 2294 | } |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2295 | |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2296 | if (log) |
| 2297 | log->Printf ("Process::%s (arg = %p, pid = %i) got a control event: %d", __FUNCTION__, this, GetID(), event_sp->GetType()); |
| 2298 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2299 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2300 | continue; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | |
| 2304 | const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 2305 | |
| 2306 | if (internal_state != eStateInvalid) |
| 2307 | { |
| 2308 | HandlePrivateEvent (event_sp); |
| 2309 | } |
| 2310 | |
Greg Clayton | 3b2c41c | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 2311 | if (internal_state == eStateInvalid || |
| 2312 | internal_state == eStateExited || |
| 2313 | internal_state == eStateDetached ) |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2314 | { |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2315 | if (log) |
| 2316 | log->Printf ("Process::%s (arg = %p, pid = %i) about to exit with internal state %s...", __FUNCTION__, this, GetID(), StateAsCString(internal_state)); |
| 2317 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2318 | break; |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2319 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 2322 | // Verify log is still enabled before attempting to write to it... |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2323 | if (log) |
| 2324 | log->Printf ("Process::%s (arg = %p, pid = %i) thread exiting...", __FUNCTION__, this, GetID()); |
| 2325 | |
Greg Clayton | a4881d0 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 2326 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
| 2327 | m_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2328 | return NULL; |
| 2329 | } |
| 2330 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2331 | //------------------------------------------------------------------ |
| 2332 | // Process Event Data |
| 2333 | //------------------------------------------------------------------ |
| 2334 | |
| 2335 | Process::ProcessEventData::ProcessEventData () : |
| 2336 | EventData (), |
| 2337 | m_process_sp (), |
| 2338 | m_state (eStateInvalid), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 2339 | m_restarted (false), |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2340 | m_update_state (false), |
| 2341 | m_interrupted (false) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2342 | { |
| 2343 | } |
| 2344 | |
| 2345 | Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) : |
| 2346 | EventData (), |
| 2347 | m_process_sp (process_sp), |
| 2348 | m_state (state), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 2349 | m_restarted (false), |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2350 | m_update_state (false), |
| 2351 | m_interrupted (false) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2352 | { |
| 2353 | } |
| 2354 | |
| 2355 | Process::ProcessEventData::~ProcessEventData() |
| 2356 | { |
| 2357 | } |
| 2358 | |
| 2359 | const ConstString & |
| 2360 | Process::ProcessEventData::GetFlavorString () |
| 2361 | { |
| 2362 | static ConstString g_flavor ("Process::ProcessEventData"); |
| 2363 | return g_flavor; |
| 2364 | } |
| 2365 | |
| 2366 | const ConstString & |
| 2367 | Process::ProcessEventData::GetFlavor () const |
| 2368 | { |
| 2369 | return ProcessEventData::GetFlavorString (); |
| 2370 | } |
| 2371 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2372 | void |
| 2373 | Process::ProcessEventData::DoOnRemoval (Event *event_ptr) |
| 2374 | { |
| 2375 | // This function gets called twice for each event, once when the event gets pulled |
| 2376 | // off of the private process event queue, and once when it gets pulled off of |
| 2377 | // the public event queue. m_update_state is used to distinguish these |
| 2378 | // two cases; it is false when we're just pulling it off for private handling, |
| 2379 | // and we don't want to do the breakpoint command handling then. |
| 2380 | |
| 2381 | if (!m_update_state) |
| 2382 | return; |
| 2383 | |
| 2384 | m_process_sp->SetPublicState (m_state); |
| 2385 | |
| 2386 | // If we're stopped and haven't restarted, then do the breakpoint commands here: |
| 2387 | if (m_state == eStateStopped && ! m_restarted) |
| 2388 | { |
| 2389 | int num_threads = m_process_sp->GetThreadList().GetSize(); |
| 2390 | int idx; |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 2391 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2392 | for (idx = 0; idx < num_threads; ++idx) |
| 2393 | { |
| 2394 | lldb::ThreadSP thread_sp = m_process_sp->GetThreadList().GetThreadAtIndex(idx); |
| 2395 | |
Jim Ingham | 6297a3a | 2010-10-20 00:39:53 +0000 | [diff] [blame] | 2396 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo (); |
| 2397 | if (stop_info_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2398 | { |
Jim Ingham | 6297a3a | 2010-10-20 00:39:53 +0000 | [diff] [blame] | 2399 | stop_info_sp->PerformAction(event_ptr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2400 | } |
| 2401 | } |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 2402 | |
Jim Ingham | 6fb8baa | 2010-08-10 00:59:59 +0000 | [diff] [blame] | 2403 | // The stop action might restart the target. If it does, then we want to mark that in the |
| 2404 | // event so that whoever is receiving it will know to wait for the running event and reflect |
| 2405 | // that state appropriately. |
| 2406 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2407 | if (m_process_sp->GetPrivateState() == eStateRunning) |
| 2408 | SetRestarted(true); |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | void |
| 2413 | Process::ProcessEventData::Dump (Stream *s) const |
| 2414 | { |
| 2415 | if (m_process_sp) |
| 2416 | s->Printf(" process = %p (pid = %u), ", m_process_sp.get(), m_process_sp->GetID()); |
| 2417 | |
| 2418 | s->Printf("state = %s", StateAsCString(GetState()));; |
| 2419 | } |
| 2420 | |
| 2421 | const Process::ProcessEventData * |
| 2422 | Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr) |
| 2423 | { |
| 2424 | if (event_ptr) |
| 2425 | { |
| 2426 | const EventData *event_data = event_ptr->GetData(); |
| 2427 | if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString()) |
| 2428 | return static_cast <const ProcessEventData *> (event_ptr->GetData()); |
| 2429 | } |
| 2430 | return NULL; |
| 2431 | } |
| 2432 | |
| 2433 | ProcessSP |
| 2434 | Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr) |
| 2435 | { |
| 2436 | ProcessSP process_sp; |
| 2437 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 2438 | if (data) |
| 2439 | process_sp = data->GetProcessSP(); |
| 2440 | return process_sp; |
| 2441 | } |
| 2442 | |
| 2443 | StateType |
| 2444 | Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr) |
| 2445 | { |
| 2446 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 2447 | if (data == NULL) |
| 2448 | return eStateInvalid; |
| 2449 | else |
| 2450 | return data->GetState(); |
| 2451 | } |
| 2452 | |
| 2453 | bool |
| 2454 | Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr) |
| 2455 | { |
| 2456 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 2457 | if (data == NULL) |
| 2458 | return false; |
| 2459 | else |
| 2460 | return data->GetRestarted(); |
| 2461 | } |
| 2462 | |
| 2463 | void |
| 2464 | Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value) |
| 2465 | { |
| 2466 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 2467 | if (data != NULL) |
| 2468 | data->SetRestarted(new_value); |
| 2469 | } |
| 2470 | |
| 2471 | bool |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2472 | Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr) |
| 2473 | { |
| 2474 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 2475 | if (data == NULL) |
| 2476 | return false; |
| 2477 | else |
| 2478 | return data->GetInterrupted (); |
| 2479 | } |
| 2480 | |
| 2481 | void |
| 2482 | Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value) |
| 2483 | { |
| 2484 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 2485 | if (data != NULL) |
| 2486 | data->SetInterrupted(new_value); |
| 2487 | } |
| 2488 | |
| 2489 | bool |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2490 | Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr) |
| 2491 | { |
| 2492 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 2493 | if (data) |
| 2494 | { |
| 2495 | data->SetUpdateStateOnRemoval(); |
| 2496 | return true; |
| 2497 | } |
| 2498 | return false; |
| 2499 | } |
| 2500 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2501 | Target * |
| 2502 | Process::CalculateTarget () |
| 2503 | { |
| 2504 | return &m_target; |
| 2505 | } |
| 2506 | |
| 2507 | Process * |
| 2508 | Process::CalculateProcess () |
| 2509 | { |
| 2510 | return this; |
| 2511 | } |
| 2512 | |
| 2513 | Thread * |
| 2514 | Process::CalculateThread () |
| 2515 | { |
| 2516 | return NULL; |
| 2517 | } |
| 2518 | |
| 2519 | StackFrame * |
| 2520 | Process::CalculateStackFrame () |
| 2521 | { |
| 2522 | return NULL; |
| 2523 | } |
| 2524 | |
| 2525 | void |
Greg Clayton | a830adb | 2010-10-04 01:05:56 +0000 | [diff] [blame] | 2526 | Process::CalculateExecutionContext (ExecutionContext &exe_ctx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2527 | { |
| 2528 | exe_ctx.target = &m_target; |
| 2529 | exe_ctx.process = this; |
| 2530 | exe_ctx.thread = NULL; |
| 2531 | exe_ctx.frame = NULL; |
| 2532 | } |
| 2533 | |
| 2534 | lldb::ProcessSP |
| 2535 | Process::GetSP () |
| 2536 | { |
| 2537 | return GetTarget().GetProcessSP(); |
| 2538 | } |
| 2539 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 2540 | uint32_t |
| 2541 | Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) |
| 2542 | { |
| 2543 | return 0; |
| 2544 | } |
| 2545 | |
| 2546 | ArchSpec |
| 2547 | Process::GetArchSpecForExistingProcess (lldb::pid_t pid) |
| 2548 | { |
| 2549 | return Host::GetArchSpecForExistingProcess (pid); |
| 2550 | } |
| 2551 | |
| 2552 | ArchSpec |
| 2553 | Process::GetArchSpecForExistingProcess (const char *process_name) |
| 2554 | { |
| 2555 | return Host::GetArchSpecForExistingProcess (process_name); |
| 2556 | } |
| 2557 | |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 2558 | void |
| 2559 | Process::AppendSTDOUT (const char * s, size_t len) |
| 2560 | { |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 2561 | Mutex::Locker locker (m_stdio_communication_mutex); |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 2562 | m_stdout_data.append (s, len); |
| 2563 | |
Greg Clayton | b378133 | 2010-12-05 19:16:56 +0000 | [diff] [blame] | 2564 | BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (GetTarget().GetProcessSP(), GetState())); |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
| 2567 | void |
| 2568 | Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len) |
| 2569 | { |
| 2570 | Process *process = (Process *) baton; |
| 2571 | process->AppendSTDOUT (static_cast<const char *>(src), src_len); |
| 2572 | } |
| 2573 | |
| 2574 | size_t |
| 2575 | Process::ProcessInputReaderCallback (void *baton, |
| 2576 | InputReader &reader, |
| 2577 | lldb::InputReaderAction notification, |
| 2578 | const char *bytes, |
| 2579 | size_t bytes_len) |
| 2580 | { |
| 2581 | Process *process = (Process *) baton; |
| 2582 | |
| 2583 | switch (notification) |
| 2584 | { |
| 2585 | case eInputReaderActivate: |
| 2586 | break; |
| 2587 | |
| 2588 | case eInputReaderDeactivate: |
| 2589 | break; |
| 2590 | |
| 2591 | case eInputReaderReactivate: |
| 2592 | break; |
| 2593 | |
| 2594 | case eInputReaderGotToken: |
| 2595 | { |
| 2596 | Error error; |
| 2597 | process->PutSTDIN (bytes, bytes_len, error); |
| 2598 | } |
| 2599 | break; |
| 2600 | |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 2601 | case eInputReaderInterrupt: |
| 2602 | process->Halt (); |
| 2603 | break; |
| 2604 | |
| 2605 | case eInputReaderEndOfFile: |
| 2606 | process->AppendSTDOUT ("^D", 2); |
| 2607 | break; |
| 2608 | |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 2609 | case eInputReaderDone: |
| 2610 | break; |
| 2611 | |
| 2612 | } |
| 2613 | |
| 2614 | return bytes_len; |
| 2615 | } |
| 2616 | |
| 2617 | void |
| 2618 | Process::ResetProcessInputReader () |
| 2619 | { |
| 2620 | m_process_input_reader.reset(); |
| 2621 | } |
| 2622 | |
| 2623 | void |
| 2624 | Process::SetUpProcessInputReader (int file_descriptor) |
| 2625 | { |
| 2626 | // First set up the Read Thread for reading/handling process I/O |
| 2627 | |
| 2628 | std::auto_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (file_descriptor, true)); |
| 2629 | |
| 2630 | if (conn_ap.get()) |
| 2631 | { |
| 2632 | m_stdio_communication.SetConnection (conn_ap.release()); |
| 2633 | if (m_stdio_communication.IsConnected()) |
| 2634 | { |
| 2635 | m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this); |
| 2636 | m_stdio_communication.StartReadThread(); |
| 2637 | |
| 2638 | // Now read thread is set up, set up input reader. |
| 2639 | |
| 2640 | if (!m_process_input_reader.get()) |
| 2641 | { |
| 2642 | m_process_input_reader.reset (new InputReader(m_target.GetDebugger())); |
| 2643 | Error err (m_process_input_reader->Initialize (Process::ProcessInputReaderCallback, |
| 2644 | this, |
| 2645 | eInputReaderGranularityByte, |
| 2646 | NULL, |
| 2647 | NULL, |
| 2648 | false)); |
| 2649 | |
| 2650 | if (err.Fail()) |
| 2651 | m_process_input_reader.reset(); |
| 2652 | } |
| 2653 | } |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | void |
| 2658 | Process::PushProcessInputReader () |
| 2659 | { |
| 2660 | if (m_process_input_reader && !m_process_input_reader->IsActive()) |
| 2661 | m_target.GetDebugger().PushInputReader (m_process_input_reader); |
| 2662 | } |
| 2663 | |
| 2664 | void |
| 2665 | Process::PopProcessInputReader () |
| 2666 | { |
| 2667 | if (m_process_input_reader && m_process_input_reader->IsActive()) |
| 2668 | m_target.GetDebugger().PopInputReader (m_process_input_reader); |
| 2669 | } |
| 2670 | |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 2671 | |
| 2672 | void |
| 2673 | Process::Initialize () |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2674 | { |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 2675 | UserSettingsControllerSP &usc = GetSettingsController(); |
| 2676 | usc.reset (new SettingsController); |
| 2677 | UserSettingsController::InitializeSettingsController (usc, |
| 2678 | SettingsController::global_settings_table, |
| 2679 | SettingsController::instance_settings_table); |
| 2680 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2681 | |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 2682 | void |
| 2683 | Process::Terminate () |
| 2684 | { |
| 2685 | UserSettingsControllerSP &usc = GetSettingsController(); |
| 2686 | UserSettingsController::FinalizeSettingsController (usc); |
| 2687 | usc.reset(); |
| 2688 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2689 | |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 2690 | UserSettingsControllerSP & |
| 2691 | Process::GetSettingsController () |
| 2692 | { |
| 2693 | static UserSettingsControllerSP g_settings_controller; |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 2694 | return g_settings_controller; |
| 2695 | } |
| 2696 | |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 2697 | void |
| 2698 | Process::UpdateInstanceName () |
| 2699 | { |
| 2700 | ModuleSP module_sp = GetTarget().GetExecutableModule(); |
| 2701 | if (module_sp) |
| 2702 | { |
| 2703 | StreamString sstr; |
| 2704 | sstr.Printf ("%s", module_sp->GetFileSpec().GetFilename().AsCString()); |
| 2705 | |
Greg Clayton | c0c1b0c | 2010-11-19 03:46:01 +0000 | [diff] [blame] | 2706 | GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 2707 | sstr.GetData()); |
| 2708 | } |
| 2709 | } |
| 2710 | |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 2711 | ExecutionResults |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2712 | Process::RunThreadPlan (ExecutionContext &exe_ctx, |
| 2713 | lldb::ThreadPlanSP &thread_plan_sp, |
| 2714 | bool stop_others, |
| 2715 | bool try_all_threads, |
| 2716 | bool discard_on_error, |
| 2717 | uint32_t single_thread_timeout_usec, |
| 2718 | Stream &errors) |
| 2719 | { |
| 2720 | ExecutionResults return_value = eExecutionSetupError; |
| 2721 | |
Jim Ingham | 15dcb7c | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 2722 | if (thread_plan_sp.get() == NULL) |
| 2723 | { |
| 2724 | errors.Printf("RunThreadPlan called with empty thread plan."); |
| 2725 | return lldb::eExecutionSetupError; |
| 2726 | } |
| 2727 | |
Jim Ingham | ac95966 | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 2728 | if (m_private_state.GetValue() != eStateStopped) |
| 2729 | { |
| 2730 | errors.Printf ("RunThreadPlan called while the private state was not stopped."); |
| 2731 | // REMOVE BEAR TRAP... |
| 2732 | // abort(); |
| 2733 | } |
| 2734 | |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2735 | // Save this value for restoration of the execution context after we run |
| 2736 | uint32_t tid = exe_ctx.thread->GetIndexID(); |
| 2737 | |
| 2738 | // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either, |
| 2739 | // so we should arrange to reset them as well. |
| 2740 | |
| 2741 | lldb::ThreadSP selected_thread_sp = exe_ctx.process->GetThreadList().GetSelectedThread(); |
| 2742 | lldb::StackFrameSP selected_frame_sp; |
| 2743 | |
| 2744 | uint32_t selected_tid; |
| 2745 | if (selected_thread_sp != NULL) |
| 2746 | { |
| 2747 | selected_tid = selected_thread_sp->GetIndexID(); |
| 2748 | selected_frame_sp = selected_thread_sp->GetSelectedFrame(); |
| 2749 | } |
| 2750 | else |
| 2751 | { |
| 2752 | selected_tid = LLDB_INVALID_THREAD_ID; |
| 2753 | } |
| 2754 | |
| 2755 | exe_ctx.thread->QueueThreadPlan(thread_plan_sp, true); |
| 2756 | |
Jim Ingham | 6ae318c | 2011-01-23 21:14:08 +0000 | [diff] [blame] | 2757 | Listener listener("lldb.process.listener.run-thread-plan"); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2758 | exe_ctx.process->HijackProcessEvents(&listener); |
Jim Ingham | ac95966 | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 2759 | |
Jim Ingham | 6ae318c | 2011-01-23 21:14:08 +0000 | [diff] [blame] | 2760 | lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 15dcb7c | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 2761 | if (log) |
| 2762 | { |
| 2763 | StreamString s; |
| 2764 | thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2765 | log->Printf ("Process::RunThreadPlan(): Resuming thread %u - 0x%4.4x to run thread plan \"%s\".", exe_ctx.thread->GetIndexID(), exe_ctx.thread->GetID(), s.GetData()); |
Jim Ingham | 15dcb7c | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2768 | Error resume_error = exe_ctx.process->Resume (); |
| 2769 | if (!resume_error.Success()) |
| 2770 | { |
| 2771 | errors.Printf("Error resuming inferior: \"%s\".\n", resume_error.AsCString()); |
| 2772 | exe_ctx.process->RestoreProcessEvents(); |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 2773 | return lldb::eExecutionSetupError; |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | // We need to call the function synchronously, so spin waiting for it to return. |
| 2777 | // If we get interrupted while executing, we're going to lose our context, and |
| 2778 | // won't be able to gather the result at this point. |
| 2779 | // We set the timeout AFTER the resume, since the resume takes some time and we |
| 2780 | // don't want to charge that to the timeout. |
| 2781 | |
| 2782 | TimeValue* timeout_ptr = NULL; |
| 2783 | TimeValue real_timeout; |
| 2784 | |
| 2785 | if (single_thread_timeout_usec != 0) |
| 2786 | { |
| 2787 | real_timeout = TimeValue::Now(); |
| 2788 | real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec); |
| 2789 | timeout_ptr = &real_timeout; |
| 2790 | } |
| 2791 | |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2792 | while (1) |
| 2793 | { |
| 2794 | lldb::EventSP event_sp; |
| 2795 | lldb::StateType stop_state = lldb::eStateInvalid; |
| 2796 | // Now wait for the process to stop again: |
| 2797 | bool got_event = listener.WaitForEvent (timeout_ptr, event_sp); |
| 2798 | |
| 2799 | if (!got_event) |
| 2800 | { |
| 2801 | // Right now this is the only way to tell we've timed out... |
| 2802 | // We should interrupt the process here... |
| 2803 | // Not really sure what to do if Halt fails here... |
Stephen Wilson | c2b9825 | 2011-01-12 04:20:03 +0000 | [diff] [blame] | 2804 | if (log) { |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2805 | if (try_all_threads) |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2806 | log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, trying with all threads enabled.", |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2807 | single_thread_timeout_usec); |
| 2808 | else |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2809 | log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, abandoning execution.", |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2810 | single_thread_timeout_usec); |
Stephen Wilson | c2b9825 | 2011-01-12 04:20:03 +0000 | [diff] [blame] | 2811 | } |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2812 | |
Jim Ingham | c556b46 | 2011-01-22 01:30:53 +0000 | [diff] [blame] | 2813 | Error halt_error = exe_ctx.process->Halt(); |
| 2814 | |
| 2815 | if (halt_error.Success()) |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2816 | { |
| 2817 | timeout_ptr = NULL; |
| 2818 | if (log) |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2819 | log->Printf ("Process::RunThreadPlan(): Halt succeeded."); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2820 | |
| 2821 | // Between the time that we got the timeout and the time we halted, but target |
| 2822 | // might have actually completed the plan. If so, we're done. Note, I call WFE here with a short |
| 2823 | // timeout to |
| 2824 | got_event = listener.WaitForEvent(NULL, event_sp); |
| 2825 | |
| 2826 | if (got_event) |
| 2827 | { |
| 2828 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 2829 | if (log) |
| 2830 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2831 | log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state)); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2832 | if (stop_state == lldb::eStateStopped && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get())) |
| 2833 | log->Printf (" Event was the Halt interruption event."); |
| 2834 | } |
| 2835 | |
| 2836 | if (exe_ctx.thread->IsThreadPlanDone (thread_plan_sp.get())) |
| 2837 | { |
| 2838 | if (log) |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2839 | log->Printf ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. Exiting wait loop."); |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 2840 | return_value = lldb::eExecutionCompleted; |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2841 | break; |
| 2842 | } |
| 2843 | |
| 2844 | if (try_all_threads |
| 2845 | && (stop_state == lldb::eStateStopped && Process::ProcessEventData::GetInterruptedFromEvent (event_sp.get()))) |
| 2846 | { |
| 2847 | |
| 2848 | thread_plan_sp->SetStopOthers (false); |
| 2849 | if (log) |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2850 | log->Printf ("Process::RunThreadPlan(): About to resume."); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2851 | |
| 2852 | exe_ctx.process->Resume(); |
| 2853 | continue; |
| 2854 | } |
| 2855 | else |
| 2856 | { |
| 2857 | exe_ctx.process->RestoreProcessEvents (); |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 2858 | return lldb::eExecutionInterrupted; |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2859 | } |
| 2860 | } |
| 2861 | } |
Jim Ingham | c556b46 | 2011-01-22 01:30:53 +0000 | [diff] [blame] | 2862 | else |
| 2863 | { |
| 2864 | |
| 2865 | if (log) |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2866 | log->Printf ("Process::RunThreadPlan(): halt failed: error = \"%s\", I'm just going to wait a little longer and see if the world gets nicer to me.", |
Jim Ingham | c556b46 | 2011-01-22 01:30:53 +0000 | [diff] [blame] | 2867 | halt_error.AsCString()); |
Jim Ingham | ac95966 | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 2868 | // abort(); |
Jim Ingham | c556b46 | 2011-01-22 01:30:53 +0000 | [diff] [blame] | 2869 | |
Jim Ingham | 6ae318c | 2011-01-23 21:14:08 +0000 | [diff] [blame] | 2870 | if (single_thread_timeout_usec != 0) |
| 2871 | { |
| 2872 | real_timeout = TimeValue::Now(); |
| 2873 | real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec); |
| 2874 | timeout_ptr = &real_timeout; |
| 2875 | } |
| 2876 | continue; |
Jim Ingham | c556b46 | 2011-01-22 01:30:53 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 2882 | if (log) |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2883 | log->Printf("Process::RunThreadPlan(): got event: %s.", StateAsCString(stop_state)); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2884 | |
| 2885 | if (stop_state == lldb::eStateRunning || stop_state == lldb::eStateStepping) |
| 2886 | continue; |
| 2887 | |
| 2888 | if (exe_ctx.thread->IsThreadPlanDone (thread_plan_sp.get())) |
| 2889 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2890 | if (log) |
| 2891 | log->Printf("Process::RunThreadPlan(): thread plan is done"); |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 2892 | return_value = lldb::eExecutionCompleted; |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2893 | break; |
| 2894 | } |
| 2895 | else if (exe_ctx.thread->WasThreadPlanDiscarded (thread_plan_sp.get())) |
| 2896 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2897 | if (log) |
| 2898 | log->Printf("Process::RunThreadPlan(): thread plan was discarded"); |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 2899 | return_value = lldb::eExecutionDiscarded; |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2900 | break; |
| 2901 | } |
| 2902 | else |
| 2903 | { |
| 2904 | if (log) |
| 2905 | { |
| 2906 | StreamString s; |
Jim Ingham | c556b46 | 2011-01-22 01:30:53 +0000 | [diff] [blame] | 2907 | if (event_sp) |
| 2908 | event_sp->Dump (&s); |
| 2909 | else |
| 2910 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2911 | log->Printf ("Process::RunThreadPlan(): Stop event that interrupted us is NULL."); |
Jim Ingham | c556b46 | 2011-01-22 01:30:53 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2914 | StreamString ts; |
| 2915 | |
| 2916 | const char *event_explanation; |
| 2917 | |
| 2918 | do |
| 2919 | { |
| 2920 | const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get()); |
| 2921 | |
| 2922 | if (!event_data) |
| 2923 | { |
| 2924 | event_explanation = "<no event data>"; |
| 2925 | break; |
| 2926 | } |
| 2927 | |
| 2928 | Process *process = event_data->GetProcessSP().get(); |
| 2929 | |
| 2930 | if (!process) |
| 2931 | { |
| 2932 | event_explanation = "<no process>"; |
| 2933 | break; |
| 2934 | } |
| 2935 | |
| 2936 | ThreadList &thread_list = process->GetThreadList(); |
| 2937 | |
| 2938 | uint32_t num_threads = thread_list.GetSize(); |
| 2939 | uint32_t thread_index; |
| 2940 | |
| 2941 | ts.Printf("<%u threads> ", num_threads); |
| 2942 | |
| 2943 | for (thread_index = 0; |
| 2944 | thread_index < num_threads; |
| 2945 | ++thread_index) |
| 2946 | { |
| 2947 | Thread *thread = thread_list.GetThreadAtIndex(thread_index).get(); |
| 2948 | |
| 2949 | if (!thread) |
| 2950 | { |
| 2951 | ts.Printf("<?> "); |
| 2952 | continue; |
| 2953 | } |
| 2954 | |
Jim Ingham | c556b46 | 2011-01-22 01:30:53 +0000 | [diff] [blame] | 2955 | ts.Printf("<0x%4.4x ", thread->GetID()); |
Greg Clayton | 08d7d3a | 2011-01-06 22:15:06 +0000 | [diff] [blame] | 2956 | RegisterContext *register_context = thread->GetRegisterContext().get(); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2957 | |
| 2958 | if (register_context) |
| 2959 | ts.Printf("[ip 0x%llx] ", register_context->GetPC()); |
| 2960 | else |
| 2961 | ts.Printf("[ip unknown] "); |
| 2962 | |
| 2963 | lldb::StopInfoSP stop_info_sp = thread->GetStopInfo(); |
| 2964 | if (stop_info_sp) |
| 2965 | { |
| 2966 | const char *stop_desc = stop_info_sp->GetDescription(); |
| 2967 | if (stop_desc) |
| 2968 | ts.PutCString (stop_desc); |
| 2969 | } |
| 2970 | ts.Printf(">"); |
| 2971 | } |
| 2972 | |
| 2973 | event_explanation = ts.GetData(); |
| 2974 | } while (0); |
| 2975 | |
Jim Ingham | c556b46 | 2011-01-22 01:30:53 +0000 | [diff] [blame] | 2976 | // See if any of the threads that stopped think we ought to stop. Otherwise continue on. |
| 2977 | if (!GetThreadList().ShouldStop(event_sp.get())) |
| 2978 | { |
| 2979 | if (log) |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2980 | log->Printf("Process::RunThreadPlan(): execution interrupted, but nobody wanted to stop, so we continued: %s %s", |
Jim Ingham | c556b46 | 2011-01-22 01:30:53 +0000 | [diff] [blame] | 2981 | s.GetData(), event_explanation); |
| 2982 | if (single_thread_timeout_usec != 0) |
| 2983 | { |
| 2984 | real_timeout = TimeValue::Now(); |
| 2985 | real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec); |
| 2986 | timeout_ptr = &real_timeout; |
| 2987 | } |
| 2988 | |
| 2989 | continue; |
| 2990 | } |
| 2991 | else |
| 2992 | { |
| 2993 | if (log) |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 2994 | log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation); |
Jim Ingham | c556b46 | 2011-01-22 01:30:53 +0000 | [diff] [blame] | 2995 | } |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
| 2998 | if (discard_on_error && thread_plan_sp) |
| 2999 | { |
| 3000 | exe_ctx.thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
| 3001 | } |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 3002 | return_value = lldb::eExecutionInterrupted; |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 3003 | break; |
| 3004 | } |
| 3005 | } |
| 3006 | |
| 3007 | if (exe_ctx.process) |
| 3008 | exe_ctx.process->RestoreProcessEvents (); |
| 3009 | |
| 3010 | // Thread we ran the function in may have gone away because we ran the target |
| 3011 | // Check that it's still there. |
| 3012 | exe_ctx.thread = exe_ctx.process->GetThreadList().FindThreadByIndexID(tid, true).get(); |
| 3013 | if (exe_ctx.thread) |
| 3014 | exe_ctx.frame = exe_ctx.thread->GetStackFrameAtIndex(0).get(); |
| 3015 | |
| 3016 | // Also restore the current process'es selected frame & thread, since this function calling may |
| 3017 | // be done behind the user's back. |
| 3018 | |
| 3019 | if (selected_tid != LLDB_INVALID_THREAD_ID) |
| 3020 | { |
| 3021 | if (exe_ctx.process->GetThreadList().SetSelectedThreadByIndexID (selected_tid)) |
| 3022 | { |
| 3023 | // We were able to restore the selected thread, now restore the frame: |
| 3024 | exe_ctx.process->GetThreadList().GetSelectedThread()->SetSelectedFrame(selected_frame_sp.get()); |
| 3025 | } |
| 3026 | } |
| 3027 | |
| 3028 | return return_value; |
| 3029 | } |
| 3030 | |
| 3031 | const char * |
| 3032 | Process::ExecutionResultAsCString (ExecutionResults result) |
| 3033 | { |
| 3034 | const char *result_name; |
| 3035 | |
| 3036 | switch (result) |
| 3037 | { |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 3038 | case lldb::eExecutionCompleted: |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 3039 | result_name = "eExecutionCompleted"; |
| 3040 | break; |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 3041 | case lldb::eExecutionDiscarded: |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 3042 | result_name = "eExecutionDiscarded"; |
| 3043 | break; |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 3044 | case lldb::eExecutionInterrupted: |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 3045 | result_name = "eExecutionInterrupted"; |
| 3046 | break; |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 3047 | case lldb::eExecutionSetupError: |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 3048 | result_name = "eExecutionSetupError"; |
| 3049 | break; |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 3050 | case lldb::eExecutionTimedOut: |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 3051 | result_name = "eExecutionTimedOut"; |
| 3052 | break; |
| 3053 | } |
| 3054 | return result_name; |
| 3055 | } |
| 3056 | |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3057 | //-------------------------------------------------------------- |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 3058 | // class Process::SettingsController |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3059 | //-------------------------------------------------------------- |
| 3060 | |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 3061 | Process::SettingsController::SettingsController () : |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 3062 | UserSettingsController ("process", Target::GetSettingsController()) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3063 | { |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 3064 | m_default_settings.reset (new ProcessInstanceSettings (*this, |
| 3065 | false, |
Caroline Tice | 004afcb | 2010-09-08 17:48:55 +0000 | [diff] [blame] | 3066 | InstanceSettings::GetDefaultName().AsCString())); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 3069 | Process::SettingsController::~SettingsController () |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3070 | { |
| 3071 | } |
| 3072 | |
| 3073 | lldb::InstanceSettingsSP |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 3074 | Process::SettingsController::CreateInstanceSettings (const char *instance_name) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3075 | { |
Greg Clayton | c0c1b0c | 2010-11-19 03:46:01 +0000 | [diff] [blame] | 3076 | ProcessInstanceSettings *new_settings = new ProcessInstanceSettings (*GetSettingsController(), |
| 3077 | false, |
| 3078 | instance_name); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3079 | lldb::InstanceSettingsSP new_settings_sp (new_settings); |
| 3080 | return new_settings_sp; |
| 3081 | } |
| 3082 | |
| 3083 | //-------------------------------------------------------------- |
| 3084 | // class ProcessInstanceSettings |
| 3085 | //-------------------------------------------------------------- |
| 3086 | |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 3087 | ProcessInstanceSettings::ProcessInstanceSettings |
| 3088 | ( |
| 3089 | UserSettingsController &owner, |
| 3090 | bool live_instance, |
| 3091 | const char *name |
| 3092 | ) : |
| 3093 | InstanceSettings (owner, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance), |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3094 | m_run_args (), |
| 3095 | m_env_vars (), |
| 3096 | m_input_path (), |
| 3097 | m_output_path (), |
| 3098 | m_error_path (), |
| 3099 | m_plugin (), |
Caroline Tice | bd66601 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 3100 | m_disable_aslr (true), |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 3101 | m_disable_stdio (false), |
| 3102 | m_inherit_host_env (true), |
| 3103 | m_got_host_env (false) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3104 | { |
Caroline Tice | 396704b | 2010-09-09 18:26:37 +0000 | [diff] [blame] | 3105 | // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called |
| 3106 | // until the vtables for ProcessInstanceSettings are properly set up, i.e. AFTER all the initializers. |
| 3107 | // 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] | 3108 | // This is true for CreateInstanceName() too. |
| 3109 | |
| 3110 | if (GetInstanceName () == InstanceSettings::InvalidName()) |
| 3111 | { |
| 3112 | ChangeInstanceName (std::string (CreateInstanceName().AsCString())); |
| 3113 | m_owner.RegisterInstanceSettings (this); |
| 3114 | } |
Caroline Tice | 396704b | 2010-09-09 18:26:37 +0000 | [diff] [blame] | 3115 | |
| 3116 | if (live_instance) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3117 | { |
| 3118 | const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); |
| 3119 | CopyInstanceSettings (pending_settings,false); |
Caroline Tice | 396704b | 2010-09-09 18:26:37 +0000 | [diff] [blame] | 3120 | //m_owner.RemovePendingSettings (m_instance_name); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3121 | } |
| 3122 | } |
| 3123 | |
| 3124 | ProcessInstanceSettings::ProcessInstanceSettings (const ProcessInstanceSettings &rhs) : |
Greg Clayton | c0c1b0c | 2010-11-19 03:46:01 +0000 | [diff] [blame] | 3125 | InstanceSettings (*Process::GetSettingsController(), CreateInstanceName().AsCString()), |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3126 | m_run_args (rhs.m_run_args), |
| 3127 | m_env_vars (rhs.m_env_vars), |
| 3128 | m_input_path (rhs.m_input_path), |
| 3129 | m_output_path (rhs.m_output_path), |
| 3130 | m_error_path (rhs.m_error_path), |
| 3131 | m_plugin (rhs.m_plugin), |
Caroline Tice | bd66601 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 3132 | m_disable_aslr (rhs.m_disable_aslr), |
| 3133 | m_disable_stdio (rhs.m_disable_stdio) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3134 | { |
| 3135 | if (m_instance_name != InstanceSettings::GetDefaultName()) |
| 3136 | { |
| 3137 | const lldb::InstanceSettingsSP &pending_settings = m_owner.FindPendingSettings (m_instance_name); |
| 3138 | CopyInstanceSettings (pending_settings,false); |
| 3139 | m_owner.RemovePendingSettings (m_instance_name); |
| 3140 | } |
| 3141 | } |
| 3142 | |
| 3143 | ProcessInstanceSettings::~ProcessInstanceSettings () |
| 3144 | { |
| 3145 | } |
| 3146 | |
| 3147 | ProcessInstanceSettings& |
| 3148 | ProcessInstanceSettings::operator= (const ProcessInstanceSettings &rhs) |
| 3149 | { |
| 3150 | if (this != &rhs) |
| 3151 | { |
| 3152 | m_run_args = rhs.m_run_args; |
| 3153 | m_env_vars = rhs.m_env_vars; |
| 3154 | m_input_path = rhs.m_input_path; |
| 3155 | m_output_path = rhs.m_output_path; |
| 3156 | m_error_path = rhs.m_error_path; |
| 3157 | m_plugin = rhs.m_plugin; |
| 3158 | m_disable_aslr = rhs.m_disable_aslr; |
Caroline Tice | bd66601 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 3159 | m_disable_stdio = rhs.m_disable_stdio; |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 3160 | m_inherit_host_env = rhs.m_inherit_host_env; |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
| 3163 | return *this; |
| 3164 | } |
| 3165 | |
| 3166 | |
| 3167 | void |
| 3168 | ProcessInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name, |
| 3169 | const char *index_value, |
| 3170 | const char *value, |
| 3171 | const ConstString &instance_name, |
| 3172 | const SettingEntry &entry, |
| 3173 | lldb::VarSetOperationType op, |
| 3174 | Error &err, |
| 3175 | bool pending) |
| 3176 | { |
| 3177 | if (var_name == RunArgsVarName()) |
| 3178 | UserSettingsController::UpdateStringArrayVariable (op, index_value, m_run_args, value, err); |
| 3179 | else if (var_name == EnvVarsVarName()) |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 3180 | { |
| 3181 | GetHostEnvironmentIfNeeded (); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3182 | UserSettingsController::UpdateDictionaryVariable (op, index_value, m_env_vars, value, err); |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 3183 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3184 | else if (var_name == InputPathVarName()) |
| 3185 | UserSettingsController::UpdateStringVariable (op, m_input_path, value, err); |
| 3186 | else if (var_name == OutputPathVarName()) |
| 3187 | UserSettingsController::UpdateStringVariable (op, m_output_path, value, err); |
| 3188 | else if (var_name == ErrorPathVarName()) |
| 3189 | UserSettingsController::UpdateStringVariable (op, m_error_path, value, err); |
| 3190 | else if (var_name == PluginVarName()) |
| 3191 | UserSettingsController::UpdateEnumVariable (entry.enum_values, (int *) &m_plugin, value, err); |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 3192 | else if (var_name == InheritHostEnvVarName()) |
| 3193 | UserSettingsController::UpdateBooleanVariable (op, m_inherit_host_env, value, err); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3194 | else if (var_name == DisableASLRVarName()) |
| 3195 | UserSettingsController::UpdateBooleanVariable (op, m_disable_aslr, value, err); |
Caroline Tice | bd66601 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 3196 | else if (var_name == DisableSTDIOVarName ()) |
| 3197 | UserSettingsController::UpdateBooleanVariable (op, m_disable_stdio, value, err); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | void |
| 3201 | ProcessInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, |
| 3202 | bool pending) |
| 3203 | { |
| 3204 | if (new_settings.get() == NULL) |
| 3205 | return; |
| 3206 | |
| 3207 | ProcessInstanceSettings *new_process_settings = (ProcessInstanceSettings *) new_settings.get(); |
| 3208 | |
| 3209 | m_run_args = new_process_settings->m_run_args; |
| 3210 | m_env_vars = new_process_settings->m_env_vars; |
| 3211 | m_input_path = new_process_settings->m_input_path; |
| 3212 | m_output_path = new_process_settings->m_output_path; |
| 3213 | m_error_path = new_process_settings->m_error_path; |
| 3214 | m_plugin = new_process_settings->m_plugin; |
| 3215 | m_disable_aslr = new_process_settings->m_disable_aslr; |
Caroline Tice | bd66601 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 3216 | m_disable_stdio = new_process_settings->m_disable_stdio; |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
Caroline Tice | bcb5b45 | 2010-09-20 21:37:42 +0000 | [diff] [blame] | 3219 | bool |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3220 | ProcessInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry, |
| 3221 | const ConstString &var_name, |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 3222 | StringList &value, |
Caroline Tice | bcb5b45 | 2010-09-20 21:37:42 +0000 | [diff] [blame] | 3223 | Error *err) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3224 | { |
| 3225 | if (var_name == RunArgsVarName()) |
| 3226 | { |
| 3227 | if (m_run_args.GetArgumentCount() > 0) |
Greg Clayton | c14069e | 2010-09-14 03:47:41 +0000 | [diff] [blame] | 3228 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3229 | for (int i = 0; i < m_run_args.GetArgumentCount(); ++i) |
| 3230 | value.AppendString (m_run_args.GetArgumentAtIndex (i)); |
Greg Clayton | c14069e | 2010-09-14 03:47:41 +0000 | [diff] [blame] | 3231 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3232 | } |
| 3233 | else if (var_name == EnvVarsVarName()) |
| 3234 | { |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 3235 | GetHostEnvironmentIfNeeded (); |
| 3236 | |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3237 | if (m_env_vars.size() > 0) |
| 3238 | { |
| 3239 | std::map<std::string, std::string>::iterator pos; |
| 3240 | for (pos = m_env_vars.begin(); pos != m_env_vars.end(); ++pos) |
| 3241 | { |
| 3242 | StreamString value_str; |
| 3243 | value_str.Printf ("%s=%s", pos->first.c_str(), pos->second.c_str()); |
| 3244 | value.AppendString (value_str.GetData()); |
| 3245 | } |
| 3246 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3247 | } |
| 3248 | else if (var_name == InputPathVarName()) |
| 3249 | { |
| 3250 | value.AppendString (m_input_path.c_str()); |
| 3251 | } |
| 3252 | else if (var_name == OutputPathVarName()) |
| 3253 | { |
| 3254 | value.AppendString (m_output_path.c_str()); |
| 3255 | } |
| 3256 | else if (var_name == ErrorPathVarName()) |
| 3257 | { |
| 3258 | value.AppendString (m_error_path.c_str()); |
| 3259 | } |
| 3260 | else if (var_name == PluginVarName()) |
| 3261 | { |
| 3262 | value.AppendString (UserSettingsController::EnumToString (entry.enum_values, (int) m_plugin)); |
| 3263 | } |
Greg Clayton | a99b0bf | 2010-12-04 00:12:24 +0000 | [diff] [blame] | 3264 | else if (var_name == InheritHostEnvVarName()) |
| 3265 | { |
| 3266 | if (m_inherit_host_env) |
| 3267 | value.AppendString ("true"); |
| 3268 | else |
| 3269 | value.AppendString ("false"); |
| 3270 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3271 | else if (var_name == DisableASLRVarName()) |
| 3272 | { |
| 3273 | if (m_disable_aslr) |
| 3274 | value.AppendString ("true"); |
| 3275 | else |
| 3276 | value.AppendString ("false"); |
| 3277 | } |
Caroline Tice | bd66601 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 3278 | else if (var_name == DisableSTDIOVarName()) |
| 3279 | { |
| 3280 | if (m_disable_stdio) |
| 3281 | value.AppendString ("true"); |
| 3282 | else |
| 3283 | value.AppendString ("false"); |
| 3284 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3285 | else |
Caroline Tice | bcb5b45 | 2010-09-20 21:37:42 +0000 | [diff] [blame] | 3286 | { |
| 3287 | if (err) |
| 3288 | err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); |
| 3289 | return false; |
| 3290 | } |
| 3291 | return true; |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3294 | const ConstString |
| 3295 | ProcessInstanceSettings::CreateInstanceName () |
| 3296 | { |
| 3297 | static int instance_count = 1; |
| 3298 | StreamString sstr; |
| 3299 | |
| 3300 | sstr.Printf ("process_%d", instance_count); |
| 3301 | ++instance_count; |
| 3302 | |
| 3303 | const ConstString ret_val (sstr.GetData()); |
| 3304 | return ret_val; |
| 3305 | } |
| 3306 | |
| 3307 | const ConstString & |
| 3308 | ProcessInstanceSettings::RunArgsVarName () |
| 3309 | { |
| 3310 | static ConstString run_args_var_name ("run-args"); |
| 3311 | |
| 3312 | return run_args_var_name; |
| 3313 | } |
| 3314 | |
| 3315 | const ConstString & |
| 3316 | ProcessInstanceSettings::EnvVarsVarName () |
| 3317 | { |
| 3318 | static ConstString env_vars_var_name ("env-vars"); |
| 3319 | |
| 3320 | return env_vars_var_name; |
| 3321 | } |
| 3322 | |
| 3323 | const ConstString & |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 3324 | ProcessInstanceSettings::InheritHostEnvVarName () |
| 3325 | { |
| 3326 | static ConstString g_name ("inherit-env"); |
| 3327 | |
| 3328 | return g_name; |
| 3329 | } |
| 3330 | |
| 3331 | const ConstString & |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3332 | ProcessInstanceSettings::InputPathVarName () |
| 3333 | { |
| 3334 | static ConstString input_path_var_name ("input-path"); |
| 3335 | |
| 3336 | return input_path_var_name; |
| 3337 | } |
| 3338 | |
| 3339 | const ConstString & |
| 3340 | ProcessInstanceSettings::OutputPathVarName () |
| 3341 | { |
Caroline Tice | 8709723 | 2010-09-07 18:35:40 +0000 | [diff] [blame] | 3342 | static ConstString output_path_var_name ("output-path"); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3343 | |
| 3344 | return output_path_var_name; |
| 3345 | } |
| 3346 | |
| 3347 | const ConstString & |
| 3348 | ProcessInstanceSettings::ErrorPathVarName () |
| 3349 | { |
Caroline Tice | 8709723 | 2010-09-07 18:35:40 +0000 | [diff] [blame] | 3350 | static ConstString error_path_var_name ("error-path"); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3351 | |
| 3352 | return error_path_var_name; |
| 3353 | } |
| 3354 | |
| 3355 | const ConstString & |
| 3356 | ProcessInstanceSettings::PluginVarName () |
| 3357 | { |
| 3358 | static ConstString plugin_var_name ("plugin"); |
| 3359 | |
| 3360 | return plugin_var_name; |
| 3361 | } |
| 3362 | |
| 3363 | |
| 3364 | const ConstString & |
| 3365 | ProcessInstanceSettings::DisableASLRVarName () |
| 3366 | { |
| 3367 | static ConstString disable_aslr_var_name ("disable-aslr"); |
| 3368 | |
| 3369 | return disable_aslr_var_name; |
| 3370 | } |
| 3371 | |
Caroline Tice | bd66601 | 2010-12-03 18:46:09 +0000 | [diff] [blame] | 3372 | const ConstString & |
| 3373 | ProcessInstanceSettings::DisableSTDIOVarName () |
| 3374 | { |
| 3375 | static ConstString disable_stdio_var_name ("disable-stdio"); |
| 3376 | |
| 3377 | return disable_stdio_var_name; |
| 3378 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3379 | |
| 3380 | //-------------------------------------------------- |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 3381 | // SettingsController Variable Tables |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3382 | //-------------------------------------------------- |
| 3383 | |
| 3384 | SettingEntry |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 3385 | Process::SettingsController::global_settings_table[] = |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3386 | { |
| 3387 | //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"}, |
| 3388 | { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } |
| 3389 | }; |
| 3390 | |
| 3391 | |
| 3392 | lldb::OptionEnumValueElement |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 3393 | Process::SettingsController::g_plugins[] = |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3394 | { |
Caroline Tice | f2c330d | 2010-09-09 18:01:59 +0000 | [diff] [blame] | 3395 | { eMacosx, "process.macosx", "Use the native MacOSX debugger plugin" }, |
| 3396 | { eRemoteDebugger, "process.gdb-remote" , "Use the GDB Remote protocol based debugger plugin" }, |
| 3397 | { 0, NULL, NULL } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3398 | }; |
| 3399 | |
| 3400 | SettingEntry |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 3401 | Process::SettingsController::instance_settings_table[] = |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3402 | { |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 3403 | //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"}, |
| 3404 | { "run-args", eSetVarTypeArray, NULL, NULL, false, false, "A list containing all the arguments to be passed to the executable when it is run." }, |
| 3405 | { "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." }, |
| 3406 | { "inherit-env", eSetVarTypeBoolean, "true", NULL, false, false, "Inherit the environment from the process that is running LLDB." }, |
Greg Clayton | de915be | 2011-01-23 05:56:20 +0000 | [diff] [blame] | 3407 | { "input-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for reading its input." }, |
| 3408 | { "output-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for writing its output." }, |
| 3409 | { "error-path", eSetVarTypeString, NULL, NULL, false, false, "The file/path to be used by the executable program for writings its error messages." }, |
| 3410 | { "plugin", eSetVarTypeEnum, NULL, g_plugins, false, false, "The plugin to be used to run the process." }, |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 3411 | { "disable-aslr", eSetVarTypeBoolean, "true", NULL, false, false, "Disable Address Space Layout Randomization (ASLR)" }, |
| 3412 | { "disable-stdio", eSetVarTypeBoolean, "false", NULL, false, false, "Disable stdin/stdout for process (e.g. for a GUI application)" }, |
| 3413 | { NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3414 | }; |
| 3415 | |
| 3416 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 3417 | |