Skip to content

Commit

Permalink
Feature request: Print actual String arguments with Xtrace part 1
Browse files Browse the repository at this point in the history
The changes reflect the feature request eclipse-openj9#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 [email protected]
  • Loading branch information
h3110n3rv3 committed Dec 17, 2024
1 parent a02170c commit e5ef9f3
Showing 1 changed file with 12 additions and 31 deletions.
43 changes: 12 additions & 31 deletions runtime/rastrace/method_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)<Memory allocation error>");
} 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);
}
}
Expand Down

0 comments on commit e5ef9f3

Please sign in to comment.