blob: 6df8f503c922c5dff75435f466ccd0e06d55eda7 [file] [log] [blame]
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "oprofile-agent.h"
31
kasperl@chromium.org71affb52009-05-26 05:44:31 +000032namespace v8 {
33namespace internal {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000034
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000035
36bool OProfileAgent::Initialize() {
37#ifdef ENABLE_OPROFILE_AGENT
38 if (FLAG_oprofile) {
39 if (handle_ != NULL) return false;
40
41 // Disable code moving by GC.
42 FLAG_always_compact = false;
43 FLAG_never_compact = true;
44
45 handle_ = op_open_agent();
46 return (handle_ != NULL);
47 } else {
48 return true;
49 }
50#else
ager@chromium.org5aa501c2009-06-23 07:57:28 +000051 if (FLAG_oprofile) {
52 OS::Print("Warning: --oprofile specified but binary compiled without "
53 "oprofile support.\n");
54 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000055 return true;
56#endif
57}
58
59
60void OProfileAgent::TearDown() {
61#ifdef ENABLE_OPROFILE_AGENT
62 if (handle_ != NULL) {
63 op_close_agent(handle_);
64 }
65#endif
66}
67
68
ager@chromium.orgb26c50a2010-03-26 09:27:16 +000069#ifdef ENABLE_OPROFILE_AGENT
70op_agent_t OProfileAgent::handle_ = NULL;
71
72
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000073void OProfileAgent::CreateNativeCodeRegion(const char* name,
74 const void* ptr, unsigned int size) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000075 op_write_native_code(handle_, name, (uint64_t)ptr, ptr, size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000076}
77
78
79void OProfileAgent::CreateNativeCodeRegion(String* name,
80 const void* ptr, unsigned int size) {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +000081 const char* func_name;
82 SmartPointer<char> str =
83 name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
84 func_name = name->length() > 0 ? *str : "<anonymous>";
85 CreateNativeCodeRegion(func_name, ptr, size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000086}
87
88
89void OProfileAgent::CreateNativeCodeRegion(String* name, String* source,
90 int line_num, const void* ptr, unsigned int size) {
ager@chromium.orgb26c50a2010-03-26 09:27:16 +000091 Vector<char> buf = Vector<char>::New(OProfileAgent::kFormattingBufSize);
92 const char* func_name;
93 SmartPointer<char> str =
94 name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
95 func_name = name->length() > 0 ? *str : "<anonymous>";
96 SmartPointer<char> source_str =
97 source->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
98 if (v8::internal::OS::SNPrintF(buf, "%s %s:%d",
99 func_name, *source_str, line_num) != -1) {
100 CreateNativeCodeRegion(buf.start(), ptr, size);
101 } else {
102 CreateNativeCodeRegion("<script/func name too long>", ptr, size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000103 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000104}
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000105
106#endif // ENABLE_OPROFILE_AGENT
107
108} } // namespace v8::internal