Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 1 | //===-- HostThreadFreeBSD.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 | // lldb Includes |
| 11 | #include "lldb/Host/freebsd/HostThreadFreeBSD.h" |
| 12 | |
| 13 | // C includes |
| 14 | #include <pthread.h> |
| 15 | #include <pthread_np.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <sys/sysctl.h> |
| 18 | |
| 19 | // C++ includes |
| 20 | #include <string> |
| 21 | |
| 22 | using namespace lldb_private; |
| 23 | |
| 24 | HostThreadFreeBSD::HostThreadFreeBSD() |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | HostThreadFreeBSD::HostThreadFreeBSD(lldb::thread_t thread) |
| 29 | : HostThreadPosix(thread) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | void |
| 34 | HostThreadFreeBSD::SetName(lldb::thread_t thread, llvm::StringRef name) |
| 35 | { |
| 36 | ::pthread_set_name_np(thread, name); |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | HostThreadFreeBSD::GetName(lldb::thread_t thread, llvm::SmallVectorImpl<char> &name) |
| 41 | { |
| 42 | name.clear(); |
| 43 | int pid = Host::GetCurrentProcessID(); |
| 44 | |
| 45 | struct kinfo_proc *kp = nullptr, *nkp; |
| 46 | size_t len = 0; |
| 47 | int error; |
| 48 | int ctl[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD, (int)pid}; |
| 49 | |
| 50 | while (1) |
| 51 | { |
| 52 | error = sysctl(ctl, 4, kp, &len, nullptr, 0); |
| 53 | if (kp == nullptr || (error != 0 && errno == ENOMEM)) |
| 54 | { |
| 55 | // Add extra space in case threads are added before next call. |
| 56 | len += sizeof(*kp) + len / 10; |
| 57 | nkp = (struct kinfo_proc *)realloc(kp, len); |
| 58 | if (nkp == nullptr) |
| 59 | { |
| 60 | free(kp); |
| 61 | return; |
| 62 | } |
| 63 | kp = nkp; |
| 64 | continue; |
| 65 | } |
| 66 | if (error != 0) |
| 67 | len = 0; |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | for (size_t i = 0; i < len / sizeof(*kp); i++) |
| 72 | { |
| 73 | if (kp[i].ki_tid == (int)thread) |
| 74 | { |
| 75 | name.append(kp[i].ki_tdname, strlen(kp[i].ki_tdname)); |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | free(kp); |
| 80 | } |