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 "lldb/Utility/LLDBLog.h"
10 #include "llvm/Support/Signposts.h"
11 
12 #include <cstdio>
13 #include <cstdlib>
14 #include <limits>
15 #include <thread>
16 
17 using namespace lldb_private;
18 using namespace lldb_private::instrumentation;
19 
20 // Whether we're currently across the API boundary.
21 static thread_local bool g_global_boundary = false;
22 
23 // Instrument SB API calls with singposts when supported.
24 static llvm::ManagedStatic<llvm::SignpostEmitter> g_api_signposts;
25 
Instrumenter(llvm::StringRef pretty_func,std::string && pretty_args)26 Instrumenter::Instrumenter(llvm::StringRef pretty_func,
27                            std::string &&pretty_args)
28     : m_pretty_func(pretty_func) {
29   if (!g_global_boundary) {
30     g_global_boundary = true;
31     m_local_boundary = true;
32     g_api_signposts->startInterval(this, m_pretty_func);
33   }
34   LLDB_LOG(GetLog(LLDBLog::API), "[{0}] {1} ({2})",
35            m_local_boundary ? "external" : "internal", m_pretty_func,
36            pretty_args);
37 }
38 
~Instrumenter()39 Instrumenter::~Instrumenter() {
40   if (m_local_boundary) {
41     g_global_boundary = false;
42     g_api_signposts->endInterval(this, m_pretty_func);
43   }
44 }
45