OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/os.h" | 5 #include "vm/os.h" |
6 | 6 |
7 #include <android/log.h> | 7 #include <android/log.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <limits.h> | 9 #include <limits.h> |
10 #include <malloc.h> | 10 #include <malloc.h> |
11 #include <time.h> | 11 #include <time.h> |
12 #include <sys/resource.h> | 12 #include <sys/resource.h> |
13 #include <sys/time.h> | 13 #include <sys/time.h> |
14 #include <sys/types.h> | 14 #include <sys/types.h> |
15 #include <unistd.h> | 15 #include <unistd.h> |
16 | 16 |
17 #include "platform/utils.h" | 17 #include "platform/utils.h" |
| 18 #include "vm/code_observers.h" |
| 19 #include "vm/dart.h" |
| 20 #include "vm/debuginfo.h" |
18 #include "vm/isolate.h" | 21 #include "vm/isolate.h" |
| 22 #include "vm/zone.h" |
| 23 |
19 | 24 |
20 namespace dart { | 25 namespace dart { |
21 | 26 |
| 27 // Android CodeObservers. |
| 28 |
| 29 DEFINE_FLAG(bool, generate_gdb_symbols, false, |
| 30 "Generate symbols of generated dart functions for debugging with GDB"); |
| 31 DEFINE_FLAG(bool, generate_perf_events_symbols, false, |
| 32 "Generate events symbols for profiling with perf"); |
| 33 DEFINE_FLAG(charp, generate_pprof_symbols, false, |
| 34 "Generate events symbols for profiling with pprof"); |
| 35 |
| 36 class PerfCodeObserver : public CodeObserver { |
| 37 public: |
| 38 PerfCodeObserver() { |
| 39 Dart_FileOpenCallback file_open = Isolate::file_open_callback(); |
| 40 if (file_open == NULL) { |
| 41 return; |
| 42 } |
| 43 const char* format = "/tmp/perf-%ld.map"; |
| 44 intptr_t pid = getpid(); |
| 45 intptr_t len = OS::SNPrint(NULL, 0, format, pid); |
| 46 char* filename = new char[len + 1]; |
| 47 OS::SNPrint(filename, len + 1, format, pid); |
| 48 out_file_ = (*file_open)(filename); |
| 49 } |
| 50 |
| 51 ~PerfCodeObserver() { |
| 52 Dart_FileCloseCallback file_close = Isolate::file_close_callback(); |
| 53 if (file_close == NULL) { |
| 54 return; |
| 55 } |
| 56 ASSERT(out_file_ != NULL); |
| 57 (*file_close)(out_file_); |
| 58 } |
| 59 |
| 60 virtual bool IsActive() const { |
| 61 return FLAG_generate_perf_events_symbols; |
| 62 } |
| 63 |
| 64 virtual void Notify(const char* name, |
| 65 uword base, |
| 66 uword prologue_offset, |
| 67 uword size, |
| 68 bool optimized) { |
| 69 Dart_FileWriteCallback file_write = Isolate::file_write_callback(); |
| 70 ASSERT(file_write != NULL); |
| 71 const char* format = "%"Px" %"Px" %s%s\n"; |
| 72 const char* marker = optimized ? "*" : ""; |
| 73 intptr_t len = OS::SNPrint(NULL, 0, format, base, size, marker, name); |
| 74 char* buffer = Isolate::Current()->current_zone()->Alloc<char>(len + 1); |
| 75 OS::SNPrint(buffer, len + 1, format, base, size, marker, name); |
| 76 ASSERT(out_file_ != NULL); |
| 77 (*file_write)(buffer, len, out_file_); |
| 78 } |
| 79 |
| 80 private: |
| 81 void* out_file_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(PerfCodeObserver); |
| 84 }; |
| 85 |
| 86 class PprofCodeObserver : public CodeObserver { |
| 87 public: |
| 88 PprofCodeObserver() { |
| 89 pprof_symbol_generator_ = DebugInfo::NewGenerator(); |
| 90 } |
| 91 |
| 92 ~PprofCodeObserver() { |
| 93 Dart_FileOpenCallback file_open = Isolate::file_open_callback(); |
| 94 if (file_open == NULL) { |
| 95 return; |
| 96 } |
| 97 Dart_FileCloseCallback file_close = Isolate::file_close_callback(); |
| 98 if (file_close == NULL) { |
| 99 return; |
| 100 } |
| 101 Dart_FileWriteCallback file_write = Isolate::file_write_callback(); |
| 102 if (file_write == NULL) { |
| 103 return; |
| 104 } |
| 105 if (FLAG_generate_pprof_symbols == NULL) { |
| 106 return; |
| 107 } |
| 108 const char* filename = FLAG_generate_pprof_symbols; |
| 109 void* out_file = (*file_open)(filename); |
| 110 ASSERT(out_file != NULL); |
| 111 DebugInfo::ByteBuffer* debug_region = new DebugInfo::ByteBuffer(); |
| 112 ASSERT(debug_region != NULL); |
| 113 pprof_symbol_generator_->WriteToMemory(debug_region); |
| 114 int buffer_size = debug_region->size(); |
| 115 void* buffer = debug_region->data(); |
| 116 delete debug_region; |
| 117 if (buffer_size > 0) { |
| 118 ASSERT(buffer != NULL); |
| 119 (*file_write)(buffer, buffer_size, out_file); |
| 120 } |
| 121 (*file_close)(out_file); |
| 122 DebugInfo::UnregisterAllSections(); |
| 123 } |
| 124 |
| 125 virtual bool IsActive() const { |
| 126 return FLAG_generate_pprof_symbols != NULL; |
| 127 } |
| 128 |
| 129 virtual void Notify(const char* name, |
| 130 uword base, |
| 131 uword prologue_offset, |
| 132 uword size, |
| 133 bool optimized) { |
| 134 ASSERT(pprof_symbol_generator_ != NULL); |
| 135 pprof_symbol_generator_->AddCode(base, size); |
| 136 pprof_symbol_generator_->AddCodeRegion(name, base, size); |
| 137 } |
| 138 |
| 139 private: |
| 140 DebugInfo* pprof_symbol_generator_; |
| 141 |
| 142 DISALLOW_COPY_AND_ASSIGN(PprofCodeObserver); |
| 143 }; |
| 144 |
| 145 class GdbCodeObserver : public CodeObserver { |
| 146 public: |
| 147 GdbCodeObserver() { } |
| 148 |
| 149 virtual bool IsActive() const { |
| 150 return FLAG_generate_gdb_symbols; |
| 151 } |
| 152 |
| 153 virtual void Notify(const char* name, |
| 154 uword base, |
| 155 uword prologue_offset, |
| 156 uword size, |
| 157 bool optimized) { |
| 158 if (prologue_offset > 0) { |
| 159 // In order to ensure that gdb sees the first instruction of a function |
| 160 // as the prologue sequence we register two symbols for the cases when |
| 161 // the prologue sequence is not the first instruction: |
| 162 // <name>_entry is used for code preceding the prologue sequence. |
| 163 // <name> for rest of the code (first instruction is prologue sequence). |
| 164 const char* kFormat = "%s_%s"; |
| 165 intptr_t len = OS::SNPrint(NULL, 0, kFormat, name, "entry"); |
| 166 char* pname = Isolate::Current()->current_zone()->Alloc<char>(len + 1); |
| 167 OS::SNPrint(pname, (len + 1), kFormat, name, "entry"); |
| 168 DebugInfo::RegisterSection(pname, base, size); |
| 169 DebugInfo::RegisterSection(name, |
| 170 (base + prologue_offset), |
| 171 (size - prologue_offset)); |
| 172 } else { |
| 173 DebugInfo::RegisterSection(name, base, size); |
| 174 } |
| 175 } |
| 176 |
| 177 private: |
| 178 DISALLOW_COPY_AND_ASSIGN(GdbCodeObserver); |
| 179 }; |
| 180 |
| 181 |
22 static bool LocalTime(int64_t seconds_since_epoch, tm* tm_result) { | 182 static bool LocalTime(int64_t seconds_since_epoch, tm* tm_result) { |
23 time_t seconds = static_cast<time_t>(seconds_since_epoch); | 183 time_t seconds = static_cast<time_t>(seconds_since_epoch); |
24 if (seconds != seconds_since_epoch) return false; | 184 if (seconds != seconds_since_epoch) return false; |
25 struct tm* error_code = localtime_r(&seconds, tm_result); | 185 struct tm* error_code = localtime_r(&seconds, tm_result); |
26 return error_code != NULL; | 186 return error_code != NULL; |
27 } | 187 } |
28 | 188 |
29 | 189 |
30 const char* OS::GetTimeZoneName(int64_t seconds_since_epoch) { | 190 const char* OS::GetTimeZoneName(int64_t seconds_since_epoch) { |
31 tm decomposed; | 191 tm decomposed; |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 (str[i + 1] == 'x' || str[i + 1] == 'X') && | 355 (str[i + 1] == 'x' || str[i + 1] == 'X') && |
196 (str[i + 2] != '\0')) { | 356 (str[i + 2] != '\0')) { |
197 base = 16; | 357 base = 16; |
198 } | 358 } |
199 errno = 0; | 359 errno = 0; |
200 *value = strtoll(str, &endptr, base); | 360 *value = strtoll(str, &endptr, base); |
201 return ((errno == 0) && (endptr != str) && (*endptr == 0)); | 361 return ((errno == 0) && (endptr != str) && (*endptr == 0)); |
202 } | 362 } |
203 | 363 |
204 | 364 |
| 365 void OS::RegisterCodeObservers() { |
| 366 if (FLAG_generate_perf_events_symbols) { |
| 367 CodeObservers::Register(new PerfCodeObserver); |
| 368 } |
| 369 if (FLAG_generate_gdb_symbols) { |
| 370 CodeObservers::Register(new GdbCodeObserver); |
| 371 } |
| 372 if (FLAG_generate_pprof_symbols != NULL) { |
| 373 CodeObservers::Register(new PprofCodeObserver); |
| 374 } |
| 375 #if defined(DART_VTUNE_SUPPORT) |
| 376 Register(new VTuneCodeObserver); |
| 377 #endif |
| 378 } |
| 379 |
| 380 |
205 void OS::PrintErr(const char* format, ...) { | 381 void OS::PrintErr(const char* format, ...) { |
206 va_list args; | 382 va_list args; |
207 va_start(args, format); | 383 va_start(args, format); |
208 VFPrint(stderr, format, args); | 384 VFPrint(stderr, format, args); |
209 // Forward to the Android log for remote access. | 385 // Forward to the Android log for remote access. |
210 __android_log_vprint(ANDROID_LOG_ERROR, "DartVM", format, args); | 386 __android_log_vprint(ANDROID_LOG_ERROR, "DartVM", format, args); |
211 va_end(args); | 387 va_end(args); |
212 } | 388 } |
213 | 389 |
214 | 390 |
(...skipping 14 matching lines...) Expand all Loading... |
229 void OS::Abort() { | 405 void OS::Abort() { |
230 abort(); | 406 abort(); |
231 } | 407 } |
232 | 408 |
233 | 409 |
234 void OS::Exit(int code) { | 410 void OS::Exit(int code) { |
235 exit(code); | 411 exit(code); |
236 } | 412 } |
237 | 413 |
238 } // namespace dart | 414 } // namespace dart |
OLD | NEW |