blob: 7948ac71e0756e3b5a586739cc8bf7578e2ed5cf [file] [log] [blame]
[email protected]ec6bf332012-03-16 21:43:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c62dd9d2011-09-21 18:05:412// 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
avi9b6f42932015-12-26 22:15:148#include <stddef.h>
9
squee3b16f12015-03-14 02:39:0310#include <cassert>
[email protected]c62dd9d2011-09-21 18:05:4111#include <string>
12
13#include "base/base_export.h"
Brett Wilsonc794cf62017-09-01 20:14:3314#include "base/debug/debugging_flags.h"
davidben411d3f72016-01-22 01:41:4115#include "base/hash.h"
[email protected]c62dd9d2011-09-21 18:05:4116
Brett Wilsonabbb9602017-09-11 23:26:3917namespace base {
[email protected]c62dd9d2011-09-21 18:05:4118
19// Location provides basic info where of an object was constructed, or was
20// significantly brought to life.
21class BASE_EXPORT Location {
22 public:
Brett Wilsonc794cf62017-09-01 20:14:3323 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]c62dd9d2011-09-21 18:05:4131 // 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 Wilsonc794cf62017-09-01 20:14:3339 // Comparator for hash map insertion. The program counter should uniquely
40 // identify a location.
squee3b16f12015-03-14 02:39:0341 bool operator==(const Location& other) const {
Brett Wilsonc794cf62017-09-01 20:14:3342 return program_counter_ == other.program_counter_;
[email protected]c62dd9d2011-09-21 18:05:4143 }
44
Brett Wilsonc794cf62017-09-01 20:14:3345 // 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]c62dd9d2011-09-21 18:05:4165 const void* program_counter() const { return program_counter_; }
66
Brett Wilson07ec3b52017-10-09 21:42:4567 // Converts to the most user-readable form possible. If function and filename
68 // are not available, this will return "pc:<hex address>".
[email protected]c62dd9d2011-09-21 18:05:4169 std::string ToString() const;
70
tzik4f05a25a2017-09-13 00:23:1671 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]c62dd9d2011-09-21 18:05:4176 private:
Brett Wilsonc794cf62017-09-01 20:14:3377 const char* function_name_ = nullptr;
78 const char* file_name_ = nullptr;
79 int line_number_ = -1;
80 const void* program_counter_ = nullptr;
[email protected]c62dd9d2011-09-21 18:05:4181};
82
[email protected]1cb05db2012-04-13 00:39:2683// A "snapshotted" representation of the Location class that can safely be
84// passed across process boundaries.
85struct BASE_EXPORT LocationSnapshot {
86 // The default constructor is exposed to support the IPC serialization macros.
87 LocationSnapshot();
Brett Wilsonabbb9602017-09-11 23:26:3988 explicit LocationSnapshot(const Location& location);
[email protected]1cb05db2012-04-13 00:39:2689 ~LocationSnapshot();
90
91 std::string file_name;
92 std::string function_name;
Brett Wilsonc794cf62017-09-01 20:14:3393 int line_number = -1;
[email protected]1cb05db2012-04-13 00:39:2694};
95
[email protected]c62dd9d2011-09-21 18:05:4196BASE_EXPORT const void* GetProgramCounter();
97
Brett Wilsonc794cf62017-09-01 20:14:3398// The macros defined here will expand to the current function.
99#if BUILDFLAG(ENABLE_LOCATION_SOURCE)
Brett Wilson5b5b7cb2017-09-01 04:40:02100
Brett Wilsonc794cf62017-09-01 20:14:33101// Full source information should be included.
102#define FROM_HERE FROM_HERE_WITH_EXPLICIT_FUNCTION(__func__)
Brett Wilsonabbb9602017-09-11 23:26:39103#define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \
tzik4f05a25a2017-09-13 00:23:16104 ::base::Location::CreateFromHere(function_name, __FILE__, __LINE__)
Brett Wilsonc794cf62017-09-01 20:14:33105
106#else
107
108// TODO(http://crbug.com/760702) remove the __FILE__ argument from these calls.
tzik4f05a25a2017-09-13 00:23:16109#define FROM_HERE ::base::Location::CreateFromHere(__FILE__)
Brett Wilsonabbb9602017-09-11 23:26:39110#define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \
tzik4f05a25a2017-09-13 00:23:16111 ::base::Location::CreateFromHere(function_name, __FILE__, -1)
Brett Wilsonc794cf62017-09-01 20:14:33112
113#endif
[email protected]c62dd9d2011-09-21 18:05:41114
Brett Wilsonabbb9602017-09-11 23:26:39115} // namespace base
116
Brett Wilsonc794cf62017-09-01 20:14:33117namespace std {
118
119// Specialization for using Location in hash tables.
120template <>
Brett Wilsonabbb9602017-09-11 23:26:39121struct hash<::base::Location> {
122 std::size_t operator()(const ::base::Location& loc) const {
Brett Wilsonc794cf62017-09-01 20:14:33123 const void* program_counter = loc.program_counter();
Brett Wilson2cea5ca62017-09-01 20:23:53124 return base::Hash(&program_counter, sizeof(void*));
Brett Wilsonc794cf62017-09-01 20:14:33125 }
126};
127
128} // namespace std
129
[email protected]c62dd9d2011-09-21 18:05:41130#endif // BASE_LOCATION_H_