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