blob: dd78515ce2b003b9ede74e8a59bff7d7451a7548 [file] [log] [blame]
isherman@chromium.orga9490ac2012-03-17 06:43:41 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +09002// 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
avia6a6a682015-12-27 07:15:14 +09008#include <stddef.h>
9
sque9f57c2c2015-03-14 11:39:03 +090010#include <cassert>
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +090011#include <string>
12
13#include "base/base_export.h"
davidbenf8b29b72016-01-22 10:41:41 +090014#include "base/hash.h"
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +090015
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +090016namespace tracked_objects {
17
18// Location provides basic info where of an object was constructed, or was
19// significantly brought to life.
20class 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
sque9f57c2c2015-03-14 11:39:03 +090033 // 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.org8e2e3002011-09-22 03:05:41 +090042 }
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
sque9f57c2c2015-03-14 11:39:03 +090051 // 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.
davidbenf8b29b72016-01-22 10:41:41 +090062 return base::HashInts(reinterpret_cast<uintptr_t>(location.file_name()),
sque9f57c2c2015-03-14 11:39:03 +090063 location.line_number());
64 }
65 };
66
jar@chromium.org666ef9c2011-10-25 03:55:16 +090067 // 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.org8e2e3002011-09-22 03:05:41 +090071 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.orgb84db522011-10-04 02:51:25 +090078 const char* function_name_;
79 const char* file_name_;
80 int line_number_;
81 const void* program_counter_;
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +090082};
83
isherman@chromium.org98c10d22012-04-13 09:39:26 +090084// A "snapshotted" representation of the Location class that can safely be
85// passed across process boundaries.
86struct 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.org8e2e3002011-09-22 03:05:41 +090097BASE_EXPORT const void* GetProgramCounter();
98
99// Define a macro to record the current source location.
pkasting392561e2016-07-27 11:18:20 +0900100#define FROM_HERE FROM_HERE_WITH_EXPLICIT_FUNCTION(__func__)
jar@chromium.org27cf2972011-11-09 02:09:21 +0900101
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.org8e2e3002011-09-22 03:05:41 +0900107
108} // namespace tracked_objects
109
110#endif // BASE_LOCATION_H_