xref: /NextBSD/contrib/llvm/tools/lldb/source/Host/common/ThisThread.cpp (revision 287e3b14e9552995def1802ec9c5034f4adf28ec)
1 //===-- ThisThread.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/Error.h"
11 #include "lldb/Host/HostInfo.h"
12 #include "lldb/Host/ThisThread.h"
13 
14 #include "llvm/ADT/STLExtras.h"
15 
16 #include <algorithm>
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 void
SetName(llvm::StringRef name,int max_length)22 ThisThread::SetName(llvm::StringRef name, int max_length)
23 {
24     std::string truncated_name(name.data());
25 
26     // Thread names are coming in like '<lldb.comm.debugger.edit>' and
27     // '<lldb.comm.debugger.editline>'.  So just chopping the end of the string
28     // off leads to a lot of similar named threads.  Go through the thread name
29     // and search for the last dot and use that.
30 
31     if (max_length > 0 && truncated_name.length() > static_cast<size_t>(max_length))
32     {
33         // First see if we can get lucky by removing any initial or final braces.
34         std::string::size_type begin = truncated_name.find_first_not_of("(<");
35         std::string::size_type end = truncated_name.find_last_not_of(")>.");
36         if (end - begin > static_cast<size_t>(max_length))
37         {
38             // We're still too long.  Since this is a dotted component, use everything after the last
39             // dot, up to a maximum of |length| characters.
40             std::string::size_type last_dot = truncated_name.find_last_of(".");
41             if (last_dot != std::string::npos)
42                 begin = last_dot + 1;
43 
44             end = std::min(end, begin + max_length);
45         }
46 
47         std::string::size_type count = end - begin + 1;
48         truncated_name = truncated_name.substr(begin, count);
49     }
50 
51     SetName(truncated_name.c_str());
52 }
53