static omr_error_t addDeferredConfigToList(const char *componentName, int32_t all, int32_t first, int32_t last, unsigned char value, int level, const char *groupName, UtDeferredConfigInfo **configList, int32_t setActive) { UtDeferredConfigInfo *dconfiginfo; UtDeferredConfigInfo *temp; OMRPORT_ACCESS_FROM_OMRPORT(OMR_TRACEGLOBAL(portLibrary)); /* 1) add to deferred options for components that register */ UT_DBGOUT(2, ("<UT> setTracePointsTo: component %s applying to all and adding to global deferred", componentName)); dconfiginfo = (UtDeferredConfigInfo *)omrmem_allocate_memory(sizeof(UtDeferredConfigInfo), OMRMEM_CATEGORY_TRACE); if (dconfiginfo == NULL) { UT_DBGOUT(1, ("<UT> Unable to set tracepoints in %s - can't allocate config info\n", componentName)); return OMR_ERROR_OUT_OF_NATIVE_MEMORY; } dconfiginfo->componentName = (char *)omrmem_allocate_memory(strlen(componentName) + 1, OMRMEM_CATEGORY_TRACE); if (dconfiginfo->componentName == NULL) { UT_DBGOUT(1, ("<UT> Unable to set tracepoints in %s - can't allocate config info componentName\n", componentName)); return OMR_ERROR_OUT_OF_NATIVE_MEMORY; } strcpy(dconfiginfo->componentName, componentName); dconfiginfo->all = all; dconfiginfo->firstTracePoint = first; dconfiginfo->lastTracePoint = last; dconfiginfo->value = value; dconfiginfo->level = level; dconfiginfo->setActive = setActive; if (groupName == NULL) { dconfiginfo->groupName = NULL; } else { dconfiginfo->groupName = (char *)omrmem_allocate_memory(strlen(groupName) + 1, OMRMEM_CATEGORY_TRACE); if (dconfiginfo->groupName == NULL) { UT_DBGOUT(1, ("<UT> Unable to set tracepoints in %s - can't allocate config info groupName\n", componentName)); return OMR_ERROR_OUT_OF_NATIVE_MEMORY; } strcpy(dconfiginfo->groupName, groupName); } dconfiginfo->next = NULL; if (*configList == NULL) { *configList = dconfiginfo; } else { temp = *configList; while (temp->next != NULL) { temp = temp->next; } temp->next = dconfiginfo; } return OMR_ERROR_NONE; }
/** * Write a formatted string to the console, lookup the last port error message and port error number, then store it. * * Update the number of failed tests * * * @param[in] portLibrary The port library * @param[in] fileName File requesting message output * @param[in] lineNumber Line number in the file of request * @param[in] testName Name of the test requesting output * @param[in] foramt Format of string to be output * @param[in] ... argument list for format string */ void outputErrorMessage(struct OMRPortLibrary *portLibrary, const char *fileName, int32_t lineNumber, const char *testName, const char *format, ...) { char *buf, *portErrorBuf = NULL; uintptr_t sizeBuf; size_t sizePortErrorBuf; va_list args; char *lastErrorMessage = NULL; int32_t lastErrorNumber = 0; OMRPORT_ACCESS_FROM_OMRPORT(portLibrary); lastErrorMessage = (char *)omrerror_last_error_message(); lastErrorNumber = omrerror_last_error_number(); /* Capture the error message now * Get the size needed to hold the last error message (don't use str_printf to avoid polluting error message */ sizePortErrorBuf = strlen(lastErrorMessage) + 1 /* for the null terminator */; portErrorBuf = (char *)omrmem_allocate_memory(sizePortErrorBuf, OMRMEM_CATEGORY_PORT_LIBRARY); if (NULL != portErrorBuf) { strncpy(portErrorBuf, lastErrorMessage, sizePortErrorBuf); } else { portTestEnv->log(LEVEL_ERROR, "\n\n******* omrmem_allocate_memory failed to allocate %i bytes, exiting.\n\n", sizePortErrorBuf); exit(EXIT_OUT_OF_MEMORY); } va_start(args, format); /* get the size needed to hold the error message that was passed in */ sizeBuf = omrstr_vprintf(NULL, 0, format, args); buf = (char *)omrmem_allocate_memory(sizeBuf, OMRMEM_CATEGORY_PORT_LIBRARY); if (NULL != buf) { omrstr_vprintf(buf, sizeBuf, format, args); } else { portTestEnv->log(LEVEL_ERROR, "\n\n******* omrmem_allocate_memory failed to allocate %i bytes, exiting.\n\n", sizeBuf); exit(EXIT_OUT_OF_MEMORY); } va_end(args); portTestEnv->log(LEVEL_ERROR, "%s line %4zi: %s ", fileName, lineNumber, testName); portTestEnv->log(LEVEL_ERROR, "%s\n", buf); portTestEnv->log(LEVEL_ERROR, "\t\tLastErrorNumber: %i\n", lastErrorNumber); portTestEnv->log(LEVEL_ERROR, "\t\tLastErrorMessage: %s\n\n", portErrorBuf); logTestFailure(OMRPORTLIB, fileName, lineNumber, testName, lastErrorNumber, portErrorBuf, buf); omrmem_free_memory(portErrorBuf); omrmem_free_memory(buf); numberFailedTestsInComponent++; }
OMR_Agent::OMR_Agent(OMR_VM *vm, char const *arg) : _handle(0), _vm(vm), _onload(NULL), _onunload(NULL), _dllpath(NULL), _options(NULL), _state(UNINITIALIZED) { OMRPORT_ACCESS_FROM_OMRVM(_vm); _agentCallbacks.version = 0; _agentCallbacks.onPreFork = onPreForkDefault; _agentCallbacks.onPostForkParent = onPostForkParentDefault; _agentCallbacks.onPostForkChild = onPostForkChildDefault; size_t len = strlen(arg) + 1; _dllpath = (char const *)omrmem_allocate_memory(len, OMRMEM_CATEGORY_VM); if (NULL != _dllpath) { memcpy((void *)_dllpath, arg, len); char *equals = strchr((char *)_dllpath, '='); if (NULL != equals) { *equals = '\0'; /* split the options from the dllpath */ if ('\0' != equals[1]) { _options = &equals[1]; } } _state = INITIALIZED; } }
ObjectEntry * GCConfigTest::createObject(const char *namePrefix, OMRGCObjectType objType, int32_t depth, int32_t nthInRow, uintptr_t size) { OMRPORT_ACCESS_FROM_OMRPORT(gcTestEnv->portLib); ObjectEntry *objEntry = NULL; char *objName = (char *)omrmem_allocate_memory(MAX_NAME_LENGTH, OMRMEM_CATEGORY_MM); if (NULL == objName) { omrtty_printf("%s:%d Failed to allocate native memory.\n", __FILE__, __LINE__); goto done; } omrstr_printf(objName, MAX_NAME_LENGTH, "%s_%d_%d", namePrefix, depth, nthInRow); objEntry = find(objName); if (NULL != objEntry) { #if defined(OMRGCTEST_DEBUG) omrtty_printf("Found object %s in object table.\n", objEntry->name); #endif omrmem_free_memory(objName); } else { objEntry = allocateHelper(objName, size); if (NULL != objEntry) { /* Keep count of the new allocated non-garbage object size for garbage insertion. If the object exists in objectTable, its size is ignored. */ if ((ROOT == objType) || (NORMAL == objType)) { gp.accumulatedSize += env->getExtensions()->objectModel.getSizeInBytesWithHeader(objEntry->objPtr); } } else { omrmem_free_memory(objName); } } done: return objEntry; }
void OMR_MethodDictionary::formatEntryProperties(const OMR_MethodDictionaryEntry *entry, char **str) const { if (_numProperties == 0) { *str = NULL; } else { OMRPORT_ACCESS_FROM_OMRVM(_vm); size_t buflen = _numProperties; /* space between each property + nul-terminator, might include some extra if we have nulls */ for (size_t i = 0; i < _numProperties; ++i) { if (NULL != entry->propertyValues[i]) { buflen += strlen(entry->propertyValues[i]); } } char *buf = (char *)omrmem_allocate_memory(buflen, OMRMEM_CATEGORY_OMRTI); uintptr_t bufUsed = 0; if (NULL != buf) { bufUsed += omrstr_printf(buf, (uintptr_t)buflen, "%s", entry->propertyValues[0]); for (size_t i = 1; i < _numProperties; ++i) { if (NULL != entry->propertyValues[i]) { bufUsed += omrstr_printf(buf + bufUsed, (uintptr_t)buflen - bufUsed, " %s", entry->propertyValues[i]); } } } *str = buf; } }
omr_error_t OMR_MethodDictionary::dupEntryStrings(OMR_MethodDictionaryEntry *dest, const OMR_MethodDictionaryEntry *src) { omr_error_t rc = OMR_ERROR_NONE; OMRPORT_ACCESS_FROM_OMRVM(_vm); for (size_t i = 0; i < _numProperties; i++) { if (NULL != src->propertyValues[i]) { size_t len = strlen(src->propertyValues[i]) + 1; void *copy = omrmem_allocate_memory(len, OMRMEM_CATEGORY_OMRTI); if (NULL != copy) { memcpy(copy, src->propertyValues[i], len); dest->propertyValues[i] = (const char *)copy; } else { rc = OMR_ERROR_OUT_OF_NATIVE_MEMORY; dest->propertyValues[i] = NULL; /* Release previously allocated strings */ for (size_t j = 0; j < i; j++) { omrmem_free_memory((void *)dest->propertyValues[j]); dest->propertyValues[j] = NULL; } break; } } } return rc; }
uint64_t incrementTraceCounter(UtModuleInfo *moduleInfo, UtComponentList *componentList, int32_t tracepoint) { UtComponentData *compData; OMRPORT_ACCESS_FROM_OMRPORT(OMR_TRACEGLOBAL(portLibrary)); if (moduleInfo == NULL) { /* this is an internal tracepoint */ UT_DBGOUT(2, ("<UT> incrementTraceCounter short circuit returning due to NULL compName\n")); return 0; } compData = getComponentDataForModule(moduleInfo, componentList); if (compData == NULL) { UT_DBGOUT(1, ("<UT> Unable to increment trace counter %s.%d - no component\n", moduleInfo->name, tracepoint)); return 0; } if (compData->moduleInfo == NULL) { UT_DBGOUT(1, ("<UT> Unable to increment trace counter %s.%d - no such loaded component\n", moduleInfo->name, tracepoint)); return 0; } if (compData->tracepointcounters == NULL) { /* first time anything in this component has been counted */ compData->tracepointcounters = (uint64_t *) omrmem_allocate_memory(sizeof(uint64_t) * compData->moduleInfo->count, OMRMEM_CATEGORY_TRACE); if (compData->tracepointcounters == NULL) { UT_DBGOUT(1, ("<UT> Unable to allocate trace counter buffers for %s\n", moduleInfo->name)); return 0; } memset(compData->tracepointcounters, 0, sizeof(uint64_t) * compData->moduleInfo->count); } return ++compData->tracepointcounters[tracepoint]; }
wchar_t * convertFromUTF8(OMRPortLibrary *portLibrary, const char *string, wchar_t *unicodeBuffer, uintptr_t unicodeBufferSize) { wchar_t *unicodeString; uintptr_t length; OMRPORT_ACCESS_FROM_OMRPORT(portLibrary); if (NULL == string) { return NULL; } length = (uintptr_t)strlen(string); if (length < unicodeBufferSize) { unicodeString = unicodeBuffer; } else { unicodeString = (wchar_t *)omrmem_allocate_memory((length + 1) * 2, OMRMEM_CATEGORY_PORT_LIBRARY); if (NULL == unicodeString) { return NULL; } } if (0 == MultiByteToWideChar(OS_ENCODING_CODE_PAGE, OS_ENCODING_MB_FLAGS, string, -1, unicodeString, (int)length + 1)) { omrerror_set_last_error(GetLastError(), OMRPORT_ERROR_OPFAILED); if (unicodeString != unicodeBuffer) { omrmem_free_memory(unicodeString); } return NULL; } return unicodeString; }
/** * Re-allocate memory. * * @param[in] portLibrary The port library * @param[in] memoryPointer Base address of memory to be re-allocated. * @param[in] byteAmount Number of bytes to re-allocated. * @param[in] callSite Allocation callsite, or NULL if the callsite from the original allocation should be inherited * @param[in] category Memory allocation category code * * @return pointer to memory on success, NULL on error or if byteAmount * is 0 and memoryPointer is non-NULL * * @note if memoryPointer is NULL, this function will behave like mem_allocate_memory * for the specified byteAmount * * @note If byteAmount is 0 and memoryPointer is non-NULL, this function will * behave like mem_free_memory for the specified memoryPointer and * returns NULL * * @note If this function is unable to allocate the requested memory, the original * memory is not freed and may still be used. * * @internal @warning Do not call error handling code @ref omrerror upon error as * the error handling code uses per thread buffers to store the last error. If memory * can not be allocated the result would be an infinite loop. */ void * omrmem_reallocate_memory(struct OMRPortLibrary *portLibrary, void *memoryPointer, uintptr_t byteAmount, const char *callSite, uint32_t category) { void *pointer = NULL; uintptr_t allocationByteAmount; reallocate_memory_func_t reallocateFunction = omrmem_reallocate_memory_basic; Trc_PRT_mem_omrmem_reallocate_memory_Entry(memoryPointer, byteAmount, callSite, category); if (memoryPointer == NULL) { pointer = omrmem_allocate_memory(portLibrary, byteAmount, NULL == callSite ? OMR_GET_CALLSITE() : callSite, category); } else if (byteAmount == 0) { omrmem_free_memory(portLibrary, memoryPointer); } else { memoryPointer = unwrapBlockAndCheckTags(portLibrary, memoryPointer); if (NULL == callSite) { /* Inherit the callsite from the original allocation */ callSite = ((J9MemTag *) memoryPointer)->callSite; } allocationByteAmount = ROUNDED_BYTE_AMOUNT(byteAmount); pointer = reallocateFunction(portLibrary, memoryPointer, allocationByteAmount); if (NULL != pointer) { pointer = wrapBlockAndSetTags(portLibrary, pointer, byteAmount, callSite, category); } if (NULL == pointer) { Trc_PRT_mem_omrmem_reallocate_memory_failed_2(callSite, memoryPointer, allocationByteAmount); } } Trc_PRT_mem_omrmem_reallocate_memory_Exit(pointer); return pointer; }
int32_t GCConfigTest::parseAttribute(AttributeElem **root, const char *attrStr) { OMRPORT_ACCESS_FROM_OMRPORT(gcTestEnv->portLib); int32_t rt = 0; const char DELIM = ','; char *copyOfAttrStr = (char *)omrmem_allocate_memory(strlen(attrStr) + 1, OMRMEM_CATEGORY_MM); char *attrStart = NULL; char *attrEnd = NULL; if (NULL == copyOfAttrStr) { rt = 1; omrtty_printf("%s:%d Failed to allocate native memory.\n", __FILE__, __LINE__); goto done; } strcpy(copyOfAttrStr, attrStr); attrStart = copyOfAttrStr; attrEnd = attrStart; while (NULL != attrEnd) { attrEnd = strchr(attrStart, DELIM); int32_t attr = 0; if (NULL != attrEnd) { *attrEnd = '\0'; attr = atoi(attrStart); attrStart = attrEnd + 1; } else { attr = atoi(attrStart); } AttributeElem *attrNode = (AttributeElem *)omrmem_allocate_memory(sizeof(AttributeElem), OMRMEM_CATEGORY_MM); if (NULL == attrNode) { rt = 1; omrtty_printf("%s:%d Failed to allocate native memory.\n", __FILE__, __LINE__); goto done; } attrNode->value = attr; J9_LINKED_LIST_ADD_LAST(*root, attrNode); } omrmem_free_memory(copyOfAttrStr); done: return rt; }
/** * This method is called to create a SupportThreadInfo for a test. It will populate the monitor and * rwmutex being used and zero out the counter * * @param functionsToRun an array of functions pointers. Each function will be run one in sequence synchronized * using the monitor within the SupporThreadInfo * @param numberFunctions the number of functions in the functionsToRun array * @returns a pointer to the newly created SupporThreadInfo */ SupportThreadInfo * createSupportThreadInfo(omrthread_entrypoint_t *functionsToRun, uintptr_t numberFunctions) { OMRPORT_ACCESS_FROM_OMRPORT(omrTestEnv->getPortLibrary()); SupportThreadInfo *info = (SupportThreadInfo *)omrmem_allocate_memory(sizeof(SupportThreadInfo), OMRMEM_CATEGORY_THREADS); info->readCounter = 0; info->writeCounter = 0; info->functionsToRun = functionsToRun; info->numberFunctions = numberFunctions; info->done = FALSE; omrthread_rwmutex_init((omrthread_rwmutex_t *)&info->handle, 0, "supportThreadInfo rwmutex"); omrthread_monitor_init_with_name(&info->synchronization, 0, "supportThreadAInfo monitor"); return info; }
OMRSpaceSaving * spaceSavingNew(OMRPortLibrary *portLibrary, uint32_t size) { OMRPORT_ACCESS_FROM_OMRPORT(portLibrary); OMRSpaceSaving *newSpaceSaving = omrmem_allocate_memory(sizeof(OMRSpaceSaving), OMRMEM_CATEGORY_MM); if (NULL == newSpaceSaving) { return NULL; } newSpaceSaving->portLib = portLibrary; newSpaceSaving->ranking = rankingNew(portLibrary, size); if (NULL == newSpaceSaving->ranking) { return NULL; } return newSpaceSaving; }
static void startChildThread(OMRTestVM *testVM, omrthread_t *childThread, omrthread_entrypoint_t entryProc, ChildThreadData **childData) { OMRPORT_ACCESS_FROM_OMRPORT(testVM->portLibrary); ChildThreadData *newChildData = (ChildThreadData *)omrmem_allocate_memory(sizeof(*newChildData), OMRMEM_CATEGORY_VM); ASSERT_FALSE(NULL == newChildData) << "Failed to alloc newChildData\n"; memset(newChildData, 0, sizeof(*newChildData)); newChildData->testVM = testVM; newChildData->childRc = OMR_ERROR_NONE; ASSERT_NO_FATAL_FAILURE(createThread(childThread, TRUE, J9THREAD_CREATE_JOINABLE, entryProc, newChildData)); *childData = newChildData; }
/** * Allocate memory for destination then copy the source into it. * * @param[in] portLibrary * @param[out] dest container for pointer to copied memory * @param[in] source string to be copied into dest */ static void allocateMemoryForAndCopyInto(struct OMRPortLibrary *portLibrary, char **dest, const char *source) { uintptr_t strLenPlusTerminator = 0; OMRPORT_ACCESS_FROM_OMRPORT(portLibrary); strLenPlusTerminator = strlen(source) + 1 /*null terminator */; *dest = (char *)omrmem_allocate_memory(strLenPlusTerminator, OMRMEM_CATEGORY_PORT_LIBRARY); if (*dest == NULL) { return; } else { strncpy(*dest, source, strLenPlusTerminator); } }
OMR_Agent * OMR_Agent::createAgent(OMR_VM *vm, char const *arg) { OMR_Agent *newAgent = NULL; if (NULL != arg) { OMRPORT_ACCESS_FROM_OMRVM(vm); newAgent = (OMR_Agent *)omrmem_allocate_memory(sizeof(*newAgent), OMRMEM_CATEGORY_VM); if (NULL != newAgent) { new(newAgent) OMR_Agent(vm, arg); if (INITIALIZED != newAgent->_state) { destroyAgent(newAgent); newAgent = NULL; } } } return newAgent; }
/* Utility function for parsing command line options */ static char * newSubString(const char *buffer, size_t ret) { OMRPORT_ACCESS_FROM_OMRPORT(OMR_TRACEGLOBAL(portLibrary)); char *temp = (char *)omrmem_allocate_memory(ret + 1, OMRMEM_CATEGORY_TRACE); UT_DBGOUT(2, ("<UT> newSubString: buffer %s size %d \n", buffer, ret)); if (temp == NULL) { /*fprintf(stderr, "newSubString error\n");*/ return NULL; } strncpy(temp, buffer, ret); temp[ret] = '\0'; UT_DBGOUT(2, ("<UT> newSubString: returning buffer %p \n", temp)); return temp; }
omr_error_t initialiseComponentList(UtComponentList **componentListPtr) { OMRPORT_ACCESS_FROM_OMRPORT(OMR_TRACEGLOBAL(portLibrary)); UtComponentList *componentList = (UtComponentList *)omrmem_allocate_memory(sizeof(UtComponentList), OMRMEM_CATEGORY_TRACE); UT_DBGOUT(2, ("<UT> initialiseComponentList: %p\n", componentListPtr)); if (componentList == NULL) { UT_DBGOUT(1, ("<UT> Unable to allocate component list\n")); return OMR_ERROR_OUT_OF_NATIVE_MEMORY; } initHeader(&componentList->header, UT_TRACE_COMPONENT_LIST, sizeof(UtComponentList)); componentList->head = NULL; componentList->deferredConfigInfoHead = NULL; *componentListPtr = componentList; UT_DBGOUT(2, ("<UT> initialiseComponentList: %p completed\n", componentListPtr)); return OMR_ERROR_NONE; }
bool MM_VerboseManagerImpl::configureVerboseGC(OMR_VM *omrVM, char *filename, uintptr_t fileCount, uintptr_t iterations) { OMRPORT_ACCESS_FROM_OMRVM(omrVM); if (!MM_VerboseManager::configureVerboseGC(omrVM, filename, fileCount, iterations)) { return false; } this->fileCount = fileCount; this->iterations = iterations; size_t len = strlen(filename); this->filename = (char *)omrmem_allocate_memory(len+1, OMRMEM_CATEGORY_MM); if (NULL == this->filename) { return false; } strcpy(this->filename, filename); return true; }
static omr_error_t startTestChildThread(OMRTestVM *testVM, OMR_VMThread *curVMThread, omrthread_t *childThread, TestChildThreadData **childData) { omr_error_t rc = OMR_ERROR_NONE; OMRPORT_ACCESS_FROM_OMRPORT(testVM->portLibrary); TestChildThreadData *newChildData = (TestChildThreadData *)omrmem_allocate_memory(sizeof(*newChildData), OMRMEM_CATEGORY_VM); if (NULL == newChildData) { rc = OMR_ERROR_OUT_OF_NATIVE_MEMORY; omrtty_printf("%s:%d ERROR: Failed to alloc newChildData\n", __FILE__, __LINE__); } if (OMR_ERROR_NONE == rc) { if (0 != omrthread_monitor_init_with_name(&newChildData->shutdownCond, 0, "traceTestChildShutdown")) { rc = OMR_ERROR_FAILED_TO_ALLOCATE_MONITOR; omrtty_printf("%s:%d ERROR: Failed to init shutdownCond monitor\n", __FILE__, __LINE__); omrmem_free_memory(newChildData); } else { newChildData->testVM = testVM; newChildData->isDead = FALSE; newChildData->childRc = OMR_ERROR_NONE; } } if (OMR_ERROR_NONE == rc) { if (0 != omrthread_create_ex( NULL, /* handle */ J9THREAD_ATTR_DEFAULT, /* attr */ FALSE, /* suspend */ childThreadMain, /* entrypoint */ newChildData) /* entryarg */ ) { rc = OMR_ERROR_OUT_OF_NATIVE_MEMORY; omrtty_printf("%s:%d ERROR: Failed to init shutdownCond monitor\n", __FILE__, __LINE__); omrthread_monitor_destroy(newChildData->shutdownCond); omrmem_free_memory(newChildData); } else { *childData = newChildData; } } return rc; }
omr_error_t omr_ras_initMethodDictionary(OMR_VM *vm, size_t numProperties, const char * const *propertyNames) { omr_error_t rc = OMR_ERROR_NONE; if (NULL == vm->_methodDictionary) { OMRPORT_ACCESS_FROM_OMRVM(vm); OMR_MethodDictionary *methodDictionary = (OMR_MethodDictionary *)omrmem_allocate_memory(sizeof(*methodDictionary), OMRMEM_CATEGORY_OMRTI); if (NULL != methodDictionary) { rc = methodDictionary->init(vm, numProperties, propertyNames); if (OMR_ERROR_NONE == rc) { vm->_methodDictionary = methodDictionary; } else { omrmem_free_memory(methodDictionary); } } else { rc = OMR_ERROR_OUT_OF_NATIVE_MEMORY; } } return rc; }
bool MM_VerboseManagerImpl::reconfigureVerboseGC(OMR_VM *omrVM) { OMRPORT_ACCESS_FROM_OMRVM(omrVM); /* If the pid is specified in the filename, then the pid of the * new process will be used during verbose reinitialization, * otherwise we append the pid of the child before the extension. */ WriterType type = parseWriterType(NULL, filename, 0, 0); /* All parameters other than filename aren't used */ if ( ((type == VERBOSE_WRITER_FILE_LOGGING_SYNCHRONOUS) || (type == VERBOSE_WRITER_FILE_LOGGING_BUFFERED)) && (NULL == strstr(filename, "%p")) && (NULL == strstr(filename, "%pid")) ) { #define MAX_PID_LENGTH 16 char pidStr[MAX_PID_LENGTH]; uintptr_t pid = omrsysinfo_get_pid(); int pidLen = snprintf(pidStr,MAX_PID_LENGTH, "_%lu",(long unsigned int)pid); /* Allocate new buffer */ char *newLog = (char *)omrmem_allocate_memory(pidLen+strlen(filename)+1, OMRMEM_CATEGORY_MM); /* Locate extension, if any */ char *extension = strchr(filename, '.'); if (NULL != extension) { size_t nameLen = extension - filename; size_t extLen = strlen(filename) - nameLen; strncpy(newLog, filename, nameLen); strncpy(newLog + nameLen, pidStr, pidLen); strncpy(newLog + nameLen + pidLen, extension, extLen); newLog[nameLen + pidLen + extLen] = '\0'; /* strncpy does NOT NULL terminate */ } else { size_t len = strlen(filename); strncpy(newLog,filename,len); newLog[len] = '\0'; /* strncpy does NOT NULL terminate */ strncat(newLog,pidStr,pidLen); /* strncat does NULL terminate */ } omrmem_free_memory(this->filename); filename = newLog; } return MM_VerboseManager::configureVerboseGC(omrVM, filename, 1, 0); }
static intptr_t translateModifiedUtf8ToPlatform(OMRPortLibrary *portLibrary, const char *inBuffer, uintptr_t inBufferSize, char **outBuffer) { OMRPORT_ACCESS_FROM_OMRPORT(portLibrary); char *result = 0; int32_t bufferLength = 0; int32_t resultLength = 0; *outBuffer = NULL; bufferLength = omrstr_convert(J9STR_CODE_MUTF8, J9STR_CODE_PLATFORM_RAW, inBuffer, inBufferSize, NULL, 0); /* get the size of the platform string */ if (bufferLength < 0) { return bufferLength; /* some error occurred */ } else { bufferLength += MAX_STRING_TERMINATOR_LENGTH; } result = (char *)omrmem_allocate_memory(bufferLength, OMRMEM_CATEGORY_PORT_LIBRARY); if (NULL == result) { return OMRPORT_ERROR_STRING_MEM_ALLOCATE_FAILED; } resultLength = omrstr_convert(J9STR_CODE_MUTF8, J9STR_CODE_PLATFORM_RAW, inBuffer, inBufferSize, result, bufferLength); /* do the conversion */ if (resultLength < 0) { omrmem_free_memory(result); return resultLength; /* some error occurred */ } else { memset(result + resultLength, 0, MAX_STRING_TERMINATOR_LENGTH); *outBuffer = result; return 0; } }
int32_t GCConfigTest::createObject(omrobjectptr_t *obj, char **objName, const char *namePrefix, OMRGCObjectType objType, int32_t depth, int32_t nthInRow, uintptr_t size) { OMRPORT_ACCESS_FROM_OMRPORT(gcTestEnv->portLib); int32_t rt = 0; ObjectEntry *objEntry = NULL; *objName = (char *)omrmem_allocate_memory(MAX_NAME_LENGTH, OMRMEM_CATEGORY_MM); if (NULL == *objName) { rt = 1; omrtty_printf("%s:%d Failed to allocate native memory.\n", __FILE__, __LINE__); goto done; } omrstr_printf(*objName, MAX_NAME_LENGTH, "%s_%d_%d", namePrefix, depth, nthInRow); if (0 == objectTable.find(&objEntry, *objName)) { #if defined(OMRGCTEST_DEBUG) omrtty_printf("Find object %s in hash table.\n", *objName); #endif *obj = objEntry->objPtr; } else { rt = allocateHelper(obj, *objName, size); OMRGCTEST_CHECK_RT(rt); /* keep object in table */ rt = objectTable.add(*objName, *obj, 0); OMRGCTEST_CHECK_RT(rt) /* Keep count of the new allocated non-garbage object size for garbage insertion. If the object exists in objectTable, its size is ignored. */ if ((ROOT == objType) || (NORMAL == objType)) { gp.accumulatedSize += env->getExtensions()->objectModel.getSizeInBytesWithHeader(*obj); } } done: return rt; }
/** * Converts command string array into a single unicode string command line * * @returns 0 upon success, negative portable error code upon failure */ static intptr_t getUnicodeCmdline(struct OMRPortLibrary *portLibrary, const char *command[], uintptr_t commandLength, wchar_t **unicodeCmdline) { char *needToBeQuoted = NULL; size_t length, l; intptr_t rc = 0; intptr_t i; char *ptr = NULL; const char *argi = NULL; char *commandAsString = NULL; OMRPORT_ACCESS_FROM_OMRPORT(portLibrary); /* * Windows needs a char* command line unlike regular C exec* functions * Therefore we need to rebuild the line that has been sliced in java... * Subtle : if a token embbeds a <space>, the token will be quoted (only * if it hasn't been quoted yet) The quote char is " * Note (see "XXX references in the code) * Our CDev scanner/parser does not handle '"' correctly. A workaround is to close * the '"' with another " , embedded in a C comment. */ needToBeQuoted = (char *)omrmem_allocate_memory(commandLength, OMRMEM_CATEGORY_PORT_LIBRARY); if (NULL == needToBeQuoted) { rc = OMRPROCESS_ERROR; } else { memset(needToBeQuoted, '\0', commandLength); length = commandLength; /*add 1 <blank> between each token + a reserved place for the last NULL*/ for (i = (intptr_t)commandLength; --i >= 0;) { intptr_t j; size_t commandILength; const char *commandStart; commandILength = strlen(command[i]); length += commandILength; /* check_for_embbeded_space */ if (commandILength > 0) { commandStart = command[i]; if (commandStart[0] != '"' /*"XXX*/) { for (j = 0; j < (intptr_t)commandILength ; j += 1) { if (commandStart[j] == ' ') { needToBeQuoted[i] = '\1'; /* a random value, different from zero though*/ length += 2; /* two quotes are added */ if (commandILength > 1 && commandStart[commandILength - 1] == '\\' && commandStart[commandILength - 2] != '\\') { length++; /* need to double slash */ } break; } } } } /* end of check_for_embedded_space */ } ptr = commandAsString = (char *)omrmem_allocate_memory(length, OMRMEM_CATEGORY_PORT_LIBRARY); if (NULL == commandAsString) { omrmem_free_memory(needToBeQuoted); rc = OMRPROCESS_ERROR; } else { uintptr_t k; for (k = 0; k < commandLength ; k += 1) { l = strlen(argi = command[k]); if (needToBeQuoted[k]) { *ptr++ = '"' /*"XXX*/ ; } memcpy(ptr, argi, l); ptr += l; if (needToBeQuoted[k]) { if (l > 1 && *(ptr - 1) == '\\' && *(ptr - 2) != '\\') { *ptr++ = '\\'; } *ptr++ = '"' /*"XXX*/ ; } *ptr++ = ' '; /* put a <blank> between each token */ } *(ptr - 1) = '\0'; /*commandLength > 0 ==> valid operation*/ omrmem_free_memory(needToBeQuoted); *unicodeCmdline = (wchar_t *)omrmem_allocate_memory((length + 1) * 2, OMRMEM_CATEGORY_PORT_LIBRARY); if (NULL == *unicodeCmdline) { rc = OMRPROCESS_ERROR; } else { convertFromUTF8(OMRPORTLIB, commandAsString, *unicodeCmdline, length + 1); omrmem_free_memory(commandAsString); } } } return rc; }
intptr_t j9process_create(OMRPortLibrary *portLibrary, const char *command[], uintptr_t commandLength, const char *dir, uint32_t options, OMRProcessHandle *processHandle) { OMRPORT_ACCESS_FROM_OMRPORT(portLibrary); intptr_t rc = 0; OMRProcessHandleStruct *processHandleStruct = NULL; #if defined(WIN32) STARTUPINFOW sinfo; PROCESS_INFORMATION pinfo; SECURITY_ATTRIBUTES sAttrib; wchar_t *unicodeDir = NULL; wchar_t *unicodeCmdline = NULL; wchar_t *unicodeEnv = NULL; OMRProcessWin32Pipes pipes; DWORD dwMode = PIPE_NOWAIT; /* it's only used if OMRPORT_PROCESS_NONBLOCKING_IO is set */ if (0 == commandLength) { return OMRPROCESS_ERROR; } processHandleStruct = (OMRProcessHandleStruct *)omrmem_allocate_memory(sizeof(OMRProcessHandleStruct), OMRMEM_CATEGORY_PORT_LIBRARY); ZeroMemory(&sinfo, sizeof(sinfo)); ZeroMemory(&pinfo, sizeof(pinfo)); ZeroMemory(&sAttrib, sizeof(sAttrib)); sinfo.cb = sizeof(sinfo); sinfo.dwFlags = STARTF_USESTDHANDLES; /* Allow handle inheritance */ sAttrib.bInheritHandle = 1; sAttrib.nLength = sizeof(sAttrib); ZeroMemory(&pipes, sizeof(OMRProcessWin32Pipes)); // Create input pipe if (0 == CreatePipe(&(pipes.inR), &(pipes.inW), &sAttrib, 512)) { closeAndDestroyPipes(&pipes); return OMRPROCESS_ERROR; } else { if (0 == DuplicateHandle(GetCurrentProcess(), pipes.inW, GetCurrentProcess(), &(pipes.inDup), 0, FALSE, DUPLICATE_SAME_ACCESS)) { closeAndDestroyPipes(&pipes); return OMRPROCESS_ERROR; } if (0 == CloseHandle(pipes.inW)) { closeAndDestroyPipes(&pipes); return OMRPROCESS_ERROR; } pipes.inW = NULL; } sinfo.hStdInput = pipes.inR; processHandleStruct->inHandle = (intptr_t)pipes.inDup; if (OMRPROCESS_DEBUG == options) { processHandleStruct->outHandle = OMRPROCESS_INVALID_FD; if (sinfo.dwFlags == STARTF_USESTDHANDLES) { sinfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); } processHandleStruct->errHandle = OMRPROCESS_INVALID_FD; if (sinfo.dwFlags == STARTF_USESTDHANDLES) { sinfo.hStdError = GetStdHandle(STD_ERROR_HANDLE); } } else { // Create output pipe if (0 == CreatePipe(&(pipes.outR), &(pipes.outW), &sAttrib, 512)) { closeAndDestroyPipes(&pipes); return OMRPROCESS_ERROR; } else { if (0 == DuplicateHandle(GetCurrentProcess(), pipes.outR, GetCurrentProcess(), &(pipes.outDup), 0, FALSE, DUPLICATE_SAME_ACCESS)) { closeAndDestroyPipes(&pipes); return OMRPROCESS_ERROR; } if (0 == CloseHandle(pipes.outR)) { closeAndDestroyPipes(&pipes); return OMRPROCESS_ERROR; } pipes.outR = NULL; } processHandleStruct->outHandle = (intptr_t)pipes.outDup; sinfo.hStdOutput = pipes.outW; // create error pipe if (0 == CreatePipe(&(pipes.errR), &(pipes.errW), &sAttrib, 512)) { closeAndDestroyPipes(&pipes); return OMRPROCESS_ERROR; } else { if (0 == DuplicateHandle(GetCurrentProcess(), pipes.errR, GetCurrentProcess(), &(pipes.errDup), 0, FALSE, DUPLICATE_SAME_ACCESS)) { closeAndDestroyPipes(&pipes); return OMRPROCESS_ERROR; } if (NULL != pipes.errR) { if (0 == CloseHandle(pipes.errR)) { closeAndDestroyPipes(&pipes); return OMRPROCESS_ERROR; } } pipes.errR = NULL; } processHandleStruct->errHandle = (intptr_t)pipes.errDup; sinfo.hStdError = pipes.errW; } if (0 == rc) { rc = getUnicodeCmdline(OMRPORTLIB, command, commandLength, &unicodeCmdline); if (0 == rc) { if (NULL != dir) { size_t length = strlen(dir); unicodeDir = (wchar_t *)omrmem_allocate_memory((length + 1) * 2, OMRMEM_CATEGORY_PORT_LIBRARY); if (NULL == unicodeDir) { rc = OMRPROCESS_ERROR; } else { convertFromUTF8(OMRPORTLIB, dir, unicodeDir, length + 1); } } if (0 == rc) { DWORD creationFlags = CREATE_UNICODE_ENVIRONMENT; if (0 == CreateProcessW( /* CreateProcessW returns 0 upon failure */ NULL, unicodeCmdline, NULL, NULL, TRUE, creationFlags, /* add DEBUG_ONLY_THIS_PROCESS for smoother debugging */ unicodeEnv, unicodeDir, &sinfo, &pinfo) /* Pointer to PROCESS_INFORMATION structure; */ ) { rc = OMRPROCESS_ERROR; } else { processHandleStruct->procHandle = (intptr_t) pinfo.hProcess; /* Close Handles passed to child if there is any */ CloseHandle(pipes.inR); if (OMRPROCESS_DEBUG != options) { CloseHandle(pipes.outW); CloseHandle(pipes.errW); } CloseHandle(pinfo.hThread); /* Implicitly created, a leak otherwise*/ } } } } if (NULL != unicodeCmdline) { omrmem_free_memory(unicodeCmdline); } if (NULL != unicodeDir) { omrmem_free_memory(unicodeDir); } if (NULL != unicodeEnv) { omrmem_free_memory(unicodeEnv); } if (0 != rc) { /* If an error occurred close all handles */ closeAndDestroyPipes(&pipes); } else { *processHandle = processHandleStruct; } return rc; #else /* defined(WIN32) */ const char *cmd = NULL; int grdpid; unsigned int i; int newFD[3][2]; int forkedChildProcess[2]; int pipeFailed = 0; int errorNumber = 0; char **newCommand; uintptr_t newCommandSize; if (0 == commandLength) { return OMRPROCESS_ERROR; } for (i = 0; i < 3; i += 1) { newFD[i][0] = OMRPROCESS_INVALID_FD; newFD[i][1] = OMRPROCESS_INVALID_FD; } /* Build the new io pipes (in/out/err) */ if (-1 == pipe(newFD[0])) { pipeFailed = 1; } if (OMRPROCESS_DEBUG != options) { if (-1 == pipe(newFD[1])) { pipeFailed = 1; } if (-1 == pipe(newFD[2])) { pipeFailed = 1; } } if (-1 == pipe(forkedChildProcess)) { /* pipe for synchronization */ forkedChildProcess[0] = OMRPROCESS_INVALID_FD; forkedChildProcess[1] = OMRPROCESS_INVALID_FD; pipeFailed = 1; } if (pipeFailed) { for (i = 0; i < 3; i++) { if (OMRPROCESS_INVALID_FD != newFD[i][0]) { if (-1 != rc) { rc = close(newFD[i][0]); } if (-1 != rc) { rc = close(newFD[i][1]); } } } if (OMRPROCESS_INVALID_FD != forkedChildProcess[0]) { if (-1 != rc) { rc = close(forkedChildProcess[0]); } if (-1 != rc) { rc = close(forkedChildProcess[1]); } } return OMRPROCESS_ERROR; } if (-1 != rc) { rc = setFdCloexec(forkedChildProcess[0]); } if (-1 != rc) { rc = setFdCloexec(forkedChildProcess[1]); } /* Create a NULL terminated array to contain the command for call to execv[p|e]() * We could do this in the child, but if mem_allocate_memory fails, it's easier to diagnose * failure in parent. Remember to free the memory */ newCommandSize = (commandLength + 1) * sizeof(uintptr_t); newCommand = (char **)omrmem_allocate_memory(newCommandSize, OMRMEM_CATEGORY_PORT_LIBRARY); if (NULL == newCommand) { return OMRPROCESS_ERROR; } memset(newCommand, 0, newCommandSize); for (i = 0 ; i < commandLength; i += 1) { #if defined(OSX) newCommand[i] = (char *)omrmem_allocate_memory(strlen(command[i]) + 1, OMRMEM_CATEGORY_PORT_LIBRARY); omrstr_printf(newCommand[i], strlen(command[i]) + 1, command[i]); #else /* defined(OSX) */ intptr_t translateStatus = translateModifiedUtf8ToPlatform(OMRPORTLIB, command[i], strlen(command[i]), &(newCommand[i])); if (0 != translateStatus) { unsigned int j = 0; /* most likely out of memory, free the strings we just converted. */ for (j = 0; j < i; j += 1) { if (NULL != newCommand[j]) { omrmem_free_memory(newCommand[j]); } } return translateStatus; } #endif /* defined(OSX) */ } cmd = newCommand[0]; newCommand[commandLength] = NULL; grdpid = fork(); if (grdpid == 0) { /* Child process */ char dummy = '\0'; /* Redirect pipes so grand-child inherits new pipes */ rc = dup2(newFD[0][0], 0); if (-1 != rc) { if (OMRPROCESS_DEBUG != options) { rc = dup2(newFD[1][1], 1); if (-1 != rc) { rc = dup2(newFD[2][1], 2); } } } /* tells the parent that that very process is running */ rc = write(forkedChildProcess[1], (void *) &dummy, 1); if ((-1 != rc) && dir) { rc = chdir(dir); } if (-1 != rc) { /* Try to perform the execv; on success, it does not return */ rc = execvp(cmd, newCommand); } /* If we get here, tell the parent that the execv failed! Send the error number. */ if (-1 != rc) { rc = write(forkedChildProcess[1], &errno, sizeof(errno)); } if (-1 != rc) { rc = close(forkedChildProcess[0]); } if (-1 != rc) { rc = close(forkedChildProcess[1]); } /* If the exec failed, we must exit or there will be two VM processes running. */ exit(rc); } else { /* In the parent process */ char dummy; for (i = 0; i < commandLength; i++) { if (NULL != newCommand[i]) { omrmem_free_memory(newCommand[i]); } } omrmem_free_memory(newCommand); if ((OMRPROCESS_INVALID_FD != newFD[0][0]) && (-1 != rc)) { rc = close(newFD[0][0]); } if ((OMRPROCESS_INVALID_FD != newFD[1][1]) && (-1 != rc)) { rc = close(newFD[1][1]); } if ((OMRPROCESS_INVALID_FD != newFD[2][1]) && (-1 != rc)) { rc = close(newFD[2][1]); } if (grdpid == -1) { /* the fork failed */ /* close the open pipes */ if (-1 != rc) { rc = close(forkedChildProcess[0]); } if (-1 != rc) { rc = close(forkedChildProcess[1]); } if ((OMRPROCESS_INVALID_FD != newFD[0][1]) && (-1 != rc)) { rc = close(newFD[0][1]); } if ((OMRPROCESS_INVALID_FD != newFD[1][0]) && (-1 != rc)) { rc = close(newFD[1][0]); } if ((OMRPROCESS_INVALID_FD != newFD[2][0]) && (-1 != rc)) { rc = close(newFD[2][0]); } return OMRPROCESS_ERROR; } /* Store the rw handles to the childs io */ processHandleStruct = (OMRProcessHandleStruct *)omrmem_allocate_memory(sizeof(OMRProcessHandleStruct), OMRMEM_CATEGORY_PORT_LIBRARY); processHandleStruct->inHandle = (intptr_t)newFD[0][1]; if (OMRPROCESS_DEBUG == options) { processHandleStruct->outHandle = OMRPROCESS_INVALID_FD; processHandleStruct->errHandle = OMRPROCESS_INVALID_FD; } else { processHandleStruct->outHandle = (intptr_t)newFD[1][0]; processHandleStruct->errHandle = (intptr_t)newFD[2][0]; } processHandleStruct->procHandle = (intptr_t)grdpid; processHandleStruct->pid = (int32_t)grdpid; /* let the forked child start. */ rc = close(forkedChildProcess[1]); if (-1 != rc) { rc = read(forkedChildProcess[0], &dummy, 1); } /* [PR CMVC 143339] * Instead of using timeout to determine if child process has been created successfully, * a single block read/write pipe call is used, if process creation failed, errorNumber * with sizeof(errno) will be returned, otherwise, read will fail due to pipe closure. */ if (-1 != rc) { rc = read(forkedChildProcess[0], &errorNumber, sizeof(errno)); } if (-1 != rc) { rc = close(forkedChildProcess[0]); } if (rc == sizeof(errno)) { return OMRPROCESS_ERROR; } *processHandle = processHandleStruct; return rc; } return OMRPROCESS_ERROR; #endif /* defined(WIN32) */ }
omr_error_t setTracePointsToParsed(const char *componentName, UtComponentList *componentList, int32_t all, int32_t first, int32_t last, unsigned char value, int level, const char *groupName, BOOLEAN suppressMessages, int32_t setActive) { const char *tempstr = NULL; char *newstr = NULL; char *newstr2 = NULL; size_t namelength = 0; size_t groupnamelength = 0; omr_error_t rc = OMR_ERROR_NONE; OMRPORT_ACCESS_FROM_OMRPORT(OMR_TRACEGLOBAL(portLibrary)); UT_DBGOUT(2, ("<UT> setTracePointsToParsed: %s\n", componentName)); if (strchr(componentName, '.') != NULL) { /* we have a range of tracepoints */ rc = parseAndSetTracePointsInRange(componentName, value, setActive, suppressMessages); /* can't combine ranges and levels etc, so just duck out now */ return rc; } if ((tempstr = strchr(componentName, '{')) != NULL || (tempstr = strchr(componentName, '(')) != NULL) { char closingBrace; UT_DBGOUT(2, ("<UT> setTracePointsTo: has detected a suboption %s in %s\n", tempstr, componentName)); namelength = tempstr - componentName; if (*tempstr == '{') { closingBrace = '}'; } else { closingBrace = ')'; } /* Look for empty braces - this is always an error */ if (*(tempstr + 1) == closingBrace) { reportCommandLineError(suppressMessages, "Error: found empty braces or parentheses"); return OMR_ERROR_ILLEGAL_ARGUMENT; } /* Look for unclosed braces - another error */ if (strchr(tempstr, closingBrace) == NULL) { reportCommandLineError(suppressMessages, "Error: unclosed braces or parentheses"); return OMR_ERROR_ILLEGAL_ARGUMENT; } tempstr++; /* points at first option char now */ if (0 == j9_cmdla_strnicmp(tempstr, UT_LEVEL_KEYWORD, strlen(UT_LEVEL_KEYWORD)) || *tempstr == 'l' || *tempstr == 'L') { while (!isdigit(*tempstr)) { if (*tempstr == ',' || *tempstr == '}' || *tempstr == '\0') { reportCommandLineError(suppressMessages, "Trace level required without an integer level specifier"); return OMR_ERROR_ILLEGAL_ARGUMENT; } tempstr++; } sscanf(tempstr, "%d", &level); newstr = (char *)omrmem_allocate_memory(namelength + 1, OMRMEM_CATEGORY_TRACE); if (newstr == NULL) { UT_DBGOUT(1, ("<UT> Unable to set tracepoints in %s - can't allocate tempname info\n", componentName)); return OMR_ERROR_OUT_OF_NATIVE_MEMORY; } strncpy(newstr, componentName, namelength); newstr[namelength] = '\0'; UT_DBGOUT(2, ("<UT> setTracePointsTo: Level detected %d in %s\n", level, newstr)); componentName = newstr; /* actual configuration of the component will now fall through to below */ } else { /* must be a group name note - types are now groups */ UT_DBGOUT(2, ("<UT> setTracePointsTo: A Group detected \n")); newstr = (char *)omrmem_allocate_memory(namelength + 1, OMRMEM_CATEGORY_TRACE); if (newstr == NULL) { UT_DBGOUT(1, ("<UT> Unable to set tracepoints in %s - can't allocate tempname info\n", componentName)); return OMR_ERROR_OUT_OF_NATIVE_MEMORY; } strncpy(newstr, componentName, namelength); newstr[namelength] = '\0'; groupnamelength = strlen(componentName) - namelength; newstr2 = (char *)omrmem_allocate_memory(groupnamelength - 1, OMRMEM_CATEGORY_TRACE); if (newstr2 == NULL) { UT_DBGOUT(1, ("<UT> Unable to set tracepoints in %s - can't allocate tempname info\n", componentName)); return OMR_ERROR_OUT_OF_NATIVE_MEMORY; } strncpy(newstr2, componentName + namelength + 1, groupnamelength - 2); newstr2[groupnamelength - 2] = '\0'; groupName = newstr2; UT_DBGOUT(2, ("<UT> setTracePointsTo: Group %s detected in %s\n", groupName, newstr)); componentName = newstr; } } rc = setTracePointsForComponent(componentName, componentList, all, first, last, value, level, groupName, suppressMessages, setActive); /* tidy up */ if (newstr != NULL) { omrmem_free_memory(newstr); } if (newstr2 != NULL) { omrmem_free_memory(newstr2); } return rc; }
omr_error_t setTracePointsTo(const char *componentName, UtComponentList *componentList, int32_t all, int32_t first, int32_t last, unsigned char value, int level, const char *groupName, BOOLEAN suppressMessages, int32_t setActive) { const char *tempstr = NULL; char *compNamesBuffer; int32_t stripbraces = FALSE; size_t namelength = 0; omr_error_t rc = OMR_ERROR_NONE; OMRPORT_ACCESS_FROM_OMRPORT(OMR_TRACEGLOBAL(portLibrary)); if (componentName == NULL) { reportCommandLineError(suppressMessages, "Can't set tracepoints for NULL componentName"); return OMR_ERROR_ILLEGAL_ARGUMENT; } if (componentList == NULL) { UT_DBGOUT(1, ("<UT> can't set tracepoints against NULL componentList\n")); return OMR_ERROR_ILLEGAL_ARGUMENT; } UT_DBGOUT(1, ("<UT> setTracePointsTo: component %s all= %s first=%d last=%d value=%d\n", componentName, (all ? "TRUE" : "FALSE"), first, last)); /* check for multiple components */ tempstr = strchr(componentName, ','); if (NULL != tempstr) { /* we have been passed multiple component names */ UT_DBGOUT(2, ("<UT> setTracePointsTo found component list: %s\n", componentName)); if (componentName[0] == '{') { /* option is a list of components enclosed in braces */ componentName++; stripbraces = TRUE; } else if ((0 == j9_cmdla_strnicmp(componentName, UT_TPNID, strlen(UT_TPNID))) && (tempstr < strchr(componentName, '}'))) { /* option is 'tpnid' followed by a list of components enclosed in braces */ componentName += (int)strlen(UT_TPNID) + 1; /* skip over the prefix and the open brace */ stripbraces = TRUE; } /* separate and process the first component name */ namelength = tempstr - componentName; compNamesBuffer = (char *)omrmem_allocate_memory(strlen(componentName) + 1, OMRMEM_CATEGORY_TRACE); if (compNamesBuffer == NULL) { UT_DBGOUT(1, ("<UT> Unable to set tracepoints in %s - can't allocate tempname info\n", componentName)); return OMR_ERROR_OUT_OF_NATIVE_MEMORY; } strncpy(compNamesBuffer, componentName, namelength); compNamesBuffer[namelength] = '\0'; /* replace the comma and terminate the string */ rc = setTracePointsToParsed(compNamesBuffer, componentList, all, first, last, value, level, groupName, suppressMessages, setActive); if (rc != OMR_ERROR_NONE) { omrmem_free_memory(compNamesBuffer); return rc; } componentName += namelength + 1; namelength = strlen(componentName); strcpy(compNamesBuffer, componentName); compNamesBuffer[namelength] = '\0'; if (stripbraces == TRUE) { compNamesBuffer[namelength - 1] = '\0'; /* blat the close brace out of the allocated copy */ } /* recurse here in case the remainder is still a comma separated list */ rc = setTracePointsTo(compNamesBuffer, componentList, all, first, last, value, level, groupName, suppressMessages, setActive); omrmem_free_memory(compNamesBuffer); return rc; } /* if we get here we have a single option, but it might still be enclosed by braces */ if (componentName[0] == '{') { componentName++; compNamesBuffer = (char *)omrmem_allocate_memory(strlen(componentName) + 1, OMRMEM_CATEGORY_TRACE); if (compNamesBuffer == NULL) { UT_DBGOUT(1, ("<UT> Unable to set tracepoints in %s - can't allocate tempname info\n", componentName)); return OMR_ERROR_OUT_OF_NATIVE_MEMORY; } namelength = strlen(componentName); strcpy(compNamesBuffer, componentName); compNamesBuffer[namelength - 1] = '\0'; /* blat the close brace out of the allocated copy */ rc = setTracePointsToParsed(compNamesBuffer, componentList, all, first, last, value, level, groupName, suppressMessages, setActive); omrmem_free_memory(compNamesBuffer); } else { rc = setTracePointsToParsed(componentName, componentList, all, first, last, value, level, groupName, suppressMessages, setActive); } return rc; }
omr_error_t initialiseComponentData(UtComponentData **componentDataPtr, UtModuleInfo *moduleInfo, const char *componentName) { OMRPORT_ACCESS_FROM_OMRPORT(OMR_TRACEGLOBAL(portLibrary)); UtComponentData *componentData = (UtComponentData *)omrmem_allocate_memory(sizeof(UtComponentData), OMRMEM_CATEGORY_TRACE); UT_DBGOUT(2, ("<UT> initialiseComponentData: %s\n", componentName)); if (componentData == NULL) { UT_DBGOUT(1, ("<UT> Unable to allocate componentData for %s\n", componentName)); return OMR_ERROR_OUT_OF_NATIVE_MEMORY; } initHeader(&componentData->header, UT_TRACE_COMPONENT_DATA, sizeof(UtComponentData)); componentData->componentName = (char *)omrmem_allocate_memory(strlen(componentName) + 1, OMRMEM_CATEGORY_TRACE); if (componentData->componentName == NULL) { UT_DBGOUT(1, ("<UT> Unable to allocate componentData's name field for %s\n", componentName)); return OMR_ERROR_OUT_OF_NATIVE_MEMORY; } strcpy(componentData->componentName, componentName); /* Setup the fully qualified name here as when we come to print out trace counters the modules may have been * freed already. */ if (moduleInfo->traceVersionInfo->traceVersion >= 7 && moduleInfo->containerModule != NULL) { char qualifiedName[MAX_QUALIFIED_NAME_LENGTH]; omrstr_printf(qualifiedName, MAX_QUALIFIED_NAME_LENGTH, "%s(%s)", moduleInfo->name, moduleInfo->containerModule->name); componentData->qualifiedComponentName = (char *)omrmem_allocate_memory(strlen(qualifiedName) + 1, OMRMEM_CATEGORY_TRACE); if (componentData->qualifiedComponentName == NULL) { UT_DBGOUT(1, ("<UT> Unable to allocate componentData's name field for %s\n", componentName)); return OMR_ERROR_OUT_OF_NATIVE_MEMORY; } strcpy(componentData->qualifiedComponentName, qualifiedName); } else { componentData->qualifiedComponentName = componentData->componentName; } if (moduleInfo->formatStringsFileName != NULL) { componentData->formatStringsFileName = (char *)omrmem_allocate_memory(strlen(moduleInfo->formatStringsFileName) + 1, OMRMEM_CATEGORY_TRACE); if (componentData->formatStringsFileName == NULL) { UT_DBGOUT(1, ("<UT> Unable to allocate componentData's format strings file name field for %s\n", componentName)); return OMR_ERROR_OUT_OF_NATIVE_MEMORY; } strcpy(componentData->formatStringsFileName, moduleInfo->formatStringsFileName); } else { componentData->formatStringsFileName = NULL; } componentData->moduleInfo = moduleInfo; componentData->tracepointCount = moduleInfo->count; componentData->numFormats = 0; componentData->tracepointFormattingStrings = NULL; componentData->tracepointcounters = NULL; componentData->alreadyfailedtoloaddetails = 0; componentData->next = NULL; componentData->prev = NULL; *componentDataPtr = componentData; UT_DBGOUT(2, ("<UT> initialiseComponentData complete: %s\n", componentName)); return OMR_ERROR_NONE; }
omr_error_t setTracePointGroupTo(const char *groupName, UtComponentData *componentData, unsigned char value, BOOLEAN suppressMessages, int32_t setActive) { UtGroupDetails *groupDetails; char *tempgrpname; const char *tempstr; size_t gnamelength; omr_error_t rc = OMR_ERROR_NONE; int i = 0; int32_t tpid = 0; BOOLEAN groupFound = FALSE; OMRPORT_ACCESS_FROM_OMRPORT(OMR_TRACEGLOBAL(portLibrary)); if (componentData == NULL) { UT_DBGOUT(1, ("<UT> setTracePointGroupTo called with invalid componentData\n")); return OMR_ERROR_ILLEGAL_ARGUMENT; } if (componentData->moduleInfo == NULL) { UT_DBGOUT(1, ("<UT> setTracePointGroupTo called on unregistered component: %s\n", componentData->componentName)); return OMR_ERROR_ILLEGAL_ARGUMENT; } if (componentData->moduleInfo->groupDetails == NULL) { reportCommandLineError(suppressMessages, "Groups not supported in component %s", componentData->componentName); return OMR_ERROR_ILLEGAL_ARGUMENT; } UT_DBGOUT(2, ("<UT> setTraceGroupTo called: groupname %s compdata %p\n", groupName, componentData)); if ((tempstr = strchr(groupName, ';')) != NULL) { gnamelength = strlen(groupName); tempgrpname = (char *)omrmem_allocate_memory(gnamelength + 1, OMRMEM_CATEGORY_TRACE); if (tempgrpname == NULL) { UT_DBGOUT(1, ("<UT> can't allocate temp group name\n")); return OMR_ERROR_OUT_OF_NATIVE_MEMORY; } strncpy(tempgrpname, groupName, (tempstr - groupName)); tempgrpname[(tempstr - groupName)] = '\0'; rc = setTracePointGroupTo(tempgrpname, componentData, value, suppressMessages, setActive); if (OMR_ERROR_NONE != rc) { omrmem_free_memory(tempgrpname); return rc; } strncpy(tempgrpname, tempstr + 1, gnamelength - (tempstr - groupName)); tempgrpname[(gnamelength - (tempstr - groupName))] = '\0'; rc = setTracePointGroupTo(tempgrpname, componentData, value, suppressMessages, setActive); omrmem_free_memory(tempgrpname); return rc; } UT_DBGOUT(2, ("<UT> setTraceGroupTo called: groupname %s component %s\n", groupName, componentData->componentName)); groupDetails = componentData->moduleInfo->groupDetails; while (groupDetails != NULL) { if (!j9_cmdla_strnicmp(groupName, groupDetails->groupName, strlen(groupDetails->groupName))) { /* found the group, now enable its tracepoints */ groupFound = TRUE; for (i = 0 ; i < groupDetails->count ; i++) { tpid = groupDetails->tpids[i]; updateActiveArray(componentData, tpid, tpid, value, setActive); } } groupDetails = groupDetails->next; } if (groupFound) { return OMR_ERROR_NONE; } else { reportCommandLineError(suppressMessages, "There is no group %s in component %s", groupName, componentData->moduleInfo->name); return OMR_ERROR_ILLEGAL_ARGUMENT; } }
static omr_error_t loadFormatStringsForComponent(UtComponentData *componentData) { omr_error_t rc = OMR_ERROR_NONE; intptr_t formatFileFD = -1; int numFormats = componentData->tracepointCount; char **formatStringsComponentArray = NULL; int formatStringsComponentArraySize = 0; char *tempPtr, *tempPtr2; int currenttp = 0; char compName[1024]; char *fileContents = NULL; int64_t fileSize; float datFileVersion; const unsigned int componentNameLength = (const unsigned int)strlen(componentData->componentName); OMRPORT_ACCESS_FROM_OMRPORT(OMR_TRACEGLOBAL(portLibrary)); UT_DBGOUT(2, ("<UT> loadFormatStringsForComponent %s\n", componentData->componentName)); if (componentData->alreadyfailedtoloaddetails != 0) { UT_DBGOUT(2, ("<UT> loadFormatStringsForComponent %s returning due to previous load failure\n", componentData->componentName)); return OMR_ERROR_INTERNAL; } UT_DBGOUT(2, ("<UT> loadFormatStringsForComponent %s parsing filename = %s\n", componentData->componentName, componentData->formatStringsFileName)); /* buffer format files into memory at some point */ if (fileContents == NULL) { /* look in jre/lib directory first */ UT_DBGOUT(1, ("<UT> loadFormatStringsForComponent trying to load = %s\n", componentData->formatStringsFileName)); formatFileFD = openFileFromDirectorySearchList(OMR_TRACEGLOBAL(traceFormatSpec), componentData->formatStringsFileName, EsOpenText | EsOpenRead, 0); if (formatFileFD == -1) { UT_DBGOUT(1, ("<UT> loadFormatStringsForComponent can't load = %s, from current directory either - marking it unfindeable\n", componentData->formatStringsFileName)); rc = OMR_ERROR_INTERNAL; goto epilogue; } fileSize = omrfile_flength(formatFileFD); if (fileSize < 0) { UT_DBGOUT(1, ("<UT> error getting file size for file %s\n", componentData->formatStringsFileName)); rc = OMR_ERROR_INTERNAL; goto epilogue; } fileContents = (char *)omrmem_allocate_memory((uintptr_t)fileSize + 1, OMRMEM_CATEGORY_TRACE); if (fileContents == NULL) { UT_DBGOUT(1, ("<UT> can't allocate buffer\n")); rc = OMR_ERROR_OUT_OF_NATIVE_MEMORY; goto epilogue; } if (omrfile_read(formatFileFD, fileContents, (int32_t)fileSize) != (int32_t) fileSize) { UT_DBGOUT(1, ("<UT> can't read the file into the buffer\n")); rc = OMR_ERROR_INTERNAL; goto epilogue; } omrfile_close(formatFileFD); formatFileFD = -1; /* if running on z/os convert to ascii */ fileContents[fileSize] = '\0'; twE2A(fileContents); } datFileVersion = getDatFileVersion(fileContents); if (datFileVersion == 0.0F) { UT_DBGOUT(1, ("<UT> dat file version error.\n")); rc = OMR_ERROR_INTERNAL; goto epilogue; } formatStringsComponentArraySize = numFormats * sizeof(char *); formatStringsComponentArray = (char **)omrmem_allocate_memory(formatStringsComponentArraySize, OMRMEM_CATEGORY_TRACE); if (formatStringsComponentArray == NULL) { UT_DBGOUT(1, ("<UT> can't allocate formatStrings array\n")); rc = OMR_ERROR_OUT_OF_NATIVE_MEMORY; goto epilogue; } /* parse the file */ currenttp = 0; tempPtr = fileContents; while (currenttp < numFormats) { if (*tempPtr == '\n') { unsigned int tempLen; tempPtr2 = ++tempPtr; /* points to start of next line */ /* read to the next space */ while (*tempPtr2 != ' ') { tempPtr2++; } tempLen = (unsigned int)(tempPtr2 - tempPtr); strncpy(compName, tempPtr, tempLen); compName[tempLen] = '\0'; if (datFileVersion >= 5.1F) { /* then the first word in each dat file line is of the form * componentName.integerID, which is all currently stored in comp name */ char *period = strchr(compName, '.'); if (period == NULL) { UT_DBGOUT(1, ("<UT> error parsing 5.1 dat file component name: [%.*s].\n", tempLen, tempPtr)); } else { *period = '\0'; /* replace the '.' with a '\0' to make compname coherent */ tempLen = (unsigned int)(period - compName); ++period; } } tempPtr = tempPtr2; if ((tempLen == componentNameLength) && (0 == strncmp(compName, componentData->componentName, tempLen))) { /* skip the next four fields: type number, overhead, level and explicit. e.g. " 0 1 1 N " */ int field; for (field = 0; field < 4; field++) { while (*++tempPtr != ' '); } tempPtr++; tempPtr2 = tempPtr; /* find the end of the trace point name */ while (*tempPtr2 != ' ') { tempPtr2++; } tempLen = (unsigned int)(tempPtr2 - tempPtr); /* Skip the name as we don't use them. */ tempPtr2 += 2; /* skip space and open quote */ tempPtr = tempPtr2; /* find the end of the tracepoint's format string */ while (*tempPtr2 != '\"') { tempPtr2++; } tempLen = (unsigned int)(tempPtr2 - tempPtr); formatStringsComponentArray[currenttp] = (char *)omrmem_allocate_memory(tempLen + 1, OMRMEM_CATEGORY_TRACE); if (formatStringsComponentArray[currenttp] == NULL) { UT_DBGOUT(1, ("<UT> can't allocate a format string\n")); rc = OMR_ERROR_OUT_OF_NATIVE_MEMORY; goto epilogue; } else { strncpy(formatStringsComponentArray[currenttp], tempPtr, tempLen); formatStringsComponentArray[currenttp][tempLen] = '\0'; } tempPtr = tempPtr2; /* up to the end of line */ currenttp++; } } tempPtr++; if (tempPtr >= (fileContents + fileSize - 1)) { /* fill any remaining positions in the arrays so they can be dereferenced */ for (; currenttp < numFormats; currenttp++) { formatStringsComponentArray[currenttp] = (char *)UT_MISSING_TRACE_FORMAT; } break; } } componentData->numFormats = numFormats; componentData->tracepointFormattingStrings = formatStringsComponentArray; epilogue: if (OMR_ERROR_NONE != rc) { int i = 0; componentData->alreadyfailedtoloaddetails = 1; /* don't try and load them again next time */ if (formatStringsComponentArray != NULL) { for (i = 0; i < formatStringsComponentArraySize; i++) { if (formatStringsComponentArray[i] != NULL) { omrmem_free_memory(formatStringsComponentArray[i]); } else { break; } } omrmem_free_memory(formatStringsComponentArray); } if (formatFileFD != -1) { omrfile_close(formatFileFD); } } if (fileContents != NULL) { omrmem_free_memory(fileContents); } return rc; }