2012-01-20 16:17:08 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2009-12-02 13:38:50 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2012-01-05 12:17:08 +00:00
|
|
|
// Platform specific code for OpenBSD and NetBSD goes here. For the POSIX
|
|
|
|
// comaptible parts the implementation is in platform-posix.cc.
|
2009-12-02 13:38:50 +00:00
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2011-11-09 12:01:45 +00:00
|
|
|
#include <sys/syscall.h>
|
2009-12-02 13:38:50 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <sys/types.h> // mmap & munmap
|
|
|
|
#include <sys/mman.h> // mmap & munmap
|
|
|
|
#include <sys/stat.h> // open
|
2011-11-09 12:01:45 +00:00
|
|
|
#include <fcntl.h> // open
|
|
|
|
#include <unistd.h> // sysconf
|
2009-12-02 13:38:50 +00:00
|
|
|
#include <execinfo.h> // backtrace, backtrace_symbols
|
|
|
|
#include <strings.h> // index
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#undef MAP_TYPE
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
2012-03-30 14:30:46 +00:00
|
|
|
#include "platform-posix.h"
|
2009-12-02 13:38:50 +00:00
|
|
|
#include "platform.h"
|
2011-11-09 12:01:45 +00:00
|
|
|
#include "v8threads.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "vm-state-inl.h"
|
2009-12-02 13:38:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
2010-01-25 12:37:31 +00:00
|
|
|
const char* OS::LocalTimezone(double time) {
|
2013-04-19 13:26:47 +00:00
|
|
|
if (std::isnan(time)) return "";
|
2010-01-25 12:37:31 +00:00
|
|
|
time_t tv = static_cast<time_t>(floor(time/msPerSecond));
|
|
|
|
struct tm* t = localtime(&tv);
|
|
|
|
if (NULL == t) return "";
|
|
|
|
return t->tm_zone;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double OS::LocalTimeOffset() {
|
|
|
|
time_t tv = time(NULL);
|
|
|
|
struct tm* t = localtime(&tv);
|
|
|
|
// tm_gmtoff includes any daylight savings offset, so subtract it.
|
|
|
|
return static_cast<double>(t->tm_gmtoff * msPerSecond -
|
|
|
|
(t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-14 14:27:06 +00:00
|
|
|
void OS::DumpBacktrace() {
|
|
|
|
// Currently unsupported.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
class PosixMemoryMappedFile : public OS::MemoryMappedFile {
|
|
|
|
public:
|
|
|
|
PosixMemoryMappedFile(FILE* file, void* memory, int size)
|
|
|
|
: file_(file), memory_(memory), size_(size) { }
|
|
|
|
virtual ~PosixMemoryMappedFile();
|
|
|
|
virtual void* memory() { return memory_; }
|
2011-01-19 14:28:15 +00:00
|
|
|
virtual int size() { return size_; }
|
2009-12-02 13:38:50 +00:00
|
|
|
private:
|
|
|
|
FILE* file_;
|
|
|
|
void* memory_;
|
|
|
|
int size_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-19 14:28:15 +00:00
|
|
|
OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
|
2011-02-21 17:17:26 +00:00
|
|
|
FILE* file = fopen(name, "r+");
|
2011-01-19 14:28:15 +00:00
|
|
|
if (file == NULL) return NULL;
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
int size = ftell(file);
|
|
|
|
|
|
|
|
void* memory =
|
|
|
|
mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
|
|
|
|
return new PosixMemoryMappedFile(file, memory, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
|
|
|
|
void* initial) {
|
|
|
|
FILE* file = fopen(name, "w+");
|
|
|
|
if (file == NULL) return NULL;
|
|
|
|
int result = fwrite(initial, size, 1, file);
|
|
|
|
if (result < 1) {
|
|
|
|
fclose(file);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
void* memory =
|
|
|
|
mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
|
|
|
|
return new PosixMemoryMappedFile(file, memory, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PosixMemoryMappedFile::~PosixMemoryMappedFile() {
|
2011-09-29 12:23:05 +00:00
|
|
|
if (memory_) OS::Free(memory_, size_);
|
2009-12-02 13:38:50 +00:00
|
|
|
fclose(file_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-11 10:59:39 +00:00
|
|
|
void OS::LogSharedLibraryAddresses(Isolate* isolate) {
|
2011-11-09 12:01:45 +00:00
|
|
|
// This function assumes that the layout of the file is as follows:
|
|
|
|
// hex_start_addr-hex_end_addr rwxp <unused data> [binary_file_name]
|
|
|
|
// If we encounter an unexpected situation we abort scanning further entries.
|
|
|
|
FILE* fp = fopen("/proc/self/maps", "r");
|
|
|
|
if (fp == NULL) return;
|
|
|
|
|
|
|
|
// Allocate enough room to be able to store a full file name.
|
|
|
|
const int kLibNameLen = FILENAME_MAX + 1;
|
|
|
|
char* lib_name = reinterpret_cast<char*>(malloc(kLibNameLen));
|
|
|
|
|
|
|
|
// This loop will terminate once the scanning hits an EOF.
|
2009-12-02 13:38:50 +00:00
|
|
|
while (true) {
|
2011-11-09 12:01:45 +00:00
|
|
|
uintptr_t start, end;
|
|
|
|
char attr_r, attr_w, attr_x, attr_p;
|
|
|
|
// Parse the addresses and permission bits at the beginning of the line.
|
|
|
|
if (fscanf(fp, "%" V8PRIxPTR "-%" V8PRIxPTR, &start, &end) != 2) break;
|
|
|
|
if (fscanf(fp, " %c%c%c%c", &attr_r, &attr_w, &attr_x, &attr_p) != 4) break;
|
|
|
|
|
|
|
|
int c;
|
|
|
|
if (attr_r == 'r' && attr_w != 'w' && attr_x == 'x') {
|
|
|
|
// Found a read-only executable entry. Skip characters until we reach
|
|
|
|
// the beginning of the filename or the end of the line.
|
|
|
|
do {
|
|
|
|
c = getc(fp);
|
|
|
|
} while ((c != EOF) && (c != '\n') && (c != '/'));
|
|
|
|
if (c == EOF) break; // EOF: Was unexpected, just exit.
|
|
|
|
|
|
|
|
// Process the filename if found.
|
|
|
|
if (c == '/') {
|
|
|
|
ungetc(c, fp); // Push the '/' back into the stream to be read below.
|
|
|
|
|
|
|
|
// Read to the end of the line. Exit if the read fails.
|
|
|
|
if (fgets(lib_name, kLibNameLen, fp) == NULL) break;
|
|
|
|
|
|
|
|
// Drop the newline character read by fgets. We do not need to check
|
|
|
|
// for a zero-length string because we know that we at least read the
|
|
|
|
// '/' character.
|
|
|
|
lib_name[strlen(lib_name) - 1] = '\0';
|
|
|
|
} else {
|
|
|
|
// No library name found, just record the raw address range.
|
|
|
|
snprintf(lib_name, kLibNameLen,
|
|
|
|
"%08" V8PRIxPTR "-%08" V8PRIxPTR, start, end);
|
|
|
|
}
|
|
|
|
LOG(isolate, SharedLibraryEvent(lib_name, start, end));
|
|
|
|
} else {
|
2012-01-13 13:09:52 +00:00
|
|
|
// Entry not describing executable data. Skip to end of line to set up
|
2011-11-09 12:01:45 +00:00
|
|
|
// reading the next entry.
|
|
|
|
do {
|
|
|
|
c = getc(fp);
|
|
|
|
} while ((c != EOF) && (c != '\n'));
|
|
|
|
if (c == EOF) break;
|
|
|
|
}
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
2011-11-09 12:01:45 +00:00
|
|
|
free(lib_name);
|
|
|
|
fclose(fp);
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-19 16:45:11 +00:00
|
|
|
void OS::SignalCodeMovingGC() {
|
2011-11-09 12:01:45 +00:00
|
|
|
// Support for ll_prof.py.
|
|
|
|
//
|
|
|
|
// The Linux profiler built into the kernel logs all mmap's with
|
|
|
|
// PROT_EXEC so that analysis tools can properly attribute ticks. We
|
|
|
|
// do a mmap with a name known by ll_prof.py and immediately munmap
|
|
|
|
// it. This injects a GC marker into the stream of events generated
|
|
|
|
// by the kernel and allows us to synchronize V8 code log and the
|
|
|
|
// kernel log.
|
|
|
|
int size = sysconf(_SC_PAGESIZE);
|
2012-09-06 15:01:20 +00:00
|
|
|
FILE* f = fopen(FLAG_gc_fake_mmap, "w+");
|
2013-05-29 19:45:19 +00:00
|
|
|
if (f == NULL) {
|
|
|
|
OS::PrintError("Failed to open %s\n", FLAG_gc_fake_mmap);
|
|
|
|
OS::Abort();
|
|
|
|
}
|
2011-11-09 12:01:45 +00:00
|
|
|
void* addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE,
|
|
|
|
fileno(f), 0);
|
|
|
|
ASSERT(addr != MAP_FAILED);
|
|
|
|
OS::Free(addr, size);
|
|
|
|
fclose(f);
|
2010-10-19 16:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-02 13:38:50 +00:00
|
|
|
int OS::StackWalk(Vector<OS::StackFrame> frames) {
|
2011-11-09 12:01:45 +00:00
|
|
|
// backtrace is a glibc extension.
|
2011-06-01 11:09:32 +00:00
|
|
|
int frames_size = frames.length();
|
|
|
|
ScopedVector<void*> addresses(frames_size);
|
|
|
|
|
|
|
|
int frames_count = backtrace(addresses.start(), frames_size);
|
|
|
|
|
|
|
|
char** symbols = backtrace_symbols(addresses.start(), frames_count);
|
|
|
|
if (symbols == NULL) {
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
|
|
|
free(symbols);
|
|
|
|
|
|
|
|
return frames_count;
|
2009-12-02 13:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|