1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Platform-specific code for FreeBSD goes here. For the POSIX-compatible
6 // parts, the implementation is in platform-posix.cc.
7 
8 #include <pthread.h>
9 #include <semaphore.h>
10 #include <signal.h>
11 #include <stdlib.h>
12 #include <sys/resource.h>
13 #include <sys/time.h>
14 #include <sys/types.h>
15 #include <sys/ucontext.h>
16 
17 #include <sys/fcntl.h>  // open
18 #include <sys/mman.h>   // mmap & munmap
19 #include <sys/stat.h>   // open
20 #include <unistd.h>     // getpagesize
21 // If you don't have execinfo.h then you need devel/libexecinfo from ports.
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdarg.h>
25 #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 
CreateTimezoneCache()39 TimezoneCache* OS::CreateTimezoneCache() {
40   return new PosixDefaultTimezoneCache();
41 }
42 
StringToLong(char * buffer)43 static unsigned StringToLong(char* buffer) {
44   return static_cast<unsigned>(strtol(buffer, nullptr, 16));  // NOLINT
45 }
46 
GetSharedLibraryAddresses()47 std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
48   std::vector<SharedLibraryAddress> result;
49   static const int MAP_LENGTH = 1024;
50   int fd = open("/proc/self/maps", O_RDONLY);
51   if (fd < 0) return result;
52   while (true) {
53     char addr_buffer[11];
54     addr_buffer[0] = '0';
55     addr_buffer[1] = 'x';
56     addr_buffer[10] = 0;
57     ssize_t bytes_read = read(fd, addr_buffer + 2, 8);
58     if (bytes_read < 8) break;
59     unsigned start = StringToLong(addr_buffer);
60     bytes_read = read(fd, addr_buffer + 2, 1);
61     if (bytes_read < 1) break;
62     if (addr_buffer[2] != '-') break;
63     bytes_read = read(fd, addr_buffer + 2, 8);
64     if (bytes_read < 8) break;
65     unsigned end = StringToLong(addr_buffer);
66     char buffer[MAP_LENGTH];
67     bytes_read = -1;
68     do {
69       bytes_read++;
70       if (bytes_read >= MAP_LENGTH - 1) break;
71       bytes_read = read(fd, buffer + bytes_read, 1);
72       if (bytes_read < 1) break;
73     } while (buffer[bytes_read] != '\n');
74     buffer[bytes_read] = 0;
75     // Ignore mappings that are not executable.
76     if (buffer[3] != 'x') continue;
77     char* start_of_path = index(buffer, '/');
78     // There may be no filename in this line.  Skip to next.
79     if (start_of_path == nullptr) continue;
80     buffer[bytes_read] = 0;
81     result.push_back(SharedLibraryAddress(start_of_path, start, end));
82   }
83   close(fd);
84   return result;
85 }
86 
SignalCodeMovingGC()87 void OS::SignalCodeMovingGC() {}
88 
89 }  // namespace base
90 }  // namespace v8
91