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