1 /*
2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
3  * Copyright 2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  *
24  */
25 
26 #include "precompiled.hpp"
27 #include "code/scopeDesc.hpp"
28 #include "interpreter/interpreter.hpp"
29 #include "interpreter/interpreterRuntime.hpp"
30 #include "memory/resourceArea.hpp"
31 #include "memory/universe.hpp"
32 #include "oops/markWord.hpp"
33 #include "oops/method.hpp"
34 #include "oops/oop.inline.hpp"
35 #include "runtime/frame.inline.hpp"
36 #include "runtime/handles.inline.hpp"
37 #include "runtime/javaCalls.hpp"
38 #include "runtime/monitorChunk.hpp"
39 #include "runtime/signature.hpp"
40 #include "runtime/stubCodeGenerator.hpp"
41 #include "runtime/stubRoutines.hpp"
42 #include "vmreg_zero.inline.hpp"
43 #ifdef COMPILER1
44 #include "c1/c1_Runtime1.hpp"
45 #include "runtime/vframeArray.hpp"
46 #endif
47 
48 #ifdef ASSERT
check_location_valid()49 void RegisterMap::check_location_valid() {
50   ShouldNotCallThis();
51 }
52 #endif
53 
is_interpreted_frame() const54 bool frame::is_interpreted_frame() const {
55   return zeroframe()->is_interpreter_frame();
56 }
57 
is_fake_stub_frame() const58 bool frame::is_fake_stub_frame() const {
59   return zeroframe()->is_fake_stub_frame();
60 }
61 
sender_for_entry_frame(RegisterMap * map) const62 frame frame::sender_for_entry_frame(RegisterMap *map) const {
63   assert(zeroframe()->is_entry_frame(), "wrong type of frame");
64   assert(map != NULL, "map must be set");
65   assert(!entry_frame_is_first(), "next Java fp must be non zero");
66   assert(entry_frame_call_wrapper()->anchor()->last_Java_sp() == sender_sp(),
67          "sender should be next Java frame");
68   map->clear();
69   assert(map->include_argument_oops(), "should be set by clear");
70   return frame(zeroframe()->next(), sender_sp());
71 }
72 
sender_for_nonentry_frame(RegisterMap * map) const73 frame frame::sender_for_nonentry_frame(RegisterMap *map) const {
74   assert(zeroframe()->is_interpreter_frame() ||
75          zeroframe()->is_fake_stub_frame(), "wrong type of frame");
76   return frame(zeroframe()->next(), sender_sp());
77 }
78 
sender(RegisterMap * map) const79 frame frame::sender(RegisterMap* map) const {
80   // Default is not to follow arguments; the various
81   // sender_for_xxx methods update this accordingly.
82   map->set_include_argument_oops(false);
83 
84   if (is_entry_frame())
85     return sender_for_entry_frame(map);
86   else
87     return sender_for_nonentry_frame(map);
88 }
89 
90 #ifdef CC_INTERP
interpreter_frame_monitor_begin() const91 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
92   return get_interpreterState()->monitor_base();
93 }
94 
interpreter_frame_monitor_end() const95 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
96   return (BasicObjectLock*) get_interpreterState()->stack_base();
97 }
98 #endif // CC_INTERP
99 
patch_pc(Thread * thread,address pc)100 void frame::patch_pc(Thread* thread, address pc) {
101   if (pc != NULL) {
102     assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
103     _pc = pc;
104     _deopt_state = is_deoptimized;
105   } else {
106     // We borrow this call to set the thread pointer in the interpreter
107     // state; the hook to set up deoptimized frames isn't supplied it.
108     assert(pc == NULL, "should be");
109     get_interpreterState()->set_thread((JavaThread *) thread);
110   }
111 }
112 
safe_for_sender(JavaThread * thread)113 bool frame::safe_for_sender(JavaThread *thread) {
114   ShouldNotCallThis();
115   return false;
116 }
117 
is_interpreted_frame_valid(JavaThread * thread) const118 bool frame::is_interpreted_frame_valid(JavaThread *thread) const {
119   ShouldNotCallThis();
120   return false;
121 }
122 
interpreter_frame_result(oop * oop_result,jvalue * value_result)123 BasicType frame::interpreter_frame_result(oop* oop_result,
124                                           jvalue* value_result) {
125   assert(is_interpreted_frame(), "interpreted frame expected");
126   Method* method = interpreter_frame_method();
127   BasicType type = method->result_type();
128   intptr_t* tos_addr = (intptr_t *) interpreter_frame_tos_address();
129   oop obj;
130 
131   switch (type) {
132   case T_VOID:
133     break;
134   case T_BOOLEAN:
135     value_result->z = *(jboolean *) tos_addr;
136     break;
137   case T_BYTE:
138     value_result->b = *(jbyte *) tos_addr;
139     break;
140   case T_CHAR:
141     value_result->c = *(jchar *) tos_addr;
142     break;
143   case T_SHORT:
144     value_result->s = *(jshort *) tos_addr;
145     break;
146   case T_INT:
147     value_result->i = *(jint *) tos_addr;
148     break;
149   case T_LONG:
150     value_result->j = *(jlong *) tos_addr;
151     break;
152   case T_FLOAT:
153     value_result->f = *(jfloat *) tos_addr;
154     break;
155   case T_DOUBLE:
156     value_result->d = *(jdouble *) tos_addr;
157     break;
158 
159   case T_OBJECT:
160   case T_ARRAY:
161     if (method->is_native()) {
162       obj = get_interpreterState()->oop_temp();
163     }
164     else {
165       oop* obj_p = (oop *) tos_addr;
166       obj = (obj_p == NULL) ? (oop) NULL : *obj_p;
167     }
168     assert(obj == NULL || Universe::heap()->is_in(obj), "sanity check");
169     *oop_result = obj;
170     break;
171 
172   default:
173     ShouldNotReachHere();
174   }
175 
176   return type;
177 }
178 
frame_size(RegisterMap * map) const179 int frame::frame_size(RegisterMap* map) const {
180 #ifdef PRODUCT
181   ShouldNotCallThis();
182 #endif // PRODUCT
183   return 0; // make javaVFrame::print_value work
184 }
185 
interpreter_frame_tos_at(jint offset) const186 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
187   int index = (Interpreter::expr_offset_in_bytes(offset) / wordSize);
188   return &interpreter_frame_tos_address()[index];
189 }
190 
zero_print_on_error(int frame_index,outputStream * st,char * buf,int buflen) const191 void frame::zero_print_on_error(int           frame_index,
192                                 outputStream* st,
193                                 char*         buf,
194                                 int           buflen) const {
195   // Divide the buffer between the field and the value
196   buflen >>= 1;
197   char *fieldbuf = buf;
198   char *valuebuf = buf + buflen;
199 
200   // Print each word of the frame
201   for (intptr_t *addr = sp(); addr <= fp(); addr++) {
202     int offset = fp() - addr;
203 
204     // Fill in default values, then try and improve them
205     snprintf(fieldbuf, buflen, "word[%d]", offset);
206     snprintf(valuebuf, buflen, PTR_FORMAT, *addr);
207     zeroframe()->identify_word(frame_index, offset, fieldbuf, valuebuf, buflen);
208     fieldbuf[buflen - 1] = '\0';
209     valuebuf[buflen - 1] = '\0';
210 
211     // Print the result
212     st->print_cr(" " PTR_FORMAT ": %-21s = %s", p2i(addr), fieldbuf, valuebuf);
213   }
214 }
215 
identify_word(int frame_index,int offset,char * fieldbuf,char * valuebuf,int buflen) const216 void ZeroFrame::identify_word(int   frame_index,
217                               int   offset,
218                               char* fieldbuf,
219                               char* valuebuf,
220                               int   buflen) const {
221   switch (offset) {
222   case next_frame_off:
223     strncpy(fieldbuf, "next_frame", buflen);
224     break;
225 
226   case frame_type_off:
227     strncpy(fieldbuf, "frame_type", buflen);
228     if (is_entry_frame())
229       strncpy(valuebuf, "ENTRY_FRAME", buflen);
230     else if (is_interpreter_frame())
231       strncpy(valuebuf, "INTERPRETER_FRAME", buflen);
232     else if (is_fake_stub_frame())
233       strncpy(valuebuf, "FAKE_STUB_FRAME", buflen);
234     break;
235 
236   default:
237     if (is_entry_frame()) {
238       as_entry_frame()->identify_word(
239         frame_index, offset, fieldbuf, valuebuf, buflen);
240     }
241     else if (is_interpreter_frame()) {
242       as_interpreter_frame()->identify_word(
243         frame_index, offset, fieldbuf, valuebuf, buflen);
244     }
245     else if (is_fake_stub_frame()) {
246       as_fake_stub_frame()->identify_word(
247         frame_index, offset, fieldbuf, valuebuf, buflen);
248     }
249   }
250 }
251 
identify_word(int frame_index,int offset,char * fieldbuf,char * valuebuf,int buflen) const252 void EntryFrame::identify_word(int   frame_index,
253                                int   offset,
254                                char* fieldbuf,
255                                char* valuebuf,
256                                int   buflen) const {
257   switch (offset) {
258   case call_wrapper_off:
259     strncpy(fieldbuf, "call_wrapper", buflen);
260     break;
261 
262   default:
263     snprintf(fieldbuf, buflen, "local[%d]", offset - 3);
264   }
265 }
266 
identify_word(int frame_index,int offset,char * fieldbuf,char * valuebuf,int buflen) const267 void InterpreterFrame::identify_word(int   frame_index,
268                                      int   offset,
269                                      char* fieldbuf,
270                                      char* valuebuf,
271                                      int   buflen) const {
272   interpreterState istate = interpreter_state();
273   bool is_valid = istate->self_link() == istate;
274   intptr_t *addr = addr_of_word(offset);
275 
276   // Fixed part
277   if (addr >= (intptr_t *) istate) {
278     const char *field = istate->name_of_field_at_address((address) addr);
279     if (field) {
280       if (is_valid && !strcmp(field, "_method")) {
281         istate->method()->name_and_sig_as_C_string(valuebuf, buflen);
282       }
283       else if (is_valid && !strcmp(field, "_bcp") && istate->bcp()) {
284         snprintf(valuebuf, buflen, PTR_FORMAT " (bci %d)",
285                  (intptr_t) istate->bcp(),
286                  istate->method()->bci_from(istate->bcp()));
287       }
288       snprintf(fieldbuf, buflen, "%sistate->%s",
289                field[strlen(field) - 1] == ')' ? "(": "", field);
290     }
291     else if (addr == (intptr_t *) istate) {
292       strncpy(fieldbuf, "(vtable for istate)", buflen);
293     }
294     return;
295   }
296 
297   // Variable part
298   if (!is_valid)
299     return;
300 
301   // JNI stuff
302   if (istate->method()->is_native() && addr < istate->stack_base()) {
303     address hA = istate->method()->signature_handler();
304     if (hA != NULL) {
305       if (hA != (address) InterpreterRuntime::slow_signature_handler) {
306         InterpreterRuntime::SignatureHandler *handler =
307           InterpreterRuntime::SignatureHandler::from_handlerAddr(hA);
308 
309         intptr_t *params = istate->stack_base() - handler->argument_count();
310         if (addr >= params) {
311           int param = addr - params;
312           const char *desc = "";
313           if (param == 0)
314             desc = " (JNIEnv)";
315           else if (param == 1) {
316             if (istate->method()->is_static())
317               desc = " (mirror)";
318             else
319               desc = " (this)";
320           }
321           snprintf(fieldbuf, buflen, "parameter[%d]%s", param, desc);
322           return;
323         }
324 
325         for (int i = 0; i < handler->argument_count(); i++) {
326           if (params[i] == (intptr_t) addr) {
327             snprintf(fieldbuf, buflen, "unboxed parameter[%d]", i);
328             return;
329           }
330         }
331       }
332     }
333     return;
334   }
335 
336   // Monitors and stack
337   identify_vp_word(frame_index, addr,
338                    (intptr_t *) istate->monitor_base(),
339                    istate->stack_base(),
340                    fieldbuf, buflen);
341 }
342 
identify_vp_word(int frame_index,intptr_t * addr,intptr_t * monitor_base,intptr_t * stack_base,char * fieldbuf,int buflen) const343 void ZeroFrame::identify_vp_word(int       frame_index,
344                                  intptr_t* addr,
345                                  intptr_t* monitor_base,
346                                  intptr_t* stack_base,
347                                  char*     fieldbuf,
348                                  int       buflen) const {
349   // Monitors
350   if (addr >= stack_base && addr < monitor_base) {
351     int monitor_size = frame::interpreter_frame_monitor_size();
352     int last_index = (monitor_base - stack_base) / monitor_size - 1;
353     int index = last_index - (addr - stack_base) / monitor_size;
354     intptr_t monitor = (intptr_t) (
355       (BasicObjectLock *) monitor_base - 1 - index);
356     intptr_t offset = (intptr_t) addr - monitor;
357 
358     if (offset == BasicObjectLock::obj_offset_in_bytes())
359       snprintf(fieldbuf, buflen, "monitor[%d]->_obj", index);
360     else if (offset ==  BasicObjectLock::lock_offset_in_bytes())
361       snprintf(fieldbuf, buflen, "monitor[%d]->_lock", index);
362 
363     return;
364   }
365 
366   // Expression stack
367   if (addr < stack_base) {
368     snprintf(fieldbuf, buflen, "%s[%d]",
369              frame_index == 0 ? "stack_word" : "local",
370              (int) (stack_base - addr - 1));
371     return;
372   }
373 }
374 
375 #ifndef PRODUCT
376 
describe_pd(FrameValues & values,int frame_no)377 void frame::describe_pd(FrameValues& values, int frame_no) {
378 
379 }
380 
381 #endif
382 
initial_deoptimization_info()383 intptr_t *frame::initial_deoptimization_info() {
384   // unused... but returns fp() to minimize changes introduced by 7087445
385   return fp();
386 }
387 
388 #ifndef PRODUCT
389 // This is a generic constructor which is only used by pns() in debug.cpp.
frame(void * sp,void * fp,void * pc)390 frame::frame(void* sp, void* fp, void* pc) {
391   Unimplemented();
392 }
393 
pd_ps()394 void frame::pd_ps() {}
395 #endif
396