From 686f6ce703dfeca4a8c3dfa98d0f22c614652860 Mon Sep 17 00:00:00 2001 From: Nick Kamal Date: Tue, 19 Nov 2024 14:28:35 -0500 Subject: [PATCH] Print actual string arguments with -Xtrace (part 2) The changes reflect the feature request #16416. Adding cmdline option to specify string argument length. -Xtrace:maxstringlength=[1-128] maxstringlength takes an unsigned integer value from 1 to 128 If maxstringlength = 0 or unspecified, default to address printing. Other invalid inputs result in the input error. There will be subsequent PR for tests (part 3). Signed-off-by: Nick Kamal --- runtime/oti/j9trace.h | 7 +++++ runtime/rasdump/dmpsup.c | 3 +- runtime/rastrace/j9rastrace.h | 6 ++-- runtime/rastrace/method_trace.c | 13 +++++---- runtime/rastrace/method_trigger.c | 48 +++++++++++++++++++++++++++---- runtime/rastrace/trcengine.c | 3 +- 6 files changed, 65 insertions(+), 15 deletions(-) diff --git a/runtime/oti/j9trace.h b/runtime/oti/j9trace.h index 03fe720b4a0..149f73ed3ec 100644 --- a/runtime/oti/j9trace.h +++ b/runtime/oti/j9trace.h @@ -63,12 +63,19 @@ typedef struct RasGlobalStorage { void * traceMethodTable; int stackdepth; unsigned int stackCompressionLevel; + unsigned int maxStringLength; ConfigureTraceFunction configureTraceEngine; #if defined(J9VM_OPT_CRIU_SUPPORT) CRIURestoreInitializeTrace criuRestoreInitializeTrace; #endif /* defined(J9VM_OPT_CRIU_SUPPORT) */ } RasGlobalStorage; +/* + * Default and maximum values for RasGlobalStorage.maxStringLength. + */ +#define RAS_MAX_STRING_LENGTH_DEFAULT 32 +#define RAS_MAX_STRING_LENGTH_LIMIT 128 + #define RAS_GLOBAL(x) ((RasGlobalStorage *)thr->javaVM->j9rasGlobalStorage)->x #define RAS_GLOBAL_FROM_JAVAVM(x,vm) ((RasGlobalStorage *)vm->j9rasGlobalStorage)->x diff --git a/runtime/rasdump/dmpsup.c b/runtime/rasdump/dmpsup.c index 5c62fbb1ad9..7ef3900c826 100644 --- a/runtime/rasdump/dmpsup.c +++ b/runtime/rasdump/dmpsup.c @@ -1570,7 +1570,8 @@ J9VMDllMain(J9JavaVM *vm, IDATA stage, void *reserved) /* RAS init may happen in either dump or trace */ vm->j9rasGlobalStorage = j9mem_allocate_memory(sizeof(RasGlobalStorage), OMRMEM_CATEGORY_VM); if (vm->j9rasGlobalStorage != NULL) { - memset (vm->j9rasGlobalStorage, '\0', sizeof(RasGlobalStorage)); + memset(vm->j9rasGlobalStorage, '\0', sizeof(RasGlobalStorage)); + RAS_GLOBAL_FROM_JAVAVM(maxStringLength, vm) = RAS_MAX_STRING_LENGTH_DEFAULT; } } break; diff --git a/runtime/rastrace/j9rastrace.h b/runtime/rastrace/j9rastrace.h index d754ec60f0d..654be067403 100644 --- a/runtime/rastrace/j9rastrace.h +++ b/runtime/rastrace/j9rastrace.h @@ -54,6 +54,7 @@ extern "C" { #define RAS_STACKDEPTH_KEYWORD "STACKDEPTH" #define RAS_SLEEPTIME_KEYWORD "SLEEPTIME" #define RAS_COMPRESSION_LEVEL_KEYWORD "STACKCOMPRESSIONLEVEL" +#define RAS_MAX_STRING_LENGTH_KEYWORD "MAXSTRINGLENGTH" /* * ====================================================================== @@ -128,8 +129,9 @@ void rasTriggerMethod(J9VMThread *thr, J9Method *mb, I_32 entry, const TriggerPh BOOLEAN matchMethod (RasMethodTable * methodTable, J9Method *method); omr_error_t processTriggerMethodClause(OMR_VMThread *, char *, BOOLEAN atRuntime); void doTriggerActionJstacktrace(OMR_VMThread *thr); -omr_error_t setStackDepth(J9JavaVM *thr, const char * value, BOOLEAN atRuntime); -omr_error_t setStackCompressionLevel(J9JavaVM * vm, const char *str, BOOLEAN atRuntime); +omr_error_t setStackDepth(J9JavaVM *thr, const char *value, BOOLEAN atRuntime); +omr_error_t setStackCompressionLevel(J9JavaVM *vm, const char *str, BOOLEAN atRuntime); +omr_error_t setMaxStringLength(J9JavaVM *vm, const char *str, BOOLEAN atRuntime); /* * ============================================================================= diff --git a/runtime/rastrace/method_trace.c b/runtime/rastrace/method_trace.c index 82b3eb207d5..b4ea0ce58ce 100644 --- a/runtime/rastrace/method_trace.c +++ b/runtime/rastrace/method_trace.c @@ -476,11 +476,13 @@ traceMethodArgObject(J9VMThread *thr, UDATA* arg0EA, char* cursor, UDATA length) } else { J9Class *clazz = J9OBJECT_CLAZZ(thr, object); J9JavaVM *vm = thr->javaVM; + const unsigned int maxStringLength = RAS_GLOBAL_FROM_JAVAVM(maxStringLength, vm); - if (clazz == J9VMJAVALANGSTRING_OR_NULL(vm)) { + if (clazz == J9VMJAVALANGSTRING_OR_NULL(vm) + && (0 != maxStringLength) + ) { /* string argument */ -#define DEFAULT_STRING_LENGTH 32 - char utf8Buffer[128]; + char utf8Buffer[RAS_MAX_STRING_LENGTH_LIMIT + 1]; UDATA utf8Length = 0; char *utf8String = vm->internalVMFunctions->copyStringToUTF8WithMemAlloc( @@ -495,8 +497,8 @@ traceMethodArgObject(J9VMThread *thr, UDATA* arg0EA, char* cursor, UDATA length) if (NULL == utf8String) { j9str_printf(PORTLIB, cursor, length, "(String)"); - } else if (utf8Length > DEFAULT_STRING_LENGTH) { - j9str_printf(PORTLIB, cursor, length, "(String)\"%.*s\"...", (U_32)DEFAULT_STRING_LENGTH, utf8String); + } else if (utf8Length > maxStringLength) { + j9str_printf(PORTLIB, cursor, length, "(String)\"%.*s\"...", (U_32)maxStringLength, utf8String); } else { j9str_printf(PORTLIB, cursor, length, "(String)\"%.*s\"", (U_32)utf8Length, utf8String); } @@ -504,7 +506,6 @@ traceMethodArgObject(J9VMThread *thr, UDATA* arg0EA, char* cursor, UDATA length) if (utf8Buffer != utf8String) { j9mem_free_memory(utf8String); } -#undef DEFAULT_STRING_LENGTH } else { /* TODO: handle arrays */ diff --git a/runtime/rastrace/method_trigger.c b/runtime/rastrace/method_trigger.c index cccaddde31f..2600c492c41 100644 --- a/runtime/rastrace/method_trigger.c +++ b/runtime/rastrace/method_trigger.c @@ -60,7 +60,6 @@ static const StackTraceFormattingFunction stackTraceFormattingFunctions[] = { #define NUM_STACK_TRACE_FORMATTING_FUNCTIONS (sizeof(stackTraceFormattingFunctions) / sizeof(stackTraceFormattingFunctions[0])) - /************************************************************************** * name - rasSetTriggerTrace * description - Called whenever a class is loaded. @@ -547,6 +546,41 @@ decimalString2Int(J9PortLibrary* portLibrary, const char *decString, I_32 signed return num; } +/************************************************************************** + * name - setMaxStringLength + * description - Set max string length for arguments and return values + * parameters - vm, str - trace options, atRuntime flag + * returns - JNI return code + *************************************************************************/ +omr_error_t +setMaxStringLength(J9JavaVM *vm, const char *str, BOOLEAN atRuntime) +{ + PORT_ACCESS_FROM_JAVAVM(vm); + int value = 0; + int length = 0; + omr_error_t rc = OMR_ERROR_NONE; + const char *param = NULL; + + if (1 != getParmNumber(str)) { + goto err; + } + + param = getPositionalParm(1, str, &length); + + value = decimalString2Int(PORTLIB, param, FALSE, &rc); + if (OMR_ERROR_NONE != rc) { + goto err; + } + + if ((0 <= value) && (value <= RAS_MAX_STRING_LENGTH_LIMIT)) { + RAS_GLOBAL_FROM_JAVAVM(maxStringLength, vm) = (unsigned int)value; + return OMR_ERROR_NONE; + } +err: + vaReportJ9VMCommandLineError(PORTLIB, "maxstringlength takes an integer value from 0 to %d", RAS_MAX_STRING_LENGTH_LIMIT); + return OMR_ERROR_INTERNAL; +} + /************************************************************************** * name - addTriggeredMethodSpec * description - Take a user specified method trigger rule (from the @@ -653,13 +687,17 @@ addTriggeredMethodSpec(J9VMThread *thr, const char *ptrMethodSpec, const struct ptr->next = methodRule; } - if (methodRule->entryAction != NULL && methodRule->entryAction->name != NULL - && j9_cmdla_stricmp((char *)methodRule->entryAction->name, "jstacktrace") == 0) { + if ((NULL != methodRule->entryAction) + && (NULL != methodRule->entryAction->name) + && (0 == j9_cmdla_stricmp((char *)methodRule->entryAction->name, "jstacktrace")) + ) { /* set up the current method spec to be enabled for trace */ setMethod(thr->javaVM, ptrMethodSpec, FALSE); } - if (methodRule->exitAction != NULL && methodRule->exitAction->name != NULL - && j9_cmdla_stricmp((char *)methodRule->exitAction->name, "jstacktrace") == 0) { + if ((NULL != methodRule->exitAction) + && (NULL != methodRule->exitAction->name) + && (0 == j9_cmdla_stricmp((char *)methodRule->exitAction->name, "jstacktrace")) + ) { /* set up the current method spec to be enabled for trace */ setMethod(thr->javaVM, ptrMethodSpec, FALSE); } diff --git a/runtime/rastrace/trcengine.c b/runtime/rastrace/trcengine.c index 2c07f8a11a2..78680dea0c6 100644 --- a/runtime/rastrace/trcengine.c +++ b/runtime/rastrace/trcengine.c @@ -117,9 +117,10 @@ const struct traceOption TRACE_OPTIONS[] = {RAS_METHODS_KEYWORD, FALSE, setMethod}, {RAS_STACKDEPTH_KEYWORD, TRUE, setStackDepth}, {RAS_COMPRESSION_LEVEL_KEYWORD, TRUE, setStackCompressionLevel}, + {RAS_MAX_STRING_LENGTH_KEYWORD, FALSE, setMaxStringLength}, }; -#define NUMBER_OF_TRACE_OPTIONS ( sizeof(TRACE_OPTIONS) / sizeof(struct traceOption)) +#define NUMBER_OF_TRACE_OPTIONS (sizeof(TRACE_OPTIONS) / sizeof(struct traceOption)) /** * The list of trigger actions defined by J9VM passed to the