| 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 |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #include "vm/globals.h" |
| 6 | #if defined(HOST_OS_LINUX) |
| 7 | |
| 8 | #include "vm/os.h" |
| 9 | |
| 10 | #include <errno.h> // NOLINT |
| 11 | #include <fcntl.h> // NOLINT |
| 12 | #include <limits.h> // NOLINT |
| 13 | #include <malloc.h> // NOLINT |
| 14 | #include <sys/mman.h> // NOLINT |
| 15 | #include <sys/resource.h> // NOLINT |
| 16 | #include <sys/stat.h> // NOLINT |
| 17 | #include <sys/syscall.h> // NOLINT |
| 18 | #include <sys/time.h> // NOLINT |
| 19 | #include <sys/types.h> // NOLINT |
| 20 | #include <time.h> // NOLINT |
| 21 | #include <unistd.h> // NOLINT |
| 22 | |
| 23 | #include "platform/memory_sanitizer.h" |
| 24 | #include "platform/utils.h" |
| 25 | #include "vm/code_observers.h" |
| 26 | #include "vm/dart.h" |
| 27 | #include "vm/flags.h" |
| 28 | #include "vm/isolate.h" |
| 29 | #include "vm/lockers.h" |
| 30 | #include "vm/os_thread.h" |
| 31 | #include "vm/zone.h" |
| 32 | |
| 33 | namespace dart { |
| 34 | |
| 35 | #ifndef PRODUCT |
| 36 | |
| 37 | DEFINE_FLAG(bool, |
| 38 | generate_perf_events_symbols, |
| 39 | false, |
| 40 | "Generate events symbols for profiling with perf (disables dual " |
| 41 | "code mapping)" ); |
| 42 | |
| 43 | DEFINE_FLAG(bool, |
| 44 | generate_perf_jitdump, |
| 45 | false, |
| 46 | "Generate jitdump file to use with perf-inject (disables dual code " |
| 47 | "mapping)" ); |
| 48 | |
| 49 | DECLARE_FLAG(bool, write_protect_code); |
| 50 | DECLARE_FLAG(bool, write_protect_vm_isolate); |
| 51 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 52 | DECLARE_FLAG(bool, code_comments); |
| 53 | #endif |
| 54 | |
| 55 | // Linux CodeObservers. |
| 56 | |
| 57 | // Simple perf support: generate /tmp/perf-<pid>.map file that maps |
| 58 | // memory ranges to symbol names for JIT generated code. This allows |
| 59 | // perf-report to resolve addresses falling into JIT generated code. |
| 60 | // However perf-annotate does not work in this mode because JIT code |
| 61 | // is transient and does not exist anymore at the moment when you |
| 62 | // invoke perf-report. |
| 63 | class PerfCodeObserver : public CodeObserver { |
| 64 | public: |
| 65 | PerfCodeObserver() : out_file_(NULL) { |
| 66 | Dart_FileOpenCallback file_open = Dart::file_open_callback(); |
| 67 | if (file_open == NULL) { |
| 68 | return; |
| 69 | } |
| 70 | intptr_t pid = getpid(); |
| 71 | char* filename = OS::SCreate(NULL, "/tmp/perf-%" Pd ".map" , pid); |
| 72 | out_file_ = (*file_open)(filename, true); |
| 73 | free(filename); |
| 74 | } |
| 75 | |
| 76 | ~PerfCodeObserver() { |
| 77 | Dart_FileCloseCallback file_close = Dart::file_close_callback(); |
| 78 | if ((file_close == NULL) || (out_file_ == NULL)) { |
| 79 | return; |
| 80 | } |
| 81 | (*file_close)(out_file_); |
| 82 | } |
| 83 | |
| 84 | virtual bool IsActive() const { |
| 85 | return FLAG_generate_perf_events_symbols && (out_file_ != NULL); |
| 86 | } |
| 87 | |
| 88 | virtual void (const char* name, |
| 89 | uword base, |
| 90 | uword prologue_offset, |
| 91 | uword size, |
| 92 | bool optimized, |
| 93 | const CodeComments* ) { |
| 94 | Dart_FileWriteCallback file_write = Dart::file_write_callback(); |
| 95 | if ((file_write == NULL) || (out_file_ == NULL)) { |
| 96 | return; |
| 97 | } |
| 98 | const char* marker = optimized ? "*" : "" ; |
| 99 | char* buffer = |
| 100 | OS::SCreate(Thread::Current()->zone(), "%" Px " %" Px " %s%s\n" , base, |
| 101 | size, marker, name); |
| 102 | { |
| 103 | MutexLocker ml(CodeObservers::mutex()); |
| 104 | (*file_write)(buffer, strlen(buffer), out_file_); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | void* out_file_; |
| 110 | |
| 111 | DISALLOW_COPY_AND_ASSIGN(PerfCodeObserver); |
| 112 | }; |
| 113 | |
| 114 | // Code observer that generates a JITDUMP[1] file that can be interpreted by |
| 115 | // perf-inject to generate ELF images for JIT generated code objects, which |
| 116 | // allows both perf-report and perf-annotate to recognize them. |
| 117 | // |
| 118 | // Usage: |
| 119 | // |
| 120 | // $ perf record -k mono dart --generate-perf-jitdump benchmark.dart |
| 121 | // $ perf inject -j -i perf.data -o perf.data.jitted |
| 122 | // $ perf report -i perf.data.jitted |
| 123 | // |
| 124 | // [1] see linux/tools/perf/Documentation/jitdump-specification.txt for |
| 125 | // JITDUMP binary format. |
| 126 | class JitDumpCodeObserver : public CodeObserver { |
| 127 | public: |
| 128 | JitDumpCodeObserver() : pid_(getpid()) { |
| 129 | char* const filename = OS::SCreate(nullptr, "/tmp/jit-%" Pd ".dump" , pid_); |
| 130 | const int fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0666); |
| 131 | free(filename); |
| 132 | |
| 133 | if (fd == -1) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | // Map JITDUMP file, this mapping will be recorded by perf. This allows |
| 138 | // perf-inject to find this file later. |
| 139 | const long page_size = sysconf(_SC_PAGESIZE); // NOLINT(runtime/int) |
| 140 | if (page_size == -1) { |
| 141 | close(fd); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | mapped_ = |
| 146 | mmap(nullptr, page_size, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0); |
| 147 | if (mapped_ == nullptr) { |
| 148 | close(fd); |
| 149 | return; |
| 150 | } |
| 151 | mapped_size_ = page_size; |
| 152 | |
| 153 | out_file_ = fdopen(fd, "w+" ); |
| 154 | if (out_file_ == nullptr) { |
| 155 | close(fd); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | // Buffer the output to avoid high IO overheads - we are going to be |
| 160 | // writing all JIT generated code out. |
| 161 | setvbuf(out_file_, nullptr, _IOFBF, 2 * MB); |
| 162 | |
| 163 | // Disable code write protection and vm isolate write protection, because |
| 164 | // calling mprotect on the pages filled with JIT generated code objects |
| 165 | // confuses perf. |
| 166 | FLAG_write_protect_code = false; |
| 167 | FLAG_write_protect_vm_isolate = false; |
| 168 | |
| 169 | #if !defined(DART_PRECOMPILED_RUNTIME) |
| 170 | // Enable code comments. |
| 171 | FLAG_code_comments = true; |
| 172 | #endif |
| 173 | |
| 174 | // Write JITDUMP header. |
| 175 | WriteHeader(); |
| 176 | } |
| 177 | |
| 178 | ~JitDumpCodeObserver() { |
| 179 | if (mapped_ != nullptr) { |
| 180 | munmap(mapped_, mapped_size_); |
| 181 | mapped_ = nullptr; |
| 182 | } |
| 183 | |
| 184 | if (out_file_ != nullptr) { |
| 185 | fclose(out_file_); |
| 186 | out_file_ = nullptr; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | virtual bool IsActive() const { |
| 191 | return FLAG_generate_perf_jitdump && (out_file_ != nullptr); |
| 192 | } |
| 193 | |
| 194 | virtual void (const char* name, |
| 195 | uword base, |
| 196 | uword prologue_offset, |
| 197 | uword size, |
| 198 | bool optimized, |
| 199 | const CodeComments* ) { |
| 200 | MutexLocker ml(CodeObservers::mutex()); |
| 201 | |
| 202 | const char* marker = optimized ? "*" : "" ; |
| 203 | char* buffer = OS::SCreate(Thread::Current()->zone(), "%s%s" , marker, name); |
| 204 | const size_t name_length = strlen(buffer); |
| 205 | |
| 206 | WriteDebugInfo(base, comments); |
| 207 | |
| 208 | CodeLoadEvent ev; |
| 209 | ev.event = BaseEvent::kLoad; |
| 210 | ev.size = sizeof(ev) + (name_length + 1) + size; |
| 211 | ev.time_stamp = OS::GetCurrentMonotonicTicks(); |
| 212 | ev.process_id = getpid(); |
| 213 | ev.thread_id = syscall(SYS_gettid); |
| 214 | ev.vma = base; |
| 215 | ev.code_address = base; |
| 216 | ev.code_size = size; |
| 217 | ev.code_id = code_id_++; |
| 218 | |
| 219 | WriteFully(&ev, sizeof(ev)); |
| 220 | WriteFully(buffer, name_length + 1); |
| 221 | WriteFully(reinterpret_cast<void*>(base), size); |
| 222 | } |
| 223 | |
| 224 | private: |
| 225 | struct { |
| 226 | const uint32_t = 0x4A695444; |
| 227 | const uint32_t = 1; |
| 228 | const uint32_t = sizeof(Header); |
| 229 | uint32_t ; |
| 230 | const uint32_t = 0xDEADBEEF; |
| 231 | uint32_t ; |
| 232 | uint64_t ; |
| 233 | const uint64_t = 0; |
| 234 | }; |
| 235 | |
| 236 | struct BaseEvent { |
| 237 | enum Event { |
| 238 | kLoad = 0, |
| 239 | kMove = 1, |
| 240 | kDebugInfo = 2, |
| 241 | kClose = 3, |
| 242 | kUnwindingInfo = 4 |
| 243 | }; |
| 244 | |
| 245 | uint32_t event; |
| 246 | uint32_t size; |
| 247 | uint64_t time_stamp; |
| 248 | }; |
| 249 | |
| 250 | struct CodeLoadEvent : BaseEvent { |
| 251 | uint32_t process_id; |
| 252 | uint32_t thread_id; |
| 253 | uint64_t vma; |
| 254 | uint64_t code_address; |
| 255 | uint64_t code_size; |
| 256 | uint64_t code_id; |
| 257 | }; |
| 258 | |
| 259 | struct DebugInfoEvent : BaseEvent { |
| 260 | uint64_t address; |
| 261 | uint64_t entry_count; |
| 262 | // DebugInfoEntry entries[entry_count_]; |
| 263 | }; |
| 264 | |
| 265 | struct DebugInfoEntry { |
| 266 | uint64_t address; |
| 267 | int32_t line_number; |
| 268 | int32_t column; |
| 269 | // Followed by nul-terminated name. |
| 270 | }; |
| 271 | |
| 272 | // ELF machine architectures |
| 273 | // From linux/include/uapi/linux/elf-em.h |
| 274 | static const uint32_t EM_386 = 3; |
| 275 | static const uint32_t EM_X86_64 = 62; |
| 276 | static const uint32_t EM_ARM = 40; |
| 277 | static const uint32_t EM_AARCH64 = 183; |
| 278 | |
| 279 | static uint32_t GetElfMachineArchitecture() { |
| 280 | #if TARGET_ARCH_IA32 |
| 281 | return EM_386; |
| 282 | #elif TARGET_ARCH_X64 |
| 283 | return EM_X86_64; |
| 284 | #elif TARGET_ARCH_ARM |
| 285 | return EM_ARM; |
| 286 | #elif TARGET_ARCH_ARM64 |
| 287 | return EM_AARCH64; |
| 288 | #else |
| 289 | UNREACHABLE(); |
| 290 | return 0; |
| 291 | #endif |
| 292 | } |
| 293 | |
| 294 | #if ARCH_IS_64_BIT |
| 295 | static const int = 0x40; |
| 296 | #else |
| 297 | static const int kElfHeaderSize = 0x34; |
| 298 | #endif |
| 299 | |
| 300 | void (uword base, const CodeComments* ) { |
| 301 | if (comments == nullptr || comments->Length() == 0) { |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | // Open the comments file for the given code object. |
| 306 | // Note: for some reason we can't emit all comments into a single file |
| 307 | // the mapping between PCs and lines goes out of sync (might be |
| 308 | // perf-annotate bug). |
| 309 | char* = |
| 310 | OS::SCreate(nullptr, "/tmp/jit-%" Pd "-%" Pd ".cmts" , pid_, code_id_); |
| 311 | const intptr_t filename_length = strlen(comments_file_name); |
| 312 | FILE* = fopen(comments_file_name, "w" ); |
| 313 | setvbuf(comments_file, nullptr, _IOFBF, 2 * MB); |
| 314 | |
| 315 | // Count the number of DebugInfoEntry we are going to emit: one |
| 316 | // per PC. |
| 317 | intptr_t entry_count = 0; |
| 318 | for (uint64_t i = 0, len = comments->Length(); i < len;) { |
| 319 | const intptr_t pc_offset = comments->PCOffsetAt(i); |
| 320 | while (i < len && comments->PCOffsetAt(i) == pc_offset) { |
| 321 | i++; |
| 322 | } |
| 323 | entry_count++; |
| 324 | } |
| 325 | |
| 326 | DebugInfoEvent info; |
| 327 | info.event = BaseEvent::kDebugInfo; |
| 328 | info.time_stamp = OS::GetCurrentMonotonicTicks(); |
| 329 | info.address = base; |
| 330 | info.entry_count = entry_count; |
| 331 | info.size = sizeof(info) + |
| 332 | entry_count * (sizeof(DebugInfoEntry) + filename_length + 1); |
| 333 | const int32_t padding = Utils::RoundUp(info.size, 8) - info.size; |
| 334 | info.size += padding; |
| 335 | |
| 336 | // Write out DebugInfoEvent record followed by entry_count DebugInfoEntry |
| 337 | // records. |
| 338 | WriteFully(&info, sizeof(info)); |
| 339 | intptr_t line_number = 0; // Line number within comments_file. |
| 340 | for (intptr_t i = 0, len = comments->Length(); i < len;) { |
| 341 | const intptr_t pc_offset = comments->PCOffsetAt(i); |
| 342 | while (i < len && comments->PCOffsetAt(i) == pc_offset) { |
| 343 | line_number += WriteLn(comments_file, comments->CommentAt(i)); |
| 344 | i++; |
| 345 | } |
| 346 | DebugInfoEntry entry; |
| 347 | entry.address = base + pc_offset + kElfHeaderSize; |
| 348 | entry.line_number = line_number; |
| 349 | entry.column = 0; |
| 350 | WriteFully(&entry, sizeof(entry)); |
| 351 | WriteFully(comments_file_name, filename_length + 1); |
| 352 | } |
| 353 | |
| 354 | // Write out the padding. |
| 355 | const char padding_bytes[8] = {0}; |
| 356 | WriteFully(padding_bytes, padding); |
| 357 | |
| 358 | fclose(comments_file); |
| 359 | free(comments_file_name); |
| 360 | } |
| 361 | |
| 362 | void () { |
| 363 | Header ; |
| 364 | header.elf_mach_target = GetElfMachineArchitecture(); |
| 365 | header.process_id = getpid(); |
| 366 | header.time_stamp = OS::GetCurrentTimeMicros(); |
| 367 | WriteFully(&header, sizeof(header)); |
| 368 | } |
| 369 | |
| 370 | // Returns number of new-lines written. |
| 371 | intptr_t WriteLn(FILE* f, const char* ) { |
| 372 | fputs(comment, f); |
| 373 | fputc('\n', f); |
| 374 | |
| 375 | intptr_t line_count = 1; |
| 376 | while ((comment = strstr(comment, "\n" )) != nullptr) { |
| 377 | line_count++; |
| 378 | } |
| 379 | return line_count; |
| 380 | } |
| 381 | |
| 382 | void WriteFully(const void* buffer, size_t size) { |
| 383 | const char* ptr = static_cast<const char*>(buffer); |
| 384 | while (size > 0) { |
| 385 | const size_t written = fwrite(ptr, 1, size, out_file_); |
| 386 | if (written == 0) { |
| 387 | UNREACHABLE(); |
| 388 | break; |
| 389 | } |
| 390 | size -= written; |
| 391 | ptr += written; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | const intptr_t pid_; |
| 396 | |
| 397 | FILE* out_file_ = nullptr; |
| 398 | void* mapped_ = nullptr; |
| 399 | long mapped_size_ = 0; // NOLINT(runtime/int) |
| 400 | |
| 401 | intptr_t code_id_ = 0; |
| 402 | |
| 403 | DISALLOW_COPY_AND_ASSIGN(JitDumpCodeObserver); |
| 404 | }; |
| 405 | |
| 406 | #endif // !PRODUCT |
| 407 | |
| 408 | const char* OS::Name() { |
| 409 | return "linux" ; |
| 410 | } |
| 411 | |
| 412 | intptr_t OS::ProcessId() { |
| 413 | return static_cast<intptr_t>(getpid()); |
| 414 | } |
| 415 | |
| 416 | static bool LocalTime(int64_t seconds_since_epoch, tm* tm_result) { |
| 417 | time_t seconds = static_cast<time_t>(seconds_since_epoch); |
| 418 | if (seconds != seconds_since_epoch) return false; |
| 419 | struct tm* error_code = localtime_r(&seconds, tm_result); |
| 420 | return error_code != NULL; |
| 421 | } |
| 422 | |
| 423 | const char* OS::GetTimeZoneName(int64_t seconds_since_epoch) { |
| 424 | tm decomposed; |
| 425 | bool succeeded = LocalTime(seconds_since_epoch, &decomposed); |
| 426 | // If unsuccessful, return an empty string like V8 does. |
| 427 | return (succeeded && (decomposed.tm_zone != NULL)) ? decomposed.tm_zone : "" ; |
| 428 | } |
| 429 | |
| 430 | int OS::GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch) { |
| 431 | tm decomposed; |
| 432 | bool succeeded = LocalTime(seconds_since_epoch, &decomposed); |
| 433 | // Even if the offset was 24 hours it would still easily fit into 32 bits. |
| 434 | // If unsuccessful, return zero like V8 does. |
| 435 | return succeeded ? static_cast<int>(decomposed.tm_gmtoff) : 0; |
| 436 | } |
| 437 | |
| 438 | int OS::GetLocalTimeZoneAdjustmentInSeconds() { |
| 439 | // TODO(floitsch): avoid excessive calls to tzset? |
| 440 | tzset(); |
| 441 | // Even if the offset was 24 hours it would still easily fit into 32 bits. |
| 442 | // Note that Unix and Dart disagree on the sign. |
| 443 | return static_cast<int>(-timezone); |
| 444 | } |
| 445 | |
| 446 | int64_t OS::GetCurrentTimeMillis() { |
| 447 | return GetCurrentTimeMicros() / 1000; |
| 448 | } |
| 449 | |
| 450 | int64_t OS::GetCurrentTimeMicros() { |
| 451 | // gettimeofday has microsecond resolution. |
| 452 | struct timeval tv; |
| 453 | if (gettimeofday(&tv, NULL) < 0) { |
| 454 | UNREACHABLE(); |
| 455 | return 0; |
| 456 | } |
| 457 | return (static_cast<int64_t>(tv.tv_sec) * 1000000) + tv.tv_usec; |
| 458 | } |
| 459 | |
| 460 | int64_t OS::GetCurrentMonotonicTicks() { |
| 461 | struct timespec ts; |
| 462 | if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { |
| 463 | UNREACHABLE(); |
| 464 | return 0; |
| 465 | } |
| 466 | // Convert to nanoseconds. |
| 467 | int64_t result = ts.tv_sec; |
| 468 | result *= kNanosecondsPerSecond; |
| 469 | result += ts.tv_nsec; |
| 470 | return result; |
| 471 | } |
| 472 | |
| 473 | int64_t OS::GetCurrentMonotonicFrequency() { |
| 474 | return kNanosecondsPerSecond; |
| 475 | } |
| 476 | |
| 477 | int64_t OS::GetCurrentMonotonicMicros() { |
| 478 | int64_t ticks = GetCurrentMonotonicTicks(); |
| 479 | ASSERT(GetCurrentMonotonicFrequency() == kNanosecondsPerSecond); |
| 480 | return ticks / kNanosecondsPerMicrosecond; |
| 481 | } |
| 482 | |
| 483 | int64_t OS::GetCurrentThreadCPUMicros() { |
| 484 | struct timespec ts; |
| 485 | if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) != 0) { |
| 486 | UNREACHABLE(); |
| 487 | return -1; |
| 488 | } |
| 489 | int64_t result = ts.tv_sec; |
| 490 | result *= kMicrosecondsPerSecond; |
| 491 | result += (ts.tv_nsec / kNanosecondsPerMicrosecond); |
| 492 | return result; |
| 493 | } |
| 494 | |
| 495 | int64_t OS::GetCurrentThreadCPUMicrosForTimeline() { |
| 496 | return OS::GetCurrentThreadCPUMicros(); |
| 497 | } |
| 498 | |
| 499 | // TODO(5411554): May need to hoist these architecture dependent code |
| 500 | // into a architecture specific file e.g: os_ia32_linux.cc |
| 501 | intptr_t OS::ActivationFrameAlignment() { |
| 502 | #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64) || \ |
| 503 | defined(TARGET_ARCH_ARM64) |
| 504 | const int kMinimumAlignment = 16; |
| 505 | #elif defined(TARGET_ARCH_ARM) |
| 506 | const int kMinimumAlignment = 8; |
| 507 | #else |
| 508 | #error Unsupported architecture. |
| 509 | #endif |
| 510 | intptr_t alignment = kMinimumAlignment; |
| 511 | // TODO(5411554): Allow overriding default stack alignment for |
| 512 | // testing purposes. |
| 513 | // Flags::DebugIsInt("stackalign", &alignment); |
| 514 | ASSERT(Utils::IsPowerOfTwo(alignment)); |
| 515 | ASSERT(alignment >= kMinimumAlignment); |
| 516 | return alignment; |
| 517 | } |
| 518 | |
| 519 | int OS::NumberOfAvailableProcessors() { |
| 520 | return sysconf(_SC_NPROCESSORS_ONLN); |
| 521 | } |
| 522 | |
| 523 | void OS::Sleep(int64_t millis) { |
| 524 | int64_t micros = millis * kMicrosecondsPerMillisecond; |
| 525 | SleepMicros(micros); |
| 526 | } |
| 527 | |
| 528 | void OS::SleepMicros(int64_t micros) { |
| 529 | struct timespec req; // requested. |
| 530 | struct timespec rem; // remainder. |
| 531 | int64_t seconds = micros / kMicrosecondsPerSecond; |
| 532 | micros = micros - seconds * kMicrosecondsPerSecond; |
| 533 | int64_t nanos = micros * kNanosecondsPerMicrosecond; |
| 534 | req.tv_sec = seconds; |
| 535 | req.tv_nsec = nanos; |
| 536 | while (true) { |
| 537 | int r = nanosleep(&req, &rem); |
| 538 | if (r == 0) { |
| 539 | break; |
| 540 | } |
| 541 | // We should only ever see an interrupt error. |
| 542 | ASSERT(errno == EINTR); |
| 543 | // Copy remainder into requested and repeat. |
| 544 | req = rem; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // TODO(regis): Function called only from the simulator. |
| 549 | void OS::DebugBreak() { |
| 550 | __builtin_trap(); |
| 551 | } |
| 552 | |
| 553 | DART_NOINLINE uintptr_t OS::GetProgramCounter() { |
| 554 | return reinterpret_cast<uintptr_t>( |
| 555 | __builtin_extract_return_addr(__builtin_return_address(0))); |
| 556 | } |
| 557 | |
| 558 | void OS::Print(const char* format, ...) { |
| 559 | va_list args; |
| 560 | va_start(args, format); |
| 561 | VFPrint(stdout, format, args); |
| 562 | va_end(args); |
| 563 | } |
| 564 | |
| 565 | void OS::VFPrint(FILE* stream, const char* format, va_list args) { |
| 566 | vfprintf(stream, format, args); |
| 567 | fflush(stream); |
| 568 | } |
| 569 | |
| 570 | char* OS::SCreate(Zone* zone, const char* format, ...) { |
| 571 | va_list args; |
| 572 | va_start(args, format); |
| 573 | char* buffer = VSCreate(zone, format, args); |
| 574 | va_end(args); |
| 575 | return buffer; |
| 576 | } |
| 577 | |
| 578 | char* OS::VSCreate(Zone* zone, const char* format, va_list args) { |
| 579 | // Measure. |
| 580 | va_list measure_args; |
| 581 | va_copy(measure_args, args); |
| 582 | intptr_t len = Utils::VSNPrint(NULL, 0, format, measure_args); |
| 583 | va_end(measure_args); |
| 584 | |
| 585 | char* buffer; |
| 586 | if (zone != nullptr) { |
| 587 | buffer = zone->Alloc<char>(len + 1); |
| 588 | } else { |
| 589 | buffer = reinterpret_cast<char*>(malloc(len + 1)); |
| 590 | } |
| 591 | ASSERT(buffer != NULL); |
| 592 | |
| 593 | // Print. |
| 594 | va_list print_args; |
| 595 | va_copy(print_args, args); |
| 596 | Utils::VSNPrint(buffer, len + 1, format, print_args); |
| 597 | va_end(print_args); |
| 598 | return buffer; |
| 599 | } |
| 600 | |
| 601 | bool OS::StringToInt64(const char* str, int64_t* value) { |
| 602 | ASSERT(str != NULL && strlen(str) > 0 && value != NULL); |
| 603 | int32_t base = 10; |
| 604 | char* endptr; |
| 605 | int i = 0; |
| 606 | if (str[0] == '-') { |
| 607 | i = 1; |
| 608 | } else if (str[0] == '+') { |
| 609 | i = 1; |
| 610 | } |
| 611 | if ((str[i] == '0') && (str[i + 1] == 'x' || str[i + 1] == 'X') && |
| 612 | (str[i + 2] != '\0')) { |
| 613 | base = 16; |
| 614 | } |
| 615 | errno = 0; |
| 616 | if (base == 16) { |
| 617 | // Unsigned 64-bit hexadecimal integer literals are allowed but |
| 618 | // immediately interpreted as signed 64-bit integers. |
| 619 | *value = static_cast<int64_t>(strtoull(str, &endptr, base)); |
| 620 | } else { |
| 621 | *value = strtoll(str, &endptr, base); |
| 622 | } |
| 623 | return ((errno == 0) && (endptr != str) && (*endptr == 0)); |
| 624 | } |
| 625 | |
| 626 | void OS::RegisterCodeObservers() { |
| 627 | #ifndef PRODUCT |
| 628 | if (FLAG_generate_perf_events_symbols) { |
| 629 | CodeObservers::Register(new PerfCodeObserver); |
| 630 | } |
| 631 | |
| 632 | if (FLAG_generate_perf_jitdump) { |
| 633 | CodeObservers::Register(new JitDumpCodeObserver); |
| 634 | } |
| 635 | #endif // !PRODUCT |
| 636 | } |
| 637 | |
| 638 | void OS::PrintErr(const char* format, ...) { |
| 639 | va_list args; |
| 640 | va_start(args, format); |
| 641 | VFPrint(stderr, format, args); |
| 642 | va_end(args); |
| 643 | } |
| 644 | |
| 645 | void OS::Init() {} |
| 646 | |
| 647 | void OS::Cleanup() {} |
| 648 | |
| 649 | void OS::PrepareToAbort() {} |
| 650 | |
| 651 | void OS::Abort() { |
| 652 | PrepareToAbort(); |
| 653 | abort(); |
| 654 | } |
| 655 | |
| 656 | void OS::Exit(int code) { |
| 657 | exit(code); |
| 658 | } |
| 659 | |
| 660 | } // namespace dart |
| 661 | |
| 662 | #endif // defined(HOST_OS_LINUX) |
| 663 | |