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