]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Host/linux/HostThreadLinux.cpp
Vendor import of lldb trunk r256945:
[FreeBSD/FreeBSD.git] / source / Host / linux / HostThreadLinux.cpp
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 {
33 #if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
34     ::pthread_setname_np(thread, name.data());
35 #else
36     (void) thread;
37     (void) name;
38 #endif
39 }
40
41 void
42 HostThreadLinux::GetName(lldb::thread_t thread, llvm::SmallVectorImpl<char> &name)
43 {
44     // Read /proc/$TID/comm file.
45     lldb::DataBufferSP buf_sp = process_linux::ProcFileReader::ReadIntoDataBuffer(thread, "comm");
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 }