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