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.
5 #include "base/location.h"
7 #if defined(COMPILER_MSVC)
11 #include "base/compiler_specific.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h"
14 #include "build/build_config.h"
18 Location::Location() = default;
19 Location::Location(const Location& other) = default;
21 Location::Location(const char* file_name, const void* program_counter)
22 : file_name_(file_name), program_counter_(program_counter) {}
24 Location::Location(const char* function_name,
25 const char* file_name,
27 const void* program_counter)
28 : function_name_(function_name),
29 file_name_(file_name),
30 line_number_(line_number),
31 program_counter_(program_counter) {
33 // The program counter should not be null except in a default constructed
34 // (empty) Location object. This value is used for identity, so if it doesn't
35 // uniquely identify a location, things will break.
37 // The program counter isn't supported in NaCl so location objects won't work
38 // properly in that context.
39 DCHECK(program_counter);
43 std::string Location::ToString() const {
44 if (has_source_info()) {
45 return std::string(function_name_) + "@" + file_name_ + ":" +
46 IntToString(line_number_);
48 return StringPrintf("pc:%p", program_counter_);
51 #if defined(COMPILER_MSVC)
52 #define RETURN_ADDRESS() _ReturnAddress()
53 #elif defined(COMPILER_GCC) && !defined(OS_NACL)
54 #define RETURN_ADDRESS() \
55 __builtin_extract_return_addr(__builtin_return_address(0))
57 #define RETURN_ADDRESS() nullptr
61 NOINLINE Location Location::CreateFromHere(const char* file_name) {
62 return Location(file_name, RETURN_ADDRESS());
66 NOINLINE Location Location::CreateFromHere(const char* function_name,
67 const char* file_name,
69 return Location(function_name, file_name, line_number, RETURN_ADDRESS());
72 //------------------------------------------------------------------------------
73 NOINLINE const void* GetProgramCounter() {
74 return RETURN_ADDRESS();