platform-freebsd.cc 2.66 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
// Platform-specific code for FreeBSD goes here. For the POSIX-compatible
// parts, the implementation is in platform-posix.cc.
7 8 9 10

#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
11
#include <stdlib.h>
12
#include <sys/resource.h>
13
#include <sys/time.h>
14
#include <sys/types.h>
15 16
#include <sys/ucontext.h>

17
#include <sys/fcntl.h>  // open
18 19 20
#include <sys/mman.h>   // mmap & munmap
#include <sys/stat.h>   // open
#include <unistd.h>     // getpagesize
21
// If you don't have execinfo.h then you need devel/libexecinfo from ports.
22 23
#include <errno.h>
#include <limits.h>
24 25
#include <stdarg.h>
#include <strings.h>    // index
26

27
#include <cmath>
28

29
#undef MAP_TYPE
30

31
#include "src/base/macros.h"
32
#include "src/base/platform/platform-posix-time.h"
33
#include "src/base/platform/platform-posix.h"
34
#include "src/base/platform/platform.h"
35

36
namespace v8 {
37
namespace base {
38

39 40 41
TimezoneCache* OS::CreateTimezoneCache() {
  return new PosixDefaultTimezoneCache();
}
42

43
static unsigned StringToLong(char* buffer) {
44
  return static_cast<unsigned>(strtol(buffer, nullptr, 16));  // NOLINT
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
}

std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
  std::vector<SharedLibraryAddress> result;
  static const int MAP_LENGTH = 1024;
  int fd = open("/proc/self/maps", O_RDONLY);
  if (fd < 0) return result;
  while (true) {
    char addr_buffer[11];
    addr_buffer[0] = '0';
    addr_buffer[1] = 'x';
    addr_buffer[10] = 0;
    ssize_t bytes_read = read(fd, addr_buffer + 2, 8);
    if (bytes_read < 8) break;
    unsigned start = StringToLong(addr_buffer);
    bytes_read = read(fd, addr_buffer + 2, 1);
    if (bytes_read < 1) break;
    if (addr_buffer[2] != '-') break;
    bytes_read = read(fd, addr_buffer + 2, 8);
    if (bytes_read < 8) break;
    unsigned end = StringToLong(addr_buffer);
    char buffer[MAP_LENGTH];
    bytes_read = -1;
    do {
      bytes_read++;
      if (bytes_read >= MAP_LENGTH - 1) break;
      bytes_read = read(fd, buffer + bytes_read, 1);
      if (bytes_read < 1) break;
    } while (buffer[bytes_read] != '\n');
    buffer[bytes_read] = 0;
    // Ignore mappings that are not executable.
    if (buffer[3] != 'x') continue;
    char* start_of_path = index(buffer, '/');
    // There may be no filename in this line.  Skip to next.
79
    if (start_of_path == nullptr) continue;
80 81 82 83 84 85 86
    buffer[bytes_read] = 0;
    result.push_back(SharedLibraryAddress(start_of_path, start, end));
  }
  close(fd);
  return result;
}

87
void OS::SignalCodeMovingGC() {}
88

89 90
void OS::AdjustSchedulingParams() {}

91 92
}  // namespace base
}  // namespace v8