blob: 2312ced0107b893d7462bf2f4e1b4233369cc164 [file] [log] [blame]
Zachary Turner39de3112014-09-09 20:54:56 +00001//===-- 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
18using namespace lldb_private;
19
20HostThreadLinux::HostThreadLinux()
21 : HostThreadPosix()
22{
23}
24
25HostThreadLinux::HostThreadLinux(lldb::thread_t thread)
26 : HostThreadPosix(thread)
27{
28}
29
30void
31HostThreadLinux::SetName(lldb::thread_t thread, llvm::StringRef name)
32{
Vasileios Kalintiris747372b2015-09-22 14:52:31 +000033#if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
Zachary Turner39de3112014-09-09 20:54:56 +000034 ::pthread_setname_np(thread, name.data());
Vasileios Kalintiris747372b2015-09-22 14:52:31 +000035#else
36 (void) thread;
37 (void) name;
38#endif
Zachary Turner39de3112014-09-09 20:54:56 +000039}
40
41void
42HostThreadLinux::GetName(lldb::thread_t thread, llvm::SmallVectorImpl<char> &name)
43{
44 // Read /proc/$TID/comm file.
Tamas Berghammerdb264a62015-03-31 09:52:22 +000045 lldb::DataBufferSP buf_sp = process_linux::ProcFileReader::ReadIntoDataBuffer(thread, "comm");
Zachary Turner39de3112014-09-09 20:54:56 +000046 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}