1 /* src/vm/jit/trace.cpp - Functions for tracing from java code.
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33 #include "mm/memory.hpp"
35 #include "native/llni.h"
37 #include "threads/thread.hpp"
39 #include "toolbox/logging.hpp"
41 #include "vm/array.hpp"
42 #include "vm/global.h"
43 #include "vm/globals.hpp"
44 #include "vm/javaobjects.hpp"
45 #include "vm/options.h"
46 #include "vm/string.hpp"
49 #include "vm/jit/argument.hpp"
50 #include "vm/jit/codegen-common.hpp"
51 #include "vm/jit/trace.hpp"
52 #include "vm/jit/show.hpp"
57 // FIXME For now we export everything as C functions.
60 /* global variables ***********************************************************/
62 #if !defined(ENABLE_THREADS)
63 s4 _no_threads_tracejavacallindent = 0;
64 u4 _no_threads_tracejavacallcount= 0;
68 /* trace_java_call_print_argument **********************************************
72 *******************************************************************************/
74 static char *trace_java_call_print_argument(methodinfo *m, char *logtext, s4 *logtextlen, typedesc *paramtype, imm_union imu)
81 switch (paramtype->type) {
83 sprintf(logtext + strlen(logtext), "%d (0x%08x)", (int32_t)imu.l, (int32_t)imu.l);
87 #if SIZEOF_VOID_P == 4
88 sprintf(logtext + strlen(logtext), "%lld (0x%016llx)", imu.l, imu.l);
90 sprintf(logtext + strlen(logtext), "%ld (0x%016lx)", imu.l, imu.l);
95 sprintf(logtext + strlen(logtext), "%g (0x%08x)", imu.f, imu.i);
99 #if SIZEOF_VOID_P == 4
100 sprintf(logtext + strlen(logtext), "%g (0x%016llx)", imu.d, imu.l);
102 sprintf(logtext + strlen(logtext), "%g (0x%016lx)", imu.d, imu.l);
107 #if SIZEOF_VOID_P == 4
108 sprintf(logtext + strlen(logtext), "0x%08x", (ptrint) imu.l);
110 sprintf(logtext + strlen(logtext), "0x%016lx", (ptrint) imu.l);
113 /* Workaround for sun.misc.Unsafe methods. In the future
114 (exact GC) we should check if the address is on the GC
117 if ((m->clazz != NULL) &&
118 (m->clazz->name == utf_new_char("sun/misc/Unsafe")))
121 /* Cast to java.lang.Object. */
123 o = (java_handle_t*) (uintptr_t) imu.l;
125 /* Check return argument for java.lang.Class or
129 if (o->vftbl->clazz == class_java_lang_String) {
130 /* get java.lang.String object and the length of the
133 u = javastring_toutf(o, false);
135 len = strlen(" (String = \"") + utf_bytes(u) + strlen("\")");
137 /* realloc memory for string length */
139 logtext = (char*) DumpMemory::reallocate(logtext, *logtextlen, *logtextlen + len);
142 /* convert to utf8 string and strcat it to the logtext */
144 strcat(logtext, " (String = \"");
146 strcat(logtext, "\")");
149 if (o->vftbl->clazz == class_java_lang_Class) {
150 /* if the object returned is a java.lang.Class
151 cast it to classinfo structure and get the name
159 /* if the object returned is not a java.lang.String or
160 a java.lang.Class just print the name of the class */
162 u = o->vftbl->clazz->name;
165 len = strlen(" (Class = \"") + utf_bytes(u) + strlen("\")");
167 /* realloc memory for string length */
169 logtext = (char*) DumpMemory::reallocate(logtext, *logtextlen, *logtextlen + len);
172 /* strcat to the logtext */
174 strcat(logtext, " (Class = \"");
175 utf_cat_classname(logtext, u);
176 strcat(logtext, "\")");
184 /* trace_java_call_enter ******************************************************
186 Traces an entry into a java method.
188 arg_regs: Array containing all argument registers as int64_t values in
189 the same order as listed in m->methoddesc. The array is usually allocated
190 on the stack and used for restoring the argument registers later.
192 stack: Pointer to first on stack argument in the same format passed to
195 *******************************************************************************/
197 void trace_java_call_enter(methodinfo *m, uint64_t *arg_regs, uint64_t *stack)
206 /* We can only trace "slow" builtin functions (those with a stub)
207 * here, because the argument passing of "fast" ones happens via
208 * the native ABI and does not fit these functions. */
210 if (method_is_builtin(m)) {
211 if (!opt_TraceBuiltinCalls)
215 if (!opt_TraceJavaCalls)
217 #if defined(ENABLE_DEBUG_FILTER)
218 if (!show_filters_test_verbosecall_enter(m))
223 #if defined(ENABLE_VMLOG)
224 vmlog_cacao_enter_method(m);
230 /* calculate message length */
233 strlen("4294967295 ") +
234 strlen("-2147483647-") + /* INT_MAX should be sufficient */
235 TRACEJAVACALLINDENT +
237 ((m->clazz == NULL) ? strlen("NULL") : utf_bytes(m->clazz->name)) +
240 utf_bytes(m->descriptor);
242 /* Actually it's not possible to have all flags printed, but:
248 strlen(" PROTECTED") +
251 strlen(" SYNCHRONIZED") +
252 strlen(" VOLATILE") +
253 strlen(" TRANSIENT") +
255 strlen(" INTERFACE") +
256 strlen(" ABSTRACT") +
257 strlen(" METHOD_BUILTIN");
259 /* add maximal argument length */
263 strlen("-9223372036854775808 (0x123456789abcdef0), ") * md->paramcount +
267 // Create new dump memory area.
270 // TODO Use a std::string here.
271 logtext = (char*) DumpMemory::allocate(sizeof(char) * logtextlen);
273 TRACEJAVACALLCOUNT++;
275 sprintf(logtext, "%10d ", TRACEJAVACALLCOUNT);
276 sprintf(logtext + strlen(logtext), "-%d-", TRACEJAVACALLINDENT);
278 pos = strlen(logtext);
280 for (i = 0; i < TRACEJAVACALLINDENT; i++)
281 logtext[pos++] = '\t';
283 strcpy(logtext + pos, "called: ");
285 if (m->clazz != NULL)
286 utf_cat_classname(logtext, m->clazz->name);
288 strcat(logtext, "NULL");
289 strcat(logtext, ".");
290 utf_cat(logtext, m->name);
291 utf_cat(logtext, m->descriptor);
293 if (m->flags & ACC_PUBLIC) strcat(logtext, " PUBLIC");
294 if (m->flags & ACC_PRIVATE) strcat(logtext, " PRIVATE");
295 if (m->flags & ACC_PROTECTED) strcat(logtext, " PROTECTED");
296 if (m->flags & ACC_STATIC) strcat(logtext, " STATIC");
297 if (m->flags & ACC_FINAL) strcat(logtext, " FINAL");
298 if (m->flags & ACC_SYNCHRONIZED) strcat(logtext, " SYNCHRONIZED");
299 if (m->flags & ACC_VOLATILE) strcat(logtext, " VOLATILE");
300 if (m->flags & ACC_TRANSIENT) strcat(logtext, " TRANSIENT");
301 if (m->flags & ACC_NATIVE) strcat(logtext, " NATIVE");
302 if (m->flags & ACC_INTERFACE) strcat(logtext, " INTERFACE");
303 if (m->flags & ACC_ABSTRACT) strcat(logtext, " ABSTRACT");
305 strcat(logtext, "(");
307 for (i = 0; i < md->paramcount; ++i) {
308 arg = argument_jitarray_load(md, i, arg_regs, stack);
309 logtext = trace_java_call_print_argument(m, logtext, &logtextlen,
310 &md->paramtypes[i], arg);
311 if (i != (md->paramcount - 1)) {
312 strcat(logtext, ", ");
316 strcat(logtext, ")");
320 TRACEJAVACALLINDENT++;
323 /* trace_java_call_exit ********************************************************
325 Traces an exit form a java method.
327 return_regs: Array of size 1 containing return register.
328 The array is usually allocated on the stack and used for restoring the
331 *******************************************************************************/
333 void trace_java_call_exit(methodinfo *m, uint64_t *return_regs)
342 /* We can only trace "slow" builtin functions (those with a stub)
343 * here, because the argument passing of "fast" ones happens via
344 * the native ABI and does not fit these functions. */
346 if (method_is_builtin(m)) {
347 if (!opt_TraceBuiltinCalls)
351 if (!opt_TraceJavaCalls)
353 #if defined(ENABLE_DEBUG_FILTER)
354 if (!show_filters_test_verbosecall_exit(m))
359 #if defined(ENABLE_VMLOG)
360 vmlog_cacao_leave_method(m);
366 /* outdent the log message */
368 if (TRACEJAVACALLINDENT)
369 TRACEJAVACALLINDENT--;
371 log_text("trace_java_call_exit: WARNING: unmatched unindent");
373 /* calculate message length */
376 strlen("4294967295 ") +
377 strlen("-2147483647-") + /* INT_MAX should be sufficient */
378 TRACEJAVACALLINDENT +
379 strlen("finished: ") +
380 ((m->clazz == NULL) ? strlen("NULL") : utf_bytes(m->clazz->name)) +
383 utf_bytes(m->descriptor) +
384 strlen(" SYNCHRONIZED") + strlen("(") + strlen(")");
386 /* add maximal argument length */
388 logtextlen += strlen("->0.4872328470301428 (0x0123456789abcdef)");
390 // Create new dump memory area.
393 // TODO Use a std::string here.
394 logtext = (char*) DumpMemory::allocate(sizeof(char) * logtextlen);
396 /* generate the message */
398 sprintf(logtext, " ");
399 sprintf(logtext + strlen(logtext), "-%d-", TRACEJAVACALLINDENT);
401 pos = strlen(logtext);
403 for (i = 0; i < TRACEJAVACALLINDENT; i++)
404 logtext[pos++] = '\t';
406 strcpy(logtext + pos, "finished: ");
407 if (m->clazz != NULL)
408 utf_cat_classname(logtext, m->clazz->name);
410 strcat(logtext, "NULL");
411 strcat(logtext, ".");
412 utf_cat(logtext, m->name);
413 utf_cat(logtext, m->descriptor);
415 if (!IS_VOID_TYPE(md->returntype.type)) {
416 strcat(logtext, "->");
417 val = argument_jitreturn_load(md, return_regs);
420 trace_java_call_print_argument(m, logtext, &logtextlen,
421 &md->returntype, val);
428 /* trace_exception *************************************************************
430 Traces an exception which is handled by exceptions_handle_exception.
432 *******************************************************************************/
434 void trace_exception(java_object_t *xptr, methodinfo *m, void *pos)
440 /* calculate message length */
444 strlen("Exception ") + utf_bytes(xptr->vftbl->clazz->name);
447 logtextlen = strlen("Some Throwable");
450 logtextlen += strlen(" thrown in ");
454 utf_bytes(m->clazz->name) +
457 utf_bytes(m->descriptor) +
458 strlen("(NOSYNC,NATIVE");
460 #if SIZEOF_VOID_P == 8
462 strlen(")(0x123456789abcdef0) at position 0x123456789abcdef0 (");
464 logtextlen += strlen(")(0x12345678) at position 0x12345678 (");
467 if (m->clazz->sourcefile == NULL)
468 logtextlen += strlen("<NO CLASSFILE INFORMATION>");
470 logtextlen += utf_bytes(m->clazz->sourcefile);
472 logtextlen += strlen(":65536)");
476 logtextlen += strlen("call_java_method");
479 logtextlen += strlen("0");
481 // Create new dump memory area.
484 // TODO Use a std::string here.
485 logtext = (char*) DumpMemory::allocate(sizeof(char) * logtextlen);
488 strcpy(logtext, "Exception ");
489 utf_cat_classname(logtext, xptr->vftbl->clazz->name);
492 strcpy(logtext, "Some Throwable");
495 strcat(logtext, " thrown in ");
498 utf_cat_classname(logtext, m->clazz->name);
499 strcat(logtext, ".");
500 utf_cat(logtext, m->name);
501 utf_cat(logtext, m->descriptor);
503 if (m->flags & ACC_SYNCHRONIZED)
504 strcat(logtext, "(SYNC");
506 strcat(logtext, "(NOSYNC");
508 if (m->flags & ACC_NATIVE) {
509 strcat(logtext, ",NATIVE");
513 #if SIZEOF_VOID_P == 8
514 sprintf(logtext + strlen(logtext),
515 ")(0x%016lx) at position 0x%016lx",
516 (ptrint) code->entrypoint, (ptrint) pos);
518 sprintf(logtext + strlen(logtext),
519 ")(0x%08x) at position 0x%08x",
520 (ptrint) code->entrypoint, (ptrint) pos);
525 /* XXX preliminary: This should get the actual codeinfo */
526 /* in which the exception happened. */
529 #if SIZEOF_VOID_P == 8
530 sprintf(logtext + strlen(logtext),
531 ")(0x%016lx) at position 0x%016lx (",
532 (ptrint) code->entrypoint, (ptrint) pos);
534 sprintf(logtext + strlen(logtext),
535 ")(0x%08x) at position 0x%08x (",
536 (ptrint) code->entrypoint, (ptrint) pos);
539 if (m->clazz->sourcefile == NULL)
540 strcat(logtext, "<NO CLASSFILE INFORMATION>");
542 utf_cat(logtext, m->clazz->sourcefile);
544 sprintf(logtext + strlen(logtext), ":%d)", 0);
548 strcat(logtext, "call_java_method");
554 /* trace_exception_builtin *****************************************************
556 Traces an exception which is thrown by builtin_throw_exception.
558 *******************************************************************************/
560 void trace_exception_builtin(java_handle_t* h)
565 java_lang_Throwable jlt(h);
567 // Get detail message.
568 java_handle_t* s = NULL;
570 if (jlt.get_handle() != NULL)
571 s = jlt.get_detailMessage();
573 java_lang_String jls(s);
575 /* calculate message length */
577 logtextlen = strlen("Builtin exception thrown: ") + strlen("0");
579 if (jlt.get_handle() != NULL) {
580 logtextlen += utf_bytes(jlt.get_vftbl()->clazz->name);
582 if (jls.get_handle()) {
583 CharArray ca(jls.get_value());
584 // FIXME This is not handle capable!
585 uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
586 logtextlen += strlen(": ") +
587 u2_utflength(ptr + jls.get_offset(), jls.get_count());
591 logtextlen += strlen("(nil)");
594 // Create new dump memory area.
597 logtext = (char*) DumpMemory::allocate(sizeof(char) * logtextlen);
599 strcpy(logtext, "Builtin exception thrown: ");
601 if (jlt.get_handle()) {
602 utf_cat_classname(logtext, jlt.get_vftbl()->clazz->name);
607 buf = javastring_tochar(jls.get_handle());
608 strcat(logtext, ": ");
609 strcat(logtext, buf);
610 MFREE(buf, char, strlen(buf) + 1);
614 strcat(logtext, "(nil)");
622 #endif /* !defined(NDEBUG) */
626 * These are local overrides for various environment variables in Emacs.
627 * Please do not remove this and leave it at the end of the file, where
628 * Emacs will automagically detect them.
629 * ---------------------------------------------------------------------
632 * indent-tabs-mode: t
636 * vim:noexpandtab:sw=4:ts=4: