[email protected] | ec6bf33 | 2012-03-16 21:43:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_LOCATION_H_ |
| 6 | #define BASE_LOCATION_H_ |
| 7 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
sque | e3b16f1 | 2015-03-14 02:39:03 | [diff] [blame] | 10 | #include <cassert> |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 11 | #include <string> |
| 12 | |
| 13 | #include "base/base_export.h" |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 14 | #include "base/debug/debugging_flags.h" |
davidben | 411d3f7 | 2016-01-22 01:41:41 | [diff] [blame] | 15 | #include "base/hash.h" |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 16 | |
Brett Wilson | abbb960 | 2017-09-11 23:26:39 | [diff] [blame] | 17 | namespace base { |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 18 | |
| 19 | // Location provides basic info where of an object was constructed, or was |
| 20 | // significantly brought to life. |
| 21 | class BASE_EXPORT Location { |
| 22 | public: |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 23 | Location(); |
| 24 | Location(const Location& other); |
| 25 | |
| 26 | // Only initializes the file name and program counter, the source information |
| 27 | // will be null for the strings, and -1 for the line number. |
| 28 | // TODO(http://crbug.com/760702) remove file name from this constructor. |
| 29 | Location(const char* file_name, const void* program_counter); |
| 30 | |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 31 | // Constructor should be called with a long-lived char*, such as __FILE__. |
| 32 | // It assumes the provided value will persist as a global constant, and it |
| 33 | // will not make a copy of it. |
| 34 | Location(const char* function_name, |
| 35 | const char* file_name, |
| 36 | int line_number, |
| 37 | const void* program_counter); |
| 38 | |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 39 | // Comparator for hash map insertion. The program counter should uniquely |
| 40 | // identify a location. |
sque | e3b16f1 | 2015-03-14 02:39:03 | [diff] [blame] | 41 | bool operator==(const Location& other) const { |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 42 | return program_counter_ == other.program_counter_; |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 43 | } |
| 44 | |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 45 | // Returns true if there is source code location info. If this is false, |
| 46 | // the Location object only contains a program counter or is |
| 47 | // default-initialized (the program counter is also null). |
| 48 | bool has_source_info() const { return function_name_ && file_name_; } |
| 49 | |
| 50 | // Will be nullptr for default initialized Location objects and when source |
| 51 | // names are disabled. |
| 52 | const char* function_name() const { return function_name_; } |
| 53 | |
| 54 | // Will be nullptr for default initialized Location objects and when source |
| 55 | // names are disabled. |
| 56 | const char* file_name() const { return file_name_; } |
| 57 | |
| 58 | // Will be -1 for default initialized Location objects and when source names |
| 59 | // are disabled. |
| 60 | int line_number() const { return line_number_; } |
| 61 | |
| 62 | // The address of the code generating this Location object. Should always be |
| 63 | // valid except for default initialized Location objects, which will be |
| 64 | // nullptr. |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 65 | const void* program_counter() const { return program_counter_; } |
| 66 | |
Brett Wilson | 07ec3b5 | 2017-10-09 21:42:45 | [diff] [blame^] | 67 | // Converts to the most user-readable form possible. If function and filename |
| 68 | // are not available, this will return "pc:<hex address>". |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 69 | std::string ToString() const; |
| 70 | |
tzik | 4f05a25a | 2017-09-13 00:23:16 | [diff] [blame] | 71 | static Location CreateFromHere(const char* file_name); |
| 72 | static Location CreateFromHere(const char* function_name, |
| 73 | const char* file_name, |
| 74 | int line_number); |
| 75 | |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 76 | private: |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 77 | const char* function_name_ = nullptr; |
| 78 | const char* file_name_ = nullptr; |
| 79 | int line_number_ = -1; |
| 80 | const void* program_counter_ = nullptr; |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 81 | }; |
| 82 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 83 | // A "snapshotted" representation of the Location class that can safely be |
| 84 | // passed across process boundaries. |
| 85 | struct BASE_EXPORT LocationSnapshot { |
| 86 | // The default constructor is exposed to support the IPC serialization macros. |
| 87 | LocationSnapshot(); |
Brett Wilson | abbb960 | 2017-09-11 23:26:39 | [diff] [blame] | 88 | explicit LocationSnapshot(const Location& location); |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 89 | ~LocationSnapshot(); |
| 90 | |
| 91 | std::string file_name; |
| 92 | std::string function_name; |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 93 | int line_number = -1; |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 94 | }; |
| 95 | |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 96 | BASE_EXPORT const void* GetProgramCounter(); |
| 97 | |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 98 | // The macros defined here will expand to the current function. |
| 99 | #if BUILDFLAG(ENABLE_LOCATION_SOURCE) |
Brett Wilson | 5b5b7cb | 2017-09-01 04:40:02 | [diff] [blame] | 100 | |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 101 | // Full source information should be included. |
| 102 | #define FROM_HERE FROM_HERE_WITH_EXPLICIT_FUNCTION(__func__) |
Brett Wilson | abbb960 | 2017-09-11 23:26:39 | [diff] [blame] | 103 | #define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \ |
tzik | 4f05a25a | 2017-09-13 00:23:16 | [diff] [blame] | 104 | ::base::Location::CreateFromHere(function_name, __FILE__, __LINE__) |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 105 | |
| 106 | #else |
| 107 | |
| 108 | // TODO(http://crbug.com/760702) remove the __FILE__ argument from these calls. |
tzik | 4f05a25a | 2017-09-13 00:23:16 | [diff] [blame] | 109 | #define FROM_HERE ::base::Location::CreateFromHere(__FILE__) |
Brett Wilson | abbb960 | 2017-09-11 23:26:39 | [diff] [blame] | 110 | #define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \ |
tzik | 4f05a25a | 2017-09-13 00:23:16 | [diff] [blame] | 111 | ::base::Location::CreateFromHere(function_name, __FILE__, -1) |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 112 | |
| 113 | #endif |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 114 | |
Brett Wilson | abbb960 | 2017-09-11 23:26:39 | [diff] [blame] | 115 | } // namespace base |
| 116 | |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 117 | namespace std { |
| 118 | |
| 119 | // Specialization for using Location in hash tables. |
| 120 | template <> |
Brett Wilson | abbb960 | 2017-09-11 23:26:39 | [diff] [blame] | 121 | struct hash<::base::Location> { |
| 122 | std::size_t operator()(const ::base::Location& loc) const { |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 123 | const void* program_counter = loc.program_counter(); |
Brett Wilson | 2cea5ca6 | 2017-09-01 20:23:53 | [diff] [blame] | 124 | return base::Hash(&program_counter, sizeof(void*)); |
Brett Wilson | c794cf6 | 2017-09-01 20:14:33 | [diff] [blame] | 125 | } |
| 126 | }; |
| 127 | |
| 128 | } // namespace std |
| 129 | |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 130 | #endif // BASE_LOCATION_H_ |