Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 1 | //===-- HostThreadLinux.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 | #include "lldb/Core/DataBuffer.h" |
| 11 | #include "lldb/Host/linux/HostThreadLinux.h" |
| 12 | #include "Plugins/Process/Linux/ProcFileReader.h" |
| 13 | |
| 14 | #include "llvm/ADT/SmallVector.h" |
| 15 | |
| 16 | #include <pthread.h> |
| 17 | |
| 18 | using namespace lldb_private; |
| 19 | |
| 20 | HostThreadLinux::HostThreadLinux() |
| 21 | : HostThreadPosix() |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | HostThreadLinux::HostThreadLinux(lldb::thread_t thread) |
| 26 | : HostThreadPosix(thread) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | void |
| 31 | HostThreadLinux::SetName(lldb::thread_t thread, llvm::StringRef name) |
| 32 | { |
Vasileios Kalintiris | 747372b | 2015-09-22 14:52:31 +0000 | [diff] [blame] | 33 | #if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__) |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 34 | ::pthread_setname_np(thread, name.data()); |
Vasileios Kalintiris | 747372b | 2015-09-22 14:52:31 +0000 | [diff] [blame] | 35 | #else |
| 36 | (void) thread; |
| 37 | (void) name; |
| 38 | #endif |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void |
| 42 | HostThreadLinux::GetName(lldb::thread_t thread, llvm::SmallVectorImpl<char> &name) |
| 43 | { |
| 44 | // Read /proc/$TID/comm file. |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 45 | lldb::DataBufferSP buf_sp = process_linux::ProcFileReader::ReadIntoDataBuffer(thread, "comm"); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 46 | const char *comm_str = (const char *)buf_sp->GetBytes(); |
| 47 | const char *cr_str = ::strchr(comm_str, '\n'); |
| 48 | size_t length = cr_str ? (cr_str - comm_str) : strlen(comm_str); |
| 49 | |
| 50 | name.clear(); |
| 51 | name.append(comm_str, comm_str + length); |
| 52 | } |