xref: /freebsd-src/contrib/llvm-project/lldb/source/Utility/Instrumentation.cpp (revision ed549cb0c53f8438c52593ce811f6fcc812248e9)
1 //===-- Instrumentation.cpp -----------------------------------------------===//
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7 
8 #include "lldb/Utility/Instrumentation.h"
9 #include "llvm/Support/Signposts.h"
10 
11 #include <cstdio>
12 #include <cstdlib>
13 #include <limits>
14 #include <thread>
15 
16 using namespace lldb_private;
17 using namespace lldb_private::instrumentation;
18 
19 // Whether we're currently across the API boundary.
20 static thread_local bool g_global_boundary = false;
21 
22 // Instrument SB API calls with singposts when supported.
23 static llvm::ManagedStatic<llvm::SignpostEmitter> g_api_signposts;
24 
25 Instrumenter::Instrumenter(llvm::StringRef pretty_func,
26                            std::string &&pretty_args)
27     : m_pretty_func(pretty_func), m_local_boundary(false) {
28   if (!g_global_boundary) {
29     g_global_boundary = true;
30     m_local_boundary = true;
31     g_api_signposts->startInterval(this, m_pretty_func);
32   }
33   LLDB_LOG(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API), "[{0}] {1} ({2})",
34            m_local_boundary ? "external" : "internal", m_pretty_func,
35            pretty_args);
36 }
37 
38 Instrumenter::~Instrumenter() {
39   if (m_local_boundary) {
40     g_global_boundary = false;
41     g_api_signposts->endInterval(this, m_pretty_func);
42   }
43 }
44