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 "build/build_config.h"
7 #if defined(COMPILER_MSVC)
8 // MSDN says to #include <intrin.h>, but that breaks the VS2005 build.
10 void* _ReturnAddress();
14 #include "base/location.h"
15 #include "base/stringprintf.h"
16 #include "base/strings/string_number_conversions.h"
18 namespace tracked_objects
{
20 Location::Location(const char* function_name
,
21 const char* file_name
,
23 const void* program_counter
)
24 : function_name_(function_name
),
25 file_name_(file_name
),
26 line_number_(line_number
),
27 program_counter_(program_counter
) {
31 : function_name_("Unknown"),
32 file_name_("Unknown"),
34 program_counter_(NULL
) {
37 std::string
Location::ToString() const {
38 return std::string(function_name_
) + "@" + file_name_
+ ":" +
39 base::IntToString(line_number_
);
42 void Location::Write(bool display_filename
, bool display_function_name
,
43 std::string
* output
) const {
44 base::StringAppendF(output
, "%s[%d] ",
45 display_filename
? file_name_
: "line",
48 if (display_function_name
) {
49 WriteFunctionName(output
);
50 output
->push_back(' ');
54 void Location::WriteFunctionName(std::string
* output
) const {
55 // Translate "<" to "<" for HTML safety.
56 // TODO(jar): Support ASCII or html for logging in ASCII.
57 for (const char *p
= function_name_
; *p
; p
++) {
60 output
->append("<");
64 output
->append(">");
68 output
->push_back(*p
);
74 //------------------------------------------------------------------------------
75 LocationSnapshot::LocationSnapshot() : line_number(-1) {
78 LocationSnapshot::LocationSnapshot(
79 const tracked_objects::Location
& location
)
80 : file_name(location
.file_name()),
81 function_name(location
.function_name()),
82 line_number(location
.line_number()) {
85 LocationSnapshot::~LocationSnapshot() {
88 //------------------------------------------------------------------------------
89 #if defined(COMPILER_MSVC)
92 BASE_EXPORT
const void* GetProgramCounter() {
93 #if defined(COMPILER_MSVC)
94 return _ReturnAddress();
95 #elif defined(COMPILER_GCC)
96 return __builtin_extract_return_addr(__builtin_return_address(0));
97 #endif // COMPILER_GCC
102 } // namespace tracked_objects