Author: labath Date: Thu Jan 19 09:26:04 2017 New Revision: 292489 URL: http://llvm.org/viewvc/llvm-project?rev=292489&view=rev Log: Refactor logging in NativeProcessLinux
Use the LLDB_LOG macro instead of the more verbose if(log) ... syntax. I have also consolidated the log channels (everything now goes to the posix channel, instead of a mixture of posix and lldb), and cleaned up some of the more convoluted log statements. Modified: lldb/trunk/source/Plugins/Process/Linux/NativeProcessLinux.cpp Modified: lldb/trunk/source/Plugins/Process/Linux/NativeProcessLinux.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/Linux/NativeProcessLinux.cpp?rev=292489&r1=292488&r2=292489&view=diff ============================================================================== --- lldb/trunk/source/Plugins/Process/Linux/NativeProcessLinux.cpp (original) +++ lldb/trunk/source/Plugins/Process/Linux/NativeProcessLinux.cpp Thu Jan 19 09:26:04 2017 @@ -78,7 +78,7 @@ static bool ProcessVmReadvSupported() { static std::once_flag flag; std::call_once(flag, [] { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); uint32_t source = 0x47424742; uint32_t dest = 0; @@ -92,16 +92,15 @@ static bool ProcessVmReadvSupported() { // value from our own process. ssize_t res = process_vm_readv(getpid(), &local, 1, &remote, 1, 0); is_supported = (res == sizeof(source) && source == dest); - if (log) { - if (is_supported) - log->Printf("%s: Detected kernel support for process_vm_readv syscall. " - "Fast memory reads enabled.", - __FUNCTION__); - else - log->Printf("%s: syscall process_vm_readv failed (error: %s). Fast " - "memory reads disabled.", - __FUNCTION__, strerror(errno)); - } + if (is_supported) + LLDB_LOG(log, + "Detected kernel support for process_vm_readv syscall. " + "Fast memory reads enabled."); + else + LLDB_LOG(log, + "syscall process_vm_readv failed (error: {0}). Fast memory " + "reads disabled.", + strerror(errno)); }); return is_supported; @@ -109,33 +108,29 @@ static bool ProcessVmReadvSupported() { namespace { void MaybeLogLaunchInfo(const ProcessLaunchInfo &info) { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); if (!log) return; if (const FileAction *action = info.GetFileActionForFD(STDIN_FILENO)) - log->Printf("%s: setting STDIN to '%s'", __FUNCTION__, - action->GetFileSpec().GetCString()); + LLDB_LOG(log, "setting STDIN to '{0}'", action->GetFileSpec()); else - log->Printf("%s leaving STDIN as is", __FUNCTION__); + LLDB_LOG(log, "leaving STDIN as is"); if (const FileAction *action = info.GetFileActionForFD(STDOUT_FILENO)) - log->Printf("%s setting STDOUT to '%s'", __FUNCTION__, - action->GetFileSpec().GetCString()); + LLDB_LOG(log, "setting STDOUT to '{0}'", action->GetFileSpec()); else - log->Printf("%s leaving STDOUT as is", __FUNCTION__); + LLDB_LOG(log, "leaving STDOUT as is"); if (const FileAction *action = info.GetFileActionForFD(STDERR_FILENO)) - log->Printf("%s setting STDERR to '%s'", __FUNCTION__, - action->GetFileSpec().GetCString()); + LLDB_LOG(log, "setting STDERR to '{0}'", action->GetFileSpec()); else - log->Printf("%s leaving STDERR as is", __FUNCTION__); + LLDB_LOG(log, "leaving STDERR as is"); int i = 0; for (const char **args = info.GetArguments().GetConstArgumentVector(); *args; ++args, ++i) - log->Printf("%s arg %d: \"%s\"", __FUNCTION__, i, - *args ? *args : "nullptr"); + LLDB_LOG(log, "arg {0}: '{1}'", i, *args); } void DisplayBytes(StreamString &s, void *bytes, uint32_t count) { @@ -148,50 +143,50 @@ void DisplayBytes(StreamString &s, void } void PtraceDisplayBytes(int &req, void *data, size_t data_size) { + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE | + POSIX_LOG_VERBOSE)); + if (!log) + return; StreamString buf; - Log *verbose_log(ProcessPOSIXLog::GetLogIfAllCategoriesSet( - POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE)); - if (verbose_log) { - switch (req) { - case PTRACE_POKETEXT: { - DisplayBytes(buf, &data, 8); - verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData()); - break; - } - case PTRACE_POKEDATA: { - DisplayBytes(buf, &data, 8); - verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData()); - break; - } - case PTRACE_POKEUSER: { - DisplayBytes(buf, &data, 8); - verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData()); - break; - } - case PTRACE_SETREGS: { - DisplayBytes(buf, data, data_size); - verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData()); - break; - } - case PTRACE_SETFPREGS: { - DisplayBytes(buf, data, data_size); - verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData()); - break; - } - case PTRACE_SETSIGINFO: { - DisplayBytes(buf, data, sizeof(siginfo_t)); - verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData()); - break; - } - case PTRACE_SETREGSET: { - // Extract iov_base from data, which is a pointer to the struct IOVEC - DisplayBytes(buf, *(void **)data, data_size); - verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData()); - break; - } - default: {} - } + switch (req) { + case PTRACE_POKETEXT: { + DisplayBytes(buf, &data, 8); + LLDB_LOG(log, "PTRACE_POKETEXT {0}", buf.GetData()); + break; + } + case PTRACE_POKEDATA: { + DisplayBytes(buf, &data, 8); + LLDB_LOG(log, "PTRACE_POKEDATA {0}", buf.GetData()); + break; + } + case PTRACE_POKEUSER: { + DisplayBytes(buf, &data, 8); + LLDB_LOG(log, "PTRACE_POKEUSER {0}", buf.GetData()); + break; + } + case PTRACE_SETREGS: { + DisplayBytes(buf, data, data_size); + LLDB_LOG(log, "PTRACE_SETREGS {0}", buf.GetData()); + break; + } + case PTRACE_SETFPREGS: { + DisplayBytes(buf, data, data_size); + LLDB_LOG(log, "PTRACE_SETFPREGS {0}", buf.GetData()); + break; + } + case PTRACE_SETSIGINFO: { + DisplayBytes(buf, data, sizeof(siginfo_t)); + LLDB_LOG(log, "PTRACE_SETSIGINFO {0}", buf.GetData()); + break; + } + case PTRACE_SETREGSET: { + // Extract iov_base from data, which is a pointer to the struct IOVEC + DisplayBytes(buf, *(void **)data, data_size); + LLDB_LOG(log, "PTRACE_SETREGSET {0}", buf.GetData()); + break; + } + default: {} } } @@ -227,7 +222,7 @@ Error NativeProcessProtocol::Launch( ProcessLaunchInfo &launch_info, NativeProcessProtocol::NativeDelegate &native_delegate, MainLoop &mainloop, NativeProcessProtocolSP &native_process_sp) { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); Error error; @@ -255,9 +250,7 @@ Error NativeProcessProtocol::Launch( if (error.Fail()) { native_process_sp.reset(); - if (log) - log->Printf("NativeProcessLinux::%s failed to launch process: %s", - __FUNCTION__, error.AsCString()); + LLDB_LOG(log, "failed to launch process: {0}", error); return error; } @@ -269,9 +262,8 @@ Error NativeProcessProtocol::Launch( Error NativeProcessProtocol::Attach( lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, MainLoop &mainloop, NativeProcessProtocolSP &native_process_sp) { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); - if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) - log->Printf("NativeProcessLinux::%s(pid = %" PRIi64 ")", __FUNCTION__, pid); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); + LLDB_LOG(log, "pid = {0:x}", pid); // Retrieve the architecture for the running process. ArchSpec process_arch; @@ -306,10 +298,8 @@ NativeProcessLinux::NativeProcessLinux() void NativeProcessLinux::AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, Error &error) { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); - if (log) - log->Printf("NativeProcessLinux::%s (pid = %" PRIi64 ")", __FUNCTION__, - pid); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); + LLDB_LOG(log, "pid = {0:x}", pid); m_sigchld_handle = mainloop.RegisterSignal( SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); @@ -321,11 +311,8 @@ void NativeProcessLinux::AttachToInferio return; // Set the architecture to the exe architecture. - if (log) - log->Printf("NativeProcessLinux::%s (pid = %" PRIi64 - ") detected architecture %s", - __FUNCTION__, pid, m_arch.GetArchitectureName()); - + LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid, + m_arch.GetArchitectureName()); m_pid = pid; SetState(eStateAttaching); @@ -349,16 +336,14 @@ Error NativeProcessLinux::LaunchInferior if (error.Fail()) return error; - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); // Wait for the child process to trap on its call to execve. ::pid_t wpid; int status; if ((wpid = waitpid(pid, &status, 0)) < 0) { error.SetErrorToErrno(); - if (log) - log->Printf("NativeProcessLinux::%s waitpid for inferior failed with %s", - __FUNCTION__, error.AsCString()); + LLDB_LOG(log, "waitpid for inferior failed with %s", error); // Mark the inferior as invalid. // FIXME this could really use a new state - eStateLaunchFailure. For now, @@ -370,16 +355,10 @@ Error NativeProcessLinux::LaunchInferior assert(WIFSTOPPED(status) && (wpid == static_cast<::pid_t>(pid)) && "Could not sync with inferior process."); - if (log) - log->Printf("NativeProcessLinux::%s inferior started, now in stopped state", - __FUNCTION__); - + LLDB_LOG(log, "inferior started, now in stopped state"); error = SetDefaultPtraceOpts(pid); if (error.Fail()) { - if (log) - log->Printf("NativeProcessLinux::%s inferior failed to set default " - "ptrace options: %s", - __FUNCTION__, error.AsCString()); + LLDB_LOG(log, "failed to set default ptrace options: {0}", error); // Mark the inferior as invalid. // FIXME this could really use a new state - eStateLaunchFailure. For now, @@ -398,10 +377,10 @@ Error NativeProcessLinux::LaunchInferior if (m_terminal_fd != -1) { error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); if (error.Fail()) { - if (log) - log->Printf("NativeProcessLinux::%s inferior EnsureFDFlags failed for " - "ensuring terminal O_NONBLOCK setting: %s", - __FUNCTION__, error.AsCString()); + LLDB_LOG(log, + "inferior EnsureFDFlags failed for ensuring terminal " + "O_NONBLOCK setting: {0}", + error); // Mark the inferior as invalid. // FIXME this could really use a new state - eStateLaunchFailure. For @@ -412,10 +391,7 @@ Error NativeProcessLinux::LaunchInferior } } - if (log) - log->Printf("NativeProcessLinux::%s() adding pid = %" PRIu64, __FUNCTION__, - uint64_t(pid)); - + LLDB_LOG(log, "adding pid = {0}", pid); ResolveProcessArchitecture(m_pid, m_arch); NativeThreadLinuxSP thread_sp = AddThread(pid); assert(thread_sp && "AddThread() returned a nullptr thread"); @@ -426,19 +402,13 @@ Error NativeProcessLinux::LaunchInferior SetCurrentThreadID(thread_sp->GetID()); SetState(StateType::eStateStopped); - if (log) { - if (error.Success()) - log->Printf("NativeProcessLinux::%s inferior launching succeeded", - __FUNCTION__); - else - log->Printf("NativeProcessLinux::%s inferior launching failed: %s", - __FUNCTION__, error.AsCString()); - } + if (error.Fail()) + LLDB_LOG(log, "inferior launching failed {0}", error); return error; } ::pid_t NativeProcessLinux::Attach(lldb::pid_t pid, Error &error) { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); // Use a map to keep track of the threads which we have attached/need to // attach. @@ -487,10 +457,7 @@ Error NativeProcessLinux::LaunchInferior if (error.Fail()) return -1; - if (log) - log->Printf("NativeProcessLinux::%s() adding tid = %" PRIu64, - __FUNCTION__, tid); - + LLDB_LOG(log, "adding tid = {0}", tid); it->second = true; // Create the thread, mark it as stopped. @@ -578,11 +545,8 @@ void NativeProcessLinux::MonitorCallback // Handle when the thread exits. if (exited) { - if (log) - log->Printf( - "NativeProcessLinux::%s() got exit signal(%d) , tid = %" PRIu64 - " (%s main thread)", - __FUNCTION__, signal, pid, is_main_thread ? "is" : "is not"); + LLDB_LOG(log, "got exit signal({0}) , tid = {1} ({2} main thread)", signal, + pid, is_main_thread ? "is" : "is not"); // This is a thread that exited. Ensure we're not tracking it anymore. const bool thread_found = StopTrackingThread(pid); @@ -596,41 +560,32 @@ void NativeProcessLinux::MonitorCallback const bool already_notified = (GetState() == StateType::eStateExited) || (GetState() == StateType::eStateCrashed); if (!already_notified) { - if (log) - log->Printf("NativeProcessLinux::%s() tid = %" PRIu64 - " handling main thread exit (%s), expected exit state " - "already set but state was %s instead, setting exit " - "state now", - __FUNCTION__, pid, - thread_found ? "stopped tracking thread metadata" - : "thread metadata not found", - StateAsCString(GetState())); + LLDB_LOG( + log, + "tid = {0} handling main thread exit ({1}), expected exit state " + "already set but state was {2} instead, setting exit state now", + pid, + thread_found ? "stopped tracking thread metadata" + : "thread metadata not found", + StateAsCString(GetState())); // The main thread exited. We're done monitoring. Report to delegate. SetExitStatus(convert_pid_status_to_exit_type(status), convert_pid_status_to_return_code(status), nullptr, true); // Notify delegate that our process has exited. SetState(StateType::eStateExited, true); - } else { - if (log) - log->Printf("NativeProcessLinux::%s() tid = %" PRIu64 - " main thread now exited (%s)", - __FUNCTION__, pid, - thread_found ? "stopped tracking thread metadata" - : "thread metadata not found"); - } + } else + LLDB_LOG(log, "tid = {0} main thread now exited (%s)", pid, + thread_found ? "stopped tracking thread metadata" + : "thread metadata not found"); } else { // Do we want to report to the delegate in this case? I think not. If - // this was an orderly - // thread exit, we would already have received the SIGTRAP | - // (PTRACE_EVENT_EXIT << 8) signal, - // and we would have done an all-stop then. - if (log) - log->Printf("NativeProcessLinux::%s() tid = %" PRIu64 - " handling non-main thread exit (%s)", - __FUNCTION__, pid, - thread_found ? "stopped tracking thread metadata" - : "thread metadata not found"); + // this was an orderly thread exit, we would already have received the + // SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, and we would have done an + // all-stop then. + LLDB_LOG(log, "tid = {0} handling non-main thread exit (%s)", pid, + thread_found ? "stopped tracking thread metadata" + : "thread metadata not found"); } return; } @@ -641,28 +596,20 @@ void NativeProcessLinux::MonitorCallback if (!thread_sp) { // Normally, the only situation when we cannot find the thread is if we have - // just - // received a new thread notification. This is indicated by GetSignalInfo() - // returning - // si_code == SI_USER and si_pid == 0 - if (log) - log->Printf("NativeProcessLinux::%s received notification about an " - "unknown tid %" PRIu64 ".", - __FUNCTION__, pid); + // just received a new thread notification. This is indicated by + // GetSignalInfo() returning si_code == SI_USER and si_pid == 0 + LLDB_LOG(log, "received notification about an unknown tid {0}.", pid); if (info_err.Fail()) { - if (log) - log->Printf("NativeProcessLinux::%s (tid %" PRIu64 - ") GetSignalInfo failed (%s). Ingoring this notification.", - __FUNCTION__, pid, info_err.AsCString()); + LLDB_LOG(log, + "(tid {0}) GetSignalInfo failed ({1}). " + "Ingoring this notification.", + pid, info_err); return; } - if (log && (info.si_code != SI_USER || info.si_pid != 0)) - log->Printf("NativeProcessLinux::%s (tid %" PRIu64 - ") unexpected signal info (si_code: %d, si_pid: %d). " - "Treating as a new thread notification anyway.", - __FUNCTION__, pid, info.si_code, info.si_pid); + LLDB_LOG(log, "tid {0}, si_code: {1}, si_pid: {2}", pid, info.si_code, + info.si_pid); auto thread_sp = AddThread(pid); // Resume the newly created thread. @@ -682,49 +629,35 @@ void NativeProcessLinux::MonitorCallback if (info_err.GetError() == EINVAL) { // This is a group stop reception for this tid. // We can reach here if we reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU - // into the - // tracee, triggering the group-stop mechanism. Normally receiving these - // would stop - // the process, pending a SIGCONT. Simulating this state in a debugger is - // hard and is - // generally not needed (one use case is debugging background task being - // managed by a - // shell). For general use, it is sufficient to stop the process in a - // signal-delivery + // into the tracee, triggering the group-stop mechanism. Normally + // receiving these would stop the process, pending a SIGCONT. Simulating + // this state in a debugger is hard and is generally not needed (one use + // case is debugging background task being managed by a shell). For + // general use, it is sufficient to stop the process in a signal-delivery // stop which happens before the group stop. This done by MonitorSignal - // and works - // correctly for all signals. - if (log) - log->Printf( - "NativeProcessLinux::%s received a group stop for pid %" PRIu64 - " tid %" PRIu64 ". Transparent handling of group stops not " - "supported, resuming the thread.", - __FUNCTION__, GetID(), pid); + // and works correctly for all signals. + LLDB_LOG(log, + "received a group stop for pid {0} tid {1}. Transparent " + "handling of group stops not supported, resuming the " + "thread.", + GetID(), pid); ResumeThread(*thread_sp, thread_sp->GetState(), LLDB_INVALID_SIGNAL_NUMBER); } else { // ptrace(GETSIGINFO) failed (but not due to group-stop). // A return value of ESRCH means the thread/process is no longer on the - // system, - // so it was killed somehow outside of our control. Either way, we can't - // do anything - // with it anymore. + // system, so it was killed somehow outside of our control. Either way, + // we can't do anything with it anymore. // Stop tracking the metadata for the thread since it's entirely off the // system now. const bool thread_found = StopTrackingThread(pid); - if (log) - log->Printf( - "NativeProcessLinux::%s GetSignalInfo failed: %s, tid = %" PRIu64 - ", signal = %d, status = %d (%s, %s, %s)", - __FUNCTION__, info_err.AsCString(), pid, signal, status, - info_err.GetError() == ESRCH ? "thread/process killed" - : "unknown reason", - is_main_thread ? "is main thread" : "is not main thread", - thread_found ? "thread metadata removed" - : "thread metadata not found"); + LLDB_LOG(log, + "GetSignalInfo failed: {0}, tid = {1}, signal = {2}, " + "status = {3}, main_thread = {4}, thread_found: {5}", + info_err, pid, signal, status, is_main_thread, thread_found); if (is_main_thread) { // Notify the delegate - our process is not available but appears to @@ -736,19 +669,18 @@ void NativeProcessLinux::MonitorCallback } else { // This thread was pulled out from underneath us. Anything to do here? // Do we want to do an all stop? - if (log) - log->Printf("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 - " non-main thread exit occurred, didn't tell delegate " - "anything since thread disappeared out from underneath " - "us", - __FUNCTION__, GetID(), pid); + LLDB_LOG(log, + "pid {0} tid {1} non-main thread exit occurred, didn't " + "tell delegate anything since thread disappeared out " + "from underneath us", + GetID(), pid); } } } } void NativeProcessLinux::WaitForNewThread(::pid_t tid) { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); NativeThreadLinuxSP new_thread_sp = GetThreadByID(tid); @@ -763,65 +695,34 @@ void NativeProcessLinux::WaitForNewThrea int status = -1; ::pid_t wait_pid; do { - if (log) - log->Printf("NativeProcessLinux::%s() received thread creation event for " - "tid %" PRIu32 - ". tid not tracked yet, waiting for thread to appear...", - __FUNCTION__, tid); + LLDB_LOG(log, + "received thread creation event for tid {0}. tid not tracked " + "yet, waiting for thread to appear...", + tid); wait_pid = waitpid(tid, &status, __WALL); } while (wait_pid == -1 && errno == EINTR); // Since we are waiting on a specific tid, this must be the creation event. - // But let's do - // some checks just in case. + // But let's do some checks just in case. if (wait_pid != tid) { - if (log) - log->Printf( - "NativeProcessLinux::%s() waiting for tid %" PRIu32 - " failed. Assuming the thread has disappeared in the meantime", - __FUNCTION__, tid); + LLDB_LOG(log, + "waiting for tid {0} failed. Assuming the thread has " + "disappeared in the meantime", + tid); // The only way I know of this could happen is if the whole process was // SIGKILLed in the mean time. In any case, we can't do anything about that // now. return; } if (WIFEXITED(status)) { - if (log) - log->Printf("NativeProcessLinux::%s() waiting for tid %" PRIu32 - " returned an 'exited' event. Not tracking the thread.", - __FUNCTION__, tid); + LLDB_LOG(log, + "waiting for tid {0} returned an 'exited' event. Not " + "tracking the thread.", + tid); // Also a very improbable event. return; } - siginfo_t info; - Error error = GetSignalInfo(tid, &info); - if (error.Fail()) { - if (log) - log->Printf( - "NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 - " failed. Assuming the thread has disappeared in the meantime.", - __FUNCTION__, tid); - return; - } - - if (((info.si_pid != 0) || (info.si_code != SI_USER)) && log) { - // We should be getting a thread creation signal here, but we received - // something - // else. There isn't much we can do about it now, so we will just log that. - // Since the - // thread is alive and we are receiving events from it, we shall pretend - // that it was - // created properly. - log->Printf("NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 - " received unexpected signal with code %d from pid %d.", - __FUNCTION__, tid, info.si_code, info.si_pid); - } - - if (log) - log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 - ": tracking new thread tid %" PRIu32, - __FUNCTION__, GetID(), tid); - + LLDB_LOG(log, "pid = {0}: tracking new thread tid {1}", GetID(), tid); new_thread_sp = AddThread(tid); ResumeThread(*new_thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); ThreadWasCreated(*new_thread_sp); @@ -829,7 +730,7 @@ void NativeProcessLinux::WaitForNewThrea void NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread) { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); const bool is_main_thread = (thread.GetID() == GetID()); assert(info.si_signo == SIGTRAP && "Unexpected child signal!"); @@ -852,11 +753,10 @@ void NativeProcessLinux::MonitorSIGTRAP( unsigned long event_message = 0; if (GetEventMessage(thread.GetID(), &event_message).Fail()) { - if (log) - log->Printf("NativeProcessLinux::%s() pid %" PRIu64 - " received thread creation event but GetEventMessage " - "failed so we don't know the new tid", - __FUNCTION__, thread.GetID()); + LLDB_LOG(log, + "pid {0} received thread creation event but " + "GetEventMessage failed so we don't know the new tid", + thread.GetID()); } else WaitForNewThread(event_message); @@ -866,35 +766,24 @@ void NativeProcessLinux::MonitorSIGTRAP( case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): { NativeThreadLinuxSP main_thread_sp; - if (log) - log->Printf("NativeProcessLinux::%s() received exec event, code = %d", - __FUNCTION__, info.si_code ^ SIGTRAP); + LLDB_LOG(log, "received exec event, code = {0}", info.si_code ^ SIGTRAP); // Exec clears any pending notifications. m_pending_notification_tid = LLDB_INVALID_THREAD_ID; // Remove all but the main thread here. Linux fork creates a new process // which only copies the main thread. - if (log) - log->Printf("NativeProcessLinux::%s exec received, stop tracking all but " - "main thread", - __FUNCTION__); + LLDB_LOG(log, "exec received, stop tracking all but main thread"); for (auto thread_sp : m_threads) { const bool is_main_thread = thread_sp && thread_sp->GetID() == GetID(); if (is_main_thread) { main_thread_sp = std::static_pointer_cast<NativeThreadLinux>(thread_sp); - if (log) - log->Printf( - "NativeProcessLinux::%s found main thread with tid %" PRIu64 - ", keeping", - __FUNCTION__, main_thread_sp->GetID()); + LLDB_LOG(log, "found main thread with tid {0}, keeping", + main_thread_sp->GetID()); } else { - if (log) - log->Printf( - "NativeProcessLinux::%s discarding non-main-thread tid %" PRIu64 - " due to exec", - __FUNCTION__, thread_sp->GetID()); + LLDB_LOG(log, "discarding non-main-thread tid {0} due to exec", + thread_sp->GetID()); } } @@ -906,11 +795,10 @@ void NativeProcessLinux::MonitorSIGTRAP( main_thread_sp->SetStoppedByExec(); } else { SetCurrentThreadID(LLDB_INVALID_THREAD_ID); - if (log) - log->Printf("NativeProcessLinux::%s pid %" PRIu64 - "no main thread found, discarded all threads, we're in a " - "no-thread state!", - __FUNCTION__, GetID()); + LLDB_LOG(log, + "pid {0} no main thread found, discarded all threads, " + "we're in a no-thread state!", + GetID()); } // Tell coordinator about about the "new" (since exec) stopped main thread. @@ -941,13 +829,11 @@ void NativeProcessLinux::MonitorSIGTRAP( if (GetEventMessage(thread.GetID(), &data).Fail()) data = -1; - if (log) { - log->Printf("NativeProcessLinux::%s() received PTRACE_EVENT_EXIT, data = " - "%lx (WIFEXITED=%s,WIFSIGNALED=%s), pid = %" PRIu64 " (%s)", - __FUNCTION__, data, WIFEXITED(data) ? "true" : "false", - WIFSIGNALED(data) ? "true" : "false", thread.GetID(), - is_main_thread ? "is main thread" : "not main thread"); - } + LLDB_LOG(log, + "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " + "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", + data, WIFEXITED(data), WIFSIGNALED(data), thread.GetID(), + is_main_thread); if (is_main_thread) { SetExitStatus(convert_pid_status_to_exit_type(data), @@ -978,11 +864,11 @@ void NativeProcessLinux::MonitorSIGTRAP( uint32_t wp_index; Error error = thread.GetRegisterContext()->GetWatchpointHitIndex( wp_index, (uintptr_t)info.si_addr); - if (error.Fail() && log) - log->Printf("NativeProcessLinux::%s() " - "received error while checking for watchpoint hits, " - "pid = %" PRIu64 " error = %s", - __FUNCTION__, thread.GetID(), error.AsCString()); + if (error.Fail()) + LLDB_LOG(log, + "received error while checking for watchpoint hits, pid = " + "{0}, error = {1}", + thread.GetID(), error); if (wp_index != LLDB_INVALID_INDEX32) { MonitorWatchpoint(thread, wp_index); break; @@ -1002,11 +888,11 @@ void NativeProcessLinux::MonitorSIGTRAP( uint32_t wp_index; Error error = thread.GetRegisterContext()->GetWatchpointHitIndex( wp_index, LLDB_INVALID_ADDRESS); - if (error.Fail() && log) - log->Printf("NativeProcessLinux::%s() " - "received error while checking for watchpoint hits, " - "pid = %" PRIu64 " error = %s", - __FUNCTION__, thread.GetID(), error.AsCString()); + if (error.Fail()) + LLDB_LOG(log, + "received error while checking for watchpoint hits, pid = " + "{0}, error = {1}", + thread.GetID(), error); if (wp_index != LLDB_INVALID_INDEX32) { MonitorWatchpoint(thread, wp_index); break; @@ -1020,31 +906,28 @@ void NativeProcessLinux::MonitorSIGTRAP( case SIGTRAP: case (SIGTRAP | 0x80): - if (log) - log->Printf("NativeProcessLinux::%s() received unknown SIGTRAP system " - "call stop event, pid %" PRIu64 "tid %" PRIu64 ", resuming", - __FUNCTION__, GetID(), thread.GetID()); + LLDB_LOG( + log, + "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming", + info.si_code, GetID(), thread.GetID()); // Ignore these signals until we know more about them. ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); break; default: - assert(false && "Unexpected SIGTRAP code!"); - if (log) - log->Printf("NativeProcessLinux::%s() pid %" PRIu64 "tid %" PRIu64 - " received unhandled SIGTRAP code: 0x%d", - __FUNCTION__, GetID(), thread.GetID(), info.si_code); + LLDB_LOG( + log, + "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming", + info.si_code, GetID(), thread.GetID()); + llvm_unreachable("Unexpected SIGTRAP code!"); break; } } void NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); - if (log) - log->Printf("NativeProcessLinux::%s() received trace event, pid = %" PRIu64 - " (single stepping)", - __FUNCTION__, thread.GetID()); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); + LLDB_LOG(log, "received trace event, pid = {0}", thread.GetID()); // This thread is currently stopped. thread.SetStoppedByTrace(); @@ -1055,18 +938,13 @@ void NativeProcessLinux::MonitorTrace(Na void NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) { Log *log( GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); - if (log) - log->Printf( - "NativeProcessLinux::%s() received breakpoint event, pid = %" PRIu64, - __FUNCTION__, thread.GetID()); + LLDB_LOG(log, "received breakpoint event, pid = {0}", thread.GetID()); // Mark the thread as stopped at breakpoint. thread.SetStoppedByBreakpoint(); Error error = FixupBreakpointPCAsNeeded(thread); if (error.Fail()) - if (log) - log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " fixup: %s", - __FUNCTION__, thread.GetID(), error.AsCString()); + LLDB_LOG(log, "pid = {0} fixup: {1}", thread.GetID(), error); if (m_threads_stepping_with_breakpoint.find(thread.GetID()) != m_threads_stepping_with_breakpoint.end()) @@ -1079,10 +957,8 @@ void NativeProcessLinux::MonitorWatchpoi uint32_t wp_index) { Log *log( GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); - if (log) - log->Printf("NativeProcessLinux::%s() received watchpoint event, " - "pid = %" PRIu64 ", wp_index = %" PRIu32, - __FUNCTION__, thread.GetID(), wp_index); + LLDB_LOG(log, "received watchpoint event, pid = {0}, wp_index = {1}", + thread.GetID(), wp_index); // Mark the thread as stopped at watchpoint. // The address is at (lldb::addr_t)info->si_addr if we need it. @@ -1098,7 +974,7 @@ void NativeProcessLinux::MonitorSignal(c const int signo = info.si_signo; const bool is_from_llgs = info.si_pid == getpid(); - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); // POSIX says that process behaviour is undefined after it ignores a SIGFPE, // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a @@ -1110,47 +986,33 @@ void NativeProcessLinux::MonitorSignal(c // Similarly, ACK signals generated by this monitor. // Handle the signal. - if (info.si_code == SI_TKILL || info.si_code == SI_USER) { - if (log) - log->Printf("NativeProcessLinux::%s() received signal %s (%d) with code " - "%s, (siginfo pid = %d (%s), waitpid pid = %" PRIu64 ")", - __FUNCTION__, Host::GetSignalAsCString(signo), signo, - (info.si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"), - info.si_pid, is_from_llgs ? "from llgs" : "not from llgs", - thread.GetID()); - } + LLDB_LOG(log, + "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " + "waitpid pid = {4})", + Host::GetSignalAsCString(signo), signo, info.si_code, + thread.GetID()); // Check for thread stop notification. if (is_from_llgs && (info.si_code == SI_TKILL) && (signo == SIGSTOP)) { // This is a tgkill()-based stop. - if (log) - log->Printf("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 - ", thread stopped", - __FUNCTION__, GetID(), thread.GetID()); + LLDB_LOG(log, "pid {0} tid {1}, thread stopped", GetID(), thread.GetID()); // Check that we're not already marked with a stop reason. // Note this thread really shouldn't already be marked as stopped - if we - // were, that would imply that - // the kernel signaled us with the thread stopping which we handled and - // marked as stopped, - // and that, without an intervening resume, we received another stop. It is - // more likely - // that we are missing the marking of a run state somewhere if we find that - // the thread was - // marked as stopped. + // were, that would imply that the kernel signaled us with the thread + // stopping which we handled and marked as stopped, and that, without an + // intervening resume, we received another stop. It is more likely that we + // are missing the marking of a run state somewhere if we find that the + // thread was marked as stopped. const StateType thread_state = thread.GetState(); if (!StateIsStoppedState(thread_state, false)) { // An inferior thread has stopped because of a SIGSTOP we have sent it. // Generally, these are not important stops and we don't want to report - // them as - // they are just used to stop other threads when one thread (the one with - // the - // *real* stop reason) hits a breakpoint (watchpoint, etc...). However, in - // the - // case of an asynchronous Interrupt(), this *is* the real stop reason, so - // we - // leave the signal intact if this is the thread that was chosen as the - // triggering thread. + // them as they are just used to stop other threads when one thread (the + // one with the *real* stop reason) hits a breakpoint (watchpoint, + // etc...). However, in the case of an asynchronous Interrupt(), this *is* + // the real stop reason, so we leave the signal intact if this is the + // thread that was chosen as the triggering thread. if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { if (m_pending_notification_tid == thread.GetID()) thread.SetStoppedBySignal(SIGSTOP, &info); @@ -1163,30 +1025,15 @@ void NativeProcessLinux::MonitorSignal(c // We can end up here if stop was initiated by LLGS but by this time a // thread stop has occurred - maybe initiated by another event. Error error = ResumeThread(thread, thread.GetState(), 0); - if (error.Fail() && log) { - log->Printf( - "NativeProcessLinux::%s failed to resume thread tid %" PRIu64 - ": %s", - __FUNCTION__, thread.GetID(), error.AsCString()); - } + if (error.Fail()) + LLDB_LOG(log, "failed to resume thread {0}: {1}", thread.GetID(), + error); } } else { - if (log) { - // Retrieve the signal name if the thread was stopped by a signal. - int stop_signo = 0; - const bool stopped_by_signal = thread.IsStopped(&stop_signo); - const char *signal_name = stopped_by_signal - ? Host::GetSignalAsCString(stop_signo) - : "<not stopped by signal>"; - if (!signal_name) - signal_name = "<no-signal-name>"; - - log->Printf("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 - ", thread was already marked as a stopped state (state=%s, " - "signal=%d (%s)), leaving stop signal as is", - __FUNCTION__, GetID(), thread.GetID(), - StateAsCString(thread_state), stop_signo, signal_name); - } + LLDB_LOG(log, + "pid {0} tid {1}, thread was already marked as a stopped " + "state (state={2}), leaving stop signal as is", + GetID(), thread.GetID(), StateAsCString(thread_state)); SignalIfAllThreadsStopped(); } @@ -1194,11 +1041,8 @@ void NativeProcessLinux::MonitorSignal(c return; } - if (log) - log->Printf("NativeProcessLinux::%s() received signal %s", __FUNCTION__, - Host::GetSignalAsCString(signo)); - // This thread is stopped. + LLDB_LOG(log, "received signal {0}", Host::GetSignalAsCString(signo)); thread.SetStoppedBySignal(signo, &info); // Send a stop to the debugger after we get all other threads to stop. @@ -1383,10 +1227,8 @@ bool NativeProcessLinux::SupportHardware } Error NativeProcessLinux::Resume(const ResumeActionList &resume_actions) { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); - if (log) - log->Printf("NativeProcessLinux::%s called: pid %" PRIu64, __FUNCTION__, - GetID()); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); + LLDB_LOG(log, "pid {0}", GetID()); bool software_single_step = !SupportHardwareSingleStepping(); @@ -1415,20 +1257,13 @@ Error NativeProcessLinux::Resume(const R resume_actions.GetActionForThread(thread_sp->GetID(), true); if (action == nullptr) { - if (log) - log->Printf( - "NativeProcessLinux::%s no action specified for pid %" PRIu64 - " tid %" PRIu64, - __FUNCTION__, GetID(), thread_sp->GetID()); + LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), + thread_sp->GetID()); continue; } - if (log) { - log->Printf("NativeProcessLinux::%s processing resume action state %s " - "for pid %" PRIu64 " tid %" PRIu64, - __FUNCTION__, StateAsCString(action->state), GetID(), - thread_sp->GetID()); - } + LLDB_LOG(log, "processing resume action state {0} for pid {1} tid {2}", + StateAsCString(action->state), GetID(), thread_sp->GetID()); switch (action->state) { case eStateRunning: @@ -1442,7 +1277,7 @@ Error NativeProcessLinux::Resume(const R case eStateSuspended: case eStateStopped: - lldbassert(0 && "Unexpected state"); + llvm_unreachable("Unexpected state"); default: return Error("NativeProcessLinux::%s (): unexpected state %s specified " @@ -1487,11 +1322,9 @@ Error NativeProcessLinux::Detach() { Error NativeProcessLinux::Signal(int signo) { Error error; - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); - if (log) - log->Printf( - "NativeProcessLinux::%s: sending signal %d (%s) to pid %" PRIu64, - __FUNCTION__, signo, Host::GetSignalAsCString(signo), GetID()); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); + LLDB_LOG(log, "sending signal {0} ({1}) to pid {1}", signo, + Host::GetSignalAsCString(signo), GetID()); if (kill(GetID(), signo)) error.SetErrorToErrno(); @@ -1502,16 +1335,12 @@ Error NativeProcessLinux::Signal(int sig Error NativeProcessLinux::Interrupt() { // Pick a running thread (or if none, a not-dead stopped thread) as // the chosen thread that will be the stop-reason thread. - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); NativeThreadProtocolSP running_thread_sp; NativeThreadProtocolSP stopped_thread_sp; - if (log) - log->Printf( - "NativeProcessLinux::%s selecting running thread for interrupt target", - __FUNCTION__); - + LLDB_LOG(log, "selecting running thread for interrupt target"); for (auto thread_sp : m_threads) { // The thread shouldn't be null but lets just cover that here. if (!thread_sp) @@ -1533,9 +1362,7 @@ Error NativeProcessLinux::Interrupt() { if (!running_thread_sp && !stopped_thread_sp) { Error error("found no running/stepping or live stopped threads as target " "for interrupt"); - if (log) - log->Printf("NativeProcessLinux::%s skipping due to error: %s", - __FUNCTION__, error.AsCString()); + LLDB_LOG(log, "skipping due to error: {0}", error); return error; } @@ -1543,12 +1370,9 @@ Error NativeProcessLinux::Interrupt() { NativeThreadProtocolSP deferred_signal_thread_sp = running_thread_sp ? running_thread_sp : stopped_thread_sp; - if (log) - log->Printf("NativeProcessLinux::%s pid %" PRIu64 " %s tid %" PRIu64 - " chosen for interrupt target", - __FUNCTION__, GetID(), - running_thread_sp ? "running" : "stopped", - deferred_signal_thread_sp->GetID()); + LLDB_LOG(log, "pid {0} {1} tid {2} chosen for interrupt target", GetID(), + running_thread_sp ? "running" : "stopped", + deferred_signal_thread_sp->GetID()); StopRunningThreads(deferred_signal_thread_sp->GetID()); @@ -1556,10 +1380,8 @@ Error NativeProcessLinux::Interrupt() { } Error NativeProcessLinux::Kill() { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); - if (log) - log->Printf("NativeProcessLinux::%s called for PID %" PRIu64, __FUNCTION__, - GetID()); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); + LLDB_LOG(log, "pid {0}", GetID()); Error error; @@ -1570,10 +1392,8 @@ Error NativeProcessLinux::Kill() { case StateType::eStateDetached: case StateType::eStateUnloaded: // Nothing to do - the process is already dead. - if (log) - log->Printf("NativeProcessLinux::%s ignored for PID %" PRIu64 - " due to current state: %s", - __FUNCTION__, GetID(), StateAsCString(m_state)); + LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), + StateAsCString(m_state)); return error; case StateType::eStateConnected: @@ -1750,16 +1570,13 @@ Error NativeProcessLinux::GetMemoryRegio } Error NativeProcessLinux::PopulateMemoryRegionCache() { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); // If our cache is empty, pull the latest. There should always be at least // one memory region if memory region handling is supported. if (!m_mem_region_cache.empty()) { - if (log) - log->Printf("NativeProcessLinux::%s reusing %" PRIu64 - " cached memory region entries", - __FUNCTION__, - static_cast<uint64_t>(m_mem_region_cache.size())); + LLDB_LOG(log, "reusing {0} cached memory region entries", + m_mem_region_cache.size()); return Error(); } @@ -1773,10 +1590,8 @@ Error NativeProcessLinux::PopulateMemory info, FileSpec(info.GetName().GetCString(), true)); return true; } else { - if (log) - log->Printf("NativeProcessLinux::%s failed to parse proc maps " - "line '%s': %s", - __FUNCTION__, line.c_str(), parse_error.AsCString()); + LLDB_LOG(log, "failed to parse proc maps line '{0}': {1}", line, + parse_error); return false; } }); @@ -1789,21 +1604,16 @@ Error NativeProcessLinux::PopulateMemory // No entries after attempting to read them. This shouldn't happen if // /proc/{pid}/maps is supported. Assume we don't support map entries // via procfs. - if (log) - log->Printf("NativeProcessLinux::%s failed to find any procfs maps " - "entries, assuming no support for memory region metadata " - "retrieval", - __FUNCTION__); + LLDB_LOG(log, + "failed to find any procfs maps entries, assuming no support " + "for memory region metadata retrieval"); m_supports_mem_region = LazyBool::eLazyBoolNo; error.SetErrorString("not supported"); return error; } - if (log) - log->Printf("NativeProcessLinux::%s read %" PRIu64 - " memory region entries from /proc/%" PRIu64 "/maps", - __FUNCTION__, static_cast<uint64_t>(m_mem_region_cache.size()), - GetID()); + LLDB_LOG(log, "read {0} memory region entries from /proc/{1}/maps", + m_mem_region_cache.size(), GetID()); // We support memory retrieval, remember that. m_supports_mem_region = LazyBool::eLazyBoolYes; @@ -1811,15 +1621,10 @@ Error NativeProcessLinux::PopulateMemory } void NativeProcessLinux::DoStopIDBumped(uint32_t newBumpId) { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); - if (log) - log->Printf("NativeProcessLinux::%s(newBumpId=%" PRIu32 ") called", - __FUNCTION__, newBumpId); - - if (log) - log->Printf("NativeProcessLinux::%s clearing %" PRIu64 - " entries from the cache", - __FUNCTION__, static_cast<uint64_t>(m_mem_region_cache.size())); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); + LLDB_LOG(log, "newBumpId={0}", newBumpId); + LLDB_LOG(log, "clearing {0} entries from memory region cache", + m_mem_region_cache.size()); m_mem_region_cache.clear(); } @@ -2154,40 +1959,30 @@ Error NativeProcessLinux::ReadMemory(lld bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0); const bool success = bytes_read == size; - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); - if (log) - log->Printf("NativeProcessLinux::%s using process_vm_readv to read %zd " - "bytes from inferior address 0x%" PRIx64 ": %s", - __FUNCTION__, size, addr, - success ? "Success" : strerror(errno)); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); + LLDB_LOG(log, + "using process_vm_readv to read {0} bytes from inferior " + "address {1:x}: {2}", + size, addr, success ? "Success" : strerror(errno)); if (success) return Error(); - // else - // the call failed for some reason, let's retry the read using ptrace - // api. + // else the call failed for some reason, let's retry the read using ptrace + // api. } unsigned char *dst = static_cast<unsigned char *>(buf); size_t remainder; long data; - Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_ALL)); - if (log) - ProcessPOSIXLog::IncNestLevel(); - if (log && ProcessPOSIXLog::AtTopNestLevel() && - log->GetMask().Test(POSIX_LOG_MEMORY)) - log->Printf("NativeProcessLinux::%s(%p, %p, %zd, _)", __FUNCTION__, - (void *)addr, buf, size); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); + LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); for (bytes_read = 0; bytes_read < size; bytes_read += remainder) { Error error = NativeProcessLinux::PtraceWrapper( PTRACE_PEEKDATA, GetID(), (void *)addr, nullptr, 0, &data); - if (error.Fail()) { - if (log) - ProcessPOSIXLog::DecNestLevel(); + if (error.Fail()) return error; - } remainder = size - bytes_read; remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; @@ -2195,24 +1990,10 @@ Error NativeProcessLinux::ReadMemory(lld // Copy the data into our buffer memcpy(dst, &data, remainder); - if (log && ProcessPOSIXLog::AtTopNestLevel() && - (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || - (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && - size <= POSIX_LOG_MEMORY_SHORT_BYTES))) { - uintptr_t print_dst = 0; - // Format bytes from data by moving into print_dst for log output - for (unsigned i = 0; i < remainder; ++i) - print_dst |= (((data >> i * 8) & 0xFF) << i * 8); - log->Printf("NativeProcessLinux::%s() [0x%" PRIx64 "]:0x%" PRIx64 - " (0x%" PRIx64 ")", - __FUNCTION__, addr, uint64_t(print_dst), uint64_t(data)); - } + LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); addr += k_ptrace_word_size; dst += k_ptrace_word_size; } - - if (log) - ProcessPOSIXLog::DecNestLevel(); return Error(); } @@ -2231,13 +2012,8 @@ Error NativeProcessLinux::WriteMemory(ll size_t remainder; Error error; - Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_ALL)); - if (log) - ProcessPOSIXLog::IncNestLevel(); - if (log && ProcessPOSIXLog::AtTopNestLevel() && - log->GetMask().Test(POSIX_LOG_MEMORY)) - log->Printf("NativeProcessLinux::%s(0x%" PRIx64 ", %p, %zu)", __FUNCTION__, - addr, buf, size); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); + LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); for (bytes_written = 0; bytes_written < size; bytes_written += remainder) { remainder = size - bytes_written; @@ -2247,54 +2023,32 @@ Error NativeProcessLinux::WriteMemory(ll unsigned long data = 0; memcpy(&data, src, k_ptrace_word_size); - if (log && ProcessPOSIXLog::AtTopNestLevel() && - (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || - (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && - size <= POSIX_LOG_MEMORY_SHORT_BYTES))) - log->Printf("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, - (void *)addr, *(const unsigned long *)src, data); - + LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); error = NativeProcessLinux::PtraceWrapper(PTRACE_POKEDATA, GetID(), (void *)addr, (void *)data); - if (error.Fail()) { - if (log) - ProcessPOSIXLog::DecNestLevel(); + if (error.Fail()) return error; - } } else { unsigned char buff[8]; size_t bytes_read; error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read); - if (error.Fail()) { - if (log) - ProcessPOSIXLog::DecNestLevel(); + if (error.Fail()) return error; - } memcpy(buff, src, remainder); size_t bytes_written_rec; error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec); - if (error.Fail()) { - if (log) - ProcessPOSIXLog::DecNestLevel(); + if (error.Fail()) return error; - } - if (log && ProcessPOSIXLog::AtTopNestLevel() && - (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || - (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && - size <= POSIX_LOG_MEMORY_SHORT_BYTES))) - log->Printf("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, - (void *)addr, *(const unsigned long *)src, - *(unsigned long *)buff); + LLDB_LOG(log, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long *)src, + *(unsigned long *)buff); } addr += k_ptrace_word_size; src += k_ptrace_word_size; } - if (log) - ProcessPOSIXLog::DecNestLevel(); return error; } @@ -2328,14 +2082,10 @@ bool NativeProcessLinux::HasThreadNoLock } bool NativeProcessLinux::StopTrackingThread(lldb::tid_t thread_id) { - Log *const log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD); - - if (log) - log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, - thread_id); + Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); + LLDB_LOG(log, "tid: {0})", thread_id); bool found = false; - for (auto it = m_threads.begin(); it != m_threads.end(); ++it) { if (*it && ((*it)->GetID() == thread_id)) { m_threads.erase(it); @@ -2345,18 +2095,12 @@ bool NativeProcessLinux::StopTrackingThr } SignalIfAllThreadsStopped(); - return found; } NativeThreadLinuxSP NativeProcessLinux::AddThread(lldb::tid_t thread_id) { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); - - if (log) { - log->Printf("NativeProcessLinux::%s pid %" PRIu64 - " adding thread with tid %" PRIu64, - __FUNCTION__, GetID(), thread_id); - } + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); + LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); assert(!HasThreadNoLock(thread_id) && "attempted to add a thread by id that already exists"); @@ -2371,7 +2115,7 @@ NativeThreadLinuxSP NativeProcessLinux:: } Error NativeProcessLinux::FixupBreakpointPCAsNeeded(NativeThreadLinux &thread) { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); Error error; @@ -2380,24 +2124,17 @@ Error NativeProcessLinux::FixupBreakpoin NativeRegisterContextSP context_sp = thread.GetRegisterContext(); if (!context_sp) { error.SetErrorString("cannot get a NativeRegisterContext for the thread"); - if (log) - log->Printf("NativeProcessLinux::%s failed: %s", __FUNCTION__, - error.AsCString()); + LLDB_LOG(log, "failed: {0}", error); return error; } uint32_t breakpoint_size = 0; error = GetSoftwareBreakpointPCOffset(breakpoint_size); if (error.Fail()) { - if (log) - log->Printf("NativeProcessLinux::%s GetBreakpointSize() failed: %s", - __FUNCTION__, error.AsCString()); + LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error); return error; - } else { - if (log) - log->Printf("NativeProcessLinux::%s breakpoint size: %" PRIu32, - __FUNCTION__, breakpoint_size); - } + } else + LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); // First try probing for a breakpoint at a software breakpoint location: PC - // breakpoint size. @@ -2415,21 +2152,19 @@ Error NativeProcessLinux::FixupBreakpoin error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); if (!error.Success() || !breakpoint_sp) { // We didn't find one at a software probe location. Nothing to do. - if (log) - log->Printf( - "NativeProcessLinux::%s pid %" PRIu64 - " no lldb breakpoint found at current pc with adjustment: 0x%" PRIx64, - __FUNCTION__, GetID(), breakpoint_addr); + LLDB_LOG(log, + "pid {0} no lldb breakpoint found at current pc with " + "adjustment: {1}", + GetID(), breakpoint_addr); return Error(); } // If the breakpoint is not a software breakpoint, nothing to do. if (!breakpoint_sp->IsSoftwareBreakpoint()) { - if (log) - log->Printf("NativeProcessLinux::%s pid %" PRIu64 - " breakpoint found at 0x%" PRIx64 - ", not software, nothing to adjust", - __FUNCTION__, GetID(), breakpoint_addr); + LLDB_LOG( + log, + "pid {0} breakpoint found at {1:x}, not software, nothing to adjust", + GetID(), breakpoint_addr); return Error(); } @@ -2440,28 +2175,21 @@ Error NativeProcessLinux::FixupBreakpoin // Sanity check. if (breakpoint_size == 0) { // Nothing to do! How did we get here? - if (log) - log->Printf( - "NativeProcessLinux::%s pid %" PRIu64 - " breakpoint found at 0x%" PRIx64 - ", it is software, but the size is zero, nothing to do (unexpected)", - __FUNCTION__, GetID(), breakpoint_addr); + LLDB_LOG(log, + "pid {0} breakpoint found at {1:x}, it is software, but the " + "size is zero, nothing to do (unexpected)", + GetID(), breakpoint_addr); return Error(); } // Change the program counter. - if (log) - log->Printf("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 - ": changing PC from 0x%" PRIx64 " to 0x%" PRIx64, - __FUNCTION__, GetID(), thread.GetID(), initial_pc_addr, - breakpoint_addr); + LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), + thread.GetID(), initial_pc_addr, breakpoint_addr); error = context_sp->SetPC(breakpoint_addr); if (error.Fail()) { - if (log) - log->Printf("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 - ": failed to set PC: %s", - __FUNCTION__, GetID(), thread.GetID(), error.AsCString()); + LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), + thread.GetID(), error); return error; } @@ -2511,11 +2239,8 @@ NativeThreadLinuxSP NativeProcessLinux:: Error NativeProcessLinux::ResumeThread(NativeThreadLinux &thread, lldb::StateType state, int signo) { - Log *const log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD); - - if (log) - log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, - thread.GetID()); + Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); + LLDB_LOG(log, "tid: {0}", thread.GetID()); // Before we do the resume below, first check if we have a pending // stop notification that is currently waiting for @@ -2523,12 +2248,12 @@ Error NativeProcessLinux::ResumeThread(N // we're ostensibly waiting for threads to stop before we send out the // pending notification, and here we are resuming one before we send // out the pending stop notification. - if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && log) { - log->Printf("NativeProcessLinux::%s about to resume tid %" PRIu64 - " per explicit request but we have a pending stop notification " - "(tid %" PRIu64 ") that is actively waiting for this thread to " - "stop. Valid sequence of events?", - __FUNCTION__, thread.GetID(), m_pending_notification_tid); + if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { + LLDB_LOG(log, + "about to resume tid {0} per explicit request but we have a " + "pending stop notification (tid {1}) that is actively " + "waiting for this thread to stop. Valid sequence of events?", + thread.GetID(), m_pending_notification_tid); } // Request a resume. We expect this to be synchronous and the system @@ -2547,9 +2272,7 @@ Error NativeProcessLinux::ResumeThread(N return step_result; } default: - if (log) - log->Printf("NativeProcessLinux::%s Unhandled state %s.", __FUNCTION__, - StateAsCString(state)); + LLDB_LOG(log, "Unhandled state {0}.", StateAsCString(state)); llvm_unreachable("Unhandled state for resume"); } } @@ -2557,13 +2280,9 @@ Error NativeProcessLinux::ResumeThread(N //===----------------------------------------------------------------------===// void NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) { - Log *const log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD); - - if (log) { - log->Printf("NativeProcessLinux::%s about to process event: " - "(triggering_tid: %" PRIu64 ")", - __FUNCTION__, triggering_tid); - } + Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); + LLDB_LOG(log, "about to process event: (triggering_tid: {0})", + triggering_tid); m_pending_notification_tid = triggering_tid; @@ -2575,10 +2294,7 @@ void NativeProcessLinux::StopRunningThre } SignalIfAllThreadsStopped(); - - if (log) { - log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__); - } + LLDB_LOG(log, "event processing done"); } void NativeProcessLinux::SignalIfAllThreadsStopped() { @@ -2599,10 +2315,8 @@ void NativeProcessLinux::SignalIfAllThre for (const auto &thread_info : m_threads_stepping_with_breakpoint) { Error error = RemoveBreakpoint(thread_info.second); if (error.Fail()) - if (log) - log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 - " remove stepping breakpoint: %s", - __FUNCTION__, thread_info.first, error.AsCString()); + LLDB_LOG(log, "pid = {0} remove stepping breakpoint: {1}", + thread_info.first, error); } m_threads_stepping_with_breakpoint.clear(); @@ -2613,11 +2327,8 @@ void NativeProcessLinux::SignalIfAllThre } void NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) { - Log *const log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD); - - if (log) - log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, - thread.GetID()); + Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); + LLDB_LOG(log, "tid: {0}", thread.GetID()); if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && StateIsRunningState(thread.GetState())) { @@ -2629,7 +2340,7 @@ void NativeProcessLinux::ThreadWasCreate } void NativeProcessLinux::SigchldHandler() { - Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); + Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); // Process all pending waitpid notifications. while (true) { int status = -1; @@ -2643,10 +2354,7 @@ void NativeProcessLinux::SigchldHandler( continue; Error error(errno, eErrorTypePOSIX); - if (log) - log->Printf("NativeProcessLinux::%s waitpid (-1, &status, __WALL | " - "__WNOTHREAD | WNOHANG) failed: %s", - __FUNCTION__, error.AsCString()); + LLDB_LOG(log, "waitpid (-1, &status, _) failed: {0}", error); break; } @@ -2671,13 +2379,10 @@ void NativeProcessLinux::SigchldHandler( } else status_cstr = "(\?\?\?)"; - if (log) - log->Printf("NativeProcessLinux::%s: waitpid (-1, &status, __WALL | " - "__WNOTHREAD | WNOHANG)" - "=> pid = %" PRIi32 - ", status = 0x%8.8x (%s), signal = %i, exit_state = %i", - __FUNCTION__, wait_pid, status, status_cstr, signal, - exit_status); + LLDB_LOG(log, + "waitpid (-1, &status, _) => pid = {0}, status = {1:x} " + "({2}), signal = {3}, exit_state = {4}", + wait_pid, status, status_cstr, signal, exit_status); MonitorCallback(wait_pid, exited, signal, exit_status); } @@ -2710,32 +2415,13 @@ Error NativeProcessLinux::PtraceWrapper( if (result) *result = ret; - if (log) - log->Printf("ptrace(%d, %" PRIu64 ", %p, %p, %zu)=%lX", req, pid, addr, - data, data_size, ret); + LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3}, {4}, {5})={6:x}", req, pid, addr, + data, data_size, ret); PtraceDisplayBytes(req, data, data_size); - if (log && error.GetError() != 0) { - const char *str; - switch (error.GetError()) { - case ESRCH: - str = "ESRCH"; - break; - case EINVAL: - str = "EINVAL"; - break; - case EBUSY: - str = "EBUSY"; - break; - case EPERM: - str = "EPERM"; - break; - default: - str = error.AsCString(); - } - log->Printf("ptrace() failed; errno=%d (%s)", error.GetError(), str); - } + if (error.Fail()) + LLDB_LOG(log, "ptrace() failed: {0}", error); return error; } _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits