location.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_LOCATION_H_
  5. #define BASE_LOCATION_H_
  6. #include <string>
  7. #include "base/base_export.h"
  8. #include "base/debug/debugging_buildflags.h"
  9. #include "base/memory/raw_ptr_exclusion.h"
  10. #include "base/trace_event/base_tracing_forward.h"
  11. #include "build/build_config.h"
  12. namespace base {
  13. #if defined(__clang__)
  14. // Clang allows detection of these builtins.
  15. #define SUPPORTS_LOCATION_BUILTINS \
  16. (__has_builtin(__builtin_FUNCTION) && __has_builtin(__builtin_FILE) && \
  17. __has_builtin(__builtin_LINE))
  18. #elif defined(COMPILER_GCC) && __GNUC__ >= 7
  19. // GCC has supported these for a long time, but they point at the function
  20. // declaration in the case of default arguments, rather than at the call site.
  21. #define SUPPORTS_LOCATION_BUILTINS 1
  22. #else
  23. #define SUPPORTS_LOCATION_BUILTINS 0
  24. #endif
  25. // Location provides basic info where of an object was constructed, or was
  26. // significantly brought to life.
  27. class BASE_EXPORT Location {
  28. public:
  29. Location();
  30. Location(const Location& other);
  31. Location(Location&& other) noexcept;
  32. Location& operator=(const Location& other);
  33. // Only initializes the file name and program counter, the source information
  34. // will be null for the strings, and -1 for the line number.
  35. // TODO(http://crbug.com/760702) remove file name from this constructor.
  36. Location(const char* file_name, const void* program_counter);
  37. // Constructor should be called with a long-lived char*, such as __FILE__.
  38. // It assumes the provided value will persist as a global constant, and it
  39. // will not make a copy of it.
  40. Location(const char* function_name,
  41. const char* file_name,
  42. int line_number,
  43. const void* program_counter);
  44. // Comparator for testing. The program counter should uniquely
  45. // identify a location.
  46. bool operator==(const Location& other) const {
  47. return program_counter_ == other.program_counter_;
  48. }
  49. // Comparator is necessary to use location object within an ordered container
  50. // type (eg. std::map).
  51. bool operator<(const Location& other) const {
  52. return program_counter_ < other.program_counter_;
  53. }
  54. // Returns true if there is source code location info. If this is false,
  55. // the Location object only contains a program counter or is
  56. // default-initialized (the program counter is also null).
  57. bool has_source_info() const { return function_name_ && file_name_; }
  58. // Will be nullptr for default initialized Location objects and when source
  59. // names are disabled.
  60. const char* function_name() const { return function_name_; }
  61. // Will be nullptr for default initialized Location objects and when source
  62. // names are disabled.
  63. const char* file_name() const { return file_name_; }
  64. // Will be -1 for default initialized Location objects and when source names
  65. // are disabled.
  66. int line_number() const { return line_number_; }
  67. // The address of the code generating this Location object. Should always be
  68. // valid except for default initialized Location objects, which will be
  69. // nullptr.
  70. const void* program_counter() const { return program_counter_; }
  71. // Converts to the most user-readable form possible. If function and filename
  72. // are not available, this will return "pc:<hex address>".
  73. std::string ToString() const;
  74. // Write a representation of this object into a trace.
  75. void WriteIntoTrace(perfetto::TracedValue context) const;
  76. #if !BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS)
  77. #if !BUILDFLAG(ENABLE_LOCATION_SOURCE)
  78. static Location CreateFromHere(const char* file_name);
  79. #else
  80. static Location CreateFromHere(const char* function_name,
  81. const char* file_name,
  82. int line_number);
  83. #endif
  84. #endif
  85. #if SUPPORTS_LOCATION_BUILTINS && BUILDFLAG(ENABLE_LOCATION_SOURCE)
  86. static Location Current(const char* function_name = __builtin_FUNCTION(),
  87. const char* file_name = __builtin_FILE(),
  88. int line_number = __builtin_LINE());
  89. #elif SUPPORTS_LOCATION_BUILTINS
  90. static Location Current(const char* file_name = __builtin_FILE());
  91. #else
  92. static Location Current();
  93. #endif
  94. private:
  95. const char* function_name_ = nullptr;
  96. const char* file_name_ = nullptr;
  97. int line_number_ = -1;
  98. // `program_counter_` is not a raw_ptr<...> for performance reasons (based on
  99. // analysis of sampling profiler data and tab_search:top100:2020).
  100. RAW_PTR_EXCLUSION const void* program_counter_ = nullptr;
  101. };
  102. BASE_EXPORT const void* GetProgramCounter();
  103. #if BUILDFLAG(FROM_HERE_USES_LOCATION_BUILTINS)
  104. #define FROM_HERE ::base::Location::Current()
  105. // The macros defined here will expand to the current function.
  106. #elif BUILDFLAG(ENABLE_LOCATION_SOURCE)
  107. // Full source information should be included.
  108. #define FROM_HERE ::base::Location::CreateFromHere(__func__, __FILE__, __LINE__)
  109. #else
  110. // TODO(http://crbug.com/760702) remove the __FILE__ argument from these calls.
  111. #define FROM_HERE ::base::Location::CreateFromHere(__FILE__)
  112. #endif
  113. } // namespace base
  114. #endif // BASE_LOCATION_H_