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" |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 28 | #include "lldb/Target/OperatingSystem.h" |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 29 | #include "lldb/Target/LanguageRuntime.h" |
| 30 | #include "lldb/Target/CPPLanguageRuntime.h" |
| 31 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 32 | #include "lldb/Target/Platform.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 34 | #include "lldb/Target/StopInfo.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | #include "lldb/Target/Target.h" |
| 36 | #include "lldb/Target/TargetList.h" |
| 37 | #include "lldb/Target/Thread.h" |
| 38 | #include "lldb/Target/ThreadPlan.h" |
Jim Ingham | d21d98b | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 39 | #include "lldb/Target/ThreadPlanBase.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | |
| 41 | using namespace lldb; |
| 42 | using namespace lldb_private; |
| 43 | |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 44 | void |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 45 | ProcessInstanceInfo::Dump (Stream &s, Platform *platform) const |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 46 | { |
| 47 | const char *cstr; |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 48 | if (m_pid != LLDB_INVALID_PROCESS_ID) |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 49 | s.Printf (" pid = %llu\n", m_pid); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 50 | |
| 51 | if (m_parent_pid != LLDB_INVALID_PROCESS_ID) |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 52 | s.Printf (" parent = %llu\n", m_parent_pid); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 53 | |
| 54 | if (m_executable) |
| 55 | { |
| 56 | s.Printf (" name = %s\n", m_executable.GetFilename().GetCString()); |
| 57 | s.PutCString (" file = "); |
| 58 | m_executable.Dump(&s); |
| 59 | s.EOL(); |
| 60 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 61 | const uint32_t argc = m_arguments.GetArgumentCount(); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 62 | if (argc > 0) |
| 63 | { |
| 64 | for (uint32_t i=0; i<argc; i++) |
| 65 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 66 | const char *arg = m_arguments.GetArgumentAtIndex(i); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 67 | if (i < 10) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 68 | s.Printf (" arg[%u] = %s\n", i, arg); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 69 | else |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 70 | s.Printf ("arg[%u] = %s\n", i, arg); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 73 | |
| 74 | const uint32_t envc = m_environment.GetArgumentCount(); |
| 75 | if (envc > 0) |
| 76 | { |
| 77 | for (uint32_t i=0; i<envc; i++) |
| 78 | { |
| 79 | const char *env = m_environment.GetArgumentAtIndex(i); |
| 80 | if (i < 10) |
| 81 | s.Printf (" env[%u] = %s\n", i, env); |
| 82 | else |
| 83 | s.Printf ("env[%u] = %s\n", i, env); |
| 84 | } |
| 85 | } |
| 86 | |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 87 | if (m_arch.IsValid()) |
| 88 | s.Printf (" arch = %s\n", m_arch.GetTriple().str().c_str()); |
| 89 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 90 | if (m_uid != UINT32_MAX) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 91 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 92 | cstr = platform->GetUserName (m_uid); |
| 93 | s.Printf (" uid = %-5u (%s)\n", m_uid, cstr ? cstr : ""); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 94 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 95 | if (m_gid != UINT32_MAX) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 96 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 97 | cstr = platform->GetGroupName (m_gid); |
| 98 | s.Printf (" gid = %-5u (%s)\n", m_gid, cstr ? cstr : ""); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 99 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 100 | if (m_euid != UINT32_MAX) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 101 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 102 | cstr = platform->GetUserName (m_euid); |
| 103 | s.Printf (" euid = %-5u (%s)\n", m_euid, cstr ? cstr : ""); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 104 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 105 | if (m_egid != UINT32_MAX) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 106 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 107 | cstr = platform->GetGroupName (m_egid); |
| 108 | s.Printf (" egid = %-5u (%s)\n", m_egid, cstr ? cstr : ""); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | void |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 113 | ProcessInstanceInfo::DumpTableHeader (Stream &s, Platform *platform, bool show_args, bool verbose) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 114 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 115 | const char *label; |
| 116 | if (show_args || verbose) |
| 117 | label = "ARGUMENTS"; |
| 118 | else |
| 119 | label = "NAME"; |
| 120 | |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 121 | if (verbose) |
| 122 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 123 | s.Printf ("PID PARENT USER GROUP EFF USER EFF GROUP TRIPLE %s\n", label); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 124 | s.PutCString ("====== ====== ========== ========== ========== ========== ======================== ============================\n"); |
| 125 | } |
| 126 | else |
| 127 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 128 | s.Printf ("PID PARENT USER ARCH %s\n", label); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 129 | s.PutCString ("====== ====== ========== ======= ============================\n"); |
| 130 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 134 | ProcessInstanceInfo::DumpAsTableRow (Stream &s, Platform *platform, bool show_args, bool verbose) const |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 135 | { |
| 136 | if (m_pid != LLDB_INVALID_PROCESS_ID) |
| 137 | { |
| 138 | const char *cstr; |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 139 | s.Printf ("%-6llu %-6llu ", m_pid, m_parent_pid); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 140 | |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 141 | |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 142 | if (verbose) |
| 143 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 144 | cstr = platform->GetUserName (m_uid); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 145 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 146 | s.Printf ("%-10s ", cstr); |
| 147 | else |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 148 | s.Printf ("%-10u ", m_uid); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 149 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 150 | cstr = platform->GetGroupName (m_gid); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 151 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 152 | s.Printf ("%-10s ", cstr); |
| 153 | else |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 154 | s.Printf ("%-10u ", m_gid); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 155 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 156 | cstr = platform->GetUserName (m_euid); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 157 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 158 | s.Printf ("%-10s ", cstr); |
| 159 | else |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 160 | s.Printf ("%-10u ", m_euid); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 161 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 162 | cstr = platform->GetGroupName (m_egid); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 163 | if (cstr && cstr[0]) // Watch for empty string that indicates lookup failed |
| 164 | s.Printf ("%-10s ", cstr); |
| 165 | else |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 166 | s.Printf ("%-10u ", m_egid); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 167 | s.Printf ("%-24s ", m_arch.IsValid() ? m_arch.GetTriple().str().c_str() : ""); |
| 168 | } |
| 169 | else |
| 170 | { |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 171 | s.Printf ("%-10s %-7d %s ", |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 172 | platform->GetUserName (m_euid), |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 173 | (int)m_arch.GetTriple().getArchName().size(), |
| 174 | m_arch.GetTriple().getArchName().data()); |
| 175 | } |
| 176 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 177 | if (verbose || show_args) |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 178 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 179 | const uint32_t argc = m_arguments.GetArgumentCount(); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 180 | if (argc > 0) |
| 181 | { |
| 182 | for (uint32_t i=0; i<argc; i++) |
| 183 | { |
| 184 | if (i > 0) |
| 185 | s.PutChar (' '); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 186 | s.PutCString (m_arguments.GetArgumentAtIndex(i)); |
Greg Clayton | ff39f74 | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | s.PutCString (GetName()); |
| 193 | } |
| 194 | |
| 195 | s.EOL(); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 199 | |
| 200 | void |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 201 | ProcessInfo::SetArguments (char const **argv, |
| 202 | bool first_arg_is_executable, |
| 203 | bool first_arg_is_executable_and_argument) |
| 204 | { |
| 205 | m_arguments.SetArguments (argv); |
| 206 | |
| 207 | // Is the first argument the executable? |
| 208 | if (first_arg_is_executable) |
| 209 | { |
| 210 | const char *first_arg = m_arguments.GetArgumentAtIndex (0); |
| 211 | if (first_arg) |
| 212 | { |
| 213 | // Yes the first argument is an executable, set it as the executable |
| 214 | // in the launch options. Don't resolve the file path as the path |
| 215 | // could be a remote platform path |
| 216 | const bool resolve = false; |
| 217 | m_executable.SetFile(first_arg, resolve); |
| 218 | |
| 219 | // If argument zero is an executable and shouldn't be included |
| 220 | // in the arguments, remove it from the front of the arguments |
| 221 | if (first_arg_is_executable_and_argument == false) |
| 222 | m_arguments.DeleteArgumentAtIndex (0); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | void |
| 227 | ProcessInfo::SetArguments (const Args& args, |
| 228 | bool first_arg_is_executable, |
| 229 | bool first_arg_is_executable_and_argument) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 230 | { |
| 231 | // Copy all arguments |
| 232 | m_arguments = args; |
| 233 | |
| 234 | // Is the first argument the executable? |
| 235 | if (first_arg_is_executable) |
| 236 | { |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 237 | const char *first_arg = m_arguments.GetArgumentAtIndex (0); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 238 | if (first_arg) |
| 239 | { |
| 240 | // Yes the first argument is an executable, set it as the executable |
| 241 | // in the launch options. Don't resolve the file path as the path |
| 242 | // could be a remote platform path |
| 243 | const bool resolve = false; |
| 244 | m_executable.SetFile(first_arg, resolve); |
| 245 | |
| 246 | // If argument zero is an executable and shouldn't be included |
| 247 | // in the arguments, remove it from the front of the arguments |
| 248 | if (first_arg_is_executable_and_argument == false) |
| 249 | m_arguments.DeleteArgumentAtIndex (0); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 254 | void |
Greg Clayton | 464c616 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 255 | ProcessLaunchInfo::FinalizeFileActions (Target *target, bool default_to_use_pty) |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 256 | { |
| 257 | // If notthing was specified, then check the process for any default |
| 258 | // settings that were set with "settings set" |
| 259 | if (m_file_actions.empty()) |
| 260 | { |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 261 | if (m_flags.Test(eLaunchFlagDisableSTDIO)) |
| 262 | { |
Greg Clayton | 95ec168 | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 263 | AppendSuppressFileAction (STDIN_FILENO , true, false); |
| 264 | AppendSuppressFileAction (STDOUT_FILENO, false, true); |
| 265 | AppendSuppressFileAction (STDERR_FILENO, false, true); |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 266 | } |
| 267 | else |
| 268 | { |
| 269 | // Check for any values that might have gotten set with any of: |
| 270 | // (lldb) settings set target.input-path |
| 271 | // (lldb) settings set target.output-path |
| 272 | // (lldb) settings set target.error-path |
Greg Clayton | 464c616 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 273 | const char *in_path = NULL; |
| 274 | const char *out_path = NULL; |
| 275 | const char *err_path = NULL; |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 276 | if (target) |
| 277 | { |
Greg Clayton | 95ec168 | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 278 | in_path = target->GetStandardInputPath(); |
| 279 | out_path = target->GetStandardOutputPath(); |
| 280 | err_path = target->GetStandardErrorPath(); |
Greg Clayton | 464c616 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | if (default_to_use_pty && (!in_path && !out_path && !err_path)) |
| 284 | { |
| 285 | if (m_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, NULL, 0)) |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 286 | { |
Greg Clayton | 464c616 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 287 | in_path = out_path = err_path = m_pty.GetSlaveName (NULL, 0); |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Greg Clayton | 464c616 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 291 | if (in_path) |
Greg Clayton | 95ec168 | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 292 | AppendOpenFileAction(STDIN_FILENO, in_path, true, false); |
| 293 | |
Greg Clayton | 464c616 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 294 | if (out_path) |
Greg Clayton | 95ec168 | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 295 | AppendOpenFileAction(STDOUT_FILENO, out_path, false, true); |
Greg Clayton | 464c616 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 296 | |
| 297 | if (err_path) |
Greg Clayton | 95ec168 | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 298 | AppendOpenFileAction(STDERR_FILENO, err_path, false, true); |
| 299 | |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 304 | |
| 305 | bool |
| 306 | ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell (Error &error, bool localhost) |
| 307 | { |
| 308 | error.Clear(); |
| 309 | |
| 310 | if (GetFlags().Test (eLaunchFlagLaunchInShell)) |
| 311 | { |
| 312 | const char *shell_executable = GetShell(); |
| 313 | if (shell_executable) |
| 314 | { |
| 315 | char shell_resolved_path[PATH_MAX]; |
| 316 | |
| 317 | if (localhost) |
| 318 | { |
| 319 | FileSpec shell_filespec (shell_executable, true); |
| 320 | |
| 321 | if (!shell_filespec.Exists()) |
| 322 | { |
| 323 | // Resolve the path in case we just got "bash", "sh" or "tcsh" |
| 324 | if (!shell_filespec.ResolveExecutableLocation ()) |
| 325 | { |
| 326 | error.SetErrorStringWithFormat("invalid shell path '%s'", shell_executable); |
| 327 | return false; |
| 328 | } |
| 329 | } |
| 330 | shell_filespec.GetPath (shell_resolved_path, sizeof(shell_resolved_path)); |
| 331 | shell_executable = shell_resolved_path; |
| 332 | } |
| 333 | |
| 334 | Args shell_arguments; |
| 335 | std::string safe_arg; |
| 336 | shell_arguments.AppendArgument (shell_executable); |
| 337 | StreamString shell_command; |
| 338 | shell_arguments.AppendArgument ("-c"); |
| 339 | shell_command.PutCString ("exec"); |
| 340 | if (GetArchitecture().IsValid()) |
| 341 | { |
| 342 | shell_command.Printf(" /usr/bin/arch -arch %s", GetArchitecture().GetArchitectureName()); |
| 343 | // Set the resume count to 2: |
| 344 | // 1 - stop in shell |
| 345 | // 2 - stop in /usr/bin/arch |
| 346 | // 3 - then we will stop in our program |
| 347 | SetResumeCount(2); |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | // Set the resume count to 1: |
| 352 | // 1 - stop in shell |
| 353 | // 2 - then we will stop in our program |
| 354 | SetResumeCount(1); |
| 355 | } |
| 356 | |
| 357 | const char **argv = GetArguments().GetConstArgumentVector (); |
| 358 | if (argv) |
| 359 | { |
| 360 | for (size_t i=0; argv[i] != NULL; ++i) |
| 361 | { |
| 362 | const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg); |
| 363 | shell_command.Printf(" %s", arg); |
| 364 | } |
| 365 | } |
| 366 | shell_arguments.AppendArgument (shell_command.GetString().c_str()); |
| 367 | |
| 368 | m_executable.SetFile(shell_executable, false); |
| 369 | m_arguments = shell_arguments; |
| 370 | return true; |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | error.SetErrorString ("invalid shell path"); |
| 375 | } |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | error.SetErrorString ("not launching in shell"); |
| 380 | } |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 385 | bool |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 386 | ProcessLaunchInfo::FileAction::Open (int fd, const char *path, bool read, bool write) |
| 387 | { |
| 388 | if ((read || write) && fd >= 0 && path && path[0]) |
| 389 | { |
| 390 | m_action = eFileActionOpen; |
| 391 | m_fd = fd; |
| 392 | if (read && write) |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 393 | m_arg = O_NOCTTY | O_CREAT | O_RDWR; |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 394 | else if (read) |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 395 | m_arg = O_NOCTTY | O_RDONLY; |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 396 | else |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 397 | m_arg = O_NOCTTY | O_CREAT | O_WRONLY; |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 398 | m_path.assign (path); |
| 399 | return true; |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | Clear(); |
| 404 | } |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | bool |
| 409 | ProcessLaunchInfo::FileAction::Close (int fd) |
| 410 | { |
| 411 | Clear(); |
| 412 | if (fd >= 0) |
| 413 | { |
| 414 | m_action = eFileActionClose; |
| 415 | m_fd = fd; |
| 416 | } |
| 417 | return m_fd >= 0; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | bool |
| 422 | ProcessLaunchInfo::FileAction::Duplicate (int fd, int dup_fd) |
| 423 | { |
| 424 | Clear(); |
| 425 | if (fd >= 0 && dup_fd >= 0) |
| 426 | { |
| 427 | m_action = eFileActionDuplicate; |
| 428 | m_fd = fd; |
| 429 | m_arg = dup_fd; |
| 430 | } |
| 431 | return m_fd >= 0; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | |
| 436 | bool |
| 437 | ProcessLaunchInfo::FileAction::AddPosixSpawnFileAction (posix_spawn_file_actions_t *file_actions, |
| 438 | const FileAction *info, |
| 439 | Log *log, |
| 440 | Error& error) |
| 441 | { |
| 442 | if (info == NULL) |
| 443 | return false; |
| 444 | |
| 445 | switch (info->m_action) |
| 446 | { |
| 447 | case eFileActionNone: |
| 448 | error.Clear(); |
| 449 | break; |
| 450 | |
| 451 | case eFileActionClose: |
| 452 | if (info->m_fd == -1) |
| 453 | error.SetErrorString ("invalid fd for posix_spawn_file_actions_addclose(...)"); |
| 454 | else |
| 455 | { |
| 456 | error.SetError (::posix_spawn_file_actions_addclose (file_actions, info->m_fd), |
| 457 | eErrorTypePOSIX); |
| 458 | if (log && (error.Fail() || log)) |
| 459 | error.PutToLog(log, "posix_spawn_file_actions_addclose (action=%p, fd=%i)", |
| 460 | file_actions, info->m_fd); |
| 461 | } |
| 462 | break; |
| 463 | |
| 464 | case eFileActionDuplicate: |
| 465 | if (info->m_fd == -1) |
| 466 | error.SetErrorString ("invalid fd for posix_spawn_file_actions_adddup2(...)"); |
| 467 | else if (info->m_arg == -1) |
| 468 | error.SetErrorString ("invalid duplicate fd for posix_spawn_file_actions_adddup2(...)"); |
| 469 | else |
| 470 | { |
| 471 | error.SetError (::posix_spawn_file_actions_adddup2 (file_actions, info->m_fd, info->m_arg), |
| 472 | eErrorTypePOSIX); |
| 473 | if (log && (error.Fail() || log)) |
| 474 | error.PutToLog(log, "posix_spawn_file_actions_adddup2 (action=%p, fd=%i, dup_fd=%i)", |
| 475 | file_actions, info->m_fd, info->m_arg); |
| 476 | } |
| 477 | break; |
| 478 | |
| 479 | case eFileActionOpen: |
| 480 | if (info->m_fd == -1) |
| 481 | error.SetErrorString ("invalid fd in posix_spawn_file_actions_addopen(...)"); |
| 482 | else |
| 483 | { |
| 484 | int oflag = info->m_arg; |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 485 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 486 | mode_t mode = 0; |
| 487 | |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 488 | if (oflag & O_CREAT) |
| 489 | mode = 0640; |
| 490 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 491 | error.SetError (::posix_spawn_file_actions_addopen (file_actions, |
| 492 | info->m_fd, |
| 493 | info->m_path.c_str(), |
| 494 | oflag, |
| 495 | mode), |
| 496 | eErrorTypePOSIX); |
| 497 | if (error.Fail() || log) |
| 498 | error.PutToLog(log, |
| 499 | "posix_spawn_file_actions_addopen (action=%p, fd=%i, path='%s', oflag=%i, mode=%i)", |
| 500 | file_actions, info->m_fd, info->m_path.c_str(), oflag, mode); |
| 501 | } |
| 502 | break; |
| 503 | |
| 504 | default: |
| 505 | error.SetErrorStringWithFormat ("invalid file action: %i", info->m_action); |
| 506 | break; |
| 507 | } |
| 508 | return error.Success(); |
| 509 | } |
| 510 | |
| 511 | Error |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 512 | ProcessLaunchCommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 513 | { |
| 514 | Error error; |
| 515 | char short_option = (char) m_getopt_table[option_idx].val; |
| 516 | |
| 517 | switch (short_option) |
| 518 | { |
| 519 | case 's': // Stop at program entry point |
| 520 | launch_info.GetFlags().Set (eLaunchFlagStopAtEntry); |
| 521 | break; |
| 522 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 523 | case 'i': // STDIN for read only |
| 524 | { |
| 525 | ProcessLaunchInfo::FileAction action; |
Greg Clayton | 95ec168 | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 526 | if (action.Open (STDIN_FILENO, option_arg, true, false)) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 527 | launch_info.AppendFileAction (action); |
| 528 | } |
| 529 | break; |
| 530 | |
| 531 | case 'o': // Open STDOUT for write only |
| 532 | { |
| 533 | ProcessLaunchInfo::FileAction action; |
Greg Clayton | 95ec168 | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 534 | if (action.Open (STDOUT_FILENO, option_arg, false, true)) |
| 535 | launch_info.AppendFileAction (action); |
| 536 | } |
| 537 | break; |
| 538 | |
| 539 | case 'e': // STDERR for write only |
| 540 | { |
| 541 | ProcessLaunchInfo::FileAction action; |
| 542 | if (action.Open (STDERR_FILENO, option_arg, false, true)) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 543 | launch_info.AppendFileAction (action); |
| 544 | } |
| 545 | break; |
| 546 | |
Greg Clayton | 95ec168 | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 547 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 548 | case 'p': // Process plug-in name |
| 549 | launch_info.SetProcessPluginName (option_arg); |
| 550 | break; |
| 551 | |
| 552 | case 'n': // Disable STDIO |
| 553 | { |
| 554 | ProcessLaunchInfo::FileAction action; |
Greg Clayton | 95ec168 | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 555 | if (action.Open (STDIN_FILENO, "/dev/null", true, false)) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 556 | launch_info.AppendFileAction (action); |
Greg Clayton | 95ec168 | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 557 | if (action.Open (STDOUT_FILENO, "/dev/null", false, true)) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 558 | launch_info.AppendFileAction (action); |
Greg Clayton | 95ec168 | 2012-03-06 04:01:04 +0000 | [diff] [blame] | 559 | if (action.Open (STDERR_FILENO, "/dev/null", false, true)) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 560 | launch_info.AppendFileAction (action); |
| 561 | } |
| 562 | break; |
| 563 | |
| 564 | case 'w': |
| 565 | launch_info.SetWorkingDirectory (option_arg); |
| 566 | break; |
| 567 | |
| 568 | case 't': // Open process in new terminal window |
| 569 | launch_info.GetFlags().Set (eLaunchFlagLaunchInTTY); |
| 570 | break; |
| 571 | |
| 572 | case 'a': |
| 573 | launch_info.GetArchitecture().SetTriple (option_arg, |
| 574 | m_interpreter.GetPlatform(true).get()); |
| 575 | break; |
| 576 | |
| 577 | case 'A': |
| 578 | launch_info.GetFlags().Set (eLaunchFlagDisableASLR); |
| 579 | break; |
| 580 | |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 581 | case 'c': |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 582 | if (option_arg && option_arg[0]) |
| 583 | launch_info.SetShell (option_arg); |
| 584 | else |
| 585 | launch_info.SetShell ("/bin/bash"); |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 586 | break; |
| 587 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 588 | case 'v': |
| 589 | launch_info.GetEnvironmentEntries().AppendArgument(option_arg); |
| 590 | break; |
| 591 | |
| 592 | default: |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 593 | error.SetErrorStringWithFormat("unrecognized short option character '%c'", short_option); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 594 | break; |
| 595 | |
| 596 | } |
| 597 | return error; |
| 598 | } |
| 599 | |
| 600 | OptionDefinition |
| 601 | ProcessLaunchCommandOptions::g_option_table[] = |
| 602 | { |
| 603 | { LLDB_OPT_SET_ALL, false, "stop-at-entry", 's', no_argument, NULL, 0, eArgTypeNone, "Stop at the entry point of the program when launching a process."}, |
| 604 | { LLDB_OPT_SET_ALL, false, "disable-aslr", 'A', no_argument, NULL, 0, eArgTypeNone, "Disable address space layout randomization when launching a process."}, |
| 605 | { LLDB_OPT_SET_ALL, false, "plugin", 'p', required_argument, NULL, 0, eArgTypePlugin, "Name of the process plugin you want to use."}, |
| 606 | { LLDB_OPT_SET_ALL, false, "working-dir", 'w', required_argument, NULL, 0, eArgTypePath, "Set the current working directory to <path> when running the inferior."}, |
| 607 | { LLDB_OPT_SET_ALL, false, "arch", 'a', required_argument, NULL, 0, eArgTypeArchitecture, "Set the architecture for the process to launch when ambiguous."}, |
| 608 | { LLDB_OPT_SET_ALL, false, "environment", 'v', required_argument, NULL, 0, eArgTypeNone, "Specify an environment variable name/value stirng (--environement NAME=VALUE). Can be specified multiple times for subsequent environment entries."}, |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 609 | { LLDB_OPT_SET_ALL, false, "shell", 'c', optional_argument, NULL, 0, eArgTypePath, "Run the process in a shell (not supported on all platforms)."}, |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 610 | |
| 611 | { LLDB_OPT_SET_1 , false, "stdin", 'i', required_argument, NULL, 0, eArgTypePath, "Redirect stdin for the process to <path>."}, |
| 612 | { LLDB_OPT_SET_1 , false, "stdout", 'o', required_argument, NULL, 0, eArgTypePath, "Redirect stdout for the process to <path>."}, |
| 613 | { LLDB_OPT_SET_1 , false, "stderr", 'e', required_argument, NULL, 0, eArgTypePath, "Redirect stderr for the process to <path>."}, |
| 614 | |
| 615 | { LLDB_OPT_SET_2 , false, "tty", 't', no_argument, NULL, 0, eArgTypeNone, "Start the process in a terminal (not supported on all platforms)."}, |
| 616 | |
| 617 | { LLDB_OPT_SET_3 , false, "no-stdio", 'n', no_argument, NULL, 0, eArgTypeNone, "Do not set up for terminal I/O to go to running process."}, |
| 618 | |
| 619 | { 0 , false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } |
| 620 | }; |
| 621 | |
| 622 | |
| 623 | |
| 624 | bool |
| 625 | ProcessInstanceInfoMatch::NameMatches (const char *process_name) const |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 626 | { |
| 627 | if (m_name_match_type == eNameMatchIgnore || process_name == NULL) |
| 628 | return true; |
| 629 | const char *match_name = m_match_info.GetName(); |
| 630 | if (!match_name) |
| 631 | return true; |
| 632 | |
| 633 | return lldb_private::NameMatches (process_name, m_name_match_type, match_name); |
| 634 | } |
| 635 | |
| 636 | bool |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 637 | ProcessInstanceInfoMatch::Matches (const ProcessInstanceInfo &proc_info) const |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 638 | { |
| 639 | if (!NameMatches (proc_info.GetName())) |
| 640 | return false; |
| 641 | |
| 642 | if (m_match_info.ProcessIDIsValid() && |
| 643 | m_match_info.GetProcessID() != proc_info.GetProcessID()) |
| 644 | return false; |
| 645 | |
| 646 | if (m_match_info.ParentProcessIDIsValid() && |
| 647 | m_match_info.GetParentProcessID() != proc_info.GetParentProcessID()) |
| 648 | return false; |
| 649 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 650 | if (m_match_info.UserIDIsValid () && |
| 651 | m_match_info.GetUserID() != proc_info.GetUserID()) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 652 | return false; |
| 653 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 654 | if (m_match_info.GroupIDIsValid () && |
| 655 | m_match_info.GetGroupID() != proc_info.GetGroupID()) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 656 | return false; |
| 657 | |
| 658 | if (m_match_info.EffectiveUserIDIsValid () && |
| 659 | m_match_info.GetEffectiveUserID() != proc_info.GetEffectiveUserID()) |
| 660 | return false; |
| 661 | |
| 662 | if (m_match_info.EffectiveGroupIDIsValid () && |
| 663 | m_match_info.GetEffectiveGroupID() != proc_info.GetEffectiveGroupID()) |
| 664 | return false; |
| 665 | |
| 666 | if (m_match_info.GetArchitecture().IsValid() && |
| 667 | m_match_info.GetArchitecture() != proc_info.GetArchitecture()) |
| 668 | return false; |
| 669 | return true; |
| 670 | } |
| 671 | |
| 672 | bool |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 673 | ProcessInstanceInfoMatch::MatchAllProcesses () const |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 674 | { |
| 675 | if (m_name_match_type != eNameMatchIgnore) |
| 676 | return false; |
| 677 | |
| 678 | if (m_match_info.ProcessIDIsValid()) |
| 679 | return false; |
| 680 | |
| 681 | if (m_match_info.ParentProcessIDIsValid()) |
| 682 | return false; |
| 683 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 684 | if (m_match_info.UserIDIsValid ()) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 685 | return false; |
| 686 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 687 | if (m_match_info.GroupIDIsValid ()) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 688 | return false; |
| 689 | |
| 690 | if (m_match_info.EffectiveUserIDIsValid ()) |
| 691 | return false; |
| 692 | |
| 693 | if (m_match_info.EffectiveGroupIDIsValid ()) |
| 694 | return false; |
| 695 | |
| 696 | if (m_match_info.GetArchitecture().IsValid()) |
| 697 | return false; |
| 698 | |
| 699 | if (m_match_all_users) |
| 700 | return false; |
| 701 | |
| 702 | return true; |
| 703 | |
| 704 | } |
| 705 | |
| 706 | void |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 707 | ProcessInstanceInfoMatch::Clear() |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 708 | { |
| 709 | m_match_info.Clear(); |
| 710 | m_name_match_type = eNameMatchIgnore; |
| 711 | m_match_all_users = false; |
| 712 | } |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 713 | |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 714 | ProcessSP |
| 715 | Process::FindPlugin (Target &target, const char *plugin_name, Listener &listener, const FileSpec *crash_file_path) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 716 | { |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 717 | ProcessSP process_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 718 | ProcessCreateInstance create_callback = NULL; |
| 719 | if (plugin_name) |
| 720 | { |
| 721 | create_callback = PluginManager::GetProcessCreateCallbackForPluginName (plugin_name); |
| 722 | if (create_callback) |
| 723 | { |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 724 | process_sp = create_callback(target, listener, crash_file_path); |
| 725 | if (process_sp) |
| 726 | { |
| 727 | if (!process_sp->CanDebug(target, true)) |
| 728 | process_sp.reset(); |
| 729 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | else |
| 733 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 734 | for (uint32_t idx = 0; (create_callback = PluginManager::GetProcessCreateCallbackAtIndex(idx)) != NULL; ++idx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 735 | { |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 736 | process_sp = create_callback(target, listener, crash_file_path); |
| 737 | if (process_sp) |
| 738 | { |
| 739 | if (!process_sp->CanDebug(target, false)) |
| 740 | process_sp.reset(); |
| 741 | else |
| 742 | break; |
| 743 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 744 | } |
| 745 | } |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 746 | return process_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Jim Ingham | 5a15e69 | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 749 | ConstString & |
| 750 | Process::GetStaticBroadcasterClass () |
| 751 | { |
| 752 | static ConstString class_name ("lldb.process"); |
| 753 | return class_name; |
| 754 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 755 | |
| 756 | //---------------------------------------------------------------------- |
| 757 | // Process constructor |
| 758 | //---------------------------------------------------------------------- |
| 759 | Process::Process(Target &target, Listener &listener) : |
| 760 | UserID (LLDB_INVALID_PROCESS_ID), |
Jim Ingham | 5a15e69 | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 761 | Broadcaster (&(target.GetDebugger()), "lldb.process"), |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 762 | ProcessInstanceSettings (GetSettingsController()), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 763 | m_target (target), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 764 | m_public_state (eStateUnloaded), |
| 765 | m_private_state (eStateUnloaded), |
Jim Ingham | 5a15e69 | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 766 | m_private_state_broadcaster (NULL, "lldb.process.internal_state_broadcaster"), |
| 767 | m_private_state_control_broadcaster (NULL, "lldb.process.internal_state_control_broadcaster"), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 768 | m_private_state_listener ("lldb.process.internal_state_listener"), |
| 769 | m_private_state_control_wait(), |
| 770 | m_private_state_thread (LLDB_INVALID_HOST_THREAD), |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 771 | m_mod_id (), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 772 | m_thread_index_id (0), |
| 773 | m_exit_status (-1), |
| 774 | m_exit_string (), |
| 775 | m_thread_list (this), |
| 776 | m_notifications (), |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 777 | m_image_tokens (), |
| 778 | m_listener (listener), |
| 779 | m_breakpoint_site_list (), |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 780 | m_dynamic_checkers_ap (), |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 781 | m_unix_signals (), |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 782 | m_abi_sp (), |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 783 | m_process_input_reader (), |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 784 | m_stdio_communication ("process.stdio"), |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 785 | m_stdio_communication_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 786 | m_stdout_data (), |
Greg Clayton | bd06ff4 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 787 | m_stderr_data (), |
Greg Clayton | 613b873 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 788 | m_memory_cache (*this), |
| 789 | m_allocated_memory_cache (*this), |
Greg Clayton | ffa43a6 | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 790 | m_should_detach (false), |
Sean Callanan | 6cf6c47 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 791 | m_next_event_action_ap(), |
Bill Wendling | ce96dad | 2012-04-06 00:10:21 +0000 | [diff] [blame] | 792 | m_run_lock (), |
| 793 | m_can_jit(eCanJITDontKnow) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 794 | { |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 795 | UpdateInstanceName(); |
Jim Ingham | 5a15e69 | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 796 | |
| 797 | CheckInWithManager (); |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 798 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 799 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 800 | if (log) |
| 801 | log->Printf ("%p Process::Process()", this); |
| 802 | |
Greg Clayton | 49ce682 | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 803 | SetEventName (eBroadcastBitStateChanged, "state-changed"); |
| 804 | SetEventName (eBroadcastBitInterrupt, "interrupt"); |
| 805 | SetEventName (eBroadcastBitSTDOUT, "stdout-available"); |
| 806 | SetEventName (eBroadcastBitSTDERR, "stderr-available"); |
| 807 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 808 | listener.StartListeningForEvents (this, |
| 809 | eBroadcastBitStateChanged | |
| 810 | eBroadcastBitInterrupt | |
| 811 | eBroadcastBitSTDOUT | |
| 812 | eBroadcastBitSTDERR); |
| 813 | |
| 814 | m_private_state_listener.StartListeningForEvents(&m_private_state_broadcaster, |
| 815 | eBroadcastBitStateChanged); |
| 816 | |
| 817 | m_private_state_listener.StartListeningForEvents(&m_private_state_control_broadcaster, |
| 818 | eBroadcastInternalStateControlStop | |
| 819 | eBroadcastInternalStateControlPause | |
| 820 | eBroadcastInternalStateControlResume); |
| 821 | } |
| 822 | |
| 823 | //---------------------------------------------------------------------- |
| 824 | // Destructor |
| 825 | //---------------------------------------------------------------------- |
| 826 | Process::~Process() |
| 827 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 828 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 829 | if (log) |
| 830 | log->Printf ("%p Process::~Process()", this); |
| 831 | StopPrivateStateThread(); |
| 832 | } |
| 833 | |
| 834 | void |
| 835 | Process::Finalize() |
| 836 | { |
Greg Clayton | ffa43a6 | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 837 | switch (GetPrivateState()) |
| 838 | { |
| 839 | case eStateConnected: |
| 840 | case eStateAttaching: |
| 841 | case eStateLaunching: |
| 842 | case eStateStopped: |
| 843 | case eStateRunning: |
| 844 | case eStateStepping: |
| 845 | case eStateCrashed: |
| 846 | case eStateSuspended: |
| 847 | if (GetShouldDetach()) |
| 848 | Detach(); |
| 849 | else |
| 850 | Destroy(); |
| 851 | break; |
| 852 | |
| 853 | case eStateInvalid: |
| 854 | case eStateUnloaded: |
| 855 | case eStateDetached: |
| 856 | case eStateExited: |
| 857 | break; |
| 858 | } |
| 859 | |
Greg Clayton | 2f57db0 | 2011-10-01 00:45:15 +0000 | [diff] [blame] | 860 | // Clear our broadcaster before we proceed with destroying |
| 861 | Broadcaster::Clear(); |
| 862 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 863 | // Do any cleanup needed prior to being destructed... Subclasses |
| 864 | // that override this method should call this superclass method as well. |
Jim Ingham | 88fa7bd | 2011-02-16 17:54:55 +0000 | [diff] [blame] | 865 | |
| 866 | // We need to destroy the loader before the derived Process class gets destroyed |
| 867 | // since it is very likely that undoing the loader will require access to the real process. |
Greg Clayton | 182be6a | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 868 | m_dynamic_checkers_ap.reset(); |
| 869 | m_abi_sp.reset(); |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 870 | m_os_ap.reset(); |
Greg Clayton | 182be6a | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 871 | m_dyld_ap.reset(); |
Greg Clayton | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 872 | m_thread_list.Destroy(); |
Greg Clayton | 182be6a | 2012-01-20 23:08:34 +0000 | [diff] [blame] | 873 | std::vector<Notifications> empty_notifications; |
| 874 | m_notifications.swap(empty_notifications); |
| 875 | m_image_tokens.clear(); |
| 876 | m_memory_cache.Clear(); |
| 877 | m_allocated_memory_cache.Clear(); |
| 878 | m_language_runtimes.clear(); |
| 879 | m_next_event_action_ap.reset(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | void |
| 883 | Process::RegisterNotificationCallbacks (const Notifications& callbacks) |
| 884 | { |
| 885 | m_notifications.push_back(callbacks); |
| 886 | if (callbacks.initialize != NULL) |
| 887 | callbacks.initialize (callbacks.baton, this); |
| 888 | } |
| 889 | |
| 890 | bool |
| 891 | Process::UnregisterNotificationCallbacks(const Notifications& callbacks) |
| 892 | { |
| 893 | std::vector<Notifications>::iterator pos, end = m_notifications.end(); |
| 894 | for (pos = m_notifications.begin(); pos != end; ++pos) |
| 895 | { |
| 896 | if (pos->baton == callbacks.baton && |
| 897 | pos->initialize == callbacks.initialize && |
| 898 | pos->process_state_changed == callbacks.process_state_changed) |
| 899 | { |
| 900 | m_notifications.erase(pos); |
| 901 | return true; |
| 902 | } |
| 903 | } |
| 904 | return false; |
| 905 | } |
| 906 | |
| 907 | void |
| 908 | Process::SynchronouslyNotifyStateChanged (StateType state) |
| 909 | { |
| 910 | std::vector<Notifications>::iterator notification_pos, notification_end = m_notifications.end(); |
| 911 | for (notification_pos = m_notifications.begin(); notification_pos != notification_end; ++notification_pos) |
| 912 | { |
| 913 | if (notification_pos->process_state_changed) |
| 914 | notification_pos->process_state_changed (notification_pos->baton, this, state); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | // FIXME: We need to do some work on events before the general Listener sees them. |
| 919 | // For instance if we are continuing from a breakpoint, we need to ensure that we do |
| 920 | // the little "insert real insn, step & stop" trick. But we can't do that when the |
| 921 | // event is delivered by the broadcaster - since that is done on the thread that is |
| 922 | // waiting for new events, so if we needed more than one event for our handling, we would |
| 923 | // stall. So instead we do it when we fetch the event off of the queue. |
| 924 | // |
| 925 | |
| 926 | StateType |
| 927 | Process::GetNextEvent (EventSP &event_sp) |
| 928 | { |
| 929 | StateType state = eStateInvalid; |
| 930 | |
| 931 | if (m_listener.GetNextEventForBroadcaster (this, event_sp) && event_sp) |
| 932 | state = Process::ProcessEventData::GetStateFromEvent (event_sp.get()); |
| 933 | |
| 934 | return state; |
| 935 | } |
| 936 | |
| 937 | |
| 938 | StateType |
| 939 | Process::WaitForProcessToStop (const TimeValue *timeout) |
| 940 | { |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 941 | // We can't just wait for a "stopped" event, because the stopped event may have restarted the target. |
| 942 | // We have to actually check each event, and in the case of a stopped event check the restarted flag |
| 943 | // on the event. |
| 944 | EventSP event_sp; |
| 945 | StateType state = GetState(); |
| 946 | // If we are exited or detached, we won't ever get back to any |
| 947 | // other valid state... |
| 948 | if (state == eStateDetached || state == eStateExited) |
| 949 | return state; |
| 950 | |
| 951 | while (state != eStateInvalid) |
| 952 | { |
| 953 | state = WaitForStateChangedEvents (timeout, event_sp); |
| 954 | switch (state) |
| 955 | { |
| 956 | case eStateCrashed: |
| 957 | case eStateDetached: |
| 958 | case eStateExited: |
| 959 | case eStateUnloaded: |
| 960 | return state; |
| 961 | case eStateStopped: |
| 962 | if (Process::ProcessEventData::GetRestartedFromEvent(event_sp.get())) |
| 963 | continue; |
| 964 | else |
| 965 | return state; |
| 966 | default: |
| 967 | continue; |
| 968 | } |
| 969 | } |
| 970 | return state; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | |
| 974 | StateType |
| 975 | Process::WaitForState |
| 976 | ( |
| 977 | const TimeValue *timeout, |
| 978 | const StateType *match_states, const uint32_t num_match_states |
| 979 | ) |
| 980 | { |
| 981 | EventSP event_sp; |
| 982 | uint32_t i; |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 983 | StateType state = GetState(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 984 | while (state != eStateInvalid) |
| 985 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 986 | // If we are exited or detached, we won't ever get back to any |
| 987 | // other valid state... |
| 988 | if (state == eStateDetached || state == eStateExited) |
| 989 | return state; |
| 990 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 991 | state = WaitForStateChangedEvents (timeout, event_sp); |
| 992 | |
| 993 | for (i=0; i<num_match_states; ++i) |
| 994 | { |
| 995 | if (match_states[i] == state) |
| 996 | return state; |
| 997 | } |
| 998 | } |
| 999 | return state; |
| 1000 | } |
| 1001 | |
Jim Ingham | 63e24d7 | 2010-10-11 23:53:14 +0000 | [diff] [blame] | 1002 | bool |
| 1003 | Process::HijackProcessEvents (Listener *listener) |
| 1004 | { |
| 1005 | if (listener != NULL) |
| 1006 | { |
| 1007 | return HijackBroadcaster(listener, eBroadcastBitStateChanged); |
| 1008 | } |
| 1009 | else |
| 1010 | return false; |
| 1011 | } |
| 1012 | |
| 1013 | void |
| 1014 | Process::RestoreProcessEvents () |
| 1015 | { |
| 1016 | RestoreBroadcaster(); |
| 1017 | } |
| 1018 | |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 1019 | bool |
| 1020 | Process::HijackPrivateProcessEvents (Listener *listener) |
| 1021 | { |
| 1022 | if (listener != NULL) |
| 1023 | { |
| 1024 | return m_private_state_broadcaster.HijackBroadcaster(listener, eBroadcastBitStateChanged); |
| 1025 | } |
| 1026 | else |
| 1027 | return false; |
| 1028 | } |
| 1029 | |
| 1030 | void |
| 1031 | Process::RestorePrivateProcessEvents () |
| 1032 | { |
| 1033 | m_private_state_broadcaster.RestoreBroadcaster(); |
| 1034 | } |
| 1035 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1036 | StateType |
| 1037 | Process::WaitForStateChangedEvents (const TimeValue *timeout, EventSP &event_sp) |
| 1038 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1039 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1040 | |
| 1041 | if (log) |
| 1042 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 1043 | |
| 1044 | StateType state = eStateInvalid; |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 1045 | if (m_listener.WaitForEventForBroadcasterWithType (timeout, |
| 1046 | this, |
| 1047 | eBroadcastBitStateChanged, |
| 1048 | event_sp)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1049 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1050 | |
| 1051 | if (log) |
| 1052 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", |
| 1053 | __FUNCTION__, |
| 1054 | timeout, |
| 1055 | StateAsCString(state)); |
| 1056 | return state; |
| 1057 | } |
| 1058 | |
| 1059 | Event * |
| 1060 | Process::PeekAtStateChangedEvents () |
| 1061 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1062 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1063 | |
| 1064 | if (log) |
| 1065 | log->Printf ("Process::%s...", __FUNCTION__); |
| 1066 | |
| 1067 | Event *event_ptr; |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 1068 | event_ptr = m_listener.PeekAtNextEventForBroadcasterWithType (this, |
| 1069 | eBroadcastBitStateChanged); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1070 | if (log) |
| 1071 | { |
| 1072 | if (event_ptr) |
| 1073 | { |
| 1074 | log->Printf ("Process::%s (event_ptr) => %s", |
| 1075 | __FUNCTION__, |
| 1076 | StateAsCString(ProcessEventData::GetStateFromEvent (event_ptr))); |
| 1077 | } |
| 1078 | else |
| 1079 | { |
| 1080 | log->Printf ("Process::%s no events found", |
| 1081 | __FUNCTION__); |
| 1082 | } |
| 1083 | } |
| 1084 | return event_ptr; |
| 1085 | } |
| 1086 | |
| 1087 | StateType |
| 1088 | Process::WaitForStateChangedEventsPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 1089 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1090 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1091 | |
| 1092 | if (log) |
| 1093 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 1094 | |
| 1095 | StateType state = eStateInvalid; |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1096 | if (m_private_state_listener.WaitForEventForBroadcasterWithType (timeout, |
| 1097 | &m_private_state_broadcaster, |
| 1098 | eBroadcastBitStateChanged, |
| 1099 | event_sp)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1100 | state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 1101 | |
| 1102 | // This is a bit of a hack, but when we wait here we could very well return |
| 1103 | // to the command-line, and that could disable the log, which would render the |
| 1104 | // log we got above invalid. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1105 | if (log) |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1106 | { |
| 1107 | if (state == eStateInvalid) |
| 1108 | log->Printf ("Process::%s (timeout = %p, event_sp) => TIMEOUT", __FUNCTION__, timeout); |
| 1109 | else |
| 1110 | log->Printf ("Process::%s (timeout = %p, event_sp) => %s", __FUNCTION__, timeout, StateAsCString(state)); |
| 1111 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1112 | return state; |
| 1113 | } |
| 1114 | |
| 1115 | bool |
| 1116 | Process::WaitForEventsPrivate (const TimeValue *timeout, EventSP &event_sp, bool control_only) |
| 1117 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1118 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1119 | |
| 1120 | if (log) |
| 1121 | log->Printf ("Process::%s (timeout = %p, event_sp)...", __FUNCTION__, timeout); |
| 1122 | |
| 1123 | if (control_only) |
| 1124 | return m_private_state_listener.WaitForEventForBroadcaster(timeout, &m_private_state_control_broadcaster, event_sp); |
| 1125 | else |
| 1126 | return m_private_state_listener.WaitForEvent(timeout, event_sp); |
| 1127 | } |
| 1128 | |
| 1129 | bool |
| 1130 | Process::IsRunning () const |
| 1131 | { |
| 1132 | return StateIsRunningState (m_public_state.GetValue()); |
| 1133 | } |
| 1134 | |
| 1135 | int |
| 1136 | Process::GetExitStatus () |
| 1137 | { |
| 1138 | if (m_public_state.GetValue() == eStateExited) |
| 1139 | return m_exit_status; |
| 1140 | return -1; |
| 1141 | } |
| 1142 | |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1144 | const char * |
| 1145 | Process::GetExitDescription () |
| 1146 | { |
| 1147 | if (m_public_state.GetValue() == eStateExited && !m_exit_string.empty()) |
| 1148 | return m_exit_string.c_str(); |
| 1149 | return NULL; |
| 1150 | } |
| 1151 | |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1152 | bool |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1153 | Process::SetExitStatus (int status, const char *cstr) |
| 1154 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1155 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
| 1156 | if (log) |
| 1157 | log->Printf("Process::SetExitStatus (status=%i (0x%8.8x), description=%s%s%s)", |
| 1158 | status, status, |
| 1159 | cstr ? "\"" : "", |
| 1160 | cstr ? cstr : "NULL", |
| 1161 | cstr ? "\"" : ""); |
| 1162 | |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1163 | // We were already in the exited state |
| 1164 | if (m_private_state.GetValue() == eStateExited) |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1165 | { |
Greg Clayton | 644ddfb | 2011-01-26 23:47:29 +0000 | [diff] [blame] | 1166 | if (log) |
| 1167 | 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] | 1168 | return false; |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1169 | } |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1170 | |
| 1171 | m_exit_status = status; |
| 1172 | if (cstr) |
| 1173 | m_exit_string = cstr; |
| 1174 | else |
| 1175 | m_exit_string.clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1176 | |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1177 | DidExit (); |
Greg Clayton | 58e844b | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 1178 | |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 1179 | SetPrivateState (eStateExited); |
| 1180 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | // This static callback can be used to watch for local child processes on |
| 1184 | // the current host. The the child process exits, the process will be |
| 1185 | // found in the global target list (we want to be completely sure that the |
| 1186 | // lldb_private::Process doesn't go away before we can deliver the signal. |
| 1187 | bool |
Greg Clayton | 1c4642c | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1188 | Process::SetProcessExitStatus (void *callback_baton, |
| 1189 | lldb::pid_t pid, |
| 1190 | bool exited, |
| 1191 | int signo, // Zero for no signal |
| 1192 | int exit_status // Exit value of process if signal is zero |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1193 | ) |
| 1194 | { |
Greg Clayton | 1c4642c | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1195 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1196 | if (log) |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 1197 | log->Printf ("Process::SetProcessExitStatus (baton=%p, pid=%llu, exited=%i, signal=%i, exit_status=%i)\n", |
Greg Clayton | 1c4642c | 2011-11-16 05:37:56 +0000 | [diff] [blame] | 1198 | callback_baton, |
| 1199 | pid, |
| 1200 | exited, |
| 1201 | signo, |
| 1202 | exit_status); |
| 1203 | |
| 1204 | if (exited) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1205 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1206 | TargetSP target_sp(Debugger::FindTargetWithProcessID (pid)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1207 | if (target_sp) |
| 1208 | { |
| 1209 | ProcessSP process_sp (target_sp->GetProcessSP()); |
| 1210 | if (process_sp) |
| 1211 | { |
| 1212 | const char *signal_cstr = NULL; |
| 1213 | if (signo) |
| 1214 | signal_cstr = process_sp->GetUnixSignals().GetSignalAsCString (signo); |
| 1215 | |
| 1216 | process_sp->SetExitStatus (exit_status, signal_cstr); |
| 1217 | } |
| 1218 | } |
| 1219 | return true; |
| 1220 | } |
| 1221 | return false; |
| 1222 | } |
| 1223 | |
| 1224 | |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1225 | void |
| 1226 | Process::UpdateThreadListIfNeeded () |
| 1227 | { |
| 1228 | const uint32_t stop_id = GetStopID(); |
| 1229 | if (m_thread_list.GetSize(false) == 0 || stop_id != m_thread_list.GetStopID()) |
| 1230 | { |
Greg Clayton | 2020608 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1231 | const StateType state = GetPrivateState(); |
| 1232 | if (StateIsStoppedState (state, true)) |
| 1233 | { |
| 1234 | Mutex::Locker locker (m_thread_list.GetMutex ()); |
Greg Clayton | ffa43a6 | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 1235 | // m_thread_list does have its own mutex, but we need to |
| 1236 | // hold onto the mutex between the call to UpdateThreadList(...) |
| 1237 | // and the os->UpdateThreadList(...) so it doesn't change on us |
Greg Clayton | 2020608 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1238 | ThreadList new_thread_list(this); |
| 1239 | // Always update the thread list with the protocol specific |
Greg Clayton | ae93235 | 2012-04-10 00:18:59 +0000 | [diff] [blame] | 1240 | // thread list, but only update if "true" is returned |
| 1241 | if (UpdateThreadList (m_thread_list, new_thread_list)) |
| 1242 | { |
| 1243 | OperatingSystem *os = GetOperatingSystem (); |
| 1244 | if (os) |
| 1245 | os->UpdateThreadList (m_thread_list, new_thread_list); |
| 1246 | m_thread_list.Update (new_thread_list); |
| 1247 | m_thread_list.SetStopID (stop_id); |
| 1248 | } |
Greg Clayton | 2020608 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1249 | } |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1253 | uint32_t |
| 1254 | Process::GetNextThreadIndexID () |
| 1255 | { |
| 1256 | return ++m_thread_index_id; |
| 1257 | } |
| 1258 | |
| 1259 | StateType |
| 1260 | Process::GetState() |
| 1261 | { |
| 1262 | // If any other threads access this we will need a mutex for it |
| 1263 | return m_public_state.GetValue (); |
| 1264 | } |
| 1265 | |
| 1266 | void |
| 1267 | Process::SetPublicState (StateType new_state) |
| 1268 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1269 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1270 | if (log) |
| 1271 | log->Printf("Process::SetPublicState (%s)", StateAsCString(new_state)); |
Greg Clayton | a894fe7 | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1272 | const StateType old_state = m_public_state.GetValue(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1273 | m_public_state.SetValue (new_state); |
Greg Clayton | a894fe7 | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1274 | if (!IsHijackedForEvent(eBroadcastBitStateChanged)) |
| 1275 | { |
| 1276 | const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1277 | const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
| 1278 | if (old_state_is_stopped != new_state_is_stopped) |
| 1279 | { |
| 1280 | if (new_state_is_stopped) |
| 1281 | { |
| 1282 | if (log) |
| 1283 | log->Printf("Process::SetPublicState (%s) -- unlocking run lock", StateAsCString(new_state)); |
| 1284 | m_run_lock.WriteUnlock(); |
| 1285 | } |
| 1286 | else |
| 1287 | { |
| 1288 | if (log) |
| 1289 | log->Printf("Process::SetPublicState (%s) -- locking run lock", StateAsCString(new_state)); |
| 1290 | m_run_lock.WriteLock(); |
| 1291 | } |
| 1292 | } |
| 1293 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | StateType |
| 1297 | Process::GetPrivateState () |
| 1298 | { |
| 1299 | return m_private_state.GetValue(); |
| 1300 | } |
| 1301 | |
| 1302 | void |
| 1303 | Process::SetPrivateState (StateType new_state) |
| 1304 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 1305 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STATE | LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1306 | bool state_changed = false; |
| 1307 | |
| 1308 | if (log) |
| 1309 | log->Printf("Process::SetPrivateState (%s)", StateAsCString(new_state)); |
| 1310 | |
| 1311 | Mutex::Locker locker(m_private_state.GetMutex()); |
| 1312 | |
| 1313 | const StateType old_state = m_private_state.GetValueNoLock (); |
| 1314 | state_changed = old_state != new_state; |
Greg Clayton | a894fe7 | 2012-04-05 16:12:35 +0000 | [diff] [blame] | 1315 | // This code is left commented out in case we ever need to control |
| 1316 | // the private process state with another run lock. Right now it doesn't |
| 1317 | // seem like we need to do this, but if we ever do, we can uncomment and |
| 1318 | // use this code. |
| 1319 | // const bool old_state_is_stopped = StateIsStoppedState(old_state, false); |
| 1320 | // const bool new_state_is_stopped = StateIsStoppedState(new_state, false); |
| 1321 | // if (old_state_is_stopped != new_state_is_stopped) |
| 1322 | // { |
| 1323 | // if (new_state_is_stopped) |
| 1324 | // m_private_run_lock.WriteUnlock(); |
| 1325 | // else |
| 1326 | // m_private_run_lock.WriteLock(); |
| 1327 | // } |
| 1328 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1329 | if (state_changed) |
| 1330 | { |
| 1331 | m_private_state.SetValueNoLock (new_state); |
Greg Clayton | 2020608 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 1332 | if (StateIsStoppedState(new_state, false)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1333 | { |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1334 | m_mod_id.BumpStopID(); |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1335 | m_memory_cache.Clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1336 | if (log) |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 1337 | log->Printf("Process::SetPrivateState (%s) stop_id = %u", StateAsCString(new_state), m_mod_id.GetStopID()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1338 | } |
| 1339 | // Use our target to get a shared pointer to ourselves... |
| 1340 | m_private_state_broadcaster.BroadcastEvent (eBroadcastBitStateChanged, new ProcessEventData (GetTarget().GetProcessSP(), new_state)); |
| 1341 | } |
| 1342 | else |
| 1343 | { |
| 1344 | if (log) |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1345 | log->Printf("Process::SetPrivateState (%s) state didn't change. Ignoring...", StateAsCString(new_state)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1346 | } |
| 1347 | } |
| 1348 | |
Jim Ingham | 0296fe7 | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 1349 | void |
| 1350 | Process::SetRunningUserExpression (bool on) |
| 1351 | { |
| 1352 | m_mod_id.SetRunningUserExpression (on); |
| 1353 | } |
| 1354 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1355 | addr_t |
| 1356 | Process::GetImageInfoAddress() |
| 1357 | { |
| 1358 | return LLDB_INVALID_ADDRESS; |
| 1359 | } |
| 1360 | |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1361 | //---------------------------------------------------------------------- |
| 1362 | // LoadImage |
| 1363 | // |
| 1364 | // This function provides a default implementation that works for most |
| 1365 | // unix variants. Any Process subclasses that need to do shared library |
| 1366 | // loading differently should override LoadImage and UnloadImage and |
| 1367 | // do what is needed. |
| 1368 | //---------------------------------------------------------------------- |
| 1369 | uint32_t |
| 1370 | Process::LoadImage (const FileSpec &image_spec, Error &error) |
| 1371 | { |
| 1372 | DynamicLoader *loader = GetDynamicLoader(); |
| 1373 | if (loader) |
| 1374 | { |
| 1375 | error = loader->CanLoadImage(); |
| 1376 | if (error.Fail()) |
| 1377 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1378 | } |
| 1379 | |
| 1380 | if (error.Success()) |
| 1381 | { |
| 1382 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1383 | |
| 1384 | if (thread_sp) |
| 1385 | { |
| 1386 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
| 1387 | |
| 1388 | if (frame_sp) |
| 1389 | { |
| 1390 | ExecutionContext exe_ctx; |
| 1391 | frame_sp->CalculateExecutionContext (exe_ctx); |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 1392 | bool unwind_on_error = true; |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1393 | StreamString expr; |
| 1394 | char path[PATH_MAX]; |
| 1395 | image_spec.GetPath(path, sizeof(path)); |
| 1396 | expr.Printf("dlopen (\"%s\", 2)", path); |
| 1397 | 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] | 1398 | lldb::ValueObjectSP result_valobj_sp; |
Sean Callanan | daa6efe | 2011-12-21 22:22:58 +0000 | [diff] [blame] | 1399 | ClangUserExpression::Evaluate (exe_ctx, eExecutionPolicyAlways, lldb::eLanguageTypeUnknown, ClangUserExpression::eResultTypeAny, unwind_on_error, expr.GetData(), prefix, result_valobj_sp); |
Johnny Chen | b14ec34 | 2011-09-09 00:01:43 +0000 | [diff] [blame] | 1400 | error = result_valobj_sp->GetError(); |
| 1401 | if (error.Success()) |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1402 | { |
| 1403 | Scalar scalar; |
Jim Ingham | fa3a16a | 2011-03-31 00:19:25 +0000 | [diff] [blame] | 1404 | if (result_valobj_sp->ResolveValue (scalar)) |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1405 | { |
| 1406 | addr_t image_ptr = scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 1407 | if (image_ptr != 0 && image_ptr != LLDB_INVALID_ADDRESS) |
| 1408 | { |
| 1409 | uint32_t image_token = m_image_tokens.size(); |
| 1410 | m_image_tokens.push_back (image_ptr); |
| 1411 | return image_token; |
| 1412 | } |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | } |
| 1417 | } |
| 1418 | return LLDB_INVALID_IMAGE_TOKEN; |
| 1419 | } |
| 1420 | |
| 1421 | //---------------------------------------------------------------------- |
| 1422 | // UnloadImage |
| 1423 | // |
| 1424 | // This function provides a default implementation that works for most |
| 1425 | // unix variants. Any Process subclasses that need to do shared library |
| 1426 | // loading differently should override LoadImage and UnloadImage and |
| 1427 | // do what is needed. |
| 1428 | //---------------------------------------------------------------------- |
| 1429 | Error |
| 1430 | Process::UnloadImage (uint32_t image_token) |
| 1431 | { |
| 1432 | Error error; |
| 1433 | if (image_token < m_image_tokens.size()) |
| 1434 | { |
| 1435 | const addr_t image_addr = m_image_tokens[image_token]; |
| 1436 | if (image_addr == LLDB_INVALID_ADDRESS) |
| 1437 | { |
| 1438 | error.SetErrorString("image already unloaded"); |
| 1439 | } |
| 1440 | else |
| 1441 | { |
| 1442 | DynamicLoader *loader = GetDynamicLoader(); |
| 1443 | if (loader) |
| 1444 | error = loader->CanLoadImage(); |
| 1445 | |
| 1446 | if (error.Success()) |
| 1447 | { |
| 1448 | ThreadSP thread_sp(GetThreadList ().GetSelectedThread()); |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1449 | |
| 1450 | if (thread_sp) |
| 1451 | { |
| 1452 | StackFrameSP frame_sp (thread_sp->GetStackFrameAtIndex (0)); |
| 1453 | |
| 1454 | if (frame_sp) |
| 1455 | { |
| 1456 | ExecutionContext exe_ctx; |
| 1457 | frame_sp->CalculateExecutionContext (exe_ctx); |
Jim Ingham | ea9d426 | 2010-11-05 19:25:48 +0000 | [diff] [blame] | 1458 | bool unwind_on_error = true; |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1459 | StreamString expr; |
| 1460 | expr.Printf("dlclose ((void *)0x%llx)", image_addr); |
| 1461 | const char *prefix = "extern \"C\" int dlclose(void* handle);\n"; |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 1462 | lldb::ValueObjectSP result_valobj_sp; |
Sean Callanan | daa6efe | 2011-12-21 22:22:58 +0000 | [diff] [blame] | 1463 | ClangUserExpression::Evaluate (exe_ctx, eExecutionPolicyAlways, lldb::eLanguageTypeUnknown, ClangUserExpression::eResultTypeAny, unwind_on_error, expr.GetData(), prefix, result_valobj_sp); |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1464 | if (result_valobj_sp->GetError().Success()) |
| 1465 | { |
| 1466 | Scalar scalar; |
Jim Ingham | fa3a16a | 2011-03-31 00:19:25 +0000 | [diff] [blame] | 1467 | if (result_valobj_sp->ResolveValue (scalar)) |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1468 | { |
| 1469 | if (scalar.UInt(1)) |
| 1470 | { |
| 1471 | error.SetErrorStringWithFormat("expression failed: \"%s\"", expr.GetData()); |
| 1472 | } |
| 1473 | else |
| 1474 | { |
| 1475 | m_image_tokens[image_token] = LLDB_INVALID_ADDRESS; |
| 1476 | } |
| 1477 | } |
| 1478 | } |
| 1479 | else |
| 1480 | { |
| 1481 | error = result_valobj_sp->GetError(); |
| 1482 | } |
| 1483 | } |
| 1484 | } |
| 1485 | } |
| 1486 | } |
| 1487 | } |
| 1488 | else |
| 1489 | { |
| 1490 | error.SetErrorString("invalid image token"); |
| 1491 | } |
| 1492 | return error; |
| 1493 | } |
| 1494 | |
Greg Clayton | 75906e4 | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 1495 | const lldb::ABISP & |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1496 | Process::GetABI() |
| 1497 | { |
Greg Clayton | 75906e4 | 2011-05-11 18:39:18 +0000 | [diff] [blame] | 1498 | if (!m_abi_sp) |
| 1499 | m_abi_sp = ABI::FindPlugin(m_target.GetArchitecture()); |
| 1500 | return m_abi_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1503 | LanguageRuntime * |
Jim Ingham | e311766 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1504 | Process::GetLanguageRuntime(lldb::LanguageType language, bool retry_if_null) |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1505 | { |
| 1506 | LanguageRuntimeCollection::iterator pos; |
| 1507 | pos = m_language_runtimes.find (language); |
Jim Ingham | e311766 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1508 | if (pos == m_language_runtimes.end() || (retry_if_null && !(*pos).second)) |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1509 | { |
Jim Ingham | e311766 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1510 | lldb::LanguageRuntimeSP runtime_sp(LanguageRuntime::FindPlugin(this, language)); |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1511 | |
Jim Ingham | e311766 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1512 | m_language_runtimes[language] = runtime_sp; |
| 1513 | return runtime_sp.get(); |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1514 | } |
| 1515 | else |
| 1516 | return (*pos).second.get(); |
| 1517 | } |
| 1518 | |
| 1519 | CPPLanguageRuntime * |
Jim Ingham | e311766 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1520 | Process::GetCPPLanguageRuntime (bool retry_if_null) |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1521 | { |
Jim Ingham | e311766 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1522 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeC_plus_plus, retry_if_null); |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1523 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeC_plus_plus) |
| 1524 | return static_cast<CPPLanguageRuntime *> (runtime); |
| 1525 | return NULL; |
| 1526 | } |
| 1527 | |
| 1528 | ObjCLanguageRuntime * |
Jim Ingham | e311766 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1529 | Process::GetObjCLanguageRuntime (bool retry_if_null) |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1530 | { |
Jim Ingham | e311766 | 2012-03-10 00:22:19 +0000 | [diff] [blame] | 1531 | LanguageRuntime *runtime = GetLanguageRuntime(eLanguageTypeObjC, retry_if_null); |
Jim Ingham | 642036f | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 1532 | if (runtime != NULL && runtime->GetLanguageType() == eLanguageTypeObjC) |
| 1533 | return static_cast<ObjCLanguageRuntime *> (runtime); |
| 1534 | return NULL; |
| 1535 | } |
| 1536 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1537 | BreakpointSiteList & |
| 1538 | Process::GetBreakpointSiteList() |
| 1539 | { |
| 1540 | return m_breakpoint_site_list; |
| 1541 | } |
| 1542 | |
| 1543 | const BreakpointSiteList & |
| 1544 | Process::GetBreakpointSiteList() const |
| 1545 | { |
| 1546 | return m_breakpoint_site_list; |
| 1547 | } |
| 1548 | |
| 1549 | |
| 1550 | void |
| 1551 | Process::DisableAllBreakpointSites () |
| 1552 | { |
| 1553 | m_breakpoint_site_list.SetEnabledForAll (false); |
| 1554 | } |
| 1555 | |
| 1556 | Error |
| 1557 | Process::ClearBreakpointSiteByID (lldb::user_id_t break_id) |
| 1558 | { |
| 1559 | Error error (DisableBreakpointSiteByID (break_id)); |
| 1560 | |
| 1561 | if (error.Success()) |
| 1562 | m_breakpoint_site_list.Remove(break_id); |
| 1563 | |
| 1564 | return error; |
| 1565 | } |
| 1566 | |
| 1567 | Error |
| 1568 | Process::DisableBreakpointSiteByID (lldb::user_id_t break_id) |
| 1569 | { |
| 1570 | Error error; |
| 1571 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 1572 | if (bp_site_sp) |
| 1573 | { |
| 1574 | if (bp_site_sp->IsEnabled()) |
| 1575 | error = DisableBreakpoint (bp_site_sp.get()); |
| 1576 | } |
| 1577 | else |
| 1578 | { |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 1579 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %llu", break_id); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | return error; |
| 1583 | } |
| 1584 | |
| 1585 | Error |
| 1586 | Process::EnableBreakpointSiteByID (lldb::user_id_t break_id) |
| 1587 | { |
| 1588 | Error error; |
| 1589 | BreakpointSiteSP bp_site_sp = m_breakpoint_site_list.FindByID (break_id); |
| 1590 | if (bp_site_sp) |
| 1591 | { |
| 1592 | if (!bp_site_sp->IsEnabled()) |
| 1593 | error = EnableBreakpoint (bp_site_sp.get()); |
| 1594 | } |
| 1595 | else |
| 1596 | { |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 1597 | error.SetErrorStringWithFormat("invalid breakpoint site ID: %llu", break_id); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1598 | } |
| 1599 | return error; |
| 1600 | } |
| 1601 | |
Stephen Wilson | 3fd1f36 | 2010-07-17 00:56:13 +0000 | [diff] [blame] | 1602 | lldb::break_id_t |
Greg Clayton | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 1603 | Process::CreateBreakpointSite (const BreakpointLocationSP &owner, bool use_hardware) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1604 | { |
Greg Clayton | 265ab33 | 2011-05-19 18:17:41 +0000 | [diff] [blame] | 1605 | const addr_t load_addr = owner->GetAddress().GetOpcodeLoadAddress (&m_target); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1606 | if (load_addr != LLDB_INVALID_ADDRESS) |
| 1607 | { |
| 1608 | BreakpointSiteSP bp_site_sp; |
| 1609 | |
| 1610 | // Look up this breakpoint site. If it exists, then add this new owner, otherwise |
| 1611 | // create a new breakpoint site and add it. |
| 1612 | |
| 1613 | bp_site_sp = m_breakpoint_site_list.FindByAddress (load_addr); |
| 1614 | |
| 1615 | if (bp_site_sp) |
| 1616 | { |
| 1617 | bp_site_sp->AddOwner (owner); |
| 1618 | owner->SetBreakpointSite (bp_site_sp); |
| 1619 | return bp_site_sp->GetID(); |
| 1620 | } |
| 1621 | else |
| 1622 | { |
| 1623 | bp_site_sp.reset (new BreakpointSite (&m_breakpoint_site_list, owner, load_addr, LLDB_INVALID_THREAD_ID, use_hardware)); |
| 1624 | if (bp_site_sp) |
| 1625 | { |
| 1626 | if (EnableBreakpoint (bp_site_sp.get()).Success()) |
| 1627 | { |
| 1628 | owner->SetBreakpointSite (bp_site_sp); |
| 1629 | return m_breakpoint_site_list.Add (bp_site_sp); |
| 1630 | } |
| 1631 | } |
| 1632 | } |
| 1633 | } |
| 1634 | // We failed to enable the breakpoint |
| 1635 | return LLDB_INVALID_BREAK_ID; |
| 1636 | |
| 1637 | } |
| 1638 | |
| 1639 | void |
| 1640 | Process::RemoveOwnerFromBreakpointSite (lldb::user_id_t owner_id, lldb::user_id_t owner_loc_id, BreakpointSiteSP &bp_site_sp) |
| 1641 | { |
| 1642 | uint32_t num_owners = bp_site_sp->RemoveOwner (owner_id, owner_loc_id); |
| 1643 | if (num_owners == 0) |
| 1644 | { |
| 1645 | DisableBreakpoint(bp_site_sp.get()); |
| 1646 | m_breakpoint_site_list.RemoveByAddress(bp_site_sp->GetLoadAddress()); |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | |
| 1651 | size_t |
| 1652 | Process::RemoveBreakpointOpcodesFromBuffer (addr_t bp_addr, size_t size, uint8_t *buf) const |
| 1653 | { |
| 1654 | size_t bytes_removed = 0; |
| 1655 | addr_t intersect_addr; |
| 1656 | size_t intersect_size; |
| 1657 | size_t opcode_offset; |
| 1658 | size_t idx; |
Greg Clayton | 987c7eb | 2011-09-17 08:33:22 +0000 | [diff] [blame] | 1659 | BreakpointSiteSP bp_sp; |
Jim Ingham | 82820f9 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 1660 | BreakpointSiteList bp_sites_in_range; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1661 | |
Jim Ingham | 82820f9 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 1662 | if (m_breakpoint_site_list.FindInRange (bp_addr, bp_addr + size, bp_sites_in_range)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1663 | { |
Greg Clayton | 987c7eb | 2011-09-17 08:33:22 +0000 | [diff] [blame] | 1664 | for (idx = 0; (bp_sp = bp_sites_in_range.GetByIndex(idx)); ++idx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1665 | { |
Greg Clayton | 987c7eb | 2011-09-17 08:33:22 +0000 | [diff] [blame] | 1666 | if (bp_sp->GetType() == BreakpointSite::eSoftware) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1667 | { |
Greg Clayton | 987c7eb | 2011-09-17 08:33:22 +0000 | [diff] [blame] | 1668 | if (bp_sp->IntersectsRange(bp_addr, size, &intersect_addr, &intersect_size, &opcode_offset)) |
Jim Ingham | 82820f9 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 1669 | { |
| 1670 | assert(bp_addr <= intersect_addr && intersect_addr < bp_addr + size); |
| 1671 | assert(bp_addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= bp_addr + size); |
Greg Clayton | 987c7eb | 2011-09-17 08:33:22 +0000 | [diff] [blame] | 1672 | assert(opcode_offset + intersect_size <= bp_sp->GetByteSize()); |
Jim Ingham | 82820f9 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 1673 | size_t buf_offset = intersect_addr - bp_addr; |
Greg Clayton | 987c7eb | 2011-09-17 08:33:22 +0000 | [diff] [blame] | 1674 | ::memcpy(buf + buf_offset, bp_sp->GetSavedOpcodeBytes() + opcode_offset, intersect_size); |
Jim Ingham | 82820f9 | 2011-06-29 19:42:28 +0000 | [diff] [blame] | 1675 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1676 | } |
| 1677 | } |
| 1678 | } |
| 1679 | return bytes_removed; |
| 1680 | } |
| 1681 | |
| 1682 | |
Greg Clayton | b1888f2 | 2011-03-19 01:12:21 +0000 | [diff] [blame] | 1683 | |
| 1684 | size_t |
| 1685 | Process::GetSoftwareBreakpointTrapOpcode (BreakpointSite* bp_site) |
| 1686 | { |
| 1687 | PlatformSP platform_sp (m_target.GetPlatform()); |
| 1688 | if (platform_sp) |
| 1689 | return platform_sp->GetSoftwareBreakpointTrapOpcode (m_target, bp_site); |
| 1690 | return 0; |
| 1691 | } |
| 1692 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1693 | Error |
| 1694 | Process::EnableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 1695 | { |
| 1696 | Error error; |
| 1697 | assert (bp_site != NULL); |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1698 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1699 | const addr_t bp_addr = bp_site->GetLoadAddress(); |
| 1700 | if (log) |
| 1701 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx", bp_site->GetID(), (uint64_t)bp_addr); |
| 1702 | if (bp_site->IsEnabled()) |
| 1703 | { |
| 1704 | if (log) |
| 1705 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already enabled", bp_site->GetID(), (uint64_t)bp_addr); |
| 1706 | return error; |
| 1707 | } |
| 1708 | |
| 1709 | if (bp_addr == LLDB_INVALID_ADDRESS) |
| 1710 | { |
| 1711 | error.SetErrorString("BreakpointSite contains an invalid load address."); |
| 1712 | return error; |
| 1713 | } |
| 1714 | // Ask the lldb::Process subclass to fill in the correct software breakpoint |
| 1715 | // trap for the breakpoint site |
| 1716 | const size_t bp_opcode_size = GetSoftwareBreakpointTrapOpcode(bp_site); |
| 1717 | |
| 1718 | if (bp_opcode_size == 0) |
| 1719 | { |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 1720 | error.SetErrorStringWithFormat ("Process::GetSoftwareBreakpointTrapOpcode() returned zero, unable to get breakpoint trap for address 0x%llx", bp_addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1721 | } |
| 1722 | else |
| 1723 | { |
| 1724 | const uint8_t * const bp_opcode_bytes = bp_site->GetTrapOpcodeBytes(); |
| 1725 | |
| 1726 | if (bp_opcode_bytes == NULL) |
| 1727 | { |
| 1728 | error.SetErrorString ("BreakpointSite doesn't contain a valid breakpoint trap opcode."); |
| 1729 | return error; |
| 1730 | } |
| 1731 | |
| 1732 | // Save the original opcode by reading it |
| 1733 | if (DoReadMemory(bp_addr, bp_site->GetSavedOpcodeBytes(), bp_opcode_size, error) == bp_opcode_size) |
| 1734 | { |
| 1735 | // Write a software breakpoint in place of the original opcode |
| 1736 | if (DoWriteMemory(bp_addr, bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 1737 | { |
| 1738 | uint8_t verify_bp_opcode_bytes[64]; |
| 1739 | if (DoReadMemory(bp_addr, verify_bp_opcode_bytes, bp_opcode_size, error) == bp_opcode_size) |
| 1740 | { |
| 1741 | if (::memcmp(bp_opcode_bytes, verify_bp_opcode_bytes, bp_opcode_size) == 0) |
| 1742 | { |
| 1743 | bp_site->SetEnabled(true); |
| 1744 | bp_site->SetType (BreakpointSite::eSoftware); |
| 1745 | if (log) |
| 1746 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", |
| 1747 | bp_site->GetID(), |
| 1748 | (uint64_t)bp_addr); |
| 1749 | } |
| 1750 | else |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 1751 | error.SetErrorString("failed to verify the breakpoint trap in memory."); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1752 | } |
| 1753 | else |
| 1754 | error.SetErrorString("Unable to read memory to verify breakpoint trap."); |
| 1755 | } |
| 1756 | else |
| 1757 | error.SetErrorString("Unable to write breakpoint trap to memory."); |
| 1758 | } |
| 1759 | else |
| 1760 | error.SetErrorString("Unable to read memory at breakpoint address."); |
| 1761 | } |
Stephen Wilson | c2b9825 | 2011-01-12 04:20:03 +0000 | [diff] [blame] | 1762 | if (log && error.Fail()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1763 | log->Printf ("Process::EnableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s", |
| 1764 | bp_site->GetID(), |
| 1765 | (uint64_t)bp_addr, |
| 1766 | error.AsCString()); |
| 1767 | return error; |
| 1768 | } |
| 1769 | |
| 1770 | Error |
| 1771 | Process::DisableSoftwareBreakpoint (BreakpointSite *bp_site) |
| 1772 | { |
| 1773 | Error error; |
| 1774 | assert (bp_site != NULL); |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 1775 | LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1776 | addr_t bp_addr = bp_site->GetLoadAddress(); |
| 1777 | lldb::user_id_t breakID = bp_site->GetID(); |
| 1778 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 1779 | log->Printf ("Process::DisableBreakpoint (breakID = %llu) addr = 0x%llx", breakID, (uint64_t)bp_addr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1780 | |
| 1781 | if (bp_site->IsHardware()) |
| 1782 | { |
| 1783 | error.SetErrorString("Breakpoint site is a hardware breakpoint."); |
| 1784 | } |
| 1785 | else if (bp_site->IsEnabled()) |
| 1786 | { |
| 1787 | const size_t break_op_size = bp_site->GetByteSize(); |
| 1788 | const uint8_t * const break_op = bp_site->GetTrapOpcodeBytes(); |
| 1789 | if (break_op_size > 0) |
| 1790 | { |
| 1791 | // Clear a software breakoint instruction |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1792 | uint8_t curr_break_op[8]; |
Stephen Wilson | 141eeac | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 1793 | assert (break_op_size <= sizeof(curr_break_op)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1794 | bool break_op_found = false; |
| 1795 | |
| 1796 | // Read the breakpoint opcode |
| 1797 | if (DoReadMemory (bp_addr, curr_break_op, break_op_size, error) == break_op_size) |
| 1798 | { |
| 1799 | bool verify = false; |
| 1800 | // Make sure we have the a breakpoint opcode exists at this address |
| 1801 | if (::memcmp (curr_break_op, break_op, break_op_size) == 0) |
| 1802 | { |
| 1803 | break_op_found = true; |
| 1804 | // We found a valid breakpoint opcode at this address, now restore |
| 1805 | // the saved opcode. |
| 1806 | if (DoWriteMemory (bp_addr, bp_site->GetSavedOpcodeBytes(), break_op_size, error) == break_op_size) |
| 1807 | { |
| 1808 | verify = true; |
| 1809 | } |
| 1810 | else |
| 1811 | error.SetErrorString("Memory write failed when restoring original opcode."); |
| 1812 | } |
| 1813 | else |
| 1814 | { |
| 1815 | error.SetErrorString("Original breakpoint trap is no longer in memory."); |
| 1816 | // Set verify to true and so we can check if the original opcode has already been restored |
| 1817 | verify = true; |
| 1818 | } |
| 1819 | |
| 1820 | if (verify) |
| 1821 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1822 | uint8_t verify_opcode[8]; |
Stephen Wilson | 141eeac | 2010-07-20 18:41:11 +0000 | [diff] [blame] | 1823 | assert (break_op_size < sizeof(verify_opcode)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1824 | // Verify that our original opcode made it back to the inferior |
| 1825 | if (DoReadMemory (bp_addr, verify_opcode, break_op_size, error) == break_op_size) |
| 1826 | { |
| 1827 | // compare the memory we just read with the original opcode |
| 1828 | if (::memcmp (bp_site->GetSavedOpcodeBytes(), verify_opcode, break_op_size) == 0) |
| 1829 | { |
| 1830 | // SUCCESS |
| 1831 | bp_site->SetEnabled(false); |
| 1832 | if (log) |
| 1833 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- SUCCESS", bp_site->GetID(), (uint64_t)bp_addr); |
| 1834 | return error; |
| 1835 | } |
| 1836 | else |
| 1837 | { |
| 1838 | if (break_op_found) |
| 1839 | error.SetErrorString("Failed to restore original opcode."); |
| 1840 | } |
| 1841 | } |
| 1842 | else |
| 1843 | error.SetErrorString("Failed to read memory to verify that breakpoint trap was restored."); |
| 1844 | } |
| 1845 | } |
| 1846 | else |
| 1847 | error.SetErrorString("Unable to read memory that should contain the breakpoint trap."); |
| 1848 | } |
| 1849 | } |
| 1850 | else |
| 1851 | { |
| 1852 | if (log) |
| 1853 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- already disabled", bp_site->GetID(), (uint64_t)bp_addr); |
| 1854 | return error; |
| 1855 | } |
| 1856 | |
| 1857 | if (log) |
| 1858 | log->Printf ("Process::DisableSoftwareBreakpoint (site_id = %d) addr = 0x%llx -- FAILED: %s", |
| 1859 | bp_site->GetID(), |
| 1860 | (uint64_t)bp_addr, |
| 1861 | error.AsCString()); |
| 1862 | return error; |
| 1863 | |
| 1864 | } |
| 1865 | |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1866 | // Comment out line below to disable memory caching |
| 1867 | #define ENABLE_MEMORY_CACHING |
| 1868 | // Uncomment to verify memory caching works after making changes to caching code |
| 1869 | //#define VERIFY_MEMORY_READS |
| 1870 | |
| 1871 | #if defined (ENABLE_MEMORY_CACHING) |
| 1872 | |
| 1873 | #if defined (VERIFY_MEMORY_READS) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1874 | |
| 1875 | size_t |
| 1876 | Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 1877 | { |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1878 | // Memory caching is enabled, with debug verification |
| 1879 | if (buf && size) |
| 1880 | { |
| 1881 | // Uncomment the line below to make sure memory caching is working. |
| 1882 | // I ran this through the test suite and got no assertions, so I am |
| 1883 | // pretty confident this is working well. If any changes are made to |
| 1884 | // memory caching, uncomment the line below and test your changes! |
| 1885 | |
| 1886 | // Verify all memory reads by using the cache first, then redundantly |
| 1887 | // reading the same memory from the inferior and comparing to make sure |
| 1888 | // everything is exactly the same. |
| 1889 | std::string verify_buf (size, '\0'); |
| 1890 | assert (verify_buf.size() == size); |
| 1891 | const size_t cache_bytes_read = m_memory_cache.Read (this, addr, buf, size, error); |
| 1892 | Error verify_error; |
| 1893 | const size_t verify_bytes_read = ReadMemoryFromInferior (addr, const_cast<char *>(verify_buf.data()), verify_buf.size(), verify_error); |
| 1894 | assert (cache_bytes_read == verify_bytes_read); |
| 1895 | assert (memcmp(buf, verify_buf.data(), verify_buf.size()) == 0); |
| 1896 | assert (verify_error.Success() == error.Success()); |
| 1897 | return cache_bytes_read; |
| 1898 | } |
| 1899 | return 0; |
| 1900 | } |
| 1901 | |
| 1902 | #else // #if defined (VERIFY_MEMORY_READS) |
| 1903 | |
| 1904 | size_t |
| 1905 | Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 1906 | { |
| 1907 | // Memory caching enabled, no verification |
Greg Clayton | 613b873 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 1908 | return m_memory_cache.Read (addr, buf, size, error); |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | #endif // #else for #if defined (VERIFY_MEMORY_READS) |
| 1912 | |
| 1913 | #else // #if defined (ENABLE_MEMORY_CACHING) |
| 1914 | |
| 1915 | size_t |
| 1916 | Process::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) |
| 1917 | { |
| 1918 | // Memory caching is disabled |
| 1919 | return ReadMemoryFromInferior (addr, buf, size, error); |
| 1920 | } |
| 1921 | |
| 1922 | #endif // #else for #if defined (ENABLE_MEMORY_CACHING) |
| 1923 | |
| 1924 | |
| 1925 | size_t |
Greg Clayton | 4a2e337 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 1926 | Process::ReadCStringFromMemory (addr_t addr, char *dst, size_t dst_max_len, Error &result_error) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1927 | { |
| 1928 | size_t total_cstr_len = 0; |
| 1929 | if (dst && dst_max_len) |
| 1930 | { |
Greg Clayton | 4a2e337 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 1931 | result_error.Clear(); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1932 | // NULL out everything just to be safe |
| 1933 | memset (dst, 0, dst_max_len); |
| 1934 | Error error; |
| 1935 | addr_t curr_addr = addr; |
| 1936 | const size_t cache_line_size = m_memory_cache.GetMemoryCacheLineSize(); |
| 1937 | size_t bytes_left = dst_max_len - 1; |
| 1938 | char *curr_dst = dst; |
| 1939 | |
| 1940 | while (bytes_left > 0) |
| 1941 | { |
| 1942 | addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size); |
| 1943 | addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left); |
| 1944 | size_t bytes_read = ReadMemory (curr_addr, curr_dst, bytes_to_read, error); |
| 1945 | |
| 1946 | if (bytes_read == 0) |
| 1947 | { |
Greg Clayton | 4a2e337 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 1948 | result_error = error; |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1949 | dst[total_cstr_len] = '\0'; |
| 1950 | break; |
| 1951 | } |
| 1952 | const size_t len = strlen(curr_dst); |
| 1953 | |
| 1954 | total_cstr_len += len; |
| 1955 | |
| 1956 | if (len < bytes_to_read) |
| 1957 | break; |
| 1958 | |
| 1959 | curr_dst += bytes_read; |
| 1960 | curr_addr += bytes_read; |
| 1961 | bytes_left -= bytes_read; |
| 1962 | } |
| 1963 | } |
Greg Clayton | 4a2e337 | 2011-12-15 03:14:23 +0000 | [diff] [blame] | 1964 | else |
| 1965 | { |
| 1966 | if (dst == NULL) |
| 1967 | result_error.SetErrorString("invalid arguments"); |
| 1968 | else |
| 1969 | result_error.Clear(); |
| 1970 | } |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1971 | return total_cstr_len; |
| 1972 | } |
| 1973 | |
| 1974 | size_t |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 1975 | Process::ReadMemoryFromInferior (addr_t addr, void *buf, size_t size, Error &error) |
| 1976 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1977 | if (buf == NULL || size == 0) |
| 1978 | return 0; |
| 1979 | |
| 1980 | size_t bytes_read = 0; |
| 1981 | uint8_t *bytes = (uint8_t *)buf; |
| 1982 | |
| 1983 | while (bytes_read < size) |
| 1984 | { |
| 1985 | const size_t curr_size = size - bytes_read; |
| 1986 | const size_t curr_bytes_read = DoReadMemory (addr + bytes_read, |
| 1987 | bytes + bytes_read, |
| 1988 | curr_size, |
| 1989 | error); |
| 1990 | bytes_read += curr_bytes_read; |
| 1991 | if (curr_bytes_read == curr_size || curr_bytes_read == 0) |
| 1992 | break; |
| 1993 | } |
| 1994 | |
| 1995 | // Replace any software breakpoint opcodes that fall into this range back |
| 1996 | // into "buf" before we return |
| 1997 | if (bytes_read > 0) |
| 1998 | RemoveBreakpointOpcodesFromBuffer (addr, bytes_read, (uint8_t *)buf); |
| 1999 | return bytes_read; |
| 2000 | } |
| 2001 | |
Greg Clayton | f72fdee | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2002 | uint64_t |
Greg Clayton | c0fa533 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2003 | Process::ReadUnsignedIntegerFromMemory (lldb::addr_t vm_addr, size_t integer_byte_size, uint64_t fail_value, Error &error) |
Greg Clayton | f72fdee | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2004 | { |
Greg Clayton | c0fa533 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2005 | Scalar scalar; |
| 2006 | if (ReadScalarIntegerFromMemory(vm_addr, integer_byte_size, false, scalar, error)) |
| 2007 | return scalar.ULongLong(fail_value); |
| 2008 | return fail_value; |
| 2009 | } |
| 2010 | |
| 2011 | addr_t |
| 2012 | Process::ReadPointerFromMemory (lldb::addr_t vm_addr, Error &error) |
| 2013 | { |
| 2014 | Scalar scalar; |
| 2015 | if (ReadScalarIntegerFromMemory(vm_addr, GetAddressByteSize(), false, scalar, error)) |
| 2016 | return scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 2017 | return LLDB_INVALID_ADDRESS; |
| 2018 | } |
| 2019 | |
| 2020 | |
| 2021 | bool |
| 2022 | Process::WritePointerToMemory (lldb::addr_t vm_addr, |
| 2023 | lldb::addr_t ptr_value, |
| 2024 | Error &error) |
| 2025 | { |
| 2026 | Scalar scalar; |
| 2027 | const uint32_t addr_byte_size = GetAddressByteSize(); |
| 2028 | if (addr_byte_size <= 4) |
| 2029 | scalar = (uint32_t)ptr_value; |
Greg Clayton | f72fdee | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2030 | else |
Greg Clayton | c0fa533 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2031 | scalar = ptr_value; |
| 2032 | return WriteScalarToMemory(vm_addr, scalar, addr_byte_size, error) == addr_byte_size; |
Greg Clayton | f72fdee | 2010-12-16 20:01:20 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2035 | size_t |
| 2036 | Process::WriteMemoryPrivate (addr_t addr, const void *buf, size_t size, Error &error) |
| 2037 | { |
| 2038 | size_t bytes_written = 0; |
| 2039 | const uint8_t *bytes = (const uint8_t *)buf; |
| 2040 | |
| 2041 | while (bytes_written < size) |
| 2042 | { |
| 2043 | const size_t curr_size = size - bytes_written; |
| 2044 | const size_t curr_bytes_written = DoWriteMemory (addr + bytes_written, |
| 2045 | bytes + bytes_written, |
| 2046 | curr_size, |
| 2047 | error); |
| 2048 | bytes_written += curr_bytes_written; |
| 2049 | if (curr_bytes_written == curr_size || curr_bytes_written == 0) |
| 2050 | break; |
| 2051 | } |
| 2052 | return bytes_written; |
| 2053 | } |
| 2054 | |
| 2055 | size_t |
| 2056 | Process::WriteMemory (addr_t addr, const void *buf, size_t size, Error &error) |
| 2057 | { |
Greg Clayton | fd11999 | 2011-01-07 06:08:19 +0000 | [diff] [blame] | 2058 | #if defined (ENABLE_MEMORY_CACHING) |
| 2059 | m_memory_cache.Flush (addr, size); |
| 2060 | #endif |
| 2061 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2062 | if (buf == NULL || size == 0) |
| 2063 | return 0; |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2064 | |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2065 | m_mod_id.BumpMemoryID(); |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2066 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2067 | // We need to write any data that would go where any current software traps |
| 2068 | // (enabled software breakpoints) any software traps (breakpoints) that we |
| 2069 | // may have placed in our tasks memory. |
| 2070 | |
| 2071 | BreakpointSiteList::collection::const_iterator iter = m_breakpoint_site_list.GetMap()->lower_bound (addr); |
| 2072 | BreakpointSiteList::collection::const_iterator end = m_breakpoint_site_list.GetMap()->end(); |
| 2073 | |
| 2074 | if (iter == end || iter->second->GetLoadAddress() > addr + size) |
Greg Clayton | c8bc1c3 | 2011-05-16 02:35:02 +0000 | [diff] [blame] | 2075 | return WriteMemoryPrivate (addr, buf, size, error); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2076 | |
| 2077 | BreakpointSiteList::collection::const_iterator pos; |
| 2078 | size_t bytes_written = 0; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 2079 | addr_t intersect_addr = 0; |
| 2080 | size_t intersect_size = 0; |
| 2081 | size_t opcode_offset = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2082 | const uint8_t *ubuf = (const uint8_t *)buf; |
| 2083 | |
| 2084 | for (pos = iter; pos != end; ++pos) |
| 2085 | { |
| 2086 | BreakpointSiteSP bp; |
| 2087 | bp = pos->second; |
| 2088 | |
| 2089 | assert(bp->IntersectsRange(addr, size, &intersect_addr, &intersect_size, &opcode_offset)); |
| 2090 | assert(addr <= intersect_addr && intersect_addr < addr + size); |
| 2091 | assert(addr < intersect_addr + intersect_size && intersect_addr + intersect_size <= addr + size); |
| 2092 | assert(opcode_offset + intersect_size <= bp->GetByteSize()); |
| 2093 | |
| 2094 | // Check for bytes before this breakpoint |
| 2095 | const addr_t curr_addr = addr + bytes_written; |
| 2096 | if (intersect_addr > curr_addr) |
| 2097 | { |
| 2098 | // There are some bytes before this breakpoint that we need to |
| 2099 | // just write to memory |
| 2100 | size_t curr_size = intersect_addr - curr_addr; |
| 2101 | size_t curr_bytes_written = WriteMemoryPrivate (curr_addr, |
| 2102 | ubuf + bytes_written, |
| 2103 | curr_size, |
| 2104 | error); |
| 2105 | bytes_written += curr_bytes_written; |
| 2106 | if (curr_bytes_written != curr_size) |
| 2107 | { |
| 2108 | // We weren't able to write all of the requested bytes, we |
| 2109 | // are done looping and will return the number of bytes that |
| 2110 | // we have written so far. |
| 2111 | break; |
| 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | // Now write any bytes that would cover up any software breakpoints |
| 2116 | // directly into the breakpoint opcode buffer |
| 2117 | ::memcpy(bp->GetSavedOpcodeBytes() + opcode_offset, ubuf + bytes_written, intersect_size); |
| 2118 | bytes_written += intersect_size; |
| 2119 | } |
| 2120 | |
| 2121 | // Write any remaining bytes after the last breakpoint if we have any left |
| 2122 | if (bytes_written < size) |
| 2123 | bytes_written += WriteMemoryPrivate (addr + bytes_written, |
| 2124 | ubuf + bytes_written, |
| 2125 | size - bytes_written, |
| 2126 | error); |
Jim Ingham | e41494a | 2011-04-16 00:01:13 +0000 | [diff] [blame] | 2127 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2128 | return bytes_written; |
| 2129 | } |
Greg Clayton | c0fa533 | 2011-05-22 22:46:53 +0000 | [diff] [blame] | 2130 | |
| 2131 | size_t |
| 2132 | Process::WriteScalarToMemory (addr_t addr, const Scalar &scalar, uint32_t byte_size, Error &error) |
| 2133 | { |
| 2134 | if (byte_size == UINT32_MAX) |
| 2135 | byte_size = scalar.GetByteSize(); |
| 2136 | if (byte_size > 0) |
| 2137 | { |
| 2138 | uint8_t buf[32]; |
| 2139 | const size_t mem_size = scalar.GetAsMemoryData (buf, byte_size, GetByteOrder(), error); |
| 2140 | if (mem_size > 0) |
| 2141 | return WriteMemory(addr, buf, mem_size, error); |
| 2142 | else |
| 2143 | error.SetErrorString ("failed to get scalar as memory data"); |
| 2144 | } |
| 2145 | else |
| 2146 | { |
| 2147 | error.SetErrorString ("invalid scalar value"); |
| 2148 | } |
| 2149 | return 0; |
| 2150 | } |
| 2151 | |
| 2152 | size_t |
| 2153 | Process::ReadScalarIntegerFromMemory (addr_t addr, |
| 2154 | uint32_t byte_size, |
| 2155 | bool is_signed, |
| 2156 | Scalar &scalar, |
| 2157 | Error &error) |
| 2158 | { |
| 2159 | uint64_t uval; |
| 2160 | |
| 2161 | if (byte_size <= sizeof(uval)) |
| 2162 | { |
| 2163 | size_t bytes_read = ReadMemory (addr, &uval, byte_size, error); |
| 2164 | if (bytes_read == byte_size) |
| 2165 | { |
| 2166 | DataExtractor data (&uval, sizeof(uval), GetByteOrder(), GetAddressByteSize()); |
| 2167 | uint32_t offset = 0; |
| 2168 | if (byte_size <= 4) |
| 2169 | scalar = data.GetMaxU32 (&offset, byte_size); |
| 2170 | else |
| 2171 | scalar = data.GetMaxU64 (&offset, byte_size); |
| 2172 | |
| 2173 | if (is_signed) |
| 2174 | scalar.SignExtend(byte_size * 8); |
| 2175 | return bytes_read; |
| 2176 | } |
| 2177 | } |
| 2178 | else |
| 2179 | { |
| 2180 | error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size); |
| 2181 | } |
| 2182 | return 0; |
| 2183 | } |
| 2184 | |
Greg Clayton | 613b873 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2185 | #define USE_ALLOCATE_MEMORY_CACHE 1 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2186 | addr_t |
| 2187 | Process::AllocateMemory(size_t size, uint32_t permissions, Error &error) |
| 2188 | { |
Jim Ingham | e6bd142 | 2011-06-20 17:32:44 +0000 | [diff] [blame] | 2189 | if (GetPrivateState() != eStateStopped) |
| 2190 | return LLDB_INVALID_ADDRESS; |
| 2191 | |
Greg Clayton | 613b873 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2192 | #if defined (USE_ALLOCATE_MEMORY_CACHE) |
| 2193 | return m_allocated_memory_cache.AllocateMemory(size, permissions, error); |
| 2194 | #else |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2195 | addr_t allocated_addr = DoAllocateMemory (size, permissions, error); |
| 2196 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2197 | if (log) |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2198 | log->Printf("Process::AllocateMemory(size=%4zu, permissions=%s) => 0x%16.16llx (m_stop_id = %u m_memory_id = %u)", |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2199 | size, |
Greg Clayton | 613b873 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2200 | GetPermissionsAsCString (permissions), |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2201 | (uint64_t)allocated_addr, |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2202 | m_mod_id.GetStopID(), |
| 2203 | m_mod_id.GetMemoryID()); |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2204 | return allocated_addr; |
Greg Clayton | 613b873 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2205 | #endif |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Sean Callanan | 6cf6c47 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2208 | bool |
| 2209 | Process::CanJIT () |
| 2210 | { |
Sean Callanan | 04200f6 | 2012-02-14 22:50:38 +0000 | [diff] [blame] | 2211 | if (m_can_jit == eCanJITDontKnow) |
| 2212 | { |
| 2213 | Error err; |
| 2214 | |
| 2215 | uint64_t allocated_memory = AllocateMemory(8, |
| 2216 | ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable, |
| 2217 | err); |
| 2218 | |
| 2219 | if (err.Success()) |
| 2220 | m_can_jit = eCanJITYes; |
| 2221 | else |
| 2222 | m_can_jit = eCanJITNo; |
| 2223 | |
| 2224 | DeallocateMemory (allocated_memory); |
| 2225 | } |
| 2226 | |
Sean Callanan | 6cf6c47 | 2011-09-20 23:01:51 +0000 | [diff] [blame] | 2227 | return m_can_jit == eCanJITYes; |
| 2228 | } |
| 2229 | |
| 2230 | void |
| 2231 | Process::SetCanJIT (bool can_jit) |
| 2232 | { |
| 2233 | m_can_jit = (can_jit ? eCanJITYes : eCanJITNo); |
| 2234 | } |
| 2235 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2236 | Error |
| 2237 | Process::DeallocateMemory (addr_t ptr) |
| 2238 | { |
Greg Clayton | 613b873 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2239 | Error error; |
| 2240 | #if defined (USE_ALLOCATE_MEMORY_CACHE) |
| 2241 | if (!m_allocated_memory_cache.DeallocateMemory(ptr)) |
| 2242 | { |
| 2243 | error.SetErrorStringWithFormat ("deallocation of memory at 0x%llx failed.", (uint64_t)ptr); |
| 2244 | } |
| 2245 | #else |
| 2246 | error = DoDeallocateMemory (ptr); |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2247 | |
| 2248 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2249 | if (log) |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2250 | log->Printf("Process::DeallocateMemory(addr=0x%16.16llx) => err = %s (m_stop_id = %u, m_memory_id = %u)", |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2251 | ptr, |
| 2252 | error.AsCString("SUCCESS"), |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2253 | m_mod_id.GetStopID(), |
| 2254 | m_mod_id.GetMemoryID()); |
Greg Clayton | 613b873 | 2011-05-17 03:37:42 +0000 | [diff] [blame] | 2255 | #endif |
Greg Clayton | 2860ba9 | 2011-01-23 19:58:49 +0000 | [diff] [blame] | 2256 | return error; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2259 | ModuleSP |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2260 | Process::ReadModuleFromMemory (const FileSpec& file_spec, |
| 2261 | lldb::addr_t header_addr, |
| 2262 | bool add_image_to_target, |
| 2263 | bool load_sections_in_target) |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2264 | { |
Greg Clayton | 6c5438b | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2265 | ModuleSP module_sp (new Module (file_spec, ArchSpec())); |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2266 | if (module_sp) |
| 2267 | { |
Greg Clayton | 6c5438b | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2268 | Error error; |
| 2269 | ObjectFile *objfile = module_sp->GetMemoryObjectFile (shared_from_this(), header_addr, error); |
| 2270 | if (objfile) |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2271 | { |
Greg Clayton | 6c5438b | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2272 | if (add_image_to_target) |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2273 | { |
Greg Clayton | 6c5438b | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2274 | m_target.GetImages().Append(module_sp); |
| 2275 | if (load_sections_in_target) |
| 2276 | { |
| 2277 | bool changed = false; |
| 2278 | module_sp->SetLoadAddress (m_target, 0, changed); |
| 2279 | } |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2280 | } |
Greg Clayton | 6c5438b | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2281 | return module_sp; |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2282 | } |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2283 | } |
Greg Clayton | 6c5438b | 2012-02-24 21:55:59 +0000 | [diff] [blame] | 2284 | return ModuleSP(); |
Greg Clayton | b5a8f14 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 2285 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2286 | |
| 2287 | Error |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2288 | Process::EnableWatchpoint (Watchpoint *watchpoint) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2289 | { |
| 2290 | Error error; |
| 2291 | error.SetErrorString("watchpoints are not supported"); |
| 2292 | return error; |
| 2293 | } |
| 2294 | |
| 2295 | Error |
Johnny Chen | ecd4feb | 2011-10-14 00:42:25 +0000 | [diff] [blame] | 2296 | Process::DisableWatchpoint (Watchpoint *watchpoint) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2297 | { |
| 2298 | Error error; |
| 2299 | error.SetErrorString("watchpoints are not supported"); |
| 2300 | return error; |
| 2301 | } |
| 2302 | |
| 2303 | StateType |
| 2304 | Process::WaitForProcessStopPrivate (const TimeValue *timeout, EventSP &event_sp) |
| 2305 | { |
| 2306 | StateType state; |
| 2307 | // Now wait for the process to launch and return control to us, and then |
| 2308 | // call DidLaunch: |
| 2309 | while (1) |
| 2310 | { |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2311 | event_sp.reset(); |
| 2312 | state = WaitForStateChangedEventsPrivate (timeout, event_sp); |
| 2313 | |
Greg Clayton | 2020608 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 2314 | if (StateIsStoppedState(state, false)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2315 | break; |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 2316 | |
| 2317 | // If state is invalid, then we timed out |
| 2318 | if (state == eStateInvalid) |
| 2319 | break; |
| 2320 | |
| 2321 | if (event_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2322 | HandlePrivateEvent (event_sp); |
| 2323 | } |
| 2324 | return state; |
| 2325 | } |
| 2326 | |
| 2327 | Error |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 2328 | Process::Launch (const ProcessLaunchInfo &launch_info) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2329 | { |
| 2330 | Error error; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2331 | m_abi_sp.reset(); |
Greg Clayton | 75c703d | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2332 | m_dyld_ap.reset(); |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 2333 | m_os_ap.reset(); |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 2334 | m_process_input_reader.reset(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2335 | |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 2336 | Module *exe_module = m_target.GetExecutableModulePointer(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2337 | if (exe_module) |
| 2338 | { |
Greg Clayton | 180546b | 2011-04-30 01:09:13 +0000 | [diff] [blame] | 2339 | char local_exec_file_path[PATH_MAX]; |
| 2340 | char platform_exec_file_path[PATH_MAX]; |
| 2341 | exe_module->GetFileSpec().GetPath(local_exec_file_path, sizeof(local_exec_file_path)); |
| 2342 | exe_module->GetPlatformFileSpec().GetPath(platform_exec_file_path, sizeof(platform_exec_file_path)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2343 | if (exe_module->GetFileSpec().Exists()) |
| 2344 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2345 | if (PrivateStateThreadIsValid ()) |
| 2346 | PausePrivateStateThread (); |
| 2347 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2348 | error = WillLaunch (exe_module); |
| 2349 | if (error.Success()) |
| 2350 | { |
Greg Clayton | d8c6253 | 2010-10-07 04:19:01 +0000 | [diff] [blame] | 2351 | SetPublicState (eStateLaunching); |
Greg Clayton | ffa43a6 | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 2352 | m_should_detach = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2353 | |
| 2354 | // Now launch using these arguments. |
Greg Clayton | 36bc5ea | 2011-11-03 21:22:33 +0000 | [diff] [blame] | 2355 | error = DoLaunch (exe_module, launch_info); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2356 | |
| 2357 | if (error.Fail()) |
| 2358 | { |
| 2359 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 2360 | { |
| 2361 | SetID (LLDB_INVALID_PROCESS_ID); |
| 2362 | const char *error_string = error.AsCString(); |
| 2363 | if (error_string == NULL) |
| 2364 | error_string = "launch failed"; |
| 2365 | SetExitStatus (-1, error_string); |
| 2366 | } |
| 2367 | } |
| 2368 | else |
| 2369 | { |
| 2370 | EventSP event_sp; |
Greg Clayton | 4985959 | 2011-06-22 01:42:17 +0000 | [diff] [blame] | 2371 | TimeValue timeout_time; |
| 2372 | timeout_time = TimeValue::Now(); |
| 2373 | timeout_time.OffsetWithSeconds(10); |
| 2374 | StateType state = WaitForProcessStopPrivate(&timeout_time, event_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2375 | |
Greg Clayton | 4985959 | 2011-06-22 01:42:17 +0000 | [diff] [blame] | 2376 | if (state == eStateInvalid || event_sp.get() == NULL) |
| 2377 | { |
| 2378 | // We were able to launch the process, but we failed to |
| 2379 | // catch the initial stop. |
| 2380 | SetExitStatus (0, "failed to catch stop after launch"); |
| 2381 | Destroy(); |
| 2382 | } |
| 2383 | else if (state == eStateStopped || state == eStateCrashed) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2384 | { |
Greg Clayton | 75c703d | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2385 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2386 | DidLaunch (); |
| 2387 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2388 | DynamicLoader *dyld = GetDynamicLoader (); |
| 2389 | if (dyld) |
| 2390 | dyld->DidLaunch(); |
Greg Clayton | 75c703d | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2391 | |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 2392 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2393 | // This delays passing the stopped event to listeners till DidLaunch gets |
| 2394 | // a chance to complete... |
| 2395 | HandlePrivateEvent (event_sp); |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2396 | |
| 2397 | if (PrivateStateThreadIsValid ()) |
| 2398 | ResumePrivateStateThread (); |
| 2399 | else |
| 2400 | StartPrivateStateThread (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2401 | } |
| 2402 | else if (state == eStateExited) |
| 2403 | { |
| 2404 | // We exited while trying to launch somehow. Don't call DidLaunch as that's |
| 2405 | // not likely to work, and return an invalid pid. |
| 2406 | HandlePrivateEvent (event_sp); |
| 2407 | } |
| 2408 | } |
| 2409 | } |
| 2410 | } |
| 2411 | else |
| 2412 | { |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 2413 | error.SetErrorStringWithFormat("file doesn't exist: '%s'", local_exec_file_path); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2414 | } |
| 2415 | } |
| 2416 | return error; |
| 2417 | } |
| 2418 | |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 2419 | |
| 2420 | Error |
| 2421 | Process::LoadCore () |
| 2422 | { |
| 2423 | Error error = DoLoadCore(); |
| 2424 | if (error.Success()) |
| 2425 | { |
| 2426 | if (PrivateStateThreadIsValid ()) |
| 2427 | ResumePrivateStateThread (); |
| 2428 | else |
| 2429 | StartPrivateStateThread (); |
| 2430 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2431 | DynamicLoader *dyld = GetDynamicLoader (); |
| 2432 | if (dyld) |
| 2433 | dyld->DidAttach(); |
| 2434 | |
| 2435 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 2436 | // We successfully loaded a core file, now pretend we stopped so we can |
| 2437 | // show all of the threads in the core file and explore the crashed |
| 2438 | // state. |
| 2439 | SetPrivateState (eStateStopped); |
| 2440 | |
| 2441 | } |
| 2442 | return error; |
| 2443 | } |
| 2444 | |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2445 | DynamicLoader * |
| 2446 | Process::GetDynamicLoader () |
| 2447 | { |
| 2448 | if (m_dyld_ap.get() == NULL) |
| 2449 | m_dyld_ap.reset (DynamicLoader::FindPlugin(this, NULL)); |
| 2450 | return m_dyld_ap.get(); |
| 2451 | } |
Greg Clayton | 46c9a35 | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 2452 | |
| 2453 | |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2454 | Process::NextEventAction::EventActionResult |
| 2455 | Process::AttachCompletionHandler::PerformAction (lldb::EventSP &event_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2456 | { |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2457 | StateType state = ProcessEventData::GetStateFromEvent (event_sp.get()); |
| 2458 | switch (state) |
Greg Clayton | c1d3775 | 2010-10-18 01:45:30 +0000 | [diff] [blame] | 2459 | { |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2460 | case eStateRunning: |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2461 | case eStateConnected: |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2462 | return eEventActionRetry; |
| 2463 | |
| 2464 | case eStateStopped: |
| 2465 | case eStateCrashed: |
Greg Clayton | 2d9adb7 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 2466 | { |
| 2467 | // During attach, prior to sending the eStateStopped event, |
Jim Ingham | 7fa7b2f | 2012-04-12 18:49:31 +0000 | [diff] [blame^] | 2468 | // lldb_private::Process subclasses must set the new process ID. |
Greg Clayton | 2d9adb7 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 2469 | assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID); |
| 2470 | if (m_exec_count > 0) |
| 2471 | { |
| 2472 | --m_exec_count; |
| 2473 | m_process->Resume(); |
| 2474 | return eEventActionRetry; |
| 2475 | } |
| 2476 | else |
| 2477 | { |
| 2478 | m_process->CompleteAttach (); |
| 2479 | return eEventActionSuccess; |
| 2480 | } |
| 2481 | } |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2482 | break; |
Greg Clayton | 2d9adb7 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 2483 | |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2484 | default: |
| 2485 | case eStateExited: |
| 2486 | case eStateInvalid: |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2487 | break; |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2488 | } |
Greg Clayton | 2d9adb7 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 2489 | |
| 2490 | m_exit_string.assign ("No valid Process"); |
| 2491 | return eEventActionExit; |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2492 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2493 | |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2494 | Process::NextEventAction::EventActionResult |
| 2495 | Process::AttachCompletionHandler::HandleBeingInterrupted() |
| 2496 | { |
| 2497 | return eEventActionSuccess; |
| 2498 | } |
| 2499 | |
| 2500 | const char * |
| 2501 | Process::AttachCompletionHandler::GetExitString () |
| 2502 | { |
| 2503 | return m_exit_string.c_str(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | Error |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2507 | Process::Attach (ProcessAttachInfo &attach_info) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2508 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2509 | m_abi_sp.reset(); |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 2510 | m_process_input_reader.reset(); |
Greg Clayton | 75c703d | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2511 | m_dyld_ap.reset(); |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 2512 | m_os_ap.reset(); |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 2513 | |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2514 | lldb::pid_t attach_pid = attach_info.GetProcessID(); |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 2515 | Error error; |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2516 | if (attach_pid == LLDB_INVALID_PROCESS_ID) |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 2517 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2518 | char process_name[PATH_MAX]; |
Jim Ingham | 0d7f777 | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 2519 | |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2520 | if (attach_info.GetExecutableFile().GetPath (process_name, sizeof(process_name))) |
Jim Ingham | ea29418 | 2010-08-17 21:54:19 +0000 | [diff] [blame] | 2521 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2522 | const bool wait_for_launch = attach_info.GetWaitForLaunch(); |
| 2523 | |
| 2524 | if (wait_for_launch) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2525 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2526 | error = WillAttachToProcessWithName(process_name, wait_for_launch); |
| 2527 | if (error.Success()) |
| 2528 | { |
Greg Clayton | ffa43a6 | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 2529 | m_should_detach = true; |
| 2530 | |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2531 | SetPublicState (eStateAttaching); |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 2532 | error = DoAttachToProcessWithName (process_name, wait_for_launch, attach_info); |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2533 | if (error.Fail()) |
| 2534 | { |
| 2535 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 2536 | { |
| 2537 | SetID (LLDB_INVALID_PROCESS_ID); |
| 2538 | if (error.AsCString() == NULL) |
| 2539 | error.SetErrorString("attach failed"); |
| 2540 | |
| 2541 | SetExitStatus(-1, error.AsCString()); |
| 2542 | } |
| 2543 | } |
| 2544 | else |
| 2545 | { |
| 2546 | SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount())); |
| 2547 | StartPrivateStateThread(); |
| 2548 | } |
| 2549 | return error; |
| 2550 | } |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 2551 | } |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2552 | else |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 2553 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2554 | ProcessInstanceInfoList process_infos; |
| 2555 | PlatformSP platform_sp (m_target.GetPlatform ()); |
| 2556 | |
| 2557 | if (platform_sp) |
| 2558 | { |
| 2559 | ProcessInstanceInfoMatch match_info; |
| 2560 | match_info.GetProcessInfo() = attach_info; |
| 2561 | match_info.SetNameMatchType (eNameMatchEquals); |
| 2562 | platform_sp->FindProcesses (match_info, process_infos); |
| 2563 | const uint32_t num_matches = process_infos.GetSize(); |
| 2564 | if (num_matches == 1) |
| 2565 | { |
| 2566 | attach_pid = process_infos.GetProcessIDAtIndex(0); |
| 2567 | // Fall through and attach using the above process ID |
| 2568 | } |
| 2569 | else |
| 2570 | { |
| 2571 | match_info.GetProcessInfo().GetExecutableFile().GetPath (process_name, sizeof(process_name)); |
| 2572 | if (num_matches > 1) |
| 2573 | error.SetErrorStringWithFormat ("more than one process named %s", process_name); |
| 2574 | else |
| 2575 | error.SetErrorStringWithFormat ("could not find a process named %s", process_name); |
| 2576 | } |
| 2577 | } |
| 2578 | else |
| 2579 | { |
| 2580 | error.SetErrorString ("invalid platform, can't find processes by name"); |
| 2581 | return error; |
| 2582 | } |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 2583 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2584 | } |
| 2585 | else |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2586 | { |
| 2587 | error.SetErrorString ("invalid process name"); |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 2588 | } |
| 2589 | } |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2590 | |
| 2591 | if (attach_pid != LLDB_INVALID_PROCESS_ID) |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 2592 | { |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2593 | error = WillAttachToProcessWithID(attach_pid); |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 2594 | if (error.Success()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2595 | { |
Greg Clayton | ffa43a6 | 2011-11-17 04:46:02 +0000 | [diff] [blame] | 2596 | m_should_detach = true; |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 2597 | SetPublicState (eStateAttaching); |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2598 | |
Han Ming Ong | d1040dd | 2012-02-25 01:07:38 +0000 | [diff] [blame] | 2599 | error = DoAttachToProcessWithID (attach_pid, attach_info); |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2600 | if (error.Success()) |
| 2601 | { |
| 2602 | |
| 2603 | SetNextEventAction(new Process::AttachCompletionHandler(this, attach_info.GetResumeCount())); |
| 2604 | StartPrivateStateThread(); |
| 2605 | } |
| 2606 | else |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 2607 | { |
| 2608 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 2609 | { |
| 2610 | SetID (LLDB_INVALID_PROCESS_ID); |
| 2611 | const char *error_string = error.AsCString(); |
| 2612 | if (error_string == NULL) |
| 2613 | error_string = "attach failed"; |
| 2614 | |
| 2615 | SetExitStatus(-1, error_string); |
| 2616 | } |
| 2617 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2618 | } |
| 2619 | } |
| 2620 | return error; |
| 2621 | } |
| 2622 | |
Greg Clayton | 527154d | 2011-11-15 03:53:30 +0000 | [diff] [blame] | 2623 | //Error |
| 2624 | //Process::Attach (const char *process_name, bool wait_for_launch) |
| 2625 | //{ |
| 2626 | // m_abi_sp.reset(); |
| 2627 | // m_process_input_reader.reset(); |
| 2628 | // |
| 2629 | // // Find the process and its architecture. Make sure it matches the architecture |
| 2630 | // // of the current Target, and if not adjust it. |
| 2631 | // Error error; |
| 2632 | // |
| 2633 | // if (!wait_for_launch) |
| 2634 | // { |
| 2635 | // ProcessInstanceInfoList process_infos; |
| 2636 | // PlatformSP platform_sp (m_target.GetPlatform ()); |
| 2637 | // assert (platform_sp.get()); |
| 2638 | // |
| 2639 | // if (platform_sp) |
| 2640 | // { |
| 2641 | // ProcessInstanceInfoMatch match_info; |
| 2642 | // match_info.GetProcessInfo().SetName(process_name); |
| 2643 | // match_info.SetNameMatchType (eNameMatchEquals); |
| 2644 | // platform_sp->FindProcesses (match_info, process_infos); |
| 2645 | // if (process_infos.GetSize() > 1) |
| 2646 | // { |
| 2647 | // error.SetErrorStringWithFormat ("more than one process named %s", process_name); |
| 2648 | // } |
| 2649 | // else if (process_infos.GetSize() == 0) |
| 2650 | // { |
| 2651 | // error.SetErrorStringWithFormat ("could not find a process named %s", process_name); |
| 2652 | // } |
| 2653 | // } |
| 2654 | // else |
| 2655 | // { |
| 2656 | // error.SetErrorString ("invalid platform"); |
| 2657 | // } |
| 2658 | // } |
| 2659 | // |
| 2660 | // if (error.Success()) |
| 2661 | // { |
| 2662 | // m_dyld_ap.reset(); |
| 2663 | // m_os_ap.reset(); |
| 2664 | // |
| 2665 | // error = WillAttachToProcessWithName(process_name, wait_for_launch); |
| 2666 | // if (error.Success()) |
| 2667 | // { |
| 2668 | // SetPublicState (eStateAttaching); |
| 2669 | // error = DoAttachToProcessWithName (process_name, wait_for_launch); |
| 2670 | // if (error.Fail()) |
| 2671 | // { |
| 2672 | // if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 2673 | // { |
| 2674 | // SetID (LLDB_INVALID_PROCESS_ID); |
| 2675 | // const char *error_string = error.AsCString(); |
| 2676 | // if (error_string == NULL) |
| 2677 | // error_string = "attach failed"; |
| 2678 | // |
| 2679 | // SetExitStatus(-1, error_string); |
| 2680 | // } |
| 2681 | // } |
| 2682 | // else |
| 2683 | // { |
| 2684 | // SetNextEventAction(new Process::AttachCompletionHandler(this, 0)); |
| 2685 | // StartPrivateStateThread(); |
| 2686 | // } |
| 2687 | // } |
| 2688 | // } |
| 2689 | // return error; |
| 2690 | //} |
| 2691 | |
Greg Clayton | 75c703d | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2692 | void |
| 2693 | Process::CompleteAttach () |
| 2694 | { |
| 2695 | // Let the process subclass figure out at much as it can about the process |
| 2696 | // before we go looking for a dynamic loader plug-in. |
| 2697 | DidAttach(); |
| 2698 | |
Jim Ingham | 0d7f777 | 2011-09-15 01:10:17 +0000 | [diff] [blame] | 2699 | // We just attached. If we have a platform, ask it for the process architecture, and if it isn't |
| 2700 | // the same as the one we've already set, switch architectures. |
| 2701 | PlatformSP platform_sp (m_target.GetPlatform ()); |
| 2702 | assert (platform_sp.get()); |
| 2703 | if (platform_sp) |
| 2704 | { |
| 2705 | ProcessInstanceInfo process_info; |
| 2706 | platform_sp->GetProcessInfo (GetID(), process_info); |
| 2707 | const ArchSpec &process_arch = process_info.GetArchitecture(); |
| 2708 | if (process_arch.IsValid() && m_target.GetArchitecture() != process_arch) |
| 2709 | m_target.SetArchitecture (process_arch); |
| 2710 | } |
| 2711 | |
| 2712 | // We have completed the attach, now it is time to find the dynamic loader |
Greg Clayton | 75c703d | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2713 | // plug-in |
Greg Clayton | 9ce9538 | 2012-02-13 23:10:39 +0000 | [diff] [blame] | 2714 | DynamicLoader *dyld = GetDynamicLoader (); |
| 2715 | if (dyld) |
| 2716 | dyld->DidAttach(); |
Greg Clayton | 75c703d | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2717 | |
Greg Clayton | 37f962e | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 2718 | m_os_ap.reset (OperatingSystem::FindPlugin (this, NULL)); |
Greg Clayton | 75c703d | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2719 | // Figure out which one is the executable, and set that in our target: |
| 2720 | ModuleList &modules = m_target.GetImages(); |
| 2721 | |
| 2722 | size_t num_modules = modules.GetSize(); |
| 2723 | for (int i = 0; i < num_modules; i++) |
| 2724 | { |
| 2725 | ModuleSP module_sp (modules.GetModuleAtIndex(i)); |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 2726 | if (module_sp && module_sp->IsExecutable()) |
Greg Clayton | 75c703d | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2727 | { |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 2728 | if (m_target.GetExecutableModulePointer() != module_sp.get()) |
Greg Clayton | 75c703d | 2011-02-16 04:46:07 +0000 | [diff] [blame] | 2729 | m_target.SetExecutableModule (module_sp, false); |
| 2730 | break; |
| 2731 | } |
| 2732 | } |
| 2733 | } |
| 2734 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2735 | Error |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 2736 | Process::ConnectRemote (const char *remote_url) |
| 2737 | { |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 2738 | m_abi_sp.reset(); |
| 2739 | m_process_input_reader.reset(); |
| 2740 | |
| 2741 | // Find the process and its architecture. Make sure it matches the architecture |
| 2742 | // of the current Target, and if not adjust it. |
| 2743 | |
| 2744 | Error error (DoConnectRemote (remote_url)); |
| 2745 | if (error.Success()) |
| 2746 | { |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2747 | if (GetID() != LLDB_INVALID_PROCESS_ID) |
| 2748 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2749 | EventSP event_sp; |
| 2750 | StateType state = WaitForProcessStopPrivate(NULL, event_sp); |
| 2751 | |
| 2752 | if (state == eStateStopped || state == eStateCrashed) |
| 2753 | { |
| 2754 | // If we attached and actually have a process on the other end, then |
| 2755 | // this ended up being the equivalent of an attach. |
| 2756 | CompleteAttach (); |
| 2757 | |
| 2758 | // This delays passing the stopped event to listeners till |
| 2759 | // CompleteAttach gets a chance to complete... |
| 2760 | HandlePrivateEvent (event_sp); |
| 2761 | |
| 2762 | } |
Greg Clayton | a2f7423 | 2011-02-24 22:24:29 +0000 | [diff] [blame] | 2763 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 2764 | |
| 2765 | if (PrivateStateThreadIsValid ()) |
| 2766 | ResumePrivateStateThread (); |
| 2767 | else |
| 2768 | StartPrivateStateThread (); |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 2769 | } |
| 2770 | return error; |
| 2771 | } |
| 2772 | |
| 2773 | |
| 2774 | Error |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2775 | Process::Resume () |
| 2776 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 2777 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2778 | if (log) |
Jim Ingham | ac95966 | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 2779 | log->Printf("Process::Resume() m_stop_id = %u, public state: %s private state: %s", |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 2780 | m_mod_id.GetStopID(), |
Jim Ingham | ac95966 | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 2781 | StateAsCString(m_public_state.GetValue()), |
| 2782 | StateAsCString(m_private_state.GetValue())); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2783 | |
| 2784 | Error error (WillResume()); |
| 2785 | // Tell the process it is about to resume before the thread list |
| 2786 | if (error.Success()) |
| 2787 | { |
Johnny Chen | 9c11d47 | 2010-12-02 20:53:05 +0000 | [diff] [blame] | 2788 | // 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] | 2789 | // can let all of our threads know that they are about to be |
| 2790 | // resumed. Threads will each be called with |
| 2791 | // Thread::WillResume(StateType) where StateType contains the state |
| 2792 | // that they are supposed to have when the process is resumed |
| 2793 | // (suspended/running/stepping). Threads should also check |
| 2794 | // their resume signal in lldb::Thread::GetResumeSignal() |
| 2795 | // to see if they are suppoed to start back up with a signal. |
| 2796 | if (m_thread_list.WillResume()) |
| 2797 | { |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 2798 | // Last thing, do the PreResumeActions. |
| 2799 | if (!RunPreResumeActions()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2800 | { |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 2801 | error.SetErrorStringWithFormat ("Process::Resume PreResumeActions failed, not resuming."); |
| 2802 | } |
| 2803 | else |
| 2804 | { |
| 2805 | m_mod_id.BumpResumeID(); |
| 2806 | error = DoResume(); |
| 2807 | if (error.Success()) |
| 2808 | { |
| 2809 | DidResume(); |
| 2810 | m_thread_list.DidResume(); |
| 2811 | if (log) |
| 2812 | log->Printf ("Process thinks the process has resumed."); |
| 2813 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2814 | } |
| 2815 | } |
| 2816 | else |
| 2817 | { |
Jim Ingham | ac95966 | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 2818 | error.SetErrorStringWithFormat("Process::WillResume() thread list returned false after WillResume"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2819 | } |
| 2820 | } |
Jim Ingham | ac95966 | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 2821 | else if (log) |
| 2822 | log->Printf ("Process::WillResume() got an error \"%s\".", error.AsCString("<unknown error>")); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2823 | return error; |
| 2824 | } |
| 2825 | |
| 2826 | Error |
| 2827 | Process::Halt () |
| 2828 | { |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2829 | // Pause our private state thread so we can ensure no one else eats |
| 2830 | // the stop event out from under us. |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 2831 | Listener halt_listener ("lldb.process.halt_listener"); |
| 2832 | HijackPrivateProcessEvents(&halt_listener); |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 2833 | |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2834 | EventSP event_sp; |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2835 | Error error (WillHalt()); |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2836 | |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2837 | if (error.Success()) |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2838 | { |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2839 | |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2840 | bool caused_stop = false; |
| 2841 | |
| 2842 | // Ask the process subclass to actually halt our process |
| 2843 | error = DoHalt(caused_stop); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2844 | if (error.Success()) |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2845 | { |
Greg Clayton | 7e2f91c | 2011-01-29 07:10:55 +0000 | [diff] [blame] | 2846 | if (m_public_state.GetValue() == eStateAttaching) |
| 2847 | { |
| 2848 | SetExitStatus(SIGKILL, "Cancelled async attach."); |
| 2849 | Destroy (); |
| 2850 | } |
| 2851 | else |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2852 | { |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2853 | // If "caused_stop" is true, then DoHalt stopped the process. If |
| 2854 | // "caused_stop" is false, the process was already stopped. |
| 2855 | // If the DoHalt caused the process to stop, then we want to catch |
| 2856 | // this event and set the interrupted bool to true before we pass |
| 2857 | // this along so clients know that the process was interrupted by |
| 2858 | // a halt command. |
| 2859 | if (caused_stop) |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 2860 | { |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 2861 | // Wait for 1 second for the process to stop. |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2862 | TimeValue timeout_time; |
| 2863 | timeout_time = TimeValue::Now(); |
| 2864 | timeout_time.OffsetWithSeconds(1); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 2865 | bool got_event = halt_listener.WaitForEvent (&timeout_time, event_sp); |
| 2866 | StateType state = ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2867 | |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 2868 | if (!got_event || state == eStateInvalid) |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 2869 | { |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2870 | // We timeout out and didn't get a stop event... |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 2871 | error.SetErrorStringWithFormat ("Halt timed out. State = %s", StateAsCString(GetState())); |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 2872 | } |
| 2873 | else |
| 2874 | { |
Greg Clayton | 2020608 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 2875 | if (StateIsStoppedState (state, false)) |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2876 | { |
| 2877 | // We caused the process to interrupt itself, so mark this |
| 2878 | // as such in the stop event so clients can tell an interrupted |
| 2879 | // process from a natural stop |
| 2880 | ProcessEventData::SetInterruptedInEvent (event_sp.get(), true); |
| 2881 | } |
| 2882 | else |
| 2883 | { |
| 2884 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2885 | if (log) |
| 2886 | log->Printf("Process::Halt() failed to stop, state is: %s", StateAsCString(state)); |
| 2887 | error.SetErrorString ("Did not get stopped event after halt."); |
| 2888 | } |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 2889 | } |
| 2890 | } |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2891 | DidHalt(); |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 2892 | } |
| 2893 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2894 | } |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2895 | // Resume our private state thread before we post the event (if any) |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 2896 | RestorePrivateProcessEvents(); |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 2897 | |
| 2898 | // Post any event we might have consumed. If all goes well, we will have |
| 2899 | // stopped the process, intercepted the event and set the interrupted |
| 2900 | // bool in the event. Post it to the private event queue and that will end up |
| 2901 | // correctly setting the state. |
| 2902 | if (event_sp) |
| 2903 | m_private_state_broadcaster.BroadcastEvent(event_sp); |
| 2904 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2905 | return error; |
| 2906 | } |
| 2907 | |
| 2908 | Error |
| 2909 | Process::Detach () |
| 2910 | { |
| 2911 | Error error (WillDetach()); |
| 2912 | |
| 2913 | if (error.Success()) |
| 2914 | { |
| 2915 | DisableAllBreakpointSites(); |
| 2916 | error = DoDetach(); |
| 2917 | if (error.Success()) |
| 2918 | { |
| 2919 | DidDetach(); |
| 2920 | StopPrivateStateThread(); |
| 2921 | } |
| 2922 | } |
| 2923 | return error; |
| 2924 | } |
| 2925 | |
| 2926 | Error |
| 2927 | Process::Destroy () |
| 2928 | { |
| 2929 | Error error (WillDestroy()); |
| 2930 | if (error.Success()) |
| 2931 | { |
| 2932 | DisableAllBreakpointSites(); |
| 2933 | error = DoDestroy(); |
| 2934 | if (error.Success()) |
| 2935 | { |
| 2936 | DidDestroy(); |
| 2937 | StopPrivateStateThread(); |
| 2938 | } |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 2939 | m_stdio_communication.StopReadThread(); |
| 2940 | m_stdio_communication.Disconnect(); |
| 2941 | if (m_process_input_reader && m_process_input_reader->IsActive()) |
| 2942 | m_target.GetDebugger().PopInputReader (m_process_input_reader); |
| 2943 | if (m_process_input_reader) |
| 2944 | m_process_input_reader.reset(); |
Jim Ingham | 7fa7b2f | 2012-04-12 18:49:31 +0000 | [diff] [blame^] | 2945 | |
| 2946 | // If we have been interrupted (to kill us) in the middle of running, we may not end up propagating |
| 2947 | // the last events through the event system, in which case we might strand the write lock. Unlock |
| 2948 | // it here so when we do to tear down the process we don't get an error destroying the lock. |
| 2949 | m_run_lock.WriteUnlock(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2950 | } |
| 2951 | return error; |
| 2952 | } |
| 2953 | |
| 2954 | Error |
| 2955 | Process::Signal (int signal) |
| 2956 | { |
| 2957 | Error error (WillSignal()); |
| 2958 | if (error.Success()) |
| 2959 | { |
| 2960 | error = DoSignal(signal); |
| 2961 | if (error.Success()) |
| 2962 | DidSignal(); |
| 2963 | } |
| 2964 | return error; |
| 2965 | } |
| 2966 | |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 2967 | lldb::ByteOrder |
| 2968 | Process::GetByteOrder () const |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2969 | { |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 2970 | return m_target.GetArchitecture().GetByteOrder(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | uint32_t |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 2974 | Process::GetAddressByteSize () const |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2975 | { |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 2976 | return m_target.GetArchitecture().GetAddressByteSize(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
Greg Clayton | 395fc33 | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 2979 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2980 | bool |
| 2981 | Process::ShouldBroadcastEvent (Event *event_ptr) |
| 2982 | { |
| 2983 | const StateType state = Process::ProcessEventData::GetStateFromEvent (event_ptr); |
| 2984 | bool return_value = true; |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 2985 | LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2986 | |
| 2987 | switch (state) |
| 2988 | { |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 2989 | case eStateConnected: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2990 | case eStateAttaching: |
| 2991 | case eStateLaunching: |
| 2992 | case eStateDetached: |
| 2993 | case eStateExited: |
| 2994 | case eStateUnloaded: |
| 2995 | // These events indicate changes in the state of the debugging session, always report them. |
| 2996 | return_value = true; |
| 2997 | break; |
| 2998 | case eStateInvalid: |
| 2999 | // We stopped for no apparent reason, don't report it. |
| 3000 | return_value = false; |
| 3001 | break; |
| 3002 | case eStateRunning: |
| 3003 | case eStateStepping: |
| 3004 | // If we've started the target running, we handle the cases where we |
| 3005 | // are already running and where there is a transition from stopped to |
| 3006 | // running differently. |
| 3007 | // running -> running: Automatically suppress extra running events |
| 3008 | // stopped -> running: Report except when there is one or more no votes |
| 3009 | // and no yes votes. |
| 3010 | SynchronouslyNotifyStateChanged (state); |
| 3011 | switch (m_public_state.GetValue()) |
| 3012 | { |
| 3013 | case eStateRunning: |
| 3014 | case eStateStepping: |
| 3015 | // We always suppress multiple runnings with no PUBLIC stop in between. |
| 3016 | return_value = false; |
| 3017 | break; |
| 3018 | default: |
| 3019 | // TODO: make this work correctly. For now always report |
| 3020 | // run if we aren't running so we don't miss any runnning |
| 3021 | // events. If I run the lldb/test/thread/a.out file and |
| 3022 | // break at main.cpp:58, run and hit the breakpoints on |
| 3023 | // multiple threads, then somehow during the stepping over |
| 3024 | // of all breakpoints no run gets reported. |
| 3025 | return_value = true; |
| 3026 | |
| 3027 | // This is a transition from stop to run. |
| 3028 | switch (m_thread_list.ShouldReportRun (event_ptr)) |
| 3029 | { |
| 3030 | case eVoteYes: |
| 3031 | case eVoteNoOpinion: |
| 3032 | return_value = true; |
| 3033 | break; |
| 3034 | case eVoteNo: |
| 3035 | return_value = false; |
| 3036 | break; |
| 3037 | } |
| 3038 | break; |
| 3039 | } |
| 3040 | break; |
| 3041 | case eStateStopped: |
| 3042 | case eStateCrashed: |
| 3043 | case eStateSuspended: |
| 3044 | { |
| 3045 | // We've stopped. First see if we're going to restart the target. |
| 3046 | // If we are going to stop, then we always broadcast the event. |
| 3047 | // 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] | 3048 | // If no thread has an opinion, we don't report it. |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3049 | if (ProcessEventData::GetInterruptedFromEvent (event_ptr)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3050 | { |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3051 | if (log) |
| 3052 | 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] | 3053 | return true; |
| 3054 | } |
| 3055 | else |
| 3056 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3057 | RefreshStateAfterStop (); |
| 3058 | |
| 3059 | if (m_thread_list.ShouldStop (event_ptr) == false) |
| 3060 | { |
| 3061 | switch (m_thread_list.ShouldReportStop (event_ptr)) |
| 3062 | { |
| 3063 | case eVoteYes: |
| 3064 | Process::ProcessEventData::SetRestartedInEvent (event_ptr, true); |
Johnny Chen | 028784b | 2010-10-14 00:54:32 +0000 | [diff] [blame] | 3065 | // Intentional fall-through here. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3066 | case eVoteNoOpinion: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3067 | case eVoteNo: |
| 3068 | return_value = false; |
| 3069 | break; |
| 3070 | } |
| 3071 | |
| 3072 | if (log) |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3073 | 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] | 3074 | Resume (); |
| 3075 | } |
| 3076 | else |
| 3077 | { |
| 3078 | return_value = true; |
| 3079 | SynchronouslyNotifyStateChanged (state); |
| 3080 | } |
| 3081 | } |
| 3082 | } |
| 3083 | } |
| 3084 | |
| 3085 | if (log) |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 3086 | log->Printf ("Process::ShouldBroadcastEvent (%p) => %s - %s", event_ptr, StateAsCString(state), return_value ? "YES" : "NO"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3087 | return return_value; |
| 3088 | } |
| 3089 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3090 | |
| 3091 | bool |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3092 | Process::StartPrivateStateThread (bool force) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3093 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 3094 | LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EVENTS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3095 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3096 | bool already_running = PrivateStateThreadIsValid (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3097 | if (log) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3098 | log->Printf ("Process::%s()%s ", __FUNCTION__, already_running ? " already running" : " starting private state thread"); |
| 3099 | |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3100 | if (!force && already_running) |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3101 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3102 | |
| 3103 | // Create a thread that watches our internal state and controls which |
| 3104 | // events make it to clients (into the DCProcess event queue). |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 3105 | char thread_name[1024]; |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3106 | if (already_running) |
| 3107 | snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state-override(pid=%llu)>", GetID()); |
| 3108 | else |
| 3109 | snprintf(thread_name, sizeof(thread_name), "<lldb.process.internal-state(pid=%llu)>", GetID()); |
Jim Ingham | d21d98b | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 3110 | |
| 3111 | // Create the private state thread, and start it running. |
Greg Clayton | a875b64 | 2011-01-09 21:07:35 +0000 | [diff] [blame] | 3112 | m_private_state_thread = Host::ThreadCreate (thread_name, Process::PrivateStateThread, this, NULL); |
Jim Ingham | d21d98b | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 3113 | bool success = IS_VALID_LLDB_HOST_THREAD(m_private_state_thread); |
| 3114 | if (success) |
| 3115 | { |
| 3116 | ResumePrivateStateThread(); |
| 3117 | return true; |
| 3118 | } |
| 3119 | else |
| 3120 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3121 | } |
| 3122 | |
| 3123 | void |
| 3124 | Process::PausePrivateStateThread () |
| 3125 | { |
| 3126 | ControlPrivateStateThread (eBroadcastInternalStateControlPause); |
| 3127 | } |
| 3128 | |
| 3129 | void |
| 3130 | Process::ResumePrivateStateThread () |
| 3131 | { |
| 3132 | ControlPrivateStateThread (eBroadcastInternalStateControlResume); |
| 3133 | } |
| 3134 | |
| 3135 | void |
| 3136 | Process::StopPrivateStateThread () |
| 3137 | { |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3138 | if (PrivateStateThreadIsValid ()) |
| 3139 | ControlPrivateStateThread (eBroadcastInternalStateControlStop); |
Jim Ingham | 7fa7b2f | 2012-04-12 18:49:31 +0000 | [diff] [blame^] | 3140 | else |
| 3141 | { |
| 3142 | LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 3143 | if (log) |
| 3144 | printf ("Went to stop the private state thread, but it was already invalid."); |
| 3145 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
| 3148 | void |
| 3149 | Process::ControlPrivateStateThread (uint32_t signal) |
| 3150 | { |
Jim Ingham | 7fa7b2f | 2012-04-12 18:49:31 +0000 | [diff] [blame^] | 3151 | LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3152 | |
| 3153 | assert (signal == eBroadcastInternalStateControlStop || |
| 3154 | signal == eBroadcastInternalStateControlPause || |
| 3155 | signal == eBroadcastInternalStateControlResume); |
| 3156 | |
| 3157 | if (log) |
Greg Clayton | f4fbc0b | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 3158 | log->Printf ("Process::%s (signal = %d)", __FUNCTION__, signal); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3159 | |
Greg Clayton | f4fbc0b | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 3160 | // Signal the private state thread. First we should copy this is case the |
| 3161 | // thread starts exiting since the private state thread will NULL this out |
| 3162 | // when it exits |
| 3163 | const lldb::thread_t private_state_thread = m_private_state_thread; |
Greg Clayton | 09c81ef | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 3164 | if (IS_VALID_LLDB_HOST_THREAD(private_state_thread)) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3165 | { |
| 3166 | TimeValue timeout_time; |
| 3167 | bool timed_out; |
| 3168 | |
| 3169 | m_private_state_control_broadcaster.BroadcastEvent (signal, NULL); |
| 3170 | |
| 3171 | timeout_time = TimeValue::Now(); |
| 3172 | timeout_time.OffsetWithSeconds(2); |
Jim Ingham | 7fa7b2f | 2012-04-12 18:49:31 +0000 | [diff] [blame^] | 3173 | if (log) |
| 3174 | log->Printf ("Sending control event of type: %d.", signal); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3175 | m_private_state_control_wait.WaitForValueEqualTo (true, &timeout_time, &timed_out); |
| 3176 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
| 3177 | |
| 3178 | if (signal == eBroadcastInternalStateControlStop) |
| 3179 | { |
| 3180 | if (timed_out) |
Jim Ingham | 7fa7b2f | 2012-04-12 18:49:31 +0000 | [diff] [blame^] | 3181 | { |
| 3182 | Error error; |
| 3183 | Host::ThreadCancel (private_state_thread, &error); |
| 3184 | if (log) |
| 3185 | log->Printf ("Timed out responding to the control event, cancel got error: \"%s\".", error.AsCString()); |
| 3186 | } |
| 3187 | else |
| 3188 | { |
| 3189 | if (log) |
| 3190 | log->Printf ("The control event killed the private state thread without having to cancel."); |
| 3191 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3192 | |
| 3193 | thread_result_t result = NULL; |
Greg Clayton | f4fbc0b | 2011-01-22 17:43:17 +0000 | [diff] [blame] | 3194 | Host::ThreadJoin (private_state_thread, &result, NULL); |
Greg Clayton | c607d86 | 2010-07-22 18:34:21 +0000 | [diff] [blame] | 3195 | m_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3196 | } |
| 3197 | } |
Jim Ingham | 7fa7b2f | 2012-04-12 18:49:31 +0000 | [diff] [blame^] | 3198 | else |
| 3199 | { |
| 3200 | if (log) |
| 3201 | log->Printf ("Private state thread already dead, no need to signal it to stop."); |
| 3202 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | void |
| 3206 | Process::HandlePrivateEvent (EventSP &event_sp) |
| 3207 | { |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 3208 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3209 | |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 3210 | const StateType new_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3211 | |
| 3212 | // First check to see if anybody wants a shot at this event: |
Jim Ingham | 68bffc5 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 3213 | if (m_next_event_action_ap.get() != NULL) |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3214 | { |
Jim Ingham | 68bffc5 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 3215 | NextEventAction::EventActionResult action_result = m_next_event_action_ap->PerformAction(event_sp); |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3216 | switch (action_result) |
| 3217 | { |
| 3218 | case NextEventAction::eEventActionSuccess: |
| 3219 | SetNextEventAction(NULL); |
| 3220 | break; |
Greg Clayton | 2d9adb7 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3221 | |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3222 | case NextEventAction::eEventActionRetry: |
| 3223 | break; |
Greg Clayton | 2d9adb7 | 2011-11-12 02:10:56 +0000 | [diff] [blame] | 3224 | |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3225 | case NextEventAction::eEventActionExit: |
Jim Ingham | 84c8638 | 2011-01-29 01:57:31 +0000 | [diff] [blame] | 3226 | // Handle Exiting Here. If we already got an exited event, |
| 3227 | // we should just propagate it. Otherwise, swallow this event, |
| 3228 | // and set our state to exit so the next event will kill us. |
| 3229 | if (new_state != eStateExited) |
| 3230 | { |
| 3231 | // FIXME: should cons up an exited event, and discard this one. |
Jim Ingham | 68bffc5 | 2011-01-29 04:05:41 +0000 | [diff] [blame] | 3232 | SetExitStatus(0, m_next_event_action_ap->GetExitString()); |
Jim Ingham | 84c8638 | 2011-01-29 01:57:31 +0000 | [diff] [blame] | 3233 | SetNextEventAction(NULL); |
| 3234 | return; |
| 3235 | } |
| 3236 | SetNextEventAction(NULL); |
Jim Ingham | c2dc7c8 | 2011-01-29 01:49:25 +0000 | [diff] [blame] | 3237 | break; |
| 3238 | } |
| 3239 | } |
| 3240 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3241 | // See if we should broadcast this state to external clients? |
| 3242 | const bool should_broadcast = ShouldBroadcastEvent (event_sp.get()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3243 | |
| 3244 | if (should_broadcast) |
| 3245 | { |
| 3246 | if (log) |
| 3247 | { |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 3248 | log->Printf ("Process::%s (pid = %llu) broadcasting new state %s (old state %s) to %s", |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 3249 | __FUNCTION__, |
| 3250 | GetID(), |
| 3251 | StateAsCString(new_state), |
| 3252 | StateAsCString (GetState ()), |
| 3253 | IsHijackedForEvent(eBroadcastBitStateChanged) ? "hijacked" : "public"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3254 | } |
Jim Ingham | d60d94a | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 3255 | Process::ProcessEventData::SetUpdateStateOnRemoval(event_sp.get()); |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 3256 | if (StateIsRunningState (new_state)) |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3257 | PushProcessInputReader (); |
| 3258 | else |
| 3259 | PopProcessInputReader (); |
Jim Ingham | d60d94a | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 3260 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3261 | BroadcastEvent (event_sp); |
| 3262 | } |
| 3263 | else |
| 3264 | { |
| 3265 | if (log) |
| 3266 | { |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 3267 | log->Printf ("Process::%s (pid = %llu) suppressing state %s (old state %s): should_broadcast == false", |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 3268 | __FUNCTION__, |
| 3269 | GetID(), |
| 3270 | StateAsCString(new_state), |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 3271 | StateAsCString (GetState ())); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3272 | } |
| 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | void * |
| 3277 | Process::PrivateStateThread (void *arg) |
| 3278 | { |
| 3279 | Process *proc = static_cast<Process*> (arg); |
| 3280 | void *result = proc->RunPrivateStateThread (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3281 | return result; |
| 3282 | } |
| 3283 | |
| 3284 | void * |
| 3285 | Process::RunPrivateStateThread () |
| 3286 | { |
Jim Ingham | d21d98b | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 3287 | bool control_only = true; |
Jim Ingham | 7fa7b2f | 2012-04-12 18:49:31 +0000 | [diff] [blame^] | 3288 | m_private_state_control_wait.SetValue (false, eBroadcastNever); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3289 | |
Greg Clayton | e005f2c | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 3290 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3291 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 3292 | log->Printf ("Process::%s (arg = %p, pid = %llu) thread starting...", __FUNCTION__, this, GetID()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3293 | |
| 3294 | bool exit_now = false; |
| 3295 | while (!exit_now) |
| 3296 | { |
| 3297 | EventSP event_sp; |
| 3298 | WaitForEventsPrivate (NULL, event_sp, control_only); |
| 3299 | if (event_sp->BroadcasterIs(&m_private_state_control_broadcaster)) |
| 3300 | { |
Jim Ingham | 7fa7b2f | 2012-04-12 18:49:31 +0000 | [diff] [blame^] | 3301 | if (log) |
| 3302 | log->Printf ("Process::%s (arg = %p, pid = %llu) got a control event: %d", __FUNCTION__, this, GetID(), event_sp->GetType()); |
| 3303 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3304 | switch (event_sp->GetType()) |
| 3305 | { |
| 3306 | case eBroadcastInternalStateControlStop: |
| 3307 | exit_now = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3308 | break; // doing any internal state managment below |
| 3309 | |
| 3310 | case eBroadcastInternalStateControlPause: |
| 3311 | control_only = true; |
| 3312 | break; |
| 3313 | |
| 3314 | case eBroadcastInternalStateControlResume: |
| 3315 | control_only = false; |
| 3316 | break; |
| 3317 | } |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3318 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3319 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3320 | continue; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3321 | } |
| 3322 | |
| 3323 | |
| 3324 | const StateType internal_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 3325 | |
| 3326 | if (internal_state != eStateInvalid) |
| 3327 | { |
| 3328 | HandlePrivateEvent (event_sp); |
| 3329 | } |
| 3330 | |
Greg Clayton | 3b2c41c | 2010-10-18 04:14:23 +0000 | [diff] [blame] | 3331 | if (internal_state == eStateInvalid || |
| 3332 | internal_state == eStateExited || |
| 3333 | internal_state == eStateDetached ) |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3334 | { |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3335 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 3336 | log->Printf ("Process::%s (arg = %p, pid = %llu) about to exit with internal state %s...", __FUNCTION__, this, GetID(), StateAsCString(internal_state)); |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3337 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3338 | break; |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3339 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
Caroline Tice | 926060e | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 3342 | // Verify log is still enabled before attempting to write to it... |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3343 | if (log) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 3344 | log->Printf ("Process::%s (arg = %p, pid = %llu) thread exiting...", __FUNCTION__, this, GetID()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3345 | |
Greg Clayton | a4881d0 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 3346 | m_private_state_control_wait.SetValue (true, eBroadcastAlways); |
| 3347 | m_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3348 | return NULL; |
| 3349 | } |
| 3350 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3351 | //------------------------------------------------------------------ |
| 3352 | // Process Event Data |
| 3353 | //------------------------------------------------------------------ |
| 3354 | |
| 3355 | Process::ProcessEventData::ProcessEventData () : |
| 3356 | EventData (), |
| 3357 | m_process_sp (), |
| 3358 | m_state (eStateInvalid), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 3359 | m_restarted (false), |
Jim Ingham | 6cf4d2b | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 3360 | m_update_state (0), |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3361 | m_interrupted (false) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3362 | { |
| 3363 | } |
| 3364 | |
| 3365 | Process::ProcessEventData::ProcessEventData (const ProcessSP &process_sp, StateType state) : |
| 3366 | EventData (), |
| 3367 | m_process_sp (process_sp), |
| 3368 | m_state (state), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 3369 | m_restarted (false), |
Jim Ingham | 6cf4d2b | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 3370 | m_update_state (0), |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3371 | m_interrupted (false) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3372 | { |
| 3373 | } |
| 3374 | |
| 3375 | Process::ProcessEventData::~ProcessEventData() |
| 3376 | { |
| 3377 | } |
| 3378 | |
| 3379 | const ConstString & |
| 3380 | Process::ProcessEventData::GetFlavorString () |
| 3381 | { |
| 3382 | static ConstString g_flavor ("Process::ProcessEventData"); |
| 3383 | return g_flavor; |
| 3384 | } |
| 3385 | |
| 3386 | const ConstString & |
| 3387 | Process::ProcessEventData::GetFlavor () const |
| 3388 | { |
| 3389 | return ProcessEventData::GetFlavorString (); |
| 3390 | } |
| 3391 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3392 | void |
| 3393 | Process::ProcessEventData::DoOnRemoval (Event *event_ptr) |
| 3394 | { |
| 3395 | // This function gets called twice for each event, once when the event gets pulled |
Jim Ingham | 6cf4d2b | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 3396 | // off of the private process event queue, and then any number of times, first when it gets pulled off of |
| 3397 | // the public event queue, then other times when we're pretending that this is where we stopped at the |
| 3398 | // end of expression evaluation. m_update_state is used to distinguish these |
| 3399 | // three cases; it is 0 when we're just pulling it off for private handling, |
| 3400 | // and > 1 for expression evaluation, and we don't want to do the breakpoint command handling then. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3401 | |
Jim Ingham | 6cf4d2b | 2011-05-22 21:45:01 +0000 | [diff] [blame] | 3402 | if (m_update_state != 1) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3403 | return; |
| 3404 | |
| 3405 | m_process_sp->SetPublicState (m_state); |
| 3406 | |
| 3407 | // If we're stopped and haven't restarted, then do the breakpoint commands here: |
| 3408 | if (m_state == eStateStopped && ! m_restarted) |
Jim Ingham | 0296fe7 | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 3409 | { |
| 3410 | ThreadList &curr_thread_list = m_process_sp->GetThreadList(); |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 3411 | uint32_t num_threads = curr_thread_list.GetSize(); |
| 3412 | uint32_t idx; |
Greg Clayton | 643ee73 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 3413 | |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 3414 | // The actions might change one of the thread's stop_info's opinions about whether we should |
| 3415 | // stop the process, so we need to query that as we go. |
Jim Ingham | 0296fe7 | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 3416 | |
| 3417 | // One other complication here, is that we try to catch any case where the target has run (except for expressions) |
| 3418 | // and immediately exit, but if we get that wrong (which is possible) then the thread list might have changed, and |
| 3419 | // that would cause our iteration here to crash. We could make a copy of the thread list, but we'd really like |
| 3420 | // to also know if it has changed at all, so we make up a vector of the thread ID's and check what we get back |
| 3421 | // against this list & bag out if anything differs. |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 3422 | std::vector<uint32_t> thread_index_array(num_threads); |
Jim Ingham | 0296fe7 | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 3423 | for (idx = 0; idx < num_threads; ++idx) |
| 3424 | thread_index_array[idx] = curr_thread_list.GetThreadAtIndex(idx)->GetIndexID(); |
| 3425 | |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 3426 | bool still_should_stop = true; |
| 3427 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3428 | for (idx = 0; idx < num_threads; ++idx) |
| 3429 | { |
Jim Ingham | 0296fe7 | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 3430 | curr_thread_list = m_process_sp->GetThreadList(); |
| 3431 | if (curr_thread_list.GetSize() != num_threads) |
| 3432 | { |
| 3433 | lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 60526c4 | 2011-12-01 20:26:15 +0000 | [diff] [blame] | 3434 | if (log) |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 3435 | log->Printf("Number of threads changed from %u to %u while processing event.", num_threads, curr_thread_list.GetSize()); |
Jim Ingham | 0296fe7 | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 3436 | break; |
| 3437 | } |
| 3438 | |
| 3439 | lldb::ThreadSP thread_sp = curr_thread_list.GetThreadAtIndex(idx); |
| 3440 | |
| 3441 | if (thread_sp->GetIndexID() != thread_index_array[idx]) |
| 3442 | { |
| 3443 | lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 60526c4 | 2011-12-01 20:26:15 +0000 | [diff] [blame] | 3444 | if (log) |
Greg Clayton | d9919d3 | 2011-12-01 23:28:38 +0000 | [diff] [blame] | 3445 | log->Printf("The thread at position %u changed from %u to %u while processing event.", |
Jim Ingham | 60526c4 | 2011-12-01 20:26:15 +0000 | [diff] [blame] | 3446 | idx, |
| 3447 | thread_index_array[idx], |
| 3448 | thread_sp->GetIndexID()); |
Jim Ingham | 0296fe7 | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 3449 | break; |
| 3450 | } |
| 3451 | |
Jim Ingham | 6297a3a | 2010-10-20 00:39:53 +0000 | [diff] [blame] | 3452 | StopInfoSP stop_info_sp = thread_sp->GetStopInfo (); |
| 3453 | if (stop_info_sp) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3454 | { |
Jim Ingham | 6297a3a | 2010-10-20 00:39:53 +0000 | [diff] [blame] | 3455 | stop_info_sp->PerformAction(event_ptr); |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 3456 | // The stop action might restart the target. If it does, then we want to mark that in the |
| 3457 | // event so that whoever is receiving it will know to wait for the running event and reflect |
| 3458 | // that state appropriately. |
| 3459 | // We also need to stop processing actions, since they aren't expecting the target to be running. |
Jim Ingham | 0296fe7 | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 3460 | |
| 3461 | // FIXME: we might have run. |
| 3462 | if (stop_info_sp->HasTargetRunSinceMe()) |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 3463 | { |
| 3464 | SetRestarted (true); |
| 3465 | break; |
| 3466 | } |
| 3467 | else if (!stop_info_sp->ShouldStop(event_ptr)) |
| 3468 | { |
| 3469 | still_should_stop = false; |
| 3470 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3471 | } |
| 3472 | } |
Jim Ingham | 6fb8baa | 2010-08-10 00:59:59 +0000 | [diff] [blame] | 3473 | |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 3474 | |
| 3475 | if (m_process_sp->GetPrivateState() != eStateRunning) |
Jim Ingham | d60d94a | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 3476 | { |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 3477 | if (!still_should_stop) |
| 3478 | { |
| 3479 | // We've been asked to continue, so do that here. |
Jim Ingham | d60d94a | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 3480 | SetRestarted(true); |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 3481 | m_process_sp->Resume(); |
| 3482 | } |
| 3483 | else |
| 3484 | { |
| 3485 | // If we didn't restart, run the Stop Hooks here: |
| 3486 | // They might also restart the target, so watch for that. |
| 3487 | m_process_sp->GetTarget().RunStopHooks(); |
| 3488 | if (m_process_sp->GetPrivateState() == eStateRunning) |
| 3489 | SetRestarted(true); |
| 3490 | } |
Jim Ingham | d60d94a | 2011-03-11 03:53:59 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3493 | } |
| 3494 | } |
| 3495 | |
| 3496 | void |
| 3497 | Process::ProcessEventData::Dump (Stream *s) const |
| 3498 | { |
| 3499 | if (m_process_sp) |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 3500 | s->Printf(" process = %p (pid = %llu), ", m_process_sp.get(), m_process_sp->GetID()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3501 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 3502 | s->Printf("state = %s", StateAsCString(GetState())); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
| 3505 | const Process::ProcessEventData * |
| 3506 | Process::ProcessEventData::GetEventDataFromEvent (const Event *event_ptr) |
| 3507 | { |
| 3508 | if (event_ptr) |
| 3509 | { |
| 3510 | const EventData *event_data = event_ptr->GetData(); |
| 3511 | if (event_data && event_data->GetFlavor() == ProcessEventData::GetFlavorString()) |
| 3512 | return static_cast <const ProcessEventData *> (event_ptr->GetData()); |
| 3513 | } |
| 3514 | return NULL; |
| 3515 | } |
| 3516 | |
| 3517 | ProcessSP |
| 3518 | Process::ProcessEventData::GetProcessFromEvent (const Event *event_ptr) |
| 3519 | { |
| 3520 | ProcessSP process_sp; |
| 3521 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 3522 | if (data) |
| 3523 | process_sp = data->GetProcessSP(); |
| 3524 | return process_sp; |
| 3525 | } |
| 3526 | |
| 3527 | StateType |
| 3528 | Process::ProcessEventData::GetStateFromEvent (const Event *event_ptr) |
| 3529 | { |
| 3530 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 3531 | if (data == NULL) |
| 3532 | return eStateInvalid; |
| 3533 | else |
| 3534 | return data->GetState(); |
| 3535 | } |
| 3536 | |
| 3537 | bool |
| 3538 | Process::ProcessEventData::GetRestartedFromEvent (const Event *event_ptr) |
| 3539 | { |
| 3540 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 3541 | if (data == NULL) |
| 3542 | return false; |
| 3543 | else |
| 3544 | return data->GetRestarted(); |
| 3545 | } |
| 3546 | |
| 3547 | void |
| 3548 | Process::ProcessEventData::SetRestartedInEvent (Event *event_ptr, bool new_value) |
| 3549 | { |
| 3550 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 3551 | if (data != NULL) |
| 3552 | data->SetRestarted(new_value); |
| 3553 | } |
| 3554 | |
| 3555 | bool |
Jim Ingham | 3ae449a | 2010-11-17 02:32:00 +0000 | [diff] [blame] | 3556 | Process::ProcessEventData::GetInterruptedFromEvent (const Event *event_ptr) |
| 3557 | { |
| 3558 | const ProcessEventData *data = GetEventDataFromEvent (event_ptr); |
| 3559 | if (data == NULL) |
| 3560 | return false; |
| 3561 | else |
| 3562 | return data->GetInterrupted (); |
| 3563 | } |
| 3564 | |
| 3565 | void |
| 3566 | Process::ProcessEventData::SetInterruptedInEvent (Event *event_ptr, bool new_value) |
| 3567 | { |
| 3568 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 3569 | if (data != NULL) |
| 3570 | data->SetInterrupted(new_value); |
| 3571 | } |
| 3572 | |
| 3573 | bool |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3574 | Process::ProcessEventData::SetUpdateStateOnRemoval (Event *event_ptr) |
| 3575 | { |
| 3576 | ProcessEventData *data = const_cast<ProcessEventData *>(GetEventDataFromEvent (event_ptr)); |
| 3577 | if (data) |
| 3578 | { |
| 3579 | data->SetUpdateStateOnRemoval(); |
| 3580 | return true; |
| 3581 | } |
| 3582 | return false; |
| 3583 | } |
| 3584 | |
Greg Clayton | 289afcb | 2012-02-18 05:35:26 +0000 | [diff] [blame] | 3585 | lldb::TargetSP |
| 3586 | Process::CalculateTarget () |
| 3587 | { |
| 3588 | return m_target.shared_from_this(); |
| 3589 | } |
| 3590 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3591 | void |
Greg Clayton | a830adb | 2010-10-04 01:05:56 +0000 | [diff] [blame] | 3592 | Process::CalculateExecutionContext (ExecutionContext &exe_ctx) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3593 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 3594 | exe_ctx.SetTargetPtr (&m_target); |
| 3595 | exe_ctx.SetProcessPtr (this); |
| 3596 | exe_ctx.SetThreadPtr(NULL); |
| 3597 | exe_ctx.SetFramePtr (NULL); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3598 | } |
| 3599 | |
Greg Clayton | e4b9c1f | 2011-03-08 22:40:15 +0000 | [diff] [blame] | 3600 | //uint32_t |
| 3601 | //Process::ListProcessesMatchingName (const char *name, StringList &matches, std::vector<lldb::pid_t> &pids) |
| 3602 | //{ |
| 3603 | // return 0; |
| 3604 | //} |
| 3605 | // |
| 3606 | //ArchSpec |
| 3607 | //Process::GetArchSpecForExistingProcess (lldb::pid_t pid) |
| 3608 | //{ |
| 3609 | // return Host::GetArchSpecForExistingProcess (pid); |
| 3610 | //} |
| 3611 | // |
| 3612 | //ArchSpec |
| 3613 | //Process::GetArchSpecForExistingProcess (const char *process_name) |
| 3614 | //{ |
| 3615 | // return Host::GetArchSpecForExistingProcess (process_name); |
| 3616 | //} |
| 3617 | // |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3618 | void |
| 3619 | Process::AppendSTDOUT (const char * s, size_t len) |
| 3620 | { |
Greg Clayton | 20d338f | 2010-11-18 05:57:03 +0000 | [diff] [blame] | 3621 | Mutex::Locker locker (m_stdio_communication_mutex); |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3622 | m_stdout_data.append (s, len); |
Greg Clayton | b378133 | 2010-12-05 19:16:56 +0000 | [diff] [blame] | 3623 | BroadcastEventIfUnique (eBroadcastBitSTDOUT, new ProcessEventData (GetTarget().GetProcessSP(), GetState())); |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3624 | } |
| 3625 | |
| 3626 | void |
Greg Clayton | bd06ff4 | 2011-11-13 04:45:22 +0000 | [diff] [blame] | 3627 | Process::AppendSTDERR (const char * s, size_t len) |
| 3628 | { |
| 3629 | Mutex::Locker locker (m_stdio_communication_mutex); |
| 3630 | m_stderr_data.append (s, len); |
| 3631 | BroadcastEventIfUnique (eBroadcastBitSTDERR, new ProcessEventData (GetTarget().GetProcessSP(), GetState())); |
| 3632 | } |
| 3633 | |
| 3634 | //------------------------------------------------------------------ |
| 3635 | // Process STDIO |
| 3636 | //------------------------------------------------------------------ |
| 3637 | |
| 3638 | size_t |
| 3639 | Process::GetSTDOUT (char *buf, size_t buf_size, Error &error) |
| 3640 | { |
| 3641 | Mutex::Locker locker(m_stdio_communication_mutex); |
| 3642 | size_t bytes_available = m_stdout_data.size(); |
| 3643 | if (bytes_available > 0) |
| 3644 | { |
| 3645 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3646 | if (log) |
| 3647 | log->Printf ("Process::GetSTDOUT (buf = %p, size = %zu)", buf, buf_size); |
| 3648 | if (bytes_available > buf_size) |
| 3649 | { |
| 3650 | memcpy(buf, m_stdout_data.c_str(), buf_size); |
| 3651 | m_stdout_data.erase(0, buf_size); |
| 3652 | bytes_available = buf_size; |
| 3653 | } |
| 3654 | else |
| 3655 | { |
| 3656 | memcpy(buf, m_stdout_data.c_str(), bytes_available); |
| 3657 | m_stdout_data.clear(); |
| 3658 | } |
| 3659 | } |
| 3660 | return bytes_available; |
| 3661 | } |
| 3662 | |
| 3663 | |
| 3664 | size_t |
| 3665 | Process::GetSTDERR (char *buf, size_t buf_size, Error &error) |
| 3666 | { |
| 3667 | Mutex::Locker locker(m_stdio_communication_mutex); |
| 3668 | size_t bytes_available = m_stderr_data.size(); |
| 3669 | if (bytes_available > 0) |
| 3670 | { |
| 3671 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3672 | if (log) |
| 3673 | log->Printf ("Process::GetSTDERR (buf = %p, size = %zu)", buf, buf_size); |
| 3674 | if (bytes_available > buf_size) |
| 3675 | { |
| 3676 | memcpy(buf, m_stderr_data.c_str(), buf_size); |
| 3677 | m_stderr_data.erase(0, buf_size); |
| 3678 | bytes_available = buf_size; |
| 3679 | } |
| 3680 | else |
| 3681 | { |
| 3682 | memcpy(buf, m_stderr_data.c_str(), bytes_available); |
| 3683 | m_stderr_data.clear(); |
| 3684 | } |
| 3685 | } |
| 3686 | return bytes_available; |
| 3687 | } |
| 3688 | |
| 3689 | void |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3690 | Process::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len) |
| 3691 | { |
| 3692 | Process *process = (Process *) baton; |
| 3693 | process->AppendSTDOUT (static_cast<const char *>(src), src_len); |
| 3694 | } |
| 3695 | |
| 3696 | size_t |
| 3697 | Process::ProcessInputReaderCallback (void *baton, |
| 3698 | InputReader &reader, |
| 3699 | lldb::InputReaderAction notification, |
| 3700 | const char *bytes, |
| 3701 | size_t bytes_len) |
| 3702 | { |
| 3703 | Process *process = (Process *) baton; |
| 3704 | |
| 3705 | switch (notification) |
| 3706 | { |
| 3707 | case eInputReaderActivate: |
| 3708 | break; |
| 3709 | |
| 3710 | case eInputReaderDeactivate: |
| 3711 | break; |
| 3712 | |
| 3713 | case eInputReaderReactivate: |
| 3714 | break; |
| 3715 | |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 3716 | case eInputReaderAsynchronousOutputWritten: |
| 3717 | break; |
| 3718 | |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3719 | case eInputReaderGotToken: |
| 3720 | { |
| 3721 | Error error; |
| 3722 | process->PutSTDIN (bytes, bytes_len, error); |
| 3723 | } |
| 3724 | break; |
| 3725 | |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 3726 | case eInputReaderInterrupt: |
| 3727 | process->Halt (); |
| 3728 | break; |
| 3729 | |
| 3730 | case eInputReaderEndOfFile: |
| 3731 | process->AppendSTDOUT ("^D", 2); |
| 3732 | break; |
| 3733 | |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3734 | case eInputReaderDone: |
| 3735 | break; |
| 3736 | |
| 3737 | } |
| 3738 | |
| 3739 | return bytes_len; |
| 3740 | } |
| 3741 | |
| 3742 | void |
| 3743 | Process::ResetProcessInputReader () |
| 3744 | { |
| 3745 | m_process_input_reader.reset(); |
| 3746 | } |
| 3747 | |
| 3748 | void |
Greg Clayton | 464c616 | 2011-11-17 22:14:31 +0000 | [diff] [blame] | 3749 | Process::SetSTDIOFileDescriptor (int file_descriptor) |
Caroline Tice | 861efb3 | 2010-11-16 05:07:41 +0000 | [diff] [blame] | 3750 | { |
| 3751 | // First set up the Read Thread for reading/handling process I/O |
| 3752 | |
| 3753 | std::auto_ptr<ConnectionFileDescriptor> conn_ap (new ConnectionFileDescriptor (file_descriptor, true)); |
| 3754 | |
| 3755 | if (conn_ap.get()) |
| 3756 | { |
| 3757 | m_stdio_communication.SetConnection (conn_ap.release()); |
| 3758 | if (m_stdio_communication.IsConnected()) |
| 3759 | { |
| 3760 | m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this); |
| 3761 | m_stdio_communication.StartReadThread(); |
| 3762 | |
| 3763 | // Now read thread is set up, set up input reader. |
| 3764 | |
| 3765 | if (!m_process_input_reader.get()) |
| 3766 | { |
| 3767 | m_process_input_reader.reset (new InputReader(m_target.GetDebugger())); |
| 3768 | Error err (m_process_input_reader->Initialize (Process::ProcessInputReaderCallback, |
| 3769 | this, |
| 3770 | eInputReaderGranularityByte, |
| 3771 | NULL, |
| 3772 | NULL, |
| 3773 | false)); |
| 3774 | |
| 3775 | if (err.Fail()) |
| 3776 | m_process_input_reader.reset(); |
| 3777 | } |
| 3778 | } |
| 3779 | } |
| 3780 | } |
| 3781 | |
| 3782 | void |
| 3783 | Process::PushProcessInputReader () |
| 3784 | { |
| 3785 | if (m_process_input_reader && !m_process_input_reader->IsActive()) |
| 3786 | m_target.GetDebugger().PushInputReader (m_process_input_reader); |
| 3787 | } |
| 3788 | |
| 3789 | void |
| 3790 | Process::PopProcessInputReader () |
| 3791 | { |
| 3792 | if (m_process_input_reader && m_process_input_reader->IsActive()) |
| 3793 | m_target.GetDebugger().PopInputReader (m_process_input_reader); |
| 3794 | } |
| 3795 | |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 3796 | // The process needs to know about installed plug-ins |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 3797 | void |
Caroline Tice | 2a45681 | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 3798 | Process::SettingsInitialize () |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3799 | { |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 3800 | static std::vector<OptionEnumValueElement> g_plugins; |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 3801 | |
| 3802 | int i=0; |
| 3803 | const char *name; |
| 3804 | OptionEnumValueElement option_enum; |
| 3805 | while ((name = PluginManager::GetProcessPluginNameAtIndex (i)) != NULL) |
| 3806 | { |
| 3807 | if (name) |
| 3808 | { |
| 3809 | option_enum.value = i; |
| 3810 | option_enum.string_value = name; |
| 3811 | option_enum.usage = PluginManager::GetProcessPluginDescriptionAtIndex (i); |
| 3812 | g_plugins.push_back (option_enum); |
| 3813 | } |
| 3814 | ++i; |
| 3815 | } |
| 3816 | option_enum.value = 0; |
| 3817 | option_enum.string_value = NULL; |
| 3818 | option_enum.usage = NULL; |
| 3819 | g_plugins.push_back (option_enum); |
| 3820 | |
| 3821 | for (i=0; (name = SettingsController::instance_settings_table[i].var_name); ++i) |
| 3822 | { |
| 3823 | if (::strcmp (name, "plugin") == 0) |
| 3824 | { |
| 3825 | SettingsController::instance_settings_table[i].enum_values = &g_plugins[0]; |
| 3826 | break; |
| 3827 | } |
| 3828 | } |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 3829 | UserSettingsControllerSP &usc = GetSettingsController(); |
| 3830 | usc.reset (new SettingsController); |
| 3831 | UserSettingsController::InitializeSettingsController (usc, |
| 3832 | SettingsController::global_settings_table, |
| 3833 | SettingsController::instance_settings_table); |
Caroline Tice | 2a45681 | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 3834 | |
| 3835 | // Now call SettingsInitialize() for each 'child' of Process settings |
| 3836 | Thread::SettingsInitialize (); |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 3837 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3838 | |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 3839 | void |
Caroline Tice | 2a45681 | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 3840 | Process::SettingsTerminate () |
Greg Clayton | d284b66 | 2011-02-18 01:44:25 +0000 | [diff] [blame] | 3841 | { |
Caroline Tice | 2a45681 | 2011-03-10 22:14:10 +0000 | [diff] [blame] | 3842 | // Must call SettingsTerminate() on each 'child' of Process settings before terminating Process settings. |
| 3843 | |
| 3844 | Thread::SettingsTerminate (); |
| 3845 | |
| 3846 | // Now terminate Process Settings. |
| 3847 | |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 3848 | UserSettingsControllerSP &usc = GetSettingsController(); |
| 3849 | UserSettingsController::FinalizeSettingsController (usc); |
| 3850 | usc.reset(); |
| 3851 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3852 | |
Greg Clayton | 990de7b | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 3853 | UserSettingsControllerSP & |
| 3854 | Process::GetSettingsController () |
| 3855 | { |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 3856 | static UserSettingsControllerSP g_settings_controller_sp; |
| 3857 | if (!g_settings_controller_sp) |
| 3858 | { |
| 3859 | g_settings_controller_sp.reset (new Process::SettingsController); |
| 3860 | // The first shared pointer to Process::SettingsController in |
| 3861 | // g_settings_controller_sp must be fully created above so that |
| 3862 | // the TargetInstanceSettings can use a weak_ptr to refer back |
| 3863 | // to the master setttings controller |
| 3864 | InstanceSettingsSP default_instance_settings_sp (new ProcessInstanceSettings (g_settings_controller_sp, |
| 3865 | false, |
| 3866 | InstanceSettings::GetDefaultName().AsCString())); |
| 3867 | g_settings_controller_sp->SetDefaultInstanceSettings (default_instance_settings_sp); |
| 3868 | } |
| 3869 | return g_settings_controller_sp; |
| 3870 | |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 3873 | void |
| 3874 | Process::UpdateInstanceName () |
| 3875 | { |
Greg Clayton | 5beb99d | 2011-08-11 02:48:45 +0000 | [diff] [blame] | 3876 | Module *module = GetTarget().GetExecutableModulePointer(); |
Greg Clayton | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 3877 | if (module && module->GetFileSpec().GetFilename()) |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 3878 | { |
Greg Clayton | c0c1b0c | 2010-11-19 03:46:01 +0000 | [diff] [blame] | 3879 | GetSettingsController()->RenameInstanceSettings (GetInstanceName().AsCString(), |
Greg Clayton | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 3880 | module->GetFileSpec().GetFilename().AsCString()); |
Caroline Tice | 1ebef44 | 2010-09-27 00:30:10 +0000 | [diff] [blame] | 3881 | } |
| 3882 | } |
| 3883 | |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 3884 | ExecutionResults |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 3885 | Process::RunThreadPlan (ExecutionContext &exe_ctx, |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3886 | lldb::ThreadPlanSP &thread_plan_sp, |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 3887 | bool stop_others, |
| 3888 | bool try_all_threads, |
| 3889 | bool discard_on_error, |
| 3890 | uint32_t single_thread_timeout_usec, |
| 3891 | Stream &errors) |
| 3892 | { |
| 3893 | ExecutionResults return_value = eExecutionSetupError; |
| 3894 | |
Jim Ingham | 15dcb7c | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 3895 | if (thread_plan_sp.get() == NULL) |
| 3896 | { |
| 3897 | errors.Printf("RunThreadPlan called with empty thread plan."); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 3898 | return eExecutionSetupError; |
Jim Ingham | 15dcb7c | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 3899 | } |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 3900 | |
| 3901 | if (exe_ctx.GetProcessPtr() != this) |
| 3902 | { |
| 3903 | errors.Printf("RunThreadPlan called on wrong process."); |
| 3904 | return eExecutionSetupError; |
| 3905 | } |
| 3906 | |
| 3907 | Thread *thread = exe_ctx.GetThreadPtr(); |
| 3908 | if (thread == NULL) |
| 3909 | { |
| 3910 | errors.Printf("RunThreadPlan called with invalid thread."); |
| 3911 | return eExecutionSetupError; |
| 3912 | } |
Jim Ingham | 15dcb7c | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 3913 | |
Jim Ingham | 5ab7fba | 2011-05-17 22:24:54 +0000 | [diff] [blame] | 3914 | // We rely on the thread plan we are running returning "PlanCompleted" if when it successfully completes. |
| 3915 | // For that to be true the plan can't be private - since private plans suppress themselves in the |
| 3916 | // GetCompletedPlan call. |
| 3917 | |
| 3918 | bool orig_plan_private = thread_plan_sp->GetPrivate(); |
| 3919 | thread_plan_sp->SetPrivate(false); |
| 3920 | |
Jim Ingham | ac95966 | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 3921 | if (m_private_state.GetValue() != eStateStopped) |
| 3922 | { |
| 3923 | errors.Printf ("RunThreadPlan called while the private state was not stopped."); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 3924 | return eExecutionSetupError; |
Jim Ingham | ac95966 | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
Jim Ingham | 7bbebaf | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 3927 | // Save the thread & frame from the exe_ctx for restoration after we run |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 3928 | const uint32_t thread_idx_id = thread->GetIndexID(); |
| 3929 | StackID ctx_frame_id = thread->GetSelectedFrame()->GetStackID(); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 3930 | |
| 3931 | // N.B. Running the target may unset the currently selected thread and frame. We don't want to do that either, |
| 3932 | // so we should arrange to reset them as well. |
| 3933 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 3934 | lldb::ThreadSP selected_thread_sp = GetThreadList().GetSelectedThread(); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 3935 | |
Jim Ingham | 7bbebaf | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 3936 | uint32_t selected_tid; |
| 3937 | StackID selected_stack_id; |
Greg Clayton | e40b642 | 2011-09-18 18:59:15 +0000 | [diff] [blame] | 3938 | if (selected_thread_sp) |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 3939 | { |
| 3940 | selected_tid = selected_thread_sp->GetIndexID(); |
Jim Ingham | 7bbebaf | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 3941 | selected_stack_id = selected_thread_sp->GetSelectedFrame()->GetStackID(); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 3942 | } |
| 3943 | else |
| 3944 | { |
| 3945 | selected_tid = LLDB_INVALID_THREAD_ID; |
| 3946 | } |
| 3947 | |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3948 | lldb::thread_t backup_private_state_thread = LLDB_INVALID_HOST_THREAD; |
Jim Ingham | d21d98b | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 3949 | lldb::StateType old_state; |
| 3950 | lldb::ThreadPlanSP stopper_base_plan_sp; |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3951 | |
Jim Ingham | d21d98b | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 3952 | lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_STEP | LIBLLDB_LOG_PROCESS)); |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3953 | if (Host::GetCurrentThread() == m_private_state_thread) |
| 3954 | { |
Jim Ingham | d21d98b | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 3955 | // Yikes, we are running on the private state thread! So we can't wait for public events on this thread, since |
| 3956 | // we are the thread that is generating public events. |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3957 | // The simplest thing to do is to spin up a temporary thread to handle private state thread events while |
Jim Ingham | d21d98b | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 3958 | // we are fielding public events here. |
| 3959 | if (log) |
| 3960 | log->Printf ("Running thread plan on private state thread, spinning up another state thread to handle the events."); |
| 3961 | |
| 3962 | |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3963 | backup_private_state_thread = m_private_state_thread; |
Jim Ingham | d21d98b | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 3964 | |
| 3965 | // One other bit of business: we want to run just this thread plan and anything it pushes, and then stop, |
| 3966 | // returning control here. |
| 3967 | // But in the normal course of things, the plan above us on the stack would be given a shot at the stop |
| 3968 | // event before deciding to stop, and we don't want that. So we insert a "stopper" base plan on the stack |
| 3969 | // before the plan we want to run. Since base plans always stop and return control to the user, that will |
| 3970 | // do just what we want. |
| 3971 | stopper_base_plan_sp.reset(new ThreadPlanBase (*thread)); |
| 3972 | thread->QueueThreadPlan (stopper_base_plan_sp, false); |
| 3973 | // Have to make sure our public state is stopped, since otherwise the reporting logic below doesn't work correctly. |
| 3974 | old_state = m_public_state.GetValue(); |
| 3975 | m_public_state.SetValueNoLock(eStateStopped); |
| 3976 | |
| 3977 | // Now spin up the private state thread: |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 3978 | StartPrivateStateThread(true); |
| 3979 | } |
| 3980 | |
| 3981 | thread->QueueThreadPlan(thread_plan_sp, false); // This used to pass "true" does that make sense? |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 3982 | |
Jim Ingham | 6ae318c | 2011-01-23 21:14:08 +0000 | [diff] [blame] | 3983 | Listener listener("lldb.process.listener.run-thread-plan"); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3984 | |
| 3985 | // This process event hijacker Hijacks the Public events and its destructor makes sure that the process events get |
| 3986 | // restored on exit to the function. |
| 3987 | |
| 3988 | ProcessEventHijacker run_thread_plan_hijacker (*this, &listener); |
Jim Ingham | ac95966 | 2011-01-24 06:34:17 +0000 | [diff] [blame] | 3989 | |
Jim Ingham | 15dcb7c | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 3990 | if (log) |
| 3991 | { |
| 3992 | StreamString s; |
| 3993 | thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose); |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 3994 | log->Printf ("Process::RunThreadPlan(): Resuming thread %u - 0x%4.4llx to run thread plan \"%s\".", |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 3995 | thread->GetIndexID(), |
| 3996 | thread->GetID(), |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 3997 | s.GetData()); |
Jim Ingham | 15dcb7c | 2011-01-20 02:03:18 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4000 | bool got_event; |
| 4001 | lldb::EventSP event_sp; |
| 4002 | lldb::StateType stop_state = lldb::eStateInvalid; |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4003 | |
| 4004 | TimeValue* timeout_ptr = NULL; |
| 4005 | TimeValue real_timeout; |
| 4006 | |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4007 | bool first_timeout = true; |
| 4008 | bool do_resume = true; |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4009 | |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4010 | while (1) |
| 4011 | { |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4012 | // We usually want to resume the process if we get to the top of the loop. |
| 4013 | // The only exception is if we get two running events with no intervening |
| 4014 | // 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] | 4015 | |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4016 | if (do_resume) |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4017 | { |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4018 | // Do the initial resume and wait for the running event before going further. |
| 4019 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4020 | Error resume_error = Resume (); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4021 | if (!resume_error.Success()) |
| 4022 | { |
| 4023 | errors.Printf("Error resuming inferior: \"%s\".\n", resume_error.AsCString()); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4024 | return_value = eExecutionSetupError; |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4025 | break; |
| 4026 | } |
| 4027 | |
| 4028 | real_timeout = TimeValue::Now(); |
| 4029 | real_timeout.OffsetWithMicroSeconds(500000); |
| 4030 | timeout_ptr = &real_timeout; |
| 4031 | |
Sean Callanan | faf0478 | 2012-01-05 02:00:14 +0000 | [diff] [blame] | 4032 | got_event = listener.WaitForEvent(timeout_ptr, event_sp); |
Jim Ingham | d21d98b | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4033 | if (!got_event) |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4034 | { |
| 4035 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4036 | log->PutCString("Didn't get any event after initial resume, exiting."); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4037 | |
| 4038 | errors.Printf("Didn't get any event after initial resume, exiting."); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4039 | return_value = eExecutionSetupError; |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4040 | break; |
| 4041 | } |
| 4042 | |
| 4043 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 4044 | if (stop_state != eStateRunning) |
| 4045 | { |
| 4046 | if (log) |
| 4047 | log->Printf("Didn't get running event after initial resume, got %s instead.", StateAsCString(stop_state)); |
| 4048 | |
| 4049 | 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] | 4050 | return_value = eExecutionSetupError; |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4051 | break; |
| 4052 | } |
| 4053 | |
| 4054 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4055 | log->PutCString ("Resuming succeeded."); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4056 | // We need to call the function synchronously, so spin waiting for it to return. |
| 4057 | // If we get interrupted while executing, we're going to lose our context, and |
| 4058 | // won't be able to gather the result at this point. |
| 4059 | // We set the timeout AFTER the resume, since the resume takes some time and we |
| 4060 | // don't want to charge that to the timeout. |
| 4061 | |
| 4062 | if (single_thread_timeout_usec != 0) |
| 4063 | { |
| 4064 | real_timeout = TimeValue::Now(); |
| 4065 | if (first_timeout) |
| 4066 | real_timeout.OffsetWithMicroSeconds(single_thread_timeout_usec); |
| 4067 | else |
| 4068 | real_timeout.OffsetWithSeconds(10); |
| 4069 | |
| 4070 | timeout_ptr = &real_timeout; |
| 4071 | } |
| 4072 | } |
| 4073 | else |
| 4074 | { |
| 4075 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4076 | log->PutCString ("Handled an extra running event."); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4077 | do_resume = true; |
| 4078 | } |
| 4079 | |
| 4080 | // Now wait for the process to stop again: |
| 4081 | stop_state = lldb::eStateInvalid; |
| 4082 | event_sp.reset(); |
| 4083 | got_event = listener.WaitForEvent (timeout_ptr, event_sp); |
| 4084 | |
| 4085 | if (got_event) |
| 4086 | { |
| 4087 | if (event_sp.get()) |
| 4088 | { |
| 4089 | bool keep_going = false; |
| 4090 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 4091 | if (log) |
| 4092 | log->Printf("In while loop, got event: %s.", StateAsCString(stop_state)); |
| 4093 | |
| 4094 | switch (stop_state) |
| 4095 | { |
| 4096 | case lldb::eStateStopped: |
Jim Ingham | 2370a97 | 2011-05-17 01:10:11 +0000 | [diff] [blame] | 4097 | { |
Greg Clayton | 4399446 | 2011-06-03 22:12:42 +0000 | [diff] [blame] | 4098 | // Yay, we're done. Now make sure that our thread plan actually completed. |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4099 | ThreadSP thread_sp = GetThreadList().FindThreadByIndexID (thread_idx_id); |
Greg Clayton | 4399446 | 2011-06-03 22:12:42 +0000 | [diff] [blame] | 4100 | if (!thread_sp) |
Jim Ingham | 2370a97 | 2011-05-17 01:10:11 +0000 | [diff] [blame] | 4101 | { |
Greg Clayton | 4399446 | 2011-06-03 22:12:42 +0000 | [diff] [blame] | 4102 | // Ooh, our thread has vanished. Unlikely that this was successful execution... |
Jim Ingham | 2370a97 | 2011-05-17 01:10:11 +0000 | [diff] [blame] | 4103 | if (log) |
Greg Clayton | 4399446 | 2011-06-03 22:12:42 +0000 | [diff] [blame] | 4104 | log->Printf ("Execution completed but our thread (index-id=%u) has vanished.", thread_idx_id); |
| 4105 | return_value = eExecutionInterrupted; |
Jim Ingham | 2370a97 | 2011-05-17 01:10:11 +0000 | [diff] [blame] | 4106 | } |
| 4107 | else |
| 4108 | { |
Greg Clayton | 4399446 | 2011-06-03 22:12:42 +0000 | [diff] [blame] | 4109 | StopInfoSP stop_info_sp (thread_sp->GetStopInfo ()); |
| 4110 | StopReason stop_reason = eStopReasonInvalid; |
| 4111 | if (stop_info_sp) |
| 4112 | stop_reason = stop_info_sp->GetStopReason(); |
| 4113 | if (stop_reason == eStopReasonPlanComplete) |
| 4114 | { |
| 4115 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4116 | log->PutCString ("Execution completed successfully."); |
Greg Clayton | 4399446 | 2011-06-03 22:12:42 +0000 | [diff] [blame] | 4117 | // Now mark this plan as private so it doesn't get reported as the stop reason |
| 4118 | // after this point. |
| 4119 | if (thread_plan_sp) |
| 4120 | thread_plan_sp->SetPrivate (orig_plan_private); |
| 4121 | return_value = eExecutionCompleted; |
| 4122 | } |
| 4123 | else |
| 4124 | { |
| 4125 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4126 | log->PutCString ("Thread plan didn't successfully complete."); |
Greg Clayton | 4399446 | 2011-06-03 22:12:42 +0000 | [diff] [blame] | 4127 | |
| 4128 | return_value = eExecutionInterrupted; |
| 4129 | } |
Jim Ingham | 2370a97 | 2011-05-17 01:10:11 +0000 | [diff] [blame] | 4130 | } |
Greg Clayton | 4399446 | 2011-06-03 22:12:42 +0000 | [diff] [blame] | 4131 | } |
| 4132 | break; |
| 4133 | |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4134 | case lldb::eStateCrashed: |
| 4135 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4136 | log->PutCString ("Execution crashed."); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4137 | return_value = eExecutionInterrupted; |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4138 | break; |
Greg Clayton | 4399446 | 2011-06-03 22:12:42 +0000 | [diff] [blame] | 4139 | |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4140 | case lldb::eStateRunning: |
| 4141 | do_resume = false; |
| 4142 | keep_going = true; |
| 4143 | break; |
Greg Clayton | 4399446 | 2011-06-03 22:12:42 +0000 | [diff] [blame] | 4144 | |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4145 | default: |
| 4146 | if (log) |
| 4147 | log->Printf("Execution stopped with unexpected state: %s.", StateAsCString(stop_state)); |
Jim Ingham | 2370a97 | 2011-05-17 01:10:11 +0000 | [diff] [blame] | 4148 | |
| 4149 | errors.Printf ("Execution stopped with unexpected state."); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4150 | return_value = eExecutionInterrupted; |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4151 | break; |
| 4152 | } |
| 4153 | if (keep_going) |
| 4154 | continue; |
| 4155 | else |
| 4156 | break; |
| 4157 | } |
| 4158 | else |
| 4159 | { |
| 4160 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4161 | log->PutCString ("got_event was true, but the event pointer was null. How odd..."); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4162 | return_value = eExecutionInterrupted; |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4163 | break; |
| 4164 | } |
| 4165 | } |
| 4166 | else |
| 4167 | { |
| 4168 | // If we didn't get an event that means we've timed out... |
| 4169 | // We will interrupt the process here. Depending on what we were asked to do we will |
| 4170 | // either exit, or try with all threads running for the same timeout. |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4171 | // Not really sure what to do if Halt fails here... |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4172 | |
Stephen Wilson | c2b9825 | 2011-01-12 04:20:03 +0000 | [diff] [blame] | 4173 | if (log) { |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4174 | if (try_all_threads) |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4175 | { |
| 4176 | if (first_timeout) |
| 4177 | log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, " |
| 4178 | "trying with all threads enabled.", |
| 4179 | single_thread_timeout_usec); |
| 4180 | else |
| 4181 | log->Printf ("Process::RunThreadPlan(): Restarting function with all threads enabled " |
| 4182 | "and timeout: %d timed out.", |
| 4183 | single_thread_timeout_usec); |
| 4184 | } |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4185 | else |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4186 | log->Printf ("Process::RunThreadPlan(): Running function with timeout: %d timed out, " |
| 4187 | "halt and abandoning execution.", |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4188 | single_thread_timeout_usec); |
Stephen Wilson | c2b9825 | 2011-01-12 04:20:03 +0000 | [diff] [blame] | 4189 | } |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4190 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4191 | Error halt_error = Halt(); |
Jim Ingham | c556b46 | 2011-01-22 01:30:53 +0000 | [diff] [blame] | 4192 | if (halt_error.Success()) |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4193 | { |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4194 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4195 | log->PutCString ("Process::RunThreadPlan(): Halt succeeded."); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4196 | |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4197 | // If halt succeeds, it always produces a stopped event. Wait for that: |
| 4198 | |
| 4199 | real_timeout = TimeValue::Now(); |
| 4200 | real_timeout.OffsetWithMicroSeconds(500000); |
| 4201 | |
| 4202 | got_event = listener.WaitForEvent(&real_timeout, event_sp); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4203 | |
| 4204 | if (got_event) |
| 4205 | { |
| 4206 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 4207 | if (log) |
| 4208 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 4209 | log->Printf ("Process::RunThreadPlan(): Stopped with event: %s", StateAsCString(stop_state)); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4210 | if (stop_state == lldb::eStateStopped |
| 4211 | && Process::ProcessEventData::GetInterruptedFromEvent(event_sp.get())) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4212 | log->PutCString (" Event was the Halt interruption event."); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4215 | if (stop_state == lldb::eStateStopped) |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4216 | { |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4217 | // Between the time we initiated the Halt and the time we delivered it, the process could have |
| 4218 | // already finished its job. Check that here: |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4219 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4220 | if (thread->IsThreadPlanDone (thread_plan_sp.get())) |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4221 | { |
| 4222 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4223 | log->PutCString ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. " |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4224 | "Exiting wait loop."); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4225 | return_value = eExecutionCompleted; |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4226 | break; |
| 4227 | } |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4228 | |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4229 | if (!try_all_threads) |
| 4230 | { |
| 4231 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4232 | log->PutCString ("try_all_threads was false, we stopped so now we're quitting."); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4233 | return_value = eExecutionInterrupted; |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4234 | break; |
| 4235 | } |
| 4236 | |
| 4237 | if (first_timeout) |
| 4238 | { |
| 4239 | // Set all the other threads to run, and return to the top of the loop, which will continue; |
| 4240 | first_timeout = false; |
| 4241 | thread_plan_sp->SetStopOthers (false); |
| 4242 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4243 | log->PutCString ("Process::RunThreadPlan(): About to resume."); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4244 | |
| 4245 | continue; |
| 4246 | } |
| 4247 | else |
| 4248 | { |
| 4249 | // Running all threads failed, so return Interrupted. |
| 4250 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4251 | log->PutCString("Process::RunThreadPlan(): running all threads timed out."); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4252 | return_value = eExecutionInterrupted; |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4253 | break; |
| 4254 | } |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4255 | } |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4256 | } |
| 4257 | else |
| 4258 | { if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4259 | log->PutCString("Process::RunThreadPlan(): halt said it succeeded, but I got no event. " |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4260 | "I'm getting out of here passing Interrupted."); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4261 | return_value = eExecutionInterrupted; |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4262 | break; |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4263 | } |
| 4264 | } |
Jim Ingham | c556b46 | 2011-01-22 01:30:53 +0000 | [diff] [blame] | 4265 | else |
| 4266 | { |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4267 | // This branch is to work around some problems with gdb-remote's Halt. It is a little racy, and can return |
| 4268 | // 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] | 4269 | if (log) |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4270 | 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.", |
| 4271 | halt_error.AsCString()); |
| 4272 | real_timeout = TimeValue::Now(); |
| 4273 | real_timeout.OffsetWithMicroSeconds(500000); |
| 4274 | timeout_ptr = &real_timeout; |
| 4275 | got_event = listener.WaitForEvent(&real_timeout, event_sp); |
| 4276 | if (!got_event || event_sp.get() == NULL) |
Jim Ingham | 6ae318c | 2011-01-23 21:14:08 +0000 | [diff] [blame] | 4277 | { |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4278 | // This is not going anywhere, bag out. |
| 4279 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4280 | log->PutCString ("Process::RunThreadPlan(): halt failed: and waiting for the stopped event failed."); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4281 | return_value = eExecutionInterrupted; |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4282 | break; |
Jim Ingham | 6ae318c | 2011-01-23 21:14:08 +0000 | [diff] [blame] | 4283 | } |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4284 | else |
| 4285 | { |
| 4286 | stop_state = Process::ProcessEventData::GetStateFromEvent(event_sp.get()); |
| 4287 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4288 | log->PutCString ("Process::RunThreadPlan(): halt failed: but then I got a stopped event. Whatever..."); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4289 | if (stop_state == lldb::eStateStopped) |
| 4290 | { |
| 4291 | // Between the time we initiated the Halt and the time we delivered it, the process could have |
| 4292 | // already finished its job. Check that here: |
| 4293 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4294 | if (thread->IsThreadPlanDone (thread_plan_sp.get())) |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4295 | { |
| 4296 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4297 | log->PutCString ("Process::RunThreadPlan(): Even though we timed out, the call plan was done. " |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4298 | "Exiting wait loop."); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4299 | return_value = eExecutionCompleted; |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4300 | break; |
| 4301 | } |
| 4302 | |
| 4303 | if (first_timeout) |
| 4304 | { |
| 4305 | // Set all the other threads to run, and return to the top of the loop, which will continue; |
| 4306 | first_timeout = false; |
| 4307 | thread_plan_sp->SetStopOthers (false); |
| 4308 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4309 | log->PutCString ("Process::RunThreadPlan(): About to resume."); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4310 | |
| 4311 | continue; |
| 4312 | } |
| 4313 | else |
| 4314 | { |
| 4315 | // Running all threads failed, so return Interrupted. |
| 4316 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4317 | log->PutCString ("Process::RunThreadPlan(): running all threads timed out."); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4318 | return_value = eExecutionInterrupted; |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4319 | break; |
| 4320 | } |
| 4321 | } |
| 4322 | else |
| 4323 | { |
Sean Callanan | ed3f86b | 2011-08-09 22:07:08 +0000 | [diff] [blame] | 4324 | if (log) |
| 4325 | log->Printf ("Process::RunThreadPlan(): halt failed, I waited and didn't get" |
| 4326 | " a stopped event, instead got %s.", StateAsCString(stop_state)); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4327 | return_value = eExecutionInterrupted; |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4328 | break; |
| 4329 | } |
| 4330 | } |
Jim Ingham | c556b46 | 2011-01-22 01:30:53 +0000 | [diff] [blame] | 4331 | } |
| 4332 | |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4335 | } // END WAIT LOOP |
| 4336 | |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4337 | // If we had to start up a temporary private state thread to run this thread plan, shut it down now. |
| 4338 | if (IS_VALID_LLDB_HOST_THREAD(backup_private_state_thread)) |
| 4339 | { |
| 4340 | StopPrivateStateThread(); |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4341 | Error error; |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4342 | m_private_state_thread = backup_private_state_thread; |
Jim Ingham | d21d98b | 2012-04-10 01:21:57 +0000 | [diff] [blame] | 4343 | if (stopper_base_plan_sp != NULL) |
| 4344 | { |
| 4345 | thread->DiscardThreadPlansUpToPlan(stopper_base_plan_sp); |
| 4346 | } |
| 4347 | m_public_state.SetValueNoLock(old_state); |
| 4348 | |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
| 4351 | |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4352 | // Now do some processing on the results of the run: |
| 4353 | if (return_value == eExecutionInterrupted) |
| 4354 | { |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4355 | if (log) |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4356 | { |
| 4357 | StreamString s; |
| 4358 | if (event_sp) |
| 4359 | event_sp->Dump (&s); |
| 4360 | else |
| 4361 | { |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4362 | log->PutCString ("Process::RunThreadPlan(): Stop event that interrupted us is NULL."); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4363 | } |
| 4364 | |
| 4365 | StreamString ts; |
| 4366 | |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4367 | const char *event_explanation = NULL; |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4368 | |
| 4369 | do |
| 4370 | { |
| 4371 | const Process::ProcessEventData *event_data = Process::ProcessEventData::GetEventDataFromEvent (event_sp.get()); |
| 4372 | |
| 4373 | if (!event_data) |
| 4374 | { |
| 4375 | event_explanation = "<no event data>"; |
| 4376 | break; |
| 4377 | } |
| 4378 | |
| 4379 | Process *process = event_data->GetProcessSP().get(); |
| 4380 | |
| 4381 | if (!process) |
| 4382 | { |
| 4383 | event_explanation = "<no process>"; |
| 4384 | break; |
| 4385 | } |
| 4386 | |
| 4387 | ThreadList &thread_list = process->GetThreadList(); |
| 4388 | |
| 4389 | uint32_t num_threads = thread_list.GetSize(); |
| 4390 | uint32_t thread_index; |
| 4391 | |
| 4392 | ts.Printf("<%u threads> ", num_threads); |
| 4393 | |
| 4394 | for (thread_index = 0; |
| 4395 | thread_index < num_threads; |
| 4396 | ++thread_index) |
| 4397 | { |
| 4398 | Thread *thread = thread_list.GetThreadAtIndex(thread_index).get(); |
| 4399 | |
| 4400 | if (!thread) |
| 4401 | { |
| 4402 | ts.Printf("<?> "); |
| 4403 | continue; |
| 4404 | } |
| 4405 | |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 4406 | ts.Printf("<0x%4.4llx ", thread->GetID()); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4407 | RegisterContext *register_context = thread->GetRegisterContext().get(); |
| 4408 | |
| 4409 | if (register_context) |
| 4410 | ts.Printf("[ip 0x%llx] ", register_context->GetPC()); |
| 4411 | else |
| 4412 | ts.Printf("[ip unknown] "); |
| 4413 | |
| 4414 | lldb::StopInfoSP stop_info_sp = thread->GetStopInfo(); |
| 4415 | if (stop_info_sp) |
| 4416 | { |
| 4417 | const char *stop_desc = stop_info_sp->GetDescription(); |
| 4418 | if (stop_desc) |
| 4419 | ts.PutCString (stop_desc); |
| 4420 | } |
| 4421 | ts.Printf(">"); |
| 4422 | } |
| 4423 | |
| 4424 | event_explanation = ts.GetData(); |
| 4425 | } while (0); |
| 4426 | |
| 4427 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4428 | { |
| 4429 | if (event_explanation) |
| 4430 | log->Printf("Process::RunThreadPlan(): execution interrupted: %s %s", s.GetData(), event_explanation); |
| 4431 | else |
| 4432 | log->Printf("Process::RunThreadPlan(): execution interrupted: %s", s.GetData()); |
| 4433 | } |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4434 | |
| 4435 | if (discard_on_error && thread_plan_sp) |
| 4436 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4437 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4438 | thread_plan_sp->SetPrivate (orig_plan_private); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4439 | } |
| 4440 | } |
| 4441 | } |
| 4442 | else if (return_value == eExecutionSetupError) |
| 4443 | { |
| 4444 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4445 | log->PutCString("Process::RunThreadPlan(): execution set up error."); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4446 | |
| 4447 | if (discard_on_error && thread_plan_sp) |
| 4448 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4449 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4450 | thread_plan_sp->SetPrivate (orig_plan_private); |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4451 | } |
| 4452 | } |
| 4453 | else |
| 4454 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4455 | if (thread->IsThreadPlanDone (thread_plan_sp.get())) |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4456 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 4457 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4458 | log->PutCString("Process::RunThreadPlan(): thread plan is done"); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4459 | return_value = eExecutionCompleted; |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4460 | } |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4461 | else if (thread->WasThreadPlanDiscarded (thread_plan_sp.get())) |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4462 | { |
Greg Clayton | 68ca823 | 2011-01-25 02:58:48 +0000 | [diff] [blame] | 4463 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4464 | log->PutCString("Process::RunThreadPlan(): thread plan was discarded"); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4465 | return_value = eExecutionDiscarded; |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4466 | } |
| 4467 | else |
| 4468 | { |
| 4469 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4470 | log->PutCString("Process::RunThreadPlan(): thread plan stopped in mid course"); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4471 | if (discard_on_error && thread_plan_sp) |
| 4472 | { |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4473 | if (log) |
Jim Ingham | f6d3d79 | 2011-08-09 22:24:33 +0000 | [diff] [blame] | 4474 | log->PutCString("Process::RunThreadPlan(): discarding thread plan 'cause discard_on_error is set."); |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4475 | thread->DiscardThreadPlansUpToPlan (thread_plan_sp); |
Jim Ingham | 21f37ad | 2011-08-09 02:12:22 +0000 | [diff] [blame] | 4476 | thread_plan_sp->SetPrivate (orig_plan_private); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4477 | } |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4478 | } |
| 4479 | } |
Jim Ingham | f9f40c2 | 2011-02-08 05:20:59 +0000 | [diff] [blame] | 4480 | |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4481 | // Thread we ran the function in may have gone away because we ran the target |
Jim Ingham | 7bbebaf | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 4482 | // Check that it's still there, and if it is put it back in the context. Also restore the |
| 4483 | // frame in the context if it is still present. |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4484 | thread = GetThreadList().FindThreadByIndexID(thread_idx_id, true).get(); |
| 4485 | if (thread) |
Jim Ingham | 7bbebaf | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 4486 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4487 | exe_ctx.SetFrameSP (thread->GetFrameWithStackID (ctx_frame_id)); |
Jim Ingham | 7bbebaf | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 4488 | } |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4489 | |
| 4490 | // Also restore the current process'es selected frame & thread, since this function calling may |
| 4491 | // be done behind the user's back. |
| 4492 | |
| 4493 | if (selected_tid != LLDB_INVALID_THREAD_ID) |
| 4494 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4495 | if (GetThreadList().SetSelectedThreadByIndexID (selected_tid) && selected_stack_id.IsValid()) |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4496 | { |
| 4497 | // We were able to restore the selected thread, now restore the frame: |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4498 | StackFrameSP old_frame_sp = GetThreadList().GetSelectedThread()->GetFrameWithStackID(selected_stack_id); |
Jim Ingham | 7bbebaf | 2011-08-13 00:56:10 +0000 | [diff] [blame] | 4499 | if (old_frame_sp) |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 4500 | GetThreadList().GetSelectedThread()->SetSelectedFrame(old_frame_sp.get()); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4501 | } |
| 4502 | } |
| 4503 | |
| 4504 | return return_value; |
| 4505 | } |
| 4506 | |
| 4507 | const char * |
| 4508 | Process::ExecutionResultAsCString (ExecutionResults result) |
| 4509 | { |
| 4510 | const char *result_name; |
| 4511 | |
| 4512 | switch (result) |
| 4513 | { |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4514 | case eExecutionCompleted: |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4515 | result_name = "eExecutionCompleted"; |
| 4516 | break; |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4517 | case eExecutionDiscarded: |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4518 | result_name = "eExecutionDiscarded"; |
| 4519 | break; |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4520 | case eExecutionInterrupted: |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4521 | result_name = "eExecutionInterrupted"; |
| 4522 | break; |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4523 | case eExecutionSetupError: |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4524 | result_name = "eExecutionSetupError"; |
| 4525 | break; |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4526 | case eExecutionTimedOut: |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 4527 | result_name = "eExecutionTimedOut"; |
| 4528 | break; |
| 4529 | } |
| 4530 | return result_name; |
| 4531 | } |
| 4532 | |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 4533 | void |
| 4534 | Process::GetStatus (Stream &strm) |
| 4535 | { |
| 4536 | const StateType state = GetState(); |
Greg Clayton | 2020608 | 2011-11-17 01:23:07 +0000 | [diff] [blame] | 4537 | if (StateIsStoppedState(state, false)) |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 4538 | { |
| 4539 | if (state == eStateExited) |
| 4540 | { |
| 4541 | int exit_status = GetExitStatus(); |
| 4542 | const char *exit_description = GetExitDescription(); |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 4543 | strm.Printf ("Process %llu exited with status = %i (0x%8.8x) %s\n", |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 4544 | GetID(), |
| 4545 | exit_status, |
| 4546 | exit_status, |
| 4547 | exit_description ? exit_description : ""); |
| 4548 | } |
| 4549 | else |
| 4550 | { |
| 4551 | if (state == eStateConnected) |
| 4552 | strm.Printf ("Connected to remote target.\n"); |
| 4553 | else |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 4554 | strm.Printf ("Process %llu %s\n", GetID(), StateAsCString (state)); |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 4555 | } |
| 4556 | } |
| 4557 | else |
| 4558 | { |
Greg Clayton | 444e35b | 2011-10-19 18:09:39 +0000 | [diff] [blame] | 4559 | strm.Printf ("Process %llu is running.\n", GetID()); |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 4560 | } |
| 4561 | } |
| 4562 | |
| 4563 | size_t |
| 4564 | Process::GetThreadStatus (Stream &strm, |
| 4565 | bool only_threads_with_stop_reason, |
| 4566 | uint32_t start_frame, |
| 4567 | uint32_t num_frames, |
| 4568 | uint32_t num_frames_with_source) |
| 4569 | { |
| 4570 | size_t num_thread_infos_dumped = 0; |
| 4571 | |
| 4572 | const size_t num_threads = GetThreadList().GetSize(); |
| 4573 | for (uint32_t i = 0; i < num_threads; i++) |
| 4574 | { |
| 4575 | Thread *thread = GetThreadList().GetThreadAtIndex(i).get(); |
| 4576 | if (thread) |
| 4577 | { |
| 4578 | if (only_threads_with_stop_reason) |
| 4579 | { |
| 4580 | if (thread->GetStopInfo().get() == NULL) |
| 4581 | continue; |
| 4582 | } |
| 4583 | thread->GetStatus (strm, |
| 4584 | start_frame, |
| 4585 | num_frames, |
| 4586 | num_frames_with_source); |
| 4587 | ++num_thread_infos_dumped; |
| 4588 | } |
| 4589 | } |
| 4590 | return num_thread_infos_dumped; |
| 4591 | } |
| 4592 | |
Greg Clayton | 7611330 | 2012-02-22 04:37:26 +0000 | [diff] [blame] | 4593 | void |
| 4594 | Process::AddInvalidMemoryRegion (const LoadRange ®ion) |
| 4595 | { |
| 4596 | m_memory_cache.AddInvalidRange(region.GetRangeBase(), region.GetByteSize()); |
| 4597 | } |
| 4598 | |
| 4599 | bool |
| 4600 | Process::RemoveInvalidMemoryRange (const LoadRange ®ion) |
| 4601 | { |
| 4602 | return m_memory_cache.RemoveInvalidRange(region.GetRangeBase(), region.GetByteSize()); |
| 4603 | } |
| 4604 | |
Jim Ingham | 1831e78 | 2012-04-07 00:00:41 +0000 | [diff] [blame] | 4605 | void |
| 4606 | Process::AddPreResumeAction (PreResumeActionCallback callback, void *baton) |
| 4607 | { |
| 4608 | m_pre_resume_actions.push_back(PreResumeCallbackAndBaton (callback, baton)); |
| 4609 | } |
| 4610 | |
| 4611 | bool |
| 4612 | Process::RunPreResumeActions () |
| 4613 | { |
| 4614 | bool result = true; |
| 4615 | while (!m_pre_resume_actions.empty()) |
| 4616 | { |
| 4617 | struct PreResumeCallbackAndBaton action = m_pre_resume_actions.back(); |
| 4618 | m_pre_resume_actions.pop_back(); |
| 4619 | bool this_result = action.callback (action.baton); |
| 4620 | if (result == true) result = this_result; |
| 4621 | } |
| 4622 | return result; |
| 4623 | } |
| 4624 | |
| 4625 | void |
| 4626 | Process::ClearPreResumeActions () |
| 4627 | { |
| 4628 | m_pre_resume_actions.clear(); |
| 4629 | } |
Greg Clayton | 7611330 | 2012-02-22 04:37:26 +0000 | [diff] [blame] | 4630 | |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4631 | //-------------------------------------------------------------- |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 4632 | // class Process::SettingsController |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4633 | //-------------------------------------------------------------- |
| 4634 | |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 4635 | Process::SettingsController::SettingsController () : |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 4636 | UserSettingsController ("process", Target::GetSettingsController()) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4637 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4638 | } |
| 4639 | |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 4640 | Process::SettingsController::~SettingsController () |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4641 | { |
| 4642 | } |
| 4643 | |
| 4644 | lldb::InstanceSettingsSP |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 4645 | Process::SettingsController::CreateInstanceSettings (const char *instance_name) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4646 | { |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 4647 | lldb::InstanceSettingsSP new_settings_sp (new ProcessInstanceSettings (GetSettingsController(), |
| 4648 | false, |
| 4649 | instance_name)); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4650 | return new_settings_sp; |
| 4651 | } |
| 4652 | |
| 4653 | //-------------------------------------------------------------- |
| 4654 | // class ProcessInstanceSettings |
| 4655 | //-------------------------------------------------------------- |
| 4656 | |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 4657 | ProcessInstanceSettings::ProcessInstanceSettings |
| 4658 | ( |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 4659 | const UserSettingsControllerSP &owner_sp, |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 4660 | bool live_instance, |
| 4661 | const char *name |
| 4662 | ) : |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 4663 | InstanceSettings (owner_sp, name ? name : InstanceSettings::InvalidName().AsCString(), live_instance) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4664 | { |
Caroline Tice | 396704b | 2010-09-09 18:26:37 +0000 | [diff] [blame] | 4665 | // CopyInstanceSettings is a pure virtual function in InstanceSettings; it therefore cannot be called |
| 4666 | // until the vtables for ProcessInstanceSettings are properly set up, i.e. AFTER all the initializers. |
| 4667 | // 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] | 4668 | // This is true for CreateInstanceName() too. |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 4669 | |
Caroline Tice | 75b11a3 | 2010-09-16 19:05:55 +0000 | [diff] [blame] | 4670 | if (GetInstanceName () == InstanceSettings::InvalidName()) |
| 4671 | { |
| 4672 | ChangeInstanceName (std::string (CreateInstanceName().AsCString())); |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 4673 | owner_sp->RegisterInstanceSettings (this); |
Caroline Tice | 75b11a3 | 2010-09-16 19:05:55 +0000 | [diff] [blame] | 4674 | } |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 4675 | |
Caroline Tice | 396704b | 2010-09-09 18:26:37 +0000 | [diff] [blame] | 4676 | if (live_instance) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4677 | { |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 4678 | const lldb::InstanceSettingsSP &pending_settings = owner_sp->FindPendingSettings (m_instance_name); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4679 | CopyInstanceSettings (pending_settings,false); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4680 | } |
| 4681 | } |
| 4682 | |
| 4683 | ProcessInstanceSettings::ProcessInstanceSettings (const ProcessInstanceSettings &rhs) : |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 4684 | InstanceSettings (Process::GetSettingsController(), CreateInstanceName().AsCString()) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4685 | { |
| 4686 | if (m_instance_name != InstanceSettings::GetDefaultName()) |
| 4687 | { |
Greg Clayton | 334d33a | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 4688 | UserSettingsControllerSP owner_sp (m_owner_wp.lock()); |
| 4689 | if (owner_sp) |
| 4690 | { |
| 4691 | CopyInstanceSettings (owner_sp->FindPendingSettings (m_instance_name), false); |
| 4692 | owner_sp->RemovePendingSettings (m_instance_name); |
| 4693 | } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4694 | } |
| 4695 | } |
| 4696 | |
| 4697 | ProcessInstanceSettings::~ProcessInstanceSettings () |
| 4698 | { |
| 4699 | } |
| 4700 | |
| 4701 | ProcessInstanceSettings& |
| 4702 | ProcessInstanceSettings::operator= (const ProcessInstanceSettings &rhs) |
| 4703 | { |
| 4704 | if (this != &rhs) |
| 4705 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4706 | } |
| 4707 | |
| 4708 | return *this; |
| 4709 | } |
| 4710 | |
| 4711 | |
| 4712 | void |
| 4713 | ProcessInstanceSettings::UpdateInstanceSettingsVariable (const ConstString &var_name, |
| 4714 | const char *index_value, |
| 4715 | const char *value, |
| 4716 | const ConstString &instance_name, |
| 4717 | const SettingEntry &entry, |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 4718 | VarSetOperationType op, |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4719 | Error &err, |
| 4720 | bool pending) |
| 4721 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4722 | } |
| 4723 | |
| 4724 | void |
| 4725 | ProcessInstanceSettings::CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, |
| 4726 | bool pending) |
| 4727 | { |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 4728 | // if (new_settings.get() == NULL) |
| 4729 | // return; |
| 4730 | // |
| 4731 | // ProcessInstanceSettings *new_process_settings = (ProcessInstanceSettings *) new_settings.get(); |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4732 | } |
| 4733 | |
Caroline Tice | bcb5b45 | 2010-09-20 21:37:42 +0000 | [diff] [blame] | 4734 | bool |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4735 | ProcessInstanceSettings::GetInstanceSettingsValue (const SettingEntry &entry, |
| 4736 | const ConstString &var_name, |
Caroline Tice | 5bc8c97 | 2010-09-20 20:44:43 +0000 | [diff] [blame] | 4737 | StringList &value, |
Caroline Tice | bcb5b45 | 2010-09-20 21:37:42 +0000 | [diff] [blame] | 4738 | Error *err) |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4739 | { |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 4740 | if (err) |
| 4741 | err->SetErrorStringWithFormat ("unrecognized variable name '%s'", var_name.AsCString()); |
| 4742 | return false; |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4745 | const ConstString |
| 4746 | ProcessInstanceSettings::CreateInstanceName () |
| 4747 | { |
| 4748 | static int instance_count = 1; |
| 4749 | StreamString sstr; |
| 4750 | |
| 4751 | sstr.Printf ("process_%d", instance_count); |
| 4752 | ++instance_count; |
| 4753 | |
| 4754 | const ConstString ret_val (sstr.GetData()); |
| 4755 | return ret_val; |
| 4756 | } |
| 4757 | |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4758 | //-------------------------------------------------- |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 4759 | // SettingsController Variable Tables |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4760 | //-------------------------------------------------- |
| 4761 | |
| 4762 | SettingEntry |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 4763 | Process::SettingsController::global_settings_table[] = |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4764 | { |
| 4765 | //{ "var-name", var-type , "default", enum-table, init'd, hidden, "help-text"}, |
| 4766 | { NULL, eSetVarTypeNone, NULL, NULL, 0, 0, NULL } |
| 4767 | }; |
| 4768 | |
| 4769 | |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4770 | SettingEntry |
Greg Clayton | d0a5a23 | 2010-09-19 02:33:57 +0000 | [diff] [blame] | 4771 | Process::SettingsController::instance_settings_table[] = |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4772 | { |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 4773 | //{ "var-name", var-type, "default", enum-table, init'd, hidden, "help-text"}, |
Greg Clayton | 638351a | 2010-12-04 00:10:17 +0000 | [diff] [blame] | 4774 | { NULL, eSetVarTypeNone, NULL, NULL, false, false, NULL } |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 4775 | }; |
| 4776 | |
| 4777 | |
Jim Ingham | 7508e73 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 4778 | |
Greg Clayton | abb3302 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 4779 | |