1 /*
  2  * Copyright (c) 1998, 2021, 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 #ifndef SHARE_CODE_DEBUGINFOREC_HPP
 26 #define SHARE_CODE_DEBUGINFOREC_HPP
 27 
 28 #include "ci/ciClassList.hpp"
 29 #include "ci/ciInstanceKlass.hpp"
 30 #include "ci/ciMethod.hpp"
 31 #include "code/debugInfo.hpp"
 32 #include "code/location.hpp"
 33 #include "code/pcDesc.hpp"
 34 #include "oops/oop.hpp"
 35 #include "utilities/growableArray.hpp"
 36 
 37 class OopMap;
 38 class OopMapSet;
 39 
 40 //** The DebugInformationRecorder collects debugging information
 41 //   for a compiled method.
 42 //   Debugging information is used for:
 43 //   - garbage collecting compiled frames
 44 //   - stack tracing across compiled frames
 45 //   - deoptimizating compiled frames
 46 //
 47 //   The implementation requires the compiler to use the recorder
 48 //   in the following order:
 49 //   1) Describe debug information for safepoints at increasing addresses.
 50 //      a) Add safepoint entry (use add_safepoint or add_non_safepoint)
 51 //      b) Describe scopes for that safepoint
 52 //         - create locals if needed (use create_scope_values)
 53 //         - create expressions if needed (use create_scope_values)
 54 //         - create monitor stack if needed (use create_monitor_values)
 55 //         - describe scope (use describe_scope)
 56 //         "repeat last four steps for all scopes"
 57 //         "outer most scope first and inner most scope last"
 58 //         NB: nodes from create_scope_values and create_locations
 59 //             can be reused for simple sharing.
 60 //         - mark the end of the scopes (end_safepoint or end_non_safepoint)
 61 //   2) Use oop_size, metadata_size, data_size, pcs_size to create the nmethod
 62 //      and finally migrate the debugging information into the nmethod
 63 //      by calling copy_to.
 64 
 65 class DebugToken; // Opaque datatype for stored:
 66                   //  - GrowableArray<ScopeValue*>
 67                   //  - GrowableArray<MonitorValue*>
 68 
 69 // Alias for InvocationEntryBci.
 70 // Both constants are used for a pseudo-BCI which refers
 71 // to the state just _before_ a method is entered.
 72 // SynchronizationEntryBCI is used where the emphasis
 73 // is on the implicit monitorenter of a synchronized method.
 74 const int SynchronizationEntryBCI = InvocationEntryBci;
 75 
 76 class DIR_Chunk; // private class, a nugget of collected information
 77 
 78 class DebugInformationRecorder: public ResourceObj {
 79  public:
 80   // constructor
 81   DebugInformationRecorder(OopRecorder* oop_recorder);
 82 
 83   // adds an oopmap at a specific offset
 84   void add_oopmap(int pc_offset, OopMap* map);
 85 
 86   // adds a jvm mapping at pc-offset, for a safepoint only
 87   void add_safepoint(int pc_offset, OopMap* map);
 88 
 89   // adds a jvm mapping at pc-offset, for a non-safepoint (profile point)
 90   void add_non_safepoint(int pc_offset);
 91 
 92   // Describes debugging information for a scope at the given pc_offset.
 93   // Calls must be in non-decreasing order of pc_offset.
 94   // If there are several calls at a single pc_offset,
 95   // then they occur in the same order as they were performed by the JVM,
 96   // with the most recent (innermost) call being described last.
 97   // For a safepoint, the pc_offset must have been mentioned
 98   // previously by add_safepoint.
 99   // Otherwise, the pc_offset must have been mentioned previously
100   // by add_non_safepoint, and the locals, expressions, and monitors
101   // must all be null.
102   void describe_scope(int         pc_offset,
103                       const methodHandle& methodH,
104                       ciMethod*   method,
105                       int         bci,
106                       bool        reexecute,
107                       bool        rethrow_exception = false,
108                       bool        is_method_handle_invoke = false,
109                       bool        is_optimized_linkToNative = false,
110                       bool        return_oop = false,
111                       bool        has_ea_local_in_scope = false,
112                       bool        arg_escape = false,
113                       DebugToken* locals      = NULL,
114                       DebugToken* expressions = NULL,
115                       DebugToken* monitors    = NULL);
116 
117 
118   void dump_object_pool(GrowableArray<ScopeValue*>* objects);
119 
120   // This call must follow every add_safepoint,
121   // after any intervening describe_scope calls.
122   void end_safepoint(int pc_offset)      { end_scopes(pc_offset, true); }
123   void end_non_safepoint(int pc_offset)  { end_scopes(pc_offset, false); }
124 
125   // helper fuctions for describe_scope to enable sharing
126   DebugToken* create_scope_values(GrowableArray<ScopeValue*>* values);
127   DebugToken* create_monitor_values(GrowableArray<MonitorValue*>* monitors);
128 
129   // returns the size of the generated scopeDescs.
130   int data_size();
131   int pcs_size();
132   int oop_size() { return oop_recorder()->oop_size(); }
133   int metadata_size() { return oop_recorder()->metadata_size(); }
134 
135   // copy the generated debugging information to nmethod
136   void copy_to(nmethod* nm);
137 
138   // verifies the debug information
139   void verify(const nmethod* code);
140 
141   static void print_statistics() PRODUCT_RETURN;
142 
143   // Method for setting oopmaps to temporarily preserve old handling of oopmaps
144   OopMapSet *_oopmaps;
145   void set_oopmaps(OopMapSet *oopmaps) { _oopmaps = oopmaps; }
146 
147   OopRecorder* oop_recorder() { return _oop_recorder; }
148 
149   int last_pc_offset() { return last_pc()->pc_offset(); }
150 
151   bool recording_non_safepoints() { return _recording_non_safepoints; }
152 
153   PcDesc* pcs() const { return _pcs; }
154   int pcs_length() const { return _pcs_length; }
155 
156   DebugInfoWriteStream* stream() const { return _stream; }
157 
158 
159  private:
160   friend class ScopeDesc;
161   friend class vframeStreamCommon;
162   friend class DIR_Chunk;
163 
164   // True if we are recording non-safepoint scopes.
165   // This flag is set if DebugNonSafepoints is true, or if
166   // JVMTI post_compiled_method_load events are enabled.
167   const bool _recording_non_safepoints;
168 
169   DebugInfoWriteStream* _stream;
170 
171   OopRecorder* _oop_recorder;
172 
173   // Scopes that have been described so far.
174   GrowableArray<DIR_Chunk*>* _all_chunks;
175   DIR_Chunk* _next_chunk;
176   DIR_Chunk* _next_chunk_limit;
177 
178 #ifdef ASSERT
179   enum { rs_null, rs_safepoint, rs_non_safepoint };
180   int _recording_state;
181 #endif
182 
183   PcDesc* _pcs;
184   int     _pcs_size;
185   int     _pcs_length;
186   // Note:  Would use GrowableArray<PcDesc>, but structs are not supported.
187 
188   // PC of most recent real safepoint before the current one,
189   // updated after end_scopes.
190   int _prev_safepoint_pc;
191 
192   PcDesc* last_pc() {
193     guarantee(_pcs_length > 0, "a safepoint must be declared already");
194     return &_pcs[_pcs_length-1];
195   }
196   PcDesc* prev_pc() {
197     guarantee(_pcs_length > 1, "a safepoint must be declared already");
198     return &_pcs[_pcs_length-2];
199   }
200   void add_new_pc_offset(int pc_offset);
201   void end_scopes(int pc_offset, bool is_safepoint);
202 
203   int  serialize_monitor_values(GrowableArray<MonitorValue*>* monitors);
204   int  serialize_scope_values(GrowableArray<ScopeValue*>* values);
205   int  find_sharable_decode_offset(int stream_offset);
206 
207 #ifndef PRODUCT
208   bool recorders_frozen();
209   void mark_recorders_frozen();
210 #endif // PRODUCT
211 
212  public:
213   enum { serialized_null = 0 };
214 };
215 
216 #endif // SHARE_CODE_DEBUGINFOREC_HPP