1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/location.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,102 @@ 1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include "build/build_config.h" 1.9 + 1.10 +#if defined(COMPILER_MSVC) 1.11 +// MSDN says to #include <intrin.h>, but that breaks the VS2005 build. 1.12 +extern "C" { 1.13 + void* _ReturnAddress(); 1.14 +} 1.15 +#endif 1.16 + 1.17 +#include "base/location.h" 1.18 +#include "base/strings/string_number_conversions.h" 1.19 +#include "base/strings/stringprintf.h" 1.20 + 1.21 +namespace tracked_objects { 1.22 + 1.23 +Location::Location(const char* function_name, 1.24 + const char* file_name, 1.25 + int line_number, 1.26 + const void* program_counter) 1.27 + : function_name_(function_name), 1.28 + file_name_(file_name), 1.29 + line_number_(line_number), 1.30 + program_counter_(program_counter) { 1.31 +} 1.32 + 1.33 +Location::Location() 1.34 + : function_name_("Unknown"), 1.35 + file_name_("Unknown"), 1.36 + line_number_(-1), 1.37 + program_counter_(NULL) { 1.38 +} 1.39 + 1.40 +std::string Location::ToString() const { 1.41 + return std::string(function_name_) + "@" + file_name_ + ":" + 1.42 + base::IntToString(line_number_); 1.43 +} 1.44 + 1.45 +void Location::Write(bool display_filename, bool display_function_name, 1.46 + std::string* output) const { 1.47 + base::StringAppendF(output, "%s[%d] ", 1.48 + display_filename ? file_name_ : "line", 1.49 + line_number_); 1.50 + 1.51 + if (display_function_name) { 1.52 + WriteFunctionName(output); 1.53 + output->push_back(' '); 1.54 + } 1.55 +} 1.56 + 1.57 +void Location::WriteFunctionName(std::string* output) const { 1.58 + // Translate "<" to "<" for HTML safety. 1.59 + // TODO(jar): Support ASCII or html for logging in ASCII. 1.60 + for (const char *p = function_name_; *p; p++) { 1.61 + switch (*p) { 1.62 + case '<': 1.63 + output->append("<"); 1.64 + break; 1.65 + 1.66 + case '>': 1.67 + output->append(">"); 1.68 + break; 1.69 + 1.70 + default: 1.71 + output->push_back(*p); 1.72 + break; 1.73 + } 1.74 + } 1.75 +} 1.76 + 1.77 +//------------------------------------------------------------------------------ 1.78 +LocationSnapshot::LocationSnapshot() : line_number(-1) { 1.79 +} 1.80 + 1.81 +LocationSnapshot::LocationSnapshot( 1.82 + const tracked_objects::Location& location) 1.83 + : file_name(location.file_name()), 1.84 + function_name(location.function_name()), 1.85 + line_number(location.line_number()) { 1.86 +} 1.87 + 1.88 +LocationSnapshot::~LocationSnapshot() { 1.89 +} 1.90 + 1.91 +//------------------------------------------------------------------------------ 1.92 +#if defined(COMPILER_MSVC) 1.93 +__declspec(noinline) 1.94 +#endif 1.95 +BASE_EXPORT const void* GetProgramCounter() { 1.96 +#if defined(COMPILER_MSVC) 1.97 + return _ReturnAddress(); 1.98 +#elif defined(COMPILER_GCC) 1.99 + return __builtin_extract_return_addr(__builtin_return_address(0)); 1.100 +#endif // COMPILER_GCC 1.101 + 1.102 + return NULL; 1.103 +} 1.104 + 1.105 +} // namespace tracked_objects