📄 platform-linux.cc.svn-base
字号:
char* start_of_path = index(buffer, '/'); // There may be no filename in this line. Skip to next. if (start_of_path == NULL) continue; buffer[bytes_read] = 0; LOG(SharedLibraryEvent(start_of_path, start, end)); } close(fd);#endif}int OS::StackWalk(OS::StackFrame* frames, int frames_size) { void** addresses = NewArray<void*>(frames_size); int frames_count = backtrace(addresses, frames_size); char** symbols; symbols = backtrace_symbols(addresses, frames_count); if (symbols == NULL) { DeleteArray(addresses); return kStackWalkError; } for (int i = 0; i < frames_count; i++) { frames[i].address = addresses[i]; // Format a text representation of the frame based on the information // available. SNPrintF(MutableCStrVector(frames[i].text, kStackWalkMaxTextLen), "%s", symbols[i]); // Make sure line termination is in place. frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; } DeleteArray(addresses); free(symbols); return frames_count;}// Constants used for mmap.static const int kMmapFd = -1;static const int kMmapFdOffset = 0;VirtualMemory::VirtualMemory(size_t size) { address_ = mmap(NULL, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, kMmapFd, kMmapFdOffset); size_ = size;}VirtualMemory::~VirtualMemory() { if (IsReserved()) { if (0 == munmap(address(), size())) address_ = MAP_FAILED; }}bool VirtualMemory::IsReserved() { return address_ != MAP_FAILED;}bool VirtualMemory::Commit(void* address, size_t size, bool executable) { int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0); if (MAP_FAILED == mmap(address, size, prot, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, kMmapFd, kMmapFdOffset)) { return false; } UpdateAllocatedSpaceLimits(address, size); return true;}bool VirtualMemory::Uncommit(void* address, size_t size) { return mmap(address, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, kMmapFd, kMmapFdOffset) != MAP_FAILED;}class ThreadHandle::PlatformData : public Malloced { public: explicit PlatformData(ThreadHandle::Kind kind) { Initialize(kind); } void Initialize(ThreadHandle::Kind kind) { switch (kind) { case ThreadHandle::SELF: thread_ = pthread_self(); break; case ThreadHandle::INVALID: thread_ = kNoThread; break; } } pthread_t thread_; // Thread handle for pthread.};ThreadHandle::ThreadHandle(Kind kind) { data_ = new PlatformData(kind);}void ThreadHandle::Initialize(ThreadHandle::Kind kind) { data_->Initialize(kind);}ThreadHandle::~ThreadHandle() { delete data_;}bool ThreadHandle::IsSelf() const { return pthread_equal(data_->thread_, pthread_self());}bool ThreadHandle::IsValid() const { return data_->thread_ != kNoThread;}Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) {}Thread::~Thread() {}static void* ThreadEntry(void* arg) { Thread* thread = reinterpret_cast<Thread*>(arg); // This is also initialized by the first argument to pthread_create() but we // don't know which thread will run first (the original thread or the new // one) so we initialize it here too. thread->thread_handle_data()->thread_ = pthread_self(); ASSERT(thread->IsValid()); thread->Run(); return NULL;}void Thread::Start() { pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this); ASSERT(IsValid());}void Thread::Join() { pthread_join(thread_handle_data()->thread_, NULL);}Thread::LocalStorageKey Thread::CreateThreadLocalKey() { pthread_key_t key; int result = pthread_key_create(&key, NULL); USE(result); ASSERT(result == 0); return static_cast<LocalStorageKey>(key);}void Thread::DeleteThreadLocalKey(LocalStorageKey key) { pthread_key_t pthread_key = static_cast<pthread_key_t>(key); int result = pthread_key_delete(pthread_key); USE(result); ASSERT(result == 0);}void* Thread::GetThreadLocal(LocalStorageKey key) { pthread_key_t pthread_key = static_cast<pthread_key_t>(key); return pthread_getspecific(pthread_key);}void Thread::SetThreadLocal(LocalStorageKey key, void* value) { pthread_key_t pthread_key = static_cast<pthread_key_t>(key); pthread_setspecific(pthread_key, value);}void Thread::YieldCPU() { sched_yield();}class LinuxMutex : public Mutex { public: LinuxMutex() { pthread_mutexattr_t attrs; int result = pthread_mutexattr_init(&attrs); ASSERT(result == 0); result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE); ASSERT(result == 0); result = pthread_mutex_init(&mutex_, &attrs); ASSERT(result == 0); } virtual ~LinuxMutex() { pthread_mutex_destroy(&mutex_); } virtual int Lock() { int result = pthread_mutex_lock(&mutex_); return result; } virtual int Unlock() { int result = pthread_mutex_unlock(&mutex_); return result; } private: pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.};Mutex* OS::CreateMutex() { return new LinuxMutex();}class LinuxSemaphore : public Semaphore { public: explicit LinuxSemaphore(int count) { sem_init(&sem_, 0, count); } virtual ~LinuxSemaphore() { sem_destroy(&sem_); } virtual void Wait(); virtual void Signal() { sem_post(&sem_); } private: sem_t sem_;};void LinuxSemaphore::Wait() { while (true) { int result = sem_wait(&sem_); if (result == 0) return; // Successfully got semaphore. CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup. }}Semaphore* OS::CreateSemaphore(int count) { return new LinuxSemaphore(count);}#ifdef ENABLE_LOGGING_AND_PROFILINGstatic Sampler* active_sampler_ = NULL;static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { USE(info); if (signal != SIGPROF) return; if (active_sampler_ == NULL) return; TickSample sample; // If profiling, we extract the current pc and sp. if (active_sampler_->IsProfiling()) { // Extracting the sample from the context is extremely machine dependent. ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); mcontext_t& mcontext = ucontext->uc_mcontext;#if defined (__arm__) || defined(__thumb__) sample.pc = mcontext.gregs[R15]; sample.sp = mcontext.gregs[R13];#else sample.pc = mcontext.gregs[REG_EIP]; sample.sp = mcontext.gregs[REG_ESP];#endif } // We always sample the VM state. sample.state = Logger::state(); active_sampler_->Tick(&sample);}class Sampler::PlatformData : public Malloced { public: PlatformData() { signal_handler_installed_ = false; } bool signal_handler_installed_; struct sigaction old_signal_handler_; struct itimerval old_timer_value_;};Sampler::Sampler(int interval, bool profiling) : interval_(interval), profiling_(profiling), active_(false) { data_ = new PlatformData();}Sampler::~Sampler() { delete data_;}void Sampler::Start() { // There can only be one active sampler at the time on POSIX // platforms. if (active_sampler_ != NULL) return; // Request profiling signals. struct sigaction sa; sa.sa_sigaction = ProfilerSignalHandler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_SIGINFO; if (sigaction(SIGPROF, &sa, &data_->old_signal_handler_) != 0) return; data_->signal_handler_installed_ = true; // Set the itimer to generate a tick for each interval. itimerval itimer; itimer.it_interval.tv_sec = interval_ / 1000; itimer.it_interval.tv_usec = (interval_ % 1000) * 1000; itimer.it_value.tv_sec = itimer.it_interval.tv_sec; itimer.it_value.tv_usec = itimer.it_interval.tv_usec; setitimer(ITIMER_PROF, &itimer, &data_->old_timer_value_); // Set this sampler as the active sampler. active_sampler_ = this; active_ = true;}void Sampler::Stop() { // Restore old signal handler if (data_->signal_handler_installed_) { setitimer(ITIMER_PROF, &data_->old_timer_value_, NULL); sigaction(SIGPROF, &data_->old_signal_handler_, 0); data_->signal_handler_installed_ = false; } // This sampler is no longer the active sampler. active_sampler_ = NULL; active_ = false;}#endif // ENABLE_LOGGING_AND_PROFILING} } // namespace v8::internal
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -