Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 1 | //===-- ProcessPOSIX.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 | |
Daniel Malea | d891f9b | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 12 | // C Includes |
| 13 | #include <errno.h> |
| 14 | |
| 15 | // C++ Includes |
| 16 | // Other libraries and framework includes |
Greg Clayton | 8ac035d | 2012-09-04 14:55:50 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Module.h" |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 18 | #include "lldb/Core/PluginManager.h" |
| 19 | #include "lldb/Core/State.h" |
Daniel Malea | 1e44fdd | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 20 | #include "lldb/Host/FileSpec.h" |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 21 | #include "lldb/Host/Host.h" |
| 22 | #include "lldb/Symbol/ObjectFile.h" |
| 23 | #include "lldb/Target/DynamicLoader.h" |
| 24 | #include "lldb/Target/Target.h" |
| 25 | |
| 26 | #include "ProcessPOSIX.h" |
| 27 | #include "ProcessPOSIXLog.h" |
| 28 | #include "Plugins/Process/Utility/InferiorCallPOSIX.h" |
| 29 | #include "ProcessMonitor.h" |
| 30 | #include "POSIXThread.h" |
| 31 | |
| 32 | using namespace lldb; |
| 33 | using namespace lldb_private; |
| 34 | |
| 35 | //------------------------------------------------------------------------------ |
| 36 | // Static functions. |
| 37 | #if 0 |
| 38 | Process* |
| 39 | ProcessPOSIX::CreateInstance(Target& target, Listener &listener) |
| 40 | { |
| 41 | return new ProcessPOSIX(target, listener); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | void |
| 46 | ProcessPOSIX::Initialize() |
| 47 | { |
| 48 | static bool g_initialized = false; |
| 49 | |
| 50 | if (!g_initialized) |
| 51 | { |
| 52 | g_initialized = true; |
| 53 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 54 | GetPluginDescriptionStatic(), |
| 55 | CreateInstance); |
| 56 | |
| 57 | Log::Callbacks log_callbacks = { |
| 58 | ProcessPOSIXLog::DisableLog, |
| 59 | ProcessPOSIXLog::EnableLog, |
| 60 | ProcessPOSIXLog::ListLogCategories |
| 61 | }; |
| 62 | |
| 63 | Log::RegisterLogChannel (ProcessPOSIX::GetPluginNameStatic(), log_callbacks); |
| 64 | } |
| 65 | } |
| 66 | #endif |
| 67 | |
| 68 | //------------------------------------------------------------------------------ |
| 69 | // Constructors and destructors. |
| 70 | |
| 71 | ProcessPOSIX::ProcessPOSIX(Target& target, Listener &listener) |
| 72 | : Process(target, listener), |
Johnny Chen | 78dae82 | 2012-04-14 00:54:42 +0000 | [diff] [blame] | 73 | m_byte_order(lldb::endian::InlHostByteOrder()), |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 74 | m_monitor(NULL), |
| 75 | m_module(NULL), |
| 76 | m_in_limbo(false), |
| 77 | m_exit_now(false) |
| 78 | { |
| 79 | // FIXME: Putting this code in the ctor and saving the byte order in a |
| 80 | // member variable is a hack to avoid const qual issues in GetByteOrder. |
Johnny Chen | 78dae82 | 2012-04-14 00:54:42 +0000 | [diff] [blame] | 81 | lldb::ModuleSP module = GetTarget().GetExecutableModule(); |
| 82 | if (module != NULL && module->GetObjectFile() != NULL) |
| 83 | m_byte_order = module->GetObjectFile()->GetByteOrder(); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | ProcessPOSIX::~ProcessPOSIX() |
| 87 | { |
| 88 | delete m_monitor; |
| 89 | } |
| 90 | |
| 91 | //------------------------------------------------------------------------------ |
| 92 | // Process protocol. |
| 93 | |
| 94 | bool |
| 95 | ProcessPOSIX::CanDebug(Target &target, bool plugin_specified_by_name) |
| 96 | { |
| 97 | // For now we are just making sure the file exists for a given module |
| 98 | ModuleSP exe_module_sp(target.GetExecutableModule()); |
| 99 | if (exe_module_sp.get()) |
| 100 | return exe_module_sp->GetFileSpec().Exists(); |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | Error |
| 105 | ProcessPOSIX::DoAttachToProcessWithID(lldb::pid_t pid) |
| 106 | { |
| 107 | Error error; |
| 108 | assert(m_monitor == NULL); |
| 109 | |
| 110 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
| 111 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 112 | log->Printf ("ProcessPOSIX::%s(pid = %" PRIi64 ")", __FUNCTION__, GetID()); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 113 | |
| 114 | m_monitor = new ProcessMonitor(this, pid, error); |
| 115 | |
| 116 | if (!error.Success()) |
| 117 | return error; |
| 118 | |
| 119 | SetID(pid); |
| 120 | return error; |
| 121 | } |
| 122 | |
| 123 | Error |
Greg Clayton | c643077 | 2012-09-07 17:51:47 +0000 | [diff] [blame] | 124 | ProcessPOSIX::DoAttachToProcessWithID (lldb::pid_t pid, const ProcessAttachInfo &attach_info) |
| 125 | { |
| 126 | return DoAttachToProcessWithID(pid); |
| 127 | } |
| 128 | |
| 129 | Error |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 130 | ProcessPOSIX::WillLaunch(Module* module) |
| 131 | { |
| 132 | Error error; |
| 133 | return error; |
| 134 | } |
| 135 | |
| 136 | const char * |
| 137 | ProcessPOSIX::GetFilePath( |
| 138 | const lldb_private::ProcessLaunchInfo::FileAction *file_action, |
| 139 | const char *default_path) |
| 140 | { |
| 141 | const char *pts_name = "/dev/pts/"; |
| 142 | const char *path = NULL; |
| 143 | |
| 144 | if (file_action) |
| 145 | { |
| 146 | if (file_action->GetAction () == ProcessLaunchInfo::FileAction::eFileActionOpen) |
| 147 | path = file_action->GetPath(); |
| 148 | // By default the stdio paths passed in will be pseudo-terminal |
| 149 | // (/dev/pts). If so, convert to using a different default path |
| 150 | // instead to redirect I/O to the debugger console. This should |
| 151 | // also handle user overrides to /dev/null or a different file. |
| 152 | if (::strncmp(path, pts_name, ::strlen(pts_name)) == 0) |
| 153 | path = default_path; |
| 154 | } |
| 155 | |
| 156 | return path; |
| 157 | } |
| 158 | |
| 159 | Error |
| 160 | ProcessPOSIX::DoLaunch (Module *module, |
| 161 | const ProcessLaunchInfo &launch_info) |
| 162 | { |
| 163 | Error error; |
| 164 | assert(m_monitor == NULL); |
| 165 | |
Daniel Malea | 1e44fdd | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 166 | const char* working_dir = launch_info.GetWorkingDirectory(); |
| 167 | if (working_dir) { |
| 168 | FileSpec WorkingDir(working_dir, true); |
| 169 | if (!WorkingDir || WorkingDir.GetFileType() != FileSpec::eFileTypeDirectory) |
| 170 | { |
| 171 | error.SetErrorStringWithFormat("No such file or directory: %s", working_dir); |
| 172 | return error; |
| 173 | } |
| 174 | } |
| 175 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 176 | SetPrivateState(eStateLaunching); |
| 177 | |
| 178 | const lldb_private::ProcessLaunchInfo::FileAction *file_action; |
| 179 | |
| 180 | // Default of NULL will mean to use existing open file descriptors |
| 181 | const char *stdin_path = NULL; |
| 182 | const char *stdout_path = NULL; |
| 183 | const char *stderr_path = NULL; |
Daniel Malea | 1e44fdd | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 184 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 185 | file_action = launch_info.GetFileActionForFD (STDIN_FILENO); |
| 186 | stdin_path = GetFilePath(file_action, stdin_path); |
| 187 | |
| 188 | file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); |
| 189 | stdout_path = GetFilePath(file_action, stdout_path); |
| 190 | |
| 191 | file_action = launch_info.GetFileActionForFD (STDERR_FILENO); |
| 192 | stderr_path = GetFilePath(file_action, stderr_path); |
| 193 | |
| 194 | m_monitor = new ProcessMonitor (this, |
| 195 | module, |
| 196 | launch_info.GetArguments().GetConstArgumentVector(), |
| 197 | launch_info.GetEnvironmentEntries().GetConstArgumentVector(), |
| 198 | stdin_path, |
| 199 | stdout_path, |
| 200 | stderr_path, |
Daniel Malea | 1e44fdd | 2013-01-08 14:49:22 +0000 | [diff] [blame] | 201 | working_dir, |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 202 | error); |
| 203 | |
| 204 | m_module = module; |
| 205 | |
| 206 | if (!error.Success()) |
| 207 | return error; |
| 208 | |
Matt Kopec | a7cd95d | 2013-03-14 21:35:26 +0000 | [diff] [blame^] | 209 | SetSTDIOFileDescriptor(m_monitor->GetTerminalFD()); |
| 210 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 211 | SetID(m_monitor->GetPID()); |
| 212 | return error; |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | ProcessPOSIX::DidLaunch() |
| 217 | { |
| 218 | } |
| 219 | |
| 220 | Error |
| 221 | ProcessPOSIX::DoResume() |
| 222 | { |
| 223 | StateType state = GetPrivateState(); |
| 224 | |
| 225 | assert(state == eStateStopped || state == eStateCrashed); |
| 226 | |
| 227 | // We are about to resume a thread that will cause the process to exit so |
| 228 | // set our exit status now. Do not change our state if the inferior |
| 229 | // crashed. |
| 230 | if (state == eStateStopped) |
| 231 | { |
| 232 | if (m_in_limbo) |
| 233 | SetExitStatus(m_exit_status, NULL); |
| 234 | else |
| 235 | SetPrivateState(eStateRunning); |
| 236 | } |
| 237 | |
| 238 | bool did_resume = false; |
| 239 | uint32_t thread_count = m_thread_list.GetSize(false); |
| 240 | for (uint32_t i = 0; i < thread_count; ++i) |
| 241 | { |
| 242 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 243 | m_thread_list.GetThreadAtIndex(i, false).get()); |
| 244 | did_resume = thread->Resume() || did_resume; |
| 245 | } |
| 246 | assert(did_resume && "Process resume failed!"); |
| 247 | |
| 248 | return Error(); |
| 249 | } |
| 250 | |
| 251 | addr_t |
| 252 | ProcessPOSIX::GetImageInfoAddress() |
| 253 | { |
| 254 | Target *target = &GetTarget(); |
| 255 | ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); |
| 256 | Address addr = obj_file->GetImageInfoAddress(); |
| 257 | |
| 258 | if (addr.IsValid()) |
| 259 | return addr.GetLoadAddress(target); |
| 260 | else |
| 261 | return LLDB_INVALID_ADDRESS; |
| 262 | } |
| 263 | |
| 264 | Error |
| 265 | ProcessPOSIX::DoHalt(bool &caused_stop) |
| 266 | { |
| 267 | Error error; |
| 268 | |
| 269 | if (IsStopped()) |
| 270 | { |
| 271 | caused_stop = false; |
| 272 | } |
| 273 | else if (kill(GetID(), SIGSTOP)) |
| 274 | { |
| 275 | caused_stop = false; |
| 276 | error.SetErrorToErrno(); |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | caused_stop = true; |
| 281 | } |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 282 | return error; |
| 283 | } |
| 284 | |
| 285 | Error |
| 286 | ProcessPOSIX::DoDetach() |
| 287 | { |
| 288 | Error error; |
| 289 | |
| 290 | error = m_monitor->Detach(); |
| 291 | if (error.Success()) |
| 292 | SetPrivateState(eStateDetached); |
| 293 | |
| 294 | return error; |
| 295 | } |
| 296 | |
| 297 | Error |
| 298 | ProcessPOSIX::DoSignal(int signal) |
| 299 | { |
| 300 | Error error; |
| 301 | |
| 302 | if (kill(GetID(), signal)) |
| 303 | error.SetErrorToErrno(); |
| 304 | |
| 305 | return error; |
| 306 | } |
| 307 | |
| 308 | Error |
| 309 | ProcessPOSIX::DoDestroy() |
| 310 | { |
| 311 | Error error; |
| 312 | |
| 313 | if (!HasExited()) |
| 314 | { |
| 315 | // Drive the exit event to completion (do not keep the inferior in |
| 316 | // limbo). |
| 317 | m_exit_now = true; |
| 318 | |
Greg Clayton | 972c438 | 2012-03-30 19:56:32 +0000 | [diff] [blame] | 319 | if ((m_monitor == NULL || kill(m_monitor->GetPID(), SIGKILL)) && error.Success()) |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 320 | { |
| 321 | error.SetErrorToErrno(); |
| 322 | return error; |
| 323 | } |
| 324 | |
| 325 | SetPrivateState(eStateExited); |
| 326 | } |
| 327 | |
| 328 | return error; |
| 329 | } |
| 330 | |
| 331 | void |
| 332 | ProcessPOSIX::SendMessage(const ProcessMessage &message) |
| 333 | { |
| 334 | Mutex::Locker lock(m_message_mutex); |
| 335 | |
| 336 | switch (message.GetKind()) |
| 337 | { |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 338 | case ProcessMessage::eInvalidMessage: |
| 339 | return; |
| 340 | |
| 341 | case ProcessMessage::eLimboMessage: |
| 342 | m_in_limbo = true; |
| 343 | m_exit_status = message.GetExitStatus(); |
| 344 | if (m_exit_now) |
| 345 | { |
| 346 | SetPrivateState(eStateExited); |
| 347 | m_monitor->Detach(); |
| 348 | } |
| 349 | else |
| 350 | SetPrivateState(eStateStopped); |
| 351 | break; |
| 352 | |
| 353 | case ProcessMessage::eExitMessage: |
| 354 | m_exit_status = message.GetExitStatus(); |
| 355 | SetExitStatus(m_exit_status, NULL); |
| 356 | break; |
| 357 | |
| 358 | case ProcessMessage::eTraceMessage: |
| 359 | case ProcessMessage::eBreakpointMessage: |
| 360 | SetPrivateState(eStateStopped); |
| 361 | break; |
| 362 | |
| 363 | case ProcessMessage::eSignalMessage: |
| 364 | case ProcessMessage::eSignalDeliveredMessage: |
Matt Kopec | f1fda37 | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 365 | { |
| 366 | lldb::tid_t tid = message.GetTID(); |
| 367 | lldb::tid_t pid = GetID(); |
| 368 | if (tid == pid) { |
| 369 | SetPrivateState(eStateStopped); |
| 370 | break; |
| 371 | } else { |
| 372 | // FIXME: Ignore any signals generated by children. |
| 373 | return; |
| 374 | } |
| 375 | } |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 376 | |
| 377 | case ProcessMessage::eCrashMessage: |
Andrew Kaylor | 05a4789 | 2012-12-14 18:24:34 +0000 | [diff] [blame] | 378 | // FIXME: Update stop reason as per bugzilla 14598 |
| 379 | SetPrivateState(eStateStopped); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 380 | break; |
Matt Kopec | f1fda37 | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 381 | |
| 382 | case ProcessMessage::eNewThreadMessage: |
| 383 | SetPrivateState(eStateStopped); |
| 384 | break; |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | m_message_queue.push(message); |
| 388 | } |
| 389 | |
| 390 | void |
| 391 | ProcessPOSIX::RefreshStateAfterStop() |
| 392 | { |
| 393 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PROCESS)); |
| 394 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
| 395 | log->Printf ("ProcessPOSIX::%s()", __FUNCTION__); |
| 396 | |
| 397 | Mutex::Locker lock(m_message_mutex); |
| 398 | if (m_message_queue.empty()) |
| 399 | return; |
| 400 | |
| 401 | ProcessMessage &message = m_message_queue.front(); |
| 402 | |
| 403 | // Resolve the thread this message corresponds to and pass it along. |
| 404 | // FIXME: we're really dealing with the pid here. This should get |
| 405 | // fixed when this code is fixed to handle multiple threads. |
| 406 | lldb::tid_t tid = message.GetTID(); |
| 407 | if (log) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 408 | log->Printf ("ProcessPOSIX::%s() pid = %" PRIi64, __FUNCTION__, tid); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 409 | POSIXThread *thread = static_cast<POSIXThread*>( |
| 410 | GetThreadList().FindThreadByID(tid, false).get()); |
| 411 | |
Matt Kopec | f1fda37 | 2013-01-08 16:30:18 +0000 | [diff] [blame] | 412 | if (message.GetKind() == ProcessMessage::eNewThreadMessage) { |
| 413 | ThreadSP thread_sp; |
| 414 | thread_sp.reset(new POSIXThread(*this, message.GetChildTID())); |
| 415 | m_thread_list.AddThread(thread_sp); |
| 416 | } |
| 417 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 418 | assert(thread); |
| 419 | thread->Notify(message); |
| 420 | |
| 421 | m_message_queue.pop(); |
| 422 | } |
| 423 | |
| 424 | bool |
| 425 | ProcessPOSIX::IsAlive() |
| 426 | { |
| 427 | StateType state = GetPrivateState(); |
| 428 | return state != eStateDetached && state != eStateExited && state != eStateInvalid; |
| 429 | } |
| 430 | |
| 431 | size_t |
| 432 | ProcessPOSIX::DoReadMemory(addr_t vm_addr, |
| 433 | void *buf, size_t size, Error &error) |
| 434 | { |
| 435 | assert(m_monitor); |
| 436 | return m_monitor->ReadMemory(vm_addr, buf, size, error); |
| 437 | } |
| 438 | |
| 439 | size_t |
| 440 | ProcessPOSIX::DoWriteMemory(addr_t vm_addr, const void *buf, size_t size, |
| 441 | Error &error) |
| 442 | { |
| 443 | assert(m_monitor); |
| 444 | return m_monitor->WriteMemory(vm_addr, buf, size, error); |
| 445 | } |
| 446 | |
| 447 | addr_t |
| 448 | ProcessPOSIX::DoAllocateMemory(size_t size, uint32_t permissions, |
| 449 | Error &error) |
| 450 | { |
| 451 | addr_t allocated_addr = LLDB_INVALID_ADDRESS; |
| 452 | |
| 453 | unsigned prot = 0; |
| 454 | if (permissions & lldb::ePermissionsReadable) |
| 455 | prot |= eMmapProtRead; |
| 456 | if (permissions & lldb::ePermissionsWritable) |
| 457 | prot |= eMmapProtWrite; |
| 458 | if (permissions & lldb::ePermissionsExecutable) |
| 459 | prot |= eMmapProtExec; |
| 460 | |
| 461 | if (InferiorCallMmap(this, allocated_addr, 0, size, prot, |
| 462 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { |
| 463 | m_addr_to_mmap_size[allocated_addr] = size; |
| 464 | error.Clear(); |
| 465 | } else { |
| 466 | allocated_addr = LLDB_INVALID_ADDRESS; |
| 467 | error.SetErrorStringWithFormat("unable to allocate %zu bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); |
| 468 | } |
| 469 | |
| 470 | return allocated_addr; |
| 471 | } |
| 472 | |
| 473 | Error |
| 474 | ProcessPOSIX::DoDeallocateMemory(lldb::addr_t addr) |
| 475 | { |
| 476 | Error error; |
| 477 | MMapMap::iterator pos = m_addr_to_mmap_size.find(addr); |
| 478 | if (pos != m_addr_to_mmap_size.end() && |
| 479 | InferiorCallMunmap(this, addr, pos->second)) |
| 480 | m_addr_to_mmap_size.erase (pos); |
| 481 | else |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 482 | error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 483 | |
| 484 | return error; |
| 485 | } |
| 486 | |
Matt Kopec | 4f9103f | 2013-02-27 20:13:38 +0000 | [diff] [blame] | 487 | addr_t |
| 488 | ProcessPOSIX::ResolveIndirectFunction(const Address *address, Error &error) |
| 489 | { |
| 490 | addr_t function_addr = LLDB_INVALID_ADDRESS; |
| 491 | if (address == NULL) { |
| 492 | error.SetErrorStringWithFormat("unable to determine direct function call for NULL address"); |
| 493 | } else if (!InferiorCall(this, address, function_addr)) { |
| 494 | function_addr = LLDB_INVALID_ADDRESS; |
Matt Kopec | 7012548 | 2013-03-01 17:44:31 +0000 | [diff] [blame] | 495 | error.SetErrorStringWithFormat("unable to determine direct function call for indirect function %s", |
| 496 | address->CalculateSymbolContextSymbol()->GetName().AsCString()); |
Matt Kopec | 4f9103f | 2013-02-27 20:13:38 +0000 | [diff] [blame] | 497 | } |
| 498 | return function_addr; |
| 499 | } |
| 500 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 501 | size_t |
| 502 | ProcessPOSIX::GetSoftwareBreakpointTrapOpcode(BreakpointSite* bp_site) |
| 503 | { |
| 504 | static const uint8_t g_i386_opcode[] = { 0xCC }; |
| 505 | |
| 506 | ArchSpec arch = GetTarget().GetArchitecture(); |
| 507 | const uint8_t *opcode = NULL; |
| 508 | size_t opcode_size = 0; |
| 509 | |
| 510 | switch (arch.GetCore()) |
| 511 | { |
| 512 | default: |
| 513 | assert(false && "CPU type not supported!"); |
| 514 | break; |
| 515 | |
| 516 | case ArchSpec::eCore_x86_32_i386: |
| 517 | case ArchSpec::eCore_x86_64_x86_64: |
| 518 | opcode = g_i386_opcode; |
| 519 | opcode_size = sizeof(g_i386_opcode); |
| 520 | break; |
| 521 | } |
| 522 | |
| 523 | bp_site->SetTrapOpcode(opcode, opcode_size); |
| 524 | return opcode_size; |
| 525 | } |
| 526 | |
| 527 | Error |
Daniel Malea | 52d8dd9 | 2013-02-15 20:23:25 +0000 | [diff] [blame] | 528 | ProcessPOSIX::EnableBreakpointSite(BreakpointSite *bp_site) |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 529 | { |
| 530 | return EnableSoftwareBreakpoint(bp_site); |
| 531 | } |
| 532 | |
| 533 | Error |
Daniel Malea | 52d8dd9 | 2013-02-15 20:23:25 +0000 | [diff] [blame] | 534 | ProcessPOSIX::DisableBreakpointSite(BreakpointSite *bp_site) |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 535 | { |
| 536 | return DisableSoftwareBreakpoint(bp_site); |
| 537 | } |
| 538 | |
| 539 | uint32_t |
| 540 | ProcessPOSIX::UpdateThreadListIfNeeded() |
| 541 | { |
| 542 | // Do not allow recursive updates. |
| 543 | return m_thread_list.GetSize(false); |
| 544 | } |
| 545 | |
Greg Clayton | c8dd570 | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 546 | bool |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 547 | ProcessPOSIX::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) |
| 548 | { |
| 549 | LogSP log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_THREAD)); |
| 550 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 551 | log->Printf ("ProcessPOSIX::%s() (pid = %" PRIi64 ")", __FUNCTION__, GetID()); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 552 | |
| 553 | // Update the process thread list with this new thread. |
| 554 | // FIXME: We should be using tid, not pid. |
| 555 | assert(m_monitor); |
| 556 | ThreadSP thread_sp (old_thread_list.FindThreadByID (GetID(), false)); |
Greg Clayton | e5eaa30 | 2012-02-21 18:40:07 +0000 | [diff] [blame] | 557 | if (!thread_sp) { |
Greg Clayton | 5e91e37 | 2012-10-12 16:23:23 +0000 | [diff] [blame] | 558 | thread_sp.reset(new POSIXThread(*this, GetID())); |
Greg Clayton | e5eaa30 | 2012-02-21 18:40:07 +0000 | [diff] [blame] | 559 | } |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 560 | |
| 561 | if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 562 | log->Printf ("ProcessPOSIX::%s() updated pid = %" PRIi64, __FUNCTION__, GetID()); |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 563 | new_thread_list.AddThread(thread_sp); |
| 564 | |
Greg Clayton | c8dd570 | 2012-04-12 19:04:34 +0000 | [diff] [blame] | 565 | return new_thread_list.GetSize(false) > 0; |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | ByteOrder |
| 569 | ProcessPOSIX::GetByteOrder() const |
| 570 | { |
| 571 | // FIXME: We should be able to extract this value directly. See comment in |
| 572 | // ProcessPOSIX(). |
| 573 | return m_byte_order; |
| 574 | } |
| 575 | |
| 576 | size_t |
| 577 | ProcessPOSIX::PutSTDIN(const char *buf, size_t len, Error &error) |
| 578 | { |
| 579 | ssize_t status; |
| 580 | if ((status = write(m_monitor->GetTerminalFD(), buf, len)) < 0) |
| 581 | { |
| 582 | error.SetErrorToErrno(); |
| 583 | return 0; |
| 584 | } |
| 585 | return status; |
| 586 | } |
| 587 | |
Johnny Chen | 2341d35 | 2012-01-05 21:48:15 +0000 | [diff] [blame] | 588 | UnixSignals & |
| 589 | ProcessPOSIX::GetUnixSignals() |
| 590 | { |
| 591 | return m_signals; |
| 592 | } |
| 593 | |
| 594 | //------------------------------------------------------------------------------ |
| 595 | // Utility functions. |
| 596 | |
| 597 | bool |
| 598 | ProcessPOSIX::HasExited() |
| 599 | { |
| 600 | switch (GetPrivateState()) |
| 601 | { |
| 602 | default: |
| 603 | break; |
| 604 | |
| 605 | case eStateDetached: |
| 606 | case eStateExited: |
| 607 | return true; |
| 608 | } |
| 609 | |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | bool |
| 614 | ProcessPOSIX::IsStopped() |
| 615 | { |
| 616 | switch (GetPrivateState()) |
| 617 | { |
| 618 | default: |
| 619 | break; |
| 620 | |
| 621 | case eStateStopped: |
| 622 | case eStateCrashed: |
| 623 | case eStateSuspended: |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | return false; |
| 628 | } |