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