Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1 | //===-- ProcessLinux.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 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Other libraries and framework includes |
| 13 | #include "lldb/Core/PluginManager.h" |
| 14 | #include "lldb/Host/Host.h" |
| 15 | #include "lldb/Symbol/ObjectFile.h" |
Stephen Wilson | 92241ef | 2011-01-16 19:45:39 +0000 | [diff] [blame] | 16 | #include "lldb/Target/DynamicLoader.h" |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 17 | #include "lldb/Target/Target.h" |
| 18 | |
| 19 | #include "ProcessLinux.h" |
| 20 | #include "ProcessMonitor.h" |
| 21 | #include "LinuxThread.h" |
| 22 | |
| 23 | using namespace lldb; |
| 24 | using namespace lldb_private; |
| 25 | |
| 26 | //------------------------------------------------------------------------------ |
| 27 | // Static functions. |
| 28 | |
| 29 | Process* |
| 30 | ProcessLinux::CreateInstance(Target& target, Listener &listener) |
| 31 | { |
| 32 | return new ProcessLinux(target, listener); |
| 33 | } |
| 34 | |
| 35 | void |
| 36 | ProcessLinux::Initialize() |
| 37 | { |
| 38 | static bool g_initialized = false; |
| 39 | |
| 40 | if (!g_initialized) |
| 41 | { |
| 42 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 43 | GetPluginDescriptionStatic(), |
| 44 | CreateInstance); |
| 45 | g_initialized = true; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | ProcessLinux::Terminate() |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | const char * |
| 55 | ProcessLinux::GetPluginNameStatic() |
| 56 | { |
| 57 | return "plugin.process.linux"; |
| 58 | } |
| 59 | |
| 60 | const char * |
| 61 | ProcessLinux::GetPluginDescriptionStatic() |
| 62 | { |
| 63 | return "Process plugin for Linux"; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | //------------------------------------------------------------------------------ |
| 68 | // Constructors and destructors. |
| 69 | |
| 70 | ProcessLinux::ProcessLinux(Target& target, Listener &listener) |
| 71 | : Process(target, listener), |
| 72 | m_monitor(NULL), |
| 73 | m_module(NULL) |
| 74 | { |
| 75 | // FIXME: Putting this code in the ctor and saving the byte order in a |
| 76 | // member variable is a hack to avoid const qual issues in GetByteOrder. |
| 77 | ObjectFile *obj_file = GetTarget().GetExecutableModule()->GetObjectFile(); |
| 78 | m_byte_order = obj_file->GetByteOrder(); |
| 79 | } |
| 80 | |
| 81 | ProcessLinux::~ProcessLinux() |
| 82 | { |
| 83 | delete m_monitor; |
| 84 | } |
| 85 | |
| 86 | //------------------------------------------------------------------------------ |
| 87 | // Process protocol. |
| 88 | |
| 89 | bool |
| 90 | ProcessLinux::CanDebug(Target &target) |
| 91 | { |
| 92 | // For now we are just making sure the file exists for a given module |
| 93 | ModuleSP exe_module_sp(target.GetExecutableModule()); |
| 94 | if (exe_module_sp.get()) |
| 95 | return exe_module_sp->GetFileSpec().Exists(); |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | Error |
| 100 | ProcessLinux::DoAttachToProcessWithID(lldb::pid_t pid) |
| 101 | { |
| 102 | return Error(1, eErrorTypeGeneric); |
| 103 | } |
| 104 | |
| 105 | Error |
Stephen Wilson | 92241ef | 2011-01-16 19:45:39 +0000 | [diff] [blame] | 106 | ProcessLinux::WillLaunch(Module* module) |
| 107 | { |
| 108 | Error error; |
| 109 | |
| 110 | m_dyld_ap.reset(DynamicLoader::FindPlugin(this, "dynamic-loader.linux-dyld")); |
| 111 | if (m_dyld_ap.get() == NULL) |
| 112 | error.SetErrorString("unable to find the dynamic loader named " |
| 113 | "'dynamic-loader.linux-dyld'"); |
| 114 | |
| 115 | return error; |
| 116 | } |
| 117 | |
| 118 | Error |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 119 | ProcessLinux::DoLaunch(Module *module, |
| 120 | char const *argv[], |
| 121 | char const *envp[], |
Stephen Wilson | 3a80431 | 2011-01-04 21:41:31 +0000 | [diff] [blame] | 122 | uint32_t launch_flags, |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 123 | const char *stdin_path, |
| 124 | const char *stdout_path, |
| 125 | const char *stderr_path) |
| 126 | { |
| 127 | Error error; |
| 128 | assert(m_monitor == NULL); |
| 129 | |
| 130 | SetPrivateState(eStateLaunching); |
| 131 | m_monitor = new ProcessMonitor(this, module, |
| 132 | argv, envp, |
| 133 | stdin_path, stdout_path, stderr_path, |
| 134 | error); |
| 135 | |
| 136 | m_module = module; |
| 137 | |
| 138 | if (!error.Success()) |
| 139 | return error; |
| 140 | |
Stephen Wilson | 1f004c6 | 2011-01-15 00:13:27 +0000 | [diff] [blame] | 141 | SetID(m_monitor->GetPID()); |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 142 | return error; |
| 143 | } |
| 144 | |
Stephen Wilson | 92241ef | 2011-01-16 19:45:39 +0000 | [diff] [blame] | 145 | void |
| 146 | ProcessLinux::DidLaunch() |
| 147 | { |
| 148 | if (m_dyld_ap.get() != NULL) |
| 149 | m_dyld_ap->DidLaunch(); |
| 150 | } |
| 151 | |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 152 | Error |
| 153 | ProcessLinux::DoResume() |
| 154 | { |
| 155 | assert(GetPrivateState() == eStateStopped && "Bad state for DoResume!"); |
| 156 | |
| 157 | // Set our state to running. This ensures inferior threads do not post a |
| 158 | // state change first. |
| 159 | SetPrivateState(eStateRunning); |
| 160 | |
| 161 | bool did_resume = false; |
| 162 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 163 | for (uint32_t i = 0; i < thread_count; ++i) |
| 164 | { |
| 165 | LinuxThread *thread = static_cast<LinuxThread*>( |
| 166 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 167 | did_resume = thread->Resume() || did_resume; |
| 168 | } |
| 169 | assert(did_resume && "Process resume failed!"); |
| 170 | |
| 171 | return Error(); |
| 172 | } |
| 173 | |
Stephen Wilson | 0131642 | 2011-01-15 00:10:37 +0000 | [diff] [blame] | 174 | addr_t |
| 175 | ProcessLinux::GetImageInfoAddress() |
| 176 | { |
| 177 | Target *target = &GetTarget(); |
| 178 | ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); |
| 179 | Address addr = obj_file->GetImageInfoAddress(); |
| 180 | |
| 181 | if (addr.IsValid()) |
| 182 | return addr.GetLoadAddress(target); |
| 183 | else |
| 184 | return LLDB_INVALID_ADDRESS; |
| 185 | } |
| 186 | |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 187 | Error |
Stephen Wilson | 3a80431 | 2011-01-04 21:41:31 +0000 | [diff] [blame] | 188 | ProcessLinux::DoHalt(bool &caused_stop) |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 189 | { |
| 190 | return Error(1, eErrorTypeGeneric); |
| 191 | } |
| 192 | |
| 193 | Error |
| 194 | ProcessLinux::DoDetach() |
| 195 | { |
| 196 | return Error(1, eErrorTypeGeneric); |
| 197 | } |
| 198 | |
| 199 | Error |
| 200 | ProcessLinux::DoSignal(int signal) |
| 201 | { |
| 202 | return Error(1, eErrorTypeGeneric); |
| 203 | } |
| 204 | |
| 205 | Error |
| 206 | ProcessLinux::DoDestroy() |
| 207 | { |
| 208 | Error error; |
| 209 | |
| 210 | if (!HasExited()) |
| 211 | { |
Greg Clayton | 5d187e5 | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 212 | // Shut down the private state thread as we will synchronize with events |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 213 | // ourselves. Discard all current thread plans. |
| 214 | PausePrivateStateThread(); |
| 215 | GetThreadList().DiscardThreadPlans(); |
| 216 | |
| 217 | // Bringing the inferior into limbo will be caught by our monitor |
| 218 | // thread, in turn updating the process state. |
| 219 | if (!m_monitor->BringProcessIntoLimbo()) |
| 220 | { |
| 221 | error.SetErrorToGenericError(); |
| 222 | error.SetErrorString("Process termination failed."); |
| 223 | return error; |
| 224 | } |
| 225 | |
Stephen Wilson | 83047fc | 2011-01-19 01:30:44 +0000 | [diff] [blame] | 226 | // Wait for the event to arrive. This is guaranteed to be an exit event. |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 227 | StateType state; |
| 228 | EventSP event; |
| 229 | do { |
Stephen Wilson | 83047fc | 2011-01-19 01:30:44 +0000 | [diff] [blame] | 230 | TimeValue timeout_time; |
| 231 | timeout_time = TimeValue::Now(); |
| 232 | timeout_time.OffsetWithSeconds(2); |
| 233 | state = WaitForStateChangedEventsPrivate(&timeout_time, event); |
| 234 | } while (state != eStateExited && state != eStateInvalid); |
| 235 | |
| 236 | // Check if we timed out waiting for the exit event to arrive. |
| 237 | if (state == eStateInvalid) |
| 238 | error.SetErrorString("ProcessLinux::DoDestroy timed out."); |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 239 | |
| 240 | // Restart standard event handling and send the process the final kill, |
| 241 | // driving it out of limbo. |
| 242 | ResumePrivateStateThread(); |
| 243 | } |
| 244 | |
Stephen Wilson | 83047fc | 2011-01-19 01:30:44 +0000 | [diff] [blame] | 245 | if (kill(m_monitor->GetPID(), SIGKILL) && error.Success()) |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 246 | error.SetErrorToErrno(); |
Stephen Wilson | 83047fc | 2011-01-19 01:30:44 +0000 | [diff] [blame] | 247 | |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 248 | return error; |
| 249 | } |
| 250 | |
| 251 | void |
| 252 | ProcessLinux::SendMessage(const ProcessMessage &message) |
| 253 | { |
| 254 | Mutex::Locker lock(m_message_mutex); |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 255 | |
| 256 | switch (message.GetKind()) |
| 257 | { |
| 258 | default: |
| 259 | SetPrivateState(eStateStopped); |
| 260 | break; |
| 261 | |
Stephen Wilson | 07fc7a9 | 2011-01-19 01:29:39 +0000 | [diff] [blame] | 262 | case ProcessMessage::eInvalidMessage: |
| 263 | return; |
| 264 | |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 265 | case ProcessMessage::eExitMessage: |
| 266 | SetExitStatus(message.GetExitStatus(), NULL); |
| 267 | break; |
| 268 | |
| 269 | case ProcessMessage::eSignalMessage: |
| 270 | SetExitStatus(-1, NULL); |
| 271 | break; |
| 272 | } |
Stephen Wilson | 07fc7a9 | 2011-01-19 01:29:39 +0000 | [diff] [blame] | 273 | |
| 274 | m_message_queue.push(message); |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void |
| 278 | ProcessLinux::RefreshStateAfterStop() |
| 279 | { |
| 280 | Mutex::Locker lock(m_message_mutex); |
| 281 | if (m_message_queue.empty()) |
| 282 | return; |
| 283 | |
| 284 | ProcessMessage &message = m_message_queue.front(); |
| 285 | |
| 286 | // Resolve the thread this message corresponds to. |
| 287 | lldb::tid_t tid = message.GetTID(); |
| 288 | LinuxThread *thread = static_cast<LinuxThread*>( |
| 289 | GetThreadList().FindThreadByID(tid, false).get()); |
| 290 | |
| 291 | switch (message.GetKind()) |
| 292 | { |
| 293 | default: |
| 294 | assert(false && "Unexpected message kind!"); |
| 295 | break; |
| 296 | |
| 297 | case ProcessMessage::eExitMessage: |
| 298 | case ProcessMessage::eSignalMessage: |
| 299 | thread->ExitNotify(); |
| 300 | break; |
| 301 | |
| 302 | case ProcessMessage::eTraceMessage: |
| 303 | thread->TraceNotify(); |
| 304 | break; |
| 305 | |
| 306 | case ProcessMessage::eBreakpointMessage: |
| 307 | thread->BreakNotify(); |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | m_message_queue.pop(); |
| 312 | } |
| 313 | |
| 314 | bool |
| 315 | ProcessLinux::IsAlive() |
| 316 | { |
| 317 | StateType state = GetPrivateState(); |
| 318 | return state != eStateExited && state != eStateInvalid; |
| 319 | } |
| 320 | |
| 321 | size_t |
| 322 | ProcessLinux::DoReadMemory(addr_t vm_addr, |
| 323 | void *buf, size_t size, Error &error) |
| 324 | { |
| 325 | return m_monitor->ReadMemory(vm_addr, buf, size, error); |
| 326 | } |
| 327 | |
| 328 | size_t |
| 329 | ProcessLinux::DoWriteMemory(addr_t vm_addr, const void *buf, size_t size, |
| 330 | Error &error) |
| 331 | { |
| 332 | return m_monitor->WriteMemory(vm_addr, buf, size, error); |
| 333 | } |
| 334 | |
| 335 | addr_t |
| 336 | ProcessLinux::DoAllocateMemory(size_t size, uint32_t permissions, |
| 337 | Error &error) |
| 338 | { |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | addr_t |
| 343 | ProcessLinux::AllocateMemory(size_t size, uint32_t permissions, Error &error) |
| 344 | { |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | Error |
| 349 | ProcessLinux::DoDeallocateMemory(lldb::addr_t ptr) |
| 350 | { |
| 351 | return Error(1, eErrorTypeGeneric); |
| 352 | } |
| 353 | |
| 354 | size_t |
| 355 | ProcessLinux::GetSoftwareBreakpointTrapOpcode(BreakpointSite* bp_site) |
| 356 | { |
| 357 | static const uint8_t g_i386_opcode[] = { 0xCC }; |
| 358 | |
| 359 | ArchSpec arch = GetTarget().GetArchitecture(); |
| 360 | const uint8_t *opcode = NULL; |
| 361 | size_t opcode_size = 0; |
| 362 | |
| 363 | switch (arch.GetGenericCPUType()) |
| 364 | { |
| 365 | default: |
| 366 | assert(false && "CPU type not supported!"); |
| 367 | break; |
| 368 | |
| 369 | case ArchSpec::eCPU_i386: |
| 370 | case ArchSpec::eCPU_x86_64: |
| 371 | opcode = g_i386_opcode; |
| 372 | opcode_size = sizeof(g_i386_opcode); |
| 373 | break; |
| 374 | } |
| 375 | |
| 376 | bp_site->SetTrapOpcode(opcode, opcode_size); |
| 377 | return opcode_size; |
| 378 | } |
| 379 | |
| 380 | Error |
| 381 | ProcessLinux::EnableBreakpoint(BreakpointSite *bp_site) |
| 382 | { |
| 383 | return EnableSoftwareBreakpoint(bp_site); |
| 384 | } |
| 385 | |
| 386 | Error |
| 387 | ProcessLinux::DisableBreakpoint(BreakpointSite *bp_site) |
| 388 | { |
| 389 | return DisableSoftwareBreakpoint(bp_site); |
| 390 | } |
| 391 | |
| 392 | uint32_t |
| 393 | ProcessLinux::UpdateThreadListIfNeeded() |
| 394 | { |
| 395 | // Do not allow recursive updates. |
| 396 | return m_thread_list.GetSize(false); |
| 397 | } |
| 398 | |
| 399 | ByteOrder |
| 400 | ProcessLinux::GetByteOrder() const |
| 401 | { |
| 402 | // FIXME: We should be able to extract this value directly. See comment in |
| 403 | // ProcessLinux(). |
| 404 | return m_byte_order; |
| 405 | } |
| 406 | |
Stephen Wilson | 92241ef | 2011-01-16 19:45:39 +0000 | [diff] [blame] | 407 | DynamicLoader * |
| 408 | ProcessLinux::GetDynamicLoader() |
| 409 | { |
| 410 | return m_dyld_ap.get(); |
| 411 | } |
| 412 | |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 413 | //------------------------------------------------------------------------------ |
| 414 | // ProcessInterface protocol. |
| 415 | |
| 416 | const char * |
| 417 | ProcessLinux::GetPluginName() |
| 418 | { |
| 419 | return "process.linux"; |
| 420 | } |
| 421 | |
| 422 | const char * |
| 423 | ProcessLinux::GetShortPluginName() |
| 424 | { |
| 425 | return "process.linux"; |
| 426 | } |
| 427 | |
| 428 | uint32_t |
| 429 | ProcessLinux::GetPluginVersion() |
| 430 | { |
| 431 | return 1; |
| 432 | } |
| 433 | |
| 434 | void |
| 435 | ProcessLinux::GetPluginCommandHelp(const char *command, Stream *strm) |
| 436 | { |
| 437 | } |
| 438 | |
| 439 | Error |
| 440 | ProcessLinux::ExecutePluginCommand(Args &command, Stream *strm) |
| 441 | { |
| 442 | return Error(1, eErrorTypeGeneric); |
| 443 | } |
| 444 | |
| 445 | Log * |
| 446 | ProcessLinux::EnablePluginLogging(Stream *strm, Args &command) |
| 447 | { |
| 448 | return NULL; |
| 449 | } |
| 450 | |
| 451 | //------------------------------------------------------------------------------ |
| 452 | // Utility functions. |
| 453 | |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 454 | bool |
| 455 | ProcessLinux::HasExited() |
| 456 | { |
| 457 | switch (GetPrivateState()) |
| 458 | { |
| 459 | default: |
| 460 | break; |
| 461 | |
| 462 | case eStateUnloaded: |
| 463 | case eStateCrashed: |
| 464 | case eStateDetached: |
| 465 | case eStateExited: |
| 466 | return true; |
| 467 | } |
| 468 | |
| 469 | return false; |
| 470 | } |