• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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 
32 namespace v8 {
33 namespace internal {
34 
35 #ifdef ENABLE_OPROFILE_AGENT
36 op_agent_t OProfileAgent::handle_ = NULL;
37 #endif
38 
39 
Initialize()40 bool OProfileAgent::Initialize() {
41 #ifdef ENABLE_OPROFILE_AGENT
42   if (FLAG_oprofile) {
43     if (handle_ != NULL) return false;
44 
45     // Disable code moving by GC.
46     FLAG_always_compact = false;
47     FLAG_never_compact = true;
48 
49     handle_ = op_open_agent();
50     return (handle_ != NULL);
51   } else {
52     return true;
53   }
54 #else
55   if (FLAG_oprofile) {
56     OS::Print("Warning: --oprofile specified but binary compiled without "
57               "oprofile support.\n");
58   }
59   return true;
60 #endif
61 }
62 
63 
TearDown()64 void OProfileAgent::TearDown() {
65 #ifdef ENABLE_OPROFILE_AGENT
66   if (handle_ != NULL) {
67     op_close_agent(handle_);
68   }
69 #endif
70 }
71 
72 
CreateNativeCodeRegion(const char * name,const void * ptr,unsigned int size)73 void OProfileAgent::CreateNativeCodeRegion(const char* name,
74     const void* ptr, unsigned int size) {
75 #ifdef ENABLE_OPROFILE_AGENT
76   if (handle_ == NULL) return;
77   op_write_native_code(handle_, name, (uint64_t)ptr, ptr, size);
78 #endif
79 }
80 
81 
CreateNativeCodeRegion(String * name,const void * ptr,unsigned int size)82 void OProfileAgent::CreateNativeCodeRegion(String* name,
83     const void* ptr, unsigned int size) {
84 #ifdef ENABLE_OPROFILE_AGENT
85   if (handle_ != NULL) {
86     const char* func_name;
87     SmartPointer<char> str =
88         name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
89     func_name = name->length() > 0 ? *str : "<anonymous>";
90     CreateNativeCodeRegion(func_name, ptr, size);
91   }
92 #endif
93 }
94 
95 
CreateNativeCodeRegion(String * name,String * source,int line_num,const void * ptr,unsigned int size)96 void OProfileAgent::CreateNativeCodeRegion(String* name, String* source,
97     int line_num, const void* ptr, unsigned int size) {
98 #ifdef ENABLE_OPROFILE_AGENT
99   if (handle_ != NULL) {
100     Vector<char> buf = Vector<char>::New(OProfileAgent::kFormattingBufSize);
101     const char* func_name;
102     SmartPointer<char> str =
103         name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
104     func_name = name->length() > 0 ? *str : "<anonymous>";
105     SmartPointer<char> source_str =
106         source->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
107     if (v8::internal::OS::SNPrintF(buf, "%s %s:%d",
108                                    func_name, *source_str, line_num) != -1) {
109       CreateNativeCodeRegion(buf.start(), ptr, size);
110     } else {
111       CreateNativeCodeRegion("<script/func name too long>", ptr, size);
112     }
113   }
114 #endif
115 }
116 } }
117