1 /*
  2  * Copyright (c) 1997, 2024, 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_SCOPEDESC_HPP
 26 #define SHARE_CODE_SCOPEDESC_HPP
 27 
 28 #include "code/debugInfo.hpp"
 29 #include "code/pcDesc.hpp"
 30 #include "oops/method.hpp"
 31 #include "utilities/growableArray.hpp"
 32 
 33 // SimpleScopeDesc is used when all you need to extract from
 34 // a given pc,nmethod pair is a Method* and a bci. This is
 35 // quite a bit faster than allocating a full ScopeDesc, but
 36 // very limited in abilities.
 37 
 38 class SimpleScopeDesc : public StackObj {
 39  private:
 40   Method* _method;
 41   int _bci;
 42 
 43  public:
 44   SimpleScopeDesc(nmethod* code, address pc) {
 45     PcDesc* pc_desc = code->pc_desc_at(pc);
 46     assert(pc_desc != nullptr, "Must be able to find matching PcDesc");
 47     // save this here so we only have to look up the PcDesc once
 48     DebugInfoReadStream buffer(code, pc_desc->scope_decode_offset());
 49     int ignore_sender = buffer.read_int();
 50     _method           = buffer.read_method();
 51     _bci              = buffer.read_bci();
 52   }
 53 
 54   Method* method() { return _method; }
 55   int bci() { return _bci; }
 56 };
 57 
 58 // ScopeDescs contain the information that makes source-level debugging of
 59 // nmethods possible; each scopeDesc describes a method activation
 60 
 61 class ScopeDesc : public ResourceObj {
 62  public:
 63   // Constructor
 64   ScopeDesc(const nmethod* code, PcDesc* pd, bool ignore_objects = false);
 65 
 66   // Direct access to scope
 67   ScopeDesc* at_offset(int decode_offset) { return new ScopeDesc(this, decode_offset); }
 68 
 69   // JVM state
 70   Method* method()      const { return _method; }
 71   int          bci()      const { return _bci;    }
 72   bool should_reexecute() const { return _reexecute; }
 73   bool rethrow_exception() const { return _rethrow_exception; }
 74   bool return_oop()       const { return _return_oop; }
 75   // Returns true if one or more NoEscape or ArgEscape objects exist in
 76   // any of the scopes at compiled pc.
 77   bool has_ea_local_in_scope() const { return _has_ea_local_in_scope; }
 78   bool arg_escape()       const { return _arg_escape; }
 79 
 80   GrowableArray<ScopeValue*>*   locals();
 81   GrowableArray<ScopeValue*>*   expressions();
 82   GrowableArray<MonitorValue*>* monitors();
 83   GrowableArray<ScopeValue*>*   objects();
 84 
 85   // Stack walking, returns nullptr if this is the outer most scope.
 86   ScopeDesc* sender() const;
 87 
 88   // Returns where the scope was decoded
 89   int decode_offset() const { return _decode_offset; }
 90 
 91   int sender_decode_offset() const { return _sender_decode_offset; }
 92 
 93   // Tells whether sender() returns nullptr
 94   bool is_top() const;
 95 
 96  private:
 97   void initialize(const ScopeDesc* parent, int decode_offset);
 98   // Alternative constructors
 99   ScopeDesc(const ScopeDesc* parent);
100   ScopeDesc(const ScopeDesc* parent, int decode_offset);
101 
102   // JVM state
103   Method*       _method;
104   int           _bci;
105   bool          _reexecute;
106   bool          _rethrow_exception;
107   bool          _return_oop;
108   bool          _has_ea_local_in_scope;       // One or more NoEscape or ArgEscape objects exist in
109                                               // any of the scopes at compiled pc.
110   bool          _arg_escape;                  // Compiled Java call in youngest scope passes ArgEscape
111 
112   // Decoding offsets
113   int _decode_offset;
114   int _sender_decode_offset;
115   int _locals_decode_offset;
116   int _expressions_decode_offset;
117   int _monitors_decode_offset;
118 
119   // Object pool
120   GrowableArray<ScopeValue*>* _objects;
121 
122   // Nmethod information
123   const nmethod* _code;
124 
125   // Decoding operations
126   void decode_body();
127   GrowableArray<ScopeValue*>* decode_scope_values(int decode_offset);
128   GrowableArray<MonitorValue*>* decode_monitor_values(int decode_offset);
129   GrowableArray<ScopeValue*>* decode_object_values(int decode_offset);
130 
131   DebugInfoReadStream* stream_at(int decode_offset) const;
132 
133 
134  public:
135   // Verification
136   void verify();
137   GrowableArray<ScopeValue*>* objects_to_rematerialize(frame& frm, RegisterMap& map);
138 
139 #ifndef PRODUCT
140  public:
141   // Printing support
142   void print_on(outputStream* st) const;
143   void print_on(outputStream* st, PcDesc* pd) const;
144   void print_value_on(outputStream* st) const;
145 #endif
146 };
147 
148 #endif // SHARE_CODE_SCOPEDESC_HPP