30 #include "metaprogramming/isRegisteredEnum.hpp"
31 #include "metaprogramming/integralConstant.hpp"
32 #include "runtime/extendedPC.hpp"
33 #include "utilities/exceptions.hpp"
34 #include "utilities/ostream.hpp"
35 #include "utilities/macros.hpp"
36 #ifndef _WINDOWS
37 # include <setjmp.h>
38 #endif
39 #ifdef __APPLE__
40 # include <mach/mach_time.h>
41 #endif
42
43 class AgentLibrary;
44 class frame;
45
46 // os defines the interface to operating system; this includes traditional
47 // OS services (time, I/O) as well as other functionality with system-
48 // dependent code.
49
50 typedef void (*dll_func)(...);
51
52 class Thread;
53 class JavaThread;
54 class NativeCallStack;
55 class methodHandle;
56 class OSThread;
57 class Mutex;
58
59 template<class E> class GrowableArray;
60
61 // %%%%% Moved ThreadState, START_FN, OSThread to new osThread.hpp. -- Rose
62
63 // Platform-independent error return values from OS functions
64 enum OSReturn {
65 OS_OK = 0, // Operation was successful
66 OS_ERR = -1, // Operation failed
67 OS_INTRPT = -2, // Operation was interrupted
68 OS_TIMEOUT = -3, // Operation timed out
69 OS_NOMEM = -5, // Operation failed for lack of memory
70 OS_NORESOURCE = -6 // Operation failed for lack of nonmemory resource
71 };
178 static void javaTimeNanos_info(jvmtiTimerInfo *info_ptr);
179 static void javaTimeSystemUTC(jlong &seconds, jlong &nanos);
180 static void run_periodic_checks();
181 static bool supports_monotonic_clock();
182
183 // Returns the elapsed time in seconds since the vm started.
184 static double elapsedTime();
185
186 // Returns real time in seconds since an arbitrary point
187 // in the past.
188 static bool getTimesSecs(double* process_real_time,
189 double* process_user_time,
190 double* process_system_time);
191
192 // Interface to the performance counter
193 static jlong elapsed_counter();
194 static jlong elapsed_frequency();
195
196 // The "virtual time" of a thread is the amount of time a thread has
197 // actually run. The first function indicates whether the OS supports
198 // this functionality for the current thread, and if so:
199 // * the second enables vtime tracking (if that is required).
200 // * the third tells whether vtime is enabled.
201 // * the fourth returns the elapsed virtual time for the current
202 // thread.
203 static bool supports_vtime();
204 static bool enable_vtime();
205 static bool vtime_enabled();
206 static double elapsedVTime();
207
208 // Return current local time in a string (YYYY-MM-DD HH:MM:SS).
209 // It is MT safe, but not async-safe, as reading time zone
210 // information may require a lock on some platforms.
211 static char* local_time_string(char *buf, size_t buflen);
212 static struct tm* localtime_pd (const time_t* clock, struct tm* res);
213 static struct tm* gmtime_pd (const time_t* clock, struct tm* res);
214 // Fill in buffer with current local time as an ISO-8601 string.
215 // E.g., YYYY-MM-DDThh:mm:ss.mmm+zzzz.
216 // Returns buffer, or NULL if it failed.
217 static char* iso8601_time(char* buffer, size_t buffer_length, bool utc = false);
218
219 // Interface for detecting multiprocessor system
220 static inline bool is_MP() {
221 // During bootstrap if _processor_count is not yet initialized
222 // we claim to be MP as that is safest. If any platform has a
223 // stub generator that might be triggered in this phase and for
224 // which being declared MP when in fact not, is a problem - then
225 // the bootstrap routine for the stub generator needs to check
237 // The returned value is guaranteed to be between 0 and (os::processor_count() - 1).
238 static uint processor_id();
239
240 // number of CPUs
241 static int processor_count() {
242 return _processor_count;
243 }
244 static void set_processor_count(int count) { _processor_count = count; }
245
246 // Returns the number of CPUs this process is currently allowed to run on.
247 // Note that on some OSes this can change dynamically.
248 static int active_processor_count();
249
250 // At startup the number of active CPUs this process is allowed to run on.
251 // This value does not change dynamically. May be different from active_processor_count().
252 static int initial_active_processor_count() {
253 assert(_initial_active_processor_count > 0, "Initial active processor count not set yet.");
254 return _initial_active_processor_count;
255 }
256
257 // Bind processes to processors.
258 // This is a two step procedure:
259 // first you generate a distribution of processes to processors,
260 // then you bind processes according to that distribution.
261 // Compute a distribution for number of processes to processors.
262 // Stores the processor id's into the distribution array argument.
263 // Returns true if it worked, false if it didn't.
264 static bool distribute_processes(uint length, uint* distribution);
265 // Binds the current process to a processor.
266 // Returns true if it worked, false if it didn't.
267 static bool bind_to_processor(uint processor_id);
268
269 // Give a name to the current thread.
270 static void set_native_thread_name(const char *name);
271
272 // Interface for stack banging (predetect possible stack overflow for
273 // exception processing) There are guard pages, and above that shadow
274 // pages for stack overflow checking.
275 static bool uses_stack_guard_pages();
276 static bool must_commit_stack_guard_pages();
277 static void map_stack_shadow_pages(address sp);
278 static bool stack_shadow_pages_available(Thread *thread, const methodHandle& method, address sp);
279
280 // Find committed memory region within specified range (start, start + size),
281 // return true if found any
282 static bool committed_in_range(address start, size_t size, address& committed_start, size_t& committed_size);
283
284 // OS interface to Virtual Memory
479 static void naked_yield () ;
480 static OSReturn set_priority(Thread* thread, ThreadPriority priority);
481 static OSReturn get_priority(const Thread* const thread, ThreadPriority& priority);
482
483 static int pd_self_suspend_thread(Thread* thread);
484
485 static ExtendedPC fetch_frame_from_context(const void* ucVoid, intptr_t** sp, intptr_t** fp);
486 static frame fetch_frame_from_context(const void* ucVoid);
487 static frame fetch_frame_from_ucontext(Thread* thread, void* ucVoid);
488
489 static void breakpoint();
490 static bool start_debugging(char *buf, int buflen);
491
492 static address current_stack_pointer();
493 static address current_stack_base();
494 static size_t current_stack_size();
495
496 static void verify_stack_alignment() PRODUCT_RETURN;
497
498 static bool message_box(const char* title, const char* message);
499 static char* do_you_want_to_debug(const char* message);
500
501 // run cmd in a separate process and return its exit code; or -1 on failures
502 static int fork_and_exec(char *cmd, bool use_vfork_if_available = false);
503
504 // Call ::exit() on all platforms but Windows
505 static void exit(int num);
506
507 // Terminate the VM, but don't exit the process
508 static void shutdown();
509
510 // Terminate with an error. Default is to generate a core file on platforms
511 // that support such things. This calls shutdown() and then aborts.
512 static void abort(bool dump_core, void *siginfo, const void *context);
513 static void abort(bool dump_core = true);
514
515 // Die immediately, no exit hook, no abort hook, no cleanup.
516 // Dump a core file, if possible, for debugging. os::abort() is the
517 // preferred means to abort the VM on error. os::die() should only
518 // be called if something has gone badly wrong. CreateCoredumpOnCrash
519 // is intentionally not honored by this function.
520 static void die();
521
522 // File i/o operations
523 static const int default_file_open_flags();
524 static int open(const char *path, int oflag, int mode);
525 static FILE* open(int fd, const char* mode);
526 static FILE* fopen(const char* path, const char* mode);
527 static int close(int fd);
528 static jlong lseek(int fd, jlong offset, int whence);
529 // This function, on Windows, canonicalizes a given path (see os_windows.cpp for details).
530 // On Posix, this function is a noop: it does not change anything and just returns
531 // the input pointer.
532 static char* native_path(char *path);
533 static int ftruncate(int fd, jlong length);
534 static int fsync(int fd);
535 static int available(int fd, jlong *bytes);
536 static int get_fileno(FILE* fp);
537 static void flockfile(FILE* fp);
538 static void funlockfile(FILE* fp);
539
540 static int compare_file_modified_times(const char* file1, const char* file2);
541
542 static bool same_files(const char* file1, const char* file2);
543
651 static void print_location(outputStream* st, intptr_t x, bool verbose = false);
652 static size_t lasterror(char *buf, size_t len);
653 static int get_last_error();
654
655 // Replacement for strerror().
656 // Will return the english description of the error (e.g. "File not found", as
657 // suggested in the POSIX standard.
658 // Will return "Unknown error" for an unknown errno value.
659 // Will not attempt to localize the returned string.
660 // Will always return a valid string which is a static constant.
661 // Will not change the value of errno.
662 static const char* strerror(int e);
663
664 // Will return the literalized version of the given errno (e.g. "EINVAL"
665 // for EINVAL).
666 // Will return "Unknown error" for an unknown errno value.
667 // Will always return a valid string which is a static constant.
668 // Will not change the value of errno.
669 static const char* errno_name(int e);
670
671 // Determines whether the calling process is being debugged by a user-mode debugger.
672 static bool is_debugger_attached();
673
674 // wait for a key press if PauseAtExit is set
675 static void wait_for_keypress_at_exit(void);
676
677 // The following two functions are used by fatal error handler to trace
678 // native (C) frames. They are not part of frame.hpp/frame.cpp because
679 // frame.hpp/cpp assume thread is JavaThread, and also because different
680 // OS/compiler may have different convention or provide different API to
681 // walk C frames.
682 //
683 // We don't attempt to become a debugger, so we only follow frames if that
684 // does not require a lookup in the unwind table, which is part of the binary
685 // file but may be unsafe to read after a fatal error. So on x86, we can
686 // only walk stack if %ebp is used as frame pointer; on ia64, it's not
687 // possible to walk C stack without having the unwind table.
688 static bool is_first_C_frame(frame *fr);
689 static frame get_sender_for_C_frame(frame *fr);
690
691 // return current frame. pc() and sp() are set to NULL on failure.
692 static frame current_frame();
693
947 }
948
949 State cancel_suspend() {
950 return switch_state(SR_SUSPEND_REQUEST, SR_RUNNING);
951 }
952
953 State suspended() {
954 return switch_state(SR_SUSPEND_REQUEST, SR_SUSPENDED);
955 }
956
957 State request_wakeup() {
958 return switch_state(SR_SUSPENDED, SR_WAKEUP_REQUEST);
959 }
960
961 State running() {
962 return switch_state(SR_WAKEUP_REQUEST, SR_RUNNING);
963 }
964
965 bool is_running() const {
966 return _state == SR_RUNNING;
967 }
968
969 bool is_suspend_request() const {
970 return _state == SR_SUSPEND_REQUEST;
971 }
972
973 bool is_suspended() const {
974 return _state == SR_SUSPENDED;
975 }
976 };
977 #endif // !WINDOWS
978
979
980 protected:
981 static volatile unsigned int _rand_seed; // seed for random number generator
982 static int _processor_count; // number of processors
983 static int _initial_active_processor_count; // number of active processors during initialization.
984
985 static char* format_boot_path(const char* format_string,
986 const char* home,
987 int home_len,
988 char fileSep,
989 char pathSep);
990 static bool set_boot_path(char fileSep, char pathSep);
|
30 #include "metaprogramming/isRegisteredEnum.hpp"
31 #include "metaprogramming/integralConstant.hpp"
32 #include "runtime/extendedPC.hpp"
33 #include "utilities/exceptions.hpp"
34 #include "utilities/ostream.hpp"
35 #include "utilities/macros.hpp"
36 #ifndef _WINDOWS
37 # include <setjmp.h>
38 #endif
39 #ifdef __APPLE__
40 # include <mach/mach_time.h>
41 #endif
42
43 class AgentLibrary;
44 class frame;
45
46 // os defines the interface to operating system; this includes traditional
47 // OS services (time, I/O) as well as other functionality with system-
48 // dependent code.
49
50 class Thread;
51 class JavaThread;
52 class NativeCallStack;
53 class methodHandle;
54 class OSThread;
55 class Mutex;
56
57 template<class E> class GrowableArray;
58
59 // %%%%% Moved ThreadState, START_FN, OSThread to new osThread.hpp. -- Rose
60
61 // Platform-independent error return values from OS functions
62 enum OSReturn {
63 OS_OK = 0, // Operation was successful
64 OS_ERR = -1, // Operation failed
65 OS_INTRPT = -2, // Operation was interrupted
66 OS_TIMEOUT = -3, // Operation timed out
67 OS_NOMEM = -5, // Operation failed for lack of memory
68 OS_NORESOURCE = -6 // Operation failed for lack of nonmemory resource
69 };
176 static void javaTimeNanos_info(jvmtiTimerInfo *info_ptr);
177 static void javaTimeSystemUTC(jlong &seconds, jlong &nanos);
178 static void run_periodic_checks();
179 static bool supports_monotonic_clock();
180
181 // Returns the elapsed time in seconds since the vm started.
182 static double elapsedTime();
183
184 // Returns real time in seconds since an arbitrary point
185 // in the past.
186 static bool getTimesSecs(double* process_real_time,
187 double* process_user_time,
188 double* process_system_time);
189
190 // Interface to the performance counter
191 static jlong elapsed_counter();
192 static jlong elapsed_frequency();
193
194 // The "virtual time" of a thread is the amount of time a thread has
195 // actually run. The first function indicates whether the OS supports
196 // this functionality for the current thread, and if so the second
197 // returns the elapsed virtual time for the current thread.
198 static bool supports_vtime();
199 static double elapsedVTime();
200
201 // Return current local time in a string (YYYY-MM-DD HH:MM:SS).
202 // It is MT safe, but not async-safe, as reading time zone
203 // information may require a lock on some platforms.
204 static char* local_time_string(char *buf, size_t buflen);
205 static struct tm* localtime_pd (const time_t* clock, struct tm* res);
206 static struct tm* gmtime_pd (const time_t* clock, struct tm* res);
207 // Fill in buffer with current local time as an ISO-8601 string.
208 // E.g., YYYY-MM-DDThh:mm:ss.mmm+zzzz.
209 // Returns buffer, or NULL if it failed.
210 static char* iso8601_time(char* buffer, size_t buffer_length, bool utc = false);
211
212 // Interface for detecting multiprocessor system
213 static inline bool is_MP() {
214 // During bootstrap if _processor_count is not yet initialized
215 // we claim to be MP as that is safest. If any platform has a
216 // stub generator that might be triggered in this phase and for
217 // which being declared MP when in fact not, is a problem - then
218 // the bootstrap routine for the stub generator needs to check
230 // The returned value is guaranteed to be between 0 and (os::processor_count() - 1).
231 static uint processor_id();
232
233 // number of CPUs
234 static int processor_count() {
235 return _processor_count;
236 }
237 static void set_processor_count(int count) { _processor_count = count; }
238
239 // Returns the number of CPUs this process is currently allowed to run on.
240 // Note that on some OSes this can change dynamically.
241 static int active_processor_count();
242
243 // At startup the number of active CPUs this process is allowed to run on.
244 // This value does not change dynamically. May be different from active_processor_count().
245 static int initial_active_processor_count() {
246 assert(_initial_active_processor_count > 0, "Initial active processor count not set yet.");
247 return _initial_active_processor_count;
248 }
249
250 // Binds the current process to a processor.
251 // Returns true if it worked, false if it didn't.
252 static bool bind_to_processor(uint processor_id);
253
254 // Give a name to the current thread.
255 static void set_native_thread_name(const char *name);
256
257 // Interface for stack banging (predetect possible stack overflow for
258 // exception processing) There are guard pages, and above that shadow
259 // pages for stack overflow checking.
260 static bool uses_stack_guard_pages();
261 static bool must_commit_stack_guard_pages();
262 static void map_stack_shadow_pages(address sp);
263 static bool stack_shadow_pages_available(Thread *thread, const methodHandle& method, address sp);
264
265 // Find committed memory region within specified range (start, start + size),
266 // return true if found any
267 static bool committed_in_range(address start, size_t size, address& committed_start, size_t& committed_size);
268
269 // OS interface to Virtual Memory
464 static void naked_yield () ;
465 static OSReturn set_priority(Thread* thread, ThreadPriority priority);
466 static OSReturn get_priority(const Thread* const thread, ThreadPriority& priority);
467
468 static int pd_self_suspend_thread(Thread* thread);
469
470 static ExtendedPC fetch_frame_from_context(const void* ucVoid, intptr_t** sp, intptr_t** fp);
471 static frame fetch_frame_from_context(const void* ucVoid);
472 static frame fetch_frame_from_ucontext(Thread* thread, void* ucVoid);
473
474 static void breakpoint();
475 static bool start_debugging(char *buf, int buflen);
476
477 static address current_stack_pointer();
478 static address current_stack_base();
479 static size_t current_stack_size();
480
481 static void verify_stack_alignment() PRODUCT_RETURN;
482
483 static bool message_box(const char* title, const char* message);
484
485 // run cmd in a separate process and return its exit code; or -1 on failures
486 static int fork_and_exec(char *cmd, bool use_vfork_if_available = false);
487
488 // Call ::exit() on all platforms but Windows
489 static void exit(int num);
490
491 // Terminate the VM, but don't exit the process
492 static void shutdown();
493
494 // Terminate with an error. Default is to generate a core file on platforms
495 // that support such things. This calls shutdown() and then aborts.
496 static void abort(bool dump_core, void *siginfo, const void *context);
497 static void abort(bool dump_core = true);
498
499 // Die immediately, no exit hook, no abort hook, no cleanup.
500 // Dump a core file, if possible, for debugging. os::abort() is the
501 // preferred means to abort the VM on error. os::die() should only
502 // be called if something has gone badly wrong. CreateCoredumpOnCrash
503 // is intentionally not honored by this function.
504 static void die();
505
506 // File i/o operations
507 static int open(const char *path, int oflag, int mode);
508 static FILE* open(int fd, const char* mode);
509 static FILE* fopen(const char* path, const char* mode);
510 static int close(int fd);
511 static jlong lseek(int fd, jlong offset, int whence);
512 // This function, on Windows, canonicalizes a given path (see os_windows.cpp for details).
513 // On Posix, this function is a noop: it does not change anything and just returns
514 // the input pointer.
515 static char* native_path(char *path);
516 static int ftruncate(int fd, jlong length);
517 static int fsync(int fd);
518 static int available(int fd, jlong *bytes);
519 static int get_fileno(FILE* fp);
520 static void flockfile(FILE* fp);
521 static void funlockfile(FILE* fp);
522
523 static int compare_file_modified_times(const char* file1, const char* file2);
524
525 static bool same_files(const char* file1, const char* file2);
526
634 static void print_location(outputStream* st, intptr_t x, bool verbose = false);
635 static size_t lasterror(char *buf, size_t len);
636 static int get_last_error();
637
638 // Replacement for strerror().
639 // Will return the english description of the error (e.g. "File not found", as
640 // suggested in the POSIX standard.
641 // Will return "Unknown error" for an unknown errno value.
642 // Will not attempt to localize the returned string.
643 // Will always return a valid string which is a static constant.
644 // Will not change the value of errno.
645 static const char* strerror(int e);
646
647 // Will return the literalized version of the given errno (e.g. "EINVAL"
648 // for EINVAL).
649 // Will return "Unknown error" for an unknown errno value.
650 // Will always return a valid string which is a static constant.
651 // Will not change the value of errno.
652 static const char* errno_name(int e);
653
654 // wait for a key press if PauseAtExit is set
655 static void wait_for_keypress_at_exit(void);
656
657 // The following two functions are used by fatal error handler to trace
658 // native (C) frames. They are not part of frame.hpp/frame.cpp because
659 // frame.hpp/cpp assume thread is JavaThread, and also because different
660 // OS/compiler may have different convention or provide different API to
661 // walk C frames.
662 //
663 // We don't attempt to become a debugger, so we only follow frames if that
664 // does not require a lookup in the unwind table, which is part of the binary
665 // file but may be unsafe to read after a fatal error. So on x86, we can
666 // only walk stack if %ebp is used as frame pointer; on ia64, it's not
667 // possible to walk C stack without having the unwind table.
668 static bool is_first_C_frame(frame *fr);
669 static frame get_sender_for_C_frame(frame *fr);
670
671 // return current frame. pc() and sp() are set to NULL on failure.
672 static frame current_frame();
673
927 }
928
929 State cancel_suspend() {
930 return switch_state(SR_SUSPEND_REQUEST, SR_RUNNING);
931 }
932
933 State suspended() {
934 return switch_state(SR_SUSPEND_REQUEST, SR_SUSPENDED);
935 }
936
937 State request_wakeup() {
938 return switch_state(SR_SUSPENDED, SR_WAKEUP_REQUEST);
939 }
940
941 State running() {
942 return switch_state(SR_WAKEUP_REQUEST, SR_RUNNING);
943 }
944
945 bool is_running() const {
946 return _state == SR_RUNNING;
947 }
948
949 bool is_suspended() const {
950 return _state == SR_SUSPENDED;
951 }
952 };
953 #endif // !WINDOWS
954
955
956 protected:
957 static volatile unsigned int _rand_seed; // seed for random number generator
958 static int _processor_count; // number of processors
959 static int _initial_active_processor_count; // number of active processors during initialization.
960
961 static char* format_boot_path(const char* format_string,
962 const char* home,
963 int home_len,
964 char fileSep,
965 char pathSep);
966 static bool set_boot_path(char fileSep, char pathSep);
|