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