From e5ef9f302d71a7830d1001400191e8cb5cd59278 Mon Sep 17 00:00:00 2001 From: Nick Kamal Date: Tue, 19 Nov 2024 14:28:35 -0500 Subject: [PATCH] Feature request: Print actual String arguments with Xtrace part 1 The changes reflect the feature request #16416. Instead of printing the memory address for string arguments, print the actual string at max of 32 characters. There will be subsequent PRs for cmdline option for string length (Part 2) and tests (Part 3). Signed-off-by: Nick Kamal nick.kamal@ibm.com --- runtime/rastrace/method_trace.c | 43 +++++++++------------------------ 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/runtime/rastrace/method_trace.c b/runtime/rastrace/method_trace.c index bc489a837f4..eca88af2ea9 100644 --- a/runtime/rastrace/method_trace.c +++ b/runtime/rastrace/method_trace.c @@ -477,48 +477,29 @@ traceMethodArgObject(J9VMThread *thr, UDATA* arg0EA, char* cursor, UDATA length) if (object == NULL) { j9str_printf(PORTLIB, cursor, length, "null"); } else { - /* string arg */ - J9Class *clazz = J9OBJECT_CLAZZ(thr, object); + J9Class* clazz = J9OBJECT_CLAZZ(thr, object); + J9ROMClass * romClass = clazz->romClass; + J9UTF8* className = J9ROMCLASS_CLASSNAME(romClass); J9JavaVM *vm = thr->javaVM; if (clazz == J9VMJAVALANGSTRING_OR_NULL(vm)) { + /* string arg */ + char stringArgBuffer[DEFAULT_BUFFER_LENGTH]; -#define DEFAULT_STRING_LENGTH 32 + J9InternalVMFunctions const * const vmFuncs = thr->javaVM->internalVMFunctions; + char *stringArgUTF8 = vmFuncs->copyStringToUTF8WithMemAlloc(thr, object, J9_STR_NULL_TERMINATE_RESULT, " ", 2, stringArgBuffer, DEFAULT_BUFFER_LENGTH, NULL); - char utf8Buffer[128]; - UDATA utf8Length = 0; - const unsigned int methodStrArgLength = ((RasGlobalStorage *)thr->javaVM->j9rasGlobalStorage)->methodStrArgLength; - unsigned int strArgLength = methodStrArgLength == 0 ? DEFAULT_STRING_LENGTH : methodStrArgLength; - - char *utf8String = vm->internalVMFunctions->copyStringToUTF8WithMemAlloc( - thr, - object, - 0, - "", - 0, - utf8Buffer, - sizeof(utf8Buffer), - &utf8Length); - - if (NULL == utf8String) { - j9str_printf(PORTLIB, cursor, length, "(String)"); - } else if (utf8Length > strArgLength) { - j9str_printf(PORTLIB, cursor, length, "(String)\"%.*s\"...", (U_32)strArgLength, utf8String); + if(DEFAULT_STRING_LENGTH < strlen(stringArgUTF8)) { + j9str_printf(PORTLIB, cursor, length, "(String)%.*s...", (U_32)DEFAULT_STRING_LENGTH, J9UTF8_DATA(stringArgUTF8)); } else { - j9str_printf(PORTLIB, cursor, length, "(String)\"%.*s\"", (U_32)utf8Length, utf8String); + j9str_printf(PORTLIB, cursor, length, "(String)%.*s", (U_32)J9UTF8_LENGTH(stringArgUTF8), J9UTF8_DATA(stringArgUTF8)); } - if (utf8Buffer != utf8String) { - j9mem_free_memory(utf8String); + if ((char*)stringArgBuffer != stringArgUTF8) { + j9mem_free_memory(stringArgUTF8); } - -#undef DEFAULT_STRING_LENGTH - } else { /* TODO: handle arrays */ - - J9ROMClass *romClass = clazz->romClass; - J9UTF8 *className = J9ROMCLASS_CLASSNAME(romClass); j9str_printf(PORTLIB, cursor, length, "%.*s@%p", (U_32)J9UTF8_LENGTH(className), J9UTF8_DATA(className), object); } }