Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:57

0001 //===-- Instrumentation.h ---------------------------------------*- C++ -*-===//
0002 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0003 // See https://llvm.org/LICENSE.txt for license information.
0004 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0005 //
0006 //===----------------------------------------------------------------------===//
0007 
0008 #ifndef LLDB_UTILITY_INSTRUMENTATION_H
0009 #define LLDB_UTILITY_INSTRUMENTATION_H
0010 
0011 #include "lldb/Utility/FileSpec.h"
0012 #include "lldb/Utility/Log.h"
0013 #include "llvm/ADT/DenseMap.h"
0014 #include "llvm/ADT/StringRef.h"
0015 #include "llvm/Support/ErrorHandling.h"
0016 
0017 #include <map>
0018 #include <thread>
0019 #include <type_traits>
0020 
0021 namespace lldb_private {
0022 namespace instrumentation {
0023 
0024 template <typename T, std::enable_if_t<std::is_fundamental<T>::value, int> = 0>
0025 inline void stringify_append(llvm::raw_string_ostream &ss, const T &t) {
0026   ss << t;
0027 }
0028 
0029 template <typename T, std::enable_if_t<!std::is_fundamental<T>::value, int> = 0>
0030 inline void stringify_append(llvm::raw_string_ostream &ss, const T &t) {
0031   ss << &t;
0032 }
0033 
0034 template <typename T>
0035 inline void stringify_append(llvm::raw_string_ostream &ss, T *t) {
0036   ss << reinterpret_cast<void *>(t);
0037 }
0038 
0039 template <typename T>
0040 inline void stringify_append(llvm::raw_string_ostream &ss, const T *t) {
0041   ss << reinterpret_cast<const void *>(t);
0042 }
0043 
0044 template <>
0045 inline void stringify_append<char>(llvm::raw_string_ostream &ss,
0046                                    const char *t) {
0047   ss << '\"' << t << '\"';
0048 }
0049 
0050 template <>
0051 inline void stringify_append<std::nullptr_t>(llvm::raw_string_ostream &ss,
0052                                              const std::nullptr_t &t) {
0053   ss << "\"nullptr\"";
0054 }
0055 
0056 template <typename Head>
0057 inline void stringify_helper(llvm::raw_string_ostream &ss, const Head &head) {
0058   stringify_append(ss, head);
0059 }
0060 
0061 template <typename Head, typename... Tail>
0062 inline void stringify_helper(llvm::raw_string_ostream &ss, const Head &head,
0063                              const Tail &...tail) {
0064   stringify_append(ss, head);
0065   ss << ", ";
0066   stringify_helper(ss, tail...);
0067 }
0068 
0069 template <typename... Ts> inline std::string stringify_args(const Ts &...ts) {
0070   std::string buffer;
0071   llvm::raw_string_ostream ss(buffer);
0072   stringify_helper(ss, ts...);
0073   return buffer;
0074 }
0075 
0076 /// RAII object for instrumenting LLDB API functions.
0077 class Instrumenter {
0078 public:
0079   Instrumenter(llvm::StringRef pretty_func, std::string &&pretty_args = {});
0080   ~Instrumenter();
0081 
0082 private:
0083   void UpdateBoundary();
0084 
0085   llvm::StringRef m_pretty_func;
0086 
0087   /// Whether this function call was the one crossing the API boundary.
0088   bool m_local_boundary = false;
0089 };
0090 } // namespace instrumentation
0091 } // namespace lldb_private
0092 
0093 #define LLDB_INSTRUMENT()                                                      \
0094   lldb_private::instrumentation::Instrumenter _instr(LLVM_PRETTY_FUNCTION);
0095 
0096 #define LLDB_INSTRUMENT_VA(...)                                                \
0097   lldb_private::instrumentation::Instrumenter _instr(                          \
0098       LLVM_PRETTY_FUNCTION,                                                    \
0099       lldb_private::instrumentation::stringify_args(__VA_ARGS__));
0100 
0101 #endif // LLDB_UTILITY_INSTRUMENTATION_H