1/*
2 * Copyright (c) 1997, 2017, 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 "prims/jvm.h"
27#include "runtime/os.hpp"
28#include "utilities/decoder.hpp"
29#include "utilities/vmError.hpp"
30
31#if defined(_WINDOWS)
32  #include "decoder_windows.hpp"
33  #include "windbghelp.hpp"
34#elif defined(__APPLE__)
35  #include "decoder_machO.hpp"
36#elif defined(AIX)
37  #include "decoder_aix.hpp"
38#else
39  #include "decoder_elf.hpp"
40#endif
41
42AbstractDecoder*  Decoder::_shared_decoder = NULL;
43AbstractDecoder*  Decoder::_error_handler_decoder = NULL;
44NullDecoder       Decoder::_do_nothing_decoder;
45Mutex*            Decoder::_shared_decoder_lock = new Mutex(Mutex::native,
46                                "SharedDecoderLock",
47                                false,
48                                Monitor::_safepoint_check_never);
49
50AbstractDecoder* Decoder::get_shared_instance() {
51  assert(_shared_decoder_lock != NULL && _shared_decoder_lock->owned_by_self(),
52    "Require DecoderLock to enter");
53
54  if (_shared_decoder == NULL) {
55    _shared_decoder = create_decoder();
56  }
57  return _shared_decoder;
58}
59
60AbstractDecoder* Decoder::get_error_handler_instance() {
61  if (_error_handler_decoder == NULL) {
62    _error_handler_decoder = create_decoder();
63  }
64  return _error_handler_decoder;
65}
66
67
68AbstractDecoder* Decoder::create_decoder() {
69  AbstractDecoder* decoder;
70#if defined(_WINDOWS)
71  decoder = new (std::nothrow) WindowsDecoder();
72#elif defined (__APPLE__)
73  decoder = new (std::nothrow)MachODecoder();
74#elif defined(AIX)
75  decoder = new (std::nothrow)AIXDecoder();
76#else
77  decoder = new (std::nothrow)ElfDecoder();
78#endif
79
80  if (decoder == NULL || decoder->has_error()) {
81    if (decoder != NULL) {
82      delete decoder;
83    }
84    decoder = &_do_nothing_decoder;
85  }
86  return decoder;
87}
88
89inline bool DecoderLocker::is_first_error_thread() {
90  return (os::current_thread_id() == VMError::get_first_error_tid());
91}
92
93DecoderLocker::DecoderLocker() :
94  MutexLockerEx(DecoderLocker::is_first_error_thread() ?
95                NULL : Decoder::shared_decoder_lock(), true) {
96  _decoder = is_first_error_thread() ?
97    Decoder::get_error_handler_instance() : Decoder::get_shared_instance();
98  assert(_decoder != NULL, "null decoder");
99}
100
101Mutex* Decoder::shared_decoder_lock() {
102  assert(_shared_decoder_lock != NULL, "Just check");
103  return _shared_decoder_lock;
104}
105
106bool Decoder::decode(address addr, char* buf, int buflen, int* offset, const char* modulepath, bool demangle) {
107  assert(_shared_decoder_lock != NULL, "Just check");
108  bool error_handling_thread = os::current_thread_id() == VMError::first_error_tid;
109  MutexLockerEx locker(error_handling_thread ? NULL : _shared_decoder_lock, true);
110  AbstractDecoder* decoder = error_handling_thread ?
111    get_error_handler_instance(): get_shared_instance();
112  assert(decoder != NULL, "null decoder");
113
114  return decoder->decode(addr, buf, buflen, offset, modulepath, demangle);
115}
116
117bool Decoder::decode(address addr, char* buf, int buflen, int* offset, const void* base) {
118  assert(_shared_decoder_lock != NULL, "Just check");
119  bool error_handling_thread = os::current_thread_id() == VMError::first_error_tid;
120  MutexLockerEx locker(error_handling_thread ? NULL : _shared_decoder_lock, true);
121  AbstractDecoder* decoder = error_handling_thread ?
122    get_error_handler_instance(): get_shared_instance();
123  assert(decoder != NULL, "null decoder");
124
125  return decoder->decode(addr, buf, buflen, offset, base);
126}
127
128
129bool Decoder::demangle(const char* symbol, char* buf, int buflen) {
130  assert(_shared_decoder_lock != NULL, "Just check");
131  bool error_handling_thread = os::current_thread_id() == VMError::first_error_tid;
132  MutexLockerEx locker(error_handling_thread ? NULL : _shared_decoder_lock, true);
133  AbstractDecoder* decoder = error_handling_thread ?
134    get_error_handler_instance(): get_shared_instance();
135  assert(decoder != NULL, "null decoder");
136  return decoder->demangle(symbol, buf, buflen);
137}
138
139bool Decoder::can_decode_C_frame_in_vm() {
140  assert(_shared_decoder_lock != NULL, "Just check");
141  bool error_handling_thread = os::current_thread_id() == VMError::first_error_tid;
142  MutexLockerEx locker(error_handling_thread ? NULL : _shared_decoder_lock, true);
143  AbstractDecoder* decoder = error_handling_thread ?
144    get_error_handler_instance(): get_shared_instance();
145  assert(decoder != NULL, "null decoder");
146  return decoder->can_decode_C_frame_in_vm();
147}
148
149/*
150 * Shutdown shared decoder and replace it with
151 * _do_nothing_decoder. Do nothing with error handler
152 * instance, since the JVM is going down.
153 */
154void Decoder::shutdown() {
155  assert(_shared_decoder_lock != NULL, "Just check");
156  MutexLockerEx locker(_shared_decoder_lock, true);
157
158  if (_shared_decoder != NULL &&
159    _shared_decoder != &_do_nothing_decoder) {
160    delete _shared_decoder;
161  }
162
163  _shared_decoder = &_do_nothing_decoder;
164}
165
166void Decoder::print_state_on(outputStream* st) {
167#ifdef _WINDOWS
168  WindowsDbgHelp::print_state_on(st);
169#endif
170}
171
172