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 don't trace builtin functions here because the argument
207 passing happens via the native ABI and does not fit these
210 if (method_is_builtin(m))
213 #if defined(ENABLE_DEBUG_FILTER)
214 if (!show_filters_test_verbosecall_enter(m))
218 #if defined(ENABLE_VMLOG)
219 vmlog_cacao_enter_method(m);
225 /* calculate message length */
228 strlen("4294967295 ") +
229 strlen("-2147483647-") + /* INT_MAX should be sufficient */
230 TRACEJAVACALLINDENT +
232 ((m->clazz == NULL) ? strlen("NULL") : utf_bytes(m->clazz->name)) +
235 utf_bytes(m->descriptor);
237 /* Actually it's not possible to have all flags printed, but:
243 strlen(" PROTECTED") +
246 strlen(" SYNCHRONIZED") +
247 strlen(" VOLATILE") +
248 strlen(" TRANSIENT") +
250 strlen(" INTERFACE") +
251 strlen(" ABSTRACT") +
252 strlen(" METHOD_BUILTIN");
254 /* add maximal argument length */
258 strlen("-9223372036854775808 (0x123456789abcdef0), ") * md->paramcount +
262 // Create new dump memory area.
265 // TODO Use a std::string here.
266 logtext = (char*) DumpMemory::allocate(sizeof(char) * logtextlen);
268 TRACEJAVACALLCOUNT++;
270 sprintf(logtext, "%10d ", TRACEJAVACALLCOUNT);
271 sprintf(logtext + strlen(logtext), "-%d-", TRACEJAVACALLINDENT);
273 pos = strlen(logtext);
275 for (i = 0; i < TRACEJAVACALLINDENT; i++)
276 logtext[pos++] = '\t';
278 strcpy(logtext + pos, "called: ");
280 if (m->clazz != NULL)
281 utf_cat_classname(logtext, m->clazz->name);
283 strcat(logtext, "NULL");
284 strcat(logtext, ".");
285 utf_cat(logtext, m->name);
286 utf_cat(logtext, m->descriptor);
288 if (m->flags & ACC_PUBLIC) strcat(logtext, " PUBLIC");
289 if (m->flags & ACC_PRIVATE) strcat(logtext, " PRIVATE");
290 if (m->flags & ACC_PROTECTED) strcat(logtext, " PROTECTED");
291 if (m->flags & ACC_STATIC) strcat(logtext, " STATIC");
292 if (m->flags & ACC_FINAL) strcat(logtext, " FINAL");
293 if (m->flags & ACC_SYNCHRONIZED) strcat(logtext, " SYNCHRONIZED");
294 if (m->flags & ACC_VOLATILE) strcat(logtext, " VOLATILE");
295 if (m->flags & ACC_TRANSIENT) strcat(logtext, " TRANSIENT");
296 if (m->flags & ACC_NATIVE) strcat(logtext, " NATIVE");
297 if (m->flags & ACC_INTERFACE) strcat(logtext, " INTERFACE");
298 if (m->flags & ACC_ABSTRACT) strcat(logtext, " ABSTRACT");
300 strcat(logtext, "(");
302 for (i = 0; i < md->paramcount; ++i) {
303 arg = argument_jitarray_load(md, i, arg_regs, stack);
304 logtext = trace_java_call_print_argument(m, logtext, &logtextlen,
305 &md->paramtypes[i], arg);
306 if (i != (md->paramcount - 1)) {
307 strcat(logtext, ", ");
311 strcat(logtext, ")");
315 TRACEJAVACALLINDENT++;
318 /* trace_java_call_exit ********************************************************
320 Traces an exit form a java method.
322 return_regs: Array of size 1 containing return register.
323 The array is usually allocated on the stack and used for restoring the
326 *******************************************************************************/
328 void trace_java_call_exit(methodinfo *m, uint64_t *return_regs)
337 /* We don't trace builtin functions here because the argument
338 passing happens via the native ABI and does not fit these
341 if (method_is_builtin(m))
344 #if defined(ENABLE_DEBUG_FILTER)
345 if (!show_filters_test_verbosecall_exit(m))
349 #if defined(ENABLE_VMLOG)
350 vmlog_cacao_leave_method(m);
356 /* outdent the log message */
358 if (TRACEJAVACALLINDENT)
359 TRACEJAVACALLINDENT--;
361 log_text("trace_java_call_exit: WARNING: unmatched unindent");
363 /* calculate message length */
366 strlen("4294967295 ") +
367 strlen("-2147483647-") + /* INT_MAX should be sufficient */
368 TRACEJAVACALLINDENT +
369 strlen("finished: ") +
370 ((m->clazz == NULL) ? strlen("NULL") : utf_bytes(m->clazz->name)) +
373 utf_bytes(m->descriptor) +
374 strlen(" SYNCHRONIZED") + strlen("(") + strlen(")");
376 /* add maximal argument length */
378 logtextlen += strlen("->0.4872328470301428 (0x0123456789abcdef)");
380 // Create new dump memory area.
383 // TODO Use a std::string here.
384 logtext = (char*) DumpMemory::allocate(sizeof(char) * logtextlen);
386 /* generate the message */
388 sprintf(logtext, " ");
389 sprintf(logtext + strlen(logtext), "-%d-", TRACEJAVACALLINDENT);
391 pos = strlen(logtext);
393 for (i = 0; i < TRACEJAVACALLINDENT; i++)
394 logtext[pos++] = '\t';
396 strcpy(logtext + pos, "finished: ");
397 if (m->clazz != NULL)
398 utf_cat_classname(logtext, m->clazz->name);
400 strcat(logtext, "NULL");
401 strcat(logtext, ".");
402 utf_cat(logtext, m->name);
403 utf_cat(logtext, m->descriptor);
405 if (!IS_VOID_TYPE(md->returntype.type)) {
406 strcat(logtext, "->");
407 val = argument_jitreturn_load(md, return_regs);
410 trace_java_call_print_argument(m, logtext, &logtextlen,
411 &md->returntype, val);
418 /* trace_exception *************************************************************
420 Traces an exception which is handled by exceptions_handle_exception.
422 *******************************************************************************/
424 void trace_exception(java_object_t *xptr, methodinfo *m, void *pos)
430 /* calculate message length */
434 strlen("Exception ") + utf_bytes(xptr->vftbl->clazz->name);
437 logtextlen = strlen("Some Throwable");
440 logtextlen += strlen(" thrown in ");
444 utf_bytes(m->clazz->name) +
447 utf_bytes(m->descriptor) +
448 strlen("(NOSYNC,NATIVE");
450 #if SIZEOF_VOID_P == 8
452 strlen(")(0x123456789abcdef0) at position 0x123456789abcdef0 (");
454 logtextlen += strlen(")(0x12345678) at position 0x12345678 (");
457 if (m->clazz->sourcefile == NULL)
458 logtextlen += strlen("<NO CLASSFILE INFORMATION>");
460 logtextlen += utf_bytes(m->clazz->sourcefile);
462 logtextlen += strlen(":65536)");
466 logtextlen += strlen("call_java_method");
469 logtextlen += strlen("0");
471 // Create new dump memory area.
474 // TODO Use a std::string here.
475 logtext = (char*) DumpMemory::allocate(sizeof(char) * logtextlen);
478 strcpy(logtext, "Exception ");
479 utf_cat_classname(logtext, xptr->vftbl->clazz->name);
482 strcpy(logtext, "Some Throwable");
485 strcat(logtext, " thrown in ");
488 utf_cat_classname(logtext, m->clazz->name);
489 strcat(logtext, ".");
490 utf_cat(logtext, m->name);
491 utf_cat(logtext, m->descriptor);
493 if (m->flags & ACC_SYNCHRONIZED)
494 strcat(logtext, "(SYNC");
496 strcat(logtext, "(NOSYNC");
498 if (m->flags & ACC_NATIVE) {
499 strcat(logtext, ",NATIVE");
503 #if SIZEOF_VOID_P == 8
504 sprintf(logtext + strlen(logtext),
505 ")(0x%016lx) at position 0x%016lx",
506 (ptrint) code->entrypoint, (ptrint) pos);
508 sprintf(logtext + strlen(logtext),
509 ")(0x%08x) at position 0x%08x",
510 (ptrint) code->entrypoint, (ptrint) pos);
515 /* XXX preliminary: This should get the actual codeinfo */
516 /* in which the exception happened. */
519 #if SIZEOF_VOID_P == 8
520 sprintf(logtext + strlen(logtext),
521 ")(0x%016lx) at position 0x%016lx (",
522 (ptrint) code->entrypoint, (ptrint) pos);
524 sprintf(logtext + strlen(logtext),
525 ")(0x%08x) at position 0x%08x (",
526 (ptrint) code->entrypoint, (ptrint) pos);
529 if (m->clazz->sourcefile == NULL)
530 strcat(logtext, "<NO CLASSFILE INFORMATION>");
532 utf_cat(logtext, m->clazz->sourcefile);
534 sprintf(logtext + strlen(logtext), ":%d)", 0);
538 strcat(logtext, "call_java_method");
544 /* trace_exception_builtin *****************************************************
546 Traces an exception which is thrown by builtin_throw_exception.
548 *******************************************************************************/
550 void trace_exception_builtin(java_handle_t* h)
555 java_lang_Throwable jlt(h);
557 // Get detail message.
558 java_handle_t* s = NULL;
560 if (jlt.get_handle() != NULL)
561 s = jlt.get_detailMessage();
563 java_lang_String jls(s);
565 /* calculate message length */
567 logtextlen = strlen("Builtin exception thrown: ") + strlen("0");
569 if (jlt.get_handle() != NULL) {
570 logtextlen += utf_bytes(jlt.get_vftbl()->clazz->name);
572 if (jls.get_handle()) {
573 CharArray ca(jls.get_value());
574 // FIXME This is not handle capable!
575 uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
576 logtextlen += strlen(": ") +
577 u2_utflength(ptr + jls.get_offset(), jls.get_count());
581 logtextlen += strlen("(nil)");
584 // Create new dump memory area.
587 logtext = (char*) DumpMemory::allocate(sizeof(char) * logtextlen);
589 strcpy(logtext, "Builtin exception thrown: ");
591 if (jlt.get_handle()) {
592 utf_cat_classname(logtext, jlt.get_vftbl()->clazz->name);
597 buf = javastring_tochar(jls.get_handle());
598 strcat(logtext, ": ");
599 strcat(logtext, buf);
600 MFREE(buf, char, strlen(buf) + 1);
604 strcat(logtext, "(nil)");
612 #endif /* !defined(NDEBUG) */
616 * These are local overrides for various environment variables in Emacs.
617 * Please do not remove this and leave it at the end of the file, where
618 * Emacs will automagically detect them.
619 * ---------------------------------------------------------------------
622 * indent-tabs-mode: t
626 * vim:noexpandtab:sw=4:ts=4: