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.h"
35 #include "native/jni.h"
36 #include "native/llni.h"
38 #include "threads/thread.hpp"
40 #include "toolbox/logging.h"
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.h"
50 #include "vm/jit/codegen-common.h"
51 #include "vm/jit/trace.hpp"
52 #include "vm/jit/show.h"
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_object_t *) (ptrint) 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 = DMREALLOC(logtext, char, *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 = DMREALLOC(logtext, char, *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)
207 /* We don't trace builtin functions here because the argument
208 passing happens via the native ABI and does not fit these
211 if (method_is_builtin(m))
214 #if defined(ENABLE_DEBUG_FILTER)
215 if (!show_filters_test_verbosecall_enter(m))
219 #if defined(ENABLE_VMLOG)
220 vmlog_cacao_enter_method(m);
226 /* calculate message length */
229 strlen("4294967295 ") +
230 strlen("-2147483647-") + /* INT_MAX should be sufficient */
231 TRACEJAVACALLINDENT +
233 ((m->clazz == NULL) ? strlen("NULL") : utf_bytes(m->clazz->name)) +
236 utf_bytes(m->descriptor);
238 /* Actually it's not possible to have all flags printed, but:
244 strlen(" PROTECTED") +
247 strlen(" SYNCHRONIZED") +
248 strlen(" VOLATILE") +
249 strlen(" TRANSIENT") +
251 strlen(" INTERFACE") +
252 strlen(" ABSTRACT") +
253 strlen(" METHOD_BUILTIN");
255 /* add maximal argument length */
259 strlen("-9223372036854775808 (0x123456789abcdef0), ") * md->paramcount +
263 /* allocate memory */
267 logtext = DMNEW(char, logtextlen);
269 TRACEJAVACALLCOUNT++;
271 sprintf(logtext, "%10d ", TRACEJAVACALLCOUNT);
272 sprintf(logtext + strlen(logtext), "-%d-", TRACEJAVACALLINDENT);
274 pos = strlen(logtext);
276 for (i = 0; i < TRACEJAVACALLINDENT; i++)
277 logtext[pos++] = '\t';
279 strcpy(logtext + pos, "called: ");
281 if (m->clazz != NULL)
282 utf_cat_classname(logtext, m->clazz->name);
284 strcat(logtext, "NULL");
285 strcat(logtext, ".");
286 utf_cat(logtext, m->name);
287 utf_cat(logtext, m->descriptor);
289 if (m->flags & ACC_PUBLIC) strcat(logtext, " PUBLIC");
290 if (m->flags & ACC_PRIVATE) strcat(logtext, " PRIVATE");
291 if (m->flags & ACC_PROTECTED) strcat(logtext, " PROTECTED");
292 if (m->flags & ACC_STATIC) strcat(logtext, " STATIC");
293 if (m->flags & ACC_FINAL) strcat(logtext, " FINAL");
294 if (m->flags & ACC_SYNCHRONIZED) strcat(logtext, " SYNCHRONIZED");
295 if (m->flags & ACC_VOLATILE) strcat(logtext, " VOLATILE");
296 if (m->flags & ACC_TRANSIENT) strcat(logtext, " TRANSIENT");
297 if (m->flags & ACC_NATIVE) strcat(logtext, " NATIVE");
298 if (m->flags & ACC_INTERFACE) strcat(logtext, " INTERFACE");
299 if (m->flags & ACC_ABSTRACT) strcat(logtext, " ABSTRACT");
301 strcat(logtext, "(");
303 for (i = 0; i < md->paramcount; ++i) {
304 arg = argument_jitarray_load(md, i, arg_regs, stack);
305 logtext = trace_java_call_print_argument(m, logtext, &logtextlen,
306 &md->paramtypes[i], arg);
307 if (i != (md->paramcount - 1)) {
308 strcat(logtext, ", ");
312 strcat(logtext, ")");
320 TRACEJAVACALLINDENT++;
324 /* trace_java_call_exit ********************************************************
326 Traces an exit form a java method.
328 return_regs: Array of size 1 containing return register.
329 The array is usually allocated on the stack and used for restoring the
332 *******************************************************************************/
334 void trace_java_call_exit(methodinfo *m, uint64_t *return_regs)
344 /* We don't trace builtin functions here because the argument
345 passing happens via the native ABI and does not fit these
348 if (method_is_builtin(m))
351 #if defined(ENABLE_DEBUG_FILTER)
352 if (!show_filters_test_verbosecall_exit(m))
356 #if defined(ENABLE_VMLOG)
357 vmlog_cacao_leave_method(m);
363 /* outdent the log message */
365 if (TRACEJAVACALLINDENT)
366 TRACEJAVACALLINDENT--;
368 log_text("trace_java_call_exit: WARNING: unmatched unindent");
370 /* calculate message length */
373 strlen("4294967295 ") +
374 strlen("-2147483647-") + /* INT_MAX should be sufficient */
375 TRACEJAVACALLINDENT +
376 strlen("finished: ") +
377 ((m->clazz == NULL) ? strlen("NULL") : utf_bytes(m->clazz->name)) +
380 utf_bytes(m->descriptor) +
381 strlen(" SYNCHRONIZED") + strlen("(") + strlen(")");
383 /* add maximal argument length */
385 logtextlen += strlen("->0.4872328470301428 (0x0123456789abcdef)");
387 /* allocate memory */
391 logtext = DMNEW(char, logtextlen);
393 /* generate the message */
395 sprintf(logtext, " ");
396 sprintf(logtext + strlen(logtext), "-%d-", TRACEJAVACALLINDENT);
398 pos = strlen(logtext);
400 for (i = 0; i < TRACEJAVACALLINDENT; i++)
401 logtext[pos++] = '\t';
403 strcpy(logtext + pos, "finished: ");
404 if (m->clazz != NULL)
405 utf_cat_classname(logtext, m->clazz->name);
407 strcat(logtext, "NULL");
408 strcat(logtext, ".");
409 utf_cat(logtext, m->name);
410 utf_cat(logtext, m->descriptor);
412 if (!IS_VOID_TYPE(md->returntype.type)) {
413 strcat(logtext, "->");
414 val = argument_jitreturn_load(md, return_regs);
417 trace_java_call_print_argument(m, logtext, &logtextlen,
418 &md->returntype, val);
429 /* trace_exception *************************************************************
431 Traces an exception which is handled by exceptions_handle_exception.
433 *******************************************************************************/
435 void trace_exception(java_object_t *xptr, methodinfo *m, void *pos)
442 /* calculate message length */
446 strlen("Exception ") + utf_bytes(xptr->vftbl->clazz->name);
449 logtextlen = strlen("Some Throwable");
452 logtextlen += strlen(" thrown in ");
456 utf_bytes(m->clazz->name) +
459 utf_bytes(m->descriptor) +
460 strlen("(NOSYNC,NATIVE");
462 #if SIZEOF_VOID_P == 8
464 strlen(")(0x123456789abcdef0) at position 0x123456789abcdef0 (");
466 logtextlen += strlen(")(0x12345678) at position 0x12345678 (");
469 if (m->clazz->sourcefile == NULL)
470 logtextlen += strlen("<NO CLASSFILE INFORMATION>");
472 logtextlen += utf_bytes(m->clazz->sourcefile);
474 logtextlen += strlen(":65536)");
478 logtextlen += strlen("call_java_method");
481 logtextlen += strlen("0");
483 /* allocate memory */
487 logtext = DMNEW(char, logtextlen);
490 strcpy(logtext, "Exception ");
491 utf_cat_classname(logtext, xptr->vftbl->clazz->name);
494 strcpy(logtext, "Some Throwable");
497 strcat(logtext, " thrown in ");
500 utf_cat_classname(logtext, m->clazz->name);
501 strcat(logtext, ".");
502 utf_cat(logtext, m->name);
503 utf_cat(logtext, m->descriptor);
505 if (m->flags & ACC_SYNCHRONIZED)
506 strcat(logtext, "(SYNC");
508 strcat(logtext, "(NOSYNC");
510 if (m->flags & ACC_NATIVE) {
511 strcat(logtext, ",NATIVE");
515 #if SIZEOF_VOID_P == 8
516 sprintf(logtext + strlen(logtext),
517 ")(0x%016lx) at position 0x%016lx",
518 (ptrint) code->entrypoint, (ptrint) pos);
520 sprintf(logtext + strlen(logtext),
521 ")(0x%08x) at position 0x%08x",
522 (ptrint) code->entrypoint, (ptrint) pos);
527 /* XXX preliminary: This should get the actual codeinfo */
528 /* in which the exception happened. */
531 #if SIZEOF_VOID_P == 8
532 sprintf(logtext + strlen(logtext),
533 ")(0x%016lx) at position 0x%016lx (",
534 (ptrint) code->entrypoint, (ptrint) pos);
536 sprintf(logtext + strlen(logtext),
537 ")(0x%08x) at position 0x%08x (",
538 (ptrint) code->entrypoint, (ptrint) pos);
541 if (m->clazz->sourcefile == NULL)
542 strcat(logtext, "<NO CLASSFILE INFORMATION>");
544 utf_cat(logtext, m->clazz->sourcefile);
546 sprintf(logtext + strlen(logtext), ":%d)", 0);
550 strcat(logtext, "call_java_method");
560 /* trace_exception_builtin *****************************************************
562 Traces an exception which is thrown by builtin_throw_exception.
564 *******************************************************************************/
566 void trace_exception_builtin(java_handle_t* h)
572 java_lang_Throwable jlt(h);
574 // Get detail message.
575 java_handle_t* s = NULL;
577 if (jlt.get_handle() != NULL)
578 s = jlt.get_detailMessage();
580 java_lang_String jls(s);
582 /* calculate message length */
584 logtextlen = strlen("Builtin exception thrown: ") + strlen("0");
586 if (jlt.get_handle() != NULL) {
587 logtextlen += utf_bytes(jlt.get_vftbl()->clazz->name);
589 if (jls.get_handle()) {
590 // FIXME This is not handle capable!
591 logtextlen += strlen(": ") +
592 u2_utflength(jls.get_value()->data + jls.get_offset(), jls.get_count());
596 logtextlen += strlen("(nil)");
599 /* allocate memory */
603 logtext = DMNEW(char, logtextlen);
605 strcpy(logtext, "Builtin exception thrown: ");
607 if (jlt.get_handle()) {
608 utf_cat_classname(logtext, jlt.get_vftbl()->clazz->name);
613 buf = javastring_tochar(jls.get_handle());
614 strcat(logtext, ": ");
615 strcat(logtext, buf);
616 MFREE(buf, char, strlen(buf) + 1);
620 strcat(logtext, "(nil)");
632 #endif /* !defined(NDEBUG) */
636 * These are local overrides for various environment variables in Emacs.
637 * Please do not remove this and leave it at the end of the file, where
638 * Emacs will automagically detect them.
639 * ---------------------------------------------------------------------
642 * indent-tabs-mode: t
646 * vim:noexpandtab:sw=4:ts=4: