* src/vm/vm.cpp (VM::abort): Removed.
[cacao.git] / src / vm / os.cpp
1 /* src/vm/os.cpp - system (OS) functions
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5    Copyright (C) 2008 Theobroma Systems Ltd.
6
7    This file is part of CACAO.
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2, or (at
12    your option) any later version.
13
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22    02110-1301, USA.
23
24 */
25
26
27 #include "config.h"
28
29 /* NOTE: In this file we check for all system headers, because we wrap
30    all system calls into functions for better portability. */
31
32 #if defined(HAVE_ERRNO_H)
33 # include <errno.h>
34 #endif
35
36 #if defined(HAVE_STDINT_H)
37 # include <stdint.h>
38 #endif
39
40 #if defined(HAVE_STRING_H)
41 # include <string.h>
42 #endif
43
44 #if defined(HAVE_UNISTD_H)
45 # include <unistd.h>
46 #endif
47
48 #if defined(HAVE_SYS_MMAN_H)
49 # include <sys/mman.h>
50 #endif
51
52 #if defined(__DARWIN__)
53 # include <mach/mach.h>
54 # include <mach/mach_host.h>
55 # include <mach/host_info.h>
56 #endif
57
58 /* this should work on BSD */
59 /* #include <sys/sysctl.h> */
60
61 #include "vm/vm.hpp"
62
63
64 /**
65  * Prints an error message and aborts the VM.
66  *
67  * @param text Error message to print.
68  */
69 void os::abort(const char* text, ...)
70 {
71         va_list ap;
72
73         // Print the log message.
74         log_start();
75
76         va_start(ap, text);
77         log_vprint(text, ap);
78         va_end(ap);
79
80         log_finish();
81
82         // Print a backtrace.
83         os::print_backtrace();
84
85         // Now abort the VM.
86         os::abort();
87 }
88
89
90 /**
91  * Prints an error message, appends ":" plus the strerror-message of
92  * errnum and aborts the VM.
93  *
94  * @param errnum Error number.
95  * @param text   Error message to print.
96  */
97 void os::abort_errnum(int errnum, const char* text, ...)
98 {
99         va_list ap;
100
101         // Print the log message.
102         log_start();
103
104         va_start(ap, text);
105         log_vprint(text, ap);
106         va_end(ap);
107
108         // Print the strerror-message of errnum.
109         log_print(": %s", os::strerror(errnum));
110
111         log_finish();
112
113         // Print a backtrace.
114         os::print_backtrace();
115
116         // Now abort the VM.
117         os::abort();
118 }
119
120
121 /**
122  * Equal to abort_errnum, but uses errno to get the error number.
123  *
124  * @param text Error message to print.
125  */
126 void os::abort_errno(const char* text, ...)
127 {
128         va_list ap;
129
130         va_start(ap, text);
131         abort_errnum(errno, text, ap);
132         va_end(ap);
133 }
134
135
136 /**
137  * Maps anonymous memory, even on systems not defining
138  * MAP_ANON(YMOUS).
139  *
140  * @param ...
141  */
142 void* os::mmap_anonymous(void *addr, size_t len, int prot, int flags)
143 {
144         void* p;
145
146 #if defined(MAP_ANON) || defined(MAP_ANONYMOUS)
147         p = mmap(addr, len, prot,
148 # if defined(MAP_ANON)
149                          MAP_ANON | flags,
150 # else
151                          MAP_ANONYMOUS | flags,
152 # endif
153                          -1, 0);
154 #else
155         int fd;
156
157         fd = open("/dev/zero", O_RDONLY, 0);
158
159         if (fd == -1)
160                 os::abort_errno("os::mmap_anonymous: open failed");
161
162         p = mmap(addr, len, prot, flags, fd, 0);
163 #endif
164
165 #if defined(MAP_FAILED)
166         if (p == MAP_FAILED)
167 #else
168         if (p == (void *) -1)
169 #endif
170                 os::abort_errno("os::mmap_anonymous: mmap failed");
171
172         return p;
173 }
174
175
176 /**
177  * Print a C backtrace.
178  */
179 void os::print_backtrace()
180 {
181 #define BACKTRACE_SIZE 100
182         void** array = new void*[SIZEOF_VOID_P * BACKTRACE_SIZE];
183
184         // Get the backtrace.
185         int size = backtrace(array, BACKTRACE_SIZE);
186
187         // Resolve the symbols.
188         char** strings = backtrace_symbols(array, size);
189
190         log_println("Backtrace (%d stack frames):", size);
191
192         for (int i = 0; i < size; i++)
193                 log_println("%s", strings[i]);
194
195         // We have to free the strings.
196         free(strings);
197 }
198
199
200 /**
201  * Returns the number of online processors in the system.
202  *
203  * @return Number of online processors.
204  */
205 int os::processors_online(void)
206 {
207 #if defined(_SC_NPROC_ONLN)
208
209         return (int) sysconf(_SC_NPROC_ONLN);
210
211 #elif defined(_SC_NPROCESSORS_ONLN)
212
213         return (int) sysconf(_SC_NPROCESSORS_ONLN);
214
215 #elif defined(__DARWIN__)
216
217         host_basic_info_data_t hinfo;
218         mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
219         kern_return_t rc;
220
221         rc = host_info(mach_host_self(), HOST_BASIC_INFO,
222                                    (host_info_t) &hinfo, &hinfo_count);
223  
224         if (rc != KERN_SUCCESS) {
225                 return -1;
226         }
227
228         /* XXX michi: according to my infos this should be
229            hinfo.max_cpus, can someone please confirm or deny that? */
230         return (int) hinfo.avail_cpus;
231
232 #elif defined(__FREEBSD__)
233 # error IMPLEMENT ME!
234
235         /* this should work in BSD */
236         /*
237         int ncpu, mib[2], rc;
238         size_t len;
239
240         mib[0] = CTL_HW;
241         mib[1] = HW_NCPU;
242         len = sizeof(ncpu);
243         rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
244
245         return (int32_t) ncpu;
246         */
247
248 #else
249
250         return 1;
251
252 #endif
253 }
254
255
256 // Legacy C interface.
257
258 extern "C" {
259         void*  os_mmap_anonymous(void *addr, size_t len, int prot, int flags) { return os::mmap_anonymous(addr, len, prot, flags); }
260
261         void   os_abort(void) { os::abort(); }
262         int    os_access(const char* pathname, int mode) { return os::access(pathname, mode); }
263         int    os_atoi(const char* nptr) { return os::atoi(nptr); }
264         void*  os_calloc(size_t nmemb, size_t size) { return os::calloc(nmemb, size); }
265 #if defined(ENABLE_JRE_LAYOUT)
266         char*  os_dirname(char* path) { return os::dirname(path); }
267 #endif
268         int    os_dlclose(void* handle) { return os::dlclose(handle); }
269         char*  os_dlerror(void) { return os::dlerror(); }
270         void*  os_dlopen(const char* filename, int flag) { return os::dlopen(filename, flag); }
271         void*  os_dlsym(void* handle, const char* symbol) { return os::dlsym(handle, symbol); }
272         int    os_fclose(FILE* fp) { return os::fclose(fp); }
273         FILE*  os_fopen(const char* path, const char* mode) { return os::fopen(path, mode); }
274         size_t os_fread(void* ptr, size_t size, size_t nmemb, FILE* stream) { return os::fread(ptr, size, nmemb, stream); }
275         void   os_free(void* ptr) { os::free(ptr); }
276         int    os_getpagesize(void) { return os::getpagesize(); }
277         void*  os_memcpy(void* dest, const void* src, size_t n) { return os::memcpy(dest, src, n); }
278         void*  os_memset(void* s, int c, size_t n) { return os::memset(s, c, n); }
279         int    os_mprotect(void* addr, size_t len, int prot) { return os::mprotect(addr, len, prot); }
280         int    os_scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*)) { return os::scandir(dir, namelist, filter, compar); }
281         int    os_stat(const char* path, struct stat* buf) { return os::stat(path, buf); }
282         char*  os_strcat(char* dest, const char* src) { return os::strcat(dest, src); }
283         char*  os_strcpy(char* dest, const char* src) { return os::strcpy(dest, src); }
284         char*  os_strdup(const char* s) { return os::strdup(s); }
285         int    os_strlen(const char* s) { return os::strlen(s); }
286
287 }
288
289
290 /*
291  * These are local overrides for various environment variables in Emacs.
292  * Please do not remove this and leave it at the end of the file, where
293  * Emacs will automagically detect them.
294  * ---------------------------------------------------------------------
295  * Local variables:
296  * mode: c++
297  * indent-tabs-mode: t
298  * c-basic-offset: 4
299  * tab-width: 4
300  * End:
301  * vim:noexpandtab:sw=4:ts=4:
302  */