1 /*
   2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jfr/jfrEvents.hpp"
  27 #include "jfr/jni/jfrJavaSupport.hpp"
  28 #include "jfr/leakprofiler/leakProfiler.hpp"
  29 #include "jfr/recorder/repository/jfrEmergencyDump.hpp"
  30 #include "jfr/recorder/service/jfrPostBox.hpp"
  31 #include "jfr/recorder/service/jfrRecorderService.hpp"
  32 #include "jfr/utilities/jfrTypes.hpp"
  33 #include "logging/log.hpp"
  34 #include "memory/resourceArea.hpp"
  35 #include "runtime/atomic.hpp"
  36 #include "runtime/handles.inline.hpp"
  37 #include "runtime/globals.hpp"
  38 #include "runtime/mutexLocker.hpp"
  39 #include "runtime/os.hpp"
  40 #include "runtime/thread.inline.hpp"
  41 #include "utilities/growableArray.hpp"
  42 
  43 static const char vm_error_filename_fmt[] = "hs_err_pid%p.jfr";
  44 static const char vm_oom_filename_fmt[] = "hs_oom_pid%p.jfr";
  45 static const char vm_soe_filename_fmt[] = "hs_soe_pid%p.jfr";
  46 static const char chunk_file_jfr_ext[] = ".jfr";
  47 static const size_t iso8601_len = 19; // "YYYY-MM-DDTHH:MM:SS"
  48 static fio_fd emergency_fd = invalid_fd;
  49 
  50 static fio_fd open_exclusivly(const char* path) {
  51   return os::open(path, O_CREAT | O_RDWR, S_IREAD | S_IWRITE);
  52 }
  53 
  54 static int file_sort(const char** const file1, const char** file2) {
  55   assert(NULL != *file1 && NULL != *file2, "invariant");
  56   int cmp = strncmp(*file1, *file2, iso8601_len);
  57   if (0 == cmp) {
  58     const char* const dot1 = strchr(*file1, '.');
  59     assert(NULL != dot1, "invariant");
  60     const char* const dot2 = strchr(*file2, '.');
  61     assert(NULL != dot2, "invariant");
  62     ptrdiff_t file1_len = dot1 - *file1;
  63     ptrdiff_t file2_len = dot2 - *file2;
  64     if (file1_len < file2_len) {
  65       return -1;
  66     }
  67     if (file1_len > file2_len) {
  68       return 1;
  69     }
  70     assert(file1_len == file2_len, "invariant");
  71     cmp = strncmp(*file1, *file2, file1_len);
  72   }
  73   assert(cmp != 0, "invariant");
  74   return cmp;
  75 }
  76 
  77 static void iso8601_to_date_time(char* iso8601_str) {
  78   assert(iso8601_str != NULL, "invariant");
  79   assert(strlen(iso8601_str) == iso8601_len, "invariant");
  80   // "YYYY-MM-DDTHH:MM:SS"
  81   for (size_t i = 0; i < iso8601_len; ++i) {
  82     switch (iso8601_str[i]) {
  83     case 'T':
  84     case '-':
  85     case ':':
  86       iso8601_str[i] = '_';
  87       break;
  88     }
  89   }
  90   // "YYYY_MM_DD_HH_MM_SS"
  91 }
  92 
  93 static void date_time(char* buffer, size_t buffer_len) {
  94   assert(buffer != NULL, "invariant");
  95   assert(buffer_len >= iso8601_len, "buffer too small");
  96   os::iso8601_time(buffer, buffer_len);
  97   assert(strlen(buffer) >= iso8601_len + 1, "invariant");
  98   // "YYYY-MM-DDTHH:MM:SS"
  99   buffer[iso8601_len] = '\0';
 100   iso8601_to_date_time(buffer);
 101 }
 102 
 103 static int64_t file_size(fio_fd fd) {
 104   assert(fd != invalid_fd, "invariant");
 105   const int64_t current_offset = os::current_file_offset(fd);
 106   const int64_t size = os::lseek(fd, 0, SEEK_END);
 107   os::seek_to_file_offset(fd, current_offset);
 108   return size;
 109 }
 110 
 111 class RepositoryIterator : public StackObj {
 112  private:
 113   const char* const _repo;
 114   const size_t _repository_len;
 115   GrowableArray<const char*>* _files;
 116   const char* const fully_qualified(const char* entry) const;
 117   mutable int _iterator;
 118 
 119  public:
 120   RepositoryIterator(const char* repository, size_t repository_len);
 121   ~RepositoryIterator() {}
 122   const char* const filter(const char* entry) const;
 123   bool has_next() const;
 124   const char* const next() const;
 125 };
 126 
 127 const char* const RepositoryIterator::fully_qualified(const char* entry) const {
 128   assert(NULL != entry, "invariant");
 129   char* file_path_entry = NULL;
 130   // only use files that have content, not placeholders
 131   const char* const file_separator = os::file_separator();
 132   if (NULL != file_separator) {
 133     const size_t entry_len = strlen(entry);
 134     const size_t file_separator_length = strlen(file_separator);
 135     const size_t file_path_entry_length = _repository_len + file_separator_length + entry_len;
 136     file_path_entry = NEW_RESOURCE_ARRAY_RETURN_NULL(char, file_path_entry_length + 1);
 137     if (NULL == file_path_entry) {
 138       return NULL;
 139     }
 140     int position = 0;
 141     position += jio_snprintf(&file_path_entry[position], _repository_len + 1, "%s", _repo);
 142     position += jio_snprintf(&file_path_entry[position], file_separator_length + 1, "%s", os::file_separator());
 143     position += jio_snprintf(&file_path_entry[position], entry_len + 1, "%s", entry);
 144     file_path_entry[position] = '\0';
 145     assert((size_t)position == file_path_entry_length, "invariant");
 146     assert(strlen(file_path_entry) == (size_t)position, "invariant");
 147   }
 148   return file_path_entry;
 149 }
 150 
 151 const char* const RepositoryIterator::filter(const char* entry) const {
 152   if (entry == NULL) {
 153     return NULL;
 154   }
 155   const size_t entry_len = strlen(entry);
 156   if (entry_len <= 2) {
 157     // for "." and ".."
 158     return NULL;
 159   }
 160   char* entry_name = NEW_RESOURCE_ARRAY_RETURN_NULL(char, entry_len + 1);
 161   if (entry_name == NULL) {
 162     return NULL;
 163   }
 164   strncpy(entry_name, entry, entry_len + 1);
 165   const char* const fully_qualified_path_entry = fully_qualified(entry_name);
 166   if (NULL == fully_qualified_path_entry) {
 167     return NULL;
 168   }
 169   const fio_fd entry_fd = open_exclusivly(fully_qualified_path_entry);
 170   if (invalid_fd == entry_fd) {
 171     return NULL;
 172   }
 173   const int64_t entry_size = file_size(entry_fd);
 174   os::close(entry_fd);
 175   if (0 == entry_size) {
 176     return NULL;
 177   }
 178   return entry_name;
 179 }
 180 
 181 RepositoryIterator::RepositoryIterator(const char* repository, size_t repository_len) :
 182   _repo(repository),
 183   _repository_len(repository_len),
 184   _files(NULL),
 185   _iterator(0) {
 186   if (NULL != _repo) {
 187     assert(strlen(_repo) == _repository_len, "invariant");
 188     _files = new GrowableArray<const char*>(10);
 189     DIR* dirp = os::opendir(_repo);
 190     if (dirp == NULL) {
 191       log_error(jfr, system)("Unable to open repository %s", _repo);
 192       return;
 193     }
 194     struct dirent* dentry;
 195     while ((dentry = os::readdir(dirp)) != NULL) {
 196       const char* const entry_path = filter(dentry->d_name);
 197       if (NULL != entry_path) {
 198         _files->append(entry_path);
 199       }
 200     }
 201     os::closedir(dirp);
 202     if (_files->length() > 1) {
 203       _files->sort(file_sort);
 204     }
 205   }
 206 }
 207 
 208 bool RepositoryIterator::has_next() const {
 209   return (_files != NULL && _iterator < _files->length());
 210 }
 211 
 212 const char* const RepositoryIterator::next() const {
 213   return _iterator >= _files->length() ? NULL : fully_qualified(_files->at(_iterator++));
 214 }
 215 
 216 static void write_emergency_file(const RepositoryIterator& iterator) {
 217   assert(emergency_fd != invalid_fd, "invariant");
 218   const size_t size_of_file_copy_block = 1 * M; // 1 mb
 219   jbyte* const file_copy_block = NEW_RESOURCE_ARRAY_RETURN_NULL(jbyte, size_of_file_copy_block);
 220   if (file_copy_block == NULL) {
 221     return;
 222   }
 223   while (iterator.has_next()) {
 224     fio_fd current_fd = invalid_fd;
 225     const char* const fqn = iterator.next();
 226     if (fqn != NULL) {
 227       current_fd = open_exclusivly(fqn);
 228       if (current_fd != invalid_fd) {
 229         const int64_t current_filesize = file_size(current_fd);
 230         assert(current_filesize > 0, "invariant");
 231         int64_t bytes_read = 0;
 232         int64_t bytes_written = 0;
 233         while (bytes_read < current_filesize) {
 234           const ssize_t read_result = os::read_at(current_fd, file_copy_block, size_of_file_copy_block, bytes_read);
 235           if (-1 == read_result) {
 236             log_info(jfr)( // For user, should not be "jfr, system"
 237               "Unable to recover JFR data");
 238             break;
 239           }
 240           bytes_read += (int64_t)read_result;
 241           assert(bytes_read - bytes_written <= (int64_t)size_of_file_copy_block, "invariant");
 242           bytes_written += (int64_t)os::write(emergency_fd, file_copy_block, bytes_read - bytes_written);
 243           assert(bytes_read == bytes_written, "invariant");
 244         }
 245         os::close(current_fd);
 246       }
 247     }
 248   }
 249 }
 250 
 251 // "emergency_dump_path" have to be allocated JVM_MAXPATHLEN or larger.
 252 static void create_emergency_dump_path(char* emergency_dump_path) {
 253   char buffer[JVM_MAXPATHLEN];
 254   emergency_dump_path[0] = '\0';
 255   const char* const cwd = os::get_current_directory(buffer, JVM_MAXPATHLEN);
 256   if (NULL == cwd) {
 257     return;
 258   }
 259   size_t pos = strlen(cwd);
 260   const int fsep_len = jio_snprintf(&buffer[pos], JVM_MAXPATHLEN - pos, "%s", os::file_separator());
 261   const char* filename_fmt = NULL;
 262   // fetch specific error cause
 263   switch (JfrJavaSupport::cause()) {
 264     case JfrJavaSupport::OUT_OF_MEMORY:
 265       filename_fmt = vm_oom_filename_fmt;
 266       break;
 267     case JfrJavaSupport::STACK_OVERFLOW:
 268       filename_fmt = vm_soe_filename_fmt;
 269       break;
 270     default:
 271       filename_fmt = vm_error_filename_fmt;
 272   }
 273   pos += fsep_len;
 274   if (Arguments::copy_expand_pid(filename_fmt, strlen(filename_fmt), &buffer[pos], JVM_MAXPATHLEN - pos)) {
 275     strncpy(emergency_dump_path, buffer, JVM_MAXPATHLEN);
 276   }
 277   if (emergency_dump_path[0] != '\0') {
 278     log_info(jfr)( // For user, should not be "jfr, system"
 279       "Attempting to recover JFR data, emergency jfr file: %s", emergency_dump_path);
 280   }
 281 }
 282 
 283 // Caller needs ResourceMark
 284 static const char* create_emergency_chunk_path(const char* repository_path) {
 285   assert(repository_path != NULL, "invariant");
 286   const size_t repository_path_len = strlen(repository_path);
 287   // date time
 288   char date_time_buffer[32] = { 0 };
 289   date_time(date_time_buffer, sizeof(date_time_buffer));
 290   size_t date_time_len = strlen(date_time_buffer);
 291   size_t chunkname_max_len = repository_path_len          // repository_base_path
 292                              + 1                          // "/"
 293                              + date_time_len              // date_time
 294                              + strlen(chunk_file_jfr_ext) // .jfr
 295                              + 1;
 296   char* chunk_path = NEW_RESOURCE_ARRAY_RETURN_NULL(char, chunkname_max_len);
 297   if (chunk_path == NULL) {
 298     return NULL;
 299   }
 300   // append the individual substrings
 301   jio_snprintf(chunk_path, chunkname_max_len, "%s%s%s%s", repository_path, os::file_separator(), date_time_buffer, chunk_file_jfr_ext);
 302   return chunk_path;
 303 }
 304 
 305 void JfrEmergencyDump::setup_emergency_dump_file_descriptor() {
 306   assert(emergency_fd == invalid_fd, "invariant");
 307   char emergency_dump_path[JVM_MAXPATHLEN];
 308   create_emergency_dump_path(emergency_dump_path);
 309   emergency_fd = (emergency_dump_path[0] != '\0') ? open_exclusivly(emergency_dump_path) : invalid_fd;
 310 }
 311 
 312 // Caller needs ResourceMark
 313 const char* JfrEmergencyDump::build_dump_path(const char* repository_path) {
 314   if (repository_path == NULL) {
 315     char* dump_path = NEW_RESOURCE_ARRAY_RETURN_NULL(char, JVM_MAXPATHLEN);
 316     if (dump_path == NULL) {
 317       return NULL;
 318     }
 319     create_emergency_dump_path(dump_path);
 320     return dump_path;
 321   } else {
 322     return create_emergency_chunk_path(repository_path);
 323   }
 324 }
 325 
 326 void JfrEmergencyDump::on_vm_error(const char* repository_path) {
 327   assert(repository_path != NULL, "invariant");
 328   ResourceMark rm;
 329   if (emergency_fd != invalid_fd) {
 330     RepositoryIterator iterator(repository_path, strlen(repository_path));
 331     write_emergency_file(iterator);
 332     os::close(emergency_fd);
 333     emergency_fd = invalid_fd;
 334   }
 335 }
 336 
 337 /*
 338 * We are just about to exit the VM, so we will be very aggressive
 339 * at this point in order to increase overall success of dumping jfr data.
 340 *
 341 * If we end up deadlocking in the attempt of dumping out jfr data,
 342 * we rely on the WatcherThread task "is_error_reported()",
 343 * to exit the VM after a hard-coded timeout (disallow WatcherThread to emergency dump).
 344 * This "safety net" somewhat explains the aggressiveness in this attempt.
 345 *
 346 */
 347 static bool prepare_for_emergency_dump(Thread* thread) {
 348   assert(thread != NULL, "invariant");
 349 
 350   if (thread->is_Watcher_thread()) {
 351     // need WatcherThread as a safeguard against potential deadlocks
 352     return false;
 353   }
 354   if (JfrStream_lock->owned_by_self()) {
 355     // crashed during jfr rotation, disallow recursion
 356     return false;
 357   }
 358 
 359 #ifdef ASSERT
 360   Mutex* owned_lock = thread->owned_locks();
 361   while (owned_lock != NULL) {
 362     Mutex* next = owned_lock->next();
 363     owned_lock->unlock();
 364     owned_lock = next;
 365   }
 366 #endif // ASSERT
 367 
 368   if (Threads_lock->owned_by_self()) {
 369     Threads_lock->unlock();
 370   }
 371 
 372   if (Module_lock->owned_by_self()) {
 373     Module_lock->unlock();
 374   }
 375 
 376   if (ClassLoaderDataGraph_lock->owned_by_self()) {
 377     ClassLoaderDataGraph_lock->unlock();
 378   }
 379 
 380   if (Heap_lock->owned_by_self()) {
 381     Heap_lock->unlock();
 382   }
 383 
 384   if (VMOperationQueue_lock->owned_by_self()) {
 385     VMOperationQueue_lock->unlock();
 386   }
 387 
 388   if (VMOperationRequest_lock->owned_by_self()) {
 389     VMOperationRequest_lock->unlock();
 390   }
 391 
 392   if (Service_lock->owned_by_self()) {
 393     Service_lock->unlock();
 394   }
 395 
 396   if (UseNotificationThread && Notification_lock->owned_by_self()) {
 397     Notification_lock->unlock();
 398   }
 399 
 400   if (CodeCache_lock->owned_by_self()) {
 401     CodeCache_lock->unlock();
 402   }
 403 
 404   if (PeriodicTask_lock->owned_by_self()) {
 405     PeriodicTask_lock->unlock();
 406   }
 407 
 408   if (JfrMsg_lock->owned_by_self()) {
 409     JfrMsg_lock->unlock();
 410   }
 411 
 412   if (JfrBuffer_lock->owned_by_self()) {
 413     JfrBuffer_lock->unlock();
 414   }
 415 
 416   if (JfrStacktrace_lock->owned_by_self()) {
 417     JfrStacktrace_lock->unlock();
 418   }
 419   return true;
 420 }
 421 
 422 static volatile int jfr_shutdown_lock = 0;
 423 
 424 static bool guard_reentrancy() {
 425   return Atomic::cmpxchg(&jfr_shutdown_lock, 0, 1) == 0;
 426 }
 427 
 428 class JavaThreadInVM : public StackObj {
 429  private:
 430   JavaThread* const _jt;
 431   JavaThreadState _original_state;
 432  public:
 433 
 434   JavaThreadInVM(Thread* t) : _jt(t->is_Java_thread() ? (JavaThread*)t : NULL),
 435                               _original_state(_thread_max_state) {
 436     if ((_jt != NULL) && (_jt->thread_state() != _thread_in_vm)) {
 437       _original_state = _jt->thread_state();
 438       _jt->set_thread_state(_thread_in_vm);
 439     }
 440   }
 441 
 442   ~JavaThreadInVM() {
 443     if (_original_state != _thread_max_state) {
 444       _jt->set_thread_state(_original_state);
 445     }
 446   }
 447 
 448 };
 449 
 450 void JfrEmergencyDump::on_vm_shutdown(bool exception_handler) {
 451   if (!guard_reentrancy()) {
 452     return;
 453   }
 454 
 455   Thread* thread = Thread::current_or_null_safe();
 456   if (thread == NULL) {
 457     return;
 458   }
 459   // Ensure a JavaThread is _thread_in_vm when we make this call
 460   JavaThreadInVM jtivm(thread);
 461   if (!prepare_for_emergency_dump(thread)) {
 462     return;
 463   }
 464 
 465   EventDumpReason event;
 466   if (event.should_commit()) {
 467     event.set_reason(exception_handler ? "Crash" : "Out of Memory");
 468     event.set_recordingId(-1);
 469     event.commit();
 470   }
 471   if (!exception_handler) {
 472     // OOM
 473     LeakProfiler::emit_events(max_jlong, false);
 474   }
 475   const int messages = MSGBIT(MSG_VM_ERROR);
 476   JfrRecorderService service;
 477   service.rotate(messages);
 478 }