isherman@chromium.org | a9490ac | 2012-03-17 06:43:41 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [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 | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame^] | 8 | #include <stddef.h> |
| 9 | |
sque | 9f57c2c | 2015-03-14 11:39:03 +0900 | [diff] [blame] | 10 | #include <cassert> |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 11 | #include <string> |
| 12 | |
| 13 | #include "base/base_export.h" |
sque | 9f57c2c | 2015-03-14 11:39:03 +0900 | [diff] [blame] | 14 | #include "base/containers/hash_tables.h" |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 15 | |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 16 | namespace tracked_objects { |
| 17 | |
| 18 | // Location provides basic info where of an object was constructed, or was |
| 19 | // significantly brought to life. |
| 20 | class BASE_EXPORT Location { |
| 21 | public: |
| 22 | // Constructor should be called with a long-lived char*, such as __FILE__. |
| 23 | // It assumes the provided value will persist as a global constant, and it |
| 24 | // will not make a copy of it. |
| 25 | Location(const char* function_name, |
| 26 | const char* file_name, |
| 27 | int line_number, |
| 28 | const void* program_counter); |
| 29 | |
| 30 | // Provide a default constructor for easy of debugging. |
| 31 | Location(); |
| 32 | |
sque | 9f57c2c | 2015-03-14 11:39:03 +0900 | [diff] [blame] | 33 | // Copy constructor. |
| 34 | Location(const Location& other); |
| 35 | |
| 36 | // Comparator for hash map insertion. |
| 37 | // No need to use |function_name_| since the other two fields uniquely |
| 38 | // identify this location. |
| 39 | bool operator==(const Location& other) const { |
| 40 | return line_number_ == other.line_number_ && |
| 41 | file_name_ == other.file_name_; |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | const char* function_name() const { return function_name_; } |
| 45 | const char* file_name() const { return file_name_; } |
| 46 | int line_number() const { return line_number_; } |
| 47 | const void* program_counter() const { return program_counter_; } |
| 48 | |
| 49 | std::string ToString() const; |
| 50 | |
sque | 9f57c2c | 2015-03-14 11:39:03 +0900 | [diff] [blame] | 51 | // Hash operator for hash maps. |
| 52 | struct Hash { |
| 53 | size_t operator()(const Location& location) const { |
| 54 | // Compute the hash value using file name pointer and line number. |
| 55 | // No need to use |function_name_| since the other two fields uniquely |
| 56 | // identify this location. |
| 57 | |
| 58 | // The file name will always be uniquely identified by its pointer since |
| 59 | // it comes from __FILE__, so no need to check the contents of the string. |
| 60 | // See the definition of FROM_HERE in location.h, and how it is used |
| 61 | // elsewhere. |
amistry | 52645f8 | 2015-12-03 08:30:58 +0900 | [diff] [blame] | 62 | return base::HashPair(reinterpret_cast<uintptr_t>(location.file_name()), |
sque | 9f57c2c | 2015-03-14 11:39:03 +0900 | [diff] [blame] | 63 | location.line_number()); |
| 64 | } |
| 65 | }; |
| 66 | |
jar@chromium.org | 666ef9c | 2011-10-25 03:55:16 +0900 | [diff] [blame] | 67 | // Translate the some of the state in this instance into a human readable |
| 68 | // string with HTML characters in the function names escaped, and append that |
| 69 | // string to |output|. Inclusion of the file_name_ and function_name_ are |
| 70 | // optional, and controlled by the boolean arguments. |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 71 | void Write(bool display_filename, bool display_function_name, |
| 72 | std::string* output) const; |
| 73 | |
| 74 | // Write function_name_ in HTML with '<' and '>' properly encoded. |
| 75 | void WriteFunctionName(std::string* output) const; |
| 76 | |
| 77 | private: |
jbates@chromium.org | b84db52 | 2011-10-04 02:51:25 +0900 | [diff] [blame] | 78 | const char* function_name_; |
| 79 | const char* file_name_; |
| 80 | int line_number_; |
| 81 | const void* program_counter_; |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 82 | }; |
| 83 | |
isherman@chromium.org | 98c10d2 | 2012-04-13 09:39:26 +0900 | [diff] [blame] | 84 | // A "snapshotted" representation of the Location class that can safely be |
| 85 | // passed across process boundaries. |
| 86 | struct BASE_EXPORT LocationSnapshot { |
| 87 | // The default constructor is exposed to support the IPC serialization macros. |
| 88 | LocationSnapshot(); |
| 89 | explicit LocationSnapshot(const tracked_objects::Location& location); |
| 90 | ~LocationSnapshot(); |
| 91 | |
| 92 | std::string file_name; |
| 93 | std::string function_name; |
| 94 | int line_number; |
| 95 | }; |
| 96 | |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 97 | BASE_EXPORT const void* GetProgramCounter(); |
| 98 | |
| 99 | // Define a macro to record the current source location. |
jar@chromium.org | 27cf297 | 2011-11-09 02:09:21 +0900 | [diff] [blame] | 100 | #define FROM_HERE FROM_HERE_WITH_EXPLICIT_FUNCTION(__FUNCTION__) |
| 101 | |
| 102 | #define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \ |
| 103 | ::tracked_objects::Location(function_name, \ |
| 104 | __FILE__, \ |
| 105 | __LINE__, \ |
| 106 | ::tracked_objects::GetProgramCounter()) |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 107 | |
| 108 | } // namespace tracked_objects |
| 109 | |
| 110 | #endif // BASE_LOCATION_H_ |