From 17c4bc477e3007db9d34f6f4947543f8ad946717 Mon Sep 17 00:00:00 2001 From: Michael Sartain Date: Wed, 31 Jul 2013 23:19:14 +0000 Subject: Optimize Host::GetThreadName() to read from /proc/$TID per Matt's suggestion. CR: mkopec git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@187542 91177308-0d34-0410-b5e6-96231b3b80d8 --- source/Host/linux/Host.cpp | 37 ++++++++++--------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/source/Host/linux/Host.cpp b/source/Host/linux/Host.cpp index cdcff1f3d..c7120e2b7 100644 --- a/source/Host/linux/Host.cpp +++ b/source/Host/linux/Host.cpp @@ -105,13 +105,6 @@ ReadProcPseudoFile (lldb::pid_t pid, const char *name) return buf_sp; } -lldb::DataBufferSP -ReadProcPseudoFile (lldb::pid_t pid, lldb::tid_t tid, const char *name) -{ - std::string process_thread_pseudo_file = "task/" + std::to_string(tid) + "/" + name; - return ReadProcPseudoFile(pid, process_thread_pseudo_file.c_str()); -} - } // anonymous namespace static bool @@ -458,28 +451,18 @@ Host::ThreadCreated (const char *thread_name) std::string Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid) { - const size_t thread_name_max_size = 16; - char pthread_name[thread_name_max_size]; - std::string thread_name; - // Read the /proc/$PID/stat file. - lldb::DataBufferSP buf_sp = ReadProcPseudoFile (pid, tid, "stat"); - - // The file/thread name of the executable is stored in parenthesis. Search for the first - // '(' and last ')' and copy everything in between. - const char *filename_start = ::strchr ((const char *)buf_sp->GetBytes(), '('); - const char *filename_end = ::strrchr ((const char *)buf_sp->GetBytes(), ')'); - - if (filename_start && filename_end) + // Read /proc/$TID/comm file. + lldb::DataBufferSP buf_sp = ReadProcPseudoFile (tid, "comm"); + if (buf_sp->GetByteSize()) { - ++filename_start; - size_t length = filename_end - filename_start; - if (length > thread_name_max_size) - length = thread_name_max_size; - strncpy(pthread_name, filename_start, length); - thread_name = std::string(pthread_name, length); - } + const char *comm_str = (const char *)buf_sp->GetBytes(); + const char *cr_str = ::strchr(comm_str, '\n'); + size_t length = cr_str ? (cr_str - comm_str) : buf_sp->GetByteSize(); - return thread_name; + std::string thread_name(comm_str, length); + return thread_name; + } + return std::string(""); } void -- cgit v1.2.3